nf_tables_api.c 178 KB
Newer Older
Patrick McHardy's avatar
Patrick McHardy committed
1
/*
2
 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy's avatar
Patrick McHardy committed
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Development of this code funded by Astaro AG (http://www.astaro.com/)
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
16
#include <linux/vmalloc.h>
NeilBrown's avatar
NeilBrown committed
17
#include <linux/rhashtable.h>
Patrick McHardy's avatar
Patrick McHardy committed
18 19 20
#include <linux/netfilter.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>
21
#include <net/netfilter/nf_flow_table.h>
Patrick McHardy's avatar
Patrick McHardy committed
22 23
#include <net/netfilter/nf_tables_core.h>
#include <net/netfilter/nf_tables.h>
24
#include <net/net_namespace.h>
Patrick McHardy's avatar
Patrick McHardy committed
25 26 27
#include <net/sock.h>

static LIST_HEAD(nf_tables_expressions);
28
static LIST_HEAD(nf_tables_objects);
29
static LIST_HEAD(nf_tables_flowtables);
30 31
static LIST_HEAD(nf_tables_destroy_list);
static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
32
static u64 table_handle;
Patrick McHardy's avatar
Patrick McHardy committed
33

34 35 36 37 38 39
enum {
	NFT_VALIDATE_SKIP	= 0,
	NFT_VALIDATE_NEED,
	NFT_VALIDATE_DO,
};

40 41 42 43 44 45 46 47 48 49 50 51 52 53
static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);

static const struct rhashtable_params nft_chain_ht_params = {
	.head_offset		= offsetof(struct nft_chain, rhlhead),
	.key_offset		= offsetof(struct nft_chain, name),
	.hashfn			= nft_chain_hash,
	.obj_hashfn		= nft_chain_hash_obj,
	.obj_cmpfn		= nft_chain_hash_cmp,
	.locks_mul		= 1,
	.automatic_shrinking	= true,
};

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
static void nft_validate_state_update(struct net *net, u8 new_validate_state)
{
	switch (net->nft.validate_state) {
	case NFT_VALIDATE_SKIP:
		WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
		break;
	case NFT_VALIDATE_NEED:
		break;
	case NFT_VALIDATE_DO:
		if (new_validate_state == NFT_VALIDATE_NEED)
			return;
	}

	net->nft.validate_state = new_validate_state;
}
69 70
static void nf_tables_trans_destroy_work(struct work_struct *w);
static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
71

72
static void nft_ctx_init(struct nft_ctx *ctx,
73
			 struct net *net,
74 75
			 const struct sk_buff *skb,
			 const struct nlmsghdr *nlh,
76
			 u8 family,
77 78 79 80
			 struct nft_table *table,
			 struct nft_chain *chain,
			 const struct nlattr * const *nla)
{
81
	ctx->net	= net;
82
	ctx->family	= family;
83
	ctx->level	= 0;
84 85 86 87 88 89
	ctx->table	= table;
	ctx->chain	= chain;
	ctx->nla   	= nla;
	ctx->portid	= NETLINK_CB(skb).portid;
	ctx->report	= nlmsg_report(nlh);
	ctx->seq	= nlh->nlmsg_seq;
90 91
}

92 93
static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
					     int msg_type, u32 size, gfp_t gfp)
94 95 96
{
	struct nft_trans *trans;

97
	trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
98 99 100
	if (trans == NULL)
		return NULL;

101
	trans->msg_type = msg_type;
102 103 104 105 106
	trans->ctx	= *ctx;

	return trans;
}

107 108 109 110 111 112
static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
					 int msg_type, u32 size)
{
	return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
}

113 114 115 116 117 118
static void nft_trans_destroy(struct nft_trans *trans)
{
	list_del(&trans->list);
	kfree(trans);
}

119 120 121
static int nf_tables_register_hook(struct net *net,
				   const struct nft_table *table,
				   struct nft_chain *chain)
122
{
123
	const struct nft_base_chain *basechain;
124
	const struct nf_hook_ops *ops;
125

126
	if (table->flags & NFT_TABLE_F_DORMANT ||
127
	    !nft_is_base_chain(chain))
128 129
		return 0;

130 131
	basechain = nft_base_chain(chain);
	ops = &basechain->ops;
132

133 134 135
	if (basechain->type->ops_register)
		return basechain->type->ops_register(net, ops);

136
	return nf_register_net_hook(net, ops);
137 138
}

139 140 141
static void nf_tables_unregister_hook(struct net *net,
				      const struct nft_table *table,
				      struct nft_chain *chain)
142
{
143 144 145
	const struct nft_base_chain *basechain;
	const struct nf_hook_ops *ops;

146
	if (table->flags & NFT_TABLE_F_DORMANT ||
147
	    !nft_is_base_chain(chain))
148
		return;
149 150
	basechain = nft_base_chain(chain);
	ops = &basechain->ops;
151

152 153 154 155
	if (basechain->type->ops_unregister)
		return basechain->type->ops_unregister(net, ops);

	nf_unregister_net_hook(net, ops);
156 157
}

158 159 160 161 162 163 164 165 166
static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
{
	struct nft_trans *trans;

	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
	if (trans == NULL)
		return -ENOMEM;

	if (msg_type == NFT_MSG_NEWTABLE)
167
		nft_activate_next(ctx->net, ctx->table);
168 169 170 171 172 173 174 175 176 177 178 179 180

	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
	return 0;
}

static int nft_deltable(struct nft_ctx *ctx)
{
	int err;

	err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
	if (err < 0)
		return err;

181
	nft_deactivate_next(ctx->net, ctx->table);
182 183 184 185 186 187 188 189 190 191 192 193
	return err;
}

static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
{
	struct nft_trans *trans;

	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
	if (trans == NULL)
		return -ENOMEM;

	if (msg_type == NFT_MSG_NEWCHAIN)
194
		nft_activate_next(ctx->net, ctx->chain);
195 196 197 198 199 200 201 202 203 204 205 206 207 208

	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
	return 0;
}

static int nft_delchain(struct nft_ctx *ctx)
{
	int err;

	err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
	if (err < 0)
		return err;

	ctx->table->use--;
209
	nft_deactivate_next(ctx->net, ctx->chain);
210 211 212 213

	return err;
}

214 215 216 217 218 219 220 221 222 223 224 225
/* either expr ops provide both activate/deactivate, or neither */
static bool nft_expr_check_ops(const struct nft_expr_ops *ops)
{
	if (!ops)
		return true;

	if (WARN_ON_ONCE((!ops->activate ^ !ops->deactivate)))
		return false;

	return true;
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static void nft_rule_expr_activate(const struct nft_ctx *ctx,
				   struct nft_rule *rule)
{
	struct nft_expr *expr;

	expr = nft_expr_first(rule);
	while (expr != nft_expr_last(rule) && expr->ops) {
		if (expr->ops->activate)
			expr->ops->activate(ctx, expr);

		expr = nft_expr_next(expr);
	}
}

static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
				     struct nft_rule *rule)
{
	struct nft_expr *expr;

