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