Linux-libre 3.16.85-gnu
[librecmc/linux-libre.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         preempt_disable();
893         stats = this_cpu_ptr(newstats);
894         stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
895         stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
896         preempt_enable();
897
898         return newstats;
899 }
900
901 static void nft_chain_stats_replace(struct nft_base_chain *chain,
902                                     struct nft_stats __percpu *newstats)
903 {
904         if (newstats == NULL)
905                 return;
906
907         if (chain->stats) {
908                 struct nft_stats __percpu *oldstats =
909                                 nft_dereference(chain->stats);
910
911                 rcu_assign_pointer(chain->stats, newstats);
912                 synchronize_rcu();
913                 free_percpu(oldstats);
914         } else
915                 rcu_assign_pointer(chain->stats, newstats);
916 }
917
918 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
919 {
920         struct nft_trans *trans;
921
922         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
923         if (trans == NULL)
924                 return -ENOMEM;
925
926         if (msg_type == NFT_MSG_NEWCHAIN)
927                 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
928
929         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
930         return 0;
931 }
932
933 static void nf_tables_chain_destroy(struct nft_chain *chain)
934 {
935         BUG_ON(chain->use > 0);
936
937         if (chain->flags & NFT_BASE_CHAIN) {
938                 module_put(nft_base_chain(chain)->type->owner);
939                 free_percpu(nft_base_chain(chain)->stats);
940                 kfree(nft_base_chain(chain));
941         } else {
942                 kfree(chain);
943         }
944 }
945
946 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
947                               const struct nlmsghdr *nlh,
948                               const struct nlattr * const nla[])
949 {
950         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
951         const struct nlattr * uninitialized_var(name);
952         struct nft_af_info *afi;
953         struct nft_table *table;
954         struct nft_chain *chain;
955         struct nft_base_chain *basechain = NULL;
956         struct nlattr *ha[NFTA_HOOK_MAX + 1];
957         struct net *net = sock_net(skb->sk);
958         int family = nfmsg->nfgen_family;
959         u8 policy = NF_ACCEPT;
960         u64 handle = 0;
961         unsigned int i;
962         struct nft_stats __percpu *stats;
963         int err;
964         bool create;
965         struct nft_ctx ctx;
966
967         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
968
969         afi = nf_tables_afinfo_lookup(net, family, true);
970         if (IS_ERR(afi))
971                 return PTR_ERR(afi);
972
973         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
974         if (IS_ERR(table))
975                 return PTR_ERR(table);
976
977         chain = NULL;
978         name = nla[NFTA_CHAIN_NAME];
979
980         if (nla[NFTA_CHAIN_HANDLE]) {
981                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
982                 chain = nf_tables_chain_lookup_byhandle(table, handle);
983                 if (IS_ERR(chain))
984                         return PTR_ERR(chain);
985         } else {
986                 chain = nf_tables_chain_lookup(table, name);
987                 if (IS_ERR(chain)) {
988                         if (PTR_ERR(chain) != -ENOENT)
989                                 return PTR_ERR(chain);
990                         chain = NULL;
991                 }
992         }
993
994         if (nla[NFTA_CHAIN_POLICY]) {
995                 if ((chain != NULL &&
996                     !(chain->flags & NFT_BASE_CHAIN)))
997                         return -EOPNOTSUPP;
998
999                 if (chain == NULL &&
1000                     nla[NFTA_CHAIN_HOOK] == NULL)
1001                         return -EOPNOTSUPP;
1002
1003                 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1004                 switch (policy) {
1005                 case NF_DROP:
1006                 case NF_ACCEPT:
1007                         break;
1008                 default:
1009                         return -EINVAL;
1010                 }
1011         }
1012
1013         if (chain != NULL) {
1014                 struct nft_stats *stats = NULL;
1015                 struct nft_trans *trans;
1016
1017                 if (chain->flags & NFT_CHAIN_INACTIVE)
1018                         return -ENOENT;
1019                 if (nlh->nlmsg_flags & NLM_F_EXCL)
1020                         return -EEXIST;
1021                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1022                         return -EOPNOTSUPP;
1023
1024                 if (nla[NFTA_CHAIN_HANDLE] && name &&
1025                     !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1026                         return -EEXIST;
1027
1028                 if (nla[NFTA_CHAIN_COUNTERS]) {
1029                         if (!(chain->flags & NFT_BASE_CHAIN))
1030                                 return -EOPNOTSUPP;
1031
1032                         stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1033                         if (IS_ERR(stats))
1034                                 return PTR_ERR(stats);
1035                 }
1036
1037                 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1038                 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1039                                         sizeof(struct nft_trans_chain));
1040                 if (trans == NULL) {
1041                         free_percpu(stats);
1042                         return -ENOMEM;
1043                 }
1044
1045                 nft_trans_chain_stats(trans) = stats;
1046                 nft_trans_chain_update(trans) = true;
1047
1048                 if (nla[NFTA_CHAIN_POLICY])
1049                         nft_trans_chain_policy(trans) = policy;
1050                 else
1051                         nft_trans_chain_policy(trans) = -1;
1052
1053                 if (nla[NFTA_CHAIN_HANDLE] && name) {
1054                         nla_strlcpy(nft_trans_chain_name(trans), name,
1055                                     NFT_CHAIN_MAXNAMELEN);
1056                 }
1057                 list_add_tail(&trans->list, &net->nft.commit_list);
1058                 return 0;
1059         }
1060
1061         if (table->use == UINT_MAX)
1062                 return -EOVERFLOW;
1063
1064         if (nla[NFTA_CHAIN_HOOK]) {
1065                 const struct nf_chain_type *type;
1066                 struct nf_hook_ops *ops;
1067                 nf_hookfn *hookfn;
1068                 u32 hooknum, priority;
1069
1070                 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1071                 if (nla[NFTA_CHAIN_TYPE]) {
1072                         type = nf_tables_chain_type_lookup(afi,
1073                                                            nla[NFTA_CHAIN_TYPE],
1074                                                            create);
1075                         if (IS_ERR(type))
1076                                 return PTR_ERR(type);
1077                 }
1078
1079                 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1080                                        nft_hook_policy);
1081                 if (err < 0)
1082                         return err;
1083                 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1084                     ha[NFTA_HOOK_PRIORITY] == NULL)
1085                         return -EINVAL;
1086
1087                 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1088                 if (hooknum >= afi->nhooks)
1089                         return -EINVAL;
1090                 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1091
1092                 if (!(type->hook_mask & (1 << hooknum)))
1093                         return -EOPNOTSUPP;
1094                 if (!try_module_get(type->owner))
1095                         return -ENOENT;
1096                 hookfn = type->hooks[hooknum];
1097
1098                 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1099                 if (basechain == NULL) {
1100                         module_put(type->owner);
1101                         return -ENOMEM;
1102                 }
1103
1104                 if (nla[NFTA_CHAIN_COUNTERS]) {
1105                         stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1106                         if (IS_ERR(stats)) {
1107                                 module_put(type->owner);
1108                                 kfree(basechain);
1109                                 return PTR_ERR(stats);
1110                         }
1111                         basechain->stats = stats;
1112                 } else {
1113                         stats = netdev_alloc_pcpu_stats(struct nft_stats);
1114                         if (stats == NULL) {
1115                                 module_put(type->owner);
1116                                 kfree(basechain);
1117                                 return -ENOMEM;
1118                         }
1119                         rcu_assign_pointer(basechain->stats, stats);
1120                 }
1121
1122                 basechain->type = type;
1123                 chain = &basechain->chain;
1124
1125                 for (i = 0; i < afi->nops; i++) {
1126                         ops = &basechain->ops[i];
1127                         ops->pf         = family;
1128                         ops->owner      = afi->owner;
1129                         ops->hooknum    = hooknum;
1130                         ops->priority   = priority;
1131                         ops->priv       = chain;
1132                         ops->hook       = afi->hooks[ops->hooknum];
1133                         if (hookfn)
1134                                 ops->hook = hookfn;
1135                         if (afi->hook_ops_init)
1136                                 afi->hook_ops_init(ops, i);
1137                 }
1138
1139                 chain->flags |= NFT_BASE_CHAIN;
1140                 basechain->policy = policy;
1141         } else {
1142                 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1143                 if (chain == NULL)
1144                         return -ENOMEM;
1145         }
1146
1147         INIT_LIST_HEAD(&chain->rules);
1148         chain->handle = nf_tables_alloc_handle(table);
1149         chain->net = net;
1150         chain->table = table;
1151         nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1152
1153         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1154             chain->flags & NFT_BASE_CHAIN) {
1155                 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1156                 if (err < 0)
1157                         goto err1;
1158         }
1159
1160         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1161         err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1162         if (err < 0)
1163                 goto err2;
1164
1165         table->use++;
1166         list_add_tail_rcu(&chain->list, &table->chains);
1167         return 0;
1168 err2:
1169         if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1170             chain->flags & NFT_BASE_CHAIN) {
1171                 nf_unregister_hooks(nft_base_chain(chain)->ops,
1172                                     afi->nops);
1173         }
1174 err1:
1175         nf_tables_chain_destroy(chain);
1176         return err;
1177 }
1178
1179 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1180                               const struct nlmsghdr *nlh,
1181                               const struct nlattr * const nla[])
1182 {
1183         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1184         struct nft_af_info *afi;
1185         struct nft_table *table;
1186         struct nft_chain *chain;
1187         struct net *net = sock_net(skb->sk);
1188         int family = nfmsg->nfgen_family;
1189         struct nft_ctx ctx;
1190         int err;
1191
1192         afi = nf_tables_afinfo_lookup(net, family, false);
1193         if (IS_ERR(afi))
1194                 return PTR_ERR(afi);
1195
1196         table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1197         if (IS_ERR(table))
1198                 return PTR_ERR(table);
1199         if (table->flags & NFT_TABLE_INACTIVE)
1200                 return -ENOENT;
1201
1202         chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1203         if (IS_ERR(chain))
1204                 return PTR_ERR(chain);
1205         if (chain->flags & NFT_CHAIN_INACTIVE)
1206                 return -ENOENT;
1207         if (chain->use > 0)
1208                 return -EBUSY;
1209
1210         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1211         err = nft_trans_chain_add(&ctx, NFT_MSG_DELCHAIN);
1212         if (err < 0)
1213                 return err;
1214
1215         table->use--;
1216         list_del_rcu(&chain->list);
1217         return 0;
1218 }
1219
1220 /*
1221  * Expressions
1222  */
1223
1224 /**
1225  *      nft_register_expr - register nf_tables expr type
1226  *      @ops: expr type
1227  *
1228  *      Registers the expr type for use with nf_tables. Returns zero on
1229  *      success or a negative errno code otherwise.
1230  */
1231 int nft_register_expr(struct nft_expr_type *type)
1232 {
1233         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1234         if (type->family == NFPROTO_UNSPEC)
1235                 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1236         else
1237                 list_add_rcu(&type->list, &nf_tables_expressions);
1238         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1239         return 0;
1240 }
1241 EXPORT_SYMBOL_GPL(nft_register_expr);
1242
1243 /**
1244  *      nft_unregister_expr - unregister nf_tables expr type
1245  *      @ops: expr type
1246  *
1247  *      Unregisters the expr typefor use with nf_tables.
1248  */
1249 void nft_unregister_expr(struct nft_expr_type *type)
1250 {
1251         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1252         list_del_rcu(&type->list);
1253         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1254 }
1255 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1256
1257 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1258                                                        struct nlattr *nla)
1259 {
1260         const struct nft_expr_type *type;
1261
1262         list_for_each_entry(type, &nf_tables_expressions, list) {
1263                 if (!nla_strcmp(nla, type->name) &&
1264                     (!type->family || type->family == family))
1265                         return type;
1266         }
1267         return NULL;
1268 }
1269
1270 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1271                                                      struct nlattr *nla)
1272 {
1273         const struct nft_expr_type *type;
1274
1275         if (nla == NULL)
1276                 return ERR_PTR(-EINVAL);
1277
1278         type = __nft_expr_type_get(family, nla);
1279         if (type != NULL && try_module_get(type->owner))
1280                 return type;
1281
1282 #ifdef CONFIG_MODULES
1283         if (type == NULL) {
1284                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1285                 request_module("nft-expr-%u-%.*s", family,
1286                                nla_len(nla), (char *)nla_data(nla));
1287                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1288                 if (__nft_expr_type_get(family, nla))
1289                         return ERR_PTR(-EAGAIN);
1290
1291                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1292                 request_module("nft-expr-%.*s",
1293                                nla_len(nla), (char *)nla_data(nla));
1294                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1295                 if (__nft_expr_type_get(family, nla))
1296                         return ERR_PTR(-EAGAIN);
1297         }
1298 #endif
1299         return ERR_PTR(-ENOENT);
1300 }
1301
1302 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1303         [NFTA_EXPR_NAME]        = { .type = NLA_STRING },
1304         [NFTA_EXPR_DATA]        = { .type = NLA_NESTED },
1305 };
1306
1307 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1308                                     const struct nft_expr *expr)
1309 {
1310         if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1311                 goto nla_put_failure;
1312
1313         if (expr->ops->dump) {
1314                 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1315                 if (data == NULL)
1316                         goto nla_put_failure;
1317                 if (expr->ops->dump(skb, expr) < 0)
1318                         goto nla_put_failure;
1319                 nla_nest_end(skb, data);
1320         }
1321
1322         return skb->len;
1323
1324 nla_put_failure:
1325         return -1;
1326 };
1327
1328 struct nft_expr_info {
1329         const struct nft_expr_ops       *ops;
1330         struct nlattr                   *tb[NFT_EXPR_MAXATTR + 1];
1331 };
1332
1333 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1334                                 const struct nlattr *nla,
1335                                 struct nft_expr_info *info)
1336 {
1337         const struct nft_expr_type *type;
1338         const struct nft_expr_ops *ops;
1339         struct nlattr *tb[NFTA_EXPR_MAX + 1];
1340         int err;
1341
1342         err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1343         if (err < 0)
1344                 return err;
1345
1346         type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1347         if (IS_ERR(type))
1348                 return PTR_ERR(type);
1349
1350         if (tb[NFTA_EXPR_DATA]) {
1351                 err = nla_parse_nested(info->tb, type->maxattr,
1352                                        tb[NFTA_EXPR_DATA], type->policy);
1353                 if (err < 0)
1354                         goto err1;
1355         } else
1356                 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1357
1358         if (type->select_ops != NULL) {
1359                 ops = type->select_ops(ctx,
1360                                        (const struct nlattr * const *)info->tb);
1361                 if (IS_ERR(ops)) {
1362                         err = PTR_ERR(ops);
1363                         goto err1;
1364                 }
1365         } else
1366                 ops = type->ops;
1367
1368         info->ops = ops;
1369         return 0;
1370
1371 err1:
1372         module_put(type->owner);
1373         return err;
1374 }
1375
1376 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1377                              const struct nft_expr_info *info,
1378                              struct nft_expr *expr)
1379 {
1380         const struct nft_expr_ops *ops = info->ops;
1381         int err;
1382
1383         expr->ops = ops;
1384         if (ops->init) {
1385                 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1386                 if (err < 0)
1387                         goto err1;
1388         }
1389
1390         return 0;
1391
1392 err1:
1393         expr->ops = NULL;
1394         return err;
1395 }
1396
1397 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1398                                    struct nft_expr *expr)
1399 {
1400         if (expr->ops->destroy)
1401                 expr->ops->destroy(ctx, expr);
1402         module_put(expr->ops->type->owner);
1403 }
1404
1405 /*
1406  * Rules
1407  */
1408
1409 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1410                                                 u64 handle)
1411 {
1412         struct nft_rule *rule;
1413
1414         // FIXME: this sucks
1415         list_for_each_entry(rule, &chain->rules, list) {
1416                 if (handle == rule->handle)
1417                         return rule;
1418         }
1419
1420         return ERR_PTR(-ENOENT);
1421 }
1422
1423 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1424                                               const struct nlattr *nla)
1425 {
1426         if (nla == NULL)
1427                 return ERR_PTR(-EINVAL);
1428
1429         return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1430 }
1431
1432 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1433         [NFTA_RULE_TABLE]       = { .type = NLA_STRING },
1434         [NFTA_RULE_CHAIN]       = { .type = NLA_STRING,
1435                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
1436         [NFTA_RULE_HANDLE]      = { .type = NLA_U64 },
1437         [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1438         [NFTA_RULE_COMPAT]      = { .type = NLA_NESTED },
1439         [NFTA_RULE_POSITION]    = { .type = NLA_U64 },
1440         [NFTA_RULE_USERDATA]    = { .type = NLA_BINARY,
1441                                     .len = NFT_USERDATA_MAXLEN },
1442 };
1443
1444 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1445                                     int event, u32 flags, int family,
1446                                     const struct nft_table *table,
1447                                     const struct nft_chain *chain,
1448                                     const struct nft_rule *rule)
1449 {
1450         struct nlmsghdr *nlh;
1451         struct nfgenmsg *nfmsg;
1452         const struct nft_expr *expr, *next;
1453         struct nlattr *list;
1454         const struct nft_rule *prule;
1455         int type = event | NFNL_SUBSYS_NFTABLES << 8;
1456
1457         nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1458                         flags);
1459         if (nlh == NULL)
1460                 goto nla_put_failure;
1461
1462         nfmsg = nlmsg_data(nlh);
1463         nfmsg->nfgen_family     = family;
1464         nfmsg->version          = NFNETLINK_V0;
1465         nfmsg->res_id           = 0;
1466
1467         if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1468                 goto nla_put_failure;
1469         if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1470                 goto nla_put_failure;
1471         if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1472                 goto nla_put_failure;
1473
1474         if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1475                 prule = list_entry(rule->list.prev, struct nft_rule, list);
1476                 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1477                                  cpu_to_be64(prule->handle)))
1478                         goto nla_put_failure;
1479         }
1480
1481         list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1482         if (list == NULL)
1483                 goto nla_put_failure;
1484         nft_rule_for_each_expr(expr, next, rule) {
1485                 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1486                 if (elem == NULL)
1487                         goto nla_put_failure;
1488                 if (nf_tables_fill_expr_info(skb, expr) < 0)
1489                         goto nla_put_failure;
1490                 nla_nest_end(skb, elem);
1491         }
1492         nla_nest_end(skb, list);
1493
1494         if (rule->ulen &&
1495             nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1496                 goto nla_put_failure;
1497
1498         return nlmsg_end(skb, nlh);
1499
1500 nla_put_failure:
1501         nlmsg_trim(skb, nlh);
1502         return -1;
1503 }
1504
1505 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1506                                  const struct nft_rule *rule,
1507                                  int event)
1508 {
1509         struct sk_buff *skb;
1510         int err;
1511
1512         if (!ctx->report &&
1513             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1514                 return 0;
1515
1516         err = -ENOBUFS;
1517         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1518         if (skb == NULL)
1519                 goto err;
1520
1521         err = nf_tables_fill_rule_info(skb, ctx->portid, ctx->seq, event, 0,
1522                                        ctx->afi->family, ctx->table,
1523                                        ctx->chain, rule);
1524         if (err < 0) {
1525                 kfree_skb(skb);
1526                 goto err;
1527         }
1528
1529         err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1530                              ctx->report, GFP_KERNEL);
1531 err:
1532         if (err < 0) {
1533                 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1534                                   err);
1535         }
1536         return err;
1537 }
1538
1539 static inline bool
1540 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1541 {
1542         return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1543 }
1544
1545 static inline int gencursor_next(struct net *net)
1546 {
1547         return net->nft.gencursor+1 == 1 ? 1 : 0;
1548 }
1549
1550 static inline int
1551 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1552 {
1553         return (rule->genmask & (1 << gencursor_next(net))) == 0;
1554 }
1555
1556 static inline void
1557 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1558 {
1559         /* Now inactive, will be active in the future */
1560         rule->genmask = (1 << net->nft.gencursor);
1561 }
1562
1563 static inline void
1564 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1565 {
1566         rule->genmask = (1 << gencursor_next(net));
1567 }
1568
1569 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1570 {
1571         rule->genmask = 0;
1572 }
1573
1574 static int nf_tables_dump_rules(struct sk_buff *skb,
1575                                 struct netlink_callback *cb)
1576 {
1577         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1578         const struct nft_af_info *afi;
1579         const struct nft_table *table;
1580         const struct nft_chain *chain;
1581         const struct nft_rule *rule;
1582         unsigned int idx = 0, s_idx = cb->args[0];
1583         struct net *net = sock_net(skb->sk);
1584         int family = nfmsg->nfgen_family;
1585
1586         rcu_read_lock();
1587         cb->seq = net->nft.base_seq;
1588
1589         list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1590                 if (family != NFPROTO_UNSPEC && family != afi->family)
1591                         continue;
1592
1593                 list_for_each_entry_rcu(table, &afi->tables, list) {
1594                         list_for_each_entry_rcu(chain, &table->chains, list) {
1595                                 list_for_each_entry_rcu(rule, &chain->rules, list) {
1596                                         if (!nft_rule_is_active(net, rule))
1597                                                 goto cont;
1598                                         if (idx < s_idx)
1599                                                 goto cont;
1600                                         if (idx > s_idx)
1601                                                 memset(&cb->args[1], 0,
1602                                                        sizeof(cb->args) - sizeof(cb->args[0]));
1603                                         if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1604                                                                       cb->nlh->nlmsg_seq,
1605                                                                       NFT_MSG_NEWRULE,
1606                                                                       NLM_F_MULTI | NLM_F_APPEND,
1607                                                                       afi->family, table, chain, rule) < 0)
1608                                                 goto done;
1609
1610                                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1611 cont:
1612                                         idx++;
1613                                 }
1614                         }
1615                 }
1616         }
1617 done:
1618         rcu_read_unlock();
1619
1620         cb->args[0] = idx;
1621         return skb->len;
1622 }
1623
1624 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1625                              const struct nlmsghdr *nlh,
1626                              const struct nlattr * const nla[])
1627 {
1628         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1629         const struct nft_af_info *afi;
1630         const struct nft_table *table;
1631         const struct nft_chain *chain;
1632         const struct nft_rule *rule;
1633         struct sk_buff *skb2;
1634         struct net *net = sock_net(skb->sk);
1635         int family = nfmsg->nfgen_family;
1636         int err;
1637
1638         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1639                 struct netlink_dump_control c = {
1640                         .dump = nf_tables_dump_rules,
1641                 };
1642                 return netlink_dump_start(nlsk, skb, nlh, &c);
1643         }
1644
1645         afi = nf_tables_afinfo_lookup(net, family, false);
1646         if (IS_ERR(afi))
1647                 return PTR_ERR(afi);
1648
1649         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1650         if (IS_ERR(table))
1651                 return PTR_ERR(table);
1652         if (table->flags & NFT_TABLE_INACTIVE)
1653                 return -ENOENT;
1654
1655         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1656         if (IS_ERR(chain))
1657                 return PTR_ERR(chain);
1658         if (chain->flags & NFT_CHAIN_INACTIVE)
1659                 return -ENOENT;
1660
1661         rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1662         if (IS_ERR(rule))
1663                 return PTR_ERR(rule);
1664
1665         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1666         if (!skb2)
1667                 return -ENOMEM;
1668
1669         err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1670                                        nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1671                                        family, table, chain, rule);
1672         if (err < 0)
1673                 goto err;
1674
1675         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1676
1677 err:
1678         kfree_skb(skb2);
1679         return err;
1680 }
1681
1682 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1683                                    struct nft_rule *rule)
1684 {
1685         struct nft_expr *expr, *next;
1686
1687         /*
1688          * Careful: some expressions might not be initialized in case this
1689          * is called on error from nf_tables_newrule().
1690          */
1691         expr = nft_expr_first(rule);
1692         while (expr != nft_expr_last(rule) && expr->ops) {
1693                 next = nft_expr_next(expr);
1694                 nf_tables_expr_destroy(ctx, expr);
1695                 expr = next;
1696         }
1697         kfree(rule);
1698 }
1699
1700 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
1701                                             struct nft_rule *rule)
1702 {
1703         struct nft_trans *trans;
1704
1705         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
1706         if (trans == NULL)
1707                 return NULL;
1708
1709         nft_trans_rule(trans) = rule;
1710         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1711
1712         return trans;
1713 }
1714
1715 #define NFT_RULE_MAXEXPRS       128
1716
1717 static struct nft_expr_info *info;
1718
1719 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1720                              const struct nlmsghdr *nlh,
1721                              const struct nlattr * const nla[])
1722 {
1723         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1724         struct nft_af_info *afi;
1725         struct net *net = sock_net(skb->sk);
1726         struct nft_table *table;
1727         struct nft_chain *chain;
1728         struct nft_rule *rule, *old_rule = NULL;
1729         struct nft_trans *trans = NULL;
1730         struct nft_expr *expr;
1731         struct nft_ctx ctx;
1732         struct nlattr *tmp;
1733         unsigned int size, i, n, ulen = 0;
1734         int err, rem;
1735         bool create;
1736         u64 handle, pos_handle;
1737
1738         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1739
1740         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1741         if (IS_ERR(afi))
1742                 return PTR_ERR(afi);
1743
1744         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1745         if (IS_ERR(table))
1746                 return PTR_ERR(table);
1747
1748         chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1749         if (IS_ERR(chain))
1750                 return PTR_ERR(chain);
1751
1752         if (nla[NFTA_RULE_HANDLE]) {
1753                 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1754                 rule = __nf_tables_rule_lookup(chain, handle);
1755                 if (IS_ERR(rule))
1756                         return PTR_ERR(rule);
1757
1758                 if (nlh->nlmsg_flags & NLM_F_EXCL)
1759                         return -EEXIST;
1760                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1761                         old_rule = rule;
1762                 else
1763                         return -EOPNOTSUPP;
1764         } else {
1765                 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1766                         return -EINVAL;
1767                 handle = nf_tables_alloc_handle(table);
1768
1769                 if (chain->use == UINT_MAX)
1770                         return -EOVERFLOW;
1771
1772                 if (nla[NFTA_RULE_POSITION]) {
1773                         pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1774                         old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1775                         if (IS_ERR(old_rule))
1776                                 return PTR_ERR(old_rule);
1777                 }
1778         }
1779
1780         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1781
1782         n = 0;
1783         size = 0;
1784         if (nla[NFTA_RULE_EXPRESSIONS]) {
1785                 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1786                         err = -EINVAL;
1787                         if (nla_type(tmp) != NFTA_LIST_ELEM)
1788                                 goto err1;
1789                         if (n == NFT_RULE_MAXEXPRS)
1790                                 goto err1;
1791                         err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1792                         if (err < 0)
1793                                 goto err1;
1794                         size += info[n].ops->size;
1795                         n++;
1796                 }
1797         }
1798         /* Check for overflow of dlen field */
1799         err = -EFBIG;
1800         if (size >= 1 << 12)
1801                 goto err1;
1802
1803         if (nla[NFTA_RULE_USERDATA])
1804                 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1805
1806         err = -ENOMEM;
1807         rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1808         if (rule == NULL)
1809                 goto err1;
1810
1811         nft_rule_activate_next(net, rule);
1812
1813         rule->handle = handle;
1814         rule->dlen   = size;
1815         rule->ulen   = ulen;
1816
1817         if (ulen)
1818                 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1819
1820         expr = nft_expr_first(rule);
1821         for (i = 0; i < n; i++) {
1822                 err = nf_tables_newexpr(&ctx, &info[i], expr);
1823                 if (err < 0)
1824                         goto err2;
1825                 info[i].ops = NULL;
1826                 expr = nft_expr_next(expr);
1827         }
1828
1829         if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1830                 if (!nft_rule_is_active_next(net, old_rule)) {
1831                         err = -ENOENT;
1832                         goto err2;
1833                 }
1834                 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
1835                                            old_rule);
1836                 if (trans == NULL) {
1837                         err = -ENOMEM;
1838                         goto err2;
1839                 }
1840                 nft_rule_disactivate_next(net, old_rule);
1841                 chain->use--;
1842
1843                 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
1844                         err = -ENOMEM;
1845                         goto err2;
1846                 }
1847
1848                 list_add_tail_rcu(&rule->list, &old_rule->list);
1849         } else {
1850                 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
1851                         err = -ENOMEM;
1852                         goto err2;
1853                 }
1854
1855                 if (nlh->nlmsg_flags & NLM_F_APPEND) {
1856                         if (old_rule)
1857                                 list_add_rcu(&rule->list, &old_rule->list);
1858                         else
1859                                 list_add_tail_rcu(&rule->list, &chain->rules);
1860                  } else {
1861                         if (old_rule)
1862                                 list_add_tail_rcu(&rule->list, &old_rule->list);
1863                         else
1864                                 list_add_rcu(&rule->list, &chain->rules);
1865                 }
1866         }
1867         chain->use++;
1868         return 0;
1869
1870 err2:
1871         nf_tables_rule_destroy(&ctx, rule);
1872 err1:
1873         for (i = 0; i < n; i++) {
1874                 if (info[i].ops != NULL)
1875                         module_put(info[i].ops->type->owner);
1876         }
1877         return err;
1878 }
1879
1880 static int
1881 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1882 {
1883         /* You cannot delete the same rule twice */
1884         if (nft_rule_is_active_next(ctx->net, rule)) {
1885                 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
1886                         return -ENOMEM;
1887                 nft_rule_disactivate_next(ctx->net, rule);
1888                 ctx->chain->use--;
1889                 return 0;
1890         }
1891         return -ENOENT;
1892 }
1893
1894 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1895 {
1896         struct nft_rule *rule;
1897         int err;
1898
1899         list_for_each_entry(rule, &ctx->chain->rules, list) {
1900                 if (!nft_rule_is_active_next(ctx->net, rule))
1901                         continue;
1902
1903                 err = nf_tables_delrule_one(ctx, rule);
1904                 if (err < 0)
1905                         return err;
1906         }
1907         return 0;
1908 }
1909
1910 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1911                              const struct nlmsghdr *nlh,
1912                              const struct nlattr * const nla[])
1913 {
1914         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1915         struct nft_af_info *afi;
1916         struct net *net = sock_net(skb->sk);
1917         struct nft_table *table;
1918         struct nft_chain *chain = NULL;
1919         struct nft_rule *rule;
1920         int family = nfmsg->nfgen_family, err = 0;
1921         struct nft_ctx ctx;
1922
1923         afi = nf_tables_afinfo_lookup(net, family, false);
1924         if (IS_ERR(afi))
1925                 return PTR_ERR(afi);
1926
1927         table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1928         if (IS_ERR(table))
1929                 return PTR_ERR(table);
1930         if (table->flags & NFT_TABLE_INACTIVE)
1931                 return -ENOENT;
1932
1933         if (nla[NFTA_RULE_CHAIN]) {
1934                 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1935                 if (IS_ERR(chain))
1936                         return PTR_ERR(chain);
1937         }
1938
1939         nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1940
1941         if (chain) {
1942                 if (nla[NFTA_RULE_HANDLE]) {
1943                         rule = nf_tables_rule_lookup(chain,
1944                                                      nla[NFTA_RULE_HANDLE]);
1945                         if (IS_ERR(rule))
1946                                 return PTR_ERR(rule);
1947
1948                         err = nf_tables_delrule_one(&ctx, rule);
1949                 } else {
1950                         err = nf_table_delrule_by_chain(&ctx);
1951                 }
1952         } else {
1953                 list_for_each_entry(chain, &table->chains, list) {
1954                         ctx.chain = chain;
1955                         err = nf_table_delrule_by_chain(&ctx);
1956                         if (err < 0)
1957                                 break;
1958                 }
1959         }
1960
1961         return err;
1962 }
1963
1964 /*
1965  * Sets
1966  */
1967
1968 static LIST_HEAD(nf_tables_set_ops);
1969
1970 int nft_register_set(struct nft_set_ops *ops)
1971 {
1972         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1973         list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
1974         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1975         return 0;
1976 }
1977 EXPORT_SYMBOL_GPL(nft_register_set);
1978
1979 void nft_unregister_set(struct nft_set_ops *ops)
1980 {
1981         nfnl_lock(NFNL_SUBSYS_NFTABLES);
1982         list_del_rcu(&ops->list);
1983         nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1984 }
1985 EXPORT_SYMBOL_GPL(nft_unregister_set);
1986
1987 /*
1988  * Select a set implementation based on the data characteristics and the
1989  * given policy. The total memory use might not be known if no size is
1990  * given, in that case the amount of memory per element is used.
1991  */
1992 static const struct nft_set_ops *
1993 nft_select_set_ops(const struct nlattr * const nla[],
1994                    const struct nft_set_desc *desc,
1995                    enum nft_set_policies policy)
1996 {
1997         const struct nft_set_ops *ops, *bops;
1998         struct nft_set_estimate est, best;
1999         u32 features;
2000
2001 #ifdef CONFIG_MODULES
2002         if (list_empty(&nf_tables_set_ops)) {
2003                 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2004                 request_module("nft-set");
2005                 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2006                 if (!list_empty(&nf_tables_set_ops))
2007                         return ERR_PTR(-EAGAIN);
2008         }
2009 #endif
2010         features = 0;
2011         if (nla[NFTA_SET_FLAGS] != NULL) {
2012                 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2013                 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2014         }
2015
2016         bops       = NULL;
2017         best.size  = ~0;
2018         best.class = ~0;
2019
2020         list_for_each_entry(ops, &nf_tables_set_ops, list) {
2021                 if ((ops->features & features) != features)
2022                         continue;
2023                 if (!ops->estimate(desc, features, &est))
2024                         continue;
2025
2026                 switch (policy) {
2027                 case NFT_SET_POL_PERFORMANCE:
2028                         if (est.class < best.class)
2029                                 break;
2030                         if (est.class == best.class && est.size < best.size)
2031                                 break;
2032                         continue;
2033                 case NFT_SET_POL_MEMORY:
2034                         if (est.size < best.size)
2035                                 break;
2036                         if (est.size == best.size && est.class < best.class)
2037                                 break;
2038                         continue;
2039                 default:
2040                         break;
2041                 }
2042
2043                 if (!try_module_get(ops->owner))
2044                         continue;
2045                 if (bops != NULL)
2046                         module_put(bops->owner);
2047
2048                 bops = ops;
2049                 best = est;
2050         }
2051
2052         if (bops != NULL)
2053                 return bops;
2054
2055         return ERR_PTR(-EOPNOTSUPP);
2056 }
2057
2058 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2059         [NFTA_SET_TABLE]                = { .type = NLA_STRING },
2060         [NFTA_SET_NAME]                 = { .type = NLA_STRING,
2061                                             .len = IFNAMSIZ - 1 },
2062         [NFTA_SET_FLAGS]                = { .type = NLA_U32 },
2063         [NFTA_SET_KEY_TYPE]             = { .type = NLA_U32 },
2064         [NFTA_SET_KEY_LEN]              = { .type = NLA_U32 },
2065         [NFTA_SET_DATA_TYPE]            = { .type = NLA_U32 },
2066         [NFTA_SET_DATA_LEN]             = { .type = NLA_U32 },
2067         [NFTA_SET_POLICY]               = { .type = NLA_U32 },
2068         [NFTA_SET_DESC]                 = { .type = NLA_NESTED },
2069         [NFTA_SET_ID]                   = { .type = NLA_U32 },
2070 };
2071
2072 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2073         [NFTA_SET_DESC_SIZE]            = { .type = NLA_U32 },
2074 };
2075
2076 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2077                                      const struct sk_buff *skb,
2078                                      const struct nlmsghdr *nlh,
2079                                      const struct nlattr * const nla[])
2080 {
2081         struct net *net = sock_net(skb->sk);
2082         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2083         struct nft_af_info *afi = NULL;
2084         struct nft_table *table = NULL;
2085
2086         if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2087                 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2088                 if (IS_ERR(afi))
2089                         return PTR_ERR(afi);
2090         }
2091
2092         if (nla[NFTA_SET_TABLE] != NULL) {
2093                 if (afi == NULL)
2094                         return -EAFNOSUPPORT;
2095
2096                 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2097                 if (IS_ERR(table))
2098                         return PTR_ERR(table);
2099                 if (table->flags & NFT_TABLE_INACTIVE)
2100                         return -ENOENT;
2101         }
2102
2103         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2104         return 0;
2105 }
2106
2107 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2108                                      const struct nlattr *nla)
2109 {
2110         struct nft_set *set;
2111
2112         if (nla == NULL)
2113                 return ERR_PTR(-EINVAL);
2114
2115         list_for_each_entry(set, &table->sets, list) {
2116                 if (!nla_strcmp(nla, set->name))
2117                         return set;
2118         }
2119         return ERR_PTR(-ENOENT);
2120 }
2121
2122 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2123                                           const struct nlattr *nla)
2124 {
2125         struct nft_trans *trans;
2126         u32 id = ntohl(nla_get_be32(nla));
2127
2128         list_for_each_entry(trans, &net->nft.commit_list, list) {
2129                 if (trans->msg_type == NFT_MSG_NEWSET &&
2130                     id == nft_trans_set_id(trans))
2131                         return nft_trans_set(trans);
2132         }
2133         return ERR_PTR(-ENOENT);
2134 }
2135
2136 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2137                                     const char *name)
2138 {
2139         const struct nft_set *i;
2140         const char *p;
2141         unsigned long *inuse;
2142         unsigned int n = 0, min = 0;
2143
2144         p = strnchr(name, IFNAMSIZ, '%');
2145         if (p != NULL) {
2146                 if (p[1] != 'd' || strchr(p + 2, '%'))
2147                         return -EINVAL;
2148
2149                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2150                 if (inuse == NULL)
2151                         return -ENOMEM;
2152 cont:
2153                 list_for_each_entry(i, &ctx->table->sets, list) {
2154                         int tmp;
2155
2156                         if (!sscanf(i->name, name, &tmp))
2157                                 continue;
2158                         if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2159                                 continue;
2160
2161                         set_bit(tmp - min, inuse);
2162                 }
2163
2164                 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2165                 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2166                         min += BITS_PER_BYTE * PAGE_SIZE;
2167                         memset(inuse, 0, PAGE_SIZE);
2168                         goto cont;
2169                 }
2170                 free_page((unsigned long)inuse);
2171         }
2172
2173         snprintf(set->name, sizeof(set->name), name, min + n);
2174         list_for_each_entry(i, &ctx->table->sets, list) {
2175                 if (!strcmp(set->name, i->name))
2176                         return -ENFILE;
2177         }
2178         return 0;
2179 }
2180
2181 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2182                               const struct nft_set *set, u16 event, u16 flags)
2183 {
2184         struct nfgenmsg *nfmsg;
2185         struct nlmsghdr *nlh;
2186         struct nlattr *desc;
2187         u32 portid = ctx->portid;
2188         u32 seq = ctx->seq;
2189
2190         event |= NFNL_SUBSYS_NFTABLES << 8;
2191         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2192                         flags);
2193         if (nlh == NULL)
2194                 goto nla_put_failure;
2195
2196         nfmsg = nlmsg_data(nlh);
2197         nfmsg->nfgen_family     = ctx->afi->family;
2198         nfmsg->version          = NFNETLINK_V0;
2199         nfmsg->res_id           = 0;
2200
2201         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2202                 goto nla_put_failure;
2203         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2204                 goto nla_put_failure;
2205         if (set->flags != 0)
2206                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2207                         goto nla_put_failure;
2208
2209         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2210                 goto nla_put_failure;
2211         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2212                 goto nla_put_failure;
2213         if (set->flags & NFT_SET_MAP) {
2214                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2215                         goto nla_put_failure;
2216                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2217                         goto nla_put_failure;
2218         }
2219
2220         desc = nla_nest_start(skb, NFTA_SET_DESC);
2221         if (desc == NULL)
2222                 goto nla_put_failure;
2223         if (set->size &&
2224             nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2225                 goto nla_put_failure;
2226         nla_nest_end(skb, desc);
2227
2228         return nlmsg_end(skb, nlh);
2229
2230 nla_put_failure:
2231         nlmsg_trim(skb, nlh);
2232         return -1;
2233 }
2234
2235 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2236                                 const struct nft_set *set,
2237                                 int event, gfp_t gfp_flags)
2238 {
2239         struct sk_buff *skb;
2240         u32 portid = ctx->portid;
2241         int err;
2242
2243         if (!ctx->report &&
2244             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2245                 return 0;
2246
2247         err = -ENOBUFS;
2248         skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2249         if (skb == NULL)
2250                 goto err;
2251
2252         err = nf_tables_fill_set(skb, ctx, set, event, 0);
2253         if (err < 0) {
2254                 kfree_skb(skb);
2255                 goto err;
2256         }
2257
2258         err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2259                              ctx->report, gfp_flags);
2260 err:
2261         if (err < 0)
2262                 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2263         return err;
2264 }
2265
2266 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2267                                      struct netlink_callback *cb)
2268 {
2269         const struct nft_set *set;
2270         unsigned int idx = 0, s_idx = cb->args[0];
2271
2272         if (cb->args[1])
2273                 return skb->len;
2274
2275         rcu_read_lock();
2276         cb->seq = ctx->net->nft.base_seq;
2277
2278         list_for_each_entry_rcu(set, &ctx->table->sets, list) {
2279                 if (idx < s_idx)
2280                         goto cont;
2281                 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2282                                        NLM_F_MULTI) < 0) {
2283                         cb->args[0] = idx;
2284                         goto done;
2285                 }
2286                 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2287 cont:
2288                 idx++;
2289         }
2290         cb->args[1] = 1;
2291 done:
2292         rcu_read_unlock();
2293         return skb->len;
2294 }
2295
2296 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2297                                       struct netlink_callback *cb)
2298 {
2299         const struct nft_set *set;
2300         unsigned int idx, s_idx = cb->args[0];
2301         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2302
2303         if (cb->args[1])
2304                 return skb->len;
2305
2306         rcu_read_lock();
2307         cb->seq = ctx->net->nft.base_seq;
2308
2309         list_for_each_entry_rcu(table, &ctx->afi->tables, list) {
2310                 if (cur_table) {
2311                         if (cur_table != table)
2312                                 continue;
2313
2314                         cur_table = NULL;
2315                 }
2316                 ctx->table = table;
2317                 idx = 0;
2318                 list_for_each_entry_rcu(set, &ctx->table->sets, list) {
2319                         if (idx < s_idx)
2320                                 goto cont;
2321                         if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2322                                                NLM_F_MULTI) < 0) {
2323                                 cb->args[0] = idx;
2324                                 cb->args[2] = (unsigned long) table;
2325                                 goto done;
2326                         }
2327                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2328 cont:
2329                         idx++;
2330                 }
2331         }
2332         cb->args[1] = 1;
2333 done:
2334         rcu_read_unlock();
2335         return skb->len;
2336 }
2337
2338 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2339                                    struct netlink_callback *cb)
2340 {
2341         const struct nft_set *set;
2342         unsigned int idx, s_idx = cb->args[0];
2343         struct nft_af_info *afi;
2344         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2345         struct net *net = sock_net(skb->sk);
2346         int cur_family = cb->args[3];
2347
2348         if (cb->args[1])
2349                 return skb->len;
2350
2351         rcu_read_lock();
2352         cb->seq = net->nft.base_seq;
2353
2354         list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2355                 if (cur_family) {
2356                         if (afi->family != cur_family)
2357                                 continue;
2358
2359                         cur_family = 0;
2360                 }
2361
2362                 list_for_each_entry_rcu(table, &afi->tables, list) {
2363                         if (cur_table) {
2364                                 if (cur_table != table)
2365                                         continue;
2366
2367                                 cur_table = NULL;
2368                         }
2369
2370                         ctx->table = table;
2371                         ctx->afi = afi;
2372                         idx = 0;
2373                         list_for_each_entry_rcu(set, &ctx->table->sets, list) {
2374                                 if (idx < s_idx)
2375                                         goto cont;
2376                                 if (nf_tables_fill_set(skb, ctx, set,
2377                                                        NFT_MSG_NEWSET,
2378                                                        NLM_F_MULTI) < 0) {
2379                                         cb->args[0] = idx;
2380                                         cb->args[2] = (unsigned long) table;
2381                                         cb->args[3] = afi->family;
2382                                         goto done;
2383                                 }
2384                                 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2385 cont:
2386                                 idx++;
2387                         }
2388                         if (s_idx)
2389                                 s_idx = 0;
2390                 }
2391         }
2392         cb->args[1] = 1;
2393 done:
2394         rcu_read_unlock();
2395         return skb->len;
2396 }
2397
2398 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2399 {
2400         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2401         struct nlattr *nla[NFTA_SET_MAX + 1];
2402         struct nft_ctx ctx;
2403         int err, ret;
2404
2405         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2406                           nft_set_policy);
2407         if (err < 0)
2408                 return err;
2409
2410         err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2411         if (err < 0)
2412                 return err;
2413
2414         if (ctx.table == NULL) {
2415                 if (ctx.afi == NULL)
2416                         ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2417                 else
2418                         ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2419         } else
2420                 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2421
2422         return ret;
2423 }
2424
2425 #define NFT_SET_INACTIVE        (1 << 15)       /* Internal set flag */
2426
2427 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2428                             const struct nlmsghdr *nlh,
2429                             const struct nlattr * const nla[])
2430 {
2431         const struct nft_set *set;
2432         struct nft_ctx ctx;
2433         struct sk_buff *skb2;
2434         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2435         int err;
2436
2437         /* Verify existance before starting dump */
2438         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2439         if (err < 0)
2440                 return err;
2441
2442         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2443                 struct netlink_dump_control c = {
2444                         .dump = nf_tables_dump_sets,
2445                 };
2446                 return netlink_dump_start(nlsk, skb, nlh, &c);
2447         }
2448
2449         /* Only accept unspec with dump */
2450         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2451                 return -EAFNOSUPPORT;
2452
2453         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2454         if (IS_ERR(set))
2455                 return PTR_ERR(set);
2456         if (set->flags & NFT_SET_INACTIVE)
2457                 return -ENOENT;
2458
2459         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2460         if (skb2 == NULL)
2461                 return -ENOMEM;
2462
2463         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2464         if (err < 0)
2465                 goto err;
2466
2467         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2468
2469 err:
2470         kfree_skb(skb2);
2471         return err;
2472 }
2473
2474 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2475                                     struct nft_set_desc *desc,
2476                                     const struct nlattr *nla)
2477 {
2478         struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2479         int err;
2480
2481         err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2482         if (err < 0)
2483                 return err;
2484
2485         if (da[NFTA_SET_DESC_SIZE] != NULL)
2486                 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2487
2488         return 0;
2489 }
2490
2491 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2492                              struct nft_set *set)
2493 {
2494         struct nft_trans *trans;
2495
2496         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2497         if (trans == NULL)
2498                 return -ENOMEM;
2499
2500         if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2501                 nft_trans_set_id(trans) =
2502                         ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2503                 set->flags |= NFT_SET_INACTIVE;
2504         }
2505         nft_trans_set(trans) = set;
2506         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2507
2508         return 0;
2509 }
2510
2511 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2512                             const struct nlmsghdr *nlh,
2513                             const struct nlattr * const nla[])
2514 {
2515         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2516         const struct nft_set_ops *ops;
2517         struct nft_af_info *afi;
2518         struct net *net = sock_net(skb->sk);
2519         struct nft_table *table;
2520         struct nft_set *set;
2521         struct nft_ctx ctx;
2522         char name[IFNAMSIZ];
2523         u64 size;
2524         bool create;
2525         u32 ktype, dtype, flags, policy;
2526         struct nft_set_desc desc;
2527         int err;
2528
2529         if (nla[NFTA_SET_TABLE] == NULL ||
2530             nla[NFTA_SET_NAME] == NULL ||
2531             nla[NFTA_SET_KEY_LEN] == NULL ||
2532             nla[NFTA_SET_ID] == NULL)
2533                 return -EINVAL;
2534
2535         memset(&desc, 0, sizeof(desc));
2536
2537         ktype = NFT_DATA_VALUE;
2538         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2539                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2540                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2541                         return -EINVAL;
2542         }
2543
2544         desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2545         if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2546                 return -EINVAL;
2547
2548         flags = 0;
2549         if (nla[NFTA_SET_FLAGS] != NULL) {
2550                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2551                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2552                               NFT_SET_INTERVAL | NFT_SET_MAP))
2553                         return -EINVAL;
2554         }
2555
2556         dtype = 0;
2557         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2558                 if (!(flags & NFT_SET_MAP))
2559                         return -EINVAL;
2560
2561                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2562                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2563                     dtype != NFT_DATA_VERDICT)
2564                         return -EINVAL;
2565
2566                 if (dtype != NFT_DATA_VERDICT) {
2567                         if (nla[NFTA_SET_DATA_LEN] == NULL)
2568                                 return -EINVAL;
2569                         desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2570                         if (desc.dlen == 0 ||
2571                             desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2572                                 return -EINVAL;
2573                 } else
2574                         desc.dlen = sizeof(struct nft_data);
2575         } else if (flags & NFT_SET_MAP)
2576                 return -EINVAL;
2577
2578         policy = NFT_SET_POL_PERFORMANCE;
2579         if (nla[NFTA_SET_POLICY] != NULL)
2580                 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2581
2582         if (nla[NFTA_SET_DESC] != NULL) {
2583                 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2584                 if (err < 0)
2585                         return err;
2586         }
2587
2588         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2589
2590         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2591         if (IS_ERR(afi))
2592                 return PTR_ERR(afi);
2593
2594         table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2595         if (IS_ERR(table))
2596                 return PTR_ERR(table);
2597
2598         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2599
2600         set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2601         if (IS_ERR(set)) {
2602                 if (PTR_ERR(set) != -ENOENT)
2603                         return PTR_ERR(set);
2604                 set = NULL;
2605         }
2606
2607         if (set != NULL) {
2608                 if (nlh->nlmsg_flags & NLM_F_EXCL)
2609                         return -EEXIST;
2610                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2611                         return -EOPNOTSUPP;
2612                 return 0;
2613         }
2614
2615         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2616                 return -ENOENT;
2617
2618         ops = nft_select_set_ops(nla, &desc, policy);
2619         if (IS_ERR(ops))
2620                 return PTR_ERR(ops);
2621
2622         size = 0;
2623         if (ops->privsize != NULL)
2624                 size = ops->privsize(nla);
2625
2626         err = -ENOMEM;
2627         set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2628         if (set == NULL)
2629                 goto err1;
2630
2631         nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2632         err = nf_tables_set_alloc_name(&ctx, set, name);
2633         if (err < 0)
2634                 goto err2;
2635
2636         INIT_LIST_HEAD(&set->bindings);
2637         set->ops   = ops;
2638         set->ktype = ktype;
2639         set->klen  = desc.klen;
2640         set->dtype = dtype;
2641         set->dlen  = desc.dlen;
2642         set->flags = flags;
2643         set->size  = desc.size;
2644
2645         err = ops->init(set, &desc, nla);
2646         if (err < 0)
2647                 goto err2;
2648
2649         err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2650         if (err < 0)
2651                 goto err3;
2652
2653         list_add_tail_rcu(&set->list, &table->sets);
2654         table->use++;
2655         return 0;
2656
2657 err3:
2658         ops->destroy(set);
2659 err2:
2660         kfree(set);
2661 err1:
2662         module_put(ops->owner);
2663         return err;
2664 }
2665
2666 static void nft_set_destroy(struct nft_set *set)
2667 {
2668         set->ops->destroy(set);
2669         module_put(set->ops->owner);
2670         kfree(set);
2671 }
2672
2673 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2674 {
2675         list_del_rcu(&set->list);
2676         nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2677         nft_set_destroy(set);
2678 }
2679
2680 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2681                             const struct nlmsghdr *nlh,
2682                             const struct nlattr * const nla[])
2683 {
2684         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2685         struct nft_set *set;
2686         struct nft_ctx ctx;
2687         int err;
2688
2689         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2690                 return -EAFNOSUPPORT;
2691         if (nla[NFTA_SET_TABLE] == NULL)
2692                 return -EINVAL;
2693
2694         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2695         if (err < 0)
2696                 return err;
2697
2698         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2699         if (IS_ERR(set))
2700                 return PTR_ERR(set);
2701         if (set->flags & NFT_SET_INACTIVE)
2702                 return -ENOENT;
2703         if (!list_empty(&set->bindings))
2704                 return -EBUSY;
2705
2706         err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2707         if (err < 0)
2708                 return err;
2709
2710         list_del_rcu(&set->list);
2711         ctx.table->use--;
2712         return 0;
2713 }
2714
2715 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2716                                         const struct nft_set *set,
2717                                         const struct nft_set_iter *iter,
2718                                         const struct nft_set_elem *elem)
2719 {
2720         enum nft_registers dreg;
2721
2722         dreg = nft_type_to_reg(set->dtype);
2723         return nft_validate_data_load(ctx, dreg, &elem->data,
2724                                       set->dtype == NFT_DATA_VERDICT ?
2725                                       NFT_DATA_VERDICT : NFT_DATA_VALUE);
2726 }
2727
2728 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2729                        struct nft_set_binding *binding)
2730 {
2731         struct nft_set_binding *i;
2732         struct nft_set_iter iter;
2733
2734         if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2735                 return -EBUSY;
2736
2737         if (set->flags & NFT_SET_MAP) {
2738                 /* If the set is already bound to the same chain all
2739                  * jumps are already validated for that chain.
2740                  */
2741                 list_for_each_entry(i, &set->bindings, list) {
2742                         if (i->chain == binding->chain)
2743                                 goto bind;
2744                 }
2745
2746                 iter.skip       = 0;
2747                 iter.count      = 0;
2748                 iter.err        = 0;
2749                 iter.fn         = nf_tables_bind_check_setelem;
2750
2751                 set->ops->walk(ctx, set, &iter);
2752                 if (iter.err < 0) {
2753                         /* Destroy anonymous sets if binding fails */
2754                         if (set->flags & NFT_SET_ANONYMOUS)
2755                                 nf_tables_set_destroy(ctx, set);
2756
2757                         return iter.err;
2758                 }
2759         }
2760 bind:
2761         binding->chain = ctx->chain;
2762         list_add_tail_rcu(&binding->list, &set->bindings);
2763         return 0;
2764 }
2765
2766 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2767                           struct nft_set_binding *binding)
2768 {
2769         list_del_rcu(&binding->list);
2770
2771         if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2772             !(set->flags & NFT_SET_INACTIVE))
2773                 nf_tables_set_destroy(ctx, set);
2774 }
2775
2776 /*
2777  * Set elements
2778  */
2779
2780 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2781         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
2782         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
2783         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
2784 };
2785
2786 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2787         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING },
2788         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING },
2789         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
2790         [NFTA_SET_ELEM_LIST_SET_ID]     = { .type = NLA_U32 },
2791 };
2792
2793 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2794                                       const struct sk_buff *skb,
2795                                       const struct nlmsghdr *nlh,
2796                                       const struct nlattr * const nla[],
2797                                       bool trans)
2798 {
2799         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2800         struct nft_af_info *afi;
2801         struct nft_table *table;
2802         struct net *net = sock_net(skb->sk);
2803
2804         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2805         if (IS_ERR(afi))
2806                 return PTR_ERR(afi);
2807
2808         table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2809         if (IS_ERR(table))
2810                 return PTR_ERR(table);
2811         if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2812                 return -ENOENT;
2813
2814         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2815         return 0;
2816 }
2817
2818 static int nf_tables_fill_setelem(struct sk_buff *skb,
2819                                   const struct nft_set *set,
2820                                   const struct nft_set_elem *elem)
2821 {
2822         unsigned char *b = skb_tail_pointer(skb);
2823         struct nlattr *nest;
2824
2825         nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2826         if (nest == NULL)
2827                 goto nla_put_failure;
2828
2829         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2830                           set->klen) < 0)
2831                 goto nla_put_failure;
2832
2833         if (set->flags & NFT_SET_MAP &&
2834             !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2835             nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2836                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2837                           set->dlen) < 0)
2838                 goto nla_put_failure;
2839
2840         if (elem->flags != 0)
2841                 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2842                         goto nla_put_failure;
2843
2844         nla_nest_end(skb, nest);
2845         return 0;
2846
2847 nla_put_failure:
2848         nlmsg_trim(skb, b);
2849         return -EMSGSIZE;
2850 }
2851
2852 struct nft_set_dump_args {
2853         const struct netlink_callback   *cb;
2854         struct nft_set_iter             iter;
2855         struct sk_buff                  *skb;
2856 };
2857
2858 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2859                                   const struct nft_set *set,
2860                                   const struct nft_set_iter *iter,
2861                                   const struct nft_set_elem *elem)
2862 {
2863         struct nft_set_dump_args *args;
2864
2865         args = container_of(iter, struct nft_set_dump_args, iter);
2866         return nf_tables_fill_setelem(args->skb, set, elem);
2867 }
2868
2869 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2870 {
2871         const struct nft_set *set;
2872         struct nft_set_dump_args args;
2873         struct nft_ctx ctx;
2874         struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2875         struct nfgenmsg *nfmsg;
2876         struct nlmsghdr *nlh;
2877         struct nlattr *nest;
2878         u32 portid, seq;
2879         int event, err;
2880
2881         err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2882                           NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2883         if (err < 0)
2884                 return err;
2885
2886         err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2887                                          false);
2888         if (err < 0)
2889                 return err;
2890
2891         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2892         if (IS_ERR(set))
2893                 return PTR_ERR(set);
2894         if (set->flags & NFT_SET_INACTIVE)
2895                 return -ENOENT;
2896
2897         event  = NFT_MSG_NEWSETELEM;
2898         event |= NFNL_SUBSYS_NFTABLES << 8;
2899         portid = NETLINK_CB(cb->skb).portid;
2900         seq    = cb->nlh->nlmsg_seq;
2901
2902         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2903                         NLM_F_MULTI);
2904         if (nlh == NULL)
2905                 goto nla_put_failure;
2906
2907         nfmsg = nlmsg_data(nlh);
2908         nfmsg->nfgen_family = ctx.afi->family;
2909         nfmsg->version      = NFNETLINK_V0;
2910         nfmsg->res_id       = 0;
2911
2912         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2913                 goto nla_put_failure;
2914         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2915                 goto nla_put_failure;
2916
2917         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2918         if (nest == NULL)
2919                 goto nla_put_failure;
2920
2921         args.cb         = cb;
2922         args.skb        = skb;
2923         args.iter.skip  = cb->args[0];
2924         args.iter.count = 0;
2925         args.iter.err   = 0;
2926         args.iter.fn    = nf_tables_dump_setelem;
2927         set->ops->walk(&ctx, set, &args.iter);
2928
2929         nla_nest_end(skb, nest);
2930         nlmsg_end(skb, nlh);
2931
2932         if (args.iter.err && args.iter.err != -EMSGSIZE)
2933                 return args.iter.err;
2934         if (args.iter.count == cb->args[0])
2935                 return 0;
2936
2937         cb->args[0] = args.iter.count;
2938         return skb->len;
2939
2940 nla_put_failure:
2941         return -ENOSPC;
2942 }
2943
2944 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2945                                 const struct nlmsghdr *nlh,
2946                                 const struct nlattr * const nla[])
2947 {
2948         const struct nft_set *set;
2949         struct nft_ctx ctx;
2950         int err;
2951
2952         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2953         if (err < 0)
2954                 return err;
2955
2956         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2957         if (IS_ERR(set))
2958                 return PTR_ERR(set);
2959         if (set->flags & NFT_SET_INACTIVE)
2960                 return -ENOENT;
2961
2962         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2963                 struct netlink_dump_control c = {
2964                         .dump = nf_tables_dump_set,
2965                 };
2966                 return netlink_dump_start(nlsk, skb, nlh, &c);
2967         }
2968         return -EOPNOTSUPP;
2969 }
2970
2971 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2972                                        const struct nft_ctx *ctx, u32 seq,
2973                                        u32 portid, int event, u16 flags,
2974                                        const struct nft_set *set,
2975                                        const struct nft_set_elem *elem)
2976 {
2977         struct nfgenmsg *nfmsg;
2978         struct nlmsghdr *nlh;
2979         struct nlattr *nest;
2980         int err;
2981
2982         event |= NFNL_SUBSYS_NFTABLES << 8;
2983         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2984                         flags);
2985         if (nlh == NULL)
2986                 goto nla_put_failure;
2987
2988         nfmsg = nlmsg_data(nlh);
2989         nfmsg->nfgen_family     = ctx->afi->family;
2990         nfmsg->version          = NFNETLINK_V0;
2991         nfmsg->res_id           = 0;
2992
2993         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2994                 goto nla_put_failure;
2995         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2996                 goto nla_put_failure;
2997
2998         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2999         if (nest == NULL)
3000                 goto nla_put_failure;
3001
3002         err = nf_tables_fill_setelem(skb, set, elem);
3003         if (err < 0)
3004                 goto nla_put_failure;
3005
3006         nla_nest_end(skb, nest);
3007
3008         return nlmsg_end(skb, nlh);
3009
3010 nla_put_failure:
3011         nlmsg_trim(skb, nlh);
3012         return -1;
3013 }
3014
3015 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3016                                     const struct nft_set *set,
3017                                     const struct nft_set_elem *elem,
3018                                     int event, u16 flags)
3019 {
3020         struct net *net = ctx->net;
3021         u32 portid = ctx->portid;
3022         struct sk_buff *skb;
3023         int err;
3024
3025         if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3026                 return 0;
3027
3028         err = -ENOBUFS;
3029         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3030         if (skb == NULL)
3031                 goto err;
3032
3033         err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3034                                           set, elem);
3035         if (err < 0) {
3036                 kfree_skb(skb);
3037                 goto err;
3038         }
3039
3040         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3041                              GFP_KERNEL);
3042 err:
3043         if (err < 0)
3044                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3045         return err;
3046 }
3047
3048 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3049                                               int msg_type,
3050                                               struct nft_set *set)
3051 {
3052         struct nft_trans *trans;
3053
3054         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3055         if (trans == NULL)
3056                 return NULL;
3057
3058         nft_trans_elem_set(trans) = set;
3059         return trans;
3060 }
3061
3062 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3063                             const struct nlattr *attr)
3064 {
3065         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3066         struct nft_data_desc d1, d2;
3067         struct nft_set_elem elem;
3068         struct nft_set_binding *binding;
3069         enum nft_registers dreg;
3070         struct nft_trans *trans;
3071         int err;
3072
3073         if (set->size && set->nelems == set->size)
3074                 return -ENFILE;
3075
3076         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3077                                nft_set_elem_policy);
3078         if (err < 0)
3079                 return err;
3080
3081         if (nla[NFTA_SET_ELEM_KEY] == NULL)
3082                 return -EINVAL;
3083
3084         elem.flags = 0;
3085         if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3086                 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3087                 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3088                         return -EINVAL;
3089         }
3090
3091         if (set->flags & NFT_SET_MAP) {
3092                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3093                     !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3094                         return -EINVAL;
3095                 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3096                     elem.flags & NFT_SET_ELEM_INTERVAL_END)
3097                         return -EINVAL;
3098         } else {
3099                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3100                         return -EINVAL;
3101         }
3102
3103         err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3104         if (err < 0)
3105                 goto err1;
3106         err = -EINVAL;
3107         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3108                 goto err2;
3109
3110         err = -EEXIST;
3111         if (set->ops->get(set, &elem) == 0)
3112                 goto err2;
3113
3114         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3115                 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3116                 if (err < 0)
3117                         goto err2;
3118
3119                 err = -EINVAL;
3120                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3121                         goto err3;
3122
3123                 dreg = nft_type_to_reg(set->dtype);
3124                 list_for_each_entry(binding, &set->bindings, list) {
3125                         struct nft_ctx bind_ctx = {
3126                                 .afi    = ctx->afi,
3127                                 .table  = ctx->table,
3128                                 .chain  = (struct nft_chain *)binding->chain,
3129                         };
3130
3131                         err = nft_validate_data_load(&bind_ctx, dreg,
3132                                                      &elem.data, d2.type);
3133                         if (err < 0)
3134                                 goto err3;
3135                 }
3136         }
3137
3138         trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3139         if (trans == NULL)
3140                 goto err3;
3141
3142         err = set->ops->insert(set, &elem);
3143         if (err < 0)
3144                 goto err4;
3145
3146         nft_trans_elem(trans) = elem;
3147         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3148         return 0;
3149
3150 err4:
3151         kfree(trans);
3152 err3:
3153         if (nla[NFTA_SET_ELEM_DATA] != NULL)
3154                 nft_data_uninit(&elem.data, d2.type);
3155 err2:
3156         nft_data_uninit(&elem.key, d1.type);
3157 err1:
3158         return err;
3159 }
3160
3161 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3162                                 const struct nlmsghdr *nlh,
3163                                 const struct nlattr * const nla[])
3164 {
3165         struct net *net = sock_net(skb->sk);
3166         const struct nlattr *attr;
3167         struct nft_set *set;
3168         struct nft_ctx ctx;
3169         int rem, err = 0;
3170
3171         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3172         if (err < 0)
3173                 return err;
3174
3175         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3176         if (IS_ERR(set)) {
3177                 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3178                         set = nf_tables_set_lookup_byid(net,
3179                                         nla[NFTA_SET_ELEM_LIST_SET_ID]);
3180                 }
3181                 if (IS_ERR(set))
3182                         return PTR_ERR(set);
3183         }
3184
3185         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3186                 return -EBUSY;
3187
3188         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3189                 err = nft_add_set_elem(&ctx, set, attr);
3190                 if (err < 0)
3191                         break;
3192
3193                 set->nelems++;
3194         }
3195         return err;
3196 }
3197
3198 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3199                            const struct nlattr *attr)
3200 {
3201         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3202         struct nft_data_desc desc;
3203         struct nft_set_elem elem;
3204         struct nft_trans *trans;
3205         int err;
3206
3207         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3208                                nft_set_elem_policy);
3209         if (err < 0)
3210                 goto err1;
3211
3212         err = -EINVAL;
3213         if (nla[NFTA_SET_ELEM_KEY] == NULL)
3214                 goto err1;
3215
3216         err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3217         if (err < 0)
3218                 goto err1;
3219
3220         err = -EINVAL;
3221         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3222                 goto err2;
3223
3224         err = set->ops->get(set, &elem);
3225         if (err < 0)
3226                 goto err2;
3227
3228         trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3229         if (trans == NULL)
3230                 goto err2;
3231
3232         nft_trans_elem(trans) = elem;
3233         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3234
3235         nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3236         if (set->flags & NFT_SET_MAP)
3237                 nft_data_uninit(&elem.data, set->dtype);
3238
3239 err2:
3240         nft_data_uninit(&elem.key, desc.type);
3241 err1:
3242         return err;
3243 }
3244
3245 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3246                                 const struct nlmsghdr *nlh,
3247                                 const struct nlattr * const nla[])
3248 {
3249         const struct nlattr *attr;
3250         struct nft_set *set;
3251         struct nft_ctx ctx;
3252         int rem, err = 0;
3253
3254         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3255         if (err < 0)
3256                 return err;
3257
3258         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3259         if (IS_ERR(set))
3260                 return PTR_ERR(set);
3261         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3262                 return -EBUSY;
3263
3264         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3265                 err = nft_del_setelem(&ctx, set, attr);
3266                 if (err < 0)
3267                         break;
3268
3269                 set->nelems--;
3270         }
3271         return err;
3272 }
3273
3274 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3275         [NFT_MSG_NEWTABLE] = {
3276                 .call_batch     = nf_tables_newtable,
3277                 .attr_count     = NFTA_TABLE_MAX,
3278                 .policy         = nft_table_policy,
3279         },
3280         [NFT_MSG_GETTABLE] = {
3281                 .call           = nf_tables_gettable,
3282                 .attr_count     = NFTA_TABLE_MAX,
3283                 .policy         = nft_table_policy,
3284         },
3285         [NFT_MSG_DELTABLE] = {
3286                 .call_batch     = nf_tables_deltable,
3287                 .attr_count     = NFTA_TABLE_MAX,
3288                 .policy         = nft_table_policy,
3289         },
3290         [NFT_MSG_NEWCHAIN] = {
3291                 .call_batch     = nf_tables_newchain,
3292                 .attr_count     = NFTA_CHAIN_MAX,
3293                 .policy         = nft_chain_policy,
3294         },
3295         [NFT_MSG_GETCHAIN] = {
3296                 .call           = nf_tables_getchain,
3297                 .attr_count     = NFTA_CHAIN_MAX,
3298                 .policy         = nft_chain_policy,
3299         },
3300         [NFT_MSG_DELCHAIN] = {
3301                 .call_batch     = nf_tables_delchain,
3302                 .attr_count     = NFTA_CHAIN_MAX,
3303                 .policy         = nft_chain_policy,
3304         },
3305         [NFT_MSG_NEWRULE] = {
3306                 .call_batch     = nf_tables_newrule,
3307                 .attr_count     = NFTA_RULE_MAX,
3308                 .policy         = nft_rule_policy,
3309         },
3310         [NFT_MSG_GETRULE] = {
3311                 .call           = nf_tables_getrule,
3312                 .attr_count     = NFTA_RULE_MAX,
3313                 .policy         = nft_rule_policy,
3314         },
3315         [NFT_MSG_DELRULE] = {
3316                 .call_batch     = nf_tables_delrule,
3317                 .attr_count     = NFTA_RULE_MAX,
3318                 .policy         = nft_rule_policy,
3319         },
3320         [NFT_MSG_NEWSET] = {
3321                 .call_batch     = nf_tables_newset,
3322                 .attr_count     = NFTA_SET_MAX,
3323                 .policy         = nft_set_policy,
3324         },
3325         [NFT_MSG_GETSET] = {
3326                 .call           = nf_tables_getset,
3327                 .attr_count     = NFTA_SET_MAX,
3328                 .policy         = nft_set_policy,
3329         },
3330         [NFT_MSG_DELSET] = {
3331                 .call_batch     = nf_tables_delset,
3332                 .attr_count     = NFTA_SET_MAX,
3333                 .policy         = nft_set_policy,
3334         },
3335         [NFT_MSG_NEWSETELEM] = {
3336                 .call_batch     = nf_tables_newsetelem,
3337                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3338                 .policy         = nft_set_elem_list_policy,
3339         },
3340         [NFT_MSG_GETSETELEM] = {
3341                 .call           = nf_tables_getsetelem,
3342                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3343                 .policy         = nft_set_elem_list_policy,
3344         },
3345         [NFT_MSG_DELSETELEM] = {
3346                 .call_batch     = nf_tables_delsetelem,
3347                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3348                 .policy         = nft_set_elem_list_policy,
3349         },
3350 };
3351
3352 static void nft_chain_commit_update(struct nft_trans *trans)
3353 {
3354         struct nft_base_chain *basechain;
3355
3356         if (nft_trans_chain_name(trans)[0])
3357                 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3358
3359         if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3360                 return;
3361
3362         basechain = nft_base_chain(trans->ctx.chain);
3363         nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3364
3365         switch (nft_trans_chain_policy(trans)) {
3366         case NF_DROP:
3367         case NF_ACCEPT:
3368                 basechain->policy = nft_trans_chain_policy(trans);
3369                 break;
3370         }
3371 }
3372
3373 /* Schedule objects for release via rcu to make sure no packets are accesing
3374  * removed rules.
3375  */
3376 static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3377 {
3378         struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3379
3380         switch (trans->msg_type) {
3381         case NFT_MSG_DELTABLE:
3382                 nf_tables_table_destroy(&trans->ctx);
3383                 break;
3384         case NFT_MSG_DELCHAIN:
3385                 nf_tables_chain_destroy(trans->ctx.chain);
3386                 break;
3387         case NFT_MSG_DELRULE:
3388                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3389                 break;
3390         case NFT_MSG_DELSET:
3391                 nft_set_destroy(nft_trans_set(trans));
3392                 break;
3393         }
3394         kfree(trans);
3395 }
3396
3397 static int nf_tables_commit(struct sk_buff *skb)
3398 {
3399         struct net *net = sock_net(skb->sk);
3400         struct nft_trans *trans, *next;
3401         struct nft_set *set;
3402
3403         /* Bump generation counter, invalidate any dump in progress */
3404         while (++net->nft.base_seq == 0);
3405
3406         /* A new generation has just started */
3407         net->nft.gencursor = gencursor_next(net);
3408
3409         /* Make sure all packets have left the previous generation before
3410          * purging old rules.
3411          */
3412         synchronize_rcu();
3413
3414         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3415                 switch (trans->msg_type) {
3416                 case NFT_MSG_NEWTABLE:
3417                         if (nft_trans_table_update(trans)) {
3418                                 if (!nft_trans_table_enable(trans)) {
3419                                         nf_tables_table_disable(trans->ctx.afi,
3420                                                                 trans->ctx.table);
3421                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3422                                 }
3423                         } else {
3424                                 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3425                         }
3426                         nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3427                         nft_trans_destroy(trans);
3428                         break;
3429                 case NFT_MSG_DELTABLE:
3430                         nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3431                         break;
3432                 case NFT_MSG_NEWCHAIN:
3433                         if (nft_trans_chain_update(trans))
3434                                 nft_chain_commit_update(trans);
3435                         else
3436                                 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3437
3438                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3439                         nft_trans_destroy(trans);
3440                         break;
3441                 case NFT_MSG_DELCHAIN:
3442                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3443                         if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3444                             trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3445                                 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3446                                                     trans->ctx.afi->nops);
3447                         }
3448                         break;
3449                 case NFT_MSG_NEWRULE:
3450                         nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3451                         nf_tables_rule_notify(&trans->ctx,
3452                                               nft_trans_rule(trans),
3453                                               NFT_MSG_NEWRULE);
3454                         nft_trans_destroy(trans);
3455                         break;
3456                 case NFT_MSG_DELRULE:
3457                         list_del_rcu(&nft_trans_rule(trans)->list);
3458                         nf_tables_rule_notify(&trans->ctx,
3459                                               nft_trans_rule(trans),
3460                                               NFT_MSG_DELRULE);
3461                         break;
3462                 case NFT_MSG_NEWSET:
3463                         nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3464                         /* This avoids hitting -EBUSY when deleting the table
3465                          * from the transaction.
3466                          */
3467                         if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3468                             !list_empty(&nft_trans_set(trans)->bindings))
3469                                 trans->ctx.table->use--;
3470
3471                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3472                                              NFT_MSG_NEWSET, GFP_KERNEL);
3473                         nft_trans_destroy(trans);
3474                         break;
3475                 case NFT_MSG_DELSET:
3476                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3477                                              NFT_MSG_DELSET, GFP_KERNEL);
3478                         break;
3479                 case NFT_MSG_NEWSETELEM:
3480                         nf_tables_setelem_notify(&trans->ctx,
3481                                                  nft_trans_elem_set(trans),
3482                                                  &nft_trans_elem(trans),
3483                                                  NFT_MSG_NEWSETELEM, 0);
3484                         nft_trans_destroy(trans);
3485                         break;
3486                 case NFT_MSG_DELSETELEM:
3487                         nf_tables_setelem_notify(&trans->ctx,
3488                                                  nft_trans_elem_set(trans),
3489                                                  &nft_trans_elem(trans),
3490                                                  NFT_MSG_DELSETELEM, 0);
3491                         set = nft_trans_elem_set(trans);
3492                         set->ops->get(set, &nft_trans_elem(trans));
3493                         set->ops->remove(set, &nft_trans_elem(trans));
3494                         nft_trans_destroy(trans);
3495                         break;
3496                 }
3497         }
3498
3499         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3500                 list_del(&trans->list);
3501                 trans->ctx.nla = NULL;
3502                 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
3503         }
3504
3505         return 0;
3506 }
3507
3508 /* Schedule objects for release via rcu to make sure no packets are accesing
3509  * aborted rules.
3510  */
3511 static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3512 {
3513         struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3514
3515         switch (trans->msg_type) {
3516         case NFT_MSG_NEWTABLE:
3517                 nf_tables_table_destroy(&trans->ctx);
3518                 break;
3519         case NFT_MSG_NEWCHAIN:
3520                 nf_tables_chain_destroy(trans->ctx.chain);
3521                 break;
3522         case NFT_MSG_NEWRULE:
3523                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3524                 break;
3525         case NFT_MSG_NEWSET:
3526                 nft_set_destroy(nft_trans_set(trans));
3527                 break;
3528         }
3529         kfree(trans);
3530 }
3531
3532 static int nf_tables_abort(struct sk_buff *skb)
3533 {
3534         struct net *net = sock_net(skb->sk);
3535         struct nft_trans *trans, *next;
3536         struct nft_set *set;
3537
3538         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3539                 switch (trans->msg_type) {
3540                 case NFT_MSG_NEWTABLE:
3541                         if (nft_trans_table_update(trans)) {
3542                                 if (nft_trans_table_enable(trans)) {
3543                                         nf_tables_table_disable(trans->ctx.afi,
3544                                                                 trans->ctx.table);
3545                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3546                                 }
3547                                 nft_trans_destroy(trans);
3548                         } else {
3549                                 list_del_rcu(&trans->ctx.table->list);
3550                         }
3551                         break;
3552                 case NFT_MSG_DELTABLE:
3553                         list_add_tail_rcu(&trans->ctx.table->list,
3554                                           &trans->ctx.afi->tables);
3555                         nft_trans_destroy(trans);
3556                         break;
3557                 case NFT_MSG_NEWCHAIN:
3558                         if (nft_trans_chain_update(trans)) {
3559                                 if (nft_trans_chain_stats(trans))
3560                                         free_percpu(nft_trans_chain_stats(trans));
3561
3562                                 nft_trans_destroy(trans);
3563                         } else {
3564                                 trans->ctx.table->use--;
3565                                 list_del_rcu(&trans->ctx.chain->list);
3566                                 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3567                                     trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3568                                         nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3569                                                             trans->ctx.afi->nops);
3570                                 }
3571                         }
3572                         break;
3573                 case NFT_MSG_DELCHAIN:
3574                         trans->ctx.table->use++;
3575                         list_add_tail_rcu(&trans->ctx.chain->list,
3576                                           &trans->ctx.table->chains);
3577                         nft_trans_destroy(trans);
3578                         break;
3579                 case NFT_MSG_NEWRULE:
3580                         trans->ctx.chain->use--;
3581                         list_del_rcu(&nft_trans_rule(trans)->list);
3582                         break;
3583                 case NFT_MSG_DELRULE:
3584                         trans->ctx.chain->use++;
3585                         nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3586                         nft_trans_destroy(trans);
3587                         break;
3588                 case NFT_MSG_NEWSET:
3589                         trans->ctx.table->use--;
3590                         list_del_rcu(&nft_trans_set(trans)->list);
3591                         break;
3592                 case NFT_MSG_DELSET:
3593                         trans->ctx.table->use++;
3594                         list_add_tail_rcu(&nft_trans_set(trans)->list,
3595                                           &trans->ctx.table->sets);
3596                         nft_trans_destroy(trans);
3597                         break;
3598                 case NFT_MSG_NEWSETELEM:
3599                         nft_trans_elem_set(trans)->nelems--;
3600                         set = nft_trans_elem_set(trans);
3601                         set->ops->get(set, &nft_trans_elem(trans));
3602                         set->ops->remove(set, &nft_trans_elem(trans));
3603                         nft_trans_destroy(trans);
3604                         break;
3605                 case NFT_MSG_DELSETELEM:
3606                         nft_trans_elem_set(trans)->nelems++;
3607                         nft_trans_destroy(trans);
3608                         break;
3609                 }
3610         }
3611
3612         list_for_each_entry_safe_reverse(trans, next,
3613                                          &net->nft.commit_list, list) {
3614                 list_del(&trans->list);
3615                 trans->ctx.nla = NULL;
3616                 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
3617         }
3618
3619         return 0;
3620 }
3621
3622 static const struct nfnetlink_subsystem nf_tables_subsys = {
3623         .name           = "nf_tables",
3624         .subsys_id      = NFNL_SUBSYS_NFTABLES,
3625         .cb_count       = NFT_MSG_MAX,
3626         .cb             = nf_tables_cb,
3627         .commit         = nf_tables_commit,
3628         .abort          = nf_tables_abort,
3629 };
3630
3631 /*
3632  * Loop detection - walk through the ruleset beginning at the destination chain
3633  * of a new jump until either the source chain is reached (loop) or all
3634  * reachable chains have been traversed.
3635  *
3636  * The loop check is performed whenever a new jump verdict is added to an
3637  * expression or verdict map or a verdict map is bound to a new chain.
3638  */
3639
3640 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3641                                  const struct nft_chain *chain);
3642
3643 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3644                                         const struct nft_set *set,
3645                                         const struct nft_set_iter *iter,
3646                                         const struct nft_set_elem *elem)
3647 {
3648         if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3649                 return 0;
3650
3651         switch (elem->data.verdict) {
3652         case NFT_JUMP:
3653         case NFT_GOTO:
3654                 return nf_tables_check_loops(ctx, elem->data.chain);
3655         default:
3656                 return 0;
3657         }
3658 }
3659
3660 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3661                                  const struct nft_chain *chain)
3662 {
3663         const struct nft_rule *rule;
3664         const struct nft_expr *expr, *last;
3665         const struct nft_set *set;
3666         struct nft_set_binding *binding;
3667         struct nft_set_iter iter;
3668
3669         if (ctx->chain == chain)
3670                 return -ELOOP;
3671
3672         list_for_each_entry(rule, &chain->rules, list) {
3673                 nft_rule_for_each_expr(expr, last, rule) {
3674                         const struct nft_data *data = NULL;
3675                         int err;
3676
3677                         if (!expr->ops->validate)
3678                                 continue;
3679
3680                         err = expr->ops->validate(ctx, expr, &data);
3681                         if (err < 0)
3682                                 return err;
3683
3684                         if (data == NULL)
3685                                 continue;
3686
3687                         switch (data->verdict) {
3688                         case NFT_JUMP:
3689                         case NFT_GOTO:
3690                                 err = nf_tables_check_loops(ctx, data->chain);
3691                                 if (err < 0)
3692                                         return err;
3693                         default:
3694                                 break;
3695                         }
3696                 }
3697         }
3698
3699         list_for_each_entry(set, &ctx->table->sets, list) {
3700                 if (!(set->flags & NFT_SET_MAP) ||
3701                     set->dtype != NFT_DATA_VERDICT)
3702                         continue;
3703
3704                 list_for_each_entry(binding, &set->bindings, list) {
3705                         if (binding->chain != chain)
3706                                 continue;
3707
3708                         iter.skip       = 0;
3709                         iter.count      = 0;
3710                         iter.err        = 0;
3711                         iter.fn         = nf_tables_loop_check_setelem;
3712
3713                         set->ops->walk(ctx, set, &iter);
3714                         if (iter.err < 0)
3715                                 return iter.err;
3716                 }
3717         }
3718
3719         return 0;
3720 }
3721
3722 /**
3723  *      nft_parse_u32_check - fetch u32 attribute and check for maximum value
3724  *
3725  *      @attr: netlink attribute to fetch value from
3726  *      @max: maximum value to be stored in dest
3727  *      @dest: pointer to the variable
3728  *
3729  *      Parse, check and store a given u32 netlink attribute into variable.
3730  *      This function returns -ERANGE if the value goes over maximum value.
3731  *      Otherwise a 0 is returned and the attribute value is stored in the
3732  *      destination variable.
3733  */
3734 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
3735 {
3736         u32 val;
3737
3738         val = ntohl(nla_get_be32(attr));
3739         if (val > max)
3740                 return -ERANGE;
3741
3742         *dest = val;
3743         return 0;
3744 }
3745 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
3746
3747 /**
3748  *      nft_validate_input_register - validate an expressions' input register
3749  *
3750  *      @reg: the register number
3751  *
3752  *      Validate that the input register is one of the general purpose
3753  *      registers.
3754  */
3755 int nft_validate_input_register(enum nft_registers reg)
3756 {
3757         if (reg <= NFT_REG_VERDICT)
3758                 return -EINVAL;
3759         if (reg > NFT_REG_MAX)
3760                 return -ERANGE;
3761         return 0;
3762 }
3763 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3764
3765 /**
3766  *      nft_validate_output_register - validate an expressions' output register
3767  *
3768  *      @reg: the register number
3769  *
3770  *      Validate that the output register is one of the general purpose
3771  *      registers or the verdict register.
3772  */
3773 int nft_validate_output_register(enum nft_registers reg)
3774 {
3775         if (reg < NFT_REG_VERDICT)
3776                 return -EINVAL;
3777         if (reg > NFT_REG_MAX)
3778                 return -ERANGE;
3779         return 0;
3780 }
3781 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3782
3783 /**
3784  *      nft_validate_data_load - validate an expressions' data load
3785  *
3786  *      @ctx: context of the expression performing the load
3787  *      @reg: the destination register number
3788  *      @data: the data to load
3789  *      @type: the data type
3790  *
3791  *      Validate that a data load uses the appropriate data type for
3792  *      the destination register. A value of NULL for the data means
3793  *      that its runtime gathered data, which is always of type
3794  *      NFT_DATA_VALUE.
3795  */
3796 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3797                            const struct nft_data *data,
3798                            enum nft_data_types type)
3799 {
3800         int err;
3801
3802         switch (reg) {
3803         case NFT_REG_VERDICT:
3804                 if (data == NULL || type != NFT_DATA_VERDICT)
3805                         return -EINVAL;
3806
3807                 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3808                         err = nf_tables_check_loops(ctx, data->chain);
3809                         if (err < 0)
3810                                 return err;
3811
3812                         if (ctx->chain->level + 1 > data->chain->level) {
3813                                 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3814                                         return -EMLINK;
3815                                 data->chain->level = ctx->chain->level + 1;
3816                         }
3817                 }
3818
3819                 return 0;
3820         default:
3821                 if (data != NULL && type != NFT_DATA_VALUE)
3822                         return -EINVAL;
3823                 return 0;
3824         }
3825 }
3826 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3827
3828 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3829         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
3830         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
3831                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
3832 };
3833
3834 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3835                             struct nft_data_desc *desc, const struct nlattr *nla)
3836 {
3837         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3838         struct nft_chain *chain;
3839         int err;
3840
3841         err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3842         if (err < 0)
3843                 return err;
3844
3845         if (!tb[NFTA_VERDICT_CODE])
3846                 return -EINVAL;
3847         data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3848
3849         switch (data->verdict) {
3850         default:
3851                 switch (data->verdict & NF_VERDICT_MASK) {
3852                 case NF_ACCEPT:
3853                 case NF_DROP:
3854                 case NF_QUEUE:
3855                         break;
3856                 default:
3857                         return -EINVAL;
3858                 }
3859                 /* fall through */
3860         case NFT_CONTINUE:
3861         case NFT_BREAK:
3862         case NFT_RETURN:
3863                 desc->len = sizeof(data->verdict);
3864                 break;
3865         case NFT_JUMP:
3866         case NFT_GOTO:
3867                 if (!tb[NFTA_VERDICT_CHAIN])
3868                         return -EINVAL;
3869                 chain = nf_tables_chain_lookup(ctx->table,
3870                                                tb[NFTA_VERDICT_CHAIN]);
3871                 if (IS_ERR(chain))
3872                         return PTR_ERR(chain);
3873                 if (chain->flags & NFT_BASE_CHAIN)
3874                         return -EOPNOTSUPP;
3875
3876                 chain->use++;
3877                 data->chain = chain;
3878                 desc->len = sizeof(data);
3879                 break;
3880         }
3881
3882         desc->type = NFT_DATA_VERDICT;
3883         return 0;
3884 }
3885
3886 static void nft_verdict_uninit(const struct nft_data *data)
3887 {
3888         switch (data->verdict) {
3889         case NFT_JUMP:
3890         case NFT_GOTO:
3891                 data->chain->use--;
3892                 break;
3893         }
3894 }
3895
3896 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3897 {
3898         struct nlattr *nest;
3899
3900         nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3901         if (!nest)
3902                 goto nla_put_failure;
3903
3904         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3905                 goto nla_put_failure;
3906
3907         switch (data->verdict) {
3908         case NFT_JUMP:
3909         case NFT_GOTO:
3910                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3911                         goto nla_put_failure;
3912         }
3913         nla_nest_end(skb, nest);
3914         return 0;
3915
3916 nla_put_failure:
3917         return -1;
3918 }
3919
3920 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3921                           struct nft_data_desc *desc, const struct nlattr *nla)
3922 {
3923         unsigned int len;
3924
3925         len = nla_len(nla);
3926         if (len == 0)
3927                 return -EINVAL;
3928         if (len > sizeof(data->data))
3929                 return -EOVERFLOW;
3930
3931         nla_memcpy(data->data, nla, sizeof(data->data));
3932         desc->type = NFT_DATA_VALUE;
3933         desc->len  = len;
3934         return 0;
3935 }
3936
3937 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3938                           unsigned int len)
3939 {
3940         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3941 }
3942
3943 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3944         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY,
3945                                     .len  = FIELD_SIZEOF(struct nft_data, data) },
3946         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
3947 };
3948
3949 /**
3950  *      nft_data_init - parse nf_tables data netlink attributes
3951  *
3952  *      @ctx: context of the expression using the data
3953  *      @data: destination struct nft_data
3954  *      @desc: data description
3955  *      @nla: netlink attribute containing data
3956  *
3957  *      Parse the netlink data attributes and initialize a struct nft_data.
3958  *      The type and length of data are returned in the data description.
3959  *
3960  *      The caller can indicate that it only wants to accept data of type
3961  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
3962  */
3963 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3964                   struct nft_data_desc *desc, const struct nlattr *nla)
3965 {
3966         struct nlattr *tb[NFTA_DATA_MAX + 1];
3967         int err;
3968
3969         err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3970         if (err < 0)
3971                 return err;
3972
3973         if (tb[NFTA_DATA_VALUE])
3974                 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3975         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3976                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3977         return -EINVAL;
3978 }
3979 EXPORT_SYMBOL_GPL(nft_data_init);
3980
3981 /**
3982  *      nft_data_uninit - release a nft_data item
3983  *
3984  *      @data: struct nft_data to release
3985  *      @type: type of data
3986  *
3987  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3988  *      all others need to be released by calling this function.
3989  */
3990 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3991 {
3992         if (type < NFT_DATA_VERDICT)
3993                 return;
3994         switch (type) {
3995         case NFT_DATA_VERDICT:
3996                 return nft_verdict_uninit(data);
3997         default:
3998                 WARN_ON(1);
3999         }
4000 }
4001 EXPORT_SYMBOL_GPL(nft_data_uninit);
4002
4003 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4004                   enum nft_data_types type, unsigned int len)
4005 {
4006         struct nlattr *nest;
4007         int err;
4008
4009         nest = nla_nest_start(skb, attr);
4010         if (nest == NULL)
4011                 return -1;
4012
4013         switch (type) {
4014         case NFT_DATA_VALUE:
4015                 err = nft_value_dump(skb, data, len);
4016                 break;
4017         case NFT_DATA_VERDICT:
4018                 err = nft_verdict_dump(skb, data);
4019                 break;
4020         default:
4021                 err = -EINVAL;
4022                 WARN_ON(1);
4023         }
4024
4025         nla_nest_end(skb, nest);
4026         return err;
4027 }
4028 EXPORT_SYMBOL_GPL(nft_data_dump);
4029
4030 static int nf_tables_init_net(struct net *net)
4031 {
4032         INIT_LIST_HEAD(&net->nft.af_info);
4033         INIT_LIST_HEAD(&net->nft.commit_list);
4034         net->nft.base_seq = 1;
4035         return 0;
4036 }
4037
4038 static struct pernet_operations nf_tables_net_ops = {
4039         .init   = nf_tables_init_net,
4040 };
4041
4042 static int __init nf_tables_module_init(void)
4043 {
4044         int err;
4045
4046         err = register_pernet_subsys(&nf_tables_net_ops);
4047         if (err < 0)
4048                 return err;
4049
4050         info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4051                        GFP_KERNEL);
4052         if (info == NULL) {
4053                 err = -ENOMEM;
4054                 goto err1;
4055         }
4056
4057         err = nf_tables_core_module_init();
4058         if (err < 0)
4059                 goto err2;
4060
4061         /* must be last */
4062         err = nfnetlink_subsys_register(&nf_tables_subsys);
4063         if (err < 0)
4064                 goto err3;
4065
4066         pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4067         return err;
4068 err3:
4069         nf_tables_core_module_exit();
4070 err2:
4071         kfree(info);
4072 err1:
4073         unregister_pernet_subsys(&nf_tables_net_ops);
4074         return err;
4075 }
4076
4077 static void __exit nf_tables_module_exit(void)
4078 {
4079         unregister_pernet_subsys(&nf_tables_net_ops);
4080         nfnetlink_subsys_unregister(&nf_tables_subsys);
4081         nf_tables_core_module_exit();
4082         kfree(info);
4083 }
4084
4085 module_init(nf_tables_module_init);
4086 module_exit(nf_tables_module_exit);
4087
4088 MODULE_LICENSE("GPL");
4089 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4090 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);