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 if (NF_INVF(arpinfo
, ARPT_INV_ARPOP
,
93 (arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
))
96 if (NF_INVF(arpinfo
, ARPT_INV_ARPHRD
,
97 (arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
))
100 if (NF_INVF(arpinfo
, ARPT_INV_ARPPRO
,
101 (arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
))
104 if (NF_INVF(arpinfo
, ARPT_INV_ARPHLN
,
105 (arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
))
108 src_devaddr
= arpptr
;
109 arpptr
+= dev
->addr_len
;
110 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
111 arpptr
+= sizeof(u32
);
112 tgt_devaddr
= arpptr
;
113 arpptr
+= dev
->addr_len
;
114 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
116 if (NF_INVF(arpinfo
, ARPT_INV_SRCDEVADDR
,
117 arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
,
119 NF_INVF(arpinfo
, ARPT_INV_TGTDEVADDR
,
120 arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
,
124 if (NF_INVF(arpinfo
, ARPT_INV_SRCIP
,
125 (src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
) ||
126 NF_INVF(arpinfo
, ARPT_INV_TGTIP
,
127 (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 (NF_INVF(arpinfo
, ARPT_INV_VIA_IN
, ret
!= 0))
136 ret
= ifname_compare(outdev
, arpinfo
->outiface
, arpinfo
->outiface_mask
);
138 if (NF_INVF(arpinfo
, ARPT_INV_VIA_OUT
, ret
!= 0))
144 static inline int arp_checkentry(const struct arpt_arp
*arp
)
146 if (arp
->flags
& ~ARPT_F_MASK
)
148 if (arp
->invflags
& ~ARPT_INV_MASK
)
155 arpt_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
157 net_err_ratelimited("arp_tables: error: '%s'\n",
158 (const char *)par
->targinfo
);
163 static inline const struct xt_entry_target
*
164 arpt_get_target_c(const struct arpt_entry
*e
)
166 return arpt_get_target((struct arpt_entry
*)e
);
169 static inline struct arpt_entry
*
170 get_entry(const void *base
, unsigned int offset
)
172 return (struct arpt_entry
*)(base
+ offset
);
176 struct arpt_entry
*arpt_next_entry(const struct arpt_entry
*entry
)
178 return (void *)entry
+ entry
->next_offset
;
181 unsigned int arpt_do_table(struct sk_buff
*skb
,
182 const struct nf_hook_state
*state
,
183 struct xt_table
*table
)
185 unsigned int hook
= state
->hook
;
186 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
187 unsigned int verdict
= NF_DROP
;
188 const struct arphdr
*arp
;
189 struct arpt_entry
*e
, **jumpstack
;
190 const char *indev
, *outdev
;
191 const void *table_base
;
192 unsigned int cpu
, stackidx
= 0;
193 const struct xt_table_info
*private;
194 struct xt_action_param acpar
;
197 if (!pskb_may_pull(skb
, arp_hdr_len(skb
->dev
)))
200 indev
= state
->in
? state
->in
->name
: nulldevname
;
201 outdev
= state
->out
? state
->out
->name
: nulldevname
;
204 addend
= xt_write_recseq_begin();
205 private = table
->private;
206 cpu
= smp_processor_id();
208 * Ensure we load private-> members after we've fetched the base
211 smp_read_barrier_depends();
212 table_base
= private->entries
;
213 jumpstack
= (struct arpt_entry
**)private->jumpstack
[cpu
];
215 /* No TEE support for arptables, so no need to switch to alternate
216 * stack. All targets that reenter must return absolute verdicts.
218 e
= get_entry(table_base
, private->hook_entry
[hook
]);
220 acpar
.net
= state
->net
;
221 acpar
.in
= state
->in
;
222 acpar
.out
= state
->out
;
223 acpar
.hooknum
= hook
;
224 acpar
.family
= NFPROTO_ARP
;
225 acpar
.hotdrop
= false;
229 const struct xt_entry_target
*t
;
230 struct xt_counters
*counter
;
232 if (!arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
233 e
= arpt_next_entry(e
);
237 counter
= xt_get_this_cpu_counter(&e
->counters
);
238 ADD_COUNTER(*counter
, arp_hdr_len(skb
->dev
), 1);
240 t
= arpt_get_target_c(e
);
242 /* Standard target? */
243 if (!t
->u
.kernel
.target
->target
) {
246 v
= ((struct xt_standard_target
*)t
)->verdict
;
248 /* Pop from stack? */
249 if (v
!= XT_RETURN
) {
250 verdict
= (unsigned int)(-v
) - 1;
254 e
= get_entry(table_base
,
255 private->underflow
[hook
]);
257 e
= jumpstack
[--stackidx
];
258 e
= arpt_next_entry(e
);
263 != arpt_next_entry(e
)) {
264 jumpstack
[stackidx
++] = e
;
267 e
= get_entry(table_base
, v
);
271 acpar
.target
= t
->u
.kernel
.target
;
272 acpar
.targinfo
= t
->data
;
273 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
275 /* Target might have changed stuff. */
278 if (verdict
== XT_CONTINUE
)
279 e
= arpt_next_entry(e
);
283 } while (!acpar
.hotdrop
);
284 xt_write_recseq_end(addend
);
293 /* All zeroes == unconditional rule. */
294 static inline bool unconditional(const struct arpt_entry
*e
)
296 static const struct arpt_arp uncond
;
298 return e
->target_offset
== sizeof(struct arpt_entry
) &&
299 memcmp(&e
->arp
, &uncond
, sizeof(uncond
)) == 0;
302 /* Figures out from what hook each rule can be called: returns 0 if
303 * there are loops. Puts hook bitmask in comefrom.
305 static int mark_source_chains(const struct xt_table_info
*newinfo
,
306 unsigned int valid_hooks
, void *entry0
,
307 unsigned int *offsets
)
311 /* No recursion; use packet counter to save back ptrs (reset
312 * to 0 as we leave), and comefrom to save source hook bitmask.
314 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
315 unsigned int pos
= newinfo
->hook_entry
[hook
];
317 = (struct arpt_entry
*)(entry0
+ pos
);
319 if (!(valid_hooks
& (1 << hook
)))
322 /* Set initial back pointer. */
323 e
->counters
.pcnt
= pos
;
326 const struct xt_standard_target
*t
327 = (void *)arpt_get_target_c(e
);
328 int visited
= e
->comefrom
& (1 << hook
);
330 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
))
334 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
336 /* Unconditional return/END. */
337 if ((unconditional(e
) &&
338 (strcmp(t
->target
.u
.user
.name
,
339 XT_STANDARD_TARGET
) == 0) &&
340 t
->verdict
< 0) || visited
) {
341 unsigned int oldpos
, size
;
343 if ((strcmp(t
->target
.u
.user
.name
,
344 XT_STANDARD_TARGET
) == 0) &&
345 t
->verdict
< -NF_MAX_VERDICT
- 1)
348 /* Return: backtrack through the last
352 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
354 pos
= e
->counters
.pcnt
;
355 e
->counters
.pcnt
= 0;
357 /* We're at the start. */
361 e
= (struct arpt_entry
*)
363 } while (oldpos
== pos
+ e
->next_offset
);
366 size
= e
->next_offset
;
367 e
= (struct arpt_entry
*)
368 (entry0
+ pos
+ size
);
369 if (pos
+ size
>= newinfo
->size
)
371 e
->counters
.pcnt
= pos
;
374 int newpos
= t
->verdict
;
376 if (strcmp(t
->target
.u
.user
.name
,
377 XT_STANDARD_TARGET
) == 0 &&
379 /* This a jump; chase it. */
380 if (!xt_find_jump_offset(offsets
, newpos
,
383 e
= (struct arpt_entry
*)
386 /* ... this is a fallthru */
387 newpos
= pos
+ e
->next_offset
;
388 if (newpos
>= newinfo
->size
)
391 e
= (struct arpt_entry
*)
393 e
->counters
.pcnt
= pos
;
402 static inline int check_target(struct arpt_entry
*e
, const char *name
)
404 struct xt_entry_target
*t
= arpt_get_target(e
);
405 struct xt_tgchk_param par
= {
408 .target
= t
->u
.kernel
.target
,
410 .hook_mask
= e
->comefrom
,
411 .family
= NFPROTO_ARP
,
414 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
), 0, false);
418 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
)
420 struct xt_entry_target
*t
;
421 struct xt_target
*target
;
425 pcnt
= xt_percpu_counter_alloc();
426 if (IS_ERR_VALUE(pcnt
))
428 e
->counters
.pcnt
= pcnt
;
430 t
= arpt_get_target(e
);
431 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
433 if (IS_ERR(target
)) {
434 ret
= PTR_ERR(target
);
437 t
->u
.kernel
.target
= target
;
439 ret
= check_target(e
, name
);
444 module_put(t
->u
.kernel
.target
->me
);
446 xt_percpu_counter_free(e
->counters
.pcnt
);
451 static bool check_underflow(const struct arpt_entry
*e
)
453 const struct xt_entry_target
*t
;
454 unsigned int verdict
;
456 if (!unconditional(e
))
458 t
= arpt_get_target_c(e
);
459 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
461 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
462 verdict
= -verdict
- 1;
463 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
466 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
467 struct xt_table_info
*newinfo
,
468 const unsigned char *base
,
469 const unsigned char *limit
,
470 const unsigned int *hook_entries
,
471 const unsigned int *underflows
,
472 unsigned int valid_hooks
)
477 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0 ||
478 (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
||
479 (unsigned char *)e
+ e
->next_offset
> limit
)
483 < sizeof(struct arpt_entry
) + sizeof(struct xt_entry_target
))
486 if (!arp_checkentry(&e
->arp
))
489 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
494 /* Check hooks & underflows */
495 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
496 if (!(valid_hooks
& (1 << h
)))
498 if ((unsigned char *)e
- base
== hook_entries
[h
])
499 newinfo
->hook_entry
[h
] = hook_entries
[h
];
500 if ((unsigned char *)e
- base
== underflows
[h
]) {
501 if (!check_underflow(e
))
504 newinfo
->underflow
[h
] = underflows
[h
];
508 /* Clear counters and comefrom */
509 e
->counters
= ((struct xt_counters
) { 0, 0 });
514 static inline void cleanup_entry(struct arpt_entry
*e
)
516 struct xt_tgdtor_param par
;
517 struct xt_entry_target
*t
;
519 t
= arpt_get_target(e
);
520 par
.target
= t
->u
.kernel
.target
;
521 par
.targinfo
= t
->data
;
522 par
.family
= NFPROTO_ARP
;
523 if (par
.target
->destroy
!= NULL
)
524 par
.target
->destroy(&par
);
525 module_put(par
.target
->me
);
526 xt_percpu_counter_free(e
->counters
.pcnt
);
529 /* Checks and translates the user-supplied table segment (held in
532 static int translate_table(struct xt_table_info
*newinfo
, void *entry0
,
533 const struct arpt_replace
*repl
)
535 struct arpt_entry
*iter
;
536 unsigned int *offsets
;
540 newinfo
->size
= repl
->size
;
541 newinfo
->number
= repl
->num_entries
;
543 /* Init all hooks to impossible value. */
544 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
545 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
546 newinfo
->underflow
[i
] = 0xFFFFFFFF;
549 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
554 /* Walk through entries, checking offsets. */
555 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
556 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
563 if (i
< repl
->num_entries
)
564 offsets
[i
] = (void *)iter
- entry0
;
566 if (strcmp(arpt_get_target(iter
)->u
.user
.name
,
567 XT_ERROR_TARGET
) == 0)
568 ++newinfo
->stacksize
;
574 if (i
!= repl
->num_entries
)
577 /* Check hooks all assigned */
578 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
579 /* Only hooks which are valid */
580 if (!(repl
->valid_hooks
& (1 << i
)))
582 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF)
584 if (newinfo
->underflow
[i
] == 0xFFFFFFFF)
588 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
594 /* Finally, each sanity check must pass */
596 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
597 ret
= find_check_entry(iter
, repl
->name
, repl
->size
);
604 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
618 static void get_counters(const struct xt_table_info
*t
,
619 struct xt_counters counters
[])
621 struct arpt_entry
*iter
;
625 for_each_possible_cpu(cpu
) {
626 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
629 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
630 struct xt_counters
*tmp
;
634 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
636 start
= read_seqcount_begin(s
);
639 } while (read_seqcount_retry(s
, start
));
641 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
647 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
649 unsigned int countersize
;
650 struct xt_counters
*counters
;
651 const struct xt_table_info
*private = table
->private;
653 /* We need atomic snapshot of counters: rest doesn't change
654 * (other than comefrom, which userspace doesn't care
657 countersize
= sizeof(struct xt_counters
) * private->number
;
658 counters
= vzalloc(countersize
);
660 if (counters
== NULL
)
661 return ERR_PTR(-ENOMEM
);
663 get_counters(private, counters
);
668 static int copy_entries_to_user(unsigned int total_size
,
669 const struct xt_table
*table
,
670 void __user
*userptr
)
672 unsigned int off
, num
;
673 const struct arpt_entry
*e
;
674 struct xt_counters
*counters
;
675 struct xt_table_info
*private = table
->private;
679 counters
= alloc_counters(table
);
680 if (IS_ERR(counters
))
681 return PTR_ERR(counters
);
683 loc_cpu_entry
= private->entries
;
684 /* ... then copy entire thing ... */
685 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
690 /* FIXME: use iterator macros --RR */
691 /* ... then go back and fix counters and names */
692 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
693 const struct xt_entry_target
*t
;
695 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
696 if (copy_to_user(userptr
+ off
697 + offsetof(struct arpt_entry
, counters
),
699 sizeof(counters
[num
])) != 0) {
704 t
= arpt_get_target_c(e
);
705 if (copy_to_user(userptr
+ off
+ e
->target_offset
706 + offsetof(struct xt_entry_target
,
708 t
->u
.kernel
.target
->name
,
709 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
721 static void compat_standard_from_user(void *dst
, const void *src
)
723 int v
= *(compat_int_t
*)src
;
726 v
+= xt_compat_calc_jump(NFPROTO_ARP
, v
);
727 memcpy(dst
, &v
, sizeof(v
));
730 static int compat_standard_to_user(void __user
*dst
, const void *src
)
732 compat_int_t cv
= *(int *)src
;
735 cv
-= xt_compat_calc_jump(NFPROTO_ARP
, cv
);
736 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
739 static int compat_calc_entry(const struct arpt_entry
*e
,
740 const struct xt_table_info
*info
,
741 const void *base
, struct xt_table_info
*newinfo
)
743 const struct xt_entry_target
*t
;
744 unsigned int entry_offset
;
747 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
748 entry_offset
= (void *)e
- base
;
750 t
= arpt_get_target_c(e
);
751 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
752 newinfo
->size
-= off
;
753 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
757 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
758 if (info
->hook_entry
[i
] &&
759 (e
< (struct arpt_entry
*)(base
+ info
->hook_entry
[i
])))
760 newinfo
->hook_entry
[i
] -= off
;
761 if (info
->underflow
[i
] &&
762 (e
< (struct arpt_entry
*)(base
+ info
->underflow
[i
])))
763 newinfo
->underflow
[i
] -= off
;
768 static int compat_table_info(const struct xt_table_info
*info
,
769 struct xt_table_info
*newinfo
)
771 struct arpt_entry
*iter
;
772 const void *loc_cpu_entry
;
775 if (!newinfo
|| !info
)
778 /* we dont care about newinfo->entries */
779 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
780 newinfo
->initial_entries
= 0;
781 loc_cpu_entry
= info
->entries
;
782 xt_compat_init_offsets(NFPROTO_ARP
, info
->number
);
783 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
784 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
792 static int get_info(struct net
*net
, void __user
*user
,
793 const int *len
, int compat
)
795 char name
[XT_TABLE_MAXNAMELEN
];
799 if (*len
!= sizeof(struct arpt_getinfo
))
802 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
805 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
808 xt_compat_lock(NFPROTO_ARP
);
810 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
811 "arptable_%s", name
);
812 if (!IS_ERR_OR_NULL(t
)) {
813 struct arpt_getinfo info
;
814 const struct xt_table_info
*private = t
->private;
816 struct xt_table_info tmp
;
819 ret
= compat_table_info(private, &tmp
);
820 xt_compat_flush_offsets(NFPROTO_ARP
);
824 memset(&info
, 0, sizeof(info
));
825 info
.valid_hooks
= t
->valid_hooks
;
826 memcpy(info
.hook_entry
, private->hook_entry
,
827 sizeof(info
.hook_entry
));
828 memcpy(info
.underflow
, private->underflow
,
829 sizeof(info
.underflow
));
830 info
.num_entries
= private->number
;
831 info
.size
= private->size
;
832 strcpy(info
.name
, name
);
834 if (copy_to_user(user
, &info
, *len
) != 0)
841 ret
= t
? PTR_ERR(t
) : -ENOENT
;
844 xt_compat_unlock(NFPROTO_ARP
);
849 static int get_entries(struct net
*net
, struct arpt_get_entries __user
*uptr
,
853 struct arpt_get_entries get
;
856 if (*len
< sizeof(get
))
858 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
860 if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
)
863 get
.name
[sizeof(get
.name
) - 1] = '\0';
865 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
866 if (!IS_ERR_OR_NULL(t
)) {
867 const struct xt_table_info
*private = t
->private;
869 if (get
.size
== private->size
)
870 ret
= copy_entries_to_user(private->size
,
871 t
, uptr
->entrytable
);
878 ret
= t
? PTR_ERR(t
) : -ENOENT
;
883 static int __do_replace(struct net
*net
, const char *name
,
884 unsigned int valid_hooks
,
885 struct xt_table_info
*newinfo
,
886 unsigned int num_counters
,
887 void __user
*counters_ptr
)
891 struct xt_table_info
*oldinfo
;
892 struct xt_counters
*counters
;
893 void *loc_cpu_old_entry
;
894 struct arpt_entry
*iter
;
897 counters
= vzalloc(num_counters
* sizeof(struct xt_counters
));
903 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
904 "arptable_%s", name
);
905 if (IS_ERR_OR_NULL(t
)) {
906 ret
= t
? PTR_ERR(t
) : -ENOENT
;
907 goto free_newinfo_counters_untrans
;
911 if (valid_hooks
!= t
->valid_hooks
) {
916 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
920 /* Update module usage count based on number of rules */
921 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
922 (newinfo
->number
<= oldinfo
->initial_entries
))
924 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
925 (newinfo
->number
<= oldinfo
->initial_entries
))
928 /* Get the old counters, and synchronize with replace */
929 get_counters(oldinfo
, counters
);
931 /* Decrease module usage counts and free resource */
932 loc_cpu_old_entry
= oldinfo
->entries
;
933 xt_entry_foreach(iter
, loc_cpu_old_entry
, oldinfo
->size
)
936 xt_free_table_info(oldinfo
);
937 if (copy_to_user(counters_ptr
, counters
,
938 sizeof(struct xt_counters
) * num_counters
) != 0) {
939 /* Silent error, can't fail, new table is already in place */
940 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
949 free_newinfo_counters_untrans
:
955 static int do_replace(struct net
*net
, const void __user
*user
,
959 struct arpt_replace tmp
;
960 struct xt_table_info
*newinfo
;
962 struct arpt_entry
*iter
;
964 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
968 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
970 if (tmp
.num_counters
== 0)
973 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
975 newinfo
= xt_alloc_table_info(tmp
.size
);
979 loc_cpu_entry
= newinfo
->entries
;
980 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
986 ret
= translate_table(newinfo
, loc_cpu_entry
, &tmp
);
990 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
991 tmp
.num_counters
, tmp
.counters
);
993 goto free_newinfo_untrans
;
996 free_newinfo_untrans
:
997 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1000 xt_free_table_info(newinfo
);
1004 static int do_add_counters(struct net
*net
, const void __user
*user
,
1005 unsigned int len
, int compat
)
1008 struct xt_counters_info tmp
;
1009 struct xt_counters
*paddc
;
1011 const struct xt_table_info
*private;
1013 struct arpt_entry
*iter
;
1014 unsigned int addend
;
1016 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1018 return PTR_ERR(paddc
);
1020 t
= xt_find_table_lock(net
, NFPROTO_ARP
, tmp
.name
);
1021 if (IS_ERR_OR_NULL(t
)) {
1022 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1027 private = t
->private;
1028 if (private->number
!= tmp
.num_counters
) {
1030 goto unlock_up_free
;
1035 addend
= xt_write_recseq_begin();
1036 xt_entry_foreach(iter
, private->entries
, private->size
) {
1037 struct xt_counters
*tmp
;
1039 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1040 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1043 xt_write_recseq_end(addend
);
1054 #ifdef CONFIG_COMPAT
1055 struct compat_arpt_replace
{
1056 char name
[XT_TABLE_MAXNAMELEN
];
1060 u32 hook_entry
[NF_ARP_NUMHOOKS
];
1061 u32 underflow
[NF_ARP_NUMHOOKS
];
1063 compat_uptr_t counters
;
1064 struct compat_arpt_entry entries
[0];
1067 static inline void compat_release_entry(struct compat_arpt_entry
*e
)
1069 struct xt_entry_target
*t
;
1071 t
= compat_arpt_get_target(e
);
1072 module_put(t
->u
.kernel
.target
->me
);
1076 check_compat_entry_size_and_hooks(struct compat_arpt_entry
*e
,
1077 struct xt_table_info
*newinfo
,
1079 const unsigned char *base
,
1080 const unsigned char *limit
)
1082 struct xt_entry_target
*t
;
1083 struct xt_target
*target
;
1084 unsigned int entry_offset
;
1087 if ((unsigned long)e
% __alignof__(struct compat_arpt_entry
) != 0 ||
1088 (unsigned char *)e
+ sizeof(struct compat_arpt_entry
) >= limit
||
1089 (unsigned char *)e
+ e
->next_offset
> limit
)
1092 if (e
->next_offset
< sizeof(struct compat_arpt_entry
) +
1093 sizeof(struct compat_xt_entry_target
))
1096 if (!arp_checkentry(&e
->arp
))
1099 ret
= xt_compat_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
1104 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1105 entry_offset
= (void *)e
- (void *)base
;
1107 t
= compat_arpt_get_target(e
);
1108 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
1109 t
->u
.user
.revision
);
1110 if (IS_ERR(target
)) {
1111 ret
= PTR_ERR(target
);
1114 t
->u
.kernel
.target
= target
;
1116 off
+= xt_compat_target_offset(target
);
1118 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
1120 goto release_target
;
1125 module_put(t
->u
.kernel
.target
->me
);
1131 compat_copy_entry_from_user(struct compat_arpt_entry
*e
, void **dstptr
,
1133 struct xt_table_info
*newinfo
, unsigned char *base
)
1135 struct xt_entry_target
*t
;
1136 struct xt_target
*target
;
1137 struct arpt_entry
*de
;
1138 unsigned int origsize
;
1142 de
= (struct arpt_entry
*)*dstptr
;
1143 memcpy(de
, e
, sizeof(struct arpt_entry
));
1144 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1146 *dstptr
+= sizeof(struct arpt_entry
);
1147 *size
+= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1149 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1150 t
= compat_arpt_get_target(e
);
1151 target
= t
->u
.kernel
.target
;
1152 xt_compat_target_from_user(t
, dstptr
, size
);
1154 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1155 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1156 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1157 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1158 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1159 newinfo
->underflow
[h
] -= origsize
- *size
;
1163 static int translate_compat_table(struct xt_table_info
**pinfo
,
1165 const struct compat_arpt_replace
*compatr
)
1168 struct xt_table_info
*newinfo
, *info
;
1169 void *pos
, *entry0
, *entry1
;
1170 struct compat_arpt_entry
*iter0
;
1171 struct arpt_replace repl
;
1177 size
= compatr
->size
;
1178 info
->number
= compatr
->num_entries
;
1181 xt_compat_lock(NFPROTO_ARP
);
1182 xt_compat_init_offsets(NFPROTO_ARP
, compatr
->num_entries
);
1183 /* Walk through entries, checking offsets. */
1184 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1185 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1187 entry0
+ compatr
->size
);
1194 if (j
!= compatr
->num_entries
)
1198 newinfo
= xt_alloc_table_info(size
);
1202 newinfo
->number
= compatr
->num_entries
;
1203 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1204 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1205 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1207 entry1
= newinfo
->entries
;
1209 size
= compatr
->size
;
1210 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1211 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1214 /* all module references in entry0 are now gone */
1216 xt_compat_flush_offsets(NFPROTO_ARP
);
1217 xt_compat_unlock(NFPROTO_ARP
);
1219 memcpy(&repl
, compatr
, sizeof(*compatr
));
1221 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1222 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1223 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1226 repl
.num_counters
= 0;
1227 repl
.counters
= NULL
;
1228 repl
.size
= newinfo
->size
;
1229 ret
= translate_table(newinfo
, entry1
, &repl
);
1235 xt_free_table_info(info
);
1239 xt_free_table_info(newinfo
);
1242 xt_compat_flush_offsets(NFPROTO_ARP
);
1243 xt_compat_unlock(NFPROTO_ARP
);
1244 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1247 compat_release_entry(iter0
);
1252 static int compat_do_replace(struct net
*net
, void __user
*user
,
1256 struct compat_arpt_replace tmp
;
1257 struct xt_table_info
*newinfo
;
1258 void *loc_cpu_entry
;
1259 struct arpt_entry
*iter
;
1261 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1264 /* overflow check */
1265 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1267 if (tmp
.num_counters
== 0)
1270 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1272 newinfo
= xt_alloc_table_info(tmp
.size
);
1276 loc_cpu_entry
= newinfo
->entries
;
1277 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
), tmp
.size
) != 0) {
1282 ret
= translate_compat_table(&newinfo
, &loc_cpu_entry
, &tmp
);
1286 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1287 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1289 goto free_newinfo_untrans
;
1292 free_newinfo_untrans
:
1293 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1294 cleanup_entry(iter
);
1296 xt_free_table_info(newinfo
);
1300 static int compat_do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1305 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1309 case ARPT_SO_SET_REPLACE
:
1310 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1313 case ARPT_SO_SET_ADD_COUNTERS
:
1314 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1324 static int compat_copy_entry_to_user(struct arpt_entry
*e
, void __user
**dstptr
,
1325 compat_uint_t
*size
,
1326 struct xt_counters
*counters
,
1329 struct xt_entry_target
*t
;
1330 struct compat_arpt_entry __user
*ce
;
1331 u_int16_t target_offset
, next_offset
;
1332 compat_uint_t origsize
;
1336 ce
= (struct compat_arpt_entry __user
*)*dstptr
;
1337 if (copy_to_user(ce
, e
, sizeof(struct arpt_entry
)) != 0 ||
1338 copy_to_user(&ce
->counters
, &counters
[i
],
1339 sizeof(counters
[i
])) != 0)
1342 *dstptr
+= sizeof(struct compat_arpt_entry
);
1343 *size
-= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1345 target_offset
= e
->target_offset
- (origsize
- *size
);
1347 t
= arpt_get_target(e
);
1348 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1351 next_offset
= e
->next_offset
- (origsize
- *size
);
1352 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1353 put_user(next_offset
, &ce
->next_offset
) != 0)
1358 static int compat_copy_entries_to_user(unsigned int total_size
,
1359 struct xt_table
*table
,
1360 void __user
*userptr
)
1362 struct xt_counters
*counters
;
1363 const struct xt_table_info
*private = table
->private;
1368 struct arpt_entry
*iter
;
1370 counters
= alloc_counters(table
);
1371 if (IS_ERR(counters
))
1372 return PTR_ERR(counters
);
1376 xt_entry_foreach(iter
, private->entries
, total_size
) {
1377 ret
= compat_copy_entry_to_user(iter
, &pos
,
1378 &size
, counters
, i
++);
1386 struct compat_arpt_get_entries
{
1387 char name
[XT_TABLE_MAXNAMELEN
];
1389 struct compat_arpt_entry entrytable
[0];
1392 static int compat_get_entries(struct net
*net
,
1393 struct compat_arpt_get_entries __user
*uptr
,
1397 struct compat_arpt_get_entries get
;
1400 if (*len
< sizeof(get
))
1402 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1404 if (*len
!= sizeof(struct compat_arpt_get_entries
) + get
.size
)
1407 get
.name
[sizeof(get
.name
) - 1] = '\0';
1409 xt_compat_lock(NFPROTO_ARP
);
1410 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1411 if (!IS_ERR_OR_NULL(t
)) {
1412 const struct xt_table_info
*private = t
->private;
1413 struct xt_table_info info
;
1415 ret
= compat_table_info(private, &info
);
1416 if (!ret
&& get
.size
== info
.size
) {
1417 ret
= compat_copy_entries_to_user(private->size
,
1418 t
, uptr
->entrytable
);
1422 xt_compat_flush_offsets(NFPROTO_ARP
);
1426 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1428 xt_compat_unlock(NFPROTO_ARP
);
1432 static int do_arpt_get_ctl(struct sock
*, int, void __user
*, int *);
1434 static int compat_do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1439 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1443 case ARPT_SO_GET_INFO
:
1444 ret
= get_info(sock_net(sk
), user
, len
, 1);
1446 case ARPT_SO_GET_ENTRIES
:
1447 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1450 ret
= do_arpt_get_ctl(sk
, cmd
, user
, len
);
1456 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1460 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1464 case ARPT_SO_SET_REPLACE
:
1465 ret
= do_replace(sock_net(sk
), user
, len
);
1468 case ARPT_SO_SET_ADD_COUNTERS
:
1469 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1479 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1483 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1487 case ARPT_SO_GET_INFO
:
1488 ret
= get_info(sock_net(sk
), user
, len
, 0);
1491 case ARPT_SO_GET_ENTRIES
:
1492 ret
= get_entries(sock_net(sk
), user
, len
);
1495 case ARPT_SO_GET_REVISION_TARGET
: {
1496 struct xt_get_revision rev
;
1498 if (*len
!= sizeof(rev
)) {
1502 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1506 rev
.name
[sizeof(rev
.name
)-1] = 0;
1508 try_then_request_module(xt_find_revision(NFPROTO_ARP
, rev
.name
,
1509 rev
.revision
, 1, &ret
),
1510 "arpt_%s", rev
.name
);
1521 static void __arpt_unregister_table(struct xt_table
*table
)
1523 struct xt_table_info
*private;
1524 void *loc_cpu_entry
;
1525 struct module
*table_owner
= table
->me
;
1526 struct arpt_entry
*iter
;
1528 private = xt_unregister_table(table
);
1530 /* Decrease module usage counts and free resources */
1531 loc_cpu_entry
= private->entries
;
1532 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1533 cleanup_entry(iter
);
1534 if (private->number
> private->initial_entries
)
1535 module_put(table_owner
);
1536 xt_free_table_info(private);
1539 int arpt_register_table(struct net
*net
,
1540 const struct xt_table
*table
,
1541 const struct arpt_replace
*repl
,
1542 const struct nf_hook_ops
*ops
,
1543 struct xt_table
**res
)
1546 struct xt_table_info
*newinfo
;
1547 struct xt_table_info bootstrap
= {0};
1548 void *loc_cpu_entry
;
1549 struct xt_table
*new_table
;
1551 newinfo
= xt_alloc_table_info(repl
->size
);
1555 loc_cpu_entry
= newinfo
->entries
;
1556 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1558 ret
= translate_table(newinfo
, loc_cpu_entry
, repl
);
1562 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1563 if (IS_ERR(new_table
)) {
1564 ret
= PTR_ERR(new_table
);
1568 /* set res now, will see skbs right after nf_register_net_hooks */
1569 WRITE_ONCE(*res
, new_table
);
1571 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1573 __arpt_unregister_table(new_table
);
1580 xt_free_table_info(newinfo
);
1584 void arpt_unregister_table(struct net
*net
, struct xt_table
*table
,
1585 const struct nf_hook_ops
*ops
)
1587 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1588 __arpt_unregister_table(table
);
1591 /* The built-in targets: standard (NULL) and error. */
1592 static struct xt_target arpt_builtin_tg
[] __read_mostly
= {
1594 .name
= XT_STANDARD_TARGET
,
1595 .targetsize
= sizeof(int),
1596 .family
= NFPROTO_ARP
,
1597 #ifdef CONFIG_COMPAT
1598 .compatsize
= sizeof(compat_int_t
),
1599 .compat_from_user
= compat_standard_from_user
,
1600 .compat_to_user
= compat_standard_to_user
,
1604 .name
= XT_ERROR_TARGET
,
1605 .target
= arpt_error
,
1606 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1607 .family
= NFPROTO_ARP
,
1611 static struct nf_sockopt_ops arpt_sockopts
= {
1613 .set_optmin
= ARPT_BASE_CTL
,
1614 .set_optmax
= ARPT_SO_SET_MAX
+1,
1615 .set
= do_arpt_set_ctl
,
1616 #ifdef CONFIG_COMPAT
1617 .compat_set
= compat_do_arpt_set_ctl
,
1619 .get_optmin
= ARPT_BASE_CTL
,
1620 .get_optmax
= ARPT_SO_GET_MAX
+1,
1621 .get
= do_arpt_get_ctl
,
1622 #ifdef CONFIG_COMPAT
1623 .compat_get
= compat_do_arpt_get_ctl
,
1625 .owner
= THIS_MODULE
,
1628 static int __net_init
arp_tables_net_init(struct net
*net
)
1630 return xt_proto_init(net
, NFPROTO_ARP
);
1633 static void __net_exit
arp_tables_net_exit(struct net
*net
)
1635 xt_proto_fini(net
, NFPROTO_ARP
);
1638 static struct pernet_operations arp_tables_net_ops
= {
1639 .init
= arp_tables_net_init
,
1640 .exit
= arp_tables_net_exit
,
1643 static int __init
arp_tables_init(void)
1647 ret
= register_pernet_subsys(&arp_tables_net_ops
);
1651 /* No one else will be downing sem now, so we won't sleep */
1652 ret
= xt_register_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1656 /* Register setsockopt */
1657 ret
= nf_register_sockopt(&arpt_sockopts
);
1661 pr_info("arp_tables: (C) 2002 David S. Miller\n");
1665 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1667 unregister_pernet_subsys(&arp_tables_net_ops
);
1672 static void __exit
arp_tables_fini(void)
1674 nf_unregister_sockopt(&arpt_sockopts
);
1675 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1676 unregister_pernet_subsys(&arp_tables_net_ops
);
1679 EXPORT_SYMBOL(arpt_register_table
);
1680 EXPORT_SYMBOL(arpt_unregister_table
);
1681 EXPORT_SYMBOL(arpt_do_table
);
1683 module_init(arp_tables_init
);
1684 module_exit(arp_tables_fini
);