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