]> Pileus Git - ~andy/linux/blob - net/netfilter/nf_tables_api.c
net_sched: act: action flushing missaccounting
[~andy/linux] / 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_table_delrule_by_chain(struct nft_ctx *ctx)
1722 {
1723         struct nft_rule *rule;
1724         int err;
1725
1726         list_for_each_entry(rule, &ctx->chain->rules, list) {
1727                 err = nf_tables_delrule_one(ctx, rule);
1728                 if (err < 0)
1729                         return err;
1730         }
1731         return 0;
1732 }
1733
1734 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1735                              const struct nlmsghdr *nlh,
1736                              const struct nlattr * const nla[])
1737 {
1738         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1739         const struct nft_af_info *afi;
1740         struct net *net = sock_net(skb->sk);
1741         const struct nft_table *table;
1742         struct nft_chain *chain = NULL;
1743         struct nft_rule *rule;
1744         int family = nfmsg->nfgen_family, err = 0;
1745         struct nft_ctx ctx;
1746
1747         afi = nf_tables_afinfo_lookup(net, family, false);
1748         if (IS_ERR(afi))
1749                 return PTR_ERR(afi);
1750
1751         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1752         if (IS_ERR(table))
1753                 return PTR_ERR(table);
1754
1755         if (nla[NFTA_RULE_CHAIN]) {
1756                 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1757                 if (IS_ERR(chain))
1758                         return PTR_ERR(chain);
1759         }
1760
1761         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1762
1763         if (chain) {
1764                 if (nla[NFTA_RULE_HANDLE]) {
1765                         rule = nf_tables_rule_lookup(chain,
1766                                                      nla[NFTA_RULE_HANDLE]);
1767                         if (IS_ERR(rule))
1768                                 return PTR_ERR(rule);
1769
1770                         err = nf_tables_delrule_one(&ctx, rule);
1771                 } else {
1772                         err = nf_table_delrule_by_chain(&ctx);
1773                 }
1774         } else {
1775                 list_for_each_entry(chain, &table->chains, list) {
1776                         ctx.chain = chain;
1777                         err = nf_table_delrule_by_chain(&ctx);
1778                         if (err < 0)
1779                                 break;
1780                 }
1781         }
1782
1783         return err;
1784 }
1785
1786 static int nf_tables_commit(struct sk_buff *skb)
1787 {
1788         struct net *net = sock_net(skb->sk);
1789         struct nft_rule_trans *rupd, *tmp;
1790
1791         /* Bump generation counter, invalidate any dump in progress */
1792         net->nft.genctr++;
1793
1794         /* A new generation has just started */
1795         net->nft.gencursor = gencursor_next(net);
1796
1797         /* Make sure all packets have left the previous generation before
1798          * purging old rules.
1799          */
1800         synchronize_rcu();
1801
1802         list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1803                 /* Delete this rule from the dirty list */
1804                 list_del(&rupd->list);
1805
1806                 /* This rule was inactive in the past and just became active.
1807                  * Clear the next bit of the genmask since its meaning has
1808                  * changed, now it is the future.
1809                  */
1810                 if (nft_rule_is_active(net, rupd->rule)) {
1811                         nft_rule_clear(net, rupd->rule);
1812                         nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1813                                               rupd->chain, rupd->rule,
1814                                               NFT_MSG_NEWRULE, 0,
1815                                               rupd->family);
1816                         kfree(rupd);
1817                         continue;
1818                 }
1819
1820                 /* This rule is in the past, get rid of it */
1821                 list_del_rcu(&rupd->rule->list);
1822                 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1823                                       rupd->rule, NFT_MSG_DELRULE, 0,
1824                                       rupd->family);
1825                 nf_tables_rule_destroy(rupd->rule);
1826                 kfree(rupd);
1827         }
1828
1829         return 0;
1830 }
1831
1832 static int nf_tables_abort(struct sk_buff *skb)
1833 {
1834         struct net *net = sock_net(skb->sk);
1835         struct nft_rule_trans *rupd, *tmp;
1836
1837         list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1838                 /* Delete all rules from the dirty list */
1839                 list_del(&rupd->list);
1840
1841                 if (!nft_rule_is_active_next(net, rupd->rule)) {
1842                         nft_rule_clear(net, rupd->rule);
1843                         kfree(rupd);
1844                         continue;
1845                 }
1846
1847                 /* This rule is inactive, get rid of it */
1848                 list_del_rcu(&rupd->rule->list);
1849                 nf_tables_rule_destroy(rupd->rule);
1850                 kfree(rupd);
1851         }
1852         return 0;
1853 }
1854
1855 /*
1856  * Sets
1857  */
1858
1859 static LIST_HEAD(nf_tables_set_ops);
1860
1861 int nft_register_set(struct nft_set_ops *ops)
1862 {
1863         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1864         list_add_tail(&ops->list, &nf_tables_set_ops);
1865         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1866         return 0;
1867 }
1868 EXPORT_SYMBOL_GPL(nft_register_set);
1869
1870 void nft_unregister_set(struct nft_set_ops *ops)
1871 {
1872         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1873         list_del(&ops->list);
1874         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1875 }
1876 EXPORT_SYMBOL_GPL(nft_unregister_set);
1877
1878 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1879 {
1880         const struct nft_set_ops *ops;
1881         u32 features;
1882
1883 #ifdef CONFIG_MODULES
1884         if (list_empty(&nf_tables_set_ops)) {
1885                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1886                 request_module("nft-set");
1887                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1888                 if (!list_empty(&nf_tables_set_ops))
1889                         return ERR_PTR(-EAGAIN);
1890         }
1891 #endif
1892         features = 0;
1893         if (nla[NFTA_SET_FLAGS] != NULL) {
1894                 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1895                 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1896         }
1897
1898         // FIXME: implement selection properly
1899         list_for_each_entry(ops, &nf_tables_set_ops, list) {
1900                 if ((ops->features & features) != features)
1901                         continue;
1902                 if (!try_module_get(ops->owner))
1903                         continue;
1904                 return ops;
1905         }
1906
1907         return ERR_PTR(-EOPNOTSUPP);
1908 }
1909
1910 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1911         [NFTA_SET_TABLE]                = { .type = NLA_STRING },
1912         [NFTA_SET_NAME]                 = { .type = NLA_STRING },
1913         [NFTA_SET_FLAGS]                = { .type = NLA_U32 },
1914         [NFTA_SET_KEY_TYPE]             = { .type = NLA_U32 },
1915         [NFTA_SET_KEY_LEN]              = { .type = NLA_U32 },
1916         [NFTA_SET_DATA_TYPE]            = { .type = NLA_U32 },
1917         [NFTA_SET_DATA_LEN]             = { .type = NLA_U32 },
1918 };
1919
1920 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1921                                      const struct sk_buff *skb,
1922                                      const struct nlmsghdr *nlh,
1923                                      const struct nlattr * const nla[])
1924 {
1925         struct net *net = sock_net(skb->sk);
1926         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1927         const struct nft_af_info *afi = NULL;
1928         const struct nft_table *table = NULL;
1929
1930         if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1931                 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1932                 if (IS_ERR(afi))
1933                         return PTR_ERR(afi);
1934         }
1935
1936         if (nla[NFTA_SET_TABLE] != NULL) {
1937                 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1938                 if (IS_ERR(table))
1939                         return PTR_ERR(table);
1940         }
1941
1942         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1943         return 0;
1944 }
1945
1946 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1947                                      const struct nlattr *nla)
1948 {
1949         struct nft_set *set;
1950
1951         if (nla == NULL)
1952                 return ERR_PTR(-EINVAL);
1953
1954         list_for_each_entry(set, &table->sets, list) {
1955                 if (!nla_strcmp(nla, set->name))
1956                         return set;
1957         }
1958         return ERR_PTR(-ENOENT);
1959 }
1960
1961 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1962                                     const char *name)
1963 {
1964         const struct nft_set *i;
1965         const char *p;
1966         unsigned long *inuse;
1967         unsigned int n = 0;
1968
1969         p = strnchr(name, IFNAMSIZ, '%');
1970         if (p != NULL) {
1971                 if (p[1] != 'd' || strchr(p + 2, '%'))
1972                         return -EINVAL;
1973
1974                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1975                 if (inuse == NULL)
1976                         return -ENOMEM;
1977
1978                 list_for_each_entry(i, &ctx->table->sets, list) {
1979                         int tmp;
1980
1981                         if (!sscanf(i->name, name, &tmp))
1982                                 continue;
1983                         if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
1984                                 continue;
1985
1986                         set_bit(tmp, inuse);
1987                 }
1988
1989                 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1990                 free_page((unsigned long)inuse);
1991         }
1992
1993         snprintf(set->name, sizeof(set->name), name, n);
1994         list_for_each_entry(i, &ctx->table->sets, list) {
1995                 if (!strcmp(set->name, i->name))
1996                         return -ENFILE;
1997         }
1998         return 0;
1999 }
2000
2001 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2002                               const struct nft_set *set, u16 event, u16 flags)
2003 {
2004         struct nfgenmsg *nfmsg;
2005         struct nlmsghdr *nlh;
2006         u32 portid = NETLINK_CB(ctx->skb).portid;
2007         u32 seq = ctx->nlh->nlmsg_seq;
2008
2009         event |= NFNL_SUBSYS_NFTABLES << 8;
2010         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2011                         flags);
2012         if (nlh == NULL)
2013                 goto nla_put_failure;
2014
2015         nfmsg = nlmsg_data(nlh);
2016         nfmsg->nfgen_family     = ctx->afi->family;
2017         nfmsg->version          = NFNETLINK_V0;
2018         nfmsg->res_id           = 0;
2019
2020         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2021                 goto nla_put_failure;
2022         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2023                 goto nla_put_failure;
2024         if (set->flags != 0)
2025                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2026                         goto nla_put_failure;
2027
2028         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2029                 goto nla_put_failure;
2030         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2031                 goto nla_put_failure;
2032         if (set->flags & NFT_SET_MAP) {
2033                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2034                         goto nla_put_failure;
2035                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2036                         goto nla_put_failure;
2037         }
2038
2039         return nlmsg_end(skb, nlh);
2040
2041 nla_put_failure:
2042         nlmsg_trim(skb, nlh);
2043         return -1;
2044 }
2045
2046 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2047                                 const struct nft_set *set,
2048                                 int event)
2049 {
2050         struct sk_buff *skb;
2051         u32 portid = NETLINK_CB(ctx->skb).portid;
2052         bool report;
2053         int err;
2054
2055         report = nlmsg_report(ctx->nlh);
2056         if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2057                 return 0;
2058
2059         err = -ENOBUFS;
2060         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2061         if (skb == NULL)
2062                 goto err;
2063
2064         err = nf_tables_fill_set(skb, ctx, set, event, 0);
2065         if (err < 0) {
2066                 kfree_skb(skb);
2067                 goto err;
2068         }
2069
2070         err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2071                              GFP_KERNEL);
2072 err:
2073         if (err < 0)
2074                 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2075         return err;
2076 }
2077
2078 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2079                                      struct netlink_callback *cb)
2080 {
2081         const struct nft_set *set;
2082         unsigned int idx = 0, s_idx = cb->args[0];
2083
2084         if (cb->args[1])
2085                 return skb->len;
2086
2087         list_for_each_entry(set, &ctx->table->sets, list) {
2088                 if (idx < s_idx)
2089                         goto cont;
2090                 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2091                                        NLM_F_MULTI) < 0) {
2092                         cb->args[0] = idx;
2093                         goto done;
2094                 }
2095 cont:
2096                 idx++;
2097         }
2098         cb->args[1] = 1;
2099 done:
2100         return skb->len;
2101 }
2102
2103 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2104                                       struct netlink_callback *cb)
2105 {
2106         const struct nft_set *set;
2107         unsigned int idx = 0, s_idx = cb->args[0];
2108         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2109
2110         if (cb->args[1])
2111                 return skb->len;
2112
2113         list_for_each_entry(table, &ctx->afi->tables, list) {
2114                 if (cur_table && cur_table != table)
2115                         continue;
2116
2117                 ctx->table = table;
2118                 list_for_each_entry(set, &ctx->table->sets, list) {
2119                         if (idx < s_idx)
2120                                 goto cont;
2121                         if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2122                                                NLM_F_MULTI) < 0) {
2123                                 cb->args[0] = idx;
2124                                 cb->args[2] = (unsigned long) table;
2125                                 goto done;
2126                         }
2127 cont:
2128                         idx++;
2129                 }
2130         }
2131         cb->args[1] = 1;
2132 done:
2133         return skb->len;
2134 }
2135
2136 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2137                                    struct netlink_callback *cb)
2138 {
2139         const struct nft_set *set;
2140         unsigned int idx, s_idx = cb->args[0];
2141         const struct nft_af_info *afi;
2142         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2143         struct net *net = sock_net(skb->sk);
2144         int cur_family = cb->args[3];
2145
2146         if (cb->args[1])
2147                 return skb->len;
2148
2149         list_for_each_entry(afi, &net->nft.af_info, list) {
2150                 if (cur_family) {
2151                         if (afi->family != cur_family)
2152                                 continue;
2153
2154                         cur_family = 0;
2155                 }
2156
2157                 list_for_each_entry(table, &afi->tables, list) {
2158                         if (cur_table) {
2159                                 if (cur_table != table)
2160                                         continue;
2161
2162                                 cur_table = NULL;
2163                         }
2164
2165                         ctx->table = table;
2166                         ctx->afi = afi;
2167                         idx = 0;
2168                         list_for_each_entry(set, &ctx->table->sets, list) {
2169                                 if (idx < s_idx)
2170                                         goto cont;
2171                                 if (nf_tables_fill_set(skb, ctx, set,
2172                                                        NFT_MSG_NEWSET,
2173                                                        NLM_F_MULTI) < 0) {
2174                                         cb->args[0] = idx;
2175                                         cb->args[2] = (unsigned long) table;
2176                                         cb->args[3] = afi->family;
2177                                         goto done;
2178                                 }
2179 cont:
2180                                 idx++;
2181                         }
2182                         if (s_idx)
2183                                 s_idx = 0;
2184                 }
2185         }
2186         cb->args[1] = 1;
2187 done:
2188         return skb->len;
2189 }
2190
2191 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2192 {
2193         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2194         struct nlattr *nla[NFTA_SET_MAX + 1];
2195         struct nft_ctx ctx;
2196         int err, ret;
2197
2198         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2199                           nft_set_policy);
2200         if (err < 0)
2201                 return err;
2202
2203         err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2204         if (err < 0)
2205                 return err;
2206
2207         if (ctx.table == NULL) {
2208                 if (ctx.afi == NULL)
2209                         ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2210                 else
2211                         ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2212         } else
2213                 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2214
2215         return ret;
2216 }
2217
2218 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2219                             const struct nlmsghdr *nlh,
2220                             const struct nlattr * const nla[])
2221 {
2222         const struct nft_set *set;
2223         struct nft_ctx ctx;
2224         struct sk_buff *skb2;
2225         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2226         int err;
2227
2228         /* Verify existance before starting dump */
2229         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2230         if (err < 0)
2231                 return err;
2232
2233         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2234                 struct netlink_dump_control c = {
2235                         .dump = nf_tables_dump_sets,
2236                 };
2237                 return netlink_dump_start(nlsk, skb, nlh, &c);
2238         }
2239
2240         /* Only accept unspec with dump */
2241         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2242                 return -EAFNOSUPPORT;
2243
2244         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2245         if (IS_ERR(set))
2246                 return PTR_ERR(set);
2247
2248         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2249         if (skb2 == NULL)
2250                 return -ENOMEM;
2251
2252         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2253         if (err < 0)
2254                 goto err;
2255
2256         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2257
2258 err:
2259         kfree_skb(skb2);
2260         return err;
2261 }
2262
2263 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2264                             const struct nlmsghdr *nlh,
2265                             const struct nlattr * const nla[])
2266 {
2267         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2268         const struct nft_set_ops *ops;
2269         const struct nft_af_info *afi;
2270         struct net *net = sock_net(skb->sk);
2271         struct nft_table *table;
2272         struct nft_set *set;
2273         struct nft_ctx ctx;
2274         char name[IFNAMSIZ];
2275         unsigned int size;
2276         bool create;
2277         u32 ktype, klen, dlen, dtype, flags;
2278         int err;
2279
2280         if (nla[NFTA_SET_TABLE] == NULL ||
2281             nla[NFTA_SET_NAME] == NULL ||
2282             nla[NFTA_SET_KEY_LEN] == NULL)
2283                 return -EINVAL;
2284
2285         ktype = NFT_DATA_VALUE;
2286         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2287                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2288                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2289                         return -EINVAL;
2290         }
2291
2292         klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2293         if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2294                 return -EINVAL;
2295
2296         flags = 0;
2297         if (nla[NFTA_SET_FLAGS] != NULL) {
2298                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2299                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2300                               NFT_SET_INTERVAL | NFT_SET_MAP))
2301                         return -EINVAL;
2302         }
2303
2304         dtype = 0;
2305         dlen  = 0;
2306         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2307                 if (!(flags & NFT_SET_MAP))
2308                         return -EINVAL;
2309
2310                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2311                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2312                     dtype != NFT_DATA_VERDICT)
2313                         return -EINVAL;
2314
2315                 if (dtype != NFT_DATA_VERDICT) {
2316                         if (nla[NFTA_SET_DATA_LEN] == NULL)
2317                                 return -EINVAL;
2318                         dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2319                         if (dlen == 0 ||
2320                             dlen > FIELD_SIZEOF(struct nft_data, data))
2321                                 return -EINVAL;
2322                 } else
2323                         dlen = sizeof(struct nft_data);
2324         } else if (flags & NFT_SET_MAP)
2325                 return -EINVAL;
2326
2327         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2328
2329         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2330         if (IS_ERR(afi))
2331                 return PTR_ERR(afi);
2332
2333         table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2334         if (IS_ERR(table))
2335                 return PTR_ERR(table);
2336
2337         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2338
2339         set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2340         if (IS_ERR(set)) {
2341                 if (PTR_ERR(set) != -ENOENT)
2342                         return PTR_ERR(set);
2343                 set = NULL;
2344         }
2345
2346         if (set != NULL) {
2347                 if (nlh->nlmsg_flags & NLM_F_EXCL)
2348                         return -EEXIST;
2349                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2350                         return -EOPNOTSUPP;
2351                 return 0;
2352         }
2353
2354         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2355                 return -ENOENT;
2356
2357         ops = nft_select_set_ops(nla);
2358         if (IS_ERR(ops))
2359                 return PTR_ERR(ops);
2360
2361         size = 0;
2362         if (ops->privsize != NULL)
2363                 size = ops->privsize(nla);
2364
2365         err = -ENOMEM;
2366         set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2367         if (set == NULL)
2368                 goto err1;
2369
2370         nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2371         err = nf_tables_set_alloc_name(&ctx, set, name);
2372         if (err < 0)
2373                 goto err2;
2374
2375         INIT_LIST_HEAD(&set->bindings);
2376         set->ops   = ops;
2377         set->ktype = ktype;
2378         set->klen  = klen;
2379         set->dtype = dtype;
2380         set->dlen  = dlen;
2381         set->flags = flags;
2382
2383         err = ops->init(set, nla);
2384         if (err < 0)
2385                 goto err2;
2386
2387         list_add_tail(&set->list, &table->sets);
2388         nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2389         return 0;
2390
2391 err2:
2392         kfree(set);
2393 err1:
2394         module_put(ops->owner);
2395         return err;
2396 }
2397
2398 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2399 {
2400         list_del(&set->list);
2401         if (!(set->flags & NFT_SET_ANONYMOUS))
2402                 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2403
2404         set->ops->destroy(set);
2405         module_put(set->ops->owner);
2406         kfree(set);
2407 }
2408
2409 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2410                             const struct nlmsghdr *nlh,
2411                             const struct nlattr * const nla[])
2412 {
2413         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2414         struct nft_set *set;
2415         struct nft_ctx ctx;
2416         int err;
2417
2418         if (nla[NFTA_SET_TABLE] == NULL)
2419                 return -EINVAL;
2420
2421         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2422         if (err < 0)
2423                 return err;
2424
2425         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2426                 return -EAFNOSUPPORT;
2427
2428         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2429         if (IS_ERR(set))
2430                 return PTR_ERR(set);
2431         if (!list_empty(&set->bindings))
2432                 return -EBUSY;
2433
2434         nf_tables_set_destroy(&ctx, set);
2435         return 0;
2436 }
2437
2438 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2439                                         const struct nft_set *set,
2440                                         const struct nft_set_iter *iter,
2441                                         const struct nft_set_elem *elem)
2442 {
2443         enum nft_registers dreg;
2444
2445         dreg = nft_type_to_reg(set->dtype);
2446         return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
2447 }
2448
2449 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2450                        struct nft_set_binding *binding)
2451 {
2452         struct nft_set_binding *i;
2453         struct nft_set_iter iter;
2454
2455         if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2456                 return -EBUSY;
2457
2458         if (set->flags & NFT_SET_MAP) {
2459                 /* If the set is already bound to the same chain all
2460                  * jumps are already validated for that chain.
2461                  */
2462                 list_for_each_entry(i, &set->bindings, list) {
2463                         if (i->chain == binding->chain)
2464                                 goto bind;
2465                 }
2466
2467                 iter.skip       = 0;
2468                 iter.count      = 0;
2469                 iter.err        = 0;
2470                 iter.fn         = nf_tables_bind_check_setelem;
2471
2472                 set->ops->walk(ctx, set, &iter);
2473                 if (iter.err < 0) {
2474                         /* Destroy anonymous sets if binding fails */
2475                         if (set->flags & NFT_SET_ANONYMOUS)
2476                                 nf_tables_set_destroy(ctx, set);
2477
2478                         return iter.err;
2479                 }
2480         }
2481 bind:
2482         binding->chain = ctx->chain;
2483         list_add_tail(&binding->list, &set->bindings);
2484         return 0;
2485 }
2486
2487 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2488                           struct nft_set_binding *binding)
2489 {
2490         list_del(&binding->list);
2491
2492         if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2493                 nf_tables_set_destroy(ctx, set);
2494 }
2495
2496 /*
2497  * Set elements
2498  */
2499
2500 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2501         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
2502         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
2503         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
2504 };
2505
2506 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2507         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING },
2508         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING },
2509         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
2510 };
2511
2512 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2513                                       const struct sk_buff *skb,
2514                                       const struct nlmsghdr *nlh,
2515                                       const struct nlattr * const nla[])
2516 {
2517         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2518         const struct nft_af_info *afi;
2519         const struct nft_table *table;
2520         struct net *net = sock_net(skb->sk);
2521
2522         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2523         if (IS_ERR(afi))
2524                 return PTR_ERR(afi);
2525
2526         table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2527         if (IS_ERR(table))
2528                 return PTR_ERR(table);
2529
2530         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2531         return 0;
2532 }
2533
2534 static int nf_tables_fill_setelem(struct sk_buff *skb,
2535                                   const struct nft_set *set,
2536                                   const struct nft_set_elem *elem)
2537 {
2538         unsigned char *b = skb_tail_pointer(skb);
2539         struct nlattr *nest;
2540
2541         nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2542         if (nest == NULL)
2543                 goto nla_put_failure;
2544
2545         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2546                           set->klen) < 0)
2547                 goto nla_put_failure;
2548
2549         if (set->flags & NFT_SET_MAP &&
2550             !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2551             nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2552                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2553                           set->dlen) < 0)
2554                 goto nla_put_failure;
2555
2556         if (elem->flags != 0)
2557                 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2558                         goto nla_put_failure;
2559
2560         nla_nest_end(skb, nest);
2561         return 0;
2562
2563 nla_put_failure:
2564         nlmsg_trim(skb, b);
2565         return -EMSGSIZE;
2566 }
2567
2568 struct nft_set_dump_args {
2569         const struct netlink_callback   *cb;
2570         struct nft_set_iter             iter;
2571         struct sk_buff                  *skb;
2572 };
2573
2574 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2575                                   const struct nft_set *set,
2576                                   const struct nft_set_iter *iter,
2577                                   const struct nft_set_elem *elem)
2578 {
2579         struct nft_set_dump_args *args;
2580
2581         args = container_of(iter, struct nft_set_dump_args, iter);
2582         return nf_tables_fill_setelem(args->skb, set, elem);
2583 }
2584
2585 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2586 {
2587         const struct nft_set *set;
2588         struct nft_set_dump_args args;
2589         struct nft_ctx ctx;
2590         struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2591         struct nfgenmsg *nfmsg;
2592         struct nlmsghdr *nlh;
2593         struct nlattr *nest;
2594         u32 portid, seq;
2595         int event, err;
2596
2597         err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2598                           NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2599         if (err < 0)
2600                 return err;
2601
2602         err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2603         if (err < 0)
2604                 return err;
2605
2606         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2607         if (IS_ERR(set))
2608                 return PTR_ERR(set);
2609
2610         event  = NFT_MSG_NEWSETELEM;
2611         event |= NFNL_SUBSYS_NFTABLES << 8;
2612         portid = NETLINK_CB(cb->skb).portid;
2613         seq    = cb->nlh->nlmsg_seq;
2614
2615         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2616                         NLM_F_MULTI);
2617         if (nlh == NULL)
2618                 goto nla_put_failure;
2619
2620         nfmsg = nlmsg_data(nlh);
2621         nfmsg->nfgen_family = NFPROTO_UNSPEC;
2622         nfmsg->version      = NFNETLINK_V0;
2623         nfmsg->res_id       = 0;
2624
2625         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2626                 goto nla_put_failure;
2627         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2628                 goto nla_put_failure;
2629
2630         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2631         if (nest == NULL)
2632                 goto nla_put_failure;
2633
2634         args.cb         = cb;
2635         args.skb        = skb;
2636         args.iter.skip  = cb->args[0];
2637         args.iter.count = 0;
2638         args.iter.err   = 0;
2639         args.iter.fn    = nf_tables_dump_setelem;
2640         set->ops->walk(&ctx, set, &args.iter);
2641
2642         nla_nest_end(skb, nest);
2643         nlmsg_end(skb, nlh);
2644
2645         if (args.iter.err && args.iter.err != -EMSGSIZE)
2646                 return args.iter.err;
2647         if (args.iter.count == cb->args[0])
2648                 return 0;
2649
2650         cb->args[0] = args.iter.count;
2651         return skb->len;
2652
2653 nla_put_failure:
2654         return -ENOSPC;
2655 }
2656
2657 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2658                                 const struct nlmsghdr *nlh,
2659                                 const struct nlattr * const nla[])
2660 {
2661         const struct nft_set *set;
2662         struct nft_ctx ctx;
2663         int err;
2664
2665         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2666         if (err < 0)
2667                 return err;
2668
2669         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2670         if (IS_ERR(set))
2671                 return PTR_ERR(set);
2672
2673         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2674                 struct netlink_dump_control c = {
2675                         .dump = nf_tables_dump_set,
2676                 };
2677                 return netlink_dump_start(nlsk, skb, nlh, &c);
2678         }
2679         return -EOPNOTSUPP;
2680 }
2681
2682 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2683                             const struct nlattr *attr)
2684 {
2685         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2686         struct nft_data_desc d1, d2;
2687         struct nft_set_elem elem;
2688         struct nft_set_binding *binding;
2689         enum nft_registers dreg;
2690         int err;
2691
2692         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2693                                nft_set_elem_policy);
2694         if (err < 0)
2695                 return err;
2696
2697         if (nla[NFTA_SET_ELEM_KEY] == NULL)
2698                 return -EINVAL;
2699
2700         elem.flags = 0;
2701         if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2702                 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2703                 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2704                         return -EINVAL;
2705         }
2706
2707         if (set->flags & NFT_SET_MAP) {
2708                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2709                     !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2710                         return -EINVAL;
2711         } else {
2712                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2713                         return -EINVAL;
2714         }
2715
2716         err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2717         if (err < 0)
2718                 goto err1;
2719         err = -EINVAL;
2720         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2721                 goto err2;
2722
2723         err = -EEXIST;
2724         if (set->ops->get(set, &elem) == 0)
2725                 goto err2;
2726
2727         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2728                 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2729                 if (err < 0)
2730                         goto err2;
2731
2732                 err = -EINVAL;
2733                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2734                         goto err3;
2735
2736                 dreg = nft_type_to_reg(set->dtype);
2737                 list_for_each_entry(binding, &set->bindings, list) {
2738                         struct nft_ctx bind_ctx = {
2739                                 .afi    = ctx->afi,
2740                                 .table  = ctx->table,
2741                                 .chain  = binding->chain,
2742                         };
2743
2744                         err = nft_validate_data_load(&bind_ctx, dreg,
2745                                                      &elem.data, d2.type);
2746                         if (err < 0)
2747                                 goto err3;
2748                 }
2749         }
2750
2751         err = set->ops->insert(set, &elem);
2752         if (err < 0)
2753                 goto err3;
2754
2755         return 0;
2756
2757 err3:
2758         if (nla[NFTA_SET_ELEM_DATA] != NULL)
2759                 nft_data_uninit(&elem.data, d2.type);
2760 err2:
2761         nft_data_uninit(&elem.key, d1.type);
2762 err1:
2763         return err;
2764 }
2765
2766 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2767                                 const struct nlmsghdr *nlh,
2768                                 const struct nlattr * const nla[])
2769 {
2770         const struct nlattr *attr;
2771         struct nft_set *set;
2772         struct nft_ctx ctx;
2773         int rem, err;
2774
2775         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2776         if (err < 0)
2777                 return err;
2778
2779         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2780         if (IS_ERR(set))
2781                 return PTR_ERR(set);
2782         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2783                 return -EBUSY;
2784
2785         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2786                 err = nft_add_set_elem(&ctx, set, attr);
2787                 if (err < 0)
2788                         return err;
2789         }
2790         return 0;
2791 }
2792
2793 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2794                            const struct nlattr *attr)
2795 {
2796         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2797         struct nft_data_desc desc;
2798         struct nft_set_elem elem;
2799         int err;
2800
2801         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2802                                nft_set_elem_policy);
2803         if (err < 0)
2804                 goto err1;
2805
2806         err = -EINVAL;
2807         if (nla[NFTA_SET_ELEM_KEY] == NULL)
2808                 goto err1;
2809
2810         err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2811         if (err < 0)
2812                 goto err1;
2813
2814         err = -EINVAL;
2815         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2816                 goto err2;
2817
2818         err = set->ops->get(set, &elem);
2819         if (err < 0)
2820                 goto err2;
2821
2822         set->ops->remove(set, &elem);
2823
2824         nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2825         if (set->flags & NFT_SET_MAP)
2826                 nft_data_uninit(&elem.data, set->dtype);
2827
2828 err2:
2829         nft_data_uninit(&elem.key, desc.type);
2830 err1:
2831         return err;
2832 }
2833
2834 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2835                                 const struct nlmsghdr *nlh,
2836                                 const struct nlattr * const nla[])
2837 {
2838         const struct nlattr *attr;
2839         struct nft_set *set;
2840         struct nft_ctx ctx;
2841         int rem, err;
2842
2843         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2844         if (err < 0)
2845                 return err;
2846
2847         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2848         if (IS_ERR(set))
2849                 return PTR_ERR(set);
2850         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2851                 return -EBUSY;
2852
2853         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2854                 err = nft_del_setelem(&ctx, set, attr);
2855                 if (err < 0)
2856                         return err;
2857         }
2858         return 0;
2859 }
2860
2861 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2862         [NFT_MSG_NEWTABLE] = {
2863                 .call           = nf_tables_newtable,
2864                 .attr_count     = NFTA_TABLE_MAX,
2865                 .policy         = nft_table_policy,
2866         },
2867         [NFT_MSG_GETTABLE] = {
2868                 .call           = nf_tables_gettable,
2869                 .attr_count     = NFTA_TABLE_MAX,
2870                 .policy         = nft_table_policy,
2871         },
2872         [NFT_MSG_DELTABLE] = {
2873                 .call           = nf_tables_deltable,
2874                 .attr_count     = NFTA_TABLE_MAX,
2875                 .policy         = nft_table_policy,
2876         },
2877         [NFT_MSG_NEWCHAIN] = {
2878                 .call           = nf_tables_newchain,
2879                 .attr_count     = NFTA_CHAIN_MAX,
2880                 .policy         = nft_chain_policy,
2881         },
2882         [NFT_MSG_GETCHAIN] = {
2883                 .call           = nf_tables_getchain,
2884                 .attr_count     = NFTA_CHAIN_MAX,
2885                 .policy         = nft_chain_policy,
2886         },
2887         [NFT_MSG_DELCHAIN] = {
2888                 .call           = nf_tables_delchain,
2889                 .attr_count     = NFTA_CHAIN_MAX,
2890                 .policy         = nft_chain_policy,
2891         },
2892         [NFT_MSG_NEWRULE] = {
2893                 .call_batch     = nf_tables_newrule,
2894                 .attr_count     = NFTA_RULE_MAX,
2895                 .policy         = nft_rule_policy,
2896         },
2897         [NFT_MSG_GETRULE] = {
2898                 .call           = nf_tables_getrule,
2899                 .attr_count     = NFTA_RULE_MAX,
2900                 .policy         = nft_rule_policy,
2901         },
2902         [NFT_MSG_DELRULE] = {
2903                 .call_batch     = nf_tables_delrule,
2904                 .attr_count     = NFTA_RULE_MAX,
2905                 .policy         = nft_rule_policy,
2906         },
2907         [NFT_MSG_NEWSET] = {
2908                 .call           = nf_tables_newset,
2909                 .attr_count     = NFTA_SET_MAX,
2910                 .policy         = nft_set_policy,
2911         },
2912         [NFT_MSG_GETSET] = {
2913                 .call           = nf_tables_getset,
2914                 .attr_count     = NFTA_SET_MAX,
2915                 .policy         = nft_set_policy,
2916         },
2917         [NFT_MSG_DELSET] = {
2918                 .call           = nf_tables_delset,
2919                 .attr_count     = NFTA_SET_MAX,
2920                 .policy         = nft_set_policy,
2921         },
2922         [NFT_MSG_NEWSETELEM] = {
2923                 .call           = nf_tables_newsetelem,
2924                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2925                 .policy         = nft_set_elem_list_policy,
2926         },
2927         [NFT_MSG_GETSETELEM] = {
2928                 .call           = nf_tables_getsetelem,
2929                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2930                 .policy         = nft_set_elem_list_policy,
2931         },
2932         [NFT_MSG_DELSETELEM] = {
2933                 .call           = nf_tables_delsetelem,
2934                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2935                 .policy         = nft_set_elem_list_policy,
2936         },
2937 };
2938
2939 static const struct nfnetlink_subsystem nf_tables_subsys = {
2940         .name           = "nf_tables",
2941         .subsys_id      = NFNL_SUBSYS_NFTABLES,
2942         .cb_count       = NFT_MSG_MAX,
2943         .cb             = nf_tables_cb,
2944         .commit         = nf_tables_commit,
2945         .abort          = nf_tables_abort,
2946 };
2947
2948 /*
2949  * Loop detection - walk through the ruleset beginning at the destination chain
2950  * of a new jump until either the source chain is reached (loop) or all
2951  * reachable chains have been traversed.
2952  *
2953  * The loop check is performed whenever a new jump verdict is added to an
2954  * expression or verdict map or a verdict map is bound to a new chain.
2955  */
2956
2957 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2958                                  const struct nft_chain *chain);
2959
2960 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2961                                         const struct nft_set *set,
2962                                         const struct nft_set_iter *iter,
2963                                         const struct nft_set_elem *elem)
2964 {
2965         switch (elem->data.verdict) {
2966         case NFT_JUMP:
2967         case NFT_GOTO:
2968                 return nf_tables_check_loops(ctx, elem->data.chain);
2969         default:
2970                 return 0;
2971         }
2972 }
2973
2974 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2975                                  const struct nft_chain *chain)
2976 {
2977         const struct nft_rule *rule;
2978         const struct nft_expr *expr, *last;
2979         const struct nft_set *set;
2980         struct nft_set_binding *binding;
2981         struct nft_set_iter iter;
2982
2983         if (ctx->chain == chain)
2984                 return -ELOOP;
2985
2986         list_for_each_entry(rule, &chain->rules, list) {
2987                 nft_rule_for_each_expr(expr, last, rule) {
2988                         const struct nft_data *data = NULL;
2989                         int err;
2990
2991                         if (!expr->ops->validate)
2992                                 continue;
2993
2994                         err = expr->ops->validate(ctx, expr, &data);
2995                         if (err < 0)
2996                                 return err;
2997
2998                         if (data == NULL)
2999                                 continue;
3000
3001                         switch (data->verdict) {
3002                         case NFT_JUMP:
3003                         case NFT_GOTO:
3004                                 err = nf_tables_check_loops(ctx, data->chain);
3005                                 if (err < 0)
3006                                         return err;
3007                         default:
3008                                 break;
3009                         }
3010                 }
3011         }
3012
3013         list_for_each_entry(set, &ctx->table->sets, list) {
3014                 if (!(set->flags & NFT_SET_MAP) ||
3015                     set->dtype != NFT_DATA_VERDICT)
3016                         continue;
3017
3018                 list_for_each_entry(binding, &set->bindings, list) {
3019                         if (binding->chain != chain)
3020                                 continue;
3021
3022                         iter.skip       = 0;
3023                         iter.count      = 0;
3024                         iter.err        = 0;
3025                         iter.fn         = nf_tables_loop_check_setelem;
3026
3027                         set->ops->walk(ctx, set, &iter);
3028                         if (iter.err < 0)
3029                                 return iter.err;
3030                 }
3031         }
3032
3033         return 0;
3034 }
3035
3036 /**
3037  *      nft_validate_input_register - validate an expressions' input register
3038  *
3039  *      @reg: the register number
3040  *
3041  *      Validate that the input register is one of the general purpose
3042  *      registers.
3043  */
3044 int nft_validate_input_register(enum nft_registers reg)
3045 {
3046         if (reg <= NFT_REG_VERDICT)
3047                 return -EINVAL;
3048         if (reg > NFT_REG_MAX)
3049                 return -ERANGE;
3050         return 0;
3051 }
3052 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3053
3054 /**
3055  *      nft_validate_output_register - validate an expressions' output register
3056  *
3057  *      @reg: the register number
3058  *
3059  *      Validate that the output register is one of the general purpose
3060  *      registers or the verdict register.
3061  */
3062 int nft_validate_output_register(enum nft_registers reg)
3063 {
3064         if (reg < NFT_REG_VERDICT)
3065                 return -EINVAL;
3066         if (reg > NFT_REG_MAX)
3067                 return -ERANGE;
3068         return 0;
3069 }
3070 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3071
3072 /**
3073  *      nft_validate_data_load - validate an expressions' data load
3074  *
3075  *      @ctx: context of the expression performing the load
3076  *      @reg: the destination register number
3077  *      @data: the data to load
3078  *      @type: the data type
3079  *
3080  *      Validate that a data load uses the appropriate data type for
3081  *      the destination register. A value of NULL for the data means
3082  *      that its runtime gathered data, which is always of type
3083  *      NFT_DATA_VALUE.
3084  */
3085 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3086                            const struct nft_data *data,
3087                            enum nft_data_types type)
3088 {
3089         int err;
3090
3091         switch (reg) {
3092         case NFT_REG_VERDICT:
3093                 if (data == NULL || type != NFT_DATA_VERDICT)
3094                         return -EINVAL;
3095
3096                 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3097                         err = nf_tables_check_loops(ctx, data->chain);
3098                         if (err < 0)
3099                                 return err;
3100
3101                         if (ctx->chain->level + 1 > data->chain->level) {
3102                                 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3103                                         return -EMLINK;
3104                                 data->chain->level = ctx->chain->level + 1;
3105                         }
3106                 }
3107
3108                 return 0;
3109         default:
3110                 if (data != NULL && type != NFT_DATA_VALUE)
3111                         return -EINVAL;
3112                 return 0;
3113         }
3114 }
3115 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3116
3117 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3118         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
3119         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
3120                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
3121 };
3122
3123 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3124                             struct nft_data_desc *desc, const struct nlattr *nla)
3125 {
3126         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3127         struct nft_chain *chain;
3128         int err;
3129
3130         err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3131         if (err < 0)
3132                 return err;
3133
3134         if (!tb[NFTA_VERDICT_CODE])
3135                 return -EINVAL;
3136         data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3137
3138         switch (data->verdict) {
3139         case NF_ACCEPT:
3140         case NF_DROP:
3141         case NF_QUEUE:
3142         case NFT_CONTINUE:
3143         case NFT_BREAK:
3144         case NFT_RETURN:
3145                 desc->len = sizeof(data->verdict);
3146                 break;
3147         case NFT_JUMP:
3148         case NFT_GOTO:
3149                 if (!tb[NFTA_VERDICT_CHAIN])
3150                         return -EINVAL;
3151                 chain = nf_tables_chain_lookup(ctx->table,
3152                                                tb[NFTA_VERDICT_CHAIN]);
3153                 if (IS_ERR(chain))
3154                         return PTR_ERR(chain);
3155                 if (chain->flags & NFT_BASE_CHAIN)
3156                         return -EOPNOTSUPP;
3157
3158                 chain->use++;
3159                 data->chain = chain;
3160                 desc->len = sizeof(data);
3161                 break;
3162         default:
3163                 return -EINVAL;
3164         }
3165
3166         desc->type = NFT_DATA_VERDICT;
3167         return 0;
3168 }
3169
3170 static void nft_verdict_uninit(const struct nft_data *data)
3171 {
3172         switch (data->verdict) {
3173         case NFT_JUMP:
3174         case NFT_GOTO:
3175                 data->chain->use--;
3176                 break;
3177         }
3178 }
3179
3180 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3181 {
3182         struct nlattr *nest;
3183
3184         nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3185         if (!nest)
3186                 goto nla_put_failure;
3187
3188         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3189                 goto nla_put_failure;
3190
3191         switch (data->verdict) {
3192         case NFT_JUMP:
3193         case NFT_GOTO:
3194                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3195                         goto nla_put_failure;
3196         }
3197         nla_nest_end(skb, nest);
3198         return 0;
3199
3200 nla_put_failure:
3201         return -1;
3202 }
3203
3204 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3205                           struct nft_data_desc *desc, const struct nlattr *nla)
3206 {
3207         unsigned int len;
3208
3209         len = nla_len(nla);
3210         if (len == 0)
3211                 return -EINVAL;
3212         if (len > sizeof(data->data))
3213                 return -EOVERFLOW;
3214
3215         nla_memcpy(data->data, nla, sizeof(data->data));
3216         desc->type = NFT_DATA_VALUE;
3217         desc->len  = len;
3218         return 0;
3219 }
3220
3221 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3222                           unsigned int len)
3223 {
3224         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3225 }
3226
3227 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3228         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY,
3229                                     .len  = FIELD_SIZEOF(struct nft_data, data) },
3230         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
3231 };
3232
3233 /**
3234  *      nft_data_init - parse nf_tables data netlink attributes
3235  *
3236  *      @ctx: context of the expression using the data
3237  *      @data: destination struct nft_data
3238  *      @desc: data description
3239  *      @nla: netlink attribute containing data
3240  *
3241  *      Parse the netlink data attributes and initialize a struct nft_data.
3242  *      The type and length of data are returned in the data description.
3243  *
3244  *      The caller can indicate that it only wants to accept data of type
3245  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
3246  */
3247 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3248                   struct nft_data_desc *desc, const struct nlattr *nla)
3249 {
3250         struct nlattr *tb[NFTA_DATA_MAX + 1];
3251         int err;
3252
3253         err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3254         if (err < 0)
3255                 return err;
3256
3257         if (tb[NFTA_DATA_VALUE])
3258                 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3259         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3260                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3261         return -EINVAL;
3262 }
3263 EXPORT_SYMBOL_GPL(nft_data_init);
3264
3265 /**
3266  *      nft_data_uninit - release a nft_data item
3267  *
3268  *      @data: struct nft_data to release
3269  *      @type: type of data
3270  *
3271  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3272  *      all others need to be released by calling this function.
3273  */
3274 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3275 {
3276         switch (type) {
3277         case NFT_DATA_VALUE:
3278                 return;
3279         case NFT_DATA_VERDICT:
3280                 return nft_verdict_uninit(data);
3281         default:
3282                 WARN_ON(1);
3283         }
3284 }
3285 EXPORT_SYMBOL_GPL(nft_data_uninit);
3286
3287 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3288                   enum nft_data_types type, unsigned int len)
3289 {
3290         struct nlattr *nest;
3291         int err;
3292
3293         nest = nla_nest_start(skb, attr);
3294         if (nest == NULL)
3295                 return -1;
3296
3297         switch (type) {
3298         case NFT_DATA_VALUE:
3299                 err = nft_value_dump(skb, data, len);
3300                 break;
3301         case NFT_DATA_VERDICT:
3302                 err = nft_verdict_dump(skb, data);
3303                 break;
3304         default:
3305                 err = -EINVAL;
3306                 WARN_ON(1);
3307         }
3308
3309         nla_nest_end(skb, nest);
3310         return err;
3311 }
3312 EXPORT_SYMBOL_GPL(nft_data_dump);
3313
3314 static int nf_tables_init_net(struct net *net)
3315 {
3316         INIT_LIST_HEAD(&net->nft.af_info);
3317         INIT_LIST_HEAD(&net->nft.commit_list);
3318         return 0;
3319 }
3320
3321 static struct pernet_operations nf_tables_net_ops = {
3322         .init   = nf_tables_init_net,
3323 };
3324
3325 static int __init nf_tables_module_init(void)
3326 {
3327         int err;
3328
3329         info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3330                        GFP_KERNEL);
3331         if (info == NULL) {
3332                 err = -ENOMEM;
3333                 goto err1;
3334         }
3335
3336         err = nf_tables_core_module_init();
3337         if (err < 0)
3338                 goto err2;
3339
3340         err = nfnetlink_subsys_register(&nf_tables_subsys);
3341         if (err < 0)
3342                 goto err3;
3343
3344         pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3345         return register_pernet_subsys(&nf_tables_net_ops);
3346 err3:
3347         nf_tables_core_module_exit();
3348 err2:
3349         kfree(info);
3350 err1:
3351         return err;
3352 }
3353
3354 static void __exit nf_tables_module_exit(void)
3355 {
3356         unregister_pernet_subsys(&nf_tables_net_ops);
3357         nfnetlink_subsys_unregister(&nf_tables_subsys);
3358         nf_tables_core_module_exit();
3359         kfree(info);
3360 }
3361
3362 module_init(nf_tables_module_init);
3363 module_exit(nf_tables_module_exit);
3364
3365 MODULE_LICENSE("GPL");
3366 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3367 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);