aa4df58d5e564db96d06f44520ddcc249ac9dc91
[platform/adaptation/renesas_rcar/renesas_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 /*
92  * Tables
93  */
94
95 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96                                           const struct nlattr *nla)
97 {
98         struct nft_table *table;
99
100         list_for_each_entry(table, &afi->tables, list) {
101                 if (!nla_strcmp(nla, table->name))
102                         return table;
103         }
104         return NULL;
105 }
106
107 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
108                                                 const struct nlattr *nla)
109 {
110         struct nft_table *table;
111
112         if (nla == NULL)
113                 return ERR_PTR(-EINVAL);
114
115         table = nft_table_lookup(afi, nla);
116         if (table != NULL)
117                 return table;
118
119         return ERR_PTR(-ENOENT);
120 }
121
122 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
123 {
124         return ++table->hgenerator;
125 }
126
127 static struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
128
129 static int __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
130 {
131         int i;
132
133         for (i=0; i<NFT_CHAIN_T_MAX; i++) {
134                 if (chain_type[family][i] != NULL &&
135                     !nla_strcmp(nla, chain_type[family][i]->name))
136                         return i;
137         }
138         return -1;
139 }
140
141 static int nf_tables_chain_type_lookup(const struct nft_af_info *afi,
142                                        const struct nlattr *nla,
143                                        bool autoload)
144 {
145         int type;
146
147         type = __nf_tables_chain_type_lookup(afi->family, nla);
148 #ifdef CONFIG_MODULES
149         if (type < 0 && autoload) {
150                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
151                 request_module("nft-chain-%u-%*.s", afi->family,
152                                nla_len(nla)-1, (const char *)nla_data(nla));
153                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
154                 type = __nf_tables_chain_type_lookup(afi->family, nla);
155         }
156 #endif
157         return type;
158 }
159
160 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
161         [NFTA_TABLE_NAME]       = { .type = NLA_STRING },
162         [NFTA_TABLE_FLAGS]      = { .type = NLA_U32 },
163 };
164
165 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
166                                      int event, u32 flags, int family,
167                                      const struct nft_table *table)
168 {
169         struct nlmsghdr *nlh;
170         struct nfgenmsg *nfmsg;
171
172         event |= NFNL_SUBSYS_NFTABLES << 8;
173         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
174         if (nlh == NULL)
175                 goto nla_put_failure;
176
177         nfmsg = nlmsg_data(nlh);
178         nfmsg->nfgen_family     = family;
179         nfmsg->version          = NFNETLINK_V0;
180         nfmsg->res_id           = 0;
181
182         if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
183             nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
184             nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
185                 goto nla_put_failure;
186
187         return nlmsg_end(skb, nlh);
188
189 nla_put_failure:
190         nlmsg_trim(skb, nlh);
191         return -1;
192 }
193
194 static int nf_tables_table_notify(const struct sk_buff *oskb,
195                                   const struct nlmsghdr *nlh,
196                                   const struct nft_table *table,
197                                   int event, int family)
198 {
199         struct sk_buff *skb;
200         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
201         u32 seq = nlh ? nlh->nlmsg_seq : 0;
202         struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
203         bool report;
204         int err;
205
206         report = nlh ? nlmsg_report(nlh) : false;
207         if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
208                 return 0;
209
210         err = -ENOBUFS;
211         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
212         if (skb == NULL)
213                 goto err;
214
215         err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
216                                         family, table);
217         if (err < 0) {
218                 kfree_skb(skb);
219                 goto err;
220         }
221
222         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
223                              GFP_KERNEL);
224 err:
225         if (err < 0)
226                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
227         return err;
228 }
229
230 static int nf_tables_dump_tables(struct sk_buff *skb,
231                                  struct netlink_callback *cb)
232 {
233         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
234         const struct nft_af_info *afi;
235         const struct nft_table *table;
236         unsigned int idx = 0, s_idx = cb->args[0];
237         struct net *net = sock_net(skb->sk);
238         int family = nfmsg->nfgen_family;
239
240         list_for_each_entry(afi, &net->nft.af_info, list) {
241                 if (family != NFPROTO_UNSPEC && family != afi->family)
242                         continue;
243
244                 list_for_each_entry(table, &afi->tables, list) {
245                         if (idx < s_idx)
246                                 goto cont;
247                         if (idx > s_idx)
248                                 memset(&cb->args[1], 0,
249                                        sizeof(cb->args) - sizeof(cb->args[0]));
250                         if (nf_tables_fill_table_info(skb,
251                                                       NETLINK_CB(cb->skb).portid,
252                                                       cb->nlh->nlmsg_seq,
253                                                       NFT_MSG_NEWTABLE,
254                                                       NLM_F_MULTI,
255                                                       afi->family, table) < 0)
256                                 goto done;
257 cont:
258                         idx++;
259                 }
260         }
261 done:
262         cb->args[0] = idx;
263         return skb->len;
264 }
265
266 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
267                               const struct nlmsghdr *nlh,
268                               const struct nlattr * const nla[])
269 {
270         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
271         const struct nft_af_info *afi;
272         const struct nft_table *table;
273         struct sk_buff *skb2;
274         struct net *net = sock_net(skb->sk);
275         int family = nfmsg->nfgen_family;
276         int err;
277
278         if (nlh->nlmsg_flags & NLM_F_DUMP) {
279                 struct netlink_dump_control c = {
280                         .dump = nf_tables_dump_tables,
281                 };
282                 return netlink_dump_start(nlsk, skb, nlh, &c);
283         }
284
285         afi = nf_tables_afinfo_lookup(net, family, false);
286         if (IS_ERR(afi))
287                 return PTR_ERR(afi);
288
289         table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
290         if (IS_ERR(table))
291                 return PTR_ERR(table);
292
293         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
294         if (!skb2)
295                 return -ENOMEM;
296
297         err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
298                                         nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
299                                         family, table);
300         if (err < 0)
301                 goto err;
302
303         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
304
305 err:
306         kfree_skb(skb2);
307         return err;
308 }
309
310 static int nf_tables_table_enable(struct nft_table *table)
311 {
312         struct nft_chain *chain;
313         int err, i = 0;
314
315         list_for_each_entry(chain, &table->chains, list) {
316                 err = nf_register_hook(&nft_base_chain(chain)->ops);
317                 if (err < 0)
318                         goto err;
319
320                 i++;
321         }
322         return 0;
323 err:
324         list_for_each_entry(chain, &table->chains, list) {
325                 if (i-- <= 0)
326                         break;
327
328                 nf_unregister_hook(&nft_base_chain(chain)->ops);
329         }
330         return err;
331 }
332
333 static int nf_tables_table_disable(struct nft_table *table)
334 {
335         struct nft_chain *chain;
336
337         list_for_each_entry(chain, &table->chains, list)
338                 nf_unregister_hook(&nft_base_chain(chain)->ops);
339
340         return 0;
341 }
342
343 static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
344                               const struct nlmsghdr *nlh,
345                               const struct nlattr * const nla[],
346                               struct nft_af_info *afi, struct nft_table *table)
347 {
348         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
349         int family = nfmsg->nfgen_family, ret = 0;
350
351         if (nla[NFTA_TABLE_FLAGS]) {
352                 __be32 flags;
353
354                 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
355                 if (flags & ~NFT_TABLE_F_DORMANT)
356                         return -EINVAL;
357
358                 if ((flags & NFT_TABLE_F_DORMANT) &&
359                     !(table->flags & NFT_TABLE_F_DORMANT)) {
360                         ret = nf_tables_table_disable(table);
361                         if (ret >= 0)
362                                 table->flags |= NFT_TABLE_F_DORMANT;
363                 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
364                            table->flags & NFT_TABLE_F_DORMANT) {
365                         ret = nf_tables_table_enable(table);
366                         if (ret >= 0)
367                                 table->flags &= ~NFT_TABLE_F_DORMANT;
368                 }
369                 if (ret < 0)
370                         goto err;
371         }
372
373         nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
374 err:
375         return ret;
376 }
377
378 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
379                               const struct nlmsghdr *nlh,
380                               const struct nlattr * const nla[])
381 {
382         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
383         const struct nlattr *name;
384         struct nft_af_info *afi;
385         struct nft_table *table;
386         struct net *net = sock_net(skb->sk);
387         int family = nfmsg->nfgen_family;
388
389         afi = nf_tables_afinfo_lookup(net, family, true);
390         if (IS_ERR(afi))
391                 return PTR_ERR(afi);
392
393         name = nla[NFTA_TABLE_NAME];
394         table = nf_tables_table_lookup(afi, name);
395         if (IS_ERR(table)) {
396                 if (PTR_ERR(table) != -ENOENT)
397                         return PTR_ERR(table);
398                 table = NULL;
399         }
400
401         if (table != NULL) {
402                 if (nlh->nlmsg_flags & NLM_F_EXCL)
403                         return -EEXIST;
404                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
405                         return -EOPNOTSUPP;
406                 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
407         }
408
409         table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
410         if (table == NULL)
411                 return -ENOMEM;
412
413         nla_strlcpy(table->name, name, nla_len(name));
414         INIT_LIST_HEAD(&table->chains);
415         INIT_LIST_HEAD(&table->sets);
416
417         if (nla[NFTA_TABLE_FLAGS]) {
418                 __be32 flags;
419
420                 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
421                 if (flags & ~NFT_TABLE_F_DORMANT) {
422                         kfree(table);
423                         return -EINVAL;
424                 }
425
426                 table->flags |= flags;
427         }
428
429         list_add_tail(&table->list, &afi->tables);
430         nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
431         return 0;
432 }
433
434 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
435                               const struct nlmsghdr *nlh,
436                               const struct nlattr * const nla[])
437 {
438         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
439         struct nft_af_info *afi;
440         struct nft_table *table;
441         struct net *net = sock_net(skb->sk);
442         int family = nfmsg->nfgen_family;
443
444         afi = nf_tables_afinfo_lookup(net, family, false);
445         if (IS_ERR(afi))
446                 return PTR_ERR(afi);
447
448         table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
449         if (IS_ERR(table))
450                 return PTR_ERR(table);
451
452         if (table->use)
453                 return -EBUSY;
454
455         list_del(&table->list);
456         nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
457         kfree(table);
458         return 0;
459 }
460
461 int nft_register_chain_type(struct nf_chain_type *ctype)
462 {
463         int err = 0;
464
465         nfnl_lock(NFNL_SUBSYS_NFTABLES);
466         if (chain_type[ctype->family][ctype->type] != NULL) {
467                 err = -EBUSY;
468                 goto out;
469         }
470
471         if (!try_module_get(ctype->me))
472                 goto out;
473
474         chain_type[ctype->family][ctype->type] = ctype;
475 out:
476         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
477         return err;
478 }
479 EXPORT_SYMBOL_GPL(nft_register_chain_type);
480
481 void nft_unregister_chain_type(struct nf_chain_type *ctype)
482 {
483         nfnl_lock(NFNL_SUBSYS_NFTABLES);
484         chain_type[ctype->family][ctype->type] = NULL;
485         module_put(ctype->me);
486         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
487 }
488 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
489
490 /*
491  * Chains
492  */
493
494 static struct nft_chain *
495 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
496 {
497         struct nft_chain *chain;
498
499         list_for_each_entry(chain, &table->chains, list) {
500                 if (chain->handle == handle)
501                         return chain;
502         }
503
504         return ERR_PTR(-ENOENT);
505 }
506
507 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
508                                                 const struct nlattr *nla)
509 {
510         struct nft_chain *chain;
511
512         if (nla == NULL)
513                 return ERR_PTR(-EINVAL);
514
515         list_for_each_entry(chain, &table->chains, list) {
516                 if (!nla_strcmp(nla, chain->name))
517                         return chain;
518         }
519
520         return ERR_PTR(-ENOENT);
521 }
522
523 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
524         [NFTA_CHAIN_TABLE]      = { .type = NLA_STRING },
525         [NFTA_CHAIN_HANDLE]     = { .type = NLA_U64 },
526         [NFTA_CHAIN_NAME]       = { .type = NLA_STRING,
527                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
528         [NFTA_CHAIN_HOOK]       = { .type = NLA_NESTED },
529         [NFTA_CHAIN_POLICY]     = { .type = NLA_U32 },
530         [NFTA_CHAIN_TYPE]       = { .type = NLA_NUL_STRING },
531         [NFTA_CHAIN_COUNTERS]   = { .type = NLA_NESTED },
532 };
533
534 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
535         [NFTA_HOOK_HOOKNUM]     = { .type = NLA_U32 },
536         [NFTA_HOOK_PRIORITY]    = { .type = NLA_U32 },
537 };
538
539 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
540 {
541         struct nft_stats *cpu_stats, total;
542         struct nlattr *nest;
543         int cpu;
544
545         memset(&total, 0, sizeof(total));
546         for_each_possible_cpu(cpu) {
547                 cpu_stats = per_cpu_ptr(stats, cpu);
548                 total.pkts += cpu_stats->pkts;
549                 total.bytes += cpu_stats->bytes;
550         }
551         nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
552         if (nest == NULL)
553                 goto nla_put_failure;
554
555         if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
556             nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
557                 goto nla_put_failure;
558
559         nla_nest_end(skb, nest);
560         return 0;
561
562 nla_put_failure:
563         return -ENOSPC;
564 }
565
566 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
567                                      int event, u32 flags, int family,
568                                      const struct nft_table *table,
569                                      const struct nft_chain *chain)
570 {
571         struct nlmsghdr *nlh;
572         struct nfgenmsg *nfmsg;
573
574         event |= NFNL_SUBSYS_NFTABLES << 8;
575         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
576         if (nlh == NULL)
577                 goto nla_put_failure;
578
579         nfmsg = nlmsg_data(nlh);
580         nfmsg->nfgen_family     = family;
581         nfmsg->version          = NFNETLINK_V0;
582         nfmsg->res_id           = 0;
583
584         if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
585                 goto nla_put_failure;
586         if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
587                 goto nla_put_failure;
588         if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
589                 goto nla_put_failure;
590
591         if (chain->flags & NFT_BASE_CHAIN) {
592                 const struct nft_base_chain *basechain = nft_base_chain(chain);
593                 const struct nf_hook_ops *ops = &basechain->ops;
594                 struct nlattr *nest;
595
596                 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
597                 if (nest == NULL)
598                         goto nla_put_failure;
599                 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
600                         goto nla_put_failure;
601                 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
602                         goto nla_put_failure;
603                 nla_nest_end(skb, nest);
604
605                 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
606                                  htonl(basechain->policy)))
607                         goto nla_put_failure;
608
609                 if (nla_put_string(skb, NFTA_CHAIN_TYPE,
610                         chain_type[ops->pf][nft_base_chain(chain)->type]->name))
611                                 goto nla_put_failure;
612
613                 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
614                         goto nla_put_failure;
615         }
616
617         if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
618                 goto nla_put_failure;
619
620         return nlmsg_end(skb, nlh);
621
622 nla_put_failure:
623         nlmsg_trim(skb, nlh);
624         return -1;
625 }
626
627 static int nf_tables_chain_notify(const struct sk_buff *oskb,
628                                   const struct nlmsghdr *nlh,
629                                   const struct nft_table *table,
630                                   const struct nft_chain *chain,
631                                   int event, int family)
632 {
633         struct sk_buff *skb;
634         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
635         struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
636         u32 seq = nlh ? nlh->nlmsg_seq : 0;
637         bool report;
638         int err;
639
640         report = nlh ? nlmsg_report(nlh) : false;
641         if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
642                 return 0;
643
644         err = -ENOBUFS;
645         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
646         if (skb == NULL)
647                 goto err;
648
649         err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
650                                         table, chain);
651         if (err < 0) {
652                 kfree_skb(skb);
653                 goto err;
654         }
655
656         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
657                              GFP_KERNEL);
658 err:
659         if (err < 0)
660                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
661         return err;
662 }
663
664 static int nf_tables_dump_chains(struct sk_buff *skb,
665                                  struct netlink_callback *cb)
666 {
667         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
668         const struct nft_af_info *afi;
669         const struct nft_table *table;
670         const struct nft_chain *chain;
671         unsigned int idx = 0, s_idx = cb->args[0];
672         struct net *net = sock_net(skb->sk);
673         int family = nfmsg->nfgen_family;
674
675         list_for_each_entry(afi, &net->nft.af_info, list) {
676                 if (family != NFPROTO_UNSPEC && family != afi->family)
677                         continue;
678
679                 list_for_each_entry(table, &afi->tables, list) {
680                         list_for_each_entry(chain, &table->chains, list) {
681                                 if (idx < s_idx)
682                                         goto cont;
683                                 if (idx > s_idx)
684                                         memset(&cb->args[1], 0,
685                                                sizeof(cb->args) - sizeof(cb->args[0]));
686                                 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
687                                                               cb->nlh->nlmsg_seq,
688                                                               NFT_MSG_NEWCHAIN,
689                                                               NLM_F_MULTI,
690                                                               afi->family, table, chain) < 0)
691                                         goto done;
692 cont:
693                                 idx++;
694                         }
695                 }
696         }
697 done:
698         cb->args[0] = idx;
699         return skb->len;
700 }
701
702
703 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
704                               const struct nlmsghdr *nlh,
705                               const struct nlattr * const nla[])
706 {
707         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
708         const struct nft_af_info *afi;
709         const struct nft_table *table;
710         const struct nft_chain *chain;
711         struct sk_buff *skb2;
712         struct net *net = sock_net(skb->sk);
713         int family = nfmsg->nfgen_family;
714         int err;
715
716         if (nlh->nlmsg_flags & NLM_F_DUMP) {
717                 struct netlink_dump_control c = {
718                         .dump = nf_tables_dump_chains,
719                 };
720                 return netlink_dump_start(nlsk, skb, nlh, &c);
721         }
722
723         afi = nf_tables_afinfo_lookup(net, family, false);
724         if (IS_ERR(afi))
725                 return PTR_ERR(afi);
726
727         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
728         if (IS_ERR(table))
729                 return PTR_ERR(table);
730
731         chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
732         if (IS_ERR(chain))
733                 return PTR_ERR(chain);
734
735         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
736         if (!skb2)
737                 return -ENOMEM;
738
739         err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
740                                         nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
741                                         family, table, chain);
742         if (err < 0)
743                 goto err;
744
745         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
746
747 err:
748         kfree_skb(skb2);
749         return err;
750 }
751
752 static int
753 nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
754 {
755         switch (ntohl(nla_get_be32(attr))) {
756         case NF_DROP:
757                 chain->policy = NF_DROP;
758                 break;
759         case NF_ACCEPT:
760                 chain->policy = NF_ACCEPT;
761                 break;
762         default:
763                 return -EINVAL;
764         }
765         return 0;
766 }
767
768 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
769         [NFTA_COUNTER_PACKETS]  = { .type = NLA_U64 },
770         [NFTA_COUNTER_BYTES]    = { .type = NLA_U64 },
771 };
772
773 static int
774 nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
775 {
776         struct nlattr *tb[NFTA_COUNTER_MAX+1];
777         struct nft_stats __percpu *newstats;
778         struct nft_stats *stats;
779         int err;
780
781         err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
782         if (err < 0)
783                 return err;
784
785         if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
786                 return -EINVAL;
787
788         newstats = alloc_percpu(struct nft_stats);
789         if (newstats == NULL)
790                 return -ENOMEM;
791
792         /* Restore old counters on this cpu, no problem. Per-cpu statistics
793          * are not exposed to userspace.
794          */
795         stats = this_cpu_ptr(newstats);
796         stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
797         stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
798
799         if (chain->stats) {
800                 /* nfnl_lock is held, add some nfnl function for this, later */
801                 struct nft_stats __percpu *oldstats =
802                         rcu_dereference_protected(chain->stats, 1);
803
804                 rcu_assign_pointer(chain->stats, newstats);
805                 synchronize_rcu();
806                 free_percpu(oldstats);
807         } else
808                 rcu_assign_pointer(chain->stats, newstats);
809
810         return 0;
811 }
812
813 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
814                               const struct nlmsghdr *nlh,
815                               const struct nlattr * const nla[])
816 {
817         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
818         const struct nlattr * uninitialized_var(name);
819         const struct nft_af_info *afi;
820         struct nft_table *table;
821         struct nft_chain *chain;
822         struct nft_base_chain *basechain = NULL;
823         struct nlattr *ha[NFTA_HOOK_MAX + 1];
824         struct net *net = sock_net(skb->sk);
825         int family = nfmsg->nfgen_family;
826         u64 handle = 0;
827         int err;
828         bool create;
829
830         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
831
832         afi = nf_tables_afinfo_lookup(net, family, true);
833         if (IS_ERR(afi))
834                 return PTR_ERR(afi);
835
836         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
837         if (IS_ERR(table))
838                 return PTR_ERR(table);
839
840         if (table->use == UINT_MAX)
841                 return -EOVERFLOW;
842
843         chain = NULL;
844         name = nla[NFTA_CHAIN_NAME];
845
846         if (nla[NFTA_CHAIN_HANDLE]) {
847                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
848                 chain = nf_tables_chain_lookup_byhandle(table, handle);
849                 if (IS_ERR(chain))
850                         return PTR_ERR(chain);
851         } else {
852                 chain = nf_tables_chain_lookup(table, name);
853                 if (IS_ERR(chain)) {
854                         if (PTR_ERR(chain) != -ENOENT)
855                                 return PTR_ERR(chain);
856                         chain = NULL;
857                 }
858         }
859
860         if (chain != NULL) {
861                 if (nlh->nlmsg_flags & NLM_F_EXCL)
862                         return -EEXIST;
863                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
864                         return -EOPNOTSUPP;
865
866                 if (nla[NFTA_CHAIN_HANDLE] && name &&
867                     !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
868                         return -EEXIST;
869
870                 if (nla[NFTA_CHAIN_POLICY]) {
871                         if (!(chain->flags & NFT_BASE_CHAIN))
872                                 return -EOPNOTSUPP;
873
874                         err = nf_tables_chain_policy(nft_base_chain(chain),
875                                                      nla[NFTA_CHAIN_POLICY]);
876                         if (err < 0)
877                                 return err;
878                 }
879
880                 if (nla[NFTA_CHAIN_COUNTERS]) {
881                         if (!(chain->flags & NFT_BASE_CHAIN))
882                                 return -EOPNOTSUPP;
883
884                         err = nf_tables_counters(nft_base_chain(chain),
885                                                  nla[NFTA_CHAIN_COUNTERS]);
886                         if (err < 0)
887                                 return err;
888                 }
889
890                 if (nla[NFTA_CHAIN_HANDLE] && name)
891                         nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
892
893                 goto notify;
894         }
895
896         if (nla[NFTA_CHAIN_HOOK]) {
897                 struct nf_hook_ops *ops;
898                 nf_hookfn *hookfn;
899                 u32 hooknum;
900                 int type = NFT_CHAIN_T_DEFAULT;
901
902                 if (nla[NFTA_CHAIN_TYPE]) {
903                         type = nf_tables_chain_type_lookup(afi,
904                                                            nla[NFTA_CHAIN_TYPE],
905                                                            create);
906                         if (type < 0)
907                                 return -ENOENT;
908                 }
909
910                 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
911                                        nft_hook_policy);
912                 if (err < 0)
913                         return err;
914                 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
915                     ha[NFTA_HOOK_PRIORITY] == NULL)
916                         return -EINVAL;
917
918                 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
919                 if (hooknum >= afi->nhooks)
920                         return -EINVAL;
921
922                 hookfn = chain_type[family][type]->fn[hooknum];
923                 if (hookfn == NULL)
924                         return -EOPNOTSUPP;
925
926                 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
927                 if (basechain == NULL)
928                         return -ENOMEM;
929
930                 basechain->type = type;
931                 chain = &basechain->chain;
932
933                 ops = &basechain->ops;
934                 ops->pf         = family;
935                 ops->owner      = afi->owner;
936                 ops->hooknum    = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
937                 ops->priority   = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
938                 ops->priv       = chain;
939                 ops->hook       = hookfn;
940                 if (afi->hooks[ops->hooknum])
941                         ops->hook = afi->hooks[ops->hooknum];
942
943                 chain->flags |= NFT_BASE_CHAIN;
944
945                 if (nla[NFTA_CHAIN_POLICY]) {
946                         err = nf_tables_chain_policy(basechain,
947                                                      nla[NFTA_CHAIN_POLICY]);
948                         if (err < 0) {
949                                 free_percpu(basechain->stats);
950                                 kfree(basechain);
951                                 return err;
952                         }
953                 } else
954                         basechain->policy = NF_ACCEPT;
955
956                 if (nla[NFTA_CHAIN_COUNTERS]) {
957                         err = nf_tables_counters(basechain,
958                                                  nla[NFTA_CHAIN_COUNTERS]);
959                         if (err < 0) {
960                                 free_percpu(basechain->stats);
961                                 kfree(basechain);
962                                 return err;
963                         }
964                 } else {
965                         struct nft_stats __percpu *newstats;
966
967                         newstats = alloc_percpu(struct nft_stats);
968                         if (newstats == NULL)
969                                 return -ENOMEM;
970
971                         rcu_assign_pointer(nft_base_chain(chain)->stats,
972                                            newstats);
973                 }
974         } else {
975                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
976                 if (chain == NULL)
977                         return -ENOMEM;
978         }
979
980         INIT_LIST_HEAD(&chain->rules);
981         chain->handle = nf_tables_alloc_handle(table);
982         chain->net = net;
983         chain->table = table;
984         nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
985
986         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
987             chain->flags & NFT_BASE_CHAIN) {
988                 err = nf_register_hook(&nft_base_chain(chain)->ops);
989                 if (err < 0) {
990                         free_percpu(basechain->stats);
991                         kfree(basechain);
992                         return err;
993                 }
994         }
995         list_add_tail(&chain->list, &table->chains);
996         table->use++;
997 notify:
998         nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
999                                family);
1000         return 0;
1001 }
1002
1003 static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1004 {
1005         struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1006
1007         BUG_ON(chain->use > 0);
1008
1009         if (chain->flags & NFT_BASE_CHAIN) {
1010                 free_percpu(nft_base_chain(chain)->stats);
1011                 kfree(nft_base_chain(chain));
1012         } else
1013                 kfree(chain);
1014 }
1015
1016 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1017                               const struct nlmsghdr *nlh,
1018                               const struct nlattr * const nla[])
1019 {
1020         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1021         const struct nft_af_info *afi;
1022         struct nft_table *table;
1023         struct nft_chain *chain;
1024         struct net *net = sock_net(skb->sk);
1025         int family = nfmsg->nfgen_family;
1026
1027         afi = nf_tables_afinfo_lookup(net, family, false);
1028         if (IS_ERR(afi))
1029                 return PTR_ERR(afi);
1030
1031         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1032         if (IS_ERR(table))
1033                 return PTR_ERR(table);
1034
1035         chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1036         if (IS_ERR(chain))
1037                 return PTR_ERR(chain);
1038
1039         if (!list_empty(&chain->rules))
1040                 return -EBUSY;
1041
1042         list_del(&chain->list);
1043         table->use--;
1044
1045         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1046             chain->flags & NFT_BASE_CHAIN)
1047                 nf_unregister_hook(&nft_base_chain(chain)->ops);
1048
1049         nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1050                                family);
1051
1052         /* Make sure all rule references are gone before this is released */
1053         call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1054         return 0;
1055 }
1056
1057 static void nft_ctx_init(struct nft_ctx *ctx,
1058                          const struct sk_buff *skb,
1059                          const struct nlmsghdr *nlh,
1060                          const struct nft_af_info *afi,
1061                          const struct nft_table *table,
1062                          const struct nft_chain *chain,
1063                          const struct nlattr * const *nla)
1064 {
1065         ctx->net   = sock_net(skb->sk);
1066         ctx->skb   = skb;
1067         ctx->nlh   = nlh;
1068         ctx->afi   = afi;
1069         ctx->table = table;
1070         ctx->chain = chain;
1071         ctx->nla   = nla;
1072 }
1073
1074 /*
1075  * Expressions
1076  */
1077
1078 /**
1079  *      nft_register_expr - register nf_tables expr type
1080  *      @ops: expr type
1081  *
1082  *      Registers the expr type for use with nf_tables. Returns zero on
1083  *      success or a negative errno code otherwise.
1084  */
1085 int nft_register_expr(struct nft_expr_type *type)
1086 {
1087         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1088         list_add_tail(&type->list, &nf_tables_expressions);
1089         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1090         return 0;
1091 }
1092 EXPORT_SYMBOL_GPL(nft_register_expr);
1093
1094 /**
1095  *      nft_unregister_expr - unregister nf_tables expr type
1096  *      @ops: expr type
1097  *
1098  *      Unregisters the expr typefor use with nf_tables.
1099  */
1100 void nft_unregister_expr(struct nft_expr_type *type)
1101 {
1102         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1103         list_del(&type->list);
1104         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1105 }
1106 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1107
1108 static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
1109 {
1110         const struct nft_expr_type *type;
1111
1112         list_for_each_entry(type, &nf_tables_expressions, list) {
1113                 if (!nla_strcmp(nla, type->name))
1114                         return type;
1115         }
1116         return NULL;
1117 }
1118
1119 static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
1120 {
1121         const struct nft_expr_type *type;
1122
1123         if (nla == NULL)
1124                 return ERR_PTR(-EINVAL);
1125
1126         type = __nft_expr_type_get(nla);
1127         if (type != NULL && try_module_get(type->owner))
1128                 return type;
1129
1130 #ifdef CONFIG_MODULES
1131         if (type == NULL) {
1132                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1133                 request_module("nft-expr-%.*s",
1134                                nla_len(nla), (char *)nla_data(nla));
1135                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1136                 if (__nft_expr_type_get(nla))
1137                         return ERR_PTR(-EAGAIN);
1138         }
1139 #endif
1140         return ERR_PTR(-ENOENT);
1141 }
1142
1143 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1144         [NFTA_EXPR_NAME]        = { .type = NLA_STRING },
1145         [NFTA_EXPR_DATA]        = { .type = NLA_NESTED },
1146 };
1147
1148 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1149                                     const struct nft_expr *expr)
1150 {
1151         if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1152                 goto nla_put_failure;
1153
1154         if (expr->ops->dump) {
1155                 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1156                 if (data == NULL)
1157                         goto nla_put_failure;
1158                 if (expr->ops->dump(skb, expr) < 0)
1159                         goto nla_put_failure;
1160                 nla_nest_end(skb, data);
1161         }
1162
1163         return skb->len;
1164
1165 nla_put_failure:
1166         return -1;
1167 };
1168
1169 struct nft_expr_info {
1170         const struct nft_expr_ops       *ops;
1171         struct nlattr                   *tb[NFT_EXPR_MAXATTR + 1];
1172 };
1173
1174 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1175                                 const struct nlattr *nla,
1176                                 struct nft_expr_info *info)
1177 {
1178         const struct nft_expr_type *type;
1179         const struct nft_expr_ops *ops;
1180         struct nlattr *tb[NFTA_EXPR_MAX + 1];
1181         int err;
1182
1183         err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1184         if (err < 0)
1185                 return err;
1186
1187         type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1188         if (IS_ERR(type))
1189                 return PTR_ERR(type);
1190
1191         if (tb[NFTA_EXPR_DATA]) {
1192                 err = nla_parse_nested(info->tb, type->maxattr,
1193                                        tb[NFTA_EXPR_DATA], type->policy);
1194                 if (err < 0)
1195                         goto err1;
1196         } else
1197                 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1198
1199         if (type->select_ops != NULL) {
1200                 ops = type->select_ops(ctx,
1201                                        (const struct nlattr * const *)info->tb);
1202                 if (IS_ERR(ops)) {
1203                         err = PTR_ERR(ops);
1204                         goto err1;
1205                 }
1206         } else
1207                 ops = type->ops;
1208
1209         info->ops = ops;
1210         return 0;
1211
1212 err1:
1213         module_put(type->owner);
1214         return err;
1215 }
1216
1217 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1218                              const struct nft_expr_info *info,
1219                              struct nft_expr *expr)
1220 {
1221         const struct nft_expr_ops *ops = info->ops;
1222         int err;
1223
1224         expr->ops = ops;
1225         if (ops->init) {
1226                 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1227                 if (err < 0)
1228                         goto err1;
1229         }
1230
1231         return 0;
1232
1233 err1:
1234         expr->ops = NULL;
1235         return err;
1236 }
1237
1238 static void nf_tables_expr_destroy(struct nft_expr *expr)
1239 {
1240         if (expr->ops->destroy)
1241                 expr->ops->destroy(expr);
1242         module_put(expr->ops->type->owner);
1243 }
1244
1245 /*
1246  * Rules
1247  */
1248
1249 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1250                                                 u64 handle)
1251 {
1252         struct nft_rule *rule;
1253
1254         // FIXME: this sucks
1255         list_for_each_entry(rule, &chain->rules, list) {
1256                 if (handle == rule->handle)
1257                         return rule;
1258         }
1259
1260         return ERR_PTR(-ENOENT);
1261 }
1262
1263 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1264                                               const struct nlattr *nla)
1265 {
1266         if (nla == NULL)
1267                 return ERR_PTR(-EINVAL);
1268
1269         return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1270 }
1271
1272 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1273         [NFTA_RULE_TABLE]       = { .type = NLA_STRING },
1274         [NFTA_RULE_CHAIN]       = { .type = NLA_STRING,
1275                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
1276         [NFTA_RULE_HANDLE]      = { .type = NLA_U64 },
1277         [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1278         [NFTA_RULE_COMPAT]      = { .type = NLA_NESTED },
1279         [NFTA_RULE_POSITION]    = { .type = NLA_U64 },
1280 };
1281
1282 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1283                                     int event, u32 flags, int family,
1284                                     const struct nft_table *table,
1285                                     const struct nft_chain *chain,
1286                                     const struct nft_rule *rule)
1287 {
1288         struct nlmsghdr *nlh;
1289         struct nfgenmsg *nfmsg;
1290         const struct nft_expr *expr, *next;
1291         struct nlattr *list;
1292         const struct nft_rule *prule;
1293         int type = event | NFNL_SUBSYS_NFTABLES << 8;
1294
1295         nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1296                         flags);
1297         if (nlh == NULL)
1298                 goto nla_put_failure;
1299
1300         nfmsg = nlmsg_data(nlh);
1301         nfmsg->nfgen_family     = family;
1302         nfmsg->version          = NFNETLINK_V0;
1303         nfmsg->res_id           = 0;
1304
1305         if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1306                 goto nla_put_failure;
1307         if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1308                 goto nla_put_failure;
1309         if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1310                 goto nla_put_failure;
1311
1312         if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1313                 prule = list_entry(rule->list.prev, struct nft_rule, list);
1314                 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1315                                  cpu_to_be64(prule->handle)))
1316                         goto nla_put_failure;
1317         }
1318
1319         list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1320         if (list == NULL)
1321                 goto nla_put_failure;
1322         nft_rule_for_each_expr(expr, next, rule) {
1323                 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1324                 if (elem == NULL)
1325                         goto nla_put_failure;
1326                 if (nf_tables_fill_expr_info(skb, expr) < 0)
1327                         goto nla_put_failure;
1328                 nla_nest_end(skb, elem);
1329         }
1330         nla_nest_end(skb, list);
1331
1332         return nlmsg_end(skb, nlh);
1333
1334 nla_put_failure:
1335         nlmsg_trim(skb, nlh);
1336         return -1;
1337 }
1338
1339 static int nf_tables_rule_notify(const struct sk_buff *oskb,
1340                                  const struct nlmsghdr *nlh,
1341                                  const struct nft_table *table,
1342                                  const struct nft_chain *chain,
1343                                  const struct nft_rule *rule,
1344                                  int event, u32 flags, int family)
1345 {
1346         struct sk_buff *skb;
1347         u32 portid = NETLINK_CB(oskb).portid;
1348         struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1349         u32 seq = nlh->nlmsg_seq;
1350         bool report;
1351         int err;
1352
1353         report = nlmsg_report(nlh);
1354         if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1355                 return 0;
1356
1357         err = -ENOBUFS;
1358         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1359         if (skb == NULL)
1360                 goto err;
1361
1362         err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1363                                        family, table, chain, rule);
1364         if (err < 0) {
1365                 kfree_skb(skb);
1366                 goto err;
1367         }
1368
1369         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1370                              GFP_KERNEL);
1371 err:
1372         if (err < 0)
1373                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1374         return err;
1375 }
1376
1377 static inline bool
1378 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1379 {
1380         return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1381 }
1382
1383 static inline int gencursor_next(struct net *net)
1384 {
1385         return net->nft.gencursor+1 == 1 ? 1 : 0;
1386 }
1387
1388 static inline int
1389 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1390 {
1391         return (rule->genmask & (1 << gencursor_next(net))) == 0;
1392 }
1393
1394 static inline void
1395 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1396 {
1397         /* Now inactive, will be active in the future */
1398         rule->genmask = (1 << net->nft.gencursor);
1399 }
1400
1401 static inline void
1402 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1403 {
1404         rule->genmask = (1 << gencursor_next(net));
1405 }
1406
1407 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1408 {
1409         rule->genmask = 0;
1410 }
1411
1412 static int nf_tables_dump_rules(struct sk_buff *skb,
1413                                 struct netlink_callback *cb)
1414 {
1415         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1416         const struct nft_af_info *afi;
1417         const struct nft_table *table;
1418         const struct nft_chain *chain;
1419         const struct nft_rule *rule;
1420         unsigned int idx = 0, s_idx = cb->args[0];
1421         struct net *net = sock_net(skb->sk);
1422         int family = nfmsg->nfgen_family;
1423         u8 genctr = ACCESS_ONCE(net->nft.genctr);
1424         u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1425
1426         list_for_each_entry(afi, &net->nft.af_info, list) {
1427                 if (family != NFPROTO_UNSPEC && family != afi->family)
1428                         continue;
1429
1430                 list_for_each_entry(table, &afi->tables, list) {
1431                         list_for_each_entry(chain, &table->chains, list) {
1432                                 list_for_each_entry(rule, &chain->rules, list) {
1433                                         if (!nft_rule_is_active(net, rule))
1434                                                 goto cont;
1435                                         if (idx < s_idx)
1436                                                 goto cont;
1437                                         if (idx > s_idx)
1438                                                 memset(&cb->args[1], 0,
1439                                                        sizeof(cb->args) - sizeof(cb->args[0]));
1440                                         if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1441                                                                       cb->nlh->nlmsg_seq,
1442                                                                       NFT_MSG_NEWRULE,
1443                                                                       NLM_F_MULTI | NLM_F_APPEND,
1444                                                                       afi->family, table, chain, rule) < 0)
1445                                                 goto done;
1446 cont:
1447                                         idx++;
1448                                 }
1449                         }
1450                 }
1451         }
1452 done:
1453         /* Invalidate this dump, a transition to the new generation happened */
1454         if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1455                 return -EBUSY;
1456
1457         cb->args[0] = idx;
1458         return skb->len;
1459 }
1460
1461 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1462                              const struct nlmsghdr *nlh,
1463                              const struct nlattr * const nla[])
1464 {
1465         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1466         const struct nft_af_info *afi;
1467         const struct nft_table *table;
1468         const struct nft_chain *chain;
1469         const struct nft_rule *rule;
1470         struct sk_buff *skb2;
1471         struct net *net = sock_net(skb->sk);
1472         int family = nfmsg->nfgen_family;
1473         int err;
1474
1475         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1476                 struct netlink_dump_control c = {
1477                         .dump = nf_tables_dump_rules,
1478                 };
1479                 return netlink_dump_start(nlsk, skb, nlh, &c);
1480         }
1481
1482         afi = nf_tables_afinfo_lookup(net, family, false);
1483         if (IS_ERR(afi))
1484                 return PTR_ERR(afi);
1485
1486         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1487         if (IS_ERR(table))
1488                 return PTR_ERR(table);
1489
1490         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1491         if (IS_ERR(chain))
1492                 return PTR_ERR(chain);
1493
1494         rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1495         if (IS_ERR(rule))
1496                 return PTR_ERR(rule);
1497
1498         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1499         if (!skb2)
1500                 return -ENOMEM;
1501
1502         err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1503                                        nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1504                                        family, table, chain, rule);
1505         if (err < 0)
1506                 goto err;
1507
1508         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1509
1510 err:
1511         kfree_skb(skb2);
1512         return err;
1513 }
1514
1515 static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1516 {
1517         struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1518         struct nft_expr *expr;
1519
1520         /*
1521          * Careful: some expressions might not be initialized in case this
1522          * is called on error from nf_tables_newrule().
1523          */
1524         expr = nft_expr_first(rule);
1525         while (expr->ops && expr != nft_expr_last(rule)) {
1526                 nf_tables_expr_destroy(expr);
1527                 expr = nft_expr_next(expr);
1528         }
1529         kfree(rule);
1530 }
1531
1532 static void nf_tables_rule_destroy(struct nft_rule *rule)
1533 {
1534         call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1535 }
1536
1537 #define NFT_RULE_MAXEXPRS       128
1538
1539 static struct nft_expr_info *info;
1540
1541 static struct nft_rule_trans *
1542 nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1543 {
1544         struct nft_rule_trans *rupd;
1545
1546         rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1547         if (rupd == NULL)
1548                return NULL;
1549
1550         rupd->chain = ctx->chain;
1551         rupd->table = ctx->table;
1552         rupd->rule = rule;
1553         rupd->family = ctx->afi->family;
1554         rupd->nlh = ctx->nlh;
1555         list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1556
1557         return rupd;
1558 }
1559
1560 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1561                              const struct nlmsghdr *nlh,
1562                              const struct nlattr * const nla[])
1563 {
1564         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1565         const struct nft_af_info *afi;
1566         struct net *net = sock_net(skb->sk);
1567         struct nft_table *table;
1568         struct nft_chain *chain;
1569         struct nft_rule *rule, *old_rule = NULL;
1570         struct nft_rule_trans *repl = NULL;
1571         struct nft_expr *expr;
1572         struct nft_ctx ctx;
1573         struct nlattr *tmp;
1574         unsigned int size, i, n;
1575         int err, rem;
1576         bool create;
1577         u64 handle, pos_handle;
1578
1579         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1580
1581         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1582         if (IS_ERR(afi))
1583                 return PTR_ERR(afi);
1584
1585         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1586         if (IS_ERR(table))
1587                 return PTR_ERR(table);
1588
1589         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1590         if (IS_ERR(chain))
1591                 return PTR_ERR(chain);
1592
1593         if (nla[NFTA_RULE_HANDLE]) {
1594                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1595                 rule = __nf_tables_rule_lookup(chain, handle);
1596                 if (IS_ERR(rule))
1597                         return PTR_ERR(rule);
1598
1599                 if (nlh->nlmsg_flags & NLM_F_EXCL)
1600                         return -EEXIST;
1601                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1602                         old_rule = rule;
1603                 else
1604                         return -EOPNOTSUPP;
1605         } else {
1606                 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1607                         return -EINVAL;
1608                 handle = nf_tables_alloc_handle(table);
1609         }
1610
1611         if (nla[NFTA_RULE_POSITION]) {
1612                 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1613                         return -EOPNOTSUPP;
1614
1615                 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1616                 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1617                 if (IS_ERR(old_rule))
1618                         return PTR_ERR(old_rule);
1619         }
1620
1621         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1622
1623         n = 0;
1624         size = 0;
1625         if (nla[NFTA_RULE_EXPRESSIONS]) {
1626                 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1627                         err = -EINVAL;
1628                         if (nla_type(tmp) != NFTA_LIST_ELEM)
1629                                 goto err1;
1630                         if (n == NFT_RULE_MAXEXPRS)
1631                                 goto err1;
1632                         err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1633                         if (err < 0)
1634                                 goto err1;
1635                         size += info[n].ops->size;
1636                         n++;
1637                 }
1638         }
1639
1640         err = -ENOMEM;
1641         rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1642         if (rule == NULL)
1643                 goto err1;
1644
1645         nft_rule_activate_next(net, rule);
1646
1647         rule->handle = handle;
1648         rule->dlen   = size;
1649
1650         expr = nft_expr_first(rule);
1651         for (i = 0; i < n; i++) {
1652                 err = nf_tables_newexpr(&ctx, &info[i], expr);
1653                 if (err < 0)
1654                         goto err2;
1655                 info[i].ops = NULL;
1656                 expr = nft_expr_next(expr);
1657         }
1658
1659         if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1660                 if (nft_rule_is_active_next(net, old_rule)) {
1661                         repl = nf_tables_trans_add(old_rule, &ctx);
1662                         if (repl == NULL) {
1663                                 err = -ENOMEM;
1664                                 goto err2;
1665                         }
1666                         nft_rule_disactivate_next(net, old_rule);
1667                         list_add_tail(&rule->list, &old_rule->list);
1668                 } else {
1669                         err = -ENOENT;
1670                         goto err2;
1671                 }
1672         } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1673                 if (old_rule)
1674                         list_add_rcu(&rule->list, &old_rule->list);
1675                 else
1676                         list_add_tail_rcu(&rule->list, &chain->rules);
1677         else {
1678                 if (old_rule)
1679                         list_add_tail_rcu(&rule->list, &old_rule->list);
1680                 else
1681                         list_add_rcu(&rule->list, &chain->rules);
1682         }
1683
1684         if (nf_tables_trans_add(rule, &ctx) == NULL) {
1685                 err = -ENOMEM;
1686                 goto err3;
1687         }
1688         return 0;
1689
1690 err3:
1691         list_del_rcu(&rule->list);
1692         if (repl) {
1693                 list_del_rcu(&repl->rule->list);
1694                 list_del(&repl->list);
1695                 nft_rule_clear(net, repl->rule);
1696                 kfree(repl);
1697         }
1698 err2:
1699         nf_tables_rule_destroy(rule);
1700 err1:
1701         for (i = 0; i < n; i++) {
1702                 if (info[i].ops != NULL)
1703                         module_put(info[i].ops->type->owner);
1704         }
1705         return err;
1706 }
1707
1708 static int
1709 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1710 {
1711         /* You cannot delete the same rule twice */
1712         if (nft_rule_is_active_next(ctx->net, rule)) {
1713                 if (nf_tables_trans_add(rule, ctx) == NULL)
1714                         return -ENOMEM;
1715                 nft_rule_disactivate_next(ctx->net, rule);
1716                 return 0;
1717         }
1718         return -ENOENT;
1719 }
1720
1721 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1722                              const struct nlmsghdr *nlh,
1723                              const struct nlattr * const nla[])
1724 {
1725         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1726         const struct nft_af_info *afi;
1727         struct net *net = sock_net(skb->sk);
1728         const struct nft_table *table;
1729         struct nft_chain *chain;
1730         struct nft_rule *rule, *tmp;
1731         int family = nfmsg->nfgen_family, err = 0;
1732         struct nft_ctx ctx;
1733
1734         afi = nf_tables_afinfo_lookup(net, family, false);
1735         if (IS_ERR(afi))
1736                 return PTR_ERR(afi);
1737
1738         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1739         if (IS_ERR(table))
1740                 return PTR_ERR(table);
1741
1742         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1743         if (IS_ERR(chain))
1744                 return PTR_ERR(chain);
1745
1746         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1747
1748         if (nla[NFTA_RULE_HANDLE]) {
1749                 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1750                 if (IS_ERR(rule))
1751                         return PTR_ERR(rule);
1752
1753                 err = nf_tables_delrule_one(&ctx, rule);
1754         } else {
1755                 /* Remove all rules in this chain */
1756                 list_for_each_entry_safe(rule, tmp, &chain->rules, list) {
1757                         err = nf_tables_delrule_one(&ctx, rule);
1758                         if (err < 0)
1759                                 break;
1760                 }
1761         }
1762
1763         return err;
1764 }
1765
1766 static int nf_tables_commit(struct sk_buff *skb)
1767 {
1768         struct net *net = sock_net(skb->sk);
1769         struct nft_rule_trans *rupd, *tmp;
1770
1771         /* Bump generation counter, invalidate any dump in progress */
1772         net->nft.genctr++;
1773
1774         /* A new generation has just started */
1775         net->nft.gencursor = gencursor_next(net);
1776
1777         /* Make sure all packets have left the previous generation before
1778          * purging old rules.
1779          */
1780         synchronize_rcu();
1781
1782         list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1783                 /* Delete this rule from the dirty list */
1784                 list_del(&rupd->list);
1785
1786                 /* This rule was inactive in the past and just became active.
1787                  * Clear the next bit of the genmask since its meaning has
1788                  * changed, now it is the future.
1789                  */
1790                 if (nft_rule_is_active(net, rupd->rule)) {
1791                         nft_rule_clear(net, rupd->rule);
1792                         nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1793                                               rupd->chain, rupd->rule,
1794                                               NFT_MSG_NEWRULE, 0,
1795                                               rupd->family);
1796                         kfree(rupd);
1797                         continue;
1798                 }
1799
1800                 /* This rule is in the past, get rid of it */
1801                 list_del_rcu(&rupd->rule->list);
1802                 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1803                                       rupd->rule, NFT_MSG_DELRULE, 0,
1804                                       rupd->family);
1805                 nf_tables_rule_destroy(rupd->rule);
1806                 kfree(rupd);
1807         }
1808
1809         return 0;
1810 }
1811
1812 static int nf_tables_abort(struct sk_buff *skb)
1813 {
1814         struct net *net = sock_net(skb->sk);
1815         struct nft_rule_trans *rupd, *tmp;
1816
1817         list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1818                 /* Delete all rules from the dirty list */
1819                 list_del(&rupd->list);
1820
1821                 if (!nft_rule_is_active_next(net, rupd->rule)) {
1822                         nft_rule_clear(net, rupd->rule);
1823                         kfree(rupd);
1824                         continue;
1825                 }
1826
1827                 /* This rule is inactive, get rid of it */
1828                 list_del_rcu(&rupd->rule->list);
1829                 nf_tables_rule_destroy(rupd->rule);
1830                 kfree(rupd);
1831         }
1832         return 0;
1833 }
1834
1835 /*
1836  * Sets
1837  */
1838
1839 static LIST_HEAD(nf_tables_set_ops);
1840
1841 int nft_register_set(struct nft_set_ops *ops)
1842 {
1843         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1844         list_add_tail(&ops->list, &nf_tables_set_ops);
1845         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1846         return 0;
1847 }
1848 EXPORT_SYMBOL_GPL(nft_register_set);
1849
1850 void nft_unregister_set(struct nft_set_ops *ops)
1851 {
1852         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1853         list_del(&ops->list);
1854         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1855 }
1856 EXPORT_SYMBOL_GPL(nft_unregister_set);
1857
1858 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1859 {
1860         const struct nft_set_ops *ops;
1861         u32 features;
1862
1863 #ifdef CONFIG_MODULES
1864         if (list_empty(&nf_tables_set_ops)) {
1865                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1866                 request_module("nft-set");
1867                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1868                 if (!list_empty(&nf_tables_set_ops))
1869                         return ERR_PTR(-EAGAIN);
1870         }
1871 #endif
1872         features = 0;
1873         if (nla[NFTA_SET_FLAGS] != NULL) {
1874                 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1875                 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1876         }
1877
1878         // FIXME: implement selection properly
1879         list_for_each_entry(ops, &nf_tables_set_ops, list) {
1880                 if ((ops->features & features) != features)
1881                         continue;
1882                 if (!try_module_get(ops->owner))
1883                         continue;
1884                 return ops;
1885         }
1886
1887         return ERR_PTR(-EOPNOTSUPP);
1888 }
1889
1890 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1891         [NFTA_SET_TABLE]                = { .type = NLA_STRING },
1892         [NFTA_SET_NAME]                 = { .type = NLA_STRING },
1893         [NFTA_SET_FLAGS]                = { .type = NLA_U32 },
1894         [NFTA_SET_KEY_TYPE]             = { .type = NLA_U32 },
1895         [NFTA_SET_KEY_LEN]              = { .type = NLA_U32 },
1896         [NFTA_SET_DATA_TYPE]            = { .type = NLA_U32 },
1897         [NFTA_SET_DATA_LEN]             = { .type = NLA_U32 },
1898 };
1899
1900 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1901                                      const struct sk_buff *skb,
1902                                      const struct nlmsghdr *nlh,
1903                                      const struct nlattr * const nla[])
1904 {
1905         struct net *net = sock_net(skb->sk);
1906         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1907         const struct nft_af_info *afi;
1908         const struct nft_table *table = NULL;
1909
1910         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1911         if (IS_ERR(afi))
1912                 return PTR_ERR(afi);
1913
1914         if (nla[NFTA_SET_TABLE] != NULL) {
1915                 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1916                 if (IS_ERR(table))
1917                         return PTR_ERR(table);
1918         }
1919
1920         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1921         return 0;
1922 }
1923
1924 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1925                                      const struct nlattr *nla)
1926 {
1927         struct nft_set *set;
1928
1929         if (nla == NULL)
1930                 return ERR_PTR(-EINVAL);
1931
1932         list_for_each_entry(set, &table->sets, list) {
1933                 if (!nla_strcmp(nla, set->name))
1934                         return set;
1935         }
1936         return ERR_PTR(-ENOENT);
1937 }
1938
1939 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1940                                     const char *name)
1941 {
1942         const struct nft_set *i;
1943         const char *p;
1944         unsigned long *inuse;
1945         unsigned int n = 0;
1946
1947         p = strnchr(name, IFNAMSIZ, '%');
1948         if (p != NULL) {
1949                 if (p[1] != 'd' || strchr(p + 2, '%'))
1950                         return -EINVAL;
1951
1952                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1953                 if (inuse == NULL)
1954                         return -ENOMEM;
1955
1956                 list_for_each_entry(i, &ctx->table->sets, list) {
1957                         int tmp;
1958
1959                         if (!sscanf(i->name, name, &tmp))
1960                                 continue;
1961                         if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
1962                                 continue;
1963
1964                         set_bit(tmp, inuse);
1965                 }
1966
1967                 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1968                 free_page((unsigned long)inuse);
1969         }
1970
1971         snprintf(set->name, sizeof(set->name), name, n);
1972         list_for_each_entry(i, &ctx->table->sets, list) {
1973                 if (!strcmp(set->name, i->name))
1974                         return -ENFILE;
1975         }
1976         return 0;
1977 }
1978
1979 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
1980                               const struct nft_set *set, u16 event, u16 flags)
1981 {
1982         struct nfgenmsg *nfmsg;
1983         struct nlmsghdr *nlh;
1984         u32 portid = NETLINK_CB(ctx->skb).portid;
1985         u32 seq = ctx->nlh->nlmsg_seq;
1986
1987         event |= NFNL_SUBSYS_NFTABLES << 8;
1988         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1989                         flags);
1990         if (nlh == NULL)
1991                 goto nla_put_failure;
1992
1993         nfmsg = nlmsg_data(nlh);
1994         nfmsg->nfgen_family     = ctx->afi->family;
1995         nfmsg->version          = NFNETLINK_V0;
1996         nfmsg->res_id           = 0;
1997
1998         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
1999                 goto nla_put_failure;
2000         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2001                 goto nla_put_failure;
2002         if (set->flags != 0)
2003                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2004                         goto nla_put_failure;
2005
2006         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2007                 goto nla_put_failure;
2008         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2009                 goto nla_put_failure;
2010         if (set->flags & NFT_SET_MAP) {
2011                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2012                         goto nla_put_failure;
2013                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2014                         goto nla_put_failure;
2015         }
2016
2017         return nlmsg_end(skb, nlh);
2018
2019 nla_put_failure:
2020         nlmsg_trim(skb, nlh);
2021         return -1;
2022 }
2023
2024 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2025                                 const struct nft_set *set,
2026                                 int event)
2027 {
2028         struct sk_buff *skb;
2029         u32 portid = NETLINK_CB(ctx->skb).portid;
2030         bool report;
2031         int err;
2032
2033         report = nlmsg_report(ctx->nlh);
2034         if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2035                 return 0;
2036
2037         err = -ENOBUFS;
2038         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2039         if (skb == NULL)
2040                 goto err;
2041
2042         err = nf_tables_fill_set(skb, ctx, set, event, 0);
2043         if (err < 0) {
2044                 kfree_skb(skb);
2045                 goto err;
2046         }
2047
2048         err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2049                              GFP_KERNEL);
2050 err:
2051         if (err < 0)
2052                 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2053         return err;
2054 }
2055
2056 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2057                                      struct netlink_callback *cb)
2058 {
2059         const struct nft_set *set;
2060         unsigned int idx = 0, s_idx = cb->args[0];
2061
2062         if (cb->args[1])
2063                 return skb->len;
2064
2065         list_for_each_entry(set, &ctx->table->sets, list) {
2066                 if (idx < s_idx)
2067                         goto cont;
2068                 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2069                                        NLM_F_MULTI) < 0) {
2070                         cb->args[0] = idx;
2071                         goto done;
2072                 }
2073 cont:
2074                 idx++;
2075         }
2076         cb->args[1] = 1;
2077 done:
2078         return skb->len;
2079 }
2080
2081 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2082                                    struct netlink_callback *cb)
2083 {
2084         const struct nft_set *set;
2085         unsigned int idx = 0, s_idx = cb->args[0];
2086         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2087
2088         if (cb->args[1])
2089                 return skb->len;
2090
2091         list_for_each_entry(table, &ctx->afi->tables, list) {
2092                 if (cur_table && cur_table != table)
2093                         continue;
2094
2095                 ctx->table = table;
2096                 list_for_each_entry(set, &ctx->table->sets, list) {
2097                         if (idx < s_idx)
2098                                 goto cont;
2099                         if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2100                                                NLM_F_MULTI) < 0) {
2101                                 cb->args[0] = idx;
2102                                 cb->args[2] = (unsigned long) table;
2103                                 goto done;
2104                         }
2105 cont:
2106                         idx++;
2107                 }
2108         }
2109         cb->args[1] = 1;
2110 done:
2111         return skb->len;
2112 }
2113
2114 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2115 {
2116         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2117         struct nlattr *nla[NFTA_SET_MAX + 1];
2118         struct nft_ctx ctx;
2119         int err, ret;
2120
2121         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2122                           nft_set_policy);
2123         if (err < 0)
2124                 return err;
2125
2126         err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2127         if (err < 0)
2128                 return err;
2129
2130         if (ctx.table == NULL)
2131                 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2132         else
2133                 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2134
2135         return ret;
2136 }
2137
2138 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2139                             const struct nlmsghdr *nlh,
2140                             const struct nlattr * const nla[])
2141 {
2142         const struct nft_set *set;
2143         struct nft_ctx ctx;
2144         struct sk_buff *skb2;
2145         int err;
2146
2147         /* Verify existance before starting dump */
2148         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2149         if (err < 0)
2150                 return err;
2151
2152         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2153                 struct netlink_dump_control c = {
2154                         .dump = nf_tables_dump_sets,
2155                 };
2156                 return netlink_dump_start(nlsk, skb, nlh, &c);
2157         }
2158
2159         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2160         if (IS_ERR(set))
2161                 return PTR_ERR(set);
2162
2163         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2164         if (skb2 == NULL)
2165                 return -ENOMEM;
2166
2167         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2168         if (err < 0)
2169                 goto err;
2170
2171         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2172
2173 err:
2174         kfree_skb(skb2);
2175         return err;
2176 }
2177
2178 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2179                             const struct nlmsghdr *nlh,
2180                             const struct nlattr * const nla[])
2181 {
2182         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2183         const struct nft_set_ops *ops;
2184         const struct nft_af_info *afi;
2185         struct net *net = sock_net(skb->sk);
2186         struct nft_table *table;
2187         struct nft_set *set;
2188         struct nft_ctx ctx;
2189         char name[IFNAMSIZ];
2190         unsigned int size;
2191         bool create;
2192         u32 ktype, klen, dlen, dtype, flags;
2193         int err;
2194
2195         if (nla[NFTA_SET_TABLE] == NULL ||
2196             nla[NFTA_SET_NAME] == NULL ||
2197             nla[NFTA_SET_KEY_LEN] == NULL)
2198                 return -EINVAL;
2199
2200         ktype = NFT_DATA_VALUE;
2201         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2202                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2203                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2204                         return -EINVAL;
2205         }
2206
2207         klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2208         if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2209                 return -EINVAL;
2210
2211         flags = 0;
2212         if (nla[NFTA_SET_FLAGS] != NULL) {
2213                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2214                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2215                               NFT_SET_INTERVAL | NFT_SET_MAP))
2216                         return -EINVAL;
2217         }
2218
2219         dtype = 0;
2220         dlen  = 0;
2221         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2222                 if (!(flags & NFT_SET_MAP))
2223                         return -EINVAL;
2224
2225                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2226                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2227                     dtype != NFT_DATA_VERDICT)
2228                         return -EINVAL;
2229
2230                 if (dtype != NFT_DATA_VERDICT) {
2231                         if (nla[NFTA_SET_DATA_LEN] == NULL)
2232                                 return -EINVAL;
2233                         dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2234                         if (dlen == 0 ||
2235                             dlen > FIELD_SIZEOF(struct nft_data, data))
2236                                 return -EINVAL;
2237                 } else
2238                         dlen = sizeof(struct nft_data);
2239         } else if (flags & NFT_SET_MAP)
2240                 return -EINVAL;
2241
2242         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2243
2244         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2245         if (IS_ERR(afi))
2246                 return PTR_ERR(afi);
2247
2248         table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2249         if (IS_ERR(table))
2250                 return PTR_ERR(table);
2251
2252         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2253
2254         set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2255         if (IS_ERR(set)) {
2256                 if (PTR_ERR(set) != -ENOENT)
2257                         return PTR_ERR(set);
2258                 set = NULL;
2259         }
2260
2261         if (set != NULL) {
2262                 if (nlh->nlmsg_flags & NLM_F_EXCL)
2263                         return -EEXIST;
2264                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2265                         return -EOPNOTSUPP;
2266                 return 0;
2267         }
2268
2269         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2270                 return -ENOENT;
2271
2272         ops = nft_select_set_ops(nla);
2273         if (IS_ERR(ops))
2274                 return PTR_ERR(ops);
2275
2276         size = 0;
2277         if (ops->privsize != NULL)
2278                 size = ops->privsize(nla);
2279
2280         err = -ENOMEM;
2281         set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2282         if (set == NULL)
2283                 goto err1;
2284
2285         nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2286         err = nf_tables_set_alloc_name(&ctx, set, name);
2287         if (err < 0)
2288                 goto err2;
2289
2290         INIT_LIST_HEAD(&set->bindings);
2291         set->ops   = ops;
2292         set->ktype = ktype;
2293         set->klen  = klen;
2294         set->dtype = dtype;
2295         set->dlen  = dlen;
2296         set->flags = flags;
2297
2298         err = ops->init(set, nla);
2299         if (err < 0)
2300                 goto err2;
2301
2302         list_add_tail(&set->list, &table->sets);
2303         nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2304         return 0;
2305
2306 err2:
2307         kfree(set);
2308 err1:
2309         module_put(ops->owner);
2310         return err;
2311 }
2312
2313 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2314 {
2315         list_del(&set->list);
2316         if (!(set->flags & NFT_SET_ANONYMOUS))
2317                 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2318
2319         set->ops->destroy(set);
2320         module_put(set->ops->owner);
2321         kfree(set);
2322 }
2323
2324 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2325                             const struct nlmsghdr *nlh,
2326                             const struct nlattr * const nla[])
2327 {
2328         struct nft_set *set;
2329         struct nft_ctx ctx;
2330         int err;
2331
2332         if (nla[NFTA_SET_TABLE] == NULL)
2333                 return -EINVAL;
2334
2335         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2336         if (err < 0)
2337                 return err;
2338
2339         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2340         if (IS_ERR(set))
2341                 return PTR_ERR(set);
2342         if (!list_empty(&set->bindings))
2343                 return -EBUSY;
2344
2345         nf_tables_set_destroy(&ctx, set);
2346         return 0;
2347 }
2348
2349 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2350                                         const struct nft_set *set,
2351                                         const struct nft_set_iter *iter,
2352                                         const struct nft_set_elem *elem)
2353 {
2354         enum nft_registers dreg;
2355
2356         dreg = nft_type_to_reg(set->dtype);
2357         return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
2358 }
2359
2360 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2361                        struct nft_set_binding *binding)
2362 {
2363         struct nft_set_binding *i;
2364         struct nft_set_iter iter;
2365
2366         if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2367                 return -EBUSY;
2368
2369         if (set->flags & NFT_SET_MAP) {
2370                 /* If the set is already bound to the same chain all
2371                  * jumps are already validated for that chain.
2372                  */
2373                 list_for_each_entry(i, &set->bindings, list) {
2374                         if (i->chain == binding->chain)
2375                                 goto bind;
2376                 }
2377
2378                 iter.skip       = 0;
2379                 iter.count      = 0;
2380                 iter.err        = 0;
2381                 iter.fn         = nf_tables_bind_check_setelem;
2382
2383                 set->ops->walk(ctx, set, &iter);
2384                 if (iter.err < 0) {
2385                         /* Destroy anonymous sets if binding fails */
2386                         if (set->flags & NFT_SET_ANONYMOUS)
2387                                 nf_tables_set_destroy(ctx, set);
2388
2389                         return iter.err;
2390                 }
2391         }
2392 bind:
2393         binding->chain = ctx->chain;
2394         list_add_tail(&binding->list, &set->bindings);
2395         return 0;
2396 }
2397
2398 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2399                           struct nft_set_binding *binding)
2400 {
2401         list_del(&binding->list);
2402
2403         if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2404                 nf_tables_set_destroy(ctx, set);
2405 }
2406
2407 /*
2408  * Set elements
2409  */
2410
2411 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2412         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
2413         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
2414         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
2415 };
2416
2417 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2418         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING },
2419         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING },
2420         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
2421 };
2422
2423 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2424                                       const struct sk_buff *skb,
2425                                       const struct nlmsghdr *nlh,
2426                                       const struct nlattr * const nla[])
2427 {
2428         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2429         const struct nft_af_info *afi;
2430         const struct nft_table *table;
2431         struct net *net = sock_net(skb->sk);
2432
2433         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2434         if (IS_ERR(afi))
2435                 return PTR_ERR(afi);
2436
2437         table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2438         if (IS_ERR(table))
2439                 return PTR_ERR(table);
2440
2441         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2442         return 0;
2443 }
2444
2445 static int nf_tables_fill_setelem(struct sk_buff *skb,
2446                                   const struct nft_set *set,
2447                                   const struct nft_set_elem *elem)
2448 {
2449         unsigned char *b = skb_tail_pointer(skb);
2450         struct nlattr *nest;
2451
2452         nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2453         if (nest == NULL)
2454                 goto nla_put_failure;
2455
2456         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2457                           set->klen) < 0)
2458                 goto nla_put_failure;
2459
2460         if (set->flags & NFT_SET_MAP &&
2461             !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2462             nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2463                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2464                           set->dlen) < 0)
2465                 goto nla_put_failure;
2466
2467         if (elem->flags != 0)
2468                 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2469                         goto nla_put_failure;
2470
2471         nla_nest_end(skb, nest);
2472         return 0;
2473
2474 nla_put_failure:
2475         nlmsg_trim(skb, b);
2476         return -EMSGSIZE;
2477 }
2478
2479 struct nft_set_dump_args {
2480         const struct netlink_callback   *cb;
2481         struct nft_set_iter             iter;
2482         struct sk_buff                  *skb;
2483 };
2484
2485 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2486                                   const struct nft_set *set,
2487                                   const struct nft_set_iter *iter,
2488                                   const struct nft_set_elem *elem)
2489 {
2490         struct nft_set_dump_args *args;
2491
2492         args = container_of(iter, struct nft_set_dump_args, iter);
2493         return nf_tables_fill_setelem(args->skb, set, elem);
2494 }
2495
2496 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2497 {
2498         const struct nft_set *set;
2499         struct nft_set_dump_args args;
2500         struct nft_ctx ctx;
2501         struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2502         struct nfgenmsg *nfmsg;
2503         struct nlmsghdr *nlh;
2504         struct nlattr *nest;
2505         u32 portid, seq;
2506         int event, err;
2507
2508         nfmsg = nlmsg_data(cb->nlh);
2509         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2510                           nft_set_elem_list_policy);
2511         if (err < 0)
2512                 return err;
2513
2514         err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2515         if (err < 0)
2516                 return err;
2517
2518         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2519         if (IS_ERR(set))
2520                 return PTR_ERR(set);
2521
2522         event  = NFT_MSG_NEWSETELEM;
2523         event |= NFNL_SUBSYS_NFTABLES << 8;
2524         portid = NETLINK_CB(cb->skb).portid;
2525         seq    = cb->nlh->nlmsg_seq;
2526
2527         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2528                         NLM_F_MULTI);
2529         if (nlh == NULL)
2530                 goto nla_put_failure;
2531
2532         nfmsg = nlmsg_data(nlh);
2533         nfmsg->nfgen_family = NFPROTO_UNSPEC;
2534         nfmsg->version      = NFNETLINK_V0;
2535         nfmsg->res_id       = 0;
2536
2537         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2538                 goto nla_put_failure;
2539         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2540                 goto nla_put_failure;
2541
2542         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2543         if (nest == NULL)
2544                 goto nla_put_failure;
2545
2546         args.cb         = cb;
2547         args.skb        = skb;
2548         args.iter.skip  = cb->args[0];
2549         args.iter.count = 0;
2550         args.iter.err   = 0;
2551         args.iter.fn    = nf_tables_dump_setelem;
2552         set->ops->walk(&ctx, set, &args.iter);
2553
2554         nla_nest_end(skb, nest);
2555         nlmsg_end(skb, nlh);
2556
2557         if (args.iter.err && args.iter.err != -EMSGSIZE)
2558                 return args.iter.err;
2559         if (args.iter.count == cb->args[0])
2560                 return 0;
2561
2562         cb->args[0] = args.iter.count;
2563         return skb->len;
2564
2565 nla_put_failure:
2566         return -ENOSPC;
2567 }
2568
2569 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2570                                 const struct nlmsghdr *nlh,
2571                                 const struct nlattr * const nla[])
2572 {
2573         const struct nft_set *set;
2574         struct nft_ctx ctx;
2575         int err;
2576
2577         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2578         if (err < 0)
2579                 return err;
2580
2581         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2582         if (IS_ERR(set))
2583                 return PTR_ERR(set);
2584
2585         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2586                 struct netlink_dump_control c = {
2587                         .dump = nf_tables_dump_set,
2588                 };
2589                 return netlink_dump_start(nlsk, skb, nlh, &c);
2590         }
2591         return -EOPNOTSUPP;
2592 }
2593
2594 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2595                             const struct nlattr *attr)
2596 {
2597         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2598         struct nft_data_desc d1, d2;
2599         struct nft_set_elem elem;
2600         struct nft_set_binding *binding;
2601         enum nft_registers dreg;
2602         int err;
2603
2604         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2605                                nft_set_elem_policy);
2606         if (err < 0)
2607                 return err;
2608
2609         if (nla[NFTA_SET_ELEM_KEY] == NULL)
2610                 return -EINVAL;
2611
2612         elem.flags = 0;
2613         if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2614                 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2615                 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2616                         return -EINVAL;
2617         }
2618
2619         if (set->flags & NFT_SET_MAP) {
2620                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2621                     !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2622                         return -EINVAL;
2623         } else {
2624                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2625                         return -EINVAL;
2626         }
2627
2628         err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2629         if (err < 0)
2630                 goto err1;
2631         err = -EINVAL;
2632         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2633                 goto err2;
2634
2635         err = -EEXIST;
2636         if (set->ops->get(set, &elem) == 0)
2637                 goto err2;
2638
2639         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2640                 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2641                 if (err < 0)
2642                         goto err2;
2643
2644                 err = -EINVAL;
2645                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2646                         goto err3;
2647
2648                 dreg = nft_type_to_reg(set->dtype);
2649                 list_for_each_entry(binding, &set->bindings, list) {
2650                         struct nft_ctx bind_ctx = {
2651                                 .afi    = ctx->afi,
2652                                 .table  = ctx->table,
2653                                 .chain  = binding->chain,
2654                         };
2655
2656                         err = nft_validate_data_load(&bind_ctx, dreg,
2657                                                      &elem.data, d2.type);
2658                         if (err < 0)
2659                                 goto err3;
2660                 }
2661         }
2662
2663         err = set->ops->insert(set, &elem);
2664         if (err < 0)
2665                 goto err3;
2666
2667         return 0;
2668
2669 err3:
2670         if (nla[NFTA_SET_ELEM_DATA] != NULL)
2671                 nft_data_uninit(&elem.data, d2.type);
2672 err2:
2673         nft_data_uninit(&elem.key, d1.type);
2674 err1:
2675         return err;
2676 }
2677
2678 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2679                                 const struct nlmsghdr *nlh,
2680                                 const struct nlattr * const nla[])
2681 {
2682         const struct nlattr *attr;
2683         struct nft_set *set;
2684         struct nft_ctx ctx;
2685         int rem, err;
2686
2687         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2688         if (err < 0)
2689                 return err;
2690
2691         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2692         if (IS_ERR(set))
2693                 return PTR_ERR(set);
2694         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2695                 return -EBUSY;
2696
2697         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2698                 err = nft_add_set_elem(&ctx, set, attr);
2699                 if (err < 0)
2700                         return err;
2701         }
2702         return 0;
2703 }
2704
2705 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2706                            const struct nlattr *attr)
2707 {
2708         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2709         struct nft_data_desc desc;
2710         struct nft_set_elem elem;
2711         int err;
2712
2713         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2714                                nft_set_elem_policy);
2715         if (err < 0)
2716                 goto err1;
2717
2718         err = -EINVAL;
2719         if (nla[NFTA_SET_ELEM_KEY] == NULL)
2720                 goto err1;
2721
2722         err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2723         if (err < 0)
2724                 goto err1;
2725
2726         err = -EINVAL;
2727         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2728                 goto err2;
2729
2730         err = set->ops->get(set, &elem);
2731         if (err < 0)
2732                 goto err2;
2733
2734         set->ops->remove(set, &elem);
2735
2736         nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2737         if (set->flags & NFT_SET_MAP)
2738                 nft_data_uninit(&elem.data, set->dtype);
2739
2740 err2:
2741         nft_data_uninit(&elem.key, desc.type);
2742 err1:
2743         return err;
2744 }
2745
2746 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2747                                 const struct nlmsghdr *nlh,
2748                                 const struct nlattr * const nla[])
2749 {
2750         const struct nlattr *attr;
2751         struct nft_set *set;
2752         struct nft_ctx ctx;
2753         int rem, err;
2754
2755         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2756         if (err < 0)
2757                 return err;
2758
2759         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2760         if (IS_ERR(set))
2761                 return PTR_ERR(set);
2762         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2763                 return -EBUSY;
2764
2765         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2766                 err = nft_del_setelem(&ctx, set, attr);
2767                 if (err < 0)
2768                         return err;
2769         }
2770         return 0;
2771 }
2772
2773 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2774         [NFT_MSG_NEWTABLE] = {
2775                 .call           = nf_tables_newtable,
2776                 .attr_count     = NFTA_TABLE_MAX,
2777                 .policy         = nft_table_policy,
2778         },
2779         [NFT_MSG_GETTABLE] = {
2780                 .call           = nf_tables_gettable,
2781                 .attr_count     = NFTA_TABLE_MAX,
2782                 .policy         = nft_table_policy,
2783         },
2784         [NFT_MSG_DELTABLE] = {
2785                 .call           = nf_tables_deltable,
2786                 .attr_count     = NFTA_TABLE_MAX,
2787                 .policy         = nft_table_policy,
2788         },
2789         [NFT_MSG_NEWCHAIN] = {
2790                 .call           = nf_tables_newchain,
2791                 .attr_count     = NFTA_CHAIN_MAX,
2792                 .policy         = nft_chain_policy,
2793         },
2794         [NFT_MSG_GETCHAIN] = {
2795                 .call           = nf_tables_getchain,
2796                 .attr_count     = NFTA_CHAIN_MAX,
2797                 .policy         = nft_chain_policy,
2798         },
2799         [NFT_MSG_DELCHAIN] = {
2800                 .call           = nf_tables_delchain,
2801                 .attr_count     = NFTA_CHAIN_MAX,
2802                 .policy         = nft_chain_policy,
2803         },
2804         [NFT_MSG_NEWRULE] = {
2805                 .call_batch     = nf_tables_newrule,
2806                 .attr_count     = NFTA_RULE_MAX,
2807                 .policy         = nft_rule_policy,
2808         },
2809         [NFT_MSG_GETRULE] = {
2810                 .call           = nf_tables_getrule,
2811                 .attr_count     = NFTA_RULE_MAX,
2812                 .policy         = nft_rule_policy,
2813         },
2814         [NFT_MSG_DELRULE] = {
2815                 .call_batch     = nf_tables_delrule,
2816                 .attr_count     = NFTA_RULE_MAX,
2817                 .policy         = nft_rule_policy,
2818         },
2819         [NFT_MSG_NEWSET] = {
2820                 .call           = nf_tables_newset,
2821                 .attr_count     = NFTA_SET_MAX,
2822                 .policy         = nft_set_policy,
2823         },
2824         [NFT_MSG_GETSET] = {
2825                 .call           = nf_tables_getset,
2826                 .attr_count     = NFTA_SET_MAX,
2827                 .policy         = nft_set_policy,
2828         },
2829         [NFT_MSG_DELSET] = {
2830                 .call           = nf_tables_delset,
2831                 .attr_count     = NFTA_SET_MAX,
2832                 .policy         = nft_set_policy,
2833         },
2834         [NFT_MSG_NEWSETELEM] = {
2835                 .call           = nf_tables_newsetelem,
2836                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2837                 .policy         = nft_set_elem_list_policy,
2838         },
2839         [NFT_MSG_GETSETELEM] = {
2840                 .call           = nf_tables_getsetelem,
2841                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2842                 .policy         = nft_set_elem_list_policy,
2843         },
2844         [NFT_MSG_DELSETELEM] = {
2845                 .call           = nf_tables_delsetelem,
2846                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2847                 .policy         = nft_set_elem_list_policy,
2848         },
2849 };
2850
2851 static const struct nfnetlink_subsystem nf_tables_subsys = {
2852         .name           = "nf_tables",
2853         .subsys_id      = NFNL_SUBSYS_NFTABLES,
2854         .cb_count       = NFT_MSG_MAX,
2855         .cb             = nf_tables_cb,
2856         .commit         = nf_tables_commit,
2857         .abort          = nf_tables_abort,
2858 };
2859
2860 /*
2861  * Loop detection - walk through the ruleset beginning at the destination chain
2862  * of a new jump until either the source chain is reached (loop) or all
2863  * reachable chains have been traversed.
2864  *
2865  * The loop check is performed whenever a new jump verdict is added to an
2866  * expression or verdict map or a verdict map is bound to a new chain.
2867  */
2868
2869 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2870                                  const struct nft_chain *chain);
2871
2872 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2873                                         const struct nft_set *set,
2874                                         const struct nft_set_iter *iter,
2875                                         const struct nft_set_elem *elem)
2876 {
2877         switch (elem->data.verdict) {
2878         case NFT_JUMP:
2879         case NFT_GOTO:
2880                 return nf_tables_check_loops(ctx, elem->data.chain);
2881         default:
2882                 return 0;
2883         }
2884 }
2885
2886 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2887                                  const struct nft_chain *chain)
2888 {
2889         const struct nft_rule *rule;
2890         const struct nft_expr *expr, *last;
2891         const struct nft_set *set;
2892         struct nft_set_binding *binding;
2893         struct nft_set_iter iter;
2894
2895         if (ctx->chain == chain)
2896                 return -ELOOP;
2897
2898         list_for_each_entry(rule, &chain->rules, list) {
2899                 nft_rule_for_each_expr(expr, last, rule) {
2900                         const struct nft_data *data = NULL;
2901                         int err;
2902
2903                         if (!expr->ops->validate)
2904                                 continue;
2905
2906                         err = expr->ops->validate(ctx, expr, &data);
2907                         if (err < 0)
2908                                 return err;
2909
2910                         if (data == NULL)
2911                                 continue;
2912
2913                         switch (data->verdict) {
2914                         case NFT_JUMP:
2915                         case NFT_GOTO:
2916                                 err = nf_tables_check_loops(ctx, data->chain);
2917                                 if (err < 0)
2918                                         return err;
2919                         default:
2920                                 break;
2921                         }
2922                 }
2923         }
2924
2925         list_for_each_entry(set, &ctx->table->sets, list) {
2926                 if (!(set->flags & NFT_SET_MAP) ||
2927                     set->dtype != NFT_DATA_VERDICT)
2928                         continue;
2929
2930                 list_for_each_entry(binding, &set->bindings, list) {
2931                         if (binding->chain != chain)
2932                                 continue;
2933
2934                         iter.skip       = 0;
2935                         iter.count      = 0;
2936                         iter.err        = 0;
2937                         iter.fn         = nf_tables_loop_check_setelem;
2938
2939                         set->ops->walk(ctx, set, &iter);
2940                         if (iter.err < 0)
2941                                 return iter.err;
2942                 }
2943         }
2944
2945         return 0;
2946 }
2947
2948 /**
2949  *      nft_validate_input_register - validate an expressions' input register
2950  *
2951  *      @reg: the register number
2952  *
2953  *      Validate that the input register is one of the general purpose
2954  *      registers.
2955  */
2956 int nft_validate_input_register(enum nft_registers reg)
2957 {
2958         if (reg <= NFT_REG_VERDICT)
2959                 return -EINVAL;
2960         if (reg > NFT_REG_MAX)
2961                 return -ERANGE;
2962         return 0;
2963 }
2964 EXPORT_SYMBOL_GPL(nft_validate_input_register);
2965
2966 /**
2967  *      nft_validate_output_register - validate an expressions' output register
2968  *
2969  *      @reg: the register number
2970  *
2971  *      Validate that the output register is one of the general purpose
2972  *      registers or the verdict register.
2973  */
2974 int nft_validate_output_register(enum nft_registers reg)
2975 {
2976         if (reg < NFT_REG_VERDICT)
2977                 return -EINVAL;
2978         if (reg > NFT_REG_MAX)
2979                 return -ERANGE;
2980         return 0;
2981 }
2982 EXPORT_SYMBOL_GPL(nft_validate_output_register);
2983
2984 /**
2985  *      nft_validate_data_load - validate an expressions' data load
2986  *
2987  *      @ctx: context of the expression performing the load
2988  *      @reg: the destination register number
2989  *      @data: the data to load
2990  *      @type: the data type
2991  *
2992  *      Validate that a data load uses the appropriate data type for
2993  *      the destination register. A value of NULL for the data means
2994  *      that its runtime gathered data, which is always of type
2995  *      NFT_DATA_VALUE.
2996  */
2997 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
2998                            const struct nft_data *data,
2999                            enum nft_data_types type)
3000 {
3001         int err;
3002
3003         switch (reg) {
3004         case NFT_REG_VERDICT:
3005                 if (data == NULL || type != NFT_DATA_VERDICT)
3006                         return -EINVAL;
3007
3008                 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3009                         err = nf_tables_check_loops(ctx, data->chain);
3010                         if (err < 0)
3011                                 return err;
3012
3013                         if (ctx->chain->level + 1 > data->chain->level) {
3014                                 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3015                                         return -EMLINK;
3016                                 data->chain->level = ctx->chain->level + 1;
3017                         }
3018                 }
3019
3020                 return 0;
3021         default:
3022                 if (data != NULL && type != NFT_DATA_VALUE)
3023                         return -EINVAL;
3024                 return 0;
3025         }
3026 }
3027 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3028
3029 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3030         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
3031         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
3032                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
3033 };
3034
3035 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3036                             struct nft_data_desc *desc, const struct nlattr *nla)
3037 {
3038         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3039         struct nft_chain *chain;
3040         int err;
3041
3042         err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3043         if (err < 0)
3044                 return err;
3045
3046         if (!tb[NFTA_VERDICT_CODE])
3047                 return -EINVAL;
3048         data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3049
3050         switch (data->verdict) {
3051         case NF_ACCEPT:
3052         case NF_DROP:
3053         case NF_QUEUE:
3054         case NFT_CONTINUE:
3055         case NFT_BREAK:
3056         case NFT_RETURN:
3057                 desc->len = sizeof(data->verdict);
3058                 break;
3059         case NFT_JUMP:
3060         case NFT_GOTO:
3061                 if (!tb[NFTA_VERDICT_CHAIN])
3062                         return -EINVAL;
3063                 chain = nf_tables_chain_lookup(ctx->table,
3064                                                tb[NFTA_VERDICT_CHAIN]);
3065                 if (IS_ERR(chain))
3066                         return PTR_ERR(chain);
3067                 if (chain->flags & NFT_BASE_CHAIN)
3068                         return -EOPNOTSUPP;
3069
3070                 chain->use++;
3071                 data->chain = chain;
3072                 desc->len = sizeof(data);
3073                 break;
3074         default:
3075                 return -EINVAL;
3076         }
3077
3078         desc->type = NFT_DATA_VERDICT;
3079         return 0;
3080 }
3081
3082 static void nft_verdict_uninit(const struct nft_data *data)
3083 {
3084         switch (data->verdict) {
3085         case NFT_JUMP:
3086         case NFT_GOTO:
3087                 data->chain->use--;
3088                 break;
3089         }
3090 }
3091
3092 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3093 {
3094         struct nlattr *nest;
3095
3096         nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3097         if (!nest)
3098                 goto nla_put_failure;
3099
3100         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3101                 goto nla_put_failure;
3102
3103         switch (data->verdict) {
3104         case NFT_JUMP:
3105         case NFT_GOTO:
3106                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3107                         goto nla_put_failure;
3108         }
3109         nla_nest_end(skb, nest);
3110         return 0;
3111
3112 nla_put_failure:
3113         return -1;
3114 }
3115
3116 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3117                           struct nft_data_desc *desc, const struct nlattr *nla)
3118 {
3119         unsigned int len;
3120
3121         len = nla_len(nla);
3122         if (len == 0)
3123                 return -EINVAL;
3124         if (len > sizeof(data->data))
3125                 return -EOVERFLOW;
3126
3127         nla_memcpy(data->data, nla, sizeof(data->data));
3128         desc->type = NFT_DATA_VALUE;
3129         desc->len  = len;
3130         return 0;
3131 }
3132
3133 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3134                           unsigned int len)
3135 {
3136         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3137 }
3138
3139 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3140         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY,
3141                                     .len  = FIELD_SIZEOF(struct nft_data, data) },
3142         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
3143 };
3144
3145 /**
3146  *      nft_data_init - parse nf_tables data netlink attributes
3147  *
3148  *      @ctx: context of the expression using the data
3149  *      @data: destination struct nft_data
3150  *      @desc: data description
3151  *      @nla: netlink attribute containing data
3152  *
3153  *      Parse the netlink data attributes and initialize a struct nft_data.
3154  *      The type and length of data are returned in the data description.
3155  *
3156  *      The caller can indicate that it only wants to accept data of type
3157  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
3158  */
3159 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3160                   struct nft_data_desc *desc, const struct nlattr *nla)
3161 {
3162         struct nlattr *tb[NFTA_DATA_MAX + 1];
3163         int err;
3164
3165         err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3166         if (err < 0)
3167                 return err;
3168
3169         if (tb[NFTA_DATA_VALUE])
3170                 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3171         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3172                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3173         return -EINVAL;
3174 }
3175 EXPORT_SYMBOL_GPL(nft_data_init);
3176
3177 /**
3178  *      nft_data_uninit - release a nft_data item
3179  *
3180  *      @data: struct nft_data to release
3181  *      @type: type of data
3182  *
3183  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3184  *      all others need to be released by calling this function.
3185  */
3186 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3187 {
3188         switch (type) {
3189         case NFT_DATA_VALUE:
3190                 return;
3191         case NFT_DATA_VERDICT:
3192                 return nft_verdict_uninit(data);
3193         default:
3194                 WARN_ON(1);
3195         }
3196 }
3197 EXPORT_SYMBOL_GPL(nft_data_uninit);
3198
3199 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3200                   enum nft_data_types type, unsigned int len)
3201 {
3202         struct nlattr *nest;
3203         int err;
3204
3205         nest = nla_nest_start(skb, attr);
3206         if (nest == NULL)
3207                 return -1;
3208
3209         switch (type) {
3210         case NFT_DATA_VALUE:
3211                 err = nft_value_dump(skb, data, len);
3212                 break;
3213         case NFT_DATA_VERDICT:
3214                 err = nft_verdict_dump(skb, data);
3215                 break;
3216         default:
3217                 err = -EINVAL;
3218                 WARN_ON(1);
3219         }
3220
3221         nla_nest_end(skb, nest);
3222         return err;
3223 }
3224 EXPORT_SYMBOL_GPL(nft_data_dump);
3225
3226 static int nf_tables_init_net(struct net *net)
3227 {
3228         INIT_LIST_HEAD(&net->nft.af_info);
3229         INIT_LIST_HEAD(&net->nft.commit_list);
3230         return 0;
3231 }
3232
3233 static struct pernet_operations nf_tables_net_ops = {
3234         .init   = nf_tables_init_net,
3235 };
3236
3237 static int __init nf_tables_module_init(void)
3238 {
3239         int err;
3240
3241         info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3242                        GFP_KERNEL);
3243         if (info == NULL) {
3244                 err = -ENOMEM;
3245                 goto err1;
3246         }
3247
3248         err = nf_tables_core_module_init();
3249         if (err < 0)
3250                 goto err2;
3251
3252         err = nfnetlink_subsys_register(&nf_tables_subsys);
3253         if (err < 0)
3254                 goto err3;
3255
3256         pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3257         return register_pernet_subsys(&nf_tables_net_ops);
3258 err3:
3259         nf_tables_core_module_exit();
3260 err2:
3261         kfree(info);
3262 err1:
3263         return err;
3264 }
3265
3266 static void __exit nf_tables_module_exit(void)
3267 {
3268         unregister_pernet_subsys(&nf_tables_net_ops);
3269         nfnetlink_subsys_unregister(&nf_tables_subsys);
3270         nf_tables_core_module_exit();
3271         kfree(info);
3272 }
3273
3274 module_init(nf_tables_module_init);
3275 module_exit(nf_tables_module_exit);
3276
3277 MODULE_LICENSE("GPL");
3278 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3279 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);