]> Git Repo - linux.git/blob - net/netfilter/nf_tables_api.c
libbpf: fix GCC8 warning for strncpy
[linux.git] / net / netfilter / nf_tables_api.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2009 Patrick McHardy <[email protected]>
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;
2013
2014         list_for_each_entry(type, &nf_tables_expressions, list) {
2015                 if (!nla_strcmp(nla, type->name) &&
2016                     (!type->family || type->family == family))
2017                         return type;
2018         }
2019         return NULL;
2020 }
2021
2022 static const struct nft_expr_type *nft_expr_type_get(struct net *net,
2023                                                      u8 family,
2024                                                      struct nlattr *nla)
2025 {
2026         const struct nft_expr_type *type;
2027
2028         if (nla == NULL)
2029                 return ERR_PTR(-EINVAL);
2030
2031         type = __nft_expr_type_get(family, nla);
2032         if (type != NULL && try_module_get(type->owner))
2033                 return type;
2034
2035         lockdep_nfnl_nft_mutex_not_held();
2036 #ifdef CONFIG_MODULES
2037         if (type == NULL) {
2038                 nft_request_module(net, "nft-expr-%u-%.*s", family,
2039                                    nla_len(nla), (char *)nla_data(nla));
2040                 if (__nft_expr_type_get(family, nla))
2041                         return ERR_PTR(-EAGAIN);
2042
2043                 nft_request_module(net, "nft-expr-%.*s",
2044                                    nla_len(nla), (char *)nla_data(nla));
2045                 if (__nft_expr_type_get(family, nla))
2046                         return ERR_PTR(-EAGAIN);
2047         }
2048 #endif
2049         return ERR_PTR(-ENOENT);
2050 }
2051
2052 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
2053         [NFTA_EXPR_NAME]        = { .type = NLA_STRING },
2054         [NFTA_EXPR_DATA]        = { .type = NLA_NESTED },
2055 };
2056
2057 static int nf_tables_fill_expr_info(struct sk_buff *skb,
2058                                     const struct nft_expr *expr)
2059 {
2060         if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
2061                 goto nla_put_failure;
2062
2063         if (expr->ops->dump) {
2064                 struct nlattr *data = nla_nest_start_noflag(skb,
2065                                                             NFTA_EXPR_DATA);
2066                 if (data == NULL)
2067                         goto nla_put_failure;
2068                 if (expr->ops->dump(skb, expr) < 0)
2069                         goto nla_put_failure;
2070                 nla_nest_end(skb, data);
2071         }
2072
2073         return skb->len;
2074
2075 nla_put_failure:
2076         return -1;
2077 };
2078
2079 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
2080                   const struct nft_expr *expr)
2081 {
2082         struct nlattr *nest;
2083
2084         nest = nla_nest_start_noflag(skb, attr);
2085         if (!nest)
2086                 goto nla_put_failure;
2087         if (nf_tables_fill_expr_info(skb, expr) < 0)
2088                 goto nla_put_failure;
2089         nla_nest_end(skb, nest);
2090         return 0;
2091
2092 nla_put_failure:
2093         return -1;
2094 }
2095
2096 struct nft_expr_info {
2097         const struct nft_expr_ops       *ops;
2098         struct nlattr                   *tb[NFT_EXPR_MAXATTR + 1];
2099 };
2100
2101 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
2102                                 const struct nlattr *nla,
2103                                 struct nft_expr_info *info)
2104 {
2105         const struct nft_expr_type *type;
2106         const struct nft_expr_ops *ops;
2107         struct nlattr *tb[NFTA_EXPR_MAX + 1];
2108         int err;
2109
2110         err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
2111                                           nft_expr_policy, NULL);
2112         if (err < 0)
2113                 return err;
2114
2115         type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
2116         if (IS_ERR(type))
2117                 return PTR_ERR(type);
2118
2119         if (tb[NFTA_EXPR_DATA]) {
2120                 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
2121                                                   tb[NFTA_EXPR_DATA],
2122                                                   type->policy, NULL);
2123                 if (err < 0)
2124                         goto err1;
2125         } else
2126                 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
2127
2128         if (type->select_ops != NULL) {
2129                 ops = type->select_ops(ctx,
2130                                        (const struct nlattr * const *)info->tb);
2131                 if (IS_ERR(ops)) {
2132                         err = PTR_ERR(ops);
2133                         goto err1;
2134                 }
2135         } else
2136                 ops = type->ops;
2137
2138         info->ops = ops;
2139         return 0;
2140
2141 err1:
2142         module_put(type->owner);
2143         return err;
2144 }
2145
2146 static int nf_tables_newexpr(const struct nft_ctx *ctx,
2147                              const struct nft_expr_info *info,
2148                              struct nft_expr *expr)
2149 {
2150         const struct nft_expr_ops *ops = info->ops;
2151         int err;
2152
2153         expr->ops = ops;
2154         if (ops->init) {
2155                 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
2156                 if (err < 0)
2157                         goto err1;
2158         }
2159
2160         return 0;
2161 err1:
2162         expr->ops = NULL;
2163         return err;
2164 }
2165
2166 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
2167                                    struct nft_expr *expr)
2168 {
2169         const struct nft_expr_type *type = expr->ops->type;
2170
2171         if (expr->ops->destroy)
2172                 expr->ops->destroy(ctx, expr);
2173         module_put(type->owner);
2174 }
2175
2176 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
2177                                const struct nlattr *nla)
2178 {
2179         struct nft_expr_info info;
2180         struct nft_expr *expr;
2181         struct module *owner;
2182         int err;
2183
2184         err = nf_tables_expr_parse(ctx, nla, &info);
2185         if (err < 0)
2186                 goto err1;
2187
2188         err = -ENOMEM;
2189         expr = kzalloc(info.ops->size, GFP_KERNEL);
2190         if (expr == NULL)
2191                 goto err2;
2192
2193         err = nf_tables_newexpr(ctx, &info, expr);
2194         if (err < 0)
2195                 goto err3;
2196
2197         return expr;
2198 err3:
2199         kfree(expr);
2200 err2:
2201         owner = info.ops->type->owner;
2202         if (info.ops->type->release_ops)
2203                 info.ops->type->release_ops(info.ops);
2204
2205         module_put(owner);
2206 err1:
2207         return ERR_PTR(err);
2208 }
2209
2210 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
2211 {
2212         nf_tables_expr_destroy(ctx, expr);
2213         kfree(expr);
2214 }
2215
2216 /*
2217  * Rules
2218  */
2219
2220 static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
2221                                           u64 handle)
2222 {
2223         struct nft_rule *rule;
2224
2225         // FIXME: this sucks
2226         list_for_each_entry_rcu(rule, &chain->rules, list) {
2227                 if (handle == rule->handle)
2228                         return rule;
2229         }
2230
2231         return ERR_PTR(-ENOENT);
2232 }
2233
2234 static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
2235                                         const struct nlattr *nla)
2236 {
2237         if (nla == NULL)
2238                 return ERR_PTR(-EINVAL);
2239
2240         return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
2241 }
2242
2243 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
2244         [NFTA_RULE_TABLE]       = { .type = NLA_STRING,
2245                                     .len = NFT_TABLE_MAXNAMELEN - 1 },
2246         [NFTA_RULE_CHAIN]       = { .type = NLA_STRING,
2247                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
2248         [NFTA_RULE_HANDLE]      = { .type = NLA_U64 },
2249         [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
2250         [NFTA_RULE_COMPAT]      = { .type = NLA_NESTED },
2251         [NFTA_RULE_POSITION]    = { .type = NLA_U64 },
2252         [NFTA_RULE_USERDATA]    = { .type = NLA_BINARY,
2253                                     .len = NFT_USERDATA_MAXLEN },
2254         [NFTA_RULE_ID]          = { .type = NLA_U32 },
2255         [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
2256 };
2257
2258 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
2259                                     u32 portid, u32 seq, int event,
2260                                     u32 flags, int family,
2261                                     const struct nft_table *table,
2262                                     const struct nft_chain *chain,
2263                                     const struct nft_rule *rule,
2264                                     const struct nft_rule *prule)
2265 {
2266         struct nlmsghdr *nlh;
2267         struct nfgenmsg *nfmsg;
2268         const struct nft_expr *expr, *next;
2269         struct nlattr *list;
2270         u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
2271
2272         nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
2273         if (nlh == NULL)
2274                 goto nla_put_failure;
2275
2276         nfmsg = nlmsg_data(nlh);
2277         nfmsg->nfgen_family     = family;
2278         nfmsg->version          = NFNETLINK_V0;
2279         nfmsg->res_id           = htons(net->nft.base_seq & 0xffff);
2280
2281         if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
2282                 goto nla_put_failure;
2283         if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
2284                 goto nla_put_failure;
2285         if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
2286                          NFTA_RULE_PAD))
2287                 goto nla_put_failure;
2288
2289         if (event != NFT_MSG_DELRULE && prule) {
2290                 if (nla_put_be64(skb, NFTA_RULE_POSITION,
2291                                  cpu_to_be64(prule->handle),
2292                                  NFTA_RULE_PAD))
2293                         goto nla_put_failure;
2294         }
2295
2296         list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
2297         if (list == NULL)
2298                 goto nla_put_failure;
2299         nft_rule_for_each_expr(expr, next, rule) {
2300                 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
2301                         goto nla_put_failure;
2302         }
2303         nla_nest_end(skb, list);
2304
2305         if (rule->udata) {
2306                 struct nft_userdata *udata = nft_userdata(rule);
2307                 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
2308                             udata->data) < 0)
2309                         goto nla_put_failure;
2310         }
2311
2312         nlmsg_end(skb, nlh);
2313         return 0;
2314
2315 nla_put_failure:
2316         nlmsg_trim(skb, nlh);
2317         return -1;
2318 }
2319
2320 static void nf_tables_rule_notify(const struct nft_ctx *ctx,
2321                                   const struct nft_rule *rule, int event)
2322 {
2323         struct sk_buff *skb;
2324         int err;
2325
2326         if (!ctx->report &&
2327             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2328                 return;
2329
2330         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2331         if (skb == NULL)
2332                 goto err;
2333
2334         err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
2335                                        event, 0, ctx->family, ctx->table,
2336                                        ctx->chain, rule, NULL);
2337         if (err < 0) {
2338                 kfree_skb(skb);
2339                 goto err;
2340         }
2341
2342         nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
2343                        ctx->report, GFP_KERNEL);
2344         return;
2345 err:
2346         nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
2347 }
2348
2349 struct nft_rule_dump_ctx {
2350         char *table;
2351         char *chain;
2352 };
2353
2354 static int __nf_tables_dump_rules(struct sk_buff *skb,
2355                                   unsigned int *idx,
2356                                   struct netlink_callback *cb,
2357                                   const struct nft_table *table,
2358                                   const struct nft_chain *chain)
2359 {
2360         struct net *net = sock_net(skb->sk);
2361         const struct nft_rule *rule, *prule;
2362         unsigned int s_idx = cb->args[0];
2363
2364         prule = NULL;
2365         list_for_each_entry_rcu(rule, &chain->rules, list) {
2366                 if (!nft_is_active(net, rule))
2367                         goto cont_skip;
2368                 if (*idx < s_idx)
2369                         goto cont;
2370                 if (*idx > s_idx) {
2371                         memset(&cb->args[1], 0,
2372                                         sizeof(cb->args) - sizeof(cb->args[0]));
2373                 }
2374                 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
2375                                         cb->nlh->nlmsg_seq,
2376                                         NFT_MSG_NEWRULE,
2377                                         NLM_F_MULTI | NLM_F_APPEND,
2378                                         table->family,
2379                                         table, chain, rule, prule) < 0)
2380                         return 1;
2381
2382                 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2383 cont:
2384                 prule = rule;
2385 cont_skip:
2386                 (*idx)++;
2387         }
2388         return 0;
2389 }
2390
2391 static int nf_tables_dump_rules(struct sk_buff *skb,
2392                                 struct netlink_callback *cb)
2393 {
2394         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2395         const struct nft_rule_dump_ctx *ctx = cb->data;
2396         struct nft_table *table;
2397         const struct nft_chain *chain;
2398         unsigned int idx = 0;
2399         struct net *net = sock_net(skb->sk);
2400         int family = nfmsg->nfgen_family;
2401
2402         rcu_read_lock();
2403         cb->seq = net->nft.base_seq;
2404
2405         list_for_each_entry_rcu(table, &net->nft.tables, list) {
2406                 if (family != NFPROTO_UNSPEC && family != table->family)
2407                         continue;
2408
2409                 if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
2410                         continue;
2411
2412                 if (ctx && ctx->table && ctx->chain) {
2413                         struct rhlist_head *list, *tmp;
2414
2415                         list = rhltable_lookup(&table->chains_ht, ctx->chain,
2416                                                nft_chain_ht_params);
2417                         if (!list)
2418                                 goto done;
2419
2420                         rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
2421                                 if (!nft_is_active(net, chain))
2422                                         continue;
2423                                 __nf_tables_dump_rules(skb, &idx,
2424                                                        cb, table, chain);
2425                                 break;
2426                         }
2427                         goto done;
2428                 }
2429
2430                 list_for_each_entry_rcu(chain, &table->chains, list) {
2431                         if (__nf_tables_dump_rules(skb, &idx, cb, table, chain))
2432                                 goto done;
2433                 }
2434
2435                 if (ctx && ctx->table)
2436                         break;
2437         }
2438 done:
2439         rcu_read_unlock();
2440
2441         cb->args[0] = idx;
2442         return skb->len;
2443 }
2444
2445 static int nf_tables_dump_rules_start(struct netlink_callback *cb)
2446 {
2447         const struct nlattr * const *nla = cb->data;
2448         struct nft_rule_dump_ctx *ctx = NULL;
2449
2450         if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
2451                 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
2452                 if (!ctx)
2453                         return -ENOMEM;
2454
2455                 if (nla[NFTA_RULE_TABLE]) {
2456                         ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
2457                                                         GFP_ATOMIC);
2458                         if (!ctx->table) {
2459                                 kfree(ctx);
2460                                 return -ENOMEM;
2461                         }
2462                 }
2463                 if (nla[NFTA_RULE_CHAIN]) {
2464                         ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
2465                                                 GFP_ATOMIC);
2466                         if (!ctx->chain) {
2467                                 kfree(ctx->table);
2468                                 kfree(ctx);
2469                                 return -ENOMEM;
2470                         }
2471                 }
2472         }
2473
2474         cb->data = ctx;
2475         return 0;
2476 }
2477
2478 static int nf_tables_dump_rules_done(struct netlink_callback *cb)
2479 {
2480         struct nft_rule_dump_ctx *ctx = cb->data;
2481
2482         if (ctx) {
2483                 kfree(ctx->table);
2484                 kfree(ctx->chain);
2485                 kfree(ctx);
2486         }
2487         return 0;
2488 }
2489
2490 /* called with rcu_read_lock held */
2491 static int nf_tables_getrule(struct net *net, struct sock *nlsk,
2492                              struct sk_buff *skb, const struct nlmsghdr *nlh,
2493                              const struct nlattr * const nla[],
2494                              struct netlink_ext_ack *extack)
2495 {
2496         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2497         u8 genmask = nft_genmask_cur(net);
2498         const struct nft_chain *chain;
2499         const struct nft_rule *rule;
2500         struct nft_table *table;
2501         struct sk_buff *skb2;
2502         int family = nfmsg->nfgen_family;
2503         int err;
2504
2505         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2506                 struct netlink_dump_control c = {
2507                         .start= nf_tables_dump_rules_start,
2508                         .dump = nf_tables_dump_rules,
2509                         .done = nf_tables_dump_rules_done,
2510                         .module = THIS_MODULE,
2511                         .data = (void *)nla,
2512                 };
2513
2514                 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
2515         }
2516
2517         table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
2518         if (IS_ERR(table)) {
2519                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
2520                 return PTR_ERR(table);
2521         }
2522
2523         chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
2524         if (IS_ERR(chain)) {
2525                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
2526                 return PTR_ERR(chain);
2527         }
2528
2529         rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
2530         if (IS_ERR(rule)) {
2531                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2532                 return PTR_ERR(rule);
2533         }
2534
2535         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
2536         if (!skb2)
2537                 return -ENOMEM;
2538
2539         err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
2540                                        nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
2541                                        family, table, chain, rule, NULL);
2542         if (err < 0)
2543                 goto err;
2544
2545         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2546
2547 err:
2548         kfree_skb(skb2);
2549         return err;
2550 }
2551
2552 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
2553                                    struct nft_rule *rule)
2554 {
2555         struct nft_expr *expr, *next;
2556
2557         /*
2558          * Careful: some expressions might not be initialized in case this
2559          * is called on error from nf_tables_newrule().
2560          */
2561         expr = nft_expr_first(rule);
2562         while (expr != nft_expr_last(rule) && expr->ops) {
2563                 next = nft_expr_next(expr);
2564                 nf_tables_expr_destroy(ctx, expr);
2565                 expr = next;
2566         }
2567         kfree(rule);
2568 }
2569
2570 static void nf_tables_rule_release(const struct nft_ctx *ctx,
2571                                    struct nft_rule *rule)
2572 {
2573         nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
2574         nf_tables_rule_destroy(ctx, rule);
2575 }
2576
2577 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
2578 {
2579         struct nft_expr *expr, *last;
2580         const struct nft_data *data;
2581         struct nft_rule *rule;
2582         int err;
2583
2584         if (ctx->level == NFT_JUMP_STACK_SIZE)
2585                 return -EMLINK;
2586
2587         list_for_each_entry(rule, &chain->rules, list) {
2588                 if (!nft_is_active_next(ctx->net, rule))
2589                         continue;
2590
2591                 nft_rule_for_each_expr(expr, last, rule) {
2592                         if (!expr->ops->validate)
2593                                 continue;
2594
2595                         err = expr->ops->validate(ctx, expr, &data);
2596                         if (err < 0)
2597                                 return err;
2598                 }
2599         }
2600
2601         return 0;
2602 }
2603 EXPORT_SYMBOL_GPL(nft_chain_validate);
2604
2605 static int nft_table_validate(struct net *net, const struct nft_table *table)
2606 {
2607         struct nft_chain *chain;
2608         struct nft_ctx ctx = {
2609                 .net    = net,
2610                 .family = table->family,
2611         };
2612         int err;
2613
2614         list_for_each_entry(chain, &table->chains, list) {
2615                 if (!nft_is_base_chain(chain))
2616                         continue;
2617
2618                 ctx.chain = chain;
2619                 err = nft_chain_validate(&ctx, chain);
2620                 if (err < 0)
2621                         return err;
2622         }
2623
2624         return 0;
2625 }
2626
2627 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
2628                                              const struct nlattr *nla);
2629
2630 #define NFT_RULE_MAXEXPRS       128
2631
2632 static int nf_tables_newrule(struct net *net, struct sock *nlsk,
2633                              struct sk_buff *skb, const struct nlmsghdr *nlh,
2634                              const struct nlattr * const nla[],
2635                              struct netlink_ext_ack *extack)
2636 {
2637         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2638         u8 genmask = nft_genmask_next(net);
2639         struct nft_expr_info *info = NULL;
2640         int family = nfmsg->nfgen_family;
2641         struct nft_table *table;
2642         struct nft_chain *chain;
2643         struct nft_rule *rule, *old_rule = NULL;
2644         struct nft_userdata *udata;
2645         struct nft_trans *trans = NULL;
2646         struct nft_expr *expr;
2647         struct nft_ctx ctx;
2648         struct nlattr *tmp;
2649         unsigned int size, i, n, ulen = 0, usize = 0;
2650         int err, rem;
2651         u64 handle, pos_handle;
2652
2653         lockdep_assert_held(&net->nft.commit_mutex);
2654
2655         table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
2656         if (IS_ERR(table)) {
2657                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
2658                 return PTR_ERR(table);
2659         }
2660
2661         chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
2662         if (IS_ERR(chain)) {
2663                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
2664                 return PTR_ERR(chain);
2665         }
2666
2667         if (nla[NFTA_RULE_HANDLE]) {
2668                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2669                 rule = __nft_rule_lookup(chain, handle);
2670                 if (IS_ERR(rule)) {
2671                         NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2672                         return PTR_ERR(rule);
2673                 }
2674
2675                 if (nlh->nlmsg_flags & NLM_F_EXCL) {
2676                         NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2677                         return -EEXIST;
2678                 }
2679                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2680                         old_rule = rule;
2681                 else
2682                         return -EOPNOTSUPP;
2683         } else {
2684                 if (!(nlh->nlmsg_flags & NLM_F_CREATE) ||
2685                     nlh->nlmsg_flags & NLM_F_REPLACE)
2686                         return -EINVAL;
2687                 handle = nf_tables_alloc_handle(table);
2688
2689                 if (chain->use == UINT_MAX)
2690                         return -EOVERFLOW;
2691
2692                 if (nla[NFTA_RULE_POSITION]) {
2693                         pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2694                         old_rule = __nft_rule_lookup(chain, pos_handle);
2695                         if (IS_ERR(old_rule)) {
2696                                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
2697                                 return PTR_ERR(old_rule);
2698                         }
2699                 } else if (nla[NFTA_RULE_POSITION_ID]) {
2700                         old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]);
2701                         if (IS_ERR(old_rule)) {
2702                                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
2703                                 return PTR_ERR(old_rule);
2704                         }
2705                 }
2706         }
2707
2708         nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
2709
2710         n = 0;
2711         size = 0;
2712         if (nla[NFTA_RULE_EXPRESSIONS]) {
2713                 info = kvmalloc_array(NFT_RULE_MAXEXPRS,
2714                                       sizeof(struct nft_expr_info),
2715                                       GFP_KERNEL);
2716                 if (!info)
2717                         return -ENOMEM;
2718
2719                 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2720                         err = -EINVAL;
2721                         if (nla_type(tmp) != NFTA_LIST_ELEM)
2722                                 goto err1;
2723                         if (n == NFT_RULE_MAXEXPRS)
2724                                 goto err1;
2725                         err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
2726                         if (err < 0)
2727                                 goto err1;
2728                         size += info[n].ops->size;
2729                         n++;
2730                 }
2731         }
2732         /* Check for overflow of dlen field */
2733         err = -EFBIG;
2734         if (size >= 1 << 12)
2735                 goto err1;
2736
2737         if (nla[NFTA_RULE_USERDATA]) {
2738                 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
2739                 if (ulen > 0)
2740                         usize = sizeof(struct nft_userdata) + ulen;
2741         }
2742
2743         err = -ENOMEM;
2744         rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
2745         if (rule == NULL)
2746                 goto err1;
2747
2748         nft_activate_next(net, rule);
2749
2750         rule->handle = handle;
2751         rule->dlen   = size;
2752         rule->udata  = ulen ? 1 : 0;
2753
2754         if (ulen) {
2755                 udata = nft_userdata(rule);
2756                 udata->len = ulen - 1;
2757                 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2758         }
2759
2760         expr = nft_expr_first(rule);
2761         for (i = 0; i < n; i++) {
2762                 err = nf_tables_newexpr(&ctx, &info[i], expr);
2763                 if (err < 0)
2764                         goto err2;
2765
2766                 if (info[i].ops->validate)
2767                         nft_validate_state_update(net, NFT_VALIDATE_NEED);
2768
2769                 info[i].ops = NULL;
2770                 expr = nft_expr_next(expr);
2771         }
2772
2773         if (nlh->nlmsg_flags & NLM_F_REPLACE) {
2774                 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
2775                 if (trans == NULL) {
2776                         err = -ENOMEM;
2777                         goto err2;
2778                 }
2779                 err = nft_delrule(&ctx, old_rule);
2780                 if (err < 0) {
2781                         nft_trans_destroy(trans);
2782                         goto err2;
2783                 }
2784
2785                 list_add_tail_rcu(&rule->list, &old_rule->list);
2786         } else {
2787                 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2788                         err = -ENOMEM;
2789                         goto err2;
2790                 }
2791
2792                 if (nlh->nlmsg_flags & NLM_F_APPEND) {
2793                         if (old_rule)
2794                                 list_add_rcu(&rule->list, &old_rule->list);
2795                         else
2796                                 list_add_tail_rcu(&rule->list, &chain->rules);
2797                  } else {
2798                         if (old_rule)
2799                                 list_add_tail_rcu(&rule->list, &old_rule->list);
2800                         else
2801                                 list_add_rcu(&rule->list, &chain->rules);
2802                 }
2803         }
2804         kvfree(info);
2805         chain->use++;
2806
2807         if (net->nft.validate_state == NFT_VALIDATE_DO)
2808                 return nft_table_validate(net, table);
2809
2810         return 0;
2811 err2:
2812         nf_tables_rule_release(&ctx, rule);
2813 err1:
2814         for (i = 0; i < n; i++) {
2815                 if (info[i].ops) {
2816                         module_put(info[i].ops->type->owner);
2817                         if (info[i].ops->type->release_ops)
2818                                 info[i].ops->type->release_ops(info[i].ops);
2819                 }
2820         }
2821         kvfree(info);
2822         return err;
2823 }
2824
2825 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
2826                                              const struct nlattr *nla)
2827 {
2828         u32 id = ntohl(nla_get_be32(nla));
2829         struct nft_trans *trans;
2830
2831         list_for_each_entry(trans, &net->nft.commit_list, list) {
2832                 struct nft_rule *rule = nft_trans_rule(trans);
2833
2834                 if (trans->msg_type == NFT_MSG_NEWRULE &&
2835                     id == nft_trans_rule_id(trans))
2836                         return rule;
2837         }
2838         return ERR_PTR(-ENOENT);
2839 }
2840
2841 static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2842                              struct sk_buff *skb, const struct nlmsghdr *nlh,
2843                              const struct nlattr * const nla[],
2844                              struct netlink_ext_ack *extack)
2845 {
2846         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2847         u8 genmask = nft_genmask_next(net);
2848         struct nft_table *table;
2849         struct nft_chain *chain = NULL;
2850         struct nft_rule *rule;
2851         int family = nfmsg->nfgen_family, err = 0;
2852         struct nft_ctx ctx;
2853
2854         table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
2855         if (IS_ERR(table)) {
2856                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
2857                 return PTR_ERR(table);
2858         }
2859
2860         if (nla[NFTA_RULE_CHAIN]) {
2861                 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
2862                                          genmask);
2863                 if (IS_ERR(chain)) {
2864                         NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
2865                         return PTR_ERR(chain);
2866                 }
2867         }
2868
2869         nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
2870
2871         if (chain) {
2872                 if (nla[NFTA_RULE_HANDLE]) {
2873                         rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
2874                         if (IS_ERR(rule)) {
2875                                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
2876                                 return PTR_ERR(rule);
2877                         }
2878
2879                         err = nft_delrule(&ctx, rule);
2880                 } else if (nla[NFTA_RULE_ID]) {
2881                         rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]);
2882                         if (IS_ERR(rule)) {
2883                                 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
2884                                 return PTR_ERR(rule);
2885                         }
2886
2887                         err = nft_delrule(&ctx, rule);
2888                 } else {
2889                         err = nft_delrule_by_chain(&ctx);
2890                 }
2891         } else {
2892                 list_for_each_entry(chain, &table->chains, list) {
2893                         if (!nft_is_active_next(net, chain))
2894                                 continue;
2895
2896                         ctx.chain = chain;
2897                         err = nft_delrule_by_chain(&ctx);
2898                         if (err < 0)
2899                                 break;
2900                 }
2901         }
2902
2903         return err;
2904 }
2905
2906 /*
2907  * Sets
2908  */
2909
2910 static LIST_HEAD(nf_tables_set_types);
2911
2912 int nft_register_set(struct nft_set_type *type)
2913 {
2914         nfnl_lock(NFNL_SUBSYS_NFTABLES);
2915         list_add_tail_rcu(&type->list, &nf_tables_set_types);
2916         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2917         return 0;
2918 }
2919 EXPORT_SYMBOL_GPL(nft_register_set);
2920
2921 void nft_unregister_set(struct nft_set_type *type)
2922 {
2923         nfnl_lock(NFNL_SUBSYS_NFTABLES);
2924         list_del_rcu(&type->list);
2925         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2926 }
2927 EXPORT_SYMBOL_GPL(nft_unregister_set);
2928
2929 #define NFT_SET_FEATURES        (NFT_SET_INTERVAL | NFT_SET_MAP | \
2930                                  NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
2931                                  NFT_SET_EVAL)
2932
2933 static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
2934 {
2935         return (flags & type->features) == (flags & NFT_SET_FEATURES);
2936 }
2937
2938 /*
2939  * Select a set implementation based on the data characteristics and the
2940  * given policy. The total memory use might not be known if no size is
2941  * given, in that case the amount of memory per element is used.
2942  */
2943 static const struct nft_set_ops *
2944 nft_select_set_ops(const struct nft_ctx *ctx,
2945                    const struct nlattr * const nla[],
2946                    const struct nft_set_desc *desc,
2947                    enum nft_set_policies policy)
2948 {
2949         const struct nft_set_ops *ops, *bops;
2950         struct nft_set_estimate est, best;
2951         const struct nft_set_type *type;
2952         u32 flags = 0;
2953
2954         lockdep_assert_held(&ctx->net->nft.commit_mutex);
2955         lockdep_nfnl_nft_mutex_not_held();
2956 #ifdef CONFIG_MODULES
2957         if (list_empty(&nf_tables_set_types)) {
2958                 nft_request_module(ctx->net, "nft-set");
2959                 if (!list_empty(&nf_tables_set_types))
2960                         return ERR_PTR(-EAGAIN);
2961         }
2962 #endif
2963         if (nla[NFTA_SET_FLAGS] != NULL)
2964                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2965
2966         bops        = NULL;
2967         best.size   = ~0;
2968         best.lookup = ~0;
2969         best.space  = ~0;
2970
2971         list_for_each_entry(type, &nf_tables_set_types, list) {
2972                 ops = &type->ops;
2973
2974                 if (!nft_set_ops_candidate(type, flags))
2975                         continue;
2976                 if (!ops->estimate(desc, flags, &est))
2977                         continue;
2978
2979                 switch (policy) {
2980                 case NFT_SET_POL_PERFORMANCE:
2981                         if (est.lookup < best.lookup)
2982                                 break;
2983                         if (est.lookup == best.lookup &&
2984                             est.space < best.space)
2985                                 break;
2986                         continue;
2987                 case NFT_SET_POL_MEMORY:
2988                         if (!desc->size) {
2989                                 if (est.space < best.space)
2990                                         break;
2991                                 if (est.space == best.space &&
2992                                     est.lookup < best.lookup)
2993                                         break;
2994                         } else if (est.size < best.size || !bops) {
2995                                 break;
2996                         }
2997                         continue;
2998                 default:
2999                         break;
3000                 }
3001
3002                 if (!try_module_get(type->owner))
3003                         continue;
3004                 if (bops != NULL)
3005                         module_put(to_set_type(bops)->owner);
3006
3007                 bops = ops;
3008                 best = est;
3009         }
3010
3011         if (bops != NULL)
3012                 return bops;
3013
3014         return ERR_PTR(-EOPNOTSUPP);
3015 }
3016
3017 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
3018         [NFTA_SET_TABLE]                = { .type = NLA_STRING,
3019                                             .len = NFT_TABLE_MAXNAMELEN - 1 },
3020         [NFTA_SET_NAME]                 = { .type = NLA_STRING,
3021                                             .len = NFT_SET_MAXNAMELEN - 1 },
3022         [NFTA_SET_FLAGS]                = { .type = NLA_U32 },
3023         [NFTA_SET_KEY_TYPE]             = { .type = NLA_U32 },
3024         [NFTA_SET_KEY_LEN]              = { .type = NLA_U32 },
3025         [NFTA_SET_DATA_TYPE]            = { .type = NLA_U32 },
3026         [NFTA_SET_DATA_LEN]             = { .type = NLA_U32 },
3027         [NFTA_SET_POLICY]               = { .type = NLA_U32 },
3028         [NFTA_SET_DESC]                 = { .type = NLA_NESTED },
3029         [NFTA_SET_ID]                   = { .type = NLA_U32 },
3030         [NFTA_SET_TIMEOUT]              = { .type = NLA_U64 },
3031         [NFTA_SET_GC_INTERVAL]          = { .type = NLA_U32 },
3032         [NFTA_SET_USERDATA]             = { .type = NLA_BINARY,
3033                                             .len  = NFT_USERDATA_MAXLEN },
3034         [NFTA_SET_OBJ_TYPE]             = { .type = NLA_U32 },
3035         [NFTA_SET_HANDLE]               = { .type = NLA_U64 },
3036 };
3037
3038 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
3039         [NFTA_SET_DESC_SIZE]            = { .type = NLA_U32 },
3040 };
3041
3042 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
3043                                      const struct sk_buff *skb,
3044                                      const struct nlmsghdr *nlh,
3045                                      const struct nlattr * const nla[],
3046                                      struct netlink_ext_ack *extack,
3047                                      u8 genmask)
3048 {
3049         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3050         int family = nfmsg->nfgen_family;
3051         struct nft_table *table = NULL;
3052
3053         if (nla[NFTA_SET_TABLE] != NULL) {
3054                 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
3055                                          genmask);
3056                 if (IS_ERR(table)) {
3057                         NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
3058                         return PTR_ERR(table);
3059                 }
3060         }
3061
3062         nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
3063         return 0;
3064 }
3065
3066 static struct nft_set *nft_set_lookup(const struct nft_table *table,
3067                                       const struct nlattr *nla, u8 genmask)
3068 {
3069         struct nft_set *set;
3070
3071         if (nla == NULL)
3072                 return ERR_PTR(-EINVAL);
3073
3074         list_for_each_entry_rcu(set, &table->sets, list) {
3075                 if (!nla_strcmp(nla, set->name) &&
3076                     nft_active_genmask(set, genmask))
3077                         return set;
3078         }
3079         return ERR_PTR(-ENOENT);
3080 }
3081
3082 static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
3083                                                const struct nlattr *nla,
3084                                                u8 genmask)
3085 {
3086         struct nft_set *set;
3087
3088         list_for_each_entry(set, &table->sets, list) {
3089                 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
3090                     nft_active_genmask(set, genmask))
3091                         return set;
3092         }
3093         return ERR_PTR(-ENOENT);
3094 }
3095
3096 static struct nft_set *nft_set_lookup_byid(const struct net *net,
3097                                            const struct nlattr *nla, u8 genmask)
3098 {
3099         struct nft_trans *trans;
3100         u32 id = ntohl(nla_get_be32(nla));
3101
3102         list_for_each_entry(trans, &net->nft.commit_list, list) {
3103                 if (trans->msg_type == NFT_MSG_NEWSET) {
3104                         struct nft_set *set = nft_trans_set(trans);
3105
3106                         if (id == nft_trans_set_id(trans) &&
3107                             nft_active_genmask(set, genmask))
3108                                 return set;
3109                 }
3110         }
3111         return ERR_PTR(-ENOENT);
3112 }
3113
3114 struct nft_set *nft_set_lookup_global(const struct net *net,
3115                                       const struct nft_table *table,
3116                                       const struct nlattr *nla_set_name,
3117                                       const struct nlattr *nla_set_id,
3118                                       u8 genmask)
3119 {
3120         struct nft_set *set;
3121
3122         set = nft_set_lookup(table, nla_set_name, genmask);
3123         if (IS_ERR(set)) {
3124                 if (!nla_set_id)
3125                         return set;
3126
3127                 set = nft_set_lookup_byid(net, nla_set_id, genmask);
3128         }
3129         return set;
3130 }
3131 EXPORT_SYMBOL_GPL(nft_set_lookup_global);
3132
3133 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
3134                                     const char *name)
3135 {
3136         const struct nft_set *i;
3137         const char *p;
3138         unsigned long *inuse;
3139         unsigned int n = 0, min = 0;
3140
3141         p = strchr(name, '%');
3142         if (p != NULL) {
3143                 if (p[1] != 'd' || strchr(p + 2, '%'))
3144                         return -EINVAL;
3145
3146                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
3147                 if (inuse == NULL)
3148                         return -ENOMEM;
3149 cont:
3150                 list_for_each_entry(i, &ctx->table->sets, list) {
3151                         int tmp;
3152
3153                         if (!nft_is_active_next(ctx->net, set))
3154                                 continue;
3155                         if (!sscanf(i->name, name, &tmp))
3156                                 continue;
3157                         if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
3158                                 continue;
3159
3160                         set_bit(tmp - min, inuse);
3161                 }
3162
3163                 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
3164                 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
3165                         min += BITS_PER_BYTE * PAGE_SIZE;
3166                         memset(inuse, 0, PAGE_SIZE);
3167                         goto cont;
3168                 }
3169                 free_page((unsigned long)inuse);
3170         }
3171
3172         set->name = kasprintf(GFP_KERNEL, name, min + n);
3173         if (!set->name)
3174                 return -ENOMEM;
3175
3176         list_for_each_entry(i, &ctx->table->sets, list) {
3177                 if (!nft_is_active_next(ctx->net, i))
3178                         continue;
3179                 if (!strcmp(set->name, i->name)) {
3180                         kfree(set->name);
3181                         return -ENFILE;
3182                 }
3183         }
3184         return 0;
3185 }
3186
3187 static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
3188 {
3189         u64 ms = be64_to_cpu(nla_get_be64(nla));
3190         u64 max = (u64)(~((u64)0));
3191
3192         max = div_u64(max, NSEC_PER_MSEC);
3193         if (ms >= max)
3194                 return -ERANGE;
3195
3196         ms *= NSEC_PER_MSEC;
3197         *result = nsecs_to_jiffies64(ms);
3198         return 0;
3199 }
3200
3201 static __be64 nf_jiffies64_to_msecs(u64 input)
3202 {
3203         return cpu_to_be64(jiffies64_to_msecs(input));
3204 }
3205
3206 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
3207                               const struct nft_set *set, u16 event, u16 flags)
3208 {
3209         struct nfgenmsg *nfmsg;
3210         struct nlmsghdr *nlh;
3211         struct nlattr *desc;
3212         u32 portid = ctx->portid;
3213         u32 seq = ctx->seq;
3214
3215         event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
3216         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3217                         flags);
3218         if (nlh == NULL)
3219                 goto nla_put_failure;
3220
3221         nfmsg = nlmsg_data(nlh);
3222         nfmsg->nfgen_family     = ctx->family;
3223         nfmsg->version          = NFNETLINK_V0;
3224         nfmsg->res_id           = htons(ctx->net->nft.base_seq & 0xffff);
3225
3226         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3227                 goto nla_put_failure;
3228         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3229                 goto nla_put_failure;
3230         if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
3231                          NFTA_SET_PAD))
3232                 goto nla_put_failure;
3233         if (set->flags != 0)
3234                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
3235                         goto nla_put_failure;
3236
3237         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
3238                 goto nla_put_failure;
3239         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
3240                 goto nla_put_failure;
3241         if (set->flags & NFT_SET_MAP) {
3242                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
3243                         goto nla_put_failure;
3244                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
3245                         goto nla_put_failure;
3246         }
3247         if (set->flags & NFT_SET_OBJECT &&
3248             nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
3249                 goto nla_put_failure;
3250
3251         if (set->timeout &&
3252             nla_put_be64(skb, NFTA_SET_TIMEOUT,
3253                          nf_jiffies64_to_msecs(set->timeout),
3254                          NFTA_SET_PAD))
3255                 goto nla_put_failure;
3256         if (set->gc_int &&
3257             nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
3258                 goto nla_put_failure;
3259
3260         if (set->policy != NFT_SET_POL_PERFORMANCE) {
3261                 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
3262                         goto nla_put_failure;
3263         }
3264
3265         if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
3266                 goto nla_put_failure;
3267
3268         desc = nla_nest_start_noflag(skb, NFTA_SET_DESC);
3269         if (desc == NULL)
3270                 goto nla_put_failure;
3271         if (set->size &&
3272             nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
3273                 goto nla_put_failure;
3274         nla_nest_end(skb, desc);
3275
3276         nlmsg_end(skb, nlh);
3277         return 0;
3278
3279 nla_put_failure:
3280         nlmsg_trim(skb, nlh);
3281         return -1;
3282 }
3283
3284 static void nf_tables_set_notify(const struct nft_ctx *ctx,
3285                                  const struct nft_set *set, int event,
3286                                  gfp_t gfp_flags)
3287 {
3288         struct sk_buff *skb;
3289         u32 portid = ctx->portid;
3290         int err;
3291
3292         if (!ctx->report &&
3293             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
3294                 return;
3295
3296         skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
3297         if (skb == NULL)
3298                 goto err;
3299
3300         err = nf_tables_fill_set(skb, ctx, set, event, 0);
3301         if (err < 0) {
3302                 kfree_skb(skb);
3303                 goto err;
3304         }
3305
3306         nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report,
3307                        gfp_flags);
3308         return;
3309 err:
3310         nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
3311 }
3312
3313 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
3314 {
3315         const struct nft_set *set;
3316         unsigned int idx, s_idx = cb->args[0];
3317         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
3318         struct net *net = sock_net(skb->sk);
3319         struct nft_ctx *ctx = cb->data, ctx_set;
3320
3321         if (cb->args[1])
3322                 return skb->len;
3323
3324         rcu_read_lock();
3325         cb->seq = net->nft.base_seq;
3326
3327         list_for_each_entry_rcu(table, &net->nft.tables, list) {
3328                 if (ctx->family != NFPROTO_UNSPEC &&
3329                     ctx->family != table->family)
3330                         continue;
3331
3332                 if (ctx->table && ctx->table != table)
3333                         continue;
3334
3335                 if (cur_table) {
3336                         if (cur_table != table)
3337                                 continue;
3338
3339                         cur_table = NULL;
3340                 }
3341                 idx = 0;
3342                 list_for_each_entry_rcu(set, &table->sets, list) {
3343                         if (idx < s_idx)
3344                                 goto cont;
3345                         if (!nft_is_active(net, set))
3346                                 goto cont;
3347
3348                         ctx_set = *ctx;
3349                         ctx_set.table = table;
3350                         ctx_set.family = table->family;
3351
3352                         if (nf_tables_fill_set(skb, &ctx_set, set,
3353                                                NFT_MSG_NEWSET,
3354                                                NLM_F_MULTI) < 0) {
3355                                 cb->args[0] = idx;
3356                                 cb->args[2] = (unsigned long) table;
3357                                 goto done;
3358                         }
3359                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3360 cont:
3361                         idx++;
3362                 }
3363                 if (s_idx)
3364                         s_idx = 0;
3365         }
3366         cb->args[1] = 1;
3367 done:
3368         rcu_read_unlock();
3369         return skb->len;
3370 }
3371
3372 static int nf_tables_dump_sets_start(struct netlink_callback *cb)
3373 {
3374         struct nft_ctx *ctx_dump = NULL;
3375
3376         ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
3377         if (ctx_dump == NULL)
3378                 return -ENOMEM;
3379
3380         cb->data = ctx_dump;
3381         return 0;
3382 }
3383
3384 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
3385 {
3386         kfree(cb->data);
3387         return 0;
3388 }
3389
3390 /* called with rcu_read_lock held */
3391 static int nf_tables_getset(struct net *net, struct sock *nlsk,
3392                             struct sk_buff *skb, const struct nlmsghdr *nlh,
3393                             const struct nlattr * const nla[],
3394                             struct netlink_ext_ack *extack)
3395 {
3396         u8 genmask = nft_genmask_cur(net);
3397         const struct nft_set *set;
3398         struct nft_ctx ctx;
3399         struct sk_buff *skb2;
3400         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3401         int err;
3402
3403         /* Verify existence before starting dump */
3404         err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
3405                                         genmask);
3406         if (err < 0)
3407                 return err;
3408
3409         if (nlh->nlmsg_flags & NLM_F_DUMP) {
3410                 struct netlink_dump_control c = {
3411                         .start = nf_tables_dump_sets_start,
3412                         .dump = nf_tables_dump_sets,
3413                         .done = nf_tables_dump_sets_done,
3414                         .data = &ctx,
3415                         .module = THIS_MODULE,
3416                 };
3417
3418                 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
3419         }
3420
3421         /* Only accept unspec with dump */
3422         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
3423                 return -EAFNOSUPPORT;
3424         if (!nla[NFTA_SET_TABLE])
3425                 return -EINVAL;
3426
3427         set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
3428         if (IS_ERR(set))
3429                 return PTR_ERR(set);
3430
3431         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3432         if (skb2 == NULL)
3433                 return -ENOMEM;
3434
3435         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
3436         if (err < 0)
3437                 goto err;
3438
3439         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3440
3441 err:
3442         kfree_skb(skb2);
3443         return err;
3444 }
3445
3446 static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
3447                                     const struct nlattr *nla)
3448 {
3449         struct nlattr *da[NFTA_SET_DESC_MAX + 1];
3450         int err;
3451
3452         err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
3453                                           nft_set_desc_policy, NULL);
3454         if (err < 0)
3455                 return err;
3456
3457         if (da[NFTA_SET_DESC_SIZE] != NULL)
3458                 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
3459
3460         return 0;
3461 }
3462
3463 static int nf_tables_newset(struct net *net, struct sock *nlsk,
3464                             struct sk_buff *skb, const struct nlmsghdr *nlh,
3465                             const struct nlattr * const nla[],
3466                             struct netlink_ext_ack *extack)
3467 {
3468         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3469         u8 genmask = nft_genmask_next(net);
3470         int family = nfmsg->nfgen_family;
3471         const struct nft_set_ops *ops;
3472         struct nft_table *table;
3473         struct nft_set *set;
3474         struct nft_ctx ctx;
3475         char *name;
3476         u64 size;
3477         u64 timeout;
3478         u32 ktype, dtype, flags, policy, gc_int, objtype;
3479         struct nft_set_desc desc;
3480         unsigned char *udata;
3481         u16 udlen;
3482         int err;
3483
3484         if (nla[NFTA_SET_TABLE] == NULL ||
3485             nla[NFTA_SET_NAME] == NULL ||
3486             nla[NFTA_SET_KEY_LEN] == NULL ||
3487             nla[NFTA_SET_ID] == NULL)
3488                 return -EINVAL;
3489
3490         memset(&desc, 0, sizeof(desc));
3491
3492         ktype = NFT_DATA_VALUE;
3493         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
3494                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
3495                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
3496                         return -EINVAL;
3497         }
3498
3499         desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
3500         if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
3501                 return -EINVAL;
3502
3503         flags = 0;
3504         if (nla[NFTA_SET_FLAGS] != NULL) {
3505                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
3506                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
3507                               NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
3508                               NFT_SET_MAP | NFT_SET_EVAL |
3509                               NFT_SET_OBJECT))
3510                         return -EINVAL;
3511                 /* Only one of these operations is supported */
3512                 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) ==
3513                              (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT))
3514                         return -EOPNOTSUPP;
3515         }
3516
3517         dtype = 0;
3518         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
3519                 if (!(flags & NFT_SET_MAP))
3520                         return -EINVAL;
3521
3522                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
3523                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
3524                     dtype != NFT_DATA_VERDICT)
3525                         return -EINVAL;
3526
3527                 if (dtype != NFT_DATA_VERDICT) {
3528                         if (nla[NFTA_SET_DATA_LEN] == NULL)
3529                                 return -EINVAL;
3530                         desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
3531                         if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
3532                                 return -EINVAL;
3533                 } else
3534                         desc.dlen = sizeof(struct nft_verdict);
3535         } else if (flags & NFT_SET_MAP)
3536                 return -EINVAL;
3537
3538         if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
3539                 if (!(flags & NFT_SET_OBJECT))
3540                         return -EINVAL;
3541
3542                 objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
3543                 if (objtype == NFT_OBJECT_UNSPEC ||
3544                     objtype > NFT_OBJECT_MAX)
3545                         return -EINVAL;
3546         } else if (flags & NFT_SET_OBJECT)
3547                 return -EINVAL;
3548         else
3549                 objtype = NFT_OBJECT_UNSPEC;
3550
3551         timeout = 0;
3552         if (nla[NFTA_SET_TIMEOUT] != NULL) {
3553                 if (!(flags & NFT_SET_TIMEOUT))
3554                         return -EINVAL;
3555
3556                 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout);
3557                 if (err)
3558                         return err;
3559         }
3560         gc_int = 0;
3561         if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
3562                 if (!(flags & NFT_SET_TIMEOUT))
3563                         return -EINVAL;
3564                 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
3565         }
3566
3567         policy = NFT_SET_POL_PERFORMANCE;
3568         if (nla[NFTA_SET_POLICY] != NULL)
3569                 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
3570
3571         if (nla[NFTA_SET_DESC] != NULL) {
3572                 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
3573                 if (err < 0)
3574                         return err;
3575         }
3576
3577         table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask);
3578         if (IS_ERR(table)) {
3579                 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
3580                 return PTR_ERR(table);
3581         }
3582
3583         nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
3584
3585         set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
3586         if (IS_ERR(set)) {
3587                 if (PTR_ERR(set) != -ENOENT) {
3588                         NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
3589                         return PTR_ERR(set);
3590                 }
3591         } else {
3592                 if (nlh->nlmsg_flags & NLM_F_EXCL) {
3593                         NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
3594                         return -EEXIST;
3595                 }
3596                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3597                         return -EOPNOTSUPP;
3598
3599                 return 0;
3600         }
3601
3602         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
3603                 return -ENOENT;
3604
3605         ops = nft_select_set_ops(&ctx, nla, &desc, policy);
3606         if (IS_ERR(ops))
3607                 return PTR_ERR(ops);
3608
3609         udlen = 0;
3610         if (nla[NFTA_SET_USERDATA])
3611                 udlen = nla_len(nla[NFTA_SET_USERDATA]);
3612
3613         size = 0;
3614         if (ops->privsize != NULL)
3615                 size = ops->privsize(nla, &desc);
3616
3617         set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
3618         if (!set) {
3619                 err = -ENOMEM;
3620                 goto err1;
3621         }
3622
3623         name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL);
3624         if (!name) {
3625                 err = -ENOMEM;
3626                 goto err2;
3627         }
3628
3629         err = nf_tables_set_alloc_name(&ctx, set, name);
3630         kfree(name);
3631         if (err < 0)
3632                 goto err2;
3633
3634         udata = NULL;
3635         if (udlen) {
3636                 udata = set->data + size;
3637                 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
3638         }
3639
3640         INIT_LIST_HEAD(&set->bindings);
3641         set->table = table;
3642         write_pnet(&set->net, net);
3643         set->ops   = ops;
3644         set->ktype = ktype;
3645         set->klen  = desc.klen;
3646         set->dtype = dtype;
3647         set->objtype = objtype;
3648         set->dlen  = desc.dlen;
3649         set->flags = flags;
3650         set->size  = desc.size;
3651         set->policy = policy;
3652         set->udlen  = udlen;
3653         set->udata  = udata;
3654         set->timeout = timeout;
3655         set->gc_int = gc_int;
3656         set->handle = nf_tables_alloc_handle(table);
3657
3658         err = ops->init(set, &desc, nla);
3659         if (err < 0)
3660                 goto err3;
3661
3662         err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
3663         if (err < 0)
3664                 goto err4;
3665
3666         list_add_tail_rcu(&set->list, &table->sets);
3667         table->use++;
3668         return 0;
3669
3670 err4:
3671         ops->destroy(set);
3672 err3:
3673         kfree(set->name);
3674 err2:
3675         kvfree(set);
3676 err1:
3677         module_put(to_set_type(ops)->owner);
3678         return err;
3679 }
3680
3681 static void nft_set_destroy(struct nft_set *set)
3682 {
3683         if (WARN_ON(set->use > 0))
3684                 return;
3685
3686         set->ops->destroy(set);
3687         module_put(to_set_type(set->ops)->owner);
3688         kfree(set->name);
3689         kvfree(set);
3690 }
3691
3692 static int nf_tables_delset(struct net *net, struct sock *nlsk,
3693                             struct sk_buff *skb, const struct nlmsghdr *nlh,
3694                             const struct nlattr * const nla[],
3695                             struct netlink_ext_ack *extack)
3696 {
3697         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3698         u8 genmask = nft_genmask_next(net);
3699         const struct nlattr *attr;
3700         struct nft_set *set;
3701         struct nft_ctx ctx;
3702         int err;
3703
3704         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
3705                 return -EAFNOSUPPORT;
3706         if (nla[NFTA_SET_TABLE] == NULL)
3707                 return -EINVAL;
3708
3709         err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
3710                                         genmask);
3711         if (err < 0)
3712                 return err;
3713
3714         if (nla[NFTA_SET_HANDLE]) {
3715                 attr = nla[NFTA_SET_HANDLE];
3716                 set = nft_set_lookup_byhandle(ctx.table, attr, genmask);
3717         } else {
3718                 attr = nla[NFTA_SET_NAME];
3719                 set = nft_set_lookup(ctx.table, attr, genmask);
3720         }
3721
3722         if (IS_ERR(set)) {
3723                 NL_SET_BAD_ATTR(extack, attr);
3724                 return PTR_ERR(set);
3725         }
3726         if (set->use ||
3727             (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) {
3728                 NL_SET_BAD_ATTR(extack, attr);
3729                 return -EBUSY;
3730         }
3731
3732         return nft_delset(&ctx, set);
3733 }
3734
3735 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
3736                                         struct nft_set *set,
3737                                         const struct nft_set_iter *iter,
3738                                         struct nft_set_elem *elem)
3739 {
3740         const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3741         enum nft_registers dreg;
3742
3743         dreg = nft_type_to_reg(set->dtype);
3744         return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
3745                                            set->dtype == NFT_DATA_VERDICT ?
3746                                            NFT_DATA_VERDICT : NFT_DATA_VALUE,
3747                                            set->dlen);
3748 }
3749
3750 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
3751                        struct nft_set_binding *binding)
3752 {
3753         struct nft_set_binding *i;
3754         struct nft_set_iter iter;
3755
3756         if (set->use == UINT_MAX)
3757                 return -EOVERFLOW;
3758
3759         if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
3760                 return -EBUSY;
3761
3762         if (binding->flags & NFT_SET_MAP) {
3763                 /* If the set is already bound to the same chain all
3764                  * jumps are already validated for that chain.
3765                  */
3766                 list_for_each_entry(i, &set->bindings, list) {
3767                         if (i->flags & NFT_SET_MAP &&
3768                             i->chain == binding->chain)
3769                                 goto bind;
3770                 }
3771
3772                 iter.genmask    = nft_genmask_next(ctx->net);
3773                 iter.skip       = 0;
3774                 iter.count      = 0;
3775                 iter.err        = 0;
3776                 iter.fn         = nf_tables_bind_check_setelem;
3777
3778                 set->ops->walk(ctx, set, &iter);
3779                 if (iter.err < 0)
3780                         return iter.err;
3781         }
3782 bind:
3783         binding->chain = ctx->chain;
3784         list_add_tail_rcu(&binding->list, &set->bindings);
3785         nft_set_trans_bind(ctx, set);
3786         set->use++;
3787
3788         return 0;
3789 }
3790 EXPORT_SYMBOL_GPL(nf_tables_bind_set);
3791
3792 static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
3793                                  struct nft_set_binding *binding, bool event)
3794 {
3795         list_del_rcu(&binding->list);
3796
3797         if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
3798                 list_del_rcu(&set->list);
3799                 if (event)
3800                         nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
3801                                              GFP_KERNEL);
3802         }
3803 }
3804
3805 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
3806                               struct nft_set_binding *binding,
3807                               enum nft_trans_phase phase)
3808 {
3809         switch (phase) {
3810         case NFT_TRANS_PREPARE:
3811                 set->use--;
3812                 return;
3813         case NFT_TRANS_ABORT:
3814         case NFT_TRANS_RELEASE:
3815                 set->use--;
3816                 /* fall through */
3817         default:
3818                 nf_tables_unbind_set(ctx, set, binding,
3819                                      phase == NFT_TRANS_COMMIT);
3820         }
3821 }
3822 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
3823
3824 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
3825 {
3826         if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
3827                 nft_set_destroy(set);
3828 }
3829 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
3830
3831 const struct nft_set_ext_type nft_set_ext_types[] = {
3832         [NFT_SET_EXT_KEY]               = {
3833                 .align  = __alignof__(u32),
3834         },
3835         [NFT_SET_EXT_DATA]              = {
3836                 .align  = __alignof__(u32),
3837         },
3838         [NFT_SET_EXT_EXPR]              = {
3839                 .align  = __alignof__(struct nft_expr),
3840         },
3841         [NFT_SET_EXT_OBJREF]            = {
3842                 .len    = sizeof(struct nft_object *),
3843                 .align  = __alignof__(struct nft_object *),
3844         },
3845         [NFT_SET_EXT_FLAGS]             = {
3846                 .len    = sizeof(u8),
3847                 .align  = __alignof__(u8),
3848         },
3849         [NFT_SET_EXT_TIMEOUT]           = {
3850                 .len    = sizeof(u64),
3851                 .align  = __alignof__(u64),
3852         },
3853         [NFT_SET_EXT_EXPIRATION]        = {
3854                 .len    = sizeof(u64),
3855                 .align  = __alignof__(u64),
3856         },
3857         [NFT_SET_EXT_USERDATA]          = {
3858                 .len    = sizeof(struct nft_userdata),
3859                 .align  = __alignof__(struct nft_userdata),
3860         },
3861 };
3862 EXPORT_SYMBOL_GPL(nft_set_ext_types);
3863
3864 /*
3865  * Set elements
3866  */
3867
3868 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3869         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
3870         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
3871         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
3872         [NFTA_SET_ELEM_TIMEOUT]         = { .type = NLA_U64 },
3873         [NFTA_SET_ELEM_USERDATA]        = { .type = NLA_BINARY,
3874                                             .len = NFT_USERDATA_MAXLEN },
3875         [NFTA_SET_ELEM_EXPR]            = { .type = NLA_NESTED },
3876         [NFTA_SET_ELEM_OBJREF]          = { .type = NLA_STRING },
3877 };
3878
3879 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3880         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING,
3881                                             .len = NFT_TABLE_MAXNAMELEN - 1 },
3882         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING,
3883                                             .len = NFT_SET_MAXNAMELEN - 1 },
3884         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
3885         [NFTA_SET_ELEM_LIST_SET_ID]     = { .type = NLA_U32 },
3886 };
3887
3888 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
3889                                       const struct sk_buff *skb,
3890                                       const struct nlmsghdr *nlh,
3891                                       const struct nlattr * const nla[],
3892                                       struct netlink_ext_ack *extack,
3893                                       u8 genmask)
3894 {
3895         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3896         int family = nfmsg->nfgen_family;
3897         struct nft_table *table;
3898
3899         table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
3900                                  genmask);
3901         if (IS_ERR(table)) {
3902                 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
3903                 return PTR_ERR(table);
3904         }
3905
3906         nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
3907         return 0;
3908 }
3909
3910 static int nf_tables_fill_setelem(struct sk_buff *skb,
3911                                   const struct nft_set *set,
3912                                   const struct nft_set_elem *elem)
3913 {
3914         const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3915         unsigned char *b = skb_tail_pointer(skb);
3916         struct nlattr *nest;
3917
3918         nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
3919         if (nest == NULL)
3920                 goto nla_put_failure;
3921
3922         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3923                           NFT_DATA_VALUE, set->klen) < 0)
3924                 goto nla_put_failure;
3925
3926         if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3927             nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
3928                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3929                           set->dlen) < 0)
3930                 goto nla_put_failure;
3931
3932         if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3933             nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3934                 goto nla_put_failure;
3935
3936         if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
3937             nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
3938                            (*nft_set_ext_obj(ext))->key.name) < 0)
3939                 goto nla_put_failure;
3940
3941         if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3942             nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3943                          htonl(*nft_set_ext_flags(ext))))
3944                 goto nla_put_failure;
3945
3946         if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3947             nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
3948                          nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
3949                          NFTA_SET_ELEM_PAD))
3950                 goto nla_put_failure;
3951
3952         if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3953                 u64 expires, now = get_jiffies_64();
3954
3955                 expires = *nft_set_ext_expiration(ext);
3956                 if (time_before64(now, expires))
3957                         expires -= now;
3958                 else
3959                         expires = 0;
3960
3961                 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
3962                                  nf_jiffies64_to_msecs(expires),
3963                                  NFTA_SET_ELEM_PAD))
3964                         goto nla_put_failure;
3965         }
3966
3967         if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3968                 struct nft_userdata *udata;
3969
3970                 udata = nft_set_ext_userdata(ext);
3971                 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3972                             udata->len + 1, udata->data))
3973                         goto nla_put_failure;
3974         }
3975
3976         nla_nest_end(skb, nest);
3977         return 0;
3978
3979 nla_put_failure:
3980         nlmsg_trim(skb, b);
3981         return -EMSGSIZE;
3982 }
3983
3984 struct nft_set_dump_args {
3985         const struct netlink_callback   *cb;
3986         struct nft_set_iter             iter;
3987         struct sk_buff                  *skb;
3988 };
3989
3990 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
3991                                   struct nft_set *set,
3992                                   const struct nft_set_iter *iter,
3993                                   struct nft_set_elem *elem)
3994 {
3995         struct nft_set_dump_args *args;
3996
3997         args = container_of(iter, struct nft_set_dump_args, iter);
3998         return nf_tables_fill_setelem(args->skb, set, elem);
3999 }
4000
4001 struct nft_set_dump_ctx {
4002         const struct nft_set    *set;
4003         struct nft_ctx          ctx;
4004 };
4005
4006 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
4007 {
4008         struct nft_set_dump_ctx *dump_ctx = cb->data;
4009         struct net *net = sock_net(skb->sk);
4010         struct nft_table *table;
4011         struct nft_set *set;
4012         struct nft_set_dump_args args;
4013         bool set_found = false;
4014         struct nfgenmsg *nfmsg;
4015         struct nlmsghdr *nlh;
4016         struct nlattr *nest;
4017         u32 portid, seq;
4018         int event;
4019
4020         rcu_read_lock();
4021         list_for_each_entry_rcu(table, &net->nft.tables, list) {
4022                 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
4023                     dump_ctx->ctx.family != table->family)
4024                         continue;
4025
4026                 if (table != dump_ctx->ctx.table)
4027                         continue;
4028
4029                 list_for_each_entry_rcu(set, &table->sets, list) {
4030                         if (set == dump_ctx->set) {
4031                                 set_found = true;
4032                                 break;
4033                         }
4034                 }
4035                 break;
4036         }
4037
4038         if (!set_found) {
4039                 rcu_read_unlock();
4040                 return -ENOENT;
4041         }
4042
4043         event  = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
4044         portid = NETLINK_CB(cb->skb).portid;
4045         seq    = cb->nlh->nlmsg_seq;
4046
4047         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4048                         NLM_F_MULTI);
4049         if (nlh == NULL)
4050                 goto nla_put_failure;
4051
4052         nfmsg = nlmsg_data(nlh);
4053         nfmsg->nfgen_family = table->family;
4054         nfmsg->version      = NFNETLINK_V0;
4055         nfmsg->res_id       = htons(net->nft.base_seq & 0xffff);
4056
4057         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
4058                 goto nla_put_failure;
4059         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
4060                 goto nla_put_failure;
4061
4062         nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
4063         if (nest == NULL)
4064                 goto nla_put_failure;
4065
4066         args.cb                 = cb;
4067         args.skb                = skb;
4068         args.iter.genmask       = nft_genmask_cur(net);
4069         args.iter.skip          = cb->args[0];
4070         args.iter.count         = 0;
4071         args.iter.err           = 0;
4072         args.iter.fn            = nf_tables_dump_setelem;
4073         set->ops->walk(&dump_ctx->ctx, set, &args.iter);
4074         rcu_read_unlock();
4075
4076         nla_nest_end(skb, nest);
4077         nlmsg_end(skb, nlh);
4078
4079         if (args.iter.err && args.iter.err != -EMSGSIZE)
4080                 return args.iter.err;
4081         if (args.iter.count == cb->args[0])
4082                 return 0;
4083
4084         cb->args[0] = args.iter.count;
4085         return skb->len;
4086
4087 nla_put_failure:
4088         rcu_read_unlock();
4089         return -ENOSPC;
4090 }
4091
4092 static int nf_tables_dump_set_start(struct netlink_callback *cb)
4093 {
4094         struct nft_set_dump_ctx *dump_ctx = cb->data;
4095
4096         cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
4097
4098         return cb->data ? 0 : -ENOMEM;
4099 }
4100
4101 static int nf_tables_dump_set_done(struct netlink_callback *cb)
4102 {
4103         kfree(cb->data);
4104         return 0;
4105 }
4106
4107 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
4108                                        const struct nft_ctx *ctx, u32 seq,
4109                                        u32 portid, int event, u16 flags,
4110                                        const struct nft_set *set,
4111                                        const struct nft_set_elem *elem)
4112 {
4113         struct nfgenmsg *nfmsg;
4114         struct nlmsghdr *nlh;
4115         struct nlattr *nest;
4116         int err;
4117
4118         event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
4119         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4120                         flags);
4121         if (nlh == NULL)
4122                 goto nla_put_failure;
4123
4124         nfmsg = nlmsg_data(nlh);
4125         nfmsg->nfgen_family     = ctx->family;
4126         nfmsg->version          = NFNETLINK_V0;
4127         nfmsg->res_id           = htons(ctx->net->nft.base_seq & 0xffff);
4128
4129         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4130                 goto nla_put_failure;
4131         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4132                 goto nla_put_failure;
4133
4134         nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
4135         if (nest == NULL)
4136                 goto nla_put_failure;
4137
4138         err = nf_tables_fill_setelem(skb, set, elem);
4139         if (err < 0)
4140                 goto nla_put_failure;
4141
4142         nla_nest_end(skb, nest);
4143
4144         nlmsg_end(skb, nlh);
4145         return 0;
4146
4147 nla_put_failure:
4148         nlmsg_trim(skb, nlh);
4149         return -1;
4150 }
4151
4152 static int nft_setelem_parse_flags(const struct nft_set *set,
4153                                    const struct nlattr *attr, u32 *flags)
4154 {
4155         if (attr == NULL)
4156                 return 0;
4157
4158         *flags = ntohl(nla_get_be32(attr));
4159         if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
4160                 return -EINVAL;
4161         if (!(set->flags & NFT_SET_INTERVAL) &&
4162             *flags & NFT_SET_ELEM_INTERVAL_END)
4163                 return -EINVAL;
4164
4165         return 0;
4166 }
4167
4168 static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
4169                             const struct nlattr *attr)
4170 {
4171         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
4172         struct nft_data_desc desc;
4173         struct nft_set_elem elem;
4174         struct sk_buff *skb;
4175         uint32_t flags = 0;
4176         void *priv;
4177         int err;
4178
4179         err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4180                                           nft_set_elem_policy, NULL);
4181         if (err < 0)
4182                 return err;
4183
4184         if (!nla[NFTA_SET_ELEM_KEY])
4185                 return -EINVAL;
4186
4187         err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4188         if (err < 0)
4189                 return err;
4190
4191         err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
4192                             nla[NFTA_SET_ELEM_KEY]);
4193         if (err < 0)
4194                 return err;
4195
4196         err = -EINVAL;
4197         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
4198                 return err;
4199
4200         priv = set->ops->get(ctx->net, set, &elem, flags);
4201         if (IS_ERR(priv))
4202                 return PTR_ERR(priv);
4203
4204         elem.priv = priv;
4205
4206         err = -ENOMEM;
4207         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
4208         if (skb == NULL)
4209                 goto err1;
4210
4211         err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
4212                                           NFT_MSG_NEWSETELEM, 0, set, &elem);
4213         if (err < 0)
4214                 goto err2;
4215
4216         err = nfnetlink_unicast(skb, ctx->net, ctx->portid, MSG_DONTWAIT);
4217         /* This avoids a loop in nfnetlink. */
4218         if (err < 0)
4219                 goto err1;
4220
4221         return 0;
4222 err2:
4223         kfree_skb(skb);
4224 err1:
4225         /* this avoids a loop in nfnetlink. */
4226         return err == -EAGAIN ? -ENOBUFS : err;
4227 }
4228
4229 /* called with rcu_read_lock held */
4230 static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
4231                                 struct sk_buff *skb, const struct nlmsghdr *nlh,
4232                                 const struct nlattr * const nla[],
4233                                 struct netlink_ext_ack *extack)
4234 {
4235         u8 genmask = nft_genmask_cur(net);
4236         struct nft_set *set;
4237         struct nlattr *attr;
4238         struct nft_ctx ctx;
4239         int rem, err = 0;
4240
4241         err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
4242                                          genmask);
4243         if (err < 0)
4244                 return err;
4245
4246         set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
4247         if (IS_ERR(set))
4248                 return PTR_ERR(set);
4249
4250         if (nlh->nlmsg_flags & NLM_F_DUMP) {
4251                 struct netlink_dump_control c = {
4252                         .start = nf_tables_dump_set_start,
4253                         .dump = nf_tables_dump_set,
4254                         .done = nf_tables_dump_set_done,
4255                         .module = THIS_MODULE,
4256                 };
4257                 struct nft_set_dump_ctx dump_ctx = {
4258                         .set = set,
4259                         .ctx = ctx,
4260                 };
4261
4262                 c.data = &dump_ctx;
4263                 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
4264         }
4265
4266         if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
4267                 return -EINVAL;
4268
4269         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4270                 err = nft_get_set_elem(&ctx, set, attr);
4271                 if (err < 0)
4272                         break;
4273         }
4274
4275         return err;
4276 }
4277
4278 static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
4279                                      const struct nft_set *set,
4280                                      const struct nft_set_elem *elem,
4281                                      int event, u16 flags)
4282 {
4283         struct net *net = ctx->net;
4284         u32 portid = ctx->portid;
4285         struct sk_buff *skb;
4286         int err;
4287
4288         if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
4289                 return;
4290
4291         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4292         if (skb == NULL)
4293                 goto err;
4294
4295         err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
4296                                           set, elem);
4297         if (err < 0) {
4298                 kfree_skb(skb);
4299                 goto err;
4300         }
4301
4302         nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
4303                        GFP_KERNEL);
4304         return;
4305 err:
4306         nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
4307 }
4308
4309 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
4310                                               int msg_type,
4311                                               struct nft_set *set)
4312 {
4313         struct nft_trans *trans;
4314
4315         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
4316         if (trans == NULL)
4317                 return NULL;
4318
4319         nft_trans_elem_set(trans) = set;
4320         return trans;
4321 }
4322
4323 void *nft_set_elem_init(const struct nft_set *set,
4324                         const struct nft_set_ext_tmpl *tmpl,
4325                         const u32 *key, const u32 *data,
4326                         u64 timeout, gfp_t gfp)
4327 {
4328         struct nft_set_ext *ext;
4329         void *elem;
4330
4331         elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
4332         if (elem == NULL)
4333                 return NULL;
4334
4335         ext = nft_set_elem_ext(set, elem);
4336         nft_set_ext_init(ext, tmpl);
4337
4338         memcpy(nft_set_ext_key(ext), key, set->klen);
4339         if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4340                 memcpy(nft_set_ext_data(ext), data, set->dlen);
4341         if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
4342                 *nft_set_ext_expiration(ext) =
4343                         get_jiffies_64() + timeout;
4344         if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
4345                 *nft_set_ext_timeout(ext) = timeout;
4346
4347         return elem;
4348 }
4349
4350 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
4351                           bool destroy_expr)
4352 {
4353         struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
4354         struct nft_ctx ctx = {
4355                 .net    = read_pnet(&set->net),
4356                 .family = set->table->family,
4357         };
4358
4359         nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
4360         if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4361                 nft_data_release(nft_set_ext_data(ext), set->dtype);
4362         if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
4363                 struct nft_expr *expr = nft_set_ext_expr(ext);
4364
4365                 if (expr->ops->destroy_clone) {
4366                         expr->ops->destroy_clone(&ctx, expr);
4367                         module_put(expr->ops->type->owner);
4368                 } else {
4369                         nf_tables_expr_destroy(&ctx, expr);
4370                 }
4371         }
4372         if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
4373                 (*nft_set_ext_obj(ext))->use--;
4374         kfree(elem);
4375 }
4376 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
4377
4378 /* Only called from commit path, nft_set_elem_deactivate() already deals with
4379  * the refcounting from the preparation phase.
4380  */
4381 static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
4382                                        const struct nft_set *set, void *elem)
4383 {
4384         struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
4385
4386         if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
4387                 nf_tables_expr_destroy(ctx, nft_set_ext_expr(ext));
4388         kfree(elem);
4389 }
4390
4391 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
4392                             const struct nlattr *attr, u32 nlmsg_flags)
4393 {
4394         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
4395         u8 genmask = nft_genmask_next(ctx->net);
4396         struct nft_data_desc d1, d2;
4397         struct nft_set_ext_tmpl tmpl;
4398         struct nft_set_ext *ext, *ext2;
4399         struct nft_set_elem elem;
4400         struct nft_set_binding *binding;
4401         struct nft_object *obj = NULL;
4402         struct nft_userdata *udata;
4403         struct nft_data data;
4404         enum nft_registers dreg;
4405         struct nft_trans *trans;
4406         u32 flags = 0;
4407         u64 timeout;
4408         u8 ulen;
4409         int err;
4410
4411         err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4412                                           nft_set_elem_policy, NULL);
4413         if (err < 0)
4414                 return err;
4415
4416         if (nla[NFTA_SET_ELEM_KEY] == NULL)
4417                 return -EINVAL;
4418
4419         nft_set_ext_prepare(&tmpl);
4420
4421         err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4422         if (err < 0)
4423                 return err;
4424         if (flags != 0)
4425                 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
4426
4427         if (set->flags & NFT_SET_MAP) {
4428                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
4429                     !(flags & NFT_SET_ELEM_INTERVAL_END))
4430                         return -EINVAL;
4431                 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
4432                     flags & NFT_SET_ELEM_INTERVAL_END)
4433                         return -EINVAL;
4434         } else {
4435                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
4436                         return -EINVAL;
4437         }
4438
4439         timeout = 0;
4440         if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
4441                 if (!(set->flags & NFT_SET_TIMEOUT))
4442                         return -EINVAL;
4443                 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
4444                                             &timeout);
4445                 if (err)
4446                         return err;
4447         } else if (set->flags & NFT_SET_TIMEOUT) {
4448                 timeout = set->timeout;
4449         }
4450
4451         err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
4452                             nla[NFTA_SET_ELEM_KEY]);
4453         if (err < 0)
4454                 goto err1;
4455         err = -EINVAL;
4456         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
4457                 goto err2;
4458
4459         nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
4460         if (timeout > 0) {
4461                 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
4462                 if (timeout != set->timeout)
4463                         nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
4464         }
4465
4466         if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
4467                 if (!(set->flags & NFT_SET_OBJECT)) {
4468                         err = -EINVAL;
4469                         goto err2;
4470                 }
4471                 obj = nft_obj_lookup(ctx->net, ctx->table,
4472                                      nla[NFTA_SET_ELEM_OBJREF],
4473                                      set->objtype, genmask);
4474                 if (IS_ERR(obj)) {
4475                         err = PTR_ERR(obj);
4476                         goto err2;
4477                 }
4478                 nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
4479         }
4480
4481         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
4482                 err = nft_data_init(ctx, &data, sizeof(data), &d2,
4483                                     nla[NFTA_SET_ELEM_DATA]);
4484                 if (err < 0)
4485                         goto err2;
4486
4487                 err = -EINVAL;
4488                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
4489                         goto err3;
4490
4491                 dreg = nft_type_to_reg(set->dtype);
4492                 list_for_each_entry(binding, &set->bindings, list) {
4493                         struct nft_ctx bind_ctx = {
4494                                 .net    = ctx->net,
4495                                 .family = ctx->family,
4496                                 .table  = ctx->table,
4497                                 .chain  = (struct nft_chain *)binding->chain,
4498                         };
4499
4500                         if (!(binding->flags & NFT_SET_MAP))
4501                                 continue;
4502
4503                         err = nft_validate_register_store(&bind_ctx, dreg,
4504                                                           &data,
4505                                                           d2.type, d2.len);
4506                         if (err < 0)
4507                                 goto err3;
4508
4509                         if (d2.type == NFT_DATA_VERDICT &&
4510                             (data.verdict.code == NFT_GOTO ||
4511                              data.verdict.code == NFT_JUMP))
4512                                 nft_validate_state_update(ctx->net,
4513                                                           NFT_VALIDATE_NEED);
4514                 }
4515
4516                 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
4517         }
4518
4519         /* The full maximum length of userdata can exceed the maximum
4520          * offset value (U8_MAX) for following extensions, therefor it
4521          * must be the last extension added.
4522          */
4523         ulen = 0;
4524         if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
4525                 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
4526                 if (ulen > 0)
4527                         nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
4528                                                ulen);
4529         }
4530
4531         err = -ENOMEM;
4532         elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
4533                                       timeout, GFP_KERNEL);
4534         if (elem.priv == NULL)
4535                 goto err3;
4536
4537         ext = nft_set_elem_ext(set, elem.priv);
4538         if (flags)
4539                 *nft_set_ext_flags(ext) = flags;
4540         if (ulen > 0) {
4541                 udata = nft_set_ext_userdata(ext);
4542                 udata->len = ulen - 1;
4543                 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
4544         }
4545         if (obj) {
4546                 *nft_set_ext_obj(ext) = obj;
4547                 obj->use++;
4548         }
4549
4550         trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
4551         if (trans == NULL)
4552                 goto err4;
4553
4554         ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
4555         err = set->ops->insert(ctx->net, set, &elem, &ext2);
4556         if (err) {
4557                 if (err == -EEXIST) {
4558                         if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
4559                             nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
4560                             nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
4561                             nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) {
4562                                 err = -EBUSY;
4563                                 goto err5;
4564                         }
4565                         if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
4566                              nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
4567                              memcmp(nft_set_ext_data(ext),
4568                                     nft_set_ext_data(ext2), set->dlen) != 0) ||
4569                             (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
4570                              nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
4571                              *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
4572                                 err = -EBUSY;
4573                         else if (!(nlmsg_flags & NLM_F_EXCL))
4574                                 err = 0;
4575                 }
4576                 goto err5;
4577         }
4578
4579         if (set->size &&
4580             !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
4581                 err = -ENFILE;
4582                 goto err6;
4583         }
4584
4585         nft_trans_elem(trans) = elem;
4586         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4587         return 0;
4588
4589 err6:
4590         set->ops->remove(ctx->net, set, &elem);
4591 err5:
4592         kfree(trans);
4593 err4:
4594         if (obj)
4595                 obj->use--;
4596         kfree(elem.priv);
4597 err3:
4598         if (nla[NFTA_SET_ELEM_DATA] != NULL)
4599                 nft_data_release(&data, d2.type);
4600 err2:
4601         nft_data_release(&elem.key.val, d1.type);
4602 err1:
4603         return err;
4604 }
4605
4606 static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
4607                                 struct sk_buff *skb, const struct nlmsghdr *nlh,
4608                                 const struct nlattr * const nla[],
4609                                 struct netlink_ext_ack *extack)
4610 {
4611         u8 genmask = nft_genmask_next(net);
4612         const struct nlattr *attr;
4613         struct nft_set *set;
4614         struct nft_ctx ctx;
4615         int rem, err;
4616
4617         if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
4618                 return -EINVAL;
4619
4620         err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
4621                                          genmask);
4622         if (err < 0)
4623                 return err;
4624
4625         set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
4626                                     nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
4627         if (IS_ERR(set))
4628                 return PTR_ERR(set);
4629
4630         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
4631                 return -EBUSY;
4632
4633         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4634                 err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags);
4635                 if (err < 0)
4636                         return err;
4637         }
4638
4639         if (net->nft.validate_state == NFT_VALIDATE_DO)
4640                 return nft_table_validate(net, ctx.table);
4641
4642         return 0;
4643 }
4644
4645 /**
4646  *      nft_data_hold - hold a nft_data item
4647  *
4648  *      @data: struct nft_data to release
4649  *      @type: type of data
4650  *
4651  *      Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4652  *      NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
4653  *      NFT_GOTO verdicts. This function must be called on active data objects
4654  *      from the second phase of the commit protocol.
4655  */
4656 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
4657 {
4658         if (type == NFT_DATA_VERDICT) {
4659                 switch (data->verdict.code) {
4660                 case NFT_JUMP:
4661                 case NFT_GOTO:
4662                         data->verdict.chain->use++;
4663                         break;
4664                 }
4665         }
4666 }
4667
4668 static void nft_set_elem_activate(const struct net *net,
4669                                   const struct nft_set *set,
4670                                   struct nft_set_elem *elem)
4671 {
4672         const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4673
4674         if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4675                 nft_data_hold(nft_set_ext_data(ext), set->dtype);
4676         if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
4677                 (*nft_set_ext_obj(ext))->use++;
4678 }
4679
4680 static void nft_set_elem_deactivate(const struct net *net,
4681                                     const struct nft_set *set,
4682                                     struct nft_set_elem *elem)
4683 {
4684         const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4685
4686         if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
4687                 nft_data_release(nft_set_ext_data(ext), set->dtype);
4688         if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
4689                 (*nft_set_ext_obj(ext))->use--;
4690 }
4691
4692 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
4693                            const struct nlattr *attr)
4694 {
4695         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
4696         struct nft_set_ext_tmpl tmpl;
4697         struct nft_data_desc desc;
4698         struct nft_set_elem elem;
4699         struct nft_set_ext *ext;
4700         struct nft_trans *trans;
4701         u32 flags = 0;
4702         void *priv;
4703         int err;
4704
4705         err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4706                                           nft_set_elem_policy, NULL);
4707         if (err < 0)
4708                 goto err1;
4709
4710         err = -EINVAL;
4711         if (nla[NFTA_SET_ELEM_KEY] == NULL)
4712                 goto err1;
4713
4714         nft_set_ext_prepare(&tmpl);
4715
4716         err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4717         if (err < 0)
4718                 return err;
4719         if (flags != 0)
4720                 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
4721
4722         err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
4723                             nla[NFTA_SET_ELEM_KEY]);
4724         if (err < 0)
4725                 goto err1;
4726
4727         err = -EINVAL;
4728         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
4729                 goto err2;
4730
4731         nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len);
4732
4733         err = -ENOMEM;
4734         elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
4735                                       GFP_KERNEL);
4736         if (elem.priv == NULL)
4737                 goto err2;
4738
4739         ext = nft_set_elem_ext(set, elem.priv);
4740         if (flags)
4741                 *nft_set_ext_flags(ext) = flags;
4742
4743         trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
4744         if (trans == NULL) {
4745                 err = -ENOMEM;
4746                 goto err3;
4747         }
4748
4749         priv = set->ops->deactivate(ctx->net, set, &elem);
4750         if (priv == NULL) {
4751                 err = -ENOENT;
4752                 goto err4;
4753         }
4754         kfree(elem.priv);
4755         elem.priv = priv;
4756
4757         nft_set_elem_deactivate(ctx->net, set, &elem);
4758
4759         nft_trans_elem(trans) = elem;
4760         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4761         return 0;
4762
4763 err4:
4764         kfree(trans);
4765 err3:
4766         kfree(elem.priv);
4767 err2:
4768         nft_data_release(&elem.key.val, desc.type);
4769 err1:
4770         return err;
4771 }
4772
4773 static int nft_flush_set(const struct nft_ctx *ctx,
4774                          struct nft_set *set,
4775                          const struct nft_set_iter *iter,
4776                          struct nft_set_elem *elem)
4777 {
4778         struct nft_trans *trans;
4779         int err;
4780
4781         trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
4782                                     sizeof(struct nft_trans_elem), GFP_ATOMIC);
4783         if (!trans)
4784                 return -ENOMEM;
4785
4786         if (!set->ops->flush(ctx->net, set, elem->priv)) {
4787                 err = -ENOENT;
4788                 goto err1;
4789         }
4790         set->ndeact++;
4791
4792         nft_set_elem_deactivate(ctx->net, set, elem);
4793         nft_trans_elem_set(trans) = set;
4794         nft_trans_elem(trans) = *elem;
4795         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4796
4797         return 0;
4798 err1:
4799         kfree(trans);
4800         return err;
4801 }
4802
4803 static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
4804                                 struct sk_buff *skb, const struct nlmsghdr *nlh,
4805                                 const struct nlattr * const nla[],
4806                                 struct netlink_ext_ack *extack)
4807 {
4808         u8 genmask = nft_genmask_next(net);
4809         const struct nlattr *attr;
4810         struct nft_set *set;
4811         struct nft_ctx ctx;
4812         int rem, err = 0;
4813
4814         err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
4815                                          genmask);
4816         if (err < 0)
4817                 return err;
4818
4819         set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
4820         if (IS_ERR(set))
4821                 return PTR_ERR(set);
4822         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
4823                 return -EBUSY;
4824
4825         if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
4826                 struct nft_set_iter iter = {
4827                         .genmask        = genmask,
4828                         .fn             = nft_flush_set,
4829                 };
4830                 set->ops->walk(&ctx, set, &iter);
4831
4832                 return iter.err;
4833         }
4834
4835         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4836                 err = nft_del_setelem(&ctx, set, attr);
4837                 if (err < 0)
4838                         break;
4839
4840                 set->ndeact++;
4841         }
4842         return err;
4843 }
4844
4845 void nft_set_gc_batch_release(struct rcu_head *rcu)
4846 {
4847         struct nft_set_gc_batch *gcb;
4848         unsigned int i;
4849
4850         gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
4851         for (i = 0; i < gcb->head.cnt; i++)
4852                 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
4853         kfree(gcb);
4854 }
4855 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
4856
4857 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
4858                                                 gfp_t gfp)
4859 {
4860         struct nft_set_gc_batch *gcb;
4861
4862         gcb = kzalloc(sizeof(*gcb), gfp);
4863         if (gcb == NULL)
4864                 return gcb;
4865         gcb->head.set = set;
4866         return gcb;
4867 }
4868 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
4869
4870 /*
4871  * Stateful objects
4872  */
4873
4874 /**
4875  *      nft_register_obj- register nf_tables stateful object type
4876  *      @obj: object type
4877  *
4878  *      Registers the object type for use with nf_tables. Returns zero on
4879  *      success or a negative errno code otherwise.
4880  */
4881 int nft_register_obj(struct nft_object_type *obj_type)
4882 {
4883         if (obj_type->type == NFT_OBJECT_UNSPEC)
4884                 return -EINVAL;
4885
4886         nfnl_lock(NFNL_SUBSYS_NFTABLES);
4887         list_add_rcu(&obj_type->list, &nf_tables_objects);
4888         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4889         return 0;
4890 }
4891 EXPORT_SYMBOL_GPL(nft_register_obj);
4892
4893 /**
4894  *      nft_unregister_obj - unregister nf_tables object type
4895  *      @obj: object type
4896  *
4897  *      Unregisters the object type for use with nf_tables.
4898  */
4899 void nft_unregister_obj(struct nft_object_type *obj_type)
4900 {
4901         nfnl_lock(NFNL_SUBSYS_NFTABLES);
4902         list_del_rcu(&obj_type->list);
4903         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4904 }
4905 EXPORT_SYMBOL_GPL(nft_unregister_obj);
4906
4907 struct nft_object *nft_obj_lookup(const struct net *net,
4908                                   const struct nft_table *table,
4909                                   const struct nlattr *nla, u32 objtype,
4910                                   u8 genmask)
4911 {
4912         struct nft_object_hash_key k = { .table = table };
4913         char search[NFT_OBJ_MAXNAMELEN];
4914         struct rhlist_head *tmp, *list;
4915         struct nft_object *obj;
4916
4917         nla_strlcpy(search, nla, sizeof(search));
4918         k.name = search;
4919
4920         WARN_ON_ONCE(!rcu_read_lock_held() &&
4921                      !lockdep_commit_lock_is_held(net));
4922
4923         rcu_read_lock();
4924         list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
4925         if (!list)
4926                 goto out;
4927
4928         rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
4929                 if (objtype == obj->ops->type->type &&
4930                     nft_active_genmask(obj, genmask)) {
4931                         rcu_read_unlock();
4932                         return obj;
4933                 }
4934         }
4935 out:
4936         rcu_read_unlock();
4937         return ERR_PTR(-ENOENT);
4938 }
4939 EXPORT_SYMBOL_GPL(nft_obj_lookup);
4940
4941 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
4942                                                   const struct nlattr *nla,
4943                                                   u32 objtype, u8 genmask)
4944 {
4945         struct nft_object *obj;
4946
4947         list_for_each_entry(obj, &table->objects, list) {
4948                 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
4949                     objtype == obj->ops->type->type &&
4950                     nft_active_genmask(obj, genmask))
4951                         return obj;
4952         }
4953         return ERR_PTR(-ENOENT);
4954 }
4955
4956 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
4957         [NFTA_OBJ_TABLE]        = { .type = NLA_STRING,
4958                                     .len = NFT_TABLE_MAXNAMELEN - 1 },
4959         [NFTA_OBJ_NAME]         = { .type = NLA_STRING,
4960                                     .len = NFT_OBJ_MAXNAMELEN - 1 },
4961         [NFTA_OBJ_TYPE]         = { .type = NLA_U32 },
4962         [NFTA_OBJ_DATA]         = { .type = NLA_NESTED },
4963         [NFTA_OBJ_HANDLE]       = { .type = NLA_U64},
4964 };
4965
4966 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
4967                                        const struct nft_object_type *type,
4968                                        const struct nlattr *attr)
4969 {
4970         struct nlattr **tb;
4971         const struct nft_object_ops *ops;
4972         struct nft_object *obj;
4973         int err = -ENOMEM;
4974
4975         tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
4976         if (!tb)
4977                 goto err1;
4978
4979         if (attr) {
4980                 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
4981                                                   type->policy, NULL);
4982                 if (err < 0)
4983                         goto err2;
4984         } else {
4985                 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
4986         }
4987
4988         if (type->select_ops) {
4989                 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
4990                 if (IS_ERR(ops)) {
4991                         err = PTR_ERR(ops);
4992                         goto err2;
4993                 }
4994         } else {
4995                 ops = type->ops;
4996         }
4997
4998         err = -ENOMEM;
4999         obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL);
5000         if (!obj)
5001                 goto err2;
5002
5003         err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
5004         if (err < 0)
5005                 goto err3;
5006
5007         obj->ops = ops;
5008
5009         kfree(tb);
5010         return obj;
5011 err3:
5012         kfree(obj);
5013 err2:
5014         kfree(tb);
5015 err1:
5016         return ERR_PTR(err);
5017 }
5018
5019 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
5020                            struct nft_object *obj, bool reset)
5021 {
5022         struct nlattr *nest;
5023
5024         nest = nla_nest_start_noflag(skb, attr);
5025         if (!nest)
5026                 goto nla_put_failure;
5027         if (obj->ops->dump(skb, obj, reset) < 0)
5028                 goto nla_put_failure;
5029         nla_nest_end(skb, nest);
5030         return 0;
5031
5032 nla_put_failure:
5033         return -1;
5034 }
5035
5036 static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
5037 {
5038         const struct nft_object_type *type;
5039
5040         list_for_each_entry(type, &nf_tables_objects, list) {
5041                 if (objtype == type->type)
5042                         return type;
5043         }
5044         return NULL;
5045 }
5046
5047 static const struct nft_object_type *
5048 nft_obj_type_get(struct net *net, u32 objtype)
5049 {
5050         const struct nft_object_type *type;
5051
5052         type = __nft_obj_type_get(objtype);
5053         if (type != NULL && try_module_get(type->owner))
5054                 return type;
5055
5056         lockdep_nfnl_nft_mutex_not_held();
5057 #ifdef CONFIG_MODULES
5058         if (type == NULL) {
5059                 nft_request_module(net, "nft-obj-%u", objtype);
5060                 if (__nft_obj_type_get(objtype))
5061                         return ERR_PTR(-EAGAIN);
5062         }
5063 #endif
5064         return ERR_PTR(-ENOENT);
5065 }
5066
5067 static int nf_tables_newobj(struct net *net, struct sock *nlsk,
5068                             struct sk_buff *skb, const struct nlmsghdr *nlh,
5069                             const struct nlattr * const nla[],
5070                             struct netlink_ext_ack *extack)
5071 {
5072         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5073         const struct nft_object_type *type;
5074         u8 genmask = nft_genmask_next(net);
5075         int family = nfmsg->nfgen_family;
5076         struct nft_table *table;
5077         struct nft_object *obj;
5078         struct nft_ctx ctx;
5079         u32 objtype;
5080         int err;
5081
5082         if (!nla[NFTA_OBJ_TYPE] ||
5083             !nla[NFTA_OBJ_NAME] ||
5084             !nla[NFTA_OBJ_DATA])
5085                 return -EINVAL;
5086
5087         table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
5088         if (IS_ERR(table)) {
5089                 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
5090                 return PTR_ERR(table);
5091         }
5092
5093         objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5094         obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
5095         if (IS_ERR(obj)) {
5096                 err = PTR_ERR(obj);
5097                 if (err != -ENOENT) {
5098                         NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
5099                         return err;
5100                 }
5101         } else {
5102                 if (nlh->nlmsg_flags & NLM_F_EXCL) {
5103                         NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
5104                         return -EEXIST;
5105                 }
5106                 return 0;
5107         }
5108
5109         nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5110
5111         type = nft_obj_type_get(net, objtype);
5112         if (IS_ERR(type))
5113                 return PTR_ERR(type);
5114
5115         obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
5116         if (IS_ERR(obj)) {
5117                 err = PTR_ERR(obj);
5118                 goto err1;
5119         }
5120         obj->key.table = table;
5121         obj->handle = nf_tables_alloc_handle(table);
5122
5123         obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL);
5124         if (!obj->key.name) {
5125                 err = -ENOMEM;
5126                 goto err2;
5127         }
5128
5129         err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
5130         if (err < 0)
5131                 goto err3;
5132
5133         err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
5134                               nft_objname_ht_params);
5135         if (err < 0)
5136                 goto err4;
5137
5138         list_add_tail_rcu(&obj->list, &table->objects);
5139         table->use++;
5140         return 0;
5141 err4:
5142         /* queued in transaction log */
5143         INIT_LIST_HEAD(&obj->list);
5144         return err;
5145 err3:
5146         kfree(obj->key.name);
5147 err2:
5148         if (obj->ops->destroy)
5149                 obj->ops->destroy(&ctx, obj);
5150         kfree(obj);
5151 err1:
5152         module_put(type->owner);
5153         return err;
5154 }
5155
5156 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
5157                                    u32 portid, u32 seq, int event, u32 flags,
5158                                    int family, const struct nft_table *table,
5159                                    struct nft_object *obj, bool reset)
5160 {
5161         struct nfgenmsg *nfmsg;
5162         struct nlmsghdr *nlh;
5163
5164         event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5165         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
5166         if (nlh == NULL)
5167                 goto nla_put_failure;
5168
5169         nfmsg = nlmsg_data(nlh);
5170         nfmsg->nfgen_family     = family;
5171         nfmsg->version          = NFNETLINK_V0;
5172         nfmsg->res_id           = htons(net->nft.base_seq & 0xffff);
5173
5174         if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
5175             nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
5176             nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
5177             nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
5178             nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) ||
5179             nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
5180                          NFTA_OBJ_PAD))
5181                 goto nla_put_failure;
5182
5183         nlmsg_end(skb, nlh);
5184         return 0;
5185
5186 nla_put_failure:
5187         nlmsg_trim(skb, nlh);
5188         return -1;
5189 }
5190
5191 struct nft_obj_filter {
5192         char            *table;
5193         u32             type;
5194 };
5195
5196 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
5197 {
5198         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
5199         const struct nft_table *table;
5200         unsigned int idx = 0, s_idx = cb->args[0];
5201         struct nft_obj_filter *filter = cb->data;
5202         struct net *net = sock_net(skb->sk);
5203         int family = nfmsg->nfgen_family;
5204         struct nft_object *obj;
5205         bool reset = false;
5206
5207         if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
5208                 reset = true;
5209
5210         rcu_read_lock();
5211         cb->seq = net->nft.base_seq;
5212
5213         list_for_each_entry_rcu(table, &net->nft.tables, list) {
5214                 if (family != NFPROTO_UNSPEC && family != table->family)
5215                         continue;
5216
5217                 list_for_each_entry_rcu(obj, &table->objects, list) {
5218                         if (!nft_is_active(net, obj))
5219                                 goto cont;
5220                         if (idx < s_idx)
5221                                 goto cont;
5222                         if (idx > s_idx)
5223                                 memset(&cb->args[1], 0,
5224                                        sizeof(cb->args) - sizeof(cb->args[0]));
5225                         if (filter && filter->table &&
5226                             strcmp(filter->table, table->name))
5227                                 goto cont;
5228                         if (filter &&
5229                             filter->type != NFT_OBJECT_UNSPEC &&
5230                             obj->ops->type->type != filter->type)
5231                                 goto cont;
5232
5233                         if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
5234                                                     cb->nlh->nlmsg_seq,
5235                                                     NFT_MSG_NEWOBJ,
5236                                                     NLM_F_MULTI | NLM_F_APPEND,
5237                                                     table->family, table,
5238                                                     obj, reset) < 0)
5239                                 goto done;
5240
5241                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5242 cont:
5243                         idx++;
5244                 }
5245         }
5246 done:
5247         rcu_read_unlock();
5248
5249         cb->args[0] = idx;
5250         return skb->len;
5251 }
5252
5253 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
5254 {
5255         const struct nlattr * const *nla = cb->data;
5256         struct nft_obj_filter *filter = NULL;
5257
5258         if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
5259                 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
5260                 if (!filter)
5261                         return -ENOMEM;
5262
5263                 if (nla[NFTA_OBJ_TABLE]) {
5264                         filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
5265                         if (!filter->table) {
5266                                 kfree(filter);
5267                                 return -ENOMEM;
5268                         }
5269                 }
5270
5271                 if (nla[NFTA_OBJ_TYPE])
5272                         filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5273         }
5274
5275         cb->data = filter;
5276         return 0;
5277 }
5278
5279 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
5280 {
5281         struct nft_obj_filter *filter = cb->data;
5282
5283         if (filter) {
5284                 kfree(filter->table);
5285                 kfree(filter);
5286         }
5287
5288         return 0;
5289 }
5290
5291 /* called with rcu_read_lock held */
5292 static int nf_tables_getobj(struct net *net, struct sock *nlsk,
5293                             struct sk_buff *skb, const struct nlmsghdr *nlh,
5294                             const struct nlattr * const nla[],
5295                             struct netlink_ext_ack *extack)
5296 {
5297         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5298         u8 genmask = nft_genmask_cur(net);
5299         int family = nfmsg->nfgen_family;
5300         const struct nft_table *table;
5301         struct nft_object *obj;
5302         struct sk_buff *skb2;
5303         bool reset = false;
5304         u32 objtype;
5305         int err;
5306
5307         if (nlh->nlmsg_flags & NLM_F_DUMP) {
5308                 struct netlink_dump_control c = {
5309                         .start = nf_tables_dump_obj_start,
5310                         .dump = nf_tables_dump_obj,
5311                         .done = nf_tables_dump_obj_done,
5312                         .module = THIS_MODULE,
5313                         .data = (void *)nla,
5314                 };
5315
5316                 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
5317         }
5318
5319         if (!nla[NFTA_OBJ_NAME] ||
5320             !nla[NFTA_OBJ_TYPE])
5321                 return -EINVAL;
5322
5323         table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
5324         if (IS_ERR(table)) {
5325                 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
5326                 return PTR_ERR(table);
5327         }
5328
5329         objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5330         obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
5331         if (IS_ERR(obj)) {
5332                 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
5333                 return PTR_ERR(obj);
5334         }
5335
5336         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
5337         if (!skb2)
5338                 return -ENOMEM;
5339
5340         if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
5341                 reset = true;
5342
5343         err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
5344                                       nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
5345                                       family, table, obj, reset);
5346         if (err < 0)
5347                 goto err;
5348
5349         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
5350 err:
5351         kfree_skb(skb2);
5352         return err;
5353 }
5354
5355 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
5356 {
5357         if (obj->ops->destroy)
5358                 obj->ops->destroy(ctx, obj);
5359
5360         module_put(obj->ops->type->owner);
5361         kfree(obj->key.name);
5362         kfree(obj);
5363 }
5364
5365 static int nf_tables_delobj(struct net *net, struct sock *nlsk,
5366                             struct sk_buff *skb, const struct nlmsghdr *nlh,
5367                             const struct nlattr * const nla[],
5368                             struct netlink_ext_ack *extack)
5369 {
5370         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5371         u8 genmask = nft_genmask_next(net);
5372         int family = nfmsg->nfgen_family;
5373         const struct nlattr *attr;
5374         struct nft_table *table;
5375         struct nft_object *obj;
5376         struct nft_ctx ctx;
5377         u32 objtype;
5378
5379         if (!nla[NFTA_OBJ_TYPE] ||
5380             (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
5381                 return -EINVAL;
5382
5383         table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
5384         if (IS_ERR(table)) {
5385                 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
5386                 return PTR_ERR(table);
5387         }
5388
5389         objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
5390         if (nla[NFTA_OBJ_HANDLE]) {
5391                 attr = nla[NFTA_OBJ_HANDLE];
5392                 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
5393         } else {
5394                 attr = nla[NFTA_OBJ_NAME];
5395                 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
5396         }
5397
5398         if (IS_ERR(obj)) {
5399                 NL_SET_BAD_ATTR(extack, attr);
5400                 return PTR_ERR(obj);
5401         }
5402         if (obj->use > 0) {
5403                 NL_SET_BAD_ATTR(extack, attr);
5404                 return -EBUSY;
5405         }
5406
5407         nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5408
5409         return nft_delobj(&ctx, obj);
5410 }
5411
5412 void nft_obj_notify(struct net *net, const struct nft_table *table,
5413                     struct nft_object *obj, u32 portid, u32 seq, int event,
5414                     int family, int report, gfp_t gfp)
5415 {
5416         struct sk_buff *skb;
5417         int err;
5418
5419         if (!report &&
5420             !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
5421                 return;
5422
5423         skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
5424         if (skb == NULL)
5425                 goto err;
5426
5427         err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family,
5428                                       table, obj, false);
5429         if (err < 0) {
5430                 kfree_skb(skb);
5431                 goto err;
5432         }
5433
5434         nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp);
5435         return;
5436 err:
5437         nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
5438 }
5439 EXPORT_SYMBOL_GPL(nft_obj_notify);
5440
5441 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
5442                                  struct nft_object *obj, int event)
5443 {
5444         nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
5445                        ctx->family, ctx->report, GFP_KERNEL);
5446 }
5447
5448 /*
5449  * Flow tables
5450  */
5451 void nft_register_flowtable_type(struct nf_flowtable_type *type)
5452 {
5453         nfnl_lock(NFNL_SUBSYS_NFTABLES);
5454         list_add_tail_rcu(&type->list, &nf_tables_flowtables);
5455         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5456 }
5457 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
5458
5459 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
5460 {
5461         nfnl_lock(NFNL_SUBSYS_NFTABLES);
5462         list_del_rcu(&type->list);
5463         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5464 }
5465 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
5466
5467 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
5468         [NFTA_FLOWTABLE_TABLE]          = { .type = NLA_STRING,
5469                                             .len = NFT_NAME_MAXLEN - 1 },
5470         [NFTA_FLOWTABLE_NAME]           = { .type = NLA_STRING,
5471                                             .len = NFT_NAME_MAXLEN - 1 },
5472         [NFTA_FLOWTABLE_HOOK]           = { .type = NLA_NESTED },
5473         [NFTA_FLOWTABLE_HANDLE]         = { .type = NLA_U64 },
5474 };
5475
5476 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
5477                                            const struct nlattr *nla, u8 genmask)
5478 {
5479         struct nft_flowtable *flowtable;
5480
5481         list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
5482                 if (!nla_strcmp(nla, flowtable->name) &&
5483                     nft_active_genmask(flowtable, genmask))
5484                         return flowtable;
5485         }
5486         return ERR_PTR(-ENOENT);
5487 }
5488 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
5489
5490 static struct nft_flowtable *
5491 nft_flowtable_lookup_byhandle(const struct nft_table *table,
5492                               const struct nlattr *nla, u8 genmask)
5493 {
5494        struct nft_flowtable *flowtable;
5495
5496        list_for_each_entry(flowtable, &table->flowtables, list) {
5497                if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
5498                    nft_active_genmask(flowtable, genmask))
5499                        return flowtable;
5500        }
5501        return ERR_PTR(-ENOENT);
5502 }
5503
5504 static int nf_tables_parse_devices(const struct nft_ctx *ctx,
5505                                    const struct nlattr *attr,
5506                                    struct net_device *dev_array[], int *len)
5507 {
5508         const struct nlattr *tmp;
5509         struct net_device *dev;
5510         char ifname[IFNAMSIZ];
5511         int rem, n = 0, err;
5512
5513         nla_for_each_nested(tmp, attr, rem) {
5514                 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
5515                         err = -EINVAL;
5516                         goto err1;
5517                 }
5518
5519                 nla_strlcpy(ifname, tmp, IFNAMSIZ);
5520                 dev = __dev_get_by_name(ctx->net, ifname);
5521                 if (!dev) {
5522                         err = -ENOENT;
5523                         goto err1;
5524                 }
5525
5526                 dev_array[n++] = dev;
5527                 if (n == NFT_FLOWTABLE_DEVICE_MAX) {
5528                         err = -EFBIG;
5529                         goto err1;
5530                 }
5531         }
5532         if (!len)
5533                 return -EINVAL;
5534
5535         err = 0;
5536 err1:
5537         *len = n;
5538         return err;
5539 }
5540
5541 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
5542         [NFTA_FLOWTABLE_HOOK_NUM]       = { .type = NLA_U32 },
5543         [NFTA_FLOWTABLE_HOOK_PRIORITY]  = { .type = NLA_U32 },
5544         [NFTA_FLOWTABLE_HOOK_DEVS]      = { .type = NLA_NESTED },
5545 };
5546
5547 static int nf_tables_flowtable_parse_hook(const struct nft_ctx *ctx,
5548                                           const struct nlattr *attr,
5549                                           struct nft_flowtable *flowtable)
5550 {
5551         struct net_device *dev_array[NFT_FLOWTABLE_DEVICE_MAX];
5552         struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
5553         struct nf_hook_ops *ops;
5554         int hooknum, priority;
5555         int err, n = 0, i;
5556
5557         err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr,
5558                                           nft_flowtable_hook_policy, NULL);
5559         if (err < 0)
5560                 return err;
5561
5562         if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
5563             !tb[NFTA_FLOWTABLE_HOOK_PRIORITY] ||
5564             !tb[NFTA_FLOWTABLE_HOOK_DEVS])
5565                 return -EINVAL;
5566
5567         hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
5568         if (hooknum != NF_NETDEV_INGRESS)
5569                 return -EINVAL;
5570
5571         priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
5572
5573         err = nf_tables_parse_devices(ctx, tb[NFTA_FLOWTABLE_HOOK_DEVS],
5574                                       dev_array, &n);
5575         if (err < 0)
5576                 return err;
5577
5578         ops = kcalloc(n, sizeof(struct nf_hook_ops), GFP_KERNEL);
5579         if (!ops)
5580                 return -ENOMEM;
5581
5582         flowtable->hooknum      = hooknum;
5583         flowtable->priority     = priority;
5584         flowtable->ops          = ops;
5585         flowtable->ops_len      = n;
5586
5587         for (i = 0; i < n; i++) {
5588                 flowtable->ops[i].pf            = NFPROTO_NETDEV;
5589                 flowtable->ops[i].hooknum       = hooknum;
5590                 flowtable->ops[i].priority      = priority;
5591                 flowtable->ops[i].priv          = &flowtable->data;
5592                 flowtable->ops[i].hook          = flowtable->data.type->hook;
5593                 flowtable->ops[i].dev           = dev_array[i];
5594         }
5595
5596         return err;
5597 }
5598
5599 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
5600 {
5601         const struct nf_flowtable_type *type;
5602
5603         list_for_each_entry(type, &nf_tables_flowtables, list) {
5604                 if (family == type->family)
5605                         return type;
5606         }
5607         return NULL;
5608 }
5609
5610 static const struct nf_flowtable_type *
5611 nft_flowtable_type_get(struct net *net, u8 family)
5612 {
5613         const struct nf_flowtable_type *type;
5614
5615         type = __nft_flowtable_type_get(family);
5616         if (type != NULL && try_module_get(type->owner))
5617                 return type;
5618
5619         lockdep_nfnl_nft_mutex_not_held();
5620 #ifdef CONFIG_MODULES
5621         if (type == NULL) {
5622                 nft_request_module(net, "nf-flowtable-%u", family);
5623                 if (__nft_flowtable_type_get(family))
5624                         return ERR_PTR(-EAGAIN);
5625         }
5626 #endif
5627         return ERR_PTR(-ENOENT);
5628 }
5629
5630 static void nft_unregister_flowtable_net_hooks(struct net *net,
5631                                                struct nft_flowtable *flowtable)
5632 {
5633         int i;
5634
5635         for (i = 0; i < flowtable->ops_len; i++) {
5636                 if (!flowtable->ops[i].dev)
5637                         continue;
5638
5639                 nf_unregister_net_hook(net, &flowtable->ops[i]);
5640         }
5641 }
5642
5643 static int nf_tables_newflowtable(struct net *net, struct sock *nlsk,
5644                                   struct sk_buff *skb,
5645                                   const struct nlmsghdr *nlh,
5646                                   const struct nlattr * const nla[],
5647                                   struct netlink_ext_ack *extack)
5648 {
5649         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5650         const struct nf_flowtable_type *type;
5651         struct nft_flowtable *flowtable, *ft;
5652         u8 genmask = nft_genmask_next(net);
5653         int family = nfmsg->nfgen_family;
5654         struct nft_table *table;
5655         struct nft_ctx ctx;
5656         int err, i, k;
5657
5658         if (!nla[NFTA_FLOWTABLE_TABLE] ||
5659             !nla[NFTA_FLOWTABLE_NAME] ||
5660             !nla[NFTA_FLOWTABLE_HOOK])
5661                 return -EINVAL;
5662
5663         table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
5664                                  genmask);
5665         if (IS_ERR(table)) {
5666                 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
5667                 return PTR_ERR(table);
5668         }
5669
5670         flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
5671                                          genmask);
5672         if (IS_ERR(flowtable)) {
5673                 err = PTR_ERR(flowtable);
5674                 if (err != -ENOENT) {
5675                         NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
5676                         return err;
5677                 }
5678         } else {
5679                 if (nlh->nlmsg_flags & NLM_F_EXCL) {
5680                         NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
5681                         return -EEXIST;
5682                 }
5683
5684                 return 0;
5685         }
5686
5687         nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5688
5689         flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL);
5690         if (!flowtable)
5691                 return -ENOMEM;
5692
5693         flowtable->table = table;
5694         flowtable->handle = nf_tables_alloc_handle(table);
5695
5696         flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL);
5697         if (!flowtable->name) {
5698                 err = -ENOMEM;
5699                 goto err1;
5700         }
5701
5702         type = nft_flowtable_type_get(net, family);
5703         if (IS_ERR(type)) {
5704                 err = PTR_ERR(type);
5705                 goto err2;
5706         }
5707
5708         flowtable->data.type = type;
5709         err = type->init(&flowtable->data);
5710         if (err < 0)
5711                 goto err3;
5712
5713         err = nf_tables_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
5714                                              flowtable);
5715         if (err < 0)
5716                 goto err4;
5717
5718         for (i = 0; i < flowtable->ops_len; i++) {
5719                 if (!flowtable->ops[i].dev)
5720                         continue;
5721
5722                 list_for_each_entry(ft, &table->flowtables, list) {
5723                         for (k = 0; k < ft->ops_len; k++) {
5724                                 if (!ft->ops[k].dev)
5725                                         continue;
5726
5727                                 if (flowtable->ops[i].dev == ft->ops[k].dev &&
5728                                     flowtable->ops[i].pf == ft->ops[k].pf) {
5729                                         err = -EBUSY;
5730                                         goto err5;
5731                                 }
5732                         }
5733                 }
5734
5735                 err = nf_register_net_hook(net, &flowtable->ops[i]);
5736                 if (err < 0)
5737                         goto err5;
5738         }
5739
5740         err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
5741         if (err < 0)
5742                 goto err6;
5743
5744         list_add_tail_rcu(&flowtable->list, &table->flowtables);
5745         table->use++;
5746
5747         return 0;
5748 err6:
5749         i = flowtable->ops_len;
5750 err5:
5751         for (k = i - 1; k >= 0; k--)
5752                 nf_unregister_net_hook(net, &flowtable->ops[k]);
5753
5754         kfree(flowtable->ops);
5755 err4:
5756         flowtable->data.type->free(&flowtable->data);
5757 err3:
5758         module_put(type->owner);
5759 err2:
5760         kfree(flowtable->name);
5761 err1:
5762         kfree(flowtable);
5763         return err;
5764 }
5765
5766 static int nf_tables_delflowtable(struct net *net, struct sock *nlsk,
5767                                   struct sk_buff *skb,
5768                                   const struct nlmsghdr *nlh,
5769                                   const struct nlattr * const nla[],
5770                                   struct netlink_ext_ack *extack)
5771 {
5772         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5773         u8 genmask = nft_genmask_next(net);
5774         int family = nfmsg->nfgen_family;
5775         struct nft_flowtable *flowtable;
5776         const struct nlattr *attr;
5777         struct nft_table *table;
5778         struct nft_ctx ctx;
5779
5780         if (!nla[NFTA_FLOWTABLE_TABLE] ||
5781             (!nla[NFTA_FLOWTABLE_NAME] &&
5782              !nla[NFTA_FLOWTABLE_HANDLE]))
5783                 return -EINVAL;
5784
5785         table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
5786                                  genmask);
5787         if (IS_ERR(table)) {
5788                 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
5789                 return PTR_ERR(table);
5790         }
5791
5792         if (nla[NFTA_FLOWTABLE_HANDLE]) {
5793                 attr = nla[NFTA_FLOWTABLE_HANDLE];
5794                 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
5795         } else {
5796                 attr = nla[NFTA_FLOWTABLE_NAME];
5797                 flowtable = nft_flowtable_lookup(table, attr, genmask);
5798         }
5799
5800         if (IS_ERR(flowtable)) {
5801                 NL_SET_BAD_ATTR(extack, attr);
5802                 return PTR_ERR(flowtable);
5803         }
5804         if (flowtable->use > 0) {
5805                 NL_SET_BAD_ATTR(extack, attr);
5806                 return -EBUSY;
5807         }
5808
5809         nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
5810
5811         return nft_delflowtable(&ctx, flowtable);
5812 }
5813
5814 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
5815                                          u32 portid, u32 seq, int event,
5816                                          u32 flags, int family,
5817                                          struct nft_flowtable *flowtable)
5818 {
5819         struct nlattr *nest, *nest_devs;
5820         struct nfgenmsg *nfmsg;
5821         struct nlmsghdr *nlh;
5822         int i;
5823
5824         event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5825         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
5826         if (nlh == NULL)
5827                 goto nla_put_failure;
5828
5829         nfmsg = nlmsg_data(nlh);
5830         nfmsg->nfgen_family     = family;
5831         nfmsg->version          = NFNETLINK_V0;
5832         nfmsg->res_id           = htons(net->nft.base_seq & 0xffff);
5833
5834         if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
5835             nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
5836             nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
5837             nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
5838                          NFTA_FLOWTABLE_PAD))
5839                 goto nla_put_failure;
5840
5841         nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
5842         if (!nest)
5843                 goto nla_put_failure;
5844         if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
5845             nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->priority)))
5846                 goto nla_put_failure;
5847
5848         nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
5849         if (!nest_devs)
5850                 goto nla_put_failure;
5851
5852         for (i = 0; i < flowtable->ops_len; i++) {
5853                 const struct net_device *dev = READ_ONCE(flowtable->ops[i].dev);
5854
5855                 if (dev &&
5856                     nla_put_string(skb, NFTA_DEVICE_NAME, dev->name))
5857                         goto nla_put_failure;
5858         }
5859         nla_nest_end(skb, nest_devs);
5860         nla_nest_end(skb, nest);
5861
5862         nlmsg_end(skb, nlh);
5863         return 0;
5864
5865 nla_put_failure:
5866         nlmsg_trim(skb, nlh);
5867         return -1;
5868 }
5869
5870 struct nft_flowtable_filter {
5871         char            *table;
5872 };
5873
5874 static int nf_tables_dump_flowtable(struct sk_buff *skb,
5875                                     struct netlink_callback *cb)
5876 {
5877         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
5878         struct nft_flowtable_filter *filter = cb->data;
5879         unsigned int idx = 0, s_idx = cb->args[0];
5880         struct net *net = sock_net(skb->sk);
5881         int family = nfmsg->nfgen_family;
5882         struct nft_flowtable *flowtable;
5883         const struct nft_table *table;
5884
5885         rcu_read_lock();
5886         cb->seq = net->nft.base_seq;
5887
5888         list_for_each_entry_rcu(table, &net->nft.tables, list) {
5889                 if (family != NFPROTO_UNSPEC && family != table->family)
5890                         continue;
5891
5892                 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
5893                         if (!nft_is_active(net, flowtable))
5894                                 goto cont;
5895                         if (idx < s_idx)
5896                                 goto cont;
5897                         if (idx > s_idx)
5898                                 memset(&cb->args[1], 0,
5899                                        sizeof(cb->args) - sizeof(cb->args[0]));
5900                         if (filter && filter->table &&
5901                             strcmp(filter->table, table->name))
5902                                 goto cont;
5903
5904                         if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
5905                                                           cb->nlh->nlmsg_seq,
5906                                                           NFT_MSG_NEWFLOWTABLE,
5907                                                           NLM_F_MULTI | NLM_F_APPEND,
5908                                                           table->family, flowtable) < 0)
5909                                 goto done;
5910
5911                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5912 cont:
5913                         idx++;
5914                 }
5915         }
5916 done:
5917         rcu_read_unlock();
5918
5919         cb->args[0] = idx;
5920         return skb->len;
5921 }
5922
5923 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
5924 {
5925         const struct nlattr * const *nla = cb->data;
5926         struct nft_flowtable_filter *filter = NULL;
5927
5928         if (nla[NFTA_FLOWTABLE_TABLE]) {
5929                 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
5930                 if (!filter)
5931                         return -ENOMEM;
5932
5933                 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
5934                                            GFP_ATOMIC);
5935                 if (!filter->table) {
5936                         kfree(filter);
5937                         return -ENOMEM;
5938                 }
5939         }
5940
5941         cb->data = filter;
5942         return 0;
5943 }
5944
5945 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
5946 {
5947         struct nft_flowtable_filter *filter = cb->data;
5948
5949         if (!filter)
5950                 return 0;
5951
5952         kfree(filter->table);
5953         kfree(filter);
5954
5955         return 0;
5956 }
5957
5958 /* called with rcu_read_lock held */
5959 static int nf_tables_getflowtable(struct net *net, struct sock *nlsk,
5960                                   struct sk_buff *skb,
5961                                   const struct nlmsghdr *nlh,
5962                                   const struct nlattr * const nla[],
5963                                   struct netlink_ext_ack *extack)
5964 {
5965         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
5966         u8 genmask = nft_genmask_cur(net);
5967         int family = nfmsg->nfgen_family;
5968         struct nft_flowtable *flowtable;
5969         const struct nft_table *table;
5970         struct sk_buff *skb2;
5971         int err;
5972
5973         if (nlh->nlmsg_flags & NLM_F_DUMP) {
5974                 struct netlink_dump_control c = {
5975                         .start = nf_tables_dump_flowtable_start,
5976                         .dump = nf_tables_dump_flowtable,
5977                         .done = nf_tables_dump_flowtable_done,
5978                         .module = THIS_MODULE,
5979                         .data = (void *)nla,
5980                 };
5981
5982                 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
5983         }
5984
5985         if (!nla[NFTA_FLOWTABLE_NAME])
5986                 return -EINVAL;
5987
5988         table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
5989                                  genmask);
5990         if (IS_ERR(table))
5991                 return PTR_ERR(table);
5992
5993         flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
5994                                          genmask);
5995         if (IS_ERR(flowtable))
5996                 return PTR_ERR(flowtable);
5997
5998         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
5999         if (!skb2)
6000                 return -ENOMEM;
6001
6002         err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
6003                                             nlh->nlmsg_seq,
6004                                             NFT_MSG_NEWFLOWTABLE, 0, family,
6005                                             flowtable);
6006         if (err < 0)
6007                 goto err;
6008
6009         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
6010 err:
6011         kfree_skb(skb2);
6012         return err;
6013 }
6014
6015 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
6016                                        struct nft_flowtable *flowtable,
6017                                        int event)
6018 {
6019         struct sk_buff *skb;
6020         int err;
6021
6022         if (ctx->report &&
6023             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
6024                 return;
6025
6026         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6027         if (skb == NULL)
6028                 goto err;
6029
6030         err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
6031                                             ctx->seq, event, 0,
6032                                             ctx->family, flowtable);
6033         if (err < 0) {
6034                 kfree_skb(skb);
6035                 goto err;
6036         }
6037
6038         nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
6039                        ctx->report, GFP_KERNEL);
6040         return;
6041 err:
6042         nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
6043 }
6044
6045 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
6046 {
6047         kfree(flowtable->ops);
6048         kfree(flowtable->name);
6049         flowtable->data.type->free(&flowtable->data);
6050         module_put(flowtable->data.type->owner);
6051         kfree(flowtable);
6052 }
6053
6054 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
6055                                    u32 portid, u32 seq)
6056 {
6057         struct nlmsghdr *nlh;
6058         struct nfgenmsg *nfmsg;
6059         char buf[TASK_COMM_LEN];
6060         int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
6061
6062         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
6063         if (nlh == NULL)
6064                 goto nla_put_failure;
6065
6066         nfmsg = nlmsg_data(nlh);
6067         nfmsg->nfgen_family     = AF_UNSPEC;
6068         nfmsg->version          = NFNETLINK_V0;
6069         nfmsg->res_id           = htons(net->nft.base_seq & 0xffff);
6070
6071         if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) ||
6072             nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
6073             nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
6074                 goto nla_put_failure;
6075
6076         nlmsg_end(skb, nlh);
6077         return 0;
6078
6079 nla_put_failure:
6080         nlmsg_trim(skb, nlh);
6081         return -EMSGSIZE;
6082 }
6083
6084 static void nft_flowtable_event(unsigned long event, struct net_device *dev,
6085                                 struct nft_flowtable *flowtable)
6086 {
6087         int i;
6088
6089         for (i = 0; i < flowtable->ops_len; i++) {
6090                 if (flowtable->ops[i].dev != dev)
6091                         continue;
6092
6093                 nf_unregister_net_hook(dev_net(dev), &flowtable->ops[i]);
6094                 flowtable->ops[i].dev = NULL;
6095                 break;
6096         }
6097 }
6098
6099 static int nf_tables_flowtable_event(struct notifier_block *this,
6100                                      unsigned long event, void *ptr)
6101 {
6102         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6103         struct nft_flowtable *flowtable;
6104         struct nft_table *table;
6105         struct net *net;
6106
6107         if (event != NETDEV_UNREGISTER)
6108                 return 0;
6109
6110         net = dev_net(dev);
6111         mutex_lock(&net->nft.commit_mutex);
6112         list_for_each_entry(table, &net->nft.tables, list) {
6113                 list_for_each_entry(flowtable, &table->flowtables, list) {
6114                         nft_flowtable_event(event, dev, flowtable);
6115                 }
6116         }
6117         mutex_unlock(&net->nft.commit_mutex);
6118
6119         return NOTIFY_DONE;
6120 }
6121
6122 static struct notifier_block nf_tables_flowtable_notifier = {
6123         .notifier_call  = nf_tables_flowtable_event,
6124 };
6125
6126 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
6127                                  int event)
6128 {
6129         struct nlmsghdr *nlh = nlmsg_hdr(skb);
6130         struct sk_buff *skb2;
6131         int err;
6132
6133         if (nlmsg_report(nlh) &&
6134             !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
6135                 return;
6136
6137         skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6138         if (skb2 == NULL)
6139                 goto err;
6140
6141         err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
6142                                       nlh->nlmsg_seq);
6143         if (err < 0) {
6144                 kfree_skb(skb2);
6145                 goto err;
6146         }
6147
6148         nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
6149                        nlmsg_report(nlh), GFP_KERNEL);
6150         return;
6151 err:
6152         nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
6153                           -ENOBUFS);
6154 }
6155
6156 static int nf_tables_getgen(struct net *net, struct sock *nlsk,
6157                             struct sk_buff *skb, const struct nlmsghdr *nlh,
6158                             const struct nlattr * const nla[],
6159                             struct netlink_ext_ack *extack)
6160 {
6161         struct sk_buff *skb2;
6162         int err;
6163
6164         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
6165         if (skb2 == NULL)
6166                 return -ENOMEM;
6167
6168         err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
6169                                       nlh->nlmsg_seq);
6170         if (err < 0)
6171                 goto err;
6172
6173         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
6174 err:
6175         kfree_skb(skb2);
6176         return err;
6177 }
6178
6179 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
6180         [NFT_MSG_NEWTABLE] = {
6181                 .call_batch     = nf_tables_newtable,
6182                 .attr_count     = NFTA_TABLE_MAX,
6183                 .policy         = nft_table_policy,
6184         },
6185         [NFT_MSG_GETTABLE] = {
6186                 .call_rcu       = nf_tables_gettable,
6187                 .attr_count     = NFTA_TABLE_MAX,
6188                 .policy         = nft_table_policy,
6189         },
6190         [NFT_MSG_DELTABLE] = {
6191                 .call_batch     = nf_tables_deltable,
6192                 .attr_count     = NFTA_TABLE_MAX,
6193                 .policy         = nft_table_policy,
6194         },
6195         [NFT_MSG_NEWCHAIN] = {
6196                 .call_batch     = nf_tables_newchain,
6197                 .attr_count     = NFTA_CHAIN_MAX,
6198                 .policy         = nft_chain_policy,
6199         },
6200         [NFT_MSG_GETCHAIN] = {
6201                 .call_rcu       = nf_tables_getchain,
6202                 .attr_count     = NFTA_CHAIN_MAX,
6203                 .policy         = nft_chain_policy,
6204         },
6205         [NFT_MSG_DELCHAIN] = {
6206                 .call_batch     = nf_tables_delchain,
6207                 .attr_count     = NFTA_CHAIN_MAX,
6208                 .policy         = nft_chain_policy,
6209         },
6210         [NFT_MSG_NEWRULE] = {
6211                 .call_batch     = nf_tables_newrule,
6212                 .attr_count     = NFTA_RULE_MAX,
6213                 .policy         = nft_rule_policy,
6214         },
6215         [NFT_MSG_GETRULE] = {
6216                 .call_rcu       = nf_tables_getrule,
6217                 .attr_count     = NFTA_RULE_MAX,
6218                 .policy         = nft_rule_policy,
6219         },
6220         [NFT_MSG_DELRULE] = {
6221                 .call_batch     = nf_tables_delrule,
6222                 .attr_count     = NFTA_RULE_MAX,
6223                 .policy         = nft_rule_policy,
6224         },
6225         [NFT_MSG_NEWSET] = {
6226                 .call_batch     = nf_tables_newset,
6227                 .attr_count     = NFTA_SET_MAX,
6228                 .policy         = nft_set_policy,
6229         },
6230         [NFT_MSG_GETSET] = {
6231                 .call_rcu       = nf_tables_getset,
6232                 .attr_count     = NFTA_SET_MAX,
6233                 .policy         = nft_set_policy,
6234         },
6235         [NFT_MSG_DELSET] = {
6236                 .call_batch     = nf_tables_delset,
6237                 .attr_count     = NFTA_SET_MAX,
6238                 .policy         = nft_set_policy,
6239         },
6240         [NFT_MSG_NEWSETELEM] = {
6241                 .call_batch     = nf_tables_newsetelem,
6242                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
6243                 .policy         = nft_set_elem_list_policy,
6244         },
6245         [NFT_MSG_GETSETELEM] = {
6246                 .call_rcu       = nf_tables_getsetelem,
6247                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
6248                 .policy         = nft_set_elem_list_policy,
6249         },
6250         [NFT_MSG_DELSETELEM] = {
6251                 .call_batch     = nf_tables_delsetelem,
6252                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
6253                 .policy         = nft_set_elem_list_policy,
6254         },
6255         [NFT_MSG_GETGEN] = {
6256                 .call_rcu       = nf_tables_getgen,
6257         },
6258         [NFT_MSG_NEWOBJ] = {
6259                 .call_batch     = nf_tables_newobj,
6260                 .attr_count     = NFTA_OBJ_MAX,
6261                 .policy         = nft_obj_policy,
6262         },
6263         [NFT_MSG_GETOBJ] = {
6264                 .call_rcu       = nf_tables_getobj,
6265                 .attr_count     = NFTA_OBJ_MAX,
6266                 .policy         = nft_obj_policy,
6267         },
6268         [NFT_MSG_DELOBJ] = {
6269                 .call_batch     = nf_tables_delobj,
6270                 .attr_count     = NFTA_OBJ_MAX,
6271                 .policy         = nft_obj_policy,
6272         },
6273         [NFT_MSG_GETOBJ_RESET] = {
6274                 .call_rcu       = nf_tables_getobj,
6275                 .attr_count     = NFTA_OBJ_MAX,
6276                 .policy         = nft_obj_policy,
6277         },
6278         [NFT_MSG_NEWFLOWTABLE] = {
6279                 .call_batch     = nf_tables_newflowtable,
6280                 .attr_count     = NFTA_FLOWTABLE_MAX,
6281                 .policy         = nft_flowtable_policy,
6282         },
6283         [NFT_MSG_GETFLOWTABLE] = {
6284                 .call_rcu       = nf_tables_getflowtable,
6285                 .attr_count     = NFTA_FLOWTABLE_MAX,
6286                 .policy         = nft_flowtable_policy,
6287         },
6288         [NFT_MSG_DELFLOWTABLE] = {
6289                 .call_batch     = nf_tables_delflowtable,
6290                 .attr_count     = NFTA_FLOWTABLE_MAX,
6291                 .policy         = nft_flowtable_policy,
6292         },
6293 };
6294
6295 static int nf_tables_validate(struct net *net)
6296 {
6297         struct nft_table *table;
6298
6299         switch (net->nft.validate_state) {
6300         case NFT_VALIDATE_SKIP:
6301                 break;
6302         case NFT_VALIDATE_NEED:
6303                 nft_validate_state_update(net, NFT_VALIDATE_DO);
6304                 /* fall through */
6305         case NFT_VALIDATE_DO:
6306                 list_for_each_entry(table, &net->nft.tables, list) {
6307                         if (nft_table_validate(net, table) < 0)
6308                                 return -EAGAIN;
6309                 }
6310                 break;
6311         }
6312
6313         return 0;
6314 }
6315
6316 /* a drop policy has to be deferred until all rules have been activated,
6317  * otherwise a large ruleset that contains a drop-policy base chain will
6318  * cause all packets to get dropped until the full transaction has been
6319  * processed.
6320  *
6321  * We defer the drop policy until the transaction has been finalized.
6322  */
6323 static void nft_chain_commit_drop_policy(struct nft_trans *trans)
6324 {
6325         struct nft_base_chain *basechain;
6326
6327         if (nft_trans_chain_policy(trans) != NF_DROP)
6328                 return;
6329
6330         if (!nft_is_base_chain(trans->ctx.chain))
6331                 return;
6332
6333         basechain = nft_base_chain(trans->ctx.chain);
6334         basechain->policy = NF_DROP;
6335 }
6336
6337 static void nft_chain_commit_update(struct nft_trans *trans)
6338 {
6339         struct nft_base_chain *basechain;
6340
6341         if (nft_trans_chain_name(trans)) {
6342                 rhltable_remove(&trans->ctx.table->chains_ht,
6343                                 &trans->ctx.chain->rhlhead,
6344                                 nft_chain_ht_params);
6345                 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
6346                 rhltable_insert_key(&trans->ctx.table->chains_ht,
6347                                     trans->ctx.chain->name,
6348                                     &trans->ctx.chain->rhlhead,
6349                                     nft_chain_ht_params);
6350         }
6351
6352         if (!nft_is_base_chain(trans->ctx.chain))
6353                 return;
6354
6355         nft_chain_stats_replace(trans);
6356
6357         basechain = nft_base_chain(trans->ctx.chain);
6358
6359         switch (nft_trans_chain_policy(trans)) {
6360         case NF_DROP:
6361         case NF_ACCEPT:
6362                 basechain->policy = nft_trans_chain_policy(trans);
6363                 break;
6364         }
6365 }
6366
6367 static void nft_commit_release(struct nft_trans *trans)
6368 {
6369         switch (trans->msg_type) {
6370         case NFT_MSG_DELTABLE:
6371                 nf_tables_table_destroy(&trans->ctx);
6372                 break;
6373         case NFT_MSG_NEWCHAIN:
6374                 free_percpu(nft_trans_chain_stats(trans));
6375                 kfree(nft_trans_chain_name(trans));
6376                 break;
6377         case NFT_MSG_DELCHAIN:
6378                 nf_tables_chain_destroy(&trans->ctx);
6379                 break;
6380         case NFT_MSG_DELRULE:
6381                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
6382                 break;
6383         case NFT_MSG_DELSET:
6384                 nft_set_destroy(nft_trans_set(trans));
6385                 break;
6386         case NFT_MSG_DELSETELEM:
6387                 nf_tables_set_elem_destroy(&trans->ctx,
6388                                            nft_trans_elem_set(trans),
6389                                            nft_trans_elem(trans).priv);
6390                 break;
6391         case NFT_MSG_DELOBJ:
6392                 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
6393                 break;
6394         case NFT_MSG_DELFLOWTABLE:
6395                 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
6396                 break;
6397         }
6398
6399         if (trans->put_net)
6400                 put_net(trans->ctx.net);
6401
6402         kfree(trans);
6403 }
6404
6405 static void nf_tables_trans_destroy_work(struct work_struct *w)
6406 {
6407         struct nft_trans *trans, *next;
6408         LIST_HEAD(head);
6409
6410         spin_lock(&nf_tables_destroy_list_lock);
6411         list_splice_init(&nf_tables_destroy_list, &head);
6412         spin_unlock(&nf_tables_destroy_list_lock);
6413
6414         if (list_empty(&head))
6415                 return;
6416
6417         synchronize_rcu();
6418
6419         list_for_each_entry_safe(trans, next, &head, list) {
6420                 list_del(&trans->list);
6421                 nft_commit_release(trans);
6422         }
6423 }
6424
6425 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
6426 {
6427         struct nft_rule *rule;
6428         unsigned int alloc = 0;
6429         int i;
6430
6431         /* already handled or inactive chain? */
6432         if (chain->rules_next || !nft_is_active_next(net, chain))
6433                 return 0;
6434
6435         rule = list_entry(&chain->rules, struct nft_rule, list);
6436         i = 0;
6437
6438         list_for_each_entry_continue(rule, &chain->rules, list) {
6439                 if (nft_is_active_next(net, rule))
6440                         alloc++;
6441         }
6442
6443         chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc);
6444         if (!chain->rules_next)
6445                 return -ENOMEM;
6446
6447         list_for_each_entry_continue(rule, &chain->rules, list) {
6448                 if (nft_is_active_next(net, rule))
6449                         chain->rules_next[i++] = rule;
6450         }
6451
6452         chain->rules_next[i] = NULL;
6453         return 0;
6454 }
6455
6456 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
6457 {
6458         struct nft_trans *trans, *next;
6459
6460         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6461                 struct nft_chain *chain = trans->ctx.chain;
6462
6463                 if (trans->msg_type == NFT_MSG_NEWRULE ||
6464                     trans->msg_type == NFT_MSG_DELRULE) {
6465                         kvfree(chain->rules_next);
6466                         chain->rules_next = NULL;
6467                 }
6468         }
6469 }
6470
6471 static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h)
6472 {
6473         struct nft_rules_old *o = container_of(h, struct nft_rules_old, h);
6474
6475         kvfree(o->start);
6476 }
6477
6478 static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules)
6479 {
6480         struct nft_rule **r = rules;
6481         struct nft_rules_old *old;
6482
6483         while (*r)
6484                 r++;
6485
6486         r++;    /* rcu_head is after end marker */
6487         old = (void *) r;
6488         old->start = rules;
6489
6490         call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old);
6491 }
6492
6493 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
6494 {
6495         struct nft_rule **g0, **g1;
6496         bool next_genbit;
6497
6498         next_genbit = nft_gencursor_next(net);
6499
6500         g0 = rcu_dereference_protected(chain->rules_gen_0,
6501                                        lockdep_commit_lock_is_held(net));
6502         g1 = rcu_dereference_protected(chain->rules_gen_1,
6503                                        lockdep_commit_lock_is_held(net));
6504
6505         /* No changes to this chain? */
6506         if (chain->rules_next == NULL) {
6507                 /* chain had no change in last or next generation */
6508                 if (g0 == g1)
6509                         return;
6510                 /*
6511                  * chain had no change in this generation; make sure next
6512                  * one uses same rules as current generation.
6513                  */
6514                 if (next_genbit) {
6515                         rcu_assign_pointer(chain->rules_gen_1, g0);
6516                         nf_tables_commit_chain_free_rules_old(g1);
6517                 } else {
6518                         rcu_assign_pointer(chain->rules_gen_0, g1);
6519                         nf_tables_commit_chain_free_rules_old(g0);
6520                 }
6521
6522                 return;
6523         }
6524
6525         if (next_genbit)
6526                 rcu_assign_pointer(chain->rules_gen_1, chain->rules_next);
6527         else
6528                 rcu_assign_pointer(chain->rules_gen_0, chain->rules_next);
6529
6530         chain->rules_next = NULL;
6531
6532         if (g0 == g1)
6533                 return;
6534
6535         if (next_genbit)
6536                 nf_tables_commit_chain_free_rules_old(g1);
6537         else
6538                 nf_tables_commit_chain_free_rules_old(g0);
6539 }
6540
6541 static void nft_obj_del(struct nft_object *obj)
6542 {
6543         rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
6544         list_del_rcu(&obj->list);
6545 }
6546
6547 static void nft_chain_del(struct nft_chain *chain)
6548 {
6549         struct nft_table *table = chain->table;
6550
6551         WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
6552                                      nft_chain_ht_params));
6553         list_del_rcu(&chain->list);
6554 }
6555
6556 static void nf_tables_commit_release(struct net *net)
6557 {
6558         struct nft_trans *trans;
6559
6560         /* all side effects have to be made visible.
6561          * For example, if a chain named 'foo' has been deleted, a
6562          * new transaction must not find it anymore.
6563          *
6564          * Memory reclaim happens asynchronously from work queue
6565          * to prevent expensive synchronize_rcu() in commit phase.
6566          */
6567         if (list_empty(&net->nft.commit_list)) {
6568                 mutex_unlock(&net->nft.commit_mutex);
6569                 return;
6570         }
6571
6572         trans = list_last_entry(&net->nft.commit_list,
6573                                 struct nft_trans, list);
6574         get_net(trans->ctx.net);
6575         WARN_ON_ONCE(trans->put_net);
6576
6577         trans->put_net = true;
6578         spin_lock(&nf_tables_destroy_list_lock);
6579         list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list);
6580         spin_unlock(&nf_tables_destroy_list_lock);
6581
6582         mutex_unlock(&net->nft.commit_mutex);
6583
6584         schedule_work(&trans_destroy_work);
6585 }
6586
6587 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
6588 {
6589         struct nft_trans *trans, *next;
6590         struct nft_trans_elem *te;
6591         struct nft_chain *chain;
6592         struct nft_table *table;
6593
6594         if (list_empty(&net->nft.commit_list)) {
6595                 mutex_unlock(&net->nft.commit_mutex);
6596                 return 0;
6597         }
6598
6599         /* 0. Validate ruleset, otherwise roll back for error reporting. */
6600         if (nf_tables_validate(net) < 0)
6601                 return -EAGAIN;
6602
6603         /* 1.  Allocate space for next generation rules_gen_X[] */
6604         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6605                 int ret;
6606
6607                 if (trans->msg_type == NFT_MSG_NEWRULE ||
6608                     trans->msg_type == NFT_MSG_DELRULE) {
6609                         chain = trans->ctx.chain;
6610
6611                         ret = nf_tables_commit_chain_prepare(net, chain);
6612                         if (ret < 0) {
6613                                 nf_tables_commit_chain_prepare_cancel(net);
6614                                 return ret;
6615                         }
6616                 }
6617         }
6618
6619         /* step 2.  Make rules_gen_X visible to packet path */
6620         list_for_each_entry(table, &net->nft.tables, list) {
6621                 list_for_each_entry(chain, &table->chains, list)
6622                         nf_tables_commit_chain(net, chain);
6623         }
6624
6625         /*
6626          * Bump generation counter, invalidate any dump in progress.
6627          * Cannot fail after this point.
6628          */
6629         while (++net->nft.base_seq == 0);
6630
6631         /* step 3. Start new generation, rules_gen_X now in use. */
6632         net->nft.gencursor = nft_gencursor_next(net);
6633
6634         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
6635                 switch (trans->msg_type) {
6636                 case NFT_MSG_NEWTABLE:
6637                         if (nft_trans_table_update(trans)) {
6638                                 if (!nft_trans_table_enable(trans)) {
6639                                         nf_tables_table_disable(net,
6640                                                                 trans->ctx.table);
6641                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
6642                                 }
6643                         } else {
6644                                 nft_clear(net, trans->ctx.table);
6645                         }
6646                         nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
6647                         nft_trans_destroy(trans);
6648                         break;
6649                 case NFT_MSG_DELTABLE:
6650                         list_del_rcu(&trans->ctx.table->list);
6651                         nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
6652                         break;
6653                 case NFT_MSG_NEWCHAIN:
6654                         if (nft_trans_chain_update(trans)) {
6655                                 nft_chain_commit_update(trans);
6656                                 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
6657                                 /* trans destroyed after rcu grace period */
6658                         } else {
6659                                 nft_chain_commit_drop_policy(trans);
6660                                 nft_clear(net, trans->ctx.chain);
6661                                 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
6662                                 nft_trans_destroy(trans);
6663                         }
6664                         break;
6665                 case NFT_MSG_DELCHAIN:
6666                         nft_chain_del(trans->ctx.chain);
6667                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
6668                         nf_tables_unregister_hook(trans->ctx.net,
6669                                                   trans->ctx.table,
6670                                                   trans->ctx.chain);
6671                         break;
6672                 case NFT_MSG_NEWRULE:
6673                         nft_clear(trans->ctx.net, nft_trans_rule(trans));
6674                         nf_tables_rule_notify(&trans->ctx,
6675                                               nft_trans_rule(trans),
6676                                               NFT_MSG_NEWRULE);
6677                         nft_trans_destroy(trans);
6678                         break;
6679                 case NFT_MSG_DELRULE:
6680                         list_del_rcu(&nft_trans_rule(trans)->list);
6681                         nf_tables_rule_notify(&trans->ctx,
6682                                               nft_trans_rule(trans),
6683                                               NFT_MSG_DELRULE);
6684                         nft_rule_expr_deactivate(&trans->ctx,
6685                                                  nft_trans_rule(trans),
6686                                                  NFT_TRANS_COMMIT);
6687                         break;
6688                 case NFT_MSG_NEWSET:
6689                         nft_clear(net, nft_trans_set(trans));
6690                         /* This avoids hitting -EBUSY when deleting the table
6691                          * from the transaction.
6692                          */
6693                         if (nft_set_is_anonymous(nft_trans_set(trans)) &&
6694                             !list_empty(&nft_trans_set(trans)->bindings))
6695                                 trans->ctx.table->use--;
6696
6697                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
6698                                              NFT_MSG_NEWSET, GFP_KERNEL);
6699                         nft_trans_destroy(trans);
6700                         break;
6701                 case NFT_MSG_DELSET:
6702                         list_del_rcu(&nft_trans_set(trans)->list);
6703                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
6704                                              NFT_MSG_DELSET, GFP_KERNEL);
6705                         break;
6706                 case NFT_MSG_NEWSETELEM:
6707                         te = (struct nft_trans_elem *)trans->data;
6708
6709                         te->set->ops->activate(net, te->set, &te->elem);
6710                         nf_tables_setelem_notify(&trans->ctx, te->set,
6711                                                  &te->elem,
6712                                                  NFT_MSG_NEWSETELEM, 0);
6713                         nft_trans_destroy(trans);
6714                         break;
6715                 case NFT_MSG_DELSETELEM:
6716                         te = (struct nft_trans_elem *)trans->data;
6717
6718                         nf_tables_setelem_notify(&trans->ctx, te->set,
6719                                                  &te->elem,
6720                                                  NFT_MSG_DELSETELEM, 0);
6721                         te->set->ops->remove(net, te->set, &te->elem);
6722                         atomic_dec(&te->set->nelems);
6723                         te->set->ndeact--;
6724                         break;
6725                 case NFT_MSG_NEWOBJ:
6726                         nft_clear(net, nft_trans_obj(trans));
6727                         nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
6728                                              NFT_MSG_NEWOBJ);
6729                         nft_trans_destroy(trans);
6730                         break;
6731                 case NFT_MSG_DELOBJ:
6732                         nft_obj_del(nft_trans_obj(trans));
6733                         nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
6734                                              NFT_MSG_DELOBJ);
6735                         break;
6736                 case NFT_MSG_NEWFLOWTABLE:
6737                         nft_clear(net, nft_trans_flowtable(trans));
6738                         nf_tables_flowtable_notify(&trans->ctx,
6739                                                    nft_trans_flowtable(trans),
6740                                                    NFT_MSG_NEWFLOWTABLE);
6741                         nft_trans_destroy(trans);
6742                         break;
6743                 case NFT_MSG_DELFLOWTABLE:
6744                         list_del_rcu(&nft_trans_flowtable(trans)->list);
6745                         nf_tables_flowtable_notify(&trans->ctx,
6746                                                    nft_trans_flowtable(trans),
6747                                                    NFT_MSG_DELFLOWTABLE);
6748                         nft_unregister_flowtable_net_hooks(net,
6749                                         nft_trans_flowtable(trans));
6750                         break;
6751                 }
6752         }
6753
6754         nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
6755         nf_tables_commit_release(net);
6756
6757         return 0;
6758 }
6759
6760 static void nf_tables_abort_release(struct nft_trans *trans)
6761 {
6762         switch (trans->msg_type) {
6763         case NFT_MSG_NEWTABLE:
6764                 nf_tables_table_destroy(&trans->ctx);
6765                 break;
6766         case NFT_MSG_NEWCHAIN:
6767                 nf_tables_chain_destroy(&trans->ctx);
6768                 break;
6769         case NFT_MSG_NEWRULE:
6770                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
6771                 break;
6772         case NFT_MSG_NEWSET:
6773                 nft_set_destroy(nft_trans_set(trans));
6774                 break;
6775         case NFT_MSG_NEWSETELEM:
6776                 nft_set_elem_destroy(nft_trans_elem_set(trans),
6777                                      nft_trans_elem(trans).priv, true);
6778                 break;
6779         case NFT_MSG_NEWOBJ:
6780                 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
6781                 break;
6782         case NFT_MSG_NEWFLOWTABLE:
6783                 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
6784                 break;
6785         }
6786         kfree(trans);
6787 }
6788
6789 static int __nf_tables_abort(struct net *net)
6790 {
6791         struct nft_trans *trans, *next;
6792         struct nft_trans_elem *te;
6793
6794         list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
6795                                          list) {
6796                 switch (trans->msg_type) {
6797                 case NFT_MSG_NEWTABLE:
6798                         if (nft_trans_table_update(trans)) {
6799                                 if (nft_trans_table_enable(trans)) {
6800                                         nf_tables_table_disable(net,
6801                                                                 trans->ctx.table);
6802                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
6803                                 }
6804                                 nft_trans_destroy(trans);
6805                         } else {
6806                                 list_del_rcu(&trans->ctx.table->list);
6807                         }
6808                         break;
6809                 case NFT_MSG_DELTABLE:
6810                         nft_clear(trans->ctx.net, trans->ctx.table);
6811                         nft_trans_destroy(trans);
6812                         break;
6813                 case NFT_MSG_NEWCHAIN:
6814                         if (nft_trans_chain_update(trans)) {
6815                                 free_percpu(nft_trans_chain_stats(trans));
6816                                 kfree(nft_trans_chain_name(trans));
6817                                 nft_trans_destroy(trans);
6818                         } else {
6819                                 trans->ctx.table->use--;
6820                                 nft_chain_del(trans->ctx.chain);
6821                                 nf_tables_unregister_hook(trans->ctx.net,
6822                                                           trans->ctx.table,
6823                                                           trans->ctx.chain);
6824                         }
6825                         break;
6826                 case NFT_MSG_DELCHAIN:
6827                         trans->ctx.table->use++;
6828                         nft_clear(trans->ctx.net, trans->ctx.chain);
6829                         nft_trans_destroy(trans);
6830                         break;
6831                 case NFT_MSG_NEWRULE:
6832                         trans->ctx.chain->use--;
6833                         list_del_rcu(&nft_trans_rule(trans)->list);
6834                         nft_rule_expr_deactivate(&trans->ctx,
6835                                                  nft_trans_rule(trans),
6836                                                  NFT_TRANS_ABORT);
6837                         break;
6838                 case NFT_MSG_DELRULE:
6839                         trans->ctx.chain->use++;
6840                         nft_clear(trans->ctx.net, nft_trans_rule(trans));
6841                         nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
6842                         nft_trans_destroy(trans);
6843                         break;
6844                 case NFT_MSG_NEWSET:
6845                         trans->ctx.table->use--;
6846                         if (nft_trans_set(trans)->bound) {
6847                                 nft_trans_destroy(trans);
6848                                 break;
6849                         }
6850                         list_del_rcu(&nft_trans_set(trans)->list);
6851                         break;
6852                 case NFT_MSG_DELSET:
6853                         trans->ctx.table->use++;
6854                         nft_clear(trans->ctx.net, nft_trans_set(trans));
6855                         nft_trans_destroy(trans);
6856                         break;
6857                 case NFT_MSG_NEWSETELEM:
6858                         if (nft_trans_elem_set(trans)->bound) {
6859                                 nft_trans_destroy(trans);
6860                                 break;
6861                         }
6862                         te = (struct nft_trans_elem *)trans->data;
6863                         te->set->ops->remove(net, te->set, &te->elem);
6864                         atomic_dec(&te->set->nelems);
6865                         break;
6866                 case NFT_MSG_DELSETELEM:
6867                         te = (struct nft_trans_elem *)trans->data;
6868
6869                         nft_set_elem_activate(net, te->set, &te->elem);
6870                         te->set->ops->activate(net, te->set, &te->elem);
6871                         te->set->ndeact--;
6872
6873                         nft_trans_destroy(trans);
6874                         break;
6875                 case NFT_MSG_NEWOBJ:
6876                         trans->ctx.table->use--;
6877                         nft_obj_del(nft_trans_obj(trans));
6878                         break;
6879                 case NFT_MSG_DELOBJ:
6880                         trans->ctx.table->use++;
6881                         nft_clear(trans->ctx.net, nft_trans_obj(trans));
6882                         nft_trans_destroy(trans);
6883                         break;
6884                 case NFT_MSG_NEWFLOWTABLE:
6885                         trans->ctx.table->use--;
6886                         list_del_rcu(&nft_trans_flowtable(trans)->list);
6887                         nft_unregister_flowtable_net_hooks(net,
6888                                         nft_trans_flowtable(trans));
6889                         break;
6890                 case NFT_MSG_DELFLOWTABLE:
6891                         trans->ctx.table->use++;
6892                         nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
6893                         nft_trans_destroy(trans);
6894                         break;
6895                 }
6896         }
6897
6898         synchronize_rcu();
6899
6900         list_for_each_entry_safe_reverse(trans, next,
6901                                          &net->nft.commit_list, list) {
6902                 list_del(&trans->list);
6903                 nf_tables_abort_release(trans);
6904         }
6905
6906         return 0;
6907 }
6908
6909 static void nf_tables_cleanup(struct net *net)
6910 {
6911         nft_validate_state_update(net, NFT_VALIDATE_SKIP);
6912 }
6913
6914 static int nf_tables_abort(struct net *net, struct sk_buff *skb)
6915 {
6916         int ret = __nf_tables_abort(net);
6917
6918         mutex_unlock(&net->nft.commit_mutex);
6919
6920         return ret;
6921 }
6922
6923 static bool nf_tables_valid_genid(struct net *net, u32 genid)
6924 {
6925         bool genid_ok;
6926
6927         mutex_lock(&net->nft.commit_mutex);
6928
6929         genid_ok = genid == 0 || net->nft.base_seq == genid;
6930         if (!genid_ok)
6931                 mutex_unlock(&net->nft.commit_mutex);
6932
6933         /* else, commit mutex has to be released by commit or abort function */
6934         return genid_ok;
6935 }
6936
6937 static const struct nfnetlink_subsystem nf_tables_subsys = {
6938         .name           = "nf_tables",
6939         .subsys_id      = NFNL_SUBSYS_NFTABLES,
6940         .cb_count       = NFT_MSG_MAX,
6941         .cb             = nf_tables_cb,
6942         .commit         = nf_tables_commit,
6943         .abort          = nf_tables_abort,
6944         .cleanup        = nf_tables_cleanup,
6945         .valid_genid    = nf_tables_valid_genid,
6946         .owner          = THIS_MODULE,
6947 };
6948
6949 int nft_chain_validate_dependency(const struct nft_chain *chain,
6950                                   enum nft_chain_types type)
6951 {
6952         const struct nft_base_chain *basechain;
6953
6954         if (nft_is_base_chain(chain)) {
6955                 basechain = nft_base_chain(chain);
6956                 if (basechain->type->type != type)
6957                         return -EOPNOTSUPP;
6958         }
6959         return 0;
6960 }
6961 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
6962
6963 int nft_chain_validate_hooks(const struct nft_chain *chain,
6964                              unsigned int hook_flags)
6965 {
6966         struct nft_base_chain *basechain;
6967
6968         if (nft_is_base_chain(chain)) {
6969                 basechain = nft_base_chain(chain);
6970
6971                 if ((1 << basechain->ops.hooknum) & hook_flags)
6972                         return 0;
6973
6974                 return -EOPNOTSUPP;
6975         }
6976
6977         return 0;
6978 }
6979 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
6980
6981 /*
6982  * Loop detection - walk through the ruleset beginning at the destination chain
6983  * of a new jump until either the source chain is reached (loop) or all
6984  * reachable chains have been traversed.
6985  *
6986  * The loop check is performed whenever a new jump verdict is added to an
6987  * expression or verdict map or a verdict map is bound to a new chain.
6988  */
6989
6990 static int nf_tables_check_loops(const struct nft_ctx *ctx,
6991                                  const struct nft_chain *chain);
6992
6993 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
6994                                         struct nft_set *set,
6995                                         const struct nft_set_iter *iter,
6996                                         struct nft_set_elem *elem)
6997 {
6998         const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6999         const struct nft_data *data;
7000
7001         if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
7002             *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
7003                 return 0;
7004
7005         data = nft_set_ext_data(ext);
7006         switch (data->verdict.code) {
7007         case NFT_JUMP:
7008         case NFT_GOTO:
7009                 return nf_tables_check_loops(ctx, data->verdict.chain);
7010         default:
7011                 return 0;
7012         }
7013 }
7014
7015 static int nf_tables_check_loops(const struct nft_ctx *ctx,
7016                                  const struct nft_chain *chain)
7017 {
7018         const struct nft_rule *rule;
7019         const struct nft_expr *expr, *last;
7020         struct nft_set *set;
7021         struct nft_set_binding *binding;
7022         struct nft_set_iter iter;
7023
7024         if (ctx->chain == chain)
7025                 return -ELOOP;
7026
7027         list_for_each_entry(rule, &chain->rules, list) {
7028                 nft_rule_for_each_expr(expr, last, rule) {
7029                         struct nft_immediate_expr *priv;
7030                         const struct nft_data *data;
7031                         int err;
7032
7033                         if (strcmp(expr->ops->type->name, "immediate"))
7034                                 continue;
7035
7036                         priv = nft_expr_priv(expr);
7037                         if (priv->dreg != NFT_REG_VERDICT)
7038                                 continue;
7039
7040                         data = &priv->data;
7041                         switch (data->verdict.code) {
7042                         case NFT_JUMP:
7043                         case NFT_GOTO:
7044                                 err = nf_tables_check_loops(ctx,
7045                                                         data->verdict.chain);
7046                                 if (err < 0)
7047                                         return err;
7048                         default:
7049                                 break;
7050                         }
7051                 }
7052         }
7053
7054         list_for_each_entry(set, &ctx->table->sets, list) {
7055                 if (!nft_is_active_next(ctx->net, set))
7056                         continue;
7057                 if (!(set->flags & NFT_SET_MAP) ||
7058                     set->dtype != NFT_DATA_VERDICT)
7059                         continue;
7060
7061                 list_for_each_entry(binding, &set->bindings, list) {
7062                         if (!(binding->flags & NFT_SET_MAP) ||
7063                             binding->chain != chain)
7064                                 continue;
7065
7066                         iter.genmask    = nft_genmask_next(ctx->net);
7067                         iter.skip       = 0;
7068                         iter.count      = 0;
7069                         iter.err        = 0;
7070                         iter.fn         = nf_tables_loop_check_setelem;
7071
7072                         set->ops->walk(ctx, set, &iter);
7073                         if (iter.err < 0)
7074                                 return iter.err;
7075                 }
7076         }
7077
7078         return 0;
7079 }
7080
7081 /**
7082  *      nft_parse_u32_check - fetch u32 attribute and check for maximum value
7083  *
7084  *      @attr: netlink attribute to fetch value from
7085  *      @max: maximum value to be stored in dest
7086  *      @dest: pointer to the variable
7087  *
7088  *      Parse, check and store a given u32 netlink attribute into variable.
7089  *      This function returns -ERANGE if the value goes over maximum value.
7090  *      Otherwise a 0 is returned and the attribute value is stored in the
7091  *      destination variable.
7092  */
7093 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
7094 {
7095         u32 val;
7096
7097         val = ntohl(nla_get_be32(attr));
7098         if (val > max)
7099                 return -ERANGE;
7100
7101         *dest = val;
7102         return 0;
7103 }
7104 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
7105
7106 /**
7107  *      nft_parse_register - parse a register value from a netlink attribute
7108  *
7109  *      @attr: netlink attribute
7110  *
7111  *      Parse and translate a register value from a netlink attribute.
7112  *      Registers used to be 128 bit wide, these register numbers will be
7113  *      mapped to the corresponding 32 bit register numbers.
7114  */
7115 unsigned int nft_parse_register(const struct nlattr *attr)
7116 {
7117         unsigned int reg;
7118
7119         reg = ntohl(nla_get_be32(attr));
7120         switch (reg) {
7121         case NFT_REG_VERDICT...NFT_REG_4:
7122                 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
7123         default:
7124                 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
7125         }
7126 }
7127 EXPORT_SYMBOL_GPL(nft_parse_register);
7128
7129 /**
7130  *      nft_dump_register - dump a register value to a netlink attribute
7131  *
7132  *      @skb: socket buffer
7133  *      @attr: attribute number
7134  *      @reg: register number
7135  *
7136  *      Construct a netlink attribute containing the register number. For
7137  *      compatibility reasons, register numbers being a multiple of 4 are
7138  *      translated to the corresponding 128 bit register numbers.
7139  */
7140 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
7141 {
7142         if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
7143                 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
7144         else
7145                 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
7146
7147         return nla_put_be32(skb, attr, htonl(reg));
7148 }
7149 EXPORT_SYMBOL_GPL(nft_dump_register);
7150
7151 /**
7152  *      nft_validate_register_load - validate a load from a register
7153  *
7154  *      @reg: the register number
7155  *      @len: the length of the data
7156  *
7157  *      Validate that the input register is one of the general purpose
7158  *      registers and that the length of the load is within the bounds.
7159  */
7160 int nft_validate_register_load(enum nft_registers reg, unsigned int len)
7161 {
7162         if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
7163                 return -EINVAL;
7164         if (len == 0)
7165                 return -EINVAL;
7166         if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
7167                 return -ERANGE;
7168
7169         return 0;
7170 }
7171 EXPORT_SYMBOL_GPL(nft_validate_register_load);
7172
7173 /**
7174  *      nft_validate_register_store - validate an expressions' register store
7175  *
7176  *      @ctx: context of the expression performing the load
7177  *      @reg: the destination register number
7178  *      @data: the data to load
7179  *      @type: the data type
7180  *      @len: the length of the data
7181  *
7182  *      Validate that a data load uses the appropriate data type for
7183  *      the destination register and the length is within the bounds.
7184  *      A value of NULL for the data means that its runtime gathered
7185  *      data.
7186  */
7187 int nft_validate_register_store(const struct nft_ctx *ctx,
7188                                 enum nft_registers reg,
7189                                 const struct nft_data *data,
7190                                 enum nft_data_types type, unsigned int len)
7191 {
7192         int err;
7193
7194         switch (reg) {
7195         case NFT_REG_VERDICT:
7196                 if (type != NFT_DATA_VERDICT)
7197                         return -EINVAL;
7198
7199                 if (data != NULL &&
7200                     (data->verdict.code == NFT_GOTO ||
7201                      data->verdict.code == NFT_JUMP)) {
7202                         err = nf_tables_check_loops(ctx, data->verdict.chain);
7203                         if (err < 0)
7204                                 return err;
7205                 }
7206
7207                 return 0;
7208         default:
7209                 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
7210                         return -EINVAL;
7211                 if (len == 0)
7212                         return -EINVAL;
7213                 if (reg * NFT_REG32_SIZE + len >
7214                     FIELD_SIZEOF(struct nft_regs, data))
7215                         return -ERANGE;
7216
7217                 if (data != NULL && type != NFT_DATA_VALUE)
7218                         return -EINVAL;
7219                 return 0;
7220         }
7221 }
7222 EXPORT_SYMBOL_GPL(nft_validate_register_store);
7223
7224 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
7225         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
7226         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
7227                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
7228 };
7229
7230 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
7231                             struct nft_data_desc *desc, const struct nlattr *nla)
7232 {
7233         u8 genmask = nft_genmask_next(ctx->net);
7234         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
7235         struct nft_chain *chain;
7236         int err;
7237
7238         err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
7239                                           nft_verdict_policy, NULL);
7240         if (err < 0)
7241                 return err;
7242
7243         if (!tb[NFTA_VERDICT_CODE])
7244                 return -EINVAL;
7245         data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
7246
7247         switch (data->verdict.code) {
7248         default:
7249                 switch (data->verdict.code & NF_VERDICT_MASK) {
7250                 case NF_ACCEPT:
7251                 case NF_DROP:
7252                 case NF_QUEUE:
7253                         break;
7254                 default:
7255                         return -EINVAL;
7256                 }
7257                 /* fall through */
7258         case NFT_CONTINUE:
7259         case NFT_BREAK:
7260         case NFT_RETURN:
7261                 break;
7262         case NFT_JUMP:
7263         case NFT_GOTO:
7264                 if (!tb[NFTA_VERDICT_CHAIN])
7265                         return -EINVAL;
7266                 chain = nft_chain_lookup(ctx->net, ctx->table,
7267                                          tb[NFTA_VERDICT_CHAIN], genmask);
7268                 if (IS_ERR(chain))
7269                         return PTR_ERR(chain);
7270                 if (nft_is_base_chain(chain))
7271                         return -EOPNOTSUPP;
7272
7273                 chain->use++;
7274                 data->verdict.chain = chain;
7275                 break;
7276         }
7277
7278         desc->len = sizeof(data->verdict);
7279         desc->type = NFT_DATA_VERDICT;
7280         return 0;
7281 }
7282
7283 static void nft_verdict_uninit(const struct nft_data *data)
7284 {
7285         switch (data->verdict.code) {
7286         case NFT_JUMP:
7287         case NFT_GOTO:
7288                 data->verdict.chain->use--;
7289                 break;
7290         }
7291 }
7292
7293 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
7294 {
7295         struct nlattr *nest;
7296
7297         nest = nla_nest_start_noflag(skb, type);
7298         if (!nest)
7299                 goto nla_put_failure;
7300
7301         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
7302                 goto nla_put_failure;
7303
7304         switch (v->code) {
7305         case NFT_JUMP:
7306         case NFT_GOTO:
7307                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
7308                                    v->chain->name))
7309                         goto nla_put_failure;
7310         }
7311         nla_nest_end(skb, nest);
7312         return 0;
7313
7314 nla_put_failure:
7315         return -1;
7316 }
7317
7318 static int nft_value_init(const struct nft_ctx *ctx,
7319                           struct nft_data *data, unsigned int size,
7320                           struct nft_data_desc *desc, const struct nlattr *nla)
7321 {
7322         unsigned int len;
7323
7324         len = nla_len(nla);
7325         if (len == 0)
7326                 return -EINVAL;
7327         if (len > size)
7328                 return -EOVERFLOW;
7329
7330         nla_memcpy(data->data, nla, len);
7331         desc->type = NFT_DATA_VALUE;
7332         desc->len  = len;
7333         return 0;
7334 }
7335
7336 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
7337                           unsigned int len)
7338 {
7339         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
7340 }
7341
7342 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
7343         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY },
7344         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
7345 };
7346
7347 /**
7348  *      nft_data_init - parse nf_tables data netlink attributes
7349  *
7350  *      @ctx: context of the expression using the data
7351  *      @data: destination struct nft_data
7352  *      @size: maximum data length
7353  *      @desc: data description
7354  *      @nla: netlink attribute containing data
7355  *
7356  *      Parse the netlink data attributes and initialize a struct nft_data.
7357  *      The type and length of data are returned in the data description.
7358  *
7359  *      The caller can indicate that it only wants to accept data of type
7360  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
7361  */
7362 int nft_data_init(const struct nft_ctx *ctx,
7363                   struct nft_data *data, unsigned int size,
7364                   struct nft_data_desc *desc, const struct nlattr *nla)
7365 {
7366         struct nlattr *tb[NFTA_DATA_MAX + 1];
7367         int err;
7368
7369         err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
7370                                           nft_data_policy, NULL);
7371         if (err < 0)
7372                 return err;
7373
7374         if (tb[NFTA_DATA_VALUE])
7375                 return nft_value_init(ctx, data, size, desc,
7376                                       tb[NFTA_DATA_VALUE]);
7377         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
7378                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
7379         return -EINVAL;
7380 }
7381 EXPORT_SYMBOL_GPL(nft_data_init);
7382
7383 /**
7384  *      nft_data_release - release a nft_data item
7385  *
7386  *      @data: struct nft_data to release
7387  *      @type: type of data
7388  *
7389  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
7390  *      all others need to be released by calling this function.
7391  */
7392 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
7393 {
7394         if (type < NFT_DATA_VERDICT)
7395                 return;
7396         switch (type) {
7397         case NFT_DATA_VERDICT:
7398                 return nft_verdict_uninit(data);
7399         default:
7400                 WARN_ON(1);
7401         }
7402 }
7403 EXPORT_SYMBOL_GPL(nft_data_release);
7404
7405 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
7406                   enum nft_data_types type, unsigned int len)
7407 {
7408         struct nlattr *nest;
7409         int err;
7410
7411         nest = nla_nest_start_noflag(skb, attr);
7412         if (nest == NULL)
7413                 return -1;
7414
7415         switch (type) {
7416         case NFT_DATA_VALUE:
7417                 err = nft_value_dump(skb, data, len);
7418                 break;
7419         case NFT_DATA_VERDICT:
7420                 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
7421                 break;
7422         default:
7423                 err = -EINVAL;
7424                 WARN_ON(1);
7425         }
7426
7427         nla_nest_end(skb, nest);
7428         return err;
7429 }
7430 EXPORT_SYMBOL_GPL(nft_data_dump);
7431
7432 int __nft_release_basechain(struct nft_ctx *ctx)
7433 {
7434         struct nft_rule *rule, *nr;
7435
7436         if (WARN_ON(!nft_is_base_chain(ctx->chain)))
7437                 return 0;
7438
7439         nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
7440         list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
7441                 list_del(&rule->list);
7442                 ctx->chain->use--;
7443                 nf_tables_rule_release(ctx, rule);
7444         }
7445         nft_chain_del(ctx->chain);
7446         ctx->table->use--;
7447         nf_tables_chain_destroy(ctx);
7448
7449         return 0;
7450 }
7451 EXPORT_SYMBOL_GPL(__nft_release_basechain);
7452
7453 static void __nft_release_tables(struct net *net)
7454 {
7455         struct nft_flowtable *flowtable, *nf;
7456         struct nft_table *table, *nt;
7457         struct nft_chain *chain, *nc;
7458         struct nft_object *obj, *ne;
7459         struct nft_rule *rule, *nr;
7460         struct nft_set *set, *ns;
7461         struct nft_ctx ctx = {
7462                 .net    = net,
7463                 .family = NFPROTO_NETDEV,
7464         };
7465
7466         list_for_each_entry_safe(table, nt, &net->nft.tables, list) {
7467                 ctx.family = table->family;
7468
7469                 list_for_each_entry(chain, &table->chains, list)
7470                         nf_tables_unregister_hook(net, table, chain);
7471                 /* No packets are walking on these chains anymore. */
7472                 ctx.table = table;
7473                 list_for_each_entry(chain, &table->chains, list) {
7474                         ctx.chain = chain;
7475                         list_for_each_entry_safe(rule, nr, &chain->rules, list) {
7476                                 list_del(&rule->list);
7477                                 chain->use--;
7478                                 nf_tables_rule_release(&ctx, rule);
7479                         }
7480                 }
7481                 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
7482                         list_del(&flowtable->list);
7483                         table->use--;
7484                         nf_tables_flowtable_destroy(flowtable);
7485                 }
7486                 list_for_each_entry_safe(set, ns, &table->sets, list) {
7487                         list_del(&set->list);
7488                         table->use--;
7489                         nft_set_destroy(set);
7490                 }
7491                 list_for_each_entry_safe(obj, ne, &table->objects, list) {
7492                         nft_obj_del(obj);
7493                         table->use--;
7494                         nft_obj_destroy(&ctx, obj);
7495                 }
7496                 list_for_each_entry_safe(chain, nc, &table->chains, list) {
7497                         ctx.chain = chain;
7498                         nft_chain_del(chain);
7499                         table->use--;
7500                         nf_tables_chain_destroy(&ctx);
7501                 }
7502                 list_del(&table->list);
7503                 nf_tables_table_destroy(&ctx);
7504         }
7505 }
7506
7507 static int __net_init nf_tables_init_net(struct net *net)
7508 {
7509         INIT_LIST_HEAD(&net->nft.tables);
7510         INIT_LIST_HEAD(&net->nft.commit_list);
7511         mutex_init(&net->nft.commit_mutex);
7512         net->nft.base_seq = 1;
7513         net->nft.validate_state = NFT_VALIDATE_SKIP;
7514
7515         return 0;
7516 }
7517
7518 static void __net_exit nf_tables_exit_net(struct net *net)
7519 {
7520         mutex_lock(&net->nft.commit_mutex);
7521         if (!list_empty(&net->nft.commit_list))
7522                 __nf_tables_abort(net);
7523         __nft_release_tables(net);
7524         mutex_unlock(&net->nft.commit_mutex);
7525         WARN_ON_ONCE(!list_empty(&net->nft.tables));
7526 }
7527
7528 static struct pernet_operations nf_tables_net_ops = {
7529         .init   = nf_tables_init_net,
7530         .exit   = nf_tables_exit_net,
7531 };
7532
7533 static int __init nf_tables_module_init(void)
7534 {
7535         int err;
7536
7537         spin_lock_init(&nf_tables_destroy_list_lock);
7538         err = register_pernet_subsys(&nf_tables_net_ops);
7539         if (err < 0)
7540                 return err;
7541
7542         err = nft_chain_filter_init();
7543         if (err < 0)
7544                 goto err1;
7545
7546         err = nf_tables_core_module_init();
7547         if (err < 0)
7548                 goto err2;
7549
7550         err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
7551         if (err < 0)
7552                 goto err3;
7553
7554         err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
7555         if (err < 0)
7556                 goto err4;
7557
7558         /* must be last */
7559         err = nfnetlink_subsys_register(&nf_tables_subsys);
7560         if (err < 0)
7561                 goto err5;
7562
7563         nft_chain_route_init();
7564         return err;
7565 err5:
7566         rhltable_destroy(&nft_objname_ht);
7567 err4:
7568         unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
7569 err3:
7570         nf_tables_core_module_exit();
7571 err2:
7572         nft_chain_filter_fini();
7573 err1:
7574         unregister_pernet_subsys(&nf_tables_net_ops);
7575         return err;
7576 }
7577
7578 static void __exit nf_tables_module_exit(void)
7579 {
7580         nfnetlink_subsys_unregister(&nf_tables_subsys);
7581         unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
7582         nft_chain_filter_fini();
7583         nft_chain_route_fini();
7584         unregister_pernet_subsys(&nf_tables_net_ops);
7585         cancel_work_sync(&trans_destroy_work);
7586         rcu_barrier();
7587         rhltable_destroy(&nft_objname_ht);
7588         nf_tables_core_module_exit();
7589 }
7590
7591 module_init(nf_tables_module_init);
7592 module_exit(nf_tables_module_exit);
7593
7594 MODULE_LICENSE("GPL");
7595 MODULE_AUTHOR("Patrick McHardy <[email protected]>");
7596 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
This page took 0.469794 seconds and 4 git commands to generate.