Linux Audio

Check our new training course

Loading...
v4.10.11
 
   1/*
   2 * x_tables core - Backend for {ip,ip6,arp}_tables
   3 *
   4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
   5 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
   6 *
   7 * Based on existing ip_tables code which is
   8 *   Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
   9 *   Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License version 2 as
  13 * published by the Free Software Foundation.
  14 *
  15 */
  16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17#include <linux/kernel.h>
  18#include <linux/module.h>
  19#include <linux/socket.h>
  20#include <linux/net.h>
  21#include <linux/proc_fs.h>
  22#include <linux/seq_file.h>
  23#include <linux/string.h>
  24#include <linux/vmalloc.h>
  25#include <linux/mutex.h>
  26#include <linux/mm.h>
  27#include <linux/slab.h>
  28#include <linux/audit.h>
  29#include <linux/user_namespace.h>
  30#include <net/net_namespace.h>
 
  31
  32#include <linux/netfilter/x_tables.h>
  33#include <linux/netfilter_arp.h>
  34#include <linux/netfilter_ipv4/ip_tables.h>
  35#include <linux/netfilter_ipv6/ip6_tables.h>
  36#include <linux/netfilter_arp/arp_tables.h>
  37
  38MODULE_LICENSE("GPL");
  39MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  40MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
  41
  42#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
  43#define XT_PCPU_BLOCK_SIZE 4096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  44
  45struct compat_delta {
  46	unsigned int offset; /* offset in kernel */
  47	int delta; /* delta in 32bit user land */
  48};
  49
  50struct xt_af {
  51	struct mutex mutex;
  52	struct list_head match;
  53	struct list_head target;
  54#ifdef CONFIG_COMPAT
  55	struct mutex compat_mutex;
  56	struct compat_delta *compat_tab;
  57	unsigned int number; /* number of slots in compat_tab[] */
  58	unsigned int cur; /* number of used slots in compat_tab[] */
  59#endif
  60};
  61
  62static struct xt_af *xt;
 
  63
  64static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
  65	[NFPROTO_UNSPEC] = "x",
  66	[NFPROTO_IPV4]   = "ip",
  67	[NFPROTO_ARP]    = "arp",
  68	[NFPROTO_BRIDGE] = "eb",
  69	[NFPROTO_IPV6]   = "ip6",
  70};
  71
  72/* Registration hooks for targets. */
  73int xt_register_target(struct xt_target *target)
  74{
  75	u_int8_t af = target->family;
  76
  77	mutex_lock(&xt[af].mutex);
  78	list_add(&target->list, &xt[af].target);
  79	mutex_unlock(&xt[af].mutex);
  80	return 0;
  81}
  82EXPORT_SYMBOL(xt_register_target);
  83
  84void
  85xt_unregister_target(struct xt_target *target)
  86{
  87	u_int8_t af = target->family;
  88
  89	mutex_lock(&xt[af].mutex);
  90	list_del(&target->list);
  91	mutex_unlock(&xt[af].mutex);
  92}
  93EXPORT_SYMBOL(xt_unregister_target);
  94
  95int
  96xt_register_targets(struct xt_target *target, unsigned int n)
  97{
  98	unsigned int i;
  99	int err = 0;
 100
 101	for (i = 0; i < n; i++) {
 102		err = xt_register_target(&target[i]);
 103		if (err)
 104			goto err;
 105	}
 106	return err;
 107
 108err:
 109	if (i > 0)
 110		xt_unregister_targets(target, i);
 111	return err;
 112}
 113EXPORT_SYMBOL(xt_register_targets);
 114
 115void
 116xt_unregister_targets(struct xt_target *target, unsigned int n)
 117{
 118	while (n-- > 0)
 119		xt_unregister_target(&target[n]);
 120}
 121EXPORT_SYMBOL(xt_unregister_targets);
 122
 123int xt_register_match(struct xt_match *match)
 124{
 125	u_int8_t af = match->family;
 126
 127	mutex_lock(&xt[af].mutex);
 128	list_add(&match->list, &xt[af].match);
 129	mutex_unlock(&xt[af].mutex);
 130	return 0;
 131}
 132EXPORT_SYMBOL(xt_register_match);
 133
 134void
 135xt_unregister_match(struct xt_match *match)
 136{
 137	u_int8_t af = match->family;
 138
 139	mutex_lock(&xt[af].mutex);
 140	list_del(&match->list);
 141	mutex_unlock(&xt[af].mutex);
 142}
 143EXPORT_SYMBOL(xt_unregister_match);
 144
 145int
 146xt_register_matches(struct xt_match *match, unsigned int n)
 147{
 148	unsigned int i;
 149	int err = 0;
 150
 151	for (i = 0; i < n; i++) {
 152		err = xt_register_match(&match[i]);
 153		if (err)
 154			goto err;
 155	}
 156	return err;
 157
 158err:
 159	if (i > 0)
 160		xt_unregister_matches(match, i);
 161	return err;
 162}
 163EXPORT_SYMBOL(xt_register_matches);
 164
 165void
 166xt_unregister_matches(struct xt_match *match, unsigned int n)
 167{
 168	while (n-- > 0)
 169		xt_unregister_match(&match[n]);
 170}
 171EXPORT_SYMBOL(xt_unregister_matches);
 172
 173
 174/*
 175 * These are weird, but module loading must not be done with mutex
 176 * held (since they will register), and we have to have a single
 177 * function to use.
 178 */
 179
 180/* Find match, grabs ref.  Returns ERR_PTR() on error. */
 181struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
 182{
 183	struct xt_match *m;
 184	int err = -ENOENT;
 185
 
 
 
 186	mutex_lock(&xt[af].mutex);
 187	list_for_each_entry(m, &xt[af].match, list) {
 188		if (strcmp(m->name, name) == 0) {
 189			if (m->revision == revision) {
 190				if (try_module_get(m->me)) {
 191					mutex_unlock(&xt[af].mutex);
 192					return m;
 193				}
 194			} else
 195				err = -EPROTOTYPE; /* Found something. */
 196		}
 197	}
 198	mutex_unlock(&xt[af].mutex);
 199
 200	if (af != NFPROTO_UNSPEC)
 201		/* Try searching again in the family-independent list */
 202		return xt_find_match(NFPROTO_UNSPEC, name, revision);
 203
 204	return ERR_PTR(err);
 205}
 206EXPORT_SYMBOL(xt_find_match);
 207
 208struct xt_match *
 209xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
 210{
 211	struct xt_match *match;
 212
 
 
 
 213	match = xt_find_match(nfproto, name, revision);
 214	if (IS_ERR(match)) {
 215		request_module("%st_%s", xt_prefix[nfproto], name);
 216		match = xt_find_match(nfproto, name, revision);
 217	}
 218
 219	return match;
 220}
 221EXPORT_SYMBOL_GPL(xt_request_find_match);
 222
 223/* Find target, grabs ref.  Returns ERR_PTR() on error. */
 224struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
 225{
 226	struct xt_target *t;
 227	int err = -ENOENT;
 228
 
 
 
 229	mutex_lock(&xt[af].mutex);
 230	list_for_each_entry(t, &xt[af].target, list) {
 231		if (strcmp(t->name, name) == 0) {
 232			if (t->revision == revision) {
 233				if (try_module_get(t->me)) {
 234					mutex_unlock(&xt[af].mutex);
 235					return t;
 236				}
 237			} else
 238				err = -EPROTOTYPE; /* Found something. */
 239		}
 240	}
 241	mutex_unlock(&xt[af].mutex);
 242
 243	if (af != NFPROTO_UNSPEC)
 244		/* Try searching again in the family-independent list */
 245		return xt_find_target(NFPROTO_UNSPEC, name, revision);
 246
 247	return ERR_PTR(err);
 248}
 249EXPORT_SYMBOL(xt_find_target);
 250
 251struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
 252{
 253	struct xt_target *target;
 254
 
 
 
 255	target = xt_find_target(af, name, revision);
 256	if (IS_ERR(target)) {
 257		request_module("%st_%s", xt_prefix[af], name);
 258		target = xt_find_target(af, name, revision);
 259	}
 260
 261	return target;
 262}
 263EXPORT_SYMBOL_GPL(xt_request_find_target);
 264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 265static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
 266{
 267	const struct xt_match *m;
 268	int have_rev = 0;
 269
 
 270	list_for_each_entry(m, &xt[af].match, list) {
 271		if (strcmp(m->name, name) == 0) {
 272			if (m->revision > *bestp)
 273				*bestp = m->revision;
 274			if (m->revision == revision)
 275				have_rev = 1;
 276		}
 277	}
 
 278
 279	if (af != NFPROTO_UNSPEC && !have_rev)
 280		return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
 281
 282	return have_rev;
 283}
 284
 285static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
 286{
 287	const struct xt_target *t;
 288	int have_rev = 0;
 289
 
 290	list_for_each_entry(t, &xt[af].target, list) {
 291		if (strcmp(t->name, name) == 0) {
 292			if (t->revision > *bestp)
 293				*bestp = t->revision;
 294			if (t->revision == revision)
 295				have_rev = 1;
 296		}
 297	}
 
 298
 299	if (af != NFPROTO_UNSPEC && !have_rev)
 300		return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
 301
 302	return have_rev;
 303}
 304
 305/* Returns true or false (if no such extension at all) */
 306int xt_find_revision(u8 af, const char *name, u8 revision, int target,
 307		     int *err)
 308{
 309	int have_rev, best = -1;
 310
 311	mutex_lock(&xt[af].mutex);
 312	if (target == 1)
 313		have_rev = target_revfn(af, name, revision, &best);
 314	else
 315		have_rev = match_revfn(af, name, revision, &best);
 316	mutex_unlock(&xt[af].mutex);
 317
 318	/* Nothing at all?  Return 0 to try loading module. */
 319	if (best == -1) {
 320		*err = -ENOENT;
 321		return 0;
 322	}
 323
 324	*err = best;
 325	if (!have_rev)
 326		*err = -EPROTONOSUPPORT;
 327	return 1;
 328}
 329EXPORT_SYMBOL_GPL(xt_find_revision);
 330
 331static char *
 332textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
 333{
 334	static const char *const inetbr_names[] = {
 335		"PREROUTING", "INPUT", "FORWARD",
 336		"OUTPUT", "POSTROUTING", "BROUTING",
 337	};
 338	static const char *const arp_names[] = {
 339		"INPUT", "FORWARD", "OUTPUT",
 340	};
 341	const char *const *names;
 342	unsigned int i, max;
 343	char *p = buf;
 344	bool np = false;
 345	int res;
 346
 347	names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
 348	max   = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
 349	                                   ARRAY_SIZE(inetbr_names);
 350	*p = '\0';
 351	for (i = 0; i < max; ++i) {
 352		if (!(mask & (1 << i)))
 353			continue;
 354		res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
 355		if (res > 0) {
 356			size -= res;
 357			p += res;
 358		}
 359		np = true;
 360	}
 361
 362	return buf;
 363}
 364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 365int xt_check_match(struct xt_mtchk_param *par,
 366		   unsigned int size, u_int8_t proto, bool inv_proto)
 367{
 368	int ret;
 369
 370	if (XT_ALIGN(par->match->matchsize) != size &&
 371	    par->match->matchsize != -1) {
 372		/*
 373		 * ebt_among is exempt from centralized matchsize checking
 374		 * because it uses a dynamic-size data set.
 375		 */
 376		pr_err("%s_tables: %s.%u match: invalid size "
 377		       "%u (kernel) != (user) %u\n",
 378		       xt_prefix[par->family], par->match->name,
 379		       par->match->revision,
 380		       XT_ALIGN(par->match->matchsize), size);
 381		return -EINVAL;
 382	}
 383	if (par->match->table != NULL &&
 384	    strcmp(par->match->table, par->table) != 0) {
 385		pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
 386		       xt_prefix[par->family], par->match->name,
 387		       par->match->table, par->table);
 388		return -EINVAL;
 389	}
 390	if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
 391		char used[64], allow[64];
 392
 393		pr_err("%s_tables: %s match: used from hooks %s, but only "
 394		       "valid from %s\n",
 395		       xt_prefix[par->family], par->match->name,
 396		       textify_hooks(used, sizeof(used), par->hook_mask,
 397		                     par->family),
 398		       textify_hooks(allow, sizeof(allow), par->match->hooks,
 399		                     par->family));
 400		return -EINVAL;
 401	}
 402	if (par->match->proto && (par->match->proto != proto || inv_proto)) {
 403		pr_err("%s_tables: %s match: only valid for protocol %u\n",
 404		       xt_prefix[par->family], par->match->name,
 405		       par->match->proto);
 406		return -EINVAL;
 407	}
 408	if (par->match->checkentry != NULL) {
 409		ret = par->match->checkentry(par);
 410		if (ret < 0)
 411			return ret;
 412		else if (ret > 0)
 413			/* Flag up potential errors. */
 414			return -EIO;
 415	}
 416	return 0;
 417}
 418EXPORT_SYMBOL_GPL(xt_check_match);
 419
 420/** xt_check_entry_match - check that matches end before start of target
 421 *
 422 * @match: beginning of xt_entry_match
 423 * @target: beginning of this rules target (alleged end of matches)
 424 * @alignment: alignment requirement of match structures
 425 *
 426 * Validates that all matches add up to the beginning of the target,
 427 * and that each match covers at least the base structure size.
 428 *
 429 * Return: 0 on success, negative errno on failure.
 430 */
 431static int xt_check_entry_match(const char *match, const char *target,
 432				const size_t alignment)
 433{
 434	const struct xt_entry_match *pos;
 435	int length = target - match;
 436
 437	if (length == 0) /* no matches */
 438		return 0;
 439
 440	pos = (struct xt_entry_match *)match;
 441	do {
 442		if ((unsigned long)pos % alignment)
 443			return -EINVAL;
 444
 445		if (length < (int)sizeof(struct xt_entry_match))
 446			return -EINVAL;
 447
 448		if (pos->u.match_size < sizeof(struct xt_entry_match))
 449			return -EINVAL;
 450
 451		if (pos->u.match_size > length)
 452			return -EINVAL;
 453
 454		length -= pos->u.match_size;
 455		pos = ((void *)((char *)(pos) + (pos)->u.match_size));
 456	} while (length > 0);
 457
 458	return 0;
 459}
 460
 461#ifdef CONFIG_COMPAT
 462int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
 
 
 
 
 
 
 
 
 463{
 464	struct xt_af *xp = &xt[af];
 
 
 465
 466	if (!xp->compat_tab) {
 467		if (!xp->number)
 
 
 
 
 
 
 
 
 468			return -EINVAL;
 469		xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
 470		if (!xp->compat_tab)
 471			return -ENOMEM;
 472		xp->cur = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 473	}
 474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 475	if (xp->cur >= xp->number)
 476		return -EINVAL;
 477
 478	if (xp->cur)
 479		delta += xp->compat_tab[xp->cur - 1].delta;
 480	xp->compat_tab[xp->cur].offset = offset;
 481	xp->compat_tab[xp->cur].delta = delta;
 482	xp->cur++;
 483	return 0;
 484}
 485EXPORT_SYMBOL_GPL(xt_compat_add_offset);
 486
 487void xt_compat_flush_offsets(u_int8_t af)
 488{
 
 
 489	if (xt[af].compat_tab) {
 490		vfree(xt[af].compat_tab);
 491		xt[af].compat_tab = NULL;
 492		xt[af].number = 0;
 493		xt[af].cur = 0;
 494	}
 495}
 496EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
 497
 498int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
 499{
 500	struct compat_delta *tmp = xt[af].compat_tab;
 501	int mid, left = 0, right = xt[af].cur - 1;
 502
 503	while (left <= right) {
 504		mid = (left + right) >> 1;
 505		if (offset > tmp[mid].offset)
 506			left = mid + 1;
 507		else if (offset < tmp[mid].offset)
 508			right = mid - 1;
 509		else
 510			return mid ? tmp[mid - 1].delta : 0;
 511	}
 512	return left ? tmp[left - 1].delta : 0;
 513}
 514EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
 515
 516void xt_compat_init_offsets(u_int8_t af, unsigned int number)
 517{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 518	xt[af].number = number;
 519	xt[af].cur = 0;
 
 
 520}
 521EXPORT_SYMBOL(xt_compat_init_offsets);
 522
 523int xt_compat_match_offset(const struct xt_match *match)
 524{
 525	u_int16_t csize = match->compatsize ? : match->matchsize;
 526	return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
 527}
 528EXPORT_SYMBOL_GPL(xt_compat_match_offset);
 529
 530void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
 531			       unsigned int *size)
 532{
 533	const struct xt_match *match = m->u.kernel.match;
 534	struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
 535	int pad, off = xt_compat_match_offset(match);
 536	u_int16_t msize = cm->u.user.match_size;
 537	char name[sizeof(m->u.user.name)];
 538
 539	m = *dstptr;
 540	memcpy(m, cm, sizeof(*cm));
 541	if (match->compat_from_user)
 542		match->compat_from_user(m->data, cm->data);
 543	else
 544		memcpy(m->data, cm->data, msize - sizeof(*cm));
 545	pad = XT_ALIGN(match->matchsize) - match->matchsize;
 546	if (pad > 0)
 547		memset(m->data + match->matchsize, 0, pad);
 548
 549	msize += off;
 550	m->u.user.match_size = msize;
 551	strlcpy(name, match->name, sizeof(name));
 552	module_put(match->me);
 553	strncpy(m->u.user.name, name, sizeof(m->u.user.name));
 554
 555	*size += off;
 556	*dstptr += msize;
 557}
 558EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
 559
 
 
 
 
 
 
 560int xt_compat_match_to_user(const struct xt_entry_match *m,
 561			    void __user **dstptr, unsigned int *size)
 562{
 563	const struct xt_match *match = m->u.kernel.match;
 564	struct compat_xt_entry_match __user *cm = *dstptr;
 565	int off = xt_compat_match_offset(match);
 566	u_int16_t msize = m->u.user.match_size - off;
 567
 568	if (copy_to_user(cm, m, sizeof(*cm)) ||
 569	    put_user(msize, &cm->u.user.match_size) ||
 570	    copy_to_user(cm->u.user.name, m->u.kernel.match->name,
 571			 strlen(m->u.kernel.match->name) + 1))
 572		return -EFAULT;
 573
 574	if (match->compat_to_user) {
 575		if (match->compat_to_user((void __user *)cm->data, m->data))
 576			return -EFAULT;
 577	} else {
 578		if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
 579			return -EFAULT;
 580	}
 581
 582	*size -= off;
 583	*dstptr += msize;
 584	return 0;
 585}
 586EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
 587
 588/* non-compat version may have padding after verdict */
 589struct compat_xt_standard_target {
 590	struct compat_xt_entry_target t;
 591	compat_uint_t verdict;
 592};
 593
 
 
 
 
 
 594int xt_compat_check_entry_offsets(const void *base, const char *elems,
 595				  unsigned int target_offset,
 596				  unsigned int next_offset)
 597{
 598	long size_of_base_struct = elems - (const char *)base;
 599	const struct compat_xt_entry_target *t;
 600	const char *e = base;
 601
 602	if (target_offset < size_of_base_struct)
 603		return -EINVAL;
 604
 605	if (target_offset + sizeof(*t) > next_offset)
 606		return -EINVAL;
 607
 608	t = (void *)(e + target_offset);
 609	if (t->u.target_size < sizeof(*t))
 610		return -EINVAL;
 611
 612	if (target_offset + t->u.target_size > next_offset)
 613		return -EINVAL;
 614
 615	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
 616	    COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
 617		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 618
 619	/* compat_xt_entry match has less strict aligment requirements,
 620	 * otherwise they are identical.  In case of padding differences
 621	 * we need to add compat version of xt_check_entry_match.
 622	 */
 623	BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
 624
 625	return xt_check_entry_match(elems, base + target_offset,
 626				    __alignof__(struct compat_xt_entry_match));
 627}
 628EXPORT_SYMBOL(xt_compat_check_entry_offsets);
 629#endif /* CONFIG_COMPAT */
 630
 631/**
 632 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
 633 *
 634 * @base: pointer to arp/ip/ip6t_entry
 635 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
 636 * @target_offset: the arp/ip/ip6_t->target_offset
 637 * @next_offset: the arp/ip/ip6_t->next_offset
 638 *
 639 * validates that target_offset and next_offset are sane and that all
 640 * match sizes (if any) align with the target offset.
 641 *
 642 * This function does not validate the targets or matches themselves, it
 643 * only tests that all the offsets and sizes are correct, that all
 644 * match structures are aligned, and that the last structure ends where
 645 * the target structure begins.
 646 *
 647 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
 648 *
 649 * The arp/ip/ip6t_entry structure @base must have passed following tests:
 650 * - it must point to a valid memory location
 651 * - base to base + next_offset must be accessible, i.e. not exceed allocated
 652 *   length.
 653 *
 654 * A well-formed entry looks like this:
 655 *
 656 * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
 657 * e->elems[]-----'                              |               |
 658 *                matchsize                      |               |
 659 *                                matchsize      |               |
 660 *                                               |               |
 661 * target_offset---------------------------------'               |
 662 * next_offset---------------------------------------------------'
 663 *
 664 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
 665 *          This is where matches (if any) and the target reside.
 666 * target_offset: beginning of target.
 667 * next_offset: start of the next rule; also: size of this rule.
 668 * Since targets have a minimum size, target_offset + minlen <= next_offset.
 669 *
 670 * Every match stores its size, sum of sizes must not exceed target_offset.
 671 *
 672 * Return: 0 on success, negative errno on failure.
 673 */
 674int xt_check_entry_offsets(const void *base,
 675			   const char *elems,
 676			   unsigned int target_offset,
 677			   unsigned int next_offset)
 678{
 679	long size_of_base_struct = elems - (const char *)base;
 680	const struct xt_entry_target *t;
 681	const char *e = base;
 682
 683	/* target start is within the ip/ip6/arpt_entry struct */
 684	if (target_offset < size_of_base_struct)
 685		return -EINVAL;
 686
 687	if (target_offset + sizeof(*t) > next_offset)
 688		return -EINVAL;
 689
 690	t = (void *)(e + target_offset);
 691	if (t->u.target_size < sizeof(*t))
 692		return -EINVAL;
 693
 694	if (target_offset + t->u.target_size > next_offset)
 695		return -EINVAL;
 696
 697	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
 698	    XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
 699		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 700
 701	return xt_check_entry_match(elems, base + target_offset,
 702				    __alignof__(struct xt_entry_match));
 703}
 704EXPORT_SYMBOL(xt_check_entry_offsets);
 705
 706/**
 707 * xt_alloc_entry_offsets - allocate array to store rule head offsets
 708 *
 709 * @size: number of entries
 710 *
 711 * Return: NULL or kmalloc'd or vmalloc'd array
 712 */
 713unsigned int *xt_alloc_entry_offsets(unsigned int size)
 714{
 715	unsigned int *off;
 716
 717	off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
 718
 719	if (off)
 720		return off;
 721
 722	if (size < (SIZE_MAX / sizeof(unsigned int)))
 723		off = vmalloc(size * sizeof(unsigned int));
 724
 725	return off;
 726}
 727EXPORT_SYMBOL(xt_alloc_entry_offsets);
 728
 729/**
 730 * xt_find_jump_offset - check if target is a valid jump offset
 731 *
 732 * @offsets: array containing all valid rule start offsets of a rule blob
 733 * @target: the jump target to search for
 734 * @size: entries in @offset
 735 */
 736bool xt_find_jump_offset(const unsigned int *offsets,
 737			 unsigned int target, unsigned int size)
 738{
 739	int m, low = 0, hi = size;
 740
 741	while (hi > low) {
 742		m = (low + hi) / 2u;
 743
 744		if (offsets[m] > target)
 745			hi = m;
 746		else if (offsets[m] < target)
 747			low = m + 1;
 748		else
 749			return true;
 750	}
 751
 752	return false;
 753}
 754EXPORT_SYMBOL(xt_find_jump_offset);
 755
 756int xt_check_target(struct xt_tgchk_param *par,
 757		    unsigned int size, u_int8_t proto, bool inv_proto)
 758{
 759	int ret;
 760
 761	if (XT_ALIGN(par->target->targetsize) != size) {
 762		pr_err("%s_tables: %s.%u target: invalid size "
 763		       "%u (kernel) != (user) %u\n",
 764		       xt_prefix[par->family], par->target->name,
 765		       par->target->revision,
 766		       XT_ALIGN(par->target->targetsize), size);
 767		return -EINVAL;
 768	}
 769	if (par->target->table != NULL &&
 770	    strcmp(par->target->table, par->table) != 0) {
 771		pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
 772		       xt_prefix[par->family], par->target->name,
 773		       par->target->table, par->table);
 774		return -EINVAL;
 775	}
 776	if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
 777		char used[64], allow[64];
 778
 779		pr_err("%s_tables: %s target: used from hooks %s, but only "
 780		       "usable from %s\n",
 781		       xt_prefix[par->family], par->target->name,
 782		       textify_hooks(used, sizeof(used), par->hook_mask,
 783		                     par->family),
 784		       textify_hooks(allow, sizeof(allow), par->target->hooks,
 785		                     par->family));
 786		return -EINVAL;
 787	}
 788	if (par->target->proto && (par->target->proto != proto || inv_proto)) {
 789		pr_err("%s_tables: %s target: only valid for protocol %u\n",
 790		       xt_prefix[par->family], par->target->name,
 791		       par->target->proto);
 792		return -EINVAL;
 793	}
 794	if (par->target->checkentry != NULL) {
 795		ret = par->target->checkentry(par);
 796		if (ret < 0)
 797			return ret;
 798		else if (ret > 0)
 799			/* Flag up potential errors. */
 800			return -EIO;
 801	}
 802	return 0;
 803}
 804EXPORT_SYMBOL_GPL(xt_check_target);
 805
 806/**
 807 * xt_copy_counters_from_user - copy counters and metadata from userspace
 808 *
 809 * @user: src pointer to userspace memory
 810 * @len: alleged size of userspace memory
 811 * @info: where to store the xt_counters_info metadata
 812 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
 813 *
 814 * Copies counter meta data from @user and stores it in @info.
 815 *
 816 * vmallocs memory to hold the counters, then copies the counter data
 817 * from @user to the new memory and returns a pointer to it.
 818 *
 819 * If @compat is true, @info gets converted automatically to the 64bit
 820 * representation.
 821 *
 822 * The metadata associated with the counters is stored in @info.
 823 *
 824 * Return: returns pointer that caller has to test via IS_ERR().
 825 * If IS_ERR is false, caller has to vfree the pointer.
 826 */
 827void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
 828				 struct xt_counters_info *info, bool compat)
 829{
 
 830	void *mem;
 831	u64 size;
 832
 833#ifdef CONFIG_COMPAT
 834	if (compat) {
 835		/* structures only differ in size due to alignment */
 836		struct compat_xt_counters_info compat_tmp;
 837
 838		if (len <= sizeof(compat_tmp))
 839			return ERR_PTR(-EINVAL);
 840
 841		len -= sizeof(compat_tmp);
 842		if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
 843			return ERR_PTR(-EFAULT);
 844
 845		strlcpy(info->name, compat_tmp.name, sizeof(info->name));
 846		info->num_counters = compat_tmp.num_counters;
 847		user += sizeof(compat_tmp);
 848	} else
 849#endif
 850	{
 851		if (len <= sizeof(*info))
 852			return ERR_PTR(-EINVAL);
 853
 854		len -= sizeof(*info);
 855		if (copy_from_user(info, user, sizeof(*info)) != 0)
 856			return ERR_PTR(-EFAULT);
 857
 858		info->name[sizeof(info->name) - 1] = '\0';
 859		user += sizeof(*info);
 860	}
 
 861
 862	size = sizeof(struct xt_counters);
 863	size *= info->num_counters;
 864
 865	if (size != (u64)len)
 866		return ERR_PTR(-EINVAL);
 867
 868	mem = vmalloc(len);
 869	if (!mem)
 870		return ERR_PTR(-ENOMEM);
 871
 872	if (copy_from_user(mem, user, len) == 0)
 873		return mem;
 874
 875	vfree(mem);
 876	return ERR_PTR(-EFAULT);
 877}
 878EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
 879
 880#ifdef CONFIG_COMPAT
 881int xt_compat_target_offset(const struct xt_target *target)
 882{
 883	u_int16_t csize = target->compatsize ? : target->targetsize;
 884	return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
 885}
 886EXPORT_SYMBOL_GPL(xt_compat_target_offset);
 887
 888void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
 889				unsigned int *size)
 890{
 891	const struct xt_target *target = t->u.kernel.target;
 892	struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
 893	int pad, off = xt_compat_target_offset(target);
 894	u_int16_t tsize = ct->u.user.target_size;
 895	char name[sizeof(t->u.user.name)];
 896
 897	t = *dstptr;
 898	memcpy(t, ct, sizeof(*ct));
 899	if (target->compat_from_user)
 900		target->compat_from_user(t->data, ct->data);
 901	else
 902		memcpy(t->data, ct->data, tsize - sizeof(*ct));
 903	pad = XT_ALIGN(target->targetsize) - target->targetsize;
 904	if (pad > 0)
 905		memset(t->data + target->targetsize, 0, pad);
 906
 907	tsize += off;
 908	t->u.user.target_size = tsize;
 909	strlcpy(name, target->name, sizeof(name));
 910	module_put(target->me);
 911	strncpy(t->u.user.name, name, sizeof(t->u.user.name));
 912
 913	*size += off;
 914	*dstptr += tsize;
 915}
 916EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
 917
 918int xt_compat_target_to_user(const struct xt_entry_target *t,
 919			     void __user **dstptr, unsigned int *size)
 920{
 921	const struct xt_target *target = t->u.kernel.target;
 922	struct compat_xt_entry_target __user *ct = *dstptr;
 923	int off = xt_compat_target_offset(target);
 924	u_int16_t tsize = t->u.user.target_size - off;
 925
 926	if (copy_to_user(ct, t, sizeof(*ct)) ||
 927	    put_user(tsize, &ct->u.user.target_size) ||
 928	    copy_to_user(ct->u.user.name, t->u.kernel.target->name,
 929			 strlen(t->u.kernel.target->name) + 1))
 930		return -EFAULT;
 931
 932	if (target->compat_to_user) {
 933		if (target->compat_to_user((void __user *)ct->data, t->data))
 934			return -EFAULT;
 935	} else {
 936		if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
 937			return -EFAULT;
 938	}
 939
 940	*size -= off;
 941	*dstptr += tsize;
 942	return 0;
 943}
 944EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
 945#endif
 946
 947struct xt_table_info *xt_alloc_table_info(unsigned int size)
 948{
 949	struct xt_table_info *info = NULL;
 950	size_t sz = sizeof(*info) + size;
 951
 952	if (sz < sizeof(*info))
 953		return NULL;
 954
 955	/* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
 956	if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
 957		return NULL;
 958
 959	if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
 960		info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
 961	if (!info) {
 962		info = __vmalloc(sz, GFP_KERNEL | __GFP_NOWARN |
 963				     __GFP_NORETRY | __GFP_HIGHMEM,
 964				 PAGE_KERNEL);
 965		if (!info)
 966			return NULL;
 967	}
 968	memset(info, 0, sizeof(*info));
 969	info->size = size;
 970	return info;
 971}
 972EXPORT_SYMBOL(xt_alloc_table_info);
 973
 974void xt_free_table_info(struct xt_table_info *info)
 975{
 976	int cpu;
 977
 978	if (info->jumpstack != NULL) {
 979		for_each_possible_cpu(cpu)
 980			kvfree(info->jumpstack[cpu]);
 981		kvfree(info->jumpstack);
 982	}
 983
 984	kvfree(info);
 985}
 986EXPORT_SYMBOL(xt_free_table_info);
 987
 988/* Find table by name, grabs mutex & ref.  Returns NULL on error. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 989struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
 990				    const char *name)
 991{
 992	struct xt_table *t, *found = NULL;
 
 
 
 993
 994	mutex_lock(&xt[af].mutex);
 995	list_for_each_entry(t, &net->xt.tables[af], list)
 996		if (strcmp(t->name, name) == 0 && try_module_get(t->me))
 997			return t;
 998
 999	if (net == &init_net)
1000		goto out;
 
1001
1002	/* Table doesn't exist in this netns, re-try init */
1003	list_for_each_entry(t, &init_net.xt.tables[af], list) {
1004		if (strcmp(t->name, name))
1005			continue;
1006		if (!try_module_get(t->me))
1007			return NULL;
 
 
1008
1009		mutex_unlock(&xt[af].mutex);
1010		if (t->table_init(net) != 0) {
1011			module_put(t->me);
1012			return NULL;
 
1013		}
1014
1015		found = t;
1016
1017		mutex_lock(&xt[af].mutex);
1018		break;
1019	}
1020
1021	if (!found)
1022		goto out;
1023
1024	/* and once again: */
1025	list_for_each_entry(t, &net->xt.tables[af], list)
1026		if (strcmp(t->name, name) == 0)
1027			return t;
1028
1029	module_put(found->me);
1030 out:
1031	mutex_unlock(&xt[af].mutex);
1032	return NULL;
1033}
1034EXPORT_SYMBOL_GPL(xt_find_table_lock);
1035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1036void xt_table_unlock(struct xt_table *table)
1037{
1038	mutex_unlock(&xt[table->af].mutex);
1039}
1040EXPORT_SYMBOL_GPL(xt_table_unlock);
1041
1042#ifdef CONFIG_COMPAT
1043void xt_compat_lock(u_int8_t af)
1044{
1045	mutex_lock(&xt[af].compat_mutex);
1046}
1047EXPORT_SYMBOL_GPL(xt_compat_lock);
1048
1049void xt_compat_unlock(u_int8_t af)
1050{
1051	mutex_unlock(&xt[af].compat_mutex);
1052}
1053EXPORT_SYMBOL_GPL(xt_compat_unlock);
1054#endif
1055
1056DEFINE_PER_CPU(seqcount_t, xt_recseq);
1057EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1058
1059struct static_key xt_tee_enabled __read_mostly;
1060EXPORT_SYMBOL_GPL(xt_tee_enabled);
1061
1062static int xt_jumpstack_alloc(struct xt_table_info *i)
1063{
1064	unsigned int size;
1065	int cpu;
1066
1067	size = sizeof(void **) * nr_cpu_ids;
1068	if (size > PAGE_SIZE)
1069		i->jumpstack = vzalloc(size);
1070	else
1071		i->jumpstack = kzalloc(size, GFP_KERNEL);
1072	if (i->jumpstack == NULL)
1073		return -ENOMEM;
1074
1075	/* ruleset without jumps -- no stack needed */
1076	if (i->stacksize == 0)
1077		return 0;
1078
1079	/* Jumpstack needs to be able to record two full callchains, one
1080	 * from the first rule set traversal, plus one table reentrancy
1081	 * via -j TEE without clobbering the callchain that brought us to
1082	 * TEE target.
1083	 *
1084	 * This is done by allocating two jumpstacks per cpu, on reentry
1085	 * the upper half of the stack is used.
1086	 *
1087	 * see the jumpstack setup in ipt_do_table() for more details.
1088	 */
1089	size = sizeof(void *) * i->stacksize * 2u;
1090	for_each_possible_cpu(cpu) {
1091		if (size > PAGE_SIZE)
1092			i->jumpstack[cpu] = vmalloc_node(size,
1093				cpu_to_node(cpu));
1094		else
1095			i->jumpstack[cpu] = kmalloc_node(size,
1096				GFP_KERNEL, cpu_to_node(cpu));
1097		if (i->jumpstack[cpu] == NULL)
1098			/*
1099			 * Freeing will be done later on by the callers. The
1100			 * chain is: xt_replace_table -> __do_replace ->
1101			 * do_replace -> xt_free_table_info.
1102			 */
1103			return -ENOMEM;
1104	}
1105
1106	return 0;
1107}
1108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1109struct xt_table_info *
1110xt_replace_table(struct xt_table *table,
1111	      unsigned int num_counters,
1112	      struct xt_table_info *newinfo,
1113	      int *error)
1114{
1115	struct xt_table_info *private;
 
1116	int ret;
1117
1118	ret = xt_jumpstack_alloc(newinfo);
1119	if (ret < 0) {
1120		*error = ret;
1121		return NULL;
1122	}
1123
1124	/* Do the substitution. */
1125	local_bh_disable();
1126	private = table->private;
1127
1128	/* Check inside lock: is the old number correct? */
1129	if (num_counters != private->number) {
1130		pr_debug("num_counters != table->private->number (%u/%u)\n",
1131			 num_counters, private->number);
1132		local_bh_enable();
1133		*error = -EAGAIN;
1134		return NULL;
1135	}
1136
1137	newinfo->initial_entries = private->initial_entries;
1138	/*
1139	 * Ensure contents of newinfo are visible before assigning to
1140	 * private.
1141	 */
1142	smp_wmb();
1143	table->private = newinfo;
1144
 
 
 
1145	/*
1146	 * Even though table entries have now been swapped, other CPU's
1147	 * may still be using the old entries. This is okay, because
1148	 * resynchronization happens because of the locking done
1149	 * during the get_counters() routine.
1150	 */
1151	local_bh_enable();
1152
1153#ifdef CONFIG_AUDIT
1154	if (audit_enabled) {
1155		struct audit_buffer *ab;
1156
1157		ab = audit_log_start(current->audit_context, GFP_KERNEL,
1158				     AUDIT_NETFILTER_CFG);
1159		if (ab) {
1160			audit_log_format(ab, "table=%s family=%u entries=%u",
1161					 table->name, table->af,
1162					 private->number);
1163			audit_log_end(ab);
1164		}
1165	}
1166#endif
1167
 
 
 
 
1168	return private;
1169}
1170EXPORT_SYMBOL_GPL(xt_replace_table);
1171
1172struct xt_table *xt_register_table(struct net *net,
1173				   const struct xt_table *input_table,
1174				   struct xt_table_info *bootstrap,
1175				   struct xt_table_info *newinfo)
1176{
1177	int ret;
1178	struct xt_table_info *private;
1179	struct xt_table *t, *table;
 
1180
1181	/* Don't add one object to multiple lists. */
1182	table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1183	if (!table) {
1184		ret = -ENOMEM;
1185		goto out;
1186	}
1187
1188	mutex_lock(&xt[table->af].mutex);
1189	/* Don't autoload: we'd eat our tail... */
1190	list_for_each_entry(t, &net->xt.tables[table->af], list) {
1191		if (strcmp(t->name, table->name) == 0) {
1192			ret = -EEXIST;
1193			goto unlock;
1194		}
1195	}
1196
1197	/* Simplifies replace_table code. */
1198	table->private = bootstrap;
1199
1200	if (!xt_replace_table(table, 0, newinfo, &ret))
1201		goto unlock;
1202
1203	private = table->private;
1204	pr_debug("table->private->number = %u\n", private->number);
1205
1206	/* save number of initial entries */
1207	private->initial_entries = private->number;
1208
1209	list_add(&table->list, &net->xt.tables[table->af]);
1210	mutex_unlock(&xt[table->af].mutex);
1211	return table;
1212
1213unlock:
1214	mutex_unlock(&xt[table->af].mutex);
1215	kfree(table);
1216out:
1217	return ERR_PTR(ret);
1218}
1219EXPORT_SYMBOL_GPL(xt_register_table);
1220
1221void *xt_unregister_table(struct xt_table *table)
1222{
1223	struct xt_table_info *private;
1224
1225	mutex_lock(&xt[table->af].mutex);
1226	private = table->private;
1227	list_del(&table->list);
1228	mutex_unlock(&xt[table->af].mutex);
 
 
 
1229	kfree(table);
1230
1231	return private;
1232}
1233EXPORT_SYMBOL_GPL(xt_unregister_table);
1234
1235#ifdef CONFIG_PROC_FS
1236struct xt_names_priv {
1237	struct seq_net_private p;
1238	u_int8_t af;
1239};
1240static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1241{
1242	struct xt_names_priv *priv = seq->private;
1243	struct net *net = seq_file_net(seq);
1244	u_int8_t af = priv->af;
 
 
1245
1246	mutex_lock(&xt[af].mutex);
1247	return seq_list_start(&net->xt.tables[af], *pos);
1248}
1249
1250static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1251{
1252	struct xt_names_priv *priv = seq->private;
1253	struct net *net = seq_file_net(seq);
1254	u_int8_t af = priv->af;
1255
1256	return seq_list_next(v, &net->xt.tables[af], pos);
 
 
1257}
1258
1259static void xt_table_seq_stop(struct seq_file *seq, void *v)
1260{
1261	struct xt_names_priv *priv = seq->private;
1262	u_int8_t af = priv->af;
1263
1264	mutex_unlock(&xt[af].mutex);
1265}
1266
1267static int xt_table_seq_show(struct seq_file *seq, void *v)
1268{
1269	struct xt_table *table = list_entry(v, struct xt_table, list);
1270
1271	if (*table->name)
1272		seq_printf(seq, "%s\n", table->name);
1273	return 0;
1274}
1275
1276static const struct seq_operations xt_table_seq_ops = {
1277	.start	= xt_table_seq_start,
1278	.next	= xt_table_seq_next,
1279	.stop	= xt_table_seq_stop,
1280	.show	= xt_table_seq_show,
1281};
1282
1283static int xt_table_open(struct inode *inode, struct file *file)
1284{
1285	int ret;
1286	struct xt_names_priv *priv;
1287
1288	ret = seq_open_net(inode, file, &xt_table_seq_ops,
1289			   sizeof(struct xt_names_priv));
1290	if (!ret) {
1291		priv = ((struct seq_file *)file->private_data)->private;
1292		priv->af = (unsigned long)PDE_DATA(inode);
1293	}
1294	return ret;
1295}
1296
1297static const struct file_operations xt_table_ops = {
1298	.owner	 = THIS_MODULE,
1299	.open	 = xt_table_open,
1300	.read	 = seq_read,
1301	.llseek	 = seq_lseek,
1302	.release = seq_release_net,
1303};
1304
1305/*
1306 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1307 * the multi-AF mutexes.
1308 */
1309struct nf_mttg_trav {
1310	struct list_head *head, *curr;
1311	uint8_t class, nfproto;
1312};
1313
1314enum {
1315	MTTG_TRAV_INIT,
1316	MTTG_TRAV_NFP_UNSPEC,
1317	MTTG_TRAV_NFP_SPEC,
1318	MTTG_TRAV_DONE,
1319};
1320
1321static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1322    bool is_target)
1323{
1324	static const uint8_t next_class[] = {
1325		[MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1326		[MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1327	};
 
1328	struct nf_mttg_trav *trav = seq->private;
1329
 
 
 
1330	switch (trav->class) {
1331	case MTTG_TRAV_INIT:
1332		trav->class = MTTG_TRAV_NFP_UNSPEC;
1333		mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1334		trav->head = trav->curr = is_target ?
1335			&xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1336 		break;
1337	case MTTG_TRAV_NFP_UNSPEC:
1338		trav->curr = trav->curr->next;
1339		if (trav->curr != trav->head)
1340			break;
1341		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1342		mutex_lock(&xt[trav->nfproto].mutex);
1343		trav->head = trav->curr = is_target ?
1344			&xt[trav->nfproto].target : &xt[trav->nfproto].match;
1345		trav->class = next_class[trav->class];
1346		break;
1347	case MTTG_TRAV_NFP_SPEC:
1348		trav->curr = trav->curr->next;
1349		if (trav->curr != trav->head)
1350			break;
1351		/* fallthru, _stop will unlock */
1352	default:
1353		return NULL;
1354	}
1355
1356	if (ppos != NULL)
1357		++*ppos;
1358	return trav;
1359}
1360
1361static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1362    bool is_target)
1363{
1364	struct nf_mttg_trav *trav = seq->private;
1365	unsigned int j;
1366
1367	trav->class = MTTG_TRAV_INIT;
1368	for (j = 0; j < *pos; ++j)
1369		if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1370			return NULL;
1371	return trav;
1372}
1373
1374static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1375{
 
1376	struct nf_mttg_trav *trav = seq->private;
1377
1378	switch (trav->class) {
1379	case MTTG_TRAV_NFP_UNSPEC:
1380		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1381		break;
1382	case MTTG_TRAV_NFP_SPEC:
1383		mutex_unlock(&xt[trav->nfproto].mutex);
1384		break;
1385	}
1386}
1387
1388static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1389{
1390	return xt_mttg_seq_start(seq, pos, false);
1391}
1392
1393static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1394{
1395	return xt_mttg_seq_next(seq, v, ppos, false);
1396}
1397
1398static int xt_match_seq_show(struct seq_file *seq, void *v)
1399{
1400	const struct nf_mttg_trav *trav = seq->private;
1401	const struct xt_match *match;
1402
1403	switch (trav->class) {
1404	case MTTG_TRAV_NFP_UNSPEC:
1405	case MTTG_TRAV_NFP_SPEC:
1406		if (trav->curr == trav->head)
1407			return 0;
1408		match = list_entry(trav->curr, struct xt_match, list);
1409		if (*match->name)
1410			seq_printf(seq, "%s\n", match->name);
1411	}
1412	return 0;
1413}
1414
1415static const struct seq_operations xt_match_seq_ops = {
1416	.start	= xt_match_seq_start,
1417	.next	= xt_match_seq_next,
1418	.stop	= xt_mttg_seq_stop,
1419	.show	= xt_match_seq_show,
1420};
1421
1422static int xt_match_open(struct inode *inode, struct file *file)
1423{
1424	struct nf_mttg_trav *trav;
1425	trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1426	if (!trav)
1427		return -ENOMEM;
1428
1429	trav->nfproto = (unsigned long)PDE_DATA(inode);
1430	return 0;
1431}
1432
1433static const struct file_operations xt_match_ops = {
1434	.owner	 = THIS_MODULE,
1435	.open	 = xt_match_open,
1436	.read	 = seq_read,
1437	.llseek	 = seq_lseek,
1438	.release = seq_release_private,
1439};
1440
1441static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1442{
1443	return xt_mttg_seq_start(seq, pos, true);
1444}
1445
1446static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1447{
1448	return xt_mttg_seq_next(seq, v, ppos, true);
1449}
1450
1451static int xt_target_seq_show(struct seq_file *seq, void *v)
1452{
1453	const struct nf_mttg_trav *trav = seq->private;
1454	const struct xt_target *target;
1455
1456	switch (trav->class) {
1457	case MTTG_TRAV_NFP_UNSPEC:
1458	case MTTG_TRAV_NFP_SPEC:
1459		if (trav->curr == trav->head)
1460			return 0;
1461		target = list_entry(trav->curr, struct xt_target, list);
1462		if (*target->name)
1463			seq_printf(seq, "%s\n", target->name);
1464	}
1465	return 0;
1466}
1467
1468static const struct seq_operations xt_target_seq_ops = {
1469	.start	= xt_target_seq_start,
1470	.next	= xt_target_seq_next,
1471	.stop	= xt_mttg_seq_stop,
1472	.show	= xt_target_seq_show,
1473};
1474
1475static int xt_target_open(struct inode *inode, struct file *file)
1476{
1477	struct nf_mttg_trav *trav;
1478	trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1479	if (!trav)
1480		return -ENOMEM;
1481
1482	trav->nfproto = (unsigned long)PDE_DATA(inode);
1483	return 0;
1484}
1485
1486static const struct file_operations xt_target_ops = {
1487	.owner	 = THIS_MODULE,
1488	.open	 = xt_target_open,
1489	.read	 = seq_read,
1490	.llseek	 = seq_lseek,
1491	.release = seq_release_private,
1492};
1493
1494#define FORMAT_TABLES	"_tables_names"
1495#define	FORMAT_MATCHES	"_tables_matches"
1496#define FORMAT_TARGETS 	"_tables_targets"
1497
1498#endif /* CONFIG_PROC_FS */
1499
1500/**
1501 * xt_hook_ops_alloc - set up hooks for a new table
1502 * @table:	table with metadata needed to set up hooks
1503 * @fn:		Hook function
1504 *
1505 * This function will create the nf_hook_ops that the x_table needs
1506 * to hand to xt_hook_link_net().
1507 */
1508struct nf_hook_ops *
1509xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1510{
1511	unsigned int hook_mask = table->valid_hooks;
1512	uint8_t i, num_hooks = hweight32(hook_mask);
1513	uint8_t hooknum;
1514	struct nf_hook_ops *ops;
1515
1516	if (!num_hooks)
1517		return ERR_PTR(-EINVAL);
1518
1519	ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1520	if (ops == NULL)
1521		return ERR_PTR(-ENOMEM);
1522
1523	for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1524	     hook_mask >>= 1, ++hooknum) {
1525		if (!(hook_mask & 1))
1526			continue;
1527		ops[i].hook     = fn;
1528		ops[i].pf       = table->af;
1529		ops[i].hooknum  = hooknum;
1530		ops[i].priority = table->priority;
1531		++i;
1532	}
1533
1534	return ops;
1535}
1536EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1538int xt_proto_init(struct net *net, u_int8_t af)
1539{
1540#ifdef CONFIG_PROC_FS
1541	char buf[XT_FUNCTION_MAXNAMELEN];
1542	struct proc_dir_entry *proc;
1543	kuid_t root_uid;
1544	kgid_t root_gid;
1545#endif
1546
1547	if (af >= ARRAY_SIZE(xt_prefix))
1548		return -EINVAL;
1549
1550
1551#ifdef CONFIG_PROC_FS
1552	root_uid = make_kuid(net->user_ns, 0);
1553	root_gid = make_kgid(net->user_ns, 0);
1554
1555	strlcpy(buf, xt_prefix[af], sizeof(buf));
1556	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1557	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1558				(void *)(unsigned long)af);
 
