2 * xt_CONNMARK - Netfilter module to modify the connection mark values
4 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5 * by Henrik Nordstrom <hno@marasystems.com>
6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
7 * Jan Engelhardt <jengelh@computergmbh.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/skbuff.h>
26 #include <net/checksum.h>
28 MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
29 MODULE_DESCRIPTION("Xtables: connection mark modification");
30 MODULE_LICENSE("GPL");
31 MODULE_ALIAS("ipt_CONNMARK");
32 MODULE_ALIAS("ip6t_CONNMARK");
34 #include <linux/netfilter/x_tables.h>
35 #include <linux/netfilter/xt_CONNMARK.h>
36 #include <net/netfilter/nf_conntrack_ecache.h>
39 connmark_tg_v0(struct sk_buff *skb, const struct net_device *in,
40 const struct net_device *out, unsigned int hooknum,
41 const struct xt_target *target, const void *targinfo)
43 const struct xt_connmark_target_info *markinfo = targinfo;
45 enum ip_conntrack_info ctinfo;
50 ct = nf_ct_get(skb, &ctinfo);
52 switch(markinfo->mode) {
54 newmark = (ct->mark & ~markinfo->mask) | markinfo->mark;
55 if (newmark != ct->mark) {
57 nf_conntrack_event_cache(IPCT_MARK, skb);
60 case XT_CONNMARK_SAVE:
61 newmark = (ct->mark & ~markinfo->mask) |
62 (skb->mark & markinfo->mask);
63 if (ct->mark != newmark) {
65 nf_conntrack_event_cache(IPCT_MARK, skb);
68 case XT_CONNMARK_RESTORE:
70 diff = (ct->mark ^ mark) & markinfo->mask;
71 skb->mark = mark ^ diff;
80 connmark_tg(struct sk_buff *skb, const struct net_device *in,
81 const struct net_device *out, unsigned int hooknum,
82 const struct xt_target *target, const void *targinfo)
84 const struct xt_connmark_tginfo1 *info = targinfo;
85 enum ip_conntrack_info ctinfo;
89 ct = nf_ct_get(skb, &ctinfo);
95 newmark = (ct->mark & ~info->ctmask) ^ info->ctmark;
96 if (ct->mark != newmark) {
98 nf_conntrack_event_cache(IPCT_MARK, skb);
101 case XT_CONNMARK_SAVE:
102 newmark = (ct->mark & ~info->ctmask) ^
103 (skb->mark & info->nfmask);
104 if (ct->mark != newmark) {
106 nf_conntrack_event_cache(IPCT_MARK, skb);
109 case XT_CONNMARK_RESTORE:
110 newmark = (skb->mark & ~info->nfmask) ^
111 (ct->mark & info->ctmask);
120 connmark_tg_check_v0(const char *tablename, const void *entry,
121 const struct xt_target *target, void *targinfo,
122 unsigned int hook_mask)
124 const struct xt_connmark_target_info *matchinfo = targinfo;
126 if (matchinfo->mode == XT_CONNMARK_RESTORE) {
127 if (strcmp(tablename, "mangle") != 0) {
128 printk(KERN_WARNING "CONNMARK: restore can only be "
129 "called from \"mangle\" table, not \"%s\"\n",
134 if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
135 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
138 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
139 printk(KERN_WARNING "can't load conntrack support for "
140 "proto=%u\n", target->family);
147 connmark_tg_check(const char *tablename, const void *entry,
148 const struct xt_target *target, void *targinfo,
149 unsigned int hook_mask)
151 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
152 printk(KERN_WARNING "cannot load conntrack support for "
153 "proto=%u\n", target->family);
160 connmark_tg_destroy(const struct xt_target *target, void *targinfo)
162 nf_ct_l3proto_module_put(target->family);
166 struct compat_xt_connmark_target_info {
167 compat_ulong_t mark, mask;
173 static void connmark_tg_compat_from_user_v0(void *dst, void *src)
175 const struct compat_xt_connmark_target_info *cm = src;
176 struct xt_connmark_target_info m = {
181 memcpy(dst, &m, sizeof(m));
184 static int connmark_tg_compat_to_user_v0(void __user *dst, void *src)
186 const struct xt_connmark_target_info *m = src;
187 struct compat_xt_connmark_target_info cm = {
192 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
194 #endif /* CONFIG_COMPAT */
196 static struct xt_target connmark_tg_reg[] __read_mostly = {
201 .checkentry = connmark_tg_check_v0,
202 .destroy = connmark_tg_destroy,
203 .target = connmark_tg_v0,
204 .targetsize = sizeof(struct xt_connmark_target_info),
206 .compatsize = sizeof(struct compat_xt_connmark_target_info),
207 .compat_from_user = connmark_tg_compat_from_user_v0,
208 .compat_to_user = connmark_tg_compat_to_user_v0,
216 .checkentry = connmark_tg_check_v0,
217 .destroy = connmark_tg_destroy,
218 .target = connmark_tg_v0,
219 .targetsize = sizeof(struct xt_connmark_target_info),
221 .compatsize = sizeof(struct compat_xt_connmark_target_info),
222 .compat_from_user = connmark_tg_compat_from_user_v0,
223 .compat_to_user = connmark_tg_compat_to_user_v0,
231 .checkentry = connmark_tg_check,
232 .target = connmark_tg,
233 .targetsize = sizeof(struct xt_connmark_tginfo1),
234 .destroy = connmark_tg_destroy,
241 .checkentry = connmark_tg_check,
242 .target = connmark_tg,
243 .targetsize = sizeof(struct xt_connmark_tginfo1),
244 .destroy = connmark_tg_destroy,
249 static int __init connmark_tg_init(void)
251 return xt_register_targets(connmark_tg_reg,
252 ARRAY_SIZE(connmark_tg_reg));
255 static void __exit connmark_tg_exit(void)
257 xt_unregister_targets(connmark_tg_reg, ARRAY_SIZE(connmark_tg_reg));
260 module_init(connmark_tg_init);
261 module_exit(connmark_tg_exit);