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