1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
4  *
5  * Development of this code funded by Astaro AG (http://www.astaro.com/)
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/skbuff.h>
12 #include <linux/netlink.h>
13 #include <linux/vmalloc.h>
14 #include <linux/rhashtable.h>
15 #include <linux/audit.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_flow_table.h>
20 #include <net/netfilter/nf_tables_core.h>
21 #include <net/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables_offload.h>
23 #include <net/net_namespace.h>
24 #include <net/sock.h>
25 
26 #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
27 
28 unsigned int nf_tables_net_id __read_mostly;
29 
30 static LIST_HEAD(nf_tables_expressions);
31 static LIST_HEAD(nf_tables_objects);
32 static LIST_HEAD(nf_tables_flowtables);
33 static LIST_HEAD(nf_tables_destroy_list);
34 static LIST_HEAD(nf_tables_gc_list);
35 static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
36 static DEFINE_SPINLOCK(nf_tables_gc_list_lock);
37 
38 enum {
39 	NFT_VALIDATE_SKIP	= 0,
40 	NFT_VALIDATE_NEED,
41 	NFT_VALIDATE_DO,
42 };
43 
44 static struct rhltable nft_objname_ht;
45 
46 static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
47 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
48 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
49 
50 static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
51 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
52 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
53 
54 static const struct rhashtable_params nft_chain_ht_params = {
55 	.head_offset		= offsetof(struct nft_chain, rhlhead),
56 	.key_offset		= offsetof(struct nft_chain, name),
57 	.hashfn			= nft_chain_hash,
58 	.obj_hashfn		= nft_chain_hash_obj,
59 	.obj_cmpfn		= nft_chain_hash_cmp,
60 	.automatic_shrinking	= true,
61 };
62 
63 static const struct rhashtable_params nft_objname_ht_params = {
64 	.head_offset		= offsetof(struct nft_object, rhlhead),
65 	.key_offset		= offsetof(struct nft_object, key),
66 	.hashfn			= nft_objname_hash,
67 	.obj_hashfn		= nft_objname_hash_obj,
68 	.obj_cmpfn		= nft_objname_hash_cmp,
69 	.automatic_shrinking	= true,
70 };
71 
72 struct nft_audit_data {
73 	struct nft_table *table;
74 	int entries;
75 	int op;
76 	struct list_head list;
77 };
78 
79 static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types
80 	[NFT_MSG_NEWTABLE]	= AUDIT_NFT_OP_TABLE_REGISTER,
81 	[NFT_MSG_GETTABLE]	= AUDIT_NFT_OP_INVALID,
82 	[NFT_MSG_DELTABLE]	= AUDIT_NFT_OP_TABLE_UNREGISTER,
83 	[NFT_MSG_NEWCHAIN]	= AUDIT_NFT_OP_CHAIN_REGISTER,
84 	[NFT_MSG_GETCHAIN]	= AUDIT_NFT_OP_INVALID,
85 	[NFT_MSG_DELCHAIN]	= AUDIT_NFT_OP_CHAIN_UNREGISTER,
86 	[NFT_MSG_NEWRULE]	= AUDIT_NFT_OP_RULE_REGISTER,
87 	[NFT_MSG_GETRULE]	= AUDIT_NFT_OP_INVALID,
88 	[NFT_MSG_DELRULE]	= AUDIT_NFT_OP_RULE_UNREGISTER,
89 	[NFT_MSG_NEWSET]	= AUDIT_NFT_OP_SET_REGISTER,
90 	[NFT_MSG_GETSET]	= AUDIT_NFT_OP_INVALID,
91 	[NFT_MSG_DELSET]	= AUDIT_NFT_OP_SET_UNREGISTER,
92 	[NFT_MSG_NEWSETELEM]	= AUDIT_NFT_OP_SETELEM_REGISTER,
93 	[NFT_MSG_GETSETELEM]	= AUDIT_NFT_OP_INVALID,
94 	[NFT_MSG_DELSETELEM]	= AUDIT_NFT_OP_SETELEM_UNREGISTER,
95 	[NFT_MSG_NEWGEN]	= AUDIT_NFT_OP_GEN_REGISTER,
96 	[NFT_MSG_GETGEN]	= AUDIT_NFT_OP_INVALID,
97 	[NFT_MSG_TRACE]		= AUDIT_NFT_OP_INVALID,
98 	[NFT_MSG_NEWOBJ]	= AUDIT_NFT_OP_OBJ_REGISTER,
99 	[NFT_MSG_GETOBJ]	= AUDIT_NFT_OP_INVALID,
100 	[NFT_MSG_DELOBJ]	= AUDIT_NFT_OP_OBJ_UNREGISTER,
101 	[NFT_MSG_GETOBJ_RESET]	= AUDIT_NFT_OP_OBJ_RESET,
102 	[NFT_MSG_NEWFLOWTABLE]	= AUDIT_NFT_OP_FLOWTABLE_REGISTER,
103 	[NFT_MSG_GETFLOWTABLE]	= AUDIT_NFT_OP_INVALID,
104 	[NFT_MSG_DELFLOWTABLE]	= AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
105 	[NFT_MSG_GETSETELEM_RESET] = AUDIT_NFT_OP_SETELEM_RESET,
106 };
107 
nft_validate_state_update(struct nft_table * table,u8 new_validate_state)108 static void nft_validate_state_update(struct nft_table *table, u8 new_validate_state)
109 {
110 	switch (table->validate_state) {
111 	case NFT_VALIDATE_SKIP:
112 		WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
113 		break;
114 	case NFT_VALIDATE_NEED:
115 		break;
116 	case NFT_VALIDATE_DO:
117 		if (new_validate_state == NFT_VALIDATE_NEED)
118 			return;
119 	}
120 
121 	table->validate_state = new_validate_state;
122 }
123 static void nf_tables_trans_destroy_work(struct work_struct *w);
124 static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
125 
126 static void nft_trans_gc_work(struct work_struct *work);
127 static DECLARE_WORK(trans_gc_work, nft_trans_gc_work);
128 
nft_ctx_init(struct nft_ctx * ctx,struct net * net,const struct sk_buff * skb,const struct nlmsghdr * nlh,u8 family,struct nft_table * table,struct nft_chain * chain,const struct nlattr * const * nla)129 static void nft_ctx_init(struct nft_ctx *ctx,
130 			 struct net *net,
131 			 const struct sk_buff *skb,
132 			 const struct nlmsghdr *nlh,
133 			 u8 family,
134 			 struct nft_table *table,
135 			 struct nft_chain *chain,
136 			 const struct nlattr * const *nla)
137 {
138 	ctx->net	= net;
139 	ctx->family	= family;
140 	ctx->level	= 0;
141 	ctx->table	= table;
142 	ctx->chain	= chain;
143 	ctx->nla   	= nla;
144 	ctx->portid	= NETLINK_CB(skb).portid;
145 	ctx->report	= nlmsg_report(nlh);
146 	ctx->flags	= nlh->nlmsg_flags;
147 	ctx->seq	= nlh->nlmsg_seq;
148 }
149 
nft_trans_alloc_gfp(const struct nft_ctx * ctx,int msg_type,u32 size,gfp_t gfp)150 static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
151 					     int msg_type, u32 size, gfp_t gfp)
152 {
153 	struct nft_trans *trans;
154 
155 	trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
156 	if (trans == NULL)
157 		return NULL;
158 
159 	INIT_LIST_HEAD(&trans->list);
160 	INIT_LIST_HEAD(&trans->binding_list);
161 	trans->msg_type = msg_type;
162 	trans->ctx	= *ctx;
163 
164 	return trans;
165 }
166 
nft_trans_alloc(const struct nft_ctx * ctx,int msg_type,u32 size)167 static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
168 					 int msg_type, u32 size)
169 {
170 	return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
171 }
172 
nft_trans_list_del(struct nft_trans * trans)173 static void nft_trans_list_del(struct nft_trans *trans)
174 {
175 	list_del(&trans->list);
176 	list_del(&trans->binding_list);
177 }
178 
nft_trans_destroy(struct nft_trans * trans)179 static void nft_trans_destroy(struct nft_trans *trans)
180 {
181 	nft_trans_list_del(trans);
182 	kfree(trans);
183 }
184 
__nft_set_trans_bind(const struct nft_ctx * ctx,struct nft_set * set,bool bind)185 static void __nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set,
186 				 bool bind)
187 {
188 	struct nftables_pernet *nft_net;
189 	struct net *net = ctx->net;
190 	struct nft_trans *trans;
191 
192 	if (!nft_set_is_anonymous(set))
193 		return;
194 
195 	nft_net = nft_pernet(net);
196 	list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
197 		switch (trans->msg_type) {
198 		case NFT_MSG_NEWSET:
199 			if (nft_trans_set(trans) == set)
200 				nft_trans_set_bound(trans) = bind;
201 			break;
202 		case NFT_MSG_NEWSETELEM:
203 			if (nft_trans_elem_set(trans) == set)
204 				nft_trans_elem_set_bound(trans) = bind;
205 			break;
206 		}
207 	}
208 }
209 
nft_set_trans_bind(const struct nft_ctx * ctx,struct nft_set * set)210 static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
211 {
212 	return __nft_set_trans_bind(ctx, set, true);
213 }
214 
nft_set_trans_unbind(const struct nft_ctx * ctx,struct nft_set * set)215 static void nft_set_trans_unbind(const struct nft_ctx *ctx, struct nft_set *set)
216 {
217 	return __nft_set_trans_bind(ctx, set, false);
218 }
219 
__nft_chain_trans_bind(const struct nft_ctx * ctx,struct nft_chain * chain,bool bind)220 static void __nft_chain_trans_bind(const struct nft_ctx *ctx,
221 				   struct nft_chain *chain, bool bind)
222 {
223 	struct nftables_pernet *nft_net;
224 	struct net *net = ctx->net;
225 	struct nft_trans *trans;
226 
227 	if (!nft_chain_binding(chain))
228 		return;
229 
230 	nft_net = nft_pernet(net);
231 	list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
232 		switch (trans->msg_type) {
233 		case NFT_MSG_NEWCHAIN:
234 			if (nft_trans_chain(trans) == chain)
235 				nft_trans_chain_bound(trans) = bind;
236 			break;
237 		case NFT_MSG_NEWRULE:
238 			if (trans->ctx.chain == chain)
239 				nft_trans_rule_bound(trans) = bind;
240 			break;
241 		}
242 	}
243 }
244 
nft_chain_trans_bind(const struct nft_ctx * ctx,struct nft_chain * chain)245 static void nft_chain_trans_bind(const struct nft_ctx *ctx,
246 				 struct nft_chain *chain)
247 {
248 	__nft_chain_trans_bind(ctx, chain, true);
249 }
250 
nf_tables_bind_chain(const struct nft_ctx * ctx,struct nft_chain * chain)251 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain)
252 {
253 	if (!nft_chain_binding(chain))
254 		return 0;
255 
256 	if (nft_chain_binding(ctx->chain))
257 		return -EOPNOTSUPP;
258 
259 	if (chain->bound)
260 		return -EBUSY;
261 
262 	if (!nft_use_inc(&chain->use))
263 		return -EMFILE;
264 
265 	chain->bound = true;
266 	nft_chain_trans_bind(ctx, chain);
267 
268 	return 0;
269 }
270 
nf_tables_unbind_chain(const struct nft_ctx * ctx,struct nft_chain * chain)271 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain)
272 {
273 	__nft_chain_trans_bind(ctx, chain, false);
274 }
275 
nft_netdev_register_hooks(struct net * net,struct list_head * hook_list)276 static int nft_netdev_register_hooks(struct net *net,
277 				     struct list_head *hook_list)
278 {
279 	struct nft_hook *hook;
280 	int err, j;
281 
282 	j = 0;
283 	list_for_each_entry(hook, hook_list, list) {
284 		err = nf_register_net_hook(net, &hook->ops);
285 		if (err < 0)
286 			goto err_register;
287 
288 		j++;
289 	}
290 	return 0;
291 
292 err_register:
293 	list_for_each_entry(hook, hook_list, list) {
294 		if (j-- <= 0)
295 			break;
296 
297 		nf_unregister_net_hook(net, &hook->ops);
298 	}
299 	return err;
300 }
301 
nft_netdev_unregister_hooks(struct net * net,struct list_head * hook_list,bool release_netdev)302 static void nft_netdev_unregister_hooks(struct net *net,
303 					struct list_head *hook_list,
304 					bool release_netdev)
305 {
306 	struct nft_hook *hook, *next;
307 
308 	list_for_each_entry_safe(hook, next, hook_list, list) {
309 		nf_unregister_net_hook(net, &hook->ops);
310 		if (release_netdev) {
311 			list_del(&hook->list);
312 			kfree_rcu(hook, rcu);
313 		}
314 	}
315 }
316 
nf_tables_register_hook(struct net * net,const struct nft_table * table,struct nft_chain * chain)317 static int nf_tables_register_hook(struct net *net,
318 				   const struct nft_table *table,
319 				   struct nft_chain *chain)
320 {
321 	struct nft_base_chain *basechain;
322 	const struct nf_hook_ops *ops;
323 
324 	if (table->flags & NFT_TABLE_F_DORMANT ||
325 	    !nft_is_base_chain(chain))
326 		return 0;
327 
328 	basechain = nft_base_chain(chain);
329 	ops = &basechain->ops;
330 
331 	if (basechain->type->ops_register)
332 		return basechain->type->ops_register(net, ops);
333 
334 	if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
335 		return nft_netdev_register_hooks(net, &basechain->hook_list);
336 
337 	return nf_register_net_hook(net, &basechain->ops);
338 }
339 
__nf_tables_unregister_hook(struct net * net,const struct nft_table * table,struct nft_chain * chain,bool release_netdev)340 static void __nf_tables_unregister_hook(struct net *net,
341 					const struct nft_table *table,
342 					struct nft_chain *chain,
343 					bool release_netdev)
344 {
345 	struct nft_base_chain *basechain;
346 	const struct nf_hook_ops *ops;
347 
348 	if (table->flags & NFT_TABLE_F_DORMANT ||
349 	    !nft_is_base_chain(chain))
350 		return;
351 	basechain = nft_base_chain(chain);
352 	ops = &basechain->ops;
353 
354 	if (basechain->type->ops_unregister)
355 		return basechain->type->ops_unregister(net, ops);
356 
357 	if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
358 		nft_netdev_unregister_hooks(net, &basechain->hook_list,
359 					    release_netdev);
360 	else
361 		nf_unregister_net_hook(net, &basechain->ops);
362 }
363 
nf_tables_unregister_hook(struct net * net,const struct nft_table * table,struct nft_chain * chain)364 static void nf_tables_unregister_hook(struct net *net,
365 				      const struct nft_table *table,
366 				      struct nft_chain *chain)
367 {
368 	return __nf_tables_unregister_hook(net, table, chain, false);
369 }
370 
nft_trans_commit_list_add_tail(struct net * net,struct nft_trans * trans)371 static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans)
372 {
373 	struct nftables_pernet *nft_net = nft_pernet(net);
374 
375 	switch (trans->msg_type) {
376 	case NFT_MSG_NEWSET:
377 		if (!nft_trans_set_update(trans) &&
378 		    nft_set_is_anonymous(nft_trans_set(trans)))
379 			list_add_tail(&trans->binding_list, &nft_net->binding_list);
380 		break;
381 	case NFT_MSG_NEWCHAIN:
382 		if (!nft_trans_chain_update(trans) &&
383 		    nft_chain_binding(nft_trans_chain(trans)))
384 			list_add_tail(&trans->binding_list, &nft_net->binding_list);
385 		break;
386 	}
387 
388 	list_add_tail(&trans->list, &nft_net->commit_list);
389 }
390 
nft_trans_table_add(struct nft_ctx * ctx,int msg_type)391 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
392 {
393 	struct nft_trans *trans;
394 
395 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
396 	if (trans == NULL)
397 		return -ENOMEM;
398 
399 	if (msg_type == NFT_MSG_NEWTABLE)
400 		nft_activate_next(ctx->net, ctx->table);
401 
402 	nft_trans_commit_list_add_tail(ctx->net, trans);
403 	return 0;
404 }
405 
nft_deltable(struct nft_ctx * ctx)406 static int nft_deltable(struct nft_ctx *ctx)
407 {
408 	int err;
409 
410 	err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
411 	if (err < 0)
412 		return err;
413 
414 	nft_deactivate_next(ctx->net, ctx->table);
415 	return err;
416 }
417 
nft_trans_chain_add(struct nft_ctx * ctx,int msg_type)418 static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
419 {
420 	struct nft_trans *trans;
421 
422 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
423 	if (trans == NULL)
424 		return ERR_PTR(-ENOMEM);
425 
426 	if (msg_type == NFT_MSG_NEWCHAIN) {
427 		nft_activate_next(ctx->net, ctx->chain);
428 
429 		if (ctx->nla[NFTA_CHAIN_ID]) {
430 			nft_trans_chain_id(trans) =
431 				ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
432 		}
433 	}
434 	nft_trans_chain(trans) = ctx->chain;
435 	nft_trans_commit_list_add_tail(ctx->net, trans);
436 
437 	return trans;
438 }
439 
nft_delchain(struct nft_ctx * ctx)440 static int nft_delchain(struct nft_ctx *ctx)
441 {
442 	struct nft_trans *trans;
443 
444 	trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
445 	if (IS_ERR(trans))
446 		return PTR_ERR(trans);
447 
448 	nft_use_dec(&ctx->table->use);
449 	nft_deactivate_next(ctx->net, ctx->chain);
450 
451 	return 0;
452 }
453 
nft_rule_expr_activate(const struct nft_ctx * ctx,struct nft_rule * rule)454 void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule)
455 {
456 	struct nft_expr *expr;
457 
458 	expr = nft_expr_first(rule);
459 	while (nft_expr_more(rule, expr)) {
460 		if (expr->ops->activate)
461 			expr->ops->activate(ctx, expr);
462 
463 		expr = nft_expr_next(expr);
464 	}
465 }
466 
nft_rule_expr_deactivate(const struct nft_ctx * ctx,struct nft_rule * rule,enum nft_trans_phase phase)467 void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
468 			      enum nft_trans_phase phase)
469 {
470 	struct nft_expr *expr;
471 
472 	expr = nft_expr_first(rule);
473 	while (nft_expr_more(rule, expr)) {
474 		if (expr->ops->deactivate)
475 			expr->ops->deactivate(ctx, expr, phase);
476 
477 		expr = nft_expr_next(expr);
478 	}
479 }
480 
481 static int
nf_tables_delrule_deactivate(struct nft_ctx * ctx,struct nft_rule * rule)482 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
483 {
484 	/* You cannot delete the same rule twice */
485 	if (nft_is_active_next(ctx->net, rule)) {
486 		nft_deactivate_next(ctx->net, rule);
487 		nft_use_dec(&ctx->chain->use);
488 		return 0;
489 	}
490 	return -ENOENT;
491 }
492 
nft_trans_rule_add(struct nft_ctx * ctx,int msg_type,struct nft_rule * rule)493 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
494 					    struct nft_rule *rule)
495 {
496 	struct nft_trans *trans;
497 
498 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
499 	if (trans == NULL)
500 		return NULL;
501 
502 	if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
503 		nft_trans_rule_id(trans) =
504 			ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
505 	}
506 	nft_trans_rule(trans) = rule;
507 	nft_trans_commit_list_add_tail(ctx->net, trans);
508 
509 	return trans;
510 }
511 
nft_delrule(struct nft_ctx * ctx,struct nft_rule * rule)512 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
513 {
514 	struct nft_flow_rule *flow;
515 	struct nft_trans *trans;
516 	int err;
517 
518 	trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
519 	if (trans == NULL)
520 		return -ENOMEM;
521 
522 	if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) {
523 		flow = nft_flow_rule_create(ctx->net, rule);
524 		if (IS_ERR(flow)) {
525 			nft_trans_destroy(trans);
526 			return PTR_ERR(flow);
527 		}
528 
529 		nft_trans_flow_rule(trans) = flow;
530 	}
531 
532 	err = nf_tables_delrule_deactivate(ctx, rule);
533 	if (err < 0) {
534 		nft_trans_destroy(trans);
535 		return err;
536 	}
537 	nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
538 
539 	return 0;
540 }
541 
nft_delrule_by_chain(struct nft_ctx * ctx)542 static int nft_delrule_by_chain(struct nft_ctx *ctx)
543 {
544 	struct nft_rule *rule;
545 	int err;
546 
547 	list_for_each_entry(rule, &ctx->chain->rules, list) {
548 		if (!nft_is_active_next(ctx->net, rule))
549 			continue;
550 
551 		err = nft_delrule(ctx, rule);
552 		if (err < 0)
553 			return err;
554 	}
555 	return 0;
556 }
557 
__nft_trans_set_add(const struct nft_ctx * ctx,int msg_type,struct nft_set * set,const struct nft_set_desc * desc)558 static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
559 			       struct nft_set *set,
560 			       const struct nft_set_desc *desc)
561 {
562 	struct nft_trans *trans;
563 
564 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
565 	if (trans == NULL)
566 		return -ENOMEM;
567 
568 	if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) {
569 		nft_trans_set_id(trans) =
570 			ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
571 		nft_activate_next(ctx->net, set);
572 	}
573 	nft_trans_set(trans) = set;
574 	if (desc) {
575 		nft_trans_set_update(trans) = true;
576 		nft_trans_set_gc_int(trans) = desc->gc_int;
577 		nft_trans_set_timeout(trans) = desc->timeout;
578 		nft_trans_set_size(trans) = desc->size;
579 	}
580 	nft_trans_commit_list_add_tail(ctx->net, trans);
581 
582 	return 0;
583 }
584 
nft_trans_set_add(const struct nft_ctx * ctx,int msg_type,struct nft_set * set)585 static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
586 			     struct nft_set *set)
587 {
588 	return __nft_trans_set_add(ctx, msg_type, set, NULL);
589 }
590 
nft_mapelem_deactivate(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)591 static int nft_mapelem_deactivate(const struct nft_ctx *ctx,
592 				  struct nft_set *set,
593 				  const struct nft_set_iter *iter,
594 				  struct nft_set_elem *elem)
595 {
596 	nft_setelem_data_deactivate(ctx->net, set, elem);
597 
598 	return 0;
599 }
600 
601 struct nft_set_elem_catchall {
602 	struct list_head	list;
603 	struct rcu_head		rcu;
604 	void			*elem;
605 };
606 
nft_map_catchall_deactivate(const struct nft_ctx * ctx,struct nft_set * set)607 static void nft_map_catchall_deactivate(const struct nft_ctx *ctx,
608 					struct nft_set *set)
609 {
610 	u8 genmask = nft_genmask_next(ctx->net);
611 	struct nft_set_elem_catchall *catchall;
612 	struct nft_set_elem elem;
613 	struct nft_set_ext *ext;
614 
615 	list_for_each_entry(catchall, &set->catchall_list, list) {
616 		ext = nft_set_elem_ext(set, catchall->elem);
617 		if (!nft_set_elem_active(ext, genmask))
618 			continue;
619 
620 		elem.priv = catchall->elem;
621 		nft_setelem_data_deactivate(ctx->net, set, &elem);
622 		break;
623 	}
624 }
625 
nft_map_deactivate(const struct nft_ctx * ctx,struct nft_set * set)626 static void nft_map_deactivate(const struct nft_ctx *ctx, struct nft_set *set)
627 {
628 	struct nft_set_iter iter = {
629 		.genmask	= nft_genmask_next(ctx->net),
630 		.fn		= nft_mapelem_deactivate,
631 	};
632 
633 	set->ops->walk(ctx, set, &iter);
634 	WARN_ON_ONCE(iter.err);
635 
636 	nft_map_catchall_deactivate(ctx, set);
637 }
638 
nft_delset(const struct nft_ctx * ctx,struct nft_set * set)639 static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
640 {
641 	int err;
642 
643 	err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
644 	if (err < 0)
645 		return err;
646 
647 	if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
648 		nft_map_deactivate(ctx, set);
649 
650 	nft_deactivate_next(ctx->net, set);
651 	nft_use_dec(&ctx->table->use);
652 
653 	return err;
654 }
655 
nft_trans_obj_add(struct nft_ctx * ctx,int msg_type,struct nft_object * obj)656 static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
657 			     struct nft_object *obj)
658 {
659 	struct nft_trans *trans;
660 
661 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
662 	if (trans == NULL)
663 		return -ENOMEM;
664 
665 	if (msg_type == NFT_MSG_NEWOBJ)
666 		nft_activate_next(ctx->net, obj);
667 
668 	nft_trans_obj(trans) = obj;
669 	nft_trans_commit_list_add_tail(ctx->net, trans);
670 
671 	return 0;
672 }
673 
nft_delobj(struct nft_ctx * ctx,struct nft_object * obj)674 static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
675 {
676 	int err;
677 
678 	err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
679 	if (err < 0)
680 		return err;
681 
682 	nft_deactivate_next(ctx->net, obj);
683 	nft_use_dec(&ctx->table->use);
684 
685 	return err;
686 }
687 
nft_trans_flowtable_add(struct nft_ctx * ctx,int msg_type,struct nft_flowtable * flowtable)688 static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
689 				   struct nft_flowtable *flowtable)
690 {
691 	struct nft_trans *trans;
692 
693 	trans = nft_trans_alloc(ctx, msg_type,
694 				sizeof(struct nft_trans_flowtable));
695 	if (trans == NULL)
696 		return -ENOMEM;
697 
698 	if (msg_type == NFT_MSG_NEWFLOWTABLE)
699 		nft_activate_next(ctx->net, flowtable);
700 
701 	INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
702 	nft_trans_flowtable(trans) = flowtable;
703 	nft_trans_commit_list_add_tail(ctx->net, trans);
704 
705 	return 0;
706 }
707 
nft_delflowtable(struct nft_ctx * ctx,struct nft_flowtable * flowtable)708 static int nft_delflowtable(struct nft_ctx *ctx,
709 			    struct nft_flowtable *flowtable)
710 {
711 	int err;
712 
713 	err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
714 	if (err < 0)
715 		return err;
716 
717 	nft_deactivate_next(ctx->net, flowtable);
718 	nft_use_dec(&ctx->table->use);
719 
720 	return err;
721 }
722 
__nft_reg_track_clobber(struct nft_regs_track * track,u8 dreg)723 static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg)
724 {
725 	int i;
726 
727 	for (i = track->regs[dreg].num_reg; i > 0; i--)
728 		__nft_reg_track_cancel(track, dreg - i);
729 }
730 
__nft_reg_track_update(struct nft_regs_track * track,const struct nft_expr * expr,u8 dreg,u8 num_reg)731 static void __nft_reg_track_update(struct nft_regs_track *track,
732 				   const struct nft_expr *expr,
733 				   u8 dreg, u8 num_reg)
734 {
735 	track->regs[dreg].selector = expr;
736 	track->regs[dreg].bitwise = NULL;
737 	track->regs[dreg].num_reg = num_reg;
738 }
739 
nft_reg_track_update(struct nft_regs_track * track,const struct nft_expr * expr,u8 dreg,u8 len)740 void nft_reg_track_update(struct nft_regs_track *track,
741 			  const struct nft_expr *expr, u8 dreg, u8 len)
742 {
743 	unsigned int regcount;
744 	int i;
745 
746 	__nft_reg_track_clobber(track, dreg);
747 
748 	regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
749 	for (i = 0; i < regcount; i++, dreg++)
750 		__nft_reg_track_update(track, expr, dreg, i);
751 }
752 EXPORT_SYMBOL_GPL(nft_reg_track_update);
753 
nft_reg_track_cancel(struct nft_regs_track * track,u8 dreg,u8 len)754 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len)
755 {
756 	unsigned int regcount;
757 	int i;
758 
759 	__nft_reg_track_clobber(track, dreg);
760 
761 	regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
762 	for (i = 0; i < regcount; i++, dreg++)
763 		__nft_reg_track_cancel(track, dreg);
764 }
765 EXPORT_SYMBOL_GPL(nft_reg_track_cancel);
766 
__nft_reg_track_cancel(struct nft_regs_track * track,u8 dreg)767 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg)
768 {
769 	track->regs[dreg].selector = NULL;
770 	track->regs[dreg].bitwise = NULL;
771 	track->regs[dreg].num_reg = 0;
772 }
773 EXPORT_SYMBOL_GPL(__nft_reg_track_cancel);
774 
775 /*
776  * Tables
777  */
778 
nft_table_lookup(const struct net * net,const struct nlattr * nla,u8 family,u8 genmask,u32 nlpid)779 static struct nft_table *nft_table_lookup(const struct net *net,
780 					  const struct nlattr *nla,
781 					  u8 family, u8 genmask, u32 nlpid)
782 {
783 	struct nftables_pernet *nft_net;
784 	struct nft_table *table;
785 
786 	if (nla == NULL)
787 		return ERR_PTR(-EINVAL);
788 
789 	nft_net = nft_pernet(net);
790 	list_for_each_entry_rcu(table, &nft_net->tables, list,
791 				lockdep_is_held(&nft_net->commit_mutex)) {
792 		if (!nla_strcmp(nla, table->name) &&
793 		    table->family == family &&
794 		    nft_active_genmask(table, genmask)) {
795 			if (nft_table_has_owner(table) &&
796 			    nlpid && table->nlpid != nlpid)
797 				return ERR_PTR(-EPERM);
798 
799 			return table;
800 		}
801 	}
802 
803 	return ERR_PTR(-ENOENT);
804 }
805 
nft_table_lookup_byhandle(const struct net * net,const struct nlattr * nla,u8 genmask,u32 nlpid)806 static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
807 						   const struct nlattr *nla,
808 						   u8 genmask, u32 nlpid)
809 {
810 	struct nftables_pernet *nft_net;
811 	struct nft_table *table;
812 
813 	nft_net = nft_pernet(net);
814 	list_for_each_entry(table, &nft_net->tables, list) {
815 		if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
816 		    nft_active_genmask(table, genmask)) {
817 			if (nft_table_has_owner(table) &&
818 			    nlpid && table->nlpid != nlpid)
819 				return ERR_PTR(-EPERM);
820 
821 			return table;
822 		}
823 	}
824 
825 	return ERR_PTR(-ENOENT);
826 }
827 
nf_tables_alloc_handle(struct nft_table * table)828 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
829 {
830 	return ++table->hgenerator;
831 }
832 
833 static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
834 
835 static const struct nft_chain_type *
__nft_chain_type_get(u8 family,enum nft_chain_types type)836 __nft_chain_type_get(u8 family, enum nft_chain_types type)
837 {
838 	if (family >= NFPROTO_NUMPROTO ||
839 	    type >= NFT_CHAIN_T_MAX)
840 		return NULL;
841 
842 	return chain_type[family][type];
843 }
844 
845 static const struct nft_chain_type *
__nf_tables_chain_type_lookup(const struct nlattr * nla,u8 family)846 __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
847 {
848 	const struct nft_chain_type *type;
849 	int i;
850 
851 	for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
852 		type = __nft_chain_type_get(family, i);
853 		if (!type)
854 			continue;
855 		if (!nla_strcmp(nla, type->name))
856 			return type;
857 	}
858 	return NULL;
859 }
860 
861 struct nft_module_request {
862 	struct list_head	list;
863 	char			module[MODULE_NAME_LEN];
864 	bool			done;
865 };
866 
867 #ifdef CONFIG_MODULES
nft_request_module(struct net * net,const char * fmt,...)868 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
869 				      ...)
870 {
871 	char module_name[MODULE_NAME_LEN];
872 	struct nftables_pernet *nft_net;
873 	struct nft_module_request *req;
874 	va_list args;
875 	int ret;
876 
877 	va_start(args, fmt);
878 	ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
879 	va_end(args);
880 	if (ret >= MODULE_NAME_LEN)
881 		return 0;
882 
883 	nft_net = nft_pernet(net);
884 	list_for_each_entry(req, &nft_net->module_list, list) {
885 		if (!strcmp(req->module, module_name)) {
886 			if (req->done)
887 				return 0;
888 
889 			/* A request to load this module already exists. */
890 			return -EAGAIN;
891 		}
892 	}
893 
894 	req = kmalloc(sizeof(*req), GFP_KERNEL);
895 	if (!req)
896 		return -ENOMEM;
897 
898 	req->done = false;
899 	strscpy(req->module, module_name, MODULE_NAME_LEN);
900 	list_add_tail(&req->list, &nft_net->module_list);
901 
902 	return -EAGAIN;
903 }
904 EXPORT_SYMBOL_GPL(nft_request_module);
905 #endif
906 
lockdep_nfnl_nft_mutex_not_held(void)907 static void lockdep_nfnl_nft_mutex_not_held(void)
908 {
909 #ifdef CONFIG_PROVE_LOCKING
910 	if (debug_locks)
911 		WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
912 #endif
913 }
914 
915 static const struct nft_chain_type *
nf_tables_chain_type_lookup(struct net * net,const struct nlattr * nla,u8 family,bool autoload)916 nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
917 			    u8 family, bool autoload)
918 {
919 	const struct nft_chain_type *type;
920 
921 	type = __nf_tables_chain_type_lookup(nla, family);
922 	if (type != NULL)
923 		return type;
924 
925 	lockdep_nfnl_nft_mutex_not_held();
926 #ifdef CONFIG_MODULES
927 	if (autoload) {
928 		if (nft_request_module(net, "nft-chain-%u-%.*s", family,
929 				       nla_len(nla),
930 				       (const char *)nla_data(nla)) == -EAGAIN)
931 			return ERR_PTR(-EAGAIN);
932 	}
933 #endif
934 	return ERR_PTR(-ENOENT);
935 }
936 
nft_base_seq(const struct net * net)937 static __be16 nft_base_seq(const struct net *net)
938 {
939 	struct nftables_pernet *nft_net = nft_pernet(net);
940 
941 	return htons(nft_net->base_seq & 0xffff);
942 }
943 
944 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
945 	[NFTA_TABLE_NAME]	= { .type = NLA_STRING,
946 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
947 	[NFTA_TABLE_FLAGS]	= { .type = NLA_U32 },
948 	[NFTA_TABLE_HANDLE]	= { .type = NLA_U64 },
949 	[NFTA_TABLE_USERDATA]	= { .type = NLA_BINARY,
950 				    .len = NFT_USERDATA_MAXLEN }
951 };
952 
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)953 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
954 				     u32 portid, u32 seq, int event, u32 flags,
955 				     int family, const struct nft_table *table)
956 {
957 	struct nlmsghdr *nlh;
958 
959 	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
960 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
961 			   NFNETLINK_V0, nft_base_seq(net));
962 	if (!nlh)
963 		goto nla_put_failure;
964 
965 	if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
966 	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
967 	    nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
968 			 NFTA_TABLE_PAD))
969 		goto nla_put_failure;
970 
971 	if (event == NFT_MSG_DELTABLE) {
972 		nlmsg_end(skb, nlh);
973 		return 0;
974 	}
975 
976 	if (nla_put_be32(skb, NFTA_TABLE_FLAGS,
977 			 htonl(table->flags & NFT_TABLE_F_MASK)))
978 		goto nla_put_failure;
979 
980 	if (nft_table_has_owner(table) &&
981 	    nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid)))
982 		goto nla_put_failure;
983 
984 	if (table->udata) {
985 		if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
986 			goto nla_put_failure;
987 	}
988 
989 	nlmsg_end(skb, nlh);
990 	return 0;
991 
992 nla_put_failure:
993 	nlmsg_trim(skb, nlh);
994 	return -1;
995 }
996 
997 struct nftnl_skb_parms {
998 	bool report;
999 };
1000 #define NFT_CB(skb)	(*(struct nftnl_skb_parms*)&((skb)->cb))
1001 
nft_notify_enqueue(struct sk_buff * skb,bool report,struct list_head * notify_list)1002 static void nft_notify_enqueue(struct sk_buff *skb, bool report,
1003 			       struct list_head *notify_list)
1004 {
1005 	NFT_CB(skb).report = report;
1006 	list_add_tail(&skb->list, notify_list);
1007 }
1008 
nf_tables_table_notify(const struct nft_ctx * ctx,int event)1009 static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
1010 {
1011 	struct nftables_pernet *nft_net;
1012 	struct sk_buff *skb;
1013 	u16 flags = 0;
1014 	int err;
1015 
1016 	if (!ctx->report &&
1017 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1018 		return;
1019 
1020 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1021 	if (skb == NULL)
1022 		goto err;
1023 
1024 	if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1025 		flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1026 
1027 	err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
1028 					event, flags, ctx->family, ctx->table);
1029 	if (err < 0) {
1030 		kfree_skb(skb);
1031 		goto err;
1032 	}
1033 
1034 	nft_net = nft_pernet(ctx->net);
1035 	nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
1036 	return;
1037 err:
1038 	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1039 }
1040 
nf_tables_dump_tables(struct sk_buff * skb,struct netlink_callback * cb)1041 static int nf_tables_dump_tables(struct sk_buff *skb,
1042 				 struct netlink_callback *cb)
1043 {
1044 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1045 	struct nftables_pernet *nft_net;
1046 	const struct nft_table *table;
1047 	unsigned int idx = 0, s_idx = cb->args[0];
1048 	struct net *net = sock_net(skb->sk);
1049 	int family = nfmsg->nfgen_family;
1050 
1051 	rcu_read_lock();
1052 	nft_net = nft_pernet(net);
1053 	cb->seq = READ_ONCE(nft_net->base_seq);
1054 
1055 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
1056 		if (family != NFPROTO_UNSPEC && family != table->family)
1057 			continue;
1058 
1059 		if (idx < s_idx)
1060 			goto cont;
1061 		if (idx > s_idx)
1062 			memset(&cb->args[1], 0,
1063 			       sizeof(cb->args) - sizeof(cb->args[0]));
1064 		if (!nft_is_active(net, table))
1065 			continue;
1066 		if (nf_tables_fill_table_info(skb, net,
1067 					      NETLINK_CB(cb->skb).portid,
1068 					      cb->nlh->nlmsg_seq,
1069 					      NFT_MSG_NEWTABLE, NLM_F_MULTI,
1070 					      table->family, table) < 0)
1071 			goto done;
1072 
1073 		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1074 cont:
1075 		idx++;
1076 	}
1077 done:
1078 	rcu_read_unlock();
1079 	cb->args[0] = idx;
1080 	return skb->len;
1081 }
1082 
nft_netlink_dump_start_rcu(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,struct netlink_dump_control * c)1083 static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
1084 				      const struct nlmsghdr *nlh,
1085 				      struct netlink_dump_control *c)
1086 {
1087 	int err;
1088 
1089 	if (!try_module_get(THIS_MODULE))
1090 		return -EINVAL;
1091 
1092 	rcu_read_unlock();
1093 	err = netlink_dump_start(nlsk, skb, nlh, c);
1094 	rcu_read_lock();
1095 	module_put(THIS_MODULE);
1096 
1097 	return err;
1098 }
1099 
1100 /* called with rcu_read_lock held */
nf_tables_gettable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1101 static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info,
1102 			      const struct nlattr * const nla[])
1103 {
1104 	struct netlink_ext_ack *extack = info->extack;
1105 	u8 genmask = nft_genmask_cur(info->net);
1106 	u8 family = info->nfmsg->nfgen_family;
1107 	const struct nft_table *table;
1108 	struct net *net = info->net;
1109 	struct sk_buff *skb2;
1110 	int err;
1111 
1112 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1113 		struct netlink_dump_control c = {
1114 			.dump = nf_tables_dump_tables,
1115 			.module = THIS_MODULE,
1116 		};
1117 
1118 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
1119 	}
1120 
1121 	table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0);
1122 	if (IS_ERR(table)) {
1123 		NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
1124 		return PTR_ERR(table);
1125 	}
1126 
1127 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1128 	if (!skb2)
1129 		return -ENOMEM;
1130 
1131 	err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
1132 					info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE,
1133 					0, family, table);
1134 	if (err < 0)
1135 		goto err_fill_table_info;
1136 
1137 	return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1138 
1139 err_fill_table_info:
1140 	kfree_skb(skb2);
1141 	return err;
1142 }
1143 
nft_table_disable(struct net * net,struct nft_table * table,u32 cnt)1144 static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
1145 {
1146 	struct nft_chain *chain;
1147 	u32 i = 0;
1148 
1149 	list_for_each_entry(chain, &table->chains, list) {
1150 		if (!nft_is_active_next(net, chain))
1151 			continue;
1152 		if (!nft_is_base_chain(chain))
1153 			continue;
1154 
1155 		if (cnt && i++ == cnt)
1156 			break;
1157 
1158 		nf_tables_unregister_hook(net, table, chain);
1159 	}
1160 }
1161 
nf_tables_table_enable(struct net * net,struct nft_table * table)1162 static int nf_tables_table_enable(struct net *net, struct nft_table *table)
1163 {
1164 	struct nft_chain *chain;
1165 	int err, i = 0;
1166 
1167 	list_for_each_entry(chain, &table->chains, list) {
1168 		if (!nft_is_active_next(net, chain))
1169 			continue;
1170 		if (!nft_is_base_chain(chain))
1171 			continue;
1172 
1173 		err = nf_tables_register_hook(net, table, chain);
1174 		if (err < 0)
1175 			goto err_register_hooks;
1176 
1177 		i++;
1178 	}
1179 	return 0;
1180 
1181 err_register_hooks:
1182 	if (i)
1183 		nft_table_disable(net, table, i);
1184 	return err;
1185 }
1186 
nf_tables_table_disable(struct net * net,struct nft_table * table)1187 static void nf_tables_table_disable(struct net *net, struct nft_table *table)
1188 {
1189 	table->flags &= ~NFT_TABLE_F_DORMANT;
1190 	nft_table_disable(net, table, 0);
1191 	table->flags |= NFT_TABLE_F_DORMANT;
1192 }
1193 
1194 #define __NFT_TABLE_F_INTERNAL		(NFT_TABLE_F_MASK + 1)
1195 #define __NFT_TABLE_F_WAS_DORMANT	(__NFT_TABLE_F_INTERNAL << 0)
1196 #define __NFT_TABLE_F_WAS_AWAKEN	(__NFT_TABLE_F_INTERNAL << 1)
1197 #define __NFT_TABLE_F_UPDATE		(__NFT_TABLE_F_WAS_DORMANT | \
1198 					 __NFT_TABLE_F_WAS_AWAKEN)
1199 
nf_tables_updtable(struct nft_ctx * ctx)1200 static int nf_tables_updtable(struct nft_ctx *ctx)
1201 {
1202 	struct nft_trans *trans;
1203 	u32 flags;
1204 	int ret;
1205 
1206 	if (!ctx->nla[NFTA_TABLE_FLAGS])
1207 		return 0;
1208 
1209 	flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
1210 	if (flags & ~NFT_TABLE_F_MASK)
1211 		return -EOPNOTSUPP;
1212 
1213 	if (flags == ctx->table->flags)
1214 		return 0;
1215 
1216 	if ((nft_table_has_owner(ctx->table) &&
1217 	     !(flags & NFT_TABLE_F_OWNER)) ||
1218 	    (!nft_table_has_owner(ctx->table) &&
1219 	     flags & NFT_TABLE_F_OWNER))
1220 		return -EOPNOTSUPP;
1221 
1222 	/* No dormant off/on/off/on games in single transaction */
1223 	if (ctx->table->flags & __NFT_TABLE_F_UPDATE)
1224 		return -EINVAL;
1225 
1226 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
1227 				sizeof(struct nft_trans_table));
1228 	if (trans == NULL)
1229 		return -ENOMEM;
1230 
1231 	if ((flags & NFT_TABLE_F_DORMANT) &&
1232 	    !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
1233 		ctx->table->flags |= NFT_TABLE_F_DORMANT;
1234 		if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE))
1235 			ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN;
1236 	} else if (!(flags & NFT_TABLE_F_DORMANT) &&
1237 		   ctx->table->flags & NFT_TABLE_F_DORMANT) {
1238 		ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
1239 		if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) {
1240 			ret = nf_tables_table_enable(ctx->net, ctx->table);
1241 			if (ret < 0)
1242 				goto err_register_hooks;
1243 
1244 			ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT;
1245 		}
1246 	}
1247 
1248 	nft_trans_table_update(trans) = true;
1249 	nft_trans_commit_list_add_tail(ctx->net, trans);
1250 
1251 	return 0;
1252 
1253 err_register_hooks:
1254 	nft_trans_destroy(trans);
1255 	return ret;
1256 }
1257 
nft_chain_hash(const void * data,u32 len,u32 seed)1258 static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
1259 {
1260 	const char *name = data;
1261 
1262 	return jhash(name, strlen(name), seed);
1263 }
1264 
nft_chain_hash_obj(const void * data,u32 len,u32 seed)1265 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
1266 {
1267 	const struct nft_chain *chain = data;
1268 
1269 	return nft_chain_hash(chain->name, 0, seed);
1270 }
1271 
nft_chain_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)1272 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
1273 			      const void *ptr)
1274 {
1275 	const struct nft_chain *chain = ptr;
1276 	const char *name = arg->key;
1277 
1278 	return strcmp(chain->name, name);
1279 }
1280 
nft_objname_hash(const void * data,u32 len,u32 seed)1281 static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
1282 {
1283 	const struct nft_object_hash_key *k = data;
1284 
1285 	seed ^= hash_ptr(k->table, 32);
1286 
1287 	return jhash(k->name, strlen(k->name), seed);
1288 }
1289 
nft_objname_hash_obj(const void * data,u32 len,u32 seed)1290 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
1291 {
1292 	const struct nft_object *obj = data;
1293 
1294 	return nft_objname_hash(&obj->key, 0, seed);
1295 }
1296 
nft_objname_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)1297 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
1298 				const void *ptr)
1299 {
1300 	const struct nft_object_hash_key *k = arg->key;
1301 	const struct nft_object *obj = ptr;
1302 
1303 	if (obj->key.table != k->table)
1304 		return -1;
1305 
1306 	return strcmp(obj->key.name, k->name);
1307 }
1308 
nft_supported_family(u8 family)1309 static bool nft_supported_family(u8 family)
1310 {
1311 	return false
1312 #ifdef CONFIG_NF_TABLES_INET
1313 		|| family == NFPROTO_INET
1314 #endif
1315 #ifdef CONFIG_NF_TABLES_IPV4
1316 		|| family == NFPROTO_IPV4
1317 #endif
1318 #ifdef CONFIG_NF_TABLES_ARP
1319 		|| family == NFPROTO_ARP
1320 #endif
1321 #ifdef CONFIG_NF_TABLES_NETDEV
1322 		|| family == NFPROTO_NETDEV
1323 #endif
1324 #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE)
1325 		|| family == NFPROTO_BRIDGE
1326 #endif
1327 #ifdef CONFIG_NF_TABLES_IPV6
1328 		|| family == NFPROTO_IPV6
1329 #endif
1330 		;
1331 }
1332 
nf_tables_newtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1333 static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
1334 			      const struct nlattr * const nla[])
1335 {
1336 	struct nftables_pernet *nft_net = nft_pernet(info->net);
1337 	struct netlink_ext_ack *extack = info->extack;
1338 	u8 genmask = nft_genmask_next(info->net);
1339 	u8 family = info->nfmsg->nfgen_family;
1340 	struct net *net = info->net;
1341 	const struct nlattr *attr;
1342 	struct nft_table *table;
1343 	struct nft_ctx ctx;
1344 	u32 flags = 0;
1345 	int err;
1346 
1347 	if (!nft_supported_family(family))
1348 		return -EOPNOTSUPP;
1349 
1350 	lockdep_assert_held(&nft_net->commit_mutex);
1351 	attr = nla[NFTA_TABLE_NAME];
1352 	table = nft_table_lookup(net, attr, family, genmask,
1353 				 NETLINK_CB(skb).portid);
1354 	if (IS_ERR(table)) {
1355 		if (PTR_ERR(table) != -ENOENT)
1356 			return PTR_ERR(table);
1357 	} else {
1358 		if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
1359 			NL_SET_BAD_ATTR(extack, attr);
1360 			return -EEXIST;
1361 		}
1362 		if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1363 			return -EOPNOTSUPP;
1364 
1365 		nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1366 
1367 		return nf_tables_updtable(&ctx);
1368 	}
1369 
1370 	if (nla[NFTA_TABLE_FLAGS]) {
1371 		flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
1372 		if (flags & ~NFT_TABLE_F_MASK)
1373 			return -EOPNOTSUPP;
1374 	}
1375 
1376 	err = -ENOMEM;
1377 	table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT);
1378 	if (table == NULL)
1379 		goto err_kzalloc;
1380 
1381 	table->validate_state = nft_net->validate_state;
1382 	table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
1383 	if (table->name == NULL)
1384 		goto err_strdup;
1385 
1386 	if (nla[NFTA_TABLE_USERDATA]) {
1387 		table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
1388 		if (table->udata == NULL)
1389 			goto err_table_udata;
1390 
1391 		table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
1392 	}
1393 
1394 	err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
1395 	if (err)
1396 		goto err_chain_ht;
1397 
1398 	INIT_LIST_HEAD(&table->chains);
1399 	INIT_LIST_HEAD(&table->sets);
1400 	INIT_LIST_HEAD(&table->objects);
1401 	INIT_LIST_HEAD(&table->flowtables);
1402 	table->family = family;
1403 	table->flags = flags;
1404 	table->handle = ++nft_net->table_handle;
1405 	if (table->flags & NFT_TABLE_F_OWNER)
1406 		table->nlpid = NETLINK_CB(skb).portid;
1407 
1408 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1409 	err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
1410 	if (err < 0)
1411 		goto err_trans;
1412 
1413 	list_add_tail_rcu(&table->list, &nft_net->tables);
1414 	return 0;
1415 err_trans:
1416 	rhltable_destroy(&table->chains_ht);
1417 err_chain_ht:
1418 	kfree(table->udata);
1419 err_table_udata:
1420 	kfree(table->name);
1421 err_strdup:
1422 	kfree(table);
1423 err_kzalloc:
1424 	return err;
1425 }
1426 
nft_flush_table(struct nft_ctx * ctx)1427 static int nft_flush_table(struct nft_ctx *ctx)
1428 {
1429 	struct nft_flowtable *flowtable, *nft;
1430 	struct nft_chain *chain, *nc;
1431 	struct nft_object *obj, *ne;
1432 	struct nft_set *set, *ns;
1433 	int err;
1434 
1435 	list_for_each_entry(chain, &ctx->table->chains, list) {
1436 		if (!nft_is_active_next(ctx->net, chain))
1437 			continue;
1438 
1439 		if (nft_chain_binding(chain))
1440 			continue;
1441 
1442 		ctx->chain = chain;
1443 
1444 		err = nft_delrule_by_chain(ctx);
1445 		if (err < 0)
1446 			goto out;
1447 	}
1448 
1449 	list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
1450 		if (!nft_is_active_next(ctx->net, set))
1451 			continue;
1452 
1453 		if (nft_set_is_anonymous(set))
1454 			continue;
1455 
1456 		err = nft_delset(ctx, set);
1457 		if (err < 0)
1458 			goto out;
1459 	}
1460 
1461 	list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
1462 		if (!nft_is_active_next(ctx->net, flowtable))
1463 			continue;
1464 
1465 		err = nft_delflowtable(ctx, flowtable);
1466 		if (err < 0)
1467 			goto out;
1468 	}
1469 
1470 	list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
1471 		if (!nft_is_active_next(ctx->net, obj))
1472 			continue;
1473 
1474 		err = nft_delobj(ctx, obj);
1475 		if (err < 0)
1476 			goto out;
1477 	}
1478 
1479 	list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
1480 		if (!nft_is_active_next(ctx->net, chain))
1481 			continue;
1482 
1483 		if (nft_chain_binding(chain))
1484 			continue;
1485 
1486 		ctx->chain = chain;
1487 
1488 		err = nft_delchain(ctx);
1489 		if (err < 0)
1490 			goto out;
1491 	}
1492 
1493 	err = nft_deltable(ctx);
1494 out:
1495 	return err;
1496 }
1497 
nft_flush(struct nft_ctx * ctx,int family)1498 static int nft_flush(struct nft_ctx *ctx, int family)
1499 {
1500 	struct nftables_pernet *nft_net = nft_pernet(ctx->net);
1501 	const struct nlattr * const *nla = ctx->nla;
1502 	struct nft_table *table, *nt;
1503 	int err = 0;
1504 
1505 	list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
1506 		if (family != AF_UNSPEC && table->family != family)
1507 			continue;
1508 
1509 		ctx->family = table->family;
1510 
1511 		if (!nft_is_active_next(ctx->net, table))
1512 			continue;
1513 
1514 		if (nft_table_has_owner(table) && table->nlpid != ctx->portid)
1515 			continue;
1516 
1517 		if (nla[NFTA_TABLE_NAME] &&
1518 		    nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1519 			continue;
1520 
1521 		ctx->table = table;
1522 
1523 		err = nft_flush_table(ctx);
1524 		if (err < 0)
1525 			goto out;
1526 	}
1527 out:
1528 	return err;
1529 }
1530 
nf_tables_deltable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1531 static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info,
1532 			      const struct nlattr * const nla[])
1533 {
1534 	struct netlink_ext_ack *extack = info->extack;
1535 	u8 genmask = nft_genmask_next(info->net);
1536 	u8 family = info->nfmsg->nfgen_family;
1537 	struct net *net = info->net;
1538 	const struct nlattr *attr;
1539 	struct nft_table *table;
1540 	struct nft_ctx ctx;
1541 
1542 	nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla);
1543 	if (family == AF_UNSPEC ||
1544 	    (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
1545 		return nft_flush(&ctx, family);
1546 
1547 	if (nla[NFTA_TABLE_HANDLE]) {
1548 		attr = nla[NFTA_TABLE_HANDLE];
1549 		table = nft_table_lookup_byhandle(net, attr, genmask,
1550 						  NETLINK_CB(skb).portid);
1551 	} else {
1552 		attr = nla[NFTA_TABLE_NAME];
1553 		table = nft_table_lookup(net, attr, family, genmask,
1554 					 NETLINK_CB(skb).portid);
1555 	}
1556 
1557 	if (IS_ERR(table)) {
1558 		if (PTR_ERR(table) == -ENOENT &&
1559 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE)
1560 			return 0;
1561 
1562 		NL_SET_BAD_ATTR(extack, attr);
1563 		return PTR_ERR(table);
1564 	}
1565 
1566 	if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
1567 	    table->use > 0)
1568 		return -EBUSY;
1569 
1570 	ctx.family = family;
1571 	ctx.table = table;
1572 
1573 	return nft_flush_table(&ctx);
1574 }
1575 
nf_tables_table_destroy(struct nft_ctx * ctx)1576 static void nf_tables_table_destroy(struct nft_ctx *ctx)
1577 {
1578 	if (WARN_ON(ctx->table->use > 0))
1579 		return;
1580 
1581 	rhltable_destroy(&ctx->table->chains_ht);
1582 	kfree(ctx->table->name);
1583 	kfree(ctx->table->udata);
1584 	kfree(ctx->table);
1585 }
1586 
nft_register_chain_type(const struct nft_chain_type * ctype)1587 void nft_register_chain_type(const struct nft_chain_type *ctype)
1588 {
1589 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
1590 	if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) {
1591 		nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1592 		return;
1593 	}
1594 	chain_type[ctype->family][ctype->type] = ctype;
1595 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1596 }
1597 EXPORT_SYMBOL_GPL(nft_register_chain_type);
1598 
nft_unregister_chain_type(const struct nft_chain_type * ctype)1599 void nft_unregister_chain_type(const struct nft_chain_type *ctype)
1600 {
1601 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
1602 	chain_type[ctype->family][ctype->type] = NULL;
1603 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1604 }
1605 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
1606 
1607 /*
1608  * Chains
1609  */
1610 
1611 static struct nft_chain *
nft_chain_lookup_byhandle(const struct nft_table * table,u64 handle,u8 genmask)1612 nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
1613 {
1614 	struct nft_chain *chain;
1615 
1616 	list_for_each_entry(chain, &table->chains, list) {
1617 		if (chain->handle == handle &&
1618 		    nft_active_genmask(chain, genmask))
1619 			return chain;
1620 	}
1621 
1622 	return ERR_PTR(-ENOENT);
1623 }
1624 
lockdep_commit_lock_is_held(const struct net * net)1625 static bool lockdep_commit_lock_is_held(const struct net *net)
1626 {
1627 #ifdef CONFIG_PROVE_LOCKING
1628 	struct nftables_pernet *nft_net = nft_pernet(net);
1629 
1630 	return lockdep_is_held(&nft_net->commit_mutex);
1631 #else
1632 	return true;
1633 #endif
1634 }
1635 
nft_chain_lookup(struct net * net,struct nft_table * table,const struct nlattr * nla,u8 genmask)1636 static struct nft_chain *nft_chain_lookup(struct net *net,
1637 					  struct nft_table *table,
1638 					  const struct nlattr *nla, u8 genmask)
1639 {
1640 	char search[NFT_CHAIN_MAXNAMELEN + 1];
1641 	struct rhlist_head *tmp, *list;
1642 	struct nft_chain *chain;
1643 
1644 	if (nla == NULL)
1645 		return ERR_PTR(-EINVAL);
1646 
1647 	nla_strscpy(search, nla, sizeof(search));
1648 
1649 	WARN_ON(!rcu_read_lock_held() &&
1650 		!lockdep_commit_lock_is_held(net));
1651 
1652 	chain = ERR_PTR(-ENOENT);
1653 	rcu_read_lock();
1654 	list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1655 	if (!list)
1656 		goto out_unlock;
1657 
1658 	rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1659 		if (nft_active_genmask(chain, genmask))
1660 			goto out_unlock;
1661 	}
1662 	chain = ERR_PTR(-ENOENT);
1663 out_unlock:
1664 	rcu_read_unlock();
1665 	return chain;
1666 }
1667 
1668 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
1669 	[NFTA_CHAIN_TABLE]	= { .type = NLA_STRING,
1670 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
1671 	[NFTA_CHAIN_HANDLE]	= { .type = NLA_U64 },
1672 	[NFTA_CHAIN_NAME]	= { .type = NLA_STRING,
1673 				    .len = NFT_CHAIN_MAXNAMELEN - 1 },
1674 	[NFTA_CHAIN_HOOK]	= { .type = NLA_NESTED },
1675 	[NFTA_CHAIN_POLICY]	= { .type = NLA_U32 },
1676 	[NFTA_CHAIN_TYPE]	= { .type = NLA_STRING,
1677 				    .len = NFT_MODULE_AUTOLOAD_LIMIT },
1678 	[NFTA_CHAIN_COUNTERS]	= { .type = NLA_NESTED },
1679 	[NFTA_CHAIN_FLAGS]	= { .type = NLA_U32 },
1680 	[NFTA_CHAIN_ID]		= { .type = NLA_U32 },
1681 	[NFTA_CHAIN_USERDATA]	= { .type = NLA_BINARY,
1682 				    .len = NFT_USERDATA_MAXLEN },
1683 };
1684 
1685 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1686 	[NFTA_HOOK_HOOKNUM]	= { .type = NLA_U32 },
1687 	[NFTA_HOOK_PRIORITY]	= { .type = NLA_U32 },
1688 	[NFTA_HOOK_DEV]		= { .type = NLA_STRING,
1689 				    .len = IFNAMSIZ - 1 },
1690 };
1691 
nft_dump_stats(struct sk_buff * skb,struct nft_stats __percpu * stats)1692 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1693 {
1694 	struct nft_stats *cpu_stats, total;
1695 	struct nlattr *nest;
1696 	unsigned int seq;
1697 	u64 pkts, bytes;
1698 	int cpu;
1699 
1700 	if (!stats)
1701 		return 0;
1702 
1703 	memset(&total, 0, sizeof(total));
1704 	for_each_possible_cpu(cpu) {
1705 		cpu_stats = per_cpu_ptr(stats, cpu);
1706 		do {
1707 			seq = u64_stats_fetch_begin(&cpu_stats->syncp);
1708 			pkts = cpu_stats->pkts;
1709 			bytes = cpu_stats->bytes;
1710 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, seq));
1711 		total.pkts += pkts;
1712 		total.bytes += bytes;
1713 	}
1714 	nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
1715 	if (nest == NULL)
1716 		goto nla_put_failure;
1717 
1718 	if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1719 			 NFTA_COUNTER_PAD) ||
1720 	    nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1721 			 NFTA_COUNTER_PAD))
1722 		goto nla_put_failure;
1723 
1724 	nla_nest_end(skb, nest);
1725 	return 0;
1726 
1727 nla_put_failure:
1728 	return -ENOSPC;
1729 }
1730 
nft_dump_basechain_hook(struct sk_buff * skb,int family,const struct nft_base_chain * basechain,const struct list_head * hook_list)1731 static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
1732 				   const struct nft_base_chain *basechain,
1733 				   const struct list_head *hook_list)
1734 {
1735 	const struct nf_hook_ops *ops = &basechain->ops;
1736 	struct nft_hook *hook, *first = NULL;
1737 	struct nlattr *nest, *nest_devs;
1738 	int n = 0;
1739 
1740 	nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1741 	if (nest == NULL)
1742 		goto nla_put_failure;
1743 	if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1744 		goto nla_put_failure;
1745 	if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1746 		goto nla_put_failure;
1747 
1748 	if (nft_base_chain_netdev(family, ops->hooknum)) {
1749 		nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS);
1750 		if (!nest_devs)
1751 			goto nla_put_failure;
1752 
1753 		if (!hook_list)
1754 			hook_list = &basechain->hook_list;
1755 
1756 		list_for_each_entry(hook, hook_list, list) {
1757 			if (!first)
1758 				first = hook;
1759 
1760 			if (nla_put_string(skb, NFTA_DEVICE_NAME,
1761 					   hook->ops.dev->name))
1762 				goto nla_put_failure;
1763 			n++;
1764 		}
1765 		nla_nest_end(skb, nest_devs);
1766 
1767 		if (n == 1 &&
1768 		    nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name))
1769 			goto nla_put_failure;
1770 	}
1771 	nla_nest_end(skb, nest);
1772 
1773 	return 0;
1774 nla_put_failure:
1775 	return -1;
1776 }
1777 
nf_tables_fill_chain_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table,const struct nft_chain * chain,const struct list_head * hook_list)1778 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1779 				     u32 portid, u32 seq, int event, u32 flags,
1780 				     int family, const struct nft_table *table,
1781 				     const struct nft_chain *chain,
1782 				     const struct list_head *hook_list)
1783 {
1784 	struct nlmsghdr *nlh;
1785 
1786 	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
1787 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
1788 			   NFNETLINK_V0, nft_base_seq(net));
1789 	if (!nlh)
1790 		goto nla_put_failure;
1791 
1792 	if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name) ||
1793 	    nla_put_string(skb, NFTA_CHAIN_NAME, chain->name) ||
1794 	    nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1795 			 NFTA_CHAIN_PAD))
1796 		goto nla_put_failure;
1797 
1798 	if (event == NFT_MSG_DELCHAIN && !hook_list) {
1799 		nlmsg_end(skb, nlh);
1800 		return 0;
1801 	}
1802 
1803 	if (nft_is_base_chain(chain)) {
1804 		const struct nft_base_chain *basechain = nft_base_chain(chain);
1805 		struct nft_stats __percpu *stats;
1806 
1807 		if (nft_dump_basechain_hook(skb, family, basechain, hook_list))
1808 			goto nla_put_failure;
1809 
1810 		if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1811 				 htonl(basechain->policy)))
1812 			goto nla_put_failure;
1813 
1814 		if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1815 			goto nla_put_failure;
1816 
1817 		stats = rcu_dereference_check(basechain->stats,
1818 					      lockdep_commit_lock_is_held(net));
1819 		if (nft_dump_stats(skb, stats))
1820 			goto nla_put_failure;
1821 	}
1822 
1823 	if (chain->flags &&
1824 	    nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags)))
1825 		goto nla_put_failure;
1826 
1827 	if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1828 		goto nla_put_failure;
1829 
1830 	if (chain->udata &&
1831 	    nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1832 		goto nla_put_failure;
1833 
1834 	nlmsg_end(skb, nlh);
1835 	return 0;
1836 
1837 nla_put_failure:
1838 	nlmsg_trim(skb, nlh);
1839 	return -1;
1840 }
1841 
nf_tables_chain_notify(const struct nft_ctx * ctx,int event,const struct list_head * hook_list)1842 static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event,
1843 				   const struct list_head *hook_list)
1844 {
1845 	struct nftables_pernet *nft_net;
1846 	struct sk_buff *skb;
1847 	u16 flags = 0;
1848 	int err;
1849 
1850 	if (!ctx->report &&
1851 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1852 		return;
1853 
1854 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1855 	if (skb == NULL)
1856 		goto err;
1857 
1858 	if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1859 		flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1860 
1861 	err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1862 					event, flags, ctx->family, ctx->table,
1863 					ctx->chain, hook_list);
1864 	if (err < 0) {
1865 		kfree_skb(skb);
1866 		goto err;
1867 	}
1868 
1869 	nft_net = nft_pernet(ctx->net);
1870 	nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
1871 	return;
1872 err:
1873 	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1874 }
1875 
nf_tables_dump_chains(struct sk_buff * skb,struct netlink_callback * cb)1876 static int nf_tables_dump_chains(struct sk_buff *skb,
1877 				 struct netlink_callback *cb)
1878 {
1879 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1880 	unsigned int idx = 0, s_idx = cb->args[0];
1881 	struct net *net = sock_net(skb->sk);
1882 	int family = nfmsg->nfgen_family;
1883 	struct nftables_pernet *nft_net;
1884 	const struct nft_table *table;
1885 	const struct nft_chain *chain;
1886 
1887 	rcu_read_lock();
1888 	nft_net = nft_pernet(net);
1889 	cb->seq = READ_ONCE(nft_net->base_seq);
1890 
1891 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
1892 		if (family != NFPROTO_UNSPEC && family != table->family)
1893 			continue;
1894 
1895 		list_for_each_entry_rcu(chain, &table->chains, list) {
1896 			if (idx < s_idx)
1897 				goto cont;
1898 			if (idx > s_idx)
1899 				memset(&cb->args[1], 0,
1900 				       sizeof(cb->args) - sizeof(cb->args[0]));
1901 			if (!nft_is_active(net, chain))
1902 				continue;
1903 			if (nf_tables_fill_chain_info(skb, net,
1904 						      NETLINK_CB(cb->skb).portid,
1905 						      cb->nlh->nlmsg_seq,
1906 						      NFT_MSG_NEWCHAIN,
1907 						      NLM_F_MULTI,
1908 						      table->family, table,
1909 						      chain, NULL) < 0)
1910 				goto done;
1911 
1912 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1913 cont:
1914 			idx++;
1915 		}
1916 	}
1917 done:
1918 	rcu_read_unlock();
1919 	cb->args[0] = idx;
1920 	return skb->len;
1921 }
1922 
1923 /* called with rcu_read_lock held */
nf_tables_getchain(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1924 static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info,
1925 			      const struct nlattr * const nla[])
1926 {
1927 	struct netlink_ext_ack *extack = info->extack;
1928 	u8 genmask = nft_genmask_cur(info->net);
1929 	u8 family = info->nfmsg->nfgen_family;
1930 	const struct nft_chain *chain;
1931 	struct net *net = info->net;
1932 	struct nft_table *table;
1933 	struct sk_buff *skb2;
1934 	int err;
1935 
1936 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1937 		struct netlink_dump_control c = {
1938 			.dump = nf_tables_dump_chains,
1939 			.module = THIS_MODULE,
1940 		};
1941 
1942 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
1943 	}
1944 
1945 	table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0);
1946 	if (IS_ERR(table)) {
1947 		NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
1948 		return PTR_ERR(table);
1949 	}
1950 
1951 	chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
1952 	if (IS_ERR(chain)) {
1953 		NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
1954 		return PTR_ERR(chain);
1955 	}
1956 
1957 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1958 	if (!skb2)
1959 		return -ENOMEM;
1960 
1961 	err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1962 					info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN,
1963 					0, family, table, chain, NULL);
1964 	if (err < 0)
1965 		goto err_fill_chain_info;
1966 
1967 	return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1968 
1969 err_fill_chain_info:
1970 	kfree_skb(skb2);
1971 	return err;
1972 }
1973 
1974 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1975 	[NFTA_COUNTER_PACKETS]	= { .type = NLA_U64 },
1976 	[NFTA_COUNTER_BYTES]	= { .type = NLA_U64 },
1977 };
1978 
nft_stats_alloc(const struct nlattr * attr)1979 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1980 {
1981 	struct nlattr *tb[NFTA_COUNTER_MAX+1];
1982 	struct nft_stats __percpu *newstats;
1983 	struct nft_stats *stats;
1984 	int err;
1985 
1986 	err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
1987 					  nft_counter_policy, NULL);
1988 	if (err < 0)
1989 		return ERR_PTR(err);
1990 
1991 	if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1992 		return ERR_PTR(-EINVAL);
1993 
1994 	newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1995 	if (newstats == NULL)
1996 		return ERR_PTR(-ENOMEM);
1997 
1998 	/* Restore old counters on this cpu, no problem. Per-cpu statistics
1999 	 * are not exposed to userspace.
2000 	 */
2001 	preempt_disable();
2002 	stats = this_cpu_ptr(newstats);
2003 	stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
2004 	stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
2005 	preempt_enable();
2006 
2007 	return newstats;
2008 }
2009 
nft_chain_stats_replace(struct nft_trans * trans)2010 static void nft_chain_stats_replace(struct nft_trans *trans)
2011 {
2012 	struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
2013 
2014 	if (!nft_trans_chain_stats(trans))
2015 		return;
2016 
2017 	nft_trans_chain_stats(trans) =
2018 		rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans),
2019 				    lockdep_commit_lock_is_held(trans->ctx.net));
2020 
2021 	if (!nft_trans_chain_stats(trans))
2022 		static_branch_inc(&nft_counters_enabled);
2023 }
2024 
nf_tables_chain_free_chain_rules(struct nft_chain * chain)2025 static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
2026 {
2027 	struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0);
2028 	struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1);
2029 
2030 	if (g0 != g1)
2031 		kvfree(g1);
2032 	kvfree(g0);
2033 
2034 	/* should be NULL either via abort or via successful commit */
2035 	WARN_ON_ONCE(chain->blob_next);
2036 	kvfree(chain->blob_next);
2037 }
2038 
nf_tables_chain_destroy(struct nft_ctx * ctx)2039 void nf_tables_chain_destroy(struct nft_ctx *ctx)
2040 {
2041 	struct nft_chain *chain = ctx->chain;
2042 	struct nft_hook *hook, *next;
2043 
2044 	if (WARN_ON(chain->use > 0))
2045 		return;
2046 
2047 	/* no concurrent access possible anymore */
2048 	nf_tables_chain_free_chain_rules(chain);
2049 
2050 	if (nft_is_base_chain(chain)) {
2051 		struct nft_base_chain *basechain = nft_base_chain(chain);
2052 
2053 		if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
2054 			list_for_each_entry_safe(hook, next,
2055 						 &basechain->hook_list, list) {
2056 				list_del_rcu(&hook->list);
2057 				kfree_rcu(hook, rcu);
2058 			}
2059 		}
2060 		module_put(basechain->type->owner);
2061 		if (rcu_access_pointer(basechain->stats)) {
2062 			static_branch_dec(&nft_counters_enabled);
2063 			free_percpu(rcu_dereference_raw(basechain->stats));
2064 		}
2065 		kfree(chain->name);
2066 		kfree(chain->udata);
2067 		kfree(basechain);
2068 	} else {
2069 		kfree(chain->name);
2070 		kfree(chain->udata);
2071 		kfree(chain);
2072 	}
2073 }
2074 
nft_netdev_hook_alloc(struct net * net,const struct nlattr * attr)2075 static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
2076 					      const struct nlattr *attr)
2077 {
2078 	struct net_device *dev;
2079 	char ifname[IFNAMSIZ];
2080 	struct nft_hook *hook;
2081 	int err;
2082 
2083 	hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT);
2084 	if (!hook) {
2085 		err = -ENOMEM;
2086 		goto err_hook_alloc;
2087 	}
2088 
2089 	nla_strscpy(ifname, attr, IFNAMSIZ);
2090 	/* nf_tables_netdev_event() is called under rtnl_mutex, this is
2091 	 * indirectly serializing all the other holders of the commit_mutex with
2092 	 * the rtnl_mutex.
2093 	 */
2094 	dev = __dev_get_by_name(net, ifname);
2095 	if (!dev) {
2096 		err = -ENOENT;
2097 		goto err_hook_dev;
2098 	}
2099 	hook->ops.dev = dev;
2100 
2101 	return hook;
2102 
2103 err_hook_dev:
2104 	kfree(hook);
2105 err_hook_alloc:
2106 	return ERR_PTR(err);
2107 }
2108 
nft_hook_list_find(struct list_head * hook_list,const struct nft_hook * this)2109 static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,
2110 					   const struct nft_hook *this)
2111 {
2112 	struct nft_hook *hook;
2113 
2114 	list_for_each_entry(hook, hook_list, list) {
2115 		if (this->ops.dev == hook->ops.dev)
2116 			return hook;
2117 	}
2118 
2119 	return NULL;
2120 }
2121 
nf_tables_parse_netdev_hooks(struct net * net,const struct nlattr * attr,struct list_head * hook_list,struct netlink_ext_ack * extack)2122 static int nf_tables_parse_netdev_hooks(struct net *net,
2123 					const struct nlattr *attr,
2124 					struct list_head *hook_list,
2125 					struct netlink_ext_ack *extack)
2126 {
2127 	struct nft_hook *hook, *next;
2128 	const struct nlattr *tmp;
2129 	int rem, n = 0, err;
2130 
2131 	nla_for_each_nested(tmp, attr, rem) {
2132 		if (nla_type(tmp) != NFTA_DEVICE_NAME) {
2133 			err = -EINVAL;
2134 			goto err_hook;
2135 		}
2136 
2137 		hook = nft_netdev_hook_alloc(net, tmp);
2138 		if (IS_ERR(hook)) {
2139 			NL_SET_BAD_ATTR(extack, tmp);
2140 			err = PTR_ERR(hook);
2141 			goto err_hook;
2142 		}
2143 		if (nft_hook_list_find(hook_list, hook)) {
2144 			NL_SET_BAD_ATTR(extack, tmp);
2145 			kfree(hook);
2146 			err = -EEXIST;
2147 			goto err_hook;
2148 		}
2149 		list_add_tail(&hook->list, hook_list);
2150 		n++;
2151 
2152 		if (n == NFT_NETDEVICE_MAX) {
2153 			err = -EFBIG;
2154 			goto err_hook;
2155 		}
2156 	}
2157 
2158 	return 0;
2159 
2160 err_hook:
2161 	list_for_each_entry_safe(hook, next, hook_list, list) {
2162 		list_del(&hook->list);
2163 		kfree(hook);
2164 	}
2165 	return err;
2166 }
2167 
2168 struct nft_chain_hook {
2169 	u32				num;
2170 	s32				priority;
2171 	const struct nft_chain_type	*type;
2172 	struct list_head		list;
2173 };
2174 
nft_chain_parse_netdev(struct net * net,struct nlattr * tb[],struct list_head * hook_list,struct netlink_ext_ack * extack,u32 flags)2175 static int nft_chain_parse_netdev(struct net *net, struct nlattr *tb[],
2176 				  struct list_head *hook_list,
2177 				  struct netlink_ext_ack *extack, u32 flags)
2178 {
2179 	struct nft_hook *hook;
2180 	int err;
2181 
2182 	if (tb[NFTA_HOOK_DEV]) {
2183 		hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
2184 		if (IS_ERR(hook)) {
2185 			NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]);
2186 			return PTR_ERR(hook);
2187 		}
2188 
2189 		list_add_tail(&hook->list, hook_list);
2190 	} else if (tb[NFTA_HOOK_DEVS]) {
2191 		err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS],
2192 						   hook_list, extack);
2193 		if (err < 0)
2194 			return err;
2195 
2196 	}
2197 
2198 	if (flags & NFT_CHAIN_HW_OFFLOAD &&
2199 	    list_empty(hook_list))
2200 		return -EINVAL;
2201 
2202 	return 0;
2203 }
2204 
nft_chain_parse_hook(struct net * net,struct nft_base_chain * basechain,const struct nlattr * const nla[],struct nft_chain_hook * hook,u8 family,u32 flags,struct netlink_ext_ack * extack)2205 static int nft_chain_parse_hook(struct net *net,
2206 				struct nft_base_chain *basechain,
2207 				const struct nlattr * const nla[],
2208 				struct nft_chain_hook *hook, u8 family,
2209 				u32 flags, struct netlink_ext_ack *extack)
2210 {
2211 	struct nftables_pernet *nft_net = nft_pernet(net);
2212 	struct nlattr *ha[NFTA_HOOK_MAX + 1];
2213 	const struct nft_chain_type *type;
2214 	int err;
2215 
2216 	lockdep_assert_held(&nft_net->commit_mutex);
2217 	lockdep_nfnl_nft_mutex_not_held();
2218 
2219 	err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
2220 					  nla[NFTA_CHAIN_HOOK],
2221 					  nft_hook_policy, NULL);
2222 	if (err < 0)
2223 		return err;
2224 
2225 	if (!basechain) {
2226 		if (!ha[NFTA_HOOK_HOOKNUM] ||
2227 		    !ha[NFTA_HOOK_PRIORITY]) {
2228 			NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2229 			return -ENOENT;
2230 		}
2231 
2232 		hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2233 		hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2234 
2235 		type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT);
2236 		if (!type)
2237 			return -EOPNOTSUPP;
2238 
2239 		if (nla[NFTA_CHAIN_TYPE]) {
2240 			type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
2241 							   family, true);
2242 			if (IS_ERR(type)) {
2243 				NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2244 				return PTR_ERR(type);
2245 			}
2246 		}
2247 		if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
2248 			return -EOPNOTSUPP;
2249 
2250 		if (type->type == NFT_CHAIN_T_NAT &&
2251 		    hook->priority <= NF_IP_PRI_CONNTRACK)
2252 			return -EOPNOTSUPP;
2253 	} else {
2254 		if (ha[NFTA_HOOK_HOOKNUM]) {
2255 			hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2256 			if (hook->num != basechain->ops.hooknum)
2257 				return -EOPNOTSUPP;
2258 		}
2259 		if (ha[NFTA_HOOK_PRIORITY]) {
2260 			hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2261 			if (hook->priority != basechain->ops.priority)
2262 				return -EOPNOTSUPP;
2263 		}
2264 
2265 		type = basechain->type;
2266 	}
2267 
2268 	if (!try_module_get(type->owner)) {
2269 		if (nla[NFTA_CHAIN_TYPE])
2270 			NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2271 		return -ENOENT;
2272 	}
2273 
2274 	hook->type = type;
2275 
2276 	INIT_LIST_HEAD(&hook->list);
2277 	if (nft_base_chain_netdev(family, hook->num)) {
2278 		err = nft_chain_parse_netdev(net, ha, &hook->list, extack, flags);
2279 		if (err < 0) {
2280 			module_put(type->owner);
2281 			return err;
2282 		}
2283 	} else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) {
2284 		module_put(type->owner);
2285 		return -EOPNOTSUPP;
2286 	}
2287 
2288 	return 0;
2289 }
2290 
nft_chain_release_hook(struct nft_chain_hook * hook)2291 static void nft_chain_release_hook(struct nft_chain_hook *hook)
2292 {
2293 	struct nft_hook *h, *next;
2294 
2295 	list_for_each_entry_safe(h, next, &hook->list, list) {
2296 		list_del(&h->list);
2297 		kfree(h);
2298 	}
2299 	module_put(hook->type->owner);
2300 }
2301 
nft_last_rule(const struct nft_chain * chain,const void * ptr)2302 static void nft_last_rule(const struct nft_chain *chain, const void *ptr)
2303 {
2304 	struct nft_rule_dp_last *lrule;
2305 
2306 	BUILD_BUG_ON(offsetof(struct nft_rule_dp_last, end) != 0);
2307 
2308 	lrule = (struct nft_rule_dp_last *)ptr;
2309 	lrule->end.is_last = 1;
2310 	lrule->chain = chain;
2311 	/* blob size does not include the trailer rule */
2312 }
2313 
nf_tables_chain_alloc_rules(const struct nft_chain * chain,unsigned int size)2314 static struct nft_rule_blob *nf_tables_chain_alloc_rules(const struct nft_chain *chain,
2315 							 unsigned int size)
2316 {
2317 	struct nft_rule_blob *blob;
2318 
2319 	if (size > INT_MAX)
2320 		return NULL;
2321 
2322 	size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rule_dp_last);
2323 
2324 	blob = kvmalloc(size, GFP_KERNEL_ACCOUNT);
2325 	if (!blob)
2326 		return NULL;
2327 
2328 	blob->size = 0;
2329 	nft_last_rule(chain, blob->data);
2330 
2331 	return blob;
2332 }
2333 
nft_basechain_hook_init(struct nf_hook_ops * ops,u8 family,const struct nft_chain_hook * hook,struct nft_chain * chain)2334 static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family,
2335 				    const struct nft_chain_hook *hook,
2336 				    struct nft_chain *chain)
2337 {
2338 	ops->pf			= family;
2339 	ops->hooknum		= hook->num;
2340 	ops->priority		= hook->priority;
2341 	ops->priv		= chain;
2342 	ops->hook		= hook->type->hooks[ops->hooknum];
2343 	ops->hook_ops_type	= NF_HOOK_OP_NF_TABLES;
2344 }
2345 
nft_basechain_init(struct nft_base_chain * basechain,u8 family,struct nft_chain_hook * hook,u32 flags)2346 static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,
2347 			      struct nft_chain_hook *hook, u32 flags)
2348 {
2349 	struct nft_chain *chain;
2350 	struct nft_hook *h;
2351 
2352 	basechain->type = hook->type;
2353 	INIT_LIST_HEAD(&basechain->hook_list);
2354 	chain = &basechain->chain;
2355 
2356 	if (nft_base_chain_netdev(family, hook->num)) {
2357 		list_splice_init(&hook->list, &basechain->hook_list);
2358 		list_for_each_entry(h, &basechain->hook_list, list)
2359 			nft_basechain_hook_init(&h->ops, family, hook, chain);
2360 	}
2361 	nft_basechain_hook_init(&basechain->ops, family, hook, chain);
2362 
2363 	chain->flags |= NFT_CHAIN_BASE | flags;
2364 	basechain->policy = NF_ACCEPT;
2365 	if (chain->flags & NFT_CHAIN_HW_OFFLOAD &&
2366 	    !nft_chain_offload_support(basechain)) {
2367 		list_splice_init(&basechain->hook_list, &hook->list);
2368 		return -EOPNOTSUPP;
2369 	}
2370 
2371 	flow_block_init(&basechain->flow_block);
2372 
2373 	return 0;
2374 }
2375 
nft_chain_add(struct nft_table * table,struct nft_chain * chain)2376 int nft_chain_add(struct nft_table *table, struct nft_chain *chain)
2377 {
2378 	int err;
2379 
2380 	err = rhltable_insert_key(&table->chains_ht, chain->name,
2381 				  &chain->rhlhead, nft_chain_ht_params);
2382 	if (err)
2383 		return err;
2384 
2385 	list_add_tail_rcu(&chain->list, &table->chains);
2386 
2387 	return 0;
2388 }
2389 
2390 static u64 chain_id;
2391 
nf_tables_addchain(struct nft_ctx * ctx,u8 family,u8 genmask,u8 policy,u32 flags,struct netlink_ext_ack * extack)2392 static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
2393 			      u8 policy, u32 flags,
2394 			      struct netlink_ext_ack *extack)
2395 {
2396 	const struct nlattr * const *nla = ctx->nla;
2397 	struct nft_table *table = ctx->table;
2398 	struct nft_base_chain *basechain;
2399 	struct net *net = ctx->net;
2400 	char name[NFT_NAME_MAXLEN];
2401 	struct nft_rule_blob *blob;
2402 	struct nft_trans *trans;
2403 	struct nft_chain *chain;
2404 	int err;
2405 
2406 	if (nla[NFTA_CHAIN_HOOK]) {
2407 		struct nft_stats __percpu *stats = NULL;
2408 		struct nft_chain_hook hook = {};
2409 
2410 		if (flags & NFT_CHAIN_BINDING)
2411 			return -EOPNOTSUPP;
2412 
2413 		err = nft_chain_parse_hook(net, NULL, nla, &hook, family, flags,
2414 					   extack);
2415 		if (err < 0)
2416 			return err;
2417 
2418 		basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT);
2419 		if (basechain == NULL) {
2420 			nft_chain_release_hook(&hook);
2421 			return -ENOMEM;
2422 		}
2423 		chain = &basechain->chain;
2424 
2425 		if (nla[NFTA_CHAIN_COUNTERS]) {
2426 			stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2427 			if (IS_ERR(stats)) {
2428 				nft_chain_release_hook(&hook);
2429 				kfree(basechain);
2430 				return PTR_ERR(stats);
2431 			}
2432 			rcu_assign_pointer(basechain->stats, stats);
2433 		}
2434 
2435 		err = nft_basechain_init(basechain, family, &hook, flags);
2436 		if (err < 0) {
2437 			nft_chain_release_hook(&hook);
2438 			kfree(basechain);
2439 			free_percpu(stats);
2440 			return err;
2441 		}
2442 		if (stats)
2443 			static_branch_inc(&nft_counters_enabled);
2444 	} else {
2445 		if (flags & NFT_CHAIN_BASE)
2446 			return -EINVAL;
2447 		if (flags & NFT_CHAIN_HW_OFFLOAD)
2448 			return -EOPNOTSUPP;
2449 
2450 		chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT);
2451 		if (chain == NULL)
2452 			return -ENOMEM;
2453 
2454 		chain->flags = flags;
2455 	}
2456 	ctx->chain = chain;
2457 
2458 	INIT_LIST_HEAD(&chain->rules);
2459 	chain->handle = nf_tables_alloc_handle(table);
2460 	chain->table = table;
2461 
2462 	if (nla[NFTA_CHAIN_NAME]) {
2463 		chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2464 	} else {
2465 		if (!(flags & NFT_CHAIN_BINDING)) {
2466 			err = -EINVAL;
2467 			goto err_destroy_chain;
2468 		}
2469 
2470 		snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2471 		chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
2472 	}
2473 
2474 	if (!chain->name) {
2475 		err = -ENOMEM;
2476 		goto err_destroy_chain;
2477 	}
2478 
2479 	if (nla[NFTA_CHAIN_USERDATA]) {
2480 		chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT);
2481 		if (chain->udata == NULL) {
2482 			err = -ENOMEM;
2483 			goto err_destroy_chain;
2484 		}
2485 		chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
2486 	}
2487 
2488 	blob = nf_tables_chain_alloc_rules(chain, 0);
2489 	if (!blob) {
2490 		err = -ENOMEM;
2491 		goto err_destroy_chain;
2492 	}
2493 
2494 	RCU_INIT_POINTER(chain->blob_gen_0, blob);
2495 	RCU_INIT_POINTER(chain->blob_gen_1, blob);
2496 
2497 	err = nf_tables_register_hook(net, table, chain);
2498 	if (err < 0)
2499 		goto err_destroy_chain;
2500 
2501 	if (!nft_use_inc(&table->use)) {
2502 		err = -EMFILE;
2503 		goto err_use;
2504 	}
2505 
2506 	trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2507 	if (IS_ERR(trans)) {
2508 		err = PTR_ERR(trans);
2509 		goto err_unregister_hook;
2510 	}
2511 
2512 	nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
2513 	if (nft_is_base_chain(chain))
2514 		nft_trans_chain_policy(trans) = policy;
2515 
2516 	err = nft_chain_add(table, chain);
2517 	if (err < 0) {
2518 		nft_trans_destroy(trans);
2519 		goto err_unregister_hook;
2520 	}
2521 
2522 	return 0;
2523 
2524 err_unregister_hook:
2525 	nft_use_dec_restore(&table->use);
2526 err_use:
2527 	nf_tables_unregister_hook(net, table, chain);
2528 err_destroy_chain:
2529 	nf_tables_chain_destroy(ctx);
2530 
2531 	return err;
2532 }
2533 
nf_tables_updchain(struct nft_ctx * ctx,u8 genmask,u8 policy,u32 flags,const struct nlattr * attr,struct netlink_ext_ack * extack)2534 static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
2535 			      u32 flags, const struct nlattr *attr,
2536 			      struct netlink_ext_ack *extack)
2537 {
2538 	const struct nlattr * const *nla = ctx->nla;
2539 	struct nft_base_chain *basechain = NULL;
2540 	struct nft_table *table = ctx->table;
2541 	struct nft_chain *chain = ctx->chain;
2542 	struct nft_chain_hook hook = {};
2543 	struct nft_stats *stats = NULL;
2544 	struct nft_hook *h, *next;
2545 	struct nf_hook_ops *ops;
2546 	struct nft_trans *trans;
2547 	bool unregister = false;
2548 	int err;
2549 
2550 	if (chain->flags ^ flags)
2551 		return -EOPNOTSUPP;
2552 
2553 	INIT_LIST_HEAD(&hook.list);
2554 
2555 	if (nla[NFTA_CHAIN_HOOK]) {
2556 		if (!nft_is_base_chain(chain)) {
2557 			NL_SET_BAD_ATTR(extack, attr);
2558 			return -EEXIST;
2559 		}
2560 
2561 		basechain = nft_base_chain(chain);
2562 		err = nft_chain_parse_hook(ctx->net, basechain, nla, &hook,
2563 					   ctx->family, flags, extack);
2564 		if (err < 0)
2565 			return err;
2566 
2567 		if (basechain->type != hook.type) {
2568 			nft_chain_release_hook(&hook);
2569 			NL_SET_BAD_ATTR(extack, attr);
2570 			return -EEXIST;
2571 		}
2572 
2573 		if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
2574 			list_for_each_entry_safe(h, next, &hook.list, list) {
2575 				h->ops.pf	= basechain->ops.pf;
2576 				h->ops.hooknum	= basechain->ops.hooknum;
2577 				h->ops.priority	= basechain->ops.priority;
2578 				h->ops.priv	= basechain->ops.priv;
2579 				h->ops.hook	= basechain->ops.hook;
2580 
2581 				if (nft_hook_list_find(&basechain->hook_list, h)) {
2582 					list_del(&h->list);
2583 					kfree(h);
2584 				}
2585 			}
2586 		} else {
2587 			ops = &basechain->ops;
2588 			if (ops->hooknum != hook.num ||
2589 			    ops->priority != hook.priority) {
2590 				nft_chain_release_hook(&hook);
2591 				NL_SET_BAD_ATTR(extack, attr);
2592 				return -EEXIST;
2593 			}
2594 		}
2595 	}
2596 
2597 	if (nla[NFTA_CHAIN_HANDLE] &&
2598 	    nla[NFTA_CHAIN_NAME]) {
2599 		struct nft_chain *chain2;
2600 
2601 		chain2 = nft_chain_lookup(ctx->net, table,
2602 					  nla[NFTA_CHAIN_NAME], genmask);
2603 		if (!IS_ERR(chain2)) {
2604 			NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2605 			err = -EEXIST;
2606 			goto err_hooks;
2607 		}
2608 	}
2609 
2610 	if (nla[NFTA_CHAIN_COUNTERS]) {
2611 		if (!nft_is_base_chain(chain)) {
2612 			err = -EOPNOTSUPP;
2613 			goto err_hooks;
2614 		}
2615 
2616 		stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2617 		if (IS_ERR(stats)) {
2618 			err = PTR_ERR(stats);
2619 			goto err_hooks;
2620 		}
2621 	}
2622 
2623 	if (!(table->flags & NFT_TABLE_F_DORMANT) &&
2624 	    nft_is_base_chain(chain) &&
2625 	    !list_empty(&hook.list)) {
2626 		basechain = nft_base_chain(chain);
2627 		ops = &basechain->ops;
2628 
2629 		if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
2630 			err = nft_netdev_register_hooks(ctx->net, &hook.list);
2631 			if (err < 0)
2632 				goto err_hooks;
2633 		}
2634 	}
2635 
2636 	unregister = true;
2637 	err = -ENOMEM;
2638 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
2639 				sizeof(struct nft_trans_chain));
2640 	if (trans == NULL)
2641 		goto err_trans;
2642 
2643 	nft_trans_chain_stats(trans) = stats;
2644 	nft_trans_chain_update(trans) = true;
2645 
2646 	if (nla[NFTA_CHAIN_POLICY])
2647 		nft_trans_chain_policy(trans) = policy;
2648 	else
2649 		nft_trans_chain_policy(trans) = -1;
2650 
2651 	if (nla[NFTA_CHAIN_HANDLE] &&
2652 	    nla[NFTA_CHAIN_NAME]) {
2653 		struct nftables_pernet *nft_net = nft_pernet(ctx->net);
2654 		struct nft_trans *tmp;
2655 		char *name;
2656 
2657 		err = -ENOMEM;
2658 		name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2659 		if (!name)
2660 			goto err_trans;
2661 
2662 		err = -EEXIST;
2663 		list_for_each_entry(tmp, &nft_net->commit_list, list) {
2664 			if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
2665 			    tmp->ctx.table == table &&
2666 			    nft_trans_chain_update(tmp) &&
2667 			    nft_trans_chain_name(tmp) &&
2668 			    strcmp(name, nft_trans_chain_name(tmp)) == 0) {
2669 				NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2670 				kfree(name);
2671 				goto err_trans;
2672 			}
2673 		}
2674 
2675 		nft_trans_chain_name(trans) = name;
2676 	}
2677 
2678 	nft_trans_basechain(trans) = basechain;
2679 	INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2680 	list_splice(&hook.list, &nft_trans_chain_hooks(trans));
2681 	if (nla[NFTA_CHAIN_HOOK])
2682 		module_put(hook.type->owner);
2683 
2684 	nft_trans_commit_list_add_tail(ctx->net, trans);
2685 
2686 	return 0;
2687 
2688 err_trans:
2689 	free_percpu(stats);
2690 	kfree(trans);
2691 err_hooks:
2692 	if (nla[NFTA_CHAIN_HOOK]) {
2693 		list_for_each_entry_safe(h, next, &hook.list, list) {
2694 			if (unregister)
2695 				nf_unregister_net_hook(ctx->net, &h->ops);
2696 			list_del(&h->list);
2697 			kfree_rcu(h, rcu);
2698 		}
2699 		module_put(hook.type->owner);
2700 	}
2701 
2702 	return err;
2703 }
2704 
nft_chain_lookup_byid(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u8 genmask)2705 static struct nft_chain *nft_chain_lookup_byid(const struct net *net,
2706 					       const struct nft_table *table,
2707 					       const struct nlattr *nla, u8 genmask)
2708 {
2709 	struct nftables_pernet *nft_net = nft_pernet(net);
2710 	u32 id = ntohl(nla_get_be32(nla));
2711 	struct nft_trans *trans;
2712 
2713 	list_for_each_entry(trans, &nft_net->commit_list, list) {
2714 		struct nft_chain *chain = trans->ctx.chain;
2715 
2716 		if (trans->msg_type == NFT_MSG_NEWCHAIN &&
2717 		    chain->table == table &&
2718 		    id == nft_trans_chain_id(trans) &&
2719 		    nft_active_genmask(chain, genmask))
2720 			return chain;
2721 	}
2722 	return ERR_PTR(-ENOENT);
2723 }
2724 
nf_tables_newchain(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])2725 static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,
2726 			      const struct nlattr * const nla[])
2727 {
2728 	struct nftables_pernet *nft_net = nft_pernet(info->net);
2729 	struct netlink_ext_ack *extack = info->extack;
2730 	u8 genmask = nft_genmask_next(info->net);
2731 	u8 family = info->nfmsg->nfgen_family;
2732 	struct nft_chain *chain = NULL;
2733 	struct net *net = info->net;
2734 	const struct nlattr *attr;
2735 	struct nft_table *table;
2736 	u8 policy = NF_ACCEPT;
2737 	struct nft_ctx ctx;
2738 	u64 handle = 0;
2739 	u32 flags = 0;
2740 
2741 	lockdep_assert_held(&nft_net->commit_mutex);
2742 
2743 	table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2744 				 NETLINK_CB(skb).portid);
2745 	if (IS_ERR(table)) {
2746 		NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
2747 		return PTR_ERR(table);
2748 	}
2749 
2750 	chain = NULL;
2751 	attr = nla[NFTA_CHAIN_NAME];
2752 
2753 	if (nla[NFTA_CHAIN_HANDLE]) {
2754 		handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
2755 		chain = nft_chain_lookup_byhandle(table, handle, genmask);
2756 		if (IS_ERR(chain)) {
2757 			NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
2758 			return PTR_ERR(chain);
2759 		}
2760 		attr = nla[NFTA_CHAIN_HANDLE];
2761 	} else if (nla[NFTA_CHAIN_NAME]) {
2762 		chain = nft_chain_lookup(net, table, attr, genmask);
2763 		if (IS_ERR(chain)) {
2764 			if (PTR_ERR(chain) != -ENOENT) {
2765 				NL_SET_BAD_ATTR(extack, attr);
2766 				return PTR_ERR(chain);
2767 			}
2768 			chain = NULL;
2769 		}
2770 	} else if (!nla[NFTA_CHAIN_ID]) {
2771 		return -EINVAL;
2772 	}
2773 
2774 	if (nla[NFTA_CHAIN_POLICY]) {
2775 		if (chain != NULL &&
2776 		    !nft_is_base_chain(chain)) {
2777 			NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
2778 			return -EOPNOTSUPP;
2779 		}
2780 
2781 		if (chain == NULL &&
2782 		    nla[NFTA_CHAIN_HOOK] == NULL) {
2783 			NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
2784 			return -EOPNOTSUPP;
2785 		}
2786 
2787 		policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
2788 		switch (policy) {
2789 		case NF_DROP:
2790 		case NF_ACCEPT:
2791 			break;
2792 		default:
2793 			return -EINVAL;
2794 		}
2795 	}
2796 
2797 	if (nla[NFTA_CHAIN_FLAGS])
2798 		flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS]));
2799 	else if (chain)
2800 		flags = chain->flags;
2801 
2802 	if (flags & ~NFT_CHAIN_FLAGS)
2803 		return -EOPNOTSUPP;
2804 
2805 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2806 
2807 	if (chain != NULL) {
2808 		if (chain->flags & NFT_CHAIN_BINDING)
2809 			return -EINVAL;
2810 
2811 		if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
2812 			NL_SET_BAD_ATTR(extack, attr);
2813 			return -EEXIST;
2814 		}
2815 		if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
2816 			return -EOPNOTSUPP;
2817 
2818 		flags |= chain->flags & NFT_CHAIN_BASE;
2819 		return nf_tables_updchain(&ctx, genmask, policy, flags, attr,
2820 					  extack);
2821 	}
2822 
2823 	return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack);
2824 }
2825 
nft_delchain_hook(struct nft_ctx * ctx,struct nft_base_chain * basechain,struct netlink_ext_ack * extack)2826 static int nft_delchain_hook(struct nft_ctx *ctx,
2827 			     struct nft_base_chain *basechain,
2828 			     struct netlink_ext_ack *extack)
2829 {
2830 	const struct nft_chain *chain = &basechain->chain;
2831 	const struct nlattr * const *nla = ctx->nla;
2832 	struct nft_chain_hook chain_hook = {};
2833 	struct nft_hook *this, *hook;
2834 	LIST_HEAD(chain_del_list);
2835 	struct nft_trans *trans;
2836 	int err;
2837 
2838 	err = nft_chain_parse_hook(ctx->net, basechain, nla, &chain_hook,
2839 				   ctx->family, chain->flags, extack);
2840 	if (err < 0)
2841 		return err;
2842 
2843 	list_for_each_entry(this, &chain_hook.list, list) {
2844 		hook = nft_hook_list_find(&basechain->hook_list, this);
2845 		if (!hook) {
2846 			err = -ENOENT;
2847 			goto err_chain_del_hook;
2848 		}
2849 		list_move(&hook->list, &chain_del_list);
2850 	}
2851 
2852 	trans = nft_trans_alloc(ctx, NFT_MSG_DELCHAIN,
2853 				sizeof(struct nft_trans_chain));
2854 	if (!trans) {
2855 		err = -ENOMEM;
2856 		goto err_chain_del_hook;
2857 	}
2858 
2859 	nft_trans_basechain(trans) = basechain;
2860 	nft_trans_chain_update(trans) = true;
2861 	INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2862 	list_splice(&chain_del_list, &nft_trans_chain_hooks(trans));
2863 	nft_chain_release_hook(&chain_hook);
2864 
2865 	nft_trans_commit_list_add_tail(ctx->net, trans);
2866 
2867 	return 0;
2868 
2869 err_chain_del_hook:
2870 	list_splice(&chain_del_list, &basechain->hook_list);
2871 	nft_chain_release_hook(&chain_hook);
2872 
2873 	return err;
2874 }
2875 
nf_tables_delchain(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])2876 static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,
2877 			      const struct nlattr * const nla[])
2878 {
2879 	struct netlink_ext_ack *extack = info->extack;
2880 	u8 genmask = nft_genmask_next(info->net);
2881 	u8 family = info->nfmsg->nfgen_family;
2882 	struct net *net = info->net;
2883 	const struct nlattr *attr;
2884 	struct nft_table *table;
2885 	struct nft_chain *chain;
2886 	struct nft_rule *rule;
2887 	struct nft_ctx ctx;
2888 	u64 handle;
2889 	u32 use;
2890 	int err;
2891 
2892 	table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2893 				 NETLINK_CB(skb).portid);
2894 	if (IS_ERR(table)) {
2895 		NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
2896 		return PTR_ERR(table);
2897 	}
2898 
2899 	if (nla[NFTA_CHAIN_HANDLE]) {
2900 		attr = nla[NFTA_CHAIN_HANDLE];
2901 		handle = be64_to_cpu(nla_get_be64(attr));
2902 		chain = nft_chain_lookup_byhandle(table, handle, genmask);
2903 	} else {
2904 		attr = nla[NFTA_CHAIN_NAME];
2905 		chain = nft_chain_lookup(net, table, attr, genmask);
2906 	}
2907 	if (IS_ERR(chain)) {
2908 		if (PTR_ERR(chain) == -ENOENT &&
2909 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN)
2910 			return 0;
2911 
2912 		NL_SET_BAD_ATTR(extack, attr);
2913 		return PTR_ERR(chain);
2914 	}
2915 
2916 	if (nft_chain_binding(chain))
2917 		return -EOPNOTSUPP;
2918 
2919 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2920 
2921 	if (nla[NFTA_CHAIN_HOOK]) {
2922 		if (chain->flags & NFT_CHAIN_HW_OFFLOAD)
2923 			return -EOPNOTSUPP;
2924 
2925 		if (nft_is_base_chain(chain)) {
2926 			struct nft_base_chain *basechain = nft_base_chain(chain);
2927 
2928 			if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
2929 				return nft_delchain_hook(&ctx, basechain, extack);
2930 		}
2931 	}
2932 
2933 	if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
2934 	    chain->use > 0)
2935 		return -EBUSY;
2936 
2937 	use = chain->use;
2938 	list_for_each_entry(rule, &chain->rules, list) {
2939 		if (!nft_is_active_next(net, rule))
2940 			continue;
2941 		use--;
2942 
2943 		err = nft_delrule(&ctx, rule);
2944 		if (err < 0)
2945 			return err;
2946 	}
2947 
2948 	/* There are rules and elements that are still holding references to us,
2949 	 * we cannot do a recursive removal in this case.
2950 	 */
2951 	if (use > 0) {
2952 		NL_SET_BAD_ATTR(extack, attr);
2953 		return -EBUSY;
2954 	}
2955 
2956 	return nft_delchain(&ctx);
2957 }
2958 
2959 /*
2960  * Expressions
2961  */
2962 
2963 /**
2964  *	nft_register_expr - register nf_tables expr type
2965  *	@type: expr type
2966  *
2967  *	Registers the expr type for use with nf_tables. Returns zero on
2968  *	success or a negative errno code otherwise.
2969  */
nft_register_expr(struct nft_expr_type * type)2970 int nft_register_expr(struct nft_expr_type *type)
2971 {
2972 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
2973 	if (type->family == NFPROTO_UNSPEC)
2974 		list_add_tail_rcu(&type->list, &nf_tables_expressions);
2975 	else
2976 		list_add_rcu(&type->list, &nf_tables_expressions);
2977 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2978 	return 0;
2979 }
2980 EXPORT_SYMBOL_GPL(nft_register_expr);
2981 
2982 /**
2983  *	nft_unregister_expr - unregister nf_tables expr type
2984  *	@type: expr type
2985  *
2986  * 	Unregisters the expr typefor use with nf_tables.
2987  */
nft_unregister_expr(struct nft_expr_type * type)2988 void nft_unregister_expr(struct nft_expr_type *type)
2989 {
2990 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
2991 	list_del_rcu(&type->list);
2992 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2993 }
2994 EXPORT_SYMBOL_GPL(nft_unregister_expr);
2995 
__nft_expr_type_get(u8 family,struct nlattr * nla)2996 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
2997 						       struct nlattr *nla)
2998 {
2999 	const struct nft_expr_type *type, *candidate = NULL;
3000 
3001 	list_for_each_entry(type, &nf_tables_expressions, list) {
3002 		if (!nla_strcmp(nla, type->name)) {
3003 			if (!type->family && !candidate)
3004 				candidate = type;
3005 			else if (type->family == family)
3006 				candidate = type;
3007 		}
3008 	}
3009 	return candidate;
3010 }
3011 
3012 #ifdef CONFIG_MODULES
nft_expr_type_request_module(struct net * net,u8 family,struct nlattr * nla)3013 static int nft_expr_type_request_module(struct net *net, u8 family,
3014 					struct nlattr *nla)
3015 {
3016 	if (nft_request_module(net, "nft-expr-%u-%.*s", family,
3017 			       nla_len(nla), (char *)nla_data(nla)) == -EAGAIN)
3018 		return -EAGAIN;
3019 
3020 	return 0;
3021 }
3022 #endif
3023 
nft_expr_type_get(struct net * net,u8 family,struct nlattr * nla)3024 static const struct nft_expr_type *nft_expr_type_get(struct net *net,
3025 						     u8 family,
3026 						     struct nlattr *nla)
3027 {
3028 	const struct nft_expr_type *type;
3029 
3030 	if (nla == NULL)
3031 		return ERR_PTR(-EINVAL);
3032 
3033 	type = __nft_expr_type_get(family, nla);
3034 	if (type != NULL && try_module_get(type->owner))
3035 		return type;
3036 
3037 	lockdep_nfnl_nft_mutex_not_held();
3038 #ifdef CONFIG_MODULES
3039 	if (type == NULL) {
3040 		if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
3041 			return ERR_PTR(-EAGAIN);
3042 
3043 		if (nft_request_module(net, "nft-expr-%.*s",
3044 				       nla_len(nla),
3045 				       (char *)nla_data(nla)) == -EAGAIN)
3046 			return ERR_PTR(-EAGAIN);
3047 	}
3048 #endif
3049 	return ERR_PTR(-ENOENT);
3050 }
3051 
3052 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
3053 	[NFTA_EXPR_NAME]	= { .type = NLA_STRING,
3054 				    .len = NFT_MODULE_AUTOLOAD_LIMIT },
3055 	[NFTA_EXPR_DATA]	= { .type = NLA_NESTED },
3056 };
3057 
nf_tables_fill_expr_info(struct sk_buff * skb,const struct nft_expr * expr,bool reset)3058 static int nf_tables_fill_expr_info(struct sk_buff *skb,
3059 				    const struct nft_expr *expr, bool reset)
3060 {
3061 	if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
3062 		goto nla_put_failure;
3063 
3064 	if (expr->ops->dump) {
3065 		struct nlattr *data = nla_nest_start_noflag(skb,
3066 							    NFTA_EXPR_DATA);
3067 		if (data == NULL)
3068 			goto nla_put_failure;
3069 		if (expr->ops->dump(skb, expr, reset) < 0)
3070 			goto nla_put_failure;
3071 		nla_nest_end(skb, data);
3072 	}
3073 
3074 	return skb->len;
3075 
3076 nla_put_failure:
3077 	return -1;
3078 };
3079 
nft_expr_dump(struct sk_buff * skb,unsigned int attr,const struct nft_expr * expr,bool reset)3080 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
3081 		  const struct nft_expr *expr, bool reset)
3082 {
3083 	struct nlattr *nest;
3084 
3085 	nest = nla_nest_start_noflag(skb, attr);
3086 	if (!nest)
3087 		goto nla_put_failure;
3088 	if (nf_tables_fill_expr_info(skb, expr, reset) < 0)
3089 		goto nla_put_failure;
3090 	nla_nest_end(skb, nest);
3091 	return 0;
3092 
3093 nla_put_failure:
3094 	return -1;
3095 }
3096 
3097 struct nft_expr_info {
3098 	const struct nft_expr_ops	*ops;
3099 	const struct nlattr		*attr;
3100 	struct nlattr			*tb[NFT_EXPR_MAXATTR + 1];
3101 };
3102 
nf_tables_expr_parse(const struct nft_ctx * ctx,const struct nlattr * nla,struct nft_expr_info * info)3103 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
3104 				const struct nlattr *nla,
3105 				struct nft_expr_info *info)
3106 {
3107 	const struct nft_expr_type *type;
3108 	const struct nft_expr_ops *ops;
3109 	struct nlattr *tb[NFTA_EXPR_MAX + 1];
3110 	int err;
3111 
3112 	err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
3113 					  nft_expr_policy, NULL);
3114 	if (err < 0)
3115 		return err;
3116 
3117 	type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
3118 	if (IS_ERR(type))
3119 		return PTR_ERR(type);
3120 
3121 	if (tb[NFTA_EXPR_DATA]) {
3122 		err = nla_parse_nested_deprecated(info->tb, type->maxattr,
3123 						  tb[NFTA_EXPR_DATA],
3124 						  type->policy, NULL);
3125 		if (err < 0)
3126 			goto err1;
3127 	} else
3128 		memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
3129 
3130 	if (type->select_ops != NULL) {
3131 		ops = type->select_ops(ctx,
3132 				       (const struct nlattr * const *)info->tb);
3133 		if (IS_ERR(ops)) {
3134 			err = PTR_ERR(ops);
3135 #ifdef CONFIG_MODULES
3136 			if (err == -EAGAIN)
3137 				if (nft_expr_type_request_module(ctx->net,
3138 								 ctx->family,
3139 								 tb[NFTA_EXPR_NAME]) != -EAGAIN)
3140 					err = -ENOENT;
3141 #endif
3142 			goto err1;
3143 		}
3144 	} else
3145 		ops = type->ops;
3146 
3147 	info->attr = nla;
3148 	info->ops = ops;
3149 
3150 	return 0;
3151 
3152 err1:
3153 	module_put(type->owner);
3154 	return err;
3155 }
3156 
nft_expr_inner_parse(const struct nft_ctx * ctx,const struct nlattr * nla,struct nft_expr_info * info)3157 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
3158 			 struct nft_expr_info *info)
3159 {
3160 	struct nlattr *tb[NFTA_EXPR_MAX + 1];
3161 	const struct nft_expr_type *type;
3162 	int err;
3163 
3164 	err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
3165 					  nft_expr_policy, NULL);
3166 	if (err < 0)
3167 		return err;
3168 
3169 	if (!tb[NFTA_EXPR_DATA] || !tb[NFTA_EXPR_NAME])
3170 		return -EINVAL;
3171 
3172 	type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]);
3173 	if (!type)
3174 		return -ENOENT;
3175 
3176 	if (!type->inner_ops)
3177 		return -EOPNOTSUPP;
3178 
3179 	err = nla_parse_nested_deprecated(info->tb, type->maxattr,
3180 					  tb[NFTA_EXPR_DATA],
3181 					  type->policy, NULL);
3182 	if (err < 0)
3183 		goto err_nla_parse;
3184 
3185 	info->attr = nla;
3186 	info->ops = type->inner_ops;
3187 
3188 	return 0;
3189 
3190 err_nla_parse:
3191 	return err;
3192 }
3193 
nf_tables_newexpr(const struct nft_ctx * ctx,const struct nft_expr_info * expr_info,struct nft_expr * expr)3194 static int nf_tables_newexpr(const struct nft_ctx *ctx,
3195 			     const struct nft_expr_info *expr_info,
3196 			     struct nft_expr *expr)
3197 {
3198 	const struct nft_expr_ops *ops = expr_info->ops;
3199 	int err;
3200 
3201 	expr->ops = ops;
3202 	if (ops->init) {
3203 		err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb);
3204 		if (err < 0)
3205 			goto err1;
3206 	}
3207 
3208 	return 0;
3209 err1:
3210 	expr->ops = NULL;
3211 	return err;
3212 }
3213 
nf_tables_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)3214 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
3215 				   struct nft_expr *expr)
3216 {
3217 	const struct nft_expr_type *type = expr->ops->type;
3218 
3219 	if (expr->ops->destroy)
3220 		expr->ops->destroy(ctx, expr);
3221 	module_put(type->owner);
3222 }
3223 
nft_expr_init(const struct nft_ctx * ctx,const struct nlattr * nla)3224 static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
3225 				      const struct nlattr *nla)
3226 {
3227 	struct nft_expr_info expr_info;
3228 	struct nft_expr *expr;
3229 	struct module *owner;
3230 	int err;
3231 
3232 	err = nf_tables_expr_parse(ctx, nla, &expr_info);
3233 	if (err < 0)
3234 		goto err_expr_parse;
3235 
3236 	err = -EOPNOTSUPP;
3237 	if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL))
3238 		goto err_expr_stateful;
3239 
3240 	err = -ENOMEM;
3241 	expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT);
3242 	if (expr == NULL)
3243 		goto err_expr_stateful;
3244 
3245 	err = nf_tables_newexpr(ctx, &expr_info, expr);
3246 	if (err < 0)
3247 		goto err_expr_new;
3248 
3249 	return expr;
3250 err_expr_new:
3251 	kfree(expr);
3252 err_expr_stateful:
3253 	owner = expr_info.ops->type->owner;
3254 	if (expr_info.ops->type->release_ops)
3255 		expr_info.ops->type->release_ops(expr_info.ops);
3256 
3257 	module_put(owner);
3258 err_expr_parse:
3259 	return ERR_PTR(err);
3260 }
3261 
nft_expr_clone(struct nft_expr * dst,struct nft_expr * src)3262 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
3263 {
3264 	int err;
3265 
3266 	if (src->ops->clone) {
3267 		dst->ops = src->ops;
3268 		err = src->ops->clone(dst, src);
3269 		if (err < 0)
3270 			return err;
3271 	} else {
3272 		memcpy(dst, src, src->ops->size);
3273 	}
3274 
3275 	__module_get(src->ops->type->owner);
3276 
3277 	return 0;
3278 }
3279 
nft_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)3280 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
3281 {
3282 	nf_tables_expr_destroy(ctx, expr);
3283 	kfree(expr);
3284 }
3285 
3286 /*
3287  * Rules
3288  */
3289 
__nft_rule_lookup(const struct nft_chain * chain,u64 handle)3290 static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
3291 					  u64 handle)
3292 {
3293 	struct nft_rule *rule;
3294 
3295 	// FIXME: this sucks
3296 	list_for_each_entry_rcu(rule, &chain->rules, list) {
3297 		if (handle == rule->handle)
3298 			return rule;
3299 	}
3300 
3301 	return ERR_PTR(-ENOENT);
3302 }
3303 
nft_rule_lookup(const struct nft_chain * chain,const struct nlattr * nla)3304 static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
3305 					const struct nlattr *nla)
3306 {
3307 	if (nla == NULL)
3308 		return ERR_PTR(-EINVAL);
3309 
3310 	return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
3311 }
3312 
3313 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
3314 	[NFTA_RULE_TABLE]	= { .type = NLA_STRING,
3315 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
3316 	[NFTA_RULE_CHAIN]	= { .type = NLA_STRING,
3317 				    .len = NFT_CHAIN_MAXNAMELEN - 1 },
3318 	[NFTA_RULE_HANDLE]	= { .type = NLA_U64 },
3319 	[NFTA_RULE_EXPRESSIONS]	= { .type = NLA_NESTED },
3320 	[NFTA_RULE_COMPAT]	= { .type = NLA_NESTED },
3321 	[NFTA_RULE_POSITION]	= { .type = NLA_U64 },
3322 	[NFTA_RULE_USERDATA]	= { .type = NLA_BINARY,
3323 				    .len = NFT_USERDATA_MAXLEN },
3324 	[NFTA_RULE_ID]		= { .type = NLA_U32 },
3325 	[NFTA_RULE_POSITION_ID]	= { .type = NLA_U32 },
3326 	[NFTA_RULE_CHAIN_ID]	= { .type = NLA_U32 },
3327 };
3328 
nf_tables_fill_rule_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table,const struct nft_chain * chain,const struct nft_rule * rule,u64 handle,bool reset)3329 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
3330 				    u32 portid, u32 seq, int event,
3331 				    u32 flags, int family,
3332 				    const struct nft_table *table,
3333 				    const struct nft_chain *chain,
3334 				    const struct nft_rule *rule, u64 handle,
3335 				    bool reset)
3336 {
3337 	struct nlmsghdr *nlh;
3338 	const struct nft_expr *expr, *next;
3339 	struct nlattr *list;
3340 	u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
3341 
3342 	nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0,
3343 			   nft_base_seq(net));
3344 	if (!nlh)
3345 		goto nla_put_failure;
3346 
3347 	if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
3348 		goto nla_put_failure;
3349 	if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
3350 		goto nla_put_failure;
3351 	if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
3352 			 NFTA_RULE_PAD))
3353 		goto nla_put_failure;
3354 
3355 	if (event != NFT_MSG_DELRULE && handle) {
3356 		if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle),
3357 				 NFTA_RULE_PAD))
3358 			goto nla_put_failure;
3359 	}
3360 
3361 	if (chain->flags & NFT_CHAIN_HW_OFFLOAD)
3362 		nft_flow_rule_stats(chain, rule);
3363 
3364 	list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
3365 	if (list == NULL)
3366 		goto nla_put_failure;
3367 	nft_rule_for_each_expr(expr, next, rule) {
3368 		if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
3369 			goto nla_put_failure;
3370 	}
3371 	nla_nest_end(skb, list);
3372 
3373 	if (rule->udata) {
3374 		struct nft_userdata *udata = nft_userdata(rule);
3375 		if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
3376 			    udata->data) < 0)
3377 			goto nla_put_failure;
3378 	}
3379 
3380 	nlmsg_end(skb, nlh);
3381 	return 0;
3382 
3383 nla_put_failure:
3384 	nlmsg_trim(skb, nlh);
3385 	return -1;
3386 }
3387 
nf_tables_rule_notify(const struct nft_ctx * ctx,const struct nft_rule * rule,int event)3388 static void nf_tables_rule_notify(const struct nft_ctx *ctx,
3389 				  const struct nft_rule *rule, int event)
3390 {
3391 	struct nftables_pernet *nft_net = nft_pernet(ctx->net);
3392 	const struct nft_rule *prule;
3393 	struct sk_buff *skb;
3394 	u64 handle = 0;
3395 	u16 flags = 0;
3396 	int err;
3397 
3398 	if (!ctx->report &&
3399 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
3400 		return;
3401 
3402 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3403 	if (skb == NULL)
3404 		goto err;
3405 
3406 	if (event == NFT_MSG_NEWRULE &&
3407 	    !list_is_first(&rule->list, &ctx->chain->rules) &&
3408 	    !list_is_last(&rule->list, &ctx->chain->rules)) {
3409 		prule = list_prev_entry(rule, list);
3410 		handle = prule->handle;
3411 	}
3412 	if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE))
3413 		flags |= NLM_F_APPEND;
3414 	if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
3415 		flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
3416 
3417 	err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
3418 				       event, flags, ctx->family, ctx->table,
3419 				       ctx->chain, rule, handle, false);
3420 	if (err < 0) {
3421 		kfree_skb(skb);
3422 		goto err;
3423 	}
3424 
3425 	nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
3426 	return;
3427 err:
3428 	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
3429 }
3430 
audit_log_rule_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)3431 static void audit_log_rule_reset(const struct nft_table *table,
3432 				 unsigned int base_seq,
3433 				 unsigned int nentries)
3434 {
3435 	char *buf = kasprintf(GFP_ATOMIC, "%s:%u",
3436 			      table->name, base_seq);
3437 
3438 	audit_log_nfcfg(buf, table->family, nentries,
3439 			AUDIT_NFT_OP_RULE_RESET, GFP_ATOMIC);
3440 	kfree(buf);
3441 }
3442 
3443 struct nft_rule_dump_ctx {
3444 	char *table;
3445 	char *chain;
3446 };
3447 
__nf_tables_dump_rules(struct sk_buff * skb,unsigned int * idx,struct netlink_callback * cb,const struct nft_table * table,const struct nft_chain * chain,bool reset)3448 static int __nf_tables_dump_rules(struct sk_buff *skb,
3449 				  unsigned int *idx,
3450 				  struct netlink_callback *cb,
3451 				  const struct nft_table *table,
3452 				  const struct nft_chain *chain,
3453 				  bool reset)
3454 {
3455 	struct net *net = sock_net(skb->sk);
3456 	const struct nft_rule *rule, *prule;
3457 	unsigned int s_idx = cb->args[0];
3458 	unsigned int entries = 0;
3459 	int ret = 0;
3460 	u64 handle;
3461 
3462 	prule = NULL;
3463 	list_for_each_entry_rcu(rule, &chain->rules, list) {
3464 		if (!nft_is_active(net, rule))
3465 			goto cont_skip;
3466 		if (*idx < s_idx)
3467 			goto cont;
3468 		if (*idx > s_idx) {
3469 			memset(&cb->args[1], 0,
3470 					sizeof(cb->args) - sizeof(cb->args[0]));
3471 		}
3472 		if (prule)
3473 			handle = prule->handle;
3474 		else
3475 			handle = 0;
3476 
3477 		if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
3478 					cb->nlh->nlmsg_seq,
3479 					NFT_MSG_NEWRULE,
3480 					NLM_F_MULTI | NLM_F_APPEND,
3481 					table->family,
3482 					table, chain, rule, handle, reset) < 0) {
3483 			ret = 1;
3484 			break;
3485 		}
3486 		entries++;
3487 		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3488 cont:
3489 		prule = rule;
3490 cont_skip:
3491 		(*idx)++;
3492 	}
3493 
3494 	if (reset && entries)
3495 		audit_log_rule_reset(table, cb->seq, entries);
3496 
3497 	return ret;
3498 }
3499 
nf_tables_dump_rules(struct sk_buff * skb,struct netlink_callback * cb)3500 static int nf_tables_dump_rules(struct sk_buff *skb,
3501 				struct netlink_callback *cb)
3502 {
3503 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3504 	const struct nft_rule_dump_ctx *ctx = cb->data;
3505 	struct nft_table *table;
3506 	const struct nft_chain *chain;
3507 	unsigned int idx = 0;
3508 	struct net *net = sock_net(skb->sk);
3509 	int family = nfmsg->nfgen_family;
3510 	struct nftables_pernet *nft_net;
3511 	bool reset = false;
3512 
3513 	if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET)
3514 		reset = true;
3515 
3516 	rcu_read_lock();
3517 	nft_net = nft_pernet(net);
3518 	cb->seq = READ_ONCE(nft_net->base_seq);
3519 
3520 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
3521 		if (family != NFPROTO_UNSPEC && family != table->family)
3522 			continue;
3523 
3524 		if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
3525 			continue;
3526 
3527 		if (ctx && ctx->table && ctx->chain) {
3528 			struct rhlist_head *list, *tmp;
3529 
3530 			list = rhltable_lookup(&table->chains_ht, ctx->chain,
3531 					       nft_chain_ht_params);
3532 			if (!list)
3533 				goto done;
3534 
3535 			rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
3536 				if (!nft_is_active(net, chain))
3537 					continue;
3538 				__nf_tables_dump_rules(skb, &idx,
3539 						       cb, table, chain, reset);
3540 				break;
3541 			}
3542 			goto done;
3543 		}
3544 
3545 		list_for_each_entry_rcu(chain, &table->chains, list) {
3546 			if (__nf_tables_dump_rules(skb, &idx,
3547 						   cb, table, chain, reset))
3548 				goto done;
3549 		}
3550 
3551 		if (ctx && ctx->table)
3552 			break;
3553 	}
3554 done:
3555 	rcu_read_unlock();
3556 
3557 	cb->args[0] = idx;
3558 	return skb->len;
3559 }
3560 
nf_tables_dump_rules_start(struct netlink_callback * cb)3561 static int nf_tables_dump_rules_start(struct netlink_callback *cb)
3562 {
3563 	const struct nlattr * const *nla = cb->data;
3564 	struct nft_rule_dump_ctx *ctx = NULL;
3565 
3566 	if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
3567 		ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
3568 		if (!ctx)
3569 			return -ENOMEM;
3570 
3571 		if (nla[NFTA_RULE_TABLE]) {
3572 			ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
3573 							GFP_ATOMIC);
3574 			if (!ctx->table) {
3575 				kfree(ctx);
3576 				return -ENOMEM;
3577 			}
3578 		}
3579 		if (nla[NFTA_RULE_CHAIN]) {
3580 			ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
3581 						GFP_ATOMIC);
3582 			if (!ctx->chain) {
3583 				kfree(ctx->table);
3584 				kfree(ctx);
3585 				return -ENOMEM;
3586 			}
3587 		}
3588 	}
3589 
3590 	cb->data = ctx;
3591 	return 0;
3592 }
3593 
nf_tables_dump_rules_done(struct netlink_callback * cb)3594 static int nf_tables_dump_rules_done(struct netlink_callback *cb)
3595 {
3596 	struct nft_rule_dump_ctx *ctx = cb->data;
3597 
3598 	if (ctx) {
3599 		kfree(ctx->table);
3600 		kfree(ctx->chain);
3601 		kfree(ctx);
3602 	}
3603 	return 0;
3604 }
3605 
3606 /* called with rcu_read_lock held */
nf_tables_getrule(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])3607 static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,
3608 			     const struct nlattr * const nla[])
3609 {
3610 	struct netlink_ext_ack *extack = info->extack;
3611 	u8 genmask = nft_genmask_cur(info->net);
3612 	u8 family = info->nfmsg->nfgen_family;
3613 	const struct nft_chain *chain;
3614 	const struct nft_rule *rule;
3615 	struct net *net = info->net;
3616 	struct nft_table *table;
3617 	struct sk_buff *skb2;
3618 	bool reset = false;
3619 	int err;
3620 
3621 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3622 		struct netlink_dump_control c = {
3623 			.start= nf_tables_dump_rules_start,
3624 			.dump = nf_tables_dump_rules,
3625 			.done = nf_tables_dump_rules_done,
3626 			.module = THIS_MODULE,
3627 			.data = (void *)nla,
3628 		};
3629 
3630 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
3631 	}
3632 
3633 	table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0);
3634 	if (IS_ERR(table)) {
3635 		NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3636 		return PTR_ERR(table);
3637 	}
3638 
3639 	chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
3640 	if (IS_ERR(chain)) {
3641 		NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3642 		return PTR_ERR(chain);
3643 	}
3644 
3645 	rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
3646 	if (IS_ERR(rule)) {
3647 		NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3648 		return PTR_ERR(rule);
3649 	}
3650 
3651 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3652 	if (!skb2)
3653 		return -ENOMEM;
3654 
3655 	if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET)
3656 		reset = true;
3657 
3658 	err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
3659 				       info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
3660 				       family, table, chain, rule, 0, reset);
3661 	if (err < 0)
3662 		goto err_fill_rule_info;
3663 
3664 	if (reset)
3665 		audit_log_rule_reset(table, nft_pernet(net)->base_seq, 1);
3666 
3667 	return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
3668 
3669 err_fill_rule_info:
3670 	kfree_skb(skb2);
3671 	return err;
3672 }
3673 
nf_tables_rule_destroy(const struct nft_ctx * ctx,struct nft_rule * rule)3674 void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule)
3675 {
3676 	struct nft_expr *expr, *next;
3677 
3678 	/*
3679 	 * Careful: some expressions might not be initialized in case this
3680 	 * is called on error from nf_tables_newrule().
3681 	 */
3682 	expr = nft_expr_first(rule);
3683 	while (nft_expr_more(rule, expr)) {
3684 		next = nft_expr_next(expr);
3685 		nf_tables_expr_destroy(ctx, expr);
3686 		expr = next;
3687 	}
3688 	kfree(rule);
3689 }
3690 
nf_tables_rule_release(const struct nft_ctx * ctx,struct nft_rule * rule)3691 static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
3692 {
3693 	nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
3694 	nf_tables_rule_destroy(ctx, rule);
3695 }
3696 
nft_chain_validate(const struct nft_ctx * ctx,const struct nft_chain * chain)3697 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
3698 {
3699 	struct nft_expr *expr, *last;
3700 	const struct nft_data *data;
3701 	struct nft_rule *rule;
3702 	int err;
3703 
3704 	if (ctx->level == NFT_JUMP_STACK_SIZE)
3705 		return -EMLINK;
3706 
3707 	list_for_each_entry(rule, &chain->rules, list) {
3708 		if (fatal_signal_pending(current))
3709 			return -EINTR;
3710 
3711 		if (!nft_is_active_next(ctx->net, rule))
3712 			continue;
3713 
3714 		nft_rule_for_each_expr(expr, last, rule) {
3715 			if (!expr->ops->validate)
3716 				continue;
3717 
3718 			err = expr->ops->validate(ctx, expr, &data);
3719 			if (err < 0)
3720 				return err;
3721 		}
3722 	}
3723 
3724 	return 0;
3725 }
3726 EXPORT_SYMBOL_GPL(nft_chain_validate);
3727 
nft_table_validate(struct net * net,const struct nft_table * table)3728 static int nft_table_validate(struct net *net, const struct nft_table *table)
3729 {
3730 	struct nft_chain *chain;
3731 	struct nft_ctx ctx = {
3732 		.net	= net,
3733 		.family	= table->family,
3734 	};
3735 	int err;
3736 
3737 	list_for_each_entry(chain, &table->chains, list) {
3738 		if (!nft_is_base_chain(chain))
3739 			continue;
3740 
3741 		ctx.chain = chain;
3742 		err = nft_chain_validate(&ctx, chain);
3743 		if (err < 0)
3744 			return err;
3745 
3746 		cond_resched();
3747 	}
3748 
3749 	return 0;
3750 }
3751 
nft_setelem_validate(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)3752 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
3753 			 const struct nft_set_iter *iter,
3754 			 struct nft_set_elem *elem)
3755 {
3756 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3757 	struct nft_ctx *pctx = (struct nft_ctx *)ctx;
3758 	const struct nft_data *data;
3759 	int err;
3760 
3761 	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3762 	    *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
3763 		return 0;
3764 
3765 	data = nft_set_ext_data(ext);
3766 	switch (data->verdict.code) {
3767 	case NFT_JUMP:
3768 	case NFT_GOTO:
3769 		pctx->level++;
3770 		err = nft_chain_validate(ctx, data->verdict.chain);
3771 		if (err < 0)
3772 			return err;
3773 		pctx->level--;
3774 		break;
3775 	default:
3776 		break;
3777 	}
3778 
3779 	return 0;
3780 }
3781 
nft_set_catchall_validate(const struct nft_ctx * ctx,struct nft_set * set)3782 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set)
3783 {
3784 	u8 genmask = nft_genmask_next(ctx->net);
3785 	struct nft_set_elem_catchall *catchall;
3786 	struct nft_set_elem elem;
3787 	struct nft_set_ext *ext;
3788 	int ret = 0;
3789 
3790 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
3791 		ext = nft_set_elem_ext(set, catchall->elem);
3792 		if (!nft_set_elem_active(ext, genmask))
3793 			continue;
3794 
3795 		elem.priv = catchall->elem;
3796 		ret = nft_setelem_validate(ctx, set, NULL, &elem);
3797 		if (ret < 0)
3798 			return ret;
3799 	}
3800 
3801 	return ret;
3802 }
3803 
3804 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3805 					     const struct nft_chain *chain,
3806 					     const struct nlattr *nla);
3807 
3808 #define NFT_RULE_MAXEXPRS	128
3809 
nf_tables_newrule(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])3810 static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,
3811 			     const struct nlattr * const nla[])
3812 {
3813 	struct nftables_pernet *nft_net = nft_pernet(info->net);
3814 	struct netlink_ext_ack *extack = info->extack;
3815 	unsigned int size, i, n, ulen = 0, usize = 0;
3816 	u8 genmask = nft_genmask_next(info->net);
3817 	struct nft_rule *rule, *old_rule = NULL;
3818 	struct nft_expr_info *expr_info = NULL;
3819 	u8 family = info->nfmsg->nfgen_family;
3820 	struct nft_flow_rule *flow = NULL;
3821 	struct net *net = info->net;
3822 	struct nft_userdata *udata;
3823 	struct nft_table *table;
3824 	struct nft_chain *chain;
3825 	struct nft_trans *trans;
3826 	u64 handle, pos_handle;
3827 	struct nft_expr *expr;
3828 	struct nft_ctx ctx;
3829 	struct nlattr *tmp;
3830 	int err, rem;
3831 
3832 	lockdep_assert_held(&nft_net->commit_mutex);
3833 
3834 	table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
3835 				 NETLINK_CB(skb).portid);
3836 	if (IS_ERR(table)) {
3837 		NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3838 		return PTR_ERR(table);
3839 	}
3840 
3841 	if (nla[NFTA_RULE_CHAIN]) {
3842 		chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3843 					 genmask);
3844 		if (IS_ERR(chain)) {
3845 			NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3846 			return PTR_ERR(chain);
3847 		}
3848 
3849 	} else if (nla[NFTA_RULE_CHAIN_ID]) {
3850 		chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID],
3851 					      genmask);
3852 		if (IS_ERR(chain)) {
3853 			NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]);
3854 			return PTR_ERR(chain);
3855 		}
3856 	} else {
3857 		return -EINVAL;
3858 	}
3859 
3860 	if (nft_chain_is_bound(chain))
3861 		return -EOPNOTSUPP;
3862 
3863 	if (nla[NFTA_RULE_HANDLE]) {
3864 		handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
3865 		rule = __nft_rule_lookup(chain, handle);
3866 		if (IS_ERR(rule)) {
3867 			NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3868 			return PTR_ERR(rule);
3869 		}
3870 
3871 		if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
3872 			NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3873 			return -EEXIST;
3874 		}
3875 		if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
3876 			old_rule = rule;
3877 		else
3878 			return -EOPNOTSUPP;
3879 	} else {
3880 		if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) ||
3881 		    info->nlh->nlmsg_flags & NLM_F_REPLACE)
3882 			return -EINVAL;
3883 		handle = nf_tables_alloc_handle(table);
3884 
3885 		if (nla[NFTA_RULE_POSITION]) {
3886 			pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
3887 			old_rule = __nft_rule_lookup(chain, pos_handle);
3888 			if (IS_ERR(old_rule)) {
3889 				NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
3890 				return PTR_ERR(old_rule);
3891 			}
3892 		} else if (nla[NFTA_RULE_POSITION_ID]) {
3893 			old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]);
3894 			if (IS_ERR(old_rule)) {
3895 				NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
3896 				return PTR_ERR(old_rule);
3897 			}
3898 		}
3899 	}
3900 
3901 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
3902 
3903 	n = 0;
3904 	size = 0;
3905 	if (nla[NFTA_RULE_EXPRESSIONS]) {
3906 		expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS,
3907 					   sizeof(struct nft_expr_info),
3908 					   GFP_KERNEL);
3909 		if (!expr_info)
3910 			return -ENOMEM;
3911 
3912 		nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
3913 			err = -EINVAL;
3914 			if (nla_type(tmp) != NFTA_LIST_ELEM)
3915 				goto err_release_expr;
3916 			if (n == NFT_RULE_MAXEXPRS)
3917 				goto err_release_expr;
3918 			err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]);
3919 			if (err < 0) {
3920 				NL_SET_BAD_ATTR(extack, tmp);
3921 				goto err_release_expr;
3922 			}
3923 			size += expr_info[n].ops->size;
3924 			n++;
3925 		}
3926 	}
3927 	/* Check for overflow of dlen field */
3928 	err = -EFBIG;
3929 	if (size >= 1 << 12)
3930 		goto err_release_expr;
3931 
3932 	if (nla[NFTA_RULE_USERDATA]) {
3933 		ulen = nla_len(nla[NFTA_RULE_USERDATA]);
3934 		if (ulen > 0)
3935 			usize = sizeof(struct nft_userdata) + ulen;
3936 	}
3937 
3938 	err = -ENOMEM;
3939 	rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT);
3940 	if (rule == NULL)
3941 		goto err_release_expr;
3942 
3943 	nft_activate_next(net, rule);
3944 
3945 	rule->handle = handle;
3946 	rule->dlen   = size;
3947 	rule->udata  = ulen ? 1 : 0;
3948 
3949 	if (ulen) {
3950 		udata = nft_userdata(rule);
3951 		udata->len = ulen - 1;
3952 		nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
3953 	}
3954 
3955 	expr = nft_expr_first(rule);
3956 	for (i = 0; i < n; i++) {
3957 		err = nf_tables_newexpr(&ctx, &expr_info[i], expr);
3958 		if (err < 0) {
3959 			NL_SET_BAD_ATTR(extack, expr_info[i].attr);
3960 			goto err_release_rule;
3961 		}
3962 
3963 		if (expr_info[i].ops->validate)
3964 			nft_validate_state_update(table, NFT_VALIDATE_NEED);
3965 
3966 		expr_info[i].ops = NULL;
3967 		expr = nft_expr_next(expr);
3968 	}
3969 
3970 	if (chain->flags & NFT_CHAIN_HW_OFFLOAD) {
3971 		flow = nft_flow_rule_create(net, rule);
3972 		if (IS_ERR(flow)) {
3973 			err = PTR_ERR(flow);
3974 			goto err_release_rule;
3975 		}
3976 	}
3977 
3978 	if (!nft_use_inc(&chain->use)) {
3979 		err = -EMFILE;
3980 		goto err_release_rule;
3981 	}
3982 
3983 	if (info->nlh->nlmsg_flags & NLM_F_REPLACE) {
3984 		if (nft_chain_binding(chain)) {
3985 			err = -EOPNOTSUPP;
3986 			goto err_destroy_flow_rule;
3987 		}
3988 
3989 		err = nft_delrule(&ctx, old_rule);
3990 		if (err < 0)
3991 			goto err_destroy_flow_rule;
3992 
3993 		trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
3994 		if (trans == NULL) {
3995 			err = -ENOMEM;
3996 			goto err_destroy_flow_rule;
3997 		}
3998 		list_add_tail_rcu(&rule->list, &old_rule->list);
3999 	} else {
4000 		trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
4001 		if (!trans) {
4002 			err = -ENOMEM;
4003 			goto err_destroy_flow_rule;
4004 		}
4005 
4006 		if (info->nlh->nlmsg_flags & NLM_F_APPEND) {
4007 			if (old_rule)
4008 				list_add_rcu(&rule->list, &old_rule->list);
4009 			else
4010 				list_add_tail_rcu(&rule->list, &chain->rules);
4011 		 } else {
4012 			if (old_rule)
4013 				list_add_tail_rcu(&rule->list, &old_rule->list);
4014 			else
4015 				list_add_rcu(&rule->list, &chain->rules);
4016 		}
4017 	}
4018 	kvfree(expr_info);
4019 
4020 	if (flow)
4021 		nft_trans_flow_rule(trans) = flow;
4022 
4023 	if (table->validate_state == NFT_VALIDATE_DO)
4024 		return nft_table_validate(net, table);
4025 
4026 	return 0;
4027 
4028 err_destroy_flow_rule:
4029 	nft_use_dec_restore(&chain->use);
4030 	if (flow)
4031 		nft_flow_rule_destroy(flow);
4032 err_release_rule:
4033 	nft_rule_expr_deactivate(&ctx, rule, NFT_TRANS_PREPARE_ERROR);
4034 	nf_tables_rule_destroy(&ctx, rule);
4035 err_release_expr:
4036 	for (i = 0; i < n; i++) {
4037 		if (expr_info[i].ops) {
4038 			module_put(expr_info[i].ops->type->owner);
4039 			if (expr_info[i].ops->type->release_ops)
4040 				expr_info[i].ops->type->release_ops(expr_info[i].ops);
4041 		}
4042 	}
4043 	kvfree(expr_info);
4044 
4045 	return err;
4046 }
4047 
nft_rule_lookup_byid(const struct net * net,const struct nft_chain * chain,const struct nlattr * nla)4048 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
4049 					     const struct nft_chain *chain,
4050 					     const struct nlattr *nla)
4051 {
4052 	struct nftables_pernet *nft_net = nft_pernet(net);
4053 	u32 id = ntohl(nla_get_be32(nla));
4054 	struct nft_trans *trans;
4055 
4056 	list_for_each_entry(trans, &nft_net->commit_list, list) {
4057 		if (trans->msg_type == NFT_MSG_NEWRULE &&
4058 		    trans->ctx.chain == chain &&
4059 		    id == nft_trans_rule_id(trans))
4060 			return nft_trans_rule(trans);
4061 	}
4062 	return ERR_PTR(-ENOENT);
4063 }
4064 
nf_tables_delrule(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])4065 static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info,
4066 			     const struct nlattr * const nla[])
4067 {
4068 	struct netlink_ext_ack *extack = info->extack;
4069 	u8 genmask = nft_genmask_next(info->net);
4070 	u8 family = info->nfmsg->nfgen_family;
4071 	struct nft_chain *chain = NULL;
4072 	struct net *net = info->net;
4073 	struct nft_table *table;
4074 	struct nft_rule *rule;
4075 	struct nft_ctx ctx;
4076 	int err = 0;
4077 
4078 	table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
4079 				 NETLINK_CB(skb).portid);
4080 	if (IS_ERR(table)) {
4081 		NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
4082 		return PTR_ERR(table);
4083 	}
4084 
4085 	if (nla[NFTA_RULE_CHAIN]) {
4086 		chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
4087 					 genmask);
4088 		if (IS_ERR(chain)) {
4089 			if (PTR_ERR(chain) == -ENOENT &&
4090 			    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
4091 				return 0;
4092 
4093 			NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
4094 			return PTR_ERR(chain);
4095 		}
4096 		if (nft_chain_binding(chain))
4097 			return -EOPNOTSUPP;
4098 	}
4099 
4100 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
4101 
4102 	if (chain) {
4103 		if (nla[NFTA_RULE_HANDLE]) {
4104 			rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
4105 			if (IS_ERR(rule)) {
4106 				if (PTR_ERR(rule) == -ENOENT &&
4107 				    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
4108 					return 0;
4109 
4110 				NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
4111 				return PTR_ERR(rule);
4112 			}
4113 
4114 			err = nft_delrule(&ctx, rule);
4115 		} else if (nla[NFTA_RULE_ID]) {
4116 			rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]);
4117 			if (IS_ERR(rule)) {
4118 				NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
4119 				return PTR_ERR(rule);
4120 			}
4121 
4122 			err = nft_delrule(&ctx, rule);
4123 		} else {
4124 			err = nft_delrule_by_chain(&ctx);
4125 		}
4126 	} else {
4127 		list_for_each_entry(chain, &table->chains, list) {
4128 			if (!nft_is_active_next(net, chain))
4129 				continue;
4130 			if (nft_chain_binding(chain))
4131 				continue;
4132 
4133 			ctx.chain = chain;
4134 			err = nft_delrule_by_chain(&ctx);
4135 			if (err < 0)
4136 				break;
4137 		}
4138 	}
4139 
4140 	return err;
4141 }
4142 
4143 /*
4144  * Sets
4145  */
4146 static const struct nft_set_type *nft_set_types[] = {
4147 	&nft_set_hash_fast_type,
4148 	&nft_set_hash_type,
4149 	&nft_set_rhash_type,
4150 	&nft_set_bitmap_type,
4151 	&nft_set_rbtree_type,
4152 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
4153 	&nft_set_pipapo_avx2_type,
4154 #endif
4155 	&nft_set_pipapo_type,
4156 };
4157 
4158 #define NFT_SET_FEATURES	(NFT_SET_INTERVAL | NFT_SET_MAP | \
4159 				 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
4160 				 NFT_SET_EVAL)
4161 
nft_set_ops_candidate(const struct nft_set_type * type,u32 flags)4162 static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
4163 {
4164 	return (flags & type->features) == (flags & NFT_SET_FEATURES);
4165 }
4166 
4167 /*
4168  * Select a set implementation based on the data characteristics and the
4169  * given policy. The total memory use might not be known if no size is
4170  * given, in that case the amount of memory per element is used.
4171  */
4172 static const struct nft_set_ops *
nft_select_set_ops(const struct nft_ctx * ctx,const struct nlattr * const nla[],const struct nft_set_desc * desc)4173 nft_select_set_ops(const struct nft_ctx *ctx,
4174 		   const struct nlattr * const nla[],
4175 		   const struct nft_set_desc *desc)
4176 {
4177 	struct nftables_pernet *nft_net = nft_pernet(ctx->net);
4178 	const struct nft_set_ops *ops, *bops;
4179 	struct nft_set_estimate est, best;
4180 	const struct nft_set_type *type;
4181 	u32 flags = 0;
4182 	int i;
4183 
4184 	lockdep_assert_held(&nft_net->commit_mutex);
4185 	lockdep_nfnl_nft_mutex_not_held();
4186 
4187 	if (nla[NFTA_SET_FLAGS] != NULL)
4188 		flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4189 
4190 	bops	    = NULL;
4191 	best.size   = ~0;
4192 	best.lookup = ~0;
4193 	best.space  = ~0;
4194 
4195 	for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) {
4196 		type = nft_set_types[i];
4197 		ops = &type->ops;
4198 
4199 		if (!nft_set_ops_candidate(type, flags))
4200 			continue;
4201 		if (!ops->estimate(desc, flags, &est))
4202 			continue;
4203 
4204 		switch (desc->policy) {
4205 		case NFT_SET_POL_PERFORMANCE:
4206 			if (est.lookup < best.lookup)
4207 				break;
4208 			if (est.lookup == best.lookup &&
4209 			    est.space < best.space)
4210 				break;
4211 			continue;
4212 		case NFT_SET_POL_MEMORY:
4213 			if (!desc->size) {
4214 				if (est.space < best.space)
4215 					break;
4216 				if (est.space == best.space &&
4217 				    est.lookup < best.lookup)
4218 					break;
4219 			} else if (est.size < best.size || !bops) {
4220 				break;
4221 			}
4222 			continue;
4223 		default:
4224 			break;
4225 		}
4226 
4227 		bops = ops;
4228 		best = est;
4229 	}
4230 
4231 	if (bops != NULL)
4232 		return bops;
4233 
4234 	return ERR_PTR(-EOPNOTSUPP);
4235 }
4236 
4237 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
4238 	[NFTA_SET_TABLE]		= { .type = NLA_STRING,
4239 					    .len = NFT_TABLE_MAXNAMELEN - 1 },
4240 	[NFTA_SET_NAME]			= { .type = NLA_STRING,
4241 					    .len = NFT_SET_MAXNAMELEN - 1 },
4242 	[NFTA_SET_FLAGS]		= { .type = NLA_U32 },
4243 	[NFTA_SET_KEY_TYPE]		= { .type = NLA_U32 },
4244 	[NFTA_SET_KEY_LEN]		= { .type = NLA_U32 },
4245 	[NFTA_SET_DATA_TYPE]		= { .type = NLA_U32 },
4246 	[NFTA_SET_DATA_LEN]		= { .type = NLA_U32 },
4247 	[NFTA_SET_POLICY]		= { .type = NLA_U32 },
4248 	[NFTA_SET_DESC]			= { .type = NLA_NESTED },
4249 	[NFTA_SET_ID]			= { .type = NLA_U32 },
4250 	[NFTA_SET_TIMEOUT]		= { .type = NLA_U64 },
4251 	[NFTA_SET_GC_INTERVAL]		= { .type = NLA_U32 },
4252 	[NFTA_SET_USERDATA]		= { .type = NLA_BINARY,
4253 					    .len  = NFT_USERDATA_MAXLEN },
4254 	[NFTA_SET_OBJ_TYPE]		= { .type = NLA_U32 },
4255 	[NFTA_SET_HANDLE]		= { .type = NLA_U64 },
4256 	[NFTA_SET_EXPR]			= { .type = NLA_NESTED },
4257 	[NFTA_SET_EXPRESSIONS]		= { .type = NLA_NESTED },
4258 };
4259 
4260 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
4261 	[NFTA_SET_DESC_SIZE]		= { .type = NLA_U32 },
4262 	[NFTA_SET_DESC_CONCAT]		= { .type = NLA_NESTED },
4263 };
4264 
nft_set_lookup(const struct nft_table * table,const struct nlattr * nla,u8 genmask)4265 static struct nft_set *nft_set_lookup(const struct nft_table *table,
4266 				      const struct nlattr *nla, u8 genmask)
4267 {
4268 	struct nft_set *set;
4269 
4270 	if (nla == NULL)
4271 		return ERR_PTR(-EINVAL);
4272 
4273 	list_for_each_entry_rcu(set, &table->sets, list) {
4274 		if (!nla_strcmp(nla, set->name) &&
4275 		    nft_active_genmask(set, genmask))
4276 			return set;
4277 	}
4278 	return ERR_PTR(-ENOENT);
4279 }
4280 
nft_set_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u8 genmask)4281 static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
4282 					       const struct nlattr *nla,
4283 					       u8 genmask)
4284 {
4285 	struct nft_set *set;
4286 
4287 	list_for_each_entry(set, &table->sets, list) {
4288 		if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
4289 		    nft_active_genmask(set, genmask))
4290 			return set;
4291 	}
4292 	return ERR_PTR(-ENOENT);
4293 }
4294 
nft_set_lookup_byid(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u8 genmask)4295 static struct nft_set *nft_set_lookup_byid(const struct net *net,
4296 					   const struct nft_table *table,
4297 					   const struct nlattr *nla, u8 genmask)
4298 {
4299 	struct nftables_pernet *nft_net = nft_pernet(net);
4300 	u32 id = ntohl(nla_get_be32(nla));
4301 	struct nft_trans *trans;
4302 
4303 	list_for_each_entry(trans, &nft_net->commit_list, list) {
4304 		if (trans->msg_type == NFT_MSG_NEWSET) {
4305 			struct nft_set *set = nft_trans_set(trans);
4306 
4307 			if (id == nft_trans_set_id(trans) &&
4308 			    set->table == table &&
4309 			    nft_active_genmask(set, genmask))
4310 				return set;
4311 		}
4312 	}
4313 	return ERR_PTR(-ENOENT);
4314 }
4315 
nft_set_lookup_global(const struct net * net,const struct nft_table * table,const struct nlattr * nla_set_name,const struct nlattr * nla_set_id,u8 genmask)4316 struct nft_set *nft_set_lookup_global(const struct net *net,
4317 				      const struct nft_table *table,
4318 				      const struct nlattr *nla_set_name,
4319 				      const struct nlattr *nla_set_id,
4320 				      u8 genmask)
4321 {
4322 	struct nft_set *set;
4323 
4324 	set = nft_set_lookup(table, nla_set_name, genmask);
4325 	if (IS_ERR(set)) {
4326 		if (!nla_set_id)
4327 			return set;
4328 
4329 		set = nft_set_lookup_byid(net, table, nla_set_id, genmask);
4330 	}
4331 	return set;
4332 }
4333 EXPORT_SYMBOL_GPL(nft_set_lookup_global);
4334 
nf_tables_set_alloc_name(struct nft_ctx * ctx,struct nft_set * set,const char * name)4335 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
4336 				    const char *name)
4337 {
4338 	const struct nft_set *i;
4339 	const char *p;
4340 	unsigned long *inuse;
4341 	unsigned int n = 0, min = 0;
4342 
4343 	p = strchr(name, '%');
4344 	if (p != NULL) {
4345 		if (p[1] != 'd' || strchr(p + 2, '%'))
4346 			return -EINVAL;
4347 
4348 		inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
4349 		if (inuse == NULL)
4350 			return -ENOMEM;
4351 cont:
4352 		list_for_each_entry(i, &ctx->table->sets, list) {
4353 			int tmp;
4354 
4355 			if (!nft_is_active_next(ctx->net, i))
4356 				continue;
4357 			if (!sscanf(i->name, name, &tmp))
4358 				continue;
4359 			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
4360 				continue;
4361 
4362 			set_bit(tmp - min, inuse);
4363 		}
4364 
4365 		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
4366 		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
4367 			min += BITS_PER_BYTE * PAGE_SIZE;
4368 			memset(inuse, 0, PAGE_SIZE);
4369 			goto cont;
4370 		}
4371 		free_page((unsigned long)inuse);
4372 	}
4373 
4374 	set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n);
4375 	if (!set->name)
4376 		return -ENOMEM;
4377 
4378 	list_for_each_entry(i, &ctx->table->sets, list) {
4379 		if (!nft_is_active_next(ctx->net, i))
4380 			continue;
4381 		if (!strcmp(set->name, i->name)) {
4382 			kfree(set->name);
4383 			set->name = NULL;
4384 			return -ENFILE;
4385 		}
4386 	}
4387 	return 0;
4388 }
4389 
nf_msecs_to_jiffies64(const struct nlattr * nla,u64 * result)4390 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
4391 {
4392 	u64 ms = be64_to_cpu(nla_get_be64(nla));
4393 	u64 max = (u64)(~((u64)0));
4394 
4395 	max = div_u64(max, NSEC_PER_MSEC);
4396 	if (ms >= max)
4397 		return -ERANGE;
4398 
4399 	ms *= NSEC_PER_MSEC;
4400 	*result = nsecs_to_jiffies64(ms);
4401 	return 0;
4402 }
4403 
nf_jiffies64_to_msecs(u64 input)4404 __be64 nf_jiffies64_to_msecs(u64 input)
4405 {
4406 	return cpu_to_be64(jiffies64_to_msecs(input));
4407 }
4408 
nf_tables_fill_set_concat(struct sk_buff * skb,const struct nft_set * set)4409 static int nf_tables_fill_set_concat(struct sk_buff *skb,
4410 				     const struct nft_set *set)
4411 {
4412 	struct nlattr *concat, *field;
4413 	int i;
4414 
4415 	concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT);
4416 	if (!concat)
4417 		return -ENOMEM;
4418 
4419 	for (i = 0; i < set->field_count; i++) {
4420 		field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
4421 		if (!field)
4422 			return -ENOMEM;
4423 
4424 		if (nla_put_be32(skb, NFTA_SET_FIELD_LEN,
4425 				 htonl(set->field_len[i])))
4426 			return -ENOMEM;
4427 
4428 		nla_nest_end(skb, field);
4429 	}
4430 
4431 	nla_nest_end(skb, concat);
4432 
4433 	return 0;
4434 }
4435 
nf_tables_fill_set(struct sk_buff * skb,const struct nft_ctx * ctx,const struct nft_set * set,u16 event,u16 flags)4436 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
4437 			      const struct nft_set *set, u16 event, u16 flags)
4438 {
4439 	u64 timeout = READ_ONCE(set->timeout);
4440 	u32 gc_int = READ_ONCE(set->gc_int);
4441 	u32 portid = ctx->portid;
4442 	struct nlmsghdr *nlh;
4443 	struct nlattr *nest;
4444 	u32 seq = ctx->seq;
4445 	int i;
4446 
4447 	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
4448 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
4449 			   NFNETLINK_V0, nft_base_seq(ctx->net));
4450 	if (!nlh)
4451 		goto nla_put_failure;
4452 
4453 	if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4454 		goto nla_put_failure;
4455 	if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4456 		goto nla_put_failure;
4457 	if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
4458 			 NFTA_SET_PAD))
4459 		goto nla_put_failure;
4460 
4461 	if (event == NFT_MSG_DELSET) {
4462 		nlmsg_end(skb, nlh);
4463 		return 0;
4464 	}
4465 
4466 	if (set->flags != 0)
4467 		if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
4468 			goto nla_put_failure;
4469 
4470 	if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
4471 		goto nla_put_failure;
4472 	if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
4473 		goto nla_put_failure;
4474 	if (set->flags & NFT_SET_MAP) {
4475 		if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
4476 			goto nla_put_failure;
4477 		if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
4478 			goto nla_put_failure;
4479 	}
4480 	if (set->flags & NFT_SET_OBJECT &&
4481 	    nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
4482 		goto nla_put_failure;
4483 
4484 	if (timeout &&
4485 	    nla_put_be64(skb, NFTA_SET_TIMEOUT,
4486 			 nf_jiffies64_to_msecs(timeout),
4487 			 NFTA_SET_PAD))
4488 		goto nla_put_failure;
4489 	if (gc_int &&
4490 	    nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int)))
4491 		goto nla_put_failure;
4492 
4493 	if (set->policy != NFT_SET_POL_PERFORMANCE) {
4494 		if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
4495 			goto nla_put_failure;
4496 	}
4497 
4498 	if (set->udata &&
4499 	    nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
4500 		goto nla_put_failure;
4501 
4502 	nest = nla_nest_start_noflag(skb, NFTA_SET_DESC);
4503 	if (!nest)
4504 		goto nla_put_failure;
4505 	if (set->size &&
4506 	    nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
4507 		goto nla_put_failure;
4508 
4509 	if (set->field_count > 1 &&
4510 	    nf_tables_fill_set_concat(skb, set))
4511 		goto nla_put_failure;
4512 
4513 	nla_nest_end(skb, nest);
4514 
4515 	if (set->num_exprs == 1) {
4516 		nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR);
4517 		if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0)
4518 			goto nla_put_failure;
4519 
4520 		nla_nest_end(skb, nest);
4521 	} else if (set->num_exprs > 1) {
4522 		nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS);
4523 		if (nest == NULL)
4524 			goto nla_put_failure;
4525 
4526 		for (i = 0; i < set->num_exprs; i++) {
4527 			if (nft_expr_dump(skb, NFTA_LIST_ELEM,
4528 					  set->exprs[i], false) < 0)
4529 				goto nla_put_failure;
4530 		}
4531 		nla_nest_end(skb, nest);
4532 	}
4533 
4534 	nlmsg_end(skb, nlh);
4535 	return 0;
4536 
4537 nla_put_failure:
4538 	nlmsg_trim(skb, nlh);
4539 	return -1;
4540 }
4541 
nf_tables_set_notify(const struct nft_ctx * ctx,const struct nft_set * set,int event,gfp_t gfp_flags)4542 static void nf_tables_set_notify(const struct nft_ctx *ctx,
4543 				 const struct nft_set *set, int event,
4544 			         gfp_t gfp_flags)
4545 {
4546 	struct nftables_pernet *nft_net = nft_pernet(ctx->net);
4547 	u32 portid = ctx->portid;
4548 	struct sk_buff *skb;
4549 	u16 flags = 0;
4550 	int err;
4551 
4552 	if (!ctx->report &&
4553 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
4554 		return;
4555 
4556 	skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
4557 	if (skb == NULL)
4558 		goto err;
4559 
4560 	if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
4561 		flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
4562 
4563 	err = nf_tables_fill_set(skb, ctx, set, event, flags);
4564 	if (err < 0) {
4565 		kfree_skb(skb);
4566 		goto err;
4567 	}
4568 
4569 	nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
4570 	return;
4571 err:
4572 	nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
4573 }
4574 
nf_tables_dump_sets(struct sk_buff * skb,struct netlink_callback * cb)4575 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
4576 {
4577 	const struct nft_set *set;
4578 	unsigned int idx, s_idx = cb->args[0];
4579 	struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
4580 	struct net *net = sock_net(skb->sk);
4581 	struct nft_ctx *ctx = cb->data, ctx_set;
4582 	struct nftables_pernet *nft_net;
4583 
4584 	if (cb->args[1])
4585 		return skb->len;
4586 
4587 	rcu_read_lock();
4588 	nft_net = nft_pernet(net);
4589 	cb->seq = READ_ONCE(nft_net->base_seq);
4590 
4591 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
4592 		if (ctx->family != NFPROTO_UNSPEC &&
4593 		    ctx->family != table->family)
4594 			continue;
4595 
4596 		if (ctx->table && ctx->table != table)
4597 			continue;
4598 
4599 		if (cur_table) {
4600 			if (cur_table != table)
4601 				continue;
4602 
4603 			cur_table = NULL;
4604 		}
4605 		idx = 0;
4606 		list_for_each_entry_rcu(set, &table->sets, list) {
4607 			if (idx < s_idx)
4608 				goto cont;
4609 			if (!nft_is_active(net, set))
4610 				goto cont;
4611 
4612 			ctx_set = *ctx;
4613 			ctx_set.table = table;
4614 			ctx_set.family = table->family;
4615 
4616 			if (nf_tables_fill_set(skb, &ctx_set, set,
4617 					       NFT_MSG_NEWSET,
4618 					       NLM_F_MULTI) < 0) {
4619 				cb->args[0] = idx;
4620 				cb->args[2] = (unsigned long) table;
4621 				goto done;
4622 			}
4623 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4624 cont:
4625 			idx++;
4626 		}
4627 		if (s_idx)
4628 			s_idx = 0;
4629 	}
4630 	cb->args[1] = 1;
4631 done:
4632 	rcu_read_unlock();
4633 	return skb->len;
4634 }
4635 
nf_tables_dump_sets_start(struct netlink_callback * cb)4636 static int nf_tables_dump_sets_start(struct netlink_callback *cb)
4637 {
4638 	struct nft_ctx *ctx_dump = NULL;
4639 
4640 	ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
4641 	if (ctx_dump == NULL)
4642 		return -ENOMEM;
4643 
4644 	cb->data = ctx_dump;
4645 	return 0;
4646 }
4647 
nf_tables_dump_sets_done(struct netlink_callback * cb)4648 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
4649 {
4650 	kfree(cb->data);
4651 	return 0;
4652 }
4653 
4654 /* called with rcu_read_lock held */
nf_tables_getset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])4655 static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info,
4656 			    const struct nlattr * const nla[])
4657 {
4658 	struct netlink_ext_ack *extack = info->extack;
4659 	u8 genmask = nft_genmask_cur(info->net);
4660 	u8 family = info->nfmsg->nfgen_family;
4661 	struct nft_table *table = NULL;
4662 	struct net *net = info->net;
4663 	const struct nft_set *set;
4664 	struct sk_buff *skb2;
4665 	struct nft_ctx ctx;
4666 	int err;
4667 
4668 	if (nla[NFTA_SET_TABLE]) {
4669 		table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
4670 					 genmask, 0);
4671 		if (IS_ERR(table)) {
4672 			NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
4673 			return PTR_ERR(table);
4674 		}
4675 	}
4676 
4677 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
4678 
4679 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
4680 		struct netlink_dump_control c = {
4681 			.start = nf_tables_dump_sets_start,
4682 			.dump = nf_tables_dump_sets,
4683 			.done = nf_tables_dump_sets_done,
4684 			.data = &ctx,
4685 			.module = THIS_MODULE,
4686 		};
4687 
4688 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
4689 	}
4690 
4691 	/* Only accept unspec with dump */
4692 	if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
4693 		return -EAFNOSUPPORT;
4694 	if (!nla[NFTA_SET_TABLE])
4695 		return -EINVAL;
4696 
4697 	set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
4698 	if (IS_ERR(set))
4699 		return PTR_ERR(set);
4700 
4701 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
4702 	if (skb2 == NULL)
4703 		return -ENOMEM;
4704 
4705 	err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
4706 	if (err < 0)
4707 		goto err_fill_set_info;
4708 
4709 	return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
4710 
4711 err_fill_set_info:
4712 	kfree_skb(skb2);
4713 	return err;
4714 }
4715 
4716 static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = {
4717 	[NFTA_SET_FIELD_LEN]	= { .type = NLA_U32 },
4718 };
4719 
nft_set_desc_concat_parse(const struct nlattr * attr,struct nft_set_desc * desc)4720 static int nft_set_desc_concat_parse(const struct nlattr *attr,
4721 				     struct nft_set_desc *desc)
4722 {
4723 	struct nlattr *tb[NFTA_SET_FIELD_MAX + 1];
4724 	u32 len;
4725 	int err;
4726 
4727 	if (desc->field_count >= ARRAY_SIZE(desc->field_len))
4728 		return -E2BIG;
4729 
4730 	err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr,
4731 					  nft_concat_policy, NULL);
4732 	if (err < 0)
4733 		return err;
4734 
4735 	if (!tb[NFTA_SET_FIELD_LEN])
4736 		return -EINVAL;
4737 
4738 	len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN]));
4739 	if (!len || len > U8_MAX)
4740 		return -EINVAL;
4741 
4742 	desc->field_len[desc->field_count++] = len;
4743 
4744 	return 0;
4745 }
4746 
nft_set_desc_concat(struct nft_set_desc * desc,const struct nlattr * nla)4747 static int nft_set_desc_concat(struct nft_set_desc *desc,
4748 			       const struct nlattr *nla)
4749 {
4750 	struct nlattr *attr;
4751 	u32 num_regs = 0;
4752 	int rem, err, i;
4753 
4754 	nla_for_each_nested(attr, nla, rem) {
4755 		if (nla_type(attr) != NFTA_LIST_ELEM)
4756 			return -EINVAL;
4757 
4758 		err = nft_set_desc_concat_parse(attr, desc);
4759 		if (err < 0)
4760 			return err;
4761 	}
4762 
4763 	for (i = 0; i < desc->field_count; i++)
4764 		num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32));
4765 
4766 	if (num_regs > NFT_REG32_COUNT)
4767 		return -E2BIG;
4768 
4769 	return 0;
4770 }
4771 
nf_tables_set_desc_parse(struct nft_set_desc * desc,const struct nlattr * nla)4772 static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
4773 				    const struct nlattr *nla)
4774 {
4775 	struct nlattr *da[NFTA_SET_DESC_MAX + 1];
4776 	int err;
4777 
4778 	err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
4779 					  nft_set_desc_policy, NULL);
4780 	if (err < 0)
4781 		return err;
4782 
4783 	if (da[NFTA_SET_DESC_SIZE] != NULL)
4784 		desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
4785 	if (da[NFTA_SET_DESC_CONCAT])
4786 		err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]);
4787 
4788 	return err;
4789 }
4790 
nft_set_expr_alloc(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * const * nla,struct nft_expr ** exprs,int * num_exprs,u32 flags)4791 static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set,
4792 			      const struct nlattr * const *nla,
4793 			      struct nft_expr **exprs, int *num_exprs,
4794 			      u32 flags)
4795 {
4796 	struct nft_expr *expr;
4797 	int err, i;
4798 
4799 	if (nla[NFTA_SET_EXPR]) {
4800 		expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]);
4801 		if (IS_ERR(expr)) {
4802 			err = PTR_ERR(expr);
4803 			goto err_set_expr_alloc;
4804 		}
4805 		exprs[0] = expr;
4806 		(*num_exprs)++;
4807 	} else if (nla[NFTA_SET_EXPRESSIONS]) {
4808 		struct nlattr *tmp;
4809 		int left;
4810 
4811 		if (!(flags & NFT_SET_EXPR)) {
4812 			err = -EINVAL;
4813 			goto err_set_expr_alloc;
4814 		}
4815 		i = 0;
4816 		nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) {
4817 			if (i == NFT_SET_EXPR_MAX) {
4818 				err = -E2BIG;
4819 				goto err_set_expr_alloc;
4820 			}
4821 			if (nla_type(tmp) != NFTA_LIST_ELEM) {
4822 				err = -EINVAL;
4823 				goto err_set_expr_alloc;
4824 			}
4825 			expr = nft_set_elem_expr_alloc(ctx, set, tmp);
4826 			if (IS_ERR(expr)) {
4827 				err = PTR_ERR(expr);
4828 				goto err_set_expr_alloc;
4829 			}
4830 			exprs[i++] = expr;
4831 			(*num_exprs)++;
4832 		}
4833 	}
4834 
4835 	return 0;
4836 
4837 err_set_expr_alloc:
4838 	for (i = 0; i < *num_exprs; i++)
4839 		nft_expr_destroy(ctx, exprs[i]);
4840 
4841 	return err;
4842 }
4843 
nft_set_is_same(const struct nft_set * set,const struct nft_set_desc * desc,struct nft_expr * exprs[],u32 num_exprs,u32 flags)4844 static bool nft_set_is_same(const struct nft_set *set,
4845 			    const struct nft_set_desc *desc,
4846 			    struct nft_expr *exprs[], u32 num_exprs, u32 flags)
4847 {
4848 	int i;
4849 
4850 	if (set->ktype != desc->ktype ||
4851 	    set->dtype != desc->dtype ||
4852 	    set->flags != flags ||
4853 	    set->klen != desc->klen ||
4854 	    set->dlen != desc->dlen ||
4855 	    set->field_count != desc->field_count ||
4856 	    set->num_exprs != num_exprs)
4857 		return false;
4858 
4859 	for (i = 0; i < desc->field_count; i++) {
4860 		if (set->field_len[i] != desc->field_len[i])
4861 			return false;
4862 	}
4863 
4864 	for (i = 0; i < num_exprs; i++) {
4865 		if (set->exprs[i]->ops != exprs[i]->ops)
4866 			return false;
4867 	}
4868 
4869 	return true;
4870 }
4871 
nf_tables_newset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])4872 static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,
4873 			    const struct nlattr * const nla[])
4874 {
4875 	struct netlink_ext_ack *extack = info->extack;
4876 	u8 genmask = nft_genmask_next(info->net);
4877 	u8 family = info->nfmsg->nfgen_family;
4878 	const struct nft_set_ops *ops;
4879 	struct net *net = info->net;
4880 	struct nft_set_desc desc;
4881 	struct nft_table *table;
4882 	unsigned char *udata;
4883 	struct nft_set *set;
4884 	struct nft_ctx ctx;
4885 	size_t alloc_size;
4886 	int num_exprs = 0;
4887 	char *name;
4888 	int err, i;
4889 	u16 udlen;
4890 	u32 flags;
4891 	u64 size;
4892 
4893 	if (nla[NFTA_SET_TABLE] == NULL ||
4894 	    nla[NFTA_SET_NAME] == NULL ||
4895 	    nla[NFTA_SET_KEY_LEN] == NULL ||
4896 	    nla[NFTA_SET_ID] == NULL)
4897 		return -EINVAL;
4898 
4899 	memset(&desc, 0, sizeof(desc));
4900 
4901 	desc.ktype = NFT_DATA_VALUE;
4902 	if (nla[NFTA_SET_KEY_TYPE] != NULL) {
4903 		desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
4904 		if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
4905 			return -EINVAL;
4906 	}
4907 
4908 	desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
4909 	if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
4910 		return -EINVAL;
4911 
4912 	flags = 0;
4913 	if (nla[NFTA_SET_FLAGS] != NULL) {
4914 		flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4915 		if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
4916 			      NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
4917 			      NFT_SET_MAP | NFT_SET_EVAL |
4918 			      NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR))
4919 			return -EOPNOTSUPP;
4920 		/* Only one of these operations is supported */
4921 		if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) ==
4922 			     (NFT_SET_MAP | NFT_SET_OBJECT))
4923 			return -EOPNOTSUPP;
4924 		if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) ==
4925 			     (NFT_SET_EVAL | NFT_SET_OBJECT))
4926 			return -EOPNOTSUPP;
4927 	}
4928 
4929 	desc.dtype = 0;
4930 	if (nla[NFTA_SET_DATA_TYPE] != NULL) {
4931 		if (!(flags & NFT_SET_MAP))
4932 			return -EINVAL;
4933 
4934 		desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
4935 		if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
4936 		    desc.dtype != NFT_DATA_VERDICT)
4937 			return -EINVAL;
4938 
4939 		if (desc.dtype != NFT_DATA_VERDICT) {
4940 			if (nla[NFTA_SET_DATA_LEN] == NULL)
4941 				return -EINVAL;
4942 			desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
4943 			if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
4944 				return -EINVAL;
4945 		} else
4946 			desc.dlen = sizeof(struct nft_verdict);
4947 	} else if (flags & NFT_SET_MAP)
4948 		return -EINVAL;
4949 
4950 	if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
4951 		if (!(flags & NFT_SET_OBJECT))
4952 			return -EINVAL;
4953 
4954 		desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
4955 		if (desc.objtype == NFT_OBJECT_UNSPEC ||
4956 		    desc.objtype > NFT_OBJECT_MAX)
4957 			return -EOPNOTSUPP;
4958 	} else if (flags & NFT_SET_OBJECT)
4959 		return -EINVAL;
4960 	else
4961 		desc.objtype = NFT_OBJECT_UNSPEC;
4962 
4963 	desc.timeout = 0;
4964 	if (nla[NFTA_SET_TIMEOUT] != NULL) {
4965 		if (!(flags & NFT_SET_TIMEOUT))
4966 			return -EINVAL;
4967 
4968 		if (flags & NFT_SET_ANONYMOUS)
4969 			return -EOPNOTSUPP;
4970 
4971 		err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout);
4972 		if (err)
4973 			return err;
4974 	}
4975 	desc.gc_int = 0;
4976 	if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
4977 		if (!(flags & NFT_SET_TIMEOUT))
4978 			return -EINVAL;
4979 
4980 		if (flags & NFT_SET_ANONYMOUS)
4981 			return -EOPNOTSUPP;
4982 
4983 		desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
4984 	}
4985 
4986 	desc.policy = NFT_SET_POL_PERFORMANCE;
4987 	if (nla[NFTA_SET_POLICY] != NULL)
4988 		desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
4989 
4990 	if (nla[NFTA_SET_DESC] != NULL) {
4991 		err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
4992 		if (err < 0)
4993 			return err;
4994 
4995 		if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT))
4996 			return -EINVAL;
4997 	} else if (flags & NFT_SET_CONCAT) {
4998 		return -EINVAL;
4999 	}
5000 
5001 	if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS])
5002 		desc.expr = true;
5003 
5004 	table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask,
5005 				 NETLINK_CB(skb).portid);
5006 	if (IS_ERR(table)) {
5007 		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
5008 		return PTR_ERR(table);
5009 	}
5010 
5011 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
5012 
5013 	set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
5014 	if (IS_ERR(set)) {
5015 		if (PTR_ERR(set) != -ENOENT) {
5016 			NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5017 			return PTR_ERR(set);
5018 		}
5019 	} else {
5020 		struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {};
5021 
5022 		if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
5023 			NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5024 			return -EEXIST;
5025 		}
5026 		if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
5027 			return -EOPNOTSUPP;
5028 
5029 		if (nft_set_is_anonymous(set))
5030 			return -EOPNOTSUPP;
5031 
5032 		err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags);
5033 		if (err < 0)
5034 			return err;
5035 
5036 		err = 0;
5037 		if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) {
5038 			NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5039 			err = -EEXIST;
5040 		}
5041 
5042 		for (i = 0; i < num_exprs; i++)
5043 			nft_expr_destroy(&ctx, exprs[i]);
5044 
5045 		if (err < 0)
5046 			return err;
5047 
5048 		return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc);
5049 	}
5050 
5051 	if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
5052 		return -ENOENT;
5053 
5054 	ops = nft_select_set_ops(&ctx, nla, &desc);
5055 	if (IS_ERR(ops))
5056 		return PTR_ERR(ops);
5057 
5058 	udlen = 0;
5059 	if (nla[NFTA_SET_USERDATA])
5060 		udlen = nla_len(nla[NFTA_SET_USERDATA]);
5061 
5062 	size = 0;
5063 	if (ops->privsize != NULL)
5064 		size = ops->privsize(nla, &desc);
5065 	alloc_size = sizeof(*set) + size + udlen;
5066 	if (alloc_size < size || alloc_size > INT_MAX)
5067 		return -ENOMEM;
5068 
5069 	if (!nft_use_inc(&table->use))
5070 		return -EMFILE;
5071 
5072 	set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT);
5073 	if (!set) {
5074 		err = -ENOMEM;
5075 		goto err_alloc;
5076 	}
5077 
5078 	name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT);
5079 	if (!name) {
5080 		err = -ENOMEM;
5081 		goto err_set_name;
5082 	}
5083 
5084 	err = nf_tables_set_alloc_name(&ctx, set, name);
5085 	kfree(name);
5086 	if (err < 0)
5087 		goto err_set_name;
5088 
5089 	udata = NULL;
5090 	if (udlen) {
5091 		udata = set->data + size;
5092 		nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
5093 	}
5094 
5095 	INIT_LIST_HEAD(&set->bindings);
5096 	INIT_LIST_HEAD(&set->catchall_list);
5097 	refcount_set(&set->refs, 1);
5098 	set->table = table;
5099 	write_pnet(&set->net, net);
5100 	set->ops = ops;
5101 	set->ktype = desc.ktype;
5102 	set->klen = desc.klen;
5103 	set->dtype = desc.dtype;
5104 	set->objtype = desc.objtype;
5105 	set->dlen = desc.dlen;
5106 	set->flags = flags;
5107 	set->size = desc.size;
5108 	set->policy = desc.policy;
5109 	set->udlen = udlen;
5110 	set->udata = udata;
5111 	set->timeout = desc.timeout;
5112 	set->gc_int = desc.gc_int;
5113 
5114 	set->field_count = desc.field_count;
5115 	for (i = 0; i < desc.field_count; i++)
5116 		set->field_len[i] = desc.field_len[i];
5117 
5118 	err = ops->init(set, &desc, nla);
5119 	if (err < 0)
5120 		goto err_set_init;
5121 
5122 	err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags);
5123 	if (err < 0)
5124 		goto err_set_destroy;
5125 
5126 	set->num_exprs = num_exprs;
5127 	set->handle = nf_tables_alloc_handle(table);
5128 	INIT_LIST_HEAD(&set->pending_update);
5129 
5130 	err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
5131 	if (err < 0)
5132 		goto err_set_expr_alloc;
5133 
5134 	list_add_tail_rcu(&set->list, &table->sets);
5135 
5136 	return 0;
5137 
5138 err_set_expr_alloc:
5139 	for (i = 0; i < set->num_exprs; i++)
5140 		nft_expr_destroy(&ctx, set->exprs[i]);
5141 err_set_destroy:
5142 	ops->destroy(&ctx, set);
5143 err_set_init:
5144 	kfree(set->name);
5145 err_set_name:
5146 	kvfree(set);
5147 err_alloc:
5148 	nft_use_dec_restore(&table->use);
5149 
5150 	return err;
5151 }
5152 
nft_set_catchall_destroy(const struct nft_ctx * ctx,struct nft_set * set)5153 static void nft_set_catchall_destroy(const struct nft_ctx *ctx,
5154 				     struct nft_set *set)
5155 {
5156 	struct nft_set_elem_catchall *next, *catchall;
5157 
5158 	list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
5159 		list_del_rcu(&catchall->list);
5160 		nf_tables_set_elem_destroy(ctx, set, catchall->elem);
5161 		kfree_rcu(catchall, rcu);
5162 	}
5163 }
5164 
nft_set_put(struct nft_set * set)5165 static void nft_set_put(struct nft_set *set)
5166 {
5167 	if (refcount_dec_and_test(&set->refs)) {
5168 		kfree(set->name);
5169 		kvfree(set);
5170 	}
5171 }
5172 
nft_set_destroy(const struct nft_ctx * ctx,struct nft_set * set)5173 static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
5174 {
5175 	int i;
5176 
5177 	if (WARN_ON(set->use > 0))
5178 		return;
5179 
5180 	for (i = 0; i < set->num_exprs; i++)
5181 		nft_expr_destroy(ctx, set->exprs[i]);
5182 
5183 	set->ops->destroy(ctx, set);
5184 	nft_set_catchall_destroy(ctx, set);
5185 	nft_set_put(set);
5186 }
5187 
nf_tables_delset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])5188 static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,
5189 			    const struct nlattr * const nla[])
5190 {
5191 	struct netlink_ext_ack *extack = info->extack;
5192 	u8 genmask = nft_genmask_next(info->net);
5193 	u8 family = info->nfmsg->nfgen_family;
5194 	struct net *net = info->net;
5195 	const struct nlattr *attr;
5196 	struct nft_table *table;
5197 	struct nft_set *set;
5198 	struct nft_ctx ctx;
5199 
5200 	if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
5201 		return -EAFNOSUPPORT;
5202 
5203 	table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
5204 				 genmask, NETLINK_CB(skb).portid);
5205 	if (IS_ERR(table)) {
5206 		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
5207 		return PTR_ERR(table);
5208 	}
5209 
5210 	if (nla[NFTA_SET_HANDLE]) {
5211 		attr = nla[NFTA_SET_HANDLE];
5212 		set = nft_set_lookup_byhandle(table, attr, genmask);
5213 	} else {
5214 		attr = nla[NFTA_SET_NAME];
5215 		set = nft_set_lookup(table, attr, genmask);
5216 	}
5217 
5218 	if (IS_ERR(set)) {
5219 		if (PTR_ERR(set) == -ENOENT &&
5220 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET)
5221 			return 0;
5222 
5223 		NL_SET_BAD_ATTR(extack, attr);
5224 		return PTR_ERR(set);
5225 	}
5226 	if (set->use ||
5227 	    (info->nlh->nlmsg_flags & NLM_F_NONREC &&
5228 	     atomic_read(&set->nelems) > 0)) {
5229 		NL_SET_BAD_ATTR(extack, attr);
5230 		return -EBUSY;
5231 	}
5232 
5233 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
5234 
5235 	return nft_delset(&ctx, set);
5236 }
5237 
5238 static int nft_validate_register_store(const struct nft_ctx *ctx,
5239 				       enum nft_registers reg,
5240 				       const struct nft_data *data,
5241 				       enum nft_data_types type,
5242 				       unsigned int len);
5243 
nft_setelem_data_validate(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem)5244 static int nft_setelem_data_validate(const struct nft_ctx *ctx,
5245 				     struct nft_set *set,
5246 				     struct nft_set_elem *elem)
5247 {
5248 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5249 	enum nft_registers dreg;
5250 
5251 	dreg = nft_type_to_reg(set->dtype);
5252 	return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
5253 					   set->dtype == NFT_DATA_VERDICT ?
5254 					   NFT_DATA_VERDICT : NFT_DATA_VALUE,
5255 					   set->dlen);
5256 }
5257 
nf_tables_bind_check_setelem(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)5258 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
5259 					struct nft_set *set,
5260 					const struct nft_set_iter *iter,
5261 					struct nft_set_elem *elem)
5262 {
5263 	return nft_setelem_data_validate(ctx, set, elem);
5264 }
5265 
nft_set_catchall_bind_check(const struct nft_ctx * ctx,struct nft_set * set)5266 static int nft_set_catchall_bind_check(const struct nft_ctx *ctx,
5267 				       struct nft_set *set)
5268 {
5269 	u8 genmask = nft_genmask_next(ctx->net);
5270 	struct nft_set_elem_catchall *catchall;
5271 	struct nft_set_elem elem;
5272 	struct nft_set_ext *ext;
5273 	int ret = 0;
5274 
5275 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5276 		ext = nft_set_elem_ext(set, catchall->elem);
5277 		if (!nft_set_elem_active(ext, genmask))
5278 			continue;
5279 
5280 		elem.priv = catchall->elem;
5281 		ret = nft_setelem_data_validate(ctx, set, &elem);
5282 		if (ret < 0)
5283 			break;
5284 	}
5285 
5286 	return ret;
5287 }
5288 
nf_tables_bind_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding)5289 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
5290 		       struct nft_set_binding *binding)
5291 {
5292 	struct nft_set_binding *i;
5293 	struct nft_set_iter iter;
5294 
5295 	if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
5296 		return -EBUSY;
5297 
5298 	if (binding->flags & NFT_SET_MAP) {
5299 		/* If the set is already bound to the same chain all
5300 		 * jumps are already validated for that chain.
5301 		 */
5302 		list_for_each_entry(i, &set->bindings, list) {
5303 			if (i->flags & NFT_SET_MAP &&
5304 			    i->chain == binding->chain)
5305 				goto bind;
5306 		}
5307 
5308 		iter.genmask	= nft_genmask_next(ctx->net);
5309 		iter.skip 	= 0;
5310 		iter.count	= 0;
5311 		iter.err	= 0;
5312 		iter.fn		= nf_tables_bind_check_setelem;
5313 
5314 		set->ops->walk(ctx, set, &iter);
5315 		if (!iter.err)
5316 			iter.err = nft_set_catchall_bind_check(ctx, set);
5317 
5318 		if (iter.err < 0)
5319 			return iter.err;
5320 	}
5321 bind:
5322 	if (!nft_use_inc(&set->use))
5323 		return -EMFILE;
5324 
5325 	binding->chain = ctx->chain;
5326 	list_add_tail_rcu(&binding->list, &set->bindings);
5327 	nft_set_trans_bind(ctx, set);
5328 
5329 	return 0;
5330 }
5331 EXPORT_SYMBOL_GPL(nf_tables_bind_set);
5332 
nf_tables_unbind_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding,bool event)5333 static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
5334 				 struct nft_set_binding *binding, bool event)
5335 {
5336 	list_del_rcu(&binding->list);
5337 
5338 	if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
5339 		list_del_rcu(&set->list);
5340 		if (event)
5341 			nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
5342 					     GFP_KERNEL);
5343 	}
5344 }
5345 
5346 static void nft_setelem_data_activate(const struct net *net,
5347 				      const struct nft_set *set,
5348 				      struct nft_set_elem *elem);
5349 
nft_mapelem_activate(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)5350 static int nft_mapelem_activate(const struct nft_ctx *ctx,
5351 				struct nft_set *set,
5352 				const struct nft_set_iter *iter,
5353 				struct nft_set_elem *elem)
5354 {
5355 	nft_setelem_data_activate(ctx->net, set, elem);
5356 
5357 	return 0;
5358 }
5359 
nft_map_catchall_activate(const struct nft_ctx * ctx,struct nft_set * set)5360 static void nft_map_catchall_activate(const struct nft_ctx *ctx,
5361 				      struct nft_set *set)
5362 {
5363 	u8 genmask = nft_genmask_next(ctx->net);
5364 	struct nft_set_elem_catchall *catchall;
5365 	struct nft_set_elem elem;
5366 	struct nft_set_ext *ext;
5367 
5368 	list_for_each_entry(catchall, &set->catchall_list, list) {
5369 		ext = nft_set_elem_ext(set, catchall->elem);
5370 		if (!nft_set_elem_active(ext, genmask))
5371 			continue;
5372 
5373 		elem.priv = catchall->elem;
5374 		nft_setelem_data_activate(ctx->net, set, &elem);
5375 		break;
5376 	}
5377 }
5378 
nft_map_activate(const struct nft_ctx * ctx,struct nft_set * set)5379 static void nft_map_activate(const struct nft_ctx *ctx, struct nft_set *set)
5380 {
5381 	struct nft_set_iter iter = {
5382 		.genmask	= nft_genmask_next(ctx->net),
5383 		.fn		= nft_mapelem_activate,
5384 	};
5385 
5386 	set->ops->walk(ctx, set, &iter);
5387 	WARN_ON_ONCE(iter.err);
5388 
5389 	nft_map_catchall_activate(ctx, set);
5390 }
5391 
nf_tables_activate_set(const struct nft_ctx * ctx,struct nft_set * set)5392 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set)
5393 {
5394 	if (nft_set_is_anonymous(set)) {
5395 		if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5396 			nft_map_activate(ctx, set);
5397 
5398 		nft_clear(ctx->net, set);
5399 	}
5400 
5401 	nft_use_inc_restore(&set->use);
5402 }
5403 EXPORT_SYMBOL_GPL(nf_tables_activate_set);
5404 
nf_tables_deactivate_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding,enum nft_trans_phase phase)5405 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
5406 			      struct nft_set_binding *binding,
5407 			      enum nft_trans_phase phase)
5408 {
5409 	switch (phase) {
5410 	case NFT_TRANS_PREPARE_ERROR:
5411 		nft_set_trans_unbind(ctx, set);
5412 		if (nft_set_is_anonymous(set))
5413 			nft_deactivate_next(ctx->net, set);
5414 		else
5415 			list_del_rcu(&binding->list);
5416 
5417 		nft_use_dec(&set->use);
5418 		break;
5419 	case NFT_TRANS_PREPARE:
5420 		if (nft_set_is_anonymous(set)) {
5421 			if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5422 				nft_map_deactivate(ctx, set);
5423 
5424 			nft_deactivate_next(ctx->net, set);
5425 		}
5426 		nft_use_dec(&set->use);
5427 		return;
5428 	case NFT_TRANS_ABORT:
5429 	case NFT_TRANS_RELEASE:
5430 		if (nft_set_is_anonymous(set) &&
5431 		    set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5432 			nft_map_deactivate(ctx, set);
5433 
5434 		nft_use_dec(&set->use);
5435 		fallthrough;
5436 	default:
5437 		nf_tables_unbind_set(ctx, set, binding,
5438 				     phase == NFT_TRANS_COMMIT);
5439 	}
5440 }
5441 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
5442 
nf_tables_destroy_set(const struct nft_ctx * ctx,struct nft_set * set)5443 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
5444 {
5445 	if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
5446 		nft_set_destroy(ctx, set);
5447 }
5448 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
5449 
5450 const struct nft_set_ext_type nft_set_ext_types[] = {
5451 	[NFT_SET_EXT_KEY]		= {
5452 		.align	= __alignof__(u32),
5453 	},
5454 	[NFT_SET_EXT_DATA]		= {
5455 		.align	= __alignof__(u32),
5456 	},
5457 	[NFT_SET_EXT_EXPRESSIONS]	= {
5458 		.align	= __alignof__(struct nft_set_elem_expr),
5459 	},
5460 	[NFT_SET_EXT_OBJREF]		= {
5461 		.len	= sizeof(struct nft_object *),
5462 		.align	= __alignof__(struct nft_object *),
5463 	},
5464 	[NFT_SET_EXT_FLAGS]		= {
5465 		.len	= sizeof(u8),
5466 		.align	= __alignof__(u8),
5467 	},
5468 	[NFT_SET_EXT_TIMEOUT]		= {
5469 		.len	= sizeof(u64),
5470 		.align	= __alignof__(u64),
5471 	},
5472 	[NFT_SET_EXT_EXPIRATION]	= {
5473 		.len	= sizeof(u64),
5474 		.align	= __alignof__(u64),
5475 	},
5476 	[NFT_SET_EXT_USERDATA]		= {
5477 		.len	= sizeof(struct nft_userdata),
5478 		.align	= __alignof__(struct nft_userdata),
5479 	},
5480 	[NFT_SET_EXT_KEY_END]		= {
5481 		.align	= __alignof__(u32),
5482 	},
5483 };
5484 
5485 /*
5486  * Set elements
5487  */
5488 
5489 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
5490 	[NFTA_SET_ELEM_KEY]		= { .type = NLA_NESTED },
5491 	[NFTA_SET_ELEM_DATA]		= { .type = NLA_NESTED },
5492 	[NFTA_SET_ELEM_FLAGS]		= { .type = NLA_U32 },
5493 	[NFTA_SET_ELEM_TIMEOUT]		= { .type = NLA_U64 },
5494 	[NFTA_SET_ELEM_EXPIRATION]	= { .type = NLA_U64 },
5495 	[NFTA_SET_ELEM_USERDATA]	= { .type = NLA_BINARY,
5496 					    .len = NFT_USERDATA_MAXLEN },
5497 	[NFTA_SET_ELEM_EXPR]		= { .type = NLA_NESTED },
5498 	[NFTA_SET_ELEM_OBJREF]		= { .type = NLA_STRING,
5499 					    .len = NFT_OBJ_MAXNAMELEN - 1 },
5500 	[NFTA_SET_ELEM_KEY_END]		= { .type = NLA_NESTED },
5501 	[NFTA_SET_ELEM_EXPRESSIONS]	= { .type = NLA_NESTED },
5502 };
5503 
5504 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
5505 	[NFTA_SET_ELEM_LIST_TABLE]	= { .type = NLA_STRING,
5506 					    .len = NFT_TABLE_MAXNAMELEN - 1 },
5507 	[NFTA_SET_ELEM_LIST_SET]	= { .type = NLA_STRING,
5508 					    .len = NFT_SET_MAXNAMELEN - 1 },
5509 	[NFTA_SET_ELEM_LIST_ELEMENTS]	= { .type = NLA_NESTED },
5510 	[NFTA_SET_ELEM_LIST_SET_ID]	= { .type = NLA_U32 },
5511 };
5512 
nft_set_elem_expr_dump(struct sk_buff * skb,const struct nft_set * set,const struct nft_set_ext * ext,bool reset)5513 static int nft_set_elem_expr_dump(struct sk_buff *skb,
5514 				  const struct nft_set *set,
5515 				  const struct nft_set_ext *ext,
5516 				  bool reset)
5517 {
5518 	struct nft_set_elem_expr *elem_expr;
5519 	u32 size, num_exprs = 0;
5520 	struct nft_expr *expr;
5521 	struct nlattr *nest;
5522 
5523 	elem_expr = nft_set_ext_expr(ext);
5524 	nft_setelem_expr_foreach(expr, elem_expr, size)
5525 		num_exprs++;
5526 
5527 	if (num_exprs == 1) {
5528 		expr = nft_setelem_expr_at(elem_expr, 0);
5529 		if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, reset) < 0)
5530 			return -1;
5531 
5532 		return 0;
5533 	} else if (num_exprs > 1) {
5534 		nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS);
5535 		if (nest == NULL)
5536 			goto nla_put_failure;
5537 
5538 		nft_setelem_expr_foreach(expr, elem_expr, size) {
5539 			expr = nft_setelem_expr_at(elem_expr, size);
5540 			if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
5541 				goto nla_put_failure;
5542 		}
5543 		nla_nest_end(skb, nest);
5544 	}
5545 	return 0;
5546 
5547 nla_put_failure:
5548 	return -1;
5549 }
5550 
nf_tables_fill_setelem(struct sk_buff * skb,const struct nft_set * set,const struct nft_set_elem * elem,bool reset)5551 static int nf_tables_fill_setelem(struct sk_buff *skb,
5552 				  const struct nft_set *set,
5553 				  const struct nft_set_elem *elem,
5554 				  bool reset)
5555 {
5556 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5557 	unsigned char *b = skb_tail_pointer(skb);
5558 	struct nlattr *nest;
5559 
5560 	nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
5561 	if (nest == NULL)
5562 		goto nla_put_failure;
5563 
5564 	if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
5565 	    nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
5566 			  NFT_DATA_VALUE, set->klen) < 0)
5567 		goto nla_put_failure;
5568 
5569 	if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
5570 	    nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext),
5571 			  NFT_DATA_VALUE, set->klen) < 0)
5572 		goto nla_put_failure;
5573 
5574 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5575 	    nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
5576 			  set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
5577 			  set->dlen) < 0)
5578 		goto nla_put_failure;
5579 
5580 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) &&
5581 	    nft_set_elem_expr_dump(skb, set, ext, reset))
5582 		goto nla_put_failure;
5583 
5584 	if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
5585 	    nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
5586 			   (*nft_set_ext_obj(ext))->key.name) < 0)
5587 		goto nla_put_failure;
5588 
5589 	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
5590 	    nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
5591 		         htonl(*nft_set_ext_flags(ext))))
5592 		goto nla_put_failure;
5593 
5594 	if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
5595 	    nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
5596 			 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
5597 			 NFTA_SET_ELEM_PAD))
5598 		goto nla_put_failure;
5599 
5600 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5601 		u64 expires, now = get_jiffies_64();
5602 
5603 		expires = *nft_set_ext_expiration(ext);
5604 		if (time_before64(now, expires))
5605 			expires -= now;
5606 		else
5607 			expires = 0;
5608 
5609 		if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
5610 				 nf_jiffies64_to_msecs(expires),
5611 				 NFTA_SET_ELEM_PAD))
5612 			goto nla_put_failure;
5613 	}
5614 
5615 	if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
5616 		struct nft_userdata *udata;
5617 
5618 		udata = nft_set_ext_userdata(ext);
5619 		if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
5620 			    udata->len + 1, udata->data))
5621 			goto nla_put_failure;
5622 	}
5623 
5624 	nla_nest_end(skb, nest);
5625 	return 0;
5626 
5627 nla_put_failure:
5628 	nlmsg_trim(skb, b);
5629 	return -EMSGSIZE;
5630 }
5631 
5632 struct nft_set_dump_args {
5633 	const struct netlink_callback	*cb;
5634 	struct nft_set_iter		iter;
5635 	struct sk_buff			*skb;
5636 	bool				reset;
5637 };
5638 
nf_tables_dump_setelem(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)5639 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
5640 				  struct nft_set *set,
5641 				  const struct nft_set_iter *iter,
5642 				  struct nft_set_elem *elem)
5643 {
5644 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5645 	struct nft_set_dump_args *args;
5646 
5647 	if (nft_set_elem_expired(ext))
5648 		return 0;
5649 
5650 	args = container_of(iter, struct nft_set_dump_args, iter);
5651 	return nf_tables_fill_setelem(args->skb, set, elem, args->reset);
5652 }
5653 
audit_log_nft_set_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)5654 static void audit_log_nft_set_reset(const struct nft_table *table,
5655 				    unsigned int base_seq,
5656 				    unsigned int nentries)
5657 {
5658 	char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
5659 
5660 	audit_log_nfcfg(buf, table->family, nentries,
5661 			AUDIT_NFT_OP_SETELEM_RESET, GFP_ATOMIC);
5662 	kfree(buf);
5663 }
5664 
5665 struct nft_set_dump_ctx {
5666 	const struct nft_set	*set;
5667 	struct nft_ctx		ctx;
5668 };
5669 
nft_set_catchall_dump(struct net * net,struct sk_buff * skb,const struct nft_set * set,bool reset,unsigned int base_seq)5670 static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb,
5671 				 const struct nft_set *set, bool reset,
5672 				 unsigned int base_seq)
5673 {
5674 	struct nft_set_elem_catchall *catchall;
5675 	u8 genmask = nft_genmask_cur(net);
5676 	struct nft_set_elem elem;
5677 	struct nft_set_ext *ext;
5678 	int ret = 0;
5679 
5680 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5681 		ext = nft_set_elem_ext(set, catchall->elem);
5682 		if (!nft_set_elem_active(ext, genmask) ||
5683 		    nft_set_elem_expired(ext))
5684 			continue;
5685 
5686 		elem.priv = catchall->elem;
5687 		ret = nf_tables_fill_setelem(skb, set, &elem, reset);
5688 		if (reset && !ret)
5689 			audit_log_nft_set_reset(set->table, base_seq, 1);
5690 		break;
5691 	}
5692 
5693 	return ret;
5694 }
5695 
nf_tables_dump_set(struct sk_buff * skb,struct netlink_callback * cb)5696 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
5697 {
5698 	struct nft_set_dump_ctx *dump_ctx = cb->data;
5699 	struct net *net = sock_net(skb->sk);
5700 	struct nftables_pernet *nft_net;
5701 	struct nft_table *table;
5702 	struct nft_set *set;
5703 	struct nft_set_dump_args args;
5704 	bool set_found = false;
5705 	struct nlmsghdr *nlh;
5706 	struct nlattr *nest;
5707 	bool reset = false;
5708 	u32 portid, seq;
5709 	int event;
5710 
5711 	rcu_read_lock();
5712 	nft_net = nft_pernet(net);
5713 	cb->seq = READ_ONCE(nft_net->base_seq);
5714 
5715 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
5716 		if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
5717 		    dump_ctx->ctx.family != table->family)
5718 			continue;
5719 
5720 		if (table != dump_ctx->ctx.table)
5721 			continue;
5722 
5723 		list_for_each_entry_rcu(set, &table->sets, list) {
5724 			if (set == dump_ctx->set) {
5725 				set_found = true;
5726 				break;
5727 			}
5728 		}
5729 		break;
5730 	}
5731 
5732 	if (!set_found) {
5733 		rcu_read_unlock();
5734 		return -ENOENT;
5735 	}
5736 
5737 	event  = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
5738 	portid = NETLINK_CB(cb->skb).portid;
5739 	seq    = cb->nlh->nlmsg_seq;
5740 
5741 	nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI,
5742 			   table->family, NFNETLINK_V0, nft_base_seq(net));
5743 	if (!nlh)
5744 		goto nla_put_failure;
5745 
5746 	if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
5747 		goto nla_put_failure;
5748 	if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
5749 		goto nla_put_failure;
5750 
5751 	nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
5752 	if (nest == NULL)
5753 		goto nla_put_failure;
5754 
5755 	if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETSETELEM_RESET)
5756 		reset = true;
5757 
5758 	args.cb			= cb;
5759 	args.skb		= skb;
5760 	args.reset		= reset;
5761 	args.iter.genmask	= nft_genmask_cur(net);
5762 	args.iter.skip		= cb->args[0];
5763 	args.iter.count		= 0;
5764 	args.iter.err		= 0;
5765 	args.iter.fn		= nf_tables_dump_setelem;
5766 	set->ops->walk(&dump_ctx->ctx, set, &args.iter);
5767 
5768 	if (!args.iter.err && args.iter.count == cb->args[0])
5769 		args.iter.err = nft_set_catchall_dump(net, skb, set,
5770 						      reset, cb->seq);
5771 	nla_nest_end(skb, nest);
5772 	nlmsg_end(skb, nlh);
5773 
5774 	if (reset && args.iter.count > args.iter.skip)
5775 		audit_log_nft_set_reset(table, cb->seq,
5776 					args.iter.count - args.iter.skip);
5777 
5778 	rcu_read_unlock();
5779 
5780 	if (args.iter.err && args.iter.err != -EMSGSIZE)
5781 		return args.iter.err;
5782 	if (args.iter.count == cb->args[0])
5783 		return 0;
5784 
5785 	cb->args[0] = args.iter.count;
5786 	return skb->len;
5787 
5788 nla_put_failure:
5789 	rcu_read_unlock();
5790 	return -ENOSPC;
5791 }
5792 
nf_tables_dump_set_start(struct netlink_callback * cb)5793 static int nf_tables_dump_set_start(struct netlink_callback *cb)
5794 {
5795 	struct nft_set_dump_ctx *dump_ctx = cb->data;
5796 
5797 	cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
5798 
5799 	return cb->data ? 0 : -ENOMEM;
5800 }
5801 
nf_tables_dump_set_done(struct netlink_callback * cb)5802 static int nf_tables_dump_set_done(struct netlink_callback *cb)
5803 {
5804 	kfree(cb->data);
5805 	return 0;
5806 }
5807 
nf_tables_fill_setelem_info(struct sk_buff * skb,const struct nft_ctx * ctx,u32 seq,u32 portid,int event,u16 flags,const struct nft_set * set,const struct nft_set_elem * elem,bool reset)5808 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
5809 				       const struct nft_ctx *ctx, u32 seq,
5810 				       u32 portid, int event, u16 flags,
5811 				       const struct nft_set *set,
5812 				       const struct nft_set_elem *elem,
5813 				       bool reset)
5814 {
5815 	struct nlmsghdr *nlh;
5816 	struct nlattr *nest;
5817 	int err;
5818 
5819 	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5820 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
5821 			   NFNETLINK_V0, nft_base_seq(ctx->net));
5822 	if (!nlh)
5823 		goto nla_put_failure;
5824 
5825 	if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
5826 		goto nla_put_failure;
5827 	if (nla_put_string(skb, NFTA_SET_NAME, set->name))
5828 		goto nla_put_failure;
5829 
5830 	nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
5831 	if (nest == NULL)
5832 		goto nla_put_failure;
5833 
5834 	err = nf_tables_fill_setelem(skb, set, elem, reset);
5835 	if (err < 0)
5836 		goto nla_put_failure;
5837 
5838 	nla_nest_end(skb, nest);
5839 
5840 	nlmsg_end(skb, nlh);
5841 	return 0;
5842 
5843 nla_put_failure:
5844 	nlmsg_trim(skb, nlh);
5845 	return -1;
5846 }
5847 
nft_setelem_parse_flags(const struct nft_set * set,const struct nlattr * attr,u32 * flags)5848 static int nft_setelem_parse_flags(const struct nft_set *set,
5849 				   const struct nlattr *attr, u32 *flags)
5850 {
5851 	if (attr == NULL)
5852 		return 0;
5853 
5854 	*flags = ntohl(nla_get_be32(attr));
5855 	if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
5856 		return -EOPNOTSUPP;
5857 	if (!(set->flags & NFT_SET_INTERVAL) &&
5858 	    *flags & NFT_SET_ELEM_INTERVAL_END)
5859 		return -EINVAL;
5860 	if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) ==
5861 	    (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
5862 		return -EINVAL;
5863 
5864 	return 0;
5865 }
5866 
nft_setelem_parse_key(struct nft_ctx * ctx,struct nft_set * set,struct nft_data * key,struct nlattr * attr)5867 static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set,
5868 				 struct nft_data *key, struct nlattr *attr)
5869 {
5870 	struct nft_data_desc desc = {
5871 		.type	= NFT_DATA_VALUE,
5872 		.size	= NFT_DATA_VALUE_MAXLEN,
5873 		.len	= set->klen,
5874 	};
5875 
5876 	return nft_data_init(ctx, key, &desc, attr);
5877 }
5878 
nft_setelem_parse_data(struct nft_ctx * ctx,struct nft_set * set,struct nft_data_desc * desc,struct nft_data * data,struct nlattr * attr)5879 static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set,
5880 				  struct nft_data_desc *desc,
5881 				  struct nft_data *data,
5882 				  struct nlattr *attr)
5883 {
5884 	u32 dtype;
5885 
5886 	if (set->dtype == NFT_DATA_VERDICT)
5887 		dtype = NFT_DATA_VERDICT;
5888 	else
5889 		dtype = NFT_DATA_VALUE;
5890 
5891 	desc->type = dtype;
5892 	desc->size = NFT_DATA_VALUE_MAXLEN;
5893 	desc->len = set->dlen;
5894 	desc->flags = NFT_DATA_DESC_SETELEM;
5895 
5896 	return nft_data_init(ctx, data, desc, attr);
5897 }
5898 
nft_setelem_catchall_get(const struct net * net,const struct nft_set * set)5899 static void *nft_setelem_catchall_get(const struct net *net,
5900 				      const struct nft_set *set)
5901 {
5902 	struct nft_set_elem_catchall *catchall;
5903 	u8 genmask = nft_genmask_cur(net);
5904 	struct nft_set_ext *ext;
5905 	void *priv = NULL;
5906 
5907 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5908 		ext = nft_set_elem_ext(set, catchall->elem);
5909 		if (!nft_set_elem_active(ext, genmask) ||
5910 		    nft_set_elem_expired(ext))
5911 			continue;
5912 
5913 		priv = catchall->elem;
5914 		break;
5915 	}
5916 
5917 	return priv;
5918 }
5919 
nft_setelem_get(struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem,u32 flags)5920 static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set,
5921 			   struct nft_set_elem *elem, u32 flags)
5922 {
5923 	void *priv;
5924 
5925 	if (!(flags & NFT_SET_ELEM_CATCHALL)) {
5926 		priv = set->ops->get(ctx->net, set, elem, flags);
5927 		if (IS_ERR(priv))
5928 			return PTR_ERR(priv);
5929 	} else {
5930 		priv = nft_setelem_catchall_get(ctx->net, set);
5931 		if (!priv)
5932 			return -ENOENT;
5933 	}
5934 	elem->priv = priv;
5935 
5936 	return 0;
5937 }
5938 
nft_get_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr,bool reset)5939 static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
5940 			    const struct nlattr *attr, bool reset)
5941 {
5942 	struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
5943 	struct nft_set_elem elem;
5944 	struct sk_buff *skb;
5945 	uint32_t flags = 0;
5946 	int err;
5947 
5948 	err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5949 					  nft_set_elem_policy, NULL);
5950 	if (err < 0)
5951 		return err;
5952 
5953 	err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5954 	if (err < 0)
5955 		return err;
5956 
5957 	if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
5958 		return -EINVAL;
5959 
5960 	if (nla[NFTA_SET_ELEM_KEY]) {
5961 		err = nft_setelem_parse_key(ctx, set, &elem.key.val,
5962 					    nla[NFTA_SET_ELEM_KEY]);
5963 		if (err < 0)
5964 			return err;
5965 	}
5966 
5967 	if (nla[NFTA_SET_ELEM_KEY_END]) {
5968 		err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
5969 					    nla[NFTA_SET_ELEM_KEY_END]);
5970 		if (err < 0)
5971 			return err;
5972 	}
5973 
5974 	err = nft_setelem_get(ctx, set, &elem, flags);
5975 	if (err < 0)
5976 		return err;
5977 
5978 	err = -ENOMEM;
5979 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
5980 	if (skb == NULL)
5981 		return err;
5982 
5983 	err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
5984 					  NFT_MSG_NEWSETELEM, 0, set, &elem,
5985 					  reset);
5986 	if (err < 0)
5987 		goto err_fill_setelem;
5988 
5989 	return nfnetlink_unicast(skb, ctx->net, ctx->portid);
5990 
5991 err_fill_setelem:
5992 	kfree_skb(skb);
5993 	return err;
5994 }
5995 
5996 /* called with rcu_read_lock held */
nf_tables_getsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])5997 static int nf_tables_getsetelem(struct sk_buff *skb,
5998 				const struct nfnl_info *info,
5999 				const struct nlattr * const nla[])
6000 {
6001 	struct netlink_ext_ack *extack = info->extack;
6002 	u8 genmask = nft_genmask_cur(info->net);
6003 	u8 family = info->nfmsg->nfgen_family;
6004 	int rem, err = 0, nelems = 0;
6005 	struct net *net = info->net;
6006 	struct nft_table *table;
6007 	struct nft_set *set;
6008 	struct nlattr *attr;
6009 	struct nft_ctx ctx;
6010 	bool reset = false;
6011 
6012 	table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6013 				 genmask, 0);
6014 	if (IS_ERR(table)) {
6015 		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6016 		return PTR_ERR(table);
6017 	}
6018 
6019 	set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
6020 	if (IS_ERR(set))
6021 		return PTR_ERR(set);
6022 
6023 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6024 
6025 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
6026 		struct netlink_dump_control c = {
6027 			.start = nf_tables_dump_set_start,
6028 			.dump = nf_tables_dump_set,
6029 			.done = nf_tables_dump_set_done,
6030 			.module = THIS_MODULE,
6031 		};
6032 		struct nft_set_dump_ctx dump_ctx = {
6033 			.set = set,
6034 			.ctx = ctx,
6035 		};
6036 
6037 		c.data = &dump_ctx;
6038 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
6039 	}
6040 
6041 	if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
6042 		return -EINVAL;
6043 
6044 	if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETSETELEM_RESET)
6045 		reset = true;
6046 
6047 	nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6048 		err = nft_get_set_elem(&ctx, set, attr, reset);
6049 		if (err < 0) {
6050 			NL_SET_BAD_ATTR(extack, attr);
6051 			break;
6052 		}
6053 		nelems++;
6054 	}
6055 
6056 	if (reset)
6057 		audit_log_nft_set_reset(table, nft_pernet(net)->base_seq,
6058 					nelems);
6059 
6060 	return err;
6061 }
6062 
nf_tables_setelem_notify(const struct nft_ctx * ctx,const struct nft_set * set,const struct nft_set_elem * elem,int event)6063 static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
6064 				     const struct nft_set *set,
6065 				     const struct nft_set_elem *elem,
6066 				     int event)
6067 {
6068 	struct nftables_pernet *nft_net;
6069 	struct net *net = ctx->net;
6070 	u32 portid = ctx->portid;
6071 	struct sk_buff *skb;
6072 	u16 flags = 0;
6073 	int err;
6074 
6075 	if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
6076 		return;
6077 
6078 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6079 	if (skb == NULL)
6080 		goto err;
6081 
6082 	if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
6083 		flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
6084 
6085 	err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
6086 					  set, elem, false);
6087 	if (err < 0) {
6088 		kfree_skb(skb);
6089 		goto err;
6090 	}
6091 
6092 	nft_net = nft_pernet(net);
6093 	nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
6094 	return;
6095 err:
6096 	nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
6097 }
6098 
nft_trans_elem_alloc(struct nft_ctx * ctx,int msg_type,struct nft_set * set)6099 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
6100 					      int msg_type,
6101 					      struct nft_set *set)
6102 {
6103 	struct nft_trans *trans;
6104 
6105 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
6106 	if (trans == NULL)
6107 		return NULL;
6108 
6109 	nft_trans_elem_set(trans) = set;
6110 	return trans;
6111 }
6112 
nft_set_elem_expr_alloc(const struct nft_ctx * ctx,const struct nft_set * set,const struct nlattr * attr)6113 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
6114 					 const struct nft_set *set,
6115 					 const struct nlattr *attr)
6116 {
6117 	struct nft_expr *expr;
6118 	int err;
6119 
6120 	expr = nft_expr_init(ctx, attr);
6121 	if (IS_ERR(expr))
6122 		return expr;
6123 
6124 	err = -EOPNOTSUPP;
6125 	if (expr->ops->type->flags & NFT_EXPR_GC) {
6126 		if (set->flags & NFT_SET_TIMEOUT)
6127 			goto err_set_elem_expr;
6128 		if (!set->ops->gc_init)
6129 			goto err_set_elem_expr;
6130 		set->ops->gc_init(set);
6131 	}
6132 
6133 	return expr;
6134 
6135 err_set_elem_expr:
6136 	nft_expr_destroy(ctx, expr);
6137 	return ERR_PTR(err);
6138 }
6139 
nft_set_ext_check(const struct nft_set_ext_tmpl * tmpl,u8 id,u32 len)6140 static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len)
6141 {
6142 	len += nft_set_ext_types[id].len;
6143 	if (len > tmpl->ext_len[id] ||
6144 	    len > U8_MAX)
6145 		return -1;
6146 
6147 	return 0;
6148 }
6149 
nft_set_ext_memcpy(const struct nft_set_ext_tmpl * tmpl,u8 id,void * to,const void * from,u32 len)6150 static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,
6151 			      void *to, const void *from, u32 len)
6152 {
6153 	if (nft_set_ext_check(tmpl, id, len) < 0)
6154 		return -1;
6155 
6156 	memcpy(to, from, len);
6157 
6158 	return 0;
6159 }
6160 
nft_set_elem_init(const struct nft_set * set,const struct nft_set_ext_tmpl * tmpl,const u32 * key,const u32 * key_end,const u32 * data,u64 timeout,u64 expiration,gfp_t gfp)6161 void *nft_set_elem_init(const struct nft_set *set,
6162 			const struct nft_set_ext_tmpl *tmpl,
6163 			const u32 *key, const u32 *key_end,
6164 			const u32 *data, u64 timeout, u64 expiration, gfp_t gfp)
6165 {
6166 	struct nft_set_ext *ext;
6167 	void *elem;
6168 
6169 	elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
6170 	if (elem == NULL)
6171 		return ERR_PTR(-ENOMEM);
6172 
6173 	ext = nft_set_elem_ext(set, elem);
6174 	nft_set_ext_init(ext, tmpl);
6175 
6176 	if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
6177 	    nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY,
6178 			       nft_set_ext_key(ext), key, set->klen) < 0)
6179 		goto err_ext_check;
6180 
6181 	if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
6182 	    nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END,
6183 			       nft_set_ext_key_end(ext), key_end, set->klen) < 0)
6184 		goto err_ext_check;
6185 
6186 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
6187 	    nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA,
6188 			       nft_set_ext_data(ext), data, set->dlen) < 0)
6189 		goto err_ext_check;
6190 
6191 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
6192 		*nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
6193 		if (expiration == 0)
6194 			*nft_set_ext_expiration(ext) += timeout;
6195 	}
6196 	if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
6197 		*nft_set_ext_timeout(ext) = timeout;
6198 
6199 	return elem;
6200 
6201 err_ext_check:
6202 	kfree(elem);
6203 
6204 	return ERR_PTR(-EINVAL);
6205 }
6206 
__nft_set_elem_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)6207 static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
6208 					struct nft_expr *expr)
6209 {
6210 	if (expr->ops->destroy_clone) {
6211 		expr->ops->destroy_clone(ctx, expr);
6212 		module_put(expr->ops->type->owner);
6213 	} else {
6214 		nf_tables_expr_destroy(ctx, expr);
6215 	}
6216 }
6217 
nft_set_elem_expr_destroy(const struct nft_ctx * ctx,struct nft_set_elem_expr * elem_expr)6218 static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
6219 				      struct nft_set_elem_expr *elem_expr)
6220 {
6221 	struct nft_expr *expr;
6222 	u32 size;
6223 
6224 	nft_setelem_expr_foreach(expr, elem_expr, size)
6225 		__nft_set_elem_expr_destroy(ctx, expr);
6226 }
6227 
6228 /* Drop references and destroy. Called from gc, dynset and abort path. */
nft_set_elem_destroy(const struct nft_set * set,void * elem,bool destroy_expr)6229 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
6230 			  bool destroy_expr)
6231 {
6232 	struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
6233 	struct nft_ctx ctx = {
6234 		.net	= read_pnet(&set->net),
6235 		.family	= set->table->family,
6236 	};
6237 
6238 	nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
6239 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6240 		nft_data_release(nft_set_ext_data(ext), set->dtype);
6241 	if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
6242 		nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
6243 
6244 	if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6245 		nft_use_dec(&(*nft_set_ext_obj(ext))->use);
6246 	kfree(elem);
6247 }
6248 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
6249 
6250 /* Destroy element. References have been already dropped in the preparation
6251  * path via nft_setelem_data_deactivate().
6252  */
nf_tables_set_elem_destroy(const struct nft_ctx * ctx,const struct nft_set * set,void * elem)6253 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
6254 				const struct nft_set *set, void *elem)
6255 {
6256 	struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
6257 
6258 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
6259 		nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
6260 
6261 	kfree(elem);
6262 }
6263 
nft_set_elem_expr_clone(const struct nft_ctx * ctx,struct nft_set * set,struct nft_expr * expr_array[])6264 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
6265 			    struct nft_expr *expr_array[])
6266 {
6267 	struct nft_expr *expr;
6268 	int err, i, k;
6269 
6270 	for (i = 0; i < set->num_exprs; i++) {
6271 		expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT);
6272 		if (!expr)
6273 			goto err_expr;
6274 
6275 		err = nft_expr_clone(expr, set->exprs[i]);
6276 		if (err < 0) {
6277 			kfree(expr);
6278 			goto err_expr;
6279 		}
6280 		expr_array[i] = expr;
6281 	}
6282 
6283 	return 0;
6284 
6285 err_expr:
6286 	for (k = i - 1; k >= 0; k--)
6287 		nft_expr_destroy(ctx, expr_array[k]);
6288 
6289 	return -ENOMEM;
6290 }
6291 
nft_set_elem_expr_setup(struct nft_ctx * ctx,const struct nft_set_ext_tmpl * tmpl,const struct nft_set_ext * ext,struct nft_expr * expr_array[],u32 num_exprs)6292 static int nft_set_elem_expr_setup(struct nft_ctx *ctx,
6293 				   const struct nft_set_ext_tmpl *tmpl,
6294 				   const struct nft_set_ext *ext,
6295 				   struct nft_expr *expr_array[],
6296 				   u32 num_exprs)
6297 {
6298 	struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
6299 	u32 len = sizeof(struct nft_set_elem_expr);
6300 	struct nft_expr *expr;
6301 	int i, err;
6302 
6303 	if (num_exprs == 0)
6304 		return 0;
6305 
6306 	for (i = 0; i < num_exprs; i++)
6307 		len += expr_array[i]->ops->size;
6308 
6309 	if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0)
6310 		return -EINVAL;
6311 
6312 	for (i = 0; i < num_exprs; i++) {
6313 		expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
6314 		err = nft_expr_clone(expr, expr_array[i]);
6315 		if (err < 0)
6316 			goto err_elem_expr_setup;
6317 
6318 		elem_expr->size += expr_array[i]->ops->size;
6319 		nft_expr_destroy(ctx, expr_array[i]);
6320 		expr_array[i] = NULL;
6321 	}
6322 
6323 	return 0;
6324 
6325 err_elem_expr_setup:
6326 	for (; i < num_exprs; i++) {
6327 		nft_expr_destroy(ctx, expr_array[i]);
6328 		expr_array[i] = NULL;
6329 	}
6330 
6331 	return -ENOMEM;
6332 }
6333 
nft_set_catchall_lookup(const struct net * net,const struct nft_set * set)6334 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
6335 					    const struct nft_set *set)
6336 {
6337 	struct nft_set_elem_catchall *catchall;
6338 	u8 genmask = nft_genmask_cur(net);
6339 	struct nft_set_ext *ext;
6340 
6341 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6342 		ext = nft_set_elem_ext(set, catchall->elem);
6343 		if (nft_set_elem_active(ext, genmask) &&
6344 		    !nft_set_elem_expired(ext) &&
6345 		    !nft_set_elem_is_dead(ext))
6346 			return ext;
6347 	}
6348 
6349 	return NULL;
6350 }
6351 EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);
6352 
nft_setelem_catchall_insert(const struct net * net,struct nft_set * set,const struct nft_set_elem * elem,struct nft_set_ext ** pext)6353 static int nft_setelem_catchall_insert(const struct net *net,
6354 				       struct nft_set *set,
6355 				       const struct nft_set_elem *elem,
6356 				       struct nft_set_ext **pext)
6357 {
6358 	struct nft_set_elem_catchall *catchall;
6359 	u8 genmask = nft_genmask_next(net);
6360 	struct nft_set_ext *ext;
6361 
6362 	list_for_each_entry(catchall, &set->catchall_list, list) {
6363 		ext = nft_set_elem_ext(set, catchall->elem);
6364 		if (nft_set_elem_active(ext, genmask)) {
6365 			*pext = ext;
6366 			return -EEXIST;
6367 		}
6368 	}
6369 
6370 	catchall = kmalloc(sizeof(*catchall), GFP_KERNEL);
6371 	if (!catchall)
6372 		return -ENOMEM;
6373 
6374 	catchall->elem = elem->priv;
6375 	list_add_tail_rcu(&catchall->list, &set->catchall_list);
6376 
6377 	return 0;
6378 }
6379 
nft_setelem_insert(const struct net * net,struct nft_set * set,const struct nft_set_elem * elem,struct nft_set_ext ** ext,unsigned int flags)6380 static int nft_setelem_insert(const struct net *net,
6381 			      struct nft_set *set,
6382 			      const struct nft_set_elem *elem,
6383 			      struct nft_set_ext **ext, unsigned int flags)
6384 {
6385 	int ret;
6386 
6387 	if (flags & NFT_SET_ELEM_CATCHALL)
6388 		ret = nft_setelem_catchall_insert(net, set, elem, ext);
6389 	else
6390 		ret = set->ops->insert(net, set, elem, ext);
6391 
6392 	return ret;
6393 }
6394 
nft_setelem_is_catchall(const struct nft_set * set,const struct nft_set_elem * elem)6395 static bool nft_setelem_is_catchall(const struct nft_set *set,
6396 				    const struct nft_set_elem *elem)
6397 {
6398 	struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6399 
6400 	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
6401 	    *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL)
6402 		return true;
6403 
6404 	return false;
6405 }
6406 
nft_setelem_activate(struct net * net,struct nft_set * set,struct nft_set_elem * elem)6407 static void nft_setelem_activate(struct net *net, struct nft_set *set,
6408 				 struct nft_set_elem *elem)
6409 {
6410 	struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6411 
6412 	if (nft_setelem_is_catchall(set, elem)) {
6413 		nft_set_elem_change_active(net, set, ext);
6414 	} else {
6415 		set->ops->activate(net, set, elem);
6416 	}
6417 }
6418 
nft_setelem_catchall_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)6419 static int nft_setelem_catchall_deactivate(const struct net *net,
6420 					   struct nft_set *set,
6421 					   struct nft_set_elem *elem)
6422 {
6423 	struct nft_set_elem_catchall *catchall;
6424 	struct nft_set_ext *ext;
6425 
6426 	list_for_each_entry(catchall, &set->catchall_list, list) {
6427 		ext = nft_set_elem_ext(set, catchall->elem);
6428 		if (!nft_is_active(net, ext))
6429 			continue;
6430 
6431 		kfree(elem->priv);
6432 		elem->priv = catchall->elem;
6433 		nft_set_elem_change_active(net, set, ext);
6434 		return 0;
6435 	}
6436 
6437 	return -ENOENT;
6438 }
6439 
__nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)6440 static int __nft_setelem_deactivate(const struct net *net,
6441 				    struct nft_set *set,
6442 				    struct nft_set_elem *elem)
6443 {
6444 	void *priv;
6445 
6446 	priv = set->ops->deactivate(net, set, elem);
6447 	if (!priv)
6448 		return -ENOENT;
6449 
6450 	kfree(elem->priv);
6451 	elem->priv = priv;
6452 	set->ndeact++;
6453 
6454 	return 0;
6455 }
6456 
nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem,u32 flags)6457 static int nft_setelem_deactivate(const struct net *net,
6458 				  struct nft_set *set,
6459 				  struct nft_set_elem *elem, u32 flags)
6460 {
6461 	int ret;
6462 
6463 	if (flags & NFT_SET_ELEM_CATCHALL)
6464 		ret = nft_setelem_catchall_deactivate(net, set, elem);
6465 	else
6466 		ret = __nft_setelem_deactivate(net, set, elem);
6467 
6468 	return ret;
6469 }
6470 
nft_setelem_catchall_remove(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)6471 static void nft_setelem_catchall_remove(const struct net *net,
6472 					const struct nft_set *set,
6473 					const struct nft_set_elem *elem)
6474 {
6475 	struct nft_set_elem_catchall *catchall, *next;
6476 
6477 	list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
6478 		if (catchall->elem == elem->priv) {
6479 			list_del_rcu(&catchall->list);
6480 			kfree_rcu(catchall, rcu);
6481 			break;
6482 		}
6483 	}
6484 }
6485 
nft_setelem_remove(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)6486 static void nft_setelem_remove(const struct net *net,
6487 			       const struct nft_set *set,
6488 			       const struct nft_set_elem *elem)
6489 {
6490 	if (nft_setelem_is_catchall(set, elem))
6491 		nft_setelem_catchall_remove(net, set, elem);
6492 	else
6493 		set->ops->remove(net, set, elem);
6494 }
6495 
nft_setelem_valid_key_end(const struct nft_set * set,struct nlattr ** nla,u32 flags)6496 static bool nft_setelem_valid_key_end(const struct nft_set *set,
6497 				      struct nlattr **nla, u32 flags)
6498 {
6499 	if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) ==
6500 			  (NFT_SET_CONCAT | NFT_SET_INTERVAL)) {
6501 		if (flags & NFT_SET_ELEM_INTERVAL_END)
6502 			return false;
6503 
6504 		if (nla[NFTA_SET_ELEM_KEY_END] &&
6505 		    flags & NFT_SET_ELEM_CATCHALL)
6506 			return false;
6507 	} else {
6508 		if (nla[NFTA_SET_ELEM_KEY_END])
6509 			return false;
6510 	}
6511 
6512 	return true;
6513 }
6514 
nft_add_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr,u32 nlmsg_flags)6515 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
6516 			    const struct nlattr *attr, u32 nlmsg_flags)
6517 {
6518 	struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
6519 	struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6520 	u8 genmask = nft_genmask_next(ctx->net);
6521 	u32 flags = 0, size = 0, num_exprs = 0;
6522 	struct nft_set_ext_tmpl tmpl;
6523 	struct nft_set_ext *ext, *ext2;
6524 	struct nft_set_elem elem;
6525 	struct nft_set_binding *binding;
6526 	struct nft_object *obj = NULL;
6527 	struct nft_userdata *udata;
6528 	struct nft_data_desc desc;
6529 	enum nft_registers dreg;
6530 	struct nft_trans *trans;
6531 	u64 timeout;
6532 	u64 expiration;
6533 	int err, i;
6534 	u8 ulen;
6535 
6536 	err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6537 					  nft_set_elem_policy, NULL);
6538 	if (err < 0)
6539 		return err;
6540 
6541 	nft_set_ext_prepare(&tmpl);
6542 
6543 	err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6544 	if (err < 0)
6545 		return err;
6546 
6547 	if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) ||
6548 	    (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY]))
6549 		return -EINVAL;
6550 
6551 	if (flags != 0) {
6552 		err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
6553 		if (err < 0)
6554 			return err;
6555 	}
6556 
6557 	if (set->flags & NFT_SET_MAP) {
6558 		if (nla[NFTA_SET_ELEM_DATA] == NULL &&
6559 		    !(flags & NFT_SET_ELEM_INTERVAL_END))
6560 			return -EINVAL;
6561 	} else {
6562 		if (nla[NFTA_SET_ELEM_DATA] != NULL)
6563 			return -EINVAL;
6564 	}
6565 
6566 	if (set->flags & NFT_SET_OBJECT) {
6567 		if (!nla[NFTA_SET_ELEM_OBJREF] &&
6568 		    !(flags & NFT_SET_ELEM_INTERVAL_END))
6569 			return -EINVAL;
6570 	} else {
6571 		if (nla[NFTA_SET_ELEM_OBJREF])
6572 			return -EINVAL;
6573 	}
6574 
6575 	if (!nft_setelem_valid_key_end(set, nla, flags))
6576 		return -EINVAL;
6577 
6578 	if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
6579 	     (nla[NFTA_SET_ELEM_DATA] ||
6580 	      nla[NFTA_SET_ELEM_OBJREF] ||
6581 	      nla[NFTA_SET_ELEM_TIMEOUT] ||
6582 	      nla[NFTA_SET_ELEM_EXPIRATION] ||
6583 	      nla[NFTA_SET_ELEM_USERDATA] ||
6584 	      nla[NFTA_SET_ELEM_EXPR] ||
6585 	      nla[NFTA_SET_ELEM_KEY_END] ||
6586 	      nla[NFTA_SET_ELEM_EXPRESSIONS]))
6587 		return -EINVAL;
6588 
6589 	timeout = 0;
6590 	if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
6591 		if (!(set->flags & NFT_SET_TIMEOUT))
6592 			return -EINVAL;
6593 		err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
6594 					    &timeout);
6595 		if (err)
6596 			return err;
6597 	} else if (set->flags & NFT_SET_TIMEOUT &&
6598 		   !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6599 		timeout = READ_ONCE(set->timeout);
6600 	}
6601 
6602 	expiration = 0;
6603 	if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
6604 		if (!(set->flags & NFT_SET_TIMEOUT))
6605 			return -EINVAL;
6606 		err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
6607 					    &expiration);
6608 		if (err)
6609 			return err;
6610 	}
6611 
6612 	if (nla[NFTA_SET_ELEM_EXPR]) {
6613 		struct nft_expr *expr;
6614 
6615 		if (set->num_exprs && set->num_exprs != 1)
6616 			return -EOPNOTSUPP;
6617 
6618 		expr = nft_set_elem_expr_alloc(ctx, set,
6619 					       nla[NFTA_SET_ELEM_EXPR]);
6620 		if (IS_ERR(expr))
6621 			return PTR_ERR(expr);
6622 
6623 		expr_array[0] = expr;
6624 		num_exprs = 1;
6625 
6626 		if (set->num_exprs && set->exprs[0]->ops != expr->ops) {
6627 			err = -EOPNOTSUPP;
6628 			goto err_set_elem_expr;
6629 		}
6630 	} else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
6631 		struct nft_expr *expr;
6632 		struct nlattr *tmp;
6633 		int left;
6634 
6635 		i = 0;
6636 		nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
6637 			if (i == NFT_SET_EXPR_MAX ||
6638 			    (set->num_exprs && set->num_exprs == i)) {
6639 				err = -E2BIG;
6640 				goto err_set_elem_expr;
6641 			}
6642 			if (nla_type(tmp) != NFTA_LIST_ELEM) {
6643 				err = -EINVAL;
6644 				goto err_set_elem_expr;
6645 			}
6646 			expr = nft_set_elem_expr_alloc(ctx, set, tmp);
6647 			if (IS_ERR(expr)) {
6648 				err = PTR_ERR(expr);
6649 				goto err_set_elem_expr;
6650 			}
6651 			expr_array[i] = expr;
6652 			num_exprs++;
6653 
6654 			if (set->num_exprs && expr->ops != set->exprs[i]->ops) {
6655 				err = -EOPNOTSUPP;
6656 				goto err_set_elem_expr;
6657 			}
6658 			i++;
6659 		}
6660 		if (set->num_exprs && set->num_exprs != i) {
6661 			err = -EOPNOTSUPP;
6662 			goto err_set_elem_expr;
6663 		}
6664 	} else if (set->num_exprs > 0 &&
6665 		   !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6666 		err = nft_set_elem_expr_clone(ctx, set, expr_array);
6667 		if (err < 0)
6668 			goto err_set_elem_expr_clone;
6669 
6670 		num_exprs = set->num_exprs;
6671 	}
6672 
6673 	if (nla[NFTA_SET_ELEM_KEY]) {
6674 		err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6675 					    nla[NFTA_SET_ELEM_KEY]);
6676 		if (err < 0)
6677 			goto err_set_elem_expr;
6678 
6679 		err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6680 		if (err < 0)
6681 			goto err_parse_key;
6682 	}
6683 
6684 	if (nla[NFTA_SET_ELEM_KEY_END]) {
6685 		err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6686 					    nla[NFTA_SET_ELEM_KEY_END]);
6687 		if (err < 0)
6688 			goto err_parse_key;
6689 
6690 		err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6691 		if (err < 0)
6692 			goto err_parse_key_end;
6693 	}
6694 
6695 	if (timeout > 0) {
6696 		err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
6697 		if (err < 0)
6698 			goto err_parse_key_end;
6699 
6700 		if (timeout != READ_ONCE(set->timeout)) {
6701 			err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
6702 			if (err < 0)
6703 				goto err_parse_key_end;
6704 		}
6705 	}
6706 
6707 	if (num_exprs) {
6708 		for (i = 0; i < num_exprs; i++)
6709 			size += expr_array[i]->ops->size;
6710 
6711 		err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
6712 					     sizeof(struct nft_set_elem_expr) + size);
6713 		if (err < 0)
6714 			goto err_parse_key_end;
6715 	}
6716 
6717 	if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
6718 		obj = nft_obj_lookup(ctx->net, ctx->table,
6719 				     nla[NFTA_SET_ELEM_OBJREF],
6720 				     set->objtype, genmask);
6721 		if (IS_ERR(obj)) {
6722 			err = PTR_ERR(obj);
6723 			obj = NULL;
6724 			goto err_parse_key_end;
6725 		}
6726 
6727 		if (!nft_use_inc(&obj->use)) {
6728 			err = -EMFILE;
6729 			obj = NULL;
6730 			goto err_parse_key_end;
6731 		}
6732 
6733 		err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
6734 		if (err < 0)
6735 			goto err_parse_key_end;
6736 	}
6737 
6738 	if (nla[NFTA_SET_ELEM_DATA] != NULL) {
6739 		err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
6740 					     nla[NFTA_SET_ELEM_DATA]);
6741 		if (err < 0)
6742 			goto err_parse_key_end;
6743 
6744 		dreg = nft_type_to_reg(set->dtype);
6745 		list_for_each_entry(binding, &set->bindings, list) {
6746 			struct nft_ctx bind_ctx = {
6747 				.net	= ctx->net,
6748 				.family	= ctx->family,
6749 				.table	= ctx->table,
6750 				.chain	= (struct nft_chain *)binding->chain,
6751 			};
6752 
6753 			if (!(binding->flags & NFT_SET_MAP))
6754 				continue;
6755 
6756 			err = nft_validate_register_store(&bind_ctx, dreg,
6757 							  &elem.data.val,
6758 							  desc.type, desc.len);
6759 			if (err < 0)
6760 				goto err_parse_data;
6761 
6762 			if (desc.type == NFT_DATA_VERDICT &&
6763 			    (elem.data.val.verdict.code == NFT_GOTO ||
6764 			     elem.data.val.verdict.code == NFT_JUMP))
6765 				nft_validate_state_update(ctx->table,
6766 							  NFT_VALIDATE_NEED);
6767 		}
6768 
6769 		err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
6770 		if (err < 0)
6771 			goto err_parse_data;
6772 	}
6773 
6774 	/* The full maximum length of userdata can exceed the maximum
6775 	 * offset value (U8_MAX) for following extensions, therefor it
6776 	 * must be the last extension added.
6777 	 */
6778 	ulen = 0;
6779 	if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
6780 		ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
6781 		if (ulen > 0) {
6782 			err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
6783 						     ulen);
6784 			if (err < 0)
6785 				goto err_parse_data;
6786 		}
6787 	}
6788 
6789 	elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
6790 				      elem.key_end.val.data, elem.data.val.data,
6791 				      timeout, expiration, GFP_KERNEL_ACCOUNT);
6792 	if (IS_ERR(elem.priv)) {
6793 		err = PTR_ERR(elem.priv);
6794 		goto err_parse_data;
6795 	}
6796 
6797 	ext = nft_set_elem_ext(set, elem.priv);
6798 	if (flags)
6799 		*nft_set_ext_flags(ext) = flags;
6800 
6801 	if (obj)
6802 		*nft_set_ext_obj(ext) = obj;
6803 
6804 	if (ulen > 0) {
6805 		if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) {
6806 			err = -EINVAL;
6807 			goto err_elem_free;
6808 		}
6809 		udata = nft_set_ext_userdata(ext);
6810 		udata->len = ulen - 1;
6811 		nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
6812 	}
6813 	err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs);
6814 	if (err < 0)
6815 		goto err_elem_free;
6816 
6817 	trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
6818 	if (trans == NULL) {
6819 		err = -ENOMEM;
6820 		goto err_elem_free;
6821 	}
6822 
6823 	ext->genmask = nft_genmask_cur(ctx->net);
6824 
6825 	err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags);
6826 	if (err) {
6827 		if (err == -EEXIST) {
6828 			if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
6829 			    nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
6830 			    nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
6831 			    nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
6832 				goto err_element_clash;
6833 			if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
6834 			     nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
6835 			     memcmp(nft_set_ext_data(ext),
6836 				    nft_set_ext_data(ext2), set->dlen) != 0) ||
6837 			    (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
6838 			     nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
6839 			     *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
6840 				goto err_element_clash;
6841 			else if (!(nlmsg_flags & NLM_F_EXCL))
6842 				err = 0;
6843 		} else if (err == -ENOTEMPTY) {
6844 			/* ENOTEMPTY reports overlapping between this element
6845 			 * and an existing one.
6846 			 */
6847 			err = -EEXIST;
6848 		}
6849 		goto err_element_clash;
6850 	}
6851 
6852 	if (!(flags & NFT_SET_ELEM_CATCHALL)) {
6853 		unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX;
6854 
6855 		if (!atomic_add_unless(&set->nelems, 1, max)) {
6856 			err = -ENFILE;
6857 			goto err_set_full;
6858 		}
6859 	}
6860 
6861 	nft_trans_elem(trans) = elem;
6862 	nft_trans_commit_list_add_tail(ctx->net, trans);
6863 	return 0;
6864 
6865 err_set_full:
6866 	nft_setelem_remove(ctx->net, set, &elem);
6867 err_element_clash:
6868 	kfree(trans);
6869 err_elem_free:
6870 	nf_tables_set_elem_destroy(ctx, set, elem.priv);
6871 err_parse_data:
6872 	if (nla[NFTA_SET_ELEM_DATA] != NULL)
6873 		nft_data_release(&elem.data.val, desc.type);
6874 err_parse_key_end:
6875 	if (obj)
6876 		nft_use_dec_restore(&obj->use);
6877 
6878 	nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
6879 err_parse_key:
6880 	nft_data_release(&elem.key.val, NFT_DATA_VALUE);
6881 err_set_elem_expr:
6882 	for (i = 0; i < num_exprs && expr_array[i]; i++)
6883 		nft_expr_destroy(ctx, expr_array[i]);
6884 err_set_elem_expr_clone:
6885 	return err;
6886 }
6887 
nf_tables_newsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])6888 static int nf_tables_newsetelem(struct sk_buff *skb,
6889 				const struct nfnl_info *info,
6890 				const struct nlattr * const nla[])
6891 {
6892 	struct netlink_ext_ack *extack = info->extack;
6893 	u8 genmask = nft_genmask_next(info->net);
6894 	u8 family = info->nfmsg->nfgen_family;
6895 	struct net *net = info->net;
6896 	const struct nlattr *attr;
6897 	struct nft_table *table;
6898 	struct nft_set *set;
6899 	struct nft_ctx ctx;
6900 	int rem, err;
6901 
6902 	if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
6903 		return -EINVAL;
6904 
6905 	table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6906 				 genmask, NETLINK_CB(skb).portid);
6907 	if (IS_ERR(table)) {
6908 		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6909 		return PTR_ERR(table);
6910 	}
6911 
6912 	set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET],
6913 				    nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
6914 	if (IS_ERR(set))
6915 		return PTR_ERR(set);
6916 
6917 	if (!list_empty(&set->bindings) &&
6918 	    (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS)))
6919 		return -EBUSY;
6920 
6921 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6922 
6923 	nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6924 		err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags);
6925 		if (err < 0) {
6926 			NL_SET_BAD_ATTR(extack, attr);
6927 			return err;
6928 		}
6929 	}
6930 
6931 	if (table->validate_state == NFT_VALIDATE_DO)
6932 		return nft_table_validate(net, table);
6933 
6934 	return 0;
6935 }
6936 
6937 /**
6938  *	nft_data_hold - hold a nft_data item
6939  *
6940  *	@data: struct nft_data to release
6941  *	@type: type of data
6942  *
6943  *	Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
6944  *	NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
6945  *	NFT_GOTO verdicts. This function must be called on active data objects
6946  *	from the second phase of the commit protocol.
6947  */
nft_data_hold(const struct nft_data * data,enum nft_data_types type)6948 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
6949 {
6950 	struct nft_chain *chain;
6951 
6952 	if (type == NFT_DATA_VERDICT) {
6953 		switch (data->verdict.code) {
6954 		case NFT_JUMP:
6955 		case NFT_GOTO:
6956 			chain = data->verdict.chain;
6957 			nft_use_inc_restore(&chain->use);
6958 			break;
6959 		}
6960 	}
6961 }
6962 
nft_setelem_data_activate(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)6963 static void nft_setelem_data_activate(const struct net *net,
6964 				      const struct nft_set *set,
6965 				      struct nft_set_elem *elem)
6966 {
6967 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6968 
6969 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6970 		nft_data_hold(nft_set_ext_data(ext), set->dtype);
6971 	if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6972 		nft_use_inc_restore(&(*nft_set_ext_obj(ext))->use);
6973 }
6974 
nft_setelem_data_deactivate(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)6975 void nft_setelem_data_deactivate(const struct net *net,
6976 				 const struct nft_set *set,
6977 				 struct nft_set_elem *elem)
6978 {
6979 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6980 
6981 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6982 		nft_data_release(nft_set_ext_data(ext), set->dtype);
6983 	if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6984 		nft_use_dec(&(*nft_set_ext_obj(ext))->use);
6985 }
6986 
nft_del_setelem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr)6987 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
6988 			   const struct nlattr *attr)
6989 {
6990 	struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6991 	struct nft_set_ext_tmpl tmpl;
6992 	struct nft_set_elem elem;
6993 	struct nft_set_ext *ext;
6994 	struct nft_trans *trans;
6995 	u32 flags = 0;
6996 	int err;
6997 
6998 	err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6999 					  nft_set_elem_policy, NULL);
7000 	if (err < 0)
7001 		return err;
7002 
7003 	err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
7004 	if (err < 0)
7005 		return err;
7006 
7007 	if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
7008 		return -EINVAL;
7009 
7010 	if (!nft_setelem_valid_key_end(set, nla, flags))
7011 		return -EINVAL;
7012 
7013 	nft_set_ext_prepare(&tmpl);
7014 
7015 	if (flags != 0) {
7016 		err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
7017 		if (err < 0)
7018 			return err;
7019 	}
7020 
7021 	if (nla[NFTA_SET_ELEM_KEY]) {
7022 		err = nft_setelem_parse_key(ctx, set, &elem.key.val,
7023 					    nla[NFTA_SET_ELEM_KEY]);
7024 		if (err < 0)
7025 			return err;
7026 
7027 		err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7028 		if (err < 0)
7029 			goto fail_elem;
7030 	}
7031 
7032 	if (nla[NFTA_SET_ELEM_KEY_END]) {
7033 		err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
7034 					    nla[NFTA_SET_ELEM_KEY_END]);
7035 		if (err < 0)
7036 			goto fail_elem;
7037 
7038 		err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
7039 		if (err < 0)
7040 			goto fail_elem_key_end;
7041 	}
7042 
7043 	err = -ENOMEM;
7044 	elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
7045 				      elem.key_end.val.data, NULL, 0, 0,
7046 				      GFP_KERNEL_ACCOUNT);
7047 	if (IS_ERR(elem.priv)) {
7048 		err = PTR_ERR(elem.priv);
7049 		goto fail_elem_key_end;
7050 	}
7051 
7052 	ext = nft_set_elem_ext(set, elem.priv);
7053 	if (flags)
7054 		*nft_set_ext_flags(ext) = flags;
7055 
7056 	trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
7057 	if (trans == NULL)
7058 		goto fail_trans;
7059 
7060 	err = nft_setelem_deactivate(ctx->net, set, &elem, flags);
7061 	if (err < 0)
7062 		goto fail_ops;
7063 
7064 	nft_setelem_data_deactivate(ctx->net, set, &elem);
7065 
7066 	nft_trans_elem(trans) = elem;
7067 	nft_trans_commit_list_add_tail(ctx->net, trans);
7068 	return 0;
7069 
7070 fail_ops:
7071 	kfree(trans);
7072 fail_trans:
7073 	kfree(elem.priv);
7074 fail_elem_key_end:
7075 	nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
7076 fail_elem:
7077 	nft_data_release(&elem.key.val, NFT_DATA_VALUE);
7078 	return err;
7079 }
7080 
nft_setelem_flush(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)7081 static int nft_setelem_flush(const struct nft_ctx *ctx,
7082 			     struct nft_set *set,
7083 			     const struct nft_set_iter *iter,
7084 			     struct nft_set_elem *elem)
7085 {
7086 	struct nft_trans *trans;
7087 	int err;
7088 
7089 	trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
7090 				    sizeof(struct nft_trans_elem), GFP_ATOMIC);
7091 	if (!trans)
7092 		return -ENOMEM;
7093 
7094 	if (!set->ops->flush(ctx->net, set, elem->priv)) {
7095 		err = -ENOENT;
7096 		goto err1;
7097 	}
7098 	set->ndeact++;
7099 
7100 	nft_setelem_data_deactivate(ctx->net, set, elem);
7101 	nft_trans_elem_set(trans) = set;
7102 	nft_trans_elem(trans) = *elem;
7103 	nft_trans_commit_list_add_tail(ctx->net, trans);
7104 
7105 	return 0;
7106 err1:
7107 	kfree(trans);
7108 	return err;
7109 }
7110 
__nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem)7111 static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
7112 				    struct nft_set *set,
7113 				    struct nft_set_elem *elem)
7114 {
7115 	struct nft_trans *trans;
7116 
7117 	trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
7118 				    sizeof(struct nft_trans_elem), GFP_KERNEL);
7119 	if (!trans)
7120 		return -ENOMEM;
7121 
7122 	nft_setelem_data_deactivate(ctx->net, set, elem);
7123 	nft_trans_elem_set(trans) = set;
7124 	nft_trans_elem(trans) = *elem;
7125 	nft_trans_commit_list_add_tail(ctx->net, trans);
7126 
7127 	return 0;
7128 }
7129 
nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set)7130 static int nft_set_catchall_flush(const struct nft_ctx *ctx,
7131 				  struct nft_set *set)
7132 {
7133 	u8 genmask = nft_genmask_next(ctx->net);
7134 	struct nft_set_elem_catchall *catchall;
7135 	struct nft_set_elem elem;
7136 	struct nft_set_ext *ext;
7137 	int ret = 0;
7138 
7139 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
7140 		ext = nft_set_elem_ext(set, catchall->elem);
7141 		if (!nft_set_elem_active(ext, genmask))
7142 			continue;
7143 
7144 		elem.priv = catchall->elem;
7145 		ret = __nft_set_catchall_flush(ctx, set, &elem);
7146 		if (ret < 0)
7147 			break;
7148 		nft_set_elem_change_active(ctx->net, set, ext);
7149 	}
7150 
7151 	return ret;
7152 }
7153 
nft_set_flush(struct nft_ctx * ctx,struct nft_set * set,u8 genmask)7154 static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask)
7155 {
7156 	struct nft_set_iter iter = {
7157 		.genmask	= genmask,
7158 		.fn		= nft_setelem_flush,
7159 	};
7160 
7161 	set->ops->walk(ctx, set, &iter);
7162 	if (!iter.err)
7163 		iter.err = nft_set_catchall_flush(ctx, set);
7164 
7165 	return iter.err;
7166 }
7167 
nf_tables_delsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7168 static int nf_tables_delsetelem(struct sk_buff *skb,
7169 				const struct nfnl_info *info,
7170 				const struct nlattr * const nla[])
7171 {
7172 	struct netlink_ext_ack *extack = info->extack;
7173 	u8 genmask = nft_genmask_next(info->net);
7174 	u8 family = info->nfmsg->nfgen_family;
7175 	struct net *net = info->net;
7176 	const struct nlattr *attr;
7177 	struct nft_table *table;
7178 	struct nft_set *set;
7179 	struct nft_ctx ctx;
7180 	int rem, err = 0;
7181 
7182 	table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
7183 				 genmask, NETLINK_CB(skb).portid);
7184 	if (IS_ERR(table)) {
7185 		NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
7186 		return PTR_ERR(table);
7187 	}
7188 
7189 	set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
7190 	if (IS_ERR(set))
7191 		return PTR_ERR(set);
7192 
7193 	if (nft_set_is_anonymous(set))
7194 		return -EOPNOTSUPP;
7195 
7196 	if (!list_empty(&set->bindings) && (set->flags & NFT_SET_CONSTANT))
7197 		return -EBUSY;
7198 
7199 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7200 
7201 	if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
7202 		return nft_set_flush(&ctx, set, genmask);
7203 
7204 	nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
7205 		err = nft_del_setelem(&ctx, set, attr);
7206 		if (err == -ENOENT &&
7207 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM)
7208 			continue;
7209 
7210 		if (err < 0) {
7211 			NL_SET_BAD_ATTR(extack, attr);
7212 			break;
7213 		}
7214 	}
7215 	return err;
7216 }
7217 
7218 /*
7219  * Stateful objects
7220  */
7221 
7222 /**
7223  *	nft_register_obj- register nf_tables stateful object type
7224  *	@obj_type: object type
7225  *
7226  *	Registers the object type for use with nf_tables. Returns zero on
7227  *	success or a negative errno code otherwise.
7228  */
nft_register_obj(struct nft_object_type * obj_type)7229 int nft_register_obj(struct nft_object_type *obj_type)
7230 {
7231 	if (obj_type->type == NFT_OBJECT_UNSPEC)
7232 		return -EINVAL;
7233 
7234 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
7235 	list_add_rcu(&obj_type->list, &nf_tables_objects);
7236 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7237 	return 0;
7238 }
7239 EXPORT_SYMBOL_GPL(nft_register_obj);
7240 
7241 /**
7242  *	nft_unregister_obj - unregister nf_tables object type
7243  *	@obj_type: object type
7244  *
7245  * 	Unregisters the object type for use with nf_tables.
7246  */
nft_unregister_obj(struct nft_object_type * obj_type)7247 void nft_unregister_obj(struct nft_object_type *obj_type)
7248 {
7249 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
7250 	list_del_rcu(&obj_type->list);
7251 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7252 }
7253 EXPORT_SYMBOL_GPL(nft_unregister_obj);
7254 
nft_obj_lookup(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)7255 struct nft_object *nft_obj_lookup(const struct net *net,
7256 				  const struct nft_table *table,
7257 				  const struct nlattr *nla, u32 objtype,
7258 				  u8 genmask)
7259 {
7260 	struct nft_object_hash_key k = { .table = table };
7261 	char search[NFT_OBJ_MAXNAMELEN];
7262 	struct rhlist_head *tmp, *list;
7263 	struct nft_object *obj;
7264 
7265 	nla_strscpy(search, nla, sizeof(search));
7266 	k.name = search;
7267 
7268 	WARN_ON_ONCE(!rcu_read_lock_held() &&
7269 		     !lockdep_commit_lock_is_held(net));
7270 
7271 	rcu_read_lock();
7272 	list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
7273 	if (!list)
7274 		goto out;
7275 
7276 	rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
7277 		if (objtype == obj->ops->type->type &&
7278 		    nft_active_genmask(obj, genmask)) {
7279 			rcu_read_unlock();
7280 			return obj;
7281 		}
7282 	}
7283 out:
7284 	rcu_read_unlock();
7285 	return ERR_PTR(-ENOENT);
7286 }
7287 EXPORT_SYMBOL_GPL(nft_obj_lookup);
7288 
nft_obj_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)7289 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
7290 						  const struct nlattr *nla,
7291 						  u32 objtype, u8 genmask)
7292 {
7293 	struct nft_object *obj;
7294 
7295 	list_for_each_entry(obj, &table->objects, list) {
7296 		if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
7297 		    objtype == obj->ops->type->type &&
7298 		    nft_active_genmask(obj, genmask))
7299 			return obj;
7300 	}
7301 	return ERR_PTR(-ENOENT);
7302 }
7303 
7304 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
7305 	[NFTA_OBJ_TABLE]	= { .type = NLA_STRING,
7306 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
7307 	[NFTA_OBJ_NAME]		= { .type = NLA_STRING,
7308 				    .len = NFT_OBJ_MAXNAMELEN - 1 },
7309 	[NFTA_OBJ_TYPE]		= { .type = NLA_U32 },
7310 	[NFTA_OBJ_DATA]		= { .type = NLA_NESTED },
7311 	[NFTA_OBJ_HANDLE]	= { .type = NLA_U64},
7312 	[NFTA_OBJ_USERDATA]	= { .type = NLA_BINARY,
7313 				    .len = NFT_USERDATA_MAXLEN },
7314 };
7315 
nft_obj_init(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr)7316 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
7317 				       const struct nft_object_type *type,
7318 				       const struct nlattr *attr)
7319 {
7320 	struct nlattr **tb;
7321 	const struct nft_object_ops *ops;
7322 	struct nft_object *obj;
7323 	int err = -ENOMEM;
7324 
7325 	tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
7326 	if (!tb)
7327 		goto err1;
7328 
7329 	if (attr) {
7330 		err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
7331 						  type->policy, NULL);
7332 		if (err < 0)
7333 			goto err2;
7334 	} else {
7335 		memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
7336 	}
7337 
7338 	if (type->select_ops) {
7339 		ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
7340 		if (IS_ERR(ops)) {
7341 			err = PTR_ERR(ops);
7342 			goto err2;
7343 		}
7344 	} else {
7345 		ops = type->ops;
7346 	}
7347 
7348 	err = -ENOMEM;
7349 	obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
7350 	if (!obj)
7351 		goto err2;
7352 
7353 	err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
7354 	if (err < 0)
7355 		goto err3;
7356 
7357 	obj->ops = ops;
7358 
7359 	kfree(tb);
7360 	return obj;
7361 err3:
7362 	kfree(obj);
7363 err2:
7364 	kfree(tb);
7365 err1:
7366 	return ERR_PTR(err);
7367 }
7368 
nft_object_dump(struct sk_buff * skb,unsigned int attr,struct nft_object * obj,bool reset)7369 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
7370 			   struct nft_object *obj, bool reset)
7371 {
7372 	struct nlattr *nest;
7373 
7374 	nest = nla_nest_start_noflag(skb, attr);
7375 	if (!nest)
7376 		goto nla_put_failure;
7377 	if (obj->ops->dump(skb, obj, reset) < 0)
7378 		goto nla_put_failure;
7379 	nla_nest_end(skb, nest);
7380 	return 0;
7381 
7382 nla_put_failure:
7383 	return -1;
7384 }
7385 
__nft_obj_type_get(u32 objtype)7386 static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
7387 {
7388 	const struct nft_object_type *type;
7389 
7390 	list_for_each_entry(type, &nf_tables_objects, list) {
7391 		if (objtype == type->type)
7392 			return type;
7393 	}
7394 	return NULL;
7395 }
7396 
7397 static const struct nft_object_type *
nft_obj_type_get(struct net * net,u32 objtype)7398 nft_obj_type_get(struct net *net, u32 objtype)
7399 {
7400 	const struct nft_object_type *type;
7401 
7402 	type = __nft_obj_type_get(objtype);
7403 	if (type != NULL && try_module_get(type->owner))
7404 		return type;
7405 
7406 	lockdep_nfnl_nft_mutex_not_held();
7407 #ifdef CONFIG_MODULES
7408 	if (type == NULL) {
7409 		if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
7410 			return ERR_PTR(-EAGAIN);
7411 	}
7412 #endif
7413 	return ERR_PTR(-ENOENT);
7414 }
7415 
nf_tables_updobj(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr,struct nft_object * obj)7416 static int nf_tables_updobj(const struct nft_ctx *ctx,
7417 			    const struct nft_object_type *type,
7418 			    const struct nlattr *attr,
7419 			    struct nft_object *obj)
7420 {
7421 	struct nft_object *newobj;
7422 	struct nft_trans *trans;
7423 	int err = -ENOMEM;
7424 
7425 	if (!try_module_get(type->owner))
7426 		return -ENOENT;
7427 
7428 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
7429 				sizeof(struct nft_trans_obj));
7430 	if (!trans)
7431 		goto err_trans;
7432 
7433 	newobj = nft_obj_init(ctx, type, attr);
7434 	if (IS_ERR(newobj)) {
7435 		err = PTR_ERR(newobj);
7436 		goto err_free_trans;
7437 	}
7438 
7439 	nft_trans_obj(trans) = obj;
7440 	nft_trans_obj_update(trans) = true;
7441 	nft_trans_obj_newobj(trans) = newobj;
7442 	nft_trans_commit_list_add_tail(ctx->net, trans);
7443 
7444 	return 0;
7445 
7446 err_free_trans:
7447 	kfree(trans);
7448 err_trans:
7449 	module_put(type->owner);
7450 	return err;
7451 }
7452 
nf_tables_newobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7453 static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
7454 			    const struct nlattr * const nla[])
7455 {
7456 	struct netlink_ext_ack *extack = info->extack;
7457 	u8 genmask = nft_genmask_next(info->net);
7458 	u8 family = info->nfmsg->nfgen_family;
7459 	const struct nft_object_type *type;
7460 	struct net *net = info->net;
7461 	struct nft_table *table;
7462 	struct nft_object *obj;
7463 	struct nft_ctx ctx;
7464 	u32 objtype;
7465 	int err;
7466 
7467 	if (!nla[NFTA_OBJ_TYPE] ||
7468 	    !nla[NFTA_OBJ_NAME] ||
7469 	    !nla[NFTA_OBJ_DATA])
7470 		return -EINVAL;
7471 
7472 	table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7473 				 NETLINK_CB(skb).portid);
7474 	if (IS_ERR(table)) {
7475 		NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7476 		return PTR_ERR(table);
7477 	}
7478 
7479 	objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7480 	obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7481 	if (IS_ERR(obj)) {
7482 		err = PTR_ERR(obj);
7483 		if (err != -ENOENT) {
7484 			NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7485 			return err;
7486 		}
7487 	} else {
7488 		if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
7489 			NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7490 			return -EEXIST;
7491 		}
7492 		if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
7493 			return -EOPNOTSUPP;
7494 
7495 		type = __nft_obj_type_get(objtype);
7496 		if (WARN_ON_ONCE(!type))
7497 			return -ENOENT;
7498 
7499 		nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7500 
7501 		return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
7502 	}
7503 
7504 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7505 
7506 	if (!nft_use_inc(&table->use))
7507 		return -EMFILE;
7508 
7509 	type = nft_obj_type_get(net, objtype);
7510 	if (IS_ERR(type)) {
7511 		err = PTR_ERR(type);
7512 		goto err_type;
7513 	}
7514 
7515 	obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
7516 	if (IS_ERR(obj)) {
7517 		err = PTR_ERR(obj);
7518 		goto err_init;
7519 	}
7520 	obj->key.table = table;
7521 	obj->handle = nf_tables_alloc_handle(table);
7522 
7523 	obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
7524 	if (!obj->key.name) {
7525 		err = -ENOMEM;
7526 		goto err_strdup;
7527 	}
7528 
7529 	if (nla[NFTA_OBJ_USERDATA]) {
7530 		obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT);
7531 		if (obj->udata == NULL)
7532 			goto err_userdata;
7533 
7534 		obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
7535 	}
7536 
7537 	err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
7538 	if (err < 0)
7539 		goto err_trans;
7540 
7541 	err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
7542 			      nft_objname_ht_params);
7543 	if (err < 0)
7544 		goto err_obj_ht;
7545 
7546 	list_add_tail_rcu(&obj->list, &table->objects);
7547 
7548 	return 0;
7549 err_obj_ht:
7550 	/* queued in transaction log */
7551 	INIT_LIST_HEAD(&obj->list);
7552 	return err;
7553 err_trans:
7554 	kfree(obj->udata);
7555 err_userdata:
7556 	kfree(obj->key.name);
7557 err_strdup:
7558 	if (obj->ops->destroy)
7559 		obj->ops->destroy(&ctx, obj);
7560 	kfree(obj);
7561 err_init:
7562 	module_put(type->owner);
7563 err_type:
7564 	nft_use_dec_restore(&table->use);
7565 
7566 	return err;
7567 }
7568 
nf_tables_fill_obj_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table,struct nft_object * obj,bool reset)7569 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
7570 				   u32 portid, u32 seq, int event, u32 flags,
7571 				   int family, const struct nft_table *table,
7572 				   struct nft_object *obj, bool reset)
7573 {
7574 	struct nlmsghdr *nlh;
7575 
7576 	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
7577 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
7578 			   NFNETLINK_V0, nft_base_seq(net));
7579 	if (!nlh)
7580 		goto nla_put_failure;
7581 
7582 	if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
7583 	    nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
7584 	    nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
7585 			 NFTA_OBJ_PAD))
7586 		goto nla_put_failure;
7587 
7588 	if (event == NFT_MSG_DELOBJ) {
7589 		nlmsg_end(skb, nlh);
7590 		return 0;
7591 	}
7592 
7593 	if (nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
7594 	    nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
7595 	    nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
7596 		goto nla_put_failure;
7597 
7598 	if (obj->udata &&
7599 	    nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
7600 		goto nla_put_failure;
7601 
7602 	nlmsg_end(skb, nlh);
7603 	return 0;
7604 
7605 nla_put_failure:
7606 	nlmsg_trim(skb, nlh);
7607 	return -1;
7608 }
7609 
audit_log_obj_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)7610 static void audit_log_obj_reset(const struct nft_table *table,
7611 				unsigned int base_seq, unsigned int nentries)
7612 {
7613 	char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
7614 
7615 	audit_log_nfcfg(buf, table->family, nentries,
7616 			AUDIT_NFT_OP_OBJ_RESET, GFP_ATOMIC);
7617 	kfree(buf);
7618 }
7619 
7620 struct nft_obj_filter {
7621 	char		*table;
7622 	u32		type;
7623 };
7624 
nf_tables_dump_obj(struct sk_buff * skb,struct netlink_callback * cb)7625 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
7626 {
7627 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
7628 	const struct nft_table *table;
7629 	unsigned int idx = 0, s_idx = cb->args[0];
7630 	struct nft_obj_filter *filter = cb->data;
7631 	struct net *net = sock_net(skb->sk);
7632 	int family = nfmsg->nfgen_family;
7633 	struct nftables_pernet *nft_net;
7634 	unsigned int entries = 0;
7635 	struct nft_object *obj;
7636 	bool reset = false;
7637 	int rc = 0;
7638 
7639 	if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
7640 		reset = true;
7641 
7642 	rcu_read_lock();
7643 	nft_net = nft_pernet(net);
7644 	cb->seq = READ_ONCE(nft_net->base_seq);
7645 
7646 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
7647 		if (family != NFPROTO_UNSPEC && family != table->family)
7648 			continue;
7649 
7650 		entries = 0;
7651 		list_for_each_entry_rcu(obj, &table->objects, list) {
7652 			if (!nft_is_active(net, obj))
7653 				goto cont;
7654 			if (idx < s_idx)
7655 				goto cont;
7656 			if (idx > s_idx)
7657 				memset(&cb->args[1], 0,
7658 				       sizeof(cb->args) - sizeof(cb->args[0]));
7659 			if (filter && filter->table &&
7660 			    strcmp(filter->table, table->name))
7661 				goto cont;
7662 			if (filter &&
7663 			    filter->type != NFT_OBJECT_UNSPEC &&
7664 			    obj->ops->type->type != filter->type)
7665 				goto cont;
7666 
7667 			rc = nf_tables_fill_obj_info(skb, net,
7668 						     NETLINK_CB(cb->skb).portid,
7669 						     cb->nlh->nlmsg_seq,
7670 						     NFT_MSG_NEWOBJ,
7671 						     NLM_F_MULTI | NLM_F_APPEND,
7672 						     table->family, table,
7673 						     obj, reset);
7674 			if (rc < 0)
7675 				break;
7676 
7677 			entries++;
7678 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7679 cont:
7680 			idx++;
7681 		}
7682 		if (reset && entries)
7683 			audit_log_obj_reset(table, nft_net->base_seq, entries);
7684 		if (rc < 0)
7685 			break;
7686 	}
7687 	rcu_read_unlock();
7688 
7689 	cb->args[0] = idx;
7690 	return skb->len;
7691 }
7692 
nf_tables_dump_obj_start(struct netlink_callback * cb)7693 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
7694 {
7695 	const struct nlattr * const *nla = cb->data;
7696 	struct nft_obj_filter *filter = NULL;
7697 
7698 	if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
7699 		filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
7700 		if (!filter)
7701 			return -ENOMEM;
7702 
7703 		if (nla[NFTA_OBJ_TABLE]) {
7704 			filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
7705 			if (!filter->table) {
7706 				kfree(filter);
7707 				return -ENOMEM;
7708 			}
7709 		}
7710 
7711 		if (nla[NFTA_OBJ_TYPE])
7712 			filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7713 	}
7714 
7715 	cb->data = filter;
7716 	return 0;
7717 }
7718 
nf_tables_dump_obj_done(struct netlink_callback * cb)7719 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
7720 {
7721 	struct nft_obj_filter *filter = cb->data;
7722 
7723 	if (filter) {
7724 		kfree(filter->table);
7725 		kfree(filter);
7726 	}
7727 
7728 	return 0;
7729 }
7730 
7731 /* called with rcu_read_lock held */
nf_tables_getobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7732 static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info,
7733 			    const struct nlattr * const nla[])
7734 {
7735 	struct netlink_ext_ack *extack = info->extack;
7736 	u8 genmask = nft_genmask_cur(info->net);
7737 	u8 family = info->nfmsg->nfgen_family;
7738 	const struct nft_table *table;
7739 	struct net *net = info->net;
7740 	struct nft_object *obj;
7741 	struct sk_buff *skb2;
7742 	bool reset = false;
7743 	u32 objtype;
7744 	int err;
7745 
7746 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
7747 		struct netlink_dump_control c = {
7748 			.start = nf_tables_dump_obj_start,
7749 			.dump = nf_tables_dump_obj,
7750 			.done = nf_tables_dump_obj_done,
7751 			.module = THIS_MODULE,
7752 			.data = (void *)nla,
7753 		};
7754 
7755 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
7756 	}
7757 
7758 	if (!nla[NFTA_OBJ_NAME] ||
7759 	    !nla[NFTA_OBJ_TYPE])
7760 		return -EINVAL;
7761 
7762 	table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0);
7763 	if (IS_ERR(table)) {
7764 		NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7765 		return PTR_ERR(table);
7766 	}
7767 
7768 	objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7769 	obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7770 	if (IS_ERR(obj)) {
7771 		NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7772 		return PTR_ERR(obj);
7773 	}
7774 
7775 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
7776 	if (!skb2)
7777 		return -ENOMEM;
7778 
7779 	if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
7780 		reset = true;
7781 
7782 	if (reset) {
7783 		const struct nftables_pernet *nft_net;
7784 		char *buf;
7785 
7786 		nft_net = nft_pernet(net);
7787 		buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq);
7788 
7789 		audit_log_nfcfg(buf,
7790 				family,
7791 				1,
7792 				AUDIT_NFT_OP_OBJ_RESET,
7793 				GFP_ATOMIC);
7794 		kfree(buf);
7795 	}
7796 
7797 	err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
7798 				      info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
7799 				      family, table, obj, reset);
7800 	if (err < 0)
7801 		goto err_fill_obj_info;
7802 
7803 	return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7804 
7805 err_fill_obj_info:
7806 	kfree_skb(skb2);
7807 	return err;
7808 }
7809 
nft_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)7810 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
7811 {
7812 	if (obj->ops->destroy)
7813 		obj->ops->destroy(ctx, obj);
7814 
7815 	module_put(obj->ops->type->owner);
7816 	kfree(obj->key.name);
7817 	kfree(obj->udata);
7818 	kfree(obj);
7819 }
7820 
nf_tables_delobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7821 static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
7822 			    const struct nlattr * const nla[])
7823 {
7824 	struct netlink_ext_ack *extack = info->extack;
7825 	u8 genmask = nft_genmask_next(info->net);
7826 	u8 family = info->nfmsg->nfgen_family;
7827 	struct net *net = info->net;
7828 	const struct nlattr *attr;
7829 	struct nft_table *table;
7830 	struct nft_object *obj;
7831 	struct nft_ctx ctx;
7832 	u32 objtype;
7833 
7834 	if (!nla[NFTA_OBJ_TYPE] ||
7835 	    (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
7836 		return -EINVAL;
7837 
7838 	table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7839 				 NETLINK_CB(skb).portid);
7840 	if (IS_ERR(table)) {
7841 		NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7842 		return PTR_ERR(table);
7843 	}
7844 
7845 	objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7846 	if (nla[NFTA_OBJ_HANDLE]) {
7847 		attr = nla[NFTA_OBJ_HANDLE];
7848 		obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
7849 	} else {
7850 		attr = nla[NFTA_OBJ_NAME];
7851 		obj = nft_obj_lookup(net, table, attr, objtype, genmask);
7852 	}
7853 
7854 	if (IS_ERR(obj)) {
7855 		if (PTR_ERR(obj) == -ENOENT &&
7856 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ)
7857 			return 0;
7858 
7859 		NL_SET_BAD_ATTR(extack, attr);
7860 		return PTR_ERR(obj);
7861 	}
7862 	if (obj->use > 0) {
7863 		NL_SET_BAD_ATTR(extack, attr);
7864 		return -EBUSY;
7865 	}
7866 
7867 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7868 
7869 	return nft_delobj(&ctx, obj);
7870 }
7871 
7872 static void
__nft_obj_notify(struct net * net,const struct nft_table * table,struct nft_object * obj,u32 portid,u32 seq,int event,u16 flags,int family,int report,gfp_t gfp)7873 __nft_obj_notify(struct net *net, const struct nft_table *table,
7874 		 struct nft_object *obj, u32 portid, u32 seq, int event,
7875 		 u16 flags, int family, int report, gfp_t gfp)
7876 {
7877 	struct nftables_pernet *nft_net = nft_pernet(net);
7878 	struct sk_buff *skb;
7879 	int err;
7880 
7881 	if (!report &&
7882 	    !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
7883 		return;
7884 
7885 	skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
7886 	if (skb == NULL)
7887 		goto err;
7888 
7889 	err = nf_tables_fill_obj_info(skb, net, portid, seq, event,
7890 				      flags & (NLM_F_CREATE | NLM_F_EXCL),
7891 				      family, table, obj, false);
7892 	if (err < 0) {
7893 		kfree_skb(skb);
7894 		goto err;
7895 	}
7896 
7897 	nft_notify_enqueue(skb, report, &nft_net->notify_list);
7898 	return;
7899 err:
7900 	nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
7901 }
7902 
nft_obj_notify(struct net * net,const struct nft_table * table,struct nft_object * obj,u32 portid,u32 seq,int event,u16 flags,int family,int report,gfp_t gfp)7903 void nft_obj_notify(struct net *net, const struct nft_table *table,
7904 		    struct nft_object *obj, u32 portid, u32 seq, int event,
7905 		    u16 flags, int family, int report, gfp_t gfp)
7906 {
7907 	struct nftables_pernet *nft_net = nft_pernet(net);
7908 	char *buf = kasprintf(gfp, "%s:%u",
7909 			      table->name, nft_net->base_seq);
7910 
7911 	audit_log_nfcfg(buf,
7912 			family,
7913 			obj->handle,
7914 			event == NFT_MSG_NEWOBJ ?
7915 				 AUDIT_NFT_OP_OBJ_REGISTER :
7916 				 AUDIT_NFT_OP_OBJ_UNREGISTER,
7917 			gfp);
7918 	kfree(buf);
7919 
7920 	__nft_obj_notify(net, table, obj, portid, seq, event,
7921 			 flags, family, report, gfp);
7922 }
7923 EXPORT_SYMBOL_GPL(nft_obj_notify);
7924 
nf_tables_obj_notify(const struct nft_ctx * ctx,struct nft_object * obj,int event)7925 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
7926 				 struct nft_object *obj, int event)
7927 {
7928 	__nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid,
7929 			 ctx->seq, event, ctx->flags, ctx->family,
7930 			 ctx->report, GFP_KERNEL);
7931 }
7932 
7933 /*
7934  * Flow tables
7935  */
nft_register_flowtable_type(struct nf_flowtable_type * type)7936 void nft_register_flowtable_type(struct nf_flowtable_type *type)
7937 {
7938 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
7939 	list_add_tail_rcu(&type->list, &nf_tables_flowtables);
7940 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7941 }
7942 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
7943 
nft_unregister_flowtable_type(struct nf_flowtable_type * type)7944 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
7945 {
7946 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
7947 	list_del_rcu(&type->list);
7948 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7949 }
7950 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
7951 
7952 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
7953 	[NFTA_FLOWTABLE_TABLE]		= { .type = NLA_STRING,
7954 					    .len = NFT_NAME_MAXLEN - 1 },
7955 	[NFTA_FLOWTABLE_NAME]		= { .type = NLA_STRING,
7956 					    .len = NFT_NAME_MAXLEN - 1 },
7957 	[NFTA_FLOWTABLE_HOOK]		= { .type = NLA_NESTED },
7958 	[NFTA_FLOWTABLE_HANDLE]		= { .type = NLA_U64 },
7959 	[NFTA_FLOWTABLE_FLAGS]		= { .type = NLA_U32 },
7960 };
7961 
nft_flowtable_lookup(const struct nft_table * table,const struct nlattr * nla,u8 genmask)7962 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
7963 					   const struct nlattr *nla, u8 genmask)
7964 {
7965 	struct nft_flowtable *flowtable;
7966 
7967 	list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
7968 		if (!nla_strcmp(nla, flowtable->name) &&
7969 		    nft_active_genmask(flowtable, genmask))
7970 			return flowtable;
7971 	}
7972 	return ERR_PTR(-ENOENT);
7973 }
7974 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
7975 
nf_tables_deactivate_flowtable(const struct nft_ctx * ctx,struct nft_flowtable * flowtable,enum nft_trans_phase phase)7976 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
7977 				    struct nft_flowtable *flowtable,
7978 				    enum nft_trans_phase phase)
7979 {
7980 	switch (phase) {
7981 	case NFT_TRANS_PREPARE_ERROR:
7982 	case NFT_TRANS_PREPARE:
7983 	case NFT_TRANS_ABORT:
7984 	case NFT_TRANS_RELEASE:
7985 		nft_use_dec(&flowtable->use);
7986 		fallthrough;
7987 	default:
7988 		return;
7989 	}
7990 }
7991 EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
7992 
7993 static struct nft_flowtable *
nft_flowtable_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u8 genmask)7994 nft_flowtable_lookup_byhandle(const struct nft_table *table,
7995 			      const struct nlattr *nla, u8 genmask)
7996 {
7997        struct nft_flowtable *flowtable;
7998 
7999        list_for_each_entry(flowtable, &table->flowtables, list) {
8000                if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
8001                    nft_active_genmask(flowtable, genmask))
8002                        return flowtable;
8003        }
8004        return ERR_PTR(-ENOENT);
8005 }
8006 
8007 struct nft_flowtable_hook {
8008 	u32			num;
8009 	int			priority;
8010 	struct list_head	list;
8011 };
8012 
8013 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
8014 	[NFTA_FLOWTABLE_HOOK_NUM]	= { .type = NLA_U32 },
8015 	[NFTA_FLOWTABLE_HOOK_PRIORITY]	= { .type = NLA_U32 },
8016 	[NFTA_FLOWTABLE_HOOK_DEVS]	= { .type = NLA_NESTED },
8017 };
8018 
nft_flowtable_parse_hook(const struct nft_ctx * ctx,const struct nlattr * const nla[],struct nft_flowtable_hook * flowtable_hook,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack,bool add)8019 static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
8020 				    const struct nlattr * const nla[],
8021 				    struct nft_flowtable_hook *flowtable_hook,
8022 				    struct nft_flowtable *flowtable,
8023 				    struct netlink_ext_ack *extack, bool add)
8024 {
8025 	struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
8026 	struct nft_hook *hook;
8027 	int hooknum, priority;
8028 	int err;
8029 
8030 	INIT_LIST_HEAD(&flowtable_hook->list);
8031 
8032 	err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX,
8033 					  nla[NFTA_FLOWTABLE_HOOK],
8034 					  nft_flowtable_hook_policy, NULL);
8035 	if (err < 0)
8036 		return err;
8037 
8038 	if (add) {
8039 		if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
8040 		    !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8041 			NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8042 			return -ENOENT;
8043 		}
8044 
8045 		hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8046 		if (hooknum != NF_NETDEV_INGRESS)
8047 			return -EOPNOTSUPP;
8048 
8049 		priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8050 
8051 		flowtable_hook->priority	= priority;
8052 		flowtable_hook->num		= hooknum;
8053 	} else {
8054 		if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
8055 			hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8056 			if (hooknum != flowtable->hooknum)
8057 				return -EOPNOTSUPP;
8058 		}
8059 
8060 		if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8061 			priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8062 			if (priority != flowtable->data.priority)
8063 				return -EOPNOTSUPP;
8064 		}
8065 
8066 		flowtable_hook->priority	= flowtable->data.priority;
8067 		flowtable_hook->num		= flowtable->hooknum;
8068 	}
8069 
8070 	if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
8071 		err = nf_tables_parse_netdev_hooks(ctx->net,
8072 						   tb[NFTA_FLOWTABLE_HOOK_DEVS],
8073 						   &flowtable_hook->list,
8074 						   extack);
8075 		if (err < 0)
8076 			return err;
8077 	}
8078 
8079 	list_for_each_entry(hook, &flowtable_hook->list, list) {
8080 		hook->ops.pf		= NFPROTO_NETDEV;
8081 		hook->ops.hooknum	= flowtable_hook->num;
8082 		hook->ops.priority	= flowtable_hook->priority;
8083 		hook->ops.priv		= &flowtable->data;
8084 		hook->ops.hook		= flowtable->data.type->hook;
8085 	}
8086 
8087 	return err;
8088 }
8089 
__nft_flowtable_type_get(u8 family)8090 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
8091 {
8092 	const struct nf_flowtable_type *type;
8093 
8094 	list_for_each_entry(type, &nf_tables_flowtables, list) {
8095 		if (family == type->family)
8096 			return type;
8097 	}
8098 	return NULL;
8099 }
8100 
8101 static const struct nf_flowtable_type *
nft_flowtable_type_get(struct net * net,u8 family)8102 nft_flowtable_type_get(struct net *net, u8 family)
8103 {
8104 	const struct nf_flowtable_type *type;
8105 
8106 	type = __nft_flowtable_type_get(family);
8107 	if (type != NULL && try_module_get(type->owner))
8108 		return type;
8109 
8110 	lockdep_nfnl_nft_mutex_not_held();
8111 #ifdef CONFIG_MODULES
8112 	if (type == NULL) {
8113 		if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
8114 			return ERR_PTR(-EAGAIN);
8115 	}
8116 #endif
8117 	return ERR_PTR(-ENOENT);
8118 }
8119 
8120 /* Only called from error and netdev event paths. */
nft_unregister_flowtable_hook(struct net * net,struct nft_flowtable * flowtable,struct nft_hook * hook)8121 static void nft_unregister_flowtable_hook(struct net *net,
8122 					  struct nft_flowtable *flowtable,
8123 					  struct nft_hook *hook)
8124 {
8125 	nf_unregister_net_hook(net, &hook->ops);
8126 	flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
8127 				    FLOW_BLOCK_UNBIND);
8128 }
8129 
__nft_unregister_flowtable_net_hooks(struct net * net,struct list_head * hook_list,bool release_netdev)8130 static void __nft_unregister_flowtable_net_hooks(struct net *net,
8131 						 struct list_head *hook_list,
8132 					         bool release_netdev)
8133 {
8134 	struct nft_hook *hook, *next;
8135 
8136 	list_for_each_entry_safe(hook, next, hook_list, list) {
8137 		nf_unregister_net_hook(net, &hook->ops);
8138 		if (release_netdev) {
8139 			list_del(&hook->list);
8140 			kfree_rcu(hook, rcu);
8141 		}
8142 	}
8143 }
8144 
nft_unregister_flowtable_net_hooks(struct net * net,struct list_head * hook_list)8145 static void nft_unregister_flowtable_net_hooks(struct net *net,
8146 					       struct list_head *hook_list)
8147 {
8148 	__nft_unregister_flowtable_net_hooks(net, hook_list, false);
8149 }
8150 
nft_register_flowtable_net_hooks(struct net * net,struct nft_table * table,struct list_head * hook_list,struct nft_flowtable * flowtable)8151 static int nft_register_flowtable_net_hooks(struct net *net,
8152 					    struct nft_table *table,
8153 					    struct list_head *hook_list,
8154 					    struct nft_flowtable *flowtable)
8155 {
8156 	struct nft_hook *hook, *hook2, *next;
8157 	struct nft_flowtable *ft;
8158 	int err, i = 0;
8159 
8160 	list_for_each_entry(hook, hook_list, list) {
8161 		list_for_each_entry(ft, &table->flowtables, list) {
8162 			if (!nft_is_active_next(net, ft))
8163 				continue;
8164 
8165 			list_for_each_entry(hook2, &ft->hook_list, list) {
8166 				if (hook->ops.dev == hook2->ops.dev &&
8167 				    hook->ops.pf == hook2->ops.pf) {
8168 					err = -EEXIST;
8169 					goto err_unregister_net_hooks;
8170 				}
8171 			}
8172 		}
8173 
8174 		err = flowtable->data.type->setup(&flowtable->data,
8175 						  hook->ops.dev,
8176 						  FLOW_BLOCK_BIND);
8177 		if (err < 0)
8178 			goto err_unregister_net_hooks;
8179 
8180 		err = nf_register_net_hook(net, &hook->ops);
8181 		if (err < 0) {
8182 			flowtable->data.type->setup(&flowtable->data,
8183 						    hook->ops.dev,
8184 						    FLOW_BLOCK_UNBIND);
8185 			goto err_unregister_net_hooks;
8186 		}
8187 
8188 		i++;
8189 	}
8190 
8191 	return 0;
8192 
8193 err_unregister_net_hooks:
8194 	list_for_each_entry_safe(hook, next, hook_list, list) {
8195 		if (i-- <= 0)
8196 			break;
8197 
8198 		nft_unregister_flowtable_hook(net, flowtable, hook);
8199 		list_del_rcu(&hook->list);
8200 		kfree_rcu(hook, rcu);
8201 	}
8202 
8203 	return err;
8204 }
8205 
nft_hooks_destroy(struct list_head * hook_list)8206 static void nft_hooks_destroy(struct list_head *hook_list)
8207 {
8208 	struct nft_hook *hook, *next;
8209 
8210 	list_for_each_entry_safe(hook, next, hook_list, list) {
8211 		list_del_rcu(&hook->list);
8212 		kfree_rcu(hook, rcu);
8213 	}
8214 }
8215 
nft_flowtable_update(struct nft_ctx * ctx,const struct nlmsghdr * nlh,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack)8216 static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
8217 				struct nft_flowtable *flowtable,
8218 				struct netlink_ext_ack *extack)
8219 {
8220 	const struct nlattr * const *nla = ctx->nla;
8221 	struct nft_flowtable_hook flowtable_hook;
8222 	struct nft_hook *hook, *next;
8223 	struct nft_trans *trans;
8224 	bool unregister = false;
8225 	u32 flags;
8226 	int err;
8227 
8228 	err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
8229 				       extack, false);
8230 	if (err < 0)
8231 		return err;
8232 
8233 	list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
8234 		if (nft_hook_list_find(&flowtable->hook_list, hook)) {
8235 			list_del(&hook->list);
8236 			kfree(hook);
8237 		}
8238 	}
8239 
8240 	if (nla[NFTA_FLOWTABLE_FLAGS]) {
8241 		flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
8242 		if (flags & ~NFT_FLOWTABLE_MASK) {
8243 			err = -EOPNOTSUPP;
8244 			goto err_flowtable_update_hook;
8245 		}
8246 		if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^
8247 		    (flags & NFT_FLOWTABLE_HW_OFFLOAD)) {
8248 			err = -EOPNOTSUPP;
8249 			goto err_flowtable_update_hook;
8250 		}
8251 	} else {
8252 		flags = flowtable->data.flags;
8253 	}
8254 
8255 	err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
8256 					       &flowtable_hook.list, flowtable);
8257 	if (err < 0)
8258 		goto err_flowtable_update_hook;
8259 
8260 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
8261 				sizeof(struct nft_trans_flowtable));
8262 	if (!trans) {
8263 		unregister = true;
8264 		err = -ENOMEM;
8265 		goto err_flowtable_update_hook;
8266 	}
8267 
8268 	nft_trans_flowtable_flags(trans) = flags;
8269 	nft_trans_flowtable(trans) = flowtable;
8270 	nft_trans_flowtable_update(trans) = true;
8271 	INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
8272 	list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
8273 
8274 	nft_trans_commit_list_add_tail(ctx->net, trans);
8275 
8276 	return 0;
8277 
8278 err_flowtable_update_hook:
8279 	list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
8280 		if (unregister)
8281 			nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
8282 		list_del_rcu(&hook->list);
8283 		kfree_rcu(hook, rcu);
8284 	}
8285 
8286 	return err;
8287 
8288 }
8289 
nf_tables_newflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8290 static int nf_tables_newflowtable(struct sk_buff *skb,
8291 				  const struct nfnl_info *info,
8292 				  const struct nlattr * const nla[])
8293 {
8294 	struct netlink_ext_ack *extack = info->extack;
8295 	struct nft_flowtable_hook flowtable_hook;
8296 	u8 genmask = nft_genmask_next(info->net);
8297 	u8 family = info->nfmsg->nfgen_family;
8298 	const struct nf_flowtable_type *type;
8299 	struct nft_flowtable *flowtable;
8300 	struct nft_hook *hook, *next;
8301 	struct net *net = info->net;
8302 	struct nft_table *table;
8303 	struct nft_ctx ctx;
8304 	int err;
8305 
8306 	if (!nla[NFTA_FLOWTABLE_TABLE] ||
8307 	    !nla[NFTA_FLOWTABLE_NAME] ||
8308 	    !nla[NFTA_FLOWTABLE_HOOK])
8309 		return -EINVAL;
8310 
8311 	table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8312 				 genmask, NETLINK_CB(skb).portid);
8313 	if (IS_ERR(table)) {
8314 		NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
8315 		return PTR_ERR(table);
8316 	}
8317 
8318 	flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8319 					 genmask);
8320 	if (IS_ERR(flowtable)) {
8321 		err = PTR_ERR(flowtable);
8322 		if (err != -ENOENT) {
8323 			NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8324 			return err;
8325 		}
8326 	} else {
8327 		if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
8328 			NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8329 			return -EEXIST;
8330 		}
8331 
8332 		nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8333 
8334 		return nft_flowtable_update(&ctx, info->nlh, flowtable, extack);
8335 	}
8336 
8337 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8338 
8339 	if (!nft_use_inc(&table->use))
8340 		return -EMFILE;
8341 
8342 	flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT);
8343 	if (!flowtable) {
8344 		err = -ENOMEM;
8345 		goto flowtable_alloc;
8346 	}
8347 
8348 	flowtable->table = table;
8349 	flowtable->handle = nf_tables_alloc_handle(table);
8350 	INIT_LIST_HEAD(&flowtable->hook_list);
8351 
8352 	flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
8353 	if (!flowtable->name) {
8354 		err = -ENOMEM;
8355 		goto err1;
8356 	}
8357 
8358 	type = nft_flowtable_type_get(net, family);
8359 	if (IS_ERR(type)) {
8360 		err = PTR_ERR(type);
8361 		goto err2;
8362 	}
8363 
8364 	if (nla[NFTA_FLOWTABLE_FLAGS]) {
8365 		flowtable->data.flags =
8366 			ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
8367 		if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) {
8368 			err = -EOPNOTSUPP;
8369 			goto err3;
8370 		}
8371 	}
8372 
8373 	write_pnet(&flowtable->data.net, net);
8374 	flowtable->data.type = type;
8375 	err = type->init(&flowtable->data);
8376 	if (err < 0)
8377 		goto err3;
8378 
8379 	err = nft_flowtable_parse_hook(&ctx, nla, &flowtable_hook, flowtable,
8380 				       extack, true);
8381 	if (err < 0)
8382 		goto err4;
8383 
8384 	list_splice(&flowtable_hook.list, &flowtable->hook_list);
8385 	flowtable->data.priority = flowtable_hook.priority;
8386 	flowtable->hooknum = flowtable_hook.num;
8387 
8388 	err = nft_register_flowtable_net_hooks(ctx.net, table,
8389 					       &flowtable->hook_list,
8390 					       flowtable);
8391 	if (err < 0) {
8392 		nft_hooks_destroy(&flowtable->hook_list);
8393 		goto err4;
8394 	}
8395 
8396 	err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
8397 	if (err < 0)
8398 		goto err5;
8399 
8400 	list_add_tail_rcu(&flowtable->list, &table->flowtables);
8401 
8402 	return 0;
8403 err5:
8404 	list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
8405 		nft_unregister_flowtable_hook(net, flowtable, hook);
8406 		list_del_rcu(&hook->list);
8407 		kfree_rcu(hook, rcu);
8408 	}
8409 err4:
8410 	flowtable->data.type->free(&flowtable->data);
8411 err3:
8412 	module_put(type->owner);
8413 err2:
8414 	kfree(flowtable->name);
8415 err1:
8416 	kfree(flowtable);
8417 flowtable_alloc:
8418 	nft_use_dec_restore(&table->use);
8419 
8420 	return err;
8421 }
8422 
nft_flowtable_hook_release(struct nft_flowtable_hook * flowtable_hook)8423 static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
8424 {
8425 	struct nft_hook *this, *next;
8426 
8427 	list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
8428 		list_del(&this->list);
8429 		kfree(this);
8430 	}
8431 }
8432 
nft_delflowtable_hook(struct nft_ctx * ctx,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack)8433 static int nft_delflowtable_hook(struct nft_ctx *ctx,
8434 				 struct nft_flowtable *flowtable,
8435 				 struct netlink_ext_ack *extack)
8436 {
8437 	const struct nlattr * const *nla = ctx->nla;
8438 	struct nft_flowtable_hook flowtable_hook;
8439 	LIST_HEAD(flowtable_del_list);
8440 	struct nft_hook *this, *hook;
8441 	struct nft_trans *trans;
8442 	int err;
8443 
8444 	err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
8445 				       extack, false);
8446 	if (err < 0)
8447 		return err;
8448 
8449 	list_for_each_entry(this, &flowtable_hook.list, list) {
8450 		hook = nft_hook_list_find(&flowtable->hook_list, this);
8451 		if (!hook) {
8452 			err = -ENOENT;
8453 			goto err_flowtable_del_hook;
8454 		}
8455 		list_move(&hook->list, &flowtable_del_list);
8456 	}
8457 
8458 	trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
8459 				sizeof(struct nft_trans_flowtable));
8460 	if (!trans) {
8461 		err = -ENOMEM;
8462 		goto err_flowtable_del_hook;
8463 	}
8464 
8465 	nft_trans_flowtable(trans) = flowtable;
8466 	nft_trans_flowtable_update(trans) = true;
8467 	INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
8468 	list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans));
8469 	nft_flowtable_hook_release(&flowtable_hook);
8470 
8471 	nft_trans_commit_list_add_tail(ctx->net, trans);
8472 
8473 	return 0;
8474 
8475 err_flowtable_del_hook:
8476 	list_splice(&flowtable_del_list, &flowtable->hook_list);
8477 	nft_flowtable_hook_release(&flowtable_hook);
8478 
8479 	return err;
8480 }
8481 
nf_tables_delflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8482 static int nf_tables_delflowtable(struct sk_buff *skb,
8483 				  const struct nfnl_info *info,
8484 				  const struct nlattr * const nla[])
8485 {
8486 	struct netlink_ext_ack *extack = info->extack;
8487 	u8 genmask = nft_genmask_next(info->net);
8488 	u8 family = info->nfmsg->nfgen_family;
8489 	struct nft_flowtable *flowtable;
8490 	struct net *net = info->net;
8491 	const struct nlattr *attr;
8492 	struct nft_table *table;
8493 	struct nft_ctx ctx;
8494 
8495 	if (!nla[NFTA_FLOWTABLE_TABLE] ||
8496 	    (!nla[NFTA_FLOWTABLE_NAME] &&
8497 	     !nla[NFTA_FLOWTABLE_HANDLE]))
8498 		return -EINVAL;
8499 
8500 	table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8501 				 genmask, NETLINK_CB(skb).portid);
8502 	if (IS_ERR(table)) {
8503 		NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
8504 		return PTR_ERR(table);
8505 	}
8506 
8507 	if (nla[NFTA_FLOWTABLE_HANDLE]) {
8508 		attr = nla[NFTA_FLOWTABLE_HANDLE];
8509 		flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
8510 	} else {
8511 		attr = nla[NFTA_FLOWTABLE_NAME];
8512 		flowtable = nft_flowtable_lookup(table, attr, genmask);
8513 	}
8514 
8515 	if (IS_ERR(flowtable)) {
8516 		if (PTR_ERR(flowtable) == -ENOENT &&
8517 		    NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE)
8518 			return 0;
8519 
8520 		NL_SET_BAD_ATTR(extack, attr);
8521 		return PTR_ERR(flowtable);
8522 	}
8523 
8524 	nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8525 
8526 	if (nla[NFTA_FLOWTABLE_HOOK])
8527 		return nft_delflowtable_hook(&ctx, flowtable, extack);
8528 
8529 	if (flowtable->use > 0) {
8530 		NL_SET_BAD_ATTR(extack, attr);
8531 		return -EBUSY;
8532 	}
8533 
8534 	return nft_delflowtable(&ctx, flowtable);
8535 }
8536 
nf_tables_fill_flowtable_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,struct nft_flowtable * flowtable,struct list_head * hook_list)8537 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
8538 					 u32 portid, u32 seq, int event,
8539 					 u32 flags, int family,
8540 					 struct nft_flowtable *flowtable,
8541 					 struct list_head *hook_list)
8542 {
8543 	struct nlattr *nest, *nest_devs;
8544 	struct nft_hook *hook;
8545 	struct nlmsghdr *nlh;
8546 
8547 	event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
8548 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
8549 			   NFNETLINK_V0, nft_base_seq(net));
8550 	if (!nlh)
8551 		goto nla_put_failure;
8552 
8553 	if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
8554 	    nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
8555 	    nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
8556 			 NFTA_FLOWTABLE_PAD))
8557 		goto nla_put_failure;
8558 
8559 	if (event == NFT_MSG_DELFLOWTABLE && !hook_list) {
8560 		nlmsg_end(skb, nlh);
8561 		return 0;
8562 	}
8563 
8564 	if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
8565 	    nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
8566 		goto nla_put_failure;
8567 
8568 	nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
8569 	if (!nest)
8570 		goto nla_put_failure;
8571 	if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
8572 	    nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
8573 		goto nla_put_failure;
8574 
8575 	nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
8576 	if (!nest_devs)
8577 		goto nla_put_failure;
8578 
8579 	if (!hook_list)
8580 		hook_list = &flowtable->hook_list;
8581 
8582 	list_for_each_entry_rcu(hook, hook_list, list) {
8583 		if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
8584 			goto nla_put_failure;
8585 	}
8586 	nla_nest_end(skb, nest_devs);
8587 	nla_nest_end(skb, nest);
8588 
8589 	nlmsg_end(skb, nlh);
8590 	return 0;
8591 
8592 nla_put_failure:
8593 	nlmsg_trim(skb, nlh);
8594 	return -1;
8595 }
8596 
8597 struct nft_flowtable_filter {
8598 	char		*table;
8599 };
8600 
nf_tables_dump_flowtable(struct sk_buff * skb,struct netlink_callback * cb)8601 static int nf_tables_dump_flowtable(struct sk_buff *skb,
8602 				    struct netlink_callback *cb)
8603 {
8604 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
8605 	struct nft_flowtable_filter *filter = cb->data;
8606 	unsigned int idx = 0, s_idx = cb->args[0];
8607 	struct net *net = sock_net(skb->sk);
8608 	int family = nfmsg->nfgen_family;
8609 	struct nft_flowtable *flowtable;
8610 	struct nftables_pernet *nft_net;
8611 	const struct nft_table *table;
8612 
8613 	rcu_read_lock();
8614 	nft_net = nft_pernet(net);
8615 	cb->seq = READ_ONCE(nft_net->base_seq);
8616 
8617 	list_for_each_entry_rcu(table, &nft_net->tables, list) {
8618 		if (family != NFPROTO_UNSPEC && family != table->family)
8619 			continue;
8620 
8621 		list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
8622 			if (!nft_is_active(net, flowtable))
8623 				goto cont;
8624 			if (idx < s_idx)
8625 				goto cont;
8626 			if (idx > s_idx)
8627 				memset(&cb->args[1], 0,
8628 				       sizeof(cb->args) - sizeof(cb->args[0]));
8629 			if (filter && filter->table &&
8630 			    strcmp(filter->table, table->name))
8631 				goto cont;
8632 
8633 			if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
8634 							  cb->nlh->nlmsg_seq,
8635 							  NFT_MSG_NEWFLOWTABLE,
8636 							  NLM_F_MULTI | NLM_F_APPEND,
8637 							  table->family,
8638 							  flowtable, NULL) < 0)
8639 				goto done;
8640 
8641 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
8642 cont:
8643 			idx++;
8644 		}
8645 	}
8646 done:
8647 	rcu_read_unlock();
8648 
8649 	cb->args[0] = idx;
8650 	return skb->len;
8651 }
8652 
nf_tables_dump_flowtable_start(struct netlink_callback * cb)8653 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
8654 {
8655 	const struct nlattr * const *nla = cb->data;
8656 	struct nft_flowtable_filter *filter = NULL;
8657 
8658 	if (nla[NFTA_FLOWTABLE_TABLE]) {
8659 		filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
8660 		if (!filter)
8661 			return -ENOMEM;
8662 
8663 		filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
8664 					   GFP_ATOMIC);
8665 		if (!filter->table) {
8666 			kfree(filter);
8667 			return -ENOMEM;
8668 		}
8669 	}
8670 
8671 	cb->data = filter;
8672 	return 0;
8673 }
8674 
nf_tables_dump_flowtable_done(struct netlink_callback * cb)8675 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
8676 {
8677 	struct nft_flowtable_filter *filter = cb->data;
8678 
8679 	if (!filter)
8680 		return 0;
8681 
8682 	kfree(filter->table);
8683 	kfree(filter);
8684 
8685 	return 0;
8686 }
8687 
8688 /* called with rcu_read_lock held */
nf_tables_getflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8689 static int nf_tables_getflowtable(struct sk_buff *skb,
8690 				  const struct nfnl_info *info,
8691 				  const struct nlattr * const nla[])
8692 {
8693 	u8 genmask = nft_genmask_cur(info->net);
8694 	u8 family = info->nfmsg->nfgen_family;
8695 	struct nft_flowtable *flowtable;
8696 	const struct nft_table *table;
8697 	struct net *net = info->net;
8698 	struct sk_buff *skb2;
8699 	int err;
8700 
8701 	if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
8702 		struct netlink_dump_control c = {
8703 			.start = nf_tables_dump_flowtable_start,
8704 			.dump = nf_tables_dump_flowtable,
8705 			.done = nf_tables_dump_flowtable_done,
8706 			.module = THIS_MODULE,
8707 			.data = (void *)nla,
8708 		};
8709 
8710 		return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
8711 	}
8712 
8713 	if (!nla[NFTA_FLOWTABLE_NAME])
8714 		return -EINVAL;
8715 
8716 	table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8717 				 genmask, 0);
8718 	if (IS_ERR(table))
8719 		return PTR_ERR(table);
8720 
8721 	flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8722 					 genmask);
8723 	if (IS_ERR(flowtable))
8724 		return PTR_ERR(flowtable);
8725 
8726 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8727 	if (!skb2)
8728 		return -ENOMEM;
8729 
8730 	err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
8731 					    info->nlh->nlmsg_seq,
8732 					    NFT_MSG_NEWFLOWTABLE, 0, family,
8733 					    flowtable, NULL);
8734 	if (err < 0)
8735 		goto err_fill_flowtable_info;
8736 
8737 	return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
8738 
8739 err_fill_flowtable_info:
8740 	kfree_skb(skb2);
8741 	return err;
8742 }
8743 
nf_tables_flowtable_notify(struct nft_ctx * ctx,struct nft_flowtable * flowtable,struct list_head * hook_list,int event)8744 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
8745 				       struct nft_flowtable *flowtable,
8746 				       struct list_head *hook_list, int event)
8747 {
8748 	struct nftables_pernet *nft_net = nft_pernet(ctx->net);
8749 	struct sk_buff *skb;
8750 	u16 flags = 0;
8751 	int err;
8752 
8753 	if (!ctx->report &&
8754 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
8755 		return;
8756 
8757 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
8758 	if (skb == NULL)
8759 		goto err;
8760 
8761 	if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
8762 		flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
8763 
8764 	err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
8765 					    ctx->seq, event, flags,
8766 					    ctx->family, flowtable, hook_list);
8767 	if (err < 0) {
8768 		kfree_skb(skb);
8769 		goto err;
8770 	}
8771 
8772 	nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
8773 	return;
8774 err:
8775 	nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
8776 }
8777 
nf_tables_flowtable_destroy(struct nft_flowtable * flowtable)8778 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
8779 {
8780 	struct nft_hook *hook, *next;
8781 
8782 	flowtable->data.type->free(&flowtable->data);
8783 	list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
8784 		flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
8785 					    FLOW_BLOCK_UNBIND);
8786 		list_del_rcu(&hook->list);
8787 		kfree(hook);
8788 	}
8789 	kfree(flowtable->name);
8790 	module_put(flowtable->data.type->owner);
8791 	kfree(flowtable);
8792 }
8793 
nf_tables_fill_gen_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq)8794 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
8795 				   u32 portid, u32 seq)
8796 {
8797 	struct nftables_pernet *nft_net = nft_pernet(net);
8798 	struct nlmsghdr *nlh;
8799 	char buf[TASK_COMM_LEN];
8800 	int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
8801 
8802 	nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC,
8803 			   NFNETLINK_V0, nft_base_seq(net));
8804 	if (!nlh)
8805 		goto nla_put_failure;
8806 
8807 	if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) ||
8808 	    nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
8809 	    nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
8810 		goto nla_put_failure;
8811 
8812 	nlmsg_end(skb, nlh);
8813 	return 0;
8814 
8815 nla_put_failure:
8816 	nlmsg_trim(skb, nlh);
8817 	return -EMSGSIZE;
8818 }
8819 
nft_flowtable_event(unsigned long event,struct net_device * dev,struct nft_flowtable * flowtable)8820 static void nft_flowtable_event(unsigned long event, struct net_device *dev,
8821 				struct nft_flowtable *flowtable)
8822 {
8823 	struct nft_hook *hook;
8824 
8825 	list_for_each_entry(hook, &flowtable->hook_list, list) {
8826 		if (hook->ops.dev != dev)
8827 			continue;
8828 
8829 		/* flow_offload_netdev_event() cleans up entries for us. */
8830 		nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
8831 		list_del_rcu(&hook->list);
8832 		kfree_rcu(hook, rcu);
8833 		break;
8834 	}
8835 }
8836 
nf_tables_flowtable_event(struct notifier_block * this,unsigned long event,void * ptr)8837 static int nf_tables_flowtable_event(struct notifier_block *this,
8838 				     unsigned long event, void *ptr)
8839 {
8840 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
8841 	struct nft_flowtable *flowtable;
8842 	struct nftables_pernet *nft_net;
8843 	struct nft_table *table;
8844 	struct net *net;
8845 
8846 	if (event != NETDEV_UNREGISTER)
8847 		return 0;
8848 
8849 	net = dev_net(dev);
8850 	nft_net = nft_pernet(net);
8851 	mutex_lock(&nft_net->commit_mutex);
8852 	list_for_each_entry(table, &nft_net->tables, list) {
8853 		list_for_each_entry(flowtable, &table->flowtables, list) {
8854 			nft_flowtable_event(event, dev, flowtable);
8855 		}
8856 	}
8857 	mutex_unlock(&nft_net->commit_mutex);
8858 
8859 	return NOTIFY_DONE;
8860 }
8861 
8862 static struct notifier_block nf_tables_flowtable_notifier = {
8863 	.notifier_call	= nf_tables_flowtable_event,
8864 };
8865 
nf_tables_gen_notify(struct net * net,struct sk_buff * skb,int event)8866 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
8867 				 int event)
8868 {
8869 	struct nlmsghdr *nlh = nlmsg_hdr(skb);
8870 	struct sk_buff *skb2;
8871 	int err;
8872 
8873 	if (!nlmsg_report(nlh) &&
8874 	    !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
8875 		return;
8876 
8877 	skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
8878 	if (skb2 == NULL)
8879 		goto err;
8880 
8881 	err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
8882 				      nlh->nlmsg_seq);
8883 	if (err < 0) {
8884 		kfree_skb(skb2);
8885 		goto err;
8886 	}
8887 
8888 	nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
8889 		       nlmsg_report(nlh), GFP_KERNEL);
8890 	return;
8891 err:
8892 	nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
8893 			  -ENOBUFS);
8894 }
8895 
nf_tables_getgen(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8896 static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info,
8897 			    const struct nlattr * const nla[])
8898 {
8899 	struct sk_buff *skb2;
8900 	int err;
8901 
8902 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8903 	if (skb2 == NULL)
8904 		return -ENOMEM;
8905 
8906 	err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid,
8907 				      info->nlh->nlmsg_seq);
8908 	if (err < 0)
8909 		goto err_fill_gen_info;
8910 
8911 	return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
8912 
8913 err_fill_gen_info:
8914 	kfree_skb(skb2);
8915 	return err;
8916 }
8917 
8918 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
8919 	[NFT_MSG_NEWTABLE] = {
8920 		.call		= nf_tables_newtable,
8921 		.type		= NFNL_CB_BATCH,
8922 		.attr_count	= NFTA_TABLE_MAX,
8923 		.policy		= nft_table_policy,
8924 	},
8925 	[NFT_MSG_GETTABLE] = {
8926 		.call		= nf_tables_gettable,
8927 		.type		= NFNL_CB_RCU,
8928 		.attr_count	= NFTA_TABLE_MAX,
8929 		.policy		= nft_table_policy,
8930 	},
8931 	[NFT_MSG_DELTABLE] = {
8932 		.call		= nf_tables_deltable,
8933 		.type		= NFNL_CB_BATCH,
8934 		.attr_count	= NFTA_TABLE_MAX,
8935 		.policy		= nft_table_policy,
8936 	},
8937 	[NFT_MSG_DESTROYTABLE] = {
8938 		.call		= nf_tables_deltable,
8939 		.type		= NFNL_CB_BATCH,
8940 		.attr_count	= NFTA_TABLE_MAX,
8941 		.policy		= nft_table_policy,
8942 	},
8943 	[NFT_MSG_NEWCHAIN] = {
8944 		.call		= nf_tables_newchain,
8945 		.type		= NFNL_CB_BATCH,
8946 		.attr_count	= NFTA_CHAIN_MAX,
8947 		.policy		= nft_chain_policy,
8948 	},
8949 	[NFT_MSG_GETCHAIN] = {
8950 		.call		= nf_tables_getchain,
8951 		.type		= NFNL_CB_RCU,
8952 		.attr_count	= NFTA_CHAIN_MAX,
8953 		.policy		= nft_chain_policy,
8954 	},
8955 	[NFT_MSG_DELCHAIN] = {
8956 		.call		= nf_tables_delchain,
8957 		.type		= NFNL_CB_BATCH,
8958 		.attr_count	= NFTA_CHAIN_MAX,
8959 		.policy		= nft_chain_policy,
8960 	},
8961 	[NFT_MSG_DESTROYCHAIN] = {
8962 		.call		= nf_tables_delchain,
8963 		.type		= NFNL_CB_BATCH,
8964 		.attr_count	= NFTA_CHAIN_MAX,
8965 		.policy		= nft_chain_policy,
8966 	},
8967 	[NFT_MSG_NEWRULE] = {
8968 		.call		= nf_tables_newrule,
8969 		.type		= NFNL_CB_BATCH,
8970 		.attr_count	= NFTA_RULE_MAX,
8971 		.policy		= nft_rule_policy,
8972 	},
8973 	[NFT_MSG_GETRULE] = {
8974 		.call		= nf_tables_getrule,
8975 		.type		= NFNL_CB_RCU,
8976 		.attr_count	= NFTA_RULE_MAX,
8977 		.policy		= nft_rule_policy,
8978 	},
8979 	[NFT_MSG_GETRULE_RESET] = {
8980 		.call		= nf_tables_getrule,
8981 		.type		= NFNL_CB_RCU,
8982 		.attr_count	= NFTA_RULE_MAX,
8983 		.policy		= nft_rule_policy,
8984 	},
8985 	[NFT_MSG_DELRULE] = {
8986 		.call		= nf_tables_delrule,
8987 		.type		= NFNL_CB_BATCH,
8988 		.attr_count	= NFTA_RULE_MAX,
8989 		.policy		= nft_rule_policy,
8990 	},
8991 	[NFT_MSG_DESTROYRULE] = {
8992 		.call		= nf_tables_delrule,
8993 		.type		= NFNL_CB_BATCH,
8994 		.attr_count	= NFTA_RULE_MAX,
8995 		.policy		= nft_rule_policy,
8996 	},
8997 	[NFT_MSG_NEWSET] = {
8998 		.call		= nf_tables_newset,
8999 		.type		= NFNL_CB_BATCH,
9000 		.attr_count	= NFTA_SET_MAX,
9001 		.policy		= nft_set_policy,
9002 	},
9003 	[NFT_MSG_GETSET] = {
9004 		.call		= nf_tables_getset,
9005 		.type		= NFNL_CB_RCU,
9006 		.attr_count	= NFTA_SET_MAX,
9007 		.policy		= nft_set_policy,
9008 	},
9009 	[NFT_MSG_DELSET] = {
9010 		.call		= nf_tables_delset,
9011 		.type		= NFNL_CB_BATCH,
9012 		.attr_count	= NFTA_SET_MAX,
9013 		.policy		= nft_set_policy,
9014 	},
9015 	[NFT_MSG_DESTROYSET] = {
9016 		.call		= nf_tables_delset,
9017 		.type		= NFNL_CB_BATCH,
9018 		.attr_count	= NFTA_SET_MAX,
9019 		.policy		= nft_set_policy,
9020 	},
9021 	[NFT_MSG_NEWSETELEM] = {
9022 		.call		= nf_tables_newsetelem,
9023 		.type		= NFNL_CB_BATCH,
9024 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
9025 		.policy		= nft_set_elem_list_policy,
9026 	},
9027 	[NFT_MSG_GETSETELEM] = {
9028 		.call		= nf_tables_getsetelem,
9029 		.type		= NFNL_CB_RCU,
9030 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
9031 		.policy		= nft_set_elem_list_policy,
9032 	},
9033 	[NFT_MSG_GETSETELEM_RESET] = {
9034 		.call		= nf_tables_getsetelem,
9035 		.type		= NFNL_CB_RCU,
9036 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
9037 		.policy		= nft_set_elem_list_policy,
9038 	},
9039 	[NFT_MSG_DELSETELEM] = {
9040 		.call		= nf_tables_delsetelem,
9041 		.type		= NFNL_CB_BATCH,
9042 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
9043 		.policy		= nft_set_elem_list_policy,
9044 	},
9045 	[NFT_MSG_DESTROYSETELEM] = {
9046 		.call		= nf_tables_delsetelem,
9047 		.type		= NFNL_CB_BATCH,
9048 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
9049 		.policy		= nft_set_elem_list_policy,
9050 	},
9051 	[NFT_MSG_GETGEN] = {
9052 		.call		= nf_tables_getgen,
9053 		.type		= NFNL_CB_RCU,
9054 	},
9055 	[NFT_MSG_NEWOBJ] = {
9056 		.call		= nf_tables_newobj,
9057 		.type		= NFNL_CB_BATCH,
9058 		.attr_count	= NFTA_OBJ_MAX,
9059 		.policy		= nft_obj_policy,
9060 	},
9061 	[NFT_MSG_GETOBJ] = {
9062 		.call		= nf_tables_getobj,
9063 		.type		= NFNL_CB_RCU,
9064 		.attr_count	= NFTA_OBJ_MAX,
9065 		.policy		= nft_obj_policy,
9066 	},
9067 	[NFT_MSG_DELOBJ] = {
9068 		.call		= nf_tables_delobj,
9069 		.type		= NFNL_CB_BATCH,
9070 		.attr_count	= NFTA_OBJ_MAX,
9071 		.policy		= nft_obj_policy,
9072 	},
9073 	[NFT_MSG_DESTROYOBJ] = {
9074 		.call		= nf_tables_delobj,
9075 		.type		= NFNL_CB_BATCH,
9076 		.attr_count	= NFTA_OBJ_MAX,
9077 		.policy		= nft_obj_policy,
9078 	},
9079 	[NFT_MSG_GETOBJ_RESET] = {
9080 		.call		= nf_tables_getobj,
9081 		.type		= NFNL_CB_RCU,
9082 		.attr_count	= NFTA_OBJ_MAX,
9083 		.policy		= nft_obj_policy,
9084 	},
9085 	[NFT_MSG_NEWFLOWTABLE] = {
9086 		.call		= nf_tables_newflowtable,
9087 		.type		= NFNL_CB_BATCH,
9088 		.attr_count	= NFTA_FLOWTABLE_MAX,
9089 		.policy		= nft_flowtable_policy,
9090 	},
9091 	[NFT_MSG_GETFLOWTABLE] = {
9092 		.call		= nf_tables_getflowtable,
9093 		.type		= NFNL_CB_RCU,
9094 		.attr_count	= NFTA_FLOWTABLE_MAX,
9095 		.policy		= nft_flowtable_policy,
9096 	},
9097 	[NFT_MSG_DELFLOWTABLE] = {
9098 		.call		= nf_tables_delflowtable,
9099 		.type		= NFNL_CB_BATCH,
9100 		.attr_count	= NFTA_FLOWTABLE_MAX,
9101 		.policy		= nft_flowtable_policy,
9102 	},
9103 	[NFT_MSG_DESTROYFLOWTABLE] = {
9104 		.call		= nf_tables_delflowtable,
9105 		.type		= NFNL_CB_BATCH,
9106 		.attr_count	= NFTA_FLOWTABLE_MAX,
9107 		.policy		= nft_flowtable_policy,
9108 	},
9109 };
9110 
nf_tables_validate(struct net * net)9111 static int nf_tables_validate(struct net *net)
9112 {
9113 	struct nftables_pernet *nft_net = nft_pernet(net);
9114 	struct nft_table *table;
9115 
9116 	list_for_each_entry(table, &nft_net->tables, list) {
9117 		switch (table->validate_state) {
9118 		case NFT_VALIDATE_SKIP:
9119 			continue;
9120 		case NFT_VALIDATE_NEED:
9121 			nft_validate_state_update(table, NFT_VALIDATE_DO);
9122 			fallthrough;
9123 		case NFT_VALIDATE_DO:
9124 			if (nft_table_validate(net, table) < 0)
9125 				return -EAGAIN;
9126 
9127 			nft_validate_state_update(table, NFT_VALIDATE_SKIP);
9128 			break;
9129 		}
9130 	}
9131 
9132 	return 0;
9133 }
9134 
9135 /* a drop policy has to be deferred until all rules have been activated,
9136  * otherwise a large ruleset that contains a drop-policy base chain will
9137  * cause all packets to get dropped until the full transaction has been
9138  * processed.
9139  *
9140  * We defer the drop policy until the transaction has been finalized.
9141  */
nft_chain_commit_drop_policy(struct nft_trans * trans)9142 static void nft_chain_commit_drop_policy(struct nft_trans *trans)
9143 {
9144 	struct nft_base_chain *basechain;
9145 
9146 	if (nft_trans_chain_policy(trans) != NF_DROP)
9147 		return;
9148 
9149 	if (!nft_is_base_chain(trans->ctx.chain))
9150 		return;
9151 
9152 	basechain = nft_base_chain(trans->ctx.chain);
9153 	basechain->policy = NF_DROP;
9154 }
9155 
nft_chain_commit_update(struct nft_trans * trans)9156 static void nft_chain_commit_update(struct nft_trans *trans)
9157 {
9158 	struct nft_base_chain *basechain;
9159 
9160 	if (nft_trans_chain_name(trans)) {
9161 		rhltable_remove(&trans->ctx.table->chains_ht,
9162 				&trans->ctx.chain->rhlhead,
9163 				nft_chain_ht_params);
9164 		swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
9165 		rhltable_insert_key(&trans->ctx.table->chains_ht,
9166 				    trans->ctx.chain->name,
9167 				    &trans->ctx.chain->rhlhead,
9168 				    nft_chain_ht_params);
9169 	}
9170 
9171 	if (!nft_is_base_chain(trans->ctx.chain))
9172 		return;
9173 
9174 	nft_chain_stats_replace(trans);
9175 
9176 	basechain = nft_base_chain(trans->ctx.chain);
9177 
9178 	switch (nft_trans_chain_policy(trans)) {
9179 	case NF_DROP:
9180 	case NF_ACCEPT:
9181 		basechain->policy = nft_trans_chain_policy(trans);
9182 		break;
9183 	}
9184 }
9185 
nft_obj_commit_update(struct nft_trans * trans)9186 static void nft_obj_commit_update(struct nft_trans *trans)
9187 {
9188 	struct nft_object *newobj;
9189 	struct nft_object *obj;
9190 
9191 	obj = nft_trans_obj(trans);
9192 	newobj = nft_trans_obj_newobj(trans);
9193 
9194 	if (obj->ops->update)
9195 		obj->ops->update(obj, newobj);
9196 
9197 	nft_obj_destroy(&trans->ctx, newobj);
9198 }
9199 
nft_commit_release(struct nft_trans * trans)9200 static void nft_commit_release(struct nft_trans *trans)
9201 {
9202 	switch (trans->msg_type) {
9203 	case NFT_MSG_DELTABLE:
9204 	case NFT_MSG_DESTROYTABLE:
9205 		nf_tables_table_destroy(&trans->ctx);
9206 		break;
9207 	case NFT_MSG_NEWCHAIN:
9208 		free_percpu(nft_trans_chain_stats(trans));
9209 		kfree(nft_trans_chain_name(trans));
9210 		break;
9211 	case NFT_MSG_DELCHAIN:
9212 	case NFT_MSG_DESTROYCHAIN:
9213 		if (nft_trans_chain_update(trans))
9214 			nft_hooks_destroy(&nft_trans_chain_hooks(trans));
9215 		else
9216 			nf_tables_chain_destroy(&trans->ctx);
9217 		break;
9218 	case NFT_MSG_DELRULE:
9219 	case NFT_MSG_DESTROYRULE:
9220 		nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
9221 		break;
9222 	case NFT_MSG_DELSET:
9223 	case NFT_MSG_DESTROYSET:
9224 		nft_set_destroy(&trans->ctx, nft_trans_set(trans));
9225 		break;
9226 	case NFT_MSG_DELSETELEM:
9227 	case NFT_MSG_DESTROYSETELEM:
9228 		nf_tables_set_elem_destroy(&trans->ctx,
9229 					   nft_trans_elem_set(trans),
9230 					   nft_trans_elem(trans).priv);
9231 		break;
9232 	case NFT_MSG_DELOBJ:
9233 	case NFT_MSG_DESTROYOBJ:
9234 		nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
9235 		break;
9236 	case NFT_MSG_DELFLOWTABLE:
9237 	case NFT_MSG_DESTROYFLOWTABLE:
9238 		if (nft_trans_flowtable_update(trans))
9239 			nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
9240 		else
9241 			nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
9242 		break;
9243 	}
9244 
9245 	if (trans->put_net)
9246 		put_net(trans->ctx.net);
9247 
9248 	kfree(trans);
9249 }
9250 
nf_tables_trans_destroy_work(struct work_struct * w)9251 static void nf_tables_trans_destroy_work(struct work_struct *w)
9252 {
9253 	struct nft_trans *trans, *next;
9254 	LIST_HEAD(head);
9255 
9256 	spin_lock(&nf_tables_destroy_list_lock);
9257 	list_splice_init(&nf_tables_destroy_list, &head);
9258 	spin_unlock(&nf_tables_destroy_list_lock);
9259 
9260 	if (list_empty(&head))
9261 		return;
9262 
9263 	synchronize_rcu();
9264 
9265 	list_for_each_entry_safe(trans, next, &head, list) {
9266 		nft_trans_list_del(trans);
9267 		nft_commit_release(trans);
9268 	}
9269 }
9270 
nf_tables_trans_destroy_flush_work(void)9271 void nf_tables_trans_destroy_flush_work(void)
9272 {
9273 	flush_work(&trans_destroy_work);
9274 }
9275 EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
9276 
nft_expr_reduce(struct nft_regs_track * track,const struct nft_expr * expr)9277 static bool nft_expr_reduce(struct nft_regs_track *track,
9278 			    const struct nft_expr *expr)
9279 {
9280 	return false;
9281 }
9282 
nf_tables_commit_chain_prepare(struct net * net,struct nft_chain * chain)9283 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
9284 {
9285 	const struct nft_expr *expr, *last;
9286 	struct nft_regs_track track = {};
9287 	unsigned int size, data_size;
9288 	void *data, *data_boundary;
9289 	struct nft_rule_dp *prule;
9290 	struct nft_rule *rule;
9291 
9292 	/* already handled or inactive chain? */
9293 	if (chain->blob_next || !nft_is_active_next(net, chain))
9294 		return 0;
9295 
9296 	data_size = 0;
9297 	list_for_each_entry(rule, &chain->rules, list) {
9298 		if (nft_is_active_next(net, rule)) {
9299 			data_size += sizeof(*prule) + rule->dlen;
9300 			if (data_size > INT_MAX)
9301 				return -ENOMEM;
9302 		}
9303 	}
9304 
9305 	chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size);
9306 	if (!chain->blob_next)
9307 		return -ENOMEM;
9308 
9309 	data = (void *)chain->blob_next->data;
9310 	data_boundary = data + data_size;
9311 	size = 0;
9312 
9313 	list_for_each_entry(rule, &chain->rules, list) {
9314 		if (!nft_is_active_next(net, rule))
9315 			continue;
9316 
9317 		prule = (struct nft_rule_dp *)data;
9318 		data += offsetof(struct nft_rule_dp, data);
9319 		if (WARN_ON_ONCE(data > data_boundary))
9320 			return -ENOMEM;
9321 
9322 		size = 0;
9323 		track.last = nft_expr_last(rule);
9324 		nft_rule_for_each_expr(expr, last, rule) {
9325 			track.cur = expr;
9326 
9327 			if (nft_expr_reduce(&track, expr)) {
9328 				expr = track.cur;
9329 				continue;
9330 			}
9331 
9332 			if (WARN_ON_ONCE(data + size + expr->ops->size > data_boundary))
9333 				return -ENOMEM;
9334 
9335 			memcpy(data + size, expr, expr->ops->size);
9336 			size += expr->ops->size;
9337 		}
9338 		if (WARN_ON_ONCE(size >= 1 << 12))
9339 			return -ENOMEM;
9340 
9341 		prule->handle = rule->handle;
9342 		prule->dlen = size;
9343 		prule->is_last = 0;
9344 
9345 		data += size;
9346 		size = 0;
9347 		chain->blob_next->size += (unsigned long)(data - (void *)prule);
9348 	}
9349 
9350 	if (WARN_ON_ONCE(data > data_boundary))
9351 		return -ENOMEM;
9352 
9353 	prule = (struct nft_rule_dp *)data;
9354 	nft_last_rule(chain, prule);
9355 
9356 	return 0;
9357 }
9358 
nf_tables_commit_chain_prepare_cancel(struct net * net)9359 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
9360 {
9361 	struct nftables_pernet *nft_net = nft_pernet(net);
9362 	struct nft_trans *trans, *next;
9363 
9364 	list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
9365 		struct nft_chain *chain = trans->ctx.chain;
9366 
9367 		if (trans->msg_type == NFT_MSG_NEWRULE ||
9368 		    trans->msg_type == NFT_MSG_DELRULE) {
9369 			kvfree(chain->blob_next);
9370 			chain->blob_next = NULL;
9371 		}
9372 	}
9373 }
9374 
__nf_tables_commit_chain_free_rules(struct rcu_head * h)9375 static void __nf_tables_commit_chain_free_rules(struct rcu_head *h)
9376 {
9377 	struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h);
9378 
9379 	kvfree(l->blob);
9380 }
9381 
nf_tables_commit_chain_free_rules_old(struct nft_rule_blob * blob)9382 static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob)
9383 {
9384 	struct nft_rule_dp_last *last;
9385 
9386 	/* last rule trailer is after end marker */
9387 	last = (void *)blob + sizeof(*blob) + blob->size;
9388 	last->blob = blob;
9389 
9390 	call_rcu(&last->h, __nf_tables_commit_chain_free_rules);
9391 }
9392 
nf_tables_commit_chain(struct net * net,struct nft_chain * chain)9393 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
9394 {
9395 	struct nft_rule_blob *g0, *g1;
9396 	bool next_genbit;
9397 
9398 	next_genbit = nft_gencursor_next(net);
9399 
9400 	g0 = rcu_dereference_protected(chain->blob_gen_0,
9401 				       lockdep_commit_lock_is_held(net));
9402 	g1 = rcu_dereference_protected(chain->blob_gen_1,
9403 				       lockdep_commit_lock_is_held(net));
9404 
9405 	/* No changes to this chain? */
9406 	if (chain->blob_next == NULL) {
9407 		/* chain had no change in last or next generation */
9408 		if (g0 == g1)
9409 			return;
9410 		/*
9411 		 * chain had no change in this generation; make sure next
9412 		 * one uses same rules as current generation.
9413 		 */
9414 		if (next_genbit) {
9415 			rcu_assign_pointer(chain->blob_gen_1, g0);
9416 			nf_tables_commit_chain_free_rules_old(g1);
9417 		} else {
9418 			rcu_assign_pointer(chain->blob_gen_0, g1);
9419 			nf_tables_commit_chain_free_rules_old(g0);
9420 		}
9421 
9422 		return;
9423 	}
9424 
9425 	if (next_genbit)
9426 		rcu_assign_pointer(chain->blob_gen_1, chain->blob_next);
9427 	else
9428 		rcu_assign_pointer(chain->blob_gen_0, chain->blob_next);
9429 
9430 	chain->blob_next = NULL;
9431 
9432 	if (g0 == g1)
9433 		return;
9434 
9435 	if (next_genbit)
9436 		nf_tables_commit_chain_free_rules_old(g1);
9437 	else
9438 		nf_tables_commit_chain_free_rules_old(g0);
9439 }
9440 
nft_obj_del(struct nft_object * obj)9441 static void nft_obj_del(struct nft_object *obj)
9442 {
9443 	rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
9444 	list_del_rcu(&obj->list);
9445 }
9446 
nft_chain_del(struct nft_chain * chain)9447 void nft_chain_del(struct nft_chain *chain)
9448 {
9449 	struct nft_table *table = chain->table;
9450 
9451 	WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
9452 				     nft_chain_ht_params));
9453 	list_del_rcu(&chain->list);
9454 }
9455 
nft_trans_gc_setelem_remove(struct nft_ctx * ctx,struct nft_trans_gc * trans)9456 static void nft_trans_gc_setelem_remove(struct nft_ctx *ctx,
9457 					struct nft_trans_gc *trans)
9458 {
9459 	void **priv = trans->priv;
9460 	unsigned int i;
9461 
9462 	for (i = 0; i < trans->count; i++) {
9463 		struct nft_set_elem elem = {
9464 			.priv = priv[i],
9465 		};
9466 
9467 		nft_setelem_data_deactivate(ctx->net, trans->set, &elem);
9468 		nft_setelem_remove(ctx->net, trans->set, &elem);
9469 	}
9470 }
9471 
nft_trans_gc_destroy(struct nft_trans_gc * trans)9472 void nft_trans_gc_destroy(struct nft_trans_gc *trans)
9473 {
9474 	nft_set_put(trans->set);
9475 	put_net(trans->net);
9476 	kfree(trans);
9477 }
9478 
nft_trans_gc_trans_free(struct rcu_head * rcu)9479 static void nft_trans_gc_trans_free(struct rcu_head *rcu)
9480 {
9481 	struct nft_set_elem elem = {};
9482 	struct nft_trans_gc *trans;
9483 	struct nft_ctx ctx = {};
9484 	unsigned int i;
9485 
9486 	trans = container_of(rcu, struct nft_trans_gc, rcu);
9487 	ctx.net	= read_pnet(&trans->set->net);
9488 
9489 	for (i = 0; i < trans->count; i++) {
9490 		elem.priv = trans->priv[i];
9491 		if (!nft_setelem_is_catchall(trans->set, &elem))
9492 			atomic_dec(&trans->set->nelems);
9493 
9494 		nf_tables_set_elem_destroy(&ctx, trans->set, elem.priv);
9495 	}
9496 
9497 	nft_trans_gc_destroy(trans);
9498 }
9499 
nft_trans_gc_work_done(struct nft_trans_gc * trans)9500 static bool nft_trans_gc_work_done(struct nft_trans_gc *trans)
9501 {
9502 	struct nftables_pernet *nft_net;
9503 	struct nft_ctx ctx = {};
9504 
9505 	nft_net = nft_pernet(trans->net);
9506 
9507 	mutex_lock(&nft_net->commit_mutex);
9508 
9509 	/* Check for race with transaction, otherwise this batch refers to
9510 	 * stale objects that might not be there anymore. Skip transaction if
9511 	 * set has been destroyed from control plane transaction in case gc
9512 	 * worker loses race.
9513 	 */
9514 	if (READ_ONCE(nft_net->gc_seq) != trans->seq || trans->set->dead) {
9515 		mutex_unlock(&nft_net->commit_mutex);
9516 		return false;
9517 	}
9518 
9519 	ctx.net = trans->net;
9520 	ctx.table = trans->set->table;
9521 
9522 	nft_trans_gc_setelem_remove(&ctx, trans);
9523 	mutex_unlock(&nft_net->commit_mutex);
9524 
9525 	return true;
9526 }
9527 
nft_trans_gc_work(struct work_struct * work)9528 static void nft_trans_gc_work(struct work_struct *work)
9529 {
9530 	struct nft_trans_gc *trans, *next;
9531 	LIST_HEAD(trans_gc_list);
9532 
9533 	spin_lock(&nf_tables_gc_list_lock);
9534 	list_splice_init(&nf_tables_gc_list, &trans_gc_list);
9535 	spin_unlock(&nf_tables_gc_list_lock);
9536 
9537 	list_for_each_entry_safe(trans, next, &trans_gc_list, list) {
9538 		list_del(&trans->list);
9539 		if (!nft_trans_gc_work_done(trans)) {
9540 			nft_trans_gc_destroy(trans);
9541 			continue;
9542 		}
9543 		call_rcu(&trans->rcu, nft_trans_gc_trans_free);
9544 	}
9545 }
9546 
nft_trans_gc_alloc(struct nft_set * set,unsigned int gc_seq,gfp_t gfp)9547 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
9548 					unsigned int gc_seq, gfp_t gfp)
9549 {
9550 	struct net *net = read_pnet(&set->net);
9551 	struct nft_trans_gc *trans;
9552 
9553 	trans = kzalloc(sizeof(*trans), gfp);
9554 	if (!trans)
9555 		return NULL;
9556 
9557 	trans->net = maybe_get_net(net);
9558 	if (!trans->net) {
9559 		kfree(trans);
9560 		return NULL;
9561 	}
9562 
9563 	refcount_inc(&set->refs);
9564 	trans->set = set;
9565 	trans->seq = gc_seq;
9566 
9567 	return trans;
9568 }
9569 
nft_trans_gc_elem_add(struct nft_trans_gc * trans,void * priv)9570 void nft_trans_gc_elem_add(struct nft_trans_gc *trans, void *priv)
9571 {
9572 	trans->priv[trans->count++] = priv;
9573 }
9574 
nft_trans_gc_queue_work(struct nft_trans_gc * trans)9575 static void nft_trans_gc_queue_work(struct nft_trans_gc *trans)
9576 {
9577 	spin_lock(&nf_tables_gc_list_lock);
9578 	list_add_tail(&trans->list, &nf_tables_gc_list);
9579 	spin_unlock(&nf_tables_gc_list_lock);
9580 
9581 	schedule_work(&trans_gc_work);
9582 }
9583 
nft_trans_gc_space(struct nft_trans_gc * trans)9584 static int nft_trans_gc_space(struct nft_trans_gc *trans)
9585 {
9586 	return NFT_TRANS_GC_BATCHCOUNT - trans->count;
9587 }
9588 
nft_trans_gc_queue_async(struct nft_trans_gc * gc,unsigned int gc_seq,gfp_t gfp)9589 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
9590 					      unsigned int gc_seq, gfp_t gfp)
9591 {
9592 	struct nft_set *set;
9593 
9594 	if (nft_trans_gc_space(gc))
9595 		return gc;
9596 
9597 	set = gc->set;
9598 	nft_trans_gc_queue_work(gc);
9599 
9600 	return nft_trans_gc_alloc(set, gc_seq, gfp);
9601 }
9602 
nft_trans_gc_queue_async_done(struct nft_trans_gc * trans)9603 void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans)
9604 {
9605 	if (trans->count == 0) {
9606 		nft_trans_gc_destroy(trans);
9607 		return;
9608 	}
9609 
9610 	nft_trans_gc_queue_work(trans);
9611 }
9612 
nft_trans_gc_queue_sync(struct nft_trans_gc * gc,gfp_t gfp)9613 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp)
9614 {
9615 	struct nft_set *set;
9616 
9617 	if (WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net)))
9618 		return NULL;
9619 
9620 	if (nft_trans_gc_space(gc))
9621 		return gc;
9622 
9623 	set = gc->set;
9624 	call_rcu(&gc->rcu, nft_trans_gc_trans_free);
9625 
9626 	return nft_trans_gc_alloc(set, 0, gfp);
9627 }
9628 
nft_trans_gc_queue_sync_done(struct nft_trans_gc * trans)9629 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans)
9630 {
9631 	WARN_ON_ONCE(!lockdep_commit_lock_is_held(trans->net));
9632 
9633 	if (trans->count == 0) {
9634 		nft_trans_gc_destroy(trans);
9635 		return;
9636 	}
9637 
9638 	call_rcu(&trans->rcu, nft_trans_gc_trans_free);
9639 }
9640 
nft_trans_gc_catchall(struct nft_trans_gc * gc,unsigned int gc_seq,bool sync)9641 static struct nft_trans_gc *nft_trans_gc_catchall(struct nft_trans_gc *gc,
9642 						  unsigned int gc_seq,
9643 						  bool sync)
9644 {
9645 	struct nft_set_elem_catchall *catchall;
9646 	const struct nft_set *set = gc->set;
9647 	struct nft_set_ext *ext;
9648 
9649 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
9650 		ext = nft_set_elem_ext(set, catchall->elem);
9651 
9652 		if (!nft_set_elem_expired(ext))
9653 			continue;
9654 		if (nft_set_elem_is_dead(ext))
9655 			goto dead_elem;
9656 
9657 		nft_set_elem_dead(ext);
9658 dead_elem:
9659 		if (sync)
9660 			gc = nft_trans_gc_queue_sync(gc, GFP_ATOMIC);
9661 		else
9662 			gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
9663 
9664 		if (!gc)
9665 			return NULL;
9666 
9667 		nft_trans_gc_elem_add(gc, catchall->elem);
9668 	}
9669 
9670 	return gc;
9671 }
9672 
nft_trans_gc_catchall_async(struct nft_trans_gc * gc,unsigned int gc_seq)9673 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
9674 						 unsigned int gc_seq)
9675 {
9676 	return nft_trans_gc_catchall(gc, gc_seq, false);
9677 }
9678 
nft_trans_gc_catchall_sync(struct nft_trans_gc * gc)9679 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc)
9680 {
9681 	return nft_trans_gc_catchall(gc, 0, true);
9682 }
9683 
nf_tables_module_autoload_cleanup(struct net * net)9684 static void nf_tables_module_autoload_cleanup(struct net *net)
9685 {
9686 	struct nftables_pernet *nft_net = nft_pernet(net);
9687 	struct nft_module_request *req, *next;
9688 
9689 	WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
9690 	list_for_each_entry_safe(req, next, &nft_net->module_list, list) {
9691 		WARN_ON_ONCE(!req->done);
9692 		list_del(&req->list);
9693 		kfree(req);
9694 	}
9695 }
9696 
nf_tables_commit_release(struct net * net)9697 static void nf_tables_commit_release(struct net *net)
9698 {
9699 	struct nftables_pernet *nft_net = nft_pernet(net);
9700 	struct nft_trans *trans;
9701 
9702 	/* all side effects have to be made visible.
9703 	 * For example, if a chain named 'foo' has been deleted, a
9704 	 * new transaction must not find it anymore.
9705 	 *
9706 	 * Memory reclaim happens asynchronously from work queue
9707 	 * to prevent expensive synchronize_rcu() in commit phase.
9708 	 */
9709 	if (list_empty(&nft_net->commit_list)) {
9710 		nf_tables_module_autoload_cleanup(net);
9711 		mutex_unlock(&nft_net->commit_mutex);
9712 		return;
9713 	}
9714 
9715 	trans = list_last_entry(&nft_net->commit_list,
9716 				struct nft_trans, list);
9717 	get_net(trans->ctx.net);
9718 	WARN_ON_ONCE(trans->put_net);
9719 
9720 	trans->put_net = true;
9721 	spin_lock(&nf_tables_destroy_list_lock);
9722 	list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list);
9723 	spin_unlock(&nf_tables_destroy_list_lock);
9724 
9725 	nf_tables_module_autoload_cleanup(net);
9726 	schedule_work(&trans_destroy_work);
9727 
9728 	mutex_unlock(&nft_net->commit_mutex);
9729 }
9730 
nft_commit_notify(struct net * net,u32 portid)9731 static void nft_commit_notify(struct net *net, u32 portid)
9732 {
9733 	struct nftables_pernet *nft_net = nft_pernet(net);
9734 	struct sk_buff *batch_skb = NULL, *nskb, *skb;
9735 	unsigned char *data;
9736 	int len;
9737 
9738 	list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) {
9739 		if (!batch_skb) {
9740 new_batch:
9741 			batch_skb = skb;
9742 			len = NLMSG_GOODSIZE - skb->len;
9743 			list_del(&skb->list);
9744 			continue;
9745 		}
9746 		len -= skb->len;
9747 		if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
9748 			data = skb_put(batch_skb, skb->len);
9749 			memcpy(data, skb->data, skb->len);
9750 			list_del(&skb->list);
9751 			kfree_skb(skb);
9752 			continue;
9753 		}
9754 		nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
9755 			       NFT_CB(batch_skb).report, GFP_KERNEL);
9756 		goto new_batch;
9757 	}
9758 
9759 	if (batch_skb) {
9760 		nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
9761 			       NFT_CB(batch_skb).report, GFP_KERNEL);
9762 	}
9763 
9764 	WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
9765 }
9766 
nf_tables_commit_audit_alloc(struct list_head * adl,struct nft_table * table)9767 static int nf_tables_commit_audit_alloc(struct list_head *adl,
9768 					struct nft_table *table)
9769 {
9770 	struct nft_audit_data *adp;
9771 
9772 	list_for_each_entry(adp, adl, list) {
9773 		if (adp->table == table)
9774 			return 0;
9775 	}
9776 	adp = kzalloc(sizeof(*adp), GFP_KERNEL);
9777 	if (!adp)
9778 		return -ENOMEM;
9779 	adp->table = table;
9780 	list_add(&adp->list, adl);
9781 	return 0;
9782 }
9783 
nf_tables_commit_audit_free(struct list_head * adl)9784 static void nf_tables_commit_audit_free(struct list_head *adl)
9785 {
9786 	struct nft_audit_data *adp, *adn;
9787 
9788 	list_for_each_entry_safe(adp, adn, adl, list) {
9789 		list_del(&adp->list);
9790 		kfree(adp);
9791 	}
9792 }
9793 
nf_tables_commit_audit_collect(struct list_head * adl,struct nft_table * table,u32 op)9794 static void nf_tables_commit_audit_collect(struct list_head *adl,
9795 					   struct nft_table *table, u32 op)
9796 {
9797 	struct nft_audit_data *adp;
9798 
9799 	list_for_each_entry(adp, adl, list) {
9800 		if (adp->table == table)
9801 			goto found;
9802 	}
9803 	WARN_ONCE(1, "table=%s not expected in commit list", table->name);
9804 	return;
9805 found:
9806 	adp->entries++;
9807 	if (!adp->op || adp->op > op)
9808 		adp->op = op;
9809 }
9810 
9811 #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22)
9812 
nf_tables_commit_audit_log(struct list_head * adl,u32 generation)9813 static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation)
9814 {
9815 	struct nft_audit_data *adp, *adn;
9816 	char aubuf[AUNFTABLENAMELEN];
9817 
9818 	list_for_each_entry_safe(adp, adn, adl, list) {
9819 		snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name,
9820 			 generation);
9821 		audit_log_nfcfg(aubuf, adp->table->family, adp->entries,
9822 				nft2audit_op[adp->op], GFP_KERNEL);
9823 		list_del(&adp->list);
9824 		kfree(adp);
9825 	}
9826 }
9827 
nft_set_commit_update(struct list_head * set_update_list)9828 static void nft_set_commit_update(struct list_head *set_update_list)
9829 {
9830 	struct nft_set *set, *next;
9831 
9832 	list_for_each_entry_safe(set, next, set_update_list, pending_update) {
9833 		list_del_init(&set->pending_update);
9834 
9835 		if (!set->ops->commit)
9836 			continue;
9837 
9838 		set->ops->commit(set);
9839 	}
9840 }
9841 
nft_gc_seq_begin(struct nftables_pernet * nft_net)9842 static unsigned int nft_gc_seq_begin(struct nftables_pernet *nft_net)
9843 {
9844 	unsigned int gc_seq;
9845 
9846 	/* Bump gc counter, it becomes odd, this is the busy mark. */
9847 	gc_seq = READ_ONCE(nft_net->gc_seq);
9848 	WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
9849 
9850 	return gc_seq;
9851 }
9852 
nft_gc_seq_end(struct nftables_pernet * nft_net,unsigned int gc_seq)9853 static void nft_gc_seq_end(struct nftables_pernet *nft_net, unsigned int gc_seq)
9854 {
9855 	WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
9856 }
9857 
nf_tables_commit(struct net * net,struct sk_buff * skb)9858 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
9859 {
9860 	struct nftables_pernet *nft_net = nft_pernet(net);
9861 	struct nft_trans *trans, *next;
9862 	unsigned int base_seq, gc_seq;
9863 	LIST_HEAD(set_update_list);
9864 	struct nft_trans_elem *te;
9865 	struct nft_chain *chain;
9866 	struct nft_table *table;
9867 	LIST_HEAD(adl);
9868 	int err;
9869 
9870 	if (list_empty(&nft_net->commit_list)) {
9871 		mutex_unlock(&nft_net->commit_mutex);
9872 		return 0;
9873 	}
9874 
9875 	list_for_each_entry(trans, &nft_net->binding_list, binding_list) {
9876 		switch (trans->msg_type) {
9877 		case NFT_MSG_NEWSET:
9878 			if (!nft_trans_set_update(trans) &&
9879 			    nft_set_is_anonymous(nft_trans_set(trans)) &&
9880 			    !nft_trans_set_bound(trans)) {
9881 				pr_warn_once("nftables ruleset with unbound set\n");
9882 				return -EINVAL;
9883 			}
9884 			break;
9885 		case NFT_MSG_NEWCHAIN:
9886 			if (!nft_trans_chain_update(trans) &&
9887 			    nft_chain_binding(nft_trans_chain(trans)) &&
9888 			    !nft_trans_chain_bound(trans)) {
9889 				pr_warn_once("nftables ruleset with unbound chain\n");
9890 				return -EINVAL;
9891 			}
9892 			break;
9893 		}
9894 	}
9895 
9896 	/* 0. Validate ruleset, otherwise roll back for error reporting. */
9897 	if (nf_tables_validate(net) < 0) {
9898 		nft_net->validate_state = NFT_VALIDATE_DO;
9899 		return -EAGAIN;
9900 	}
9901 
9902 	err = nft_flow_rule_offload_commit(net);
9903 	if (err < 0)
9904 		return err;
9905 
9906 	/* 1.  Allocate space for next generation rules_gen_X[] */
9907 	list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
9908 		int ret;
9909 
9910 		ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table);
9911 		if (ret) {
9912 			nf_tables_commit_chain_prepare_cancel(net);
9913 			nf_tables_commit_audit_free(&adl);
9914 			return ret;
9915 		}
9916 		if (trans->msg_type == NFT_MSG_NEWRULE ||
9917 		    trans->msg_type == NFT_MSG_DELRULE) {
9918 			chain = trans->ctx.chain;
9919 
9920 			ret = nf_tables_commit_chain_prepare(net, chain);
9921 			if (ret < 0) {
9922 				nf_tables_commit_chain_prepare_cancel(net);
9923 				nf_tables_commit_audit_free(&adl);
9924 				return ret;
9925 			}
9926 		}
9927 	}
9928 
9929 	/* step 2.  Make rules_gen_X visible to packet path */
9930 	list_for_each_entry(table, &nft_net->tables, list) {
9931 		list_for_each_entry(chain, &table->chains, list)
9932 			nf_tables_commit_chain(net, chain);
9933 	}
9934 
9935 	/*
9936 	 * Bump generation counter, invalidate any dump in progress.
9937 	 * Cannot fail after this point.
9938 	 */
9939 	base_seq = READ_ONCE(nft_net->base_seq);
9940 	while (++base_seq == 0)
9941 		;
9942 
9943 	WRITE_ONCE(nft_net->base_seq, base_seq);
9944 
9945 	gc_seq = nft_gc_seq_begin(nft_net);
9946 
9947 	/* step 3. Start new generation, rules_gen_X now in use. */
9948 	net->nft.gencursor = nft_gencursor_next(net);
9949 
9950 	list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
9951 		nf_tables_commit_audit_collect(&adl, trans->ctx.table,
9952 					       trans->msg_type);
9953 		switch (trans->msg_type) {
9954 		case NFT_MSG_NEWTABLE:
9955 			if (nft_trans_table_update(trans)) {
9956 				if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
9957 					nft_trans_destroy(trans);
9958 					break;
9959 				}
9960 				if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
9961 					nf_tables_table_disable(net, trans->ctx.table);
9962 
9963 				trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
9964 			} else {
9965 				nft_clear(net, trans->ctx.table);
9966 			}
9967 			nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
9968 			nft_trans_destroy(trans);
9969 			break;
9970 		case NFT_MSG_DELTABLE:
9971 		case NFT_MSG_DESTROYTABLE:
9972 			list_del_rcu(&trans->ctx.table->list);
9973 			nf_tables_table_notify(&trans->ctx, trans->msg_type);
9974 			break;
9975 		case NFT_MSG_NEWCHAIN:
9976 			if (nft_trans_chain_update(trans)) {
9977 				nft_chain_commit_update(trans);
9978 				nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN,
9979 						       &nft_trans_chain_hooks(trans));
9980 				list_splice(&nft_trans_chain_hooks(trans),
9981 					    &nft_trans_basechain(trans)->hook_list);
9982 				/* trans destroyed after rcu grace period */
9983 			} else {
9984 				nft_chain_commit_drop_policy(trans);
9985 				nft_clear(net, trans->ctx.chain);
9986 				nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, NULL);
9987 				nft_trans_destroy(trans);
9988 			}
9989 			break;
9990 		case NFT_MSG_DELCHAIN:
9991 		case NFT_MSG_DESTROYCHAIN:
9992 			if (nft_trans_chain_update(trans)) {
9993 				nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN,
9994 						       &nft_trans_chain_hooks(trans));
9995 				nft_netdev_unregister_hooks(net,
9996 							    &nft_trans_chain_hooks(trans),
9997 							    true);
9998 			} else {
9999 				nft_chain_del(trans->ctx.chain);
10000 				nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN,
10001 						       NULL);
10002 				nf_tables_unregister_hook(trans->ctx.net,
10003 							  trans->ctx.table,
10004 							  trans->ctx.chain);
10005 			}
10006 			break;
10007 		case NFT_MSG_NEWRULE:
10008 			nft_clear(trans->ctx.net, nft_trans_rule(trans));
10009 			nf_tables_rule_notify(&trans->ctx,
10010 					      nft_trans_rule(trans),
10011 					      NFT_MSG_NEWRULE);
10012 			if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10013 				nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10014 
10015 			nft_trans_destroy(trans);
10016 			break;
10017 		case NFT_MSG_DELRULE:
10018 		case NFT_MSG_DESTROYRULE:
10019 			list_del_rcu(&nft_trans_rule(trans)->list);
10020 			nf_tables_rule_notify(&trans->ctx,
10021 					      nft_trans_rule(trans),
10022 					      trans->msg_type);
10023 			nft_rule_expr_deactivate(&trans->ctx,
10024 						 nft_trans_rule(trans),
10025 						 NFT_TRANS_COMMIT);
10026 
10027 			if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10028 				nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10029 			break;
10030 		case NFT_MSG_NEWSET:
10031 			if (nft_trans_set_update(trans)) {
10032 				struct nft_set *set = nft_trans_set(trans);
10033 
10034 				WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans));
10035 				WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans));
10036 
10037 				if (nft_trans_set_size(trans))
10038 					WRITE_ONCE(set->size, nft_trans_set_size(trans));
10039 			} else {
10040 				nft_clear(net, nft_trans_set(trans));
10041 				/* This avoids hitting -EBUSY when deleting the table
10042 				 * from the transaction.
10043 				 */
10044 				if (nft_set_is_anonymous(nft_trans_set(trans)) &&
10045 				    !list_empty(&nft_trans_set(trans)->bindings))
10046 					nft_use_dec(&trans->ctx.table->use);
10047 			}
10048 			nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
10049 					     NFT_MSG_NEWSET, GFP_KERNEL);
10050 			nft_trans_destroy(trans);
10051 			break;
10052 		case NFT_MSG_DELSET:
10053 		case NFT_MSG_DESTROYSET:
10054 			nft_trans_set(trans)->dead = 1;
10055 			list_del_rcu(&nft_trans_set(trans)->list);
10056 			nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
10057 					     trans->msg_type, GFP_KERNEL);
10058 			break;
10059 		case NFT_MSG_NEWSETELEM:
10060 			te = (struct nft_trans_elem *)trans->data;
10061 
10062 			nft_setelem_activate(net, te->set, &te->elem);
10063 			nf_tables_setelem_notify(&trans->ctx, te->set,
10064 						 &te->elem,
10065 						 NFT_MSG_NEWSETELEM);
10066 			if (te->set->ops->commit &&
10067 			    list_empty(&te->set->pending_update)) {
10068 				list_add_tail(&te->set->pending_update,
10069 					      &set_update_list);
10070 			}
10071 			nft_trans_destroy(trans);
10072 			break;
10073 		case NFT_MSG_DELSETELEM:
10074 		case NFT_MSG_DESTROYSETELEM:
10075 			te = (struct nft_trans_elem *)trans->data;
10076 
10077 			nf_tables_setelem_notify(&trans->ctx, te->set,
10078 						 &te->elem,
10079 						 trans->msg_type);
10080 			nft_setelem_remove(net, te->set, &te->elem);
10081 			if (!nft_setelem_is_catchall(te->set, &te->elem)) {
10082 				atomic_dec(&te->set->nelems);
10083 				te->set->ndeact--;
10084 			}
10085 			if (te->set->ops->commit &&
10086 			    list_empty(&te->set->pending_update)) {
10087 				list_add_tail(&te->set->pending_update,
10088 					      &set_update_list);
10089 			}
10090 			break;
10091 		case NFT_MSG_NEWOBJ:
10092 			if (nft_trans_obj_update(trans)) {
10093 				nft_obj_commit_update(trans);
10094 				nf_tables_obj_notify(&trans->ctx,
10095 						     nft_trans_obj(trans),
10096 						     NFT_MSG_NEWOBJ);
10097 			} else {
10098 				nft_clear(net, nft_trans_obj(trans));
10099 				nf_tables_obj_notify(&trans->ctx,
10100 						     nft_trans_obj(trans),
10101 						     NFT_MSG_NEWOBJ);
10102 				nft_trans_destroy(trans);
10103 			}
10104 			break;
10105 		case NFT_MSG_DELOBJ:
10106 		case NFT_MSG_DESTROYOBJ:
10107 			nft_obj_del(nft_trans_obj(trans));
10108 			nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
10109 					     trans->msg_type);
10110 			break;
10111 		case NFT_MSG_NEWFLOWTABLE:
10112 			if (nft_trans_flowtable_update(trans)) {
10113 				nft_trans_flowtable(trans)->data.flags =
10114 					nft_trans_flowtable_flags(trans);
10115 				nf_tables_flowtable_notify(&trans->ctx,
10116 							   nft_trans_flowtable(trans),
10117 							   &nft_trans_flowtable_hooks(trans),
10118 							   NFT_MSG_NEWFLOWTABLE);
10119 				list_splice(&nft_trans_flowtable_hooks(trans),
10120 					    &nft_trans_flowtable(trans)->hook_list);
10121 			} else {
10122 				nft_clear(net, nft_trans_flowtable(trans));
10123 				nf_tables_flowtable_notify(&trans->ctx,
10124 							   nft_trans_flowtable(trans),
10125 							   NULL,
10126 							   NFT_MSG_NEWFLOWTABLE);
10127 			}
10128 			nft_trans_destroy(trans);
10129 			break;
10130 		case NFT_MSG_DELFLOWTABLE:
10131 		case NFT_MSG_DESTROYFLOWTABLE:
10132 			if (nft_trans_flowtable_update(trans)) {
10133 				nf_tables_flowtable_notify(&trans->ctx,
10134 							   nft_trans_flowtable(trans),
10135 							   &nft_trans_flowtable_hooks(trans),
10136 							   trans->msg_type);
10137 				nft_unregister_flowtable_net_hooks(net,
10138 								   &nft_trans_flowtable_hooks(trans));
10139 			} else {
10140 				list_del_rcu(&nft_trans_flowtable(trans)->list);
10141 				nf_tables_flowtable_notify(&trans->ctx,
10142 							   nft_trans_flowtable(trans),
10143 							   NULL,
10144 							   trans->msg_type);
10145 				nft_unregister_flowtable_net_hooks(net,
10146 						&nft_trans_flowtable(trans)->hook_list);
10147 			}
10148 			break;
10149 		}
10150 	}
10151 
10152 	nft_set_commit_update(&set_update_list);
10153 
10154 	nft_commit_notify(net, NETLINK_CB(skb).portid);
10155 	nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
10156 	nf_tables_commit_audit_log(&adl, nft_net->base_seq);
10157 
10158 	nft_gc_seq_end(nft_net, gc_seq);
10159 	nft_net->validate_state = NFT_VALIDATE_SKIP;
10160 	nf_tables_commit_release(net);
10161 
10162 	return 0;
10163 }
10164 
nf_tables_module_autoload(struct net * net)10165 static void nf_tables_module_autoload(struct net *net)
10166 {
10167 	struct nftables_pernet *nft_net = nft_pernet(net);
10168 	struct nft_module_request *req, *next;
10169 	LIST_HEAD(module_list);
10170 
10171 	list_splice_init(&nft_net->module_list, &module_list);
10172 	mutex_unlock(&nft_net->commit_mutex);
10173 	list_for_each_entry_safe(req, next, &module_list, list) {
10174 		request_module("%s", req->module);
10175 		req->done = true;
10176 	}
10177 	mutex_lock(&nft_net->commit_mutex);
10178 	list_splice(&module_list, &nft_net->module_list);
10179 }
10180 
nf_tables_abort_release(struct nft_trans * trans)10181 static void nf_tables_abort_release(struct nft_trans *trans)
10182 {
10183 	switch (trans->msg_type) {
10184 	case NFT_MSG_NEWTABLE:
10185 		nf_tables_table_destroy(&trans->ctx);
10186 		break;
10187 	case NFT_MSG_NEWCHAIN:
10188 		if (nft_trans_chain_update(trans))
10189 			nft_hooks_destroy(&nft_trans_chain_hooks(trans));
10190 		else
10191 			nf_tables_chain_destroy(&trans->ctx);
10192 		break;
10193 	case NFT_MSG_NEWRULE:
10194 		nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
10195 		break;
10196 	case NFT_MSG_NEWSET:
10197 		nft_set_destroy(&trans->ctx, nft_trans_set(trans));
10198 		break;
10199 	case NFT_MSG_NEWSETELEM:
10200 		nft_set_elem_destroy(nft_trans_elem_set(trans),
10201 				     nft_trans_elem(trans).priv, true);
10202 		break;
10203 	case NFT_MSG_NEWOBJ:
10204 		nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
10205 		break;
10206 	case NFT_MSG_NEWFLOWTABLE:
10207 		if (nft_trans_flowtable_update(trans))
10208 			nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
10209 		else
10210 			nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
10211 		break;
10212 	}
10213 	kfree(trans);
10214 }
10215 
nft_set_abort_update(struct list_head * set_update_list)10216 static void nft_set_abort_update(struct list_head *set_update_list)
10217 {
10218 	struct nft_set *set, *next;
10219 
10220 	list_for_each_entry_safe(set, next, set_update_list, pending_update) {
10221 		list_del_init(&set->pending_update);
10222 
10223 		if (!set->ops->abort)
10224 			continue;
10225 
10226 		set->ops->abort(set);
10227 	}
10228 }
10229 
__nf_tables_abort(struct net * net,enum nfnl_abort_action action)10230 static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
10231 {
10232 	struct nftables_pernet *nft_net = nft_pernet(net);
10233 	struct nft_trans *trans, *next;
10234 	LIST_HEAD(set_update_list);
10235 	struct nft_trans_elem *te;
10236 
10237 	if (action == NFNL_ABORT_VALIDATE &&
10238 	    nf_tables_validate(net) < 0)
10239 		return -EAGAIN;
10240 
10241 	list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
10242 					 list) {
10243 		switch (trans->msg_type) {
10244 		case NFT_MSG_NEWTABLE:
10245 			if (nft_trans_table_update(trans)) {
10246 				if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
10247 					nft_trans_destroy(trans);
10248 					break;
10249 				}
10250 				if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) {
10251 					nf_tables_table_disable(net, trans->ctx.table);
10252 					trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
10253 				} else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
10254 					trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT;
10255 				}
10256 				trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
10257 				nft_trans_destroy(trans);
10258 			} else {
10259 				list_del_rcu(&trans->ctx.table->list);
10260 			}
10261 			break;
10262 		case NFT_MSG_DELTABLE:
10263 		case NFT_MSG_DESTROYTABLE:
10264 			nft_clear(trans->ctx.net, trans->ctx.table);
10265 			nft_trans_destroy(trans);
10266 			break;
10267 		case NFT_MSG_NEWCHAIN:
10268 			if (nft_trans_chain_update(trans)) {
10269 				nft_netdev_unregister_hooks(net,
10270 							    &nft_trans_chain_hooks(trans),
10271 							    true);
10272 				free_percpu(nft_trans_chain_stats(trans));
10273 				kfree(nft_trans_chain_name(trans));
10274 				nft_trans_destroy(trans);
10275 			} else {
10276 				if (nft_trans_chain_bound(trans)) {
10277 					nft_trans_destroy(trans);
10278 					break;
10279 				}
10280 				nft_use_dec_restore(&trans->ctx.table->use);
10281 				nft_chain_del(trans->ctx.chain);
10282 				nf_tables_unregister_hook(trans->ctx.net,
10283 							  trans->ctx.table,
10284 							  trans->ctx.chain);
10285 			}
10286 			break;
10287 		case NFT_MSG_DELCHAIN:
10288 		case NFT_MSG_DESTROYCHAIN:
10289 			if (nft_trans_chain_update(trans)) {
10290 				list_splice(&nft_trans_chain_hooks(trans),
10291 					    &nft_trans_basechain(trans)->hook_list);
10292 			} else {
10293 				nft_use_inc_restore(&trans->ctx.table->use);
10294 				nft_clear(trans->ctx.net, trans->ctx.chain);
10295 			}
10296 			nft_trans_destroy(trans);
10297 			break;
10298 		case NFT_MSG_NEWRULE:
10299 			if (nft_trans_rule_bound(trans)) {
10300 				nft_trans_destroy(trans);
10301 				break;
10302 			}
10303 			nft_use_dec_restore(&trans->ctx.chain->use);
10304 			list_del_rcu(&nft_trans_rule(trans)->list);
10305 			nft_rule_expr_deactivate(&trans->ctx,
10306 						 nft_trans_rule(trans),
10307 						 NFT_TRANS_ABORT);
10308 			if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10309 				nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10310 			break;
10311 		case NFT_MSG_DELRULE:
10312 		case NFT_MSG_DESTROYRULE:
10313 			nft_use_inc_restore(&trans->ctx.chain->use);
10314 			nft_clear(trans->ctx.net, nft_trans_rule(trans));
10315 			nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
10316 			if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10317 				nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10318 
10319 			nft_trans_destroy(trans);
10320 			break;
10321 		case NFT_MSG_NEWSET:
10322 			if (nft_trans_set_update(trans)) {
10323 				nft_trans_destroy(trans);
10324 				break;
10325 			}
10326 			nft_use_dec_restore(&trans->ctx.table->use);
10327 			if (nft_trans_set_bound(trans)) {
10328 				nft_trans_destroy(trans);
10329 				break;
10330 			}
10331 			list_del_rcu(&nft_trans_set(trans)->list);
10332 			break;
10333 		case NFT_MSG_DELSET:
10334 		case NFT_MSG_DESTROYSET:
10335 			nft_use_inc_restore(&trans->ctx.table->use);
10336 			nft_clear(trans->ctx.net, nft_trans_set(trans));
10337 			if (nft_trans_set(trans)->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
10338 				nft_map_activate(&trans->ctx, nft_trans_set(trans));
10339 
10340 			nft_trans_destroy(trans);
10341 			break;
10342 		case NFT_MSG_NEWSETELEM:
10343 			if (nft_trans_elem_set_bound(trans)) {
10344 				nft_trans_destroy(trans);
10345 				break;
10346 			}
10347 			te = (struct nft_trans_elem *)trans->data;
10348 			nft_setelem_remove(net, te->set, &te->elem);
10349 			if (!nft_setelem_is_catchall(te->set, &te->elem))
10350 				atomic_dec(&te->set->nelems);
10351 
10352 			if (te->set->ops->abort &&
10353 			    list_empty(&te->set->pending_update)) {
10354 				list_add_tail(&te->set->pending_update,
10355 					      &set_update_list);
10356 			}
10357 			break;
10358 		case NFT_MSG_DELSETELEM:
10359 		case NFT_MSG_DESTROYSETELEM:
10360 			te = (struct nft_trans_elem *)trans->data;
10361 
10362 			nft_setelem_data_activate(net, te->set, &te->elem);
10363 			nft_setelem_activate(net, te->set, &te->elem);
10364 			if (!nft_setelem_is_catchall(te->set, &te->elem))
10365 				te->set->ndeact--;
10366 
10367 			if (te->set->ops->abort &&
10368 			    list_empty(&te->set->pending_update)) {
10369 				list_add_tail(&te->set->pending_update,
10370 					      &set_update_list);
10371 			}
10372 			nft_trans_destroy(trans);
10373 			break;
10374 		case NFT_MSG_NEWOBJ:
10375 			if (nft_trans_obj_update(trans)) {
10376 				nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans));
10377 				nft_trans_destroy(trans);
10378 			} else {
10379 				nft_use_dec_restore(&trans->ctx.table->use);
10380 				nft_obj_del(nft_trans_obj(trans));
10381 			}
10382 			break;
10383 		case NFT_MSG_DELOBJ:
10384 		case NFT_MSG_DESTROYOBJ:
10385 			nft_use_inc_restore(&trans->ctx.table->use);
10386 			nft_clear(trans->ctx.net, nft_trans_obj(trans));
10387 			nft_trans_destroy(trans);
10388 			break;
10389 		case NFT_MSG_NEWFLOWTABLE:
10390 			if (nft_trans_flowtable_update(trans)) {
10391 				nft_unregister_flowtable_net_hooks(net,
10392 						&nft_trans_flowtable_hooks(trans));
10393 			} else {
10394 				nft_use_dec_restore(&trans->ctx.table->use);
10395 				list_del_rcu(&nft_trans_flowtable(trans)->list);
10396 				nft_unregister_flowtable_net_hooks(net,
10397 						&nft_trans_flowtable(trans)->hook_list);
10398 			}
10399 			break;
10400 		case NFT_MSG_DELFLOWTABLE:
10401 		case NFT_MSG_DESTROYFLOWTABLE:
10402 			if (nft_trans_flowtable_update(trans)) {
10403 				list_splice(&nft_trans_flowtable_hooks(trans),
10404 					    &nft_trans_flowtable(trans)->hook_list);
10405 			} else {
10406 				nft_use_inc_restore(&trans->ctx.table->use);
10407 				nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
10408 			}
10409 			nft_trans_destroy(trans);
10410 			break;
10411 		}
10412 	}
10413 
10414 	nft_set_abort_update(&set_update_list);
10415 
10416 	synchronize_rcu();
10417 
10418 	list_for_each_entry_safe_reverse(trans, next,
10419 					 &nft_net->commit_list, list) {
10420 		nft_trans_list_del(trans);
10421 		nf_tables_abort_release(trans);
10422 	}
10423 
10424 	if (action == NFNL_ABORT_AUTOLOAD)
10425 		nf_tables_module_autoload(net);
10426 	else
10427 		nf_tables_module_autoload_cleanup(net);
10428 
10429 	return 0;
10430 }
10431 
nf_tables_abort(struct net * net,struct sk_buff * skb,enum nfnl_abort_action action)10432 static int nf_tables_abort(struct net *net, struct sk_buff *skb,
10433 			   enum nfnl_abort_action action)
10434 {
10435 	struct nftables_pernet *nft_net = nft_pernet(net);
10436 	unsigned int gc_seq;
10437 	int ret;
10438 
10439 	gc_seq = nft_gc_seq_begin(nft_net);
10440 	ret = __nf_tables_abort(net, action);
10441 	nft_gc_seq_end(nft_net, gc_seq);
10442 	mutex_unlock(&nft_net->commit_mutex);
10443 
10444 	return ret;
10445 }
10446 
nf_tables_valid_genid(struct net * net,u32 genid)10447 static bool nf_tables_valid_genid(struct net *net, u32 genid)
10448 {
10449 	struct nftables_pernet *nft_net = nft_pernet(net);
10450 	bool genid_ok;
10451 
10452 	mutex_lock(&nft_net->commit_mutex);
10453 
10454 	genid_ok = genid == 0 || nft_net->base_seq == genid;
10455 	if (!genid_ok)
10456 		mutex_unlock(&nft_net->commit_mutex);
10457 
10458 	/* else, commit mutex has to be released by commit or abort function */
10459 	return genid_ok;
10460 }
10461 
10462 static const struct nfnetlink_subsystem nf_tables_subsys = {
10463 	.name		= "nf_tables",
10464 	.subsys_id	= NFNL_SUBSYS_NFTABLES,
10465 	.cb_count	= NFT_MSG_MAX,
10466 	.cb		= nf_tables_cb,
10467 	.commit		= nf_tables_commit,
10468 	.abort		= nf_tables_abort,
10469 	.valid_genid	= nf_tables_valid_genid,
10470 	.owner		= THIS_MODULE,
10471 };
10472 
nft_chain_validate_dependency(const struct nft_chain * chain,enum nft_chain_types type)10473 int nft_chain_validate_dependency(const struct nft_chain *chain,
10474 				  enum nft_chain_types type)
10475 {
10476 	const struct nft_base_chain *basechain;
10477 
10478 	if (nft_is_base_chain(chain)) {
10479 		basechain = nft_base_chain(chain);
10480 		if (basechain->type->type != type)
10481 			return -EOPNOTSUPP;
10482 	}
10483 	return 0;
10484 }
10485 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
10486 
nft_chain_validate_hooks(const struct nft_chain * chain,unsigned int hook_flags)10487 int nft_chain_validate_hooks(const struct nft_chain *chain,
10488 			     unsigned int hook_flags)
10489 {
10490 	struct nft_base_chain *basechain;
10491 
10492 	if (nft_is_base_chain(chain)) {
10493 		basechain = nft_base_chain(chain);
10494 
10495 		if ((1 << basechain->ops.hooknum) & hook_flags)
10496 			return 0;
10497 
10498 		return -EOPNOTSUPP;
10499 	}
10500 
10501 	return 0;
10502 }
10503 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
10504 
10505 /*
10506  * Loop detection - walk through the ruleset beginning at the destination chain
10507  * of a new jump until either the source chain is reached (loop) or all
10508  * reachable chains have been traversed.
10509  *
10510  * The loop check is performed whenever a new jump verdict is added to an
10511  * expression or verdict map or a verdict map is bound to a new chain.
10512  */
10513 
10514 static int nf_tables_check_loops(const struct nft_ctx *ctx,
10515 				 const struct nft_chain *chain);
10516 
nft_check_loops(const struct nft_ctx * ctx,const struct nft_set_ext * ext)10517 static int nft_check_loops(const struct nft_ctx *ctx,
10518 			   const struct nft_set_ext *ext)
10519 {
10520 	const struct nft_data *data;
10521 	int ret;
10522 
10523 	data = nft_set_ext_data(ext);
10524 	switch (data->verdict.code) {
10525 	case NFT_JUMP:
10526 	case NFT_GOTO:
10527 		ret = nf_tables_check_loops(ctx, data->verdict.chain);
10528 		break;
10529 	default:
10530 		ret = 0;
10531 		break;
10532 	}
10533 
10534 	return ret;
10535 }
10536 
nf_tables_loop_check_setelem(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)10537 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
10538 					struct nft_set *set,
10539 					const struct nft_set_iter *iter,
10540 					struct nft_set_elem *elem)
10541 {
10542 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
10543 
10544 	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
10545 	    *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
10546 		return 0;
10547 
10548 	return nft_check_loops(ctx, ext);
10549 }
10550 
nft_set_catchall_loops(const struct nft_ctx * ctx,struct nft_set * set)10551 static int nft_set_catchall_loops(const struct nft_ctx *ctx,
10552 				  struct nft_set *set)
10553 {
10554 	u8 genmask = nft_genmask_next(ctx->net);
10555 	struct nft_set_elem_catchall *catchall;
10556 	struct nft_set_ext *ext;
10557 	int ret = 0;
10558 
10559 	list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
10560 		ext = nft_set_elem_ext(set, catchall->elem);
10561 		if (!nft_set_elem_active(ext, genmask))
10562 			continue;
10563 
10564 		ret = nft_check_loops(ctx, ext);
10565 		if (ret < 0)
10566 			return ret;
10567 	}
10568 
10569 	return ret;
10570 }
10571 
nf_tables_check_loops(const struct nft_ctx * ctx,const struct nft_chain * chain)10572 static int nf_tables_check_loops(const struct nft_ctx *ctx,
10573 				 const struct nft_chain *chain)
10574 {
10575 	const struct nft_rule *rule;
10576 	const struct nft_expr *expr, *last;
10577 	struct nft_set *set;
10578 	struct nft_set_binding *binding;
10579 	struct nft_set_iter iter;
10580 
10581 	if (ctx->chain == chain)
10582 		return -ELOOP;
10583 
10584 	if (fatal_signal_pending(current))
10585 		return -EINTR;
10586 
10587 	list_for_each_entry(rule, &chain->rules, list) {
10588 		nft_rule_for_each_expr(expr, last, rule) {
10589 			struct nft_immediate_expr *priv;
10590 			const struct nft_data *data;
10591 			int err;
10592 
10593 			if (strcmp(expr->ops->type->name, "immediate"))
10594 				continue;
10595 
10596 			priv = nft_expr_priv(expr);
10597 			if (priv->dreg != NFT_REG_VERDICT)
10598 				continue;
10599 
10600 			data = &priv->data;
10601 			switch (data->verdict.code) {
10602 			case NFT_JUMP:
10603 			case NFT_GOTO:
10604 				err = nf_tables_check_loops(ctx,
10605 							data->verdict.chain);
10606 				if (err < 0)
10607 					return err;
10608 				break;
10609 			default:
10610 				break;
10611 			}
10612 		}
10613 	}
10614 
10615 	list_for_each_entry(set, &ctx->table->sets, list) {
10616 		if (!nft_is_active_next(ctx->net, set))
10617 			continue;
10618 		if (!(set->flags & NFT_SET_MAP) ||
10619 		    set->dtype != NFT_DATA_VERDICT)
10620 			continue;
10621 
10622 		list_for_each_entry(binding, &set->bindings, list) {
10623 			if (!(binding->flags & NFT_SET_MAP) ||
10624 			    binding->chain != chain)
10625 				continue;
10626 
10627 			iter.genmask	= nft_genmask_next(ctx->net);
10628 			iter.skip 	= 0;
10629 			iter.count	= 0;
10630 			iter.err	= 0;
10631 			iter.fn		= nf_tables_loop_check_setelem;
10632 
10633 			set->ops->walk(ctx, set, &iter);
10634 			if (!iter.err)
10635 				iter.err = nft_set_catchall_loops(ctx, set);
10636 
10637 			if (iter.err < 0)
10638 				return iter.err;
10639 		}
10640 	}
10641 
10642 	return 0;
10643 }
10644 
10645 /**
10646  *	nft_parse_u32_check - fetch u32 attribute and check for maximum value
10647  *
10648  *	@attr: netlink attribute to fetch value from
10649  *	@max: maximum value to be stored in dest
10650  *	@dest: pointer to the variable
10651  *
10652  *	Parse, check and store a given u32 netlink attribute into variable.
10653  *	This function returns -ERANGE if the value goes over maximum value.
10654  *	Otherwise a 0 is returned and the attribute value is stored in the
10655  *	destination variable.
10656  */
nft_parse_u32_check(const struct nlattr * attr,int max,u32 * dest)10657 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
10658 {
10659 	u32 val;
10660 
10661 	val = ntohl(nla_get_be32(attr));
10662 	if (val > max)
10663 		return -ERANGE;
10664 
10665 	*dest = val;
10666 	return 0;
10667 }
10668 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
10669 
nft_parse_register(const struct nlattr * attr,u32 * preg)10670 static int nft_parse_register(const struct nlattr *attr, u32 *preg)
10671 {
10672 	unsigned int reg;
10673 
10674 	reg = ntohl(nla_get_be32(attr));
10675 	switch (reg) {
10676 	case NFT_REG_VERDICT...NFT_REG_4:
10677 		*preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE;
10678 		break;
10679 	case NFT_REG32_00...NFT_REG32_15:
10680 		*preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
10681 		break;
10682 	default:
10683 		return -ERANGE;
10684 	}
10685 
10686 	return 0;
10687 }
10688 
10689 /**
10690  *	nft_dump_register - dump a register value to a netlink attribute
10691  *
10692  *	@skb: socket buffer
10693  *	@attr: attribute number
10694  *	@reg: register number
10695  *
10696  *	Construct a netlink attribute containing the register number. For
10697  *	compatibility reasons, register numbers being a multiple of 4 are
10698  *	translated to the corresponding 128 bit register numbers.
10699  */
nft_dump_register(struct sk_buff * skb,unsigned int attr,unsigned int reg)10700 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
10701 {
10702 	if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
10703 		reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
10704 	else
10705 		reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
10706 
10707 	return nla_put_be32(skb, attr, htonl(reg));
10708 }
10709 EXPORT_SYMBOL_GPL(nft_dump_register);
10710 
nft_validate_register_load(enum nft_registers reg,unsigned int len)10711 static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
10712 {
10713 	if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
10714 		return -EINVAL;
10715 	if (len == 0)
10716 		return -EINVAL;
10717 	if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
10718 		return -ERANGE;
10719 
10720 	return 0;
10721 }
10722 
nft_parse_register_load(const struct nlattr * attr,u8 * sreg,u32 len)10723 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len)
10724 {
10725 	u32 reg;
10726 	int err;
10727 
10728 	err = nft_parse_register(attr, &reg);
10729 	if (err < 0)
10730 		return err;
10731 
10732 	err = nft_validate_register_load(reg, len);
10733 	if (err < 0)
10734 		return err;
10735 
10736 	*sreg = reg;
10737 	return 0;
10738 }
10739 EXPORT_SYMBOL_GPL(nft_parse_register_load);
10740 
nft_validate_register_store(const struct nft_ctx * ctx,enum nft_registers reg,const struct nft_data * data,enum nft_data_types type,unsigned int len)10741 static int nft_validate_register_store(const struct nft_ctx *ctx,
10742 				       enum nft_registers reg,
10743 				       const struct nft_data *data,
10744 				       enum nft_data_types type,
10745 				       unsigned int len)
10746 {
10747 	int err;
10748 
10749 	switch (reg) {
10750 	case NFT_REG_VERDICT:
10751 		if (type != NFT_DATA_VERDICT)
10752 			return -EINVAL;
10753 
10754 		if (data != NULL &&
10755 		    (data->verdict.code == NFT_GOTO ||
10756 		     data->verdict.code == NFT_JUMP)) {
10757 			err = nf_tables_check_loops(ctx, data->verdict.chain);
10758 			if (err < 0)
10759 				return err;
10760 		}
10761 
10762 		return 0;
10763 	default:
10764 		if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
10765 			return -EINVAL;
10766 		if (len == 0)
10767 			return -EINVAL;
10768 		if (reg * NFT_REG32_SIZE + len >
10769 		    sizeof_field(struct nft_regs, data))
10770 			return -ERANGE;
10771 
10772 		if (data != NULL && type != NFT_DATA_VALUE)
10773 			return -EINVAL;
10774 		return 0;
10775 	}
10776 }
10777 
nft_parse_register_store(const struct nft_ctx * ctx,const struct nlattr * attr,u8 * dreg,const struct nft_data * data,enum nft_data_types type,unsigned int len)10778 int nft_parse_register_store(const struct nft_ctx *ctx,
10779 			     const struct nlattr *attr, u8 *dreg,
10780 			     const struct nft_data *data,
10781 			     enum nft_data_types type, unsigned int len)
10782 {
10783 	int err;
10784 	u32 reg;
10785 
10786 	err = nft_parse_register(attr, &reg);
10787 	if (err < 0)
10788 		return err;
10789 
10790 	err = nft_validate_register_store(ctx, reg, data, type, len);
10791 	if (err < 0)
10792 		return err;
10793 
10794 	*dreg = reg;
10795 	return 0;
10796 }
10797 EXPORT_SYMBOL_GPL(nft_parse_register_store);
10798 
10799 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
10800 	[NFTA_VERDICT_CODE]	= { .type = NLA_U32 },
10801 	[NFTA_VERDICT_CHAIN]	= { .type = NLA_STRING,
10802 				    .len = NFT_CHAIN_MAXNAMELEN - 1 },
10803 	[NFTA_VERDICT_CHAIN_ID]	= { .type = NLA_U32 },
10804 };
10805 
nft_verdict_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)10806 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
10807 			    struct nft_data_desc *desc, const struct nlattr *nla)
10808 {
10809 	u8 genmask = nft_genmask_next(ctx->net);
10810 	struct nlattr *tb[NFTA_VERDICT_MAX + 1];
10811 	struct nft_chain *chain;
10812 	int err;
10813 
10814 	err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
10815 					  nft_verdict_policy, NULL);
10816 	if (err < 0)
10817 		return err;
10818 
10819 	if (!tb[NFTA_VERDICT_CODE])
10820 		return -EINVAL;
10821 
10822 	/* zero padding hole for memcmp */
10823 	memset(data, 0, sizeof(*data));
10824 	data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
10825 
10826 	switch (data->verdict.code) {
10827 	default:
10828 		switch (data->verdict.code & NF_VERDICT_MASK) {
10829 		case NF_ACCEPT:
10830 		case NF_DROP:
10831 		case NF_QUEUE:
10832 			break;
10833 		default:
10834 			return -EINVAL;
10835 		}
10836 		fallthrough;
10837 	case NFT_CONTINUE:
10838 	case NFT_BREAK:
10839 	case NFT_RETURN:
10840 		break;
10841 	case NFT_JUMP:
10842 	case NFT_GOTO:
10843 		if (tb[NFTA_VERDICT_CHAIN]) {
10844 			chain = nft_chain_lookup(ctx->net, ctx->table,
10845 						 tb[NFTA_VERDICT_CHAIN],
10846 						 genmask);
10847 		} else if (tb[NFTA_VERDICT_CHAIN_ID]) {
10848 			chain = nft_chain_lookup_byid(ctx->net, ctx->table,
10849 						      tb[NFTA_VERDICT_CHAIN_ID],
10850 						      genmask);
10851 			if (IS_ERR(chain))
10852 				return PTR_ERR(chain);
10853 		} else {
10854 			return -EINVAL;
10855 		}
10856 
10857 		if (IS_ERR(chain))
10858 			return PTR_ERR(chain);
10859 		if (nft_is_base_chain(chain))
10860 			return -EOPNOTSUPP;
10861 		if (nft_chain_is_bound(chain))
10862 			return -EINVAL;
10863 		if (desc->flags & NFT_DATA_DESC_SETELEM &&
10864 		    chain->flags & NFT_CHAIN_BINDING)
10865 			return -EINVAL;
10866 		if (!nft_use_inc(&chain->use))
10867 			return -EMFILE;
10868 
10869 		data->verdict.chain = chain;
10870 		break;
10871 	}
10872 
10873 	desc->len = sizeof(data->verdict);
10874 
10875 	return 0;
10876 }
10877 
nft_verdict_uninit(const struct nft_data * data)10878 static void nft_verdict_uninit(const struct nft_data *data)
10879 {
10880 	struct nft_chain *chain;
10881 
10882 	switch (data->verdict.code) {
10883 	case NFT_JUMP:
10884 	case NFT_GOTO:
10885 		chain = data->verdict.chain;
10886 		nft_use_dec(&chain->use);
10887 		break;
10888 	}
10889 }
10890 
nft_verdict_dump(struct sk_buff * skb,int type,const struct nft_verdict * v)10891 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
10892 {
10893 	struct nlattr *nest;
10894 
10895 	nest = nla_nest_start_noflag(skb, type);
10896 	if (!nest)
10897 		goto nla_put_failure;
10898 
10899 	if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
10900 		goto nla_put_failure;
10901 
10902 	switch (v->code) {
10903 	case NFT_JUMP:
10904 	case NFT_GOTO:
10905 		if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
10906 				   v->chain->name))
10907 			goto nla_put_failure;
10908 	}
10909 	nla_nest_end(skb, nest);
10910 	return 0;
10911 
10912 nla_put_failure:
10913 	return -1;
10914 }
10915 
nft_value_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)10916 static int nft_value_init(const struct nft_ctx *ctx,
10917 			  struct nft_data *data, struct nft_data_desc *desc,
10918 			  const struct nlattr *nla)
10919 {
10920 	unsigned int len;
10921 
10922 	len = nla_len(nla);
10923 	if (len == 0)
10924 		return -EINVAL;
10925 	if (len > desc->size)
10926 		return -EOVERFLOW;
10927 	if (desc->len) {
10928 		if (len != desc->len)
10929 			return -EINVAL;
10930 	} else {
10931 		desc->len = len;
10932 	}
10933 
10934 	nla_memcpy(data->data, nla, len);
10935 
10936 	return 0;
10937 }
10938 
nft_value_dump(struct sk_buff * skb,const struct nft_data * data,unsigned int len)10939 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
10940 			  unsigned int len)
10941 {
10942 	return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
10943 }
10944 
10945 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
10946 	[NFTA_DATA_VALUE]	= { .type = NLA_BINARY },
10947 	[NFTA_DATA_VERDICT]	= { .type = NLA_NESTED },
10948 };
10949 
10950 /**
10951  *	nft_data_init - parse nf_tables data netlink attributes
10952  *
10953  *	@ctx: context of the expression using the data
10954  *	@data: destination struct nft_data
10955  *	@desc: data description
10956  *	@nla: netlink attribute containing data
10957  *
10958  *	Parse the netlink data attributes and initialize a struct nft_data.
10959  *	The type and length of data are returned in the data description.
10960  *
10961  *	The caller can indicate that it only wants to accept data of type
10962  *	NFT_DATA_VALUE by passing NULL for the ctx argument.
10963  */
nft_data_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)10964 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
10965 		  struct nft_data_desc *desc, const struct nlattr *nla)
10966 {
10967 	struct nlattr *tb[NFTA_DATA_MAX + 1];
10968 	int err;
10969 
10970 	if (WARN_ON_ONCE(!desc->size))
10971 		return -EINVAL;
10972 
10973 	err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
10974 					  nft_data_policy, NULL);
10975 	if (err < 0)
10976 		return err;
10977 
10978 	if (tb[NFTA_DATA_VALUE]) {
10979 		if (desc->type != NFT_DATA_VALUE)
10980 			return -EINVAL;
10981 
10982 		err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
10983 	} else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) {
10984 		if (desc->type != NFT_DATA_VERDICT)
10985 			return -EINVAL;
10986 
10987 		err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
10988 	} else {
10989 		err = -EINVAL;
10990 	}
10991 
10992 	return err;
10993 }
10994 EXPORT_SYMBOL_GPL(nft_data_init);
10995 
10996 /**
10997  *	nft_data_release - release a nft_data item
10998  *
10999  *	@data: struct nft_data to release
11000  *	@type: type of data
11001  *
11002  *	Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
11003  *	all others need to be released by calling this function.
11004  */
nft_data_release(const struct nft_data * data,enum nft_data_types type)11005 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
11006 {
11007 	if (type < NFT_DATA_VERDICT)
11008 		return;
11009 	switch (type) {
11010 	case NFT_DATA_VERDICT:
11011 		return nft_verdict_uninit(data);
11012 	default:
11013 		WARN_ON(1);
11014 	}
11015 }
11016 EXPORT_SYMBOL_GPL(nft_data_release);
11017 
nft_data_dump(struct sk_buff * skb,int attr,const struct nft_data * data,enum nft_data_types type,unsigned int len)11018 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
11019 		  enum nft_data_types type, unsigned int len)
11020 {
11021 	struct nlattr *nest;
11022 	int err;
11023 
11024 	nest = nla_nest_start_noflag(skb, attr);
11025 	if (nest == NULL)
11026 		return -1;
11027 
11028 	switch (type) {
11029 	case NFT_DATA_VALUE:
11030 		err = nft_value_dump(skb, data, len);
11031 		break;
11032 	case NFT_DATA_VERDICT:
11033 		err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
11034 		break;
11035 	default:
11036 		err = -EINVAL;
11037 		WARN_ON(1);
11038 	}
11039 
11040 	nla_nest_end(skb, nest);
11041 	return err;
11042 }
11043 EXPORT_SYMBOL_GPL(nft_data_dump);
11044 
__nft_release_basechain(struct nft_ctx * ctx)11045 int __nft_release_basechain(struct nft_ctx *ctx)
11046 {
11047 	struct nft_rule *rule, *nr;
11048 
11049 	if (WARN_ON(!nft_is_base_chain(ctx->chain)))
11050 		return 0;
11051 
11052 	nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
11053 	list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
11054 		list_del(&rule->list);
11055 		nft_use_dec(&ctx->chain->use);
11056 		nf_tables_rule_release(ctx, rule);
11057 	}
11058 	nft_chain_del(ctx->chain);
11059 	nft_use_dec(&ctx->table->use);
11060 	nf_tables_chain_destroy(ctx);
11061 
11062 	return 0;
11063 }
11064 EXPORT_SYMBOL_GPL(__nft_release_basechain);
11065 
__nft_release_hook(struct net * net,struct nft_table * table)11066 static void __nft_release_hook(struct net *net, struct nft_table *table)
11067 {
11068 	struct nft_flowtable *flowtable;
11069 	struct nft_chain *chain;
11070 
11071 	list_for_each_entry(chain, &table->chains, list)
11072 		__nf_tables_unregister_hook(net, table, chain, true);
11073 	list_for_each_entry(flowtable, &table->flowtables, list)
11074 		__nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list,
11075 						     true);
11076 }
11077 
__nft_release_hooks(struct net * net)11078 static void __nft_release_hooks(struct net *net)
11079 {
11080 	struct nftables_pernet *nft_net = nft_pernet(net);
11081 	struct nft_table *table;
11082 
11083 	list_for_each_entry(table, &nft_net->tables, list) {
11084 		if (nft_table_has_owner(table))
11085 			continue;
11086 
11087 		__nft_release_hook(net, table);
11088 	}
11089 }
11090 
__nft_release_table(struct net * net,struct nft_table * table)11091 static void __nft_release_table(struct net *net, struct nft_table *table)
11092 {
11093 	struct nft_flowtable *flowtable, *nf;
11094 	struct nft_chain *chain, *nc;
11095 	struct nft_object *obj, *ne;
11096 	struct nft_rule *rule, *nr;
11097 	struct nft_set *set, *ns;
11098 	struct nft_ctx ctx = {
11099 		.net	= net,
11100 		.family	= NFPROTO_NETDEV,
11101 	};
11102 
11103 	ctx.family = table->family;
11104 	ctx.table = table;
11105 	list_for_each_entry(chain, &table->chains, list) {
11106 		if (nft_chain_binding(chain))
11107 			continue;
11108 
11109 		ctx.chain = chain;
11110 		list_for_each_entry_safe(rule, nr, &chain->rules, list) {
11111 			list_del(&rule->list);
11112 			nft_use_dec(&chain->use);
11113 			nf_tables_rule_release(&ctx, rule);
11114 		}
11115 	}
11116 	list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
11117 		list_del(&flowtable->list);
11118 		nft_use_dec(&table->use);
11119 		nf_tables_flowtable_destroy(flowtable);
11120 	}
11121 	list_for_each_entry_safe(set, ns, &table->sets, list) {
11122 		list_del(&set->list);
11123 		nft_use_dec(&table->use);
11124 		if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
11125 			nft_map_deactivate(&ctx, set);
11126 
11127 		nft_set_destroy(&ctx, set);
11128 	}
11129 	list_for_each_entry_safe(obj, ne, &table->objects, list) {
11130 		nft_obj_del(obj);
11131 		nft_use_dec(&table->use);
11132 		nft_obj_destroy(&ctx, obj);
11133 	}
11134 	list_for_each_entry_safe(chain, nc, &table->chains, list) {
11135 		ctx.chain = chain;
11136 		nft_chain_del(chain);
11137 		nft_use_dec(&table->use);
11138 		nf_tables_chain_destroy(&ctx);
11139 	}
11140 	nf_tables_table_destroy(&ctx);
11141 }
11142 
__nft_release_tables(struct net * net)11143 static void __nft_release_tables(struct net *net)
11144 {
11145 	struct nftables_pernet *nft_net = nft_pernet(net);
11146 	struct nft_table *table, *nt;
11147 
11148 	list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
11149 		if (nft_table_has_owner(table))
11150 			continue;
11151 
11152 		list_del(&table->list);
11153 
11154 		__nft_release_table(net, table);
11155 	}
11156 }
11157 
nft_rcv_nl_event(struct notifier_block * this,unsigned long event,void * ptr)11158 static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event,
11159 			    void *ptr)
11160 {
11161 	struct nft_table *table, *to_delete[8];
11162 	struct nftables_pernet *nft_net;
11163 	struct netlink_notify *n = ptr;
11164 	struct net *net = n->net;
11165 	unsigned int deleted;
11166 	bool restart = false;
11167 	unsigned int gc_seq;
11168 
11169 	if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER)
11170 		return NOTIFY_DONE;
11171 
11172 	nft_net = nft_pernet(net);
11173 	deleted = 0;
11174 	mutex_lock(&nft_net->commit_mutex);
11175 
11176 	gc_seq = nft_gc_seq_begin(nft_net);
11177 
11178 	if (!list_empty(&nf_tables_destroy_list))
11179 		nf_tables_trans_destroy_flush_work();
11180 again:
11181 	list_for_each_entry(table, &nft_net->tables, list) {
11182 		if (nft_table_has_owner(table) &&
11183 		    n->portid == table->nlpid) {
11184 			__nft_release_hook(net, table);
11185 			list_del_rcu(&table->list);
11186 			to_delete[deleted++] = table;
11187 			if (deleted >= ARRAY_SIZE(to_delete))
11188 				break;
11189 		}
11190 	}
11191 	if (deleted) {
11192 		restart = deleted >= ARRAY_SIZE(to_delete);
11193 		synchronize_rcu();
11194 		while (deleted)
11195 			__nft_release_table(net, to_delete[--deleted]);
11196 
11197 		if (restart)
11198 			goto again;
11199 	}
11200 	nft_gc_seq_end(nft_net, gc_seq);
11201 
11202 	mutex_unlock(&nft_net->commit_mutex);
11203 
11204 	return NOTIFY_DONE;
11205 }
11206 
11207 static struct notifier_block nft_nl_notifier = {
11208 	.notifier_call  = nft_rcv_nl_event,
11209 };
11210 
nf_tables_init_net(struct net * net)11211 static int __net_init nf_tables_init_net(struct net *net)
11212 {
11213 	struct nftables_pernet *nft_net = nft_pernet(net);
11214 
11215 	INIT_LIST_HEAD(&nft_net->tables);
11216 	INIT_LIST_HEAD(&nft_net->commit_list);
11217 	INIT_LIST_HEAD(&nft_net->binding_list);
11218 	INIT_LIST_HEAD(&nft_net->module_list);
11219 	INIT_LIST_HEAD(&nft_net->notify_list);
11220 	mutex_init(&nft_net->commit_mutex);
11221 	nft_net->base_seq = 1;
11222 	nft_net->gc_seq = 0;
11223 	nft_net->validate_state = NFT_VALIDATE_SKIP;
11224 
11225 	return 0;
11226 }
11227 
nf_tables_pre_exit_net(struct net * net)11228 static void __net_exit nf_tables_pre_exit_net(struct net *net)
11229 {
11230 	struct nftables_pernet *nft_net = nft_pernet(net);
11231 
11232 	mutex_lock(&nft_net->commit_mutex);
11233 	__nft_release_hooks(net);
11234 	mutex_unlock(&nft_net->commit_mutex);
11235 }
11236 
nf_tables_exit_net(struct net * net)11237 static void __net_exit nf_tables_exit_net(struct net *net)
11238 {
11239 	struct nftables_pernet *nft_net = nft_pernet(net);
11240 	unsigned int gc_seq;
11241 
11242 	mutex_lock(&nft_net->commit_mutex);
11243 
11244 	gc_seq = nft_gc_seq_begin(nft_net);
11245 
11246 	if (!list_empty(&nft_net->commit_list) ||
11247 	    !list_empty(&nft_net->module_list))
11248 		__nf_tables_abort(net, NFNL_ABORT_NONE);
11249 
11250 	__nft_release_tables(net);
11251 
11252 	nft_gc_seq_end(nft_net, gc_seq);
11253 
11254 	mutex_unlock(&nft_net->commit_mutex);
11255 	WARN_ON_ONCE(!list_empty(&nft_net->tables));
11256 	WARN_ON_ONCE(!list_empty(&nft_net->module_list));
11257 	WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
11258 }
11259 
nf_tables_exit_batch(struct list_head * net_exit_list)11260 static void nf_tables_exit_batch(struct list_head *net_exit_list)
11261 {
11262 	flush_work(&trans_gc_work);
11263 }
11264 
11265 static struct pernet_operations nf_tables_net_ops = {
11266 	.init		= nf_tables_init_net,
11267 	.pre_exit	= nf_tables_pre_exit_net,
11268 	.exit		= nf_tables_exit_net,
11269 	.exit_batch	= nf_tables_exit_batch,
11270 	.id		= &nf_tables_net_id,
11271 	.size		= sizeof(struct nftables_pernet),
11272 };
11273 
nf_tables_module_init(void)11274 static int __init nf_tables_module_init(void)
11275 {
11276 	int err;
11277 
11278 	err = register_pernet_subsys(&nf_tables_net_ops);
11279 	if (err < 0)
11280 		return err;
11281 
11282 	err = nft_chain_filter_init();
11283 	if (err < 0)
11284 		goto err_chain_filter;
11285 
11286 	err = nf_tables_core_module_init();
11287 	if (err < 0)
11288 		goto err_core_module;
11289 
11290 	err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
11291 	if (err < 0)
11292 		goto err_netdev_notifier;
11293 
11294 	err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
11295 	if (err < 0)
11296 		goto err_rht_objname;
11297 
11298 	err = nft_offload_init();
11299 	if (err < 0)
11300 		goto err_offload;
11301 
11302 	err = netlink_register_notifier(&nft_nl_notifier);
11303 	if (err < 0)
11304 		goto err_netlink_notifier;
11305 
11306 	/* must be last */
11307 	err = nfnetlink_subsys_register(&nf_tables_subsys);
11308 	if (err < 0)
11309 		goto err_nfnl_subsys;
11310 
11311 	nft_chain_route_init();
11312 
11313 	return err;
11314 
11315 err_nfnl_subsys:
11316 	netlink_unregister_notifier(&nft_nl_notifier);
11317 err_netlink_notifier:
11318 	nft_offload_exit();
11319 err_offload:
11320 	rhltable_destroy(&nft_objname_ht);
11321 err_rht_objname:
11322 	unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
11323 err_netdev_notifier:
11324 	nf_tables_core_module_exit();
11325 err_core_module:
11326 	nft_chain_filter_fini();
11327 err_chain_filter:
11328 	unregister_pernet_subsys(&nf_tables_net_ops);
11329 	return err;
11330 }
11331 
nf_tables_module_exit(void)11332 static void __exit nf_tables_module_exit(void)
11333 {
11334 	nfnetlink_subsys_unregister(&nf_tables_subsys);
11335 	netlink_unregister_notifier(&nft_nl_notifier);
11336 	nft_offload_exit();
11337 	unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
11338 	nft_chain_filter_fini();
11339 	nft_chain_route_fini();
11340 	unregister_pernet_subsys(&nf_tables_net_ops);
11341 	cancel_work_sync(&trans_gc_work);
11342 	cancel_work_sync(&trans_destroy_work);
11343 	rcu_barrier();
11344 	rhltable_destroy(&nft_objname_ht);
11345 	nf_tables_core_module_exit();
11346 }
11347 
11348 module_init(nf_tables_module_init);
11349 module_exit(nf_tables_module_exit);
11350 
11351 MODULE_LICENSE("GPL");
11352 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
11353 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
11354