xt_CONNSECMARK.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * This module is used to copy security markings from packets
 * to connections, and restore security markings from connections
 * back to packets.  This would normally be performed in conjunction
 * with the SECMARK target and state match.
 *
 * Based somewhat on CONNMARK:
 *   Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
 *    by Henrik Nordstrom <hno@marasystems.com>
 *
 * (C) 2006 Red Hat, Inc., James Morris <jmorris@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_CONNSECMARK.h>
22
#include <net/netfilter/nf_conntrack.h>
23 24 25 26 27 28 29 30 31 32 33 34 35

#define PFX "CONNSECMARK: "

MODULE_LICENSE("GPL");
MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
MODULE_DESCRIPTION("ip[6]tables CONNSECMARK module");
MODULE_ALIAS("ipt_CONNSECMARK");
MODULE_ALIAS("ip6t_CONNSECMARK");

/*
 * If the packet has a security mark and the connection does not, copy
 * the security mark from the packet to the connection.
 */
36
static void secmark_save(const struct sk_buff *skb)
37 38
{
	if (skb->secmark) {
39
		struct nf_conn *ct;
40 41
		enum ip_conntrack_info ctinfo;

42 43 44
		ct = nf_ct_get(skb, &ctinfo);
		if (ct && !ct->secmark)
			ct->secmark = skb->secmark;
45 46 47 48 49 50 51 52 53 54
	}
}

/*
 * If packet has no security mark, and the connection does, restore the
 * security mark from the connection to the packet.
 */
static void secmark_restore(struct sk_buff *skb)
{
	if (!skb->secmark) {
55
		struct nf_conn *ct;
56 57
		enum ip_conntrack_info ctinfo;

58 59 60
		ct = nf_ct_get(skb, &ctinfo);
		if (ct && ct->secmark)
			skb->secmark = ct->secmark;
61 62 63 64 65 66
	}
}

static unsigned int target(struct sk_buff **pskb, const struct net_device *in,
			   const struct net_device *out, unsigned int hooknum,
			   const struct xt_target *target,
67
			   const void *targinfo)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	struct sk_buff *skb = *pskb;
	const struct xt_connsecmark_target_info *info = targinfo;

	switch (info->mode) {
	case CONNSECMARK_SAVE:
		secmark_save(skb);
		break;

	case CONNSECMARK_RESTORE:
		secmark_restore(skb);
		break;

	default:
		BUG();
	}

	return XT_CONTINUE;
}

88 89 90
static bool checkentry(const char *tablename, const void *entry,
		       const struct xt_target *target, void *targinfo,
		       unsigned int hook_mask)
91
{
92
	const struct xt_connsecmark_target_info *info = targinfo;
93

94 95 96
	if (nf_ct_l3proto_try_module_get(target->family) < 0) {
		printk(KERN_WARNING "can't load conntrack support for "
				    "proto=%d\n", target->family);
97
		return false;
98
	}
99 100 101 102 103 104 105
	switch (info->mode) {
	case CONNSECMARK_SAVE:
	case CONNSECMARK_RESTORE:
		break;

	default:
		printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
106
		return false;
107 108
	}

109
	return true;
110 111
}

112 113 114 115 116 117
static void
destroy(const struct xt_target *target, void *targinfo)
{
	nf_ct_l3proto_module_put(target->family);
}

118
static struct xt_target xt_connsecmark_target[] __read_mostly = {
119 120 121 122
	{
		.name		= "CONNSECMARK",
		.family		= AF_INET,
		.checkentry	= checkentry,
123
		.destroy	= destroy,
124 125 126 127 128 129 130 131 132
		.target		= target,
		.targetsize	= sizeof(struct xt_connsecmark_target_info),
		.table		= "mangle",
		.me		= THIS_MODULE,
	},
	{
		.name		= "CONNSECMARK",
		.family		= AF_INET6,
		.checkentry	= checkentry,
133
		.destroy	= destroy,
134 135 136 137 138
		.target		= target,
		.targetsize	= sizeof(struct xt_connsecmark_target_info),
		.table		= "mangle",
		.me		= THIS_MODULE,
	},
139 140 141 142
};

static int __init xt_connsecmark_init(void)
{
143 144
	return xt_register_targets(xt_connsecmark_target,
				   ARRAY_SIZE(xt_connsecmark_target));
145 146 147 148
}

static void __exit xt_connsecmark_fini(void)
{
149 150
	xt_unregister_targets(xt_connsecmark_target,
			      ARRAY_SIZE(xt_connsecmark_target));
151 152 153 154
}

module_init(xt_connsecmark_init);
module_exit(xt_connsecmark_fini);