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 /*#define DEBUG_ARP_TABLES*/
38 /*#define DEBUG_ARP_TABLES_USER*/
40 #ifdef DEBUG_ARP_TABLES
41 #define dprintf(format, args...) pr_debug(format, ## args)
43 #define dprintf(format, args...)
46 #ifdef DEBUG_ARP_TABLES_USER
47 #define duprintf(format, args...) pr_debug(format, ## args)
49 #define duprintf(format, args...)
52 #ifdef CONFIG_NETFILTER_DEBUG
53 #define ARP_NF_ASSERT(x) WARN_ON(!(x))
55 #define ARP_NF_ASSERT(x)
58 void *arpt_alloc_initial_table(const struct xt_table *info)
60 return xt_alloc_initial_table(arpt, ARPT);
62 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
64 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
65 const char *hdr_addr, int len)
69 if (len > ARPT_DEV_ADDR_LEN_MAX)
70 len = ARPT_DEV_ADDR_LEN_MAX;
73 for (i = 0; i < len; i++)
74 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
80 * Unfortunately, _b and _mask are not aligned to an int (or long int)
81 * Some arches dont care, unrolling the loop is a win on them.
82 * For other arches, we only have a 16bit alignement.
84 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
86 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
87 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
89 unsigned long ret = 0;
90 const u16 *a = (const u16 *)_a;
91 const u16 *b = (const u16 *)_b;
92 const u16 *mask = (const u16 *)_mask;
95 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
96 ret |= (a[i] ^ b[i]) & mask[i];
101 /* Returns whether packet matches rule or not. */
102 static inline int arp_packet_match(const struct arphdr *arphdr,
103 struct net_device *dev,
106 const struct arpt_arp *arpinfo)
108 const char *arpptr = (char *)(arphdr + 1);
109 const char *src_devaddr, *tgt_devaddr;
110 __be32 src_ipaddr, tgt_ipaddr;
113 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
115 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
117 dprintf("ARP operation field mismatch.\n");
118 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
119 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
123 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
125 dprintf("ARP hardware address format mismatch.\n");
126 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
127 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
131 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
133 dprintf("ARP protocol address format mismatch.\n");
134 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
135 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
139 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
141 dprintf("ARP hardware address length mismatch.\n");
142 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
143 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
147 src_devaddr = arpptr;
148 arpptr += dev->addr_len;
149 memcpy(&src_ipaddr, arpptr, sizeof(u32));
150 arpptr += sizeof(u32);
151 tgt_devaddr = arpptr;
152 arpptr += dev->addr_len;
153 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
155 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
156 ARPT_INV_SRCDEVADDR) ||
157 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
158 ARPT_INV_TGTDEVADDR)) {
159 dprintf("Source or target device address mismatch.\n");
164 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
166 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
168 dprintf("Source or target IP address mismatch.\n");
170 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
172 &arpinfo->smsk.s_addr,
173 &arpinfo->src.s_addr,
174 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
175 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
177 &arpinfo->tmsk.s_addr,
178 &arpinfo->tgt.s_addr,
179 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
183 /* Look for ifname matches. */
184 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
186 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
187 dprintf("VIA in mismatch (%s vs %s).%s\n",
188 indev, arpinfo->iniface,
189 arpinfo->invflags & ARPT_INV_VIA_IN ? " (INV)" : "");
193 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
195 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
196 dprintf("VIA out mismatch (%s vs %s).%s\n",
197 outdev, arpinfo->outiface,
198 arpinfo->invflags & ARPT_INV_VIA_OUT ? " (INV)" : "");
206 static inline int arp_checkentry(const struct arpt_arp *arp)
208 if (arp->flags & ~ARPT_F_MASK) {
209 duprintf("Unknown flag bits set: %08X\n",
210 arp->flags & ~ARPT_F_MASK);
213 if (arp->invflags & ~ARPT_INV_MASK) {
214 duprintf("Unknown invflag bits set: %08X\n",
215 arp->invflags & ~ARPT_INV_MASK);
223 arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
225 net_err_ratelimited("arp_tables: error: '%s'\n",
226 (const char *)par->targinfo);
231 static inline const struct xt_entry_target *
232 arpt_get_target_c(const struct arpt_entry *e)
234 return arpt_get_target((struct arpt_entry *)e);
237 static inline struct arpt_entry *
238 get_entry(const void *base, unsigned int offset)
240 return (struct arpt_entry *)(base + offset);
244 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
246 return (void *)entry + entry->next_offset;
249 unsigned int arpt_do_table(struct sk_buff *skb,
250 const struct nf_hook_state *state,
251 struct xt_table *table)
253 unsigned int hook = state->hook;
254 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
255 unsigned int verdict = NF_DROP;
256 const struct arphdr *arp;
257 struct arpt_entry *e, **jumpstack;
258 const char *indev, *outdev;
259 const void *table_base;
260 unsigned int cpu, stackidx = 0;
261 const struct xt_table_info *private;
262 struct xt_action_param acpar;
265 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
268 indev = state->in ? state->in->name : nulldevname;
269 outdev = state->out ? state->out->name : nulldevname;
272 addend = xt_write_recseq_begin();
273 private = table->private;
274 cpu = smp_processor_id();
276 * Ensure we load private-> members after we've fetched the base
279 smp_read_barrier_depends();
280 table_base = private->entries;
281 jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
283 /* No TEE support for arptables, so no need to switch to alternate
284 * stack. All targets that reenter must return absolute verdicts.
286 e = get_entry(table_base, private->hook_entry[hook]);
288 acpar.net = state->net;
289 acpar.in = state->in;
290 acpar.out = state->out;
291 acpar.hooknum = hook;
292 acpar.family = NFPROTO_ARP;
293 acpar.hotdrop = false;
297 const struct xt_entry_target *t;
298 struct xt_counters *counter;
300 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
301 e = arpt_next_entry(e);
305 counter = xt_get_this_cpu_counter(&e->counters);
306 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
308 t = arpt_get_target_c(e);
310 /* Standard target? */
311 if (!t->u.kernel.target->target) {
314 v = ((struct xt_standard_target *)t)->verdict;
316 /* Pop from stack? */
317 if (v != XT_RETURN) {
318 verdict = (unsigned int)(-v) - 1;
322 e = get_entry(table_base,
323 private->underflow[hook]);
325 e = jumpstack[--stackidx];
326 e = arpt_next_entry(e);
331 != arpt_next_entry(e)) {
332 jumpstack[stackidx++] = e;
335 e = get_entry(table_base, v);
339 acpar.target = t->u.kernel.target;
340 acpar.targinfo = t->data;
341 verdict = t->u.kernel.target->target(skb, &acpar);
343 /* Target might have changed stuff. */
346 if (verdict == XT_CONTINUE)
347 e = arpt_next_entry(e);
351 } while (!acpar.hotdrop);
352 xt_write_recseq_end(addend);
361 /* All zeroes == unconditional rule. */
362 static inline bool unconditional(const struct arpt_arp *arp)
364 static const struct arpt_arp uncond;
366 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
369 /* Figures out from what hook each rule can be called: returns 0 if
370 * there are loops. Puts hook bitmask in comefrom.
372 static int mark_source_chains(const struct xt_table_info *newinfo,
373 unsigned int valid_hooks, void *entry0)
377 /* No recursion; use packet counter to save back ptrs (reset
378 * to 0 as we leave), and comefrom to save source hook bitmask.
380 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
381 unsigned int pos = newinfo->hook_entry[hook];
383 = (struct arpt_entry *)(entry0 + pos);
385 if (!(valid_hooks & (1 << hook)))
388 /* Set initial back pointer. */
389 e->counters.pcnt = pos;
392 const struct xt_standard_target *t
393 = (void *)arpt_get_target_c(e);
394 int visited = e->comefrom & (1 << hook);
396 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
397 pr_notice("arptables: loop hook %u pos %u %08X.\n",
398 hook, pos, e->comefrom);
402 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
404 /* Unconditional return/END. */
405 if ((e->target_offset == sizeof(struct arpt_entry) &&
406 (strcmp(t->target.u.user.name,
407 XT_STANDARD_TARGET) == 0) &&
408 t->verdict < 0 && unconditional(&e->arp)) ||
410 unsigned int oldpos, size;
412 if ((strcmp(t->target.u.user.name,
413 XT_STANDARD_TARGET) == 0) &&
414 t->verdict < -NF_MAX_VERDICT - 1) {
415 duprintf("mark_source_chains: bad "
416 "negative verdict (%i)\n",
421 /* Return: backtrack through the last
425 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
427 pos = e->counters.pcnt;
428 e->counters.pcnt = 0;
430 /* We're at the start. */
434 e = (struct arpt_entry *)
436 } while (oldpos == pos + e->next_offset);
439 size = e->next_offset;
440 e = (struct arpt_entry *)
441 (entry0 + pos + size);
442 e->counters.pcnt = pos;
445 int newpos = t->verdict;
447 if (strcmp(t->target.u.user.name,
448 XT_STANDARD_TARGET) == 0 &&
450 if (newpos > newinfo->size -
451 sizeof(struct arpt_entry)) {
452 duprintf("mark_source_chains: "
453 "bad verdict (%i)\n",
458 /* This a jump; chase it. */
459 duprintf("Jump rule %u -> %u\n",
462 /* ... this is a fallthru */
463 newpos = pos + e->next_offset;
465 e = (struct arpt_entry *)
467 e->counters.pcnt = pos;
472 duprintf("Finished chain %u\n", hook);
477 static inline int check_entry(const struct arpt_entry *e, const char *name)
479 const struct xt_entry_target *t;
481 if (!arp_checkentry(&e->arp)) {
482 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
486 if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
489 t = arpt_get_target_c(e);
490 if (e->target_offset + t->u.target_size > e->next_offset)
496 static inline int check_target(struct arpt_entry *e, const char *name)
498 struct xt_entry_target *t = arpt_get_target(e);
500 struct xt_tgchk_param par = {
503 .target = t->u.kernel.target,
505 .hook_mask = e->comefrom,
506 .family = NFPROTO_ARP,
509 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
511 duprintf("arp_tables: check failed for `%s'.\n",
512 t->u.kernel.target->name);
519 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
521 struct xt_entry_target *t;
522 struct xt_target *target;
525 ret = check_entry(e, name);
529 e->counters.pcnt = xt_percpu_counter_alloc();
530 if (IS_ERR_VALUE(e->counters.pcnt))
533 t = arpt_get_target(e);
534 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
536 if (IS_ERR(target)) {
537 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
538 ret = PTR_ERR(target);
541 t->u.kernel.target = target;
543 ret = check_target(e, name);
548 module_put(t->u.kernel.target->me);
550 xt_percpu_counter_free(e->counters.pcnt);
555 static bool check_underflow(const struct arpt_entry *e)
557 const struct xt_entry_target *t;
558 unsigned int verdict;
560 if (!unconditional(&e->arp))
562 t = arpt_get_target_c(e);
563 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
565 verdict = ((struct xt_standard_target *)t)->verdict;
566 verdict = -verdict - 1;
567 return verdict == NF_DROP || verdict == NF_ACCEPT;
570 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
571 struct xt_table_info *newinfo,
572 const unsigned char *base,
573 const unsigned char *limit,
574 const unsigned int *hook_entries,
575 const unsigned int *underflows,
576 unsigned int valid_hooks)
580 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
581 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
582 duprintf("Bad offset %p\n", e);
587 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) {
588 duprintf("checking: element %p size %u\n",
593 /* Check hooks & underflows */
594 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
595 if (!(valid_hooks & (1 << h)))
597 if ((unsigned char *)e - base == hook_entries[h])
598 newinfo->hook_entry[h] = hook_entries[h];
599 if ((unsigned char *)e - base == underflows[h]) {
600 if (!check_underflow(e)) {
601 pr_err("Underflows must be unconditional and "
602 "use the STANDARD target with "
606 newinfo->underflow[h] = underflows[h];
610 /* Clear counters and comefrom */
611 e->counters = ((struct xt_counters) { 0, 0 });
616 static inline void cleanup_entry(struct arpt_entry *e)
618 struct xt_tgdtor_param par;
619 struct xt_entry_target *t;
621 t = arpt_get_target(e);
622 par.target = t->u.kernel.target;
623 par.targinfo = t->data;
624 par.family = NFPROTO_ARP;
625 if (par.target->destroy != NULL)
626 par.target->destroy(&par);
627 module_put(par.target->me);
628 xt_percpu_counter_free(e->counters.pcnt);
631 /* Checks and translates the user-supplied table segment (held in
634 static int translate_table(struct xt_table_info *newinfo, void *entry0,
635 const struct arpt_replace *repl)
637 struct arpt_entry *iter;
641 newinfo->size = repl->size;
642 newinfo->number = repl->num_entries;
644 /* Init all hooks to impossible value. */
645 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
646 newinfo->hook_entry[i] = 0xFFFFFFFF;
647 newinfo->underflow[i] = 0xFFFFFFFF;
650 duprintf("translate_table: size %u\n", newinfo->size);
653 /* Walk through entries, checking offsets. */
654 xt_entry_foreach(iter, entry0, newinfo->size) {
655 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
663 if (strcmp(arpt_get_target(iter)->u.user.name,
664 XT_ERROR_TARGET) == 0)
665 ++newinfo->stacksize;
667 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
671 if (i != repl->num_entries) {
672 duprintf("translate_table: %u not %u entries\n",
673 i, repl->num_entries);
677 /* Check hooks all assigned */
678 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
679 /* Only hooks which are valid */
680 if (!(repl->valid_hooks & (1 << i)))
682 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
683 duprintf("Invalid hook entry %u %u\n",
684 i, repl->hook_entry[i]);
687 if (newinfo->underflow[i] == 0xFFFFFFFF) {
688 duprintf("Invalid underflow %u %u\n",
689 i, repl->underflow[i]);
694 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
695 duprintf("Looping hook\n");
699 /* Finally, each sanity check must pass */
701 xt_entry_foreach(iter, entry0, newinfo->size) {
702 ret = find_check_entry(iter, repl->name, repl->size);
709 xt_entry_foreach(iter, entry0, newinfo->size) {
720 static void get_counters(const struct xt_table_info *t,
721 struct xt_counters counters[])
723 struct arpt_entry *iter;
727 for_each_possible_cpu(cpu) {
728 seqcount_t *s = &per_cpu(xt_recseq, cpu);
731 xt_entry_foreach(iter, t->entries, t->size) {
732 struct xt_counters *tmp;
736 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
738 start = read_seqcount_begin(s);
741 } while (read_seqcount_retry(s, start));
743 ADD_COUNTER(counters[i], bcnt, pcnt);
749 static struct xt_counters *alloc_counters(const struct xt_table *table)
751 unsigned int countersize;
752 struct xt_counters *counters;
753 const struct xt_table_info *private = table->private;
755 /* We need atomic snapshot of counters: rest doesn't change
756 * (other than comefrom, which userspace doesn't care
759 countersize = sizeof(struct xt_counters) * private->number;
760 counters = vzalloc(countersize);
762 if (counters == NULL)
763 return ERR_PTR(-ENOMEM);
765 get_counters(private, counters);
770 static int copy_entries_to_user(unsigned int total_size,
771 const struct xt_table *table,
772 void __user *userptr)
774 unsigned int off, num;
775 const struct arpt_entry *e;
776 struct xt_counters *counters;
777 struct xt_table_info *private = table->private;
781 counters = alloc_counters(table);
782 if (IS_ERR(counters))
783 return PTR_ERR(counters);
785 loc_cpu_entry = private->entries;
786 /* ... then copy entire thing ... */
787 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
792 /* FIXME: use iterator macros --RR */
793 /* ... then go back and fix counters and names */
794 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
795 const struct xt_entry_target *t;
797 e = (struct arpt_entry *)(loc_cpu_entry + off);
798 if (copy_to_user(userptr + off
799 + offsetof(struct arpt_entry, counters),
801 sizeof(counters[num])) != 0) {
806 t = arpt_get_target_c(e);
807 if (copy_to_user(userptr + off + e->target_offset
808 + offsetof(struct xt_entry_target,
810 t->u.kernel.target->name,
811 strlen(t->u.kernel.target->name)+1) != 0) {
823 static void compat_standard_from_user(void *dst, const void *src)
825 int v = *(compat_int_t *)src;
828 v += xt_compat_calc_jump(NFPROTO_ARP, v);
829 memcpy(dst, &v, sizeof(v));
832 static int compat_standard_to_user(void __user *dst, const void *src)
834 compat_int_t cv = *(int *)src;
837 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
838 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
841 static int compat_calc_entry(const struct arpt_entry *e,
842 const struct xt_table_info *info,
843 const void *base, struct xt_table_info *newinfo)
845 const struct xt_entry_target *t;
846 unsigned int entry_offset;
849 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
850 entry_offset = (void *)e - base;
852 t = arpt_get_target_c(e);
853 off += xt_compat_target_offset(t->u.kernel.target);
854 newinfo->size -= off;
855 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
859 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
860 if (info->hook_entry[i] &&
861 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
862 newinfo->hook_entry[i] -= off;
863 if (info->underflow[i] &&
864 (e < (struct arpt_entry *)(base + info->underflow[i])))
865 newinfo->underflow[i] -= off;
870 static int compat_table_info(const struct xt_table_info *info,
871 struct xt_table_info *newinfo)
873 struct arpt_entry *iter;
874 const void *loc_cpu_entry;
877 if (!newinfo || !info)
880 /* we dont care about newinfo->entries */
881 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
882 newinfo->initial_entries = 0;
883 loc_cpu_entry = info->entries;
884 xt_compat_init_offsets(NFPROTO_ARP, info->number);
885 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
886 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
894 static int get_info(struct net *net, void __user *user,
895 const int *len, int compat)
897 char name[XT_TABLE_MAXNAMELEN];
901 if (*len != sizeof(struct arpt_getinfo)) {
902 duprintf("length %u != %Zu\n", *len,
903 sizeof(struct arpt_getinfo));
907 if (copy_from_user(name, user, sizeof(name)) != 0)
910 name[XT_TABLE_MAXNAMELEN-1] = '\0';
913 xt_compat_lock(NFPROTO_ARP);
915 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
916 "arptable_%s", name);
917 if (!IS_ERR_OR_NULL(t)) {
918 struct arpt_getinfo info;
919 const struct xt_table_info *private = t->private;
921 struct xt_table_info tmp;
924 ret = compat_table_info(private, &tmp);
925 xt_compat_flush_offsets(NFPROTO_ARP);
929 memset(&info, 0, sizeof(info));
930 info.valid_hooks = t->valid_hooks;
931 memcpy(info.hook_entry, private->hook_entry,
932 sizeof(info.hook_entry));
933 memcpy(info.underflow, private->underflow,
934 sizeof(info.underflow));
935 info.num_entries = private->number;
936 info.size = private->size;
937 strcpy(info.name, name);
939 if (copy_to_user(user, &info, *len) != 0)
946 ret = t ? PTR_ERR(t) : -ENOENT;
949 xt_compat_unlock(NFPROTO_ARP);
954 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
958 struct arpt_get_entries get;
961 if (*len < sizeof(get)) {
962 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
965 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
967 if (*len != sizeof(struct arpt_get_entries) + get.size) {
968 duprintf("get_entries: %u != %Zu\n", *len,
969 sizeof(struct arpt_get_entries) + get.size);
973 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
974 if (!IS_ERR_OR_NULL(t)) {
975 const struct xt_table_info *private = t->private;
977 duprintf("t->private->number = %u\n",
979 if (get.size == private->size)
980 ret = copy_entries_to_user(private->size,
981 t, uptr->entrytable);
983 duprintf("get_entries: I've got %u not %u!\n",
984 private->size, get.size);
990 ret = t ? PTR_ERR(t) : -ENOENT;
995 static int __do_replace(struct net *net, const char *name,
996 unsigned int valid_hooks,
997 struct xt_table_info *newinfo,
998 unsigned int num_counters,
999 void __user *counters_ptr)
1003 struct xt_table_info *oldinfo;
1004 struct xt_counters *counters;
1005 void *loc_cpu_old_entry;
1006 struct arpt_entry *iter;
1009 counters = vzalloc(num_counters * sizeof(struct xt_counters));
1015 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1016 "arptable_%s", name);
1017 if (IS_ERR_OR_NULL(t)) {
1018 ret = t ? PTR_ERR(t) : -ENOENT;
1019 goto free_newinfo_counters_untrans;
1023 if (valid_hooks != t->valid_hooks) {
1024 duprintf("Valid hook crap: %08X vs %08X\n",
1025 valid_hooks, t->valid_hooks);
1030 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1034 /* Update module usage count based on number of rules */
1035 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1036 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1037 if ((oldinfo->number > oldinfo->initial_entries) ||
1038 (newinfo->number <= oldinfo->initial_entries))
1040 if ((oldinfo->number > oldinfo->initial_entries) &&
1041 (newinfo->number <= oldinfo->initial_entries))
1044 /* Get the old counters, and synchronize with replace */
1045 get_counters(oldinfo, counters);
1047 /* Decrease module usage counts and free resource */
1048 loc_cpu_old_entry = oldinfo->entries;
1049 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1050 cleanup_entry(iter);
1052 xt_free_table_info(oldinfo);
1053 if (copy_to_user(counters_ptr, counters,
1054 sizeof(struct xt_counters) * num_counters) != 0) {
1055 /* Silent error, can't fail, new table is already in place */
1056 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
1065 free_newinfo_counters_untrans:
1071 static int do_replace(struct net *net, const void __user *user,
1075 struct arpt_replace tmp;
1076 struct xt_table_info *newinfo;
1077 void *loc_cpu_entry;
1078 struct arpt_entry *iter;
1080 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1083 /* overflow check */
1084 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1086 if (tmp.num_counters == 0)
1089 tmp.name[sizeof(tmp.name)-1] = 0;
1091 newinfo = xt_alloc_table_info(tmp.size);
1095 loc_cpu_entry = newinfo->entries;
1096 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1102 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
1106 duprintf("arp_tables: Translated table\n");
1108 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1109 tmp.num_counters, tmp.counters);
1111 goto free_newinfo_untrans;
1114 free_newinfo_untrans:
1115 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1116 cleanup_entry(iter);
1118 xt_free_table_info(newinfo);
1122 static int do_add_counters(struct net *net, const void __user *user,
1123 unsigned int len, int compat)
1126 struct xt_counters_info tmp;
1127 struct xt_counters *paddc;
1128 unsigned int num_counters;
1133 const struct xt_table_info *private;
1135 struct arpt_entry *iter;
1136 unsigned int addend;
1137 #ifdef CONFIG_COMPAT
1138 struct compat_xt_counters_info compat_tmp;
1142 size = sizeof(struct compat_xt_counters_info);
1147 size = sizeof(struct xt_counters_info);
1150 if (copy_from_user(ptmp, user, size) != 0)
1153 #ifdef CONFIG_COMPAT
1155 num_counters = compat_tmp.num_counters;
1156 name = compat_tmp.name;
1160 num_counters = tmp.num_counters;
1164 if (len != size + num_counters * sizeof(struct xt_counters))
1167 paddc = vmalloc(len - size);
1171 if (copy_from_user(paddc, user + size, len - size) != 0) {
1176 t = xt_find_table_lock(net, NFPROTO_ARP, name);
1177 if (IS_ERR_OR_NULL(t)) {
1178 ret = t ? PTR_ERR(t) : -ENOENT;
1183 private = t->private;
1184 if (private->number != num_counters) {
1186 goto unlock_up_free;
1191 addend = xt_write_recseq_begin();
1192 xt_entry_foreach(iter, private->entries, private->size) {
1193 struct xt_counters *tmp;
1195 tmp = xt_get_this_cpu_counter(&iter->counters);
1196 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1199 xt_write_recseq_end(addend);
1210 #ifdef CONFIG_COMPAT
1211 static inline void compat_release_entry(struct compat_arpt_entry *e)
1213 struct xt_entry_target *t;
1215 t = compat_arpt_get_target(e);
1216 module_put(t->u.kernel.target->me);
1220 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1221 struct xt_table_info *newinfo,
1223 const unsigned char *base,
1224 const unsigned char *limit,
1225 const unsigned int *hook_entries,
1226 const unsigned int *underflows,
1229 struct xt_entry_target *t;
1230 struct xt_target *target;
1231 unsigned int entry_offset;
1234 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1235 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1236 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1237 duprintf("Bad offset %p, limit = %p\n", e, limit);
1241 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1242 sizeof(struct compat_xt_entry_target)) {
1243 duprintf("checking: element %p size %u\n",
1248 /* For purposes of check_entry casting the compat entry is fine */
1249 ret = check_entry((struct arpt_entry *)e, name);
1253 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1254 entry_offset = (void *)e - (void *)base;
1256 t = compat_arpt_get_target(e);
1257 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1258 t->u.user.revision);
1259 if (IS_ERR(target)) {
1260 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1262 ret = PTR_ERR(target);
1265 t->u.kernel.target = target;
1267 off += xt_compat_target_offset(target);
1269 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1271 goto release_target;
1273 /* Check hooks & underflows */
1274 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1275 if ((unsigned char *)e - base == hook_entries[h])
1276 newinfo->hook_entry[h] = hook_entries[h];
1277 if ((unsigned char *)e - base == underflows[h])
1278 newinfo->underflow[h] = underflows[h];
1281 /* Clear counters and comefrom */
1282 memset(&e->counters, 0, sizeof(e->counters));
1287 module_put(t->u.kernel.target->me);
1293 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1294 unsigned int *size, const char *name,
1295 struct xt_table_info *newinfo, unsigned char *base)
1297 struct xt_entry_target *t;
1298 struct xt_target *target;
1299 struct arpt_entry *de;
1300 unsigned int origsize;
1305 de = (struct arpt_entry *)*dstptr;
1306 memcpy(de, e, sizeof(struct arpt_entry));
1307 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1309 *dstptr += sizeof(struct arpt_entry);
1310 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1312 de->target_offset = e->target_offset - (origsize - *size);
1313 t = compat_arpt_get_target(e);
1314 target = t->u.kernel.target;
1315 xt_compat_target_from_user(t, dstptr, size);
1317 de->next_offset = e->next_offset - (origsize - *size);
1318 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1319 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1320 newinfo->hook_entry[h] -= origsize - *size;
1321 if ((unsigned char *)de - base < newinfo->underflow[h])
1322 newinfo->underflow[h] -= origsize - *size;
1327 static int translate_compat_table(const char *name,
1328 unsigned int valid_hooks,
1329 struct xt_table_info **pinfo,
1331 unsigned int total_size,
1332 unsigned int number,
1333 unsigned int *hook_entries,
1334 unsigned int *underflows)
1337 struct xt_table_info *newinfo, *info;
1338 void *pos, *entry0, *entry1;
1339 struct compat_arpt_entry *iter0;
1340 struct arpt_entry *iter1;
1347 info->number = number;
1349 /* Init all hooks to impossible value. */
1350 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1351 info->hook_entry[i] = 0xFFFFFFFF;
1352 info->underflow[i] = 0xFFFFFFFF;
1355 duprintf("translate_compat_table: size %u\n", info->size);
1357 xt_compat_lock(NFPROTO_ARP);
1358 xt_compat_init_offsets(NFPROTO_ARP, number);
1359 /* Walk through entries, checking offsets. */
1360 xt_entry_foreach(iter0, entry0, total_size) {
1361 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1363 entry0 + total_size,
1374 duprintf("translate_compat_table: %u not %u entries\n",
1379 /* Check hooks all assigned */
1380 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1381 /* Only hooks which are valid */
1382 if (!(valid_hooks & (1 << i)))
1384 if (info->hook_entry[i] == 0xFFFFFFFF) {
1385 duprintf("Invalid hook entry %u %u\n",
1386 i, hook_entries[i]);
1389 if (info->underflow[i] == 0xFFFFFFFF) {
1390 duprintf("Invalid underflow %u %u\n",
1397 newinfo = xt_alloc_table_info(size);
1401 newinfo->number = number;
1402 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1403 newinfo->hook_entry[i] = info->hook_entry[i];
1404 newinfo->underflow[i] = info->underflow[i];
1406 entry1 = newinfo->entries;
1409 xt_entry_foreach(iter0, entry0, total_size) {
1410 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1411 name, newinfo, entry1);
1415 xt_compat_flush_offsets(NFPROTO_ARP);
1416 xt_compat_unlock(NFPROTO_ARP);
1421 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1425 xt_entry_foreach(iter1, entry1, newinfo->size) {
1426 iter1->counters.pcnt = xt_percpu_counter_alloc();
1427 if (IS_ERR_VALUE(iter1->counters.pcnt)) {
1432 ret = check_target(iter1, name);
1434 xt_percpu_counter_free(iter1->counters.pcnt);
1438 if (strcmp(arpt_get_target(iter1)->u.user.name,
1439 XT_ERROR_TARGET) == 0)
1440 ++newinfo->stacksize;
1444 * The first i matches need cleanup_entry (calls ->destroy)
1445 * because they had called ->check already. The other j-i
1446 * entries need only release.
1450 xt_entry_foreach(iter0, entry0, newinfo->size) {
1455 compat_release_entry(iter0);
1457 xt_entry_foreach(iter1, entry1, newinfo->size) {
1460 cleanup_entry(iter1);
1462 xt_free_table_info(newinfo);
1468 xt_free_table_info(info);
1472 xt_free_table_info(newinfo);
1474 xt_entry_foreach(iter0, entry0, total_size) {
1477 compat_release_entry(iter0);
1481 xt_compat_flush_offsets(NFPROTO_ARP);
1482 xt_compat_unlock(NFPROTO_ARP);
1486 struct compat_arpt_replace {
1487 char name[XT_TABLE_MAXNAMELEN];
1491 u32 hook_entry[NF_ARP_NUMHOOKS];
1492 u32 underflow[NF_ARP_NUMHOOKS];
1494 compat_uptr_t counters;
1495 struct compat_arpt_entry entries[0];
1498 static int compat_do_replace(struct net *net, void __user *user,
1502 struct compat_arpt_replace tmp;
1503 struct xt_table_info *newinfo;
1504 void *loc_cpu_entry;
1505 struct arpt_entry *iter;
1507 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1510 /* overflow check */
1511 if (tmp.size >= INT_MAX / num_possible_cpus())
1513 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1515 if (tmp.num_counters == 0)
1518 tmp.name[sizeof(tmp.name)-1] = 0;
1520 newinfo = xt_alloc_table_info(tmp.size);
1524 loc_cpu_entry = newinfo->entries;
1525 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1530 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1531 &newinfo, &loc_cpu_entry, tmp.size,
1532 tmp.num_entries, tmp.hook_entry,
1537 duprintf("compat_do_replace: Translated table\n");
1539 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1540 tmp.num_counters, compat_ptr(tmp.counters));
1542 goto free_newinfo_untrans;
1545 free_newinfo_untrans:
1546 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1547 cleanup_entry(iter);
1549 xt_free_table_info(newinfo);
1553 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1558 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1562 case ARPT_SO_SET_REPLACE:
1563 ret = compat_do_replace(sock_net(sk), user, len);
1566 case ARPT_SO_SET_ADD_COUNTERS:
1567 ret = do_add_counters(sock_net(sk), user, len, 1);
1571 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1578 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1579 compat_uint_t *size,
1580 struct xt_counters *counters,
1583 struct xt_entry_target *t;
1584 struct compat_arpt_entry __user *ce;
1585 u_int16_t target_offset, next_offset;
1586 compat_uint_t origsize;
1590 ce = (struct compat_arpt_entry __user *)*dstptr;
1591 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1592 copy_to_user(&ce->counters, &counters[i],
1593 sizeof(counters[i])) != 0)
1596 *dstptr += sizeof(struct compat_arpt_entry);
1597 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1599 target_offset = e->target_offset - (origsize - *size);
1601 t = arpt_get_target(e);
1602 ret = xt_compat_target_to_user(t, dstptr, size);
1605 next_offset = e->next_offset - (origsize - *size);
1606 if (put_user(target_offset, &ce->target_offset) != 0 ||
1607 put_user(next_offset, &ce->next_offset) != 0)
1612 static int compat_copy_entries_to_user(unsigned int total_size,
1613 struct xt_table *table,
1614 void __user *userptr)
1616 struct xt_counters *counters;
1617 const struct xt_table_info *private = table->private;
1622 struct arpt_entry *iter;
1624 counters = alloc_counters(table);
1625 if (IS_ERR(counters))
1626 return PTR_ERR(counters);
1630 xt_entry_foreach(iter, private->entries, total_size) {
1631 ret = compat_copy_entry_to_user(iter, &pos,
1632 &size, counters, i++);
1640 struct compat_arpt_get_entries {
1641 char name[XT_TABLE_MAXNAMELEN];
1643 struct compat_arpt_entry entrytable[0];
1646 static int compat_get_entries(struct net *net,
1647 struct compat_arpt_get_entries __user *uptr,
1651 struct compat_arpt_get_entries get;
1654 if (*len < sizeof(get)) {
1655 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1658 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1660 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1661 duprintf("compat_get_entries: %u != %zu\n",
1662 *len, sizeof(get) + get.size);
1666 xt_compat_lock(NFPROTO_ARP);
1667 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1668 if (!IS_ERR_OR_NULL(t)) {
1669 const struct xt_table_info *private = t->private;
1670 struct xt_table_info info;
1672 duprintf("t->private->number = %u\n", private->number);
1673 ret = compat_table_info(private, &info);
1674 if (!ret && get.size == info.size) {
1675 ret = compat_copy_entries_to_user(private->size,
1676 t, uptr->entrytable);
1678 duprintf("compat_get_entries: I've got %u not %u!\n",
1679 private->size, get.size);
1682 xt_compat_flush_offsets(NFPROTO_ARP);
1686 ret = t ? PTR_ERR(t) : -ENOENT;
1688 xt_compat_unlock(NFPROTO_ARP);
1692 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1694 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1699 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1703 case ARPT_SO_GET_INFO:
1704 ret = get_info(sock_net(sk), user, len, 1);
1706 case ARPT_SO_GET_ENTRIES:
1707 ret = compat_get_entries(sock_net(sk), user, len);
1710 ret = do_arpt_get_ctl(sk, cmd, user, len);
1716 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1720 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1724 case ARPT_SO_SET_REPLACE:
1725 ret = do_replace(sock_net(sk), user, len);
1728 case ARPT_SO_SET_ADD_COUNTERS:
1729 ret = do_add_counters(sock_net(sk), user, len, 0);
1733 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1740 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1744 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1748 case ARPT_SO_GET_INFO:
1749 ret = get_info(sock_net(sk), user, len, 0);
1752 case ARPT_SO_GET_ENTRIES:
1753 ret = get_entries(sock_net(sk), user, len);
1756 case ARPT_SO_GET_REVISION_TARGET: {
1757 struct xt_get_revision rev;
1759 if (*len != sizeof(rev)) {
1763 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1767 rev.name[sizeof(rev.name)-1] = 0;
1769 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1770 rev.revision, 1, &ret),
1771 "arpt_%s", rev.name);
1776 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1783 static void __arpt_unregister_table(struct xt_table *table)
1785 struct xt_table_info *private;
1786 void *loc_cpu_entry;
1787 struct module *table_owner = table->me;
1788 struct arpt_entry *iter;
1790 private = xt_unregister_table(table);
1792 /* Decrease module usage counts and free resources */
1793 loc_cpu_entry = private->entries;
1794 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1795 cleanup_entry(iter);
1796 if (private->number > private->initial_entries)
1797 module_put(table_owner);
1798 xt_free_table_info(private);
1801 int arpt_register_table(struct net *net,
1802 const struct xt_table *table,
1803 const struct arpt_replace *repl,
1804 const struct nf_hook_ops *ops,
1805 struct xt_table **res)
1808 struct xt_table_info *newinfo;
1809 struct xt_table_info bootstrap = {0};
1810 void *loc_cpu_entry;
1811 struct xt_table *new_table;
1813 newinfo = xt_alloc_table_info(repl->size);
1817 loc_cpu_entry = newinfo->entries;
1818 memcpy(loc_cpu_entry, repl->entries, repl->size);
1820 ret = translate_table(newinfo, loc_cpu_entry, repl);
1821 duprintf("arpt_register_table: translate table gives %d\n", ret);
1825 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1826 if (IS_ERR(new_table)) {
1827 ret = PTR_ERR(new_table);
1831 /* set res now, will see skbs right after nf_register_net_hooks */
1832 WRITE_ONCE(*res, new_table);
1834 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1836 __arpt_unregister_table(new_table);
1843 xt_free_table_info(newinfo);
1847 void arpt_unregister_table(struct net *net, struct xt_table *table,
1848 const struct nf_hook_ops *ops)
1850 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1851 __arpt_unregister_table(table);
1854 /* The built-in targets: standard (NULL) and error. */
1855 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1857 .name = XT_STANDARD_TARGET,
1858 .targetsize = sizeof(int),
1859 .family = NFPROTO_ARP,
1860 #ifdef CONFIG_COMPAT
1861 .compatsize = sizeof(compat_int_t),
1862 .compat_from_user = compat_standard_from_user,
1863 .compat_to_user = compat_standard_to_user,
1867 .name = XT_ERROR_TARGET,
1868 .target = arpt_error,
1869 .targetsize = XT_FUNCTION_MAXNAMELEN,
1870 .family = NFPROTO_ARP,
1874 static struct nf_sockopt_ops arpt_sockopts = {
1876 .set_optmin = ARPT_BASE_CTL,
1877 .set_optmax = ARPT_SO_SET_MAX+1,
1878 .set = do_arpt_set_ctl,
1879 #ifdef CONFIG_COMPAT
1880 .compat_set = compat_do_arpt_set_ctl,
1882 .get_optmin = ARPT_BASE_CTL,
1883 .get_optmax = ARPT_SO_GET_MAX+1,
1884 .get = do_arpt_get_ctl,
1885 #ifdef CONFIG_COMPAT
1886 .compat_get = compat_do_arpt_get_ctl,
1888 .owner = THIS_MODULE,
1891 static int __net_init arp_tables_net_init(struct net *net)
1893 return xt_proto_init(net, NFPROTO_ARP);
1896 static void __net_exit arp_tables_net_exit(struct net *net)
1898 xt_proto_fini(net, NFPROTO_ARP);
1901 static struct pernet_operations arp_tables_net_ops = {
1902 .init = arp_tables_net_init,
1903 .exit = arp_tables_net_exit,
1906 static int __init arp_tables_init(void)
1910 ret = register_pernet_subsys(&arp_tables_net_ops);
1914 /* No one else will be downing sem now, so we won't sleep */
1915 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1919 /* Register setsockopt */
1920 ret = nf_register_sockopt(&arpt_sockopts);
1924 pr_info("arp_tables: (C) 2002 David S. Miller\n");
1928 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1930 unregister_pernet_subsys(&arp_tables_net_ops);
1935 static void __exit arp_tables_fini(void)
1937 nf_unregister_sockopt(&arpt_sockopts);
1938 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1939 unregister_pernet_subsys(&arp_tables_net_ops);
1942 EXPORT_SYMBOL(arpt_register_table);
1943 EXPORT_SYMBOL(arpt_unregister_table);
1944 EXPORT_SYMBOL(arpt_do_table);
1946 module_init(arp_tables_init);
1947 module_exit(arp_tables_fini);