Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / netfilter / xt_MARK.c
blobf9ce20b58981c757c85980557a919b276919299e
1 /*
2 * xt_MARK - Netfilter module to modify the NFMARK field of an skb
4 * (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
5 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
6 * Jan Engelhardt <jengelh@computergmbh.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/ip.h>
16 #include <net/checksum.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter/xt_MARK.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
23 MODULE_DESCRIPTION("Xtables: packet mark modification");
24 MODULE_ALIAS("ipt_MARK");
25 MODULE_ALIAS("ip6t_MARK");
27 static unsigned int
28 mark_tg_v0(struct sk_buff *skb, const struct net_device *in,
29 const struct net_device *out, unsigned int hooknum,
30 const struct xt_target *target, const void *targinfo)
32 const struct xt_mark_target_info *markinfo = targinfo;
34 skb->mark = markinfo->mark;
35 return XT_CONTINUE;
38 static unsigned int
39 mark_tg_v1(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_mark_target_info_v1 *markinfo = targinfo;
44 int mark = 0;
46 switch (markinfo->mode) {
47 case XT_MARK_SET:
48 mark = markinfo->mark;
49 break;
51 case XT_MARK_AND:
52 mark = skb->mark & markinfo->mark;
53 break;
55 case XT_MARK_OR:
56 mark = skb->mark | markinfo->mark;
57 break;
60 skb->mark = mark;
61 return XT_CONTINUE;
64 static unsigned int
65 mark_tg(struct sk_buff *skb, const struct net_device *in,
66 const struct net_device *out, unsigned int hooknum,
67 const struct xt_target *target, const void *targinfo)
69 const struct xt_mark_tginfo2 *info = targinfo;
71 skb->mark = (skb->mark & ~info->mask) ^ info->mark;
72 return XT_CONTINUE;
75 static bool
76 mark_tg_check_v0(const char *tablename, const void *entry,
77 const struct xt_target *target, void *targinfo,
78 unsigned int hook_mask)
80 const struct xt_mark_target_info *markinfo = targinfo;
82 if (markinfo->mark > 0xffffffff) {
83 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
84 return false;
86 return true;
89 static bool
90 mark_tg_check_v1(const char *tablename, const void *entry,
91 const struct xt_target *target, void *targinfo,
92 unsigned int hook_mask)
94 const struct xt_mark_target_info_v1 *markinfo = targinfo;
96 if (markinfo->mode != XT_MARK_SET
97 && markinfo->mode != XT_MARK_AND
98 && markinfo->mode != XT_MARK_OR) {
99 printk(KERN_WARNING "MARK: unknown mode %u\n",
100 markinfo->mode);
101 return false;
103 if (markinfo->mark > 0xffffffff) {
104 printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
105 return false;
107 return true;
110 #ifdef CONFIG_COMPAT
111 struct compat_xt_mark_target_info {
112 compat_ulong_t mark;
115 static void mark_tg_compat_from_user_v0(void *dst, void *src)
117 const struct compat_xt_mark_target_info *cm = src;
118 struct xt_mark_target_info m = {
119 .mark = cm->mark,
121 memcpy(dst, &m, sizeof(m));
124 static int mark_tg_compat_to_user_v0(void __user *dst, void *src)
126 const struct xt_mark_target_info *m = src;
127 struct compat_xt_mark_target_info cm = {
128 .mark = m->mark,
130 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
133 struct compat_xt_mark_target_info_v1 {
134 compat_ulong_t mark;
135 u_int8_t mode;
136 u_int8_t __pad1;
137 u_int16_t __pad2;
140 static void mark_tg_compat_from_user_v1(void *dst, void *src)
142 const struct compat_xt_mark_target_info_v1 *cm = src;
143 struct xt_mark_target_info_v1 m = {
144 .mark = cm->mark,
145 .mode = cm->mode,
147 memcpy(dst, &m, sizeof(m));
150 static int mark_tg_compat_to_user_v1(void __user *dst, void *src)
152 const struct xt_mark_target_info_v1 *m = src;
153 struct compat_xt_mark_target_info_v1 cm = {
154 .mark = m->mark,
155 .mode = m->mode,
157 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
159 #endif /* CONFIG_COMPAT */
161 static struct xt_target mark_tg_reg[] __read_mostly = {
163 .name = "MARK",
164 .family = AF_INET,
165 .revision = 0,
166 .checkentry = mark_tg_check_v0,
167 .target = mark_tg_v0,
168 .targetsize = sizeof(struct xt_mark_target_info),
169 #ifdef CONFIG_COMPAT
170 .compatsize = sizeof(struct compat_xt_mark_target_info),
171 .compat_from_user = mark_tg_compat_from_user_v0,
172 .compat_to_user = mark_tg_compat_to_user_v0,
173 #endif
174 .table = "mangle",
175 .me = THIS_MODULE,
178 .name = "MARK",
179 .family = AF_INET,
180 .revision = 1,
181 .checkentry = mark_tg_check_v1,
182 .target = mark_tg_v1,
183 .targetsize = sizeof(struct xt_mark_target_info_v1),
184 #ifdef CONFIG_COMPAT
185 .compatsize = sizeof(struct compat_xt_mark_target_info_v1),
186 .compat_from_user = mark_tg_compat_from_user_v1,
187 .compat_to_user = mark_tg_compat_to_user_v1,
188 #endif
189 .table = "mangle",
190 .me = THIS_MODULE,
193 .name = "MARK",
194 .family = AF_INET6,
195 .revision = 0,
196 .checkentry = mark_tg_check_v0,
197 .target = mark_tg_v0,
198 .targetsize = sizeof(struct xt_mark_target_info),
199 #ifdef CONFIG_COMPAT
200 .compatsize = sizeof(struct compat_xt_mark_target_info),
201 .compat_from_user = mark_tg_compat_from_user_v0,
202 .compat_to_user = mark_tg_compat_to_user_v0,
203 #endif
204 .table = "mangle",
205 .me = THIS_MODULE,
208 .name = "MARK",
209 .family = AF_INET6,
210 .revision = 1,
211 .checkentry = mark_tg_check_v1,
212 .target = mark_tg_v1,
213 .targetsize = sizeof(struct xt_mark_target_info_v1),
214 #ifdef CONFIG_COMPAT
215 .compatsize = sizeof(struct compat_xt_mark_target_info_v1),
216 .compat_from_user = mark_tg_compat_from_user_v1,
217 .compat_to_user = mark_tg_compat_to_user_v1,
218 #endif
219 .table = "mangle",
220 .me = THIS_MODULE,
223 .name = "MARK",
224 .revision = 2,
225 .family = AF_INET,
226 .target = mark_tg,
227 .targetsize = sizeof(struct xt_mark_tginfo2),
228 .me = THIS_MODULE,
231 .name = "MARK",
232 .revision = 2,
233 .family = AF_INET6,
234 .target = mark_tg,
235 .targetsize = sizeof(struct xt_mark_tginfo2),
236 .me = THIS_MODULE,
240 static int __init mark_tg_init(void)
242 return xt_register_targets(mark_tg_reg, ARRAY_SIZE(mark_tg_reg));
245 static void __exit mark_tg_exit(void)
247 xt_unregister_targets(mark_tg_reg, ARRAY_SIZE(mark_tg_reg));
250 module_init(mark_tg_init);
251 module_exit(mark_tg_exit);