1 /* This kernel module is used to modify the connection mark values, or
2 * to optionally restore the skb nfmark from the connection mark
4 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5 * by Henrik Nordstrom <hno@marasystems.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/skbuff.h>
24 #include <net/checksum.h>
26 MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
27 MODULE_DESCRIPTION("IP tables CONNMARK matching module");
28 MODULE_LICENSE("GPL");
29 MODULE_ALIAS("ipt_CONNMARK");
31 #include <linux/netfilter/x_tables.h>
32 #include <linux/netfilter/xt_CONNMARK.h>
33 #include <net/netfilter/nf_conntrack_compat.h>
36 target(struct sk_buff
**pskb
,
37 const struct net_device
*in
,
38 const struct net_device
*out
,
40 const struct xt_target
*target
,
43 const struct xt_connmark_target_info
*markinfo
= targinfo
;
48 u_int32_t
*ctmark
= nf_ct_get_mark(*pskb
, &ctinfo
);
51 switch(markinfo
->mode
) {
53 newmark
= (*ctmark
& ~markinfo
->mask
) | markinfo
->mark
;
54 if (newmark
!= *ctmark
) {
56 #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
57 ip_conntrack_event_cache(IPCT_MARK
, *pskb
);
59 nf_conntrack_event_cache(IPCT_MARK
, *pskb
);
63 case XT_CONNMARK_SAVE
:
64 newmark
= (*ctmark
& ~markinfo
->mask
) |
65 ((*pskb
)->nfmark
& markinfo
->mask
);
66 if (*ctmark
!= newmark
) {
68 #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
69 ip_conntrack_event_cache(IPCT_MARK
, *pskb
);
71 nf_conntrack_event_cache(IPCT_MARK
, *pskb
);
75 case XT_CONNMARK_RESTORE
:
76 nfmark
= (*pskb
)->nfmark
;
77 diff
= (*ctmark
^ nfmark
) & markinfo
->mask
;
79 (*pskb
)->nfmark
= nfmark
^ diff
;
88 checkentry(const char *tablename
,
90 const struct xt_target
*target
,
92 unsigned int hook_mask
)
94 struct xt_connmark_target_info
*matchinfo
= targinfo
;
96 if (matchinfo
->mode
== XT_CONNMARK_RESTORE
) {
97 if (strcmp(tablename
, "mangle") != 0) {
98 printk(KERN_WARNING
"CONNMARK: restore can only be "
99 "called from \"mangle\" table, not \"%s\"\n",
104 if (matchinfo
->mark
> 0xffffffff || matchinfo
->mask
> 0xffffffff) {
105 printk(KERN_WARNING
"CONNMARK: Only supports 32bit mark\n");
112 struct compat_xt_connmark_target_info
{
113 compat_ulong_t mark
, mask
;
119 static void compat_from_user(void *dst
, void *src
)
121 struct compat_xt_connmark_target_info
*cm
= src
;
122 struct xt_connmark_target_info m
= {
127 memcpy(dst
, &m
, sizeof(m
));
130 static int compat_to_user(void __user
*dst
, void *src
)
132 struct xt_connmark_target_info
*m
= src
;
133 struct compat_xt_connmark_target_info cm
= {
138 return copy_to_user(dst
, &cm
, sizeof(cm
)) ? -EFAULT
: 0;
140 #endif /* CONFIG_COMPAT */
142 static struct xt_target xt_connmark_target
[] = {
146 .checkentry
= checkentry
,
148 .targetsize
= sizeof(struct xt_connmark_target_info
),
150 .compatsize
= sizeof(struct compat_xt_connmark_target_info
),
151 .compat_from_user
= compat_from_user
,
152 .compat_to_user
= compat_to_user
,
159 .checkentry
= checkentry
,
161 .targetsize
= sizeof(struct xt_connmark_target_info
),
166 static int __init
xt_connmark_init(void)
169 return xt_register_targets(xt_connmark_target
,
170 ARRAY_SIZE(xt_connmark_target
));
173 static void __exit
xt_connmark_fini(void)
175 xt_unregister_targets(xt_connmark_target
,
176 ARRAY_SIZE(xt_connmark_target
));
179 module_init(xt_connmark_init
);
180 module_exit(xt_connmark_fini
);