1559	if (!proc)
1560		goto out;
1561	if (uid_valid(root_uid) && gid_valid(root_gid))
1562		proc_set_user(proc, root_uid, root_gid);
1563
1564	strlcpy(buf, xt_prefix[af], sizeof(buf));
1565	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1566	proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1567				(void *)(unsigned long)af);
 
1568	if (!proc)
1569		goto out_remove_tables;
1570	if (uid_valid(root_uid) && gid_valid(root_gid))
1571		proc_set_user(proc, root_uid, root_gid);
1572
1573	strlcpy(buf, xt_prefix[af], sizeof(buf));
1574	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1575	proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1576				(void *)(unsigned long)af);
 
1577	if (!proc)
1578		goto out_remove_matches;
1579	if (uid_valid(root_uid) && gid_valid(root_gid))
1580		proc_set_user(proc, root_uid, root_gid);
1581#endif
1582
1583	return 0;
1584
1585#ifdef CONFIG_PROC_FS
1586out_remove_matches:
1587	strlcpy(buf, xt_prefix[af], sizeof(buf));
1588	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1589	remove_proc_entry(buf, net->proc_net);
1590
1591out_remove_tables:
1592	strlcpy(buf, xt_prefix[af], sizeof(buf));
1593	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1594	remove_proc_entry(buf, net->proc_net);
1595out:
1596	return -1;
1597#endif
1598}
1599EXPORT_SYMBOL_GPL(xt_proto_init);
1600
1601void xt_proto_fini(struct net *net, u_int8_t af)
1602{
1603#ifdef CONFIG_PROC_FS
1604	char buf[XT_FUNCTION_MAXNAMELEN];
1605
1606	strlcpy(buf, xt_prefix[af], sizeof(buf));
1607	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1608	remove_proc_entry(buf, net->proc_net);
1609
1610	strlcpy(buf, xt_prefix[af], sizeof(buf));
1611	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1612	remove_proc_entry(buf, net->proc_net);
1613
1614	strlcpy(buf, xt_prefix[af], sizeof(buf));
1615	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1616	remove_proc_entry(buf, net->proc_net);
1617#endif /*CONFIG_PROC_FS*/
1618}
1619EXPORT_SYMBOL_GPL(xt_proto_fini);
1620
1621/**
1622 * xt_percpu_counter_alloc - allocate x_tables rule counter
1623 *
1624 * @state: pointer to xt_percpu allocation state
1625 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1626 *
1627 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1628 * contain the address of the real (percpu) counter.
1629 *
1630 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1631 * to fetch the real percpu counter.
1632 *
1633 * To speed up allocation and improve data locality, a 4kb block is
1634 * allocated.
 
 
1635 *
1636 * xt_percpu_counter_alloc_state contains the base address of the
1637 * allocated page and the current sub-offset.
1638 *
1639 * returns false on error.
1640 */
1641bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1642			     struct xt_counters *counter)
1643{
1644	BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1645
1646	if (nr_cpu_ids <= 1)
1647		return true;
1648
1649	if (!state->mem) {
1650		state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1651					    XT_PCPU_BLOCK_SIZE);
1652		if (!state->mem)
1653			return false;
1654	}
1655	counter->pcnt = (__force unsigned long)(state->mem + state->off);
1656	state->off += sizeof(*counter);
1657	if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1658		state->mem = NULL;
1659		state->off = 0;
1660	}
1661	return true;
1662}
1663EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1664
1665void xt_percpu_counter_free(struct xt_counters *counters)
1666{
1667	unsigned long pcnt = counters->pcnt;
1668
1669	if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1670		free_percpu((void __percpu *)pcnt);
1671}
1672EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1673
1674static int __net_init xt_net_init(struct net *net)
1675{
 
1676	int i;
1677
1678	for (i = 0; i < NFPROTO_NUMPROTO; i++)
1679		INIT_LIST_HEAD(&net->xt.tables[i]);
1680	return 0;
1681}
1682
 
 
 
 
 
 
 
 
 
