ip_tables.c 55.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Packet matching code.
 *
 * 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>
L
Linus Torvalds 已提交
7 8 9 10 11
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
12
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
L
Linus Torvalds 已提交
13
#include <linux/cache.h>
14
#include <linux/capability.h>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21
#include <linux/skbuff.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/netdevice.h>
#include <linux/module.h>
#include <linux/icmp.h>
#include <net/ip.h>
22
#include <net/compat.h>
L
Linus Torvalds 已提交
23
#include <asm/uaccess.h>
I
Ingo Molnar 已提交
24
#include <linux/mutex.h>
L
Linus Torvalds 已提交
25 26
#include <linux/proc_fs.h>
#include <linux/err.h>
27
#include <linux/cpumask.h>
L
Linus Torvalds 已提交
28

29
#include <linux/netfilter/x_tables.h>
L
Linus Torvalds 已提交
30
#include <linux/netfilter_ipv4/ip_tables.h>
31
#include <net/netfilter/nf_log.h>
32
#include "../../netfilter/xt_repldata.h"
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
MODULE_DESCRIPTION("IPv4 packet filter");

/*#define DEBUG_IP_FIREWALL*/
/*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
/*#define DEBUG_IP_FIREWALL_USER*/

#ifdef DEBUG_IP_FIREWALL
43
#define dprintf(format, args...) pr_info(format , ## args)
L
Linus Torvalds 已提交
44 45 46 47 48
#else
#define dprintf(format, args...)
#endif

#ifdef DEBUG_IP_FIREWALL_USER
49
#define duprintf(format, args...) pr_info(format , ## args)
L
Linus Torvalds 已提交
50 51 52 53 54
#else
#define duprintf(format, args...)
#endif

#ifdef CONFIG_NETFILTER_DEBUG
55
#define IP_NF_ASSERT(x)		WARN_ON(!(x))
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65
#else
#define IP_NF_ASSERT(x)
#endif

#if 0
/* All the better to debug you with... */
#define static
#define inline
#endif

66 67 68 69 70 71
void *ipt_alloc_initial_table(const struct xt_table *info)
{
	return xt_alloc_initial_table(ipt, IPT);
}
EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);

L
Linus Torvalds 已提交
72
/* Returns whether matches rule or not. */
73
/* Performance critical - called for every packet */
74
static inline bool
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82
ip_packet_match(const struct iphdr *ip,
		const char *indev,
		const char *outdev,
		const struct ipt_ip *ipinfo,
		int isfrag)
{
	unsigned long ret;

83
#define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
L
Linus Torvalds 已提交
84 85

	if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
86 87 88
		  IPT_INV_SRCIP) ||
	    FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
		  IPT_INV_DSTIP)) {
L
Linus Torvalds 已提交
89 90
		dprintf("Source or dest mismatch.\n");

91 92
		dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
			&ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
L
Linus Torvalds 已提交
93
			ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
94 95
		dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
			&ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
L
Linus Torvalds 已提交
96
			ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
97
		return false;
L
Linus Torvalds 已提交
98 99
	}

100
	ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
L
Linus Torvalds 已提交
101 102 103 104

	if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
		dprintf("VIA in mismatch (%s vs %s).%s\n",
			indev, ipinfo->iniface,
105
			ipinfo->invflags & IPT_INV_VIA_IN ? " (INV)" : "");
106
		return false;
L
Linus Torvalds 已提交
107 108
	}

109
	ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
L
Linus Torvalds 已提交
110 111 112 113

	if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
		dprintf("VIA out mismatch (%s vs %s).%s\n",
			outdev, ipinfo->outiface,
114
			ipinfo->invflags & IPT_INV_VIA_OUT ? " (INV)" : "");
115
		return false;
L
Linus Torvalds 已提交
116 117 118
	}

	/* Check specific protocol */
119 120
	if (ipinfo->proto &&
	    FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
L
Linus Torvalds 已提交
121 122
		dprintf("Packet protocol %hi does not match %hi.%s\n",
			ip->protocol, ipinfo->proto,
123
			ipinfo->invflags & IPT_INV_PROTO ? " (INV)" : "");
124
		return false;
L
Linus Torvalds 已提交
125 126 127 128 129 130 131
	}

	/* If we have a fragment rule but the packet is not a fragment
	 * then we return zero */
	if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
		dprintf("Fragment rule but not fragment.%s\n",
			ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
132
		return false;
L
Linus Torvalds 已提交
133 134
	}

135
	return true;
L
Linus Torvalds 已提交
136 137
}

138
static bool
L
Linus Torvalds 已提交
139 140 141 142 143
ip_checkentry(const struct ipt_ip *ip)
{
	if (ip->flags & ~IPT_F_MASK) {
		duprintf("Unknown flag bits set: %08X\n",
			 ip->flags & ~IPT_F_MASK);
144
		return false;
L
Linus Torvalds 已提交
145 146 147 148
	}
	if (ip->invflags & ~IPT_INV_MASK) {
		duprintf("Unknown invflag bits set: %08X\n",
			 ip->invflags & ~IPT_INV_MASK);
149
		return false;
L
Linus Torvalds 已提交
150
	}
151
	return true;
L
Linus Torvalds 已提交
152 153 154
}

static unsigned int
155
ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
L
Linus Torvalds 已提交
156
{
157
	net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
L
Linus Torvalds 已提交
158 159 160 161

	return NF_DROP;
}

162
/* Performance critical */
L
Linus Torvalds 已提交
163
static inline struct ipt_entry *
164
get_entry(const void *base, unsigned int offset)
L
Linus Torvalds 已提交
165 166 167 168
{
	return (struct ipt_entry *)(base + offset);
}

169
/* All zeroes == unconditional rule. */
170
/* Mildly perf critical (only if packet tracing is on) */
171
static inline bool unconditional(const struct ipt_ip *ip)
172
{
173
	static const struct ipt_ip uncond;
174

175
	return memcmp(ip, &uncond, sizeof(uncond)) == 0;
176
#undef FWINV
177 178
}

179
/* for const-correctness */
180
static inline const struct xt_entry_target *
181 182 183 184 185
ipt_get_target_c(const struct ipt_entry *e)
{
	return ipt_get_target((struct ipt_entry *)e);
}

186
#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
187
static const char *const hooknames[] = {
188 189
	[NF_INET_PRE_ROUTING]		= "PREROUTING",
	[NF_INET_LOCAL_IN]		= "INPUT",
190
	[NF_INET_FORWARD]		= "FORWARD",
191 192
	[NF_INET_LOCAL_OUT]		= "OUTPUT",
	[NF_INET_POST_ROUTING]		= "POSTROUTING",
193 194 195 196 197 198 199 200
};

enum nf_ip_trace_comments {
	NF_IP_TRACE_COMMENT_RULE,
	NF_IP_TRACE_COMMENT_RETURN,
	NF_IP_TRACE_COMMENT_POLICY,
};

201
static const char *const comments[] = {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	[NF_IP_TRACE_COMMENT_RULE]	= "rule",
	[NF_IP_TRACE_COMMENT_RETURN]	= "return",
	[NF_IP_TRACE_COMMENT_POLICY]	= "policy",
};

static struct nf_loginfo trace_loginfo = {
	.type = NF_LOG_TYPE_LOG,
	.u = {
		.log = {
			.level = 4,
			.logflags = NF_LOG_MASK,
		},
	},
};

217
/* Mildly perf critical (only if packet tracing is on) */
218
static inline int
219
get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
220 221
		      const char *hookname, const char **chainname,
		      const char **comment, unsigned int *rulenum)
222
{
223
	const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
224

225
	if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
226 227 228 229 230 231
		/* Head of user chain: ERROR target with chainname */
		*chainname = t->target.data;
		(*rulenum) = 0;
	} else if (s == e) {
		(*rulenum)++;

232 233
		if (s->target_offset == sizeof(struct ipt_entry) &&
		    strcmp(t->target.u.kernel.target->name,
234
			   XT_STANDARD_TARGET) == 0 &&
235 236
		   t->verdict < 0 &&
		   unconditional(&s->ip)) {
237 238
			/* Tail of chains: STANDARD target (return/policy) */
			*comment = *chainname == hookname
239 240
				? comments[NF_IP_TRACE_COMMENT_POLICY]
				: comments[NF_IP_TRACE_COMMENT_RETURN];
241 242 243 244 245 246 247 248
		}
		return 1;
	} else
		(*rulenum)++;

	return 0;
}

249 250
static void trace_packet(struct net *net,
			 const struct sk_buff *skb,
251 252 253
			 unsigned int hook,
			 const struct net_device *in,
			 const struct net_device *out,
254
			 const char *tablename,
255 256
			 const struct xt_table_info *private,
			 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 262
	unsigned int rulenum = 0;

263
	root = get_entry(private->entries, private->hook_entry[hook]);
264

265 266
	hookname = chainname = hooknames[hook];
	comment = comments[NF_IP_TRACE_COMMENT_RULE];
267

268 269 270 271
	xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
		if (get_chainname_rulenum(iter, e, hookname,
		    &chainname, &comment, &rulenum) != 0)
			break;
272

273 274 275
	nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
		     "TRACE: %s:%s:%s:%u ",
		     tablename, chainname, comment, rulenum);
276 277 278
}
#endif

279
static inline
280 281 282 283 284
struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
{
	return (void *)entry + entry->next_offset;
}

L
Linus Torvalds 已提交
285 286
/* Returns one of the generic firewall policies, like NF_ACCEPT. */
unsigned int
287
ipt_do_table(struct sk_buff *skb,
288
	     const struct nf_hook_state *state,
289
	     struct xt_table *table)
