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)
9 * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
23 #include <linux/mutex.h>
24 #include <linux/err.h>
25 #include <net/compat.h>
27 #include <asm/uaccess.h>
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_arp/arp_tables.h>
31 #include "../../netfilter/xt_repldata.h"
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
35 MODULE_DESCRIPTION("arptables core");
37 /*#define DEBUG_ARP_TABLES*/
38 /*#define DEBUG_ARP_TABLES_USER*/
40 #ifdef DEBUG_ARP_TABLES
41 #define dprintf(format, args...) pr_debug(format, ## args)
43 #define dprintf(format, args...)
46 #ifdef DEBUG_ARP_TABLES_USER
47 #define duprintf(format, args...) pr_debug(format, ## args)
49 #define duprintf(format, args...)
52 #ifdef CONFIG_NETFILTER_DEBUG
53 #define ARP_NF_ASSERT(x) WARN_ON(!(x))
55 #define ARP_NF_ASSERT(x)
58 void *arpt_alloc_initial_table(const struct xt_table
*info
)
60 return xt_alloc_initial_table(arpt
, ARPT
);
62 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table
);
64 static inline int arp_devaddr_compare(const struct arpt_devaddr_info
*ap
,
65 const char *hdr_addr
, int len
)
69 if (len
> ARPT_DEV_ADDR_LEN_MAX
)
70 len
= ARPT_DEV_ADDR_LEN_MAX
;
73 for (i
= 0; i
< len
; i
++)
74 ret
|= (hdr_addr
[i
] ^ ap
->addr
[i
]) & ap
->mask
[i
];
80 * Unfortunately, _b and _mask are not aligned to an int (or long int)
81 * Some arches dont care, unrolling the loop is a win on them.
82 * For other arches, we only have a 16bit alignement.
84 static unsigned long ifname_compare(const char *_a
, const char *_b
, const char *_mask
)
86 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
87 unsigned long ret
= ifname_compare_aligned(_a
, _b
, _mask
);
89 unsigned long ret
= 0;
90 const u16
*a
= (const u16
*)_a
;
91 const u16
*b
= (const u16
*)_b
;
92 const u16
*mask
= (const u16
*)_mask
;
95 for (i
= 0; i
< IFNAMSIZ
/sizeof(u16
); i
++)
96 ret
|= (a
[i
] ^ b
[i
]) & mask
[i
];
101 /* Returns whether packet matches rule or not. */
102 static inline int arp_packet_match(const struct arphdr
*arphdr
,
103 struct net_device
*dev
,
106 const struct arpt_arp
*arpinfo
)
108 const char *arpptr
= (char *)(arphdr
+ 1);
109 const char *src_devaddr
, *tgt_devaddr
;
110 __be32 src_ipaddr
, tgt_ipaddr
;
113 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
115 if (FWINV((arphdr
->ar_op
& arpinfo
->arpop_mask
) != arpinfo
->arpop
,
117 dprintf("ARP operation field mismatch.\n");
118 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
119 arphdr
->ar_op
, arpinfo
->arpop
, arpinfo
->arpop_mask
);
123 if (FWINV((arphdr
->ar_hrd
& arpinfo
->arhrd_mask
) != arpinfo
->arhrd
,
125 dprintf("ARP hardware address format mismatch.\n");
126 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
127 arphdr
->ar_hrd
, arpinfo
->arhrd
, arpinfo
->arhrd_mask
);
131 if (FWINV((arphdr
->ar_pro
& arpinfo
->arpro_mask
) != arpinfo
->arpro
,
133 dprintf("ARP protocol address format mismatch.\n");
134 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
135 arphdr
->ar_pro
, arpinfo
->arpro
, arpinfo
->arpro_mask
);
139 if (FWINV((arphdr
->ar_hln
& arpinfo
->arhln_mask
) != arpinfo
->arhln
,
141 dprintf("ARP hardware address length mismatch.\n");
142 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
143 arphdr
->ar_hln
, arpinfo
->arhln
, arpinfo
->arhln_mask
);
147 src_devaddr
= arpptr
;
148 arpptr
+= dev
->addr_len
;
149 memcpy(&src_ipaddr
, arpptr
, sizeof(u32
));
150 arpptr
+= sizeof(u32
);
151 tgt_devaddr
= arpptr
;
152 arpptr
+= dev
->addr_len
;
153 memcpy(&tgt_ipaddr
, arpptr
, sizeof(u32
));
155 if (FWINV(arp_devaddr_compare(&arpinfo
->src_devaddr
, src_devaddr
, dev
->addr_len
),
156 ARPT_INV_SRCDEVADDR
) ||
157 FWINV(arp_devaddr_compare(&arpinfo
->tgt_devaddr
, tgt_devaddr
, dev
->addr_len
),
158 ARPT_INV_TGTDEVADDR
)) {
159 dprintf("Source or target device address mismatch.\n");
164 if (FWINV((src_ipaddr
& arpinfo
->smsk
.s_addr
) != arpinfo
->src
.s_addr
,
166 FWINV(((tgt_ipaddr
& arpinfo
->tmsk
.s_addr
) != arpinfo
->tgt
.s_addr
),
168 dprintf("Source or target IP address mismatch.\n");
170 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
172 &arpinfo
->smsk
.s_addr
,
173 &arpinfo
->src
.s_addr
,
174 arpinfo
->invflags
& ARPT_INV_SRCIP
? " (INV)" : "");
175 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
177 &arpinfo
->tmsk
.s_addr
,
178 &arpinfo
->tgt
.s_addr
,
179 arpinfo
->invflags
& ARPT_INV_TGTIP
? " (INV)" : "");
183 /* Look for ifname matches. */
184 ret
= ifname_compare(indev
, arpinfo
->iniface
, arpinfo
->iniface_mask
);
186 if (FWINV(ret
!= 0, ARPT_INV_VIA_IN
)) {
187 dprintf("VIA in mismatch (%s vs %s).%s\n",
188 indev
, arpinfo
->iniface
,
189 arpinfo
->invflags
& ARPT_INV_VIA_IN
? " (INV)" : "");
193 ret
= ifname_compare(outdev
, arpinfo
->outiface
, arpinfo
->outiface_mask
);
195 if (FWINV(ret
!= 0, ARPT_INV_VIA_OUT
)) {
196 dprintf("VIA out mismatch (%s vs %s).%s\n",
197 outdev
, arpinfo
->outiface
,
198 arpinfo
->invflags
& ARPT_INV_VIA_OUT
? " (INV)" : "");
206 static inline int arp_checkentry(const struct arpt_arp
*arp
)
208 if (arp
->flags
& ~ARPT_F_MASK
) {
209 duprintf("Unknown flag bits set: %08X\n",
210 arp
->flags
& ~ARPT_F_MASK
);
213 if (arp
->invflags
& ~ARPT_INV_MASK
) {
214 duprintf("Unknown invflag bits set: %08X\n",
215 arp
->invflags
& ~ARPT_INV_MASK
);
223 arpt_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
225 net_err_ratelimited("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
,
250 const struct nf_hook_state
*state
,
251 struct xt_table
*table
)
253 unsigned int hook
= state
->hook
;
254 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
255 unsigned int verdict
= NF_DROP
;
256 const struct arphdr
*arp
;
257 struct arpt_entry
*e
, **jumpstack
;
258 const char *indev
, *outdev
;
259 const void *table_base
;
260 unsigned int cpu
, stackidx
= 0;
261 const struct xt_table_info
*private;
262 struct xt_action_param acpar
;
265 if (!pskb_may_pull(skb
, arp_hdr_len(skb
->dev
)))
268 indev
= state
->in
? state
->in
->name
: nulldevname
;
269 outdev
= state
->out
? state
->out
->name
: nulldevname
;
272 addend
= xt_write_recseq_begin();
273 private = table
->private;
274 cpu
= smp_processor_id();
276 * Ensure we load private-> members after we've fetched the base
279 smp_read_barrier_depends();
280 table_base
= private->entries
;
281 jumpstack
= (struct arpt_entry
**)private->jumpstack
[cpu
];
283 /* No TEE support for arptables, so no need to switch to alternate
284 * stack. All targets that reenter must return absolute verdicts.
286 e
= get_entry(table_base
, private->hook_entry
[hook
]);
288 acpar
.net
= state
->net
;
289 acpar
.in
= state
->in
;
290 acpar
.out
= state
->out
;
291 acpar
.hooknum
= hook
;
292 acpar
.family
= NFPROTO_ARP
;
293 acpar
.hotdrop
= false;
297 const struct xt_entry_target
*t
;
298 struct xt_counters
*counter
;
300 if (!arp_packet_match(arp
, skb
->dev
, indev
, outdev
, &e
->arp
)) {
301 e
= arpt_next_entry(e
);
305 counter
= xt_get_this_cpu_counter(&e
->counters
);
306 ADD_COUNTER(*counter
, arp_hdr_len(skb
->dev
), 1);
308 t
= arpt_get_target_c(e
);
310 /* Standard target? */
311 if (!t
->u
.kernel
.target
->target
) {
314 v
= ((struct xt_standard_target
*)t
)->verdict
;
316 /* Pop from stack? */
317 if (v
!= XT_RETURN
) {
318 verdict
= (unsigned int)(-v
) - 1;
322 e
= get_entry(table_base
,
323 private->underflow
[hook
]);
325 e
= jumpstack
[--stackidx
];
326 e
= arpt_next_entry(e
);
331 != arpt_next_entry(e
)) {
332 jumpstack
[stackidx
++] = e
;
335 e
= get_entry(table_base
, v
);
339 acpar
.target
= t
->u
.kernel
.target
;
340 acpar
.targinfo
= t
->data
;
341 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
343 /* Target might have changed stuff. */
346 if (verdict
== XT_CONTINUE
)
347 e
= arpt_next_entry(e
);
351 } while (!acpar
.hotdrop
);
352 xt_write_recseq_end(addend
);
361 /* All zeroes == unconditional rule. */
362 static inline bool unconditional(const struct arpt_entry
*e
)
364 static const struct arpt_arp uncond
;
366 return e
->target_offset
== sizeof(struct arpt_entry
) &&
367 memcmp(&e
->arp
, &uncond
, sizeof(uncond
)) == 0;
370 /* Figures out from what hook each rule can be called: returns 0 if
371 * there are loops. Puts hook bitmask in comefrom.
373 static int mark_source_chains(const struct xt_table_info
*newinfo
,
374 unsigned int valid_hooks
, void *entry0
)
378 /* No recursion; use packet counter to save back ptrs (reset
379 * to 0 as we leave), and comefrom to save source hook bitmask.
381 for (hook
= 0; hook
< NF_ARP_NUMHOOKS
; hook
++) {
382 unsigned int pos
= newinfo
->hook_entry
[hook
];
384 = (struct arpt_entry
*)(entry0
+ pos
);
386 if (!(valid_hooks
& (1 << hook
)))
389 /* Set initial back pointer. */
390 e
->counters
.pcnt
= pos
;
393 const struct xt_standard_target
*t
394 = (void *)arpt_get_target_c(e
);
395 int visited
= e
->comefrom
& (1 << hook
);
397 if (e
->comefrom
& (1 << NF_ARP_NUMHOOKS
)) {
398 pr_notice("arptables: loop hook %u pos %u %08X.\n",
399 hook
, pos
, e
->comefrom
);
403 |= ((1 << hook
) | (1 << NF_ARP_NUMHOOKS
));
405 /* Unconditional return/END. */
406 if ((unconditional(e
) &&
407 (strcmp(t
->target
.u
.user
.name
,
408 XT_STANDARD_TARGET
) == 0) &&
409 t
->verdict
< 0) || visited
) {
410 unsigned int oldpos
, size
;
412 if ((strcmp(t
->target
.u
.user
.name
,
413 XT_STANDARD_TARGET
) == 0) &&
414 t
->verdict
< -NF_MAX_VERDICT
- 1) {
415 duprintf("mark_source_chains: bad "
416 "negative verdict (%i)\n",
421 /* Return: backtrack through the last
425 e
->comefrom
^= (1<<NF_ARP_NUMHOOKS
);
427 pos
= e
->counters
.pcnt
;
428 e
->counters
.pcnt
= 0;
430 /* We're at the start. */
434 e
= (struct arpt_entry
*)
436 } while (oldpos
== pos
+ e
->next_offset
);
439 size
= e
->next_offset
;
440 e
= (struct arpt_entry
*)
441 (entry0
+ pos
+ size
);
442 e
->counters
.pcnt
= pos
;
445 int newpos
= t
->verdict
;
447 if (strcmp(t
->target
.u
.user
.name
,
448 XT_STANDARD_TARGET
) == 0 &&
450 if (newpos
> newinfo
->size
-
451 sizeof(struct arpt_entry
)) {
452 duprintf("mark_source_chains: "
453 "bad verdict (%i)\n",
458 /* This a jump; chase it. */
459 duprintf("Jump rule %u -> %u\n",
462 /* ... this is a fallthru */
463 newpos
= pos
+ e
->next_offset
;
465 e
= (struct arpt_entry
*)
467 e
->counters
.pcnt
= pos
;
472 duprintf("Finished chain %u\n", hook
);
477 static inline int check_entry(const struct arpt_entry
*e
)
479 const struct xt_entry_target
*t
;
481 if (!arp_checkentry(&e
->arp
))
484 if (e
->target_offset
+ sizeof(struct xt_entry_target
) > e
->next_offset
)
487 t
= arpt_get_target_c(e
);
488 if (e
->target_offset
+ t
->u
.target_size
> e
->next_offset
)
494 static inline int check_target(struct arpt_entry
*e
, const char *name
)
496 struct xt_entry_target
*t
= arpt_get_target(e
);
498 struct xt_tgchk_param par
= {
501 .target
= t
->u
.kernel
.target
,
503 .hook_mask
= e
->comefrom
,
504 .family
= NFPROTO_ARP
,
507 ret
= xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
), 0, false);
509 duprintf("arp_tables: check failed for `%s'.\n",
510 t
->u
.kernel
.target
->name
);
517 find_check_entry(struct arpt_entry
*e
, const char *name
, unsigned int size
)
519 struct xt_entry_target
*t
;
520 struct xt_target
*target
;
523 e
->counters
.pcnt
= xt_percpu_counter_alloc();
524 if (IS_ERR_VALUE(e
->counters
.pcnt
))
527 t
= arpt_get_target(e
);
528 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
530 if (IS_ERR(target
)) {
531 duprintf("find_check_entry: `%s' not found\n", t
->u
.user
.name
);
532 ret
= PTR_ERR(target
);
535 t
->u
.kernel
.target
= target
;
537 ret
= check_target(e
, name
);
542 module_put(t
->u
.kernel
.target
->me
);
544 xt_percpu_counter_free(e
->counters
.pcnt
);
549 static bool check_underflow(const struct arpt_entry
*e
)
551 const struct xt_entry_target
*t
;
552 unsigned int verdict
;
554 if (!unconditional(e
))
556 t
= arpt_get_target_c(e
);
557 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
559 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
560 verdict
= -verdict
- 1;
561 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
564 static inline int check_entry_size_and_hooks(struct arpt_entry
*e
,
565 struct xt_table_info
*newinfo
,
566 const unsigned char *base
,
567 const unsigned char *limit
,
568 const unsigned int *hook_entries
,
569 const unsigned int *underflows
,
570 unsigned int valid_hooks
)
575 if ((unsigned long)e
% __alignof__(struct arpt_entry
) != 0 ||
576 (unsigned char *)e
+ sizeof(struct arpt_entry
) >= limit
||
577 (unsigned char *)e
+ e
->next_offset
> limit
) {
578 duprintf("Bad offset %p\n", e
);
583 < sizeof(struct arpt_entry
) + sizeof(struct xt_entry_target
)) {
584 duprintf("checking: element %p size %u\n",
589 err
= check_entry(e
);
593 /* Check hooks & underflows */
594 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
595 if (!(valid_hooks
& (1 << h
)))
597 if ((unsigned char *)e
- base
== hook_entries
[h
])
598 newinfo
->hook_entry
[h
] = hook_entries
[h
];
599 if ((unsigned char *)e
- base
== underflows
[h
]) {
600 if (!check_underflow(e
)) {
601 pr_debug("Underflows must be unconditional and "
602 "use the STANDARD target with "
606 newinfo
->underflow
[h
] = underflows
[h
];
610 /* Clear counters and comefrom */
611 e
->counters
= ((struct xt_counters
) { 0, 0 });
616 static inline void cleanup_entry(struct arpt_entry
*e
)
618 struct xt_tgdtor_param par
;
619 struct xt_entry_target
*t
;
621 t
= arpt_get_target(e
);
622 par
.target
= t
->u
.kernel
.target
;
623 par
.targinfo
= t
->data
;
624 par
.family
= NFPROTO_ARP
;
625 if (par
.target
->destroy
!= NULL
)
626 par
.target
->destroy(&par
);
627 module_put(par
.target
->me
);
628 xt_percpu_counter_free(e
->counters
.pcnt
);
631 /* Checks and translates the user-supplied table segment (held in
634 static int translate_table(struct xt_table_info
*newinfo
, void *entry0
,
635 const struct arpt_replace
*repl
)
637 struct arpt_entry
*iter
;
641 newinfo
->size
= repl
->size
;
642 newinfo
->number
= repl
->num_entries
;
644 /* Init all hooks to impossible value. */
645 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
646 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
647 newinfo
->underflow
[i
] = 0xFFFFFFFF;
650 duprintf("translate_table: size %u\n", newinfo
->size
);
653 /* Walk through entries, checking offsets. */
654 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
655 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
663 if (strcmp(arpt_get_target(iter
)->u
.user
.name
,
664 XT_ERROR_TARGET
) == 0)
665 ++newinfo
->stacksize
;
667 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret
);
671 if (i
!= repl
->num_entries
) {
672 duprintf("translate_table: %u not %u entries\n",
673 i
, repl
->num_entries
);
677 /* Check hooks all assigned */
678 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
679 /* Only hooks which are valid */
680 if (!(repl
->valid_hooks
& (1 << i
)))
682 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF) {
683 duprintf("Invalid hook entry %u %u\n",
684 i
, repl
->hook_entry
[i
]);
687 if (newinfo
->underflow
[i
] == 0xFFFFFFFF) {
688 duprintf("Invalid underflow %u %u\n",
689 i
, repl
->underflow
[i
]);
694 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
)) {
695 duprintf("Looping hook\n");
699 /* Finally, each sanity check must pass */
701 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
702 ret
= find_check_entry(iter
, repl
->name
, repl
->size
);
709 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
720 static void get_counters(const struct xt_table_info
*t
,
721 struct xt_counters counters
[])
723 struct arpt_entry
*iter
;
727 for_each_possible_cpu(cpu
) {
728 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
731 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
732 struct xt_counters
*tmp
;
736 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
738 start
= read_seqcount_begin(s
);
741 } while (read_seqcount_retry(s
, start
));
743 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
749 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
751 unsigned int countersize
;
752 struct xt_counters
*counters
;
753 const struct xt_table_info
*private = table
->private;
755 /* We need atomic snapshot of counters: rest doesn't change
756 * (other than comefrom, which userspace doesn't care
759 countersize
= sizeof(struct xt_counters
) * private->number
;
760 counters
= vzalloc(countersize
);
762 if (counters
== NULL
)
763 return ERR_PTR(-ENOMEM
);
765 get_counters(private, counters
);
770 static int copy_entries_to_user(unsigned int total_size
,
771 const struct xt_table
*table
,
772 void __user
*userptr
)
774 unsigned int off
, num
;
775 const struct arpt_entry
*e
;
776 struct xt_counters
*counters
;
777 struct xt_table_info
*private = table
->private;
781 counters
= alloc_counters(table
);
782 if (IS_ERR(counters
))
783 return PTR_ERR(counters
);
785 loc_cpu_entry
= private->entries
;
786 /* ... then copy entire thing ... */
787 if (copy_to_user(userptr
, loc_cpu_entry
, total_size
) != 0) {
792 /* FIXME: use iterator macros --RR */
793 /* ... then go back and fix counters and names */
794 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
795 const struct xt_entry_target
*t
;
797 e
= (struct arpt_entry
*)(loc_cpu_entry
+ off
);
798 if (copy_to_user(userptr
+ off
799 + offsetof(struct arpt_entry
, counters
),
801 sizeof(counters
[num
])) != 0) {
806 t
= arpt_get_target_c(e
);
807 if (copy_to_user(userptr
+ off
+ e
->target_offset
808 + offsetof(struct xt_entry_target
,
810 t
->u
.kernel
.target
->name
,
811 strlen(t
->u
.kernel
.target
->name
)+1) != 0) {
823 static void compat_standard_from_user(void *dst
, const void *src
)
825 int v
= *(compat_int_t
*)src
;
828 v
+= xt_compat_calc_jump(NFPROTO_ARP
, v
);
829 memcpy(dst
, &v
, sizeof(v
));
832 static int compat_standard_to_user(void __user
*dst
, const void *src
)
834 compat_int_t cv
= *(int *)src
;
837 cv
-= xt_compat_calc_jump(NFPROTO_ARP
, cv
);
838 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
841 static int compat_calc_entry(const struct arpt_entry
*e
,
842 const struct xt_table_info
*info
,
843 const void *base
, struct xt_table_info
*newinfo
)
845 const struct xt_entry_target
*t
;
846 unsigned int entry_offset
;
849 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
850 entry_offset
= (void *)e
- base
;
852 t
= arpt_get_target_c(e
);
853 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
854 newinfo
->size
-= off
;
855 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
859 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
860 if (info
->hook_entry
[i
] &&
861 (e
< (struct arpt_entry
*)(base
+ info
->hook_entry
[i
])))
862 newinfo
->hook_entry
[i
] -= off
;
863 if (info
->underflow
[i
] &&
864 (e
< (struct arpt_entry
*)(base
+ info
->underflow
[i
])))
865 newinfo
->underflow
[i
] -= off
;
870 static int compat_table_info(const struct xt_table_info
*info
,
871 struct xt_table_info
*newinfo
)
873 struct arpt_entry
*iter
;
874 const void *loc_cpu_entry
;
877 if (!newinfo
|| !info
)
880 /* we dont care about newinfo->entries */
881 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
882 newinfo
->initial_entries
= 0;
883 loc_cpu_entry
= info
->entries
;
884 xt_compat_init_offsets(NFPROTO_ARP
, info
->number
);
885 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
886 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
894 static int get_info(struct net
*net
, void __user
*user
,
895 const int *len
, int compat
)
897 char name
[XT_TABLE_MAXNAMELEN
];
901 if (*len
!= sizeof(struct arpt_getinfo
)) {
902 duprintf("length %u != %Zu\n", *len
,
903 sizeof(struct arpt_getinfo
));
907 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
910 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
913 xt_compat_lock(NFPROTO_ARP
);
915 t
= try_then_request_module(xt_find_table_lock(net
, NFPROTO_ARP
, name
),
916 "arptable_%s", name
);
917 if (!IS_ERR_OR_NULL(t
)) {
918 struct arpt_getinfo info
;
919 const struct xt_table_info
*private = t
->private;
921 struct xt_table_info tmp
;
924 ret
= compat_table_info(private, &tmp
);
925 xt_compat_flush_offsets(NFPROTO_ARP
);
929 memset(&info
, 0, sizeof(info
));
930 info
.valid_hooks
= t
->valid_hooks
;
931 memcpy(info
.hook_entry
, private->hook_entry
,
932 sizeof(info
.hook_entry
));
933 memcpy(info
.underflow
, private->underflow
,
934 sizeof(info
.underflow
));
935 info
.num_entries
= private->number
;
936 info
.size
= private->size
;
937 strcpy(info
.name
, name
);
939 if (copy_to_user(user
, &info
, *len
) != 0)
946 ret
= t
? PTR_ERR(t
) : -ENOENT
;
949 xt_compat_unlock(NFPROTO_ARP
);
954 static int get_entries(struct net
*net
, struct arpt_get_entries __user
*uptr
,
958 struct arpt_get_entries get
;
961 if (*len
< sizeof(get
)) {
962 duprintf("get_entries: %u < %Zu\n", *len
, sizeof(get
));
965 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
967 if (*len
!= sizeof(struct arpt_get_entries
) + get
.size
) {
968 duprintf("get_entries: %u != %Zu\n", *len
,
969 sizeof(struct arpt_get_entries
) + get
.size
);
972 get
.name
[sizeof(get
.name
) - 1] = '\0';
974 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
975 if (!IS_ERR_OR_NULL(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
= vzalloc(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 (IS_ERR_OR_NULL(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
;
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) {
1056 /* Silent error, can't fail, new table is already in place */
1057 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
1066 free_newinfo_counters_untrans
:
1072 static int do_replace(struct net
*net
, const void __user
*user
,
1076 struct arpt_replace tmp
;
1077 struct xt_table_info
*newinfo
;
1078 void *loc_cpu_entry
;
1079 struct arpt_entry
*iter
;
1081 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1084 /* overflow check */
1085 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1087 if (tmp
.num_counters
== 0)
1090 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1092 newinfo
= xt_alloc_table_info(tmp
.size
);
1096 loc_cpu_entry
= newinfo
->entries
;
1097 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1103 ret
= translate_table(newinfo
, loc_cpu_entry
, &tmp
);
1107 duprintf("arp_tables: Translated table\n");
1109 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1110 tmp
.num_counters
, tmp
.counters
);
1112 goto free_newinfo_untrans
;
1115 free_newinfo_untrans
:
1116 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1117 cleanup_entry(iter
);
1119 xt_free_table_info(newinfo
);
1123 static int do_add_counters(struct net
*net
, const void __user
*user
,
1124 unsigned int len
, int compat
)
1127 struct xt_counters_info tmp
;
1128 struct xt_counters
*paddc
;
1129 unsigned int num_counters
;
1134 const struct xt_table_info
*private;
1136 struct arpt_entry
*iter
;
1137 unsigned int addend
;
1138 #ifdef CONFIG_COMPAT
1139 struct compat_xt_counters_info compat_tmp
;
1143 size
= sizeof(struct compat_xt_counters_info
);
1148 size
= sizeof(struct xt_counters_info
);
1151 if (copy_from_user(ptmp
, user
, size
) != 0)
1154 #ifdef CONFIG_COMPAT
1156 num_counters
= compat_tmp
.num_counters
;
1157 name
= compat_tmp
.name
;
1161 num_counters
= tmp
.num_counters
;
1165 if (len
!= size
+ num_counters
* sizeof(struct xt_counters
))
1168 paddc
= vmalloc(len
- size
);
1172 if (copy_from_user(paddc
, user
+ size
, len
- size
) != 0) {
1177 t
= xt_find_table_lock(net
, NFPROTO_ARP
, name
);
1178 if (IS_ERR_OR_NULL(t
)) {
1179 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1184 private = t
->private;
1185 if (private->number
!= num_counters
) {
1187 goto unlock_up_free
;
1192 addend
= xt_write_recseq_begin();
1193 xt_entry_foreach(iter
, private->entries
, private->size
) {
1194 struct xt_counters
*tmp
;
1196 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1197 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1200 xt_write_recseq_end(addend
);
1211 #ifdef CONFIG_COMPAT
1212 static inline void compat_release_entry(struct compat_arpt_entry
*e
)
1214 struct xt_entry_target
*t
;
1216 t
= compat_arpt_get_target(e
);
1217 module_put(t
->u
.kernel
.target
->me
);
1221 check_compat_entry_size_and_hooks(struct compat_arpt_entry
*e
,
1222 struct xt_table_info
*newinfo
,
1224 const unsigned char *base
,
1225 const unsigned char *limit
,
1226 const unsigned int *hook_entries
,
1227 const unsigned int *underflows
,
1230 struct xt_entry_target
*t
;
1231 struct xt_target
*target
;
1232 unsigned int entry_offset
;
1235 duprintf("check_compat_entry_size_and_hooks %p\n", e
);
1236 if ((unsigned long)e
% __alignof__(struct compat_arpt_entry
) != 0 ||
1237 (unsigned char *)e
+ sizeof(struct compat_arpt_entry
) >= limit
||
1238 (unsigned char *)e
+ e
->next_offset
> limit
) {
1239 duprintf("Bad offset %p, limit = %p\n", e
, limit
);
1243 if (e
->next_offset
< sizeof(struct compat_arpt_entry
) +
1244 sizeof(struct compat_xt_entry_target
)) {
1245 duprintf("checking: element %p size %u\n",
1250 /* For purposes of check_entry casting the compat entry is fine */
1251 ret
= check_entry((struct arpt_entry
*)e
);
1255 off
= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1256 entry_offset
= (void *)e
- (void *)base
;
1258 t
= compat_arpt_get_target(e
);
1259 target
= xt_request_find_target(NFPROTO_ARP
, t
->u
.user
.name
,
1260 t
->u
.user
.revision
);
1261 if (IS_ERR(target
)) {
1262 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1264 ret
= PTR_ERR(target
);
1267 t
->u
.kernel
.target
= target
;
1269 off
+= xt_compat_target_offset(target
);
1271 ret
= xt_compat_add_offset(NFPROTO_ARP
, entry_offset
, off
);
1273 goto release_target
;
1275 /* Check hooks & underflows */
1276 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1277 if ((unsigned char *)e
- base
== hook_entries
[h
])
1278 newinfo
->hook_entry
[h
] = hook_entries
[h
];
1279 if ((unsigned char *)e
- base
== underflows
[h
])
1280 newinfo
->underflow
[h
] = underflows
[h
];
1283 /* Clear counters and comefrom */
1284 memset(&e
->counters
, 0, sizeof(e
->counters
));
1289 module_put(t
->u
.kernel
.target
->me
);
1295 compat_copy_entry_from_user(struct compat_arpt_entry
*e
, void **dstptr
,
1296 unsigned int *size
, const char *name
,
1297 struct xt_table_info
*newinfo
, unsigned char *base
)
1299 struct xt_entry_target
*t
;
1300 struct xt_target
*target
;
1301 struct arpt_entry
*de
;
1302 unsigned int origsize
;
1307 de
= (struct arpt_entry
*)*dstptr
;
1308 memcpy(de
, e
, sizeof(struct arpt_entry
));
1309 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1311 *dstptr
+= sizeof(struct arpt_entry
);
1312 *size
+= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1314 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1315 t
= compat_arpt_get_target(e
);
1316 target
= t
->u
.kernel
.target
;
1317 xt_compat_target_from_user(t
, dstptr
, size
);
1319 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1320 for (h
= 0; h
< NF_ARP_NUMHOOKS
; h
++) {
1321 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1322 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1323 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1324 newinfo
->underflow
[h
] -= origsize
- *size
;
1329 static int translate_compat_table(const char *name
,
1330 unsigned int valid_hooks
,
1331 struct xt_table_info
**pinfo
,
1333 unsigned int total_size
,
1334 unsigned int number
,
1335 unsigned int *hook_entries
,
1336 unsigned int *underflows
)
1339 struct xt_table_info
*newinfo
, *info
;
1340 void *pos
, *entry0
, *entry1
;
1341 struct compat_arpt_entry
*iter0
;
1342 struct arpt_entry
*iter1
;
1349 info
->number
= number
;
1351 /* Init all hooks to impossible value. */
1352 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1353 info
->hook_entry
[i
] = 0xFFFFFFFF;
1354 info
->underflow
[i
] = 0xFFFFFFFF;
1357 duprintf("translate_compat_table: size %u\n", info
->size
);
1359 xt_compat_lock(NFPROTO_ARP
);
1360 xt_compat_init_offsets(NFPROTO_ARP
, number
);
1361 /* Walk through entries, checking offsets. */
1362 xt_entry_foreach(iter0
, entry0
, total_size
) {
1363 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1365 entry0
+ total_size
,
1376 duprintf("translate_compat_table: %u not %u entries\n",
1381 /* Check hooks all assigned */
1382 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1383 /* Only hooks which are valid */
1384 if (!(valid_hooks
& (1 << i
)))
1386 if (info
->hook_entry
[i
] == 0xFFFFFFFF) {
1387 duprintf("Invalid hook entry %u %u\n",
1388 i
, hook_entries
[i
]);
1391 if (info
->underflow
[i
] == 0xFFFFFFFF) {
1392 duprintf("Invalid underflow %u %u\n",
1399 newinfo
= xt_alloc_table_info(size
);
1403 newinfo
->number
= number
;
1404 for (i
= 0; i
< NF_ARP_NUMHOOKS
; i
++) {
1405 newinfo
->hook_entry
[i
] = info
->hook_entry
[i
];
1406 newinfo
->underflow
[i
] = info
->underflow
[i
];
1408 entry1
= newinfo
->entries
;
1411 xt_entry_foreach(iter0
, entry0
, total_size
) {
1412 ret
= compat_copy_entry_from_user(iter0
, &pos
, &size
,
1413 name
, newinfo
, entry1
);
1417 xt_compat_flush_offsets(NFPROTO_ARP
);
1418 xt_compat_unlock(NFPROTO_ARP
);
1423 if (!mark_source_chains(newinfo
, valid_hooks
, entry1
))
1427 xt_entry_foreach(iter1
, entry1
, newinfo
->size
) {
1428 iter1
->counters
.pcnt
= xt_percpu_counter_alloc();
1429 if (IS_ERR_VALUE(iter1
->counters
.pcnt
)) {
1434 ret
= check_target(iter1
, name
);
1436 xt_percpu_counter_free(iter1
->counters
.pcnt
);
1440 if (strcmp(arpt_get_target(iter1
)->u
.user
.name
,
1441 XT_ERROR_TARGET
) == 0)
1442 ++newinfo
->stacksize
;
1446 * The first i matches need cleanup_entry (calls ->destroy)
1447 * because they had called ->check already. The other j-i
1448 * entries need only release.
1452 xt_entry_foreach(iter0
, entry0
, newinfo
->size
) {
1457 compat_release_entry(iter0
);
1459 xt_entry_foreach(iter1
, entry1
, newinfo
->size
) {
1462 cleanup_entry(iter1
);
1464 xt_free_table_info(newinfo
);
1470 xt_free_table_info(info
);
1474 xt_free_table_info(newinfo
);
1476 xt_entry_foreach(iter0
, entry0
, total_size
) {
1479 compat_release_entry(iter0
);
1483 xt_compat_flush_offsets(NFPROTO_ARP
);
1484 xt_compat_unlock(NFPROTO_ARP
);
1488 struct compat_arpt_replace
{
1489 char name
[XT_TABLE_MAXNAMELEN
];
1493 u32 hook_entry
[NF_ARP_NUMHOOKS
];
1494 u32 underflow
[NF_ARP_NUMHOOKS
];
1496 compat_uptr_t counters
;
1497 struct compat_arpt_entry entries
[0];
1500 static int compat_do_replace(struct net
*net
, void __user
*user
,
1504 struct compat_arpt_replace tmp
;
1505 struct xt_table_info
*newinfo
;
1506 void *loc_cpu_entry
;
1507 struct arpt_entry
*iter
;
1509 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1512 /* overflow check */
1513 if (tmp
.size
>= INT_MAX
/ num_possible_cpus())
1515 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1517 if (tmp
.num_counters
== 0)
1520 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1522 newinfo
= xt_alloc_table_info(tmp
.size
);
1526 loc_cpu_entry
= newinfo
->entries
;
1527 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
), tmp
.size
) != 0) {
1532 ret
= translate_compat_table(tmp
.name
, tmp
.valid_hooks
,
1533 &newinfo
, &loc_cpu_entry
, tmp
.size
,
1534 tmp
.num_entries
, tmp
.hook_entry
,
1539 duprintf("compat_do_replace: Translated table\n");
1541 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1542 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1544 goto free_newinfo_untrans
;
1547 free_newinfo_untrans
:
1548 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1549 cleanup_entry(iter
);
1551 xt_free_table_info(newinfo
);
1555 static int compat_do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1560 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1564 case ARPT_SO_SET_REPLACE
:
1565 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1568 case ARPT_SO_SET_ADD_COUNTERS
:
1569 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1573 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1580 static int compat_copy_entry_to_user(struct arpt_entry
*e
, void __user
**dstptr
,
1581 compat_uint_t
*size
,
1582 struct xt_counters
*counters
,
1585 struct xt_entry_target
*t
;
1586 struct compat_arpt_entry __user
*ce
;
1587 u_int16_t target_offset
, next_offset
;
1588 compat_uint_t origsize
;
1592 ce
= (struct compat_arpt_entry __user
*)*dstptr
;
1593 if (copy_to_user(ce
, e
, sizeof(struct arpt_entry
)) != 0 ||
1594 copy_to_user(&ce
->counters
, &counters
[i
],
1595 sizeof(counters
[i
])) != 0)
1598 *dstptr
+= sizeof(struct compat_arpt_entry
);
1599 *size
-= sizeof(struct arpt_entry
) - sizeof(struct compat_arpt_entry
);
1601 target_offset
= e
->target_offset
- (origsize
- *size
);
1603 t
= arpt_get_target(e
);
1604 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1607 next_offset
= e
->next_offset
- (origsize
- *size
);
1608 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1609 put_user(next_offset
, &ce
->next_offset
) != 0)
1614 static int compat_copy_entries_to_user(unsigned int total_size
,
1615 struct xt_table
*table
,
1616 void __user
*userptr
)
1618 struct xt_counters
*counters
;
1619 const struct xt_table_info
*private = table
->private;
1624 struct arpt_entry
*iter
;
1626 counters
= alloc_counters(table
);
1627 if (IS_ERR(counters
))
1628 return PTR_ERR(counters
);
1632 xt_entry_foreach(iter
, private->entries
, total_size
) {
1633 ret
= compat_copy_entry_to_user(iter
, &pos
,
1634 &size
, counters
, i
++);
1642 struct compat_arpt_get_entries
{
1643 char name
[XT_TABLE_MAXNAMELEN
];
1645 struct compat_arpt_entry entrytable
[0];
1648 static int compat_get_entries(struct net
*net
,
1649 struct compat_arpt_get_entries __user
*uptr
,
1653 struct compat_arpt_get_entries get
;
1656 if (*len
< sizeof(get
)) {
1657 duprintf("compat_get_entries: %u < %zu\n", *len
, sizeof(get
));
1660 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1662 if (*len
!= sizeof(struct compat_arpt_get_entries
) + get
.size
) {
1663 duprintf("compat_get_entries: %u != %zu\n",
1664 *len
, sizeof(get
) + get
.size
);
1667 get
.name
[sizeof(get
.name
) - 1] = '\0';
1669 xt_compat_lock(NFPROTO_ARP
);
1670 t
= xt_find_table_lock(net
, NFPROTO_ARP
, get
.name
);
1671 if (!IS_ERR_OR_NULL(t
)) {
1672 const struct xt_table_info
*private = t
->private;
1673 struct xt_table_info info
;
1675 duprintf("t->private->number = %u\n", private->number
);
1676 ret
= compat_table_info(private, &info
);
1677 if (!ret
&& get
.size
== info
.size
) {
1678 ret
= compat_copy_entries_to_user(private->size
,
1679 t
, uptr
->entrytable
);
1681 duprintf("compat_get_entries: I've got %u not %u!\n",
1682 private->size
, get
.size
);
1685 xt_compat_flush_offsets(NFPROTO_ARP
);
1689 ret
= t
? PTR_ERR(t
) : -ENOENT
;
1691 xt_compat_unlock(NFPROTO_ARP
);
1695 static int do_arpt_get_ctl(struct sock
*, int, void __user
*, int *);
1697 static int compat_do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1702 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1706 case ARPT_SO_GET_INFO
:
1707 ret
= get_info(sock_net(sk
), user
, len
, 1);
1709 case ARPT_SO_GET_ENTRIES
:
1710 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1713 ret
= do_arpt_get_ctl(sk
, cmd
, user
, len
);
1719 static int do_arpt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1723 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1727 case ARPT_SO_SET_REPLACE
:
1728 ret
= do_replace(sock_net(sk
), user
, len
);
1731 case ARPT_SO_SET_ADD_COUNTERS
:
1732 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1736 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd
);
1743 static int do_arpt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1747 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1751 case ARPT_SO_GET_INFO
:
1752 ret
= get_info(sock_net(sk
), user
, len
, 0);
1755 case ARPT_SO_GET_ENTRIES
:
1756 ret
= get_entries(sock_net(sk
), user
, len
);
1759 case ARPT_SO_GET_REVISION_TARGET
: {
1760 struct xt_get_revision rev
;
1762 if (*len
!= sizeof(rev
)) {
1766 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1770 rev
.name
[sizeof(rev
.name
)-1] = 0;
1772 try_then_request_module(xt_find_revision(NFPROTO_ARP
, rev
.name
,
1773 rev
.revision
, 1, &ret
),
1774 "arpt_%s", rev
.name
);
1779 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd
);
1786 static void __arpt_unregister_table(struct xt_table
*table
)
1788 struct xt_table_info
*private;
1789 void *loc_cpu_entry
;
1790 struct module
*table_owner
= table
->me
;
1791 struct arpt_entry
*iter
;
1793 private = xt_unregister_table(table
);
1795 /* Decrease module usage counts and free resources */
1796 loc_cpu_entry
= private->entries
;
1797 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1798 cleanup_entry(iter
);
1799 if (private->number
> private->initial_entries
)
1800 module_put(table_owner
);
1801 xt_free_table_info(private);
1804 int arpt_register_table(struct net
*net
,
1805 const struct xt_table
*table
,
1806 const struct arpt_replace
*repl
,
1807 const struct nf_hook_ops
*ops
,
1808 struct xt_table
**res
)
1811 struct xt_table_info
*newinfo
;
1812 struct xt_table_info bootstrap
= {0};
1813 void *loc_cpu_entry
;
1814 struct xt_table
*new_table
;
1816 newinfo
= xt_alloc_table_info(repl
->size
);
1820 loc_cpu_entry
= newinfo
->entries
;
1821 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1823 ret
= translate_table(newinfo
, loc_cpu_entry
, repl
);
1824 duprintf("arpt_register_table: translate table gives %d\n", ret
);
1828 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1829 if (IS_ERR(new_table
)) {
1830 ret
= PTR_ERR(new_table
);
1834 /* set res now, will see skbs right after nf_register_net_hooks */
1835 WRITE_ONCE(*res
, new_table
);
1837 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1839 __arpt_unregister_table(new_table
);
1846 xt_free_table_info(newinfo
);
1850 void arpt_unregister_table(struct net
*net
, struct xt_table
*table
,
1851 const struct nf_hook_ops
*ops
)
1853 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1854 __arpt_unregister_table(table
);
1857 /* The built-in targets: standard (NULL) and error. */
1858 static struct xt_target arpt_builtin_tg
[] __read_mostly
= {
1860 .name
= XT_STANDARD_TARGET
,
1861 .targetsize
= sizeof(int),
1862 .family
= NFPROTO_ARP
,
1863 #ifdef CONFIG_COMPAT
1864 .compatsize
= sizeof(compat_int_t
),
1865 .compat_from_user
= compat_standard_from_user
,
1866 .compat_to_user
= compat_standard_to_user
,
1870 .name
= XT_ERROR_TARGET
,
1871 .target
= arpt_error
,
1872 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1873 .family
= NFPROTO_ARP
,
1877 static struct nf_sockopt_ops arpt_sockopts
= {
1879 .set_optmin
= ARPT_BASE_CTL
,
1880 .set_optmax
= ARPT_SO_SET_MAX
+1,
1881 .set
= do_arpt_set_ctl
,
1882 #ifdef CONFIG_COMPAT
1883 .compat_set
= compat_do_arpt_set_ctl
,
1885 .get_optmin
= ARPT_BASE_CTL
,
1886 .get_optmax
= ARPT_SO_GET_MAX
+1,
1887 .get
= do_arpt_get_ctl
,
1888 #ifdef CONFIG_COMPAT
1889 .compat_get
= compat_do_arpt_get_ctl
,
1891 .owner
= THIS_MODULE
,
1894 static int __net_init
arp_tables_net_init(struct net
*net
)
1896 return xt_proto_init(net
, NFPROTO_ARP
);
1899 static void __net_exit
arp_tables_net_exit(struct net
*net
)
1901 xt_proto_fini(net
, NFPROTO_ARP
);
1904 static struct pernet_operations arp_tables_net_ops
= {
1905 .init
= arp_tables_net_init
,
1906 .exit
= arp_tables_net_exit
,
1909 static int __init
arp_tables_init(void)
1913 ret
= register_pernet_subsys(&arp_tables_net_ops
);
1917 /* No one else will be downing sem now, so we won't sleep */
1918 ret
= xt_register_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1922 /* Register setsockopt */
1923 ret
= nf_register_sockopt(&arpt_sockopts
);
1927 pr_info("arp_tables: (C) 2002 David S. Miller\n");
1931 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1933 unregister_pernet_subsys(&arp_tables_net_ops
);
1938 static void __exit
arp_tables_fini(void)
1940 nf_unregister_sockopt(&arpt_sockopts
);
1941 xt_unregister_targets(arpt_builtin_tg
, ARRAY_SIZE(arpt_builtin_tg
));
1942 unregister_pernet_subsys(&arp_tables_net_ops
);
1945 EXPORT_SYMBOL(arpt_register_table
);
1946 EXPORT_SYMBOL(arpt_unregister_table
);
1947 EXPORT_SYMBOL(arpt_do_table
);
1949 module_init(arp_tables_init
);
1950 module_exit(arp_tables_fini
);