Merge branch 'core-debug-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / net / netfilter / nf_tables_api.c
1 /*
2  * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Development of this code funded by Astaro AG (http://www.astaro.com/)
9  */
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
22 #include <net/sock.h>
23
24 static LIST_HEAD(nf_tables_expressions);
25
26 /**
27  *      nft_register_afinfo - register nf_tables address family info
28  *
29  *      @afi: address family info to register
30  *
31  *      Register the address family for use with nf_tables. Returns zero on
32  *      success or a negative errno code otherwise.
33  */
34 int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
35 {
36         INIT_LIST_HEAD(&afi->tables);
37         nfnl_lock(NFNL_SUBSYS_NFTABLES);
38         list_add_tail(&afi->list, &net->nft.af_info);
39         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40         return 0;
41 }
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44 /**
45  *      nft_unregister_afinfo - unregister nf_tables address family info
46  *
47  *      @afi: address family info to unregister
48  *
49  *      Unregister the address family for use with nf_tables.
50  */
51 void nft_unregister_afinfo(struct nft_af_info *afi)
52 {
53         nfnl_lock(NFNL_SUBSYS_NFTABLES);
54         list_del(&afi->list);
55         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56 }
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
59 static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
60 {
61         struct nft_af_info *afi;
62
63         list_for_each_entry(afi, &net->nft.af_info, list) {
64                 if (afi->family == family)
65                         return afi;
66         }
67         return NULL;
68 }
69
70 static struct nft_af_info *
71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
72 {
73         struct nft_af_info *afi;
74
75         afi = nft_afinfo_lookup(net, family);
76         if (afi != NULL)
77                 return afi;
78 #ifdef CONFIG_MODULES
79         if (autoload) {
80                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81                 request_module("nft-afinfo-%u", family);
82                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
83                 afi = nft_afinfo_lookup(net, family);
84                 if (afi != NULL)
85                         return ERR_PTR(-EAGAIN);
86         }
87 #endif
88         return ERR_PTR(-EAFNOSUPPORT);
89 }
90
91 /*
92  * Tables
93  */
94
95 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96                                           const struct nlattr *nla)
97 {
98         struct nft_table *table;
99
100         list_for_each_entry(table, &afi->tables, list) {
101                 if (!nla_strcmp(nla, table->name))
102                         return table;
103         }
104         return NULL;
105 }
106
107 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
108                                                 const struct nlattr *nla)
109 {
110         struct nft_table *table;
111
112         if (nla == NULL)
113                 return ERR_PTR(-EINVAL);
114
115         table = nft_table_lookup(afi, nla);
116         if (table != NULL)
117                 return table;
118
119         return ERR_PTR(-ENOENT);
120 }
121
122 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
123 {
124         return ++table->hgenerator;
125 }
126
127 static struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
128
129 static int __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
130 {
131         int i;
132
133         for (i=0; i<NFT_CHAIN_T_MAX; i++) {
134                 if (chain_type[family][i] != NULL &&
135                     !nla_strcmp(nla, chain_type[family][i]->name))
136                         return i;
137         }
138         return -1;
139 }
140
141 static int nf_tables_chain_type_lookup(const struct nft_af_info *afi,
142                                        const struct nlattr *nla,
143                                        bool autoload)
144 {
145         int type;
146
147         type = __nf_tables_chain_type_lookup(afi->family, nla);
148 #ifdef CONFIG_MODULES
149         if (type < 0 && autoload) {
150                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
151                 request_module("nft-chain-%u-%*.s", afi->family,
152                                nla_len(nla)-1, (const char *)nla_data(nla));
153                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
154                 type = __nf_tables_chain_type_lookup(afi->family, nla);
155         }
156 #endif
157         return type;
158 }
159
160 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
161         [NFTA_TABLE_NAME]       = { .type = NLA_STRING },
162         [NFTA_TABLE_FLAGS]      = { .type = NLA_U32 },
163 };
164
165 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
166                                      int event, u32 flags, int family,
167                                      const struct nft_table *table)
168 {
169         struct nlmsghdr *nlh;
170         struct nfgenmsg *nfmsg;
171
172         event |= NFNL_SUBSYS_NFTABLES << 8;
173         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
174         if (nlh == NULL)
175                 goto nla_put_failure;
176
177         nfmsg = nlmsg_data(nlh);
178         nfmsg->nfgen_family     = family;
179         nfmsg->version          = NFNETLINK_V0;
180         nfmsg->res_id           = 0;
181
182         if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
183             nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)))
184                 goto nla_put_failure;
185
186         return nlmsg_end(skb, nlh);
187
188 nla_put_failure:
189         nlmsg_trim(skb, nlh);
190         return -1;
191 }
192
193 static int nf_tables_table_notify(const struct sk_buff *oskb,
194                                   const struct nlmsghdr *nlh,
195                                   const struct nft_table *table,
196                                   int event, int family)
197 {
198         struct sk_buff *skb;
199         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
200         u32 seq = nlh ? nlh->nlmsg_seq : 0;
201         struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
202         bool report;
203         int err;
204
205         report = nlh ? nlmsg_report(nlh) : false;
206         if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
207                 return 0;
208
209         err = -ENOBUFS;
210         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
211         if (skb == NULL)
212                 goto err;
213
214         err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
215                                         family, table);
216         if (err < 0) {
217                 kfree_skb(skb);
218                 goto err;
219         }
220
221         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
222                              GFP_KERNEL);
223 err:
224         if (err < 0)
225                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
226         return err;
227 }
228
229 static int nf_tables_dump_tables(struct sk_buff *skb,
230                                  struct netlink_callback *cb)
231 {
232         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
233         const struct nft_af_info *afi;
234         const struct nft_table *table;
235         unsigned int idx = 0, s_idx = cb->args[0];
236         struct net *net = sock_net(skb->sk);
237         int family = nfmsg->nfgen_family;
238
239         list_for_each_entry(afi, &net->nft.af_info, list) {
240                 if (family != NFPROTO_UNSPEC && family != afi->family)
241                         continue;
242
243                 list_for_each_entry(table, &afi->tables, list) {
244                         if (idx < s_idx)
245                                 goto cont;
246                         if (idx > s_idx)
247                                 memset(&cb->args[1], 0,
248                                        sizeof(cb->args) - sizeof(cb->args[0]));
249                         if (nf_tables_fill_table_info(skb,
250                                                       NETLINK_CB(cb->skb).portid,
251                                                       cb->nlh->nlmsg_seq,
252                                                       NFT_MSG_NEWTABLE,
253                                                       NLM_F_MULTI,
254                                                       afi->family, table) < 0)
255                                 goto done;
256 cont:
257                         idx++;
258                 }
259         }
260 done:
261         cb->args[0] = idx;
262         return skb->len;
263 }
264
265 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
266                               const struct nlmsghdr *nlh,
267                               const struct nlattr * const nla[])
268 {
269         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
270         const struct nft_af_info *afi;
271         const struct nft_table *table;
272         struct sk_buff *skb2;
273         struct net *net = sock_net(skb->sk);
274         int family = nfmsg->nfgen_family;
275         int err;
276
277         if (nlh->nlmsg_flags & NLM_F_DUMP) {
278                 struct netlink_dump_control c = {
279                         .dump = nf_tables_dump_tables,
280                 };
281                 return netlink_dump_start(nlsk, skb, nlh, &c);
282         }
283
284         afi = nf_tables_afinfo_lookup(net, family, false);
285         if (IS_ERR(afi))
286                 return PTR_ERR(afi);
287
288         table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
289         if (IS_ERR(table))
290                 return PTR_ERR(table);
291
292         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
293         if (!skb2)
294                 return -ENOMEM;
295
296         err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
297                                         nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
298                                         family, table);
299         if (err < 0)
300                 goto err;
301
302         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
303
304 err:
305         kfree_skb(skb2);
306         return err;
307 }
308
309 static int nf_tables_table_enable(struct nft_table *table)
310 {
311         struct nft_chain *chain;
312         int err, i = 0;
313
314         list_for_each_entry(chain, &table->chains, list) {
315                 if (!(chain->flags & NFT_BASE_CHAIN))
316                         continue;
317
318                 err = nf_register_hook(&nft_base_chain(chain)->ops);
319                 if (err < 0)
320                         goto err;
321
322                 i++;
323         }
324         return 0;
325 err:
326         list_for_each_entry(chain, &table->chains, list) {
327                 if (!(chain->flags & NFT_BASE_CHAIN))
328                         continue;
329
330                 if (i-- <= 0)
331                         break;
332
333                 nf_unregister_hook(&nft_base_chain(chain)->ops);
334         }
335         return err;
336 }
337
338 static int nf_tables_table_disable(struct nft_table *table)
339 {
340         struct nft_chain *chain;
341
342         list_for_each_entry(chain, &table->chains, list) {
343                 if (chain->flags & NFT_BASE_CHAIN)
344                         nf_unregister_hook(&nft_base_chain(chain)->ops);
345         }
346
347         return 0;
348 }
349
350 static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
351                               const struct nlmsghdr *nlh,
352                               const struct nlattr * const nla[],
353                               struct nft_af_info *afi, struct nft_table *table)
354 {
355         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
356         int family = nfmsg->nfgen_family, ret = 0;
357
358         if (nla[NFTA_TABLE_FLAGS]) {
359                 __be32 flags;
360
361                 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
362                 if (flags & ~NFT_TABLE_F_DORMANT)
363                         return -EINVAL;
364
365                 if ((flags & NFT_TABLE_F_DORMANT) &&
366                     !(table->flags & NFT_TABLE_F_DORMANT)) {
367                         ret = nf_tables_table_disable(table);
368                         if (ret >= 0)
369                                 table->flags |= NFT_TABLE_F_DORMANT;
370                 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
371                            table->flags & NFT_TABLE_F_DORMANT) {
372                         ret = nf_tables_table_enable(table);
373                         if (ret >= 0)
374                                 table->flags &= ~NFT_TABLE_F_DORMANT;
375                 }
376                 if (ret < 0)
377                         goto err;
378         }
379
380         nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
381 err:
382         return ret;
383 }
384
385 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
386                               const struct nlmsghdr *nlh,
387                               const struct nlattr * const nla[])
388 {
389         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
390         const struct nlattr *name;
391         struct nft_af_info *afi;
392         struct nft_table *table;
393         struct net *net = sock_net(skb->sk);
394         int family = nfmsg->nfgen_family;
395
396         afi = nf_tables_afinfo_lookup(net, family, true);
397         if (IS_ERR(afi))
398                 return PTR_ERR(afi);
399
400         name = nla[NFTA_TABLE_NAME];
401         table = nf_tables_table_lookup(afi, name);
402         if (IS_ERR(table)) {
403                 if (PTR_ERR(table) != -ENOENT)
404                         return PTR_ERR(table);
405                 table = NULL;
406         }
407
408         if (table != NULL) {
409                 if (nlh->nlmsg_flags & NLM_F_EXCL)
410                         return -EEXIST;
411                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
412                         return -EOPNOTSUPP;
413                 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
414         }
415
416         table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
417         if (table == NULL)
418                 return -ENOMEM;
419
420         nla_strlcpy(table->name, name, nla_len(name));
421         INIT_LIST_HEAD(&table->chains);
422         INIT_LIST_HEAD(&table->sets);
423
424         if (nla[NFTA_TABLE_FLAGS]) {
425                 __be32 flags;
426
427                 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
428                 if (flags & ~NFT_TABLE_F_DORMANT) {
429                         kfree(table);
430                         return -EINVAL;
431                 }
432
433                 table->flags |= flags;
434         }
435
436         list_add_tail(&table->list, &afi->tables);
437         nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
438         return 0;
439 }
440
441 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
442                               const struct nlmsghdr *nlh,
443                               const struct nlattr * const nla[])
444 {
445         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
446         struct nft_af_info *afi;
447         struct nft_table *table;
448         struct net *net = sock_net(skb->sk);
449         int family = nfmsg->nfgen_family;
450
451         afi = nf_tables_afinfo_lookup(net, family, false);
452         if (IS_ERR(afi))
453                 return PTR_ERR(afi);
454
455         table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
456         if (IS_ERR(table))
457                 return PTR_ERR(table);
458
459         if (table->use)
460                 return -EBUSY;
461
462         list_del(&table->list);
463         nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
464         kfree(table);
465         return 0;
466 }
467
468 int nft_register_chain_type(struct nf_chain_type *ctype)
469 {
470         int err = 0;
471
472         nfnl_lock(NFNL_SUBSYS_NFTABLES);
473         if (chain_type[ctype->family][ctype->type] != NULL) {
474                 err = -EBUSY;
475                 goto out;
476         }
477
478         if (!try_module_get(ctype->me))
479                 goto out;
480
481         chain_type[ctype->family][ctype->type] = ctype;
482 out:
483         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
484         return err;
485 }
486 EXPORT_SYMBOL_GPL(nft_register_chain_type);
487
488 void nft_unregister_chain_type(struct nf_chain_type *ctype)
489 {
490         nfnl_lock(NFNL_SUBSYS_NFTABLES);
491         chain_type[ctype->family][ctype->type] = NULL;
492         module_put(ctype->me);
493         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
494 }
495 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
496
497 /*
498  * Chains
499  */
500
501 static struct nft_chain *
502 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
503 {
504         struct nft_chain *chain;
505
506         list_for_each_entry(chain, &table->chains, list) {
507                 if (chain->handle == handle)
508                         return chain;
509         }
510
511         return ERR_PTR(-ENOENT);
512 }
513
514 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
515                                                 const struct nlattr *nla)
516 {
517         struct nft_chain *chain;
518
519         if (nla == NULL)
520                 return ERR_PTR(-EINVAL);
521
522         list_for_each_entry(chain, &table->chains, list) {
523                 if (!nla_strcmp(nla, chain->name))
524                         return chain;
525         }
526
527         return ERR_PTR(-ENOENT);
528 }
529
530 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
531         [NFTA_CHAIN_TABLE]      = { .type = NLA_STRING },
532         [NFTA_CHAIN_HANDLE]     = { .type = NLA_U64 },
533         [NFTA_CHAIN_NAME]       = { .type = NLA_STRING,
534                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
535         [NFTA_CHAIN_HOOK]       = { .type = NLA_NESTED },
536         [NFTA_CHAIN_POLICY]     = { .type = NLA_U32 },
537         [NFTA_CHAIN_TYPE]       = { .type = NLA_NUL_STRING },
538         [NFTA_CHAIN_COUNTERS]   = { .type = NLA_NESTED },
539 };
540
541 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
542         [NFTA_HOOK_HOOKNUM]     = { .type = NLA_U32 },
543         [NFTA_HOOK_PRIORITY]    = { .type = NLA_U32 },
544 };
545
546 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
547 {
548         struct nft_stats *cpu_stats, total;
549         struct nlattr *nest;
550         int cpu;
551
552         memset(&total, 0, sizeof(total));
553         for_each_possible_cpu(cpu) {
554                 cpu_stats = per_cpu_ptr(stats, cpu);
555                 total.pkts += cpu_stats->pkts;
556                 total.bytes += cpu_stats->bytes;
557         }
558         nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
559         if (nest == NULL)
560                 goto nla_put_failure;
561
562         if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
563             nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
564                 goto nla_put_failure;
565
566         nla_nest_end(skb, nest);
567         return 0;
568
569 nla_put_failure:
570         return -ENOSPC;
571 }
572
573 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
574                                      int event, u32 flags, int family,
575                                      const struct nft_table *table,
576                                      const struct nft_chain *chain)
577 {
578         struct nlmsghdr *nlh;
579         struct nfgenmsg *nfmsg;
580
581         event |= NFNL_SUBSYS_NFTABLES << 8;
582         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
583         if (nlh == NULL)
584                 goto nla_put_failure;
585
586         nfmsg = nlmsg_data(nlh);
587         nfmsg->nfgen_family     = family;
588         nfmsg->version          = NFNETLINK_V0;
589         nfmsg->res_id           = 0;
590
591         if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
592                 goto nla_put_failure;
593         if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
594                 goto nla_put_failure;
595         if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
596                 goto nla_put_failure;
597
598         if (chain->flags & NFT_BASE_CHAIN) {
599                 const struct nft_base_chain *basechain = nft_base_chain(chain);
600                 const struct nf_hook_ops *ops = &basechain->ops;
601                 struct nlattr *nest;
602
603                 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
604                 if (nest == NULL)
605                         goto nla_put_failure;
606                 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
607                         goto nla_put_failure;
608                 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
609                         goto nla_put_failure;
610                 nla_nest_end(skb, nest);
611
612                 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
613                                  htonl(basechain->policy)))
614                         goto nla_put_failure;
615
616                 if (nla_put_string(skb, NFTA_CHAIN_TYPE,
617                         chain_type[ops->pf][nft_base_chain(chain)->type]->name))
618                                 goto nla_put_failure;
619
620                 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
621                         goto nla_put_failure;
622         }
623
624         if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
625                 goto nla_put_failure;
626
627         return nlmsg_end(skb, nlh);
628
629 nla_put_failure:
630         nlmsg_trim(skb, nlh);
631         return -1;
632 }
633
634 static int nf_tables_chain_notify(const struct sk_buff *oskb,
635                                   const struct nlmsghdr *nlh,
636                                   const struct nft_table *table,
637                                   const struct nft_chain *chain,
638                                   int event, int family)
639 {
640         struct sk_buff *skb;
641         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
642         struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
643         u32 seq = nlh ? nlh->nlmsg_seq : 0;
644         bool report;
645         int err;
646
647         report = nlh ? nlmsg_report(nlh) : false;
648         if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
649                 return 0;
650
651         err = -ENOBUFS;
652         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
653         if (skb == NULL)
654                 goto err;
655
656         err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
657                                         table, chain);
658         if (err < 0) {
659                 kfree_skb(skb);
660                 goto err;
661         }
662
663         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
664                              GFP_KERNEL);
665 err:
666         if (err < 0)
667                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
668         return err;
669 }
670
671 static int nf_tables_dump_chains(struct sk_buff *skb,
672                                  struct netlink_callback *cb)
673 {
674         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
675         const struct nft_af_info *afi;
676         const struct nft_table *table;
677         const struct nft_chain *chain;
678         unsigned int idx = 0, s_idx = cb->args[0];
679         struct net *net = sock_net(skb->sk);
680         int family = nfmsg->nfgen_family;
681
682         list_for_each_entry(afi, &net->nft.af_info, list) {
683                 if (family != NFPROTO_UNSPEC && family != afi->family)
684                         continue;
685
686                 list_for_each_entry(table, &afi->tables, list) {
687                         list_for_each_entry(chain, &table->chains, list) {
688                                 if (idx < s_idx)
689                                         goto cont;
690                                 if (idx > s_idx)
691                                         memset(&cb->args[1], 0,
692                                                sizeof(cb->args) - sizeof(cb->args[0]));
693                                 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
694                                                               cb->nlh->nlmsg_seq,
695                                                               NFT_MSG_NEWCHAIN,
696                                                               NLM_F_MULTI,
697                                                               afi->family, table, chain) < 0)
698                                         goto done;
699 cont:
700                                 idx++;
701                         }
702                 }
703         }
704 done:
705         cb->args[0] = idx;
706         return skb->len;
707 }
708
709
710 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
711                               const struct nlmsghdr *nlh,
712                               const struct nlattr * const nla[])
713 {
714         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
715         const struct nft_af_info *afi;
716         const struct nft_table *table;
717         const struct nft_chain *chain;
718         struct sk_buff *skb2;
719         struct net *net = sock_net(skb->sk);
720         int family = nfmsg->nfgen_family;
721         int err;
722
723         if (nlh->nlmsg_flags & NLM_F_DUMP) {
724                 struct netlink_dump_control c = {
725                         .dump = nf_tables_dump_chains,
726                 };
727                 return netlink_dump_start(nlsk, skb, nlh, &c);
728         }
729
730         afi = nf_tables_afinfo_lookup(net, family, false);
731         if (IS_ERR(afi))
732                 return PTR_ERR(afi);
733
734         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
735         if (IS_ERR(table))
736                 return PTR_ERR(table);
737
738         chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
739         if (IS_ERR(chain))
740                 return PTR_ERR(chain);
741
742         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
743         if (!skb2)
744                 return -ENOMEM;
745
746         err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
747                                         nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
748                                         family, table, chain);
749         if (err < 0)
750                 goto err;
751
752         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
753
754 err:
755         kfree_skb(skb2);
756         return err;
757 }
758
759 static int
760 nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
761 {
762         switch (ntohl(nla_get_be32(attr))) {
763         case NF_DROP:
764                 chain->policy = NF_DROP;
765                 break;
766         case NF_ACCEPT:
767                 chain->policy = NF_ACCEPT;
768                 break;
769         default:
770                 return -EINVAL;
771         }
772         return 0;
773 }
774
775 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
776         [NFTA_COUNTER_PACKETS]  = { .type = NLA_U64 },
777         [NFTA_COUNTER_BYTES]    = { .type = NLA_U64 },
778 };
779
780 static int
781 nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
782 {
783         struct nlattr *tb[NFTA_COUNTER_MAX+1];
784         struct nft_stats __percpu *newstats;
785         struct nft_stats *stats;
786         int err;
787
788         err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
789         if (err < 0)
790                 return err;
791
792         if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
793                 return -EINVAL;
794
795         newstats = alloc_percpu(struct nft_stats);
796         if (newstats == NULL)
797                 return -ENOMEM;
798
799         /* Restore old counters on this cpu, no problem. Per-cpu statistics
800          * are not exposed to userspace.
801          */
802         stats = this_cpu_ptr(newstats);
803         stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
804         stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
805
806         if (chain->stats) {
807                 /* nfnl_lock is held, add some nfnl function for this, later */
808                 struct nft_stats __percpu *oldstats =
809                         rcu_dereference_protected(chain->stats, 1);
810
811                 rcu_assign_pointer(chain->stats, newstats);
812                 synchronize_rcu();
813                 free_percpu(oldstats);
814         } else
815                 rcu_assign_pointer(chain->stats, newstats);
816
817         return 0;
818 }
819
820 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
821                               const struct nlmsghdr *nlh,
822                               const struct nlattr * const nla[])
823 {
824         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
825         const struct nlattr * uninitialized_var(name);
826         const struct nft_af_info *afi;
827         struct nft_table *table;
828         struct nft_chain *chain;
829         struct nft_base_chain *basechain = NULL;
830         struct nlattr *ha[NFTA_HOOK_MAX + 1];
831         struct net *net = sock_net(skb->sk);
832         int family = nfmsg->nfgen_family;
833         u64 handle = 0;
834         int err;
835         bool create;
836
837         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
838
839         afi = nf_tables_afinfo_lookup(net, family, true);
840         if (IS_ERR(afi))
841                 return PTR_ERR(afi);
842
843         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
844         if (IS_ERR(table))
845                 return PTR_ERR(table);
846
847         if (table->use == UINT_MAX)
848                 return -EOVERFLOW;
849
850         chain = NULL;
851         name = nla[NFTA_CHAIN_NAME];
852
853         if (nla[NFTA_CHAIN_HANDLE]) {
854                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
855                 chain = nf_tables_chain_lookup_byhandle(table, handle);
856                 if (IS_ERR(chain))
857                         return PTR_ERR(chain);
858         } else {
859                 chain = nf_tables_chain_lookup(table, name);
860                 if (IS_ERR(chain)) {
861                         if (PTR_ERR(chain) != -ENOENT)
862                                 return PTR_ERR(chain);
863                         chain = NULL;
864                 }
865         }
866
867         if (chain != NULL) {
868                 if (nlh->nlmsg_flags & NLM_F_EXCL)
869                         return -EEXIST;
870                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
871                         return -EOPNOTSUPP;
872
873                 if (nla[NFTA_CHAIN_HANDLE] && name &&
874                     !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
875                         return -EEXIST;
876
877                 if (nla[NFTA_CHAIN_POLICY]) {
878                         if (!(chain->flags & NFT_BASE_CHAIN))
879                                 return -EOPNOTSUPP;
880
881                         err = nf_tables_chain_policy(nft_base_chain(chain),
882                                                      nla[NFTA_CHAIN_POLICY]);
883                         if (err < 0)
884                                 return err;
885                 }
886
887                 if (nla[NFTA_CHAIN_COUNTERS]) {
888                         if (!(chain->flags & NFT_BASE_CHAIN))
889                                 return -EOPNOTSUPP;
890
891                         err = nf_tables_counters(nft_base_chain(chain),
892                                                  nla[NFTA_CHAIN_COUNTERS]);
893                         if (err < 0)
894                                 return err;
895                 }
896
897                 if (nla[NFTA_CHAIN_HANDLE] && name)
898                         nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
899
900                 goto notify;
901         }
902
903         if (nla[NFTA_CHAIN_HOOK]) {
904                 struct nf_hook_ops *ops;
905                 nf_hookfn *hookfn;
906                 u32 hooknum;
907                 int type = NFT_CHAIN_T_DEFAULT;
908
909                 if (nla[NFTA_CHAIN_TYPE]) {
910                         type = nf_tables_chain_type_lookup(afi,
911                                                            nla[NFTA_CHAIN_TYPE],
912                                                            create);
913                         if (type < 0)
914                                 return -ENOENT;
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
929                 hookfn = chain_type[family][type]->fn[hooknum];
930                 if (hookfn == NULL)
931                         return -EOPNOTSUPP;
932
933                 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
934                 if (basechain == NULL)
935                         return -ENOMEM;
936
937                 basechain->type = type;
938                 chain = &basechain->chain;
939
940                 ops = &basechain->ops;
941                 ops->pf         = family;
942                 ops->owner      = afi->owner;
943                 ops->hooknum    = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
944                 ops->priority   = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
945                 ops->priv       = chain;
946                 ops->hook       = hookfn;
947                 if (afi->hooks[ops->hooknum])
948                         ops->hook = afi->hooks[ops->hooknum];
949
950                 chain->flags |= NFT_BASE_CHAIN;
951
952                 if (nla[NFTA_CHAIN_POLICY]) {
953                         err = nf_tables_chain_policy(basechain,
954                                                      nla[NFTA_CHAIN_POLICY]);
955                         if (err < 0) {
956                                 free_percpu(basechain->stats);
957                                 kfree(basechain);
958                                 return err;
959                         }
960                 } else
961                         basechain->policy = NF_ACCEPT;
962
963                 if (nla[NFTA_CHAIN_COUNTERS]) {
964                         err = nf_tables_counters(basechain,
965                                                  nla[NFTA_CHAIN_COUNTERS]);
966                         if (err < 0) {
967                                 free_percpu(basechain->stats);
968                                 kfree(basechain);
969                                 return err;
970                         }
971                 } else {
972                         struct nft_stats __percpu *newstats;
973
974                         newstats = alloc_percpu(struct nft_stats);
975                         if (newstats == NULL)
976                                 return -ENOMEM;
977
978                         rcu_assign_pointer(nft_base_chain(chain)->stats,
979                                            newstats);
980                 }
981         } else {
982                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
983                 if (chain == NULL)
984                         return -ENOMEM;
985         }
986
987         INIT_LIST_HEAD(&chain->rules);
988         chain->handle = nf_tables_alloc_handle(table);
989         chain->net = net;
990         chain->table = table;
991         nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
992
993         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
994             chain->flags & NFT_BASE_CHAIN) {
995                 err = nf_register_hook(&nft_base_chain(chain)->ops);
996                 if (err < 0) {
997                         free_percpu(basechain->stats);
998                         kfree(basechain);
999                         return err;
1000                 }
1001         }
1002         list_add_tail(&chain->list, &table->chains);
1003         table->use++;
1004 notify:
1005         nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1006                                family);
1007         return 0;
1008 }
1009
1010 static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1011 {
1012         struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1013
1014         BUG_ON(chain->use > 0);
1015
1016         if (chain->flags & NFT_BASE_CHAIN) {
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_hook(&nft_base_chain(chain)->ops);
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;
1935         const struct nft_table *table = NULL;
1936
1937         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1938         if (IS_ERR(afi))
1939                 return PTR_ERR(afi);
1940
1941         if (nla[NFTA_SET_TABLE] != NULL) {
1942                 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1943                 if (IS_ERR(table))
1944                         return PTR_ERR(table);
1945         }
1946
1947         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1948         return 0;
1949 }
1950
1951 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1952                                      const struct nlattr *nla)
1953 {
1954         struct nft_set *set;
1955
1956         if (nla == NULL)
1957                 return ERR_PTR(-EINVAL);
1958
1959         list_for_each_entry(set, &table->sets, list) {
1960                 if (!nla_strcmp(nla, set->name))
1961                         return set;
1962         }
1963         return ERR_PTR(-ENOENT);
1964 }
1965
1966 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1967                                     const char *name)
1968 {
1969         const struct nft_set *i;
1970         const char *p;
1971         unsigned long *inuse;
1972         unsigned int n = 0;
1973
1974         p = strnchr(name, IFNAMSIZ, '%');
1975         if (p != NULL) {
1976                 if (p[1] != 'd' || strchr(p + 2, '%'))
1977                         return -EINVAL;
1978
1979                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1980                 if (inuse == NULL)
1981                         return -ENOMEM;
1982
1983                 list_for_each_entry(i, &ctx->table->sets, list) {
1984                         if (!sscanf(i->name, name, &n))
1985                                 continue;
1986                         if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
1987                                 continue;
1988                         set_bit(n, inuse);
1989                 }
1990
1991                 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1992                 free_page((unsigned long)inuse);
1993         }
1994
1995         snprintf(set->name, sizeof(set->name), name, n);
1996         list_for_each_entry(i, &ctx->table->sets, list) {
1997                 if (!strcmp(set->name, i->name))
1998                         return -ENFILE;
1999         }
2000         return 0;
2001 }
2002
2003 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2004                               const struct nft_set *set, u16 event, u16 flags)
2005 {
2006         struct nfgenmsg *nfmsg;
2007         struct nlmsghdr *nlh;
2008         u32 portid = NETLINK_CB(ctx->skb).portid;
2009         u32 seq = ctx->nlh->nlmsg_seq;
2010
2011         event |= NFNL_SUBSYS_NFTABLES << 8;
2012         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2013                         flags);
2014         if (nlh == NULL)
2015                 goto nla_put_failure;
2016
2017         nfmsg = nlmsg_data(nlh);
2018         nfmsg->nfgen_family     = ctx->afi->family;
2019         nfmsg->version          = NFNETLINK_V0;
2020         nfmsg->res_id           = 0;
2021
2022         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2023                 goto nla_put_failure;
2024         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2025                 goto nla_put_failure;
2026         if (set->flags != 0)
2027                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2028                         goto nla_put_failure;
2029
2030         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2031                 goto nla_put_failure;
2032         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2033                 goto nla_put_failure;
2034         if (set->flags & NFT_SET_MAP) {
2035                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2036                         goto nla_put_failure;
2037                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2038                         goto nla_put_failure;
2039         }
2040
2041         return nlmsg_end(skb, nlh);
2042
2043 nla_put_failure:
2044         nlmsg_trim(skb, nlh);
2045         return -1;
2046 }
2047
2048 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2049                                 const struct nft_set *set,
2050                                 int event)
2051 {
2052         struct sk_buff *skb;
2053         u32 portid = NETLINK_CB(ctx->skb).portid;
2054         bool report;
2055         int err;
2056
2057         report = nlmsg_report(ctx->nlh);
2058         if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2059                 return 0;
2060
2061         err = -ENOBUFS;
2062         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2063         if (skb == NULL)
2064                 goto err;
2065
2066         err = nf_tables_fill_set(skb, ctx, set, event, 0);
2067         if (err < 0) {
2068                 kfree_skb(skb);
2069                 goto err;
2070         }
2071
2072         err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2073                              GFP_KERNEL);
2074 err:
2075         if (err < 0)
2076                 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2077         return err;
2078 }
2079
2080 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2081                                      struct netlink_callback *cb)
2082 {
2083         const struct nft_set *set;
2084         unsigned int idx = 0, s_idx = cb->args[0];
2085
2086         if (cb->args[1])
2087                 return skb->len;
2088
2089         list_for_each_entry(set, &ctx->table->sets, list) {
2090                 if (idx < s_idx)
2091                         goto cont;
2092                 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2093                                        NLM_F_MULTI) < 0) {
2094                         cb->args[0] = idx;
2095                         goto done;
2096                 }
2097 cont:
2098                 idx++;
2099         }
2100         cb->args[1] = 1;
2101 done:
2102         return skb->len;
2103 }
2104
2105 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2106                                    struct netlink_callback *cb)
2107 {
2108         const struct nft_set *set;
2109         unsigned int idx, s_idx = cb->args[0];
2110         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2111
2112         if (cb->args[1])
2113                 return skb->len;
2114
2115         list_for_each_entry(table, &ctx->afi->tables, list) {
2116                 if (cur_table) {
2117                         if (cur_table != table)
2118                                 continue;
2119
2120                         cur_table = NULL;
2121                 }
2122                 ctx->table = table;
2123                 idx = 0;
2124                 list_for_each_entry(set, &ctx->table->sets, list) {
2125                         if (idx < s_idx)
2126                                 goto cont;
2127                         if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2128                                                NLM_F_MULTI) < 0) {
2129                                 cb->args[0] = idx;
2130                                 cb->args[2] = (unsigned long) table;
2131                                 goto done;
2132                         }
2133 cont:
2134                         idx++;
2135                 }
2136         }
2137         cb->args[1] = 1;
2138 done:
2139         return skb->len;
2140 }
2141
2142 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2143 {
2144         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2145         struct nlattr *nla[NFTA_SET_MAX + 1];
2146         struct nft_ctx ctx;
2147         int err, ret;
2148
2149         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2150                           nft_set_policy);
2151         if (err < 0)
2152                 return err;
2153
2154         err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2155         if (err < 0)
2156                 return err;
2157
2158         if (ctx.table == NULL)
2159                 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2160         else
2161                 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2162
2163         return ret;
2164 }
2165
2166 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2167                             const struct nlmsghdr *nlh,
2168                             const struct nlattr * const nla[])
2169 {
2170         const struct nft_set *set;
2171         struct nft_ctx ctx;
2172         struct sk_buff *skb2;
2173         int err;
2174
2175         /* Verify existance before starting dump */
2176         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2177         if (err < 0)
2178                 return err;
2179
2180         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2181                 struct netlink_dump_control c = {
2182                         .dump = nf_tables_dump_sets,
2183                 };
2184                 return netlink_dump_start(nlsk, skb, nlh, &c);
2185         }
2186
2187         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2188         if (IS_ERR(set))
2189                 return PTR_ERR(set);
2190
2191         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2192         if (skb2 == NULL)
2193                 return -ENOMEM;
2194
2195         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2196         if (err < 0)
2197                 goto err;
2198
2199         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2200
2201 err:
2202         kfree_skb(skb2);
2203         return err;
2204 }
2205
2206 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2207                             const struct nlmsghdr *nlh,
2208                             const struct nlattr * const nla[])
2209 {
2210         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2211         const struct nft_set_ops *ops;
2212         const struct nft_af_info *afi;
2213         struct net *net = sock_net(skb->sk);
2214         struct nft_table *table;
2215         struct nft_set *set;
2216         struct nft_ctx ctx;
2217         char name[IFNAMSIZ];
2218         unsigned int size;
2219         bool create;
2220         u32 ktype, klen, dlen, dtype, flags;
2221         int err;
2222
2223         if (nla[NFTA_SET_TABLE] == NULL ||
2224             nla[NFTA_SET_NAME] == NULL ||
2225             nla[NFTA_SET_KEY_LEN] == NULL)
2226                 return -EINVAL;
2227
2228         ktype = NFT_DATA_VALUE;
2229         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2230                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2231                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2232                         return -EINVAL;
2233         }
2234
2235         klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2236         if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2237                 return -EINVAL;
2238
2239         flags = 0;
2240         if (nla[NFTA_SET_FLAGS] != NULL) {
2241                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2242                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2243                               NFT_SET_INTERVAL | NFT_SET_MAP))
2244                         return -EINVAL;
2245         }
2246
2247         dtype = 0;
2248         dlen  = 0;
2249         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2250                 if (!(flags & NFT_SET_MAP))
2251                         return -EINVAL;
2252
2253                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2254                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2255                     dtype != NFT_DATA_VERDICT)
2256                         return -EINVAL;
2257
2258                 if (dtype != NFT_DATA_VERDICT) {
2259                         if (nla[NFTA_SET_DATA_LEN] == NULL)
2260                                 return -EINVAL;
2261                         dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2262                         if (dlen == 0 ||
2263                             dlen > FIELD_SIZEOF(struct nft_data, data))
2264                                 return -EINVAL;
2265                 } else
2266                         dlen = sizeof(struct nft_data);
2267         } else if (flags & NFT_SET_MAP)
2268                 return -EINVAL;
2269
2270         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2271
2272         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2273         if (IS_ERR(afi))
2274                 return PTR_ERR(afi);
2275
2276         table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2277         if (IS_ERR(table))
2278                 return PTR_ERR(table);
2279
2280         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2281
2282         set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2283         if (IS_ERR(set)) {
2284                 if (PTR_ERR(set) != -ENOENT)
2285                         return PTR_ERR(set);
2286                 set = NULL;
2287         }
2288
2289         if (set != NULL) {
2290                 if (nlh->nlmsg_flags & NLM_F_EXCL)
2291                         return -EEXIST;
2292                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2293                         return -EOPNOTSUPP;
2294                 return 0;
2295         }
2296
2297         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2298                 return -ENOENT;
2299
2300         ops = nft_select_set_ops(nla);
2301         if (IS_ERR(ops))
2302                 return PTR_ERR(ops);
2303
2304         size = 0;
2305         if (ops->privsize != NULL)
2306                 size = ops->privsize(nla);
2307
2308         err = -ENOMEM;
2309         set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2310         if (set == NULL)
2311                 goto err1;
2312
2313         nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2314         err = nf_tables_set_alloc_name(&ctx, set, name);
2315         if (err < 0)
2316                 goto err2;
2317
2318         INIT_LIST_HEAD(&set->bindings);
2319         set->ops   = ops;
2320         set->ktype = ktype;
2321         set->klen  = klen;
2322         set->dtype = dtype;
2323         set->dlen  = dlen;
2324         set->flags = flags;
2325
2326         err = ops->init(set, nla);
2327         if (err < 0)
2328                 goto err2;
2329
2330         list_add_tail(&set->list, &table->sets);
2331         nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2332         return 0;
2333
2334 err2:
2335         kfree(set);
2336 err1:
2337         module_put(ops->owner);
2338         return err;
2339 }
2340
2341 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2342 {
2343         list_del(&set->list);
2344         if (!(set->flags & NFT_SET_ANONYMOUS))
2345                 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2346
2347         set->ops->destroy(set);
2348         module_put(set->ops->owner);
2349         kfree(set);
2350 }
2351
2352 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2353                             const struct nlmsghdr *nlh,
2354                             const struct nlattr * const nla[])
2355 {
2356         struct nft_set *set;
2357         struct nft_ctx ctx;
2358         int err;
2359
2360         if (nla[NFTA_SET_TABLE] == NULL)
2361                 return -EINVAL;
2362
2363         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2364         if (err < 0)
2365                 return err;
2366
2367         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2368         if (IS_ERR(set))
2369                 return PTR_ERR(set);
2370         if (!list_empty(&set->bindings))
2371                 return -EBUSY;
2372
2373         nf_tables_set_destroy(&ctx, set);
2374         return 0;
2375 }
2376
2377 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2378                                         const struct nft_set *set,
2379                                         const struct nft_set_iter *iter,
2380                                         const struct nft_set_elem *elem)
2381 {
2382         enum nft_registers dreg;
2383
2384         dreg = nft_type_to_reg(set->dtype);
2385         return nft_validate_data_load(ctx, dreg, &elem->data,
2386                                       set->dtype == NFT_DATA_VERDICT ?
2387                                       NFT_DATA_VERDICT : NFT_DATA_VALUE);
2388 }
2389
2390 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2391                        struct nft_set_binding *binding)
2392 {
2393         struct nft_set_binding *i;
2394         struct nft_set_iter iter;
2395
2396         if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2397                 return -EBUSY;
2398
2399         if (set->flags & NFT_SET_MAP) {
2400                 /* If the set is already bound to the same chain all
2401                  * jumps are already validated for that chain.
2402                  */
2403                 list_for_each_entry(i, &set->bindings, list) {
2404                         if (i->chain == binding->chain)
2405                                 goto bind;
2406                 }
2407
2408                 iter.skip       = 0;
2409                 iter.count      = 0;
2410                 iter.err        = 0;
2411                 iter.fn         = nf_tables_bind_check_setelem;
2412
2413                 set->ops->walk(ctx, set, &iter);
2414                 if (iter.err < 0) {
2415                         /* Destroy anonymous sets if binding fails */
2416                         if (set->flags & NFT_SET_ANONYMOUS)
2417                                 nf_tables_set_destroy(ctx, set);
2418
2419                         return iter.err;
2420                 }
2421         }
2422 bind:
2423         binding->chain = ctx->chain;
2424         list_add_tail(&binding->list, &set->bindings);
2425         return 0;
2426 }
2427
2428 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2429                           struct nft_set_binding *binding)
2430 {
2431         list_del(&binding->list);
2432
2433         if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2434                 nf_tables_set_destroy(ctx, set);
2435 }
2436
2437 /*
2438  * Set elements
2439  */
2440
2441 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2442         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
2443         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
2444         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
2445 };
2446
2447 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2448         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING },
2449         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING },
2450         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
2451 };
2452
2453 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2454                                       const struct sk_buff *skb,
2455                                       const struct nlmsghdr *nlh,
2456                                       const struct nlattr * const nla[])
2457 {
2458         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2459         const struct nft_af_info *afi;
2460         const struct nft_table *table;
2461         struct net *net = sock_net(skb->sk);
2462
2463         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2464         if (IS_ERR(afi))
2465                 return PTR_ERR(afi);
2466
2467         table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2468         if (IS_ERR(table))
2469                 return PTR_ERR(table);
2470
2471         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2472         return 0;
2473 }
2474
2475 static int nf_tables_fill_setelem(struct sk_buff *skb,
2476                                   const struct nft_set *set,
2477                                   const struct nft_set_elem *elem)
2478 {
2479         unsigned char *b = skb_tail_pointer(skb);
2480         struct nlattr *nest;
2481
2482         nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2483         if (nest == NULL)
2484                 goto nla_put_failure;
2485
2486         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2487                           set->klen) < 0)
2488                 goto nla_put_failure;
2489
2490         if (set->flags & NFT_SET_MAP &&
2491             !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2492             nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2493                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2494                           set->dlen) < 0)
2495                 goto nla_put_failure;
2496
2497         if (elem->flags != 0)
2498                 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2499                         goto nla_put_failure;
2500
2501         nla_nest_end(skb, nest);
2502         return 0;
2503
2504 nla_put_failure:
2505         nlmsg_trim(skb, b);
2506         return -EMSGSIZE;
2507 }
2508
2509 struct nft_set_dump_args {
2510         const struct netlink_callback   *cb;
2511         struct nft_set_iter             iter;
2512         struct sk_buff                  *skb;
2513 };
2514
2515 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2516                                   const struct nft_set *set,
2517                                   const struct nft_set_iter *iter,
2518                                   const struct nft_set_elem *elem)
2519 {
2520         struct nft_set_dump_args *args;
2521
2522         args = container_of(iter, struct nft_set_dump_args, iter);
2523         return nf_tables_fill_setelem(args->skb, set, elem);
2524 }
2525
2526 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2527 {
2528         const struct nft_set *set;
2529         struct nft_set_dump_args args;
2530         struct nft_ctx ctx;
2531         struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2532         struct nfgenmsg *nfmsg;
2533         struct nlmsghdr *nlh;
2534         struct nlattr *nest;
2535         u32 portid, seq;
2536         int event, err;
2537
2538         nfmsg = nlmsg_data(cb->nlh);
2539         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2540                           nft_set_elem_list_policy);
2541         if (err < 0)
2542                 return err;
2543
2544         err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2545         if (err < 0)
2546                 return err;
2547
2548         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2549         if (IS_ERR(set))
2550                 return PTR_ERR(set);
2551
2552         event  = NFT_MSG_NEWSETELEM;
2553         event |= NFNL_SUBSYS_NFTABLES << 8;
2554         portid = NETLINK_CB(cb->skb).portid;
2555         seq    = cb->nlh->nlmsg_seq;
2556
2557         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2558                         NLM_F_MULTI);
2559         if (nlh == NULL)
2560                 goto nla_put_failure;
2561
2562         nfmsg = nlmsg_data(nlh);
2563         nfmsg->nfgen_family = NFPROTO_UNSPEC;
2564         nfmsg->version      = NFNETLINK_V0;
2565         nfmsg->res_id       = 0;
2566
2567         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2568                 goto nla_put_failure;
2569         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2570                 goto nla_put_failure;
2571
2572         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2573         if (nest == NULL)
2574                 goto nla_put_failure;
2575
2576         args.cb         = cb;
2577         args.skb        = skb;
2578         args.iter.skip  = cb->args[0];
2579         args.iter.count = 0;
2580         args.iter.err   = 0;
2581         args.iter.fn    = nf_tables_dump_setelem;
2582         set->ops->walk(&ctx, set, &args.iter);
2583
2584         nla_nest_end(skb, nest);
2585         nlmsg_end(skb, nlh);
2586
2587         if (args.iter.err && args.iter.err != -EMSGSIZE)
2588                 return args.iter.err;
2589         if (args.iter.count == cb->args[0])
2590                 return 0;
2591
2592         cb->args[0] = args.iter.count;
2593         return skb->len;
2594
2595 nla_put_failure:
2596         return -ENOSPC;
2597 }
2598
2599 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2600                                 const struct nlmsghdr *nlh,
2601                                 const struct nlattr * const nla[])
2602 {
2603         const struct nft_set *set;
2604         struct nft_ctx ctx;
2605         int err;
2606
2607         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2608         if (err < 0)
2609                 return err;
2610
2611         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2612         if (IS_ERR(set))
2613                 return PTR_ERR(set);
2614
2615         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2616                 struct netlink_dump_control c = {
2617                         .dump = nf_tables_dump_set,
2618                 };
2619                 return netlink_dump_start(nlsk, skb, nlh, &c);
2620         }
2621         return -EOPNOTSUPP;
2622 }
2623
2624 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2625                             const struct nlattr *attr)
2626 {
2627         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2628         struct nft_data_desc d1, d2;
2629         struct nft_set_elem elem;
2630         struct nft_set_binding *binding;
2631         enum nft_registers dreg;
2632         int err;
2633
2634         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2635                                nft_set_elem_policy);
2636         if (err < 0)
2637                 return err;
2638
2639         if (nla[NFTA_SET_ELEM_KEY] == NULL)
2640                 return -EINVAL;
2641
2642         elem.flags = 0;
2643         if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2644                 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2645                 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2646                         return -EINVAL;
2647         }
2648
2649         if (set->flags & NFT_SET_MAP) {
2650                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2651                     !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2652                         return -EINVAL;
2653         } else {
2654                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2655                         return -EINVAL;
2656         }
2657
2658         err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2659         if (err < 0)
2660                 goto err1;
2661         err = -EINVAL;
2662         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2663                 goto err2;
2664
2665         err = -EEXIST;
2666         if (set->ops->get(set, &elem) == 0)
2667                 goto err2;
2668
2669         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2670                 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2671                 if (err < 0)
2672                         goto err2;
2673
2674                 err = -EINVAL;
2675                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2676                         goto err3;
2677
2678                 dreg = nft_type_to_reg(set->dtype);
2679                 list_for_each_entry(binding, &set->bindings, list) {
2680                         struct nft_ctx bind_ctx = {
2681                                 .afi    = ctx->afi,
2682                                 .table  = ctx->table,
2683                                 .chain  = binding->chain,
2684                         };
2685
2686                         err = nft_validate_data_load(&bind_ctx, dreg,
2687                                                      &elem.data, d2.type);
2688                         if (err < 0)
2689                                 goto err3;
2690                 }
2691         }
2692
2693         err = set->ops->insert(set, &elem);
2694         if (err < 0)
2695                 goto err3;
2696
2697         return 0;
2698
2699 err3:
2700         if (nla[NFTA_SET_ELEM_DATA] != NULL)
2701                 nft_data_uninit(&elem.data, d2.type);
2702 err2:
2703         nft_data_uninit(&elem.key, d1.type);
2704 err1:
2705         return err;
2706 }
2707
2708 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2709                                 const struct nlmsghdr *nlh,
2710                                 const struct nlattr * const nla[])
2711 {
2712         const struct nlattr *attr;
2713         struct nft_set *set;
2714         struct nft_ctx ctx;
2715         int rem, err;
2716
2717         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2718         if (err < 0)
2719                 return err;
2720
2721         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2722         if (IS_ERR(set))
2723                 return PTR_ERR(set);
2724         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2725                 return -EBUSY;
2726
2727         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2728                 err = nft_add_set_elem(&ctx, set, attr);
2729                 if (err < 0)
2730                         return err;
2731         }
2732         return 0;
2733 }
2734
2735 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2736                            const struct nlattr *attr)
2737 {
2738         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2739         struct nft_data_desc desc;
2740         struct nft_set_elem elem;
2741         int err;
2742
2743         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2744                                nft_set_elem_policy);
2745         if (err < 0)
2746                 goto err1;
2747
2748         err = -EINVAL;
2749         if (nla[NFTA_SET_ELEM_KEY] == NULL)
2750                 goto err1;
2751
2752         err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2753         if (err < 0)
2754                 goto err1;
2755
2756         err = -EINVAL;
2757         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2758                 goto err2;
2759
2760         err = set->ops->get(set, &elem);
2761         if (err < 0)
2762                 goto err2;
2763
2764         set->ops->remove(set, &elem);
2765
2766         nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2767         if (set->flags & NFT_SET_MAP)
2768                 nft_data_uninit(&elem.data, set->dtype);
2769
2770 err2:
2771         nft_data_uninit(&elem.key, desc.type);
2772 err1:
2773         return err;
2774 }
2775
2776 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2777                                 const struct nlmsghdr *nlh,
2778                                 const struct nlattr * const nla[])
2779 {
2780         const struct nlattr *attr;
2781         struct nft_set *set;
2782         struct nft_ctx ctx;
2783         int rem, err;
2784
2785         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2786         if (err < 0)
2787                 return err;
2788
2789         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2790         if (IS_ERR(set))
2791                 return PTR_ERR(set);
2792         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2793                 return -EBUSY;
2794
2795         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2796                 err = nft_del_setelem(&ctx, set, attr);
2797                 if (err < 0)
2798                         return err;
2799         }
2800         return 0;
2801 }
2802
2803 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2804         [NFT_MSG_NEWTABLE] = {
2805                 .call           = nf_tables_newtable,
2806                 .attr_count     = NFTA_TABLE_MAX,
2807                 .policy         = nft_table_policy,
2808         },
2809         [NFT_MSG_GETTABLE] = {
2810                 .call           = nf_tables_gettable,
2811                 .attr_count     = NFTA_TABLE_MAX,
2812                 .policy         = nft_table_policy,
2813         },
2814         [NFT_MSG_DELTABLE] = {
2815                 .call           = nf_tables_deltable,
2816                 .attr_count     = NFTA_TABLE_MAX,
2817                 .policy         = nft_table_policy,
2818         },
2819         [NFT_MSG_NEWCHAIN] = {
2820                 .call           = nf_tables_newchain,
2821                 .attr_count     = NFTA_CHAIN_MAX,
2822                 .policy         = nft_chain_policy,
2823         },
2824         [NFT_MSG_GETCHAIN] = {
2825                 .call           = nf_tables_getchain,
2826                 .attr_count     = NFTA_CHAIN_MAX,
2827                 .policy         = nft_chain_policy,
2828         },
2829         [NFT_MSG_DELCHAIN] = {
2830                 .call           = nf_tables_delchain,
2831                 .attr_count     = NFTA_CHAIN_MAX,
2832                 .policy         = nft_chain_policy,
2833         },
2834         [NFT_MSG_NEWRULE] = {
2835                 .call_batch     = nf_tables_newrule,
2836                 .attr_count     = NFTA_RULE_MAX,
2837                 .policy         = nft_rule_policy,
2838         },
2839         [NFT_MSG_GETRULE] = {
2840                 .call           = nf_tables_getrule,
2841                 .attr_count     = NFTA_RULE_MAX,
2842                 .policy         = nft_rule_policy,
2843         },
2844         [NFT_MSG_DELRULE] = {
2845                 .call_batch     = nf_tables_delrule,
2846                 .attr_count     = NFTA_RULE_MAX,
2847                 .policy         = nft_rule_policy,
2848         },
2849         [NFT_MSG_NEWSET] = {
2850                 .call           = nf_tables_newset,
2851                 .attr_count     = NFTA_SET_MAX,
2852                 .policy         = nft_set_policy,
2853         },
2854         [NFT_MSG_GETSET] = {
2855                 .call           = nf_tables_getset,
2856                 .attr_count     = NFTA_SET_MAX,
2857                 .policy         = nft_set_policy,
2858         },
2859         [NFT_MSG_DELSET] = {
2860                 .call           = nf_tables_delset,
2861                 .attr_count     = NFTA_SET_MAX,
2862                 .policy         = nft_set_policy,
2863         },
2864         [NFT_MSG_NEWSETELEM] = {
2865                 .call           = nf_tables_newsetelem,
2866                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2867                 .policy         = nft_set_elem_list_policy,
2868         },
2869         [NFT_MSG_GETSETELEM] = {
2870                 .call           = nf_tables_getsetelem,
2871                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2872                 .policy         = nft_set_elem_list_policy,
2873         },
2874         [NFT_MSG_DELSETELEM] = {
2875                 .call           = nf_tables_delsetelem,
2876                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
2877                 .policy         = nft_set_elem_list_policy,
2878         },
2879 };
2880
2881 static const struct nfnetlink_subsystem nf_tables_subsys = {
2882         .name           = "nf_tables",
2883         .subsys_id      = NFNL_SUBSYS_NFTABLES,
2884         .cb_count       = NFT_MSG_MAX,
2885         .cb             = nf_tables_cb,
2886         .commit         = nf_tables_commit,
2887         .abort          = nf_tables_abort,
2888 };
2889
2890 /*
2891  * Loop detection - walk through the ruleset beginning at the destination chain
2892  * of a new jump until either the source chain is reached (loop) or all
2893  * reachable chains have been traversed.
2894  *
2895  * The loop check is performed whenever a new jump verdict is added to an
2896  * expression or verdict map or a verdict map is bound to a new chain.
2897  */
2898
2899 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2900                                  const struct nft_chain *chain);
2901
2902 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2903                                         const struct nft_set *set,
2904                                         const struct nft_set_iter *iter,
2905                                         const struct nft_set_elem *elem)
2906 {
2907         switch (elem->data.verdict) {
2908         case NFT_JUMP:
2909         case NFT_GOTO:
2910                 return nf_tables_check_loops(ctx, elem->data.chain);
2911         default:
2912                 return 0;
2913         }
2914 }
2915
2916 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2917                                  const struct nft_chain *chain)
2918 {
2919         const struct nft_rule *rule;
2920         const struct nft_expr *expr, *last;
2921         const struct nft_set *set;
2922         struct nft_set_binding *binding;
2923         struct nft_set_iter iter;
2924
2925         if (ctx->chain == chain)
2926                 return -ELOOP;
2927
2928         list_for_each_entry(rule, &chain->rules, list) {
2929                 nft_rule_for_each_expr(expr, last, rule) {
2930                         const struct nft_data *data = NULL;
2931                         int err;
2932
2933                         if (!expr->ops->validate)
2934                                 continue;
2935
2936                         err = expr->ops->validate(ctx, expr, &data);
2937                         if (err < 0)
2938                                 return err;
2939
2940                         if (data == NULL)
2941                                 continue;
2942
2943                         switch (data->verdict) {
2944                         case NFT_JUMP:
2945                         case NFT_GOTO:
2946                                 err = nf_tables_check_loops(ctx, data->chain);
2947                                 if (err < 0)
2948                                         return err;
2949                         default:
2950                                 break;
2951                         }
2952                 }
2953         }
2954
2955         list_for_each_entry(set, &ctx->table->sets, list) {
2956                 if (!(set->flags & NFT_SET_MAP) ||
2957                     set->dtype != NFT_DATA_VERDICT)
2958                         continue;
2959
2960                 list_for_each_entry(binding, &set->bindings, list) {
2961                         if (binding->chain != chain)
2962                                 continue;
2963
2964                         iter.skip       = 0;
2965                         iter.count      = 0;
2966                         iter.err        = 0;
2967                         iter.fn         = nf_tables_loop_check_setelem;
2968
2969                         set->ops->walk(ctx, set, &iter);
2970                         if (iter.err < 0)
2971                                 return iter.err;
2972                 }
2973         }
2974
2975         return 0;
2976 }
2977
2978 /**
2979  *      nft_validate_input_register - validate an expressions' input register
2980  *
2981  *      @reg: the register number
2982  *
2983  *      Validate that the input register is one of the general purpose
2984  *      registers.
2985  */
2986 int nft_validate_input_register(enum nft_registers reg)
2987 {
2988         if (reg <= NFT_REG_VERDICT)
2989                 return -EINVAL;
2990         if (reg > NFT_REG_MAX)
2991                 return -ERANGE;
2992         return 0;
2993 }
2994 EXPORT_SYMBOL_GPL(nft_validate_input_register);
2995
2996 /**
2997  *      nft_validate_output_register - validate an expressions' output register
2998  *
2999  *      @reg: the register number
3000  *
3001  *      Validate that the output register is one of the general purpose
3002  *      registers or the verdict register.
3003  */
3004 int nft_validate_output_register(enum nft_registers reg)
3005 {
3006         if (reg < NFT_REG_VERDICT)
3007                 return -EINVAL;
3008         if (reg > NFT_REG_MAX)
3009                 return -ERANGE;
3010         return 0;
3011 }
3012 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3013
3014 /**
3015  *      nft_validate_data_load - validate an expressions' data load
3016  *
3017  *      @ctx: context of the expression performing the load
3018  *      @reg: the destination register number
3019  *      @data: the data to load
3020  *      @type: the data type
3021  *
3022  *      Validate that a data load uses the appropriate data type for
3023  *      the destination register. A value of NULL for the data means
3024  *      that its runtime gathered data, which is always of type
3025  *      NFT_DATA_VALUE.
3026  */
3027 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3028                            const struct nft_data *data,
3029                            enum nft_data_types type)
3030 {
3031         int err;
3032
3033         switch (reg) {
3034         case NFT_REG_VERDICT:
3035                 if (data == NULL || type != NFT_DATA_VERDICT)
3036                         return -EINVAL;
3037
3038                 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3039                         err = nf_tables_check_loops(ctx, data->chain);
3040                         if (err < 0)
3041                                 return err;
3042
3043                         if (ctx->chain->level + 1 > data->chain->level) {
3044                                 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3045                                         return -EMLINK;
3046                                 data->chain->level = ctx->chain->level + 1;
3047                         }
3048                 }
3049
3050                 return 0;
3051         default:
3052                 if (data != NULL && type != NFT_DATA_VALUE)
3053                         return -EINVAL;
3054                 return 0;
3055         }
3056 }
3057 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3058
3059 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3060         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
3061         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
3062                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
3063 };
3064
3065 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3066                             struct nft_data_desc *desc, const struct nlattr *nla)
3067 {
3068         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3069         struct nft_chain *chain;
3070         int err;
3071
3072         err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3073         if (err < 0)
3074                 return err;
3075
3076         if (!tb[NFTA_VERDICT_CODE])
3077                 return -EINVAL;
3078         data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3079
3080         switch (data->verdict) {
3081         case NF_ACCEPT:
3082         case NF_DROP:
3083         case NF_QUEUE:
3084         case NFT_CONTINUE:
3085         case NFT_BREAK:
3086         case NFT_RETURN:
3087                 desc->len = sizeof(data->verdict);
3088                 break;
3089         case NFT_JUMP:
3090         case NFT_GOTO:
3091                 if (!tb[NFTA_VERDICT_CHAIN])
3092                         return -EINVAL;
3093                 chain = nf_tables_chain_lookup(ctx->table,
3094                                                tb[NFTA_VERDICT_CHAIN]);
3095                 if (IS_ERR(chain))
3096                         return PTR_ERR(chain);
3097                 if (chain->flags & NFT_BASE_CHAIN)
3098                         return -EOPNOTSUPP;
3099
3100                 chain->use++;
3101                 data->chain = chain;
3102                 desc->len = sizeof(data);
3103                 break;
3104         default:
3105                 return -EINVAL;
3106         }
3107
3108         desc->type = NFT_DATA_VERDICT;
3109         return 0;
3110 }
3111
3112 static void nft_verdict_uninit(const struct nft_data *data)
3113 {
3114         switch (data->verdict) {
3115         case NFT_JUMP:
3116         case NFT_GOTO:
3117                 data->chain->use--;
3118                 break;
3119         }
3120 }
3121
3122 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3123 {
3124         struct nlattr *nest;
3125
3126         nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3127         if (!nest)
3128                 goto nla_put_failure;
3129
3130         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3131                 goto nla_put_failure;
3132
3133         switch (data->verdict) {
3134         case NFT_JUMP:
3135         case NFT_GOTO:
3136                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3137                         goto nla_put_failure;
3138         }
3139         nla_nest_end(skb, nest);
3140         return 0;
3141
3142 nla_put_failure:
3143         return -1;
3144 }
3145
3146 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3147                           struct nft_data_desc *desc, const struct nlattr *nla)
3148 {
3149         unsigned int len;
3150
3151         len = nla_len(nla);
3152         if (len == 0)
3153                 return -EINVAL;
3154         if (len > sizeof(data->data))
3155                 return -EOVERFLOW;
3156
3157         nla_memcpy(data->data, nla, sizeof(data->data));
3158         desc->type = NFT_DATA_VALUE;
3159         desc->len  = len;
3160         return 0;
3161 }
3162
3163 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3164                           unsigned int len)
3165 {
3166         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3167 }
3168
3169 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3170         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY,
3171                                     .len  = FIELD_SIZEOF(struct nft_data, data) },
3172         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
3173 };
3174
3175 /**
3176  *      nft_data_init - parse nf_tables data netlink attributes
3177  *
3178  *      @ctx: context of the expression using the data
3179  *      @data: destination struct nft_data
3180  *      @desc: data description
3181  *      @nla: netlink attribute containing data
3182  *
3183  *      Parse the netlink data attributes and initialize a struct nft_data.
3184  *      The type and length of data are returned in the data description.
3185  *
3186  *      The caller can indicate that it only wants to accept data of type
3187  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
3188  */
3189 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3190                   struct nft_data_desc *desc, const struct nlattr *nla)
3191 {
3192         struct nlattr *tb[NFTA_DATA_MAX + 1];
3193         int err;
3194
3195         err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3196         if (err < 0)
3197                 return err;
3198
3199         if (tb[NFTA_DATA_VALUE])
3200                 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3201         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3202                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3203         return -EINVAL;
3204 }
3205 EXPORT_SYMBOL_GPL(nft_data_init);
3206
3207 /**
3208  *      nft_data_uninit - release a nft_data item
3209  *
3210  *      @data: struct nft_data to release
3211  *      @type: type of data
3212  *
3213  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3214  *      all others need to be released by calling this function.
3215  */
3216 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3217 {
3218         switch (type) {
3219         case NFT_DATA_VALUE:
3220                 return;
3221         case NFT_DATA_VERDICT:
3222                 return nft_verdict_uninit(data);
3223         default:
3224                 WARN_ON(1);
3225         }
3226 }
3227 EXPORT_SYMBOL_GPL(nft_data_uninit);
3228
3229 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3230                   enum nft_data_types type, unsigned int len)
3231 {
3232         struct nlattr *nest;
3233         int err;
3234
3235         nest = nla_nest_start(skb, attr);
3236         if (nest == NULL)
3237                 return -1;
3238
3239         switch (type) {
3240         case NFT_DATA_VALUE:
3241                 err = nft_value_dump(skb, data, len);
3242                 break;
3243         case NFT_DATA_VERDICT:
3244                 err = nft_verdict_dump(skb, data);
3245                 break;
3246         default:
3247                 err = -EINVAL;
3248                 WARN_ON(1);
3249         }
3250
3251         nla_nest_end(skb, nest);
3252         return err;
3253 }
3254 EXPORT_SYMBOL_GPL(nft_data_dump);
3255
3256 static int nf_tables_init_net(struct net *net)
3257 {
3258         INIT_LIST_HEAD(&net->nft.af_info);
3259         INIT_LIST_HEAD(&net->nft.commit_list);
3260         return 0;
3261 }
3262
3263 static struct pernet_operations nf_tables_net_ops = {
3264         .init   = nf_tables_init_net,
3265 };
3266
3267 static int __init nf_tables_module_init(void)
3268 {
3269         int err;
3270
3271         info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3272                        GFP_KERNEL);
3273         if (info == NULL) {
3274                 err = -ENOMEM;
3275                 goto err1;
3276         }
3277
3278         err = nf_tables_core_module_init();
3279         if (err < 0)
3280                 goto err2;
3281
3282         err = nfnetlink_subsys_register(&nf_tables_subsys);
3283         if (err < 0)
3284                 goto err3;
3285
3286         pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3287         return register_pernet_subsys(&nf_tables_net_ops);
3288 err3:
3289         nf_tables_core_module_exit();
3290 err2:
3291         kfree(info);
3292 err1:
3293         return err;
3294 }
3295
3296 static void __exit nf_tables_module_exit(void)
3297 {
3298         unregister_pernet_subsys(&nf_tables_net_ops);
3299         nfnetlink_subsys_unregister(&nf_tables_subsys);
3300         nf_tables_core_module_exit();
3301         kfree(info);
3302 }
3303
3304 module_init(nf_tables_module_init);
3305 module_exit(nf_tables_module_exit);
3306
3307 MODULE_LICENSE("GPL");
3308 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3309 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);