]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - net/netfilter/nf_tables_api.c
netfilter: nf_tables: force module load in case select_ops() returns -EAGAIN
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nf_tables_api.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
4 *
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
6 */
7
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/skbuff.h>
12 #include <linux/netlink.h>
13 #include <linux/vmalloc.h>
14 #include <linux/rhashtable.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nfnetlink.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_flow_table.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 static LIST_HEAD(nf_tables_objects);
26 static LIST_HEAD(nf_tables_flowtables);
27 static LIST_HEAD(nf_tables_destroy_list);
28 static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
29 static u64 table_handle;
30
31 enum {
32 NFT_VALIDATE_SKIP = 0,
33 NFT_VALIDATE_NEED,
34 NFT_VALIDATE_DO,
35 };
36
37 static struct rhltable nft_objname_ht;
38
39 static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
40 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
41 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
42
43 static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
44 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
45 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
46
47 static const struct rhashtable_params nft_chain_ht_params = {
48 .head_offset = offsetof(struct nft_chain, rhlhead),
49 .key_offset = offsetof(struct nft_chain, name),
50 .hashfn = nft_chain_hash,
51 .obj_hashfn = nft_chain_hash_obj,
52 .obj_cmpfn = nft_chain_hash_cmp,
53 .automatic_shrinking = true,
54 };
55
56 static const struct rhashtable_params nft_objname_ht_params = {
57 .head_offset = offsetof(struct nft_object, rhlhead),
58 .key_offset = offsetof(struct nft_object, key),
59 .hashfn = nft_objname_hash,
60 .obj_hashfn = nft_objname_hash_obj,
61 .obj_cmpfn = nft_objname_hash_cmp,
62 .automatic_shrinking = true,
63 };
64
65 static void nft_validate_state_update(struct net *net, u8 new_validate_state)
66 {
67 switch (net->nft.validate_state) {
68 case NFT_VALIDATE_SKIP:
69 WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
70 break;
71 case NFT_VALIDATE_NEED:
72 break;
73 case NFT_VALIDATE_DO:
74 if (new_validate_state == NFT_VALIDATE_NEED)
75 return;
76 }
77
78 net->nft.validate_state = new_validate_state;
79 }
80 static void nf_tables_trans_destroy_work(struct work_struct *w);
81 static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
82
83 static void nft_ctx_init(struct nft_ctx *ctx,
84 struct net *net,
85 const struct sk_buff *skb,
86 const struct nlmsghdr *nlh,
87 u8 family,
88 struct nft_table *table,
89 struct nft_chain *chain,
90 const struct nlattr * const *nla)
91 {
92 ctx->net = net;
93 ctx->family = family;
94 ctx->level = 0;
95 ctx->table = table;
96 ctx->chain = chain;
97 ctx->nla = nla;
98 ctx->portid = NETLINK_CB(skb).portid;
99 ctx->report = nlmsg_report(nlh);
100 ctx->seq = nlh->nlmsg_seq;
101 }
102
103 static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
104 int msg_type, u32 size, gfp_t gfp)
105 {
106 struct nft_trans *trans;
107
108 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
109 if (trans == NULL)
110 return NULL;
111
112 trans->msg_type = msg_type;
113 trans->ctx = *ctx;
114
115 return trans;
116 }
117
118 static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
119 int msg_type, u32 size)
120 {
121 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
122 }
123
124 static void nft_trans_destroy(struct nft_trans *trans)
125 {
126 list_del(&trans->list);
127 kfree(trans);
128 }
129
130 static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
131 {
132 struct net *net = ctx->net;
133 struct nft_trans *trans;
134
135 if (!nft_set_is_anonymous(set))
136 return;
137
138 list_for_each_entry_reverse(trans, &net->nft.commit_list, list) {
139 if (trans->msg_type == NFT_MSG_NEWSET &&
140 nft_trans_set(trans) == set) {
141 set->bound = true;
142 break;
143 }
144 }
145 }
146
147 static int nf_tables_register_hook(struct net *net,
148 const struct nft_table *table,
149 struct nft_chain *chain)
150 {
151 const struct nft_base_chain *basechain;
152 const struct nf_hook_ops *ops;
153
154 if (table->flags & NFT_TABLE_F_DORMANT ||
155 !nft_is_base_chain(chain))
156 return 0;
157
158 basechain = nft_base_chain(chain);
159 ops = &basechain->ops;
160
161 if (basechain->type->ops_register)
162 return basechain->type->ops_register(net, ops);
163
164 return nf_register_net_hook(net, ops);
165 }
166
167 static void nf_tables_unregister_hook(struct net *net,
168 const struct nft_table *table,
169 struct nft_chain *chain)
170 {
171 const struct nft_base_chain *basechain;
172 const struct nf_hook_ops *ops;
173
174 if (table->flags & NFT_TABLE_F_DORMANT ||
175 !nft_is_base_chain(chain))
176 return;
177 basechain = nft_base_chain(chain);
178 ops = &basechain->ops;
179
180 if (basechain->type->ops_unregister)
181 return basechain->type->ops_unregister(net, ops);
182
183 nf_unregister_net_hook(net, ops);
184 }
185
186 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
187 {
188 struct nft_trans *trans;
189
190 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
191 if (trans == NULL)
192 return -ENOMEM;
193
194 if (msg_type == NFT_MSG_NEWTABLE)
195 nft_activate_next(ctx->net, ctx->table);
196
197 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
198 return 0;
199 }
200
201 static int nft_deltable(struct nft_ctx *ctx)
202 {
203 int err;
204
205 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
206 if (err < 0)
207 return err;
208
209 nft_deactivate_next(ctx->net, ctx->table);
210 return err;
211 }
212
213 static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
214 {
215 struct nft_trans *trans;
216
217 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
218 if (trans == NULL)
219 return ERR_PTR(-ENOMEM);
220
221 if (msg_type == NFT_MSG_NEWCHAIN)
222 nft_activate_next(ctx->net, ctx->chain);
223
224 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
225 return trans;
226 }
227
228 static int nft_delchain(struct nft_ctx *ctx)
229 {
230 struct nft_trans *trans;
231
232 trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
233 if (IS_ERR(trans))
234 return PTR_ERR(trans);
235
236 ctx->table->use--;
237 nft_deactivate_next(ctx->net, ctx->chain);
238
239 return 0;
240 }
241
242 static void nft_rule_expr_activate(const struct nft_ctx *ctx,
243 struct nft_rule *rule)
244 {
245 struct nft_expr *expr;
246
247 expr = nft_expr_first(rule);
248 while (expr != nft_expr_last(rule) && expr->ops) {
249 if (expr->ops->activate)
250 expr->ops->activate(ctx, expr);
251
252 expr = nft_expr_next(expr);
253 }
254 }
255
256 static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
257 struct nft_rule *rule,
258 enum nft_trans_phase phase)
259 {
260 struct nft_expr *expr;
261
262 expr = nft_expr_first(rule);
263 while (expr != nft_expr_last(rule) && expr->ops) {
264 if (expr->ops->deactivate)
265 expr->ops->deactivate(ctx, expr, phase);
266
267 expr = nft_expr_next(expr);
268 }
269 }
270
271 static int
272 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
273 {
274 /* You cannot delete the same rule twice */
275 if (nft_is_active_next(ctx->net, rule)) {
276 nft_deactivate_next(ctx->net, rule);
277 ctx->chain->use--;
278 return 0;
279 }
280 return -ENOENT;
281 }
282
283 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
284 struct nft_rule *rule)
285 {
286 struct nft_trans *trans;
287
288 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
289 if (trans == NULL)
290 return NULL;
291
292 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
293 nft_trans_rule_id(trans) =
294 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
295 }
296 nft_trans_rule(trans) = rule;
297 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
298
299 return trans;
300 }
301
302 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
303 {
304 struct nft_trans *trans;
305 int err;
306
307 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
308 if (trans == NULL)
309 return -ENOMEM;
310
311 err = nf_tables_delrule_deactivate(ctx, rule);
312 if (err < 0) {
313 nft_trans_destroy(trans);
314 return err;
315 }
316 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
317
318 return 0;
319 }
320
321 static int nft_delrule_by_chain(struct nft_ctx *ctx)
322 {
323 struct nft_rule *rule;
324 int err;
325
326 list_for_each_entry(rule, &ctx->chain->rules, list) {
327 if (!nft_is_active_next(ctx->net, rule))
328 continue;
329
330 err = nft_delrule(ctx, rule);
331 if (err < 0)
332 return err;
333 }
334 return 0;
335 }
336
337 static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
338 struct nft_set *set)
339 {
340 struct nft_trans *trans;
341
342 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
343 if (trans == NULL)
344 return -ENOMEM;
345
346 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
347 nft_trans_set_id(trans) =
348 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
349 nft_activate_next(ctx->net, set);
350 }
351 nft_trans_set(trans) = set;
352 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
353
354 return 0;
355 }
356
357 static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
358 {
359 int err;
360
361 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
362 if (err < 0)
363 return err;
364
365 nft_deactivate_next(ctx->net, set);
366 ctx->table->use--;
367
368 return err;
369 }
370
371 static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
372 struct nft_object *obj)
373 {
374 struct nft_trans *trans;
375
376 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
377 if (trans == NULL)
378 return -ENOMEM;
379
380 if (msg_type == NFT_MSG_NEWOBJ)
381 nft_activate_next(ctx->net, obj);
382
383 nft_trans_obj(trans) = obj;
384 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
385
386 return 0;
387 }
388
389 static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
390 {
391 int err;
392
393 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
394 if (err < 0)
395 return err;
396
397 nft_deactivate_next(ctx->net, obj);
398 ctx->table->use--;
399
400 return err;
401 }
402
403 static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
404 struct nft_flowtable *flowtable)
405 {
406 struct nft_trans *trans;
407
408 trans = nft_trans_alloc(ctx, msg_type,
409 sizeof(struct nft_trans_flowtable));
410 if (trans == NULL)
411 return -ENOMEM;
412
413 if (msg_type == NFT_MSG_NEWFLOWTABLE)
414 nft_activate_next(ctx->net, flowtable);
415
416 nft_trans_flowtable(trans) = flowtable;
417 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
418
419 return 0;
420 }
421
422 static int nft_delflowtable(struct nft_ctx *ctx,
423 struct nft_flowtable *flowtable)
424 {
425 int err;
426
427 err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
428 if (err < 0)
429 return err;
430
431 nft_deactivate_next(ctx->net, flowtable);
432 ctx->table->use--;
433
434 return err;
435 }
436
437 /*
438 * Tables
439 */
440
441 static struct nft_table *nft_table_lookup(const struct net *net,
442 const struct nlattr *nla,
443 u8 family, u8 genmask)
444 {
445 struct nft_table *table;
446
447 if (nla == NULL)
448 return ERR_PTR(-EINVAL);
449
450 list_for_each_entry_rcu(table, &net->nft.tables, list) {
451 if (!nla_strcmp(nla, table->name) &&
452 table->family == family &&
453 nft_active_genmask(table, genmask))
454 return table;
455 }
456
457 return ERR_PTR(-ENOENT);
458 }
459
460 static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
461 const struct nlattr *nla,
462 u8 genmask)
463 {
464 struct nft_table *table;
465
466 list_for_each_entry(table, &net->nft.tables, list) {
467 if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
468 nft_active_genmask(table, genmask))
469 return table;
470 }
471
472 return ERR_PTR(-ENOENT);
473 }
474
475 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
476 {
477 return ++table->hgenerator;
478 }
479
480 static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
481
482 static const struct nft_chain_type *
483 __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
484 {
485 int i;
486
487 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
488 if (chain_type[family][i] != NULL &&
489 !nla_strcmp(nla, chain_type[family][i]->name))
490 return chain_type[family][i];
491 }
492 return NULL;
493 }
494
495 /*
496 * Loading a module requires dropping mutex that guards the
497 * transaction.
498 * We first need to abort any pending transactions as once
499 * mutex is unlocked a different client could start a new
500 * transaction. It must not see any 'future generation'
501 * changes * as these changes will never happen.
502 */
503 #ifdef CONFIG_MODULES
504 static int __nf_tables_abort(struct net *net);
505
506 static void nft_request_module(struct net *net, const char *fmt, ...)
507 {
508 char module_name[MODULE_NAME_LEN];
509 va_list args;
510 int ret;
511
512 __nf_tables_abort(net);
513
514 va_start(args, fmt);
515 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
516 va_end(args);
517 if (WARN(ret >= MODULE_NAME_LEN, "truncated: '%s' (len %d)", module_name, ret))
518 return;
519
520 mutex_unlock(&net->nft.commit_mutex);
521 request_module("%s", module_name);
522 mutex_lock(&net->nft.commit_mutex);
523 }
524 #endif
525
526 static void lockdep_nfnl_nft_mutex_not_held(void)
527 {
528 #ifdef CONFIG_PROVE_LOCKING
529 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
530 #endif
531 }
532
533 static const struct nft_chain_type *
534 nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
535 u8 family, bool autoload)
536 {
537 const struct nft_chain_type *type;
538
539 type = __nf_tables_chain_type_lookup(nla, family);
540 if (type != NULL)
541 return type;
542
543 lockdep_nfnl_nft_mutex_not_held();
544 #ifdef CONFIG_MODULES
545 if (autoload) {
546 nft_request_module(net, "nft-chain-%u-%.*s", family,
547 nla_len(nla), (const char *)nla_data(nla));
548 type = __nf_tables_chain_type_lookup(nla, family);
549 if (type != NULL)
550 return ERR_PTR(-EAGAIN);
551 }
552 #endif
553 return ERR_PTR(-ENOENT);
554 }
555
556 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
557 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
558 .len = NFT_TABLE_MAXNAMELEN - 1 },
559 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
560 [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
561 };
562
563 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
564 u32 portid, u32 seq, int event, u32 flags,
565 int family, const struct nft_table *table)
566 {
567 struct nlmsghdr *nlh;
568 struct nfgenmsg *nfmsg;
569
570 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
571 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
572 if (nlh == NULL)
573 goto nla_put_failure;
574
575 nfmsg = nlmsg_data(nlh);
576 nfmsg->nfgen_family = family;
577 nfmsg->version = NFNETLINK_V0;
578 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
579
580 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
581 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
582 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
583 nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
584 NFTA_TABLE_PAD))
585 goto nla_put_failure;
586
587 nlmsg_end(skb, nlh);
588 return 0;
589
590 nla_put_failure:
591 nlmsg_trim(skb, nlh);
592 return -1;
593 }
594
595 static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
596 {
597 struct sk_buff *skb;
598 int err;
599
600 if (!ctx->report &&
601 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
602 return;
603
604 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
605 if (skb == NULL)
606 goto err;
607
608 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
609 event, 0, ctx->family, ctx->table);
610 if (err < 0) {
611 kfree_skb(skb);
612 goto err;
613 }
614
615 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
616 ctx->report, GFP_KERNEL);
617 return;
618 err:
619 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
620 }
621
622 static int nf_tables_dump_tables(struct sk_buff *skb,
623 struct netlink_callback *cb)
624 {
625 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
626 const struct nft_table *table;
627 unsigned int idx = 0, s_idx = cb->args[0];
628 struct net *net = sock_net(skb->sk);
629 int family = nfmsg->nfgen_family;
630
631 rcu_read_lock();
632 cb->seq = net->nft.base_seq;
633
634 list_for_each_entry_rcu(table, &net->nft.tables, list) {
635 if (family != NFPROTO_UNSPEC && family != table->family)
636 continue;
637
638 if (idx < s_idx)
639 goto cont;
640 if (idx > s_idx)
641 memset(&cb->args[1], 0,
642 sizeof(cb->args) - sizeof(cb->args[0]));
643 if (!nft_is_active(net, table))
644 continue;
645 if (nf_tables_fill_table_info(skb, net,
646 NETLINK_CB(cb->skb).portid,
647 cb->nlh->nlmsg_seq,
648 NFT_MSG_NEWTABLE, NLM_F_MULTI,
649 table->family, table) < 0)
650 goto done;
651
652 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
653 cont:
654 idx++;
655 }
656 done:
657 rcu_read_unlock();
658 cb->args[0] = idx;
659 return skb->len;
660 }
661
662 static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
663 const struct nlmsghdr *nlh,
664 struct netlink_dump_control *c)
665 {
666 int err;
667
668 if (!try_module_get(THIS_MODULE))
669 return -EINVAL;
670
671 rcu_read_unlock();
672 err = netlink_dump_start(nlsk, skb, nlh, c);
673 rcu_read_lock();
674 module_put(THIS_MODULE);
675
676 return err;
677 }
678
679 /* called with rcu_read_lock held */
680 static int nf_tables_gettable(struct net *net, struct sock *nlsk,
681 struct sk_buff *skb, const struct nlmsghdr *nlh,
682 const struct nlattr * const nla[],
683 struct netlink_ext_ack *extack)
684 {
685 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
686 u8 genmask = nft_genmask_cur(net);
687 const struct nft_table *table;
688 struct sk_buff *skb2;
689 int family = nfmsg->nfgen_family;
690 int err;
691
692 if (nlh->nlmsg_flags & NLM_F_DUMP) {
693 struct netlink_dump_control c = {
694 .dump = nf_tables_dump_tables,
695 .module = THIS_MODULE,
696 };
697
698 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
699 }
700
701 table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask);
702 if (IS_ERR(table)) {
703 NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
704 return PTR_ERR(table);
705 }
706
707 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
708 if (!skb2)
709 return -ENOMEM;
710
711 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
712 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
713 family, table);
714 if (err < 0)
715 goto err;
716
717 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
718
719 err:
720 kfree_skb(skb2);
721 return err;
722 }
723
724 static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
725 {
726 struct nft_chain *chain;
727 u32 i = 0;
728
729 list_for_each_entry(chain, &table->chains, list) {
730 if (!nft_is_active_next(net, chain))
731 continue;
732 if (!nft_is_base_chain(chain))
733 continue;
734
735 if (cnt && i++ == cnt)
736 break;
737
738 nf_unregister_net_hook(net, &nft_base_chain(chain)->ops);
739 }
740 }
741
742 static int nf_tables_table_enable(struct net *net, struct nft_table *table)
743 {
744 struct nft_chain *chain;
745 int err, i = 0;
746
747 list_for_each_entry(chain, &table->chains, list) {
748 if (!nft_is_active_next(net, chain))
749 continue;
750 if (!nft_is_base_chain(chain))
751 continue;
752
753 err = nf_register_net_hook(net, &nft_base_chain(chain)->ops);
754 if (err < 0)
755 goto err;
756
757 i++;
758 }
759 return 0;
760 err:
761 if (i)
762 nft_table_disable(net, table, i);
763 return err;
764 }
765
766 static void nf_tables_table_disable(struct net *net, struct nft_table *table)
767 {
768 nft_table_disable(net, table, 0);
769 }
770
771 static int nf_tables_updtable(struct nft_ctx *ctx)
772 {
773 struct nft_trans *trans;
774 u32 flags;
775 int ret = 0;
776
777 if (!ctx->nla[NFTA_TABLE_FLAGS])
778 return 0;
779
780 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
781 if (flags & ~NFT_TABLE_F_DORMANT)
782 return -EINVAL;
783
784 if (flags == ctx->table->flags)
785 return 0;
786
787 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
788 sizeof(struct nft_trans_table));
789 if (trans == NULL)
790 return -ENOMEM;
791
792 if ((flags & NFT_TABLE_F_DORMANT) &&
793 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
794 nft_trans_table_enable(trans) = false;
795 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
796 ctx->table->flags & NFT_TABLE_F_DORMANT) {
797 ret = nf_tables_table_enable(ctx->net, ctx->table);
798 if (ret >= 0) {
799 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
800 nft_trans_table_enable(trans) = true;
801 }
802 }
803 if (ret < 0)
804 goto err;
805
806 nft_trans_table_update(trans) = true;
807 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
808 return 0;
809 err:
810 nft_trans_destroy(trans);
811 return ret;
812 }
813
814 static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
815 {
816 const char *name = data;
817
818 return jhash(name, strlen(name), seed);
819 }
820
821 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
822 {
823 const struct nft_chain *chain = data;
824
825 return nft_chain_hash(chain->name, 0, seed);
826 }
827
828 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
829 const void *ptr)
830 {
831 const struct nft_chain *chain = ptr;
832 const char *name = arg->key;
833
834 return strcmp(chain->name, name);
835 }
836
837 static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
838 {
839 const struct nft_object_hash_key *k = data;
840
841 seed ^= hash_ptr(k->table, 32);
842
843 return jhash(k->name, strlen(k->name), seed);
844 }
845
846 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
847 {
848 const struct nft_object *obj = data;
849
850 return nft_objname_hash(&obj->key, 0, seed);
851 }
852
853 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
854 const void *ptr)
855 {
856 const struct nft_object_hash_key *k = arg->key;
857 const struct nft_object *obj = ptr;
858
859 if (obj->key.table != k->table)
860 return -1;
861
862 return strcmp(obj->key.name, k->name);
863 }
864
865 static int nf_tables_newtable(struct net *net, struct sock *nlsk,
866 struct sk_buff *skb, const struct nlmsghdr *nlh,
867 const struct nlattr * const nla[],
868 struct netlink_ext_ack *extack)
869 {
870 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
871 u8 genmask = nft_genmask_next(net);
872 int family = nfmsg->nfgen_family;
873 const struct nlattr *attr;
874 struct nft_table *table;
875 u32 flags = 0;
876 struct nft_ctx ctx;
877 int err;
878
879 lockdep_assert_held(&net->nft.commit_mutex);
880 attr = nla[NFTA_TABLE_NAME];
881 table = nft_table_lookup(net, attr, family, genmask);
882 if (IS_ERR(table)) {
883 if (PTR_ERR(table) != -ENOENT)
884 return PTR_ERR(table);
885 } else {
886 if (nlh->nlmsg_flags & NLM_F_EXCL) {
887 NL_SET_BAD_ATTR(extack, attr);
888 return -EEXIST;
889 }
890 if (nlh->nlmsg_flags & NLM_F_REPLACE)
891 return -EOPNOTSUPP;
892
893 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
894 return nf_tables_updtable(&ctx);
895 }
896
897 if (nla[NFTA_TABLE_FLAGS]) {
898 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
899 if (flags & ~NFT_TABLE_F_DORMANT)
900 return -EINVAL;
901 }
902
903 err = -ENOMEM;
904 table = kzalloc(sizeof(*table), GFP_KERNEL);
905 if (table == NULL)
906 goto err_kzalloc;
907
908 table->name = nla_strdup(attr, GFP_KERNEL);
909 if (table->name == NULL)
910 goto err_strdup;
911
912 err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
913 if (err)
914 goto err_chain_ht;
915
916 INIT_LIST_HEAD(&table->chains);
917 INIT_LIST_HEAD(&table->sets);
918 INIT_LIST_HEAD(&table->objects);
919 INIT_LIST_HEAD(&table->flowtables);
920 table->family = family;
921 table->flags = flags;
922 table->handle = ++table_handle;
923
924 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
925 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
926 if (err < 0)
927 goto err_trans;
928
929 list_add_tail_rcu(&table->list, &net->nft.tables);
930 return 0;
931 err_trans:
932 rhltable_destroy(&table->chains_ht);
933 err_chain_ht:
934 kfree(table->name);
935 err_strdup:
936 kfree(table);
937 err_kzalloc:
938 return err;
939 }
940
941 static int nft_flush_table(struct nft_ctx *ctx)
942 {
943 struct nft_flowtable *flowtable, *nft;
944 struct nft_chain *chain, *nc;
945 struct nft_object *obj, *ne;
946 struct nft_set *set, *ns;
947 int err;
948
949 list_for_each_entry(chain, &ctx->table->chains, list) {
950 if (!nft_is_active_next(ctx->net, chain))
951 continue;
952
953 ctx->chain = chain;
954
955 err = nft_delrule_by_chain(ctx);
956 if (err < 0)
957 goto out;
958 }
959
960 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
961 if (!nft_is_active_next(ctx->net, set))
962 continue;
963
964 if (nft_set_is_anonymous(set) &&
965 !list_empty(&set->bindings))
966 continue;
967
968 err = nft_delset(ctx, set);
969 if (err < 0)
970 goto out;
971 }
972
973 list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
974 err = nft_delflowtable(ctx, flowtable);
975 if (err < 0)
976 goto out;
977 }
978
979 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
980 err = nft_delobj(ctx, obj);
981 if (err < 0)
982 goto out;
983 }
984
985 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
986 if (!nft_is_active_next(ctx->net, chain))
987 continue;
988
989 ctx->chain = chain;
990
991 err = nft_delchain(ctx);
992 if (err < 0)
993 goto out;
994 }
995
996 err = nft_deltable(ctx);
997 out:
998 return err;
999 }
1000
1001 static int nft_flush(struct nft_ctx *ctx, int family)
1002 {
1003 struct nft_table *table, *nt;
1004 const struct nlattr * const *nla = ctx->nla;
1005 int err = 0;
1006
1007 list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) {
1008 if (family != AF_UNSPEC && table->family != family)
1009 continue;
1010
1011 ctx->family = table->family;
1012
1013 if (!nft_is_active_next(ctx->net, table))
1014 continue;
1015
1016 if (nla[NFTA_TABLE_NAME] &&
1017 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1018 continue;
1019
1020 ctx->table = table;
1021
1022 err = nft_flush_table(ctx);
1023 if (err < 0)
1024 goto out;
1025 }
1026 out:
1027 return err;
1028 }
1029
1030 static int nf_tables_deltable(struct net *net, struct sock *nlsk,
1031 struct sk_buff *skb, const struct nlmsghdr *nlh,
1032 const struct nlattr * const nla[],
1033 struct netlink_ext_ack *extack)
1034 {
1035 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1036 u8 genmask = nft_genmask_next(net);
1037 int family = nfmsg->nfgen_family;
1038 const struct nlattr *attr;
1039 struct nft_table *table;
1040 struct nft_ctx ctx;
1041
1042 nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla);
1043 if (family == AF_UNSPEC ||
1044 (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
1045 return nft_flush(&ctx, family);
1046
1047 if (nla[NFTA_TABLE_HANDLE]) {
1048 attr = nla[NFTA_TABLE_HANDLE];
1049 table = nft_table_lookup_byhandle(net, attr, genmask);
1050 } else {
1051 attr = nla[NFTA_TABLE_NAME];
1052 table = nft_table_lookup(net, attr, family, genmask);
1053 }
1054
1055 if (IS_ERR(table)) {
1056 NL_SET_BAD_ATTR(extack, attr);
1057 return PTR_ERR(table);
1058 }
1059
1060 if (nlh->nlmsg_flags & NLM_F_NONREC &&
1061 table->use > 0)
1062 return -EBUSY;
1063
1064 ctx.family = family;
1065 ctx.table = table;
1066
1067 return nft_flush_table(&ctx);
1068 }
1069
1070 static void nf_tables_table_destroy(struct nft_ctx *ctx)
1071 {
1072 if (WARN_ON(ctx->table->use > 0))
1073 return;
1074
1075 rhltable_destroy(&ctx->table->chains_ht);
1076 kfree(ctx->table->name);
1077 kfree(ctx->table);
1078 }
1079
1080 void nft_register_chain_type(const struct nft_chain_type *ctype)
1081 {
1082 if (WARN_ON(ctype->family >= NFPROTO_NUMPROTO))
1083 return;
1084
1085 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1086 if (WARN_ON(chain_type[ctype->family][ctype->type] != NULL)) {
1087 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1088 return;
1089 }
1090 chain_type[ctype->family][ctype->type] = ctype;
1091 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1092 }
1093 EXPORT_SYMBOL_GPL(nft_register_chain_type);
1094
1095 void nft_unregister_chain_type(const struct nft_chain_type *ctype)
1096 {
1097 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1098 chain_type[ctype->family][ctype->type] = NULL;
1099 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1100 }
1101 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
1102
1103 /*
1104 * Chains
1105 */
1106
1107 static struct nft_chain *
1108 nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
1109 {
1110 struct nft_chain *chain;
1111
1112 list_for_each_entry(chain, &table->chains, list) {
1113 if (chain->handle == handle &&
1114 nft_active_genmask(chain, genmask))
1115 return chain;
1116 }
1117
1118 return ERR_PTR(-ENOENT);
1119 }
1120
1121 static bool lockdep_commit_lock_is_held(const struct net *net)
1122 {
1123 #ifdef CONFIG_PROVE_LOCKING
1124 return lockdep_is_held(&net->nft.commit_mutex);
1125 #else
1126 return true;
1127 #endif
1128 }
1129
1130 static struct nft_chain *nft_chain_lookup(struct net *net,
1131 struct nft_table *table,
1132 const struct nlattr *nla, u8 genmask)
1133 {
1134 char search[NFT_CHAIN_MAXNAMELEN + 1];
1135 struct rhlist_head *tmp, *list;
1136 struct nft_chain *chain;
1137
1138 if (nla == NULL)
1139 return ERR_PTR(-EINVAL);
1140
1141 nla_strlcpy(search, nla, sizeof(search));
1142
1143 WARN_ON(!rcu_read_lock_held() &&
1144 !lockdep_commit_lock_is_held(net));
1145
1146 chain = ERR_PTR(-ENOENT);
1147 rcu_read_lock();
1148 list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1149 if (!list)
1150 goto out_unlock;
1151
1152 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1153 if (nft_active_genmask(chain, genmask))
1154 goto out_unlock;
1155 }
1156 chain = ERR_PTR(-ENOENT);
1157 out_unlock:
1158 rcu_read_unlock();
1159 return chain;
1160 }
1161
1162 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
1163 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
1164 .len = NFT_TABLE_MAXNAMELEN - 1 },
1165 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
1166 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
1167 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1168 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
1169 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
1170 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
1171 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
1172 };
1173
1174 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1175 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
1176 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
1177 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
1178 .len = IFNAMSIZ - 1 },
1179 };
1180
1181 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1182 {
1183 struct nft_stats *cpu_stats, total;
1184 struct nlattr *nest;
1185 unsigned int seq;
1186 u64 pkts, bytes;
1187 int cpu;
1188
1189 if (!stats)
1190 return 0;
1191
1192 memset(&total, 0, sizeof(total));
1193 for_each_possible_cpu(cpu) {
1194 cpu_stats = per_cpu_ptr(stats, cpu);
1195 do {
1196 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1197 pkts = cpu_stats->pkts;
1198 bytes = cpu_stats->bytes;
1199 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
1200 total.pkts += pkts;
1201 total.bytes += bytes;
1202 }
1203 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
1204 if (nest == NULL)
1205 goto nla_put_failure;
1206
1207 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1208 NFTA_COUNTER_PAD) ||
1209 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1210 NFTA_COUNTER_PAD))
1211 goto nla_put_failure;
1212
1213 nla_nest_end(skb, nest);
1214 return 0;
1215
1216 nla_put_failure:
1217 return -ENOSPC;
1218 }
1219
1220 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1221 u32 portid, u32 seq, int event, u32 flags,
1222 int family, const struct nft_table *table,
1223 const struct nft_chain *chain)
1224 {
1225 struct nlmsghdr *nlh;
1226 struct nfgenmsg *nfmsg;
1227
1228 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
1229 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
1230 if (nlh == NULL)
1231 goto nla_put_failure;
1232
1233 nfmsg = nlmsg_data(nlh);
1234 nfmsg->nfgen_family = family;
1235 nfmsg->version = NFNETLINK_V0;
1236 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
1237
1238 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
1239 goto nla_put_failure;
1240 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1241 NFTA_CHAIN_PAD))
1242 goto nla_put_failure;
1243 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
1244 goto nla_put_failure;
1245
1246 if (nft_is_base_chain(chain)) {
1247 const struct nft_base_chain *basechain = nft_base_chain(chain);
1248 const struct nf_hook_ops *ops = &basechain->ops;
1249 struct nft_stats __percpu *stats;
1250 struct nlattr *nest;
1251
1252 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1253 if (nest == NULL)
1254 goto nla_put_failure;
1255 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1256 goto nla_put_failure;
1257 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1258 goto nla_put_failure;
1259 if (basechain->dev_name[0] &&
1260 nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name))
1261 goto nla_put_failure;
1262 nla_nest_end(skb, nest);
1263
1264 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1265 htonl(basechain->policy)))
1266 goto nla_put_failure;
1267
1268 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1269 goto nla_put_failure;
1270
1271 stats = rcu_dereference_check(basechain->stats,
1272 lockdep_commit_lock_is_held(net));
1273 if (nft_dump_stats(skb, stats))
1274 goto nla_put_failure;
1275 }
1276
1277 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1278 goto nla_put_failure;
1279
1280 nlmsg_end(skb, nlh);
1281 return 0;
1282
1283 nla_put_failure:
1284 nlmsg_trim(skb, nlh);
1285 return -1;
1286 }
1287
1288 static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
1289 {
1290 struct sk_buff *skb;
1291 int err;
1292
1293 if (!ctx->report &&
1294 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1295 return;
1296
1297 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1298 if (skb == NULL)
1299 goto err;
1300
1301 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1302 event, 0, ctx->family, ctx->table,
1303 ctx->chain);
1304 if (err < 0) {
1305 kfree_skb(skb);
1306 goto err;
1307 }
1308
1309 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1310 ctx->report, GFP_KERNEL);
1311 return;
1312 err:
1313 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1314 }
1315
1316 static int nf_tables_dump_chains(struct sk_buff *skb,
1317 struct netlink_callback *cb)
1318 {
1319 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1320 const struct nft_table *table;
1321 const struct nft_chain *chain;
1322 unsigned int idx = 0, s_idx = cb->args[0];
1323 struct net *net = sock_net(skb->sk);
1324 int family = nfmsg->nfgen_family;
1325
1326 rcu_read_lock();
1327 cb->seq = net->nft.base_seq;
1328
1329 list_for_each_entry_rcu(table, &net->nft.tables, list) {
1330 if (family != NFPROTO_UNSPEC && family != table->family)
1331 continue;
1332
1333 list_for_each_entry_rcu(chain, &table->chains, list) {
1334 if (idx < s_idx)
1335 goto cont;
1336 if (idx > s_idx)
1337 memset(&cb->args[1], 0,
1338 sizeof(cb->args) - sizeof(cb->args[0]));
1339 if (!nft_is_active(net, chain))
1340 continue;
1341 if (nf_tables_fill_chain_info(skb, net,
1342 NETLINK_CB(cb->skb).portid,
1343 cb->nlh->nlmsg_seq,
1344 NFT_MSG_NEWCHAIN,
1345 NLM_F_MULTI,
1346 table->family, table,
1347 chain) < 0)
1348 goto done;
1349
1350 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1351 cont:
1352 idx++;
1353 }
1354 }
1355 done:
1356 rcu_read_unlock();
1357 cb->args[0] = idx;
1358 return skb->len;
1359 }
1360
1361 /* called with rcu_read_lock held */
1362 static int nf_tables_getchain(struct net *net, struct sock *nlsk,
1363 struct sk_buff *skb, const struct nlmsghdr *nlh,
1364 const struct nlattr * const nla[],
1365 struct netlink_ext_ack *extack)
1366 {
1367 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1368 u8 genmask = nft_genmask_cur(net);
1369 const struct nft_chain *chain;
1370 struct nft_table *table;
1371 struct sk_buff *skb2;
1372 int family = nfmsg->nfgen_family;
1373 int err;
1374
1375 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1376 struct netlink_dump_control c = {
1377 .dump = nf_tables_dump_chains,
1378 .module = THIS_MODULE,
1379 };
1380
1381 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
1382 }
1383
1384 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
1385 if (IS_ERR(table)) {
1386 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
1387 return PTR_ERR(table);
1388 }
1389
1390 chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
1391 if (IS_ERR(chain)) {
1392 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
1393 return PTR_ERR(chain);
1394 }
1395
1396 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1397 if (!skb2)
1398 return -ENOMEM;
1399
1400 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1401 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1402 family, table, chain);
1403 if (err < 0)
1404 goto err;
1405
1406 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1407
1408 err:
1409 kfree_skb(skb2);
1410 return err;
1411 }
1412
1413 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1414 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1415 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1416 };
1417
1418 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1419 {
1420 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1421 struct nft_stats __percpu *newstats;
1422 struct nft_stats *stats;
1423 int err;
1424
1425 err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
1426 nft_counter_policy, NULL);
1427 if (err < 0)
1428 return ERR_PTR(err);
1429
1430 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1431 return ERR_PTR(-EINVAL);
1432
1433 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1434 if (newstats == NULL)
1435 return ERR_PTR(-ENOMEM);
1436
1437 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1438 * are not exposed to userspace.
1439 */
1440 preempt_disable();
1441 stats = this_cpu_ptr(newstats);
1442 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1443 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1444 preempt_enable();
1445
1446 return newstats;
1447 }
1448
1449 static void nft_chain_stats_replace(struct nft_trans *trans)
1450 {
1451 struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
1452
1453 if (!nft_trans_chain_stats(trans))
1454 return;
1455
1456 rcu_swap_protected(chain->stats, nft_trans_chain_stats(trans),
1457 lockdep_commit_lock_is_held(trans->ctx.net));
1458
1459 if (!nft_trans_chain_stats(trans))
1460 static_branch_inc(&nft_counters_enabled);
1461 }
1462
1463 static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
1464 {
1465 struct nft_rule **g0 = rcu_dereference_raw(chain->rules_gen_0);
1466 struct nft_rule **g1 = rcu_dereference_raw(chain->rules_gen_1);
1467
1468 if (g0 != g1)
1469 kvfree(g1);
1470 kvfree(g0);
1471
1472 /* should be NULL either via abort or via successful commit */
1473 WARN_ON_ONCE(chain->rules_next);
1474 kvfree(chain->rules_next);
1475 }
1476
1477 static void nf_tables_chain_destroy(struct nft_ctx *ctx)
1478 {
1479 struct nft_chain *chain = ctx->chain;
1480
1481 if (WARN_ON(chain->use > 0))
1482 return;
1483
1484 /* no concurrent access possible anymore */
1485 nf_tables_chain_free_chain_rules(chain);
1486
1487 if (nft_is_base_chain(chain)) {
1488 struct nft_base_chain *basechain = nft_base_chain(chain);
1489
1490 module_put(basechain->type->owner);
1491 if (rcu_access_pointer(basechain->stats)) {
1492 static_branch_dec(&nft_counters_enabled);
1493 free_percpu(rcu_dereference_raw(basechain->stats));
1494 }
1495 kfree(chain->name);
1496 kfree(basechain);
1497 } else {
1498 kfree(chain->name);
1499 kfree(chain);
1500 }
1501 }
1502
1503 struct nft_chain_hook {
1504 u32 num;
1505 s32 priority;
1506 const struct nft_chain_type *type;
1507 struct net_device *dev;
1508 };
1509
1510 static int nft_chain_parse_hook(struct net *net,
1511 const struct nlattr * const nla[],
1512 struct nft_chain_hook *hook, u8 family,
1513 bool autoload)
1514 {
1515 struct nlattr *ha[NFTA_HOOK_MAX + 1];
1516 const struct nft_chain_type *type;
1517 struct net_device *dev;
1518 int err;
1519
1520 lockdep_assert_held(&net->nft.commit_mutex);
1521 lockdep_nfnl_nft_mutex_not_held();
1522
1523 err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
1524 nla[NFTA_CHAIN_HOOK],
1525 nft_hook_policy, NULL);
1526 if (err < 0)
1527 return err;
1528
1529 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1530 ha[NFTA_HOOK_PRIORITY] == NULL)
1531 return -EINVAL;
1532
1533 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1534 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1535
1536 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1537 if (nla[NFTA_CHAIN_TYPE]) {
1538 type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
1539 family, autoload);
1540 if (IS_ERR(type))
1541 return PTR_ERR(type);
1542 }
1543 if (hook->num > NF_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
1544 return -EOPNOTSUPP;
1545
1546 if (type->type == NFT_CHAIN_T_NAT &&
1547 hook->priority <= NF_IP_PRI_CONNTRACK)
1548 return -EOPNOTSUPP;
1549
1550 if (!try_module_get(type->owner))
1551 return -ENOENT;
1552
1553 hook->type = type;
1554
1555 hook->dev = NULL;
1556 if (family == NFPROTO_NETDEV) {
1557 char ifname[IFNAMSIZ];
1558
1559 if (!ha[NFTA_HOOK_DEV]) {
1560 module_put(type->owner);
1561 return -EOPNOTSUPP;
1562 }
1563
1564 nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ);
1565 dev = __dev_get_by_name(net, ifname);
1566 if (!dev) {
1567 module_put(type->owner);
1568 return -ENOENT;
1569 }
1570 hook->dev = dev;
1571 } else if (ha[NFTA_HOOK_DEV]) {
1572 module_put(type->owner);
1573 return -EOPNOTSUPP;
1574 }
1575
1576 return 0;
1577 }
1578
1579 static void nft_chain_release_hook(struct nft_chain_hook *hook)
1580 {
1581 module_put(hook->type->owner);
1582 }
1583
1584 struct nft_rules_old {
1585 struct rcu_head h;
1586 struct nft_rule **start;
1587 };
1588
1589 static struct nft_rule **nf_tables_chain_alloc_rules(const struct nft_chain *chain,
1590 unsigned int alloc)
1591 {
1592 if (alloc > INT_MAX)
1593 return NULL;
1594
1595 alloc += 1; /* NULL, ends rules */
1596 if (sizeof(struct nft_rule *) > INT_MAX / alloc)
1597 return NULL;
1598
1599 alloc *= sizeof(struct nft_rule *);
1600 alloc += sizeof(struct nft_rules_old);
1601
1602 return kvmalloc(alloc, GFP_KERNEL);
1603 }
1604
1605 static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
1606 u8 policy)
1607 {
1608 const struct nlattr * const *nla = ctx->nla;
1609 struct nft_table *table = ctx->table;
1610 struct nft_base_chain *basechain;
1611 struct nft_stats __percpu *stats;
1612 struct net *net = ctx->net;
1613 struct nft_trans *trans;
1614 struct nft_chain *chain;
1615 struct nft_rule **rules;
1616 int err;
1617
1618 if (table->use == UINT_MAX)
1619 return -EOVERFLOW;
1620
1621 if (nla[NFTA_CHAIN_HOOK]) {
1622 struct nft_chain_hook hook;
1623 struct nf_hook_ops *ops;
1624
1625 err = nft_chain_parse_hook(net, nla, &hook, family, true);
1626 if (err < 0)
1627 return err;
1628
1629 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1630 if (basechain == NULL) {
1631 nft_chain_release_hook(&hook);
1632 return -ENOMEM;
1633 }
1634
1635 if (hook.dev != NULL)
1636 strncpy(basechain->dev_name, hook.dev->name, IFNAMSIZ);
1637
1638 if (nla[NFTA_CHAIN_COUNTERS]) {
1639 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1640 if (IS_ERR(stats)) {
1641 nft_chain_release_hook(&hook);
1642 kfree(basechain);
1643 return PTR_ERR(stats);
1644 }
1645 rcu_assign_pointer(basechain->stats, stats);
1646 static_branch_inc(&nft_counters_enabled);
1647 }
1648
1649 basechain->type = hook.type;
1650 chain = &basechain->chain;
1651
1652 ops = &basechain->ops;
1653 ops->pf = family;
1654 ops->hooknum = hook.num;
1655 ops->priority = hook.priority;
1656 ops->priv = chain;
1657 ops->hook = hook.type->hooks[ops->hooknum];
1658 ops->dev = hook.dev;
1659
1660 chain->flags |= NFT_BASE_CHAIN;
1661 basechain->policy = NF_ACCEPT;
1662 } else {
1663 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1664 if (chain == NULL)
1665 return -ENOMEM;
1666 }
1667 ctx->chain = chain;
1668
1669 INIT_LIST_HEAD(&chain->rules);
1670 chain->handle = nf_tables_alloc_handle(table);
1671 chain->table = table;
1672 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
1673 if (!chain->name) {
1674 err = -ENOMEM;
1675 goto err1;
1676 }
1677
1678 rules = nf_tables_chain_alloc_rules(chain, 0);
1679 if (!rules) {
1680 err = -ENOMEM;
1681 goto err1;
1682 }
1683
1684 *rules = NULL;
1685 rcu_assign_pointer(chain->rules_gen_0, rules);
1686 rcu_assign_pointer(chain->rules_gen_1, rules);
1687
1688 err = nf_tables_register_hook(net, table, chain);
1689 if (err < 0)
1690 goto err1;
1691
1692 err = rhltable_insert_key(&table->chains_ht, chain->name,
1693 &chain->rhlhead, nft_chain_ht_params);
1694 if (err)
1695 goto err2;
1696
1697 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
1698 if (IS_ERR(trans)) {
1699 err = PTR_ERR(trans);
1700 rhltable_remove(&table->chains_ht, &chain->rhlhead,
1701 nft_chain_ht_params);
1702 goto err2;
1703 }
1704
1705 nft_trans_chain_policy(trans) = -1;
1706 if (nft_is_base_chain(chain))
1707 nft_trans_chain_policy(trans) = policy;
1708
1709 table->use++;
1710 list_add_tail_rcu(&chain->list, &table->chains);
1711
1712 return 0;
1713 err2:
1714 nf_tables_unregister_hook(net, table, chain);
1715 err1:
1716 nf_tables_chain_destroy(ctx);
1717
1718 return err;
1719 }
1720
1721 static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy)
1722 {
1723 const struct nlattr * const *nla = ctx->nla;
1724 struct nft_table *table = ctx->table;
1725 struct nft_chain *chain = ctx->chain;
1726 struct nft_base_chain *basechain;
1727 struct nft_stats *stats = NULL;
1728 struct nft_chain_hook hook;
1729 struct nf_hook_ops *ops;
1730 struct nft_trans *trans;
1731 int err;
1732
1733 if (nla[NFTA_CHAIN_HOOK]) {
1734 if (!nft_is_base_chain(chain))
1735 return -EBUSY;
1736
1737 err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family,
1738 false);
1739 if (err < 0)
1740 return err;
1741
1742 basechain = nft_base_chain(chain);
1743 if (basechain->type != hook.type) {
1744 nft_chain_release_hook(&hook);
1745 return -EBUSY;
1746 }
1747
1748 ops = &basechain->ops;
1749 if (ops->hooknum != hook.num ||
1750 ops->priority != hook.priority ||
1751 ops->dev != hook.dev) {
1752 nft_chain_release_hook(&hook);
1753 return -EBUSY;
1754 }
1755 nft_chain_release_hook(&hook);
1756 }
1757
1758 if (nla[NFTA_CHAIN_HANDLE] &&
1759 nla[NFTA_CHAIN_NAME]) {
1760 struct nft_chain *chain2;
1761
1762 chain2 = nft_chain_lookup(ctx->net, table,
1763 nla[NFTA_CHAIN_NAME], genmask);
1764 if (!IS_ERR(chain2))
1765 return -EEXIST;
1766 }
1767
1768 if (nla[NFTA_CHAIN_COUNTERS]) {
1769 if (!nft_is_base_chain(chain))
1770 return -EOPNOTSUPP;
1771
1772 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1773 if (IS_ERR(stats))
1774 return PTR_ERR(stats);
1775 }
1776
1777 err = -ENOMEM;
1778 trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
1779 sizeof(struct nft_trans_chain));
1780 if (trans == NULL)
1781 goto err;
1782
1783 nft_trans_chain_stats(trans) = stats;
1784 nft_trans_chain_update(trans) = true;
1785
1786 if (nla[NFTA_CHAIN_POLICY])
1787 nft_trans_chain_policy(trans) = policy;
1788 else
1789 nft_trans_chain_policy(trans) = -1;
1790
1791 if (nla[NFTA_CHAIN_HANDLE] &&
1792 nla[NFTA_CHAIN_NAME]) {
1793 struct nft_trans *tmp;
1794 char *name;
1795
1796 err = -ENOMEM;
1797 name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
1798 if (!name)
1799 goto err;
1800
1801 err = -EEXIST;
1802 list_for_each_entry(tmp, &ctx->net->nft.commit_list, list) {
1803 if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
1804 tmp->ctx.table == table &&
1805 nft_trans_chain_update(tmp) &&
1806 nft_trans_chain_name(tmp) &&
1807 strcmp(name, nft_trans_chain_name(tmp)) == 0) {
1808 kfree(name);
1809 goto err;
1810 }
1811 }
1812
1813 nft_trans_chain_name(trans) = name;
1814 }
1815 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1816
1817 return 0;
1818 err:
1819 free_percpu(stats);
1820 kfree(trans);
1821 return err;
1822 }
1823
1824 static int nf_tables_newchain(struct net *net, struct sock *nlsk,
1825 struct sk_buff *skb, const struct nlmsghdr *nlh,
1826 const struct nlattr * const nla[],
1827 struct netlink_ext_ack *extack)
1828 {
1829 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1830 u8 genmask = nft_genmask_next(net);
1831 int family = nfmsg->nfgen_family;
1832 const struct nlattr *attr;
1833 struct nft_table *table;
1834 struct nft_chain *chain;
1835 u8 policy = NF_ACCEPT;
1836 struct nft_ctx ctx;
1837 u64 handle = 0;
1838
1839 lockdep_assert_held(&net->nft.commit_mutex);
1840
1841 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
1842 if (IS_ERR(table)) {
1843 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
1844 return PTR_ERR(table);
1845 }
1846
1847 chain = NULL;
1848 attr = nla[NFTA_CHAIN_NAME];
1849
1850 if (nla[NFTA_CHAIN_HANDLE]) {
1851 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1852 chain = nft_chain_lookup_byhandle(table, handle, genmask);
1853 if (IS_ERR(chain)) {
1854 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
1855 return PTR_ERR(chain);
1856 }
1857 attr = nla[NFTA_CHAIN_HANDLE];
1858 } else {
1859 chain = nft_chain_lookup(net, table, attr, genmask);
1860 if (IS_ERR(chain)) {
1861 if (PTR_ERR(chain) != -ENOENT) {
1862 NL_SET_BAD_ATTR(extack, attr);
1863 return PTR_ERR(chain);
1864 }
1865 chain = NULL;
1866 }
1867 }
1868
1869 if (nla[NFTA_CHAIN_POLICY]) {
1870 if (chain != NULL &&
1871 !nft_is_base_chain(chain)) {
1872 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
1873 return -EOPNOTSUPP;
1874 }
1875
1876 if (chain == NULL &&
1877 nla[NFTA_CHAIN_HOOK] == NULL) {
1878 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
1879 return -EOPNOTSUPP;
1880 }
1881
1882 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1883 switch (policy) {
1884 case NF_DROP:
1885 case NF_ACCEPT:
1886 break;
1887 default:
1888 return -EINVAL;
1889 }
1890 }
1891
1892 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
1893
1894 if (chain != NULL) {
1895 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1896 NL_SET_BAD_ATTR(extack, attr);
1897 return -EEXIST;
1898 }
1899 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1900 return -EOPNOTSUPP;
1901
1902 return nf_tables_updchain(&ctx, genmask, policy);
1903 }
1904
1905 return nf_tables_addchain(&ctx, family, genmask, policy);
1906 }
1907
1908 static int nf_tables_delchain(struct net *net, struct sock *nlsk,
1909 struct sk_buff *skb, const struct nlmsghdr *nlh,
1910 const struct nlattr * const nla[],
1911 struct netlink_ext_ack *extack)
1912 {
1913 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1914 u8 genmask = nft_genmask_next(net);
1915 int family = nfmsg->nfgen_family;
1916 const struct nlattr *attr;
1917 struct nft_table *table;
1918 struct nft_chain *chain;
1919 struct nft_rule *rule;
1920 struct nft_ctx ctx;
1921 u64 handle;
1922 u32 use;
1923 int err;
1924
1925 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
1926 if (IS_ERR(table)) {
1927 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
1928 return PTR_ERR(table);
1929 }
1930
1931 if (nla[NFTA_CHAIN_HANDLE]) {
1932 attr = nla[NFTA_CHAIN_HANDLE];
1933 handle = be64_to_cpu(nla_get_be64(attr));
1934 chain = nft_chain_lookup_byhandle(table, handle, genmask);
1935 } else {
1936 attr = nla[NFTA_CHAIN_NAME];
1937 chain = nft_chain_lookup(net, table, attr, genmask);
1938 }
1939 if (IS_ERR(chain)) {
1940 NL_SET_BAD_ATTR(extack, attr);
1941 return PTR_ERR(chain);
1942 }
1943
1944 if (nlh->nlmsg_flags & NLM_F_NONREC &&
1945 chain->use > 0)
1946 return -EBUSY;
1947
1948 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
1949
1950 use = chain->use;
1951 list_for_each_entry(rule, &chain->rules, list) {
1952 if (!nft_is_active_next(net, rule))
1953 continue;
1954 use--;
1955
1956 err = nft_delrule(&ctx, rule);
1957 if (err < 0)
1958 return err;
1959 }
1960
1961 /* There are rules and elements that are still holding references to us,
1962 * we cannot do a recursive removal in this case.
1963 */
1964 if (use > 0) {
1965 NL_SET_BAD_ATTR(extack, attr);
1966 return -EBUSY;
1967 }
1968
1969 return nft_delchain(&ctx);
1970 }
1971
1972 /*
1973 * Expressions
1974 */
1975
1976 /**
1977 * nft_register_expr - register nf_tables expr type
1978 * @ops: expr type
1979 *
1980 * Registers the expr type for use with nf_tables. Returns zero on
1981 * success or a negative errno code otherwise.
1982 */
1983 int nft_register_expr(struct nft_expr_type *type)
1984 {
1985 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1986 if (type->family == NFPROTO_UNSPEC)
1987 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1988 else
1989 list_add_rcu(&type->list, &nf_tables_expressions);
1990 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1991 return 0;
1992 }
1993 EXPORT_SYMBOL_GPL(nft_register_expr);
1994
1995 /**
1996 * nft_unregister_expr - unregister nf_tables expr type
1997 * @ops: expr type
1998 *
1999 * Unregisters the expr typefor use with nf_tables.
2000 */
2001 void nft_unregister_expr(struct nft_expr_type *type)
2002 {
2003 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2004 list_del_rcu(&type->list);
2005 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2006 }
2007 EXPORT_SYMBOL_GPL(nft_unregister_expr);
2008
2009 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
2010 struct nlattr *nla)
2011 {
2012 const struct nft_expr_type *type, *candidate = NULL;
2013
2014 list_for_each_entry(type, &nf_tables_expressions, list) {
2015 if (!nla_strcmp(nla, type->name)) {
2016 if (!type->family && !candidate)
2017 candidate = type;
2018 else if (type->family == family)
2019 candidate = type;
2020 }
2021 }
2022 return candidate;
2023 }
2024
2025 #ifdef CONFIG_MODULES
2026 static int nft_expr_type_request_module(struct net *net, u8 family,
2027 struct nlattr *nla)
2028 {
2029 nft_request_module(net, "nft-expr-%u-%.*s", family,
2030 nla_len(nla), (char *)nla_data(nla));
2031 if (__nft_expr_type_get(family, nla))
2032 return -EAGAIN;
2033
2034 return 0;
2035 }
2036 #endif
2037
2038 static const struct nft_expr_type *nft_expr_type_get(struct net *net,
2039 u8 family,
2040 struct nlattr *nla)
2041 {
2042 const struct nft_expr_type *type;
2043
2044 if (nla == NULL)
2045 return ERR_PTR(-EINVAL);
2046
2047 type = __nft_expr_type_get(family, nla);
2048 if (type != NULL && try_module_get(type->owner))
2049 return type;
2050
2051 lockdep_nfnl_nft_mutex_not_held();
2052 #ifdef CONFIG_MODULES
2053 if (type == NULL) {
2054 if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
2055 return ERR_PTR(-EAGAIN);
2056
2057 nft_request_module(net, "nft-expr-%.*s",
2058 nla_len(nla), (char *)nla_data(nla));
2059 if (__nft_expr_type_get(family, nla))
2060 return ERR_PTR(-EAGAIN);
2061 }
2062 #endif
2063 return ERR_PTR(-ENOENT);
2064 }
2065
2066 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
2067 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
2068 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
2069 };
2070
2071 static int nf_tables_fill_expr_info(struct sk_buff *skb,
2072 const struct nft_expr *expr)
2073 {
2074 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
2075 goto nla_put_failure;
2076
2077 if (expr->ops->dump) {
2078 struct nlattr *data = nla_nest_start_noflag(skb,
2079 NFTA_EXPR_DATA);
2080 if (data == NULL)
2081 goto nla_put_failure;
2082 if (expr->ops->dump(skb, expr) < 0)
2083 goto nla_put_failure;
2084 nla_nest_end(skb, data);
2085 }
2086
2087 return skb->len;
2088
2089 nla_put_failure:
2090 return -1;
2091 };
2092
2093 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
2094 const struct nft_expr *expr)
2095 {
2096 struct nlattr *nest;
2097
2098 nest = nla_nest_start_noflag(skb, attr);
2099 if (!nest)
2100 goto nla_put_failure;
2101 if (nf_tables_fill_expr_info(skb, expr) < 0)
2102 goto nla_put_failure;
2103 nla_nest_end(skb, nest);
2104 return 0;
2105
2106 nla_put_failure:
2107 return -1;
2108 }
2109
2110 struct nft_expr_info {
2111 const struct nft_expr_ops *ops;
2112 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
2113 };
2114
2115 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
2116 const struct nlattr *nla,
2117 struct nft_expr_info *info)
2118 {
2119 const struct nft_expr_type *type;
2120 const struct nft_expr_ops *ops;
2121 struct nlattr *tb[NFTA_EXPR_MAX + 1];
2122 int err;
2123
2124 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
2125 nft_expr_policy, NULL);
2126 if (err < 0)
2127 return err;
2128
2129 type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
2130 if (IS_ERR(type))
2131 return PTR_ERR(type);
2132
2133 if (tb[NFTA_EXPR_DATA]) {
2134 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
2135 tb[NFTA_EXPR_DATA],
2136 type->policy, NULL);
2137 if (err < 0)
2138 goto err1;
2139 } else
2140 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
2141
2142 if (type->select_ops != NULL) {
2143 ops = type->select_ops(ctx,
2144 (const struct nlattr * const *)info->tb);
2145 if (IS_ERR(ops)) {
2146 err = PTR_ERR(ops);
2147 #ifdef CONFIG_MODULES
2148 if (err == -EAGAIN)
2149 nft_expr_type_request_module(ctx->net,
2150 ctx->family,
2151 tb[NFTA_EXPR_NAME]);
2152 #endif
2153 goto err1;
2154 }
2155 } else
2156 ops = type->ops;
2157
2158 info->ops = ops;
2159 return 0;
2160
2161 err1:
2162 module_put(type->owner);
2163 return err;
2164 }
2165
2166 static int nf_tables_newexpr(const struct nft_ctx *ctx,
2167 const struct nft_expr_info *info,
2168 struct nft_expr *expr)
2169 {
2170 const struct nft_expr_ops *ops = info->ops;
2171 int err;
2172
2173 expr->ops = ops;
2174 if (ops->init) {
2175 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
2176 if (err < 0)
2177 goto err1;
2178 }
2179
2180 return 0;
2181 err1:
2182 expr->ops = NULL;
2183 return err;
2184 }
2185
2186 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
2187 struct nft_expr *expr)
2188 {
2189 const struct nft_expr_type *type = expr->ops->type;
2190
2191 if (expr->ops->destroy)
2192 expr->ops->destroy(ctx, expr);
2193 module_put(type->owner);
2194 }
2195
2196 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
2197 const struct nlattr *nla)
2198 {
2199 struct nft_expr_info info;
2200 struct nft_expr *expr;
2201 struct module *owner;
2202 int err;
2203
2204 err = nf_tables_expr_parse(ctx, nla, &info);
2205 if (err < 0)
2206 goto err1;
2207
2208 err = -ENOMEM;
2209 expr = kzalloc(info.ops->size, GFP_KERNEL);
2210 if (expr == NULL)
2211 goto err2;
2212
2213 err = nf_tables_newexpr(ctx, &info, expr);
2214 if (err < 0)
2215 goto err3;
2216
2217 return expr;
2218 err3:
2219 kfree(expr);
2220 err2:
2221 owner = info.ops->type->owner;
2222 if (info.ops->type->release_ops)
2223 info.ops->type->release_ops(info.ops);
2224
2225 module_put(owner);
2226 err1:
2227 return ERR_PTR(err);
2228 }
2229
2230 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
2231 {
2232 nf_tables_expr_destroy(ctx, expr);
2233 kfree(expr);
2234 }
2235
2236 /*
2237 * Rules
2238 */
2239
2240 static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
2241 u64 handle)
2242 {
2243 struct nft_rule *rule;
2244
2245 // FIXME: this sucks
2246 list_for_each_entry_rcu(rule, &chain->rules, list) {
2247 if (handle == rule->handle)
2248 return rule;
2249 }
2250
2251 return ERR_PTR(-ENOENT);
2252 }
2253
2254 static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
2255 const struct nlattr *nla)
2256 {
2257 if (nla == NULL)
2258 return ERR_PTR(-EINVAL);
2259
2260 return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
2261 }
2262
2263 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
2264 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
2265 .len = NFT_TABLE_MAXNAMELEN - 1 },
2266 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
2267 .len = NFT_CHAIN_MAXNAMELEN - 1 },
2268 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
2269 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
2270 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
2271 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
2272 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
2273 .len = NFT_USERDATA_MAXLEN },
2274 [NFTA_RULE_ID] = { .type = NLA_U32 },
2275 [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
2276 };
2277
2278 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
2279 u32 portid, u32 seq, int event,
2280 u32 flags, int family,
2281 const struct nft_table *table,
2282 const struct nft_chain *chain,
2283 const struct nft_rule *rule,
2284 const struct nft_rule *prule)
2285 {
2286 struct nlmsghdr *nlh;
2287 struct nfgenmsg *nfmsg;
2288 const struct nft_expr *expr, *next;
2289 struct nlattr *list;
2290 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
2291
2292 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
2293 if (nlh == NULL)
2294 goto nla_put_failure;
2295
2296 nfmsg = nlmsg_data(nlh);
2297 nfmsg->nfgen_family = family;
2298 nfmsg->version = NFNETLINK_V0;
2299 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
2300
2301 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
2302 goto nla_put_failure;
2303 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
2304 goto nla_put_failure;
2305 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
2306 NFTA_RULE_PAD))
2307 goto nla_put_failure;
2308
2309 if (event != NFT_MSG_DELRULE && prule) {
2310 if (nla_put_be64(skb, NFTA_RULE_POSITION,
2311 cpu_to_be64(prule->handle),
2312 NFTA_RULE_PAD))
2313 goto nla_put_failure;
2314 }
2315
2316 list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
2317 if (list == NULL)
2318 goto nla_put_failure;
2319 nft_rule_for_each_expr(expr, next, rule) {
2320 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
2321 goto nla_put_failure;
2322 }
2323 nla_nest_end(skb, list);
2324
2325 if (rule->udata) {
2326 struct nft_userdata *udata = nft_userdata(rule);
2327 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
2328 udata->data) < 0)
2329 goto nla_put_failure;
2330 }
2331
2332 nlmsg_end(skb, nlh);
2333 return 0;
2334
2335 nla_put_failure:
2336 nlmsg_trim(skb, nlh);
2337 return -1;
2338 }
2339
2340 static void nf_tables_rule_notify(const struct nft_ctx *ctx,
2341 const struct nft_rule *rule, int event)
2342 {
2343 struct sk_buff *skb;
2344 int err;
2345
2346 if (!ctx->report &&
2347 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2348 return;
2349
2350 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2351 if (skb == NULL)
2352 goto err;
2353
2354 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
2355 event, 0, ctx->family, ctx->table,
2356 ctx->chain, rule, NULL);
2357 if (err < 0) {
2358 kfree_skb(skb);
2359 goto err;
2360 }
2361
2362 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
2363 ctx->report, GFP_KERNEL);
2364 return;
2365 err:
2366 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
2367 }
2368
2369 struct nft_rule_dump_ctx {
2370 char *table;
2371 char *chain;
2372 };
2373
2374 static int __nf_tables_dump_rules(struct sk_buff *skb,
2375 unsigned int *idx,
2376 struct netlink_callback *cb,
2377 const struct nft_table *table,
2378 const struct nft_chain *chain)
2379 {
2380 struct net *net = sock_net(skb->sk);
2381 const struct nft_rule *rule, *prule;
2382 unsigned int s_idx = cb->args[0];
2383
2384 prule = NULL;
2385 list_for_each_entry_rcu(rule, &chain->rules, list) {
2386 if (!nft_is_active(net, rule))
2387 goto cont_skip;
2388 if (*idx < s_idx)
2389 goto cont;
2390 if (*idx > s_idx) {
2391 memset(&cb->args[1], 0,
2392 sizeof(cb->args) - sizeof(cb->args[0]));
2393 }
2394 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
2395 cb->nlh->nlmsg_seq,
2396 NFT_MSG_NEWRULE,
2397 NLM_F_MULTI | NLM_F_APPEND,
2398 table->family,
2399 table, chain, rule, prule) < 0)
2400 return 1;
2401
2402 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2403 cont:
2404 prule = rule;
2405 cont_skip:
2406 (*idx)++;
2407 }
2408 return 0;
2409 }
2410
2411 static int nf_tables_dump_rules(struct sk_buff *skb,
2412 struct netlink_callback *cb)
2413 {
2414 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2415 const struct nft_rule_dump_ctx *ctx = cb->data;
2416 struct nft_table *table;
2417 const struct nft_chain *chain;
2418 unsigned int idx = 0;
2419 struct net *net = sock_net(skb->sk);
2420 int family = nfmsg->nfgen_family;
2421
2422 rcu_read_lock();
2423 cb->seq = net->nft.base_seq;
2424
2425 list_for_each_entry_rcu(table, &net->nft.tables, list) {
2426 if (family != NFPROTO_UNSPEC && family != table->family)
2427 continue;
2428
2429 if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
2430 continue;
2431
2432 if (ctx && ctx->table && ctx->chain) {
2433 struct rhlist_head *list, *tmp;
2434
2435 list = rhltable_lookup(&table->chains_ht, ctx->chain,
2436 nft_chain_ht_params);
2437 if (!list)
2438 goto done;
2439
2440 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
2441 if (!nft_is_active(net, chain))
2442 continue;
2443 __nf_tables_dump_rules(skb, &idx,
2444 cb, table, chain);
2445 break;
2446 }
2447 goto done;
2448 }
2449
2450 list_for_each_entry_rcu(chain, &table->chains, list) {
2451 if (__nf_tables_dump_rules(skb, &idx, cb, table, chain))
2452 goto done;
2453 }
2454
2455 if (ctx && ctx->table)
2456 break;
2457 }
2458 done:
2459 rcu_read_unlock();
2460
2461 cb->args[0] = idx;
2462 return skb->len;
2463 }
2464
2465 static int nf_tables_dump_rules_start(struct netlink_callback *cb)
2466 {
2467 const struct nlattr * const *nla = cb->data;
2468 struct nft_rule_dump_ctx *ctx = NULL;
2469
2470 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
2471 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
2472 if (!ctx)
2473 return -ENOMEM;
2474
2475 if (nla[NFTA_RULE_TABLE]) {
2476 ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
2477 GFP_ATOMIC);
2478 if (!ctx->table) {
2479 kfree(ctx);
2480 return -ENOMEM;
2481 }
2482 }
2483 if (nla[NFTA_RULE_CHAIN]) {
2484 ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
2485 GFP_ATOMIC);
2486 if (!ctx->chain) {
2487 kfree(ctx->table);
2488 kfree(ctx);
2489 return -ENOMEM;
2490 }
2491 }
2492 }
2493
2494 cb->data = ctx;
2495 return 0;
2496 }
2497
2498 static int nf_tables_dump_rules_done(struct netlink_callback *cb)
2499 {
2500 struct nft_rule_dump_ctx *ctx = cb->data;
2501
2502 if (ctx) {
2503 kfree(ctx->table);
2504 kfree(ctx->chain);
2505 kfree(ctx);
2506 }
2507 return 0;
2508 }
2509
2510 /* called with rcu_read_lock held */
2511 static int nf_tables_getrule(struct net *net, struct sock *nlsk,
2512 struct sk_buff *skb, const struct nlmsghdr *nlh,
2513 const struct nlattr * const nla[],
2514 struct netlink_ext_ack *extack)
2515 {
2516 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2517 u8 genmask = nft_genmask_cur(net);
2518 const struct nft_chain *chain;
2519 const struct nft_rule *rule;
2520 struct nft_table *table;
2521 struct sk_buff *skb2;
2522 int family = nfmsg->nfgen_family;
2523 int err;
2524
2525 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2526 struct netlink_dump_control c = {
2527 .start= nf_tables_dump_rules_start,
2528 .dump = nf_tables_dump_rules,
2529 .done = nf_tables_dump_rules_done,
2530 .module = THIS_MODULE,
2531 .data = (void *)nla,
2532 };
2533
2534 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
2535 }
2536
2537 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
2538 if (IS_ERR(table)) {
2539 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
2540 return PTR_ERR(table);
2541 }
2542
2543 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
2544 if (IS_ERR(chain)) {
2545 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
2546 return PTR_ERR(chain);
2547 }
2548
2549 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
2550 if (IS_ERR(rule)) {
2551 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2552 return PTR_ERR(rule);
2553 }
2554
2555 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
2556 if (!skb2)
2557 return -ENOMEM;
2558
2559 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
2560 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
2561 family, table, chain, rule, NULL);
2562 if (err < 0)
2563 goto err;
2564
2565 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2566
2567 err:
2568 kfree_skb(skb2);
2569 return err;
2570 }
2571
2572 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
2573 struct nft_rule *rule)
2574 {
2575 struct nft_expr *expr, *next;
2576
2577 /*
2578 * Careful: some expressions might not be initialized in case this
2579 * is called on error from nf_tables_newrule().
2580 */
2581 expr = nft_expr_first(rule);
2582 while (expr != nft_expr_last(rule) && expr->ops) {
2583 next = nft_expr_next(expr);
2584 nf_tables_expr_destroy(ctx, expr);
2585 expr = next;
2586 }
2587 kfree(rule);
2588 }
2589
2590 static void nf_tables_rule_release(const struct nft_ctx *ctx,
2591 struct nft_rule *rule)
2592 {
2593 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
2594 nf_tables_rule_destroy(ctx, rule);
2595 }
2596
2597 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
2598 {
2599 struct nft_expr *expr, *last;
2600 const struct nft_data *data;
2601 struct nft_rule *rule;
2602 int err;
2603
2604 if (ctx->level == NFT_JUMP_STACK_SIZE)
2605 return -EMLINK;
2606
2607 list_for_each_entry(rule, &chain->rules, list) {
2608 if (!nft_is_active_next(ctx->net, rule))
2609 continue;
2610
2611 nft_rule_for_each_expr(expr, last, rule) {
2612 if (!expr->ops->validate)
2613 continue;
2614
2615 err = expr->ops->validate(ctx, expr, &data);
2616 if (err < 0)
2617 return err;
2618 }
2619 }
2620
2621 return 0;
2622 }
2623 EXPORT_SYMBOL_GPL(nft_chain_validate);
2624
2625 static int nft_table_validate(struct net *net, const struct nft_table *table)
2626 {
2627 struct nft_chain *chain;
2628 struct nft_ctx ctx = {
2629 .net = net,
2630 .family = table->family,
2631 };
2632 int err;
2633
2634 list_for_each_entry(chain, &table->chains, list) {
2635 if (!nft_is_base_chain(chain))
2636 continue;
2637
2638 ctx.chain = chain;
2639 err = nft_chain_validate(&ctx, chain);
2640 if (err < 0)
2641 return err;
2642 }
2643
2644 return 0;
2645 }
2646
2647 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
2648 const struct nlattr *nla);
2649
2650 #define NFT_RULE_MAXEXPRS 128
2651
2652 static int nf_tables_newrule(struct net *net, struct sock *nlsk,
2653 struct sk_buff *skb, const struct nlmsghdr *nlh,
2654 const struct nlattr * const nla[],
2655 struct netlink_ext_ack *extack)
2656 {
2657 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2658 u8 genmask = nft_genmask_next(net);
2659 struct nft_expr_info *info = NULL;
2660 int family = nfmsg->nfgen_family;
2661 struct nft_table *table;
2662 struct nft_chain *chain;
2663 struct nft_rule *rule, *old_rule = NULL;
2664 struct nft_userdata *udata;
2665 struct nft_trans *trans = NULL;
2666 struct nft_expr *expr;
2667 struct nft_ctx ctx;
2668 struct nlattr *tmp;
2669 unsigned int size, i, n, ulen = 0, usize = 0;
2670 int err, rem;
2671 u64 handle, pos_handle;
2672
2673 lockdep_assert_held(&net->nft.commit_mutex);
2674
2675 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
2676 if (IS_ERR(table)) {
2677 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
2678 return PTR_ERR(table);
2679 }
2680
2681 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
2682 if (IS_ERR(chain)) {
2683 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
2684 return PTR_ERR(chain);
2685 }
2686
2687 if (nla[NFTA_RULE_HANDLE]) {
2688 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2689 rule = __nft_rule_lookup(chain, handle);
2690 if (IS_ERR(rule)) {
2691 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2692 return PTR_ERR(rule);
2693 }
2694
2695 if (nlh->nlmsg_flags & NLM_F_EXCL) {
2696 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2697 return -EEXIST;
2698 }
2699 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2700 old_rule = rule;
2701 else
2702 return -EOPNOTSUPP;
2703 } else {
2704 if (!(nlh->nlmsg_flags & NLM_F_CREATE) ||
2705 nlh->nlmsg_flags & NLM_F_REPLACE)
2706 return -EINVAL;
2707 handle = nf_tables_alloc_handle(table);
2708
2709 if (chain->use == UINT_MAX)
2710 return -EOVERFLOW;
2711
2712 if (nla[NFTA_RULE_POSITION]) {
2713 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2714 old_rule = __nft_rule_lookup(chain, pos_handle);
2715 if (IS_ERR(old_rule)) {
2716 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
2717 return PTR_ERR(old_rule);
2718 }
2719 } else if (nla[NFTA_RULE_POSITION_ID]) {
2720 old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]);
2721 if (IS_ERR(old_rule)) {
2722 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
2723 return PTR_ERR(old_rule);
2724 }
2725 }
2726 }
2727
2728 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
2729
2730 n = 0;
2731 size = 0;
2732 if (nla[NFTA_RULE_EXPRESSIONS]) {
2733 info = kvmalloc_array(NFT_RULE_MAXEXPRS,
2734 sizeof(struct nft_expr_info),
2735 GFP_KERNEL);
2736 if (!info)
2737 return -ENOMEM;
2738
2739 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2740 err = -EINVAL;
2741 if (nla_type(tmp) != NFTA_LIST_ELEM)
2742 goto err1;
2743 if (n == NFT_RULE_MAXEXPRS)
2744 goto err1;
2745 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
2746 if (err < 0)
2747 goto err1;
2748 size += info[n].ops->size;
2749 n++;
2750 }
2751 }
2752 /* Check for overflow of dlen field */
2753 err = -EFBIG;
2754 if (size >= 1 << 12)
2755 goto err1;
2756
2757 if (nla[NFTA_RULE_USERDATA]) {
2758 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
2759 if (ulen > 0)
2760 usize = sizeof(struct nft_userdata) + ulen;
2761 }
2762
2763 err = -ENOMEM;
2764 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
2765 if (rule == NULL)
2766 goto err1;
2767
2768 nft_activate_next(net, rule);
2769
2770 rule->handle = handle;
2771 rule->dlen = size;
2772 rule->udata = ulen ? 1 : 0;
2773
2774 if (ulen) {
2775 udata = nft_userdata(rule);
2776 udata->len = ulen - 1;
2777 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2778 }
2779
2780 expr = nft_expr_first(rule);
2781 for (i = 0; i < n; i++) {
2782 err = nf_tables_newexpr(&ctx, &info[i], expr);
2783 if (err < 0)
2784 goto err2;
2785
2786 if (info[i].ops->validate)
2787 nft_validate_state_update(net, NFT_VALIDATE_NEED);
2788
2789 info[i].ops = NULL;
2790 expr = nft_expr_next(expr);
2791 }
2792
2793 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
2794 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
2795 if (trans == NULL) {
2796 err = -ENOMEM;
2797 goto err2;
2798 }
2799 err = nft_delrule(&ctx, old_rule);
2800 if (err < 0) {
2801 nft_trans_destroy(trans);
2802 goto err2;
2803 }
2804
2805 list_add_tail_rcu(&rule->list, &old_rule->list);
2806 } else {
2807 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2808 err = -ENOMEM;
2809 goto err2;
2810 }
2811
2812 if (nlh->nlmsg_flags & NLM_F_APPEND) {
2813 if (old_rule)
2814 list_add_rcu(&rule->list, &old_rule->list);
2815 else
2816 list_add_tail_rcu(&rule->list, &chain->rules);
2817 } else {
2818 if (old_rule)
2819 list_add_tail_rcu(&rule->list, &old_rule->list);
2820 else
2821 list_add_rcu(&rule->list, &chain->rules);
2822 }
2823 }
2824 kvfree(info);
2825 chain->use++;
2826
2827 if (net->nft.validate_state == NFT_VALIDATE_DO)
2828 return nft_table_validate(net, table);
2829
2830 return 0;
2831 err2:
2832 nf_tables_rule_release(&ctx, rule);
2833 err1:
2834 for (i = 0; i < n; i++) {
2835 if (info[i].ops) {
2836 module_put(info[i].ops->type->owner);
2837 if (info[i].ops->type->release_ops)
2838 info[i].ops->type->release_ops(info[i].ops);
2839 }
2840 }
2841 kvfree(info);
2842 return err;
2843 }
2844
2845 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
2846 const struct nlattr *nla)
2847 {
2848 u32 id = ntohl(nla_get_be32(nla));
2849 struct nft_trans *trans;
2850
2851 list_for_each_entry(trans, &net->nft.commit_list, list) {
2852 struct nft_rule *rule = nft_trans_rule(trans);
2853
2854 if (trans->msg_type == NFT_MSG_NEWRULE &&
2855 id == nft_trans_rule_id(trans))
2856 return rule;
2857 }
2858 return ERR_PTR(-ENOENT);
2859 }
2860
2861 static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2862 struct sk_buff *skb, const struct nlmsghdr *nlh,
2863 const struct nlattr * const nla[],
2864 struct netlink_ext_ack *extack)
2865 {
2866 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2867 u8 genmask = nft_genmask_next(net);
2868 struct nft_table *table;
2869 struct nft_chain *chain = NULL;
2870 struct nft_rule *rule;
2871 int family = nfmsg->nfgen_family, err = 0;
2872 struct nft_ctx ctx;
2873
2874 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
2875 if (IS_ERR(table)) {
2876 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
2877 return PTR_ERR(table);
2878 }
2879
2880 if (nla[NFTA_RULE_CHAIN]) {
2881 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
2882 genmask);
2883 if (IS_ERR(chain)) {
2884 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
2885 return PTR_ERR(chain);
2886 }
2887 }
2888
2889 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
2890
2891 if (chain) {
2892 if (nla[NFTA_RULE_HANDLE]) {
2893 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
2894 if (IS_ERR(rule)) {
2895 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2896 return PTR_ERR(rule);
2897 }
2898
2899 err = nft_delrule(&ctx, rule);
2900 } else if (nla[NFTA_RULE_ID]) {
2901 rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]);
2902 if (IS_ERR(rule)) {
2903 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
2904 return PTR_ERR(rule);
2905 }
2906
2907 err = nft_delrule(&ctx, rule);
2908 } else {
2909 err = nft_delrule_by_chain(&ctx);
2910 }
2911 } else {
2912 list_for_each_entry(chain, &table->chains, list) {
2913 if (!nft_is_active_next(net, chain))
2914 continue;
2915
2916 ctx.chain = chain;
2917 err = nft_delrule_by_chain(&ctx);
2918 if (err < 0)
2919 break;
2920 }
2921 }
2922
2923 return err;
2924 }
2925
2926 /*
2927 * Sets
2928 */
2929
2930 static LIST_HEAD(nf_tables_set_types);
2931
2932 int nft_register_set(struct nft_set_type *type)
2933 {
2934 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2935 list_add_tail_rcu(&type->list, &nf_tables_set_types);
2936 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2937 return 0;
2938 }
2939 EXPORT_SYMBOL_GPL(nft_register_set);
2940
2941 void nft_unregister_set(struct nft_set_type *type)
2942 {
2943 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2944 list_del_rcu(&type->list);
2945 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2946 }
2947 EXPORT_SYMBOL_GPL(nft_unregister_set);
2948
2949 #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
2950 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
2951 NFT_SET_EVAL)
2952
2953 static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
2954 {
2955 return (flags & type->features) == (flags & NFT_SET_FEATURES);
2956 }
2957
2958 /*
2959 * Select a set implementation based on the data characteristics and the
2960 * given policy. The total memory use might not be known if no size is
2961 * given, in that case the amount of memory per element is used.
2962 */
2963 static const struct nft_set_ops *
2964 nft_select_set_ops(const struct nft_ctx *ctx,
2965 const struct nlattr * const nla[],
2966 const struct nft_set_desc *desc,
2967 enum nft_set_policies policy)
2968 {
2969 const struct nft_set_ops *ops, *bops;
2970 struct nft_set_estimate est, best;
2971 const struct nft_set_type *type;
2972 u32 flags = 0;
2973
2974 lockdep_assert_held(&ctx->net->nft.commit_mutex);
2975 lockdep_nfnl_nft_mutex_not_held();
2976 #ifdef CONFIG_MODULES
2977 if (list_empty(&nf_tables_set_types)) {
2978 nft_request_module(ctx->net, "nft-set");
2979 if (!list_empty(&nf_tables_set_types))
2980 return ERR_PTR(-EAGAIN);
2981 }
2982 #endif
2983 if (nla[NFTA_SET_FLAGS] != NULL)
2984 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2985
2986 bops = NULL;
2987 best.size = ~0;
2988 best.lookup = ~0;
2989 best.space = ~0;
2990
2991 list_for_each_entry(type, &nf_tables_set_types, list) {
2992 ops = &type->ops;
2993
2994 if (!nft_set_ops_candidate(type, flags))
2995 continue;
2996 if (!ops->estimate(desc, flags, &est))
2997 continue;
2998
2999 switch (policy) {
3000 case NFT_SET_POL_PERFORMANCE:
3001 if (est.lookup < best.lookup)
3002 break;
3003 if (est.lookup == best.lookup &&
3004 est.space < best.space)
3005 break;
3006 continue;
3007 case NFT_SET_POL_MEMORY:
3008 if (!desc->size) {
3009 if (est.space < best.space)
3010 break;
3011 if (est.space == best.space &&
3012 est.lookup < best.lookup)
3013 break;
3014 } else if (est.size < best.size || !bops) {
3015 break;
3016 }
3017 continue;
3018 default:
3019 break;
3020 }
3021
3022 if (!try_module_get(type->owner))
3023 continue;
3024 if (bops != NULL)
3025 module_put(to_set_type(bops)->owner);
3026
3027 bops = ops;
3028 best = est;
3029 }
3030
3031 if (bops != NULL)
3032 return bops;
3033
3034 return ERR_PTR(-EOPNOTSUPP);
3035 }
3036
3037 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
3038 [NFTA_SET_TABLE] = { .type = NLA_STRING,
3039 .len = NFT_TABLE_MAXNAMELEN - 1 },
3040 [NFTA_SET_NAME] = { .type = NLA_STRING,
3041 .len = NFT_SET_MAXNAMELEN - 1 },
3042 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
3043 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
3044 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
3045 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
3046 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
3047 [NFTA_SET_POLICY] = { .type = NLA_U32 },
3048 [NFTA_SET_DESC] = { .type = NLA_NESTED },
3049 [NFTA_SET_ID] = { .type = NLA_U32 },
3050 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
3051 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
3052 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
3053 .len = NFT_USERDATA_MAXLEN },
3054 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
3055 [NFTA_SET_HANDLE] = { .type = NLA_U64 },
3056 };
3057
3058 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
3059 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
3060 };
3061
3062 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
3063 const struct sk_buff *skb,
3064 const struct nlmsghdr *nlh,
3065 const struct nlattr * const nla[],
3066 struct netlink_ext_ack *extack,
3067 u8 genmask)
3068 {
3069 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3070 int family = nfmsg->nfgen_family;
3071 struct nft_table *table = NULL;
3072
3073 if (nla[NFTA_SET_TABLE] != NULL) {
3074 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
3075 genmask);
3076 if (IS_ERR(table)) {
3077 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
3078 return PTR_ERR(table);
3079 }
3080 }
3081
3082 nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
3083 return 0;
3084 }
3085
3086 static struct nft_set *nft_set_lookup(const struct nft_table *table,
3087 const struct nlattr *nla, u8 genmask)
3088 {
3089 struct nft_set *set;
3090
3091 if (nla == NULL)
3092 return ERR_PTR(-EINVAL);
3093
3094 list_for_each_entry_rcu(set, &table->sets, list) {
3095 if (!nla_strcmp(nla, set->name) &&
3096 nft_active_genmask(set, genmask))
3097 return set;
3098 }
3099 return ERR_PTR(-ENOENT);
3100 }
3101
3102 static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
3103 const struct nlattr *nla,
3104 u8 genmask)
3105 {
3106 struct nft_set *set;
3107
3108 list_for_each_entry(set, &table->sets, list) {
3109 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
3110 nft_active_genmask(set, genmask))
3111 return set;
3112 }
3113 return ERR_PTR(-ENOENT);
3114 }
3115
3116 static struct nft_set *nft_set_lookup_byid(const struct net *net,
3117 const struct nlattr *nla, u8 genmask)
3118 {
3119 struct nft_trans *trans;
3120 u32 id = ntohl(nla_get_be32(nla));
3121
3122 list_for_each_entry(trans, &net->nft.commit_list, list) {
3123 if (trans->msg_type == NFT_MSG_NEWSET) {
3124 struct nft_set *set = nft_trans_set(trans);
3125
3126 if (id == nft_trans_set_id(trans) &&
3127 nft_active_genmask(set, genmask))
3128 return set;
3129 }
3130 }
3131 return ERR_PTR(-ENOENT);
3132 }
3133
3134 struct nft_set *nft_set_lookup_global(const struct net *net,
3135 const struct nft_table *table,
3136 const struct nlattr *nla_set_name,
3137 const struct nlattr *nla_set_id,
3138 u8 genmask)
3139 {
3140 struct nft_set *set;
3141
3142 set = nft_set_lookup(table, nla_set_name, genmask);
3143 if (IS_ERR(set)) {
3144 if (!nla_set_id)
3145 return set;
3146
3147 set = nft_set_lookup_byid(net, nla_set_id, genmask);
3148 }
3149 return set;
3150 }
3151 EXPORT_SYMBOL_GPL(nft_set_lookup_global);
3152
3153 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
3154 const char *name)
3155 {
3156 const struct nft_set *i;
3157 const char *p;
3158 unsigned long *inuse;
3159 unsigned int n = 0, min = 0;
3160
3161 p = strchr(name, '%');
3162 if (p != NULL) {
3163 if (p[1] != 'd' || strchr(p + 2, '%'))
3164 return -EINVAL;
3165
3166 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
3167 if (inuse == NULL)
3168 return -ENOMEM;
3169 cont:
3170 list_for_each_entry(i, &ctx->table->sets, list) {
3171 int tmp;
3172
3173 if (!nft_is_active_next(ctx->net, set))
3174 continue;
3175 if (!sscanf(i->name, name, &tmp))
3176 continue;
3177 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
3178 continue;
3179
3180 set_bit(tmp - min, inuse);
3181 }
3182
3183 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
3184 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
3185 min += BITS_PER_BYTE * PAGE_SIZE;
3186 memset(inuse, 0, PAGE_SIZE);
3187 goto cont;
3188 }
3189 free_page((unsigned long)inuse);
3190 }
3191
3192 set->name = kasprintf(GFP_KERNEL, name, min + n);
3193 if (!set->name)
3194 return -ENOMEM;
3195
3196 list_for_each_entry(i, &ctx->table->sets, list) {
3197 if (!nft_is_active_next(ctx->net, i))
3198 continue;
3199 if (!strcmp(set->name, i->name)) {
3200 kfree(set->name);
3201 return -ENFILE;
3202 }
3203 }
3204 return 0;
3205 }
3206
3207 static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
3208 {
3209 u64 ms = be64_to_cpu(nla_get_be64(nla));
3210 u64 max = (u64)(~((u64)0));
3211
3212 max = div_u64(max, NSEC_PER_MSEC);
3213 if (ms >= max)
3214 return -ERANGE;
3215
3216 ms *= NSEC_PER_MSEC;
3217 *result = nsecs_to_jiffies64(ms);
3218 return 0;
3219 }
3220
3221 static __be64 nf_jiffies64_to_msecs(u64 input)
3222 {
3223 return cpu_to_be64(jiffies64_to_msecs(input));
3224 }
3225
3226 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
3227 const struct nft_set *set, u16 event, u16 flags)
3228 {
3229 struct nfgenmsg *nfmsg;
3230 struct nlmsghdr *nlh;
3231 struct nlattr *desc;
3232 u32 portid = ctx->portid;
3233 u32 seq = ctx->seq;
3234
3235 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
3236 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3237 flags);
3238 if (nlh == NULL)
3239 goto nla_put_failure;
3240
3241 nfmsg = nlmsg_data(nlh);
3242 nfmsg->nfgen_family = ctx->family;
3243 nfmsg->version = NFNETLINK_V0;
3244 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
3245
3246 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3247 goto nla_put_failure;
3248 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3249 goto nla_put_failure;
3250 if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
3251 NFTA_SET_PAD))
3252 goto nla_put_failure;
3253 if (set->flags != 0)
3254 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
3255 goto nla_put_failure;
3256
3257 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
3258 goto nla_put_failure;
3259 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
3260 goto nla_put_failure;
3261 if (set->flags & NFT_SET_MAP) {
3262 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
3263 goto nla_put_failure;
3264 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
3265 goto nla_put_failure;
3266 }
3267 if (set->flags & NFT_SET_OBJECT &&
3268 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
3269 goto nla_put_failure;
3270
3271 if (set->timeout &&
3272 nla_put_be64(skb, NFTA_SET_TIMEOUT,
3273 nf_jiffies64_to_msecs(set->timeout),
3274 NFTA_SET_PAD))
3275 goto nla_put_failure;
3276 if (set->gc_int &&
3277 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
3278 goto nla_put_failure;
3279
3280 if (set->policy != NFT_SET_POL_PERFORMANCE) {
3281 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
3282 goto nla_put_failure;
3283 }
3284
3285 if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
3286 goto nla_put_failure;
3287
3288 desc = nla_nest_start_noflag(skb, NFTA_SET_DESC);
3289 if (desc == NULL)
3290 goto nla_put_failure;
3291 if (set->size &&
3292 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
3293 goto nla_put_failure;
3294 nla_nest_end(skb, desc);
3295
3296 nlmsg_end(skb, nlh);
3297 return 0;
3298
3299 nla_put_failure:
3300 nlmsg_trim(skb, nlh);
3301 return -1;
3302 }
3303
3304 static void nf_tables_set_notify(const struct nft_ctx *ctx,
3305 const struct nft_set *set, int event,
3306 gfp_t gfp_flags)
3307 {
3308 struct sk_buff *skb;
3309 u32 portid = ctx->portid;
3310 int err;
3311
3312 if (!ctx->report &&
3313 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
3314 return;
3315
3316 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
3317 if (skb == NULL)
3318 goto err;
3319
3320 err = nf_tables_fill_set(skb, ctx, set, event, 0);
3321 if (err < 0) {
3322 kfree_skb(skb);
3323 goto err;
3324 }
3325
3326 nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report,
3327 gfp_flags);
3328 return;
3329 err:
3330 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
3331 }
3332
3333 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
3334 {
3335 const struct nft_set *set;
3336 unsigned int idx, s_idx = cb->args[0];
3337 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
3338 struct net *net = sock_net(skb->sk);
3339 struct nft_ctx *ctx = cb->data, ctx_set;
3340
3341 if (cb->args[1])
3342 return skb->len;
3343
3344 rcu_read_lock();
3345 cb->seq = net->nft.base_seq;
3346
3347 list_for_each_entry_rcu(table, &net->nft.tables, list) {
3348 if (ctx->family != NFPROTO_UNSPEC &&
3349 ctx->family != table->family)
3350 continue;
3351
3352 if (ctx->table && ctx->table != table)
3353 continue;
3354
3355 if (cur_table) {
3356 if (cur_table != table)
3357 continue;
3358
3359 cur_table = NULL;
3360 }
3361 idx = 0;
3362 list_for_each_entry_rcu(set, &table->sets, list) {
3363 if (idx < s_idx)
3364 goto cont;
3365 if (!nft_is_active(net, set))
3366 goto cont;
3367
3368 ctx_set = *ctx;
3369 ctx_set.table = table;
3370 ctx_set.family = table->family;
3371
3372 if (nf_tables_fill_set(skb, &ctx_set, set,
3373 NFT_MSG_NEWSET,
3374 NLM_F_MULTI) < 0) {
3375 cb->args[0] = idx;
3376 cb->args[2] = (unsigned long) table;
3377 goto done;
3378 }
3379 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3380 cont:
3381 idx++;
3382 }
3383 if (s_idx)
3384 s_idx = 0;
3385 }
3386 cb->args[1] = 1;
3387 done:
3388 rcu_read_unlock();
3389 return skb->len;
3390 }
3391
3392 static int nf_tables_dump_sets_start(struct netlink_callback *cb)
3393 {
3394 struct nft_ctx *ctx_dump = NULL;
3395
3396 ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
3397 if (ctx_dump == NULL)
3398 return -ENOMEM;
3399
3400 cb->data = ctx_dump;
3401 return 0;
3402 }
3403
3404 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
3405 {
3406 kfree(cb->data);
3407 return 0;
3408 }
3409
3410 /* called with rcu_read_lock held */
3411 static int nf_tables_getset(struct net *net, struct sock *nlsk,
3412 struct sk_buff *skb, const struct nlmsghdr *nlh,
3413 const struct nlattr * const nla[],
3414 struct netlink_ext_ack *extack)
3415 {
3416 u8 genmask = nft_genmask_cur(net);
3417 const struct nft_set *set;
3418 struct nft_ctx ctx;
3419 struct sk_buff *skb2;
3420 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3421 int err;
3422
3423 /* Verify existence before starting dump */
3424 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
3425 genmask);
3426 if (err < 0)
3427 return err;
3428
3429 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3430 struct netlink_dump_control c = {
3431 .start = nf_tables_dump_sets_start,
3432 .dump = nf_tables_dump_sets,
3433 .done = nf_tables_dump_sets_done,
3434 .data = &ctx,
3435 .module = THIS_MODULE,
3436 };
3437
3438 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
3439 }
3440
3441 /* Only accept unspec with dump */
3442 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
3443 return -EAFNOSUPPORT;
3444 if (!nla[NFTA_SET_TABLE])
3445 return -EINVAL;
3446
3447 set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
3448 if (IS_ERR(set))
3449 return PTR_ERR(set);
3450
3451 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3452 if (skb2 == NULL)
3453 return -ENOMEM;
3454
3455 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
3456 if (err < 0)
3457 goto err;
3458
3459 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3460
3461 err:
3462 kfree_skb(skb2);
3463 return err;
3464 }
3465
3466 static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
3467 const struct nlattr *nla)
3468 {
3469 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
3470 int err;
3471
3472 err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
3473 nft_set_desc_policy, NULL);
3474 if (err < 0)
3475 return err;
3476
3477 if (da[NFTA_SET_DESC_SIZE] != NULL)
3478 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
3479
3480 return 0;
3481 }
3482
3483 static int nf_tables_newset(struct net *net, struct sock *nlsk,
3484 struct sk_buff *skb, const struct nlmsghdr *nlh,
3485 const struct nlattr * const nla[],
3486 struct netlink_ext_ack *extack)
3487 {
3488 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3489 u8 genmask = nft_genmask_next(net);
3490 int family = nfmsg->nfgen_family;
3491 const struct nft_set_ops *ops;
3492 struct nft_table *table;
3493 struct nft_set *set;
3494 struct nft_ctx ctx;
3495 char *name;
3496 u64 size;
3497 u64 timeout;
3498 u32 ktype, dtype, flags, policy, gc_int, objtype;
3499 struct nft_set_desc desc;
3500 unsigned char *udata;
3501 u16 udlen;
3502 int err;
3503
3504 if (nla[NFTA_SET_TABLE] == NULL ||
3505 nla[NFTA_SET_NAME] == NULL ||
3506 nla[NFTA_SET_KEY_LEN] == NULL ||
3507 nla[NFTA_SET_ID] == NULL)
3508 return -EINVAL;
3509
3510 memset(&desc, 0, sizeof(desc));
3511
3512 ktype = NFT_DATA_VALUE;
3513 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
3514 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
3515 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
3516 return -EINVAL;
3517 }
3518
3519 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
3520 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
3521 return -EINVAL;
3522
3523 flags = 0;
3524 if (nla[NFTA_SET_FLAGS] != NULL) {
3525 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
3526 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
3527 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
3528 NFT_SET_MAP | NFT_SET_EVAL |
3529 NFT_SET_OBJECT))
3530 return -EINVAL;
3531 /* Only one of these operations is supported */
3532 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) ==
3533 (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT))
3534 return -EOPNOTSUPP;
3535 }
3536
3537 dtype = 0;
3538 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
3539 if (!(flags & NFT_SET_MAP))
3540 return -EINVAL;
3541
3542 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
3543 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
3544 dtype != NFT_DATA_VERDICT)
3545 return -EINVAL;
3546
3547 if (dtype != NFT_DATA_VERDICT) {
3548 if (nla[NFTA_SET_DATA_LEN] == NULL)
3549 return -EINVAL;
3550 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
3551 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
3552 return -EINVAL;
3553 } else
3554 desc.dlen = sizeof(struct nft_verdict);
3555 } else if (flags & NFT_SET_MAP)
3556 return -EINVAL;
3557
3558 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
3559 if (!(flags & NFT_SET_OBJECT))
3560 return -EINVAL;
3561
3562 objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
3563 if (objtype == NFT_OBJECT_UNSPEC ||
3564 objtype > NFT_OBJECT_MAX)
3565 return -EINVAL;
3566 } else if (flags & NFT_SET_OBJECT)
3567 return -EINVAL;
3568 else
3569 objtype = NFT_OBJECT_UNSPEC;
3570
3571 timeout = 0;
3572 if (nla[NFTA_SET_TIMEOUT] != NULL) {
3573 if (!(flags & NFT_SET_TIMEOUT))
3574 return -EINVAL;
3575
3576 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout);
3577 if (err)
3578 return err;
3579 }
3580 gc_int = 0;
3581 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
3582 if (!(flags & NFT_SET_TIMEOUT))
3583 return -EINVAL;
3584 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
3585 }
3586
3587 policy = NFT_SET_POL_PERFORMANCE;
3588 if (nla[NFTA_SET_POLICY] != NULL)
3589 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
3590
3591 if (nla[NFTA_SET_DESC] != NULL) {
3592 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
3593 if (err < 0)
3594 return err;
3595 }
3596
3597 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask);
3598 if (IS_ERR(table)) {
3599 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
3600 return PTR_ERR(table);
3601 }
3602
3603 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
3604
3605 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
3606 if (IS_ERR(set)) {
3607 if (PTR_ERR(set) != -ENOENT) {
3608 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
3609 return PTR_ERR(set);
3610 }
3611 } else {
3612 if (nlh->nlmsg_flags & NLM_F_EXCL) {
3613 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
3614 return -EEXIST;
3615 }
3616 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3617 return -EOPNOTSUPP;
3618
3619 return 0;
3620 }
3621
3622 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
3623 return -ENOENT;
3624
3625 ops = nft_select_set_ops(&ctx, nla, &desc, policy);
3626 if (IS_ERR(ops))
3627 return PTR_ERR(ops);
3628
3629 udlen = 0;
3630 if (nla[NFTA_SET_USERDATA])
3631 udlen = nla_len(nla[NFTA_SET_USERDATA]);
3632
3633 size = 0;
3634 if (ops->privsize != NULL)
3635 size = ops->privsize(nla, &desc);
3636
3637 set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
3638 if (!set) {
3639 err = -ENOMEM;
3640 goto err1;
3641 }
3642
3643 name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL);
3644 if (!name) {
3645 err = -ENOMEM;
3646 goto err2;
3647 }
3648
3649 err = nf_tables_set_alloc_name(&ctx, set, name);
3650 kfree(name);
3651 if (err < 0)
3652 goto err2;
3653
3654 udata = NULL;
3655 if (udlen) {
3656 udata = set->data + size;
3657 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
3658 }
3659
3660 INIT_LIST_HEAD(&set->bindings);
3661 set->table = table;
3662 write_pnet(&set->net, net);
3663 set->ops = ops;
3664 set->ktype = ktype;
3665 set->klen = desc.klen;
3666 set->dtype = dtype;
3667 set->objtype = objtype;
3668 set->dlen = desc.dlen;
3669 set->flags = flags;
3670 set->size = desc.size;
3671 set->policy = policy;
3672 set->udlen = udlen;
3673 set->udata = udata;
3674 set->timeout = timeout;
3675 set->gc_int = gc_int;
3676 set->handle = nf_tables_alloc_handle(table);
3677
3678 err = ops->init(set, &desc, nla);
3679 if (err < 0)
3680 goto err3;
3681
3682 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
3683 if (err < 0)
3684 goto err4;
3685
3686 list_add_tail_rcu(&set->list, &table->sets);
3687 table->use++;
3688 return 0;
3689
3690 err4:
3691 ops->destroy(set);
3692 err3:
3693 kfree(set->name);
3694 err2:
3695 kvfree(set);
3696 err1:
3697 module_put(to_set_type(ops)->owner);
3698 return err;
3699 }
3700
3701 static void nft_set_destroy(struct nft_set *set)
3702 {
3703 if (WARN_ON(set->use > 0))
3704 return;
3705
3706 set->ops->destroy(set);
3707 module_put(to_set_type(set->ops)->owner);
3708 kfree(set->name);
3709 kvfree(set);
3710 }
3711
3712 static int nf_tables_delset(struct net *net, struct sock *nlsk,
3713 struct sk_buff *skb, const struct nlmsghdr *nlh,
3714 const struct nlattr * const nla[],
3715 struct netlink_ext_ack *extack)
3716 {
3717 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3718 u8 genmask = nft_genmask_next(net);
3719 const struct nlattr *attr;
3720 struct nft_set *set;
3721 struct nft_ctx ctx;
3722 int err;
3723
3724 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
3725 return -EAFNOSUPPORT;
3726 if (nla[NFTA_SET_TABLE] == NULL)
3727 return -EINVAL;
3728
3729 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
3730 genmask);
3731 if (err < 0)
3732 return err;
3733
3734 if (nla[NFTA_SET_HANDLE]) {
3735 attr = nla[NFTA_SET_HANDLE];
3736 set = nft_set_lookup_byhandle(ctx.table, attr, genmask);
3737 } else {
3738 attr = nla[NFTA_SET_NAME];
3739 set = nft_set_lookup(ctx.table, attr, genmask);
3740 }
3741
3742 if (IS_ERR(set)) {
3743 NL_SET_BAD_ATTR(extack, attr);
3744 return PTR_ERR(set);
3745 }
3746 if (set->use ||
3747 (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) {
3748 NL_SET_BAD_ATTR(extack, attr);
3749 return -EBUSY;
3750 }
3751
3752 return nft_delset(&ctx, set);
3753 }
3754
3755 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
3756 struct nft_set *set,
3757 const struct nft_set_iter *iter,
3758 struct nft_set_elem *elem)
3759 {
3760 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3761 enum nft_registers dreg;
3762
3763 dreg = nft_type_to_reg(set->dtype);
3764 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
3765 set->dtype == NFT_DATA_VERDICT ?
3766 NFT_DATA_VERDICT : NFT_DATA_VALUE,
3767 set->dlen);
3768 }
3769
3770 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
3771 struct nft_set_binding *binding)
3772 {
3773 struct nft_set_binding *i;
3774 struct nft_set_iter iter;
3775
3776 if (set->use == UINT_MAX)
3777 return -EOVERFLOW;
3778
3779 if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
3780 return -EBUSY;
3781
3782 if (binding->flags & NFT_SET_MAP) {
3783 /* If the set is already bound to the same chain all
3784 * jumps are already validated for that chain.
3785 */
3786 list_for_each_entry(i, &set->bindings, list) {
3787 if (i->flags & NFT_SET_MAP &&
3788 i->chain == binding->chain)
3789 goto bind;
3790 }
3791
3792 iter.genmask = nft_genmask_next(ctx->net);
3793 iter.skip = 0;
3794 iter.count = 0;
3795 iter.err = 0;
3796 iter.fn = nf_tables_bind_check_setelem;
3797
3798 set->ops->walk(ctx, set, &iter);
3799 if (iter.err < 0)
3800 return iter.err;
3801 }
3802 bind:
3803 binding->chain = ctx->chain;
3804 list_add_tail_rcu(&binding->list, &set->bindings);
3805 nft_set_trans_bind(ctx, set);
3806 set->use++;
3807
3808 return 0;
3809 }
3810 EXPORT_SYMBOL_GPL(nf_tables_bind_set);
3811
3812 static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
3813 struct nft_set_binding *binding, bool event)
3814 {
3815 list_del_rcu(&binding->list);
3816
3817 if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
3818 list_del_rcu(&set->list);
3819 if (event)
3820 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
3821 GFP_KERNEL);
3822 }
3823 }
3824
3825 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
3826 struct nft_set_binding *binding,
3827 enum nft_trans_phase phase)
3828 {
3829 switch (phase) {
3830 case NFT_TRANS_PREPARE:
3831 set->use--;
3832 return;
3833 case NFT_TRANS_ABORT:
3834 case NFT_TRANS_RELEASE:
3835 set->use--;
3836 /* fall through */
3837 default:
3838 nf_tables_unbind_set(ctx, set, binding,
3839 phase == NFT_TRANS_COMMIT);
3840 }
3841 }
3842 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
3843
3844 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
3845 {
3846 if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
3847 nft_set_destroy(set);
3848 }
3849 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
3850
3851 const struct nft_set_ext_type nft_set_ext_types[] = {
3852 [NFT_SET_EXT_KEY] = {
3853 .align = __alignof__(u32),
3854 },
3855 [NFT_SET_EXT_DATA] = {
3856 .align = __alignof__(u32),
3857 },
3858 [NFT_SET_EXT_EXPR] = {
3859 .align = __alignof__(struct nft_expr),
3860 },
3861 [NFT_SET_EXT_OBJREF] = {
3862 .len = sizeof(struct nft_object *),
3863 .align = __alignof__(struct nft_object *),
3864 },
3865 [NFT_SET_EXT_FLAGS] = {
3866 .len = sizeof(u8),
3867 .align = __alignof__(u8),
3868 },
3869 [NFT_SET_EXT_TIMEOUT] = {
3870 .len = sizeof(u64),
3871 .align = __alignof__(u64),
3872 },
3873 [NFT_SET_EXT_EXPIRATION] = {
3874 .len = sizeof(u64),
3875 .align = __alignof__(u64),
3876 },
3877 [NFT_SET_EXT_USERDATA] = {
3878 .len = sizeof(struct nft_userdata),
3879 .align = __alignof__(struct nft_userdata),
3880 },
3881 };
3882 EXPORT_SYMBOL_GPL(nft_set_ext_types);
3883
3884 /*
3885 * Set elements
3886 */
3887
3888 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3889 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
3890 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
3891 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
3892 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
3893 [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
3894 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
3895 .len = NFT_USERDATA_MAXLEN },
3896 [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
3897 [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING },
3898 };
3899
3900 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3901 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
3902 .len = NFT_TABLE_MAXNAMELEN - 1 },
3903 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
3904 .len = NFT_SET_MAXNAMELEN - 1 },
3905 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
3906 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
3907 };
3908
3909 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
3910 const struct sk_buff *skb,
3911 const struct nlmsghdr *nlh,
3912 const struct nlattr * const nla[],
3913 struct netlink_ext_ack *extack,
3914 u8 genmask)
3915 {
3916 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3917 int family = nfmsg->nfgen_family;
3918 struct nft_table *table;
3919
3920 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
3921 genmask);
3922 if (IS_ERR(table)) {
3923 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
3924 return PTR_ERR(table);
3925 }
3926
3927 nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
3928 return 0;
3929 }
3930
3931 static int nf_tables_fill_setelem(struct sk_buff *skb,
3932 const struct nft_set *set,
3933 const struct nft_set_elem *elem)
3934 {
3935 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3936 unsigned char *b = skb_tail_pointer(skb);
3937 struct nlattr *nest;
3938
3939 nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
3940 if (nest == NULL)
3941 goto nla_put_failure;
3942
3943 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3944 NFT_DATA_VALUE, set->klen) < 0)
3945 goto nla_put_failure;
3946
3947 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3948 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
3949 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3950 set->dlen) < 0)
3951 goto nla_put_failure;
3952
3953 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3954 nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3955 goto nla_put_failure;
3956
3957 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
3958 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
3959 (*nft_set_ext_obj(ext))->key.name) < 0)
3960 goto nla_put_failure;
3961
3962 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3963 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3964 htonl(*nft_set_ext_flags(ext))))
3965 goto nla_put_failure;
3966
3967 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3968 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
3969 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
3970 NFTA_SET_ELEM_PAD))
3971 goto nla_put_failure;
3972
3973 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3974 u64 expires, now = get_jiffies_64();
3975
3976 expires = *nft_set_ext_expiration(ext);
3977 if (time_before64(now, expires))
3978 expires -= now;
3979 else
3980 expires = 0;
3981
3982 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
3983 nf_jiffies64_to_msecs(expires),
3984 NFTA_SET_ELEM_PAD))
3985 goto nla_put_failure;
3986 }
3987
3988 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3989 struct nft_userdata *udata;
3990
3991 udata = nft_set_ext_userdata(ext);
3992 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3993 udata->len + 1, udata->data))
3994 goto nla_put_failure;
3995 }
3996
3997 nla_nest_end(skb, nest);
3998 return 0;
3999
4000 nla_put_failure:
4001 nlmsg_trim(skb, b);
4002 return -EMSGSIZE;
4003 }
4004
4005 struct nft_set_dump_args {
4006 const struct netlink_callback *cb;
4007 struct nft_set_iter iter;
4008 struct sk_buff *skb;
4009 };
4010
4011 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
4012 struct nft_set *set,
4013 const struct nft_set_iter *iter,
4014 struct nft_set_elem *elem)
4015 {
4016 struct nft_set_dump_args *args;
4017
4018 args = container_of(iter, struct nft_set_dump_args, iter);
4019 return nf_tables_fill_setelem(args->skb, set, elem);
4020 }
4021
4022 struct nft_set_dump_ctx {
4023 const struct nft_set *set;
4024 struct nft_ctx ctx;
4025 };
4026
4027 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
4028 {
4029 struct nft_set_dump_ctx *dump_ctx = cb->data;
4030 struct net *net = sock_net(skb->sk);
4031 struct nft_table *table;
4032 struct nft_set *set;
4033 struct nft_set_dump_args args;
4034 bool set_found = false;
4035 struct nfgenmsg *nfmsg;
4036 struct nlmsghdr *nlh;
4037 struct nlattr *nest;
4038 u32 portid, seq;
4039 int event;
4040
4041 rcu_read_lock();
4042 list_for_each_entry_rcu(table, &net->nft.tables, list) {
4043 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
4044 dump_ctx->ctx.family != table->family)
4045 continue;
4046
4047 if (table != dump_ctx->ctx.table)
4048 continue;
4049
4050 list_for_each_entry_rcu(set, &table->sets, list) {
4051 if (set == dump_ctx->set) {
4052 set_found = true;
4053 break;
4054 }
4055 }
4056 break;
4057 }
4058
4059 if (!set_found) {
4060 rcu_read_unlock();
4061 return -ENOENT;
4062 }
4063
4064 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
4065 portid = NETLINK_CB(cb->skb).portid;
4066 seq = cb->nlh->nlmsg_seq;
4067
4068 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4069 NLM_F_MULTI);
4070 if (nlh == NULL)
4071 goto nla_put_failure;
4072
4073 nfmsg = nlmsg_data(nlh);
4074 nfmsg->nfgen_family = table->family;
4075 nfmsg->version = NFNETLINK_V0;
4076 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
4077
4078 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
4079 goto nla_put_failure;
4080 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
4081 goto nla_put_failure;
4082
4083 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
4084 if (nest == NULL)
4085 goto nla_put_failure;
4086
4087 args.cb = cb;
4088 args.skb = skb;
4089 args.iter.genmask = nft_genmask_cur(net);
4090 args.iter.skip = cb->args[0];
4091 args.iter.count = 0;
4092 args.iter.err = 0;
4093 args.iter.fn = nf_tables_dump_setelem;
4094 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
4095 rcu_read_unlock();
4096
4097 nla_nest_end(skb, nest);
4098 nlmsg_end(skb, nlh);
4099
4100 if (args.iter.err && args.iter.err != -EMSGSIZE)
4101 return args.iter.err;
4102 if (args.iter.count == cb->args[0])
4103 return 0;
4104
4105 cb->args[0] = args.iter.count;
4106 return skb->len;
4107
4108 nla_put_failure:
4109 rcu_read_unlock();
4110 return -ENOSPC;
4111 }
4112
4113 static int nf_tables_dump_set_start(struct netlink_callback *cb)
4114 {
4115 struct nft_set_dump_ctx *dump_ctx = cb->data;
4116
4117 cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
4118
4119 return cb->data ? 0 : -ENOMEM;
4120 }
4121
4122 static int nf_tables_dump_set_done(struct netlink_callback *cb)
4123 {
4124 kfree(cb->data);
4125 return 0;
4126 }
4127
4128 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
4129 const struct nft_ctx *ctx, u32 seq,
4130 u32 portid, int event, u16 flags,
4131 const struct nft_set *set,
4132 const struct nft_set_elem *elem)
4133 {
4134 struct nfgenmsg *nfmsg;
4135 struct nlmsghdr *nlh;
4136 struct nlattr *nest;
4137 int err;
4138
4139 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
4140 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4141 flags);
4142 if (nlh == NULL)
4143 goto nla_put_failure;
4144
4145 nfmsg = nlmsg_data(nlh);
4146 nfmsg->nfgen_family = ctx->family;
4147 nfmsg->version = NFNETLINK_V0;
4148 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
4149
4150 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4151 goto nla_put_failure;
4152 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4153 goto nla_put_failure;
4154
4155 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
4156 if (nest == NULL)
4157 goto nla_put_failure;
4158
4159 err = nf_tables_fill_setelem(skb, set, elem);
4160 if (err < 0)
4161 goto nla_put_failure;
4162
4163 nla_nest_end(skb, nest);
4164
4165 nlmsg_end(skb, nlh);
4166 return 0;
4167
4168 nla_put_failure:
4169 nlmsg_trim(skb, nlh);
4170 return -1;
4171 }
4172
4173 static int nft_setelem_parse_flags(const struct nft_set *set,
4174 const struct nlattr *attr, u32 *flags)
4175 {
4176 if (attr == NULL)
4177 return 0;
4178
4179 *flags = ntohl(nla_get_be32(attr));
4180 if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
4181 return -EINVAL;
4182 if (!(set->flags & NFT_SET_INTERVAL) &&
4183 *flags & NFT_SET_ELEM_INTERVAL_END)
4184 return -EINVAL;
4185
4186 return 0;
4187 }
4188
4189 static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
4190 const struct nlattr *attr)
4191 {
4192 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
4193 struct nft_data_desc desc;
4194 struct nft_set_elem elem;
4195 struct sk_buff *skb;
4196 uint32_t flags = 0;
4197 void *priv;
4198 int err;
4199
4200 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4201 nft_set_elem_policy, NULL);
4202 if (err < 0)
4203 return err;
4204
4205 if (!nla[NFTA_SET_ELEM_KEY])
4206 return -EINVAL;
4207
4208 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4209 if (err < 0)
4210 return err;
4211
4212 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
4213 nla[NFTA_SET_ELEM_KEY]);
4214 if (err < 0)
4215 return err;
4216
4217 err = -EINVAL;
4218 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
4219 return err;
4220
4221 priv = set->ops->get(ctx->net, set, &elem, flags);
4222 if (IS_ERR(priv))
4223 return PTR_ERR(priv);
4224
4225 elem.priv = priv;
4226
4227 err = -ENOMEM;
4228 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
4229 if (skb == NULL)
4230 goto err1;
4231
4232 err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
4233 NFT_MSG_NEWSETELEM, 0, set, &elem);
4234 if (err < 0)
4235 goto err2;
4236
4237 err = nfnetlink_unicast(skb, ctx->net, ctx->portid, MSG_DONTWAIT);
4238 /* This avoids a loop in nfnetlink. */
4239 if (err < 0)
4240 goto err1;
4241
4242 return 0;
4243 err2:
4244 kfree_skb(skb);
4245 err1:
4246 /* this avoids a loop in nfnetlink. */
4247 return err == -EAGAIN ? -ENOBUFS : err;
4248 }
4249
4250 /* called with rcu_read_lock held */
4251 static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
4252 struct sk_buff *skb, const struct nlmsghdr *nlh,
4253 const struct nlattr * const nla[],
4254 struct netlink_ext_ack *extack)
4255 {
4256 u8 genmask = nft_genmask_cur(net);
4257 struct nft_set *set;
4258 struct nlattr *attr;
4259 struct nft_ctx ctx;
4260 int rem, err = 0;
4261
4262 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
4263 genmask);
4264 if (err < 0)
4265 return err;
4266
4267 set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
4268 if (IS_ERR(set))
4269 return PTR_ERR(set);
4270
4271 if (nlh->nlmsg_flags & NLM_F_DUMP) {
4272 struct netlink_dump_control c = {
4273 .start = nf_tables_dump_set_start,
4274 .dump = nf_tables_dump_set,
4275 .done = nf_tables_dump_set_done,
4276 .module = THIS_MODULE,
4277 };
4278 struct nft_set_dump_ctx dump_ctx = {
4279 .set = set,
4280 .ctx = ctx,
4281 };
4282
4283 c.data = &dump_ctx;
4284 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
4285 }
4286
4287 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
4288 return -EINVAL;
4289
4290 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4291 err = nft_get_set_elem(&ctx, set, attr);
4292 if (err < 0)
4293 break;
4294 }
4295
4296 return err;
4297 }
4298
4299 static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
4300 const struct nft_set *set,
4301 const struct nft_set_elem *elem,
4302 int event, u16 flags)
4303 {
4304 struct net *net = ctx->net;
4305 u32 portid = ctx->portid;
4306 struct sk_buff *skb;
4307 int err;
4308
4309 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
4310 return;
4311
4312 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4313 if (skb == NULL)
4314 goto err;
4315
4316 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
4317 set, elem);
4318 if (err < 0) {
4319 kfree_skb(skb);
4320 goto err;
4321 }
4322
4323 nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
4324 GFP_KERNEL);
4325 return;
4326 err:
4327 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
4328 }
4329
4330 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
4331 int msg_type,
4332 struct nft_set *set)
4333 {
4334 struct nft_trans *trans;
4335
4336 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
4337 if (trans == NULL)
4338 return NULL;
4339
4340 nft_trans_elem_set(trans) = set;
4341 return trans;
4342 }
4343
4344 void *nft_set_elem_init(const struct nft_set *set,
4345 const struct nft_set_ext_tmpl *tmpl,
4346 const u32 *key, const u32 *data,
4347 u64 timeout, u64 expiration, gfp_t gfp)
4348 {
4349 struct nft_set_ext *ext;
4350 void *elem;
4351
4352 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
4353 if (elem == NULL)
4354 return NULL;
4355
4356 ext = nft_set_elem_ext(set, elem);
4357 nft_set_ext_init(ext, tmpl);
4358
4359 memcpy(nft_set_ext_key(ext), key, set->klen);
4360 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4361 memcpy(nft_set_ext_data(ext), data, set->dlen);
4362 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
4363 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
4364 if (expiration == 0)
4365 *nft_set_ext_expiration(ext) += timeout;
4366 }
4367 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
4368 *nft_set_ext_timeout(ext) = timeout;
4369
4370 return elem;
4371 }
4372
4373 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
4374 bool destroy_expr)
4375 {
4376 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
4377 struct nft_ctx ctx = {
4378 .net = read_pnet(&set->net),
4379 .family = set->table->family,
4380 };
4381
4382 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
4383 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4384 nft_data_release(nft_set_ext_data(ext), set->dtype);
4385 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
4386 struct nft_expr *expr = nft_set_ext_expr(ext);
4387
4388 if (expr->ops->destroy_clone) {
4389 expr->ops->destroy_clone(&ctx, expr);
4390 module_put(expr->ops->type->owner);
4391 } else {
4392 nf_tables_expr_destroy(&ctx, expr);
4393 }
4394 }
4395 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
4396 (*nft_set_ext_obj(ext))->use--;
4397 kfree(elem);
4398 }
4399 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
4400
4401 /* Only called from commit path, nft_set_elem_deactivate() already deals with
4402 * the refcounting from the preparation phase.
4403 */
4404 static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
4405 const struct nft_set *set, void *elem)
4406 {
4407 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
4408
4409 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
4410 nf_tables_expr_destroy(ctx, nft_set_ext_expr(ext));
4411 kfree(elem);
4412 }
4413
4414 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
4415 const struct nlattr *attr, u32 nlmsg_flags)
4416 {
4417 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
4418 u8 genmask = nft_genmask_next(ctx->net);
4419 struct nft_data_desc d1, d2;
4420 struct nft_set_ext_tmpl tmpl;
4421 struct nft_set_ext *ext, *ext2;
4422 struct nft_set_elem elem;
4423 struct nft_set_binding *binding;
4424 struct nft_object *obj = NULL;
4425 struct nft_userdata *udata;
4426 struct nft_data data;
4427 enum nft_registers dreg;
4428 struct nft_trans *trans;
4429 u32 flags = 0;
4430 u64 timeout;
4431 u64 expiration;
4432 u8 ulen;
4433 int err;
4434
4435 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4436 nft_set_elem_policy, NULL);
4437 if (err < 0)
4438 return err;
4439
4440 if (nla[NFTA_SET_ELEM_KEY] == NULL)
4441 return -EINVAL;
4442
4443 nft_set_ext_prepare(&tmpl);
4444
4445 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4446 if (err < 0)
4447 return err;
4448 if (flags != 0)
4449 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
4450
4451 if (set->flags & NFT_SET_MAP) {
4452 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
4453 !(flags & NFT_SET_ELEM_INTERVAL_END))
4454 return -EINVAL;
4455 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
4456 flags & NFT_SET_ELEM_INTERVAL_END)
4457 return -EINVAL;
4458 } else {
4459 if (nla[NFTA_SET_ELEM_DATA] != NULL)
4460 return -EINVAL;
4461 }
4462
4463 timeout = 0;
4464 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
4465 if (!(set->flags & NFT_SET_TIMEOUT))
4466 return -EINVAL;
4467 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
4468 &timeout);
4469 if (err)
4470 return err;
4471 } else if (set->flags & NFT_SET_TIMEOUT) {
4472 timeout = set->timeout;
4473 }
4474
4475 expiration = 0;
4476 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
4477 if (!(set->flags & NFT_SET_TIMEOUT))
4478 return -EINVAL;
4479 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
4480 &expiration);
4481 if (err)
4482 return err;
4483 }
4484
4485 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
4486 nla[NFTA_SET_ELEM_KEY]);
4487 if (err < 0)
4488 goto err1;
4489 err = -EINVAL;
4490 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
4491 goto err2;
4492
4493 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
4494 if (timeout > 0) {
4495 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
4496 if (timeout != set->timeout)
4497 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
4498 }
4499
4500 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
4501 if (!(set->flags & NFT_SET_OBJECT)) {
4502 err = -EINVAL;
4503 goto err2;
4504 }
4505 obj = nft_obj_lookup(ctx->net, ctx->table,
4506 nla[NFTA_SET_ELEM_OBJREF],
4507 set->objtype, genmask);
4508 if (IS_ERR(obj)) {
4509 err = PTR_ERR(obj);
4510 goto err2;
4511 }
4512 nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
4513 }
4514
4515 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
4516 err = nft_data_init(ctx, &data, sizeof(data), &d2,
4517 nla[NFTA_SET_ELEM_DATA]);
4518 if (err < 0)
4519 goto err2;
4520
4521 err = -EINVAL;
4522 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
4523 goto err3;
4524
4525 dreg = nft_type_to_reg(set->dtype);
4526 list_for_each_entry(binding, &set->bindings, list) {
4527 struct nft_ctx bind_ctx = {
4528 .net = ctx->net,
4529 .family = ctx->family,
4530 .table = ctx->table,
4531 .chain = (struct nft_chain *)binding->chain,
4532 };
4533
4534 if (!(binding->flags & NFT_SET_MAP))
4535 continue;
4536
4537 err = nft_validate_register_store(&bind_ctx, dreg,
4538 &data,
4539 d2.type, d2.len);
4540 if (err < 0)
4541 goto err3;
4542
4543 if (d2.type == NFT_DATA_VERDICT &&
4544 (data.verdict.code == NFT_GOTO ||
4545 data.verdict.code == NFT_JUMP))
4546 nft_validate_state_update(ctx->net,
4547 NFT_VALIDATE_NEED);
4548 }
4549
4550 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
4551 }
4552
4553 /* The full maximum length of userdata can exceed the maximum
4554 * offset value (U8_MAX) for following extensions, therefor it
4555 * must be the last extension added.
4556 */
4557 ulen = 0;
4558 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
4559 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
4560 if (ulen > 0)
4561 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
4562 ulen);
4563 }
4564
4565 err = -ENOMEM;
4566 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
4567 timeout, expiration, GFP_KERNEL);
4568 if (elem.priv == NULL)
4569 goto err3;
4570
4571 ext = nft_set_elem_ext(set, elem.priv);
4572 if (flags)
4573 *nft_set_ext_flags(ext) = flags;
4574 if (ulen > 0) {
4575 udata = nft_set_ext_userdata(ext);
4576 udata->len = ulen - 1;
4577 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
4578 }
4579 if (obj) {
4580 *nft_set_ext_obj(ext) = obj;
4581 obj->use++;
4582 }
4583
4584 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
4585 if (trans == NULL)
4586 goto err4;
4587
4588 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
4589 err = set->ops->insert(ctx->net, set, &elem, &ext2);
4590 if (err) {
4591 if (err == -EEXIST) {
4592 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
4593 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
4594 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
4595 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) {
4596 err = -EBUSY;
4597 goto err5;
4598 }
4599 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
4600 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
4601 memcmp(nft_set_ext_data(ext),
4602 nft_set_ext_data(ext2), set->dlen) != 0) ||
4603 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
4604 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
4605 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
4606 err = -EBUSY;
4607 else if (!(nlmsg_flags & NLM_F_EXCL))
4608 err = 0;
4609 }
4610 goto err5;
4611 }
4612
4613 if (set->size &&
4614 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
4615 err = -ENFILE;
4616 goto err6;
4617 }
4618
4619 nft_trans_elem(trans) = elem;
4620 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4621 return 0;
4622
4623 err6:
4624 set->ops->remove(ctx->net, set, &elem);
4625 err5:
4626 kfree(trans);
4627 err4:
4628 if (obj)
4629 obj->use--;
4630 kfree(elem.priv);
4631 err3:
4632 if (nla[NFTA_SET_ELEM_DATA] != NULL)
4633 nft_data_release(&data, d2.type);
4634 err2:
4635 nft_data_release(&elem.key.val, d1.type);
4636 err1:
4637 return err;
4638 }
4639
4640 static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
4641 struct sk_buff *skb, const struct nlmsghdr *nlh,
4642 const struct nlattr * const nla[],
4643 struct netlink_ext_ack *extack)
4644 {
4645 u8 genmask = nft_genmask_next(net);
4646 const struct nlattr *attr;
4647 struct nft_set *set;
4648 struct nft_ctx ctx;
4649 int rem, err;
4650
4651 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
4652 return -EINVAL;
4653
4654 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
4655 genmask);
4656 if (err < 0)
4657 return err;
4658
4659 set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
4660 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
4661 if (IS_ERR(set))
4662 return PTR_ERR(set);
4663
4664 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
4665 return -EBUSY;
4666
4667 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4668 err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags);
4669 if (err < 0)
4670 return err;
4671 }
4672
4673 if (net->nft.validate_state == NFT_VALIDATE_DO)
4674 return nft_table_validate(net, ctx.table);
4675
4676 return 0;
4677 }
4678
4679 /**
4680 * nft_data_hold - hold a nft_data item
4681 *
4682 * @data: struct nft_data to release
4683 * @type: type of data
4684 *
4685 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4686 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
4687 * NFT_GOTO verdicts. This function must be called on active data objects
4688 * from the second phase of the commit protocol.
4689 */
4690 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
4691 {
4692 if (type == NFT_DATA_VERDICT) {
4693 switch (data->verdict.code) {
4694 case NFT_JUMP:
4695 case NFT_GOTO:
4696 data->verdict.chain->use++;
4697 break;
4698 }
4699 }
4700 }
4701
4702 static void nft_set_elem_activate(const struct net *net,
4703 const struct nft_set *set,
4704 struct nft_set_elem *elem)
4705 {
4706 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4707
4708 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4709 nft_data_hold(nft_set_ext_data(ext), set->dtype);
4710 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
4711 (*nft_set_ext_obj(ext))->use++;
4712 }
4713
4714 static void nft_set_elem_deactivate(const struct net *net,
4715 const struct nft_set *set,
4716 struct nft_set_elem *elem)
4717 {
4718 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4719
4720 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4721 nft_data_release(nft_set_ext_data(ext), set->dtype);
4722 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
4723 (*nft_set_ext_obj(ext))->use--;
4724 }
4725
4726 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
4727 const struct nlattr *attr)
4728 {
4729 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
4730 struct nft_set_ext_tmpl tmpl;
4731 struct nft_data_desc desc;
4732 struct nft_set_elem elem;
4733 struct nft_set_ext *ext;
4734 struct nft_trans *trans;
4735 u32 flags = 0;
4736 void *priv;
4737 int err;
4738
4739 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4740 nft_set_elem_policy, NULL);
4741 if (err < 0)
4742 goto err1;
4743
4744 err = -EINVAL;
4745 if (nla[NFTA_SET_ELEM_KEY] == NULL)
4746 goto err1;
4747
4748 nft_set_ext_prepare(&tmpl);
4749
4750 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4751 if (err < 0)
4752 return err;
4753 if (flags != 0)
4754 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
4755
4756 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
4757 nla[NFTA_SET_ELEM_KEY]);
4758 if (err < 0)
4759 goto err1;
4760
4761 err = -EINVAL;
4762 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
4763 goto err2;
4764
4765 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len);
4766
4767 err = -ENOMEM;
4768 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
4769 0, GFP_KERNEL);
4770 if (elem.priv == NULL)
4771 goto err2;
4772
4773 ext = nft_set_elem_ext(set, elem.priv);
4774 if (flags)
4775 *nft_set_ext_flags(ext) = flags;
4776
4777 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
4778 if (trans == NULL) {
4779 err = -ENOMEM;
4780 goto err3;
4781 }
4782
4783 priv = set->ops->deactivate(ctx->net, set, &elem);
4784 if (priv == NULL) {
4785 err = -ENOENT;
4786 goto err4;
4787 }
4788 kfree(elem.priv);
4789 elem.priv = priv;
4790
4791 nft_set_elem_deactivate(ctx->net, set, &elem);
4792
4793 nft_trans_elem(trans) = elem;
4794 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4795 return 0;
4796
4797 err4:
4798 kfree(trans);
4799 err3:
4800 kfree(elem.priv);
4801 err2:
4802 nft_data_release(&elem.key.val, desc.type);
4803 err1:
4804 return err;
4805 }
4806
4807 static int nft_flush_set(const struct nft_ctx *ctx,
4808 struct nft_set *set,
4809 const struct nft_set_iter *iter,
4810 struct nft_set_elem *elem)
4811 {
4812 struct nft_trans *trans;
4813 int err;
4814
4815 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
4816 sizeof(struct nft_trans_elem), GFP_ATOMIC);
4817 if (!trans)
4818 return -ENOMEM;
4819
4820 if (!set->ops->flush(ctx->net, set, elem->priv)) {
4821 err = -ENOENT;
4822 goto err1;
4823 }
4824 set->ndeact++;
4825
4826 nft_set_elem_deactivate(ctx->net, set, elem);
4827 nft_trans_elem_set(trans) = set;
4828 nft_trans_elem(trans) = *elem;
4829 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4830
4831 return 0;
4832 err1:
4833 kfree(trans);
4834 return err;
4835 }
4836
4837 static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
4838 struct sk_buff *skb, const struct nlmsghdr *nlh,
4839 const struct nlattr * const nla[],
4840 struct netlink_ext_ack *extack)
4841 {
4842 u8 genmask = nft_genmask_next(net);
4843 const struct nlattr *attr;
4844 struct nft_set *set;
4845 struct nft_ctx ctx;
4846 int rem, err = 0;
4847
4848 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
4849 genmask);
4850 if (err < 0)
4851 return err;
4852
4853 set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
4854 if (IS_ERR(set))
4855 return PTR_ERR(set);
4856 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
4857 return -EBUSY;
4858
4859 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
4860 struct nft_set_iter iter = {
4861 .genmask = genmask,
4862 .fn = nft_flush_set,
4863 };
4864 set->ops->walk(&ctx, set, &iter);
4865
4866 return iter.err;
4867 }
4868
4869 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4870 err = nft_del_setelem(&ctx, set, attr);
4871 if (err < 0)
4872 break;
4873
4874 set->ndeact++;
4875 }
4876 return err;
4877 }
4878
4879 void nft_set_gc_batch_release(struct rcu_head *rcu)
4880 {
4881 struct nft_set_gc_batch *gcb;
4882 unsigned int i;
4883
4884 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
4885 for (i = 0; i < gcb->head.cnt; i++)
4886 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
4887 kfree(gcb);
4888 }
4889 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
4890
4891 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
4892 gfp_t gfp)
4893 {
4894 struct nft_set_gc_batch *gcb;
4895
4896 gcb = kzalloc(sizeof(*gcb), gfp);
4897 if (gcb == NULL)
4898 return gcb;
4899 gcb->head.set = set;
4900 return gcb;
4901 }
4902 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
4903
4904 /*
4905 * Stateful objects
4906 */
4907
4908 /**
4909 * nft_register_obj- register nf_tables stateful object type
4910 * @obj: object type
4911 *
4912 * Registers the object type for use with nf_tables. Returns zero on
4913 * success or a negative errno code otherwise.
4914 */
4915 int nft_register_obj(struct nft_object_type *obj_type)
4916 {
4917 if (obj_type->type == NFT_OBJECT_UNSPEC)
4918 return -EINVAL;
4919
4920 nfnl_lock(NFNL_SUBSYS_NFTABLES);
4921 list_add_rcu(&obj_type->list, &nf_tables_objects);
4922 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4923 return 0;
4924 }
4925 EXPORT_SYMBOL_GPL(nft_register_obj);
4926
4927 /**
4928 * nft_unregister_obj - unregister nf_tables object type
4929 * @obj: object type
4930 *
4931 * Unregisters the object type for use with nf_tables.
4932 */
4933 void nft_unregister_obj(struct nft_object_type *obj_type)
4934 {
4935 nfnl_lock(NFNL_SUBSYS_NFTABLES);
4936 list_del_rcu(&obj_type->list);
4937 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4938 }
4939 EXPORT_SYMBOL_GPL(nft_unregister_obj);
4940
4941 struct nft_object *nft_obj_lookup(const struct net *net,
4942 const struct nft_table *table,
4943 const struct nlattr *nla, u32 objtype,
4944 u8 genmask)
4945 {
4946 struct nft_object_hash_key k = { .table = table };
4947 char search[NFT_OBJ_MAXNAMELEN];
4948 struct rhlist_head *tmp, *list;
4949 struct nft_object *obj;
4950
4951 nla_strlcpy(search, nla, sizeof(search));
4952 k.name = search;
4953
4954 WARN_ON_ONCE(!rcu_read_lock_held() &&
4955 !lockdep_commit_lock_is_held(net));
4956
4957 rcu_read_lock();
4958 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
4959 if (!list)
4960 goto out;
4961
4962 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
4963 if (objtype == obj->ops->type->type &&
4964 nft_active_genmask(obj, genmask)) {
4965 rcu_read_unlock();
4966 return obj;
4967 }
4968 }
4969 out:
4970 rcu_read_unlock();
4971 return ERR_PTR(-ENOENT);
4972 }
4973 EXPORT_SYMBOL_GPL(nft_obj_lookup);
4974
4975 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
4976 const struct nlattr *nla,
4977 u32 objtype, u8 genmask)
4978 {
4979 struct nft_object *obj;
4980
4981 list_for_each_entry(obj, &table->objects, list) {
4982 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
4983 objtype == obj->ops->type->type &&
4984 nft_active_genmask(obj, genmask))
4985 return obj;
4986 }
4987 return ERR_PTR(-ENOENT);
4988 }
4989
4990 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
4991 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
4992 .len = NFT_TABLE_MAXNAMELEN - 1 },
4993 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
4994 .len = NFT_OBJ_MAXNAMELEN - 1 },
4995 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
4996 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
4997 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
4998 };
4999
5000 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
5001 const struct nft_object_type *type,
5002 const struct nlattr *attr)
5003 {
5004 struct nlattr **tb;
5005 const struct nft_object_ops *ops;
5006 struct nft_object *obj;
5007 int err = -ENOMEM;
5008
5009 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
5010 if (!tb)
5011 goto err1;
5012
5013 if (attr) {
5014 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
5015 type->policy, NULL);
5016 if (err < 0)
5017 goto err2;
5018 } else {
5019 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
5020 }
5021
5022 if (type->select_ops) {
5023 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
5024 if (IS_ERR(ops)) {
5025 err = PTR_ERR(ops);
5026 goto err2;
5027 }
5028 } else {
5029 ops = type->ops;
5030 }
5031
5032 err = -ENOMEM;
5033 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL);
5034 if (!obj)
5035 goto err2;
5036
5037 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
5038 if (err < 0)
5039 goto err3;
5040
5041 obj->ops = ops;
5042
5043 kfree(tb);
5044 return obj;
5045 err3:
5046 kfree(obj);
5047 err2:
5048 kfree(tb);
5049 err1:
5050 return ERR_PTR(err);
5051 }
5052
5053 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
5054 struct nft_object *obj, bool reset)
5055 {
5056 struct nlattr *nest;
5057
5058 nest = nla_nest_start_noflag(skb, attr);
5059 if (!nest)
5060 goto nla_put_failure;
5061 if (obj->ops->dump(skb, obj, reset) < 0)
5062 goto nla_put_failure;
5063 nla_nest_end(skb, nest);
5064 return 0;
5065
5066 nla_put_failure:
5067 return -1;
5068 }
5069
5070 static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
5071 {
5072 const struct nft_object_type *type;
5073
5074 list_for_each_entry(type, &nf_tables_objects, list) {
5075 if (objtype == type->type)
5076 return type;
5077 }
5078 return NULL;
5079 }
5080
5081 static const struct nft_object_type *
5082 nft_obj_type_get(struct net *net, u32 objtype)
5083 {
5084 const struct nft_object_type *type;
5085
5086 type = __nft_obj_type_get(objtype);
5087 if (type != NULL && try_module_get(type->owner))
5088 return type;
5089
5090 lockdep_nfnl_nft_mutex_not_held();
5091 #ifdef CONFIG_MODULES
5092 if (type == NULL) {
5093 nft_request_module(net, "nft-obj-%u", objtype);
5094 if (__nft_obj_type_get(objtype))
5095 return ERR_PTR(-EAGAIN);
5096 }
5097 #endif
5098 return ERR_PTR(-ENOENT);
5099 }
5100
5101 static int nf_tables_newobj(struct net *net, struct sock *nlsk,
5102 struct sk_buff *skb, const struct nlmsghdr *nlh,
5103 const struct nlattr * const nla[],
5104 struct netlink_ext_ack *extack)
5105 {
5106 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5107 const struct nft_object_type *type;
5108 u8 genmask = nft_genmask_next(net);
5109 int family = nfmsg->nfgen_family;
5110 struct nft_table *table;
5111 struct nft_object *obj;
5112 struct nft_ctx ctx;
5113 u32 objtype;
5114 int err;
5115
5116 if (!nla[NFTA_OBJ_TYPE] ||
5117 !nla[NFTA_OBJ_NAME] ||
5118 !nla[NFTA_OBJ_DATA])
5119 return -EINVAL;
5120
5121 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
5122 if (IS_ERR(table)) {
5123 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
5124 return PTR_ERR(table);
5125 }
5126
5127 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5128 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
5129 if (IS_ERR(obj)) {
5130 err = PTR_ERR(obj);
5131 if (err != -ENOENT) {
5132 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
5133 return err;
5134 }
5135 } else {
5136 if (nlh->nlmsg_flags & NLM_F_EXCL) {
5137 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
5138 return -EEXIST;
5139 }
5140 return 0;
5141 }
5142
5143 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5144
5145 type = nft_obj_type_get(net, objtype);
5146 if (IS_ERR(type))
5147 return PTR_ERR(type);
5148
5149 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
5150 if (IS_ERR(obj)) {
5151 err = PTR_ERR(obj);
5152 goto err1;
5153 }
5154 obj->key.table = table;
5155 obj->handle = nf_tables_alloc_handle(table);
5156
5157 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL);
5158 if (!obj->key.name) {
5159 err = -ENOMEM;
5160 goto err2;
5161 }
5162
5163 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
5164 if (err < 0)
5165 goto err3;
5166
5167 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
5168 nft_objname_ht_params);
5169 if (err < 0)
5170 goto err4;
5171
5172 list_add_tail_rcu(&obj->list, &table->objects);
5173 table->use++;
5174 return 0;
5175 err4:
5176 /* queued in transaction log */
5177 INIT_LIST_HEAD(&obj->list);
5178 return err;
5179 err3:
5180 kfree(obj->key.name);
5181 err2:
5182 if (obj->ops->destroy)
5183 obj->ops->destroy(&ctx, obj);
5184 kfree(obj);
5185 err1:
5186 module_put(type->owner);
5187 return err;
5188 }
5189
5190 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
5191 u32 portid, u32 seq, int event, u32 flags,
5192 int family, const struct nft_table *table,
5193 struct nft_object *obj, bool reset)
5194 {
5195 struct nfgenmsg *nfmsg;
5196 struct nlmsghdr *nlh;
5197
5198 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5199 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
5200 if (nlh == NULL)
5201 goto nla_put_failure;
5202
5203 nfmsg = nlmsg_data(nlh);
5204 nfmsg->nfgen_family = family;
5205 nfmsg->version = NFNETLINK_V0;
5206 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
5207
5208 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
5209 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
5210 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
5211 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
5212 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) ||
5213 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
5214 NFTA_OBJ_PAD))
5215 goto nla_put_failure;
5216
5217 nlmsg_end(skb, nlh);
5218 return 0;
5219
5220 nla_put_failure:
5221 nlmsg_trim(skb, nlh);
5222 return -1;
5223 }
5224
5225 struct nft_obj_filter {
5226 char *table;
5227 u32 type;
5228 };
5229
5230 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
5231 {
5232 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
5233 const struct nft_table *table;
5234 unsigned int idx = 0, s_idx = cb->args[0];
5235 struct nft_obj_filter *filter = cb->data;
5236 struct net *net = sock_net(skb->sk);
5237 int family = nfmsg->nfgen_family;
5238 struct nft_object *obj;
5239 bool reset = false;
5240
5241 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
5242 reset = true;
5243
5244 rcu_read_lock();
5245 cb->seq = net->nft.base_seq;
5246
5247 list_for_each_entry_rcu(table, &net->nft.tables, list) {
5248 if (family != NFPROTO_UNSPEC && family != table->family)
5249 continue;
5250
5251 list_for_each_entry_rcu(obj, &table->objects, list) {
5252 if (!nft_is_active(net, obj))
5253 goto cont;
5254 if (idx < s_idx)
5255 goto cont;
5256 if (idx > s_idx)
5257 memset(&cb->args[1], 0,
5258 sizeof(cb->args) - sizeof(cb->args[0]));
5259 if (filter && filter->table &&
5260 strcmp(filter->table, table->name))
5261 goto cont;
5262 if (filter &&
5263 filter->type != NFT_OBJECT_UNSPEC &&
5264 obj->ops->type->type != filter->type)
5265 goto cont;
5266
5267 if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
5268 cb->nlh->nlmsg_seq,
5269 NFT_MSG_NEWOBJ,
5270 NLM_F_MULTI | NLM_F_APPEND,
5271 table->family, table,
5272 obj, reset) < 0)
5273 goto done;
5274
5275 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5276 cont:
5277 idx++;
5278 }
5279 }
5280 done:
5281 rcu_read_unlock();
5282
5283 cb->args[0] = idx;
5284 return skb->len;
5285 }
5286
5287 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
5288 {
5289 const struct nlattr * const *nla = cb->data;
5290 struct nft_obj_filter *filter = NULL;
5291
5292 if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
5293 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
5294 if (!filter)
5295 return -ENOMEM;
5296
5297 if (nla[NFTA_OBJ_TABLE]) {
5298 filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
5299 if (!filter->table) {
5300 kfree(filter);
5301 return -ENOMEM;
5302 }
5303 }
5304
5305 if (nla[NFTA_OBJ_TYPE])
5306 filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5307 }
5308
5309 cb->data = filter;
5310 return 0;
5311 }
5312
5313 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
5314 {
5315 struct nft_obj_filter *filter = cb->data;
5316
5317 if (filter) {
5318 kfree(filter->table);
5319 kfree(filter);
5320 }
5321
5322 return 0;
5323 }
5324
5325 /* called with rcu_read_lock held */
5326 static int nf_tables_getobj(struct net *net, struct sock *nlsk,
5327 struct sk_buff *skb, const struct nlmsghdr *nlh,
5328 const struct nlattr * const nla[],
5329 struct netlink_ext_ack *extack)
5330 {
5331 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5332 u8 genmask = nft_genmask_cur(net);
5333 int family = nfmsg->nfgen_family;
5334 const struct nft_table *table;
5335 struct nft_object *obj;
5336 struct sk_buff *skb2;
5337 bool reset = false;
5338 u32 objtype;
5339 int err;
5340
5341 if (nlh->nlmsg_flags & NLM_F_DUMP) {
5342 struct netlink_dump_control c = {
5343 .start = nf_tables_dump_obj_start,
5344 .dump = nf_tables_dump_obj,
5345 .done = nf_tables_dump_obj_done,
5346 .module = THIS_MODULE,
5347 .data = (void *)nla,
5348 };
5349
5350 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
5351 }
5352
5353 if (!nla[NFTA_OBJ_NAME] ||
5354 !nla[NFTA_OBJ_TYPE])
5355 return -EINVAL;
5356
5357 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
5358 if (IS_ERR(table)) {
5359 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
5360 return PTR_ERR(table);
5361 }
5362
5363 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5364 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
5365 if (IS_ERR(obj)) {
5366 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
5367 return PTR_ERR(obj);
5368 }
5369
5370 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
5371 if (!skb2)
5372 return -ENOMEM;
5373
5374 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
5375 reset = true;
5376
5377 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
5378 nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
5379 family, table, obj, reset);
5380 if (err < 0)
5381 goto err;
5382
5383 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
5384 err:
5385 kfree_skb(skb2);
5386 return err;
5387 }
5388
5389 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
5390 {
5391 if (obj->ops->destroy)
5392 obj->ops->destroy(ctx, obj);
5393
5394 module_put(obj->ops->type->owner);
5395 kfree(obj->key.name);
5396 kfree(obj);
5397 }
5398
5399 static int nf_tables_delobj(struct net *net, struct sock *nlsk,
5400 struct sk_buff *skb, const struct nlmsghdr *nlh,
5401 const struct nlattr * const nla[],
5402 struct netlink_ext_ack *extack)
5403 {
5404 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5405 u8 genmask = nft_genmask_next(net);
5406 int family = nfmsg->nfgen_family;
5407 const struct nlattr *attr;
5408 struct nft_table *table;
5409 struct nft_object *obj;
5410 struct nft_ctx ctx;
5411 u32 objtype;
5412
5413 if (!nla[NFTA_OBJ_TYPE] ||
5414 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
5415 return -EINVAL;
5416
5417 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
5418 if (IS_ERR(table)) {
5419 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
5420 return PTR_ERR(table);
5421 }
5422
5423 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5424 if (nla[NFTA_OBJ_HANDLE]) {
5425 attr = nla[NFTA_OBJ_HANDLE];
5426 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
5427 } else {
5428 attr = nla[NFTA_OBJ_NAME];
5429 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
5430 }
5431
5432 if (IS_ERR(obj)) {
5433 NL_SET_BAD_ATTR(extack, attr);
5434 return PTR_ERR(obj);
5435 }
5436 if (obj->use > 0) {
5437 NL_SET_BAD_ATTR(extack, attr);
5438 return -EBUSY;
5439 }
5440
5441 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5442
5443 return nft_delobj(&ctx, obj);
5444 }
5445
5446 void nft_obj_notify(struct net *net, const struct nft_table *table,
5447 struct nft_object *obj, u32 portid, u32 seq, int event,
5448 int family, int report, gfp_t gfp)
5449 {
5450 struct sk_buff *skb;
5451 int err;
5452
5453 if (!report &&
5454 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
5455 return;
5456
5457 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
5458 if (skb == NULL)
5459 goto err;
5460
5461 err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family,
5462 table, obj, false);
5463 if (err < 0) {
5464 kfree_skb(skb);
5465 goto err;
5466 }
5467
5468 nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp);
5469 return;
5470 err:
5471 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
5472 }
5473 EXPORT_SYMBOL_GPL(nft_obj_notify);
5474
5475 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
5476 struct nft_object *obj, int event)
5477 {
5478 nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
5479 ctx->family, ctx->report, GFP_KERNEL);
5480 }
5481
5482 /*
5483 * Flow tables
5484 */
5485 void nft_register_flowtable_type(struct nf_flowtable_type *type)
5486 {
5487 nfnl_lock(NFNL_SUBSYS_NFTABLES);
5488 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
5489 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5490 }
5491 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
5492
5493 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
5494 {
5495 nfnl_lock(NFNL_SUBSYS_NFTABLES);
5496 list_del_rcu(&type->list);
5497 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5498 }
5499 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
5500
5501 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
5502 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
5503 .len = NFT_NAME_MAXLEN - 1 },
5504 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
5505 .len = NFT_NAME_MAXLEN - 1 },
5506 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
5507 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
5508 };
5509
5510 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
5511 const struct nlattr *nla, u8 genmask)
5512 {
5513 struct nft_flowtable *flowtable;
5514
5515 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
5516 if (!nla_strcmp(nla, flowtable->name) &&
5517 nft_active_genmask(flowtable, genmask))
5518 return flowtable;
5519 }
5520 return ERR_PTR(-ENOENT);
5521 }
5522 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
5523
5524 static struct nft_flowtable *
5525 nft_flowtable_lookup_byhandle(const struct nft_table *table,
5526 const struct nlattr *nla, u8 genmask)
5527 {
5528 struct nft_flowtable *flowtable;
5529
5530 list_for_each_entry(flowtable, &table->flowtables, list) {
5531 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
5532 nft_active_genmask(flowtable, genmask))
5533 return flowtable;
5534 }
5535 return ERR_PTR(-ENOENT);
5536 }
5537
5538 static int nf_tables_parse_devices(const struct nft_ctx *ctx,
5539 const struct nlattr *attr,
5540 struct net_device *dev_array[], int *len)
5541 {
5542 const struct nlattr *tmp;
5543 struct net_device *dev;
5544 char ifname[IFNAMSIZ];
5545 int rem, n = 0, err;
5546
5547 nla_for_each_nested(tmp, attr, rem) {
5548 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
5549 err = -EINVAL;
5550 goto err1;
5551 }
5552
5553 nla_strlcpy(ifname, tmp, IFNAMSIZ);
5554 dev = __dev_get_by_name(ctx->net, ifname);
5555 if (!dev) {
5556 err = -ENOENT;
5557 goto err1;
5558 }
5559
5560 dev_array[n++] = dev;
5561 if (n == NFT_FLOWTABLE_DEVICE_MAX) {
5562 err = -EFBIG;
5563 goto err1;
5564 }
5565 }
5566 if (!len)
5567 return -EINVAL;
5568
5569 err = 0;
5570 err1:
5571 *len = n;
5572 return err;
5573 }
5574
5575 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
5576 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
5577 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
5578 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
5579 };
5580
5581 static int nf_tables_flowtable_parse_hook(const struct nft_ctx *ctx,
5582 const struct nlattr *attr,
5583 struct nft_flowtable *flowtable)
5584 {
5585 struct net_device *dev_array[NFT_FLOWTABLE_DEVICE_MAX];
5586 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
5587 struct nf_hook_ops *ops;
5588 int hooknum, priority;
5589 int err, n = 0, i;
5590
5591 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr,
5592 nft_flowtable_hook_policy, NULL);
5593 if (err < 0)
5594 return err;
5595
5596 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
5597 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY] ||
5598 !tb[NFTA_FLOWTABLE_HOOK_DEVS])
5599 return -EINVAL;
5600
5601 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
5602 if (hooknum != NF_NETDEV_INGRESS)
5603 return -EINVAL;
5604
5605 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
5606
5607 err = nf_tables_parse_devices(ctx, tb[NFTA_FLOWTABLE_HOOK_DEVS],
5608 dev_array, &n);
5609 if (err < 0)
5610 return err;
5611
5612 ops = kcalloc(n, sizeof(struct nf_hook_ops), GFP_KERNEL);
5613 if (!ops)
5614 return -ENOMEM;
5615
5616 flowtable->hooknum = hooknum;
5617 flowtable->priority = priority;
5618 flowtable->ops = ops;
5619 flowtable->ops_len = n;
5620
5621 for (i = 0; i < n; i++) {
5622 flowtable->ops[i].pf = NFPROTO_NETDEV;
5623 flowtable->ops[i].hooknum = hooknum;
5624 flowtable->ops[i].priority = priority;
5625 flowtable->ops[i].priv = &flowtable->data;
5626 flowtable->ops[i].hook = flowtable->data.type->hook;
5627 flowtable->ops[i].dev = dev_array[i];
5628 }
5629
5630 return err;
5631 }
5632
5633 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
5634 {
5635 const struct nf_flowtable_type *type;
5636
5637 list_for_each_entry(type, &nf_tables_flowtables, list) {
5638 if (family == type->family)
5639 return type;
5640 }
5641 return NULL;
5642 }
5643
5644 static const struct nf_flowtable_type *
5645 nft_flowtable_type_get(struct net *net, u8 family)
5646 {
5647 const struct nf_flowtable_type *type;
5648
5649 type = __nft_flowtable_type_get(family);
5650 if (type != NULL && try_module_get(type->owner))
5651 return type;
5652
5653 lockdep_nfnl_nft_mutex_not_held();
5654 #ifdef CONFIG_MODULES
5655 if (type == NULL) {
5656 nft_request_module(net, "nf-flowtable-%u", family);
5657 if (__nft_flowtable_type_get(family))
5658 return ERR_PTR(-EAGAIN);
5659 }
5660 #endif
5661 return ERR_PTR(-ENOENT);
5662 }
5663
5664 static void nft_unregister_flowtable_net_hooks(struct net *net,
5665 struct nft_flowtable *flowtable)
5666 {
5667 int i;
5668
5669 for (i = 0; i < flowtable->ops_len; i++) {
5670 if (!flowtable->ops[i].dev)
5671 continue;
5672
5673 nf_unregister_net_hook(net, &flowtable->ops[i]);
5674 }
5675 }
5676
5677 static int nf_tables_newflowtable(struct net *net, struct sock *nlsk,
5678 struct sk_buff *skb,
5679 const struct nlmsghdr *nlh,
5680 const struct nlattr * const nla[],
5681 struct netlink_ext_ack *extack)
5682 {
5683 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5684 const struct nf_flowtable_type *type;
5685 struct nft_flowtable *flowtable, *ft;
5686 u8 genmask = nft_genmask_next(net);
5687 int family = nfmsg->nfgen_family;
5688 struct nft_table *table;
5689 struct nft_ctx ctx;
5690 int err, i, k;
5691
5692 if (!nla[NFTA_FLOWTABLE_TABLE] ||
5693 !nla[NFTA_FLOWTABLE_NAME] ||
5694 !nla[NFTA_FLOWTABLE_HOOK])
5695 return -EINVAL;
5696
5697 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
5698 genmask);
5699 if (IS_ERR(table)) {
5700 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
5701 return PTR_ERR(table);
5702 }
5703
5704 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
5705 genmask);
5706 if (IS_ERR(flowtable)) {
5707 err = PTR_ERR(flowtable);
5708 if (err != -ENOENT) {
5709 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
5710 return err;
5711 }
5712 } else {
5713 if (nlh->nlmsg_flags & NLM_F_EXCL) {
5714 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
5715 return -EEXIST;
5716 }
5717
5718 return 0;
5719 }
5720
5721 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5722
5723 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL);
5724 if (!flowtable)
5725 return -ENOMEM;
5726
5727 flowtable->table = table;
5728 flowtable->handle = nf_tables_alloc_handle(table);
5729
5730 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL);
5731 if (!flowtable->name) {
5732 err = -ENOMEM;
5733 goto err1;
5734 }
5735
5736 type = nft_flowtable_type_get(net, family);
5737 if (IS_ERR(type)) {
5738 err = PTR_ERR(type);
5739 goto err2;
5740 }
5741
5742 flowtable->data.type = type;
5743 err = type->init(&flowtable->data);
5744 if (err < 0)
5745 goto err3;
5746
5747 err = nf_tables_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
5748 flowtable);
5749 if (err < 0)
5750 goto err4;
5751
5752 for (i = 0; i < flowtable->ops_len; i++) {
5753 if (!flowtable->ops[i].dev)
5754 continue;
5755
5756 list_for_each_entry(ft, &table->flowtables, list) {
5757 for (k = 0; k < ft->ops_len; k++) {
5758 if (!ft->ops[k].dev)
5759 continue;
5760
5761 if (flowtable->ops[i].dev == ft->ops[k].dev &&
5762 flowtable->ops[i].pf == ft->ops[k].pf) {
5763 err = -EBUSY;
5764 goto err5;
5765 }
5766 }
5767 }
5768
5769 err = nf_register_net_hook(net, &flowtable->ops[i]);
5770 if (err < 0)
5771 goto err5;
5772 }
5773
5774 err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
5775 if (err < 0)
5776 goto err6;
5777
5778 list_add_tail_rcu(&flowtable->list, &table->flowtables);
5779 table->use++;
5780
5781 return 0;
5782 err6:
5783 i = flowtable->ops_len;
5784 err5:
5785 for (k = i - 1; k >= 0; k--)
5786 nf_unregister_net_hook(net, &flowtable->ops[k]);
5787
5788 kfree(flowtable->ops);
5789 err4:
5790 flowtable->data.type->free(&flowtable->data);
5791 err3:
5792 module_put(type->owner);
5793 err2:
5794 kfree(flowtable->name);
5795 err1:
5796 kfree(flowtable);
5797 return err;
5798 }
5799
5800 static int nf_tables_delflowtable(struct net *net, struct sock *nlsk,
5801 struct sk_buff *skb,
5802 const struct nlmsghdr *nlh,
5803 const struct nlattr * const nla[],
5804 struct netlink_ext_ack *extack)
5805 {
5806 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5807 u8 genmask = nft_genmask_next(net);
5808 int family = nfmsg->nfgen_family;
5809 struct nft_flowtable *flowtable;
5810 const struct nlattr *attr;
5811 struct nft_table *table;
5812 struct nft_ctx ctx;
5813
5814 if (!nla[NFTA_FLOWTABLE_TABLE] ||
5815 (!nla[NFTA_FLOWTABLE_NAME] &&
5816 !nla[NFTA_FLOWTABLE_HANDLE]))
5817 return -EINVAL;
5818
5819 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
5820 genmask);
5821 if (IS_ERR(table)) {
5822 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
5823 return PTR_ERR(table);
5824 }
5825
5826 if (nla[NFTA_FLOWTABLE_HANDLE]) {
5827 attr = nla[NFTA_FLOWTABLE_HANDLE];
5828 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
5829 } else {
5830 attr = nla[NFTA_FLOWTABLE_NAME];
5831 flowtable = nft_flowtable_lookup(table, attr, genmask);
5832 }
5833
5834 if (IS_ERR(flowtable)) {
5835 NL_SET_BAD_ATTR(extack, attr);
5836 return PTR_ERR(flowtable);
5837 }
5838 if (flowtable->use > 0) {
5839 NL_SET_BAD_ATTR(extack, attr);
5840 return -EBUSY;
5841 }
5842
5843 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5844
5845 return nft_delflowtable(&ctx, flowtable);
5846 }
5847
5848 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
5849 u32 portid, u32 seq, int event,
5850 u32 flags, int family,
5851 struct nft_flowtable *flowtable)
5852 {
5853 struct nlattr *nest, *nest_devs;
5854 struct nfgenmsg *nfmsg;
5855 struct nlmsghdr *nlh;
5856 int i;
5857
5858 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5859 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
5860 if (nlh == NULL)
5861 goto nla_put_failure;
5862
5863 nfmsg = nlmsg_data(nlh);
5864 nfmsg->nfgen_family = family;
5865 nfmsg->version = NFNETLINK_V0;
5866 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
5867
5868 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
5869 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
5870 nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
5871 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
5872 NFTA_FLOWTABLE_PAD))
5873 goto nla_put_failure;
5874
5875 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
5876 if (!nest)
5877 goto nla_put_failure;
5878 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
5879 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->priority)))
5880 goto nla_put_failure;
5881
5882 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
5883 if (!nest_devs)
5884 goto nla_put_failure;
5885
5886 for (i = 0; i < flowtable->ops_len; i++) {
5887 const struct net_device *dev = READ_ONCE(flowtable->ops[i].dev);
5888
5889 if (dev &&
5890 nla_put_string(skb, NFTA_DEVICE_NAME, dev->name))
5891 goto nla_put_failure;
5892 }
5893 nla_nest_end(skb, nest_devs);
5894 nla_nest_end(skb, nest);
5895
5896 nlmsg_end(skb, nlh);
5897 return 0;
5898
5899 nla_put_failure:
5900 nlmsg_trim(skb, nlh);
5901 return -1;
5902 }
5903
5904 struct nft_flowtable_filter {
5905 char *table;
5906 };
5907
5908 static int nf_tables_dump_flowtable(struct sk_buff *skb,
5909 struct netlink_callback *cb)
5910 {
5911 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
5912 struct nft_flowtable_filter *filter = cb->data;
5913 unsigned int idx = 0, s_idx = cb->args[0];
5914 struct net *net = sock_net(skb->sk);
5915 int family = nfmsg->nfgen_family;
5916 struct nft_flowtable *flowtable;
5917 const struct nft_table *table;
5918
5919 rcu_read_lock();
5920 cb->seq = net->nft.base_seq;
5921
5922 list_for_each_entry_rcu(table, &net->nft.tables, list) {
5923 if (family != NFPROTO_UNSPEC && family != table->family)
5924 continue;
5925
5926 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
5927 if (!nft_is_active(net, flowtable))
5928 goto cont;
5929 if (idx < s_idx)
5930 goto cont;
5931 if (idx > s_idx)
5932 memset(&cb->args[1], 0,
5933 sizeof(cb->args) - sizeof(cb->args[0]));
5934 if (filter && filter->table &&
5935 strcmp(filter->table, table->name))
5936 goto cont;
5937
5938 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
5939 cb->nlh->nlmsg_seq,
5940 NFT_MSG_NEWFLOWTABLE,
5941 NLM_F_MULTI | NLM_F_APPEND,
5942 table->family, flowtable) < 0)
5943 goto done;
5944
5945 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5946 cont:
5947 idx++;
5948 }
5949 }
5950 done:
5951 rcu_read_unlock();
5952
5953 cb->args[0] = idx;
5954 return skb->len;
5955 }
5956
5957 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
5958 {
5959 const struct nlattr * const *nla = cb->data;
5960 struct nft_flowtable_filter *filter = NULL;
5961
5962 if (nla[NFTA_FLOWTABLE_TABLE]) {
5963 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
5964 if (!filter)
5965 return -ENOMEM;
5966
5967 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
5968 GFP_ATOMIC);
5969 if (!filter->table) {
5970 kfree(filter);
5971 return -ENOMEM;
5972 }
5973 }
5974
5975 cb->data = filter;
5976 return 0;
5977 }
5978
5979 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
5980 {
5981 struct nft_flowtable_filter *filter = cb->data;
5982
5983 if (!filter)
5984 return 0;
5985
5986 kfree(filter->table);
5987 kfree(filter);
5988
5989 return 0;
5990 }
5991
5992 /* called with rcu_read_lock held */
5993 static int nf_tables_getflowtable(struct net *net, struct sock *nlsk,
5994 struct sk_buff *skb,
5995 const struct nlmsghdr *nlh,
5996 const struct nlattr * const nla[],
5997 struct netlink_ext_ack *extack)
5998 {
5999 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6000 u8 genmask = nft_genmask_cur(net);
6001 int family = nfmsg->nfgen_family;
6002 struct nft_flowtable *flowtable;
6003 const struct nft_table *table;
6004 struct sk_buff *skb2;
6005 int err;
6006
6007 if (nlh->nlmsg_flags & NLM_F_DUMP) {
6008 struct netlink_dump_control c = {
6009 .start = nf_tables_dump_flowtable_start,
6010 .dump = nf_tables_dump_flowtable,
6011 .done = nf_tables_dump_flowtable_done,
6012 .module = THIS_MODULE,
6013 .data = (void *)nla,
6014 };
6015
6016 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
6017 }
6018
6019 if (!nla[NFTA_FLOWTABLE_NAME])
6020 return -EINVAL;
6021
6022 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
6023 genmask);
6024 if (IS_ERR(table))
6025 return PTR_ERR(table);
6026
6027 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
6028 genmask);
6029 if (IS_ERR(flowtable))
6030 return PTR_ERR(flowtable);
6031
6032 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
6033 if (!skb2)
6034 return -ENOMEM;
6035
6036 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
6037 nlh->nlmsg_seq,
6038 NFT_MSG_NEWFLOWTABLE, 0, family,
6039 flowtable);
6040 if (err < 0)
6041 goto err;
6042
6043 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
6044 err:
6045 kfree_skb(skb2);
6046 return err;
6047 }
6048
6049 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
6050 struct nft_flowtable *flowtable,
6051 int event)
6052 {
6053 struct sk_buff *skb;
6054 int err;
6055
6056 if (ctx->report &&
6057 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
6058 return;
6059
6060 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6061 if (skb == NULL)
6062 goto err;
6063
6064 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
6065 ctx->seq, event, 0,
6066 ctx->family, flowtable);
6067 if (err < 0) {
6068 kfree_skb(skb);
6069 goto err;
6070 }
6071
6072 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
6073 ctx->report, GFP_KERNEL);
6074 return;
6075 err:
6076 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
6077 }
6078
6079 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
6080 {
6081 kfree(flowtable->ops);
6082 kfree(flowtable->name);
6083 flowtable->data.type->free(&flowtable->data);
6084 module_put(flowtable->data.type->owner);
6085 kfree(flowtable);
6086 }
6087
6088 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
6089 u32 portid, u32 seq)
6090 {
6091 struct nlmsghdr *nlh;
6092 struct nfgenmsg *nfmsg;
6093 char buf[TASK_COMM_LEN];
6094 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
6095
6096 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
6097 if (nlh == NULL)
6098 goto nla_put_failure;
6099
6100 nfmsg = nlmsg_data(nlh);
6101 nfmsg->nfgen_family = AF_UNSPEC;
6102 nfmsg->version = NFNETLINK_V0;
6103 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
6104
6105 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) ||
6106 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
6107 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
6108 goto nla_put_failure;
6109
6110 nlmsg_end(skb, nlh);
6111 return 0;
6112
6113 nla_put_failure:
6114 nlmsg_trim(skb, nlh);
6115 return -EMSGSIZE;
6116 }
6117
6118 static void nft_flowtable_event(unsigned long event, struct net_device *dev,
6119 struct nft_flowtable *flowtable)
6120 {
6121 int i;
6122
6123 for (i = 0; i < flowtable->ops_len; i++) {
6124 if (flowtable->ops[i].dev != dev)
6125 continue;
6126
6127 nf_unregister_net_hook(dev_net(dev), &flowtable->ops[i]);
6128 flowtable->ops[i].dev = NULL;
6129 break;
6130 }
6131 }
6132
6133 static int nf_tables_flowtable_event(struct notifier_block *this,
6134 unsigned long event, void *ptr)
6135 {
6136 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6137 struct nft_flowtable *flowtable;
6138 struct nft_table *table;
6139 struct net *net;
6140
6141 if (event != NETDEV_UNREGISTER)
6142 return 0;
6143
6144 net = dev_net(dev);
6145 mutex_lock(&net->nft.commit_mutex);
6146 list_for_each_entry(table, &net->nft.tables, list) {
6147 list_for_each_entry(flowtable, &table->flowtables, list) {
6148 nft_flowtable_event(event, dev, flowtable);
6149 }
6150 }
6151 mutex_unlock(&net->nft.commit_mutex);
6152
6153 return NOTIFY_DONE;
6154 }
6155
6156 static struct notifier_block nf_tables_flowtable_notifier = {
6157 .notifier_call = nf_tables_flowtable_event,
6158 };
6159
6160 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
6161 int event)
6162 {
6163 struct nlmsghdr *nlh = nlmsg_hdr(skb);
6164 struct sk_buff *skb2;
6165 int err;
6166
6167 if (nlmsg_report(nlh) &&
6168 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
6169 return;
6170
6171 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6172 if (skb2 == NULL)
6173 goto err;
6174
6175 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
6176 nlh->nlmsg_seq);
6177 if (err < 0) {
6178 kfree_skb(skb2);
6179 goto err;
6180 }
6181
6182 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
6183 nlmsg_report(nlh), GFP_KERNEL);
6184 return;
6185 err:
6186 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
6187 -ENOBUFS);
6188 }
6189
6190 static int nf_tables_getgen(struct net *net, struct sock *nlsk,
6191 struct sk_buff *skb, const struct nlmsghdr *nlh,
6192 const struct nlattr * const nla[],
6193 struct netlink_ext_ack *extack)
6194 {
6195 struct sk_buff *skb2;
6196 int err;
6197
6198 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
6199 if (skb2 == NULL)
6200 return -ENOMEM;
6201
6202 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
6203 nlh->nlmsg_seq);
6204 if (err < 0)
6205 goto err;
6206
6207 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
6208 err:
6209 kfree_skb(skb2);
6210 return err;
6211 }
6212
6213 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
6214 [NFT_MSG_NEWTABLE] = {
6215 .call_batch = nf_tables_newtable,
6216 .attr_count = NFTA_TABLE_MAX,
6217 .policy = nft_table_policy,
6218 },
6219 [NFT_MSG_GETTABLE] = {
6220 .call_rcu = nf_tables_gettable,
6221 .attr_count = NFTA_TABLE_MAX,
6222 .policy = nft_table_policy,
6223 },
6224 [NFT_MSG_DELTABLE] = {
6225 .call_batch = nf_tables_deltable,
6226 .attr_count = NFTA_TABLE_MAX,
6227 .policy = nft_table_policy,
6228 },
6229 [NFT_MSG_NEWCHAIN] = {
6230 .call_batch = nf_tables_newchain,
6231 .attr_count = NFTA_CHAIN_MAX,
6232 .policy = nft_chain_policy,
6233 },
6234 [NFT_MSG_GETCHAIN] = {
6235 .call_rcu = nf_tables_getchain,
6236 .attr_count = NFTA_CHAIN_MAX,
6237 .policy = nft_chain_policy,
6238 },
6239 [NFT_MSG_DELCHAIN] = {
6240 .call_batch = nf_tables_delchain,
6241 .attr_count = NFTA_CHAIN_MAX,
6242 .policy = nft_chain_policy,
6243 },
6244 [NFT_MSG_NEWRULE] = {
6245 .call_batch = nf_tables_newrule,
6246 .attr_count = NFTA_RULE_MAX,
6247 .policy = nft_rule_policy,
6248 },
6249 [NFT_MSG_GETRULE] = {
6250 .call_rcu = nf_tables_getrule,
6251 .attr_count = NFTA_RULE_MAX,
6252 .policy = nft_rule_policy,
6253 },
6254 [NFT_MSG_DELRULE] = {
6255 .call_batch = nf_tables_delrule,
6256 .attr_count = NFTA_RULE_MAX,
6257 .policy = nft_rule_policy,
6258 },
6259 [NFT_MSG_NEWSET] = {
6260 .call_batch = nf_tables_newset,
6261 .attr_count = NFTA_SET_MAX,
6262 .policy = nft_set_policy,
6263 },
6264 [NFT_MSG_GETSET] = {
6265 .call_rcu = nf_tables_getset,
6266 .attr_count = NFTA_SET_MAX,
6267 .policy = nft_set_policy,
6268 },
6269 [NFT_MSG_DELSET] = {
6270 .call_batch = nf_tables_delset,
6271 .attr_count = NFTA_SET_MAX,
6272 .policy = nft_set_policy,
6273 },
6274 [NFT_MSG_NEWSETELEM] = {
6275 .call_batch = nf_tables_newsetelem,
6276 .attr_count = NFTA_SET_ELEM_LIST_MAX,
6277 .policy = nft_set_elem_list_policy,
6278 },
6279 [NFT_MSG_GETSETELEM] = {
6280 .call_rcu = nf_tables_getsetelem,
6281 .attr_count = NFTA_SET_ELEM_LIST_MAX,
6282 .policy = nft_set_elem_list_policy,
6283 },
6284 [NFT_MSG_DELSETELEM] = {
6285 .call_batch = nf_tables_delsetelem,
6286 .attr_count = NFTA_SET_ELEM_LIST_MAX,
6287 .policy = nft_set_elem_list_policy,
6288 },
6289 [NFT_MSG_GETGEN] = {
6290 .call_rcu = nf_tables_getgen,
6291 },
6292 [NFT_MSG_NEWOBJ] = {
6293 .call_batch = nf_tables_newobj,
6294 .attr_count = NFTA_OBJ_MAX,
6295 .policy = nft_obj_policy,
6296 },
6297 [NFT_MSG_GETOBJ] = {
6298 .call_rcu = nf_tables_getobj,
6299 .attr_count = NFTA_OBJ_MAX,
6300 .policy = nft_obj_policy,
6301 },
6302 [NFT_MSG_DELOBJ] = {
6303 .call_batch = nf_tables_delobj,
6304 .attr_count = NFTA_OBJ_MAX,
6305 .policy = nft_obj_policy,
6306 },
6307 [NFT_MSG_GETOBJ_RESET] = {
6308 .call_rcu = nf_tables_getobj,
6309 .attr_count = NFTA_OBJ_MAX,
6310 .policy = nft_obj_policy,
6311 },
6312 [NFT_MSG_NEWFLOWTABLE] = {
6313 .call_batch = nf_tables_newflowtable,
6314 .attr_count = NFTA_FLOWTABLE_MAX,
6315 .policy = nft_flowtable_policy,
6316 },
6317 [NFT_MSG_GETFLOWTABLE] = {
6318 .call_rcu = nf_tables_getflowtable,
6319 .attr_count = NFTA_FLOWTABLE_MAX,
6320 .policy = nft_flowtable_policy,
6321 },
6322 [NFT_MSG_DELFLOWTABLE] = {
6323 .call_batch = nf_tables_delflowtable,
6324 .attr_count = NFTA_FLOWTABLE_MAX,
6325 .policy = nft_flowtable_policy,
6326 },
6327 };
6328
6329 static int nf_tables_validate(struct net *net)
6330 {
6331 struct nft_table *table;
6332
6333 switch (net->nft.validate_state) {
6334 case NFT_VALIDATE_SKIP:
6335 break;
6336 case NFT_VALIDATE_NEED:
6337 nft_validate_state_update(net, NFT_VALIDATE_DO);
6338 /* fall through */
6339 case NFT_VALIDATE_DO:
6340 list_for_each_entry(table, &net->nft.tables, list) {
6341 if (nft_table_validate(net, table) < 0)
6342 return -EAGAIN;
6343 }
6344 break;
6345 }
6346
6347 return 0;
6348 }
6349
6350 /* a drop policy has to be deferred until all rules have been activated,
6351 * otherwise a large ruleset that contains a drop-policy base chain will
6352 * cause all packets to get dropped until the full transaction has been
6353 * processed.
6354 *
6355 * We defer the drop policy until the transaction has been finalized.
6356 */
6357 static void nft_chain_commit_drop_policy(struct nft_trans *trans)
6358 {
6359 struct nft_base_chain *basechain;
6360
6361 if (nft_trans_chain_policy(trans) != NF_DROP)
6362 return;
6363
6364 if (!nft_is_base_chain(trans->ctx.chain))
6365 return;
6366
6367 basechain = nft_base_chain(trans->ctx.chain);
6368 basechain->policy = NF_DROP;
6369 }
6370
6371 static void nft_chain_commit_update(struct nft_trans *trans)
6372 {
6373 struct nft_base_chain *basechain;
6374
6375 if (nft_trans_chain_name(trans)) {
6376 rhltable_remove(&trans->ctx.table->chains_ht,
6377 &trans->ctx.chain->rhlhead,
6378 nft_chain_ht_params);
6379 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
6380 rhltable_insert_key(&trans->ctx.table->chains_ht,
6381 trans->ctx.chain->name,
6382 &trans->ctx.chain->rhlhead,
6383 nft_chain_ht_params);
6384 }
6385
6386 if (!nft_is_base_chain(trans->ctx.chain))
6387 return;
6388
6389 nft_chain_stats_replace(trans);
6390
6391 basechain = nft_base_chain(trans->ctx.chain);
6392
6393 switch (nft_trans_chain_policy(trans)) {
6394 case NF_DROP:
6395 case NF_ACCEPT:
6396 basechain->policy = nft_trans_chain_policy(trans);
6397 break;
6398 }
6399 }
6400
6401 static void nft_commit_release(struct nft_trans *trans)
6402 {
6403 switch (trans->msg_type) {
6404 case NFT_MSG_DELTABLE:
6405 nf_tables_table_destroy(&trans->ctx);
6406 break;
6407 case NFT_MSG_NEWCHAIN:
6408 free_percpu(nft_trans_chain_stats(trans));
6409 kfree(nft_trans_chain_name(trans));
6410 break;
6411 case NFT_MSG_DELCHAIN:
6412 nf_tables_chain_destroy(&trans->ctx);
6413 break;
6414 case NFT_MSG_DELRULE:
6415 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
6416 break;
6417 case NFT_MSG_DELSET:
6418 nft_set_destroy(nft_trans_set(trans));
6419 break;
6420 case NFT_MSG_DELSETELEM:
6421 nf_tables_set_elem_destroy(&trans->ctx,
6422 nft_trans_elem_set(trans),
6423 nft_trans_elem(trans).priv);
6424 break;
6425 case NFT_MSG_DELOBJ:
6426 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
6427 break;
6428 case NFT_MSG_DELFLOWTABLE:
6429 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
6430 break;
6431 }
6432
6433 if (trans->put_net)
6434 put_net(trans->ctx.net);
6435
6436 kfree(trans);
6437 }
6438
6439 static void nf_tables_trans_destroy_work(struct work_struct *w)
6440 {
6441 struct nft_trans *trans, *next;
6442 LIST_HEAD(head);
6443
6444 spin_lock(&nf_tables_destroy_list_lock);
6445 list_splice_init(&nf_tables_destroy_list, &head);
6446 spin_unlock(&nf_tables_destroy_list_lock);
6447
6448 if (list_empty(&head))
6449 return;
6450
6451 synchronize_rcu();
6452
6453 list_for_each_entry_safe(trans, next, &head, list) {
6454 list_del(&trans->list);
6455 nft_commit_release(trans);
6456 }
6457 }
6458
6459 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
6460 {
6461 struct nft_rule *rule;
6462 unsigned int alloc = 0;
6463 int i;
6464
6465 /* already handled or inactive chain? */
6466 if (chain->rules_next || !nft_is_active_next(net, chain))
6467 return 0;
6468
6469 rule = list_entry(&chain->rules, struct nft_rule, list);
6470 i = 0;
6471
6472 list_for_each_entry_continue(rule, &chain->rules, list) {
6473 if (nft_is_active_next(net, rule))
6474 alloc++;
6475 }
6476
6477 chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc);
6478 if (!chain->rules_next)
6479 return -ENOMEM;
6480
6481 list_for_each_entry_continue(rule, &chain->rules, list) {
6482 if (nft_is_active_next(net, rule))
6483 chain->rules_next[i++] = rule;
6484 }
6485
6486 chain->rules_next[i] = NULL;
6487 return 0;
6488 }
6489
6490 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
6491 {
6492 struct nft_trans *trans, *next;
6493
6494 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6495 struct nft_chain *chain = trans->ctx.chain;
6496
6497 if (trans->msg_type == NFT_MSG_NEWRULE ||
6498 trans->msg_type == NFT_MSG_DELRULE) {
6499 kvfree(chain->rules_next);
6500 chain->rules_next = NULL;
6501 }
6502 }
6503 }
6504
6505 static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h)
6506 {
6507 struct nft_rules_old *o = container_of(h, struct nft_rules_old, h);
6508
6509 kvfree(o->start);
6510 }
6511
6512 static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules)
6513 {
6514 struct nft_rule **r = rules;
6515 struct nft_rules_old *old;
6516
6517 while (*r)
6518 r++;
6519
6520 r++; /* rcu_head is after end marker */
6521 old = (void *) r;
6522 old->start = rules;
6523
6524 call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old);
6525 }
6526
6527 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
6528 {
6529 struct nft_rule **g0, **g1;
6530 bool next_genbit;
6531
6532 next_genbit = nft_gencursor_next(net);
6533
6534 g0 = rcu_dereference_protected(chain->rules_gen_0,
6535 lockdep_commit_lock_is_held(net));
6536 g1 = rcu_dereference_protected(chain->rules_gen_1,
6537 lockdep_commit_lock_is_held(net));
6538
6539 /* No changes to this chain? */
6540 if (chain->rules_next == NULL) {
6541 /* chain had no change in last or next generation */
6542 if (g0 == g1)
6543 return;
6544 /*
6545 * chain had no change in this generation; make sure next
6546 * one uses same rules as current generation.
6547 */
6548 if (next_genbit) {
6549 rcu_assign_pointer(chain->rules_gen_1, g0);
6550 nf_tables_commit_chain_free_rules_old(g1);
6551 } else {
6552 rcu_assign_pointer(chain->rules_gen_0, g1);
6553 nf_tables_commit_chain_free_rules_old(g0);
6554 }
6555
6556 return;
6557 }
6558
6559 if (next_genbit)
6560 rcu_assign_pointer(chain->rules_gen_1, chain->rules_next);
6561 else
6562 rcu_assign_pointer(chain->rules_gen_0, chain->rules_next);
6563
6564 chain->rules_next = NULL;
6565
6566 if (g0 == g1)
6567 return;
6568
6569 if (next_genbit)
6570 nf_tables_commit_chain_free_rules_old(g1);
6571 else
6572 nf_tables_commit_chain_free_rules_old(g0);
6573 }
6574
6575 static void nft_obj_del(struct nft_object *obj)
6576 {
6577 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
6578 list_del_rcu(&obj->list);
6579 }
6580
6581 static void nft_chain_del(struct nft_chain *chain)
6582 {
6583 struct nft_table *table = chain->table;
6584
6585 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
6586 nft_chain_ht_params));
6587 list_del_rcu(&chain->list);
6588 }
6589
6590 static void nf_tables_commit_release(struct net *net)
6591 {
6592 struct nft_trans *trans;
6593
6594 /* all side effects have to be made visible.
6595 * For example, if a chain named 'foo' has been deleted, a
6596 * new transaction must not find it anymore.
6597 *
6598 * Memory reclaim happens asynchronously from work queue
6599 * to prevent expensive synchronize_rcu() in commit phase.
6600 */
6601 if (list_empty(&net->nft.commit_list)) {
6602 mutex_unlock(&net->nft.commit_mutex);
6603 return;
6604 }
6605
6606 trans = list_last_entry(&net->nft.commit_list,
6607 struct nft_trans, list);
6608 get_net(trans->ctx.net);
6609 WARN_ON_ONCE(trans->put_net);
6610
6611 trans->put_net = true;
6612 spin_lock(&nf_tables_destroy_list_lock);
6613 list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list);
6614 spin_unlock(&nf_tables_destroy_list_lock);
6615
6616 mutex_unlock(&net->nft.commit_mutex);
6617
6618 schedule_work(&trans_destroy_work);
6619 }
6620
6621 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
6622 {
6623 struct nft_trans *trans, *next;
6624 struct nft_trans_elem *te;
6625 struct nft_chain *chain;
6626 struct nft_table *table;
6627
6628 if (list_empty(&net->nft.commit_list)) {
6629 mutex_unlock(&net->nft.commit_mutex);
6630 return 0;
6631 }
6632
6633 /* 0. Validate ruleset, otherwise roll back for error reporting. */
6634 if (nf_tables_validate(net) < 0)
6635 return -EAGAIN;
6636
6637 /* 1. Allocate space for next generation rules_gen_X[] */
6638 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6639 int ret;
6640
6641 if (trans->msg_type == NFT_MSG_NEWRULE ||
6642 trans->msg_type == NFT_MSG_DELRULE) {
6643 chain = trans->ctx.chain;
6644
6645 ret = nf_tables_commit_chain_prepare(net, chain);
6646 if (ret < 0) {
6647 nf_tables_commit_chain_prepare_cancel(net);
6648 return ret;
6649 }
6650 }
6651 }
6652
6653 /* step 2. Make rules_gen_X visible to packet path */
6654 list_for_each_entry(table, &net->nft.tables, list) {
6655 list_for_each_entry(chain, &table->chains, list)
6656 nf_tables_commit_chain(net, chain);
6657 }
6658
6659 /*
6660 * Bump generation counter, invalidate any dump in progress.
6661 * Cannot fail after this point.
6662 */
6663 while (++net->nft.base_seq == 0);
6664
6665 /* step 3. Start new generation, rules_gen_X now in use. */
6666 net->nft.gencursor = nft_gencursor_next(net);
6667
6668 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6669 switch (trans->msg_type) {
6670 case NFT_MSG_NEWTABLE:
6671 if (nft_trans_table_update(trans)) {
6672 if (!nft_trans_table_enable(trans)) {
6673 nf_tables_table_disable(net,
6674 trans->ctx.table);
6675 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
6676 }
6677 } else {
6678 nft_clear(net, trans->ctx.table);
6679 }
6680 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
6681 nft_trans_destroy(trans);
6682 break;
6683 case NFT_MSG_DELTABLE:
6684 list_del_rcu(&trans->ctx.table->list);
6685 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
6686 break;
6687 case NFT_MSG_NEWCHAIN:
6688 if (nft_trans_chain_update(trans)) {
6689 nft_chain_commit_update(trans);
6690 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
6691 /* trans destroyed after rcu grace period */
6692 } else {
6693 nft_chain_commit_drop_policy(trans);
6694 nft_clear(net, trans->ctx.chain);
6695 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
6696 nft_trans_destroy(trans);
6697 }
6698 break;
6699 case NFT_MSG_DELCHAIN:
6700 nft_chain_del(trans->ctx.chain);
6701 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
6702 nf_tables_unregister_hook(trans->ctx.net,
6703 trans->ctx.table,
6704 trans->ctx.chain);
6705 break;
6706 case NFT_MSG_NEWRULE:
6707 nft_clear(trans->ctx.net, nft_trans_rule(trans));
6708 nf_tables_rule_notify(&trans->ctx,
6709 nft_trans_rule(trans),
6710 NFT_MSG_NEWRULE);
6711 nft_trans_destroy(trans);
6712 break;
6713 case NFT_MSG_DELRULE:
6714 list_del_rcu(&nft_trans_rule(trans)->list);
6715 nf_tables_rule_notify(&trans->ctx,
6716 nft_trans_rule(trans),
6717 NFT_MSG_DELRULE);
6718 nft_rule_expr_deactivate(&trans->ctx,
6719 nft_trans_rule(trans),
6720 NFT_TRANS_COMMIT);
6721 break;
6722 case NFT_MSG_NEWSET:
6723 nft_clear(net, nft_trans_set(trans));
6724 /* This avoids hitting -EBUSY when deleting the table
6725 * from the transaction.
6726 */
6727 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
6728 !list_empty(&nft_trans_set(trans)->bindings))
6729 trans->ctx.table->use--;
6730
6731 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
6732 NFT_MSG_NEWSET, GFP_KERNEL);
6733 nft_trans_destroy(trans);
6734 break;
6735 case NFT_MSG_DELSET:
6736 list_del_rcu(&nft_trans_set(trans)->list);
6737 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
6738 NFT_MSG_DELSET, GFP_KERNEL);
6739 break;
6740 case NFT_MSG_NEWSETELEM:
6741 te = (struct nft_trans_elem *)trans->data;
6742
6743 te->set->ops->activate(net, te->set, &te->elem);
6744 nf_tables_setelem_notify(&trans->ctx, te->set,
6745 &te->elem,
6746 NFT_MSG_NEWSETELEM, 0);
6747 nft_trans_destroy(trans);
6748 break;
6749 case NFT_MSG_DELSETELEM:
6750 te = (struct nft_trans_elem *)trans->data;
6751
6752 nf_tables_setelem_notify(&trans->ctx, te->set,
6753 &te->elem,
6754 NFT_MSG_DELSETELEM, 0);
6755 te->set->ops->remove(net, te->set, &te->elem);
6756 atomic_dec(&te->set->nelems);
6757 te->set->ndeact--;
6758 break;
6759 case NFT_MSG_NEWOBJ:
6760 nft_clear(net, nft_trans_obj(trans));
6761 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
6762 NFT_MSG_NEWOBJ);
6763 nft_trans_destroy(trans);
6764 break;
6765 case NFT_MSG_DELOBJ:
6766 nft_obj_del(nft_trans_obj(trans));
6767 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
6768 NFT_MSG_DELOBJ);
6769 break;
6770 case NFT_MSG_NEWFLOWTABLE:
6771 nft_clear(net, nft_trans_flowtable(trans));
6772 nf_tables_flowtable_notify(&trans->ctx,
6773 nft_trans_flowtable(trans),
6774 NFT_MSG_NEWFLOWTABLE);
6775 nft_trans_destroy(trans);
6776 break;
6777 case NFT_MSG_DELFLOWTABLE:
6778 list_del_rcu(&nft_trans_flowtable(trans)->list);
6779 nf_tables_flowtable_notify(&trans->ctx,
6780 nft_trans_flowtable(trans),
6781 NFT_MSG_DELFLOWTABLE);
6782 nft_unregister_flowtable_net_hooks(net,
6783 nft_trans_flowtable(trans));
6784 break;
6785 }
6786 }
6787
6788 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
6789 nf_tables_commit_release(net);
6790
6791 return 0;
6792 }
6793
6794 static void nf_tables_abort_release(struct nft_trans *trans)
6795 {
6796 switch (trans->msg_type) {
6797 case NFT_MSG_NEWTABLE:
6798 nf_tables_table_destroy(&trans->ctx);
6799 break;
6800 case NFT_MSG_NEWCHAIN:
6801 nf_tables_chain_destroy(&trans->ctx);
6802 break;
6803 case NFT_MSG_NEWRULE:
6804 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
6805 break;
6806 case NFT_MSG_NEWSET:
6807 nft_set_destroy(nft_trans_set(trans));
6808 break;
6809 case NFT_MSG_NEWSETELEM:
6810 nft_set_elem_destroy(nft_trans_elem_set(trans),
6811 nft_trans_elem(trans).priv, true);
6812 break;
6813 case NFT_MSG_NEWOBJ:
6814 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
6815 break;
6816 case NFT_MSG_NEWFLOWTABLE:
6817 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
6818 break;
6819 }
6820 kfree(trans);
6821 }
6822
6823 static int __nf_tables_abort(struct net *net)
6824 {
6825 struct nft_trans *trans, *next;
6826 struct nft_trans_elem *te;
6827
6828 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
6829 list) {
6830 switch (trans->msg_type) {
6831 case NFT_MSG_NEWTABLE:
6832 if (nft_trans_table_update(trans)) {
6833 if (nft_trans_table_enable(trans)) {
6834 nf_tables_table_disable(net,
6835 trans->ctx.table);
6836 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
6837 }
6838 nft_trans_destroy(trans);
6839 } else {
6840 list_del_rcu(&trans->ctx.table->list);
6841 }
6842 break;
6843 case NFT_MSG_DELTABLE:
6844 nft_clear(trans->ctx.net, trans->ctx.table);
6845 nft_trans_destroy(trans);
6846 break;
6847 case NFT_MSG_NEWCHAIN:
6848 if (nft_trans_chain_update(trans)) {
6849 free_percpu(nft_trans_chain_stats(trans));
6850 kfree(nft_trans_chain_name(trans));
6851 nft_trans_destroy(trans);
6852 } else {
6853 trans->ctx.table->use--;
6854 nft_chain_del(trans->ctx.chain);
6855 nf_tables_unregister_hook(trans->ctx.net,
6856 trans->ctx.table,
6857 trans->ctx.chain);
6858 }
6859 break;
6860 case NFT_MSG_DELCHAIN:
6861 trans->ctx.table->use++;
6862 nft_clear(trans->ctx.net, trans->ctx.chain);
6863 nft_trans_destroy(trans);
6864 break;
6865 case NFT_MSG_NEWRULE:
6866 trans->ctx.chain->use--;
6867 list_del_rcu(&nft_trans_rule(trans)->list);
6868 nft_rule_expr_deactivate(&trans->ctx,
6869 nft_trans_rule(trans),
6870 NFT_TRANS_ABORT);
6871 break;
6872 case NFT_MSG_DELRULE:
6873 trans->ctx.chain->use++;
6874 nft_clear(trans->ctx.net, nft_trans_rule(trans));
6875 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
6876 nft_trans_destroy(trans);
6877 break;
6878 case NFT_MSG_NEWSET:
6879 trans->ctx.table->use--;
6880 if (nft_trans_set(trans)->bound) {
6881 nft_trans_destroy(trans);
6882 break;
6883 }
6884 list_del_rcu(&nft_trans_set(trans)->list);
6885 break;
6886 case NFT_MSG_DELSET:
6887 trans->ctx.table->use++;
6888 nft_clear(trans->ctx.net, nft_trans_set(trans));
6889 nft_trans_destroy(trans);
6890 break;
6891 case NFT_MSG_NEWSETELEM:
6892 if (nft_trans_elem_set(trans)->bound) {
6893 nft_trans_destroy(trans);
6894 break;
6895 }
6896 te = (struct nft_trans_elem *)trans->data;
6897 te->set->ops->remove(net, te->set, &te->elem);
6898 atomic_dec(&te->set->nelems);
6899 break;
6900 case NFT_MSG_DELSETELEM:
6901 te = (struct nft_trans_elem *)trans->data;
6902
6903 nft_set_elem_activate(net, te->set, &te->elem);
6904 te->set->ops->activate(net, te->set, &te->elem);
6905 te->set->ndeact--;
6906
6907 nft_trans_destroy(trans);
6908 break;
6909 case NFT_MSG_NEWOBJ:
6910 trans->ctx.table->use--;
6911 nft_obj_del(nft_trans_obj(trans));
6912 break;
6913 case NFT_MSG_DELOBJ:
6914 trans->ctx.table->use++;
6915 nft_clear(trans->ctx.net, nft_trans_obj(trans));
6916 nft_trans_destroy(trans);
6917 break;
6918 case NFT_MSG_NEWFLOWTABLE:
6919 trans->ctx.table->use--;
6920 list_del_rcu(&nft_trans_flowtable(trans)->list);
6921 nft_unregister_flowtable_net_hooks(net,
6922 nft_trans_flowtable(trans));
6923 break;
6924 case NFT_MSG_DELFLOWTABLE:
6925 trans->ctx.table->use++;
6926 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
6927 nft_trans_destroy(trans);
6928 break;
6929 }
6930 }
6931
6932 synchronize_rcu();
6933
6934 list_for_each_entry_safe_reverse(trans, next,
6935 &net->nft.commit_list, list) {
6936 list_del(&trans->list);
6937 nf_tables_abort_release(trans);
6938 }
6939
6940 return 0;
6941 }
6942
6943 static void nf_tables_cleanup(struct net *net)
6944 {
6945 nft_validate_state_update(net, NFT_VALIDATE_SKIP);
6946 }
6947
6948 static int nf_tables_abort(struct net *net, struct sk_buff *skb)
6949 {
6950 int ret = __nf_tables_abort(net);
6951
6952 mutex_unlock(&net->nft.commit_mutex);
6953
6954 return ret;
6955 }
6956
6957 static bool nf_tables_valid_genid(struct net *net, u32 genid)
6958 {
6959 bool genid_ok;
6960
6961 mutex_lock(&net->nft.commit_mutex);
6962
6963 genid_ok = genid == 0 || net->nft.base_seq == genid;
6964 if (!genid_ok)
6965 mutex_unlock(&net->nft.commit_mutex);
6966
6967 /* else, commit mutex has to be released by commit or abort function */
6968 return genid_ok;
6969 }
6970
6971 static const struct nfnetlink_subsystem nf_tables_subsys = {
6972 .name = "nf_tables",
6973 .subsys_id = NFNL_SUBSYS_NFTABLES,
6974 .cb_count = NFT_MSG_MAX,
6975 .cb = nf_tables_cb,
6976 .commit = nf_tables_commit,
6977 .abort = nf_tables_abort,
6978 .cleanup = nf_tables_cleanup,
6979 .valid_genid = nf_tables_valid_genid,
6980 .owner = THIS_MODULE,
6981 };
6982
6983 int nft_chain_validate_dependency(const struct nft_chain *chain,
6984 enum nft_chain_types type)
6985 {
6986 const struct nft_base_chain *basechain;
6987
6988 if (nft_is_base_chain(chain)) {
6989 basechain = nft_base_chain(chain);
6990 if (basechain->type->type != type)
6991 return -EOPNOTSUPP;
6992 }
6993 return 0;
6994 }
6995 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
6996
6997 int nft_chain_validate_hooks(const struct nft_chain *chain,
6998 unsigned int hook_flags)
6999 {
7000 struct nft_base_chain *basechain;
7001
7002 if (nft_is_base_chain(chain)) {
7003 basechain = nft_base_chain(chain);
7004
7005 if ((1 << basechain->ops.hooknum) & hook_flags)
7006 return 0;
7007
7008 return -EOPNOTSUPP;
7009 }
7010
7011 return 0;
7012 }
7013 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
7014
7015 /*
7016 * Loop detection - walk through the ruleset beginning at the destination chain
7017 * of a new jump until either the source chain is reached (loop) or all
7018 * reachable chains have been traversed.
7019 *
7020 * The loop check is performed whenever a new jump verdict is added to an
7021 * expression or verdict map or a verdict map is bound to a new chain.
7022 */
7023
7024 static int nf_tables_check_loops(const struct nft_ctx *ctx,
7025 const struct nft_chain *chain);
7026
7027 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
7028 struct nft_set *set,
7029 const struct nft_set_iter *iter,
7030 struct nft_set_elem *elem)
7031 {
7032 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
7033 const struct nft_data *data;
7034
7035 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
7036 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
7037 return 0;
7038
7039 data = nft_set_ext_data(ext);
7040 switch (data->verdict.code) {
7041 case NFT_JUMP:
7042 case NFT_GOTO:
7043 return nf_tables_check_loops(ctx, data->verdict.chain);
7044 default:
7045 return 0;
7046 }
7047 }
7048
7049 static int nf_tables_check_loops(const struct nft_ctx *ctx,
7050 const struct nft_chain *chain)
7051 {
7052 const struct nft_rule *rule;
7053 const struct nft_expr *expr, *last;
7054 struct nft_set *set;
7055 struct nft_set_binding *binding;
7056 struct nft_set_iter iter;
7057
7058 if (ctx->chain == chain)
7059 return -ELOOP;
7060
7061 list_for_each_entry(rule, &chain->rules, list) {
7062 nft_rule_for_each_expr(expr, last, rule) {
7063 struct nft_immediate_expr *priv;
7064 const struct nft_data *data;
7065 int err;
7066
7067 if (strcmp(expr->ops->type->name, "immediate"))
7068 continue;
7069
7070 priv = nft_expr_priv(expr);
7071 if (priv->dreg != NFT_REG_VERDICT)
7072 continue;
7073
7074 data = &priv->data;
7075 switch (data->verdict.code) {
7076 case NFT_JUMP:
7077 case NFT_GOTO:
7078 err = nf_tables_check_loops(ctx,
7079 data->verdict.chain);
7080 if (err < 0)
7081 return err;
7082 default:
7083 break;
7084 }
7085 }
7086 }
7087
7088 list_for_each_entry(set, &ctx->table->sets, list) {
7089 if (!nft_is_active_next(ctx->net, set))
7090 continue;
7091 if (!(set->flags & NFT_SET_MAP) ||
7092 set->dtype != NFT_DATA_VERDICT)
7093 continue;
7094
7095 list_for_each_entry(binding, &set->bindings, list) {
7096 if (!(binding->flags & NFT_SET_MAP) ||
7097 binding->chain != chain)
7098 continue;
7099
7100 iter.genmask = nft_genmask_next(ctx->net);
7101 iter.skip = 0;
7102 iter.count = 0;
7103 iter.err = 0;
7104 iter.fn = nf_tables_loop_check_setelem;
7105
7106 set->ops->walk(ctx, set, &iter);
7107 if (iter.err < 0)
7108 return iter.err;
7109 }
7110 }
7111
7112 return 0;
7113 }
7114
7115 /**
7116 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
7117 *
7118 * @attr: netlink attribute to fetch value from
7119 * @max: maximum value to be stored in dest
7120 * @dest: pointer to the variable
7121 *
7122 * Parse, check and store a given u32 netlink attribute into variable.
7123 * This function returns -ERANGE if the value goes over maximum value.
7124 * Otherwise a 0 is returned and the attribute value is stored in the
7125 * destination variable.
7126 */
7127 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
7128 {
7129 u32 val;
7130
7131 val = ntohl(nla_get_be32(attr));
7132 if (val > max)
7133 return -ERANGE;
7134
7135 *dest = val;
7136 return 0;
7137 }
7138 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
7139
7140 /**
7141 * nft_parse_register - parse a register value from a netlink attribute
7142 *
7143 * @attr: netlink attribute
7144 *
7145 * Parse and translate a register value from a netlink attribute.
7146 * Registers used to be 128 bit wide, these register numbers will be
7147 * mapped to the corresponding 32 bit register numbers.
7148 */
7149 unsigned int nft_parse_register(const struct nlattr *attr)
7150 {
7151 unsigned int reg;
7152
7153 reg = ntohl(nla_get_be32(attr));
7154 switch (reg) {
7155 case NFT_REG_VERDICT...NFT_REG_4:
7156 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
7157 default:
7158 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
7159 }
7160 }
7161 EXPORT_SYMBOL_GPL(nft_parse_register);
7162
7163 /**
7164 * nft_dump_register - dump a register value to a netlink attribute
7165 *
7166 * @skb: socket buffer
7167 * @attr: attribute number
7168 * @reg: register number
7169 *
7170 * Construct a netlink attribute containing the register number. For
7171 * compatibility reasons, register numbers being a multiple of 4 are
7172 * translated to the corresponding 128 bit register numbers.
7173 */
7174 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
7175 {
7176 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
7177 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
7178 else
7179 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
7180
7181 return nla_put_be32(skb, attr, htonl(reg));
7182 }
7183 EXPORT_SYMBOL_GPL(nft_dump_register);
7184
7185 /**
7186 * nft_validate_register_load - validate a load from a register
7187 *
7188 * @reg: the register number
7189 * @len: the length of the data
7190 *
7191 * Validate that the input register is one of the general purpose
7192 * registers and that the length of the load is within the bounds.
7193 */
7194 int nft_validate_register_load(enum nft_registers reg, unsigned int len)
7195 {
7196 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
7197 return -EINVAL;
7198 if (len == 0)
7199 return -EINVAL;
7200 if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
7201 return -ERANGE;
7202
7203 return 0;
7204 }
7205 EXPORT_SYMBOL_GPL(nft_validate_register_load);
7206
7207 /**
7208 * nft_validate_register_store - validate an expressions' register store
7209 *
7210 * @ctx: context of the expression performing the load
7211 * @reg: the destination register number
7212 * @data: the data to load
7213 * @type: the data type
7214 * @len: the length of the data
7215 *
7216 * Validate that a data load uses the appropriate data type for
7217 * the destination register and the length is within the bounds.
7218 * A value of NULL for the data means that its runtime gathered
7219 * data.
7220 */
7221 int nft_validate_register_store(const struct nft_ctx *ctx,
7222 enum nft_registers reg,
7223 const struct nft_data *data,
7224 enum nft_data_types type, unsigned int len)
7225 {
7226 int err;
7227
7228 switch (reg) {
7229 case NFT_REG_VERDICT:
7230 if (type != NFT_DATA_VERDICT)
7231 return -EINVAL;
7232
7233 if (data != NULL &&
7234 (data->verdict.code == NFT_GOTO ||
7235 data->verdict.code == NFT_JUMP)) {
7236 err = nf_tables_check_loops(ctx, data->verdict.chain);
7237 if (err < 0)
7238 return err;
7239 }
7240
7241 return 0;
7242 default:
7243 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
7244 return -EINVAL;
7245 if (len == 0)
7246 return -EINVAL;
7247 if (reg * NFT_REG32_SIZE + len >
7248 FIELD_SIZEOF(struct nft_regs, data))
7249 return -ERANGE;
7250
7251 if (data != NULL && type != NFT_DATA_VALUE)
7252 return -EINVAL;
7253 return 0;
7254 }
7255 }
7256 EXPORT_SYMBOL_GPL(nft_validate_register_store);
7257
7258 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
7259 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
7260 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
7261 .len = NFT_CHAIN_MAXNAMELEN - 1 },
7262 };
7263
7264 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
7265 struct nft_data_desc *desc, const struct nlattr *nla)
7266 {
7267 u8 genmask = nft_genmask_next(ctx->net);
7268 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
7269 struct nft_chain *chain;
7270 int err;
7271
7272 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
7273 nft_verdict_policy, NULL);
7274 if (err < 0)
7275 return err;
7276
7277 if (!tb[NFTA_VERDICT_CODE])
7278 return -EINVAL;
7279 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
7280
7281 switch (data->verdict.code) {
7282 default:
7283 switch (data->verdict.code & NF_VERDICT_MASK) {
7284 case NF_ACCEPT:
7285 case NF_DROP:
7286 case NF_QUEUE:
7287 break;
7288 default:
7289 return -EINVAL;
7290 }
7291 /* fall through */
7292 case NFT_CONTINUE:
7293 case NFT_BREAK:
7294 case NFT_RETURN:
7295 break;
7296 case NFT_JUMP:
7297 case NFT_GOTO:
7298 if (!tb[NFTA_VERDICT_CHAIN])
7299 return -EINVAL;
7300 chain = nft_chain_lookup(ctx->net, ctx->table,
7301 tb[NFTA_VERDICT_CHAIN], genmask);
7302 if (IS_ERR(chain))
7303 return PTR_ERR(chain);
7304 if (nft_is_base_chain(chain))
7305 return -EOPNOTSUPP;
7306
7307 chain->use++;
7308 data->verdict.chain = chain;
7309 break;
7310 }
7311
7312 desc->len = sizeof(data->verdict);
7313 desc->type = NFT_DATA_VERDICT;
7314 return 0;
7315 }
7316
7317 static void nft_verdict_uninit(const struct nft_data *data)
7318 {
7319 switch (data->verdict.code) {
7320 case NFT_JUMP:
7321 case NFT_GOTO:
7322 data->verdict.chain->use--;
7323 break;
7324 }
7325 }
7326
7327 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
7328 {
7329 struct nlattr *nest;
7330
7331 nest = nla_nest_start_noflag(skb, type);
7332 if (!nest)
7333 goto nla_put_failure;
7334
7335 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
7336 goto nla_put_failure;
7337
7338 switch (v->code) {
7339 case NFT_JUMP:
7340 case NFT_GOTO:
7341 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
7342 v->chain->name))
7343 goto nla_put_failure;
7344 }
7345 nla_nest_end(skb, nest);
7346 return 0;
7347
7348 nla_put_failure:
7349 return -1;
7350 }
7351
7352 static int nft_value_init(const struct nft_ctx *ctx,
7353 struct nft_data *data, unsigned int size,
7354 struct nft_data_desc *desc, const struct nlattr *nla)
7355 {
7356 unsigned int len;
7357
7358 len = nla_len(nla);
7359 if (len == 0)
7360 return -EINVAL;
7361 if (len > size)
7362 return -EOVERFLOW;
7363
7364 nla_memcpy(data->data, nla, len);
7365 desc->type = NFT_DATA_VALUE;
7366 desc->len = len;
7367 return 0;
7368 }
7369
7370 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
7371 unsigned int len)
7372 {
7373 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
7374 }
7375
7376 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
7377 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
7378 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
7379 };
7380
7381 /**
7382 * nft_data_init - parse nf_tables data netlink attributes
7383 *
7384 * @ctx: context of the expression using the data
7385 * @data: destination struct nft_data
7386 * @size: maximum data length
7387 * @desc: data description
7388 * @nla: netlink attribute containing data
7389 *
7390 * Parse the netlink data attributes and initialize a struct nft_data.
7391 * The type and length of data are returned in the data description.
7392 *
7393 * The caller can indicate that it only wants to accept data of type
7394 * NFT_DATA_VALUE by passing NULL for the ctx argument.
7395 */
7396 int nft_data_init(const struct nft_ctx *ctx,
7397 struct nft_data *data, unsigned int size,
7398 struct nft_data_desc *desc, const struct nlattr *nla)
7399 {
7400 struct nlattr *tb[NFTA_DATA_MAX + 1];
7401 int err;
7402
7403 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
7404 nft_data_policy, NULL);
7405 if (err < 0)
7406 return err;
7407
7408 if (tb[NFTA_DATA_VALUE])
7409 return nft_value_init(ctx, data, size, desc,
7410 tb[NFTA_DATA_VALUE]);
7411 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
7412 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
7413 return -EINVAL;
7414 }
7415 EXPORT_SYMBOL_GPL(nft_data_init);
7416
7417 /**
7418 * nft_data_release - release a nft_data item
7419 *
7420 * @data: struct nft_data to release
7421 * @type: type of data
7422 *
7423 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
7424 * all others need to be released by calling this function.
7425 */
7426 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
7427 {
7428 if (type < NFT_DATA_VERDICT)
7429 return;
7430 switch (type) {
7431 case NFT_DATA_VERDICT:
7432 return nft_verdict_uninit(data);
7433 default:
7434 WARN_ON(1);
7435 }
7436 }
7437 EXPORT_SYMBOL_GPL(nft_data_release);
7438
7439 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
7440 enum nft_data_types type, unsigned int len)
7441 {
7442 struct nlattr *nest;
7443 int err;
7444
7445 nest = nla_nest_start_noflag(skb, attr);
7446 if (nest == NULL)
7447 return -1;
7448
7449 switch (type) {
7450 case NFT_DATA_VALUE:
7451 err = nft_value_dump(skb, data, len);
7452 break;
7453 case NFT_DATA_VERDICT:
7454 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
7455 break;
7456 default:
7457 err = -EINVAL;
7458 WARN_ON(1);
7459 }
7460
7461 nla_nest_end(skb, nest);
7462 return err;
7463 }
7464 EXPORT_SYMBOL_GPL(nft_data_dump);
7465
7466 int __nft_release_basechain(struct nft_ctx *ctx)
7467 {
7468 struct nft_rule *rule, *nr;
7469
7470 if (WARN_ON(!nft_is_base_chain(ctx->chain)))
7471 return 0;
7472
7473 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
7474 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
7475 list_del(&rule->list);
7476 ctx->chain->use--;
7477 nf_tables_rule_release(ctx, rule);
7478 }
7479 nft_chain_del(ctx->chain);
7480 ctx->table->use--;
7481 nf_tables_chain_destroy(ctx);
7482
7483 return 0;
7484 }
7485 EXPORT_SYMBOL_GPL(__nft_release_basechain);
7486
7487 static void __nft_release_tables(struct net *net)
7488 {
7489 struct nft_flowtable *flowtable, *nf;
7490 struct nft_table *table, *nt;
7491 struct nft_chain *chain, *nc;
7492 struct nft_object *obj, *ne;
7493 struct nft_rule *rule, *nr;
7494 struct nft_set *set, *ns;
7495 struct nft_ctx ctx = {
7496 .net = net,
7497 .family = NFPROTO_NETDEV,
7498 };
7499
7500 list_for_each_entry_safe(table, nt, &net->nft.tables, list) {
7501 ctx.family = table->family;
7502
7503 list_for_each_entry(chain, &table->chains, list)
7504 nf_tables_unregister_hook(net, table, chain);
7505 /* No packets are walking on these chains anymore. */
7506 ctx.table = table;
7507 list_for_each_entry(chain, &table->chains, list) {
7508 ctx.chain = chain;
7509 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
7510 list_del(&rule->list);
7511 chain->use--;
7512 nf_tables_rule_release(&ctx, rule);
7513 }
7514 }
7515 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
7516 list_del(&flowtable->list);
7517 table->use--;
7518 nf_tables_flowtable_destroy(flowtable);
7519 }
7520 list_for_each_entry_safe(set, ns, &table->sets, list) {
7521 list_del(&set->list);
7522 table->use--;
7523 nft_set_destroy(set);
7524 }
7525 list_for_each_entry_safe(obj, ne, &table->objects, list) {
7526 nft_obj_del(obj);
7527 table->use--;
7528 nft_obj_destroy(&ctx, obj);
7529 }
7530 list_for_each_entry_safe(chain, nc, &table->chains, list) {
7531 ctx.chain = chain;
7532 nft_chain_del(chain);
7533 table->use--;
7534 nf_tables_chain_destroy(&ctx);
7535 }
7536 list_del(&table->list);
7537 nf_tables_table_destroy(&ctx);
7538 }
7539 }
7540
7541 static int __net_init nf_tables_init_net(struct net *net)
7542 {
7543 INIT_LIST_HEAD(&net->nft.tables);
7544 INIT_LIST_HEAD(&net->nft.commit_list);
7545 mutex_init(&net->nft.commit_mutex);
7546 net->nft.base_seq = 1;
7547 net->nft.validate_state = NFT_VALIDATE_SKIP;
7548
7549 return 0;
7550 }
7551
7552 static void __net_exit nf_tables_exit_net(struct net *net)
7553 {
7554 mutex_lock(&net->nft.commit_mutex);
7555 if (!list_empty(&net->nft.commit_list))
7556 __nf_tables_abort(net);
7557 __nft_release_tables(net);
7558 mutex_unlock(&net->nft.commit_mutex);
7559 WARN_ON_ONCE(!list_empty(&net->nft.tables));
7560 }
7561
7562 static struct pernet_operations nf_tables_net_ops = {
7563 .init = nf_tables_init_net,
7564 .exit = nf_tables_exit_net,
7565 };
7566
7567 static int __init nf_tables_module_init(void)
7568 {
7569 int err;
7570
7571 spin_lock_init(&nf_tables_destroy_list_lock);
7572 err = register_pernet_subsys(&nf_tables_net_ops);
7573 if (err < 0)
7574 return err;
7575
7576 err = nft_chain_filter_init();
7577 if (err < 0)
7578 goto err1;
7579
7580 err = nf_tables_core_module_init();
7581 if (err < 0)
7582 goto err2;
7583
7584 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
7585 if (err < 0)
7586 goto err3;
7587
7588 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
7589 if (err < 0)
7590 goto err4;
7591
7592 /* must be last */
7593 err = nfnetlink_subsys_register(&nf_tables_subsys);
7594 if (err < 0)
7595 goto err5;
7596
7597 nft_chain_route_init();
7598 return err;
7599 err5:
7600 rhltable_destroy(&nft_objname_ht);
7601 err4:
7602 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
7603 err3:
7604 nf_tables_core_module_exit();
7605 err2:
7606 nft_chain_filter_fini();
7607 err1:
7608 unregister_pernet_subsys(&nf_tables_net_ops);
7609 return err;
7610 }
7611
7612 static void __exit nf_tables_module_exit(void)
7613 {
7614 nfnetlink_subsys_unregister(&nf_tables_subsys);
7615 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
7616 nft_chain_filter_fini();
7617 nft_chain_route_fini();
7618 unregister_pernet_subsys(&nf_tables_net_ops);
7619 cancel_work_sync(&trans_destroy_work);
7620 rcu_barrier();
7621 rhltable_destroy(&nft_objname_ht);
7622 nf_tables_core_module_exit();
7623 }
7624
7625 module_init(nf_tables_module_init);
7626 module_exit(nf_tables_module_exit);
7627
7628 MODULE_LICENSE("GPL");
7629 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
7630 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);