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