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