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 <net/net_namespace.h>
31 #include <linux/netfilter/x_tables.h>
32 #include <linux/netfilter_arp.h>
33 #include <linux/netfilter_ipv4/ip_tables.h>
34 #include <linux/netfilter_ipv6/ip6_tables.h>
35 #include <linux/netfilter_arp/arp_tables.h>
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
39 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
41 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
44 unsigned int offset; /* offset in kernel */
45 int delta; /* delta in 32bit user land */
50 struct list_head match;
51 struct list_head target;
53 struct mutex compat_mutex;
54 struct compat_delta *compat_tab;
55 unsigned int number; /* number of slots in compat_tab[] */
56 unsigned int cur; /* number of used slots in compat_tab[] */
60 static struct xt_af *xt;
62 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
63 [NFPROTO_UNSPEC] = "x",
64 [NFPROTO_IPV4] = "ip",
65 [NFPROTO_ARP] = "arp",
66 [NFPROTO_BRIDGE] = "eb",
67 [NFPROTO_IPV6] = "ip6",
70 /* Registration hooks for targets. */
71 int xt_register_target(struct xt_target *target)
73 u_int8_t af = target->family;
75 mutex_lock(&xt[af].mutex);
76 list_add(&target->list, &xt[af].target);
77 mutex_unlock(&xt[af].mutex);
80 EXPORT_SYMBOL(xt_register_target);
83 xt_unregister_target(struct xt_target *target)
85 u_int8_t af = target->family;
87 mutex_lock(&xt[af].mutex);
88 list_del(&target->list);
89 mutex_unlock(&xt[af].mutex);
91 EXPORT_SYMBOL(xt_unregister_target);
94 xt_register_targets(struct xt_target *target, unsigned int n)
99 for (i = 0; i < n; i++) {
100 err = xt_register_target(&target[i]);
108 xt_unregister_targets(target, i);
111 EXPORT_SYMBOL(xt_register_targets);
114 xt_unregister_targets(struct xt_target *target, unsigned int n)
117 xt_unregister_target(&target[n]);
119 EXPORT_SYMBOL(xt_unregister_targets);
121 int xt_register_match(struct xt_match *match)
123 u_int8_t af = match->family;
125 mutex_lock(&xt[af].mutex);
126 list_add(&match->list, &xt[af].match);
127 mutex_unlock(&xt[af].mutex);
130 EXPORT_SYMBOL(xt_register_match);
133 xt_unregister_match(struct xt_match *match)
135 u_int8_t af = match->family;
137 mutex_lock(&xt[af].mutex);
138 list_del(&match->list);
139 mutex_unlock(&xt[af].mutex);
141 EXPORT_SYMBOL(xt_unregister_match);
144 xt_register_matches(struct xt_match *match, unsigned int n)
149 for (i = 0; i < n; i++) {
150 err = xt_register_match(&match[i]);
158 xt_unregister_matches(match, i);
161 EXPORT_SYMBOL(xt_register_matches);
164 xt_unregister_matches(struct xt_match *match, unsigned int n)
167 xt_unregister_match(&match[n]);
169 EXPORT_SYMBOL(xt_unregister_matches);
173 * These are weird, but module loading must not be done with mutex
174 * held (since they will register), and we have to have a single
178 /* Find match, grabs ref. Returns ERR_PTR() on error. */
179 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
184 mutex_lock(&xt[af].mutex);
185 list_for_each_entry(m, &xt[af].match, list) {
186 if (strcmp(m->name, name) == 0) {
187 if (m->revision == revision) {
188 if (try_module_get(m->me)) {
189 mutex_unlock(&xt[af].mutex);
193 err = -EPROTOTYPE; /* Found something. */
196 mutex_unlock(&xt[af].mutex);
198 if (af != NFPROTO_UNSPEC)
199 /* Try searching again in the family-independent list */
200 return xt_find_match(NFPROTO_UNSPEC, name, revision);
204 EXPORT_SYMBOL(xt_find_match);
207 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
209 struct xt_match *match;
211 match = xt_find_match(nfproto, name, revision);
213 request_module("%st_%s", xt_prefix[nfproto], name);
214 match = xt_find_match(nfproto, name, revision);
219 EXPORT_SYMBOL_GPL(xt_request_find_match);
221 /* Find target, grabs ref. Returns ERR_PTR() on error. */
222 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
227 mutex_lock(&xt[af].mutex);
228 list_for_each_entry(t, &xt[af].target, list) {
229 if (strcmp(t->name, name) == 0) {
230 if (t->revision == revision) {
231 if (try_module_get(t->me)) {
232 mutex_unlock(&xt[af].mutex);
236 err = -EPROTOTYPE; /* Found something. */
239 mutex_unlock(&xt[af].mutex);
241 if (af != NFPROTO_UNSPEC)
242 /* Try searching again in the family-independent list */
243 return xt_find_target(NFPROTO_UNSPEC, name, revision);
247 EXPORT_SYMBOL(xt_find_target);
249 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
251 struct xt_target *target;
253 target = xt_find_target(af, name, revision);
254 if (IS_ERR(target)) {
255 request_module("%st_%s", xt_prefix[af], name);
256 target = xt_find_target(af, name, revision);
261 EXPORT_SYMBOL_GPL(xt_request_find_target);
263 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
265 const struct xt_match *m;
268 list_for_each_entry(m, &xt[af].match, list) {
269 if (strcmp(m->name, name) == 0) {
270 if (m->revision > *bestp)
271 *bestp = m->revision;
272 if (m->revision == revision)
277 if (af != NFPROTO_UNSPEC && !have_rev)
278 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
283 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
285 const struct xt_target *t;
288 list_for_each_entry(t, &xt[af].target, list) {
289 if (strcmp(t->name, name) == 0) {
290 if (t->revision > *bestp)
291 *bestp = t->revision;
292 if (t->revision == revision)
297 if (af != NFPROTO_UNSPEC && !have_rev)
298 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
303 /* Returns true or false (if no such extension at all) */
304 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
307 int have_rev, best = -1;
309 mutex_lock(&xt[af].mutex);
311 have_rev = target_revfn(af, name, revision, &best);
313 have_rev = match_revfn(af, name, revision, &best);
314 mutex_unlock(&xt[af].mutex);
316 /* Nothing at all? Return 0 to try loading module. */
324 *err = -EPROTONOSUPPORT;
327 EXPORT_SYMBOL_GPL(xt_find_revision);
330 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
332 static const char *const inetbr_names[] = {
333 "PREROUTING", "INPUT", "FORWARD",
334 "OUTPUT", "POSTROUTING", "BROUTING",
336 static const char *const arp_names[] = {
337 "INPUT", "FORWARD", "OUTPUT",
339 const char *const *names;
345 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
346 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
347 ARRAY_SIZE(inetbr_names);
349 for (i = 0; i < max; ++i) {
350 if (!(mask & (1 << i)))
352 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
363 int xt_check_match(struct xt_mtchk_param *par,
364 unsigned int size, u_int8_t proto, bool inv_proto)
368 if (XT_ALIGN(par->match->matchsize) != size &&
369 par->match->matchsize != -1) {
371 * ebt_among is exempt from centralized matchsize checking
372 * because it uses a dynamic-size data set.
374 pr_err("%s_tables: %s.%u match: invalid size "
375 "%u (kernel) != (user) %u\n",
376 xt_prefix[par->family], par->match->name,
377 par->match->revision,
378 XT_ALIGN(par->match->matchsize), size);
381 if (par->match->table != NULL &&
382 strcmp(par->match->table, par->table) != 0) {
383 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
384 xt_prefix[par->family], par->match->name,
385 par->match->table, par->table);
388 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
389 char used[64], allow[64];
391 pr_err("%s_tables: %s match: used from hooks %s, but only "
393 xt_prefix[par->family], par->match->name,
394 textify_hooks(used, sizeof(used), par->hook_mask,
396 textify_hooks(allow, sizeof(allow), par->match->hooks,
400 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
401 pr_err("%s_tables: %s match: only valid for protocol %u\n",
402 xt_prefix[par->family], par->match->name,
406 if (par->match->checkentry != NULL) {
407 ret = par->match->checkentry(par);
411 /* Flag up potential errors. */
416 EXPORT_SYMBOL_GPL(xt_check_match);
419 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
421 struct xt_af *xp = &xt[af];
423 if (!xp->compat_tab) {
426 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
432 if (xp->cur >= xp->number)
436 delta += xp->compat_tab[xp->cur - 1].delta;
437 xp->compat_tab[xp->cur].offset = offset;
438 xp->compat_tab[xp->cur].delta = delta;
442 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
444 void xt_compat_flush_offsets(u_int8_t af)
446 if (xt[af].compat_tab) {
447 vfree(xt[af].compat_tab);
448 xt[af].compat_tab = NULL;
453 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
455 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
457 struct compat_delta *tmp = xt[af].compat_tab;
458 int mid, left = 0, right = xt[af].cur - 1;
460 while (left <= right) {
461 mid = (left + right) >> 1;
462 if (offset > tmp[mid].offset)
464 else if (offset < tmp[mid].offset)
467 return mid ? tmp[mid - 1].delta : 0;
469 return left ? tmp[left - 1].delta : 0;
471 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
473 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
475 xt[af].number = number;
478 EXPORT_SYMBOL(xt_compat_init_offsets);
480 int xt_compat_match_offset(const struct xt_match *match)
482 u_int16_t csize = match->compatsize ? : match->matchsize;
483 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
485 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
487 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
490 const struct xt_match *match = m->u.kernel.match;
491 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
492 int pad, off = xt_compat_match_offset(match);
493 u_int16_t msize = cm->u.user.match_size;
496 memcpy(m, cm, sizeof(*cm));
497 if (match->compat_from_user)
498 match->compat_from_user(m->data, cm->data);
500 memcpy(m->data, cm->data, msize - sizeof(*cm));
501 pad = XT_ALIGN(match->matchsize) - match->matchsize;
503 memset(m->data + match->matchsize, 0, pad);
506 m->u.user.match_size = msize;
512 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
514 int xt_compat_match_to_user(const struct xt_entry_match *m,
515 void __user **dstptr, unsigned int *size)
517 const struct xt_match *match = m->u.kernel.match;
518 struct compat_xt_entry_match __user *cm = *dstptr;
519 int off = xt_compat_match_offset(match);
520 u_int16_t msize = m->u.user.match_size - off;
522 if (copy_to_user(cm, m, sizeof(*cm)) ||
523 put_user(msize, &cm->u.user.match_size) ||
524 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
525 strlen(m->u.kernel.match->name) + 1))
528 if (match->compat_to_user) {
529 if (match->compat_to_user((void __user *)cm->data, m->data))
532 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
540 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
542 int xt_compat_check_entry_offsets(const void *base,
543 unsigned int target_offset,
544 unsigned int next_offset)
546 const struct compat_xt_entry_target *t;
547 const char *e = base;
549 if (target_offset + sizeof(*t) > next_offset)
552 t = (void *)(e + target_offset);
553 if (t->u.target_size < sizeof(*t))
556 if (target_offset + t->u.target_size > next_offset)
561 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
562 #endif /* CONFIG_COMPAT */
565 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
567 * @base: pointer to arp/ip/ip6t_entry
568 * @target_offset: the arp/ip/ip6_t->target_offset
569 * @next_offset: the arp/ip/ip6_t->next_offset
571 * validates that target_offset and next_offset are sane.
572 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
574 * The arp/ip/ip6t_entry structure @base must have passed following tests:
575 * - it must point to a valid memory location
576 * - base to base + next_offset must be accessible, i.e. not exceed allocated
579 * Return: 0 on success, negative errno on failure.
581 int xt_check_entry_offsets(const void *base,
582 unsigned int target_offset,
583 unsigned int next_offset)
585 const struct xt_entry_target *t;
586 const char *e = base;
588 if (target_offset + sizeof(*t) > next_offset)
591 t = (void *)(e + target_offset);
592 if (t->u.target_size < sizeof(*t))
595 if (target_offset + t->u.target_size > next_offset)
600 EXPORT_SYMBOL(xt_check_entry_offsets);
602 int xt_check_target(struct xt_tgchk_param *par,
603 unsigned int size, u_int8_t proto, bool inv_proto)
607 if (XT_ALIGN(par->target->targetsize) != size) {
608 pr_err("%s_tables: %s.%u target: invalid size "
609 "%u (kernel) != (user) %u\n",
610 xt_prefix[par->family], par->target->name,
611 par->target->revision,
612 XT_ALIGN(par->target->targetsize), size);
615 if (par->target->table != NULL &&
616 strcmp(par->target->table, par->table) != 0) {
617 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
618 xt_prefix[par->family], par->target->name,
619 par->target->table, par->table);
622 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
623 char used[64], allow[64];
625 pr_err("%s_tables: %s target: used from hooks %s, but only "
627 xt_prefix[par->family], par->target->name,
628 textify_hooks(used, sizeof(used), par->hook_mask,
630 textify_hooks(allow, sizeof(allow), par->target->hooks,
634 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
635 pr_err("%s_tables: %s target: only valid for protocol %u\n",
636 xt_prefix[par->family], par->target->name,
640 if (par->target->checkentry != NULL) {
641 ret = par->target->checkentry(par);
645 /* Flag up potential errors. */
650 EXPORT_SYMBOL_GPL(xt_check_target);
653 int xt_compat_target_offset(const struct xt_target *target)
655 u_int16_t csize = target->compatsize ? : target->targetsize;
656 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
658 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
660 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
663 const struct xt_target *target = t->u.kernel.target;
664 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
665 int pad, off = xt_compat_target_offset(target);
666 u_int16_t tsize = ct->u.user.target_size;
669 memcpy(t, ct, sizeof(*ct));
670 if (target->compat_from_user)
671 target->compat_from_user(t->data, ct->data);
673 memcpy(t->data, ct->data, tsize - sizeof(*ct));
674 pad = XT_ALIGN(target->targetsize) - target->targetsize;
676 memset(t->data + target->targetsize, 0, pad);
679 t->u.user.target_size = tsize;
684 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
686 int xt_compat_target_to_user(const struct xt_entry_target *t,
687 void __user **dstptr, unsigned int *size)
689 const struct xt_target *target = t->u.kernel.target;
690 struct compat_xt_entry_target __user *ct = *dstptr;
691 int off = xt_compat_target_offset(target);
692 u_int16_t tsize = t->u.user.target_size - off;
694 if (copy_to_user(ct, t, sizeof(*ct)) ||
695 put_user(tsize, &ct->u.user.target_size) ||
696 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
697 strlen(t->u.kernel.target->name) + 1))
700 if (target->compat_to_user) {
701 if (target->compat_to_user((void __user *)ct->data, t->data))
704 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
712 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
715 struct xt_table_info *xt_alloc_table_info(unsigned int size)
717 struct xt_table_info *info = NULL;
718 size_t sz = sizeof(*info) + size;
720 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
721 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
724 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
725 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
731 memset(info, 0, sizeof(*info));
735 EXPORT_SYMBOL(xt_alloc_table_info);
737 void xt_free_table_info(struct xt_table_info *info)
741 if (info->jumpstack != NULL) {
742 for_each_possible_cpu(cpu)
743 kvfree(info->jumpstack[cpu]);
744 kvfree(info->jumpstack);
749 EXPORT_SYMBOL(xt_free_table_info);
751 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
752 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
757 mutex_lock(&xt[af].mutex);
758 list_for_each_entry(t, &net->xt.tables[af], list)
759 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
761 mutex_unlock(&xt[af].mutex);
764 EXPORT_SYMBOL_GPL(xt_find_table_lock);
766 void xt_table_unlock(struct xt_table *table)
768 mutex_unlock(&xt[table->af].mutex);
770 EXPORT_SYMBOL_GPL(xt_table_unlock);
773 void xt_compat_lock(u_int8_t af)
775 mutex_lock(&xt[af].compat_mutex);
777 EXPORT_SYMBOL_GPL(xt_compat_lock);
779 void xt_compat_unlock(u_int8_t af)
781 mutex_unlock(&xt[af].compat_mutex);
783 EXPORT_SYMBOL_GPL(xt_compat_unlock);
786 DEFINE_PER_CPU(seqcount_t, xt_recseq);
787 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
789 struct static_key xt_tee_enabled __read_mostly;
790 EXPORT_SYMBOL_GPL(xt_tee_enabled);
792 static int xt_jumpstack_alloc(struct xt_table_info *i)
797 size = sizeof(void **) * nr_cpu_ids;
798 if (size > PAGE_SIZE)
799 i->jumpstack = vzalloc(size);
801 i->jumpstack = kzalloc(size, GFP_KERNEL);
802 if (i->jumpstack == NULL)
805 /* ruleset without jumps -- no stack needed */
806 if (i->stacksize == 0)
809 /* Jumpstack needs to be able to record two full callchains, one
810 * from the first rule set traversal, plus one table reentrancy
811 * via -j TEE without clobbering the callchain that brought us to
814 * This is done by allocating two jumpstacks per cpu, on reentry
815 * the upper half of the stack is used.
817 * see the jumpstack setup in ipt_do_table() for more details.
819 size = sizeof(void *) * i->stacksize * 2u;
820 for_each_possible_cpu(cpu) {
821 if (size > PAGE_SIZE)
822 i->jumpstack[cpu] = vmalloc_node(size,
825 i->jumpstack[cpu] = kmalloc_node(size,
826 GFP_KERNEL, cpu_to_node(cpu));
827 if (i->jumpstack[cpu] == NULL)
829 * Freeing will be done later on by the callers. The
830 * chain is: xt_replace_table -> __do_replace ->
831 * do_replace -> xt_free_table_info.
839 struct xt_table_info *
840 xt_replace_table(struct xt_table *table,
841 unsigned int num_counters,
842 struct xt_table_info *newinfo,
845 struct xt_table_info *private;
848 ret = xt_jumpstack_alloc(newinfo);
854 /* Do the substitution. */
856 private = table->private;
858 /* Check inside lock: is the old number correct? */
859 if (num_counters != private->number) {
860 pr_debug("num_counters != table->private->number (%u/%u)\n",
861 num_counters, private->number);
867 newinfo->initial_entries = private->initial_entries;
869 * Ensure contents of newinfo are visible before assigning to
873 table->private = newinfo;
876 * Even though table entries have now been swapped, other CPU's
877 * may still be using the old entries. This is okay, because
878 * resynchronization happens because of the locking done
879 * during the get_counters() routine.
885 struct audit_buffer *ab;
887 ab = audit_log_start(current->audit_context, GFP_KERNEL,
888 AUDIT_NETFILTER_CFG);
890 audit_log_format(ab, "table=%s family=%u entries=%u",
891 table->name, table->af,
900 EXPORT_SYMBOL_GPL(xt_replace_table);
902 struct xt_table *xt_register_table(struct net *net,
903 const struct xt_table *input_table,
904 struct xt_table_info *bootstrap,
905 struct xt_table_info *newinfo)
908 struct xt_table_info *private;
909 struct xt_table *t, *table;
911 /* Don't add one object to multiple lists. */
912 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
918 mutex_lock(&xt[table->af].mutex);
919 /* Don't autoload: we'd eat our tail... */
920 list_for_each_entry(t, &net->xt.tables[table->af], list) {
921 if (strcmp(t->name, table->name) == 0) {
927 /* Simplifies replace_table code. */
928 table->private = bootstrap;
930 if (!xt_replace_table(table, 0, newinfo, &ret))
933 private = table->private;
934 pr_debug("table->private->number = %u\n", private->number);
936 /* save number of initial entries */
937 private->initial_entries = private->number;
939 list_add(&table->list, &net->xt.tables[table->af]);
940 mutex_unlock(&xt[table->af].mutex);
944 mutex_unlock(&xt[table->af].mutex);
949 EXPORT_SYMBOL_GPL(xt_register_table);
951 void *xt_unregister_table(struct xt_table *table)
953 struct xt_table_info *private;
955 mutex_lock(&xt[table->af].mutex);
956 private = table->private;
957 list_del(&table->list);
958 mutex_unlock(&xt[table->af].mutex);
963 EXPORT_SYMBOL_GPL(xt_unregister_table);
965 #ifdef CONFIG_PROC_FS
966 struct xt_names_priv {
967 struct seq_net_private p;
970 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
972 struct xt_names_priv *priv = seq->private;
973 struct net *net = seq_file_net(seq);
974 u_int8_t af = priv->af;
976 mutex_lock(&xt[af].mutex);
977 return seq_list_start(&net->xt.tables[af], *pos);
980 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
982 struct xt_names_priv *priv = seq->private;
983 struct net *net = seq_file_net(seq);
984 u_int8_t af = priv->af;
986 return seq_list_next(v, &net->xt.tables[af], pos);
989 static void xt_table_seq_stop(struct seq_file *seq, void *v)
991 struct xt_names_priv *priv = seq->private;
992 u_int8_t af = priv->af;
994 mutex_unlock(&xt[af].mutex);
997 static int xt_table_seq_show(struct seq_file *seq, void *v)
999 struct xt_table *table = list_entry(v, struct xt_table, list);
1002 seq_printf(seq, "%s\n", table->name);
1006 static const struct seq_operations xt_table_seq_ops = {
1007 .start = xt_table_seq_start,
1008 .next = xt_table_seq_next,
1009 .stop = xt_table_seq_stop,
1010 .show = xt_table_seq_show,
1013 static int xt_table_open(struct inode *inode, struct file *file)
1016 struct xt_names_priv *priv;
1018 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1019 sizeof(struct xt_names_priv));
1021 priv = ((struct seq_file *)file->private_data)->private;
1022 priv->af = (unsigned long)PDE_DATA(inode);
1027 static const struct file_operations xt_table_ops = {
1028 .owner = THIS_MODULE,
1029 .open = xt_table_open,
1031 .llseek = seq_lseek,
1032 .release = seq_release_net,
1036 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1037 * the multi-AF mutexes.
1039 struct nf_mttg_trav {
1040 struct list_head *head, *curr;
1041 uint8_t class, nfproto;
1046 MTTG_TRAV_NFP_UNSPEC,
1051 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1054 static const uint8_t next_class[] = {
1055 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1056 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1058 struct nf_mttg_trav *trav = seq->private;
1060 switch (trav->class) {
1061 case MTTG_TRAV_INIT:
1062 trav->class = MTTG_TRAV_NFP_UNSPEC;
1063 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1064 trav->head = trav->curr = is_target ?
1065 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1067 case MTTG_TRAV_NFP_UNSPEC:
1068 trav->curr = trav->curr->next;
1069 if (trav->curr != trav->head)
1071 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1072 mutex_lock(&xt[trav->nfproto].mutex);
1073 trav->head = trav->curr = is_target ?
1074 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1075 trav->class = next_class[trav->class];
1077 case MTTG_TRAV_NFP_SPEC:
1078 trav->curr = trav->curr->next;
1079 if (trav->curr != trav->head)
1081 /* fallthru, _stop will unlock */
1091 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1094 struct nf_mttg_trav *trav = seq->private;
1097 trav->class = MTTG_TRAV_INIT;
1098 for (j = 0; j < *pos; ++j)
1099 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1104 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1106 struct nf_mttg_trav *trav = seq->private;
1108 switch (trav->class) {
1109 case MTTG_TRAV_NFP_UNSPEC:
1110 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1112 case MTTG_TRAV_NFP_SPEC:
1113 mutex_unlock(&xt[trav->nfproto].mutex);
1118 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1120 return xt_mttg_seq_start(seq, pos, false);
1123 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1125 return xt_mttg_seq_next(seq, v, ppos, false);
1128 static int xt_match_seq_show(struct seq_file *seq, void *v)
1130 const struct nf_mttg_trav *trav = seq->private;
1131 const struct xt_match *match;
1133 switch (trav->class) {
1134 case MTTG_TRAV_NFP_UNSPEC:
1135 case MTTG_TRAV_NFP_SPEC:
1136 if (trav->curr == trav->head)
1138 match = list_entry(trav->curr, struct xt_match, list);
1140 seq_printf(seq, "%s\n", match->name);
1145 static const struct seq_operations xt_match_seq_ops = {
1146 .start = xt_match_seq_start,
1147 .next = xt_match_seq_next,
1148 .stop = xt_mttg_seq_stop,
1149 .show = xt_match_seq_show,
1152 static int xt_match_open(struct inode *inode, struct file *file)
1154 struct nf_mttg_trav *trav;
1155 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1159 trav->nfproto = (unsigned long)PDE_DATA(inode);
1163 static const struct file_operations xt_match_ops = {
1164 .owner = THIS_MODULE,
1165 .open = xt_match_open,
1167 .llseek = seq_lseek,
1168 .release = seq_release_private,
1171 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1173 return xt_mttg_seq_start(seq, pos, true);
1176 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1178 return xt_mttg_seq_next(seq, v, ppos, true);
1181 static int xt_target_seq_show(struct seq_file *seq, void *v)
1183 const struct nf_mttg_trav *trav = seq->private;
1184 const struct xt_target *target;
1186 switch (trav->class) {
1187 case MTTG_TRAV_NFP_UNSPEC:
1188 case MTTG_TRAV_NFP_SPEC:
1189 if (trav->curr == trav->head)
1191 target = list_entry(trav->curr, struct xt_target, list);
1193 seq_printf(seq, "%s\n", target->name);
1198 static const struct seq_operations xt_target_seq_ops = {
1199 .start = xt_target_seq_start,
1200 .next = xt_target_seq_next,
1201 .stop = xt_mttg_seq_stop,
1202 .show = xt_target_seq_show,
1205 static int xt_target_open(struct inode *inode, struct file *file)
1207 struct nf_mttg_trav *trav;
1208 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1212 trav->nfproto = (unsigned long)PDE_DATA(inode);
1216 static const struct file_operations xt_target_ops = {
1217 .owner = THIS_MODULE,
1218 .open = xt_target_open,
1220 .llseek = seq_lseek,
1221 .release = seq_release_private,
1224 #define FORMAT_TABLES "_tables_names"
1225 #define FORMAT_MATCHES "_tables_matches"
1226 #define FORMAT_TARGETS "_tables_targets"
1228 #endif /* CONFIG_PROC_FS */
1231 * xt_hook_link - set up hooks for a new table
1232 * @table: table with metadata needed to set up hooks
1233 * @fn: Hook function
1235 * This function will take care of creating and registering the necessary
1236 * Netfilter hooks for XT tables.
1238 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1240 unsigned int hook_mask = table->valid_hooks;
1241 uint8_t i, num_hooks = hweight32(hook_mask);
1243 struct nf_hook_ops *ops;
1246 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1248 return ERR_PTR(-ENOMEM);
1250 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1251 hook_mask >>= 1, ++hooknum) {
1252 if (!(hook_mask & 1))
1255 ops[i].pf = table->af;
1256 ops[i].hooknum = hooknum;
1257 ops[i].priority = table->priority;
1261 ret = nf_register_hooks(ops, num_hooks);
1264 return ERR_PTR(ret);
1269 EXPORT_SYMBOL_GPL(xt_hook_link);
1272 * xt_hook_unlink - remove hooks for a table
1273 * @ops: nf_hook_ops array as returned by nf_hook_link
1274 * @hook_mask: the very same mask that was passed to nf_hook_link
1276 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1278 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1281 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1283 int xt_proto_init(struct net *net, u_int8_t af)
1285 #ifdef CONFIG_PROC_FS
1286 char buf[XT_FUNCTION_MAXNAMELEN];
1287 struct proc_dir_entry *proc;
1290 if (af >= ARRAY_SIZE(xt_prefix))
1294 #ifdef CONFIG_PROC_FS
1295 strlcpy(buf, xt_prefix[af], sizeof(buf));
1296 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1297 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1298 (void *)(unsigned long)af);
1302 strlcpy(buf, xt_prefix[af], sizeof(buf));
1303 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1304 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1305 (void *)(unsigned long)af);
1307 goto out_remove_tables;
1309 strlcpy(buf, xt_prefix[af], sizeof(buf));
1310 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1311 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1312 (void *)(unsigned long)af);
1314 goto out_remove_matches;
1319 #ifdef CONFIG_PROC_FS
1321 strlcpy(buf, xt_prefix[af], sizeof(buf));
1322 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1323 remove_proc_entry(buf, net->proc_net);
1326 strlcpy(buf, xt_prefix[af], sizeof(buf));
1327 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1328 remove_proc_entry(buf, net->proc_net);
1333 EXPORT_SYMBOL_GPL(xt_proto_init);
1335 void xt_proto_fini(struct net *net, u_int8_t af)
1337 #ifdef CONFIG_PROC_FS
1338 char buf[XT_FUNCTION_MAXNAMELEN];
1340 strlcpy(buf, xt_prefix[af], sizeof(buf));
1341 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1342 remove_proc_entry(buf, net->proc_net);
1344 strlcpy(buf, xt_prefix[af], sizeof(buf));
1345 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1346 remove_proc_entry(buf, net->proc_net);
1348 strlcpy(buf, xt_prefix[af], sizeof(buf));
1349 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1350 remove_proc_entry(buf, net->proc_net);
1351 #endif /*CONFIG_PROC_FS*/
1353 EXPORT_SYMBOL_GPL(xt_proto_fini);
1355 static int __net_init xt_net_init(struct net *net)
1359 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1360 INIT_LIST_HEAD(&net->xt.tables[i]);
1364 static struct pernet_operations xt_net_ops = {
1365 .init = xt_net_init,
1368 static int __init xt_init(void)
1373 for_each_possible_cpu(i) {
1374 seqcount_init(&per_cpu(xt_recseq, i));
1377 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1381 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1382 mutex_init(&xt[i].mutex);
1383 #ifdef CONFIG_COMPAT
1384 mutex_init(&xt[i].compat_mutex);
1385 xt[i].compat_tab = NULL;
1387 INIT_LIST_HEAD(&xt[i].target);
1388 INIT_LIST_HEAD(&xt[i].match);
1390 rv = register_pernet_subsys(&xt_net_ops);
1396 static void __exit xt_fini(void)
1398 unregister_pernet_subsys(&xt_net_ops);
1402 module_init(xt_init);
1403 module_exit(xt_fini);