L
Linus Torvalds 已提交
290
{
291
	unsigned int hook = state->hook;
L
Linus Torvalds 已提交
292
	static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
293
	const struct iphdr *ip;
L
Linus Torvalds 已提交
294 295 296
	/* Initializing verdict to NF_DROP keeps gcc happy. */
	unsigned int verdict = NF_DROP;
	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;
L
Linus Torvalds 已提交
303 304

	/* Initialization */
305
	stackidx = 0;
306
	ip = ip_hdr(skb);
307 308
	indev = state->in ? state->in->name : nulldevname;
	outdev = state->out ? state->out->name : nulldevname;
L
Linus Torvalds 已提交
309 310 311 312 313 314
	/* We handle fragments by dealing with the first fragment as
	 * if it was a normal packet.  All other fragments are treated
	 * normally, except that they will NEVER match rules that ask
	 * things we don't know, ie. tcp syn flag or ports).  If the
	 * rule is also a fragment-specific rule, non-fragments won't
	 * match it. */
315 316
	acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
	acpar.thoff   = ip_hdrlen(skb);
317
	acpar.hotdrop = false;
318
	acpar.net     = state->net;
319 320
	acpar.in      = state->in;
	acpar.out     = state->out;
321 322
	acpar.family  = NFPROTO_IPV4;
	acpar.hooknum = hook;
L
Linus Torvalds 已提交
323 324

	IP_NF_ASSERT(table->valid_hooks & (1 << hook));
325 326
	local_bh_disable();
	addend = xt_write_recseq_begin();
327
	private = table->private;
328
	cpu        = smp_processor_id();
329 330 331 332 333
	/*
	 * Ensure we load private-> members after we've fetched the base
	 * pointer.
	 */
	smp_read_barrier_depends();
334
	table_base = private->entries;
335
	jumpstack  = (struct ipt_entry **)private->jumpstack[cpu];
336 337 338 339 340 341 342 343

	/* Switch to alternate jumpstack if we're being invoked via TEE.
	 * TEE issues XT_CONTINUE verdict on original skb so we must not
	 * clobber the jumpstack.
	 *
	 * For recursion via REJECT or SYNPROXY the stack will be clobbered
	 * but it is no problem since absolute verdict is issued by these.
	 */
344 345
	if (static_key_false(&xt_tee_enabled))
		jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
346

347
	e = get_entry(table_base, private->hook_entry[hook]);
L
Linus Torvalds 已提交
348

349 350
	pr_debug("Entering %s(hook %u), UF %p\n",
		 table->name, hook,
351
		 get_entry(table_base, private->underflow[hook]));
L
Linus Torvalds 已提交
352 353

	do {
354
		const struct xt_entry_target *t;
355
		const struct xt_entry_match *ematch;
356
		struct xt_counters *counter;
357

L
Linus Torvalds 已提交
358
		IP_NF_ASSERT(e);
359
		if (!ip_packet_match(ip, indev, outdev,
360
		    &e->ip, acpar.fragoff)) {
361
 no_match:
362 363 364
			e = ipt_next_entry(e);
			continue;
		}
L
Linus Torvalds 已提交
365

366
		xt_ematch_foreach(ematch, e) {
367 368 369
			acpar.match     = ematch->u.kernel.match;
			acpar.matchinfo = ematch->data;
			if (!acpar.match->match(skb, &acpar))
370
				goto no_match;
371
		}
372

373 374
		counter = xt_get_this_cpu_counter(&e->counters);
		ADD_COUNTER(*counter, skb->len, 1);
L
Linus Torvalds 已提交
375

376 377
		t = ipt_get_target(e);
		IP_NF_ASSERT(t->u.kernel.target);
378

379
#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
380 381
		/* The packet is traced: log it */
		if (unlikely(skb->nf_trace))
382 383
			trace_packet(state->net, skb, hook, state->in,
				     state->out, table->name, private, e);
384
#endif
385 386 387 388
		/* Standard target? */
		if (!t->u.kernel.target->target) {
			int v;

389
			v = ((struct xt_standard_target *)t)->verdict;
390 391
			if (v < 0) {
				/* Pop from stack? */
392
				if (v != XT_RETURN) {
393
					verdict = (unsigned int)(-v) - 1;
394
					break;
L
Linus Torvalds 已提交
395
				}
396
				if (stackidx == 0) {
397 398
					e = get_entry(table_base,
					    private->underflow[hook]);
399
					pr_debug("Underflow (this is normal) "
400 401
						 "to %p\n", e);
				} else {
402
					e = jumpstack[--stackidx];
403
					pr_debug("Pulled %p out from pos %u\n",
404
						 e, stackidx);
405 406
					e = ipt_next_entry(e);
				}
407 408
				continue;
			}
409 410
			if (table_base + v != ipt_next_entry(e) &&
			    !(e->ip.flags & IPT_F_GOTO)) {
411
				jumpstack[stackidx++] = e;
412
				pr_debug("Pushed %p into pos %u\n",
413
					 e, stackidx - 1);
414
			}
L
Linus Torvalds 已提交
415

416
			e = get_entry(table_base, v);
417 418 419
			continue;
		}

420 421
		acpar.target   = t->u.kernel.target;
		acpar.targinfo = t->data;
422

423
		verdict = t->u.kernel.target->target(skb, &acpar);
424 425
		/* Target might have changed stuff. */
		ip = ip_hdr(skb);
426
		if (verdict == XT_CONTINUE)
427 428 429 430
			e = ipt_next_entry(e);
		else
			/* Verdict */
			break;
431
	} while (!acpar.hotdrop);
432 433
	pr_debug("Exiting %s; sp at %u\n", __func__, stackidx);

I
Ian Morris 已提交
434 435
	xt_write_recseq_end(addend);
	local_bh_enable();
436

L
Linus Torvalds 已提交
437 438 439
#ifdef DEBUG_ALLOW_ALL
	return NF_ACCEPT;
#else
440
	if (acpar.hotdrop)
L
Linus Torvalds 已提交
441 442 443 444 445 446
		return NF_DROP;
	else return verdict;
#endif
}

/* Figures out from what hook each rule can be called: returns 0 if
447
   there are loops.  Puts hook bitmask in comefrom. */
L
Linus Torvalds 已提交
448
static int
449
mark_source_chains(const struct xt_table_info *newinfo,
450
		   unsigned int valid_hooks, void *entry0)
L
Linus Torvalds 已提交
451 452 453 454 455
{
	unsigned int hook;

	/* No recursion; use packet counter to save back ptrs (reset
	   to 0 as we leave), and comefrom to save source hook bitmask */
456
	for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
L
Linus Torvalds 已提交
457
		unsigned int pos = newinfo->hook_entry[hook];
458
		struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466

		if (!(valid_hooks & (1 << hook)))
			continue;

		/* Set initial back pointer. */
		e->counters.pcnt = pos;

		for (;;) {
467
			const struct xt_standard_target *t
468
				= (void *)ipt_get_target_c(e);
469
			int visited = e->comefrom & (1 << hook);
L
Linus Torvalds 已提交
470

471
			if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
472
				pr_err("iptables: loop hook %u pos %u %08X.\n",
L
Linus Torvalds 已提交
473 474 475
				       hook, pos, e->comefrom);
				return 0;
			}
476
			e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
L
Linus Torvalds 已提交
477 478

			/* Unconditional return/END. */
479 480
			if ((e->target_offset == sizeof(struct ipt_entry) &&
			     (strcmp(t->target.u.user.name,
481
				     XT_STANDARD_TARGET) == 0) &&
482 483
			     t->verdict < 0 && unconditional(&e->ip)) ||
			    visited) {
L
Linus Torvalds 已提交
484 485
				unsigned int oldpos, size;

486
				if ((strcmp(t->target.u.user.name,
I
Ian Morris 已提交
487
					    XT_STANDARD_TARGET) == 0) &&
488
				    t->verdict < -NF_MAX_VERDICT - 1) {
489 490 491 492 493 494
					duprintf("mark_source_chains: bad "
						"negative verdict (%i)\n",
								t->verdict);
					return 0;
				}

L
Linus Torvalds 已提交
495 496 497
				/* Return: backtrack through the last
				   big jump. */
				do {
498
					e->comefrom ^= (1<<NF_INET_NUMHOOKS);
L
Linus Torvalds 已提交
499 500
#ifdef DEBUG_IP_FIREWALL_USER
					if (e->comefrom
501
					    & (1 << NF_INET_NUMHOOKS)) {
L
Linus Torvalds 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
						duprintf("Back unset "
							 "on hook %u "
							 "rule %u\n",
							 hook, pos);
					}
#endif
					oldpos = pos;
					pos = e->counters.pcnt;
					e->counters.pcnt = 0;

					/* We're at the start. */
					if (pos == oldpos)
						goto next;

					e = (struct ipt_entry *)
517
						(entry0 + pos);
L
Linus Torvalds 已提交
518 519 520 521 522
				} while (oldpos == pos + e->next_offset);

				/* Move along one */
				size = e->next_offset;
				e = (struct ipt_entry *)
523
					(entry0 + pos + size);
L
Linus Torvalds 已提交
524 525 526 527 528 529
				e->counters.pcnt = pos;
				pos += size;
			} else {
				int newpos = t->verdict;

				if (strcmp(t->target.u.user.name,
530
					   XT_STANDARD_TARGET) == 0 &&
531
				    newpos >= 0) {
532 533 534 535 536 537 538
					if (newpos > newinfo->size -
						sizeof(struct ipt_entry)) {
						duprintf("mark_source_chains: "
							"bad verdict (%i)\n",
								newpos);
						return 0;
					}
L
Linus Torvalds 已提交
539
					/* This a jump; chase it. */
540 541
					duprintf("Jump rule %u -> %u\n",
						 pos, newpos);
L
Linus Torvalds 已提交
542 543 544 545 546
				} else {
					/* ... this is a fallthru */
					newpos = pos + e->next_offset;
				}
				e = (struct ipt_entry *)
547
					(entry0 + newpos);
L
Linus Torvalds 已提交
548 549 550 551
				e->counters.pcnt = pos;
				pos = newpos;
			}
		}
I
Ian Morris 已提交
552
next:
L
Linus Torvalds 已提交
553 554 555 556 557
		duprintf("Finished chain %u\n", hook);
	}
	return 1;
}