	expr = nft_expr_first(rule);
	while (expr != nft_expr_last(rule) && expr->ops) {
		if (expr->ops->deactivate)
			expr->ops->deactivate(ctx, expr);

		expr = nft_expr_next(expr);
	}
}

254 255 256 257
static int
nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
{
	/* You cannot delete the same rule twice */
258 259
	if (nft_is_active_next(ctx->net, rule)) {
		nft_deactivate_next(ctx->net, rule);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		ctx->chain->use--;
		return 0;
	}
	return -ENOENT;
}

static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
					    struct nft_rule *rule)
{
	struct nft_trans *trans;

	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
	if (trans == NULL)
		return NULL;

275 276 277 278
	if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
		nft_trans_rule_id(trans) =
			ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
	}
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	nft_trans_rule(trans) = rule;
	list_add_tail(&trans->list, &ctx->net->nft.commit_list);

	return trans;
}

static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
{
	struct nft_trans *trans;
	int err;

	trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
	if (trans == NULL)
		return -ENOMEM;

	err = nf_tables_delrule_deactivate(ctx, rule);
	if (err < 0) {
		nft_trans_destroy(trans);
		return err;
	}
299
	nft_rule_expr_deactivate(ctx, rule);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

	return 0;
}

static int nft_delrule_by_chain(struct nft_ctx *ctx)
{
	struct nft_rule *rule;
	int err;

	list_for_each_entry(rule, &ctx->chain->rules, list) {
		err = nft_delrule(ctx, rule);
		if (err < 0)
			return err;
	}
	return 0;
}

