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