netfilter: x_tables: don't reject valid target size on some architectures
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / x_tables.c
1 /*
2 * x_tables core - Backend for {ip,ip6,arp}_tables
3 *
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
5 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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 */
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/module.h>
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>
25 #include <linux/mutex.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/audit.h>
29 #include <net/net_namespace.h>
30
31 #include <linux/netfilter/x_tables.h>
32 #include <linux/netfilter_arp.h>
33 #include <linux/netfilter_ipv4/ip_tables.h>
34 #include <linux/netfilter_ipv6/ip6_tables.h>
35 #include <linux/netfilter_arp/arp_tables.h>
36
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
39 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
40
41 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
42
43 struct compat_delta {
44 unsigned int offset; /* offset in kernel */
45 int delta; /* delta in 32bit user land */
46 };
47
48 struct xt_af {
49 struct mutex mutex;
50 struct list_head match;
51 struct list_head target;
52 #ifdef CONFIG_COMPAT
53 struct mutex compat_mutex;
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[] */
57 #endif
58 };
59
60 static struct xt_af *xt;
61
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",
68 };
69
70 /* Allow this many total (re)entries. */
71 static const unsigned int xt_jumpstack_multiplier = 2;
72
73 /* Registration hooks for targets. */
74 int
75 xt_register_target(struct xt_target *target)
76 {
77 u_int8_t af = target->family;
78 int ret;
79
80 ret = mutex_lock_interruptible(&xt[af].mutex);
81 if (ret != 0)
82 return ret;
83 list_add(&target->list, &xt[af].target);
84 mutex_unlock(&xt[af].mutex);
85 return ret;
86 }
87 EXPORT_SYMBOL(xt_register_target);
88
89 void
90 xt_unregister_target(struct xt_target *target)
91 {
92 u_int8_t af = target->family;
93
94 mutex_lock(&xt[af].mutex);
95 list_del(&target->list);
96 mutex_unlock(&xt[af].mutex);
97 }
98 EXPORT_SYMBOL(xt_unregister_target);
99
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 {
123 while (n-- > 0)
124 xt_unregister_target(&target[n]);
125 }
126 EXPORT_SYMBOL(xt_unregister_targets);
127
128 int
129 xt_register_match(struct xt_match *match)
130 {
131 u_int8_t af = match->family;
132 int ret;
133
134 ret = mutex_lock_interruptible(&xt[af].mutex);
135 if (ret != 0)
136 return ret;
137
138 list_add(&match->list, &xt[af].match);
139 mutex_unlock(&xt[af].mutex);
140
141 return ret;
142 }
143 EXPORT_SYMBOL(xt_register_match);
144
145 void
146 xt_unregister_match(struct xt_match *match)
147 {
148 u_int8_t af = match->family;
149
150 mutex_lock(&xt[af].mutex);
151 list_del(&match->list);
152 mutex_unlock(&xt[af].mutex);
153 }
154 EXPORT_SYMBOL(xt_unregister_match);
155
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 {
179 while (n-- > 0)
180 xt_unregister_match(&match[n]);
181 }
182 EXPORT_SYMBOL(xt_unregister_matches);
183
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
188 * function to use.
189 */
190
191 /* Find match, grabs ref. Returns ERR_PTR() on error. */
192 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
193 {
194 struct xt_match *m;
195 int err = -ENOENT;
196
197 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
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)) {
204 mutex_unlock(&xt[af].mutex);
205 return m;
206 }
207 } else
208 err = -EPROTOTYPE; /* Found something. */
209 }
210 }
211 mutex_unlock(&xt[af].mutex);
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
217 return ERR_PTR(err);
218 }
219 EXPORT_SYMBOL(xt_find_match);
220
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
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;
233 }
234 EXPORT_SYMBOL_GPL(xt_request_find_match);
235
236 /* Find target, grabs ref. Returns ERR_PTR() on error. */
237 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
238 {
239 struct xt_target *t;
240 int err = -ENOENT;
241
242 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
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)) {
249 mutex_unlock(&xt[af].mutex);
250 return t;
251 }
252 } else
253 err = -EPROTOTYPE; /* Found something. */
254 }
255 }
256 mutex_unlock(&xt[af].mutex);
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
262 return ERR_PTR(err);
263 }
264 EXPORT_SYMBOL(xt_find_target);
265
266 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
267 {
268 struct xt_target *target;
269
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;
277 }
278 EXPORT_SYMBOL_GPL(xt_request_find_target);
279
280 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
281 {
282 const struct xt_match *m;
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 }
293
294 if (af != NFPROTO_UNSPEC && !have_rev)
295 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
296
297 return have_rev;
298 }
299
300 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
301 {
302 const struct xt_target *t;
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 }
313
314 if (af != NFPROTO_UNSPEC && !have_rev)
315 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
316
317 return have_rev;
318 }
319
320 /* Returns true or false (if no such extension at all) */
321 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
322 int *err)
323 {
324 int have_rev, best = -1;
325
326 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
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);
334 mutex_unlock(&xt[af].mutex);
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
349 static char *
350 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
351 {
352 static const char *const inetbr_names[] = {
353 "PREROUTING", "INPUT", "FORWARD",
354 "OUTPUT", "POSTROUTING", "BROUTING",
355 };
356 static const char *const arp_names[] = {
357 "INPUT", "FORWARD", "OUTPUT",
358 };
359 const char *const *names;
360 unsigned int i, max;
361 char *p = buf;
362 bool np = false;
363 int res;
364
365 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
366 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
367 ARRAY_SIZE(inetbr_names);
368 *p = '\0';
369 for (i = 0; i < max; ++i) {
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
383 int xt_check_match(struct xt_mtchk_param *par,
384 unsigned int size, u_int8_t proto, bool inv_proto)
385 {
386 int ret;
387
388 if (XT_ALIGN(par->match->matchsize) != size &&
389 par->match->matchsize != -1) {
390 /*
391 * ebt_among is exempt from centralized matchsize checking
392 * because it uses a dynamic-size data set.
393 */
394 pr_err("%s_tables: %s.%u match: invalid size "
395 "%u (kernel) != (user) %u\n",
396 xt_prefix[par->family], par->match->name,
397 par->match->revision,
398 XT_ALIGN(par->match->matchsize), size);
399 return -EINVAL;
400 }
401 if (par->match->table != NULL &&
402 strcmp(par->match->table, par->table) != 0) {
403 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
404 xt_prefix[par->family], par->match->name,
405 par->match->table, par->table);
406 return -EINVAL;
407 }
408 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
409 char used[64], allow[64];
410
411 pr_err("%s_tables: %s match: used from hooks %s, but only "
412 "valid from %s\n",
413 xt_prefix[par->family], par->match->name,
414 textify_hooks(used, sizeof(used), par->hook_mask,
415 par->family),
416 textify_hooks(allow, sizeof(allow), par->match->hooks,
417 par->family));
418 return -EINVAL;
419 }
420 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
421 pr_err("%s_tables: %s match: only valid for protocol %u\n",
422 xt_prefix[par->family], par->match->name,
423 par->match->proto);
424 return -EINVAL;
425 }
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 }
434 return 0;
435 }
436 EXPORT_SYMBOL_GPL(xt_check_match);
437
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
479 #ifdef CONFIG_COMPAT
480 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
481 {
482 struct xt_af *xp = &xt[af];
483
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 }
492
493 if (xp->cur >= xp->number)
494 return -EINVAL;
495
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++;
501 return 0;
502 }
503 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
504
505 void xt_compat_flush_offsets(u_int8_t af)
506 {
507 if (xt[af].compat_tab) {
508 vfree(xt[af].compat_tab);
509 xt[af].compat_tab = NULL;
510 xt[af].number = 0;
511 xt[af].cur = 0;
512 }
513 }
514 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
515
516 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
517 {
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 }
530 return left ? tmp[left - 1].delta : 0;
531 }
532 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
533
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
541 int xt_compat_match_offset(const struct xt_match *match)
542 {
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
548 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
549 unsigned int *size)
550 {
551 const struct xt_match *match = m->u.kernel.match;
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;
555
556 m = *dstptr;
557 memcpy(m, cm, sizeof(*cm));
558 if (match->compat_from_user)
559 match->compat_from_user(m->data, cm->data);
560 else
561 memcpy(m->data, cm->data, msize - sizeof(*cm));
562 pad = XT_ALIGN(match->matchsize) - match->matchsize;
563 if (pad > 0)
564 memset(m->data + match->matchsize, 0, pad);
565
566 msize += off;
567 m->u.user.match_size = msize;
568
569 *size += off;
570 *dstptr += msize;
571 return 0;
572 }
573 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
574
575 int xt_compat_match_to_user(const struct xt_entry_match *m,
576 void __user **dstptr, unsigned int *size)
577 {
578 const struct xt_match *match = m->u.kernel.match;
579 struct compat_xt_entry_match __user *cm = *dstptr;
580 int off = xt_compat_match_offset(match);
581 u_int16_t msize = m->u.user.match_size - off;
582
583 if (copy_to_user(cm, m, sizeof(*cm)) ||
584 put_user(msize, &cm->u.user.match_size) ||
585 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
586 strlen(m->u.kernel.match->name) + 1))
587 return -EFAULT;
588
589 if (match->compat_to_user) {
590 if (match->compat_to_user((void __user *)cm->data, m->data))
591 return -EFAULT;
592 } else {
593 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
594 return -EFAULT;
595 }
596
597 *size -= off;
598 *dstptr += msize;
599 return 0;
600 }
601 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
602
603 /* non-compat version may have padding after verdict */
604 struct compat_xt_standard_target {
605 struct compat_xt_entry_target t;
606 compat_uint_t verdict;
607 };
608
609 int xt_compat_check_entry_offsets(const void *base, const char *elems,
610 unsigned int target_offset,
611 unsigned int next_offset)
612 {
613 long size_of_base_struct = elems - (const char *)base;
614 const struct compat_xt_entry_target *t;
615 const char *e = base;
616
617 if (target_offset < size_of_base_struct)
618 return -EINVAL;
619
620 if (target_offset + sizeof(*t) > next_offset)
621 return -EINVAL;
622
623 t = (void *)(e + target_offset);
624 if (t->u.target_size < sizeof(*t))
625 return -EINVAL;
626
627 if (target_offset + t->u.target_size > next_offset)
628 return -EINVAL;
629
630 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
631 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
632 return -EINVAL;
633
634 /* compat_xt_entry match has less strict aligment requirements,
635 * otherwise they are identical. In case of padding differences
636 * we need to add compat version of xt_check_entry_match.
637 */
638 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
639
640 return xt_check_entry_match(elems, base + target_offset,
641 __alignof__(struct compat_xt_entry_match));
642 }
643 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
644 #endif /* CONFIG_COMPAT */
645
646 /**
647 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
648 *
649 * @base: pointer to arp/ip/ip6t_entry
650 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
651 * @target_offset: the arp/ip/ip6_t->target_offset
652 * @next_offset: the arp/ip/ip6_t->next_offset
653 *
654 * validates that target_offset and next_offset are sane and that all
655 * match sizes (if any) align with the target offset.
656 *
657 * This function does not validate the targets or matches themselves, it
658 * only tests that all the offsets and sizes are correct, that all
659 * match structures are aligned, and that the last structure ends where
660 * the target structure begins.
661 *
662 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
663 *
664 * The arp/ip/ip6t_entry structure @base must have passed following tests:
665 * - it must point to a valid memory location
666 * - base to base + next_offset must be accessible, i.e. not exceed allocated
667 * length.
668 *
669 * A well-formed entry looks like this:
670 *
671 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
672 * e->elems[]-----' | |
673 * matchsize | |
674 * matchsize | |
675 * | |
676 * target_offset---------------------------------' |
677 * next_offset---------------------------------------------------'
678 *
679 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
680 * This is where matches (if any) and the target reside.
681 * target_offset: beginning of target.
682 * next_offset: start of the next rule; also: size of this rule.
683 * Since targets have a minimum size, target_offset + minlen <= next_offset.
684 *
685 * Every match stores its size, sum of sizes must not exceed target_offset.
686 *
687 * Return: 0 on success, negative errno on failure.
688 */
689 int xt_check_entry_offsets(const void *base,
690 const char *elems,
691 unsigned int target_offset,
692 unsigned int next_offset)
693 {
694 long size_of_base_struct = elems - (const char *)base;
695 const struct xt_entry_target *t;
696 const char *e = base;
697
698 /* target start is within the ip/ip6/arpt_entry struct */
699 if (target_offset < size_of_base_struct)
700 return -EINVAL;
701
702 if (target_offset + sizeof(*t) > next_offset)
703 return -EINVAL;
704
705 t = (void *)(e + target_offset);
706 if (t->u.target_size < sizeof(*t))
707 return -EINVAL;
708
709 if (target_offset + t->u.target_size > next_offset)
710 return -EINVAL;
711
712 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
713 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
714 return -EINVAL;
715
716 return xt_check_entry_match(elems, base + target_offset,
717 __alignof__(struct xt_entry_match));
718 }
719 EXPORT_SYMBOL(xt_check_entry_offsets);
720
721 int xt_check_target(struct xt_tgchk_param *par,
722 unsigned int size, u_int8_t proto, bool inv_proto)
723 {
724 int ret;
725
726 if (XT_ALIGN(par->target->targetsize) != size) {
727 pr_err("%s_tables: %s.%u target: invalid size "
728 "%u (kernel) != (user) %u\n",
729 xt_prefix[par->family], par->target->name,
730 par->target->revision,
731 XT_ALIGN(par->target->targetsize), size);
732 return -EINVAL;
733 }
734 if (par->target->table != NULL &&
735 strcmp(par->target->table, par->table) != 0) {
736 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
737 xt_prefix[par->family], par->target->name,
738 par->target->table, par->table);
739 return -EINVAL;
740 }
741 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
742 char used[64], allow[64];
743
744 pr_err("%s_tables: %s target: used from hooks %s, but only "
745 "usable from %s\n",
746 xt_prefix[par->family], par->target->name,
747 textify_hooks(used, sizeof(used), par->hook_mask,
748 par->family),
749 textify_hooks(allow, sizeof(allow), par->target->hooks,
750 par->family));
751 return -EINVAL;
752 }
753 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
754 pr_err("%s_tables: %s target: only valid for protocol %u\n",
755 xt_prefix[par->family], par->target->name,
756 par->target->proto);
757 return -EINVAL;
758 }
759 if (par->target->checkentry != NULL) {
760 ret = par->target->checkentry(par);
761 if (ret < 0)
762 return ret;
763 else if (ret > 0)
764 /* Flag up potential errors. */
765 return -EIO;
766 }
767 return 0;
768 }
769 EXPORT_SYMBOL_GPL(xt_check_target);
770
771 #ifdef CONFIG_COMPAT
772 int xt_compat_target_offset(const struct xt_target *target)
773 {
774 u_int16_t csize = target->compatsize ? : target->targetsize;
775 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
776 }
777 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
778
779 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
780 unsigned int *size)
781 {
782 const struct xt_target *target = t->u.kernel.target;
783 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
784 int pad, off = xt_compat_target_offset(target);
785 u_int16_t tsize = ct->u.user.target_size;
786
787 t = *dstptr;
788 memcpy(t, ct, sizeof(*ct));
789 if (target->compat_from_user)
790 target->compat_from_user(t->data, ct->data);
791 else
792 memcpy(t->data, ct->data, tsize - sizeof(*ct));
793 pad = XT_ALIGN(target->targetsize) - target->targetsize;
794 if (pad > 0)
795 memset(t->data + target->targetsize, 0, pad);
796
797 tsize += off;
798 t->u.user.target_size = tsize;
799
800 *size += off;
801 *dstptr += tsize;
802 }
803 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
804
805 int xt_compat_target_to_user(const struct xt_entry_target *t,
806 void __user **dstptr, unsigned int *size)
807 {
808 const struct xt_target *target = t->u.kernel.target;
809 struct compat_xt_entry_target __user *ct = *dstptr;
810 int off = xt_compat_target_offset(target);
811 u_int16_t tsize = t->u.user.target_size - off;
812
813 if (copy_to_user(ct, t, sizeof(*ct)) ||
814 put_user(tsize, &ct->u.user.target_size) ||
815 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
816 strlen(t->u.kernel.target->name) + 1))
817 return -EFAULT;
818
819 if (target->compat_to_user) {
820 if (target->compat_to_user((void __user *)ct->data, t->data))
821 return -EFAULT;
822 } else {
823 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
824 return -EFAULT;
825 }
826
827 *size -= off;
828 *dstptr += tsize;
829 return 0;
830 }
831 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
832 #endif
833
834 struct xt_table_info *xt_alloc_table_info(unsigned int size)
835 {
836 struct xt_table_info *newinfo;
837 int cpu;
838
839 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
840 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
841 return NULL;
842
843 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
844 if (!newinfo)
845 return NULL;
846
847 newinfo->size = size;
848
849 for_each_possible_cpu(cpu) {
850 if (size <= PAGE_SIZE)
851 newinfo->entries[cpu] = kmalloc_node(size,
852 GFP_KERNEL,
853 cpu_to_node(cpu));
854 else
855 newinfo->entries[cpu] = vmalloc_node(size,
856 cpu_to_node(cpu));
857
858 if (newinfo->entries[cpu] == NULL) {
859 xt_free_table_info(newinfo);
860 return NULL;
861 }
862 }
863
864 return newinfo;
865 }
866 EXPORT_SYMBOL(xt_alloc_table_info);
867
868 void xt_free_table_info(struct xt_table_info *info)
869 {
870 int cpu;
871
872 for_each_possible_cpu(cpu) {
873 if (info->size <= PAGE_SIZE)
874 kfree(info->entries[cpu]);
875 else
876 vfree(info->entries[cpu]);
877 }
878
879 if (info->jumpstack != NULL) {
880 if (sizeof(void *) * info->stacksize > PAGE_SIZE) {
881 for_each_possible_cpu(cpu)
882 vfree(info->jumpstack[cpu]);
883 } else {
884 for_each_possible_cpu(cpu)
885 kfree(info->jumpstack[cpu]);
886 }
887 }
888
889 if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE)
890 vfree(info->jumpstack);
891 else
892 kfree(info->jumpstack);
893
894 free_percpu(info->stackptr);
895
896 kfree(info);
897 }
898 EXPORT_SYMBOL(xt_free_table_info);
899
900 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
901 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
902 const char *name)
903 {
904 struct xt_table *t;
905
906 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
907 return ERR_PTR(-EINTR);
908
909 list_for_each_entry(t, &net->xt.tables[af], list)
910 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
911 return t;
912 mutex_unlock(&xt[af].mutex);
913 return NULL;
914 }
915 EXPORT_SYMBOL_GPL(xt_find_table_lock);
916
917 void xt_table_unlock(struct xt_table *table)
918 {
919 mutex_unlock(&xt[table->af].mutex);
920 }
921 EXPORT_SYMBOL_GPL(xt_table_unlock);
922
923 #ifdef CONFIG_COMPAT
924 void xt_compat_lock(u_int8_t af)
925 {
926 mutex_lock(&xt[af].compat_mutex);
927 }
928 EXPORT_SYMBOL_GPL(xt_compat_lock);
929
930 void xt_compat_unlock(u_int8_t af)
931 {
932 mutex_unlock(&xt[af].compat_mutex);
933 }
934 EXPORT_SYMBOL_GPL(xt_compat_unlock);
935 #endif
936
937 DEFINE_PER_CPU(seqcount_t, xt_recseq);
938 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
939
940 static int xt_jumpstack_alloc(struct xt_table_info *i)
941 {
942 unsigned int size;
943 int cpu;
944
945 i->stackptr = alloc_percpu(unsigned int);
946 if (i->stackptr == NULL)
947 return -ENOMEM;
948
949 size = sizeof(void **) * nr_cpu_ids;
950 if (size > PAGE_SIZE)
951 i->jumpstack = vzalloc(size);
952 else
953 i->jumpstack = kzalloc(size, GFP_KERNEL);
954 if (i->jumpstack == NULL)
955 return -ENOMEM;
956
957 i->stacksize *= xt_jumpstack_multiplier;
958 size = sizeof(void *) * i->stacksize;
959 for_each_possible_cpu(cpu) {
960 if (size > PAGE_SIZE)
961 i->jumpstack[cpu] = vmalloc_node(size,
962 cpu_to_node(cpu));
963 else
964 i->jumpstack[cpu] = kmalloc_node(size,
965 GFP_KERNEL, cpu_to_node(cpu));
966 if (i->jumpstack[cpu] == NULL)
967 /*
968 * Freeing will be done later on by the callers. The
969 * chain is: xt_replace_table -> __do_replace ->
970 * do_replace -> xt_free_table_info.
971 */
972 return -ENOMEM;
973 }
974
975 return 0;
976 }
977
978 struct xt_table_info *
979 xt_replace_table(struct xt_table *table,
980 unsigned int num_counters,
981 struct xt_table_info *newinfo,
982 int *error)
983 {
984 struct xt_table_info *private;
985 int ret;
986
987 ret = xt_jumpstack_alloc(newinfo);
988 if (ret < 0) {
989 *error = ret;
990 return NULL;
991 }
992
993 /* Do the substitution. */
994 local_bh_disable();
995 private = table->private;
996
997 /* Check inside lock: is the old number correct? */
998 if (num_counters != private->number) {
999 pr_debug("num_counters != table->private->number (%u/%u)\n",
1000 num_counters, private->number);
1001 local_bh_enable();
1002 *error = -EAGAIN;
1003 return NULL;
1004 }
1005
1006 table->private = newinfo;
1007 newinfo->initial_entries = private->initial_entries;
1008
1009 /*
1010 * Even though table entries have now been swapped, other CPU's
1011 * may still be using the old entries. This is okay, because
1012 * resynchronization happens because of the locking done
1013 * during the get_counters() routine.
1014 */
1015 local_bh_enable();
1016
1017 #ifdef CONFIG_AUDIT
1018 if (audit_enabled) {
1019 struct audit_buffer *ab;
1020
1021 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1022 AUDIT_NETFILTER_CFG);
1023 if (ab) {
1024 audit_log_format(ab, "table=%s family=%u entries=%u",
1025 table->name, table->af,
1026 private->number);
1027 audit_log_end(ab);
1028 }
1029 }
1030 #endif
1031
1032 return private;
1033 }
1034 EXPORT_SYMBOL_GPL(xt_replace_table);
1035
1036 struct xt_table *xt_register_table(struct net *net,
1037 const struct xt_table *input_table,
1038 struct xt_table_info *bootstrap,
1039 struct xt_table_info *newinfo)
1040 {
1041 int ret;
1042 struct xt_table_info *private;
1043 struct xt_table *t, *table;
1044
1045 /* Don't add one object to multiple lists. */
1046 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1047 if (!table) {
1048 ret = -ENOMEM;
1049 goto out;
1050 }
1051
1052 ret = mutex_lock_interruptible(&xt[table->af].mutex);
1053 if (ret != 0)
1054 goto out_free;
1055
1056 /* Don't autoload: we'd eat our tail... */
1057 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1058 if (strcmp(t->name, table->name) == 0) {
1059 ret = -EEXIST;
1060 goto unlock;
1061 }
1062 }
1063
1064 /* Simplifies replace_table code. */
1065 table->private = bootstrap;
1066
1067 if (!xt_replace_table(table, 0, newinfo, &ret))
1068 goto unlock;
1069
1070 private = table->private;
1071 pr_debug("table->private->number = %u\n", private->number);
1072
1073 /* save number of initial entries */
1074 private->initial_entries = private->number;
1075
1076 list_add(&table->list, &net->xt.tables[table->af]);
1077 mutex_unlock(&xt[table->af].mutex);
1078 return table;
1079
1080 unlock:
1081 mutex_unlock(&xt[table->af].mutex);
1082 out_free:
1083 kfree(table);
1084 out:
1085 return ERR_PTR(ret);
1086 }
1087 EXPORT_SYMBOL_GPL(xt_register_table);
1088
1089 void *xt_unregister_table(struct xt_table *table)
1090 {
1091 struct xt_table_info *private;
1092
1093 mutex_lock(&xt[table->af].mutex);
1094 private = table->private;
1095 list_del(&table->list);
1096 mutex_unlock(&xt[table->af].mutex);
1097 kfree(table);
1098
1099 return private;
1100 }
1101 EXPORT_SYMBOL_GPL(xt_unregister_table);
1102
1103 #ifdef CONFIG_PROC_FS
1104 struct xt_names_priv {
1105 struct seq_net_private p;
1106 u_int8_t af;
1107 };
1108 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1109 {
1110 struct xt_names_priv *priv = seq->private;
1111 struct net *net = seq_file_net(seq);
1112 u_int8_t af = priv->af;
1113
1114 mutex_lock(&xt[af].mutex);
1115 return seq_list_start(&net->xt.tables[af], *pos);
1116 }
1117
1118 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1119 {
1120 struct xt_names_priv *priv = seq->private;
1121 struct net *net = seq_file_net(seq);
1122 u_int8_t af = priv->af;
1123
1124 return seq_list_next(v, &net->xt.tables[af], pos);
1125 }
1126
1127 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1128 {
1129 struct xt_names_priv *priv = seq->private;
1130 u_int8_t af = priv->af;
1131
1132 mutex_unlock(&xt[af].mutex);
1133 }
1134
1135 static int xt_table_seq_show(struct seq_file *seq, void *v)
1136 {
1137 struct xt_table *table = list_entry(v, struct xt_table, list);
1138
1139 if (strlen(table->name))
1140 return seq_printf(seq, "%s\n", table->name);
1141 else
1142 return 0;
1143 }
1144
1145 static const struct seq_operations xt_table_seq_ops = {
1146 .start = xt_table_seq_start,
1147 .next = xt_table_seq_next,
1148 .stop = xt_table_seq_stop,
1149 .show = xt_table_seq_show,
1150 };
1151
1152 static int xt_table_open(struct inode *inode, struct file *file)
1153 {
1154 int ret;
1155 struct xt_names_priv *priv;
1156
1157 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1158 sizeof(struct xt_names_priv));
1159 if (!ret) {
1160 priv = ((struct seq_file *)file->private_data)->private;
1161 priv->af = (unsigned long)PDE_DATA(inode);
1162 }
1163 return ret;
1164 }
1165
1166 static const struct file_operations xt_table_ops = {
1167 .owner = THIS_MODULE,
1168 .open = xt_table_open,
1169 .read = seq_read,
1170 .llseek = seq_lseek,
1171 .release = seq_release_net,
1172 };
1173
1174 /*
1175 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1176 * the multi-AF mutexes.
1177 */
1178 struct nf_mttg_trav {
1179 struct list_head *head, *curr;
1180 uint8_t class, nfproto;
1181 };
1182
1183 enum {
1184 MTTG_TRAV_INIT,
1185 MTTG_TRAV_NFP_UNSPEC,
1186 MTTG_TRAV_NFP_SPEC,
1187 MTTG_TRAV_DONE,
1188 };
1189
1190 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1191 bool is_target)
1192 {
1193 static const uint8_t next_class[] = {
1194 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1195 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1196 };
1197 struct nf_mttg_trav *trav = seq->private;
1198
1199 switch (trav->class) {
1200 case MTTG_TRAV_INIT:
1201 trav->class = MTTG_TRAV_NFP_UNSPEC;
1202 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1203 trav->head = trav->curr = is_target ?
1204 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1205 break;
1206 case MTTG_TRAV_NFP_UNSPEC:
1207 trav->curr = trav->curr->next;
1208 if (trav->curr != trav->head)
1209 break;
1210 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1211 mutex_lock(&xt[trav->nfproto].mutex);
1212 trav->head = trav->curr = is_target ?
1213 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1214 trav->class = next_class[trav->class];
1215 break;
1216 case MTTG_TRAV_NFP_SPEC:
1217 trav->curr = trav->curr->next;
1218 if (trav->curr != trav->head)
1219 break;
1220 /* fallthru, _stop will unlock */
1221 default:
1222 return NULL;
1223 }
1224
1225 if (ppos != NULL)
1226 ++*ppos;
1227 return trav;
1228 }
1229
1230 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1231 bool is_target)
1232 {
1233 struct nf_mttg_trav *trav = seq->private;
1234 unsigned int j;
1235
1236 trav->class = MTTG_TRAV_INIT;
1237 for (j = 0; j < *pos; ++j)
1238 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1239 return NULL;
1240 return trav;
1241 }
1242
1243 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1244 {
1245 struct nf_mttg_trav *trav = seq->private;
1246
1247 switch (trav->class) {
1248 case MTTG_TRAV_NFP_UNSPEC:
1249 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1250 break;
1251 case MTTG_TRAV_NFP_SPEC:
1252 mutex_unlock(&xt[trav->nfproto].mutex);
1253 break;
1254 }
1255 }
1256
1257 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1258 {
1259 return xt_mttg_seq_start(seq, pos, false);
1260 }
1261
1262 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1263 {
1264 return xt_mttg_seq_next(seq, v, ppos, false);
1265 }
1266
1267 static int xt_match_seq_show(struct seq_file *seq, void *v)
1268 {
1269 const struct nf_mttg_trav *trav = seq->private;
1270 const struct xt_match *match;
1271
1272 switch (trav->class) {
1273 case MTTG_TRAV_NFP_UNSPEC:
1274 case MTTG_TRAV_NFP_SPEC:
1275 if (trav->curr == trav->head)
1276 return 0;
1277 match = list_entry(trav->curr, struct xt_match, list);
1278 return (*match->name == '\0') ? 0 :
1279 seq_printf(seq, "%s\n", match->name);
1280 }
1281 return 0;
1282 }
1283
1284 static const struct seq_operations xt_match_seq_ops = {
1285 .start = xt_match_seq_start,
1286 .next = xt_match_seq_next,
1287 .stop = xt_mttg_seq_stop,
1288 .show = xt_match_seq_show,
1289 };
1290
1291 static int xt_match_open(struct inode *inode, struct file *file)
1292 {
1293 struct seq_file *seq;
1294 struct nf_mttg_trav *trav;
1295 int ret;
1296
1297 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1298 if (trav == NULL)
1299 return -ENOMEM;
1300
1301 ret = seq_open(file, &xt_match_seq_ops);
1302 if (ret < 0) {
1303 kfree(trav);
1304 return ret;
1305 }
1306
1307 seq = file->private_data;
1308 seq->private = trav;
1309 trav->nfproto = (unsigned long)PDE_DATA(inode);
1310 return 0;
1311 }
1312
1313 static const struct file_operations xt_match_ops = {
1314 .owner = THIS_MODULE,
1315 .open = xt_match_open,
1316 .read = seq_read,
1317 .llseek = seq_lseek,
1318 .release = seq_release_private,
1319 };
1320
1321 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1322 {
1323 return xt_mttg_seq_start(seq, pos, true);
1324 }
1325
1326 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1327 {
1328 return xt_mttg_seq_next(seq, v, ppos, true);
1329 }
1330
1331 static int xt_target_seq_show(struct seq_file *seq, void *v)
1332 {
1333 const struct nf_mttg_trav *trav = seq->private;
1334 const struct xt_target *target;
1335
1336 switch (trav->class) {
1337 case MTTG_TRAV_NFP_UNSPEC:
1338 case MTTG_TRAV_NFP_SPEC:
1339 if (trav->curr == trav->head)
1340 return 0;
1341 target = list_entry(trav->curr, struct xt_target, list);
1342 return (*target->name == '\0') ? 0 :
1343 seq_printf(seq, "%s\n", target->name);
1344 }
1345 return 0;
1346 }
1347
1348 static const struct seq_operations xt_target_seq_ops = {
1349 .start = xt_target_seq_start,
1350 .next = xt_target_seq_next,
1351 .stop = xt_mttg_seq_stop,
1352 .show = xt_target_seq_show,
1353 };
1354
1355 static int xt_target_open(struct inode *inode, struct file *file)
1356 {
1357 struct seq_file *seq;
1358 struct nf_mttg_trav *trav;
1359 int ret;
1360
1361 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1362 if (trav == NULL)
1363 return -ENOMEM;
1364
1365 ret = seq_open(file, &xt_target_seq_ops);
1366 if (ret < 0) {
1367 kfree(trav);
1368 return ret;
1369 }
1370
1371 seq = file->private_data;
1372 seq->private = trav;
1373 trav->nfproto = (unsigned long)PDE_DATA(inode);
1374 return 0;
1375 }
1376
1377 static const struct file_operations xt_target_ops = {
1378 .owner = THIS_MODULE,
1379 .open = xt_target_open,
1380 .read = seq_read,
1381 .llseek = seq_lseek,
1382 .release = seq_release_private,
1383 };
1384
1385 #define FORMAT_TABLES "_tables_names"
1386 #define FORMAT_MATCHES "_tables_matches"
1387 #define FORMAT_TARGETS "_tables_targets"
1388
1389 #endif /* CONFIG_PROC_FS */
1390
1391 /**
1392 * xt_hook_link - set up hooks for a new table
1393 * @table: table with metadata needed to set up hooks
1394 * @fn: Hook function
1395 *
1396 * This function will take care of creating and registering the necessary
1397 * Netfilter hooks for XT tables.
1398 */
1399 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1400 {
1401 unsigned int hook_mask = table->valid_hooks;
1402 uint8_t i, num_hooks = hweight32(hook_mask);
1403 uint8_t hooknum;
1404 struct nf_hook_ops *ops;
1405 int ret;
1406
1407 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1408 if (ops == NULL)
1409 return ERR_PTR(-ENOMEM);
1410
1411 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1412 hook_mask >>= 1, ++hooknum) {
1413 if (!(hook_mask & 1))
1414 continue;
1415 ops[i].hook = fn;
1416 ops[i].owner = table->me;
1417 ops[i].pf = table->af;
1418 ops[i].hooknum = hooknum;
1419 ops[i].priority = table->priority;
1420 ++i;
1421 }
1422
1423 ret = nf_register_hooks(ops, num_hooks);
1424 if (ret < 0) {
1425 kfree(ops);
1426 return ERR_PTR(ret);
1427 }
1428
1429 return ops;
1430 }
1431 EXPORT_SYMBOL_GPL(xt_hook_link);
1432
1433 /**
1434 * xt_hook_unlink - remove hooks for a table
1435 * @ops: nf_hook_ops array as returned by nf_hook_link
1436 * @hook_mask: the very same mask that was passed to nf_hook_link
1437 */
1438 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1439 {
1440 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1441 kfree(ops);
1442 }
1443 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1444
1445 int xt_proto_init(struct net *net, u_int8_t af)
1446 {
1447 #ifdef CONFIG_PROC_FS
1448 char buf[XT_FUNCTION_MAXNAMELEN];
1449 struct proc_dir_entry *proc;
1450 #endif
1451
1452 if (af >= ARRAY_SIZE(xt_prefix))
1453 return -EINVAL;
1454
1455
1456 #ifdef CONFIG_PROC_FS
1457 strlcpy(buf, xt_prefix[af], sizeof(buf));
1458 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1459 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1460 (void *)(unsigned long)af);
1461 if (!proc)
1462 goto out;
1463
1464 strlcpy(buf, xt_prefix[af], sizeof(buf));
1465 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1466 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1467 (void *)(unsigned long)af);
1468 if (!proc)
1469 goto out_remove_tables;
1470
1471 strlcpy(buf, xt_prefix[af], sizeof(buf));
1472 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1473 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1474 (void *)(unsigned long)af);
1475 if (!proc)
1476 goto out_remove_matches;
1477 #endif
1478
1479 return 0;
1480
1481 #ifdef CONFIG_PROC_FS
1482 out_remove_matches:
1483 strlcpy(buf, xt_prefix[af], sizeof(buf));
1484 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1485 remove_proc_entry(buf, net->proc_net);
1486
1487 out_remove_tables:
1488 strlcpy(buf, xt_prefix[af], sizeof(buf));
1489 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1490 remove_proc_entry(buf, net->proc_net);
1491 out:
1492 return -1;
1493 #endif
1494 }
1495 EXPORT_SYMBOL_GPL(xt_proto_init);
1496
1497 void xt_proto_fini(struct net *net, u_int8_t af)
1498 {
1499 #ifdef CONFIG_PROC_FS
1500 char buf[XT_FUNCTION_MAXNAMELEN];
1501
1502 strlcpy(buf, xt_prefix[af], sizeof(buf));
1503 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1504 remove_proc_entry(buf, net->proc_net);
1505
1506 strlcpy(buf, xt_prefix[af], sizeof(buf));
1507 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1508 remove_proc_entry(buf, net->proc_net);
1509
1510 strlcpy(buf, xt_prefix[af], sizeof(buf));
1511 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1512 remove_proc_entry(buf, net->proc_net);
1513 #endif /*CONFIG_PROC_FS*/
1514 }
1515 EXPORT_SYMBOL_GPL(xt_proto_fini);
1516
1517 static int __net_init xt_net_init(struct net *net)
1518 {
1519 int i;
1520
1521 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1522 INIT_LIST_HEAD(&net->xt.tables[i]);
1523 return 0;
1524 }
1525
1526 static struct pernet_operations xt_net_ops = {
1527 .init = xt_net_init,
1528 };
1529
1530 static int __init xt_init(void)
1531 {
1532 unsigned int i;
1533 int rv;
1534
1535 for_each_possible_cpu(i) {
1536 seqcount_init(&per_cpu(xt_recseq, i));
1537 }
1538
1539 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1540 if (!xt)
1541 return -ENOMEM;
1542
1543 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1544 mutex_init(&xt[i].mutex);
1545 #ifdef CONFIG_COMPAT
1546 mutex_init(&xt[i].compat_mutex);
1547 xt[i].compat_tab = NULL;
1548 #endif
1549 INIT_LIST_HEAD(&xt[i].target);
1550 INIT_LIST_HEAD(&xt[i].match);
1551 }
1552 rv = register_pernet_subsys(&xt_net_ops);
1553 if (rv < 0)
1554 kfree(xt);
1555 return rv;
1556 }
1557
1558 static void __exit xt_fini(void)
1559 {
1560 unregister_pernet_subsys(&xt_net_ops);
1561 kfree(xt);
1562 }
1563
1564 module_init(xt_init);
1565 module_exit(xt_fini);
1566