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) WARN_ON(!(x))
54 #define ARP_NF_ASSERT(x)
57 void *arpt_alloc_initial_table(const struct xt_table
*info
)
59 return xt_alloc_initial_table(arpt
, ARPT
);
61 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table
);
63 static inline int arp_devaddr_compare(const struct arpt_devaddr_info
*ap
,
64 const char *hdr_addr
, int len
)
68 if (len
> ARPT_DEV_ADDR_LEN_MAX
)
69 len
= ARPT_DEV_ADDR_LEN_MAX
;
72 for (i
= 0; i
< len
; i
++)
73 ret
|= (hdr_addr
[i
] ^ ap
->addr
[i
]) & ap
->mask
[i
];
79 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
80 * Some arches dont care, unrolling the loop is a win on them.
81 * For other arches, we only have a 16bit alignement.
83 static unsigned long ifname_compare(const char *_a
, const char *_b
, const char *_mask
)
85 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
86 unsigned long ret
= ifname_compare_aligned(_a
, _b
, _mask
);
88 unsigned long ret
= 0;
89 const u16
*a
= (const u16
*)_a
;
90 const u16
*b
= (const u16
*)_b
;
91 const u16
*mask
= (const u16
*)_mask
;
94 for (i
= 0; i
< IFNAMSIZ
/sizeof(u16
); i
++)
95 ret
|= (a
[i
] ^ b
[i
]) & mask
[i
];
100 /* Returns whether packet matches rule or not. */
101 static inline int arp_packet_match(const struct arphdr
*arphdr
,
102 struct net_device
*dev
,
105 const struct arpt_arp
*arpinfo
)
107 const char *arpptr
= (char *)(arphdr
+ 1);
108 const char *src_devaddr
, *tgt_devaddr
;
109 __be32 src_ipaddr
, tgt_ipaddr
;
112 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
114 if (FWINV((arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
,
116 dprintf("ARP operation field mismatch.\n");
117 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
118 arphdr
->ar_op
, arpinfo
->arpop
, arpinfo
->arpop_mask
);
122 if (FWINV((arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
,
124 dprintf("ARP hardware address format mismatch.\n");
125 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
126 arphdr
->ar_hrd
, arpinfo
->arhrd
, arpinfo
->arhrd_mask
);
130 if (FWINV((arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
,
132 dprintf("ARP protocol address format mismatch.\n");
133 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
134 arphdr
->ar_pro
, arpinfo
->arpro
, arpinfo
->arpro_mask
);
138 if (FWINV((arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
,
140 dprintf("ARP hardware address length mismatch.\n");
141 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
142 arphdr
->ar_hln
, arpinfo
->arhln
, arpinfo
->arhln_mask
);
146 src_devaddr
= arpptr
;
147 arpptr
+= dev
->addr_len
;
148 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
149 arpptr
+= sizeof(u32
);
150 tgt_devaddr
= arpptr
;
151 arpptr
+= dev
->addr_len
;
152 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
154 if (FWINV(arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
, dev
->addr_len
),
155 ARPT_INV_SRCDEVADDR
) ||
156 FWINV(arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
, dev
->addr_len
),
157 ARPT_INV_TGTDEVADDR
)) {
158 dprintf("Source or target device address mismatch.\n");
163 if (FWINV((src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
,
165 FWINV(((tgt_ipaddr
& arpinfo
->tmsk
.s_addr
) != arpinfo
->tgt
.s_addr
),
167 dprintf("Source or target IP address mismatch.\n");
169 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
171 &arpinfo
->smsk
.s_addr
,
172 &arpinfo
->src
.s_addr
,
173 arpinfo
->invflags
& ARPT_INV_SRCIP
? " (INV)" : "");
174 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
176 &arpinfo
->tmsk
.s_addr
,
177 &arpinfo
->tgt
.s_addr
,
178 arpinfo
->invflags
& ARPT_INV_TGTIP
? " (INV)" : "");
182 /* Look for ifname matches. */
183 ret
= ifname_compare(indev
, arpinfo
->iniface
, arpinfo
->iniface_mask
);
185 if (FWINV(ret
!= 0, ARPT_INV_VIA_IN
)) {
186 dprintf("VIA in mismatch (%s vs %s).%s\n",
187 indev
, arpinfo
->iniface
,
188 arpinfo
->invflags
&ARPT_INV_VIA_IN
?" (INV)":"");
192 ret
= ifname_compare(outdev
, arpinfo
->outiface
, arpinfo
->outiface_mask
);
194 if (FWINV(ret
!= 0, ARPT_INV_VIA_OUT
)) {
195 dprintf("VIA out mismatch (%s vs %s).%s\n",
196 outdev
, arpinfo
->outiface
,
197 arpinfo
->invflags
&ARPT_INV_VIA_OUT
?" (INV)":"");
205 static inline int arp_checkentry(const struct arpt_arp
*arp
)
207 if (arp
->flags
& ~ARPT_F_MASK
) {
208 duprintf("Unknown flag bits set: %08X\n",
209 arp
->flags
& ~ARPT_F_MASK
);
212 if (arp
->invflags
& ~ARPT_INV_MASK
) {
213 duprintf("Unknown invflag bits set: %08X\n",
214 arp
->invflags
& ~ARPT_INV_MASK
);
222 arpt_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
225 pr_err("arp_tables: error: '%s'\n",
226 (const char *)par
->targinfo
);
231 static inline const struct xt_entry_target
*
232 arpt_get_target_c(const struct arpt_entry
*e
)
234 return arpt_get_target((struct arpt_entry
*)e
);
237 static inline struct arpt_entry
*
238 get_entry(const void *base
, unsigned int offset
)
240 return (struct arpt_entry
*)(base
+ offset
);
244 struct arpt_entry
*arpt_next_entry(const struct arpt_entry
*entry
)
246 return (void *)entry
+ entry
->next_offset
;
249 unsigned int arpt_do_table(struct sk_buff
*skb
,
251 const struct net_device
*in
,
252 const struct net_device
*out
,
253 struct xt_table
*table
)
255 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
256 unsigned int verdict
= NF_DROP
;
257 const struct arphdr
*arp
;
258 struct arpt_entry
*e
, *back
;
259 const char *indev
, *outdev
;
261 const struct xt_table_info
*private;
262 struct xt_action_param acpar
;
264 if (!pskb_may_pull(skb
, arp_hdr_len(skb
->dev
)))
267 indev
= in
? in
->name
: nulldevname
;
268 outdev
= out
? out
->name
: nulldevname
;
271 private = table
->private;
272 table_base
= private->entries
[smp_processor_id()];
274 e
= get_entry(table_base
, private->hook_entry
[hook
]);
275 back
= get_entry(table_base
, private->underflow
[hook
]);
279 acpar
.hooknum
= hook
;
280 acpar
.family
= NFPROTO_ARP
;
281 acpar
.hotdrop
= false;
285 const struct xt_entry_target
*t
;
287 if (!arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
288 e
= arpt_next_entry(e
);
292 ADD_COUNTER(e
->counters
, arp_hdr_len(skb
->dev
), 1);
294 t
= arpt_get_target_c(e
);
296 /* Standard target? */
297 if (!t
->u
.kernel
.target
->target
) {
300 v
= ((struct xt_standard_target
*)t
)->verdict
;
302 /* Pop from stack? */
303 if (v
!= XT_RETURN
) {
304 verdict
= (unsigned)(-v
) - 1;
308 back
= get_entry(table_base
, back
->comefrom
);
312 != arpt_next_entry(e
)) {
313 /* Save old back ptr in next entry */
314 struct arpt_entry
*next
= arpt_next_entry(e
);
315 next
->comefrom
= (void *)back
- table_base
;
317 /* set back pointer to next entry */
321 e
= get_entry(table_base
, v
);
325 /* Targets which reenter must return
328 acpar
.target
= t
->u
.kernel
.target
;
329 acpar
.targinfo
= t
->data
;
330 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
332 /* Target might have changed stuff. */
335 if (verdict
== XT_CONTINUE
)
336 e
= arpt_next_entry(e
);
340 } while (!acpar
.hotdrop
);
341 xt_info_rdunlock_bh();
349 /* All zeroes == unconditional rule. */
350 static inline bool unconditional(const struct arpt_arp
*arp
)
352 static const struct arpt_arp uncond
;
354 return memcmp(arp
, &uncond
, sizeof(uncond
)) == 0;
357 /* Figures out from what hook each rule can be called: returns 0 if
358 * there are loops. Puts hook bitmask in comefrom.
360 static int mark_source_chains(const struct xt_table_info
*newinfo
,
361 unsigned int valid_hooks
, void *entry0
)
365 /* No recursion; use packet counter to save back ptrs (reset
366 * to 0 as we leave), and comefrom to save source hook bitmask.
368 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
369 unsigned int pos
= newinfo
->hook_entry
[hook
];
371 = (struct arpt_entry
*)(entry0
+ pos
);
373 if (!(valid_hooks
& (1 << hook
)))
376 /* Set initial back pointer. */
377 e
->counters
.pcnt
= pos
;
380 const struct xt_standard_target
*t
381 = (void *)arpt_get_target_c(e
);
382 int visited
= e
->comefrom
& (1 << hook
);
384 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
385 pr_notice("arptables: loop hook %u pos %u %08X.\n",
386 hook
, pos
, e
->comefrom
);
390 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
392 /* Unconditional return/END. */
393 if ((e
->target_offset
== sizeof(struct arpt_entry
) &&
394 (strcmp(t
->target
.u
.user
.name
,
395 XT_STANDARD_TARGET
) == 0) &&
396 t
->verdict
< 0 && unconditional(&e
->arp
)) ||
398 unsigned int oldpos
, size
;
400 if ((strcmp(t
->target
.u
.user
.name
,
401 XT_STANDARD_TARGET
) == 0) &&
402 t
->verdict
< -NF_MAX_VERDICT
- 1) {
403 duprintf("mark_source_chains: bad "
404 "negative verdict (%i)\n",
409 /* Return: backtrack through the last
413 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
415 pos
= e
->counters
.pcnt
;
416 e
->counters
.pcnt
= 0;
418 /* We're at the start. */
422 e
= (struct arpt_entry
*)
424 } while (oldpos
== pos
+ e
->next_offset
);
427 size
= e
->next_offset
;
428 e
= (struct arpt_entry
*)
429 (entry0
+ pos
+ size
);
430 e
->counters
.pcnt
= pos
;
433 int newpos
= t
->verdict
;
435 if (strcmp(t
->target
.u
.user
.name
,
436 XT_STANDARD_TARGET
) == 0 &&
438 if (newpos
> newinfo
->size
-
439 sizeof(struct arpt_entry
)) {
440 duprintf("mark_source_chains: "
441 "bad verdict (%i)\n",
446 /* This a jump; chase it. */
447 duprintf("Jump rule %u -> %u\n",
450 /* ... this is a fallthru */
451 newpos
= pos
+ e
->next_offset
;
453 e
= (struct arpt_entry
*)
455 e
->counters
.pcnt
= pos
;
460 duprintf("Finished chain %u\n", hook
);
465 static inline int check_entry(const struct arpt_entry
*e
, const char *name
)
467 const struct xt_entry_target
*t
;
469 if (!arp_checkentry(&e
->arp
)) {
470 duprintf("arp_tables: arp check failed %p %s.\n", e
, name
);
474 if (e
->target_offset
+ sizeof(struct xt_entry_target
) > e
->next_offset
)
477 t
= arpt_get_target_c(e
);
478 if (e
->target_offset
+ t
->u
.target_size
> e
->next_offset
)
484 static inline int check_target(struct arpt_entry
*e
, const char *name
)
486 struct xt_entry_target
*t
= arpt_get_target(e
);
488 struct xt_tgchk_param par
= {
491 .target
= t
->u
.kernel
.target
,
493 .hook_mask
= e
->comefrom
,
494 .family
= NFPROTO_ARP
,
497 ret
= xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
), 0, false);
499 duprintf("arp_tables: check failed for `%s'.\n",
500 t
->u
.kernel
.target
->name
);
507 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
)
509 struct xt_entry_target
*t
;
510 struct xt_target
*target
;
513 ret
= check_entry(e
, name
);
517 t
= arpt_get_target(e
);
518 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
520 if (IS_ERR(target
)) {
521 duprintf("find_check_entry: `%s' not found\n", t
->u
.user
.name
);
522 ret
= PTR_ERR(target
);
525 t
->u
.kernel
.target
= target
;
527 ret
= check_target(e
, name
);
532 module_put(t
->u
.kernel
.target
->me
);
537 static bool check_underflow(const struct arpt_entry
*e
)
539 const struct xt_entry_target
*t
;
540 unsigned int verdict
;
542 if (!unconditional(&e
->arp
))
544 t
= arpt_get_target_c(e
);
545 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
547 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
548 verdict
= -verdict
- 1;
549 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
552 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
553 struct xt_table_info
*newinfo
,
554 const unsigned char *base
,
555 const unsigned char *limit
,
556 const unsigned int *hook_entries
,
557 const unsigned int *underflows
,
558 unsigned int valid_hooks
)
562 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0 ||
563 (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
) {
564 duprintf("Bad offset %p\n", e
);
569 < sizeof(struct arpt_entry
) + sizeof(struct xt_entry_target
)) {
570 duprintf("checking: element %p size %u\n",
575 /* Check hooks & underflows */
576 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
577 if (!(valid_hooks
& (1 << h
)))
579 if ((unsigned char *)e
- base
== hook_entries
[h
])
580 newinfo
->hook_entry
[h
] = hook_entries
[h
];
581 if ((unsigned char *)e
- base
== underflows
[h
]) {
582 if (!check_underflow(e
)) {
583 pr_err("Underflows must be unconditional and "
584 "use the STANDARD target with "
588 newinfo
->underflow
[h
] = underflows
[h
];
592 /* Clear counters and comefrom */
593 e
->counters
= ((struct xt_counters
) { 0, 0 });
598 static inline void cleanup_entry(struct arpt_entry
*e
)
600 struct xt_tgdtor_param par
;
601 struct xt_entry_target
*t
;
603 t
= arpt_get_target(e
);
604 par
.target
= t
->u
.kernel
.target
;
605 par
.targinfo
= t
->data
;
606 par
.family
= NFPROTO_ARP
;
607 if (par
.target
->destroy
!= NULL
)
608 par
.target
->destroy(&par
);
609 module_put(par
.target
->me
);
612 /* Checks and translates the user-supplied table segment (held in
615 static int translate_table(struct xt_table_info
*newinfo
, void *entry0
,
616 const struct arpt_replace
*repl
)
618 struct arpt_entry
*iter
;
622 newinfo
->size
= repl
->size
;
623 newinfo
->number
= repl
->num_entries
;
625 /* Init all hooks to impossible value. */
626 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
627 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
628 newinfo
->underflow
[i
] = 0xFFFFFFFF;
631 duprintf("translate_table: size %u\n", newinfo
->size
);
634 /* Walk through entries, checking offsets. */
635 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
636 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
644 if (strcmp(arpt_get_target(iter
)->u
.user
.name
,
645 XT_ERROR_TARGET
) == 0)
646 ++newinfo
->stacksize
;
648 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
652 if (i
!= repl
->num_entries
) {
653 duprintf("translate_table: %u not %u entries\n",
654 i
, repl
->num_entries
);
658 /* Check hooks all assigned */
659 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
660 /* Only hooks which are valid */
661 if (!(repl
->valid_hooks
& (1 << i
)))
663 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
664 duprintf("Invalid hook entry %u %u\n",
665 i
, repl
->hook_entry
[i
]);
668 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
669 duprintf("Invalid underflow %u %u\n",
670 i
, repl
->underflow
[i
]);
675 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
)) {
676 duprintf("Looping hook\n");
680 /* Finally, each sanity check must pass */
682 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
683 ret
= find_check_entry(iter
, repl
->name
, repl
->size
);
690 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
698 /* And one copy for every other CPU */
699 for_each_possible_cpu(i
) {
700 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry0
)
701 memcpy(newinfo
->entries
[i
], entry0
, newinfo
->size
);
707 static void get_counters(const struct xt_table_info
*t
,
708 struct xt_counters counters
[])
710 struct arpt_entry
*iter
;
713 unsigned int curcpu
= get_cpu();
715 /* Instead of clearing (by a previous call to memset())
716 * the counters and using adds, we set the counters
717 * with data used by 'current' CPU
719 * Bottom half has to be disabled to prevent deadlock
720 * if new softirq were to run and call ipt_do_table
724 xt_entry_foreach(iter
, t
->entries
[curcpu
], t
->size
) {
725 SET_COUNTER(counters
[i
], iter
->counters
.bcnt
,
726 iter
->counters
.pcnt
);
730 /* Processing counters from other cpus, we can let bottom half enabled,
731 * (preemption is disabled)
734 for_each_possible_cpu(cpu
) {
740 xt_entry_foreach(iter
, t
->entries
[cpu
], t
->size
) {
741 ADD_COUNTER(counters
[i
], iter
->counters
.bcnt
,
742 iter
->counters
.pcnt
);
745 xt_info_wrunlock(cpu
);
751 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
753 unsigned int countersize
;
754 struct xt_counters
*counters
;
755 const struct xt_table_info
*private = table
->private;
757 /* We need atomic snapshot of counters: rest doesn't change
758 * (other than comefrom, which userspace doesn't care
761 countersize
= sizeof(struct xt_counters
) * private->number
;
762 counters
= vmalloc(countersize
);
764 if (counters
== NULL
)
765 return ERR_PTR(-ENOMEM
);
767 get_counters(private, counters
);
772 static int copy_entries_to_user(unsigned int total_size
,
773 const struct xt_table
*table
,
774 void __user
*userptr
)
776 unsigned int off
, num
;
777 const struct arpt_entry
*e
;
778 struct xt_counters
*counters
;
779 struct xt_table_info
*private = table
->private;
783 counters
= alloc_counters(table
);
784 if (IS_ERR(counters
))
785 return PTR_ERR(counters
);
787 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
788 /* ... then copy entire thing ... */
789 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
794 /* FIXME: use iterator macros --RR */
795 /* ... then go back and fix counters and names */
796 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
797 const struct xt_entry_target
*t
;
799 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
800 if (copy_to_user(userptr
+ off
801 + offsetof(struct arpt_entry
, counters
),
803 sizeof(counters
[num
])) != 0) {
808 t
= arpt_get_target_c(e
);
809 if (copy_to_user(userptr
+ off
+ e
->target_offset
810 + offsetof(struct xt_entry_target
,
812 t
->u
.kernel
.target
->name
,
813 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
825 static void compat_standard_from_user(void *dst
, const void *src
)
827 int v
= *(compat_int_t
*)src
;
830 v
+= xt_compat_calc_jump(NFPROTO_ARP
, v
);
831 memcpy(dst
, &v
, sizeof(v
));
834 static int compat_standard_to_user(void __user
*dst
, const void *src
)
836 compat_int_t cv
= *(int *)src
;
839 cv
-= xt_compat_calc_jump(NFPROTO_ARP
, cv
);
840 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
843 static int compat_calc_entry(const struct arpt_entry
*e
,
844 const struct xt_table_info
*info
,
845 const void *base
, struct xt_table_info
*newinfo
)
847 const struct xt_entry_target
*t
;
848 unsigned int entry_offset
;
851 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
852 entry_offset
= (void *)e
- base
;
854 t
= arpt_get_target_c(e
);
855 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
856 newinfo
->size
-= off
;
857 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
861 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
862 if (info
->hook_entry
[i
] &&
863 (e
< (struct arpt_entry
*)(base
+ info
->hook_entry
[i
])))
864 newinfo
->hook_entry
[i
] -= off
;
865 if (info
->underflow
[i
] &&
866 (e
< (struct arpt_entry
*)(base
+ info
->underflow
[i
])))
867 newinfo
->underflow
[i
] -= off
;
872 static int compat_table_info(const struct xt_table_info
*info
,
873 struct xt_table_info
*newinfo
)
875 struct arpt_entry
*iter
;
879 if (!newinfo
|| !info
)
882 /* we dont care about newinfo->entries[] */
883 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
884 newinfo
->initial_entries
= 0;
885 loc_cpu_entry
= info
->entries
[raw_smp_processor_id()];
886 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
887 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
895 static int get_info(struct net
*net
, void __user
*user
,
896 const int *len
, int compat
)
898 char name
[XT_TABLE_MAXNAMELEN
];
902 if (*len
!= sizeof(struct arpt_getinfo
)) {
903 duprintf("length %u != %Zu\n", *len
,
904 sizeof(struct arpt_getinfo
));
908 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
911 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
914 xt_compat_lock(NFPROTO_ARP
);
916 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
917 "arptable_%s", name
);
918 if (t
&& !IS_ERR(t
)) {
919 struct arpt_getinfo info
;
920 const struct xt_table_info
*private = t
->private;
922 struct xt_table_info tmp
;
925 ret
= compat_table_info(private, &tmp
);
926 xt_compat_flush_offsets(NFPROTO_ARP
);
930 memset(&info
, 0, sizeof(info
));
931 info
.valid_hooks
= t
->valid_hooks
;
932 memcpy(info
.hook_entry
, private->hook_entry
,
933 sizeof(info
.hook_entry
));
934 memcpy(info
.underflow
, private->underflow
,
935 sizeof(info
.underflow
));
936 info
.num_entries
= private->number
;
937 info
.size
= private->size
;
938 strcpy(info
.name
, name
);
940 if (copy_to_user(user
, &info
, *len
) != 0)
947 ret
= t
? PTR_ERR(t
) : -ENOENT
;
950 xt_compat_unlock(NFPROTO_ARP
);
955 static int get_entries(struct net
*net
, struct arpt_get_entries __user
*uptr
,
959 struct arpt_get_entries get
;
962 if (*len
< sizeof(get
)) {
963 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
966 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
968 if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
969 duprintf("get_entries: %u != %Zu\n", *len
,
970 sizeof(struct arpt_get_entries
) + get
.size
);
974 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
975 if (t
&& !IS_ERR(t
)) {
976 const struct xt_table_info
*private = t
->private;
978 duprintf("t->private->number = %u\n",
980 if (get
.size
== private->size
)
981 ret
= copy_entries_to_user(private->size
,
982 t
, uptr
->entrytable
);
984 duprintf("get_entries: I've got %u not %u!\n",
985 private->size
, get
.size
);
991 ret
= t
? PTR_ERR(t
) : -ENOENT
;
996 static int __do_replace(struct net
*net
, const char *name
,
997 unsigned int valid_hooks
,
998 struct xt_table_info
*newinfo
,
999 unsigned int num_counters
,
1000 void __user
*counters_ptr
)
1004 struct xt_table_info
*oldinfo
;
1005 struct xt_counters
*counters
;
1006 void *loc_cpu_old_entry
;
1007 struct arpt_entry
*iter
;
1010 counters
= vmalloc(num_counters
* sizeof(struct xt_counters
));
1016 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
1017 "arptable_%s", name
);
1018 if (!t
|| IS_ERR(t
)) {
1019 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1020 goto free_newinfo_counters_untrans
;
1024 if (valid_hooks
!= t
->valid_hooks
) {
1025 duprintf("Valid hook crap: %08X vs %08X\n",
1026 valid_hooks
, t
->valid_hooks
);
1031 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
1035 /* Update module usage count based on number of rules */
1036 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1037 oldinfo
->number
, oldinfo
->initial_entries
, newinfo
->number
);
1038 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
1039 (newinfo
->number
<= oldinfo
->initial_entries
))
1041 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
1042 (newinfo
->number
<= oldinfo
->initial_entries
))
1045 /* Get the old counters, and synchronize with replace */
1046 get_counters(oldinfo
, counters
);
1048 /* Decrease module usage counts and free resource */
1049 loc_cpu_old_entry
= oldinfo
->entries
[raw_smp_processor_id()];
1050 xt_entry_foreach(iter
, loc_cpu_old_entry
, oldinfo
->size
)
1051 cleanup_entry(iter
);
1053 xt_free_table_info(oldinfo
);
1054 if (copy_to_user(counters_ptr
, counters
,
1055 sizeof(struct xt_counters
) * num_counters
) != 0)
1064 free_newinfo_counters_untrans
:
1070 static int do_replace(struct net
*net
, const void __user
*user
,
1074 struct arpt_replace tmp
;
1075 struct xt_table_info
*newinfo
;
1076 void *loc_cpu_entry
;
1077 struct arpt_entry
*iter
;
1079 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1082 /* overflow check */
1083 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1086 newinfo
= xt_alloc_table_info(tmp
.size
);
1090 /* choose the copy that is on our node/cpu */
1091 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1092 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1098 ret
= translate_table(newinfo
, loc_cpu_entry
, &tmp
);
1102 duprintf("arp_tables: Translated table\n");
1104 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1105 tmp
.num_counters
, tmp
.counters
);
1107 goto free_newinfo_untrans
;
1110 free_newinfo_untrans
:
1111 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1112 cleanup_entry(iter
);
1114 xt_free_table_info(newinfo
);
1118 static int do_add_counters(struct net
*net
, const void __user
*user
,
1119 unsigned int len
, int compat
)
1121 unsigned int i
, curcpu
;
1122 struct xt_counters_info tmp
;
1123 struct xt_counters
*paddc
;
1124 unsigned int num_counters
;
1129 const struct xt_table_info
*private;
1131 void *loc_cpu_entry
;
1132 struct arpt_entry
*iter
;
1133 #ifdef CONFIG_COMPAT
1134 struct compat_xt_counters_info compat_tmp
;
1138 size
= sizeof(struct compat_xt_counters_info
);
1143 size
= sizeof(struct xt_counters_info
);
1146 if (copy_from_user(ptmp
, user
, size
) != 0)
1149 #ifdef CONFIG_COMPAT
1151 num_counters
= compat_tmp
.num_counters
;
1152 name
= compat_tmp
.name
;
1156 num_counters
= tmp
.num_counters
;
1160 if (len
!= size
+ num_counters
* sizeof(struct xt_counters
))
1163 paddc
= vmalloc(len
- size
);
1167 if (copy_from_user(paddc
, user
+ size
, len
- size
) != 0) {
1172 t
= xt_find_table_lock(net
, NFPROTO_ARP
, name
);
1173 if (!t
|| IS_ERR(t
)) {
1174 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1179 private = t
->private;
1180 if (private->number
!= num_counters
) {
1182 goto unlock_up_free
;
1186 /* Choose the copy that is on our node */
1187 curcpu
= smp_processor_id();
1188 loc_cpu_entry
= private->entries
[curcpu
];
1189 xt_info_wrlock(curcpu
);
1190 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
) {
1191 ADD_COUNTER(iter
->counters
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1194 xt_info_wrunlock(curcpu
);
1205 #ifdef CONFIG_COMPAT
1206 static inline void compat_release_entry(struct compat_arpt_entry
*e
)
1208 struct xt_entry_target
*t
;
1210 t
= compat_arpt_get_target(e
);
1211 module_put(t
->u
.kernel
.target
->me
);
1215 check_compat_entry_size_and_hooks(struct compat_arpt_entry
*e
,
1216 struct xt_table_info
*newinfo
,
1218 const unsigned char *base
,
1219 const unsigned char *limit
,
1220 const unsigned int *hook_entries
,
1221 const unsigned int *underflows
,
1224 struct xt_entry_target
*t
;
1225 struct xt_target
*target
;
1226 unsigned int entry_offset
;
1229 duprintf("check_compat_entry_size_and_hooks %p\n", e
);
1230 if ((unsigned long)e
% __alignof__(struct compat_arpt_entry
) != 0 ||
1231 (unsigned char *)e
+ sizeof(struct compat_arpt_entry
) >= limit
) {
1232 duprintf("Bad offset %p, limit = %p\n", e
, limit
);
1236 if (e
->next_offset
< sizeof(struct compat_arpt_entry
) +
1237 sizeof(struct compat_xt_entry_target
)) {
1238 duprintf("checking: element %p size %u\n",
1243 /* For purposes of check_entry casting the compat entry is fine */
1244 ret
= check_entry((struct arpt_entry
*)e
, name
);
1248 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1249 entry_offset
= (void *)e
- (void *)base
;
1251 t
= compat_arpt_get_target(e
);
1252 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
1253 t
->u
.user
.revision
);
1254 if (IS_ERR(target
)) {
1255 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1257 ret
= PTR_ERR(target
);
1260 t
->u
.kernel
.target
= target
;
1262 off
+= xt_compat_target_offset(target
);
1264 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
1266 goto release_target
;
1268 /* Check hooks & underflows */
1269 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1270 if ((unsigned char *)e
- base
== hook_entries
[h
])
1271 newinfo
->hook_entry
[h
] = hook_entries
[h
];
1272 if ((unsigned char *)e
- base
== underflows
[h
])
1273 newinfo
->underflow
[h
] = underflows
[h
];
1276 /* Clear counters and comefrom */
1277 memset(&e
->counters
, 0, sizeof(e
->counters
));
1282 module_put(t
->u
.kernel
.target
->me
);
1288 compat_copy_entry_from_user(struct compat_arpt_entry
*e
, void **dstptr
,
1289 unsigned int *size
, const char *name
,
1290 struct xt_table_info
*newinfo
, unsigned char *base
)
1292 struct xt_entry_target
*t
;
1293 struct xt_target
*target
;
1294 struct arpt_entry
*de
;
1295 unsigned int origsize
;
1300 de
= (struct arpt_entry
*)*dstptr
;
1301 memcpy(de
, e
, sizeof(struct arpt_entry
));
1302 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1304 *dstptr
+= sizeof(struct arpt_entry
);
1305 *size
+= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1307 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1308 t
= compat_arpt_get_target(e
);
1309 target
= t
->u
.kernel
.target
;
1310 xt_compat_target_from_user(t
, dstptr
, size
);
1312 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1313 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1314 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1315 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1316 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1317 newinfo
->underflow
[h
] -= origsize
- *size
;
1322 static int translate_compat_table(const char *name
,
1323 unsigned int valid_hooks
,
1324 struct xt_table_info
**pinfo
,
1326 unsigned int total_size
,
1327 unsigned int number
,
1328 unsigned int *hook_entries
,
1329 unsigned int *underflows
)
1332 struct xt_table_info
*newinfo
, *info
;
1333 void *pos
, *entry0
, *entry1
;
1334 struct compat_arpt_entry
*iter0
;
1335 struct arpt_entry
*iter1
;
1342 info
->number
= number
;
1344 /* Init all hooks to impossible value. */
1345 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1346 info
->hook_entry
[i
] = 0xFFFFFFFF;
1347 info
->underflow
[i
] = 0xFFFFFFFF;
1350 duprintf("translate_compat_table: size %u\n", info
->size
);
1352 xt_compat_lock(NFPROTO_ARP
);
1353 /* Walk through entries, checking offsets. */
1354 xt_entry_foreach(iter0
, entry0
, total_size
) {
1355 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1357 entry0
+ total_size
,
1368 duprintf("translate_compat_table: %u not %u entries\n",
1373 /* Check hooks all assigned */
1374 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1375 /* Only hooks which are valid */
1376 if (!(valid_hooks
& (1 << i
)))
1378 if (info
->hook_entry
[i
] == 0xFFFFFFFF) {
1379 duprintf("Invalid hook entry %u %u\n",
1380 i
, hook_entries
[i
]);
1383 if (info
->underflow
[i
] == 0xFFFFFFFF) {
1384 duprintf("Invalid underflow %u %u\n",
1391 newinfo
= xt_alloc_table_info(size
);
1395 newinfo
->number
= number
;
1396 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1397 newinfo
->hook_entry
[i
] = info
->hook_entry
[i
];
1398 newinfo
->underflow
[i
] = info
->underflow
[i
];
1400 entry1
= newinfo
->entries
[raw_smp_processor_id()];
1403 xt_entry_foreach(iter0
, entry0
, total_size
) {
1404 ret
= compat_copy_entry_from_user(iter0
, &pos
, &size
,
1405 name
, newinfo
, entry1
);
1409 xt_compat_flush_offsets(NFPROTO_ARP
);
1410 xt_compat_unlock(NFPROTO_ARP
);
1415 if (!mark_source_chains(newinfo
, valid_hooks
, entry1
))
1419 xt_entry_foreach(iter1
, entry1
, newinfo
->size
) {
1420 ret
= check_target(iter1
, name
);
1424 if (strcmp(arpt_get_target(iter1
)->u
.user
.name
,
1425 XT_ERROR_TARGET
) == 0)
1426 ++newinfo
->stacksize
;
1430 * The first i matches need cleanup_entry (calls ->destroy)
1431 * because they had called ->check already. The other j-i
1432 * entries need only release.
1436 xt_entry_foreach(iter0
, entry0
, newinfo
->size
) {
1441 compat_release_entry(iter0
);
1443 xt_entry_foreach(iter1
, entry1
, newinfo
->size
) {
1446 cleanup_entry(iter1
);
1448 xt_free_table_info(newinfo
);
1452 /* And one copy for every other CPU */
1453 for_each_possible_cpu(i
)
1454 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry1
)
1455 memcpy(newinfo
->entries
[i
], entry1
, newinfo
->size
);
1459 xt_free_table_info(info
);
1463 xt_free_table_info(newinfo
);
1465 xt_entry_foreach(iter0
, entry0
, total_size
) {
1468 compat_release_entry(iter0
);
1472 xt_compat_flush_offsets(NFPROTO_ARP
);
1473 xt_compat_unlock(NFPROTO_ARP
);
1477 struct compat_arpt_replace
{
1478 char name
[XT_TABLE_MAXNAMELEN
];
1482 u32 hook_entry
[NF_ARP_NUMHOOKS
];
1483 u32 underflow
[NF_ARP_NUMHOOKS
];
1485 compat_uptr_t counters
;
1486 struct compat_arpt_entry entries
[0];
1489 static int compat_do_replace(struct net
*net
, void __user
*user
,
1493 struct compat_arpt_replace tmp
;
1494 struct xt_table_info
*newinfo
;
1495 void *loc_cpu_entry
;
1496 struct arpt_entry
*iter
;
1498 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1501 /* overflow check */
1502 if (tmp
.size
>= INT_MAX
/ num_possible_cpus())
1504 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1507 newinfo
= xt_alloc_table_info(tmp
.size
);
1511 /* choose the copy that is on our node/cpu */
1512 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1513 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
), tmp
.size
) != 0) {
1518 ret
= translate_compat_table(tmp
.name
, tmp
.valid_hooks
,
1519 &newinfo
, &loc_cpu_entry
, tmp
.size
,
1520 tmp
.num_entries
, tmp
.hook_entry
,
1525 duprintf("compat_do_replace: Translated table\n");
1527 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1528 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1530 goto free_newinfo_untrans
;
1533 free_newinfo_untrans
:
1534 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1535 cleanup_entry(iter
);
1537 xt_free_table_info(newinfo
);
1541 static int compat_do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1546 if (!capable(CAP_NET_ADMIN
))
1550 case ARPT_SO_SET_REPLACE
:
1551 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1554 case ARPT_SO_SET_ADD_COUNTERS
:
1555 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1559 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1566 static int compat_copy_entry_to_user(struct arpt_entry
*e
, void __user
**dstptr
,
1567 compat_uint_t
*size
,
1568 struct xt_counters
*counters
,
1571 struct xt_entry_target
*t
;
1572 struct compat_arpt_entry __user
*ce
;
1573 u_int16_t target_offset
, next_offset
;
1574 compat_uint_t origsize
;
1578 ce
= (struct compat_arpt_entry __user
*)*dstptr
;
1579 if (copy_to_user(ce
, e
, sizeof(struct arpt_entry
)) != 0 ||
1580 copy_to_user(&ce
->counters
, &counters
[i
],
1581 sizeof(counters
[i
])) != 0)
1584 *dstptr
+= sizeof(struct compat_arpt_entry
);
1585 *size
-= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1587 target_offset
= e
->target_offset
- (origsize
- *size
);
1589 t
= arpt_get_target(e
);
1590 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1593 next_offset
= e
->next_offset
- (origsize
- *size
);
1594 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1595 put_user(next_offset
, &ce
->next_offset
) != 0)
1600 static int compat_copy_entries_to_user(unsigned int total_size
,
1601 struct xt_table
*table
,
1602 void __user
*userptr
)
1604 struct xt_counters
*counters
;
1605 const struct xt_table_info
*private = table
->private;
1609 void *loc_cpu_entry
;
1611 struct arpt_entry
*iter
;
1613 counters
= alloc_counters(table
);
1614 if (IS_ERR(counters
))
1615 return PTR_ERR(counters
);
1617 /* choose the copy on our node/cpu */
1618 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1621 xt_entry_foreach(iter
, loc_cpu_entry
, total_size
) {
1622 ret
= compat_copy_entry_to_user(iter
, &pos
,
1623 &size
, counters
, i
++);
1631 struct compat_arpt_get_entries
{
1632 char name
[XT_TABLE_MAXNAMELEN
];
1634 struct compat_arpt_entry entrytable
[0];
1637 static int compat_get_entries(struct net
*net
,
1638 struct compat_arpt_get_entries __user
*uptr
,
1642 struct compat_arpt_get_entries get
;
1645 if (*len
< sizeof(get
)) {
1646 duprintf("compat_get_entries: %u < %zu\n", *len
, sizeof(get
));
1649 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1651 if (*len
!= sizeof(struct compat_arpt_get_entries
) + get
.size
) {
1652 duprintf("compat_get_entries: %u != %zu\n",
1653 *len
, sizeof(get
) + get
.size
);
1657 xt_compat_lock(NFPROTO_ARP
);
1658 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1659 if (t
&& !IS_ERR(t
)) {
1660 const struct xt_table_info
*private = t
->private;
1661 struct xt_table_info info
;
1663 duprintf("t->private->number = %u\n", private->number
);
1664 ret
= compat_table_info(private, &info
);
1665 if (!ret
&& get
.size
== info
.size
) {
1666 ret
= compat_copy_entries_to_user(private->size
,
1667 t
, uptr
->entrytable
);
1669 duprintf("compat_get_entries: I've got %u not %u!\n",
1670 private->size
, get
.size
);
1673 xt_compat_flush_offsets(NFPROTO_ARP
);
1677 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1679 xt_compat_unlock(NFPROTO_ARP
);
1683 static int do_arpt_get_ctl(struct sock
*, int, void __user
*, int *);
1685 static int compat_do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1690 if (!capable(CAP_NET_ADMIN
))
1694 case ARPT_SO_GET_INFO
:
1695 ret
= get_info(sock_net(sk
), user
, len
, 1);
1697 case ARPT_SO_GET_ENTRIES
:
1698 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1701 ret
= do_arpt_get_ctl(sk
, cmd
, user
, len
);
1707 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1711 if (!capable(CAP_NET_ADMIN
))
1715 case ARPT_SO_SET_REPLACE
:
1716 ret
= do_replace(sock_net(sk
), user
, len
);
1719 case ARPT_SO_SET_ADD_COUNTERS
:
1720 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1724 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1731 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1735 if (!capable(CAP_NET_ADMIN
))
1739 case ARPT_SO_GET_INFO
:
1740 ret
= get_info(sock_net(sk
), user
, len
, 0);
1743 case ARPT_SO_GET_ENTRIES
:
1744 ret
= get_entries(sock_net(sk
), user
, len
);
1747 case ARPT_SO_GET_REVISION_TARGET
: {
1748 struct xt_get_revision rev
;
1750 if (*len
!= sizeof(rev
)) {
1754 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1759 try_then_request_module(xt_find_revision(NFPROTO_ARP
, rev
.name
,
1760 rev
.revision
, 1, &ret
),
1761 "arpt_%s", rev
.name
);
1766 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1773 struct xt_table
*arpt_register_table(struct net
*net
,
1774 const struct xt_table
*table
,
1775 const struct arpt_replace
*repl
)
1778 struct xt_table_info
*newinfo
;
1779 struct xt_table_info bootstrap
= {0};
1780 void *loc_cpu_entry
;
1781 struct xt_table
*new_table
;
1783 newinfo
= xt_alloc_table_info(repl
->size
);
1789 /* choose the copy on our node/cpu */
1790 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1791 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1793 ret
= translate_table(newinfo
, loc_cpu_entry
, repl
);
1794 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1798 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1799 if (IS_ERR(new_table
)) {
1800 ret
= PTR_ERR(new_table
);
1806 xt_free_table_info(newinfo
);
1808 return ERR_PTR(ret
);
1811 void arpt_unregister_table(struct xt_table
*table
)
1813 struct xt_table_info
*private;
1814 void *loc_cpu_entry
;
1815 struct module
*table_owner
= table
->me
;
1816 struct arpt_entry
*iter
;
1818 private = xt_unregister_table(table
);
1820 /* Decrease module usage counts and free resources */
1821 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1822 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1823 cleanup_entry(iter
);
1824 if (private->number
> private->initial_entries
)
1825 module_put(table_owner
);
1826 xt_free_table_info(private);
1829 /* The built-in targets: standard (NULL) and error. */
1830 static struct xt_target arpt_builtin_tg
[] __read_mostly
= {
1832 .name
= XT_STANDARD_TARGET
,
1833 .targetsize
= sizeof(int),
1834 .family
= NFPROTO_ARP
,
1835 #ifdef CONFIG_COMPAT
1836 .compatsize
= sizeof(compat_int_t
),
1837 .compat_from_user
= compat_standard_from_user
,
1838 .compat_to_user
= compat_standard_to_user
,
1842 .name
= XT_ERROR_TARGET
,
1843 .target
= arpt_error
,
1844 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1845 .family
= NFPROTO_ARP
,
1849 static struct nf_sockopt_ops arpt_sockopts
= {
1851 .set_optmin
= ARPT_BASE_CTL
,
1852 .set_optmax
= ARPT_SO_SET_MAX
+1,
1853 .set
= do_arpt_set_ctl
,
1854 #ifdef CONFIG_COMPAT
1855 .compat_set
= compat_do_arpt_set_ctl
,
1857 .get_optmin
= ARPT_BASE_CTL
,
1858 .get_optmax
= ARPT_SO_GET_MAX
+1,
1859 .get
= do_arpt_get_ctl
,
1860 #ifdef CONFIG_COMPAT
1861 .compat_get
= compat_do_arpt_get_ctl
,
1863 .owner
= THIS_MODULE
,
1866 static int __net_init
arp_tables_net_init(struct net
*net
)
1868 return xt_proto_init(net
, NFPROTO_ARP
);
1871 static void __net_exit
arp_tables_net_exit(struct net
*net
)
1873 xt_proto_fini(net
, NFPROTO_ARP
);
1876 static struct pernet_operations arp_tables_net_ops
= {
1877 .init
= arp_tables_net_init
,
1878 .exit
= arp_tables_net_exit
,
1881 static int __init
arp_tables_init(void)
1885 ret
= register_pernet_subsys(&arp_tables_net_ops
);
1889 /* Noone else will be downing sem now, so we won't sleep */
1890 ret
= xt_register_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1894 /* Register setsockopt */
1895 ret
= nf_register_sockopt(&arpt_sockopts
);
1899 printk(KERN_INFO
"arp_tables: (C) 2002 David S. Miller\n");
1903 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1905 unregister_pernet_subsys(&arp_tables_net_ops
);
1910 static void __exit
arp_tables_fini(void)
1912 nf_unregister_sockopt(&arpt_sockopts
);
1913 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1914 unregister_pernet_subsys(&arp_tables_net_ops
);
1917 EXPORT_SYMBOL(arpt_register_table
);
1918 EXPORT_SYMBOL(arpt_unregister_table
);
1919 EXPORT_SYMBOL(arpt_do_table
);
1921 module_init(arp_tables_init
);
1922 module_exit(arp_tables_fini
);