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