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