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);
418 /** xt_check_entry_match - check that matches end before start of target
420 * @match: beginning of xt_entry_match
421 * @target: beginning of this rules target (alleged end of matches)
422 * @alignment: alignment requirement of match structures
424 * Validates that all matches add up to the beginning of the target,
425 * and that each match covers at least the base structure size.
427 * Return: 0 on success, negative errno on failure.
429 static int xt_check_entry_match(const char *match, const char *target,
430 const size_t alignment)
432 const struct xt_entry_match *pos;
433 int length = target - match;
435 if (length == 0) /* no matches */
438 pos = (struct xt_entry_match *)match;
440 if ((unsigned long)pos % alignment)
443 if (length < (int)sizeof(struct xt_entry_match))
446 if (pos->u.match_size < sizeof(struct xt_entry_match))
449 if (pos->u.match_size > length)
452 length -= pos->u.match_size;
453 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
454 } while (length > 0);
460 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
462 struct xt_af *xp = &xt[af];
464 if (!xp->compat_tab) {
467 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
473 if (xp->cur >= xp->number)
477 delta += xp->compat_tab[xp->cur - 1].delta;
478 xp->compat_tab[xp->cur].offset = offset;
479 xp->compat_tab[xp->cur].delta = delta;
483 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
485 void xt_compat_flush_offsets(u_int8_t af)
487 if (xt[af].compat_tab) {
488 vfree(xt[af].compat_tab);
489 xt[af].compat_tab = NULL;
494 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
496 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
498 struct compat_delta *tmp = xt[af].compat_tab;
499 int mid, left = 0, right = xt[af].cur - 1;
501 while (left <= right) {
502 mid = (left + right) >> 1;
503 if (offset > tmp[mid].offset)
505 else if (offset < tmp[mid].offset)
508 return mid ? tmp[mid - 1].delta : 0;
510 return left ? tmp[left - 1].delta : 0;
512 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
514 void xt_compat_init_offsets(u_int8_t af, unsigned int number)
516 xt[af].number = number;
519 EXPORT_SYMBOL(xt_compat_init_offsets);
521 int xt_compat_match_offset(const struct xt_match *match)
523 u_int16_t csize = match->compatsize ? : match->matchsize;
524 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
526 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
528 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
531 const struct xt_match *match = m->u.kernel.match;
532 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
533 int pad, off = xt_compat_match_offset(match);
534 u_int16_t msize = cm->u.user.match_size;
535 char name[sizeof(m->u.user.name)];
538 memcpy(m, cm, sizeof(*cm));
539 if (match->compat_from_user)
540 match->compat_from_user(m->data, cm->data);
542 memcpy(m->data, cm->data, msize - sizeof(*cm));
543 pad = XT_ALIGN(match->matchsize) - match->matchsize;
545 memset(m->data + match->matchsize, 0, pad);
548 m->u.user.match_size = msize;
549 strlcpy(name, match->name, sizeof(name));
550 module_put(match->me);
551 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
556 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
558 int xt_compat_match_to_user(const struct xt_entry_match *m,
559 void __user **dstptr, unsigned int *size)
561 const struct xt_match *match = m->u.kernel.match;
562 struct compat_xt_entry_match __user *cm = *dstptr;
563 int off = xt_compat_match_offset(match);
564 u_int16_t msize = m->u.user.match_size - off;
566 if (copy_to_user(cm, m, sizeof(*cm)) ||
567 put_user(msize, &cm->u.user.match_size) ||
568 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
569 strlen(m->u.kernel.match->name) + 1))
572 if (match->compat_to_user) {
573 if (match->compat_to_user((void __user *)cm->data, m->data))
576 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
584 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
586 /* non-compat version may have padding after verdict */
587 struct compat_xt_standard_target {
588 struct compat_xt_entry_target t;
589 compat_uint_t verdict;
592 int xt_compat_check_entry_offsets(const void *base, const char *elems,
593 unsigned int target_offset,
594 unsigned int next_offset)
596 long size_of_base_struct = elems - (const char *)base;
597 const struct compat_xt_entry_target *t;
598 const char *e = base;
600 if (target_offset < size_of_base_struct)
603 if (target_offset + sizeof(*t) > next_offset)
606 t = (void *)(e + target_offset);
607 if (t->u.target_size < sizeof(*t))
610 if (target_offset + t->u.target_size > next_offset)
613 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
614 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
617 /* compat_xt_entry match has less strict aligment requirements,
618 * otherwise they are identical. In case of padding differences
619 * we need to add compat version of xt_check_entry_match.
621 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
623 return xt_check_entry_match(elems, base + target_offset,
624 __alignof__(struct compat_xt_entry_match));
626 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
627 #endif /* CONFIG_COMPAT */
630 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
632 * @base: pointer to arp/ip/ip6t_entry
633 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
634 * @target_offset: the arp/ip/ip6_t->target_offset
635 * @next_offset: the arp/ip/ip6_t->next_offset
637 * validates that target_offset and next_offset are sane and that all
638 * match sizes (if any) align with the target offset.
640 * This function does not validate the targets or matches themselves, it
641 * only tests that all the offsets and sizes are correct, that all
642 * match structures are aligned, and that the last structure ends where
643 * the target structure begins.
645 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
647 * The arp/ip/ip6t_entry structure @base must have passed following tests:
648 * - it must point to a valid memory location
649 * - base to base + next_offset must be accessible, i.e. not exceed allocated
652 * A well-formed entry looks like this:
654 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
655 * e->elems[]-----' | |
659 * target_offset---------------------------------' |
660 * next_offset---------------------------------------------------'
662 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
663 * This is where matches (if any) and the target reside.
664 * target_offset: beginning of target.
665 * next_offset: start of the next rule; also: size of this rule.
666 * Since targets have a minimum size, target_offset + minlen <= next_offset.
668 * Every match stores its size, sum of sizes must not exceed target_offset.
670 * Return: 0 on success, negative errno on failure.
672 int xt_check_entry_offsets(const void *base,
674 unsigned int target_offset,
675 unsigned int next_offset)
677 long size_of_base_struct = elems - (const char *)base;
678 const struct xt_entry_target *t;
679 const char *e = base;
681 /* target start is within the ip/ip6/arpt_entry struct */
682 if (target_offset < size_of_base_struct)
685 if (target_offset + sizeof(*t) > next_offset)
688 t = (void *)(e + target_offset);
689 if (t->u.target_size < sizeof(*t))
692 if (target_offset + t->u.target_size > next_offset)
695 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
696 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
699 return xt_check_entry_match(elems, base + target_offset,
700 __alignof__(struct xt_entry_match));
702 EXPORT_SYMBOL(xt_check_entry_offsets);
704 int xt_check_target(struct xt_tgchk_param *par,
705 unsigned int size, u_int8_t proto, bool inv_proto)
709 if (XT_ALIGN(par->target->targetsize) != size) {
710 pr_err("%s_tables: %s.%u target: invalid size "
711 "%u (kernel) != (user) %u\n",
712 xt_prefix[par->family], par->target->name,
713 par->target->revision,
714 XT_ALIGN(par->target->targetsize), size);
717 if (par->target->table != NULL &&
718 strcmp(par->target->table, par->table) != 0) {
719 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
720 xt_prefix[par->family], par->target->name,
721 par->target->table, par->table);
724 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
725 char used[64], allow[64];
727 pr_err("%s_tables: %s target: used from hooks %s, but only "
729 xt_prefix[par->family], par->target->name,
730 textify_hooks(used, sizeof(used), par->hook_mask,
732 textify_hooks(allow, sizeof(allow), par->target->hooks,
736 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
737 pr_err("%s_tables: %s target: only valid for protocol %u\n",
738 xt_prefix[par->family], par->target->name,
742 if (par->target->checkentry != NULL) {
743 ret = par->target->checkentry(par);
747 /* Flag up potential errors. */
752 EXPORT_SYMBOL_GPL(xt_check_target);
755 * xt_copy_counters_from_user - copy counters and metadata from userspace
757 * @user: src pointer to userspace memory
758 * @len: alleged size of userspace memory
759 * @info: where to store the xt_counters_info metadata
760 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
762 * Copies counter meta data from @user and stores it in @info.
764 * vmallocs memory to hold the counters, then copies the counter data
765 * from @user to the new memory and returns a pointer to it.
767 * If @compat is true, @info gets converted automatically to the 64bit
770 * The metadata associated with the counters is stored in @info.
772 * Return: returns pointer that caller has to test via IS_ERR().
773 * If IS_ERR is false, caller has to vfree the pointer.
775 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
776 struct xt_counters_info *info, bool compat)
783 /* structures only differ in size due to alignment */
784 struct compat_xt_counters_info compat_tmp;
786 if (len <= sizeof(compat_tmp))
787 return ERR_PTR(-EINVAL);
789 len -= sizeof(compat_tmp);
790 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
791 return ERR_PTR(-EFAULT);
793 strlcpy(info->name, compat_tmp.name, sizeof(info->name));
794 info->num_counters = compat_tmp.num_counters;
795 user += sizeof(compat_tmp);
799 if (len <= sizeof(*info))
800 return ERR_PTR(-EINVAL);
802 len -= sizeof(*info);
803 if (copy_from_user(info, user, sizeof(*info)) != 0)
804 return ERR_PTR(-EFAULT);
806 info->name[sizeof(info->name) - 1] = '\0';
807 user += sizeof(*info);
810 size = sizeof(struct xt_counters);
811 size *= info->num_counters;
813 if (size != (u64)len)
814 return ERR_PTR(-EINVAL);
818 return ERR_PTR(-ENOMEM);
820 if (copy_from_user(mem, user, len) == 0)
824 return ERR_PTR(-EFAULT);
826 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
829 int xt_compat_target_offset(const struct xt_target *target)
831 u_int16_t csize = target->compatsize ? : target->targetsize;
832 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
834 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
836 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
839 const struct xt_target *target = t->u.kernel.target;
840 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
841 int pad, off = xt_compat_target_offset(target);
842 u_int16_t tsize = ct->u.user.target_size;
843 char name[sizeof(t->u.user.name)];
846 memcpy(t, ct, sizeof(*ct));
847 if (target->compat_from_user)
848 target->compat_from_user(t->data, ct->data);
850 memcpy(t->data, ct->data, tsize - sizeof(*ct));
851 pad = XT_ALIGN(target->targetsize) - target->targetsize;
853 memset(t->data + target->targetsize, 0, pad);
856 t->u.user.target_size = tsize;
857 strlcpy(name, target->name, sizeof(name));
858 module_put(target->me);
859 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
864 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
866 int xt_compat_target_to_user(const struct xt_entry_target *t,
867 void __user **dstptr, unsigned int *size)
869 const struct xt_target *target = t->u.kernel.target;
870 struct compat_xt_entry_target __user *ct = *dstptr;
871 int off = xt_compat_target_offset(target);
872 u_int16_t tsize = t->u.user.target_size - off;
874 if (copy_to_user(ct, t, sizeof(*ct)) ||
875 put_user(tsize, &ct->u.user.target_size) ||
876 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
877 strlen(t->u.kernel.target->name) + 1))
880 if (target->compat_to_user) {
881 if (target->compat_to_user((void __user *)ct->data, t->data))
884 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
892 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
895 struct xt_table_info *xt_alloc_table_info(unsigned int size)
897 struct xt_table_info *info = NULL;
898 size_t sz = sizeof(*info) + size;
900 if (sz < sizeof(*info))
903 if (sz < sizeof(*info))
906 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
907 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
910 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
911 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
917 memset(info, 0, sizeof(*info));
921 EXPORT_SYMBOL(xt_alloc_table_info);
923 void xt_free_table_info(struct xt_table_info *info)
927 if (info->jumpstack != NULL) {
928 for_each_possible_cpu(cpu)
929 kvfree(info->jumpstack[cpu]);
930 kvfree(info->jumpstack);
935 EXPORT_SYMBOL(xt_free_table_info);
937 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
938 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
943 mutex_lock(&xt[af].mutex);
944 list_for_each_entry(t, &net->xt.tables[af], list)
945 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
947 mutex_unlock(&xt[af].mutex);
950 EXPORT_SYMBOL_GPL(xt_find_table_lock);
952 void xt_table_unlock(struct xt_table *table)
954 mutex_unlock(&xt[table->af].mutex);
956 EXPORT_SYMBOL_GPL(xt_table_unlock);
959 void xt_compat_lock(u_int8_t af)
961 mutex_lock(&xt[af].compat_mutex);
963 EXPORT_SYMBOL_GPL(xt_compat_lock);
965 void xt_compat_unlock(u_int8_t af)
967 mutex_unlock(&xt[af].compat_mutex);
969 EXPORT_SYMBOL_GPL(xt_compat_unlock);
972 DEFINE_PER_CPU(seqcount_t, xt_recseq);
973 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
975 struct static_key xt_tee_enabled __read_mostly;
976 EXPORT_SYMBOL_GPL(xt_tee_enabled);
978 static int xt_jumpstack_alloc(struct xt_table_info *i)
983 size = sizeof(void **) * nr_cpu_ids;
984 if (size > PAGE_SIZE)
985 i->jumpstack = vzalloc(size);
987 i->jumpstack = kzalloc(size, GFP_KERNEL);
988 if (i->jumpstack == NULL)
991 /* ruleset without jumps -- no stack needed */
992 if (i->stacksize == 0)
995 /* Jumpstack needs to be able to record two full callchains, one
996 * from the first rule set traversal, plus one table reentrancy
997 * via -j TEE without clobbering the callchain that brought us to
1000 * This is done by allocating two jumpstacks per cpu, on reentry
1001 * the upper half of the stack is used.
1003 * see the jumpstack setup in ipt_do_table() for more details.
1005 size = sizeof(void *) * i->stacksize * 2u;
1006 for_each_possible_cpu(cpu) {
1007 if (size > PAGE_SIZE)
1008 i->jumpstack[cpu] = vmalloc_node(size,
1011 i->jumpstack[cpu] = kmalloc_node(size,
1012 GFP_KERNEL, cpu_to_node(cpu));
1013 if (i->jumpstack[cpu] == NULL)
1015 * Freeing will be done later on by the callers. The
1016 * chain is: xt_replace_table -> __do_replace ->
1017 * do_replace -> xt_free_table_info.
1025 struct xt_table_info *
1026 xt_replace_table(struct xt_table *table,
1027 unsigned int num_counters,
1028 struct xt_table_info *newinfo,
1031 struct xt_table_info *private;
1034 ret = xt_jumpstack_alloc(newinfo);
1040 /* Do the substitution. */
1042 private = table->private;
1044 /* Check inside lock: is the old number correct? */
1045 if (num_counters != private->number) {
1046 pr_debug("num_counters != table->private->number (%u/%u)\n",
1047 num_counters, private->number);
1053 newinfo->initial_entries = private->initial_entries;
1055 * Ensure contents of newinfo are visible before assigning to
1059 table->private = newinfo;
1062 * Even though table entries have now been swapped, other CPU's
1063 * may still be using the old entries. This is okay, because
1064 * resynchronization happens because of the locking done
1065 * during the get_counters() routine.
1070 if (audit_enabled) {
1071 struct audit_buffer *ab;
1073 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1074 AUDIT_NETFILTER_CFG);
1076 audit_log_format(ab, "table=%s family=%u entries=%u",
1077 table->name, table->af,
1086 EXPORT_SYMBOL_GPL(xt_replace_table);
1088 struct xt_table *xt_register_table(struct net *net,
1089 const struct xt_table *input_table,
1090 struct xt_table_info *bootstrap,
1091 struct xt_table_info *newinfo)
1094 struct xt_table_info *private;
1095 struct xt_table *t, *table;
1097 /* Don't add one object to multiple lists. */
1098 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1104 mutex_lock(&xt[table->af].mutex);
1105 /* Don't autoload: we'd eat our tail... */
1106 list_for_each_entry(t, &net->xt.tables[table->af], list) {
1107 if (strcmp(t->name, table->name) == 0) {
1113 /* Simplifies replace_table code. */
1114 table->private = bootstrap;
1116 if (!xt_replace_table(table, 0, newinfo, &ret))
1119 private = table->private;
1120 pr_debug("table->private->number = %u\n", private->number);
1122 /* save number of initial entries */
1123 private->initial_entries = private->number;
1125 list_add(&table->list, &net->xt.tables[table->af]);
1126 mutex_unlock(&xt[table->af].mutex);
1130 mutex_unlock(&xt[table->af].mutex);
1133 return ERR_PTR(ret);
1135 EXPORT_SYMBOL_GPL(xt_register_table);
1137 void *xt_unregister_table(struct xt_table *table)
1139 struct xt_table_info *private;
1141 mutex_lock(&xt[table->af].mutex);
1142 private = table->private;
1143 list_del(&table->list);
1144 mutex_unlock(&xt[table->af].mutex);
1149 EXPORT_SYMBOL_GPL(xt_unregister_table);
1151 #ifdef CONFIG_PROC_FS
1152 struct xt_names_priv {
1153 struct seq_net_private p;
1156 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1158 struct xt_names_priv *priv = seq->private;
1159 struct net *net = seq_file_net(seq);
1160 u_int8_t af = priv->af;
1162 mutex_lock(&xt[af].mutex);
1163 return seq_list_start(&net->xt.tables[af], *pos);
1166 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1168 struct xt_names_priv *priv = seq->private;
1169 struct net *net = seq_file_net(seq);
1170 u_int8_t af = priv->af;
1172 return seq_list_next(v, &net->xt.tables[af], pos);
1175 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1177 struct xt_names_priv *priv = seq->private;
1178 u_int8_t af = priv->af;
1180 mutex_unlock(&xt[af].mutex);
1183 static int xt_table_seq_show(struct seq_file *seq, void *v)
1185 struct xt_table *table = list_entry(v, struct xt_table, list);
1188 seq_printf(seq, "%s\n", table->name);
1192 static const struct seq_operations xt_table_seq_ops = {
1193 .start = xt_table_seq_start,
1194 .next = xt_table_seq_next,
1195 .stop = xt_table_seq_stop,
1196 .show = xt_table_seq_show,
1199 static int xt_table_open(struct inode *inode, struct file *file)
1202 struct xt_names_priv *priv;
1204 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1205 sizeof(struct xt_names_priv));
1207 priv = ((struct seq_file *)file->private_data)->private;
1208 priv->af = (unsigned long)PDE_DATA(inode);
1213 static const struct file_operations xt_table_ops = {
1214 .owner = THIS_MODULE,
1215 .open = xt_table_open,
1217 .llseek = seq_lseek,
1218 .release = seq_release_net,
1222 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1223 * the multi-AF mutexes.
1225 struct nf_mttg_trav {
1226 struct list_head *head, *curr;
1227 uint8_t class, nfproto;
1232 MTTG_TRAV_NFP_UNSPEC,
1237 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1240 static const uint8_t next_class[] = {
1241 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1242 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1244 struct nf_mttg_trav *trav = seq->private;
1246 switch (trav->class) {
1247 case MTTG_TRAV_INIT:
1248 trav->class = MTTG_TRAV_NFP_UNSPEC;
1249 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1250 trav->head = trav->curr = is_target ?
1251 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1253 case MTTG_TRAV_NFP_UNSPEC:
1254 trav->curr = trav->curr->next;
1255 if (trav->curr != trav->head)
1257 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1258 mutex_lock(&xt[trav->nfproto].mutex);
1259 trav->head = trav->curr = is_target ?
1260 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1261 trav->class = next_class[trav->class];
1263 case MTTG_TRAV_NFP_SPEC:
1264 trav->curr = trav->curr->next;
1265 if (trav->curr != trav->head)
1267 /* fallthru, _stop will unlock */
1277 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1280 struct nf_mttg_trav *trav = seq->private;
1283 trav->class = MTTG_TRAV_INIT;
1284 for (j = 0; j < *pos; ++j)
1285 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1290 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1292 struct nf_mttg_trav *trav = seq->private;
1294 switch (trav->class) {
1295 case MTTG_TRAV_NFP_UNSPEC:
1296 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1298 case MTTG_TRAV_NFP_SPEC:
1299 mutex_unlock(&xt[trav->nfproto].mutex);
1304 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1306 return xt_mttg_seq_start(seq, pos, false);
1309 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1311 return xt_mttg_seq_next(seq, v, ppos, false);
1314 static int xt_match_seq_show(struct seq_file *seq, void *v)
1316 const struct nf_mttg_trav *trav = seq->private;
1317 const struct xt_match *match;
1319 switch (trav->class) {
1320 case MTTG_TRAV_NFP_UNSPEC:
1321 case MTTG_TRAV_NFP_SPEC:
1322 if (trav->curr == trav->head)
1324 match = list_entry(trav->curr, struct xt_match, list);
1326 seq_printf(seq, "%s\n", match->name);
1331 static const struct seq_operations xt_match_seq_ops = {
1332 .start = xt_match_seq_start,
1333 .next = xt_match_seq_next,
1334 .stop = xt_mttg_seq_stop,
1335 .show = xt_match_seq_show,
1338 static int xt_match_open(struct inode *inode, struct file *file)
1340 struct nf_mttg_trav *trav;
1341 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1345 trav->nfproto = (unsigned long)PDE_DATA(inode);
1349 static const struct file_operations xt_match_ops = {
1350 .owner = THIS_MODULE,
1351 .open = xt_match_open,
1353 .llseek = seq_lseek,
1354 .release = seq_release_private,
1357 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1359 return xt_mttg_seq_start(seq, pos, true);
1362 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1364 return xt_mttg_seq_next(seq, v, ppos, true);
1367 static int xt_target_seq_show(struct seq_file *seq, void *v)
1369 const struct nf_mttg_trav *trav = seq->private;
1370 const struct xt_target *target;
1372 switch (trav->class) {
1373 case MTTG_TRAV_NFP_UNSPEC:
1374 case MTTG_TRAV_NFP_SPEC:
1375 if (trav->curr == trav->head)
1377 target = list_entry(trav->curr, struct xt_target, list);
1379 seq_printf(seq, "%s\n", target->name);
1384 static const struct seq_operations xt_target_seq_ops = {
1385 .start = xt_target_seq_start,
1386 .next = xt_target_seq_next,
1387 .stop = xt_mttg_seq_stop,
1388 .show = xt_target_seq_show,
1391 static int xt_target_open(struct inode *inode, struct file *file)
1393 struct nf_mttg_trav *trav;
1394 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1398 trav->nfproto = (unsigned long)PDE_DATA(inode);
1402 static const struct file_operations xt_target_ops = {
1403 .owner = THIS_MODULE,
1404 .open = xt_target_open,
1406 .llseek = seq_lseek,
1407 .release = seq_release_private,
1410 #define FORMAT_TABLES "_tables_names"
1411 #define FORMAT_MATCHES "_tables_matches"
1412 #define FORMAT_TARGETS "_tables_targets"
1414 #endif /* CONFIG_PROC_FS */
1417 * xt_hook_link - set up hooks for a new table
1418 * @table: table with metadata needed to set up hooks
1419 * @fn: Hook function
1421 * This function will take care of creating and registering the necessary
1422 * Netfilter hooks for XT tables.
1424 struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1426 unsigned int hook_mask = table->valid_hooks;
1427 uint8_t i, num_hooks = hweight32(hook_mask);
1429 struct nf_hook_ops *ops;
1432 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1434 return ERR_PTR(-ENOMEM);
1436 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1437 hook_mask >>= 1, ++hooknum) {
1438 if (!(hook_mask & 1))
1441 ops[i].pf = table->af;
1442 ops[i].hooknum = hooknum;
1443 ops[i].priority = table->priority;
1447 ret = nf_register_hooks(ops, num_hooks);
1450 return ERR_PTR(ret);
1455 EXPORT_SYMBOL_GPL(xt_hook_link);
1458 * xt_hook_unlink - remove hooks for a table
1459 * @ops: nf_hook_ops array as returned by nf_hook_link
1460 * @hook_mask: the very same mask that was passed to nf_hook_link
1462 void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1464 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1467 EXPORT_SYMBOL_GPL(xt_hook_unlink);
1469 int xt_proto_init(struct net *net, u_int8_t af)
1471 #ifdef CONFIG_PROC_FS
1472 char buf[XT_FUNCTION_MAXNAMELEN];
1473 struct proc_dir_entry *proc;
1476 if (af >= ARRAY_SIZE(xt_prefix))
1480 #ifdef CONFIG_PROC_FS
1481 strlcpy(buf, xt_prefix[af], sizeof(buf));
1482 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1483 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1484 (void *)(unsigned long)af);
1488 strlcpy(buf, xt_prefix[af], sizeof(buf));
1489 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1490 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1491 (void *)(unsigned long)af);
1493 goto out_remove_tables;
1495 strlcpy(buf, xt_prefix[af], sizeof(buf));
1496 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1497 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1498 (void *)(unsigned long)af);
1500 goto out_remove_matches;
1505 #ifdef CONFIG_PROC_FS
1507 strlcpy(buf, xt_prefix[af], sizeof(buf));
1508 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1509 remove_proc_entry(buf, net->proc_net);
1512 strlcpy(buf, xt_prefix[af], sizeof(buf));
1513 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1514 remove_proc_entry(buf, net->proc_net);
1519 EXPORT_SYMBOL_GPL(xt_proto_init);
1521 void xt_proto_fini(struct net *net, u_int8_t af)
1523 #ifdef CONFIG_PROC_FS
1524 char buf[XT_FUNCTION_MAXNAMELEN];
1526 strlcpy(buf, xt_prefix[af], sizeof(buf));
1527 strlcat(buf, FORMAT_TABLES, sizeof(buf));
1528 remove_proc_entry(buf, net->proc_net);
1530 strlcpy(buf, xt_prefix[af], sizeof(buf));
1531 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1532 remove_proc_entry(buf, net->proc_net);
1534 strlcpy(buf, xt_prefix[af], sizeof(buf));
1535 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1536 remove_proc_entry(buf, net->proc_net);
1537 #endif /*CONFIG_PROC_FS*/
1539 EXPORT_SYMBOL_GPL(xt_proto_fini);
1541 static int __net_init xt_net_init(struct net *net)
1545 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1546 INIT_LIST_HEAD(&net->xt.tables[i]);
1550 static struct pernet_operations xt_net_ops = {
1551 .init = xt_net_init,
1554 static int __init xt_init(void)
1559 for_each_possible_cpu(i) {
1560 seqcount_init(&per_cpu(xt_recseq, i));
1563 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1567 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1568 mutex_init(&xt[i].mutex);
1569 #ifdef CONFIG_COMPAT
1570 mutex_init(&xt[i].compat_mutex);
1571 xt[i].compat_tab = NULL;
1573 INIT_LIST_HEAD(&xt[i].target);
1574 INIT_LIST_HEAD(&xt[i].match);
1576 rv = register_pernet_subsys(&xt_net_ops);
1582 static void __exit xt_fini(void)
1584 unregister_pernet_subsys(&xt_net_ops);
1588 module_init(xt_init);
1589 module_exit(xt_fini);