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)
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>
23 #include <asm/uaccess.h>
24 #include <linux/mutex.h>
26 #include <linux/netfilter/x_tables.h>
27 #include <linux/netfilter_arp/arp_tables.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
31 MODULE_DESCRIPTION("arptables core");
33 /*#define DEBUG_ARP_TABLES*/
34 /*#define DEBUG_ARP_TABLES_USER*/
36 #ifdef DEBUG_ARP_TABLES
37 #define dprintf(format, args...) printk(format , ## args)
39 #define dprintf(format, args...)
42 #ifdef DEBUG_ARP_TABLES_USER
43 #define duprintf(format, args...) printk(format , ## args)
45 #define duprintf(format, args...)
48 #ifdef CONFIG_NETFILTER_DEBUG
49 #define ARP_NF_ASSERT(x) \
52 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
53 __FUNCTION__, __FILE__, __LINE__); \
56 #define ARP_NF_ASSERT(x)
59 static inline int arp_devaddr_compare(const struct arpt_devaddr_info
*ap
,
60 char *hdr_addr
, int len
)
64 if (len
> ARPT_DEV_ADDR_LEN_MAX
)
65 len
= ARPT_DEV_ADDR_LEN_MAX
;
68 for (i
= 0; i
< len
; i
++)
69 ret
|= (hdr_addr
[i
] ^ ap
->addr
[i
]) & ap
->mask
[i
];
75 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
76 * Some arches dont care, unrolling the loop is a win on them.
77 * For other arches, we only have a 16bit alignement.
79 static unsigned long ifname_compare(const char *_a
, const char *_b
, const char *_mask
)
81 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
82 unsigned long ret
= ifname_compare_aligned(_a
, _b
, _mask
);
84 unsigned long ret
= 0;
85 const u16
*a
= (const u16
*)_a
;
86 const u16
*b
= (const u16
*)_b
;
87 const u16
*mask
= (const u16
*)_mask
;
90 for (i
= 0; i
< IFNAMSIZ
/sizeof(u16
); i
++)
91 ret
|= (a
[i
] ^ b
[i
]) & mask
[i
];
96 /* Returns whether packet matches rule or not. */
97 static inline int arp_packet_match(const struct arphdr
*arphdr
,
98 struct net_device
*dev
,
101 const struct arpt_arp
*arpinfo
)
103 char *arpptr
= (char *)(arphdr
+ 1);
104 char *src_devaddr
, *tgt_devaddr
;
105 __be32 src_ipaddr
, tgt_ipaddr
;
108 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
110 if (FWINV((arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
,
112 dprintf("ARP operation field mismatch.\n");
113 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
114 arphdr
->ar_op
, arpinfo
->arpop
, arpinfo
->arpop_mask
);
118 if (FWINV((arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
,
120 dprintf("ARP hardware address format mismatch.\n");
121 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
122 arphdr
->ar_hrd
, arpinfo
->arhrd
, arpinfo
->arhrd_mask
);
126 if (FWINV((arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
,
128 dprintf("ARP protocol address format mismatch.\n");
129 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
130 arphdr
->ar_pro
, arpinfo
->arpro
, arpinfo
->arpro_mask
);
134 if (FWINV((arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
,
136 dprintf("ARP hardware address length mismatch.\n");
137 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
138 arphdr
->ar_hln
, arpinfo
->arhln
, arpinfo
->arhln_mask
);
142 src_devaddr
= arpptr
;
143 arpptr
+= dev
->addr_len
;
144 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
145 arpptr
+= sizeof(u32
);
146 tgt_devaddr
= arpptr
;
147 arpptr
+= dev
->addr_len
;
148 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
150 if (FWINV(arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
, dev
->addr_len
),
151 ARPT_INV_SRCDEVADDR
) ||
152 FWINV(arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
, dev
->addr_len
),
153 ARPT_INV_TGTDEVADDR
)) {
154 dprintf("Source or target device address mismatch.\n");
159 if (FWINV((src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
,
161 FWINV(((tgt_ipaddr
& arpinfo
->tmsk
.s_addr
) != arpinfo
->tgt
.s_addr
),
163 dprintf("Source or target IP address mismatch.\n");
165 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
167 NIPQUAD(arpinfo
->smsk
.s_addr
),
168 NIPQUAD(arpinfo
->src
.s_addr
),
169 arpinfo
->invflags
& ARPT_INV_SRCIP
? " (INV)" : "");
170 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
172 NIPQUAD(arpinfo
->tmsk
.s_addr
),
173 NIPQUAD(arpinfo
->tgt
.s_addr
),
174 arpinfo
->invflags
& ARPT_INV_TGTIP
? " (INV)" : "");
178 /* Look for ifname matches. */
179 ret
= ifname_compare(indev
, arpinfo
->iniface
, arpinfo
->iniface_mask
);
181 if (FWINV(ret
!= 0, ARPT_INV_VIA_IN
)) {
182 dprintf("VIA in mismatch (%s vs %s).%s\n",
183 indev
, arpinfo
->iniface
,
184 arpinfo
->invflags
&ARPT_INV_VIA_IN
?" (INV)":"");
188 ret
= ifname_compare(outdev
, arpinfo
->outiface
, arpinfo
->outiface_mask
);
190 if (FWINV(ret
!= 0, ARPT_INV_VIA_OUT
)) {
191 dprintf("VIA out mismatch (%s vs %s).%s\n",
192 outdev
, arpinfo
->outiface
,
193 arpinfo
->invflags
&ARPT_INV_VIA_OUT
?" (INV)":"");
200 static inline int arp_checkentry(const struct arpt_arp
*arp
)
202 if (arp
->flags
& ~ARPT_F_MASK
) {
203 duprintf("Unknown flag bits set: %08X\n",
204 arp
->flags
& ~ARPT_F_MASK
);
207 if (arp
->invflags
& ~ARPT_INV_MASK
) {
208 duprintf("Unknown invflag bits set: %08X\n",
209 arp
->invflags
& ~ARPT_INV_MASK
);
216 static unsigned int arpt_error(struct sk_buff
*skb
,
217 const struct net_device
*in
,
218 const struct net_device
*out
,
219 unsigned int hooknum
,
220 const struct xt_target
*target
,
221 const void *targinfo
)
224 printk("arp_tables: error: '%s'\n", (char *)targinfo
);
229 static inline struct arpt_entry
*get_entry(void *base
, unsigned int offset
)
231 return (struct arpt_entry
*)(base
+ offset
);
234 unsigned int arpt_do_table(struct sk_buff
*skb
,
236 const struct net_device
*in
,
237 const struct net_device
*out
,
238 struct arpt_table
*table
)
240 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
241 unsigned int verdict
= NF_DROP
;
244 struct arpt_entry
*e
, *back
;
245 const char *indev
, *outdev
;
247 struct xt_table_info
*private;
249 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
250 if (!pskb_may_pull(skb
, (sizeof(struct arphdr
) +
251 (2 * skb
->dev
->addr_len
) +
255 indev
= in
? in
->name
: nulldevname
;
256 outdev
= out
? out
->name
: nulldevname
;
259 private = table
->private;
260 table_base
= private->entries
[smp_processor_id()];
262 e
= get_entry(table_base
, private->hook_entry
[hook
]);
263 back
= get_entry(table_base
, private->underflow
[hook
]);
267 if (arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
268 struct arpt_entry_target
*t
;
271 hdr_len
= sizeof(*arp
) + (2 * sizeof(struct in_addr
)) +
272 (2 * skb
->dev
->addr_len
);
274 ADD_COUNTER(e
->counters
, hdr_len
, 1);
276 t
= arpt_get_target(e
);
278 /* Standard target? */
279 if (!t
->u
.kernel
.target
->target
) {
282 v
= ((struct arpt_standard_target
*)t
)->verdict
;
284 /* Pop from stack? */
285 if (v
!= ARPT_RETURN
) {
286 verdict
= (unsigned)(-v
) - 1;
290 back
= get_entry(table_base
,
295 != (void *)e
+ e
->next_offset
) {
296 /* Save old back ptr in next entry */
297 struct arpt_entry
*next
298 = (void *)e
+ e
->next_offset
;
300 (void *)back
- table_base
;
302 /* set back pointer to next entry */
306 e
= get_entry(table_base
, v
);
308 /* Targets which reenter must return
311 verdict
= t
->u
.kernel
.target
->target(skb
,
317 /* Target might have changed stuff. */
320 if (verdict
== ARPT_CONTINUE
)
321 e
= (void *)e
+ e
->next_offset
;
327 e
= (void *)e
+ e
->next_offset
;
330 xt_info_rdunlock_bh();
338 /* All zeroes == unconditional rule. */
339 static inline int unconditional(const struct arpt_arp
*arp
)
343 for (i
= 0; i
< sizeof(*arp
)/sizeof(__u32
); i
++)
344 if (((__u32
*)arp
)[i
])
350 /* Figures out from what hook each rule can be called: returns 0 if
351 * there are loops. Puts hook bitmask in comefrom.
353 static int mark_source_chains(struct xt_table_info
*newinfo
,
354 unsigned int valid_hooks
, void *entry0
)
358 /* No recursion; use packet counter to save back ptrs (reset
359 * to 0 as we leave), and comefrom to save source hook bitmask.
361 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
362 unsigned int pos
= newinfo
->hook_entry
[hook
];
364 = (struct arpt_entry
*)(entry0
+ pos
);
366 if (!(valid_hooks
& (1 << hook
)))
369 /* Set initial back pointer. */
370 e
->counters
.pcnt
= pos
;
373 struct arpt_standard_target
*t
374 = (void *)arpt_get_target(e
);
375 int visited
= e
->comefrom
& (1 << hook
);
377 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
378 printk("arptables: loop hook %u pos %u %08X.\n",
379 hook
, pos
, e
->comefrom
);
383 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
385 /* Unconditional return/END. */
386 if ((e
->target_offset
== sizeof(struct arpt_entry
)
387 && (strcmp(t
->target
.u
.user
.name
,
388 ARPT_STANDARD_TARGET
) == 0)
390 && unconditional(&e
->arp
)) || visited
) {
391 unsigned int oldpos
, size
;
393 if ((strcmp(t
->target
.u
.user
.name
,
394 ARPT_STANDARD_TARGET
) == 0) &&
395 t
->verdict
< -NF_MAX_VERDICT
- 1) {
396 duprintf("mark_source_chains: bad "
397 "negative verdict (%i)\n",
402 /* Return: backtrack through the last
406 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
408 pos
= e
->counters
.pcnt
;
409 e
->counters
.pcnt
= 0;
411 /* We're at the start. */
415 e
= (struct arpt_entry
*)
417 } while (oldpos
== pos
+ e
->next_offset
);
420 size
= e
->next_offset
;
421 e
= (struct arpt_entry
*)
422 (entry0
+ pos
+ size
);
423 e
->counters
.pcnt
= pos
;
426 int newpos
= t
->verdict
;
428 if (strcmp(t
->target
.u
.user
.name
,
429 ARPT_STANDARD_TARGET
) == 0
431 if (newpos
> newinfo
->size
-
432 sizeof(struct arpt_entry
)) {
433 duprintf("mark_source_chains: "
434 "bad verdict (%i)\n",
439 /* This a jump; chase it. */
440 duprintf("Jump rule %u -> %u\n",
443 /* ... this is a fallthru */
444 newpos
= pos
+ e
->next_offset
;
446 e
= (struct arpt_entry
*)
448 e
->counters
.pcnt
= pos
;
453 duprintf("Finished chain %u\n", hook
);
458 static inline int check_entry(struct arpt_entry
*e
, const char *name
)
460 struct arpt_entry_target
*t
;
462 if (!arp_checkentry(&e
->arp
)) {
463 duprintf("arp_tables: arp check failed %p %s.\n", e
, name
);
467 if (e
->target_offset
+ sizeof(struct arpt_entry_target
) > e
->next_offset
)
470 t
= arpt_get_target(e
);
471 if (e
->target_offset
+ t
->u
.target_size
> e
->next_offset
)
477 static inline int check_target(struct arpt_entry
*e
, const char *name
)
479 struct arpt_entry_target
*t
;
480 struct arpt_target
*target
;
483 t
= arpt_get_target(e
);
484 target
= t
->u
.kernel
.target
;
486 ret
= xt_check_target(target
, NF_ARP
, t
->u
.target_size
- sizeof(*t
),
487 name
, e
->comefrom
, 0, 0);
488 if (!ret
&& t
->u
.kernel
.target
->checkentry
489 && !t
->u
.kernel
.target
->checkentry(name
, e
, target
, t
->data
,
491 duprintf("arp_tables: check failed for `%s'.\n",
492 t
->u
.kernel
.target
->name
);
499 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
,
502 struct arpt_entry_target
*t
;
503 struct arpt_target
*target
;
506 ret
= check_entry(e
, name
);
510 t
= arpt_get_target(e
);
511 target
= try_then_request_module(xt_find_target(NF_ARP
, t
->u
.user
.name
,
513 "arpt_%s", t
->u
.user
.name
);
514 if (IS_ERR(target
) || !target
) {
515 duprintf("find_check_entry: `%s' not found\n", t
->u
.user
.name
);
516 ret
= target
? PTR_ERR(target
) : -ENOENT
;
519 t
->u
.kernel
.target
= target
;
521 ret
= check_target(e
, name
);
528 module_put(t
->u
.kernel
.target
->me
);
533 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
534 struct xt_table_info
*newinfo
,
536 unsigned char *limit
,
537 const unsigned int *hook_entries
,
538 const unsigned int *underflows
,
543 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0
544 || (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
) {
545 duprintf("Bad offset %p\n", e
);
550 < sizeof(struct arpt_entry
) + sizeof(struct arpt_entry_target
)) {
551 duprintf("checking: element %p size %u\n",
556 /* Check hooks & underflows */
557 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
558 if ((unsigned char *)e
- base
== hook_entries
[h
])
559 newinfo
->hook_entry
[h
] = hook_entries
[h
];
560 if ((unsigned char *)e
- base
== underflows
[h
])
561 newinfo
->underflow
[h
] = underflows
[h
];
564 /* FIXME: underflows must be unconditional, standard verdicts
565 < 0 (not ARPT_RETURN). --RR */
567 /* Clear counters and comefrom */
568 e
->counters
= ((struct xt_counters
) { 0, 0 });
575 static inline int cleanup_entry(struct arpt_entry
*e
, unsigned int *i
)
577 struct arpt_entry_target
*t
;
579 if (i
&& (*i
)-- == 0)
582 t
= arpt_get_target(e
);
583 if (t
->u
.kernel
.target
->destroy
)
584 t
->u
.kernel
.target
->destroy(t
->u
.kernel
.target
, t
->data
);
585 module_put(t
->u
.kernel
.target
->me
);
589 /* Checks and translates the user-supplied table segment (held in
592 static int translate_table(const char *name
,
593 unsigned int valid_hooks
,
594 struct xt_table_info
*newinfo
,
598 const unsigned int *hook_entries
,
599 const unsigned int *underflows
)
604 newinfo
->size
= size
;
605 newinfo
->number
= number
;
607 /* Init all hooks to impossible value. */
608 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
609 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
610 newinfo
->underflow
[i
] = 0xFFFFFFFF;
613 duprintf("translate_table: size %u\n", newinfo
->size
);
616 /* Walk through entries, checking offsets. */
617 ret
= ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
618 check_entry_size_and_hooks
,
622 hook_entries
, underflows
, &i
);
623 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
628 duprintf("translate_table: %u not %u entries\n",
633 /* Check hooks all assigned */
634 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
635 /* Only hooks which are valid */
636 if (!(valid_hooks
& (1 << i
)))
638 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
639 duprintf("Invalid hook entry %u %u\n",
643 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
644 duprintf("Invalid underflow %u %u\n",
650 if (!mark_source_chains(newinfo
, valid_hooks
, entry0
)) {
651 duprintf("Looping hook\n");
655 /* Finally, each sanity check must pass */
657 ret
= ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
658 find_check_entry
, name
, size
, &i
);
661 ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
666 /* And one copy for every other CPU */
667 for_each_possible_cpu(i
) {
668 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry0
)
669 memcpy(newinfo
->entries
[i
], entry0
, newinfo
->size
);
676 static inline int add_entry_to_counter(const struct arpt_entry
*e
,
677 struct xt_counters total
[],
680 ADD_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
686 static inline int set_entry_to_counter(const struct arpt_entry
*e
,
687 struct xt_counters total
[],
690 SET_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
696 static void get_counters(const struct xt_table_info
*t
,
697 struct xt_counters counters
[])
703 /* Instead of clearing (by a previous call to memset())
704 * the counters and using adds, we set the counters
705 * with data used by 'current' CPU
707 * Bottom half has to be disabled to prevent deadlock
708 * if new softirq were to run and call ipt_do_table
711 curcpu
= smp_processor_id();
714 ARPT_ENTRY_ITERATE(t
->entries
[curcpu
],
716 set_entry_to_counter
,
720 for_each_possible_cpu(cpu
) {
725 ARPT_ENTRY_ITERATE(t
->entries
[cpu
],
727 add_entry_to_counter
,
730 xt_info_wrunlock(cpu
);
735 static struct xt_counters
*alloc_counters(struct arpt_table
*table
)
737 unsigned int countersize
;
738 struct xt_counters
*counters
;
739 struct xt_table_info
*private = table
->private;
741 /* We need atomic snapshot of counters: rest doesn't change
742 * (other than comefrom, which userspace doesn't care
745 countersize
= sizeof(struct xt_counters
) * private->number
;
746 counters
= vmalloc(countersize
);
748 if (counters
== NULL
)
749 return ERR_PTR(-ENOMEM
);
751 get_counters(private, counters
);
756 static int copy_entries_to_user(unsigned int total_size
,
757 struct arpt_table
*table
,
758 void __user
*userptr
)
760 unsigned int off
, num
;
761 struct arpt_entry
*e
;
762 struct xt_counters
*counters
;
763 struct xt_table_info
*private = table
->private;
767 counters
= alloc_counters(table
);
768 if (IS_ERR(counters
))
769 return PTR_ERR(counters
);
771 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
772 /* ... then copy entire thing ... */
773 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
778 /* FIXME: use iterator macros --RR */
779 /* ... then go back and fix counters and names */
780 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
781 struct arpt_entry_target
*t
;
783 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
784 if (copy_to_user(userptr
+ off
785 + offsetof(struct arpt_entry
, counters
),
787 sizeof(counters
[num
])) != 0) {
792 t
= arpt_get_target(e
);
793 if (copy_to_user(userptr
+ off
+ e
->target_offset
794 + offsetof(struct arpt_entry_target
,
796 t
->u
.kernel
.target
->name
,
797 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
808 static int get_entries(const struct arpt_get_entries
*entries
,
809 struct arpt_get_entries __user
*uptr
)
812 struct arpt_table
*t
;
814 t
= xt_find_table_lock(NF_ARP
, entries
->name
);
815 if (t
&& !IS_ERR(t
)) {
816 struct xt_table_info
*private = t
->private;
817 duprintf("t->private->number = %u\n",
819 if (entries
->size
== private->size
)
820 ret
= copy_entries_to_user(private->size
,
821 t
, uptr
->entrytable
);
823 duprintf("get_entries: I've got %u not %u!\n",
824 private->size
, entries
->size
);
830 ret
= t
? PTR_ERR(t
) : -ENOENT
;
835 static int do_replace(void __user
*user
, unsigned int len
)
838 struct arpt_replace tmp
;
839 struct arpt_table
*t
;
840 struct xt_table_info
*newinfo
, *oldinfo
;
841 struct xt_counters
*counters
;
842 void *loc_cpu_entry
, *loc_cpu_old_entry
;
844 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
848 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
851 newinfo
= xt_alloc_table_info(tmp
.size
);
855 /* choose the copy that is on our node/cpu */
856 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
857 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
863 counters
= vmalloc(tmp
.num_counters
* sizeof(struct xt_counters
));
869 ret
= translate_table(tmp
.name
, tmp
.valid_hooks
,
870 newinfo
, loc_cpu_entry
, tmp
.size
, tmp
.num_entries
,
871 tmp
.hook_entry
, tmp
.underflow
);
873 goto free_newinfo_counters
;
875 duprintf("arp_tables: Translated table\n");
877 t
= try_then_request_module(xt_find_table_lock(NF_ARP
, tmp
.name
),
878 "arptable_%s", tmp
.name
);
879 if (!t
|| IS_ERR(t
)) {
880 ret
= t
? PTR_ERR(t
) : -ENOENT
;
881 goto free_newinfo_counters_untrans
;
885 if (tmp
.valid_hooks
!= t
->valid_hooks
) {
886 duprintf("Valid hook crap: %08X vs %08X\n",
887 tmp
.valid_hooks
, t
->valid_hooks
);
892 oldinfo
= xt_replace_table(t
, tmp
.num_counters
, newinfo
, &ret
);
896 /* Update module usage count based on number of rules */
897 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
898 oldinfo
->number
, oldinfo
->initial_entries
, newinfo
->number
);
899 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
900 (newinfo
->number
<= oldinfo
->initial_entries
))
902 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
903 (newinfo
->number
<= oldinfo
->initial_entries
))
906 /* Get the old counters, and synchronize with replace */
907 get_counters(oldinfo
, counters
);
909 /* Decrease module usage counts and free resource */
910 loc_cpu_old_entry
= oldinfo
->entries
[raw_smp_processor_id()];
911 ARPT_ENTRY_ITERATE(loc_cpu_old_entry
, oldinfo
->size
, cleanup_entry
,NULL
);
913 xt_free_table_info(oldinfo
);
914 if (copy_to_user(tmp
.counters
, counters
,
915 sizeof(struct xt_counters
) * tmp
.num_counters
) != 0)
924 free_newinfo_counters_untrans
:
925 ARPT_ENTRY_ITERATE(loc_cpu_entry
, newinfo
->size
, cleanup_entry
, NULL
);
926 free_newinfo_counters
:
929 xt_free_table_info(newinfo
);
933 /* We're lazy, and add to the first CPU; overflow works its fey magic
934 * and everything is OK. */
936 add_counter_to_entry(struct arpt_entry
*e
,
937 const struct xt_counters addme
[],
940 ADD_COUNTER(e
->counters
, addme
[*i
].bcnt
, addme
[*i
].pcnt
);
946 static int do_add_counters(void __user
*user
, unsigned int len
)
948 unsigned int i
, curcpu
;
949 struct xt_counters_info tmp
, *paddc
;
950 struct arpt_table
*t
;
951 struct xt_table_info
*private;
955 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
958 if (len
!= sizeof(tmp
) + tmp
.num_counters
*sizeof(struct xt_counters
))
961 paddc
= vmalloc(len
);
965 if (copy_from_user(paddc
, user
, len
) != 0) {
970 t
= xt_find_table_lock(NF_ARP
, tmp
.name
);
971 if (!t
|| IS_ERR(t
)) {
972 ret
= t
? PTR_ERR(t
) : -ENOENT
;
977 private = t
->private;
978 if (private->number
!= tmp
.num_counters
) {
984 /* Choose the copy that is on our node */
985 curcpu
= smp_processor_id();
986 loc_cpu_entry
= private->entries
[curcpu
];
987 xt_info_wrlock(curcpu
);
988 ARPT_ENTRY_ITERATE(loc_cpu_entry
,
990 add_counter_to_entry
,
993 xt_info_wrunlock(curcpu
);
1004 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1008 if (!capable(CAP_NET_ADMIN
))
1012 case ARPT_SO_SET_REPLACE
:
1013 ret
= do_replace(user
, len
);
1016 case ARPT_SO_SET_ADD_COUNTERS
:
1017 ret
= do_add_counters(user
, len
);
1021 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1028 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1032 if (!capable(CAP_NET_ADMIN
))
1036 case ARPT_SO_GET_INFO
: {
1037 char name
[ARPT_TABLE_MAXNAMELEN
];
1038 struct arpt_table
*t
;
1040 if (*len
!= sizeof(struct arpt_getinfo
)) {
1041 duprintf("length %u != %Zu\n", *len
,
1042 sizeof(struct arpt_getinfo
));
1047 if (copy_from_user(name
, user
, sizeof(name
)) != 0) {
1051 name
[ARPT_TABLE_MAXNAMELEN
-1] = '\0';
1053 t
= try_then_request_module(xt_find_table_lock(NF_ARP
, name
),
1054 "arptable_%s", name
);
1055 if (t
&& !IS_ERR(t
)) {
1056 struct arpt_getinfo info
;
1057 struct xt_table_info
*private = t
->private;
1059 info
.valid_hooks
= t
->valid_hooks
;
1060 memcpy(info
.hook_entry
, private->hook_entry
,
1061 sizeof(info
.hook_entry
));
1062 memcpy(info
.underflow
, private->underflow
,
1063 sizeof(info
.underflow
));
1064 info
.num_entries
= private->number
;
1065 info
.size
= private->size
;
1066 strcpy(info
.name
, name
);
1068 if (copy_to_user(user
, &info
, *len
) != 0)
1075 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1079 case ARPT_SO_GET_ENTRIES
: {
1080 struct arpt_get_entries get
;
1082 if (*len
< sizeof(get
)) {
1083 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
1085 } else if (copy_from_user(&get
, user
, sizeof(get
)) != 0) {
1087 } else if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
1088 duprintf("get_entries: %u != %Zu\n", *len
,
1089 sizeof(struct arpt_get_entries
) + get
.size
);
1092 ret
= get_entries(&get
, user
);
1096 case ARPT_SO_GET_REVISION_TARGET
: {
1097 struct xt_get_revision rev
;
1099 if (*len
!= sizeof(rev
)) {
1103 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1108 try_then_request_module(xt_find_revision(NF_ARP
, rev
.name
,
1109 rev
.revision
, 1, &ret
),
1110 "arpt_%s", rev
.name
);
1115 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1122 int arpt_register_table(struct arpt_table
*table
,
1123 const struct arpt_replace
*repl
)
1126 struct xt_table_info
*newinfo
;
1127 struct xt_table_info bootstrap
1128 = { 0, 0, 0, { 0 }, { 0 }, { } };
1129 void *loc_cpu_entry
;
1131 newinfo
= xt_alloc_table_info(repl
->size
);
1137 /* choose the copy on our node/cpu */
1138 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1139 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1141 ret
= translate_table(table
->name
, table
->valid_hooks
,
1142 newinfo
, loc_cpu_entry
, repl
->size
,
1147 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1149 xt_free_table_info(newinfo
);
1153 ret
= xt_register_table(table
, &bootstrap
, newinfo
);
1155 xt_free_table_info(newinfo
);
1162 void arpt_unregister_table(struct arpt_table
*table
)
1164 struct xt_table_info
*private;
1165 void *loc_cpu_entry
;
1167 private = xt_unregister_table(table
);
1169 /* Decrease module usage counts and free resources */
1170 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1171 ARPT_ENTRY_ITERATE(loc_cpu_entry
, private->size
,
1172 cleanup_entry
, NULL
);
1173 xt_free_table_info(private);
1176 /* The built-in targets: standard (NULL) and error. */
1177 static struct arpt_target arpt_standard_target
= {
1178 .name
= ARPT_STANDARD_TARGET
,
1179 .targetsize
= sizeof(int),
1183 static struct arpt_target arpt_error_target
= {
1184 .name
= ARPT_ERROR_TARGET
,
1185 .target
= arpt_error
,
1186 .targetsize
= ARPT_FUNCTION_MAXNAMELEN
,
1190 static struct nf_sockopt_ops arpt_sockopts
= {
1192 .set_optmin
= ARPT_BASE_CTL
,
1193 .set_optmax
= ARPT_SO_SET_MAX
+1,
1194 .set
= do_arpt_set_ctl
,
1195 .get_optmin
= ARPT_BASE_CTL
,
1196 .get_optmax
= ARPT_SO_GET_MAX
+1,
1197 .get
= do_arpt_get_ctl
,
1200 static int __init
arp_tables_init(void)
1204 ret
= xt_proto_init(NF_ARP
);
1208 /* Noone else will be downing sem now, so we won't sleep */
1209 ret
= xt_register_target(&arpt_standard_target
);
1212 ret
= xt_register_target(&arpt_error_target
);
1216 /* Register setsockopt */
1217 ret
= nf_register_sockopt(&arpt_sockopts
);
1221 printk("arp_tables: (C) 2002 David S. Miller\n");
1225 xt_unregister_target(&arpt_error_target
);
1227 xt_unregister_target(&arpt_standard_target
);
1229 xt_proto_fini(NF_ARP
);
1234 static void __exit
arp_tables_fini(void)
1236 nf_unregister_sockopt(&arpt_sockopts
);
1237 xt_unregister_target(&arpt_error_target
);
1238 xt_unregister_target(&arpt_standard_target
);
1239 xt_proto_fini(NF_ARP
);
1242 EXPORT_SYMBOL(arpt_register_table
);
1243 EXPORT_SYMBOL(arpt_unregister_table
);
1244 EXPORT_SYMBOL(arpt_do_table
);
1246 module_init(arp_tables_init
);
1247 module_exit(arp_tables_fini
);