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...) printk(format , ## args)
43 #define dprintf(format, args...)
46 #ifdef DEBUG_ARP_TABLES_USER
47 #define duprintf(format, args...) printk(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
,
251 const struct net_device
*in
,
252 const struct net_device
*out
,
253 struct xt_table
*table
)
255 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
256 unsigned int verdict
= NF_DROP
;
257 const struct arphdr
*arp
;
258 struct arpt_entry
*e
, *back
;
259 const char *indev
, *outdev
;
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
= in
? in
->name
: nulldevname
;
269 outdev
= out
? out
->name
: nulldevname
;
272 addend
= xt_write_recseq_begin();
273 private = table
->private;
274 table_base
= private->entries
[smp_processor_id()];
276 e
= get_entry(table_base
, private->hook_entry
[hook
]);
277 back
= get_entry(table_base
, private->underflow
[hook
]);
281 acpar
.hooknum
= hook
;
282 acpar
.family
= NFPROTO_ARP
;
283 acpar
.hotdrop
= false;
287 const struct xt_entry_target
*t
;
289 if (!arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
290 e
= arpt_next_entry(e
);
294 ADD_COUNTER(e
->counters
, arp_hdr_len(skb
->dev
), 1);
296 t
= arpt_get_target_c(e
);
298 /* Standard target? */
299 if (!t
->u
.kernel
.target
->target
) {
302 v
= ((struct xt_standard_target
*)t
)->verdict
;
304 /* Pop from stack? */
305 if (v
!= XT_RETURN
) {
306 verdict
= (unsigned int)(-v
) - 1;
310 back
= get_entry(table_base
, back
->comefrom
);
314 != arpt_next_entry(e
)) {
315 /* Save old back ptr in next entry */
316 struct arpt_entry
*next
= arpt_next_entry(e
);
317 next
->comefrom
= (void *)back
- table_base
;
319 /* set back pointer to next entry */
323 e
= get_entry(table_base
, v
);
327 /* Targets which reenter must return
330 acpar
.target
= t
->u
.kernel
.target
;
331 acpar
.targinfo
= t
->data
;
332 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
334 /* Target might have changed stuff. */
337 if (verdict
== XT_CONTINUE
)
338 e
= arpt_next_entry(e
);
342 } while (!acpar
.hotdrop
);
343 xt_write_recseq_end(addend
);
352 /* All zeroes == unconditional rule. */
353 static inline bool unconditional(const struct arpt_entry
*e
)
355 static const struct arpt_arp uncond
;
357 return e
->target_offset
== sizeof(struct arpt_entry
) &&
358 memcmp(&e
->arp
, &uncond
, sizeof(uncond
)) == 0;
361 /* Figures out from what hook each rule can be called: returns 0 if
362 * there are loops. Puts hook bitmask in comefrom.
364 static int mark_source_chains(const struct xt_table_info
*newinfo
,
365 unsigned int valid_hooks
, void *entry0
)
369 /* No recursion; use packet counter to save back ptrs (reset
370 * to 0 as we leave), and comefrom to save source hook bitmask.
372 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
373 unsigned int pos
= newinfo
->hook_entry
[hook
];
375 = (struct arpt_entry
*)(entry0
+ pos
);
377 if (!(valid_hooks
& (1 << hook
)))
380 /* Set initial back pointer. */
381 e
->counters
.pcnt
= pos
;
384 const struct xt_standard_target
*t
385 = (void *)arpt_get_target_c(e
);
386 int visited
= e
->comefrom
& (1 << hook
);
388 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
389 pr_notice("arptables: loop hook %u pos %u %08X.\n",
390 hook
, pos
, e
->comefrom
);
394 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
396 /* Unconditional return/END. */
397 if ((unconditional(e
) &&
398 (strcmp(t
->target
.u
.user
.name
,
399 XT_STANDARD_TARGET
) == 0) &&
400 t
->verdict
< 0) || visited
) {
401 unsigned int oldpos
, size
;
403 if ((strcmp(t
->target
.u
.user
.name
,
404 XT_STANDARD_TARGET
) == 0) &&
405 t
->verdict
< -NF_MAX_VERDICT
- 1) {
406 duprintf("mark_source_chains: bad "
407 "negative verdict (%i)\n",
412 /* Return: backtrack through the last
416 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
418 pos
= e
->counters
.pcnt
;
419 e
->counters
.pcnt
= 0;
421 /* We're at the start. */
425 e
= (struct arpt_entry
*)
427 } while (oldpos
== pos
+ e
->next_offset
);
430 size
= e
->next_offset
;
431 e
= (struct arpt_entry
*)
432 (entry0
+ pos
+ size
);
433 if (pos
+ size
>= newinfo
->size
)
435 e
->counters
.pcnt
= pos
;
438 int newpos
= t
->verdict
;
440 if (strcmp(t
->target
.u
.user
.name
,
441 XT_STANDARD_TARGET
) == 0 &&
443 if (newpos
> newinfo
->size
-
444 sizeof(struct arpt_entry
)) {
445 duprintf("mark_source_chains: "
446 "bad verdict (%i)\n",
451 /* This a jump; chase it. */
452 duprintf("Jump rule %u -> %u\n",
455 /* ... this is a fallthru */
456 newpos
= pos
+ e
->next_offset
;
457 if (newpos
>= newinfo
->size
)
460 e
= (struct arpt_entry
*)
462 e
->counters
.pcnt
= pos
;
467 duprintf("Finished chain %u\n", hook
);
472 static inline int check_target(struct arpt_entry
*e
, const char *name
)
474 struct xt_entry_target
*t
= arpt_get_target(e
);
476 struct xt_tgchk_param par
= {
479 .target
= t
->u
.kernel
.target
,
481 .hook_mask
= e
->comefrom
,
482 .family
= NFPROTO_ARP
,
485 ret
= xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
), 0, false);
487 duprintf("arp_tables: check failed for `%s'.\n",
488 t
->u
.kernel
.target
->name
);
495 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
)
497 struct xt_entry_target
*t
;
498 struct xt_target
*target
;
501 t
= arpt_get_target(e
);
502 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
504 if (IS_ERR(target
)) {
505 duprintf("find_check_entry: `%s' not found\n", t
->u
.user
.name
);
506 ret
= PTR_ERR(target
);
509 t
->u
.kernel
.target
= target
;
511 ret
= check_target(e
, name
);
516 module_put(t
->u
.kernel
.target
->me
);
521 static bool check_underflow(const struct arpt_entry
*e
)
523 const struct xt_entry_target
*t
;
524 unsigned int verdict
;
526 if (!unconditional(e
))
528 t
= arpt_get_target_c(e
);
529 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
531 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
532 verdict
= -verdict
- 1;
533 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
536 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
537 struct xt_table_info
*newinfo
,
538 const unsigned char *base
,
539 const unsigned char *limit
,
540 const unsigned int *hook_entries
,
541 const unsigned int *underflows
,
542 unsigned int valid_hooks
)
547 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0 ||
548 (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
||
549 (unsigned char *)e
+ e
->next_offset
> limit
) {
550 duprintf("Bad offset %p\n", e
);
555 < sizeof(struct arpt_entry
) + sizeof(struct xt_entry_target
)) {
556 duprintf("checking: element %p size %u\n",
561 if (!arp_checkentry(&e
->arp
))
564 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
569 /* Check hooks & underflows */
570 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
571 if (!(valid_hooks
& (1 << h
)))
573 if ((unsigned char *)e
- base
== hook_entries
[h
])
574 newinfo
->hook_entry
[h
] = hook_entries
[h
];
575 if ((unsigned char *)e
- base
== underflows
[h
]) {
576 if (!check_underflow(e
)) {
577 pr_debug("Underflows must be unconditional and "
578 "use the STANDARD target with "
582 newinfo
->underflow
[h
] = underflows
[h
];
586 /* Clear counters and comefrom */
587 e
->counters
= ((struct xt_counters
) { 0, 0 });
592 static inline void cleanup_entry(struct arpt_entry
*e
)
594 struct xt_tgdtor_param par
;
595 struct xt_entry_target
*t
;
597 t
= arpt_get_target(e
);
598 par
.target
= t
->u
.kernel
.target
;
599 par
.targinfo
= t
->data
;
600 par
.family
= NFPROTO_ARP
;
601 if (par
.target
->destroy
!= NULL
)
602 par
.target
->destroy(&par
);
603 module_put(par
.target
->me
);
606 /* Checks and translates the user-supplied table segment (held in
609 static int translate_table(struct xt_table_info
*newinfo
, void *entry0
,
610 const struct arpt_replace
*repl
)
612 struct arpt_entry
*iter
;
616 newinfo
->size
= repl
->size
;
617 newinfo
->number
= repl
->num_entries
;
619 /* Init all hooks to impossible value. */
620 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
621 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
622 newinfo
->underflow
[i
] = 0xFFFFFFFF;
625 duprintf("translate_table: size %u\n", newinfo
->size
);
628 /* Walk through entries, checking offsets. */
629 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
630 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
638 if (strcmp(arpt_get_target(iter
)->u
.user
.name
,
639 XT_ERROR_TARGET
) == 0)
640 ++newinfo
->stacksize
;
642 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
646 if (i
!= repl
->num_entries
) {
647 duprintf("translate_table: %u not %u entries\n",
648 i
, repl
->num_entries
);
652 /* Check hooks all assigned */
653 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
654 /* Only hooks which are valid */
655 if (!(repl
->valid_hooks
& (1 << i
)))
657 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
658 duprintf("Invalid hook entry %u %u\n",
659 i
, repl
->hook_entry
[i
]);
662 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
663 duprintf("Invalid underflow %u %u\n",
664 i
, repl
->underflow
[i
]);
669 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
))
672 /* Finally, each sanity check must pass */
674 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
675 ret
= find_check_entry(iter
, repl
->name
, repl
->size
);
682 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
690 /* And one copy for every other CPU */
691 for_each_possible_cpu(i
) {
692 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry0
)
693 memcpy(newinfo
->entries
[i
], entry0
, newinfo
->size
);
699 static void get_counters(const struct xt_table_info
*t
,
700 struct xt_counters counters
[])
702 struct arpt_entry
*iter
;
706 for_each_possible_cpu(cpu
) {
707 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
710 xt_entry_foreach(iter
, t
->entries
[cpu
], t
->size
) {
715 start
= read_seqcount_begin(s
);
716 bcnt
= iter
->counters
.bcnt
;
717 pcnt
= iter
->counters
.pcnt
;
718 } while (read_seqcount_retry(s
, start
));
720 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
726 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
728 unsigned int countersize
;
729 struct xt_counters
*counters
;
730 const struct xt_table_info
*private = table
->private;
732 /* We need atomic snapshot of counters: rest doesn't change
733 * (other than comefrom, which userspace doesn't care
736 countersize
= sizeof(struct xt_counters
) * private->number
;
737 counters
= vzalloc(countersize
);
739 if (counters
== NULL
)
740 return ERR_PTR(-ENOMEM
);
742 get_counters(private, counters
);
747 static int copy_entries_to_user(unsigned int total_size
,
748 const struct xt_table
*table
,
749 void __user
*userptr
)
751 unsigned int off
, num
;
752 const struct arpt_entry
*e
;
753 struct xt_counters
*counters
;
754 struct xt_table_info
*private = table
->private;
758 counters
= alloc_counters(table
);
759 if (IS_ERR(counters
))
760 return PTR_ERR(counters
);
762 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
763 /* ... then copy entire thing ... */
764 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
769 /* FIXME: use iterator macros --RR */
770 /* ... then go back and fix counters and names */
771 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
772 const struct xt_entry_target
*t
;
774 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
775 if (copy_to_user(userptr
+ off
776 + offsetof(struct arpt_entry
, counters
),
778 sizeof(counters
[num
])) != 0) {
783 t
= arpt_get_target_c(e
);
784 if (copy_to_user(userptr
+ off
+ e
->target_offset
785 + offsetof(struct xt_entry_target
,
787 t
->u
.kernel
.target
->name
,
788 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
800 static void compat_standard_from_user(void *dst
, const void *src
)
802 int v
= *(compat_int_t
*)src
;
805 v
+= xt_compat_calc_jump(NFPROTO_ARP
, v
);
806 memcpy(dst
, &v
, sizeof(v
));
809 static int compat_standard_to_user(void __user
*dst
, const void *src
)
811 compat_int_t cv
= *(int *)src
;
814 cv
-= xt_compat_calc_jump(NFPROTO_ARP
, cv
);
815 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
818 static int compat_calc_entry(const struct arpt_entry
*e
,
819 const struct xt_table_info
*info
,
820 const void *base
, struct xt_table_info
*newinfo
)
822 const struct xt_entry_target
*t
;
823 unsigned int entry_offset
;
826 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
827 entry_offset
= (void *)e
- base
;
829 t
= arpt_get_target_c(e
);
830 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
831 newinfo
->size
-= off
;
832 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
836 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
837 if (info
->hook_entry
[i
] &&
838 (e
< (struct arpt_entry
*)(base
+ info
->hook_entry
[i
])))
839 newinfo
->hook_entry
[i
] -= off
;
840 if (info
->underflow
[i
] &&
841 (e
< (struct arpt_entry
*)(base
+ info
->underflow
[i
])))
842 newinfo
->underflow
[i
] -= off
;
847 static int compat_table_info(const struct xt_table_info
*info
,
848 struct xt_table_info
*newinfo
)
850 struct arpt_entry
*iter
;
854 if (!newinfo
|| !info
)
857 /* we dont care about newinfo->entries[] */
858 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
859 newinfo
->initial_entries
= 0;
860 loc_cpu_entry
= info
->entries
[raw_smp_processor_id()];
861 xt_compat_init_offsets(NFPROTO_ARP
, info
->number
);
862 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
863 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
871 static int get_info(struct net
*net
, void __user
*user
,
872 const int *len
, int compat
)
874 char name
[XT_TABLE_MAXNAMELEN
];
878 if (*len
!= sizeof(struct arpt_getinfo
)) {
879 duprintf("length %u != %Zu\n", *len
,
880 sizeof(struct arpt_getinfo
));
884 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
887 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
890 xt_compat_lock(NFPROTO_ARP
);
892 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
893 "arptable_%s", name
);
894 if (!IS_ERR_OR_NULL(t
)) {
895 struct arpt_getinfo info
;
896 const struct xt_table_info
*private = t
->private;
898 struct xt_table_info tmp
;
901 ret
= compat_table_info(private, &tmp
);
902 xt_compat_flush_offsets(NFPROTO_ARP
);
906 memset(&info
, 0, sizeof(info
));
907 info
.valid_hooks
= t
->valid_hooks
;
908 memcpy(info
.hook_entry
, private->hook_entry
,
909 sizeof(info
.hook_entry
));
910 memcpy(info
.underflow
, private->underflow
,
911 sizeof(info
.underflow
));
912 info
.num_entries
= private->number
;
913 info
.size
= private->size
;
914 strcpy(info
.name
, name
);
916 if (copy_to_user(user
, &info
, *len
) != 0)
923 ret
= t
? PTR_ERR(t
) : -ENOENT
;
926 xt_compat_unlock(NFPROTO_ARP
);
931 static int get_entries(struct net
*net
, struct arpt_get_entries __user
*uptr
,
935 struct arpt_get_entries get
;
938 if (*len
< sizeof(get
)) {
939 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
942 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
944 if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
945 duprintf("get_entries: %u != %Zu\n", *len
,
946 sizeof(struct arpt_get_entries
) + get
.size
);
950 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
951 if (!IS_ERR_OR_NULL(t
)) {
952 const struct xt_table_info
*private = t
->private;
954 duprintf("t->private->number = %u\n",
956 if (get
.size
== private->size
)
957 ret
= copy_entries_to_user(private->size
,
958 t
, uptr
->entrytable
);
960 duprintf("get_entries: I've got %u not %u!\n",
961 private->size
, get
.size
);
967 ret
= t
? PTR_ERR(t
) : -ENOENT
;
972 static int __do_replace(struct net
*net
, const char *name
,
973 unsigned int valid_hooks
,
974 struct xt_table_info
*newinfo
,
975 unsigned int num_counters
,
976 void __user
*counters_ptr
)
980 struct xt_table_info
*oldinfo
;
981 struct xt_counters
*counters
;
982 void *loc_cpu_old_entry
;
983 struct arpt_entry
*iter
;
986 counters
= vzalloc(num_counters
* sizeof(struct xt_counters
));
992 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
993 "arptable_%s", name
);
994 if (IS_ERR_OR_NULL(t
)) {
995 ret
= t
? PTR_ERR(t
) : -ENOENT
;
996 goto free_newinfo_counters_untrans
;
1000 if (valid_hooks
!= t
->valid_hooks
) {
1001 duprintf("Valid hook crap: %08X vs %08X\n",
1002 valid_hooks
, t
->valid_hooks
);
1007 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
1011 /* Update module usage count based on number of rules */
1012 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1013 oldinfo
->number
, oldinfo
->initial_entries
, newinfo
->number
);
1014 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
1015 (newinfo
->number
<= oldinfo
->initial_entries
))
1017 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
1018 (newinfo
->number
<= oldinfo
->initial_entries
))
1021 /* Get the old counters, and synchronize with replace */
1022 get_counters(oldinfo
, counters
);
1024 /* Decrease module usage counts and free resource */
1025 loc_cpu_old_entry
= oldinfo
->entries
[raw_smp_processor_id()];
1026 xt_entry_foreach(iter
, loc_cpu_old_entry
, oldinfo
->size
)
1027 cleanup_entry(iter
);
1029 xt_free_table_info(oldinfo
);
1030 if (copy_to_user(counters_ptr
, counters
,
1031 sizeof(struct xt_counters
) * num_counters
) != 0) {
1032 /* Silent error, can't fail, new table is already in place */
1033 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
1042 free_newinfo_counters_untrans
:
1048 static int do_replace(struct net
*net
, const void __user
*user
,
1052 struct arpt_replace tmp
;
1053 struct xt_table_info
*newinfo
;
1054 void *loc_cpu_entry
;
1055 struct arpt_entry
*iter
;
1057 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1060 /* overflow check */
1061 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1063 if (tmp
.num_counters
== 0)
1066 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1068 newinfo
= xt_alloc_table_info(tmp
.size
);
1072 /* choose the copy that is on our node/cpu */
1073 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1074 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1080 ret
= translate_table(newinfo
, loc_cpu_entry
, &tmp
);
1084 duprintf("arp_tables: Translated table\n");
1086 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1087 tmp
.num_counters
, tmp
.counters
);
1089 goto free_newinfo_untrans
;
1092 free_newinfo_untrans
:
1093 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1094 cleanup_entry(iter
);
1096 xt_free_table_info(newinfo
);
1100 static int do_add_counters(struct net
*net
, const void __user
*user
,
1101 unsigned int len
, int compat
)
1103 unsigned int i
, curcpu
;
1104 struct xt_counters_info tmp
;
1105 struct xt_counters
*paddc
;
1107 const struct xt_table_info
*private;
1109 void *loc_cpu_entry
;
1110 struct arpt_entry
*iter
;
1111 unsigned int addend
;
1113 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1115 return PTR_ERR(paddc
);
1117 t
= xt_find_table_lock(net
, NFPROTO_ARP
, tmp
.name
);
1118 if (IS_ERR_OR_NULL(t
)) {
1119 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1124 private = t
->private;
1125 if (private->number
!= tmp
.num_counters
) {
1127 goto unlock_up_free
;
1131 /* Choose the copy that is on our node */
1132 curcpu
= smp_processor_id();
1133 loc_cpu_entry
= private->entries
[curcpu
];
1134 addend
= xt_write_recseq_begin();
1135 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
) {
1136 ADD_COUNTER(iter
->counters
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1139 xt_write_recseq_end(addend
);
1150 #ifdef CONFIG_COMPAT
1151 struct compat_arpt_replace
{
1152 char name
[XT_TABLE_MAXNAMELEN
];
1156 u32 hook_entry
[NF_ARP_NUMHOOKS
];
1157 u32 underflow
[NF_ARP_NUMHOOKS
];
1159 compat_uptr_t counters
;
1160 struct compat_arpt_entry entries
[0];
1163 static inline void compat_release_entry(struct compat_arpt_entry
*e
)
1165 struct xt_entry_target
*t
;
1167 t
= compat_arpt_get_target(e
);
1168 module_put(t
->u
.kernel
.target
->me
);
1172 check_compat_entry_size_and_hooks(struct compat_arpt_entry
*e
,
1173 struct xt_table_info
*newinfo
,
1175 const unsigned char *base
,
1176 const unsigned char *limit
)
1178 struct xt_entry_target
*t
;
1179 struct xt_target
*target
;
1180 unsigned int entry_offset
;
1183 duprintf("check_compat_entry_size_and_hooks %p\n", e
);
1184 if ((unsigned long)e
% __alignof__(struct compat_arpt_entry
) != 0 ||
1185 (unsigned char *)e
+ sizeof(struct compat_arpt_entry
) >= limit
||
1186 (unsigned char *)e
+ e
->next_offset
> limit
) {
1187 duprintf("Bad offset %p, limit = %p\n", e
, limit
);
1191 if (e
->next_offset
< sizeof(struct compat_arpt_entry
) +
1192 sizeof(struct compat_xt_entry_target
)) {
1193 duprintf("checking: element %p size %u\n",
1198 if (!arp_checkentry(&e
->arp
))
1201 ret
= xt_compat_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
1206 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1207 entry_offset
= (void *)e
- (void *)base
;
1209 t
= compat_arpt_get_target(e
);
1210 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
1211 t
->u
.user
.revision
);
1212 if (IS_ERR(target
)) {
1213 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1215 ret
= PTR_ERR(target
);
1218 t
->u
.kernel
.target
= target
;
1220 off
+= xt_compat_target_offset(target
);
1222 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
1224 goto release_target
;
1229 module_put(t
->u
.kernel
.target
->me
);
1235 compat_copy_entry_from_user(struct compat_arpt_entry
*e
, void **dstptr
,
1237 struct xt_table_info
*newinfo
, unsigned char *base
)
1239 struct xt_entry_target
*t
;
1240 struct xt_target
*target
;
1241 struct arpt_entry
*de
;
1242 unsigned int origsize
;
1246 de
= (struct arpt_entry
*)*dstptr
;
1247 memcpy(de
, e
, sizeof(struct arpt_entry
));
1248 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1250 *dstptr
+= sizeof(struct arpt_entry
);
1251 *size
+= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1253 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1254 t
= compat_arpt_get_target(e
);
1255 target
= t
->u
.kernel
.target
;
1256 xt_compat_target_from_user(t
, dstptr
, size
);
1258 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1259 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1260 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1261 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1262 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1263 newinfo
->underflow
[h
] -= origsize
- *size
;
1267 static int translate_compat_table(struct xt_table_info
**pinfo
,
1269 const struct compat_arpt_replace
*compatr
)
1272 struct xt_table_info
*newinfo
, *info
;
1273 void *pos
, *entry0
, *entry1
;
1274 struct compat_arpt_entry
*iter0
;
1275 struct arpt_replace repl
;
1281 size
= compatr
->size
;
1282 info
->number
= compatr
->num_entries
;
1284 duprintf("translate_compat_table: size %u\n", info
->size
);
1286 xt_compat_lock(NFPROTO_ARP
);
1287 xt_compat_init_offsets(NFPROTO_ARP
, compatr
->num_entries
);
1288 /* Walk through entries, checking offsets. */
1289 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1290 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1292 entry0
+ compatr
->size
);
1299 if (j
!= compatr
->num_entries
) {
1300 duprintf("translate_compat_table: %u not %u entries\n",
1301 j
, compatr
->num_entries
);
1306 newinfo
= xt_alloc_table_info(size
);
1310 newinfo
->number
= compatr
->num_entries
;
1311 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1312 newinfo
->hook_entry
[i
] = info
->hook_entry
[i
];
1313 newinfo
->underflow
[i
] = info
->underflow
[i
];
1315 entry1
= newinfo
->entries
[raw_smp_processor_id()];
1317 size
= compatr
->size
;
1318 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1319 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1322 /* all module references in entry0 are now gone */
1324 xt_compat_flush_offsets(NFPROTO_ARP
);
1325 xt_compat_unlock(NFPROTO_ARP
);
1327 memcpy(&repl
, compatr
, sizeof(*compatr
));
1329 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1330 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1331 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1334 repl
.num_counters
= 0;
1335 repl
.counters
= NULL
;
1336 repl
.size
= newinfo
->size
;
1337 ret
= translate_table(newinfo
, entry1
, &repl
);
1343 xt_free_table_info(info
);
1347 xt_free_table_info(newinfo
);
1350 xt_compat_flush_offsets(NFPROTO_ARP
);
1351 xt_compat_unlock(NFPROTO_ARP
);
1352 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1355 compat_release_entry(iter0
);
1360 static int compat_do_replace(struct net
*net
, void __user
*user
,
1364 struct compat_arpt_replace tmp
;
1365 struct xt_table_info
*newinfo
;
1366 void *loc_cpu_entry
;
1367 struct arpt_entry
*iter
;
1369 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1372 /* overflow check */
1373 if (tmp
.size
>= INT_MAX
/ num_possible_cpus())
1375 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1377 if (tmp
.num_counters
== 0)
1380 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1382 newinfo
= xt_alloc_table_info(tmp
.size
);
1386 /* choose the copy that is on our node/cpu */
1387 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1388 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
), tmp
.size
) != 0) {
1393 ret
= translate_compat_table(&newinfo
, &loc_cpu_entry
, &tmp
);
1397 duprintf("compat_do_replace: Translated table\n");
1399 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1400 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1402 goto free_newinfo_untrans
;
1405 free_newinfo_untrans
:
1406 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1407 cleanup_entry(iter
);
1409 xt_free_table_info(newinfo
);
1413 static int compat_do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1418 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1422 case ARPT_SO_SET_REPLACE
:
1423 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1426 case ARPT_SO_SET_ADD_COUNTERS
:
1427 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1431 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1438 static int compat_copy_entry_to_user(struct arpt_entry
*e
, void __user
**dstptr
,
1439 compat_uint_t
*size
,
1440 struct xt_counters
*counters
,
1443 struct xt_entry_target
*t
;
1444 struct compat_arpt_entry __user
*ce
;
1445 u_int16_t target_offset
, next_offset
;
1446 compat_uint_t origsize
;
1450 ce
= (struct compat_arpt_entry __user
*)*dstptr
;
1451 if (copy_to_user(ce
, e
, sizeof(struct arpt_entry
)) != 0 ||
1452 copy_to_user(&ce
->counters
, &counters
[i
],
1453 sizeof(counters
[i
])) != 0)
1456 *dstptr
+= sizeof(struct compat_arpt_entry
);
1457 *size
-= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1459 target_offset
= e
->target_offset
- (origsize
- *size
);
1461 t
= arpt_get_target(e
);
1462 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1465 next_offset
= e
->next_offset
- (origsize
- *size
);
1466 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1467 put_user(next_offset
, &ce
->next_offset
) != 0)
1472 static int compat_copy_entries_to_user(unsigned int total_size
,
1473 struct xt_table
*table
,
1474 void __user
*userptr
)
1476 struct xt_counters
*counters
;
1477 const struct xt_table_info
*private = table
->private;
1481 void *loc_cpu_entry
;
1483 struct arpt_entry
*iter
;
1485 counters
= alloc_counters(table
);
1486 if (IS_ERR(counters
))
1487 return PTR_ERR(counters
);
1489 /* choose the copy on our node/cpu */
1490 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1493 xt_entry_foreach(iter
, loc_cpu_entry
, total_size
) {
1494 ret
= compat_copy_entry_to_user(iter
, &pos
,
1495 &size
, counters
, i
++);
1503 struct compat_arpt_get_entries
{
1504 char name
[XT_TABLE_MAXNAMELEN
];
1506 struct compat_arpt_entry entrytable
[0];
1509 static int compat_get_entries(struct net
*net
,
1510 struct compat_arpt_get_entries __user
*uptr
,
1514 struct compat_arpt_get_entries get
;
1517 if (*len
< sizeof(get
)) {
1518 duprintf("compat_get_entries: %u < %zu\n", *len
, sizeof(get
));
1521 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1523 if (*len
!= sizeof(struct compat_arpt_get_entries
) + get
.size
) {
1524 duprintf("compat_get_entries: %u != %zu\n",
1525 *len
, sizeof(get
) + get
.size
);
1529 xt_compat_lock(NFPROTO_ARP
);
1530 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1531 if (!IS_ERR_OR_NULL(t
)) {
1532 const struct xt_table_info
*private = t
->private;
1533 struct xt_table_info info
;
1535 duprintf("t->private->number = %u\n", private->number
);
1536 ret
= compat_table_info(private, &info
);
1537 if (!ret
&& get
.size
== info
.size
) {
1538 ret
= compat_copy_entries_to_user(private->size
,
1539 t
, uptr
->entrytable
);
1541 duprintf("compat_get_entries: I've got %u not %u!\n",
1542 private->size
, get
.size
);
1545 xt_compat_flush_offsets(NFPROTO_ARP
);
1549 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1551 xt_compat_unlock(NFPROTO_ARP
);
1555 static int do_arpt_get_ctl(struct sock
*, int, void __user
*, int *);
1557 static int compat_do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1562 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1566 case ARPT_SO_GET_INFO
:
1567 ret
= get_info(sock_net(sk
), user
, len
, 1);
1569 case ARPT_SO_GET_ENTRIES
:
1570 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1573 ret
= do_arpt_get_ctl(sk
, cmd
, user
, len
);
1579 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1583 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1587 case ARPT_SO_SET_REPLACE
:
1588 ret
= do_replace(sock_net(sk
), user
, len
);
1591 case ARPT_SO_SET_ADD_COUNTERS
:
1592 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1596 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1603 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1607 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1611 case ARPT_SO_GET_INFO
:
1612 ret
= get_info(sock_net(sk
), user
, len
, 0);
1615 case ARPT_SO_GET_ENTRIES
:
1616 ret
= get_entries(sock_net(sk
), user
, len
);
1619 case ARPT_SO_GET_REVISION_TARGET
: {
1620 struct xt_get_revision rev
;
1622 if (*len
!= sizeof(rev
)) {
1626 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1630 rev
.name
[sizeof(rev
.name
)-1] = 0;
1632 try_then_request_module(xt_find_revision(NFPROTO_ARP
, rev
.name
,
1633 rev
.revision
, 1, &ret
),
1634 "arpt_%s", rev
.name
);
1639 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1646 struct xt_table
*arpt_register_table(struct net
*net
,
1647 const struct xt_table
*table
,
1648 const struct arpt_replace
*repl
)
1651 struct xt_table_info
*newinfo
;
1652 struct xt_table_info bootstrap
= {0};
1653 void *loc_cpu_entry
;
1654 struct xt_table
*new_table
;
1656 newinfo
= xt_alloc_table_info(repl
->size
);
1662 /* choose the copy on our node/cpu */
1663 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1664 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1666 ret
= translate_table(newinfo
, loc_cpu_entry
, repl
);
1667 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1671 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1672 if (IS_ERR(new_table
)) {
1673 ret
= PTR_ERR(new_table
);
1679 xt_free_table_info(newinfo
);
1681 return ERR_PTR(ret
);
1684 void arpt_unregister_table(struct xt_table
*table
)
1686 struct xt_table_info
*private;
1687 void *loc_cpu_entry
;
1688 struct module
*table_owner
= table
->me
;
1689 struct arpt_entry
*iter
;
1691 private = xt_unregister_table(table
);
1693 /* Decrease module usage counts and free resources */
1694 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1695 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1696 cleanup_entry(iter
);
1697 if (private->number
> private->initial_entries
)
1698 module_put(table_owner
);
1699 xt_free_table_info(private);
1702 /* The built-in targets: standard (NULL) and error. */
1703 static struct xt_target arpt_builtin_tg
[] __read_mostly
= {
1705 .name
= XT_STANDARD_TARGET
,
1706 .targetsize
= sizeof(int),
1707 .family
= NFPROTO_ARP
,
1708 #ifdef CONFIG_COMPAT
1709 .compatsize
= sizeof(compat_int_t
),
1710 .compat_from_user
= compat_standard_from_user
,
1711 .compat_to_user
= compat_standard_to_user
,
1715 .name
= XT_ERROR_TARGET
,
1716 .target
= arpt_error
,
1717 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1718 .family
= NFPROTO_ARP
,
1722 static struct nf_sockopt_ops arpt_sockopts
= {
1724 .set_optmin
= ARPT_BASE_CTL
,
1725 .set_optmax
= ARPT_SO_SET_MAX
+1,
1726 .set
= do_arpt_set_ctl
,
1727 #ifdef CONFIG_COMPAT
1728 .compat_set
= compat_do_arpt_set_ctl
,
1730 .get_optmin
= ARPT_BASE_CTL
,
1731 .get_optmax
= ARPT_SO_GET_MAX
+1,
1732 .get
= do_arpt_get_ctl
,
1733 #ifdef CONFIG_COMPAT
1734 .compat_get
= compat_do_arpt_get_ctl
,
1736 .owner
= THIS_MODULE
,
1739 static int __net_init
arp_tables_net_init(struct net
*net
)
1741 return xt_proto_init(net
, NFPROTO_ARP
);
1744 static void __net_exit
arp_tables_net_exit(struct net
*net
)
1746 xt_proto_fini(net
, NFPROTO_ARP
);
1749 static struct pernet_operations arp_tables_net_ops
= {
1750 .init
= arp_tables_net_init
,
1751 .exit
= arp_tables_net_exit
,
1754 static int __init
arp_tables_init(void)
1758 ret
= register_pernet_subsys(&arp_tables_net_ops
);
1762 /* No one else will be downing sem now, so we won't sleep */
1763 ret
= xt_register_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1767 /* Register setsockopt */
1768 ret
= nf_register_sockopt(&arpt_sockopts
);
1772 printk(KERN_INFO
"arp_tables: (C) 2002 David S. Miller\n");
1776 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1778 unregister_pernet_subsys(&arp_tables_net_ops
);
1783 static void __exit
arp_tables_fini(void)
1785 nf_unregister_sockopt(&arpt_sockopts
);
1786 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1787 unregister_pernet_subsys(&arp_tables_net_ops
);
1790 EXPORT_SYMBOL(arpt_register_table
);
1791 EXPORT_SYMBOL(arpt_unregister_table
);
1792 EXPORT_SYMBOL(arpt_do_table
);
1794 module_init(arp_tables_init
);
1795 module_exit(arp_tables_fini
);