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