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