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