317
static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
318 319 320 321 322 323 324 325 326 327 328
			     struct nft_set *set)
{
	struct nft_trans *trans;

	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
	if (trans == NULL)
		return -ENOMEM;

	if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
		nft_trans_set_id(trans) =
			ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
329
		nft_activate_next(ctx->net, set);
330 331 332 333 334 335 336
	}
	nft_trans_set(trans) = set;
	list_add_tail(&trans->list, &ctx->net->nft.commit_list);

	return 0;
}

337
static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
338 339 340 341 342 343 344
{
	int err;

	err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
	if (err < 0)
		return err;

345
	nft_deactivate_next(ctx->net, set);
346 347 348 349 350
	ctx->table->use--;

	return err;
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
			     struct nft_object *obj)
{
	struct nft_trans *trans;

	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
	if (trans == NULL)
		return -ENOMEM;

	if (msg_type == NFT_MSG_NEWOBJ)
		nft_activate_next(ctx->net, obj);

	nft_trans_obj(trans) = obj;
	list_add_tail(&trans->list, &ctx->net->nft.commit_list);

	return 0;
}

static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
{
	int err;

	err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
	if (err < 0)
		return err;

	nft_deactivate_next(ctx->net, obj);
	ctx->table->use--;

	return err;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
				   struct nft_flowtable *flowtable)
{
	struct nft_trans *trans;

	trans = nft_trans_alloc(ctx, msg_type,
				sizeof(struct nft_trans_flowtable));
	if (trans == NULL)
		return -ENOMEM;

	if (msg_type == NFT_MSG_NEWFLOWTABLE)
		nft_activate_next(ctx->net, flowtable);

	nft_trans_flowtable(trans) = flowtable;
	list_add_tail(&trans->list, &ctx->net->nft.commit_list);

	return 0;
}

static int nft_delflowtable(struct nft_ctx *ctx,
			    struct nft_flowtable *flowtable)
{
	int err;

	err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
	if (err < 0)
		return err;

	nft_deactivate_next(ctx->net, flowtable);
	ctx->table->use--;

	return err;
}

Patrick McHardy's avatar
Patrick McHardy committed
417 418 419 420
/*
 * Tables
 */

421
static struct nft_table *nft_table_lookup(const struct net *net,
422
					  const struct nlattr *nla,
423
					  u8 family, u8 genmask)
Patrick McHardy's avatar
Patrick McHardy committed
424 425 426
{
	struct nft_table *table;

427 428 429
	if (nla == NULL)
		return ERR_PTR(-EINVAL);

430
	list_for_each_entry_rcu(table, &net->nft.tables, list) {
431
		if (!nla_strcmp(nla, table->name) &&
432
		    table->family == family &&
433
		    nft_active_genmask(table, genmask))
Patrick McHardy's avatar
Patrick McHardy committed
434 435
			return table;
	}
436 437

	return ERR_PTR(-ENOENT);
Patrick McHardy's avatar
Patrick McHardy committed
438 439
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
						   const struct nlattr *nla,
						   u8 genmask)
{
	struct nft_table *table;

	list_for_each_entry(table, &net->nft.tables, list) {
		if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
		    nft_active_genmask(table, genmask))
			return table;
	}

	return ERR_PTR(-ENOENT);
}

Patrick McHardy's avatar
Patrick McHardy committed
455 456 457 458 459
static inline u64 nf_tables_alloc_handle(struct nft_table *table)
{
	return ++table->hgenerator;
}

460
static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
461

462
static const struct nft_chain_type *
463
__nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
464 465 466
{
	int i;

467
	for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
468 469
		if (chain_type[family][i] != NULL &&
		    !nla_strcmp(nla, chain_type[family][i]->name))
470
			return chain_type[family][i];
471
	}
472
	return NULL;
473 474
}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/*
 * Loading a module requires dropping mutex that guards the
 * transaction.
 * We first need to abort any pending transactions as once
 * mutex is unlocked a different client could start a new
 * transaction.  It must not see any 'future generation'
 * changes * as these changes will never happen.
 */
