047884776586db680a9b9547ceb94e9a2035bf9c
[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), (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                                             .len = IFNAMSIZ - 1 },
2014         [NFTA_SET_FLAGS]                = { .type = NLA_U32 },
2015         [NFTA_SET_KEY_TYPE]             = { .type = NLA_U32 },
2016         [NFTA_SET_KEY_LEN]              = { .type = NLA_U32 },
2017         [NFTA_SET_DATA_TYPE]            = { .type = NLA_U32 },
2018         [NFTA_SET_DATA_LEN]             = { .type = NLA_U32 },
2019         [NFTA_SET_POLICY]               = { .type = NLA_U32 },
2020         [NFTA_SET_DESC]                 = { .type = NLA_NESTED },
2021         [NFTA_SET_ID]                   = { .type = NLA_U32 },
2022 };
2023
2024 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2025         [NFTA_SET_DESC_SIZE]            = { .type = NLA_U32 },
2026 };
2027
2028 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2029                                      const struct sk_buff *skb,
2030                                      const struct nlmsghdr *nlh,
2031                                      const struct nlattr * const nla[])
2032 {
2033         struct net *net = sock_net(skb->sk);
2034         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2035         struct nft_af_info *afi = NULL;
2036         struct nft_table *table = NULL;
2037
2038         if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2039                 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2040                 if (IS_ERR(afi))
2041                         return PTR_ERR(afi);
2042         }
2043
2044         if (nla[NFTA_SET_TABLE] != NULL) {
2045                 if (afi == NULL)
2046                         return -EAFNOSUPPORT;
2047
2048                 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2049                 if (IS_ERR(table))
2050                         return PTR_ERR(table);
2051                 if (table->flags & NFT_TABLE_INACTIVE)
2052                         return -ENOENT;
2053         }
2054
2055         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2056         return 0;
2057 }
2058
2059 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2060                                      const struct nlattr *nla)
2061 {
2062         struct nft_set *set;
2063
2064         if (nla == NULL)
2065                 return ERR_PTR(-EINVAL);
2066
2067         list_for_each_entry(set, &table->sets, list) {
2068                 if (!nla_strcmp(nla, set->name))
2069                         return set;
2070         }
2071         return ERR_PTR(-ENOENT);
2072 }
2073
2074 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2075                                           const struct nlattr *nla)
2076 {
2077         struct nft_trans *trans;
2078         u32 id = ntohl(nla_get_be32(nla));
2079
2080         list_for_each_entry(trans, &net->nft.commit_list, list) {
2081                 if (trans->msg_type == NFT_MSG_NEWSET &&
2082                     id == nft_trans_set_id(trans))
2083                         return nft_trans_set(trans);
2084         }
2085         return ERR_PTR(-ENOENT);
2086 }
2087
2088 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2089                                     const char *name)
2090 {
2091         const struct nft_set *i;
2092         const char *p;
2093         unsigned long *inuse;
2094         unsigned int n = 0, min = 0;
2095
2096         p = strnchr(name, IFNAMSIZ, '%');
2097         if (p != NULL) {
2098                 if (p[1] != 'd' || strchr(p + 2, '%'))
2099                         return -EINVAL;
2100
2101                 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2102                 if (inuse == NULL)
2103                         return -ENOMEM;
2104 cont:
2105                 list_for_each_entry(i, &ctx->table->sets, list) {
2106                         int tmp;
2107
2108                         if (!sscanf(i->name, name, &tmp))
2109                                 continue;
2110                         if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2111                                 continue;
2112
2113                         set_bit(tmp - min, inuse);
2114                 }
2115
2116                 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2117                 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2118                         min += BITS_PER_BYTE * PAGE_SIZE;
2119                         memset(inuse, 0, PAGE_SIZE);
2120                         goto cont;
2121                 }
2122                 free_page((unsigned long)inuse);
2123         }
2124
2125         snprintf(set->name, sizeof(set->name), name, min + n);
2126         list_for_each_entry(i, &ctx->table->sets, list) {
2127                 if (!strcmp(set->name, i->name))
2128                         return -ENFILE;
2129         }
2130         return 0;
2131 }
2132
2133 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2134                               const struct nft_set *set, u16 event, u16 flags)
2135 {
2136         struct nfgenmsg *nfmsg;
2137         struct nlmsghdr *nlh;
2138         struct nlattr *desc;
2139         u32 portid = ctx->portid;
2140         u32 seq = ctx->seq;
2141
2142         event |= NFNL_SUBSYS_NFTABLES << 8;
2143         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2144                         flags);
2145         if (nlh == NULL)
2146                 goto nla_put_failure;
2147
2148         nfmsg = nlmsg_data(nlh);
2149         nfmsg->nfgen_family     = ctx->afi->family;
2150         nfmsg->version          = NFNETLINK_V0;
2151         nfmsg->res_id           = 0;
2152
2153         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2154                 goto nla_put_failure;
2155         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2156                 goto nla_put_failure;
2157         if (set->flags != 0)
2158                 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2159                         goto nla_put_failure;
2160
2161         if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2162                 goto nla_put_failure;
2163         if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2164                 goto nla_put_failure;
2165         if (set->flags & NFT_SET_MAP) {
2166                 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2167                         goto nla_put_failure;
2168                 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2169                         goto nla_put_failure;
2170         }
2171
2172         desc = nla_nest_start(skb, NFTA_SET_DESC);
2173         if (desc == NULL)
2174                 goto nla_put_failure;
2175         if (set->size &&
2176             nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2177                 goto nla_put_failure;
2178         nla_nest_end(skb, desc);
2179
2180         return nlmsg_end(skb, nlh);
2181
2182 nla_put_failure:
2183         nlmsg_trim(skb, nlh);
2184         return -1;
2185 }
2186
2187 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2188                                 const struct nft_set *set,
2189                                 int event)
2190 {
2191         struct sk_buff *skb;
2192         u32 portid = ctx->portid;
2193         int err;
2194
2195         if (!ctx->report &&
2196             !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2197                 return 0;
2198
2199         err = -ENOBUFS;
2200         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2201         if (skb == NULL)
2202                 goto err;
2203
2204         err = nf_tables_fill_set(skb, ctx, set, event, 0);
2205         if (err < 0) {
2206                 kfree_skb(skb);
2207                 goto err;
2208         }
2209
2210         err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2211                              ctx->report, GFP_KERNEL);
2212 err:
2213         if (err < 0)
2214                 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2215         return err;
2216 }
2217
2218 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2219                                      struct netlink_callback *cb)
2220 {
2221         const struct nft_set *set;
2222         unsigned int idx = 0, s_idx = cb->args[0];
2223
2224         if (cb->args[1])
2225                 return skb->len;
2226
2227         list_for_each_entry(set, &ctx->table->sets, list) {
2228                 if (idx < s_idx)
2229                         goto cont;
2230                 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2231                                        NLM_F_MULTI) < 0) {
2232                         cb->args[0] = idx;
2233                         goto done;
2234                 }
2235 cont:
2236                 idx++;
2237         }
2238         cb->args[1] = 1;
2239 done:
2240         return skb->len;
2241 }
2242
2243 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2244                                       struct netlink_callback *cb)
2245 {
2246         const struct nft_set *set;
2247         unsigned int idx, s_idx = cb->args[0];
2248         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2249
2250         if (cb->args[1])
2251                 return skb->len;
2252
2253         list_for_each_entry(table, &ctx->afi->tables, list) {
2254                 if (cur_table) {
2255                         if (cur_table != table)
2256                                 continue;
2257
2258                         cur_table = NULL;
2259                 }
2260                 ctx->table = table;
2261                 idx = 0;
2262                 list_for_each_entry(set, &ctx->table->sets, list) {
2263                         if (idx < s_idx)
2264                                 goto cont;
2265                         if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2266                                                NLM_F_MULTI) < 0) {
2267                                 cb->args[0] = idx;
2268                                 cb->args[2] = (unsigned long) table;
2269                                 goto done;
2270                         }
2271 cont:
2272                         idx++;
2273                 }
2274         }
2275         cb->args[1] = 1;
2276 done:
2277         return skb->len;
2278 }
2279
2280 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2281                                    struct netlink_callback *cb)
2282 {
2283         const struct nft_set *set;
2284         unsigned int idx, s_idx = cb->args[0];
2285         struct nft_af_info *afi;
2286         struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2287         struct net *net = sock_net(skb->sk);
2288         int cur_family = cb->args[3];
2289
2290         if (cb->args[1])
2291                 return skb->len;
2292
2293         list_for_each_entry(afi, &net->nft.af_info, list) {
2294                 if (cur_family) {
2295                         if (afi->family != cur_family)
2296                                 continue;
2297
2298                         cur_family = 0;
2299                 }
2300
2301                 list_for_each_entry(table, &afi->tables, list) {
2302                         if (cur_table) {
2303                                 if (cur_table != table)
2304                                         continue;
2305
2306                                 cur_table = NULL;
2307                         }
2308
2309                         ctx->table = table;
2310                         ctx->afi = afi;
2311                         idx = 0;
2312                         list_for_each_entry(set, &ctx->table->sets, list) {
2313                                 if (idx < s_idx)
2314                                         goto cont;
2315                                 if (nf_tables_fill_set(skb, ctx, set,
2316                                                        NFT_MSG_NEWSET,
2317                                                        NLM_F_MULTI) < 0) {
2318                                         cb->args[0] = idx;
2319                                         cb->args[2] = (unsigned long) table;
2320                                         cb->args[3] = afi->family;
2321                                         goto done;
2322                                 }
2323 cont:
2324                                 idx++;
2325                         }
2326                         if (s_idx)
2327                                 s_idx = 0;
2328                 }
2329         }
2330         cb->args[1] = 1;
2331 done:
2332         return skb->len;
2333 }
2334
2335 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2336 {
2337         const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2338         struct nlattr *nla[NFTA_SET_MAX + 1];
2339         struct nft_ctx ctx;
2340         int err, ret;
2341
2342         err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2343                           nft_set_policy);
2344         if (err < 0)
2345                 return err;
2346
2347         err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2348         if (err < 0)
2349                 return err;
2350
2351         if (ctx.table == NULL) {
2352                 if (ctx.afi == NULL)
2353                         ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2354                 else
2355                         ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2356         } else
2357                 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2358
2359         return ret;
2360 }
2361
2362 #define NFT_SET_INACTIVE        (1 << 15)       /* Internal set flag */
2363
2364 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2365                             const struct nlmsghdr *nlh,
2366                             const struct nlattr * const nla[])
2367 {
2368         const struct nft_set *set;
2369         struct nft_ctx ctx;
2370         struct sk_buff *skb2;
2371         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2372         int err;
2373
2374         /* Verify existance before starting dump */
2375         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2376         if (err < 0)
2377                 return err;
2378
2379         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2380                 struct netlink_dump_control c = {
2381                         .dump = nf_tables_dump_sets,
2382                 };
2383                 return netlink_dump_start(nlsk, skb, nlh, &c);
2384         }
2385
2386         /* Only accept unspec with dump */
2387         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2388                 return -EAFNOSUPPORT;
2389
2390         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2391         if (IS_ERR(set))
2392                 return PTR_ERR(set);
2393         if (set->flags & NFT_SET_INACTIVE)
2394                 return -ENOENT;
2395
2396         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2397         if (skb2 == NULL)
2398                 return -ENOMEM;
2399
2400         err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2401         if (err < 0)
2402                 goto err;
2403
2404         return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2405
2406 err:
2407         kfree_skb(skb2);
2408         return err;
2409 }
2410
2411 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2412                                     struct nft_set_desc *desc,
2413                                     const struct nlattr *nla)
2414 {
2415         struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2416         int err;
2417
2418         err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2419         if (err < 0)
2420                 return err;
2421
2422         if (da[NFTA_SET_DESC_SIZE] != NULL)
2423                 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2424
2425         return 0;
2426 }
2427
2428 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2429                              struct nft_set *set)
2430 {
2431         struct nft_trans *trans;
2432
2433         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2434         if (trans == NULL)
2435                 return -ENOMEM;
2436
2437         if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2438                 nft_trans_set_id(trans) =
2439                         ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2440                 set->flags |= NFT_SET_INACTIVE;
2441         }
2442         nft_trans_set(trans) = set;
2443         list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2444
2445         return 0;
2446 }
2447
2448 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2449                             const struct nlmsghdr *nlh,
2450                             const struct nlattr * const nla[])
2451 {
2452         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2453         const struct nft_set_ops *ops;
2454         struct nft_af_info *afi;
2455         struct net *net = sock_net(skb->sk);
2456         struct nft_table *table;
2457         struct nft_set *set;
2458         struct nft_ctx ctx;
2459         char name[IFNAMSIZ];
2460         unsigned int size;
2461         bool create;
2462         u32 ktype, dtype, flags, policy;
2463         struct nft_set_desc desc;
2464         int err;
2465
2466         if (nla[NFTA_SET_TABLE] == NULL ||
2467             nla[NFTA_SET_NAME] == NULL ||
2468             nla[NFTA_SET_KEY_LEN] == NULL ||
2469             nla[NFTA_SET_ID] == NULL)
2470                 return -EINVAL;
2471
2472         memset(&desc, 0, sizeof(desc));
2473
2474         ktype = NFT_DATA_VALUE;
2475         if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2476                 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2477                 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2478                         return -EINVAL;
2479         }
2480
2481         desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2482         if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2483                 return -EINVAL;
2484
2485         flags = 0;
2486         if (nla[NFTA_SET_FLAGS] != NULL) {
2487                 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2488                 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2489                               NFT_SET_INTERVAL | NFT_SET_MAP))
2490                         return -EINVAL;
2491         }
2492
2493         dtype = 0;
2494         if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2495                 if (!(flags & NFT_SET_MAP))
2496                         return -EINVAL;
2497
2498                 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2499                 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2500                     dtype != NFT_DATA_VERDICT)
2501                         return -EINVAL;
2502
2503                 if (dtype != NFT_DATA_VERDICT) {
2504                         if (nla[NFTA_SET_DATA_LEN] == NULL)
2505                                 return -EINVAL;
2506                         desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2507                         if (desc.dlen == 0 ||
2508                             desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2509                                 return -EINVAL;
2510                 } else
2511                         desc.dlen = sizeof(struct nft_data);
2512         } else if (flags & NFT_SET_MAP)
2513                 return -EINVAL;
2514
2515         policy = NFT_SET_POL_PERFORMANCE;
2516         if (nla[NFTA_SET_POLICY] != NULL)
2517                 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2518
2519         if (nla[NFTA_SET_DESC] != NULL) {
2520                 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2521                 if (err < 0)
2522                         return err;
2523         }
2524
2525         create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2526
2527         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2528         if (IS_ERR(afi))
2529                 return PTR_ERR(afi);
2530
2531         table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2532         if (IS_ERR(table))
2533                 return PTR_ERR(table);
2534
2535         nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2536
2537         set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2538         if (IS_ERR(set)) {
2539                 if (PTR_ERR(set) != -ENOENT)
2540                         return PTR_ERR(set);
2541                 set = NULL;
2542         }
2543
2544         if (set != NULL) {
2545                 if (nlh->nlmsg_flags & NLM_F_EXCL)
2546                         return -EEXIST;
2547                 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2548                         return -EOPNOTSUPP;
2549                 return 0;
2550         }
2551
2552         if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2553                 return -ENOENT;
2554
2555         ops = nft_select_set_ops(nla, &desc, policy);
2556         if (IS_ERR(ops))
2557                 return PTR_ERR(ops);
2558
2559         size = 0;
2560         if (ops->privsize != NULL)
2561                 size = ops->privsize(nla);
2562
2563         err = -ENOMEM;
2564         set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2565         if (set == NULL)
2566                 goto err1;
2567
2568         nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2569         err = nf_tables_set_alloc_name(&ctx, set, name);
2570         if (err < 0)
2571                 goto err2;
2572
2573         INIT_LIST_HEAD(&set->bindings);
2574         set->ops   = ops;
2575         set->ktype = ktype;
2576         set->klen  = desc.klen;
2577         set->dtype = dtype;
2578         set->dlen  = desc.dlen;
2579         set->flags = flags;
2580         set->size  = desc.size;
2581
2582         err = ops->init(set, &desc, nla);
2583         if (err < 0)
2584                 goto err2;
2585
2586         err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2587         if (err < 0)
2588                 goto err2;
2589
2590         list_add_tail(&set->list, &table->sets);
2591         return 0;
2592
2593 err2:
2594         kfree(set);
2595 err1:
2596         module_put(ops->owner);
2597         return err;
2598 }
2599
2600 static void nft_set_destroy(struct nft_set *set)
2601 {
2602         set->ops->destroy(set);
2603         module_put(set->ops->owner);
2604         kfree(set);
2605 }
2606
2607 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2608 {
2609         list_del(&set->list);
2610         nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2611         nft_set_destroy(set);
2612 }
2613
2614 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2615                             const struct nlmsghdr *nlh,
2616                             const struct nlattr * const nla[])
2617 {
2618         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2619         struct nft_set *set;
2620         struct nft_ctx ctx;
2621         int err;
2622
2623         if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2624                 return -EAFNOSUPPORT;
2625         if (nla[NFTA_SET_TABLE] == NULL)
2626                 return -EINVAL;
2627
2628         err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2629         if (err < 0)
2630                 return err;
2631
2632         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2633         if (IS_ERR(set))
2634                 return PTR_ERR(set);
2635         if (set->flags & NFT_SET_INACTIVE)
2636                 return -ENOENT;
2637         if (!list_empty(&set->bindings))
2638                 return -EBUSY;
2639
2640         err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2641         if (err < 0)
2642                 return err;
2643
2644         list_del(&set->list);
2645         return 0;
2646 }
2647
2648 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2649                                         const struct nft_set *set,
2650                                         const struct nft_set_iter *iter,
2651                                         const struct nft_set_elem *elem)
2652 {
2653         enum nft_registers dreg;
2654
2655         dreg = nft_type_to_reg(set->dtype);
2656         return nft_validate_data_load(ctx, dreg, &elem->data,
2657                                       set->dtype == NFT_DATA_VERDICT ?
2658                                       NFT_DATA_VERDICT : NFT_DATA_VALUE);
2659 }
2660
2661 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2662                        struct nft_set_binding *binding)
2663 {
2664         struct nft_set_binding *i;
2665         struct nft_set_iter iter;
2666
2667         if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2668                 return -EBUSY;
2669
2670         if (set->flags & NFT_SET_MAP) {
2671                 /* If the set is already bound to the same chain all
2672                  * jumps are already validated for that chain.
2673                  */
2674                 list_for_each_entry(i, &set->bindings, list) {
2675                         if (i->chain == binding->chain)
2676                                 goto bind;
2677                 }
2678
2679                 iter.skip       = 0;
2680                 iter.count      = 0;
2681                 iter.err        = 0;
2682                 iter.fn         = nf_tables_bind_check_setelem;
2683
2684                 set->ops->walk(ctx, set, &iter);
2685                 if (iter.err < 0) {
2686                         /* Destroy anonymous sets if binding fails */
2687                         if (set->flags & NFT_SET_ANONYMOUS)
2688                                 nf_tables_set_destroy(ctx, set);
2689
2690                         return iter.err;
2691                 }
2692         }
2693 bind:
2694         binding->chain = ctx->chain;
2695         list_add_tail(&binding->list, &set->bindings);
2696         return 0;
2697 }
2698
2699 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2700                           struct nft_set_binding *binding)
2701 {
2702         list_del(&binding->list);
2703
2704         if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2705             !(set->flags & NFT_SET_INACTIVE))
2706                 nf_tables_set_destroy(ctx, set);
2707 }
2708
2709 /*
2710  * Set elements
2711  */
2712
2713 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2714         [NFTA_SET_ELEM_KEY]             = { .type = NLA_NESTED },
2715         [NFTA_SET_ELEM_DATA]            = { .type = NLA_NESTED },
2716         [NFTA_SET_ELEM_FLAGS]           = { .type = NLA_U32 },
2717 };
2718
2719 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2720         [NFTA_SET_ELEM_LIST_TABLE]      = { .type = NLA_STRING },
2721         [NFTA_SET_ELEM_LIST_SET]        = { .type = NLA_STRING },
2722         [NFTA_SET_ELEM_LIST_ELEMENTS]   = { .type = NLA_NESTED },
2723         [NFTA_SET_ELEM_LIST_SET_ID]     = { .type = NLA_U32 },
2724 };
2725
2726 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2727                                       const struct sk_buff *skb,
2728                                       const struct nlmsghdr *nlh,
2729                                       const struct nlattr * const nla[],
2730                                       bool trans)
2731 {
2732         const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2733         struct nft_af_info *afi;
2734         struct nft_table *table;
2735         struct net *net = sock_net(skb->sk);
2736
2737         afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2738         if (IS_ERR(afi))
2739                 return PTR_ERR(afi);
2740
2741         table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2742         if (IS_ERR(table))
2743                 return PTR_ERR(table);
2744         if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2745                 return -ENOENT;
2746
2747         nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2748         return 0;
2749 }
2750
2751 static int nf_tables_fill_setelem(struct sk_buff *skb,
2752                                   const struct nft_set *set,
2753                                   const struct nft_set_elem *elem)
2754 {
2755         unsigned char *b = skb_tail_pointer(skb);
2756         struct nlattr *nest;
2757
2758         nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2759         if (nest == NULL)
2760                 goto nla_put_failure;
2761
2762         if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2763                           set->klen) < 0)
2764                 goto nla_put_failure;
2765
2766         if (set->flags & NFT_SET_MAP &&
2767             !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2768             nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2769                           set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2770                           set->dlen) < 0)
2771                 goto nla_put_failure;
2772
2773         if (elem->flags != 0)
2774                 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2775                         goto nla_put_failure;
2776
2777         nla_nest_end(skb, nest);
2778         return 0;
2779
2780 nla_put_failure:
2781         nlmsg_trim(skb, b);
2782         return -EMSGSIZE;
2783 }
2784
2785 struct nft_set_dump_args {
2786         const struct netlink_callback   *cb;
2787         struct nft_set_iter             iter;
2788         struct sk_buff                  *skb;
2789 };
2790
2791 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2792                                   const struct nft_set *set,
2793                                   const struct nft_set_iter *iter,
2794                                   const struct nft_set_elem *elem)
2795 {
2796         struct nft_set_dump_args *args;
2797
2798         args = container_of(iter, struct nft_set_dump_args, iter);
2799         return nf_tables_fill_setelem(args->skb, set, elem);
2800 }
2801
2802 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2803 {
2804         const struct nft_set *set;
2805         struct nft_set_dump_args args;
2806         struct nft_ctx ctx;
2807         struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2808         struct nfgenmsg *nfmsg;
2809         struct nlmsghdr *nlh;
2810         struct nlattr *nest;
2811         u32 portid, seq;
2812         int event, err;
2813
2814         err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2815                           NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2816         if (err < 0)
2817                 return err;
2818
2819         err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2820                                          false);
2821         if (err < 0)
2822                 return err;
2823
2824         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2825         if (IS_ERR(set))
2826                 return PTR_ERR(set);
2827         if (set->flags & NFT_SET_INACTIVE)
2828                 return -ENOENT;
2829
2830         event  = NFT_MSG_NEWSETELEM;
2831         event |= NFNL_SUBSYS_NFTABLES << 8;
2832         portid = NETLINK_CB(cb->skb).portid;
2833         seq    = cb->nlh->nlmsg_seq;
2834
2835         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2836                         NLM_F_MULTI);
2837         if (nlh == NULL)
2838                 goto nla_put_failure;
2839
2840         nfmsg = nlmsg_data(nlh);
2841         nfmsg->nfgen_family = NFPROTO_UNSPEC;
2842         nfmsg->version      = NFNETLINK_V0;
2843         nfmsg->res_id       = 0;
2844
2845         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2846                 goto nla_put_failure;
2847         if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2848                 goto nla_put_failure;
2849
2850         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2851         if (nest == NULL)
2852                 goto nla_put_failure;
2853
2854         args.cb         = cb;
2855         args.skb        = skb;
2856         args.iter.skip  = cb->args[0];
2857         args.iter.count = 0;
2858         args.iter.err   = 0;
2859         args.iter.fn    = nf_tables_dump_setelem;
2860         set->ops->walk(&ctx, set, &args.iter);
2861
2862         nla_nest_end(skb, nest);
2863         nlmsg_end(skb, nlh);
2864
2865         if (args.iter.err && args.iter.err != -EMSGSIZE)
2866                 return args.iter.err;
2867         if (args.iter.count == cb->args[0])
2868                 return 0;
2869
2870         cb->args[0] = args.iter.count;
2871         return skb->len;
2872
2873 nla_put_failure:
2874         return -ENOSPC;
2875 }
2876
2877 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2878                                 const struct nlmsghdr *nlh,
2879                                 const struct nlattr * const nla[])
2880 {
2881         const struct nft_set *set;
2882         struct nft_ctx ctx;
2883         int err;
2884
2885         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2886         if (err < 0)
2887                 return err;
2888
2889         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2890         if (IS_ERR(set))
2891                 return PTR_ERR(set);
2892         if (set->flags & NFT_SET_INACTIVE)
2893                 return -ENOENT;
2894
2895         if (nlh->nlmsg_flags & NLM_F_DUMP) {
2896                 struct netlink_dump_control c = {
2897                         .dump = nf_tables_dump_set,
2898                 };
2899                 return netlink_dump_start(nlsk, skb, nlh, &c);
2900         }
2901         return -EOPNOTSUPP;
2902 }
2903
2904 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2905                                        const struct nft_ctx *ctx, u32 seq,
2906                                        u32 portid, int event, u16 flags,
2907                                        const struct nft_set *set,
2908                                        const struct nft_set_elem *elem)
2909 {
2910         struct nfgenmsg *nfmsg;
2911         struct nlmsghdr *nlh;
2912         struct nlattr *nest;
2913         int err;
2914
2915         event |= NFNL_SUBSYS_NFTABLES << 8;
2916         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2917                         flags);
2918         if (nlh == NULL)
2919                 goto nla_put_failure;
2920
2921         nfmsg = nlmsg_data(nlh);
2922         nfmsg->nfgen_family     = ctx->afi->family;
2923         nfmsg->version          = NFNETLINK_V0;
2924         nfmsg->res_id           = 0;
2925
2926         if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2927                 goto nla_put_failure;
2928         if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2929                 goto nla_put_failure;
2930
2931         nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2932         if (nest == NULL)
2933                 goto nla_put_failure;
2934
2935         err = nf_tables_fill_setelem(skb, set, elem);
2936         if (err < 0)
2937                 goto nla_put_failure;
2938
2939         nla_nest_end(skb, nest);
2940
2941         return nlmsg_end(skb, nlh);
2942
2943 nla_put_failure:
2944         nlmsg_trim(skb, nlh);
2945         return -1;
2946 }
2947
2948 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2949                                     const struct nft_set *set,
2950                                     const struct nft_set_elem *elem,
2951                                     int event, u16 flags)
2952 {
2953         struct net *net = ctx->net;
2954         u32 portid = ctx->portid;
2955         struct sk_buff *skb;
2956         int err;
2957
2958         if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2959                 return 0;
2960
2961         err = -ENOBUFS;
2962         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2963         if (skb == NULL)
2964                 goto err;
2965
2966         err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2967                                           set, elem);
2968         if (err < 0) {
2969                 kfree_skb(skb);
2970                 goto err;
2971         }
2972
2973         err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
2974                              GFP_KERNEL);
2975 err:
2976         if (err < 0)
2977                 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2978         return err;
2979 }
2980
2981 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
2982                                               int msg_type,
2983                                               struct nft_set *set)
2984 {
2985         struct nft_trans *trans;
2986
2987         trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
2988         if (trans == NULL)
2989                 return NULL;
2990
2991         nft_trans_elem_set(trans) = set;
2992         return trans;
2993 }
2994
2995 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
2996                             const struct nlattr *attr)
2997 {
2998         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2999         struct nft_data_desc d1, d2;
3000         struct nft_set_elem elem;
3001         struct nft_set_binding *binding;
3002         enum nft_registers dreg;
3003         struct nft_trans *trans;
3004         int err;
3005
3006         if (set->size && set->nelems == set->size)
3007                 return -ENFILE;
3008
3009         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3010                                nft_set_elem_policy);
3011         if (err < 0)
3012                 return err;
3013
3014         if (nla[NFTA_SET_ELEM_KEY] == NULL)
3015                 return -EINVAL;
3016
3017         elem.flags = 0;
3018         if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3019                 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3020                 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3021                         return -EINVAL;
3022         }
3023
3024         if (set->flags & NFT_SET_MAP) {
3025                 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3026                     !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3027                         return -EINVAL;
3028                 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3029                     elem.flags & NFT_SET_ELEM_INTERVAL_END)
3030                         return -EINVAL;
3031         } else {
3032                 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3033                         return -EINVAL;
3034         }
3035
3036         err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3037         if (err < 0)
3038                 goto err1;
3039         err = -EINVAL;
3040         if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3041                 goto err2;
3042
3043         err = -EEXIST;
3044         if (set->ops->get(set, &elem) == 0)
3045                 goto err2;
3046
3047         if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3048                 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3049                 if (err < 0)
3050                         goto err2;
3051
3052                 err = -EINVAL;
3053                 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3054                         goto err3;
3055
3056                 dreg = nft_type_to_reg(set->dtype);
3057                 list_for_each_entry(binding, &set->bindings, list) {
3058                         struct nft_ctx bind_ctx = {
3059                                 .afi    = ctx->afi,
3060                                 .table  = ctx->table,
3061                                 .chain  = (struct nft_chain *)binding->chain,
3062                         };
3063
3064                         err = nft_validate_data_load(&bind_ctx, dreg,
3065                                                      &elem.data, d2.type);
3066                         if (err < 0)
3067                                 goto err3;
3068                 }
3069         }
3070
3071         trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3072         if (trans == NULL)
3073                 goto err3;
3074
3075         err = set->ops->insert(set, &elem);
3076         if (err < 0)
3077                 goto err4;
3078
3079         nft_trans_elem(trans) = elem;
3080         list_add(&trans->list, &ctx->net->nft.commit_list);
3081         return 0;
3082
3083 err4:
3084         kfree(trans);
3085 err3:
3086         if (nla[NFTA_SET_ELEM_DATA] != NULL)
3087                 nft_data_uninit(&elem.data, d2.type);
3088 err2:
3089         nft_data_uninit(&elem.key, d1.type);
3090 err1:
3091         return err;
3092 }
3093
3094 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3095                                 const struct nlmsghdr *nlh,
3096                                 const struct nlattr * const nla[])
3097 {
3098         struct net *net = sock_net(skb->sk);
3099         const struct nlattr *attr;
3100         struct nft_set *set;
3101         struct nft_ctx ctx;
3102         int rem, err = 0;
3103
3104         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3105         if (err < 0)
3106                 return err;
3107
3108         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3109         if (IS_ERR(set)) {
3110                 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3111                         set = nf_tables_set_lookup_byid(net,
3112                                         nla[NFTA_SET_ELEM_LIST_SET_ID]);
3113                 }
3114                 if (IS_ERR(set))
3115                         return PTR_ERR(set);
3116         }
3117
3118         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3119                 return -EBUSY;
3120
3121         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3122                 err = nft_add_set_elem(&ctx, set, attr);
3123                 if (err < 0)
3124                         break;
3125         }
3126         return err;
3127 }
3128
3129 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3130                            const struct nlattr *attr)
3131 {
3132         struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3133         struct nft_data_desc desc;
3134         struct nft_set_elem elem;
3135         struct nft_trans *trans;
3136         int err;
3137
3138         err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3139                                nft_set_elem_policy);
3140         if (err < 0)
3141                 goto err1;
3142
3143         err = -EINVAL;
3144         if (nla[NFTA_SET_ELEM_KEY] == NULL)
3145                 goto err1;
3146
3147         err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3148         if (err < 0)
3149                 goto err1;
3150
3151         err = -EINVAL;
3152         if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3153                 goto err2;
3154
3155         err = set->ops->get(set, &elem);
3156         if (err < 0)
3157                 goto err2;
3158
3159         trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3160         if (trans == NULL)
3161                 goto err2;
3162
3163         nft_trans_elem(trans) = elem;
3164         list_add(&trans->list, &ctx->net->nft.commit_list);
3165
3166         nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3167         if (set->flags & NFT_SET_MAP)
3168                 nft_data_uninit(&elem.data, set->dtype);
3169
3170 err2:
3171         nft_data_uninit(&elem.key, desc.type);
3172 err1:
3173         return err;
3174 }
3175
3176 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3177                                 const struct nlmsghdr *nlh,
3178                                 const struct nlattr * const nla[])
3179 {
3180         const struct nlattr *attr;
3181         struct nft_set *set;
3182         struct nft_ctx ctx;
3183         int rem, err = 0;
3184
3185         err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3186         if (err < 0)
3187                 return err;
3188
3189         set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3190         if (IS_ERR(set))
3191                 return PTR_ERR(set);
3192         if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3193                 return -EBUSY;
3194
3195         nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3196                 err = nft_del_setelem(&ctx, set, attr);
3197                 if (err < 0)
3198                         break;
3199         }
3200         return err;
3201 }
3202
3203 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3204         [NFT_MSG_NEWTABLE] = {
3205                 .call_batch     = nf_tables_newtable,
3206                 .attr_count     = NFTA_TABLE_MAX,
3207                 .policy         = nft_table_policy,
3208         },
3209         [NFT_MSG_GETTABLE] = {
3210                 .call           = nf_tables_gettable,
3211                 .attr_count     = NFTA_TABLE_MAX,
3212                 .policy         = nft_table_policy,
3213         },
3214         [NFT_MSG_DELTABLE] = {
3215                 .call_batch     = nf_tables_deltable,
3216                 .attr_count     = NFTA_TABLE_MAX,
3217                 .policy         = nft_table_policy,
3218         },
3219         [NFT_MSG_NEWCHAIN] = {
3220                 .call_batch     = nf_tables_newchain,
3221                 .attr_count     = NFTA_CHAIN_MAX,
3222                 .policy         = nft_chain_policy,
3223         },
3224         [NFT_MSG_GETCHAIN] = {
3225                 .call           = nf_tables_getchain,
3226                 .attr_count     = NFTA_CHAIN_MAX,
3227                 .policy         = nft_chain_policy,
3228         },
3229         [NFT_MSG_DELCHAIN] = {
3230                 .call_batch     = nf_tables_delchain,
3231                 .attr_count     = NFTA_CHAIN_MAX,
3232                 .policy         = nft_chain_policy,
3233         },
3234         [NFT_MSG_NEWRULE] = {
3235                 .call_batch     = nf_tables_newrule,
3236                 .attr_count     = NFTA_RULE_MAX,
3237                 .policy         = nft_rule_policy,
3238         },
3239         [NFT_MSG_GETRULE] = {
3240                 .call           = nf_tables_getrule,
3241                 .attr_count     = NFTA_RULE_MAX,
3242                 .policy         = nft_rule_policy,
3243         },
3244         [NFT_MSG_DELRULE] = {
3245                 .call_batch     = nf_tables_delrule,
3246                 .attr_count     = NFTA_RULE_MAX,
3247                 .policy         = nft_rule_policy,
3248         },
3249         [NFT_MSG_NEWSET] = {
3250                 .call_batch     = nf_tables_newset,
3251                 .attr_count     = NFTA_SET_MAX,
3252                 .policy         = nft_set_policy,
3253         },
3254         [NFT_MSG_GETSET] = {
3255                 .call           = nf_tables_getset,
3256                 .attr_count     = NFTA_SET_MAX,
3257                 .policy         = nft_set_policy,
3258         },
3259         [NFT_MSG_DELSET] = {
3260                 .call_batch     = nf_tables_delset,
3261                 .attr_count     = NFTA_SET_MAX,
3262                 .policy         = nft_set_policy,
3263         },
3264         [NFT_MSG_NEWSETELEM] = {
3265                 .call_batch     = nf_tables_newsetelem,
3266                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3267                 .policy         = nft_set_elem_list_policy,
3268         },
3269         [NFT_MSG_GETSETELEM] = {
3270                 .call           = nf_tables_getsetelem,
3271                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3272                 .policy         = nft_set_elem_list_policy,
3273         },
3274         [NFT_MSG_DELSETELEM] = {
3275                 .call_batch     = nf_tables_delsetelem,
3276                 .attr_count     = NFTA_SET_ELEM_LIST_MAX,
3277                 .policy         = nft_set_elem_list_policy,
3278         },
3279 };
3280
3281 static void nft_chain_commit_update(struct nft_trans *trans)
3282 {
3283         struct nft_base_chain *basechain;
3284
3285         if (nft_trans_chain_name(trans)[0])
3286                 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3287
3288         if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3289                 return;
3290
3291         basechain = nft_base_chain(trans->ctx.chain);
3292         nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3293
3294         switch (nft_trans_chain_policy(trans)) {
3295         case NF_DROP:
3296         case NF_ACCEPT:
3297                 basechain->policy = nft_trans_chain_policy(trans);
3298                 break;
3299         }
3300 }
3301
3302 /* Schedule objects for release via rcu to make sure no packets are accesing
3303  * removed rules.
3304  */
3305 static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3306 {
3307         struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3308
3309         switch (trans->msg_type) {
3310         case NFT_MSG_DELTABLE:
3311                 nf_tables_table_destroy(&trans->ctx);
3312                 break;
3313         case NFT_MSG_DELCHAIN:
3314                 nf_tables_chain_destroy(trans->ctx.chain);
3315                 break;
3316         case NFT_MSG_DELRULE:
3317                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3318                 break;
3319         case NFT_MSG_DELSET:
3320                 nft_set_destroy(nft_trans_set(trans));
3321                 break;
3322         }
3323         kfree(trans);
3324 }
3325
3326 static int nf_tables_commit(struct sk_buff *skb)
3327 {
3328         struct net *net = sock_net(skb->sk);
3329         struct nft_trans *trans, *next;
3330         struct nft_set *set;
3331
3332         /* Bump generation counter, invalidate any dump in progress */
3333         net->nft.genctr++;
3334
3335         /* A new generation has just started */
3336         net->nft.gencursor = gencursor_next(net);
3337
3338         /* Make sure all packets have left the previous generation before
3339          * purging old rules.
3340          */
3341         synchronize_rcu();
3342
3343         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3344                 switch (trans->msg_type) {
3345                 case NFT_MSG_NEWTABLE:
3346                         if (nft_trans_table_update(trans)) {
3347                                 if (!nft_trans_table_enable(trans)) {
3348                                         nf_tables_table_disable(trans->ctx.afi,
3349                                                                 trans->ctx.table);
3350                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3351                                 }
3352                         } else {
3353                                 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3354                         }
3355                         nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3356                         nft_trans_destroy(trans);
3357                         break;
3358                 case NFT_MSG_DELTABLE:
3359                         nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3360                         break;
3361                 case NFT_MSG_NEWCHAIN:
3362                         if (nft_trans_chain_update(trans))
3363                                 nft_chain_commit_update(trans);
3364                         else {
3365                                 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3366                                 trans->ctx.table->use++;
3367                         }
3368                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3369                         nft_trans_destroy(trans);
3370                         break;
3371                 case NFT_MSG_DELCHAIN:
3372                         trans->ctx.table->use--;
3373                         nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3374                         if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3375                             trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3376                                 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3377                                                     trans->ctx.afi->nops);
3378                         }
3379                         break;
3380                 case NFT_MSG_NEWRULE:
3381                         nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3382                         nf_tables_rule_notify(&trans->ctx,
3383                                               nft_trans_rule(trans),
3384                                               NFT_MSG_NEWRULE);
3385                         nft_trans_destroy(trans);
3386                         break;
3387                 case NFT_MSG_DELRULE:
3388                         list_del_rcu(&nft_trans_rule(trans)->list);
3389                         nf_tables_rule_notify(&trans->ctx,
3390                                               nft_trans_rule(trans),
3391                                               NFT_MSG_DELRULE);
3392                         break;
3393                 case NFT_MSG_NEWSET:
3394                         nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3395                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3396                                              NFT_MSG_NEWSET);
3397                         nft_trans_destroy(trans);
3398                         break;
3399                 case NFT_MSG_DELSET:
3400                         nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3401                                              NFT_MSG_DELSET);
3402                         break;
3403                 case NFT_MSG_NEWSETELEM:
3404                         nft_trans_elem_set(trans)->nelems++;
3405                         nf_tables_setelem_notify(&trans->ctx,
3406                                                  nft_trans_elem_set(trans),
3407                                                  &nft_trans_elem(trans),
3408                                                  NFT_MSG_NEWSETELEM, 0);
3409                         nft_trans_destroy(trans);
3410                         break;
3411                 case NFT_MSG_DELSETELEM:
3412                         nft_trans_elem_set(trans)->nelems--;
3413                         nf_tables_setelem_notify(&trans->ctx,
3414                                                  nft_trans_elem_set(trans),
3415                                                  &nft_trans_elem(trans),
3416                                                  NFT_MSG_DELSETELEM, 0);
3417                         set = nft_trans_elem_set(trans);
3418                         set->ops->get(set, &nft_trans_elem(trans));
3419                         set->ops->remove(set, &nft_trans_elem(trans));
3420                         nft_trans_destroy(trans);
3421                         break;
3422                 }
3423         }
3424
3425         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3426                 list_del(&trans->list);
3427                 trans->ctx.nla = NULL;
3428                 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
3429         }
3430
3431         return 0;
3432 }
3433
3434 /* Schedule objects for release via rcu to make sure no packets are accesing
3435  * aborted rules.
3436  */
3437 static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3438 {
3439         struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3440
3441         switch (trans->msg_type) {
3442         case NFT_MSG_NEWTABLE:
3443                 nf_tables_table_destroy(&trans->ctx);
3444                 break;
3445         case NFT_MSG_NEWCHAIN:
3446                 nf_tables_chain_destroy(trans->ctx.chain);
3447                 break;
3448         case NFT_MSG_NEWRULE:
3449                 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3450                 break;
3451         case NFT_MSG_NEWSET:
3452                 nft_set_destroy(nft_trans_set(trans));
3453                 break;
3454         }
3455         kfree(trans);
3456 }
3457
3458 static int nf_tables_abort(struct sk_buff *skb)
3459 {
3460         struct net *net = sock_net(skb->sk);
3461         struct nft_trans *trans, *next;
3462         struct nft_set *set;
3463
3464         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3465                 switch (trans->msg_type) {
3466                 case NFT_MSG_NEWTABLE:
3467                         if (nft_trans_table_update(trans)) {
3468                                 if (nft_trans_table_enable(trans)) {
3469                                         nf_tables_table_disable(trans->ctx.afi,
3470                                                                 trans->ctx.table);
3471                                         trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3472                                 }
3473                                 nft_trans_destroy(trans);
3474                         } else {
3475                                 list_del(&trans->ctx.table->list);
3476                         }
3477                         break;
3478                 case NFT_MSG_DELTABLE:
3479                         list_add_tail(&trans->ctx.table->list,
3480                                       &trans->ctx.afi->tables);
3481                         nft_trans_destroy(trans);
3482                         break;
3483                 case NFT_MSG_NEWCHAIN:
3484                         if (nft_trans_chain_update(trans)) {
3485                                 if (nft_trans_chain_stats(trans))
3486                                         free_percpu(nft_trans_chain_stats(trans));
3487
3488                                 nft_trans_destroy(trans);
3489                         } else {
3490                                 list_del(&trans->ctx.chain->list);
3491                                 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3492                                     trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3493                                         nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3494                                                             trans->ctx.afi->nops);
3495                                 }
3496                         }
3497                         break;
3498                 case NFT_MSG_DELCHAIN:
3499                         list_add_tail(&trans->ctx.chain->list,
3500                                       &trans->ctx.table->chains);
3501                         nft_trans_destroy(trans);
3502                         break;
3503                 case NFT_MSG_NEWRULE:
3504                         list_del_rcu(&nft_trans_rule(trans)->list);
3505                         break;
3506                 case NFT_MSG_DELRULE:
3507                         nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3508                         nft_trans_destroy(trans);
3509                         break;
3510                 case NFT_MSG_NEWSET:
3511                         list_del(&nft_trans_set(trans)->list);
3512                         break;
3513                 case NFT_MSG_DELSET:
3514                         list_add_tail(&nft_trans_set(trans)->list,
3515                                       &trans->ctx.table->sets);
3516                         nft_trans_destroy(trans);
3517                         break;
3518                 case NFT_MSG_NEWSETELEM:
3519                         set = nft_trans_elem_set(trans);
3520                         set->ops->get(set, &nft_trans_elem(trans));
3521                         set->ops->remove(set, &nft_trans_elem(trans));
3522                         nft_trans_destroy(trans);
3523                         break;
3524                 case NFT_MSG_DELSETELEM:
3525                         nft_trans_destroy(trans);
3526                         break;
3527                 }
3528         }
3529
3530         list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3531                 list_del(&trans->list);
3532                 trans->ctx.nla = NULL;
3533                 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
3534         }
3535
3536         return 0;
3537 }
3538
3539 static const struct nfnetlink_subsystem nf_tables_subsys = {
3540         .name           = "nf_tables",
3541         .subsys_id      = NFNL_SUBSYS_NFTABLES,
3542         .cb_count       = NFT_MSG_MAX,
3543         .cb             = nf_tables_cb,
3544         .commit         = nf_tables_commit,
3545         .abort          = nf_tables_abort,
3546 };
3547
3548 /*
3549  * Loop detection - walk through the ruleset beginning at the destination chain
3550  * of a new jump until either the source chain is reached (loop) or all
3551  * reachable chains have been traversed.
3552  *
3553  * The loop check is performed whenever a new jump verdict is added to an
3554  * expression or verdict map or a verdict map is bound to a new chain.
3555  */
3556
3557 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3558                                  const struct nft_chain *chain);
3559
3560 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3561                                         const struct nft_set *set,
3562                                         const struct nft_set_iter *iter,
3563                                         const struct nft_set_elem *elem)
3564 {
3565         if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3566                 return 0;
3567
3568         switch (elem->data.verdict) {
3569         case NFT_JUMP:
3570         case NFT_GOTO:
3571                 return nf_tables_check_loops(ctx, elem->data.chain);
3572         default:
3573                 return 0;
3574         }
3575 }
3576
3577 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3578                                  const struct nft_chain *chain)
3579 {
3580         const struct nft_rule *rule;
3581         const struct nft_expr *expr, *last;
3582         const struct nft_set *set;
3583         struct nft_set_binding *binding;
3584         struct nft_set_iter iter;
3585
3586         if (ctx->chain == chain)
3587                 return -ELOOP;
3588
3589         list_for_each_entry(rule, &chain->rules, list) {
3590                 nft_rule_for_each_expr(expr, last, rule) {
3591                         const struct nft_data *data = NULL;
3592                         int err;
3593
3594                         if (!expr->ops->validate)
3595                                 continue;
3596
3597                         err = expr->ops->validate(ctx, expr, &data);
3598                         if (err < 0)
3599                                 return err;
3600
3601                         if (data == NULL)
3602                                 continue;
3603
3604                         switch (data->verdict) {
3605                         case NFT_JUMP:
3606                         case NFT_GOTO:
3607                                 err = nf_tables_check_loops(ctx, data->chain);
3608                                 if (err < 0)
3609                                         return err;
3610                         default:
3611                                 break;
3612                         }
3613                 }
3614         }
3615
3616         list_for_each_entry(set, &ctx->table->sets, list) {
3617                 if (!(set->flags & NFT_SET_MAP) ||
3618                     set->dtype != NFT_DATA_VERDICT)
3619                         continue;
3620
3621                 list_for_each_entry(binding, &set->bindings, list) {
3622                         if (binding->chain != chain)
3623                                 continue;
3624
3625                         iter.skip       = 0;
3626                         iter.count      = 0;
3627                         iter.err        = 0;
3628                         iter.fn         = nf_tables_loop_check_setelem;
3629
3630                         set->ops->walk(ctx, set, &iter);
3631                         if (iter.err < 0)
3632                                 return iter.err;
3633                 }
3634         }
3635
3636         return 0;
3637 }
3638
3639 /**
3640  *      nft_validate_input_register - validate an expressions' input register
3641  *
3642  *      @reg: the register number
3643  *
3644  *      Validate that the input register is one of the general purpose
3645  *      registers.
3646  */
3647 int nft_validate_input_register(enum nft_registers reg)
3648 {
3649         if (reg <= NFT_REG_VERDICT)
3650                 return -EINVAL;
3651         if (reg > NFT_REG_MAX)
3652                 return -ERANGE;
3653         return 0;
3654 }
3655 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3656
3657 /**
3658  *      nft_validate_output_register - validate an expressions' output register
3659  *
3660  *      @reg: the register number
3661  *
3662  *      Validate that the output register is one of the general purpose
3663  *      registers or the verdict register.
3664  */
3665 int nft_validate_output_register(enum nft_registers reg)
3666 {
3667         if (reg < NFT_REG_VERDICT)
3668                 return -EINVAL;
3669         if (reg > NFT_REG_MAX)
3670                 return -ERANGE;
3671         return 0;
3672 }
3673 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3674
3675 /**
3676  *      nft_validate_data_load - validate an expressions' data load
3677  *
3678  *      @ctx: context of the expression performing the load
3679  *      @reg: the destination register number
3680  *      @data: the data to load
3681  *      @type: the data type
3682  *
3683  *      Validate that a data load uses the appropriate data type for
3684  *      the destination register. A value of NULL for the data means
3685  *      that its runtime gathered data, which is always of type
3686  *      NFT_DATA_VALUE.
3687  */
3688 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3689                            const struct nft_data *data,
3690                            enum nft_data_types type)
3691 {
3692         int err;
3693
3694         switch (reg) {
3695         case NFT_REG_VERDICT:
3696                 if (data == NULL || type != NFT_DATA_VERDICT)
3697                         return -EINVAL;
3698
3699                 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3700                         err = nf_tables_check_loops(ctx, data->chain);
3701                         if (err < 0)
3702                                 return err;
3703
3704                         if (ctx->chain->level + 1 > data->chain->level) {
3705                                 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3706                                         return -EMLINK;
3707                                 data->chain->level = ctx->chain->level + 1;
3708                         }
3709                 }
3710
3711                 return 0;
3712         default:
3713                 if (data != NULL && type != NFT_DATA_VALUE)
3714                         return -EINVAL;
3715                 return 0;
3716         }
3717 }
3718 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3719
3720 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3721         [NFTA_VERDICT_CODE]     = { .type = NLA_U32 },
3722         [NFTA_VERDICT_CHAIN]    = { .type = NLA_STRING,
3723                                     .len = NFT_CHAIN_MAXNAMELEN - 1 },
3724 };
3725
3726 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3727                             struct nft_data_desc *desc, const struct nlattr *nla)
3728 {
3729         struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3730         struct nft_chain *chain;
3731         int err;
3732
3733         err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3734         if (err < 0)
3735                 return err;
3736
3737         if (!tb[NFTA_VERDICT_CODE])
3738                 return -EINVAL;
3739         data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3740
3741         switch (data->verdict) {
3742         default:
3743                 switch (data->verdict & NF_VERDICT_MASK) {
3744                 case NF_ACCEPT:
3745                 case NF_DROP:
3746                 case NF_QUEUE:
3747                         break;
3748                 default:
3749                         return -EINVAL;
3750                 }
3751                 /* fall through */
3752         case NFT_CONTINUE:
3753         case NFT_BREAK:
3754         case NFT_RETURN:
3755                 desc->len = sizeof(data->verdict);
3756                 break;
3757         case NFT_JUMP:
3758         case NFT_GOTO:
3759                 if (!tb[NFTA_VERDICT_CHAIN])
3760                         return -EINVAL;
3761                 chain = nf_tables_chain_lookup(ctx->table,
3762                                                tb[NFTA_VERDICT_CHAIN]);
3763                 if (IS_ERR(chain))
3764                         return PTR_ERR(chain);
3765                 if (chain->flags & NFT_BASE_CHAIN)
3766                         return -EOPNOTSUPP;
3767
3768                 chain->use++;
3769                 data->chain = chain;
3770                 desc->len = sizeof(data);
3771                 break;
3772         }
3773
3774         desc->type = NFT_DATA_VERDICT;
3775         return 0;
3776 }
3777
3778 static void nft_verdict_uninit(const struct nft_data *data)
3779 {
3780         switch (data->verdict) {
3781         case NFT_JUMP:
3782         case NFT_GOTO:
3783                 data->chain->use--;
3784                 break;
3785         }
3786 }
3787
3788 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3789 {
3790         struct nlattr *nest;
3791
3792         nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3793         if (!nest)
3794                 goto nla_put_failure;
3795
3796         if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3797                 goto nla_put_failure;
3798
3799         switch (data->verdict) {
3800         case NFT_JUMP:
3801         case NFT_GOTO:
3802                 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3803                         goto nla_put_failure;
3804         }
3805         nla_nest_end(skb, nest);
3806         return 0;
3807
3808 nla_put_failure:
3809         return -1;
3810 }
3811
3812 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3813                           struct nft_data_desc *desc, const struct nlattr *nla)
3814 {
3815         unsigned int len;
3816
3817         len = nla_len(nla);
3818         if (len == 0)
3819                 return -EINVAL;
3820         if (len > sizeof(data->data))
3821                 return -EOVERFLOW;
3822
3823         nla_memcpy(data->data, nla, sizeof(data->data));
3824         desc->type = NFT_DATA_VALUE;
3825         desc->len  = len;
3826         return 0;
3827 }
3828
3829 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3830                           unsigned int len)
3831 {
3832         return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3833 }
3834
3835 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3836         [NFTA_DATA_VALUE]       = { .type = NLA_BINARY,
3837                                     .len  = FIELD_SIZEOF(struct nft_data, data) },
3838         [NFTA_DATA_VERDICT]     = { .type = NLA_NESTED },
3839 };
3840
3841 /**
3842  *      nft_data_init - parse nf_tables data netlink attributes
3843  *
3844  *      @ctx: context of the expression using the data
3845  *      @data: destination struct nft_data
3846  *      @desc: data description
3847  *      @nla: netlink attribute containing data
3848  *
3849  *      Parse the netlink data attributes and initialize a struct nft_data.
3850  *      The type and length of data are returned in the data description.
3851  *
3852  *      The caller can indicate that it only wants to accept data of type
3853  *      NFT_DATA_VALUE by passing NULL for the ctx argument.
3854  */
3855 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3856                   struct nft_data_desc *desc, const struct nlattr *nla)
3857 {
3858         struct nlattr *tb[NFTA_DATA_MAX + 1];
3859         int err;
3860
3861         err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3862         if (err < 0)
3863                 return err;
3864
3865         if (tb[NFTA_DATA_VALUE])
3866                 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3867         if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3868                 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3869         return -EINVAL;
3870 }
3871 EXPORT_SYMBOL_GPL(nft_data_init);
3872
3873 /**
3874  *      nft_data_uninit - release a nft_data item
3875  *
3876  *      @data: struct nft_data to release
3877  *      @type: type of data
3878  *
3879  *      Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3880  *      all others need to be released by calling this function.
3881  */
3882 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3883 {
3884         switch (type) {
3885         case NFT_DATA_VALUE:
3886                 return;
3887         case NFT_DATA_VERDICT:
3888                 return nft_verdict_uninit(data);
3889         default:
3890                 WARN_ON(1);
3891         }
3892 }
3893 EXPORT_SYMBOL_GPL(nft_data_uninit);
3894
3895 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3896                   enum nft_data_types type, unsigned int len)
3897 {
3898         struct nlattr *nest;
3899         int err;
3900
3901         nest = nla_nest_start(skb, attr);
3902         if (nest == NULL)
3903                 return -1;
3904
3905         switch (type) {
3906         case NFT_DATA_VALUE:
3907                 err = nft_value_dump(skb, data, len);
3908                 break;
3909         case NFT_DATA_VERDICT:
3910                 err = nft_verdict_dump(skb, data);
3911                 break;
3912         default:
3913                 err = -EINVAL;
3914                 WARN_ON(1);
3915         }
3916
3917         nla_nest_end(skb, nest);
3918         return err;
3919 }
3920 EXPORT_SYMBOL_GPL(nft_data_dump);
3921
3922 static int nf_tables_init_net(struct net *net)
3923 {
3924         INIT_LIST_HEAD(&net->nft.af_info);
3925         INIT_LIST_HEAD(&net->nft.commit_list);
3926         return 0;
3927 }
3928
3929 static struct pernet_operations nf_tables_net_ops = {
3930         .init   = nf_tables_init_net,
3931 };
3932
3933 static int __init nf_tables_module_init(void)
3934 {
3935         int err;
3936
3937         info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3938                        GFP_KERNEL);
3939         if (info == NULL) {
3940                 err = -ENOMEM;
3941                 goto err1;
3942         }
3943
3944         err = nf_tables_core_module_init();
3945         if (err < 0)
3946                 goto err2;
3947
3948         err = nfnetlink_subsys_register(&nf_tables_subsys);
3949         if (err < 0)
3950                 goto err3;
3951
3952         pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3953         return register_pernet_subsys(&nf_tables_net_ops);
3954 err3:
3955         nf_tables_core_module_exit();
3956 err2:
3957         kfree(info);
3958 err1:
3959         return err;
3960 }
3961
3962 static void __exit nf_tables_module_exit(void)
3963 {
3964         unregister_pernet_subsys(&nf_tables_net_ops);
3965         nfnetlink_subsys_unregister(&nf_tables_subsys);
3966         nf_tables_core_module_exit();
3967         kfree(info);
3968 }
3969
3970 module_init(nf_tables_module_init);
3971 module_exit(nf_tables_module_exit);
3972
3973 MODULE_LICENSE("GPL");
3974 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3975 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);