1 // SPDX-License-Identifier: GPL-2.0-only
3 * x_tables core - Backend for {ip,ip6,arp}_tables
5 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
6 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
8 * Based on existing ip_tables code which is
9 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
10 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/socket.h>
16 #include <linux/net.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/string.h>
20 #include <linux/vmalloc.h>
21 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/audit.h>
25 #include <linux/user_namespace.h>
26 #include <net/net_namespace.h>
28 #include <linux/netfilter/x_tables.h>
29 #include <linux/netfilter_arp.h>
30 #include <linux/netfilter_ipv4/ip_tables.h>
31 #include <linux/netfilter_ipv6/ip6_tables.h>
32 #include <linux/netfilter_arp/arp_tables.h>
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
36 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
38 #define XT_PCPU_BLOCK_SIZE 4096
39 #define XT_MAX_TABLE_SIZE (512 * 1024 * 1024)
42 unsigned int offset; /* offset in kernel */
43 int delta; /* delta in 32bit user land */
48 struct list_head match;
49 struct list_head target;
51 struct mutex compat_mutex;
52 struct compat_delta *compat_tab;
53 unsigned int number; /* number of slots in compat_tab[] */
54 unsigned int cur; /* number of used slots in compat_tab[] */
58 static struct xt_af *xt;
60 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
61 [NFPROTO_UNSPEC] = "x",
62 [NFPROTO_IPV4] = "ip",
63 [NFPROTO_ARP] = "arp",
64 [NFPROTO_BRIDGE] = "eb",
65 [NFPROTO_IPV6] = "ip6",
68 /* Registration hooks for targets. */
69 int xt_register_target(struct xt_target *target)
71 u_int8_t af = target->family;
73 mutex_lock(&xt[af].mutex);
74 list_add(&target->list, &xt[af].target);
75 mutex_unlock(&xt[af].mutex);
78 EXPORT_SYMBOL(xt_register_target);
81 xt_unregister_target(struct xt_target *target)
83 u_int8_t af = target->family;
85 mutex_lock(&xt[af].mutex);
86 list_del(&target->list);
87 mutex_unlock(&xt[af].mutex);
89 EXPORT_SYMBOL(xt_unregister_target);
92 xt_register_targets(struct xt_target *target, unsigned int n)
97 for (i = 0; i < n; i++) {
98 err = xt_register_target(&target[i]);
106 xt_unregister_targets(target, i);
109 EXPORT_SYMBOL(xt_register_targets);
112 xt_unregister_targets(struct xt_target *target, unsigned int n)
115 xt_unregister_target(&target[n]);
117 EXPORT_SYMBOL(xt_unregister_targets);
119 int xt_register_match(struct xt_match *match)
121 u_int8_t af = match->family;
123 mutex_lock(&xt[af].mutex);
124 list_add(&match->list, &xt[af].match);
125 mutex_unlock(&xt[af].mutex);
128 EXPORT_SYMBOL(xt_register_match);
131 xt_unregister_match(struct xt_match *match)
133 u_int8_t af = match->family;
135 mutex_lock(&xt[af].mutex);
136 list_del(&match->list);
137 mutex_unlock(&xt[af].mutex);
139 EXPORT_SYMBOL(xt_unregister_match);
142 xt_register_matches(struct xt_match *match, unsigned int n)
147 for (i = 0; i < n; i++) {
148 err = xt_register_match(&match[i]);
156 xt_unregister_matches(match, i);
159 EXPORT_SYMBOL(xt_register_matches);
162 xt_unregister_matches(struct xt_match *match, unsigned int n)
165 xt_unregister_match(&match[n]);
167 EXPORT_SYMBOL(xt_unregister_matches);
171 * These are weird, but module loading must not be done with mutex
172 * held (since they will register), and we have to have a single
176 /* Find match, grabs ref. Returns ERR_PTR() on error. */
177 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
182 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
183 return ERR_PTR(-EINVAL);
185 mutex_lock(&xt[af].mutex);
186 list_for_each_entry(m, &xt[af].match, list) {
187 if (strcmp(m->name, name) == 0) {
188 if (m->revision == revision) {
189 if (try_module_get(m->me)) {
190 mutex_unlock(&xt[af].mutex);
194 err = -EPROTOTYPE; /* Found something. */
197 mutex_unlock(&xt[af].mutex);
199 if (af != NFPROTO_UNSPEC)
200 /* Try searching again in the family-independent list */
201 return xt_find_match(NFPROTO_UNSPEC, name, revision);
205 EXPORT_SYMBOL(xt_find_match);
208 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
210 struct xt_match *match;
212 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
213 return ERR_PTR(-EINVAL);
215 match = xt_find_match(nfproto, name, revision);
217 request_module("%st_%s", xt_prefix[nfproto], name);
218 match = xt_find_match(nfproto, name, revision);
223 EXPORT_SYMBOL_GPL(xt_request_find_match);
225 /* Find target, grabs ref. Returns ERR_PTR() on error. */
226 static struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
231 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
232 return ERR_PTR(-EINVAL);
234 mutex_lock(&xt[af].mutex);
235 list_for_each_entry(t, &xt[af].target, list) {
236 if (strcmp(t->name, name) == 0) {
237 if (t->revision == revision) {
238 if (try_module_get(t->me)) {
239 mutex_unlock(&xt[af].mutex);
243 err = -EPROTOTYPE; /* Found something. */
246 mutex_unlock(&xt[af].mutex);
248 if (af != NFPROTO_UNSPEC)
249 /* Try searching again in the family-independent list */
250 return xt_find_target(NFPROTO_UNSPEC, name, revision);
255 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
257 struct xt_target *target;
259 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
260 return ERR_PTR(-EINVAL);
262 target = xt_find_target(af, name, revision);
263 if (IS_ERR(target)) {
264 request_module("%st_%s", xt_prefix[af], name);
265 target = xt_find_target(af, name, revision);
270 EXPORT_SYMBOL_GPL(xt_request_find_target);
273 static int xt_obj_to_user(u16 __user *psize, u16 size,
274 void __user *pname, const char *name,
275 u8 __user *prev, u8 rev)
277 if (put_user(size, psize))
279 if (copy_to_user(pname, name, strlen(name) + 1))
281 if (put_user(rev, prev))
287 #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
288 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
289 U->u.user.name, K->u.kernel.TYPE->name, \
290 &U->u.user.revision, K->u.kernel.TYPE->revision)
292 int xt_data_to_user(void __user *dst, const void *src,
293 int usersize, int size, int aligned_size)
295 usersize = usersize ? : size;
296 if (copy_to_user(dst, src, usersize))
298 if (usersize != aligned_size &&
299 clear_user(dst + usersize, aligned_size - usersize))
304 EXPORT_SYMBOL_GPL(xt_data_to_user);
306 #define XT_DATA_TO_USER(U, K, TYPE) \
307 xt_data_to_user(U->data, K->data, \
308 K->u.kernel.TYPE->usersize, \
309 K->u.kernel.TYPE->TYPE##size, \
310 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
312 int xt_match_to_user(const struct xt_entry_match *m,
313 struct xt_entry_match __user *u)
315 return XT_OBJ_TO_USER(u, m, match, 0) ||
316 XT_DATA_TO_USER(u, m, match);
318 EXPORT_SYMBOL_GPL(xt_match_to_user);
320 int xt_target_to_user(const struct xt_entry_target *t,
321 struct xt_entry_target __user *u)
323 return XT_OBJ_TO_USER(u, t, target, 0) ||
324 XT_DATA_TO_USER(u, t, target);
326 EXPORT_SYMBOL_GPL(xt_target_to_user);
328 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
330 const struct xt_match *m;
333 mutex_lock(&xt[af].mutex);
334 list_for_each_entry(m, &xt[af].match, list) {
335 if (strcmp(m->name, name) == 0) {
336 if (m->revision > *bestp)
337 *bestp = m->revision;
338 if (m->revision == revision)
342 mutex_unlock(&xt[af].mutex);
344 if (af != NFPROTO_UNSPEC && !have_rev)
345 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
350 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
352 const struct xt_target *t;
355 mutex_lock(&xt[af].mutex);
356 list_for_each_entry(t, &xt[af].target, list) {
357 if (strcmp(t->name, name) == 0) {
358 if (t->revision > *bestp)
359 *bestp = t->revision;
360 if (t->revision == revision)
364 mutex_unlock(&xt[af].mutex);
366 if (af != NFPROTO_UNSPEC && !have_rev)
367 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
372 /* Returns true or false (if no such extension at all) */
373 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
376 int have_rev, best = -1;
379 have_rev = target_revfn(af, name, revision, &best);
381 have_rev = match_revfn(af, name, revision, &best);
383 /* Nothing at all? Return 0 to try loading module. */
391 *err = -EPROTONOSUPPORT;
394 EXPORT_SYMBOL_GPL(xt_find_revision);
397 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
399 static const char *const inetbr_names[] = {
400 "PREROUTING", "INPUT", "FORWARD",
401 "OUTPUT", "POSTROUTING", "BROUTING",
403 static const char *const arp_names[] = {
404 "INPUT", "FORWARD", "OUTPUT",
406 const char *const *names;
412 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
413 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
414 ARRAY_SIZE(inetbr_names);
416 for (i = 0; i < max; ++i) {
417 if (!(mask & (1 << i)))
419 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
431 * xt_check_proc_name - check that name is suitable for /proc file creation
433 * @name: file name candidate
434 * @size: length of buffer
436 * some x_tables modules wish to create a file in /proc.
437 * This function makes sure that the name is suitable for this
438 * purpose, it checks that name is NUL terminated and isn't a 'special'
441 * returns negative number on error or 0 if name is useable.
443 int xt_check_proc_name(const char *name, unsigned int size)
448 if (strnlen(name, size) == size)
449 return -ENAMETOOLONG;
451 if (strcmp(name, ".") == 0 ||
452 strcmp(name, "..") == 0 ||
458 EXPORT_SYMBOL(xt_check_proc_name);
460 int xt_check_match(struct xt_mtchk_param *par,
461 unsigned int size, u16 proto, bool inv_proto)
465 if (XT_ALIGN(par->match->matchsize) != size &&
466 par->match->matchsize != -1) {
468 * ebt_among is exempt from centralized matchsize checking
469 * because it uses a dynamic-size data set.
471 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
472 xt_prefix[par->family], par->match->name,
473 par->match->revision,
474 XT_ALIGN(par->match->matchsize), size);
477 if (par->match->table != NULL &&
478 strcmp(par->match->table, par->table) != 0) {
479 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
480 xt_prefix[par->family], par->match->name,
481 par->match->table, par->table);
484 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
485 char used[64], allow[64];
487 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
488 xt_prefix[par->family], par->match->name,
489 textify_hooks(used, sizeof(used),
490 par->hook_mask, par->family),
491 textify_hooks(allow, sizeof(allow),
496 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
497 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
498 xt_prefix[par->family], par->match->name,
502 if (par->match->checkentry != NULL) {
503 ret = par->match->checkentry(par);
507 /* Flag up potential errors. */
512 EXPORT_SYMBOL_GPL(xt_check_match);
514 /** xt_check_entry_match - check that matches end before start of target
516 * @match: beginning of xt_entry_match
517 * @target: beginning of this rules target (alleged end of matches)
518 * @alignment: alignment requirement of match structures
520 * Validates that all matches add up to the beginning of the target,
521 * and that each match covers at least the base structure size.
523 * Return: 0 on success, negative errno on failure.
525 static int xt_check_entry_match(const char *match, const char *target,
526 const size_t alignment)
528 const struct xt_entry_match *pos;
529 int length = target - match;
531 if (length == 0) /* no matches */
534 pos = (struct xt_entry_match *)match;
536 if ((unsigned long)pos % alignment)
539 if (length < (int)sizeof(struct xt_entry_match))
542 if (pos->u.match_size < sizeof(struct xt_entry_match))
545 if (pos->u.match_size > length)
548 length -= pos->u.match_size;
549 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
550 } while (length > 0);
555 /** xt_check_table_hooks - check hook entry points are sane
557 * @info xt_table_info to check
558 * @valid_hooks - hook entry points that we can enter from
560 * Validates that the hook entry and underflows points are set up.
562 * Return: 0 on success, negative errno on failure.
564 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
566 const char *err = "unsorted underflow";
567 unsigned int i, max_uflow, max_entry;
568 bool check_hooks = false;
570 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
575 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
576 if (!(valid_hooks & (1 << i)))
579 if (info->hook_entry[i] == 0xFFFFFFFF)
581 if (info->underflow[i] == 0xFFFFFFFF)
585 if (max_uflow > info->underflow[i])
588 if (max_uflow == info->underflow[i]) {
589 err = "duplicate underflow";
592 if (max_entry > info->hook_entry[i]) {
593 err = "unsorted entry";
596 if (max_entry == info->hook_entry[i]) {
597 err = "duplicate entry";
601 max_entry = info->hook_entry[i];
602 max_uflow = info->underflow[i];
608 pr_err_ratelimited("%s at hook %d\n", err, i);
611 EXPORT_SYMBOL(xt_check_table_hooks);
613 static bool verdict_ok(int verdict)
619 int v = -verdict - 1;
621 if (verdict == XT_RETURN)
625 case NF_ACCEPT: return true;
626 case NF_DROP: return true;
627 case NF_QUEUE: return true;
638 static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
639 const char *msg, unsigned int msglen)
641 return usersize == kernsize && strnlen(msg, msglen) < msglen;
645 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
647 struct xt_af *xp = &xt[af];
649 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
651 if (WARN_ON(!xp->compat_tab))
654 if (xp->cur >= xp->number)
658 delta += xp->compat_tab[xp->cur - 1].delta;
659 xp->compat_tab[xp->cur].offset = offset;
660 xp->compat_tab[xp->cur].delta = delta;
664 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
666 void xt_compat_flush_offsets(u_int8_t af)
668 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
670 if (xt[af].compat_tab) {
671 vfree(xt[af].compat_tab);
672 xt[af].compat_tab = NULL;
677 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
679 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
681 struct compat_delta *tmp = xt[af].compat_tab;
682 int mid, left = 0, right = xt[af].cur - 1;
684 while (left <= right) {
685 mid = (left + right) >> 1;
686 if (offset > tmp[mid].offset)
688 else if (offset < tmp[mid].offset)
691 return mid ? tmp[mid - 1].delta : 0;
693 return left ? tmp[left - 1].delta : 0;
695 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
697 int xt_compat_init_offsets(u8 af, unsigned int number)
701 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
703 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
706 if (WARN_ON(xt[af].compat_tab))
709 mem = sizeof(struct compat_delta) * number;
710 if (mem > XT_MAX_TABLE_SIZE)
713 xt[af].compat_tab = vmalloc(mem);
714 if (!xt[af].compat_tab)
717 xt[af].number = number;
722 EXPORT_SYMBOL(xt_compat_init_offsets);
724 int xt_compat_match_offset(const struct xt_match *match)
726 u_int16_t csize = match->compatsize ? : match->matchsize;
727 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
729 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
731 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
734 const struct xt_match *match = m->u.kernel.match;
735 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
736 int pad, off = xt_compat_match_offset(match);
737 u_int16_t msize = cm->u.user.match_size;
738 char name[sizeof(m->u.user.name)];
741 memcpy(m, cm, sizeof(*cm));
742 if (match->compat_from_user)
743 match->compat_from_user(m->data, cm->data);
745 memcpy(m->data, cm->data, msize - sizeof(*cm));
746 pad = XT_ALIGN(match->matchsize) - match->matchsize;
748 memset(m->data + match->matchsize, 0, pad);
751 m->u.user.match_size = msize;
752 strlcpy(name, match->name, sizeof(name));
753 module_put(match->me);
754 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
759 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
761 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
762 xt_data_to_user(U->data, K->data, \
763 K->u.kernel.TYPE->usersize, \
765 COMPAT_XT_ALIGN(C_SIZE))
767 int xt_compat_match_to_user(const struct xt_entry_match *m,
768 void __user **dstptr, unsigned int *size)
770 const struct xt_match *match = m->u.kernel.match;
771 struct compat_xt_entry_match __user *cm = *dstptr;
772 int off = xt_compat_match_offset(match);
773 u_int16_t msize = m->u.user.match_size - off;
775 if (XT_OBJ_TO_USER(cm, m, match, msize))
778 if (match->compat_to_user) {
779 if (match->compat_to_user((void __user *)cm->data, m->data))
782 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
790 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
792 /* non-compat version may have padding after verdict */
793 struct compat_xt_standard_target {
794 struct compat_xt_entry_target t;
795 compat_uint_t verdict;
798 struct compat_xt_error_target {
799 struct compat_xt_entry_target t;
800 char errorname[XT_FUNCTION_MAXNAMELEN];
803 int xt_compat_check_entry_offsets(const void *base, const char *elems,
804 unsigned int target_offset,
805 unsigned int next_offset)
807 long size_of_base_struct = elems - (const char *)base;
808 const struct compat_xt_entry_target *t;
809 const char *e = base;
811 if (target_offset < size_of_base_struct)
814 if (target_offset + sizeof(*t) > next_offset)
817 t = (void *)(e + target_offset);
818 if (t->u.target_size < sizeof(*t))
821 if (target_offset + t->u.target_size > next_offset)
824 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
825 const struct compat_xt_standard_target *st = (const void *)t;
827 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
830 if (!verdict_ok(st->verdict))
832 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
833 const struct compat_xt_error_target *et = (const void *)t;
835 if (!error_tg_ok(t->u.target_size, sizeof(*et),
836 et->errorname, sizeof(et->errorname)))
840 /* compat_xt_entry match has less strict alignment requirements,
841 * otherwise they are identical. In case of padding differences
842 * we need to add compat version of xt_check_entry_match.
844 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
846 return xt_check_entry_match(elems, base + target_offset,
847 __alignof__(struct compat_xt_entry_match));
849 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
850 #endif /* CONFIG_COMPAT */
853 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
855 * @base: pointer to arp/ip/ip6t_entry
856 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
857 * @target_offset: the arp/ip/ip6_t->target_offset
858 * @next_offset: the arp/ip/ip6_t->next_offset
860 * validates that target_offset and next_offset are sane and that all
861 * match sizes (if any) align with the target offset.
863 * This function does not validate the targets or matches themselves, it
864 * only tests that all the offsets and sizes are correct, that all
865 * match structures are aligned, and that the last structure ends where
866 * the target structure begins.
868 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
870 * The arp/ip/ip6t_entry structure @base must have passed following tests:
871 * - it must point to a valid memory location
872 * - base to base + next_offset must be accessible, i.e. not exceed allocated
875 * A well-formed entry looks like this:
877 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
878 * e->elems[]-----' | |
882 * target_offset---------------------------------' |
883 * next_offset---------------------------------------------------'
885 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
886 * This is where matches (if any) and the target reside.
887 * target_offset: beginning of target.
888 * next_offset: start of the next rule; also: size of this rule.
889 * Since targets have a minimum size, target_offset + minlen <= next_offset.
891 * Every match stores its size, sum of sizes must not exceed target_offset.
893 * Return: 0 on success, negative errno on failure.
895 int xt_check_entry_offsets(const void *base,
897 unsigned int target_offset,
898 unsigned int next_offset)
900 long size_of_base_struct = elems - (const char *)base;
901 const struct xt_entry_target *t;
902 const char *e = base;
904 /* target start is within the ip/ip6/arpt_entry struct */
905 if (target_offset < size_of_base_struct)
908 if (target_offset + sizeof(*t) > next_offset)
911 t = (void *)(e + target_offset);
912 if (t->u.target_size < sizeof(*t))
915 if (target_offset + t->u.target_size > next_offset)
918 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
919 const struct xt_standard_target *st = (const void *)t;
921 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
924 if (!verdict_ok(st->verdict))
926 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
927 const struct xt_error_target *et = (const void *)t;
929 if (!error_tg_ok(t->u.target_size, sizeof(*et),
930 et->errorname, sizeof(et->errorname)))
934 return xt_check_entry_match(elems, base + target_offset,
935 __alignof__(struct xt_entry_match));
937 EXPORT_SYMBOL(xt_check_entry_offsets);
940 * xt_alloc_entry_offsets - allocate array to store rule head offsets
942 * @size: number of entries
944 * Return: NULL or zeroed kmalloc'd or vmalloc'd array
946 unsigned int *xt_alloc_entry_offsets(unsigned int size)
948 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
951 return kvcalloc(size, sizeof(unsigned int), GFP_KERNEL);
954 EXPORT_SYMBOL(xt_alloc_entry_offsets);
957 * xt_find_jump_offset - check if target is a valid jump offset
959 * @offsets: array containing all valid rule start offsets of a rule blob
960 * @target: the jump target to search for
961 * @size: entries in @offset
963 bool xt_find_jump_offset(const unsigned int *offsets,
964 unsigned int target, unsigned int size)
966 int m, low = 0, hi = size;
971 if (offsets[m] > target)
973 else if (offsets[m] < target)
981 EXPORT_SYMBOL(xt_find_jump_offset);
983 int xt_check_target(struct xt_tgchk_param *par,
984 unsigned int size, u16 proto, bool inv_proto)
988 if (XT_ALIGN(par->target->targetsize) != size) {
989 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
990 xt_prefix[par->family], par->target->name,
991 par->target->revision,
992 XT_ALIGN(par->target->targetsize), size);
995 if (par->target->table != NULL &&
996 strcmp(par->target->table, par->table) != 0) {
997 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
998 xt_prefix[par->family], par->target->name,
999 par->target->table, par->table);
1002 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1003 char used[64], allow[64];
1005 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1006 xt_prefix[par->family], par->target->name,
1007 textify_hooks(used, sizeof(used),
1008 par->hook_mask, par->family),
1009 textify_hooks(allow, sizeof(allow),
1014 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1015 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1016 xt_prefix[par->family], par->target->name,
1017 par->target->proto);
1020 if (par->target->checkentry != NULL) {
1021 ret = par->target->checkentry(par);
1025 /* Flag up potential errors. */
1030 EXPORT_SYMBOL_GPL(xt_check_target);
1033 * xt_copy_counters - copy counters and metadata from a sockptr_t
1036 * @len: alleged size of userspace memory
1037 * @info: where to store the xt_counters_info metadata
1039 * Copies counter meta data from @user and stores it in @info.
1041 * vmallocs memory to hold the counters, then copies the counter data
1042 * from @user to the new memory and returns a pointer to it.
1044 * If called from a compat syscall, @info gets converted automatically to the
1045 * 64bit representation.
1047 * The metadata associated with the counters is stored in @info.
1049 * Return: returns pointer that caller has to test via IS_ERR().
1050 * If IS_ERR is false, caller has to vfree the pointer.
1052 void *xt_copy_counters(sockptr_t arg, unsigned int len,
1053 struct xt_counters_info *info)
1059 #ifdef CONFIG_COMPAT
1060 if (in_compat_syscall()) {
1061 /* structures only differ in size due to alignment */
1062 struct compat_xt_counters_info compat_tmp;
1064 if (len <= sizeof(compat_tmp))
1065 return ERR_PTR(-EINVAL);
1067 len -= sizeof(compat_tmp);
1068 if (copy_from_sockptr(&compat_tmp, arg, sizeof(compat_tmp)) != 0)
1069 return ERR_PTR(-EFAULT);
1071 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1072 info->num_counters = compat_tmp.num_counters;
1073 offset = sizeof(compat_tmp);
1077 if (len <= sizeof(*info))
1078 return ERR_PTR(-EINVAL);
1080 len -= sizeof(*info);
1081 if (copy_from_sockptr(info, arg, sizeof(*info)) != 0)
1082 return ERR_PTR(-EFAULT);
1084 offset = sizeof(*info);
1086 info->name[sizeof(info->name) - 1] = '\0';
1088 size = sizeof(struct xt_counters);
1089 size *= info->num_counters;
1091 if (size != (u64)len)
1092 return ERR_PTR(-EINVAL);
1096 return ERR_PTR(-ENOMEM);
1098 if (copy_from_sockptr_offset(mem, arg, offset, len) == 0)
1102 return ERR_PTR(-EFAULT);
1104 EXPORT_SYMBOL_GPL(xt_copy_counters);
1106 #ifdef CONFIG_COMPAT
1107 int xt_compat_target_offset(const struct xt_target *target)
1109 u_int16_t csize = target->compatsize ? : target->targetsize;
1110 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1112 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1114 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1117 const struct xt_target *target = t->u.kernel.target;
1118 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1119 int pad, off = xt_compat_target_offset(target);
1120 u_int16_t tsize = ct->u.user.target_size;
1121 char name[sizeof(t->u.user.name)];
1124 memcpy(t, ct, sizeof(*ct));
1125 if (target->compat_from_user)
1126 target->compat_from_user(t->data, ct->data);
1128 memcpy(t->data, ct->data, tsize - sizeof(*ct));
1129 pad = XT_ALIGN(target->targetsize) - target->targetsize;
1131 memset(t->data + target->targetsize, 0, pad);
1134 t->u.user.target_size = tsize;
1135 strlcpy(name, target->name, sizeof(name));
1136 module_put(target->me);
1137 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
1142 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1144 int xt_compat_target_to_user(const struct xt_entry_target *t,
1145 void __user **dstptr, unsigned int *size)
1147 const struct xt_target *target = t->u.kernel.target;
1148 struct compat_xt_entry_target __user *ct = *dstptr;
1149 int off = xt_compat_target_offset(target);
1150 u_int16_t tsize = t->u.user.target_size - off;
1152 if (XT_OBJ_TO_USER(ct, t, target, tsize))
1155 if (target->compat_to_user) {
1156 if (target->compat_to_user((void __user *)ct->data, t->data))
1159 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1167 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1170 struct xt_table_info *xt_alloc_table_info(unsigned int size)
1172 struct xt_table_info *info = NULL;
1173 size_t sz = sizeof(*info) + size;
1175 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1178 info = kvmalloc(sz, GFP_KERNEL_ACCOUNT);
1182 memset(info, 0, sizeof(*info));
1186 EXPORT_SYMBOL(xt_alloc_table_info);
1188 void xt_free_table_info(struct xt_table_info *info)
1192 if (info->jumpstack != NULL) {
1193 for_each_possible_cpu(cpu)
1194 kvfree(info->jumpstack[cpu]);
1195 kvfree(info->jumpstack);
1200 EXPORT_SYMBOL(xt_free_table_info);
1202 /* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
1203 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1206 struct xt_table *t, *found = NULL;
1208 mutex_lock(&xt[af].mutex);
1209 list_for_each_entry(t, &net->xt.tables[af], list)
1210 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1213 if (net == &init_net)
1216 /* Table doesn't exist in this netns, re-try init */
1217 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1220 if (strcmp(t->name, name))
1222 if (!try_module_get(t->me))
1224 mutex_unlock(&xt[af].mutex);
1225 err = t->table_init(net);
1228 return ERR_PTR(err);
1233 mutex_lock(&xt[af].mutex);
1240 /* and once again: */
1241 list_for_each_entry(t, &net->xt.tables[af], list)
1242 if (strcmp(t->name, name) == 0)
1245 module_put(found->me);
1247 mutex_unlock(&xt[af].mutex);
1248 return ERR_PTR(-ENOENT);
1250 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1252 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1255 struct xt_table *t = xt_find_table_lock(net, af, name);
1257 #ifdef CONFIG_MODULES
1259 int err = request_module("%stable_%s", xt_prefix[af], name);
1261 return ERR_PTR(err);
1262 t = xt_find_table_lock(net, af, name);
1268 EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1270 void xt_table_unlock(struct xt_table *table)
1272 mutex_unlock(&xt[table->af].mutex);
1274 EXPORT_SYMBOL_GPL(xt_table_unlock);
1276 #ifdef CONFIG_COMPAT
1277 void xt_compat_lock(u_int8_t af)
1279 mutex_lock(&xt[af].compat_mutex);
1281 EXPORT_SYMBOL_GPL(xt_compat_lock);
1283 void xt_compat_unlock(u_int8_t af)
1285 mutex_unlock(&xt[af].compat_mutex);
1287 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1290 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1291 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1293 struct static_key xt_tee_enabled __read_mostly;
1294 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1296 static int xt_jumpstack_alloc(struct xt_table_info *i)
1301 size = sizeof(void **) * nr_cpu_ids;
1302 if (size > PAGE_SIZE)
1303 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1305 i->jumpstack = kzalloc(size, GFP_KERNEL);
1306 if (i->jumpstack == NULL)
1309 /* ruleset without jumps -- no stack needed */
1310 if (i->stacksize == 0)
1313 /* Jumpstack needs to be able to record two full callchains, one
1314 * from the first rule set traversal, plus one table reentrancy
1315 * via -j TEE without clobbering the callchain that brought us to
1318 * This is done by allocating two jumpstacks per cpu, on reentry
1319 * the upper half of the stack is used.
1321 * see the jumpstack setup in ipt_do_table() for more details.
1323 size = sizeof(void *) * i->stacksize * 2u;
1324 for_each_possible_cpu(cpu) {
1325 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1327 if (i->jumpstack[cpu] == NULL)
1329 * Freeing will be done later on by the callers. The
1330 * chain is: xt_replace_table -> __do_replace ->
1331 * do_replace -> xt_free_table_info.
1339 struct xt_counters *xt_counters_alloc(unsigned int counters)
1341 struct xt_counters *mem;
1343 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1346 counters *= sizeof(*mem);
1347 if (counters > XT_MAX_TABLE_SIZE)
1350 return vzalloc(counters);
1352 EXPORT_SYMBOL(xt_counters_alloc);
1354 struct xt_table_info *
1355 xt_replace_table(struct xt_table *table,
1356 unsigned int num_counters,
1357 struct xt_table_info *newinfo,
1360 struct xt_table_info *private;
1364 ret = xt_jumpstack_alloc(newinfo);
1370 /* Do the substitution. */
1372 private = table->private;
1374 /* Check inside lock: is the old number correct? */
1375 if (num_counters != private->number) {
1376 pr_debug("num_counters != table->private->number (%u/%u)\n",
1377 num_counters, private->number);
1383 newinfo->initial_entries = private->initial_entries;
1385 * Ensure contents of newinfo are visible before assigning to
1389 table->private = newinfo;
1391 /* make sure all cpus see new ->private value */
1395 * Even though table entries have now been swapped, other CPU's
1396 * may still be using the old entries...
1400 /* ... so wait for even xt_recseq on all cpus */
1401 for_each_possible_cpu(cpu) {
1402 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1403 u32 seq = raw_read_seqcount(s);
1409 } while (seq == raw_read_seqcount(s));
1413 audit_log_nfcfg(table->name, table->af, private->number,
1414 !private->number ? AUDIT_XT_OP_REGISTER :
1415 AUDIT_XT_OP_REPLACE,
1419 EXPORT_SYMBOL_GPL(xt_replace_table);
1421 struct xt_table *xt_register_table(struct net *net,
1422 const struct xt_table *input_table,
1423 struct xt_table_info *bootstrap,
1424 struct xt_table_info *newinfo)
1427 struct xt_table_info *private;
1428 struct xt_table *t, *table;
1430 /* Don't add one object to multiple lists. */
1431 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1437 mutex_lock(&xt[table->af].mutex);
1438 /* Don't autoload: we'd eat our tail... */
1439 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1440 if (strcmp(t->name, table->name) == 0) {
1446 /* Simplifies replace_table code. */
1447 table->private = bootstrap;
1449 if (!xt_replace_table(table, 0, newinfo, &ret))
1452 private = table->private;
1453 pr_debug("table->private->number = %u\n", private->number);
1455 /* save number of initial entries */
1456 private->initial_entries = private->number;
1458 list_add(&table->list, &net->xt.tables[table->af]);
1459 mutex_unlock(&xt[table->af].mutex);
1463 mutex_unlock(&xt[table->af].mutex);
1466 return ERR_PTR(ret);
1468 EXPORT_SYMBOL_GPL(xt_register_table);
1470 void *xt_unregister_table(struct xt_table *table)
1472 struct xt_table_info *private;
1474 mutex_lock(&xt[table->af].mutex);
1475 private = table->private;
1476 list_del(&table->list);
1477 mutex_unlock(&xt[table->af].mutex);
1478 audit_log_nfcfg(table->name, table->af, private->number,
1479 AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
1484 EXPORT_SYMBOL_GPL(xt_unregister_table);
1486 #ifdef CONFIG_PROC_FS
1487 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1489 struct net *net = seq_file_net(seq);
1490 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1492 mutex_lock(&xt[af].mutex);
1493 return seq_list_start(&net->xt.tables[af], *pos);
1496 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1498 struct net *net = seq_file_net(seq);
1499 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1501 return seq_list_next(v, &net->xt.tables[af], pos);
1504 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1506 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1508 mutex_unlock(&xt[af].mutex);
1511 static int xt_table_seq_show(struct seq_file *seq, void *v)
1513 struct xt_table *table = list_entry(v, struct xt_table, list);
1516 seq_printf(seq, "%s\n", table->name);
1520 static const struct seq_operations xt_table_seq_ops = {
1521 .start = xt_table_seq_start,
1522 .next = xt_table_seq_next,
1523 .stop = xt_table_seq_stop,
1524 .show = xt_table_seq_show,
1528 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1529 * the multi-AF mutexes.
1531 struct nf_mttg_trav {
1532 struct list_head *head, *curr;
1538 MTTG_TRAV_NFP_UNSPEC,
1543 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1546 static const uint8_t next_class[] = {
1547 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1548 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1550 uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file));
1551 struct nf_mttg_trav *trav = seq->private;
1556 switch (trav->class) {
1557 case MTTG_TRAV_INIT:
1558 trav->class = MTTG_TRAV_NFP_UNSPEC;
1559 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1560 trav->head = trav->curr = is_target ?
1561 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1563 case MTTG_TRAV_NFP_UNSPEC:
1564 trav->curr = trav->curr->next;
1565 if (trav->curr != trav->head)
1567 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1568 mutex_lock(&xt[nfproto].mutex);
1569 trav->head = trav->curr = is_target ?
1570 &xt[nfproto].target : &xt[nfproto].match;
1571 trav->class = next_class[trav->class];
1573 case MTTG_TRAV_NFP_SPEC:
1574 trav->curr = trav->curr->next;
1575 if (trav->curr != trav->head)
1584 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1587 struct nf_mttg_trav *trav = seq->private;
1590 trav->class = MTTG_TRAV_INIT;
1591 for (j = 0; j < *pos; ++j)
1592 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1597 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1599 uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file));
1600 struct nf_mttg_trav *trav = seq->private;
1602 switch (trav->class) {
1603 case MTTG_TRAV_NFP_UNSPEC:
1604 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1606 case MTTG_TRAV_NFP_SPEC:
1607 mutex_unlock(&xt[nfproto].mutex);
1612 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1614 return xt_mttg_seq_start(seq, pos, false);
1617 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1619 return xt_mttg_seq_next(seq, v, ppos, false);
1622 static int xt_match_seq_show(struct seq_file *seq, void *v)
1624 const struct nf_mttg_trav *trav = seq->private;
1625 const struct xt_match *match;
1627 switch (trav->class) {
1628 case MTTG_TRAV_NFP_UNSPEC:
1629 case MTTG_TRAV_NFP_SPEC:
1630 if (trav->curr == trav->head)
1632 match = list_entry(trav->curr, struct xt_match, list);
1634 seq_printf(seq, "%s\n", match->name);
1639 static const struct seq_operations xt_match_seq_ops = {
1640 .start = xt_match_seq_start,
1641 .next = xt_match_seq_next,
1642 .stop = xt_mttg_seq_stop,
1643 .show = xt_match_seq_show,
1646 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1648 return xt_mttg_seq_start(seq, pos, true);
1651 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1653 return xt_mttg_seq_next(seq, v, ppos, true);
1656 static int xt_target_seq_show(struct seq_file *seq, void *v)
1658 const struct nf_mttg_trav *trav = seq->private;
1659 const struct xt_target *target;
1661 switch (trav->class) {
1662 case MTTG_TRAV_NFP_UNSPEC:
1663 case MTTG_TRAV_NFP_SPEC:
1664 if (trav->curr == trav->head)
1666 target = list_entry(trav->curr, struct xt_target, list);
1668 seq_printf(seq, "%s\n", target->name);
1673 static const struct seq_operations xt_target_seq_ops = {
1674 .start = xt_target_seq_start,
1675 .next = xt_target_seq_next,
1676 .stop = xt_mttg_seq_stop,
1677 .show = xt_target_seq_show,
1680 #define FORMAT_TABLES "_tables_names"
1681 #define FORMAT_MATCHES "_tables_matches"
1682 #define FORMAT_TARGETS "_tables_targets"
1684 #endif /* CONFIG_PROC_FS */
1687 * xt_hook_ops_alloc - set up hooks for a new table
1688 * @table: table with metadata needed to set up hooks
1689 * @fn: Hook function
1691 * This function will create the nf_hook_ops that the x_table needs
1692 * to hand to xt_hook_link_net().
1694 struct nf_hook_ops *
1695 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1697 unsigned int hook_mask = table->valid_hooks;
1698 uint8_t i, num_hooks = hweight32(hook_mask);
1700 struct nf_hook_ops *ops;
1703 return ERR_PTR(-EINVAL);
1705 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1707 return ERR_PTR(-ENOMEM);
1709 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1710 hook_mask >>= 1, ++hooknum) {
1711 if (!(hook_mask & 1))
1714 ops[i].pf = table->af;
1715 ops[i].hooknum = hooknum;
1716 ops[i].priority = table->priority;
1722 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1724 int xt_proto_init(struct net *net, u_int8_t af)
1726 #ifdef CONFIG_PROC_FS
1727 char buf[XT_FUNCTION_MAXNAMELEN];
1728 struct proc_dir_entry *proc;
1733 if (af >= ARRAY_SIZE(xt_prefix))
1737 #ifdef CONFIG_PROC_FS
1738 root_uid = make_kuid(net->user_ns, 0);
1739 root_gid = make_kgid(net->user_ns, 0);
1741 strlcpy(buf, xt_prefix[af], sizeof(buf));
1742 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1743 proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops,
1744 sizeof(struct seq_net_private),
1745 (void *)(unsigned long)af);
1748 if (uid_valid(root_uid) && gid_valid(root_gid))
1749 proc_set_user(proc, root_uid, root_gid);
1751 strlcpy(buf, xt_prefix[af], sizeof(buf));
1752 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1753 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1754 &xt_match_seq_ops, sizeof(struct nf_mttg_trav),
1755 (void *)(unsigned long)af);
1757 goto out_remove_tables;
1758 if (uid_valid(root_uid) && gid_valid(root_gid))
1759 proc_set_user(proc, root_uid, root_gid);
1761 strlcpy(buf, xt_prefix[af], sizeof(buf));
1762 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1763 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1764 &xt_target_seq_ops, sizeof(struct nf_mttg_trav),
1765 (void *)(unsigned long)af);
1767 goto out_remove_matches;
1768 if (uid_valid(root_uid) && gid_valid(root_gid))
1769 proc_set_user(proc, root_uid, root_gid);
1774 #ifdef CONFIG_PROC_FS
1776 strlcpy(buf, xt_prefix[af], sizeof(buf));
1777 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1778 remove_proc_entry(buf, net->proc_net);
1781 strlcpy(buf, xt_prefix[af], sizeof(buf));
1782 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1783 remove_proc_entry(buf, net->proc_net);
1788 EXPORT_SYMBOL_GPL(xt_proto_init);
1790 void xt_proto_fini(struct net *net, u_int8_t af)
1792 #ifdef CONFIG_PROC_FS
1793 char buf[XT_FUNCTION_MAXNAMELEN];
1795 strlcpy(buf, xt_prefix[af], sizeof(buf));
1796 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1797 remove_proc_entry(buf, net->proc_net);
1799 strlcpy(buf, xt_prefix[af], sizeof(buf));
1800 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1801 remove_proc_entry(buf, net->proc_net);
1803 strlcpy(buf, xt_prefix[af], sizeof(buf));
1804 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1805 remove_proc_entry(buf, net->proc_net);
1806 #endif /*CONFIG_PROC_FS*/
1808 EXPORT_SYMBOL_GPL(xt_proto_fini);
1811 * xt_percpu_counter_alloc - allocate x_tables rule counter
1813 * @state: pointer to xt_percpu allocation state
1814 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1816 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1817 * contain the address of the real (percpu) counter.
1819 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1820 * to fetch the real percpu counter.
1822 * To speed up allocation and improve data locality, a 4kb block is
1823 * allocated. Freeing any counter may free an entire block, so all
1824 * counters allocated using the same state must be freed at the same
1827 * xt_percpu_counter_alloc_state contains the base address of the
1828 * allocated page and the current sub-offset.
1830 * returns false on error.
1832 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1833 struct xt_counters *counter)
1835 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1837 if (nr_cpu_ids <= 1)
1841 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1842 XT_PCPU_BLOCK_SIZE);
1846 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1847 state->off += sizeof(*counter);
1848 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1854 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1856 void xt_percpu_counter_free(struct xt_counters *counters)
1858 unsigned long pcnt = counters->pcnt;
1860 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1861 free_percpu((void __percpu *)pcnt);
1863 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1865 static int __net_init xt_net_init(struct net *net)
1869 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1870 INIT_LIST_HEAD(&net->xt.tables[i]);
1874 static void __net_exit xt_net_exit(struct net *net)
1878 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1879 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1882 static struct pernet_operations xt_net_ops = {
1883 .init = xt_net_init,
1884 .exit = xt_net_exit,
1887 static int __init xt_init(void)
1892 for_each_possible_cpu(i) {
1893 seqcount_init(&per_cpu(xt_recseq, i));
1896 xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
1900 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1901 mutex_init(&xt[i].mutex);
1902 #ifdef CONFIG_COMPAT
1903 mutex_init(&xt[i].compat_mutex);
1904 xt[i].compat_tab = NULL;
1906 INIT_LIST_HEAD(&xt[i].target);
1907 INIT_LIST_HEAD(&xt[i].match);
1909 rv = register_pernet_subsys(&xt_net_ops);
1915 static void __exit xt_fini(void)
1917 unregister_pernet_subsys(&xt_net_ops);
1921 module_init(xt_init);
1922 module_exit(xt_fini);