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 SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
45 unsigned int offset; /* offset in kernel */
46 int delta; /* delta in 32bit user land */
51 struct list_head match;
52 struct list_head target;
54 struct mutex compat_mutex;
55 struct compat_delta *compat_tab;
56 unsigned int number; /* number of slots in compat_tab[] */
57 unsigned int cur; /* number of used slots in compat_tab[] */
61 static struct xt_af *xt;
63 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
64 [NFPROTO_UNSPEC] = "x",
65 [NFPROTO_IPV4] = "ip",
66 [NFPROTO_ARP] = "arp",
67 [NFPROTO_BRIDGE] = "eb",
68 [NFPROTO_IPV6] = "ip6",
71 /* Registration hooks for targets. */
72 int xt_register_target(struct xt_target *target)
74 u_int8_t af = target->family;
76 mutex_lock(&xt[af].mutex);
77 list_add(&target->list, &xt[af].target);
78 mutex_unlock(&xt[af].mutex);
81 EXPORT_SYMBOL(xt_register_target);
84 xt_unregister_target(struct xt_target *target)
86 u_int8_t af = target->family;
88 mutex_lock(&xt[af].mutex);
89 list_del(&target->list);
90 mutex_unlock(&xt[af].mutex);
92 EXPORT_SYMBOL(xt_unregister_target);
95 xt_register_targets(struct xt_target *target, unsigned int n)
100 for (i = 0; i < n; i++) {
101 err = xt_register_target(&target[i]);
109 xt_unregister_targets(target, i);
112 EXPORT_SYMBOL(xt_register_targets);
115 xt_unregister_targets(struct xt_target *target, unsigned int n)
118 xt_unregister_target(&target[n]);
120 EXPORT_SYMBOL(xt_unregister_targets);
122 int xt_register_match(struct xt_match *match)
124 u_int8_t af = match->family;
126 mutex_lock(&xt[af].mutex);
127 list_add(&match->list, &xt[af].match);
128 mutex_unlock(&xt[af].mutex);
131 EXPORT_SYMBOL(xt_register_match);
134 xt_unregister_match(struct xt_match *match)
136 u_int8_t af = match->family;
138 mutex_lock(&xt[af].mutex);
139 list_del(&match->list);
140 mutex_unlock(&xt[af].mutex);
142 EXPORT_SYMBOL(xt_unregister_match);
145 xt_register_matches(struct xt_match *match, unsigned int n)
150 for (i = 0; i < n; i++) {
151 err = xt_register_match(&match[i]);
159 xt_unregister_matches(match, i);
162 EXPORT_SYMBOL(xt_register_matches);
165 xt_unregister_matches(struct xt_match *match, unsigned int n)
168 xt_unregister_match(&match[n]);
170 EXPORT_SYMBOL(xt_unregister_matches);
174 * These are weird, but module loading must not be done with mutex
175 * held (since they will register), and we have to have a single
179 /* Find match, grabs ref. Returns ERR_PTR() on error. */
180 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
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 match = xt_find_match(nfproto, name, revision);
214 request_module("%st_%s", xt_prefix[nfproto], name);
215 match = xt_find_match(nfproto, name, revision);
220 EXPORT_SYMBOL_GPL(xt_request_find_match);
222 /* Find target, grabs ref. Returns ERR_PTR() on error. */
223 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
228 mutex_lock(&xt[af].mutex);
229 list_for_each_entry(t, &xt[af].target, list) {
230 if (strcmp(t->name, name) == 0) {
231 if (t->revision == revision) {
232 if (try_module_get(t->me)) {
233 mutex_unlock(&xt[af].mutex);
237 err = -EPROTOTYPE; /* Found something. */
240 mutex_unlock(&xt[af].mutex);
242 if (af != NFPROTO_UNSPEC)
243 /* Try searching again in the family-independent list */
244 return xt_find_target(NFPROTO_UNSPEC, name, revision);
248 EXPORT_SYMBOL(xt_find_target);
250 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
252 struct xt_target *target;
254 target = xt_find_target(af, name, revision);
255 if (IS_ERR(target)) {
256 request_module("%st_%s", xt_prefix[af], name);
257 target = xt_find_target(af, name, revision);
262 EXPORT_SYMBOL_GPL(xt_request_find_target);
264 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
266 const struct xt_match *m;
269 list_for_each_entry(m, &xt[af].match, list) {
270 if (strcmp(m->name, name) == 0) {
271 if (m->revision > *bestp)
272 *bestp = m->revision;
273 if (m->revision == revision)
278 if (af != NFPROTO_UNSPEC && !have_rev)
279 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
284 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
286 const struct xt_target *t;
289 list_for_each_entry(t, &xt[af].target, list) {
290 if (strcmp(t->name, name) == 0) {
291 if (t->revision > *bestp)
292 *bestp = t->revision;
293 if (t->revision == revision)
298 if (af != NFPROTO_UNSPEC && !have_rev)
299 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
304 /* Returns true or false (if no such extension at all) */
305 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
308 int have_rev, best = -1;
310 mutex_lock(&xt[af].mutex);
312 have_rev = target_revfn(af, name, revision, &best);
314 have_rev = match_revfn(af, name, revision, &best);
315 mutex_unlock(&xt[af].mutex);
317 /* Nothing at all? Return 0 to try loading module. */
325 *err = -EPROTONOSUPPORT;
328 EXPORT_SYMBOL_GPL(xt_find_revision);
331 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
333 static const char *const inetbr_names[] = {
334 "PREROUTING", "INPUT", "FORWARD",
335 "OUTPUT", "POSTROUTING", "BROUTING",
337 static const char *const arp_names[] = {
338 "INPUT", "FORWARD", "OUTPUT",
340 const char *const *names;
346 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
347 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
348 ARRAY_SIZE(inetbr_names);
350 for (i = 0; i < max; ++i) {
351 if (!(mask & (1 << i)))
353 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
364 int xt_check_match(struct xt_mtchk_param *par,
365 unsigned int size, u_int8_t proto, bool inv_proto)
369 if (XT_ALIGN(par->match->matchsize) != size &&
370 par->match->matchsize != -1) {
372 * ebt_among is exempt from centralized matchsize checking
373 * because it uses a dynamic-size data set.
375 pr_err("%s_tables: %s.%u match: invalid size "
376 "%u (kernel) != (user) %u\n",
377 xt_prefix[par->family], par->match->name,
378 par->match->revision,
379 XT_ALIGN(par->match->matchsize), size);
382 if (par->match->table != NULL &&
383 strcmp(par->match->table, par->table) != 0) {
384 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
385 xt_prefix[par->family], par->match->name,
386 par->match->table, par->table);
389 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
390 char used[64], allow[64];
392 pr_err("%s_tables: %s match: used from hooks %s, but only "
394 xt_prefix[par->family], par->match->name,
395 textify_hooks(used, sizeof(used), par->hook_mask,
397 textify_hooks(allow, sizeof(allow), par->match->hooks,
401 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
402 pr_err("%s_tables: %s match: only valid for protocol %u\n",
403 xt_prefix[par->family], par->match->name,
407 if (par->match->checkentry != NULL) {
408 ret = par->match->checkentry(par);
412 /* Flag up potential errors. */
417 EXPORT_SYMBOL_GPL(xt_check_match);
420 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
422 struct xt_af *xp = &xt[af];
424 if (!xp->compat_tab) {
427 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
433 if (xp->cur >= xp->number)
437 delta += xp->compat_tab[xp->cur - 1].delta;
438 xp->compat_tab[xp->cur].offset = offset;
439 xp->compat_tab[xp->cur].delta = delta;
443 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
445 void xt_compat_flush_offsets(u_int8_t af)
447 if (xt[af].compat_tab) {
448 vfree(xt[af].compat_tab);
449 xt[af].compat_tab = NULL;
454 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
456 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
458 struct compat_delta *tmp = xt[af].compat_tab;
459 int mid, left = 0, right = xt[af].cur - 1;
461 while (left <= right) {
462 mid = (left + right) >> 1;
463 if (offset > tmp[mid].offset)
465 else if (offset < tmp[mid].offset)
468 return mid ? tmp[mid - 1].delta : 0;
470 return left ? tmp[left - 1].delta : 0;
472 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
474 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
476 xt[af].number = number;
479 EXPORT_SYMBOL(xt_compat_init_offsets);
481 int xt_compat_match_offset(const struct xt_match *match)
483 u_int16_t csize = match->compatsize ? : match->matchsize;
484 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
486 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
488 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
491 const struct xt_match *match = m->u.kernel.match;
492 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
493 int pad, off = xt_compat_match_offset(match);
494 u_int16_t msize = cm->u.user.match_size;
497 memcpy(m, cm, sizeof(*cm));
498 if (match->compat_from_user)
499 match->compat_from_user(m->data, cm->data);
501 memcpy(m->data, cm->data, msize - sizeof(*cm));
502 pad = XT_ALIGN(match->matchsize) - match->matchsize;
504 memset(m->data + match->matchsize, 0, pad);
507 m->u.user.match_size = msize;
513 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
515 int xt_compat_match_to_user(const struct xt_entry_match *m,
516 void __user **dstptr, unsigned int *size)
518 const struct xt_match *match = m->u.kernel.match;
519 struct compat_xt_entry_match __user *cm = *dstptr;
520 int off = xt_compat_match_offset(match);
521 u_int16_t msize = m->u.user.match_size - off;
523 if (copy_to_user(cm, m, sizeof(*cm)) ||
524 put_user(msize, &cm->u.user.match_size) ||
525 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
526 strlen(m->u.kernel.match->name) + 1))
529 if (match->compat_to_user) {
530 if (match->compat_to_user((void __user *)cm->data, m->data))
533 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
541 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
542 #endif /* CONFIG_COMPAT */
544 int xt_check_target(struct xt_tgchk_param *par,
545 unsigned int size, u_int8_t proto, bool inv_proto)
549 if (XT_ALIGN(par->target->targetsize) != size) {
550 pr_err("%s_tables: %s.%u target: invalid size "
551 "%u (kernel) != (user) %u\n",
552 xt_prefix[par->family], par->target->name,
553 par->target->revision,
554 XT_ALIGN(par->target->targetsize), size);
557 if (par->target->table != NULL &&
558 strcmp(par->target->table, par->table) != 0) {
559 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
560 xt_prefix[par->family], par->target->name,
561 par->target->table, par->table);
564 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
565 char used[64], allow[64];
567 pr_err("%s_tables: %s target: used from hooks %s, but only "
569 xt_prefix[par->family], par->target->name,
570 textify_hooks(used, sizeof(used), par->hook_mask,
572 textify_hooks(allow, sizeof(allow), par->target->hooks,
576 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
577 pr_err("%s_tables: %s target: only valid for protocol %u\n",
578 xt_prefix[par->family], par->target->name,
582 if (par->target->checkentry != NULL) {
583 ret = par->target->checkentry(par);
587 /* Flag up potential errors. */
592 EXPORT_SYMBOL_GPL(xt_check_target);
595 int xt_compat_target_offset(const struct xt_target *target)
597 u_int16_t csize = target->compatsize ? : target->targetsize;
598 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
600 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
602 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
605 const struct xt_target *target = t->u.kernel.target;
606 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
607 int pad, off = xt_compat_target_offset(target);
608 u_int16_t tsize = ct->u.user.target_size;
611 memcpy(t, ct, sizeof(*ct));
612 if (target->compat_from_user)
613 target->compat_from_user(t->data, ct->data);
615 memcpy(t->data, ct->data, tsize - sizeof(*ct));
616 pad = XT_ALIGN(target->targetsize) - target->targetsize;
618 memset(t->data + target->targetsize, 0, pad);
621 t->u.user.target_size = tsize;
626 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
628 int xt_compat_target_to_user(const struct xt_entry_target *t,
629 void __user **dstptr, unsigned int *size)
631 const struct xt_target *target = t->u.kernel.target;
632 struct compat_xt_entry_target __user *ct = *dstptr;
633 int off = xt_compat_target_offset(target);
634 u_int16_t tsize = t->u.user.target_size - off;
636 if (copy_to_user(ct, t, sizeof(*ct)) ||
637 put_user(tsize, &ct->u.user.target_size) ||
638 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
639 strlen(t->u.kernel.target->name) + 1))
642 if (target->compat_to_user) {
643 if (target->compat_to_user((void __user *)ct->data, t->data))
646 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
654 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
657 struct xt_table_info *xt_alloc_table_info(unsigned int size)
659 struct xt_table_info *info = NULL;
660 size_t sz = sizeof(*info) + size;
662 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
663 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
666 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
667 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
673 memset(info, 0, sizeof(*info));
677 EXPORT_SYMBOL(xt_alloc_table_info);
679 void xt_free_table_info(struct xt_table_info *info)
683 if (info->jumpstack != NULL) {
684 for_each_possible_cpu(cpu)
685 kvfree(info->jumpstack[cpu]);
686 kvfree(info->jumpstack);
691 EXPORT_SYMBOL(xt_free_table_info);
693 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
694 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
699 mutex_lock(&xt[af].mutex);
700 list_for_each_entry(t, &net->xt.tables[af], list)
701 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
703 mutex_unlock(&xt[af].mutex);
706 EXPORT_SYMBOL_GPL(xt_find_table_lock);
708 void xt_table_unlock(struct xt_table *table)
710 mutex_unlock(&xt[table->af].mutex);
712 EXPORT_SYMBOL_GPL(xt_table_unlock);
715 void xt_compat_lock(u_int8_t af)
717 mutex_lock(&xt[af].compat_mutex);
719 EXPORT_SYMBOL_GPL(xt_compat_lock);
721 void xt_compat_unlock(u_int8_t af)
723 mutex_unlock(&xt[af].compat_mutex);
725 EXPORT_SYMBOL_GPL(xt_compat_unlock);
728 DEFINE_PER_CPU(seqcount_t, xt_recseq);
729 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
731 struct static_key xt_tee_enabled __read_mostly;
732 EXPORT_SYMBOL_GPL(xt_tee_enabled);
734 static int xt_jumpstack_alloc(struct xt_table_info *i)
739 size = sizeof(void **) * nr_cpu_ids;
740 if (size > PAGE_SIZE)
741 i->jumpstack = vzalloc(size);
743 i->jumpstack = kzalloc(size, GFP_KERNEL);
744 if (i->jumpstack == NULL)
747 /* ruleset without jumps -- no stack needed */
748 if (i->stacksize == 0)
751 /* Jumpstack needs to be able to record two full callchains, one
752 * from the first rule set traversal, plus one table reentrancy
753 * via -j TEE without clobbering the callchain that brought us to
756 * This is done by allocating two jumpstacks per cpu, on reentry
757 * the upper half of the stack is used.
759 * see the jumpstack setup in ipt_do_table() for more details.
761 size = sizeof(void *) * i->stacksize * 2u;
762 for_each_possible_cpu(cpu) {
763 if (size > PAGE_SIZE)
764 i->jumpstack[cpu] = vmalloc_node(size,
767 i->jumpstack[cpu] = kmalloc_node(size,
768 GFP_KERNEL, cpu_to_node(cpu));
769 if (i->jumpstack[cpu] == NULL)
771 * Freeing will be done later on by the callers. The
772 * chain is: xt_replace_table -> __do_replace ->
773 * do_replace -> xt_free_table_info.
781 struct xt_table_info *
782 xt_replace_table(struct xt_table *table,
783 unsigned int num_counters,
784 struct xt_table_info *newinfo,
787 struct xt_table_info *private;
790 ret = xt_jumpstack_alloc(newinfo);
796 /* Do the substitution. */
798 private = table->private;
800 /* Check inside lock: is the old number correct? */
801 if (num_counters != private->number) {
802 pr_debug("num_counters != table->private->number (%u/%u)\n",
803 num_counters, private->number);
809 newinfo->initial_entries = private->initial_entries;
811 * Ensure contents of newinfo are visible before assigning to
815 table->private = newinfo;
818 * Even though table entries have now been swapped, other CPU's
819 * may still be using the old entries. This is okay, because
820 * resynchronization happens because of the locking done
821 * during the get_counters() routine.
827 struct audit_buffer *ab;
829 ab = audit_log_start(current->audit_context, GFP_KERNEL,
830 AUDIT_NETFILTER_CFG);
832 audit_log_format(ab, "table=%s family=%u entries=%u",
833 table->name, table->af,
842 EXPORT_SYMBOL_GPL(xt_replace_table);
844 struct xt_table *xt_register_table(struct net *net,
845 const struct xt_table *input_table,
846 struct xt_table_info *bootstrap,
847 struct xt_table_info *newinfo)
850 struct xt_table_info *private;
851 struct xt_table *t, *table;
853 /* Don't add one object to multiple lists. */
854 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
860 mutex_lock(&xt[table->af].mutex);
861 /* Don't autoload: we'd eat our tail... */
862 list_for_each_entry(t, &net->xt.tables[table->af], list) {
863 if (strcmp(t->name, table->name) == 0) {
869 /* Simplifies replace_table code. */
870 table->private = bootstrap;
872 if (!xt_replace_table(table, 0, newinfo, &ret))
875 private = table->private;
876 pr_debug("table->private->number = %u\n", private->number);
878 /* save number of initial entries */
879 private->initial_entries = private->number;
881 list_add(&table->list, &net->xt.tables[table->af]);
882 mutex_unlock(&xt[table->af].mutex);
886 mutex_unlock(&xt[table->af].mutex);
891 EXPORT_SYMBOL_GPL(xt_register_table);
893 void *xt_unregister_table(struct xt_table *table)
895 struct xt_table_info *private;
897 mutex_lock(&xt[table->af].mutex);
898 private = table->private;
899 list_del(&table->list);
900 mutex_unlock(&xt[table->af].mutex);
905 EXPORT_SYMBOL_GPL(xt_unregister_table);
907 #ifdef CONFIG_PROC_FS
908 struct xt_names_priv {
909 struct seq_net_private p;
912 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
914 struct xt_names_priv *priv = seq->private;
915 struct net *net = seq_file_net(seq);
916 u_int8_t af = priv->af;
918 mutex_lock(&xt[af].mutex);
919 return seq_list_start(&net->xt.tables[af], *pos);
922 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
924 struct xt_names_priv *priv = seq->private;
925 struct net *net = seq_file_net(seq);
926 u_int8_t af = priv->af;
928 return seq_list_next(v, &net->xt.tables[af], pos);
931 static void xt_table_seq_stop(struct seq_file *seq, void *v)
933 struct xt_names_priv *priv = seq->private;
934 u_int8_t af = priv->af;
936 mutex_unlock(&xt[af].mutex);
939 static int xt_table_seq_show(struct seq_file *seq, void *v)
941 struct xt_table *table = list_entry(v, struct xt_table, list);
944 seq_printf(seq, "%s\n", table->name);
948 static const struct seq_operations xt_table_seq_ops = {
949 .start = xt_table_seq_start,
950 .next = xt_table_seq_next,
951 .stop = xt_table_seq_stop,
952 .show = xt_table_seq_show,
955 static int xt_table_open(struct inode *inode, struct file *file)
958 struct xt_names_priv *priv;
960 ret = seq_open_net(inode, file, &xt_table_seq_ops,
961 sizeof(struct xt_names_priv));
963 priv = ((struct seq_file *)file->private_data)->private;
964 priv->af = (unsigned long)PDE_DATA(inode);
969 static const struct file_operations xt_table_ops = {
970 .owner = THIS_MODULE,
971 .open = xt_table_open,
974 .release = seq_release_net,
978 * Traverse state for ip{,6}_{tables,matches} for helping crossing
979 * the multi-AF mutexes.
981 struct nf_mttg_trav {
982 struct list_head *head, *curr;
983 uint8_t class, nfproto;
988 MTTG_TRAV_NFP_UNSPEC,
993 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
996 static const uint8_t next_class[] = {
997 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
998 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1000 struct nf_mttg_trav *trav = seq->private;
1002 switch (trav->class) {
1003 case MTTG_TRAV_INIT:
1004 trav->class = MTTG_TRAV_NFP_UNSPEC;
1005 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1006 trav->head = trav->curr = is_target ?
1007 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1009 case MTTG_TRAV_NFP_UNSPEC:
1010 trav->curr = trav->curr->next;
1011 if (trav->curr != trav->head)
1013 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1014 mutex_lock(&xt[trav->nfproto].mutex);
1015 trav->head = trav->curr = is_target ?
1016 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1017 trav->class = next_class[trav->class];
1019 case MTTG_TRAV_NFP_SPEC:
1020 trav->curr = trav->curr->next;
1021 if (trav->curr != trav->head)
1023 /* fallthru, _stop will unlock */
1033 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1036 struct nf_mttg_trav *trav = seq->private;
1039 trav->class = MTTG_TRAV_INIT;
1040 for (j = 0; j < *pos; ++j)
1041 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1046 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1048 struct nf_mttg_trav *trav = seq->private;
1050 switch (trav->class) {
1051 case MTTG_TRAV_NFP_UNSPEC:
1052 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1054 case MTTG_TRAV_NFP_SPEC:
1055 mutex_unlock(&xt[trav->nfproto].mutex);
1060 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1062 return xt_mttg_seq_start(seq, pos, false);
1065 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1067 return xt_mttg_seq_next(seq, v, ppos, false);
1070 static int xt_match_seq_show(struct seq_file *seq, void *v)
1072 const struct nf_mttg_trav *trav = seq->private;
1073 const struct xt_match *match;
1075 switch (trav->class) {
1076 case MTTG_TRAV_NFP_UNSPEC:
1077 case MTTG_TRAV_NFP_SPEC:
1078 if (trav->curr == trav->head)
1080 match = list_entry(trav->curr, struct xt_match, list);
1082 seq_printf(seq, "%s\n", match->name);
1087 static const struct seq_operations xt_match_seq_ops = {
1088 .start = xt_match_seq_start,
1089 .next = xt_match_seq_next,
1090 .stop = xt_mttg_seq_stop,
1091 .show = xt_match_seq_show,
1094 static int xt_match_open(struct inode *inode, struct file *file)
1096 struct nf_mttg_trav *trav;
1097 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1101 trav->nfproto = (unsigned long)PDE_DATA(inode);
1105 static const struct file_operations xt_match_ops = {
1106 .owner = THIS_MODULE,
1107 .open = xt_match_open,
1109 .llseek = seq_lseek,
1110 .release = seq_release_private,
1113 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1115 return xt_mttg_seq_start(seq, pos, true);
1118 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1120 return xt_mttg_seq_next(seq, v, ppos, true);
1123 static int xt_target_seq_show(struct seq_file *seq, void *v)
1125 const struct nf_mttg_trav *trav = seq->private;
1126 const struct xt_target *target;
1128 switch (trav->class) {
1129 case MTTG_TRAV_NFP_UNSPEC:
1130 case MTTG_TRAV_NFP_SPEC:
1131 if (trav->curr == trav->head)
1133 target = list_entry(trav->curr, struct xt_target, list);
1135 seq_printf(seq, "%s\n", target->name);
1140 static const struct seq_operations xt_target_seq_ops = {
1141 .start = xt_target_seq_start,
1142 .next = xt_target_seq_next,
1143 .stop = xt_mttg_seq_stop,
1144 .show = xt_target_seq_show,
1147 static int xt_target_open(struct inode *inode, struct file *file)
1149 struct nf_mttg_trav *trav;
1150 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1154 trav->nfproto = (unsigned long)PDE_DATA(inode);
1158 static const struct file_operations xt_target_ops = {
1159 .owner = THIS_MODULE,
1160 .open = xt_target_open,
1162 .llseek = seq_lseek,
1163 .release = seq_release_private,
1166 #define FORMAT_TABLES "_tables_names"
1167 #define FORMAT_MATCHES "_tables_matches"
1168 #define FORMAT_TARGETS "_tables_targets"
1170 #endif /* CONFIG_PROC_FS */
1173 * xt_hook_link - set up hooks for a new table
1174 * @table: table with metadata needed to set up hooks
1175 * @fn: Hook function
1177 * This function will take care of creating and registering the necessary
1178 * Netfilter hooks for XT tables.
1180 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1182 unsigned int hook_mask = table->valid_hooks;
1183 uint8_t i, num_hooks = hweight32(hook_mask);
1185 struct nf_hook_ops *ops;
1188 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1190 return ERR_PTR(-ENOMEM);
1192 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1193 hook_mask >>= 1, ++hooknum) {
1194 if (!(hook_mask & 1))
1197 ops[i].pf = table->af;
1198 ops[i].hooknum = hooknum;
1199 ops[i].priority = table->priority;
1203 ret = nf_register_hooks(ops, num_hooks);
1206 return ERR_PTR(ret);
1211 EXPORT_SYMBOL_GPL(xt_hook_link);
1214 * xt_hook_unlink - remove hooks for a table
1215 * @ops: nf_hook_ops array as returned by nf_hook_link
1216 * @hook_mask: the very same mask that was passed to nf_hook_link
1218 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1220 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1223 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1225 int xt_proto_init(struct net *net, u_int8_t af)
1227 #ifdef CONFIG_PROC_FS
1228 char buf[XT_FUNCTION_MAXNAMELEN];
1229 struct proc_dir_entry *proc;
1234 if (af >= ARRAY_SIZE(xt_prefix))
1238 #ifdef CONFIG_PROC_FS
1239 root_uid = make_kuid(net->user_ns, 0);
1240 root_gid = make_kgid(net->user_ns, 0);
1242 strlcpy(buf, xt_prefix[af], sizeof(buf));
1243 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1244 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1245 (void *)(unsigned long)af);
1248 if (uid_valid(root_uid) && gid_valid(root_gid))
1249 proc_set_user(proc, root_uid, root_gid);
1251 strlcpy(buf, xt_prefix[af], sizeof(buf));
1252 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1253 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1254 (void *)(unsigned long)af);
1256 goto out_remove_tables;
1257 if (uid_valid(root_uid) && gid_valid(root_gid))
1258 proc_set_user(proc, root_uid, root_gid);
1260 strlcpy(buf, xt_prefix[af], sizeof(buf));
1261 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1262 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1263 (void *)(unsigned long)af);
1265 goto out_remove_matches;
1266 if (uid_valid(root_uid) && gid_valid(root_gid))
1267 proc_set_user(proc, root_uid, root_gid);
1272 #ifdef CONFIG_PROC_FS
1274 strlcpy(buf, xt_prefix[af], sizeof(buf));
1275 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1276 remove_proc_entry(buf, net->proc_net);
1279 strlcpy(buf, xt_prefix[af], sizeof(buf));
1280 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1281 remove_proc_entry(buf, net->proc_net);
1286 EXPORT_SYMBOL_GPL(xt_proto_init);
1288 void xt_proto_fini(struct net *net, u_int8_t af)
1290 #ifdef CONFIG_PROC_FS
1291 char buf[XT_FUNCTION_MAXNAMELEN];
1293 strlcpy(buf, xt_prefix[af], sizeof(buf));
1294 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1295 remove_proc_entry(buf, net->proc_net);
1297 strlcpy(buf, xt_prefix[af], sizeof(buf));
1298 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1299 remove_proc_entry(buf, net->proc_net);
1301 strlcpy(buf, xt_prefix[af], sizeof(buf));
1302 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1303 remove_proc_entry(buf, net->proc_net);
1304 #endif /*CONFIG_PROC_FS*/
1306 EXPORT_SYMBOL_GPL(xt_proto_fini);
1308 static int __net_init xt_net_init(struct net *net)
1312 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1313 INIT_LIST_HEAD(&net->xt.tables[i]);
1317 static struct pernet_operations xt_net_ops = {
1318 .init = xt_net_init,
1321 static int __init xt_init(void)
1326 for_each_possible_cpu(i) {
1327 seqcount_init(&per_cpu(xt_recseq, i));
1330 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1334 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1335 mutex_init(&xt[i].mutex);
1336 #ifdef CONFIG_COMPAT
1337 mutex_init(&xt[i].compat_mutex);
1338 xt[i].compat_tab = NULL;
1340 INIT_LIST_HEAD(&xt[i].target);
1341 INIT_LIST_HEAD(&xt[i].match);
1343 rv = register_pernet_subsys(&xt_net_ops);
1349 static void __exit xt_fini(void)
1351 unregister_pernet_subsys(&xt_net_ops);
1355 module_init(xt_init);
1356 module_exit(xt_fini);