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,2008 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>
23 #include <net/netfilter/nf_conntrack_ecache.h>
25 #define PFX "CONNSECMARK: "
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
29 MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
30 MODULE_ALIAS("ipt_CONNSECMARK");
31 MODULE_ALIAS("ip6t_CONNSECMARK");
34 * If the packet has a security mark and the connection does not, copy
35 * the security mark from the packet to the connection.
37 static void secmark_save(const struct sk_buff *skb)
41 enum ip_conntrack_info ctinfo;
43 ct = nf_ct_get(skb, &ctinfo);
44 if (ct && !ct->secmark) {
45 ct->secmark = skb->secmark;
46 nf_conntrack_event_cache(IPCT_SECMARK, ct);
52 * If packet has no security mark, and the connection does, restore the
53 * security mark from the connection to the packet.
55 static void secmark_restore(struct sk_buff *skb)
58 const struct nf_conn *ct;
59 enum ip_conntrack_info ctinfo;
61 ct = nf_ct_get(skb, &ctinfo);
62 if (ct && ct->secmark)
63 skb->secmark = ct->secmark;
68 connsecmark_tg(struct sk_buff *skb, const struct xt_target_param *par)
70 const struct xt_connsecmark_target_info *info = par->targinfo;
73 case CONNSECMARK_SAVE:
77 case CONNSECMARK_RESTORE:
89 connsecmark_tg_check(const char *tablename, const void *entry,
90 const struct xt_target *target, void *targinfo,
91 unsigned int hook_mask)
93 const struct xt_connsecmark_target_info *info = targinfo;
95 if (strcmp(tablename, "mangle") && strcmp(tablename, "security")) {
96 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
97 "or \'security\' tables, not \'%s\'.\n", tablename);
101 switch (info->mode) {
102 case CONNSECMARK_SAVE:
103 case CONNSECMARK_RESTORE:
107 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
111 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
112 printk(KERN_WARNING "can't load conntrack support for "
113 "proto=%u\n", target->family);
120 connsecmark_tg_destroy(const struct xt_target *target, void *targinfo)
122 nf_ct_l3proto_module_put(target->family);
125 static struct xt_target connsecmark_tg_reg[] __read_mostly = {
127 .name = "CONNSECMARK",
128 .family = NFPROTO_IPV4,
129 .checkentry = connsecmark_tg_check,
130 .destroy = connsecmark_tg_destroy,
131 .target = connsecmark_tg,
132 .targetsize = sizeof(struct xt_connsecmark_target_info),
136 .name = "CONNSECMARK",
137 .family = NFPROTO_IPV6,
138 .checkentry = connsecmark_tg_check,
139 .destroy = connsecmark_tg_destroy,
140 .target = connsecmark_tg,
141 .targetsize = sizeof(struct xt_connsecmark_target_info),
146 static int __init connsecmark_tg_init(void)
148 return xt_register_targets(connsecmark_tg_reg,
149 ARRAY_SIZE(connsecmark_tg_reg));
152 static void __exit connsecmark_tg_exit(void)
154 xt_unregister_targets(connsecmark_tg_reg,
155 ARRAY_SIZE(connsecmark_tg_reg));
158 module_init(connsecmark_tg_init);
159 module_exit(connsecmark_tg_exit);