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