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