2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
23 static LIST_HEAD(nf_tables_afinfo);
24 static LIST_HEAD(nf_tables_expressions);
27 * nft_register_afinfo - register nf_tables address family info
29 * @afi: address family info to register
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
34 int nft_register_afinfo(struct nft_af_info *afi)
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail(&afi->list, &nf_tables_afinfo);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
45 * nft_unregister_afinfo - unregister nf_tables address family info
47 * @afi: address family info to unregister
49 * Unregister the address family for use with nf_tables.
51 void nft_unregister_afinfo(struct nft_af_info *afi)
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
59 static struct nft_af_info *nft_afinfo_lookup(int family)
61 struct nft_af_info *afi;
63 list_for_each_entry(afi, &nf_tables_afinfo, list) {
64 if (afi->family == family)
70 static struct nft_af_info *nf_tables_afinfo_lookup(int family, bool autoload)
72 struct nft_af_info *afi;
74 afi = nft_afinfo_lookup(family);
79 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
80 request_module("nft-afinfo-%u", family);
81 nfnl_lock(NFNL_SUBSYS_NFTABLES);
82 afi = nft_afinfo_lookup(family);
84 return ERR_PTR(-EAGAIN);
87 return ERR_PTR(-EAFNOSUPPORT);
94 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
95 const struct nlattr *nla)
97 struct nft_table *table;
99 list_for_each_entry(table, &afi->tables, list) {
100 if (!nla_strcmp(nla, table->name))
106 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
107 const struct nlattr *nla,
110 struct nft_table *table;
113 return ERR_PTR(-EINVAL);
115 table = nft_table_lookup(afi, nla);
119 #ifdef CONFIG_MODULES
121 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
122 request_module("nft-table-%u-%*.s", afi->family,
123 nla_len(nla)-1, (const char *)nla_data(nla));
124 nfnl_lock(NFNL_SUBSYS_NFTABLES);
125 if (nft_table_lookup(afi, nla))
126 return ERR_PTR(-EAGAIN);
129 return ERR_PTR(-ENOENT);
132 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
134 return ++table->hgenerator;
137 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
138 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
141 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
142 int event, u32 flags, int family,
143 const struct nft_table *table)
145 struct nlmsghdr *nlh;
146 struct nfgenmsg *nfmsg;
148 event |= NFNL_SUBSYS_NFTABLES << 8;
149 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
151 goto nla_put_failure;
153 nfmsg = nlmsg_data(nlh);
154 nfmsg->nfgen_family = family;
155 nfmsg->version = NFNETLINK_V0;
158 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name))
159 goto nla_put_failure;
161 return nlmsg_end(skb, nlh);
164 nlmsg_trim(skb, nlh);
168 static int nf_tables_table_notify(const struct sk_buff *oskb,
169 const struct nlmsghdr *nlh,
170 const struct nft_table *table,
171 int event, int family)
174 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
175 u32 seq = nlh ? nlh->nlmsg_seq : 0;
176 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
180 report = nlh ? nlmsg_report(nlh) : false;
181 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
185 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
189 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
196 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
200 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
204 static int nf_tables_dump_tables(struct sk_buff *skb,
205 struct netlink_callback *cb)
207 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
208 const struct nft_af_info *afi;
209 const struct nft_table *table;
210 unsigned int idx = 0, s_idx = cb->args[0];
211 int family = nfmsg->nfgen_family;
213 list_for_each_entry(afi, &nf_tables_afinfo, list) {
214 if (family != NFPROTO_UNSPEC && family != afi->family)
217 list_for_each_entry(table, &afi->tables, list) {
221 memset(&cb->args[1], 0,
222 sizeof(cb->args) - sizeof(cb->args[0]));
223 if (nf_tables_fill_table_info(skb,
224 NETLINK_CB(cb->skb).portid,
228 afi->family, table) < 0)
239 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
240 const struct nlmsghdr *nlh,
241 const struct nlattr * const nla[])
243 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
244 const struct nft_af_info *afi;
245 const struct nft_table *table;
246 struct sk_buff *skb2;
247 int family = nfmsg->nfgen_family;
250 if (nlh->nlmsg_flags & NLM_F_DUMP) {
251 struct netlink_dump_control c = {
252 .dump = nf_tables_dump_tables,
254 return netlink_dump_start(nlsk, skb, nlh, &c);
257 afi = nf_tables_afinfo_lookup(family, false);
261 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], false);
263 return PTR_ERR(table);
265 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
269 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
270 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
275 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
282 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
283 const struct nlmsghdr *nlh,
284 const struct nlattr * const nla[])
286 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
287 const struct nlattr *name;
288 struct nft_af_info *afi;
289 struct nft_table *table;
290 int family = nfmsg->nfgen_family;
292 afi = nf_tables_afinfo_lookup(family, true);
296 name = nla[NFTA_TABLE_NAME];
297 table = nf_tables_table_lookup(afi, name, false);
299 if (PTR_ERR(table) != -ENOENT)
300 return PTR_ERR(table);
305 if (nlh->nlmsg_flags & NLM_F_EXCL)
307 if (nlh->nlmsg_flags & NLM_F_REPLACE)
312 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
316 nla_strlcpy(table->name, name, nla_len(name));
317 INIT_LIST_HEAD(&table->chains);
318 INIT_LIST_HEAD(&table->sets);
320 list_add_tail(&table->list, &afi->tables);
321 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
325 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
326 const struct nlmsghdr *nlh,
327 const struct nlattr * const nla[])
329 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
330 struct nft_af_info *afi;
331 struct nft_table *table;
332 int family = nfmsg->nfgen_family;
334 afi = nf_tables_afinfo_lookup(family, false);
338 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], false);
340 return PTR_ERR(table);
342 if (table->flags & NFT_TABLE_BUILTIN)
348 list_del(&table->list);
349 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
354 static struct nft_table *__nf_tables_table_lookup(const struct nft_af_info *afi,
357 struct nft_table *table;
359 list_for_each_entry(table, &afi->tables, list) {
360 if (!strcmp(name, table->name))
364 return ERR_PTR(-ENOENT);
367 static int nf_tables_chain_notify(const struct sk_buff *oskb,
368 const struct nlmsghdr *nlh,
369 const struct nft_table *table,
370 const struct nft_chain *chain,
371 int event, int family);
374 * nft_register_table - register a built-in table
376 * @table: the table to register
377 * @family: protocol family to register table with
379 * Register a built-in table for use with nf_tables. Returns zero on
380 * success or a negative errno code otherwise.
382 int nft_register_table(struct nft_table *table, int family)
384 struct nft_af_info *afi;
386 struct nft_chain *chain;
389 nfnl_lock(NFNL_SUBSYS_NFTABLES);
391 afi = nf_tables_afinfo_lookup(family, true);
399 t = __nf_tables_table_lookup(afi, table->name);
412 table->flags |= NFT_TABLE_BUILTIN;
413 INIT_LIST_HEAD(&table->sets);
414 list_add_tail(&table->list, &afi->tables);
415 nf_tables_table_notify(NULL, NULL, table, NFT_MSG_NEWTABLE, family);
416 list_for_each_entry(chain, &table->chains, list)
417 nf_tables_chain_notify(NULL, NULL, table, chain,
418 NFT_MSG_NEWCHAIN, family);
421 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
424 EXPORT_SYMBOL_GPL(nft_register_table);
427 * nft_unregister_table - unregister a built-in table
429 * @table: the table to unregister
430 * @family: protocol family to unregister table with
432 * Unregister a built-in table for use with nf_tables.
434 void nft_unregister_table(struct nft_table *table, int family)
436 struct nft_chain *chain;
438 nfnl_lock(NFNL_SUBSYS_NFTABLES);
439 list_del(&table->list);
440 list_for_each_entry(chain, &table->chains, list)
441 nf_tables_chain_notify(NULL, NULL, table, chain,
442 NFT_MSG_DELCHAIN, family);
443 nf_tables_table_notify(NULL, NULL, table, NFT_MSG_DELTABLE, family);
444 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
446 EXPORT_SYMBOL_GPL(nft_unregister_table);
452 static struct nft_chain *
453 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
455 struct nft_chain *chain;
457 list_for_each_entry(chain, &table->chains, list) {
458 if (chain->handle == handle)
462 return ERR_PTR(-ENOENT);
465 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
466 const struct nlattr *nla)
468 struct nft_chain *chain;
471 return ERR_PTR(-EINVAL);
473 list_for_each_entry(chain, &table->chains, list) {
474 if (!nla_strcmp(nla, chain->name))
478 return ERR_PTR(-ENOENT);
481 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
482 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
483 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
484 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
485 .len = NFT_CHAIN_MAXNAMELEN - 1 },
486 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
489 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
490 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
491 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
494 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
495 int event, u32 flags, int family,
496 const struct nft_table *table,
497 const struct nft_chain *chain)
499 struct nlmsghdr *nlh;
500 struct nfgenmsg *nfmsg;
502 event |= NFNL_SUBSYS_NFTABLES << 8;
503 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
505 goto nla_put_failure;
507 nfmsg = nlmsg_data(nlh);
508 nfmsg->nfgen_family = family;
509 nfmsg->version = NFNETLINK_V0;
512 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
513 goto nla_put_failure;
514 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
515 goto nla_put_failure;
516 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
517 goto nla_put_failure;
519 if (chain->flags & NFT_BASE_CHAIN) {
520 const struct nf_hook_ops *ops = &nft_base_chain(chain)->ops;
521 struct nlattr *nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
523 goto nla_put_failure;
524 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
525 goto nla_put_failure;
526 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
527 goto nla_put_failure;
528 nla_nest_end(skb, nest);
531 return nlmsg_end(skb, nlh);
534 nlmsg_trim(skb, nlh);
538 static int nf_tables_chain_notify(const struct sk_buff *oskb,
539 const struct nlmsghdr *nlh,
540 const struct nft_table *table,
541 const struct nft_chain *chain,
542 int event, int family)
545 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
546 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
547 u32 seq = nlh ? nlh->nlmsg_seq : 0;
551 report = nlh ? nlmsg_report(nlh) : false;
552 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
556 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
560 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
567 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
571 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
575 static int nf_tables_dump_chains(struct sk_buff *skb,
576 struct netlink_callback *cb)
578 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
579 const struct nft_af_info *afi;
580 const struct nft_table *table;
581 const struct nft_chain *chain;
582 unsigned int idx = 0, s_idx = cb->args[0];
583 int family = nfmsg->nfgen_family;
585 list_for_each_entry(afi, &nf_tables_afinfo, list) {
586 if (family != NFPROTO_UNSPEC && family != afi->family)
589 list_for_each_entry(table, &afi->tables, list) {
590 list_for_each_entry(chain, &table->chains, list) {
594 memset(&cb->args[1], 0,
595 sizeof(cb->args) - sizeof(cb->args[0]));
596 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
600 afi->family, table, chain) < 0)
613 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
614 const struct nlmsghdr *nlh,
615 const struct nlattr * const nla[])
617 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
618 const struct nft_af_info *afi;
619 const struct nft_table *table;
620 const struct nft_chain *chain;
621 struct sk_buff *skb2;
622 int family = nfmsg->nfgen_family;
625 if (nlh->nlmsg_flags & NLM_F_DUMP) {
626 struct netlink_dump_control c = {
627 .dump = nf_tables_dump_chains,
629 return netlink_dump_start(nlsk, skb, nlh, &c);
632 afi = nf_tables_afinfo_lookup(family, false);
636 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], false);
638 return PTR_ERR(table);
640 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
642 return PTR_ERR(chain);
644 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
648 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
649 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
650 family, table, chain);
654 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
661 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
662 const struct nlmsghdr *nlh,
663 const struct nlattr * const nla[])
665 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
666 const struct nlattr * uninitialized_var(name);
667 const struct nft_af_info *afi;
668 struct nft_table *table;
669 struct nft_chain *chain;
670 struct nft_base_chain *basechain;
671 struct nlattr *ha[NFTA_HOOK_MAX + 1];
672 int family = nfmsg->nfgen_family;
677 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
679 afi = nf_tables_afinfo_lookup(family, true);
683 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], create);
685 return PTR_ERR(table);
687 if (table->use == UINT_MAX)
691 name = nla[NFTA_CHAIN_NAME];
693 if (nla[NFTA_CHAIN_HANDLE]) {
694 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
695 chain = nf_tables_chain_lookup_byhandle(table, handle);
697 return PTR_ERR(chain);
699 chain = nf_tables_chain_lookup(table, name);
701 if (PTR_ERR(chain) != -ENOENT)
702 return PTR_ERR(chain);
708 if (nlh->nlmsg_flags & NLM_F_EXCL)
710 if (nlh->nlmsg_flags & NLM_F_REPLACE)
713 if (nla[NFTA_CHAIN_HANDLE] && name &&
714 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
717 if (nla[NFTA_CHAIN_HANDLE] && name)
718 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
723 if (nla[NFTA_CHAIN_HOOK]) {
724 struct nf_hook_ops *ops;
726 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
730 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
731 ha[NFTA_HOOK_PRIORITY] == NULL)
733 if (ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])) >= afi->nhooks)
736 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
737 if (basechain == NULL)
739 chain = &basechain->chain;
741 ops = &basechain->ops;
743 ops->owner = afi->owner;
744 ops->hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
745 ops->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
747 ops->hook = nft_do_chain;
748 if (afi->hooks[ops->hooknum])
749 ops->hook = afi->hooks[ops->hooknum];
751 chain->flags |= NFT_BASE_CHAIN;
753 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
758 INIT_LIST_HEAD(&chain->rules);
759 chain->handle = nf_tables_alloc_handle(table);
760 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
762 list_add_tail(&chain->list, &table->chains);
765 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
770 static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
772 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
774 BUG_ON(chain->use > 0);
776 if (chain->flags & NFT_BASE_CHAIN)
777 kfree(nft_base_chain(chain));
782 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
783 const struct nlmsghdr *nlh,
784 const struct nlattr * const nla[])
786 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
787 const struct nft_af_info *afi;
788 struct nft_table *table;
789 struct nft_chain *chain;
790 int family = nfmsg->nfgen_family;
792 afi = nf_tables_afinfo_lookup(family, false);
796 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], false);
798 return PTR_ERR(table);
800 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
802 return PTR_ERR(chain);
804 if (chain->flags & NFT_CHAIN_BUILTIN)
807 if (!list_empty(&chain->rules))
810 list_del(&chain->list);
813 if (chain->flags & NFT_BASE_CHAIN)
814 nf_unregister_hook(&nft_base_chain(chain)->ops);
816 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
819 /* Make sure all rule references are gone before this is released */
820 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
824 static void nft_ctx_init(struct nft_ctx *ctx,
825 const struct sk_buff *skb,
826 const struct nlmsghdr *nlh,
827 const struct nft_af_info *afi,
828 const struct nft_table *table,
829 const struct nft_chain *chain)
843 * nft_register_expr - register nf_tables expr type
846 * Registers the expr type for use with nf_tables. Returns zero on
847 * success or a negative errno code otherwise.
849 int nft_register_expr(struct nft_expr_type *type)
851 nfnl_lock(NFNL_SUBSYS_NFTABLES);
852 list_add_tail(&type->list, &nf_tables_expressions);
853 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
856 EXPORT_SYMBOL_GPL(nft_register_expr);
859 * nft_unregister_expr - unregister nf_tables expr type
862 * Unregisters the expr typefor use with nf_tables.
864 void nft_unregister_expr(struct nft_expr_type *type)
866 nfnl_lock(NFNL_SUBSYS_NFTABLES);
867 list_del(&type->list);
868 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
870 EXPORT_SYMBOL_GPL(nft_unregister_expr);
872 static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
874 const struct nft_expr_type *type;
876 list_for_each_entry(type, &nf_tables_expressions, list) {
877 if (!nla_strcmp(nla, type->name))
883 static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
885 const struct nft_expr_type *type;
888 return ERR_PTR(-EINVAL);
890 type = __nft_expr_type_get(nla);
891 if (type != NULL && try_module_get(type->owner))
894 #ifdef CONFIG_MODULES
896 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
897 request_module("nft-expr-%.*s",
898 nla_len(nla), (char *)nla_data(nla));
899 nfnl_lock(NFNL_SUBSYS_NFTABLES);
900 if (__nft_expr_type_get(nla))
901 return ERR_PTR(-EAGAIN);
904 return ERR_PTR(-ENOENT);
907 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
908 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
909 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
912 static int nf_tables_fill_expr_info(struct sk_buff *skb,
913 const struct nft_expr *expr)
915 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
916 goto nla_put_failure;
918 if (expr->ops->dump) {
919 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
921 goto nla_put_failure;
922 if (expr->ops->dump(skb, expr) < 0)
923 goto nla_put_failure;
924 nla_nest_end(skb, data);
933 struct nft_expr_info {
934 const struct nft_expr_ops *ops;
935 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
938 static int nf_tables_expr_parse(const struct nlattr *nla,
939 struct nft_expr_info *info)
941 const struct nft_expr_type *type;
942 const struct nft_expr_ops *ops;
943 struct nlattr *tb[NFTA_EXPR_MAX + 1];
946 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
950 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
952 return PTR_ERR(type);
954 if (tb[NFTA_EXPR_DATA]) {
955 err = nla_parse_nested(info->tb, type->maxattr,
956 tb[NFTA_EXPR_DATA], type->policy);
960 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
962 if (type->select_ops != NULL) {
963 ops = type->select_ops((const struct nlattr * const *)info->tb);
975 module_put(type->owner);
979 static int nf_tables_newexpr(const struct nft_ctx *ctx,
980 const struct nft_expr_info *info,
981 struct nft_expr *expr)
983 const struct nft_expr_ops *ops = info->ops;
988 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1000 static void nf_tables_expr_destroy(struct nft_expr *expr)
1002 if (expr->ops->destroy)
1003 expr->ops->destroy(expr);
1004 module_put(expr->ops->type->owner);
1011 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1014 struct nft_rule *rule;
1016 // FIXME: this sucks
1017 list_for_each_entry(rule, &chain->rules, list) {
1018 if (handle == rule->handle)
1022 return ERR_PTR(-ENOENT);
1025 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1026 const struct nlattr *nla)
1029 return ERR_PTR(-EINVAL);
1031 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1034 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1035 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1036 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1037 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1038 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1039 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1042 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1043 int event, u32 flags, int family,
1044 const struct nft_table *table,
1045 const struct nft_chain *chain,
1046 const struct nft_rule *rule)
1048 struct nlmsghdr *nlh;
1049 struct nfgenmsg *nfmsg;
1050 const struct nft_expr *expr, *next;
1051 struct nlattr *list;
1053 event |= NFNL_SUBSYS_NFTABLES << 8;
1054 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1057 goto nla_put_failure;
1059 nfmsg = nlmsg_data(nlh);
1060 nfmsg->nfgen_family = family;
1061 nfmsg->version = NFNETLINK_V0;
1064 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1065 goto nla_put_failure;
1066 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1067 goto nla_put_failure;
1068 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1069 goto nla_put_failure;
1071 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1073 goto nla_put_failure;
1074 nft_rule_for_each_expr(expr, next, rule) {
1075 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1077 goto nla_put_failure;
1078 if (nf_tables_fill_expr_info(skb, expr) < 0)
1079 goto nla_put_failure;
1080 nla_nest_end(skb, elem);
1082 nla_nest_end(skb, list);
1084 return nlmsg_end(skb, nlh);
1087 nlmsg_trim(skb, nlh);
1091 static int nf_tables_rule_notify(const struct sk_buff *oskb,
1092 const struct nlmsghdr *nlh,
1093 const struct nft_table *table,
1094 const struct nft_chain *chain,
1095 const struct nft_rule *rule,
1096 int event, u32 flags, int family)
1098 struct sk_buff *skb;
1099 u32 portid = NETLINK_CB(oskb).portid;
1100 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1101 u32 seq = nlh->nlmsg_seq;
1105 report = nlmsg_report(nlh);
1106 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1110 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1114 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1115 family, table, chain, rule);
1121 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1125 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1129 static int nf_tables_dump_rules(struct sk_buff *skb,
1130 struct netlink_callback *cb)
1132 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1133 const struct nft_af_info *afi;
1134 const struct nft_table *table;
1135 const struct nft_chain *chain;
1136 const struct nft_rule *rule;
1137 unsigned int idx = 0, s_idx = cb->args[0];
1138 int family = nfmsg->nfgen_family;
1140 list_for_each_entry(afi, &nf_tables_afinfo, list) {
1141 if (family != NFPROTO_UNSPEC && family != afi->family)
1144 list_for_each_entry(table, &afi->tables, list) {
1145 list_for_each_entry(chain, &table->chains, list) {
1146 list_for_each_entry(rule, &chain->rules, list) {
1150 memset(&cb->args[1], 0,
1151 sizeof(cb->args) - sizeof(cb->args[0]));
1152 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1155 NLM_F_MULTI | NLM_F_APPEND,
1156 afi->family, table, chain, rule) < 0)
1169 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1170 const struct nlmsghdr *nlh,
1171 const struct nlattr * const nla[])
1173 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1174 const struct nft_af_info *afi;
1175 const struct nft_table *table;
1176 const struct nft_chain *chain;
1177 const struct nft_rule *rule;
1178 struct sk_buff *skb2;
1179 int family = nfmsg->nfgen_family;
1182 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1183 struct netlink_dump_control c = {
1184 .dump = nf_tables_dump_rules,
1186 return netlink_dump_start(nlsk, skb, nlh, &c);
1189 afi = nf_tables_afinfo_lookup(family, false);
1191 return PTR_ERR(afi);
1193 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], false);
1195 return PTR_ERR(table);
1197 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1199 return PTR_ERR(chain);
1201 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1203 return PTR_ERR(rule);
1205 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1209 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1210 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1211 family, table, chain, rule);
1215 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1222 static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1224 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1225 struct nft_expr *expr;
1228 * Careful: some expressions might not be initialized in case this
1229 * is called on error from nf_tables_newrule().
1231 expr = nft_expr_first(rule);
1232 while (expr->ops && expr != nft_expr_last(rule)) {
1233 nf_tables_expr_destroy(expr);
1234 expr = nft_expr_next(expr);
1239 static void nf_tables_rule_destroy(struct nft_rule *rule)
1241 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1244 #define NFT_RULE_MAXEXPRS 128
1246 static struct nft_expr_info *info;
1248 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1249 const struct nlmsghdr *nlh,
1250 const struct nlattr * const nla[])
1252 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1253 const struct nft_af_info *afi;
1254 struct nft_table *table;
1255 struct nft_chain *chain;
1256 struct nft_rule *rule, *old_rule = NULL;
1257 struct nft_expr *expr;
1260 unsigned int size, i, n;
1265 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1267 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, create);
1269 return PTR_ERR(afi);
1271 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], create);
1273 return PTR_ERR(table);
1275 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1277 return PTR_ERR(chain);
1279 if (nla[NFTA_RULE_HANDLE]) {
1280 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1281 rule = __nf_tables_rule_lookup(chain, handle);
1283 return PTR_ERR(rule);
1285 if (nlh->nlmsg_flags & NLM_F_EXCL)
1287 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1292 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1294 handle = nf_tables_alloc_handle(table);
1299 if (nla[NFTA_RULE_EXPRESSIONS]) {
1300 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1302 if (nla_type(tmp) != NFTA_LIST_ELEM)
1304 if (n == NFT_RULE_MAXEXPRS)
1306 err = nf_tables_expr_parse(tmp, &info[n]);
1309 size += info[n].ops->size;
1315 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1319 rule->handle = handle;
1322 nft_ctx_init(&ctx, skb, nlh, afi, table, chain);
1323 expr = nft_expr_first(rule);
1324 for (i = 0; i < n; i++) {
1325 err = nf_tables_newexpr(&ctx, &info[i], expr);
1329 expr = nft_expr_next(expr);
1332 /* Register hook when first rule is inserted into a base chain */
1333 if (list_empty(&chain->rules) && chain->flags & NFT_BASE_CHAIN) {
1334 err = nf_register_hook(&nft_base_chain(chain)->ops);
1339 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1340 list_replace_rcu(&old_rule->list, &rule->list);
1341 nf_tables_rule_destroy(old_rule);
1342 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1343 list_add_tail_rcu(&rule->list, &chain->rules);
1345 list_add_rcu(&rule->list, &chain->rules);
1347 nf_tables_rule_notify(skb, nlh, table, chain, rule, NFT_MSG_NEWRULE,
1348 nlh->nlmsg_flags & (NLM_F_APPEND | NLM_F_REPLACE),
1349 nfmsg->nfgen_family);
1353 nf_tables_rule_destroy(rule);
1355 for (i = 0; i < n; i++) {
1356 if (info[i].ops != NULL)
1357 module_put(info[i].ops->type->owner);
1362 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1363 const struct nlmsghdr *nlh,
1364 const struct nlattr * const nla[])
1366 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1367 const struct nft_af_info *afi;
1368 const struct nft_table *table;
1369 struct nft_chain *chain;
1370 struct nft_rule *rule, *tmp;
1371 int family = nfmsg->nfgen_family;
1373 afi = nf_tables_afinfo_lookup(family, false);
1375 return PTR_ERR(afi);
1377 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], false);
1379 return PTR_ERR(table);
1381 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1383 return PTR_ERR(chain);
1385 if (nla[NFTA_RULE_HANDLE]) {
1386 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1388 return PTR_ERR(rule);
1390 /* List removal must be visible before destroying expressions */
1391 list_del_rcu(&rule->list);
1393 nf_tables_rule_notify(skb, nlh, table, chain, rule,
1394 NFT_MSG_DELRULE, 0, family);
1395 nf_tables_rule_destroy(rule);
1397 /* Remove all rules in this chain */
1398 list_for_each_entry_safe(rule, tmp, &chain->rules, list) {
1399 list_del_rcu(&rule->list);
1401 nf_tables_rule_notify(skb, nlh, table, chain, rule,
1402 NFT_MSG_DELRULE, 0, family);
1403 nf_tables_rule_destroy(rule);
1407 /* Unregister hook when last rule from base chain is deleted */
1408 if (list_empty(&chain->rules) && chain->flags & NFT_BASE_CHAIN)
1409 nf_unregister_hook(&nft_base_chain(chain)->ops);
1418 static LIST_HEAD(nf_tables_set_ops);
1420 int nft_register_set(struct nft_set_ops *ops)
1422 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1423 list_add_tail(&ops->list, &nf_tables_set_ops);
1424 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1427 EXPORT_SYMBOL_GPL(nft_register_set);
1429 void nft_unregister_set(struct nft_set_ops *ops)
1431 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1432 list_del(&ops->list);
1433 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1435 EXPORT_SYMBOL_GPL(nft_unregister_set);
1437 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1439 const struct nft_set_ops *ops;
1442 #ifdef CONFIG_MODULES
1443 if (list_empty(&nf_tables_set_ops)) {
1444 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1445 request_module("nft-set");
1446 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1447 if (!list_empty(&nf_tables_set_ops))
1448 return ERR_PTR(-EAGAIN);
1452 if (nla[NFTA_SET_FLAGS] != NULL) {
1453 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1454 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1457 // FIXME: implement selection properly
1458 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1459 if ((ops->features & features) != features)
1461 if (!try_module_get(ops->owner))
1466 return ERR_PTR(-EOPNOTSUPP);
1469 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1470 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1471 [NFTA_SET_NAME] = { .type = NLA_STRING },
1472 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1473 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1474 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1475 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1476 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1479 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1480 const struct sk_buff *skb,
1481 const struct nlmsghdr *nlh,
1482 const struct nlattr * const nla[])
1484 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1485 const struct nft_af_info *afi;
1486 const struct nft_table *table = NULL;
1488 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, false);
1490 return PTR_ERR(afi);
1492 if (nla[NFTA_SET_TABLE] != NULL) {
1493 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], false);
1495 return PTR_ERR(table);
1498 nft_ctx_init(ctx, skb, nlh, afi, table, NULL);
1502 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1503 const struct nlattr *nla)
1505 struct nft_set *set;
1508 return ERR_PTR(-EINVAL);
1510 list_for_each_entry(set, &table->sets, list) {
1511 if (!nla_strcmp(nla, set->name))
1514 return ERR_PTR(-ENOENT);
1517 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1520 const struct nft_set *i;
1522 unsigned long *inuse;
1525 p = strnchr(name, IFNAMSIZ, '%');
1527 if (p[1] != 'd' || strchr(p + 2, '%'))
1530 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1534 list_for_each_entry(i, &ctx->table->sets, list) {
1535 if (!sscanf(i->name, name, &n))
1537 if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
1542 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1543 free_page((unsigned long)inuse);
1546 snprintf(set->name, sizeof(set->name), name, n);
1547 list_for_each_entry(i, &ctx->table->sets, list) {
1548 if (!strcmp(set->name, i->name))
1554 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
1555 const struct nft_set *set, u16 event, u16 flags)
1557 struct nfgenmsg *nfmsg;
1558 struct nlmsghdr *nlh;
1559 u32 portid = NETLINK_CB(ctx->skb).portid;
1560 u32 seq = ctx->nlh->nlmsg_seq;
1562 event |= NFNL_SUBSYS_NFTABLES << 8;
1563 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1566 goto nla_put_failure;
1568 nfmsg = nlmsg_data(nlh);
1569 nfmsg->nfgen_family = ctx->afi->family;
1570 nfmsg->version = NFNETLINK_V0;
1573 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
1574 goto nla_put_failure;
1575 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
1576 goto nla_put_failure;
1577 if (set->flags != 0)
1578 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
1579 goto nla_put_failure;
1581 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
1582 goto nla_put_failure;
1583 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
1584 goto nla_put_failure;
1585 if (set->flags & NFT_SET_MAP) {
1586 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
1587 goto nla_put_failure;
1588 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
1589 goto nla_put_failure;
1592 return nlmsg_end(skb, nlh);
1595 nlmsg_trim(skb, nlh);
1599 static int nf_tables_set_notify(const struct nft_ctx *ctx,
1600 const struct nft_set *set,
1603 struct sk_buff *skb;
1604 u32 portid = NETLINK_CB(ctx->skb).portid;
1605 struct net *net = sock_net(ctx->skb->sk);
1609 report = nlmsg_report(ctx->nlh);
1610 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1614 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1618 err = nf_tables_fill_set(skb, ctx, set, event, 0);
1624 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1628 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1632 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
1633 struct netlink_callback *cb)
1635 const struct nft_set *set;
1636 unsigned int idx = 0, s_idx = cb->args[0];
1641 list_for_each_entry(set, &ctx->table->sets, list) {
1644 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
1657 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
1658 struct netlink_callback *cb)
1660 const struct nft_set *set;
1661 unsigned int idx = 0, s_idx = cb->args[0];
1662 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
1667 list_for_each_entry(table, &ctx->afi->tables, list) {
1668 if (cur_table && cur_table != table)
1672 list_for_each_entry(set, &ctx->table->sets, list) {
1675 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
1678 cb->args[2] = (unsigned long) table;
1690 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
1692 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1693 struct nlattr *nla[NFTA_SET_MAX + 1];
1697 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
1702 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
1706 if (ctx.table == NULL)
1707 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
1709 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
1714 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
1715 const struct nlmsghdr *nlh,
1716 const struct nlattr * const nla[])
1718 const struct nft_set *set;
1720 struct sk_buff *skb2;
1723 /* Verify existance before starting dump */
1724 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
1728 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1729 struct netlink_dump_control c = {
1730 .dump = nf_tables_dump_sets,
1732 return netlink_dump_start(nlsk, skb, nlh, &c);
1735 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
1737 return PTR_ERR(set);
1739 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1743 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
1747 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1754 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
1755 const struct nlmsghdr *nlh,
1756 const struct nlattr * const nla[])
1758 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1759 const struct nft_set_ops *ops;
1760 const struct nft_af_info *afi;
1761 struct nft_table *table;
1762 struct nft_set *set;
1764 char name[IFNAMSIZ];
1767 u32 ktype, klen, dlen, dtype, flags;
1770 if (nla[NFTA_SET_TABLE] == NULL ||
1771 nla[NFTA_SET_NAME] == NULL ||
1772 nla[NFTA_SET_KEY_LEN] == NULL)
1775 ktype = NFT_DATA_VALUE;
1776 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
1777 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
1778 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
1782 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
1783 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
1787 if (nla[NFTA_SET_FLAGS] != NULL) {
1788 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1789 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
1790 NFT_SET_INTERVAL | NFT_SET_MAP))
1796 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
1797 if (!(flags & NFT_SET_MAP))
1800 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
1801 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
1802 dtype != NFT_DATA_VERDICT)
1805 if (dtype != NFT_DATA_VERDICT) {
1806 if (nla[NFTA_SET_DATA_LEN] == NULL)
1808 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
1810 dlen > FIELD_SIZEOF(struct nft_data, data))
1813 dlen = sizeof(struct nft_data);
1814 } else if (flags & NFT_SET_MAP)
1817 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1819 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, create);
1821 return PTR_ERR(afi);
1823 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], create);
1825 return PTR_ERR(table);
1827 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL);
1829 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
1831 if (PTR_ERR(set) != -ENOENT)
1832 return PTR_ERR(set);
1837 if (nlh->nlmsg_flags & NLM_F_EXCL)
1839 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1844 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1847 ops = nft_select_set_ops(nla);
1849 return PTR_ERR(ops);
1852 if (ops->privsize != NULL)
1853 size = ops->privsize(nla);
1856 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
1860 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
1861 err = nf_tables_set_alloc_name(&ctx, set, name);
1865 INIT_LIST_HEAD(&set->bindings);
1873 err = ops->init(set, nla);
1877 list_add_tail(&set->list, &table->sets);
1878 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
1884 module_put(ops->owner);
1888 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
1890 list_del(&set->list);
1891 if (!(set->flags & NFT_SET_ANONYMOUS))
1892 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
1894 set->ops->destroy(set);
1895 module_put(set->ops->owner);
1899 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
1900 const struct nlmsghdr *nlh,
1901 const struct nlattr * const nla[])
1903 struct nft_set *set;
1907 if (nla[NFTA_SET_TABLE] == NULL)
1910 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
1914 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
1916 return PTR_ERR(set);
1917 if (!list_empty(&set->bindings))
1920 nf_tables_set_destroy(&ctx, set);
1924 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
1925 const struct nft_set *set,
1926 const struct nft_set_iter *iter,
1927 const struct nft_set_elem *elem)
1929 enum nft_registers dreg;
1931 dreg = nft_type_to_reg(set->dtype);
1932 return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
1935 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
1936 struct nft_set_binding *binding)
1938 struct nft_set_binding *i;
1939 struct nft_set_iter iter;
1941 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
1944 if (set->flags & NFT_SET_MAP) {
1945 /* If the set is already bound to the same chain all
1946 * jumps are already validated for that chain.
1948 list_for_each_entry(i, &set->bindings, list) {
1949 if (i->chain == binding->chain)
1956 iter.fn = nf_tables_bind_check_setelem;
1958 set->ops->walk(ctx, set, &iter);
1960 /* Destroy anonymous sets if binding fails */
1961 if (set->flags & NFT_SET_ANONYMOUS)
1962 nf_tables_set_destroy(ctx, set);
1968 binding->chain = ctx->chain;
1969 list_add_tail(&binding->list, &set->bindings);
1973 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
1974 struct nft_set_binding *binding)
1976 list_del(&binding->list);
1978 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
1979 nf_tables_set_destroy(ctx, set);
1986 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
1987 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
1988 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
1989 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
1992 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
1993 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
1994 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
1995 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
1998 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
1999 const struct sk_buff *skb,
2000 const struct nlmsghdr *nlh,
2001 const struct nlattr * const nla[])
2003 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2004 const struct nft_af_info *afi;
2005 const struct nft_table *table;
2007 afi = nf_tables_afinfo_lookup(nfmsg->nfgen_family, false);
2009 return PTR_ERR(afi);
2011 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE], false);
2013 return PTR_ERR(table);
2015 nft_ctx_init(ctx, skb, nlh, afi, table, NULL);
2019 static int nf_tables_fill_setelem(struct sk_buff *skb,
2020 const struct nft_set *set,
2021 const struct nft_set_elem *elem)
2023 unsigned char *b = skb_tail_pointer(skb);
2024 struct nlattr *nest;
2026 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2028 goto nla_put_failure;
2030 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2032 goto nla_put_failure;
2034 if (set->flags & NFT_SET_MAP &&
2035 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2036 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2037 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2039 goto nla_put_failure;
2041 if (elem->flags != 0)
2042 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2043 goto nla_put_failure;
2045 nla_nest_end(skb, nest);
2053 struct nft_set_dump_args {
2054 const struct netlink_callback *cb;
2055 struct nft_set_iter iter;
2056 struct sk_buff *skb;
2059 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2060 const struct nft_set *set,
2061 const struct nft_set_iter *iter,
2062 const struct nft_set_elem *elem)
2064 struct nft_set_dump_args *args;
2066 args = container_of(iter, struct nft_set_dump_args, iter);
2067 return nf_tables_fill_setelem(args->skb, set, elem);
2070 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2072 const struct nft_set *set;
2073 struct nft_set_dump_args args;
2075 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2076 struct nfgenmsg *nfmsg;
2077 struct nlmsghdr *nlh;
2078 struct nlattr *nest;
2082 nfmsg = nlmsg_data(cb->nlh);
2083 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2084 nft_set_elem_list_policy);
2088 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2092 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2094 return PTR_ERR(set);
2096 event = NFT_MSG_NEWSETELEM;
2097 event |= NFNL_SUBSYS_NFTABLES << 8;
2098 portid = NETLINK_CB(cb->skb).portid;
2099 seq = cb->nlh->nlmsg_seq;
2101 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2104 goto nla_put_failure;
2106 nfmsg = nlmsg_data(nlh);
2107 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2108 nfmsg->version = NFNETLINK_V0;
2111 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2112 goto nla_put_failure;
2113 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2114 goto nla_put_failure;
2116 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2118 goto nla_put_failure;
2122 args.iter.skip = cb->args[0];
2123 args.iter.count = 0;
2125 args.iter.fn = nf_tables_dump_setelem;
2126 set->ops->walk(&ctx, set, &args.iter);
2128 nla_nest_end(skb, nest);
2129 nlmsg_end(skb, nlh);
2131 if (args.iter.err && args.iter.err != -EMSGSIZE)
2132 return args.iter.err;
2133 if (args.iter.count == cb->args[0])
2136 cb->args[0] = args.iter.count;
2143 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2144 const struct nlmsghdr *nlh,
2145 const struct nlattr * const nla[])
2147 const struct nft_set *set;
2151 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2155 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2157 return PTR_ERR(set);
2159 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2160 struct netlink_dump_control c = {
2161 .dump = nf_tables_dump_set,
2163 return netlink_dump_start(nlsk, skb, nlh, &c);
2168 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2169 const struct nlattr *attr)
2171 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2172 struct nft_data_desc d1, d2;
2173 struct nft_set_elem elem;
2174 struct nft_set_binding *binding;
2175 enum nft_registers dreg;
2178 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2179 nft_set_elem_policy);
2183 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2187 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2188 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2189 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2193 if (set->flags & NFT_SET_MAP) {
2194 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2195 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2198 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2202 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2206 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2210 if (set->ops->get(set, &elem) == 0)
2213 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2214 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2219 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2222 dreg = nft_type_to_reg(set->dtype);
2223 list_for_each_entry(binding, &set->bindings, list) {
2224 struct nft_ctx bind_ctx = {
2226 .table = ctx->table,
2227 .chain = binding->chain,
2230 err = nft_validate_data_load(&bind_ctx, dreg,
2231 &elem.data, d2.type);
2237 err = set->ops->insert(set, &elem);
2244 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2245 nft_data_uninit(&elem.data, d2.type);
2247 nft_data_uninit(&elem.key, d1.type);
2252 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2253 const struct nlmsghdr *nlh,
2254 const struct nlattr * const nla[])
2256 const struct nlattr *attr;
2257 struct nft_set *set;
2261 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2265 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2267 return PTR_ERR(set);
2268 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2271 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2272 err = nft_add_set_elem(&ctx, set, attr);
2279 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2280 const struct nlattr *attr)
2282 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2283 struct nft_data_desc desc;
2284 struct nft_set_elem elem;
2287 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2288 nft_set_elem_policy);
2293 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2296 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2301 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2304 err = set->ops->get(set, &elem);
2308 set->ops->remove(set, &elem);
2310 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2311 if (set->flags & NFT_SET_MAP)
2312 nft_data_uninit(&elem.data, set->dtype);
2315 nft_data_uninit(&elem.key, desc.type);
2320 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2321 const struct nlmsghdr *nlh,
2322 const struct nlattr * const nla[])
2324 const struct nlattr *attr;
2325 struct nft_set *set;
2329 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2333 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2335 return PTR_ERR(set);
2336 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2339 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2340 err = nft_del_setelem(&ctx, set, attr);
2347 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2348 [NFT_MSG_NEWTABLE] = {
2349 .call = nf_tables_newtable,
2350 .attr_count = NFTA_TABLE_MAX,
2351 .policy = nft_table_policy,
2353 [NFT_MSG_GETTABLE] = {
2354 .call = nf_tables_gettable,
2355 .attr_count = NFTA_TABLE_MAX,
2356 .policy = nft_table_policy,
2358 [NFT_MSG_DELTABLE] = {
2359 .call = nf_tables_deltable,
2360 .attr_count = NFTA_TABLE_MAX,
2361 .policy = nft_table_policy,
2363 [NFT_MSG_NEWCHAIN] = {
2364 .call = nf_tables_newchain,
2365 .attr_count = NFTA_CHAIN_MAX,
2366 .policy = nft_chain_policy,
2368 [NFT_MSG_GETCHAIN] = {
2369 .call = nf_tables_getchain,
2370 .attr_count = NFTA_CHAIN_MAX,
2371 .policy = nft_chain_policy,
2373 [NFT_MSG_DELCHAIN] = {
2374 .call = nf_tables_delchain,
2375 .attr_count = NFTA_CHAIN_MAX,
2376 .policy = nft_chain_policy,
2378 [NFT_MSG_NEWRULE] = {
2379 .call = nf_tables_newrule,
2380 .attr_count = NFTA_RULE_MAX,
2381 .policy = nft_rule_policy,
2383 [NFT_MSG_GETRULE] = {
2384 .call = nf_tables_getrule,
2385 .attr_count = NFTA_RULE_MAX,
2386 .policy = nft_rule_policy,
2388 [NFT_MSG_DELRULE] = {
2389 .call = nf_tables_delrule,
2390 .attr_count = NFTA_RULE_MAX,
2391 .policy = nft_rule_policy,
2393 [NFT_MSG_NEWSET] = {
2394 .call = nf_tables_newset,
2395 .attr_count = NFTA_SET_MAX,
2396 .policy = nft_set_policy,
2398 [NFT_MSG_GETSET] = {
2399 .call = nf_tables_getset,
2400 .attr_count = NFTA_SET_MAX,
2401 .policy = nft_set_policy,
2403 [NFT_MSG_DELSET] = {
2404 .call = nf_tables_delset,
2405 .attr_count = NFTA_SET_MAX,
2406 .policy = nft_set_policy,
2408 [NFT_MSG_NEWSETELEM] = {
2409 .call = nf_tables_newsetelem,
2410 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2411 .policy = nft_set_elem_list_policy,
2413 [NFT_MSG_GETSETELEM] = {
2414 .call = nf_tables_getsetelem,
2415 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2416 .policy = nft_set_elem_list_policy,
2418 [NFT_MSG_DELSETELEM] = {
2419 .call = nf_tables_delsetelem,
2420 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2421 .policy = nft_set_elem_list_policy,
2425 static const struct nfnetlink_subsystem nf_tables_subsys = {
2426 .name = "nf_tables",
2427 .subsys_id = NFNL_SUBSYS_NFTABLES,
2428 .cb_count = NFT_MSG_MAX,
2433 * Loop detection - walk through the ruleset beginning at the destination chain
2434 * of a new jump until either the source chain is reached (loop) or all
2435 * reachable chains have been traversed.
2437 * The loop check is performed whenever a new jump verdict is added to an
2438 * expression or verdict map or a verdict map is bound to a new chain.
2441 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2442 const struct nft_chain *chain);
2444 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2445 const struct nft_set *set,
2446 const struct nft_set_iter *iter,
2447 const struct nft_set_elem *elem)
2449 switch (elem->data.verdict) {
2452 return nf_tables_check_loops(ctx, elem->data.chain);
2458 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2459 const struct nft_chain *chain)
2461 const struct nft_rule *rule;
2462 const struct nft_expr *expr, *last;
2463 const struct nft_data *data;
2464 const struct nft_set *set;
2465 struct nft_set_binding *binding;
2466 struct nft_set_iter iter;
2469 if (ctx->chain == chain)
2472 list_for_each_entry(rule, &chain->rules, list) {
2473 nft_rule_for_each_expr(expr, last, rule) {
2474 if (!expr->ops->get_verdict)
2477 data = expr->ops->get_verdict(expr);
2481 switch (data->verdict) {
2484 err = nf_tables_check_loops(ctx, data->chain);
2493 list_for_each_entry(set, &ctx->table->sets, list) {
2494 if (!(set->flags & NFT_SET_MAP) ||
2495 set->dtype != NFT_DATA_VERDICT)
2498 list_for_each_entry(binding, &set->bindings, list) {
2499 if (binding->chain != chain)
2505 iter.fn = nf_tables_loop_check_setelem;
2507 set->ops->walk(ctx, set, &iter);
2517 * nft_validate_input_register - validate an expressions' input register
2519 * @reg: the register number
2521 * Validate that the input register is one of the general purpose
2524 int nft_validate_input_register(enum nft_registers reg)
2526 if (reg <= NFT_REG_VERDICT)
2528 if (reg > NFT_REG_MAX)
2532 EXPORT_SYMBOL_GPL(nft_validate_input_register);
2535 * nft_validate_output_register - validate an expressions' output register
2537 * @reg: the register number
2539 * Validate that the output register is one of the general purpose
2540 * registers or the verdict register.
2542 int nft_validate_output_register(enum nft_registers reg)
2544 if (reg < NFT_REG_VERDICT)
2546 if (reg > NFT_REG_MAX)
2550 EXPORT_SYMBOL_GPL(nft_validate_output_register);
2553 * nft_validate_data_load - validate an expressions' data load
2555 * @ctx: context of the expression performing the load
2556 * @reg: the destination register number
2557 * @data: the data to load
2558 * @type: the data type
2560 * Validate that a data load uses the appropriate data type for
2561 * the destination register. A value of NULL for the data means
2562 * that its runtime gathered data, which is always of type
2565 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
2566 const struct nft_data *data,
2567 enum nft_data_types type)
2572 case NFT_REG_VERDICT:
2573 if (data == NULL || type != NFT_DATA_VERDICT)
2576 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
2577 err = nf_tables_check_loops(ctx, data->chain);
2581 if (ctx->chain->level + 1 > data->chain->level) {
2582 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
2584 data->chain->level = ctx->chain->level + 1;
2590 if (data != NULL && type != NFT_DATA_VALUE)
2595 EXPORT_SYMBOL_GPL(nft_validate_data_load);
2597 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
2598 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
2599 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
2600 .len = NFT_CHAIN_MAXNAMELEN - 1 },
2603 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
2604 struct nft_data_desc *desc, const struct nlattr *nla)
2606 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
2607 struct nft_chain *chain;
2610 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
2614 if (!tb[NFTA_VERDICT_CODE])
2616 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
2618 switch (data->verdict) {
2625 desc->len = sizeof(data->verdict);
2629 if (!tb[NFTA_VERDICT_CHAIN])
2631 chain = nf_tables_chain_lookup(ctx->table,
2632 tb[NFTA_VERDICT_CHAIN]);
2634 return PTR_ERR(chain);
2635 if (chain->flags & NFT_BASE_CHAIN)
2639 data->chain = chain;
2640 desc->len = sizeof(data);
2646 desc->type = NFT_DATA_VERDICT;
2650 static void nft_verdict_uninit(const struct nft_data *data)
2652 switch (data->verdict) {
2660 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
2662 struct nlattr *nest;
2664 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
2666 goto nla_put_failure;
2668 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
2669 goto nla_put_failure;
2671 switch (data->verdict) {
2674 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
2675 goto nla_put_failure;
2677 nla_nest_end(skb, nest);
2684 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
2685 struct nft_data_desc *desc, const struct nlattr *nla)
2692 if (len > sizeof(data->data))
2695 nla_memcpy(data->data, nla, sizeof(data->data));
2696 desc->type = NFT_DATA_VALUE;
2701 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
2704 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
2707 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
2708 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
2709 .len = FIELD_SIZEOF(struct nft_data, data) },
2710 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
2714 * nft_data_init - parse nf_tables data netlink attributes
2716 * @ctx: context of the expression using the data
2717 * @data: destination struct nft_data
2718 * @desc: data description
2719 * @nla: netlink attribute containing data
2721 * Parse the netlink data attributes and initialize a struct nft_data.
2722 * The type and length of data are returned in the data description.
2724 * The caller can indicate that it only wants to accept data of type
2725 * NFT_DATA_VALUE by passing NULL for the ctx argument.
2727 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
2728 struct nft_data_desc *desc, const struct nlattr *nla)
2730 struct nlattr *tb[NFTA_DATA_MAX + 1];
2733 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
2737 if (tb[NFTA_DATA_VALUE])
2738 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
2739 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
2740 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
2743 EXPORT_SYMBOL_GPL(nft_data_init);
2746 * nft_data_uninit - release a nft_data item
2748 * @data: struct nft_data to release
2749 * @type: type of data
2751 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
2752 * all others need to be released by calling this function.
2754 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
2757 case NFT_DATA_VALUE:
2759 case NFT_DATA_VERDICT:
2760 return nft_verdict_uninit(data);
2765 EXPORT_SYMBOL_GPL(nft_data_uninit);
2767 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
2768 enum nft_data_types type, unsigned int len)
2770 struct nlattr *nest;
2773 nest = nla_nest_start(skb, attr);
2778 case NFT_DATA_VALUE:
2779 err = nft_value_dump(skb, data, len);
2781 case NFT_DATA_VERDICT:
2782 err = nft_verdict_dump(skb, data);
2789 nla_nest_end(skb, nest);
2792 EXPORT_SYMBOL_GPL(nft_data_dump);
2794 static int __init nf_tables_module_init(void)
2798 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
2805 err = nf_tables_core_module_init();
2809 err = nfnetlink_subsys_register(&nf_tables_subsys);
2813 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
2816 nf_tables_core_module_exit();
2823 static void __exit nf_tables_module_exit(void)
2825 nfnetlink_subsys_unregister(&nf_tables_subsys);
2826 nf_tables_core_module_exit();
2830 module_init(nf_tables_module_init);
2831 module_exit(nf_tables_module_exit);
2833 MODULE_LICENSE("GPL");
2834 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
2835 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);