1 // SPDX-License-Identifier: GPL-2.0-only
3 * Packet matching code.
5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/cache.h>
11 #include <linux/capability.h>
12 #include <linux/skbuff.h>
13 #include <linux/kmod.h>
14 #include <linux/vmalloc.h>
15 #include <linux/netdevice.h>
16 #include <linux/module.h>
18 #include <net/compat.h>
19 #include <linux/uaccess.h>
20 #include <linux/mutex.h>
21 #include <linux/proc_fs.h>
22 #include <linux/err.h>
23 #include <linux/cpumask.h>
25 #include <linux/netfilter/x_tables.h>
26 #include <linux/netfilter_ipv4/ip_tables.h>
27 #include <net/netfilter/nf_log.h>
28 #include "../../netfilter/xt_repldata.h"
30 MODULE_LICENSE("GPL");
32 MODULE_DESCRIPTION("IPv4 packet filter");
34 void *ipt_alloc_initial_table(const struct xt_table *info)
36 return xt_alloc_initial_table(ipt, IPT);
38 EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
40 /* Returns whether matches rule or not. */
41 /* Performance critical - called for every packet */
43 ip_packet_match(const struct iphdr *ip,
46 const struct ipt_ip *ipinfo,
51 if (NF_INVF(ipinfo, IPT_INV_SRCIP,
52 (ip->saddr & ipinfo->smsk.s_addr) != ipinfo->src.s_addr) ||
53 NF_INVF(ipinfo, IPT_INV_DSTIP,
54 (ip->daddr & ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr))
57 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
59 if (NF_INVF(ipinfo, IPT_INV_VIA_IN, ret != 0))
62 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
64 if (NF_INVF(ipinfo, IPT_INV_VIA_OUT, ret != 0))
67 /* Check specific protocol */
69 NF_INVF(ipinfo, IPT_INV_PROTO, ip->protocol != ipinfo->proto))
72 /* If we have a fragment rule but the packet is not a fragment
73 * then we return zero */
74 if (NF_INVF(ipinfo, IPT_INV_FRAG,
75 (ipinfo->flags & IPT_F_FRAG) && !isfrag))
82 ip_checkentry(const struct ipt_ip *ip)
84 if (ip->flags & ~IPT_F_MASK)
86 if (ip->invflags & ~IPT_INV_MASK)
92 ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
94 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
99 /* Performance critical */
100 static inline struct ipt_entry *
101 get_entry(const void *base, unsigned int offset)
103 return (struct ipt_entry *)(base + offset);
106 /* All zeroes == unconditional rule. */
107 /* Mildly perf critical (only if packet tracing is on) */
108 static inline bool unconditional(const struct ipt_entry *e)
110 static const struct ipt_ip uncond;
112 return e->target_offset == sizeof(struct ipt_entry) &&
113 memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
116 /* for const-correctness */
117 static inline const struct xt_entry_target *
118 ipt_get_target_c(const struct ipt_entry *e)
120 return ipt_get_target((struct ipt_entry *)e);
123 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
124 static const char *const hooknames[] = {
125 [NF_INET_PRE_ROUTING] = "PREROUTING",
126 [NF_INET_LOCAL_IN] = "INPUT",
127 [NF_INET_FORWARD] = "FORWARD",
128 [NF_INET_LOCAL_OUT] = "OUTPUT",
129 [NF_INET_POST_ROUTING] = "POSTROUTING",
132 enum nf_ip_trace_comments {
133 NF_IP_TRACE_COMMENT_RULE,
134 NF_IP_TRACE_COMMENT_RETURN,
135 NF_IP_TRACE_COMMENT_POLICY,
138 static const char *const comments[] = {
139 [NF_IP_TRACE_COMMENT_RULE] = "rule",
140 [NF_IP_TRACE_COMMENT_RETURN] = "return",
141 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
144 static const struct nf_loginfo trace_loginfo = {
145 .type = NF_LOG_TYPE_LOG,
149 .logflags = NF_LOG_DEFAULT_MASK,
154 /* Mildly perf critical (only if packet tracing is on) */
156 get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
157 const char *hookname, const char **chainname,
158 const char **comment, unsigned int *rulenum)
160 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
162 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
163 /* Head of user chain: ERROR target with chainname */
164 *chainname = t->target.data;
169 if (unconditional(s) &&
170 strcmp(t->target.u.kernel.target->name,
171 XT_STANDARD_TARGET) == 0 &&
173 /* Tail of chains: STANDARD target (return/policy) */
174 *comment = *chainname == hookname
175 ? comments[NF_IP_TRACE_COMMENT_POLICY]
176 : comments[NF_IP_TRACE_COMMENT_RETURN];
185 static void trace_packet(struct net *net,
186 const struct sk_buff *skb,
188 const struct net_device *in,
189 const struct net_device *out,
190 const char *tablename,
191 const struct xt_table_info *private,
192 const struct ipt_entry *e)
194 const struct ipt_entry *root;
195 const char *hookname, *chainname, *comment;
196 const struct ipt_entry *iter;
197 unsigned int rulenum = 0;
199 root = get_entry(private->entries, private->hook_entry[hook]);
201 hookname = chainname = hooknames[hook];
202 comment = comments[NF_IP_TRACE_COMMENT_RULE];
204 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
205 if (get_chainname_rulenum(iter, e, hookname,
206 &chainname, &comment, &rulenum) != 0)
209 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
210 "TRACE: %s:%s:%s:%u ",
211 tablename, chainname, comment, rulenum);
216 struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
218 return (void *)entry + entry->next_offset;
221 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
223 ipt_do_table(void *priv,
225 const struct nf_hook_state *state)
227 const struct xt_table *table = priv;
228 unsigned int hook = state->hook;
229 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
230 const struct iphdr *ip;
231 /* Initializing verdict to NF_DROP keeps gcc happy. */
232 unsigned int verdict = NF_DROP;
233 const char *indev, *outdev;
234 const void *table_base;
235 struct ipt_entry *e, **jumpstack;
236 unsigned int stackidx, cpu;
237 const struct xt_table_info *private;
238 struct xt_action_param acpar;
244 indev = state->in ? state->in->name : nulldevname;
245 outdev = state->out ? state->out->name : nulldevname;
246 /* We handle fragments by dealing with the first fragment as
247 * if it was a normal packet. All other fragments are treated
248 * normally, except that they will NEVER match rules that ask
249 * things we don't know, ie. tcp syn flag or ports). If the
250 * rule is also a fragment-specific rule, non-fragments won't
252 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
253 acpar.thoff = ip_hdrlen(skb);
254 acpar.hotdrop = false;
257 WARN_ON(!(table->valid_hooks & (1 << hook)));
259 addend = xt_write_recseq_begin();
260 private = READ_ONCE(table->private); /* Address dependency. */
261 cpu = smp_processor_id();
262 table_base = private->entries;
263 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
265 /* Switch to alternate jumpstack if we're being invoked via TEE.
266 * TEE issues XT_CONTINUE verdict on original skb so we must not
267 * clobber the jumpstack.
269 * For recursion via REJECT or SYNPROXY the stack will be clobbered
270 * but it is no problem since absolute verdict is issued by these.
272 if (static_key_false(&xt_tee_enabled))
273 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
275 e = get_entry(table_base, private->hook_entry[hook]);
278 const struct xt_entry_target *t;
279 const struct xt_entry_match *ematch;
280 struct xt_counters *counter;
283 if (!ip_packet_match(ip, indev, outdev,
284 &e->ip, acpar.fragoff)) {
286 e = ipt_next_entry(e);
290 xt_ematch_foreach(ematch, e) {
291 acpar.match = ematch->u.kernel.match;
292 acpar.matchinfo = ematch->data;
293 if (!acpar.match->match(skb, &acpar))
297 counter = xt_get_this_cpu_counter(&e->counters);
298 ADD_COUNTER(*counter, skb->len, 1);
300 t = ipt_get_target_c(e);
301 WARN_ON(!t->u.kernel.target);
303 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
304 /* The packet is traced: log it */
305 if (unlikely(skb->nf_trace))
306 trace_packet(state->net, skb, hook, state->in,
307 state->out, table->name, private, e);
309 /* Standard target? */
310 if (!t->u.kernel.target->target) {
313 v = ((struct xt_standard_target *)t)->verdict;
315 /* Pop from stack? */
316 if (v != XT_RETURN) {
317 verdict = (unsigned int)(-v) - 1;
321 e = get_entry(table_base,
322 private->underflow[hook]);
324 e = jumpstack[--stackidx];
325 e = ipt_next_entry(e);
329 if (table_base + v != ipt_next_entry(e) &&
330 !(e->ip.flags & IPT_F_GOTO)) {
331 if (unlikely(stackidx >= private->stacksize)) {
335 jumpstack[stackidx++] = e;
338 e = get_entry(table_base, v);
342 acpar.target = t->u.kernel.target;
343 acpar.targinfo = t->data;
345 verdict = t->u.kernel.target->target(skb, &acpar);
346 if (verdict == XT_CONTINUE) {
347 /* Target might have changed stuff. */
349 e = ipt_next_entry(e);
354 } while (!acpar.hotdrop);
356 xt_write_recseq_end(addend);
364 /* Figures out from what hook each rule can be called: returns 0 if
365 there are loops. Puts hook bitmask in comefrom. */
367 mark_source_chains(const struct xt_table_info *newinfo,
368 unsigned int valid_hooks, void *entry0,
369 unsigned int *offsets)
373 /* No recursion; use packet counter to save back ptrs (reset
374 to 0 as we leave), and comefrom to save source hook bitmask */
375 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
376 unsigned int pos = newinfo->hook_entry[hook];
377 struct ipt_entry *e = entry0 + pos;
379 if (!(valid_hooks & (1 << hook)))
382 /* Set initial back pointer. */
383 e->counters.pcnt = pos;
386 const struct xt_standard_target *t
387 = (void *)ipt_get_target_c(e);
388 int visited = e->comefrom & (1 << hook);
390 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
393 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
395 /* Unconditional return/END. */
396 if ((unconditional(e) &&
397 (strcmp(t->target.u.user.name,
398 XT_STANDARD_TARGET) == 0) &&
399 t->verdict < 0) || visited) {
400 unsigned int oldpos, size;
402 /* Return: backtrack through the last
405 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
407 pos = e->counters.pcnt;
408 e->counters.pcnt = 0;
410 /* We're at the start. */
415 } while (oldpos == pos + e->next_offset);
418 size = e->next_offset;
419 e = entry0 + pos + size;
420 if (pos + size >= newinfo->size)
422 e->counters.pcnt = pos;
425 int newpos = t->verdict;
427 if (strcmp(t->target.u.user.name,
428 XT_STANDARD_TARGET) == 0 &&
430 /* This a jump; chase it. */
431 if (!xt_find_jump_offset(offsets, newpos,
435 /* ... this is a fallthru */
436 newpos = pos + e->next_offset;
437 if (newpos >= newinfo->size)
441 e->counters.pcnt = pos;
450 static void cleanup_match(struct xt_entry_match *m, struct net *net)
452 struct xt_mtdtor_param par;
455 par.match = m->u.kernel.match;
456 par.matchinfo = m->data;
457 par.family = NFPROTO_IPV4;
458 if (par.match->destroy != NULL)
459 par.match->destroy(&par);
460 module_put(par.match->me);
464 check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
466 const struct ipt_ip *ip = par->entryinfo;
468 par->match = m->u.kernel.match;
469 par->matchinfo = m->data;
471 return xt_check_match(par, m->u.match_size - sizeof(*m),
472 ip->proto, ip->invflags & IPT_INV_PROTO);
476 find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
478 struct xt_match *match;
481 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
484 return PTR_ERR(match);
485 m->u.kernel.match = match;
487 ret = check_match(m, par);
493 module_put(m->u.kernel.match->me);
497 static int check_target(struct ipt_entry *e, struct net *net, const char *name)
499 struct xt_entry_target *t = ipt_get_target(e);
500 struct xt_tgchk_param par = {
504 .target = t->u.kernel.target,
506 .hook_mask = e->comefrom,
507 .family = NFPROTO_IPV4,
510 return xt_check_target(&par, t->u.target_size - sizeof(*t),
511 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
515 find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
517 struct xt_percpu_counter_alloc_state *alloc_state)
519 struct xt_entry_target *t;
520 struct xt_target *target;
523 struct xt_mtchk_param mtpar;
524 struct xt_entry_match *ematch;
526 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
530 memset(&mtpar, 0, sizeof(mtpar));
533 mtpar.entryinfo = &e->ip;
534 mtpar.hook_mask = e->comefrom;
535 mtpar.family = NFPROTO_IPV4;
536 xt_ematch_foreach(ematch, e) {
537 ret = find_check_match(ematch, &mtpar);
539 goto cleanup_matches;
543 t = ipt_get_target(e);
544 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
546 if (IS_ERR(target)) {
547 ret = PTR_ERR(target);
548 goto cleanup_matches;
550 t->u.kernel.target = target;
552 ret = check_target(e, net, name);
558 module_put(t->u.kernel.target->me);
560 xt_ematch_foreach(ematch, e) {
563 cleanup_match(ematch, net);
566 xt_percpu_counter_free(&e->counters);
571 static bool check_underflow(const struct ipt_entry *e)
573 const struct xt_entry_target *t;
574 unsigned int verdict;
576 if (!unconditional(e))
578 t = ipt_get_target_c(e);
579 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
581 verdict = ((struct xt_standard_target *)t)->verdict;
582 verdict = -verdict - 1;
583 return verdict == NF_DROP || verdict == NF_ACCEPT;
587 check_entry_size_and_hooks(struct ipt_entry *e,
588 struct xt_table_info *newinfo,
589 const unsigned char *base,
590 const unsigned char *limit,
591 const unsigned int *hook_entries,
592 const unsigned int *underflows,
593 unsigned int valid_hooks)
598 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
599 (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
600 (unsigned char *)e + e->next_offset > limit)
604 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
607 if (!ip_checkentry(&e->ip))
610 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
615 /* Check hooks & underflows */
616 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
617 if (!(valid_hooks & (1 << h)))
619 if ((unsigned char *)e - base == hook_entries[h])
620 newinfo->hook_entry[h] = hook_entries[h];
621 if ((unsigned char *)e - base == underflows[h]) {
622 if (!check_underflow(e))
625 newinfo->underflow[h] = underflows[h];
629 /* Clear counters and comefrom */
630 e->counters = ((struct xt_counters) { 0, 0 });
636 cleanup_entry(struct ipt_entry *e, struct net *net)
638 struct xt_tgdtor_param par;
639 struct xt_entry_target *t;
640 struct xt_entry_match *ematch;
642 /* Cleanup all matches */
643 xt_ematch_foreach(ematch, e)
644 cleanup_match(ematch, net);
645 t = ipt_get_target(e);
648 par.target = t->u.kernel.target;
649 par.targinfo = t->data;
650 par.family = NFPROTO_IPV4;
651 if (par.target->destroy != NULL)
652 par.target->destroy(&par);
653 module_put(par.target->me);
654 xt_percpu_counter_free(&e->counters);
657 /* Checks and translates the user-supplied table segment (held in
660 translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
661 const struct ipt_replace *repl)
663 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
664 struct ipt_entry *iter;
665 unsigned int *offsets;
669 newinfo->size = repl->size;
670 newinfo->number = repl->num_entries;
672 /* Init all hooks to impossible value. */
673 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
674 newinfo->hook_entry[i] = 0xFFFFFFFF;
675 newinfo->underflow[i] = 0xFFFFFFFF;
678 offsets = xt_alloc_entry_offsets(newinfo->number);
682 /* Walk through entries, checking offsets. */
683 xt_entry_foreach(iter, entry0, newinfo->size) {
684 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
691 if (i < repl->num_entries)
692 offsets[i] = (void *)iter - entry0;
694 if (strcmp(ipt_get_target(iter)->u.user.name,
695 XT_ERROR_TARGET) == 0)
696 ++newinfo->stacksize;
700 if (i != repl->num_entries)
703 ret = xt_check_table_hooks(newinfo, repl->valid_hooks);
707 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
713 /* Finally, each sanity check must pass */
715 xt_entry_foreach(iter, entry0, newinfo->size) {
716 ret = find_check_entry(iter, net, repl->name, repl->size,
724 xt_entry_foreach(iter, entry0, newinfo->size) {
727 cleanup_entry(iter, net);
739 get_counters(const struct xt_table_info *t,
740 struct xt_counters counters[])
742 struct ipt_entry *iter;
746 for_each_possible_cpu(cpu) {
747 seqcount_t *s = &per_cpu(xt_recseq, cpu);
750 xt_entry_foreach(iter, t->entries, t->size) {
751 struct xt_counters *tmp;
755 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
757 start = read_seqcount_begin(s);
760 } while (read_seqcount_retry(s, start));
762 ADD_COUNTER(counters[i], bcnt, pcnt);
763 ++i; /* macro does multi eval of i */
769 static void get_old_counters(const struct xt_table_info *t,
770 struct xt_counters counters[])
772 struct ipt_entry *iter;
775 for_each_possible_cpu(cpu) {
777 xt_entry_foreach(iter, t->entries, t->size) {
778 const struct xt_counters *tmp;
780 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
781 ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
782 ++i; /* macro does multi eval of i */
789 static struct xt_counters *alloc_counters(const struct xt_table *table)
791 unsigned int countersize;
792 struct xt_counters *counters;
793 const struct xt_table_info *private = table->private;
795 /* We need atomic snapshot of counters: rest doesn't change
796 (other than comefrom, which userspace doesn't care
798 countersize = sizeof(struct xt_counters) * private->number;
799 counters = vzalloc(countersize);
801 if (counters == NULL)
802 return ERR_PTR(-ENOMEM);
804 get_counters(private, counters);
810 copy_entries_to_user(unsigned int total_size,
811 const struct xt_table *table,
812 void __user *userptr)
814 unsigned int off, num;
815 const struct ipt_entry *e;
816 struct xt_counters *counters;
817 const struct xt_table_info *private = table->private;
819 const void *loc_cpu_entry;
821 counters = alloc_counters(table);
822 if (IS_ERR(counters))
823 return PTR_ERR(counters);
825 loc_cpu_entry = private->entries;
827 /* FIXME: use iterator macros --RR */
828 /* ... then go back and fix counters and names */
829 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
831 const struct xt_entry_match *m;
832 const struct xt_entry_target *t;
834 e = loc_cpu_entry + off;
835 if (copy_to_user(userptr + off, e, sizeof(*e))) {
839 if (copy_to_user(userptr + off
840 + offsetof(struct ipt_entry, counters),
842 sizeof(counters[num])) != 0) {
847 for (i = sizeof(struct ipt_entry);
848 i < e->target_offset;
849 i += m->u.match_size) {
852 if (xt_match_to_user(m, userptr + off + i)) {
858 t = ipt_get_target_c(e);
859 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
870 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
871 static void compat_standard_from_user(void *dst, const void *src)
873 int v = *(compat_int_t *)src;
876 v += xt_compat_calc_jump(AF_INET, v);
877 memcpy(dst, &v, sizeof(v));
880 static int compat_standard_to_user(void __user *dst, const void *src)
882 compat_int_t cv = *(int *)src;
885 cv -= xt_compat_calc_jump(AF_INET, cv);
886 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
889 static int compat_calc_entry(const struct ipt_entry *e,
890 const struct xt_table_info *info,
891 const void *base, struct xt_table_info *newinfo)
893 const struct xt_entry_match *ematch;
894 const struct xt_entry_target *t;
895 unsigned int entry_offset;
898 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
899 entry_offset = (void *)e - base;
900 xt_ematch_foreach(ematch, e)
901 off += xt_compat_match_offset(ematch->u.kernel.match);
902 t = ipt_get_target_c(e);
903 off += xt_compat_target_offset(t->u.kernel.target);
904 newinfo->size -= off;
905 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
909 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
910 if (info->hook_entry[i] &&
911 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
912 newinfo->hook_entry[i] -= off;
913 if (info->underflow[i] &&
914 (e < (struct ipt_entry *)(base + info->underflow[i])))
915 newinfo->underflow[i] -= off;
920 static int compat_table_info(const struct xt_table_info *info,
921 struct xt_table_info *newinfo)
923 struct ipt_entry *iter;
924 const void *loc_cpu_entry;
927 if (!newinfo || !info)
930 /* we dont care about newinfo->entries */
931 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
932 newinfo->initial_entries = 0;
933 loc_cpu_entry = info->entries;
934 ret = xt_compat_init_offsets(AF_INET, info->number);
937 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
938 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
946 static int get_info(struct net *net, void __user *user, const int *len)
948 char name[XT_TABLE_MAXNAMELEN];
952 if (*len != sizeof(struct ipt_getinfo))
955 if (copy_from_user(name, user, sizeof(name)) != 0)
958 name[XT_TABLE_MAXNAMELEN-1] = '\0';
959 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
960 if (in_compat_syscall())
961 xt_compat_lock(AF_INET);
963 t = xt_request_find_table_lock(net, AF_INET, name);
965 struct ipt_getinfo info;
966 const struct xt_table_info *private = t->private;
967 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
968 struct xt_table_info tmp;
970 if (in_compat_syscall()) {
971 ret = compat_table_info(private, &tmp);
972 xt_compat_flush_offsets(AF_INET);
976 memset(&info, 0, sizeof(info));
977 info.valid_hooks = t->valid_hooks;
978 memcpy(info.hook_entry, private->hook_entry,
979 sizeof(info.hook_entry));
980 memcpy(info.underflow, private->underflow,
981 sizeof(info.underflow));
982 info.num_entries = private->number;
983 info.size = private->size;
984 strscpy(info.name, name);
986 if (copy_to_user(user, &info, *len) != 0)
995 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
996 if (in_compat_syscall())
997 xt_compat_unlock(AF_INET);
1003 get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1007 struct ipt_get_entries get;
1010 if (*len < sizeof(get))
1012 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1014 if (*len != sizeof(struct ipt_get_entries) + get.size)
1016 get.name[sizeof(get.name) - 1] = '\0';
1018 t = xt_find_table_lock(net, AF_INET, get.name);
1020 const struct xt_table_info *private = t->private;
1021 if (get.size == private->size)
1022 ret = copy_entries_to_user(private->size,
1023 t, uptr->entrytable);
1036 __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1037 struct xt_table_info *newinfo, unsigned int num_counters,
1038 void __user *counters_ptr)
1042 struct xt_table_info *oldinfo;
1043 struct xt_counters *counters;
1044 struct ipt_entry *iter;
1046 counters = xt_counters_alloc(num_counters);
1052 t = xt_request_find_table_lock(net, AF_INET, name);
1055 goto free_newinfo_counters_untrans;
1059 if (valid_hooks != t->valid_hooks) {
1064 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1068 /* Update module usage count based on number of rules */
1069 if ((oldinfo->number > oldinfo->initial_entries) ||
1070 (newinfo->number <= oldinfo->initial_entries))
1072 if ((oldinfo->number > oldinfo->initial_entries) &&
1073 (newinfo->number <= oldinfo->initial_entries))
1078 get_old_counters(oldinfo, counters);
1080 /* Decrease module usage counts and free resource */
1081 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1082 cleanup_entry(iter, net);
1084 xt_free_table_info(oldinfo);
1085 if (copy_to_user(counters_ptr, counters,
1086 sizeof(struct xt_counters) * num_counters) != 0) {
1087 /* Silent error, can't fail, new table is already in place */
1088 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1096 free_newinfo_counters_untrans:
1103 do_replace(struct net *net, sockptr_t arg, unsigned int len)
1106 struct ipt_replace tmp;
1107 struct xt_table_info *newinfo;
1108 void *loc_cpu_entry;
1109 struct ipt_entry *iter;
1111 if (len < sizeof(tmp))
1113 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1116 /* overflow check */
1117 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1119 if (tmp.num_counters == 0)
1121 if ((u64)len < (u64)tmp.size + sizeof(tmp))
1124 tmp.name[sizeof(tmp.name)-1] = 0;
1126 newinfo = xt_alloc_table_info(tmp.size);
1130 loc_cpu_entry = newinfo->entries;
1131 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1137 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1141 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1142 tmp.num_counters, tmp.counters);
1144 goto free_newinfo_untrans;
1147 free_newinfo_untrans:
1148 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1149 cleanup_entry(iter, net);
1151 xt_free_table_info(newinfo);
1156 do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
1159 struct xt_counters_info tmp;
1160 struct xt_counters *paddc;
1162 const struct xt_table_info *private;
1164 struct ipt_entry *iter;
1165 unsigned int addend;
1167 paddc = xt_copy_counters(arg, len, &tmp);
1169 return PTR_ERR(paddc);
1171 t = xt_find_table_lock(net, AF_INET, tmp.name);
1178 private = t->private;
1179 if (private->number != tmp.num_counters) {
1181 goto unlock_up_free;
1185 addend = xt_write_recseq_begin();
1186 xt_entry_foreach(iter, private->entries, private->size) {
1187 struct xt_counters *tmp;
1189 tmp = xt_get_this_cpu_counter(&iter->counters);
1190 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1193 xt_write_recseq_end(addend);
1204 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1205 struct compat_ipt_replace {
1206 char name[XT_TABLE_MAXNAMELEN];
1210 u32 hook_entry[NF_INET_NUMHOOKS];
1211 u32 underflow[NF_INET_NUMHOOKS];
1213 compat_uptr_t counters; /* struct xt_counters * */
1214 struct compat_ipt_entry entries[];
1218 compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1219 unsigned int *size, struct xt_counters *counters,
1222 struct xt_entry_target *t;
1223 struct compat_ipt_entry __user *ce;
1224 u_int16_t target_offset, next_offset;
1225 compat_uint_t origsize;
1226 const struct xt_entry_match *ematch;
1231 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1232 copy_to_user(&ce->counters, &counters[i],
1233 sizeof(counters[i])) != 0)
1236 *dstptr += sizeof(struct compat_ipt_entry);
1237 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1239 xt_ematch_foreach(ematch, e) {
1240 ret = xt_compat_match_to_user(ematch, dstptr, size);
1244 target_offset = e->target_offset - (origsize - *size);
1245 t = ipt_get_target(e);
1246 ret = xt_compat_target_to_user(t, dstptr, size);
1249 next_offset = e->next_offset - (origsize - *size);
1250 if (put_user(target_offset, &ce->target_offset) != 0 ||
1251 put_user(next_offset, &ce->next_offset) != 0)
1257 compat_find_calc_match(struct xt_entry_match *m,
1258 const struct ipt_ip *ip,
1261 struct xt_match *match;
1263 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1264 m->u.user.revision);
1266 return PTR_ERR(match);
1268 m->u.kernel.match = match;
1269 *size += xt_compat_match_offset(match);
1273 static void compat_release_entry(struct compat_ipt_entry *e)
1275 struct xt_entry_target *t;
1276 struct xt_entry_match *ematch;
1278 /* Cleanup all matches */
1279 xt_ematch_foreach(ematch, e)
1280 module_put(ematch->u.kernel.match->me);
1281 t = compat_ipt_get_target(e);
1282 module_put(t->u.kernel.target->me);
1286 check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
1287 struct xt_table_info *newinfo,
1289 const unsigned char *base,
1290 const unsigned char *limit)
1292 struct xt_entry_match *ematch;
1293 struct xt_entry_target *t;
1294 struct xt_target *target;
1295 unsigned int entry_offset;
1299 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1300 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
1301 (unsigned char *)e + e->next_offset > limit)
1304 if (e->next_offset < sizeof(struct compat_ipt_entry) +
1305 sizeof(struct compat_xt_entry_target))
1308 if (!ip_checkentry(&e->ip))
1311 ret = xt_compat_check_entry_offsets(e, e->elems,
1312 e->target_offset, e->next_offset);
1316 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1317 entry_offset = (void *)e - (void *)base;
1319 xt_ematch_foreach(ematch, e) {
1320 ret = compat_find_calc_match(ematch, &e->ip, &off);
1322 goto release_matches;
1326 t = compat_ipt_get_target(e);
1327 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1328 t->u.user.revision);
1329 if (IS_ERR(target)) {
1330 ret = PTR_ERR(target);
1331 goto release_matches;
1333 t->u.kernel.target = target;
1335 off += xt_compat_target_offset(target);
1337 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1344 module_put(t->u.kernel.target->me);
1346 xt_ematch_foreach(ematch, e) {
1349 module_put(ematch->u.kernel.match->me);
1355 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
1357 struct xt_table_info *newinfo, unsigned char *base)
1359 struct xt_entry_target *t;
1360 struct ipt_entry *de;
1361 unsigned int origsize;
1363 struct xt_entry_match *ematch;
1367 memcpy(de, e, sizeof(struct ipt_entry));
1368 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1370 *dstptr += sizeof(struct ipt_entry);
1371 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1373 xt_ematch_foreach(ematch, e)
1374 xt_compat_match_from_user(ematch, dstptr, size);
1376 de->target_offset = e->target_offset - (origsize - *size);
1377 t = compat_ipt_get_target(e);
1378 xt_compat_target_from_user(t, dstptr, size);
1380 de->next_offset = e->next_offset - (origsize - *size);
1382 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1383 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1384 newinfo->hook_entry[h] -= origsize - *size;
1385 if ((unsigned char *)de - base < newinfo->underflow[h])
1386 newinfo->underflow[h] -= origsize - *size;
1391 translate_compat_table(struct net *net,
1392 struct xt_table_info **pinfo,
1394 const struct compat_ipt_replace *compatr)
1397 struct xt_table_info *newinfo, *info;
1398 void *pos, *entry0, *entry1;
1399 struct compat_ipt_entry *iter0;
1400 struct ipt_replace repl;
1406 size = compatr->size;
1407 info->number = compatr->num_entries;
1410 xt_compat_lock(AF_INET);
1411 ret = xt_compat_init_offsets(AF_INET, compatr->num_entries);
1414 /* Walk through entries, checking offsets. */
1415 xt_entry_foreach(iter0, entry0, compatr->size) {
1416 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1418 entry0 + compatr->size);
1425 if (j != compatr->num_entries)
1429 newinfo = xt_alloc_table_info(size);
1433 memset(newinfo->entries, 0, size);
1435 newinfo->number = compatr->num_entries;
1436 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1437 newinfo->hook_entry[i] = compatr->hook_entry[i];
1438 newinfo->underflow[i] = compatr->underflow[i];
1440 entry1 = newinfo->entries;
1442 size = compatr->size;
1443 xt_entry_foreach(iter0, entry0, compatr->size)
1444 compat_copy_entry_from_user(iter0, &pos, &size,
1447 /* all module references in entry0 are now gone.
1448 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1449 * generated by 64bit userspace.
1451 * Call standard translate_table() to validate all hook_entrys,
1452 * underflows, check for loops, etc.
1454 xt_compat_flush_offsets(AF_INET);
1455 xt_compat_unlock(AF_INET);
1457 memcpy(&repl, compatr, sizeof(*compatr));
1459 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1460 repl.hook_entry[i] = newinfo->hook_entry[i];
1461 repl.underflow[i] = newinfo->underflow[i];
1464 repl.num_counters = 0;
1465 repl.counters = NULL;
1466 repl.size = newinfo->size;
1467 ret = translate_table(net, newinfo, entry1, &repl);
1473 xt_free_table_info(info);
1477 xt_free_table_info(newinfo);
1480 xt_compat_flush_offsets(AF_INET);
1481 xt_compat_unlock(AF_INET);
1482 xt_entry_foreach(iter0, entry0, compatr->size) {
1485 compat_release_entry(iter0);
1491 compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
1494 struct compat_ipt_replace tmp;
1495 struct xt_table_info *newinfo;
1496 void *loc_cpu_entry;
1497 struct ipt_entry *iter;
1499 if (len < sizeof(tmp))
1501 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1504 /* overflow check */
1505 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1507 if (tmp.num_counters == 0)
1509 if ((u64)len < (u64)tmp.size + sizeof(tmp))
1512 tmp.name[sizeof(tmp.name)-1] = 0;
1514 newinfo = xt_alloc_table_info(tmp.size);
1518 loc_cpu_entry = newinfo->entries;
1519 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1525 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1529 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1530 tmp.num_counters, compat_ptr(tmp.counters));
1532 goto free_newinfo_untrans;
1535 free_newinfo_untrans:
1536 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1537 cleanup_entry(iter, net);
1539 xt_free_table_info(newinfo);
1543 struct compat_ipt_get_entries {
1544 char name[XT_TABLE_MAXNAMELEN];
1546 struct compat_ipt_entry entrytable[];
1550 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1551 void __user *userptr)
1553 struct xt_counters *counters;
1554 const struct xt_table_info *private = table->private;
1559 struct ipt_entry *iter;
1561 counters = alloc_counters(table);
1562 if (IS_ERR(counters))
1563 return PTR_ERR(counters);
1567 xt_entry_foreach(iter, private->entries, total_size) {
1568 ret = compat_copy_entry_to_user(iter, &pos,
1569 &size, counters, i++);
1579 compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1583 struct compat_ipt_get_entries get;
1586 if (*len < sizeof(get))
1589 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1592 if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
1595 get.name[sizeof(get.name) - 1] = '\0';
1597 xt_compat_lock(AF_INET);
1598 t = xt_find_table_lock(net, AF_INET, get.name);
1600 const struct xt_table_info *private = t->private;
1601 struct xt_table_info info;
1602 ret = compat_table_info(private, &info);
1603 if (!ret && get.size == info.size)
1604 ret = compat_copy_entries_to_user(private->size,
1605 t, uptr->entrytable);
1609 xt_compat_flush_offsets(AF_INET);
1615 xt_compat_unlock(AF_INET);
1621 do_ipt_set_ctl(struct sock *sk, int cmd, sockptr_t arg, unsigned int len)
1625 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1629 case IPT_SO_SET_REPLACE:
1630 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1631 if (in_compat_syscall())
1632 ret = compat_do_replace(sock_net(sk), arg, len);
1635 ret = do_replace(sock_net(sk), arg, len);
1638 case IPT_SO_SET_ADD_COUNTERS:
1639 ret = do_add_counters(sock_net(sk), arg, len);
1650 do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1654 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1658 case IPT_SO_GET_INFO:
1659 ret = get_info(sock_net(sk), user, len);
1662 case IPT_SO_GET_ENTRIES:
1663 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1664 if (in_compat_syscall())
1665 ret = compat_get_entries(sock_net(sk), user, len);
1668 ret = get_entries(sock_net(sk), user, len);
1671 case IPT_SO_GET_REVISION_MATCH:
1672 case IPT_SO_GET_REVISION_TARGET: {
1673 struct xt_get_revision rev;
1676 if (*len != sizeof(rev)) {
1680 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1684 rev.name[sizeof(rev.name)-1] = 0;
1686 if (cmd == IPT_SO_GET_REVISION_TARGET)
1691 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1694 "ipt_%s", rev.name);
1705 static void __ipt_unregister_table(struct net *net, struct xt_table *table)
1707 struct xt_table_info *private;
1708 void *loc_cpu_entry;
1709 struct module *table_owner = table->me;
1710 struct ipt_entry *iter;
1712 private = xt_unregister_table(table);
1714 /* Decrease module usage counts and free resources */
1715 loc_cpu_entry = private->entries;
1716 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1717 cleanup_entry(iter, net);
1718 if (private->number > private->initial_entries)
1719 module_put(table_owner);
1720 xt_free_table_info(private);
1723 int ipt_register_table(struct net *net, const struct xt_table *table,
1724 const struct ipt_replace *repl,
1725 const struct nf_hook_ops *template_ops)
1727 struct nf_hook_ops *ops;
1728 unsigned int num_ops;
1730 struct xt_table_info *newinfo;
1731 struct xt_table_info bootstrap = {0};
1732 void *loc_cpu_entry;
1733 struct xt_table *new_table;
1735 newinfo = xt_alloc_table_info(repl->size);
1739 loc_cpu_entry = newinfo->entries;
1740 memcpy(loc_cpu_entry, repl->entries, repl->size);
1742 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1744 xt_free_table_info(newinfo);
1748 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1749 if (IS_ERR(new_table)) {
1750 struct ipt_entry *iter;
1752 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1753 cleanup_entry(iter, net);
1754 xt_free_table_info(newinfo);
1755 return PTR_ERR(new_table);
1758 /* No template? No need to do anything. This is used by 'nat' table, it registers
1759 * with the nat core instead of the netfilter core.
1764 num_ops = hweight32(table->valid_hooks);
1770 ops = kmemdup_array(template_ops, num_ops, sizeof(*ops), GFP_KERNEL);
1776 for (i = 0; i < num_ops; i++)
1777 ops[i].priv = new_table;
1779 new_table->ops = ops;
1781 ret = nf_register_net_hooks(net, ops, num_ops);
1788 __ipt_unregister_table(net, new_table);
1792 void ipt_unregister_table_pre_exit(struct net *net, const char *name)
1794 struct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);
1797 nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks));
1800 void ipt_unregister_table_exit(struct net *net, const char *name)
1802 struct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);
1805 __ipt_unregister_table(net, table);
1808 static struct xt_target ipt_builtin_tg[] __read_mostly = {
1810 .name = XT_STANDARD_TARGET,
1811 .targetsize = sizeof(int),
1812 .family = NFPROTO_IPV4,
1813 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1814 .compatsize = sizeof(compat_int_t),
1815 .compat_from_user = compat_standard_from_user,
1816 .compat_to_user = compat_standard_to_user,
1820 .name = XT_ERROR_TARGET,
1821 .target = ipt_error,
1822 .targetsize = XT_FUNCTION_MAXNAMELEN,
1823 .family = NFPROTO_IPV4,
1827 static struct nf_sockopt_ops ipt_sockopts = {
1829 .set_optmin = IPT_BASE_CTL,
1830 .set_optmax = IPT_SO_SET_MAX+1,
1831 .set = do_ipt_set_ctl,
1832 .get_optmin = IPT_BASE_CTL,
1833 .get_optmax = IPT_SO_GET_MAX+1,
1834 .get = do_ipt_get_ctl,
1835 .owner = THIS_MODULE,
1838 static int __net_init ip_tables_net_init(struct net *net)
1840 return xt_proto_init(net, NFPROTO_IPV4);
1843 static void __net_exit ip_tables_net_exit(struct net *net)
1845 xt_proto_fini(net, NFPROTO_IPV4);
1848 static struct pernet_operations ip_tables_net_ops = {
1849 .init = ip_tables_net_init,
1850 .exit = ip_tables_net_exit,
1853 static int __init ip_tables_init(void)
1857 ret = register_pernet_subsys(&ip_tables_net_ops);
1861 /* No one else will be downing sem now, so we won't sleep */
1862 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1866 /* Register setsockopt */
1867 ret = nf_register_sockopt(&ipt_sockopts);
1874 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1876 unregister_pernet_subsys(&ip_tables_net_ops);
1881 static void __exit ip_tables_fini(void)
1883 nf_unregister_sockopt(&ipt_sockopts);
1885 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1886 unregister_pernet_subsys(&ip_tables_net_ops);
1889 EXPORT_SYMBOL(ipt_register_table);
1890 EXPORT_SYMBOL(ipt_unregister_table_pre_exit);
1891 EXPORT_SYMBOL(ipt_unregister_table_exit);
1892 EXPORT_SYMBOL(ipt_do_table);
1893 module_init(ip_tables_init);
1894 module_exit(ip_tables_fini);