558
static void cleanup_match(struct xt_entry_match *m, struct net *net)
L
Linus Torvalds 已提交
559
{
560 561
	struct xt_mtdtor_param par;

562
	par.net       = net;
563 564
	par.match     = m->u.kernel.match;
	par.matchinfo = m->data;
565
	par.family    = NFPROTO_IPV4;
566 567 568
	if (par.match->destroy != NULL)
		par.match->destroy(&par);
	module_put(par.match->me);
L
Linus Torvalds 已提交
569 570
}

571
static int
572
check_entry(const struct ipt_entry *e, const char *name)
573
{
574
	const struct xt_entry_target *t;
575 576

	if (!ip_checkentry(&e->ip)) {
577
		duprintf("ip check failed %p %s.\n", e, name);
578 579 580
		return -EINVAL;
	}

581
	if (e->target_offset + sizeof(struct xt_entry_target) >
582
	    e->next_offset)
583 584
		return -EINVAL;

585
	t = ipt_get_target_c(e);
586 587 588 589 590 591
	if (e->target_offset + t->u.target_size > e->next_offset)
		return -EINVAL;

	return 0;
}

592
static int
593
check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
594
{
595
	const struct ipt_ip *ip = par->entryinfo;
596 597
	int ret;

598 599 600
	par->match     = m->u.kernel.match;
	par->matchinfo = m->data;

601
	ret = xt_check_match(par, m->u.match_size - sizeof(*m),
602
	      ip->proto, ip->invflags & IPT_INV_PROTO);
603
	if (ret < 0) {
604
		duprintf("check failed for `%s'.\n", par->match->name);
605
		return ret;
606
	}
607
	return 0;
608 609
}

610
static int
611
find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
L
Linus Torvalds 已提交
612
{
613
	struct xt_match *match;
614
	int ret;
L
Linus Torvalds 已提交
615

616 617 618
	match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
				      m->u.user.revision);
	if (IS_ERR(match)) {
619
		duprintf("find_check_match: `%s' not found\n", m->u.user.name);
620
		return PTR_ERR(match);
L
Linus Torvalds 已提交
621 622 623
	}
	m->u.kernel.match = match;

624
	ret = check_match(m, par);
625 626 627
	if (ret)
		goto err;

L
Linus Torvalds 已提交
628
	return 0;
629 630 631
err:
	module_put(m->u.kernel.match->me);
	return ret;
L
Linus Torvalds 已提交
632 633
}

634
static int check_target(struct ipt_entry *e, struct net *net, const char *name)
635
{
636
	struct xt_entry_target *t = ipt_get_target(e);
637
	struct xt_tgchk_param par = {
638
		.net       = net,
639 640 641 642 643
		.table     = name,
		.entryinfo = e,
		.target    = t->u.kernel.target,
		.targinfo  = t->data,
		.hook_mask = e->comefrom,
644
		.family    = NFPROTO_IPV4,
645
	};
646
	int ret;
647

648
	ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
649
	      e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
650
	if (ret < 0) {
651
		duprintf("check failed for `%s'.\n",
652
			 t->u.kernel.target->name);
653
		return ret;
654
	}
655
	return 0;
656
}
L
Linus Torvalds 已提交
657

658
static int
659
find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
660
		 unsigned int size)
L
Linus Torvalds 已提交
661
{
662
	struct xt_entry_target *t;
663
	struct xt_target *target;
L
Linus Torvalds 已提交
664 665
	int ret;
	unsigned int j;
666
	struct xt_mtchk_param mtpar;
667
	struct xt_entry_match *ematch;
L
Linus Torvalds 已提交
668

669 670 671
	ret = check_entry(e, name);
	if (ret)
		return ret;
672

673 674 675 676
	e->counters.pcnt = xt_percpu_counter_alloc();
	if (IS_ERR_VALUE(e->counters.pcnt))
		return -ENOMEM;

L
Linus Torvalds 已提交
677
	j = 0;
678
	mtpar.net	= net;
679 680 681
	mtpar.table     = name;
	mtpar.entryinfo = &e->ip;
	mtpar.hook_mask = e->comefrom;
682
	mtpar.family    = NFPROTO_IPV4;
683
	xt_ematch_foreach(ematch, e) {
684
		ret = find_check_match(ematch, &mtpar);
685
		if (ret != 0)
686 687
			goto cleanup_matches;
		++j;
688
	}
L
Linus Torvalds 已提交
689 690

	t = ipt_get_target(e);
691 692 693
	target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
					t->u.user.revision);
	if (IS_ERR(target)) {
694
		duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
695
		ret = PTR_ERR(target);
L
Linus Torvalds 已提交
696 697 698 699
		goto cleanup_matches;
	}
	t->u.kernel.target = target;

700
	ret = check_target(e, net, name);
701 702
	if (ret)
		goto err;
703

L
Linus Torvalds 已提交
704
	return 0;
705 706
 err:
	module_put(t->u.kernel.target->me);
L
Linus Torvalds 已提交
707
 cleanup_matches:
708 709
	xt_ematch_foreach(ematch, e) {
		if (j-- == 0)
710
			break;
711 712
		cleanup_match(ematch, net);
	}
713 714 715

	xt_percpu_counter_free(e->counters.pcnt);

L
Linus Torvalds 已提交
716 717 718
	return ret;
}

719
static bool check_underflow(const struct ipt_entry *e)
720
{
721
	const struct xt_entry_target *t;
722 723 724 725
	unsigned int verdict;

	if (!unconditional(&e->ip))
		return false;
726
	t = ipt_get_target_c(e);
727 728
	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
		return false;
729
	verdict = ((struct xt_standard_target *)t)->verdict;
730 731 732 733
	verdict = -verdict - 1;
	return verdict == NF_DROP || verdict == NF_ACCEPT;
}

734
static int
L
Linus Torvalds 已提交
735
check_entry_size_and_hooks(struct ipt_entry *e,
736
			   struct xt_table_info *newinfo,
737 738
			   const unsigned char *base,
			   const unsigned char *limit,
L
Linus Torvalds 已提交
739 740
			   const unsigned int *hook_entries,
			   const unsigned int *underflows,
741
			   unsigned int valid_hooks)
L
Linus Torvalds 已提交
742 743 744
{
	unsigned int h;

745 746
	if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
	    (unsigned char *)e + sizeof(struct ipt_entry) >= limit) {
L
Linus Torvalds 已提交
747 748 749 750 751
		duprintf("Bad offset %p\n", e);
		return -EINVAL;
	}

	if (e->next_offset
752
	    < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) {
L
Linus Torvalds 已提交
753 754 755 756 757 758
		duprintf("checking: element %p size %u\n",
			 e, e->next_offset);
		return -EINVAL;
	}

	/* Check hooks & underflows */
759
	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
760 761
		if (!(valid_hooks & (1 << h)))
			continue;
L
Linus Torvalds 已提交
762 763
		if ((unsigned char *)e - base == hook_entries[h])
			newinfo->hook_entry[h] = hook_entries[h];
764
		if ((unsigned char *)e - base == underflows[h]) {
765 766 767 768
			if (!check_underflow(e)) {
				pr_err("Underflows must be unconditional and "
				       "use the STANDARD target with "
				       "ACCEPT/DROP\n");
769 770
				return -EINVAL;
			}
L
Linus Torvalds 已提交
771
			newinfo->underflow[h] = underflows[h];
772
		}
L
Linus Torvalds 已提交
773 774 775
	}

	/* Clear counters and comefrom */
776
	e->counters = ((struct xt_counters) { 0, 0 });
L
Linus Torvalds 已提交
777 778 779 780
	e->comefrom = 0;
	return 0;
}

781 782
static void
cleanup_entry(struct ipt_entry *e, struct net *net)
L
Linus Torvalds 已提交
783
{
784
	struct xt_tgdtor_param par;
785
	struct xt_entry_target *t;
786
	struct xt_entry_match *ematch;
L
Linus Torvalds 已提交
787 788

	/* Cleanup all matches */
789
	xt_ematch_foreach(ematch, e)
790
		cleanup_match(ematch, net);
L
Linus Torvalds 已提交
791
	t = ipt_get_target(e);
792

793
	par.net      = net;
794 795
	par.target   = t->u.kernel.target;
	par.targinfo = t->data;
796
	par.family   = NFPROTO_IPV4;
797 798 799
	if (par.target->destroy != NULL)
		par.target->destroy(&par);
	module_put(par.target->me);
800
	xt_percpu_counter_free(e->counters.pcnt);
L
Linus Torvalds 已提交
801 802 803 804 805
}

/* Checks and translates the user-supplied table segment (held in
   newinfo) */
static int
806
translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
807
		const struct ipt_replace *repl)
L
Linus Torvalds 已提交
808
{
809
	struct ipt_entry *iter;
L
Linus Torvalds 已提交
810
	unsigned int i;
811
	int ret = 0;
L
Linus Torvalds 已提交
812

813 814
	newinfo->size = repl->size;
	newinfo->number = repl->num_entries;
L
Linus Torvalds 已提交
815 816

	/* Init all hooks to impossible value. */
817
	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
L
Linus Torvalds 已提交
818 819 820 821 822 823 824
		newinfo->hook_entry[i] = 0xFFFFFFFF;
		newinfo->underflow[i] = 0xFFFFFFFF;
	}

	duprintf("translate_table: size %u\n", newinfo->size);
	i = 0;
	/* Walk through entries, checking offsets. */
825 826
	xt_entry_foreach(iter, entry0, newinfo->size) {
		ret = check_entry_size_and_hooks(iter, newinfo, entry0,
827 828 829 830
						 entry0 + repl->size,
						 repl->hook_entry,
						 repl->underflow,
						 repl->valid_hooks);
831
		if (ret != 0)
832 833
			return ret;
		++i;
834 835 836
		if (strcmp(ipt_get_target(iter)->u.user.name,
		    XT_ERROR_TARGET) == 0)
			++newinfo->stacksize;
837
	}
L
Linus Torvalds 已提交
838

839
	if (i != repl->num_entries) {
L
Linus Torvalds 已提交
840
		duprintf("translate_table: %u not %u entries\n",
841
			 i, repl->num_entries);
L
Linus Torvalds 已提交
842 843 844 845
		return -EINVAL;
	}

	/* Check hooks all assigned */
846
	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
