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