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