#ifdef CONFIG_MODULES
static int __nf_tables_abort(struct net *net);

static void nft_request_module(struct net *net, const char *fmt, ...)
{
	char module_name[MODULE_NAME_LEN];
	va_list args;
	int ret;

	__nf_tables_abort(net);

	va_start(args, fmt);
	ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
	va_end(args);
	if (WARN(ret >= MODULE_NAME_LEN, "truncated: '%s' (len %d)", module_name, ret))
		return;

500
	mutex_unlock(&net->nft.commit_mutex);
501
	request_module("%s", module_name);
502
	mutex_lock(&net->nft.commit_mutex);
503 504 505
}
#endif

506 507 508 509 510 511 512
static void lockdep_nfnl_nft_mutex_not_held(void)
{
#ifdef CONFIG_PROVE_LOCKING
	WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
#endif
}

513
static const struct nft_chain_type *
514 515
nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
			    u8 family, bool autoload)
516
{
517
	const struct nft_chain_type *type;
518

519
	type = __nf_tables_chain_type_lookup(nla, family);
520 521
	if (type != NULL)
		return type;
522 523

	lockdep_nfnl_nft_mutex_not_held();
524
#ifdef CONFIG_MODULES
525
	if (autoload) {
526 527
		nft_request_module(net, "nft-chain-%u-%.*s", family,
				   nla_len(nla), (const char *)nla_data(nla));
528
		type = __nf_tables_chain_type_lookup(nla, family);
529 530
		if (type != NULL)
			return ERR_PTR(-EAGAIN);
531 532
	}
#endif
533
	return ERR_PTR(-ENOENT);
534 535
}

Patrick McHardy's avatar
Patrick McHardy committed
536
static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
537 538
	[NFTA_TABLE_NAME]	= { .type = NLA_STRING,
				    .len = NFT_TABLE_MAXNAMELEN - 1 },
539
	[NFTA_TABLE_FLAGS]	= { .type = NLA_U32 },
540
	[NFTA_TABLE_HANDLE]	= { .type = NLA_U64 },
Patrick McHardy's avatar
Patrick McHardy committed
541 542
};

543 544 545
static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
				     u32 portid, u32 seq, int event, u32 flags,
				     int family, const struct nft_table *table)
Patrick McHardy's avatar
Patrick McHardy committed
546 547 548 549
{
	struct nlmsghdr *nlh;
	struct nfgenmsg *nfmsg;

550
	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
Patrick McHardy's avatar
Patrick McHardy committed
551 552 553 554 555 556 557
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
	if (nlh == NULL)
		goto nla_put_failure;

	nfmsg = nlmsg_data(nlh);
	nfmsg->nfgen_family	= family;
	nfmsg->version		= NFNETLINK_V0;
558
	nfmsg->res_id		= htons(net->nft.base_seq & 0xffff);
Patrick McHardy's avatar
Patrick McHardy committed
559

560
	if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
561
	    nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
562 563 564
	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
	    nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
			 NFTA_TABLE_PAD))
Patrick McHardy's avatar
Patrick McHardy committed
565 566
		goto nla_put_failure;

567 568
	nlmsg_end(skb, nlh);
	return 0;
Patrick McHardy's avatar
Patrick McHardy committed
569 570 571 572 573 574

nla_put_failure:
	nlmsg_trim(skb, nlh);
	return -1;
}

575
static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy's avatar
Patrick McHardy committed
576 577 578 579
{
	struct sk_buff *skb;
	int err;

580 581
	if (!ctx->report &&
	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
582
		return;
Patrick McHardy's avatar
Patrick McHardy committed
583 584 585 586 587

	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
	if (skb == NULL)
		goto err;

588
	err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
589
					event, 0, ctx->family, ctx->table);
Patrick McHardy's avatar
Patrick McHardy committed
590 591 592 593 594
	if (err < 0) {
		kfree_skb(skb);
		goto err;
	}

595 596 597
	nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
		       ctx->report, GFP_KERNEL);
	return;
Patrick McHardy's avatar
Patrick McHardy committed
598
err:
599
	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
Patrick McHardy's avatar
Patrick McHardy committed
600 601 602 603 604 605 606 607
}