L
Linus Torvalds 已提交
847
		/* Only hooks which are valid */
848
		if (!(repl->valid_hooks & (1 << i)))
L
Linus Torvalds 已提交
849 850 851
			continue;
		if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
			duprintf("Invalid hook entry %u %u\n",
852
				 i, repl->hook_entry[i]);
L
Linus Torvalds 已提交
853 854 855 856
			return -EINVAL;
		}
		if (newinfo->underflow[i] == 0xFFFFFFFF) {
			duprintf("Invalid underflow %u %u\n",
857
				 i, repl->underflow[i]);
L
Linus Torvalds 已提交
858 859 860 861
			return -EINVAL;
		}
	}

862
	if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
863 864
		return -ELOOP;

L
Linus Torvalds 已提交
865 866
	/* Finally, each sanity check must pass */
	i = 0;
867
	xt_entry_foreach(iter, entry0, newinfo->size) {
868
		ret = find_check_entry(iter, net, repl->name, repl->size);
869 870
		if (ret != 0)
			break;
871
		++i;
872
	}
L
Linus Torvalds 已提交
873

874
	if (ret != 0) {
875 876
		xt_entry_foreach(iter, entry0, newinfo->size) {
			if (i-- == 0)
877
				break;
878 879
			cleanup_entry(iter, net);
		}
880 881
		return ret;
	}
L
Linus Torvalds 已提交
882 883 884 885 886

	return ret;
}

static void
887 888
get_counters(const struct xt_table_info *t,
	     struct xt_counters counters[])
L
Linus Torvalds 已提交
889
{
890
	struct ipt_entry *iter;
L
Linus Torvalds 已提交
891 892 893
	unsigned int cpu;
	unsigned int i;

894
	for_each_possible_cpu(cpu) {
895
		seqcount_t *s = &per_cpu(xt_recseq, cpu);
896

L
Linus Torvalds 已提交
897
		i = 0;
898
		xt_entry_foreach(iter, t->entries, t->size) {
899
			struct xt_counters *tmp;
900 901 902
			u64 bcnt, pcnt;
			unsigned int start;

903
			tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
904
			do {
905
				start = read_seqcount_begin(s);
906 907
				bcnt = tmp->bcnt;
				pcnt = tmp->pcnt;
908
			} while (read_seqcount_retry(s, start));
909 910

			ADD_COUNTER(counters[i], bcnt, pcnt);
911 912
			++i; /* macro does multi eval of i */
		}
L
Linus Torvalds 已提交
913
	}
914 915
}

916
static struct xt_counters *alloc_counters(const struct xt_table *table)
L
Linus Torvalds 已提交
917
{
918
	unsigned int countersize;
919
	struct xt_counters *counters;
920
	const struct xt_table_info *private = table->private;
L
Linus Torvalds 已提交
921 922 923 924

	/* We need atomic snapshot of counters: rest doesn't change
	   (other than comefrom, which userspace doesn't care
	   about). */
925
	countersize = sizeof(struct xt_counters) * private->number;
926
	counters = vzalloc(countersize);
L
Linus Torvalds 已提交
927 928

	if (counters == NULL)
929
		return ERR_PTR(-ENOMEM);
930

931
	get_counters(private, counters);
L
Linus Torvalds 已提交
932

933 934 935 936 937
	return counters;
}

static int
copy_entries_to_user(unsigned int total_size,
938
		     const struct xt_table *table,
939 940 941
		     void __user *userptr)
{
	unsigned int off, num;
942
	const struct ipt_entry *e;
943
	struct xt_counters *counters;
944
	const struct xt_table_info *private = table->private;
945
	int ret = 0;
946
	const void *loc_cpu_entry;
947 948 949 950 951

	counters = alloc_counters(table);
	if (IS_ERR(counters))
		return PTR_ERR(counters);

952
	loc_cpu_entry = private->entries;
953
	if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
L
Linus Torvalds 已提交
954 955 956 957 958 959 960 961
		ret = -EFAULT;
		goto free_counters;
	}

	/* FIXME: use iterator macros --RR */
	/* ... then go back and fix counters and names */
	for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
		unsigned int i;
962 963
		const struct xt_entry_match *m;
		const struct xt_entry_target *t;
L
Linus Torvalds 已提交
964

965
		e = (struct ipt_entry *)(loc_cpu_entry + off);
L
Linus Torvalds 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979
		if (copy_to_user(userptr + off
				 + offsetof(struct ipt_entry, counters),
				 &counters[num],
				 sizeof(counters[num])) != 0) {
			ret = -EFAULT;
			goto free_counters;
		}

		for (i = sizeof(struct ipt_entry);
		     i < e->target_offset;
		     i += m->u.match_size) {
			m = (void *)e + i;

			if (copy_to_user(userptr + off + i
980
					 + offsetof(struct xt_entry_match,
L
Linus Torvalds 已提交
981 982 983 984 985 986 987 988 989
						    u.user.name),
					 m->u.kernel.match->name,
					 strlen(m->u.kernel.match->name)+1)
			    != 0) {
				ret = -EFAULT;
				goto free_counters;
			}
		}

990
		t = ipt_get_target_c(e);
L
Linus Torvalds 已提交
991
		if (copy_to_user(userptr + off + e->target_offset
992
				 + offsetof(struct xt_entry_target,
L
Linus Torvalds 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
					    u.user.name),
				 t->u.kernel.target->name,
				 strlen(t->u.kernel.target->name)+1) != 0) {
			ret = -EFAULT;
			goto free_counters;
		}
	}

 free_counters:
	vfree(counters);
	return ret;
}

1006
#ifdef CONFIG_COMPAT
1007
static void compat_standard_from_user(void *dst, const void *src)
1008
{
1009
	int v = *(compat_int_t *)src;
1010

1011
	if (v > 0)
1012
		v += xt_compat_calc_jump(AF_INET, v);
1013 1014
	memcpy(dst, &v, sizeof(v));
}
1015

1016
static int compat_standard_to_user(void __user *dst, const void *src)
1017
{
1018
	compat_int_t cv = *(int *)src;
1019

1020
	if (cv > 0)
1021
		cv -= xt_compat_calc_jump(AF_INET, cv);
1022
	return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
1023 1024
}

1025
static int compat_calc_entry(const struct ipt_entry *e,
1026
			     const struct xt_table_info *info,
1027
			     const void *base, struct xt_table_info *newinfo)
1028
{
1029
	const struct xt_entry_match *ematch;
1030
	const struct xt_entry_target *t;
1031
	unsigned int entry_offset;
1032 1033
	int off, i, ret;

1034
	off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1035
	entry_offset = (void *)e - base;
1036
	xt_ematch_foreach(ematch, e)
1037
		off += xt_compat_match_offset(ematch->u.kernel.match);
1038
	t = ipt_get_target_c(e);
1039
	off += xt_compat_target_offset(t->u.kernel.target);
1040
	newinfo->size -= off;
1041
	ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1042 1043 1044
	if (ret)
		return ret;

1045
	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1046 1047
		if (info->hook_entry[i] &&
		    (e < (struct ipt_entry *)(base + info->hook_entry[i])))
1048
			newinfo->hook_entry[i] -= off;
1049 1050
		if (info->underflow[i] &&
		    (e < (struct ipt_entry *)(base + info->underflow[i])))
1051 1052 1053 1054 1055
			newinfo->underflow[i] -= off;
	}
	return 0;
}

1056
static int compat_table_info(const struct xt_table_info *info,
1057
			     struct xt_table_info *newinfo)
1058
{
1059
	struct ipt_entry *iter;
1060
	const void *loc_cpu_entry;
1061
	int ret;
1062 1063 1064 1065

	if (!newinfo || !info)
		return -EINVAL;

1066
	/* we dont care about newinfo->entries */
1067 1068
	memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
	newinfo->initial_entries = 0;
1069
	loc_cpu_entry = info->entries;
1070
	xt_compat_init_offsets(AF_INET, info->number);
1071 1072 1073
	xt_entry_foreach(iter, loc_cpu_entry, info->size) {
		ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
		if (ret != 0)
1074
			return ret;
1075
	}
1076
	return 0;
1077 1078 1079
}
#endif

1080
static int get_info(struct net *net, void __user *user,
1081
		    const int *len, int compat)
1082
{
1083
	char name[XT_TABLE_MAXNAMELEN];
1084
	struct xt_table *t;
1085 1086 1087
	int ret;

	if (*len != sizeof(struct ipt_getinfo)) {
1088 1089
		duprintf("length %u != %zu\n", *len,
			 sizeof(struct ipt_getinfo));
1090 1091 1092 1093 1094 1095
		return -EINVAL;
	}

	if (copy_from_user(name, user, sizeof(name)) != 0)
		return -EFAULT;

1096
	name[XT_TABLE_MAXNAMELEN-1] = '\0';
1097 1098 1099 1100
#ifdef CONFIG_COMPAT
	if (compat)
		xt_compat_lock(AF_INET);
#endif
1101
	t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1102
				    "iptable_%s", name);
1103
	if (!IS_ERR_OR_NULL(t)) {
1104
		struct ipt_getinfo info;
1105
		const struct xt_table_info *private = t->private;
1106
#ifdef CONFIG_COMPAT
1107 1108
		struct xt_table_info tmp;

1109 1110
		if (compat) {
			ret = compat_table_info(private, &tmp);
1111
			xt_compat_flush_offsets(AF_INET);
1112
			private = &tmp;
1113 1114
		}
#endif
1115
		memset(&info, 0, sizeof(info));
1116 1117
		info.valid_hooks = t->valid_hooks;
		memcpy(info.hook_entry, private->hook_entry,
1118
		       sizeof(info.hook_entry));
1119
		memcpy(info.underflow, private->underflow,
1120
		       sizeof(info.underflow));
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
		info.num_entries = private->number;
		info.size = private->size;
		strcpy(info.name, name);

		if (copy_to_user(user, &info, *len) != 0)
			ret = -EFAULT;
		else
			ret = 0;

		xt_table_unlock(t);
		module_put(t->me);
	} else
		ret = t ? PTR_ERR(t) : -ENOENT;
