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;
406 afi = nf_tables_afinfo_lookup(net, family, true);
410 name = nla[NFTA_TABLE_NAME];
411 table = nf_tables_table_lookup(afi, name);
413 if (PTR_ERR(table) != -ENOENT)
414 return PTR_ERR(table);
419 if (nlh->nlmsg_flags & NLM_F_EXCL)
421 if (nlh->nlmsg_flags & NLM_F_REPLACE)
423 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
426 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
430 nla_strlcpy(table->name, name, nla_len(name));
431 INIT_LIST_HEAD(&table->chains);
432 INIT_LIST_HEAD(&table->sets);
434 if (nla[NFTA_TABLE_FLAGS]) {
437 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
438 if (flags & ~NFT_TABLE_F_DORMANT) {
443 table->flags |= flags;
446 list_add_tail(&table->list, &afi->tables);
447 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
451 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
452 const struct nlmsghdr *nlh,
453 const struct nlattr * const nla[])
455 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
456 struct nft_af_info *afi;
457 struct nft_table *table;
458 struct net *net = sock_net(skb->sk);
459 int family = nfmsg->nfgen_family;
461 afi = nf_tables_afinfo_lookup(net, family, false);
465 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
467 return PTR_ERR(table);
472 list_del(&table->list);
473 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
478 int nft_register_chain_type(const struct nf_chain_type *ctype)
482 nfnl_lock(NFNL_SUBSYS_NFTABLES);
483 if (chain_type[ctype->family][ctype->type] != NULL) {
487 chain_type[ctype->family][ctype->type] = ctype;
489 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
492 EXPORT_SYMBOL_GPL(nft_register_chain_type);
494 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
496 nfnl_lock(NFNL_SUBSYS_NFTABLES);
497 chain_type[ctype->family][ctype->type] = NULL;
498 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
500 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
506 static struct nft_chain *
507 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
509 struct nft_chain *chain;
511 list_for_each_entry(chain, &table->chains, list) {
512 if (chain->handle == handle)
516 return ERR_PTR(-ENOENT);
519 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
520 const struct nlattr *nla)
522 struct nft_chain *chain;
525 return ERR_PTR(-EINVAL);
527 list_for_each_entry(chain, &table->chains, list) {
528 if (!nla_strcmp(nla, chain->name))
532 return ERR_PTR(-ENOENT);
535 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
536 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
537 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
538 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
539 .len = NFT_CHAIN_MAXNAMELEN - 1 },
540 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
541 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
542 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
543 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
546 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
547 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
548 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
551 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
553 struct nft_stats *cpu_stats, total;
557 memset(&total, 0, sizeof(total));
558 for_each_possible_cpu(cpu) {
559 cpu_stats = per_cpu_ptr(stats, cpu);
560 total.pkts += cpu_stats->pkts;
561 total.bytes += cpu_stats->bytes;
563 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
565 goto nla_put_failure;
567 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
568 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
569 goto nla_put_failure;
571 nla_nest_end(skb, nest);
578 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
579 int event, u32 flags, int family,
580 const struct nft_table *table,
581 const struct nft_chain *chain)
583 struct nlmsghdr *nlh;
584 struct nfgenmsg *nfmsg;
586 event |= NFNL_SUBSYS_NFTABLES << 8;
587 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
589 goto nla_put_failure;
591 nfmsg = nlmsg_data(nlh);
592 nfmsg->nfgen_family = family;
593 nfmsg->version = NFNETLINK_V0;
596 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
597 goto nla_put_failure;
598 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
599 goto nla_put_failure;
600 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
601 goto nla_put_failure;
603 if (chain->flags & NFT_BASE_CHAIN) {
604 const struct nft_base_chain *basechain = nft_base_chain(chain);
605 const struct nf_hook_ops *ops = &basechain->ops[0];
608 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
610 goto nla_put_failure;
611 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
612 goto nla_put_failure;
613 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
614 goto nla_put_failure;
615 nla_nest_end(skb, nest);
617 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
618 htonl(basechain->policy)))
619 goto nla_put_failure;
621 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
622 goto nla_put_failure;
624 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
625 goto nla_put_failure;
628 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
629 goto nla_put_failure;
631 return nlmsg_end(skb, nlh);
634 nlmsg_trim(skb, nlh);
638 static int nf_tables_chain_notify(const struct sk_buff *oskb,
639 const struct nlmsghdr *nlh,
640 const struct nft_table *table,
641 const struct nft_chain *chain,
642 int event, int family)
645 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
646 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
647 u32 seq = nlh ? nlh->nlmsg_seq : 0;
651 report = nlh ? nlmsg_report(nlh) : false;
652 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
656 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
660 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
667 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
671 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
675 static int nf_tables_dump_chains(struct sk_buff *skb,
676 struct netlink_callback *cb)
678 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
679 const struct nft_af_info *afi;
680 const struct nft_table *table;
681 const struct nft_chain *chain;
682 unsigned int idx = 0, s_idx = cb->args[0];
683 struct net *net = sock_net(skb->sk);
684 int family = nfmsg->nfgen_family;
686 list_for_each_entry(afi, &net->nft.af_info, list) {
687 if (family != NFPROTO_UNSPEC && family != afi->family)
690 list_for_each_entry(table, &afi->tables, list) {
691 list_for_each_entry(chain, &table->chains, list) {
695 memset(&cb->args[1], 0,
696 sizeof(cb->args) - sizeof(cb->args[0]));
697 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
701 afi->family, table, chain) < 0)
714 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
715 const struct nlmsghdr *nlh,
716 const struct nlattr * const nla[])
718 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
719 const struct nft_af_info *afi;
720 const struct nft_table *table;
721 const struct nft_chain *chain;
722 struct sk_buff *skb2;
723 struct net *net = sock_net(skb->sk);
724 int family = nfmsg->nfgen_family;
727 if (nlh->nlmsg_flags & NLM_F_DUMP) {
728 struct netlink_dump_control c = {
729 .dump = nf_tables_dump_chains,
731 return netlink_dump_start(nlsk, skb, nlh, &c);
734 afi = nf_tables_afinfo_lookup(net, family, false);
738 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
740 return PTR_ERR(table);
742 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
744 return PTR_ERR(chain);
746 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
750 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
751 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
752 family, table, chain);
756 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
763 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
764 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
765 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
769 nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
771 struct nlattr *tb[NFTA_COUNTER_MAX+1];
772 struct nft_stats __percpu *newstats;
773 struct nft_stats *stats;
776 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
780 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
783 newstats = alloc_percpu(struct nft_stats);
784 if (newstats == NULL)
787 /* Restore old counters on this cpu, no problem. Per-cpu statistics
788 * are not exposed to userspace.
790 stats = this_cpu_ptr(newstats);
791 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
792 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
795 /* nfnl_lock is held, add some nfnl function for this, later */
796 struct nft_stats __percpu *oldstats =
797 rcu_dereference_protected(chain->stats, 1);
799 rcu_assign_pointer(chain->stats, newstats);
801 free_percpu(oldstats);
803 rcu_assign_pointer(chain->stats, newstats);
808 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
809 const struct nlmsghdr *nlh,
810 const struct nlattr * const nla[])
812 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
813 const struct nlattr * uninitialized_var(name);
814 const struct nft_af_info *afi;
815 struct nft_table *table;
816 struct nft_chain *chain;
817 struct nft_base_chain *basechain = NULL;
818 struct nlattr *ha[NFTA_HOOK_MAX + 1];
819 struct net *net = sock_net(skb->sk);
820 int family = nfmsg->nfgen_family;
821 u8 policy = NF_ACCEPT;
827 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
829 afi = nf_tables_afinfo_lookup(net, family, true);
833 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
835 return PTR_ERR(table);
838 name = nla[NFTA_CHAIN_NAME];
840 if (nla[NFTA_CHAIN_HANDLE]) {
841 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
842 chain = nf_tables_chain_lookup_byhandle(table, handle);
844 return PTR_ERR(chain);
846 chain = nf_tables_chain_lookup(table, name);
848 if (PTR_ERR(chain) != -ENOENT)
849 return PTR_ERR(chain);
854 if (nla[NFTA_CHAIN_POLICY]) {
855 if ((chain != NULL &&
856 !(chain->flags & NFT_BASE_CHAIN)) ||
857 nla[NFTA_CHAIN_HOOK] == NULL)
860 policy = nla_get_be32(nla[NFTA_CHAIN_POLICY]);
871 if (nlh->nlmsg_flags & NLM_F_EXCL)
873 if (nlh->nlmsg_flags & NLM_F_REPLACE)
876 if (nla[NFTA_CHAIN_HANDLE] && name &&
877 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
880 if (nla[NFTA_CHAIN_COUNTERS]) {
881 if (!(chain->flags & NFT_BASE_CHAIN))
884 err = nf_tables_counters(nft_base_chain(chain),
885 nla[NFTA_CHAIN_COUNTERS]);
890 if (nla[NFTA_CHAIN_POLICY])
891 nft_base_chain(chain)->policy = policy;
893 if (nla[NFTA_CHAIN_HANDLE] && name)
894 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
899 if (table->use == UINT_MAX)
902 if (nla[NFTA_CHAIN_HOOK]) {
903 const struct nf_chain_type *type;
904 struct nf_hook_ops *ops;
906 u32 hooknum, priority;
908 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
909 if (nla[NFTA_CHAIN_TYPE]) {
910 type = nf_tables_chain_type_lookup(afi,
911 nla[NFTA_CHAIN_TYPE],
914 return PTR_ERR(type);
917 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
921 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
922 ha[NFTA_HOOK_PRIORITY] == NULL)
925 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
926 if (hooknum >= afi->nhooks)
928 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
930 if (!(type->hook_mask & (1 << hooknum)))
932 if (!try_module_get(type->owner))
934 hookfn = type->hooks[hooknum];
936 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
937 if (basechain == NULL)
940 if (nla[NFTA_CHAIN_COUNTERS]) {
941 err = nf_tables_counters(basechain,
942 nla[NFTA_CHAIN_COUNTERS]);
944 module_put(type->owner);
949 struct nft_stats __percpu *newstats;
951 newstats = alloc_percpu(struct nft_stats);
952 if (newstats == NULL) {
953 module_put(type->owner);
957 rcu_assign_pointer(basechain->stats, newstats);
960 basechain->type = type;
961 chain = &basechain->chain;
963 for (i = 0; i < afi->nops; i++) {
964 ops = &basechain->ops[i];
966 ops->owner = afi->owner;
967 ops->hooknum = hooknum;
968 ops->priority = priority;
970 ops->hook = afi->hooks[ops->hooknum];
973 if (afi->hook_ops_init)
974 afi->hook_ops_init(ops, i);
977 chain->flags |= NFT_BASE_CHAIN;
978 basechain->policy = policy;
980 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
985 INIT_LIST_HEAD(&chain->rules);
986 chain->handle = nf_tables_alloc_handle(table);
988 chain->table = table;
989 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
991 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
992 chain->flags & NFT_BASE_CHAIN) {
993 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
995 module_put(basechain->type->owner);
996 free_percpu(basechain->stats);
1001 list_add_tail(&chain->list, &table->chains);
1004 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1009 static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1011 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1013 BUG_ON(chain->use > 0);
1015 if (chain->flags & NFT_BASE_CHAIN) {
1016 module_put(nft_base_chain(chain)->type->owner);
1017 free_percpu(nft_base_chain(chain)->stats);
1018 kfree(nft_base_chain(chain));
1023 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1024 const struct nlmsghdr *nlh,
1025 const struct nlattr * const nla[])
1027 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1028 const struct nft_af_info *afi;
1029 struct nft_table *table;
1030 struct nft_chain *chain;
1031 struct net *net = sock_net(skb->sk);
1032 int family = nfmsg->nfgen_family;
1034 afi = nf_tables_afinfo_lookup(net, family, false);
1036 return PTR_ERR(afi);
1038 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1040 return PTR_ERR(table);
1042 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1044 return PTR_ERR(chain);
1046 if (!list_empty(&chain->rules))
1049 list_del(&chain->list);
1052 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1053 chain->flags & NFT_BASE_CHAIN)
1054 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
1056 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1059 /* Make sure all rule references are gone before this is released */
1060 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1064 static void nft_ctx_init(struct nft_ctx *ctx,
1065 const struct sk_buff *skb,
1066 const struct nlmsghdr *nlh,
1067 const struct nft_af_info *afi,
1068 const struct nft_table *table,
1069 const struct nft_chain *chain,
1070 const struct nlattr * const *nla)
1072 ctx->net = sock_net(skb->sk);
1086 * nft_register_expr - register nf_tables expr type
1089 * Registers the expr type for use with nf_tables. Returns zero on
1090 * success or a negative errno code otherwise.
1092 int nft_register_expr(struct nft_expr_type *type)
1094 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1095 list_add_tail(&type->list, &nf_tables_expressions);
1096 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1099 EXPORT_SYMBOL_GPL(nft_register_expr);
1102 * nft_unregister_expr - unregister nf_tables expr type
1105 * Unregisters the expr typefor use with nf_tables.
1107 void nft_unregister_expr(struct nft_expr_type *type)
1109 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1110 list_del(&type->list);
1111 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1113 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1115 static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
1117 const struct nft_expr_type *type;
1119 list_for_each_entry(type, &nf_tables_expressions, list) {
1120 if (!nla_strcmp(nla, type->name))
1126 static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
1128 const struct nft_expr_type *type;
1131 return ERR_PTR(-EINVAL);
1133 type = __nft_expr_type_get(nla);
1134 if (type != NULL && try_module_get(type->owner))
1137 #ifdef CONFIG_MODULES
1139 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1140 request_module("nft-expr-%.*s",
1141 nla_len(nla), (char *)nla_data(nla));
1142 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1143 if (__nft_expr_type_get(nla))
1144 return ERR_PTR(-EAGAIN);
1147 return ERR_PTR(-ENOENT);
1150 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1151 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1152 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1155 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1156 const struct nft_expr *expr)
1158 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1159 goto nla_put_failure;
1161 if (expr->ops->dump) {
1162 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1164 goto nla_put_failure;
1165 if (expr->ops->dump(skb, expr) < 0)
1166 goto nla_put_failure;
1167 nla_nest_end(skb, data);
1176 struct nft_expr_info {
1177 const struct nft_expr_ops *ops;
1178 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1181 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1182 const struct nlattr *nla,
1183 struct nft_expr_info *info)
1185 const struct nft_expr_type *type;
1186 const struct nft_expr_ops *ops;
1187 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1190 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1194 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1196 return PTR_ERR(type);
1198 if (tb[NFTA_EXPR_DATA]) {
1199 err = nla_parse_nested(info->tb, type->maxattr,
1200 tb[NFTA_EXPR_DATA], type->policy);
1204 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1206 if (type->select_ops != NULL) {
1207 ops = type->select_ops(ctx,
1208 (const struct nlattr * const *)info->tb);
1220 module_put(type->owner);
1224 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1225 const struct nft_expr_info *info,
1226 struct nft_expr *expr)
1228 const struct nft_expr_ops *ops = info->ops;
1233 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1245 static void nf_tables_expr_destroy(struct nft_expr *expr)
1247 if (expr->ops->destroy)
1248 expr->ops->destroy(expr);
1249 module_put(expr->ops->type->owner);
1256 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1259 struct nft_rule *rule;
1261 // FIXME: this sucks
1262 list_for_each_entry(rule, &chain->rules, list) {
1263 if (handle == rule->handle)
1267 return ERR_PTR(-ENOENT);
1270 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1271 const struct nlattr *nla)
1274 return ERR_PTR(-EINVAL);
1276 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1279 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1280 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1281 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1282 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1283 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1284 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1285 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1286 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1289 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1290 int event, u32 flags, int family,
1291 const struct nft_table *table,
1292 const struct nft_chain *chain,
1293 const struct nft_rule *rule)
1295 struct nlmsghdr *nlh;
1296 struct nfgenmsg *nfmsg;
1297 const struct nft_expr *expr, *next;
1298 struct nlattr *list;
1299 const struct nft_rule *prule;
1300 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1302 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1305 goto nla_put_failure;
1307 nfmsg = nlmsg_data(nlh);
1308 nfmsg->nfgen_family = family;
1309 nfmsg->version = NFNETLINK_V0;
1312 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1313 goto nla_put_failure;
1314 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1315 goto nla_put_failure;
1316 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1317 goto nla_put_failure;
1319 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1320 prule = list_entry(rule->list.prev, struct nft_rule, list);
1321 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1322 cpu_to_be64(prule->handle)))
1323 goto nla_put_failure;
1326 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1328 goto nla_put_failure;
1329 nft_rule_for_each_expr(expr, next, rule) {
1330 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1332 goto nla_put_failure;
1333 if (nf_tables_fill_expr_info(skb, expr) < 0)
1334 goto nla_put_failure;
1335 nla_nest_end(skb, elem);
1337 nla_nest_end(skb, list);
1339 return nlmsg_end(skb, nlh);
1342 nlmsg_trim(skb, nlh);
1346 static int nf_tables_rule_notify(const struct sk_buff *oskb,
1347 const struct nlmsghdr *nlh,
1348 const struct nft_table *table,
1349 const struct nft_chain *chain,
1350 const struct nft_rule *rule,
1351 int event, u32 flags, int family)
1353 struct sk_buff *skb;
1354 u32 portid = NETLINK_CB(oskb).portid;
1355 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1356 u32 seq = nlh->nlmsg_seq;
1360 report = nlmsg_report(nlh);
1361 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1365 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1369 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1370 family, table, chain, rule);
1376 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1380 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1385 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1387 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1390 static inline int gencursor_next(struct net *net)
1392 return net->nft.gencursor+1 == 1 ? 1 : 0;
1396 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1398 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1402 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1404 /* Now inactive, will be active in the future */
1405 rule->genmask = (1 << net->nft.gencursor);
1409 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1411 rule->genmask = (1 << gencursor_next(net));
1414 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1419 static int nf_tables_dump_rules(struct sk_buff *skb,
1420 struct netlink_callback *cb)
1422 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1423 const struct nft_af_info *afi;
1424 const struct nft_table *table;
1425 const struct nft_chain *chain;
1426 const struct nft_rule *rule;
1427 unsigned int idx = 0, s_idx = cb->args[0];
1428 struct net *net = sock_net(skb->sk);
1429 int family = nfmsg->nfgen_family;
1430 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1431 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1433 list_for_each_entry(afi, &net->nft.af_info, list) {
1434 if (family != NFPROTO_UNSPEC && family != afi->family)
1437 list_for_each_entry(table, &afi->tables, list) {
1438 list_for_each_entry(chain, &table->chains, list) {
1439 list_for_each_entry(rule, &chain->rules, list) {
1440 if (!nft_rule_is_active(net, rule))
1445 memset(&cb->args[1], 0,
1446 sizeof(cb->args) - sizeof(cb->args[0]));
1447 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1450 NLM_F_MULTI | NLM_F_APPEND,
1451 afi->family, table, chain, rule) < 0)
1460 /* Invalidate this dump, a transition to the new generation happened */
1461 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1468 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1469 const struct nlmsghdr *nlh,
1470 const struct nlattr * const nla[])
1472 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1473 const struct nft_af_info *afi;
1474 const struct nft_table *table;
1475 const struct nft_chain *chain;
1476 const struct nft_rule *rule;
1477 struct sk_buff *skb2;
1478 struct net *net = sock_net(skb->sk);
1479 int family = nfmsg->nfgen_family;
1482 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1483 struct netlink_dump_control c = {
1484 .dump = nf_tables_dump_rules,
1486 return netlink_dump_start(nlsk, skb, nlh, &c);
1489 afi = nf_tables_afinfo_lookup(net, family, false);
1491 return PTR_ERR(afi);
1493 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1495 return PTR_ERR(table);
1497 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1499 return PTR_ERR(chain);
1501 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1503 return PTR_ERR(rule);
1505 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1509 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1510 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1511 family, table, chain, rule);
1515 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1522 static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1524 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1525 struct nft_expr *expr;
1528 * Careful: some expressions might not be initialized in case this
1529 * is called on error from nf_tables_newrule().
1531 expr = nft_expr_first(rule);
1532 while (expr->ops && expr != nft_expr_last(rule)) {
1533 nf_tables_expr_destroy(expr);
1534 expr = nft_expr_next(expr);
1539 static void nf_tables_rule_destroy(struct nft_rule *rule)
1541 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1544 #define NFT_RULE_MAXEXPRS 128
1546 static struct nft_expr_info *info;
1548 static struct nft_rule_trans *
1549 nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1551 struct nft_rule_trans *rupd;
1553 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1557 rupd->chain = ctx->chain;
1558 rupd->table = ctx->table;
1560 rupd->family = ctx->afi->family;
1561 rupd->nlh = ctx->nlh;
1562 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1567 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1568 const struct nlmsghdr *nlh,
1569 const struct nlattr * const nla[])
1571 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1572 const struct nft_af_info *afi;
1573 struct net *net = sock_net(skb->sk);
1574 struct nft_table *table;
1575 struct nft_chain *chain;
1576 struct nft_rule *rule, *old_rule = NULL;
1577 struct nft_rule_trans *repl = NULL;
1578 struct nft_expr *expr;
1581 unsigned int size, i, n;
1584 u64 handle, pos_handle;
1586 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1588 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1590 return PTR_ERR(afi);
1592 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1594 return PTR_ERR(table);
1596 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1598 return PTR_ERR(chain);
1600 if (nla[NFTA_RULE_HANDLE]) {
1601 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1602 rule = __nf_tables_rule_lookup(chain, handle);
1604 return PTR_ERR(rule);
1606 if (nlh->nlmsg_flags & NLM_F_EXCL)
1608 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1613 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1615 handle = nf_tables_alloc_handle(table);
1618 if (nla[NFTA_RULE_POSITION]) {
1619 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1622 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1623 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1624 if (IS_ERR(old_rule))
1625 return PTR_ERR(old_rule);
1628 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1632 if (nla[NFTA_RULE_EXPRESSIONS]) {
1633 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1635 if (nla_type(tmp) != NFTA_LIST_ELEM)
1637 if (n == NFT_RULE_MAXEXPRS)
1639 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1642 size += info[n].ops->size;
1648 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1652 nft_rule_activate_next(net, rule);
1654 rule->handle = handle;
1657 expr = nft_expr_first(rule);
1658 for (i = 0; i < n; i++) {
1659 err = nf_tables_newexpr(&ctx, &info[i], expr);
1663 expr = nft_expr_next(expr);
1666 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1667 if (nft_rule_is_active_next(net, old_rule)) {
1668 repl = nf_tables_trans_add(old_rule, &ctx);
1673 nft_rule_disactivate_next(net, old_rule);
1674 list_add_tail(&rule->list, &old_rule->list);
1679 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1681 list_add_rcu(&rule->list, &old_rule->list);
1683 list_add_tail_rcu(&rule->list, &chain->rules);
1686 list_add_tail_rcu(&rule->list, &old_rule->list);
1688 list_add_rcu(&rule->list, &chain->rules);
1691 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1698 list_del_rcu(&rule->list);
1700 list_del_rcu(&repl->rule->list);
1701 list_del(&repl->list);
1702 nft_rule_clear(net, repl->rule);
1706 nf_tables_rule_destroy(rule);
1708 for (i = 0; i < n; i++) {
1709 if (info[i].ops != NULL)
1710 module_put(info[i].ops->type->owner);
1716 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1718 /* You cannot delete the same rule twice */
1719 if (nft_rule_is_active_next(ctx->net, rule)) {
1720 if (nf_tables_trans_add(rule, ctx) == NULL)
1722 nft_rule_disactivate_next(ctx->net, rule);
1728 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1730 struct nft_rule *rule;
1733 list_for_each_entry(rule, &ctx->chain->rules, list) {
1734 err = nf_tables_delrule_one(ctx, rule);
1741 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1742 const struct nlmsghdr *nlh,
1743 const struct nlattr * const nla[])
1745 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1746 const struct nft_af_info *afi;
1747 struct net *net = sock_net(skb->sk);
1748 const struct nft_table *table;
1749 struct nft_chain *chain = NULL;
1750 struct nft_rule *rule;
1751 int family = nfmsg->nfgen_family, err = 0;
1754 afi = nf_tables_afinfo_lookup(net, family, false);
1756 return PTR_ERR(afi);
1758 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1760 return PTR_ERR(table);
1762 if (nla[NFTA_RULE_CHAIN]) {
1763 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1765 return PTR_ERR(chain);
1768 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1771 if (nla[NFTA_RULE_HANDLE]) {
1772 rule = nf_tables_rule_lookup(chain,
1773 nla[NFTA_RULE_HANDLE]);
1775 return PTR_ERR(rule);
1777 err = nf_tables_delrule_one(&ctx, rule);
1779 err = nf_table_delrule_by_chain(&ctx);
1782 list_for_each_entry(chain, &table->chains, list) {
1784 err = nf_table_delrule_by_chain(&ctx);
1793 static int nf_tables_commit(struct sk_buff *skb)
1795 struct net *net = sock_net(skb->sk);
1796 struct nft_rule_trans *rupd, *tmp;
1798 /* Bump generation counter, invalidate any dump in progress */
1801 /* A new generation has just started */
1802 net->nft.gencursor = gencursor_next(net);
1804 /* Make sure all packets have left the previous generation before
1805 * purging old rules.
1809 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1810 /* Delete this rule from the dirty list */
1811 list_del(&rupd->list);
1813 /* This rule was inactive in the past and just became active.
1814 * Clear the next bit of the genmask since its meaning has
1815 * changed, now it is the future.
1817 if (nft_rule_is_active(net, rupd->rule)) {
1818 nft_rule_clear(net, rupd->rule);
1819 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1820 rupd->chain, rupd->rule,
1827 /* This rule is in the past, get rid of it */
1828 list_del_rcu(&rupd->rule->list);
1829 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1830 rupd->rule, NFT_MSG_DELRULE, 0,
1832 nf_tables_rule_destroy(rupd->rule);
1839 static int nf_tables_abort(struct sk_buff *skb)
1841 struct net *net = sock_net(skb->sk);
1842 struct nft_rule_trans *rupd, *tmp;
1844 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1845 /* Delete all rules from the dirty list */
1846 list_del(&rupd->list);
1848 if (!nft_rule_is_active_next(net, rupd->rule)) {
1849 nft_rule_clear(net, rupd->rule);
1854 /* This rule is inactive, get rid of it */
1855 list_del_rcu(&rupd->rule->list);
1856 nf_tables_rule_destroy(rupd->rule);
1866 static LIST_HEAD(nf_tables_set_ops);
1868 int nft_register_set(struct nft_set_ops *ops)
1870 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1871 list_add_tail(&ops->list, &nf_tables_set_ops);
1872 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1875 EXPORT_SYMBOL_GPL(nft_register_set);
1877 void nft_unregister_set(struct nft_set_ops *ops)
1879 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1880 list_del(&ops->list);
1881 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1883 EXPORT_SYMBOL_GPL(nft_unregister_set);
1885 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1887 const struct nft_set_ops *ops;
1890 #ifdef CONFIG_MODULES
1891 if (list_empty(&nf_tables_set_ops)) {
1892 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1893 request_module("nft-set");
1894 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1895 if (!list_empty(&nf_tables_set_ops))
1896 return ERR_PTR(-EAGAIN);
1900 if (nla[NFTA_SET_FLAGS] != NULL) {
1901 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1902 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1905 // FIXME: implement selection properly
1906 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1907 if ((ops->features & features) != features)
1909 if (!try_module_get(ops->owner))
1914 return ERR_PTR(-EOPNOTSUPP);
1917 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1918 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1919 [NFTA_SET_NAME] = { .type = NLA_STRING },
1920 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1921 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1922 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1923 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1924 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1927 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1928 const struct sk_buff *skb,
1929 const struct nlmsghdr *nlh,
1930 const struct nlattr * const nla[])
1932 struct net *net = sock_net(skb->sk);
1933 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1934 const struct nft_af_info *afi = NULL;
1935 const struct nft_table *table = NULL;
1937 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1938 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1940 return PTR_ERR(afi);
1943 if (nla[NFTA_SET_TABLE] != NULL) {
1944 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1946 return PTR_ERR(table);
1949 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1953 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1954 const struct nlattr *nla)
1956 struct nft_set *set;
1959 return ERR_PTR(-EINVAL);
1961 list_for_each_entry(set, &table->sets, list) {
1962 if (!nla_strcmp(nla, set->name))
1965 return ERR_PTR(-ENOENT);
1968 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1971 const struct nft_set *i;
1973 unsigned long *inuse;
1976 p = strnchr(name, IFNAMSIZ, '%');
1978 if (p[1] != 'd' || strchr(p + 2, '%'))
1981 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1985 list_for_each_entry(i, &ctx->table->sets, list) {
1988 if (!sscanf(i->name, name, &tmp))
1990 if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
1993 set_bit(tmp, inuse);
1996 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1997 free_page((unsigned long)inuse);
2000 snprintf(set->name, sizeof(set->name), name, n);
2001 list_for_each_entry(i, &ctx->table->sets, list) {
2002 if (!strcmp(set->name, i->name))
2008 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2009 const struct nft_set *set, u16 event, u16 flags)
2011 struct nfgenmsg *nfmsg;
2012 struct nlmsghdr *nlh;
2013 u32 portid = NETLINK_CB(ctx->skb).portid;
2014 u32 seq = ctx->nlh->nlmsg_seq;
2016 event |= NFNL_SUBSYS_NFTABLES << 8;
2017 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2020 goto nla_put_failure;
2022 nfmsg = nlmsg_data(nlh);
2023 nfmsg->nfgen_family = ctx->afi->family;
2024 nfmsg->version = NFNETLINK_V0;
2027 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2028 goto nla_put_failure;
2029 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2030 goto nla_put_failure;
2031 if (set->flags != 0)
2032 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2033 goto nla_put_failure;
2035 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2036 goto nla_put_failure;
2037 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2038 goto nla_put_failure;
2039 if (set->flags & NFT_SET_MAP) {
2040 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2041 goto nla_put_failure;
2042 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2043 goto nla_put_failure;
2046 return nlmsg_end(skb, nlh);
2049 nlmsg_trim(skb, nlh);
2053 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2054 const struct nft_set *set,
2057 struct sk_buff *skb;
2058 u32 portid = NETLINK_CB(ctx->skb).portid;
2062 report = nlmsg_report(ctx->nlh);
2063 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2067 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2071 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2077 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2081 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2085 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2086 struct netlink_callback *cb)
2088 const struct nft_set *set;
2089 unsigned int idx = 0, s_idx = cb->args[0];
2094 list_for_each_entry(set, &ctx->table->sets, list) {
2097 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2110 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2111 struct netlink_callback *cb)
2113 const struct nft_set *set;
2114 unsigned int idx, s_idx = cb->args[0];
2115 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2120 list_for_each_entry(table, &ctx->afi->tables, list) {
2122 if (cur_table != table)
2129 list_for_each_entry(set, &ctx->table->sets, list) {
2132 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2135 cb->args[2] = (unsigned long) table;
2147 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2148 struct netlink_callback *cb)
2150 const struct nft_set *set;
2151 unsigned int idx, s_idx = cb->args[0];
2152 const struct nft_af_info *afi;
2153 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2154 struct net *net = sock_net(skb->sk);
2155 int cur_family = cb->args[3];
2160 list_for_each_entry(afi, &net->nft.af_info, list) {
2162 if (afi->family != cur_family)
2168 list_for_each_entry(table, &afi->tables, list) {
2170 if (cur_table != table)
2179 list_for_each_entry(set, &ctx->table->sets, list) {
2182 if (nf_tables_fill_set(skb, ctx, set,
2186 cb->args[2] = (unsigned long) table;
2187 cb->args[3] = afi->family;
2202 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2204 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2205 struct nlattr *nla[NFTA_SET_MAX + 1];
2209 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2214 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2218 if (ctx.table == NULL) {
2219 if (ctx.afi == NULL)
2220 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2222 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2224 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2229 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2230 const struct nlmsghdr *nlh,
2231 const struct nlattr * const nla[])
2233 const struct nft_set *set;
2235 struct sk_buff *skb2;
2236 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2239 /* Verify existance before starting dump */
2240 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2244 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2245 struct netlink_dump_control c = {
2246 .dump = nf_tables_dump_sets,
2248 return netlink_dump_start(nlsk, skb, nlh, &c);
2251 /* Only accept unspec with dump */
2252 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2253 return -EAFNOSUPPORT;
2255 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2257 return PTR_ERR(set);
2259 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2263 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2267 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2274 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2275 const struct nlmsghdr *nlh,
2276 const struct nlattr * const nla[])
2278 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2279 const struct nft_set_ops *ops;
2280 const struct nft_af_info *afi;
2281 struct net *net = sock_net(skb->sk);
2282 struct nft_table *table;
2283 struct nft_set *set;
2285 char name[IFNAMSIZ];
2288 u32 ktype, klen, dlen, dtype, flags;
2291 if (nla[NFTA_SET_TABLE] == NULL ||
2292 nla[NFTA_SET_NAME] == NULL ||
2293 nla[NFTA_SET_KEY_LEN] == NULL)
2296 ktype = NFT_DATA_VALUE;
2297 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2298 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2299 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2303 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2304 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2308 if (nla[NFTA_SET_FLAGS] != NULL) {
2309 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2310 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2311 NFT_SET_INTERVAL | NFT_SET_MAP))
2317 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2318 if (!(flags & NFT_SET_MAP))
2321 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2322 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2323 dtype != NFT_DATA_VERDICT)
2326 if (dtype != NFT_DATA_VERDICT) {
2327 if (nla[NFTA_SET_DATA_LEN] == NULL)
2329 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2331 dlen > FIELD_SIZEOF(struct nft_data, data))
2334 dlen = sizeof(struct nft_data);
2335 } else if (flags & NFT_SET_MAP)
2338 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2340 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2342 return PTR_ERR(afi);
2344 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2346 return PTR_ERR(table);
2348 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2350 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2352 if (PTR_ERR(set) != -ENOENT)
2353 return PTR_ERR(set);
2358 if (nlh->nlmsg_flags & NLM_F_EXCL)
2360 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2365 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2368 ops = nft_select_set_ops(nla);
2370 return PTR_ERR(ops);
2373 if (ops->privsize != NULL)
2374 size = ops->privsize(nla);
2377 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2381 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2382 err = nf_tables_set_alloc_name(&ctx, set, name);
2386 INIT_LIST_HEAD(&set->bindings);
2394 err = ops->init(set, nla);
2398 list_add_tail(&set->list, &table->sets);
2399 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2405 module_put(ops->owner);
2409 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2411 list_del(&set->list);
2412 if (!(set->flags & NFT_SET_ANONYMOUS))
2413 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2415 set->ops->destroy(set);
2416 module_put(set->ops->owner);
2420 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2421 const struct nlmsghdr *nlh,
2422 const struct nlattr * const nla[])
2424 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2425 struct nft_set *set;
2429 if (nla[NFTA_SET_TABLE] == NULL)
2432 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2436 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2437 return -EAFNOSUPPORT;
2439 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2441 return PTR_ERR(set);
2442 if (!list_empty(&set->bindings))
2445 nf_tables_set_destroy(&ctx, set);
2449 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2450 const struct nft_set *set,
2451 const struct nft_set_iter *iter,
2452 const struct nft_set_elem *elem)
2454 enum nft_registers dreg;
2456 dreg = nft_type_to_reg(set->dtype);
2457 return nft_validate_data_load(ctx, dreg, &elem->data,
2458 set->dtype == NFT_DATA_VERDICT ?
2459 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2462 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2463 struct nft_set_binding *binding)
2465 struct nft_set_binding *i;
2466 struct nft_set_iter iter;
2468 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2471 if (set->flags & NFT_SET_MAP) {
2472 /* If the set is already bound to the same chain all
2473 * jumps are already validated for that chain.
2475 list_for_each_entry(i, &set->bindings, list) {
2476 if (i->chain == binding->chain)
2483 iter.fn = nf_tables_bind_check_setelem;
2485 set->ops->walk(ctx, set, &iter);
2487 /* Destroy anonymous sets if binding fails */
2488 if (set->flags & NFT_SET_ANONYMOUS)
2489 nf_tables_set_destroy(ctx, set);
2495 binding->chain = ctx->chain;
2496 list_add_tail(&binding->list, &set->bindings);
2500 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2501 struct nft_set_binding *binding)
2503 list_del(&binding->list);
2505 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2506 nf_tables_set_destroy(ctx, set);
2513 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2514 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2515 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2516 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2519 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2520 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2521 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2522 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2525 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2526 const struct sk_buff *skb,
2527 const struct nlmsghdr *nlh,
2528 const struct nlattr * const nla[])
2530 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2531 const struct nft_af_info *afi;
2532 const struct nft_table *table;
2533 struct net *net = sock_net(skb->sk);
2535 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2537 return PTR_ERR(afi);
2539 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2541 return PTR_ERR(table);
2543 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2547 static int nf_tables_fill_setelem(struct sk_buff *skb,
2548 const struct nft_set *set,
2549 const struct nft_set_elem *elem)
2551 unsigned char *b = skb_tail_pointer(skb);
2552 struct nlattr *nest;
2554 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2556 goto nla_put_failure;
2558 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2560 goto nla_put_failure;
2562 if (set->flags & NFT_SET_MAP &&
2563 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2564 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2565 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2567 goto nla_put_failure;
2569 if (elem->flags != 0)
2570 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2571 goto nla_put_failure;
2573 nla_nest_end(skb, nest);
2581 struct nft_set_dump_args {
2582 const struct netlink_callback *cb;
2583 struct nft_set_iter iter;
2584 struct sk_buff *skb;
2587 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2588 const struct nft_set *set,
2589 const struct nft_set_iter *iter,
2590 const struct nft_set_elem *elem)
2592 struct nft_set_dump_args *args;
2594 args = container_of(iter, struct nft_set_dump_args, iter);
2595 return nf_tables_fill_setelem(args->skb, set, elem);
2598 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2600 const struct nft_set *set;
2601 struct nft_set_dump_args args;
2603 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2604 struct nfgenmsg *nfmsg;
2605 struct nlmsghdr *nlh;
2606 struct nlattr *nest;
2610 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2611 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2615 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2619 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2621 return PTR_ERR(set);
2623 event = NFT_MSG_NEWSETELEM;
2624 event |= NFNL_SUBSYS_NFTABLES << 8;
2625 portid = NETLINK_CB(cb->skb).portid;
2626 seq = cb->nlh->nlmsg_seq;
2628 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2631 goto nla_put_failure;
2633 nfmsg = nlmsg_data(nlh);
2634 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2635 nfmsg->version = NFNETLINK_V0;
2638 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2639 goto nla_put_failure;
2640 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2641 goto nla_put_failure;
2643 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2645 goto nla_put_failure;
2649 args.iter.skip = cb->args[0];
2650 args.iter.count = 0;
2652 args.iter.fn = nf_tables_dump_setelem;
2653 set->ops->walk(&ctx, set, &args.iter);
2655 nla_nest_end(skb, nest);
2656 nlmsg_end(skb, nlh);
2658 if (args.iter.err && args.iter.err != -EMSGSIZE)
2659 return args.iter.err;
2660 if (args.iter.count == cb->args[0])
2663 cb->args[0] = args.iter.count;
2670 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2671 const struct nlmsghdr *nlh,
2672 const struct nlattr * const nla[])
2674 const struct nft_set *set;
2678 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2682 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2684 return PTR_ERR(set);
2686 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2687 struct netlink_dump_control c = {
2688 .dump = nf_tables_dump_set,
2690 return netlink_dump_start(nlsk, skb, nlh, &c);
2695 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2696 const struct nlattr *attr)
2698 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2699 struct nft_data_desc d1, d2;
2700 struct nft_set_elem elem;
2701 struct nft_set_binding *binding;
2702 enum nft_registers dreg;
2705 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2706 nft_set_elem_policy);
2710 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2714 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2715 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2716 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2720 if (set->flags & NFT_SET_MAP) {
2721 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2722 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2725 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2729 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2733 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2737 if (set->ops->get(set, &elem) == 0)
2740 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2741 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2746 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2749 dreg = nft_type_to_reg(set->dtype);
2750 list_for_each_entry(binding, &set->bindings, list) {
2751 struct nft_ctx bind_ctx = {
2753 .table = ctx->table,
2754 .chain = binding->chain,
2757 err = nft_validate_data_load(&bind_ctx, dreg,
2758 &elem.data, d2.type);
2764 err = set->ops->insert(set, &elem);
2771 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2772 nft_data_uninit(&elem.data, d2.type);
2774 nft_data_uninit(&elem.key, d1.type);
2779 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2780 const struct nlmsghdr *nlh,
2781 const struct nlattr * const nla[])
2783 const struct nlattr *attr;
2784 struct nft_set *set;
2788 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2792 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2794 return PTR_ERR(set);
2795 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2798 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2799 err = nft_add_set_elem(&ctx, set, attr);
2806 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2807 const struct nlattr *attr)
2809 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2810 struct nft_data_desc desc;
2811 struct nft_set_elem elem;
2814 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2815 nft_set_elem_policy);
2820 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2823 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2828 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2831 err = set->ops->get(set, &elem);
2835 set->ops->remove(set, &elem);
2837 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2838 if (set->flags & NFT_SET_MAP)
2839 nft_data_uninit(&elem.data, set->dtype);
2842 nft_data_uninit(&elem.key, desc.type);
2847 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2848 const struct nlmsghdr *nlh,
2849 const struct nlattr * const nla[])
2851 const struct nlattr *attr;
2852 struct nft_set *set;
2856 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2860 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2862 return PTR_ERR(set);
2863 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2866 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2867 err = nft_del_setelem(&ctx, set, attr);
2874 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2875 [NFT_MSG_NEWTABLE] = {
2876 .call = nf_tables_newtable,
2877 .attr_count = NFTA_TABLE_MAX,
2878 .policy = nft_table_policy,
2880 [NFT_MSG_GETTABLE] = {
2881 .call = nf_tables_gettable,
2882 .attr_count = NFTA_TABLE_MAX,
2883 .policy = nft_table_policy,
2885 [NFT_MSG_DELTABLE] = {
2886 .call = nf_tables_deltable,
2887 .attr_count = NFTA_TABLE_MAX,
2888 .policy = nft_table_policy,
2890 [NFT_MSG_NEWCHAIN] = {
2891 .call = nf_tables_newchain,
2892 .attr_count = NFTA_CHAIN_MAX,
2893 .policy = nft_chain_policy,
2895 [NFT_MSG_GETCHAIN] = {
2896 .call = nf_tables_getchain,
2897 .attr_count = NFTA_CHAIN_MAX,
2898 .policy = nft_chain_policy,
2900 [NFT_MSG_DELCHAIN] = {
2901 .call = nf_tables_delchain,
2902 .attr_count = NFTA_CHAIN_MAX,
2903 .policy = nft_chain_policy,
2905 [NFT_MSG_NEWRULE] = {
2906 .call_batch = nf_tables_newrule,
2907 .attr_count = NFTA_RULE_MAX,
2908 .policy = nft_rule_policy,
2910 [NFT_MSG_GETRULE] = {
2911 .call = nf_tables_getrule,
2912 .attr_count = NFTA_RULE_MAX,
2913 .policy = nft_rule_policy,
2915 [NFT_MSG_DELRULE] = {
2916 .call_batch = nf_tables_delrule,
2917 .attr_count = NFTA_RULE_MAX,
2918 .policy = nft_rule_policy,
2920 [NFT_MSG_NEWSET] = {
2921 .call = nf_tables_newset,
2922 .attr_count = NFTA_SET_MAX,
2923 .policy = nft_set_policy,
2925 [NFT_MSG_GETSET] = {
2926 .call = nf_tables_getset,
2927 .attr_count = NFTA_SET_MAX,
2928 .policy = nft_set_policy,
2930 [NFT_MSG_DELSET] = {
2931 .call = nf_tables_delset,
2932 .attr_count = NFTA_SET_MAX,
2933 .policy = nft_set_policy,
2935 [NFT_MSG_NEWSETELEM] = {
2936 .call = nf_tables_newsetelem,
2937 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2938 .policy = nft_set_elem_list_policy,
2940 [NFT_MSG_GETSETELEM] = {
2941 .call = nf_tables_getsetelem,
2942 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2943 .policy = nft_set_elem_list_policy,
2945 [NFT_MSG_DELSETELEM] = {
2946 .call = nf_tables_delsetelem,
2947 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2948 .policy = nft_set_elem_list_policy,
2952 static const struct nfnetlink_subsystem nf_tables_subsys = {
2953 .name = "nf_tables",
2954 .subsys_id = NFNL_SUBSYS_NFTABLES,
2955 .cb_count = NFT_MSG_MAX,
2957 .commit = nf_tables_commit,
2958 .abort = nf_tables_abort,
2962 * Loop detection - walk through the ruleset beginning at the destination chain
2963 * of a new jump until either the source chain is reached (loop) or all
2964 * reachable chains have been traversed.
2966 * The loop check is performed whenever a new jump verdict is added to an
2967 * expression or verdict map or a verdict map is bound to a new chain.
2970 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2971 const struct nft_chain *chain);
2973 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2974 const struct nft_set *set,
2975 const struct nft_set_iter *iter,
2976 const struct nft_set_elem *elem)
2978 switch (elem->data.verdict) {
2981 return nf_tables_check_loops(ctx, elem->data.chain);
2987 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2988 const struct nft_chain *chain)
2990 const struct nft_rule *rule;
2991 const struct nft_expr *expr, *last;
2992 const struct nft_set *set;
2993 struct nft_set_binding *binding;
2994 struct nft_set_iter iter;
2996 if (ctx->chain == chain)
2999 list_for_each_entry(rule, &chain->rules, list) {
3000 nft_rule_for_each_expr(expr, last, rule) {
3001 const struct nft_data *data = NULL;
3004 if (!expr->ops->validate)
3007 err = expr->ops->validate(ctx, expr, &data);
3014 switch (data->verdict) {
3017 err = nf_tables_check_loops(ctx, data->chain);
3026 list_for_each_entry(set, &ctx->table->sets, list) {
3027 if (!(set->flags & NFT_SET_MAP) ||
3028 set->dtype != NFT_DATA_VERDICT)
3031 list_for_each_entry(binding, &set->bindings, list) {
3032 if (binding->chain != chain)
3038 iter.fn = nf_tables_loop_check_setelem;
3040 set->ops->walk(ctx, set, &iter);
3050 * nft_validate_input_register - validate an expressions' input register
3052 * @reg: the register number
3054 * Validate that the input register is one of the general purpose
3057 int nft_validate_input_register(enum nft_registers reg)
3059 if (reg <= NFT_REG_VERDICT)
3061 if (reg > NFT_REG_MAX)
3065 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3068 * nft_validate_output_register - validate an expressions' output register
3070 * @reg: the register number
3072 * Validate that the output register is one of the general purpose
3073 * registers or the verdict register.
3075 int nft_validate_output_register(enum nft_registers reg)
3077 if (reg < NFT_REG_VERDICT)
3079 if (reg > NFT_REG_MAX)
3083 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3086 * nft_validate_data_load - validate an expressions' data load
3088 * @ctx: context of the expression performing the load
3089 * @reg: the destination register number
3090 * @data: the data to load
3091 * @type: the data type
3093 * Validate that a data load uses the appropriate data type for
3094 * the destination register. A value of NULL for the data means
3095 * that its runtime gathered data, which is always of type
3098 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3099 const struct nft_data *data,
3100 enum nft_data_types type)
3105 case NFT_REG_VERDICT:
3106 if (data == NULL || type != NFT_DATA_VERDICT)
3109 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3110 err = nf_tables_check_loops(ctx, data->chain);
3114 if (ctx->chain->level + 1 > data->chain->level) {
3115 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3117 data->chain->level = ctx->chain->level + 1;
3123 if (data != NULL && type != NFT_DATA_VALUE)
3128 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3130 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3131 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3132 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3133 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3136 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3137 struct nft_data_desc *desc, const struct nlattr *nla)
3139 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3140 struct nft_chain *chain;
3143 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3147 if (!tb[NFTA_VERDICT_CODE])
3149 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3151 switch (data->verdict) {
3158 desc->len = sizeof(data->verdict);
3162 if (!tb[NFTA_VERDICT_CHAIN])
3164 chain = nf_tables_chain_lookup(ctx->table,
3165 tb[NFTA_VERDICT_CHAIN]);
3167 return PTR_ERR(chain);
3168 if (chain->flags & NFT_BASE_CHAIN)
3172 data->chain = chain;
3173 desc->len = sizeof(data);
3179 desc->type = NFT_DATA_VERDICT;
3183 static void nft_verdict_uninit(const struct nft_data *data)
3185 switch (data->verdict) {
3193 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3195 struct nlattr *nest;
3197 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3199 goto nla_put_failure;
3201 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3202 goto nla_put_failure;
3204 switch (data->verdict) {
3207 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3208 goto nla_put_failure;
3210 nla_nest_end(skb, nest);
3217 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3218 struct nft_data_desc *desc, const struct nlattr *nla)
3225 if (len > sizeof(data->data))
3228 nla_memcpy(data->data, nla, sizeof(data->data));
3229 desc->type = NFT_DATA_VALUE;
3234 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3237 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3240 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3241 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3242 .len = FIELD_SIZEOF(struct nft_data, data) },
3243 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3247 * nft_data_init - parse nf_tables data netlink attributes
3249 * @ctx: context of the expression using the data
3250 * @data: destination struct nft_data
3251 * @desc: data description
3252 * @nla: netlink attribute containing data
3254 * Parse the netlink data attributes and initialize a struct nft_data.
3255 * The type and length of data are returned in the data description.
3257 * The caller can indicate that it only wants to accept data of type
3258 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3260 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3261 struct nft_data_desc *desc, const struct nlattr *nla)
3263 struct nlattr *tb[NFTA_DATA_MAX + 1];
3266 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3270 if (tb[NFTA_DATA_VALUE])
3271 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3272 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3273 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3276 EXPORT_SYMBOL_GPL(nft_data_init);
3279 * nft_data_uninit - release a nft_data item
3281 * @data: struct nft_data to release
3282 * @type: type of data
3284 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3285 * all others need to be released by calling this function.
3287 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3290 case NFT_DATA_VALUE:
3292 case NFT_DATA_VERDICT:
3293 return nft_verdict_uninit(data);
3298 EXPORT_SYMBOL_GPL(nft_data_uninit);
3300 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3301 enum nft_data_types type, unsigned int len)
3303 struct nlattr *nest;
3306 nest = nla_nest_start(skb, attr);
3311 case NFT_DATA_VALUE:
3312 err = nft_value_dump(skb, data, len);
3314 case NFT_DATA_VERDICT:
3315 err = nft_verdict_dump(skb, data);
3322 nla_nest_end(skb, nest);
3325 EXPORT_SYMBOL_GPL(nft_data_dump);
3327 static int nf_tables_init_net(struct net *net)
3329 INIT_LIST_HEAD(&net->nft.af_info);
3330 INIT_LIST_HEAD(&net->nft.commit_list);
3334 static struct pernet_operations nf_tables_net_ops = {
3335 .init = nf_tables_init_net,
3338 static int __init nf_tables_module_init(void)
3342 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3349 err = nf_tables_core_module_init();
3353 err = nfnetlink_subsys_register(&nf_tables_subsys);
3357 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3358 return register_pernet_subsys(&nf_tables_net_ops);
3360 nf_tables_core_module_exit();
3367 static void __exit nf_tables_module_exit(void)
3369 unregister_pernet_subsys(&nf_tables_net_ops);
3370 nfnetlink_subsys_unregister(&nf_tables_subsys);
3371 nf_tables_core_module_exit();
3375 module_init(nf_tables_module_init);
3376 module_exit(nf_tables_module_exit);
3378 MODULE_LICENSE("GPL");
3379 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3380 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);