static int nf_tables_dump_tables(struct sk_buff *skb,
				 struct netlink_callback *cb)
{
	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
	const struct nft_table *table;
	unsigned int idx = 0, s_idx = cb->args[0];
608
	struct net *net = sock_net(skb->sk);
Patrick McHardy's avatar
Patrick McHardy committed
609 610
	int family = nfmsg->nfgen_family;

611
	rcu_read_lock();
612 613
	cb->seq = net->nft.base_seq;

614
	list_for_each_entry_rcu(table, &net->nft.tables, list) {
615
		if (family != NFPROTO_UNSPEC && family != table->family)
Patrick McHardy's avatar
Patrick McHardy committed
616 617
			continue;

618 619 620 621 622 623 624 625 626 627 628
		if (idx < s_idx)
			goto cont;
		if (idx > s_idx)
			memset(&cb->args[1], 0,
			       sizeof(cb->args) - sizeof(cb->args[0]));
		if (!nft_is_active(net, table))
			continue;
		if (nf_tables_fill_table_info(skb, net,
					      NETLINK_CB(cb->skb).portid,
					      cb->nlh->nlmsg_seq,
					      NFT_MSG_NEWTABLE, NLM_F_MULTI,
629
					      table->family, table) < 0)
630 631 632
			goto done;

		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy's avatar
Patrick McHardy committed
633
cont:
634
		idx++;
Patrick McHardy's avatar
Patrick McHardy committed
635 636
	}
done:
637
	rcu_read_unlock();
Patrick McHardy's avatar
Patrick McHardy committed
638 639 640 641
	cb->args[0] = idx;
	return skb->len;
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
				      const struct nlmsghdr *nlh,
				      struct netlink_dump_control *c)
{
	int err;

	if (!try_module_get(THIS_MODULE))
		return -EINVAL;

	rcu_read_unlock();
	err = netlink_dump_start(nlsk, skb, nlh, c);
	rcu_read_lock();
	module_put(THIS_MODULE);

	return err;
}

/* called with rcu_read_lock held */
660 661
static int nf_tables_gettable(struct net *net, struct sock *nlsk,
			      struct sk_buff *skb, const struct nlmsghdr *nlh,
662 663
			      const struct nlattr * const nla[],
			      struct netlink_ext_ack *extack)
Patrick McHardy's avatar
Patrick McHardy committed
664 665
{
	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
666
	u8 genmask = nft_genmask_cur(net);
Patrick McHardy's avatar
Patrick McHardy committed
667 668 669 670 671 672 673 674
	const struct nft_table *table;
	struct sk_buff *skb2;
	int family = nfmsg->nfgen_family;
	int err;

	if (nlh->nlmsg_flags & NLM_F_DUMP) {
		struct netlink_dump_control c = {
			.dump = nf_tables_dump_tables,
675
			.module = THIS_MODULE,
Patrick McHardy's avatar
Patrick McHardy committed
676
		};
677 678

		return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
Patrick McHardy's avatar
Patrick McHardy committed
679 680
	}

681
	table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask);
682 683
	if (IS_ERR(table)) {
		NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
Patrick McHardy's avatar
Patrick McHardy committed
684
		return PTR_ERR(table);
685
	}
Patrick McHardy's avatar
Patrick McHardy committed
686

687
	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
Patrick McHardy's avatar
Patrick McHardy committed
688 689 690
	if (!skb2)
		return -ENOMEM;

691
	err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy's avatar
Patrick McHardy committed
692 693 694 695 696 697 698 699 700 701 702 703
					nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
					family, table);
	if (err < 0)
		goto err;

	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);

err:
	kfree_skb(skb2);
	return err;
}

704
static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
705 706 707 708 709 710 711
{
	struct nft_chain *chain;
	u32 i = 0;

	list_for_each_entry(chain, &table->chains, list) {
		if (!nft_is_active_next(net, chain))
			continue;
712
		if (!nft_is_base_chain(chain))
713 714 715 716 717
			continue;

		if (cnt && i++ == cnt)
			break;

718
		nf_unregister_net_hook(net, &nft_base_chain(chain)->ops);
719 720 721
	}
}

