2 * This module is used to copy security markings from packets
3 * to connections, and restore security markings from connections
4 * back to packets. This would normally be performed in conjunction
5 * with the SECMARK target and state match.
7 * Based somewhat on CONNMARK:
8 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
9 * by Henrik Nordstrom <hno@marasystems.com>
11 * (C) 2006 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/module.h>
19 #include <linux/skbuff.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter/xt_CONNSECMARK.h>
22 #include <net/netfilter/nf_conntrack.h>
24 #define PFX "CONNSECMARK: "
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
28 MODULE_DESCRIPTION("ip[6]tables CONNSECMARK module");
29 MODULE_ALIAS("ipt_CONNSECMARK");
30 MODULE_ALIAS("ip6t_CONNSECMARK");
33 * If the packet has a security mark and the connection does not, copy
34 * the security mark from the packet to the connection.
36 static void secmark_save(const struct sk_buff *skb)
40 enum ip_conntrack_info ctinfo;
42 ct = nf_ct_get(skb, &ctinfo);
43 if (ct && !ct->secmark)
44 ct->secmark = skb->secmark;
49 * If packet has no security mark, and the connection does, restore the
50 * security mark from the connection to the packet.
52 static void secmark_restore(struct sk_buff *skb)
56 enum ip_conntrack_info ctinfo;
58 ct = nf_ct_get(skb, &ctinfo);
59 if (ct && ct->secmark)
60 skb->secmark = ct->secmark;
64 static unsigned int target(struct sk_buff **pskb, const struct net_device *in,
65 const struct net_device *out, unsigned int hooknum,
66 const struct xt_target *target,
69 struct sk_buff *skb = *pskb;
70 const struct xt_connsecmark_target_info *info = targinfo;
73 case CONNSECMARK_SAVE:
77 case CONNSECMARK_RESTORE:
88 static bool checkentry(const char *tablename, const void *entry,
89 const struct xt_target *target, void *targinfo,
90 unsigned int hook_mask)
92 const struct xt_connsecmark_target_info *info = targinfo;
94 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
95 printk(KERN_WARNING "can't load conntrack support for "
96 "proto=%d\n", target->family);
100 case CONNSECMARK_SAVE:
101 case CONNSECMARK_RESTORE:
105 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
113 destroy(const struct xt_target *target, void *targinfo)
115 nf_ct_l3proto_module_put(target->family);
118 static struct xt_target xt_connsecmark_target[] __read_mostly = {
120 .name = "CONNSECMARK",
122 .checkentry = checkentry,
125 .targetsize = sizeof(struct xt_connsecmark_target_info),
130 .name = "CONNSECMARK",
132 .checkentry = checkentry,
135 .targetsize = sizeof(struct xt_connsecmark_target_info),
141 static int __init xt_connsecmark_init(void)
143 return xt_register_targets(xt_connsecmark_target,
144 ARRAY_SIZE(xt_connsecmark_target));
147 static void __exit xt_connsecmark_fini(void)
149 xt_unregister_targets(xt_connsecmark_target,
150 ARRAY_SIZE(xt_connsecmark_target));
153 module_init(xt_connsecmark_init);
154 module_exit(xt_connsecmark_fini);