xref: /linux/net/ipv4/netfilter/arp_tables.c (revision ab520be8cd5d56867fc95cfbc34b90880faf1f9d)
1 /*
2  * Packet matching code for ARP packets.
3  *
4  * Based heavily, if not almost entirely, upon ip_tables.c framework.
5  *
6  * Some ARP specific bits are:
7  *
8  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
9  * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
10  *
11  */
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>
26 #include <net/sock.h>
27 #include <linux/uaccess.h>
28 
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_arp/arp_tables.h>
31 #include "../../netfilter/xt_repldata.h"
32 
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
35 MODULE_DESCRIPTION("arptables core");
36 
37 void *arpt_alloc_initial_table(const struct xt_table *info)
38 {
39 	return xt_alloc_initial_table(arpt, ARPT);
40 }
41 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
42 
43 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
44 				      const char *hdr_addr, int len)
45 {
46 	int i, ret;
47 
48 	if (len > ARPT_DEV_ADDR_LEN_MAX)
49 		len = ARPT_DEV_ADDR_LEN_MAX;
50 
51 	ret = 0;
52 	for (i = 0; i < len; i++)
53 		ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
54 
55 	return ret != 0;
56 }
57 
58 /*
59  * Unfortunately, _b and _mask are not aligned to an int (or long int)
60  * Some arches dont care, unrolling the loop is a win on them.
61  * For other arches, we only have a 16bit alignement.
62  */
63 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
64 {
65 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
66 	unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
67 #else
68 	unsigned long ret = 0;
69 	const u16 *a = (const u16 *)_a;
70 	const u16 *b = (const u16 *)_b;
71 	const u16 *mask = (const u16 *)_mask;
72 	int i;
73 
74 	for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
75 		ret |= (a[i] ^ b[i]) & mask[i];
76 #endif
77 	return ret;
78 }
79 
80 /* Returns whether packet matches rule or not. */
81 static inline int arp_packet_match(const struct arphdr *arphdr,
82 				   struct net_device *dev,
83 				   const char *indev,
84 				   const char *outdev,
85 				   const struct arpt_arp *arpinfo)
86 {
87 	const char *arpptr = (char *)(arphdr + 1);
88 	const char *src_devaddr, *tgt_devaddr;
89 	__be32 src_ipaddr, tgt_ipaddr;
90 	long ret;
91 
92 	if (NF_INVF(arpinfo, ARPT_INV_ARPOP,
93 		    (arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop))
94 		return 0;
95 
96 	if (NF_INVF(arpinfo, ARPT_INV_ARPHRD,
97 		    (arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd))
98 		return 0;
99 
100 	if (NF_INVF(arpinfo, ARPT_INV_ARPPRO,
101 		    (arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro))
102 		return 0;
103 
104 	if (NF_INVF(arpinfo, ARPT_INV_ARPHLN,
105 		    (arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln))
106 		return 0;
107 
108 	src_devaddr = arpptr;
109 	arpptr += dev->addr_len;
110 	memcpy(&src_ipaddr, arpptr, sizeof(u32));
111 	arpptr += sizeof(u32);
112 	tgt_devaddr = arpptr;
113 	arpptr += dev->addr_len;
114 	memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
115 
116 	if (NF_INVF(arpinfo, ARPT_INV_SRCDEVADDR,
117 		    arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr,
118 					dev->addr_len)) ||
119 	    NF_INVF(arpinfo, ARPT_INV_TGTDEVADDR,
120 		    arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr,
121 					dev->addr_len)))
122 		return 0;
123 
124 	if (NF_INVF(arpinfo, ARPT_INV_SRCIP,
125 		    (src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr) ||
126 	    NF_INVF(arpinfo, ARPT_INV_TGTIP,
127 		    (tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr))
128 		return 0;
129 
130 	/* Look for ifname matches.  */
131 	ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
132 
133 	if (NF_INVF(arpinfo, ARPT_INV_VIA_IN, ret != 0))
134 		return 0;
135 
136 	ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
137 
138 	if (NF_INVF(arpinfo, ARPT_INV_VIA_OUT, ret != 0))
139 		return 0;
140 
141 	return 1;
142 }
143 
144 static inline int arp_checkentry(const struct arpt_arp *arp)
145 {
146 	if (arp->flags & ~ARPT_F_MASK)
147 		return 0;
148 	if (arp->invflags & ~ARPT_INV_MASK)
149 		return 0;
150 
151 	return 1;
152 }
153 
154 static unsigned int
155 arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
156 {
157 	net_err_ratelimited("arp_tables: error: '%s'\n",
158 			    (const char *)par->targinfo);
159 
160 	return NF_DROP;
161 }
162 
163 static inline const struct xt_entry_target *
164 arpt_get_target_c(const struct arpt_entry *e)
165 {
166 	return arpt_get_target((struct arpt_entry *)e);
167 }
168 
169 static inline struct arpt_entry *
170 get_entry(const void *base, unsigned int offset)
171 {
172 	return (struct arpt_entry *)(base + offset);
173 }
174 
175 static inline
176 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
177 {
178 	return (void *)entry + entry->next_offset;
179 }
180 
181 unsigned int arpt_do_table(struct sk_buff *skb,
182 			   const struct nf_hook_state *state,
183 			   struct xt_table *table)
184 {
185 	unsigned int hook = state->hook;
186 	static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
187 	unsigned int verdict = NF_DROP;
188 	const struct arphdr *arp;
189 	struct arpt_entry *e, **jumpstack;
190 	const char *indev, *outdev;
191 	const void *table_base;
192 	unsigned int cpu, stackidx = 0;
193 	const struct xt_table_info *private;
194 	struct xt_action_param acpar;
195 	unsigned int addend;
196 
197 	if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
198 		return NF_DROP;
199 
200 	indev = state->in ? state->in->name : nulldevname;
201 	outdev = state->out ? state->out->name : nulldevname;
202 
203 	local_bh_disable();
204 	addend = xt_write_recseq_begin();
205 	private = table->private;
206 	cpu     = smp_processor_id();
207 	/*
208 	 * Ensure we load private-> members after we've fetched the base
209 	 * pointer.
210 	 */
211 	smp_read_barrier_depends();
212 	table_base = private->entries;
213 	jumpstack  = (struct arpt_entry **)private->jumpstack[cpu];
214 
215 	/* No TEE support for arptables, so no need to switch to alternate
216 	 * stack.  All targets that reenter must return absolute verdicts.
217 	 */
218 	e = get_entry(table_base, private->hook_entry[hook]);
219 
220 	acpar.state   = state;
221 	acpar.hotdrop = false;
222 
223 	arp = arp_hdr(skb);
224 	do {
225 		const struct xt_entry_target *t;
226 		struct xt_counters *counter;
227 
228 		if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
229 			e = arpt_next_entry(e);
230 			continue;
231 		}
232 
233 		counter = xt_get_this_cpu_counter(&e->counters);
234 		ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
235 
236 		t = arpt_get_target_c(e);
237 
238 		/* Standard target? */
239 		if (!t->u.kernel.target->target) {
240 			int v;
241 
242 			v = ((struct xt_standard_target *)t)->verdict;
243 			if (v < 0) {
244 				/* Pop from stack? */
245 				if (v != XT_RETURN) {
246 					verdict = (unsigned int)(-v) - 1;
247 					break;
248 				}
249 				if (stackidx == 0) {
250 					e = get_entry(table_base,
251 						      private->underflow[hook]);
252 				} else {
253 					e = jumpstack[--stackidx];
254 					e = arpt_next_entry(e);
255 				}
256 				continue;
257 			}
258 			if (table_base + v
259 			    != arpt_next_entry(e)) {
260 				jumpstack[stackidx++] = e;
261 			}
262 
263 			e = get_entry(table_base, v);
264 			continue;
265 		}
266 
267 		acpar.target   = t->u.kernel.target;
268 		acpar.targinfo = t->data;
269 		verdict = t->u.kernel.target->target(skb, &acpar);
270 
271 		/* Target might have changed stuff. */
272 		arp = arp_hdr(skb);
273 
274 		if (verdict == XT_CONTINUE)
275 			e = arpt_next_entry(e);
276 		else
277 			/* Verdict */
278 			break;
279 	} while (!acpar.hotdrop);
280 	xt_write_recseq_end(addend);
281 	local_bh_enable();
282 
283 	if (acpar.hotdrop)
284 		return NF_DROP;
285 	else
286 		return verdict;
287 }
288 
289 /* All zeroes == unconditional rule. */
290 static inline bool unconditional(const struct arpt_entry *e)
291 {
292 	static const struct arpt_arp uncond;
293 
294 	return e->target_offset == sizeof(struct arpt_entry) &&
295 	       memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
296 }
297 
298 /* Figures out from what hook each rule can be called: returns 0 if
299  * there are loops.  Puts hook bitmask in comefrom.
300  */
301 static int mark_source_chains(const struct xt_table_info *newinfo,
302 			      unsigned int valid_hooks, void *entry0,
303 			      unsigned int *offsets)
304 {
305 	unsigned int hook;
306 
307 	/* No recursion; use packet counter to save back ptrs (reset
308 	 * to 0 as we leave), and comefrom to save source hook bitmask.
309 	 */
310 	for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
311 		unsigned int pos = newinfo->hook_entry[hook];
312 		struct arpt_entry *e
313 			= (struct arpt_entry *)(entry0 + pos);
314 
315 		if (!(valid_hooks & (1 << hook)))
316 			continue;
317 
318 		/* Set initial back pointer. */
319 		e->counters.pcnt = pos;
320 
321 		for (;;) {
322 			const struct xt_standard_target *t
323 				= (void *)arpt_get_target_c(e);
324 			int visited = e->comefrom & (1 << hook);
325 
326 			if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
327 				return 0;
328 
329 			e->comefrom
330 				|= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
331 
332 			/* Unconditional return/END. */
333 			if ((unconditional(e) &&
334 			     (strcmp(t->target.u.user.name,
335 				     XT_STANDARD_TARGET) == 0) &&
336 			     t->verdict < 0) || visited) {
337 				unsigned int oldpos, size;
338 
339 				if ((strcmp(t->target.u.user.name,
340 					    XT_STANDARD_TARGET) == 0) &&
341 				    t->verdict < -NF_MAX_VERDICT - 1)
342 					return 0;
343 
344 				/* Return: backtrack through the last
345 				 * big jump.
346 				 */
347 				do {
348 					e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
349 					oldpos = pos;
350 					pos = e->counters.pcnt;
351 					e->counters.pcnt = 0;
352 
353 					/* We're at the start. */
354 					if (pos == oldpos)
355 						goto next;
356 
357 					e = (struct arpt_entry *)
358 						(entry0 + pos);
359 				} while (oldpos == pos + e->next_offset);
360 
361 				/* Move along one */
362 				size = e->next_offset;
363 				e = (struct arpt_entry *)
364 					(entry0 + pos + size);
365 				if (pos + size >= newinfo->size)
366 					return 0;
367 				e->counters.pcnt = pos;
368 				pos += size;
369 			} else {
370 				int newpos = t->verdict;
371 
372 				if (strcmp(t->target.u.user.name,
373 					   XT_STANDARD_TARGET) == 0 &&
374 				    newpos >= 0) {
375 					/* This a jump; chase it. */
376 					if (!xt_find_jump_offset(offsets, newpos,
377 								 newinfo->number))
378 						return 0;
379 					e = (struct arpt_entry *)
380 						(entry0 + newpos);
381 				} else {
382 					/* ... this is a fallthru */
383 					newpos = pos + e->next_offset;
384 					if (newpos >= newinfo->size)
385 						return 0;
386 				}
387 				e = (struct arpt_entry *)
388 					(entry0 + newpos);
389 				e->counters.pcnt = pos;
390 				pos = newpos;
391 			}
392 		}
393 next:		;
394 	}
395 	return 1;
396 }
397 
398 static inline int check_target(struct arpt_entry *e, const char *name)
399 {
400 	struct xt_entry_target *t = arpt_get_target(e);
401 	struct xt_tgchk_param par = {
402 		.table     = name,
403 		.entryinfo = e,
404 		.target    = t->u.kernel.target,
405 		.targinfo  = t->data,
406 		.hook_mask = e->comefrom,
407 		.family    = NFPROTO_ARP,
408 	};
409 
410 	return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
411 }
412 
413 static inline int
414 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
415 		 struct xt_percpu_counter_alloc_state *alloc_state)
416 {
417 	struct xt_entry_target *t;
418 	struct xt_target *target;
419 	int ret;
420 
421 	if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
422 		return -ENOMEM;
423 
424 	t = arpt_get_target(e);
425 	target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
426 					t->u.user.revision);
427 	if (IS_ERR(target)) {
428 		ret = PTR_ERR(target);
429 		goto out;
430 	}
431 	t->u.kernel.target = target;
432 
433 	ret = check_target(e, name);
434 	if (ret)
435 		goto err;
436 	return 0;
437 err:
438 	module_put(t->u.kernel.target->me);
439 out:
440 	xt_percpu_counter_free(&e->counters);
441 
442 	return ret;
443 }
444 
445 static bool check_underflow(const struct arpt_entry *e)
446 {
447 	const struct xt_entry_target *t;
448 	unsigned int verdict;
449 
450 	if (!unconditional(e))
451 		return false;
452 	t = arpt_get_target_c(e);
453 	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
454 		return false;
455 	verdict = ((struct xt_standard_target *)t)->verdict;
456 	verdict = -verdict - 1;
457 	return verdict == NF_DROP || verdict == NF_ACCEPT;
458 }
459 
460 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
461 					     struct xt_table_info *newinfo,
462 					     const unsigned char *base,
463 					     const unsigned char *limit,
464 					     const unsigned int *hook_entries,
465 					     const unsigned int *underflows,
466 					     unsigned int valid_hooks)
467 {
468 	unsigned int h;
469 	int err;
470 
471 	if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
472 	    (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
473 	    (unsigned char *)e + e->next_offset > limit)
474 		return -EINVAL;
475 
476 	if (e->next_offset
477 	    < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target))
478 		return -EINVAL;
479 
480 	if (!arp_checkentry(&e->arp))
481 		return -EINVAL;
482 
483 	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
484 				     e->next_offset);
485 	if (err)
486 		return err;
487 
488 	/* Check hooks & underflows */
489 	for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
490 		if (!(valid_hooks & (1 << h)))
491 			continue;
492 		if ((unsigned char *)e - base == hook_entries[h])
493 			newinfo->hook_entry[h] = hook_entries[h];
494 		if ((unsigned char *)e - base == underflows[h]) {
495 			if (!check_underflow(e))
496 				return -EINVAL;
497 
498 			newinfo->underflow[h] = underflows[h];
499 		}
500 	}
501 
502 	/* Clear counters and comefrom */
503 	e->counters = ((struct xt_counters) { 0, 0 });
504 	e->comefrom = 0;
505 	return 0;
506 }
507 
508 static inline void cleanup_entry(struct arpt_entry *e)
509 {
510 	struct xt_tgdtor_param par;
511 	struct xt_entry_target *t;
512 
513 	t = arpt_get_target(e);
514 	par.target   = t->u.kernel.target;
515 	par.targinfo = t->data;
516 	par.family   = NFPROTO_ARP;
517 	if (par.target->destroy != NULL)
518 		par.target->destroy(&par);
519 	module_put(par.target->me);
520 	xt_percpu_counter_free(&e->counters);
521 }
522 
523 /* Checks and translates the user-supplied table segment (held in
524  * newinfo).
525  */
526 static int translate_table(struct xt_table_info *newinfo, void *entry0,
527 			   const struct arpt_replace *repl)
528 {
529 	struct xt_percpu_counter_alloc_state alloc_state = { 0 };
530 	struct arpt_entry *iter;
531 	unsigned int *offsets;
532 	unsigned int i;
533 	int ret = 0;
534 
535 	newinfo->size = repl->size;
536 	newinfo->number = repl->num_entries;
537 
538 	/* Init all hooks to impossible value. */
539 	for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
540 		newinfo->hook_entry[i] = 0xFFFFFFFF;
541 		newinfo->underflow[i] = 0xFFFFFFFF;
542 	}
543 
544 	offsets = xt_alloc_entry_offsets(newinfo->number);
545 	if (!offsets)
546 		return -ENOMEM;
547 	i = 0;
548 
549 	/* Walk through entries, checking offsets. */
550 	xt_entry_foreach(iter, entry0, newinfo->size) {
551 		ret = check_entry_size_and_hooks(iter, newinfo, entry0,
552 						 entry0 + repl->size,
553 						 repl->hook_entry,
554 						 repl->underflow,
555 						 repl->valid_hooks);
556 		if (ret != 0)
557 			goto out_free;
558 		if (i < repl->num_entries)
559 			offsets[i] = (void *)iter - entry0;
560 		++i;
561 		if (strcmp(arpt_get_target(iter)->u.user.name,
562 		    XT_ERROR_TARGET) == 0)
563 			++newinfo->stacksize;
564 	}
565 	if (ret != 0)
566 		goto out_free;
567 
568 	ret = -EINVAL;
569 	if (i != repl->num_entries)
570 		goto out_free;
571 
572 	/* Check hooks all assigned */
573 	for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
574 		/* Only hooks which are valid */
575 		if (!(repl->valid_hooks & (1 << i)))
576 			continue;
577 		if (newinfo->hook_entry[i] == 0xFFFFFFFF)
578 			goto out_free;
579 		if (newinfo->underflow[i] == 0xFFFFFFFF)
580 			goto out_free;
581 	}
582 
583 	if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
584 		ret = -ELOOP;
585 		goto out_free;
586 	}
587 	kvfree(offsets);
588 
589 	/* Finally, each sanity check must pass */
590 	i = 0;
591 	xt_entry_foreach(iter, entry0, newinfo->size) {
592 		ret = find_check_entry(iter, repl->name, repl->size,
593 				       &alloc_state);
594 		if (ret != 0)
595 			break;
596 		++i;
597 	}
598 
599 	if (ret != 0) {
600 		xt_entry_foreach(iter, entry0, newinfo->size) {
601 			if (i-- == 0)
602 				break;
603 			cleanup_entry(iter);
604 		}
605 		return ret;
606 	}
607 
608 	return ret;
609  out_free:
610 	kvfree(offsets);
611 	return ret;
612 }
613 
614 static void get_counters(const struct xt_table_info *t,
615 			 struct xt_counters counters[])
616 {
617 	struct arpt_entry *iter;
618 	unsigned int cpu;
619 	unsigned int i;
620 
621 	for_each_possible_cpu(cpu) {
622 		seqcount_t *s = &per_cpu(xt_recseq, cpu);
623 
624 		i = 0;
625 		xt_entry_foreach(iter, t->entries, t->size) {
626 			struct xt_counters *tmp;
627 			u64 bcnt, pcnt;
628 			unsigned int start;
629 
630 			tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
631 			do {
632 				start = read_seqcount_begin(s);
633 				bcnt = tmp->bcnt;
634 				pcnt = tmp->pcnt;
635 			} while (read_seqcount_retry(s, start));
636 
637 			ADD_COUNTER(counters[i], bcnt, pcnt);
638 			++i;
639 		}
640 	}
641 }
642 
643 static struct xt_counters *alloc_counters(const struct xt_table *table)
644 {
645 	unsigned int countersize;
646 	struct xt_counters *counters;
647 	const struct xt_table_info *private = table->private;
648 
649 	/* We need atomic snapshot of counters: rest doesn't change
650 	 * (other than comefrom, which userspace doesn't care
651 	 * about).
652 	 */
653 	countersize = sizeof(struct xt_counters) * private->number;
654 	counters = vzalloc(countersize);
655 
656 	if (counters == NULL)
657 		return ERR_PTR(-ENOMEM);
658 
659 	get_counters(private, counters);
660 
661 	return counters;
662 }
663 
664 static int copy_entries_to_user(unsigned int total_size,
665 				const struct xt_table *table,
666 				void __user *userptr)
667 {
668 	unsigned int off, num;
669 	const struct arpt_entry *e;
670 	struct xt_counters *counters;
671 	struct xt_table_info *private = table->private;
672 	int ret = 0;
673 	void *loc_cpu_entry;
674 
675 	counters = alloc_counters(table);
676 	if (IS_ERR(counters))
677 		return PTR_ERR(counters);
678 
679 	loc_cpu_entry = private->entries;
680 	/* ... then copy entire thing ... */
681 	if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
682 		ret = -EFAULT;
683 		goto free_counters;
684 	}
685 
686 	/* FIXME: use iterator macros --RR */
687 	/* ... then go back and fix counters and names */
688 	for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
689 		const struct xt_entry_target *t;
690 
691 		e = (struct arpt_entry *)(loc_cpu_entry + off);
692 		if (copy_to_user(userptr + off
693 				 + offsetof(struct arpt_entry, counters),
694 				 &counters[num],
695 				 sizeof(counters[num])) != 0) {
696 			ret = -EFAULT;
697 			goto free_counters;
698 		}
699 
700 		t = arpt_get_target_c(e);
701 		if (copy_to_user(userptr + off + e->target_offset
702 				 + offsetof(struct xt_entry_target,
703 					    u.user.name),
704 				 t->u.kernel.target->name,
705 				 strlen(t->u.kernel.target->name)+1) != 0) {
706 			ret = -EFAULT;
707 			goto free_counters;
708 		}
709 	}
710 
711  free_counters:
712 	vfree(counters);
713 	return ret;
714 }
715 
716 #ifdef CONFIG_COMPAT
717 static void compat_standard_from_user(void *dst, const void *src)
718 {
719 	int v = *(compat_int_t *)src;
720 
721 	if (v > 0)
722 		v += xt_compat_calc_jump(NFPROTO_ARP, v);
723 	memcpy(dst, &v, sizeof(v));
724 }
725 
726 static int compat_standard_to_user(void __user *dst, const void *src)
727 {
728 	compat_int_t cv = *(int *)src;
729 
730 	if (cv > 0)
731 		cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
732 	return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
733 }
734 
735 static int compat_calc_entry(const struct arpt_entry *e,
736 			     const struct xt_table_info *info,
737 			     const void *base, struct xt_table_info *newinfo)
738 {
739 	const struct xt_entry_target *t;
740 	unsigned int entry_offset;
741 	int off, i, ret;
742 
743 	off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
744 	entry_offset = (void *)e - base;
745 
746 	t = arpt_get_target_c(e);
747 	off += xt_compat_target_offset(t->u.kernel.target);
748 	newinfo->size -= off;
749 	ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
750 	if (ret)
751 		return ret;
752 
753 	for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
754 		if (info->hook_entry[i] &&
755 		    (e < (struct arpt_entry *)(base + info->hook_entry[i])))
756 			newinfo->hook_entry[i] -= off;
757 		if (info->underflow[i] &&
758 		    (e < (struct arpt_entry *)(base + info->underflow[i])))
759 			newinfo->underflow[i] -= off;
760 	}
761 	return 0;
762 }
763 
764 static int compat_table_info(const struct xt_table_info *info,
765 			     struct xt_table_info *newinfo)
766 {
767 	struct arpt_entry *iter;
768 	const void *loc_cpu_entry;
769 	int ret;
770 
771 	if (!newinfo || !info)
772 		return -EINVAL;
773 
774 	/* we dont care about newinfo->entries */
775 	memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
776 	newinfo->initial_entries = 0;
777 	loc_cpu_entry = info->entries;
778 	xt_compat_init_offsets(NFPROTO_ARP, info->number);
779 	xt_entry_foreach(iter, loc_cpu_entry, info->size) {
780 		ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
781 		if (ret != 0)
782 			return ret;
783 	}
784 	return 0;
785 }
786 #endif
787 
788 static int get_info(struct net *net, void __user *user,
789 		    const int *len, int compat)
790 {
791 	char name[XT_TABLE_MAXNAMELEN];
792 	struct xt_table *t;
793 	int ret;
794 
795 	if (*len != sizeof(struct arpt_getinfo))
796 		return -EINVAL;
797 
798 	if (copy_from_user(name, user, sizeof(name)) != 0)
799 		return -EFAULT;
800 
801 	name[XT_TABLE_MAXNAMELEN-1] = '\0';
802 #ifdef CONFIG_COMPAT
803 	if (compat)
804 		xt_compat_lock(NFPROTO_ARP);
805 #endif
806 	t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
807 				    "arptable_%s", name);
808 	if (t) {
809 		struct arpt_getinfo info;
810 		const struct xt_table_info *private = t->private;
811 #ifdef CONFIG_COMPAT
812 		struct xt_table_info tmp;
813 
814 		if (compat) {
815 			ret = compat_table_info(private, &tmp);
816 			xt_compat_flush_offsets(NFPROTO_ARP);
817 			private = &tmp;
818 		}
819 #endif
820 		memset(&info, 0, sizeof(info));
821 		info.valid_hooks = t->valid_hooks;
822 		memcpy(info.hook_entry, private->hook_entry,
823 		       sizeof(info.hook_entry));
824 		memcpy(info.underflow, private->underflow,
825 		       sizeof(info.underflow));
826 		info.num_entries = private->number;
827 		info.size = private->size;
828 		strcpy(info.name, name);
829 
830 		if (copy_to_user(user, &info, *len) != 0)
831 			ret = -EFAULT;
832 		else
833 			ret = 0;
834 		xt_table_unlock(t);
835 		module_put(t->me);
836 	} else
837 		ret = -ENOENT;
838 #ifdef CONFIG_COMPAT
839 	if (compat)
840 		xt_compat_unlock(NFPROTO_ARP);
841 #endif
842 	return ret;
843 }
844 
845 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
846 		       const int *len)
847 {
848 	int ret;
849 	struct arpt_get_entries get;
850 	struct xt_table *t;
851 
852 	if (*len < sizeof(get))
853 		return -EINVAL;
854 	if (copy_from_user(&get, uptr, sizeof(get)) != 0)
855 		return -EFAULT;
856 	if (*len != sizeof(struct arpt_get_entries) + get.size)
857 		return -EINVAL;
858 
859 	get.name[sizeof(get.name) - 1] = '\0';
860 
861 	t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
862 	if (t) {
863 		const struct xt_table_info *private = t->private;
864 
865 		if (get.size == private->size)
866 			ret = copy_entries_to_user(private->size,
867 						   t, uptr->entrytable);
868 		else
869 			ret = -EAGAIN;
870 
871 		module_put(t->me);
872 		xt_table_unlock(t);
873 	} else
874 		ret = -ENOENT;
875 
876 	return ret;
877 }
878 
879 static int __do_replace(struct net *net, const char *name,
880 			unsigned int valid_hooks,
881 			struct xt_table_info *newinfo,
882 			unsigned int num_counters,
883 			void __user *counters_ptr)
884 {
885 	int ret;
886 	struct xt_table *t;
887 	struct xt_table_info *oldinfo;
888 	struct xt_counters *counters;
889 	void *loc_cpu_old_entry;
890 	struct arpt_entry *iter;
891 
892 	ret = 0;
893 	counters = vzalloc(num_counters * sizeof(struct xt_counters));
894 	if (!counters) {
895 		ret = -ENOMEM;
896 		goto out;
897 	}
898 
899 	t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
900 				    "arptable_%s", name);
901 	if (!t) {
902 		ret = -ENOENT;
903 		goto free_newinfo_counters_untrans;
904 	}
905 
906 	/* You lied! */
907 	if (valid_hooks != t->valid_hooks) {
908 		ret = -EINVAL;
909 		goto put_module;
910 	}
911 
912 	oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
913 	if (!oldinfo)
914 		goto put_module;
915 
916 	/* Update module usage count based on number of rules */
917 	if ((oldinfo->number > oldinfo->initial_entries) ||
918 	    (newinfo->number <= oldinfo->initial_entries))
919 		module_put(t->me);
920 	if ((oldinfo->number > oldinfo->initial_entries) &&
921 	    (newinfo->number <= oldinfo->initial_entries))
922 		module_put(t->me);
923 
924 	/* Get the old counters, and synchronize with replace */
925 	get_counters(oldinfo, counters);
926 
927 	/* Decrease module usage counts and free resource */
928 	loc_cpu_old_entry = oldinfo->entries;
929 	xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
930 		cleanup_entry(iter);
931 
932 	xt_free_table_info(oldinfo);
933 	if (copy_to_user(counters_ptr, counters,
934 			 sizeof(struct xt_counters) * num_counters) != 0) {
935 		/* Silent error, can't fail, new table is already in place */
936 		net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
937 	}
938 	vfree(counters);
939 	xt_table_unlock(t);
940 	return ret;
941 
942  put_module:
943 	module_put(t->me);
944 	xt_table_unlock(t);
945  free_newinfo_counters_untrans:
946 	vfree(counters);
947  out:
948 	return ret;
949 }
950 
951 static int do_replace(struct net *net, const void __user *user,
952 		      unsigned int len)
953 {
954 	int ret;
955 	struct arpt_replace tmp;
956 	struct xt_table_info *newinfo;
957 	void *loc_cpu_entry;
958 	struct arpt_entry *iter;
959 
960 	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
961 		return -EFAULT;
962 
963 	/* overflow check */
964 	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
965 		return -ENOMEM;
966 	if (tmp.num_counters == 0)
967 		return -EINVAL;
968 
969 	tmp.name[sizeof(tmp.name)-1] = 0;
970 
971 	newinfo = xt_alloc_table_info(tmp.size);
972 	if (!newinfo)
973 		return -ENOMEM;
974 
975 	loc_cpu_entry = newinfo->entries;
976 	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
977 			   tmp.size) != 0) {
978 		ret = -EFAULT;
979 		goto free_newinfo;
980 	}
981 
982 	ret = translate_table(newinfo, loc_cpu_entry, &tmp);
983 	if (ret != 0)
984 		goto free_newinfo;
985 
986 	ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
987 			   tmp.num_counters, tmp.counters);
988 	if (ret)
989 		goto free_newinfo_untrans;
990 	return 0;
991 
992  free_newinfo_untrans:
993 	xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
994 		cleanup_entry(iter);
995  free_newinfo:
996 	xt_free_table_info(newinfo);
997 	return ret;
998 }
999 
1000 static int do_add_counters(struct net *net, const void __user *user,
1001 			   unsigned int len, int compat)
1002 {
1003 	unsigned int i;
1004 	struct xt_counters_info tmp;
1005 	struct xt_counters *paddc;
1006 	struct xt_table *t;
1007 	const struct xt_table_info *private;
1008 	int ret = 0;
1009 	struct arpt_entry *iter;
1010 	unsigned int addend;
1011 
1012 	paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1013 	if (IS_ERR(paddc))
1014 		return PTR_ERR(paddc);
1015 
1016 	t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1017 	if (!t) {
1018 		ret = -ENOENT;
1019 		goto free;
1020 	}
1021 
1022 	local_bh_disable();
1023 	private = t->private;
1024 	if (private->number != tmp.num_counters) {
1025 		ret = -EINVAL;
1026 		goto unlock_up_free;
1027 	}
1028 
1029 	i = 0;
1030 
1031 	addend = xt_write_recseq_begin();
1032 	xt_entry_foreach(iter,  private->entries, private->size) {
1033 		struct xt_counters *tmp;
1034 
1035 		tmp = xt_get_this_cpu_counter(&iter->counters);
1036 		ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1037 		++i;
1038 	}
1039 	xt_write_recseq_end(addend);
1040  unlock_up_free:
1041 	local_bh_enable();
1042 	xt_table_unlock(t);
1043 	module_put(t->me);
1044  free:
1045 	vfree(paddc);
1046 
1047 	return ret;
1048 }
1049 
1050 #ifdef CONFIG_COMPAT
1051 struct compat_arpt_replace {
1052 	char				name[XT_TABLE_MAXNAMELEN];
1053 	u32				valid_hooks;
1054 	u32				num_entries;
1055 	u32				size;
1056 	u32				hook_entry[NF_ARP_NUMHOOKS];
1057 	u32				underflow[NF_ARP_NUMHOOKS];
1058 	u32				num_counters;
1059 	compat_uptr_t			counters;
1060 	struct compat_arpt_entry	entries[0];
1061 };
1062 
1063 static inline void compat_release_entry(struct compat_arpt_entry *e)
1064 {
1065 	struct xt_entry_target *t;
1066 
1067 	t = compat_arpt_get_target(e);
1068 	module_put(t->u.kernel.target->me);
1069 }
1070 
1071 static int
1072 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1073 				  struct xt_table_info *newinfo,
1074 				  unsigned int *size,
1075 				  const unsigned char *base,
1076 				  const unsigned char *limit)
1077 {
1078 	struct xt_entry_target *t;
1079 	struct xt_target *target;
1080 	unsigned int entry_offset;
1081 	int ret, off;
1082 
1083 	if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1084 	    (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1085 	    (unsigned char *)e + e->next_offset > limit)
1086 		return -EINVAL;
1087 
1088 	if (e->next_offset < sizeof(struct compat_arpt_entry) +
1089 			     sizeof(struct compat_xt_entry_target))
1090 		return -EINVAL;
1091 
1092 	if (!arp_checkentry(&e->arp))
1093 		return -EINVAL;
1094 
1095 	ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1096 					    e->next_offset);
1097 	if (ret)
1098 		return ret;
1099 
1100 	off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1101 	entry_offset = (void *)e - (void *)base;
1102 
1103 	t = compat_arpt_get_target(e);
1104 	target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1105 					t->u.user.revision);
1106 	if (IS_ERR(target)) {
1107 		ret = PTR_ERR(target);
1108 		goto out;
1109 	}
1110 	t->u.kernel.target = target;
1111 
1112 	off += xt_compat_target_offset(target);
1113 	*size += off;
1114 	ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1115 	if (ret)
1116 		goto release_target;
1117 
1118 	return 0;
1119 
1120 release_target:
1121 	module_put(t->u.kernel.target->me);
1122 out:
1123 	return ret;
1124 }
1125 
1126 static void
1127 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1128 			    unsigned int *size,
1129 			    struct xt_table_info *newinfo, unsigned char *base)
1130 {
1131 	struct xt_entry_target *t;
1132 	struct xt_target *target;
1133 	struct arpt_entry *de;
1134 	unsigned int origsize;
1135 	int h;
1136 
1137 	origsize = *size;
1138 	de = (struct arpt_entry *)*dstptr;
1139 	memcpy(de, e, sizeof(struct arpt_entry));
1140 	memcpy(&de->counters, &e->counters, sizeof(e->counters));
1141 
1142 	*dstptr += sizeof(struct arpt_entry);
1143 	*size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1144 
1145 	de->target_offset = e->target_offset - (origsize - *size);
1146 	t = compat_arpt_get_target(e);
1147 	target = t->u.kernel.target;
1148 	xt_compat_target_from_user(t, dstptr, size);
1149 
1150 	de->next_offset = e->next_offset - (origsize - *size);
1151 	for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1152 		if ((unsigned char *)de - base < newinfo->hook_entry[h])
1153 			newinfo->hook_entry[h] -= origsize - *size;
1154 		if ((unsigned char *)de - base < newinfo->underflow[h])
1155 			newinfo->underflow[h] -= origsize - *size;
1156 	}
1157 }
1158 
1159 static int translate_compat_table(struct xt_table_info **pinfo,
1160 				  void **pentry0,
1161 				  const struct compat_arpt_replace *compatr)
1162 {
1163 	unsigned int i, j;
1164 	struct xt_table_info *newinfo, *info;
1165 	void *pos, *entry0, *entry1;
1166 	struct compat_arpt_entry *iter0;
1167 	struct arpt_replace repl;
1168 	unsigned int size;
1169 	int ret = 0;
1170 
1171 	info = *pinfo;
1172 	entry0 = *pentry0;
1173 	size = compatr->size;
1174 	info->number = compatr->num_entries;
1175 
1176 	j = 0;
1177 	xt_compat_lock(NFPROTO_ARP);
1178 	xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
1179 	/* Walk through entries, checking offsets. */
1180 	xt_entry_foreach(iter0, entry0, compatr->size) {
1181 		ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1182 							entry0,
1183 							entry0 + compatr->size);
1184 		if (ret != 0)
1185 			goto out_unlock;
1186 		++j;
1187 	}
1188 
1189 	ret = -EINVAL;
1190 	if (j != compatr->num_entries)
1191 		goto out_unlock;
1192 
1193 	ret = -ENOMEM;
1194 	newinfo = xt_alloc_table_info(size);
1195 	if (!newinfo)
1196 		goto out_unlock;
1197 
1198 	newinfo->number = compatr->num_entries;
1199 	for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1200 		newinfo->hook_entry[i] = compatr->hook_entry[i];
1201 		newinfo->underflow[i] = compatr->underflow[i];
1202 	}
1203 	entry1 = newinfo->entries;
1204 	pos = entry1;
1205 	size = compatr->size;
1206 	xt_entry_foreach(iter0, entry0, compatr->size)
1207 		compat_copy_entry_from_user(iter0, &pos, &size,
1208 					    newinfo, entry1);
1209 
1210 	/* all module references in entry0 are now gone */
1211 
1212 	xt_compat_flush_offsets(NFPROTO_ARP);
1213 	xt_compat_unlock(NFPROTO_ARP);
1214 
1215 	memcpy(&repl, compatr, sizeof(*compatr));
1216 
1217 	for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1218 		repl.hook_entry[i] = newinfo->hook_entry[i];
1219 		repl.underflow[i] = newinfo->underflow[i];
1220 	}
1221 
1222 	repl.num_counters = 0;
1223 	repl.counters = NULL;
1224 	repl.size = newinfo->size;
1225 	ret = translate_table(newinfo, entry1, &repl);
1226 	if (ret)
1227 		goto free_newinfo;
1228 
1229 	*pinfo = newinfo;
1230 	*pentry0 = entry1;
1231 	xt_free_table_info(info);
1232 	return 0;
1233 
1234 free_newinfo:
1235 	xt_free_table_info(newinfo);
1236 	return ret;
1237 out_unlock:
1238 	xt_compat_flush_offsets(NFPROTO_ARP);
1239 	xt_compat_unlock(NFPROTO_ARP);
1240 	xt_entry_foreach(iter0, entry0, compatr->size) {
1241 		if (j-- == 0)
1242 			break;
1243 		compat_release_entry(iter0);
1244 	}
1245 	return ret;
1246 }
1247 
1248 static int compat_do_replace(struct net *net, void __user *user,
1249 			     unsigned int len)
1250 {
1251 	int ret;
1252 	struct compat_arpt_replace tmp;
1253 	struct xt_table_info *newinfo;
1254 	void *loc_cpu_entry;
1255 	struct arpt_entry *iter;
1256 
1257 	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1258 		return -EFAULT;
1259 
1260 	/* overflow check */
1261 	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1262 		return -ENOMEM;
1263 	if (tmp.num_counters == 0)
1264 		return -EINVAL;
1265 
1266 	tmp.name[sizeof(tmp.name)-1] = 0;
1267 
1268 	newinfo = xt_alloc_table_info(tmp.size);
1269 	if (!newinfo)
1270 		return -ENOMEM;
1271 
1272 	loc_cpu_entry = newinfo->entries;
1273 	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1274 		ret = -EFAULT;
1275 		goto free_newinfo;
1276 	}
1277 
1278 	ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp);
1279 	if (ret != 0)
1280 		goto free_newinfo;
1281 
1282 	ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1283 			   tmp.num_counters, compat_ptr(tmp.counters));
1284 	if (ret)
1285 		goto free_newinfo_untrans;
1286 	return 0;
1287 
1288  free_newinfo_untrans:
1289 	xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1290 		cleanup_entry(iter);
1291  free_newinfo:
1292 	xt_free_table_info(newinfo);
1293 	return ret;
1294 }
1295 
1296 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1297 				  unsigned int len)
1298 {
1299 	int ret;
1300 
1301 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1302 		return -EPERM;
1303 
1304 	switch (cmd) {
1305 	case ARPT_SO_SET_REPLACE:
1306 		ret = compat_do_replace(sock_net(sk), user, len);
1307 		break;
1308 
1309 	case ARPT_SO_SET_ADD_COUNTERS:
1310 		ret = do_add_counters(sock_net(sk), user, len, 1);
1311 		break;
1312 
1313 	default:
1314 		ret = -EINVAL;
1315 	}
1316 
1317 	return ret;
1318 }
1319 
1320 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1321 				     compat_uint_t *size,
1322 				     struct xt_counters *counters,
1323 				     unsigned int i)
1324 {
1325 	struct xt_entry_target *t;
1326 	struct compat_arpt_entry __user *ce;
1327 	u_int16_t target_offset, next_offset;
1328 	compat_uint_t origsize;
1329 	int ret;
1330 
1331 	origsize = *size;
1332 	ce = (struct compat_arpt_entry __user *)*dstptr;
1333 	if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1334 	    copy_to_user(&ce->counters, &counters[i],
1335 	    sizeof(counters[i])) != 0)
1336 		return -EFAULT;
1337 
1338 	*dstptr += sizeof(struct compat_arpt_entry);
1339 	*size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1340 
1341 	target_offset = e->target_offset - (origsize - *size);
1342 
1343 	t = arpt_get_target(e);
1344 	ret = xt_compat_target_to_user(t, dstptr, size);
1345 	if (ret)
1346 		return ret;
1347 	next_offset = e->next_offset - (origsize - *size);
1348 	if (put_user(target_offset, &ce->target_offset) != 0 ||
1349 	    put_user(next_offset, &ce->next_offset) != 0)
1350 		return -EFAULT;
1351 	return 0;
1352 }
1353 
1354 static int compat_copy_entries_to_user(unsigned int total_size,
1355 				       struct xt_table *table,
1356 				       void __user *userptr)
1357 {
1358 	struct xt_counters *counters;
1359 	const struct xt_table_info *private = table->private;
1360 	void __user *pos;
1361 	unsigned int size;
1362 	int ret = 0;
1363 	unsigned int i = 0;
1364 	struct arpt_entry *iter;
1365 
1366 	counters = alloc_counters(table);
1367 	if (IS_ERR(counters))
1368 		return PTR_ERR(counters);
1369 
1370 	pos = userptr;
1371 	size = total_size;
1372 	xt_entry_foreach(iter, private->entries, total_size) {
1373 		ret = compat_copy_entry_to_user(iter, &pos,
1374 						&size, counters, i++);
1375 		if (ret != 0)
1376 			break;
1377 	}
1378 	vfree(counters);
1379 	return ret;
1380 }
1381 
1382 struct compat_arpt_get_entries {
1383 	char name[XT_TABLE_MAXNAMELEN];
1384 	compat_uint_t size;
1385 	struct compat_arpt_entry entrytable[0];
1386 };
1387 
1388 static int compat_get_entries(struct net *net,
1389 			      struct compat_arpt_get_entries __user *uptr,
1390 			      int *len)
1391 {
1392 	int ret;
1393 	struct compat_arpt_get_entries get;
1394 	struct xt_table *t;
1395 
1396 	if (*len < sizeof(get))
1397 		return -EINVAL;
1398 	if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1399 		return -EFAULT;
1400 	if (*len != sizeof(struct compat_arpt_get_entries) + get.size)
1401 		return -EINVAL;
1402 
1403 	get.name[sizeof(get.name) - 1] = '\0';
1404 
1405 	xt_compat_lock(NFPROTO_ARP);
1406 	t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1407 	if (t) {
1408 		const struct xt_table_info *private = t->private;
1409 		struct xt_table_info info;
1410 
1411 		ret = compat_table_info(private, &info);
1412 		if (!ret && get.size == info.size) {
1413 			ret = compat_copy_entries_to_user(private->size,
1414 							  t, uptr->entrytable);
1415 		} else if (!ret)
1416 			ret = -EAGAIN;
1417 
1418 		xt_compat_flush_offsets(NFPROTO_ARP);
1419 		module_put(t->me);
1420 		xt_table_unlock(t);
1421 	} else
1422 		ret = -ENOENT;
1423 
1424 	xt_compat_unlock(NFPROTO_ARP);
1425 	return ret;
1426 }
1427 
1428 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1429 
1430 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1431 				  int *len)
1432 {
1433 	int ret;
1434 
1435 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1436 		return -EPERM;
1437 
1438 	switch (cmd) {
1439 	case ARPT_SO_GET_INFO:
1440 		ret = get_info(sock_net(sk), user, len, 1);
1441 		break;
1442 	case ARPT_SO_GET_ENTRIES:
1443 		ret = compat_get_entries(sock_net(sk), user, len);
1444 		break;
1445 	default:
1446 		ret = do_arpt_get_ctl(sk, cmd, user, len);
1447 	}
1448 	return ret;
1449 }
1450 #endif
1451 
1452 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1453 {
1454 	int ret;
1455 
1456 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1457 		return -EPERM;
1458 
1459 	switch (cmd) {
1460 	case ARPT_SO_SET_REPLACE:
1461 		ret = do_replace(sock_net(sk), user, len);
1462 		break;
1463 
1464 	case ARPT_SO_SET_ADD_COUNTERS:
1465 		ret = do_add_counters(sock_net(sk), user, len, 0);
1466 		break;
1467 
1468 	default:
1469 		ret = -EINVAL;
1470 	}
1471 
1472 	return ret;
1473 }
1474 
1475 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1476 {
1477 	int ret;
1478 
1479 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1480 		return -EPERM;
1481 
1482 	switch (cmd) {
1483 	case ARPT_SO_GET_INFO:
1484 		ret = get_info(sock_net(sk), user, len, 0);
1485 		break;
1486 
1487 	case ARPT_SO_GET_ENTRIES:
1488 		ret = get_entries(sock_net(sk), user, len);
1489 		break;
1490 
1491 	case ARPT_SO_GET_REVISION_TARGET: {
1492 		struct xt_get_revision rev;
1493 
1494 		if (*len != sizeof(rev)) {
1495 			ret = -EINVAL;
1496 			break;
1497 		}
1498 		if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1499 			ret = -EFAULT;
1500 			break;
1501 		}
1502 		rev.name[sizeof(rev.name)-1] = 0;
1503 
1504 		try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1505 							 rev.revision, 1, &ret),
1506 					"arpt_%s", rev.name);
1507 		break;
1508 	}
1509 
1510 	default:
1511 		ret = -EINVAL;
1512 	}
1513 
1514 	return ret;
1515 }
1516 
1517 static void __arpt_unregister_table(struct xt_table *table)
1518 {
1519 	struct xt_table_info *private;
1520 	void *loc_cpu_entry;
1521 	struct module *table_owner = table->me;
1522 	struct arpt_entry *iter;
1523 
1524 	private = xt_unregister_table(table);
1525 
1526 	/* Decrease module usage counts and free resources */
1527 	loc_cpu_entry = private->entries;
1528 	xt_entry_foreach(iter, loc_cpu_entry, private->size)
1529 		cleanup_entry(iter);
1530 	if (private->number > private->initial_entries)
1531 		module_put(table_owner);
1532 	xt_free_table_info(private);
1533 }
1534 
1535 int arpt_register_table(struct net *net,
1536 			const struct xt_table *table,
1537 			const struct arpt_replace *repl,
1538 			const struct nf_hook_ops *ops,
1539 			struct xt_table **res)
1540 {
1541 	int ret;
1542 	struct xt_table_info *newinfo;
1543 	struct xt_table_info bootstrap = {0};
1544 	void *loc_cpu_entry;
1545 	struct xt_table *new_table;
1546 
1547 	newinfo = xt_alloc_table_info(repl->size);
1548 	if (!newinfo)
1549 		return -ENOMEM;
1550 
1551 	loc_cpu_entry = newinfo->entries;
1552 	memcpy(loc_cpu_entry, repl->entries, repl->size);
1553 
1554 	ret = translate_table(newinfo, loc_cpu_entry, repl);
1555 	if (ret != 0)
1556 		goto out_free;
1557 
1558 	new_table = xt_register_table(net, table, &bootstrap, newinfo);
1559 	if (IS_ERR(new_table)) {
1560 		ret = PTR_ERR(new_table);
1561 		goto out_free;
1562 	}
1563 
1564 	/* set res now, will see skbs right after nf_register_net_hooks */
1565 	WRITE_ONCE(*res, new_table);
1566 
1567 	ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1568 	if (ret != 0) {
1569 		__arpt_unregister_table(new_table);
1570 		*res = NULL;
1571 	}
1572 
1573 	return ret;
1574 
1575 out_free:
1576 	xt_free_table_info(newinfo);
1577 	return ret;
1578 }
1579 
1580 void arpt_unregister_table(struct net *net, struct xt_table *table,
1581 			   const struct nf_hook_ops *ops)
1582 {
1583 	nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1584 	__arpt_unregister_table(table);
1585 }
1586 
1587 /* The built-in targets: standard (NULL) and error. */
1588 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1589 	{
1590 		.name             = XT_STANDARD_TARGET,
1591 		.targetsize       = sizeof(int),
1592 		.family           = NFPROTO_ARP,
1593 #ifdef CONFIG_COMPAT
1594 		.compatsize       = sizeof(compat_int_t),
1595 		.compat_from_user = compat_standard_from_user,
1596 		.compat_to_user   = compat_standard_to_user,
1597 #endif
1598 	},
1599 	{
1600 		.name             = XT_ERROR_TARGET,
1601 		.target           = arpt_error,
1602 		.targetsize       = XT_FUNCTION_MAXNAMELEN,
1603 		.family           = NFPROTO_ARP,
1604 	},
1605 };
1606 
1607 static struct nf_sockopt_ops arpt_sockopts = {
1608 	.pf		= PF_INET,
1609 	.set_optmin	= ARPT_BASE_CTL,
1610 	.set_optmax	= ARPT_SO_SET_MAX+1,
1611 	.set		= do_arpt_set_ctl,
1612 #ifdef CONFIG_COMPAT
1613 	.compat_set	= compat_do_arpt_set_ctl,
1614 #endif
1615 	.get_optmin	= ARPT_BASE_CTL,
1616 	.get_optmax	= ARPT_SO_GET_MAX+1,
1617 	.get		= do_arpt_get_ctl,
1618 #ifdef CONFIG_COMPAT
1619 	.compat_get	= compat_do_arpt_get_ctl,
1620 #endif
1621 	.owner		= THIS_MODULE,
1622 };
1623 
1624 static int __net_init arp_tables_net_init(struct net *net)
1625 {
1626 	return xt_proto_init(net, NFPROTO_ARP);
1627 }
1628 
1629 static void __net_exit arp_tables_net_exit(struct net *net)
1630 {
1631 	xt_proto_fini(net, NFPROTO_ARP);
1632 }
1633 
1634 static struct pernet_operations arp_tables_net_ops = {
1635 	.init = arp_tables_net_init,
1636 	.exit = arp_tables_net_exit,
1637 };
1638 
1639 static int __init arp_tables_init(void)
1640 {
1641 	int ret;
1642 
1643 	ret = register_pernet_subsys(&arp_tables_net_ops);
1644 	if (ret < 0)
1645 		goto err1;
1646 
1647 	/* No one else will be downing sem now, so we won't sleep */
1648 	ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1649 	if (ret < 0)
1650 		goto err2;
1651 
1652 	/* Register setsockopt */
1653 	ret = nf_register_sockopt(&arpt_sockopts);
1654 	if (ret < 0)
1655 		goto err4;
1656 
1657 	pr_info("arp_tables: (C) 2002 David S. Miller\n");
1658 	return 0;
1659 
1660 err4:
1661 	xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1662 err2:
1663 	unregister_pernet_subsys(&arp_tables_net_ops);
1664 err1:
1665 	return ret;
1666 }
1667 
1668 static void __exit arp_tables_fini(void)
1669 {
1670 	nf_unregister_sockopt(&arpt_sockopts);
1671 	xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1672 	unregister_pernet_subsys(&arp_tables_net_ops);
1673 }
1674 
1675 EXPORT_SYMBOL(arpt_register_table);
1676 EXPORT_SYMBOL(arpt_unregister_table);
1677 EXPORT_SYMBOL(arpt_do_table);
1678 
1679 module_init(arp_tables_init);
1680 module_exit(arp_tables_fini);
1681