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 <linux/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
]);
221 acpar
.hotdrop
= false;
225 const struct xt_entry_target
*t
;
226 struct xt_counters
*counter
;
228 if (!arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
229 e
= arpt_next_entry(e
);
233 counter
= xt_get_this_cpu_counter(&e
->counters
);
234 ADD_COUNTER(*counter
, arp_hdr_len(skb
->dev
), 1);
236 t
= arpt_get_target_c(e
);
238 /* Standard target? */
239 if (!t
->u
.kernel
.target
->target
) {
242 v
= ((struct xt_standard_target
*)t
)->verdict
;
244 /* Pop from stack? */
245 if (v
!= XT_RETURN
) {
246 verdict
= (unsigned int)(-v
) - 1;
250 e
= get_entry(table_base
,
251 private->underflow
[hook
]);
253 e
= jumpstack
[--stackidx
];
254 e
= arpt_next_entry(e
);
259 != arpt_next_entry(e
)) {
260 jumpstack
[stackidx
++] = e
;
263 e
= get_entry(table_base
, v
);
267 acpar
.target
= t
->u
.kernel
.target
;
268 acpar
.targinfo
= t
->data
;
269 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
271 /* Target might have changed stuff. */
274 if (verdict
== XT_CONTINUE
)
275 e
= arpt_next_entry(e
);
279 } while (!acpar
.hotdrop
);
280 xt_write_recseq_end(addend
);
289 /* All zeroes == unconditional rule. */
290 static inline bool unconditional(const struct arpt_entry
*e
)
292 static const struct arpt_arp uncond
;
294 return e
->target_offset
== sizeof(struct arpt_entry
) &&
295 memcmp(&e
->arp
, &uncond
, sizeof(uncond
)) == 0;
298 /* Figures out from what hook each rule can be called: returns 0 if
299 * there are loops. Puts hook bitmask in comefrom.
301 static int mark_source_chains(const struct xt_table_info
*newinfo
,
302 unsigned int valid_hooks
, void *entry0
,
303 unsigned int *offsets
)
307 /* No recursion; use packet counter to save back ptrs (reset
308 * to 0 as we leave), and comefrom to save source hook bitmask.
310 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
311 unsigned int pos
= newinfo
->hook_entry
[hook
];
312 struct arpt_entry
*e
= entry0
+ pos
;
314 if (!(valid_hooks
& (1 << hook
)))
317 /* Set initial back pointer. */
318 e
->counters
.pcnt
= pos
;
321 const struct xt_standard_target
*t
322 = (void *)arpt_get_target_c(e
);
323 int visited
= e
->comefrom
& (1 << hook
);
325 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
))
329 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
331 /* Unconditional return/END. */
332 if ((unconditional(e
) &&
333 (strcmp(t
->target
.u
.user
.name
,
334 XT_STANDARD_TARGET
) == 0) &&
335 t
->verdict
< 0) || visited
) {
336 unsigned int oldpos
, size
;
338 if ((strcmp(t
->target
.u
.user
.name
,
339 XT_STANDARD_TARGET
) == 0) &&
340 t
->verdict
< -NF_MAX_VERDICT
- 1)
343 /* Return: backtrack through the last
347 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
349 pos
= e
->counters
.pcnt
;
350 e
->counters
.pcnt
= 0;
352 /* We're at the start. */
357 } while (oldpos
== pos
+ e
->next_offset
);
360 size
= e
->next_offset
;
361 e
= entry0
+ pos
+ size
;
362 if (pos
+ size
>= newinfo
->size
)
364 e
->counters
.pcnt
= pos
;
367 int newpos
= t
->verdict
;
369 if (strcmp(t
->target
.u
.user
.name
,
370 XT_STANDARD_TARGET
) == 0 &&
372 /* This a jump; chase it. */
373 if (!xt_find_jump_offset(offsets
, newpos
,
378 /* ... this is a fallthru */
379 newpos
= pos
+ e
->next_offset
;
380 if (newpos
>= newinfo
->size
)
384 e
->counters
.pcnt
= pos
;
393 static inline int check_target(struct arpt_entry
*e
, const char *name
)
395 struct xt_entry_target
*t
= arpt_get_target(e
);
396 struct xt_tgchk_param par
= {
399 .target
= t
->u
.kernel
.target
,
401 .hook_mask
= e
->comefrom
,
402 .family
= NFPROTO_ARP
,
405 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
), 0, false);
409 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
,
410 struct xt_percpu_counter_alloc_state
*alloc_state
)
412 struct xt_entry_target
*t
;
413 struct xt_target
*target
;
416 if (!xt_percpu_counter_alloc(alloc_state
, &e
->counters
))
419 t
= arpt_get_target(e
);
420 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
422 if (IS_ERR(target
)) {
423 ret
= PTR_ERR(target
);
426 t
->u
.kernel
.target
= target
;
428 ret
= check_target(e
, name
);
433 module_put(t
->u
.kernel
.target
->me
);
435 xt_percpu_counter_free(&e
->counters
);
440 static bool check_underflow(const struct arpt_entry
*e
)
442 const struct xt_entry_target
*t
;
443 unsigned int verdict
;
445 if (!unconditional(e
))
447 t
= arpt_get_target_c(e
);
448 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
450 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
451 verdict
= -verdict
- 1;
452 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
455 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
456 struct xt_table_info
*newinfo
,
457 const unsigned char *base
,
458 const unsigned char *limit
,
459 const unsigned int *hook_entries
,
460 const unsigned int *underflows
,
461 unsigned int valid_hooks
)
466 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0 ||
467 (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
||
468 (unsigned char *)e
+ e
->next_offset
> limit
)
472 < sizeof(struct arpt_entry
) + sizeof(struct xt_entry_target
))
475 if (!arp_checkentry(&e
->arp
))
478 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
483 /* Check hooks & underflows */
484 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
485 if (!(valid_hooks
& (1 << h
)))
487 if ((unsigned char *)e
- base
== hook_entries
[h
])
488 newinfo
->hook_entry
[h
] = hook_entries
[h
];
489 if ((unsigned char *)e
- base
== underflows
[h
]) {
490 if (!check_underflow(e
))
493 newinfo
->underflow
[h
] = underflows
[h
];
497 /* Clear counters and comefrom */
498 e
->counters
= ((struct xt_counters
) { 0, 0 });
503 static inline void cleanup_entry(struct arpt_entry
*e
)
505 struct xt_tgdtor_param par
;
506 struct xt_entry_target
*t
;
508 t
= arpt_get_target(e
);
509 par
.target
= t
->u
.kernel
.target
;
510 par
.targinfo
= t
->data
;
511 par
.family
= NFPROTO_ARP
;
512 if (par
.target
->destroy
!= NULL
)
513 par
.target
->destroy(&par
);
514 module_put(par
.target
->me
);
515 xt_percpu_counter_free(&e
->counters
);
518 /* Checks and translates the user-supplied table segment (held in
521 static int translate_table(struct xt_table_info
*newinfo
, void *entry0
,
522 const struct arpt_replace
*repl
)
524 struct xt_percpu_counter_alloc_state alloc_state
= { 0 };
525 struct arpt_entry
*iter
;
526 unsigned int *offsets
;
530 newinfo
->size
= repl
->size
;
531 newinfo
->number
= repl
->num_entries
;
533 /* Init all hooks to impossible value. */
534 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
535 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
536 newinfo
->underflow
[i
] = 0xFFFFFFFF;
539 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
544 /* Walk through entries, checking offsets. */
545 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
546 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
553 if (i
< repl
->num_entries
)
554 offsets
[i
] = (void *)iter
- entry0
;
556 if (strcmp(arpt_get_target(iter
)->u
.user
.name
,
557 XT_ERROR_TARGET
) == 0)
558 ++newinfo
->stacksize
;
562 if (i
!= repl
->num_entries
)
565 /* Check hooks all assigned */
566 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
567 /* Only hooks which are valid */
568 if (!(repl
->valid_hooks
& (1 << i
)))
570 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF)
572 if (newinfo
->underflow
[i
] == 0xFFFFFFFF)
576 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
582 /* Finally, each sanity check must pass */
584 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
585 ret
= find_check_entry(iter
, repl
->name
, repl
->size
,
593 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
607 static void get_counters(const struct xt_table_info
*t
,
608 struct xt_counters counters
[])
610 struct arpt_entry
*iter
;
614 for_each_possible_cpu(cpu
) {
615 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
618 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
619 struct xt_counters
*tmp
;
623 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
625 start
= read_seqcount_begin(s
);
628 } while (read_seqcount_retry(s
, start
));
630 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
636 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
638 unsigned int countersize
;
639 struct xt_counters
*counters
;
640 const struct xt_table_info
*private = table
->private;
642 /* We need atomic snapshot of counters: rest doesn't change
643 * (other than comefrom, which userspace doesn't care
646 countersize
= sizeof(struct xt_counters
) * private->number
;
647 counters
= vzalloc(countersize
);
649 if (counters
== NULL
)
650 return ERR_PTR(-ENOMEM
);
652 get_counters(private, counters
);
657 static int copy_entries_to_user(unsigned int total_size
,
658 const struct xt_table
*table
,
659 void __user
*userptr
)
661 unsigned int off
, num
;
662 const struct arpt_entry
*e
;
663 struct xt_counters
*counters
;
664 struct xt_table_info
*private = table
->private;
668 counters
= alloc_counters(table
);
669 if (IS_ERR(counters
))
670 return PTR_ERR(counters
);
672 loc_cpu_entry
= private->entries
;
674 /* FIXME: use iterator macros --RR */
675 /* ... then go back and fix counters and names */
676 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
677 const struct xt_entry_target
*t
;
679 e
= loc_cpu_entry
+ off
;
680 if (copy_to_user(userptr
+ off
, e
, sizeof(*e
))) {
684 if (copy_to_user(userptr
+ off
685 + offsetof(struct arpt_entry
, counters
),
687 sizeof(counters
[num
])) != 0) {
692 t
= arpt_get_target_c(e
);
693 if (xt_target_to_user(t
, userptr
+ off
+ e
->target_offset
)) {
705 static void compat_standard_from_user(void *dst
, const void *src
)
707 int v
= *(compat_int_t
*)src
;
710 v
+= xt_compat_calc_jump(NFPROTO_ARP
, v
);
711 memcpy(dst
, &v
, sizeof(v
));
714 static int compat_standard_to_user(void __user
*dst
, const void *src
)
716 compat_int_t cv
= *(int *)src
;
719 cv
-= xt_compat_calc_jump(NFPROTO_ARP
, cv
);
720 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
723 static int compat_calc_entry(const struct arpt_entry
*e
,
724 const struct xt_table_info
*info
,
725 const void *base
, struct xt_table_info
*newinfo
)
727 const struct xt_entry_target
*t
;
728 unsigned int entry_offset
;
731 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
732 entry_offset
= (void *)e
- base
;
734 t
= arpt_get_target_c(e
);
735 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
736 newinfo
->size
-= off
;
737 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
741 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
742 if (info
->hook_entry
[i
] &&
743 (e
< (struct arpt_entry
*)(base
+ info
->hook_entry
[i
])))
744 newinfo
->hook_entry
[i
] -= off
;
745 if (info
->underflow
[i
] &&
746 (e
< (struct arpt_entry
*)(base
+ info
->underflow
[i
])))
747 newinfo
->underflow
[i
] -= off
;
752 static int compat_table_info(const struct xt_table_info
*info
,
753 struct xt_table_info
*newinfo
)
755 struct arpt_entry
*iter
;
756 const void *loc_cpu_entry
;
759 if (!newinfo
|| !info
)
762 /* we dont care about newinfo->entries */
763 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
764 newinfo
->initial_entries
= 0;
765 loc_cpu_entry
= info
->entries
;
766 xt_compat_init_offsets(NFPROTO_ARP
, info
->number
);
767 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
768 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
776 static int get_info(struct net
*net
, void __user
*user
,
777 const int *len
, int compat
)
779 char name
[XT_TABLE_MAXNAMELEN
];
783 if (*len
!= sizeof(struct arpt_getinfo
))
786 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
789 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
792 xt_compat_lock(NFPROTO_ARP
);
794 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
795 "arptable_%s", name
);
797 struct arpt_getinfo info
;
798 const struct xt_table_info
*private = t
->private;
800 struct xt_table_info tmp
;
803 ret
= compat_table_info(private, &tmp
);
804 xt_compat_flush_offsets(NFPROTO_ARP
);
808 memset(&info
, 0, sizeof(info
));
809 info
.valid_hooks
= t
->valid_hooks
;
810 memcpy(info
.hook_entry
, private->hook_entry
,
811 sizeof(info
.hook_entry
));
812 memcpy(info
.underflow
, private->underflow
,
813 sizeof(info
.underflow
));
814 info
.num_entries
= private->number
;
815 info
.size
= private->size
;
816 strcpy(info
.name
, name
);
818 if (copy_to_user(user
, &info
, *len
) != 0)
828 xt_compat_unlock(NFPROTO_ARP
);
833 static int get_entries(struct net
*net
, struct arpt_get_entries __user
*uptr
,
837 struct arpt_get_entries get
;
840 if (*len
< sizeof(get
))
842 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
844 if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
)
847 get
.name
[sizeof(get
.name
) - 1] = '\0';
849 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
851 const struct xt_table_info
*private = t
->private;
853 if (get
.size
== private->size
)
854 ret
= copy_entries_to_user(private->size
,
855 t
, uptr
->entrytable
);
867 static int __do_replace(struct net
*net
, const char *name
,
868 unsigned int valid_hooks
,
869 struct xt_table_info
*newinfo
,
870 unsigned int num_counters
,
871 void __user
*counters_ptr
)
875 struct xt_table_info
*oldinfo
;
876 struct xt_counters
*counters
;
877 void *loc_cpu_old_entry
;
878 struct arpt_entry
*iter
;
881 counters
= vzalloc(num_counters
* sizeof(struct xt_counters
));
887 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
888 "arptable_%s", name
);
891 goto free_newinfo_counters_untrans
;
895 if (valid_hooks
!= t
->valid_hooks
) {
900 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
904 /* Update module usage count based on number of rules */
905 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
906 (newinfo
->number
<= oldinfo
->initial_entries
))
908 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
909 (newinfo
->number
<= oldinfo
->initial_entries
))
912 /* Get the old counters, and synchronize with replace */
913 get_counters(oldinfo
, counters
);
915 /* Decrease module usage counts and free resource */
916 loc_cpu_old_entry
= oldinfo
->entries
;
917 xt_entry_foreach(iter
, loc_cpu_old_entry
, oldinfo
->size
)
920 xt_free_table_info(oldinfo
);
921 if (copy_to_user(counters_ptr
, counters
,
922 sizeof(struct xt_counters
) * num_counters
) != 0) {
923 /* Silent error, can't fail, new table is already in place */
924 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
933 free_newinfo_counters_untrans
:
939 static int do_replace(struct net
*net
, const void __user
*user
,
943 struct arpt_replace tmp
;
944 struct xt_table_info
*newinfo
;
946 struct arpt_entry
*iter
;
948 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
952 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
954 if (tmp
.num_counters
== 0)
957 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
959 newinfo
= xt_alloc_table_info(tmp
.size
);
963 loc_cpu_entry
= newinfo
->entries
;
964 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
970 ret
= translate_table(newinfo
, loc_cpu_entry
, &tmp
);
974 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
975 tmp
.num_counters
, tmp
.counters
);
977 goto free_newinfo_untrans
;
980 free_newinfo_untrans
:
981 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
984 xt_free_table_info(newinfo
);
988 static int do_add_counters(struct net
*net
, const void __user
*user
,
989 unsigned int len
, int compat
)
992 struct xt_counters_info tmp
;
993 struct xt_counters
*paddc
;
995 const struct xt_table_info
*private;
997 struct arpt_entry
*iter
;
1000 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1002 return PTR_ERR(paddc
);
1004 t
= xt_find_table_lock(net
, NFPROTO_ARP
, tmp
.name
);
1011 private = t
->private;
1012 if (private->number
!= tmp
.num_counters
) {
1014 goto unlock_up_free
;
1019 addend
= xt_write_recseq_begin();
1020 xt_entry_foreach(iter
, private->entries
, private->size
) {
1021 struct xt_counters
*tmp
;
1023 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1024 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1027 xt_write_recseq_end(addend
);
1038 #ifdef CONFIG_COMPAT
1039 struct compat_arpt_replace
{
1040 char name
[XT_TABLE_MAXNAMELEN
];
1044 u32 hook_entry
[NF_ARP_NUMHOOKS
];
1045 u32 underflow
[NF_ARP_NUMHOOKS
];
1047 compat_uptr_t counters
;
1048 struct compat_arpt_entry entries
[0];
1051 static inline void compat_release_entry(struct compat_arpt_entry
*e
)
1053 struct xt_entry_target
*t
;
1055 t
= compat_arpt_get_target(e
);
1056 module_put(t
->u
.kernel
.target
->me
);
1060 check_compat_entry_size_and_hooks(struct compat_arpt_entry
*e
,
1061 struct xt_table_info
*newinfo
,
1063 const unsigned char *base
,
1064 const unsigned char *limit
)
1066 struct xt_entry_target
*t
;
1067 struct xt_target
*target
;
1068 unsigned int entry_offset
;
1071 if ((unsigned long)e
% __alignof__(struct compat_arpt_entry
) != 0 ||
1072 (unsigned char *)e
+ sizeof(struct compat_arpt_entry
) >= limit
||
1073 (unsigned char *)e
+ e
->next_offset
> limit
)
1076 if (e
->next_offset
< sizeof(struct compat_arpt_entry
) +
1077 sizeof(struct compat_xt_entry_target
))
1080 if (!arp_checkentry(&e
->arp
))
1083 ret
= xt_compat_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
1088 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1089 entry_offset
= (void *)e
- (void *)base
;
1091 t
= compat_arpt_get_target(e
);
1092 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
1093 t
->u
.user
.revision
);
1094 if (IS_ERR(target
)) {
1095 ret
= PTR_ERR(target
);
1098 t
->u
.kernel
.target
= target
;
1100 off
+= xt_compat_target_offset(target
);
1102 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
1104 goto release_target
;
1109 module_put(t
->u
.kernel
.target
->me
);
1115 compat_copy_entry_from_user(struct compat_arpt_entry
*e
, void **dstptr
,
1117 struct xt_table_info
*newinfo
, unsigned char *base
)
1119 struct xt_entry_target
*t
;
1120 struct xt_target
*target
;
1121 struct arpt_entry
*de
;
1122 unsigned int origsize
;
1127 memcpy(de
, e
, sizeof(struct arpt_entry
));
1128 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1130 *dstptr
+= sizeof(struct arpt_entry
);
1131 *size
+= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1133 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1134 t
= compat_arpt_get_target(e
);
1135 target
= t
->u
.kernel
.target
;
1136 xt_compat_target_from_user(t
, dstptr
, size
);
1138 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1139 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1140 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1141 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1142 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1143 newinfo
->underflow
[h
] -= origsize
- *size
;
1147 static int translate_compat_table(struct xt_table_info
**pinfo
,
1149 const struct compat_arpt_replace
*compatr
)
1152 struct xt_table_info
*newinfo
, *info
;
1153 void *pos
, *entry0
, *entry1
;
1154 struct compat_arpt_entry
*iter0
;
1155 struct arpt_replace repl
;
1161 size
= compatr
->size
;
1162 info
->number
= compatr
->num_entries
;
1165 xt_compat_lock(NFPROTO_ARP
);
1166 xt_compat_init_offsets(NFPROTO_ARP
, compatr
->num_entries
);
1167 /* Walk through entries, checking offsets. */
1168 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1169 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1171 entry0
+ compatr
->size
);
1178 if (j
!= compatr
->num_entries
)
1182 newinfo
= xt_alloc_table_info(size
);
1186 newinfo
->number
= compatr
->num_entries
;
1187 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1188 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1189 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1191 entry1
= newinfo
->entries
;
1193 size
= compatr
->size
;
1194 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1195 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1198 /* all module references in entry0 are now gone */
1200 xt_compat_flush_offsets(NFPROTO_ARP
);
1201 xt_compat_unlock(NFPROTO_ARP
);
1203 memcpy(&repl
, compatr
, sizeof(*compatr
));
1205 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1206 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1207 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1210 repl
.num_counters
= 0;
1211 repl
.counters
= NULL
;
1212 repl
.size
= newinfo
->size
;
1213 ret
= translate_table(newinfo
, entry1
, &repl
);
1219 xt_free_table_info(info
);
1223 xt_free_table_info(newinfo
);
1226 xt_compat_flush_offsets(NFPROTO_ARP
);
1227 xt_compat_unlock(NFPROTO_ARP
);
1228 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1231 compat_release_entry(iter0
);
1236 static int compat_do_replace(struct net
*net
, void __user
*user
,
1240 struct compat_arpt_replace tmp
;
1241 struct xt_table_info
*newinfo
;
1242 void *loc_cpu_entry
;
1243 struct arpt_entry
*iter
;
1245 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1248 /* overflow check */
1249 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1251 if (tmp
.num_counters
== 0)
1254 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1256 newinfo
= xt_alloc_table_info(tmp
.size
);
1260 loc_cpu_entry
= newinfo
->entries
;
1261 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
), tmp
.size
) != 0) {
1266 ret
= translate_compat_table(&newinfo
, &loc_cpu_entry
, &tmp
);
1270 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1271 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1273 goto free_newinfo_untrans
;
1276 free_newinfo_untrans
:
1277 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1278 cleanup_entry(iter
);
1280 xt_free_table_info(newinfo
);
1284 static int compat_do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1289 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1293 case ARPT_SO_SET_REPLACE
:
1294 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1297 case ARPT_SO_SET_ADD_COUNTERS
:
1298 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1308 static int compat_copy_entry_to_user(struct arpt_entry
*e
, void __user
**dstptr
,
1309 compat_uint_t
*size
,
1310 struct xt_counters
*counters
,
1313 struct xt_entry_target
*t
;
1314 struct compat_arpt_entry __user
*ce
;
1315 u_int16_t target_offset
, next_offset
;
1316 compat_uint_t origsize
;
1321 if (copy_to_user(ce
, e
, sizeof(struct arpt_entry
)) != 0 ||
1322 copy_to_user(&ce
->counters
, &counters
[i
],
1323 sizeof(counters
[i
])) != 0)
1326 *dstptr
+= sizeof(struct compat_arpt_entry
);
1327 *size
-= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1329 target_offset
= e
->target_offset
- (origsize
- *size
);
1331 t
= arpt_get_target(e
);
1332 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1335 next_offset
= e
->next_offset
- (origsize
- *size
);
1336 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1337 put_user(next_offset
, &ce
->next_offset
) != 0)
1342 static int compat_copy_entries_to_user(unsigned int total_size
,
1343 struct xt_table
*table
,
1344 void __user
*userptr
)
1346 struct xt_counters
*counters
;
1347 const struct xt_table_info
*private = table
->private;
1352 struct arpt_entry
*iter
;
1354 counters
= alloc_counters(table
);
1355 if (IS_ERR(counters
))
1356 return PTR_ERR(counters
);
1360 xt_entry_foreach(iter
, private->entries
, total_size
) {
1361 ret
= compat_copy_entry_to_user(iter
, &pos
,
1362 &size
, counters
, i
++);
1370 struct compat_arpt_get_entries
{
1371 char name
[XT_TABLE_MAXNAMELEN
];
1373 struct compat_arpt_entry entrytable
[0];
1376 static int compat_get_entries(struct net
*net
,
1377 struct compat_arpt_get_entries __user
*uptr
,
1381 struct compat_arpt_get_entries get
;
1384 if (*len
< sizeof(get
))
1386 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1388 if (*len
!= sizeof(struct compat_arpt_get_entries
) + get
.size
)
1391 get
.name
[sizeof(get
.name
) - 1] = '\0';
1393 xt_compat_lock(NFPROTO_ARP
);
1394 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1396 const struct xt_table_info
*private = t
->private;
1397 struct xt_table_info info
;
1399 ret
= compat_table_info(private, &info
);
1400 if (!ret
&& get
.size
== info
.size
) {
1401 ret
= compat_copy_entries_to_user(private->size
,
1402 t
, uptr
->entrytable
);
1406 xt_compat_flush_offsets(NFPROTO_ARP
);
1412 xt_compat_unlock(NFPROTO_ARP
);
1416 static int do_arpt_get_ctl(struct sock
*, int, void __user
*, int *);
1418 static int compat_do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1423 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1427 case ARPT_SO_GET_INFO
:
1428 ret
= get_info(sock_net(sk
), user
, len
, 1);
1430 case ARPT_SO_GET_ENTRIES
:
1431 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1434 ret
= do_arpt_get_ctl(sk
, cmd
, user
, len
);
1440 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1444 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1448 case ARPT_SO_SET_REPLACE
:
1449 ret
= do_replace(sock_net(sk
), user
, len
);
1452 case ARPT_SO_SET_ADD_COUNTERS
:
1453 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1463 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1467 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1471 case ARPT_SO_GET_INFO
:
1472 ret
= get_info(sock_net(sk
), user
, len
, 0);
1475 case ARPT_SO_GET_ENTRIES
:
1476 ret
= get_entries(sock_net(sk
), user
, len
);
1479 case ARPT_SO_GET_REVISION_TARGET
: {
1480 struct xt_get_revision rev
;
1482 if (*len
!= sizeof(rev
)) {
1486 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1490 rev
.name
[sizeof(rev
.name
)-1] = 0;
1492 try_then_request_module(xt_find_revision(NFPROTO_ARP
, rev
.name
,
1493 rev
.revision
, 1, &ret
),
1494 "arpt_%s", rev
.name
);
1505 static void __arpt_unregister_table(struct xt_table
*table
)
1507 struct xt_table_info
*private;
1508 void *loc_cpu_entry
;
1509 struct module
*table_owner
= table
->me
;
1510 struct arpt_entry
*iter
;
1512 private = xt_unregister_table(table
);
1514 /* Decrease module usage counts and free resources */
1515 loc_cpu_entry
= private->entries
;
1516 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1517 cleanup_entry(iter
);
1518 if (private->number
> private->initial_entries
)
1519 module_put(table_owner
);
1520 xt_free_table_info(private);
1523 int arpt_register_table(struct net
*net
,
1524 const struct xt_table
*table
,
1525 const struct arpt_replace
*repl
,
1526 const struct nf_hook_ops
*ops
,
1527 struct xt_table
**res
)
1530 struct xt_table_info
*newinfo
;
1531 struct xt_table_info bootstrap
= {0};
1532 void *loc_cpu_entry
;
1533 struct xt_table
*new_table
;
1535 newinfo
= xt_alloc_table_info(repl
->size
);
1539 loc_cpu_entry
= newinfo
->entries
;
1540 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1542 ret
= translate_table(newinfo
, loc_cpu_entry
, repl
);
1546 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1547 if (IS_ERR(new_table
)) {
1548 ret
= PTR_ERR(new_table
);
1552 /* set res now, will see skbs right after nf_register_net_hooks */
1553 WRITE_ONCE(*res
, new_table
);
1555 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1557 __arpt_unregister_table(new_table
);
1564 xt_free_table_info(newinfo
);
1568 void arpt_unregister_table(struct net
*net
, struct xt_table
*table
,
1569 const struct nf_hook_ops
*ops
)
1571 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1572 __arpt_unregister_table(table
);
1575 /* The built-in targets: standard (NULL) and error. */
1576 static struct xt_target arpt_builtin_tg
[] __read_mostly
= {
1578 .name
= XT_STANDARD_TARGET
,
1579 .targetsize
= sizeof(int),
1580 .family
= NFPROTO_ARP
,
1581 #ifdef CONFIG_COMPAT
1582 .compatsize
= sizeof(compat_int_t
),
1583 .compat_from_user
= compat_standard_from_user
,
1584 .compat_to_user
= compat_standard_to_user
,
1588 .name
= XT_ERROR_TARGET
,
1589 .target
= arpt_error
,
1590 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1591 .family
= NFPROTO_ARP
,
1595 static struct nf_sockopt_ops arpt_sockopts
= {
1597 .set_optmin
= ARPT_BASE_CTL
,
1598 .set_optmax
= ARPT_SO_SET_MAX
+1,
1599 .set
= do_arpt_set_ctl
,
1600 #ifdef CONFIG_COMPAT
1601 .compat_set
= compat_do_arpt_set_ctl
,
1603 .get_optmin
= ARPT_BASE_CTL
,
1604 .get_optmax
= ARPT_SO_GET_MAX
+1,
1605 .get
= do_arpt_get_ctl
,
1606 #ifdef CONFIG_COMPAT
1607 .compat_get
= compat_do_arpt_get_ctl
,
1609 .owner
= THIS_MODULE
,
1612 static int __net_init
arp_tables_net_init(struct net
*net
)
1614 return xt_proto_init(net
, NFPROTO_ARP
);
1617 static void __net_exit
arp_tables_net_exit(struct net
*net
)
1619 xt_proto_fini(net
, NFPROTO_ARP
);
1622 static struct pernet_operations arp_tables_net_ops
= {
1623 .init
= arp_tables_net_init
,
1624 .exit
= arp_tables_net_exit
,
1627 static int __init
arp_tables_init(void)
1631 ret
= register_pernet_subsys(&arp_tables_net_ops
);
1635 /* No one else will be downing sem now, so we won't sleep */
1636 ret
= xt_register_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1640 /* Register setsockopt */
1641 ret
= nf_register_sockopt(&arpt_sockopts
);
1645 pr_info("arp_tables: (C) 2002 David S. Miller\n");
1649 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1651 unregister_pernet_subsys(&arp_tables_net_ops
);
1656 static void __exit
arp_tables_fini(void)
1658 nf_unregister_sockopt(&arpt_sockopts
);
1659 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1660 unregister_pernet_subsys(&arp_tables_net_ops
);
1663 EXPORT_SYMBOL(arpt_register_table
);
1664 EXPORT_SYMBOL(arpt_unregister_table
);
1665 EXPORT_SYMBOL(arpt_do_table
);
1667 module_init(arp_tables_init
);
1668 module_exit(arp_tables_fini
);