#ifdef CONFIG_COMPAT
	if (compat)
		xt_compat_unlock(AF_INET);
#endif
	return ret;
}

static int
1142 1143
get_entries(struct net *net, struct ipt_get_entries __user *uptr,
	    const int *len)
1144 1145 1146
{
	int ret;
	struct ipt_get_entries get;
1147
	struct xt_table *t;
1148 1149

	if (*len < sizeof(get)) {
1150
		duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
1151 1152 1153 1154 1155
		return -EINVAL;
	}
	if (copy_from_user(&get, uptr, sizeof(get)) != 0)
		return -EFAULT;
	if (*len != sizeof(struct ipt_get_entries) + get.size) {
1156 1157
		duprintf("get_entries: %u != %zu\n",
			 *len, sizeof(get) + get.size);
1158 1159 1160
		return -EINVAL;
	}

1161
	t = xt_find_table_lock(net, AF_INET, get.name);
1162
	if (!IS_ERR_OR_NULL(t)) {
1163
		const struct xt_table_info *private = t->private;
1164
		duprintf("t->private->number = %u\n", private->number);
1165 1166 1167 1168 1169
		if (get.size == private->size)
			ret = copy_entries_to_user(private->size,
						   t, uptr->entrytable);
		else {
			duprintf("get_entries: I've got %u not %u!\n",
1170
				 private->size, get.size);
1171
			ret = -EAGAIN;
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
		}
		module_put(t->me);
		xt_table_unlock(t);
	} else
		ret = t ? PTR_ERR(t) : -ENOENT;

	return ret;
}

static int
1182
__do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1183 1184
	     struct xt_table_info *newinfo, unsigned int num_counters,
	     void __user *counters_ptr)
1185 1186
{
	int ret;
1187
	struct xt_table *t;
1188 1189
	struct xt_table_info *oldinfo;
	struct xt_counters *counters;
1190
	struct ipt_entry *iter;
1191 1192

	ret = 0;
1193
	counters = vzalloc(num_counters * sizeof(struct xt_counters));
1194 1195 1196 1197 1198
	if (!counters) {
		ret = -ENOMEM;
		goto out;
	}

1199
	t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1200
				    "iptable_%s", name);
1201
	if (IS_ERR_OR_NULL(t)) {
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
		ret = t ? PTR_ERR(t) : -ENOENT;
		goto free_newinfo_counters_untrans;
	}

	/* You lied! */
	if (valid_hooks != t->valid_hooks) {
		duprintf("Valid hook crap: %08X vs %08X\n",
			 valid_hooks, t->valid_hooks);
		ret = -EINVAL;
		goto put_module;
	}

	oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
	if (!oldinfo)
		goto put_module;

	/* Update module usage count based on number of rules */
	duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
		oldinfo->number, oldinfo->initial_entries, newinfo->number);
	if ((oldinfo->number > oldinfo->initial_entries) ||
	    (newinfo->number <= oldinfo->initial_entries))
		module_put(t->me);
	if ((oldinfo->number > oldinfo->initial_entries) &&
	    (newinfo->number <= oldinfo->initial_entries))
		module_put(t->me);

1228
	/* Get the old counters, and synchronize with replace */
1229
	get_counters(oldinfo, counters);
1230

1231
	/* Decrease module usage counts and free resource */
1232
	xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1233
		cleanup_entry(iter, net);
1234

1235 1236
	xt_free_table_info(oldinfo);
	if (copy_to_user(counters_ptr, counters,
1237 1238 1239 1240
			 sizeof(struct xt_counters) * num_counters) != 0) {
		/* Silent error, can't fail, new table is already in place */
		net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
	}
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	vfree(counters);
	xt_table_unlock(t);
	return ret;

 put_module:
	module_put(t->me);
	xt_table_unlock(t);
 free_newinfo_counters_untrans:
	vfree(counters);
 out:
	return ret;
}

static int
1255
do_replace(struct net *net, const void __user *user, unsigned int len)
1256 1257 1258 1259 1260
{
	int ret;
	struct ipt_replace tmp;
	struct xt_table_info *newinfo;
	void *loc_cpu_entry;
1261
	struct ipt_entry *iter;
1262 1263 1264 1265 1266 1267 1268

	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
		return -EFAULT;

	/* overflow check */
	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
		return -ENOMEM;
1269 1270 1271
	if (tmp.num_counters == 0)
		return -EINVAL;

1272
	tmp.name[sizeof(tmp.name)-1] = 0;
1273 1274 1275 1276 1277

	newinfo = xt_alloc_table_info(tmp.size);
	if (!newinfo)
		return -ENOMEM;

1278
	loc_cpu_entry = newinfo->entries;
1279 1280 1281 1282 1283 1284
	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
			   tmp.size) != 0) {
		ret = -EFAULT;
		goto free_newinfo;
	}

1285
	ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1286 1287 1288
	if (ret != 0)
		goto free_newinfo;

1289
	duprintf("Translated table\n");
1290

1291
	ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1292
			   tmp.num_counters, tmp.counters);
1293 1294 1295 1296 1297
	if (ret)
		goto free_newinfo_untrans;
	return 0;

 free_newinfo_untrans:
1298
	xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1299
		cleanup_entry(iter, net);
1300 1301 1302 1303 1304 1305
 free_newinfo:
	xt_free_table_info(newinfo);
	return ret;
}

static int
1306
do_add_counters(struct net *net, const void __user *user,
1307
		unsigned int len, int compat)
1308
{
1309
	unsigned int i;
1310 1311 1312
	struct xt_counters_info tmp;
	struct xt_counters *paddc;
	unsigned int num_counters;
1313
	const char *name;
1314 1315
	int size;
	void *ptmp;
1316
	struct xt_table *t;
1317
	const struct xt_table_info *private;
1318
	int ret = 0;
1319
	struct ipt_entry *iter;
1320
	unsigned int addend;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
#ifdef CONFIG_COMPAT
	struct compat_xt_counters_info compat_tmp;

	if (compat) {
		ptmp = &compat_tmp;
		size = sizeof(struct compat_xt_counters_info);
	} else
#endif
	{
		ptmp = &tmp;
		size = sizeof(struct xt_counters_info);
	}

	if (copy_from_user(ptmp, user, size) != 0)
		return -EFAULT;

#ifdef CONFIG_COMPAT
	if (compat) {
		num_counters = compat_tmp.num_counters;
		name = compat_tmp.name;
	} else
#endif
	{
		num_counters = tmp.num_counters;
		name = tmp.name;
	}

	if (len != size + num_counters * sizeof(struct xt_counters))
		return -EINVAL;

E
Eric Dumazet 已提交
1351
	paddc = vmalloc(len - size);
1352 1353 1354 1355 1356 1357 1358 1359
	if (!paddc)
		return -ENOMEM;

	if (copy_from_user(paddc, user + size, len - size) != 0) {
		ret = -EFAULT;
		goto free;
	}

1360
	t = xt_find_table_lock(net, AF_INET, name);
1361
	if (IS_ERR_OR_NULL(t)) {
1362 1363 1364 1365
		ret = t ? PTR_ERR(t) : -ENOENT;
		goto free;
	}

1366
	local_bh_disable();
1367 1368 1369 1370 1371 1372 1373
	private = t->private;
	if (private->number != num_counters) {
		ret = -EINVAL;
		goto unlock_up_free;
	}

	i = 0;
1374
	addend = xt_write_recseq_begin();
1375
	xt_entry_foreach(iter, private->entries, private->size) {
1376 1377 1378 1379
		struct xt_counters *tmp;

		tmp = xt_get_this_cpu_counter(&iter->counters);
		ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1380 1381
		++i;
	}
1382
	xt_write_recseq_end(addend);
1383
 unlock_up_free:
1384
	local_bh_enable();
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	xt_table_unlock(t);
	module_put(t->me);
 free:
	vfree(paddc);

	return ret;
}

#ifdef CONFIG_COMPAT
struct compat_ipt_replace {
1395
	char			name[XT_TABLE_MAXNAMELEN];
1396 1397 1398
	u32			valid_hooks;
	u32			num_entries;
	u32			size;
1399 1400
	u32			hook_entry[NF_INET_NUMHOOKS];
	u32			underflow[NF_INET_NUMHOOKS];
1401
	u32			num_counters;
1402
	compat_uptr_t		counters;	/* struct xt_counters * */
1403 1404 1405
	struct compat_ipt_entry	entries[0];
};

1406 1407
static int
compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1408
			  unsigned int *size, struct xt_counters *counters,
1409
			  unsigned int i)
1410
{
1411
	struct xt_entry_target *t;
1412 1413 1414
	struct compat_ipt_entry __user *ce;
	u_int16_t target_offset, next_offset;
	compat_uint_t origsize;
1415 1416
	const struct xt_entry_match *ematch;
	int ret = 0;
1417 1418 1419

	origsize = *size;
	ce = (struct compat_ipt_entry __user *)*dstptr;
1420 1421 1422 1423
	if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
	    copy_to_user(&ce->counters, &counters[i],
	    sizeof(counters[i])) != 0)
		return -EFAULT;
1424

1425
	*dstptr += sizeof(struct compat_ipt_entry);
1426 1427
	*size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);

1428 1429 1430
	xt_ematch_foreach(ematch, e) {
		ret = xt_compat_match_to_user(ematch, dstptr, size);
		if (ret != 0)
1431
			return ret;
1432
	}
1433 1434
	target_offset = e->target_offset - (origsize - *size);
	t = ipt_get_target(e);
1435
	ret = xt_compat_target_to_user(t, dstptr, size);
1436
	if (ret)
1437
		return ret;
1438
	next_offset = e->next_offset - (origsize - *size);
1439 1440 1441
	if (put_user(target_offset, &ce->target_offset) != 0 ||
	    put_user(next_offset, &ce->next_offset) != 0)
		return -EFAULT;
1442 1443 1444
	return 0;
}

1445
static int
1446
compat_find_calc_match(struct xt_entry_match *m,
1447 1448
		       const char *name,
		       const struct ipt_ip *ip,
1449
		       int *size)
