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