Commit | Line | Data |
---|---|---|
2e4e6a17 HW |
1 | /* |
2 | * x_tables core - Backend for {ip,ip6,arp}_tables | |
3 | * | |
4 | * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org> | |
f229f6ce | 5 | * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
2e4e6a17 HW |
6 | * |
7 | * Based on existing ip_tables code which is | |
8 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling | |
9 | * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | * | |
15 | */ | |
be91fd5e | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
2e4e6a17 | 17 | #include <linux/kernel.h> |
3a9a231d | 18 | #include <linux/module.h> |
2e4e6a17 HW |
19 | #include <linux/socket.h> |
20 | #include <linux/net.h> | |
21 | #include <linux/proc_fs.h> | |
22 | #include <linux/seq_file.h> | |
23 | #include <linux/string.h> | |
24 | #include <linux/vmalloc.h> | |
9e19bb6d | 25 | #include <linux/mutex.h> |
d7fe0f24 | 26 | #include <linux/mm.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
fbabf31e | 28 | #include <linux/audit.h> |
457c4cbc | 29 | #include <net/net_namespace.h> |
2e4e6a17 HW |
30 | |
31 | #include <linux/netfilter/x_tables.h> | |
32 | #include <linux/netfilter_arp.h> | |
e3eaa991 JE |
33 | #include <linux/netfilter_ipv4/ip_tables.h> |
34 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
35 | #include <linux/netfilter_arp/arp_tables.h> | |
9e19bb6d | 36 | |
2e4e6a17 HW |
37 | MODULE_LICENSE("GPL"); |
38 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
043ef46c | 39 | MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module"); |
2e4e6a17 HW |
40 | |
41 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) | |
42 | ||
b386d9f5 | 43 | struct compat_delta { |
255d0dc3 ED |
44 | unsigned int offset; /* offset in kernel */ |
45 | int delta; /* delta in 32bit user land */ | |
b386d9f5 PM |
46 | }; |
47 | ||
2e4e6a17 | 48 | struct xt_af { |
9e19bb6d | 49 | struct mutex mutex; |
2e4e6a17 HW |
50 | struct list_head match; |
51 | struct list_head target; | |
b386d9f5 | 52 | #ifdef CONFIG_COMPAT |
2722971c | 53 | struct mutex compat_mutex; |
255d0dc3 ED |
54 | struct compat_delta *compat_tab; |
55 | unsigned int number; /* number of slots in compat_tab[] */ | |
56 | unsigned int cur; /* number of used slots in compat_tab[] */ | |
b386d9f5 | 57 | #endif |
2e4e6a17 HW |
58 | }; |
59 | ||
60 | static struct xt_af *xt; | |
61 | ||
7e9c6eeb JE |
62 | static const char *const xt_prefix[NFPROTO_NUMPROTO] = { |
63 | [NFPROTO_UNSPEC] = "x", | |
64 | [NFPROTO_IPV4] = "ip", | |
65 | [NFPROTO_ARP] = "arp", | |
66 | [NFPROTO_BRIDGE] = "eb", | |
67 | [NFPROTO_IPV6] = "ip6", | |
37f9f733 PM |
68 | }; |
69 | ||
f3c5c1bf JE |
70 | /* Allow this many total (re)entries. */ |
71 | static const unsigned int xt_jumpstack_multiplier = 2; | |
72 | ||
2e4e6a17 HW |
73 | /* Registration hooks for targets. */ |
74 | int | |
a45049c5 | 75 | xt_register_target(struct xt_target *target) |
2e4e6a17 | 76 | { |
76108cea JE |
77 | u_int8_t af = target->family; |
78 | int ret; | |
2e4e6a17 | 79 | |
9e19bb6d | 80 | ret = mutex_lock_interruptible(&xt[af].mutex); |
2e4e6a17 HW |
81 | if (ret != 0) |
82 | return ret; | |
83 | list_add(&target->list, &xt[af].target); | |
9e19bb6d | 84 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
85 | return ret; |
86 | } | |
87 | EXPORT_SYMBOL(xt_register_target); | |
88 | ||
89 | void | |
a45049c5 | 90 | xt_unregister_target(struct xt_target *target) |
2e4e6a17 | 91 | { |
76108cea | 92 | u_int8_t af = target->family; |
a45049c5 | 93 | |
9e19bb6d | 94 | mutex_lock(&xt[af].mutex); |
df0933dc | 95 | list_del(&target->list); |
9e19bb6d | 96 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
97 | } |
98 | EXPORT_SYMBOL(xt_unregister_target); | |
99 | ||
52d9c42e PM |
100 | int |
101 | xt_register_targets(struct xt_target *target, unsigned int n) | |
102 | { | |
103 | unsigned int i; | |
104 | int err = 0; | |
105 | ||
106 | for (i = 0; i < n; i++) { | |
107 | err = xt_register_target(&target[i]); | |
108 | if (err) | |
109 | goto err; | |
110 | } | |
111 | return err; | |
112 | ||
113 | err: | |
114 | if (i > 0) | |
115 | xt_unregister_targets(target, i); | |
116 | return err; | |
117 | } | |
118 | EXPORT_SYMBOL(xt_register_targets); | |
119 | ||
120 | void | |
121 | xt_unregister_targets(struct xt_target *target, unsigned int n) | |
122 | { | |
f68c5301 CG |
123 | while (n-- > 0) |
124 | xt_unregister_target(&target[n]); | |
52d9c42e PM |
125 | } |
126 | EXPORT_SYMBOL(xt_unregister_targets); | |
127 | ||
2e4e6a17 | 128 | int |
a45049c5 | 129 | xt_register_match(struct xt_match *match) |
2e4e6a17 | 130 | { |
76108cea JE |
131 | u_int8_t af = match->family; |
132 | int ret; | |
2e4e6a17 | 133 | |
9e19bb6d | 134 | ret = mutex_lock_interruptible(&xt[af].mutex); |
2e4e6a17 HW |
135 | if (ret != 0) |
136 | return ret; | |
137 | ||
138 | list_add(&match->list, &xt[af].match); | |
9e19bb6d | 139 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
140 | |
141 | return ret; | |
142 | } | |
143 | EXPORT_SYMBOL(xt_register_match); | |
144 | ||
145 | void | |
a45049c5 | 146 | xt_unregister_match(struct xt_match *match) |
2e4e6a17 | 147 | { |
76108cea | 148 | u_int8_t af = match->family; |
a45049c5 | 149 | |
9e19bb6d | 150 | mutex_lock(&xt[af].mutex); |
df0933dc | 151 | list_del(&match->list); |
9e19bb6d | 152 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
153 | } |
154 | EXPORT_SYMBOL(xt_unregister_match); | |
155 | ||
52d9c42e PM |
156 | int |
157 | xt_register_matches(struct xt_match *match, unsigned int n) | |
158 | { | |
159 | unsigned int i; | |
160 | int err = 0; | |
161 | ||
162 | for (i = 0; i < n; i++) { | |
163 | err = xt_register_match(&match[i]); | |
164 | if (err) | |
165 | goto err; | |
166 | } | |
167 | return err; | |
168 | ||
169 | err: | |
170 | if (i > 0) | |
171 | xt_unregister_matches(match, i); | |
172 | return err; | |
173 | } | |
174 | EXPORT_SYMBOL(xt_register_matches); | |
175 | ||
176 | void | |
177 | xt_unregister_matches(struct xt_match *match, unsigned int n) | |
178 | { | |
f68c5301 CG |
179 | while (n-- > 0) |
180 | xt_unregister_match(&match[n]); | |
52d9c42e PM |
181 | } |
182 | EXPORT_SYMBOL(xt_unregister_matches); | |
183 | ||
2e4e6a17 HW |
184 | |
185 | /* | |
186 | * These are weird, but module loading must not be done with mutex | |
187 | * held (since they will register), and we have to have a single | |
adb00ae2 | 188 | * function to use. |
2e4e6a17 HW |
189 | */ |
190 | ||
191 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ | |
76108cea | 192 | struct xt_match *xt_find_match(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
193 | { |
194 | struct xt_match *m; | |
42046e2e | 195 | int err = -ENOENT; |
2e4e6a17 | 196 | |
9e19bb6d | 197 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
2e4e6a17 HW |
198 | return ERR_PTR(-EINTR); |
199 | ||
200 | list_for_each_entry(m, &xt[af].match, list) { | |
201 | if (strcmp(m->name, name) == 0) { | |
202 | if (m->revision == revision) { | |
203 | if (try_module_get(m->me)) { | |
9e19bb6d | 204 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
205 | return m; |
206 | } | |
207 | } else | |
208 | err = -EPROTOTYPE; /* Found something. */ | |
209 | } | |
210 | } | |
9e19bb6d | 211 | mutex_unlock(&xt[af].mutex); |
55b69e91 JE |
212 | |
213 | if (af != NFPROTO_UNSPEC) | |
214 | /* Try searching again in the family-independent list */ | |
215 | return xt_find_match(NFPROTO_UNSPEC, name, revision); | |
216 | ||
2e4e6a17 HW |
217 | return ERR_PTR(err); |
218 | } | |
219 | EXPORT_SYMBOL(xt_find_match); | |
220 | ||
fd0ec0e6 JE |
221 | struct xt_match * |
222 | xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) | |
223 | { | |
224 | struct xt_match *match; | |
225 | ||
adb00ae2 SH |
226 | match = xt_find_match(nfproto, name, revision); |
227 | if (IS_ERR(match)) { | |
228 | request_module("%st_%s", xt_prefix[nfproto], name); | |
229 | match = xt_find_match(nfproto, name, revision); | |
230 | } | |
231 | ||
232 | return match; | |
fd0ec0e6 JE |
233 | } |
234 | EXPORT_SYMBOL_GPL(xt_request_find_match); | |
235 | ||
2e4e6a17 | 236 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
76108cea | 237 | struct xt_target *xt_find_target(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
238 | { |
239 | struct xt_target *t; | |
42046e2e | 240 | int err = -ENOENT; |
2e4e6a17 | 241 | |
9e19bb6d | 242 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
2e4e6a17 HW |
243 | return ERR_PTR(-EINTR); |
244 | ||
245 | list_for_each_entry(t, &xt[af].target, list) { | |
246 | if (strcmp(t->name, name) == 0) { | |
247 | if (t->revision == revision) { | |
248 | if (try_module_get(t->me)) { | |
9e19bb6d | 249 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
250 | return t; |
251 | } | |
252 | } else | |
253 | err = -EPROTOTYPE; /* Found something. */ | |
254 | } | |
255 | } | |
9e19bb6d | 256 | mutex_unlock(&xt[af].mutex); |
55b69e91 JE |
257 | |
258 | if (af != NFPROTO_UNSPEC) | |
259 | /* Try searching again in the family-independent list */ | |
260 | return xt_find_target(NFPROTO_UNSPEC, name, revision); | |
261 | ||
2e4e6a17 HW |
262 | return ERR_PTR(err); |
263 | } | |
264 | EXPORT_SYMBOL(xt_find_target); | |
265 | ||
76108cea | 266 | struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision) |
2e4e6a17 HW |
267 | { |
268 | struct xt_target *target; | |
269 | ||
adb00ae2 SH |
270 | target = xt_find_target(af, name, revision); |
271 | if (IS_ERR(target)) { | |
272 | request_module("%st_%s", xt_prefix[af], name); | |
273 | target = xt_find_target(af, name, revision); | |
274 | } | |
275 | ||
276 | return target; | |
2e4e6a17 HW |
277 | } |
278 | EXPORT_SYMBOL_GPL(xt_request_find_target); | |
279 | ||
76108cea | 280 | static int match_revfn(u8 af, const char *name, u8 revision, int *bestp) |
2e4e6a17 | 281 | { |
5452e425 | 282 | const struct xt_match *m; |
2e4e6a17 HW |
283 | int have_rev = 0; |
284 | ||
285 | list_for_each_entry(m, &xt[af].match, list) { | |
286 | if (strcmp(m->name, name) == 0) { | |
287 | if (m->revision > *bestp) | |
288 | *bestp = m->revision; | |
289 | if (m->revision == revision) | |
290 | have_rev = 1; | |
291 | } | |
292 | } | |
656caff2 PM |
293 | |
294 | if (af != NFPROTO_UNSPEC && !have_rev) | |
295 | return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); | |
296 | ||
2e4e6a17 HW |
297 | return have_rev; |
298 | } | |
299 | ||
76108cea | 300 | static int target_revfn(u8 af, const char *name, u8 revision, int *bestp) |
2e4e6a17 | 301 | { |
5452e425 | 302 | const struct xt_target *t; |
2e4e6a17 HW |
303 | int have_rev = 0; |
304 | ||
305 | list_for_each_entry(t, &xt[af].target, list) { | |
306 | if (strcmp(t->name, name) == 0) { | |
307 | if (t->revision > *bestp) | |
308 | *bestp = t->revision; | |
309 | if (t->revision == revision) | |
310 | have_rev = 1; | |
311 | } | |
312 | } | |
656caff2 PM |
313 | |
314 | if (af != NFPROTO_UNSPEC && !have_rev) | |
315 | return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); | |
316 | ||
2e4e6a17 HW |
317 | return have_rev; |
318 | } | |
319 | ||
320 | /* Returns true or false (if no such extension at all) */ | |
76108cea | 321 | int xt_find_revision(u8 af, const char *name, u8 revision, int target, |
2e4e6a17 HW |
322 | int *err) |
323 | { | |
324 | int have_rev, best = -1; | |
325 | ||
9e19bb6d | 326 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) { |
2e4e6a17 HW |
327 | *err = -EINTR; |
328 | return 1; | |
329 | } | |
330 | if (target == 1) | |
331 | have_rev = target_revfn(af, name, revision, &best); | |
332 | else | |
333 | have_rev = match_revfn(af, name, revision, &best); | |
9e19bb6d | 334 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
335 | |
336 | /* Nothing at all? Return 0 to try loading module. */ | |
337 | if (best == -1) { | |
338 | *err = -ENOENT; | |
339 | return 0; | |
340 | } | |
341 | ||
342 | *err = best; | |
343 | if (!have_rev) | |
344 | *err = -EPROTONOSUPPORT; | |
345 | return 1; | |
346 | } | |
347 | EXPORT_SYMBOL_GPL(xt_find_revision); | |
348 | ||
5b76c494 JE |
349 | static char * |
350 | textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto) | |
45185364 | 351 | { |
5b76c494 | 352 | static const char *const inetbr_names[] = { |
45185364 JE |
353 | "PREROUTING", "INPUT", "FORWARD", |
354 | "OUTPUT", "POSTROUTING", "BROUTING", | |
355 | }; | |
5b76c494 JE |
356 | static const char *const arp_names[] = { |
357 | "INPUT", "FORWARD", "OUTPUT", | |
358 | }; | |
359 | const char *const *names; | |
360 | unsigned int i, max; | |
45185364 JE |
361 | char *p = buf; |
362 | bool np = false; | |
363 | int res; | |
364 | ||
5b76c494 JE |
365 | names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names; |
366 | max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) : | |
367 | ARRAY_SIZE(inetbr_names); | |
45185364 | 368 | *p = '\0'; |
5b76c494 | 369 | for (i = 0; i < max; ++i) { |
45185364 JE |
370 | if (!(mask & (1 << i))) |
371 | continue; | |
372 | res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]); | |
373 | if (res > 0) { | |
374 | size -= res; | |
375 | p += res; | |
376 | } | |
377 | np = true; | |
378 | } | |
379 | ||
380 | return buf; | |
381 | } | |
382 | ||
916a917d | 383 | int xt_check_match(struct xt_mtchk_param *par, |
9b4fce7a | 384 | unsigned int size, u_int8_t proto, bool inv_proto) |
37f9f733 | 385 | { |
bd414ee6 JE |
386 | int ret; |
387 | ||
9b4fce7a JE |
388 | if (XT_ALIGN(par->match->matchsize) != size && |
389 | par->match->matchsize != -1) { | |
043ef46c JE |
390 | /* |
391 | * ebt_among is exempt from centralized matchsize checking | |
392 | * because it uses a dynamic-size data set. | |
393 | */ | |
b402405d JE |
394 | pr_err("%s_tables: %s.%u match: invalid size " |
395 | "%u (kernel) != (user) %u\n", | |
916a917d | 396 | xt_prefix[par->family], par->match->name, |
b402405d | 397 | par->match->revision, |
9b4fce7a | 398 | XT_ALIGN(par->match->matchsize), size); |
37f9f733 PM |
399 | return -EINVAL; |
400 | } | |
9b4fce7a JE |
401 | if (par->match->table != NULL && |
402 | strcmp(par->match->table, par->table) != 0) { | |
3dd5d7e3 | 403 | pr_err("%s_tables: %s match: only valid in %s table, not %s\n", |
916a917d | 404 | xt_prefix[par->family], par->match->name, |
9b4fce7a | 405 | par->match->table, par->table); |
37f9f733 PM |
406 | return -EINVAL; |
407 | } | |
9b4fce7a | 408 | if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) { |
45185364 JE |
409 | char used[64], allow[64]; |
410 | ||
3dd5d7e3 | 411 | pr_err("%s_tables: %s match: used from hooks %s, but only " |
45185364 | 412 | "valid from %s\n", |
916a917d | 413 | xt_prefix[par->family], par->match->name, |
5b76c494 JE |
414 | textify_hooks(used, sizeof(used), par->hook_mask, |
415 | par->family), | |
416 | textify_hooks(allow, sizeof(allow), par->match->hooks, | |
417 | par->family)); | |
37f9f733 PM |
418 | return -EINVAL; |
419 | } | |
9b4fce7a | 420 | if (par->match->proto && (par->match->proto != proto || inv_proto)) { |
3dd5d7e3 | 421 | pr_err("%s_tables: %s match: only valid for protocol %u\n", |
916a917d JE |
422 | xt_prefix[par->family], par->match->name, |
423 | par->match->proto); | |
37f9f733 PM |
424 | return -EINVAL; |
425 | } | |
bd414ee6 JE |
426 | if (par->match->checkentry != NULL) { |
427 | ret = par->match->checkentry(par); | |
428 | if (ret < 0) | |
429 | return ret; | |
430 | else if (ret > 0) | |
431 | /* Flag up potential errors. */ | |
432 | return -EIO; | |
433 | } | |
37f9f733 PM |
434 | return 0; |
435 | } | |
436 | EXPORT_SYMBOL_GPL(xt_check_match); | |
437 | ||
1b507b92 FW |
438 | /** xt_check_entry_match - check that matches end before start of target |
439 | * | |
440 | * @match: beginning of xt_entry_match | |
441 | * @target: beginning of this rules target (alleged end of matches) | |
442 | * @alignment: alignment requirement of match structures | |
443 | * | |
444 | * Validates that all matches add up to the beginning of the target, | |
445 | * and that each match covers at least the base structure size. | |
446 | * | |
447 | * Return: 0 on success, negative errno on failure. | |
448 | */ | |
449 | static int xt_check_entry_match(const char *match, const char *target, | |
450 | const size_t alignment) | |
451 | { | |
452 | const struct xt_entry_match *pos; | |
453 | int length = target - match; | |
454 | ||
455 | if (length == 0) /* no matches */ | |
456 | return 0; | |
457 | ||
458 | pos = (struct xt_entry_match *)match; | |
459 | do { | |
460 | if ((unsigned long)pos % alignment) | |
461 | return -EINVAL; | |
462 | ||
463 | if (length < (int)sizeof(struct xt_entry_match)) | |
464 | return -EINVAL; | |
465 | ||
466 | if (pos->u.match_size < sizeof(struct xt_entry_match)) | |
467 | return -EINVAL; | |
468 | ||
469 | if (pos->u.match_size > length) | |
470 | return -EINVAL; | |
471 | ||
472 | length -= pos->u.match_size; | |
473 | pos = ((void *)((char *)(pos) + (pos)->u.match_size)); | |
474 | } while (length > 0); | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
2722971c | 479 | #ifdef CONFIG_COMPAT |
255d0dc3 | 480 | int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta) |
b386d9f5 | 481 | { |
255d0dc3 | 482 | struct xt_af *xp = &xt[af]; |
b386d9f5 | 483 | |
255d0dc3 ED |
484 | if (!xp->compat_tab) { |
485 | if (!xp->number) | |
486 | return -EINVAL; | |
487 | xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number); | |
488 | if (!xp->compat_tab) | |
489 | return -ENOMEM; | |
490 | xp->cur = 0; | |
491 | } | |
b386d9f5 | 492 | |
255d0dc3 ED |
493 | if (xp->cur >= xp->number) |
494 | return -EINVAL; | |
b386d9f5 | 495 | |
255d0dc3 ED |
496 | if (xp->cur) |
497 | delta += xp->compat_tab[xp->cur - 1].delta; | |
498 | xp->compat_tab[xp->cur].offset = offset; | |
499 | xp->compat_tab[xp->cur].delta = delta; | |
500 | xp->cur++; | |
b386d9f5 PM |
501 | return 0; |
502 | } | |
503 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); | |
504 | ||
76108cea | 505 | void xt_compat_flush_offsets(u_int8_t af) |
b386d9f5 | 506 | { |
255d0dc3 ED |
507 | if (xt[af].compat_tab) { |
508 | vfree(xt[af].compat_tab); | |
509 | xt[af].compat_tab = NULL; | |
510 | xt[af].number = 0; | |
5a6351ee | 511 | xt[af].cur = 0; |
b386d9f5 PM |
512 | } |
513 | } | |
514 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); | |
515 | ||
3e5e524f | 516 | int xt_compat_calc_jump(u_int8_t af, unsigned int offset) |
b386d9f5 | 517 | { |
255d0dc3 ED |
518 | struct compat_delta *tmp = xt[af].compat_tab; |
519 | int mid, left = 0, right = xt[af].cur - 1; | |
520 | ||
521 | while (left <= right) { | |
522 | mid = (left + right) >> 1; | |
523 | if (offset > tmp[mid].offset) | |
524 | left = mid + 1; | |
525 | else if (offset < tmp[mid].offset) | |
526 | right = mid - 1; | |
527 | else | |
528 | return mid ? tmp[mid - 1].delta : 0; | |
529 | } | |
5a6351ee | 530 | return left ? tmp[left - 1].delta : 0; |
b386d9f5 PM |
531 | } |
532 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); | |
533 | ||
255d0dc3 ED |
534 | void xt_compat_init_offsets(u_int8_t af, unsigned int number) |
535 | { | |
536 | xt[af].number = number; | |
537 | xt[af].cur = 0; | |
538 | } | |
539 | EXPORT_SYMBOL(xt_compat_init_offsets); | |
540 | ||
5452e425 | 541 | int xt_compat_match_offset(const struct xt_match *match) |
2722971c | 542 | { |
9fa492cd PM |
543 | u_int16_t csize = match->compatsize ? : match->matchsize; |
544 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); | |
545 | } | |
546 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); | |
547 | ||
fbe426f8 FW |
548 | void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
549 | unsigned int *size) | |
9fa492cd | 550 | { |
5452e425 | 551 | const struct xt_match *match = m->u.kernel.match; |
9fa492cd PM |
552 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
553 | int pad, off = xt_compat_match_offset(match); | |
554 | u_int16_t msize = cm->u.user.match_size; | |
151cc2f5 | 555 | char name[sizeof(m->u.user.name)]; |
9fa492cd PM |
556 | |
557 | m = *dstptr; | |
558 | memcpy(m, cm, sizeof(*cm)); | |
559 | if (match->compat_from_user) | |
560 | match->compat_from_user(m->data, cm->data); | |
561 | else | |
562 | memcpy(m->data, cm->data, msize - sizeof(*cm)); | |
563 | pad = XT_ALIGN(match->matchsize) - match->matchsize; | |
564 | if (pad > 0) | |
565 | memset(m->data + match->matchsize, 0, pad); | |
566 | ||
567 | msize += off; | |
568 | m->u.user.match_size = msize; | |
151cc2f5 FW |
569 | strlcpy(name, match->name, sizeof(name)); |
570 | module_put(match->me); | |
571 | strncpy(m->u.user.name, name, sizeof(m->u.user.name)); | |
9fa492cd PM |
572 | |
573 | *size += off; | |
574 | *dstptr += msize; | |
575 | } | |
576 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); | |
577 | ||
739674fb JE |
578 | int xt_compat_match_to_user(const struct xt_entry_match *m, |
579 | void __user **dstptr, unsigned int *size) | |
9fa492cd | 580 | { |
5452e425 | 581 | const struct xt_match *match = m->u.kernel.match; |
9fa492cd PM |
582 | struct compat_xt_entry_match __user *cm = *dstptr; |
583 | int off = xt_compat_match_offset(match); | |
584 | u_int16_t msize = m->u.user.match_size - off; | |
585 | ||
586 | if (copy_to_user(cm, m, sizeof(*cm)) || | |
a18aa31b PM |
587 | put_user(msize, &cm->u.user.match_size) || |
588 | copy_to_user(cm->u.user.name, m->u.kernel.match->name, | |
589 | strlen(m->u.kernel.match->name) + 1)) | |
601e68e1 | 590 | return -EFAULT; |
9fa492cd PM |
591 | |
592 | if (match->compat_to_user) { | |
593 | if (match->compat_to_user((void __user *)cm->data, m->data)) | |
594 | return -EFAULT; | |
595 | } else { | |
596 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) | |
597 | return -EFAULT; | |
2722971c | 598 | } |
9fa492cd PM |
599 | |
600 | *size -= off; | |
601 | *dstptr += msize; | |
602 | return 0; | |
2722971c | 603 | } |
9fa492cd | 604 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
30532332 | 605 | |
5366deb6 FW |
606 | /* non-compat version may have padding after verdict */ |
607 | struct compat_xt_standard_target { | |
608 | struct compat_xt_entry_target t; | |
609 | compat_uint_t verdict; | |
610 | }; | |
611 | ||
71f72bbf | 612 | int xt_compat_check_entry_offsets(const void *base, const char *elems, |
30532332 FW |
613 | unsigned int target_offset, |
614 | unsigned int next_offset) | |
615 | { | |
71f72bbf | 616 | long size_of_base_struct = elems - (const char *)base; |
30532332 FW |
617 | const struct compat_xt_entry_target *t; |
618 | const char *e = base; | |
619 | ||
71f72bbf FW |
620 | if (target_offset < size_of_base_struct) |
621 | return -EINVAL; | |
622 | ||
30532332 FW |
623 | if (target_offset + sizeof(*t) > next_offset) |
624 | return -EINVAL; | |
625 | ||
626 | t = (void *)(e + target_offset); | |
627 | if (t->u.target_size < sizeof(*t)) | |
628 | return -EINVAL; | |
629 | ||
630 | if (target_offset + t->u.target_size > next_offset) | |
631 | return -EINVAL; | |
632 | ||
5366deb6 | 633 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 && |
1a10388e | 634 | COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset) |
5366deb6 FW |
635 | return -EINVAL; |
636 | ||
1b507b92 FW |
637 | /* compat_xt_entry match has less strict aligment requirements, |
638 | * otherwise they are identical. In case of padding differences | |
639 | * we need to add compat version of xt_check_entry_match. | |
640 | */ | |
641 | BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match)); | |
642 | ||
643 | return xt_check_entry_match(elems, base + target_offset, | |
644 | __alignof__(struct compat_xt_entry_match)); | |
30532332 FW |
645 | } |
646 | EXPORT_SYMBOL(xt_compat_check_entry_offsets); | |
9fa492cd | 647 | #endif /* CONFIG_COMPAT */ |
2722971c | 648 | |
ff9be206 FW |
649 | /** |
650 | * xt_check_entry_offsets - validate arp/ip/ip6t_entry | |
651 | * | |
652 | * @base: pointer to arp/ip/ip6t_entry | |
71f72bbf | 653 | * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems |
ff9be206 FW |
654 | * @target_offset: the arp/ip/ip6_t->target_offset |
655 | * @next_offset: the arp/ip/ip6_t->next_offset | |
656 | * | |
1b507b92 FW |
657 | * validates that target_offset and next_offset are sane and that all |
658 | * match sizes (if any) align with the target offset. | |
ff9be206 | 659 | * |
71f72bbf | 660 | * This function does not validate the targets or matches themselves, it |
1b507b92 FW |
661 | * only tests that all the offsets and sizes are correct, that all |
662 | * match structures are aligned, and that the last structure ends where | |
663 | * the target structure begins. | |
664 | * | |
665 | * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. | |
71f72bbf | 666 | * |
ff9be206 FW |
667 | * The arp/ip/ip6t_entry structure @base must have passed following tests: |
668 | * - it must point to a valid memory location | |
669 | * - base to base + next_offset must be accessible, i.e. not exceed allocated | |
670 | * length. | |
671 | * | |
1b507b92 FW |
672 | * A well-formed entry looks like this: |
673 | * | |
674 | * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry | |
675 | * e->elems[]-----' | | | |
676 | * matchsize | | | |
677 | * matchsize | | | |
678 | * | | | |
679 | * target_offset---------------------------------' | | |
680 | * next_offset---------------------------------------------------' | |
681 | * | |
682 | * elems[]: flexible array member at end of ip(6)/arpt_entry struct. | |
683 | * This is where matches (if any) and the target reside. | |
684 | * target_offset: beginning of target. | |
685 | * next_offset: start of the next rule; also: size of this rule. | |
686 | * Since targets have a minimum size, target_offset + minlen <= next_offset. | |
687 | * | |
688 | * Every match stores its size, sum of sizes must not exceed target_offset. | |
689 | * | |
ff9be206 FW |
690 | * Return: 0 on success, negative errno on failure. |
691 | */ | |
692 | int xt_check_entry_offsets(const void *base, | |
71f72bbf | 693 | const char *elems, |
ff9be206 FW |
694 | unsigned int target_offset, |
695 | unsigned int next_offset) | |
696 | { | |
71f72bbf | 697 | long size_of_base_struct = elems - (const char *)base; |
ff9be206 FW |
698 | const struct xt_entry_target *t; |
699 | const char *e = base; | |
700 | ||
71f72bbf FW |
701 | /* target start is within the ip/ip6/arpt_entry struct */ |
702 | if (target_offset < size_of_base_struct) | |
703 | return -EINVAL; | |
704 | ||
ff9be206 FW |
705 | if (target_offset + sizeof(*t) > next_offset) |
706 | return -EINVAL; | |
707 | ||
708 | t = (void *)(e + target_offset); | |
6b9f8b74 FW |
709 | if (t->u.target_size < sizeof(*t)) |
710 | return -EINVAL; | |
711 | ||
ff9be206 FW |
712 | if (target_offset + t->u.target_size > next_offset) |
713 | return -EINVAL; | |
714 | ||
5366deb6 | 715 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 && |
1a10388e | 716 | XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset) |
5366deb6 FW |
717 | return -EINVAL; |
718 | ||
1b507b92 FW |
719 | return xt_check_entry_match(elems, base + target_offset, |
720 | __alignof__(struct xt_entry_match)); | |
ff9be206 FW |
721 | } |
722 | EXPORT_SYMBOL(xt_check_entry_offsets); | |
723 | ||
916a917d | 724 | int xt_check_target(struct xt_tgchk_param *par, |
af5d6dc2 | 725 | unsigned int size, u_int8_t proto, bool inv_proto) |
37f9f733 | 726 | { |
d6b00a53 JE |
727 | int ret; |
728 | ||
af5d6dc2 | 729 | if (XT_ALIGN(par->target->targetsize) != size) { |
b402405d JE |
730 | pr_err("%s_tables: %s.%u target: invalid size " |
731 | "%u (kernel) != (user) %u\n", | |
916a917d | 732 | xt_prefix[par->family], par->target->name, |
b402405d | 733 | par->target->revision, |
af5d6dc2 | 734 | XT_ALIGN(par->target->targetsize), size); |
37f9f733 PM |
735 | return -EINVAL; |
736 | } | |
af5d6dc2 JE |
737 | if (par->target->table != NULL && |
738 | strcmp(par->target->table, par->table) != 0) { | |
3dd5d7e3 | 739 | pr_err("%s_tables: %s target: only valid in %s table, not %s\n", |
916a917d | 740 | xt_prefix[par->family], par->target->name, |
af5d6dc2 | 741 | par->target->table, par->table); |
37f9f733 PM |
742 | return -EINVAL; |
743 | } | |
af5d6dc2 | 744 | if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) { |
45185364 JE |
745 | char used[64], allow[64]; |
746 | ||
3dd5d7e3 | 747 | pr_err("%s_tables: %s target: used from hooks %s, but only " |
45185364 | 748 | "usable from %s\n", |
916a917d | 749 | xt_prefix[par->family], par->target->name, |
5b76c494 JE |
750 | textify_hooks(used, sizeof(used), par->hook_mask, |
751 | par->family), | |
752 | textify_hooks(allow, sizeof(allow), par->target->hooks, | |
753 | par->family)); | |
37f9f733 PM |
754 | return -EINVAL; |
755 | } | |
af5d6dc2 | 756 | if (par->target->proto && (par->target->proto != proto || inv_proto)) { |
3dd5d7e3 | 757 | pr_err("%s_tables: %s target: only valid for protocol %u\n", |
916a917d | 758 | xt_prefix[par->family], par->target->name, |
af5d6dc2 | 759 | par->target->proto); |
37f9f733 PM |
760 | return -EINVAL; |
761 | } | |
d6b00a53 JE |
762 | if (par->target->checkentry != NULL) { |
763 | ret = par->target->checkentry(par); | |
764 | if (ret < 0) | |
765 | return ret; | |
766 | else if (ret > 0) | |
767 | /* Flag up potential errors. */ | |
768 | return -EIO; | |
769 | } | |
37f9f733 PM |
770 | return 0; |
771 | } | |
772 | EXPORT_SYMBOL_GPL(xt_check_target); | |
773 | ||
0a052338 FW |
774 | /** |
775 | * xt_copy_counters_from_user - copy counters and metadata from userspace | |
776 | * | |
777 | * @user: src pointer to userspace memory | |
778 | * @len: alleged size of userspace memory | |
779 | * @info: where to store the xt_counters_info metadata | |
780 | * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel | |
781 | * | |
782 | * Copies counter meta data from @user and stores it in @info. | |
783 | * | |
784 | * vmallocs memory to hold the counters, then copies the counter data | |
785 | * from @user to the new memory and returns a pointer to it. | |
786 | * | |
787 | * If @compat is true, @info gets converted automatically to the 64bit | |
788 | * representation. | |
789 | * | |
790 | * The metadata associated with the counters is stored in @info. | |
791 | * | |
792 | * Return: returns pointer that caller has to test via IS_ERR(). | |
793 | * If IS_ERR is false, caller has to vfree the pointer. | |
794 | */ | |
795 | void *xt_copy_counters_from_user(const void __user *user, unsigned int len, | |
796 | struct xt_counters_info *info, bool compat) | |
797 | { | |
798 | void *mem; | |
799 | u64 size; | |
800 | ||
801 | #ifdef CONFIG_COMPAT | |
802 | if (compat) { | |
803 | /* structures only differ in size due to alignment */ | |
804 | struct compat_xt_counters_info compat_tmp; | |
805 | ||
806 | if (len <= sizeof(compat_tmp)) | |
807 | return ERR_PTR(-EINVAL); | |
808 | ||
809 | len -= sizeof(compat_tmp); | |
810 | if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0) | |
811 | return ERR_PTR(-EFAULT); | |
812 | ||
813 | strlcpy(info->name, compat_tmp.name, sizeof(info->name)); | |
814 | info->num_counters = compat_tmp.num_counters; | |
815 | user += sizeof(compat_tmp); | |
816 | } else | |
817 | #endif | |
818 | { | |
819 | if (len <= sizeof(*info)) | |
820 | return ERR_PTR(-EINVAL); | |
821 | ||
822 | len -= sizeof(*info); | |
823 | if (copy_from_user(info, user, sizeof(*info)) != 0) | |
824 | return ERR_PTR(-EFAULT); | |
825 | ||
826 | info->name[sizeof(info->name) - 1] = '\0'; | |
827 | user += sizeof(*info); | |
828 | } | |
829 | ||
830 | size = sizeof(struct xt_counters); | |
831 | size *= info->num_counters; | |
832 | ||
833 | if (size != (u64)len) | |
834 | return ERR_PTR(-EINVAL); | |
835 | ||
836 | mem = vmalloc(len); | |
837 | if (!mem) | |
838 | return ERR_PTR(-ENOMEM); | |
839 | ||
840 | if (copy_from_user(mem, user, len) == 0) | |
841 | return mem; | |
842 | ||
843 | vfree(mem); | |
844 | return ERR_PTR(-EFAULT); | |
845 | } | |
846 | EXPORT_SYMBOL_GPL(xt_copy_counters_from_user); | |
847 | ||
2722971c | 848 | #ifdef CONFIG_COMPAT |
5452e425 | 849 | int xt_compat_target_offset(const struct xt_target *target) |
2722971c | 850 | { |
9fa492cd PM |
851 | u_int16_t csize = target->compatsize ? : target->targetsize; |
852 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); | |
853 | } | |
854 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); | |
855 | ||
856 | void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, | |
b0a6363c | 857 | unsigned int *size) |
9fa492cd | 858 | { |
5452e425 | 859 | const struct xt_target *target = t->u.kernel.target; |
9fa492cd PM |
860 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
861 | int pad, off = xt_compat_target_offset(target); | |
862 | u_int16_t tsize = ct->u.user.target_size; | |
151cc2f5 | 863 | char name[sizeof(t->u.user.name)]; |
9fa492cd PM |
864 | |
865 | t = *dstptr; | |
866 | memcpy(t, ct, sizeof(*ct)); | |
867 | if (target->compat_from_user) | |
868 | target->compat_from_user(t->data, ct->data); | |
869 | else | |
870 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); | |
871 | pad = XT_ALIGN(target->targetsize) - target->targetsize; | |
872 | if (pad > 0) | |
873 | memset(t->data + target->targetsize, 0, pad); | |
874 | ||
875 | tsize += off; | |
876 | t->u.user.target_size = tsize; | |
151cc2f5 FW |
877 | strlcpy(name, target->name, sizeof(name)); |
878 | module_put(target->me); | |
879 | strncpy(t->u.user.name, name, sizeof(t->u.user.name)); | |
9fa492cd PM |
880 | |
881 | *size += off; | |
882 | *dstptr += tsize; | |
883 | } | |
884 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); | |
885 | ||
739674fb JE |
886 | int xt_compat_target_to_user(const struct xt_entry_target *t, |
887 | void __user **dstptr, unsigned int *size) | |
9fa492cd | 888 | { |
5452e425 | 889 | const struct xt_target *target = t->u.kernel.target; |
9fa492cd PM |
890 | struct compat_xt_entry_target __user *ct = *dstptr; |
891 | int off = xt_compat_target_offset(target); | |
892 | u_int16_t tsize = t->u.user.target_size - off; | |
893 | ||
894 | if (copy_to_user(ct, t, sizeof(*ct)) || | |
a18aa31b PM |
895 | put_user(tsize, &ct->u.user.target_size) || |
896 | copy_to_user(ct->u.user.name, t->u.kernel.target->name, | |
897 | strlen(t->u.kernel.target->name) + 1)) | |
601e68e1 | 898 | return -EFAULT; |
9fa492cd PM |
899 | |
900 | if (target->compat_to_user) { | |
901 | if (target->compat_to_user((void __user *)ct->data, t->data)) | |
902 | return -EFAULT; | |
903 | } else { | |
904 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) | |
905 | return -EFAULT; | |
2722971c | 906 | } |
9fa492cd PM |
907 | |
908 | *size -= off; | |
909 | *dstptr += tsize; | |
910 | return 0; | |
2722971c | 911 | } |
9fa492cd | 912 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
2722971c DM |
913 | #endif |
914 | ||
2e4e6a17 HW |
915 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
916 | { | |
917 | struct xt_table_info *newinfo; | |
918 | int cpu; | |
919 | ||
920 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ | |
4481374c | 921 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages) |
2e4e6a17 HW |
922 | return NULL; |
923 | ||
259d4e41 | 924 | newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL); |
2e4e6a17 HW |
925 | if (!newinfo) |
926 | return NULL; | |
927 | ||
928 | newinfo->size = size; | |
929 | ||
6f912042 | 930 | for_each_possible_cpu(cpu) { |
2e4e6a17 HW |
931 | if (size <= PAGE_SIZE) |
932 | newinfo->entries[cpu] = kmalloc_node(size, | |
933 | GFP_KERNEL, | |
934 | cpu_to_node(cpu)); | |
935 | else | |
936 | newinfo->entries[cpu] = vmalloc_node(size, | |
937 | cpu_to_node(cpu)); | |
938 | ||
939 | if (newinfo->entries[cpu] == NULL) { | |
940 | xt_free_table_info(newinfo); | |
941 | return NULL; | |
942 | } | |
943 | } | |
944 | ||
945 | return newinfo; | |
946 | } | |
947 | EXPORT_SYMBOL(xt_alloc_table_info); | |
948 | ||
949 | void xt_free_table_info(struct xt_table_info *info) | |
950 | { | |
951 | int cpu; | |
952 | ||
6f912042 | 953 | for_each_possible_cpu(cpu) { |
2e4e6a17 HW |
954 | if (info->size <= PAGE_SIZE) |
955 | kfree(info->entries[cpu]); | |
956 | else | |
957 | vfree(info->entries[cpu]); | |
958 | } | |
f3c5c1bf JE |
959 | |
960 | if (info->jumpstack != NULL) { | |
961 | if (sizeof(void *) * info->stacksize > PAGE_SIZE) { | |
962 | for_each_possible_cpu(cpu) | |
963 | vfree(info->jumpstack[cpu]); | |
964 | } else { | |
965 | for_each_possible_cpu(cpu) | |
966 | kfree(info->jumpstack[cpu]); | |
967 | } | |
968 | } | |
969 | ||
970 | if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE) | |
971 | vfree(info->jumpstack); | |
972 | else | |
973 | kfree(info->jumpstack); | |
7489aec8 ED |
974 | |
975 | free_percpu(info->stackptr); | |
f3c5c1bf | 976 | |
2e4e6a17 HW |
977 | kfree(info); |
978 | } | |
979 | EXPORT_SYMBOL(xt_free_table_info); | |
980 | ||
981 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ | |
76108cea JE |
982 | struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af, |
983 | const char *name) | |
2e4e6a17 HW |
984 | { |
985 | struct xt_table *t; | |
986 | ||
9e19bb6d | 987 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
2e4e6a17 HW |
988 | return ERR_PTR(-EINTR); |
989 | ||
8d870052 | 990 | list_for_each_entry(t, &net->xt.tables[af], list) |
2e4e6a17 HW |
991 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
992 | return t; | |
9e19bb6d | 993 | mutex_unlock(&xt[af].mutex); |
2e4e6a17 HW |
994 | return NULL; |
995 | } | |
996 | EXPORT_SYMBOL_GPL(xt_find_table_lock); | |
997 | ||
998 | void xt_table_unlock(struct xt_table *table) | |
999 | { | |
9e19bb6d | 1000 | mutex_unlock(&xt[table->af].mutex); |
2e4e6a17 HW |
1001 | } |
1002 | EXPORT_SYMBOL_GPL(xt_table_unlock); | |
1003 | ||
2722971c | 1004 | #ifdef CONFIG_COMPAT |
76108cea | 1005 | void xt_compat_lock(u_int8_t af) |
2722971c DM |
1006 | { |
1007 | mutex_lock(&xt[af].compat_mutex); | |
1008 | } | |
1009 | EXPORT_SYMBOL_GPL(xt_compat_lock); | |
1010 | ||
76108cea | 1011 | void xt_compat_unlock(u_int8_t af) |
2722971c DM |
1012 | { |
1013 | mutex_unlock(&xt[af].compat_mutex); | |
1014 | } | |
1015 | EXPORT_SYMBOL_GPL(xt_compat_unlock); | |
1016 | #endif | |
2e4e6a17 | 1017 | |
7f5c6d4f ED |
1018 | DEFINE_PER_CPU(seqcount_t, xt_recseq); |
1019 | EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq); | |
942e4a2b | 1020 | |
f3c5c1bf JE |
1021 | static int xt_jumpstack_alloc(struct xt_table_info *i) |
1022 | { | |
1023 | unsigned int size; | |
1024 | int cpu; | |
1025 | ||
7489aec8 | 1026 | i->stackptr = alloc_percpu(unsigned int); |
f3c5c1bf JE |
1027 | if (i->stackptr == NULL) |
1028 | return -ENOMEM; | |
f3c5c1bf JE |
1029 | |
1030 | size = sizeof(void **) * nr_cpu_ids; | |
1031 | if (size > PAGE_SIZE) | |
3dbd4439 | 1032 | i->jumpstack = vzalloc(size); |
f3c5c1bf | 1033 | else |
3dbd4439 | 1034 | i->jumpstack = kzalloc(size, GFP_KERNEL); |
f3c5c1bf JE |
1035 | if (i->jumpstack == NULL) |
1036 | return -ENOMEM; | |
f3c5c1bf JE |
1037 | |
1038 | i->stacksize *= xt_jumpstack_multiplier; | |
1039 | size = sizeof(void *) * i->stacksize; | |
1040 | for_each_possible_cpu(cpu) { | |
1041 | if (size > PAGE_SIZE) | |
1042 | i->jumpstack[cpu] = vmalloc_node(size, | |
1043 | cpu_to_node(cpu)); | |
1044 | else | |
1045 | i->jumpstack[cpu] = kmalloc_node(size, | |
1046 | GFP_KERNEL, cpu_to_node(cpu)); | |
1047 | if (i->jumpstack[cpu] == NULL) | |
1048 | /* | |
1049 | * Freeing will be done later on by the callers. The | |
1050 | * chain is: xt_replace_table -> __do_replace -> | |
1051 | * do_replace -> xt_free_table_info. | |
1052 | */ | |
1053 | return -ENOMEM; | |
1054 | } | |
1055 | ||
1056 | return 0; | |
1057 | } | |
942e4a2b | 1058 | |
2e4e6a17 HW |
1059 | struct xt_table_info * |
1060 | xt_replace_table(struct xt_table *table, | |
1061 | unsigned int num_counters, | |
1062 | struct xt_table_info *newinfo, | |
1063 | int *error) | |
1064 | { | |
942e4a2b | 1065 | struct xt_table_info *private; |
f3c5c1bf | 1066 | int ret; |
2e4e6a17 | 1067 | |
d97a9e47 JE |
1068 | ret = xt_jumpstack_alloc(newinfo); |
1069 | if (ret < 0) { | |
1070 | *error = ret; | |
1071 | return NULL; | |
1072 | } | |
1073 | ||
2e4e6a17 | 1074 | /* Do the substitution. */ |
942e4a2b | 1075 | local_bh_disable(); |
2e4e6a17 | 1076 | private = table->private; |
942e4a2b | 1077 | |
2e4e6a17 HW |
1078 | /* Check inside lock: is the old number correct? */ |
1079 | if (num_counters != private->number) { | |
be91fd5e | 1080 | pr_debug("num_counters != table->private->number (%u/%u)\n", |
2e4e6a17 | 1081 | num_counters, private->number); |
942e4a2b | 1082 | local_bh_enable(); |
2e4e6a17 HW |
1083 | *error = -EAGAIN; |
1084 | return NULL; | |
1085 | } | |
2e4e6a17 | 1086 | |
942e4a2b SH |
1087 | table->private = newinfo; |
1088 | newinfo->initial_entries = private->initial_entries; | |
1089 | ||
1090 | /* | |
1091 | * Even though table entries have now been swapped, other CPU's | |
1092 | * may still be using the old entries. This is okay, because | |
1093 | * resynchronization happens because of the locking done | |
1094 | * during the get_counters() routine. | |
1095 | */ | |
1096 | local_bh_enable(); | |
1097 | ||
fbabf31e TG |
1098 | #ifdef CONFIG_AUDIT |
1099 | if (audit_enabled) { | |
1100 | struct audit_buffer *ab; | |
1101 | ||
1102 | ab = audit_log_start(current->audit_context, GFP_KERNEL, | |
1103 | AUDIT_NETFILTER_CFG); | |
1104 | if (ab) { | |
1105 | audit_log_format(ab, "table=%s family=%u entries=%u", | |
1106 | table->name, table->af, | |
1107 | private->number); | |
1108 | audit_log_end(ab); | |
1109 | } | |
1110 | } | |
1111 | #endif | |
1112 | ||
942e4a2b | 1113 | return private; |
2e4e6a17 HW |
1114 | } |
1115 | EXPORT_SYMBOL_GPL(xt_replace_table); | |
1116 | ||
35aad0ff JE |
1117 | struct xt_table *xt_register_table(struct net *net, |
1118 | const struct xt_table *input_table, | |
a98da11d AD |
1119 | struct xt_table_info *bootstrap, |
1120 | struct xt_table_info *newinfo) | |
2e4e6a17 HW |
1121 | { |
1122 | int ret; | |
1123 | struct xt_table_info *private; | |
35aad0ff | 1124 | struct xt_table *t, *table; |
2e4e6a17 | 1125 | |
44d34e72 | 1126 | /* Don't add one object to multiple lists. */ |
35aad0ff | 1127 | table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL); |
44d34e72 AD |
1128 | if (!table) { |
1129 | ret = -ENOMEM; | |
1130 | goto out; | |
1131 | } | |
1132 | ||
9e19bb6d | 1133 | ret = mutex_lock_interruptible(&xt[table->af].mutex); |
2e4e6a17 | 1134 | if (ret != 0) |
44d34e72 | 1135 | goto out_free; |
2e4e6a17 HW |
1136 | |
1137 | /* Don't autoload: we'd eat our tail... */ | |
8d870052 | 1138 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
df0933dc PM |
1139 | if (strcmp(t->name, table->name) == 0) { |
1140 | ret = -EEXIST; | |
1141 | goto unlock; | |
1142 | } | |
2e4e6a17 HW |
1143 | } |
1144 | ||
1145 | /* Simplifies replace_table code. */ | |
1146 | table->private = bootstrap; | |
78454473 | 1147 | |
2e4e6a17 HW |
1148 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
1149 | goto unlock; | |
1150 | ||
1151 | private = table->private; | |
be91fd5e | 1152 | pr_debug("table->private->number = %u\n", private->number); |
2e4e6a17 HW |
1153 | |
1154 | /* save number of initial entries */ | |
1155 | private->initial_entries = private->number; | |
1156 | ||
8d870052 | 1157 | list_add(&table->list, &net->xt.tables[table->af]); |
a98da11d AD |
1158 | mutex_unlock(&xt[table->af].mutex); |
1159 | return table; | |
2e4e6a17 | 1160 | |
2e4e6a17 | 1161 | unlock: |
9e19bb6d | 1162 | mutex_unlock(&xt[table->af].mutex); |
44d34e72 AD |
1163 | out_free: |
1164 | kfree(table); | |
a98da11d AD |
1165 | out: |
1166 | return ERR_PTR(ret); | |
2e4e6a17 HW |
1167 | } |
1168 | EXPORT_SYMBOL_GPL(xt_register_table); | |
1169 | ||
1170 | void *xt_unregister_table(struct xt_table *table) | |
1171 | { | |
1172 | struct xt_table_info *private; | |
1173 | ||
9e19bb6d | 1174 | mutex_lock(&xt[table->af].mutex); |
2e4e6a17 | 1175 | private = table->private; |
df0933dc | 1176 | list_del(&table->list); |
9e19bb6d | 1177 | mutex_unlock(&xt[table->af].mutex); |
44d34e72 | 1178 | kfree(table); |
2e4e6a17 HW |
1179 | |
1180 | return private; | |
1181 | } | |
1182 | EXPORT_SYMBOL_GPL(xt_unregister_table); | |
1183 | ||
1184 | #ifdef CONFIG_PROC_FS | |
715cf35a AD |
1185 | struct xt_names_priv { |
1186 | struct seq_net_private p; | |
76108cea | 1187 | u_int8_t af; |
715cf35a | 1188 | }; |
025d93d1 | 1189 | static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos) |
2e4e6a17 | 1190 | { |
715cf35a | 1191 | struct xt_names_priv *priv = seq->private; |
1218854a | 1192 | struct net *net = seq_file_net(seq); |
76108cea | 1193 | u_int8_t af = priv->af; |
2e4e6a17 | 1194 | |
025d93d1 | 1195 | mutex_lock(&xt[af].mutex); |
715cf35a | 1196 | return seq_list_start(&net->xt.tables[af], *pos); |
025d93d1 | 1197 | } |
2e4e6a17 | 1198 | |
025d93d1 AD |
1199 | static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
1200 | { | |
715cf35a | 1201 | struct xt_names_priv *priv = seq->private; |
1218854a | 1202 | struct net *net = seq_file_net(seq); |
76108cea | 1203 | u_int8_t af = priv->af; |
2e4e6a17 | 1204 | |
715cf35a | 1205 | return seq_list_next(v, &net->xt.tables[af], pos); |
2e4e6a17 HW |
1206 | } |
1207 | ||
025d93d1 | 1208 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
2e4e6a17 | 1209 | { |
715cf35a | 1210 | struct xt_names_priv *priv = seq->private; |
76108cea | 1211 | u_int8_t af = priv->af; |
2e4e6a17 | 1212 | |
025d93d1 AD |
1213 | mutex_unlock(&xt[af].mutex); |
1214 | } | |
2e4e6a17 | 1215 | |
025d93d1 AD |
1216 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
1217 | { | |
1218 | struct xt_table *table = list_entry(v, struct xt_table, list); | |
2e4e6a17 | 1219 | |
025d93d1 AD |
1220 | if (strlen(table->name)) |
1221 | return seq_printf(seq, "%s\n", table->name); | |
1222 | else | |
1223 | return 0; | |
1224 | } | |
601e68e1 | 1225 | |
025d93d1 AD |
1226 | static const struct seq_operations xt_table_seq_ops = { |
1227 | .start = xt_table_seq_start, | |
1228 | .next = xt_table_seq_next, | |
1229 | .stop = xt_table_seq_stop, | |
1230 | .show = xt_table_seq_show, | |
1231 | }; | |
1232 | ||
1233 | static int xt_table_open(struct inode *inode, struct file *file) | |
1234 | { | |
1235 | int ret; | |
715cf35a | 1236 | struct xt_names_priv *priv; |
025d93d1 | 1237 | |
715cf35a AD |
1238 | ret = seq_open_net(inode, file, &xt_table_seq_ops, |
1239 | sizeof(struct xt_names_priv)); | |
025d93d1 | 1240 | if (!ret) { |
715cf35a | 1241 | priv = ((struct seq_file *)file->private_data)->private; |
d9dda78b | 1242 | priv->af = (unsigned long)PDE_DATA(inode); |
025d93d1 AD |
1243 | } |
1244 | return ret; | |
2e4e6a17 HW |
1245 | } |
1246 | ||
025d93d1 AD |
1247 | static const struct file_operations xt_table_ops = { |
1248 | .owner = THIS_MODULE, | |
1249 | .open = xt_table_open, | |
1250 | .read = seq_read, | |
1251 | .llseek = seq_lseek, | |
0e93bb94 | 1252 | .release = seq_release_net, |
025d93d1 AD |
1253 | }; |
1254 | ||
eb132205 JE |
1255 | /* |
1256 | * Traverse state for ip{,6}_{tables,matches} for helping crossing | |
1257 | * the multi-AF mutexes. | |
1258 | */ | |
1259 | struct nf_mttg_trav { | |
1260 | struct list_head *head, *curr; | |
1261 | uint8_t class, nfproto; | |
1262 | }; | |
1263 | ||
1264 | enum { | |
1265 | MTTG_TRAV_INIT, | |
1266 | MTTG_TRAV_NFP_UNSPEC, | |
1267 | MTTG_TRAV_NFP_SPEC, | |
1268 | MTTG_TRAV_DONE, | |
1269 | }; | |
1270 | ||
1271 | static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos, | |
1272 | bool is_target) | |
2e4e6a17 | 1273 | { |
eb132205 JE |
1274 | static const uint8_t next_class[] = { |
1275 | [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC, | |
1276 | [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE, | |
1277 | }; | |
1278 | struct nf_mttg_trav *trav = seq->private; | |
1279 | ||
1280 | switch (trav->class) { | |
1281 | case MTTG_TRAV_INIT: | |
1282 | trav->class = MTTG_TRAV_NFP_UNSPEC; | |
1283 | mutex_lock(&xt[NFPROTO_UNSPEC].mutex); | |
1284 | trav->head = trav->curr = is_target ? | |
1285 | &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match; | |
1286 | break; | |
1287 | case MTTG_TRAV_NFP_UNSPEC: | |
1288 | trav->curr = trav->curr->next; | |
1289 | if (trav->curr != trav->head) | |
1290 | break; | |
1291 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); | |
1292 | mutex_lock(&xt[trav->nfproto].mutex); | |
1293 | trav->head = trav->curr = is_target ? | |
1294 | &xt[trav->nfproto].target : &xt[trav->nfproto].match; | |
1295 | trav->class = next_class[trav->class]; | |
1296 | break; | |
1297 | case MTTG_TRAV_NFP_SPEC: | |
1298 | trav->curr = trav->curr->next; | |
1299 | if (trav->curr != trav->head) | |
1300 | break; | |
1301 | /* fallthru, _stop will unlock */ | |
1302 | default: | |
1303 | return NULL; | |
1304 | } | |
2e4e6a17 | 1305 | |
eb132205 JE |
1306 | if (ppos != NULL) |
1307 | ++*ppos; | |
1308 | return trav; | |
025d93d1 | 1309 | } |
601e68e1 | 1310 | |
eb132205 JE |
1311 | static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos, |
1312 | bool is_target) | |
025d93d1 | 1313 | { |
eb132205 JE |
1314 | struct nf_mttg_trav *trav = seq->private; |
1315 | unsigned int j; | |
2e4e6a17 | 1316 | |
eb132205 JE |
1317 | trav->class = MTTG_TRAV_INIT; |
1318 | for (j = 0; j < *pos; ++j) | |
1319 | if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL) | |
1320 | return NULL; | |
1321 | return trav; | |
2e4e6a17 HW |
1322 | } |
1323 | ||
eb132205 | 1324 | static void xt_mttg_seq_stop(struct seq_file *seq, void *v) |
2e4e6a17 | 1325 | { |
eb132205 JE |
1326 | struct nf_mttg_trav *trav = seq->private; |
1327 | ||
1328 | switch (trav->class) { | |
1329 | case MTTG_TRAV_NFP_UNSPEC: | |
1330 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); | |
1331 | break; | |
1332 | case MTTG_TRAV_NFP_SPEC: | |
1333 | mutex_unlock(&xt[trav->nfproto].mutex); | |
1334 | break; | |
1335 | } | |
1336 | } | |
2e4e6a17 | 1337 | |
eb132205 JE |
1338 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
1339 | { | |
1340 | return xt_mttg_seq_start(seq, pos, false); | |
2e4e6a17 HW |
1341 | } |
1342 | ||
eb132205 | 1343 | static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
2e4e6a17 | 1344 | { |
eb132205 JE |
1345 | return xt_mttg_seq_next(seq, v, ppos, false); |
1346 | } | |
2e4e6a17 | 1347 | |
eb132205 JE |
1348 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
1349 | { | |
1350 | const struct nf_mttg_trav *trav = seq->private; | |
1351 | const struct xt_match *match; | |
1352 | ||
1353 | switch (trav->class) { | |
1354 | case MTTG_TRAV_NFP_UNSPEC: | |
1355 | case MTTG_TRAV_NFP_SPEC: | |
1356 | if (trav->curr == trav->head) | |
1357 | return 0; | |
1358 | match = list_entry(trav->curr, struct xt_match, list); | |
1359 | return (*match->name == '\0') ? 0 : | |
1360 | seq_printf(seq, "%s\n", match->name); | |
1361 | } | |
1362 | return 0; | |
2e4e6a17 HW |
1363 | } |
1364 | ||
025d93d1 AD |
1365 | static const struct seq_operations xt_match_seq_ops = { |
1366 | .start = xt_match_seq_start, | |
1367 | .next = xt_match_seq_next, | |
eb132205 | 1368 | .stop = xt_mttg_seq_stop, |
025d93d1 | 1369 | .show = xt_match_seq_show, |
2e4e6a17 HW |
1370 | }; |
1371 | ||
025d93d1 | 1372 | static int xt_match_open(struct inode *inode, struct file *file) |
2e4e6a17 | 1373 | { |
eb132205 JE |
1374 | struct seq_file *seq; |
1375 | struct nf_mttg_trav *trav; | |
2e4e6a17 HW |
1376 | int ret; |
1377 | ||
eb132205 JE |
1378 | trav = kmalloc(sizeof(*trav), GFP_KERNEL); |
1379 | if (trav == NULL) | |
1380 | return -ENOMEM; | |
2e4e6a17 | 1381 | |
eb132205 JE |
1382 | ret = seq_open(file, &xt_match_seq_ops); |
1383 | if (ret < 0) { | |
1384 | kfree(trav); | |
1385 | return ret; | |
2e4e6a17 | 1386 | } |
eb132205 JE |
1387 | |
1388 | seq = file->private_data; | |
1389 | seq->private = trav; | |
d9dda78b | 1390 | trav->nfproto = (unsigned long)PDE_DATA(inode); |
eb132205 | 1391 | return 0; |
025d93d1 AD |
1392 | } |
1393 | ||
1394 | static const struct file_operations xt_match_ops = { | |
1395 | .owner = THIS_MODULE, | |
1396 | .open = xt_match_open, | |
1397 | .read = seq_read, | |
1398 | .llseek = seq_lseek, | |
eb132205 | 1399 | .release = seq_release_private, |
025d93d1 | 1400 | }; |
2e4e6a17 | 1401 | |
025d93d1 AD |
1402 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
1403 | { | |
eb132205 | 1404 | return xt_mttg_seq_start(seq, pos, true); |
025d93d1 AD |
1405 | } |
1406 | ||
eb132205 | 1407 | static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
025d93d1 | 1408 | { |
eb132205 | 1409 | return xt_mttg_seq_next(seq, v, ppos, true); |
025d93d1 AD |
1410 | } |
1411 | ||
1412 | static int xt_target_seq_show(struct seq_file *seq, void *v) | |
1413 | { | |
eb132205 JE |
1414 | const struct nf_mttg_trav *trav = seq->private; |
1415 | const struct xt_target *target; | |
1416 | ||
1417 | switch (trav->class) { | |
1418 | case MTTG_TRAV_NFP_UNSPEC: | |
1419 | case MTTG_TRAV_NFP_SPEC: | |
1420 | if (trav->curr == trav->head) | |
1421 | return 0; | |
1422 | target = list_entry(trav->curr, struct xt_target, list); | |
1423 | return (*target->name == '\0') ? 0 : | |
1424 | seq_printf(seq, "%s\n", target->name); | |
1425 | } | |
1426 | return 0; | |
025d93d1 AD |
1427 | } |
1428 | ||
1429 | static const struct seq_operations xt_target_seq_ops = { | |
1430 | .start = xt_target_seq_start, | |
1431 | .next = xt_target_seq_next, | |
eb132205 | 1432 | .stop = xt_mttg_seq_stop, |
025d93d1 AD |
1433 | .show = xt_target_seq_show, |
1434 | }; | |
1435 | ||
1436 | static int xt_target_open(struct inode *inode, struct file *file) | |
1437 | { | |
eb132205 JE |
1438 | struct seq_file *seq; |
1439 | struct nf_mttg_trav *trav; | |
025d93d1 AD |
1440 | int ret; |
1441 | ||
eb132205 JE |
1442 | trav = kmalloc(sizeof(*trav), GFP_KERNEL); |
1443 | if (trav == NULL) | |
1444 | return -ENOMEM; | |
025d93d1 | 1445 | |
eb132205 JE |
1446 | ret = seq_open(file, &xt_target_seq_ops); |
1447 | if (ret < 0) { | |
1448 | kfree(trav); | |
1449 | return ret; | |
025d93d1 | 1450 | } |
eb132205 JE |
1451 | |
1452 | seq = file->private_data; | |
1453 | seq->private = trav; | |
d9dda78b | 1454 | trav->nfproto = (unsigned long)PDE_DATA(inode); |
eb132205 | 1455 | return 0; |
2e4e6a17 HW |
1456 | } |
1457 | ||
025d93d1 | 1458 | static const struct file_operations xt_target_ops = { |
2e4e6a17 | 1459 | .owner = THIS_MODULE, |
025d93d1 | 1460 | .open = xt_target_open, |
2e4e6a17 HW |
1461 | .read = seq_read, |
1462 | .llseek = seq_lseek, | |
eb132205 | 1463 | .release = seq_release_private, |
2e4e6a17 HW |
1464 | }; |
1465 | ||
1466 | #define FORMAT_TABLES "_tables_names" | |
1467 | #define FORMAT_MATCHES "_tables_matches" | |
1468 | #define FORMAT_TARGETS "_tables_targets" | |
1469 | ||
1470 | #endif /* CONFIG_PROC_FS */ | |
1471 | ||
2b95efe7 JE |
1472 | /** |
1473 | * xt_hook_link - set up hooks for a new table | |
1474 | * @table: table with metadata needed to set up hooks | |
1475 | * @fn: Hook function | |
1476 | * | |
1477 | * This function will take care of creating and registering the necessary | |
1478 | * Netfilter hooks for XT tables. | |
1479 | */ | |
1480 | struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn) | |
1481 | { | |
1482 | unsigned int hook_mask = table->valid_hooks; | |
1483 | uint8_t i, num_hooks = hweight32(hook_mask); | |
1484 | uint8_t hooknum; | |
1485 | struct nf_hook_ops *ops; | |
1486 | int ret; | |
1487 | ||
1488 | ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL); | |
1489 | if (ops == NULL) | |
1490 | return ERR_PTR(-ENOMEM); | |
1491 | ||
1492 | for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0; | |
1493 | hook_mask >>= 1, ++hooknum) { | |
1494 | if (!(hook_mask & 1)) | |
1495 | continue; | |
1496 | ops[i].hook = fn; | |
1497 | ops[i].owner = table->me; | |
1498 | ops[i].pf = table->af; | |
1499 | ops[i].hooknum = hooknum; | |
1500 | ops[i].priority = table->priority; | |
1501 | ++i; | |
1502 | } | |
1503 | ||
1504 | ret = nf_register_hooks(ops, num_hooks); | |
1505 | if (ret < 0) { | |
1506 | kfree(ops); | |
1507 | return ERR_PTR(ret); | |
1508 | } | |
1509 | ||
1510 | return ops; | |
1511 | } | |
1512 | EXPORT_SYMBOL_GPL(xt_hook_link); | |
1513 | ||
1514 | /** | |
1515 | * xt_hook_unlink - remove hooks for a table | |
1516 | * @ops: nf_hook_ops array as returned by nf_hook_link | |
1517 | * @hook_mask: the very same mask that was passed to nf_hook_link | |
1518 | */ | |
1519 | void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops) | |
1520 | { | |
1521 | nf_unregister_hooks(ops, hweight32(table->valid_hooks)); | |
1522 | kfree(ops); | |
1523 | } | |
1524 | EXPORT_SYMBOL_GPL(xt_hook_unlink); | |
1525 | ||
76108cea | 1526 | int xt_proto_init(struct net *net, u_int8_t af) |
2e4e6a17 HW |
1527 | { |
1528 | #ifdef CONFIG_PROC_FS | |
1529 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
1530 | struct proc_dir_entry *proc; | |
1531 | #endif | |
1532 | ||
7e9c6eeb | 1533 | if (af >= ARRAY_SIZE(xt_prefix)) |
2e4e6a17 HW |
1534 | return -EINVAL; |
1535 | ||
1536 | ||
1537 | #ifdef CONFIG_PROC_FS | |
ce18afe5 | 1538 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1539 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
8b169240 DL |
1540 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops, |
1541 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1542 | if (!proc) |
1543 | goto out; | |
2e4e6a17 | 1544 | |
ce18afe5 | 1545 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1546 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
8b169240 DL |
1547 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops, |
1548 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1549 | if (!proc) |
1550 | goto out_remove_tables; | |
2e4e6a17 | 1551 | |
ce18afe5 | 1552 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1553 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
8b169240 DL |
1554 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops, |
1555 | (void *)(unsigned long)af); | |
2e4e6a17 HW |
1556 | if (!proc) |
1557 | goto out_remove_matches; | |
2e4e6a17 HW |
1558 | #endif |
1559 | ||
1560 | return 0; | |
1561 | ||
1562 | #ifdef CONFIG_PROC_FS | |
1563 | out_remove_matches: | |
ce18afe5 | 1564 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1565 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
ece31ffd | 1566 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 HW |
1567 | |
1568 | out_remove_tables: | |
ce18afe5 | 1569 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1570 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
ece31ffd | 1571 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 HW |
1572 | out: |
1573 | return -1; | |
1574 | #endif | |
1575 | } | |
1576 | EXPORT_SYMBOL_GPL(xt_proto_init); | |
1577 | ||
76108cea | 1578 | void xt_proto_fini(struct net *net, u_int8_t af) |
2e4e6a17 HW |
1579 | { |
1580 | #ifdef CONFIG_PROC_FS | |
1581 | char buf[XT_FUNCTION_MAXNAMELEN]; | |
1582 | ||
ce18afe5 | 1583 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1584 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
ece31ffd | 1585 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 | 1586 | |
ce18afe5 | 1587 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1588 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
ece31ffd | 1589 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 | 1590 | |
ce18afe5 | 1591 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
2e4e6a17 | 1592 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
ece31ffd | 1593 | remove_proc_entry(buf, net->proc_net); |
2e4e6a17 HW |
1594 | #endif /*CONFIG_PROC_FS*/ |
1595 | } | |
1596 | EXPORT_SYMBOL_GPL(xt_proto_fini); | |
1597 | ||
8d870052 AD |
1598 | static int __net_init xt_net_init(struct net *net) |
1599 | { | |
1600 | int i; | |
1601 | ||
7e9c6eeb | 1602 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
8d870052 AD |
1603 | INIT_LIST_HEAD(&net->xt.tables[i]); |
1604 | return 0; | |
1605 | } | |
1606 | ||
1607 | static struct pernet_operations xt_net_ops = { | |
1608 | .init = xt_net_init, | |
1609 | }; | |
2e4e6a17 HW |
1610 | |
1611 | static int __init xt_init(void) | |
1612 | { | |
942e4a2b SH |
1613 | unsigned int i; |
1614 | int rv; | |
1615 | ||
1616 | for_each_possible_cpu(i) { | |
7f5c6d4f | 1617 | seqcount_init(&per_cpu(xt_recseq, i)); |
942e4a2b | 1618 | } |
2e4e6a17 | 1619 | |
7e9c6eeb | 1620 | xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL); |
2e4e6a17 HW |
1621 | if (!xt) |
1622 | return -ENOMEM; | |
1623 | ||
7e9c6eeb | 1624 | for (i = 0; i < NFPROTO_NUMPROTO; i++) { |
9e19bb6d | 1625 | mutex_init(&xt[i].mutex); |
2722971c DM |
1626 | #ifdef CONFIG_COMPAT |
1627 | mutex_init(&xt[i].compat_mutex); | |
255d0dc3 | 1628 | xt[i].compat_tab = NULL; |
2722971c | 1629 | #endif |
2e4e6a17 HW |
1630 | INIT_LIST_HEAD(&xt[i].target); |
1631 | INIT_LIST_HEAD(&xt[i].match); | |
2e4e6a17 | 1632 | } |
8d870052 AD |
1633 | rv = register_pernet_subsys(&xt_net_ops); |
1634 | if (rv < 0) | |
1635 | kfree(xt); | |
1636 | return rv; | |
2e4e6a17 HW |
1637 | } |
1638 | ||
1639 | static void __exit xt_fini(void) | |
1640 | { | |
8d870052 | 1641 | unregister_pernet_subsys(&xt_net_ops); |
2e4e6a17 HW |
1642 | kfree(xt); |
1643 | } | |
1644 | ||
1645 | module_init(xt_init); | |
1646 | module_exit(xt_fini); | |
1647 |