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)
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/netdevice.h>
15 #include <linux/capability.h>
16 #include <linux/if_arp.h>
17 #include <linux/kmod.h>
18 #include <linux/vmalloc.h>
19 #include <linux/proc_fs.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/mutex.h>
23 #include <linux/err.h>
24 #include <net/compat.h>
26 #include <asm/uaccess.h>
28 #include <linux/netfilter/x_tables.h>
29 #include <linux/netfilter_arp/arp_tables.h>
30 #include "../../netfilter/xt_repldata.h"
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
34 MODULE_DESCRIPTION("arptables core");
36 /*#define DEBUG_ARP_TABLES*/
37 /*#define DEBUG_ARP_TABLES_USER*/
39 #ifdef DEBUG_ARP_TABLES
40 #define dprintf(format, args...) printk(format , ## args)
42 #define dprintf(format, args...)
45 #ifdef DEBUG_ARP_TABLES_USER
46 #define duprintf(format, args...) printk(format , ## args)
48 #define duprintf(format, args...)
51 #ifdef CONFIG_NETFILTER_DEBUG
52 #define ARP_NF_ASSERT(x) \
55 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
56 __func__, __FILE__, __LINE__); \
59 #define ARP_NF_ASSERT(x)
62 void *arpt_alloc_initial_table(const struct xt_table
*info
)
64 return xt_alloc_initial_table(arpt
, ARPT
);
66 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table
);
68 static inline int arp_devaddr_compare(const struct arpt_devaddr_info
*ap
,
69 const char *hdr_addr
, int len
)
73 if (len
> ARPT_DEV_ADDR_LEN_MAX
)
74 len
= ARPT_DEV_ADDR_LEN_MAX
;
77 for (i
= 0; i
< len
; i
++)
78 ret
|= (hdr_addr
[i
] ^ ap
->addr
[i
]) & ap
->mask
[i
];
84 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
85 * Some arches dont care, unrolling the loop is a win on them.
86 * For other arches, we only have a 16bit alignement.
88 static unsigned long ifname_compare(const char *_a
, const char *_b
, const char *_mask
)
90 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
91 unsigned long ret
= ifname_compare_aligned(_a
, _b
, _mask
);
93 unsigned long ret
= 0;
94 const u16
*a
= (const u16
*)_a
;
95 const u16
*b
= (const u16
*)_b
;
96 const u16
*mask
= (const u16
*)_mask
;
99 for (i
= 0; i
< IFNAMSIZ
/sizeof(u16
); i
++)
100 ret
|= (a
[i
] ^ b
[i
]) & mask
[i
];
105 /* Returns whether packet matches rule or not. */
106 static inline int arp_packet_match(const struct arphdr
*arphdr
,
107 struct net_device
*dev
,
110 const struct arpt_arp
*arpinfo
)
112 const char *arpptr
= (char *)(arphdr
+ 1);
113 const char *src_devaddr
, *tgt_devaddr
;
114 __be32 src_ipaddr
, tgt_ipaddr
;
117 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
119 if (FWINV((arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
,
121 dprintf("ARP operation field mismatch.\n");
122 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
123 arphdr
->ar_op
, arpinfo
->arpop
, arpinfo
->arpop_mask
);
127 if (FWINV((arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
,
129 dprintf("ARP hardware address format mismatch.\n");
130 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
131 arphdr
->ar_hrd
, arpinfo
->arhrd
, arpinfo
->arhrd_mask
);
135 if (FWINV((arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
,
137 dprintf("ARP protocol address format mismatch.\n");
138 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
139 arphdr
->ar_pro
, arpinfo
->arpro
, arpinfo
->arpro_mask
);
143 if (FWINV((arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
,
145 dprintf("ARP hardware address length mismatch.\n");
146 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
147 arphdr
->ar_hln
, arpinfo
->arhln
, arpinfo
->arhln_mask
);
151 src_devaddr
= arpptr
;
152 arpptr
+= dev
->addr_len
;
153 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
154 arpptr
+= sizeof(u32
);
155 tgt_devaddr
= arpptr
;
156 arpptr
+= dev
->addr_len
;
157 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
159 if (FWINV(arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
, dev
->addr_len
),
160 ARPT_INV_SRCDEVADDR
) ||
161 FWINV(arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
, dev
->addr_len
),
162 ARPT_INV_TGTDEVADDR
)) {
163 dprintf("Source or target device address mismatch.\n");
168 if (FWINV((src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
,
170 FWINV(((tgt_ipaddr
& arpinfo
->tmsk
.s_addr
) != arpinfo
->tgt
.s_addr
),
172 dprintf("Source or target IP address mismatch.\n");
174 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
176 &arpinfo
->smsk
.s_addr
,
177 &arpinfo
->src
.s_addr
,
178 arpinfo
->invflags
& ARPT_INV_SRCIP
? " (INV)" : "");
179 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
181 &arpinfo
->tmsk
.s_addr
,
182 &arpinfo
->tgt
.s_addr
,
183 arpinfo
->invflags
& ARPT_INV_TGTIP
? " (INV)" : "");
187 /* Look for ifname matches. */
188 ret
= ifname_compare(indev
, arpinfo
->iniface
, arpinfo
->iniface_mask
);
190 if (FWINV(ret
!= 0, ARPT_INV_VIA_IN
)) {
191 dprintf("VIA in mismatch (%s vs %s).%s\n",
192 indev
, arpinfo
->iniface
,
193 arpinfo
->invflags
&ARPT_INV_VIA_IN
?" (INV)":"");
197 ret
= ifname_compare(outdev
, arpinfo
->outiface
, arpinfo
->outiface_mask
);
199 if (FWINV(ret
!= 0, ARPT_INV_VIA_OUT
)) {
200 dprintf("VIA out mismatch (%s vs %s).%s\n",
201 outdev
, arpinfo
->outiface
,
202 arpinfo
->invflags
&ARPT_INV_VIA_OUT
?" (INV)":"");
210 static inline int arp_checkentry(const struct arpt_arp
*arp
)
212 if (arp
->flags
& ~ARPT_F_MASK
) {
213 duprintf("Unknown flag bits set: %08X\n",
214 arp
->flags
& ~ARPT_F_MASK
);
217 if (arp
->invflags
& ~ARPT_INV_MASK
) {
218 duprintf("Unknown invflag bits set: %08X\n",
219 arp
->invflags
& ~ARPT_INV_MASK
);
227 arpt_error(struct sk_buff
*skb
, const struct xt_target_param
*par
)
230 printk("arp_tables: error: '%s'\n",
231 (const char *)par
->targinfo
);
236 static inline const struct arpt_entry_target
*
237 arpt_get_target_c(const struct arpt_entry
*e
)
239 return arpt_get_target((struct arpt_entry
*)e
);
242 static inline struct arpt_entry
*
243 get_entry(const void *base
, unsigned int offset
)
245 return (struct arpt_entry
*)(base
+ offset
);
249 struct arpt_entry
*arpt_next_entry(const struct arpt_entry
*entry
)
251 return (void *)entry
+ entry
->next_offset
;
254 unsigned int arpt_do_table(struct sk_buff
*skb
,
256 const struct net_device
*in
,
257 const struct net_device
*out
,
258 struct xt_table
*table
)
260 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
261 unsigned int verdict
= NF_DROP
;
262 const struct arphdr
*arp
;
263 bool hotdrop
= false;
264 struct arpt_entry
*e
, *back
;
265 const char *indev
, *outdev
;
267 const struct xt_table_info
*private;
268 struct xt_target_param tgpar
;
270 if (!pskb_may_pull(skb
, arp_hdr_len(skb
->dev
)))
273 indev
= in
? in
->name
: nulldevname
;
274 outdev
= out
? out
->name
: nulldevname
;
277 private = table
->private;
278 table_base
= private->entries
[smp_processor_id()];
280 e
= get_entry(table_base
, private->hook_entry
[hook
]);
281 back
= get_entry(table_base
, private->underflow
[hook
]);
285 tgpar
.hooknum
= hook
;
286 tgpar
.family
= NFPROTO_ARP
;
290 const struct arpt_entry_target
*t
;
293 if (!arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
294 e
= arpt_next_entry(e
);
298 hdr_len
= sizeof(*arp
) + (2 * sizeof(struct in_addr
)) +
299 (2 * skb
->dev
->addr_len
);
300 ADD_COUNTER(e
->counters
, hdr_len
, 1);
302 t
= arpt_get_target_c(e
);
304 /* Standard target? */
305 if (!t
->u
.kernel
.target
->target
) {
308 v
= ((struct arpt_standard_target
*)t
)->verdict
;
310 /* Pop from stack? */
311 if (v
!= ARPT_RETURN
) {
312 verdict
= (unsigned)(-v
) - 1;
316 back
= get_entry(table_base
, back
->comefrom
);
320 != arpt_next_entry(e
)) {
321 /* Save old back ptr in next entry */
322 struct arpt_entry
*next
= arpt_next_entry(e
);
323 next
->comefrom
= (void *)back
- table_base
;
325 /* set back pointer to next entry */
329 e
= get_entry(table_base
, v
);
333 /* Targets which reenter must return
336 tgpar
.target
= t
->u
.kernel
.target
;
337 tgpar
.targinfo
= t
->data
;
338 verdict
= t
->u
.kernel
.target
->target(skb
, &tgpar
);
340 /* Target might have changed stuff. */
343 if (verdict
== ARPT_CONTINUE
)
344 e
= arpt_next_entry(e
);
349 xt_info_rdunlock_bh();
357 /* All zeroes == unconditional rule. */
358 static inline bool unconditional(const struct arpt_arp
*arp
)
360 static const struct arpt_arp uncond
;
362 return memcmp(arp
, &uncond
, sizeof(uncond
)) == 0;
365 /* Figures out from what hook each rule can be called: returns 0 if
366 * there are loops. Puts hook bitmask in comefrom.
368 static int mark_source_chains(const struct xt_table_info
*newinfo
,
369 unsigned int valid_hooks
, void *entry0
)
373 /* No recursion; use packet counter to save back ptrs (reset
374 * to 0 as we leave), and comefrom to save source hook bitmask.
376 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
377 unsigned int pos
= newinfo
->hook_entry
[hook
];
379 = (struct arpt_entry
*)(entry0
+ pos
);
381 if (!(valid_hooks
& (1 << hook
)))
384 /* Set initial back pointer. */
385 e
->counters
.pcnt
= pos
;
388 const struct arpt_standard_target
*t
389 = (void *)arpt_get_target_c(e
);
390 int visited
= e
->comefrom
& (1 << hook
);
392 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
393 printk("arptables: loop hook %u pos %u %08X.\n",
394 hook
, pos
, e
->comefrom
);
398 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
400 /* Unconditional return/END. */
401 if ((e
->target_offset
== sizeof(struct arpt_entry
) &&
402 (strcmp(t
->target
.u
.user
.name
,
403 ARPT_STANDARD_TARGET
) == 0) &&
404 t
->verdict
< 0 && unconditional(&e
->arp
)) ||
406 unsigned int oldpos
, size
;
408 if ((strcmp(t
->target
.u
.user
.name
,
409 ARPT_STANDARD_TARGET
) == 0) &&
410 t
->verdict
< -NF_MAX_VERDICT
- 1) {
411 duprintf("mark_source_chains: bad "
412 "negative verdict (%i)\n",
417 /* Return: backtrack through the last
421 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
423 pos
= e
->counters
.pcnt
;
424 e
->counters
.pcnt
= 0;
426 /* We're at the start. */
430 e
= (struct arpt_entry
*)
432 } while (oldpos
== pos
+ e
->next_offset
);
435 size
= e
->next_offset
;
436 e
= (struct arpt_entry
*)
437 (entry0
+ pos
+ size
);
438 e
->counters
.pcnt
= pos
;
441 int newpos
= t
->verdict
;
443 if (strcmp(t
->target
.u
.user
.name
,
444 ARPT_STANDARD_TARGET
) == 0 &&
446 if (newpos
> newinfo
->size
-
447 sizeof(struct arpt_entry
)) {
448 duprintf("mark_source_chains: "
449 "bad verdict (%i)\n",
454 /* This a jump; chase it. */
455 duprintf("Jump rule %u -> %u\n",
458 /* ... this is a fallthru */
459 newpos
= pos
+ e
->next_offset
;
461 e
= (struct arpt_entry
*)
463 e
->counters
.pcnt
= pos
;
468 duprintf("Finished chain %u\n", hook
);
473 static inline int check_entry(const struct arpt_entry
*e
, const char *name
)
475 const struct arpt_entry_target
*t
;
477 if (!arp_checkentry(&e
->arp
)) {
478 duprintf("arp_tables: arp check failed %p %s.\n", e
, name
);
482 if (e
->target_offset
+ sizeof(struct arpt_entry_target
) > e
->next_offset
)
485 t
= arpt_get_target_c(e
);
486 if (e
->target_offset
+ t
->u
.target_size
> e
->next_offset
)
492 static inline int check_target(struct arpt_entry
*e
, const char *name
)
494 struct arpt_entry_target
*t
= arpt_get_target(e
);
496 struct xt_tgchk_param par
= {
499 .target
= t
->u
.kernel
.target
,
501 .hook_mask
= e
->comefrom
,
502 .family
= NFPROTO_ARP
,
505 ret
= xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
), 0, false);
507 duprintf("arp_tables: check failed for `%s'.\n",
508 t
->u
.kernel
.target
->name
);
515 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
,
518 struct arpt_entry_target
*t
;
519 struct xt_target
*target
;
522 ret
= check_entry(e
, name
);
526 t
= arpt_get_target(e
);
527 target
= try_then_request_module(xt_find_target(NFPROTO_ARP
,
530 "arpt_%s", t
->u
.user
.name
);
531 if (IS_ERR(target
) || !target
) {
532 duprintf("find_check_entry: `%s' not found\n", t
->u
.user
.name
);
533 ret
= target
? PTR_ERR(target
) : -ENOENT
;
536 t
->u
.kernel
.target
= target
;
538 ret
= check_target(e
, name
);
545 module_put(t
->u
.kernel
.target
->me
);
550 static bool check_underflow(const struct arpt_entry
*e
)
552 const struct arpt_entry_target
*t
;
553 unsigned int verdict
;
555 if (!unconditional(&e
->arp
))
557 t
= arpt_get_target_c(e
);
558 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
560 verdict
= ((struct arpt_standard_target
*)t
)->verdict
;
561 verdict
= -verdict
- 1;
562 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
565 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
566 struct xt_table_info
*newinfo
,
567 const unsigned char *base
,
568 const unsigned char *limit
,
569 const unsigned int *hook_entries
,
570 const unsigned int *underflows
,
571 unsigned int valid_hooks
,
576 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0 ||
577 (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
) {
578 duprintf("Bad offset %p\n", e
);
583 < sizeof(struct arpt_entry
) + sizeof(struct arpt_entry_target
)) {
584 duprintf("checking: element %p size %u\n",
589 /* Check hooks & underflows */
590 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
591 if (!(valid_hooks
& (1 << h
)))
593 if ((unsigned char *)e
- base
== hook_entries
[h
])
594 newinfo
->hook_entry
[h
] = hook_entries
[h
];
595 if ((unsigned char *)e
- base
== underflows
[h
]) {
596 if (!check_underflow(e
)) {
597 pr_err("Underflows must be unconditional and "
598 "use the STANDARD target with "
602 newinfo
->underflow
[h
] = underflows
[h
];
606 /* Clear counters and comefrom */
607 e
->counters
= ((struct xt_counters
) { 0, 0 });
614 static inline int cleanup_entry(struct arpt_entry
*e
, unsigned int *i
)
616 struct xt_tgdtor_param par
;
617 struct arpt_entry_target
*t
;
619 if (i
&& (*i
)-- == 0)
622 t
= arpt_get_target(e
);
623 par
.target
= t
->u
.kernel
.target
;
624 par
.targinfo
= t
->data
;
625 par
.family
= NFPROTO_ARP
;
626 if (par
.target
->destroy
!= NULL
)
627 par
.target
->destroy(&par
);
628 module_put(par
.target
->me
);
632 /* Checks and translates the user-supplied table segment (held in
635 static int translate_table(const char *name
,
636 unsigned int valid_hooks
,
637 struct xt_table_info
*newinfo
,
641 const unsigned int *hook_entries
,
642 const unsigned int *underflows
)
644 struct arpt_entry
*iter
;
648 newinfo
->size
= size
;
649 newinfo
->number
= number
;
651 /* Init all hooks to impossible value. */
652 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
653 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
654 newinfo
->underflow
[i
] = 0xFFFFFFFF;
657 duprintf("translate_table: size %u\n", newinfo
->size
);
660 /* Walk through entries, checking offsets. */
661 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
662 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
663 entry0
+ size
, hook_entries
, underflows
,
668 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
673 duprintf("translate_table: %u not %u entries\n",
678 /* Check hooks all assigned */
679 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
680 /* Only hooks which are valid */
681 if (!(valid_hooks
& (1 << i
)))
683 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
684 duprintf("Invalid hook entry %u %u\n",
688 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
689 duprintf("Invalid underflow %u %u\n",
695 if (!mark_source_chains(newinfo
, valid_hooks
, entry0
)) {
696 duprintf("Looping hook\n");
700 /* Finally, each sanity check must pass */
702 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
703 ret
= find_check_entry(iter
, name
, size
, &i
);
709 xt_entry_foreach(iter
, entry0
, newinfo
->size
)
710 if (cleanup_entry(iter
, &i
) != 0)
715 /* And one copy for every other CPU */
716 for_each_possible_cpu(i
) {
717 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry0
)
718 memcpy(newinfo
->entries
[i
], entry0
, newinfo
->size
);
725 static inline int add_entry_to_counter(const struct arpt_entry
*e
,
726 struct xt_counters total
[],
729 ADD_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
735 static inline int set_entry_to_counter(const struct arpt_entry
*e
,
736 struct xt_counters total
[],
739 SET_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
745 static void get_counters(const struct xt_table_info
*t
,
746 struct xt_counters counters
[])
748 struct arpt_entry
*iter
;
753 /* Instead of clearing (by a previous call to memset())
754 * the counters and using adds, we set the counters
755 * with data used by 'current' CPU
757 * Bottom half has to be disabled to prevent deadlock
758 * if new softirq were to run and call ipt_do_table
761 curcpu
= smp_processor_id();
764 xt_entry_foreach(iter
, t
->entries
[curcpu
], t
->size
)
765 if (set_entry_to_counter(iter
, counters
, &i
) != 0)
768 for_each_possible_cpu(cpu
) {
773 xt_entry_foreach(iter
, t
->entries
[cpu
], t
->size
)
774 if (add_entry_to_counter(iter
, counters
, &i
) != 0)
776 xt_info_wrunlock(cpu
);
781 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
783 unsigned int countersize
;
784 struct xt_counters
*counters
;
785 const struct xt_table_info
*private = table
->private;
787 /* We need atomic snapshot of counters: rest doesn't change
788 * (other than comefrom, which userspace doesn't care
791 countersize
= sizeof(struct xt_counters
) * private->number
;
792 counters
= vmalloc_node(countersize
, numa_node_id());
794 if (counters
== NULL
)
795 return ERR_PTR(-ENOMEM
);
797 get_counters(private, counters
);
802 static int copy_entries_to_user(unsigned int total_size
,
803 const struct xt_table
*table
,
804 void __user
*userptr
)
806 unsigned int off
, num
;
807 const struct arpt_entry
*e
;
808 struct xt_counters
*counters
;
809 struct xt_table_info
*private = table
->private;
813 counters
= alloc_counters(table
);
814 if (IS_ERR(counters
))
815 return PTR_ERR(counters
);
817 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
818 /* ... then copy entire thing ... */
819 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
824 /* FIXME: use iterator macros --RR */
825 /* ... then go back and fix counters and names */
826 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
827 const struct arpt_entry_target
*t
;
829 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
830 if (copy_to_user(userptr
+ off
831 + offsetof(struct arpt_entry
, counters
),
833 sizeof(counters
[num
])) != 0) {
838 t
= arpt_get_target_c(e
);
839 if (copy_to_user(userptr
+ off
+ e
->target_offset
840 + offsetof(struct arpt_entry_target
,
842 t
->u
.kernel
.target
->name
,
843 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
855 static void compat_standard_from_user(void *dst
, const void *src
)
857 int v
= *(compat_int_t
*)src
;
860 v
+= xt_compat_calc_jump(NFPROTO_ARP
, v
);
861 memcpy(dst
, &v
, sizeof(v
));
864 static int compat_standard_to_user(void __user
*dst
, const void *src
)
866 compat_int_t cv
= *(int *)src
;
869 cv
-= xt_compat_calc_jump(NFPROTO_ARP
, cv
);
870 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
873 static int compat_calc_entry(const struct arpt_entry
*e
,
874 const struct xt_table_info
*info
,
875 const void *base
, struct xt_table_info
*newinfo
)
877 const struct arpt_entry_target
*t
;
878 unsigned int entry_offset
;
881 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
882 entry_offset
= (void *)e
- base
;
884 t
= arpt_get_target_c(e
);
885 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
886 newinfo
->size
-= off
;
887 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
891 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
892 if (info
->hook_entry
[i
] &&
893 (e
< (struct arpt_entry
*)(base
+ info
->hook_entry
[i
])))
894 newinfo
->hook_entry
[i
] -= off
;
895 if (info
->underflow
[i
] &&
896 (e
< (struct arpt_entry
*)(base
+ info
->underflow
[i
])))
897 newinfo
->underflow
[i
] -= off
;
902 static int compat_table_info(const struct xt_table_info
*info
,
903 struct xt_table_info
*newinfo
)
905 struct arpt_entry
*iter
;
909 if (!newinfo
|| !info
)
912 /* we dont care about newinfo->entries[] */
913 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
914 newinfo
->initial_entries
= 0;
915 loc_cpu_entry
= info
->entries
[raw_smp_processor_id()];
916 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
917 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
925 static int get_info(struct net
*net
, void __user
*user
,
926 const int *len
, int compat
)
928 char name
[ARPT_TABLE_MAXNAMELEN
];
932 if (*len
!= sizeof(struct arpt_getinfo
)) {
933 duprintf("length %u != %Zu\n", *len
,
934 sizeof(struct arpt_getinfo
));
938 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
941 name
[ARPT_TABLE_MAXNAMELEN
-1] = '\0';
944 xt_compat_lock(NFPROTO_ARP
);
946 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
947 "arptable_%s", name
);
948 if (t
&& !IS_ERR(t
)) {
949 struct arpt_getinfo info
;
950 const struct xt_table_info
*private = t
->private;
952 struct xt_table_info tmp
;
955 ret
= compat_table_info(private, &tmp
);
956 xt_compat_flush_offsets(NFPROTO_ARP
);
960 info
.valid_hooks
= t
->valid_hooks
;
961 memcpy(info
.hook_entry
, private->hook_entry
,
962 sizeof(info
.hook_entry
));
963 memcpy(info
.underflow
, private->underflow
,
964 sizeof(info
.underflow
));
965 info
.num_entries
= private->number
;
966 info
.size
= private->size
;
967 strcpy(info
.name
, name
);
969 if (copy_to_user(user
, &info
, *len
) != 0)
976 ret
= t
? PTR_ERR(t
) : -ENOENT
;
979 xt_compat_unlock(NFPROTO_ARP
);
984 static int get_entries(struct net
*net
, struct arpt_get_entries __user
*uptr
,
988 struct arpt_get_entries get
;
991 if (*len
< sizeof(get
)) {
992 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
995 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
997 if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
998 duprintf("get_entries: %u != %Zu\n", *len
,
999 sizeof(struct arpt_get_entries
) + get
.size
);
1003 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1004 if (t
&& !IS_ERR(t
)) {
1005 const struct xt_table_info
*private = t
->private;
1007 duprintf("t->private->number = %u\n",
1009 if (get
.size
== private->size
)
1010 ret
= copy_entries_to_user(private->size
,
1011 t
, uptr
->entrytable
);
1013 duprintf("get_entries: I've got %u not %u!\n",
1014 private->size
, get
.size
);
1020 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1025 static int __do_replace(struct net
*net
, const char *name
,
1026 unsigned int valid_hooks
,
1027 struct xt_table_info
*newinfo
,
1028 unsigned int num_counters
,
1029 void __user
*counters_ptr
)
1033 struct xt_table_info
*oldinfo
;
1034 struct xt_counters
*counters
;
1035 void *loc_cpu_old_entry
;
1036 struct arpt_entry
*iter
;
1039 counters
= vmalloc_node(num_counters
* sizeof(struct xt_counters
),
1046 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
1047 "arptable_%s", name
);
1048 if (!t
|| IS_ERR(t
)) {
1049 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1050 goto free_newinfo_counters_untrans
;
1054 if (valid_hooks
!= t
->valid_hooks
) {
1055 duprintf("Valid hook crap: %08X vs %08X\n",
1056 valid_hooks
, t
->valid_hooks
);
1061 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
1065 /* Update module usage count based on number of rules */
1066 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1067 oldinfo
->number
, oldinfo
->initial_entries
, newinfo
->number
);
1068 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
1069 (newinfo
->number
<= oldinfo
->initial_entries
))
1071 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
1072 (newinfo
->number
<= oldinfo
->initial_entries
))
1075 /* Get the old counters, and synchronize with replace */
1076 get_counters(oldinfo
, counters
);
1078 /* Decrease module usage counts and free resource */
1079 loc_cpu_old_entry
= oldinfo
->entries
[raw_smp_processor_id()];
1080 xt_entry_foreach(iter
, loc_cpu_old_entry
, oldinfo
->size
)
1081 if (cleanup_entry(iter
, NULL
) != 0)
1084 xt_free_table_info(oldinfo
);
1085 if (copy_to_user(counters_ptr
, counters
,
1086 sizeof(struct xt_counters
) * num_counters
) != 0)
1095 free_newinfo_counters_untrans
:
1101 static int do_replace(struct net
*net
, const void __user
*user
,
1105 struct arpt_replace tmp
;
1106 struct xt_table_info
*newinfo
;
1107 void *loc_cpu_entry
;
1108 struct arpt_entry
*iter
;
1110 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1113 /* overflow check */
1114 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1117 newinfo
= xt_alloc_table_info(tmp
.size
);
1121 /* choose the copy that is on our node/cpu */
1122 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1123 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1129 ret
= translate_table(tmp
.name
, tmp
.valid_hooks
,
1130 newinfo
, loc_cpu_entry
, tmp
.size
, tmp
.num_entries
,
1131 tmp
.hook_entry
, tmp
.underflow
);
1135 duprintf("arp_tables: Translated table\n");
1137 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1138 tmp
.num_counters
, tmp
.counters
);
1140 goto free_newinfo_untrans
;
1143 free_newinfo_untrans
:
1144 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1145 if (cleanup_entry(iter
, NULL
) != 0)
1148 xt_free_table_info(newinfo
);
1152 /* We're lazy, and add to the first CPU; overflow works its fey magic
1153 * and everything is OK. */
1155 add_counter_to_entry(struct arpt_entry
*e
,
1156 const struct xt_counters addme
[],
1159 ADD_COUNTER(e
->counters
, addme
[*i
].bcnt
, addme
[*i
].pcnt
);
1165 static int do_add_counters(struct net
*net
, const void __user
*user
,
1166 unsigned int len
, int compat
)
1168 unsigned int i
, curcpu
;
1169 struct xt_counters_info tmp
;
1170 struct xt_counters
*paddc
;
1171 unsigned int num_counters
;
1176 const struct xt_table_info
*private;
1178 void *loc_cpu_entry
;
1179 struct arpt_entry
*iter
;
1180 #ifdef CONFIG_COMPAT
1181 struct compat_xt_counters_info compat_tmp
;
1185 size
= sizeof(struct compat_xt_counters_info
);
1190 size
= sizeof(struct xt_counters_info
);
1193 if (copy_from_user(ptmp
, user
, size
) != 0)
1196 #ifdef CONFIG_COMPAT
1198 num_counters
= compat_tmp
.num_counters
;
1199 name
= compat_tmp
.name
;
1203 num_counters
= tmp
.num_counters
;
1207 if (len
!= size
+ num_counters
* sizeof(struct xt_counters
))
1210 paddc
= vmalloc_node(len
- size
, numa_node_id());
1214 if (copy_from_user(paddc
, user
+ size
, len
- size
) != 0) {
1219 t
= xt_find_table_lock(net
, NFPROTO_ARP
, name
);
1220 if (!t
|| IS_ERR(t
)) {
1221 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1226 private = t
->private;
1227 if (private->number
!= num_counters
) {
1229 goto unlock_up_free
;
1233 /* Choose the copy that is on our node */
1234 curcpu
= smp_processor_id();
1235 loc_cpu_entry
= private->entries
[curcpu
];
1236 xt_info_wrlock(curcpu
);
1237 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1238 if (add_counter_to_entry(iter
, paddc
, &i
) != 0)
1240 xt_info_wrunlock(curcpu
);
1251 #ifdef CONFIG_COMPAT
1253 compat_release_entry(struct compat_arpt_entry
*e
, unsigned int *i
)
1255 struct arpt_entry_target
*t
;
1257 if (i
&& (*i
)-- == 0)
1260 t
= compat_arpt_get_target(e
);
1261 module_put(t
->u
.kernel
.target
->me
);
1266 check_compat_entry_size_and_hooks(struct compat_arpt_entry
*e
,
1267 struct xt_table_info
*newinfo
,
1269 const unsigned char *base
,
1270 const unsigned char *limit
,
1271 const unsigned int *hook_entries
,
1272 const unsigned int *underflows
,
1276 struct arpt_entry_target
*t
;
1277 struct xt_target
*target
;
1278 unsigned int entry_offset
;
1281 duprintf("check_compat_entry_size_and_hooks %p\n", e
);
1282 if ((unsigned long)e
% __alignof__(struct compat_arpt_entry
) != 0 ||
1283 (unsigned char *)e
+ sizeof(struct compat_arpt_entry
) >= limit
) {
1284 duprintf("Bad offset %p, limit = %p\n", e
, limit
);
1288 if (e
->next_offset
< sizeof(struct compat_arpt_entry
) +
1289 sizeof(struct compat_xt_entry_target
)) {
1290 duprintf("checking: element %p size %u\n",
1295 /* For purposes of check_entry casting the compat entry is fine */
1296 ret
= check_entry((struct arpt_entry
*)e
, name
);
1300 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1301 entry_offset
= (void *)e
- (void *)base
;
1303 t
= compat_arpt_get_target(e
);
1304 target
= try_then_request_module(xt_find_target(NFPROTO_ARP
,
1306 t
->u
.user
.revision
),
1307 "arpt_%s", t
->u
.user
.name
);
1308 if (IS_ERR(target
) || !target
) {
1309 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1311 ret
= target
? PTR_ERR(target
) : -ENOENT
;
1314 t
->u
.kernel
.target
= target
;
1316 off
+= xt_compat_target_offset(target
);
1318 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
1320 goto release_target
;
1322 /* Check hooks & underflows */
1323 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1324 if ((unsigned char *)e
- base
== hook_entries
[h
])
1325 newinfo
->hook_entry
[h
] = hook_entries
[h
];
1326 if ((unsigned char *)e
- base
== underflows
[h
])
1327 newinfo
->underflow
[h
] = underflows
[h
];
1330 /* Clear counters and comefrom */
1331 memset(&e
->counters
, 0, sizeof(e
->counters
));
1338 module_put(t
->u
.kernel
.target
->me
);
1344 compat_copy_entry_from_user(struct compat_arpt_entry
*e
, void **dstptr
,
1345 unsigned int *size
, const char *name
,
1346 struct xt_table_info
*newinfo
, unsigned char *base
)
1348 struct arpt_entry_target
*t
;
1349 struct xt_target
*target
;
1350 struct arpt_entry
*de
;
1351 unsigned int origsize
;
1356 de
= (struct arpt_entry
*)*dstptr
;
1357 memcpy(de
, e
, sizeof(struct arpt_entry
));
1358 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1360 *dstptr
+= sizeof(struct arpt_entry
);
1361 *size
+= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1363 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1364 t
= compat_arpt_get_target(e
);
1365 target
= t
->u
.kernel
.target
;
1366 xt_compat_target_from_user(t
, dstptr
, size
);
1368 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1369 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1370 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1371 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1372 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1373 newinfo
->underflow
[h
] -= origsize
- *size
;
1378 static inline int compat_check_entry(struct arpt_entry
*e
, const char *name
,
1383 ret
= check_target(e
, name
);
1391 static int translate_compat_table(const char *name
,
1392 unsigned int valid_hooks
,
1393 struct xt_table_info
**pinfo
,
1395 unsigned int total_size
,
1396 unsigned int number
,
1397 unsigned int *hook_entries
,
1398 unsigned int *underflows
)
1401 struct xt_table_info
*newinfo
, *info
;
1402 void *pos
, *entry0
, *entry1
;
1403 struct compat_arpt_entry
*iter0
;
1404 struct arpt_entry
*iter1
;
1411 info
->number
= number
;
1413 /* Init all hooks to impossible value. */
1414 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1415 info
->hook_entry
[i
] = 0xFFFFFFFF;
1416 info
->underflow
[i
] = 0xFFFFFFFF;
1419 duprintf("translate_compat_table: size %u\n", info
->size
);
1421 xt_compat_lock(NFPROTO_ARP
);
1422 /* Walk through entries, checking offsets. */
1423 xt_entry_foreach(iter0
, entry0
, total_size
) {
1424 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1425 entry0
, entry0
+ total_size
, hook_entries
, underflows
,
1435 duprintf("translate_compat_table: %u not %u entries\n",
1440 /* Check hooks all assigned */
1441 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1442 /* Only hooks which are valid */
1443 if (!(valid_hooks
& (1 << i
)))
1445 if (info
->hook_entry
[i
] == 0xFFFFFFFF) {
1446 duprintf("Invalid hook entry %u %u\n",
1447 i
, hook_entries
[i
]);
1450 if (info
->underflow
[i
] == 0xFFFFFFFF) {
1451 duprintf("Invalid underflow %u %u\n",
1458 newinfo
= xt_alloc_table_info(size
);
1462 newinfo
->number
= number
;
1463 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1464 newinfo
->hook_entry
[i
] = info
->hook_entry
[i
];
1465 newinfo
->underflow
[i
] = info
->underflow
[i
];
1467 entry1
= newinfo
->entries
[raw_smp_processor_id()];
1470 xt_entry_foreach(iter0
, entry0
, total_size
) {
1471 ret
= compat_copy_entry_from_user(iter0
, &pos
,
1472 &size
, name
, newinfo
, entry1
);
1476 xt_compat_flush_offsets(NFPROTO_ARP
);
1477 xt_compat_unlock(NFPROTO_ARP
);
1482 if (!mark_source_chains(newinfo
, valid_hooks
, entry1
))
1486 xt_entry_foreach(iter1
, entry1
, newinfo
->size
) {
1487 ret
= compat_check_entry(iter1
, name
, &i
);
1493 * The first i matches need cleanup_entry (calls ->destroy)
1494 * because they had called ->check already. The other j-i
1495 * entries need only release.
1499 xt_entry_foreach(iter0
, entry0
, newinfo
->size
) {
1502 if (compat_release_entry(iter0
, &j
) != 0)
1505 xt_entry_foreach(iter1
, entry1
, newinfo
->size
)
1506 if (cleanup_entry(iter1
, &i
) != 0)
1508 xt_free_table_info(newinfo
);
1512 /* And one copy for every other CPU */
1513 for_each_possible_cpu(i
)
1514 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry1
)
1515 memcpy(newinfo
->entries
[i
], entry1
, newinfo
->size
);
1519 xt_free_table_info(info
);
1523 xt_free_table_info(newinfo
);
1525 xt_entry_foreach(iter0
, entry0
, total_size
)
1526 if (compat_release_entry(iter0
, &j
) != 0)
1530 xt_compat_flush_offsets(NFPROTO_ARP
);
1531 xt_compat_unlock(NFPROTO_ARP
);
1535 struct compat_arpt_replace
{
1536 char name
[ARPT_TABLE_MAXNAMELEN
];
1540 u32 hook_entry
[NF_ARP_NUMHOOKS
];
1541 u32 underflow
[NF_ARP_NUMHOOKS
];
1543 compat_uptr_t counters
;
1544 struct compat_arpt_entry entries
[0];
1547 static int compat_do_replace(struct net
*net
, void __user
*user
,
1551 struct compat_arpt_replace tmp
;
1552 struct xt_table_info
*newinfo
;
1553 void *loc_cpu_entry
;
1554 struct arpt_entry
*iter
;
1556 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1559 /* overflow check */
1560 if (tmp
.size
>= INT_MAX
/ num_possible_cpus())
1562 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1565 newinfo
= xt_alloc_table_info(tmp
.size
);
1569 /* choose the copy that is on our node/cpu */
1570 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1571 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
), tmp
.size
) != 0) {
1576 ret
= translate_compat_table(tmp
.name
, tmp
.valid_hooks
,
1577 &newinfo
, &loc_cpu_entry
, tmp
.size
,
1578 tmp
.num_entries
, tmp
.hook_entry
,
1583 duprintf("compat_do_replace: Translated table\n");
1585 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1586 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1588 goto free_newinfo_untrans
;
1591 free_newinfo_untrans
:
1592 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1593 if (cleanup_entry(iter
, NULL
) != 0)
1596 xt_free_table_info(newinfo
);
1600 static int compat_do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1605 if (!capable(CAP_NET_ADMIN
))
1609 case ARPT_SO_SET_REPLACE
:
1610 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1613 case ARPT_SO_SET_ADD_COUNTERS
:
1614 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1618 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1625 static int compat_copy_entry_to_user(struct arpt_entry
*e
, void __user
**dstptr
,
1626 compat_uint_t
*size
,
1627 struct xt_counters
*counters
,
1630 struct arpt_entry_target
*t
;
1631 struct compat_arpt_entry __user
*ce
;
1632 u_int16_t target_offset
, next_offset
;
1633 compat_uint_t origsize
;
1638 ce
= (struct compat_arpt_entry __user
*)*dstptr
;
1639 if (copy_to_user(ce
, e
, sizeof(struct arpt_entry
)))
1642 if (copy_to_user(&ce
->counters
, &counters
[*i
], sizeof(counters
[*i
])))
1645 *dstptr
+= sizeof(struct compat_arpt_entry
);
1646 *size
-= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1648 target_offset
= e
->target_offset
- (origsize
- *size
);
1650 t
= arpt_get_target(e
);
1651 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1655 next_offset
= e
->next_offset
- (origsize
- *size
);
1656 if (put_user(target_offset
, &ce
->target_offset
))
1658 if (put_user(next_offset
, &ce
->next_offset
))
1667 static int compat_copy_entries_to_user(unsigned int total_size
,
1668 struct xt_table
*table
,
1669 void __user
*userptr
)
1671 struct xt_counters
*counters
;
1672 const struct xt_table_info
*private = table
->private;
1676 void *loc_cpu_entry
;
1678 struct arpt_entry
*iter
;
1680 counters
= alloc_counters(table
);
1681 if (IS_ERR(counters
))
1682 return PTR_ERR(counters
);
1684 /* choose the copy on our node/cpu */
1685 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1688 xt_entry_foreach(iter
, loc_cpu_entry
, total_size
) {
1689 ret
= compat_copy_entry_to_user(iter
, &pos
,
1690 &size
, counters
, &i
);
1698 struct compat_arpt_get_entries
{
1699 char name
[ARPT_TABLE_MAXNAMELEN
];
1701 struct compat_arpt_entry entrytable
[0];
1704 static int compat_get_entries(struct net
*net
,
1705 struct compat_arpt_get_entries __user
*uptr
,
1709 struct compat_arpt_get_entries get
;
1712 if (*len
< sizeof(get
)) {
1713 duprintf("compat_get_entries: %u < %zu\n", *len
, sizeof(get
));
1716 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1718 if (*len
!= sizeof(struct compat_arpt_get_entries
) + get
.size
) {
1719 duprintf("compat_get_entries: %u != %zu\n",
1720 *len
, sizeof(get
) + get
.size
);
1724 xt_compat_lock(NFPROTO_ARP
);
1725 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1726 if (t
&& !IS_ERR(t
)) {
1727 const struct xt_table_info
*private = t
->private;
1728 struct xt_table_info info
;
1730 duprintf("t->private->number = %u\n", private->number
);
1731 ret
= compat_table_info(private, &info
);
1732 if (!ret
&& get
.size
== info
.size
) {
1733 ret
= compat_copy_entries_to_user(private->size
,
1734 t
, uptr
->entrytable
);
1736 duprintf("compat_get_entries: I've got %u not %u!\n",
1737 private->size
, get
.size
);
1740 xt_compat_flush_offsets(NFPROTO_ARP
);
1744 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1746 xt_compat_unlock(NFPROTO_ARP
);
1750 static int do_arpt_get_ctl(struct sock
*, int, void __user
*, int *);
1752 static int compat_do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1757 if (!capable(CAP_NET_ADMIN
))
1761 case ARPT_SO_GET_INFO
:
1762 ret
= get_info(sock_net(sk
), user
, len
, 1);
1764 case ARPT_SO_GET_ENTRIES
:
1765 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1768 ret
= do_arpt_get_ctl(sk
, cmd
, user
, len
);
1774 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1778 if (!capable(CAP_NET_ADMIN
))
1782 case ARPT_SO_SET_REPLACE
:
1783 ret
= do_replace(sock_net(sk
), user
, len
);
1786 case ARPT_SO_SET_ADD_COUNTERS
:
1787 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1791 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1798 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1802 if (!capable(CAP_NET_ADMIN
))
1806 case ARPT_SO_GET_INFO
:
1807 ret
= get_info(sock_net(sk
), user
, len
, 0);
1810 case ARPT_SO_GET_ENTRIES
:
1811 ret
= get_entries(sock_net(sk
), user
, len
);
1814 case ARPT_SO_GET_REVISION_TARGET
: {
1815 struct xt_get_revision rev
;
1817 if (*len
!= sizeof(rev
)) {
1821 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1826 try_then_request_module(xt_find_revision(NFPROTO_ARP
, rev
.name
,
1827 rev
.revision
, 1, &ret
),
1828 "arpt_%s", rev
.name
);
1833 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1840 struct xt_table
*arpt_register_table(struct net
*net
,
1841 const struct xt_table
*table
,
1842 const struct arpt_replace
*repl
)
1845 struct xt_table_info
*newinfo
;
1846 struct xt_table_info bootstrap
1847 = { 0, 0, 0, { 0 }, { 0 }, { } };
1848 void *loc_cpu_entry
;
1849 struct xt_table
*new_table
;
1851 newinfo
= xt_alloc_table_info(repl
->size
);
1857 /* choose the copy on our node/cpu */
1858 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1859 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1861 ret
= translate_table(table
->name
, table
->valid_hooks
,
1862 newinfo
, loc_cpu_entry
, repl
->size
,
1867 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1871 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1872 if (IS_ERR(new_table
)) {
1873 ret
= PTR_ERR(new_table
);
1879 xt_free_table_info(newinfo
);
1881 return ERR_PTR(ret
);
1884 void arpt_unregister_table(struct xt_table
*table
)
1886 struct xt_table_info
*private;
1887 void *loc_cpu_entry
;
1888 struct module
*table_owner
= table
->me
;
1889 struct arpt_entry
*iter
;
1891 private = xt_unregister_table(table
);
1893 /* Decrease module usage counts and free resources */
1894 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1895 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1896 if (cleanup_entry(iter
, NULL
) != 0)
1898 if (private->number
> private->initial_entries
)
1899 module_put(table_owner
);
1900 xt_free_table_info(private);
1903 /* The built-in targets: standard (NULL) and error. */
1904 static struct xt_target arpt_standard_target __read_mostly
= {
1905 .name
= ARPT_STANDARD_TARGET
,
1906 .targetsize
= sizeof(int),
1907 .family
= NFPROTO_ARP
,
1908 #ifdef CONFIG_COMPAT
1909 .compatsize
= sizeof(compat_int_t
),
1910 .compat_from_user
= compat_standard_from_user
,
1911 .compat_to_user
= compat_standard_to_user
,
1915 static struct xt_target arpt_error_target __read_mostly
= {
1916 .name
= ARPT_ERROR_TARGET
,
1917 .target
= arpt_error
,
1918 .targetsize
= ARPT_FUNCTION_MAXNAMELEN
,
1919 .family
= NFPROTO_ARP
,
1922 static struct nf_sockopt_ops arpt_sockopts
= {
1924 .set_optmin
= ARPT_BASE_CTL
,
1925 .set_optmax
= ARPT_SO_SET_MAX
+1,
1926 .set
= do_arpt_set_ctl
,
1927 #ifdef CONFIG_COMPAT
1928 .compat_set
= compat_do_arpt_set_ctl
,
1930 .get_optmin
= ARPT_BASE_CTL
,
1931 .get_optmax
= ARPT_SO_GET_MAX
+1,
1932 .get
= do_arpt_get_ctl
,
1933 #ifdef CONFIG_COMPAT
1934 .compat_get
= compat_do_arpt_get_ctl
,
1936 .owner
= THIS_MODULE
,
1939 static int __net_init
arp_tables_net_init(struct net
*net
)
1941 return xt_proto_init(net
, NFPROTO_ARP
);
1944 static void __net_exit
arp_tables_net_exit(struct net
*net
)
1946 xt_proto_fini(net
, NFPROTO_ARP
);
1949 static struct pernet_operations arp_tables_net_ops
= {
1950 .init
= arp_tables_net_init
,
1951 .exit
= arp_tables_net_exit
,
1954 static int __init
arp_tables_init(void)
1958 ret
= register_pernet_subsys(&arp_tables_net_ops
);
1962 /* Noone else will be downing sem now, so we won't sleep */
1963 ret
= xt_register_target(&arpt_standard_target
);
1966 ret
= xt_register_target(&arpt_error_target
);
1970 /* Register setsockopt */
1971 ret
= nf_register_sockopt(&arpt_sockopts
);
1975 printk(KERN_INFO
"arp_tables: (C) 2002 David S. Miller\n");
1979 xt_unregister_target(&arpt_error_target
);
1981 xt_unregister_target(&arpt_standard_target
);
1983 unregister_pernet_subsys(&arp_tables_net_ops
);
1988 static void __exit
arp_tables_fini(void)
1990 nf_unregister_sockopt(&arpt_sockopts
);
1991 xt_unregister_target(&arpt_error_target
);
1992 xt_unregister_target(&arpt_standard_target
);
1993 unregister_pernet_subsys(&arp_tables_net_ops
);
1996 EXPORT_SYMBOL(arpt_register_table
);
1997 EXPORT_SYMBOL(arpt_unregister_table
);
1998 EXPORT_SYMBOL(arpt_do_table
);
2000 module_init(arp_tables_init
);
2001 module_exit(arp_tables_fini
);