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_rcu(&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);
54 list_del_rcu(&afi->list);
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);
91 static void nft_ctx_init(struct nft_ctx *ctx,
92 const struct sk_buff *skb,
93 const struct nlmsghdr *nlh,
94 struct nft_af_info *afi,
95 struct nft_table *table,
96 struct nft_chain *chain,
97 const struct nlattr * const *nla)
99 ctx->net = sock_net(skb->sk);
104 ctx->portid = NETLINK_CB(skb).portid;
105 ctx->report = nlmsg_report(nlh);
106 ctx->seq = nlh->nlmsg_seq;
109 static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
112 struct nft_trans *trans;
114 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
118 trans->msg_type = msg_type;
124 static void nft_trans_destroy(struct nft_trans *trans)
126 list_del(&trans->list);
134 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
135 const struct nlattr *nla)
137 struct nft_table *table;
139 list_for_each_entry(table, &afi->tables, list) {
140 if (!nla_strcmp(nla, table->name))
146 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
147 const struct nlattr *nla)
149 struct nft_table *table;
152 return ERR_PTR(-EINVAL);
154 table = nft_table_lookup(afi, nla);
158 return ERR_PTR(-ENOENT);
161 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
163 return ++table->hgenerator;
166 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
168 static const struct nf_chain_type *
169 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
173 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
174 if (chain_type[family][i] != NULL &&
175 !nla_strcmp(nla, chain_type[family][i]->name))
176 return chain_type[family][i];
181 static const struct nf_chain_type *
182 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
183 const struct nlattr *nla,
186 const struct nf_chain_type *type;
188 type = __nf_tables_chain_type_lookup(afi->family, nla);
191 #ifdef CONFIG_MODULES
193 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
194 request_module("nft-chain-%u-%.*s", afi->family,
195 nla_len(nla), (const char *)nla_data(nla));
196 nfnl_lock(NFNL_SUBSYS_NFTABLES);
197 type = __nf_tables_chain_type_lookup(afi->family, nla);
199 return ERR_PTR(-EAGAIN);
202 return ERR_PTR(-ENOENT);
205 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
206 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
207 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
210 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
211 int event, u32 flags, int family,
212 const struct nft_table *table)
214 struct nlmsghdr *nlh;
215 struct nfgenmsg *nfmsg;
217 event |= NFNL_SUBSYS_NFTABLES << 8;
218 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
220 goto nla_put_failure;
222 nfmsg = nlmsg_data(nlh);
223 nfmsg->nfgen_family = family;
224 nfmsg->version = NFNETLINK_V0;
227 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
228 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
229 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
230 goto nla_put_failure;
232 return nlmsg_end(skb, nlh);
235 nlmsg_trim(skb, nlh);
239 static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
245 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
249 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
253 err = nf_tables_fill_table_info(skb, ctx->portid, ctx->seq, event, 0,
254 ctx->afi->family, ctx->table);
260 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
261 ctx->report, GFP_KERNEL);
264 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
270 static int nf_tables_dump_tables(struct sk_buff *skb,
271 struct netlink_callback *cb)
273 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
274 const struct nft_af_info *afi;
275 const struct nft_table *table;
276 unsigned int idx = 0, s_idx = cb->args[0];
277 struct net *net = sock_net(skb->sk);
278 int family = nfmsg->nfgen_family;
281 cb->seq = net->nft.base_seq;
283 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
284 if (family != NFPROTO_UNSPEC && family != afi->family)
287 list_for_each_entry_rcu(table, &afi->tables, list) {
291 memset(&cb->args[1], 0,
292 sizeof(cb->args) - sizeof(cb->args[0]));
293 if (nf_tables_fill_table_info(skb,
294 NETLINK_CB(cb->skb).portid,
298 afi->family, table) < 0)
301 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
312 /* Internal table flags */
313 #define NFT_TABLE_INACTIVE (1 << 15)
315 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
316 const struct nlmsghdr *nlh,
317 const struct nlattr * const nla[])
319 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
320 const struct nft_af_info *afi;
321 const struct nft_table *table;
322 struct sk_buff *skb2;
323 struct net *net = sock_net(skb->sk);
324 int family = nfmsg->nfgen_family;
327 if (nlh->nlmsg_flags & NLM_F_DUMP) {
328 struct netlink_dump_control c = {
329 .dump = nf_tables_dump_tables,
331 return netlink_dump_start(nlsk, skb, nlh, &c);
334 afi = nf_tables_afinfo_lookup(net, family, false);
338 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
340 return PTR_ERR(table);
341 if (table->flags & NFT_TABLE_INACTIVE)
344 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
348 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
349 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
354 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
361 static int nf_tables_table_enable(const struct nft_af_info *afi,
362 struct nft_table *table)
364 struct nft_chain *chain;
367 list_for_each_entry(chain, &table->chains, list) {
368 if (!(chain->flags & NFT_BASE_CHAIN))
371 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
379 list_for_each_entry(chain, &table->chains, list) {
380 if (!(chain->flags & NFT_BASE_CHAIN))
386 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
391 static void nf_tables_table_disable(const struct nft_af_info *afi,
392 struct nft_table *table)
394 struct nft_chain *chain;
396 list_for_each_entry(chain, &table->chains, list) {
397 if (chain->flags & NFT_BASE_CHAIN)
398 nf_unregister_hooks(nft_base_chain(chain)->ops,
403 static int nf_tables_updtable(struct nft_ctx *ctx)
405 struct nft_trans *trans;
409 if (!ctx->nla[NFTA_TABLE_FLAGS])
412 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
413 if (flags & ~NFT_TABLE_F_DORMANT)
416 if (flags == ctx->table->flags)
419 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
420 sizeof(struct nft_trans_table));
424 if ((flags & NFT_TABLE_F_DORMANT) &&
425 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
426 nft_trans_table_enable(trans) = false;
427 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
428 ctx->table->flags & NFT_TABLE_F_DORMANT) {
429 ret = nf_tables_table_enable(ctx->afi, ctx->table);
431 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
432 nft_trans_table_enable(trans) = true;
438 nft_trans_table_update(trans) = true;
439 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
442 nft_trans_destroy(trans);
446 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
448 struct nft_trans *trans;
450 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
454 if (msg_type == NFT_MSG_NEWTABLE)
455 ctx->table->flags |= NFT_TABLE_INACTIVE;
457 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
461 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
462 const struct nlmsghdr *nlh,
463 const struct nlattr * const nla[])
465 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
466 const struct nlattr *name;
467 struct nft_af_info *afi;
468 struct nft_table *table;
469 struct net *net = sock_net(skb->sk);
470 int family = nfmsg->nfgen_family;
475 afi = nf_tables_afinfo_lookup(net, family, true);
479 name = nla[NFTA_TABLE_NAME];
480 table = nf_tables_table_lookup(afi, name);
482 if (PTR_ERR(table) != -ENOENT)
483 return PTR_ERR(table);
488 if (table->flags & NFT_TABLE_INACTIVE)
490 if (nlh->nlmsg_flags & NLM_F_EXCL)
492 if (nlh->nlmsg_flags & NLM_F_REPLACE)
495 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
496 return nf_tables_updtable(&ctx);
499 if (nla[NFTA_TABLE_FLAGS]) {
500 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
501 if (flags & ~NFT_TABLE_F_DORMANT)
505 if (!try_module_get(afi->owner))
506 return -EAFNOSUPPORT;
508 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
510 module_put(afi->owner);
514 nla_strlcpy(table->name, name, nla_len(name));
515 INIT_LIST_HEAD(&table->chains);
516 INIT_LIST_HEAD(&table->sets);
517 table->flags = flags;
519 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
520 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
523 module_put(afi->owner);
526 list_add_tail_rcu(&table->list, &afi->tables);
530 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
531 const struct nlmsghdr *nlh,
532 const struct nlattr * const nla[])
534 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
535 struct nft_af_info *afi;
536 struct nft_table *table;
537 struct net *net = sock_net(skb->sk);
538 int family = nfmsg->nfgen_family, err;
541 afi = nf_tables_afinfo_lookup(net, family, false);
545 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
547 return PTR_ERR(table);
548 if (table->flags & NFT_TABLE_INACTIVE)
553 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
554 err = nft_trans_table_add(&ctx, NFT_MSG_DELTABLE);
558 list_del_rcu(&table->list);
562 static void nf_tables_table_destroy(struct nft_ctx *ctx)
564 BUG_ON(ctx->table->use > 0);
567 module_put(ctx->afi->owner);
570 int nft_register_chain_type(const struct nf_chain_type *ctype)
574 nfnl_lock(NFNL_SUBSYS_NFTABLES);
575 if (chain_type[ctype->family][ctype->type] != NULL) {
579 chain_type[ctype->family][ctype->type] = ctype;
581 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
584 EXPORT_SYMBOL_GPL(nft_register_chain_type);
586 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
588 nfnl_lock(NFNL_SUBSYS_NFTABLES);
589 chain_type[ctype->family][ctype->type] = NULL;
590 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
592 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
598 static struct nft_chain *
599 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
601 struct nft_chain *chain;
603 list_for_each_entry(chain, &table->chains, list) {
604 if (chain->handle == handle)
608 return ERR_PTR(-ENOENT);
611 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
612 const struct nlattr *nla)
614 struct nft_chain *chain;
617 return ERR_PTR(-EINVAL);
619 list_for_each_entry(chain, &table->chains, list) {
620 if (!nla_strcmp(nla, chain->name))
624 return ERR_PTR(-ENOENT);
627 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
628 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
629 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
630 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
631 .len = NFT_CHAIN_MAXNAMELEN - 1 },
632 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
633 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
634 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
635 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
638 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
639 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
640 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
643 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
645 struct nft_stats *cpu_stats, total;
651 memset(&total, 0, sizeof(total));
652 for_each_possible_cpu(cpu) {
653 cpu_stats = per_cpu_ptr(stats, cpu);
655 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
656 pkts = cpu_stats->pkts;
657 bytes = cpu_stats->bytes;
658 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
660 total.bytes += bytes;
662 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
664 goto nla_put_failure;
666 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
667 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
668 goto nla_put_failure;
670 nla_nest_end(skb, nest);
677 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
678 int event, u32 flags, int family,
679 const struct nft_table *table,
680 const struct nft_chain *chain)
682 struct nlmsghdr *nlh;
683 struct nfgenmsg *nfmsg;
685 event |= NFNL_SUBSYS_NFTABLES << 8;
686 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
688 goto nla_put_failure;
690 nfmsg = nlmsg_data(nlh);
691 nfmsg->nfgen_family = family;
692 nfmsg->version = NFNETLINK_V0;
695 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
696 goto nla_put_failure;
697 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
698 goto nla_put_failure;
699 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
700 goto nla_put_failure;
702 if (chain->flags & NFT_BASE_CHAIN) {
703 const struct nft_base_chain *basechain = nft_base_chain(chain);
704 const struct nf_hook_ops *ops = &basechain->ops[0];
707 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
709 goto nla_put_failure;
710 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
711 goto nla_put_failure;
712 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
713 goto nla_put_failure;
714 nla_nest_end(skb, nest);
716 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
717 htonl(basechain->policy)))
718 goto nla_put_failure;
720 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
721 goto nla_put_failure;
723 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
724 goto nla_put_failure;
727 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
728 goto nla_put_failure;
730 return nlmsg_end(skb, nlh);
733 nlmsg_trim(skb, nlh);
737 static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
743 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
747 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
751 err = nf_tables_fill_chain_info(skb, ctx->portid, ctx->seq, event, 0,
752 ctx->afi->family, ctx->table,
759 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
760 ctx->report, GFP_KERNEL);
763 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
769 static int nf_tables_dump_chains(struct sk_buff *skb,
770 struct netlink_callback *cb)
772 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
773 const struct nft_af_info *afi;
774 const struct nft_table *table;
775 const struct nft_chain *chain;
776 unsigned int idx = 0, s_idx = cb->args[0];
777 struct net *net = sock_net(skb->sk);
778 int family = nfmsg->nfgen_family;
781 cb->seq = net->nft.base_seq;
783 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
784 if (family != NFPROTO_UNSPEC && family != afi->family)
787 list_for_each_entry_rcu(table, &afi->tables, list) {
788 list_for_each_entry_rcu(chain, &table->chains, list) {
792 memset(&cb->args[1], 0,
793 sizeof(cb->args) - sizeof(cb->args[0]));
794 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
798 afi->family, table, chain) < 0)
801 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
813 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
814 const struct nlmsghdr *nlh,
815 const struct nlattr * const nla[])
817 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
818 const struct nft_af_info *afi;
819 const struct nft_table *table;
820 const struct nft_chain *chain;
821 struct sk_buff *skb2;
822 struct net *net = sock_net(skb->sk);
823 int family = nfmsg->nfgen_family;
826 if (nlh->nlmsg_flags & NLM_F_DUMP) {
827 struct netlink_dump_control c = {
828 .dump = nf_tables_dump_chains,
830 return netlink_dump_start(nlsk, skb, nlh, &c);
833 afi = nf_tables_afinfo_lookup(net, family, false);
837 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
839 return PTR_ERR(table);
840 if (table->flags & NFT_TABLE_INACTIVE)
843 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
845 return PTR_ERR(chain);
846 if (chain->flags & NFT_CHAIN_INACTIVE)
849 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
853 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
854 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
855 family, table, chain);
859 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
866 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
867 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
868 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
871 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
873 struct nlattr *tb[NFTA_COUNTER_MAX+1];
874 struct nft_stats __percpu *newstats;
875 struct nft_stats *stats;
878 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
882 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
883 return ERR_PTR(-EINVAL);
885 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
886 if (newstats == NULL)
887 return ERR_PTR(-ENOMEM);
889 /* Restore old counters on this cpu, no problem. Per-cpu statistics
890 * are not exposed to userspace.
892 stats = this_cpu_ptr(newstats);
893 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
894 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
899 static void nft_chain_stats_replace(struct nft_base_chain *chain,
900 struct nft_stats __percpu *newstats)
903 struct nft_stats __percpu *oldstats =
904 nft_dereference(chain->stats);
906 rcu_assign_pointer(chain->stats, newstats);
908 free_percpu(oldstats);
910 rcu_assign_pointer(chain->stats, newstats);
913 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
915 struct nft_trans *trans;
917 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
921 if (msg_type == NFT_MSG_NEWCHAIN)
922 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
924 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
928 static void nf_tables_chain_destroy(struct nft_chain *chain)
930 BUG_ON(chain->use > 0);
932 if (chain->flags & NFT_BASE_CHAIN) {
933 module_put(nft_base_chain(chain)->type->owner);
934 free_percpu(nft_base_chain(chain)->stats);
935 kfree(nft_base_chain(chain));
941 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
942 const struct nlmsghdr *nlh,
943 const struct nlattr * const nla[])
945 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
946 const struct nlattr * uninitialized_var(name);
947 struct nft_af_info *afi;
948 struct nft_table *table;
949 struct nft_chain *chain;
950 struct nft_base_chain *basechain = NULL;
951 struct nlattr *ha[NFTA_HOOK_MAX + 1];
952 struct net *net = sock_net(skb->sk);
953 int family = nfmsg->nfgen_family;
954 u8 policy = NF_ACCEPT;
957 struct nft_stats __percpu *stats;
962 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
964 afi = nf_tables_afinfo_lookup(net, family, true);
968 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
970 return PTR_ERR(table);
973 name = nla[NFTA_CHAIN_NAME];
975 if (nla[NFTA_CHAIN_HANDLE]) {
976 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
977 chain = nf_tables_chain_lookup_byhandle(table, handle);
979 return PTR_ERR(chain);
981 chain = nf_tables_chain_lookup(table, name);
983 if (PTR_ERR(chain) != -ENOENT)
984 return PTR_ERR(chain);
989 if (nla[NFTA_CHAIN_POLICY]) {
990 if ((chain != NULL &&
991 !(chain->flags & NFT_BASE_CHAIN)) ||
992 nla[NFTA_CHAIN_HOOK] == NULL)
995 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1005 if (chain != NULL) {
1006 struct nft_stats *stats = NULL;
1007 struct nft_trans *trans;
1009 if (chain->flags & NFT_CHAIN_INACTIVE)
1011 if (nlh->nlmsg_flags & NLM_F_EXCL)
1013 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1016 if (nla[NFTA_CHAIN_HANDLE] && name &&
1017 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1020 if (nla[NFTA_CHAIN_COUNTERS]) {
1021 if (!(chain->flags & NFT_BASE_CHAIN))
1024 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1026 return PTR_ERR(stats);
1029 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1030 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1031 sizeof(struct nft_trans_chain));
1035 nft_trans_chain_stats(trans) = stats;
1036 nft_trans_chain_update(trans) = true;
1038 if (nla[NFTA_CHAIN_POLICY])
1039 nft_trans_chain_policy(trans) = policy;
1041 nft_trans_chain_policy(trans) = -1;
1043 if (nla[NFTA_CHAIN_HANDLE] && name) {
1044 nla_strlcpy(nft_trans_chain_name(trans), name,
1045 NFT_CHAIN_MAXNAMELEN);
1047 list_add_tail(&trans->list, &net->nft.commit_list);
1051 if (table->use == UINT_MAX)
1054 if (nla[NFTA_CHAIN_HOOK]) {
1055 const struct nf_chain_type *type;
1056 struct nf_hook_ops *ops;
1058 u32 hooknum, priority;
1060 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1061 if (nla[NFTA_CHAIN_TYPE]) {
1062 type = nf_tables_chain_type_lookup(afi,
1063 nla[NFTA_CHAIN_TYPE],
1066 return PTR_ERR(type);
1069 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1073 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1074 ha[NFTA_HOOK_PRIORITY] == NULL)
1077 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1078 if (hooknum >= afi->nhooks)
1080 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1082 if (!(type->hook_mask & (1 << hooknum)))
1084 if (!try_module_get(type->owner))
1086 hookfn = type->hooks[hooknum];
1088 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1089 if (basechain == NULL)
1092 if (nla[NFTA_CHAIN_COUNTERS]) {
1093 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1094 if (IS_ERR(stats)) {
1095 module_put(type->owner);
1097 return PTR_ERR(stats);
1099 basechain->stats = stats;
1101 stats = netdev_alloc_pcpu_stats(struct nft_stats);
1102 if (IS_ERR(stats)) {
1103 module_put(type->owner);
1105 return PTR_ERR(stats);
1107 rcu_assign_pointer(basechain->stats, stats);
1110 basechain->type = type;
1111 chain = &basechain->chain;
1113 for (i = 0; i < afi->nops; i++) {
1114 ops = &basechain->ops[i];
1116 ops->owner = afi->owner;
1117 ops->hooknum = hooknum;
1118 ops->priority = priority;
1120 ops->hook = afi->hooks[ops->hooknum];
1123 if (afi->hook_ops_init)
1124 afi->hook_ops_init(ops, i);
1127 chain->flags |= NFT_BASE_CHAIN;
1128 basechain->policy = policy;
1130 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1135 INIT_LIST_HEAD(&chain->rules);
1136 chain->handle = nf_tables_alloc_handle(table);
1138 chain->table = table;
1139 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1141 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1142 chain->flags & NFT_BASE_CHAIN) {
1143 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1148 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1149 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1154 list_add_tail_rcu(&chain->list, &table->chains);
1157 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1158 chain->flags & NFT_BASE_CHAIN) {
1159 nf_unregister_hooks(nft_base_chain(chain)->ops,
1163 nf_tables_chain_destroy(chain);
1167 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1168 const struct nlmsghdr *nlh,
1169 const struct nlattr * const nla[])
1171 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1172 struct nft_af_info *afi;
1173 struct nft_table *table;
1174 struct nft_chain *chain;
1175 struct net *net = sock_net(skb->sk);
1176 int family = nfmsg->nfgen_family;
1180 afi = nf_tables_afinfo_lookup(net, family, false);
1182 return PTR_ERR(afi);
1184 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1186 return PTR_ERR(table);
1187 if (table->flags & NFT_TABLE_INACTIVE)
1190 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1192 return PTR_ERR(chain);
1193 if (chain->flags & NFT_CHAIN_INACTIVE)
1198 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1199 err = nft_trans_chain_add(&ctx, NFT_MSG_DELCHAIN);
1204 list_del_rcu(&chain->list);
1213 * nft_register_expr - register nf_tables expr type
1216 * Registers the expr type for use with nf_tables. Returns zero on
1217 * success or a negative errno code otherwise.
1219 int nft_register_expr(struct nft_expr_type *type)
1221 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1222 if (type->family == NFPROTO_UNSPEC)
1223 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1225 list_add_rcu(&type->list, &nf_tables_expressions);
1226 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1229 EXPORT_SYMBOL_GPL(nft_register_expr);
1232 * nft_unregister_expr - unregister nf_tables expr type
1235 * Unregisters the expr typefor use with nf_tables.
1237 void nft_unregister_expr(struct nft_expr_type *type)
1239 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1240 list_del_rcu(&type->list);
1241 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1243 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1245 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1248 const struct nft_expr_type *type;
1250 list_for_each_entry(type, &nf_tables_expressions, list) {
1251 if (!nla_strcmp(nla, type->name) &&
1252 (!type->family || type->family == family))
1258 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1261 const struct nft_expr_type *type;
1264 return ERR_PTR(-EINVAL);
1266 type = __nft_expr_type_get(family, nla);
1267 if (type != NULL && try_module_get(type->owner))
1270 #ifdef CONFIG_MODULES
1272 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1273 request_module("nft-expr-%u-%.*s", family,
1274 nla_len(nla), (char *)nla_data(nla));
1275 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1276 if (__nft_expr_type_get(family, nla))
1277 return ERR_PTR(-EAGAIN);
1279 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1280 request_module("nft-expr-%.*s",
1281 nla_len(nla), (char *)nla_data(nla));
1282 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1283 if (__nft_expr_type_get(family, nla))
1284 return ERR_PTR(-EAGAIN);
1287 return ERR_PTR(-ENOENT);
1290 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1291 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1292 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1295 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1296 const struct nft_expr *expr)
1298 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1299 goto nla_put_failure;
1301 if (expr->ops->dump) {
1302 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1304 goto nla_put_failure;
1305 if (expr->ops->dump(skb, expr) < 0)
1306 goto nla_put_failure;
1307 nla_nest_end(skb, data);
1316 struct nft_expr_info {
1317 const struct nft_expr_ops *ops;
1318 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1321 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1322 const struct nlattr *nla,
1323 struct nft_expr_info *info)
1325 const struct nft_expr_type *type;
1326 const struct nft_expr_ops *ops;
1327 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1330 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1334 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1336 return PTR_ERR(type);
1338 if (tb[NFTA_EXPR_DATA]) {
1339 err = nla_parse_nested(info->tb, type->maxattr,
1340 tb[NFTA_EXPR_DATA], type->policy);
1344 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1346 if (type->select_ops != NULL) {
1347 ops = type->select_ops(ctx,
1348 (const struct nlattr * const *)info->tb);
1360 module_put(type->owner);
1364 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1365 const struct nft_expr_info *info,
1366 struct nft_expr *expr)
1368 const struct nft_expr_ops *ops = info->ops;
1373 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1385 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1386 struct nft_expr *expr)
1388 if (expr->ops->destroy)
1389 expr->ops->destroy(ctx, expr);
1390 module_put(expr->ops->type->owner);
1397 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1400 struct nft_rule *rule;
1402 // FIXME: this sucks
1403 list_for_each_entry(rule, &chain->rules, list) {
1404 if (handle == rule->handle)
1408 return ERR_PTR(-ENOENT);
1411 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1412 const struct nlattr *nla)
1415 return ERR_PTR(-EINVAL);
1417 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1420 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1421 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1422 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1423 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1424 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1425 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1426 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1427 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1428 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1429 .len = NFT_USERDATA_MAXLEN },
1432 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1433 int event, u32 flags, int family,
1434 const struct nft_table *table,
1435 const struct nft_chain *chain,
1436 const struct nft_rule *rule)
1438 struct nlmsghdr *nlh;
1439 struct nfgenmsg *nfmsg;
1440 const struct nft_expr *expr, *next;
1441 struct nlattr *list;
1442 const struct nft_rule *prule;
1443 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1445 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1448 goto nla_put_failure;
1450 nfmsg = nlmsg_data(nlh);
1451 nfmsg->nfgen_family = family;
1452 nfmsg->version = NFNETLINK_V0;
1455 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1456 goto nla_put_failure;
1457 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1458 goto nla_put_failure;
1459 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1460 goto nla_put_failure;
1462 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1463 prule = list_entry(rule->list.prev, struct nft_rule, list);
1464 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1465 cpu_to_be64(prule->handle)))
1466 goto nla_put_failure;
1469 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1471 goto nla_put_failure;
1472 nft_rule_for_each_expr(expr, next, rule) {
1473 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1475 goto nla_put_failure;
1476 if (nf_tables_fill_expr_info(skb, expr) < 0)
1477 goto nla_put_failure;
1478 nla_nest_end(skb, elem);
1480 nla_nest_end(skb, list);
1483 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1484 goto nla_put_failure;
1486 return nlmsg_end(skb, nlh);
1489 nlmsg_trim(skb, nlh);
1493 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1494 const struct nft_rule *rule,
1497 struct sk_buff *skb;
1501 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1505 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1509 err = nf_tables_fill_rule_info(skb, ctx->portid, ctx->seq, event, 0,
1510 ctx->afi->family, ctx->table,
1517 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1518 ctx->report, GFP_KERNEL);
1521 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1528 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1530 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1533 static inline int gencursor_next(struct net *net)
1535 return net->nft.gencursor+1 == 1 ? 1 : 0;
1539 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1541 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1545 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1547 /* Now inactive, will be active in the future */
1548 rule->genmask = (1 << net->nft.gencursor);
1552 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1554 rule->genmask = (1 << gencursor_next(net));
1557 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1562 static int nf_tables_dump_rules(struct sk_buff *skb,
1563 struct netlink_callback *cb)
1565 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1566 const struct nft_af_info *afi;
1567 const struct nft_table *table;
1568 const struct nft_chain *chain;
1569 const struct nft_rule *rule;
1570 unsigned int idx = 0, s_idx = cb->args[0];
1571 struct net *net = sock_net(skb->sk);
1572 int family = nfmsg->nfgen_family;
1575 cb->seq = net->nft.base_seq;
1577 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1578 if (family != NFPROTO_UNSPEC && family != afi->family)
1581 list_for_each_entry_rcu(table, &afi->tables, list) {
1582 list_for_each_entry_rcu(chain, &table->chains, list) {
1583 list_for_each_entry_rcu(rule, &chain->rules, list) {
1584 if (!nft_rule_is_active(net, rule))
1589 memset(&cb->args[1], 0,
1590 sizeof(cb->args) - sizeof(cb->args[0]));
1591 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1594 NLM_F_MULTI | NLM_F_APPEND,
1595 afi->family, table, chain, rule) < 0)
1598 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1612 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1613 const struct nlmsghdr *nlh,
1614 const struct nlattr * const nla[])
1616 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1617 const struct nft_af_info *afi;
1618 const struct nft_table *table;
1619 const struct nft_chain *chain;
1620 const struct nft_rule *rule;
1621 struct sk_buff *skb2;
1622 struct net *net = sock_net(skb->sk);
1623 int family = nfmsg->nfgen_family;
1626 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1627 struct netlink_dump_control c = {
1628 .dump = nf_tables_dump_rules,
1630 return netlink_dump_start(nlsk, skb, nlh, &c);
1633 afi = nf_tables_afinfo_lookup(net, family, false);
1635 return PTR_ERR(afi);
1637 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1639 return PTR_ERR(table);
1640 if (table->flags & NFT_TABLE_INACTIVE)
1643 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1645 return PTR_ERR(chain);
1646 if (chain->flags & NFT_CHAIN_INACTIVE)
1649 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1651 return PTR_ERR(rule);
1653 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1657 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1658 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1659 family, table, chain, rule);
1663 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1670 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1671 struct nft_rule *rule)
1673 struct nft_expr *expr;
1676 * Careful: some expressions might not be initialized in case this
1677 * is called on error from nf_tables_newrule().
1679 expr = nft_expr_first(rule);
1680 while (expr->ops && expr != nft_expr_last(rule)) {
1681 nf_tables_expr_destroy(ctx, expr);
1682 expr = nft_expr_next(expr);
1687 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
1688 struct nft_rule *rule)
1690 struct nft_trans *trans;
1692 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
1696 nft_trans_rule(trans) = rule;
1697 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1702 #define NFT_RULE_MAXEXPRS 128
1704 static struct nft_expr_info *info;
1706 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1707 const struct nlmsghdr *nlh,
1708 const struct nlattr * const nla[])
1710 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1711 struct nft_af_info *afi;
1712 struct net *net = sock_net(skb->sk);
1713 struct nft_table *table;
1714 struct nft_chain *chain;
1715 struct nft_rule *rule, *old_rule = NULL;
1716 struct nft_trans *trans = NULL;
1717 struct nft_expr *expr;
1720 unsigned int size, i, n, ulen = 0;
1723 u64 handle, pos_handle;
1725 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1727 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1729 return PTR_ERR(afi);
1731 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1733 return PTR_ERR(table);
1735 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1737 return PTR_ERR(chain);
1739 if (nla[NFTA_RULE_HANDLE]) {
1740 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1741 rule = __nf_tables_rule_lookup(chain, handle);
1743 return PTR_ERR(rule);
1745 if (nlh->nlmsg_flags & NLM_F_EXCL)
1747 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1752 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1754 handle = nf_tables_alloc_handle(table);
1756 if (chain->use == UINT_MAX)
1760 if (nla[NFTA_RULE_POSITION]) {
1761 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1764 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1765 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1766 if (IS_ERR(old_rule))
1767 return PTR_ERR(old_rule);
1770 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1774 if (nla[NFTA_RULE_EXPRESSIONS]) {
1775 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1777 if (nla_type(tmp) != NFTA_LIST_ELEM)
1779 if (n == NFT_RULE_MAXEXPRS)
1781 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1784 size += info[n].ops->size;
1789 if (nla[NFTA_RULE_USERDATA])
1790 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1793 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1797 nft_rule_activate_next(net, rule);
1799 rule->handle = handle;
1804 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1806 expr = nft_expr_first(rule);
1807 for (i = 0; i < n; i++) {
1808 err = nf_tables_newexpr(&ctx, &info[i], expr);
1812 expr = nft_expr_next(expr);
1815 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1816 if (nft_rule_is_active_next(net, old_rule)) {
1817 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
1819 if (trans == NULL) {
1823 nft_rule_disactivate_next(net, old_rule);
1825 list_add_tail_rcu(&rule->list, &old_rule->list);
1830 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1832 list_add_rcu(&rule->list, &old_rule->list);
1834 list_add_tail_rcu(&rule->list, &chain->rules);
1837 list_add_tail_rcu(&rule->list, &old_rule->list);
1839 list_add_rcu(&rule->list, &chain->rules);
1842 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
1850 list_del_rcu(&rule->list);
1852 list_del_rcu(&nft_trans_rule(trans)->list);
1853 nft_rule_clear(net, nft_trans_rule(trans));
1854 nft_trans_destroy(trans);
1858 nf_tables_rule_destroy(&ctx, rule);
1860 for (i = 0; i < n; i++) {
1861 if (info[i].ops != NULL)
1862 module_put(info[i].ops->type->owner);
1868 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1870 /* You cannot delete the same rule twice */
1871 if (nft_rule_is_active_next(ctx->net, rule)) {
1872 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
1874 nft_rule_disactivate_next(ctx->net, rule);
1881 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1883 struct nft_rule *rule;
1886 list_for_each_entry(rule, &ctx->chain->rules, list) {
1887 err = nf_tables_delrule_one(ctx, rule);
1894 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1895 const struct nlmsghdr *nlh,
1896 const struct nlattr * const nla[])
1898 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1899 struct nft_af_info *afi;
1900 struct net *net = sock_net(skb->sk);
1901 struct nft_table *table;
1902 struct nft_chain *chain = NULL;
1903 struct nft_rule *rule;
1904 int family = nfmsg->nfgen_family, err = 0;
1907 afi = nf_tables_afinfo_lookup(net, family, false);
1909 return PTR_ERR(afi);
1911 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1913 return PTR_ERR(table);
1914 if (table->flags & NFT_TABLE_INACTIVE)
1917 if (nla[NFTA_RULE_CHAIN]) {
1918 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1920 return PTR_ERR(chain);
1923 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1926 if (nla[NFTA_RULE_HANDLE]) {
1927 rule = nf_tables_rule_lookup(chain,
1928 nla[NFTA_RULE_HANDLE]);
1930 return PTR_ERR(rule);
1932 err = nf_tables_delrule_one(&ctx, rule);
1934 err = nf_table_delrule_by_chain(&ctx);
1937 list_for_each_entry(chain, &table->chains, list) {
1939 err = nf_table_delrule_by_chain(&ctx);
1952 static LIST_HEAD(nf_tables_set_ops);
1954 int nft_register_set(struct nft_set_ops *ops)
1956 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1957 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
1958 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1961 EXPORT_SYMBOL_GPL(nft_register_set);
1963 void nft_unregister_set(struct nft_set_ops *ops)
1965 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1966 list_del_rcu(&ops->list);
1967 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1969 EXPORT_SYMBOL_GPL(nft_unregister_set);
1972 * Select a set implementation based on the data characteristics and the
1973 * given policy. The total memory use might not be known if no size is
1974 * given, in that case the amount of memory per element is used.
1976 static const struct nft_set_ops *
1977 nft_select_set_ops(const struct nlattr * const nla[],
1978 const struct nft_set_desc *desc,
1979 enum nft_set_policies policy)
1981 const struct nft_set_ops *ops, *bops;
1982 struct nft_set_estimate est, best;
1985 #ifdef CONFIG_MODULES
1986 if (list_empty(&nf_tables_set_ops)) {
1987 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1988 request_module("nft-set");
1989 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1990 if (!list_empty(&nf_tables_set_ops))
1991 return ERR_PTR(-EAGAIN);
1995 if (nla[NFTA_SET_FLAGS] != NULL) {
1996 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1997 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2004 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2005 if ((ops->features & features) != features)
2007 if (!ops->estimate(desc, features, &est))
2011 case NFT_SET_POL_PERFORMANCE:
2012 if (est.class < best.class)
2014 if (est.class == best.class && est.size < best.size)
2017 case NFT_SET_POL_MEMORY:
2018 if (est.size < best.size)
2020 if (est.size == best.size && est.class < best.class)
2027 if (!try_module_get(ops->owner))
2030 module_put(bops->owner);
2039 return ERR_PTR(-EOPNOTSUPP);
2042 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2043 [NFTA_SET_TABLE] = { .type = NLA_STRING },
2044 [NFTA_SET_NAME] = { .type = NLA_STRING,
2045 .len = IFNAMSIZ - 1 },
2046 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2047 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2048 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2049 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2050 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
2051 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2052 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2053 [NFTA_SET_ID] = { .type = NLA_U32 },
2056 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2057 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
2060 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2061 const struct sk_buff *skb,
2062 const struct nlmsghdr *nlh,
2063 const struct nlattr * const nla[])
2065 struct net *net = sock_net(skb->sk);
2066 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2067 struct nft_af_info *afi = NULL;
2068 struct nft_table *table = NULL;
2070 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2071 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2073 return PTR_ERR(afi);
2076 if (nla[NFTA_SET_TABLE] != NULL) {
2078 return -EAFNOSUPPORT;
2080 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2082 return PTR_ERR(table);
2083 if (table->flags & NFT_TABLE_INACTIVE)
2087 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2091 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2092 const struct nlattr *nla)
2094 struct nft_set *set;
2097 return ERR_PTR(-EINVAL);
2099 list_for_each_entry(set, &table->sets, list) {
2100 if (!nla_strcmp(nla, set->name))
2103 return ERR_PTR(-ENOENT);
2106 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2107 const struct nlattr *nla)
2109 struct nft_trans *trans;
2110 u32 id = ntohl(nla_get_be32(nla));
2112 list_for_each_entry(trans, &net->nft.commit_list, list) {
2113 if (trans->msg_type == NFT_MSG_NEWSET &&
2114 id == nft_trans_set_id(trans))
2115 return nft_trans_set(trans);
2117 return ERR_PTR(-ENOENT);
2120 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2123 const struct nft_set *i;
2125 unsigned long *inuse;
2126 unsigned int n = 0, min = 0;
2128 p = strnchr(name, IFNAMSIZ, '%');
2130 if (p[1] != 'd' || strchr(p + 2, '%'))
2133 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2137 list_for_each_entry(i, &ctx->table->sets, list) {
2140 if (!sscanf(i->name, name, &tmp))
2142 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2145 set_bit(tmp - min, inuse);
2148 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2149 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2150 min += BITS_PER_BYTE * PAGE_SIZE;
2151 memset(inuse, 0, PAGE_SIZE);
2154 free_page((unsigned long)inuse);
2157 snprintf(set->name, sizeof(set->name), name, min + n);
2158 list_for_each_entry(i, &ctx->table->sets, list) {
2159 if (!strcmp(set->name, i->name))
2165 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2166 const struct nft_set *set, u16 event, u16 flags)
2168 struct nfgenmsg *nfmsg;
2169 struct nlmsghdr *nlh;
2170 struct nlattr *desc;
2171 u32 portid = ctx->portid;
2174 event |= NFNL_SUBSYS_NFTABLES << 8;
2175 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2178 goto nla_put_failure;
2180 nfmsg = nlmsg_data(nlh);
2181 nfmsg->nfgen_family = ctx->afi->family;
2182 nfmsg->version = NFNETLINK_V0;
2185 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2186 goto nla_put_failure;
2187 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2188 goto nla_put_failure;
2189 if (set->flags != 0)
2190 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2191 goto nla_put_failure;
2193 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2194 goto nla_put_failure;
2195 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2196 goto nla_put_failure;
2197 if (set->flags & NFT_SET_MAP) {
2198 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2199 goto nla_put_failure;
2200 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2201 goto nla_put_failure;
2204 desc = nla_nest_start(skb, NFTA_SET_DESC);
2206 goto nla_put_failure;
2208 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2209 goto nla_put_failure;
2210 nla_nest_end(skb, desc);
2212 return nlmsg_end(skb, nlh);
2215 nlmsg_trim(skb, nlh);
2219 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2220 const struct nft_set *set,
2221 int event, gfp_t gfp_flags)
2223 struct sk_buff *skb;
2224 u32 portid = ctx->portid;
2228 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2232 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2236 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2242 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2243 ctx->report, gfp_flags);
2246 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2250 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2252 const struct nft_set *set;
2253 unsigned int idx, s_idx = cb->args[0];
2254 struct nft_af_info *afi;
2255 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2256 struct net *net = sock_net(skb->sk);
2257 int cur_family = cb->args[3];
2258 struct nft_ctx *ctx = cb->data, ctx_set;
2264 cb->seq = net->nft.base_seq;
2266 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2267 if (ctx->afi && ctx->afi != afi)
2271 if (afi->family != cur_family)
2276 list_for_each_entry_rcu(table, &afi->tables, list) {
2277 if (ctx->table && ctx->table != table)
2281 if (cur_table != table)
2287 list_for_each_entry_rcu(set, &table->sets, list) {
2292 ctx_set.table = table;
2294 if (nf_tables_fill_set(skb, &ctx_set, set,
2298 cb->args[2] = (unsigned long) table;
2299 cb->args[3] = afi->family;
2302 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2316 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
2322 #define NFT_SET_INACTIVE (1 << 15) /* Internal set flag */
2324 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2325 const struct nlmsghdr *nlh,
2326 const struct nlattr * const nla[])
2328 const struct nft_set *set;
2330 struct sk_buff *skb2;
2331 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2334 /* Verify existance before starting dump */
2335 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2339 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2340 struct netlink_dump_control c = {
2341 .dump = nf_tables_dump_sets,
2342 .done = nf_tables_dump_sets_done,
2344 struct nft_ctx *ctx_dump;
2346 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2347 if (ctx_dump == NULL)
2353 return netlink_dump_start(nlsk, skb, nlh, &c);
2356 /* Only accept unspec with dump */
2357 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2358 return -EAFNOSUPPORT;
2360 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2362 return PTR_ERR(set);
2363 if (set->flags & NFT_SET_INACTIVE)
2366 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2370 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2374 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2381 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2382 struct nft_set_desc *desc,
2383 const struct nlattr *nla)
2385 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2388 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2392 if (da[NFTA_SET_DESC_SIZE] != NULL)
2393 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2398 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2399 struct nft_set *set)
2401 struct nft_trans *trans;
2403 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2407 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2408 nft_trans_set_id(trans) =
2409 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2410 set->flags |= NFT_SET_INACTIVE;
2412 nft_trans_set(trans) = set;
2413 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2418 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2419 const struct nlmsghdr *nlh,
2420 const struct nlattr * const nla[])
2422 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2423 const struct nft_set_ops *ops;
2424 struct nft_af_info *afi;
2425 struct net *net = sock_net(skb->sk);
2426 struct nft_table *table;
2427 struct nft_set *set;
2429 char name[IFNAMSIZ];
2432 u32 ktype, dtype, flags, policy;
2433 struct nft_set_desc desc;
2436 if (nla[NFTA_SET_TABLE] == NULL ||
2437 nla[NFTA_SET_NAME] == NULL ||
2438 nla[NFTA_SET_KEY_LEN] == NULL ||
2439 nla[NFTA_SET_ID] == NULL)
2442 memset(&desc, 0, sizeof(desc));
2444 ktype = NFT_DATA_VALUE;
2445 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2446 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2447 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2451 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2452 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2456 if (nla[NFTA_SET_FLAGS] != NULL) {
2457 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2458 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2459 NFT_SET_INTERVAL | NFT_SET_MAP))
2464 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2465 if (!(flags & NFT_SET_MAP))
2468 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2469 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2470 dtype != NFT_DATA_VERDICT)
2473 if (dtype != NFT_DATA_VERDICT) {
2474 if (nla[NFTA_SET_DATA_LEN] == NULL)
2476 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2477 if (desc.dlen == 0 ||
2478 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2481 desc.dlen = sizeof(struct nft_data);
2482 } else if (flags & NFT_SET_MAP)
2485 policy = NFT_SET_POL_PERFORMANCE;
2486 if (nla[NFTA_SET_POLICY] != NULL)
2487 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2489 if (nla[NFTA_SET_DESC] != NULL) {
2490 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2495 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2497 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2499 return PTR_ERR(afi);
2501 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2503 return PTR_ERR(table);
2505 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2507 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2509 if (PTR_ERR(set) != -ENOENT)
2510 return PTR_ERR(set);
2515 if (nlh->nlmsg_flags & NLM_F_EXCL)
2517 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2522 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2525 ops = nft_select_set_ops(nla, &desc, policy);
2527 return PTR_ERR(ops);
2530 if (ops->privsize != NULL)
2531 size = ops->privsize(nla);
2534 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2538 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2539 err = nf_tables_set_alloc_name(&ctx, set, name);
2543 INIT_LIST_HEAD(&set->bindings);
2546 set->klen = desc.klen;
2548 set->dlen = desc.dlen;
2550 set->size = desc.size;
2552 err = ops->init(set, &desc, nla);
2556 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2560 list_add_tail_rcu(&set->list, &table->sets);
2567 module_put(ops->owner);
2571 static void nft_set_destroy(struct nft_set *set)
2573 set->ops->destroy(set);
2574 module_put(set->ops->owner);
2578 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2580 list_del_rcu(&set->list);
2581 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2582 nft_set_destroy(set);
2585 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2586 const struct nlmsghdr *nlh,
2587 const struct nlattr * const nla[])
2589 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2590 struct nft_set *set;
2594 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2595 return -EAFNOSUPPORT;
2596 if (nla[NFTA_SET_TABLE] == NULL)
2599 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2603 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2605 return PTR_ERR(set);
2606 if (set->flags & NFT_SET_INACTIVE)
2608 if (!list_empty(&set->bindings))
2611 err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2615 list_del_rcu(&set->list);
2620 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2621 const struct nft_set *set,
2622 const struct nft_set_iter *iter,
2623 const struct nft_set_elem *elem)
2625 enum nft_registers dreg;
2627 dreg = nft_type_to_reg(set->dtype);
2628 return nft_validate_data_load(ctx, dreg, &elem->data,
2629 set->dtype == NFT_DATA_VERDICT ?
2630 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2633 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2634 struct nft_set_binding *binding)
2636 struct nft_set_binding *i;
2637 struct nft_set_iter iter;
2639 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2642 if (set->flags & NFT_SET_MAP) {
2643 /* If the set is already bound to the same chain all
2644 * jumps are already validated for that chain.
2646 list_for_each_entry(i, &set->bindings, list) {
2647 if (i->chain == binding->chain)
2654 iter.fn = nf_tables_bind_check_setelem;
2656 set->ops->walk(ctx, set, &iter);
2658 /* Destroy anonymous sets if binding fails */
2659 if (set->flags & NFT_SET_ANONYMOUS)
2660 nf_tables_set_destroy(ctx, set);
2666 binding->chain = ctx->chain;
2667 list_add_tail_rcu(&binding->list, &set->bindings);
2671 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2672 struct nft_set_binding *binding)
2674 list_del_rcu(&binding->list);
2676 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2677 !(set->flags & NFT_SET_INACTIVE))
2678 nf_tables_set_destroy(ctx, set);
2685 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2686 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2687 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2688 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2691 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2692 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2693 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2694 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2695 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
2698 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2699 const struct sk_buff *skb,
2700 const struct nlmsghdr *nlh,
2701 const struct nlattr * const nla[],
2704 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2705 struct nft_af_info *afi;
2706 struct nft_table *table;
2707 struct net *net = sock_net(skb->sk);
2709 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2711 return PTR_ERR(afi);
2713 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2715 return PTR_ERR(table);
2716 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2719 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2723 static int nf_tables_fill_setelem(struct sk_buff *skb,
2724 const struct nft_set *set,
2725 const struct nft_set_elem *elem)
2727 unsigned char *b = skb_tail_pointer(skb);
2728 struct nlattr *nest;
2730 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2732 goto nla_put_failure;
2734 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2736 goto nla_put_failure;
2738 if (set->flags & NFT_SET_MAP &&
2739 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2740 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2741 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2743 goto nla_put_failure;
2745 if (elem->flags != 0)
2746 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2747 goto nla_put_failure;
2749 nla_nest_end(skb, nest);
2757 struct nft_set_dump_args {
2758 const struct netlink_callback *cb;
2759 struct nft_set_iter iter;
2760 struct sk_buff *skb;
2763 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2764 const struct nft_set *set,
2765 const struct nft_set_iter *iter,
2766 const struct nft_set_elem *elem)
2768 struct nft_set_dump_args *args;
2770 args = container_of(iter, struct nft_set_dump_args, iter);
2771 return nf_tables_fill_setelem(args->skb, set, elem);
2774 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2776 const struct nft_set *set;
2777 struct nft_set_dump_args args;
2779 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2780 struct nfgenmsg *nfmsg;
2781 struct nlmsghdr *nlh;
2782 struct nlattr *nest;
2786 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2787 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2791 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2796 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2798 return PTR_ERR(set);
2799 if (set->flags & NFT_SET_INACTIVE)
2802 event = NFT_MSG_NEWSETELEM;
2803 event |= NFNL_SUBSYS_NFTABLES << 8;
2804 portid = NETLINK_CB(cb->skb).portid;
2805 seq = cb->nlh->nlmsg_seq;
2807 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2810 goto nla_put_failure;
2812 nfmsg = nlmsg_data(nlh);
2813 nfmsg->nfgen_family = ctx.afi->family;
2814 nfmsg->version = NFNETLINK_V0;
2817 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2818 goto nla_put_failure;
2819 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2820 goto nla_put_failure;
2822 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2824 goto nla_put_failure;
2828 args.iter.skip = cb->args[0];
2829 args.iter.count = 0;
2831 args.iter.fn = nf_tables_dump_setelem;
2832 set->ops->walk(&ctx, set, &args.iter);
2834 nla_nest_end(skb, nest);
2835 nlmsg_end(skb, nlh);
2837 if (args.iter.err && args.iter.err != -EMSGSIZE)
2838 return args.iter.err;
2839 if (args.iter.count == cb->args[0])
2842 cb->args[0] = args.iter.count;
2849 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2850 const struct nlmsghdr *nlh,
2851 const struct nlattr * const nla[])
2853 const struct nft_set *set;
2857 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2861 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2863 return PTR_ERR(set);
2864 if (set->flags & NFT_SET_INACTIVE)
2867 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2868 struct netlink_dump_control c = {
2869 .dump = nf_tables_dump_set,
2871 return netlink_dump_start(nlsk, skb, nlh, &c);
2876 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2877 const struct nft_ctx *ctx, u32 seq,
2878 u32 portid, int event, u16 flags,
2879 const struct nft_set *set,
2880 const struct nft_set_elem *elem)
2882 struct nfgenmsg *nfmsg;
2883 struct nlmsghdr *nlh;
2884 struct nlattr *nest;
2887 event |= NFNL_SUBSYS_NFTABLES << 8;
2888 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2891 goto nla_put_failure;
2893 nfmsg = nlmsg_data(nlh);
2894 nfmsg->nfgen_family = ctx->afi->family;
2895 nfmsg->version = NFNETLINK_V0;
2898 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2899 goto nla_put_failure;
2900 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2901 goto nla_put_failure;
2903 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2905 goto nla_put_failure;
2907 err = nf_tables_fill_setelem(skb, set, elem);
2909 goto nla_put_failure;
2911 nla_nest_end(skb, nest);
2913 return nlmsg_end(skb, nlh);
2916 nlmsg_trim(skb, nlh);
2920 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2921 const struct nft_set *set,
2922 const struct nft_set_elem *elem,
2923 int event, u16 flags)
2925 struct net *net = ctx->net;
2926 u32 portid = ctx->portid;
2927 struct sk_buff *skb;
2930 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2934 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2938 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2945 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
2949 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2953 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
2955 struct nft_set *set)
2957 struct nft_trans *trans;
2959 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
2963 nft_trans_elem_set(trans) = set;
2967 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
2968 const struct nlattr *attr)
2970 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2971 struct nft_data_desc d1, d2;
2972 struct nft_set_elem elem;
2973 struct nft_set_binding *binding;
2974 enum nft_registers dreg;
2975 struct nft_trans *trans;
2978 if (set->size && set->nelems == set->size)
2981 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2982 nft_set_elem_policy);
2986 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2990 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2991 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2992 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2996 if (set->flags & NFT_SET_MAP) {
2997 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2998 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3000 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3001 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3004 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3008 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3012 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3016 if (set->ops->get(set, &elem) == 0)
3019 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3020 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3025 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3028 dreg = nft_type_to_reg(set->dtype);
3029 list_for_each_entry(binding, &set->bindings, list) {
3030 struct nft_ctx bind_ctx = {
3032 .table = ctx->table,
3033 .chain = (struct nft_chain *)binding->chain,
3036 err = nft_validate_data_load(&bind_ctx, dreg,
3037 &elem.data, d2.type);
3043 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3047 err = set->ops->insert(set, &elem);
3051 nft_trans_elem(trans) = elem;
3052 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3058 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3059 nft_data_uninit(&elem.data, d2.type);
3061 nft_data_uninit(&elem.key, d1.type);
3066 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3067 const struct nlmsghdr *nlh,
3068 const struct nlattr * const nla[])
3070 struct net *net = sock_net(skb->sk);
3071 const struct nlattr *attr;
3072 struct nft_set *set;
3076 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3079 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3083 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3085 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3086 set = nf_tables_set_lookup_byid(net,
3087 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3090 return PTR_ERR(set);
3093 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3096 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3097 err = nft_add_set_elem(&ctx, set, attr);
3106 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3107 const struct nlattr *attr)
3109 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3110 struct nft_data_desc desc;
3111 struct nft_set_elem elem;
3112 struct nft_trans *trans;
3115 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3116 nft_set_elem_policy);
3121 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3124 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3129 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3132 err = set->ops->get(set, &elem);
3136 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3140 nft_trans_elem(trans) = elem;
3141 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3144 nft_data_uninit(&elem.key, desc.type);
3149 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3150 const struct nlmsghdr *nlh,
3151 const struct nlattr * const nla[])
3153 const struct nlattr *attr;
3154 struct nft_set *set;
3158 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3161 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3165 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3167 return PTR_ERR(set);
3168 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3171 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3172 err = nft_del_setelem(&ctx, set, attr);
3181 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3182 [NFT_MSG_NEWTABLE] = {
3183 .call_batch = nf_tables_newtable,
3184 .attr_count = NFTA_TABLE_MAX,
3185 .policy = nft_table_policy,
3187 [NFT_MSG_GETTABLE] = {
3188 .call = nf_tables_gettable,
3189 .attr_count = NFTA_TABLE_MAX,
3190 .policy = nft_table_policy,
3192 [NFT_MSG_DELTABLE] = {
3193 .call_batch = nf_tables_deltable,
3194 .attr_count = NFTA_TABLE_MAX,
3195 .policy = nft_table_policy,
3197 [NFT_MSG_NEWCHAIN] = {
3198 .call_batch = nf_tables_newchain,
3199 .attr_count = NFTA_CHAIN_MAX,
3200 .policy = nft_chain_policy,
3202 [NFT_MSG_GETCHAIN] = {
3203 .call = nf_tables_getchain,
3204 .attr_count = NFTA_CHAIN_MAX,
3205 .policy = nft_chain_policy,
3207 [NFT_MSG_DELCHAIN] = {
3208 .call_batch = nf_tables_delchain,
3209 .attr_count = NFTA_CHAIN_MAX,
3210 .policy = nft_chain_policy,
3212 [NFT_MSG_NEWRULE] = {
3213 .call_batch = nf_tables_newrule,
3214 .attr_count = NFTA_RULE_MAX,
3215 .policy = nft_rule_policy,
3217 [NFT_MSG_GETRULE] = {
3218 .call = nf_tables_getrule,
3219 .attr_count = NFTA_RULE_MAX,
3220 .policy = nft_rule_policy,
3222 [NFT_MSG_DELRULE] = {
3223 .call_batch = nf_tables_delrule,
3224 .attr_count = NFTA_RULE_MAX,
3225 .policy = nft_rule_policy,
3227 [NFT_MSG_NEWSET] = {
3228 .call_batch = nf_tables_newset,
3229 .attr_count = NFTA_SET_MAX,
3230 .policy = nft_set_policy,
3232 [NFT_MSG_GETSET] = {
3233 .call = nf_tables_getset,
3234 .attr_count = NFTA_SET_MAX,
3235 .policy = nft_set_policy,
3237 [NFT_MSG_DELSET] = {
3238 .call_batch = nf_tables_delset,
3239 .attr_count = NFTA_SET_MAX,
3240 .policy = nft_set_policy,
3242 [NFT_MSG_NEWSETELEM] = {
3243 .call_batch = nf_tables_newsetelem,
3244 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3245 .policy = nft_set_elem_list_policy,
3247 [NFT_MSG_GETSETELEM] = {
3248 .call = nf_tables_getsetelem,
3249 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3250 .policy = nft_set_elem_list_policy,
3252 [NFT_MSG_DELSETELEM] = {
3253 .call_batch = nf_tables_delsetelem,
3254 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3255 .policy = nft_set_elem_list_policy,
3259 static void nft_chain_commit_update(struct nft_trans *trans)
3261 struct nft_base_chain *basechain;
3263 if (nft_trans_chain_name(trans)[0])
3264 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3266 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3269 basechain = nft_base_chain(trans->ctx.chain);
3270 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3272 switch (nft_trans_chain_policy(trans)) {
3275 basechain->policy = nft_trans_chain_policy(trans);
3280 /* Schedule objects for release via rcu to make sure no packets are accesing
3283 static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3285 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3287 switch (trans->msg_type) {
3288 case NFT_MSG_DELTABLE:
3289 nf_tables_table_destroy(&trans->ctx);
3291 case NFT_MSG_DELCHAIN:
3292 nf_tables_chain_destroy(trans->ctx.chain);
3294 case NFT_MSG_DELRULE:
3295 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3297 case NFT_MSG_DELSET:
3298 nft_set_destroy(nft_trans_set(trans));
3304 static int nf_tables_commit(struct sk_buff *skb)
3306 struct net *net = sock_net(skb->sk);
3307 struct nft_trans *trans, *next;
3308 struct nft_trans_elem *te;
3310 /* Bump generation counter, invalidate any dump in progress */
3311 while (++net->nft.base_seq == 0);
3313 /* A new generation has just started */
3314 net->nft.gencursor = gencursor_next(net);
3316 /* Make sure all packets have left the previous generation before
3317 * purging old rules.
3321 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3322 switch (trans->msg_type) {
3323 case NFT_MSG_NEWTABLE:
3324 if (nft_trans_table_update(trans)) {
3325 if (!nft_trans_table_enable(trans)) {
3326 nf_tables_table_disable(trans->ctx.afi,
3328 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3331 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3333 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3334 nft_trans_destroy(trans);
3336 case NFT_MSG_DELTABLE:
3337 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3339 case NFT_MSG_NEWCHAIN:
3340 if (nft_trans_chain_update(trans))
3341 nft_chain_commit_update(trans);
3343 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3345 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3346 nft_trans_destroy(trans);
3348 case NFT_MSG_DELCHAIN:
3349 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3350 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3351 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3352 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3353 trans->ctx.afi->nops);
3356 case NFT_MSG_NEWRULE:
3357 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3358 nf_tables_rule_notify(&trans->ctx,
3359 nft_trans_rule(trans),
3361 nft_trans_destroy(trans);
3363 case NFT_MSG_DELRULE:
3364 list_del_rcu(&nft_trans_rule(trans)->list);
3365 nf_tables_rule_notify(&trans->ctx,
3366 nft_trans_rule(trans),
3369 case NFT_MSG_NEWSET:
3370 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3371 /* This avoids hitting -EBUSY when deleting the table
3372 * from the transaction.
3374 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3375 !list_empty(&nft_trans_set(trans)->bindings))
3376 trans->ctx.table->use--;
3378 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3379 NFT_MSG_NEWSET, GFP_KERNEL);
3380 nft_trans_destroy(trans);
3382 case NFT_MSG_DELSET:
3383 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3384 NFT_MSG_DELSET, GFP_KERNEL);
3386 case NFT_MSG_NEWSETELEM:
3387 nf_tables_setelem_notify(&trans->ctx,
3388 nft_trans_elem_set(trans),
3389 &nft_trans_elem(trans),
3390 NFT_MSG_NEWSETELEM, 0);
3391 nft_trans_destroy(trans);
3393 case NFT_MSG_DELSETELEM:
3394 te = (struct nft_trans_elem *)trans->data;
3395 nf_tables_setelem_notify(&trans->ctx, te->set,
3397 NFT_MSG_DELSETELEM, 0);
3398 te->set->ops->get(te->set, &te->elem);
3399 te->set->ops->remove(te->set, &te->elem);
3400 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3401 if (te->elem.flags & NFT_SET_MAP) {
3402 nft_data_uninit(&te->elem.data,
3405 nft_trans_destroy(trans);
3410 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3411 list_del(&trans->list);
3412 trans->ctx.nla = NULL;
3413 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
3419 /* Schedule objects for release via rcu to make sure no packets are accesing
3422 static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3424 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3426 switch (trans->msg_type) {
3427 case NFT_MSG_NEWTABLE:
3428 nf_tables_table_destroy(&trans->ctx);
3430 case NFT_MSG_NEWCHAIN:
3431 nf_tables_chain_destroy(trans->ctx.chain);
3433 case NFT_MSG_NEWRULE:
3434 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3436 case NFT_MSG_NEWSET:
3437 nft_set_destroy(nft_trans_set(trans));
3443 static int nf_tables_abort(struct sk_buff *skb)
3445 struct net *net = sock_net(skb->sk);
3446 struct nft_trans *trans, *next;
3447 struct nft_set *set;
3449 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3450 switch (trans->msg_type) {
3451 case NFT_MSG_NEWTABLE:
3452 if (nft_trans_table_update(trans)) {
3453 if (nft_trans_table_enable(trans)) {
3454 nf_tables_table_disable(trans->ctx.afi,
3456 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3458 nft_trans_destroy(trans);
3460 list_del_rcu(&trans->ctx.table->list);
3463 case NFT_MSG_DELTABLE:
3464 list_add_tail_rcu(&trans->ctx.table->list,
3465 &trans->ctx.afi->tables);
3466 nft_trans_destroy(trans);
3468 case NFT_MSG_NEWCHAIN:
3469 if (nft_trans_chain_update(trans)) {
3470 if (nft_trans_chain_stats(trans))
3471 free_percpu(nft_trans_chain_stats(trans));
3473 nft_trans_destroy(trans);
3475 trans->ctx.table->use--;
3476 list_del_rcu(&trans->ctx.chain->list);
3477 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3478 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3479 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3480 trans->ctx.afi->nops);
3484 case NFT_MSG_DELCHAIN:
3485 trans->ctx.table->use++;
3486 list_add_tail_rcu(&trans->ctx.chain->list,
3487 &trans->ctx.table->chains);
3488 nft_trans_destroy(trans);
3490 case NFT_MSG_NEWRULE:
3491 trans->ctx.chain->use--;
3492 list_del_rcu(&nft_trans_rule(trans)->list);
3494 case NFT_MSG_DELRULE:
3495 trans->ctx.chain->use++;
3496 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3497 nft_trans_destroy(trans);
3499 case NFT_MSG_NEWSET:
3500 trans->ctx.table->use--;
3501 list_del_rcu(&nft_trans_set(trans)->list);
3503 case NFT_MSG_DELSET:
3504 trans->ctx.table->use++;
3505 list_add_tail_rcu(&nft_trans_set(trans)->list,
3506 &trans->ctx.table->sets);
3507 nft_trans_destroy(trans);
3509 case NFT_MSG_NEWSETELEM:
3510 nft_trans_elem_set(trans)->nelems--;
3511 set = nft_trans_elem_set(trans);
3512 set->ops->get(set, &nft_trans_elem(trans));
3513 set->ops->remove(set, &nft_trans_elem(trans));
3514 nft_trans_destroy(trans);
3516 case NFT_MSG_DELSETELEM:
3517 nft_trans_elem_set(trans)->nelems++;
3518 nft_trans_destroy(trans);
3523 list_for_each_entry_safe_reverse(trans, next,
3524 &net->nft.commit_list, list) {
3525 list_del(&trans->list);
3526 trans->ctx.nla = NULL;
3527 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
3533 static const struct nfnetlink_subsystem nf_tables_subsys = {
3534 .name = "nf_tables",
3535 .subsys_id = NFNL_SUBSYS_NFTABLES,
3536 .cb_count = NFT_MSG_MAX,
3538 .commit = nf_tables_commit,
3539 .abort = nf_tables_abort,
3543 * Loop detection - walk through the ruleset beginning at the destination chain
3544 * of a new jump until either the source chain is reached (loop) or all
3545 * reachable chains have been traversed.
3547 * The loop check is performed whenever a new jump verdict is added to an
3548 * expression or verdict map or a verdict map is bound to a new chain.
3551 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3552 const struct nft_chain *chain);
3554 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3555 const struct nft_set *set,
3556 const struct nft_set_iter *iter,
3557 const struct nft_set_elem *elem)
3559 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3562 switch (elem->data.verdict) {
3565 return nf_tables_check_loops(ctx, elem->data.chain);
3571 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3572 const struct nft_chain *chain)
3574 const struct nft_rule *rule;
3575 const struct nft_expr *expr, *last;
3576 const struct nft_set *set;
3577 struct nft_set_binding *binding;
3578 struct nft_set_iter iter;
3580 if (ctx->chain == chain)
3583 list_for_each_entry(rule, &chain->rules, list) {
3584 nft_rule_for_each_expr(expr, last, rule) {
3585 const struct nft_data *data = NULL;
3588 if (!expr->ops->validate)
3591 err = expr->ops->validate(ctx, expr, &data);
3598 switch (data->verdict) {
3601 err = nf_tables_check_loops(ctx, data->chain);
3610 list_for_each_entry(set, &ctx->table->sets, list) {
3611 if (!(set->flags & NFT_SET_MAP) ||
3612 set->dtype != NFT_DATA_VERDICT)
3615 list_for_each_entry(binding, &set->bindings, list) {
3616 if (binding->chain != chain)
3622 iter.fn = nf_tables_loop_check_setelem;
3624 set->ops->walk(ctx, set, &iter);
3634 * nft_validate_input_register - validate an expressions' input register
3636 * @reg: the register number
3638 * Validate that the input register is one of the general purpose
3641 int nft_validate_input_register(enum nft_registers reg)
3643 if (reg <= NFT_REG_VERDICT)
3645 if (reg > NFT_REG_MAX)
3649 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3652 * nft_validate_output_register - validate an expressions' output register
3654 * @reg: the register number
3656 * Validate that the output register is one of the general purpose
3657 * registers or the verdict register.
3659 int nft_validate_output_register(enum nft_registers reg)
3661 if (reg < NFT_REG_VERDICT)
3663 if (reg > NFT_REG_MAX)
3667 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3670 * nft_validate_data_load - validate an expressions' data load
3672 * @ctx: context of the expression performing the load
3673 * @reg: the destination register number
3674 * @data: the data to load
3675 * @type: the data type
3677 * Validate that a data load uses the appropriate data type for
3678 * the destination register. A value of NULL for the data means
3679 * that its runtime gathered data, which is always of type
3682 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3683 const struct nft_data *data,
3684 enum nft_data_types type)
3689 case NFT_REG_VERDICT:
3690 if (data == NULL || type != NFT_DATA_VERDICT)
3693 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3694 err = nf_tables_check_loops(ctx, data->chain);
3698 if (ctx->chain->level + 1 > data->chain->level) {
3699 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3701 data->chain->level = ctx->chain->level + 1;
3707 if (data != NULL && type != NFT_DATA_VALUE)
3712 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3714 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3715 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3716 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3717 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3720 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3721 struct nft_data_desc *desc, const struct nlattr *nla)
3723 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3724 struct nft_chain *chain;
3727 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3731 if (!tb[NFTA_VERDICT_CODE])
3733 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3735 switch (data->verdict) {
3737 switch (data->verdict & NF_VERDICT_MASK) {
3749 desc->len = sizeof(data->verdict);
3753 if (!tb[NFTA_VERDICT_CHAIN])
3755 chain = nf_tables_chain_lookup(ctx->table,
3756 tb[NFTA_VERDICT_CHAIN]);
3758 return PTR_ERR(chain);
3759 if (chain->flags & NFT_BASE_CHAIN)
3763 data->chain = chain;
3764 desc->len = sizeof(data);
3768 desc->type = NFT_DATA_VERDICT;
3772 static void nft_verdict_uninit(const struct nft_data *data)
3774 switch (data->verdict) {
3782 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3784 struct nlattr *nest;
3786 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3788 goto nla_put_failure;
3790 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3791 goto nla_put_failure;
3793 switch (data->verdict) {
3796 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3797 goto nla_put_failure;
3799 nla_nest_end(skb, nest);
3806 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3807 struct nft_data_desc *desc, const struct nlattr *nla)
3814 if (len > sizeof(data->data))
3817 nla_memcpy(data->data, nla, sizeof(data->data));
3818 desc->type = NFT_DATA_VALUE;
3823 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3826 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3829 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3830 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3831 .len = FIELD_SIZEOF(struct nft_data, data) },
3832 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3836 * nft_data_init - parse nf_tables data netlink attributes
3838 * @ctx: context of the expression using the data
3839 * @data: destination struct nft_data
3840 * @desc: data description
3841 * @nla: netlink attribute containing data
3843 * Parse the netlink data attributes and initialize a struct nft_data.
3844 * The type and length of data are returned in the data description.
3846 * The caller can indicate that it only wants to accept data of type
3847 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3849 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3850 struct nft_data_desc *desc, const struct nlattr *nla)
3852 struct nlattr *tb[NFTA_DATA_MAX + 1];
3855 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3859 if (tb[NFTA_DATA_VALUE])
3860 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3861 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3862 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3865 EXPORT_SYMBOL_GPL(nft_data_init);
3868 * nft_data_uninit - release a nft_data item
3870 * @data: struct nft_data to release
3871 * @type: type of data
3873 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3874 * all others need to be released by calling this function.
3876 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3879 case NFT_DATA_VALUE:
3881 case NFT_DATA_VERDICT:
3882 return nft_verdict_uninit(data);
3887 EXPORT_SYMBOL_GPL(nft_data_uninit);
3889 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3890 enum nft_data_types type, unsigned int len)
3892 struct nlattr *nest;
3895 nest = nla_nest_start(skb, attr);
3900 case NFT_DATA_VALUE:
3901 err = nft_value_dump(skb, data, len);
3903 case NFT_DATA_VERDICT:
3904 err = nft_verdict_dump(skb, data);
3911 nla_nest_end(skb, nest);
3914 EXPORT_SYMBOL_GPL(nft_data_dump);
3916 static int nf_tables_init_net(struct net *net)
3918 INIT_LIST_HEAD(&net->nft.af_info);
3919 INIT_LIST_HEAD(&net->nft.commit_list);
3920 net->nft.base_seq = 1;
3924 static struct pernet_operations nf_tables_net_ops = {
3925 .init = nf_tables_init_net,
3928 static int __init nf_tables_module_init(void)
3932 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3939 err = nf_tables_core_module_init();
3943 err = nfnetlink_subsys_register(&nf_tables_subsys);
3947 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3948 return register_pernet_subsys(&nf_tables_net_ops);
3950 nf_tables_core_module_exit();
3957 static void __exit nf_tables_module_exit(void)
3959 unregister_pernet_subsys(&nf_tables_net_ops);
3960 nfnetlink_subsys_unregister(&nf_tables_subsys);
3961 nf_tables_core_module_exit();
3965 module_init(nf_tables_module_init);
3966 module_exit(nf_tables_module_exit);
3968 MODULE_LICENSE("GPL");
3969 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3970 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);