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