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