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 list_for_each_entry(m, &xt[af].match, list) {
339 if (strcmp(m->name, name) == 0) {
340 if (m->revision > *bestp)
341 *bestp = m->revision;
342 if (m->revision == revision)
347 if (af != NFPROTO_UNSPEC && !have_rev)
348 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
353 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
355 const struct xt_target *t;
358 list_for_each_entry(t, &xt[af].target, list) {
359 if (strcmp(t->name, name) == 0) {
360 if (t->revision > *bestp)
361 *bestp = t->revision;
362 if (t->revision == revision)
367 if (af != NFPROTO_UNSPEC && !have_rev)
368 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
373 /* Returns true or false (if no such extension at all) */
374 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
377 int have_rev, best = -1;
379 mutex_lock(&xt[af].mutex);
381 have_rev = target_revfn(af, name, revision, &best);
383 have_rev = match_revfn(af, name, revision, &best);
384 mutex_unlock(&xt[af].mutex);
386 /* Nothing at all? Return 0 to try loading module. */
394 *err = -EPROTONOSUPPORT;
397 EXPORT_SYMBOL_GPL(xt_find_revision);
400 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
402 static const char *const inetbr_names[] = {
403 "PREROUTING", "INPUT", "FORWARD",
404 "OUTPUT", "POSTROUTING", "BROUTING",
406 static const char *const arp_names[] = {
407 "INPUT", "FORWARD", "OUTPUT",
409 const char *const *names;
415 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
416 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
417 ARRAY_SIZE(inetbr_names);
419 for (i = 0; i < max; ++i) {
420 if (!(mask & (1 << i)))
422 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
434 * xt_check_proc_name - check that name is suitable for /proc file creation
436 * @name: file name candidate
437 * @size: length of buffer
439 * some x_tables modules wish to create a file in /proc.
440 * This function makes sure that the name is suitable for this
441 * purpose, it checks that name is NUL terminated and isn't a 'special'
444 * returns negative number on error or 0 if name is useable.
446 int xt_check_proc_name(const char *name, unsigned int size)
451 if (strnlen(name, size) == size)
452 return -ENAMETOOLONG;
454 if (strcmp(name, ".") == 0 ||
455 strcmp(name, "..") == 0 ||
461 EXPORT_SYMBOL(xt_check_proc_name);
463 int xt_check_match(struct xt_mtchk_param *par,
464 unsigned int size, u_int8_t proto, bool inv_proto)
468 if (XT_ALIGN(par->match->matchsize) != size &&
469 par->match->matchsize != -1) {
471 * ebt_among is exempt from centralized matchsize checking
472 * because it uses a dynamic-size data set.
474 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
475 xt_prefix[par->family], par->match->name,
476 par->match->revision,
477 XT_ALIGN(par->match->matchsize), size);
480 if (par->match->table != NULL &&
481 strcmp(par->match->table, par->table) != 0) {
482 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
483 xt_prefix[par->family], par->match->name,
484 par->match->table, par->table);
487 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
488 char used[64], allow[64];
490 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
491 xt_prefix[par->family], par->match->name,
492 textify_hooks(used, sizeof(used),
493 par->hook_mask, par->family),
494 textify_hooks(allow, sizeof(allow),
499 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
500 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
501 xt_prefix[par->family], par->match->name,
505 if (par->match->checkentry != NULL) {
506 ret = par->match->checkentry(par);
510 /* Flag up potential errors. */
515 EXPORT_SYMBOL_GPL(xt_check_match);
517 /** xt_check_entry_match - check that matches end before start of target
519 * @match: beginning of xt_entry_match
520 * @target: beginning of this rules target (alleged end of matches)
521 * @alignment: alignment requirement of match structures
523 * Validates that all matches add up to the beginning of the target,
524 * and that each match covers at least the base structure size.
526 * Return: 0 on success, negative errno on failure.
528 static int xt_check_entry_match(const char *match, const char *target,
529 const size_t alignment)
531 const struct xt_entry_match *pos;
532 int length = target - match;
534 if (length == 0) /* no matches */
537 pos = (struct xt_entry_match *)match;
539 if ((unsigned long)pos % alignment)
542 if (length < (int)sizeof(struct xt_entry_match))
545 if (pos->u.match_size < sizeof(struct xt_entry_match))
548 if (pos->u.match_size > length)
551 length -= pos->u.match_size;
552 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
553 } while (length > 0);
558 /** xt_check_table_hooks - check hook entry points are sane
560 * @info xt_table_info to check
561 * @valid_hooks - hook entry points that we can enter from
563 * Validates that the hook entry and underflows points are set up.
565 * Return: 0 on success, negative errno on failure.
567 int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
569 const char *err = "unsorted underflow";
570 unsigned int i, max_uflow, max_entry;
571 bool check_hooks = false;
573 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
578 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
579 if (!(valid_hooks & (1 << i)))
582 if (info->hook_entry[i] == 0xFFFFFFFF)
584 if (info->underflow[i] == 0xFFFFFFFF)
588 if (max_uflow > info->underflow[i])
591 if (max_uflow == info->underflow[i]) {
592 err = "duplicate underflow";
595 if (max_entry > info->hook_entry[i]) {
596 err = "unsorted entry";
599 if (max_entry == info->hook_entry[i]) {
600 err = "duplicate entry";
604 max_entry = info->hook_entry[i];
605 max_uflow = info->underflow[i];
611 pr_err_ratelimited("%s at hook %d\n", err, i);
614 EXPORT_SYMBOL(xt_check_table_hooks);
616 static bool verdict_ok(int verdict)
622 int v = -verdict - 1;
624 if (verdict == XT_RETURN)
628 case NF_ACCEPT: return true;
629 case NF_DROP: return true;
630 case NF_QUEUE: return true;
641 static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
642 const char *msg, unsigned int msglen)
644 return usersize == kernsize && strnlen(msg, msglen) < msglen;
648 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
650 struct xt_af *xp = &xt[af];
652 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
654 if (WARN_ON(!xp->compat_tab))
657 if (xp->cur >= xp->number)
661 delta += xp->compat_tab[xp->cur - 1].delta;
662 xp->compat_tab[xp->cur].offset = offset;
663 xp->compat_tab[xp->cur].delta = delta;
667 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
669 void xt_compat_flush_offsets(u_int8_t af)
671 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
673 if (xt[af].compat_tab) {
674 vfree(xt[af].compat_tab);
675 xt[af].compat_tab = NULL;
680 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
682 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
684 struct compat_delta *tmp = xt[af].compat_tab;
685 int mid, left = 0, right = xt[af].cur - 1;
687 while (left <= right) {
688 mid = (left + right) >> 1;
689 if (offset > tmp[mid].offset)
691 else if (offset < tmp[mid].offset)
694 return mid ? tmp[mid - 1].delta : 0;
696 return left ? tmp[left - 1].delta : 0;
698 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
700 int xt_compat_init_offsets(u8 af, unsigned int number)
704 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
706 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
709 if (WARN_ON(xt[af].compat_tab))
712 mem = sizeof(struct compat_delta) * number;
713 if (mem > XT_MAX_TABLE_SIZE)
716 xt[af].compat_tab = vmalloc(mem);
717 if (!xt[af].compat_tab)
720 xt[af].number = number;
725 EXPORT_SYMBOL(xt_compat_init_offsets);
727 int xt_compat_match_offset(const struct xt_match *match)
729 u_int16_t csize = match->compatsize ? : match->matchsize;
730 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
732 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
734 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
737 const struct xt_match *match = m->u.kernel.match;
738 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
739 int pad, off = xt_compat_match_offset(match);
740 u_int16_t msize = cm->u.user.match_size;
741 char name[sizeof(m->u.user.name)];
744 memcpy(m, cm, sizeof(*cm));
745 if (match->compat_from_user)
746 match->compat_from_user(m->data, cm->data);
748 memcpy(m->data, cm->data, msize - sizeof(*cm));
749 pad = XT_ALIGN(match->matchsize) - match->matchsize;
751 memset(m->data + match->matchsize, 0, pad);
754 m->u.user.match_size = msize;
755 strlcpy(name, match->name, sizeof(name));
756 module_put(match->me);
757 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
762 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
764 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
765 xt_data_to_user(U->data, K->data, \
766 K->u.kernel.TYPE->usersize, \
768 COMPAT_XT_ALIGN(C_SIZE))
770 int xt_compat_match_to_user(const struct xt_entry_match *m,
771 void __user **dstptr, unsigned int *size)
773 const struct xt_match *match = m->u.kernel.match;
774 struct compat_xt_entry_match __user *cm = *dstptr;
775 int off = xt_compat_match_offset(match);
776 u_int16_t msize = m->u.user.match_size - off;
778 if (XT_OBJ_TO_USER(cm, m, match, msize))
781 if (match->compat_to_user) {
782 if (match->compat_to_user((void __user *)cm->data, m->data))
785 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
793 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
795 /* non-compat version may have padding after verdict */
796 struct compat_xt_standard_target {
797 struct compat_xt_entry_target t;
798 compat_uint_t verdict;
801 struct compat_xt_error_target {
802 struct compat_xt_entry_target t;
803 char errorname[XT_FUNCTION_MAXNAMELEN];
806 int xt_compat_check_entry_offsets(const void *base, const char *elems,
807 unsigned int target_offset,
808 unsigned int next_offset)
810 long size_of_base_struct = elems - (const char *)base;
811 const struct compat_xt_entry_target *t;
812 const char *e = base;
814 if (target_offset < size_of_base_struct)
817 if (target_offset + sizeof(*t) > next_offset)
820 t = (void *)(e + target_offset);
821 if (t->u.target_size < sizeof(*t))
824 if (target_offset + t->u.target_size > next_offset)
827 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
828 const struct compat_xt_standard_target *st = (const void *)t;
830 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
833 if (!verdict_ok(st->verdict))
835 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
836 const struct compat_xt_error_target *et = (const void *)t;
838 if (!error_tg_ok(t->u.target_size, sizeof(*et),
839 et->errorname, sizeof(et->errorname)))
843 /* compat_xt_entry match has less strict alignment requirements,
844 * otherwise they are identical. In case of padding differences
845 * we need to add compat version of xt_check_entry_match.
847 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
849 return xt_check_entry_match(elems, base + target_offset,
850 __alignof__(struct compat_xt_entry_match));
852 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
853 #endif /* CONFIG_COMPAT */
856 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
858 * @base: pointer to arp/ip/ip6t_entry
859 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
860 * @target_offset: the arp/ip/ip6_t->target_offset
861 * @next_offset: the arp/ip/ip6_t->next_offset
863 * validates that target_offset and next_offset are sane and that all
864 * match sizes (if any) align with the target offset.
866 * This function does not validate the targets or matches themselves, it
867 * only tests that all the offsets and sizes are correct, that all
868 * match structures are aligned, and that the last structure ends where
869 * the target structure begins.
871 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
873 * The arp/ip/ip6t_entry structure @base must have passed following tests:
874 * - it must point to a valid memory location
875 * - base to base + next_offset must be accessible, i.e. not exceed allocated
878 * A well-formed entry looks like this:
880 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
881 * e->elems[]-----' | |
885 * target_offset---------------------------------' |
886 * next_offset---------------------------------------------------'
888 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
889 * This is where matches (if any) and the target reside.
890 * target_offset: beginning of target.
891 * next_offset: start of the next rule; also: size of this rule.
892 * Since targets have a minimum size, target_offset + minlen <= next_offset.
894 * Every match stores its size, sum of sizes must not exceed target_offset.
896 * Return: 0 on success, negative errno on failure.
898 int xt_check_entry_offsets(const void *base,
900 unsigned int target_offset,
901 unsigned int next_offset)
903 long size_of_base_struct = elems - (const char *)base;
904 const struct xt_entry_target *t;
905 const char *e = base;
907 /* target start is within the ip/ip6/arpt_entry struct */
908 if (target_offset < size_of_base_struct)
911 if (target_offset + sizeof(*t) > next_offset)
914 t = (void *)(e + target_offset);
915 if (t->u.target_size < sizeof(*t))
918 if (target_offset + t->u.target_size > next_offset)
921 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
922 const struct xt_standard_target *st = (const void *)t;
924 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
927 if (!verdict_ok(st->verdict))
929 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
930 const struct xt_error_target *et = (const void *)t;
932 if (!error_tg_ok(t->u.target_size, sizeof(*et),
933 et->errorname, sizeof(et->errorname)))
937 return xt_check_entry_match(elems, base + target_offset,
938 __alignof__(struct xt_entry_match));
940 EXPORT_SYMBOL(xt_check_entry_offsets);
943 * xt_alloc_entry_offsets - allocate array to store rule head offsets
945 * @size: number of entries
947 * Return: NULL or kmalloc'd or vmalloc'd array
949 unsigned int *xt_alloc_entry_offsets(unsigned int size)
951 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
954 return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
957 EXPORT_SYMBOL(xt_alloc_entry_offsets);
960 * xt_find_jump_offset - check if target is a valid jump offset
962 * @offsets: array containing all valid rule start offsets of a rule blob
963 * @target: the jump target to search for
964 * @size: entries in @offset
966 bool xt_find_jump_offset(const unsigned int *offsets,
967 unsigned int target, unsigned int size)
969 int m, low = 0, hi = size;
974 if (offsets[m] > target)
976 else if (offsets[m] < target)
984 EXPORT_SYMBOL(xt_find_jump_offset);
986 int xt_check_target(struct xt_tgchk_param *par,
987 unsigned int size, u_int8_t proto, bool inv_proto)
991 if (XT_ALIGN(par->target->targetsize) != size) {
992 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
993 xt_prefix[par->family], par->target->name,
994 par->target->revision,
995 XT_ALIGN(par->target->targetsize), size);
998 if (par->target->table != NULL &&
999 strcmp(par->target->table, par->table) != 0) {
1000 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
1001 xt_prefix[par->family], par->target->name,
1002 par->target->table, par->table);
1005 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1006 char used[64], allow[64];
1008 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1009 xt_prefix[par->family], par->target->name,
1010 textify_hooks(used, sizeof(used),
1011 par->hook_mask, par->family),
1012 textify_hooks(allow, sizeof(allow),
1017 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1018 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1019 xt_prefix[par->family], par->target->name,
1020 par->target->proto);
1023 if (par->target->checkentry != NULL) {
1024 ret = par->target->checkentry(par);
1028 /* Flag up potential errors. */
1033 EXPORT_SYMBOL_GPL(xt_check_target);
1036 * xt_copy_counters_from_user - copy counters and metadata from userspace
1038 * @user: src pointer to userspace memory
1039 * @len: alleged size of userspace memory
1040 * @info: where to store the xt_counters_info metadata
1041 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
1043 * Copies counter meta data from @user and stores it in @info.
1045 * vmallocs memory to hold the counters, then copies the counter data
1046 * from @user to the new memory and returns a pointer to it.
1048 * If @compat is true, @info gets converted automatically to the 64bit
1051 * The metadata associated with the counters is stored in @info.
1053 * Return: returns pointer that caller has to test via IS_ERR().
1054 * If IS_ERR is false, caller has to vfree the pointer.
1056 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
1057 struct xt_counters_info *info, bool compat)
1062 #ifdef CONFIG_COMPAT
1064 /* structures only differ in size due to alignment */
1065 struct compat_xt_counters_info compat_tmp;
1067 if (len <= sizeof(compat_tmp))
1068 return ERR_PTR(-EINVAL);
1070 len -= sizeof(compat_tmp);
1071 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
1072 return ERR_PTR(-EFAULT);
1074 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1075 info->num_counters = compat_tmp.num_counters;
1076 user += sizeof(compat_tmp);
1080 if (len <= sizeof(*info))
1081 return ERR_PTR(-EINVAL);
1083 len -= sizeof(*info);
1084 if (copy_from_user(info, user, sizeof(*info)) != 0)
1085 return ERR_PTR(-EFAULT);
1087 user += sizeof(*info);
1089 info->name[sizeof(info->name) - 1] = '\0';
1091 size = sizeof(struct xt_counters);
1092 size *= info->num_counters;
1094 if (size != (u64)len)
1095 return ERR_PTR(-EINVAL);
1099 return ERR_PTR(-ENOMEM);
1101 if (copy_from_user(mem, user, len) == 0)
1105 return ERR_PTR(-EFAULT);
1107 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
1109 #ifdef CONFIG_COMPAT
1110 int xt_compat_target_offset(const struct xt_target *target)
1112 u_int16_t csize = target->compatsize ? : target->targetsize;
1113 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1115 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1117 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1120 const struct xt_target *target = t->u.kernel.target;
1121 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1122 int pad, off = xt_compat_target_offset(target);
1123 u_int16_t tsize = ct->u.user.target_size;
1124 char name[sizeof(t->u.user.name)];
1127 memcpy(t, ct, sizeof(*ct));
1128 if (target->compat_from_user)
1129 target->compat_from_user(t->data, ct->data);
1131 memcpy(t->data, ct->data, tsize - sizeof(*ct));
1132 pad = XT_ALIGN(target->targetsize) - target->targetsize;
1134 memset(t->data + target->targetsize, 0, pad);
1137 t->u.user.target_size = tsize;
1138 strlcpy(name, target->name, sizeof(name));
1139 module_put(target->me);
1140 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
1145 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1147 int xt_compat_target_to_user(const struct xt_entry_target *t,
1148 void __user **dstptr, unsigned int *size)
1150 const struct xt_target *target = t->u.kernel.target;
1151 struct compat_xt_entry_target __user *ct = *dstptr;
1152 int off = xt_compat_target_offset(target);
1153 u_int16_t tsize = t->u.user.target_size - off;
1155 if (XT_OBJ_TO_USER(ct, t, target, tsize))
1158 if (target->compat_to_user) {
1159 if (target->compat_to_user((void __user *)ct->data, t->data))
1162 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1170 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1173 struct xt_table_info *xt_alloc_table_info(unsigned int size)
1175 struct xt_table_info *info = NULL;
1176 size_t sz = sizeof(*info) + size;
1178 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1181 /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1182 * work reasonably well if sz is too large and bail out rather
1183 * than shoot all processes down before realizing there is nothing
1186 info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
1190 memset(info, 0, sizeof(*info));
1194 EXPORT_SYMBOL(xt_alloc_table_info);
1196 void xt_free_table_info(struct xt_table_info *info)
1200 if (info->jumpstack != NULL) {
1201 for_each_possible_cpu(cpu)
1202 kvfree(info->jumpstack[cpu]);
1203 kvfree(info->jumpstack);
1208 EXPORT_SYMBOL(xt_free_table_info);
1210 /* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
1211 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1214 struct xt_table *t, *found = NULL;
1216 mutex_lock(&xt[af].mutex);
1217 list_for_each_entry(t, &net->xt.tables[af], list)
1218 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1221 if (net == &init_net)
1224 /* Table doesn't exist in this netns, re-try init */
1225 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1228 if (strcmp(t->name, name))
1230 if (!try_module_get(t->me))
1232 mutex_unlock(&xt[af].mutex);
1233 err = t->table_init(net);
1236 return ERR_PTR(err);
1241 mutex_lock(&xt[af].mutex);
1248 /* and once again: */
1249 list_for_each_entry(t, &net->xt.tables[af], list)
1250 if (strcmp(t->name, name) == 0)
1253 module_put(found->me);
1255 mutex_unlock(&xt[af].mutex);
1256 return ERR_PTR(-ENOENT);
1258 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1260 struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1263 struct xt_table *t = xt_find_table_lock(net, af, name);
1265 #ifdef CONFIG_MODULES
1267 int err = request_module("%stable_%s", xt_prefix[af], name);
1269 return ERR_PTR(err);
1270 t = xt_find_table_lock(net, af, name);
1276 EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1278 void xt_table_unlock(struct xt_table *table)
1280 mutex_unlock(&xt[table->af].mutex);
1282 EXPORT_SYMBOL_GPL(xt_table_unlock);
1284 #ifdef CONFIG_COMPAT
1285 void xt_compat_lock(u_int8_t af)
1287 mutex_lock(&xt[af].compat_mutex);
1289 EXPORT_SYMBOL_GPL(xt_compat_lock);
1291 void xt_compat_unlock(u_int8_t af)
1293 mutex_unlock(&xt[af].compat_mutex);
1295 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1298 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1299 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1301 struct static_key xt_tee_enabled __read_mostly;
1302 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1304 static int xt_jumpstack_alloc(struct xt_table_info *i)
1309 size = sizeof(void **) * nr_cpu_ids;
1310 if (size > PAGE_SIZE)
1311 i->jumpstack = kvzalloc(size, GFP_KERNEL);
1313 i->jumpstack = kzalloc(size, GFP_KERNEL);
1314 if (i->jumpstack == NULL)
1317 /* ruleset without jumps -- no stack needed */
1318 if (i->stacksize == 0)
1321 /* Jumpstack needs to be able to record two full callchains, one
1322 * from the first rule set traversal, plus one table reentrancy
1323 * via -j TEE without clobbering the callchain that brought us to
1326 * This is done by allocating two jumpstacks per cpu, on reentry
1327 * the upper half of the stack is used.
1329 * see the jumpstack setup in ipt_do_table() for more details.
1331 size = sizeof(void *) * i->stacksize * 2u;
1332 for_each_possible_cpu(cpu) {
1333 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1335 if (i->jumpstack[cpu] == NULL)
1337 * Freeing will be done later on by the callers. The
1338 * chain is: xt_replace_table -> __do_replace ->
1339 * do_replace -> xt_free_table_info.
1347 struct xt_counters *xt_counters_alloc(unsigned int counters)
1349 struct xt_counters *mem;
1351 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1354 counters *= sizeof(*mem);
1355 if (counters > XT_MAX_TABLE_SIZE)
1358 return vzalloc(counters);
1360 EXPORT_SYMBOL(xt_counters_alloc);
1362 struct xt_table_info *
1363 xt_replace_table(struct xt_table *table,
1364 unsigned int num_counters,
1365 struct xt_table_info *newinfo,
1368 struct xt_table_info *private;
1372 ret = xt_jumpstack_alloc(newinfo);
1378 /* Do the substitution. */
1380 private = table->private;
1382 /* Check inside lock: is the old number correct? */
1383 if (num_counters != private->number) {
1384 pr_debug("num_counters != table->private->number (%u/%u)\n",
1385 num_counters, private->number);
1391 newinfo->initial_entries = private->initial_entries;
1393 * Ensure contents of newinfo are visible before assigning to
1397 table->private = newinfo;
1399 /* make sure all cpus see new ->private value */
1403 * Even though table entries have now been swapped, other CPU's
1404 * may still be using the old entries...
1408 /* ... so wait for even xt_recseq on all cpus */
1409 for_each_possible_cpu(cpu) {
1410 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1411 u32 seq = raw_read_seqcount(s);
1417 } while (seq == raw_read_seqcount(s));
1422 if (audit_enabled) {
1423 audit_log(audit_context(), GFP_KERNEL,
1424 AUDIT_NETFILTER_CFG,
1425 "table=%s family=%u entries=%u",
1426 table->name, table->af, private->number);
1432 EXPORT_SYMBOL_GPL(xt_replace_table);
1434 struct xt_table *xt_register_table(struct net *net,
1435 const struct xt_table *input_table,
1436 struct xt_table_info *bootstrap,
1437 struct xt_table_info *newinfo)
1440 struct xt_table_info *private;
1441 struct xt_table *t, *table;
1443 /* Don't add one object to multiple lists. */
1444 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1450 mutex_lock(&xt[table->af].mutex);
1451 /* Don't autoload: we'd eat our tail... */
1452 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1453 if (strcmp(t->name, table->name) == 0) {
1459 /* Simplifies replace_table code. */
1460 table->private = bootstrap;
1462 if (!xt_replace_table(table, 0, newinfo, &ret))
1465 private = table->private;
1466 pr_debug("table->private->number = %u\n", private->number);
1468 /* save number of initial entries */
1469 private->initial_entries = private->number;
1471 list_add(&table->list, &net->xt.tables[table->af]);
1472 mutex_unlock(&xt[table->af].mutex);
1476 mutex_unlock(&xt[table->af].mutex);
1479 return ERR_PTR(ret);
1481 EXPORT_SYMBOL_GPL(xt_register_table);
1483 void *xt_unregister_table(struct xt_table *table)
1485 struct xt_table_info *private;
1487 mutex_lock(&xt[table->af].mutex);
1488 private = table->private;
1489 list_del(&table->list);
1490 mutex_unlock(&xt[table->af].mutex);
1495 EXPORT_SYMBOL_GPL(xt_unregister_table);
1497 #ifdef CONFIG_PROC_FS
1498 static void *xt_table_seq_start(struct seq_file *seq, 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 mutex_lock(&xt[af].mutex);
1504 return seq_list_start(&net->xt.tables[af], *pos);
1507 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1509 struct net *net = seq_file_net(seq);
1510 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
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 u_int8_t af = (unsigned long)PDE_DATA(file_inode(seq->file));
1519 mutex_unlock(&xt[af].mutex);
1522 static int xt_table_seq_show(struct seq_file *seq, void *v)
1524 struct xt_table *table = list_entry(v, struct xt_table, list);
1527 seq_printf(seq, "%s\n", table->name);
1531 static const struct seq_operations xt_table_seq_ops = {
1532 .start = xt_table_seq_start,
1533 .next = xt_table_seq_next,
1534 .stop = xt_table_seq_stop,
1535 .show = xt_table_seq_show,
1539 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1540 * the multi-AF mutexes.
1542 struct nf_mttg_trav {
1543 struct list_head *head, *curr;
1549 MTTG_TRAV_NFP_UNSPEC,
1554 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1557 static const uint8_t next_class[] = {
1558 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1559 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1561 uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file));
1562 struct nf_mttg_trav *trav = seq->private;
1564 switch (trav->class) {
1565 case MTTG_TRAV_INIT:
1566 trav->class = MTTG_TRAV_NFP_UNSPEC;
1567 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1568 trav->head = trav->curr = is_target ?
1569 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1571 case MTTG_TRAV_NFP_UNSPEC:
1572 trav->curr = trav->curr->next;
1573 if (trav->curr != trav->head)
1575 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1576 mutex_lock(&xt[nfproto].mutex);
1577 trav->head = trav->curr = is_target ?
1578 &xt[nfproto].target : &xt[nfproto].match;
1579 trav->class = next_class[trav->class];
1581 case MTTG_TRAV_NFP_SPEC:
1582 trav->curr = trav->curr->next;
1583 if (trav->curr != trav->head)
1595 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1598 struct nf_mttg_trav *trav = seq->private;
1601 trav->class = MTTG_TRAV_INIT;
1602 for (j = 0; j < *pos; ++j)
1603 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1608 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1610 uint8_t nfproto = (unsigned long)PDE_DATA(file_inode(seq->file));
1611 struct nf_mttg_trav *trav = seq->private;
1613 switch (trav->class) {
1614 case MTTG_TRAV_NFP_UNSPEC:
1615 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1617 case MTTG_TRAV_NFP_SPEC:
1618 mutex_unlock(&xt[nfproto].mutex);
1623 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1625 return xt_mttg_seq_start(seq, pos, false);
1628 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1630 return xt_mttg_seq_next(seq, v, ppos, false);
1633 static int xt_match_seq_show(struct seq_file *seq, void *v)
1635 const struct nf_mttg_trav *trav = seq->private;
1636 const struct xt_match *match;
1638 switch (trav->class) {
1639 case MTTG_TRAV_NFP_UNSPEC:
1640 case MTTG_TRAV_NFP_SPEC:
1641 if (trav->curr == trav->head)
1643 match = list_entry(trav->curr, struct xt_match, list);
1645 seq_printf(seq, "%s\n", match->name);
1650 static const struct seq_operations xt_match_seq_ops = {
1651 .start = xt_match_seq_start,
1652 .next = xt_match_seq_next,
1653 .stop = xt_mttg_seq_stop,
1654 .show = xt_match_seq_show,
1657 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1659 return xt_mttg_seq_start(seq, pos, true);
1662 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1664 return xt_mttg_seq_next(seq, v, ppos, true);
1667 static int xt_target_seq_show(struct seq_file *seq, void *v)
1669 const struct nf_mttg_trav *trav = seq->private;
1670 const struct xt_target *target;
1672 switch (trav->class) {
1673 case MTTG_TRAV_NFP_UNSPEC:
1674 case MTTG_TRAV_NFP_SPEC:
1675 if (trav->curr == trav->head)
1677 target = list_entry(trav->curr, struct xt_target, list);
1679 seq_printf(seq, "%s\n", target->name);
1684 static const struct seq_operations xt_target_seq_ops = {
1685 .start = xt_target_seq_start,
1686 .next = xt_target_seq_next,
1687 .stop = xt_mttg_seq_stop,
1688 .show = xt_target_seq_show,
1691 #define FORMAT_TABLES "_tables_names"
1692 #define FORMAT_MATCHES "_tables_matches"
1693 #define FORMAT_TARGETS "_tables_targets"
1695 #endif /* CONFIG_PROC_FS */
1698 * xt_hook_ops_alloc - set up hooks for a new table
1699 * @table: table with metadata needed to set up hooks
1700 * @fn: Hook function
1702 * This function will create the nf_hook_ops that the x_table needs
1703 * to hand to xt_hook_link_net().
1705 struct nf_hook_ops *
1706 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1708 unsigned int hook_mask = table->valid_hooks;
1709 uint8_t i, num_hooks = hweight32(hook_mask);
1711 struct nf_hook_ops *ops;
1714 return ERR_PTR(-EINVAL);
1716 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1718 return ERR_PTR(-ENOMEM);
1720 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1721 hook_mask >>= 1, ++hooknum) {
1722 if (!(hook_mask & 1))
1725 ops[i].pf = table->af;
1726 ops[i].hooknum = hooknum;
1727 ops[i].priority = table->priority;
1733 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1735 int xt_proto_init(struct net *net, u_int8_t af)
1737 #ifdef CONFIG_PROC_FS
1738 char buf[XT_FUNCTION_MAXNAMELEN];
1739 struct proc_dir_entry *proc;
1744 if (af >= ARRAY_SIZE(xt_prefix))
1748 #ifdef CONFIG_PROC_FS
1749 root_uid = make_kuid(net->user_ns, 0);
1750 root_gid = make_kgid(net->user_ns, 0);
1752 strlcpy(buf, xt_prefix[af], sizeof(buf));
1753 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1754 proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops,
1755 sizeof(struct seq_net_private),
1756 (void *)(unsigned long)af);
1759 if (uid_valid(root_uid) && gid_valid(root_gid))
1760 proc_set_user(proc, root_uid, root_gid);
1762 strlcpy(buf, xt_prefix[af], sizeof(buf));
1763 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1764 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1765 &xt_match_seq_ops, sizeof(struct nf_mttg_trav),
1766 (void *)(unsigned long)af);
1768 goto out_remove_tables;
1769 if (uid_valid(root_uid) && gid_valid(root_gid))
1770 proc_set_user(proc, root_uid, root_gid);
1772 strlcpy(buf, xt_prefix[af], sizeof(buf));
1773 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1774 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1775 &xt_target_seq_ops, sizeof(struct nf_mttg_trav),
1776 (void *)(unsigned long)af);
1778 goto out_remove_matches;
1779 if (uid_valid(root_uid) && gid_valid(root_gid))
1780 proc_set_user(proc, root_uid, root_gid);
1785 #ifdef CONFIG_PROC_FS
1787 strlcpy(buf, xt_prefix[af], sizeof(buf));
1788 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1789 remove_proc_entry(buf, net->proc_net);
1792 strlcpy(buf, xt_prefix[af], sizeof(buf));
1793 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1794 remove_proc_entry(buf, net->proc_net);
1799 EXPORT_SYMBOL_GPL(xt_proto_init);
1801 void xt_proto_fini(struct net *net, u_int8_t af)
1803 #ifdef CONFIG_PROC_FS
1804 char buf[XT_FUNCTION_MAXNAMELEN];
1806 strlcpy(buf, xt_prefix[af], sizeof(buf));
1807 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1808 remove_proc_entry(buf, net->proc_net);
1810 strlcpy(buf, xt_prefix[af], sizeof(buf));
1811 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1812 remove_proc_entry(buf, net->proc_net);
1814 strlcpy(buf, xt_prefix[af], sizeof(buf));
1815 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1816 remove_proc_entry(buf, net->proc_net);
1817 #endif /*CONFIG_PROC_FS*/
1819 EXPORT_SYMBOL_GPL(xt_proto_fini);
1822 * xt_percpu_counter_alloc - allocate x_tables rule counter
1824 * @state: pointer to xt_percpu allocation state
1825 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1827 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1828 * contain the address of the real (percpu) counter.
1830 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1831 * to fetch the real percpu counter.
1833 * To speed up allocation and improve data locality, a 4kb block is
1834 * allocated. Freeing any counter may free an entire block, so all
1835 * counters allocated using the same state must be freed at the same
1838 * xt_percpu_counter_alloc_state contains the base address of the
1839 * allocated page and the current sub-offset.
1841 * returns false on error.
1843 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1844 struct xt_counters *counter)
1846 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1848 if (nr_cpu_ids <= 1)
1852 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1853 XT_PCPU_BLOCK_SIZE);
1857 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1858 state->off += sizeof(*counter);
1859 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1865 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1867 void xt_percpu_counter_free(struct xt_counters *counters)
1869 unsigned long pcnt = counters->pcnt;
1871 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1872 free_percpu((void __percpu *)pcnt);
1874 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1876 static int __net_init xt_net_init(struct net *net)
1880 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1881 INIT_LIST_HEAD(&net->xt.tables[i]);
1885 static void __net_exit xt_net_exit(struct net *net)
1889 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1890 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1893 static struct pernet_operations xt_net_ops = {
1894 .init = xt_net_init,
1895 .exit = xt_net_exit,
1898 static int __init xt_init(void)
1903 for_each_possible_cpu(i) {
1904 seqcount_init(&per_cpu(xt_recseq, i));
1907 xt = kmalloc_array(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
1911 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1912 mutex_init(&xt[i].mutex);
1913 #ifdef CONFIG_COMPAT
1914 mutex_init(&xt[i].compat_mutex);
1915 xt[i].compat_tab = NULL;
1917 INIT_LIST_HEAD(&xt[i].target);
1918 INIT_LIST_HEAD(&xt[i].match);
1920 rv = register_pernet_subsys(&xt_net_ops);
1926 static void __exit xt_fini(void)
1928 unregister_pernet_subsys(&xt_net_ops);
1932 module_init(xt_init);
1933 module_exit(xt_fini);