1450
{
1451
	struct xt_match *match;
1452

1453 1454 1455
	match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
				      m->u.user.revision);
	if (IS_ERR(match)) {
1456
		duprintf("compat_check_calc_match: `%s' not found\n",
1457
			 m->u.user.name);
1458
		return PTR_ERR(match);
1459 1460
	}
	m->u.kernel.match = match;
1461
	*size += xt_compat_match_offset(match);
1462 1463 1464
	return 0;
}

1465
static void compat_release_entry(struct compat_ipt_entry *e)
1466
{
1467
	struct xt_entry_target *t;
1468
	struct xt_entry_match *ematch;
1469 1470

	/* Cleanup all matches */
1471
	xt_ematch_foreach(ematch, e)
1472
		module_put(ematch->u.kernel.match->me);
1473
	t = compat_ipt_get_target(e);
1474 1475 1476
	module_put(t->u.kernel.target->me);
}

1477
static int
1478
check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
1479 1480
				  struct xt_table_info *newinfo,
				  unsigned int *size,
1481 1482 1483 1484
				  const unsigned char *base,
				  const unsigned char *limit,
				  const unsigned int *hook_entries,
				  const unsigned int *underflows,
1485
				  const char *name)
1486
{
1487
	struct xt_entry_match *ematch;
1488
	struct xt_entry_target *t;
1489
	struct xt_target *target;
1490
	unsigned int entry_offset;
1491 1492
	unsigned int j;
	int ret, off, h;
1493 1494

	duprintf("check_compat_entry_size_and_hooks %p\n", e);
1495 1496
	if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
	    (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit) {
1497 1498 1499 1500 1501
		duprintf("Bad offset %p, limit = %p\n", e, limit);
		return -EINVAL;
	}

	if (e->next_offset < sizeof(struct compat_ipt_entry) +
1502
			     sizeof(struct compat_xt_entry_target)) {
1503 1504 1505 1506 1507
		duprintf("checking: element %p size %u\n",
			 e, e->next_offset);
		return -EINVAL;
	}

1508 1509
	/* For purposes of check_entry casting the compat entry is fine */
	ret = check_entry((struct ipt_entry *)e, name);
1510 1511
	if (ret)
		return ret;
1512

1513
	off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1514 1515
	entry_offset = (void *)e - (void *)base;
	j = 0;
1516
	xt_ematch_foreach(ematch, e) {
1517
		ret = compat_find_calc_match(ematch, name, &e->ip, &off);
1518
		if (ret != 0)
1519 1520
			goto release_matches;
		++j;
1521
	}
1522

1523
	t = compat_ipt_get_target(e);
1524 1525 1526
	target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
					t->u.user.revision);
	if (IS_ERR(target)) {
1527
		duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1528
			 t->u.user.name);
1529
		ret = PTR_ERR(target);
1530
		goto release_matches;
1531 1532 1533
	}
	t->u.kernel.target = target;

1534
	off += xt_compat_target_offset(target);
1535
	*size += off;
1536
	ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1537 1538 1539 1540
	if (ret)
		goto out;

	/* Check hooks & underflows */
1541
	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1542 1543 1544 1545 1546 1547 1548
		if ((unsigned char *)e - base == hook_entries[h])
			newinfo->hook_entry[h] = hook_entries[h];
		if ((unsigned char *)e - base == underflows[h])
			newinfo->underflow[h] = underflows[h];
	}

	/* Clear counters and comefrom */
1549
	memset(&e->counters, 0, sizeof(e->counters));
1550 1551
	e->comefrom = 0;
	return 0;
1552

1553
out:
1554
	module_put(t->u.kernel.target->me);
1555
release_matches:
1556 1557
	xt_ematch_foreach(ematch, e) {
		if (j-- == 0)
1558
			break;
1559 1560
		module_put(ematch->u.kernel.match->me);
	}
1561 1562 1563
	return ret;
}

1564
static int
1565
compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
1566 1567
			    unsigned int *size, const char *name,
			    struct xt_table_info *newinfo, unsigned char *base)
1568
{
1569
	struct xt_entry_target *t;
1570
	struct xt_target *target;
1571 1572
	struct ipt_entry *de;
	unsigned int origsize;
1573
	int ret, h;
1574
	struct xt_entry_match *ematch;
1575 1576 1577 1578 1579

	ret = 0;
	origsize = *size;
	de = (struct ipt_entry *)*dstptr;
	memcpy(de, e, sizeof(struct ipt_entry));
1580
	memcpy(&de->counters, &e->counters, sizeof(e->counters));
1581

1582
	*dstptr += sizeof(struct ipt_entry);
1583 1584
	*size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);

1585 1586 1587
	xt_ematch_foreach(ematch, e) {
		ret = xt_compat_match_from_user(ematch, dstptr, size);
		if (ret != 0)
1588
			return ret;
1589
	}
1590
	de->target_offset = e->target_offset - (origsize - *size);
1591
	t = compat_ipt_get_target(e);
1592
	target = t->u.kernel.target;
1593
	xt_compat_target_from_user(t, dstptr, size);
1594 1595

	de->next_offset = e->next_offset - (origsize - *size);
1596
	for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1597 1598 1599 1600 1601
		if ((unsigned char *)de - base < newinfo->hook_entry[h])
			newinfo->hook_entry[h] -= origsize - *size;
		if ((unsigned char *)de - base < newinfo->underflow[h])
			newinfo->underflow[h] -= origsize - *size;
	}
1602 1603 1604
	return ret;
}

1605
static int
1606
compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)
1607
{
1608
	struct xt_entry_match *ematch;
1609
	struct xt_mtchk_param mtpar;
1610
	unsigned int j;
1611
	int ret = 0;
1612

1613 1614 1615 1616
	e->counters.pcnt = xt_percpu_counter_alloc();
	if (IS_ERR_VALUE(e->counters.pcnt))
		return -ENOMEM;

1617
	j = 0;
1618
	mtpar.net	= net;
1619 1620 1621
	mtpar.table     = name;
	mtpar.entryinfo = &e->ip;
	mtpar.hook_mask = e->comefrom;
1622
	mtpar.family    = NFPROTO_IPV4;
1623
	xt_ematch_foreach(ematch, e) {
1624
		ret = check_match(ematch, &mtpar);
1625
		if (ret != 0)
1626 1627
			goto cleanup_matches;
		++j;
1628
	}
1629

1630
	ret = check_target(e, net, name);
1631 1632 1633 1634 1635
	if (ret)
		goto cleanup_matches;
	return 0;

 cleanup_matches:
1636 1637
	xt_ematch_foreach(ematch, e) {
		if (j-- == 0)
1638
			break;
1639 1640
		cleanup_match(ematch, net);
	}
1641 1642 1643

	xt_percpu_counter_free(e->counters.pcnt);

1644
	return ret;
1645 1646
}

L
Linus Torvalds 已提交
1647
static int
1648 1649
translate_compat_table(struct net *net,
		       const char *name,
1650 1651 1652 1653 1654 1655 1656
		       unsigned int valid_hooks,
		       struct xt_table_info **pinfo,
		       void **pentry0,
		       unsigned int total_size,
		       unsigned int number,
		       unsigned int *hook_entries,
		       unsigned int *underflows)
L
Linus Torvalds 已提交
1657
{
1658
	unsigned int i, j;
1659 1660
	struct xt_table_info *newinfo, *info;
	void *pos, *entry0, *entry1;
1661 1662
	struct compat_ipt_entry *iter0;
	struct ipt_entry *iter1;
1663
	unsigned int size;
1664
	int ret;
L
Linus Torvalds 已提交
1665

1666 1667 1668 1669 1670 1671
	info = *pinfo;
	entry0 = *pentry0;
	size = total_size;
	info->number = number;

	/* Init all hooks to impossible value. */
1672
	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1673 1674 1675 1676 1677
		info->hook_entry[i] = 0xFFFFFFFF;
		info->underflow[i] = 0xFFFFFFFF;
	}

	duprintf("translate_compat_table: size %u\n", info->size);
1678
	j = 0;
1679
	xt_compat_lock(AF_INET);
1680
	xt_compat_init_offsets(AF_INET, number);
1681
	/* Walk through entries, checking offsets. */
1682 1683
	xt_entry_foreach(iter0, entry0, total_size) {
		ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1684 1685 1686 1687 1688
							entry0,
							entry0 + total_size,
							hook_entries,
							underflows,
							name);
1689
		if (ret != 0)
1690 1691
			goto out_unlock;
		++j;
1692
	}
1693 1694

	ret = -EINVAL;
1695
	if (j != number) {
1696
		duprintf("translate_compat_table: %u not %u entries\n",
1697
			 j, number);
1698 1699 1700 1701
		goto out_unlock;
	}

	/* Check hooks all assigned */
1702
	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1703 1704 1705 1706 1707 1708 1709
		/* Only hooks which are valid */
		if (!(valid_hooks & (1 << i)))
			continue;
		if (info->hook_entry[i] == 0xFFFFFFFF) {
			duprintf("Invalid hook entry %u %u\n",
				 i, hook_entries[i]);
			goto out_unlock;
L
Linus Torvalds 已提交
1710
		}
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
		if (info->underflow[i] == 0xFFFFFFFF) {
			duprintf("Invalid underflow %u %u\n",
				 i, underflows[i]);
			goto out_unlock;
		}
	}

	ret = -ENOMEM;
	newinfo = xt_alloc_table_info(size);
	if (!newinfo)
		goto out_unlock;

	newinfo->number = number;
1724
	for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1725 1726 1727
		newinfo->hook_entry[i] = info->hook_entry[i];
		newinfo->underflow[i] = info->underflow[i];
	}
1728
	entry1 = newinfo->entries;
1729
	pos = entry1;
1730
	size = total_size;
1731
	xt_entry_foreach(iter0, entry0, total_size) {
1732 1733
		ret = compat_copy_entry_from_user(iter0, &pos, &size,
						  name, newinfo, entry1);
1734 1735 1736
		if (ret != 0)
			break;
	}
1737
	xt_compat_flush_offsets(AF_INET);
