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