1683static struct pernet_operations xt_net_ops = {
1684	.init = xt_net_init,
 
 
 
1685};
1686
1687static int __init xt_init(void)
1688{
1689	unsigned int i;
1690	int rv;
1691
1692	for_each_possible_cpu(i) {
1693		seqcount_init(&per_cpu(xt_recseq, i));
1694	}
1695
1696	xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
1697	if (!xt)
1698		return -ENOMEM;
1699
1700	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1701		mutex_init(&xt[i].mutex);
1702#ifdef CONFIG_COMPAT
1703		mutex_init(&xt[i].compat_mutex);
1704		xt[i].compat_tab = NULL;
1705#endif
1706		INIT_LIST_HEAD(&xt[i].target);
1707		INIT_LIST_HEAD(&xt[i].match);
 
1708	}
1709	rv = register_pernet_subsys(&xt_net_ops);
1710	if (rv < 0)
1711		kfree(xt);
1712	return rv;
1713}
1714
1715static void __exit xt_fini(void)
1716{
1717	unregister_pernet_subsys(&xt_net_ops);
1718	kfree(xt);
1719}
1720
1721module_init(xt_init);
1722module_exit(xt_fini);
1723
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * x_tables core - Backend for {ip,ip6,arp}_tables
   4 *
   5 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
   6 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
   7 *
   8 * Based on existing ip_tables code which is
   9 *   Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  10 *   Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
 
 
 
 
 
  11 */
  12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/socket.h>
  16#include <linux/net.h>
  17#include <linux/proc_fs.h>
  18#include <linux/seq_file.h>
  19#include <linux/string.h>
  20#include <linux/vmalloc.h>
  21#include <linux/mutex.h>
  22#include <linux/mm.h>
  23#include <linux/slab.h>
  24#include <linux/audit.h>
  25#include <linux/user_namespace.h>
  26#include <net/net_namespace.h>
  27#include <net/netns/generic.h>
  28
  29#include <linux/netfilter/x_tables.h>
  30#include <linux/netfilter_arp.h>
  31#include <linux/netfilter_ipv4/ip_tables.h>
  32#include <linux/netfilter_ipv6/ip6_tables.h>
  33#include <linux/netfilter_arp/arp_tables.h>
  34
  35MODULE_LICENSE("GPL");
  36MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  37MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
  38
 
  39#define XT_PCPU_BLOCK_SIZE 4096
  40#define XT_MAX_TABLE_SIZE	(512 * 1024 * 1024)
  41
  42struct xt_template {
  43	struct list_head list;
  44
  45	/* called when table is needed in the given netns */
  46	int (*table_init)(struct net *net);
  47
  48	struct module *me;
  49
  50	/* A unique name... */
  51	char name[XT_TABLE_MAXNAMELEN];
  52};
  53
  54static struct list_head xt_templates[NFPROTO_NUMPROTO];
  55
  56struct xt_pernet {
  57	struct list_head tables[NFPROTO_NUMPROTO];
  58};
  59
  60struct compat_delta {
  61	unsigned int offset; /* offset in kernel */
  62	int delta; /* delta in 32bit user land */
  63};
  64
  65struct xt_af {
  66	struct mutex mutex;
  67	struct list_head match;
  68	struct list_head target;
  69#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
  70	struct mutex compat_mutex;
  71	struct compat_delta *compat_tab;
  72	unsigned int number; /* number of slots in compat_tab[] */
  73	unsigned int cur; /* number of used slots in compat_tab[] */
  74#endif
  75};
  76
  77static unsigned int xt_pernet_id __read_mostly;
  78static struct xt_af *xt __read_mostly;
  79
  80static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
  81	[NFPROTO_UNSPEC] = "x",
  82	[NFPROTO_IPV4]   = "ip",
  83	[NFPROTO_ARP]    = "arp",
  84	[NFPROTO_BRIDGE] = "eb",
  85	[NFPROTO_IPV6]   = "ip6",
  86};
  87
  88/* Registration hooks for targets. */
  89int xt_register_target(struct xt_target *target)
  90{
  91	u_int8_t af = target->family;
  92
  93	mutex_lock(&xt[af].mutex);
  94	list_add(&target->list, &xt[af].target);
  95	mutex_unlock(&xt[af].mutex);
  96	return 0;
  97}
  98EXPORT_SYMBOL(xt_register_target);
  99
 100void
 101xt_unregister_target(struct xt_target *target)
 102{
 103	u_int8_t af = target->family;
 104
 105	mutex_lock(&xt[af].mutex);
 106	list_del(&target->list);
 107	mutex_unlock(&xt[af].mutex);
 108}
 109EXPORT_SYMBOL(xt_unregister_target);
 110
 111int
 112xt_register_targets(struct xt_target *target, unsigned int n)
 113{
 114	unsigned int i;
 115	int err = 0;
 116
 117	for (i = 0; i < n; i++) {
 118		err = xt_register_target(&target[i]);
 119		if (err)
 120			goto err;
 121	}
 122	return err;
 123
 124err:
 125	if (i > 0)
 126		xt_unregister_targets(target, i);
 127	return err;
 128}
 129EXPORT_SYMBOL(xt_register_targets);
 130
 131void
 132xt_unregister_targets(struct xt_target *target, unsigned int n)
 133{
 134	while (n-- > 0)
 135		xt_unregister_target(&target[n]);
 136}
 137EXPORT_SYMBOL(xt_unregister_targets);
 138
 139int xt_register_match(struct xt_match *match)
 140{
 141	u_int8_t af = match->family;
 142
 143	mutex_lock(&xt[af].mutex);
 144	list_add(&match->list, &xt[af].match);
 145	mutex_unlock(&xt[af].mutex);
 146	return 0;
 147}
 148EXPORT_SYMBOL(xt_register_match);
 149
 150void
 151xt_unregister_match(struct xt_match *match)
 152{
 153	u_int8_t af = match->family;
 154
 155	mutex_lock(&xt[af].mutex);
 156	list_del(&match->list);
 157	mutex_unlock(&xt[af].mutex);
 158}
 159EXPORT_SYMBOL(xt_unregister_match);
 160
 161int
 162xt_register_matches(struct xt_match *match, unsigned int n)
 163{
 164	unsigned int i;
 165	int err = 0;
 166
 167	for (i = 0; i < n; i++) {
 168		err = xt_register_match(&match[i]);
 169		if (err)
 170			goto err;
 171	}
 172	return err;
 173
 174err:
 175	if (i > 0)
 176		xt_unregister_matches(match, i);
 177	return err;
 178}
 179EXPORT_SYMBOL(xt_register_matches);
 180
 181void
 182xt_unregister_matches(struct xt_match *match, unsigned int n)
 183{
 184	while (n-- > 0)
 185		xt_unregister_match(&match[n]);
 186}
 187EXPORT_SYMBOL(xt_unregister_matches);
 188
 189
 190/*
 191 * These are weird, but module loading must not be done with mutex
 192 * held (since they will register), and we have to have a single
 193 * function to use.
 194 */
 195
 196/* Find match, grabs ref.  Returns ERR_PTR() on error. */
 197struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
 198{
 199	struct xt_match *m;
 200	int err = -ENOENT;
 201
 202	if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
 203		return ERR_PTR(-EINVAL);
 204
 205	mutex_lock(&xt[af].mutex);
 206	list_for_each_entry(m, &xt[af].match, list) {
 207		if (strcmp(m->name, name) == 0) {
 208			if (m->revision == revision) {
 209				if (try_module_get(m->me)) {
 210					mutex_unlock(&xt[af].mutex);
 211					return m;
 212				}
 213			} else
 214				err = -EPROTOTYPE; /* Found something. */
 215		}
 216	}
 217	mutex_unlock(&xt[af].mutex);
 218
 219	if (af != NFPROTO_UNSPEC)
 220		/* Try searching again in the family-independent list */
 221		return xt_find_match(NFPROTO_UNSPEC, name, revision);
 222
 223	return ERR_PTR(err);
 224}
 225EXPORT_SYMBOL(xt_find_match);
 226
 227struct xt_match *
 228xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
 229{
 230	struct xt_match *match;
 231
 232	if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
 233		return ERR_PTR(-EINVAL);
 234
 235	match = xt_find_match(nfproto, name, revision);
 236	if (IS_ERR(match)) {
 237		request_module("%st_%s", xt_prefix[nfproto], name);
 238		match = xt_find_match(nfproto, name, revision);
 239	}
 240
 241	return match;
 242}
 243EXPORT_SYMBOL_GPL(xt_request_find_match);
 244
 245/* Find target, grabs ref.  Returns ERR_PTR() on error. */
 246static struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
 247{
 248	struct xt_target *t;
 249	int err = -ENOENT;
 250
 251	if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
 252		return ERR_PTR(-EINVAL);
 253
 254	mutex_lock(&xt[af].mutex);
 255	list_for_each_entry(t, &xt[af].target, list) {
 256		if (strcmp(t->name, name) == 0) {
 257			if (t->revision == revision) {
 258				if (try_module_get(t->me)) {
 259					mutex_unlock(&xt[af].mutex);
 260					return t;
 261				}
 262			} else
 263				err = -EPROTOTYPE; /* Found something. */
 264		}
 265	}
 266	mutex_unlock(&xt[af].mutex);
 267
 268	if (af != NFPROTO_UNSPEC)
 269		/* Try searching again in the family-independent list */
 270		return xt_find_target(NFPROTO_UNSPEC, name, revision);
 271
 272	return ERR_PTR(err);
 273}
 
 274
 275struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
 276{
 277	struct xt_target *target;
 278
 279	if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
 280		return ERR_PTR(-EINVAL);
 281
 282	target = xt_find_target(af, name, revision);
 283	if (IS_ERR(target)) {
 284		request_module("%st_%s", xt_prefix[af], name);
 285		target = xt_find_target(af, name, revision);
 286	}
 287
 288	return target;
 289}
 290EXPORT_SYMBOL_GPL(xt_request_find_target);
 291
 292
 293static int xt_obj_to_user(u16 __user *psize, u16 size,
 294			  void __user *pname, const char *name,
 295			  u8 __user *prev, u8 rev)
 296{
 297	if (put_user(size, psize))
 298		return -EFAULT;
 299	if (copy_to_user(pname, name, strlen(name) + 1))
 300		return -EFAULT;
 301	if (put_user(rev, prev))
 302		return -EFAULT;
 303
 304	return 0;
 305}
 306
 307#define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE)				\
 308	xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size,	\
 309		       U->u.user.name, K->u.kernel.TYPE->name,		\
 310		       &U->u.user.revision, K->u.kernel.TYPE->revision)
 311
 312int xt_data_to_user(void __user *dst, const void *src,
 313		    int usersize, int size, int aligned_size)
 314{
 315	usersize = usersize ? : size;
 316	if (copy_to_user(dst, src, usersize))
 317		return -EFAULT;
 318	if (usersize != aligned_size &&
 319	    clear_user(dst + usersize, aligned_size - usersize))
 320		return -EFAULT;
 321
 322	return 0;
 323}
 324EXPORT_SYMBOL_GPL(xt_data_to_user);
 325
 326#define XT_DATA_TO_USER(U, K, TYPE)					\
 327	xt_data_to_user(U->data, K->data,				\
 328			K->u.kernel.TYPE->usersize,			\
 329			K->u.kernel.TYPE->TYPE##size,			\
 330			XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
 331
 332int xt_match_to_user(const struct xt_entry_match *m,
 333		     struct xt_entry_match __user *u)
 334{
 335	return XT_OBJ_TO_USER(u, m, match, 0) ||
 336	       XT_DATA_TO_USER(u, m, match);
 337}
 338EXPORT_SYMBOL_GPL(xt_match_to_user);
 339
 340int xt_target_to_user(const struct xt_entry_target *t,
 341		      struct xt_entry_target __user *u)
 342{
 343	return XT_OBJ_TO_USER(u, t, target, 0) ||
 344	       XT_DATA_TO_USER(u, t, target);
 345}
 346EXPORT_SYMBOL_GPL(xt_target_to_user);
 347
 348static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
 349{
 350	const struct xt_match *m;
 351	int have_rev = 0;
 352
 353	mutex_lock(&xt[af].mutex);
 354	list_for_each_entry(m, &xt[af].match, list) {
 355		if (strcmp(m->name, name) == 0) {
 356			if (m->revision > *bestp)
 357				*bestp = m->revision;
 358			if (m->revision == revision)
 359				have_rev = 1;
 360		}
 361	}
 362	mutex_unlock(&xt[af].mutex);
 363
 364	if (af != NFPROTO_UNSPEC && !have_rev)
 365		return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
 366
 367	return have_rev;
 368}
 369
 370static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
 371{
 372	const struct xt_target *t;
 373	int have_rev = 0;
 374
 375	mutex_lock(&xt[af].mutex);
 376	list_for_each_entry(t, &xt[af].target, list) {
 377		if (strcmp(t->name, name) == 0) {
 378			if (t->revision > *bestp)
 379				*bestp = t->revision;
 380			if (t->revision == revision)
 381				have_rev = 1;
 382		}
 383	}
 384	mutex_unlock(&xt[af].mutex);
 385
 386	if (af != NFPROTO_UNSPEC && !have_rev)
 387		return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
 388
 389	return have_rev;
 390}
 391
 392/* Returns true or false (if no such extension at all) */
 393int xt_find_revision(u8 af, const char *name, u8 revision, int target,
 394		     int *err)
 395{
 396	int have_rev, best = -1;
 397
 
 398	if (target == 1)
 399		have_rev = target_revfn(af, name, revision, &best);
 400	else
 401		have_rev = match_revfn(af, name, revision, &best);
 
 402
 403	/* Nothing at all?  Return 0 to try loading module. */
 404	if (best == -1) {
 405		*err = -ENOENT;
 406		return 0;
 407	}
 408
 409	*err = best;
 410	if (!have_rev)
 411		*err = -EPROTONOSUPPORT;
 412	return 1;
 413}
 414EXPORT_SYMBOL_GPL(xt_find_revision);
 415
 416static char *
 417textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
 418{
 419	static const char *const inetbr_names[] = {
 420		"PREROUTING", "INPUT", "FORWARD",
 421		"OUTPUT", "POSTROUTING", "BROUTING",
 422	};
 423	static const char *const arp_names[] = {
 424		"INPUT", "FORWARD", "OUTPUT",
 425	};
 426	const char *const *names;
 427	unsigned int i, max;
 428	char *p = buf;
 429	bool np = false;
 430	int res;
 431
 432	names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
 433	max   = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
 434	                                   ARRAY_SIZE(inetbr_names);
 435	*p = '\0';
 436	for (i = 0; i < max; ++i) {
 437		if (!(mask & (1 << i)))
 438			continue;
 439		res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
 440		if (res > 0) {
 441			size -= res;
 442			p += res;
 443		}
 444		np = true;
 445	}
 446
 447	return buf;
 448}
 449
 450/**
 451 * xt_check_proc_name - check that name is suitable for /proc file creation
 452 *
 453 * @name: file name candidate
 454 * @size: length of buffer
 455 *
 456 * some x_tables modules wish to create a file in /proc.
 457 * This function makes sure that the name is suitable for this
 458 * purpose, it checks that name is NUL terminated and isn't a 'special'
 459 * name, like "..".
 460 *
 461 * returns negative number on error or 0 if name is useable.
 462 */
 463int xt_check_proc_name(const char *name, unsigned int size)
 464{
 465	if (name[0] == '\0')
 466		return -EINVAL;
 467
 468	if (strnlen(name, size) == size)
 469		return -ENAMETOOLONG;
 470
 471	if (strcmp(name, ".") == 0 ||
 472	    strcmp(name, "..") == 0 ||
 473	    strchr(name, '/'))
 474		return -EINVAL;
 475
 476	return 0;
 477}
 478EXPORT_SYMBOL(xt_check_proc_name);
 479
 480int xt_check_match(struct xt_mtchk_param *par,
 481		   unsigned int size, u16 proto, bool inv_proto)
 482{
 483	int ret;
 484
 485	if (XT_ALIGN(par->match->matchsize) != size &&
 486	    par->match->matchsize != -1) {
 487		/*
 488		 * ebt_among is exempt from centralized matchsize checking
 489		 * because it uses a dynamic-size data set.
 490		 */
 491		pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
 492				   xt_prefix[par->family], par->match->name,
 493				   par->match->revision,
 494				   XT_ALIGN(par->match->matchsize), size);
 
 495		return -EINVAL;
 496	}
 497	if (par->match->table != NULL &&
 498	    strcmp(par->match->table, par->table) != 0) {
 499		pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
 500				    xt_prefix[par->family], par->match->name,
 501				    par->match->table, par->table);
 502		return -EINVAL;
 503	}
 504	if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
 505		char used[64], allow[64];
 506
 507		pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
 508				    xt_prefix[par->family], par->match->name,
 509				    textify_hooks(used, sizeof(used),
 510						  par->hook_mask, par->family),
 511				    textify_hooks(allow, sizeof(allow),
 512						  par->match->hooks,
 513						  par->family));
 514		return -EINVAL;
 515	}
 516	if (par->match->proto && (par->match->proto != proto || inv_proto)) {
 517		pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
 518				    xt_prefix[par->family], par->match->name,
 519				    par->match->proto);
 520		return -EINVAL;
 521	}
 522	if (par->match->checkentry != NULL) {
 523		ret = par->match->checkentry(par);
 524		if (ret < 0)
 525			return ret;
 526		else if (ret > 0)
 527			/* Flag up potential errors. */
 528			return -EIO;
 529	}
 530	return 0;
 531}
 532EXPORT_SYMBOL_GPL(xt_check_match);
 533
 534/** xt_check_entry_match - check that matches end before start of target
 535 *
 536 * @match: beginning of xt_entry_match
 537 * @target: beginning of this rules target (alleged end of matches)
 538 * @alignment: alignment requirement of match structures
 539 *
 540 * Validates that all matches add up to the beginning of the target,
 541 * and that each match covers at least the base structure size.
 542 *
 543 * Return: 0 on success, negative errno on failure.
 544 */
 545static int xt_check_entry_match(const char *match, const char *target,
 546				const size_t alignment)
 547{
 548	const struct xt_entry_match *pos;
 549	int length = target - match;
 550
 551	if (length == 0) /* no matches */
 552		return 0;
 553
 554	pos = (struct xt_entry_match *)match;
 555	do {
 556		if ((unsigned long)pos % alignment)
 557			return -EINVAL;
 558
 559		if (length < (int)sizeof(struct xt_entry_match))
 560			return -EINVAL;
 561
 562		if (pos->u.match_size < sizeof(struct xt_entry_match))
 563			return -EINVAL;
 564
 565		if (pos->u.match_size > length)
 566			return -EINVAL;
 567
 568		length -= pos->u.match_size;
 569		pos = ((void *)((char *)(pos) + (pos)->u.match_size));
 570	} while (length > 0);
 571
 572	return 0;
 573}
 574
 575/** xt_check_table_hooks - check hook entry points are sane
 576 *
 577 * @info xt_table_info to check
 578 * @valid_hooks - hook entry points that we can enter from
 579 *
 580 * Validates that the hook entry and underflows points are set up.
 581 *
 582 * Return: 0 on success, negative errno on failure.
 583 */
 584int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
 585{
 586	const char *err = "unsorted underflow";
 587	unsigned int i, max_uflow, max_entry;
 588	bool check_hooks = false;
 589
 590	BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
 591
 592	max_entry = 0;
 593	max_uflow = 0;
 594
 595	for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
 596		if (!(valid_hooks & (1 << i)))
 597			continue;
 598
 599		if (info->hook_entry[i] == 0xFFFFFFFF)
 600			return -EINVAL;
 601		if (info->underflow[i] == 0xFFFFFFFF)
 602			return -EINVAL;
 603
 604		if (check_hooks) {
 605			if (max_uflow > info->underflow[i])
 606				goto error;
 607
 608			if (max_uflow == info->underflow[i]) {
 609				err = "duplicate underflow";
 610				goto error;
 611			}
 612			if (max_entry > info->hook_entry[i]) {
 613				err = "unsorted entry";
 614				goto error;
 615			}
 616			if (max_entry == info->hook_entry[i]) {
 617				err = "duplicate entry";
 618				goto error;
 619			}
 620		}
 621		max_entry = info->hook_entry[i];
 622		max_uflow = info->underflow[i];
 623		check_hooks = true;
 624	}
 625
 626	return 0;
 627error:
 628	pr_err_ratelimited("%s at hook %d\n", err, i);
 629	return -EINVAL;
 630}
 631EXPORT_SYMBOL(xt_check_table_hooks);
 632
 633static bool verdict_ok(int verdict)
 634{
 635	if (verdict > 0)
 636		return true;
 637
 638	if (verdict < 0) {
 639		int v = -verdict - 1;
 640
 641		if (verdict == XT_RETURN)
 642			return true;
 643
 644		switch (v) {
 645		case NF_ACCEPT: return true;
 646		case NF_DROP: return true;
 647		case NF_QUEUE: return true;
 648		default:
 649			break;
 650		}
 651
 652		return false;
 653	}
 654
 655	return false;
 656}
 657
 658static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
 659			const char *msg, unsigned int msglen)
 660{
 661	return usersize == kernsize && strnlen(msg, msglen) < msglen;
 662}
 663
 664#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
 665int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
 666{
 667	struct xt_af *xp = &xt[af];
 668
 669	WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
 670
 671	if (WARN_ON(!xp->compat_tab))
 672		return -ENOMEM;
 673
 674	if (xp->cur >= xp->number)
 675		return -EINVAL;
 676
 677	if (xp->cur)
 678		delta += xp->compat_tab[xp->cur - 1].delta;
 679	xp->compat_tab[xp->cur].offset = offset;
 680	xp->compat_tab[xp->cur].delta = delta;
 681	xp->cur++;
 682	return 0;
 683}
 684EXPORT_SYMBOL_GPL(xt_compat_add_offset);
 685
 686void xt_compat_flush_offsets(u_int8_t af)
 687{
 688	WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
 689
 690	if (xt[af].compat_tab) {
 691		vfree(xt[af].compat_tab);
 692		xt[af].compat_tab = NULL;
 693		xt[af].number = 0;
 694		xt[af].cur = 0;
 695	}
 696}
 697EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
 698
 699int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
 700{
 701	struct compat_delta *tmp = xt[af].compat_tab;
 702	int mid, left = 0, right = xt[af].cur - 1;
 703
 704	while (left <= right) {
 705		mid = (left + right) >> 1;
 706		if (offset > tmp[mid].offset)
 707			left = mid + 1;
 708		else if (offset < tmp[mid].offset)
 709			right = mid - 1;
 710		else
 711			return mid ? tmp[mid - 1].delta : 0;
 712	}
 713	return left ? tmp[left - 1].delta : 0;
 714}
 715EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
 716
 717int xt_compat_init_offsets(u8 af, unsigned int number)
 718{
 719	size_t mem;
 720
 721	WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
 722
 723	if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
 724		return -EINVAL;
 725
 726	if (WARN_ON(xt[af].compat_tab))
 727		return -EINVAL;
 728
 729	mem = sizeof(struct compat_delta) * number;
 730	if (mem > XT_MAX_TABLE_SIZE)
 731		return -ENOMEM;
 732
 733	xt[af].compat_tab = vmalloc(mem);
 734	if (!xt[af].compat_tab)
 735		return -ENOMEM;
 736
 737	xt[af].number = number;
 738	xt[af].cur = 0;
 739
 740	return 0;
 741}
 742EXPORT_SYMBOL(xt_compat_init_offsets);
 743
 744int xt_compat_match_offset(const struct xt_match *match)
 745{
 746	u_int16_t csize = match->compatsize ? : match->matchsize;
 747	return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
 748}
 749EXPORT_SYMBOL_GPL(xt_compat_match_offset);
 750
 751void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
 752			       unsigned int *size)
 753{
 754	const struct xt_match *match = m->u.kernel.match;
 755	struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
 756	int off = xt_compat_match_offset(match);
 757	u_int16_t msize = cm->u.user.match_size;
 758	char name[sizeof(m->u.user.name)];
 759
 760	m = *dstptr;
 761	memcpy(m, cm, sizeof(*cm));
 762	if (match->compat_from_user)
 763		match->compat_from_user(m->data, cm->data);
 764	else
 765		memcpy(m->data, cm->data, msize - sizeof(*cm));
 
 
 
 766
 767	msize += off;
 768	m->u.user.match_size = msize;
 769	strscpy(name, match->name, sizeof(name));
 770	module_put(match->me);
 771	strscpy_pad(m->u.user.name, name, sizeof(m->u.user.name));
 772
 773	*size += off;
 774	*dstptr += msize;
 775}
 776EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
 777
 778#define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE)			\
 779	xt_data_to_user(U->data, K->data,				\
 780			K->u.kernel.TYPE->usersize,			\
 781			C_SIZE,						\
 782			COMPAT_XT_ALIGN(C_SIZE))
 783
 784int xt_compat_match_to_user(const struct xt_entry_match *m,
 785			    void __user **dstptr, unsigned int *size)
 786{
 787	const struct xt_match *match = m->u.kernel.match;
 788	struct compat_xt_entry_match __user *cm = *dstptr;
 789	int off = xt_compat_match_offset(match);
 790	u_int16_t msize = m->u.user.match_size - off;
 791
 792	if (XT_OBJ_TO_USER(cm, m, match, msize))
 
 
 
 793		return -EFAULT;
 794
 795	if (match->compat_to_user) {
 796		if (match->compat_to_user((void __user *)cm->data, m->data))
 797			return -EFAULT;
 798	} else {
 799		if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
 800			return -EFAULT;
 801	}
 802
 803	*size -= off;
 804	*dstptr += msize;
 805	return 0;
 806}
 807EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
 808
 809/* non-compat version may have padding after verdict */
 810struct compat_xt_standard_target {
 811	struct compat_xt_entry_target t;
 812	compat_uint_t verdict;
 813};
 814
 815struct compat_xt_error_target {
 816	struct compat_xt_entry_target t;
 817	char errorname[XT_FUNCTION_MAXNAMELEN];
 818};
 819
 820int xt_compat_check_entry_offsets(const void *base, const char *elems,
 821				  unsigned int target_offset,
 822				  unsigned int next_offset)
 823{
 824	long size_of_base_struct = elems - (const char *)base;
 825	const struct compat_xt_entry_target *t;
 826	const char *e = base;
 827
 828	if (target_offset < size_of_base_struct)
 829		return -EINVAL;
 830
 831	if (target_offset + sizeof(*t) > next_offset)
 832		return -EINVAL;
 833
 834	t = (void *)(e + target_offset);
 835	if (t->u.target_size < sizeof(*t))
 836		return -EINVAL;
 837
 838	if (target_offset + t->u.target_size > next_offset)
 839		return -EINVAL;
 840
 841	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
 842		const struct compat_xt_standard_target *st = (const void *)t;
 843
 844		if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
 845			return -EINVAL;
 846
 847		if (!verdict_ok(st->verdict))
 848			return -EINVAL;
 849	} else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
 850		const struct compat_xt_error_target *et = (const void *)t;
 851
 852		if (!error_tg_ok(t->u.target_size, sizeof(*et),
 853				 et->errorname, sizeof(et->errorname)))
 854			return -EINVAL;
 855	}
 856
 857	/* compat_xt_entry match has less strict alignment requirements,
 858	 * otherwise they are identical.  In case of padding differences
 859	 * we need to add compat version of xt_check_entry_match.
 860	 */
 861	BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
 862
 863	return xt_check_entry_match(elems, base + target_offset,
 864				    __alignof__(struct compat_xt_entry_match));
 865}
 866EXPORT_SYMBOL(xt_compat_check_entry_offsets);
 867#endif /* CONFIG_NETFILTER_XTABLES_COMPAT */
 868
 869/**
 870 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
 871 *
 872 * @base: pointer to arp/ip/ip6t_entry
 873 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
 874 * @target_offset: the arp/ip/ip6_t->target_offset
 875 * @next_offset: the arp/ip/ip6_t->next_offset
 876 *
 877 * validates that target_offset and next_offset are sane and that all
 878 * match sizes (if any) align with the target offset.
 879 *
 880 * This function does not validate the targets or matches themselves, it
 881 * only tests that all the offsets and sizes are correct, that all
 882 * match structures are aligned, and that the last structure ends where
 883 * the target structure begins.
 884 *
 885 * Also see xt_compat_check_entry_offsets for CONFIG_NETFILTER_XTABLES_COMPAT version.
 886 *
 887 * The arp/ip/ip6t_entry structure @base must have passed following tests:
 888 * - it must point to a valid memory location
 889 * - base to base + next_offset must be accessible, i.e. not exceed allocated
 890 *   length.
 891 *
 892 * A well-formed entry looks like this:
 893 *
 894 * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
 895 * e->elems[]-----'                              |               |
 896 *                matchsize                      |               |
 897 *                                matchsize      |               |
 898 *                                               |               |
 899 * target_offset---------------------------------'               |
 900 * next_offset---------------------------------------------------'
 901 *
 902 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
 903 *          This is where matches (if any) and the target reside.
 904 * target_offset: beginning of target.
 905 * next_offset: start of the next rule; also: size of this rule.
 906 * Since targets have a minimum size, target_offset + minlen <= next_offset.
 907 *
 908 * Every match stores its size, sum of sizes must not exceed target_offset.
 909 *
 910 * Return: 0 on success, negative errno on failure.
 911 */
 912int xt_check_entry_offsets(const void *base,
 913			   const char *elems,
 914			   unsigned int target_offset,
 915			   unsigned int next_offset)
 916{
 917	long size_of_base_struct = elems - (const char *)base;
 918	const struct xt_entry_target *t;
 919	const char *e = base;
 920
 921	/* target start is within the ip/ip6/arpt_entry struct */
 922	if (target_offset < size_of_base_struct)
 923		return -EINVAL;
 924
 925	if (target_offset + sizeof(*t) > next_offset)
 926		return -EINVAL;
 927
 928	t = (void *)(e + target_offset);
 929	if (t->u.target_size < sizeof(*t))
 930		return -EINVAL;
 931
 932	if (target_offset + t->u.target_size > next_offset)
 933		return -EINVAL;
 934
 935	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
 936		const struct xt_standard_target *st = (const void *)t;
 937
 938		if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
 939			return -EINVAL;
 940
 941		if (!verdict_ok(st->verdict))
 942			return -EINVAL;
 943	} else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
 944		const struct xt_error_target *et = (const void *)t;
 945
 946		if (!error_tg_ok(t->u.target_size, sizeof(*et),
 947				 et->errorname, sizeof(et->errorname)))
 948			return -EINVAL;
 949	}
 950
 951	return xt_check_entry_match(elems, base + target_offset,
 952				    __alignof__(struct xt_entry_match));
 953}
 954EXPORT_SYMBOL(xt_check_entry_offsets);
 955
 956/**
 957 * xt_alloc_entry_offsets - allocate array to store rule head offsets
 958 *
 959 * @size: number of entries
 960 *
 961 * Return: NULL or zeroed kmalloc'd or vmalloc'd array
 962 */
 963unsigned int *xt_alloc_entry_offsets(unsigned int size)
 964{
 965	if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
 966		return NULL;
 
 
 
 
 967
 968	return kvcalloc(size, sizeof(unsigned int), GFP_KERNEL);
 
 969
 
 970}
 971EXPORT_SYMBOL(xt_alloc_entry_offsets);
 972
 973/**
 974 * xt_find_jump_offset - check if target is a valid jump offset
 975 *
 976 * @offsets: array containing all valid rule start offsets of a rule blob
 977 * @target: the jump target to search for
 978 * @size: entries in @offset
 979 */
 980bool xt_find_jump_offset(const unsigned int *offsets,
 981			 unsigned int target, unsigned int size)
 982{
 983	int m, low = 0, hi = size;
 984
 985	while (hi > low) {
 986		m = (low + hi) / 2u;
 987
 988		if (offsets[m] > target)
 989			hi = m;
 990		else if (offsets[m] < target)
 991			low = m + 1;
 992		else
 993			return true;
 994	}
 995
 996	return false;
 997}
 998EXPORT_SYMBOL(xt_find_jump_offset);
 999
