File Index
Files and directories
Sign in
FILE

x_tables.c

File preview with syntax highlighting.

linux-6.19.14/net/netfilter/x_tables.c C
   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
 505	/* NFPROTO_UNSPEC implies NF_INET_* hooks which do not overlap with
 506	 * NF_ARP_IN,OUT,FORWARD, allow explicit extensions with NFPROTO_ARP
 507	 * support.
 508	 */
 509	if (par->family == NFPROTO_ARP &&
 510	    par->match->family != NFPROTO_ARP) {
 511		pr_info_ratelimited("%s_tables: %s match: not valid for this family\n",
 512				    xt_prefix[par->family], par->match->name);
 513		return -EINVAL;
 514	}
 515	if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
 516		char used[64], allow[64];
 517
 518		pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
 519				    xt_prefix[par->family], par->match->name,
 520				    textify_hooks(used, sizeof(used),
 521						  par->hook_mask, par->family),
 522				    textify_hooks(allow, sizeof(allow),
 523						  par->match->hooks,
 524						  par->family));
 525		return -EINVAL;
 526	}
 527	if (par->match->proto && (par->match->proto != proto || inv_proto)) {
 528		pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
 529				    xt_prefix[par->family], par->match->name,
 530				    par->match->proto);
 531		return -EINVAL;
 532	}
 533	if (par->match->checkentry != NULL) {
 534		ret = par->match->checkentry(par);
 535		if (ret < 0)
 536			return ret;
 537		else if (ret > 0)
 538			/* Flag up potential errors. */
 539			return -EIO;
 540	}
 541	return 0;
 542}
 543EXPORT_SYMBOL_GPL(xt_check_match);
 544
 545/** xt_check_entry_match - check that matches end before start of target
 546 *
 547 * @match: beginning of xt_entry_match
 548 * @target: beginning of this rules target (alleged end of matches)
 549 * @alignment: alignment requirement of match structures
 550 *
 551 * Validates that all matches add up to the beginning of the target,
 552 * and that each match covers at least the base structure size.
 553 *
 554 * Return: 0 on success, negative errno on failure.
 555 */
 556static int xt_check_entry_match(const char *match, const char *target,
 557				const size_t alignment)
 558{
 559	const struct xt_entry_match *pos;
 560	int length = target - match;
 561
 562	if (length == 0) /* no matches */
 563		return 0;
 564
 565	pos = (struct xt_entry_match *)match;
 566	do {
 567		if ((unsigned long)pos % alignment)
 568			return -EINVAL;
 569
 570		if (length < (int)sizeof(struct xt_entry_match))
 571			return -EINVAL;
 572
 573		if (pos->u.match_size < sizeof(struct xt_entry_match))
 574			return -EINVAL;
 575
 576		if (pos->u.match_size > length)
 577			return -EINVAL;
 578
 579		length -= pos->u.match_size;
 580		pos = ((void *)((char *)(pos) + (pos)->u.match_size));
 581	} while (length > 0);
 582
 583	return 0;
 584}
 585
 586/** xt_check_table_hooks - check hook entry points are sane
 587 *
 588 * @info xt_table_info to check
 589 * @valid_hooks - hook entry points that we can enter from
 590 *
 591 * Validates that the hook entry and underflows points are set up.
 592 *
 593 * Return: 0 on success, negative errno on failure.
 594 */
 595int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
 596{
 597	const char *err = "unsorted underflow";
 598	unsigned int i, max_uflow, max_entry;
 599	bool check_hooks = false;
 600
 601	BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
 602
 603	max_entry = 0;
 604	max_uflow = 0;
 605
 606	for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
 607		if (!(valid_hooks & (1 << i)))
 608			continue;
 609
 610		if (info->hook_entry[i] == 0xFFFFFFFF)
 611			return -EINVAL;
 612		if (info->underflow[i] == 0xFFFFFFFF)
 613			return -EINVAL;
 614
 615		if (check_hooks) {
 616			if (max_uflow > info->underflow[i])
 617				goto error;
 618
 619			if (max_uflow == info->underflow[i]) {
 620				err = "duplicate underflow";
 621				goto error;
 622			}
 623			if (max_entry > info->hook_entry[i]) {
 624				err = "unsorted entry";
 625				goto error;
 626			}
 627			if (max_entry == info->hook_entry[i]) {
 628				err = "duplicate entry";
 629				goto error;
 630			}
 631		}
 632		max_entry = info->hook_entry[i];
 633		max_uflow = info->underflow[i];
 634		check_hooks = true;
 635	}
 636
 637	return 0;
 638error:
 639	pr_err_ratelimited("%s at hook %d\n", err, i);
 640	return -EINVAL;
 641}
 642EXPORT_SYMBOL(xt_check_table_hooks);
 643
 644static bool verdict_ok(int verdict)
 645{
 646	if (verdict > 0)
 647		return true;
 648
 649	if (verdict < 0) {
 650		int v = -verdict - 1;
 651
 652		if (verdict == XT_RETURN)
 653			return true;
 654
 655		switch (v) {
 656		case NF_ACCEPT: return true;
 657		case NF_DROP: return true;
 658		case NF_QUEUE: return true;
 659		default:
 660			break;
 661		}
 662
 663		return false;
 664	}
 665
 666	return false;
 667}
 668
 669static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
 670			const char *msg, unsigned int msglen)
 671{
 672	return usersize == kernsize && strnlen(msg, msglen) < msglen;
 673}
 674
 675#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
 676int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
 677{
 678	struct xt_af *xp = &xt[af];
 679
 680	WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
 681
 682	if (WARN_ON(!xp->compat_tab))
 683		return -ENOMEM;
 684
 685	if (xp->cur >= xp->number)
 686		return -EINVAL;
 687
 688	if (xp->cur)
 689		delta += xp->compat_tab[xp->cur - 1].delta;
 690	xp->compat_tab[xp->cur].offset = offset;
 691	xp->compat_tab[xp->cur].delta = delta;
 692	xp->cur++;
 693	return 0;
 694}
 695EXPORT_SYMBOL_GPL(xt_compat_add_offset);
 696
 697void xt_compat_flush_offsets(u_int8_t af)
 698{
 699	WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
 700
 701	if (xt[af].compat_tab) {
 702		vfree(xt[af].compat_tab);
 703		xt[af].compat_tab = NULL;
 704		xt[af].number = 0;
 705		xt[af].cur = 0;
 706	}
 707}
 708EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
 709
 710int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
 711{
 712	struct compat_delta *tmp = xt[af].compat_tab;
 713	int mid, left = 0, right = xt[af].cur - 1;
 714
 715	while (left <= right) {
 716		mid = (left + right) >> 1;
 717		if (offset > tmp[mid].offset)
 718			left = mid + 1;
 719		else if (offset < tmp[mid].offset)
 720			right = mid - 1;
 721		else
 722			return mid ? tmp[mid - 1].delta : 0;
 723	}
 724	return left ? tmp[left - 1].delta : 0;
 725}
 726EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
 727
 728int xt_compat_init_offsets(u8 af, unsigned int number)
 729{
 730	size_t mem;
 731
 732	WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
 733
 734	if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
 735		return -EINVAL;
 736
 737	if (WARN_ON(xt[af].compat_tab))
 738		return -EINVAL;
 739
 740	mem = sizeof(struct compat_delta) * number;
 741	if (mem > XT_MAX_TABLE_SIZE)
 742		return -ENOMEM;
 743
 744	xt[af].compat_tab = vmalloc(mem);
 745	if (!xt[af].compat_tab)
 746		return -ENOMEM;
 747
 748	xt[af].number = number;
 749	xt[af].cur = 0;
 750
 751	return 0;
 752}
 753EXPORT_SYMBOL(xt_compat_init_offsets);
 754
 755int xt_compat_match_offset(const struct xt_match *match)
 756{
 757	u_int16_t csize = match->compatsize ? : match->matchsize;
 758	return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
 759}
 760EXPORT_SYMBOL_GPL(xt_compat_match_offset);
 761
 762void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
 763			       unsigned int *size)
 764{
 765	const struct xt_match *match = m->u.kernel.match;
 766	struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
 767	int off = xt_compat_match_offset(match);
 768	u_int16_t msize = cm->u.user.match_size;
 769	char name[sizeof(m->u.user.name)];
 770
 771	m = *dstptr;
 772	memcpy(m, cm, sizeof(*cm));
 773	if (match->compat_from_user)
 774		match->compat_from_user(m->data, cm->data);
 775	else
 776		memcpy(m->data, cm->data, msize - sizeof(*cm));
 777
 778	msize += off;
 779	m->u.user.match_size = msize;
 780	strscpy(name, match->name, sizeof(name));
 781	module_put(match->me);
 782	strscpy_pad(m->u.user.name, name, sizeof(m->u.user.name));
 783
 784	*size += off;
 785	*dstptr += msize;
 786}
 787EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
 788
 789#define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE)			\
 790	xt_data_to_user(U->data, K->data,				\
 791			K->u.kernel.TYPE->usersize,			\
 792			C_SIZE,						\
 793			COMPAT_XT_ALIGN(C_SIZE))
 794
 795int xt_compat_match_to_user(const struct xt_entry_match *m,
 796			    void __user **dstptr, unsigned int *size)
 797{
 798	const struct xt_match *match = m->u.kernel.match;
 799	struct compat_xt_entry_match __user *cm = *dstptr;
 800	int off = xt_compat_match_offset(match);
 801	u_int16_t msize = m->u.user.match_size - off;
 802
 803	if (XT_OBJ_TO_USER(cm, m, match, msize))
 804		return -EFAULT;
 805
 806	if (match->compat_to_user) {
 807		if (match->compat_to_user((void __user *)cm->data, m->data))
 808			return -EFAULT;
 809	} else {
 810		if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
 811			return -EFAULT;
 812	}
 813
 814	*size -= off;
 815	*dstptr += msize;
 816	return 0;
 817}
 818EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
 819
 820/* non-compat version may have padding after verdict */
 821struct compat_xt_standard_target {
 822	struct compat_xt_entry_target t;
 823	compat_uint_t verdict;
 824};
 825
 826struct compat_xt_error_target {
 827	struct compat_xt_entry_target t;
 828	char errorname[XT_FUNCTION_MAXNAMELEN];
 829};
 830
 831int xt_compat_check_entry_offsets(const void *base, const char *elems,
 832				  unsigned int target_offset,
 833				  unsigned int next_offset)
 834{
 835	long size_of_base_struct = elems - (const char *)base;
 836	const struct compat_xt_entry_target *t;
 837	const char *e = base;
 838
 839	if (target_offset < size_of_base_struct)
 840		return -EINVAL;
 841
 842	if (target_offset + sizeof(*t) > next_offset)
 843		return -EINVAL;
 844
 845	t = (void *)(e + target_offset);
 846	if (t->u.target_size < sizeof(*t))
 847		return -EINVAL;
 848
 849	if (target_offset + t->u.target_size > next_offset)
 850		return -EINVAL;
 851
 852	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
 853		const struct compat_xt_standard_target *st = (const void *)t;
 854
 855		if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
 856			return -EINVAL;
 857
 858		if (!verdict_ok(st->verdict))
 859			return -EINVAL;
 860	} else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
 861		const struct compat_xt_error_target *et = (const void *)t;
 862
 863		if (!error_tg_ok(t->u.target_size, sizeof(*et),
 864				 et->errorname, sizeof(et->errorname)))
 865			return -EINVAL;
 866	}
 867
 868	/* compat_xt_entry match has less strict alignment requirements,
 869	 * otherwise they are identical.  In case of padding differences
 870	 * we need to add compat version of xt_check_entry_match.
 871	 */
 872	BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
 873
 874	return xt_check_entry_match(elems, base + target_offset,
 875				    __alignof__(struct compat_xt_entry_match));
 876}
 877EXPORT_SYMBOL(xt_compat_check_entry_offsets);
 878#endif /* CONFIG_NETFILTER_XTABLES_COMPAT */
 879
 880/**
 881 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
 882 *
 883 * @base: pointer to arp/ip/ip6t_entry
 884 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
 885 * @target_offset: the arp/ip/ip6_t->target_offset
 886 * @next_offset: the arp/ip/ip6_t->next_offset
 887 *
 888 * validates that target_offset and next_offset are sane and that all
 889 * match sizes (if any) align with the target offset.
 890 *
 891 * This function does not validate the targets or matches themselves, it
 892 * only tests that all the offsets and sizes are correct, that all
 893 * match structures are aligned, and that the last structure ends where
 894 * the target structure begins.
 895 *
 896 * Also see xt_compat_check_entry_offsets for CONFIG_NETFILTER_XTABLES_COMPAT version.
 897 *
 898 * The arp/ip/ip6t_entry structure @base must have passed following tests:
 899 * - it must point to a valid memory location
 900 * - base to base + next_offset must be accessible, i.e. not exceed allocated
 901 *   length.
 902 *
 903 * A well-formed entry looks like this:
 904 *
 905 * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
 906 * e->elems[]-----'                              |               |
 907 *                matchsize                      |               |
 908 *                                matchsize      |               |
 909 *                                               |               |
 910 * target_offset---------------------------------'               |
 911 * next_offset---------------------------------------------------'
 912 *
 913 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
 914 *          This is where matches (if any) and the target reside.
 915 * target_offset: beginning of target.
 916 * next_offset: start of the next rule; also: size of this rule.
 917 * Since targets have a minimum size, target_offset + minlen <= next_offset.
 918 *
 919 * Every match stores its size, sum of sizes must not exceed target_offset.
 920 *
 921 * Return: 0 on success, negative errno on failure.
 922 */
 923int xt_check_entry_offsets(const void *base,
 924			   const char *elems,
 925			   unsigned int target_offset,
 926			   unsigned int next_offset)
 927{
 928	long size_of_base_struct = elems - (const char *)base;
 929	const struct xt_entry_target *t;
 930	const char *e = base;
 931
 932	/* target start is within the ip/ip6/arpt_entry struct */
 933	if (target_offset < size_of_base_struct)
 934		return -EINVAL;
 935
 936	if (target_offset + sizeof(*t) > next_offset)
 937		return -EINVAL;
 938
 939	t = (void *)(e + target_offset);
 940	if (t->u.target_size < sizeof(*t))
 941		return -EINVAL;
 942
 943	if (target_offset + t->u.target_size > next_offset)
 944		return -EINVAL;
 945
 946	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
 947		const struct xt_standard_target *st = (const void *)t;
 948
 949		if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
 950			return -EINVAL;
 951
 952		if (!verdict_ok(st->verdict))
 953			return -EINVAL;
 954	} else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
 955		const struct xt_error_target *et = (const void *)t;
 956
 957		if (!error_tg_ok(t->u.target_size, sizeof(*et),
 958				 et->errorname, sizeof(et->errorname)))
 959			return -EINVAL;
 960	}
 961
 962	return xt_check_entry_match(elems, base + target_offset,
 963				    __alignof__(struct xt_entry_match));
 964}
 965EXPORT_SYMBOL(xt_check_entry_offsets);
 966
 967/**
 968 * xt_alloc_entry_offsets - allocate array to store rule head offsets
 969 *
 970 * @size: number of entries
 971 *
 972 * Return: NULL or zeroed kmalloc'd or vmalloc'd array
 973 */
 974unsigned int *xt_alloc_entry_offsets(unsigned int size)
 975{
 976	if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
 977		return NULL;
 978
 979	return kvcalloc(size, sizeof(unsigned int), GFP_KERNEL);
 980
 981}
 982EXPORT_SYMBOL(xt_alloc_entry_offsets);
 983
 984/**
 985 * xt_find_jump_offset - check if target is a valid jump offset
 986 *
 987 * @offsets: array containing all valid rule start offsets of a rule blob
 988 * @target: the jump target to search for
 989 * @size: entries in @offset
 990 */
 991bool xt_find_jump_offset(const unsigned int *offsets,
 992			 unsigned int target, unsigned int size)
 993{
 994	int m, low = 0, hi = size;
 995
 996	while (hi > low) {
 997		m = (low + hi) / 2u;
 998
 999		if (offsets[m] > target)
1000			hi = m;
1001		else if (offsets[m] < target)
1002			low = m + 1;
1003		else
1004			return true;
1005	}
1006
1007	return false;
1008}
1009EXPORT_SYMBOL(xt_find_jump_offset);
1010
1011int xt_check_target(struct xt_tgchk_param *par,
1012		    unsigned int size, u16 proto, bool inv_proto)
1013{
1014	int ret;
1015
1016	if (XT_ALIGN(par->target->targetsize) != size) {
1017		pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
1018				   xt_prefix[par->family], par->target->name,
1019				   par->target->revision,
1020				   XT_ALIGN(par->target->targetsize), size);
1021		return -EINVAL;
1022	}
1023	if (par->target->table != NULL &&
1024	    strcmp(par->target->table, par->table) != 0) {
1025		pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
1026				    xt_prefix[par->family], par->target->name,
1027				    par->target->table, par->table);
1028		return -EINVAL;
1029	}
1030
1031	/* NFPROTO_UNSPEC implies NF_INET_* hooks which do not overlap with
1032	 * NF_ARP_IN,OUT,FORWARD, allow explicit extensions with NFPROTO_ARP
1033	 * support.
1034	 */
1035	if (par->family == NFPROTO_ARP &&
1036	    par->target->family != NFPROTO_ARP) {
1037		pr_info_ratelimited("%s_tables: %s target: not valid for this family\n",
1038				    xt_prefix[par->family], par->target->name);
1039		return -EINVAL;
1040	}
1041
1042	if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
1043		char used[64], allow[64];
1044
1045		pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1046				    xt_prefix[par->family], par->target->name,
1047				    textify_hooks(used, sizeof(used),
1048						  par->hook_mask, par->family),
1049				    textify_hooks(allow, sizeof(allow),
1050						  par->target->hooks,
1051						  par->family));
1052		return -EINVAL;
1053	}
1054	if (par->target->proto && (par->target->proto != proto || inv_proto)) {
1055		pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1056				    xt_prefix[par->family], par->target->name,
1057				    par->target->proto);
1058		return -EINVAL;
1059	}
1060	if (par->target->checkentry != NULL) {
1061		ret = par->target->checkentry(par);
1062		if (ret < 0)
1063			return ret;
1064		else if (ret > 0)
1065			/* Flag up potential errors. */
1066			return -EIO;
1067	}
1068	return 0;
1069}
1070EXPORT_SYMBOL_GPL(xt_check_target);
1071
1072/**
1073 * xt_copy_counters - copy counters and metadata from a sockptr_t
1074 *
1075 * @arg: src sockptr
1076 * @len: alleged size of userspace memory
1077 * @info: where to store the xt_counters_info metadata
1078 *
1079 * Copies counter meta data from @user and stores it in @info.
1080 *
1081 * vmallocs memory to hold the counters, then copies the counter data
1082 * from @user to the new memory and returns a pointer to it.
1083 *
1084 * If called from a compat syscall, @info gets converted automatically to the
1085 * 64bit representation.
1086 *
1087 * The metadata associated with the counters is stored in @info.
1088 *
1089 * Return: returns pointer that caller has to test via IS_ERR().
1090 * If IS_ERR is false, caller has to vfree the pointer.
1091 */
1092void *xt_copy_counters(sockptr_t arg, unsigned int len,
1093		       struct xt_counters_info *info)
1094{
1095	size_t offset;
1096	void *mem;
1097	u64 size;
1098
1099#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1100	if (in_compat_syscall()) {
1101		/* structures only differ in size due to alignment */
1102		struct compat_xt_counters_info compat_tmp;
1103
1104		if (len <= sizeof(compat_tmp))
1105			return ERR_PTR(-EINVAL);
1106
1107		len -= sizeof(compat_tmp);
1108		if (copy_from_sockptr(&compat_tmp, arg, sizeof(compat_tmp)) != 0)
1109			return ERR_PTR(-EFAULT);
1110
1111		memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
1112		info->num_counters = compat_tmp.num_counters;
1113		offset = sizeof(compat_tmp);
1114	} else
1115#endif
1116	{
1117		if (len <= sizeof(*info))
1118			return ERR_PTR(-EINVAL);
1119
1120		len -= sizeof(*info);
1121		if (copy_from_sockptr(info, arg, sizeof(*info)) != 0)
1122			return ERR_PTR(-EFAULT);
1123
1124		offset = sizeof(*info);
1125	}
1126	info->name[sizeof(info->name) - 1] = '\0';
1127
1128	size = sizeof(struct xt_counters);
1129	size *= info->num_counters;
1130
1131	if (size != (u64)len)
1132		return ERR_PTR(-EINVAL);
1133
1134	mem = vmalloc(len);
1135	if (!mem)
1136		return ERR_PTR(-ENOMEM);
1137
1138	if (copy_from_sockptr_offset(mem, arg, offset, len) == 0)
1139		return mem;
1140
1141	vfree(mem);
1142	return ERR_PTR(-EFAULT);
1143}
1144EXPORT_SYMBOL_GPL(xt_copy_counters);
1145
1146#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1147int xt_compat_target_offset(const struct xt_target *target)
1148{
1149	u_int16_t csize = target->compatsize ? : target->targetsize;
1150	return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
1151}
1152EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1153
1154void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
1155				unsigned int *size)
1156{
1157	const struct xt_target *target = t->u.kernel.target;
1158	struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1159	int off = xt_compat_target_offset(target);
1160	u_int16_t tsize = ct->u.user.target_size;
1161	char name[sizeof(t->u.user.name)];
1162
1163	t = *dstptr;
1164	memcpy(t, ct, sizeof(*ct));
1165	if (target->compat_from_user)
1166		target->compat_from_user(t->data, ct->data);
1167	else
1168		unsafe_memcpy(t->data, ct->data, tsize - sizeof(*ct),
1169			      /* UAPI 0-sized destination */);
1170
1171	tsize += off;
1172	t->u.user.target_size = tsize;
1173	strscpy(name, target->name, sizeof(name));
1174	module_put(target->me);
1175	strscpy_pad(t->u.user.name, name, sizeof(t->u.user.name));
1176
1177	*size += off;
1178	*dstptr += tsize;
1179}
1180EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1181
1182int xt_compat_target_to_user(const struct xt_entry_target *t,
1183			     void __user **dstptr, unsigned int *size)
1184{
1185	const struct xt_target *target = t->u.kernel.target;
1186	struct compat_xt_entry_target __user *ct = *dstptr;
1187	int off = xt_compat_target_offset(target);
1188	u_int16_t tsize = t->u.user.target_size - off;
1189
1190	if (XT_OBJ_TO_USER(ct, t, target, tsize))
1191		return -EFAULT;
1192
1193	if (target->compat_to_user) {
1194		if (target->compat_to_user((void __user *)ct->data, t->data))
1195			return -EFAULT;
1196	} else {
1197		if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1198			return -EFAULT;
1199	}
1200
1201	*size -= off;
1202	*dstptr += tsize;
1203	return 0;
1204}
1205EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1206#endif
1207
1208struct xt_table_info *xt_alloc_table_info(unsigned int size)
1209{
1210	struct xt_table_info *info = NULL;
1211	size_t sz = sizeof(*info) + size;
1212
1213	if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1214		return NULL;
1215
1216	info = kvmalloc(sz, GFP_KERNEL_ACCOUNT);
1217	if (!info)
1218		return NULL;
1219
1220	memset(info, 0, sizeof(*info));
1221	info->size = size;
1222	return info;
1223}
1224EXPORT_SYMBOL(xt_alloc_table_info);
1225
1226void xt_free_table_info(struct xt_table_info *info)
1227{
1228	int cpu;
1229
1230	if (info->jumpstack != NULL) {
1231		for_each_possible_cpu(cpu)
1232			kvfree(info->jumpstack[cpu]);
1233		kvfree(info->jumpstack);
1234	}
1235
1236	kvfree(info);
1237}
1238EXPORT_SYMBOL(xt_free_table_info);
1239
1240struct xt_table *xt_find_table(struct net *net, u8 af, const char *name)
1241{
1242	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1243	struct xt_table *t;
1244
1245	mutex_lock(&xt[af].mutex);
1246	list_for_each_entry(t, &xt_net->tables[af], list) {
1247		if (strcmp(t->name, name) == 0) {
1248			mutex_unlock(&xt[af].mutex);
1249			return t;
1250		}
1251	}
1252	mutex_unlock(&xt[af].mutex);
1253	return NULL;
1254}
1255EXPORT_SYMBOL(xt_find_table);
1256
1257/* Find table by name, grabs mutex & ref.  Returns ERR_PTR on error. */
1258struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1259				    const char *name)
1260{
1261	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1262	struct module *owner = NULL;
1263	struct xt_template *tmpl;
1264	struct xt_table *t;
1265
1266	mutex_lock(&xt[af].mutex);
1267	list_for_each_entry(t, &xt_net->tables[af], list)
1268		if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1269			return t;
1270
1271	/* Table doesn't exist in this netns, check larval list */
1272	list_for_each_entry(tmpl, &xt_templates[af], list) {
1273		int err;
1274
1275		if (strcmp(tmpl->name, name))
1276			continue;
1277		if (!try_module_get(tmpl->me))
1278			goto out;
1279
1280		owner = tmpl->me;
1281
1282		mutex_unlock(&xt[af].mutex);
1283		err = tmpl->table_init(net);
1284		if (err < 0) {
1285			module_put(owner);
1286			return ERR_PTR(err);
1287		}
1288
1289		mutex_lock(&xt[af].mutex);
1290		break;
1291	}
1292
1293	/* and once again: */
1294	list_for_each_entry(t, &xt_net->tables[af], list)
1295		if (strcmp(t->name, name) == 0 && owner == t->me)
1296			return t;
1297
1298	module_put(owner);
1299 out:
1300	mutex_unlock(&xt[af].mutex);
1301	return ERR_PTR(-ENOENT);
1302}
1303EXPORT_SYMBOL_GPL(xt_find_table_lock);
1304
1305struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1306					    const char *name)
1307{
1308	struct xt_table *t = xt_find_table_lock(net, af, name);
1309
1310#ifdef CONFIG_MODULES
1311	if (IS_ERR(t)) {
1312		int err = request_module("%stable_%s", xt_prefix[af], name);
1313		if (err < 0)
1314			return ERR_PTR(err);
1315		t = xt_find_table_lock(net, af, name);
1316	}
1317#endif
1318
1319	return t;
1320}
1321EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1322
1323void xt_table_unlock(struct xt_table *table)
1324{
1325	mutex_unlock(&xt[table->af].mutex);
1326}
1327EXPORT_SYMBOL_GPL(xt_table_unlock);
1328
1329#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1330void xt_compat_lock(u_int8_t af)
1331{
1332	mutex_lock(&xt[af].compat_mutex);
1333}
1334EXPORT_SYMBOL_GPL(xt_compat_lock);
1335
1336void xt_compat_unlock(u_int8_t af)
1337{
1338	mutex_unlock(&xt[af].compat_mutex);
1339}
1340EXPORT_SYMBOL_GPL(xt_compat_unlock);
1341#endif
1342
1343struct static_key xt_tee_enabled __read_mostly;
1344EXPORT_SYMBOL_GPL(xt_tee_enabled);
1345
1346#ifdef CONFIG_NETFILTER_XTABLES_LEGACY
1347DEFINE_PER_CPU(seqcount_t, xt_recseq);
1348EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1349
1350static int xt_jumpstack_alloc(struct xt_table_info *i)
1351{
1352	unsigned int size;
1353	int cpu;
1354
1355	size = sizeof(void **) * nr_cpu_ids;
1356	if (size > PAGE_SIZE)
1357		i->jumpstack = kvzalloc(size, GFP_KERNEL);
1358	else
1359		i->jumpstack = kzalloc(size, GFP_KERNEL);
1360	if (i->jumpstack == NULL)
1361		return -ENOMEM;
1362
1363	/* ruleset without jumps -- no stack needed */
1364	if (i->stacksize == 0)
1365		return 0;
1366
1367	/* Jumpstack needs to be able to record two full callchains, one
1368	 * from the first rule set traversal, plus one table reentrancy
1369	 * via -j TEE without clobbering the callchain that brought us to
1370	 * TEE target.
1371	 *
1372	 * This is done by allocating two jumpstacks per cpu, on reentry
1373	 * the upper half of the stack is used.
1374	 *
1375	 * see the jumpstack setup in ipt_do_table() for more details.
1376	 */
1377	size = sizeof(void *) * i->stacksize * 2u;
1378	for_each_possible_cpu(cpu) {
1379		i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1380			cpu_to_node(cpu));
1381		if (i->jumpstack[cpu] == NULL)
1382			/*
1383			 * Freeing will be done later on by the callers. The
1384			 * chain is: xt_replace_table -> __do_replace ->
1385			 * do_replace -> xt_free_table_info.
1386			 */
1387			return -ENOMEM;
1388	}
1389
1390	return 0;
1391}
1392
1393struct xt_counters *xt_counters_alloc(unsigned int counters)
1394{
1395	struct xt_counters *mem;
1396
1397	if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1398		return NULL;
1399
1400	counters *= sizeof(*mem);
1401	if (counters > XT_MAX_TABLE_SIZE)
1402		return NULL;
1403
1404	return vzalloc(counters);
1405}
1406EXPORT_SYMBOL(xt_counters_alloc);
1407
1408struct xt_table_info *
1409xt_replace_table(struct xt_table *table,
1410	      unsigned int num_counters,
1411	      struct xt_table_info *newinfo,
1412	      int *error)
1413{
1414	struct xt_table_info *private;
1415	unsigned int cpu;
1416	int ret;
1417
1418	ret = xt_jumpstack_alloc(newinfo);
1419	if (ret < 0) {
1420		*error = ret;
1421		return NULL;
1422	}
1423
1424	/* Do the substitution. */
1425	local_bh_disable();
1426	private = table->private;
1427
1428	/* Check inside lock: is the old number correct? */
1429	if (num_counters != private->number) {
1430		pr_debug("num_counters != table->private->number (%u/%u)\n",
1431			 num_counters, private->number);
1432		local_bh_enable();
1433		*error = -EAGAIN;
1434		return NULL;
1435	}
1436
1437	newinfo->initial_entries = private->initial_entries;
1438	/*
1439	 * Ensure contents of newinfo are visible before assigning to
1440	 * private.
1441	 */
1442	smp_wmb();
1443	table->private = newinfo;
1444
1445	/* make sure all cpus see new ->private value */
1446	smp_mb();
1447
1448	/*
1449	 * Even though table entries have now been swapped, other CPU's
1450	 * may still be using the old entries...
1451	 */
1452	local_bh_enable();
1453
1454	/* ... so wait for even xt_recseq on all cpus */
1455	for_each_possible_cpu(cpu) {
1456		seqcount_t *s = &per_cpu(xt_recseq, cpu);
1457		u32 seq = raw_read_seqcount(s);
1458
1459		if (seq & 1) {
1460			do {
1461				cond_resched();
1462				cpu_relax();
1463			} while (seq == raw_read_seqcount(s));
1464		}
1465	}
1466
1467	audit_log_nfcfg(table->name, table->af, private->number,
1468			!private->number ? AUDIT_XT_OP_REGISTER :
1469					   AUDIT_XT_OP_REPLACE,
1470			GFP_KERNEL);
1471	return private;
1472}
1473EXPORT_SYMBOL_GPL(xt_replace_table);
1474
1475struct xt_table *xt_register_table(struct net *net,
1476				   const struct xt_table *input_table,
1477				   struct xt_table_info *bootstrap,
1478				   struct xt_table_info *newinfo)
1479{
1480	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1481	struct xt_table_info *private;
1482	struct xt_table *t, *table;
1483	int ret;
1484
1485	/* Don't add one object to multiple lists. */
1486	table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1487	if (!table) {
1488		ret = -ENOMEM;
1489		goto out;
1490	}
1491
1492	mutex_lock(&xt[table->af].mutex);
1493	/* Don't autoload: we'd eat our tail... */
1494	list_for_each_entry(t, &xt_net->tables[table->af], list) {
1495		if (strcmp(t->name, table->name) == 0) {
1496			ret = -EEXIST;
1497			goto unlock;
1498		}
1499	}
1500
1501	/* Simplifies replace_table code. */
1502	table->private = bootstrap;
1503
1504	if (!xt_replace_table(table, 0, newinfo, &ret))
1505		goto unlock;
1506
1507	private = table->private;
1508	pr_debug("table->private->number = %u\n", private->number);
1509
1510	/* save number of initial entries */
1511	private->initial_entries = private->number;
1512
1513	list_add(&table->list, &xt_net->tables[table->af]);
1514	mutex_unlock(&xt[table->af].mutex);
1515	return table;
1516
1517unlock:
1518	mutex_unlock(&xt[table->af].mutex);
1519	kfree(table);
1520out:
1521	return ERR_PTR(ret);
1522}
1523EXPORT_SYMBOL_GPL(xt_register_table);
1524
1525void *xt_unregister_table(struct xt_table *table)
1526{
1527	struct xt_table_info *private;
1528
1529	mutex_lock(&xt[table->af].mutex);
1530	private = table->private;
1531	list_del(&table->list);
1532	mutex_unlock(&xt[table->af].mutex);
1533	audit_log_nfcfg(table->name, table->af, private->number,
1534			AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
1535	kfree(table->ops);
1536	kfree(table);
1537
1538	return private;
1539}
1540EXPORT_SYMBOL_GPL(xt_unregister_table);
1541#endif
1542
1543#ifdef CONFIG_PROC_FS
1544static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1545{
1546	u8 af = (unsigned long)pde_data(file_inode(seq->file));
1547	struct net *net = seq_file_net(seq);
1548	struct xt_pernet *xt_net;
1549
1550	xt_net = net_generic(net, xt_pernet_id);
1551
1552	mutex_lock(&xt[af].mutex);
1553	return seq_list_start(&xt_net->tables[af], *pos);
1554}
1555
1556static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1557{
1558	u8 af = (unsigned long)pde_data(file_inode(seq->file));
1559	struct net *net = seq_file_net(seq);
1560	struct xt_pernet *xt_net;
1561
1562	xt_net = net_generic(net, xt_pernet_id);
1563
1564	return seq_list_next(v, &xt_net->tables[af], pos);
1565}
1566
1567static void xt_table_seq_stop(struct seq_file *seq, void *v)
1568{
1569	u_int8_t af = (unsigned long)pde_data(file_inode(seq->file));
1570
1571	mutex_unlock(&xt[af].mutex);
1572}
1573
1574static int xt_table_seq_show(struct seq_file *seq, void *v)
1575{
1576	struct xt_table *table = list_entry(v, struct xt_table, list);
1577
1578	if (*table->name)
1579		seq_printf(seq, "%s\n", table->name);
1580	return 0;
1581}
1582
1583static const struct seq_operations xt_table_seq_ops = {
1584	.start	= xt_table_seq_start,
1585	.next	= xt_table_seq_next,
1586	.stop	= xt_table_seq_stop,
1587	.show	= xt_table_seq_show,
1588};
1589
1590/*
1591 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1592 * the multi-AF mutexes.
1593 */
1594struct nf_mttg_trav {
1595	struct list_head *head, *curr;
1596	uint8_t class;
1597};
1598
1599enum {
1600	MTTG_TRAV_INIT,
1601	MTTG_TRAV_NFP_UNSPEC,
1602	MTTG_TRAV_NFP_SPEC,
1603	MTTG_TRAV_DONE,
1604};
1605
1606static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1607    bool is_target)
1608{
1609	static const uint8_t next_class[] = {
1610		[MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1611		[MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1612	};
1613	uint8_t nfproto = (unsigned long)pde_data(file_inode(seq->file));
1614	struct nf_mttg_trav *trav = seq->private;
1615
1616	if (ppos != NULL)
1617		++(*ppos);
1618
1619	switch (trav->class) {
1620	case MTTG_TRAV_INIT:
1621		trav->class = MTTG_TRAV_NFP_UNSPEC;
1622		mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1623		trav->head = trav->curr = is_target ?
1624			&xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1625 		break;
1626	case MTTG_TRAV_NFP_UNSPEC:
1627		trav->curr = trav->curr->next;
1628		if (trav->curr != trav->head)
1629			break;
1630		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1631		mutex_lock(&xt[nfproto].mutex);
1632		trav->head = trav->curr = is_target ?
1633			&xt[nfproto].target : &xt[nfproto].match;
1634		trav->class = next_class[trav->class];
1635		break;
1636	case MTTG_TRAV_NFP_SPEC:
1637		trav->curr = trav->curr->next;
1638		if (trav->curr != trav->head)
1639			break;
1640		fallthrough;
1641	default:
1642		return NULL;
1643	}
1644	return trav;
1645}
1646
1647static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1648    bool is_target)
1649{
1650	struct nf_mttg_trav *trav = seq->private;
1651	unsigned int j;
1652
1653	trav->class = MTTG_TRAV_INIT;
1654	for (j = 0; j < *pos; ++j)
1655		if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1656			return NULL;
1657	return trav;
1658}
1659
1660static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1661{
1662	uint8_t nfproto = (unsigned long)pde_data(file_inode(seq->file));
1663	struct nf_mttg_trav *trav = seq->private;
1664
1665	switch (trav->class) {
1666	case MTTG_TRAV_NFP_UNSPEC:
1667		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1668		break;
1669	case MTTG_TRAV_NFP_SPEC:
1670		mutex_unlock(&xt[nfproto].mutex);
1671		break;
1672	}
1673}
1674
1675static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1676{
1677	return xt_mttg_seq_start(seq, pos, false);
1678}
1679
1680static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1681{
1682	return xt_mttg_seq_next(seq, v, ppos, false);
1683}
1684
1685static int xt_match_seq_show(struct seq_file *seq, void *v)
1686{
1687	const struct nf_mttg_trav *trav = seq->private;
1688	const struct xt_match *match;
1689
1690	switch (trav->class) {
1691	case MTTG_TRAV_NFP_UNSPEC:
1692	case MTTG_TRAV_NFP_SPEC:
1693		if (trav->curr == trav->head)
1694			return 0;
1695		match = list_entry(trav->curr, struct xt_match, list);
1696		if (*match->name)
1697			seq_printf(seq, "%s\n", match->name);
1698	}
1699	return 0;
1700}
1701
1702static const struct seq_operations xt_match_seq_ops = {
1703	.start	= xt_match_seq_start,
1704	.next	= xt_match_seq_next,
1705	.stop	= xt_mttg_seq_stop,
1706	.show	= xt_match_seq_show,
1707};
1708
1709static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1710{
1711	return xt_mttg_seq_start(seq, pos, true);
1712}
1713
1714static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1715{
1716	return xt_mttg_seq_next(seq, v, ppos, true);
1717}
1718
1719static int xt_target_seq_show(struct seq_file *seq, void *v)
1720{
1721	const struct nf_mttg_trav *trav = seq->private;
1722	const struct xt_target *target;
1723
1724	switch (trav->class) {
1725	case MTTG_TRAV_NFP_UNSPEC:
1726	case MTTG_TRAV_NFP_SPEC:
1727		if (trav->curr == trav->head)
1728			return 0;
1729		target = list_entry(trav->curr, struct xt_target, list);
1730		if (*target->name)
1731			seq_printf(seq, "%s\n", target->name);
1732	}
1733	return 0;
1734}
1735
1736static const struct seq_operations xt_target_seq_ops = {
1737	.start	= xt_target_seq_start,
1738	.next	= xt_target_seq_next,
1739	.stop	= xt_mttg_seq_stop,
1740	.show	= xt_target_seq_show,
1741};
1742
1743#define FORMAT_TABLES	"_tables_names"
1744#define	FORMAT_MATCHES	"_tables_matches"
1745#define FORMAT_TARGETS 	"_tables_targets"
1746
1747#endif /* CONFIG_PROC_FS */
1748
1749/**
1750 * xt_hook_ops_alloc - set up hooks for a new table
1751 * @table:	table with metadata needed to set up hooks
1752 * @fn:		Hook function
1753 *
1754 * This function will create the nf_hook_ops that the x_table needs
1755 * to hand to xt_hook_link_net().
1756 */
1757struct nf_hook_ops *
1758xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1759{
1760	unsigned int hook_mask = table->valid_hooks;
1761	uint8_t i, num_hooks = hweight32(hook_mask);
1762	uint8_t hooknum;
1763	struct nf_hook_ops *ops;
1764
1765	if (!num_hooks)
1766		return ERR_PTR(-EINVAL);
1767
1768	ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1769	if (ops == NULL)
1770		return ERR_PTR(-ENOMEM);
1771
1772	for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1773	     hook_mask >>= 1, ++hooknum) {
1774		if (!(hook_mask & 1))
1775			continue;
1776		ops[i].hook     = fn;
1777		ops[i].pf       = table->af;
1778		ops[i].hooknum  = hooknum;
1779		ops[i].priority = table->priority;
1780		++i;
1781	}
1782
1783	return ops;
1784}
1785EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1786
1787int xt_register_template(const struct xt_table *table,
1788			 int (*table_init)(struct net *net))
1789{
1790	int ret = -EBUSY, af = table->af;
1791	struct xt_template *t;
1792
1793	mutex_lock(&xt[af].mutex);
1794
1795	list_for_each_entry(t, &xt_templates[af], list) {
1796		if (WARN_ON_ONCE(strcmp(table->name, t->name) == 0))
1797			goto out_unlock;
1798	}
1799
1800	ret = -ENOMEM;
1801	t = kzalloc(sizeof(*t), GFP_KERNEL);
1802	if (!t)
1803		goto out_unlock;
1804
1805	BUILD_BUG_ON(sizeof(t->name) != sizeof(table->name));
1806
1807	strscpy(t->name, table->name, sizeof(t->name));
1808	t->table_init = table_init;
1809	t->me = table->me;
1810	list_add(&t->list, &xt_templates[af]);
1811	ret = 0;
1812out_unlock:
1813	mutex_unlock(&xt[af].mutex);
1814	return ret;
1815}
1816EXPORT_SYMBOL_GPL(xt_register_template);
1817
1818void xt_unregister_template(const struct xt_table *table)
1819{
1820	struct xt_template *t;
1821	int af = table->af;
1822
1823	mutex_lock(&xt[af].mutex);
1824	list_for_each_entry(t, &xt_templates[af], list) {
1825		if (strcmp(table->name, t->name))
1826			continue;
1827
1828		list_del(&t->list);
1829		mutex_unlock(&xt[af].mutex);
1830		kfree(t);
1831		return;
1832	}
1833
1834	mutex_unlock(&xt[af].mutex);
1835	WARN_ON_ONCE(1);
1836}
1837EXPORT_SYMBOL_GPL(xt_unregister_template);
1838
1839int xt_proto_init(struct net *net, u_int8_t af)
1840{
1841#ifdef CONFIG_PROC_FS
1842	char buf[XT_FUNCTION_MAXNAMELEN];
1843	struct proc_dir_entry *proc;
1844	kuid_t root_uid;
1845	kgid_t root_gid;
1846#endif
1847
1848	if (af >= ARRAY_SIZE(xt_prefix))
1849		return -EINVAL;
1850
1851
1852#ifdef CONFIG_PROC_FS
1853	root_uid = make_kuid(net->user_ns, 0);
1854	root_gid = make_kgid(net->user_ns, 0);
1855
1856	strscpy(buf, xt_prefix[af], sizeof(buf));
1857	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1858	proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops,
1859			sizeof(struct seq_net_private),
1860			(void *)(unsigned long)af);
1861	if (!proc)
1862		goto out;
1863	if (uid_valid(root_uid) && gid_valid(root_gid))
1864		proc_set_user(proc, root_uid, root_gid);
1865
1866	strscpy(buf, xt_prefix[af], sizeof(buf));
1867	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1868	proc = proc_create_seq_private(buf, 0440, net->proc_net,
1869			&xt_match_seq_ops, sizeof(struct nf_mttg_trav),
1870			(void *)(unsigned long)af);
1871	if (!proc)
1872		goto out_remove_tables;
1873	if (uid_valid(root_uid) && gid_valid(root_gid))
1874		proc_set_user(proc, root_uid, root_gid);
1875
1876	strscpy(buf, xt_prefix[af], sizeof(buf));
1877	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1878	proc = proc_create_seq_private(buf, 0440, net->proc_net,
1879			 &xt_target_seq_ops, sizeof(struct nf_mttg_trav),
1880			 (void *)(unsigned long)af);
1881	if (!proc)
1882		goto out_remove_matches;
1883	if (uid_valid(root_uid) && gid_valid(root_gid))
1884		proc_set_user(proc, root_uid, root_gid);
1885#endif
1886
1887	return 0;
1888
1889#ifdef CONFIG_PROC_FS
1890out_remove_matches:
1891	strscpy(buf, xt_prefix[af], sizeof(buf));
1892	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1893	remove_proc_entry(buf, net->proc_net);
1894
1895out_remove_tables:
1896	strscpy(buf, xt_prefix[af], sizeof(buf));
1897	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1898	remove_proc_entry(buf, net->proc_net);
1899out:
1900	return -1;
1901#endif
1902}
1903EXPORT_SYMBOL_GPL(xt_proto_init);
1904
1905void xt_proto_fini(struct net *net, u_int8_t af)
1906{
1907#ifdef CONFIG_PROC_FS
1908	char buf[XT_FUNCTION_MAXNAMELEN];
1909
1910	strscpy(buf, xt_prefix[af], sizeof(buf));
1911	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1912	remove_proc_entry(buf, net->proc_net);
1913
1914	strscpy(buf, xt_prefix[af], sizeof(buf));
1915	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1916	remove_proc_entry(buf, net->proc_net);
1917
1918	strscpy(buf, xt_prefix[af], sizeof(buf));
1919	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1920	remove_proc_entry(buf, net->proc_net);
1921#endif /*CONFIG_PROC_FS*/
1922}
1923EXPORT_SYMBOL_GPL(xt_proto_fini);
1924
1925#ifdef CONFIG_NETFILTER_XTABLES_LEGACY
1926/**
1927 * xt_percpu_counter_alloc - allocate x_tables rule counter
1928 *
1929 * @state: pointer to xt_percpu allocation state
1930 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1931 *
1932 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1933 * contain the address of the real (percpu) counter.
1934 *
1935 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1936 * to fetch the real percpu counter.
1937 *
1938 * To speed up allocation and improve data locality, a 4kb block is
1939 * allocated.  Freeing any counter may free an entire block, so all
1940 * counters allocated using the same state must be freed at the same
1941 * time.
1942 *
1943 * xt_percpu_counter_alloc_state contains the base address of the
1944 * allocated page and the current sub-offset.
1945 *
1946 * returns false on error.
1947 */
1948bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1949			     struct xt_counters *counter)
1950{
1951	BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1952
1953	if (nr_cpu_ids <= 1)
1954		return true;
1955
1956	if (!state->mem) {
1957		state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1958					    XT_PCPU_BLOCK_SIZE);
1959		if (!state->mem)
1960			return false;
1961	}
1962	counter->pcnt = (__force unsigned long)(state->mem + state->off);
1963	state->off += sizeof(*counter);
1964	if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1965		state->mem = NULL;
1966		state->off = 0;
1967	}
1968	return true;
1969}
1970EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1971
1972void xt_percpu_counter_free(struct xt_counters *counters)
1973{
1974	unsigned long pcnt = counters->pcnt;
1975
1976	if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1977		free_percpu((void __percpu *)pcnt);
1978}
1979EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1980#endif
1981
1982static int __net_init xt_net_init(struct net *net)
1983{
1984	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1985	int i;
1986
1987	for (i = 0; i < NFPROTO_NUMPROTO; i++)
1988		INIT_LIST_HEAD(&xt_net->tables[i]);
1989	return 0;
1990}
1991
1992static void __net_exit xt_net_exit(struct net *net)
1993{
1994	struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1995	int i;
1996
1997	for (i = 0; i < NFPROTO_NUMPROTO; i++)
1998		WARN_ON_ONCE(!list_empty(&xt_net->tables[i]));
1999}
2000
2001static struct pernet_operations xt_net_ops = {
2002	.init = xt_net_init,
2003	.exit = xt_net_exit,
2004	.id   = &xt_pernet_id,
2005	.size = sizeof(struct xt_pernet),
2006};
2007
2008static int __init xt_init(void)
2009{
2010	unsigned int i;
2011	int rv;
2012
2013	if (IS_ENABLED(CONFIG_NETFILTER_XTABLES_LEGACY)) {
2014		for_each_possible_cpu(i) {
2015			seqcount_init(&per_cpu(xt_recseq, i));
2016		}
2017	}
2018
2019	xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
2020	if (!xt)
2021		return -ENOMEM;
2022
2023	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
2024		mutex_init(&xt[i].mutex);
2025#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
2026		mutex_init(&xt[i].compat_mutex);
2027		xt[i].compat_tab = NULL;
2028#endif
2029		INIT_LIST_HEAD(&xt[i].target);
2030		INIT_LIST_HEAD(&xt[i].match);
2031		INIT_LIST_HEAD(&xt_templates[i]);
2032	}
2033	rv = register_pernet_subsys(&xt_net_ops);
2034	if (rv < 0)
2035		kfree(xt);
2036	return rv;
2037}
2038
2039static void __exit xt_fini(void)
2040{
2041	unregister_pernet_subsys(&xt_net_ops);
2042	kfree(xt);
2043}
2044
2045module_init(xt_init);
2046module_exit(xt_fini);
~/ Read-only file preview.