1738 1739 1740 1741 1742 1743 1744 1745
	xt_compat_unlock(AF_INET);
	if (ret)
		goto free_newinfo;

	ret = -ELOOP;
	if (!mark_source_chains(newinfo, valid_hooks, entry1))
		goto free_newinfo;

1746
	i = 0;
1747
	xt_entry_foreach(iter1, entry1, newinfo->size) {
1748
		ret = compat_check_entry(iter1, net, name);
1749 1750
		if (ret != 0)
			break;
1751
		++i;
1752 1753 1754
		if (strcmp(ipt_get_target(iter1)->u.user.name,
		    XT_ERROR_TARGET) == 0)
			++newinfo->stacksize;
1755
	}
1756
	if (ret) {
1757 1758 1759 1760 1761 1762
		/*
		 * The first i matches need cleanup_entry (calls ->destroy)
		 * because they had called ->check already. The other j-i
		 * entries need only release.
		 */
		int skip = i;
1763
		j -= i;
1764 1765 1766
		xt_entry_foreach(iter0, entry0, newinfo->size) {
			if (skip-- > 0)
				continue;
1767
			if (j-- == 0)
1768
				break;
1769
			compat_release_entry(iter0);
1770
		}
1771 1772
		xt_entry_foreach(iter1, entry1, newinfo->size) {
			if (i-- == 0)
1773
				break;
1774 1775
			cleanup_entry(iter1, net);
		}
1776 1777 1778
		xt_free_table_info(newinfo);
		return ret;
	}
1779

1780 1781 1782 1783
	*pinfo = newinfo;
	*pentry0 = entry1;
	xt_free_table_info(info);
	return 0;
L
Linus Torvalds 已提交
1784

1785 1786 1787
free_newinfo:
	xt_free_table_info(newinfo);
out:
1788 1789
	xt_entry_foreach(iter0, entry0, total_size) {
		if (j-- == 0)
1790
			break;
1791 1792
		compat_release_entry(iter0);
	}
L
Linus Torvalds 已提交
1793
	return ret;
1794
out_unlock:
1795
	xt_compat_flush_offsets(AF_INET);
1796 1797
	xt_compat_unlock(AF_INET);
	goto out;
L
Linus Torvalds 已提交
1798 1799 1800
}

static int
1801
compat_do_replace(struct net *net, void __user *user, unsigned int len)
L
Linus Torvalds 已提交
1802 1803
{
	int ret;
1804 1805 1806
	struct compat_ipt_replace tmp;
	struct xt_table_info *newinfo;
	void *loc_cpu_entry;
1807
	struct ipt_entry *iter;
L
Linus Torvalds 已提交
1808 1809 1810 1811

	if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
		return -EFAULT;

1812
	/* overflow check */
1813
	if (tmp.size >= INT_MAX / num_possible_cpus())
1814 1815 1816
		return -ENOMEM;
	if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
		return -ENOMEM;
1817 1818 1819
	if (tmp.num_counters == 0)
		return -EINVAL;

1820
	tmp.name[sizeof(tmp.name)-1] = 0;
1821

1822
	newinfo = xt_alloc_table_info(tmp.size);
L
Linus Torvalds 已提交
1823 1824 1825
	if (!newinfo)
		return -ENOMEM;

1826
	loc_cpu_entry = newinfo->entries;
1827
	if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
L
Linus Torvalds 已提交
1828 1829 1830 1831 1832
			   tmp.size) != 0) {
		ret = -EFAULT;
		goto free_newinfo;
	}

1833
	ret = translate_compat_table(net, tmp.name, tmp.valid_hooks,
1834 1835 1836
				     &newinfo, &loc_cpu_entry, tmp.size,
				     tmp.num_entries, tmp.hook_entry,
				     tmp.underflow);
1837
	if (ret != 0)
L
Linus Torvalds 已提交
1838 1839
		goto free_newinfo;

1840
	duprintf("compat_do_replace: Translated table\n");
L
Linus Torvalds 已提交
1841

1842
	ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1843
			   tmp.num_counters, compat_ptr(tmp.counters));
1844 1845 1846
	if (ret)
		goto free_newinfo_untrans;
	return 0;
L
Linus Torvalds 已提交
1847

1848
 free_newinfo_untrans:
1849
	xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1850
		cleanup_entry(iter, net);
1851 1852 1853 1854
 free_newinfo:
	xt_free_table_info(newinfo);
	return ret;
}
L
Linus Torvalds 已提交
1855

1856 1857
static int
compat_do_ipt_set_ctl(struct sock *sk,	int cmd, void __user *user,
1858
		      unsigned int len)
1859 1860
{
	int ret;
L
Linus Torvalds 已提交
1861

1862
	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1863
		return -EPERM;
L
Linus Torvalds 已提交
1864

1865 1866
	switch (cmd) {
	case IPT_SO_SET_REPLACE:
1867
		ret = compat_do_replace(sock_net(sk), user, len);
1868
		break;
L
Linus Torvalds 已提交
1869

1870
	case IPT_SO_SET_ADD_COUNTERS:
1871
		ret = do_add_counters(sock_net(sk), user, len, 1);
1872 1873 1874 1875 1876 1877
		break;

	default:
		duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);
		ret = -EINVAL;
	}
L
Linus Torvalds 已提交
1878 1879 1880 1881

	return ret;
}

1882
struct compat_ipt_get_entries {
1883
	char name[XT_TABLE_MAXNAMELEN];
1884 1885 1886
	compat_uint_t size;
	struct compat_ipt_entry entrytable[0];
};
L
Linus Torvalds 已提交
1887

1888 1889 1890
static int
compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
			    void __user *userptr)
1891 1892
{
	struct xt_counters *counters;
1893
	const struct xt_table_info *private = table->private;
1894 1895 1896
	void __user *pos;
	unsigned int size;
	int ret = 0;
1897
	unsigned int i = 0;
1898
	struct ipt_entry *iter;
L
Linus Torvalds 已提交
1899

1900 1901 1902 1903 1904 1905
	counters = alloc_counters(table);
	if (IS_ERR(counters))
		return PTR_ERR(counters);

	pos = userptr;
	size = total_size;
1906
	xt_entry_foreach(iter, private->entries, total_size) {
1907
		ret = compat_copy_entry_to_user(iter, &pos,
1908
						&size, counters, i++);
1909 1910 1911
		if (ret != 0)
			break;
	}
1912 1913 1914

	vfree(counters);
	return ret;
L
Linus Torvalds 已提交
1915 1916 1917
}

static int
1918 1919
compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
		   int *len)
L
Linus Torvalds 已提交
1920
{
1921 1922
	int ret;
	struct compat_ipt_get_entries get;
1923
	struct xt_table *t;
L
Linus Torvalds 已提交
1924

1925
	if (*len < sizeof(get)) {
1926
		duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
L
Linus Torvalds 已提交
1927
		return -EINVAL;
1928
	}
L
Linus Torvalds 已提交
1929

1930 1931
	if (copy_from_user(&get, uptr, sizeof(get)) != 0)
		return -EFAULT;
L
Linus Torvalds 已提交
1932

1933
	if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
1934 1935
		duprintf("compat_get_entries: %u != %zu\n",
			 *len, sizeof(get) + get.size);
1936
		return -EINVAL;
L
Linus Torvalds 已提交
1937 1938
	}

1939
	xt_compat_lock(AF_INET);
1940
	t = xt_find_table_lock(net, AF_INET, get.name);
1941
	if (!IS_ERR_OR_NULL(t)) {
1942
		const struct xt_table_info *private = t->private;
1943
		struct xt_table_info info;
1944
		duprintf("t->private->number = %u\n", private->number);
1945 1946 1947
		ret = compat_table_info(private, &info);
		if (!ret && get.size == info.size) {
			ret = compat_copy_entries_to_user(private->size,
1948
							  t, uptr->entrytable);
1949 1950
		} else if (!ret) {
			duprintf("compat_get_entries: I've got %u not %u!\n",
1951
				 private->size, get.size);
1952
			ret = -EAGAIN;
1953
		}
1954
		xt_compat_flush_offsets(AF_INET);
1955 1956 1957
		module_put(t->me);
		xt_table_unlock(t);
	} else
L
Linus Torvalds 已提交
1958 1959
		ret = t ? PTR_ERR(t) : -ENOENT;

1960 1961 1962
	xt_compat_unlock(AF_INET);
	return ret;
}
L
Linus Torvalds 已提交
1963

1964 1965
static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);

1966 1967 1968 1969
static int
compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
{
	int ret;
L
Linus Torvalds 已提交
1970

1971
	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1972 1973
		return -EPERM;

1974 1975
	switch (cmd) {
	case IPT_SO_GET_INFO:
1976
		ret = get_info(sock_net(sk), user, len, 1);
1977 1978
		break;
	case IPT_SO_GET_ENTRIES:
1979
		ret = compat_get_entries(sock_net(sk), user, len);
1980 1981
		break;
	default:
1982
		ret = do_ipt_get_ctl(sk, cmd, user, len);
1983
	}
L
Linus Torvalds 已提交
1984 1985
	return ret;
}
1986
#endif
L
Linus Torvalds 已提交
1987 1988

static int
1989
do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
L
Linus Torvalds 已提交
1990 1991 1992
{
	int ret;

1993
	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
1994 1995 1996 1997
		return -EPERM;

	switch (cmd) {
	case IPT_SO_SET_REPLACE:
1998
		ret = do_replace(sock_net(sk), user, len);
L
Linus Torvalds 已提交
1999 2000 2001
		break;

	case IPT_SO_SET_ADD_COUNTERS:
2002
		ret = do_add_counters(sock_net(sk), user, len, 0);
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
		break;

	default:
		duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);
		ret = -EINVAL;
	}

	return ret;
}

