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