]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/nf_tables_api.c
netfilter: nf_tables: safe RCU iteration on list when dumping
[karo-tx-linux.git] / net / netfilter / nf_tables_api.c
1 /*
2  * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Development of this code funded by Astaro AG (http://www.astaro.com/)
9  */
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
22 #include <net/sock.h>
23
24 static LIST_HEAD(nf_tables_expressions);
25
26 /**
27  *      nft_register_afinfo - register nf_tables address family info
28  *
29  *      @afi: address family info to register
30  *
31  *      Register the address family for use with nf_tables. Returns zero on
32  *      success or a negative errno code otherwise.
33  */
34 int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
35 {
36         INIT_LIST_HEAD(&afi->tables);
37         nfnl_lock(NFNL_SUBSYS_NFTABLES);
38         list_add_tail_rcu(&afi->list, &net->nft.af_info);
39         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40         return 0;
41 }
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44 /**
45  *      nft_unregister_afinfo - unregister nf_tables address family info
46  *
47  *      @afi: address family info to unregister
48  *
49  *      Unregister the address family for use with nf_tables.
50  */
51 void nft_unregister_afinfo(struct nft_af_info *afi)
52 {
53         nfnl_lock(NFNL_SUBSYS_NFTABLES);
54         list_del_rcu(&afi->list);
55         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56 }
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
59 static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
60 {
61         struct nft_af_info *afi;
62
63         list_for_each_entry(afi, &net->nft.af_info, list) {
64                 if (afi->family == family)
65                         return afi;
66         }
67         return NULL;
68 }
69
70 static struct nft_af_info *
71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
72 {
73         struct nft_af_info *afi;
74
75         afi = nft_afinfo_lookup(net, family);
76         if (afi != NULL)
77                 return afi;
78 #ifdef CONFIG_MODULES
79         if (autoload) {
80                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81                 request_module("nft-afinfo-%u", family);
82                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
83                 afi = nft_afinfo_lookup(net, family);
84                 if (afi != NULL)
85                         return ERR_PTR(-EAGAIN);
86         }
87 #endif
88         return ERR_PTR(-EAFNOSUPPORT);
89 }
90
91 static void nft_ctx_init(struct nft_ctx *ctx,
92                          const struct sk_buff *skb,
93                          const struct nlmsghdr *nlh,
94                          struct nft_af_info *afi,
95                          struct nft_table *table,
96                          struct nft_chain *chain,
97                          const struct nlattr * const *nla)
98 {
99         ctx->net        = sock_net(skb->sk);
100         ctx->afi        = afi;
101         ctx->table      = table;
102         ctx->chain      = chain;
103         ctx->nla        = nla;
104         ctx->portid     = NETLINK_CB(skb).portid;
105         ctx->report     = nlmsg_report(nlh);
106         ctx->seq        = nlh->nlmsg_seq;
107 }
108
109 static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
110                                          u32 size)
111 {
112         struct nft_trans *trans;
113
114         trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
115         if (trans == NULL)
116                 return NULL;
117
118         trans->msg_type = msg_type;
119         trans->ctx      = *ctx;
120
121         return trans;
122 }
123
124 static void nft_trans_destroy(struct nft_trans *trans)
125 {
126         list_del(&trans->list);
127         kfree(trans);
128 }
129
130 /*
131  * Tables
132  */
133
134 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
135                                           const struct nlattr *nla)
136 {
137         struct nft_table *table;
138
139         list_for_each_entry(table, &afi->tables, list) {
140                 if (!nla_strcmp(nla, table->name))
141                         return table;
142         }
143         return NULL;
144 }
145
146 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
147                                                 const struct nlattr *nla)
148 {
149         struct nft_table *table;
150
151         if (nla == NULL)
152                 return ERR_PTR(-EINVAL);
153
154         table = nft_table_lookup(afi, nla);
155         if (table != NULL)
156                 return table;
157
158         return ERR_PTR(-ENOENT);
159 }
160
161 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
162 {
163         return ++table->hgenerator;
164 }
165
166 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
167
168 static const struct nf_chain_type *
169 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
170 {
171         int i;
172
173         for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
174                 if (chain_type[family][i] != NULL &&
175                     !nla_strcmp(nla, chain_type[family][i]->name))
176                         return chain_type[family][i];
177         }
178         return NULL;
179 }
180
181 static const struct nf_chain_type *
182 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
183                             const struct nlattr *nla,
184                             bool autoload)
185 {
186         const struct nf_chain_type *type;
187
188         type = __nf_tables_chain_type_lookup(afi->family, nla);
189         if (type != NULL)
190                 return type;
191 #ifdef CONFIG_MODULES
192         if (autoload) {
193                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
194                 request_module("nft-chain-%u-%.*s", afi->family,
195                                nla_len(nla), (const char *)nla_data(nla));
196                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
197                 type = __nf_tables_chain_type_lookup(afi->family, nla);
198                 if (type != NULL)
199                         return ERR_PTR(-EAGAIN);
200         }
201 #endif
202         return ERR_PTR(-ENOENT);
203 }
204
205 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
206         [NFTA_TABLE_NAME]       = { .type = NLA_STRING },
207         [NFTA_TABLE_FLAGS]      = { .type = NLA_U32 },
208 };
209
210 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
211                                      int event, u32 flags, int family,
212                                      const struct nft_table *table)
213 {
214         struct nlmsghdr *nlh;
215         struct nfgenmsg *nfmsg;
216
217         event |= NFNL_SUBSYS_NFTABLES << 8;
218         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
219         if (nlh == NULL)
220                 goto nla_put_failure;
221
222         nfmsg = nlmsg_data(nlh);
223         nfmsg->nfgen_family     = family;
224         nfmsg->version          = NFNETLINK_V0;
225         nfmsg->res_id           = 0;
226
227         if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
228             nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
229             nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
230                 goto nla_put_failure;
231
232         return nlmsg_end(skb, nlh);
233
234 nla_put_failure:
235         nlmsg_trim(skb, nlh);
236         return -1;
237 }
238
239 static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
240 {
241         struct sk_buff *skb;
242         int err;
243
244         if (!ctx->report &&
245             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
246                 return 0;
247
248         err = -ENOBUFS;
249         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
250         if (skb == NULL)
251                 goto err;
252
253         err = nf_tables_fill_table_info(skb, ctx->portid, ctx->seq, event, 0,
254                                         ctx->afi->family, ctx->table);
255         if (err < 0) {
256                 kfree_skb(skb);
257                 goto err;
258         }
259
260         err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
261                              ctx->report, GFP_KERNEL);
262 err:
263         if (err < 0) {
264                 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
265                                   err);
266         }
267         return err;
268 }
269
270 static int nf_tables_dump_tables(struct sk_buff *skb,
271                                  struct netlink_callback *cb)
272 {
273         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
274         const struct nft_af_info *afi;
275         const struct nft_table *table;
276         unsigned int idx = 0, s_idx = cb->args[0];
277         struct net *net = sock_net(skb->sk);
278         int family = nfmsg->nfgen_family;
279
280         rcu_read_lock();
281         list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
282                 if (family != NFPROTO_UNSPEC && family != afi->family)
283                         continue;
284
285                 list_for_each_entry_rcu(table, &afi->tables, list) {
286                         if (idx < s_idx)
287                                 goto cont;
288                         if (idx > s_idx)
289                                 memset(&cb->args[1], 0,
290                                        sizeof(cb->args) - sizeof(cb->args[0]));
291                         if (nf_tables_fill_table_info(skb,
292                                                       NETLINK_CB(cb->skb).portid,
293                                                       cb->nlh->nlmsg_seq,
294                                                       NFT_MSG_NEWTABLE,
295                                                       NLM_F_MULTI,
296                                                       afi->family, table) < 0)
297                                 goto done;
298 cont:
299                         idx++;
300                 }
301         }
302 done:
303         rcu_read_unlock();
304         cb->args[0] = idx;
305         return skb->len;
306 }
307
308 /* Internal table flags */
309 #define NFT_TABLE_INACTIVE      (1 << 15)
310
311 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
312                               const struct nlmsghdr *nlh,
313                               const struct nlattr * const nla[])
314 {
315         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
316         const struct nft_af_info *afi;
317         const struct nft_table *table;
318         struct sk_buff *skb2;
319         struct net *net = sock_net(skb->sk);
320         int family = nfmsg->nfgen_family;
321         int err;
322
323         if (nlh->nlmsg_flags & NLM_F_DUMP) {
324                 struct netlink_dump_control c = {
325                         .dump = nf_tables_dump_tables,
326                 };
327                 return netlink_dump_start(nlsk, skb, nlh, &c);
328         }
329
330         afi = nf_tables_afinfo_lookup(net, family, false);
331         if (IS_ERR(afi))
332                 return PTR_ERR(afi);
333
334         table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
335         if (IS_ERR(table))
336                 return PTR_ERR(table);
337         if (table->flags & NFT_TABLE_INACTIVE)
338                 return -ENOENT;
339
340         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
341         if (!skb2)
342                 return -ENOMEM;
343
344         err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
345                                         nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
346                                         family, table);
347         if (err < 0)
348                 goto err;
349
350         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
351
352 err:
353         kfree_skb(skb2);
354         return err;
355 }
356
357 static int nf_tables_table_enable(const struct nft_af_info *afi,
358                                   struct nft_table *table)
359 {
360         struct nft_chain *chain;
361         int err, i = 0;
362
363         list_for_each_entry(chain, &table->chains, list) {
364                 if (!(chain->flags & NFT_BASE_CHAIN))
365                         continue;
366
367                 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
368                 if (err < 0)
369                         goto err;
370
371                 i++;
372         }
373         return 0;
374 err:
375         list_for_each_entry(chain, &table->chains, list) {
376                 if (!(chain->flags & NFT_BASE_CHAIN))
377                         continue;
378
379                 if (i-- <= 0)
380                         break;
381
382                 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
383         }
384         return err;
385 }
386
387 static void nf_tables_table_disable(const struct nft_af_info *afi,
388                                    struct nft_table *table)
389 {
390         struct nft_chain *chain;
391
392         list_for_each_entry(chain, &table->chains, list) {
393                 if (chain->flags & NFT_BASE_CHAIN)
394                         nf_unregister_hooks(nft_base_chain(chain)->ops,
395                                             afi->nops);
396         }
397 }
398
399 static int nf_tables_updtable(struct nft_ctx *ctx)
400 {
401         struct nft_trans *trans;
402         u32 flags;
403         int ret = 0;
404
405         if (!ctx->nla[NFTA_TABLE_FLAGS])
406                 return 0;
407
408         flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
409         if (flags & ~NFT_TABLE_F_DORMANT)
410                 return -EINVAL;
411
412         if (flags == ctx->table->flags)
413                 return 0;
414
415         trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
416                                 sizeof(struct nft_trans_table));
417         if (trans == NULL)
418                 return -ENOMEM;
419
420         if ((flags & NFT_TABLE_F_DORMANT) &&
421             !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
422                 nft_trans_table_enable(trans) = false;
423         } else if (!(flags & NFT_TABLE_F_DORMANT) &&
424                    ctx->table->flags & NFT_TABLE_F_DORMANT) {
425                 ret = nf_tables_table_enable(ctx->afi, ctx->table);
426                 if (ret >= 0) {
427                         ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
428                         nft_trans_table_enable(trans) = true;
429                 }
430         }
431         if (ret < 0)
432                 goto err;
433
434         nft_trans_table_update(trans) = true;
435         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
436         return 0;
437 err:
438         nft_trans_destroy(trans);
439         return ret;
440 }
441
442 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
443 {
444         struct nft_trans *trans;
445
446         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
447         if (trans == NULL)
448                 return -ENOMEM;
449
450         if (msg_type == NFT_MSG_NEWTABLE)
451                 ctx->table->flags |= NFT_TABLE_INACTIVE;
452
453         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
454         return 0;
455 }
456
457 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
458                               const struct nlmsghdr *nlh,
459                               const struct nlattr * const nla[])
460 {
461         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
462         const struct nlattr *name;
463         struct nft_af_info *afi;
464         struct nft_table *table;
465         struct net *net = sock_net(skb->sk);
466         int family = nfmsg->nfgen_family;
467         u32 flags = 0;
468         struct nft_ctx ctx;
469         int err;
470
471         afi = nf_tables_afinfo_lookup(net, family, true);
472         if (IS_ERR(afi))
473                 return PTR_ERR(afi);
474
475         name = nla[NFTA_TABLE_NAME];
476         table = nf_tables_table_lookup(afi, name);
477         if (IS_ERR(table)) {
478                 if (PTR_ERR(table) != -ENOENT)
479                         return PTR_ERR(table);
480                 table = NULL;
481         }
482
483         if (table != NULL) {
484                 if (table->flags & NFT_TABLE_INACTIVE)
485                         return -ENOENT;
486                 if (nlh->nlmsg_flags & NLM_F_EXCL)
487                         return -EEXIST;
488                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
489                         return -EOPNOTSUPP;
490
491                 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
492                 return nf_tables_updtable(&ctx);
493         }
494
495         if (nla[NFTA_TABLE_FLAGS]) {
496                 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
497                 if (flags & ~NFT_TABLE_F_DORMANT)
498                         return -EINVAL;
499         }
500
501         if (!try_module_get(afi->owner))
502                 return -EAFNOSUPPORT;
503
504         table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
505         if (table == NULL) {
506                 module_put(afi->owner);
507                 return -ENOMEM;
508         }
509
510         nla_strlcpy(table->name, name, nla_len(name));
511         INIT_LIST_HEAD(&table->chains);
512         INIT_LIST_HEAD(&table->sets);
513         table->flags = flags;
514
515         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
516         err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
517         if (err < 0) {
518                 kfree(table);
519                 module_put(afi->owner);
520                 return err;
521         }
522         list_add_tail_rcu(&table->list, &afi->tables);
523         return 0;
524 }
525
526 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
527                               const struct nlmsghdr *nlh,
528                               const struct nlattr * const nla[])
529 {
530         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
531         struct nft_af_info *afi;
532         struct nft_table *table;
533         struct net *net = sock_net(skb->sk);
534         int family = nfmsg->nfgen_family, err;
535         struct nft_ctx ctx;
536
537         afi = nf_tables_afinfo_lookup(net, family, false);
538         if (IS_ERR(afi))
539                 return PTR_ERR(afi);
540
541         table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
542         if (IS_ERR(table))
543                 return PTR_ERR(table);
544         if (table->flags & NFT_TABLE_INACTIVE)
545                 return -ENOENT;
546         if (table->use > 0)
547                 return -EBUSY;
548
549         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
550         err = nft_trans_table_add(&ctx, NFT_MSG_DELTABLE);
551         if (err < 0)
552                 return err;
553
554         list_del_rcu(&table->list);
555         return 0;
556 }
557
558 static void nf_tables_table_destroy(struct nft_ctx *ctx)
559 {
560         BUG_ON(ctx->table->use > 0);
561
562         kfree(ctx->table);
563         module_put(ctx->afi->owner);
564 }
565
566 int nft_register_chain_type(const struct nf_chain_type *ctype)
567 {
568         int err = 0;
569
570         nfnl_lock(NFNL_SUBSYS_NFTABLES);
571         if (chain_type[ctype->family][ctype->type] != NULL) {
572                 err = -EBUSY;
573                 goto out;
574         }
575         chain_type[ctype->family][ctype->type] = ctype;
576 out:
577         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
578         return err;
579 }
580 EXPORT_SYMBOL_GPL(nft_register_chain_type);
581
582 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
583 {
584         nfnl_lock(NFNL_SUBSYS_NFTABLES);
585         chain_type[ctype->family][ctype->type] = NULL;
586         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
587 }
588 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
589
590 /*
591  * Chains
592  */
593
594 static struct nft_chain *
595 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
596 {
597         struct nft_chain *chain;
598
599         list_for_each_entry(chain, &table->chains, list) {
600                 if (chain->handle == handle)
601                         return chain;
602         }
603
604         return ERR_PTR(-ENOENT);
605 }
606
607 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
608                                                 const struct nlattr *nla)
609 {
610         struct nft_chain *chain;
611
612         if (nla == NULL)
613                 return ERR_PTR(-EINVAL);
614
615         list_for_each_entry(chain, &table->chains, list) {
616                 if (!nla_strcmp(nla, chain->name))
617                         return chain;
618         }
619
620         return ERR_PTR(-ENOENT);
621 }
622
623 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
624         [NFTA_CHAIN_TABLE]      = { .type = NLA_STRING },
625         [NFTA_CHAIN_HANDLE]     = { .type = NLA_U64 },
626         [NFTA_CHAIN_NAME]       = { .type = NLA_STRING,
627                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
628         [NFTA_CHAIN_HOOK]       = { .type = NLA_NESTED },
629         [NFTA_CHAIN_POLICY]     = { .type = NLA_U32 },
630         [NFTA_CHAIN_TYPE]       = { .type = NLA_STRING },
631         [NFTA_CHAIN_COUNTERS]   = { .type = NLA_NESTED },
632 };
633
634 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
635         [NFTA_HOOK_HOOKNUM]     = { .type = NLA_U32 },
636         [NFTA_HOOK_PRIORITY]    = { .type = NLA_U32 },
637 };
638
639 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
640 {
641         struct nft_stats *cpu_stats, total;
642         struct nlattr *nest;
643         int cpu;
644
645         memset(&total, 0, sizeof(total));
646         for_each_possible_cpu(cpu) {
647                 cpu_stats = per_cpu_ptr(stats, cpu);
648                 total.pkts += cpu_stats->pkts;
649                 total.bytes += cpu_stats->bytes;
650         }
651         nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
652         if (nest == NULL)
653                 goto nla_put_failure;
654
655         if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
656             nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
657                 goto nla_put_failure;
658
659         nla_nest_end(skb, nest);
660         return 0;
661
662 nla_put_failure:
663         return -ENOSPC;
664 }
665
666 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
667                                      int event, u32 flags, int family,
668                                      const struct nft_table *table,
669                                      const struct nft_chain *chain)
670 {
671         struct nlmsghdr *nlh;
672         struct nfgenmsg *nfmsg;
673
674         event |= NFNL_SUBSYS_NFTABLES << 8;
675         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
676         if (nlh == NULL)
677                 goto nla_put_failure;
678
679         nfmsg = nlmsg_data(nlh);
680         nfmsg->nfgen_family     = family;
681         nfmsg->version          = NFNETLINK_V0;
682         nfmsg->res_id           = 0;
683
684         if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
685                 goto nla_put_failure;
686         if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
687                 goto nla_put_failure;
688         if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
689                 goto nla_put_failure;
690
691         if (chain->flags & NFT_BASE_CHAIN) {
692                 const struct nft_base_chain *basechain = nft_base_chain(chain);
693                 const struct nf_hook_ops *ops = &basechain->ops[0];
694                 struct nlattr *nest;
695
696                 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
697                 if (nest == NULL)
698                         goto nla_put_failure;
699                 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
700                         goto nla_put_failure;
701                 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
702                         goto nla_put_failure;
703                 nla_nest_end(skb, nest);
704
705                 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
706                                  htonl(basechain->policy)))
707                         goto nla_put_failure;
708
709                 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
710                         goto nla_put_failure;
711
712                 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
713                         goto nla_put_failure;
714         }
715
716         if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
717                 goto nla_put_failure;
718
719         return nlmsg_end(skb, nlh);
720
721 nla_put_failure:
722         nlmsg_trim(skb, nlh);
723         return -1;
724 }
725
726 static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
727 {
728         struct sk_buff *skb;
729         int err;
730
731         if (!ctx->report &&
732             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
733                 return 0;
734
735         err = -ENOBUFS;
736         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
737         if (skb == NULL)
738                 goto err;
739
740         err = nf_tables_fill_chain_info(skb, ctx->portid, ctx->seq, event, 0,
741                                         ctx->afi->family, ctx->table,
742                                         ctx->chain);
743         if (err < 0) {
744                 kfree_skb(skb);
745                 goto err;
746         }
747
748         err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
749                              ctx->report, GFP_KERNEL);
750 err:
751         if (err < 0) {
752                 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
753                                   err);
754         }
755         return err;
756 }
757
758 static int nf_tables_dump_chains(struct sk_buff *skb,
759                                  struct netlink_callback *cb)
760 {
761         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
762         const struct nft_af_info *afi;
763         const struct nft_table *table;
764         const struct nft_chain *chain;
765         unsigned int idx = 0, s_idx = cb->args[0];
766         struct net *net = sock_net(skb->sk);
767         int family = nfmsg->nfgen_family;
768
769         rcu_read_lock();
770         list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
771                 if (family != NFPROTO_UNSPEC && family != afi->family)
772                         continue;
773
774                 list_for_each_entry_rcu(table, &afi->tables, list) {
775                         list_for_each_entry_rcu(chain, &table->chains, list) {
776                                 if (idx < s_idx)
777                                         goto cont;
778                                 if (idx > s_idx)
779                                         memset(&cb->args[1], 0,
780                                                sizeof(cb->args) - sizeof(cb->args[0]));
781                                 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
782                                                               cb->nlh->nlmsg_seq,
783                                                               NFT_MSG_NEWCHAIN,
784                                                               NLM_F_MULTI,
785                                                               afi->family, table, chain) < 0)
786                                         goto done;
787 cont:
788                                 idx++;
789                         }
790                 }
791         }
792 done:
793         rcu_read_unlock();
794         cb->args[0] = idx;
795         return skb->len;
796 }
797
798 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
799                               const struct nlmsghdr *nlh,
800                               const struct nlattr * const nla[])
801 {
802         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
803         const struct nft_af_info *afi;
804         const struct nft_table *table;
805         const struct nft_chain *chain;
806         struct sk_buff *skb2;
807         struct net *net = sock_net(skb->sk);
808         int family = nfmsg->nfgen_family;
809         int err;
810
811         if (nlh->nlmsg_flags & NLM_F_DUMP) {
812                 struct netlink_dump_control c = {
813                         .dump = nf_tables_dump_chains,
814                 };
815                 return netlink_dump_start(nlsk, skb, nlh, &c);
816         }
817
818         afi = nf_tables_afinfo_lookup(net, family, false);
819         if (IS_ERR(afi))
820                 return PTR_ERR(afi);
821
822         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
823         if (IS_ERR(table))
824                 return PTR_ERR(table);
825         if (table->flags & NFT_TABLE_INACTIVE)
826                 return -ENOENT;
827
828         chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
829         if (IS_ERR(chain))
830                 return PTR_ERR(chain);
831         if (chain->flags & NFT_CHAIN_INACTIVE)
832                 return -ENOENT;
833
834         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
835         if (!skb2)
836                 return -ENOMEM;
837
838         err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
839                                         nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
840                                         family, table, chain);
841         if (err < 0)
842                 goto err;
843
844         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
845
846 err:
847         kfree_skb(skb2);
848         return err;
849 }
850
851 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
852         [NFTA_COUNTER_PACKETS]  = { .type = NLA_U64 },
853         [NFTA_COUNTER_BYTES]    = { .type = NLA_U64 },
854 };
855
856 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
857 {
858         struct nlattr *tb[NFTA_COUNTER_MAX+1];
859         struct nft_stats __percpu *newstats;
860         struct nft_stats *stats;
861         int err;
862
863         err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
864         if (err < 0)
865                 return ERR_PTR(err);
866
867         if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
868                 return ERR_PTR(-EINVAL);
869
870         newstats = alloc_percpu(struct nft_stats);
871         if (newstats == NULL)
872                 return ERR_PTR(-ENOMEM);
873
874         /* Restore old counters on this cpu, no problem. Per-cpu statistics
875          * are not exposed to userspace.
876          */
877         stats = this_cpu_ptr(newstats);
878         stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
879         stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
880
881         return newstats;
882 }
883
884 static void nft_chain_stats_replace(struct nft_base_chain *chain,
885                                     struct nft_stats __percpu *newstats)
886 {
887         if (chain->stats) {
888                 struct nft_stats __percpu *oldstats =
889                                 nft_dereference(chain->stats);
890
891                 rcu_assign_pointer(chain->stats, newstats);
892                 synchronize_rcu();
893                 free_percpu(oldstats);
894         } else
895                 rcu_assign_pointer(chain->stats, newstats);
896 }
897
898 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
899 {
900         struct nft_trans *trans;
901
902         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
903         if (trans == NULL)
904                 return -ENOMEM;
905
906         if (msg_type == NFT_MSG_NEWCHAIN)
907                 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
908
909         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
910         return 0;
911 }
912
913 static void nf_tables_chain_destroy(struct nft_chain *chain)
914 {
915         BUG_ON(chain->use > 0);
916
917         if (chain->flags & NFT_BASE_CHAIN) {
918                 module_put(nft_base_chain(chain)->type->owner);
919                 free_percpu(nft_base_chain(chain)->stats);
920                 kfree(nft_base_chain(chain));
921         } else {
922                 kfree(chain);
923         }
924 }
925
926 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
927                               const struct nlmsghdr *nlh,
928                               const struct nlattr * const nla[])
929 {
930         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
931         const struct nlattr * uninitialized_var(name);
932         struct nft_af_info *afi;
933         struct nft_table *table;
934         struct nft_chain *chain;
935         struct nft_base_chain *basechain = NULL;
936         struct nlattr *ha[NFTA_HOOK_MAX + 1];
937         struct net *net = sock_net(skb->sk);
938         int family = nfmsg->nfgen_family;
939         u8 policy = NF_ACCEPT;
940         u64 handle = 0;
941         unsigned int i;
942         struct nft_stats __percpu *stats;
943         int err;
944         bool create;
945         struct nft_ctx ctx;
946
947         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
948
949         afi = nf_tables_afinfo_lookup(net, family, true);
950         if (IS_ERR(afi))
951                 return PTR_ERR(afi);
952
953         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
954         if (IS_ERR(table))
955                 return PTR_ERR(table);
956
957         chain = NULL;
958         name = nla[NFTA_CHAIN_NAME];
959
960         if (nla[NFTA_CHAIN_HANDLE]) {
961                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
962                 chain = nf_tables_chain_lookup_byhandle(table, handle);
963                 if (IS_ERR(chain))
964                         return PTR_ERR(chain);
965         } else {
966                 chain = nf_tables_chain_lookup(table, name);
967                 if (IS_ERR(chain)) {
968                         if (PTR_ERR(chain) != -ENOENT)
969                                 return PTR_ERR(chain);
970                         chain = NULL;
971                 }
972         }
973
974         if (nla[NFTA_CHAIN_POLICY]) {
975                 if ((chain != NULL &&
976                     !(chain->flags & NFT_BASE_CHAIN)) ||
977                     nla[NFTA_CHAIN_HOOK] == NULL)
978                         return -EOPNOTSUPP;
979
980                 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
981                 switch (policy) {
982                 case NF_DROP:
983                 case NF_ACCEPT:
984                         break;
985                 default:
986                         return -EINVAL;
987                 }
988         }
989
990         if (chain != NULL) {
991                 struct nft_stats *stats = NULL;
992                 struct nft_trans *trans;
993
994                 if (chain->flags & NFT_CHAIN_INACTIVE)
995                         return -ENOENT;
996                 if (nlh->nlmsg_flags & NLM_F_EXCL)
997                         return -EEXIST;
998                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
999                         return -EOPNOTSUPP;
1000
1001                 if (nla[NFTA_CHAIN_HANDLE] && name &&
1002                     !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1003                         return -EEXIST;
1004
1005                 if (nla[NFTA_CHAIN_COUNTERS]) {
1006                         if (!(chain->flags & NFT_BASE_CHAIN))
1007                                 return -EOPNOTSUPP;
1008
1009                         stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1010                         if (IS_ERR(stats))
1011                                 return PTR_ERR(stats);
1012                 }
1013
1014                 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1015                 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1016                                         sizeof(struct nft_trans_chain));
1017                 if (trans == NULL)
1018                         return -ENOMEM;
1019
1020                 nft_trans_chain_stats(trans) = stats;
1021                 nft_trans_chain_update(trans) = true;
1022
1023                 if (nla[NFTA_CHAIN_POLICY])
1024                         nft_trans_chain_policy(trans) = policy;
1025                 else
1026                         nft_trans_chain_policy(trans) = -1;
1027
1028                 if (nla[NFTA_CHAIN_HANDLE] && name) {
1029                         nla_strlcpy(nft_trans_chain_name(trans), name,
1030                                     NFT_CHAIN_MAXNAMELEN);
1031                 }
1032                 list_add_tail(&trans->list, &net->nft.commit_list);
1033                 return 0;
1034         }
1035
1036         if (table->use == UINT_MAX)
1037                 return -EOVERFLOW;
1038
1039         if (nla[NFTA_CHAIN_HOOK]) {
1040                 const struct nf_chain_type *type;
1041                 struct nf_hook_ops *ops;
1042                 nf_hookfn *hookfn;
1043                 u32 hooknum, priority;
1044
1045                 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1046                 if (nla[NFTA_CHAIN_TYPE]) {
1047                         type = nf_tables_chain_type_lookup(afi,
1048                                                            nla[NFTA_CHAIN_TYPE],
1049                                                            create);
1050                         if (IS_ERR(type))
1051                                 return PTR_ERR(type);
1052                 }
1053
1054                 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1055                                        nft_hook_policy);
1056                 if (err < 0)
1057                         return err;
1058                 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1059                     ha[NFTA_HOOK_PRIORITY] == NULL)
1060                         return -EINVAL;
1061
1062                 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1063                 if (hooknum >= afi->nhooks)
1064                         return -EINVAL;
1065                 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1066
1067                 if (!(type->hook_mask & (1 << hooknum)))
1068                         return -EOPNOTSUPP;
1069                 if (!try_module_get(type->owner))
1070                         return -ENOENT;
1071                 hookfn = type->hooks[hooknum];
1072
1073                 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1074                 if (basechain == NULL)
1075                         return -ENOMEM;
1076
1077                 if (nla[NFTA_CHAIN_COUNTERS]) {
1078                         stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1079                         if (IS_ERR(stats)) {
1080                                 module_put(type->owner);
1081                                 kfree(basechain);
1082                                 return PTR_ERR(stats);
1083                         }
1084                         basechain->stats = stats;
1085                 } else {
1086                         stats = alloc_percpu(struct nft_stats);
1087                         if (IS_ERR(stats)) {
1088                                 module_put(type->owner);
1089                                 kfree(basechain);
1090                                 return PTR_ERR(stats);
1091                         }
1092                         rcu_assign_pointer(basechain->stats, stats);
1093                 }
1094
1095                 basechain->type = type;
1096                 chain = &basechain->chain;
1097
1098                 for (i = 0; i < afi->nops; i++) {
1099                         ops = &basechain->ops[i];
1100                         ops->pf         = family;
1101                         ops->owner      = afi->owner;
1102                         ops->hooknum    = hooknum;
1103                         ops->priority   = priority;
1104                         ops->priv       = chain;
1105                         ops->hook       = afi->hooks[ops->hooknum];
1106                         if (hookfn)
1107                                 ops->hook = hookfn;
1108                         if (afi->hook_ops_init)
1109                                 afi->hook_ops_init(ops, i);
1110                 }
1111
1112                 chain->flags |= NFT_BASE_CHAIN;
1113                 basechain->policy = policy;
1114         } else {
1115                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1116                 if (chain == NULL)
1117                         return -ENOMEM;
1118         }
1119
1120         INIT_LIST_HEAD(&chain->rules);
1121         chain->handle = nf_tables_alloc_handle(table);
1122         chain->net = net;
1123         chain->table = table;
1124         nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1125
1126         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1127             chain->flags & NFT_BASE_CHAIN) {
1128                 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1129                 if (err < 0)
1130                         goto err1;
1131         }
1132
1133         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1134         err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1135         if (err < 0)
1136                 goto err2;
1137
1138         table->use++;
1139         list_add_tail_rcu(&chain->list, &table->chains);
1140         return 0;
1141 err2:
1142         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1143             chain->flags & NFT_BASE_CHAIN) {
1144                 nf_unregister_hooks(nft_base_chain(chain)->ops,
1145                                     afi->nops);
1146         }
1147 err1:
1148         nf_tables_chain_destroy(chain);
1149         return err;
1150 }
1151
1152 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1153                               const struct nlmsghdr *nlh,
1154                               const struct nlattr * const nla[])
1155 {
1156         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1157         struct nft_af_info *afi;
1158         struct nft_table *table;
1159         struct nft_chain *chain;
1160         struct net *net = sock_net(skb->sk);
1161         int family = nfmsg->nfgen_family;
1162         struct nft_ctx ctx;
1163         int err;
1164
1165         afi = nf_tables_afinfo_lookup(net, family, false);
1166         if (IS_ERR(afi))
1167                 return PTR_ERR(afi);
1168
1169         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1170         if (IS_ERR(table))
1171                 return PTR_ERR(table);
1172         if (table->flags & NFT_TABLE_INACTIVE)
1173                 return -ENOENT;
1174
1175         chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1176         if (IS_ERR(chain))
1177                 return PTR_ERR(chain);
1178         if (chain->flags & NFT_CHAIN_INACTIVE)
1179                 return -ENOENT;
1180         if (chain->use > 0)
1181                 return -EBUSY;
1182
1183         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1184         err = nft_trans_chain_add(&ctx, NFT_MSG_DELCHAIN);
1185         if (err < 0)
1186                 return err;
1187
1188         table->use--;
1189         list_del_rcu(&chain->list);
1190         return 0;
1191 }
1192
1193 /*
1194  * Expressions
1195  */
1196
1197 /**
1198  *      nft_register_expr - register nf_tables expr type
1199  *      @ops: expr type
1200  *
1201  *      Registers the expr type for use with nf_tables. Returns zero on
1202  *      success or a negative errno code otherwise.
1203  */
1204 int nft_register_expr(struct nft_expr_type *type)
1205 {
1206         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1207         if (type->family == NFPROTO_UNSPEC)
1208                 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1209         else
1210                 list_add_rcu(&type->list, &nf_tables_expressions);
1211         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1212         return 0;
1213 }
1214 EXPORT_SYMBOL_GPL(nft_register_expr);
1215
1216 /**
1217  *      nft_unregister_expr - unregister nf_tables expr type
1218  *      @ops: expr type
1219  *
1220  *      Unregisters the expr typefor use with nf_tables.
1221  */
1222 void nft_unregister_expr(struct nft_expr_type *type)
1223 {
1224         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1225         list_del_rcu(&type->list);
1226         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1227 }
1228 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1229
1230 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1231                                                        struct nlattr *nla)
1232 {
1233         const struct nft_expr_type *type;
1234
1235         list_for_each_entry(type, &nf_tables_expressions, list) {
1236                 if (!nla_strcmp(nla, type->name) &&
1237                     (!type->family || type->family == family))
1238                         return type;
1239         }
1240         return NULL;
1241 }
1242
1243 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1244                                                      struct nlattr *nla)
1245 {
1246         const struct nft_expr_type *type;
1247
1248         if (nla == NULL)
1249                 return ERR_PTR(-EINVAL);
1250
1251         type = __nft_expr_type_get(family, nla);
1252         if (type != NULL && try_module_get(type->owner))
1253                 return type;
1254
1255 #ifdef CONFIG_MODULES
1256         if (type == NULL) {
1257                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1258                 request_module("nft-expr-%u-%.*s", family,
1259                                nla_len(nla), (char *)nla_data(nla));
1260                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1261                 if (__nft_expr_type_get(family, nla))
1262                         return ERR_PTR(-EAGAIN);
1263
1264                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1265                 request_module("nft-expr-%.*s",
1266                                nla_len(nla), (char *)nla_data(nla));
1267                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1268                 if (__nft_expr_type_get(family, nla))
1269                         return ERR_PTR(-EAGAIN);
1270         }
1271 #endif
1272         return ERR_PTR(-ENOENT);
1273 }
1274
1275 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1276         [NFTA_EXPR_NAME]        = { .type = NLA_STRING },
1277         [NFTA_EXPR_DATA]        = { .type = NLA_NESTED },
1278 };
1279
1280 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1281                                     const struct nft_expr *expr)
1282 {
1283         if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1284                 goto nla_put_failure;
1285
1286         if (expr->ops->dump) {
1287                 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1288                 if (data == NULL)
1289                         goto nla_put_failure;
1290                 if (expr->ops->dump(skb, expr) < 0)
1291                         goto nla_put_failure;
1292                 nla_nest_end(skb, data);
1293         }
1294
1295         return skb->len;
1296
1297 nla_put_failure:
1298         return -1;
1299 };
1300
1301 struct nft_expr_info {
1302         const struct nft_expr_ops       *ops;
1303         struct nlattr                   *tb[NFT_EXPR_MAXATTR + 1];
1304 };
1305
1306 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1307                                 const struct nlattr *nla,
1308                                 struct nft_expr_info *info)
1309 {
1310         const struct nft_expr_type *type;
1311         const struct nft_expr_ops *ops;
1312         struct nlattr *tb[NFTA_EXPR_MAX + 1];
1313         int err;
1314
1315         err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1316         if (err < 0)
1317                 return err;
1318
1319         type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1320         if (IS_ERR(type))
1321                 return PTR_ERR(type);
1322
1323         if (tb[NFTA_EXPR_DATA]) {
1324                 err = nla_parse_nested(info->tb, type->maxattr,
1325                                        tb[NFTA_EXPR_DATA], type->policy);
1326                 if (err < 0)
1327                         goto err1;
1328         } else
1329                 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1330
1331         if (type->select_ops != NULL) {
1332                 ops = type->select_ops(ctx,
1333                                        (const struct nlattr * const *)info->tb);
1334                 if (IS_ERR(ops)) {
1335                         err = PTR_ERR(ops);
1336                         goto err1;
1337                 }
1338         } else
1339                 ops = type->ops;
1340
1341         info->ops = ops;
1342         return 0;
1343
1344 err1:
1345         module_put(type->owner);
1346         return err;
1347 }
1348
1349 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1350                              const struct nft_expr_info *info,
1351                              struct nft_expr *expr)
1352 {
1353         const struct nft_expr_ops *ops = info->ops;
1354         int err;
1355
1356         expr->ops = ops;
1357         if (ops->init) {
1358                 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1359                 if (err < 0)
1360                         goto err1;
1361         }
1362
1363         return 0;
1364
1365 err1:
1366         expr->ops = NULL;
1367         return err;
1368 }
1369
1370 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1371                                    struct nft_expr *expr)
1372 {
1373         if (expr->ops->destroy)
1374                 expr->ops->destroy(ctx, expr);
1375         module_put(expr->ops->type->owner);
1376 }
1377
1378 /*
1379  * Rules
1380  */
1381
1382 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1383                                                 u64 handle)
1384 {
1385         struct nft_rule *rule;
1386
1387         // FIXME: this sucks
1388         list_for_each_entry(rule, &chain->rules, list) {
1389                 if (handle == rule->handle)
1390                         return rule;
1391         }
1392
1393         return ERR_PTR(-ENOENT);
1394 }
1395
1396 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1397                                               const struct nlattr *nla)
1398 {
1399         if (nla == NULL)
1400                 return ERR_PTR(-EINVAL);
1401
1402         return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1403 }
1404
1405 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1406         [NFTA_RULE_TABLE]       = { .type = NLA_STRING },
1407         [NFTA_RULE_CHAIN]       = { .type = NLA_STRING,
1408                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
1409         [NFTA_RULE_HANDLE]      = { .type = NLA_U64 },
1410         [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1411         [NFTA_RULE_COMPAT]      = { .type = NLA_NESTED },
1412         [NFTA_RULE_POSITION]    = { .type = NLA_U64 },
1413         [NFTA_RULE_USERDATA]    = { .type = NLA_BINARY,
1414                                     .len = NFT_USERDATA_MAXLEN },
1415 };
1416
1417 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1418                                     int event, u32 flags, int family,
1419                                     const struct nft_table *table,
1420                                     const struct nft_chain *chain,
1421                                     const struct nft_rule *rule)
1422 {
1423         struct nlmsghdr *nlh;
1424         struct nfgenmsg *nfmsg;
1425         const struct nft_expr *expr, *next;
1426         struct nlattr *list;
1427         const struct nft_rule *prule;
1428         int type = event | NFNL_SUBSYS_NFTABLES << 8;
1429
1430         nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1431                         flags);
1432         if (nlh == NULL)
1433                 goto nla_put_failure;
1434
1435         nfmsg = nlmsg_data(nlh);
1436         nfmsg->nfgen_family     = family;
1437         nfmsg->version          = NFNETLINK_V0;
1438         nfmsg->res_id           = 0;
1439
1440         if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1441                 goto nla_put_failure;
1442         if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1443                 goto nla_put_failure;
1444         if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1445                 goto nla_put_failure;
1446
1447         if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1448                 prule = list_entry(rule->list.prev, struct nft_rule, list);
1449                 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1450                                  cpu_to_be64(prule->handle)))
1451                         goto nla_put_failure;
1452         }
1453
1454         list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1455         if (list == NULL)
1456                 goto nla_put_failure;
1457         nft_rule_for_each_expr(expr, next, rule) {
1458                 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1459                 if (elem == NULL)
1460                         goto nla_put_failure;
1461                 if (nf_tables_fill_expr_info(skb, expr) < 0)
1462                         goto nla_put_failure;
1463                 nla_nest_end(skb, elem);
1464         }
1465         nla_nest_end(skb, list);
1466
1467         if (rule->ulen &&
1468             nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1469                 goto nla_put_failure;
1470
1471         return nlmsg_end(skb, nlh);
1472
1473 nla_put_failure:
1474         nlmsg_trim(skb, nlh);
1475         return -1;
1476 }
1477
1478 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1479                                  const struct nft_rule *rule,
1480                                  int event)
1481 {
1482         struct sk_buff *skb;
1483         int err;
1484
1485         if (!ctx->report &&
1486             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1487                 return 0;
1488
1489         err = -ENOBUFS;
1490         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1491         if (skb == NULL)
1492                 goto err;
1493
1494         err = nf_tables_fill_rule_info(skb, ctx->portid, ctx->seq, event, 0,
1495                                        ctx->afi->family, ctx->table,
1496                                        ctx->chain, rule);
1497         if (err < 0) {
1498                 kfree_skb(skb);
1499                 goto err;
1500         }
1501
1502         err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1503                              ctx->report, GFP_KERNEL);
1504 err:
1505         if (err < 0) {
1506                 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1507                                   err);
1508         }
1509         return err;
1510 }
1511
1512 static inline bool
1513 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1514 {
1515         return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1516 }
1517
1518 static inline int gencursor_next(struct net *net)
1519 {
1520         return net->nft.gencursor+1 == 1 ? 1 : 0;
1521 }
1522
1523 static inline int
1524 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1525 {
1526         return (rule->genmask & (1 << gencursor_next(net))) == 0;
1527 }
1528
1529 static inline void
1530 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1531 {
1532         /* Now inactive, will be active in the future */
1533         rule->genmask = (1 << net->nft.gencursor);
1534 }
1535
1536 static inline void
1537 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1538 {
1539         rule->genmask = (1 << gencursor_next(net));
1540 }
1541
1542 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1543 {
1544         rule->genmask = 0;
1545 }
1546
1547 static int nf_tables_dump_rules(struct sk_buff *skb,
1548                                 struct netlink_callback *cb)
1549 {
1550         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1551         const struct nft_af_info *afi;
1552         const struct nft_table *table;
1553         const struct nft_chain *chain;
1554         const struct nft_rule *rule;
1555         unsigned int idx = 0, s_idx = cb->args[0];
1556         struct net *net = sock_net(skb->sk);
1557         int family = nfmsg->nfgen_family;
1558         u8 genctr = ACCESS_ONCE(net->nft.genctr);
1559         u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1560
1561         rcu_read_lock();
1562         list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1563                 if (family != NFPROTO_UNSPEC && family != afi->family)
1564                         continue;
1565
1566                 list_for_each_entry_rcu(table, &afi->tables, list) {
1567                         list_for_each_entry_rcu(chain, &table->chains, list) {
1568                                 list_for_each_entry_rcu(rule, &chain->rules, list) {
1569                                         if (!nft_rule_is_active(net, rule))
1570                                                 goto cont;
1571                                         if (idx < s_idx)
1572                                                 goto cont;
1573                                         if (idx > s_idx)
1574                                                 memset(&cb->args[1], 0,
1575                                                        sizeof(cb->args) - sizeof(cb->args[0]));
1576                                         if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1577                                                                       cb->nlh->nlmsg_seq,
1578                                                                       NFT_MSG_NEWRULE,
1579                                                                       NLM_F_MULTI | NLM_F_APPEND,
1580                                                                       afi->family, table, chain, rule) < 0)
1581                                                 goto done;
1582 cont:
1583                                         idx++;
1584                                 }
1585                         }
1586                 }
1587         }
1588 done:
1589         rcu_read_unlock();
1590
1591         /* Invalidate this dump, a transition to the new generation happened */
1592         if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1593                 return -EBUSY;
1594
1595         cb->args[0] = idx;
1596         return skb->len;
1597 }
1598
1599 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1600                              const struct nlmsghdr *nlh,
1601                              const struct nlattr * const nla[])
1602 {
1603         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1604         const struct nft_af_info *afi;
1605         const struct nft_table *table;
1606         const struct nft_chain *chain;
1607         const struct nft_rule *rule;
1608         struct sk_buff *skb2;
1609         struct net *net = sock_net(skb->sk);
1610         int family = nfmsg->nfgen_family;
1611         int err;
1612
1613         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1614                 struct netlink_dump_control c = {
1615                         .dump = nf_tables_dump_rules,
1616                 };
1617                 return netlink_dump_start(nlsk, skb, nlh, &c);
1618         }
1619
1620         afi = nf_tables_afinfo_lookup(net, family, false);
1621         if (IS_ERR(afi))
1622                 return PTR_ERR(afi);
1623
1624         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1625         if (IS_ERR(table))
1626                 return PTR_ERR(table);
1627         if (table->flags & NFT_TABLE_INACTIVE)
1628                 return -ENOENT;
1629
1630         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1631         if (IS_ERR(chain))
1632                 return PTR_ERR(chain);
1633         if (chain->flags & NFT_CHAIN_INACTIVE)
1634                 return -ENOENT;
1635
1636         rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1637         if (IS_ERR(rule))
1638                 return PTR_ERR(rule);
1639
1640         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1641         if (!skb2)
1642                 return -ENOMEM;
1643
1644         err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1645                                        nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1646                                        family, table, chain, rule);
1647         if (err < 0)
1648                 goto err;
1649
1650         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1651
1652 err:
1653         kfree_skb(skb2);
1654         return err;
1655 }
1656
1657 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1658                                    struct nft_rule *rule)
1659 {
1660         struct nft_expr *expr;
1661
1662         /*
1663          * Careful: some expressions might not be initialized in case this
1664          * is called on error from nf_tables_newrule().
1665          */
1666         expr = nft_expr_first(rule);
1667         while (expr->ops && expr != nft_expr_last(rule)) {
1668                 nf_tables_expr_destroy(ctx, expr);
1669                 expr = nft_expr_next(expr);
1670         }
1671         kfree(rule);
1672 }
1673
1674 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
1675                                             struct nft_rule *rule)
1676 {
1677         struct nft_trans *trans;
1678
1679         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
1680         if (trans == NULL)
1681                 return NULL;
1682
1683         nft_trans_rule(trans) = rule;
1684         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1685
1686         return trans;
1687 }
1688
1689 #define NFT_RULE_MAXEXPRS       128
1690
1691 static struct nft_expr_info *info;
1692
1693 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1694                              const struct nlmsghdr *nlh,
1695                              const struct nlattr * const nla[])
1696 {
1697         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1698         struct nft_af_info *afi;
1699         struct net *net = sock_net(skb->sk);
1700         struct nft_table *table;
1701         struct nft_chain *chain;
1702         struct nft_rule *rule, *old_rule = NULL;
1703         struct nft_trans *trans = NULL;
1704         struct nft_expr *expr;
1705         struct nft_ctx ctx;
1706         struct nlattr *tmp;
1707         unsigned int size, i, n, ulen = 0;
1708         int err, rem;
1709         bool create;
1710         u64 handle, pos_handle;
1711
1712         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1713
1714         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1715         if (IS_ERR(afi))
1716                 return PTR_ERR(afi);
1717
1718         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1719         if (IS_ERR(table))
1720                 return PTR_ERR(table);
1721
1722         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1723         if (IS_ERR(chain))
1724                 return PTR_ERR(chain);
1725
1726         if (nla[NFTA_RULE_HANDLE]) {
1727                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1728                 rule = __nf_tables_rule_lookup(chain, handle);
1729                 if (IS_ERR(rule))
1730                         return PTR_ERR(rule);
1731
1732                 if (nlh->nlmsg_flags & NLM_F_EXCL)
1733                         return -EEXIST;
1734                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1735                         old_rule = rule;
1736                 else
1737                         return -EOPNOTSUPP;
1738         } else {
1739                 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1740                         return -EINVAL;
1741                 handle = nf_tables_alloc_handle(table);
1742
1743                 if (chain->use == UINT_MAX)
1744                         return -EOVERFLOW;
1745         }
1746
1747         if (nla[NFTA_RULE_POSITION]) {
1748                 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1749                         return -EOPNOTSUPP;
1750
1751                 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1752                 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1753                 if (IS_ERR(old_rule))
1754                         return PTR_ERR(old_rule);
1755         }
1756
1757         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1758
1759         n = 0;
1760         size = 0;
1761         if (nla[NFTA_RULE_EXPRESSIONS]) {
1762                 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1763                         err = -EINVAL;
1764                         if (nla_type(tmp) != NFTA_LIST_ELEM)
1765                                 goto err1;
1766                         if (n == NFT_RULE_MAXEXPRS)
1767                                 goto err1;
1768                         err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1769                         if (err < 0)
1770                                 goto err1;
1771                         size += info[n].ops->size;
1772                         n++;
1773                 }
1774         }
1775
1776         if (nla[NFTA_RULE_USERDATA])
1777                 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1778
1779         err = -ENOMEM;
1780         rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1781         if (rule == NULL)
1782                 goto err1;
1783
1784         nft_rule_activate_next(net, rule);
1785
1786         rule->handle = handle;
1787         rule->dlen   = size;
1788         rule->ulen   = ulen;
1789
1790         if (ulen)
1791                 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1792
1793         expr = nft_expr_first(rule);
1794         for (i = 0; i < n; i++) {
1795                 err = nf_tables_newexpr(&ctx, &info[i], expr);
1796                 if (err < 0)
1797                         goto err2;
1798                 info[i].ops = NULL;
1799                 expr = nft_expr_next(expr);
1800         }
1801
1802         if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1803                 if (nft_rule_is_active_next(net, old_rule)) {
1804                         trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
1805                                                    old_rule);
1806                         if (trans == NULL) {
1807                                 err = -ENOMEM;
1808                                 goto err2;
1809                         }
1810                         nft_rule_disactivate_next(net, old_rule);
1811                         chain->use--;
1812                         list_add_tail_rcu(&rule->list, &old_rule->list);
1813                 } else {
1814                         err = -ENOENT;
1815                         goto err2;
1816                 }
1817         } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1818                 if (old_rule)
1819                         list_add_rcu(&rule->list, &old_rule->list);
1820                 else
1821                         list_add_tail_rcu(&rule->list, &chain->rules);
1822         else {
1823                 if (old_rule)
1824                         list_add_tail_rcu(&rule->list, &old_rule->list);
1825                 else
1826                         list_add_rcu(&rule->list, &chain->rules);
1827         }
1828
1829         if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
1830                 err = -ENOMEM;
1831                 goto err3;
1832         }
1833         chain->use++;
1834         return 0;
1835
1836 err3:
1837         list_del_rcu(&rule->list);
1838         if (trans) {
1839                 list_del_rcu(&nft_trans_rule(trans)->list);
1840                 nft_rule_clear(net, nft_trans_rule(trans));
1841                 nft_trans_destroy(trans);
1842                 chain->use++;
1843         }
1844 err2:
1845         nf_tables_rule_destroy(&ctx, rule);
1846 err1:
1847         for (i = 0; i < n; i++) {
1848                 if (info[i].ops != NULL)
1849                         module_put(info[i].ops->type->owner);
1850         }
1851         return err;
1852 }
1853
1854 static int
1855 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1856 {
1857         /* You cannot delete the same rule twice */
1858         if (nft_rule_is_active_next(ctx->net, rule)) {
1859                 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
1860                         return -ENOMEM;
1861                 nft_rule_disactivate_next(ctx->net, rule);
1862                 ctx->chain->use--;
1863                 return 0;
1864         }
1865         return -ENOENT;
1866 }
1867
1868 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1869 {
1870         struct nft_rule *rule;
1871         int err;
1872
1873         list_for_each_entry(rule, &ctx->chain->rules, list) {
1874                 err = nf_tables_delrule_one(ctx, rule);
1875                 if (err < 0)
1876                         return err;
1877         }
1878         return 0;
1879 }
1880
1881 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1882                              const struct nlmsghdr *nlh,
1883                              const struct nlattr * const nla[])
1884 {
1885         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1886         struct nft_af_info *afi;
1887         struct net *net = sock_net(skb->sk);
1888         struct nft_table *table;
1889         struct nft_chain *chain = NULL;
1890         struct nft_rule *rule;
1891         int family = nfmsg->nfgen_family, err = 0;
1892         struct nft_ctx ctx;
1893
1894         afi = nf_tables_afinfo_lookup(net, family, false);
1895         if (IS_ERR(afi))
1896                 return PTR_ERR(afi);
1897
1898         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1899         if (IS_ERR(table))
1900                 return PTR_ERR(table);
1901         if (table->flags & NFT_TABLE_INACTIVE)
1902                 return -ENOENT;
1903
1904         if (nla[NFTA_RULE_CHAIN]) {
1905                 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1906                 if (IS_ERR(chain))
1907                         return PTR_ERR(chain);
1908         }
1909
1910         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1911
1912         if (chain) {
1913                 if (nla[NFTA_RULE_HANDLE]) {
1914                         rule = nf_tables_rule_lookup(chain,
1915                                                      nla[NFTA_RULE_HANDLE]);
1916                         if (IS_ERR(rule))
1917                                 return PTR_ERR(rule);
1918
1919                         err = nf_tables_delrule_one(&ctx, rule);
1920                 } else {
1921                         err = nf_table_delrule_by_chain(&ctx);
1922                 }
1923         } else {
1924                 list_for_each_entry(chain, &table->chains, list) {
1925                         ctx.chain = chain;
1926                         err = nf_table_delrule_by_chain(&ctx);
1927                         if (err < 0)
1928                                 break;
1929                 }
1930         }
1931
1932         return err;
1933 }
1934
1935 /*
1936  * Sets
1937  */
1938
1939 static LIST_HEAD(nf_tables_set_ops);
1940
1941 int nft_register_set(struct nft_set_ops *ops)
1942 {
1943         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1944         list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
1945         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1946         return 0;
1947 }
1948 EXPORT_SYMBOL_GPL(nft_register_set);
1949
1950 void nft_unregister_set(struct nft_set_ops *ops)
1951 {
1952         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1953         list_del_rcu(&ops->list);
1954         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1955 }
1956 EXPORT_SYMBOL_GPL(nft_unregister_set);
1957
1958 /*
1959  * Select a set implementation based on the data characteristics and the
1960  * given policy. The total memory use might not be known if no size is
1961  * given, in that case the amount of memory per element is used.
1962  */
1963 static const struct nft_set_ops *
1964 nft_select_set_ops(const struct nlattr * const nla[],
1965                    const struct nft_set_desc *desc,
1966                    enum nft_set_policies policy)
1967 {
1968         const struct nft_set_ops *ops, *bops;
1969         struct nft_set_estimate est, best;
1970         u32 features;
1971
1972 #ifdef CONFIG_MODULES
1973         if (list_empty(&nf_tables_set_ops)) {
1974                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1975                 request_module("nft-set");
1976                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1977                 if (!list_empty(&nf_tables_set_ops))
1978                         return ERR_PTR(-EAGAIN);
1979         }
1980 #endif
1981         features = 0;
1982         if (nla[NFTA_SET_FLAGS] != NULL) {
1983                 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1984                 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1985         }
1986
1987         bops       = NULL;
1988         best.size  = ~0;
1989         best.class = ~0;
1990
1991         list_for_each_entry(ops, &nf_tables_set_ops, list) {
1992                 if ((ops->features & features) != features)
1993                         continue;
1994                 if (!ops->estimate(desc, features, &est))
1995                         continue;
1996
1997                 switch (policy) {
1998                 case NFT_SET_POL_PERFORMANCE:
1999                         if (est.class < best.class)
2000                                 break;
2001                         if (est.class == best.class && est.size < best.size)
2002                                 break;
2003                         continue;
2004                 case NFT_SET_POL_MEMORY:
2005                         if (est.size < best.size)
2006                                 break;
2007                         if (est.size == best.size && est.class < best.class)
2008                                 break;
2009                         continue;
2010                 default:
2011                         break;
2012                 }
2013
2014                 if (!try_module_get(ops->owner))
2015                         continue;
2016                 if (bops != NULL)
2017                         module_put(bops->owner);
2018
2019                 bops = ops;
2020                 best = est;
2021         }
2022
2023         if (bops != NULL)
2024                 return bops;
2025
2026         return ERR_PTR(-EOPNOTSUPP);
2027 }
2028
2029 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2030         [NFTA_SET_TABLE]                = { .type = NLA_STRING },
2031         [NFTA_SET_NAME]                 = { .type = NLA_STRING,
2032                                             .len = IFNAMSIZ - 1 },
2033         [NFTA_SET_FLAGS]                = { .type = NLA_U32 },
2034         [NFTA_SET_KEY_TYPE]             = { .type = NLA_U32 },
2035         [NFTA_SET_KEY_LEN]              = { .type = NLA_U32 },
2036         [NFTA_SET_DATA_TYPE]            = { .type = NLA_U32 },
2037         [NFTA_SET_DATA_LEN]             = { .type = NLA_U32 },
2038         [NFTA_SET_POLICY]               = { .type = NLA_U32 },
2039         [NFTA_SET_DESC]                 = { .type = NLA_NESTED },
2040         [NFTA_SET_ID]                   = { .type = NLA_U32 },
2041 };
2042
2043 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2044         [NFTA_SET_DESC_SIZE]            = { .type = NLA_U32 },
2045 };
2046
2047 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2048                                      const struct sk_buff *skb,
2049                                      const struct nlmsghdr *nlh,
2050                                      const struct nlattr * const nla[])
2051 {
2052         struct net *net = sock_net(skb->sk);
2053         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2054         struct nft_af_info *afi = NULL;
2055         struct nft_table *table = NULL;
2056
2057         if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2058                 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2059                 if (IS_ERR(afi))
2060                         return PTR_ERR(afi);
2061         }
2062
2063         if (nla[NFTA_SET_TABLE] != NULL) {
2064                 if (afi == NULL)
2065                         return -EAFNOSUPPORT;
2066
2067                 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2068                 if (IS_ERR(table))
2069                         return PTR_ERR(table);
2070                 if (table->flags & NFT_TABLE_INACTIVE)
2071                         return -ENOENT;
2072         }
2073
2074         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2075         return 0;
2076 }
2077
2078 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2079                                      const struct nlattr *nla)
2080 {
2081         struct nft_set *set;
2082
2083         if (nla == NULL)
2084                 return ERR_PTR(-EINVAL);
2085
2086         list_for_each_entry(set, &table->sets, list) {
2087                 if (!nla_strcmp(nla, set->name))
2088                         return set;
2089         }
2090         return ERR_PTR(-ENOENT);
2091 }
2092
2093 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2094                                           const struct nlattr *nla)
2095 {
2096         struct nft_trans *trans;
2097         u32 id = ntohl(nla_get_be32(nla));
2098
2099         list_for_each_entry(trans, &net->nft.commit_list, list) {
2100                 if (trans->msg_type == NFT_MSG_NEWSET &&
2101                     id == nft_trans_set_id(trans))
2102                         return nft_trans_set(trans);
2103         }
2104         return ERR_PTR(-ENOENT);
2105 }
2106
2107 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2108                                     const char *name)
2109 {
2110         const struct nft_set *i;
2111         const char *p;
2112         unsigned long *inuse;
2113         unsigned int n = 0, min = 0;
2114
2115         p = strnchr(name, IFNAMSIZ, '%');
2116         if (p != NULL) {
2117                 if (p[1] != 'd' || strchr(p + 2, '%'))
2118                         return -EINVAL;
2119
2120                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2121                 if (inuse == NULL)
2122                         return -ENOMEM;
2123 cont:
2124                 list_for_each_entry(i, &ctx->table->sets, list) {
2125                         int tmp;
2126
2127                         if (!sscanf(i->name, name, &tmp))
2128                                 continue;
2129                         if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2130                                 continue;
2131
2132                         set_bit(tmp - min, inuse);
2133                 }
2134
2135                 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2136                 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2137                         min += BITS_PER_BYTE * PAGE_SIZE;
2138                         memset(inuse, 0, PAGE_SIZE);
2139                         goto cont;
2140                 }
2141                 free_page((unsigned long)inuse);
2142         }
2143
2144         snprintf(set->name, sizeof(set->name), name, min + n);
2145         list_for_each_entry(i, &ctx->table->sets, list) {
2146                 if (!strcmp(set->name, i->name))
2147                         return -ENFILE;
2148         }
2149         return 0;
2150 }
2151
2152 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2153                               const struct nft_set *set, u16 event, u16 flags)
2154 {
2155         struct nfgenmsg *nfmsg;
2156         struct nlmsghdr *nlh;
2157         struct nlattr *desc;
2158         u32 portid = ctx->portid;
2159         u32 seq = ctx->seq;
2160
2161         event |= NFNL_SUBSYS_NFTABLES << 8;
2162         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2163                         flags);
2164         if (nlh == NULL)
2165                 goto nla_put_failure;
2166
2167         nfmsg = nlmsg_data(nlh);
2168         nfmsg->nfgen_family     = ctx->afi->family;
2169         nfmsg->version          = NFNETLINK_V0;
2170         nfmsg->res_id           = 0;
2171
2172         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2173                 goto nla_put_failure;
2174         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2175                 goto nla_put_failure;
2176         if (set->flags != 0)
2177                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2178                         goto nla_put_failure;
2179
2180         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2181                 goto nla_put_failure;
2182         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2183                 goto nla_put_failure;
2184         if (set->flags & NFT_SET_MAP) {
2185                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2186                         goto nla_put_failure;
2187                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2188                         goto nla_put_failure;
2189         }
2190
2191         desc = nla_nest_start(skb, NFTA_SET_DESC);
2192         if (desc == NULL)
2193                 goto nla_put_failure;
2194         if (set->size &&
2195             nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2196                 goto nla_put_failure;
2197         nla_nest_end(skb, desc);
2198
2199         return nlmsg_end(skb, nlh);
2200
2201 nla_put_failure:
2202         nlmsg_trim(skb, nlh);
2203         return -1;
2204 }
2205
2206 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2207                                 const struct nft_set *set,
2208                                 int event, gfp_t gfp_flags)
2209 {
2210         struct sk_buff *skb;
2211         u32 portid = ctx->portid;
2212         int err;
2213
2214         if (!ctx->report &&
2215             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2216                 return 0;
2217
2218         err = -ENOBUFS;
2219         skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2220         if (skb == NULL)
2221                 goto err;
2222
2223         err = nf_tables_fill_set(skb, ctx, set, event, 0);
2224         if (err < 0) {
2225                 kfree_skb(skb);
2226                 goto err;
2227         }
2228
2229         err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2230                              ctx->report, gfp_flags);
2231 err:
2232         if (err < 0)
2233                 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2234         return err;
2235 }
2236
2237 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2238                                      struct netlink_callback *cb)
2239 {
2240         const struct nft_set *set;
2241         unsigned int idx = 0, s_idx = cb->args[0];
2242
2243         if (cb->args[1])
2244                 return skb->len;
2245
2246         rcu_read_lock();
2247         list_for_each_entry_rcu(set, &ctx->table->sets, list) {
2248                 if (idx < s_idx)
2249                         goto cont;
2250                 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2251                                        NLM_F_MULTI) < 0) {
2252                         cb->args[0] = idx;
2253                         goto done;
2254                 }
2255 cont:
2256                 idx++;
2257         }
2258         cb->args[1] = 1;
2259 done:
2260         rcu_read_unlock();
2261         return skb->len;
2262 }
2263
2264 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2265                                       struct netlink_callback *cb)
2266 {
2267         const struct nft_set *set;
2268         unsigned int idx, s_idx = cb->args[0];
2269         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2270
2271         if (cb->args[1])
2272                 return skb->len;
2273
2274         rcu_read_lock();
2275         list_for_each_entry_rcu(table, &ctx->afi->tables, list) {
2276                 if (cur_table) {
2277                         if (cur_table != table)
2278                                 continue;
2279
2280                         cur_table = NULL;
2281                 }
2282                 ctx->table = table;
2283                 idx = 0;
2284                 list_for_each_entry_rcu(set, &ctx->table->sets, list) {
2285                         if (idx < s_idx)
2286                                 goto cont;
2287                         if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2288                                                NLM_F_MULTI) < 0) {
2289                                 cb->args[0] = idx;
2290                                 cb->args[2] = (unsigned long) table;
2291                                 goto done;
2292                         }
2293 cont:
2294                         idx++;
2295                 }
2296         }
2297         cb->args[1] = 1;
2298 done:
2299         rcu_read_unlock();
2300         return skb->len;
2301 }
2302
2303 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2304                                    struct netlink_callback *cb)
2305 {
2306         const struct nft_set *set;
2307         unsigned int idx, s_idx = cb->args[0];
2308         struct nft_af_info *afi;
2309         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2310         struct net *net = sock_net(skb->sk);
2311         int cur_family = cb->args[3];
2312
2313         if (cb->args[1])
2314                 return skb->len;
2315
2316         rcu_read_lock();
2317         list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2318                 if (cur_family) {
2319                         if (afi->family != cur_family)
2320                                 continue;
2321
2322                         cur_family = 0;
2323                 }
2324
2325                 list_for_each_entry_rcu(table, &afi->tables, list) {
2326                         if (cur_table) {
2327                                 if (cur_table != table)
2328                                         continue;
2329
2330                                 cur_table = NULL;
2331                         }
2332
2333                         ctx->table = table;
2334                         ctx->afi = afi;
2335                         idx = 0;
2336                         list_for_each_entry_rcu(set, &ctx->table->sets, list) {
2337                                 if (idx < s_idx)
2338                                         goto cont;
2339                                 if (nf_tables_fill_set(skb, ctx, set,
2340                                                        NFT_MSG_NEWSET,
2341                                                        NLM_F_MULTI) < 0) {
2342                                         cb->args[0] = idx;
2343                                         cb->args[2] = (unsigned long) table;
2344                                         cb->args[3] = afi->family;
2345                                         goto done;
2346                                 }
2347 cont:
2348                                 idx++;
2349                         }
2350                         if (s_idx)
2351                                 s_idx = 0;
2352                 }
2353         }
2354         cb->args[1] = 1;
2355 done:
2356         rcu_read_unlock();
2357         return skb->len;
2358 }
2359
2360 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2361 {
2362         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2363         struct nlattr *nla[NFTA_SET_MAX + 1];
2364         struct nft_ctx ctx;
2365         int err, ret;
2366
2367         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2368                           nft_set_policy);
2369         if (err < 0)
2370                 return err;
2371
2372         err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2373         if (err < 0)
2374                 return err;
2375
2376         if (ctx.table == NULL) {
2377                 if (ctx.afi == NULL)
2378                         ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2379                 else
2380                         ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2381         } else
2382                 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2383
2384         return ret;
2385 }
2386
2387 #define NFT_SET_INACTIVE        (1 << 15)       /* Internal set flag */
2388
2389 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2390                             const struct nlmsghdr *nlh,
2391                             const struct nlattr * const nla[])
2392 {
2393         const struct nft_set *set;
2394         struct nft_ctx ctx;
2395         struct sk_buff *skb2;
2396         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2397         int err;
2398
2399         /* Verify existance before starting dump */
2400         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2401         if (err < 0)
2402                 return err;
2403
2404         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2405                 struct netlink_dump_control c = {
2406                         .dump = nf_tables_dump_sets,
2407                 };
2408                 return netlink_dump_start(nlsk, skb, nlh, &c);
2409         }
2410
2411         /* Only accept unspec with dump */
2412         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2413                 return -EAFNOSUPPORT;
2414
2415         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2416         if (IS_ERR(set))
2417                 return PTR_ERR(set);
2418         if (set->flags & NFT_SET_INACTIVE)
2419                 return -ENOENT;
2420
2421         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2422         if (skb2 == NULL)
2423                 return -ENOMEM;
2424
2425         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2426         if (err < 0)
2427                 goto err;
2428
2429         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2430
2431 err:
2432         kfree_skb(skb2);
2433         return err;
2434 }
2435
2436 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2437                                     struct nft_set_desc *desc,
2438                                     const struct nlattr *nla)
2439 {
2440         struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2441         int err;
2442
2443         err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2444         if (err < 0)
2445                 return err;
2446
2447         if (da[NFTA_SET_DESC_SIZE] != NULL)
2448                 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2449
2450         return 0;
2451 }
2452
2453 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2454                              struct nft_set *set)
2455 {
2456         struct nft_trans *trans;
2457
2458         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2459         if (trans == NULL)
2460                 return -ENOMEM;
2461
2462         if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2463                 nft_trans_set_id(trans) =
2464                         ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2465                 set->flags |= NFT_SET_INACTIVE;
2466         }
2467         nft_trans_set(trans) = set;
2468         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2469
2470         return 0;
2471 }
2472
2473 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2474                             const struct nlmsghdr *nlh,
2475                             const struct nlattr * const nla[])
2476 {
2477         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2478         const struct nft_set_ops *ops;
2479         struct nft_af_info *afi;
2480         struct net *net = sock_net(skb->sk);
2481         struct nft_table *table;
2482         struct nft_set *set;
2483         struct nft_ctx ctx;
2484         char name[IFNAMSIZ];
2485         unsigned int size;
2486         bool create;
2487         u32 ktype, dtype, flags, policy;
2488         struct nft_set_desc desc;
2489         int err;
2490
2491         if (nla[NFTA_SET_TABLE] == NULL ||
2492             nla[NFTA_SET_NAME] == NULL ||
2493             nla[NFTA_SET_KEY_LEN] == NULL ||
2494             nla[NFTA_SET_ID] == NULL)
2495                 return -EINVAL;
2496
2497         memset(&desc, 0, sizeof(desc));
2498
2499         ktype = NFT_DATA_VALUE;
2500         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2501                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2502                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2503                         return -EINVAL;
2504         }
2505
2506         desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2507         if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2508                 return -EINVAL;
2509
2510         flags = 0;
2511         if (nla[NFTA_SET_FLAGS] != NULL) {
2512                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2513                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2514                               NFT_SET_INTERVAL | NFT_SET_MAP))
2515                         return -EINVAL;
2516         }
2517
2518         dtype = 0;
2519         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2520                 if (!(flags & NFT_SET_MAP))
2521                         return -EINVAL;
2522
2523                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2524                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2525                     dtype != NFT_DATA_VERDICT)
2526                         return -EINVAL;
2527
2528                 if (dtype != NFT_DATA_VERDICT) {
2529                         if (nla[NFTA_SET_DATA_LEN] == NULL)
2530                                 return -EINVAL;
2531                         desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2532                         if (desc.dlen == 0 ||
2533                             desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2534                                 return -EINVAL;
2535                 } else
2536                         desc.dlen = sizeof(struct nft_data);
2537         } else if (flags & NFT_SET_MAP)
2538                 return -EINVAL;
2539
2540         policy = NFT_SET_POL_PERFORMANCE;
2541         if (nla[NFTA_SET_POLICY] != NULL)
2542                 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2543
2544         if (nla[NFTA_SET_DESC] != NULL) {
2545                 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2546                 if (err < 0)
2547                         return err;
2548         }
2549
2550         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2551
2552         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2553         if (IS_ERR(afi))
2554                 return PTR_ERR(afi);
2555
2556         table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2557         if (IS_ERR(table))
2558                 return PTR_ERR(table);
2559
2560         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2561
2562         set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2563         if (IS_ERR(set)) {
2564                 if (PTR_ERR(set) != -ENOENT)
2565                         return PTR_ERR(set);
2566                 set = NULL;
2567         }
2568
2569         if (set != NULL) {
2570                 if (nlh->nlmsg_flags & NLM_F_EXCL)
2571                         return -EEXIST;
2572                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2573                         return -EOPNOTSUPP;
2574                 return 0;
2575         }
2576
2577         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2578                 return -ENOENT;
2579
2580         ops = nft_select_set_ops(nla, &desc, policy);
2581         if (IS_ERR(ops))
2582                 return PTR_ERR(ops);
2583
2584         size = 0;
2585         if (ops->privsize != NULL)
2586                 size = ops->privsize(nla);
2587
2588         err = -ENOMEM;
2589         set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2590         if (set == NULL)
2591                 goto err1;
2592
2593         nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2594         err = nf_tables_set_alloc_name(&ctx, set, name);
2595         if (err < 0)
2596                 goto err2;
2597
2598         INIT_LIST_HEAD(&set->bindings);
2599         set->ops   = ops;
2600         set->ktype = ktype;
2601         set->klen  = desc.klen;
2602         set->dtype = dtype;
2603         set->dlen  = desc.dlen;
2604         set->flags = flags;
2605         set->size  = desc.size;
2606
2607         err = ops->init(set, &desc, nla);
2608         if (err < 0)
2609                 goto err2;
2610
2611         err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2612         if (err < 0)
2613                 goto err2;
2614
2615         list_add_tail_rcu(&set->list, &table->sets);
2616         table->use++;
2617         return 0;
2618
2619 err2:
2620         kfree(set);
2621 err1:
2622         module_put(ops->owner);
2623         return err;
2624 }
2625
2626 static void nft_set_destroy(struct nft_set *set)
2627 {
2628         set->ops->destroy(set);
2629         module_put(set->ops->owner);
2630         kfree(set);
2631 }
2632
2633 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2634 {
2635         list_del_rcu(&set->list);
2636         nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2637         nft_set_destroy(set);
2638 }
2639
2640 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2641                             const struct nlmsghdr *nlh,
2642                             const struct nlattr * const nla[])
2643 {
2644         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2645         struct nft_set *set;
2646         struct nft_ctx ctx;
2647         int err;
2648
2649         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2650                 return -EAFNOSUPPORT;
2651         if (nla[NFTA_SET_TABLE] == NULL)
2652                 return -EINVAL;
2653
2654         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2655         if (err < 0)
2656                 return err;
2657
2658         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2659         if (IS_ERR(set))
2660                 return PTR_ERR(set);
2661         if (set->flags & NFT_SET_INACTIVE)
2662                 return -ENOENT;
2663         if (!list_empty(&set->bindings))
2664                 return -EBUSY;
2665
2666         err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2667         if (err < 0)
2668                 return err;
2669
2670         list_del_rcu(&set->list);
2671         ctx.table->use--;
2672         return 0;
2673 }
2674
2675 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2676                                         const struct nft_set *set,
2677                                         const struct nft_set_iter *iter,
2678                                         const struct nft_set_elem *elem)
2679 {
2680         enum nft_registers dreg;
2681
2682         dreg = nft_type_to_reg(set->dtype);
2683         return nft_validate_data_load(ctx, dreg, &elem->data,
2684                                       set->dtype == NFT_DATA_VERDICT ?
2685                                       NFT_DATA_VERDICT : NFT_DATA_VALUE);
2686 }
2687
2688 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2689                        struct nft_set_binding *binding)
2690 {
2691         struct nft_set_binding *i;
2692         struct nft_set_iter iter;
2693
2694         if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2695                 return -EBUSY;
2696
2697         if (set->flags & NFT_SET_MAP) {
2698                 /* If the set is already bound to the same chain all
2699                  * jumps are already validated for that chain.
2700                  */
2701                 list_for_each_entry(i, &set->bindings, list) {
2702                         if (i->chain == binding->chain)
2703                                 goto bind;
2704                 }
2705
2706                 iter.skip       = 0;
2707                 iter.count      = 0;
2708                 iter.err        = 0;
2709                 iter.fn         = nf_tables_bind_check_setelem;
2710
2711                 set->ops->walk(ctx, set, &iter);
2712                 if (iter.err < 0) {
2713                         /* Destroy anonymous sets if binding fails */
2714                         if (set->flags & NFT_SET_ANONYMOUS)
2715                                 nf_tables_set_destroy(ctx, set);
2716
2717                         return iter.err;
2718                 }
2719         }
2720 bind:
2721         binding->chain = ctx->chain;
2722         list_add_tail_rcu(&binding->list, &set->bindings);
2723         return 0;
2724 }
2725
2726 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2727                           struct nft_set_binding *binding)
2728 {
2729         list_del_rcu(&binding->list);
2730
2731         if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2732             !(set->flags & NFT_SET_INACTIVE))
2733                 nf_tables_set_destroy(ctx, set);
2734 }
2735
2736 /*
2737  * Set elements
2738  */
2739
2740 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2741         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
2742         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
2743         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
2744 };
2745
2746 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2747         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING },
2748         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING },
2749         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
2750         [NFTA_SET_ELEM_LIST_SET_ID]     = { .type = NLA_U32 },
2751 };
2752
2753 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2754                                       const struct sk_buff *skb,
2755                                       const struct nlmsghdr *nlh,
2756                                       const struct nlattr * const nla[],
2757                                       bool trans)
2758 {
2759         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2760         struct nft_af_info *afi;
2761         struct nft_table *table;
2762         struct net *net = sock_net(skb->sk);
2763
2764         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2765         if (IS_ERR(afi))
2766                 return PTR_ERR(afi);
2767
2768         table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2769         if (IS_ERR(table))
2770                 return PTR_ERR(table);
2771         if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2772                 return -ENOENT;
2773
2774         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2775         return 0;
2776 }
2777
2778 static int nf_tables_fill_setelem(struct sk_buff *skb,
2779                                   const struct nft_set *set,
2780                                   const struct nft_set_elem *elem)
2781 {
2782         unsigned char *b = skb_tail_pointer(skb);
2783         struct nlattr *nest;
2784
2785         nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2786         if (nest == NULL)
2787                 goto nla_put_failure;
2788
2789         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2790                           set->klen) < 0)
2791                 goto nla_put_failure;
2792
2793         if (set->flags & NFT_SET_MAP &&
2794             !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2795             nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2796                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2797                           set->dlen) < 0)
2798                 goto nla_put_failure;
2799
2800         if (elem->flags != 0)
2801                 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2802                         goto nla_put_failure;
2803
2804         nla_nest_end(skb, nest);
2805         return 0;
2806
2807 nla_put_failure:
2808         nlmsg_trim(skb, b);
2809         return -EMSGSIZE;
2810 }
2811
2812 struct nft_set_dump_args {
2813         const struct netlink_callback   *cb;
2814         struct nft_set_iter             iter;
2815         struct sk_buff                  *skb;
2816 };
2817
2818 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2819                                   const struct nft_set *set,
2820                                   const struct nft_set_iter *iter,
2821                                   const struct nft_set_elem *elem)
2822 {
2823         struct nft_set_dump_args *args;
2824
2825         args = container_of(iter, struct nft_set_dump_args, iter);
2826         return nf_tables_fill_setelem(args->skb, set, elem);
2827 }
2828
2829 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2830 {
2831         const struct nft_set *set;
2832         struct nft_set_dump_args args;
2833         struct nft_ctx ctx;
2834         struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2835         struct nfgenmsg *nfmsg;
2836         struct nlmsghdr *nlh;
2837         struct nlattr *nest;
2838         u32 portid, seq;
2839         int event, err;
2840
2841         err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2842                           NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2843         if (err < 0)
2844                 return err;
2845
2846         err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2847                                          false);
2848         if (err < 0)
2849                 return err;
2850
2851         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2852         if (IS_ERR(set))
2853                 return PTR_ERR(set);
2854         if (set->flags & NFT_SET_INACTIVE)
2855                 return -ENOENT;
2856
2857         event  = NFT_MSG_NEWSETELEM;
2858         event |= NFNL_SUBSYS_NFTABLES << 8;
2859         portid = NETLINK_CB(cb->skb).portid;
2860         seq    = cb->nlh->nlmsg_seq;
2861
2862         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2863                         NLM_F_MULTI);
2864         if (nlh == NULL)
2865                 goto nla_put_failure;
2866
2867         nfmsg = nlmsg_data(nlh);
2868         nfmsg->nfgen_family = ctx.afi->family;
2869         nfmsg->version      = NFNETLINK_V0;
2870         nfmsg->res_id       = 0;
2871
2872         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2873                 goto nla_put_failure;
2874         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2875                 goto nla_put_failure;
2876
2877         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2878         if (nest == NULL)
2879                 goto nla_put_failure;
2880
2881         args.cb         = cb;
2882         args.skb        = skb;
2883         args.iter.skip  = cb->args[0];
2884         args.iter.count = 0;
2885         args.iter.err   = 0;
2886         args.iter.fn    = nf_tables_dump_setelem;
2887         set->ops->walk(&ctx, set, &args.iter);
2888
2889         nla_nest_end(skb, nest);
2890         nlmsg_end(skb, nlh);
2891
2892         if (args.iter.err && args.iter.err != -EMSGSIZE)
2893                 return args.iter.err;
2894         if (args.iter.count == cb->args[0])
2895                 return 0;
2896
2897         cb->args[0] = args.iter.count;
2898         return skb->len;
2899
2900 nla_put_failure:
2901         return -ENOSPC;
2902 }
2903
2904 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2905                                 const struct nlmsghdr *nlh,
2906                                 const struct nlattr * const nla[])
2907 {
2908         const struct nft_set *set;
2909         struct nft_ctx ctx;
2910         int err;
2911
2912         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2913         if (err < 0)
2914                 return err;
2915
2916         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2917         if (IS_ERR(set))
2918                 return PTR_ERR(set);
2919         if (set->flags & NFT_SET_INACTIVE)
2920                 return -ENOENT;
2921
2922         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2923                 struct netlink_dump_control c = {
2924                         .dump = nf_tables_dump_set,
2925                 };
2926                 return netlink_dump_start(nlsk, skb, nlh, &c);
2927         }
2928         return -EOPNOTSUPP;
2929 }
2930
2931 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2932                                        const struct nft_ctx *ctx, u32 seq,
2933                                        u32 portid, int event, u16 flags,
2934                                        const struct nft_set *set,
2935                                        const struct nft_set_elem *elem)
2936 {
2937         struct nfgenmsg *nfmsg;
2938         struct nlmsghdr *nlh;
2939         struct nlattr *nest;
2940         int err;
2941
2942         event |= NFNL_SUBSYS_NFTABLES << 8;
2943         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2944                         flags);
2945         if (nlh == NULL)
2946                 goto nla_put_failure;
2947
2948         nfmsg = nlmsg_data(nlh);
2949         nfmsg->nfgen_family     = ctx->afi->family;
2950         nfmsg->version          = NFNETLINK_V0;
2951         nfmsg->res_id           = 0;
2952
2953         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2954                 goto nla_put_failure;
2955         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2956                 goto nla_put_failure;
2957
2958         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2959         if (nest == NULL)
2960                 goto nla_put_failure;
2961
2962         err = nf_tables_fill_setelem(skb, set, elem);
2963         if (err < 0)
2964                 goto nla_put_failure;
2965
2966         nla_nest_end(skb, nest);
2967
2968         return nlmsg_end(skb, nlh);
2969
2970 nla_put_failure:
2971         nlmsg_trim(skb, nlh);
2972         return -1;
2973 }
2974
2975 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2976                                     const struct nft_set *set,
2977                                     const struct nft_set_elem *elem,
2978                                     int event, u16 flags)
2979 {
2980         struct net *net = ctx->net;
2981         u32 portid = ctx->portid;
2982         struct sk_buff *skb;
2983         int err;
2984
2985         if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2986                 return 0;
2987
2988         err = -ENOBUFS;
2989         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2990         if (skb == NULL)
2991                 goto err;
2992
2993         err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2994                                           set, elem);
2995         if (err < 0) {
2996                 kfree_skb(skb);
2997                 goto err;
2998         }
2999
3000         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3001                              GFP_KERNEL);
3002 err:
3003         if (err < 0)
3004                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3005         return err;
3006 }
3007
3008 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3009                                               int msg_type,
3010                                               struct nft_set *set)
3011 {
3012         struct nft_trans *trans;
3013
3014         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3015         if (trans == NULL)
3016                 return NULL;
3017
3018         nft_trans_elem_set(trans) = set;
3019         return trans;
3020 }
3021
3022 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3023                             const struct nlattr *attr)
3024 {
3025         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3026         struct nft_data_desc d1, d2;
3027         struct nft_set_elem elem;
3028         struct nft_set_binding *binding;
3029         enum nft_registers dreg;
3030         struct nft_trans *trans;
3031         int err;
3032
3033         if (set->size && set->nelems == set->size)
3034                 return -ENFILE;
3035
3036         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3037                                nft_set_elem_policy);
3038         if (err < 0)
3039                 return err;
3040
3041         if (nla[NFTA_SET_ELEM_KEY] == NULL)
3042                 return -EINVAL;
3043
3044         elem.flags = 0;
3045         if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3046                 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3047                 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3048                         return -EINVAL;
3049         }
3050
3051         if (set->flags & NFT_SET_MAP) {
3052                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3053                     !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3054                         return -EINVAL;
3055                 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3056                     elem.flags & NFT_SET_ELEM_INTERVAL_END)
3057                         return -EINVAL;
3058         } else {
3059                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3060                         return -EINVAL;
3061         }
3062
3063         err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3064         if (err < 0)
3065                 goto err1;
3066         err = -EINVAL;
3067         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3068                 goto err2;
3069
3070         err = -EEXIST;
3071         if (set->ops->get(set, &elem) == 0)
3072                 goto err2;
3073
3074         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3075                 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3076                 if (err < 0)
3077                         goto err2;
3078
3079                 err = -EINVAL;
3080                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3081                         goto err3;
3082
3083                 dreg = nft_type_to_reg(set->dtype);
3084                 list_for_each_entry(binding, &set->bindings, list) {
3085                         struct nft_ctx bind_ctx = {
3086                                 .afi    = ctx->afi,
3087                                 .table  = ctx->table,
3088                                 .chain  = (struct nft_chain *)binding->chain,
3089                         };
3090
3091                         err = nft_validate_data_load(&bind_ctx, dreg,
3092                                                      &elem.data, d2.type);
3093                         if (err < 0)
3094                                 goto err3;
3095                 }
3096         }
3097
3098         trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3099         if (trans == NULL)
3100                 goto err3;
3101
3102         err = set->ops->insert(set, &elem);
3103         if (err < 0)
3104                 goto err4;
3105
3106         nft_trans_elem(trans) = elem;
3107         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3108         return 0;
3109
3110 err4:
3111         kfree(trans);
3112 err3:
3113         if (nla[NFTA_SET_ELEM_DATA] != NULL)
3114                 nft_data_uninit(&elem.data, d2.type);
3115 err2:
3116         nft_data_uninit(&elem.key, d1.type);
3117 err1:
3118         return err;
3119 }
3120
3121 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3122                                 const struct nlmsghdr *nlh,
3123                                 const struct nlattr * const nla[])
3124 {
3125         struct net *net = sock_net(skb->sk);
3126         const struct nlattr *attr;
3127         struct nft_set *set;
3128         struct nft_ctx ctx;
3129         int rem, err = 0;
3130
3131         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3132         if (err < 0)
3133                 return err;
3134
3135         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3136         if (IS_ERR(set)) {
3137                 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3138                         set = nf_tables_set_lookup_byid(net,
3139                                         nla[NFTA_SET_ELEM_LIST_SET_ID]);
3140                 }
3141                 if (IS_ERR(set))
3142                         return PTR_ERR(set);
3143         }
3144
3145         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3146                 return -EBUSY;
3147
3148         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3149                 err = nft_add_set_elem(&ctx, set, attr);
3150                 if (err < 0)
3151                         break;
3152
3153                 set->nelems++;
3154         }
3155         return err;
3156 }
3157
3158 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3159                            const struct nlattr *attr)
3160 {
3161         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3162         struct nft_data_desc desc;
3163         struct nft_set_elem elem;
3164         struct nft_trans *trans;
3165         int err;
3166
3167         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3168                                nft_set_elem_policy);
3169         if (err < 0)
3170                 goto err1;
3171
3172         err = -EINVAL;
3173         if (nla[NFTA_SET_ELEM_KEY] == NULL)
3174                 goto err1;
3175
3176         err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3177         if (err < 0)
3178                 goto err1;
3179
3180         err = -EINVAL;
3181         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3182                 goto err2;
3183
3184         err = set->ops->get(set, &elem);
3185         if (err < 0)
3186                 goto err2;
3187
3188         trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3189         if (trans == NULL)
3190                 goto err2;
3191
3192         nft_trans_elem(trans) = elem;
3193         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3194
3195         nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3196         if (set->flags & NFT_SET_MAP)
3197                 nft_data_uninit(&elem.data, set->dtype);
3198
3199 err2:
3200         nft_data_uninit(&elem.key, desc.type);
3201 err1:
3202         return err;
3203 }
3204
3205 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3206                                 const struct nlmsghdr *nlh,
3207                                 const struct nlattr * const nla[])
3208 {
3209         const struct nlattr *attr;
3210         struct nft_set *set;
3211         struct nft_ctx ctx;
3212         int rem, err = 0;
3213
3214         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3215         if (err < 0)
3216                 return err;
3217
3218         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3219         if (IS_ERR(set))
3220                 return PTR_ERR(set);
3221         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3222                 return -EBUSY;
3223
3224         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3225                 err = nft_del_setelem(&ctx, set, attr);
3226                 if (err < 0)
3227                         break;
3228
3229                 set->nelems--;
3230         }
3231         return err;
3232 }
3233
3234 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3235         [NFT_MSG_NEWTABLE] = {
3236                 .call_batch     = nf_tables_newtable,
3237                 .attr_count     = NFTA_TABLE_MAX,
3238                 .policy         = nft_table_policy,
3239         },
3240         [NFT_MSG_GETTABLE] = {
3241                 .call           = nf_tables_gettable,
3242                 .attr_count     = NFTA_TABLE_MAX,
3243                 .policy         = nft_table_policy,
3244         },
3245         [NFT_MSG_DELTABLE] = {
3246                 .call_batch     = nf_tables_deltable,
3247                 .attr_count     = NFTA_TABLE_MAX,
3248                 .policy         = nft_table_policy,
3249         },
3250         [NFT_MSG_NEWCHAIN] = {
3251                 .call_batch     = nf_tables_newchain,
3252                 .attr_count     = NFTA_CHAIN_MAX,
3253                 .policy         = nft_chain_policy,
3254         },
3255         [NFT_MSG_GETCHAIN] = {
3256                 .call           = nf_tables_getchain,
3257                 .attr_count     = NFTA_CHAIN_MAX,
3258                 .policy         = nft_chain_policy,
3259         },
3260         [NFT_MSG_DELCHAIN] = {
3261                 .call_batch     = nf_tables_delchain,
3262                 .attr_count     = NFTA_CHAIN_MAX,
3263                 .policy         = nft_chain_policy,
3264         },
3265         [NFT_MSG_NEWRULE] = {
3266                 .call_batch     = nf_tables_newrule,
3267                 .attr_count     = NFTA_RULE_MAX,
3268                 .policy         = nft_rule_policy,
3269         },
3270         [NFT_MSG_GETRULE] = {
3271                 .call           = nf_tables_getrule,
3272                 .attr_count     = NFTA_RULE_MAX,
3273                 .policy         = nft_rule_policy,
3274         },
3275         [NFT_MSG_DELRULE] = {
3276                 .call_batch     = nf_tables_delrule,
3277                 .attr_count     = NFTA_RULE_MAX,
3278                 .policy         = nft_rule_policy,
3279         },
3280         [NFT_MSG_NEWSET] = {
3281                 .call_batch     = nf_tables_newset,
3282                 .attr_count     = NFTA_SET_MAX,
3283                 .policy         = nft_set_policy,
3284         },
3285         [NFT_MSG_GETSET] = {
3286                 .call           = nf_tables_getset,
3287                 .attr_count     = NFTA_SET_MAX,
3288                 .policy         = nft_set_policy,
3289         },
3290         [NFT_MSG_DELSET] = {
3291                 .call_batch     = nf_tables_delset,
3292                 .attr_count     = NFTA_SET_MAX,
3293                 .policy         = nft_set_policy,
3294         },
3295         [NFT_MSG_NEWSETELEM] = {
3296                 .call_batch     = nf_tables_newsetelem,
3297                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3298                 .policy         = nft_set_elem_list_policy,
3299         },
3300         [NFT_MSG_GETSETELEM] = {
3301                 .call           = nf_tables_getsetelem,
3302                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3303                 .policy         = nft_set_elem_list_policy,
3304         },
3305         [NFT_MSG_DELSETELEM] = {
3306                 .call_batch     = nf_tables_delsetelem,
3307                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3308                 .policy         = nft_set_elem_list_policy,
3309         },
3310 };
3311
3312 static void nft_chain_commit_update(struct nft_trans *trans)
3313 {
3314         struct nft_base_chain *basechain;
3315
3316         if (nft_trans_chain_name(trans)[0])
3317                 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3318
3319         if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3320                 return;
3321
3322         basechain = nft_base_chain(trans->ctx.chain);
3323         nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3324
3325         switch (nft_trans_chain_policy(trans)) {
3326         case NF_DROP:
3327         case NF_ACCEPT:
3328                 basechain->policy = nft_trans_chain_policy(trans);
3329                 break;
3330         }
3331 }
3332
3333 /* Schedule objects for release via rcu to make sure no packets are accesing
3334  * removed rules.
3335  */
3336 static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3337 {
3338         struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3339
3340         switch (trans->msg_type) {
3341         case NFT_MSG_DELTABLE:
3342                 nf_tables_table_destroy(&trans->ctx);
3343                 break;
3344         case NFT_MSG_DELCHAIN:
3345                 nf_tables_chain_destroy(trans->ctx.chain);
3346                 break;
3347         case NFT_MSG_DELRULE:
3348                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3349                 break;
3350         case NFT_MSG_DELSET:
3351                 nft_set_destroy(nft_trans_set(trans));
3352                 break;
3353         }
3354         kfree(trans);
3355 }
3356
3357 static int nf_tables_commit(struct sk_buff *skb)
3358 {
3359         struct net *net = sock_net(skb->sk);
3360         struct nft_trans *trans, *next;
3361         struct nft_set *set;
3362
3363         /* Bump generation counter, invalidate any dump in progress */
3364         net->nft.genctr++;
3365
3366         /* A new generation has just started */
3367         net->nft.gencursor = gencursor_next(net);
3368
3369         /* Make sure all packets have left the previous generation before
3370          * purging old rules.
3371          */
3372         synchronize_rcu();
3373
3374         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3375                 switch (trans->msg_type) {
3376                 case NFT_MSG_NEWTABLE:
3377                         if (nft_trans_table_update(trans)) {
3378                                 if (!nft_trans_table_enable(trans)) {
3379                                         nf_tables_table_disable(trans->ctx.afi,
3380                                                                 trans->ctx.table);
3381                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3382                                 }
3383                         } else {
3384                                 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3385                         }
3386                         nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3387                         nft_trans_destroy(trans);
3388                         break;
3389                 case NFT_MSG_DELTABLE:
3390                         nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3391                         break;
3392                 case NFT_MSG_NEWCHAIN:
3393                         if (nft_trans_chain_update(trans))
3394                                 nft_chain_commit_update(trans);
3395                         else
3396                                 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3397
3398                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3399                         nft_trans_destroy(trans);
3400                         break;
3401                 case NFT_MSG_DELCHAIN:
3402                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3403                         if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3404                             trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3405                                 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3406                                                     trans->ctx.afi->nops);
3407                         }
3408                         break;
3409                 case NFT_MSG_NEWRULE:
3410                         nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3411                         nf_tables_rule_notify(&trans->ctx,
3412                                               nft_trans_rule(trans),
3413                                               NFT_MSG_NEWRULE);
3414                         nft_trans_destroy(trans);
3415                         break;
3416                 case NFT_MSG_DELRULE:
3417                         list_del_rcu(&nft_trans_rule(trans)->list);
3418                         nf_tables_rule_notify(&trans->ctx,
3419                                               nft_trans_rule(trans),
3420                                               NFT_MSG_DELRULE);
3421                         break;
3422                 case NFT_MSG_NEWSET:
3423                         nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3424                         /* This avoids hitting -EBUSY when deleting the table
3425                          * from the transaction.
3426                          */
3427                         if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3428                             !list_empty(&nft_trans_set(trans)->bindings))
3429                                 trans->ctx.table->use--;
3430
3431                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3432                                              NFT_MSG_NEWSET, GFP_KERNEL);
3433                         nft_trans_destroy(trans);
3434                         break;
3435                 case NFT_MSG_DELSET:
3436                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3437                                              NFT_MSG_DELSET, GFP_KERNEL);
3438                         break;
3439                 case NFT_MSG_NEWSETELEM:
3440                         nf_tables_setelem_notify(&trans->ctx,
3441                                                  nft_trans_elem_set(trans),
3442                                                  &nft_trans_elem(trans),
3443                                                  NFT_MSG_NEWSETELEM, 0);
3444                         nft_trans_destroy(trans);
3445                         break;
3446                 case NFT_MSG_DELSETELEM:
3447                         nf_tables_setelem_notify(&trans->ctx,
3448                                                  nft_trans_elem_set(trans),
3449                                                  &nft_trans_elem(trans),
3450                                                  NFT_MSG_DELSETELEM, 0);
3451                         set = nft_trans_elem_set(trans);
3452                         set->ops->get(set, &nft_trans_elem(trans));
3453                         set->ops->remove(set, &nft_trans_elem(trans));
3454                         nft_trans_destroy(trans);
3455                         break;
3456                 }
3457         }
3458
3459         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3460                 list_del(&trans->list);
3461                 trans->ctx.nla = NULL;
3462                 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
3463         }
3464
3465         return 0;
3466 }
3467
3468 /* Schedule objects for release via rcu to make sure no packets are accesing
3469  * aborted rules.
3470  */
3471 static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3472 {
3473         struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3474
3475         switch (trans->msg_type) {
3476         case NFT_MSG_NEWTABLE:
3477                 nf_tables_table_destroy(&trans->ctx);
3478                 break;
3479         case NFT_MSG_NEWCHAIN:
3480                 nf_tables_chain_destroy(trans->ctx.chain);
3481                 break;
3482         case NFT_MSG_NEWRULE:
3483                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3484                 break;
3485         case NFT_MSG_NEWSET:
3486                 nft_set_destroy(nft_trans_set(trans));
3487                 break;
3488         }
3489         kfree(trans);
3490 }
3491
3492 static int nf_tables_abort(struct sk_buff *skb)
3493 {
3494         struct net *net = sock_net(skb->sk);
3495         struct nft_trans *trans, *next;
3496         struct nft_set *set;
3497
3498         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3499                 switch (trans->msg_type) {
3500                 case NFT_MSG_NEWTABLE:
3501                         if (nft_trans_table_update(trans)) {
3502                                 if (nft_trans_table_enable(trans)) {
3503                                         nf_tables_table_disable(trans->ctx.afi,
3504                                                                 trans->ctx.table);
3505                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3506                                 }
3507                                 nft_trans_destroy(trans);
3508                         } else {
3509                                 list_del_rcu(&trans->ctx.table->list);
3510                         }
3511                         break;
3512                 case NFT_MSG_DELTABLE:
3513                         list_add_tail_rcu(&trans->ctx.table->list,
3514                                           &trans->ctx.afi->tables);
3515                         nft_trans_destroy(trans);
3516                         break;
3517                 case NFT_MSG_NEWCHAIN:
3518                         if (nft_trans_chain_update(trans)) {
3519                                 if (nft_trans_chain_stats(trans))
3520                                         free_percpu(nft_trans_chain_stats(trans));
3521
3522                                 nft_trans_destroy(trans);
3523                         } else {
3524                                 trans->ctx.table->use--;
3525                                 list_del_rcu(&trans->ctx.chain->list);
3526                                 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3527                                     trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3528                                         nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3529                                                             trans->ctx.afi->nops);
3530                                 }
3531                         }
3532                         break;
3533                 case NFT_MSG_DELCHAIN:
3534                         trans->ctx.table->use++;
3535                         list_add_tail_rcu(&trans->ctx.chain->list,
3536                                           &trans->ctx.table->chains);
3537                         nft_trans_destroy(trans);
3538                         break;
3539                 case NFT_MSG_NEWRULE:
3540                         trans->ctx.chain->use--;
3541                         list_del_rcu(&nft_trans_rule(trans)->list);
3542                         break;
3543                 case NFT_MSG_DELRULE:
3544                         trans->ctx.chain->use++;
3545                         nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3546                         nft_trans_destroy(trans);
3547                         break;
3548                 case NFT_MSG_NEWSET:
3549                         trans->ctx.table->use--;
3550                         list_del_rcu(&nft_trans_set(trans)->list);
3551                         break;
3552                 case NFT_MSG_DELSET:
3553                         trans->ctx.table->use++;
3554                         list_add_tail_rcu(&nft_trans_set(trans)->list,
3555                                           &trans->ctx.table->sets);
3556                         nft_trans_destroy(trans);
3557                         break;
3558                 case NFT_MSG_NEWSETELEM:
3559                         nft_trans_elem_set(trans)->nelems--;
3560                         set = nft_trans_elem_set(trans);
3561                         set->ops->get(set, &nft_trans_elem(trans));
3562                         set->ops->remove(set, &nft_trans_elem(trans));
3563                         nft_trans_destroy(trans);
3564                         break;
3565                 case NFT_MSG_DELSETELEM:
3566                         nft_trans_elem_set(trans)->nelems++;
3567                         nft_trans_destroy(trans);
3568                         break;
3569                 }
3570         }
3571
3572         list_for_each_entry_safe_reverse(trans, next,
3573                                          &net->nft.commit_list, list) {
3574                 list_del(&trans->list);
3575                 trans->ctx.nla = NULL;
3576                 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
3577         }
3578
3579         return 0;
3580 }
3581
3582 static const struct nfnetlink_subsystem nf_tables_subsys = {
3583         .name           = "nf_tables",
3584         .subsys_id      = NFNL_SUBSYS_NFTABLES,
3585         .cb_count       = NFT_MSG_MAX,
3586         .cb             = nf_tables_cb,
3587         .commit         = nf_tables_commit,
3588         .abort          = nf_tables_abort,
3589 };
3590
3591 /*
3592  * Loop detection - walk through the ruleset beginning at the destination chain
3593  * of a new jump until either the source chain is reached (loop) or all
3594  * reachable chains have been traversed.
3595  *
3596  * The loop check is performed whenever a new jump verdict is added to an
3597  * expression or verdict map or a verdict map is bound to a new chain.
3598  */
3599
3600 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3601                                  const struct nft_chain *chain);
3602
3603 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3604                                         const struct nft_set *set,
3605                                         const struct nft_set_iter *iter,
3606                                         const struct nft_set_elem *elem)
3607 {
3608         if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3609                 return 0;
3610
3611         switch (elem->data.verdict) {
3612         case NFT_JUMP:
3613         case NFT_GOTO:
3614                 return nf_tables_check_loops(ctx, elem->data.chain);
3615         default:
3616                 return 0;
3617         }
3618 }
3619
3620 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3621                                  const struct nft_chain *chain)
3622 {
3623         const struct nft_rule *rule;
3624         const struct nft_expr *expr, *last;
3625         const struct nft_set *set;
3626         struct nft_set_binding *binding;
3627         struct nft_set_iter iter;
3628
3629         if (ctx->chain == chain)
3630                 return -ELOOP;
3631
3632         list_for_each_entry(rule, &chain->rules, list) {
3633                 nft_rule_for_each_expr(expr, last, rule) {
3634                         const struct nft_data *data = NULL;
3635                         int err;
3636
3637                         if (!expr->ops->validate)
3638                                 continue;
3639
3640                         err = expr->ops->validate(ctx, expr, &data);
3641                         if (err < 0)
3642                                 return err;
3643
3644                         if (data == NULL)
3645                                 continue;
3646
3647                         switch (data->verdict) {
3648                         case NFT_JUMP:
3649                         case NFT_GOTO:
3650                                 err = nf_tables_check_loops(ctx, data->chain);
3651                                 if (err < 0)
3652                                         return err;
3653                         default:
3654                                 break;
3655                         }
3656                 }
3657         }
3658
3659         list_for_each_entry(set, &ctx->table->sets, list) {
3660                 if (!(set->flags & NFT_SET_MAP) ||
3661                     set->dtype != NFT_DATA_VERDICT)
3662                         continue;
3663
3664                 list_for_each_entry(binding, &set->bindings, list) {
3665                         if (binding->chain != chain)
3666                                 continue;
3667
3668                         iter.skip       = 0;
3669                         iter.count      = 0;
3670                         iter.err        = 0;
3671                         iter.fn         = nf_tables_loop_check_setelem;
3672
3673                         set->ops->walk(ctx, set, &iter);
3674                         if (iter.err < 0)
3675                                 return iter.err;
3676                 }
3677         }
3678
3679         return 0;
3680 }
3681
3682 /**
3683  *      nft_validate_input_register - validate an expressions' input register
3684  *
3685  *      @reg: the register number
3686  *
3687  *      Validate that the input register is one of the general purpose
3688  *      registers.
3689  */
3690 int nft_validate_input_register(enum nft_registers reg)
3691 {
3692         if (reg <= NFT_REG_VERDICT)
3693                 return -EINVAL;
3694         if (reg > NFT_REG_MAX)
3695                 return -ERANGE;
3696         return 0;
3697 }
3698 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3699
3700 /**
3701  *      nft_validate_output_register - validate an expressions' output register
3702  *
3703  *      @reg: the register number
3704  *
3705  *      Validate that the output register is one of the general purpose
3706  *      registers or the verdict register.
3707  */
3708 int nft_validate_output_register(enum nft_registers reg)
3709 {
3710         if (reg < NFT_REG_VERDICT)
3711                 return -EINVAL;
3712         if (reg > NFT_REG_MAX)
3713                 return -ERANGE;
3714         return 0;
3715 }
3716 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3717
3718 /**
3719  *      nft_validate_data_load - validate an expressions' data load
3720  *
3721  *      @ctx: context of the expression performing the load
3722  *      @reg: the destination register number
3723  *      @data: the data to load
3724  *      @type: the data type
3725  *
3726  *      Validate that a data load uses the appropriate data type for
3727  *      the destination register. A value of NULL for the data means
3728  *      that its runtime gathered data, which is always of type
3729  *      NFT_DATA_VALUE.
3730  */
3731 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3732                            const struct nft_data *data,
3733                            enum nft_data_types type)
3734 {
3735         int err;
3736
3737         switch (reg) {
3738         case NFT_REG_VERDICT:
3739                 if (data == NULL || type != NFT_DATA_VERDICT)
3740                         return -EINVAL;
3741
3742                 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3743                         err = nf_tables_check_loops(ctx, data->chain);
3744                         if (err < 0)
3745                                 return err;
3746
3747                         if (ctx->chain->level + 1 > data->chain->level) {
3748                                 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3749                                         return -EMLINK;
3750                                 data->chain->level = ctx->chain->level + 1;
3751                         }
3752                 }
3753
3754                 return 0;
3755         default:
3756                 if (data != NULL && type != NFT_DATA_VALUE)
3757                         return -EINVAL;
3758                 return 0;
3759         }
3760 }
3761 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3762
3763 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3764         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
3765         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
3766                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
3767 };
3768
3769 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3770                             struct nft_data_desc *desc, const struct nlattr *nla)
3771 {
3772         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3773         struct nft_chain *chain;
3774         int err;
3775
3776         err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3777         if (err < 0)
3778                 return err;
3779
3780         if (!tb[NFTA_VERDICT_CODE])
3781                 return -EINVAL;
3782         data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3783
3784         switch (data->verdict) {
3785         default:
3786                 switch (data->verdict & NF_VERDICT_MASK) {
3787                 case NF_ACCEPT:
3788                 case NF_DROP:
3789                 case NF_QUEUE:
3790                         break;
3791                 default:
3792                         return -EINVAL;
3793                 }
3794                 /* fall through */
3795         case NFT_CONTINUE:
3796         case NFT_BREAK:
3797         case NFT_RETURN:
3798                 desc->len = sizeof(data->verdict);
3799                 break;
3800         case NFT_JUMP:
3801         case NFT_GOTO:
3802                 if (!tb[NFTA_VERDICT_CHAIN])
3803                         return -EINVAL;
3804                 chain = nf_tables_chain_lookup(ctx->table,
3805                                                tb[NFTA_VERDICT_CHAIN]);
3806                 if (IS_ERR(chain))
3807                         return PTR_ERR(chain);
3808                 if (chain->flags & NFT_BASE_CHAIN)
3809                         return -EOPNOTSUPP;
3810
3811                 chain->use++;
3812                 data->chain = chain;
3813                 desc->len = sizeof(data);
3814                 break;
3815         }
3816
3817         desc->type = NFT_DATA_VERDICT;
3818         return 0;
3819 }
3820
3821 static void nft_verdict_uninit(const struct nft_data *data)
3822 {
3823         switch (data->verdict) {
3824         case NFT_JUMP:
3825         case NFT_GOTO:
3826                 data->chain->use--;
3827                 break;
3828         }
3829 }
3830
3831 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3832 {
3833         struct nlattr *nest;
3834
3835         nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3836         if (!nest)
3837                 goto nla_put_failure;
3838
3839         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3840                 goto nla_put_failure;
3841
3842         switch (data->verdict) {
3843         case NFT_JUMP:
3844         case NFT_GOTO:
3845                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3846                         goto nla_put_failure;
3847         }
3848         nla_nest_end(skb, nest);
3849         return 0;
3850
3851 nla_put_failure:
3852         return -1;
3853 }
3854
3855 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3856                           struct nft_data_desc *desc, const struct nlattr *nla)
3857 {
3858         unsigned int len;
3859
3860         len = nla_len(nla);
3861         if (len == 0)
3862                 return -EINVAL;
3863         if (len > sizeof(data->data))
3864                 return -EOVERFLOW;
3865
3866         nla_memcpy(data->data, nla, sizeof(data->data));
3867         desc->type = NFT_DATA_VALUE;
3868         desc->len  = len;
3869         return 0;
3870 }
3871
3872 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3873                           unsigned int len)
3874 {
3875         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3876 }
3877
3878 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3879         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY,
3880                                     .len  = FIELD_SIZEOF(struct nft_data, data) },
3881         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
3882 };
3883
3884 /**
3885  *      nft_data_init - parse nf_tables data netlink attributes
3886  *
3887  *      @ctx: context of the expression using the data
3888  *      @data: destination struct nft_data
3889  *      @desc: data description
3890  *      @nla: netlink attribute containing data
3891  *
3892  *      Parse the netlink data attributes and initialize a struct nft_data.
3893  *      The type and length of data are returned in the data description.
3894  *
3895  *      The caller can indicate that it only wants to accept data of type
3896  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
3897  */
3898 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3899                   struct nft_data_desc *desc, const struct nlattr *nla)
3900 {
3901         struct nlattr *tb[NFTA_DATA_MAX + 1];
3902         int err;
3903
3904         err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3905         if (err < 0)
3906                 return err;
3907
3908         if (tb[NFTA_DATA_VALUE])
3909                 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3910         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3911                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3912         return -EINVAL;
3913 }
3914 EXPORT_SYMBOL_GPL(nft_data_init);
3915
3916 /**
3917  *      nft_data_uninit - release a nft_data item
3918  *
3919  *      @data: struct nft_data to release
3920  *      @type: type of data
3921  *
3922  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3923  *      all others need to be released by calling this function.
3924  */
3925 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3926 {
3927         switch (type) {
3928         case NFT_DATA_VALUE:
3929                 return;
3930         case NFT_DATA_VERDICT:
3931                 return nft_verdict_uninit(data);
3932         default:
3933                 WARN_ON(1);
3934         }
3935 }
3936 EXPORT_SYMBOL_GPL(nft_data_uninit);
3937
3938 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3939                   enum nft_data_types type, unsigned int len)
3940 {
3941         struct nlattr *nest;
3942         int err;
3943
3944         nest = nla_nest_start(skb, attr);
3945         if (nest == NULL)
3946                 return -1;
3947
3948         switch (type) {
3949         case NFT_DATA_VALUE:
3950                 err = nft_value_dump(skb, data, len);
3951                 break;
3952         case NFT_DATA_VERDICT:
3953                 err = nft_verdict_dump(skb, data);
3954                 break;
3955         default:
3956                 err = -EINVAL;
3957                 WARN_ON(1);
3958         }
3959
3960         nla_nest_end(skb, nest);
3961         return err;
3962 }
3963 EXPORT_SYMBOL_GPL(nft_data_dump);
3964
3965 static int nf_tables_init_net(struct net *net)
3966 {
3967         INIT_LIST_HEAD(&net->nft.af_info);
3968         INIT_LIST_HEAD(&net->nft.commit_list);
3969         return 0;
3970 }
3971
3972 static struct pernet_operations nf_tables_net_ops = {
3973         .init   = nf_tables_init_net,
3974 };
3975
3976 static int __init nf_tables_module_init(void)
3977 {
3978         int err;
3979
3980         info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3981                        GFP_KERNEL);
3982         if (info == NULL) {
3983                 err = -ENOMEM;
3984                 goto err1;
3985         }
3986
3987         err = nf_tables_core_module_init();
3988         if (err < 0)
3989                 goto err2;
3990
3991         err = nfnetlink_subsys_register(&nf_tables_subsys);
3992         if (err < 0)
3993                 goto err3;
3994
3995         pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3996         return register_pernet_subsys(&nf_tables_net_ops);
3997 err3:
3998         nf_tables_core_module_exit();
3999 err2:
4000         kfree(info);
4001 err1:
4002         return err;
4003 }
4004
4005 static void __exit nf_tables_module_exit(void)
4006 {
4007         unregister_pernet_subsys(&nf_tables_net_ops);
4008         nfnetlink_subsys_unregister(&nf_tables_subsys);
4009         nf_tables_core_module_exit();
4010         kfree(info);
4011 }
4012
4013 module_init(nf_tables_module_init);
4014 module_exit(nf_tables_module_exit);
4015
4016 MODULE_LICENSE("GPL");
4017 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4018 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);