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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/skbuff.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/xt_CONNSECMARK.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_ecache.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
28 MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
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
;
45 nf_conntrack_event_cache(IPCT_SECMARK
, ct
);
51 * If packet has no security mark, and the connection does, restore the
52 * security mark from the connection to the packet.
54 static void secmark_restore(struct sk_buff
*skb
)
57 const struct nf_conn
*ct
;
58 enum ip_conntrack_info ctinfo
;
60 ct
= nf_ct_get(skb
, &ctinfo
);
61 if (ct
&& ct
->secmark
)
62 skb
->secmark
= ct
->secmark
;
67 connsecmark_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
69 const struct xt_connsecmark_target_info
*info
= par
->targinfo
;
72 case CONNSECMARK_SAVE
:
76 case CONNSECMARK_RESTORE
:
87 static int connsecmark_tg_check(const struct xt_tgchk_param
*par
)
89 const struct xt_connsecmark_target_info
*info
= par
->targinfo
;
92 if (strcmp(par
->table
, "mangle") != 0 &&
93 strcmp(par
->table
, "security") != 0) {
94 pr_info("target only valid in the \'mangle\' "
95 "or \'security\' tables, not \'%s\'.\n", par
->table
);
100 case CONNSECMARK_SAVE
:
101 case CONNSECMARK_RESTORE
:
105 pr_info("invalid mode: %hu\n", info
->mode
);
109 ret
= nf_ct_netns_get(par
->net
, par
->family
);
111 pr_info("cannot load conntrack support for proto=%u\n",
116 static void connsecmark_tg_destroy(const struct xt_tgdtor_param
*par
)
118 nf_ct_netns_put(par
->net
, par
->family
);
121 static struct xt_target connsecmark_tg_reg __read_mostly
= {
122 .name
= "CONNSECMARK",
124 .family
= NFPROTO_UNSPEC
,
125 .checkentry
= connsecmark_tg_check
,
126 .destroy
= connsecmark_tg_destroy
,
127 .target
= connsecmark_tg
,
128 .targetsize
= sizeof(struct xt_connsecmark_target_info
),
132 static int __init
connsecmark_tg_init(void)
134 return xt_register_target(&connsecmark_tg_reg
);
137 static void __exit
connsecmark_tg_exit(void)
139 xt_unregister_target(&connsecmark_tg_reg
);
142 module_init(connsecmark_tg_init
);
143 module_exit(connsecmark_tg_exit
);