1 /*
2  * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Development of this code funded by Astaro AG (http://www.astaro.com/)
9  */
10 
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
22 #include <net/sock.h>
23 
24 static LIST_HEAD(nf_tables_expressions);
25 
26 /**
27  *	nft_register_afinfo - register nf_tables address family info
28  *
29  *	@afi: address family info to register
30  *
31  *	Register the address family for use with nf_tables. Returns zero on
32  *	success or a negative errno code otherwise.
33  */
nft_register_afinfo(struct net * net,struct nft_af_info * afi)34 int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
35 {
36 	INIT_LIST_HEAD(&afi->tables);
37 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 	list_add_tail_rcu(&afi->list, &net->nft.af_info);
39 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 	return 0;
41 }
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
43 
44 /**
45  *	nft_unregister_afinfo - unregister nf_tables address family info
46  *
47  *	@afi: address family info to unregister
48  *
49  *	Unregister the address family for use with nf_tables.
50  */
nft_unregister_afinfo(struct nft_af_info * afi)51 void nft_unregister_afinfo(struct nft_af_info *afi)
52 {
53 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 	list_del_rcu(&afi->list);
55 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56 }
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58 
nft_afinfo_lookup(struct net * net,int family)59 static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
60 {
61 	struct nft_af_info *afi;
62 
63 	list_for_each_entry(afi, &net->nft.af_info, list) {
64 		if (afi->family == family)
65 			return afi;
66 	}
67 	return NULL;
68 }
69 
70 static struct nft_af_info *
nf_tables_afinfo_lookup(struct net * net,int family,bool autoload)71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
72 {
73 	struct nft_af_info *afi;
74 
75 	afi = nft_afinfo_lookup(net, family);
76 	if (afi != NULL)
77 		return afi;
78 #ifdef CONFIG_MODULES
79 	if (autoload) {
80 		nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 		request_module("nft-afinfo-%u", family);
82 		nfnl_lock(NFNL_SUBSYS_NFTABLES);
83 		afi = nft_afinfo_lookup(net, family);
84 		if (afi != NULL)
85 			return ERR_PTR(-EAGAIN);
86 	}
87 #endif
88 	return ERR_PTR(-EAFNOSUPPORT);
89 }
90 
nft_ctx_init(struct nft_ctx * ctx,struct net * net,const struct sk_buff * skb,const struct nlmsghdr * nlh,struct nft_af_info * afi,struct nft_table * table,struct nft_chain * chain,const struct nlattr * const * nla)91 static void nft_ctx_init(struct nft_ctx *ctx,
92 			 struct net *net,
93 			 const struct sk_buff *skb,
94 			 const struct nlmsghdr *nlh,
95 			 struct nft_af_info *afi,
96 			 struct nft_table *table,
97 			 struct nft_chain *chain,
98 			 const struct nlattr * const *nla)
99 {
100 	ctx->net	= net;
101 	ctx->afi	= afi;
102 	ctx->table	= table;
103 	ctx->chain	= chain;
104 	ctx->nla   	= nla;
105 	ctx->portid	= NETLINK_CB(skb).portid;
106 	ctx->report	= nlmsg_report(nlh);
107 	ctx->seq	= nlh->nlmsg_seq;
108 }
109 
nft_trans_alloc(struct nft_ctx * ctx,int msg_type,u32 size)110 static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
111 					 u32 size)
112 {
113 	struct nft_trans *trans;
114 
115 	trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
116 	if (trans == NULL)
117 		return NULL;
118 
119 	trans->msg_type = msg_type;
120 	trans->ctx	= *ctx;
121 
122 	return trans;
123 }
124 
nft_trans_destroy(struct nft_trans * trans)125 static void nft_trans_destroy(struct nft_trans *trans)
126 {
127 	list_del(&trans->list);
128 	kfree(trans);
129 }
130 
nft_register_basechain(struct nft_base_chain * basechain,unsigned int hook_nops)131 int nft_register_basechain(struct nft_base_chain *basechain,
132 			   unsigned int hook_nops)
133 {
134 	struct net *net = read_pnet(&basechain->pnet);
135 
136 	if (basechain->flags & NFT_BASECHAIN_DISABLED)
137 		return 0;
138 
139 	return nf_register_net_hooks(net, basechain->ops, hook_nops);
140 }
141 EXPORT_SYMBOL_GPL(nft_register_basechain);
142 
nft_unregister_basechain(struct nft_base_chain * basechain,unsigned int hook_nops)143 void nft_unregister_basechain(struct nft_base_chain *basechain,
144 			      unsigned int hook_nops)
145 {
146 	struct net *net = read_pnet(&basechain->pnet);
147 
148 	if (basechain->flags & NFT_BASECHAIN_DISABLED)
149 		return;
150 
151 	nf_unregister_net_hooks(net, basechain->ops, hook_nops);
152 }
153 EXPORT_SYMBOL_GPL(nft_unregister_basechain);
154 
nf_tables_register_hooks(const struct nft_table * table,struct nft_chain * chain,unsigned int hook_nops)155 static int nf_tables_register_hooks(const struct nft_table *table,
156 				    struct nft_chain *chain,
157 				    unsigned int hook_nops)
158 {
159 	if (table->flags & NFT_TABLE_F_DORMANT ||
160 	    !(chain->flags & NFT_BASE_CHAIN))
161 		return 0;
162 
163 	return nft_register_basechain(nft_base_chain(chain), hook_nops);
164 }
165 
nf_tables_unregister_hooks(const struct nft_table * table,struct nft_chain * chain,unsigned int hook_nops)166 static void nf_tables_unregister_hooks(const struct nft_table *table,
167 				       struct nft_chain *chain,
168 				       unsigned int hook_nops)
169 {
170 	if (table->flags & NFT_TABLE_F_DORMANT ||
171 	    !(chain->flags & NFT_BASE_CHAIN))
172 		return;
173 
174 	nft_unregister_basechain(nft_base_chain(chain), hook_nops);
175 }
176 
177 /* Internal table flags */
178 #define NFT_TABLE_INACTIVE	(1 << 15)
179 
nft_trans_table_add(struct nft_ctx * ctx,int msg_type)180 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
181 {
182 	struct nft_trans *trans;
183 
184 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
185 	if (trans == NULL)
186 		return -ENOMEM;
187 
188 	if (msg_type == NFT_MSG_NEWTABLE)
189 		ctx->table->flags |= NFT_TABLE_INACTIVE;
190 
191 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
192 	return 0;
193 }
194 
nft_deltable(struct nft_ctx * ctx)195 static int nft_deltable(struct nft_ctx *ctx)
196 {
197 	int err;
198 
199 	err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
200 	if (err < 0)
201 		return err;
202 
203 	list_del_rcu(&ctx->table->list);
204 	return err;
205 }
206 
nft_trans_chain_add(struct nft_ctx * ctx,int msg_type)207 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
208 {
209 	struct nft_trans *trans;
210 
211 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
212 	if (trans == NULL)
213 		return -ENOMEM;
214 
215 	if (msg_type == NFT_MSG_NEWCHAIN)
216 		ctx->chain->flags |= NFT_CHAIN_INACTIVE;
217 
218 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
219 	return 0;
220 }
221 
nft_delchain(struct nft_ctx * ctx)222 static int nft_delchain(struct nft_ctx *ctx)
223 {
224 	int err;
225 
226 	err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
227 	if (err < 0)
228 		return err;
229 
230 	ctx->table->use--;
231 	list_del_rcu(&ctx->chain->list);
232 
233 	return err;
234 }
235 
236 static inline bool
nft_rule_is_active(struct net * net,const struct nft_rule * rule)237 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
238 {
239 	return (rule->genmask & nft_genmask_cur(net)) == 0;
240 }
241 
242 static inline int
nft_rule_is_active_next(struct net * net,const struct nft_rule * rule)243 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
244 {
245 	return (rule->genmask & nft_genmask_next(net)) == 0;
246 }
247 
248 static inline void
nft_rule_activate_next(struct net * net,struct nft_rule * rule)249 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
250 {
251 	/* Now inactive, will be active in the future */
252 	rule->genmask = nft_genmask_cur(net);
253 }
254 
255 static inline void
nft_rule_deactivate_next(struct net * net,struct nft_rule * rule)256 nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
257 {
258 	rule->genmask = nft_genmask_next(net);
259 }
260 
nft_rule_clear(struct net * net,struct nft_rule * rule)261 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
262 {
263 	rule->genmask &= ~nft_genmask_next(net);
264 }
265 
266 static int
nf_tables_delrule_deactivate(struct nft_ctx * ctx,struct nft_rule * rule)267 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
268 {
269 	/* You cannot delete the same rule twice */
270 	if (nft_rule_is_active_next(ctx->net, rule)) {
271 		nft_rule_deactivate_next(ctx->net, rule);
272 		ctx->chain->use--;
273 		return 0;
274 	}
275 	return -ENOENT;
276 }
277 
nft_trans_rule_add(struct nft_ctx * ctx,int msg_type,struct nft_rule * rule)278 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
279 					    struct nft_rule *rule)
280 {
281 	struct nft_trans *trans;
282 
283 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
284 	if (trans == NULL)
285 		return NULL;
286 
287 	nft_trans_rule(trans) = rule;
288 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
289 
290 	return trans;
291 }
292 
nft_delrule(struct nft_ctx * ctx,struct nft_rule * rule)293 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
294 {
295 	struct nft_trans *trans;
296 	int err;
297 
298 	trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
299 	if (trans == NULL)
300 		return -ENOMEM;
301 
302 	err = nf_tables_delrule_deactivate(ctx, rule);
303 	if (err < 0) {
304 		nft_trans_destroy(trans);
305 		return err;
306 	}
307 
308 	return 0;
309 }
310 
nft_delrule_by_chain(struct nft_ctx * ctx)311 static int nft_delrule_by_chain(struct nft_ctx *ctx)
312 {
313 	struct nft_rule *rule;
314 	int err;
315 
316 	list_for_each_entry(rule, &ctx->chain->rules, list) {
317 		err = nft_delrule(ctx, rule);
318 		if (err < 0)
319 			return err;
320 	}
321 	return 0;
322 }
323 
324 /* Internal set flag */
325 #define NFT_SET_INACTIVE	(1 << 15)
326 
nft_trans_set_add(struct nft_ctx * ctx,int msg_type,struct nft_set * set)327 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
328 			     struct nft_set *set)
329 {
330 	struct nft_trans *trans;
331 
332 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
333 	if (trans == NULL)
334 		return -ENOMEM;
335 
336 	if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
337 		nft_trans_set_id(trans) =
338 			ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
339 		set->flags |= NFT_SET_INACTIVE;
340 	}
341 	nft_trans_set(trans) = set;
342 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
343 
344 	return 0;
345 }
346 
nft_delset(struct nft_ctx * ctx,struct nft_set * set)347 static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
348 {
349 	int err;
350 
351 	err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
352 	if (err < 0)
353 		return err;
354 
355 	list_del_rcu(&set->list);
356 	ctx->table->use--;
357 
358 	return err;
359 }
360 
361 /*
362  * Tables
363  */
364 
nft_table_lookup(const struct nft_af_info * afi,const struct nlattr * nla)365 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
366 					  const struct nlattr *nla)
367 {
368 	struct nft_table *table;
369 
370 	list_for_each_entry(table, &afi->tables, list) {
371 		if (!nla_strcmp(nla, table->name))
372 			return table;
373 	}
374 	return NULL;
375 }
376 
nf_tables_table_lookup(const struct nft_af_info * afi,const struct nlattr * nla)377 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
378 						const struct nlattr *nla)
379 {
380 	struct nft_table *table;
381 
382 	if (nla == NULL)
383 		return ERR_PTR(-EINVAL);
384 
385 	table = nft_table_lookup(afi, nla);
386 	if (table != NULL)
387 		return table;
388 
389 	return ERR_PTR(-ENOENT);
390 }
391 
nf_tables_alloc_handle(struct nft_table * table)392 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
393 {
394 	return ++table->hgenerator;
395 }
396 
397 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
398 
399 static const struct nf_chain_type *
__nf_tables_chain_type_lookup(int family,const struct nlattr * nla)400 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
401 {
402 	int i;
403 
404 	for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
405 		if (chain_type[family][i] != NULL &&
406 		    !nla_strcmp(nla, chain_type[family][i]->name))
407 			return chain_type[family][i];
408 	}
409 	return NULL;
410 }
411 
412 static const struct nf_chain_type *
nf_tables_chain_type_lookup(const struct nft_af_info * afi,const struct nlattr * nla,bool autoload)413 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
414 			    const struct nlattr *nla,
415 			    bool autoload)
416 {
417 	const struct nf_chain_type *type;
418 
419 	type = __nf_tables_chain_type_lookup(afi->family, nla);
420 	if (type != NULL)
421 		return type;
422 #ifdef CONFIG_MODULES
423 	if (autoload) {
424 		nfnl_unlock(NFNL_SUBSYS_NFTABLES);
425 		request_module("nft-chain-%u-%.*s", afi->family,
426 			       nla_len(nla), (const char *)nla_data(nla));
427 		nfnl_lock(NFNL_SUBSYS_NFTABLES);
428 		type = __nf_tables_chain_type_lookup(afi->family, nla);
429 		if (type != NULL)
430 			return ERR_PTR(-EAGAIN);
431 	}
432 #endif
433 	return ERR_PTR(-ENOENT);
434 }
435 
436 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
437 	[NFTA_TABLE_NAME]	= { .type = NLA_STRING,
438 				    .len = NFT_TABLE_MAXNAMELEN - 1 },
439 	[NFTA_TABLE_FLAGS]	= { .type = NLA_U32 },
440 };
441 
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)442 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
443 				     u32 portid, u32 seq, int event, u32 flags,
444 				     int family, const struct nft_table *table)
445 {
446 	struct nlmsghdr *nlh;
447 	struct nfgenmsg *nfmsg;
448 
449 	event |= NFNL_SUBSYS_NFTABLES << 8;
450 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
451 	if (nlh == NULL)
452 		goto nla_put_failure;
453 
454 	nfmsg = nlmsg_data(nlh);
455 	nfmsg->nfgen_family	= family;
456 	nfmsg->version		= NFNETLINK_V0;
457 	nfmsg->res_id		= htons(net->nft.base_seq & 0xffff);
458 
459 	if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
460 	    nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
461 	    nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
462 		goto nla_put_failure;
463 
464 	nlmsg_end(skb, nlh);
465 	return 0;
466 
467 nla_put_failure:
468 	nlmsg_trim(skb, nlh);
469 	return -1;
470 }
471 
nf_tables_table_notify(const struct nft_ctx * ctx,int event)472 static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
473 {
474 	struct sk_buff *skb;
475 	int err;
476 
477 	if (!ctx->report &&
478 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
479 		return 0;
480 
481 	err = -ENOBUFS;
482 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
483 	if (skb == NULL)
484 		goto err;
485 
486 	err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
487 					event, 0, ctx->afi->family, ctx->table);
488 	if (err < 0) {
489 		kfree_skb(skb);
490 		goto err;
491 	}
492 
493 	err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
494 			     ctx->report, GFP_KERNEL);
495 err:
496 	if (err < 0) {
497 		nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
498 				  err);
499 	}
500 	return err;
501 }
502 
nf_tables_dump_tables(struct sk_buff * skb,struct netlink_callback * cb)503 static int nf_tables_dump_tables(struct sk_buff *skb,
504 				 struct netlink_callback *cb)
505 {
506 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
507 	const struct nft_af_info *afi;
508 	const struct nft_table *table;
509 	unsigned int idx = 0, s_idx = cb->args[0];
510 	struct net *net = sock_net(skb->sk);
511 	int family = nfmsg->nfgen_family;
512 
513 	rcu_read_lock();
514 	cb->seq = net->nft.base_seq;
515 
516 	list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
517 		if (family != NFPROTO_UNSPEC && family != afi->family)
518 			continue;
519 
520 		list_for_each_entry_rcu(table, &afi->tables, list) {
521 			if (idx < s_idx)
522 				goto cont;
523 			if (idx > s_idx)
524 				memset(&cb->args[1], 0,
525 				       sizeof(cb->args) - sizeof(cb->args[0]));
526 			if (nf_tables_fill_table_info(skb, net,
527 						      NETLINK_CB(cb->skb).portid,
528 						      cb->nlh->nlmsg_seq,
529 						      NFT_MSG_NEWTABLE,
530 						      NLM_F_MULTI,
531 						      afi->family, table) < 0)
532 				goto done;
533 
534 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
535 cont:
536 			idx++;
537 		}
538 	}
539 done:
540 	rcu_read_unlock();
541 	cb->args[0] = idx;
542 	return skb->len;
543 }
544 
nf_tables_gettable(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])545 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
546 			      const struct nlmsghdr *nlh,
547 			      const struct nlattr * const nla[])
548 {
549 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
550 	const struct nft_af_info *afi;
551 	const struct nft_table *table;
552 	struct sk_buff *skb2;
553 	struct net *net = sock_net(skb->sk);
554 	int family = nfmsg->nfgen_family;
555 	int err;
556 
557 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
558 		struct netlink_dump_control c = {
559 			.dump = nf_tables_dump_tables,
560 		};
561 		return netlink_dump_start(nlsk, skb, nlh, &c);
562 	}
563 
564 	afi = nf_tables_afinfo_lookup(net, family, false);
565 	if (IS_ERR(afi))
566 		return PTR_ERR(afi);
567 
568 	table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
569 	if (IS_ERR(table))
570 		return PTR_ERR(table);
571 	if (table->flags & NFT_TABLE_INACTIVE)
572 		return -ENOENT;
573 
574 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
575 	if (!skb2)
576 		return -ENOMEM;
577 
578 	err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
579 					nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
580 					family, table);
581 	if (err < 0)
582 		goto err;
583 
584 	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
585 
586 err:
587 	kfree_skb(skb2);
588 	return err;
589 }
590 
nf_tables_table_enable(const struct nft_af_info * afi,struct nft_table * table)591 static int nf_tables_table_enable(const struct nft_af_info *afi,
592 				  struct nft_table *table)
593 {
594 	struct nft_chain *chain;
595 	int err, i = 0;
596 
597 	list_for_each_entry(chain, &table->chains, list) {
598 		if (!(chain->flags & NFT_BASE_CHAIN))
599 			continue;
600 
601 		err = nft_register_basechain(nft_base_chain(chain), afi->nops);
602 		if (err < 0)
603 			goto err;
604 
605 		i++;
606 	}
607 	return 0;
608 err:
609 	list_for_each_entry(chain, &table->chains, list) {
610 		if (!(chain->flags & NFT_BASE_CHAIN))
611 			continue;
612 
613 		if (i-- <= 0)
614 			break;
615 
616 		nft_unregister_basechain(nft_base_chain(chain), afi->nops);
617 	}
618 	return err;
619 }
620 
nf_tables_table_disable(const struct nft_af_info * afi,struct nft_table * table)621 static void nf_tables_table_disable(const struct nft_af_info *afi,
622 				    struct nft_table *table)
623 {
624 	struct nft_chain *chain;
625 
626 	list_for_each_entry(chain, &table->chains, list) {
627 		if (chain->flags & NFT_BASE_CHAIN)
628 			nft_unregister_basechain(nft_base_chain(chain),
629 						 afi->nops);
630 	}
631 }
632 
nf_tables_updtable(struct nft_ctx * ctx)633 static int nf_tables_updtable(struct nft_ctx *ctx)
634 {
635 	struct nft_trans *trans;
636 	u32 flags;
637 	int ret = 0;
638 
639 	if (!ctx->nla[NFTA_TABLE_FLAGS])
640 		return 0;
641 
642 	flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
643 	if (flags & ~NFT_TABLE_F_DORMANT)
644 		return -EINVAL;
645 
646 	if (flags == ctx->table->flags)
647 		return 0;
648 
649 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
650 				sizeof(struct nft_trans_table));
651 	if (trans == NULL)
652 		return -ENOMEM;
653 
654 	if ((flags & NFT_TABLE_F_DORMANT) &&
655 	    !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
656 		nft_trans_table_enable(trans) = false;
657 	} else if (!(flags & NFT_TABLE_F_DORMANT) &&
658 		   ctx->table->flags & NFT_TABLE_F_DORMANT) {
659 		ret = nf_tables_table_enable(ctx->afi, ctx->table);
660 		if (ret >= 0) {
661 			ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
662 			nft_trans_table_enable(trans) = true;
663 		}
664 	}
665 	if (ret < 0)
666 		goto err;
667 
668 	nft_trans_table_update(trans) = true;
669 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
670 	return 0;
671 err:
672 	nft_trans_destroy(trans);
673 	return ret;
674 }
675 
nf_tables_newtable(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])676 static int nf_tables_newtable(struct net *net, struct sock *nlsk,
677 			      struct sk_buff *skb, const struct nlmsghdr *nlh,
678 			      const struct nlattr * const nla[])
679 {
680 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
681 	const struct nlattr *name;
682 	struct nft_af_info *afi;
683 	struct nft_table *table;
684 	int family = nfmsg->nfgen_family;
685 	u32 flags = 0;
686 	struct nft_ctx ctx;
687 	int err;
688 
689 	afi = nf_tables_afinfo_lookup(net, family, true);
690 	if (IS_ERR(afi))
691 		return PTR_ERR(afi);
692 
693 	name = nla[NFTA_TABLE_NAME];
694 	table = nf_tables_table_lookup(afi, name);
695 	if (IS_ERR(table)) {
696 		if (PTR_ERR(table) != -ENOENT)
697 			return PTR_ERR(table);
698 		table = NULL;
699 	}
700 
701 	if (table != NULL) {
702 		if (table->flags & NFT_TABLE_INACTIVE)
703 			return -ENOENT;
704 		if (nlh->nlmsg_flags & NLM_F_EXCL)
705 			return -EEXIST;
706 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
707 			return -EOPNOTSUPP;
708 
709 		nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
710 		return nf_tables_updtable(&ctx);
711 	}
712 
713 	if (nla[NFTA_TABLE_FLAGS]) {
714 		flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
715 		if (flags & ~NFT_TABLE_F_DORMANT)
716 			return -EINVAL;
717 	}
718 
719 	err = -EAFNOSUPPORT;
720 	if (!try_module_get(afi->owner))
721 		goto err1;
722 
723 	err = -ENOMEM;
724 	table = kzalloc(sizeof(*table), GFP_KERNEL);
725 	if (table == NULL)
726 		goto err2;
727 
728 	nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
729 	INIT_LIST_HEAD(&table->chains);
730 	INIT_LIST_HEAD(&table->sets);
731 	table->flags = flags;
732 
733 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
734 	err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
735 	if (err < 0)
736 		goto err3;
737 
738 	list_add_tail_rcu(&table->list, &afi->tables);
739 	return 0;
740 err3:
741 	kfree(table);
742 err2:
743 	module_put(afi->owner);
744 err1:
745 	return err;
746 }
747 
nft_flush_table(struct nft_ctx * ctx)748 static int nft_flush_table(struct nft_ctx *ctx)
749 {
750 	int err;
751 	struct nft_chain *chain, *nc;
752 	struct nft_set *set, *ns;
753 
754 	list_for_each_entry(chain, &ctx->table->chains, list) {
755 		ctx->chain = chain;
756 
757 		err = nft_delrule_by_chain(ctx);
758 		if (err < 0)
759 			goto out;
760 	}
761 
762 	list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
763 		if (set->flags & NFT_SET_ANONYMOUS &&
764 		    !list_empty(&set->bindings))
765 			continue;
766 
767 		err = nft_delset(ctx, set);
768 		if (err < 0)
769 			goto out;
770 	}
771 
772 	list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
773 		ctx->chain = chain;
774 
775 		err = nft_delchain(ctx);
776 		if (err < 0)
777 			goto out;
778 	}
779 
780 	err = nft_deltable(ctx);
781 out:
782 	return err;
783 }
784 
nft_flush(struct nft_ctx * ctx,int family)785 static int nft_flush(struct nft_ctx *ctx, int family)
786 {
787 	struct nft_af_info *afi;
788 	struct nft_table *table, *nt;
789 	const struct nlattr * const *nla = ctx->nla;
790 	int err = 0;
791 
792 	list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
793 		if (family != AF_UNSPEC && afi->family != family)
794 			continue;
795 
796 		ctx->afi = afi;
797 		list_for_each_entry_safe(table, nt, &afi->tables, list) {
798 			if (nla[NFTA_TABLE_NAME] &&
799 			    nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
800 				continue;
801 
802 			ctx->table = table;
803 
804 			err = nft_flush_table(ctx);
805 			if (err < 0)
806 				goto out;
807 		}
808 	}
809 out:
810 	return err;
811 }
812 
nf_tables_deltable(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])813 static int nf_tables_deltable(struct net *net, struct sock *nlsk,
814 			      struct sk_buff *skb, const struct nlmsghdr *nlh,
815 			      const struct nlattr * const nla[])
816 {
817 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
818 	struct nft_af_info *afi;
819 	struct nft_table *table;
820 	int family = nfmsg->nfgen_family;
821 	struct nft_ctx ctx;
822 
823 	nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla);
824 	if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
825 		return nft_flush(&ctx, family);
826 
827 	afi = nf_tables_afinfo_lookup(net, family, false);
828 	if (IS_ERR(afi))
829 		return PTR_ERR(afi);
830 
831 	table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
832 	if (IS_ERR(table))
833 		return PTR_ERR(table);
834 	if (table->flags & NFT_TABLE_INACTIVE)
835 		return -ENOENT;
836 
837 	ctx.afi = afi;
838 	ctx.table = table;
839 
840 	return nft_flush_table(&ctx);
841 }
842 
nf_tables_table_destroy(struct nft_ctx * ctx)843 static void nf_tables_table_destroy(struct nft_ctx *ctx)
844 {
845 	BUG_ON(ctx->table->use > 0);
846 
847 	kfree(ctx->table);
848 	module_put(ctx->afi->owner);
849 }
850 
nft_register_chain_type(const struct nf_chain_type * ctype)851 int nft_register_chain_type(const struct nf_chain_type *ctype)
852 {
853 	int err = 0;
854 
855 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
856 	if (chain_type[ctype->family][ctype->type] != NULL) {
857 		err = -EBUSY;
858 		goto out;
859 	}
860 	chain_type[ctype->family][ctype->type] = ctype;
861 out:
862 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
863 	return err;
864 }
865 EXPORT_SYMBOL_GPL(nft_register_chain_type);
866 
nft_unregister_chain_type(const struct nf_chain_type * ctype)867 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
868 {
869 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
870 	chain_type[ctype->family][ctype->type] = NULL;
871 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
872 }
873 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
874 
875 /*
876  * Chains
877  */
878 
879 static struct nft_chain *
nf_tables_chain_lookup_byhandle(const struct nft_table * table,u64 handle)880 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
881 {
882 	struct nft_chain *chain;
883 
884 	list_for_each_entry(chain, &table->chains, list) {
885 		if (chain->handle == handle)
886 			return chain;
887 	}
888 
889 	return ERR_PTR(-ENOENT);
890 }
891 
nf_tables_chain_lookup(const struct nft_table * table,const struct nlattr * nla)892 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
893 						const struct nlattr *nla)
894 {
895 	struct nft_chain *chain;
896 
897 	if (nla == NULL)
898 		return ERR_PTR(-EINVAL);
899 
900 	list_for_each_entry(chain, &table->chains, list) {
901 		if (!nla_strcmp(nla, chain->name))
902 			return chain;
903 	}
904 
905 	return ERR_PTR(-ENOENT);
906 }
907 
908 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
909 	[NFTA_CHAIN_TABLE]	= { .type = NLA_STRING },
910 	[NFTA_CHAIN_HANDLE]	= { .type = NLA_U64 },
911 	[NFTA_CHAIN_NAME]	= { .type = NLA_STRING,
912 				    .len = NFT_CHAIN_MAXNAMELEN - 1 },
913 	[NFTA_CHAIN_HOOK]	= { .type = NLA_NESTED },
914 	[NFTA_CHAIN_POLICY]	= { .type = NLA_U32 },
915 	[NFTA_CHAIN_TYPE]	= { .type = NLA_STRING },
916 	[NFTA_CHAIN_COUNTERS]	= { .type = NLA_NESTED },
917 };
918 
919 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
920 	[NFTA_HOOK_HOOKNUM]	= { .type = NLA_U32 },
921 	[NFTA_HOOK_PRIORITY]	= { .type = NLA_U32 },
922 	[NFTA_HOOK_DEV]		= { .type = NLA_STRING,
923 				    .len = IFNAMSIZ - 1 },
924 };
925 
nft_dump_stats(struct sk_buff * skb,struct nft_stats __percpu * stats)926 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
927 {
928 	struct nft_stats *cpu_stats, total;
929 	struct nlattr *nest;
930 	unsigned int seq;
931 	u64 pkts, bytes;
932 	int cpu;
933 
934 	memset(&total, 0, sizeof(total));
935 	for_each_possible_cpu(cpu) {
936 		cpu_stats = per_cpu_ptr(stats, cpu);
937 		do {
938 			seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
939 			pkts = cpu_stats->pkts;
940 			bytes = cpu_stats->bytes;
941 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
942 		total.pkts += pkts;
943 		total.bytes += bytes;
944 	}
945 	nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
946 	if (nest == NULL)
947 		goto nla_put_failure;
948 
949 	if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
950 	    nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
951 		goto nla_put_failure;
952 
953 	nla_nest_end(skb, nest);
954 	return 0;
955 
956 nla_put_failure:
957 	return -ENOSPC;
958 }
959 
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)960 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
961 				     u32 portid, u32 seq, int event, u32 flags,
962 				     int family, const struct nft_table *table,
963 				     const struct nft_chain *chain)
964 {
965 	struct nlmsghdr *nlh;
966 	struct nfgenmsg *nfmsg;
967 
968 	event |= NFNL_SUBSYS_NFTABLES << 8;
969 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
970 	if (nlh == NULL)
971 		goto nla_put_failure;
972 
973 	nfmsg = nlmsg_data(nlh);
974 	nfmsg->nfgen_family	= family;
975 	nfmsg->version		= NFNETLINK_V0;
976 	nfmsg->res_id		= htons(net->nft.base_seq & 0xffff);
977 
978 	if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
979 		goto nla_put_failure;
980 	if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
981 		goto nla_put_failure;
982 	if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
983 		goto nla_put_failure;
984 
985 	if (chain->flags & NFT_BASE_CHAIN) {
986 		const struct nft_base_chain *basechain = nft_base_chain(chain);
987 		const struct nf_hook_ops *ops = &basechain->ops[0];
988 		struct nlattr *nest;
989 
990 		nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
991 		if (nest == NULL)
992 			goto nla_put_failure;
993 		if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
994 			goto nla_put_failure;
995 		if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
996 			goto nla_put_failure;
997 		if (basechain->dev_name[0] &&
998 		    nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name))
999 			goto nla_put_failure;
1000 		nla_nest_end(skb, nest);
1001 
1002 		if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1003 				 htonl(basechain->policy)))
1004 			goto nla_put_failure;
1005 
1006 		if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1007 			goto nla_put_failure;
1008 
1009 		if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
1010 			goto nla_put_failure;
1011 	}
1012 
1013 	if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1014 		goto nla_put_failure;
1015 
1016 	nlmsg_end(skb, nlh);
1017 	return 0;
1018 
1019 nla_put_failure:
1020 	nlmsg_trim(skb, nlh);
1021 	return -1;
1022 }
1023 
nf_tables_chain_notify(const struct nft_ctx * ctx,int event)1024 static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
1025 {
1026 	struct sk_buff *skb;
1027 	int err;
1028 
1029 	if (!ctx->report &&
1030 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1031 		return 0;
1032 
1033 	err = -ENOBUFS;
1034 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1035 	if (skb == NULL)
1036 		goto err;
1037 
1038 	err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1039 					event, 0, ctx->afi->family, ctx->table,
1040 					ctx->chain);
1041 	if (err < 0) {
1042 		kfree_skb(skb);
1043 		goto err;
1044 	}
1045 
1046 	err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1047 			     ctx->report, GFP_KERNEL);
1048 err:
1049 	if (err < 0) {
1050 		nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1051 				  err);
1052 	}
1053 	return err;
1054 }
1055 
nf_tables_dump_chains(struct sk_buff * skb,struct netlink_callback * cb)1056 static int nf_tables_dump_chains(struct sk_buff *skb,
1057 				 struct netlink_callback *cb)
1058 {
1059 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1060 	const struct nft_af_info *afi;
1061 	const struct nft_table *table;
1062 	const struct nft_chain *chain;
1063 	unsigned int idx = 0, s_idx = cb->args[0];
1064 	struct net *net = sock_net(skb->sk);
1065 	int family = nfmsg->nfgen_family;
1066 
1067 	rcu_read_lock();
1068 	cb->seq = net->nft.base_seq;
1069 
1070 	list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1071 		if (family != NFPROTO_UNSPEC && family != afi->family)
1072 			continue;
1073 
1074 		list_for_each_entry_rcu(table, &afi->tables, list) {
1075 			list_for_each_entry_rcu(chain, &table->chains, list) {
1076 				if (idx < s_idx)
1077 					goto cont;
1078 				if (idx > s_idx)
1079 					memset(&cb->args[1], 0,
1080 					       sizeof(cb->args) - sizeof(cb->args[0]));
1081 				if (nf_tables_fill_chain_info(skb, net,
1082 							      NETLINK_CB(cb->skb).portid,
1083 							      cb->nlh->nlmsg_seq,
1084 							      NFT_MSG_NEWCHAIN,
1085 							      NLM_F_MULTI,
1086 							      afi->family, table, chain) < 0)
1087 					goto done;
1088 
1089 				nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1090 cont:
1091 				idx++;
1092 			}
1093 		}
1094 	}
1095 done:
1096 	rcu_read_unlock();
1097 	cb->args[0] = idx;
1098 	return skb->len;
1099 }
1100 
nf_tables_getchain(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])1101 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
1102 			      const struct nlmsghdr *nlh,
1103 			      const struct nlattr * const nla[])
1104 {
1105 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1106 	const struct nft_af_info *afi;
1107 	const struct nft_table *table;
1108 	const struct nft_chain *chain;
1109 	struct sk_buff *skb2;
1110 	struct net *net = sock_net(skb->sk);
1111 	int family = nfmsg->nfgen_family;
1112 	int err;
1113 
1114 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1115 		struct netlink_dump_control c = {
1116 			.dump = nf_tables_dump_chains,
1117 		};
1118 		return netlink_dump_start(nlsk, skb, nlh, &c);
1119 	}
1120 
1121 	afi = nf_tables_afinfo_lookup(net, family, false);
1122 	if (IS_ERR(afi))
1123 		return PTR_ERR(afi);
1124 
1125 	table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1126 	if (IS_ERR(table))
1127 		return PTR_ERR(table);
1128 	if (table->flags & NFT_TABLE_INACTIVE)
1129 		return -ENOENT;
1130 
1131 	chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1132 	if (IS_ERR(chain))
1133 		return PTR_ERR(chain);
1134 	if (chain->flags & NFT_CHAIN_INACTIVE)
1135 		return -ENOENT;
1136 
1137 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1138 	if (!skb2)
1139 		return -ENOMEM;
1140 
1141 	err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1142 					nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1143 					family, table, chain);
1144 	if (err < 0)
1145 		goto err;
1146 
1147 	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1148 
1149 err:
1150 	kfree_skb(skb2);
1151 	return err;
1152 }
1153 
1154 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1155 	[NFTA_COUNTER_PACKETS]	= { .type = NLA_U64 },
1156 	[NFTA_COUNTER_BYTES]	= { .type = NLA_U64 },
1157 };
1158 
nft_stats_alloc(const struct nlattr * attr)1159 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1160 {
1161 	struct nlattr *tb[NFTA_COUNTER_MAX+1];
1162 	struct nft_stats __percpu *newstats;
1163 	struct nft_stats *stats;
1164 	int err;
1165 
1166 	err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1167 	if (err < 0)
1168 		return ERR_PTR(err);
1169 
1170 	if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1171 		return ERR_PTR(-EINVAL);
1172 
1173 	newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1174 	if (newstats == NULL)
1175 		return ERR_PTR(-ENOMEM);
1176 
1177 	/* Restore old counters on this cpu, no problem. Per-cpu statistics
1178 	 * are not exposed to userspace.
1179 	 */
1180 	preempt_disable();
1181 	stats = this_cpu_ptr(newstats);
1182 	stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1183 	stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1184 	preempt_enable();
1185 
1186 	return newstats;
1187 }
1188 
nft_chain_stats_replace(struct nft_base_chain * chain,struct nft_stats __percpu * newstats)1189 static void nft_chain_stats_replace(struct nft_base_chain *chain,
1190 				    struct nft_stats __percpu *newstats)
1191 {
1192 	if (newstats == NULL)
1193 		return;
1194 
1195 	if (chain->stats) {
1196 		struct nft_stats __percpu *oldstats =
1197 				nft_dereference(chain->stats);
1198 
1199 		rcu_assign_pointer(chain->stats, newstats);
1200 		synchronize_rcu();
1201 		free_percpu(oldstats);
1202 	} else
1203 		rcu_assign_pointer(chain->stats, newstats);
1204 }
1205 
nf_tables_chain_destroy(struct nft_chain * chain)1206 static void nf_tables_chain_destroy(struct nft_chain *chain)
1207 {
1208 	BUG_ON(chain->use > 0);
1209 
1210 	if (chain->flags & NFT_BASE_CHAIN) {
1211 		struct nft_base_chain *basechain = nft_base_chain(chain);
1212 
1213 		module_put(basechain->type->owner);
1214 		free_percpu(basechain->stats);
1215 		if (basechain->ops[0].dev != NULL)
1216 			dev_put(basechain->ops[0].dev);
1217 		kfree(basechain);
1218 	} else {
1219 		kfree(chain);
1220 	}
1221 }
1222 
nf_tables_newchain(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])1223 static int nf_tables_newchain(struct net *net, struct sock *nlsk,
1224 			      struct sk_buff *skb, const struct nlmsghdr *nlh,
1225 			      const struct nlattr * const nla[])
1226 {
1227 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1228 	const struct nlattr * uninitialized_var(name);
1229 	struct nft_af_info *afi;
1230 	struct nft_table *table;
1231 	struct nft_chain *chain;
1232 	struct nft_base_chain *basechain = NULL;
1233 	struct nlattr *ha[NFTA_HOOK_MAX + 1];
1234 	int family = nfmsg->nfgen_family;
1235 	struct net_device *dev = NULL;
1236 	u8 policy = NF_ACCEPT;
1237 	u64 handle = 0;
1238 	unsigned int i;
1239 	struct nft_stats __percpu *stats;
1240 	int err;
1241 	bool create;
1242 	struct nft_ctx ctx;
1243 
1244 	create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1245 
1246 	afi = nf_tables_afinfo_lookup(net, family, true);
1247 	if (IS_ERR(afi))
1248 		return PTR_ERR(afi);
1249 
1250 	table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1251 	if (IS_ERR(table))
1252 		return PTR_ERR(table);
1253 
1254 	chain = NULL;
1255 	name = nla[NFTA_CHAIN_NAME];
1256 
1257 	if (nla[NFTA_CHAIN_HANDLE]) {
1258 		handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1259 		chain = nf_tables_chain_lookup_byhandle(table, handle);
1260 		if (IS_ERR(chain))
1261 			return PTR_ERR(chain);
1262 	} else {
1263 		chain = nf_tables_chain_lookup(table, name);
1264 		if (IS_ERR(chain)) {
1265 			if (PTR_ERR(chain) != -ENOENT)
1266 				return PTR_ERR(chain);
1267 			chain = NULL;
1268 		}
1269 	}
1270 
1271 	if (nla[NFTA_CHAIN_POLICY]) {
1272 		if ((chain != NULL &&
1273 		    !(chain->flags & NFT_BASE_CHAIN)))
1274 			return -EOPNOTSUPP;
1275 
1276 		if (chain == NULL &&
1277 		    nla[NFTA_CHAIN_HOOK] == NULL)
1278 			return -EOPNOTSUPP;
1279 
1280 		policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1281 		switch (policy) {
1282 		case NF_DROP:
1283 		case NF_ACCEPT:
1284 			break;
1285 		default:
1286 			return -EINVAL;
1287 		}
1288 	}
1289 
1290 	if (chain != NULL) {
1291 		struct nft_stats *stats = NULL;
1292 		struct nft_trans *trans;
1293 
1294 		if (chain->flags & NFT_CHAIN_INACTIVE)
1295 			return -ENOENT;
1296 		if (nlh->nlmsg_flags & NLM_F_EXCL)
1297 			return -EEXIST;
1298 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
1299 			return -EOPNOTSUPP;
1300 
1301 		if (nla[NFTA_CHAIN_HANDLE] && name &&
1302 		    !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1303 			return -EEXIST;
1304 
1305 		if (nla[NFTA_CHAIN_COUNTERS]) {
1306 			if (!(chain->flags & NFT_BASE_CHAIN))
1307 				return -EOPNOTSUPP;
1308 
1309 			stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1310 			if (IS_ERR(stats))
1311 				return PTR_ERR(stats);
1312 		}
1313 
1314 		nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
1315 		trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1316 					sizeof(struct nft_trans_chain));
1317 		if (trans == NULL) {
1318 			free_percpu(stats);
1319 			return -ENOMEM;
1320 		}
1321 
1322 		nft_trans_chain_stats(trans) = stats;
1323 		nft_trans_chain_update(trans) = true;
1324 
1325 		if (nla[NFTA_CHAIN_POLICY])
1326 			nft_trans_chain_policy(trans) = policy;
1327 		else
1328 			nft_trans_chain_policy(trans) = -1;
1329 
1330 		if (nla[NFTA_CHAIN_HANDLE] && name) {
1331 			nla_strlcpy(nft_trans_chain_name(trans), name,
1332 				    NFT_CHAIN_MAXNAMELEN);
1333 		}
1334 		list_add_tail(&trans->list, &net->nft.commit_list);
1335 		return 0;
1336 	}
1337 
1338 	if (table->use == UINT_MAX)
1339 		return -EOVERFLOW;
1340 
1341 	if (nla[NFTA_CHAIN_HOOK]) {
1342 		const struct nf_chain_type *type;
1343 		struct nf_hook_ops *ops;
1344 		nf_hookfn *hookfn;
1345 		u32 hooknum, priority;
1346 
1347 		type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1348 		if (nla[NFTA_CHAIN_TYPE]) {
1349 			type = nf_tables_chain_type_lookup(afi,
1350 							   nla[NFTA_CHAIN_TYPE],
1351 							   create);
1352 			if (IS_ERR(type))
1353 				return PTR_ERR(type);
1354 		}
1355 
1356 		err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1357 				       nft_hook_policy);
1358 		if (err < 0)
1359 			return err;
1360 		if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1361 		    ha[NFTA_HOOK_PRIORITY] == NULL)
1362 			return -EINVAL;
1363 
1364 		hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1365 		if (hooknum >= afi->nhooks)
1366 			return -EINVAL;
1367 		priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1368 
1369 		if (!(type->hook_mask & (1 << hooknum)))
1370 			return -EOPNOTSUPP;
1371 		if (!try_module_get(type->owner))
1372 			return -ENOENT;
1373 		hookfn = type->hooks[hooknum];
1374 
1375 		if (afi->flags & NFT_AF_NEEDS_DEV) {
1376 			char ifname[IFNAMSIZ];
1377 
1378 			if (!ha[NFTA_HOOK_DEV]) {
1379 				module_put(type->owner);
1380 				return -EOPNOTSUPP;
1381 			}
1382 
1383 			nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ);
1384 			dev = dev_get_by_name(net, ifname);
1385 			if (!dev) {
1386 				module_put(type->owner);
1387 				return -ENOENT;
1388 			}
1389 		} else if (ha[NFTA_HOOK_DEV]) {
1390 			module_put(type->owner);
1391 			return -EOPNOTSUPP;
1392 		}
1393 
1394 		basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1395 		if (basechain == NULL) {
1396 			module_put(type->owner);
1397 			if (dev != NULL)
1398 				dev_put(dev);
1399 			return -ENOMEM;
1400 		}
1401 
1402 		if (dev != NULL)
1403 			strncpy(basechain->dev_name, dev->name, IFNAMSIZ);
1404 
1405 		if (nla[NFTA_CHAIN_COUNTERS]) {
1406 			stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1407 			if (IS_ERR(stats)) {
1408 				module_put(type->owner);
1409 				kfree(basechain);
1410 				if (dev != NULL)
1411 					dev_put(dev);
1412 				return PTR_ERR(stats);
1413 			}
1414 			basechain->stats = stats;
1415 		} else {
1416 			stats = netdev_alloc_pcpu_stats(struct nft_stats);
1417 			if (stats == NULL) {
1418 				module_put(type->owner);
1419 				kfree(basechain);
1420 				if (dev != NULL)
1421 					dev_put(dev);
1422 				return -ENOMEM;
1423 			}
1424 			rcu_assign_pointer(basechain->stats, stats);
1425 		}
1426 
1427 		write_pnet(&basechain->pnet, net);
1428 		basechain->type = type;
1429 		chain = &basechain->chain;
1430 
1431 		for (i = 0; i < afi->nops; i++) {
1432 			ops = &basechain->ops[i];
1433 			ops->pf		= family;
1434 			ops->hooknum	= hooknum;
1435 			ops->priority	= priority;
1436 			ops->priv	= chain;
1437 			ops->hook	= afi->hooks[ops->hooknum];
1438 			ops->dev	= dev;
1439 			if (hookfn)
1440 				ops->hook = hookfn;
1441 			if (afi->hook_ops_init)
1442 				afi->hook_ops_init(ops, i);
1443 		}
1444 
1445 		chain->flags |= NFT_BASE_CHAIN;
1446 		basechain->policy = policy;
1447 	} else {
1448 		chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1449 		if (chain == NULL)
1450 			return -ENOMEM;
1451 	}
1452 
1453 	INIT_LIST_HEAD(&chain->rules);
1454 	chain->handle = nf_tables_alloc_handle(table);
1455 	chain->table = table;
1456 	nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1457 
1458 	err = nf_tables_register_hooks(table, chain, afi->nops);
1459 	if (err < 0)
1460 		goto err1;
1461 
1462 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
1463 	err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1464 	if (err < 0)
1465 		goto err2;
1466 
1467 	table->use++;
1468 	list_add_tail_rcu(&chain->list, &table->chains);
1469 	return 0;
1470 err2:
1471 	nf_tables_unregister_hooks(table, chain, afi->nops);
1472 err1:
1473 	nf_tables_chain_destroy(chain);
1474 	return err;
1475 }
1476 
nf_tables_delchain(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])1477 static int nf_tables_delchain(struct net *net, struct sock *nlsk,
1478 			      struct sk_buff *skb, const struct nlmsghdr *nlh,
1479 			      const struct nlattr * const nla[])
1480 {
1481 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1482 	struct nft_af_info *afi;
1483 	struct nft_table *table;
1484 	struct nft_chain *chain;
1485 	int family = nfmsg->nfgen_family;
1486 	struct nft_ctx ctx;
1487 
1488 	afi = nf_tables_afinfo_lookup(net, family, false);
1489 	if (IS_ERR(afi))
1490 		return PTR_ERR(afi);
1491 
1492 	table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1493 	if (IS_ERR(table))
1494 		return PTR_ERR(table);
1495 	if (table->flags & NFT_TABLE_INACTIVE)
1496 		return -ENOENT;
1497 
1498 	chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1499 	if (IS_ERR(chain))
1500 		return PTR_ERR(chain);
1501 	if (chain->flags & NFT_CHAIN_INACTIVE)
1502 		return -ENOENT;
1503 	if (chain->use > 0)
1504 		return -EBUSY;
1505 
1506 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
1507 
1508 	return nft_delchain(&ctx);
1509 }
1510 
1511 /*
1512  * Expressions
1513  */
1514 
1515 /**
1516  *	nft_register_expr - register nf_tables expr type
1517  *	@ops: expr type
1518  *
1519  *	Registers the expr type for use with nf_tables. Returns zero on
1520  *	success or a negative errno code otherwise.
1521  */
nft_register_expr(struct nft_expr_type * type)1522 int nft_register_expr(struct nft_expr_type *type)
1523 {
1524 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
1525 	if (type->family == NFPROTO_UNSPEC)
1526 		list_add_tail_rcu(&type->list, &nf_tables_expressions);
1527 	else
1528 		list_add_rcu(&type->list, &nf_tables_expressions);
1529 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1530 	return 0;
1531 }
1532 EXPORT_SYMBOL_GPL(nft_register_expr);
1533 
1534 /**
1535  *	nft_unregister_expr - unregister nf_tables expr type
1536  *	@ops: expr type
1537  *
1538  * 	Unregisters the expr typefor use with nf_tables.
1539  */
nft_unregister_expr(struct nft_expr_type * type)1540 void nft_unregister_expr(struct nft_expr_type *type)
1541 {
1542 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
1543 	list_del_rcu(&type->list);
1544 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1545 }
1546 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1547 
__nft_expr_type_get(u8 family,struct nlattr * nla)1548 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1549 						       struct nlattr *nla)
1550 {
1551 	const struct nft_expr_type *type;
1552 
1553 	list_for_each_entry(type, &nf_tables_expressions, list) {
1554 		if (!nla_strcmp(nla, type->name) &&
1555 		    (!type->family || type->family == family))
1556 			return type;
1557 	}
1558 	return NULL;
1559 }
1560 
nft_expr_type_get(u8 family,struct nlattr * nla)1561 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1562 						     struct nlattr *nla)
1563 {
1564 	const struct nft_expr_type *type;
1565 
1566 	if (nla == NULL)
1567 		return ERR_PTR(-EINVAL);
1568 
1569 	type = __nft_expr_type_get(family, nla);
1570 	if (type != NULL && try_module_get(type->owner))
1571 		return type;
1572 
1573 #ifdef CONFIG_MODULES
1574 	if (type == NULL) {
1575 		nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1576 		request_module("nft-expr-%u-%.*s", family,
1577 			       nla_len(nla), (char *)nla_data(nla));
1578 		nfnl_lock(NFNL_SUBSYS_NFTABLES);
1579 		if (__nft_expr_type_get(family, nla))
1580 			return ERR_PTR(-EAGAIN);
1581 
1582 		nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1583 		request_module("nft-expr-%.*s",
1584 			       nla_len(nla), (char *)nla_data(nla));
1585 		nfnl_lock(NFNL_SUBSYS_NFTABLES);
1586 		if (__nft_expr_type_get(family, nla))
1587 			return ERR_PTR(-EAGAIN);
1588 	}
1589 #endif
1590 	return ERR_PTR(-ENOENT);
1591 }
1592 
1593 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1594 	[NFTA_EXPR_NAME]	= { .type = NLA_STRING },
1595 	[NFTA_EXPR_DATA]	= { .type = NLA_NESTED },
1596 };
1597 
nf_tables_fill_expr_info(struct sk_buff * skb,const struct nft_expr * expr)1598 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1599 				    const struct nft_expr *expr)
1600 {
1601 	if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1602 		goto nla_put_failure;
1603 
1604 	if (expr->ops->dump) {
1605 		struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1606 		if (data == NULL)
1607 			goto nla_put_failure;
1608 		if (expr->ops->dump(skb, expr) < 0)
1609 			goto nla_put_failure;
1610 		nla_nest_end(skb, data);
1611 	}
1612 
1613 	return skb->len;
1614 
1615 nla_put_failure:
1616 	return -1;
1617 };
1618 
nft_expr_dump(struct sk_buff * skb,unsigned int attr,const struct nft_expr * expr)1619 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
1620 		  const struct nft_expr *expr)
1621 {
1622 	struct nlattr *nest;
1623 
1624 	nest = nla_nest_start(skb, attr);
1625 	if (!nest)
1626 		goto nla_put_failure;
1627 	if (nf_tables_fill_expr_info(skb, expr) < 0)
1628 		goto nla_put_failure;
1629 	nla_nest_end(skb, nest);
1630 	return 0;
1631 
1632 nla_put_failure:
1633 	return -1;
1634 }
1635 
1636 struct nft_expr_info {
1637 	const struct nft_expr_ops	*ops;
1638 	struct nlattr			*tb[NFT_EXPR_MAXATTR + 1];
1639 };
1640 
nf_tables_expr_parse(const struct nft_ctx * ctx,const struct nlattr * nla,struct nft_expr_info * info)1641 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1642 				const struct nlattr *nla,
1643 				struct nft_expr_info *info)
1644 {
1645 	const struct nft_expr_type *type;
1646 	const struct nft_expr_ops *ops;
1647 	struct nlattr *tb[NFTA_EXPR_MAX + 1];
1648 	int err;
1649 
1650 	err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1651 	if (err < 0)
1652 		return err;
1653 
1654 	type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1655 	if (IS_ERR(type))
1656 		return PTR_ERR(type);
1657 
1658 	if (tb[NFTA_EXPR_DATA]) {
1659 		err = nla_parse_nested(info->tb, type->maxattr,
1660 				       tb[NFTA_EXPR_DATA], type->policy);
1661 		if (err < 0)
1662 			goto err1;
1663 	} else
1664 		memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1665 
1666 	if (type->select_ops != NULL) {
1667 		ops = type->select_ops(ctx,
1668 				       (const struct nlattr * const *)info->tb);
1669 		if (IS_ERR(ops)) {
1670 			err = PTR_ERR(ops);
1671 			goto err1;
1672 		}
1673 	} else
1674 		ops = type->ops;
1675 
1676 	info->ops = ops;
1677 	return 0;
1678 
1679 err1:
1680 	module_put(type->owner);
1681 	return err;
1682 }
1683 
nf_tables_newexpr(const struct nft_ctx * ctx,const struct nft_expr_info * info,struct nft_expr * expr)1684 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1685 			     const struct nft_expr_info *info,
1686 			     struct nft_expr *expr)
1687 {
1688 	const struct nft_expr_ops *ops = info->ops;
1689 	int err;
1690 
1691 	expr->ops = ops;
1692 	if (ops->init) {
1693 		err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1694 		if (err < 0)
1695 			goto err1;
1696 	}
1697 
1698 	return 0;
1699 
1700 err1:
1701 	expr->ops = NULL;
1702 	return err;
1703 }
1704 
nf_tables_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)1705 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1706 				   struct nft_expr *expr)
1707 {
1708 	if (expr->ops->destroy)
1709 		expr->ops->destroy(ctx, expr);
1710 	module_put(expr->ops->type->owner);
1711 }
1712 
nft_expr_init(const struct nft_ctx * ctx,const struct nlattr * nla)1713 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
1714 			       const struct nlattr *nla)
1715 {
1716 	struct nft_expr_info info;
1717 	struct nft_expr *expr;
1718 	int err;
1719 
1720 	err = nf_tables_expr_parse(ctx, nla, &info);
1721 	if (err < 0)
1722 		goto err1;
1723 
1724 	err = -ENOMEM;
1725 	expr = kzalloc(info.ops->size, GFP_KERNEL);
1726 	if (expr == NULL)
1727 		goto err2;
1728 
1729 	err = nf_tables_newexpr(ctx, &info, expr);
1730 	if (err < 0)
1731 		goto err2;
1732 
1733 	return expr;
1734 err2:
1735 	module_put(info.ops->type->owner);
1736 err1:
1737 	return ERR_PTR(err);
1738 }
1739 
nft_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)1740 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
1741 {
1742 	nf_tables_expr_destroy(ctx, expr);
1743 	kfree(expr);
1744 }
1745 
1746 /*
1747  * Rules
1748  */
1749 
__nf_tables_rule_lookup(const struct nft_chain * chain,u64 handle)1750 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1751 						u64 handle)
1752 {
1753 	struct nft_rule *rule;
1754 
1755 	// FIXME: this sucks
1756 	list_for_each_entry(rule, &chain->rules, list) {
1757 		if (handle == rule->handle)
1758 			return rule;
1759 	}
1760 
1761 	return ERR_PTR(-ENOENT);
1762 }
1763 
nf_tables_rule_lookup(const struct nft_chain * chain,const struct nlattr * nla)1764 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1765 					      const struct nlattr *nla)
1766 {
1767 	if (nla == NULL)
1768 		return ERR_PTR(-EINVAL);
1769 
1770 	return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1771 }
1772 
1773 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1774 	[NFTA_RULE_TABLE]	= { .type = NLA_STRING },
1775 	[NFTA_RULE_CHAIN]	= { .type = NLA_STRING,
1776 				    .len = NFT_CHAIN_MAXNAMELEN - 1 },
1777 	[NFTA_RULE_HANDLE]	= { .type = NLA_U64 },
1778 	[NFTA_RULE_EXPRESSIONS]	= { .type = NLA_NESTED },
1779 	[NFTA_RULE_COMPAT]	= { .type = NLA_NESTED },
1780 	[NFTA_RULE_POSITION]	= { .type = NLA_U64 },
1781 	[NFTA_RULE_USERDATA]	= { .type = NLA_BINARY,
1782 				    .len = NFT_USERDATA_MAXLEN },
1783 };
1784 
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)1785 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1786 				    u32 portid, u32 seq, int event,
1787 				    u32 flags, int family,
1788 				    const struct nft_table *table,
1789 				    const struct nft_chain *chain,
1790 				    const struct nft_rule *rule)
1791 {
1792 	struct nlmsghdr *nlh;
1793 	struct nfgenmsg *nfmsg;
1794 	const struct nft_expr *expr, *next;
1795 	struct nlattr *list;
1796 	const struct nft_rule *prule;
1797 	int type = event | NFNL_SUBSYS_NFTABLES << 8;
1798 
1799 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1800 			flags);
1801 	if (nlh == NULL)
1802 		goto nla_put_failure;
1803 
1804 	nfmsg = nlmsg_data(nlh);
1805 	nfmsg->nfgen_family	= family;
1806 	nfmsg->version		= NFNETLINK_V0;
1807 	nfmsg->res_id		= htons(net->nft.base_seq & 0xffff);
1808 
1809 	if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1810 		goto nla_put_failure;
1811 	if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1812 		goto nla_put_failure;
1813 	if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1814 		goto nla_put_failure;
1815 
1816 	if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1817 		prule = list_entry(rule->list.prev, struct nft_rule, list);
1818 		if (nla_put_be64(skb, NFTA_RULE_POSITION,
1819 				 cpu_to_be64(prule->handle)))
1820 			goto nla_put_failure;
1821 	}
1822 
1823 	list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1824 	if (list == NULL)
1825 		goto nla_put_failure;
1826 	nft_rule_for_each_expr(expr, next, rule) {
1827 		if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
1828 			goto nla_put_failure;
1829 	}
1830 	nla_nest_end(skb, list);
1831 
1832 	if (rule->udata) {
1833 		struct nft_userdata *udata = nft_userdata(rule);
1834 		if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1835 			    udata->data) < 0)
1836 			goto nla_put_failure;
1837 	}
1838 
1839 	nlmsg_end(skb, nlh);
1840 	return 0;
1841 
1842 nla_put_failure:
1843 	nlmsg_trim(skb, nlh);
1844 	return -1;
1845 }
1846 
nf_tables_rule_notify(const struct nft_ctx * ctx,const struct nft_rule * rule,int event)1847 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1848 				 const struct nft_rule *rule,
1849 				 int event)
1850 {
1851 	struct sk_buff *skb;
1852 	int err;
1853 
1854 	if (!ctx->report &&
1855 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1856 		return 0;
1857 
1858 	err = -ENOBUFS;
1859 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1860 	if (skb == NULL)
1861 		goto err;
1862 
1863 	err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1864 				       event, 0, ctx->afi->family, ctx->table,
1865 				       ctx->chain, rule);
1866 	if (err < 0) {
1867 		kfree_skb(skb);
1868 		goto err;
1869 	}
1870 
1871 	err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1872 			     ctx->report, GFP_KERNEL);
1873 err:
1874 	if (err < 0) {
1875 		nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1876 				  err);
1877 	}
1878 	return err;
1879 }
1880 
nf_tables_dump_rules(struct sk_buff * skb,struct netlink_callback * cb)1881 static int nf_tables_dump_rules(struct sk_buff *skb,
1882 				struct netlink_callback *cb)
1883 {
1884 	const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1885 	const struct nft_af_info *afi;
1886 	const struct nft_table *table;
1887 	const struct nft_chain *chain;
1888 	const struct nft_rule *rule;
1889 	unsigned int idx = 0, s_idx = cb->args[0];
1890 	struct net *net = sock_net(skb->sk);
1891 	int family = nfmsg->nfgen_family;
1892 
1893 	rcu_read_lock();
1894 	cb->seq = net->nft.base_seq;
1895 
1896 	list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1897 		if (family != NFPROTO_UNSPEC && family != afi->family)
1898 			continue;
1899 
1900 		list_for_each_entry_rcu(table, &afi->tables, list) {
1901 			list_for_each_entry_rcu(chain, &table->chains, list) {
1902 				list_for_each_entry_rcu(rule, &chain->rules, list) {
1903 					if (!nft_rule_is_active(net, rule))
1904 						goto cont;
1905 					if (idx < s_idx)
1906 						goto cont;
1907 					if (idx > s_idx)
1908 						memset(&cb->args[1], 0,
1909 						       sizeof(cb->args) - sizeof(cb->args[0]));
1910 					if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
1911 								      cb->nlh->nlmsg_seq,
1912 								      NFT_MSG_NEWRULE,
1913 								      NLM_F_MULTI | NLM_F_APPEND,
1914 								      afi->family, table, chain, rule) < 0)
1915 						goto done;
1916 
1917 					nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1918 cont:
1919 					idx++;
1920 				}
1921 			}
1922 		}
1923 	}
1924 done:
1925 	rcu_read_unlock();
1926 
1927 	cb->args[0] = idx;
1928 	return skb->len;
1929 }
1930 
nf_tables_getrule(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])1931 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1932 			     const struct nlmsghdr *nlh,
1933 			     const struct nlattr * const nla[])
1934 {
1935 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1936 	const struct nft_af_info *afi;
1937 	const struct nft_table *table;
1938 	const struct nft_chain *chain;
1939 	const struct nft_rule *rule;
1940 	struct sk_buff *skb2;
1941 	struct net *net = sock_net(skb->sk);
1942 	int family = nfmsg->nfgen_family;
1943 	int err;
1944 
1945 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1946 		struct netlink_dump_control c = {
1947 			.dump = nf_tables_dump_rules,
1948 		};
1949 		return netlink_dump_start(nlsk, skb, nlh, &c);
1950 	}
1951 
1952 	afi = nf_tables_afinfo_lookup(net, family, false);
1953 	if (IS_ERR(afi))
1954 		return PTR_ERR(afi);
1955 
1956 	table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1957 	if (IS_ERR(table))
1958 		return PTR_ERR(table);
1959 	if (table->flags & NFT_TABLE_INACTIVE)
1960 		return -ENOENT;
1961 
1962 	chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1963 	if (IS_ERR(chain))
1964 		return PTR_ERR(chain);
1965 	if (chain->flags & NFT_CHAIN_INACTIVE)
1966 		return -ENOENT;
1967 
1968 	rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1969 	if (IS_ERR(rule))
1970 		return PTR_ERR(rule);
1971 
1972 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1973 	if (!skb2)
1974 		return -ENOMEM;
1975 
1976 	err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
1977 				       nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1978 				       family, table, chain, rule);
1979 	if (err < 0)
1980 		goto err;
1981 
1982 	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1983 
1984 err:
1985 	kfree_skb(skb2);
1986 	return err;
1987 }
1988 
nf_tables_rule_destroy(const struct nft_ctx * ctx,struct nft_rule * rule)1989 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1990 				   struct nft_rule *rule)
1991 {
1992 	struct nft_expr *expr;
1993 
1994 	/*
1995 	 * Careful: some expressions might not be initialized in case this
1996 	 * is called on error from nf_tables_newrule().
1997 	 */
1998 	expr = nft_expr_first(rule);
1999 	while (expr->ops && expr != nft_expr_last(rule)) {
2000 		nf_tables_expr_destroy(ctx, expr);
2001 		expr = nft_expr_next(expr);
2002 	}
2003 	kfree(rule);
2004 }
2005 
2006 #define NFT_RULE_MAXEXPRS	128
2007 
2008 static struct nft_expr_info *info;
2009 
nf_tables_newrule(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])2010 static int nf_tables_newrule(struct net *net, struct sock *nlsk,
2011 			     struct sk_buff *skb, const struct nlmsghdr *nlh,
2012 			     const struct nlattr * const nla[])
2013 {
2014 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2015 	struct nft_af_info *afi;
2016 	struct nft_table *table;
2017 	struct nft_chain *chain;
2018 	struct nft_rule *rule, *old_rule = NULL;
2019 	struct nft_userdata *udata;
2020 	struct nft_trans *trans = NULL;
2021 	struct nft_expr *expr;
2022 	struct nft_ctx ctx;
2023 	struct nlattr *tmp;
2024 	unsigned int size, i, n, ulen = 0, usize = 0;
2025 	int err, rem;
2026 	bool create;
2027 	u64 handle, pos_handle;
2028 
2029 	create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2030 
2031 	afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2032 	if (IS_ERR(afi))
2033 		return PTR_ERR(afi);
2034 
2035 	table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
2036 	if (IS_ERR(table))
2037 		return PTR_ERR(table);
2038 
2039 	chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2040 	if (IS_ERR(chain))
2041 		return PTR_ERR(chain);
2042 
2043 	if (nla[NFTA_RULE_HANDLE]) {
2044 		handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2045 		rule = __nf_tables_rule_lookup(chain, handle);
2046 		if (IS_ERR(rule))
2047 			return PTR_ERR(rule);
2048 
2049 		if (nlh->nlmsg_flags & NLM_F_EXCL)
2050 			return -EEXIST;
2051 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
2052 			old_rule = rule;
2053 		else
2054 			return -EOPNOTSUPP;
2055 	} else {
2056 		if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
2057 			return -EINVAL;
2058 		handle = nf_tables_alloc_handle(table);
2059 
2060 		if (chain->use == UINT_MAX)
2061 			return -EOVERFLOW;
2062 	}
2063 
2064 	if (nla[NFTA_RULE_POSITION]) {
2065 		if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2066 			return -EOPNOTSUPP;
2067 
2068 		pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2069 		old_rule = __nf_tables_rule_lookup(chain, pos_handle);
2070 		if (IS_ERR(old_rule))
2071 			return PTR_ERR(old_rule);
2072 	}
2073 
2074 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
2075 
2076 	n = 0;
2077 	size = 0;
2078 	if (nla[NFTA_RULE_EXPRESSIONS]) {
2079 		nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2080 			err = -EINVAL;
2081 			if (nla_type(tmp) != NFTA_LIST_ELEM)
2082 				goto err1;
2083 			if (n == NFT_RULE_MAXEXPRS)
2084 				goto err1;
2085 			err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
2086 			if (err < 0)
2087 				goto err1;
2088 			size += info[n].ops->size;
2089 			n++;
2090 		}
2091 	}
2092 	/* Check for overflow of dlen field */
2093 	err = -EFBIG;
2094 	if (size >= 1 << 12)
2095 		goto err1;
2096 
2097 	if (nla[NFTA_RULE_USERDATA]) {
2098 		ulen = nla_len(nla[NFTA_RULE_USERDATA]);
2099 		if (ulen > 0)
2100 			usize = sizeof(struct nft_userdata) + ulen;
2101 	}
2102 
2103 	err = -ENOMEM;
2104 	rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
2105 	if (rule == NULL)
2106 		goto err1;
2107 
2108 	nft_rule_activate_next(net, rule);
2109 
2110 	rule->handle = handle;
2111 	rule->dlen   = size;
2112 	rule->udata  = ulen ? 1 : 0;
2113 
2114 	if (ulen) {
2115 		udata = nft_userdata(rule);
2116 		udata->len = ulen - 1;
2117 		nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2118 	}
2119 
2120 	expr = nft_expr_first(rule);
2121 	for (i = 0; i < n; i++) {
2122 		err = nf_tables_newexpr(&ctx, &info[i], expr);
2123 		if (err < 0)
2124 			goto err2;
2125 		info[i].ops = NULL;
2126 		expr = nft_expr_next(expr);
2127 	}
2128 
2129 	if (nlh->nlmsg_flags & NLM_F_REPLACE) {
2130 		if (nft_rule_is_active_next(net, old_rule)) {
2131 			trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
2132 						   old_rule);
2133 			if (trans == NULL) {
2134 				err = -ENOMEM;
2135 				goto err2;
2136 			}
2137 			nft_rule_deactivate_next(net, old_rule);
2138 			chain->use--;
2139 			list_add_tail_rcu(&rule->list, &old_rule->list);
2140 		} else {
2141 			err = -ENOENT;
2142 			goto err2;
2143 		}
2144 	} else if (nlh->nlmsg_flags & NLM_F_APPEND)
2145 		if (old_rule)
2146 			list_add_rcu(&rule->list, &old_rule->list);
2147 		else
2148 			list_add_tail_rcu(&rule->list, &chain->rules);
2149 	else {
2150 		if (old_rule)
2151 			list_add_tail_rcu(&rule->list, &old_rule->list);
2152 		else
2153 			list_add_rcu(&rule->list, &chain->rules);
2154 	}
2155 
2156 	if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2157 		err = -ENOMEM;
2158 		goto err3;
2159 	}
2160 	chain->use++;
2161 	return 0;
2162 
2163 err3:
2164 	list_del_rcu(&rule->list);
2165 err2:
2166 	nf_tables_rule_destroy(&ctx, rule);
2167 err1:
2168 	for (i = 0; i < n; i++) {
2169 		if (info[i].ops != NULL)
2170 			module_put(info[i].ops->type->owner);
2171 	}
2172 	return err;
2173 }
2174 
nf_tables_delrule(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])2175 static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2176 			     struct sk_buff *skb, const struct nlmsghdr *nlh,
2177 			     const struct nlattr * const nla[])
2178 {
2179 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2180 	struct nft_af_info *afi;
2181 	struct nft_table *table;
2182 	struct nft_chain *chain = NULL;
2183 	struct nft_rule *rule;
2184 	int family = nfmsg->nfgen_family, err = 0;
2185 	struct nft_ctx ctx;
2186 
2187 	afi = nf_tables_afinfo_lookup(net, family, false);
2188 	if (IS_ERR(afi))
2189 		return PTR_ERR(afi);
2190 
2191 	table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
2192 	if (IS_ERR(table))
2193 		return PTR_ERR(table);
2194 	if (table->flags & NFT_TABLE_INACTIVE)
2195 		return -ENOENT;
2196 
2197 	if (nla[NFTA_RULE_CHAIN]) {
2198 		chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2199 		if (IS_ERR(chain))
2200 			return PTR_ERR(chain);
2201 	}
2202 
2203 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
2204 
2205 	if (chain) {
2206 		if (nla[NFTA_RULE_HANDLE]) {
2207 			rule = nf_tables_rule_lookup(chain,
2208 						     nla[NFTA_RULE_HANDLE]);
2209 			if (IS_ERR(rule))
2210 				return PTR_ERR(rule);
2211 
2212 			err = nft_delrule(&ctx, rule);
2213 		} else {
2214 			err = nft_delrule_by_chain(&ctx);
2215 		}
2216 	} else {
2217 		list_for_each_entry(chain, &table->chains, list) {
2218 			ctx.chain = chain;
2219 			err = nft_delrule_by_chain(&ctx);
2220 			if (err < 0)
2221 				break;
2222 		}
2223 	}
2224 
2225 	return err;
2226 }
2227 
2228 /*
2229  * Sets
2230  */
2231 
2232 static LIST_HEAD(nf_tables_set_ops);
2233 
nft_register_set(struct nft_set_ops * ops)2234 int nft_register_set(struct nft_set_ops *ops)
2235 {
2236 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
2237 	list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
2238 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2239 	return 0;
2240 }
2241 EXPORT_SYMBOL_GPL(nft_register_set);
2242 
nft_unregister_set(struct nft_set_ops * ops)2243 void nft_unregister_set(struct nft_set_ops *ops)
2244 {
2245 	nfnl_lock(NFNL_SUBSYS_NFTABLES);
2246 	list_del_rcu(&ops->list);
2247 	nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2248 }
2249 EXPORT_SYMBOL_GPL(nft_unregister_set);
2250 
2251 /*
2252  * Select a set implementation based on the data characteristics and the
2253  * given policy. The total memory use might not be known if no size is
2254  * given, in that case the amount of memory per element is used.
2255  */
2256 static const struct nft_set_ops *
nft_select_set_ops(const struct nlattr * const nla[],const struct nft_set_desc * desc,enum nft_set_policies policy)2257 nft_select_set_ops(const struct nlattr * const nla[],
2258 		   const struct nft_set_desc *desc,
2259 		   enum nft_set_policies policy)
2260 {
2261 	const struct nft_set_ops *ops, *bops;
2262 	struct nft_set_estimate est, best;
2263 	u32 features;
2264 
2265 #ifdef CONFIG_MODULES
2266 	if (list_empty(&nf_tables_set_ops)) {
2267 		nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2268 		request_module("nft-set");
2269 		nfnl_lock(NFNL_SUBSYS_NFTABLES);
2270 		if (!list_empty(&nf_tables_set_ops))
2271 			return ERR_PTR(-EAGAIN);
2272 	}
2273 #endif
2274 	features = 0;
2275 	if (nla[NFTA_SET_FLAGS] != NULL) {
2276 		features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2277 		features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT;
2278 	}
2279 
2280 	bops	   = NULL;
2281 	best.size  = ~0;
2282 	best.class = ~0;
2283 
2284 	list_for_each_entry(ops, &nf_tables_set_ops, list) {
2285 		if ((ops->features & features) != features)
2286 			continue;
2287 		if (!ops->estimate(desc, features, &est))
2288 			continue;
2289 
2290 		switch (policy) {
2291 		case NFT_SET_POL_PERFORMANCE:
2292 			if (est.class < best.class)
2293 				break;
2294 			if (est.class == best.class && est.size < best.size)
2295 				break;
2296 			continue;
2297 		case NFT_SET_POL_MEMORY:
2298 			if (est.size < best.size)
2299 				break;
2300 			if (est.size == best.size && est.class < best.class)
2301 				break;
2302 			continue;
2303 		default:
2304 			break;
2305 		}
2306 
2307 		if (!try_module_get(ops->owner))
2308 			continue;
2309 		if (bops != NULL)
2310 			module_put(bops->owner);
2311 
2312 		bops = ops;
2313 		best = est;
2314 	}
2315 
2316 	if (bops != NULL)
2317 		return bops;
2318 
2319 	return ERR_PTR(-EOPNOTSUPP);
2320 }
2321 
2322 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2323 	[NFTA_SET_TABLE]		= { .type = NLA_STRING },
2324 	[NFTA_SET_NAME]			= { .type = NLA_STRING,
2325 					    .len = IFNAMSIZ - 1 },
2326 	[NFTA_SET_FLAGS]		= { .type = NLA_U32 },
2327 	[NFTA_SET_KEY_TYPE]		= { .type = NLA_U32 },
2328 	[NFTA_SET_KEY_LEN]		= { .type = NLA_U32 },
2329 	[NFTA_SET_DATA_TYPE]		= { .type = NLA_U32 },
2330 	[NFTA_SET_DATA_LEN]		= { .type = NLA_U32 },
2331 	[NFTA_SET_POLICY]		= { .type = NLA_U32 },
2332 	[NFTA_SET_DESC]			= { .type = NLA_NESTED },
2333 	[NFTA_SET_ID]			= { .type = NLA_U32 },
2334 	[NFTA_SET_TIMEOUT]		= { .type = NLA_U64 },
2335 	[NFTA_SET_GC_INTERVAL]		= { .type = NLA_U32 },
2336 };
2337 
2338 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2339 	[NFTA_SET_DESC_SIZE]		= { .type = NLA_U32 },
2340 };
2341 
nft_ctx_init_from_setattr(struct nft_ctx * ctx,struct net * net,const struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])2342 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
2343 				     const struct sk_buff *skb,
2344 				     const struct nlmsghdr *nlh,
2345 				     const struct nlattr * const nla[])
2346 {
2347 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2348 	struct nft_af_info *afi = NULL;
2349 	struct nft_table *table = NULL;
2350 
2351 	if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2352 		afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2353 		if (IS_ERR(afi))
2354 			return PTR_ERR(afi);
2355 	}
2356 
2357 	if (nla[NFTA_SET_TABLE] != NULL) {
2358 		if (afi == NULL)
2359 			return -EAFNOSUPPORT;
2360 
2361 		table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2362 		if (IS_ERR(table))
2363 			return PTR_ERR(table);
2364 		if (table->flags & NFT_TABLE_INACTIVE)
2365 			return -ENOENT;
2366 	}
2367 
2368 	nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
2369 	return 0;
2370 }
2371 
nf_tables_set_lookup(const struct nft_table * table,const struct nlattr * nla)2372 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2373 				     const struct nlattr *nla)
2374 {
2375 	struct nft_set *set;
2376 
2377 	if (nla == NULL)
2378 		return ERR_PTR(-EINVAL);
2379 
2380 	list_for_each_entry(set, &table->sets, list) {
2381 		if (!nla_strcmp(nla, set->name))
2382 			return set;
2383 	}
2384 	return ERR_PTR(-ENOENT);
2385 }
2386 
nf_tables_set_lookup_byid(const struct net * net,const struct nlattr * nla)2387 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2388 					  const struct nlattr *nla)
2389 {
2390 	struct nft_trans *trans;
2391 	u32 id = ntohl(nla_get_be32(nla));
2392 
2393 	list_for_each_entry(trans, &net->nft.commit_list, list) {
2394 		if (trans->msg_type == NFT_MSG_NEWSET &&
2395 		    id == nft_trans_set_id(trans))
2396 			return nft_trans_set(trans);
2397 	}
2398 	return ERR_PTR(-ENOENT);
2399 }
2400 
nf_tables_set_alloc_name(struct nft_ctx * ctx,struct nft_set * set,const char * name)2401 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2402 				    const char *name)
2403 {
2404 	const struct nft_set *i;
2405 	const char *p;
2406 	unsigned long *inuse;
2407 	unsigned int n = 0, min = 0;
2408 
2409 	p = strnchr(name, IFNAMSIZ, '%');
2410 	if (p != NULL) {
2411 		if (p[1] != 'd' || strchr(p + 2, '%'))
2412 			return -EINVAL;
2413 
2414 		inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2415 		if (inuse == NULL)
2416 			return -ENOMEM;
2417 cont:
2418 		list_for_each_entry(i, &ctx->table->sets, list) {
2419 			int tmp;
2420 
2421 			if (!sscanf(i->name, name, &tmp))
2422 				continue;
2423 			if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2424 				continue;
2425 
2426 			set_bit(tmp - min, inuse);
2427 		}
2428 
2429 		n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2430 		if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2431 			min += BITS_PER_BYTE * PAGE_SIZE;
2432 			memset(inuse, 0, PAGE_SIZE);
2433 			goto cont;
2434 		}
2435 		free_page((unsigned long)inuse);
2436 	}
2437 
2438 	snprintf(set->name, sizeof(set->name), name, min + n);
2439 	list_for_each_entry(i, &ctx->table->sets, list) {
2440 		if (!strcmp(set->name, i->name))
2441 			return -ENFILE;
2442 	}
2443 	return 0;
2444 }
2445 
nf_tables_fill_set(struct sk_buff * skb,const struct nft_ctx * ctx,const struct nft_set * set,u16 event,u16 flags)2446 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2447 			      const struct nft_set *set, u16 event, u16 flags)
2448 {
2449 	struct nfgenmsg *nfmsg;
2450 	struct nlmsghdr *nlh;
2451 	struct nlattr *desc;
2452 	u32 portid = ctx->portid;
2453 	u32 seq = ctx->seq;
2454 
2455 	event |= NFNL_SUBSYS_NFTABLES << 8;
2456 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2457 			flags);
2458 	if (nlh == NULL)
2459 		goto nla_put_failure;
2460 
2461 	nfmsg = nlmsg_data(nlh);
2462 	nfmsg->nfgen_family	= ctx->afi->family;
2463 	nfmsg->version		= NFNETLINK_V0;
2464 	nfmsg->res_id		= htons(ctx->net->nft.base_seq & 0xffff);
2465 
2466 	if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2467 		goto nla_put_failure;
2468 	if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2469 		goto nla_put_failure;
2470 	if (set->flags != 0)
2471 		if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2472 			goto nla_put_failure;
2473 
2474 	if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2475 		goto nla_put_failure;
2476 	if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2477 		goto nla_put_failure;
2478 	if (set->flags & NFT_SET_MAP) {
2479 		if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2480 			goto nla_put_failure;
2481 		if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2482 			goto nla_put_failure;
2483 	}
2484 
2485 	if (set->timeout &&
2486 	    nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout)))
2487 		goto nla_put_failure;
2488 	if (set->gc_int &&
2489 	    nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2490 		goto nla_put_failure;
2491 
2492 	if (set->policy != NFT_SET_POL_PERFORMANCE) {
2493 		if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2494 			goto nla_put_failure;
2495 	}
2496 
2497 	desc = nla_nest_start(skb, NFTA_SET_DESC);
2498 	if (desc == NULL)
2499 		goto nla_put_failure;
2500 	if (set->size &&
2501 	    nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2502 		goto nla_put_failure;
2503 	nla_nest_end(skb, desc);
2504 
2505 	nlmsg_end(skb, nlh);
2506 	return 0;
2507 
2508 nla_put_failure:
2509 	nlmsg_trim(skb, nlh);
2510 	return -1;
2511 }
2512 
nf_tables_set_notify(const struct nft_ctx * ctx,const struct nft_set * set,int event,gfp_t gfp_flags)2513 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2514 				const struct nft_set *set,
2515 				int event, gfp_t gfp_flags)
2516 {
2517 	struct sk_buff *skb;
2518 	u32 portid = ctx->portid;
2519 	int err;
2520 
2521 	if (!ctx->report &&
2522 	    !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2523 		return 0;
2524 
2525 	err = -ENOBUFS;
2526 	skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2527 	if (skb == NULL)
2528 		goto err;
2529 
2530 	err = nf_tables_fill_set(skb, ctx, set, event, 0);
2531 	if (err < 0) {
2532 		kfree_skb(skb);
2533 		goto err;
2534 	}
2535 
2536 	err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2537 			     ctx->report, gfp_flags);
2538 err:
2539 	if (err < 0)
2540 		nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2541 	return err;
2542 }
2543 
nf_tables_dump_sets(struct sk_buff * skb,struct netlink_callback * cb)2544 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2545 {
2546 	const struct nft_set *set;
2547 	unsigned int idx, s_idx = cb->args[0];
2548 	struct nft_af_info *afi;
2549 	struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2550 	struct net *net = sock_net(skb->sk);
2551 	int cur_family = cb->args[3];
2552 	struct nft_ctx *ctx = cb->data, ctx_set;
2553 
2554 	if (cb->args[1])
2555 		return skb->len;
2556 
2557 	rcu_read_lock();
2558 	cb->seq = net->nft.base_seq;
2559 
2560 	list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2561 		if (ctx->afi && ctx->afi != afi)
2562 			continue;
2563 
2564 		if (cur_family) {
2565 			if (afi->family != cur_family)
2566 				continue;
2567 
2568 			cur_family = 0;
2569 		}
2570 		list_for_each_entry_rcu(table, &afi->tables, list) {
2571 			if (ctx->table && ctx->table != table)
2572 				continue;
2573 
2574 			if (cur_table) {
2575 				if (cur_table != table)
2576 					continue;
2577 
2578 				cur_table = NULL;
2579 			}
2580 			idx = 0;
2581 			list_for_each_entry_rcu(set, &table->sets, list) {
2582 				if (idx < s_idx)
2583 					goto cont;
2584 
2585 				ctx_set = *ctx;
2586 				ctx_set.table = table;
2587 				ctx_set.afi = afi;
2588 				if (nf_tables_fill_set(skb, &ctx_set, set,
2589 						       NFT_MSG_NEWSET,
2590 						       NLM_F_MULTI) < 0) {
2591 					cb->args[0] = idx;
2592 					cb->args[2] = (unsigned long) table;
2593 					cb->args[3] = afi->family;
2594 					goto done;
2595 				}
2596 				nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2597 cont:
2598 				idx++;
2599 			}
2600 			if (s_idx)
2601 				s_idx = 0;
2602 		}
2603 	}
2604 	cb->args[1] = 1;
2605 done:
2606 	rcu_read_unlock();
2607 	return skb->len;
2608 }
2609 
nf_tables_dump_sets_done(struct netlink_callback * cb)2610 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
2611 {
2612 	kfree(cb->data);
2613 	return 0;
2614 }
2615 
nf_tables_getset(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])2616 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2617 			    const struct nlmsghdr *nlh,
2618 			    const struct nlattr * const nla[])
2619 {
2620 	struct net *net = sock_net(skb->sk);
2621 	const struct nft_set *set;
2622 	struct nft_ctx ctx;
2623 	struct sk_buff *skb2;
2624 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2625 	int err;
2626 
2627 	/* Verify existence before starting dump */
2628 	err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla);
2629 	if (err < 0)
2630 		return err;
2631 
2632 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
2633 		struct netlink_dump_control c = {
2634 			.dump = nf_tables_dump_sets,
2635 			.done = nf_tables_dump_sets_done,
2636 		};
2637 		struct nft_ctx *ctx_dump;
2638 
2639 		ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2640 		if (ctx_dump == NULL)
2641 			return -ENOMEM;
2642 
2643 		*ctx_dump = ctx;
2644 		c.data = ctx_dump;
2645 
2646 		return netlink_dump_start(nlsk, skb, nlh, &c);
2647 	}
2648 
2649 	/* Only accept unspec with dump */
2650 	if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2651 		return -EAFNOSUPPORT;
2652 
2653 	set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2654 	if (IS_ERR(set))
2655 		return PTR_ERR(set);
2656 	if (set->flags & NFT_SET_INACTIVE)
2657 		return -ENOENT;
2658 
2659 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2660 	if (skb2 == NULL)
2661 		return -ENOMEM;
2662 
2663 	err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2664 	if (err < 0)
2665 		goto err;
2666 
2667 	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2668 
2669 err:
2670 	kfree_skb(skb2);
2671 	return err;
2672 }
2673 
nf_tables_set_desc_parse(const struct nft_ctx * ctx,struct nft_set_desc * desc,const struct nlattr * nla)2674 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2675 				    struct nft_set_desc *desc,
2676 				    const struct nlattr *nla)
2677 {
2678 	struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2679 	int err;
2680 
2681 	err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2682 	if (err < 0)
2683 		return err;
2684 
2685 	if (da[NFTA_SET_DESC_SIZE] != NULL)
2686 		desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2687 
2688 	return 0;
2689 }
2690 
nf_tables_newset(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])2691 static int nf_tables_newset(struct net *net, struct sock *nlsk,
2692 			    struct sk_buff *skb, const struct nlmsghdr *nlh,
2693 			    const struct nlattr * const nla[])
2694 {
2695 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2696 	const struct nft_set_ops *ops;
2697 	struct nft_af_info *afi;
2698 	struct nft_table *table;
2699 	struct nft_set *set;
2700 	struct nft_ctx ctx;
2701 	char name[IFNAMSIZ];
2702 	unsigned int size;
2703 	bool create;
2704 	u64 timeout;
2705 	u32 ktype, dtype, flags, policy, gc_int;
2706 	struct nft_set_desc desc;
2707 	int err;
2708 
2709 	if (nla[NFTA_SET_TABLE] == NULL ||
2710 	    nla[NFTA_SET_NAME] == NULL ||
2711 	    nla[NFTA_SET_KEY_LEN] == NULL ||
2712 	    nla[NFTA_SET_ID] == NULL)
2713 		return -EINVAL;
2714 
2715 	memset(&desc, 0, sizeof(desc));
2716 
2717 	ktype = NFT_DATA_VALUE;
2718 	if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2719 		ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2720 		if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2721 			return -EINVAL;
2722 	}
2723 
2724 	desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2725 	if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
2726 		return -EINVAL;
2727 
2728 	flags = 0;
2729 	if (nla[NFTA_SET_FLAGS] != NULL) {
2730 		flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2731 		if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2732 			      NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
2733 			      NFT_SET_MAP | NFT_SET_EVAL))
2734 			return -EINVAL;
2735 		/* Only one of both operations is supported */
2736 		if ((flags & (NFT_SET_MAP | NFT_SET_EVAL)) ==
2737 			     (NFT_SET_MAP | NFT_SET_EVAL))
2738 			return -EOPNOTSUPP;
2739 	}
2740 
2741 	dtype = 0;
2742 	if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2743 		if (!(flags & NFT_SET_MAP))
2744 			return -EINVAL;
2745 
2746 		dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2747 		if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2748 		    dtype != NFT_DATA_VERDICT)
2749 			return -EINVAL;
2750 
2751 		if (dtype != NFT_DATA_VERDICT) {
2752 			if (nla[NFTA_SET_DATA_LEN] == NULL)
2753 				return -EINVAL;
2754 			desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2755 			if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
2756 				return -EINVAL;
2757 		} else
2758 			desc.dlen = sizeof(struct nft_verdict);
2759 	} else if (flags & NFT_SET_MAP)
2760 		return -EINVAL;
2761 
2762 	timeout = 0;
2763 	if (nla[NFTA_SET_TIMEOUT] != NULL) {
2764 		if (!(flags & NFT_SET_TIMEOUT))
2765 			return -EINVAL;
2766 		timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT]));
2767 	}
2768 	gc_int = 0;
2769 	if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2770 		if (!(flags & NFT_SET_TIMEOUT))
2771 			return -EINVAL;
2772 		gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2773 	}
2774 
2775 	policy = NFT_SET_POL_PERFORMANCE;
2776 	if (nla[NFTA_SET_POLICY] != NULL)
2777 		policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2778 
2779 	if (nla[NFTA_SET_DESC] != NULL) {
2780 		err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2781 		if (err < 0)
2782 			return err;
2783 	}
2784 
2785 	create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2786 
2787 	afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2788 	if (IS_ERR(afi))
2789 		return PTR_ERR(afi);
2790 
2791 	table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2792 	if (IS_ERR(table))
2793 		return PTR_ERR(table);
2794 
2795 	nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
2796 
2797 	set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2798 	if (IS_ERR(set)) {
2799 		if (PTR_ERR(set) != -ENOENT)
2800 			return PTR_ERR(set);
2801 		set = NULL;
2802 	}
2803 
2804 	if (set != NULL) {
2805 		if (nlh->nlmsg_flags & NLM_F_EXCL)
2806 			return -EEXIST;
2807 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
2808 			return -EOPNOTSUPP;
2809 		return 0;
2810 	}
2811 
2812 	if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2813 		return -ENOENT;
2814 
2815 	ops = nft_select_set_ops(nla, &desc, policy);
2816 	if (IS_ERR(ops))
2817 		return PTR_ERR(ops);
2818 
2819 	size = 0;
2820 	if (ops->privsize != NULL)
2821 		size = ops->privsize(nla);
2822 
2823 	err = -ENOMEM;
2824 	set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2825 	if (set == NULL)
2826 		goto err1;
2827 
2828 	nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2829 	err = nf_tables_set_alloc_name(&ctx, set, name);
2830 	if (err < 0)
2831 		goto err2;
2832 
2833 	INIT_LIST_HEAD(&set->bindings);
2834 	write_pnet(&set->pnet, net);
2835 	set->ops   = ops;
2836 	set->ktype = ktype;
2837 	set->klen  = desc.klen;
2838 	set->dtype = dtype;
2839 	set->dlen  = desc.dlen;
2840 	set->flags = flags;
2841 	set->size  = desc.size;
2842 	set->policy = policy;
2843 	set->timeout = timeout;
2844 	set->gc_int = gc_int;
2845 
2846 	err = ops->init(set, &desc, nla);
2847 	if (err < 0)
2848 		goto err2;
2849 
2850 	err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2851 	if (err < 0)
2852 		goto err2;
2853 
2854 	list_add_tail_rcu(&set->list, &table->sets);
2855 	table->use++;
2856 	return 0;
2857 
2858 err2:
2859 	kfree(set);
2860 err1:
2861 	module_put(ops->owner);
2862 	return err;
2863 }
2864 
nft_set_destroy(struct nft_set * set)2865 static void nft_set_destroy(struct nft_set *set)
2866 {
2867 	set->ops->destroy(set);
2868 	module_put(set->ops->owner);
2869 	kfree(set);
2870 }
2871 
nf_tables_set_destroy(const struct nft_ctx * ctx,struct nft_set * set)2872 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2873 {
2874 	list_del_rcu(&set->list);
2875 	nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2876 	nft_set_destroy(set);
2877 }
2878 
nf_tables_delset(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])2879 static int nf_tables_delset(struct net *net, struct sock *nlsk,
2880 			    struct sk_buff *skb, const struct nlmsghdr *nlh,
2881 			    const struct nlattr * const nla[])
2882 {
2883 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2884 	struct nft_set *set;
2885 	struct nft_ctx ctx;
2886 	int err;
2887 
2888 	if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2889 		return -EAFNOSUPPORT;
2890 	if (nla[NFTA_SET_TABLE] == NULL)
2891 		return -EINVAL;
2892 
2893 	err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla);
2894 	if (err < 0)
2895 		return err;
2896 
2897 	set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2898 	if (IS_ERR(set))
2899 		return PTR_ERR(set);
2900 	if (set->flags & NFT_SET_INACTIVE)
2901 		return -ENOENT;
2902 	if (!list_empty(&set->bindings))
2903 		return -EBUSY;
2904 
2905 	return nft_delset(&ctx, set);
2906 }
2907 
nf_tables_bind_check_setelem(const struct nft_ctx * ctx,const struct nft_set * set,const struct nft_set_iter * iter,const struct nft_set_elem * elem)2908 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2909 					const struct nft_set *set,
2910 					const struct nft_set_iter *iter,
2911 					const struct nft_set_elem *elem)
2912 {
2913 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
2914 	enum nft_registers dreg;
2915 
2916 	dreg = nft_type_to_reg(set->dtype);
2917 	return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
2918 					   set->dtype == NFT_DATA_VERDICT ?
2919 					   NFT_DATA_VERDICT : NFT_DATA_VALUE,
2920 					   set->dlen);
2921 }
2922 
nf_tables_bind_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding)2923 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2924 		       struct nft_set_binding *binding)
2925 {
2926 	struct nft_set_binding *i;
2927 	struct nft_set_iter iter;
2928 
2929 	if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2930 		return -EBUSY;
2931 
2932 	if (binding->flags & NFT_SET_MAP) {
2933 		/* If the set is already bound to the same chain all
2934 		 * jumps are already validated for that chain.
2935 		 */
2936 		list_for_each_entry(i, &set->bindings, list) {
2937 			if (binding->flags & NFT_SET_MAP &&
2938 			    i->chain == binding->chain)
2939 				goto bind;
2940 		}
2941 
2942 		iter.skip 	= 0;
2943 		iter.count	= 0;
2944 		iter.err	= 0;
2945 		iter.fn		= nf_tables_bind_check_setelem;
2946 
2947 		set->ops->walk(ctx, set, &iter);
2948 		if (iter.err < 0) {
2949 			/* Destroy anonymous sets if binding fails */
2950 			if (set->flags & NFT_SET_ANONYMOUS)
2951 				nf_tables_set_destroy(ctx, set);
2952 
2953 			return iter.err;
2954 		}
2955 	}
2956 bind:
2957 	binding->chain = ctx->chain;
2958 	list_add_tail_rcu(&binding->list, &set->bindings);
2959 	return 0;
2960 }
2961 
nf_tables_unbind_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding)2962 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2963 			  struct nft_set_binding *binding)
2964 {
2965 	list_del_rcu(&binding->list);
2966 
2967 	if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2968 	    !(set->flags & NFT_SET_INACTIVE))
2969 		nf_tables_set_destroy(ctx, set);
2970 }
2971 
2972 const struct nft_set_ext_type nft_set_ext_types[] = {
2973 	[NFT_SET_EXT_KEY]		= {
2974 		.align	= __alignof__(u32),
2975 	},
2976 	[NFT_SET_EXT_DATA]		= {
2977 		.align	= __alignof__(u32),
2978 	},
2979 	[NFT_SET_EXT_EXPR]		= {
2980 		.align	= __alignof__(struct nft_expr),
2981 	},
2982 	[NFT_SET_EXT_FLAGS]		= {
2983 		.len	= sizeof(u8),
2984 		.align	= __alignof__(u8),
2985 	},
2986 	[NFT_SET_EXT_TIMEOUT]		= {
2987 		.len	= sizeof(u64),
2988 		.align	= __alignof__(u64),
2989 	},
2990 	[NFT_SET_EXT_EXPIRATION]	= {
2991 		.len	= sizeof(unsigned long),
2992 		.align	= __alignof__(unsigned long),
2993 	},
2994 	[NFT_SET_EXT_USERDATA]		= {
2995 		.len	= sizeof(struct nft_userdata),
2996 		.align	= __alignof__(struct nft_userdata),
2997 	},
2998 };
2999 EXPORT_SYMBOL_GPL(nft_set_ext_types);
3000 
3001 /*
3002  * Set elements
3003  */
3004 
3005 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3006 	[NFTA_SET_ELEM_KEY]		= { .type = NLA_NESTED },
3007 	[NFTA_SET_ELEM_DATA]		= { .type = NLA_NESTED },
3008 	[NFTA_SET_ELEM_FLAGS]		= { .type = NLA_U32 },
3009 	[NFTA_SET_ELEM_TIMEOUT]		= { .type = NLA_U64 },
3010 	[NFTA_SET_ELEM_USERDATA]	= { .type = NLA_BINARY,
3011 					    .len = NFT_USERDATA_MAXLEN },
3012 };
3013 
3014 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3015 	[NFTA_SET_ELEM_LIST_TABLE]	= { .type = NLA_STRING },
3016 	[NFTA_SET_ELEM_LIST_SET]	= { .type = NLA_STRING },
3017 	[NFTA_SET_ELEM_LIST_ELEMENTS]	= { .type = NLA_NESTED },
3018 	[NFTA_SET_ELEM_LIST_SET_ID]	= { .type = NLA_U32 },
3019 };
3020 
nft_ctx_init_from_elemattr(struct nft_ctx * ctx,struct net * net,const struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[],bool trans)3021 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
3022 				      const struct sk_buff *skb,
3023 				      const struct nlmsghdr *nlh,
3024 				      const struct nlattr * const nla[],
3025 				      bool trans)
3026 {
3027 	const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3028 	struct nft_af_info *afi;
3029 	struct nft_table *table;
3030 
3031 	afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
3032 	if (IS_ERR(afi))
3033 		return PTR_ERR(afi);
3034 
3035 	table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
3036 	if (IS_ERR(table))
3037 		return PTR_ERR(table);
3038 	if (!trans && (table->flags & NFT_TABLE_INACTIVE))
3039 		return -ENOENT;
3040 
3041 	nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
3042 	return 0;
3043 }
3044 
nf_tables_fill_setelem(struct sk_buff * skb,const struct nft_set * set,const struct nft_set_elem * elem)3045 static int nf_tables_fill_setelem(struct sk_buff *skb,
3046 				  const struct nft_set *set,
3047 				  const struct nft_set_elem *elem)
3048 {
3049 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3050 	unsigned char *b = skb_tail_pointer(skb);
3051 	struct nlattr *nest;
3052 
3053 	nest = nla_nest_start(skb, NFTA_LIST_ELEM);
3054 	if (nest == NULL)
3055 		goto nla_put_failure;
3056 
3057 	if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3058 			  NFT_DATA_VALUE, set->klen) < 0)
3059 		goto nla_put_failure;
3060 
3061 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3062 	    nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
3063 			  set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3064 			  set->dlen) < 0)
3065 		goto nla_put_failure;
3066 
3067 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3068 	    nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3069 		goto nla_put_failure;
3070 
3071 	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3072 	    nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3073 		         htonl(*nft_set_ext_flags(ext))))
3074 		goto nla_put_failure;
3075 
3076 	if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3077 	    nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
3078 			 cpu_to_be64(*nft_set_ext_timeout(ext))))
3079 		goto nla_put_failure;
3080 
3081 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3082 		unsigned long expires, now = jiffies;
3083 
3084 		expires = *nft_set_ext_expiration(ext);
3085 		if (time_before(now, expires))
3086 			expires -= now;
3087 		else
3088 			expires = 0;
3089 
3090 		if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
3091 				 cpu_to_be64(jiffies_to_msecs(expires))))
3092 			goto nla_put_failure;
3093 	}
3094 
3095 	if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3096 		struct nft_userdata *udata;
3097 
3098 		udata = nft_set_ext_userdata(ext);
3099 		if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3100 			    udata->len + 1, udata->data))
3101 			goto nla_put_failure;
3102 	}
3103 
3104 	nla_nest_end(skb, nest);
3105 	return 0;
3106 
3107 nla_put_failure:
3108 	nlmsg_trim(skb, b);
3109 	return -EMSGSIZE;
3110 }
3111 
3112 struct nft_set_dump_args {
3113 	const struct netlink_callback	*cb;
3114 	struct nft_set_iter		iter;
3115 	struct sk_buff			*skb;
3116 };
3117 
nf_tables_dump_setelem(const struct nft_ctx * ctx,const struct nft_set * set,const struct nft_set_iter * iter,const struct nft_set_elem * elem)3118 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
3119 				  const struct nft_set *set,
3120 				  const struct nft_set_iter *iter,
3121 				  const struct nft_set_elem *elem)
3122 {
3123 	struct nft_set_dump_args *args;
3124 
3125 	args = container_of(iter, struct nft_set_dump_args, iter);
3126 	return nf_tables_fill_setelem(args->skb, set, elem);
3127 }
3128 
nf_tables_dump_set(struct sk_buff * skb,struct netlink_callback * cb)3129 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
3130 {
3131 	struct net *net = sock_net(skb->sk);
3132 	const struct nft_set *set;
3133 	struct nft_set_dump_args args;
3134 	struct nft_ctx ctx;
3135 	struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
3136 	struct nfgenmsg *nfmsg;
3137 	struct nlmsghdr *nlh;
3138 	struct nlattr *nest;
3139 	u32 portid, seq;
3140 	int event, err;
3141 
3142 	err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
3143 			  NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
3144 	if (err < 0)
3145 		return err;
3146 
3147 	err = nft_ctx_init_from_elemattr(&ctx, net, cb->skb, cb->nlh,
3148 					 (void *)nla, false);
3149 	if (err < 0)
3150 		return err;
3151 
3152 	set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3153 	if (IS_ERR(set))
3154 		return PTR_ERR(set);
3155 	if (set->flags & NFT_SET_INACTIVE)
3156 		return -ENOENT;
3157 
3158 	event  = NFT_MSG_NEWSETELEM;
3159 	event |= NFNL_SUBSYS_NFTABLES << 8;
3160 	portid = NETLINK_CB(cb->skb).portid;
3161 	seq    = cb->nlh->nlmsg_seq;
3162 
3163 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3164 			NLM_F_MULTI);
3165 	if (nlh == NULL)
3166 		goto nla_put_failure;
3167 
3168 	nfmsg = nlmsg_data(nlh);
3169 	nfmsg->nfgen_family = ctx.afi->family;
3170 	nfmsg->version      = NFNETLINK_V0;
3171 	nfmsg->res_id	    = htons(ctx.net->nft.base_seq & 0xffff);
3172 
3173 	if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
3174 		goto nla_put_failure;
3175 	if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
3176 		goto nla_put_failure;
3177 
3178 	nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3179 	if (nest == NULL)
3180 		goto nla_put_failure;
3181 
3182 	args.cb		= cb;
3183 	args.skb	= skb;
3184 	args.iter.skip	= cb->args[0];
3185 	args.iter.count	= 0;
3186 	args.iter.err   = 0;
3187 	args.iter.fn	= nf_tables_dump_setelem;
3188 	set->ops->walk(&ctx, set, &args.iter);
3189 
3190 	nla_nest_end(skb, nest);
3191 	nlmsg_end(skb, nlh);
3192 
3193 	if (args.iter.err && args.iter.err != -EMSGSIZE)
3194 		return args.iter.err;
3195 	if (args.iter.count == cb->args[0])
3196 		return 0;
3197 
3198 	cb->args[0] = args.iter.count;
3199 	return skb->len;
3200 
3201 nla_put_failure:
3202 	return -ENOSPC;
3203 }
3204 
nf_tables_getsetelem(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])3205 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
3206 				const struct nlmsghdr *nlh,
3207 				const struct nlattr * const nla[])
3208 {
3209 	struct net *net = sock_net(skb->sk);
3210 	const struct nft_set *set;
3211 	struct nft_ctx ctx;
3212 	int err;
3213 
3214 	err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, false);
3215 	if (err < 0)
3216 		return err;
3217 
3218 	set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3219 	if (IS_ERR(set))
3220 		return PTR_ERR(set);
3221 	if (set->flags & NFT_SET_INACTIVE)
3222 		return -ENOENT;
3223 
3224 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
3225 		struct netlink_dump_control c = {
3226 			.dump = nf_tables_dump_set,
3227 		};
3228 		return netlink_dump_start(nlsk, skb, nlh, &c);
3229 	}
3230 	return -EOPNOTSUPP;
3231 }
3232 
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)3233 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3234 				       const struct nft_ctx *ctx, u32 seq,
3235 				       u32 portid, int event, u16 flags,
3236 				       const struct nft_set *set,
3237 				       const struct nft_set_elem *elem)
3238 {
3239 	struct nfgenmsg *nfmsg;
3240 	struct nlmsghdr *nlh;
3241 	struct nlattr *nest;
3242 	int err;
3243 
3244 	event |= NFNL_SUBSYS_NFTABLES << 8;
3245 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3246 			flags);
3247 	if (nlh == NULL)
3248 		goto nla_put_failure;
3249 
3250 	nfmsg = nlmsg_data(nlh);
3251 	nfmsg->nfgen_family	= ctx->afi->family;
3252 	nfmsg->version		= NFNETLINK_V0;
3253 	nfmsg->res_id		= htons(ctx->net->nft.base_seq & 0xffff);
3254 
3255 	if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3256 		goto nla_put_failure;
3257 	if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3258 		goto nla_put_failure;
3259 
3260 	nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3261 	if (nest == NULL)
3262 		goto nla_put_failure;
3263 
3264 	err = nf_tables_fill_setelem(skb, set, elem);
3265 	if (err < 0)
3266 		goto nla_put_failure;
3267 
3268 	nla_nest_end(skb, nest);
3269 
3270 	nlmsg_end(skb, nlh);
3271 	return 0;
3272 
3273 nla_put_failure:
3274 	nlmsg_trim(skb, nlh);
3275 	return -1;
3276 }
3277 
nf_tables_setelem_notify(const struct nft_ctx * ctx,const struct nft_set * set,const struct nft_set_elem * elem,int event,u16 flags)3278 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3279 				    const struct nft_set *set,
3280 				    const struct nft_set_elem *elem,
3281 				    int event, u16 flags)
3282 {
3283 	struct net *net = ctx->net;
3284 	u32 portid = ctx->portid;
3285 	struct sk_buff *skb;
3286 	int err;
3287 
3288 	if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3289 		return 0;
3290 
3291 	err = -ENOBUFS;
3292 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3293 	if (skb == NULL)
3294 		goto err;
3295 
3296 	err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3297 					  set, elem);
3298 	if (err < 0) {
3299 		kfree_skb(skb);
3300 		goto err;
3301 	}
3302 
3303 	err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3304 			     GFP_KERNEL);
3305 err:
3306 	if (err < 0)
3307 		nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3308 	return err;
3309 }
3310 
nft_trans_elem_alloc(struct nft_ctx * ctx,int msg_type,struct nft_set * set)3311 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3312 					      int msg_type,
3313 					      struct nft_set *set)
3314 {
3315 	struct nft_trans *trans;
3316 
3317 	trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3318 	if (trans == NULL)
3319 		return NULL;
3320 
3321 	nft_trans_elem_set(trans) = set;
3322 	return trans;
3323 }
3324 
nft_set_elem_init(const struct nft_set * set,const struct nft_set_ext_tmpl * tmpl,const u32 * key,const u32 * data,u64 timeout,gfp_t gfp)3325 void *nft_set_elem_init(const struct nft_set *set,
3326 			const struct nft_set_ext_tmpl *tmpl,
3327 			const u32 *key, const u32 *data,
3328 			u64 timeout, gfp_t gfp)
3329 {
3330 	struct nft_set_ext *ext;
3331 	void *elem;
3332 
3333 	elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3334 	if (elem == NULL)
3335 		return NULL;
3336 
3337 	ext = nft_set_elem_ext(set, elem);
3338 	nft_set_ext_init(ext, tmpl);
3339 
3340 	memcpy(nft_set_ext_key(ext), key, set->klen);
3341 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3342 		memcpy(nft_set_ext_data(ext), data, set->dlen);
3343 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3344 		*nft_set_ext_expiration(ext) =
3345 			jiffies + msecs_to_jiffies(timeout);
3346 	if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3347 		*nft_set_ext_timeout(ext) = timeout;
3348 
3349 	return elem;
3350 }
3351 
nft_set_elem_destroy(const struct nft_set * set,void * elem)3352 void nft_set_elem_destroy(const struct nft_set *set, void *elem)
3353 {
3354 	struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3355 
3356 	nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE);
3357 	if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3358 		nft_data_uninit(nft_set_ext_data(ext), set->dtype);
3359 	if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3360 		nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
3361 
3362 	kfree(elem);
3363 }
3364 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3365 
nft_add_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr)3366 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3367 			    const struct nlattr *attr)
3368 {
3369 	struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3370 	struct nft_data_desc d1, d2;
3371 	struct nft_set_ext_tmpl tmpl;
3372 	struct nft_set_ext *ext;
3373 	struct nft_set_elem elem;
3374 	struct nft_set_binding *binding;
3375 	struct nft_userdata *udata;
3376 	struct nft_data data;
3377 	enum nft_registers dreg;
3378 	struct nft_trans *trans;
3379 	u64 timeout;
3380 	u32 flags;
3381 	u8 ulen;
3382 	int err;
3383 
3384 	err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3385 			       nft_set_elem_policy);
3386 	if (err < 0)
3387 		return err;
3388 
3389 	if (nla[NFTA_SET_ELEM_KEY] == NULL)
3390 		return -EINVAL;
3391 
3392 	nft_set_ext_prepare(&tmpl);
3393 
3394 	flags = 0;
3395 	if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3396 		flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3397 		if (flags & ~NFT_SET_ELEM_INTERVAL_END)
3398 			return -EINVAL;
3399 		if (!(set->flags & NFT_SET_INTERVAL) &&
3400 		    flags & NFT_SET_ELEM_INTERVAL_END)
3401 			return -EINVAL;
3402 		if (flags != 0)
3403 			nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
3404 	}
3405 
3406 	if (set->flags & NFT_SET_MAP) {
3407 		if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3408 		    !(flags & NFT_SET_ELEM_INTERVAL_END))
3409 			return -EINVAL;
3410 		if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3411 		    flags & NFT_SET_ELEM_INTERVAL_END)
3412 			return -EINVAL;
3413 	} else {
3414 		if (nla[NFTA_SET_ELEM_DATA] != NULL)
3415 			return -EINVAL;
3416 	}
3417 
3418 	timeout = 0;
3419 	if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3420 		if (!(set->flags & NFT_SET_TIMEOUT))
3421 			return -EINVAL;
3422 		timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT]));
3423 	} else if (set->flags & NFT_SET_TIMEOUT) {
3424 		timeout = set->timeout;
3425 	}
3426 
3427 	err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
3428 			    nla[NFTA_SET_ELEM_KEY]);
3429 	if (err < 0)
3430 		goto err1;
3431 	err = -EINVAL;
3432 	if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3433 		goto err2;
3434 
3435 	nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
3436 	if (timeout > 0) {
3437 		nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3438 		if (timeout != set->timeout)
3439 			nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3440 	}
3441 
3442 	if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3443 		err = nft_data_init(ctx, &data, sizeof(data), &d2,
3444 				    nla[NFTA_SET_ELEM_DATA]);
3445 		if (err < 0)
3446 			goto err2;
3447 
3448 		err = -EINVAL;
3449 		if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3450 			goto err3;
3451 
3452 		dreg = nft_type_to_reg(set->dtype);
3453 		list_for_each_entry(binding, &set->bindings, list) {
3454 			struct nft_ctx bind_ctx = {
3455 				.afi	= ctx->afi,
3456 				.table	= ctx->table,
3457 				.chain	= (struct nft_chain *)binding->chain,
3458 			};
3459 
3460 			if (!(binding->flags & NFT_SET_MAP))
3461 				continue;
3462 
3463 			err = nft_validate_register_store(&bind_ctx, dreg,
3464 							  &data,
3465 							  d2.type, d2.len);
3466 			if (err < 0)
3467 				goto err3;
3468 		}
3469 
3470 		nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
3471 	}
3472 
3473 	/* The full maximum length of userdata can exceed the maximum
3474 	 * offset value (U8_MAX) for following extensions, therefor it
3475 	 * must be the last extension added.
3476 	 */
3477 	ulen = 0;
3478 	if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3479 		ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3480 		if (ulen > 0)
3481 			nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3482 					       ulen);
3483 	}
3484 
3485 	err = -ENOMEM;
3486 	elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
3487 				      timeout, GFP_KERNEL);
3488 	if (elem.priv == NULL)
3489 		goto err3;
3490 
3491 	ext = nft_set_elem_ext(set, elem.priv);
3492 	if (flags)
3493 		*nft_set_ext_flags(ext) = flags;
3494 	if (ulen > 0) {
3495 		udata = nft_set_ext_userdata(ext);
3496 		udata->len = ulen - 1;
3497 		nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3498 	}
3499 
3500 	trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3501 	if (trans == NULL)
3502 		goto err4;
3503 
3504 	ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
3505 	err = set->ops->insert(set, &elem);
3506 	if (err < 0)
3507 		goto err5;
3508 
3509 	nft_trans_elem(trans) = elem;
3510 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3511 	return 0;
3512 
3513 err5:
3514 	kfree(trans);
3515 err4:
3516 	kfree(elem.priv);
3517 err3:
3518 	if (nla[NFTA_SET_ELEM_DATA] != NULL)
3519 		nft_data_uninit(&data, d2.type);
3520 err2:
3521 	nft_data_uninit(&elem.key.val, d1.type);
3522 err1:
3523 	return err;
3524 }
3525 
nf_tables_newsetelem(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])3526 static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
3527 				struct sk_buff *skb, const struct nlmsghdr *nlh,
3528 				const struct nlattr * const nla[])
3529 {
3530 	const struct nlattr *attr;
3531 	struct nft_set *set;
3532 	struct nft_ctx ctx;
3533 	int rem, err = 0;
3534 
3535 	if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3536 		return -EINVAL;
3537 
3538 	err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, true);
3539 	if (err < 0)
3540 		return err;
3541 
3542 	set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3543 	if (IS_ERR(set)) {
3544 		if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3545 			set = nf_tables_set_lookup_byid(net,
3546 					nla[NFTA_SET_ELEM_LIST_SET_ID]);
3547 		}
3548 		if (IS_ERR(set))
3549 			return PTR_ERR(set);
3550 	}
3551 
3552 	if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3553 		return -EBUSY;
3554 
3555 	nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3556 		if (set->size &&
3557 		    !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))
3558 			return -ENFILE;
3559 
3560 		err = nft_add_set_elem(&ctx, set, attr);
3561 		if (err < 0) {
3562 			atomic_dec(&set->nelems);
3563 			break;
3564 		}
3565 	}
3566 	return err;
3567 }
3568 
nft_del_setelem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr)3569 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3570 			   const struct nlattr *attr)
3571 {
3572 	struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3573 	struct nft_data_desc desc;
3574 	struct nft_set_elem elem;
3575 	struct nft_trans *trans;
3576 	int err;
3577 
3578 	err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3579 			       nft_set_elem_policy);
3580 	if (err < 0)
3581 		goto err1;
3582 
3583 	err = -EINVAL;
3584 	if (nla[NFTA_SET_ELEM_KEY] == NULL)
3585 		goto err1;
3586 
3587 	err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
3588 			    nla[NFTA_SET_ELEM_KEY]);
3589 	if (err < 0)
3590 		goto err1;
3591 
3592 	err = -EINVAL;
3593 	if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3594 		goto err2;
3595 
3596 	trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3597 	if (trans == NULL) {
3598 		err = -ENOMEM;
3599 		goto err2;
3600 	}
3601 
3602 	elem.priv = set->ops->deactivate(set, &elem);
3603 	if (elem.priv == NULL) {
3604 		err = -ENOENT;
3605 		goto err3;
3606 	}
3607 
3608 	nft_trans_elem(trans) = elem;
3609 	list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3610 	return 0;
3611 
3612 err3:
3613 	kfree(trans);
3614 err2:
3615 	nft_data_uninit(&elem.key.val, desc.type);
3616 err1:
3617 	return err;
3618 }
3619 
nf_tables_delsetelem(struct net * net,struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])3620 static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
3621 				struct sk_buff *skb, const struct nlmsghdr *nlh,
3622 				const struct nlattr * const nla[])
3623 {
3624 	const struct nlattr *attr;
3625 	struct nft_set *set;
3626 	struct nft_ctx ctx;
3627 	int rem, err = 0;
3628 
3629 	if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3630 		return -EINVAL;
3631 
3632 	err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, false);
3633 	if (err < 0)
3634 		return err;
3635 
3636 	set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3637 	if (IS_ERR(set))
3638 		return PTR_ERR(set);
3639 	if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3640 		return -EBUSY;
3641 
3642 	nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3643 		err = nft_del_setelem(&ctx, set, attr);
3644 		if (err < 0)
3645 			break;
3646 
3647 		set->ndeact++;
3648 	}
3649 	return err;
3650 }
3651 
nft_set_gc_batch_release(struct rcu_head * rcu)3652 void nft_set_gc_batch_release(struct rcu_head *rcu)
3653 {
3654 	struct nft_set_gc_batch *gcb;
3655 	unsigned int i;
3656 
3657 	gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
3658 	for (i = 0; i < gcb->head.cnt; i++)
3659 		nft_set_elem_destroy(gcb->head.set, gcb->elems[i]);
3660 	kfree(gcb);
3661 }
3662 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
3663 
nft_set_gc_batch_alloc(const struct nft_set * set,gfp_t gfp)3664 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
3665 						gfp_t gfp)
3666 {
3667 	struct nft_set_gc_batch *gcb;
3668 
3669 	gcb = kzalloc(sizeof(*gcb), gfp);
3670 	if (gcb == NULL)
3671 		return gcb;
3672 	gcb->head.set = set;
3673 	return gcb;
3674 }
3675 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
3676 
nf_tables_fill_gen_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq)3677 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3678 				   u32 portid, u32 seq)
3679 {
3680 	struct nlmsghdr *nlh;
3681 	struct nfgenmsg *nfmsg;
3682 	int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3683 
3684 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3685 	if (nlh == NULL)
3686 		goto nla_put_failure;
3687 
3688 	nfmsg = nlmsg_data(nlh);
3689 	nfmsg->nfgen_family	= AF_UNSPEC;
3690 	nfmsg->version		= NFNETLINK_V0;
3691 	nfmsg->res_id		= htons(net->nft.base_seq & 0xffff);
3692 
3693 	if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3694 		goto nla_put_failure;
3695 
3696 	nlmsg_end(skb, nlh);
3697 	return 0;
3698 
3699 nla_put_failure:
3700 	nlmsg_trim(skb, nlh);
3701 	return -EMSGSIZE;
3702 }
3703 
nf_tables_gen_notify(struct net * net,struct sk_buff * skb,int event)3704 static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3705 {
3706 	struct nlmsghdr *nlh = nlmsg_hdr(skb);
3707 	struct sk_buff *skb2;
3708 	int err;
3709 
3710 	if (nlmsg_report(nlh) &&
3711 	    !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3712 		return 0;
3713 
3714 	err = -ENOBUFS;
3715 	skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3716 	if (skb2 == NULL)
3717 		goto err;
3718 
3719 	err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3720 				      nlh->nlmsg_seq);
3721 	if (err < 0) {
3722 		kfree_skb(skb2);
3723 		goto err;
3724 	}
3725 
3726 	err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3727 			     NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3728 err:
3729 	if (err < 0) {
3730 		nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3731 				  err);
3732 	}
3733 	return err;
3734 }
3735 
nf_tables_getgen(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const nla[])3736 static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3737 			    const struct nlmsghdr *nlh,
3738 			    const struct nlattr * const nla[])
3739 {
3740 	struct net *net = sock_net(skb->sk);
3741 	struct sk_buff *skb2;
3742 	int err;
3743 
3744 	skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3745 	if (skb2 == NULL)
3746 		return -ENOMEM;
3747 
3748 	err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3749 				      nlh->nlmsg_seq);
3750 	if (err < 0)
3751 		goto err;
3752 
3753 	return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3754 err:
3755 	kfree_skb(skb2);
3756 	return err;
3757 }
3758 
3759 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3760 	[NFT_MSG_NEWTABLE] = {
3761 		.call_batch	= nf_tables_newtable,
3762 		.attr_count	= NFTA_TABLE_MAX,
3763 		.policy		= nft_table_policy,
3764 	},
3765 	[NFT_MSG_GETTABLE] = {
3766 		.call		= nf_tables_gettable,
3767 		.attr_count	= NFTA_TABLE_MAX,
3768 		.policy		= nft_table_policy,
3769 	},
3770 	[NFT_MSG_DELTABLE] = {
3771 		.call_batch	= nf_tables_deltable,
3772 		.attr_count	= NFTA_TABLE_MAX,
3773 		.policy		= nft_table_policy,
3774 	},
3775 	[NFT_MSG_NEWCHAIN] = {
3776 		.call_batch	= nf_tables_newchain,
3777 		.attr_count	= NFTA_CHAIN_MAX,
3778 		.policy		= nft_chain_policy,
3779 	},
3780 	[NFT_MSG_GETCHAIN] = {
3781 		.call		= nf_tables_getchain,
3782 		.attr_count	= NFTA_CHAIN_MAX,
3783 		.policy		= nft_chain_policy,
3784 	},
3785 	[NFT_MSG_DELCHAIN] = {
3786 		.call_batch	= nf_tables_delchain,
3787 		.attr_count	= NFTA_CHAIN_MAX,
3788 		.policy		= nft_chain_policy,
3789 	},
3790 	[NFT_MSG_NEWRULE] = {
3791 		.call_batch	= nf_tables_newrule,
3792 		.attr_count	= NFTA_RULE_MAX,
3793 		.policy		= nft_rule_policy,
3794 	},
3795 	[NFT_MSG_GETRULE] = {
3796 		.call		= nf_tables_getrule,
3797 		.attr_count	= NFTA_RULE_MAX,
3798 		.policy		= nft_rule_policy,
3799 	},
3800 	[NFT_MSG_DELRULE] = {
3801 		.call_batch	= nf_tables_delrule,
3802 		.attr_count	= NFTA_RULE_MAX,
3803 		.policy		= nft_rule_policy,
3804 	},
3805 	[NFT_MSG_NEWSET] = {
3806 		.call_batch	= nf_tables_newset,
3807 		.attr_count	= NFTA_SET_MAX,
3808 		.policy		= nft_set_policy,
3809 	},
3810 	[NFT_MSG_GETSET] = {
3811 		.call		= nf_tables_getset,
3812 		.attr_count	= NFTA_SET_MAX,
3813 		.policy		= nft_set_policy,
3814 	},
3815 	[NFT_MSG_DELSET] = {
3816 		.call_batch	= nf_tables_delset,
3817 		.attr_count	= NFTA_SET_MAX,
3818 		.policy		= nft_set_policy,
3819 	},
3820 	[NFT_MSG_NEWSETELEM] = {
3821 		.call_batch	= nf_tables_newsetelem,
3822 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
3823 		.policy		= nft_set_elem_list_policy,
3824 	},
3825 	[NFT_MSG_GETSETELEM] = {
3826 		.call		= nf_tables_getsetelem,
3827 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
3828 		.policy		= nft_set_elem_list_policy,
3829 	},
3830 	[NFT_MSG_DELSETELEM] = {
3831 		.call_batch	= nf_tables_delsetelem,
3832 		.attr_count	= NFTA_SET_ELEM_LIST_MAX,
3833 		.policy		= nft_set_elem_list_policy,
3834 	},
3835 	[NFT_MSG_GETGEN] = {
3836 		.call		= nf_tables_getgen,
3837 	},
3838 };
3839 
nft_chain_commit_update(struct nft_trans * trans)3840 static void nft_chain_commit_update(struct nft_trans *trans)
3841 {
3842 	struct nft_base_chain *basechain;
3843 
3844 	if (nft_trans_chain_name(trans)[0])
3845 		strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3846 
3847 	if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3848 		return;
3849 
3850 	basechain = nft_base_chain(trans->ctx.chain);
3851 	nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3852 
3853 	switch (nft_trans_chain_policy(trans)) {
3854 	case NF_DROP:
3855 	case NF_ACCEPT:
3856 		basechain->policy = nft_trans_chain_policy(trans);
3857 		break;
3858 	}
3859 }
3860 
nf_tables_commit_release(struct nft_trans * trans)3861 static void nf_tables_commit_release(struct nft_trans *trans)
3862 {
3863 	switch (trans->msg_type) {
3864 	case NFT_MSG_DELTABLE:
3865 		nf_tables_table_destroy(&trans->ctx);
3866 		break;
3867 	case NFT_MSG_DELCHAIN:
3868 		nf_tables_chain_destroy(trans->ctx.chain);
3869 		break;
3870 	case NFT_MSG_DELRULE:
3871 		nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3872 		break;
3873 	case NFT_MSG_DELSET:
3874 		nft_set_destroy(nft_trans_set(trans));
3875 		break;
3876 	case NFT_MSG_DELSETELEM:
3877 		nft_set_elem_destroy(nft_trans_elem_set(trans),
3878 				     nft_trans_elem(trans).priv);
3879 		break;
3880 	}
3881 	kfree(trans);
3882 }
3883 
nf_tables_commit(struct sk_buff * skb)3884 static int nf_tables_commit(struct sk_buff *skb)
3885 {
3886 	struct net *net = sock_net(skb->sk);
3887 	struct nft_trans *trans, *next;
3888 	struct nft_trans_elem *te;
3889 
3890 	/* Bump generation counter, invalidate any dump in progress */
3891 	while (++net->nft.base_seq == 0);
3892 
3893 	/* A new generation has just started */
3894 	net->nft.gencursor = nft_gencursor_next(net);
3895 
3896 	/* Make sure all packets have left the previous generation before
3897 	 * purging old rules.
3898 	 */
3899 	synchronize_rcu();
3900 
3901 	list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3902 		switch (trans->msg_type) {
3903 		case NFT_MSG_NEWTABLE:
3904 			if (nft_trans_table_update(trans)) {
3905 				if (!nft_trans_table_enable(trans)) {
3906 					nf_tables_table_disable(trans->ctx.afi,
3907 								trans->ctx.table);
3908 					trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3909 				}
3910 			} else {
3911 				trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3912 			}
3913 			nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3914 			nft_trans_destroy(trans);
3915 			break;
3916 		case NFT_MSG_DELTABLE:
3917 			nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3918 			break;
3919 		case NFT_MSG_NEWCHAIN:
3920 			if (nft_trans_chain_update(trans))
3921 				nft_chain_commit_update(trans);
3922 			else
3923 				trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3924 
3925 			nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3926 			nft_trans_destroy(trans);
3927 			break;
3928 		case NFT_MSG_DELCHAIN:
3929 			nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3930 			nf_tables_unregister_hooks(trans->ctx.table,
3931 						   trans->ctx.chain,
3932 						   trans->ctx.afi->nops);
3933 			break;
3934 		case NFT_MSG_NEWRULE:
3935 			nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3936 			nf_tables_rule_notify(&trans->ctx,
3937 					      nft_trans_rule(trans),
3938 					      NFT_MSG_NEWRULE);
3939 			nft_trans_destroy(trans);
3940 			break;
3941 		case NFT_MSG_DELRULE:
3942 			list_del_rcu(&nft_trans_rule(trans)->list);
3943 			nf_tables_rule_notify(&trans->ctx,
3944 					      nft_trans_rule(trans),
3945 					      NFT_MSG_DELRULE);
3946 			break;
3947 		case NFT_MSG_NEWSET:
3948 			nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3949 			/* This avoids hitting -EBUSY when deleting the table
3950 			 * from the transaction.
3951 			 */
3952 			if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3953 			    !list_empty(&nft_trans_set(trans)->bindings))
3954 				trans->ctx.table->use--;
3955 
3956 			nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3957 					     NFT_MSG_NEWSET, GFP_KERNEL);
3958 			nft_trans_destroy(trans);
3959 			break;
3960 		case NFT_MSG_DELSET:
3961 			nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3962 					     NFT_MSG_DELSET, GFP_KERNEL);
3963 			break;
3964 		case NFT_MSG_NEWSETELEM:
3965 			te = (struct nft_trans_elem *)trans->data;
3966 
3967 			te->set->ops->activate(te->set, &te->elem);
3968 			nf_tables_setelem_notify(&trans->ctx, te->set,
3969 						 &te->elem,
3970 						 NFT_MSG_NEWSETELEM, 0);
3971 			nft_trans_destroy(trans);
3972 			break;
3973 		case NFT_MSG_DELSETELEM:
3974 			te = (struct nft_trans_elem *)trans->data;
3975 
3976 			nf_tables_setelem_notify(&trans->ctx, te->set,
3977 						 &te->elem,
3978 						 NFT_MSG_DELSETELEM, 0);
3979 			te->set->ops->remove(te->set, &te->elem);
3980 			atomic_dec(&te->set->nelems);
3981 			te->set->ndeact--;
3982 			break;
3983 		}
3984 	}
3985 
3986 	synchronize_rcu();
3987 
3988 	list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3989 		list_del(&trans->list);
3990 		nf_tables_commit_release(trans);
3991 	}
3992 
3993 	nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3994 
3995 	return 0;
3996 }
3997 
nf_tables_abort_release(struct nft_trans * trans)3998 static void nf_tables_abort_release(struct nft_trans *trans)
3999 {
4000 	switch (trans->msg_type) {
4001 	case NFT_MSG_NEWTABLE:
4002 		nf_tables_table_destroy(&trans->ctx);
4003 		break;
4004 	case NFT_MSG_NEWCHAIN:
4005 		nf_tables_chain_destroy(trans->ctx.chain);
4006 		break;
4007 	case NFT_MSG_NEWRULE:
4008 		nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
4009 		break;
4010 	case NFT_MSG_NEWSET:
4011 		nft_set_destroy(nft_trans_set(trans));
4012 		break;
4013 	case NFT_MSG_NEWSETELEM:
4014 		nft_set_elem_destroy(nft_trans_elem_set(trans),
4015 				     nft_trans_elem(trans).priv);
4016 		break;
4017 	}
4018 	kfree(trans);
4019 }
4020 
nf_tables_abort(struct sk_buff * skb)4021 static int nf_tables_abort(struct sk_buff *skb)
4022 {
4023 	struct net *net = sock_net(skb->sk);
4024 	struct nft_trans *trans, *next;
4025 	struct nft_trans_elem *te;
4026 
4027 	list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
4028 					 list) {
4029 		switch (trans->msg_type) {
4030 		case NFT_MSG_NEWTABLE:
4031 			if (nft_trans_table_update(trans)) {
4032 				if (nft_trans_table_enable(trans)) {
4033 					nf_tables_table_disable(trans->ctx.afi,
4034 								trans->ctx.table);
4035 					trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
4036 				}
4037 				nft_trans_destroy(trans);
4038 			} else {
4039 				list_del_rcu(&trans->ctx.table->list);
4040 			}
4041 			break;
4042 		case NFT_MSG_DELTABLE:
4043 			list_add_tail_rcu(&trans->ctx.table->list,
4044 					  &trans->ctx.afi->tables);
4045 			nft_trans_destroy(trans);
4046 			break;
4047 		case NFT_MSG_NEWCHAIN:
4048 			if (nft_trans_chain_update(trans)) {
4049 				free_percpu(nft_trans_chain_stats(trans));
4050 
4051 				nft_trans_destroy(trans);
4052 			} else {
4053 				trans->ctx.table->use--;
4054 				list_del_rcu(&trans->ctx.chain->list);
4055 				nf_tables_unregister_hooks(trans->ctx.table,
4056 							   trans->ctx.chain,
4057 							   trans->ctx.afi->nops);
4058 			}
4059 			break;
4060 		case NFT_MSG_DELCHAIN:
4061 			trans->ctx.table->use++;
4062 			list_add_tail_rcu(&trans->ctx.chain->list,
4063 					  &trans->ctx.table->chains);
4064 			nft_trans_destroy(trans);
4065 			break;
4066 		case NFT_MSG_NEWRULE:
4067 			trans->ctx.chain->use--;
4068 			list_del_rcu(&nft_trans_rule(trans)->list);
4069 			break;
4070 		case NFT_MSG_DELRULE:
4071 			trans->ctx.chain->use++;
4072 			nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
4073 			nft_trans_destroy(trans);
4074 			break;
4075 		case NFT_MSG_NEWSET:
4076 			trans->ctx.table->use--;
4077 			list_del_rcu(&nft_trans_set(trans)->list);
4078 			break;
4079 		case NFT_MSG_DELSET:
4080 			trans->ctx.table->use++;
4081 			list_add_tail_rcu(&nft_trans_set(trans)->list,
4082 					  &trans->ctx.table->sets);
4083 			nft_trans_destroy(trans);
4084 			break;
4085 		case NFT_MSG_NEWSETELEM:
4086 			te = (struct nft_trans_elem *)trans->data;
4087 
4088 			te->set->ops->remove(te->set, &te->elem);
4089 			atomic_dec(&te->set->nelems);
4090 			break;
4091 		case NFT_MSG_DELSETELEM:
4092 			te = (struct nft_trans_elem *)trans->data;
4093 
4094 			te->set->ops->activate(te->set, &te->elem);
4095 			te->set->ndeact--;
4096 
4097 			nft_trans_destroy(trans);
4098 			break;
4099 		}
4100 	}
4101 
4102 	synchronize_rcu();
4103 
4104 	list_for_each_entry_safe_reverse(trans, next,
4105 					 &net->nft.commit_list, list) {
4106 		list_del(&trans->list);
4107 		nf_tables_abort_release(trans);
4108 	}
4109 
4110 	return 0;
4111 }
4112 
4113 static const struct nfnetlink_subsystem nf_tables_subsys = {
4114 	.name		= "nf_tables",
4115 	.subsys_id	= NFNL_SUBSYS_NFTABLES,
4116 	.cb_count	= NFT_MSG_MAX,
4117 	.cb		= nf_tables_cb,
4118 	.commit		= nf_tables_commit,
4119 	.abort		= nf_tables_abort,
4120 };
4121 
nft_chain_validate_dependency(const struct nft_chain * chain,enum nft_chain_type type)4122 int nft_chain_validate_dependency(const struct nft_chain *chain,
4123 				  enum nft_chain_type type)
4124 {
4125 	const struct nft_base_chain *basechain;
4126 
4127 	if (chain->flags & NFT_BASE_CHAIN) {
4128 		basechain = nft_base_chain(chain);
4129 		if (basechain->type->type != type)
4130 			return -EOPNOTSUPP;
4131 	}
4132 	return 0;
4133 }
4134 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
4135 
nft_chain_validate_hooks(const struct nft_chain * chain,unsigned int hook_flags)4136 int nft_chain_validate_hooks(const struct nft_chain *chain,
4137 			     unsigned int hook_flags)
4138 {
4139 	struct nft_base_chain *basechain;
4140 
4141 	if (chain->flags & NFT_BASE_CHAIN) {
4142 		basechain = nft_base_chain(chain);
4143 
4144 		if ((1 << basechain->ops[0].hooknum) & hook_flags)
4145 			return 0;
4146 
4147 		return -EOPNOTSUPP;
4148 	}
4149 
4150 	return 0;
4151 }
4152 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
4153 
4154 /*
4155  * Loop detection - walk through the ruleset beginning at the destination chain
4156  * of a new jump until either the source chain is reached (loop) or all
4157  * reachable chains have been traversed.
4158  *
4159  * The loop check is performed whenever a new jump verdict is added to an
4160  * expression or verdict map or a verdict map is bound to a new chain.
4161  */
4162 
4163 static int nf_tables_check_loops(const struct nft_ctx *ctx,
4164 				 const struct nft_chain *chain);
4165 
nf_tables_loop_check_setelem(const struct nft_ctx * ctx,const struct nft_set * set,const struct nft_set_iter * iter,const struct nft_set_elem * elem)4166 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
4167 					const struct nft_set *set,
4168 					const struct nft_set_iter *iter,
4169 					const struct nft_set_elem *elem)
4170 {
4171 	const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4172 	const struct nft_data *data;
4173 
4174 	if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4175 	    *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
4176 		return 0;
4177 
4178 	data = nft_set_ext_data(ext);
4179 	switch (data->verdict.code) {
4180 	case NFT_JUMP:
4181 	case NFT_GOTO:
4182 		return nf_tables_check_loops(ctx, data->verdict.chain);
4183 	default:
4184 		return 0;
4185 	}
4186 }
4187 
nf_tables_check_loops(const struct nft_ctx * ctx,const struct nft_chain * chain)4188 static int nf_tables_check_loops(const struct nft_ctx *ctx,
4189 				 const struct nft_chain *chain)
4190 {
4191 	const struct nft_rule *rule;
4192 	const struct nft_expr *expr, *last;
4193 	const struct nft_set *set;
4194 	struct nft_set_binding *binding;
4195 	struct nft_set_iter iter;
4196 
4197 	if (ctx->chain == chain)
4198 		return -ELOOP;
4199 
4200 	list_for_each_entry(rule, &chain->rules, list) {
4201 		nft_rule_for_each_expr(expr, last, rule) {
4202 			const struct nft_data *data = NULL;
4203 			int err;
4204 
4205 			if (!expr->ops->validate)
4206 				continue;
4207 
4208 			err = expr->ops->validate(ctx, expr, &data);
4209 			if (err < 0)
4210 				return err;
4211 
4212 			if (data == NULL)
4213 				continue;
4214 
4215 			switch (data->verdict.code) {
4216 			case NFT_JUMP:
4217 			case NFT_GOTO:
4218 				err = nf_tables_check_loops(ctx,
4219 							data->verdict.chain);
4220 				if (err < 0)
4221 					return err;
4222 			default:
4223 				break;
4224 			}
4225 		}
4226 	}
4227 
4228 	list_for_each_entry(set, &ctx->table->sets, list) {
4229 		if (!(set->flags & NFT_SET_MAP) ||
4230 		    set->dtype != NFT_DATA_VERDICT)
4231 			continue;
4232 
4233 		list_for_each_entry(binding, &set->bindings, list) {
4234 			if (!(binding->flags & NFT_SET_MAP) ||
4235 			    binding->chain != chain)
4236 				continue;
4237 
4238 			iter.skip 	= 0;
4239 			iter.count	= 0;
4240 			iter.err	= 0;
4241 			iter.fn		= nf_tables_loop_check_setelem;
4242 
4243 			set->ops->walk(ctx, set, &iter);
4244 			if (iter.err < 0)
4245 				return iter.err;
4246 		}
4247 	}
4248 
4249 	return 0;
4250 }
4251 
4252 /**
4253  *	nft_parse_register - parse a register value from a netlink attribute
4254  *
4255  *	@attr: netlink attribute
4256  *
4257  *	Parse and translate a register value from a netlink attribute.
4258  *	Registers used to be 128 bit wide, these register numbers will be
4259  *	mapped to the corresponding 32 bit register numbers.
4260  */
nft_parse_register(const struct nlattr * attr)4261 unsigned int nft_parse_register(const struct nlattr *attr)
4262 {
4263 	unsigned int reg;
4264 
4265 	reg = ntohl(nla_get_be32(attr));
4266 	switch (reg) {
4267 	case NFT_REG_VERDICT...NFT_REG_4:
4268 		return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
4269 	default:
4270 		return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
4271 	}
4272 }
4273 EXPORT_SYMBOL_GPL(nft_parse_register);
4274 
4275 /**
4276  *	nft_dump_register - dump a register value to a netlink attribute
4277  *
4278  *	@skb: socket buffer
4279  *	@attr: attribute number
4280  *	@reg: register number
4281  *
4282  *	Construct a netlink attribute containing the register number. For
4283  *	compatibility reasons, register numbers being a multiple of 4 are
4284  *	translated to the corresponding 128 bit register numbers.
4285  */
nft_dump_register(struct sk_buff * skb,unsigned int attr,unsigned int reg)4286 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
4287 {
4288 	if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
4289 		reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
4290 	else
4291 		reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
4292 
4293 	return nla_put_be32(skb, attr, htonl(reg));
4294 }
4295 EXPORT_SYMBOL_GPL(nft_dump_register);
4296 
4297 /**
4298  *	nft_validate_register_load - validate a load from a register
4299  *
4300  *	@reg: the register number
4301  *	@len: the length of the data
4302  *
4303  * 	Validate that the input register is one of the general purpose
4304  * 	registers and that the length of the load is within the bounds.
4305  */
nft_validate_register_load(enum nft_registers reg,unsigned int len)4306 int nft_validate_register_load(enum nft_registers reg, unsigned int len)
4307 {
4308 	if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
4309 		return -EINVAL;
4310 	if (len == 0)
4311 		return -EINVAL;
4312 	if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
4313 		return -ERANGE;
4314 
4315 	return 0;
4316 }
4317 EXPORT_SYMBOL_GPL(nft_validate_register_load);
4318 
4319 /**
4320  *	nft_validate_register_store - validate an expressions' register store
4321  *
4322  *	@ctx: context of the expression performing the load
4323  * 	@reg: the destination register number
4324  * 	@data: the data to load
4325  * 	@type: the data type
4326  * 	@len: the length of the data
4327  *
4328  * 	Validate that a data load uses the appropriate data type for
4329  * 	the destination register and the length is within the bounds.
4330  * 	A value of NULL for the data means that its runtime gathered
4331  * 	data.
4332  */
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)4333 int nft_validate_register_store(const struct nft_ctx *ctx,
4334 				enum nft_registers reg,
4335 				const struct nft_data *data,
4336 				enum nft_data_types type, unsigned int len)
4337 {
4338 	int err;
4339 
4340 	switch (reg) {
4341 	case NFT_REG_VERDICT:
4342 		if (type != NFT_DATA_VERDICT)
4343 			return -EINVAL;
4344 
4345 		if (data != NULL &&
4346 		    (data->verdict.code == NFT_GOTO ||
4347 		     data->verdict.code == NFT_JUMP)) {
4348 			err = nf_tables_check_loops(ctx, data->verdict.chain);
4349 			if (err < 0)
4350 				return err;
4351 
4352 			if (ctx->chain->level + 1 >
4353 			    data->verdict.chain->level) {
4354 				if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
4355 					return -EMLINK;
4356 				data->verdict.chain->level = ctx->chain->level + 1;
4357 			}
4358 		}
4359 
4360 		return 0;
4361 	default:
4362 		if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
4363 			return -EINVAL;
4364 		if (len == 0)
4365 			return -EINVAL;
4366 		if (reg * NFT_REG32_SIZE + len >
4367 		    FIELD_SIZEOF(struct nft_regs, data))
4368 			return -ERANGE;
4369 
4370 		if (data != NULL && type != NFT_DATA_VALUE)
4371 			return -EINVAL;
4372 		return 0;
4373 	}
4374 }
4375 EXPORT_SYMBOL_GPL(nft_validate_register_store);
4376 
4377 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
4378 	[NFTA_VERDICT_CODE]	= { .type = NLA_U32 },
4379 	[NFTA_VERDICT_CHAIN]	= { .type = NLA_STRING,
4380 				    .len = NFT_CHAIN_MAXNAMELEN - 1 },
4381 };
4382 
nft_verdict_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)4383 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4384 			    struct nft_data_desc *desc, const struct nlattr *nla)
4385 {
4386 	struct nlattr *tb[NFTA_VERDICT_MAX + 1];
4387 	struct nft_chain *chain;
4388 	int err;
4389 
4390 	err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
4391 	if (err < 0)
4392 		return err;
4393 
4394 	if (!tb[NFTA_VERDICT_CODE])
4395 		return -EINVAL;
4396 	data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
4397 
4398 	switch (data->verdict.code) {
4399 	default:
4400 		switch (data->verdict.code & NF_VERDICT_MASK) {
4401 		case NF_ACCEPT:
4402 		case NF_DROP:
4403 		case NF_QUEUE:
4404 			break;
4405 		default:
4406 			return -EINVAL;
4407 		}
4408 		/* fall through */
4409 	case NFT_CONTINUE:
4410 	case NFT_BREAK:
4411 	case NFT_RETURN:
4412 		break;
4413 	case NFT_JUMP:
4414 	case NFT_GOTO:
4415 		if (!tb[NFTA_VERDICT_CHAIN])
4416 			return -EINVAL;
4417 		chain = nf_tables_chain_lookup(ctx->table,
4418 					       tb[NFTA_VERDICT_CHAIN]);
4419 		if (IS_ERR(chain))
4420 			return PTR_ERR(chain);
4421 		if (chain->flags & NFT_BASE_CHAIN)
4422 			return -EOPNOTSUPP;
4423 
4424 		chain->use++;
4425 		data->verdict.chain = chain;
4426 		break;
4427 	}
4428 
4429 	desc->len = sizeof(data->verdict);
4430 	desc->type = NFT_DATA_VERDICT;
4431 	return 0;
4432 }
4433 
nft_verdict_uninit(const struct nft_data * data)4434 static void nft_verdict_uninit(const struct nft_data *data)
4435 {
4436 	switch (data->verdict.code) {
4437 	case NFT_JUMP:
4438 	case NFT_GOTO:
4439 		data->verdict.chain->use--;
4440 		break;
4441 	}
4442 }
4443 
nft_verdict_dump(struct sk_buff * skb,const struct nft_data * data)4444 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4445 {
4446 	struct nlattr *nest;
4447 
4448 	nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4449 	if (!nest)
4450 		goto nla_put_failure;
4451 
4452 	if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict.code)))
4453 		goto nla_put_failure;
4454 
4455 	switch (data->verdict.code) {
4456 	case NFT_JUMP:
4457 	case NFT_GOTO:
4458 		if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
4459 				   data->verdict.chain->name))
4460 			goto nla_put_failure;
4461 	}
4462 	nla_nest_end(skb, nest);
4463 	return 0;
4464 
4465 nla_put_failure:
4466 	return -1;
4467 }
4468 
nft_value_init(const struct nft_ctx * ctx,struct nft_data * data,unsigned int size,struct nft_data_desc * desc,const struct nlattr * nla)4469 static int nft_value_init(const struct nft_ctx *ctx,
4470 			  struct nft_data *data, unsigned int size,
4471 			  struct nft_data_desc *desc, const struct nlattr *nla)
4472 {
4473 	unsigned int len;
4474 
4475 	len = nla_len(nla);
4476 	if (len == 0)
4477 		return -EINVAL;
4478 	if (len > size)
4479 		return -EOVERFLOW;
4480 
4481 	nla_memcpy(data->data, nla, len);
4482 	desc->type = NFT_DATA_VALUE;
4483 	desc->len  = len;
4484 	return 0;
4485 }
4486 
nft_value_dump(struct sk_buff * skb,const struct nft_data * data,unsigned int len)4487 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4488 			  unsigned int len)
4489 {
4490 	return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4491 }
4492 
4493 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4494 	[NFTA_DATA_VALUE]	= { .type = NLA_BINARY },
4495 	[NFTA_DATA_VERDICT]	= { .type = NLA_NESTED },
4496 };
4497 
4498 /**
4499  *	nft_data_init - parse nf_tables data netlink attributes
4500  *
4501  *	@ctx: context of the expression using the data
4502  *	@data: destination struct nft_data
4503  *	@size: maximum data length
4504  *	@desc: data description
4505  *	@nla: netlink attribute containing data
4506  *
4507  *	Parse the netlink data attributes and initialize a struct nft_data.
4508  *	The type and length of data are returned in the data description.
4509  *
4510  *	The caller can indicate that it only wants to accept data of type
4511  *	NFT_DATA_VALUE by passing NULL for the ctx argument.
4512  */
nft_data_init(const struct nft_ctx * ctx,struct nft_data * data,unsigned int size,struct nft_data_desc * desc,const struct nlattr * nla)4513 int nft_data_init(const struct nft_ctx *ctx,
4514 		  struct nft_data *data, unsigned int size,
4515 		  struct nft_data_desc *desc, const struct nlattr *nla)
4516 {
4517 	struct nlattr *tb[NFTA_DATA_MAX + 1];
4518 	int err;
4519 
4520 	err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4521 	if (err < 0)
4522 		return err;
4523 
4524 	if (tb[NFTA_DATA_VALUE])
4525 		return nft_value_init(ctx, data, size, desc,
4526 				      tb[NFTA_DATA_VALUE]);
4527 	if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4528 		return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4529 	return -EINVAL;
4530 }
4531 EXPORT_SYMBOL_GPL(nft_data_init);
4532 
4533 /**
4534  *	nft_data_uninit - release a nft_data item
4535  *
4536  *	@data: struct nft_data to release
4537  *	@type: type of data
4538  *
4539  *	Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4540  *	all others need to be released by calling this function.
4541  */
nft_data_uninit(const struct nft_data * data,enum nft_data_types type)4542 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4543 {
4544 	if (type < NFT_DATA_VERDICT)
4545 		return;
4546 	switch (type) {
4547 	case NFT_DATA_VERDICT:
4548 		return nft_verdict_uninit(data);
4549 	default:
4550 		WARN_ON(1);
4551 	}
4552 }
4553 EXPORT_SYMBOL_GPL(nft_data_uninit);
4554 
nft_data_dump(struct sk_buff * skb,int attr,const struct nft_data * data,enum nft_data_types type,unsigned int len)4555 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4556 		  enum nft_data_types type, unsigned int len)
4557 {
4558 	struct nlattr *nest;
4559 	int err;
4560 
4561 	nest = nla_nest_start(skb, attr);
4562 	if (nest == NULL)
4563 		return -1;
4564 
4565 	switch (type) {
4566 	case NFT_DATA_VALUE:
4567 		err = nft_value_dump(skb, data, len);
4568 		break;
4569 	case NFT_DATA_VERDICT:
4570 		err = nft_verdict_dump(skb, data);
4571 		break;
4572 	default:
4573 		err = -EINVAL;
4574 		WARN_ON(1);
4575 	}
4576 
4577 	nla_nest_end(skb, nest);
4578 	return err;
4579 }
4580 EXPORT_SYMBOL_GPL(nft_data_dump);
4581 
nf_tables_init_net(struct net * net)4582 static int nf_tables_init_net(struct net *net)
4583 {
4584 	INIT_LIST_HEAD(&net->nft.af_info);
4585 	INIT_LIST_HEAD(&net->nft.commit_list);
4586 	net->nft.base_seq = 1;
4587 	return 0;
4588 }
4589 
4590 static struct pernet_operations nf_tables_net_ops = {
4591 	.init	= nf_tables_init_net,
4592 };
4593 
nf_tables_module_init(void)4594 static int __init nf_tables_module_init(void)
4595 {
4596 	int err;
4597 
4598 	info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4599 		       GFP_KERNEL);
4600 	if (info == NULL) {
4601 		err = -ENOMEM;
4602 		goto err1;
4603 	}
4604 
4605 	err = nf_tables_core_module_init();
4606 	if (err < 0)
4607 		goto err2;
4608 
4609 	err = nfnetlink_subsys_register(&nf_tables_subsys);
4610 	if (err < 0)
4611 		goto err3;
4612 
4613 	pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4614 	return register_pernet_subsys(&nf_tables_net_ops);
4615 err3:
4616 	nf_tables_core_module_exit();
4617 err2:
4618 	kfree(info);
4619 err1:
4620 	return err;
4621 }
4622 
nf_tables_module_exit(void)4623 static void __exit nf_tables_module_exit(void)
4624 {
4625 	unregister_pernet_subsys(&nf_tables_net_ops);
4626 	nfnetlink_subsys_unregister(&nf_tables_subsys);
4627 	rcu_barrier();
4628 	nf_tables_core_module_exit();
4629 	kfree(info);
4630 }
4631 
4632 module_init(nf_tables_module_init);
4633 module_exit(nf_tables_module_exit);
4634 
4635 MODULE_LICENSE("GPL");
4636 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4637 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
4638