Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* This kernel module is used to modify the connection mark values, or
2 * to optionally restore the skb nfmark from the connection mark
3 *
4 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5 * by Henrik Nordstrom <hno@marasystems.com>
6 *
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.
11 *
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.
16 *
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
20 */
21#include <linux/module.h>
22#include <linux/skbuff.h>
23#include <linux/ip.h>
24#include <net/checksum.h>
25
26MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
27MODULE_DESCRIPTION("IP tables CONNMARK matching module");
28MODULE_LICENSE("GPL");
29MODULE_ALIAS("ipt_CONNMARK");
30
31#include <linux/netfilter/x_tables.h>
32#include <linux/netfilter/xt_CONNMARK.h>
33#include <net/netfilter/nf_conntrack_compat.h>
34#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
35#include <net/netfilter/nf_conntrack_ecache.h>
36#endif
37
38static unsigned int
39target(struct sk_buff **pskb,
40 const struct net_device *in,
41 const struct net_device *out,
42 unsigned int hooknum,
43 const struct xt_target *target,
44 const void *targinfo)
45{
46 const struct xt_connmark_target_info *markinfo = targinfo;
47 u_int32_t diff;
48 u_int32_t mark;
49 u_int32_t newmark;
50 u_int32_t ctinfo;
51 u_int32_t *ctmark = nf_ct_get_mark(*pskb, &ctinfo);
52
53 if (ctmark) {
54 switch(markinfo->mode) {
55 case XT_CONNMARK_SET:
56 newmark = (*ctmark & ~markinfo->mask) | markinfo->mark;
57 if (newmark != *ctmark) {
58 *ctmark = newmark;
59#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
60 ip_conntrack_event_cache(IPCT_MARK, *pskb);
61#else
62 nf_conntrack_event_cache(IPCT_MARK, *pskb);
63#endif
64 }
65 break;
66 case XT_CONNMARK_SAVE:
67 newmark = (*ctmark & ~markinfo->mask) |
68 ((*pskb)->mark & markinfo->mask);
69 if (*ctmark != newmark) {
70 *ctmark = newmark;
71#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
72 ip_conntrack_event_cache(IPCT_MARK, *pskb);
73#else
74 nf_conntrack_event_cache(IPCT_MARK, *pskb);
75#endif
76 }
77 break;
78 case XT_CONNMARK_RESTORE:
79 mark = (*pskb)->mark;
80 diff = (*ctmark ^ mark) & markinfo->mask;
81 if (diff != 0)
82 (*pskb)->mark = mark ^ diff;
83 break;
84 }
85 }
86
87 return XT_CONTINUE;
88}
89
90static int
91checkentry(const char *tablename,
92 const void *entry,
93 const struct xt_target *target,
94 void *targinfo,
95 unsigned int hook_mask)
96{
97 struct xt_connmark_target_info *matchinfo = targinfo;
98
99 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
100 printk(KERN_WARNING "can't load conntrack support for "
101 "proto=%d\n", target->family);
102 return 0;
103 }
104 if (matchinfo->mode == XT_CONNMARK_RESTORE) {
105 if (strcmp(tablename, "mangle") != 0) {
106 printk(KERN_WARNING "CONNMARK: restore can only be "
107 "called from \"mangle\" table, not \"%s\"\n",
108 tablename);
109 return 0;
110 }
111 }
112 if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) {
113 printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n");
114 return 0;
115 }
116 return 1;
117}
118
119static void
120destroy(const struct xt_target *target, void *targinfo)
121{
122 nf_ct_l3proto_module_put(target->family);
123}
124
125#ifdef CONFIG_COMPAT
126struct compat_xt_connmark_target_info {
127 compat_ulong_t mark, mask;
128 u_int8_t mode;
129 u_int8_t __pad1;
130 u_int16_t __pad2;
131};
132
133static void compat_from_user(void *dst, void *src)
134{
135 struct compat_xt_connmark_target_info *cm = src;
136 struct xt_connmark_target_info m = {
137 .mark = cm->mark,
138 .mask = cm->mask,
139 .mode = cm->mode,
140 };
141 memcpy(dst, &m, sizeof(m));
142}
143
144static int compat_to_user(void __user *dst, void *src)
145{
146 struct xt_connmark_target_info *m = src;
147 struct compat_xt_connmark_target_info cm = {
148 .mark = m->mark,
149 .mask = m->mask,
150 .mode = m->mode,
151 };
152 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
153}
154#endif /* CONFIG_COMPAT */
155
156static struct xt_target xt_connmark_target[] = {
157 {
158 .name = "CONNMARK",
159 .family = AF_INET,
160 .checkentry = checkentry,
161 .destroy = destroy,
162 .target = target,
163 .targetsize = sizeof(struct xt_connmark_target_info),
164#ifdef CONFIG_COMPAT
165 .compatsize = sizeof(struct compat_xt_connmark_target_info),
166 .compat_from_user = compat_from_user,
167 .compat_to_user = compat_to_user,
168#endif
169 .me = THIS_MODULE
170 },
171 {
172 .name = "CONNMARK",
173 .family = AF_INET6,
174 .checkentry = checkentry,
175 .destroy = destroy,
176 .target = target,
177 .targetsize = sizeof(struct xt_connmark_target_info),
178 .me = THIS_MODULE
179 },
180};
181
182static int __init xt_connmark_init(void)
183{
184 return xt_register_targets(xt_connmark_target,
185 ARRAY_SIZE(xt_connmark_target));
186}
187
188static void __exit xt_connmark_fini(void)
189{
190 xt_unregister_targets(xt_connmark_target,
191 ARRAY_SIZE(xt_connmark_target));
192}
193
194module_init(xt_connmark_init);
195module_exit(xt_connmark_fini);