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