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