722
static int nf_tables_table_enable(struct net *net, struct nft_table *table)
723 724 725 726 727
{
	struct nft_chain *chain;
	int err, i = 0;

	list_for_each_entry(chain, &table->chains, list) {
728 729
		if (!nft_is_active_next(net, chain))
			continue;
730
		if (!nft_is_base_chain(chain))
731 732
			continue;

733
		err = nf_register_net_hook(net, &nft_base_chain(chain)->ops);
734 735 736 737 738 739 740
		if (err < 0)
			goto err;

		i++;
	}
	return 0;
err:
741
	if (i)
742
		nft_table_disable(net, table, i);
743 744 745
	return err;
}

746
static void nf_tables_table_disable(struct net *net, struct nft_table *table)
747
{
748
	nft_table_disable(net, table, 0);
749 750
}

751
static int nf_tables_updtable(struct nft_ctx *ctx)
752
{
753
	struct nft_trans *trans;
754
	u32 flags;
755
	int ret = 0;
756

757 758
	if (!ctx->nla[NFTA_TABLE_FLAGS])
		return 0;
759

760 761 762 763
	flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
	if (flags & ~NFT_TABLE_F_DORMANT)
		return -EINVAL;

764 765 766
	if (flags == ctx->table->flags)
		return 0;

767 768 769 770
	trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
				sizeof(struct nft_trans_table));
	if (trans == NULL)
		return -ENOMEM;
771

772 773
	if ((flags & NFT_TABLE_F_DORMANT) &&
	    !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
774
		nft_trans_table_enable(trans) = false;
775 776
	} else if (!(flags & NFT_TABLE_F_DORMANT) &&
		   ctx->table->flags & NFT_TABLE_F_DORMANT) {
777
		ret = nf_tables_table_enable(ctx->net, ctx->table);
778
		if (ret >= 0) {
779
			ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
780
			nft_trans_table_enable(trans) = true;
781 782
		}
	}
783 784
	if (ret < 0)
		goto err;
785

786 787 788
	nft_trans_table_update(trans) = true;
	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
	return 0;
789
err:
790
	nft_trans_destroy(trans);
791 792 793
	return ret;
}

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
{
	const char *name = data;

	return jhash(name, strlen(name), seed);
}

static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
{
	const struct nft_chain *chain = data;

	return nft_chain_hash(chain->name, 0, seed);
}

static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
			      const void *ptr)
{
	const struct nft_chain *chain = ptr;
	const char *name = arg->key;

	return strcmp(chain->name, name);
}

817 818
static int nf_tables_newtable(struct net *net, struct sock *nlsk,
			      struct sk_buff *skb, const struct nlmsghdr *nlh,
819 820
			      const struct nlattr * const nla[],
			      struct netlink_ext_ack *extack)
Patrick McHardy's avatar
Patrick McHardy committed
821 822
{
	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
823
	u8 genmask = nft_genmask_next(net);
Patrick McHardy's avatar
Patrick McHardy committed
824
	int family = nfmsg->nfgen_family;
825 826
	const struct nlattr *attr;
	struct nft_table *table;
827
	u32 flags = 0;
828
	struct nft_ctx ctx;
829
	int err;
Patrick McHardy's avatar
Patrick McHardy committed
830

831
	lockdep_assert_held(&net->nft.commit_mutex);
832 833
	attr = nla[NFTA_TABLE_NAME];
	table = nft_table_lookup(net, attr, family, genmask);
Patrick McHardy's avatar
Patrick McHardy committed
834 835 836
	if (IS_ERR(table)) {
		if (PTR_ERR(table) != -ENOENT)
			return PTR_ERR(table);
837
	} else {
838 839
		if (nlh->nlmsg_flags & NLM_F_EXCL) {
			NL_SET_BAD_ATTR(extack, attr);
Patrick McHardy's avatar
Patrick McHardy committed
840
			return -EEXIST;
841
		}
Patrick McHardy's avatar
Patrick McHardy committed
842 843
		if (nlh->nlmsg_flags & NLM_F_REPLACE)
			return -EOPNOTSUPP;
844

845
		nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
846
		return nf_tables_updtable(&ctx);
Patrick McHardy's avatar
Patrick McHardy committed
847 848
	}

849 850 851 852 853 854
	if (nla[NFTA_TABLE_FLAGS]) {
		flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
		if (flags & ~NFT_TABLE_F_DORMANT)
			return -EINVAL;
	}

855
	err = -ENOMEM;
856
	table = kzalloc(sizeof(*table), GFP_KERNEL);
857
	if (table == NULL)
858
		goto err_kzalloc;
Patrick McHardy's avatar
Patrick McHardy committed
859

860
	table->name = nla_strdup(attr, GFP_KERNEL);
861
	if (table->name == NULL)
862
		goto err_strdup;
863

864 865 866 867
	err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
	if (err)
		goto err_chain_ht;

Patrick McHardy's avatar
Patrick McHardy committed
868
	INIT_LIST_HEAD(&table->chains);
869
	INIT_LIST_HEAD(&table->sets);
870
	INIT_LIST_HEAD(&table->objects);
871
	INIT_LIST_HEAD(&table->flowtables);
872
	table->family = family;
873
	table->flags = flags;
874
	table->handle = ++table_handle;
875

876
	nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
877
	err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
878
	if (err < 0)
879
		goto err_trans;
880

881
	list_add_tail_rcu(&table->list, &net->nft.tables);
Patrick McHardy's avatar
Patrick McHardy committed
882
	return 0;
883
err_trans:
884 885
	rhltable_destroy(&table->chains_ht);
err_chain_ht:
886
	kfree(table->name);
887
err_strdup:
888
	kfree(table);
889
err_kzalloc:
890
	return err;
Patrick McHardy's avatar
Patrick McHardy committed
891 892
}

