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