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 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
187 return ERR_PTR(-EINVAL);
189 mutex_lock(&xt[af].mutex);
190 list_for_each_entry(m, &xt[af].match, list) {
191 if (strcmp(m->name, name) == 0) {
192 if (m->revision == revision) {
193 if (try_module_get(m->me)) {
194 mutex_unlock(&xt[af].mutex);
198 err = -EPROTOTYPE; /* Found something. */
201 mutex_unlock(&xt[af].mutex);
203 if (af != NFPROTO_UNSPEC)
204 /* Try searching again in the family-independent list */
205 return xt_find_match(NFPROTO_UNSPEC, name, revision);
209 EXPORT_SYMBOL(xt_find_match);
212 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
214 struct xt_match *match;
216 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
217 return ERR_PTR(-EINVAL);
219 match = xt_find_match(nfproto, name, revision);
221 request_module("%st_%s", xt_prefix[nfproto], name);
222 match = xt_find_match(nfproto, name, revision);
227 EXPORT_SYMBOL_GPL(xt_request_find_match);
229 /* Find target, grabs ref. Returns ERR_PTR() on error. */
230 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
235 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
236 return ERR_PTR(-EINVAL);
238 mutex_lock(&xt[af].mutex);
239 list_for_each_entry(t, &xt[af].target, list) {
240 if (strcmp(t->name, name) == 0) {
241 if (t->revision == revision) {
242 if (try_module_get(t->me)) {
243 mutex_unlock(&xt[af].mutex);
247 err = -EPROTOTYPE; /* Found something. */
250 mutex_unlock(&xt[af].mutex);
252 if (af != NFPROTO_UNSPEC)
253 /* Try searching again in the family-independent list */
254 return xt_find_target(NFPROTO_UNSPEC, name, revision);
258 EXPORT_SYMBOL(xt_find_target);
260 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
262 struct xt_target *target;
264 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
265 return ERR_PTR(-EINVAL);
267 target = xt_find_target(af, name, revision);
268 if (IS_ERR(target)) {
269 request_module("%st_%s", xt_prefix[af], name);
270 target = xt_find_target(af, name, revision);
275 EXPORT_SYMBOL_GPL(xt_request_find_target);
278 static int xt_obj_to_user(u16 __user *psize, u16 size,
279 void __user *pname, const char *name,
280 u8 __user *prev, u8 rev)
282 if (put_user(size, psize))
284 if (copy_to_user(pname, name, strlen(name) + 1))
286 if (put_user(rev, prev))
292 #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
293 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
294 U->u.user.name, K->u.kernel.TYPE->name, \
295 &U->u.user.revision, K->u.kernel.TYPE->revision)
297 int xt_data_to_user(void __user *dst, const void *src,
298 int usersize, int size, int aligned_size)
300 usersize = usersize ? : size;
301 if (copy_to_user(dst, src, usersize))
303 if (usersize != aligned_size &&
304 clear_user(dst + usersize, aligned_size - usersize))
309 EXPORT_SYMBOL_GPL(xt_data_to_user);
311 #define XT_DATA_TO_USER(U, K, TYPE) \
312 xt_data_to_user(U->data, K->data, \
313 K->u.kernel.TYPE->usersize, \
314 K->u.kernel.TYPE->TYPE##size, \
315 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
317 int xt_match_to_user(const struct xt_entry_match *m,
318 struct xt_entry_match __user *u)
320 return XT_OBJ_TO_USER(u, m, match, 0) ||
321 XT_DATA_TO_USER(u, m, match);
323 EXPORT_SYMBOL_GPL(xt_match_to_user);
325 int xt_target_to_user(const struct xt_entry_target *t,
326 struct xt_entry_target __user *u)
328 return XT_OBJ_TO_USER(u, t, target, 0) ||
329 XT_DATA_TO_USER(u, t, target);
331 EXPORT_SYMBOL_GPL(xt_target_to_user);
333 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
335 const struct xt_match *m;
338 mutex_lock(&xt[af].mutex);
339 list_for_each_entry(m, &xt[af].match, list) {
340 if (strcmp(m->name, name) == 0) {
341 if (m->revision > *bestp)
342 *bestp = m->revision;
343 if (m->revision == revision)
347 mutex_unlock(&xt[af].mutex);
349 if (af != NFPROTO_UNSPEC && !have_rev)
350 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
355 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
357 const struct xt_target *t;
360 mutex_lock(&xt[af].mutex);
361 list_for_each_entry(t, &xt[af].target, list) {
362 if (strcmp(t->name, name) == 0) {
363 if (t->revision > *bestp)
364 *bestp = t->revision;
365 if (t->revision == revision)
369 mutex_unlock(&xt[af].mutex);
371 if (af != NFPROTO_UNSPEC && !have_rev)
372 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
377 /* Returns true or false (if no such extension at all) */
378 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
381 int have_rev, best = -1;
384 have_rev = target_revfn(af, name, revision, &best);
386 have_rev = match_revfn(af, name, revision, &best);
388 /* Nothing at all? Return 0 to try loading module. */
396 *err = -EPROTONOSUPPORT;
399 EXPORT_SYMBOL_GPL(xt_find_revision);
402 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
404 static const char *const inetbr_names[] = {
405 "PREROUTING", "INPUT", "FORWARD",
406 "OUTPUT", "POSTROUTING", "BROUTING",
408 static const char *const arp_names[] = {
409 "INPUT", "FORWARD", "OUTPUT",
411 const char *const *names;
417 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
418 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
419 ARRAY_SIZE(inetbr_names);
421 for (i = 0; i < max; ++i) {
422 if (!(mask & (1 << i)))
424 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
436 * xt_check_proc_name - check that name is suitable for /proc file creation
438 * @name: file name candidate
439 * @size: length of buffer
441 * some x_tables modules wish to create a file in /proc.
442 * This function makes sure that the name is suitable for this
443 * purpose, it checks that name is NUL terminated and isn't a 'special'
446 * returns negative number on error or 0 if name is useable.
448 int xt_check_proc_name(const char *name, unsigned int size)
453 if (strnlen(name, size) == size)
454 return -ENAMETOOLONG;
456 if (strcmp(name, ".") == 0 ||
457 strcmp(name, "..") == 0 ||
463 EXPORT_SYMBOL(xt_check_proc_name);
465 int xt_check_match(struct xt_mtchk_param *par,
466 unsigned int size, u_int8_t proto, bool inv_proto)
470 if (XT_ALIGN(par->match->matchsize) != size &&
471 par->match->matchsize != -1) {
473 * ebt_among is exempt from centralized matchsize checking
474 * because it uses a dynamic-size data set.
476 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
477 xt_prefix[par->family], par->match->name,
478 par->match->revision,
479 XT_ALIGN(par->match->matchsize), size);
482 if (par->match->table != NULL &&
483 strcmp(par->match->table, par->table) != 0) {
484 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
485 xt_prefix[par->family], par->match->name,
486 par->match->table, par->table);
489 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
490 char used[64], allow[64];
492 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
493 xt_prefix[par->family], par->match->name,
494 textify_hooks(used, sizeof(used),
495 par->hook_mask, par->family),
496 textify_hooks(allow, sizeof(allow),
501 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
502 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
503 xt_prefix[par->family], par->match->name,
507 if (par->match->checkentry != NULL) {
508 ret = par->match->checkentry(par);
512 /* Flag up potential errors. */
517 EXPORT_SYMBOL_GPL(xt_check_match);
519 /** xt_check_entry_match - check that matches end before start of target
521 * @match: beginning of xt_entry_match
522 * @target: beginning of this rules target (alleged end of matches)
523 * @alignment: alignment requirement of match structures
525 * Validates that all matches add up to the beginning of the target,
526 * and that each match covers at least the base structure size.
528 * Return: 0 on success, negative errno on failure.
530 static int xt_check_entry_match(const char *match, const char *target,
531 const size_t alignment)
533 const struct xt_entry_match *pos;
534 int length = target - match;
536 if (length == 0) /* no matches */
539 pos = (struct xt_entry_match *)match;
541 if ((unsigned long)pos % alignment)
544 if (length < (int)sizeof(struct xt_entry_match))
547 if (pos->u.match_size < sizeof(struct xt_entry_match))
550 if (pos->u.match_size > length)
553 length -= pos->u.match_size;
554 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
555 } while (length > 0);
560 /** xt_check_table_hooks - check hook entry points are sane
562 * @info xt_table_info to check
563 * @valid_hooks - hook entry points that we can enter from
565 * Validates that the hook entry and underflows points are set up.
567 * Return: 0 on success, negative errno on failure.
569 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
571 const char *err = "unsorted underflow";
572 unsigned int i, max_uflow, max_entry;
573 bool check_hooks = false;
575 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
580 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
581 if (!(valid_hooks & (1 << i)))
584 if (info->hook_entry[i] == 0xFFFFFFFF)
586 if (info->underflow[i] == 0xFFFFFFFF)
590 if (max_uflow > info->underflow[i])
593 if (max_uflow == info->underflow[i]) {
594 err = "duplicate underflow";
597 if (max_entry > info->hook_entry[i]) {
598 err = "unsorted entry";
601 if (max_entry == info->hook_entry[i]) {
602 err = "duplicate entry";
606 max_entry = info->hook_entry[i];
607 max_uflow = info->underflow[i];
613 pr_err_ratelimited("%s at hook %d\n", err, i);
616 EXPORT_SYMBOL(xt_check_table_hooks);
618 static bool verdict_ok(int verdict)
624 int v = -verdict - 1;
626 if (verdict == XT_RETURN)
630 case NF_ACCEPT: return true;
631 case NF_DROP: return true;
632 case NF_QUEUE: return true;
643 static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
644 const char *msg, unsigned int msglen)
646 return usersize == kernsize && strnlen(msg, msglen) < msglen;
650 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
652 struct xt_af *xp = &xt[af];
654 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
656 if (WARN_ON(!xp->compat_tab))
659 if (xp->cur >= xp->number)
663 delta += xp->compat_tab[xp->cur - 1].delta;
664 xp->compat_tab[xp->cur].offset = offset;
665 xp->compat_tab[xp->cur].delta = delta;
669 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
671 void xt_compat_flush_offsets(u_int8_t af)
673 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
675 if (xt[af].compat_tab) {
676 vfree(xt[af].compat_tab);
677 xt[af].compat_tab = NULL;
682 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
684 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
686 struct compat_delta *tmp = xt[af].compat_tab;
687 int mid, left = 0, right = xt[af].cur - 1;
689 while (left <= right) {
690 mid = (left + right) >> 1;
691 if (offset > tmp[mid].offset)
693 else if (offset < tmp[mid].offset)
696 return mid ? tmp[mid - 1].delta : 0;
698 return left ? tmp[left - 1].delta : 0;
700 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
702 int xt_compat_init_offsets(u8 af, unsigned int number)
706 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
708 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
711 if (WARN_ON(xt[af].compat_tab))
714 mem = sizeof(struct compat_delta) * number;
715 if (mem > XT_MAX_TABLE_SIZE)
718 xt[af].compat_tab = vmalloc(mem);
719 if (!xt[af].compat_tab)
722 xt[af].number = number;
727 EXPORT_SYMBOL(xt_compat_init_offsets);
729 int xt_compat_match_offset(const struct xt_match *match)
731 u_int16_t csize = match->compatsize ? : match->matchsize;
732 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
734 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
736 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
739 const struct xt_match *match = m->u.kernel.match;
740 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
741 int off = xt_compat_match_offset(match);
742 u_int16_t msize = cm->u.user.match_size;
743 char name[sizeof(m->u.user.name)];
746 memcpy(m, cm, sizeof(*cm));
747 if (match->compat_from_user)
748 match->compat_from_user(m->data, cm->data);
750 memcpy(m->data, cm->data, msize - sizeof(*cm));
753 m->u.user.match_size = msize;
754 strlcpy(name, match->name, sizeof(name));
755 module_put(match->me);
756 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
761 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
763 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
764 xt_data_to_user(U->data, K->data, \
765 K->u.kernel.TYPE->usersize, \
767 COMPAT_XT_ALIGN(C_SIZE))
769 int xt_compat_match_to_user(const struct xt_entry_match *m,
770 void __user **dstptr, unsigned int *size)
772 const struct xt_match *match = m->u.kernel.match;
773 struct compat_xt_entry_match __user *cm = *dstptr;
774 int off = xt_compat_match_offset(match);
775 u_int16_t msize = m->u.user.match_size - off;
777 if (XT_OBJ_TO_USER(cm, m, match, msize))
780 if (match->compat_to_user) {
781 if (match->compat_to_user((void __user *)cm->data, m->data))
784 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
792 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
794 /* non-compat version may have padding after verdict */
795 struct compat_xt_standard_target {
796 struct compat_xt_entry_target t;
797 compat_uint_t verdict;
800 struct compat_xt_error_target {
801 struct compat_xt_entry_target t;
802 char errorname[XT_FUNCTION_MAXNAMELEN];
805 int xt_compat_check_entry_offsets(const void *base, const char *elems,
806 unsigned int target_offset,
807 unsigned int next_offset)
809 long size_of_base_struct = elems - (const char *)base;
810 const struct compat_xt_entry_target *t;
811 const char *e = base;
813 if (target_offset < size_of_base_struct)
816 if (target_offset + sizeof(*t) > next_offset)
819 t = (void *)(e + target_offset);
820 if (t->u.target_size < sizeof(*t))
823 if (target_offset + t->u.target_size > next_offset)
826 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
827 const struct compat_xt_standard_target *st = (const void *)t;
829 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
832 if (!verdict_ok(st->verdict))
834 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
835 const struct compat_xt_error_target *et = (const void *)t;
837 if (!error_tg_ok(t->u.target_size, sizeof(*et),
838 et->errorname, sizeof(et->errorname)))
842 /* compat_xt_entry match has less strict alignment requirements,
843 * otherwise they are identical. In case of padding differences
844 * we need to add compat version of xt_check_entry_match.
846 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
848 return xt_check_entry_match(elems, base + target_offset,
849 __alignof__(struct compat_xt_entry_match));
851 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
852 #endif /* CONFIG_COMPAT */
855 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
857 * @base: pointer to arp/ip/ip6t_entry
858 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
859 * @target_offset: the arp/ip/ip6_t->target_offset
860 * @next_offset: the arp/ip/ip6_t->next_offset
862 * validates that target_offset and next_offset are sane and that all
863 * match sizes (if any) align with the target offset.
865 * This function does not validate the targets or matches themselves, it
866 * only tests that all the offsets and sizes are correct, that all
867 * match structures are aligned, and that the last structure ends where
868 * the target structure begins.
870 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
872 * The arp/ip/ip6t_entry structure @base must have passed following tests:
873 * - it must point to a valid memory location
874 * - base to base + next_offset must be accessible, i.e. not exceed allocated
877 * A well-formed entry looks like this:
879 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
880 * e->elems[]-----' | |
884 * target_offset---------------------------------' |
885 * next_offset---------------------------------------------------'
887 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
888 * This is where matches (if any) and the target reside.
889 * target_offset: beginning of target.
890 * next_offset: start of the next rule; also: size of this rule.
891 * Since targets have a minimum size, target_offset + minlen <= next_offset.
893 * Every match stores its size, sum of sizes must not exceed target_offset.
895 * Return: 0 on success, negative errno on failure.
897 int xt_check_entry_offsets(const void *base,
899 unsigned int target_offset,
900 unsigned int next_offset)
902 long size_of_base_struct = elems - (const char *)base;
903 const struct xt_entry_target *t;
904 const char *e = base;
906 /* target start is within the ip/ip6/arpt_entry struct */
907 if (target_offset < size_of_base_struct)
910 if (target_offset + sizeof(*t) > next_offset)
913 t = (void *)(e + target_offset);
914 if (t->u.target_size < sizeof(*t))
917 if (target_offset + t->u.target_size > next_offset)
920 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
921 const struct xt_standard_target *st = (const void *)t;
923 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
926 if (!verdict_ok(st->verdict))
928 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
929 const struct xt_error_target *et = (const void *)t;
931 if (!error_tg_ok(t->u.target_size, sizeof(*et),
932 et->errorname, sizeof(et->errorname)))
936 return xt_check_entry_match(elems, base + target_offset,
937 __alignof__(struct xt_entry_match));
939 EXPORT_SYMBOL(xt_check_entry_offsets);
942 * xt_alloc_entry_offsets - allocate array to store rule head offsets
944 * @size: number of entries
946 * Return: NULL or kmalloc'd or vmalloc'd array
948 unsigned int *xt_alloc_entry_offsets(unsigned int size)
950 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
953 return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
956 EXPORT_SYMBOL(xt_alloc_entry_offsets);
959 * xt_find_jump_offset - check if target is a valid jump offset
961 * @offsets: array containing all valid rule start offsets of a rule blob
962 * @target: the jump target to search for
963 * @size: entries in @offset
965 bool xt_find_jump_offset(const unsigned int *offsets,
966 unsigned int target, unsigned int size)
968 int m, low = 0, hi = size;
973 if (offsets[m] > target)
975 else if (offsets[m] < target)
983 EXPORT_SYMBOL(xt_find_jump_offset);
985 int xt_check_target(struct xt_tgchk_param *par,
986 unsigned int size, u_int8_t proto, bool inv_proto)
990 if (XT_ALIGN(par->target->targetsize) != size) {
991 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
992 xt_prefix[par->family], par->target->name,
993 par->target->revision,
994 XT_ALIGN(par->target->targetsize), size);
997 if (par->target->table != NULL &&
998 strcmp(par->target->table, par->table) != 0) {
999 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
1000 xt_prefix[par->family], par->target->name,
1001 par->target->table, par->table);
1004 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1005 char used[64], allow[64];
1007 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1008 xt_prefix[par->family], par->target->name,
1009 textify_hooks(used, sizeof(used),
1010 par->hook_mask, par->family),
1011 textify_hooks(allow, sizeof(allow),
1016 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1017 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1018 xt_prefix[par->family], par->target->name,
1019 par->target->proto);
1022 if (par->target->checkentry != NULL) {
1023 ret = par->target->checkentry(par);
1027 /* Flag up potential errors. */
1032 EXPORT_SYMBOL_GPL(xt_check_target);
1035 * xt_copy_counters_from_user - copy counters and metadata from userspace
1037 * @user: src pointer to userspace memory
1038 * @len: alleged size of userspace memory
1039 * @info: where to store the xt_counters_info metadata
1040 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
1042 * Copies counter meta data from @user and stores it in @info.
1044 * vmallocs memory to hold the counters, then copies the counter data
1045 * from @user to the new memory and returns a pointer to it.
1047 * If @compat is true, @info gets converted automatically to the 64bit
1050 * The metadata associated with the counters is stored in @info.
1052 * Return: returns pointer that caller has to test via IS_ERR().
1053 * If IS_ERR is false, caller has to vfree the pointer.
1055 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
1056 struct xt_counters_info *info, bool compat)
1061 #ifdef CONFIG_COMPAT
1063 /* structures only differ in size due to alignment */
1064 struct compat_xt_counters_info compat_tmp;
1066 if (len <= sizeof(compat_tmp))
1067 return ERR_PTR(-EINVAL);
1069 len -= sizeof(compat_tmp);
1070 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
1071 return ERR_PTR(-EFAULT);
1073 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1074 info->num_counters = compat_tmp.num_counters;
1075 user += sizeof(compat_tmp);
1079 if (len <= sizeof(*info))
1080 return ERR_PTR(-EINVAL);
1082 len -= sizeof(*info);
1083 if (copy_from_user(info, user, sizeof(*info)) != 0)
1084 return ERR_PTR(-EFAULT);
1086 user += sizeof(*info);
1088 info->name[sizeof(info->name) - 1] = '\0';
1090 size = sizeof(struct xt_counters);
1091 size *= info->num_counters;
1093 if (size != (u64)len)
1094 return ERR_PTR(-EINVAL);
1098 return ERR_PTR(-ENOMEM);
1100 if (copy_from_user(mem, user, len) == 0)
1104 return ERR_PTR(-EFAULT);
1106 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
1108 #ifdef CONFIG_COMPAT
1109 int xt_compat_target_offset(const struct xt_target *target)
1111 u_int16_t csize = target->compatsize ? : target->targetsize;
1112 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1114 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1116 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1119 const struct xt_target *target = t->u.kernel.target;
1120 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1121 int off = xt_compat_target_offset(target);
1122 u_int16_t tsize = ct->u.user.target_size;
1123 char name[sizeof(t->u.user.name)];
1126 memcpy(t, ct, sizeof(*ct));
1127 if (target->compat_from_user)
1128 target->compat_from_user(t->data, ct->data);
1130 memcpy(t->data, ct->data, tsize - sizeof(*ct));
1133 t->u.user.target_size = tsize;
1134 strlcpy(name, target->name, sizeof(name));
1135 module_put(target->me);
1136 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
1141 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1143 int xt_compat_target_to_user(const struct xt_entry_target *t,
1144 void __user **dstptr, unsigned int *size)
1146 const struct xt_target *target = t->u.kernel.target;
1147 struct compat_xt_entry_target __user *ct = *dstptr;
1148 int off = xt_compat_target_offset(target);
1149 u_int16_t tsize = t->u.user.target_size - off;
1151 if (XT_OBJ_TO_USER(ct, t, target, tsize))
1154 if (target->compat_to_user) {
1155 if (target->compat_to_user((void __user *)ct->data, t->data))
1158 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1166 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1169 struct xt_table_info *xt_alloc_table_info(unsigned int size)
1171 struct xt_table_info *info = NULL;
1172 size_t sz = sizeof(*info) + size;
1174 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1177 info = kvmalloc(sz, GFP_KERNEL_ACCOUNT);
1181 memset(info, 0, sizeof(*info));
1185 EXPORT_SYMBOL(xt_alloc_table_info);
1187 void xt_free_table_info(struct xt_table_info *info)
1191 if (info->jumpstack != NULL) {
1192 for_each_possible_cpu(cpu)
1193 kvfree(info->jumpstack[cpu]);
1194 kvfree(info->jumpstack);
1199 EXPORT_SYMBOL(xt_free_table_info);
1201 /* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
1202 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1205 struct xt_table *t, *found = NULL;
1207 mutex_lock(&xt[af].mutex);
1208 list_for_each_entry(t, &net->xt.tables[af], list)
1209 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1212 if (net == &init_net)
1215 /* Table doesn't exist in this netns, re-try init */
1216 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1219 if (strcmp(t->name, name))
1221 if (!try_module_get(t->me))
1223 mutex_unlock(&xt[af].mutex);
1224 err = t->table_init(net);
1227 return ERR_PTR(err);
1232 mutex_lock(&xt[af].mutex);
1239 /* and once again: */
1240 list_for_each_entry(t, &net->xt.tables[af], list)
1241 if (strcmp(t->name, name) == 0)
1244 module_put(found->me);
1246 mutex_unlock(&xt[af].mutex);
1247 return ERR_PTR(-ENOENT);
1249 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1251 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1254 struct xt_table *t = xt_find_table_lock(net, af, name);
1256 #ifdef CONFIG_MODULES
1258 int err = request_module("%stable_%s", xt_prefix[af], name);
1260 return ERR_PTR(err);
1261 t = xt_find_table_lock(net, af, name);
1267 EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1269 void xt_table_unlock(struct xt_table *table)
1271 mutex_unlock(&xt[table->af].mutex);
1273 EXPORT_SYMBOL_GPL(xt_table_unlock);
1275 #ifdef CONFIG_COMPAT
1276 void xt_compat_lock(u_int8_t af)
1278 mutex_lock(&xt[af].compat_mutex);
1280 EXPORT_SYMBOL_GPL(xt_compat_lock);
1282 void xt_compat_unlock(u_int8_t af)
1284 mutex_unlock(&xt[af].compat_mutex);
1286 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1289 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1290 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1292 struct static_key xt_tee_enabled __read_mostly;
1293 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1295 static int xt_jumpstack_alloc(struct xt_table_info *i)
1300 size = sizeof(void **) * nr_cpu_ids;
1301 if (size > PAGE_SIZE)
1302 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1304 i->jumpstack = kzalloc(size, GFP_KERNEL);
1305 if (i->jumpstack == NULL)
1308 /* ruleset without jumps -- no stack needed */
1309 if (i->stacksize == 0)
1312 /* Jumpstack needs to be able to record two full callchains, one
1313 * from the first rule set traversal, plus one table reentrancy
1314 * via -j TEE without clobbering the callchain that brought us to
1317 * This is done by allocating two jumpstacks per cpu, on reentry
1318 * the upper half of the stack is used.
1320 * see the jumpstack setup in ipt_do_table() for more details.
1322 size = sizeof(void *) * i->stacksize * 2u;
1323 for_each_possible_cpu(cpu) {
1324 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1326 if (i->jumpstack[cpu] == NULL)
1328 * Freeing will be done later on by the callers. The
1329 * chain is: xt_replace_table -> __do_replace ->
1330 * do_replace -> xt_free_table_info.
1338 struct xt_counters *xt_counters_alloc(unsigned int counters)
1340 struct xt_counters *mem;
1342 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1345 counters *= sizeof(*mem);
1346 if (counters > XT_MAX_TABLE_SIZE)
1349 return vzalloc(counters);
1351 EXPORT_SYMBOL(xt_counters_alloc);
1353 struct xt_table_info *
1354 xt_replace_table(struct xt_table *table,
1355 unsigned int num_counters,
1356 struct xt_table_info *newinfo,
1359 struct xt_table_info *private;
1363 ret = xt_jumpstack_alloc(newinfo);
1369 /* Do the substitution. */
1371 private = table->private;
1373 /* Check inside lock: is the old number correct? */
1374 if (num_counters != private->number) {
1375 pr_debug("num_counters != table->private->number (%u/%u)\n",
1376 num_counters, private->number);
1382 newinfo->initial_entries = private->initial_entries;
1384 * Ensure contents of newinfo are visible before assigning to
1388 table->private = newinfo;
1390 /* make sure all cpus see new ->private value */
1394 * Even though table entries have now been swapped, other CPU's
1395 * may still be using the old entries...
1399 /* ... so wait for even xt_recseq on all cpus */
1400 for_each_possible_cpu(cpu) {
1401 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1402 u32 seq = raw_read_seqcount(s);
1408 } while (seq == raw_read_seqcount(s));
1413 if (audit_enabled) {
1414 audit_log(audit_context(), GFP_KERNEL,
1415 AUDIT_NETFILTER_CFG,
1416 "table=%s family=%u entries=%u",
1417 table->name, table->af, private->number);
1423 EXPORT_SYMBOL_GPL(xt_replace_table);
1425 struct xt_table *xt_register_table(struct net *net,
1426 const struct xt_table *input_table,
1427 struct xt_table_info *bootstrap,
1428 struct xt_table_info *newinfo)
1431 struct xt_table_info *private;
1432 struct xt_table *t, *table;
1434 /* Don't add one object to multiple lists. */
1435 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1441 mutex_lock(&xt[table->af].mutex);
1442 /* Don't autoload: we'd eat our tail... */
1443 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1444 if (strcmp(t->name, table->name) == 0) {
1450 /* Simplifies replace_table code. */
1451 table->private = bootstrap;
1453 if (!xt_replace_table(table, 0, newinfo, &ret))
1456 private = table->private;
1457 pr_debug("table->private->number = %u\n", private->number);
1459 /* save number of initial entries */
1460 private->initial_entries = private->number;
1462 list_add(&table->list, &net->xt.tables[table->af]);
1463 mutex_unlock(&xt[table->af].mutex);
1467 mutex_unlock(&xt[table->af].mutex);
1470 return ERR_PTR(ret);
1472 EXPORT_SYMBOL_GPL(xt_register_table);
1474 void *xt_unregister_table(struct xt_table *table)
1476 struct xt_table_info *private;
1478 mutex_lock(&xt[table->af].mutex);
1479 private = table->private;
1480 list_del(&table->list);
1481 mutex_unlock(&xt[table->af].mutex);
1486 EXPORT_SYMBOL_GPL(xt_unregister_table);
1488 #ifdef CONFIG_PROC_FS
1489 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1491 struct net *net = seq_file_net(seq);
1492 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1494 mutex_lock(&xt[af].mutex);
1495 return seq_list_start(&net->xt.tables[af], *pos);
1498 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1500 struct net *net = seq_file_net(seq);
1501 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1503 return seq_list_next(v, &net->xt.tables[af], pos);
1506 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1508 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1510 mutex_unlock(&xt[af].mutex);
1513 static int xt_table_seq_show(struct seq_file *seq, void *v)
1515 struct xt_table *table = list_entry(v, struct xt_table, list);
1518 seq_printf(seq, "%s\n", table->name);
1522 static const struct seq_operations xt_table_seq_ops = {
1523 .start = xt_table_seq_start,
1524 .next = xt_table_seq_next,
1525 .stop = xt_table_seq_stop,
1526 .show = xt_table_seq_show,
1530 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1531 * the multi-AF mutexes.
1533 struct nf_mttg_trav {
1534 struct list_head *head, *curr;
1540 MTTG_TRAV_NFP_UNSPEC,
1545 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1548 static const uint8_t next_class[] = {
1549 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1550 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1552 uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file));
1553 struct nf_mttg_trav *trav = seq->private;
1558 switch (trav->class) {
1559 case MTTG_TRAV_INIT:
1560 trav->class = MTTG_TRAV_NFP_UNSPEC;
1561 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1562 trav->head = trav->curr = is_target ?
1563 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1565 case MTTG_TRAV_NFP_UNSPEC:
1566 trav->curr = trav->curr->next;
1567 if (trav->curr != trav->head)
1569 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1570 mutex_lock(&xt[nfproto].mutex);
1571 trav->head = trav->curr = is_target ?
1572 &xt[nfproto].target : &xt[nfproto].match;
1573 trav->class = next_class[trav->class];
1575 case MTTG_TRAV_NFP_SPEC:
1576 trav->curr = trav->curr->next;
1577 if (trav->curr != trav->head)
1586 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1589 struct nf_mttg_trav *trav = seq->private;
1592 trav->class = MTTG_TRAV_INIT;
1593 for (j = 0; j < *pos; ++j)
1594 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1599 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1601 uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file));
1602 struct nf_mttg_trav *trav = seq->private;
1604 switch (trav->class) {
1605 case MTTG_TRAV_NFP_UNSPEC:
1606 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1608 case MTTG_TRAV_NFP_SPEC:
1609 mutex_unlock(&xt[nfproto].mutex);
1614 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1616 return xt_mttg_seq_start(seq, pos, false);
1619 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1621 return xt_mttg_seq_next(seq, v, ppos, false);
1624 static int xt_match_seq_show(struct seq_file *seq, void *v)
1626 const struct nf_mttg_trav *trav = seq->private;
1627 const struct xt_match *match;
1629 switch (trav->class) {
1630 case MTTG_TRAV_NFP_UNSPEC:
1631 case MTTG_TRAV_NFP_SPEC:
1632 if (trav->curr == trav->head)
1634 match = list_entry(trav->curr, struct xt_match, list);
1636 seq_printf(seq, "%s\n", match->name);
1641 static const struct seq_operations xt_match_seq_ops = {
1642 .start = xt_match_seq_start,
1643 .next = xt_match_seq_next,
1644 .stop = xt_mttg_seq_stop,
1645 .show = xt_match_seq_show,
1648 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1650 return xt_mttg_seq_start(seq, pos, true);
1653 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1655 return xt_mttg_seq_next(seq, v, ppos, true);
1658 static int xt_target_seq_show(struct seq_file *seq, void *v)
1660 const struct nf_mttg_trav *trav = seq->private;
1661 const struct xt_target *target;
1663 switch (trav->class) {
1664 case MTTG_TRAV_NFP_UNSPEC:
1665 case MTTG_TRAV_NFP_SPEC:
1666 if (trav->curr == trav->head)
1668 target = list_entry(trav->curr, struct xt_target, list);
1670 seq_printf(seq, "%s\n", target->name);
1675 static const struct seq_operations xt_target_seq_ops = {
1676 .start = xt_target_seq_start,
1677 .next = xt_target_seq_next,
1678 .stop = xt_mttg_seq_stop,
1679 .show = xt_target_seq_show,
1682 #define FORMAT_TABLES "_tables_names"
1683 #define FORMAT_MATCHES "_tables_matches"
1684 #define FORMAT_TARGETS "_tables_targets"
1686 #endif /* CONFIG_PROC_FS */
1689 * xt_hook_ops_alloc - set up hooks for a new table
1690 * @table: table with metadata needed to set up hooks
1691 * @fn: Hook function
1693 * This function will create the nf_hook_ops that the x_table needs
1694 * to hand to xt_hook_link_net().
1696 struct nf_hook_ops *
1697 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1699 unsigned int hook_mask = table->valid_hooks;
1700 uint8_t i, num_hooks = hweight32(hook_mask);
1702 struct nf_hook_ops *ops;
1705 return ERR_PTR(-EINVAL);
1707 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1709 return ERR_PTR(-ENOMEM);
1711 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1712 hook_mask >>= 1, ++hooknum) {
1713 if (!(hook_mask & 1))
1716 ops[i].pf = table->af;
1717 ops[i].hooknum = hooknum;
1718 ops[i].priority = table->priority;
1724 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1726 int xt_proto_init(struct net *net, u_int8_t af)
1728 #ifdef CONFIG_PROC_FS
1729 char buf[XT_FUNCTION_MAXNAMELEN];
1730 struct proc_dir_entry *proc;
1735 if (af >= ARRAY_SIZE(xt_prefix))
1739 #ifdef CONFIG_PROC_FS
1740 root_uid = make_kuid(net->user_ns, 0);
1741 root_gid = make_kgid(net->user_ns, 0);
1743 strlcpy(buf, xt_prefix[af], sizeof(buf));
1744 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1745 proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops,
1746 sizeof(struct seq_net_private),
1747 (void *)(unsigned long)af);
1750 if (uid_valid(root_uid) && gid_valid(root_gid))
1751 proc_set_user(proc, root_uid, root_gid);
1753 strlcpy(buf, xt_prefix[af], sizeof(buf));
1754 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1755 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1756 &xt_match_seq_ops, sizeof(struct nf_mttg_trav),
1757 (void *)(unsigned long)af);
1759 goto out_remove_tables;
1760 if (uid_valid(root_uid) && gid_valid(root_gid))
1761 proc_set_user(proc, root_uid, root_gid);
1763 strlcpy(buf, xt_prefix[af], sizeof(buf));
1764 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1765 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1766 &xt_target_seq_ops, sizeof(struct nf_mttg_trav),
1767 (void *)(unsigned long)af);
1769 goto out_remove_matches;
1770 if (uid_valid(root_uid) && gid_valid(root_gid))
1771 proc_set_user(proc, root_uid, root_gid);
1776 #ifdef CONFIG_PROC_FS
1778 strlcpy(buf, xt_prefix[af], sizeof(buf));
1779 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1780 remove_proc_entry(buf, net->proc_net);
1783 strlcpy(buf, xt_prefix[af], sizeof(buf));
1784 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1785 remove_proc_entry(buf, net->proc_net);
1790 EXPORT_SYMBOL_GPL(xt_proto_init);
1792 void xt_proto_fini(struct net *net, u_int8_t af)
1794 #ifdef CONFIG_PROC_FS
1795 char buf[XT_FUNCTION_MAXNAMELEN];
1797 strlcpy(buf, xt_prefix[af], sizeof(buf));
1798 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1799 remove_proc_entry(buf, net->proc_net);
1801 strlcpy(buf, xt_prefix[af], sizeof(buf));
1802 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1803 remove_proc_entry(buf, net->proc_net);
1805 strlcpy(buf, xt_prefix[af], sizeof(buf));
1806 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1807 remove_proc_entry(buf, net->proc_net);
1808 #endif /*CONFIG_PROC_FS*/
1810 EXPORT_SYMBOL_GPL(xt_proto_fini);
1813 * xt_percpu_counter_alloc - allocate x_tables rule counter
1815 * @state: pointer to xt_percpu allocation state
1816 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1818 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1819 * contain the address of the real (percpu) counter.
1821 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1822 * to fetch the real percpu counter.
1824 * To speed up allocation and improve data locality, a 4kb block is
1825 * allocated. Freeing any counter may free an entire block, so all
1826 * counters allocated using the same state must be freed at the same
1829 * xt_percpu_counter_alloc_state contains the base address of the
1830 * allocated page and the current sub-offset.
1832 * returns false on error.
1834 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1835 struct xt_counters *counter)
1837 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1839 if (nr_cpu_ids <= 1)
1843 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1844 XT_PCPU_BLOCK_SIZE);
1848 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1849 state->off += sizeof(*counter);
1850 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1856 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1858 void xt_percpu_counter_free(struct xt_counters *counters)
1860 unsigned long pcnt = counters->pcnt;
1862 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1863 free_percpu((void __percpu *)pcnt);
1865 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1867 static int __net_init xt_net_init(struct net *net)
1871 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1872 INIT_LIST_HEAD(&net->xt.tables[i]);
1876 static void __net_exit xt_net_exit(struct net *net)
1880 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1881 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1884 static struct pernet_operations xt_net_ops = {
1885 .init = xt_net_init,
1886 .exit = xt_net_exit,
1889 static int __init xt_init(void)
1894 for_each_possible_cpu(i) {
1895 seqcount_init(&per_cpu(xt_recseq, i));
1898 xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
1902 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1903 mutex_init(&xt[i].mutex);
1904 #ifdef CONFIG_COMPAT
1905 mutex_init(&xt[i].compat_mutex);
1906 xt[i].compat_tab = NULL;
1908 INIT_LIST_HEAD(&xt[i].target);
1909 INIT_LIST_HEAD(&xt[i].match);
1911 rv = register_pernet_subsys(&xt_net_ops);
1917 static void __exit xt_fini(void)
1919 unregister_pernet_subsys(&xt_net_ops);
1923 module_init(xt_init);
1924 module_exit(xt_fini);