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>
21 #include <net/net_namespace.h>
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 net *net, struct nft_af_info *afi)
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail(&afi->list, &net->nft.af_info);
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(struct net *net, int family)
61 struct nft_af_info *afi;
63 list_for_each_entry(afi, &net->nft.af_info, list) {
64 if (afi->family == family)
70 static struct nft_af_info *
71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
73 struct nft_af_info *afi;
75 afi = nft_afinfo_lookup(net, family);
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);
85 return ERR_PTR(-EAGAIN);
88 return ERR_PTR(-EAFNOSUPPORT);
95 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96 const struct nlattr *nla)
98 struct nft_table *table;
100 list_for_each_entry(table, &afi->tables, list) {
101 if (!nla_strcmp(nla, table->name))
107 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
108 const struct nlattr *nla)
110 struct nft_table *table;
113 return ERR_PTR(-EINVAL);
115 table = nft_table_lookup(afi, nla);
119 return ERR_PTR(-ENOENT);
122 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
124 return ++table->hgenerator;
127 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
129 static const struct nf_chain_type *
130 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
134 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
135 if (chain_type[family][i] != NULL &&
136 !nla_strcmp(nla, chain_type[family][i]->name))
137 return chain_type[family][i];
142 static const struct nf_chain_type *
143 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
144 const struct nlattr *nla,
147 const struct nf_chain_type *type;
149 type = __nf_tables_chain_type_lookup(afi->family, nla);
152 #ifdef CONFIG_MODULES
154 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
155 request_module("nft-chain-%u-%*.s", afi->family,
156 nla_len(nla)-1, (const char *)nla_data(nla));
157 nfnl_lock(NFNL_SUBSYS_NFTABLES);
158 type = __nf_tables_chain_type_lookup(afi->family, nla);
160 return ERR_PTR(-EAGAIN);
163 return ERR_PTR(-ENOENT);
166 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
167 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
168 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
171 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
172 int event, u32 flags, int family,
173 const struct nft_table *table)
175 struct nlmsghdr *nlh;
176 struct nfgenmsg *nfmsg;
178 event |= NFNL_SUBSYS_NFTABLES << 8;
179 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
181 goto nla_put_failure;
183 nfmsg = nlmsg_data(nlh);
184 nfmsg->nfgen_family = family;
185 nfmsg->version = NFNETLINK_V0;
188 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
189 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
190 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
191 goto nla_put_failure;
193 return nlmsg_end(skb, nlh);
196 nlmsg_trim(skb, nlh);
200 static int nf_tables_table_notify(const struct sk_buff *oskb,
201 const struct nlmsghdr *nlh,
202 const struct nft_table *table,
203 int event, int family)
206 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
207 u32 seq = nlh ? nlh->nlmsg_seq : 0;
208 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
212 report = nlh ? nlmsg_report(nlh) : false;
213 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
217 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
221 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
228 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
232 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
236 static int nf_tables_dump_tables(struct sk_buff *skb,
237 struct netlink_callback *cb)
239 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
240 const struct nft_af_info *afi;
241 const struct nft_table *table;
242 unsigned int idx = 0, s_idx = cb->args[0];
243 struct net *net = sock_net(skb->sk);
244 int family = nfmsg->nfgen_family;
246 list_for_each_entry(afi, &net->nft.af_info, list) {
247 if (family != NFPROTO_UNSPEC && family != afi->family)
250 list_for_each_entry(table, &afi->tables, list) {
254 memset(&cb->args[1], 0,
255 sizeof(cb->args) - sizeof(cb->args[0]));
256 if (nf_tables_fill_table_info(skb,
257 NETLINK_CB(cb->skb).portid,
261 afi->family, table) < 0)
272 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
273 const struct nlmsghdr *nlh,
274 const struct nlattr * const nla[])
276 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
277 const struct nft_af_info *afi;
278 const struct nft_table *table;
279 struct sk_buff *skb2;
280 struct net *net = sock_net(skb->sk);
281 int family = nfmsg->nfgen_family;
284 if (nlh->nlmsg_flags & NLM_F_DUMP) {
285 struct netlink_dump_control c = {
286 .dump = nf_tables_dump_tables,
288 return netlink_dump_start(nlsk, skb, nlh, &c);
291 afi = nf_tables_afinfo_lookup(net, family, false);
295 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
297 return PTR_ERR(table);
299 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
303 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
304 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
309 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
316 static int nf_tables_table_enable(const struct nft_af_info *afi,
317 struct nft_table *table)
319 struct nft_chain *chain;
322 list_for_each_entry(chain, &table->chains, list) {
323 if (!(chain->flags & NFT_BASE_CHAIN))
326 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
334 list_for_each_entry(chain, &table->chains, list) {
335 if (!(chain->flags & NFT_BASE_CHAIN))
341 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
346 static int nf_tables_table_disable(const struct nft_af_info *afi,
347 struct nft_table *table)
349 struct nft_chain *chain;
351 list_for_each_entry(chain, &table->chains, list) {
352 if (chain->flags & NFT_BASE_CHAIN)
353 nf_unregister_hooks(nft_base_chain(chain)->ops,
360 static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
361 const struct nlmsghdr *nlh,
362 const struct nlattr * const nla[],
363 struct nft_af_info *afi, struct nft_table *table)
365 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
366 int family = nfmsg->nfgen_family, ret = 0;
368 if (nla[NFTA_TABLE_FLAGS]) {
371 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
372 if (flags & ~NFT_TABLE_F_DORMANT)
375 if ((flags & NFT_TABLE_F_DORMANT) &&
376 !(table->flags & NFT_TABLE_F_DORMANT)) {
377 ret = nf_tables_table_disable(afi, table);
379 table->flags |= NFT_TABLE_F_DORMANT;
380 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
381 table->flags & NFT_TABLE_F_DORMANT) {
382 ret = nf_tables_table_enable(afi, table);
384 table->flags &= ~NFT_TABLE_F_DORMANT;
390 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
395 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
396 const struct nlmsghdr *nlh,
397 const struct nlattr * const nla[])
399 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
400 const struct nlattr *name;
401 struct nft_af_info *afi;
402 struct nft_table *table;
403 struct net *net = sock_net(skb->sk);
404 int family = nfmsg->nfgen_family;
407 afi = nf_tables_afinfo_lookup(net, family, true);
411 name = nla[NFTA_TABLE_NAME];
412 table = nf_tables_table_lookup(afi, name);
414 if (PTR_ERR(table) != -ENOENT)
415 return PTR_ERR(table);
420 if (nlh->nlmsg_flags & NLM_F_EXCL)
422 if (nlh->nlmsg_flags & NLM_F_REPLACE)
424 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
427 if (nla[NFTA_TABLE_FLAGS]) {
428 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
429 if (flags & ~NFT_TABLE_F_DORMANT)
433 if (!try_module_get(afi->owner))
434 return -EAFNOSUPPORT;
436 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
438 module_put(afi->owner);
442 nla_strlcpy(table->name, name, nla_len(name));
443 INIT_LIST_HEAD(&table->chains);
444 INIT_LIST_HEAD(&table->sets);
445 table->flags = flags;
447 list_add_tail(&table->list, &afi->tables);
448 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
452 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
453 const struct nlmsghdr *nlh,
454 const struct nlattr * const nla[])
456 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
457 struct nft_af_info *afi;
458 struct nft_table *table;
459 struct net *net = sock_net(skb->sk);
460 int family = nfmsg->nfgen_family;
462 afi = nf_tables_afinfo_lookup(net, family, false);
466 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
468 return PTR_ERR(table);
470 if (!list_empty(&table->chains) || !list_empty(&table->sets))
473 list_del(&table->list);
474 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
476 module_put(afi->owner);
480 int nft_register_chain_type(const struct nf_chain_type *ctype)
484 nfnl_lock(NFNL_SUBSYS_NFTABLES);
485 if (chain_type[ctype->family][ctype->type] != NULL) {
489 chain_type[ctype->family][ctype->type] = ctype;
491 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
494 EXPORT_SYMBOL_GPL(nft_register_chain_type);
496 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
498 nfnl_lock(NFNL_SUBSYS_NFTABLES);
499 chain_type[ctype->family][ctype->type] = NULL;
500 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
502 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
508 static struct nft_chain *
509 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
511 struct nft_chain *chain;
513 list_for_each_entry(chain, &table->chains, list) {
514 if (chain->handle == handle)
518 return ERR_PTR(-ENOENT);
521 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
522 const struct nlattr *nla)
524 struct nft_chain *chain;
527 return ERR_PTR(-EINVAL);
529 list_for_each_entry(chain, &table->chains, list) {
530 if (!nla_strcmp(nla, chain->name))
534 return ERR_PTR(-ENOENT);
537 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
538 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
539 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
540 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
541 .len = NFT_CHAIN_MAXNAMELEN - 1 },
542 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
543 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
544 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
545 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
548 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
549 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
550 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
553 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
555 struct nft_stats *cpu_stats, total;
559 memset(&total, 0, sizeof(total));
560 for_each_possible_cpu(cpu) {
561 cpu_stats = per_cpu_ptr(stats, cpu);
562 total.pkts += cpu_stats->pkts;
563 total.bytes += cpu_stats->bytes;
565 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
567 goto nla_put_failure;
569 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
570 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
571 goto nla_put_failure;
573 nla_nest_end(skb, nest);
580 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
581 int event, u32 flags, int family,
582 const struct nft_table *table,
583 const struct nft_chain *chain)
585 struct nlmsghdr *nlh;
586 struct nfgenmsg *nfmsg;
588 event |= NFNL_SUBSYS_NFTABLES << 8;
589 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
591 goto nla_put_failure;
593 nfmsg = nlmsg_data(nlh);
594 nfmsg->nfgen_family = family;
595 nfmsg->version = NFNETLINK_V0;
598 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
599 goto nla_put_failure;
600 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
601 goto nla_put_failure;
602 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
603 goto nla_put_failure;
605 if (chain->flags & NFT_BASE_CHAIN) {
606 const struct nft_base_chain *basechain = nft_base_chain(chain);
607 const struct nf_hook_ops *ops = &basechain->ops[0];
610 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
612 goto nla_put_failure;
613 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
614 goto nla_put_failure;
615 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
616 goto nla_put_failure;
617 nla_nest_end(skb, nest);
619 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
620 htonl(basechain->policy)))
621 goto nla_put_failure;
623 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
624 goto nla_put_failure;
626 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
627 goto nla_put_failure;
630 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
631 goto nla_put_failure;
633 return nlmsg_end(skb, nlh);
636 nlmsg_trim(skb, nlh);
640 static int nf_tables_chain_notify(const struct sk_buff *oskb,
641 const struct nlmsghdr *nlh,
642 const struct nft_table *table,
643 const struct nft_chain *chain,
644 int event, int family)
647 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
648 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
649 u32 seq = nlh ? nlh->nlmsg_seq : 0;
653 report = nlh ? nlmsg_report(nlh) : false;
654 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
658 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
662 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
669 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
673 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
677 static int nf_tables_dump_chains(struct sk_buff *skb,
678 struct netlink_callback *cb)
680 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
681 const struct nft_af_info *afi;
682 const struct nft_table *table;
683 const struct nft_chain *chain;
684 unsigned int idx = 0, s_idx = cb->args[0];
685 struct net *net = sock_net(skb->sk);
686 int family = nfmsg->nfgen_family;
688 list_for_each_entry(afi, &net->nft.af_info, list) {
689 if (family != NFPROTO_UNSPEC && family != afi->family)
692 list_for_each_entry(table, &afi->tables, list) {
693 list_for_each_entry(chain, &table->chains, list) {
697 memset(&cb->args[1], 0,
698 sizeof(cb->args) - sizeof(cb->args[0]));
699 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
703 afi->family, table, chain) < 0)
716 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
717 const struct nlmsghdr *nlh,
718 const struct nlattr * const nla[])
720 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
721 const struct nft_af_info *afi;
722 const struct nft_table *table;
723 const struct nft_chain *chain;
724 struct sk_buff *skb2;
725 struct net *net = sock_net(skb->sk);
726 int family = nfmsg->nfgen_family;
729 if (nlh->nlmsg_flags & NLM_F_DUMP) {
730 struct netlink_dump_control c = {
731 .dump = nf_tables_dump_chains,
733 return netlink_dump_start(nlsk, skb, nlh, &c);
736 afi = nf_tables_afinfo_lookup(net, family, false);
740 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
742 return PTR_ERR(table);
744 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
746 return PTR_ERR(chain);
748 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
752 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
753 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
754 family, table, chain);
758 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
765 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
766 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
767 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
771 nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
773 struct nlattr *tb[NFTA_COUNTER_MAX+1];
774 struct nft_stats __percpu *newstats;
775 struct nft_stats *stats;
778 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
782 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
785 newstats = alloc_percpu(struct nft_stats);
786 if (newstats == NULL)
789 /* Restore old counters on this cpu, no problem. Per-cpu statistics
790 * are not exposed to userspace.
792 stats = this_cpu_ptr(newstats);
793 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
794 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
797 /* nfnl_lock is held, add some nfnl function for this, later */
798 struct nft_stats __percpu *oldstats =
799 rcu_dereference_protected(chain->stats, 1);
801 rcu_assign_pointer(chain->stats, newstats);
803 free_percpu(oldstats);
805 rcu_assign_pointer(chain->stats, newstats);
810 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
811 const struct nlmsghdr *nlh,
812 const struct nlattr * const nla[])
814 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
815 const struct nlattr * uninitialized_var(name);
816 const struct nft_af_info *afi;
817 struct nft_table *table;
818 struct nft_chain *chain;
819 struct nft_base_chain *basechain = NULL;
820 struct nlattr *ha[NFTA_HOOK_MAX + 1];
821 struct net *net = sock_net(skb->sk);
822 int family = nfmsg->nfgen_family;
823 u8 policy = NF_ACCEPT;
829 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
831 afi = nf_tables_afinfo_lookup(net, family, true);
835 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
837 return PTR_ERR(table);
840 name = nla[NFTA_CHAIN_NAME];
842 if (nla[NFTA_CHAIN_HANDLE]) {
843 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
844 chain = nf_tables_chain_lookup_byhandle(table, handle);
846 return PTR_ERR(chain);
848 chain = nf_tables_chain_lookup(table, name);
850 if (PTR_ERR(chain) != -ENOENT)
851 return PTR_ERR(chain);
856 if (nla[NFTA_CHAIN_POLICY]) {
857 if ((chain != NULL &&
858 !(chain->flags & NFT_BASE_CHAIN)) ||
859 nla[NFTA_CHAIN_HOOK] == NULL)
862 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
873 if (nlh->nlmsg_flags & NLM_F_EXCL)
875 if (nlh->nlmsg_flags & NLM_F_REPLACE)
878 if (nla[NFTA_CHAIN_HANDLE] && name &&
879 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
882 if (nla[NFTA_CHAIN_COUNTERS]) {
883 if (!(chain->flags & NFT_BASE_CHAIN))
886 err = nf_tables_counters(nft_base_chain(chain),
887 nla[NFTA_CHAIN_COUNTERS]);
892 if (nla[NFTA_CHAIN_POLICY])
893 nft_base_chain(chain)->policy = policy;
895 if (nla[NFTA_CHAIN_HANDLE] && name)
896 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
901 if (table->use == UINT_MAX)
904 if (nla[NFTA_CHAIN_HOOK]) {
905 const struct nf_chain_type *type;
906 struct nf_hook_ops *ops;
908 u32 hooknum, priority;
910 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
911 if (nla[NFTA_CHAIN_TYPE]) {
912 type = nf_tables_chain_type_lookup(afi,
913 nla[NFTA_CHAIN_TYPE],
916 return PTR_ERR(type);
919 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
923 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
924 ha[NFTA_HOOK_PRIORITY] == NULL)
927 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
928 if (hooknum >= afi->nhooks)
930 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
932 if (!(type->hook_mask & (1 << hooknum)))
934 if (!try_module_get(type->owner))
936 hookfn = type->hooks[hooknum];
938 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
939 if (basechain == NULL)
942 if (nla[NFTA_CHAIN_COUNTERS]) {
943 err = nf_tables_counters(basechain,
944 nla[NFTA_CHAIN_COUNTERS]);
946 module_put(type->owner);
951 struct nft_stats __percpu *newstats;
953 newstats = alloc_percpu(struct nft_stats);
954 if (newstats == NULL) {
955 module_put(type->owner);
959 rcu_assign_pointer(basechain->stats, newstats);
962 basechain->type = type;
963 chain = &basechain->chain;
965 for (i = 0; i < afi->nops; i++) {
966 ops = &basechain->ops[i];
968 ops->owner = afi->owner;
969 ops->hooknum = hooknum;
970 ops->priority = priority;
972 ops->hook = afi->hooks[ops->hooknum];
975 if (afi->hook_ops_init)
976 afi->hook_ops_init(ops, i);
979 chain->flags |= NFT_BASE_CHAIN;
980 basechain->policy = policy;
982 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
987 INIT_LIST_HEAD(&chain->rules);
988 chain->handle = nf_tables_alloc_handle(table);
990 chain->table = table;
991 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
993 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
994 chain->flags & NFT_BASE_CHAIN) {
995 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
997 module_put(basechain->type->owner);
998 free_percpu(basechain->stats);
1003 list_add_tail(&chain->list, &table->chains);
1006 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1011 static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1013 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1015 BUG_ON(chain->use > 0);
1017 if (chain->flags & NFT_BASE_CHAIN) {
1018 module_put(nft_base_chain(chain)->type->owner);
1019 free_percpu(nft_base_chain(chain)->stats);
1020 kfree(nft_base_chain(chain));
1025 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1026 const struct nlmsghdr *nlh,
1027 const struct nlattr * const nla[])
1029 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1030 const struct nft_af_info *afi;
1031 struct nft_table *table;
1032 struct nft_chain *chain;
1033 struct net *net = sock_net(skb->sk);
1034 int family = nfmsg->nfgen_family;
1036 afi = nf_tables_afinfo_lookup(net, family, false);
1038 return PTR_ERR(afi);
1040 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1042 return PTR_ERR(table);
1044 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1046 return PTR_ERR(chain);
1048 if (!list_empty(&chain->rules) || chain->use > 0)
1051 list_del(&chain->list);
1054 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1055 chain->flags & NFT_BASE_CHAIN)
1056 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
1058 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1061 /* Make sure all rule references are gone before this is released */
1062 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1066 static void nft_ctx_init(struct nft_ctx *ctx,
1067 const struct sk_buff *skb,
1068 const struct nlmsghdr *nlh,
1069 const struct nft_af_info *afi,
1070 const struct nft_table *table,
1071 const struct nft_chain *chain,
1072 const struct nlattr * const *nla)
1074 ctx->net = sock_net(skb->sk);
1088 * nft_register_expr - register nf_tables expr type
1091 * Registers the expr type for use with nf_tables. Returns zero on
1092 * success or a negative errno code otherwise.
1094 int nft_register_expr(struct nft_expr_type *type)
1096 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1097 list_add_tail(&type->list, &nf_tables_expressions);
1098 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1101 EXPORT_SYMBOL_GPL(nft_register_expr);
1104 * nft_unregister_expr - unregister nf_tables expr type
1107 * Unregisters the expr typefor use with nf_tables.
1109 void nft_unregister_expr(struct nft_expr_type *type)
1111 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1112 list_del(&type->list);
1113 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1115 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1117 static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
1119 const struct nft_expr_type *type;
1121 list_for_each_entry(type, &nf_tables_expressions, list) {
1122 if (!nla_strcmp(nla, type->name))
1128 static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
1130 const struct nft_expr_type *type;
1133 return ERR_PTR(-EINVAL);
1135 type = __nft_expr_type_get(nla);
1136 if (type != NULL && try_module_get(type->owner))
1139 #ifdef CONFIG_MODULES
1141 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1142 request_module("nft-expr-%.*s",
1143 nla_len(nla), (char *)nla_data(nla));
1144 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1145 if (__nft_expr_type_get(nla))
1146 return ERR_PTR(-EAGAIN);
1149 return ERR_PTR(-ENOENT);
1152 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1153 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1154 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1157 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1158 const struct nft_expr *expr)
1160 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1161 goto nla_put_failure;
1163 if (expr->ops->dump) {
1164 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1166 goto nla_put_failure;
1167 if (expr->ops->dump(skb, expr) < 0)
1168 goto nla_put_failure;
1169 nla_nest_end(skb, data);
1178 struct nft_expr_info {
1179 const struct nft_expr_ops *ops;
1180 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1183 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1184 const struct nlattr *nla,
1185 struct nft_expr_info *info)
1187 const struct nft_expr_type *type;
1188 const struct nft_expr_ops *ops;
1189 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1192 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1196 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1198 return PTR_ERR(type);
1200 if (tb[NFTA_EXPR_DATA]) {
1201 err = nla_parse_nested(info->tb, type->maxattr,
1202 tb[NFTA_EXPR_DATA], type->policy);
1206 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1208 if (type->select_ops != NULL) {
1209 ops = type->select_ops(ctx,
1210 (const struct nlattr * const *)info->tb);
1222 module_put(type->owner);
1226 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1227 const struct nft_expr_info *info,
1228 struct nft_expr *expr)
1230 const struct nft_expr_ops *ops = info->ops;
1235 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1247 static void nf_tables_expr_destroy(struct nft_expr *expr)
1249 if (expr->ops->destroy)
1250 expr->ops->destroy(expr);
1251 module_put(expr->ops->type->owner);
1258 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1261 struct nft_rule *rule;
1263 // FIXME: this sucks
1264 list_for_each_entry(rule, &chain->rules, list) {
1265 if (handle == rule->handle)
1269 return ERR_PTR(-ENOENT);
1272 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1273 const struct nlattr *nla)
1276 return ERR_PTR(-EINVAL);
1278 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1281 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1282 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1283 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1284 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1285 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1286 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1287 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1288 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1291 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1292 int event, u32 flags, int family,
1293 const struct nft_table *table,
1294 const struct nft_chain *chain,
1295 const struct nft_rule *rule)
1297 struct nlmsghdr *nlh;
1298 struct nfgenmsg *nfmsg;
1299 const struct nft_expr *expr, *next;
1300 struct nlattr *list;
1301 const struct nft_rule *prule;
1302 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1304 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1307 goto nla_put_failure;
1309 nfmsg = nlmsg_data(nlh);
1310 nfmsg->nfgen_family = family;
1311 nfmsg->version = NFNETLINK_V0;
1314 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1315 goto nla_put_failure;
1316 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1317 goto nla_put_failure;
1318 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1319 goto nla_put_failure;
1321 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1322 prule = list_entry(rule->list.prev, struct nft_rule, list);
1323 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1324 cpu_to_be64(prule->handle)))
1325 goto nla_put_failure;
1328 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1330 goto nla_put_failure;
1331 nft_rule_for_each_expr(expr, next, rule) {
1332 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1334 goto nla_put_failure;
1335 if (nf_tables_fill_expr_info(skb, expr) < 0)
1336 goto nla_put_failure;
1337 nla_nest_end(skb, elem);
1339 nla_nest_end(skb, list);
1341 return nlmsg_end(skb, nlh);
1344 nlmsg_trim(skb, nlh);
1348 static int nf_tables_rule_notify(const struct sk_buff *oskb,
1349 const struct nlmsghdr *nlh,
1350 const struct nft_table *table,
1351 const struct nft_chain *chain,
1352 const struct nft_rule *rule,
1353 int event, u32 flags, int family)
1355 struct sk_buff *skb;
1356 u32 portid = NETLINK_CB(oskb).portid;
1357 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1358 u32 seq = nlh->nlmsg_seq;
1362 report = nlmsg_report(nlh);
1363 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1367 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1371 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1372 family, table, chain, rule);
1378 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1382 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1387 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1389 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1392 static inline int gencursor_next(struct net *net)
1394 return net->nft.gencursor+1 == 1 ? 1 : 0;
1398 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1400 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1404 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1406 /* Now inactive, will be active in the future */
1407 rule->genmask = (1 << net->nft.gencursor);
1411 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1413 rule->genmask = (1 << gencursor_next(net));
1416 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1421 static int nf_tables_dump_rules(struct sk_buff *skb,
1422 struct netlink_callback *cb)
1424 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1425 const struct nft_af_info *afi;
1426 const struct nft_table *table;
1427 const struct nft_chain *chain;
1428 const struct nft_rule *rule;
1429 unsigned int idx = 0, s_idx = cb->args[0];
1430 struct net *net = sock_net(skb->sk);
1431 int family = nfmsg->nfgen_family;
1432 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1433 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1435 list_for_each_entry(afi, &net->nft.af_info, list) {
1436 if (family != NFPROTO_UNSPEC && family != afi->family)
1439 list_for_each_entry(table, &afi->tables, list) {
1440 list_for_each_entry(chain, &table->chains, list) {
1441 list_for_each_entry(rule, &chain->rules, list) {
1442 if (!nft_rule_is_active(net, rule))
1447 memset(&cb->args[1], 0,
1448 sizeof(cb->args) - sizeof(cb->args[0]));
1449 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1452 NLM_F_MULTI | NLM_F_APPEND,
1453 afi->family, table, chain, rule) < 0)
1462 /* Invalidate this dump, a transition to the new generation happened */
1463 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1470 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1471 const struct nlmsghdr *nlh,
1472 const struct nlattr * const nla[])
1474 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1475 const struct nft_af_info *afi;
1476 const struct nft_table *table;
1477 const struct nft_chain *chain;
1478 const struct nft_rule *rule;
1479 struct sk_buff *skb2;
1480 struct net *net = sock_net(skb->sk);
1481 int family = nfmsg->nfgen_family;
1484 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1485 struct netlink_dump_control c = {
1486 .dump = nf_tables_dump_rules,
1488 return netlink_dump_start(nlsk, skb, nlh, &c);
1491 afi = nf_tables_afinfo_lookup(net, family, false);
1493 return PTR_ERR(afi);
1495 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1497 return PTR_ERR(table);
1499 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1501 return PTR_ERR(chain);
1503 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1505 return PTR_ERR(rule);
1507 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1511 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1512 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1513 family, table, chain, rule);
1517 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1524 static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1526 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1527 struct nft_expr *expr;
1530 * Careful: some expressions might not be initialized in case this
1531 * is called on error from nf_tables_newrule().
1533 expr = nft_expr_first(rule);
1534 while (expr->ops && expr != nft_expr_last(rule)) {
1535 nf_tables_expr_destroy(expr);
1536 expr = nft_expr_next(expr);
1541 static void nf_tables_rule_destroy(struct nft_rule *rule)
1543 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1546 #define NFT_RULE_MAXEXPRS 128
1548 static struct nft_expr_info *info;
1550 static struct nft_rule_trans *
1551 nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1553 struct nft_rule_trans *rupd;
1555 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1559 rupd->chain = ctx->chain;
1560 rupd->table = ctx->table;
1562 rupd->family = ctx->afi->family;
1563 rupd->nlh = ctx->nlh;
1564 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1569 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1570 const struct nlmsghdr *nlh,
1571 const struct nlattr * const nla[])
1573 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1574 const struct nft_af_info *afi;
1575 struct net *net = sock_net(skb->sk);
1576 struct nft_table *table;
1577 struct nft_chain *chain;
1578 struct nft_rule *rule, *old_rule = NULL;
1579 struct nft_rule_trans *repl = NULL;
1580 struct nft_expr *expr;
1583 unsigned int size, i, n;
1586 u64 handle, pos_handle;
1588 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1590 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1592 return PTR_ERR(afi);
1594 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1596 return PTR_ERR(table);
1598 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1600 return PTR_ERR(chain);
1602 if (nla[NFTA_RULE_HANDLE]) {
1603 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1604 rule = __nf_tables_rule_lookup(chain, handle);
1606 return PTR_ERR(rule);
1608 if (nlh->nlmsg_flags & NLM_F_EXCL)
1610 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1615 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1617 handle = nf_tables_alloc_handle(table);
1620 if (nla[NFTA_RULE_POSITION]) {
1621 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1624 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1625 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1626 if (IS_ERR(old_rule))
1627 return PTR_ERR(old_rule);
1630 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1634 if (nla[NFTA_RULE_EXPRESSIONS]) {
1635 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1637 if (nla_type(tmp) != NFTA_LIST_ELEM)
1639 if (n == NFT_RULE_MAXEXPRS)
1641 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1644 size += info[n].ops->size;
1650 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1654 nft_rule_activate_next(net, rule);
1656 rule->handle = handle;
1659 expr = nft_expr_first(rule);
1660 for (i = 0; i < n; i++) {
1661 err = nf_tables_newexpr(&ctx, &info[i], expr);
1665 expr = nft_expr_next(expr);
1668 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1669 if (nft_rule_is_active_next(net, old_rule)) {
1670 repl = nf_tables_trans_add(old_rule, &ctx);
1675 nft_rule_disactivate_next(net, old_rule);
1676 list_add_tail(&rule->list, &old_rule->list);
1681 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1683 list_add_rcu(&rule->list, &old_rule->list);
1685 list_add_tail_rcu(&rule->list, &chain->rules);
1688 list_add_tail_rcu(&rule->list, &old_rule->list);
1690 list_add_rcu(&rule->list, &chain->rules);
1693 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1700 list_del_rcu(&rule->list);
1702 list_del_rcu(&repl->rule->list);
1703 list_del(&repl->list);
1704 nft_rule_clear(net, repl->rule);
1708 nf_tables_rule_destroy(rule);
1710 for (i = 0; i < n; i++) {
1711 if (info[i].ops != NULL)
1712 module_put(info[i].ops->type->owner);
1718 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1720 /* You cannot delete the same rule twice */
1721 if (nft_rule_is_active_next(ctx->net, rule)) {
1722 if (nf_tables_trans_add(rule, ctx) == NULL)
1724 nft_rule_disactivate_next(ctx->net, rule);
1730 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1732 struct nft_rule *rule;
1735 list_for_each_entry(rule, &ctx->chain->rules, list) {
1736 err = nf_tables_delrule_one(ctx, rule);
1743 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1744 const struct nlmsghdr *nlh,
1745 const struct nlattr * const nla[])
1747 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1748 const struct nft_af_info *afi;
1749 struct net *net = sock_net(skb->sk);
1750 const struct nft_table *table;
1751 struct nft_chain *chain = NULL;
1752 struct nft_rule *rule;
1753 int family = nfmsg->nfgen_family, err = 0;
1756 afi = nf_tables_afinfo_lookup(net, family, false);
1758 return PTR_ERR(afi);
1760 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1762 return PTR_ERR(table);
1764 if (nla[NFTA_RULE_CHAIN]) {
1765 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1767 return PTR_ERR(chain);
1770 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1773 if (nla[NFTA_RULE_HANDLE]) {
1774 rule = nf_tables_rule_lookup(chain,
1775 nla[NFTA_RULE_HANDLE]);
1777 return PTR_ERR(rule);
1779 err = nf_tables_delrule_one(&ctx, rule);
1781 err = nf_table_delrule_by_chain(&ctx);
1784 list_for_each_entry(chain, &table->chains, list) {
1786 err = nf_table_delrule_by_chain(&ctx);
1795 static int nf_tables_commit(struct sk_buff *skb)
1797 struct net *net = sock_net(skb->sk);
1798 struct nft_rule_trans *rupd, *tmp;
1800 /* Bump generation counter, invalidate any dump in progress */
1803 /* A new generation has just started */
1804 net->nft.gencursor = gencursor_next(net);
1806 /* Make sure all packets have left the previous generation before
1807 * purging old rules.
1811 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1812 /* Delete this rule from the dirty list */
1813 list_del(&rupd->list);
1815 /* This rule was inactive in the past and just became active.
1816 * Clear the next bit of the genmask since its meaning has
1817 * changed, now it is the future.
1819 if (nft_rule_is_active(net, rupd->rule)) {
1820 nft_rule_clear(net, rupd->rule);
1821 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1822 rupd->chain, rupd->rule,
1829 /* This rule is in the past, get rid of it */
1830 list_del_rcu(&rupd->rule->list);
1831 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1832 rupd->rule, NFT_MSG_DELRULE, 0,
1834 nf_tables_rule_destroy(rupd->rule);
1841 static int nf_tables_abort(struct sk_buff *skb)
1843 struct net *net = sock_net(skb->sk);
1844 struct nft_rule_trans *rupd, *tmp;
1846 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1847 /* Delete all rules from the dirty list */
1848 list_del(&rupd->list);
1850 if (!nft_rule_is_active_next(net, rupd->rule)) {
1851 nft_rule_clear(net, rupd->rule);
1856 /* This rule is inactive, get rid of it */
1857 list_del_rcu(&rupd->rule->list);
1858 nf_tables_rule_destroy(rupd->rule);
1868 static LIST_HEAD(nf_tables_set_ops);
1870 int nft_register_set(struct nft_set_ops *ops)
1872 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1873 list_add_tail(&ops->list, &nf_tables_set_ops);
1874 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1877 EXPORT_SYMBOL_GPL(nft_register_set);
1879 void nft_unregister_set(struct nft_set_ops *ops)
1881 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1882 list_del(&ops->list);
1883 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1885 EXPORT_SYMBOL_GPL(nft_unregister_set);
1887 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1889 const struct nft_set_ops *ops;
1892 #ifdef CONFIG_MODULES
1893 if (list_empty(&nf_tables_set_ops)) {
1894 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1895 request_module("nft-set");
1896 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1897 if (!list_empty(&nf_tables_set_ops))
1898 return ERR_PTR(-EAGAIN);
1902 if (nla[NFTA_SET_FLAGS] != NULL) {
1903 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1904 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1907 // FIXME: implement selection properly
1908 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1909 if ((ops->features & features) != features)
1911 if (!try_module_get(ops->owner))
1916 return ERR_PTR(-EOPNOTSUPP);
1919 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1920 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1921 [NFTA_SET_NAME] = { .type = NLA_STRING },
1922 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1923 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1924 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1925 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1926 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1929 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1930 const struct sk_buff *skb,
1931 const struct nlmsghdr *nlh,
1932 const struct nlattr * const nla[])
1934 struct net *net = sock_net(skb->sk);
1935 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1936 const struct nft_af_info *afi = NULL;
1937 const struct nft_table *table = NULL;
1939 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1940 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1942 return PTR_ERR(afi);
1945 if (nla[NFTA_SET_TABLE] != NULL) {
1947 return -EAFNOSUPPORT;
1949 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1951 return PTR_ERR(table);
1954 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1958 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1959 const struct nlattr *nla)
1961 struct nft_set *set;
1964 return ERR_PTR(-EINVAL);
1966 list_for_each_entry(set, &table->sets, list) {
1967 if (!nla_strcmp(nla, set->name))
1970 return ERR_PTR(-ENOENT);
1973 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1976 const struct nft_set *i;
1978 unsigned long *inuse;
1981 p = strnchr(name, IFNAMSIZ, '%');
1983 if (p[1] != 'd' || strchr(p + 2, '%'))
1986 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1990 list_for_each_entry(i, &ctx->table->sets, list) {
1993 if (!sscanf(i->name, name, &tmp))
1995 if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
1998 set_bit(tmp, inuse);
2001 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2002 free_page((unsigned long)inuse);
2005 snprintf(set->name, sizeof(set->name), name, n);
2006 list_for_each_entry(i, &ctx->table->sets, list) {
2007 if (!strcmp(set->name, i->name))
2013 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2014 const struct nft_set *set, u16 event, u16 flags)
2016 struct nfgenmsg *nfmsg;
2017 struct nlmsghdr *nlh;
2018 u32 portid = NETLINK_CB(ctx->skb).portid;
2019 u32 seq = ctx->nlh->nlmsg_seq;
2021 event |= NFNL_SUBSYS_NFTABLES << 8;
2022 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2025 goto nla_put_failure;
2027 nfmsg = nlmsg_data(nlh);
2028 nfmsg->nfgen_family = ctx->afi->family;
2029 nfmsg->version = NFNETLINK_V0;
2032 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2033 goto nla_put_failure;
2034 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2035 goto nla_put_failure;
2036 if (set->flags != 0)
2037 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2038 goto nla_put_failure;
2040 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2041 goto nla_put_failure;
2042 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2043 goto nla_put_failure;
2044 if (set->flags & NFT_SET_MAP) {
2045 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2046 goto nla_put_failure;
2047 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2048 goto nla_put_failure;
2051 return nlmsg_end(skb, nlh);
2054 nlmsg_trim(skb, nlh);
2058 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2059 const struct nft_set *set,
2062 struct sk_buff *skb;
2063 u32 portid = NETLINK_CB(ctx->skb).portid;
2067 report = nlmsg_report(ctx->nlh);
2068 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2072 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2076 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2082 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2086 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2090 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2091 struct netlink_callback *cb)
2093 const struct nft_set *set;
2094 unsigned int idx = 0, s_idx = cb->args[0];
2099 list_for_each_entry(set, &ctx->table->sets, list) {
2102 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2115 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2116 struct netlink_callback *cb)
2118 const struct nft_set *set;
2119 unsigned int idx, s_idx = cb->args[0];
2120 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2125 list_for_each_entry(table, &ctx->afi->tables, list) {
2127 if (cur_table != table)
2134 list_for_each_entry(set, &ctx->table->sets, list) {
2137 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2140 cb->args[2] = (unsigned long) table;
2152 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2153 struct netlink_callback *cb)
2155 const struct nft_set *set;
2156 unsigned int idx, s_idx = cb->args[0];
2157 const struct nft_af_info *afi;
2158 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2159 struct net *net = sock_net(skb->sk);
2160 int cur_family = cb->args[3];
2165 list_for_each_entry(afi, &net->nft.af_info, list) {
2167 if (afi->family != cur_family)
2173 list_for_each_entry(table, &afi->tables, list) {
2175 if (cur_table != table)
2184 list_for_each_entry(set, &ctx->table->sets, list) {
2187 if (nf_tables_fill_set(skb, ctx, set,
2191 cb->args[2] = (unsigned long) table;
2192 cb->args[3] = afi->family;
2207 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2209 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2210 struct nlattr *nla[NFTA_SET_MAX + 1];
2214 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2219 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2223 if (ctx.table == NULL) {
2224 if (ctx.afi == NULL)
2225 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2227 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2229 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2234 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2235 const struct nlmsghdr *nlh,
2236 const struct nlattr * const nla[])
2238 const struct nft_set *set;
2240 struct sk_buff *skb2;
2241 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2244 /* Verify existance before starting dump */
2245 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2249 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2250 struct netlink_dump_control c = {
2251 .dump = nf_tables_dump_sets,
2253 return netlink_dump_start(nlsk, skb, nlh, &c);
2256 /* Only accept unspec with dump */
2257 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2258 return -EAFNOSUPPORT;
2260 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2262 return PTR_ERR(set);
2264 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2268 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2272 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2279 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2280 const struct nlmsghdr *nlh,
2281 const struct nlattr * const nla[])
2283 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2284 const struct nft_set_ops *ops;
2285 const struct nft_af_info *afi;
2286 struct net *net = sock_net(skb->sk);
2287 struct nft_table *table;
2288 struct nft_set *set;
2290 char name[IFNAMSIZ];
2293 u32 ktype, klen, dlen, dtype, flags;
2296 if (nla[NFTA_SET_TABLE] == NULL ||
2297 nla[NFTA_SET_NAME] == NULL ||
2298 nla[NFTA_SET_KEY_LEN] == NULL)
2301 ktype = NFT_DATA_VALUE;
2302 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2303 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2304 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2308 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2309 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2313 if (nla[NFTA_SET_FLAGS] != NULL) {
2314 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2315 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2316 NFT_SET_INTERVAL | NFT_SET_MAP))
2322 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2323 if (!(flags & NFT_SET_MAP))
2326 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2327 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2328 dtype != NFT_DATA_VERDICT)
2331 if (dtype != NFT_DATA_VERDICT) {
2332 if (nla[NFTA_SET_DATA_LEN] == NULL)
2334 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2336 dlen > FIELD_SIZEOF(struct nft_data, data))
2339 dlen = sizeof(struct nft_data);
2340 } else if (flags & NFT_SET_MAP)
2343 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2345 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2347 return PTR_ERR(afi);
2349 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2351 return PTR_ERR(table);
2353 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2355 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2357 if (PTR_ERR(set) != -ENOENT)
2358 return PTR_ERR(set);
2363 if (nlh->nlmsg_flags & NLM_F_EXCL)
2365 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2370 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2373 ops = nft_select_set_ops(nla);
2375 return PTR_ERR(ops);
2378 if (ops->privsize != NULL)
2379 size = ops->privsize(nla);
2382 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2386 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2387 err = nf_tables_set_alloc_name(&ctx, set, name);
2391 INIT_LIST_HEAD(&set->bindings);
2399 err = ops->init(set, nla);
2403 list_add_tail(&set->list, &table->sets);
2404 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2410 module_put(ops->owner);
2414 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2416 list_del(&set->list);
2417 if (!(set->flags & NFT_SET_ANONYMOUS))
2418 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2420 set->ops->destroy(set);
2421 module_put(set->ops->owner);
2425 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2426 const struct nlmsghdr *nlh,
2427 const struct nlattr * const nla[])
2429 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2430 struct nft_set *set;
2434 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2435 return -EAFNOSUPPORT;
2436 if (nla[NFTA_SET_TABLE] == NULL)
2439 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2443 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2445 return PTR_ERR(set);
2446 if (!list_empty(&set->bindings))
2449 nf_tables_set_destroy(&ctx, set);
2453 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2454 const struct nft_set *set,
2455 const struct nft_set_iter *iter,
2456 const struct nft_set_elem *elem)
2458 enum nft_registers dreg;
2460 dreg = nft_type_to_reg(set->dtype);
2461 return nft_validate_data_load(ctx, dreg, &elem->data,
2462 set->dtype == NFT_DATA_VERDICT ?
2463 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2466 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2467 struct nft_set_binding *binding)
2469 struct nft_set_binding *i;
2470 struct nft_set_iter iter;
2472 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2475 if (set->flags & NFT_SET_MAP) {
2476 /* If the set is already bound to the same chain all
2477 * jumps are already validated for that chain.
2479 list_for_each_entry(i, &set->bindings, list) {
2480 if (i->chain == binding->chain)
2487 iter.fn = nf_tables_bind_check_setelem;
2489 set->ops->walk(ctx, set, &iter);
2491 /* Destroy anonymous sets if binding fails */
2492 if (set->flags & NFT_SET_ANONYMOUS)
2493 nf_tables_set_destroy(ctx, set);
2499 binding->chain = ctx->chain;
2500 list_add_tail(&binding->list, &set->bindings);
2504 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2505 struct nft_set_binding *binding)
2507 list_del(&binding->list);
2509 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2510 nf_tables_set_destroy(ctx, set);
2517 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2518 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2519 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2520 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2523 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2524 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2525 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2526 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2529 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2530 const struct sk_buff *skb,
2531 const struct nlmsghdr *nlh,
2532 const struct nlattr * const nla[])
2534 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2535 const struct nft_af_info *afi;
2536 const struct nft_table *table;
2537 struct net *net = sock_net(skb->sk);
2539 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2541 return PTR_ERR(afi);
2543 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2545 return PTR_ERR(table);
2547 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2551 static int nf_tables_fill_setelem(struct sk_buff *skb,
2552 const struct nft_set *set,
2553 const struct nft_set_elem *elem)
2555 unsigned char *b = skb_tail_pointer(skb);
2556 struct nlattr *nest;
2558 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2560 goto nla_put_failure;
2562 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2564 goto nla_put_failure;
2566 if (set->flags & NFT_SET_MAP &&
2567 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2568 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2569 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2571 goto nla_put_failure;
2573 if (elem->flags != 0)
2574 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2575 goto nla_put_failure;
2577 nla_nest_end(skb, nest);
2585 struct nft_set_dump_args {
2586 const struct netlink_callback *cb;
2587 struct nft_set_iter iter;
2588 struct sk_buff *skb;
2591 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2592 const struct nft_set *set,
2593 const struct nft_set_iter *iter,
2594 const struct nft_set_elem *elem)
2596 struct nft_set_dump_args *args;
2598 args = container_of(iter, struct nft_set_dump_args, iter);
2599 return nf_tables_fill_setelem(args->skb, set, elem);
2602 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2604 const struct nft_set *set;
2605 struct nft_set_dump_args args;
2607 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2608 struct nfgenmsg *nfmsg;
2609 struct nlmsghdr *nlh;
2610 struct nlattr *nest;
2614 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2615 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2619 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2623 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2625 return PTR_ERR(set);
2627 event = NFT_MSG_NEWSETELEM;
2628 event |= NFNL_SUBSYS_NFTABLES << 8;
2629 portid = NETLINK_CB(cb->skb).portid;
2630 seq = cb->nlh->nlmsg_seq;
2632 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2635 goto nla_put_failure;
2637 nfmsg = nlmsg_data(nlh);
2638 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2639 nfmsg->version = NFNETLINK_V0;
2642 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2643 goto nla_put_failure;
2644 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2645 goto nla_put_failure;
2647 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2649 goto nla_put_failure;
2653 args.iter.skip = cb->args[0];
2654 args.iter.count = 0;
2656 args.iter.fn = nf_tables_dump_setelem;
2657 set->ops->walk(&ctx, set, &args.iter);
2659 nla_nest_end(skb, nest);
2660 nlmsg_end(skb, nlh);
2662 if (args.iter.err && args.iter.err != -EMSGSIZE)
2663 return args.iter.err;
2664 if (args.iter.count == cb->args[0])
2667 cb->args[0] = args.iter.count;
2674 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2675 const struct nlmsghdr *nlh,
2676 const struct nlattr * const nla[])
2678 const struct nft_set *set;
2682 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2686 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2688 return PTR_ERR(set);
2690 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2691 struct netlink_dump_control c = {
2692 .dump = nf_tables_dump_set,
2694 return netlink_dump_start(nlsk, skb, nlh, &c);
2699 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2700 const struct nlattr *attr)
2702 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2703 struct nft_data_desc d1, d2;
2704 struct nft_set_elem elem;
2705 struct nft_set_binding *binding;
2706 enum nft_registers dreg;
2709 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2710 nft_set_elem_policy);
2714 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2718 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2719 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2720 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2724 if (set->flags & NFT_SET_MAP) {
2725 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2726 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2729 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2733 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2737 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2741 if (set->ops->get(set, &elem) == 0)
2744 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2745 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2750 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2753 dreg = nft_type_to_reg(set->dtype);
2754 list_for_each_entry(binding, &set->bindings, list) {
2755 struct nft_ctx bind_ctx = {
2757 .table = ctx->table,
2758 .chain = binding->chain,
2761 err = nft_validate_data_load(&bind_ctx, dreg,
2762 &elem.data, d2.type);
2768 err = set->ops->insert(set, &elem);
2775 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2776 nft_data_uninit(&elem.data, d2.type);
2778 nft_data_uninit(&elem.key, d1.type);
2783 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2784 const struct nlmsghdr *nlh,
2785 const struct nlattr * const nla[])
2787 const struct nlattr *attr;
2788 struct nft_set *set;
2792 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2796 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2798 return PTR_ERR(set);
2799 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2802 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2803 err = nft_add_set_elem(&ctx, set, attr);
2810 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2811 const struct nlattr *attr)
2813 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2814 struct nft_data_desc desc;
2815 struct nft_set_elem elem;
2818 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2819 nft_set_elem_policy);
2824 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2827 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2832 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2835 err = set->ops->get(set, &elem);
2839 set->ops->remove(set, &elem);
2841 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2842 if (set->flags & NFT_SET_MAP)
2843 nft_data_uninit(&elem.data, set->dtype);
2846 nft_data_uninit(&elem.key, desc.type);
2851 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2852 const struct nlmsghdr *nlh,
2853 const struct nlattr * const nla[])
2855 const struct nlattr *attr;
2856 struct nft_set *set;
2860 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2864 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2866 return PTR_ERR(set);
2867 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2870 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2871 err = nft_del_setelem(&ctx, set, attr);
2878 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2879 [NFT_MSG_NEWTABLE] = {
2880 .call = nf_tables_newtable,
2881 .attr_count = NFTA_TABLE_MAX,
2882 .policy = nft_table_policy,
2884 [NFT_MSG_GETTABLE] = {
2885 .call = nf_tables_gettable,
2886 .attr_count = NFTA_TABLE_MAX,
2887 .policy = nft_table_policy,
2889 [NFT_MSG_DELTABLE] = {
2890 .call = nf_tables_deltable,
2891 .attr_count = NFTA_TABLE_MAX,
2892 .policy = nft_table_policy,
2894 [NFT_MSG_NEWCHAIN] = {
2895 .call = nf_tables_newchain,
2896 .attr_count = NFTA_CHAIN_MAX,
2897 .policy = nft_chain_policy,
2899 [NFT_MSG_GETCHAIN] = {
2900 .call = nf_tables_getchain,
2901 .attr_count = NFTA_CHAIN_MAX,
2902 .policy = nft_chain_policy,
2904 [NFT_MSG_DELCHAIN] = {
2905 .call = nf_tables_delchain,
2906 .attr_count = NFTA_CHAIN_MAX,
2907 .policy = nft_chain_policy,
2909 [NFT_MSG_NEWRULE] = {
2910 .call_batch = nf_tables_newrule,
2911 .attr_count = NFTA_RULE_MAX,
2912 .policy = nft_rule_policy,
2914 [NFT_MSG_GETRULE] = {
2915 .call = nf_tables_getrule,
2916 .attr_count = NFTA_RULE_MAX,
2917 .policy = nft_rule_policy,
2919 [NFT_MSG_DELRULE] = {
2920 .call_batch = nf_tables_delrule,
2921 .attr_count = NFTA_RULE_MAX,
2922 .policy = nft_rule_policy,
2924 [NFT_MSG_NEWSET] = {
2925 .call = nf_tables_newset,
2926 .attr_count = NFTA_SET_MAX,
2927 .policy = nft_set_policy,
2929 [NFT_MSG_GETSET] = {
2930 .call = nf_tables_getset,
2931 .attr_count = NFTA_SET_MAX,
2932 .policy = nft_set_policy,
2934 [NFT_MSG_DELSET] = {
2935 .call = nf_tables_delset,
2936 .attr_count = NFTA_SET_MAX,
2937 .policy = nft_set_policy,
2939 [NFT_MSG_NEWSETELEM] = {
2940 .call = nf_tables_newsetelem,
2941 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2942 .policy = nft_set_elem_list_policy,
2944 [NFT_MSG_GETSETELEM] = {
2945 .call = nf_tables_getsetelem,
2946 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2947 .policy = nft_set_elem_list_policy,
2949 [NFT_MSG_DELSETELEM] = {
2950 .call = nf_tables_delsetelem,
2951 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2952 .policy = nft_set_elem_list_policy,
2956 static const struct nfnetlink_subsystem nf_tables_subsys = {
2957 .name = "nf_tables",
2958 .subsys_id = NFNL_SUBSYS_NFTABLES,
2959 .cb_count = NFT_MSG_MAX,
2961 .commit = nf_tables_commit,
2962 .abort = nf_tables_abort,
2966 * Loop detection - walk through the ruleset beginning at the destination chain
2967 * of a new jump until either the source chain is reached (loop) or all
2968 * reachable chains have been traversed.
2970 * The loop check is performed whenever a new jump verdict is added to an
2971 * expression or verdict map or a verdict map is bound to a new chain.
2974 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2975 const struct nft_chain *chain);
2977 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2978 const struct nft_set *set,
2979 const struct nft_set_iter *iter,
2980 const struct nft_set_elem *elem)
2982 switch (elem->data.verdict) {
2985 return nf_tables_check_loops(ctx, elem->data.chain);
2991 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2992 const struct nft_chain *chain)
2994 const struct nft_rule *rule;
2995 const struct nft_expr *expr, *last;
2996 const struct nft_set *set;
2997 struct nft_set_binding *binding;
2998 struct nft_set_iter iter;
3000 if (ctx->chain == chain)
3003 list_for_each_entry(rule, &chain->rules, list) {
3004 nft_rule_for_each_expr(expr, last, rule) {
3005 const struct nft_data *data = NULL;
3008 if (!expr->ops->validate)
3011 err = expr->ops->validate(ctx, expr, &data);
3018 switch (data->verdict) {
3021 err = nf_tables_check_loops(ctx, data->chain);
3030 list_for_each_entry(set, &ctx->table->sets, list) {
3031 if (!(set->flags & NFT_SET_MAP) ||
3032 set->dtype != NFT_DATA_VERDICT)
3035 list_for_each_entry(binding, &set->bindings, list) {
3036 if (binding->chain != chain)
3042 iter.fn = nf_tables_loop_check_setelem;
3044 set->ops->walk(ctx, set, &iter);
3054 * nft_validate_input_register - validate an expressions' input register
3056 * @reg: the register number
3058 * Validate that the input register is one of the general purpose
3061 int nft_validate_input_register(enum nft_registers reg)
3063 if (reg <= NFT_REG_VERDICT)
3065 if (reg > NFT_REG_MAX)
3069 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3072 * nft_validate_output_register - validate an expressions' output register
3074 * @reg: the register number
3076 * Validate that the output register is one of the general purpose
3077 * registers or the verdict register.
3079 int nft_validate_output_register(enum nft_registers reg)
3081 if (reg < NFT_REG_VERDICT)
3083 if (reg > NFT_REG_MAX)
3087 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3090 * nft_validate_data_load - validate an expressions' data load
3092 * @ctx: context of the expression performing the load
3093 * @reg: the destination register number
3094 * @data: the data to load
3095 * @type: the data type
3097 * Validate that a data load uses the appropriate data type for
3098 * the destination register. A value of NULL for the data means
3099 * that its runtime gathered data, which is always of type
3102 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3103 const struct nft_data *data,
3104 enum nft_data_types type)
3109 case NFT_REG_VERDICT:
3110 if (data == NULL || type != NFT_DATA_VERDICT)
3113 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3114 err = nf_tables_check_loops(ctx, data->chain);
3118 if (ctx->chain->level + 1 > data->chain->level) {
3119 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3121 data->chain->level = ctx->chain->level + 1;
3127 if (data != NULL && type != NFT_DATA_VALUE)
3132 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3134 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3135 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3136 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3137 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3140 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3141 struct nft_data_desc *desc, const struct nlattr *nla)
3143 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3144 struct nft_chain *chain;
3147 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3151 if (!tb[NFTA_VERDICT_CODE])
3153 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3155 switch (data->verdict) {
3162 desc->len = sizeof(data->verdict);
3166 if (!tb[NFTA_VERDICT_CHAIN])
3168 chain = nf_tables_chain_lookup(ctx->table,
3169 tb[NFTA_VERDICT_CHAIN]);
3171 return PTR_ERR(chain);
3172 if (chain->flags & NFT_BASE_CHAIN)
3176 data->chain = chain;
3177 desc->len = sizeof(data);
3183 desc->type = NFT_DATA_VERDICT;
3187 static void nft_verdict_uninit(const struct nft_data *data)
3189 switch (data->verdict) {
3197 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3199 struct nlattr *nest;
3201 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3203 goto nla_put_failure;
3205 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3206 goto nla_put_failure;
3208 switch (data->verdict) {
3211 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3212 goto nla_put_failure;
3214 nla_nest_end(skb, nest);
3221 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3222 struct nft_data_desc *desc, const struct nlattr *nla)
3229 if (len > sizeof(data->data))
3232 nla_memcpy(data->data, nla, sizeof(data->data));
3233 desc->type = NFT_DATA_VALUE;
3238 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3241 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3244 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3245 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3246 .len = FIELD_SIZEOF(struct nft_data, data) },
3247 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3251 * nft_data_init - parse nf_tables data netlink attributes
3253 * @ctx: context of the expression using the data
3254 * @data: destination struct nft_data
3255 * @desc: data description
3256 * @nla: netlink attribute containing data
3258 * Parse the netlink data attributes and initialize a struct nft_data.
3259 * The type and length of data are returned in the data description.
3261 * The caller can indicate that it only wants to accept data of type
3262 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3264 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3265 struct nft_data_desc *desc, const struct nlattr *nla)
3267 struct nlattr *tb[NFTA_DATA_MAX + 1];
3270 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3274 if (tb[NFTA_DATA_VALUE])
3275 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3276 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3277 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3280 EXPORT_SYMBOL_GPL(nft_data_init);
3283 * nft_data_uninit - release a nft_data item
3285 * @data: struct nft_data to release
3286 * @type: type of data
3288 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3289 * all others need to be released by calling this function.
3291 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3294 case NFT_DATA_VALUE:
3296 case NFT_DATA_VERDICT:
3297 return nft_verdict_uninit(data);
3302 EXPORT_SYMBOL_GPL(nft_data_uninit);
3304 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3305 enum nft_data_types type, unsigned int len)
3307 struct nlattr *nest;
3310 nest = nla_nest_start(skb, attr);
3315 case NFT_DATA_VALUE:
3316 err = nft_value_dump(skb, data, len);
3318 case NFT_DATA_VERDICT:
3319 err = nft_verdict_dump(skb, data);
3326 nla_nest_end(skb, nest);
3329 EXPORT_SYMBOL_GPL(nft_data_dump);
3331 static int nf_tables_init_net(struct net *net)
3333 INIT_LIST_HEAD(&net->nft.af_info);
3334 INIT_LIST_HEAD(&net->nft.commit_list);
3338 static struct pernet_operations nf_tables_net_ops = {
3339 .init = nf_tables_init_net,
3342 static int __init nf_tables_module_init(void)
3346 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3353 err = nf_tables_core_module_init();
3357 err = nfnetlink_subsys_register(&nf_tables_subsys);
3361 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3362 return register_pernet_subsys(&nf_tables_net_ops);
3364 nf_tables_core_module_exit();
3371 static void __exit nf_tables_module_exit(void)
3373 unregister_pernet_subsys(&nf_tables_net_ops);
3374 nfnetlink_subsys_unregister(&nf_tables_subsys);
3375 nf_tables_core_module_exit();
3379 module_init(nf_tables_module_init);
3380 module_exit(nf_tables_module_exit);
3382 MODULE_LICENSE("GPL");
3383 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3384 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);