xref: /openbmc/linux/net/ipv6/netfilter/ip6_tables.c (revision 160b8e75)
1 /*
2  * Packet matching code.
3  *
4  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5  * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
6  * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/kernel.h>
16 #include <linux/capability.h>
17 #include <linux/in.h>
18 #include <linux/skbuff.h>
19 #include <linux/kmod.h>
20 #include <linux/vmalloc.h>
21 #include <linux/netdevice.h>
22 #include <linux/module.h>
23 #include <linux/poison.h>
24 #include <linux/icmpv6.h>
25 #include <net/ipv6.h>
26 #include <net/compat.h>
27 #include <linux/uaccess.h>
28 #include <linux/mutex.h>
29 #include <linux/proc_fs.h>
30 #include <linux/err.h>
31 #include <linux/cpumask.h>
32 
33 #include <linux/netfilter_ipv6/ip6_tables.h>
34 #include <linux/netfilter/x_tables.h>
35 #include <net/netfilter/nf_log.h>
36 #include "../../netfilter/xt_repldata.h"
37 
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
40 MODULE_DESCRIPTION("IPv6 packet filter");
41 
42 void *ip6t_alloc_initial_table(const struct xt_table *info)
43 {
44 	return xt_alloc_initial_table(ip6t, IP6T);
45 }
46 EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table);
47 
48 /* Returns whether matches rule or not. */
49 /* Performance critical - called for every packet */
50 static inline bool
51 ip6_packet_match(const struct sk_buff *skb,
52 		 const char *indev,
53 		 const char *outdev,
54 		 const struct ip6t_ip6 *ip6info,
55 		 unsigned int *protoff,
56 		 int *fragoff, bool *hotdrop)
57 {
58 	unsigned long ret;
59 	const struct ipv6hdr *ipv6 = ipv6_hdr(skb);
60 
61 	if (NF_INVF(ip6info, IP6T_INV_SRCIP,
62 		    ipv6_masked_addr_cmp(&ipv6->saddr, &ip6info->smsk,
63 					 &ip6info->src)) ||
64 	    NF_INVF(ip6info, IP6T_INV_DSTIP,
65 		    ipv6_masked_addr_cmp(&ipv6->daddr, &ip6info->dmsk,
66 					 &ip6info->dst)))
67 		return false;
68 
69 	ret = ifname_compare_aligned(indev, ip6info->iniface, ip6info->iniface_mask);
70 
71 	if (NF_INVF(ip6info, IP6T_INV_VIA_IN, ret != 0))
72 		return false;
73 
74 	ret = ifname_compare_aligned(outdev, ip6info->outiface, ip6info->outiface_mask);
75 
76 	if (NF_INVF(ip6info, IP6T_INV_VIA_OUT, ret != 0))
77 		return false;
78 
79 /* ... might want to do something with class and flowlabel here ... */
80 
81 	/* look for the desired protocol header */
82 	if (ip6info->flags & IP6T_F_PROTO) {
83 		int protohdr;
84 		unsigned short _frag_off;
85 
86 		protohdr = ipv6_find_hdr(skb, protoff, -1, &_frag_off, NULL);
87 		if (protohdr < 0) {
88 			if (_frag_off == 0)
89 				*hotdrop = true;
90 			return false;
91 		}
92 		*fragoff = _frag_off;
93 
94 		if (ip6info->proto == protohdr) {
95 			if (ip6info->invflags & IP6T_INV_PROTO)
96 				return false;
97 
98 			return true;
99 		}
100 
101 		/* We need match for the '-p all', too! */
102 		if ((ip6info->proto != 0) &&
103 			!(ip6info->invflags & IP6T_INV_PROTO))
104 			return false;
105 	}
106 	return true;
107 }
108 
109 /* should be ip6 safe */
110 static bool
111 ip6_checkentry(const struct ip6t_ip6 *ipv6)
112 {
113 	if (ipv6->flags & ~IP6T_F_MASK)
114 		return false;
115 	if (ipv6->invflags & ~IP6T_INV_MASK)
116 		return false;
117 
118 	return true;
119 }
120 
121 static unsigned int
122 ip6t_error(struct sk_buff *skb, const struct xt_action_param *par)
123 {
124 	net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
125 
126 	return NF_DROP;
127 }
128 
129 static inline struct ip6t_entry *
130 get_entry(const void *base, unsigned int offset)
131 {
132 	return (struct ip6t_entry *)(base + offset);
133 }
134 
135 /* All zeroes == unconditional rule. */
136 /* Mildly perf critical (only if packet tracing is on) */
137 static inline bool unconditional(const struct ip6t_entry *e)
138 {
139 	static const struct ip6t_ip6 uncond;
140 
141 	return e->target_offset == sizeof(struct ip6t_entry) &&
142 	       memcmp(&e->ipv6, &uncond, sizeof(uncond)) == 0;
143 }
144 
145 static inline const struct xt_entry_target *
146 ip6t_get_target_c(const struct ip6t_entry *e)
147 {
148 	return ip6t_get_target((struct ip6t_entry *)e);
149 }
150 
151 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
152 /* This cries for unification! */
153 static const char *const hooknames[] = {
154 	[NF_INET_PRE_ROUTING]		= "PREROUTING",
155 	[NF_INET_LOCAL_IN]		= "INPUT",
156 	[NF_INET_FORWARD]		= "FORWARD",
157 	[NF_INET_LOCAL_OUT]		= "OUTPUT",
158 	[NF_INET_POST_ROUTING]		= "POSTROUTING",
159 };
160 
161 enum nf_ip_trace_comments {
162 	NF_IP6_TRACE_COMMENT_RULE,
163 	NF_IP6_TRACE_COMMENT_RETURN,
164 	NF_IP6_TRACE_COMMENT_POLICY,
165 };
166 
167 static const char *const comments[] = {
168 	[NF_IP6_TRACE_COMMENT_RULE]	= "rule",
169 	[NF_IP6_TRACE_COMMENT_RETURN]	= "return",
170 	[NF_IP6_TRACE_COMMENT_POLICY]	= "policy",
171 };
172 
173 static const struct nf_loginfo trace_loginfo = {
174 	.type = NF_LOG_TYPE_LOG,
175 	.u = {
176 		.log = {
177 			.level = LOGLEVEL_WARNING,
178 			.logflags = NF_LOG_DEFAULT_MASK,
179 		},
180 	},
181 };
182 
183 /* Mildly perf critical (only if packet tracing is on) */
184 static inline int
185 get_chainname_rulenum(const struct ip6t_entry *s, const struct ip6t_entry *e,
186 		      const char *hookname, const char **chainname,
187 		      const char **comment, unsigned int *rulenum)
188 {
189 	const struct xt_standard_target *t = (void *)ip6t_get_target_c(s);
190 
191 	if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
192 		/* Head of user chain: ERROR target with chainname */
193 		*chainname = t->target.data;
194 		(*rulenum) = 0;
195 	} else if (s == e) {
196 		(*rulenum)++;
197 
198 		if (unconditional(s) &&
199 		    strcmp(t->target.u.kernel.target->name,
200 			   XT_STANDARD_TARGET) == 0 &&
201 		    t->verdict < 0) {
202 			/* Tail of chains: STANDARD target (return/policy) */
203 			*comment = *chainname == hookname
204 				? comments[NF_IP6_TRACE_COMMENT_POLICY]
205 				: comments[NF_IP6_TRACE_COMMENT_RETURN];
206 		}
207 		return 1;
208 	} else
209 		(*rulenum)++;
210 
211 	return 0;
212 }
213 
214 static void trace_packet(struct net *net,
215 			 const struct sk_buff *skb,
216 			 unsigned int hook,
217 			 const struct net_device *in,
218 			 const struct net_device *out,
219 			 const char *tablename,
220 			 const struct xt_table_info *private,
221 			 const struct ip6t_entry *e)
222 {
223 	const struct ip6t_entry *root;
224 	const char *hookname, *chainname, *comment;
225 	const struct ip6t_entry *iter;
226 	unsigned int rulenum = 0;
227 
228 	root = get_entry(private->entries, private->hook_entry[hook]);
229 
230 	hookname = chainname = hooknames[hook];
231 	comment = comments[NF_IP6_TRACE_COMMENT_RULE];
232 
233 	xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
234 		if (get_chainname_rulenum(iter, e, hookname,
235 		    &chainname, &comment, &rulenum) != 0)
236 			break;
237 
238 	nf_log_trace(net, AF_INET6, hook, skb, in, out, &trace_loginfo,
239 		     "TRACE: %s:%s:%s:%u ",
240 		     tablename, chainname, comment, rulenum);
241 }
242 #endif
243 
244 static inline struct ip6t_entry *
245 ip6t_next_entry(const struct ip6t_entry *entry)
246 {
247 	return (void *)entry + entry->next_offset;
248 }
249 
250 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
251 unsigned int
252 ip6t_do_table(struct sk_buff *skb,
253 	      const struct nf_hook_state *state,
254 	      struct xt_table *table)
255 {
256 	unsigned int hook = state->hook;
257 	static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
258 	/* Initializing verdict to NF_DROP keeps gcc happy. */
259 	unsigned int verdict = NF_DROP;
260 	const char *indev, *outdev;
261 	const void *table_base;
262 	struct ip6t_entry *e, **jumpstack;
263 	unsigned int stackidx, cpu;
264 	const struct xt_table_info *private;
265 	struct xt_action_param acpar;
266 	unsigned int addend;
267 
268 	/* Initialization */
269 	stackidx = 0;
270 	indev = state->in ? state->in->name : nulldevname;
271 	outdev = state->out ? state->out->name : nulldevname;
272 	/* We handle fragments by dealing with the first fragment as
273 	 * if it was a normal packet.  All other fragments are treated
274 	 * normally, except that they will NEVER match rules that ask
275 	 * things we don't know, ie. tcp syn flag or ports).  If the
276 	 * rule is also a fragment-specific rule, non-fragments won't
277 	 * match it. */
278 	acpar.hotdrop = false;
279 	acpar.state   = state;
280 
281 	WARN_ON(!(table->valid_hooks & (1 << hook)));
282 
283 	local_bh_disable();
284 	addend = xt_write_recseq_begin();
285 	private = READ_ONCE(table->private); /* Address dependency. */
286 	cpu        = smp_processor_id();
287 	table_base = private->entries;
288 	jumpstack  = (struct ip6t_entry **)private->jumpstack[cpu];
289 
290 	/* Switch to alternate jumpstack if we're being invoked via TEE.
291 	 * TEE issues XT_CONTINUE verdict on original skb so we must not
292 	 * clobber the jumpstack.
293 	 *
294 	 * For recursion via REJECT or SYNPROXY the stack will be clobbered
295 	 * but it is no problem since absolute verdict is issued by these.
296 	 */
297 	if (static_key_false(&xt_tee_enabled))
298 		jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
299 
300 	e = get_entry(table_base, private->hook_entry[hook]);
301 
302 	do {
303 		const struct xt_entry_target *t;
304 		const struct xt_entry_match *ematch;
305 		struct xt_counters *counter;
306 
307 		WARN_ON(!e);
308 		acpar.thoff = 0;
309 		if (!ip6_packet_match(skb, indev, outdev, &e->ipv6,
310 		    &acpar.thoff, &acpar.fragoff, &acpar.hotdrop)) {
311  no_match:
312 			e = ip6t_next_entry(e);
313 			continue;
314 		}
315 
316 		xt_ematch_foreach(ematch, e) {
317 			acpar.match     = ematch->u.kernel.match;
318 			acpar.matchinfo = ematch->data;
319 			if (!acpar.match->match(skb, &acpar))
320 				goto no_match;
321 		}
322 
323 		counter = xt_get_this_cpu_counter(&e->counters);
324 		ADD_COUNTER(*counter, skb->len, 1);
325 
326 		t = ip6t_get_target_c(e);
327 		WARN_ON(!t->u.kernel.target);
328 
329 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
330 		/* The packet is traced: log it */
331 		if (unlikely(skb->nf_trace))
332 			trace_packet(state->net, skb, hook, state->in,
333 				     state->out, table->name, private, e);
334 #endif
335 		/* Standard target? */
336 		if (!t->u.kernel.target->target) {
337 			int v;
338 
339 			v = ((struct xt_standard_target *)t)->verdict;
340 			if (v < 0) {
341 				/* Pop from stack? */
342 				if (v != XT_RETURN) {
343 					verdict = (unsigned int)(-v) - 1;
344 					break;
345 				}
346 				if (stackidx == 0)
347 					e = get_entry(table_base,
348 					    private->underflow[hook]);
349 				else
350 					e = ip6t_next_entry(jumpstack[--stackidx]);
351 				continue;
352 			}
353 			if (table_base + v != ip6t_next_entry(e) &&
354 			    !(e->ipv6.flags & IP6T_F_GOTO)) {
355 				jumpstack[stackidx++] = e;
356 			}
357 
358 			e = get_entry(table_base, v);
359 			continue;
360 		}
361 
362 		acpar.target   = t->u.kernel.target;
363 		acpar.targinfo = t->data;
364 
365 		verdict = t->u.kernel.target->target(skb, &acpar);
366 		if (verdict == XT_CONTINUE)
367 			e = ip6t_next_entry(e);
368 		else
369 			/* Verdict */
370 			break;
371 	} while (!acpar.hotdrop);
372 
373 	xt_write_recseq_end(addend);
374 	local_bh_enable();
375 
376 	if (acpar.hotdrop)
377 		return NF_DROP;
378 	else return verdict;
379 }
380 
381 /* Figures out from what hook each rule can be called: returns 0 if
382    there are loops.  Puts hook bitmask in comefrom. */
383 static int
384 mark_source_chains(const struct xt_table_info *newinfo,
385 		   unsigned int valid_hooks, void *entry0,
386 		   unsigned int *offsets)
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 	for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
393 		unsigned int pos = newinfo->hook_entry[hook];
394 		struct ip6t_entry *e = entry0 + pos;
395 
396 		if (!(valid_hooks & (1 << hook)))
397 			continue;
398 
399 		/* Set initial back pointer. */
400 		e->counters.pcnt = pos;
401 
402 		for (;;) {
403 			const struct xt_standard_target *t
404 				= (void *)ip6t_get_target_c(e);
405 			int visited = e->comefrom & (1 << hook);
406 
407 			if (e->comefrom & (1 << NF_INET_NUMHOOKS))
408 				return 0;
409 
410 			e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
411 
412 			/* Unconditional return/END. */
413 			if ((unconditional(e) &&
414 			     (strcmp(t->target.u.user.name,
415 				     XT_STANDARD_TARGET) == 0) &&
416 			     t->verdict < 0) || visited) {
417 				unsigned int oldpos, size;
418 
419 				if ((strcmp(t->target.u.user.name,
420 					    XT_STANDARD_TARGET) == 0) &&
421 				    t->verdict < -NF_MAX_VERDICT - 1)
422 					return 0;
423 
424 				/* Return: backtrack through the last
425 				   big jump. */
426 				do {
427 					e->comefrom ^= (1<<NF_INET_NUMHOOKS);
428 					oldpos = pos;
429 					pos = e->counters.pcnt;
430 					e->counters.pcnt = 0;
431 
432 					/* We're at the start. */
433 					if (pos == oldpos)
434 						goto next;
435 
436 					e = entry0 + pos;
437 				} while (oldpos == pos + e->next_offset);
438 
439 				/* Move along one */
440 				size = e->next_offset;
441 				e = entry0 + pos + size;
442 				if (pos + size >= newinfo->size)
443 					return 0;
444 				e->counters.pcnt = pos;
445 				pos += size;
446 			} else {
447 				int newpos = t->verdict;
448 
449 				if (strcmp(t->target.u.user.name,
450 					   XT_STANDARD_TARGET) == 0 &&
451 				    newpos >= 0) {
452 					/* This a jump; chase it. */
453 					if (!xt_find_jump_offset(offsets, newpos,
454 								 newinfo->number))
455 						return 0;
456 				} else {
457 					/* ... this is a fallthru */
458 					newpos = pos + e->next_offset;
459 					if (newpos >= newinfo->size)
460 						return 0;
461 				}
462 				e = entry0 + newpos;
463 				e->counters.pcnt = pos;
464 				pos = newpos;
465 			}
466 		}
467 next:		;
468 	}
469 	return 1;
470 }
471 
472 static void cleanup_match(struct xt_entry_match *m, struct net *net)
473 {
474 	struct xt_mtdtor_param par;
475 
476 	par.net       = net;
477 	par.match     = m->u.kernel.match;
478 	par.matchinfo = m->data;
479 	par.family    = NFPROTO_IPV6;
480 	if (par.match->destroy != NULL)
481 		par.match->destroy(&par);
482 	module_put(par.match->me);
483 }
484 
485 static int check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
486 {
487 	const struct ip6t_ip6 *ipv6 = par->entryinfo;
488 
489 	par->match     = m->u.kernel.match;
490 	par->matchinfo = m->data;
491 
492 	return xt_check_match(par, m->u.match_size - sizeof(*m),
493 			      ipv6->proto, ipv6->invflags & IP6T_INV_PROTO);
494 }
495 
496 static int
497 find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
498 {
499 	struct xt_match *match;
500 	int ret;
501 
502 	match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
503 				      m->u.user.revision);
504 	if (IS_ERR(match))
505 		return PTR_ERR(match);
506 
507 	m->u.kernel.match = match;
508 
509 	ret = check_match(m, par);
510 	if (ret)
511 		goto err;
512 
513 	return 0;
514 err:
515 	module_put(m->u.kernel.match->me);
516 	return ret;
517 }
518 
519 static int check_target(struct ip6t_entry *e, struct net *net, const char *name)
520 {
521 	struct xt_entry_target *t = ip6t_get_target(e);
522 	struct xt_tgchk_param par = {
523 		.net       = net,
524 		.table     = name,
525 		.entryinfo = e,
526 		.target    = t->u.kernel.target,
527 		.targinfo  = t->data,
528 		.hook_mask = e->comefrom,
529 		.family    = NFPROTO_IPV6,
530 	};
531 
532 	t = ip6t_get_target(e);
533 	return xt_check_target(&par, t->u.target_size - sizeof(*t),
534 			       e->ipv6.proto,
535 			       e->ipv6.invflags & IP6T_INV_PROTO);
536 }
537 
538 static int
539 find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
540 		 unsigned int size,
541 		 struct xt_percpu_counter_alloc_state *alloc_state)
542 {
543 	struct xt_entry_target *t;
544 	struct xt_target *target;
545 	int ret;
546 	unsigned int j;
547 	struct xt_mtchk_param mtpar;
548 	struct xt_entry_match *ematch;
549 
550 	if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
551 		return -ENOMEM;
552 
553 	j = 0;
554 	mtpar.net	= net;
555 	mtpar.table     = name;
556 	mtpar.entryinfo = &e->ipv6;
557 	mtpar.hook_mask = e->comefrom;
558 	mtpar.family    = NFPROTO_IPV6;
559 	xt_ematch_foreach(ematch, e) {
560 		ret = find_check_match(ematch, &mtpar);
561 		if (ret != 0)
562 			goto cleanup_matches;
563 		++j;
564 	}
565 
566 	t = ip6t_get_target(e);
567 	target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
568 					t->u.user.revision);
569 	if (IS_ERR(target)) {
570 		ret = PTR_ERR(target);
571 		goto cleanup_matches;
572 	}
573 	t->u.kernel.target = target;
574 
575 	ret = check_target(e, net, name);
576 	if (ret)
577 		goto err;
578 	return 0;
579  err:
580 	module_put(t->u.kernel.target->me);
581  cleanup_matches:
582 	xt_ematch_foreach(ematch, e) {
583 		if (j-- == 0)
584 			break;
585 		cleanup_match(ematch, net);
586 	}
587 
588 	xt_percpu_counter_free(&e->counters);
589 
590 	return ret;
591 }
592 
593 static bool check_underflow(const struct ip6t_entry *e)
594 {
595 	const struct xt_entry_target *t;
596 	unsigned int verdict;
597 
598 	if (!unconditional(e))
599 		return false;
600 	t = ip6t_get_target_c(e);
601 	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
602 		return false;
603 	verdict = ((struct xt_standard_target *)t)->verdict;
604 	verdict = -verdict - 1;
605 	return verdict == NF_DROP || verdict == NF_ACCEPT;
606 }
607 
608 static int
609 check_entry_size_and_hooks(struct ip6t_entry *e,
610 			   struct xt_table_info *newinfo,
611 			   const unsigned char *base,
612 			   const unsigned char *limit,
613 			   const unsigned int *hook_entries,
614 			   const unsigned int *underflows,
615 			   unsigned int valid_hooks)
616 {
617 	unsigned int h;
618 	int err;
619 
620 	if ((unsigned long)e % __alignof__(struct ip6t_entry) != 0 ||
621 	    (unsigned char *)e + sizeof(struct ip6t_entry) >= limit ||
622 	    (unsigned char *)e + e->next_offset > limit)
623 		return -EINVAL;
624 
625 	if (e->next_offset
626 	    < sizeof(struct ip6t_entry) + sizeof(struct xt_entry_target))
627 		return -EINVAL;
628 
629 	if (!ip6_checkentry(&e->ipv6))
630 		return -EINVAL;
631 
632 	err = xt_check_entry_offsets(e, e->elems, e->target_offset,
633 				     e->next_offset);
634 	if (err)
635 		return err;
636 
637 	/* Check hooks & underflows */
638 	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
639 		if (!(valid_hooks & (1 << h)))
640 			continue;
641 		if ((unsigned char *)e - base == hook_entries[h])
642 			newinfo->hook_entry[h] = hook_entries[h];
643 		if ((unsigned char *)e - base == underflows[h]) {
644 			if (!check_underflow(e))
645 				return -EINVAL;
646 
647 			newinfo->underflow[h] = underflows[h];
648 		}
649 	}
650 
651 	/* Clear counters and comefrom */
652 	e->counters = ((struct xt_counters) { 0, 0 });
653 	e->comefrom = 0;
654 	return 0;
655 }
656 
657 static void cleanup_entry(struct ip6t_entry *e, struct net *net)
658 {
659 	struct xt_tgdtor_param par;
660 	struct xt_entry_target *t;
661 	struct xt_entry_match *ematch;
662 
663 	/* Cleanup all matches */
664 	xt_ematch_foreach(ematch, e)
665 		cleanup_match(ematch, net);
666 	t = ip6t_get_target(e);
667 
668 	par.net      = net;
669 	par.target   = t->u.kernel.target;
670 	par.targinfo = t->data;
671 	par.family   = NFPROTO_IPV6;
672 	if (par.target->destroy != NULL)
673 		par.target->destroy(&par);
674 	module_put(par.target->me);
675 	xt_percpu_counter_free(&e->counters);
676 }
677 
678 /* Checks and translates the user-supplied table segment (held in
679    newinfo) */
680 static int
681 translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
682 		const struct ip6t_replace *repl)
683 {
684 	struct xt_percpu_counter_alloc_state alloc_state = { 0 };
685 	struct ip6t_entry *iter;
686 	unsigned int *offsets;
687 	unsigned int i;
688 	int ret = 0;
689 
690 	newinfo->size = repl->size;
691 	newinfo->number = repl->num_entries;
692 
693 	/* Init all hooks to impossible value. */
694 	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
695 		newinfo->hook_entry[i] = 0xFFFFFFFF;
696 		newinfo->underflow[i] = 0xFFFFFFFF;
697 	}
698 
699 	offsets = xt_alloc_entry_offsets(newinfo->number);
700 	if (!offsets)
701 		return -ENOMEM;
702 	i = 0;
703 	/* Walk through entries, checking offsets. */
704 	xt_entry_foreach(iter, entry0, newinfo->size) {
705 		ret = check_entry_size_and_hooks(iter, newinfo, entry0,
706 						 entry0 + repl->size,
707 						 repl->hook_entry,
708 						 repl->underflow,
709 						 repl->valid_hooks);
710 		if (ret != 0)
711 			goto out_free;
712 		if (i < repl->num_entries)
713 			offsets[i] = (void *)iter - entry0;
714 		++i;
715 		if (strcmp(ip6t_get_target(iter)->u.user.name,
716 		    XT_ERROR_TARGET) == 0)
717 			++newinfo->stacksize;
718 	}
719 
720 	ret = -EINVAL;
721 	if (i != repl->num_entries)
722 		goto out_free;
723 
724 	/* Check hooks all assigned */
725 	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
726 		/* Only hooks which are valid */
727 		if (!(repl->valid_hooks & (1 << i)))
728 			continue;
729 		if (newinfo->hook_entry[i] == 0xFFFFFFFF)
730 			goto out_free;
731 		if (newinfo->underflow[i] == 0xFFFFFFFF)
732 			goto out_free;
733 	}
734 
735 	if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
736 		ret = -ELOOP;
737 		goto out_free;
738 	}
739 	kvfree(offsets);
740 
741 	/* Finally, each sanity check must pass */
742 	i = 0;
743 	xt_entry_foreach(iter, entry0, newinfo->size) {
744 		ret = find_check_entry(iter, net, repl->name, repl->size,
745 				       &alloc_state);
746 		if (ret != 0)
747 			break;
748 		++i;
749 	}
750 
751 	if (ret != 0) {
752 		xt_entry_foreach(iter, entry0, newinfo->size) {
753 			if (i-- == 0)
754 				break;
755 			cleanup_entry(iter, net);
756 		}
757 		return ret;
758 	}
759 
760 	return ret;
761  out_free:
762 	kvfree(offsets);
763 	return ret;
764 }
765 
766 static void
767 get_counters(const struct xt_table_info *t,
768 	     struct xt_counters counters[])
769 {
770 	struct ip6t_entry *iter;
771 	unsigned int cpu;
772 	unsigned int i;
773 
774 	for_each_possible_cpu(cpu) {
775 		seqcount_t *s = &per_cpu(xt_recseq, cpu);
776 
777 		i = 0;
778 		xt_entry_foreach(iter, t->entries, t->size) {
779 			struct xt_counters *tmp;
780 			u64 bcnt, pcnt;
781 			unsigned int start;
782 
783 			tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
784 			do {
785 				start = read_seqcount_begin(s);
786 				bcnt = tmp->bcnt;
787 				pcnt = tmp->pcnt;
788 			} while (read_seqcount_retry(s, start));
789 
790 			ADD_COUNTER(counters[i], bcnt, pcnt);
791 			++i;
792 			cond_resched();
793 		}
794 	}
795 }
796 
797 static void get_old_counters(const struct xt_table_info *t,
798 			     struct xt_counters counters[])
799 {
800 	struct ip6t_entry *iter;
801 	unsigned int cpu, i;
802 
803 	for_each_possible_cpu(cpu) {
804 		i = 0;
805 		xt_entry_foreach(iter, t->entries, t->size) {
806 			const struct xt_counters *tmp;
807 
808 			tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
809 			ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
810 			++i;
811 		}
812 		cond_resched();
813 	}
814 }
815 
816 static struct xt_counters *alloc_counters(const struct xt_table *table)
817 {
818 	unsigned int countersize;
819 	struct xt_counters *counters;
820 	const struct xt_table_info *private = table->private;
821 
822 	/* We need atomic snapshot of counters: rest doesn't change
823 	   (other than comefrom, which userspace doesn't care
824 	   about). */
825 	countersize = sizeof(struct xt_counters) * private->number;
826 	counters = vzalloc(countersize);
827 
828 	if (counters == NULL)
829 		return ERR_PTR(-ENOMEM);
830 
831 	get_counters(private, counters);
832 
833 	return counters;
834 }
835 
836 static int
837 copy_entries_to_user(unsigned int total_size,
838 		     const struct xt_table *table,
839 		     void __user *userptr)
840 {
841 	unsigned int off, num;
842 	const struct ip6t_entry *e;
843 	struct xt_counters *counters;
844 	const struct xt_table_info *private = table->private;
845 	int ret = 0;
846 	const void *loc_cpu_entry;
847 
848 	counters = alloc_counters(table);
849 	if (IS_ERR(counters))
850 		return PTR_ERR(counters);
851 
852 	loc_cpu_entry = private->entries;
853 
854 	/* FIXME: use iterator macros --RR */
855 	/* ... then go back and fix counters and names */
856 	for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
857 		unsigned int i;
858 		const struct xt_entry_match *m;
859 		const struct xt_entry_target *t;
860 
861 		e = loc_cpu_entry + off;
862 		if (copy_to_user(userptr + off, e, sizeof(*e))) {
863 			ret = -EFAULT;
864 			goto free_counters;
865 		}
866 		if (copy_to_user(userptr + off
867 				 + offsetof(struct ip6t_entry, counters),
868 				 &counters[num],
869 				 sizeof(counters[num])) != 0) {
870 			ret = -EFAULT;
871 			goto free_counters;
872 		}
873 
874 		for (i = sizeof(struct ip6t_entry);
875 		     i < e->target_offset;
876 		     i += m->u.match_size) {
877 			m = (void *)e + i;
878 
879 			if (xt_match_to_user(m, userptr + off + i)) {
880 				ret = -EFAULT;
881 				goto free_counters;
882 			}
883 		}
884 
885 		t = ip6t_get_target_c(e);
886 		if (xt_target_to_user(t, userptr + off + e->target_offset)) {
887 			ret = -EFAULT;
888 			goto free_counters;
889 		}
890 	}
891 
892  free_counters:
893 	vfree(counters);
894 	return ret;
895 }
896 
897 #ifdef CONFIG_COMPAT
898 static void compat_standard_from_user(void *dst, const void *src)
899 {
900 	int v = *(compat_int_t *)src;
901 
902 	if (v > 0)
903 		v += xt_compat_calc_jump(AF_INET6, v);
904 	memcpy(dst, &v, sizeof(v));
905 }
906 
907 static int compat_standard_to_user(void __user *dst, const void *src)
908 {
909 	compat_int_t cv = *(int *)src;
910 
911 	if (cv > 0)
912 		cv -= xt_compat_calc_jump(AF_INET6, cv);
913 	return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
914 }
915 
916 static int compat_calc_entry(const struct ip6t_entry *e,
917 			     const struct xt_table_info *info,
918 			     const void *base, struct xt_table_info *newinfo)
919 {
920 	const struct xt_entry_match *ematch;
921 	const struct xt_entry_target *t;
922 	unsigned int entry_offset;
923 	int off, i, ret;
924 
925 	off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
926 	entry_offset = (void *)e - base;
927 	xt_ematch_foreach(ematch, e)
928 		off += xt_compat_match_offset(ematch->u.kernel.match);
929 	t = ip6t_get_target_c(e);
930 	off += xt_compat_target_offset(t->u.kernel.target);
931 	newinfo->size -= off;
932 	ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
933 	if (ret)
934 		return ret;
935 
936 	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
937 		if (info->hook_entry[i] &&
938 		    (e < (struct ip6t_entry *)(base + info->hook_entry[i])))
939 			newinfo->hook_entry[i] -= off;
940 		if (info->underflow[i] &&
941 		    (e < (struct ip6t_entry *)(base + info->underflow[i])))
942 			newinfo->underflow[i] -= off;
943 	}
944 	return 0;
945 }
946 
947 static int compat_table_info(const struct xt_table_info *info,
948 			     struct xt_table_info *newinfo)
949 {
950 	struct ip6t_entry *iter;
951 	const void *loc_cpu_entry;
952 	int ret;
953 
954 	if (!newinfo || !info)
955 		return -EINVAL;
956 
957 	/* we dont care about newinfo->entries */
958 	memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
959 	newinfo->initial_entries = 0;
960 	loc_cpu_entry = info->entries;
961 	xt_compat_init_offsets(AF_INET6, info->number);
962 	xt_entry_foreach(iter, loc_cpu_entry, info->size) {
963 		ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
964 		if (ret != 0)
965 			return ret;
966 	}
967 	return 0;
968 }
969 #endif
970 
971 static int get_info(struct net *net, void __user *user,
972 		    const int *len, int compat)
973 {
974 	char name[XT_TABLE_MAXNAMELEN];
975 	struct xt_table *t;
976 	int ret;
977 
978 	if (*len != sizeof(struct ip6t_getinfo))
979 		return -EINVAL;
980 
981 	if (copy_from_user(name, user, sizeof(name)) != 0)
982 		return -EFAULT;
983 
984 	name[XT_TABLE_MAXNAMELEN-1] = '\0';
985 #ifdef CONFIG_COMPAT
986 	if (compat)
987 		xt_compat_lock(AF_INET6);
988 #endif
989 	t = xt_request_find_table_lock(net, AF_INET6, name);
990 	if (!IS_ERR(t)) {
991 		struct ip6t_getinfo info;
992 		const struct xt_table_info *private = t->private;
993 #ifdef CONFIG_COMPAT
994 		struct xt_table_info tmp;
995 
996 		if (compat) {
997 			ret = compat_table_info(private, &tmp);
998 			xt_compat_flush_offsets(AF_INET6);
999 			private = &tmp;
1000 		}
1001 #endif
1002 		memset(&info, 0, sizeof(info));
1003 		info.valid_hooks = t->valid_hooks;
1004 		memcpy(info.hook_entry, private->hook_entry,
1005 		       sizeof(info.hook_entry));
1006 		memcpy(info.underflow, private->underflow,
1007 		       sizeof(info.underflow));
1008 		info.num_entries = private->number;
1009 		info.size = private->size;
1010 		strcpy(info.name, name);
1011 
1012 		if (copy_to_user(user, &info, *len) != 0)
1013 			ret = -EFAULT;
1014 		else
1015 			ret = 0;
1016 
1017 		xt_table_unlock(t);
1018 		module_put(t->me);
1019 	} else
1020 		ret = PTR_ERR(t);
1021 #ifdef CONFIG_COMPAT
1022 	if (compat)
1023 		xt_compat_unlock(AF_INET6);
1024 #endif
1025 	return ret;
1026 }
1027 
1028 static int
1029 get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
1030 	    const int *len)
1031 {
1032 	int ret;
1033 	struct ip6t_get_entries get;
1034 	struct xt_table *t;
1035 
1036 	if (*len < sizeof(get))
1037 		return -EINVAL;
1038 	if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1039 		return -EFAULT;
1040 	if (*len != sizeof(struct ip6t_get_entries) + get.size)
1041 		return -EINVAL;
1042 
1043 	get.name[sizeof(get.name) - 1] = '\0';
1044 
1045 	t = xt_find_table_lock(net, AF_INET6, get.name);
1046 	if (!IS_ERR(t)) {
1047 		struct xt_table_info *private = t->private;
1048 		if (get.size == private->size)
1049 			ret = copy_entries_to_user(private->size,
1050 						   t, uptr->entrytable);
1051 		else
1052 			ret = -EAGAIN;
1053 
1054 		module_put(t->me);
1055 		xt_table_unlock(t);
1056 	} else
1057 		ret = PTR_ERR(t);
1058 
1059 	return ret;
1060 }
1061 
1062 static int
1063 __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1064 	     struct xt_table_info *newinfo, unsigned int num_counters,
1065 	     void __user *counters_ptr)
1066 {
1067 	int ret;
1068 	struct xt_table *t;
1069 	struct xt_table_info *oldinfo;
1070 	struct xt_counters *counters;
1071 	struct ip6t_entry *iter;
1072 
1073 	ret = 0;
1074 	counters = vzalloc(num_counters * sizeof(struct xt_counters));
1075 	if (!counters) {
1076 		ret = -ENOMEM;
1077 		goto out;
1078 	}
1079 
1080 	t = xt_request_find_table_lock(net, AF_INET6, name);
1081 	if (IS_ERR(t)) {
1082 		ret = PTR_ERR(t);
1083 		goto free_newinfo_counters_untrans;
1084 	}
1085 
1086 	/* You lied! */
1087 	if (valid_hooks != t->valid_hooks) {
1088 		ret = -EINVAL;
1089 		goto put_module;
1090 	}
1091 
1092 	oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1093 	if (!oldinfo)
1094 		goto put_module;
1095 
1096 	/* Update module usage count based on number of rules */
1097 	if ((oldinfo->number > oldinfo->initial_entries) ||
1098 	    (newinfo->number <= oldinfo->initial_entries))
1099 		module_put(t->me);
1100 	if ((oldinfo->number > oldinfo->initial_entries) &&
1101 	    (newinfo->number <= oldinfo->initial_entries))
1102 		module_put(t->me);
1103 
1104 	get_old_counters(oldinfo, counters);
1105 
1106 	/* Decrease module usage counts and free resource */
1107 	xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1108 		cleanup_entry(iter, net);
1109 
1110 	xt_free_table_info(oldinfo);
1111 	if (copy_to_user(counters_ptr, counters,
1112 			 sizeof(struct xt_counters) * num_counters) != 0) {
1113 		/* Silent error, can't fail, new table is already in place */
1114 		net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1115 	}
1116 	vfree(counters);
1117 	xt_table_unlock(t);
1118 	return ret;
1119 
1120  put_module:
1121 	module_put(t->me);
1122 	xt_table_unlock(t);
1123  free_newinfo_counters_untrans:
1124 	vfree(counters);
1125  out:
1126 	return ret;
1127 }
1128 
1129 static int
1130 do_replace(struct net *net, const void __user *user, unsigned int len)
1131 {
1132 	int ret;
1133 	struct ip6t_replace tmp;
1134 	struct xt_table_info *newinfo;
1135 	void *loc_cpu_entry;
1136 	struct ip6t_entry *iter;
1137 
1138 	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1139 		return -EFAULT;
1140 
1141 	/* overflow check */
1142 	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1143 		return -ENOMEM;
1144 	if (tmp.num_counters == 0)
1145 		return -EINVAL;
1146 
1147 	tmp.name[sizeof(tmp.name)-1] = 0;
1148 
1149 	newinfo = xt_alloc_table_info(tmp.size);
1150 	if (!newinfo)
1151 		return -ENOMEM;
1152 
1153 	loc_cpu_entry = newinfo->entries;
1154 	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1155 			   tmp.size) != 0) {
1156 		ret = -EFAULT;
1157 		goto free_newinfo;
1158 	}
1159 
1160 	ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1161 	if (ret != 0)
1162 		goto free_newinfo;
1163 
1164 	ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1165 			   tmp.num_counters, tmp.counters);
1166 	if (ret)
1167 		goto free_newinfo_untrans;
1168 	return 0;
1169 
1170  free_newinfo_untrans:
1171 	xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1172 		cleanup_entry(iter, net);
1173  free_newinfo:
1174 	xt_free_table_info(newinfo);
1175 	return ret;
1176 }
1177 
1178 static int
1179 do_add_counters(struct net *net, const void __user *user, unsigned int len,
1180 		int compat)
1181 {
1182 	unsigned int i;
1183 	struct xt_counters_info tmp;
1184 	struct xt_counters *paddc;
1185 	struct xt_table *t;
1186 	const struct xt_table_info *private;
1187 	int ret = 0;
1188 	struct ip6t_entry *iter;
1189 	unsigned int addend;
1190 
1191 	paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1192 	if (IS_ERR(paddc))
1193 		return PTR_ERR(paddc);
1194 	t = xt_find_table_lock(net, AF_INET6, tmp.name);
1195 	if (IS_ERR(t)) {
1196 		ret = PTR_ERR(t);
1197 		goto free;
1198 	}
1199 
1200 	local_bh_disable();
1201 	private = t->private;
1202 	if (private->number != tmp.num_counters) {
1203 		ret = -EINVAL;
1204 		goto unlock_up_free;
1205 	}
1206 
1207 	i = 0;
1208 	addend = xt_write_recseq_begin();
1209 	xt_entry_foreach(iter, private->entries, private->size) {
1210 		struct xt_counters *tmp;
1211 
1212 		tmp = xt_get_this_cpu_counter(&iter->counters);
1213 		ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1214 		++i;
1215 	}
1216 	xt_write_recseq_end(addend);
1217  unlock_up_free:
1218 	local_bh_enable();
1219 	xt_table_unlock(t);
1220 	module_put(t->me);
1221  free:
1222 	vfree(paddc);
1223 
1224 	return ret;
1225 }
1226 
1227 #ifdef CONFIG_COMPAT
1228 struct compat_ip6t_replace {
1229 	char			name[XT_TABLE_MAXNAMELEN];
1230 	u32			valid_hooks;
1231 	u32			num_entries;
1232 	u32			size;
1233 	u32			hook_entry[NF_INET_NUMHOOKS];
1234 	u32			underflow[NF_INET_NUMHOOKS];
1235 	u32			num_counters;
1236 	compat_uptr_t		counters;	/* struct xt_counters * */
1237 	struct compat_ip6t_entry entries[0];
1238 };
1239 
1240 static int
1241 compat_copy_entry_to_user(struct ip6t_entry *e, void __user **dstptr,
1242 			  unsigned int *size, struct xt_counters *counters,
1243 			  unsigned int i)
1244 {
1245 	struct xt_entry_target *t;
1246 	struct compat_ip6t_entry __user *ce;
1247 	u_int16_t target_offset, next_offset;
1248 	compat_uint_t origsize;
1249 	const struct xt_entry_match *ematch;
1250 	int ret = 0;
1251 
1252 	origsize = *size;
1253 	ce = *dstptr;
1254 	if (copy_to_user(ce, e, sizeof(struct ip6t_entry)) != 0 ||
1255 	    copy_to_user(&ce->counters, &counters[i],
1256 	    sizeof(counters[i])) != 0)
1257 		return -EFAULT;
1258 
1259 	*dstptr += sizeof(struct compat_ip6t_entry);
1260 	*size -= sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1261 
1262 	xt_ematch_foreach(ematch, e) {
1263 		ret = xt_compat_match_to_user(ematch, dstptr, size);
1264 		if (ret != 0)
1265 			return ret;
1266 	}
1267 	target_offset = e->target_offset - (origsize - *size);
1268 	t = ip6t_get_target(e);
1269 	ret = xt_compat_target_to_user(t, dstptr, size);
1270 	if (ret)
1271 		return ret;
1272 	next_offset = e->next_offset - (origsize - *size);
1273 	if (put_user(target_offset, &ce->target_offset) != 0 ||
1274 	    put_user(next_offset, &ce->next_offset) != 0)
1275 		return -EFAULT;
1276 	return 0;
1277 }
1278 
1279 static int
1280 compat_find_calc_match(struct xt_entry_match *m,
1281 		       const struct ip6t_ip6 *ipv6,
1282 		       int *size)
1283 {
1284 	struct xt_match *match;
1285 
1286 	match = xt_request_find_match(NFPROTO_IPV6, m->u.user.name,
1287 				      m->u.user.revision);
1288 	if (IS_ERR(match))
1289 		return PTR_ERR(match);
1290 
1291 	m->u.kernel.match = match;
1292 	*size += xt_compat_match_offset(match);
1293 	return 0;
1294 }
1295 
1296 static void compat_release_entry(struct compat_ip6t_entry *e)
1297 {
1298 	struct xt_entry_target *t;
1299 	struct xt_entry_match *ematch;
1300 
1301 	/* Cleanup all matches */
1302 	xt_ematch_foreach(ematch, e)
1303 		module_put(ematch->u.kernel.match->me);
1304 	t = compat_ip6t_get_target(e);
1305 	module_put(t->u.kernel.target->me);
1306 }
1307 
1308 static int
1309 check_compat_entry_size_and_hooks(struct compat_ip6t_entry *e,
1310 				  struct xt_table_info *newinfo,
1311 				  unsigned int *size,
1312 				  const unsigned char *base,
1313 				  const unsigned char *limit)
1314 {
1315 	struct xt_entry_match *ematch;
1316 	struct xt_entry_target *t;
1317 	struct xt_target *target;
1318 	unsigned int entry_offset;
1319 	unsigned int j;
1320 	int ret, off;
1321 
1322 	if ((unsigned long)e % __alignof__(struct compat_ip6t_entry) != 0 ||
1323 	    (unsigned char *)e + sizeof(struct compat_ip6t_entry) >= limit ||
1324 	    (unsigned char *)e + e->next_offset > limit)
1325 		return -EINVAL;
1326 
1327 	if (e->next_offset < sizeof(struct compat_ip6t_entry) +
1328 			     sizeof(struct compat_xt_entry_target))
1329 		return -EINVAL;
1330 
1331 	if (!ip6_checkentry(&e->ipv6))
1332 		return -EINVAL;
1333 
1334 	ret = xt_compat_check_entry_offsets(e, e->elems,
1335 					    e->target_offset, e->next_offset);
1336 	if (ret)
1337 		return ret;
1338 
1339 	off = sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1340 	entry_offset = (void *)e - (void *)base;
1341 	j = 0;
1342 	xt_ematch_foreach(ematch, e) {
1343 		ret = compat_find_calc_match(ematch, &e->ipv6, &off);
1344 		if (ret != 0)
1345 			goto release_matches;
1346 		++j;
1347 	}
1348 
1349 	t = compat_ip6t_get_target(e);
1350 	target = xt_request_find_target(NFPROTO_IPV6, t->u.user.name,
1351 					t->u.user.revision);
1352 	if (IS_ERR(target)) {
1353 		ret = PTR_ERR(target);
1354 		goto release_matches;
1355 	}
1356 	t->u.kernel.target = target;
1357 
1358 	off += xt_compat_target_offset(target);
1359 	*size += off;
1360 	ret = xt_compat_add_offset(AF_INET6, entry_offset, off);
1361 	if (ret)
1362 		goto out;
1363 
1364 	return 0;
1365 
1366 out:
1367 	module_put(t->u.kernel.target->me);
1368 release_matches:
1369 	xt_ematch_foreach(ematch, e) {
1370 		if (j-- == 0)
1371 			break;
1372 		module_put(ematch->u.kernel.match->me);
1373 	}
1374 	return ret;
1375 }
1376 
1377 static void
1378 compat_copy_entry_from_user(struct compat_ip6t_entry *e, void **dstptr,
1379 			    unsigned int *size,
1380 			    struct xt_table_info *newinfo, unsigned char *base)
1381 {
1382 	struct xt_entry_target *t;
1383 	struct ip6t_entry *de;
1384 	unsigned int origsize;
1385 	int h;
1386 	struct xt_entry_match *ematch;
1387 
1388 	origsize = *size;
1389 	de = *dstptr;
1390 	memcpy(de, e, sizeof(struct ip6t_entry));
1391 	memcpy(&de->counters, &e->counters, sizeof(e->counters));
1392 
1393 	*dstptr += sizeof(struct ip6t_entry);
1394 	*size += sizeof(struct ip6t_entry) - sizeof(struct compat_ip6t_entry);
1395 
1396 	xt_ematch_foreach(ematch, e)
1397 		xt_compat_match_from_user(ematch, dstptr, size);
1398 
1399 	de->target_offset = e->target_offset - (origsize - *size);
1400 	t = compat_ip6t_get_target(e);
1401 	xt_compat_target_from_user(t, dstptr, size);
1402 
1403 	de->next_offset = e->next_offset - (origsize - *size);
1404 	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1405 		if ((unsigned char *)de - base < newinfo->hook_entry[h])
1406 			newinfo->hook_entry[h] -= origsize - *size;
1407 		if ((unsigned char *)de - base < newinfo->underflow[h])
1408 			newinfo->underflow[h] -= origsize - *size;
1409 	}
1410 }
1411 
1412 static int
1413 translate_compat_table(struct net *net,
1414 		       struct xt_table_info **pinfo,
1415 		       void **pentry0,
1416 		       const struct compat_ip6t_replace *compatr)
1417 {
1418 	unsigned int i, j;
1419 	struct xt_table_info *newinfo, *info;
1420 	void *pos, *entry0, *entry1;
1421 	struct compat_ip6t_entry *iter0;
1422 	struct ip6t_replace repl;
1423 	unsigned int size;
1424 	int ret = 0;
1425 
1426 	info = *pinfo;
1427 	entry0 = *pentry0;
1428 	size = compatr->size;
1429 	info->number = compatr->num_entries;
1430 
1431 	j = 0;
1432 	xt_compat_lock(AF_INET6);
1433 	xt_compat_init_offsets(AF_INET6, compatr->num_entries);
1434 	/* Walk through entries, checking offsets. */
1435 	xt_entry_foreach(iter0, entry0, compatr->size) {
1436 		ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1437 							entry0,
1438 							entry0 + compatr->size);
1439 		if (ret != 0)
1440 			goto out_unlock;
1441 		++j;
1442 	}
1443 
1444 	ret = -EINVAL;
1445 	if (j != compatr->num_entries)
1446 		goto out_unlock;
1447 
1448 	ret = -ENOMEM;
1449 	newinfo = xt_alloc_table_info(size);
1450 	if (!newinfo)
1451 		goto out_unlock;
1452 
1453 	newinfo->number = compatr->num_entries;
1454 	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1455 		newinfo->hook_entry[i] = compatr->hook_entry[i];
1456 		newinfo->underflow[i] = compatr->underflow[i];
1457 	}
1458 	entry1 = newinfo->entries;
1459 	pos = entry1;
1460 	size = compatr->size;
1461 	xt_entry_foreach(iter0, entry0, compatr->size)
1462 		compat_copy_entry_from_user(iter0, &pos, &size,
1463 					    newinfo, entry1);
1464 
1465 	/* all module references in entry0 are now gone. */
1466 	xt_compat_flush_offsets(AF_INET6);
1467 	xt_compat_unlock(AF_INET6);
1468 
1469 	memcpy(&repl, compatr, sizeof(*compatr));
1470 
1471 	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1472 		repl.hook_entry[i] = newinfo->hook_entry[i];
1473 		repl.underflow[i] = newinfo->underflow[i];
1474 	}
1475 
1476 	repl.num_counters = 0;
1477 	repl.counters = NULL;
1478 	repl.size = newinfo->size;
1479 	ret = translate_table(net, newinfo, entry1, &repl);
1480 	if (ret)
1481 		goto free_newinfo;
1482 
1483 	*pinfo = newinfo;
1484 	*pentry0 = entry1;
1485 	xt_free_table_info(info);
1486 	return 0;
1487 
1488 free_newinfo:
1489 	xt_free_table_info(newinfo);
1490 	return ret;
1491 out_unlock:
1492 	xt_compat_flush_offsets(AF_INET6);
1493 	xt_compat_unlock(AF_INET6);
1494 	xt_entry_foreach(iter0, entry0, compatr->size) {
1495 		if (j-- == 0)
1496 			break;
1497 		compat_release_entry(iter0);
1498 	}
1499 	return ret;
1500 }
1501 
1502 static int
1503 compat_do_replace(struct net *net, void __user *user, unsigned int len)
1504 {
1505 	int ret;
1506 	struct compat_ip6t_replace tmp;
1507 	struct xt_table_info *newinfo;
1508 	void *loc_cpu_entry;
1509 	struct ip6t_entry *iter;
1510 
1511 	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1512 		return -EFAULT;
1513 
1514 	/* overflow check */
1515 	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1516 		return -ENOMEM;
1517 	if (tmp.num_counters == 0)
1518 		return -EINVAL;
1519 
1520 	tmp.name[sizeof(tmp.name)-1] = 0;
1521 
1522 	newinfo = xt_alloc_table_info(tmp.size);
1523 	if (!newinfo)
1524 		return -ENOMEM;
1525 
1526 	loc_cpu_entry = newinfo->entries;
1527 	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1528 			   tmp.size) != 0) {
1529 		ret = -EFAULT;
1530 		goto free_newinfo;
1531 	}
1532 
1533 	ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1534 	if (ret != 0)
1535 		goto free_newinfo;
1536 
1537 	ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1538 			   tmp.num_counters, compat_ptr(tmp.counters));
1539 	if (ret)
1540 		goto free_newinfo_untrans;
1541 	return 0;
1542 
1543  free_newinfo_untrans:
1544 	xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1545 		cleanup_entry(iter, net);
1546  free_newinfo:
1547 	xt_free_table_info(newinfo);
1548 	return ret;
1549 }
1550 
1551 static int
1552 compat_do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user,
1553 		       unsigned int len)
1554 {
1555 	int ret;
1556 
1557 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1558 		return -EPERM;
1559 
1560 	switch (cmd) {
1561 	case IP6T_SO_SET_REPLACE:
1562 		ret = compat_do_replace(sock_net(sk), user, len);
1563 		break;
1564 
1565 	case IP6T_SO_SET_ADD_COUNTERS:
1566 		ret = do_add_counters(sock_net(sk), user, len, 1);
1567 		break;
1568 
1569 	default:
1570 		ret = -EINVAL;
1571 	}
1572 
1573 	return ret;
1574 }
1575 
1576 struct compat_ip6t_get_entries {
1577 	char name[XT_TABLE_MAXNAMELEN];
1578 	compat_uint_t size;
1579 	struct compat_ip6t_entry entrytable[0];
1580 };
1581 
1582 static int
1583 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1584 			    void __user *userptr)
1585 {
1586 	struct xt_counters *counters;
1587 	const struct xt_table_info *private = table->private;
1588 	void __user *pos;
1589 	unsigned int size;
1590 	int ret = 0;
1591 	unsigned int i = 0;
1592 	struct ip6t_entry *iter;
1593 
1594 	counters = alloc_counters(table);
1595 	if (IS_ERR(counters))
1596 		return PTR_ERR(counters);
1597 
1598 	pos = userptr;
1599 	size = total_size;
1600 	xt_entry_foreach(iter, private->entries, total_size) {
1601 		ret = compat_copy_entry_to_user(iter, &pos,
1602 						&size, counters, i++);
1603 		if (ret != 0)
1604 			break;
1605 	}
1606 
1607 	vfree(counters);
1608 	return ret;
1609 }
1610 
1611 static int
1612 compat_get_entries(struct net *net, struct compat_ip6t_get_entries __user *uptr,
1613 		   int *len)
1614 {
1615 	int ret;
1616 	struct compat_ip6t_get_entries get;
1617 	struct xt_table *t;
1618 
1619 	if (*len < sizeof(get))
1620 		return -EINVAL;
1621 
1622 	if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1623 		return -EFAULT;
1624 
1625 	if (*len != sizeof(struct compat_ip6t_get_entries) + get.size)
1626 		return -EINVAL;
1627 
1628 	get.name[sizeof(get.name) - 1] = '\0';
1629 
1630 	xt_compat_lock(AF_INET6);
1631 	t = xt_find_table_lock(net, AF_INET6, get.name);
1632 	if (!IS_ERR(t)) {
1633 		const struct xt_table_info *private = t->private;
1634 		struct xt_table_info info;
1635 		ret = compat_table_info(private, &info);
1636 		if (!ret && get.size == info.size)
1637 			ret = compat_copy_entries_to_user(private->size,
1638 							  t, uptr->entrytable);
1639 		else if (!ret)
1640 			ret = -EAGAIN;
1641 
1642 		xt_compat_flush_offsets(AF_INET6);
1643 		module_put(t->me);
1644 		xt_table_unlock(t);
1645 	} else
1646 		ret = PTR_ERR(t);
1647 
1648 	xt_compat_unlock(AF_INET6);
1649 	return ret;
1650 }
1651 
1652 static int do_ip6t_get_ctl(struct sock *, int, void __user *, int *);
1653 
1654 static int
1655 compat_do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1656 {
1657 	int ret;
1658 
1659 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1660 		return -EPERM;
1661 
1662 	switch (cmd) {
1663 	case IP6T_SO_GET_INFO:
1664 		ret = get_info(sock_net(sk), user, len, 1);
1665 		break;
1666 	case IP6T_SO_GET_ENTRIES:
1667 		ret = compat_get_entries(sock_net(sk), user, len);
1668 		break;
1669 	default:
1670 		ret = do_ip6t_get_ctl(sk, cmd, user, len);
1671 	}
1672 	return ret;
1673 }
1674 #endif
1675 
1676 static int
1677 do_ip6t_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1678 {
1679 	int ret;
1680 
1681 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1682 		return -EPERM;
1683 
1684 	switch (cmd) {
1685 	case IP6T_SO_SET_REPLACE:
1686 		ret = do_replace(sock_net(sk), user, len);
1687 		break;
1688 
1689 	case IP6T_SO_SET_ADD_COUNTERS:
1690 		ret = do_add_counters(sock_net(sk), user, len, 0);
1691 		break;
1692 
1693 	default:
1694 		ret = -EINVAL;
1695 	}
1696 
1697 	return ret;
1698 }
1699 
1700 static int
1701 do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1702 {
1703 	int ret;
1704 
1705 	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1706 		return -EPERM;
1707 
1708 	switch (cmd) {
1709 	case IP6T_SO_GET_INFO:
1710 		ret = get_info(sock_net(sk), user, len, 0);
1711 		break;
1712 
1713 	case IP6T_SO_GET_ENTRIES:
1714 		ret = get_entries(sock_net(sk), user, len);
1715 		break;
1716 
1717 	case IP6T_SO_GET_REVISION_MATCH:
1718 	case IP6T_SO_GET_REVISION_TARGET: {
1719 		struct xt_get_revision rev;
1720 		int target;
1721 
1722 		if (*len != sizeof(rev)) {
1723 			ret = -EINVAL;
1724 			break;
1725 		}
1726 		if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1727 			ret = -EFAULT;
1728 			break;
1729 		}
1730 		rev.name[sizeof(rev.name)-1] = 0;
1731 
1732 		if (cmd == IP6T_SO_GET_REVISION_TARGET)
1733 			target = 1;
1734 		else
1735 			target = 0;
1736 
1737 		try_then_request_module(xt_find_revision(AF_INET6, rev.name,
1738 							 rev.revision,
1739 							 target, &ret),
1740 					"ip6t_%s", rev.name);
1741 		break;
1742 	}
1743 
1744 	default:
1745 		ret = -EINVAL;
1746 	}
1747 
1748 	return ret;
1749 }
1750 
1751 static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
1752 {
1753 	struct xt_table_info *private;
1754 	void *loc_cpu_entry;
1755 	struct module *table_owner = table->me;
1756 	struct ip6t_entry *iter;
1757 
1758 	private = xt_unregister_table(table);
1759 
1760 	/* Decrease module usage counts and free resources */
1761 	loc_cpu_entry = private->entries;
1762 	xt_entry_foreach(iter, loc_cpu_entry, private->size)
1763 		cleanup_entry(iter, net);
1764 	if (private->number > private->initial_entries)
1765 		module_put(table_owner);
1766 	xt_free_table_info(private);
1767 }
1768 
1769 int ip6t_register_table(struct net *net, const struct xt_table *table,
1770 			const struct ip6t_replace *repl,
1771 			const struct nf_hook_ops *ops,
1772 			struct xt_table **res)
1773 {
1774 	int ret;
1775 	struct xt_table_info *newinfo;
1776 	struct xt_table_info bootstrap = {0};
1777 	void *loc_cpu_entry;
1778 	struct xt_table *new_table;
1779 
1780 	newinfo = xt_alloc_table_info(repl->size);
1781 	if (!newinfo)
1782 		return -ENOMEM;
1783 
1784 	loc_cpu_entry = newinfo->entries;
1785 	memcpy(loc_cpu_entry, repl->entries, repl->size);
1786 
1787 	ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1788 	if (ret != 0)
1789 		goto out_free;
1790 
1791 	new_table = xt_register_table(net, table, &bootstrap, newinfo);
1792 	if (IS_ERR(new_table)) {
1793 		ret = PTR_ERR(new_table);
1794 		goto out_free;
1795 	}
1796 
1797 	/* set res now, will see skbs right after nf_register_net_hooks */
1798 	WRITE_ONCE(*res, new_table);
1799 
1800 	ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1801 	if (ret != 0) {
1802 		__ip6t_unregister_table(net, new_table);
1803 		*res = NULL;
1804 	}
1805 
1806 	return ret;
1807 
1808 out_free:
1809 	xt_free_table_info(newinfo);
1810 	return ret;
1811 }
1812 
1813 void ip6t_unregister_table(struct net *net, struct xt_table *table,
1814 			   const struct nf_hook_ops *ops)
1815 {
1816 	nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1817 	__ip6t_unregister_table(net, table);
1818 }
1819 
1820 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1821 static inline bool
1822 icmp6_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1823 		     u_int8_t type, u_int8_t code,
1824 		     bool invert)
1825 {
1826 	return (type == test_type && code >= min_code && code <= max_code)
1827 		^ invert;
1828 }
1829 
1830 static bool
1831 icmp6_match(const struct sk_buff *skb, struct xt_action_param *par)
1832 {
1833 	const struct icmp6hdr *ic;
1834 	struct icmp6hdr _icmph;
1835 	const struct ip6t_icmp *icmpinfo = par->matchinfo;
1836 
1837 	/* Must not be a fragment. */
1838 	if (par->fragoff != 0)
1839 		return false;
1840 
1841 	ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
1842 	if (ic == NULL) {
1843 		/* We've been asked to examine this packet, and we
1844 		 * can't.  Hence, no choice but to drop.
1845 		 */
1846 		par->hotdrop = true;
1847 		return false;
1848 	}
1849 
1850 	return icmp6_type_code_match(icmpinfo->type,
1851 				     icmpinfo->code[0],
1852 				     icmpinfo->code[1],
1853 				     ic->icmp6_type, ic->icmp6_code,
1854 				     !!(icmpinfo->invflags&IP6T_ICMP_INV));
1855 }
1856 
1857 /* Called when user tries to insert an entry of this type. */
1858 static int icmp6_checkentry(const struct xt_mtchk_param *par)
1859 {
1860 	const struct ip6t_icmp *icmpinfo = par->matchinfo;
1861 
1862 	/* Must specify no unknown invflags */
1863 	return (icmpinfo->invflags & ~IP6T_ICMP_INV) ? -EINVAL : 0;
1864 }
1865 
1866 /* The built-in targets: standard (NULL) and error. */
1867 static struct xt_target ip6t_builtin_tg[] __read_mostly = {
1868 	{
1869 		.name             = XT_STANDARD_TARGET,
1870 		.targetsize       = sizeof(int),
1871 		.family           = NFPROTO_IPV6,
1872 #ifdef CONFIG_COMPAT
1873 		.compatsize       = sizeof(compat_int_t),
1874 		.compat_from_user = compat_standard_from_user,
1875 		.compat_to_user   = compat_standard_to_user,
1876 #endif
1877 	},
1878 	{
1879 		.name             = XT_ERROR_TARGET,
1880 		.target           = ip6t_error,
1881 		.targetsize       = XT_FUNCTION_MAXNAMELEN,
1882 		.family           = NFPROTO_IPV6,
1883 	},
1884 };
1885 
1886 static struct nf_sockopt_ops ip6t_sockopts = {
1887 	.pf		= PF_INET6,
1888 	.set_optmin	= IP6T_BASE_CTL,
1889 	.set_optmax	= IP6T_SO_SET_MAX+1,
1890 	.set		= do_ip6t_set_ctl,
1891 #ifdef CONFIG_COMPAT
1892 	.compat_set	= compat_do_ip6t_set_ctl,
1893 #endif
1894 	.get_optmin	= IP6T_BASE_CTL,
1895 	.get_optmax	= IP6T_SO_GET_MAX+1,
1896 	.get		= do_ip6t_get_ctl,
1897 #ifdef CONFIG_COMPAT
1898 	.compat_get	= compat_do_ip6t_get_ctl,
1899 #endif
1900 	.owner		= THIS_MODULE,
1901 };
1902 
1903 static struct xt_match ip6t_builtin_mt[] __read_mostly = {
1904 	{
1905 		.name       = "icmp6",
1906 		.match      = icmp6_match,
1907 		.matchsize  = sizeof(struct ip6t_icmp),
1908 		.checkentry = icmp6_checkentry,
1909 		.proto      = IPPROTO_ICMPV6,
1910 		.family     = NFPROTO_IPV6,
1911 	},
1912 };
1913 
1914 static int __net_init ip6_tables_net_init(struct net *net)
1915 {
1916 	return xt_proto_init(net, NFPROTO_IPV6);
1917 }
1918 
1919 static void __net_exit ip6_tables_net_exit(struct net *net)
1920 {
1921 	xt_proto_fini(net, NFPROTO_IPV6);
1922 }
1923 
1924 static struct pernet_operations ip6_tables_net_ops = {
1925 	.init = ip6_tables_net_init,
1926 	.exit = ip6_tables_net_exit,
1927 };
1928 
1929 static int __init ip6_tables_init(void)
1930 {
1931 	int ret;
1932 
1933 	ret = register_pernet_subsys(&ip6_tables_net_ops);
1934 	if (ret < 0)
1935 		goto err1;
1936 
1937 	/* No one else will be downing sem now, so we won't sleep */
1938 	ret = xt_register_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
1939 	if (ret < 0)
1940 		goto err2;
1941 	ret = xt_register_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1942 	if (ret < 0)
1943 		goto err4;
1944 
1945 	/* Register setsockopt */
1946 	ret = nf_register_sockopt(&ip6t_sockopts);
1947 	if (ret < 0)
1948 		goto err5;
1949 
1950 	return 0;
1951 
1952 err5:
1953 	xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1954 err4:
1955 	xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
1956 err2:
1957 	unregister_pernet_subsys(&ip6_tables_net_ops);
1958 err1:
1959 	return ret;
1960 }
1961 
1962 static void __exit ip6_tables_fini(void)
1963 {
1964 	nf_unregister_sockopt(&ip6t_sockopts);
1965 
1966 	xt_unregister_matches(ip6t_builtin_mt, ARRAY_SIZE(ip6t_builtin_mt));
1967 	xt_unregister_targets(ip6t_builtin_tg, ARRAY_SIZE(ip6t_builtin_tg));
1968 	unregister_pernet_subsys(&ip6_tables_net_ops);
1969 }
1970 
1971 EXPORT_SYMBOL(ip6t_register_table);
1972 EXPORT_SYMBOL(ip6t_unregister_table);
1973 EXPORT_SYMBOL(ip6t_do_table);
1974 
1975 module_init(ip6_tables_init);
1976 module_exit(ip6_tables_fini);
1977