893 894
static int nft_flush_table(struct nft_ctx *ctx)
{
895
	struct nft_flowtable *flowtable, *nft;
896
	struct nft_chain *chain, *nc;
897
	struct nft_object *obj, *ne;
898
	struct nft_set *set, *ns;
899
	int err;
900

901
	list_for_each_entry(chain, &ctx->table->chains, list) {
902 903 904
		if (!nft_is_active_next(ctx->net, chain))
			continue;

905 906 907 908 909 910 911 912
		ctx->chain = chain;

		err = nft_delrule_by_chain(ctx);
		if (err < 0)
			goto out;
	}

	list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
913 914 915
		if (!nft_is_active_next(ctx->net, set))
			continue;

916
		if (nft_set_is_anonymous(set) &&
917 918 919 920 921 922 923 924
		    !list_empty(&set->bindings))
			continue;

		err = nft_delset(ctx, set);
		if (err < 0)
			goto out;
	}

925 926 927 928 929 930
	list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
		err = nft_delflowtable(ctx, flowtable);
		if (err < 0)
			goto out;
	}

931 932 933 934 935 936
	list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
		err = nft_delobj(ctx, obj);
		if (err < 0)
			goto out;
	}

937
	list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
938 939 940
		if (!nft_is_active_next(ctx->net, chain))
			continue;

941 942 943 944 945 946 947
		ctx->chain = chain;

		err = nft_delchain(ctx);
		if (err < 0)
			goto out;
	}

948 949 950 951 952 953 954 955 956 957 958
	err = nft_deltable(ctx);
out:
	return err;
}

static int nft_flush(struct nft_ctx *ctx, int family)
{
	struct nft_table *table, *nt;
	const struct nlattr * const *nla = ctx->nla;
	int err = 0;

959
	list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) {
960
		if (family != AF_UNSPEC && table->family != family)
961 962
			continue;

963
		ctx->family = table->family;
964

965 966
		if (!nft_is_active_next(ctx->net, table))
			continue;
967

968 969 970
		if (nla[NFTA_TABLE_NAME] &&
		    nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
			continue;
971

972 973 974 975 976
		ctx->table = table;

		err = nft_flush_table(ctx);
		if (err < 0)
			goto out;
977 978 979 980 981
	}
out:
	return err;
}

982 983
static int nf_tables_deltable(struct net *net, struct sock *nlsk,
			      struct sk_buff *skb, const struct nlmsghdr *nlh,
984 985
			      const struct nlattr * const nla[],
			      struct netlink_ext_ack *extack)
Patrick McHardy's avatar
Patrick McHardy committed
986 987
{
	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
988
	u8 genmask = nft_genmask_next(net);
989
	int family = nfmsg->nfgen_family;
990 991
	const struct nlattr *attr;
	struct nft_table *table;
992
	struct nft_ctx ctx;