1 // SPDX-License-Identifier: GPL-2.0-only
3 * Packet matching code for ARP packets.
5 * Based heavily, if not almost entirely, upon ip_tables.c framework.
7 * Some ARP specific bits are:
9 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
10 * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/netdevice.h>
17 #include <linux/capability.h>
18 #include <linux/if_arp.h>
19 #include <linux/kmod.h>
20 #include <linux/vmalloc.h>
21 #include <linux/proc_fs.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/mutex.h>
25 #include <linux/err.h>
26 #include <net/compat.h>
28 #include <linux/uaccess.h>
30 #include <linux/netfilter/x_tables.h>
31 #include <linux/netfilter_arp/arp_tables.h>
32 #include "../../netfilter/xt_repldata.h"
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
36 MODULE_DESCRIPTION("arptables core");
38 void *arpt_alloc_initial_table(const struct xt_table *info)
40 return xt_alloc_initial_table(arpt, ARPT);
42 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
44 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
45 const char *hdr_addr, int len)
49 if (len > ARPT_DEV_ADDR_LEN_MAX)
50 len = ARPT_DEV_ADDR_LEN_MAX;
53 for (i = 0; i < len; i++)
54 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
60 * Unfortunately, _b and _mask are not aligned to an int (or long int)
61 * Some arches dont care, unrolling the loop is a win on them.
62 * For other arches, we only have a 16bit alignement.
64 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
66 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
67 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
69 unsigned long ret = 0;
70 const u16 *a = (const u16 *)_a;
71 const u16 *b = (const u16 *)_b;
72 const u16 *mask = (const u16 *)_mask;
75 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
76 ret |= (a[i] ^ b[i]) & mask[i];
81 /* Returns whether packet matches rule or not. */
82 static inline int arp_packet_match(const struct arphdr *arphdr,
83 struct net_device *dev,
86 const struct arpt_arp *arpinfo)
88 const char *arpptr = (char *)(arphdr + 1);
89 const char *src_devaddr, *tgt_devaddr;
90 __be32 src_ipaddr, tgt_ipaddr;
93 if (NF_INVF(arpinfo, ARPT_INV_ARPOP,
94 (arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop))
97 if (NF_INVF(arpinfo, ARPT_INV_ARPHRD,
98 (arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd))
101 if (NF_INVF(arpinfo, ARPT_INV_ARPPRO,
102 (arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro))
105 if (NF_INVF(arpinfo, ARPT_INV_ARPHLN,
106 (arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln))
109 src_devaddr = arpptr;
110 arpptr += dev->addr_len;
111 memcpy(&src_ipaddr, arpptr, sizeof(u32));
112 arpptr += sizeof(u32);
113 tgt_devaddr = arpptr;
114 arpptr += dev->addr_len;
115 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
117 if (NF_INVF(arpinfo, ARPT_INV_SRCDEVADDR,
118 arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr,
120 NF_INVF(arpinfo, ARPT_INV_TGTDEVADDR,
121 arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr,
125 if (NF_INVF(arpinfo, ARPT_INV_SRCIP,
126 (src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr) ||
127 NF_INVF(arpinfo, ARPT_INV_TGTIP,
128 (tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr))
131 /* Look for ifname matches. */
132 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
134 if (NF_INVF(arpinfo, ARPT_INV_VIA_IN, ret != 0))
137 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
139 if (NF_INVF(arpinfo, ARPT_INV_VIA_OUT, ret != 0))
145 static inline int arp_checkentry(const struct arpt_arp *arp)
147 if (arp->flags & ~ARPT_F_MASK)
149 if (arp->invflags & ~ARPT_INV_MASK)
156 arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
158 net_err_ratelimited("arp_tables: error: '%s'\n",
159 (const char *)par->targinfo);
164 static inline const struct xt_entry_target *
165 arpt_get_target_c(const struct arpt_entry *e)
167 return arpt_get_target((struct arpt_entry *)e);
170 static inline struct arpt_entry *
171 get_entry(const void *base, unsigned int offset)
173 return (struct arpt_entry *)(base + offset);
177 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
179 return (void *)entry + entry->next_offset;
182 unsigned int arpt_do_table(struct sk_buff *skb,
183 const struct nf_hook_state *state,
184 struct xt_table *table)
186 unsigned int hook = state->hook;
187 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
188 unsigned int verdict = NF_DROP;
189 const struct arphdr *arp;
190 struct arpt_entry *e, **jumpstack;
191 const char *indev, *outdev;
192 const void *table_base;
193 unsigned int cpu, stackidx = 0;
194 const struct xt_table_info *private;
195 struct xt_action_param acpar;
198 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
201 indev = state->in ? state->in->name : nulldevname;
202 outdev = state->out ? state->out->name : nulldevname;
205 addend = xt_write_recseq_begin();
206 private = rcu_access_pointer(table->private);
207 cpu = smp_processor_id();
208 table_base = private->entries;
209 jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
211 /* No TEE support for arptables, so no need to switch to alternate
212 * stack. All targets that reenter must return absolute verdicts.
214 e = get_entry(table_base, private->hook_entry[hook]);
217 acpar.hotdrop = false;
221 const struct xt_entry_target *t;
222 struct xt_counters *counter;
224 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
225 e = arpt_next_entry(e);
229 counter = xt_get_this_cpu_counter(&e->counters);
230 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
232 t = arpt_get_target_c(e);
234 /* Standard target? */
235 if (!t->u.kernel.target->target) {
238 v = ((struct xt_standard_target *)t)->verdict;
240 /* Pop from stack? */
241 if (v != XT_RETURN) {
242 verdict = (unsigned int)(-v) - 1;
246 e = get_entry(table_base,
247 private->underflow[hook]);
249 e = jumpstack[--stackidx];
250 e = arpt_next_entry(e);
255 != arpt_next_entry(e)) {
256 if (unlikely(stackidx >= private->stacksize)) {
260 jumpstack[stackidx++] = e;
263 e = get_entry(table_base, v);
267 acpar.target = t->u.kernel.target;
268 acpar.targinfo = t->data;
269 verdict = t->u.kernel.target->target(skb, &acpar);
271 if (verdict == XT_CONTINUE) {
272 /* Target might have changed stuff. */
274 e = arpt_next_entry(e);
279 } while (!acpar.hotdrop);
280 xt_write_recseq_end(addend);
289 /* All zeroes == unconditional rule. */
290 static inline bool unconditional(const struct arpt_entry *e)
292 static const struct arpt_arp uncond;
294 return e->target_offset == sizeof(struct arpt_entry) &&
295 memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
298 /* Figures out from what hook each rule can be called: returns 0 if
299 * there are loops. Puts hook bitmask in comefrom.
301 static int mark_source_chains(const struct xt_table_info *newinfo,
302 unsigned int valid_hooks, void *entry0,
303 unsigned int *offsets)
307 /* No recursion; use packet counter to save back ptrs (reset
308 * to 0 as we leave), and comefrom to save source hook bitmask.
310 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
311 unsigned int pos = newinfo->hook_entry[hook];
312 struct arpt_entry *e = entry0 + pos;
314 if (!(valid_hooks & (1 << hook)))
317 /* Set initial back pointer. */
318 e->counters.pcnt = pos;
321 const struct xt_standard_target *t
322 = (void *)arpt_get_target_c(e);
323 int visited = e->comefrom & (1 << hook);
325 if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
329 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
331 /* Unconditional return/END. */
332 if ((unconditional(e) &&
333 (strcmp(t->target.u.user.name,
334 XT_STANDARD_TARGET) == 0) &&
335 t->verdict < 0) || visited) {
336 unsigned int oldpos, size;
338 /* Return: backtrack through the last
342 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
344 pos = e->counters.pcnt;
345 e->counters.pcnt = 0;
347 /* We're at the start. */
352 } while (oldpos == pos + e->next_offset);
355 size = e->next_offset;
356 e = entry0 + pos + size;
357 if (pos + size >= newinfo->size)
359 e->counters.pcnt = pos;
362 int newpos = t->verdict;
364 if (strcmp(t->target.u.user.name,
365 XT_STANDARD_TARGET) == 0 &&
367 /* This a jump; chase it. */
368 if (!xt_find_jump_offset(offsets, newpos,
372 /* ... this is a fallthru */
373 newpos = pos + e->next_offset;
374 if (newpos >= newinfo->size)
378 e->counters.pcnt = pos;
387 static int check_target(struct arpt_entry *e, struct net *net, const char *name)
389 struct xt_entry_target *t = arpt_get_target(e);
390 struct xt_tgchk_param par = {
394 .target = t->u.kernel.target,
396 .hook_mask = e->comefrom,
397 .family = NFPROTO_ARP,
400 return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
404 find_check_entry(struct arpt_entry *e, struct net *net, const char *name,
406 struct xt_percpu_counter_alloc_state *alloc_state)
408 struct xt_entry_target *t;
409 struct xt_target *target;
412 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
415 t = arpt_get_target(e);
416 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
418 if (IS_ERR(target)) {
419 ret = PTR_ERR(target);
422 t->u.kernel.target = target;
424 ret = check_target(e, net, name);
429 module_put(t->u.kernel.target->me);
431 xt_percpu_counter_free(&e->counters);
436 static bool check_underflow(const struct arpt_entry *e)
438 const struct xt_entry_target *t;
439 unsigned int verdict;
441 if (!unconditional(e))
443 t = arpt_get_target_c(e);
444 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
446 verdict = ((struct xt_standard_target *)t)->verdict;
447 verdict = -verdict - 1;
448 return verdict == NF_DROP || verdict == NF_ACCEPT;
451 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
452 struct xt_table_info *newinfo,
453 const unsigned char *base,
454 const unsigned char *limit,
455 const unsigned int *hook_entries,
456 const unsigned int *underflows,
457 unsigned int valid_hooks)
462 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
463 (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
464 (unsigned char *)e + e->next_offset > limit)
468 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target))
471 if (!arp_checkentry(&e->arp))
474 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
479 /* Check hooks & underflows */
480 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
481 if (!(valid_hooks & (1 << h)))
483 if ((unsigned char *)e - base == hook_entries[h])
484 newinfo->hook_entry[h] = hook_entries[h];
485 if ((unsigned char *)e - base == underflows[h]) {
486 if (!check_underflow(e))
489 newinfo->underflow[h] = underflows[h];
493 /* Clear counters and comefrom */
494 e->counters = ((struct xt_counters) { 0, 0 });
499 static void cleanup_entry(struct arpt_entry *e, struct net *net)
501 struct xt_tgdtor_param par;
502 struct xt_entry_target *t;
504 t = arpt_get_target(e);
506 par.target = t->u.kernel.target;
507 par.targinfo = t->data;
508 par.family = NFPROTO_ARP;
509 if (par.target->destroy != NULL)
510 par.target->destroy(&par);
511 module_put(par.target->me);
512 xt_percpu_counter_free(&e->counters);
515 /* Checks and translates the user-supplied table segment (held in
518 static int translate_table(struct net *net,
519 struct xt_table_info *newinfo,
521 const struct arpt_replace *repl)
523 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
524 struct arpt_entry *iter;
525 unsigned int *offsets;
529 newinfo->size = repl->size;
530 newinfo->number = repl->num_entries;
532 /* Init all hooks to impossible value. */
533 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
534 newinfo->hook_entry[i] = 0xFFFFFFFF;
535 newinfo->underflow[i] = 0xFFFFFFFF;
538 offsets = xt_alloc_entry_offsets(newinfo->number);
543 /* Walk through entries, checking offsets. */
544 xt_entry_foreach(iter, entry0, newinfo->size) {
545 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
552 if (i < repl->num_entries)
553 offsets[i] = (void *)iter - entry0;
555 if (strcmp(arpt_get_target(iter)->u.user.name,
556 XT_ERROR_TARGET) == 0)
557 ++newinfo->stacksize;
561 if (i != repl->num_entries)
564 ret = xt_check_table_hooks(newinfo, repl->valid_hooks);
568 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
574 /* Finally, each sanity check must pass */
576 xt_entry_foreach(iter, entry0, newinfo->size) {
577 ret = find_check_entry(iter, net, repl->name, repl->size,
585 xt_entry_foreach(iter, entry0, newinfo->size) {
588 cleanup_entry(iter, net);
599 static void get_counters(const struct xt_table_info *t,
600 struct xt_counters counters[])
602 struct arpt_entry *iter;
606 for_each_possible_cpu(cpu) {
607 seqcount_t *s = &per_cpu(xt_recseq, cpu);
610 xt_entry_foreach(iter, t->entries, t->size) {
611 struct xt_counters *tmp;
615 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
617 start = read_seqcount_begin(s);
620 } while (read_seqcount_retry(s, start));
622 ADD_COUNTER(counters[i], bcnt, pcnt);
629 static void get_old_counters(const struct xt_table_info *t,
630 struct xt_counters counters[])
632 struct arpt_entry *iter;
635 for_each_possible_cpu(cpu) {
637 xt_entry_foreach(iter, t->entries, t->size) {
638 struct xt_counters *tmp;
640 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
641 ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
648 static struct xt_counters *alloc_counters(const struct xt_table *table)
650 unsigned int countersize;
651 struct xt_counters *counters;
652 const struct xt_table_info *private = xt_table_get_private_protected(table);
654 /* We need atomic snapshot of counters: rest doesn't change
655 * (other than comefrom, which userspace doesn't care
658 countersize = sizeof(struct xt_counters) * private->number;
659 counters = vzalloc(countersize);
661 if (counters == NULL)
662 return ERR_PTR(-ENOMEM);
664 get_counters(private, counters);
669 static int copy_entries_to_user(unsigned int total_size,
670 const struct xt_table *table,
671 void __user *userptr)
673 unsigned int off, num;
674 const struct arpt_entry *e;
675 struct xt_counters *counters;
676 struct xt_table_info *private = xt_table_get_private_protected(table);
680 counters = alloc_counters(table);
681 if (IS_ERR(counters))
682 return PTR_ERR(counters);
684 loc_cpu_entry = private->entries;
686 /* FIXME: use iterator macros --RR */
687 /* ... then go back and fix counters and names */
688 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
689 const struct xt_entry_target *t;
691 e = loc_cpu_entry + off;
692 if (copy_to_user(userptr + off, e, sizeof(*e))) {
696 if (copy_to_user(userptr + off
697 + offsetof(struct arpt_entry, counters),
699 sizeof(counters[num])) != 0) {
704 t = arpt_get_target_c(e);
705 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
717 static void compat_standard_from_user(void *dst, const void *src)
719 int v = *(compat_int_t *)src;
722 v += xt_compat_calc_jump(NFPROTO_ARP, v);
723 memcpy(dst, &v, sizeof(v));
726 static int compat_standard_to_user(void __user *dst, const void *src)
728 compat_int_t cv = *(int *)src;
731 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
732 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
735 static int compat_calc_entry(const struct arpt_entry *e,
736 const struct xt_table_info *info,
737 const void *base, struct xt_table_info *newinfo)
739 const struct xt_entry_target *t;
740 unsigned int entry_offset;
743 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
744 entry_offset = (void *)e - base;
746 t = arpt_get_target_c(e);
747 off += xt_compat_target_offset(t->u.kernel.target);
748 newinfo->size -= off;
749 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
753 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
754 if (info->hook_entry[i] &&
755 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
756 newinfo->hook_entry[i] -= off;
757 if (info->underflow[i] &&
758 (e < (struct arpt_entry *)(base + info->underflow[i])))
759 newinfo->underflow[i] -= off;
764 static int compat_table_info(const struct xt_table_info *info,
765 struct xt_table_info *newinfo)
767 struct arpt_entry *iter;
768 const void *loc_cpu_entry;
771 if (!newinfo || !info)
774 /* we dont care about newinfo->entries */
775 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
776 newinfo->initial_entries = 0;
777 loc_cpu_entry = info->entries;
778 ret = xt_compat_init_offsets(NFPROTO_ARP, info->number);
781 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
782 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
790 static int get_info(struct net *net, void __user *user, const int *len)
792 char name[XT_TABLE_MAXNAMELEN];
796 if (*len != sizeof(struct arpt_getinfo))
799 if (copy_from_user(name, user, sizeof(name)) != 0)
802 name[XT_TABLE_MAXNAMELEN-1] = '\0';
804 if (in_compat_syscall())
805 xt_compat_lock(NFPROTO_ARP);
807 t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
809 struct arpt_getinfo info;
810 const struct xt_table_info *private = xt_table_get_private_protected(t);
812 struct xt_table_info tmp;
814 if (in_compat_syscall()) {
815 ret = compat_table_info(private, &tmp);
816 xt_compat_flush_offsets(NFPROTO_ARP);
820 memset(&info, 0, sizeof(info));
821 info.valid_hooks = t->valid_hooks;
822 memcpy(info.hook_entry, private->hook_entry,
823 sizeof(info.hook_entry));
824 memcpy(info.underflow, private->underflow,
825 sizeof(info.underflow));
826 info.num_entries = private->number;
827 info.size = private->size;
828 strcpy(info.name, name);
830 if (copy_to_user(user, &info, *len) != 0)
839 if (in_compat_syscall())
840 xt_compat_unlock(NFPROTO_ARP);
845 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
849 struct arpt_get_entries get;
852 if (*len < sizeof(get))
854 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
856 if (*len != sizeof(struct arpt_get_entries) + get.size)
859 get.name[sizeof(get.name) - 1] = '\0';
861 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
863 const struct xt_table_info *private = xt_table_get_private_protected(t);
865 if (get.size == private->size)
866 ret = copy_entries_to_user(private->size,
867 t, uptr->entrytable);
879 static int __do_replace(struct net *net, const char *name,
880 unsigned int valid_hooks,
881 struct xt_table_info *newinfo,
882 unsigned int num_counters,
883 void __user *counters_ptr)
887 struct xt_table_info *oldinfo;
888 struct xt_counters *counters;
889 void *loc_cpu_old_entry;
890 struct arpt_entry *iter;
893 counters = xt_counters_alloc(num_counters);
899 t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
902 goto free_newinfo_counters_untrans;
906 if (valid_hooks != t->valid_hooks) {
911 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
915 /* Update module usage count based on number of rules */
916 if ((oldinfo->number > oldinfo->initial_entries) ||
917 (newinfo->number <= oldinfo->initial_entries))
919 if ((oldinfo->number > oldinfo->initial_entries) &&
920 (newinfo->number <= oldinfo->initial_entries))
925 get_old_counters(oldinfo, counters);
927 /* Decrease module usage counts and free resource */
928 loc_cpu_old_entry = oldinfo->entries;
929 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
930 cleanup_entry(iter, net);
932 xt_free_table_info(oldinfo);
933 if (copy_to_user(counters_ptr, counters,
934 sizeof(struct xt_counters) * num_counters) != 0) {
935 /* Silent error, can't fail, new table is already in place */
936 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
944 free_newinfo_counters_untrans:
950 static int do_replace(struct net *net, sockptr_t arg, unsigned int len)
953 struct arpt_replace tmp;
954 struct xt_table_info *newinfo;
956 struct arpt_entry *iter;
958 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
962 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
964 if (tmp.num_counters == 0)
967 tmp.name[sizeof(tmp.name)-1] = 0;
969 newinfo = xt_alloc_table_info(tmp.size);
973 loc_cpu_entry = newinfo->entries;
974 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
980 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
984 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
985 tmp.num_counters, tmp.counters);
987 goto free_newinfo_untrans;
990 free_newinfo_untrans:
991 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
992 cleanup_entry(iter, net);
994 xt_free_table_info(newinfo);
998 static int do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
1001 struct xt_counters_info tmp;
1002 struct xt_counters *paddc;
1004 const struct xt_table_info *private;
1006 struct arpt_entry *iter;
1007 unsigned int addend;
1009 paddc = xt_copy_counters(arg, len, &tmp);
1011 return PTR_ERR(paddc);
1013 t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1020 private = xt_table_get_private_protected(t);
1021 if (private->number != tmp.num_counters) {
1023 goto unlock_up_free;
1028 addend = xt_write_recseq_begin();
1029 xt_entry_foreach(iter, private->entries, private->size) {
1030 struct xt_counters *tmp;
1032 tmp = xt_get_this_cpu_counter(&iter->counters);
1033 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1036 xt_write_recseq_end(addend);
1047 #ifdef CONFIG_COMPAT
1048 struct compat_arpt_replace {
1049 char name[XT_TABLE_MAXNAMELEN];
1053 u32 hook_entry[NF_ARP_NUMHOOKS];
1054 u32 underflow[NF_ARP_NUMHOOKS];
1056 compat_uptr_t counters;
1057 struct compat_arpt_entry entries[];
1060 static inline void compat_release_entry(struct compat_arpt_entry *e)
1062 struct xt_entry_target *t;
1064 t = compat_arpt_get_target(e);
1065 module_put(t->u.kernel.target->me);
1069 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1070 struct xt_table_info *newinfo,
1072 const unsigned char *base,
1073 const unsigned char *limit)
1075 struct xt_entry_target *t;
1076 struct xt_target *target;
1077 unsigned int entry_offset;
1080 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1081 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1082 (unsigned char *)e + e->next_offset > limit)
1085 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1086 sizeof(struct compat_xt_entry_target))
1089 if (!arp_checkentry(&e->arp))
1092 ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1097 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1098 entry_offset = (void *)e - (void *)base;
1100 t = compat_arpt_get_target(e);
1101 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1102 t->u.user.revision);
1103 if (IS_ERR(target)) {
1104 ret = PTR_ERR(target);
1107 t->u.kernel.target = target;
1109 off += xt_compat_target_offset(target);
1111 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1113 goto release_target;
1118 module_put(t->u.kernel.target->me);
1124 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1126 struct xt_table_info *newinfo, unsigned char *base)
1128 struct xt_entry_target *t;
1129 struct arpt_entry *de;
1130 unsigned int origsize;
1135 memcpy(de, e, sizeof(struct arpt_entry));
1136 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1138 *dstptr += sizeof(struct arpt_entry);
1139 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1141 de->target_offset = e->target_offset - (origsize - *size);
1142 t = compat_arpt_get_target(e);
1143 xt_compat_target_from_user(t, dstptr, size);
1145 de->next_offset = e->next_offset - (origsize - *size);
1146 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1147 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1148 newinfo->hook_entry[h] -= origsize - *size;
1149 if ((unsigned char *)de - base < newinfo->underflow[h])
1150 newinfo->underflow[h] -= origsize - *size;
1154 static int translate_compat_table(struct net *net,
1155 struct xt_table_info **pinfo,
1157 const struct compat_arpt_replace *compatr)
1160 struct xt_table_info *newinfo, *info;
1161 void *pos, *entry0, *entry1;
1162 struct compat_arpt_entry *iter0;
1163 struct arpt_replace repl;
1169 size = compatr->size;
1170 info->number = compatr->num_entries;
1173 xt_compat_lock(NFPROTO_ARP);
1174 ret = xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
1177 /* Walk through entries, checking offsets. */
1178 xt_entry_foreach(iter0, entry0, compatr->size) {
1179 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1181 entry0 + compatr->size);
1188 if (j != compatr->num_entries)
1192 newinfo = xt_alloc_table_info(size);
1196 newinfo->number = compatr->num_entries;
1197 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1198 newinfo->hook_entry[i] = compatr->hook_entry[i];
1199 newinfo->underflow[i] = compatr->underflow[i];
1201 entry1 = newinfo->entries;
1203 size = compatr->size;
1204 xt_entry_foreach(iter0, entry0, compatr->size)
1205 compat_copy_entry_from_user(iter0, &pos, &size,
1208 /* all module references in entry0 are now gone */
1210 xt_compat_flush_offsets(NFPROTO_ARP);
1211 xt_compat_unlock(NFPROTO_ARP);
1213 memcpy(&repl, compatr, sizeof(*compatr));
1215 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1216 repl.hook_entry[i] = newinfo->hook_entry[i];
1217 repl.underflow[i] = newinfo->underflow[i];
1220 repl.num_counters = 0;
1221 repl.counters = NULL;
1222 repl.size = newinfo->size;
1223 ret = translate_table(net, newinfo, entry1, &repl);
1229 xt_free_table_info(info);
1233 xt_free_table_info(newinfo);
1236 xt_compat_flush_offsets(NFPROTO_ARP);
1237 xt_compat_unlock(NFPROTO_ARP);
1238 xt_entry_foreach(iter0, entry0, compatr->size) {
1241 compat_release_entry(iter0);
1246 static int compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
1249 struct compat_arpt_replace tmp;
1250 struct xt_table_info *newinfo;
1251 void *loc_cpu_entry;
1252 struct arpt_entry *iter;
1254 if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1257 /* overflow check */
1258 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1260 if (tmp.num_counters == 0)
1263 tmp.name[sizeof(tmp.name)-1] = 0;
1265 newinfo = xt_alloc_table_info(tmp.size);
1269 loc_cpu_entry = newinfo->entries;
1270 if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
1276 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1280 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1281 tmp.num_counters, compat_ptr(tmp.counters));
1283 goto free_newinfo_untrans;
1286 free_newinfo_untrans:
1287 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1288 cleanup_entry(iter, net);
1290 xt_free_table_info(newinfo);
1294 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1295 compat_uint_t *size,
1296 struct xt_counters *counters,
1299 struct xt_entry_target *t;
1300 struct compat_arpt_entry __user *ce;
1301 u_int16_t target_offset, next_offset;
1302 compat_uint_t origsize;
1307 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1308 copy_to_user(&ce->counters, &counters[i],
1309 sizeof(counters[i])) != 0)
1312 *dstptr += sizeof(struct compat_arpt_entry);
1313 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1315 target_offset = e->target_offset - (origsize - *size);
1317 t = arpt_get_target(e);
1318 ret = xt_compat_target_to_user(t, dstptr, size);
1321 next_offset = e->next_offset - (origsize - *size);
1322 if (put_user(target_offset, &ce->target_offset) != 0 ||
1323 put_user(next_offset, &ce->next_offset) != 0)
1328 static int compat_copy_entries_to_user(unsigned int total_size,
1329 struct xt_table *table,
1330 void __user *userptr)
1332 struct xt_counters *counters;
1333 const struct xt_table_info *private = xt_table_get_private_protected(table);
1338 struct arpt_entry *iter;
1340 counters = alloc_counters(table);
1341 if (IS_ERR(counters))
1342 return PTR_ERR(counters);
1346 xt_entry_foreach(iter, private->entries, total_size) {
1347 ret = compat_copy_entry_to_user(iter, &pos,
1348 &size, counters, i++);
1356 struct compat_arpt_get_entries {
1357 char name[XT_TABLE_MAXNAMELEN];
1359 struct compat_arpt_entry entrytable[];
1362 static int compat_get_entries(struct net *net,
1363 struct compat_arpt_get_entries __user *uptr,
1367 struct compat_arpt_get_entries get;
1370 if (*len < sizeof(get))
1372 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1374 if (*len != sizeof(struct compat_arpt_get_entries) + get.size)
1377 get.name[sizeof(get.name) - 1] = '\0';
1379 xt_compat_lock(NFPROTO_ARP);
1380 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1382 const struct xt_table_info *private = xt_table_get_private_protected(t);
1383 struct xt_table_info info;
1385 ret = compat_table_info(private, &info);
1386 if (!ret && get.size == info.size) {
1387 ret = compat_copy_entries_to_user(private->size,
1388 t, uptr->entrytable);
1392 xt_compat_flush_offsets(NFPROTO_ARP);
1398 xt_compat_unlock(NFPROTO_ARP);
1403 static int do_arpt_set_ctl(struct sock *sk, int cmd, sockptr_t arg,
1408 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1412 case ARPT_SO_SET_REPLACE:
1413 #ifdef CONFIG_COMPAT
1414 if (in_compat_syscall())
1415 ret = compat_do_replace(sock_net(sk), arg, len);
1418 ret = do_replace(sock_net(sk), arg, len);
1421 case ARPT_SO_SET_ADD_COUNTERS:
1422 ret = do_add_counters(sock_net(sk), arg, len);
1432 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1436 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1440 case ARPT_SO_GET_INFO:
1441 ret = get_info(sock_net(sk), user, len);
1444 case ARPT_SO_GET_ENTRIES:
1445 #ifdef CONFIG_COMPAT
1446 if (in_compat_syscall())
1447 ret = compat_get_entries(sock_net(sk), user, len);
1450 ret = get_entries(sock_net(sk), user, len);
1453 case ARPT_SO_GET_REVISION_TARGET: {
1454 struct xt_get_revision rev;
1456 if (*len != sizeof(rev)) {
1460 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1464 rev.name[sizeof(rev.name)-1] = 0;
1466 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1467 rev.revision, 1, &ret),
1468 "arpt_%s", rev.name);
1479 static void __arpt_unregister_table(struct net *net, struct xt_table *table)
1481 struct xt_table_info *private;
1482 void *loc_cpu_entry;
1483 struct module *table_owner = table->me;
1484 struct arpt_entry *iter;
1486 private = xt_unregister_table(table);
1488 /* Decrease module usage counts and free resources */
1489 loc_cpu_entry = private->entries;
1490 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1491 cleanup_entry(iter, net);
1492 if (private->number > private->initial_entries)
1493 module_put(table_owner);
1494 xt_free_table_info(private);
1497 int arpt_register_table(struct net *net,
1498 const struct xt_table *table,
1499 const struct arpt_replace *repl,
1500 const struct nf_hook_ops *ops,
1501 struct xt_table **res)
1504 struct xt_table_info *newinfo;
1505 struct xt_table_info bootstrap = {0};
1506 void *loc_cpu_entry;
1507 struct xt_table *new_table;
1509 newinfo = xt_alloc_table_info(repl->size);
1513 loc_cpu_entry = newinfo->entries;
1514 memcpy(loc_cpu_entry, repl->entries, repl->size);
1516 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1520 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1521 if (IS_ERR(new_table)) {
1522 ret = PTR_ERR(new_table);
1526 /* set res now, will see skbs right after nf_register_net_hooks */
1527 WRITE_ONCE(*res, new_table);
1529 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1531 __arpt_unregister_table(net, new_table);
1538 xt_free_table_info(newinfo);
1542 void arpt_unregister_table(struct net *net, struct xt_table *table,
1543 const struct nf_hook_ops *ops)
1545 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1546 __arpt_unregister_table(net, table);
1549 /* The built-in targets: standard (NULL) and error. */
1550 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1552 .name = XT_STANDARD_TARGET,
1553 .targetsize = sizeof(int),
1554 .family = NFPROTO_ARP,
1555 #ifdef CONFIG_COMPAT
1556 .compatsize = sizeof(compat_int_t),
1557 .compat_from_user = compat_standard_from_user,
1558 .compat_to_user = compat_standard_to_user,
1562 .name = XT_ERROR_TARGET,
1563 .target = arpt_error,
1564 .targetsize = XT_FUNCTION_MAXNAMELEN,
1565 .family = NFPROTO_ARP,
1569 static struct nf_sockopt_ops arpt_sockopts = {
1571 .set_optmin = ARPT_BASE_CTL,
1572 .set_optmax = ARPT_SO_SET_MAX+1,
1573 .set = do_arpt_set_ctl,
1574 .get_optmin = ARPT_BASE_CTL,
1575 .get_optmax = ARPT_SO_GET_MAX+1,
1576 .get = do_arpt_get_ctl,
1577 .owner = THIS_MODULE,
1580 static int __net_init arp_tables_net_init(struct net *net)
1582 return xt_proto_init(net, NFPROTO_ARP);
1585 static void __net_exit arp_tables_net_exit(struct net *net)
1587 xt_proto_fini(net, NFPROTO_ARP);
1590 static struct pernet_operations arp_tables_net_ops = {
1591 .init = arp_tables_net_init,
1592 .exit = arp_tables_net_exit,
1595 static int __init arp_tables_init(void)
1599 ret = register_pernet_subsys(&arp_tables_net_ops);
1603 /* No one else will be downing sem now, so we won't sleep */
1604 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1608 /* Register setsockopt */
1609 ret = nf_register_sockopt(&arpt_sockopts);
1616 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1618 unregister_pernet_subsys(&arp_tables_net_ops);
1623 static void __exit arp_tables_fini(void)
1625 nf_unregister_sockopt(&arpt_sockopts);
1626 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1627 unregister_pernet_subsys(&arp_tables_net_ops);
1630 EXPORT_SYMBOL(arpt_register_table);
1631 EXPORT_SYMBOL(arpt_unregister_table);
1632 EXPORT_SYMBOL(arpt_do_table);
1634 module_init(arp_tables_init);
1635 module_exit(arp_tables_fini);