2 * Packet matching code for ARP packets.
4 * Based heavily, if not almost entirely, upon ip_tables.c framework.
6 * Some ARP specific bits are:
8 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
9 * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/capability.h>
17 #include <linux/if_arp.h>
18 #include <linux/kmod.h>
19 #include <linux/vmalloc.h>
20 #include <linux/proc_fs.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/mutex.h>
24 #include <linux/err.h>
25 #include <net/compat.h>
27 #include <asm/uaccess.h>
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_arp/arp_tables.h>
31 #include "../../netfilter/xt_repldata.h"
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
35 MODULE_DESCRIPTION("arptables core");
37 void *arpt_alloc_initial_table(const struct xt_table *info)
39 return xt_alloc_initial_table(arpt, ARPT);
41 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
43 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
44 const char *hdr_addr, int len)
48 if (len > ARPT_DEV_ADDR_LEN_MAX)
49 len = ARPT_DEV_ADDR_LEN_MAX;
52 for (i = 0; i < len; i++)
53 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
59 * Unfortunately, _b and _mask are not aligned to an int (or long int)
60 * Some arches dont care, unrolling the loop is a win on them.
61 * For other arches, we only have a 16bit alignement.
63 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
65 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
66 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
68 unsigned long ret = 0;
69 const u16 *a = (const u16 *)_a;
70 const u16 *b = (const u16 *)_b;
71 const u16 *mask = (const u16 *)_mask;
74 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
75 ret |= (a[i] ^ b[i]) & mask[i];
80 /* Returns whether packet matches rule or not. */
81 static inline int arp_packet_match(const struct arphdr *arphdr,
82 struct net_device *dev,
85 const struct arpt_arp *arpinfo)
87 const char *arpptr = (char *)(arphdr + 1);
88 const char *src_devaddr, *tgt_devaddr;
89 __be32 src_ipaddr, tgt_ipaddr;
92 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
94 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
98 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
102 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
106 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
110 src_devaddr = arpptr;
111 arpptr += dev->addr_len;
112 memcpy(&src_ipaddr, arpptr, sizeof(u32));
113 arpptr += sizeof(u32);
114 tgt_devaddr = arpptr;
115 arpptr += dev->addr_len;
116 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
118 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
119 ARPT_INV_SRCDEVADDR) ||
120 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
121 ARPT_INV_TGTDEVADDR))
124 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
126 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
130 /* Look for ifname matches. */
131 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
133 if (FWINV(ret != 0, ARPT_INV_VIA_IN))
136 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
138 if (FWINV(ret != 0, ARPT_INV_VIA_OUT))
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 = table->private;
207 cpu = smp_processor_id();
209 * Ensure we load private-> members after we've fetched the base
212 smp_read_barrier_depends();
213 table_base = private->entries;
214 jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
216 /* No TEE support for arptables, so no need to switch to alternate
217 * stack. All targets that reenter must return absolute verdicts.
219 e = get_entry(table_base, private->hook_entry[hook]);
221 acpar.net = state->net;
222 acpar.in = state->in;
223 acpar.out = state->out;
224 acpar.hooknum = hook;
225 acpar.family = NFPROTO_ARP;
226 acpar.hotdrop = false;
230 const struct xt_entry_target *t;
231 struct xt_counters *counter;
233 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
234 e = arpt_next_entry(e);
238 counter = xt_get_this_cpu_counter(&e->counters);
239 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
241 t = arpt_get_target_c(e);
243 /* Standard target? */
244 if (!t->u.kernel.target->target) {
247 v = ((struct xt_standard_target *)t)->verdict;
249 /* Pop from stack? */
250 if (v != XT_RETURN) {
251 verdict = (unsigned int)(-v) - 1;
255 e = get_entry(table_base,
256 private->underflow[hook]);
258 e = jumpstack[--stackidx];
259 e = arpt_next_entry(e);
264 != arpt_next_entry(e)) {
265 jumpstack[stackidx++] = e;
268 e = get_entry(table_base, v);
272 acpar.target = t->u.kernel.target;
273 acpar.targinfo = t->data;
274 verdict = t->u.kernel.target->target(skb, &acpar);
276 /* Target might have changed stuff. */
279 if (verdict == XT_CONTINUE)
280 e = arpt_next_entry(e);
284 } while (!acpar.hotdrop);
285 xt_write_recseq_end(addend);
294 /* All zeroes == unconditional rule. */
295 static inline bool unconditional(const struct arpt_entry *e)
297 static const struct arpt_arp uncond;
299 return e->target_offset == sizeof(struct arpt_entry) &&
300 memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
303 static bool find_jump_target(const struct xt_table_info *t,
304 const struct arpt_entry *target)
306 struct arpt_entry *iter;
308 xt_entry_foreach(iter, t->entries, t->size) {
315 /* Figures out from what hook each rule can be called: returns 0 if
316 * there are loops. Puts hook bitmask in comefrom.
318 static int mark_source_chains(const struct xt_table_info *newinfo,
319 unsigned int valid_hooks, void *entry0)
323 /* No recursion; use packet counter to save back ptrs (reset
324 * to 0 as we leave), and comefrom to save source hook bitmask.
326 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
327 unsigned int pos = newinfo->hook_entry[hook];
329 = (struct arpt_entry *)(entry0 + pos);
331 if (!(valid_hooks & (1 << hook)))
334 /* Set initial back pointer. */
335 e->counters.pcnt = pos;
338 const struct xt_standard_target *t
339 = (void *)arpt_get_target_c(e);
340 int visited = e->comefrom & (1 << hook);
342 if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
346 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
348 /* Unconditional return/END. */
349 if ((unconditional(e) &&
350 (strcmp(t->target.u.user.name,
351 XT_STANDARD_TARGET) == 0) &&
352 t->verdict < 0) || visited) {
353 unsigned int oldpos, size;
355 if ((strcmp(t->target.u.user.name,
356 XT_STANDARD_TARGET) == 0) &&
357 t->verdict < -NF_MAX_VERDICT - 1)
360 /* Return: backtrack through the last
364 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
366 pos = e->counters.pcnt;
367 e->counters.pcnt = 0;
369 /* We're at the start. */
373 e = (struct arpt_entry *)
375 } while (oldpos == pos + e->next_offset);
378 size = e->next_offset;
379 e = (struct arpt_entry *)
380 (entry0 + pos + size);
381 if (pos + size >= newinfo->size)
383 e->counters.pcnt = pos;
386 int newpos = t->verdict;
388 if (strcmp(t->target.u.user.name,
389 XT_STANDARD_TARGET) == 0 &&
391 /* This a jump; chase it. */
392 e = (struct arpt_entry *)
394 if (!find_jump_target(newinfo, e))
397 /* ... this is a fallthru */
398 newpos = pos + e->next_offset;
399 if (newpos >= newinfo->size)
402 e = (struct arpt_entry *)
404 e->counters.pcnt = pos;
413 static inline int check_target(struct arpt_entry *e, const char *name)
415 struct xt_entry_target *t = arpt_get_target(e);
416 struct xt_tgchk_param par = {
419 .target = t->u.kernel.target,
421 .hook_mask = e->comefrom,
422 .family = NFPROTO_ARP,
425 return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
429 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
431 struct xt_entry_target *t;
432 struct xt_target *target;
436 pcnt = xt_percpu_counter_alloc();
437 if (IS_ERR_VALUE(pcnt))
439 e->counters.pcnt = pcnt;
441 t = arpt_get_target(e);
442 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
444 if (IS_ERR(target)) {
445 ret = PTR_ERR(target);
448 t->u.kernel.target = target;
450 ret = check_target(e, name);
455 module_put(t->u.kernel.target->me);
457 xt_percpu_counter_free(e->counters.pcnt);
462 static bool check_underflow(const struct arpt_entry *e)
464 const struct xt_entry_target *t;
465 unsigned int verdict;
467 if (!unconditional(e))
469 t = arpt_get_target_c(e);
470 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
472 verdict = ((struct xt_standard_target *)t)->verdict;
473 verdict = -verdict - 1;
474 return verdict == NF_DROP || verdict == NF_ACCEPT;
477 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
478 struct xt_table_info *newinfo,
479 const unsigned char *base,
480 const unsigned char *limit,
481 const unsigned int *hook_entries,
482 const unsigned int *underflows,
483 unsigned int valid_hooks)
488 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
489 (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
490 (unsigned char *)e + e->next_offset > limit)
494 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target))
497 if (!arp_checkentry(&e->arp))
500 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
505 /* Check hooks & underflows */
506 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
507 if (!(valid_hooks & (1 << h)))
509 if ((unsigned char *)e - base == hook_entries[h])
510 newinfo->hook_entry[h] = hook_entries[h];
511 if ((unsigned char *)e - base == underflows[h]) {
512 if (!check_underflow(e))
515 newinfo->underflow[h] = underflows[h];
519 /* Clear counters and comefrom */
520 e->counters = ((struct xt_counters) { 0, 0 });
525 static inline void cleanup_entry(struct arpt_entry *e)
527 struct xt_tgdtor_param par;
528 struct xt_entry_target *t;
530 t = arpt_get_target(e);
531 par.target = t->u.kernel.target;
532 par.targinfo = t->data;
533 par.family = NFPROTO_ARP;
534 if (par.target->destroy != NULL)
535 par.target->destroy(&par);
536 module_put(par.target->me);
537 xt_percpu_counter_free(e->counters.pcnt);
540 /* Checks and translates the user-supplied table segment (held in
543 static int translate_table(struct xt_table_info *newinfo, void *entry0,
544 const struct arpt_replace *repl)
546 struct arpt_entry *iter;
550 newinfo->size = repl->size;
551 newinfo->number = repl->num_entries;
553 /* Init all hooks to impossible value. */
554 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
555 newinfo->hook_entry[i] = 0xFFFFFFFF;
556 newinfo->underflow[i] = 0xFFFFFFFF;
561 /* Walk through entries, checking offsets. */
562 xt_entry_foreach(iter, entry0, newinfo->size) {
563 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
571 if (strcmp(arpt_get_target(iter)->u.user.name,
572 XT_ERROR_TARGET) == 0)
573 ++newinfo->stacksize;
578 if (i != repl->num_entries)
581 /* Check hooks all assigned */
582 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
583 /* Only hooks which are valid */
584 if (!(repl->valid_hooks & (1 << i)))
586 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
588 if (newinfo->underflow[i] == 0xFFFFFFFF)
592 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
595 /* Finally, each sanity check must pass */
597 xt_entry_foreach(iter, entry0, newinfo->size) {
598 ret = find_check_entry(iter, repl->name, repl->size);
605 xt_entry_foreach(iter, entry0, newinfo->size) {
616 static void get_counters(const struct xt_table_info *t,
617 struct xt_counters counters[])
619 struct arpt_entry *iter;
623 for_each_possible_cpu(cpu) {
624 seqcount_t *s = &per_cpu(xt_recseq, cpu);
627 xt_entry_foreach(iter, t->entries, t->size) {
628 struct xt_counters *tmp;
632 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
634 start = read_seqcount_begin(s);
637 } while (read_seqcount_retry(s, start));
639 ADD_COUNTER(counters[i], bcnt, pcnt);
645 static struct xt_counters *alloc_counters(const struct xt_table *table)
647 unsigned int countersize;
648 struct xt_counters *counters;
649 const struct xt_table_info *private = table->private;
651 /* We need atomic snapshot of counters: rest doesn't change
652 * (other than comefrom, which userspace doesn't care
655 countersize = sizeof(struct xt_counters) * private->number;
656 counters = vzalloc(countersize);
658 if (counters == NULL)
659 return ERR_PTR(-ENOMEM);
661 get_counters(private, counters);
666 static int copy_entries_to_user(unsigned int total_size,
667 const struct xt_table *table,
668 void __user *userptr)
670 unsigned int off, num;
671 const struct arpt_entry *e;
672 struct xt_counters *counters;
673 struct xt_table_info *private = table->private;
677 counters = alloc_counters(table);
678 if (IS_ERR(counters))
679 return PTR_ERR(counters);
681 loc_cpu_entry = private->entries;
682 /* ... then copy entire thing ... */
683 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
688 /* FIXME: use iterator macros --RR */
689 /* ... then go back and fix counters and names */
690 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
691 const struct xt_entry_target *t;
693 e = (struct arpt_entry *)(loc_cpu_entry + off);
694 if (copy_to_user(userptr + off
695 + offsetof(struct arpt_entry, counters),
697 sizeof(counters[num])) != 0) {
702 t = arpt_get_target_c(e);
703 if (copy_to_user(userptr + off + e->target_offset
704 + offsetof(struct xt_entry_target,
706 t->u.kernel.target->name,
707 strlen(t->u.kernel.target->name)+1) != 0) {
719 static void compat_standard_from_user(void *dst, const void *src)
721 int v = *(compat_int_t *)src;
724 v += xt_compat_calc_jump(NFPROTO_ARP, v);
725 memcpy(dst, &v, sizeof(v));
728 static int compat_standard_to_user(void __user *dst, const void *src)
730 compat_int_t cv = *(int *)src;
733 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
734 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
737 static int compat_calc_entry(const struct arpt_entry *e,
738 const struct xt_table_info *info,
739 const void *base, struct xt_table_info *newinfo)
741 const struct xt_entry_target *t;
742 unsigned int entry_offset;
745 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
746 entry_offset = (void *)e - base;
748 t = arpt_get_target_c(e);
749 off += xt_compat_target_offset(t->u.kernel.target);
750 newinfo->size -= off;
751 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
755 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
756 if (info->hook_entry[i] &&
757 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
758 newinfo->hook_entry[i] -= off;
759 if (info->underflow[i] &&
760 (e < (struct arpt_entry *)(base + info->underflow[i])))
761 newinfo->underflow[i] -= off;
766 static int compat_table_info(const struct xt_table_info *info,
767 struct xt_table_info *newinfo)
769 struct arpt_entry *iter;
770 const void *loc_cpu_entry;
773 if (!newinfo || !info)
776 /* we dont care about newinfo->entries */
777 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
778 newinfo->initial_entries = 0;
779 loc_cpu_entry = info->entries;
780 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,
791 const int *len, int compat)
793 char name[XT_TABLE_MAXNAMELEN];
797 if (*len != sizeof(struct arpt_getinfo))
800 if (copy_from_user(name, user, sizeof(name)) != 0)
803 name[XT_TABLE_MAXNAMELEN-1] = '\0';
806 xt_compat_lock(NFPROTO_ARP);
808 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
809 "arptable_%s", name);
810 if (!IS_ERR_OR_NULL(t)) {
811 struct arpt_getinfo info;
812 const struct xt_table_info *private = t->private;
814 struct xt_table_info tmp;
817 ret = compat_table_info(private, &tmp);
818 xt_compat_flush_offsets(NFPROTO_ARP);
822 memset(&info, 0, sizeof(info));
823 info.valid_hooks = t->valid_hooks;
824 memcpy(info.hook_entry, private->hook_entry,
825 sizeof(info.hook_entry));
826 memcpy(info.underflow, private->underflow,
827 sizeof(info.underflow));
828 info.num_entries = private->number;
829 info.size = private->size;
830 strcpy(info.name, name);
832 if (copy_to_user(user, &info, *len) != 0)
839 ret = t ? PTR_ERR(t) : -ENOENT;
842 xt_compat_unlock(NFPROTO_ARP);
847 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
851 struct arpt_get_entries get;
854 if (*len < sizeof(get))
856 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
858 if (*len != sizeof(struct arpt_get_entries) + get.size)
861 get.name[sizeof(get.name) - 1] = '\0';
863 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
864 if (!IS_ERR_OR_NULL(t)) {
865 const struct xt_table_info *private = t->private;
867 if (get.size == private->size)
868 ret = copy_entries_to_user(private->size,
869 t, uptr->entrytable);
876 ret = t ? PTR_ERR(t) : -ENOENT;
881 static int __do_replace(struct net *net, const char *name,
882 unsigned int valid_hooks,
883 struct xt_table_info *newinfo,
884 unsigned int num_counters,
885 void __user *counters_ptr)
889 struct xt_table_info *oldinfo;
890 struct xt_counters *counters;
891 void *loc_cpu_old_entry;
892 struct arpt_entry *iter;
895 counters = vzalloc(num_counters * sizeof(struct xt_counters));
901 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
902 "arptable_%s", name);
903 if (IS_ERR_OR_NULL(t)) {
904 ret = t ? PTR_ERR(t) : -ENOENT;
905 goto free_newinfo_counters_untrans;
909 if (valid_hooks != t->valid_hooks) {
914 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
918 /* Update module usage count based on number of rules */
919 if ((oldinfo->number > oldinfo->initial_entries) ||
920 (newinfo->number <= oldinfo->initial_entries))
922 if ((oldinfo->number > oldinfo->initial_entries) &&
923 (newinfo->number <= oldinfo->initial_entries))
926 /* Get the old counters, and synchronize with replace */
927 get_counters(oldinfo, counters);
929 /* Decrease module usage counts and free resource */
930 loc_cpu_old_entry = oldinfo->entries;
931 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
934 xt_free_table_info(oldinfo);
935 if (copy_to_user(counters_ptr, counters,
936 sizeof(struct xt_counters) * num_counters) != 0) {
937 /* Silent error, can't fail, new table is already in place */
938 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
947 free_newinfo_counters_untrans:
953 static int do_replace(struct net *net, const void __user *user,
957 struct arpt_replace tmp;
958 struct xt_table_info *newinfo;
960 struct arpt_entry *iter;
962 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
966 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
968 if (tmp.num_counters == 0)
971 tmp.name[sizeof(tmp.name)-1] = 0;
973 newinfo = xt_alloc_table_info(tmp.size);
977 loc_cpu_entry = newinfo->entries;
978 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
984 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
988 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
989 tmp.num_counters, tmp.counters);
991 goto free_newinfo_untrans;
994 free_newinfo_untrans:
995 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
998 xt_free_table_info(newinfo);
1002 static int do_add_counters(struct net *net, const void __user *user,
1003 unsigned int len, int compat)
1006 struct xt_counters_info tmp;
1007 struct xt_counters *paddc;
1009 const struct xt_table_info *private;
1011 struct arpt_entry *iter;
1012 unsigned int addend;
1014 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1016 return PTR_ERR(paddc);
1018 t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1019 if (IS_ERR_OR_NULL(t)) {
1020 ret = t ? PTR_ERR(t) : -ENOENT;
1025 private = t->private;
1026 if (private->number != tmp.num_counters) {
1028 goto unlock_up_free;
1033 addend = xt_write_recseq_begin();
1034 xt_entry_foreach(iter, private->entries, private->size) {
1035 struct xt_counters *tmp;
1037 tmp = xt_get_this_cpu_counter(&iter->counters);
1038 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1041 xt_write_recseq_end(addend);
1052 #ifdef CONFIG_COMPAT
1053 struct compat_arpt_replace {
1054 char name[XT_TABLE_MAXNAMELEN];
1058 u32 hook_entry[NF_ARP_NUMHOOKS];
1059 u32 underflow[NF_ARP_NUMHOOKS];
1061 compat_uptr_t counters;
1062 struct compat_arpt_entry entries[0];
1065 static inline void compat_release_entry(struct compat_arpt_entry *e)
1067 struct xt_entry_target *t;
1069 t = compat_arpt_get_target(e);
1070 module_put(t->u.kernel.target->me);
1074 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1075 struct xt_table_info *newinfo,
1077 const unsigned char *base,
1078 const unsigned char *limit)
1080 struct xt_entry_target *t;
1081 struct xt_target *target;
1082 unsigned int entry_offset;
1085 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1086 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1087 (unsigned char *)e + e->next_offset > limit)
1090 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1091 sizeof(struct compat_xt_entry_target))
1094 if (!arp_checkentry(&e->arp))
1097 ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1102 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1103 entry_offset = (void *)e - (void *)base;
1105 t = compat_arpt_get_target(e);
1106 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1107 t->u.user.revision);
1108 if (IS_ERR(target)) {
1109 ret = PTR_ERR(target);
1112 t->u.kernel.target = target;
1114 off += xt_compat_target_offset(target);
1116 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1118 goto release_target;
1123 module_put(t->u.kernel.target->me);
1129 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1131 struct xt_table_info *newinfo, unsigned char *base)
1133 struct xt_entry_target *t;
1134 struct xt_target *target;
1135 struct arpt_entry *de;
1136 unsigned int origsize;
1140 de = (struct arpt_entry *)*dstptr;
1141 memcpy(de, e, sizeof(struct arpt_entry));
1142 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1144 *dstptr += sizeof(struct arpt_entry);
1145 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1147 de->target_offset = e->target_offset - (origsize - *size);
1148 t = compat_arpt_get_target(e);
1149 target = t->u.kernel.target;
1150 xt_compat_target_from_user(t, dstptr, size);
1152 de->next_offset = e->next_offset - (origsize - *size);
1153 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1154 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1155 newinfo->hook_entry[h] -= origsize - *size;
1156 if ((unsigned char *)de - base < newinfo->underflow[h])
1157 newinfo->underflow[h] -= origsize - *size;
1161 static int translate_compat_table(struct xt_table_info **pinfo,
1163 const struct compat_arpt_replace *compatr)
1166 struct xt_table_info *newinfo, *info;
1167 void *pos, *entry0, *entry1;
1168 struct compat_arpt_entry *iter0;
1169 struct arpt_replace repl;
1175 size = compatr->size;
1176 info->number = compatr->num_entries;
1179 xt_compat_lock(NFPROTO_ARP);
1180 xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
1181 /* Walk through entries, checking offsets. */
1182 xt_entry_foreach(iter0, entry0, compatr->size) {
1183 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1185 entry0 + compatr->size);
1192 if (j != compatr->num_entries)
1196 newinfo = xt_alloc_table_info(size);
1200 newinfo->number = compatr->num_entries;
1201 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1202 newinfo->hook_entry[i] = info->hook_entry[i];
1203 newinfo->underflow[i] = info->underflow[i];
1205 entry1 = newinfo->entries;
1207 size = compatr->size;
1208 xt_entry_foreach(iter0, entry0, compatr->size)
1209 compat_copy_entry_from_user(iter0, &pos, &size,
1212 /* all module references in entry0 are now gone */
1214 xt_compat_flush_offsets(NFPROTO_ARP);
1215 xt_compat_unlock(NFPROTO_ARP);
1217 memcpy(&repl, compatr, sizeof(*compatr));
1219 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1220 repl.hook_entry[i] = newinfo->hook_entry[i];
1221 repl.underflow[i] = newinfo->underflow[i];
1224 repl.num_counters = 0;
1225 repl.counters = NULL;
1226 repl.size = newinfo->size;
1227 ret = translate_table(newinfo, entry1, &repl);
1233 xt_free_table_info(info);
1237 xt_free_table_info(newinfo);
1240 xt_compat_flush_offsets(NFPROTO_ARP);
1241 xt_compat_unlock(NFPROTO_ARP);
1242 xt_entry_foreach(iter0, entry0, compatr->size) {
1245 compat_release_entry(iter0);
1250 static int compat_do_replace(struct net *net, void __user *user,
1254 struct compat_arpt_replace tmp;
1255 struct xt_table_info *newinfo;
1256 void *loc_cpu_entry;
1257 struct arpt_entry *iter;
1259 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1262 /* overflow check */
1263 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1265 if (tmp.num_counters == 0)
1268 tmp.name[sizeof(tmp.name)-1] = 0;
1270 newinfo = xt_alloc_table_info(tmp.size);
1274 loc_cpu_entry = newinfo->entries;
1275 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1280 ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp);
1284 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1285 tmp.num_counters, compat_ptr(tmp.counters));
1287 goto free_newinfo_untrans;
1290 free_newinfo_untrans:
1291 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1292 cleanup_entry(iter);
1294 xt_free_table_info(newinfo);
1298 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1303 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1307 case ARPT_SO_SET_REPLACE:
1308 ret = compat_do_replace(sock_net(sk), user, len);
1311 case ARPT_SO_SET_ADD_COUNTERS:
1312 ret = do_add_counters(sock_net(sk), user, len, 1);
1322 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1323 compat_uint_t *size,
1324 struct xt_counters *counters,
1327 struct xt_entry_target *t;
1328 struct compat_arpt_entry __user *ce;
1329 u_int16_t target_offset, next_offset;
1330 compat_uint_t origsize;
1334 ce = (struct compat_arpt_entry __user *)*dstptr;
1335 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1336 copy_to_user(&ce->counters, &counters[i],
1337 sizeof(counters[i])) != 0)
1340 *dstptr += sizeof(struct compat_arpt_entry);
1341 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1343 target_offset = e->target_offset - (origsize - *size);
1345 t = arpt_get_target(e);
1346 ret = xt_compat_target_to_user(t, dstptr, size);
1349 next_offset = e->next_offset - (origsize - *size);
1350 if (put_user(target_offset, &ce->target_offset) != 0 ||
1351 put_user(next_offset, &ce->next_offset) != 0)
1356 static int compat_copy_entries_to_user(unsigned int total_size,
1357 struct xt_table *table,
1358 void __user *userptr)
1360 struct xt_counters *counters;
1361 const struct xt_table_info *private = table->private;
1366 struct arpt_entry *iter;
1368 counters = alloc_counters(table);
1369 if (IS_ERR(counters))
1370 return PTR_ERR(counters);
1374 xt_entry_foreach(iter, private->entries, total_size) {
1375 ret = compat_copy_entry_to_user(iter, &pos,
1376 &size, counters, i++);
1384 struct compat_arpt_get_entries {
1385 char name[XT_TABLE_MAXNAMELEN];
1387 struct compat_arpt_entry entrytable[0];
1390 static int compat_get_entries(struct net *net,
1391 struct compat_arpt_get_entries __user *uptr,
1395 struct compat_arpt_get_entries get;
1398 if (*len < sizeof(get))
1400 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1402 if (*len != sizeof(struct compat_arpt_get_entries) + get.size)
1405 get.name[sizeof(get.name) - 1] = '\0';
1407 xt_compat_lock(NFPROTO_ARP);
1408 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1409 if (!IS_ERR_OR_NULL(t)) {
1410 const struct xt_table_info *private = t->private;
1411 struct xt_table_info info;
1413 ret = compat_table_info(private, &info);
1414 if (!ret && get.size == info.size) {
1415 ret = compat_copy_entries_to_user(private->size,
1416 t, uptr->entrytable);
1420 xt_compat_flush_offsets(NFPROTO_ARP);
1424 ret = t ? PTR_ERR(t) : -ENOENT;
1426 xt_compat_unlock(NFPROTO_ARP);
1430 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1432 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1437 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1441 case ARPT_SO_GET_INFO:
1442 ret = get_info(sock_net(sk), user, len, 1);
1444 case ARPT_SO_GET_ENTRIES:
1445 ret = compat_get_entries(sock_net(sk), user, len);
1448 ret = do_arpt_get_ctl(sk, cmd, user, len);
1454 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1458 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1462 case ARPT_SO_SET_REPLACE:
1463 ret = do_replace(sock_net(sk), user, len);
1466 case ARPT_SO_SET_ADD_COUNTERS:
1467 ret = do_add_counters(sock_net(sk), user, len, 0);
1477 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1481 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1485 case ARPT_SO_GET_INFO:
1486 ret = get_info(sock_net(sk), user, len, 0);
1489 case ARPT_SO_GET_ENTRIES:
1490 ret = get_entries(sock_net(sk), user, len);
1493 case ARPT_SO_GET_REVISION_TARGET: {
1494 struct xt_get_revision rev;
1496 if (*len != sizeof(rev)) {
1500 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1504 rev.name[sizeof(rev.name)-1] = 0;
1506 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1507 rev.revision, 1, &ret),
1508 "arpt_%s", rev.name);
1519 static void __arpt_unregister_table(struct xt_table *table)
1521 struct xt_table_info *private;
1522 void *loc_cpu_entry;
1523 struct module *table_owner = table->me;
1524 struct arpt_entry *iter;
1526 private = xt_unregister_table(table);
1528 /* Decrease module usage counts and free resources */
1529 loc_cpu_entry = private->entries;
1530 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1531 cleanup_entry(iter);
1532 if (private->number > private->initial_entries)
1533 module_put(table_owner);
1534 xt_free_table_info(private);
1537 int arpt_register_table(struct net *net,
1538 const struct xt_table *table,
1539 const struct arpt_replace *repl,
1540 const struct nf_hook_ops *ops,
1541 struct xt_table **res)
1544 struct xt_table_info *newinfo;
1545 struct xt_table_info bootstrap = {0};
1546 void *loc_cpu_entry;
1547 struct xt_table *new_table;
1549 newinfo = xt_alloc_table_info(repl->size);
1553 loc_cpu_entry = newinfo->entries;
1554 memcpy(loc_cpu_entry, repl->entries, repl->size);
1556 ret = translate_table(newinfo, loc_cpu_entry, repl);
1560 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1561 if (IS_ERR(new_table)) {
1562 ret = PTR_ERR(new_table);
1566 /* set res now, will see skbs right after nf_register_net_hooks */
1567 WRITE_ONCE(*res, new_table);
1569 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1571 __arpt_unregister_table(new_table);
1578 xt_free_table_info(newinfo);
1582 void arpt_unregister_table(struct net *net, struct xt_table *table,
1583 const struct nf_hook_ops *ops)
1585 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1586 __arpt_unregister_table(table);
1589 /* The built-in targets: standard (NULL) and error. */
1590 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1592 .name = XT_STANDARD_TARGET,
1593 .targetsize = sizeof(int),
1594 .family = NFPROTO_ARP,
1595 #ifdef CONFIG_COMPAT
1596 .compatsize = sizeof(compat_int_t),
1597 .compat_from_user = compat_standard_from_user,
1598 .compat_to_user = compat_standard_to_user,
1602 .name = XT_ERROR_TARGET,
1603 .target = arpt_error,
1604 .targetsize = XT_FUNCTION_MAXNAMELEN,
1605 .family = NFPROTO_ARP,
1609 static struct nf_sockopt_ops arpt_sockopts = {
1611 .set_optmin = ARPT_BASE_CTL,
1612 .set_optmax = ARPT_SO_SET_MAX+1,
1613 .set = do_arpt_set_ctl,
1614 #ifdef CONFIG_COMPAT
1615 .compat_set = compat_do_arpt_set_ctl,
1617 .get_optmin = ARPT_BASE_CTL,
1618 .get_optmax = ARPT_SO_GET_MAX+1,
1619 .get = do_arpt_get_ctl,
1620 #ifdef CONFIG_COMPAT
1621 .compat_get = compat_do_arpt_get_ctl,
1623 .owner = THIS_MODULE,
1626 static int __net_init arp_tables_net_init(struct net *net)
1628 return xt_proto_init(net, NFPROTO_ARP);
1631 static void __net_exit arp_tables_net_exit(struct net *net)
1633 xt_proto_fini(net, NFPROTO_ARP);
1636 static struct pernet_operations arp_tables_net_ops = {
1637 .init = arp_tables_net_init,
1638 .exit = arp_tables_net_exit,
1641 static int __init arp_tables_init(void)
1645 ret = register_pernet_subsys(&arp_tables_net_ops);
1649 /* No one else will be downing sem now, so we won't sleep */
1650 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1654 /* Register setsockopt */
1655 ret = nf_register_sockopt(&arpt_sockopts);
1659 pr_info("arp_tables: (C) 2002 David S. Miller\n");
1663 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1665 unregister_pernet_subsys(&arp_tables_net_ops);
1670 static void __exit arp_tables_fini(void)
1672 nf_unregister_sockopt(&arpt_sockopts);
1673 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1674 unregister_pernet_subsys(&arp_tables_net_ops);
1677 EXPORT_SYMBOL(arpt_register_table);
1678 EXPORT_SYMBOL(arpt_unregister_table);
1679 EXPORT_SYMBOL(arpt_do_table);
1681 module_init(arp_tables_init);
1682 module_exit(arp_tables_fini);