1000int xt_check_target(struct xt_tgchk_param *par,
1001		    unsigned int size, u16 proto, bool inv_proto)
1002{
1003	int ret;
1004
1005	if (XT_ALIGN(par->target->targetsize) != size) {
1006		pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
1007				   xt_prefix[par->family], par->target->name,
1008				   par->target->revision,
1009				   XT_ALIGN(par->target->targetsize), size);
 
1010		return -EINVAL;
1011	}
1012	if (par->target->table != NULL &&
1013	    strcmp(par->target->table, par->table) != 0) {
1014		pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
1015				    xt_prefix[par->family], par->target->name,
1016				    par->target->table, par->table);
1017		return -EINVAL;
1018	}
1019	if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1020		char used[64], allow[64];
1021
1022		pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1023				    xt_prefix[par->family], par->target->name,
1024				    textify_hooks(used, sizeof(used),
1025						  par->hook_mask, par->family),
1026				    textify_hooks(allow, sizeof(allow),
1027						  par->target->hooks,
1028						  par->family));
1029		return -EINVAL;
1030	}
1031	if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1032		pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1033				    xt_prefix[par->family], par->target->name,
1034				    par->target->proto);
1035		return -EINVAL;
1036	}
1037	if (par->target->checkentry != NULL) {
1038		ret = par->target->checkentry(par);
1039		if (ret < 0)
1040			return ret;
1041		else if (ret > 0)
1042			/* Flag up potential errors. */
1043			return -EIO;
1044	}
1045	return 0;
1046}
1047EXPORT_SYMBOL_GPL(xt_check_target);
1048
1049/**
1050 * xt_copy_counters - copy counters and metadata from a sockptr_t
1051 *
1052 * @arg: src sockptr
1053 * @len: alleged size of userspace memory
1054 * @info: where to store the xt_counters_info metadata
 
1055 *
1056 * Copies counter meta data from @user and stores it in @info.
1057 *
1058 * vmallocs memory to hold the counters, then copies the counter data
1059 * from @user to the new memory and returns a pointer to it.
1060 *
1061 * If called from a compat syscall, @info gets converted automatically to the
1062 * 64bit representation.
1063 *
1064 * The metadata associated with the counters is stored in @info.
1065 *
1066 * Return: returns pointer that caller has to test via IS_ERR().
1067 * If IS_ERR is false, caller has to vfree the pointer.
1068 */
1069void *xt_copy_counters(sockptr_t arg, unsigned int len,
1070		       struct xt_counters_info *info)
1071{
1072	size_t offset;
1073	void *mem;
1074	u64 size;
1075
1076#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1077	if (in_compat_syscall()) {
1078		/* structures only differ in size due to alignment */
1079		struct compat_xt_counters_info compat_tmp;
1080
1081		if (len <= sizeof(compat_tmp))
1082			return ERR_PTR(-EINVAL);
1083
1084		len -= sizeof(compat_tmp);
1085		if (copy_from_sockptr(&compat_tmp, arg, sizeof(compat_tmp)) != 0)
1086			return ERR_PTR(-EFAULT);
1087
1088		memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1089		info->num_counters = compat_tmp.num_counters;
1090		offset = sizeof(compat_tmp);
1091	} else
1092#endif
1093	{
1094		if (len <= sizeof(*info))
1095			return ERR_PTR(-EINVAL);
1096
1097		len -= sizeof(*info);
1098		if (copy_from_sockptr(info, arg, sizeof(*info)) != 0)
1099			return ERR_PTR(-EFAULT);
1100
1101		offset = sizeof(*info);
 
1102	}
1103	info->name[sizeof(info->name) - 1] = '\0';
1104
1105	size = sizeof(struct xt_counters);
1106	size *= info->num_counters;
1107
1108	if (size != (u64)len)
1109		return ERR_PTR(-EINVAL);
1110
1111	mem = vmalloc(len);
1112	if (!mem)
1113		return ERR_PTR(-ENOMEM);
1114
1115	if (copy_from_sockptr_offset(mem, arg, offset, len) == 0)
1116		return mem;
1117
1118	vfree(mem);
1119	return ERR_PTR(-EFAULT);
1120}
1121EXPORT_SYMBOL_GPL(xt_copy_counters);
1122
1123#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1124int xt_compat_target_offset(const struct xt_target *target)
1125{
1126	u_int16_t csize = target->compatsize ? : target->targetsize;
1127	return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1128}
1129EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1130
1131void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1132				unsigned int *size)
1133{
1134	const struct xt_target *target = t->u.kernel.target;
1135	struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1136	int off = xt_compat_target_offset(target);
1137	u_int16_t tsize = ct->u.user.target_size;
1138	char name[sizeof(t->u.user.name)];
1139
1140	t = *dstptr;
1141	memcpy(t, ct, sizeof(*ct));
1142	if (target->compat_from_user)
1143		target->compat_from_user(t->data, ct->data);
1144	else
1145		unsafe_memcpy(t->data, ct->data, tsize - sizeof(*ct),
1146			      /* UAPI 0-sized destination */);
 
 
1147
1148	tsize += off;
1149	t->u.user.target_size = tsize;
1150	strscpy(name, target->name, sizeof(name));
1151	module_put(target->me);
1152	strscpy_pad(t->u.user.name, name, sizeof(t->u.user.name));
1153
1154	*size += off;
1155	*dstptr += tsize;
1156}
1157EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1158
1159int xt_compat_target_to_user(const struct xt_entry_target *t,
1160			     void __user **dstptr, unsigned int *size)
1161{
1162	const struct xt_target *target = t->u.kernel.target;
1163	struct compat_xt_entry_target __user *ct = *dstptr;
1164	int off = xt_compat_target_offset(target);
1165	u_int16_t tsize = t->u.user.target_size - off;
1166
1167	if (XT_OBJ_TO_USER(ct, t, target, tsize))
 
 
 
1168		return -EFAULT;
1169
1170	if (target->compat_to_user) {
1171		if (target->compat_to_user((void __user *)ct->data, t->data))
1172			return -EFAULT;
1173	} else {
1174		if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1175			return -EFAULT;
1176	}
1177
1178	*size -= off;
1179	*dstptr += tsize;
1180	return 0;
1181}
1182EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1183#endif
1184
1185struct xt_table_info *xt_alloc_table_info(unsigned int size)
1186{
1187	struct xt_table_info *info = NULL;
1188	size_t sz = sizeof(*info) + size;
1189
1190	if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1191		return NULL;
1192
1193	info = kvmalloc(sz, GFP_KERNEL_ACCOUNT);
1194	if (!info)
1195		return NULL;
1196
 
 
 
 
 
 
 
 
 
1197	memset(info, 0, sizeof(*info));
1198	info->size = size;
1199	return info;
1200}
1201EXPORT_SYMBOL(xt_alloc_table_info);
1202
1203void xt_free_table_info(struct xt_table_info *info)
1204{
1205	int cpu;
1206
1207	if (info->jumpstack != NULL) {
1208		for_each_possible_cpu(cpu)
1209			kvfree(info->jumpstack[cpu]);
1210		kvfree(info->jumpstack);
1211	}
1212
1213	kvfree(info);
1214}
1215EXPORT_SYMBOL(xt_free_table_info);
1216
1217struct xt_table *xt_find_table(struct net *net, u8 af, const char *name)
1218{
1219	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1220	struct xt_table *t;
1221
1222	mutex_lock(&xt[af].mutex);
1223	list_for_each_entry(t, &xt_net->tables[af], list) {
1224		if (strcmp(t->name, name) == 0) {
1225			mutex_unlock(&xt[af].mutex);
1226			return t;
1227		}
1228	}
1229	mutex_unlock(&xt[af].mutex);
1230	return NULL;
1231}
1232EXPORT_SYMBOL(xt_find_table);
1233
1234/* Find table by name, grabs mutex & ref.  Returns ERR_PTR on error. */
1235struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1236				    const char *name)
1237{
1238	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1239	struct module *owner = NULL;
1240	struct xt_template *tmpl;
1241	struct xt_table *t;
1242
1243	mutex_lock(&xt[af].mutex);
1244	list_for_each_entry(t, &xt_net->tables[af], list)
1245		if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1246			return t;
1247
1248	/* Table doesn't exist in this netns, check larval list */
1249	list_for_each_entry(tmpl, &xt_templates[af], list) {
1250		int err;
1251
1252		if (strcmp(tmpl->name, name))
 
 
1253			continue;
1254		if (!try_module_get(tmpl->me))
1255			goto out;
1256
1257		owner = tmpl->me;
1258
1259		mutex_unlock(&xt[af].mutex);
1260		err = tmpl->table_init(net);
1261		if (err < 0) {
1262			module_put(owner);
1263			return ERR_PTR(err);
1264		}
1265
 
 
1266		mutex_lock(&xt[af].mutex);
1267		break;
1268	}
1269
 
 
 
1270	/* and once again: */
1271	list_for_each_entry(t, &xt_net->tables[af], list)
1272		if (strcmp(t->name, name) == 0 && owner == t->me)
1273			return t;
1274
1275	module_put(owner);
1276 out:
1277	mutex_unlock(&xt[af].mutex);
1278	return ERR_PTR(-ENOENT);
1279}
1280EXPORT_SYMBOL_GPL(xt_find_table_lock);
1281
1282struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1283					    const char *name)
1284{
1285	struct xt_table *t = xt_find_table_lock(net, af, name);
1286
1287#ifdef CONFIG_MODULES
1288	if (IS_ERR(t)) {
1289		int err = request_module("%stable_%s", xt_prefix[af], name);
1290		if (err < 0)
1291			return ERR_PTR(err);
1292		t = xt_find_table_lock(net, af, name);
1293	}
1294#endif
1295
1296	return t;
1297}
1298EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1299
1300void xt_table_unlock(struct xt_table *table)
1301{
1302	mutex_unlock(&xt[table->af].mutex);
1303}
1304EXPORT_SYMBOL_GPL(xt_table_unlock);
1305
1306#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1307void xt_compat_lock(u_int8_t af)
1308{
1309	mutex_lock(&xt[af].compat_mutex);
1310}
1311EXPORT_SYMBOL_GPL(xt_compat_lock);
1312
1313void xt_compat_unlock(u_int8_t af)
1314{
1315	mutex_unlock(&xt[af].compat_mutex);
1316}
1317EXPORT_SYMBOL_GPL(xt_compat_unlock);
1318#endif
1319
1320DEFINE_PER_CPU(seqcount_t, xt_recseq);
1321EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1322
1323struct static_key xt_tee_enabled __read_mostly;
1324EXPORT_SYMBOL_GPL(xt_tee_enabled);
1325
1326static int xt_jumpstack_alloc(struct xt_table_info *i)
1327{
1328	unsigned int size;
1329	int cpu;
1330
1331	size = sizeof(void **) * nr_cpu_ids;
1332	if (size > PAGE_SIZE)
1333		i->jumpstack = kvzalloc(size, GFP_KERNEL);
1334	else
1335		i->jumpstack = kzalloc(size, GFP_KERNEL);
1336	if (i->jumpstack == NULL)
1337		return -ENOMEM;
1338
1339	/* ruleset without jumps -- no stack needed */
1340	if (i->stacksize == 0)
1341		return 0;
1342
1343	/* Jumpstack needs to be able to record two full callchains, one
1344	 * from the first rule set traversal, plus one table reentrancy
1345	 * via -j TEE without clobbering the callchain that brought us to
1346	 * TEE target.
1347	 *
1348	 * This is done by allocating two jumpstacks per cpu, on reentry
1349	 * the upper half of the stack is used.
1350	 *
1351	 * see the jumpstack setup in ipt_do_table() for more details.
1352	 */
1353	size = sizeof(void *) * i->stacksize * 2u;
1354	for_each_possible_cpu(cpu) {
1355		i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1356			cpu_to_node(cpu));
 
 
 
 
1357		if (i->jumpstack[cpu] == NULL)
1358			/*
1359			 * Freeing will be done later on by the callers. The
1360			 * chain is: xt_replace_table -> __do_replace ->
1361			 * do_replace -> xt_free_table_info.
1362			 */
1363			return -ENOMEM;
1364	}
1365
1366	return 0;
1367}
1368
1369struct xt_counters *xt_counters_alloc(unsigned int counters)
1370{
1371	struct xt_counters *mem;
1372
1373	if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1374		return NULL;
1375
1376	counters *= sizeof(*mem);
1377	if (counters > XT_MAX_TABLE_SIZE)
1378		return NULL;
1379
1380	return vzalloc(counters);
1381}
1382EXPORT_SYMBOL(xt_counters_alloc);
1383
1384struct xt_table_info *
1385xt_replace_table(struct xt_table *table,
1386	      unsigned int num_counters,
1387	      struct xt_table_info *newinfo,
1388	      int *error)
1389{
1390	struct xt_table_info *private;
1391	unsigned int cpu;
1392	int ret;
1393
1394	ret = xt_jumpstack_alloc(newinfo);
1395	if (ret < 0) {
1396		*error = ret;
1397		return NULL;
1398	}
1399
1400	/* Do the substitution. */
1401	local_bh_disable();
1402	private = table->private;
1403
1404	/* Check inside lock: is the old number correct? */
1405	if (num_counters != private->number) {
1406		pr_debug("num_counters != table->private->number (%u/%u)\n",
1407			 num_counters, private->number);
1408		local_bh_enable();
1409		*error = -EAGAIN;
1410		return NULL;
1411	}
1412
1413	newinfo->initial_entries = private->initial_entries;
1414	/*
1415	 * Ensure contents of newinfo are visible before assigning to
1416	 * private.
1417	 */
1418	smp_wmb();
1419	table->private = newinfo;
1420
1421	/* make sure all cpus see new ->private value */
1422	smp_mb();
1423
1424	/*
1425	 * Even though table entries have now been swapped, other CPU's
1426	 * may still be using the old entries...
 
 
1427	 */
1428	local_bh_enable();
1429
1430	/* ... so wait for even xt_recseq on all cpus */
1431	for_each_possible_cpu(cpu) {
1432		seqcount_t *s = &per_cpu(xt_recseq, cpu);
1433		u32 seq = raw_read_seqcount(s);
1434
1435		if (seq & 1) {
1436			do {
1437				cond_resched();
1438				cpu_relax();
1439			} while (seq == raw_read_seqcount(s));
 
1440		}
1441	}
 
1442
1443	audit_log_nfcfg(table->name, table->af, private->number,
1444			!private->number ? AUDIT_XT_OP_REGISTER :
1445					   AUDIT_XT_OP_REPLACE,
1446			GFP_KERNEL);
1447	return private;
1448}
1449EXPORT_SYMBOL_GPL(xt_replace_table);
1450
1451struct xt_table *xt_register_table(struct net *net,
1452				   const struct xt_table *input_table,
1453				   struct xt_table_info *bootstrap,
1454				   struct xt_table_info *newinfo)
1455{
1456	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1457	struct xt_table_info *private;
1458	struct xt_table *t, *table;
1459	int ret;
1460
1461	/* Don't add one object to multiple lists. */
1462	table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1463	if (!table) {
1464		ret = -ENOMEM;
1465		goto out;
1466	}
1467
1468	mutex_lock(&xt[table->af].mutex);
1469	/* Don't autoload: we'd eat our tail... */
1470	list_for_each_entry(t, &xt_net->tables[table->af], list) {
1471		if (strcmp(t->name, table->name) == 0) {
1472			ret = -EEXIST;
1473			goto unlock;
1474		}
1475	}
1476
1477	/* Simplifies replace_table code. */
1478	table->private = bootstrap;
1479
1480	if (!xt_replace_table(table, 0, newinfo, &ret))
1481		goto unlock;
1482
1483	private = table->private;
1484	pr_debug("table->private->number = %u\n", private->number);
1485
1486	/* save number of initial entries */
1487	private->initial_entries = private->number;
1488
1489	list_add(&table->list, &xt_net->tables[table->af]);
1490	mutex_unlock(&xt[table->af].mutex);
1491	return table;
1492
1493unlock:
1494	mutex_unlock(&xt[table->af].mutex);
1495	kfree(table);
1496out:
1497	return ERR_PTR(ret);
1498}
1499EXPORT_SYMBOL_GPL(xt_register_table);
1500
1501void *xt_unregister_table(struct xt_table *table)
1502{
1503	struct xt_table_info *private;
1504
1505	mutex_lock(&xt[table->af].mutex);
1506	private = table->private;
1507	list_del(&table->list);
1508	mutex_unlock(&xt[table->af].mutex);
1509	audit_log_nfcfg(table->name, table->af, private->number,
1510			AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
1511	kfree(table->ops);
1512	kfree(table);
1513
1514	return private;
1515}
1516EXPORT_SYMBOL_GPL(xt_unregister_table);
1517
1518#ifdef CONFIG_PROC_FS
 
 
 
 
1519static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1520{
1521	u8 af = (unsigned long)pde_data(file_inode(seq->file));
1522	struct net *net = seq_file_net(seq);
1523	struct xt_pernet *xt_net;
1524
1525	xt_net = net_generic(net, xt_pernet_id);
1526
1527	mutex_lock(&xt[af].mutex);
1528	return seq_list_start(&xt_net->tables[af], *pos);
1529}
1530
1531static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1532{
1533	u8 af = (unsigned long)pde_data(file_inode(seq->file));
1534	struct net *net = seq_file_net(seq);
1535	struct xt_pernet *xt_net;
1536
1537	xt_net = net_generic(net, xt_pernet_id);
1538
1539	return seq_list_next(v, &xt_net->tables[af], pos);
1540}
1541
1542static void xt_table_seq_stop(struct seq_file *seq, void *v)
1543{
1544	u_int8_t af = (unsigned long)pde_data(file_inode(seq->file));
 
1545
1546	mutex_unlock(&xt[af].mutex);
1547}
1548
1549static int xt_table_seq_show(struct seq_file *seq, void *v)
1550{
1551	struct xt_table *table = list_entry(v, struct xt_table, list);
1552
1553	if (*table->name)
1554		seq_printf(seq, "%s\n", table->name);
1555	return 0;
1556}
1557
1558static const struct seq_operations xt_table_seq_ops = {
1559	.start	= xt_table_seq_start,
1560	.next	= xt_table_seq_next,
1561	.stop	= xt_table_seq_stop,
1562	.show	= xt_table_seq_show,
1563};
1564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1565/*
1566 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1567 * the multi-AF mutexes.
1568 */
1569struct nf_mttg_trav {
1570	struct list_head *head, *curr;
1571	uint8_t class;
1572};
1573
1574enum {
1575	MTTG_TRAV_INIT,
1576	MTTG_TRAV_NFP_UNSPEC,
1577	MTTG_TRAV_NFP_SPEC,
1578	MTTG_TRAV_DONE,
1579};
1580
1581static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1582    bool is_target)
1583{
1584	static const uint8_t next_class[] = {
1585		[MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1586		[MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1587	};
1588	uint8_t nfproto = (unsigned long)pde_data(file_inode(seq->file));
1589	struct nf_mttg_trav *trav = seq->private;
1590
1591	if (ppos != NULL)
1592		++(*ppos);
1593
1594	switch (trav->class) {
1595	case MTTG_TRAV_INIT:
1596		trav->class = MTTG_TRAV_NFP_UNSPEC;
1597		mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1598		trav->head = trav->curr = is_target ?
1599			&xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1600 		break;
1601	case MTTG_TRAV_NFP_UNSPEC:
1602		trav->curr = trav->curr->next;
1603		if (trav->curr != trav->head)
1604			break;
1605		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1606		mutex_lock(&xt[nfproto].mutex);
1607		trav->head = trav->curr = is_target ?
1608			&xt[nfproto].target : &xt[nfproto].match;
1609		trav->class = next_class[trav->class];
1610		break;
1611	case MTTG_TRAV_NFP_SPEC:
1612		trav->curr = trav->curr->next;
1613		if (trav->curr != trav->head)
1614			break;
1615		fallthrough;
1616	default:
1617		return NULL;
1618	}
 
 
 
1619	return trav;
1620}
1621
1622static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1623    bool is_target)
1624{
1625	struct nf_mttg_trav *trav = seq->private;
1626	unsigned int j;
1627
1628	trav->class = MTTG_TRAV_INIT;
1629	for (j = 0; j < *pos; ++j)
1630		if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1631			return NULL;
1632	return trav;
1633}
1634
1635static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1636{
1637	uint8_t nfproto = (unsigned long)pde_data(file_inode(seq->file));
1638	struct nf_mttg_trav *trav = seq->private;
1639
1640	switch (trav->class) {
1641	case MTTG_TRAV_NFP_UNSPEC:
1642		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1643		break;
1644	case MTTG_TRAV_NFP_SPEC:
1645		mutex_unlock(&xt[nfproto].mutex);
1646		break;
1647	}
1648}
1649
1650static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1651{
1652	return xt_mttg_seq_start(seq, pos, false);
1653}
1654
1655static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1656{
1657	return xt_mttg_seq_next(seq, v, ppos, false);
1658}
1659
1660static int xt_match_seq_show(struct seq_file *seq, void *v)
1661{
1662	const struct nf_mttg_trav *trav = seq->private;
1663	const struct xt_match *match;
1664
1665	switch (trav->class) {
1666	case MTTG_TRAV_NFP_UNSPEC:
1667	case MTTG_TRAV_NFP_SPEC:
1668		if (trav->curr == trav->head)
1669			return 0;
1670		match = list_entry(trav->curr, struct xt_match, list);
1671		if (*match->name)
1672			seq_printf(seq, "%s\n", match->name);
1673	}
1674	return 0;
1675}
1676
1677static const struct seq_operations xt_match_seq_ops = {
1678	.start	= xt_match_seq_start,
1679	.next	= xt_match_seq_next,
1680	.stop	= xt_mttg_seq_stop,
1681	.show	= xt_match_seq_show,
1682};
1683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1684static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1685{
1686	return xt_mttg_seq_start(seq, pos, true);
1687}
1688
1689static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1690{
1691	return xt_mttg_seq_next(seq, v, ppos, true);
1692}
1693
1694static int xt_target_seq_show(struct seq_file *seq, void *v)
1695{
1696	const struct nf_mttg_trav *trav = seq->private;
1697	const struct xt_target *target;
1698
1699	switch (trav->class) {
1700	case MTTG_TRAV_NFP_UNSPEC:
1701	case MTTG_TRAV_NFP_SPEC:
1702		if (trav->curr == trav->head)
1703			return 0;
1704		target = list_entry(trav->curr, struct xt_target, list);
1705		if (*target->name)
1706			seq_printf(seq, "%s\n", target->name);
1707	}
1708	return 0;
1709}
1710
1711static const struct seq_operations xt_target_seq_ops = {
1712	.start	= xt_target_seq_start,
1713	.next	= xt_target_seq_next,
1714	.stop	= xt_mttg_seq_stop,
1715	.show	= xt_target_seq_show,
1716};
1717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1718#define FORMAT_TABLES	"_tables_names"
1719#define	FORMAT_MATCHES	"_tables_matches"
1720#define FORMAT_TARGETS 	"_tables_targets"
1721
1722#endif /* CONFIG_PROC_FS */
1723
1724/**
1725 * xt_hook_ops_alloc - set up hooks for a new table
1726 * @table:	table with metadata needed to set up hooks
1727 * @fn:		Hook function
1728 *
1729 * This function will create the nf_hook_ops that the x_table needs
1730 * to hand to xt_hook_link_net().
1731 */
1732struct nf_hook_ops *
1733xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1734{
1735	unsigned int hook_mask = table->valid_hooks;
1736	uint8_t i, num_hooks = hweight32(hook_mask);
1737	uint8_t hooknum;
1738	struct nf_hook_ops *ops;
1739
1740	if (!num_hooks)
1741		return ERR_PTR(-EINVAL);
1742
1743	ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1744	if (ops == NULL)
1745		return ERR_PTR(-ENOMEM);
1746
1747	for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1748	     hook_mask >>= 1, ++hooknum) {
1749		if (!(hook_mask & 1))
1750			continue;
1751		ops[i].hook     = fn;
1752		ops[i].pf       = table->af;
1753		ops[i].hooknum  = hooknum;
1754		ops[i].priority = table->priority;
1755		++i;
1756	}
1757
1758	return ops;
1759}
1760EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1761
1762int xt_register_template(const struct xt_table *table,
1763			 int (*table_init)(struct net *net))
1764{
1765	int ret = -EEXIST, af = table->af;
1766	struct xt_template *t;
1767
1768	mutex_lock(&xt[af].mutex);
1769
1770	list_for_each_entry(t, &xt_templates[af], list) {
1771		if (WARN_ON_ONCE(strcmp(table->name, t->name) == 0))
1772			goto out_unlock;
1773	}
1774
1775	ret = -ENOMEM;
1776	t = kzalloc(sizeof(*t), GFP_KERNEL);
1777	if (!t)
1778		goto out_unlock;
1779
1780	BUILD_BUG_ON(sizeof(t->name) != sizeof(table->name));
1781
1782	strscpy(t->name, table->name, sizeof(t->name));
1783	t->table_init = table_init;
1784	t->me = table->me;
1785	list_add(&t->list, &xt_templates[af]);
1786	ret = 0;
1787out_unlock:
1788	mutex_unlock(&xt[af].mutex);
1789	return ret;
1790}
1791EXPORT_SYMBOL_GPL(xt_register_template);
1792
1793void xt_unregister_template(const struct xt_table *table)
1794{
1795	struct xt_template *t;
1796	int af = table->af;
1797
1798	mutex_lock(&xt[af].mutex);
1799	list_for_each_entry(t, &xt_templates[af], list) {
1800		if (strcmp(table->name, t->name))
1801			continue;
1802
1803		list_del(&t->list);
1804		mutex_unlock(&xt[af].mutex);
1805		kfree(t);
1806		return;
1807	}
1808
1809	mutex_unlock(&xt[af].mutex);
1810	WARN_ON_ONCE(1);
1811}
1812EXPORT_SYMBOL_GPL(xt_unregister_template);
1813
1814int xt_proto_init(struct net *net, u_int8_t af)
1815{
1816#ifdef CONFIG_PROC_FS
1817	char buf[XT_FUNCTION_MAXNAMELEN];
1818	struct proc_dir_entry *proc;
1819	kuid_t root_uid;
1820	kgid_t root_gid;
1821#endif
1822
1823	if (af >= ARRAY_SIZE(xt_prefix))
1824		return -EINVAL;
1825
1826
1827#ifdef CONFIG_PROC_FS
1828	root_uid = make_kuid(net->user_ns, 0);
1829	root_gid = make_kgid(net->user_ns, 0);
1830
1831	strscpy(buf, xt_prefix[af], sizeof(buf));
1832	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1833	proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops,
1834			sizeof(struct seq_net_private),
1835			(void *)(unsigned long)af);
1836	if (!proc)
1837		goto out;
1838	if (uid_valid(root_uid) && gid_valid(root_gid))
1839		proc_set_user(proc, root_uid, root_gid);
1840
1841	strscpy(buf, xt_prefix[af], sizeof(buf));
1842	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1843	proc = proc_create_seq_private(buf, 0440, net->proc_net,
1844			&xt_match_seq_ops, sizeof(struct nf_mttg_trav),
1845			(void *)(unsigned long)af);
1846	if (!proc)
1847		goto out_remove_tables;
1848	if (uid_valid(root_uid) && gid_valid(root_gid))
1849		proc_set_user(proc, root_uid, root_gid);
1850
1851	strscpy(buf, xt_prefix[af], sizeof(buf));
1852	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1853	proc = proc_create_seq_private(buf, 0440, net->proc_net,
1854			 &xt_target_seq_ops, sizeof(struct nf_mttg_trav),
1855			 (void *)(unsigned long)af);
1856	if (!proc)
1857		goto out_remove_matches;
1858	if (uid_valid(root_uid) && gid_valid(root_gid))
1859		proc_set_user(proc, root_uid, root_gid);
1860#endif
1861
1862	return 0;
1863
1864#ifdef CONFIG_PROC_FS
1865out_remove_matches:
1866	strscpy(buf, xt_prefix[af], sizeof(buf));
1867	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1868	remove_proc_entry(buf, net->proc_net);
1869
1870out_remove_tables:
1871	strscpy(buf, xt_prefix[af], sizeof(buf));
1872	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1873	remove_proc_entry(buf, net->proc_net);
1874out:
1875	return -1;
1876#endif
1877}
1878EXPORT_SYMBOL_GPL(xt_proto_init);
1879
1880void xt_proto_fini(struct net *net, u_int8_t af)
1881{
1882#ifdef CONFIG_PROC_FS
1883	char buf[XT_FUNCTION_MAXNAMELEN];
1884
1885	strscpy(buf, xt_prefix[af], sizeof(buf));
1886	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1887	remove_proc_entry(buf, net->proc_net);
1888
1889	strscpy(buf, xt_prefix[af], sizeof(buf));
1890	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1891	remove_proc_entry(buf, net->proc_net);
1892
1893	strscpy(buf, xt_prefix[af], sizeof(buf));
1894	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1895	remove_proc_entry(buf, net->proc_net);
1896#endif /*CONFIG_PROC_FS*/
1897}
1898EXPORT_SYMBOL_GPL(xt_proto_fini);
1899
1900/**
1901 * xt_percpu_counter_alloc - allocate x_tables rule counter
1902 *
1903 * @state: pointer to xt_percpu allocation state
1904 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1905 *
1906 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1907 * contain the address of the real (percpu) counter.
1908 *
1909 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1910 * to fetch the real percpu counter.
1911 *
1912 * To speed up allocation and improve data locality, a 4kb block is
1913 * allocated.  Freeing any counter may free an entire block, so all
1914 * counters allocated using the same state must be freed at the same
1915 * time.
1916 *
1917 * xt_percpu_counter_alloc_state contains the base address of the
1918 * allocated page and the current sub-offset.
1919 *
1920 * returns false on error.
1921 */
1922bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1923			     struct xt_counters *counter)
1924{
1925	BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1926
1927	if (nr_cpu_ids <= 1)
1928		return true;
1929
1930	if (!state->mem) {
1931		state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1932					    XT_PCPU_BLOCK_SIZE);
1933		if (!state->mem)
1934			return false;
1935	}
1936	counter->pcnt = (__force unsigned long)(state->mem + state->off);
1937	state->off += sizeof(*counter);
1938	if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1939		state->mem = NULL;
1940		state->off = 0;
1941	}
1942	return true;
1943}
1944EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1945
1946void xt_percpu_counter_free(struct xt_counters *counters)
1947{
1948	unsigned long pcnt = counters->pcnt;
1949
1950	if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1951		free_percpu((void __percpu *)pcnt);
1952}
1953EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1954
1955static int __net_init xt_net_init(struct net *net)
1956{
1957	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1958	int i;
1959
1960	for (i = 0; i < NFPROTO_NUMPROTO; i++)
1961		INIT_LIST_HEAD(&xt_net->tables[i]);
1962	return 0;
1963}
1964
1965static void __net_exit xt_net_exit(struct net *net)
1966{
1967	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1968	int i;
1969
1970	for (i = 0; i < NFPROTO_NUMPROTO; i++)
1971		WARN_ON_ONCE(!list_empty(&xt_net->tables[i]));
1972}
1973
1974static struct pernet_operations xt_net_ops = {
1975	.init = xt_net_init,
1976	.exit = xt_net_exit,
1977	.id   = &xt_pernet_id,
1978	.size = sizeof(struct xt_pernet),
1979};
1980
1981static int __init xt_init(void)
1982{
1983	unsigned int i;
1984	int rv;
1985
1986	for_each_possible_cpu(i) {
1987		seqcount_init(&per_cpu(xt_recseq, i));
1988	}
1989
1990	xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
1991	if (!xt)
1992		return -ENOMEM;
1993
1994	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1995		mutex_init(&xt[i].mutex);
1996#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1997		mutex_init(&xt[i].compat_mutex);
1998		xt[i].compat_tab = NULL;
1999#endif
2000		INIT_LIST_HEAD(&xt[i].target);
2001		INIT_LIST_HEAD(&xt[i].match);
2002		INIT_LIST_HEAD(&xt_templates[i]);
2003	}
2004	rv = register_pernet_subsys(&xt_net_ops);
2005	if (rv < 0)
2006		kfree(xt);
2007	return rv;
2008}
2009
2010static void __exit xt_fini(void)
2011{
2012	unregister_pernet_subsys(&xt_net_ops);
2013	kfree(xt);
2014}
2015
2016module_init(xt_init);
2017module_exit(xt_fini);