powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / net / netfilter / x_tables.c
blob2ff499680cc60b6ea7e22a8d02e2b82e32005a23
1 /*
2 * x_tables core - Backend for {ip,ip6,arp}_tables
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
5 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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>
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.
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 <linux/user_namespace.h>
30 #include <net/net_namespace.h>
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter_arp.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #include <linux/netfilter_arp/arp_tables.h>
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
42 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
43 #define XT_PCPU_BLOCK_SIZE 4096
45 struct compat_delta {
46 unsigned int offset; /* offset in kernel */
47 int delta; /* delta in 32bit user land */
50 struct xt_af {
51 struct mutex mutex;
52 struct list_head match;
53 struct list_head target;
54 #ifdef CONFIG_COMPAT
55 struct mutex compat_mutex;
56 struct compat_delta *compat_tab;
57 unsigned int number; /* number of slots in compat_tab[] */
58 unsigned int cur; /* number of used slots in compat_tab[] */
59 #endif
62 static struct xt_af *xt;
64 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
65 [NFPROTO_UNSPEC] = "x",
66 [NFPROTO_IPV4] = "ip",
67 [NFPROTO_ARP] = "arp",
68 [NFPROTO_BRIDGE] = "eb",
69 [NFPROTO_IPV6] = "ip6",
72 /* Registration hooks for targets. */
73 int xt_register_target(struct xt_target *target)
75 u_int8_t af = target->family;
77 mutex_lock(&xt[af].mutex);
78 list_add(&target->list, &xt[af].target);
79 mutex_unlock(&xt[af].mutex);
80 return 0;
82 EXPORT_SYMBOL(xt_register_target);
84 void
85 xt_unregister_target(struct xt_target *target)
87 u_int8_t af = target->family;
89 mutex_lock(&xt[af].mutex);
90 list_del(&target->list);
91 mutex_unlock(&xt[af].mutex);
93 EXPORT_SYMBOL(xt_unregister_target);
95 int
96 xt_register_targets(struct xt_target *target, unsigned int n)
98 unsigned int i;
99 int err = 0;
101 for (i = 0; i < n; i++) {
102 err = xt_register_target(&target[i]);
103 if (err)
104 goto err;
106 return err;
108 err:
109 if (i > 0)
110 xt_unregister_targets(target, i);
111 return err;
113 EXPORT_SYMBOL(xt_register_targets);
115 void
116 xt_unregister_targets(struct xt_target *target, unsigned int n)
118 while (n-- > 0)
119 xt_unregister_target(&target[n]);
121 EXPORT_SYMBOL(xt_unregister_targets);
123 int xt_register_match(struct xt_match *match)
125 u_int8_t af = match->family;
127 mutex_lock(&xt[af].mutex);
128 list_add(&match->list, &xt[af].match);
129 mutex_unlock(&xt[af].mutex);
130 return 0;
132 EXPORT_SYMBOL(xt_register_match);
134 void
135 xt_unregister_match(struct xt_match *match)
137 u_int8_t af = match->family;
139 mutex_lock(&xt[af].mutex);
140 list_del(&match->list);
141 mutex_unlock(&xt[af].mutex);
143 EXPORT_SYMBOL(xt_unregister_match);
146 xt_register_matches(struct xt_match *match, unsigned int n)
148 unsigned int i;
149 int err = 0;
151 for (i = 0; i < n; i++) {
152 err = xt_register_match(&match[i]);
153 if (err)
154 goto err;
156 return err;
158 err:
159 if (i > 0)
160 xt_unregister_matches(match, i);
161 return err;
163 EXPORT_SYMBOL(xt_register_matches);
165 void
166 xt_unregister_matches(struct xt_match *match, unsigned int n)
168 while (n-- > 0)
169 xt_unregister_match(&match[n]);
171 EXPORT_SYMBOL(xt_unregister_matches);
175 * These are weird, but module loading must not be done with mutex
176 * held (since they will register), and we have to have a single
177 * function to use.
180 /* Find match, grabs ref. Returns ERR_PTR() on error. */
181 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
183 struct xt_match *m;
184 int err = -ENOENT;
186 mutex_lock(&xt[af].mutex);
187 list_for_each_entry(m, &xt[af].match, list) {
188 if (strcmp(m->name, name) == 0) {
189 if (m->revision == revision) {
190 if (try_module_get(m->me)) {
191 mutex_unlock(&xt[af].mutex);
192 return m;
194 } else
195 err = -EPROTOTYPE; /* Found something. */
198 mutex_unlock(&xt[af].mutex);
200 if (af != NFPROTO_UNSPEC)
201 /* Try searching again in the family-independent list */
202 return xt_find_match(NFPROTO_UNSPEC, name, revision);
204 return ERR_PTR(err);
206 EXPORT_SYMBOL(xt_find_match);
208 struct xt_match *
209 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
211 struct xt_match *match;
213 match = xt_find_match(nfproto, name, revision);
214 if (IS_ERR(match)) {
215 request_module("%st_%s", xt_prefix[nfproto], name);
216 match = xt_find_match(nfproto, name, revision);
219 return match;
221 EXPORT_SYMBOL_GPL(xt_request_find_match);
223 /* Find target, grabs ref. Returns ERR_PTR() on error. */
224 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
226 struct xt_target *t;
227 int err = -ENOENT;
229 mutex_lock(&xt[af].mutex);
230 list_for_each_entry(t, &xt[af].target, list) {
231 if (strcmp(t->name, name) == 0) {
232 if (t->revision == revision) {
233 if (try_module_get(t->me)) {
234 mutex_unlock(&xt[af].mutex);
235 return t;
237 } else
238 err = -EPROTOTYPE; /* Found something. */
241 mutex_unlock(&xt[af].mutex);
243 if (af != NFPROTO_UNSPEC)
244 /* Try searching again in the family-independent list */
245 return xt_find_target(NFPROTO_UNSPEC, name, revision);
247 return ERR_PTR(err);
249 EXPORT_SYMBOL(xt_find_target);
251 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
253 struct xt_target *target;
255 target = xt_find_target(af, name, revision);
256 if (IS_ERR(target)) {
257 request_module("%st_%s", xt_prefix[af], name);
258 target = xt_find_target(af, name, revision);
261 return target;
263 EXPORT_SYMBOL_GPL(xt_request_find_target);
265 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
267 const struct xt_match *m;
268 int have_rev = 0;
270 list_for_each_entry(m, &xt[af].match, list) {
271 if (strcmp(m->name, name) == 0) {
272 if (m->revision > *bestp)
273 *bestp = m->revision;
274 if (m->revision == revision)
275 have_rev = 1;
279 if (af != NFPROTO_UNSPEC && !have_rev)
280 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
282 return have_rev;
285 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
287 const struct xt_target *t;
288 int have_rev = 0;
290 list_for_each_entry(t, &xt[af].target, list) {
291 if (strcmp(t->name, name) == 0) {
292 if (t->revision > *bestp)
293 *bestp = t->revision;
294 if (t->revision == revision)
295 have_rev = 1;
299 if (af != NFPROTO_UNSPEC && !have_rev)
300 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
302 return have_rev;
305 /* Returns true or false (if no such extension at all) */
306 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
307 int *err)
309 int have_rev, best = -1;
311 mutex_lock(&xt[af].mutex);
312 if (target == 1)
313 have_rev = target_revfn(af, name, revision, &best);
314 else
315 have_rev = match_revfn(af, name, revision, &best);
316 mutex_unlock(&xt[af].mutex);
318 /* Nothing at all? Return 0 to try loading module. */
319 if (best == -1) {
320 *err = -ENOENT;
321 return 0;
324 *err = best;
325 if (!have_rev)
326 *err = -EPROTONOSUPPORT;
327 return 1;
329 EXPORT_SYMBOL_GPL(xt_find_revision);
331 static char *
332 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
334 static const char *const inetbr_names[] = {
335 "PREROUTING", "INPUT", "FORWARD",
336 "OUTPUT", "POSTROUTING", "BROUTING",
338 static const char *const arp_names[] = {
339 "INPUT", "FORWARD", "OUTPUT",
341 const char *const *names;
342 unsigned int i, max;
343 char *p = buf;
344 bool np = false;
345 int res;
347 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
348 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
349 ARRAY_SIZE(inetbr_names);
350 *p = '\0';
351 for (i = 0; i < max; ++i) {
352 if (!(mask & (1 << i)))
353 continue;
354 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
355 if (res > 0) {
356 size -= res;
357 p += res;
359 np = true;
362 return buf;
365 int xt_check_match(struct xt_mtchk_param *par,
366 unsigned int size, u_int8_t proto, bool inv_proto)
368 int ret;
370 if (XT_ALIGN(par->match->matchsize) != size &&
371 par->match->matchsize != -1) {
373 * ebt_among is exempt from centralized matchsize checking
374 * because it uses a dynamic-size data set.
376 pr_err("%s_tables: %s.%u match: invalid size "
377 "%u (kernel) != (user) %u\n",
378 xt_prefix[par->family], par->match->name,
379 par->match->revision,
380 XT_ALIGN(par->match->matchsize), size);
381 return -EINVAL;
383 if (par->match->table != NULL &&
384 strcmp(par->match->table, par->table) != 0) {
385 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
386 xt_prefix[par->family], par->match->name,
387 par->match->table, par->table);
388 return -EINVAL;
390 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
391 char used[64], allow[64];
393 pr_err("%s_tables: %s match: used from hooks %s, but only "
394 "valid from %s\n",
395 xt_prefix[par->family], par->match->name,
396 textify_hooks(used, sizeof(used), par->hook_mask,
397 par->family),
398 textify_hooks(allow, sizeof(allow), par->match->hooks,
399 par->family));
400 return -EINVAL;
402 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
403 pr_err("%s_tables: %s match: only valid for protocol %u\n",
404 xt_prefix[par->family], par->match->name,
405 par->match->proto);
406 return -EINVAL;
408 if (par->match->checkentry != NULL) {
409 ret = par->match->checkentry(par);
410 if (ret < 0)
411 return ret;
412 else if (ret > 0)
413 /* Flag up potential errors. */
414 return -EIO;
416 return 0;
418 EXPORT_SYMBOL_GPL(xt_check_match);
420 /** xt_check_entry_match - check that matches end before start of target
422 * @match: beginning of xt_entry_match
423 * @target: beginning of this rules target (alleged end of matches)
424 * @alignment: alignment requirement of match structures
426 * Validates that all matches add up to the beginning of the target,
427 * and that each match covers at least the base structure size.
429 * Return: 0 on success, negative errno on failure.
431 static int xt_check_entry_match(const char *match, const char *target,
432 const size_t alignment)
434 const struct xt_entry_match *pos;
435 int length = target - match;
437 if (length == 0) /* no matches */
438 return 0;
440 pos = (struct xt_entry_match *)match;
441 do {
442 if ((unsigned long)pos % alignment)
443 return -EINVAL;
445 if (length < (int)sizeof(struct xt_entry_match))
446 return -EINVAL;
448 if (pos->u.match_size < sizeof(struct xt_entry_match))
449 return -EINVAL;
451 if (pos->u.match_size > length)
452 return -EINVAL;
454 length -= pos->u.match_size;
455 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
456 } while (length > 0);
458 return 0;
461 #ifdef CONFIG_COMPAT
462 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
464 struct xt_af *xp = &xt[af];
466 if (!xp->compat_tab) {
467 if (!xp->number)
468 return -EINVAL;
469 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
470 if (!xp->compat_tab)
471 return -ENOMEM;
472 xp->cur = 0;
475 if (xp->cur >= xp->number)
476 return -EINVAL;
478 if (xp->cur)
479 delta += xp->compat_tab[xp->cur - 1].delta;
480 xp->compat_tab[xp->cur].offset = offset;
481 xp->compat_tab[xp->cur].delta = delta;
482 xp->cur++;
483 return 0;
485 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
487 void xt_compat_flush_offsets(u_int8_t af)
489 if (xt[af].compat_tab) {
490 vfree(xt[af].compat_tab);
491 xt[af].compat_tab = NULL;
492 xt[af].number = 0;
493 xt[af].cur = 0;
496 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
498 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
500 struct compat_delta *tmp = xt[af].compat_tab;
501 int mid, left = 0, right = xt[af].cur - 1;
503 while (left <= right) {
504 mid = (left + right) >> 1;
505 if (offset > tmp[mid].offset)
506 left = mid + 1;
507 else if (offset < tmp[mid].offset)
508 right = mid - 1;
509 else
510 return mid ? tmp[mid - 1].delta : 0;
512 return left ? tmp[left - 1].delta : 0;
514 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
516 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
518 xt[af].number = number;
519 xt[af].cur = 0;
521 EXPORT_SYMBOL(xt_compat_init_offsets);
523 int xt_compat_match_offset(const struct xt_match *match)
525 u_int16_t csize = match->compatsize ? : match->matchsize;
526 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
528 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
530 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
531 unsigned int *size)
533 const struct xt_match *match = m->u.kernel.match;
534 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
535 int pad, off = xt_compat_match_offset(match);
536 u_int16_t msize = cm->u.user.match_size;
537 char name[sizeof(m->u.user.name)];
539 m = *dstptr;
540 memcpy(m, cm, sizeof(*cm));
541 if (match->compat_from_user)
542 match->compat_from_user(m->data, cm->data);
543 else
544 memcpy(m->data, cm->data, msize - sizeof(*cm));
545 pad = XT_ALIGN(match->matchsize) - match->matchsize;
546 if (pad > 0)
547 memset(m->data + match->matchsize, 0, pad);
549 msize += off;
550 m->u.user.match_size = msize;
551 strlcpy(name, match->name, sizeof(name));
552 module_put(match->me);
553 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
555 *size += off;
556 *dstptr += msize;
558 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
560 int xt_compat_match_to_user(const struct xt_entry_match *m,
561 void __user **dstptr, unsigned int *size)
563 const struct xt_match *match = m->u.kernel.match;
564 struct compat_xt_entry_match __user *cm = *dstptr;
565 int off = xt_compat_match_offset(match);
566 u_int16_t msize = m->u.user.match_size - off;
568 if (copy_to_user(cm, m, sizeof(*cm)) ||
569 put_user(msize, &cm->u.user.match_size) ||
570 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
571 strlen(m->u.kernel.match->name) + 1))
572 return -EFAULT;
574 if (match->compat_to_user) {
575 if (match->compat_to_user((void __user *)cm->data, m->data))
576 return -EFAULT;
577 } else {
578 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
579 return -EFAULT;
582 *size -= off;
583 *dstptr += msize;
584 return 0;
586 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
588 /* non-compat version may have padding after verdict */
589 struct compat_xt_standard_target {
590 struct compat_xt_entry_target t;
591 compat_uint_t verdict;
594 int xt_compat_check_entry_offsets(const void *base, const char *elems,
595 unsigned int target_offset,
596 unsigned int next_offset)
598 long size_of_base_struct = elems - (const char *)base;
599 const struct compat_xt_entry_target *t;
600 const char *e = base;
602 if (target_offset < size_of_base_struct)
603 return -EINVAL;
605 if (target_offset + sizeof(*t) > next_offset)
606 return -EINVAL;
608 t = (void *)(e + target_offset);
609 if (t->u.target_size < sizeof(*t))
610 return -EINVAL;
612 if (target_offset + t->u.target_size > next_offset)
613 return -EINVAL;
615 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
616 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
617 return -EINVAL;
619 /* compat_xt_entry match has less strict aligment requirements,
620 * otherwise they are identical. In case of padding differences
621 * we need to add compat version of xt_check_entry_match.
623 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
625 return xt_check_entry_match(elems, base + target_offset,
626 __alignof__(struct compat_xt_entry_match));
628 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
629 #endif /* CONFIG_COMPAT */
632 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
634 * @base: pointer to arp/ip/ip6t_entry
635 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
636 * @target_offset: the arp/ip/ip6_t->target_offset
637 * @next_offset: the arp/ip/ip6_t->next_offset
639 * validates that target_offset and next_offset are sane and that all
640 * match sizes (if any) align with the target offset.
642 * This function does not validate the targets or matches themselves, it
643 * only tests that all the offsets and sizes are correct, that all
644 * match structures are aligned, and that the last structure ends where
645 * the target structure begins.
647 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
649 * The arp/ip/ip6t_entry structure @base must have passed following tests:
650 * - it must point to a valid memory location
651 * - base to base + next_offset must be accessible, i.e. not exceed allocated
652 * length.
654 * A well-formed entry looks like this:
656 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
657 * e->elems[]-----' | |
658 * matchsize | |
659 * matchsize | |
660 * | |
661 * target_offset---------------------------------' |
662 * next_offset---------------------------------------------------'
664 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
665 * This is where matches (if any) and the target reside.
666 * target_offset: beginning of target.
667 * next_offset: start of the next rule; also: size of this rule.
668 * Since targets have a minimum size, target_offset + minlen <= next_offset.
670 * Every match stores its size, sum of sizes must not exceed target_offset.
672 * Return: 0 on success, negative errno on failure.
674 int xt_check_entry_offsets(const void *base,
675 const char *elems,
676 unsigned int target_offset,
677 unsigned int next_offset)
679 long size_of_base_struct = elems - (const char *)base;
680 const struct xt_entry_target *t;
681 const char *e = base;
683 /* target start is within the ip/ip6/arpt_entry struct */
684 if (target_offset < size_of_base_struct)
685 return -EINVAL;
687 if (target_offset + sizeof(*t) > next_offset)
688 return -EINVAL;
690 t = (void *)(e + target_offset);
691 if (t->u.target_size < sizeof(*t))
692 return -EINVAL;
694 if (target_offset + t->u.target_size > next_offset)
695 return -EINVAL;
697 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
698 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
699 return -EINVAL;
701 return xt_check_entry_match(elems, base + target_offset,
702 __alignof__(struct xt_entry_match));
704 EXPORT_SYMBOL(xt_check_entry_offsets);
707 * xt_alloc_entry_offsets - allocate array to store rule head offsets
709 * @size: number of entries
711 * Return: NULL or kmalloc'd or vmalloc'd array
713 unsigned int *xt_alloc_entry_offsets(unsigned int size)
715 unsigned int *off;
717 off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
719 if (off)
720 return off;
722 if (size < (SIZE_MAX / sizeof(unsigned int)))
723 off = vmalloc(size * sizeof(unsigned int));
725 return off;
727 EXPORT_SYMBOL(xt_alloc_entry_offsets);
730 * xt_find_jump_offset - check if target is a valid jump offset
732 * @offsets: array containing all valid rule start offsets of a rule blob
733 * @target: the jump target to search for
734 * @size: entries in @offset
736 bool xt_find_jump_offset(const unsigned int *offsets,
737 unsigned int target, unsigned int size)
739 int m, low = 0, hi = size;
741 while (hi > low) {
742 m = (low + hi) / 2u;
744 if (offsets[m] > target)
745 hi = m;
746 else if (offsets[m] < target)
747 low = m + 1;
748 else
749 return true;
752 return false;
754 EXPORT_SYMBOL(xt_find_jump_offset);
756 int xt_check_target(struct xt_tgchk_param *par,
757 unsigned int size, u_int8_t proto, bool inv_proto)
759 int ret;
761 if (XT_ALIGN(par->target->targetsize) != size) {
762 pr_err("%s_tables: %s.%u target: invalid size "
763 "%u (kernel) != (user) %u\n",
764 xt_prefix[par->family], par->target->name,
765 par->target->revision,
766 XT_ALIGN(par->target->targetsize), size);
767 return -EINVAL;
769 if (par->target->table != NULL &&
770 strcmp(par->target->table, par->table) != 0) {
771 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
772 xt_prefix[par->family], par->target->name,
773 par->target->table, par->table);
774 return -EINVAL;
776 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
777 char used[64], allow[64];
779 pr_err("%s_tables: %s target: used from hooks %s, but only "
780 "usable from %s\n",
781 xt_prefix[par->family], par->target->name,
782 textify_hooks(used, sizeof(used), par->hook_mask,
783 par->family),
784 textify_hooks(allow, sizeof(allow), par->target->hooks,
785 par->family));
786 return -EINVAL;
788 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
789 pr_err("%s_tables: %s target: only valid for protocol %u\n",
790 xt_prefix[par->family], par->target->name,
791 par->target->proto);
792 return -EINVAL;
794 if (par->target->checkentry != NULL) {
795 ret = par->target->checkentry(par);
796 if (ret < 0)
797 return ret;
798 else if (ret > 0)
799 /* Flag up potential errors. */
800 return -EIO;
802 return 0;
804 EXPORT_SYMBOL_GPL(xt_check_target);
807 * xt_copy_counters_from_user - copy counters and metadata from userspace
809 * @user: src pointer to userspace memory
810 * @len: alleged size of userspace memory
811 * @info: where to store the xt_counters_info metadata
812 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
814 * Copies counter meta data from @user and stores it in @info.
816 * vmallocs memory to hold the counters, then copies the counter data
817 * from @user to the new memory and returns a pointer to it.
819 * If @compat is true, @info gets converted automatically to the 64bit
820 * representation.
822 * The metadata associated with the counters is stored in @info.
824 * Return: returns pointer that caller has to test via IS_ERR().
825 * If IS_ERR is false, caller has to vfree the pointer.
827 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
828 struct xt_counters_info *info, bool compat)
830 void *mem;
831 u64 size;
833 #ifdef CONFIG_COMPAT
834 if (compat) {
835 /* structures only differ in size due to alignment */
836 struct compat_xt_counters_info compat_tmp;
838 if (len <= sizeof(compat_tmp))
839 return ERR_PTR(-EINVAL);
841 len -= sizeof(compat_tmp);
842 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
843 return ERR_PTR(-EFAULT);
845 strlcpy(info->name, compat_tmp.name, sizeof(info->name));
846 info->num_counters = compat_tmp.num_counters;
847 user += sizeof(compat_tmp);
848 } else
849 #endif
851 if (len <= sizeof(*info))
852 return ERR_PTR(-EINVAL);
854 len -= sizeof(*info);
855 if (copy_from_user(info, user, sizeof(*info)) != 0)
856 return ERR_PTR(-EFAULT);
858 info->name[sizeof(info->name) - 1] = '\0';
859 user += sizeof(*info);
862 size = sizeof(struct xt_counters);
863 size *= info->num_counters;
865 if (size != (u64)len)
866 return ERR_PTR(-EINVAL);
868 mem = vmalloc(len);
869 if (!mem)
870 return ERR_PTR(-ENOMEM);
872 if (copy_from_user(mem, user, len) == 0)
873 return mem;
875 vfree(mem);
876 return ERR_PTR(-EFAULT);
878 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
880 #ifdef CONFIG_COMPAT
881 int xt_compat_target_offset(const struct xt_target *target)
883 u_int16_t csize = target->compatsize ? : target->targetsize;
884 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
886 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
888 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
889 unsigned int *size)
891 const struct xt_target *target = t->u.kernel.target;
892 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
893 int pad, off = xt_compat_target_offset(target);
894 u_int16_t tsize = ct->u.user.target_size;
895 char name[sizeof(t->u.user.name)];
897 t = *dstptr;
898 memcpy(t, ct, sizeof(*ct));
899 if (target->compat_from_user)
900 target->compat_from_user(t->data, ct->data);
901 else
902 memcpy(t->data, ct->data, tsize - sizeof(*ct));
903 pad = XT_ALIGN(target->targetsize) - target->targetsize;
904 if (pad > 0)
905 memset(t->data + target->targetsize, 0, pad);
907 tsize += off;
908 t->u.user.target_size = tsize;
909 strlcpy(name, target->name, sizeof(name));
910 module_put(target->me);
911 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
913 *size += off;
914 *dstptr += tsize;
916 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
918 int xt_compat_target_to_user(const struct xt_entry_target *t,
919 void __user **dstptr, unsigned int *size)
921 const struct xt_target *target = t->u.kernel.target;
922 struct compat_xt_entry_target __user *ct = *dstptr;
923 int off = xt_compat_target_offset(target);
924 u_int16_t tsize = t->u.user.target_size - off;
926 if (copy_to_user(ct, t, sizeof(*ct)) ||
927 put_user(tsize, &ct->u.user.target_size) ||
928 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
929 strlen(t->u.kernel.target->name) + 1))
930 return -EFAULT;
932 if (target->compat_to_user) {
933 if (target->compat_to_user((void __user *)ct->data, t->data))
934 return -EFAULT;
935 } else {
936 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
937 return -EFAULT;
940 *size -= off;
941 *dstptr += tsize;
942 return 0;
944 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
945 #endif
947 struct xt_table_info *xt_alloc_table_info(unsigned int size)
949 struct xt_table_info *info = NULL;
950 size_t sz = sizeof(*info) + size;
952 if (sz < sizeof(*info))
953 return NULL;
955 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
956 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
957 return NULL;
959 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
960 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
961 if (!info) {
962 info = __vmalloc(sz, GFP_KERNEL | __GFP_NOWARN |
963 __GFP_NORETRY | __GFP_HIGHMEM,
964 PAGE_KERNEL);
965 if (!info)
966 return NULL;
968 memset(info, 0, sizeof(*info));
969 info->size = size;
970 return info;
972 EXPORT_SYMBOL(xt_alloc_table_info);
974 void xt_free_table_info(struct xt_table_info *info)
976 int cpu;
978 if (info->jumpstack != NULL) {
979 for_each_possible_cpu(cpu)
980 kvfree(info->jumpstack[cpu]);
981 kvfree(info->jumpstack);
984 kvfree(info);
986 EXPORT_SYMBOL(xt_free_table_info);
988 /* Find table by name, grabs mutex & ref. Returns NULL on error. */
989 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
990 const char *name)
992 struct xt_table *t, *found = NULL;
994 mutex_lock(&xt[af].mutex);
995 list_for_each_entry(t, &net->xt.tables[af], list)
996 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
997 return t;
999 if (net == &init_net)
1000 goto out;
1002 /* Table doesn't exist in this netns, re-try init */
1003 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1004 if (strcmp(t->name, name))
1005 continue;
1006 if (!try_module_get(t->me))
1007 return NULL;
1009 mutex_unlock(&xt[af].mutex);
1010 if (t->table_init(net) != 0) {
1011 module_put(t->me);
1012 return NULL;
1015 found = t;
1017 mutex_lock(&xt[af].mutex);
1018 break;
1021 if (!found)
1022 goto out;
1024 /* and once again: */
1025 list_for_each_entry(t, &net->xt.tables[af], list)
1026 if (strcmp(t->name, name) == 0)
1027 return t;
1029 module_put(found->me);
1030 out:
1031 mutex_unlock(&xt[af].mutex);
1032 return NULL;
1034 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1036 void xt_table_unlock(struct xt_table *table)
1038 mutex_unlock(&xt[table->af].mutex);
1040 EXPORT_SYMBOL_GPL(xt_table_unlock);
1042 #ifdef CONFIG_COMPAT
1043 void xt_compat_lock(u_int8_t af)
1045 mutex_lock(&xt[af].compat_mutex);
1047 EXPORT_SYMBOL_GPL(xt_compat_lock);
1049 void xt_compat_unlock(u_int8_t af)
1051 mutex_unlock(&xt[af].compat_mutex);
1053 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1054 #endif
1056 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1057 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1059 struct static_key xt_tee_enabled __read_mostly;
1060 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1062 static int xt_jumpstack_alloc(struct xt_table_info *i)
1064 unsigned int size;
1065 int cpu;
1067 size = sizeof(void **) * nr_cpu_ids;
1068 if (size > PAGE_SIZE)
1069 i->jumpstack = vzalloc(size);
1070 else
1071 i->jumpstack = kzalloc(size, GFP_KERNEL);
1072 if (i->jumpstack == NULL)
1073 return -ENOMEM;
1075 /* ruleset without jumps -- no stack needed */
1076 if (i->stacksize == 0)
1077 return 0;
1079 /* Jumpstack needs to be able to record two full callchains, one
1080 * from the first rule set traversal, plus one table reentrancy
1081 * via -j TEE without clobbering the callchain that brought us to
1082 * TEE target.
1084 * This is done by allocating two jumpstacks per cpu, on reentry
1085 * the upper half of the stack is used.
1087 * see the jumpstack setup in ipt_do_table() for more details.
1089 size = sizeof(void *) * i->stacksize * 2u;
1090 for_each_possible_cpu(cpu) {
1091 if (size > PAGE_SIZE)
1092 i->jumpstack[cpu] = vmalloc_node(size,
1093 cpu_to_node(cpu));
1094 else
1095 i->jumpstack[cpu] = kmalloc_node(size,
1096 GFP_KERNEL, cpu_to_node(cpu));
1097 if (i->jumpstack[cpu] == NULL)
1099 * Freeing will be done later on by the callers. The
1100 * chain is: xt_replace_table -> __do_replace ->
1101 * do_replace -> xt_free_table_info.
1103 return -ENOMEM;
1106 return 0;
1109 struct xt_table_info *
1110 xt_replace_table(struct xt_table *table,
1111 unsigned int num_counters,
1112 struct xt_table_info *newinfo,
1113 int *error)
1115 struct xt_table_info *private;
1116 int ret;
1118 ret = xt_jumpstack_alloc(newinfo);
1119 if (ret < 0) {
1120 *error = ret;
1121 return NULL;
1124 /* Do the substitution. */
1125 local_bh_disable();
1126 private = table->private;
1128 /* Check inside lock: is the old number correct? */
1129 if (num_counters != private->number) {
1130 pr_debug("num_counters != table->private->number (%u/%u)\n",
1131 num_counters, private->number);
1132 local_bh_enable();
1133 *error = -EAGAIN;
1134 return NULL;
1137 newinfo->initial_entries = private->initial_entries;
1139 * Ensure contents of newinfo are visible before assigning to
1140 * private.
1142 smp_wmb();
1143 table->private = newinfo;
1146 * Even though table entries have now been swapped, other CPU's
1147 * may still be using the old entries. This is okay, because
1148 * resynchronization happens because of the locking done
1149 * during the get_counters() routine.
1151 local_bh_enable();
1153 #ifdef CONFIG_AUDIT
1154 if (audit_enabled) {
1155 struct audit_buffer *ab;
1157 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1158 AUDIT_NETFILTER_CFG);
1159 if (ab) {
1160 audit_log_format(ab, "table=%s family=%u entries=%u",
1161 table->name, table->af,
1162 private->number);
1163 audit_log_end(ab);
1166 #endif
1168 return private;
1170 EXPORT_SYMBOL_GPL(xt_replace_table);
1172 struct xt_table *xt_register_table(struct net *net,
1173 const struct xt_table *input_table,
1174 struct xt_table_info *bootstrap,
1175 struct xt_table_info *newinfo)
1177 int ret;
1178 struct xt_table_info *private;
1179 struct xt_table *t, *table;
1181 /* Don't add one object to multiple lists. */
1182 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1183 if (!table) {
1184 ret = -ENOMEM;
1185 goto out;
1188 mutex_lock(&xt[table->af].mutex);
1189 /* Don't autoload: we'd eat our tail... */
1190 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1191 if (strcmp(t->name, table->name) == 0) {
1192 ret = -EEXIST;
1193 goto unlock;
1197 /* Simplifies replace_table code. */
1198 table->private = bootstrap;
1200 if (!xt_replace_table(table, 0, newinfo, &ret))
1201 goto unlock;
1203 private = table->private;
1204 pr_debug("table->private->number = %u\n", private->number);
1206 /* save number of initial entries */
1207 private->initial_entries = private->number;
1209 list_add(&table->list, &net->xt.tables[table->af]);
1210 mutex_unlock(&xt[table->af].mutex);
1211 return table;
1213 unlock:
1214 mutex_unlock(&xt[table->af].mutex);
1215 kfree(table);
1216 out:
1217 return ERR_PTR(ret);
1219 EXPORT_SYMBOL_GPL(xt_register_table);
1221 void *xt_unregister_table(struct xt_table *table)
1223 struct xt_table_info *private;
1225 mutex_lock(&xt[table->af].mutex);
1226 private = table->private;
1227 list_del(&table->list);
1228 mutex_unlock(&xt[table->af].mutex);
1229 kfree(table);
1231 return private;
1233 EXPORT_SYMBOL_GPL(xt_unregister_table);
1235 #ifdef CONFIG_PROC_FS
1236 struct xt_names_priv {
1237 struct seq_net_private p;
1238 u_int8_t af;
1240 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1242 struct xt_names_priv *priv = seq->private;
1243 struct net *net = seq_file_net(seq);
1244 u_int8_t af = priv->af;
1246 mutex_lock(&xt[af].mutex);
1247 return seq_list_start(&net->xt.tables[af], *pos);
1250 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1252 struct xt_names_priv *priv = seq->private;
1253 struct net *net = seq_file_net(seq);
1254 u_int8_t af = priv->af;
1256 return seq_list_next(v, &net->xt.tables[af], pos);
1259 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1261 struct xt_names_priv *priv = seq->private;
1262 u_int8_t af = priv->af;
1264 mutex_unlock(&xt[af].mutex);
1267 static int xt_table_seq_show(struct seq_file *seq, void *v)
1269 struct xt_table *table = list_entry(v, struct xt_table, list);
1271 if (*table->name)
1272 seq_printf(seq, "%s\n", table->name);
1273 return 0;
1276 static const struct seq_operations xt_table_seq_ops = {
1277 .start = xt_table_seq_start,
1278 .next = xt_table_seq_next,
1279 .stop = xt_table_seq_stop,
1280 .show = xt_table_seq_show,
1283 static int xt_table_open(struct inode *inode, struct file *file)
1285 int ret;
1286 struct xt_names_priv *priv;
1288 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1289 sizeof(struct xt_names_priv));
1290 if (!ret) {
1291 priv = ((struct seq_file *)file->private_data)->private;
1292 priv->af = (unsigned long)PDE_DATA(inode);
1294 return ret;
1297 static const struct file_operations xt_table_ops = {
1298 .owner = THIS_MODULE,
1299 .open = xt_table_open,
1300 .read = seq_read,
1301 .llseek = seq_lseek,
1302 .release = seq_release_net,
1306 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1307 * the multi-AF mutexes.
1309 struct nf_mttg_trav {
1310 struct list_head *head, *curr;
1311 uint8_t class, nfproto;
1314 enum {
1315 MTTG_TRAV_INIT,
1316 MTTG_TRAV_NFP_UNSPEC,
1317 MTTG_TRAV_NFP_SPEC,
1318 MTTG_TRAV_DONE,
1321 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1322 bool is_target)
1324 static const uint8_t next_class[] = {
1325 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1326 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1328 struct nf_mttg_trav *trav = seq->private;
1330 switch (trav->class) {
1331 case MTTG_TRAV_INIT:
1332 trav->class = MTTG_TRAV_NFP_UNSPEC;
1333 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1334 trav->head = trav->curr = is_target ?
1335 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1336 break;
1337 case MTTG_TRAV_NFP_UNSPEC:
1338 trav->curr = trav->curr->next;
1339 if (trav->curr != trav->head)
1340 break;
1341 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1342 mutex_lock(&xt[trav->nfproto].mutex);
1343 trav->head = trav->curr = is_target ?
1344 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1345 trav->class = next_class[trav->class];
1346 break;
1347 case MTTG_TRAV_NFP_SPEC:
1348 trav->curr = trav->curr->next;
1349 if (trav->curr != trav->head)
1350 break;
1351 /* fallthru, _stop will unlock */
1352 default:
1353 return NULL;
1356 if (ppos != NULL)
1357 ++*ppos;
1358 return trav;
1361 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1362 bool is_target)
1364 struct nf_mttg_trav *trav = seq->private;
1365 unsigned int j;
1367 trav->class = MTTG_TRAV_INIT;
1368 for (j = 0; j < *pos; ++j)
1369 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1370 return NULL;
1371 return trav;
1374 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1376 struct nf_mttg_trav *trav = seq->private;
1378 switch (trav->class) {
1379 case MTTG_TRAV_NFP_UNSPEC:
1380 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1381 break;
1382 case MTTG_TRAV_NFP_SPEC:
1383 mutex_unlock(&xt[trav->nfproto].mutex);
1384 break;
1388 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1390 return xt_mttg_seq_start(seq, pos, false);
1393 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1395 return xt_mttg_seq_next(seq, v, ppos, false);
1398 static int xt_match_seq_show(struct seq_file *seq, void *v)
1400 const struct nf_mttg_trav *trav = seq->private;
1401 const struct xt_match *match;
1403 switch (trav->class) {
1404 case MTTG_TRAV_NFP_UNSPEC:
1405 case MTTG_TRAV_NFP_SPEC:
1406 if (trav->curr == trav->head)
1407 return 0;
1408 match = list_entry(trav->curr, struct xt_match, list);
1409 if (*match->name)
1410 seq_printf(seq, "%s\n", match->name);
1412 return 0;
1415 static const struct seq_operations xt_match_seq_ops = {
1416 .start = xt_match_seq_start,
1417 .next = xt_match_seq_next,
1418 .stop = xt_mttg_seq_stop,
1419 .show = xt_match_seq_show,
1422 static int xt_match_open(struct inode *inode, struct file *file)
1424 struct nf_mttg_trav *trav;
1425 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1426 if (!trav)
1427 return -ENOMEM;
1429 trav->nfproto = (unsigned long)PDE_DATA(inode);
1430 return 0;
1433 static const struct file_operations xt_match_ops = {
1434 .owner = THIS_MODULE,
1435 .open = xt_match_open,
1436 .read = seq_read,
1437 .llseek = seq_lseek,
1438 .release = seq_release_private,
1441 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1443 return xt_mttg_seq_start(seq, pos, true);
1446 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1448 return xt_mttg_seq_next(seq, v, ppos, true);
1451 static int xt_target_seq_show(struct seq_file *seq, void *v)
1453 const struct nf_mttg_trav *trav = seq->private;
1454 const struct xt_target *target;
1456 switch (trav->class) {
1457 case MTTG_TRAV_NFP_UNSPEC:
1458 case MTTG_TRAV_NFP_SPEC:
1459 if (trav->curr == trav->head)
1460 return 0;
1461 target = list_entry(trav->curr, struct xt_target, list);
1462 if (*target->name)
1463 seq_printf(seq, "%s\n", target->name);
1465 return 0;
1468 static const struct seq_operations xt_target_seq_ops = {
1469 .start = xt_target_seq_start,
1470 .next = xt_target_seq_next,
1471 .stop = xt_mttg_seq_stop,
1472 .show = xt_target_seq_show,
1475 static int xt_target_open(struct inode *inode, struct file *file)
1477 struct nf_mttg_trav *trav;
1478 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1479 if (!trav)
1480 return -ENOMEM;
1482 trav->nfproto = (unsigned long)PDE_DATA(inode);
1483 return 0;
1486 static const struct file_operations xt_target_ops = {
1487 .owner = THIS_MODULE,
1488 .open = xt_target_open,
1489 .read = seq_read,
1490 .llseek = seq_lseek,
1491 .release = seq_release_private,
1494 #define FORMAT_TABLES "_tables_names"
1495 #define FORMAT_MATCHES "_tables_matches"
1496 #define FORMAT_TARGETS "_tables_targets"
1498 #endif /* CONFIG_PROC_FS */
1501 * xt_hook_ops_alloc - set up hooks for a new table
1502 * @table: table with metadata needed to set up hooks
1503 * @fn: Hook function
1505 * This function will create the nf_hook_ops that the x_table needs
1506 * to hand to xt_hook_link_net().
1508 struct nf_hook_ops *
1509 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1511 unsigned int hook_mask = table->valid_hooks;
1512 uint8_t i, num_hooks = hweight32(hook_mask);
1513 uint8_t hooknum;
1514 struct nf_hook_ops *ops;
1516 if (!num_hooks)
1517 return ERR_PTR(-EINVAL);
1519 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1520 if (ops == NULL)
1521 return ERR_PTR(-ENOMEM);
1523 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1524 hook_mask >>= 1, ++hooknum) {
1525 if (!(hook_mask & 1))
1526 continue;
1527 ops[i].hook = fn;
1528 ops[i].pf = table->af;
1529 ops[i].hooknum = hooknum;
1530 ops[i].priority = table->priority;
1531 ++i;
1534 return ops;
1536 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1538 int xt_proto_init(struct net *net, u_int8_t af)
1540 #ifdef CONFIG_PROC_FS
1541 char buf[XT_FUNCTION_MAXNAMELEN];
1542 struct proc_dir_entry *proc;
1543 kuid_t root_uid;
1544 kgid_t root_gid;
1545 #endif
1547 if (af >= ARRAY_SIZE(xt_prefix))
1548 return -EINVAL;
1551 #ifdef CONFIG_PROC_FS
1552 root_uid = make_kuid(net->user_ns, 0);
1553 root_gid = make_kgid(net->user_ns, 0);
1555 strlcpy(buf, xt_prefix[af], sizeof(buf));
1556 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1557 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1558 (void *)(unsigned long)af);
1559 if (!proc)
1560 goto out;
1561 if (uid_valid(root_uid) && gid_valid(root_gid))
1562 proc_set_user(proc, root_uid, root_gid);
1564 strlcpy(buf, xt_prefix[af], sizeof(buf));
1565 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1566 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1567 (void *)(unsigned long)af);
1568 if (!proc)
1569 goto out_remove_tables;
1570 if (uid_valid(root_uid) && gid_valid(root_gid))
1571 proc_set_user(proc, root_uid, root_gid);
1573 strlcpy(buf, xt_prefix[af], sizeof(buf));
1574 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1575 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1576 (void *)(unsigned long)af);
1577 if (!proc)
1578 goto out_remove_matches;
1579 if (uid_valid(root_uid) && gid_valid(root_gid))
1580 proc_set_user(proc, root_uid, root_gid);
1581 #endif
1583 return 0;
1585 #ifdef CONFIG_PROC_FS
1586 out_remove_matches:
1587 strlcpy(buf, xt_prefix[af], sizeof(buf));
1588 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1589 remove_proc_entry(buf, net->proc_net);
1591 out_remove_tables:
1592 strlcpy(buf, xt_prefix[af], sizeof(buf));
1593 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1594 remove_proc_entry(buf, net->proc_net);
1595 out:
1596 return -1;
1597 #endif
1599 EXPORT_SYMBOL_GPL(xt_proto_init);
1601 void xt_proto_fini(struct net *net, u_int8_t af)
1603 #ifdef CONFIG_PROC_FS
1604 char buf[XT_FUNCTION_MAXNAMELEN];
1606 strlcpy(buf, xt_prefix[af], sizeof(buf));
1607 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1608 remove_proc_entry(buf, net->proc_net);
1610 strlcpy(buf, xt_prefix[af], sizeof(buf));
1611 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1612 remove_proc_entry(buf, net->proc_net);
1614 strlcpy(buf, xt_prefix[af], sizeof(buf));
1615 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1616 remove_proc_entry(buf, net->proc_net);
1617 #endif /*CONFIG_PROC_FS*/
1619 EXPORT_SYMBOL_GPL(xt_proto_fini);
1622 * xt_percpu_counter_alloc - allocate x_tables rule counter
1624 * @state: pointer to xt_percpu allocation state
1625 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1627 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1628 * contain the address of the real (percpu) counter.
1630 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1631 * to fetch the real percpu counter.
1633 * To speed up allocation and improve data locality, a 4kb block is
1634 * allocated.
1636 * xt_percpu_counter_alloc_state contains the base address of the
1637 * allocated page and the current sub-offset.
1639 * returns false on error.
1641 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1642 struct xt_counters *counter)
1644 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1646 if (nr_cpu_ids <= 1)
1647 return true;
1649 if (!state->mem) {
1650 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1651 XT_PCPU_BLOCK_SIZE);
1652 if (!state->mem)
1653 return false;
1655 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1656 state->off += sizeof(*counter);
1657 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1658 state->mem = NULL;
1659 state->off = 0;
1661 return true;
1663 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1665 void xt_percpu_counter_free(struct xt_counters *counters)
1667 unsigned long pcnt = counters->pcnt;
1669 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1670 free_percpu((void __percpu *)pcnt);
1672 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1674 static int __net_init xt_net_init(struct net *net)
1676 int i;
1678 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1679 INIT_LIST_HEAD(&net->xt.tables[i]);
1680 return 0;
1683 static struct pernet_operations xt_net_ops = {
1684 .init = xt_net_init,
1687 static int __init xt_init(void)
1689 unsigned int i;
1690 int rv;
1692 for_each_possible_cpu(i) {
1693 seqcount_init(&per_cpu(xt_recseq, i));
1696 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1697 if (!xt)
1698 return -ENOMEM;
1700 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1701 mutex_init(&xt[i].mutex);
1702 #ifdef CONFIG_COMPAT
1703 mutex_init(&xt[i].compat_mutex);
1704 xt[i].compat_tab = NULL;
1705 #endif
1706 INIT_LIST_HEAD(&xt[i].target);
1707 INIT_LIST_HEAD(&xt[i].match);
1709 rv = register_pernet_subsys(&xt_net_ops);
1710 if (rv < 0)
1711 kfree(xt);
1712 return rv;
1715 static void __exit xt_fini(void)
1717 unregister_pernet_subsys(&xt_net_ops);
1718 kfree(xt);
1721 module_init(xt_init);
1722 module_exit(xt_fini);