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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/capability.h>
17 #include <linux/if_arp.h>
18 #include <linux/kmod.h>
19 #include <linux/vmalloc.h>
20 #include <linux/proc_fs.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <asm/semaphore.h>
27 #include <linux/netfilter/x_tables.h>
28 #include <linux/netfilter_arp/arp_tables.h>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
32 MODULE_DESCRIPTION("arptables core");
34 /*#define DEBUG_ARP_TABLES*/
35 /*#define DEBUG_ARP_TABLES_USER*/
37 #ifdef DEBUG_ARP_TABLES
38 #define dprintf(format, args...) printk(format , ## args)
40 #define dprintf(format, args...)
43 #ifdef DEBUG_ARP_TABLES_USER
44 #define duprintf(format, args...) printk(format , ## args)
46 #define duprintf(format, args...)
49 #ifdef CONFIG_NETFILTER_DEBUG
50 #define ARP_NF_ASSERT(x) \
53 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
54 __FUNCTION__, __FILE__, __LINE__); \
57 #define ARP_NF_ASSERT(x)
60 #include <linux/netfilter_ipv4/listhelp.h>
62 static inline int arp_devaddr_compare(const struct arpt_devaddr_info
*ap
,
63 char *hdr_addr
, int len
)
67 if (len
> ARPT_DEV_ADDR_LEN_MAX
)
68 len
= ARPT_DEV_ADDR_LEN_MAX
;
71 for (i
= 0; i
< len
; i
++)
72 ret
|= (hdr_addr
[i
] ^ ap
->addr
[i
]) & ap
->mask
[i
];
77 /* Returns whether packet matches rule or not. */
78 static inline int arp_packet_match(const struct arphdr
*arphdr
,
79 struct net_device
*dev
,
82 const struct arpt_arp
*arpinfo
)
84 char *arpptr
= (char *)(arphdr
+ 1);
85 char *src_devaddr
, *tgt_devaddr
;
86 u32 src_ipaddr
, tgt_ipaddr
;
89 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
91 if (FWINV((arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
,
93 dprintf("ARP operation field mismatch.\n");
94 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
95 arphdr
->ar_op
, arpinfo
->arpop
, arpinfo
->arpop_mask
);
99 if (FWINV((arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
,
101 dprintf("ARP hardware address format mismatch.\n");
102 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
103 arphdr
->ar_hrd
, arpinfo
->arhrd
, arpinfo
->arhrd_mask
);
107 if (FWINV((arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
,
109 dprintf("ARP protocol address format mismatch.\n");
110 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
111 arphdr
->ar_pro
, arpinfo
->arpro
, arpinfo
->arpro_mask
);
115 if (FWINV((arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
,
117 dprintf("ARP hardware address length mismatch.\n");
118 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
119 arphdr
->ar_hln
, arpinfo
->arhln
, arpinfo
->arhln_mask
);
123 src_devaddr
= arpptr
;
124 arpptr
+= dev
->addr_len
;
125 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
126 arpptr
+= sizeof(u32
);
127 tgt_devaddr
= arpptr
;
128 arpptr
+= dev
->addr_len
;
129 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
131 if (FWINV(arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
, dev
->addr_len
),
132 ARPT_INV_SRCDEVADDR
) ||
133 FWINV(arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
, dev
->addr_len
),
134 ARPT_INV_TGTDEVADDR
)) {
135 dprintf("Source or target device address mismatch.\n");
140 if (FWINV((src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
,
142 FWINV(((tgt_ipaddr
& arpinfo
->tmsk
.s_addr
) != arpinfo
->tgt
.s_addr
),
144 dprintf("Source or target IP address mismatch.\n");
146 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
148 NIPQUAD(arpinfo
->smsk
.s_addr
),
149 NIPQUAD(arpinfo
->src
.s_addr
),
150 arpinfo
->invflags
& ARPT_INV_SRCIP
? " (INV)" : "");
151 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
153 NIPQUAD(arpinfo
->tmsk
.s_addr
),
154 NIPQUAD(arpinfo
->tgt
.s_addr
),
155 arpinfo
->invflags
& ARPT_INV_TGTIP
? " (INV)" : "");
159 /* Look for ifname matches. */
160 for (i
= 0, ret
= 0; i
< IFNAMSIZ
; i
++) {
161 ret
|= (indev
[i
] ^ arpinfo
->iniface
[i
])
162 & arpinfo
->iniface_mask
[i
];
165 if (FWINV(ret
!= 0, ARPT_INV_VIA_IN
)) {
166 dprintf("VIA in mismatch (%s vs %s).%s\n",
167 indev
, arpinfo
->iniface
,
168 arpinfo
->invflags
&ARPT_INV_VIA_IN
?" (INV)":"");
172 for (i
= 0, ret
= 0; i
< IFNAMSIZ
/sizeof(unsigned long); i
++) {
174 memcpy(&odev
, outdev
+ i
*sizeof(unsigned long),
175 sizeof(unsigned long));
177 ^ ((const unsigned long *)arpinfo
->outiface
)[i
])
178 & ((const unsigned long *)arpinfo
->outiface_mask
)[i
];
181 if (FWINV(ret
!= 0, ARPT_INV_VIA_OUT
)) {
182 dprintf("VIA out mismatch (%s vs %s).%s\n",
183 outdev
, arpinfo
->outiface
,
184 arpinfo
->invflags
&ARPT_INV_VIA_OUT
?" (INV)":"");
191 static inline int arp_checkentry(const struct arpt_arp
*arp
)
193 if (arp
->flags
& ~ARPT_F_MASK
) {
194 duprintf("Unknown flag bits set: %08X\n",
195 arp
->flags
& ~ARPT_F_MASK
);
198 if (arp
->invflags
& ~ARPT_INV_MASK
) {
199 duprintf("Unknown invflag bits set: %08X\n",
200 arp
->invflags
& ~ARPT_INV_MASK
);
207 static unsigned int arpt_error(struct sk_buff
**pskb
,
208 const struct net_device
*in
,
209 const struct net_device
*out
,
210 unsigned int hooknum
,
211 const void *targinfo
,
215 printk("arp_tables: error: '%s'\n", (char *)targinfo
);
220 static inline struct arpt_entry
*get_entry(void *base
, unsigned int offset
)
222 return (struct arpt_entry
*)(base
+ offset
);
225 unsigned int arpt_do_table(struct sk_buff
**pskb
,
227 const struct net_device
*in
,
228 const struct net_device
*out
,
229 struct arpt_table
*table
,
232 static const char nulldevname
[IFNAMSIZ
];
233 unsigned int verdict
= NF_DROP
;
236 struct arpt_entry
*e
, *back
;
237 const char *indev
, *outdev
;
239 struct xt_table_info
*private = table
->private;
241 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
242 if (!pskb_may_pull((*pskb
), (sizeof(struct arphdr
) +
243 (2 * (*pskb
)->dev
->addr_len
) +
247 indev
= in
? in
->name
: nulldevname
;
248 outdev
= out
? out
->name
: nulldevname
;
250 read_lock_bh(&table
->lock
);
251 table_base
= (void *)private->entries
[smp_processor_id()];
252 e
= get_entry(table_base
, private->hook_entry
[hook
]);
253 back
= get_entry(table_base
, private->underflow
[hook
]);
255 arp
= (*pskb
)->nh
.arph
;
257 if (arp_packet_match(arp
, (*pskb
)->dev
, indev
, outdev
, &e
->arp
)) {
258 struct arpt_entry_target
*t
;
261 hdr_len
= sizeof(*arp
) + (2 * sizeof(struct in_addr
)) +
262 (2 * (*pskb
)->dev
->addr_len
);
263 ADD_COUNTER(e
->counters
, hdr_len
, 1);
265 t
= arpt_get_target(e
);
267 /* Standard target? */
268 if (!t
->u
.kernel
.target
->target
) {
271 v
= ((struct arpt_standard_target
*)t
)->verdict
;
273 /* Pop from stack? */
274 if (v
!= ARPT_RETURN
) {
275 verdict
= (unsigned)(-v
) - 1;
279 back
= get_entry(table_base
,
284 != (void *)e
+ e
->next_offset
) {
285 /* Save old back ptr in next entry */
286 struct arpt_entry
*next
287 = (void *)e
+ e
->next_offset
;
289 (void *)back
- table_base
;
291 /* set back pointer to next entry */
295 e
= get_entry(table_base
, v
);
297 /* Targets which reenter must return
300 verdict
= t
->u
.kernel
.target
->target(pskb
,
306 /* Target might have changed stuff. */
307 arp
= (*pskb
)->nh
.arph
;
309 if (verdict
== ARPT_CONTINUE
)
310 e
= (void *)e
+ e
->next_offset
;
316 e
= (void *)e
+ e
->next_offset
;
319 read_unlock_bh(&table
->lock
);
327 /* All zeroes == unconditional rule. */
328 static inline int unconditional(const struct arpt_arp
*arp
)
332 for (i
= 0; i
< sizeof(*arp
)/sizeof(__u32
); i
++)
333 if (((__u32
*)arp
)[i
])
339 /* Figures out from what hook each rule can be called: returns 0 if
340 * there are loops. Puts hook bitmask in comefrom.
342 static int mark_source_chains(struct xt_table_info
*newinfo
,
343 unsigned int valid_hooks
, void *entry0
)
347 /* No recursion; use packet counter to save back ptrs (reset
348 * to 0 as we leave), and comefrom to save source hook bitmask.
350 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
351 unsigned int pos
= newinfo
->hook_entry
[hook
];
353 = (struct arpt_entry
*)(entry0
+ pos
);
355 if (!(valid_hooks
& (1 << hook
)))
358 /* Set initial back pointer. */
359 e
->counters
.pcnt
= pos
;
362 struct arpt_standard_target
*t
363 = (void *)arpt_get_target(e
);
365 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
366 printk("arptables: loop hook %u pos %u %08X.\n",
367 hook
, pos
, e
->comefrom
);
371 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
373 /* Unconditional return/END. */
374 if (e
->target_offset
== sizeof(struct arpt_entry
)
375 && (strcmp(t
->target
.u
.user
.name
,
376 ARPT_STANDARD_TARGET
) == 0)
378 && unconditional(&e
->arp
)) {
379 unsigned int oldpos
, size
;
381 /* Return: backtrack through the last
385 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
387 pos
= e
->counters
.pcnt
;
388 e
->counters
.pcnt
= 0;
390 /* We're at the start. */
394 e
= (struct arpt_entry
*)
396 } while (oldpos
== pos
+ e
->next_offset
);
399 size
= e
->next_offset
;
400 e
= (struct arpt_entry
*)
401 (entry0
+ pos
+ size
);
402 e
->counters
.pcnt
= pos
;
405 int newpos
= t
->verdict
;
407 if (strcmp(t
->target
.u
.user
.name
,
408 ARPT_STANDARD_TARGET
) == 0
410 /* This a jump; chase it. */
411 duprintf("Jump rule %u -> %u\n",
414 /* ... this is a fallthru */
415 newpos
= pos
+ e
->next_offset
;
417 e
= (struct arpt_entry
*)
419 e
->counters
.pcnt
= pos
;
424 duprintf("Finished chain %u\n", hook
);
429 static inline int standard_check(const struct arpt_entry_target
*t
,
430 unsigned int max_offset
)
432 struct arpt_standard_target
*targ
= (void *)t
;
434 /* Check standard info. */
436 != ARPT_ALIGN(sizeof(struct arpt_standard_target
))) {
437 duprintf("arpt_standard_check: target size %u != %Zu\n",
439 ARPT_ALIGN(sizeof(struct arpt_standard_target
)));
443 if (targ
->verdict
>= 0
444 && targ
->verdict
> max_offset
- sizeof(struct arpt_entry
)) {
445 duprintf("arpt_standard_check: bad verdict (%i)\n",
450 if (targ
->verdict
< -NF_MAX_VERDICT
- 1) {
451 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
458 static struct arpt_target arpt_standard_target
;
460 static inline int check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
,
463 struct arpt_entry_target
*t
;
464 struct arpt_target
*target
;
467 if (!arp_checkentry(&e
->arp
)) {
468 duprintf("arp_tables: arp check failed %p %s.\n", e
, name
);
472 t
= arpt_get_target(e
);
473 target
= try_then_request_module(xt_find_target(NF_ARP
, t
->u
.user
.name
,
475 "arpt_%s", t
->u
.user
.name
);
476 if (IS_ERR(target
) || !target
) {
477 duprintf("check_entry: `%s' not found\n", t
->u
.user
.name
);
478 ret
= target
? PTR_ERR(target
) : -ENOENT
;
481 t
->u
.kernel
.target
= target
;
483 if (t
->u
.kernel
.target
== &arpt_standard_target
) {
484 if (!standard_check(t
, size
)) {
488 } else if (t
->u
.kernel
.target
->checkentry
489 && !t
->u
.kernel
.target
->checkentry(name
, e
, t
->data
,
493 module_put(t
->u
.kernel
.target
->me
);
494 duprintf("arp_tables: check failed for `%s'.\n",
495 t
->u
.kernel
.target
->name
);
507 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
508 struct xt_table_info
*newinfo
,
510 unsigned char *limit
,
511 const unsigned int *hook_entries
,
512 const unsigned int *underflows
,
517 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0
518 || (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
) {
519 duprintf("Bad offset %p\n", e
);
524 < sizeof(struct arpt_entry
) + sizeof(struct arpt_entry_target
)) {
525 duprintf("checking: element %p size %u\n",
530 /* Check hooks & underflows */
531 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
532 if ((unsigned char *)e
- base
== hook_entries
[h
])
533 newinfo
->hook_entry
[h
] = hook_entries
[h
];
534 if ((unsigned char *)e
- base
== underflows
[h
])
535 newinfo
->underflow
[h
] = underflows
[h
];
538 /* FIXME: underflows must be unconditional, standard verdicts
539 < 0 (not ARPT_RETURN). --RR */
541 /* Clear counters and comefrom */
542 e
->counters
= ((struct xt_counters
) { 0, 0 });
549 static inline int cleanup_entry(struct arpt_entry
*e
, unsigned int *i
)
551 struct arpt_entry_target
*t
;
553 if (i
&& (*i
)-- == 0)
556 t
= arpt_get_target(e
);
557 if (t
->u
.kernel
.target
->destroy
)
558 t
->u
.kernel
.target
->destroy(t
->data
,
559 t
->u
.target_size
- sizeof(*t
));
560 module_put(t
->u
.kernel
.target
->me
);
564 /* Checks and translates the user-supplied table segment (held in
567 static int translate_table(const char *name
,
568 unsigned int valid_hooks
,
569 struct xt_table_info
*newinfo
,
573 const unsigned int *hook_entries
,
574 const unsigned int *underflows
)
579 newinfo
->size
= size
;
580 newinfo
->number
= number
;
582 /* Init all hooks to impossible value. */
583 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
584 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
585 newinfo
->underflow
[i
] = 0xFFFFFFFF;
588 duprintf("translate_table: size %u\n", newinfo
->size
);
591 /* Walk through entries, checking offsets. */
592 ret
= ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
593 check_entry_size_and_hooks
,
597 hook_entries
, underflows
, &i
);
598 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
603 duprintf("translate_table: %u not %u entries\n",
608 /* Check hooks all assigned */
609 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
610 /* Only hooks which are valid */
611 if (!(valid_hooks
& (1 << i
)))
613 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
614 duprintf("Invalid hook entry %u %u\n",
618 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
619 duprintf("Invalid underflow %u %u\n",
625 if (!mark_source_chains(newinfo
, valid_hooks
, entry0
)) {
626 duprintf("Looping hook\n");
630 /* Finally, each sanity check must pass */
632 ret
= ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
633 check_entry
, name
, size
, &i
);
636 ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
641 /* And one copy for every other CPU */
643 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry0
)
644 memcpy(newinfo
->entries
[i
], entry0
, newinfo
->size
);
651 static inline int add_entry_to_counter(const struct arpt_entry
*e
,
652 struct xt_counters total
[],
655 ADD_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
661 static inline int set_entry_to_counter(const struct arpt_entry
*e
,
662 struct xt_counters total
[],
665 SET_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
671 static void get_counters(const struct xt_table_info
*t
,
672 struct xt_counters counters
[])
678 /* Instead of clearing (by a previous call to memset())
679 * the counters and using adds, we set the counters
680 * with data used by 'current' CPU
681 * We dont care about preemption here.
683 curcpu
= raw_smp_processor_id();
686 ARPT_ENTRY_ITERATE(t
->entries
[curcpu
],
688 set_entry_to_counter
,
696 ARPT_ENTRY_ITERATE(t
->entries
[cpu
],
698 add_entry_to_counter
,
704 static int copy_entries_to_user(unsigned int total_size
,
705 struct arpt_table
*table
,
706 void __user
*userptr
)
708 unsigned int off
, num
, countersize
;
709 struct arpt_entry
*e
;
710 struct xt_counters
*counters
;
711 struct xt_table_info
*private = table
->private;
715 /* We need atomic snapshot of counters: rest doesn't change
716 * (other than comefrom, which userspace doesn't care
719 countersize
= sizeof(struct xt_counters
) * private->number
;
720 counters
= vmalloc_node(countersize
, numa_node_id());
722 if (counters
== NULL
)
725 /* First, sum counters... */
726 write_lock_bh(&table
->lock
);
727 get_counters(private, counters
);
728 write_unlock_bh(&table
->lock
);
730 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
731 /* ... then copy entire thing ... */
732 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
737 /* FIXME: use iterator macros --RR */
738 /* ... then go back and fix counters and names */
739 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
740 struct arpt_entry_target
*t
;
742 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
743 if (copy_to_user(userptr
+ off
744 + offsetof(struct arpt_entry
, counters
),
746 sizeof(counters
[num
])) != 0) {
751 t
= arpt_get_target(e
);
752 if (copy_to_user(userptr
+ off
+ e
->target_offset
753 + offsetof(struct arpt_entry_target
,
755 t
->u
.kernel
.target
->name
,
756 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
767 static int get_entries(const struct arpt_get_entries
*entries
,
768 struct arpt_get_entries __user
*uptr
)
771 struct arpt_table
*t
;
773 t
= xt_find_table_lock(NF_ARP
, entries
->name
);
774 if (t
|| !IS_ERR(t
)) {
775 struct xt_table_info
*private = t
->private;
776 duprintf("t->private->number = %u\n",
778 if (entries
->size
== private->size
)
779 ret
= copy_entries_to_user(private->size
,
780 t
, uptr
->entrytable
);
782 duprintf("get_entries: I've got %u not %u!\n",
783 private->size
, entries
->size
);
789 ret
= t
? PTR_ERR(t
) : -ENOENT
;
794 static int do_replace(void __user
*user
, unsigned int len
)
797 struct arpt_replace tmp
;
798 struct arpt_table
*t
;
799 struct xt_table_info
*newinfo
, *oldinfo
;
800 struct xt_counters
*counters
;
801 void *loc_cpu_entry
, *loc_cpu_old_entry
;
803 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
806 /* Hack: Causes ipchains to give correct error msg --RR */
807 if (len
!= sizeof(tmp
) + tmp
.size
)
811 if (tmp
.size
>= (INT_MAX
- sizeof(struct xt_table_info
)) / NR_CPUS
-
814 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
817 newinfo
= xt_alloc_table_info(tmp
.size
);
821 /* choose the copy that is on our node/cpu */
822 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
823 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
829 counters
= vmalloc(tmp
.num_counters
* sizeof(struct xt_counters
));
835 ret
= translate_table(tmp
.name
, tmp
.valid_hooks
,
836 newinfo
, loc_cpu_entry
, tmp
.size
, tmp
.num_entries
,
837 tmp
.hook_entry
, tmp
.underflow
);
839 goto free_newinfo_counters
;
841 duprintf("arp_tables: Translated table\n");
843 t
= try_then_request_module(xt_find_table_lock(NF_ARP
, tmp
.name
),
844 "arptable_%s", tmp
.name
);
845 if (!t
|| IS_ERR(t
)) {
846 ret
= t
? PTR_ERR(t
) : -ENOENT
;
847 goto free_newinfo_counters_untrans
;
851 if (tmp
.valid_hooks
!= t
->valid_hooks
) {
852 duprintf("Valid hook crap: %08X vs %08X\n",
853 tmp
.valid_hooks
, t
->valid_hooks
);
858 oldinfo
= xt_replace_table(t
, tmp
.num_counters
, newinfo
, &ret
);
862 /* Update module usage count based on number of rules */
863 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
864 oldinfo
->number
, oldinfo
->initial_entries
, newinfo
->number
);
865 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
866 (newinfo
->number
<= oldinfo
->initial_entries
))
868 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
869 (newinfo
->number
<= oldinfo
->initial_entries
))
872 /* Get the old counters. */
873 get_counters(oldinfo
, counters
);
874 /* Decrease module usage counts and free resource */
875 loc_cpu_old_entry
= oldinfo
->entries
[raw_smp_processor_id()];
876 ARPT_ENTRY_ITERATE(loc_cpu_old_entry
, oldinfo
->size
, cleanup_entry
,NULL
);
878 xt_free_table_info(oldinfo
);
879 if (copy_to_user(tmp
.counters
, counters
,
880 sizeof(struct xt_counters
) * tmp
.num_counters
) != 0)
889 free_newinfo_counters_untrans
:
890 ARPT_ENTRY_ITERATE(loc_cpu_entry
, newinfo
->size
, cleanup_entry
, NULL
);
891 free_newinfo_counters
:
894 xt_free_table_info(newinfo
);
898 /* We're lazy, and add to the first CPU; overflow works its fey magic
899 * and everything is OK.
901 static inline int add_counter_to_entry(struct arpt_entry
*e
,
902 const struct xt_counters addme
[],
906 ADD_COUNTER(e
->counters
, addme
[*i
].bcnt
, addme
[*i
].pcnt
);
912 static int do_add_counters(void __user
*user
, unsigned int len
)
915 struct xt_counters_info tmp
, *paddc
;
916 struct arpt_table
*t
;
917 struct xt_table_info
*private;
921 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
924 if (len
!= sizeof(tmp
) + tmp
.num_counters
*sizeof(struct xt_counters
))
927 paddc
= vmalloc(len
);
931 if (copy_from_user(paddc
, user
, len
) != 0) {
936 t
= xt_find_table_lock(NF_ARP
, tmp
.name
);
937 if (!t
|| IS_ERR(t
)) {
938 ret
= t
? PTR_ERR(t
) : -ENOENT
;
942 write_lock_bh(&t
->lock
);
943 private = t
->private;
944 if (private->number
!= paddc
->num_counters
) {
950 /* Choose the copy that is on our node */
951 loc_cpu_entry
= private->entries
[smp_processor_id()];
952 ARPT_ENTRY_ITERATE(loc_cpu_entry
,
954 add_counter_to_entry
,
958 write_unlock_bh(&t
->lock
);
967 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
971 if (!capable(CAP_NET_ADMIN
))
975 case ARPT_SO_SET_REPLACE
:
976 ret
= do_replace(user
, len
);
979 case ARPT_SO_SET_ADD_COUNTERS
:
980 ret
= do_add_counters(user
, len
);
984 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
991 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
995 if (!capable(CAP_NET_ADMIN
))
999 case ARPT_SO_GET_INFO
: {
1000 char name
[ARPT_TABLE_MAXNAMELEN
];
1001 struct arpt_table
*t
;
1003 if (*len
!= sizeof(struct arpt_getinfo
)) {
1004 duprintf("length %u != %Zu\n", *len
,
1005 sizeof(struct arpt_getinfo
));
1010 if (copy_from_user(name
, user
, sizeof(name
)) != 0) {
1014 name
[ARPT_TABLE_MAXNAMELEN
-1] = '\0';
1016 t
= try_then_request_module(xt_find_table_lock(NF_ARP
, name
),
1017 "arptable_%s", name
);
1018 if (t
&& !IS_ERR(t
)) {
1019 struct arpt_getinfo info
;
1020 struct xt_table_info
*private = t
->private;
1022 info
.valid_hooks
= t
->valid_hooks
;
1023 memcpy(info
.hook_entry
, private->hook_entry
,
1024 sizeof(info
.hook_entry
));
1025 memcpy(info
.underflow
, private->underflow
,
1026 sizeof(info
.underflow
));
1027 info
.num_entries
= private->number
;
1028 info
.size
= private->size
;
1029 strcpy(info
.name
, name
);
1031 if (copy_to_user(user
, &info
, *len
) != 0)
1038 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1042 case ARPT_SO_GET_ENTRIES
: {
1043 struct arpt_get_entries get
;
1045 if (*len
< sizeof(get
)) {
1046 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
1048 } else if (copy_from_user(&get
, user
, sizeof(get
)) != 0) {
1050 } else if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
1051 duprintf("get_entries: %u != %Zu\n", *len
,
1052 sizeof(struct arpt_get_entries
) + get
.size
);
1055 ret
= get_entries(&get
, user
);
1059 case ARPT_SO_GET_REVISION_TARGET
: {
1060 struct xt_get_revision rev
;
1062 if (*len
!= sizeof(rev
)) {
1066 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1071 try_then_request_module(xt_find_revision(NF_ARP
, rev
.name
,
1072 rev
.revision
, 1, &ret
),
1073 "arpt_%s", rev
.name
);
1078 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1085 int arpt_register_table(struct arpt_table
*table
,
1086 const struct arpt_replace
*repl
)
1089 struct xt_table_info
*newinfo
;
1090 static struct xt_table_info bootstrap
1091 = { 0, 0, 0, { 0 }, { 0 }, { } };
1092 void *loc_cpu_entry
;
1094 newinfo
= xt_alloc_table_info(repl
->size
);
1100 /* choose the copy on our node/cpu */
1101 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1102 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1104 ret
= translate_table(table
->name
, table
->valid_hooks
,
1105 newinfo
, loc_cpu_entry
, repl
->size
,
1110 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1112 xt_free_table_info(newinfo
);
1116 if (xt_register_table(table
, &bootstrap
, newinfo
) != 0) {
1117 xt_free_table_info(newinfo
);
1124 void arpt_unregister_table(struct arpt_table
*table
)
1126 struct xt_table_info
*private;
1127 void *loc_cpu_entry
;
1129 private = xt_unregister_table(table
);
1131 /* Decrease module usage counts and free resources */
1132 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1133 ARPT_ENTRY_ITERATE(loc_cpu_entry
, private->size
,
1134 cleanup_entry
, NULL
);
1135 xt_free_table_info(private);
1138 /* The built-in targets: standard (NULL) and error. */
1139 static struct arpt_target arpt_standard_target
= {
1140 .name
= ARPT_STANDARD_TARGET
,
1143 static struct arpt_target arpt_error_target
= {
1144 .name
= ARPT_ERROR_TARGET
,
1145 .target
= arpt_error
,
1148 static struct nf_sockopt_ops arpt_sockopts
= {
1150 .set_optmin
= ARPT_BASE_CTL
,
1151 .set_optmax
= ARPT_SO_SET_MAX
+1,
1152 .set
= do_arpt_set_ctl
,
1153 .get_optmin
= ARPT_BASE_CTL
,
1154 .get_optmax
= ARPT_SO_GET_MAX
+1,
1155 .get
= do_arpt_get_ctl
,
1158 static int __init
init(void)
1162 xt_proto_init(NF_ARP
);
1164 /* Noone else will be downing sem now, so we won't sleep */
1165 xt_register_target(NF_ARP
, &arpt_standard_target
);
1166 xt_register_target(NF_ARP
, &arpt_error_target
);
1168 /* Register setsockopt */
1169 ret
= nf_register_sockopt(&arpt_sockopts
);
1171 duprintf("Unable to register sockopts.\n");
1175 printk("arp_tables: (C) 2002 David S. Miller\n");
1179 static void __exit
fini(void)
1181 nf_unregister_sockopt(&arpt_sockopts
);
1182 xt_proto_fini(NF_ARP
);
1185 EXPORT_SYMBOL(arpt_register_table
);
1186 EXPORT_SYMBOL(arpt_unregister_table
);
1187 EXPORT_SYMBOL(arpt_do_table
);