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>
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 XT_PCPU_BLOCK_SIZE 4096
43 #define XT_MAX_TABLE_SIZE (512 * 1024 * 1024)
46 unsigned int offset; /* offset in kernel */
47 int delta; /* delta in 32bit user land */
52 struct list_head match;
53 struct list_head target;
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[] */
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);
82 EXPORT_SYMBOL(xt_register_target);
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);
96 xt_register_targets(struct xt_target *target, unsigned int n)
101 for (i = 0; i < n; i++) {
102 err = xt_register_target(&target[i]);
110 xt_unregister_targets(target, i);
113 EXPORT_SYMBOL(xt_register_targets);
116 xt_unregister_targets(struct xt_target *target, unsigned int n)
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);
132 EXPORT_SYMBOL(xt_register_match);
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)
151 for (i = 0; i < n; i++) {
152 err = xt_register_match(&match[i]);
160 xt_unregister_matches(match, i);
163 EXPORT_SYMBOL(xt_register_matches);
166 xt_unregister_matches(struct xt_match *match, unsigned int n)
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
180 /* Find match, grabs ref. Returns ERR_PTR() on error. */
181 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
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);
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);
206 EXPORT_SYMBOL(xt_find_match);
209 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
211 struct xt_match *match;
213 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
214 return ERR_PTR(-EINVAL);
216 match = xt_find_match(nfproto, name, revision);
218 request_module("%st_%s", xt_prefix[nfproto], name);
219 match = xt_find_match(nfproto, name, revision);
224 EXPORT_SYMBOL_GPL(xt_request_find_match);
226 /* Find target, grabs ref. Returns ERR_PTR() on error. */
227 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
232 mutex_lock(&xt[af].mutex);
233 list_for_each_entry(t, &xt[af].target, list) {
234 if (strcmp(t->name, name) == 0) {
235 if (t->revision == revision) {
236 if (try_module_get(t->me)) {
237 mutex_unlock(&xt[af].mutex);
241 err = -EPROTOTYPE; /* Found something. */
244 mutex_unlock(&xt[af].mutex);
246 if (af != NFPROTO_UNSPEC)
247 /* Try searching again in the family-independent list */
248 return xt_find_target(NFPROTO_UNSPEC, name, revision);
252 EXPORT_SYMBOL(xt_find_target);
254 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
256 struct xt_target *target;
258 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
259 return ERR_PTR(-EINVAL);
261 target = xt_find_target(af, name, revision);
262 if (IS_ERR(target)) {
263 request_module("%st_%s", xt_prefix[af], name);
264 target = xt_find_target(af, name, revision);
269 EXPORT_SYMBOL_GPL(xt_request_find_target);
272 static int xt_obj_to_user(u16 __user *psize, u16 size,
273 void __user *pname, const char *name,
274 u8 __user *prev, u8 rev)
276 if (put_user(size, psize))
278 if (copy_to_user(pname, name, strlen(name) + 1))
280 if (put_user(rev, prev))
286 #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
287 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
288 U->u.user.name, K->u.kernel.TYPE->name, \
289 &U->u.user.revision, K->u.kernel.TYPE->revision)
291 int xt_data_to_user(void __user *dst, const void *src,
292 int usersize, int size, int aligned_size)
294 usersize = usersize ? : size;
295 if (copy_to_user(dst, src, usersize))
297 if (usersize != aligned_size &&
298 clear_user(dst + usersize, aligned_size - usersize))
303 EXPORT_SYMBOL_GPL(xt_data_to_user);
305 #define XT_DATA_TO_USER(U, K, TYPE) \
306 xt_data_to_user(U->data, K->data, \
307 K->u.kernel.TYPE->usersize, \
308 K->u.kernel.TYPE->TYPE##size, \
309 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
311 int xt_match_to_user(const struct xt_entry_match *m,
312 struct xt_entry_match __user *u)
314 return XT_OBJ_TO_USER(u, m, match, 0) ||
315 XT_DATA_TO_USER(u, m, match);
317 EXPORT_SYMBOL_GPL(xt_match_to_user);
319 int xt_target_to_user(const struct xt_entry_target *t,
320 struct xt_entry_target __user *u)
322 return XT_OBJ_TO_USER(u, t, target, 0) ||
323 XT_DATA_TO_USER(u, t, target);
325 EXPORT_SYMBOL_GPL(xt_target_to_user);
327 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
329 const struct xt_match *m;
332 list_for_each_entry(m, &xt[af].match, list) {
333 if (strcmp(m->name, name) == 0) {
334 if (m->revision > *bestp)
335 *bestp = m->revision;
336 if (m->revision == revision)
341 if (af != NFPROTO_UNSPEC && !have_rev)
342 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
347 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
349 const struct xt_target *t;
352 list_for_each_entry(t, &xt[af].target, list) {
353 if (strcmp(t->name, name) == 0) {
354 if (t->revision > *bestp)
355 *bestp = t->revision;
356 if (t->revision == revision)
361 if (af != NFPROTO_UNSPEC && !have_rev)
362 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
367 /* Returns true or false (if no such extension at all) */
368 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
371 int have_rev, best = -1;
373 mutex_lock(&xt[af].mutex);
375 have_rev = target_revfn(af, name, revision, &best);
377 have_rev = match_revfn(af, name, revision, &best);
378 mutex_unlock(&xt[af].mutex);
380 /* Nothing at all? Return 0 to try loading module. */
388 *err = -EPROTONOSUPPORT;
391 EXPORT_SYMBOL_GPL(xt_find_revision);
394 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
396 static const char *const inetbr_names[] = {
397 "PREROUTING", "INPUT", "FORWARD",
398 "OUTPUT", "POSTROUTING", "BROUTING",
400 static const char *const arp_names[] = {
401 "INPUT", "FORWARD", "OUTPUT",
403 const char *const *names;
409 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
410 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
411 ARRAY_SIZE(inetbr_names);
413 for (i = 0; i < max; ++i) {
414 if (!(mask & (1 << i)))
416 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
428 * xt_check_proc_name - check that name is suitable for /proc file creation
430 * @name: file name candidate
431 * @size: length of buffer
433 * some x_tables modules wish to create a file in /proc.
434 * This function makes sure that the name is suitable for this
435 * purpose, it checks that name is NUL terminated and isn't a 'special'
438 * returns negative number on error or 0 if name is useable.
440 int xt_check_proc_name(const char *name, unsigned int size)
445 if (strnlen(name, size) == size)
446 return -ENAMETOOLONG;
448 if (strcmp(name, ".") == 0 ||
449 strcmp(name, "..") == 0 ||
455 EXPORT_SYMBOL(xt_check_proc_name);
457 int xt_check_match(struct xt_mtchk_param *par,
458 unsigned int size, u_int8_t proto, bool inv_proto)
462 if (XT_ALIGN(par->match->matchsize) != size &&
463 par->match->matchsize != -1) {
465 * ebt_among is exempt from centralized matchsize checking
466 * because it uses a dynamic-size data set.
468 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
469 xt_prefix[par->family], par->match->name,
470 par->match->revision,
471 XT_ALIGN(par->match->matchsize), size);
474 if (par->match->table != NULL &&
475 strcmp(par->match->table, par->table) != 0) {
476 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
477 xt_prefix[par->family], par->match->name,
478 par->match->table, par->table);
481 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
482 char used[64], allow[64];
484 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
485 xt_prefix[par->family], par->match->name,
486 textify_hooks(used, sizeof(used),
487 par->hook_mask, par->family),
488 textify_hooks(allow, sizeof(allow),
493 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
494 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
495 xt_prefix[par->family], par->match->name,
499 if (par->match->checkentry != NULL) {
500 ret = par->match->checkentry(par);
504 /* Flag up potential errors. */
509 EXPORT_SYMBOL_GPL(xt_check_match);
511 /** xt_check_entry_match - check that matches end before start of target
513 * @match: beginning of xt_entry_match
514 * @target: beginning of this rules target (alleged end of matches)
515 * @alignment: alignment requirement of match structures
517 * Validates that all matches add up to the beginning of the target,
518 * and that each match covers at least the base structure size.
520 * Return: 0 on success, negative errno on failure.
522 static int xt_check_entry_match(const char *match, const char *target,
523 const size_t alignment)
525 const struct xt_entry_match *pos;
526 int length = target - match;
528 if (length == 0) /* no matches */
531 pos = (struct xt_entry_match *)match;
533 if ((unsigned long)pos % alignment)
536 if (length < (int)sizeof(struct xt_entry_match))
539 if (pos->u.match_size < sizeof(struct xt_entry_match))
542 if (pos->u.match_size > length)
545 length -= pos->u.match_size;
546 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
547 } while (length > 0);
552 /** xt_check_table_hooks - check hook entry points are sane
554 * @info xt_table_info to check
555 * @valid_hooks - hook entry points that we can enter from
557 * Validates that the hook entry and underflows points are set up.
559 * Return: 0 on success, negative errno on failure.
561 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
563 const char *err = "unsorted underflow";
564 unsigned int i, max_uflow, max_entry;
565 bool check_hooks = false;
567 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
572 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
573 if (!(valid_hooks & (1 << i)))
576 if (info->hook_entry[i] == 0xFFFFFFFF)
578 if (info->underflow[i] == 0xFFFFFFFF)
582 if (max_uflow > info->underflow[i])
585 if (max_uflow == info->underflow[i]) {
586 err = "duplicate underflow";
589 if (max_entry > info->hook_entry[i]) {
590 err = "unsorted entry";
593 if (max_entry == info->hook_entry[i]) {
594 err = "duplicate entry";
598 max_entry = info->hook_entry[i];
599 max_uflow = info->underflow[i];
605 pr_err_ratelimited("%s at hook %d\n", err, i);
608 EXPORT_SYMBOL(xt_check_table_hooks);
610 static bool verdict_ok(int verdict)
616 int v = -verdict - 1;
618 if (verdict == XT_RETURN)
622 case NF_ACCEPT: return true;
623 case NF_DROP: return true;
624 case NF_QUEUE: return true;
635 static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
636 const char *msg, unsigned int msglen)
638 return usersize == kernsize && strnlen(msg, msglen) < msglen;
642 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
644 struct xt_af *xp = &xt[af];
646 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
648 if (WARN_ON(!xp->compat_tab))
651 if (xp->cur >= xp->number)
655 delta += xp->compat_tab[xp->cur - 1].delta;
656 xp->compat_tab[xp->cur].offset = offset;
657 xp->compat_tab[xp->cur].delta = delta;
661 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
663 void xt_compat_flush_offsets(u_int8_t af)
665 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
667 if (xt[af].compat_tab) {
668 vfree(xt[af].compat_tab);
669 xt[af].compat_tab = NULL;
674 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
676 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
678 struct compat_delta *tmp = xt[af].compat_tab;
679 int mid, left = 0, right = xt[af].cur - 1;
681 while (left <= right) {
682 mid = (left + right) >> 1;
683 if (offset > tmp[mid].offset)
685 else if (offset < tmp[mid].offset)
688 return mid ? tmp[mid - 1].delta : 0;
690 return left ? tmp[left - 1].delta : 0;
692 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
694 int xt_compat_init_offsets(u8 af, unsigned int number)
698 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
700 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
703 if (WARN_ON(xt[af].compat_tab))
706 mem = sizeof(struct compat_delta) * number;
707 if (mem > XT_MAX_TABLE_SIZE)
710 xt[af].compat_tab = vmalloc(mem);
711 if (!xt[af].compat_tab)
714 xt[af].number = number;
719 EXPORT_SYMBOL(xt_compat_init_offsets);
721 int xt_compat_match_offset(const struct xt_match *match)
723 u_int16_t csize = match->compatsize ? : match->matchsize;
724 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
726 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
728 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
731 const struct xt_match *match = m->u.kernel.match;
732 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
733 int pad, off = xt_compat_match_offset(match);
734 u_int16_t msize = cm->u.user.match_size;
735 char name[sizeof(m->u.user.name)];
738 memcpy(m, cm, sizeof(*cm));
739 if (match->compat_from_user)
740 match->compat_from_user(m->data, cm->data);
742 memcpy(m->data, cm->data, msize - sizeof(*cm));
743 pad = XT_ALIGN(match->matchsize) - match->matchsize;
745 memset(m->data + match->matchsize, 0, pad);
748 m->u.user.match_size = msize;
749 strlcpy(name, match->name, sizeof(name));
750 module_put(match->me);
751 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
756 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
758 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
759 xt_data_to_user(U->data, K->data, \
760 K->u.kernel.TYPE->usersize, \
762 COMPAT_XT_ALIGN(C_SIZE))
764 int xt_compat_match_to_user(const struct xt_entry_match *m,
765 void __user **dstptr, unsigned int *size)
767 const struct xt_match *match = m->u.kernel.match;
768 struct compat_xt_entry_match __user *cm = *dstptr;
769 int off = xt_compat_match_offset(match);
770 u_int16_t msize = m->u.user.match_size - off;
772 if (XT_OBJ_TO_USER(cm, m, match, msize))
775 if (match->compat_to_user) {
776 if (match->compat_to_user((void __user *)cm->data, m->data))
779 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
787 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
789 /* non-compat version may have padding after verdict */
790 struct compat_xt_standard_target {
791 struct compat_xt_entry_target t;
792 compat_uint_t verdict;
795 struct compat_xt_error_target {
796 struct compat_xt_entry_target t;
797 char errorname[XT_FUNCTION_MAXNAMELEN];
800 int xt_compat_check_entry_offsets(const void *base, const char *elems,
801 unsigned int target_offset,
802 unsigned int next_offset)
804 long size_of_base_struct = elems - (const char *)base;
805 const struct compat_xt_entry_target *t;
806 const char *e = base;
808 if (target_offset < size_of_base_struct)
811 if (target_offset + sizeof(*t) > next_offset)
814 t = (void *)(e + target_offset);
815 if (t->u.target_size < sizeof(*t))
818 if (target_offset + t->u.target_size > next_offset)
821 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
822 const struct compat_xt_standard_target *st = (const void *)t;
824 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
827 if (!verdict_ok(st->verdict))
829 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
830 const struct compat_xt_error_target *et = (const void *)t;
832 if (!error_tg_ok(t->u.target_size, sizeof(*et),
833 et->errorname, sizeof(et->errorname)))
837 /* compat_xt_entry match has less strict alignment requirements,
838 * otherwise they are identical. In case of padding differences
839 * we need to add compat version of xt_check_entry_match.
841 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
843 return xt_check_entry_match(elems, base + target_offset,
844 __alignof__(struct compat_xt_entry_match));
846 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
847 #endif /* CONFIG_COMPAT */
850 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
852 * @base: pointer to arp/ip/ip6t_entry
853 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
854 * @target_offset: the arp/ip/ip6_t->target_offset
855 * @next_offset: the arp/ip/ip6_t->next_offset
857 * validates that target_offset and next_offset are sane and that all
858 * match sizes (if any) align with the target offset.
860 * This function does not validate the targets or matches themselves, it
861 * only tests that all the offsets and sizes are correct, that all
862 * match structures are aligned, and that the last structure ends where
863 * the target structure begins.
865 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
867 * The arp/ip/ip6t_entry structure @base must have passed following tests:
868 * - it must point to a valid memory location
869 * - base to base + next_offset must be accessible, i.e. not exceed allocated
872 * A well-formed entry looks like this:
874 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
875 * e->elems[]-----' | |
879 * target_offset---------------------------------' |
880 * next_offset---------------------------------------------------'
882 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
883 * This is where matches (if any) and the target reside.
884 * target_offset: beginning of target.
885 * next_offset: start of the next rule; also: size of this rule.
886 * Since targets have a minimum size, target_offset + minlen <= next_offset.
888 * Every match stores its size, sum of sizes must not exceed target_offset.
890 * Return: 0 on success, negative errno on failure.
892 int xt_check_entry_offsets(const void *base,
894 unsigned int target_offset,
895 unsigned int next_offset)
897 long size_of_base_struct = elems - (const char *)base;
898 const struct xt_entry_target *t;
899 const char *e = base;
901 /* target start is within the ip/ip6/arpt_entry struct */
902 if (target_offset < size_of_base_struct)
905 if (target_offset + sizeof(*t) > next_offset)
908 t = (void *)(e + target_offset);
909 if (t->u.target_size < sizeof(*t))
912 if (target_offset + t->u.target_size > next_offset)
915 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
916 const struct xt_standard_target *st = (const void *)t;
918 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
921 if (!verdict_ok(st->verdict))
923 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
924 const struct xt_error_target *et = (const void *)t;
926 if (!error_tg_ok(t->u.target_size, sizeof(*et),
927 et->errorname, sizeof(et->errorname)))
931 return xt_check_entry_match(elems, base + target_offset,
932 __alignof__(struct xt_entry_match));
934 EXPORT_SYMBOL(xt_check_entry_offsets);
937 * xt_alloc_entry_offsets - allocate array to store rule head offsets
939 * @size: number of entries
941 * Return: NULL or kmalloc'd or vmalloc'd array
943 unsigned int *xt_alloc_entry_offsets(unsigned int size)
945 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
948 return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
951 EXPORT_SYMBOL(xt_alloc_entry_offsets);
954 * xt_find_jump_offset - check if target is a valid jump offset
956 * @offsets: array containing all valid rule start offsets of a rule blob
957 * @target: the jump target to search for
958 * @size: entries in @offset
960 bool xt_find_jump_offset(const unsigned int *offsets,
961 unsigned int target, unsigned int size)
963 int m, low = 0, hi = size;
968 if (offsets[m] > target)
970 else if (offsets[m] < target)
978 EXPORT_SYMBOL(xt_find_jump_offset);
980 int xt_check_target(struct xt_tgchk_param *par,
981 unsigned int size, u_int8_t proto, bool inv_proto)
985 if (XT_ALIGN(par->target->targetsize) != size) {
986 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
987 xt_prefix[par->family], par->target->name,
988 par->target->revision,
989 XT_ALIGN(par->target->targetsize), size);
992 if (par->target->table != NULL &&
993 strcmp(par->target->table, par->table) != 0) {
994 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
995 xt_prefix[par->family], par->target->name,
996 par->target->table, par->table);
999 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1000 char used[64], allow[64];
1002 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1003 xt_prefix[par->family], par->target->name,
1004 textify_hooks(used, sizeof(used),
1005 par->hook_mask, par->family),
1006 textify_hooks(allow, sizeof(allow),
1011 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1012 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1013 xt_prefix[par->family], par->target->name,
1014 par->target->proto);
1017 if (par->target->checkentry != NULL) {
1018 ret = par->target->checkentry(par);
1022 /* Flag up potential errors. */
1027 EXPORT_SYMBOL_GPL(xt_check_target);
1030 * xt_copy_counters_from_user - copy counters and metadata from userspace
1032 * @user: src pointer to userspace memory
1033 * @len: alleged size of userspace memory
1034 * @info: where to store the xt_counters_info metadata
1035 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
1037 * Copies counter meta data from @user and stores it in @info.
1039 * vmallocs memory to hold the counters, then copies the counter data
1040 * from @user to the new memory and returns a pointer to it.
1042 * If @compat is true, @info gets converted automatically to the 64bit
1045 * The metadata associated with the counters is stored in @info.
1047 * Return: returns pointer that caller has to test via IS_ERR().
1048 * If IS_ERR is false, caller has to vfree the pointer.
1050 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
1051 struct xt_counters_info *info, bool compat)
1056 #ifdef CONFIG_COMPAT
1058 /* structures only differ in size due to alignment */
1059 struct compat_xt_counters_info compat_tmp;
1061 if (len <= sizeof(compat_tmp))
1062 return ERR_PTR(-EINVAL);
1064 len -= sizeof(compat_tmp);
1065 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
1066 return ERR_PTR(-EFAULT);
1068 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1069 info->num_counters = compat_tmp.num_counters;
1070 user += sizeof(compat_tmp);
1074 if (len <= sizeof(*info))
1075 return ERR_PTR(-EINVAL);
1077 len -= sizeof(*info);
1078 if (copy_from_user(info, user, sizeof(*info)) != 0)
1079 return ERR_PTR(-EFAULT);
1081 user += sizeof(*info);
1083 info->name[sizeof(info->name) - 1] = '\0';
1085 size = sizeof(struct xt_counters);
1086 size *= info->num_counters;
1088 if (size != (u64)len)
1089 return ERR_PTR(-EINVAL);
1093 return ERR_PTR(-ENOMEM);
1095 if (copy_from_user(mem, user, len) == 0)
1099 return ERR_PTR(-EFAULT);
1101 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
1103 #ifdef CONFIG_COMPAT
1104 int xt_compat_target_offset(const struct xt_target *target)
1106 u_int16_t csize = target->compatsize ? : target->targetsize;
1107 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1109 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1111 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1114 const struct xt_target *target = t->u.kernel.target;
1115 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1116 int pad, off = xt_compat_target_offset(target);
1117 u_int16_t tsize = ct->u.user.target_size;
1118 char name[sizeof(t->u.user.name)];
1121 memcpy(t, ct, sizeof(*ct));
1122 if (target->compat_from_user)
1123 target->compat_from_user(t->data, ct->data);
1125 memcpy(t->data, ct->data, tsize - sizeof(*ct));
1126 pad = XT_ALIGN(target->targetsize) - target->targetsize;
1128 memset(t->data + target->targetsize, 0, pad);
1131 t->u.user.target_size = tsize;
1132 strlcpy(name, target->name, sizeof(name));
1133 module_put(target->me);
1134 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
1139 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1141 int xt_compat_target_to_user(const struct xt_entry_target *t,
1142 void __user **dstptr, unsigned int *size)
1144 const struct xt_target *target = t->u.kernel.target;
1145 struct compat_xt_entry_target __user *ct = *dstptr;
1146 int off = xt_compat_target_offset(target);
1147 u_int16_t tsize = t->u.user.target_size - off;
1149 if (XT_OBJ_TO_USER(ct, t, target, tsize))
1152 if (target->compat_to_user) {
1153 if (target->compat_to_user((void __user *)ct->data, t->data))
1156 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1164 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1167 struct xt_table_info *xt_alloc_table_info(unsigned int size)
1169 struct xt_table_info *info = NULL;
1170 size_t sz = sizeof(*info) + size;
1172 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1175 /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1176 * work reasonably well if sz is too large and bail out rather
1177 * than shoot all processes down before realizing there is nothing
1180 info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
1184 memset(info, 0, sizeof(*info));
1188 EXPORT_SYMBOL(xt_alloc_table_info);
1190 void xt_free_table_info(struct xt_table_info *info)
1194 if (info->jumpstack != NULL) {
1195 for_each_possible_cpu(cpu)
1196 kvfree(info->jumpstack[cpu]);
1197 kvfree(info->jumpstack);
1202 EXPORT_SYMBOL(xt_free_table_info);
1204 /* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
1205 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1208 struct xt_table *t, *found = NULL;
1210 mutex_lock(&xt[af].mutex);
1211 list_for_each_entry(t, &net->xt.tables[af], list)
1212 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1215 if (net == &init_net)
1218 /* Table doesn't exist in this netns, re-try init */
1219 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1222 if (strcmp(t->name, name))
1224 if (!try_module_get(t->me))
1226 mutex_unlock(&xt[af].mutex);
1227 err = t->table_init(net);
1230 return ERR_PTR(err);
1235 mutex_lock(&xt[af].mutex);
1242 /* and once again: */
1243 list_for_each_entry(t, &net->xt.tables[af], list)
1244 if (strcmp(t->name, name) == 0)
1247 module_put(found->me);
1249 mutex_unlock(&xt[af].mutex);
1250 return ERR_PTR(-ENOENT);
1252 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1254 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1257 struct xt_table *t = xt_find_table_lock(net, af, name);
1259 #ifdef CONFIG_MODULES
1261 int err = request_module("%stable_%s", xt_prefix[af], name);
1263 return ERR_PTR(err);
1264 t = xt_find_table_lock(net, af, name);
1270 EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1272 void xt_table_unlock(struct xt_table *table)
1274 mutex_unlock(&xt[table->af].mutex);
1276 EXPORT_SYMBOL_GPL(xt_table_unlock);
1278 #ifdef CONFIG_COMPAT
1279 void xt_compat_lock(u_int8_t af)
1281 mutex_lock(&xt[af].compat_mutex);
1283 EXPORT_SYMBOL_GPL(xt_compat_lock);
1285 void xt_compat_unlock(u_int8_t af)
1287 mutex_unlock(&xt[af].compat_mutex);
1289 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1292 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1293 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1295 struct static_key xt_tee_enabled __read_mostly;
1296 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1298 static int xt_jumpstack_alloc(struct xt_table_info *i)
1303 size = sizeof(void **) * nr_cpu_ids;
1304 if (size > PAGE_SIZE)
1305 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1307 i->jumpstack = kzalloc(size, GFP_KERNEL);
1308 if (i->jumpstack == NULL)
1311 /* ruleset without jumps -- no stack needed */
1312 if (i->stacksize == 0)
1315 /* Jumpstack needs to be able to record two full callchains, one
1316 * from the first rule set traversal, plus one table reentrancy
1317 * via -j TEE without clobbering the callchain that brought us to
1320 * This is done by allocating two jumpstacks per cpu, on reentry
1321 * the upper half of the stack is used.
1323 * see the jumpstack setup in ipt_do_table() for more details.
1325 size = sizeof(void *) * i->stacksize * 2u;
1326 for_each_possible_cpu(cpu) {
1327 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1329 if (i->jumpstack[cpu] == NULL)
1331 * Freeing will be done later on by the callers. The
1332 * chain is: xt_replace_table -> __do_replace ->
1333 * do_replace -> xt_free_table_info.
1341 struct xt_counters *xt_counters_alloc(unsigned int counters)
1343 struct xt_counters *mem;
1345 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1348 counters *= sizeof(*mem);
1349 if (counters > XT_MAX_TABLE_SIZE)
1352 return vzalloc(counters);
1354 EXPORT_SYMBOL(xt_counters_alloc);
1356 struct xt_table_info *
1357 xt_replace_table(struct xt_table *table,
1358 unsigned int num_counters,
1359 struct xt_table_info *newinfo,
1362 struct xt_table_info *private;
1366 ret = xt_jumpstack_alloc(newinfo);
1372 /* Do the substitution. */
1374 private = table->private;
1376 /* Check inside lock: is the old number correct? */
1377 if (num_counters != private->number) {
1378 pr_debug("num_counters != table->private->number (%u/%u)\n",
1379 num_counters, private->number);
1385 newinfo->initial_entries = private->initial_entries;
1387 * Ensure contents of newinfo are visible before assigning to
1391 table->private = newinfo;
1393 /* make sure all cpus see new ->private value */
1397 * Even though table entries have now been swapped, other CPU's
1398 * may still be using the old entries...
1402 /* ... so wait for even xt_recseq on all cpus */
1403 for_each_possible_cpu(cpu) {
1404 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1405 u32 seq = raw_read_seqcount(s);
1411 } while (seq == raw_read_seqcount(s));
1416 if (audit_enabled) {
1417 audit_log(current->audit_context, GFP_KERNEL,
1418 AUDIT_NETFILTER_CFG,
1419 "table=%s family=%u entries=%u",
1420 table->name, table->af, private->number);
1426 EXPORT_SYMBOL_GPL(xt_replace_table);
1428 struct xt_table *xt_register_table(struct net *net,
1429 const struct xt_table *input_table,
1430 struct xt_table_info *bootstrap,
1431 struct xt_table_info *newinfo)
1434 struct xt_table_info *private;
1435 struct xt_table *t, *table;
1437 /* Don't add one object to multiple lists. */
1438 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1444 mutex_lock(&xt[table->af].mutex);
1445 /* Don't autoload: we'd eat our tail... */
1446 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1447 if (strcmp(t->name, table->name) == 0) {
1453 /* Simplifies replace_table code. */
1454 table->private = bootstrap;
1456 if (!xt_replace_table(table, 0, newinfo, &ret))
1459 private = table->private;
1460 pr_debug("table->private->number = %u\n", private->number);
1462 /* save number of initial entries */
1463 private->initial_entries = private->number;
1465 list_add(&table->list, &net->xt.tables[table->af]);
1466 mutex_unlock(&xt[table->af].mutex);
1470 mutex_unlock(&xt[table->af].mutex);
1473 return ERR_PTR(ret);
1475 EXPORT_SYMBOL_GPL(xt_register_table);
1477 void *xt_unregister_table(struct xt_table *table)
1479 struct xt_table_info *private;
1481 mutex_lock(&xt[table->af].mutex);
1482 private = table->private;
1483 list_del(&table->list);
1484 mutex_unlock(&xt[table->af].mutex);
1489 EXPORT_SYMBOL_GPL(xt_unregister_table);
1491 #ifdef CONFIG_PROC_FS
1492 struct xt_names_priv {
1493 struct seq_net_private p;
1496 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1498 struct xt_names_priv *priv = seq->private;
1499 struct net *net = seq_file_net(seq);
1500 u_int8_t af = priv->af;
1502 mutex_lock(&xt[af].mutex);
1503 return seq_list_start(&net->xt.tables[af], *pos);
1506 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1508 struct xt_names_priv *priv = seq->private;
1509 struct net *net = seq_file_net(seq);
1510 u_int8_t af = priv->af;
1512 return seq_list_next(v, &net->xt.tables[af], pos);
1515 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1517 struct xt_names_priv *priv = seq->private;
1518 u_int8_t af = priv->af;
1520 mutex_unlock(&xt[af].mutex);
1523 static int xt_table_seq_show(struct seq_file *seq, void *v)
1525 struct xt_table *table = list_entry(v, struct xt_table, list);
1528 seq_printf(seq, "%s\n", table->name);
1532 static const struct seq_operations xt_table_seq_ops = {
1533 .start = xt_table_seq_start,
1534 .next = xt_table_seq_next,
1535 .stop = xt_table_seq_stop,
1536 .show = xt_table_seq_show,
1539 static int xt_table_open(struct inode *inode, struct file *file)
1542 struct xt_names_priv *priv;
1544 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1545 sizeof(struct xt_names_priv));
1547 priv = ((struct seq_file *)file->private_data)->private;
1548 priv->af = (unsigned long)PDE_DATA(inode);
1553 static const struct file_operations xt_table_ops = {
1554 .open = xt_table_open,
1556 .llseek = seq_lseek,
1557 .release = seq_release_net,
1561 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1562 * the multi-AF mutexes.
1564 struct nf_mttg_trav {
1565 struct list_head *head, *curr;
1566 uint8_t class, nfproto;
1571 MTTG_TRAV_NFP_UNSPEC,
1576 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1579 static const uint8_t next_class[] = {
1580 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1581 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1583 struct nf_mttg_trav *trav = seq->private;
1585 switch (trav->class) {
1586 case MTTG_TRAV_INIT:
1587 trav->class = MTTG_TRAV_NFP_UNSPEC;
1588 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1589 trav->head = trav->curr = is_target ?
1590 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1592 case MTTG_TRAV_NFP_UNSPEC:
1593 trav->curr = trav->curr->next;
1594 if (trav->curr != trav->head)
1596 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1597 mutex_lock(&xt[trav->nfproto].mutex);
1598 trav->head = trav->curr = is_target ?
1599 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1600 trav->class = next_class[trav->class];
1602 case MTTG_TRAV_NFP_SPEC:
1603 trav->curr = trav->curr->next;
1604 if (trav->curr != trav->head)
1616 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1619 struct nf_mttg_trav *trav = seq->private;
1622 trav->class = MTTG_TRAV_INIT;
1623 for (j = 0; j < *pos; ++j)
1624 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1629 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1631 struct nf_mttg_trav *trav = seq->private;
1633 switch (trav->class) {
1634 case MTTG_TRAV_NFP_UNSPEC:
1635 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1637 case MTTG_TRAV_NFP_SPEC:
1638 mutex_unlock(&xt[trav->nfproto].mutex);
1643 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1645 return xt_mttg_seq_start(seq, pos, false);
1648 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1650 return xt_mttg_seq_next(seq, v, ppos, false);
1653 static int xt_match_seq_show(struct seq_file *seq, void *v)
1655 const struct nf_mttg_trav *trav = seq->private;
1656 const struct xt_match *match;
1658 switch (trav->class) {
1659 case MTTG_TRAV_NFP_UNSPEC:
1660 case MTTG_TRAV_NFP_SPEC:
1661 if (trav->curr == trav->head)
1663 match = list_entry(trav->curr, struct xt_match, list);
1665 seq_printf(seq, "%s\n", match->name);
1670 static const struct seq_operations xt_match_seq_ops = {
1671 .start = xt_match_seq_start,
1672 .next = xt_match_seq_next,
1673 .stop = xt_mttg_seq_stop,
1674 .show = xt_match_seq_show,
1677 static int xt_match_open(struct inode *inode, struct file *file)
1679 struct nf_mttg_trav *trav;
1680 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1684 trav->nfproto = (unsigned long)PDE_DATA(inode);
1688 static const struct file_operations xt_match_ops = {
1689 .open = xt_match_open,
1691 .llseek = seq_lseek,
1692 .release = seq_release_private,
1695 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1697 return xt_mttg_seq_start(seq, pos, true);
1700 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1702 return xt_mttg_seq_next(seq, v, ppos, true);
1705 static int xt_target_seq_show(struct seq_file *seq, void *v)
1707 const struct nf_mttg_trav *trav = seq->private;
1708 const struct xt_target *target;
1710 switch (trav->class) {
1711 case MTTG_TRAV_NFP_UNSPEC:
1712 case MTTG_TRAV_NFP_SPEC:
1713 if (trav->curr == trav->head)
1715 target = list_entry(trav->curr, struct xt_target, list);
1717 seq_printf(seq, "%s\n", target->name);
1722 static const struct seq_operations xt_target_seq_ops = {
1723 .start = xt_target_seq_start,
1724 .next = xt_target_seq_next,
1725 .stop = xt_mttg_seq_stop,
1726 .show = xt_target_seq_show,
1729 static int xt_target_open(struct inode *inode, struct file *file)
1731 struct nf_mttg_trav *trav;
1732 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1736 trav->nfproto = (unsigned long)PDE_DATA(inode);
1740 static const struct file_operations xt_target_ops = {
1741 .open = xt_target_open,
1743 .llseek = seq_lseek,
1744 .release = seq_release_private,
1747 #define FORMAT_TABLES "_tables_names"
1748 #define FORMAT_MATCHES "_tables_matches"
1749 #define FORMAT_TARGETS "_tables_targets"
1751 #endif /* CONFIG_PROC_FS */
1754 * xt_hook_ops_alloc - set up hooks for a new table
1755 * @table: table with metadata needed to set up hooks
1756 * @fn: Hook function
1758 * This function will create the nf_hook_ops that the x_table needs
1759 * to hand to xt_hook_link_net().
1761 struct nf_hook_ops *
1762 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1764 unsigned int hook_mask = table->valid_hooks;
1765 uint8_t i, num_hooks = hweight32(hook_mask);
1767 struct nf_hook_ops *ops;
1770 return ERR_PTR(-EINVAL);
1772 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1774 return ERR_PTR(-ENOMEM);
1776 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1777 hook_mask >>= 1, ++hooknum) {
1778 if (!(hook_mask & 1))
1781 ops[i].pf = table->af;
1782 ops[i].hooknum = hooknum;
1783 ops[i].priority = table->priority;
1789 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1791 int xt_proto_init(struct net *net, u_int8_t af)
1793 #ifdef CONFIG_PROC_FS
1794 char buf[XT_FUNCTION_MAXNAMELEN];
1795 struct proc_dir_entry *proc;
1800 if (af >= ARRAY_SIZE(xt_prefix))
1804 #ifdef CONFIG_PROC_FS
1805 root_uid = make_kuid(net->user_ns, 0);
1806 root_gid = make_kgid(net->user_ns, 0);
1808 strlcpy(buf, xt_prefix[af], sizeof(buf));
1809 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1810 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1811 (void *)(unsigned long)af);
1814 if (uid_valid(root_uid) && gid_valid(root_gid))
1815 proc_set_user(proc, root_uid, root_gid);
1817 strlcpy(buf, xt_prefix[af], sizeof(buf));
1818 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1819 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1820 (void *)(unsigned long)af);
1822 goto out_remove_tables;
1823 if (uid_valid(root_uid) && gid_valid(root_gid))
1824 proc_set_user(proc, root_uid, root_gid);
1826 strlcpy(buf, xt_prefix[af], sizeof(buf));
1827 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1828 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1829 (void *)(unsigned long)af);
1831 goto out_remove_matches;
1832 if (uid_valid(root_uid) && gid_valid(root_gid))
1833 proc_set_user(proc, root_uid, root_gid);
1838 #ifdef CONFIG_PROC_FS
1840 strlcpy(buf, xt_prefix[af], sizeof(buf));
1841 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1842 remove_proc_entry(buf, net->proc_net);
1845 strlcpy(buf, xt_prefix[af], sizeof(buf));
1846 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1847 remove_proc_entry(buf, net->proc_net);
1852 EXPORT_SYMBOL_GPL(xt_proto_init);
1854 void xt_proto_fini(struct net *net, u_int8_t af)
1856 #ifdef CONFIG_PROC_FS
1857 char buf[XT_FUNCTION_MAXNAMELEN];
1859 strlcpy(buf, xt_prefix[af], sizeof(buf));
1860 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1861 remove_proc_entry(buf, net->proc_net);
1863 strlcpy(buf, xt_prefix[af], sizeof(buf));
1864 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1865 remove_proc_entry(buf, net->proc_net);
1867 strlcpy(buf, xt_prefix[af], sizeof(buf));
1868 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1869 remove_proc_entry(buf, net->proc_net);
1870 #endif /*CONFIG_PROC_FS*/
1872 EXPORT_SYMBOL_GPL(xt_proto_fini);
1875 * xt_percpu_counter_alloc - allocate x_tables rule counter
1877 * @state: pointer to xt_percpu allocation state
1878 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1880 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1881 * contain the address of the real (percpu) counter.
1883 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1884 * to fetch the real percpu counter.
1886 * To speed up allocation and improve data locality, a 4kb block is
1887 * allocated. Freeing any counter may free an entire block, so all
1888 * counters allocated using the same state must be freed at the same
1891 * xt_percpu_counter_alloc_state contains the base address of the
1892 * allocated page and the current sub-offset.
1894 * returns false on error.
1896 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1897 struct xt_counters *counter)
1899 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1901 if (nr_cpu_ids <= 1)
1905 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1906 XT_PCPU_BLOCK_SIZE);
1910 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1911 state->off += sizeof(*counter);
1912 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1918 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1920 void xt_percpu_counter_free(struct xt_counters *counters)
1922 unsigned long pcnt = counters->pcnt;
1924 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1925 free_percpu((void __percpu *)pcnt);
1927 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1929 static int __net_init xt_net_init(struct net *net)
1933 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1934 INIT_LIST_HEAD(&net->xt.tables[i]);
1938 static void __net_exit xt_net_exit(struct net *net)
1942 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1943 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1946 static struct pernet_operations xt_net_ops = {
1947 .init = xt_net_init,
1948 .exit = xt_net_exit,
1951 static int __init xt_init(void)
1956 for_each_possible_cpu(i) {
1957 seqcount_init(&per_cpu(xt_recseq, i));
1960 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1964 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1965 mutex_init(&xt[i].mutex);
1966 #ifdef CONFIG_COMPAT
1967 mutex_init(&xt[i].compat_mutex);
1968 xt[i].compat_tab = NULL;
1970 INIT_LIST_HEAD(&xt[i].target);
1971 INIT_LIST_HEAD(&xt[i].match);
1973 rv = register_pernet_subsys(&xt_net_ops);
1979 static void __exit xt_fini(void)
1981 unregister_pernet_subsys(&xt_net_ops);
1985 module_init(xt_init);
1986 module_exit(xt_fini);