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 #include <linux/netfilter_ipv4/listhelp.h>
61 static inline int arp_devaddr_compare(const struct arpt_devaddr_info
*ap
,
62 char *hdr_addr
, int len
)
66 if (len
> ARPT_DEV_ADDR_LEN_MAX
)
67 len
= ARPT_DEV_ADDR_LEN_MAX
;
70 for (i
= 0; i
< len
; i
++)
71 ret
|= (hdr_addr
[i
] ^ ap
->addr
[i
]) & ap
->mask
[i
];
76 /* Returns whether packet matches rule or not. */
77 static inline int arp_packet_match(const struct arphdr
*arphdr
,
78 struct net_device
*dev
,
81 const struct arpt_arp
*arpinfo
)
83 char *arpptr
= (char *)(arphdr
+ 1);
84 char *src_devaddr
, *tgt_devaddr
;
85 u32 src_ipaddr
, tgt_ipaddr
;
88 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
90 if (FWINV((arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
,
92 dprintf("ARP operation field mismatch.\n");
93 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
94 arphdr
->ar_op
, arpinfo
->arpop
, arpinfo
->arpop_mask
);
98 if (FWINV((arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
,
100 dprintf("ARP hardware address format mismatch.\n");
101 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
102 arphdr
->ar_hrd
, arpinfo
->arhrd
, arpinfo
->arhrd_mask
);
106 if (FWINV((arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
,
108 dprintf("ARP protocol address format mismatch.\n");
109 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
110 arphdr
->ar_pro
, arpinfo
->arpro
, arpinfo
->arpro_mask
);
114 if (FWINV((arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
,
116 dprintf("ARP hardware address length mismatch.\n");
117 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
118 arphdr
->ar_hln
, arpinfo
->arhln
, arpinfo
->arhln_mask
);
122 src_devaddr
= arpptr
;
123 arpptr
+= dev
->addr_len
;
124 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
125 arpptr
+= sizeof(u32
);
126 tgt_devaddr
= arpptr
;
127 arpptr
+= dev
->addr_len
;
128 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
130 if (FWINV(arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
, dev
->addr_len
),
131 ARPT_INV_SRCDEVADDR
) ||
132 FWINV(arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
, dev
->addr_len
),
133 ARPT_INV_TGTDEVADDR
)) {
134 dprintf("Source or target device address mismatch.\n");
139 if (FWINV((src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
,
141 FWINV(((tgt_ipaddr
& arpinfo
->tmsk
.s_addr
) != arpinfo
->tgt
.s_addr
),
143 dprintf("Source or target IP address mismatch.\n");
145 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
147 NIPQUAD(arpinfo
->smsk
.s_addr
),
148 NIPQUAD(arpinfo
->src
.s_addr
),
149 arpinfo
->invflags
& ARPT_INV_SRCIP
? " (INV)" : "");
150 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
152 NIPQUAD(arpinfo
->tmsk
.s_addr
),
153 NIPQUAD(arpinfo
->tgt
.s_addr
),
154 arpinfo
->invflags
& ARPT_INV_TGTIP
? " (INV)" : "");
158 /* Look for ifname matches. */
159 for (i
= 0, ret
= 0; i
< IFNAMSIZ
; i
++) {
160 ret
|= (indev
[i
] ^ arpinfo
->iniface
[i
])
161 & arpinfo
->iniface_mask
[i
];
164 if (FWINV(ret
!= 0, ARPT_INV_VIA_IN
)) {
165 dprintf("VIA in mismatch (%s vs %s).%s\n",
166 indev
, arpinfo
->iniface
,
167 arpinfo
->invflags
&ARPT_INV_VIA_IN
?" (INV)":"");
171 for (i
= 0, ret
= 0; i
< IFNAMSIZ
/sizeof(unsigned long); i
++) {
173 memcpy(&odev
, outdev
+ i
*sizeof(unsigned long),
174 sizeof(unsigned long));
176 ^ ((const unsigned long *)arpinfo
->outiface
)[i
])
177 & ((const unsigned long *)arpinfo
->outiface_mask
)[i
];
180 if (FWINV(ret
!= 0, ARPT_INV_VIA_OUT
)) {
181 dprintf("VIA out mismatch (%s vs %s).%s\n",
182 outdev
, arpinfo
->outiface
,
183 arpinfo
->invflags
&ARPT_INV_VIA_OUT
?" (INV)":"");
190 static inline int arp_checkentry(const struct arpt_arp
*arp
)
192 if (arp
->flags
& ~ARPT_F_MASK
) {
193 duprintf("Unknown flag bits set: %08X\n",
194 arp
->flags
& ~ARPT_F_MASK
);
197 if (arp
->invflags
& ~ARPT_INV_MASK
) {
198 duprintf("Unknown invflag bits set: %08X\n",
199 arp
->invflags
& ~ARPT_INV_MASK
);
206 static unsigned int arpt_error(struct sk_buff
**pskb
,
207 const struct net_device
*in
,
208 const struct net_device
*out
,
209 unsigned int hooknum
,
210 const struct xt_target
*target
,
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
,
307 /* Target might have changed stuff. */
308 arp
= (*pskb
)->nh
.arph
;
310 if (verdict
== ARPT_CONTINUE
)
311 e
= (void *)e
+ e
->next_offset
;
317 e
= (void *)e
+ e
->next_offset
;
320 read_unlock_bh(&table
->lock
);
328 /* All zeroes == unconditional rule. */
329 static inline int unconditional(const struct arpt_arp
*arp
)
333 for (i
= 0; i
< sizeof(*arp
)/sizeof(__u32
); i
++)
334 if (((__u32
*)arp
)[i
])
340 /* Figures out from what hook each rule can be called: returns 0 if
341 * there are loops. Puts hook bitmask in comefrom.
343 static int mark_source_chains(struct xt_table_info
*newinfo
,
344 unsigned int valid_hooks
, void *entry0
)
348 /* No recursion; use packet counter to save back ptrs (reset
349 * to 0 as we leave), and comefrom to save source hook bitmask.
351 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
352 unsigned int pos
= newinfo
->hook_entry
[hook
];
354 = (struct arpt_entry
*)(entry0
+ pos
);
356 if (!(valid_hooks
& (1 << hook
)))
359 /* Set initial back pointer. */
360 e
->counters
.pcnt
= pos
;
363 struct arpt_standard_target
*t
364 = (void *)arpt_get_target(e
);
366 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
367 printk("arptables: loop hook %u pos %u %08X.\n",
368 hook
, pos
, e
->comefrom
);
372 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
374 /* Unconditional return/END. */
375 if (e
->target_offset
== sizeof(struct arpt_entry
)
376 && (strcmp(t
->target
.u
.user
.name
,
377 ARPT_STANDARD_TARGET
) == 0)
379 && unconditional(&e
->arp
)) {
380 unsigned int oldpos
, size
;
382 /* Return: backtrack through the last
386 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
388 pos
= e
->counters
.pcnt
;
389 e
->counters
.pcnt
= 0;
391 /* We're at the start. */
395 e
= (struct arpt_entry
*)
397 } while (oldpos
== pos
+ e
->next_offset
);
400 size
= e
->next_offset
;
401 e
= (struct arpt_entry
*)
402 (entry0
+ pos
+ size
);
403 e
->counters
.pcnt
= pos
;
406 int newpos
= t
->verdict
;
408 if (strcmp(t
->target
.u
.user
.name
,
409 ARPT_STANDARD_TARGET
) == 0
411 /* This a jump; chase it. */
412 duprintf("Jump rule %u -> %u\n",
415 /* ... this is a fallthru */
416 newpos
= pos
+ e
->next_offset
;
418 e
= (struct arpt_entry
*)
420 e
->counters
.pcnt
= pos
;
425 duprintf("Finished chain %u\n", hook
);
430 static inline int standard_check(const struct arpt_entry_target
*t
,
431 unsigned int max_offset
)
433 struct arpt_standard_target
*targ
= (void *)t
;
435 /* Check standard info. */
437 != ARPT_ALIGN(sizeof(struct arpt_standard_target
))) {
438 duprintf("arpt_standard_check: target size %u != %Zu\n",
440 ARPT_ALIGN(sizeof(struct arpt_standard_target
)));
444 if (targ
->verdict
>= 0
445 && targ
->verdict
> max_offset
- sizeof(struct arpt_entry
)) {
446 duprintf("arpt_standard_check: bad verdict (%i)\n",
451 if (targ
->verdict
< -NF_MAX_VERDICT
- 1) {
452 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
459 static struct arpt_target arpt_standard_target
;
461 static inline int check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
,
464 struct arpt_entry_target
*t
;
465 struct arpt_target
*target
;
468 if (!arp_checkentry(&e
->arp
)) {
469 duprintf("arp_tables: arp check failed %p %s.\n", e
, name
);
473 t
= arpt_get_target(e
);
474 target
= try_then_request_module(xt_find_target(NF_ARP
, t
->u
.user
.name
,
476 "arpt_%s", t
->u
.user
.name
);
477 if (IS_ERR(target
) || !target
) {
478 duprintf("check_entry: `%s' not found\n", t
->u
.user
.name
);
479 ret
= target
? PTR_ERR(target
) : -ENOENT
;
482 t
->u
.kernel
.target
= target
;
484 ret
= xt_check_target(target
, NF_ARP
, t
->u
.target_size
- sizeof(*t
),
485 name
, e
->comefrom
, 0, 0);
489 if (t
->u
.kernel
.target
== &arpt_standard_target
) {
490 if (!standard_check(t
, size
)) {
494 } else if (t
->u
.kernel
.target
->checkentry
495 && !t
->u
.kernel
.target
->checkentry(name
, e
, target
, t
->data
,
499 duprintf("arp_tables: check failed for `%s'.\n",
500 t
->u
.kernel
.target
->name
);
508 module_put(t
->u
.kernel
.target
->me
);
513 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
514 struct xt_table_info
*newinfo
,
516 unsigned char *limit
,
517 const unsigned int *hook_entries
,
518 const unsigned int *underflows
,
523 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0
524 || (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
) {
525 duprintf("Bad offset %p\n", e
);
530 < sizeof(struct arpt_entry
) + sizeof(struct arpt_entry_target
)) {
531 duprintf("checking: element %p size %u\n",
536 /* Check hooks & underflows */
537 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
538 if ((unsigned char *)e
- base
== hook_entries
[h
])
539 newinfo
->hook_entry
[h
] = hook_entries
[h
];
540 if ((unsigned char *)e
- base
== underflows
[h
])
541 newinfo
->underflow
[h
] = underflows
[h
];
544 /* FIXME: underflows must be unconditional, standard verdicts
545 < 0 (not ARPT_RETURN). --RR */
547 /* Clear counters and comefrom */
548 e
->counters
= ((struct xt_counters
) { 0, 0 });
555 static inline int cleanup_entry(struct arpt_entry
*e
, unsigned int *i
)
557 struct arpt_entry_target
*t
;
559 if (i
&& (*i
)-- == 0)
562 t
= arpt_get_target(e
);
563 if (t
->u
.kernel
.target
->destroy
)
564 t
->u
.kernel
.target
->destroy(t
->u
.kernel
.target
, t
->data
,
565 t
->u
.target_size
- sizeof(*t
));
566 module_put(t
->u
.kernel
.target
->me
);
570 /* Checks and translates the user-supplied table segment (held in
573 static int translate_table(const char *name
,
574 unsigned int valid_hooks
,
575 struct xt_table_info
*newinfo
,
579 const unsigned int *hook_entries
,
580 const unsigned int *underflows
)
585 newinfo
->size
= size
;
586 newinfo
->number
= number
;
588 /* Init all hooks to impossible value. */
589 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
590 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
591 newinfo
->underflow
[i
] = 0xFFFFFFFF;
594 duprintf("translate_table: size %u\n", newinfo
->size
);
597 /* Walk through entries, checking offsets. */
598 ret
= ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
599 check_entry_size_and_hooks
,
603 hook_entries
, underflows
, &i
);
604 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
609 duprintf("translate_table: %u not %u entries\n",
614 /* Check hooks all assigned */
615 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
616 /* Only hooks which are valid */
617 if (!(valid_hooks
& (1 << i
)))
619 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
620 duprintf("Invalid hook entry %u %u\n",
624 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
625 duprintf("Invalid underflow %u %u\n",
631 if (!mark_source_chains(newinfo
, valid_hooks
, entry0
)) {
632 duprintf("Looping hook\n");
636 /* Finally, each sanity check must pass */
638 ret
= ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
639 check_entry
, name
, size
, &i
);
642 ARPT_ENTRY_ITERATE(entry0
, newinfo
->size
,
647 /* And one copy for every other CPU */
648 for_each_possible_cpu(i
) {
649 if (newinfo
->entries
[i
] && newinfo
->entries
[i
] != entry0
)
650 memcpy(newinfo
->entries
[i
], entry0
, newinfo
->size
);
657 static inline int add_entry_to_counter(const struct arpt_entry
*e
,
658 struct xt_counters total
[],
661 ADD_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
667 static inline int set_entry_to_counter(const struct arpt_entry
*e
,
668 struct xt_counters total
[],
671 SET_COUNTER(total
[*i
], e
->counters
.bcnt
, e
->counters
.pcnt
);
677 static void get_counters(const struct xt_table_info
*t
,
678 struct xt_counters counters
[])
684 /* Instead of clearing (by a previous call to memset())
685 * the counters and using adds, we set the counters
686 * with data used by 'current' CPU
687 * We dont care about preemption here.
689 curcpu
= raw_smp_processor_id();
692 ARPT_ENTRY_ITERATE(t
->entries
[curcpu
],
694 set_entry_to_counter
,
698 for_each_possible_cpu(cpu
) {
702 ARPT_ENTRY_ITERATE(t
->entries
[cpu
],
704 add_entry_to_counter
,
710 static int copy_entries_to_user(unsigned int total_size
,
711 struct arpt_table
*table
,
712 void __user
*userptr
)
714 unsigned int off
, num
, countersize
;
715 struct arpt_entry
*e
;
716 struct xt_counters
*counters
;
717 struct xt_table_info
*private = table
->private;
721 /* We need atomic snapshot of counters: rest doesn't change
722 * (other than comefrom, which userspace doesn't care
725 countersize
= sizeof(struct xt_counters
) * private->number
;
726 counters
= vmalloc_node(countersize
, numa_node_id());
728 if (counters
== NULL
)
731 /* First, sum counters... */
732 write_lock_bh(&table
->lock
);
733 get_counters(private, counters
);
734 write_unlock_bh(&table
->lock
);
736 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
737 /* ... then copy entire thing ... */
738 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
743 /* FIXME: use iterator macros --RR */
744 /* ... then go back and fix counters and names */
745 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
746 struct arpt_entry_target
*t
;
748 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
749 if (copy_to_user(userptr
+ off
750 + offsetof(struct arpt_entry
, counters
),
752 sizeof(counters
[num
])) != 0) {
757 t
= arpt_get_target(e
);
758 if (copy_to_user(userptr
+ off
+ e
->target_offset
759 + offsetof(struct arpt_entry_target
,
761 t
->u
.kernel
.target
->name
,
762 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
773 static int get_entries(const struct arpt_get_entries
*entries
,
774 struct arpt_get_entries __user
*uptr
)
777 struct arpt_table
*t
;
779 t
= xt_find_table_lock(NF_ARP
, entries
->name
);
780 if (t
&& !IS_ERR(t
)) {
781 struct xt_table_info
*private = t
->private;
782 duprintf("t->private->number = %u\n",
784 if (entries
->size
== private->size
)
785 ret
= copy_entries_to_user(private->size
,
786 t
, uptr
->entrytable
);
788 duprintf("get_entries: I've got %u not %u!\n",
789 private->size
, entries
->size
);
795 ret
= t
? PTR_ERR(t
) : -ENOENT
;
800 static int do_replace(void __user
*user
, unsigned int len
)
803 struct arpt_replace tmp
;
804 struct arpt_table
*t
;
805 struct xt_table_info
*newinfo
, *oldinfo
;
806 struct xt_counters
*counters
;
807 void *loc_cpu_entry
, *loc_cpu_old_entry
;
809 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
812 /* Hack: Causes ipchains to give correct error msg --RR */
813 if (len
!= sizeof(tmp
) + tmp
.size
)
817 if (tmp
.size
>= (INT_MAX
- sizeof(struct xt_table_info
)) / NR_CPUS
-
820 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
823 newinfo
= xt_alloc_table_info(tmp
.size
);
827 /* choose the copy that is on our node/cpu */
828 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
829 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
835 counters
= vmalloc(tmp
.num_counters
* sizeof(struct xt_counters
));
841 ret
= translate_table(tmp
.name
, tmp
.valid_hooks
,
842 newinfo
, loc_cpu_entry
, tmp
.size
, tmp
.num_entries
,
843 tmp
.hook_entry
, tmp
.underflow
);
845 goto free_newinfo_counters
;
847 duprintf("arp_tables: Translated table\n");
849 t
= try_then_request_module(xt_find_table_lock(NF_ARP
, tmp
.name
),
850 "arptable_%s", tmp
.name
);
851 if (!t
|| IS_ERR(t
)) {
852 ret
= t
? PTR_ERR(t
) : -ENOENT
;
853 goto free_newinfo_counters_untrans
;
857 if (tmp
.valid_hooks
!= t
->valid_hooks
) {
858 duprintf("Valid hook crap: %08X vs %08X\n",
859 tmp
.valid_hooks
, t
->valid_hooks
);
864 oldinfo
= xt_replace_table(t
, tmp
.num_counters
, newinfo
, &ret
);
868 /* Update module usage count based on number of rules */
869 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
870 oldinfo
->number
, oldinfo
->initial_entries
, newinfo
->number
);
871 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
872 (newinfo
->number
<= oldinfo
->initial_entries
))
874 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
875 (newinfo
->number
<= oldinfo
->initial_entries
))
878 /* Get the old counters. */
879 get_counters(oldinfo
, counters
);
880 /* Decrease module usage counts and free resource */
881 loc_cpu_old_entry
= oldinfo
->entries
[raw_smp_processor_id()];
882 ARPT_ENTRY_ITERATE(loc_cpu_old_entry
, oldinfo
->size
, cleanup_entry
,NULL
);
884 xt_free_table_info(oldinfo
);
885 if (copy_to_user(tmp
.counters
, counters
,
886 sizeof(struct xt_counters
) * tmp
.num_counters
) != 0)
895 free_newinfo_counters_untrans
:
896 ARPT_ENTRY_ITERATE(loc_cpu_entry
, newinfo
->size
, cleanup_entry
, NULL
);
897 free_newinfo_counters
:
900 xt_free_table_info(newinfo
);
904 /* We're lazy, and add to the first CPU; overflow works its fey magic
905 * and everything is OK.
907 static inline int add_counter_to_entry(struct arpt_entry
*e
,
908 const struct xt_counters addme
[],
912 ADD_COUNTER(e
->counters
, addme
[*i
].bcnt
, addme
[*i
].pcnt
);
918 static int do_add_counters(void __user
*user
, unsigned int len
)
921 struct xt_counters_info tmp
, *paddc
;
922 struct arpt_table
*t
;
923 struct xt_table_info
*private;
927 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
930 if (len
!= sizeof(tmp
) + tmp
.num_counters
*sizeof(struct xt_counters
))
933 paddc
= vmalloc(len
);
937 if (copy_from_user(paddc
, user
, len
) != 0) {
942 t
= xt_find_table_lock(NF_ARP
, tmp
.name
);
943 if (!t
|| IS_ERR(t
)) {
944 ret
= t
? PTR_ERR(t
) : -ENOENT
;
948 write_lock_bh(&t
->lock
);
949 private = t
->private;
950 if (private->number
!= tmp
.num_counters
) {
956 /* Choose the copy that is on our node */
957 loc_cpu_entry
= private->entries
[smp_processor_id()];
958 ARPT_ENTRY_ITERATE(loc_cpu_entry
,
960 add_counter_to_entry
,
964 write_unlock_bh(&t
->lock
);
973 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
977 if (!capable(CAP_NET_ADMIN
))
981 case ARPT_SO_SET_REPLACE
:
982 ret
= do_replace(user
, len
);
985 case ARPT_SO_SET_ADD_COUNTERS
:
986 ret
= do_add_counters(user
, len
);
990 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
997 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1001 if (!capable(CAP_NET_ADMIN
))
1005 case ARPT_SO_GET_INFO
: {
1006 char name
[ARPT_TABLE_MAXNAMELEN
];
1007 struct arpt_table
*t
;
1009 if (*len
!= sizeof(struct arpt_getinfo
)) {
1010 duprintf("length %u != %Zu\n", *len
,
1011 sizeof(struct arpt_getinfo
));
1016 if (copy_from_user(name
, user
, sizeof(name
)) != 0) {
1020 name
[ARPT_TABLE_MAXNAMELEN
-1] = '\0';
1022 t
= try_then_request_module(xt_find_table_lock(NF_ARP
, name
),
1023 "arptable_%s", name
);
1024 if (t
&& !IS_ERR(t
)) {
1025 struct arpt_getinfo info
;
1026 struct xt_table_info
*private = t
->private;
1028 info
.valid_hooks
= t
->valid_hooks
;
1029 memcpy(info
.hook_entry
, private->hook_entry
,
1030 sizeof(info
.hook_entry
));
1031 memcpy(info
.underflow
, private->underflow
,
1032 sizeof(info
.underflow
));
1033 info
.num_entries
= private->number
;
1034 info
.size
= private->size
;
1035 strcpy(info
.name
, name
);
1037 if (copy_to_user(user
, &info
, *len
) != 0)
1044 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1048 case ARPT_SO_GET_ENTRIES
: {
1049 struct arpt_get_entries get
;
1051 if (*len
< sizeof(get
)) {
1052 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
1054 } else if (copy_from_user(&get
, user
, sizeof(get
)) != 0) {
1056 } else if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
1057 duprintf("get_entries: %u != %Zu\n", *len
,
1058 sizeof(struct arpt_get_entries
) + get
.size
);
1061 ret
= get_entries(&get
, user
);
1065 case ARPT_SO_GET_REVISION_TARGET
: {
1066 struct xt_get_revision rev
;
1068 if (*len
!= sizeof(rev
)) {
1072 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1077 try_then_request_module(xt_find_revision(NF_ARP
, rev
.name
,
1078 rev
.revision
, 1, &ret
),
1079 "arpt_%s", rev
.name
);
1084 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1091 int arpt_register_table(struct arpt_table
*table
,
1092 const struct arpt_replace
*repl
)
1095 struct xt_table_info
*newinfo
;
1096 static struct xt_table_info bootstrap
1097 = { 0, 0, 0, { 0 }, { 0 }, { } };
1098 void *loc_cpu_entry
;
1100 newinfo
= xt_alloc_table_info(repl
->size
);
1106 /* choose the copy on our node/cpu */
1107 loc_cpu_entry
= newinfo
->entries
[raw_smp_processor_id()];
1108 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1110 ret
= translate_table(table
->name
, table
->valid_hooks
,
1111 newinfo
, loc_cpu_entry
, repl
->size
,
1116 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1118 xt_free_table_info(newinfo
);
1122 ret
= xt_register_table(table
, &bootstrap
, newinfo
);
1124 xt_free_table_info(newinfo
);
1131 void arpt_unregister_table(struct arpt_table
*table
)
1133 struct xt_table_info
*private;
1134 void *loc_cpu_entry
;
1136 private = xt_unregister_table(table
);
1138 /* Decrease module usage counts and free resources */
1139 loc_cpu_entry
= private->entries
[raw_smp_processor_id()];
1140 ARPT_ENTRY_ITERATE(loc_cpu_entry
, private->size
,
1141 cleanup_entry
, NULL
);
1142 xt_free_table_info(private);
1145 /* The built-in targets: standard (NULL) and error. */
1146 static struct arpt_target arpt_standard_target
= {
1147 .name
= ARPT_STANDARD_TARGET
,
1148 .targetsize
= sizeof(int),
1152 static struct arpt_target arpt_error_target
= {
1153 .name
= ARPT_ERROR_TARGET
,
1154 .target
= arpt_error
,
1155 .targetsize
= ARPT_FUNCTION_MAXNAMELEN
,
1159 static struct nf_sockopt_ops arpt_sockopts
= {
1161 .set_optmin
= ARPT_BASE_CTL
,
1162 .set_optmax
= ARPT_SO_SET_MAX
+1,
1163 .set
= do_arpt_set_ctl
,
1164 .get_optmin
= ARPT_BASE_CTL
,
1165 .get_optmax
= ARPT_SO_GET_MAX
+1,
1166 .get
= do_arpt_get_ctl
,
1169 static int __init
arp_tables_init(void)
1173 ret
= xt_proto_init(NF_ARP
);
1177 /* Noone else will be downing sem now, so we won't sleep */
1178 ret
= xt_register_target(&arpt_standard_target
);
1181 ret
= xt_register_target(&arpt_error_target
);
1185 /* Register setsockopt */
1186 ret
= nf_register_sockopt(&arpt_sockopts
);
1190 printk("arp_tables: (C) 2002 David S. Miller\n");
1194 xt_unregister_target(&arpt_error_target
);
1196 xt_unregister_target(&arpt_standard_target
);
1198 xt_proto_fini(NF_ARP
);
1203 static void __exit
arp_tables_fini(void)
1205 nf_unregister_sockopt(&arpt_sockopts
);
1206 xt_proto_fini(NF_ARP
);
1209 EXPORT_SYMBOL(arpt_register_table
);
1210 EXPORT_SYMBOL(arpt_unregister_table
);
1211 EXPORT_SYMBOL(arpt_do_table
);
1213 module_init(arp_tables_init
);
1214 module_exit(arp_tables_fini
);