netfilter: x_tables: xt_compat_match_from_user doesn't need a retval
[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 void 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 }
572 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
573
574 int xt_compat_match_to_user(const struct xt_entry_match *m,
575 void __user **dstptr, unsigned int *size)
576 {
577 const struct xt_match *match = m->u.kernel.match;
578 struct compat_xt_entry_match __user *cm = *dstptr;
579 int off = xt_compat_match_offset(match);
580 u_int16_t msize = m->u.user.match_size - off;
581
582 if (copy_to_user(cm, m, sizeof(*cm)) ||
583 put_user(msize, &cm->u.user.match_size) ||
584 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
585 strlen(m->u.kernel.match->name) + 1))
586 return -EFAULT;
587
588 if (match->compat_to_user) {
589 if (match->compat_to_user((void __user *)cm->data, m->data))
590 return -EFAULT;
591 } else {
592 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
593 return -EFAULT;
594 }
595
596 *size -= off;
597 *dstptr += msize;
598 return 0;
599 }
600 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
601
602 /* non-compat version may have padding after verdict */
603 struct compat_xt_standard_target {
604 struct compat_xt_entry_target t;
605 compat_uint_t verdict;
606 };
607
608 int xt_compat_check_entry_offsets(const void *base, const char *elems,
609 unsigned int target_offset,
610 unsigned int next_offset)
611 {
612 long size_of_base_struct = elems - (const char *)base;
613 const struct compat_xt_entry_target *t;
614 const char *e = base;
615
616 if (target_offset < size_of_base_struct)
617 return -EINVAL;
618
619 if (target_offset + sizeof(*t) > next_offset)
620 return -EINVAL;
621
622 t = (void *)(e + target_offset);
623 if (t->u.target_size < sizeof(*t))
624 return -EINVAL;
625
626 if (target_offset + t->u.target_size > next_offset)
627 return -EINVAL;
628
629 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
630 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
631 return -EINVAL;
632
633 /* compat_xt_entry match has less strict aligment requirements,
634 * otherwise they are identical. In case of padding differences
635 * we need to add compat version of xt_check_entry_match.
636 */
637 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
638
639 return xt_check_entry_match(elems, base + target_offset,
640 __alignof__(struct compat_xt_entry_match));
641 }
642 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
643 #endif /* CONFIG_COMPAT */
644
645 /**
646 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
647 *
648 * @base: pointer to arp/ip/ip6t_entry
649 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
650 * @target_offset: the arp/ip/ip6_t->target_offset
651 * @next_offset: the arp/ip/ip6_t->next_offset
652 *
653 * validates that target_offset and next_offset are sane and that all
654 * match sizes (if any) align with the target offset.
655 *
656 * This function does not validate the targets or matches themselves, it
657 * only tests that all the offsets and sizes are correct, that all
658 * match structures are aligned, and that the last structure ends where
659 * the target structure begins.
660 *
661 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
662 *
663 * The arp/ip/ip6t_entry structure @base must have passed following tests:
664 * - it must point to a valid memory location
665 * - base to base + next_offset must be accessible, i.e. not exceed allocated
666 * length.
667 *
668 * A well-formed entry looks like this:
669 *
670 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
671 * e->elems[]-----' | |
672 * matchsize | |
673 * matchsize | |
674 * | |
675 * target_offset---------------------------------' |
676 * next_offset---------------------------------------------------'
677 *
678 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
679 * This is where matches (if any) and the target reside.
680 * target_offset: beginning of target.
681 * next_offset: start of the next rule; also: size of this rule.
682 * Since targets have a minimum size, target_offset + minlen <= next_offset.
683 *
684 * Every match stores its size, sum of sizes must not exceed target_offset.
685 *
686 * Return: 0 on success, negative errno on failure.
687 */
688 int xt_check_entry_offsets(const void *base,
689 const char *elems,
690 unsigned int target_offset,
691 unsigned int next_offset)
692 {
693 long size_of_base_struct = elems - (const char *)base;
694 const struct xt_entry_target *t;
695 const char *e = base;
696
697 /* target start is within the ip/ip6/arpt_entry struct */
698 if (target_offset < size_of_base_struct)
699 return -EINVAL;
700
701 if (target_offset + sizeof(*t) > next_offset)
702 return -EINVAL;
703
704 t = (void *)(e + target_offset);
705 if (t->u.target_size < sizeof(*t))
706 return -EINVAL;
707
708 if (target_offset + t->u.target_size > next_offset)
709 return -EINVAL;
710
711 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
712 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
713 return -EINVAL;
714
715 return xt_check_entry_match(elems, base + target_offset,
716 __alignof__(struct xt_entry_match));
717 }
718 EXPORT_SYMBOL(xt_check_entry_offsets);
719
720 int xt_check_target(struct xt_tgchk_param *par,
721 unsigned int size, u_int8_t proto, bool inv_proto)
722 {
723 int ret;
724
725 if (XT_ALIGN(par->target->targetsize) != size) {
726 pr_err("%s_tables: %s.%u target: invalid size "
727 "%u (kernel) != (user) %u\n",
728 xt_prefix[par->family], par->target->name,
729 par->target->revision,
730 XT_ALIGN(par->target->targetsize), size);
731 return -EINVAL;
732 }
733 if (par->target->table != NULL &&
734 strcmp(par->target->table, par->table) != 0) {
735 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
736 xt_prefix[par->family], par->target->name,
737 par->target->table, par->table);
738 return -EINVAL;
739 }
740 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
741 char used[64], allow[64];
742
743 pr_err("%s_tables: %s target: used from hooks %s, but only "
744 "usable from %s\n",
745 xt_prefix[par->family], par->target->name,
746 textify_hooks(used, sizeof(used), par->hook_mask,
747 par->family),
748 textify_hooks(allow, sizeof(allow), par->target->hooks,
749 par->family));
750 return -EINVAL;
751 }
752 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
753 pr_err("%s_tables: %s target: only valid for protocol %u\n",
754 xt_prefix[par->family], par->target->name,
755 par->target->proto);
756 return -EINVAL;
757 }
758 if (par->target->checkentry != NULL) {
759 ret = par->target->checkentry(par);
760 if (ret < 0)
761 return ret;
762 else if (ret > 0)
763 /* Flag up potential errors. */
764 return -EIO;
765 }
766 return 0;
767 }
768 EXPORT_SYMBOL_GPL(xt_check_target);
769
770 #ifdef CONFIG_COMPAT
771 int xt_compat_target_offset(const struct xt_target *target)
772 {
773 u_int16_t csize = target->compatsize ? : target->targetsize;
774 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
775 }
776 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
777
778 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
779 unsigned int *size)
780 {
781 const struct xt_target *target = t->u.kernel.target;
782 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
783 int pad, off = xt_compat_target_offset(target);
784 u_int16_t tsize = ct->u.user.target_size;
785
786 t = *dstptr;
787 memcpy(t, ct, sizeof(*ct));
788 if (target->compat_from_user)
789 target->compat_from_user(t->data, ct->data);
790 else
791 memcpy(t->data, ct->data, tsize - sizeof(*ct));
792 pad = XT_ALIGN(target->targetsize) - target->targetsize;
793 if (pad > 0)
794 memset(t->data + target->targetsize, 0, pad);
795
796 tsize += off;
797 t->u.user.target_size = tsize;
798
799 *size += off;
800 *dstptr += tsize;
801 }
802 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
803
804 int xt_compat_target_to_user(const struct xt_entry_target *t,
805 void __user **dstptr, unsigned int *size)
806 {
807 const struct xt_target *target = t->u.kernel.target;
808 struct compat_xt_entry_target __user *ct = *dstptr;
809 int off = xt_compat_target_offset(target);
810 u_int16_t tsize = t->u.user.target_size - off;
811
812 if (copy_to_user(ct, t, sizeof(*ct)) ||
813 put_user(tsize, &ct->u.user.target_size) ||
814 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
815 strlen(t->u.kernel.target->name) + 1))
816 return -EFAULT;
817
818 if (target->compat_to_user) {
819 if (target->compat_to_user((void __user *)ct->data, t->data))
820 return -EFAULT;
821 } else {
822 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
823 return -EFAULT;
824 }
825
826 *size -= off;
827 *dstptr += tsize;
828 return 0;
829 }
830 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
831 #endif
832
833 struct xt_table_info *xt_alloc_table_info(unsigned int size)
834 {
835 struct xt_table_info *newinfo;
836 int cpu;
837
838 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
839 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
840 return NULL;
841
842 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
843 if (!newinfo)
844 return NULL;
845
846 newinfo->size = size;
847
848 for_each_possible_cpu(cpu) {
849 if (size <= PAGE_SIZE)
850 newinfo->entries[cpu] = kmalloc_node(size,
851 GFP_KERNEL,
852 cpu_to_node(cpu));
853 else
854 newinfo->entries[cpu] = vmalloc_node(size,
855 cpu_to_node(cpu));
856
857 if (newinfo->entries[cpu] == NULL) {
858 xt_free_table_info(newinfo);
859 return NULL;
860 }
861 }
862
863 return newinfo;
864 }
865 EXPORT_SYMBOL(xt_alloc_table_info);
866
867 void xt_free_table_info(struct xt_table_info *info)
868 {
869 int cpu;
870
871 for_each_possible_cpu(cpu) {
872 if (info->size <= PAGE_SIZE)
873 kfree(info->entries[cpu]);
874 else
875 vfree(info->entries[cpu]);
876 }
877
878 if (info->jumpstack != NULL) {
879 if (sizeof(void *) * info->stacksize > PAGE_SIZE) {
880 for_each_possible_cpu(cpu)
881 vfree(info->jumpstack[cpu]);
882 } else {
883 for_each_possible_cpu(cpu)
884 kfree(info->jumpstack[cpu]);
885 }
886 }
887
888 if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE)
889 vfree(info->jumpstack);
890 else
891 kfree(info->jumpstack);
892
893 free_percpu(info->stackptr);
894
895 kfree(info);
896 }
897 EXPORT_SYMBOL(xt_free_table_info);
898
899 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
900 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
901 const char *name)
902 {
903 struct xt_table *t;
904
905 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
906 return ERR_PTR(-EINTR);
907
908 list_for_each_entry(t, &net->xt.tables[af], list)
909 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
910 return t;
911 mutex_unlock(&xt[af].mutex);
912 return NULL;
913 }
914 EXPORT_SYMBOL_GPL(xt_find_table_lock);
915
916 void xt_table_unlock(struct xt_table *table)
917 {
918 mutex_unlock(&xt[table->af].mutex);
919 }
920 EXPORT_SYMBOL_GPL(xt_table_unlock);
921
922 #ifdef CONFIG_COMPAT
923 void xt_compat_lock(u_int8_t af)
924 {
925 mutex_lock(&xt[af].compat_mutex);
926 }
927 EXPORT_SYMBOL_GPL(xt_compat_lock);
928
929 void xt_compat_unlock(u_int8_t af)
930 {
931 mutex_unlock(&xt[af].compat_mutex);
932 }
933 EXPORT_SYMBOL_GPL(xt_compat_unlock);
934 #endif
935
936 DEFINE_PER_CPU(seqcount_t, xt_recseq);
937 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
938
939 static int xt_jumpstack_alloc(struct xt_table_info *i)
940 {
941 unsigned int size;
942 int cpu;
943
944 i->stackptr = alloc_percpu(unsigned int);
945 if (i->stackptr == NULL)
946 return -ENOMEM;
947
948 size = sizeof(void **) * nr_cpu_ids;
949 if (size > PAGE_SIZE)
950 i->jumpstack = vzalloc(size);
951 else
952 i->jumpstack = kzalloc(size, GFP_KERNEL);
953 if (i->jumpstack == NULL)
954 return -ENOMEM;
955
956 i->stacksize *= xt_jumpstack_multiplier;
957 size = sizeof(void *) * i->stacksize;
958 for_each_possible_cpu(cpu) {
959 if (size > PAGE_SIZE)
960 i->jumpstack[cpu] = vmalloc_node(size,
961 cpu_to_node(cpu));
962 else
963 i->jumpstack[cpu] = kmalloc_node(size,
964 GFP_KERNEL, cpu_to_node(cpu));
965 if (i->jumpstack[cpu] == NULL)
966 /*
967 * Freeing will be done later on by the callers. The
968 * chain is: xt_replace_table -> __do_replace ->
969 * do_replace -> xt_free_table_info.
970 */
971 return -ENOMEM;
972 }
973
974 return 0;
975 }
976
977 struct xt_table_info *
978 xt_replace_table(struct xt_table *table,
979 unsigned int num_counters,
980 struct xt_table_info *newinfo,
981 int *error)
982 {
983 struct xt_table_info *private;
984 int ret;
985
986 ret = xt_jumpstack_alloc(newinfo);
987 if (ret < 0) {
988 *error = ret;
989 return NULL;
990 }
991
992 /* Do the substitution. */
993 local_bh_disable();
994 private = table->private;
995
996 /* Check inside lock: is the old number correct? */
997 if (num_counters != private->number) {
998 pr_debug("num_counters != table->private->number (%u/%u)\n",
999 num_counters, private->number);
1000 local_bh_enable();
1001 *error = -EAGAIN;
1002 return NULL;
1003 }
1004
1005 table->private = newinfo;
1006 newinfo->initial_entries = private->initial_entries;
1007
1008 /*
1009 * Even though table entries have now been swapped, other CPU's
1010 * may still be using the old entries. This is okay, because
1011 * resynchronization happens because of the locking done
1012 * during the get_counters() routine.
1013 */
1014 local_bh_enable();
1015
1016 #ifdef CONFIG_AUDIT
1017 if (audit_enabled) {
1018 struct audit_buffer *ab;
1019
1020 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1021 AUDIT_NETFILTER_CFG);
1022 if (ab) {
1023 audit_log_format(ab, "table=%s family=%u entries=%u",
1024 table->name, table->af,
1025 private->number);
1026 audit_log_end(ab);
1027 }
1028 }
1029 #endif
1030
1031 return private;
1032 }
1033 EXPORT_SYMBOL_GPL(xt_replace_table);
1034
1035 struct xt_table *xt_register_table(struct net *net,
1036 const struct xt_table *input_table,
1037 struct xt_table_info *bootstrap,
1038 struct xt_table_info *newinfo)
1039 {
1040 int ret;
1041 struct xt_table_info *private;
1042 struct xt_table *t, *table;
1043
1044 /* Don't add one object to multiple lists. */
1045 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1046 if (!table) {
1047 ret = -ENOMEM;
1048 goto out;
1049 }
1050
1051 ret = mutex_lock_interruptible(&xt[table->af].mutex);
1052 if (ret != 0)
1053 goto out_free;
1054
1055 /* Don't autoload: we'd eat our tail... */
1056 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1057 if (strcmp(t->name, table->name) == 0) {
1058 ret = -EEXIST;
1059 goto unlock;
1060 }
1061 }
1062
1063 /* Simplifies replace_table code. */
1064 table->private = bootstrap;
1065
1066 if (!xt_replace_table(table, 0, newinfo, &ret))
1067 goto unlock;
1068
1069 private = table->private;
1070 pr_debug("table->private->number = %u\n", private->number);
1071
1072 /* save number of initial entries */
1073 private->initial_entries = private->number;
1074
1075 list_add(&table->list, &net->xt.tables[table->af]);
1076 mutex_unlock(&xt[table->af].mutex);
1077 return table;
1078
1079 unlock:
1080 mutex_unlock(&xt[table->af].mutex);
1081 out_free:
1082 kfree(table);
1083 out:
1084 return ERR_PTR(ret);
1085 }
1086 EXPORT_SYMBOL_GPL(xt_register_table);
1087
1088 void *xt_unregister_table(struct xt_table *table)
1089 {
1090 struct xt_table_info *private;
1091
1092 mutex_lock(&xt[table->af].mutex);
1093 private = table->private;
1094 list_del(&table->list);
1095 mutex_unlock(&xt[table->af].mutex);
1096 kfree(table);
1097
1098 return private;
1099 }
1100 EXPORT_SYMBOL_GPL(xt_unregister_table);
1101
1102 #ifdef CONFIG_PROC_FS
1103 struct xt_names_priv {
1104 struct seq_net_private p;
1105 u_int8_t af;
1106 };
1107 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1108 {
1109 struct xt_names_priv *priv = seq->private;
1110 struct net *net = seq_file_net(seq);
1111 u_int8_t af = priv->af;
1112
1113 mutex_lock(&xt[af].mutex);
1114 return seq_list_start(&net->xt.tables[af], *pos);
1115 }
1116
1117 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1118 {
1119 struct xt_names_priv *priv = seq->private;
1120 struct net *net = seq_file_net(seq);
1121 u_int8_t af = priv->af;
1122
1123 return seq_list_next(v, &net->xt.tables[af], pos);
1124 }
1125
1126 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1127 {
1128 struct xt_names_priv *priv = seq->private;
1129 u_int8_t af = priv->af;
1130
1131 mutex_unlock(&xt[af].mutex);
1132 }
1133
1134 static int xt_table_seq_show(struct seq_file *seq, void *v)
1135 {
1136 struct xt_table *table = list_entry(v, struct xt_table, list);
1137
1138 if (strlen(table->name))
1139 return seq_printf(seq, "%s\n", table->name);
1140 else
1141 return 0;
1142 }
1143
1144 static const struct seq_operations xt_table_seq_ops = {
1145 .start = xt_table_seq_start,
1146 .next = xt_table_seq_next,
1147 .stop = xt_table_seq_stop,
1148 .show = xt_table_seq_show,
1149 };
1150
1151 static int xt_table_open(struct inode *inode, struct file *file)
1152 {
1153 int ret;
1154 struct xt_names_priv *priv;
1155
1156 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1157 sizeof(struct xt_names_priv));
1158 if (!ret) {
1159 priv = ((struct seq_file *)file->private_data)->private;
1160 priv->af = (unsigned long)PDE_DATA(inode);
1161 }
1162 return ret;
1163 }
1164
1165 static const struct file_operations xt_table_ops = {
1166 .owner = THIS_MODULE,
1167 .open = xt_table_open,
1168 .read = seq_read,
1169 .llseek = seq_lseek,
1170 .release = seq_release_net,
1171 };
1172
1173 /*
1174 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1175 * the multi-AF mutexes.
1176 */
1177 struct nf_mttg_trav {
1178 struct list_head *head, *curr;
1179 uint8_t class, nfproto;
1180 };
1181
1182 enum {
1183 MTTG_TRAV_INIT,
1184 MTTG_TRAV_NFP_UNSPEC,
1185 MTTG_TRAV_NFP_SPEC,
1186 MTTG_TRAV_DONE,
1187 };
1188
1189 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1190 bool is_target)
1191 {
1192 static const uint8_t next_class[] = {
1193 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1194 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1195 };
1196 struct nf_mttg_trav *trav = seq->private;
1197
1198 switch (trav->class) {
1199 case MTTG_TRAV_INIT:
1200 trav->class = MTTG_TRAV_NFP_UNSPEC;
1201 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1202 trav->head = trav->curr = is_target ?
1203 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1204 break;
1205 case MTTG_TRAV_NFP_UNSPEC:
1206 trav->curr = trav->curr->next;
1207 if (trav->curr != trav->head)
1208 break;
1209 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1210 mutex_lock(&xt[trav->nfproto].mutex);
1211 trav->head = trav->curr = is_target ?
1212 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1213 trav->class = next_class[trav->class];
1214 break;
1215 case MTTG_TRAV_NFP_SPEC:
1216 trav->curr = trav->curr->next;
1217 if (trav->curr != trav->head)
1218 break;
1219 /* fallthru, _stop will unlock */
1220 default:
1221 return NULL;
1222 }
1223
1224 if (ppos != NULL)
1225 ++*ppos;
1226 return trav;
1227 }
1228
1229 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1230 bool is_target)
1231 {
1232 struct nf_mttg_trav *trav = seq->private;
1233 unsigned int j;
1234
1235 trav->class = MTTG_TRAV_INIT;
1236 for (j = 0; j < *pos; ++j)
1237 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1238 return NULL;
1239 return trav;
1240 }
1241
1242 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1243 {
1244 struct nf_mttg_trav *trav = seq->private;
1245
1246 switch (trav->class) {
1247 case MTTG_TRAV_NFP_UNSPEC:
1248 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1249 break;
1250 case MTTG_TRAV_NFP_SPEC:
1251 mutex_unlock(&xt[trav->nfproto].mutex);
1252 break;
1253 }
1254 }
1255
1256 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1257 {
1258 return xt_mttg_seq_start(seq, pos, false);
1259 }
1260
1261 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1262 {
1263 return xt_mttg_seq_next(seq, v, ppos, false);
1264 }
1265
1266 static int xt_match_seq_show(struct seq_file *seq, void *v)
1267 {
1268 const struct nf_mttg_trav *trav = seq->private;
1269 const struct xt_match *match;
1270
1271 switch (trav->class) {
1272 case MTTG_TRAV_NFP_UNSPEC:
1273 case MTTG_TRAV_NFP_SPEC:
1274 if (trav->curr == trav->head)
1275 return 0;
1276 match = list_entry(trav->curr, struct xt_match, list);
1277 return (*match->name == '\0') ? 0 :
1278 seq_printf(seq, "%s\n", match->name);
1279 }
1280 return 0;
1281 }
1282
1283 static const struct seq_operations xt_match_seq_ops = {
1284 .start = xt_match_seq_start,
1285 .next = xt_match_seq_next,
1286 .stop = xt_mttg_seq_stop,
1287 .show = xt_match_seq_show,
1288 };
1289
1290 static int xt_match_open(struct inode *inode, struct file *file)
1291 {
1292 struct seq_file *seq;
1293 struct nf_mttg_trav *trav;
1294 int ret;
1295
1296 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1297 if (trav == NULL)
1298 return -ENOMEM;
1299
1300 ret = seq_open(file, &xt_match_seq_ops);
1301 if (ret < 0) {
1302 kfree(trav);
1303 return ret;
1304 }
1305
1306 seq = file->private_data;
1307 seq->private = trav;
1308 trav->nfproto = (unsigned long)PDE_DATA(inode);
1309 return 0;
1310 }
1311
1312 static const struct file_operations xt_match_ops = {
1313 .owner = THIS_MODULE,
1314 .open = xt_match_open,
1315 .read = seq_read,
1316 .llseek = seq_lseek,
1317 .release = seq_release_private,
1318 };
1319
1320 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1321 {
1322 return xt_mttg_seq_start(seq, pos, true);
1323 }
1324
1325 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1326 {
1327 return xt_mttg_seq_next(seq, v, ppos, true);
1328 }
1329
1330 static int xt_target_seq_show(struct seq_file *seq, void *v)
1331 {
1332 const struct nf_mttg_trav *trav = seq->private;
1333 const struct xt_target *target;
1334
1335 switch (trav->class) {
1336 case MTTG_TRAV_NFP_UNSPEC:
1337 case MTTG_TRAV_NFP_SPEC:
1338 if (trav->curr == trav->head)
1339 return 0;
1340 target = list_entry(trav->curr, struct xt_target, list);
1341 return (*target->name == '\0') ? 0 :
1342 seq_printf(seq, "%s\n", target->name);
1343 }
1344 return 0;
1345 }
1346
1347 static const struct seq_operations xt_target_seq_ops = {
1348 .start = xt_target_seq_start,
1349 .next = xt_target_seq_next,
1350 .stop = xt_mttg_seq_stop,
1351 .show = xt_target_seq_show,
1352 };
1353
1354 static int xt_target_open(struct inode *inode, struct file *file)
1355 {
1356 struct seq_file *seq;
1357 struct nf_mttg_trav *trav;
1358 int ret;
1359
1360 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1361 if (trav == NULL)
1362 return -ENOMEM;
1363
1364 ret = seq_open(file, &xt_target_seq_ops);
1365 if (ret < 0) {
1366 kfree(trav);
1367 return ret;
1368 }
1369
1370 seq = file->private_data;
1371 seq->private = trav;
1372 trav->nfproto = (unsigned long)PDE_DATA(inode);
1373 return 0;
1374 }
1375
1376 static const struct file_operations xt_target_ops = {
1377 .owner = THIS_MODULE,
1378 .open = xt_target_open,
1379 .read = seq_read,
1380 .llseek = seq_lseek,
1381 .release = seq_release_private,
1382 };
1383
1384 #define FORMAT_TABLES "_tables_names"
1385 #define FORMAT_MATCHES "_tables_matches"
1386 #define FORMAT_TARGETS "_tables_targets"
1387
1388 #endif /* CONFIG_PROC_FS */
1389
1390 /**
1391 * xt_hook_link - set up hooks for a new table
1392 * @table: table with metadata needed to set up hooks
1393 * @fn: Hook function
1394 *
1395 * This function will take care of creating and registering the necessary
1396 * Netfilter hooks for XT tables.
1397 */
1398 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1399 {
1400 unsigned int hook_mask = table->valid_hooks;
1401 uint8_t i, num_hooks = hweight32(hook_mask);
1402 uint8_t hooknum;
1403 struct nf_hook_ops *ops;
1404 int ret;
1405
1406 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1407 if (ops == NULL)
1408 return ERR_PTR(-ENOMEM);
1409
1410 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1411 hook_mask >>= 1, ++hooknum) {
1412 if (!(hook_mask & 1))
1413 continue;
1414 ops[i].hook = fn;
1415 ops[i].owner = table->me;
1416 ops[i].pf = table->af;
1417 ops[i].hooknum = hooknum;
1418 ops[i].priority = table->priority;
1419 ++i;
1420 }
1421
1422 ret = nf_register_hooks(ops, num_hooks);
1423 if (ret < 0) {
1424 kfree(ops);
1425 return ERR_PTR(ret);
1426 }
1427
1428 return ops;
1429 }
1430 EXPORT_SYMBOL_GPL(xt_hook_link);
1431
1432 /**
1433 * xt_hook_unlink - remove hooks for a table
1434 * @ops: nf_hook_ops array as returned by nf_hook_link
1435 * @hook_mask: the very same mask that was passed to nf_hook_link
1436 */
1437 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1438 {
1439 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1440 kfree(ops);
1441 }
1442 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1443
1444 int xt_proto_init(struct net *net, u_int8_t af)
1445 {
1446 #ifdef CONFIG_PROC_FS
1447 char buf[XT_FUNCTION_MAXNAMELEN];
1448 struct proc_dir_entry *proc;
1449 #endif
1450
1451 if (af >= ARRAY_SIZE(xt_prefix))
1452 return -EINVAL;
1453
1454
1455 #ifdef CONFIG_PROC_FS
1456 strlcpy(buf, xt_prefix[af], sizeof(buf));
1457 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1458 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1459 (void *)(unsigned long)af);
1460 if (!proc)
1461 goto out;
1462
1463 strlcpy(buf, xt_prefix[af], sizeof(buf));
1464 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1465 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1466 (void *)(unsigned long)af);
1467 if (!proc)
1468 goto out_remove_tables;
1469
1470 strlcpy(buf, xt_prefix[af], sizeof(buf));
1471 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1472 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1473 (void *)(unsigned long)af);
1474 if (!proc)
1475 goto out_remove_matches;
1476 #endif
1477
1478 return 0;
1479
1480 #ifdef CONFIG_PROC_FS
1481 out_remove_matches:
1482 strlcpy(buf, xt_prefix[af], sizeof(buf));
1483 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1484 remove_proc_entry(buf, net->proc_net);
1485
1486 out_remove_tables:
1487 strlcpy(buf, xt_prefix[af], sizeof(buf));
1488 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1489 remove_proc_entry(buf, net->proc_net);
1490 out:
1491 return -1;
1492 #endif
1493 }
1494 EXPORT_SYMBOL_GPL(xt_proto_init);
1495
1496 void xt_proto_fini(struct net *net, u_int8_t af)
1497 {
1498 #ifdef CONFIG_PROC_FS
1499 char buf[XT_FUNCTION_MAXNAMELEN];
1500
1501 strlcpy(buf, xt_prefix[af], sizeof(buf));
1502 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1503 remove_proc_entry(buf, net->proc_net);
1504
1505 strlcpy(buf, xt_prefix[af], sizeof(buf));
1506 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1507 remove_proc_entry(buf, net->proc_net);
1508
1509 strlcpy(buf, xt_prefix[af], sizeof(buf));
1510 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1511 remove_proc_entry(buf, net->proc_net);
1512 #endif /*CONFIG_PROC_FS*/
1513 }
1514 EXPORT_SYMBOL_GPL(xt_proto_fini);
1515
1516 static int __net_init xt_net_init(struct net *net)
1517 {
1518 int i;
1519
1520 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1521 INIT_LIST_HEAD(&net->xt.tables[i]);
1522 return 0;
1523 }
1524
1525 static struct pernet_operations xt_net_ops = {
1526 .init = xt_net_init,
1527 };
1528
1529 static int __init xt_init(void)
1530 {
1531 unsigned int i;
1532 int rv;
1533
1534 for_each_possible_cpu(i) {
1535 seqcount_init(&per_cpu(xt_recseq, i));
1536 }
1537
1538 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1539 if (!xt)
1540 return -ENOMEM;
1541
1542 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1543 mutex_init(&xt[i].mutex);
1544 #ifdef CONFIG_COMPAT
1545 mutex_init(&xt[i].compat_mutex);
1546 xt[i].compat_tab = NULL;
1547 #endif
1548 INIT_LIST_HEAD(&xt[i].target);
1549 INIT_LIST_HEAD(&xt[i].match);
1550 }
1551 rv = register_pernet_subsys(&xt_net_ops);
1552 if (rv < 0)
1553 kfree(xt);
1554 return rv;
1555 }
1556
1557 static void __exit xt_fini(void)
1558 {
1559 unregister_pernet_subsys(&xt_net_ops);
1560 kfree(xt);
1561 }
1562
1563 module_init(xt_init);
1564 module_exit(xt_fini);
1565