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