]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* This is a module which is used for setting the NFMARK field of an skb. */ |
2 | ||
3 | /* (C) 1999-2001 Marc Boucher <[email protected]> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/ip.h> | |
13 | #include <net/checksum.h> | |
14 | ||
2e4e6a17 HW |
15 | #include <linux/netfilter/x_tables.h> |
16 | #include <linux/netfilter/xt_MARK.h> | |
1da177e4 LT |
17 | |
18 | MODULE_LICENSE("GPL"); | |
19 | MODULE_AUTHOR("Marc Boucher <[email protected]>"); | |
2e4e6a17 HW |
20 | MODULE_DESCRIPTION("ip[6]tables MARK modification module"); |
21 | MODULE_ALIAS("ipt_MARK"); | |
22 | MODULE_ALIAS("ip6t_MARK"); | |
1da177e4 LT |
23 | |
24 | static unsigned int | |
25 | target_v0(struct sk_buff **pskb, | |
26 | const struct net_device *in, | |
27 | const struct net_device *out, | |
28 | unsigned int hooknum, | |
c4986734 | 29 | const struct xt_target *target, |
fe1cb108 | 30 | const void *targinfo) |
1da177e4 | 31 | { |
2e4e6a17 | 32 | const struct xt_mark_target_info *markinfo = targinfo; |
1da177e4 | 33 | |
6869c4d8 | 34 | if((*pskb)->nfmark != markinfo->mark) |
1da177e4 | 35 | (*pskb)->nfmark = markinfo->mark; |
6869c4d8 | 36 | |
2e4e6a17 | 37 | return XT_CONTINUE; |
1da177e4 LT |
38 | } |
39 | ||
40 | static unsigned int | |
41 | target_v1(struct sk_buff **pskb, | |
42 | const struct net_device *in, | |
43 | const struct net_device *out, | |
44 | unsigned int hooknum, | |
c4986734 | 45 | const struct xt_target *target, |
fe1cb108 | 46 | const void *targinfo) |
1da177e4 | 47 | { |
2e4e6a17 | 48 | const struct xt_mark_target_info_v1 *markinfo = targinfo; |
1da177e4 LT |
49 | int mark = 0; |
50 | ||
51 | switch (markinfo->mode) { | |
2e4e6a17 | 52 | case XT_MARK_SET: |
1da177e4 LT |
53 | mark = markinfo->mark; |
54 | break; | |
55 | ||
2e4e6a17 | 56 | case XT_MARK_AND: |
1da177e4 LT |
57 | mark = (*pskb)->nfmark & markinfo->mark; |
58 | break; | |
59 | ||
2e4e6a17 | 60 | case XT_MARK_OR: |
1da177e4 LT |
61 | mark = (*pskb)->nfmark | markinfo->mark; |
62 | break; | |
63 | } | |
64 | ||
6869c4d8 | 65 | if((*pskb)->nfmark != mark) |
1da177e4 | 66 | (*pskb)->nfmark = mark; |
6869c4d8 | 67 | |
2e4e6a17 | 68 | return XT_CONTINUE; |
1da177e4 LT |
69 | } |
70 | ||
71 | ||
72 | static int | |
73 | checkentry_v0(const char *tablename, | |
2e4e6a17 | 74 | const void *entry, |
c4986734 | 75 | const struct xt_target *target, |
1da177e4 | 76 | void *targinfo, |
1da177e4 LT |
77 | unsigned int hook_mask) |
78 | { | |
2e4e6a17 | 79 | struct xt_mark_target_info *markinfo = targinfo; |
bf3a46aa | 80 | |
bf3a46aa HW |
81 | if (markinfo->mark > 0xffffffff) { |
82 | printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n"); | |
83 | return 0; | |
84 | } | |
1da177e4 LT |
85 | return 1; |
86 | } | |
87 | ||
88 | static int | |
89 | checkentry_v1(const char *tablename, | |
2e4e6a17 | 90 | const void *entry, |
c4986734 | 91 | const struct xt_target *target, |
1da177e4 | 92 | void *targinfo, |
1da177e4 LT |
93 | unsigned int hook_mask) |
94 | { | |
2e4e6a17 | 95 | struct xt_mark_target_info_v1 *markinfo = targinfo; |
1da177e4 | 96 | |
2e4e6a17 HW |
97 | if (markinfo->mode != XT_MARK_SET |
98 | && markinfo->mode != XT_MARK_AND | |
99 | && markinfo->mode != XT_MARK_OR) { | |
1da177e4 LT |
100 | printk(KERN_WARNING "MARK: unknown mode %u\n", |
101 | markinfo->mode); | |
102 | return 0; | |
103 | } | |
bf3a46aa HW |
104 | if (markinfo->mark > 0xffffffff) { |
105 | printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n"); | |
106 | return 0; | |
107 | } | |
1da177e4 LT |
108 | return 1; |
109 | } | |
110 | ||
be7263b7 PM |
111 | #ifdef CONFIG_COMPAT |
112 | struct compat_xt_mark_target_info_v1 { | |
113 | compat_ulong_t mark; | |
114 | u_int8_t mode; | |
115 | u_int8_t __pad1; | |
116 | u_int16_t __pad2; | |
117 | }; | |
118 | ||
119 | static void compat_from_user_v1(void *dst, void *src) | |
120 | { | |
121 | struct compat_xt_mark_target_info_v1 *cm = src; | |
122 | struct xt_mark_target_info_v1 m = { | |
123 | .mark = cm->mark, | |
124 | .mode = cm->mode, | |
125 | }; | |
126 | memcpy(dst, &m, sizeof(m)); | |
127 | } | |
128 | ||
129 | static int compat_to_user_v1(void __user *dst, void *src) | |
130 | { | |
131 | struct xt_mark_target_info_v1 *m = src; | |
132 | struct compat_xt_mark_target_info_v1 cm = { | |
133 | .mark = m->mark, | |
134 | .mode = m->mode, | |
135 | }; | |
136 | return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0; | |
137 | } | |
138 | #endif /* CONFIG_COMPAT */ | |
139 | ||
4470bbc7 PM |
140 | static struct xt_target xt_mark_target[] = { |
141 | { | |
142 | .name = "MARK", | |
143 | .family = AF_INET, | |
144 | .revision = 0, | |
145 | .checkentry = checkentry_v0, | |
146 | .target = target_v0, | |
147 | .targetsize = sizeof(struct xt_mark_target_info), | |
148 | .table = "mangle", | |
149 | .me = THIS_MODULE, | |
150 | }, | |
151 | { | |
152 | .name = "MARK", | |
153 | .family = AF_INET, | |
154 | .revision = 1, | |
155 | .checkentry = checkentry_v1, | |
156 | .target = target_v1, | |
157 | .targetsize = sizeof(struct xt_mark_target_info_v1), | |
be7263b7 PM |
158 | #ifdef CONFIG_COMPAT |
159 | .compatsize = sizeof(struct compat_xt_mark_target_info_v1), | |
160 | .compat_from_user = compat_from_user_v1, | |
161 | .compat_to_user = compat_to_user_v1, | |
162 | #endif | |
4470bbc7 PM |
163 | .table = "mangle", |
164 | .me = THIS_MODULE, | |
165 | }, | |
166 | { | |
167 | .name = "MARK", | |
168 | .family = AF_INET6, | |
169 | .revision = 0, | |
170 | .checkentry = checkentry_v0, | |
171 | .target = target_v0, | |
172 | .targetsize = sizeof(struct xt_mark_target_info), | |
173 | .table = "mangle", | |
174 | .me = THIS_MODULE, | |
175 | }, | |
2e4e6a17 HW |
176 | }; |
177 | ||
65b4b4e8 | 178 | static int __init xt_mark_init(void) |
1da177e4 | 179 | { |
4470bbc7 | 180 | return xt_register_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target)); |
1da177e4 LT |
181 | } |
182 | ||
65b4b4e8 | 183 | static void __exit xt_mark_fini(void) |
1da177e4 | 184 | { |
4470bbc7 | 185 | xt_unregister_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target)); |
1da177e4 LT |
186 | } |
187 | ||
65b4b4e8 AM |
188 | module_init(xt_mark_init); |
189 | module_exit(xt_mark_fini); |