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