static int
do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
{
	int ret;

2018
	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
2019 2020 2021
		return -EPERM;

	switch (cmd) {
2022
	case IPT_SO_GET_INFO:
2023
		ret = get_info(sock_net(sk), user, len, 0);
2024
		break;
L
Linus Torvalds 已提交
2025

2026
	case IPT_SO_GET_ENTRIES:
2027
		ret = get_entries(sock_net(sk), user, len);
L
Linus Torvalds 已提交
2028 2029 2030 2031
		break;

	case IPT_SO_GET_REVISION_MATCH:
	case IPT_SO_GET_REVISION_TARGET: {
2032
		struct xt_get_revision rev;
2033
		int target;
L
Linus Torvalds 已提交
2034 2035 2036 2037 2038 2039 2040 2041 2042

		if (*len != sizeof(rev)) {
			ret = -EINVAL;
			break;
		}
		if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
			ret = -EFAULT;
			break;
		}
2043
		rev.name[sizeof(rev.name)-1] = 0;
L
Linus Torvalds 已提交
2044 2045

		if (cmd == IPT_SO_GET_REVISION_TARGET)
2046
			target = 1;
L
Linus Torvalds 已提交
2047
		else
2048
			target = 0;
L
Linus Torvalds 已提交
2049

2050 2051 2052
		try_then_request_module(xt_find_revision(AF_INET, rev.name,
							 rev.revision,
							 target, &ret),
L
Linus Torvalds 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
					"ipt_%s", rev.name);
		break;
	}

	default:
		duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
		ret = -EINVAL;
	}

	return ret;
}

2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
static void __ipt_unregister_table(struct net *net, struct xt_table *table)
{
	struct xt_table_info *private;
	void *loc_cpu_entry;
	struct module *table_owner = table->me;
	struct ipt_entry *iter;

	private = xt_unregister_table(table);

	/* Decrease module usage counts and free resources */
	loc_cpu_entry = private->entries;
	xt_entry_foreach(iter, loc_cpu_entry, private->size)
		cleanup_entry(iter, net);
	if (private->number > private->initial_entries)
		module_put(table_owner);
	xt_free_table_info(private);
}

2083 2084 2085
int ipt_register_table(struct net *net, const struct xt_table *table,
		       const struct ipt_replace *repl,
		       const struct nf_hook_ops *ops, struct xt_table **res)
L
Linus Torvalds 已提交
2086 2087
{
	int ret;
2088
	struct xt_table_info *newinfo;
2089
	struct xt_table_info bootstrap = {0};
2090
	void *loc_cpu_entry;
2091
	struct xt_table *new_table;
L
Linus Torvalds 已提交
2092

2093
	newinfo = xt_alloc_table_info(repl->size);
2094 2095
	if (!newinfo)
		return -ENOMEM;
L
Linus Torvalds 已提交
2096

2097
	loc_cpu_entry = newinfo->entries;
2098
	memcpy(loc_cpu_entry, repl->entries, repl->size);
L
Linus Torvalds 已提交
2099

2100
	ret = translate_table(net, newinfo, loc_cpu_entry, repl);
2101 2102
	if (ret != 0)
		goto out_free;
L
Linus Torvalds 已提交
2103

2104
	new_table = xt_register_table(net, table, &bootstrap, newinfo);
2105
	if (IS_ERR(new_table)) {
2106 2107
		ret = PTR_ERR(new_table);
		goto out_free;
L
Linus Torvalds 已提交
2108 2109
	}

2110
	/* set res now, will see skbs right after nf_register_net_hooks */
2111
	WRITE_ONCE(*res, new_table);
2112 2113 2114 2115 2116 2117 2118

	ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
	if (ret != 0) {
		__ipt_unregister_table(net, new_table);
		*res = NULL;
	}

2119
	return ret;
2120 2121 2122

out_free:
	xt_free_table_info(newinfo);
2123
	return ret;
L
Linus Torvalds 已提交
2124 2125
}

2126 2127
void ipt_unregister_table(struct net *net, struct xt_table *table,
			  const struct nf_hook_ops *ops)
L
Linus Torvalds 已提交
2128
{
2129 2130
	nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
	__ipt_unregister_table(net, table);
L
Linus Torvalds 已提交
2131 2132 2133
}

/* Returns 1 if the type and code is matched by the range, 0 otherwise */
2134
static inline bool
L
Linus Torvalds 已提交
2135 2136
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,
2137
		     bool invert)
L
Linus Torvalds 已提交
2138
{
2139 2140
	return ((test_type == 0xFF) ||
		(type == test_type && code >= min_code && code <= max_code))
L
Linus Torvalds 已提交
2141 2142 2143
		^ invert;
}

2144
static bool
2145
icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
L
Linus Torvalds 已提交
2146
{
2147 2148
	const struct icmphdr *ic;
	struct icmphdr _icmph;
2149
	const struct ipt_icmp *icmpinfo = par->matchinfo;
L
Linus Torvalds 已提交
2150 2151

	/* Must not be a fragment. */
2152
	if (par->fragoff != 0)
2153
		return false;
L
Linus Torvalds 已提交
2154

2155
	ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
L
Linus Torvalds 已提交
2156 2157 2158 2159 2160
	if (ic == NULL) {
		/* We've been asked to examine this packet, and we
		 * can't.  Hence, no choice but to drop.
		 */
		duprintf("Dropping evil ICMP tinygram.\n");
2161
		par->hotdrop = true;
2162
		return false;
L
Linus Torvalds 已提交
2163 2164 2165 2166 2167 2168 2169 2170 2171
	}

	return icmp_type_code_match(icmpinfo->type,
				    icmpinfo->code[0],
				    icmpinfo->code[1],
				    ic->type, ic->code,
				    !!(icmpinfo->invflags&IPT_ICMP_INV));
}

2172
static int icmp_checkentry(const struct xt_mtchk_param *par)
L
Linus Torvalds 已提交
2173
{
2174
	const struct ipt_icmp *icmpinfo = par->matchinfo;
L
Linus Torvalds 已提交
2175

2176
	/* Must specify no unknown invflags */
2177
	return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
L
Linus Torvalds 已提交
2178 2179
}

2180 2181
static struct xt_target ipt_builtin_tg[] __read_mostly = {
	{
2182
		.name             = XT_STANDARD_TARGET,
2183 2184
		.targetsize       = sizeof(int),
		.family           = NFPROTO_IPV4,
2185
#ifdef CONFIG_COMPAT
2186 2187 2188
		.compatsize       = sizeof(compat_int_t),
		.compat_from_user = compat_standard_from_user,
		.compat_to_user   = compat_standard_to_user,
2189
#endif
2190 2191
	},
	{
2192
		.name             = XT_ERROR_TARGET,
2193
		.target           = ipt_error,
2194
		.targetsize       = XT_FUNCTION_MAXNAMELEN,
2195 2196
		.family           = NFPROTO_IPV4,
	},
L
Linus Torvalds 已提交
2197 2198 2199 2200 2201 2202 2203
};

static struct nf_sockopt_ops ipt_sockopts = {
	.pf		= PF_INET,
	.set_optmin	= IPT_BASE_CTL,
	.set_optmax	= IPT_SO_SET_MAX+1,
	.set		= do_ipt_set_ctl,
2204 2205 2206
#ifdef CONFIG_COMPAT
	.compat_set	= compat_do_ipt_set_ctl,
#endif
L
Linus Torvalds 已提交
2207 2208 2209
	.get_optmin	= IPT_BASE_CTL,
	.get_optmax	= IPT_SO_GET_MAX+1,
	.get		= do_ipt_get_ctl,
2210 2211 2212
#ifdef CONFIG_COMPAT
	.compat_get	= compat_do_ipt_get_ctl,
#endif
2213
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
2214 2215
};

2216 2217 2218 2219 2220 2221 2222 2223 2224
static struct xt_match ipt_builtin_mt[] __read_mostly = {
	{
		.name       = "icmp",
		.match      = icmp_match,
		.matchsize  = sizeof(struct ipt_icmp),
		.checkentry = icmp_checkentry,
		.proto      = IPPROTO_ICMP,
		.family     = NFPROTO_IPV4,
	},
L
Linus Torvalds 已提交
2225 2226
};

2227 2228
static int __net_init ip_tables_net_init(struct net *net)
{
2229
	return xt_proto_init(net, NFPROTO_IPV4);
2230 2231 2232 2233
}

static void __net_exit ip_tables_net_exit(struct net *net)
{
2234
	xt_proto_fini(net, NFPROTO_IPV4);
2235 2236 2237 2238 2239 2240 2241
}

static struct pernet_operations ip_tables_net_ops = {
	.init = ip_tables_net_init,
	.exit = ip_tables_net_exit,
};

2242
static int __init ip_tables_init(void)
L
Linus Torvalds 已提交
2243 2244 2245
{
	int ret;

2246
	ret = register_pernet_subsys(&ip_tables_net_ops);
2247 2248
	if (ret < 0)
		goto err1;
2249

L
Lucas De Marchi 已提交
2250
	/* No one else will be downing sem now, so we won't sleep */
2251
	ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2252 2253
	if (ret < 0)
		goto err2;
2254
	ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2255 2256
	if (ret < 0)
		goto err4;
L
Linus Torvalds 已提交
2257 2258 2259

	/* Register setsockopt */
	ret = nf_register_sockopt(&ipt_sockopts);
2260 2261
	if (ret < 0)
		goto err5;
L
Linus Torvalds 已提交
2262

2263
	pr_info("(C) 2000-2006 Netfilter Core Team\n");
L
Linus Torvalds 已提交
2264
	return 0;
2265 2266

err5:
2267
	xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2268
err4:
2269
	xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2270
err2:
2271
	unregister_pernet_subsys(&ip_tables_net_ops);
2272 2273
err1:
	return ret;
L
Linus Torvalds 已提交
2274 2275
}

2276
static void __exit ip_tables_fini(void)
L
Linus Torvalds 已提交
2277 2278
{
	nf_unregister_sockopt(&ipt_sockopts);
2279

2280 2281
	xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
	xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2282
	unregister_pernet_subsys(&ip_tables_net_ops);
L
Linus Torvalds 已提交
2283 2284 2285 2286 2287
}

EXPORT_SYMBOL(ipt_register_table);
EXPORT_SYMBOL(ipt_unregister_table);
EXPORT_SYMBOL(ipt_do_table);
2288 2289
module_init(ip_tables_init);
module_exit(ip_tables_fini);