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);
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;
280 list_for_each_entry(afi, &net->nft.af_info, list) {
281 if (family != NFPROTO_UNSPEC && family != afi->family)
284 list_for_each_entry(table, &afi->tables, list) {
288 memset(&cb->args[1], 0,
289 sizeof(cb->args) - sizeof(cb->args[0]));
290 if (nf_tables_fill_table_info(skb,
291 NETLINK_CB(cb->skb).portid,
295 afi->family, table) < 0)
306 /* Internal table flags */
307 #define NFT_TABLE_INACTIVE (1 << 15)
309 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
310 const struct nlmsghdr *nlh,
311 const struct nlattr * const nla[])
313 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
314 const struct nft_af_info *afi;
315 const struct nft_table *table;
316 struct sk_buff *skb2;
317 struct net *net = sock_net(skb->sk);
318 int family = nfmsg->nfgen_family;
321 if (nlh->nlmsg_flags & NLM_F_DUMP) {
322 struct netlink_dump_control c = {
323 .dump = nf_tables_dump_tables,
325 return netlink_dump_start(nlsk, skb, nlh, &c);
328 afi = nf_tables_afinfo_lookup(net, family, false);
332 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
334 return PTR_ERR(table);
335 if (table->flags & NFT_TABLE_INACTIVE)
338 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
342 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
343 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
348 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
355 static int nf_tables_table_enable(const struct nft_af_info *afi,
356 struct nft_table *table)
358 struct nft_chain *chain;
361 list_for_each_entry(chain, &table->chains, list) {
362 if (!(chain->flags & NFT_BASE_CHAIN))
365 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
373 list_for_each_entry(chain, &table->chains, list) {
374 if (!(chain->flags & NFT_BASE_CHAIN))
380 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
385 static void nf_tables_table_disable(const struct nft_af_info *afi,
386 struct nft_table *table)
388 struct nft_chain *chain;
390 list_for_each_entry(chain, &table->chains, list) {
391 if (chain->flags & NFT_BASE_CHAIN)
392 nf_unregister_hooks(nft_base_chain(chain)->ops,
397 static int nf_tables_updtable(struct nft_ctx *ctx)
399 struct nft_trans *trans;
403 if (!ctx->nla[NFTA_TABLE_FLAGS])
406 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
407 if (flags & ~NFT_TABLE_F_DORMANT)
410 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
411 sizeof(struct nft_trans_table));
415 if ((flags & NFT_TABLE_F_DORMANT) &&
416 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
417 nft_trans_table_enable(trans) = false;
418 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
419 ctx->table->flags & NFT_TABLE_F_DORMANT) {
420 ret = nf_tables_table_enable(ctx->afi, ctx->table);
422 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
423 nft_trans_table_enable(trans) = true;
429 nft_trans_table_update(trans) = true;
430 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
433 nft_trans_destroy(trans);
437 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
439 struct nft_trans *trans;
441 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
445 if (msg_type == NFT_MSG_NEWTABLE)
446 ctx->table->flags |= NFT_TABLE_INACTIVE;
448 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
452 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
453 const struct nlmsghdr *nlh,
454 const struct nlattr * const nla[])
456 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
457 const struct nlattr *name;
458 struct nft_af_info *afi;
459 struct nft_table *table;
460 struct net *net = sock_net(skb->sk);
461 int family = nfmsg->nfgen_family;
466 afi = nf_tables_afinfo_lookup(net, family, true);
470 name = nla[NFTA_TABLE_NAME];
471 table = nf_tables_table_lookup(afi, name);
473 if (PTR_ERR(table) != -ENOENT)
474 return PTR_ERR(table);
479 if (table->flags & NFT_TABLE_INACTIVE)
481 if (nlh->nlmsg_flags & NLM_F_EXCL)
483 if (nlh->nlmsg_flags & NLM_F_REPLACE)
486 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
487 return nf_tables_updtable(&ctx);
490 if (nla[NFTA_TABLE_FLAGS]) {
491 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
492 if (flags & ~NFT_TABLE_F_DORMANT)
496 if (!try_module_get(afi->owner))
497 return -EAFNOSUPPORT;
499 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
501 module_put(afi->owner);
505 nla_strlcpy(table->name, name, nla_len(name));
506 INIT_LIST_HEAD(&table->chains);
507 INIT_LIST_HEAD(&table->sets);
508 table->flags = flags;
510 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
511 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
514 module_put(afi->owner);
517 list_add_tail(&table->list, &afi->tables);
521 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
522 const struct nlmsghdr *nlh,
523 const struct nlattr * const nla[])
525 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
526 struct nft_af_info *afi;
527 struct nft_table *table;
528 struct net *net = sock_net(skb->sk);
529 int family = nfmsg->nfgen_family, err;
532 afi = nf_tables_afinfo_lookup(net, family, false);
536 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
538 return PTR_ERR(table);
539 if (table->flags & NFT_TABLE_INACTIVE)
542 if (!list_empty(&table->chains) || !list_empty(&table->sets))
545 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
546 err = nft_trans_table_add(&ctx, NFT_MSG_DELTABLE);
550 list_del(&table->list);
554 static void nf_tables_table_destroy(struct nft_ctx *ctx)
557 module_put(ctx->afi->owner);
560 int nft_register_chain_type(const struct nf_chain_type *ctype)
564 nfnl_lock(NFNL_SUBSYS_NFTABLES);
565 if (chain_type[ctype->family][ctype->type] != NULL) {
569 chain_type[ctype->family][ctype->type] = ctype;
571 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
574 EXPORT_SYMBOL_GPL(nft_register_chain_type);
576 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
578 nfnl_lock(NFNL_SUBSYS_NFTABLES);
579 chain_type[ctype->family][ctype->type] = NULL;
580 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
582 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
588 static struct nft_chain *
589 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
591 struct nft_chain *chain;
593 list_for_each_entry(chain, &table->chains, list) {
594 if (chain->handle == handle)
598 return ERR_PTR(-ENOENT);
601 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
602 const struct nlattr *nla)
604 struct nft_chain *chain;
607 return ERR_PTR(-EINVAL);
609 list_for_each_entry(chain, &table->chains, list) {
610 if (!nla_strcmp(nla, chain->name))
614 return ERR_PTR(-ENOENT);
617 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
618 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
619 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
620 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
621 .len = NFT_CHAIN_MAXNAMELEN - 1 },
622 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
623 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
624 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
625 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
628 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
629 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
630 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
633 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
635 struct nft_stats *cpu_stats, total;
639 memset(&total, 0, sizeof(total));
640 for_each_possible_cpu(cpu) {
641 cpu_stats = per_cpu_ptr(stats, cpu);
642 total.pkts += cpu_stats->pkts;
643 total.bytes += cpu_stats->bytes;
645 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
647 goto nla_put_failure;
649 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
650 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
651 goto nla_put_failure;
653 nla_nest_end(skb, nest);
660 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
661 int event, u32 flags, int family,
662 const struct nft_table *table,
663 const struct nft_chain *chain)
665 struct nlmsghdr *nlh;
666 struct nfgenmsg *nfmsg;
668 event |= NFNL_SUBSYS_NFTABLES << 8;
669 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
671 goto nla_put_failure;
673 nfmsg = nlmsg_data(nlh);
674 nfmsg->nfgen_family = family;
675 nfmsg->version = NFNETLINK_V0;
678 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
679 goto nla_put_failure;
680 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
681 goto nla_put_failure;
682 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
683 goto nla_put_failure;
685 if (chain->flags & NFT_BASE_CHAIN) {
686 const struct nft_base_chain *basechain = nft_base_chain(chain);
687 const struct nf_hook_ops *ops = &basechain->ops[0];
690 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
692 goto nla_put_failure;
693 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
694 goto nla_put_failure;
695 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
696 goto nla_put_failure;
697 nla_nest_end(skb, nest);
699 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
700 htonl(basechain->policy)))
701 goto nla_put_failure;
703 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
704 goto nla_put_failure;
706 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
707 goto nla_put_failure;
710 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
711 goto nla_put_failure;
713 return nlmsg_end(skb, nlh);
716 nlmsg_trim(skb, nlh);
720 static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
726 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
730 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
734 err = nf_tables_fill_chain_info(skb, ctx->portid, ctx->seq, event, 0,
735 ctx->afi->family, ctx->table,
742 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
743 ctx->report, GFP_KERNEL);
746 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
752 static int nf_tables_dump_chains(struct sk_buff *skb,
753 struct netlink_callback *cb)
755 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
756 const struct nft_af_info *afi;
757 const struct nft_table *table;
758 const struct nft_chain *chain;
759 unsigned int idx = 0, s_idx = cb->args[0];
760 struct net *net = sock_net(skb->sk);
761 int family = nfmsg->nfgen_family;
763 list_for_each_entry(afi, &net->nft.af_info, list) {
764 if (family != NFPROTO_UNSPEC && family != afi->family)
767 list_for_each_entry(table, &afi->tables, list) {
768 list_for_each_entry(chain, &table->chains, list) {
772 memset(&cb->args[1], 0,
773 sizeof(cb->args) - sizeof(cb->args[0]));
774 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
778 afi->family, table, chain) < 0)
791 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
792 const struct nlmsghdr *nlh,
793 const struct nlattr * const nla[])
795 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
796 const struct nft_af_info *afi;
797 const struct nft_table *table;
798 const struct nft_chain *chain;
799 struct sk_buff *skb2;
800 struct net *net = sock_net(skb->sk);
801 int family = nfmsg->nfgen_family;
804 if (nlh->nlmsg_flags & NLM_F_DUMP) {
805 struct netlink_dump_control c = {
806 .dump = nf_tables_dump_chains,
808 return netlink_dump_start(nlsk, skb, nlh, &c);
811 afi = nf_tables_afinfo_lookup(net, family, false);
815 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
817 return PTR_ERR(table);
818 if (table->flags & NFT_TABLE_INACTIVE)
821 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
823 return PTR_ERR(chain);
824 if (chain->flags & NFT_CHAIN_INACTIVE)
827 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
831 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
832 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
833 family, table, chain);
837 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
844 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
845 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
846 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
849 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
851 struct nlattr *tb[NFTA_COUNTER_MAX+1];
852 struct nft_stats __percpu *newstats;
853 struct nft_stats *stats;
856 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
860 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
861 return ERR_PTR(-EINVAL);
863 newstats = alloc_percpu(struct nft_stats);
864 if (newstats == NULL)
865 return ERR_PTR(-ENOMEM);
867 /* Restore old counters on this cpu, no problem. Per-cpu statistics
868 * are not exposed to userspace.
870 stats = this_cpu_ptr(newstats);
871 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
872 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
877 static void nft_chain_stats_replace(struct nft_base_chain *chain,
878 struct nft_stats __percpu *newstats)
881 struct nft_stats __percpu *oldstats =
882 nft_dereference(chain->stats);
884 rcu_assign_pointer(chain->stats, newstats);
886 free_percpu(oldstats);
888 rcu_assign_pointer(chain->stats, newstats);
891 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
893 struct nft_trans *trans;
895 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
899 if (msg_type == NFT_MSG_NEWCHAIN)
900 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
902 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
906 static void nf_tables_chain_destroy(struct nft_chain *chain)
908 BUG_ON(chain->use > 0);
910 if (chain->flags & NFT_BASE_CHAIN) {
911 module_put(nft_base_chain(chain)->type->owner);
912 free_percpu(nft_base_chain(chain)->stats);
913 kfree(nft_base_chain(chain));
919 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
920 const struct nlmsghdr *nlh,
921 const struct nlattr * const nla[])
923 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
924 const struct nlattr * uninitialized_var(name);
925 struct nft_af_info *afi;
926 struct nft_table *table;
927 struct nft_chain *chain;
928 struct nft_base_chain *basechain = NULL;
929 struct nlattr *ha[NFTA_HOOK_MAX + 1];
930 struct net *net = sock_net(skb->sk);
931 int family = nfmsg->nfgen_family;
932 u8 policy = NF_ACCEPT;
935 struct nft_stats __percpu *stats;
940 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
942 afi = nf_tables_afinfo_lookup(net, family, true);
946 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
948 return PTR_ERR(table);
951 name = nla[NFTA_CHAIN_NAME];
953 if (nla[NFTA_CHAIN_HANDLE]) {
954 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
955 chain = nf_tables_chain_lookup_byhandle(table, handle);
957 return PTR_ERR(chain);
959 chain = nf_tables_chain_lookup(table, name);
961 if (PTR_ERR(chain) != -ENOENT)
962 return PTR_ERR(chain);
967 if (nla[NFTA_CHAIN_POLICY]) {
968 if ((chain != NULL &&
969 !(chain->flags & NFT_BASE_CHAIN)) ||
970 nla[NFTA_CHAIN_HOOK] == NULL)
973 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
984 struct nft_stats *stats = NULL;
985 struct nft_trans *trans;
987 if (chain->flags & NFT_CHAIN_INACTIVE)
989 if (nlh->nlmsg_flags & NLM_F_EXCL)
991 if (nlh->nlmsg_flags & NLM_F_REPLACE)
994 if (nla[NFTA_CHAIN_HANDLE] && name &&
995 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
998 if (nla[NFTA_CHAIN_COUNTERS]) {
999 if (!(chain->flags & NFT_BASE_CHAIN))
1002 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1004 return PTR_ERR(stats);
1007 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1008 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1009 sizeof(struct nft_trans_chain));
1013 nft_trans_chain_stats(trans) = stats;
1014 nft_trans_chain_update(trans) = true;
1016 if (nla[NFTA_CHAIN_POLICY])
1017 nft_trans_chain_policy(trans) = policy;
1019 nft_trans_chain_policy(trans) = -1;
1021 if (nla[NFTA_CHAIN_HANDLE] && name) {
1022 nla_strlcpy(nft_trans_chain_name(trans), name,
1023 NFT_CHAIN_MAXNAMELEN);
1025 list_add_tail(&trans->list, &net->nft.commit_list);
1029 if (table->use == UINT_MAX)
1032 if (nla[NFTA_CHAIN_HOOK]) {
1033 const struct nf_chain_type *type;
1034 struct nf_hook_ops *ops;
1036 u32 hooknum, priority;
1038 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1039 if (nla[NFTA_CHAIN_TYPE]) {
1040 type = nf_tables_chain_type_lookup(afi,
1041 nla[NFTA_CHAIN_TYPE],
1044 return PTR_ERR(type);
1047 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1051 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1052 ha[NFTA_HOOK_PRIORITY] == NULL)
1055 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1056 if (hooknum >= afi->nhooks)
1058 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1060 if (!(type->hook_mask & (1 << hooknum)))
1062 if (!try_module_get(type->owner))
1064 hookfn = type->hooks[hooknum];
1066 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1067 if (basechain == NULL)
1070 if (nla[NFTA_CHAIN_COUNTERS]) {
1071 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1072 if (IS_ERR(stats)) {
1073 module_put(type->owner);
1075 return PTR_ERR(stats);
1077 basechain->stats = stats;
1079 stats = alloc_percpu(struct nft_stats);
1080 if (IS_ERR(stats)) {
1081 module_put(type->owner);
1083 return PTR_ERR(stats);
1085 rcu_assign_pointer(basechain->stats, stats);
1088 basechain->type = type;
1089 chain = &basechain->chain;
1091 for (i = 0; i < afi->nops; i++) {
1092 ops = &basechain->ops[i];
1094 ops->owner = afi->owner;
1095 ops->hooknum = hooknum;
1096 ops->priority = priority;
1098 ops->hook = afi->hooks[ops->hooknum];
1101 if (afi->hook_ops_init)
1102 afi->hook_ops_init(ops, i);
1105 chain->flags |= NFT_BASE_CHAIN;
1106 basechain->policy = policy;
1108 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1113 INIT_LIST_HEAD(&chain->rules);
1114 chain->handle = nf_tables_alloc_handle(table);
1116 chain->table = table;
1117 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1119 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1120 chain->flags & NFT_BASE_CHAIN) {
1121 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1126 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1127 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1131 list_add_tail(&chain->list, &table->chains);
1134 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1135 chain->flags & NFT_BASE_CHAIN) {
1136 nf_unregister_hooks(nft_base_chain(chain)->ops,
1140 nf_tables_chain_destroy(chain);
1144 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1145 const struct nlmsghdr *nlh,
1146 const struct nlattr * const nla[])
1148 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1149 struct nft_af_info *afi;
1150 struct nft_table *table;
1151 struct nft_chain *chain;
1152 struct net *net = sock_net(skb->sk);
1153 int family = nfmsg->nfgen_family;
1157 afi = nf_tables_afinfo_lookup(net, family, false);
1159 return PTR_ERR(afi);
1161 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1163 return PTR_ERR(table);
1164 if (table->flags & NFT_TABLE_INACTIVE)
1167 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1169 return PTR_ERR(chain);
1170 if (chain->flags & NFT_CHAIN_INACTIVE)
1172 if (!list_empty(&chain->rules) || chain->use > 0)
1175 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1176 err = nft_trans_chain_add(&ctx, NFT_MSG_DELCHAIN);
1180 list_del(&chain->list);
1189 * nft_register_expr - register nf_tables expr type
1192 * Registers the expr type for use with nf_tables. Returns zero on
1193 * success or a negative errno code otherwise.
1195 int nft_register_expr(struct nft_expr_type *type)
1197 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1198 if (type->family == NFPROTO_UNSPEC)
1199 list_add_tail(&type->list, &nf_tables_expressions);
1201 list_add(&type->list, &nf_tables_expressions);
1202 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1205 EXPORT_SYMBOL_GPL(nft_register_expr);
1208 * nft_unregister_expr - unregister nf_tables expr type
1211 * Unregisters the expr typefor use with nf_tables.
1213 void nft_unregister_expr(struct nft_expr_type *type)
1215 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1216 list_del(&type->list);
1217 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1219 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1221 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1224 const struct nft_expr_type *type;
1226 list_for_each_entry(type, &nf_tables_expressions, list) {
1227 if (!nla_strcmp(nla, type->name) &&
1228 (!type->family || type->family == family))
1234 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1237 const struct nft_expr_type *type;
1240 return ERR_PTR(-EINVAL);
1242 type = __nft_expr_type_get(family, nla);
1243 if (type != NULL && try_module_get(type->owner))
1246 #ifdef CONFIG_MODULES
1248 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1249 request_module("nft-expr-%u-%.*s", family,
1250 nla_len(nla), (char *)nla_data(nla));
1251 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1252 if (__nft_expr_type_get(family, nla))
1253 return ERR_PTR(-EAGAIN);
1255 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1256 request_module("nft-expr-%.*s",
1257 nla_len(nla), (char *)nla_data(nla));
1258 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1259 if (__nft_expr_type_get(family, nla))
1260 return ERR_PTR(-EAGAIN);
1263 return ERR_PTR(-ENOENT);
1266 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1267 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1268 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1271 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1272 const struct nft_expr *expr)
1274 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1275 goto nla_put_failure;
1277 if (expr->ops->dump) {
1278 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1280 goto nla_put_failure;
1281 if (expr->ops->dump(skb, expr) < 0)
1282 goto nla_put_failure;
1283 nla_nest_end(skb, data);
1292 struct nft_expr_info {
1293 const struct nft_expr_ops *ops;
1294 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1297 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1298 const struct nlattr *nla,
1299 struct nft_expr_info *info)
1301 const struct nft_expr_type *type;
1302 const struct nft_expr_ops *ops;
1303 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1306 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1310 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1312 return PTR_ERR(type);
1314 if (tb[NFTA_EXPR_DATA]) {
1315 err = nla_parse_nested(info->tb, type->maxattr,
1316 tb[NFTA_EXPR_DATA], type->policy);
1320 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1322 if (type->select_ops != NULL) {
1323 ops = type->select_ops(ctx,
1324 (const struct nlattr * const *)info->tb);
1336 module_put(type->owner);
1340 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1341 const struct nft_expr_info *info,
1342 struct nft_expr *expr)
1344 const struct nft_expr_ops *ops = info->ops;
1349 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1361 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1362 struct nft_expr *expr)
1364 if (expr->ops->destroy)
1365 expr->ops->destroy(ctx, expr);
1366 module_put(expr->ops->type->owner);
1373 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1376 struct nft_rule *rule;
1378 // FIXME: this sucks
1379 list_for_each_entry(rule, &chain->rules, list) {
1380 if (handle == rule->handle)
1384 return ERR_PTR(-ENOENT);
1387 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1388 const struct nlattr *nla)
1391 return ERR_PTR(-EINVAL);
1393 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1396 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1397 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1398 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1399 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1400 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1401 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1402 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1403 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1404 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1405 .len = NFT_USERDATA_MAXLEN },
1408 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1409 int event, u32 flags, int family,
1410 const struct nft_table *table,
1411 const struct nft_chain *chain,
1412 const struct nft_rule *rule)
1414 struct nlmsghdr *nlh;
1415 struct nfgenmsg *nfmsg;
1416 const struct nft_expr *expr, *next;
1417 struct nlattr *list;
1418 const struct nft_rule *prule;
1419 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1421 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1424 goto nla_put_failure;
1426 nfmsg = nlmsg_data(nlh);
1427 nfmsg->nfgen_family = family;
1428 nfmsg->version = NFNETLINK_V0;
1431 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1432 goto nla_put_failure;
1433 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1434 goto nla_put_failure;
1435 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1436 goto nla_put_failure;
1438 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1439 prule = list_entry(rule->list.prev, struct nft_rule, list);
1440 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1441 cpu_to_be64(prule->handle)))
1442 goto nla_put_failure;
1445 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1447 goto nla_put_failure;
1448 nft_rule_for_each_expr(expr, next, rule) {
1449 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1451 goto nla_put_failure;
1452 if (nf_tables_fill_expr_info(skb, expr) < 0)
1453 goto nla_put_failure;
1454 nla_nest_end(skb, elem);
1456 nla_nest_end(skb, list);
1459 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1460 goto nla_put_failure;
1462 return nlmsg_end(skb, nlh);
1465 nlmsg_trim(skb, nlh);
1469 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1470 const struct nft_rule *rule,
1473 struct sk_buff *skb;
1477 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1481 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1485 err = nf_tables_fill_rule_info(skb, ctx->portid, ctx->seq, event, 0,
1486 ctx->afi->family, ctx->table,
1493 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1494 ctx->report, GFP_KERNEL);
1497 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1504 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1506 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1509 static inline int gencursor_next(struct net *net)
1511 return net->nft.gencursor+1 == 1 ? 1 : 0;
1515 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1517 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1521 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1523 /* Now inactive, will be active in the future */
1524 rule->genmask = (1 << net->nft.gencursor);
1528 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1530 rule->genmask = (1 << gencursor_next(net));
1533 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1538 static int nf_tables_dump_rules(struct sk_buff *skb,
1539 struct netlink_callback *cb)
1541 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1542 const struct nft_af_info *afi;
1543 const struct nft_table *table;
1544 const struct nft_chain *chain;
1545 const struct nft_rule *rule;
1546 unsigned int idx = 0, s_idx = cb->args[0];
1547 struct net *net = sock_net(skb->sk);
1548 int family = nfmsg->nfgen_family;
1549 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1550 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1552 list_for_each_entry(afi, &net->nft.af_info, list) {
1553 if (family != NFPROTO_UNSPEC && family != afi->family)
1556 list_for_each_entry(table, &afi->tables, list) {
1557 list_for_each_entry(chain, &table->chains, list) {
1558 list_for_each_entry(rule, &chain->rules, list) {
1559 if (!nft_rule_is_active(net, rule))
1564 memset(&cb->args[1], 0,
1565 sizeof(cb->args) - sizeof(cb->args[0]));
1566 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1569 NLM_F_MULTI | NLM_F_APPEND,
1570 afi->family, table, chain, rule) < 0)
1579 /* Invalidate this dump, a transition to the new generation happened */
1580 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1587 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1588 const struct nlmsghdr *nlh,
1589 const struct nlattr * const nla[])
1591 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1592 const struct nft_af_info *afi;
1593 const struct nft_table *table;
1594 const struct nft_chain *chain;
1595 const struct nft_rule *rule;
1596 struct sk_buff *skb2;
1597 struct net *net = sock_net(skb->sk);
1598 int family = nfmsg->nfgen_family;
1601 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1602 struct netlink_dump_control c = {
1603 .dump = nf_tables_dump_rules,
1605 return netlink_dump_start(nlsk, skb, nlh, &c);
1608 afi = nf_tables_afinfo_lookup(net, family, false);
1610 return PTR_ERR(afi);
1612 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1614 return PTR_ERR(table);
1615 if (table->flags & NFT_TABLE_INACTIVE)
1618 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1620 return PTR_ERR(chain);
1621 if (chain->flags & NFT_CHAIN_INACTIVE)
1624 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1626 return PTR_ERR(rule);
1628 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1632 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1633 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1634 family, table, chain, rule);
1638 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1645 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1646 struct nft_rule *rule)
1648 struct nft_expr *expr;
1651 * Careful: some expressions might not be initialized in case this
1652 * is called on error from nf_tables_newrule().
1654 expr = nft_expr_first(rule);
1655 while (expr->ops && expr != nft_expr_last(rule)) {
1656 nf_tables_expr_destroy(ctx, expr);
1657 expr = nft_expr_next(expr);
1662 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
1663 struct nft_rule *rule)
1665 struct nft_trans *trans;
1667 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
1671 nft_trans_rule(trans) = rule;
1672 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1677 #define NFT_RULE_MAXEXPRS 128
1679 static struct nft_expr_info *info;
1681 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1682 const struct nlmsghdr *nlh,
1683 const struct nlattr * const nla[])
1685 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1686 struct nft_af_info *afi;
1687 struct net *net = sock_net(skb->sk);
1688 struct nft_table *table;
1689 struct nft_chain *chain;
1690 struct nft_rule *rule, *old_rule = NULL;
1691 struct nft_trans *trans = NULL;
1692 struct nft_expr *expr;
1695 unsigned int size, i, n, ulen = 0;
1698 u64 handle, pos_handle;
1700 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1702 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1704 return PTR_ERR(afi);
1706 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1708 return PTR_ERR(table);
1710 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1712 return PTR_ERR(chain);
1714 if (nla[NFTA_RULE_HANDLE]) {
1715 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1716 rule = __nf_tables_rule_lookup(chain, handle);
1718 return PTR_ERR(rule);
1720 if (nlh->nlmsg_flags & NLM_F_EXCL)
1722 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1727 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1729 handle = nf_tables_alloc_handle(table);
1732 if (nla[NFTA_RULE_POSITION]) {
1733 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1736 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1737 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1738 if (IS_ERR(old_rule))
1739 return PTR_ERR(old_rule);
1742 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1746 if (nla[NFTA_RULE_EXPRESSIONS]) {
1747 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1749 if (nla_type(tmp) != NFTA_LIST_ELEM)
1751 if (n == NFT_RULE_MAXEXPRS)
1753 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1756 size += info[n].ops->size;
1761 if (nla[NFTA_RULE_USERDATA])
1762 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1765 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1769 nft_rule_activate_next(net, rule);
1771 rule->handle = handle;
1776 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1778 expr = nft_expr_first(rule);
1779 for (i = 0; i < n; i++) {
1780 err = nf_tables_newexpr(&ctx, &info[i], expr);
1784 expr = nft_expr_next(expr);
1787 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1788 if (nft_rule_is_active_next(net, old_rule)) {
1789 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE,
1791 if (trans == NULL) {
1795 nft_rule_disactivate_next(net, old_rule);
1796 list_add_tail(&rule->list, &old_rule->list);
1801 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1803 list_add_rcu(&rule->list, &old_rule->list);
1805 list_add_tail_rcu(&rule->list, &chain->rules);
1808 list_add_tail_rcu(&rule->list, &old_rule->list);
1810 list_add_rcu(&rule->list, &chain->rules);
1813 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
1820 list_del_rcu(&rule->list);
1822 list_del_rcu(&nft_trans_rule(trans)->list);
1823 nft_rule_clear(net, nft_trans_rule(trans));
1824 nft_trans_destroy(trans);
1827 nf_tables_rule_destroy(&ctx, rule);
1829 for (i = 0; i < n; i++) {
1830 if (info[i].ops != NULL)
1831 module_put(info[i].ops->type->owner);
1837 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1839 /* You cannot delete the same rule twice */
1840 if (nft_rule_is_active_next(ctx->net, rule)) {
1841 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
1843 nft_rule_disactivate_next(ctx->net, rule);
1849 static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1851 struct nft_rule *rule;
1854 list_for_each_entry(rule, &ctx->chain->rules, list) {
1855 err = nf_tables_delrule_one(ctx, rule);
1862 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1863 const struct nlmsghdr *nlh,
1864 const struct nlattr * const nla[])
1866 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1867 struct nft_af_info *afi;
1868 struct net *net = sock_net(skb->sk);
1869 struct nft_table *table;
1870 struct nft_chain *chain = NULL;
1871 struct nft_rule *rule;
1872 int family = nfmsg->nfgen_family, err = 0;
1875 afi = nf_tables_afinfo_lookup(net, family, false);
1877 return PTR_ERR(afi);
1879 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1881 return PTR_ERR(table);
1882 if (table->flags & NFT_TABLE_INACTIVE)
1885 if (nla[NFTA_RULE_CHAIN]) {
1886 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1888 return PTR_ERR(chain);
1891 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1894 if (nla[NFTA_RULE_HANDLE]) {
1895 rule = nf_tables_rule_lookup(chain,
1896 nla[NFTA_RULE_HANDLE]);
1898 return PTR_ERR(rule);
1900 err = nf_tables_delrule_one(&ctx, rule);
1902 err = nf_table_delrule_by_chain(&ctx);
1905 list_for_each_entry(chain, &table->chains, list) {
1907 err = nf_table_delrule_by_chain(&ctx);
1920 static LIST_HEAD(nf_tables_set_ops);
1922 int nft_register_set(struct nft_set_ops *ops)
1924 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1925 list_add_tail(&ops->list, &nf_tables_set_ops);
1926 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1929 EXPORT_SYMBOL_GPL(nft_register_set);
1931 void nft_unregister_set(struct nft_set_ops *ops)
1933 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1934 list_del(&ops->list);
1935 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1937 EXPORT_SYMBOL_GPL(nft_unregister_set);
1940 * Select a set implementation based on the data characteristics and the
1941 * given policy. The total memory use might not be known if no size is
1942 * given, in that case the amount of memory per element is used.
1944 static const struct nft_set_ops *
1945 nft_select_set_ops(const struct nlattr * const nla[],
1946 const struct nft_set_desc *desc,
1947 enum nft_set_policies policy)
1949 const struct nft_set_ops *ops, *bops;
1950 struct nft_set_estimate est, best;
1953 #ifdef CONFIG_MODULES
1954 if (list_empty(&nf_tables_set_ops)) {
1955 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1956 request_module("nft-set");
1957 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1958 if (!list_empty(&nf_tables_set_ops))
1959 return ERR_PTR(-EAGAIN);
1963 if (nla[NFTA_SET_FLAGS] != NULL) {
1964 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1965 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1972 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1973 if ((ops->features & features) != features)
1975 if (!ops->estimate(desc, features, &est))
1979 case NFT_SET_POL_PERFORMANCE:
1980 if (est.class < best.class)
1982 if (est.class == best.class && est.size < best.size)
1985 case NFT_SET_POL_MEMORY:
1986 if (est.size < best.size)
1988 if (est.size == best.size && est.class < best.class)
1995 if (!try_module_get(ops->owner))
1998 module_put(bops->owner);
2007 return ERR_PTR(-EOPNOTSUPP);
2010 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2011 [NFTA_SET_TABLE] = { .type = NLA_STRING },
2012 [NFTA_SET_NAME] = { .type = NLA_STRING,
2013 .len = IFNAMSIZ - 1 },
2014 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2015 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2016 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2017 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2018 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
2019 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2020 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2021 [NFTA_SET_ID] = { .type = NLA_U32 },
2024 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2025 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
2028 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2029 const struct sk_buff *skb,
2030 const struct nlmsghdr *nlh,
2031 const struct nlattr * const nla[])
2033 struct net *net = sock_net(skb->sk);
2034 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2035 struct nft_af_info *afi = NULL;
2036 struct nft_table *table = NULL;
2038 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2039 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2041 return PTR_ERR(afi);
2044 if (nla[NFTA_SET_TABLE] != NULL) {
2046 return -EAFNOSUPPORT;
2048 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2050 return PTR_ERR(table);
2051 if (table->flags & NFT_TABLE_INACTIVE)
2055 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2059 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2060 const struct nlattr *nla)
2062 struct nft_set *set;
2065 return ERR_PTR(-EINVAL);
2067 list_for_each_entry(set, &table->sets, list) {
2068 if (!nla_strcmp(nla, set->name))
2071 return ERR_PTR(-ENOENT);
2074 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2075 const struct nlattr *nla)
2077 struct nft_trans *trans;
2078 u32 id = ntohl(nla_get_be32(nla));
2080 list_for_each_entry(trans, &net->nft.commit_list, list) {
2081 if (trans->msg_type == NFT_MSG_NEWSET &&
2082 id == nft_trans_set_id(trans))
2083 return nft_trans_set(trans);
2085 return ERR_PTR(-ENOENT);
2088 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2091 const struct nft_set *i;
2093 unsigned long *inuse;
2094 unsigned int n = 0, min = 0;
2096 p = strnchr(name, IFNAMSIZ, '%');
2098 if (p[1] != 'd' || strchr(p + 2, '%'))
2101 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2105 list_for_each_entry(i, &ctx->table->sets, list) {
2108 if (!sscanf(i->name, name, &tmp))
2110 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2113 set_bit(tmp - min, inuse);
2116 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2117 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2118 min += BITS_PER_BYTE * PAGE_SIZE;
2119 memset(inuse, 0, PAGE_SIZE);
2122 free_page((unsigned long)inuse);
2125 snprintf(set->name, sizeof(set->name), name, min + n);
2126 list_for_each_entry(i, &ctx->table->sets, list) {
2127 if (!strcmp(set->name, i->name))
2133 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2134 const struct nft_set *set, u16 event, u16 flags)
2136 struct nfgenmsg *nfmsg;
2137 struct nlmsghdr *nlh;
2138 struct nlattr *desc;
2139 u32 portid = ctx->portid;
2142 event |= NFNL_SUBSYS_NFTABLES << 8;
2143 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2146 goto nla_put_failure;
2148 nfmsg = nlmsg_data(nlh);
2149 nfmsg->nfgen_family = ctx->afi->family;
2150 nfmsg->version = NFNETLINK_V0;
2153 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2154 goto nla_put_failure;
2155 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2156 goto nla_put_failure;
2157 if (set->flags != 0)
2158 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2159 goto nla_put_failure;
2161 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2162 goto nla_put_failure;
2163 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2164 goto nla_put_failure;
2165 if (set->flags & NFT_SET_MAP) {
2166 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2167 goto nla_put_failure;
2168 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2169 goto nla_put_failure;
2172 desc = nla_nest_start(skb, NFTA_SET_DESC);
2174 goto nla_put_failure;
2176 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2177 goto nla_put_failure;
2178 nla_nest_end(skb, desc);
2180 return nlmsg_end(skb, nlh);
2183 nlmsg_trim(skb, nlh);
2187 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2188 const struct nft_set *set,
2191 struct sk_buff *skb;
2192 u32 portid = ctx->portid;
2196 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2200 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2204 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2210 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2211 ctx->report, GFP_KERNEL);
2214 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2218 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2219 struct netlink_callback *cb)
2221 const struct nft_set *set;
2222 unsigned int idx = 0, s_idx = cb->args[0];
2227 list_for_each_entry(set, &ctx->table->sets, list) {
2230 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2243 static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2244 struct netlink_callback *cb)
2246 const struct nft_set *set;
2247 unsigned int idx, s_idx = cb->args[0];
2248 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2253 list_for_each_entry(table, &ctx->afi->tables, list) {
2255 if (cur_table != table)
2262 list_for_each_entry(set, &ctx->table->sets, list) {
2265 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2268 cb->args[2] = (unsigned long) table;
2280 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2281 struct netlink_callback *cb)
2283 const struct nft_set *set;
2284 unsigned int idx, s_idx = cb->args[0];
2285 struct nft_af_info *afi;
2286 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2287 struct net *net = sock_net(skb->sk);
2288 int cur_family = cb->args[3];
2293 list_for_each_entry(afi, &net->nft.af_info, list) {
2295 if (afi->family != cur_family)
2301 list_for_each_entry(table, &afi->tables, list) {
2303 if (cur_table != table)
2312 list_for_each_entry(set, &ctx->table->sets, list) {
2315 if (nf_tables_fill_set(skb, ctx, set,
2319 cb->args[2] = (unsigned long) table;
2320 cb->args[3] = afi->family;
2335 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2337 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2338 struct nlattr *nla[NFTA_SET_MAX + 1];
2342 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2347 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2351 if (ctx.table == NULL) {
2352 if (ctx.afi == NULL)
2353 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2355 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2357 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2362 #define NFT_SET_INACTIVE (1 << 15) /* Internal set flag */
2364 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2365 const struct nlmsghdr *nlh,
2366 const struct nlattr * const nla[])
2368 const struct nft_set *set;
2370 struct sk_buff *skb2;
2371 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2374 /* Verify existance before starting dump */
2375 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2379 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2380 struct netlink_dump_control c = {
2381 .dump = nf_tables_dump_sets,
2383 return netlink_dump_start(nlsk, skb, nlh, &c);
2386 /* Only accept unspec with dump */
2387 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2388 return -EAFNOSUPPORT;
2390 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2392 return PTR_ERR(set);
2393 if (set->flags & NFT_SET_INACTIVE)
2396 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2400 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2404 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2411 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2412 struct nft_set_desc *desc,
2413 const struct nlattr *nla)
2415 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2418 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2422 if (da[NFTA_SET_DESC_SIZE] != NULL)
2423 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2428 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2429 struct nft_set *set)
2431 struct nft_trans *trans;
2433 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2437 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2438 nft_trans_set_id(trans) =
2439 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2440 set->flags |= NFT_SET_INACTIVE;
2442 nft_trans_set(trans) = set;
2443 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2448 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2449 const struct nlmsghdr *nlh,
2450 const struct nlattr * const nla[])
2452 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2453 const struct nft_set_ops *ops;
2454 struct nft_af_info *afi;
2455 struct net *net = sock_net(skb->sk);
2456 struct nft_table *table;
2457 struct nft_set *set;
2459 char name[IFNAMSIZ];
2462 u32 ktype, dtype, flags, policy;
2463 struct nft_set_desc desc;
2466 if (nla[NFTA_SET_TABLE] == NULL ||
2467 nla[NFTA_SET_NAME] == NULL ||
2468 nla[NFTA_SET_KEY_LEN] == NULL ||
2469 nla[NFTA_SET_ID] == NULL)
2472 memset(&desc, 0, sizeof(desc));
2474 ktype = NFT_DATA_VALUE;
2475 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2476 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2477 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2481 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2482 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2486 if (nla[NFTA_SET_FLAGS] != NULL) {
2487 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2488 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2489 NFT_SET_INTERVAL | NFT_SET_MAP))
2494 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2495 if (!(flags & NFT_SET_MAP))
2498 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2499 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2500 dtype != NFT_DATA_VERDICT)
2503 if (dtype != NFT_DATA_VERDICT) {
2504 if (nla[NFTA_SET_DATA_LEN] == NULL)
2506 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2507 if (desc.dlen == 0 ||
2508 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2511 desc.dlen = sizeof(struct nft_data);
2512 } else if (flags & NFT_SET_MAP)
2515 policy = NFT_SET_POL_PERFORMANCE;
2516 if (nla[NFTA_SET_POLICY] != NULL)
2517 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2519 if (nla[NFTA_SET_DESC] != NULL) {
2520 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2525 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2527 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2529 return PTR_ERR(afi);
2531 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2533 return PTR_ERR(table);
2535 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2537 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2539 if (PTR_ERR(set) != -ENOENT)
2540 return PTR_ERR(set);
2545 if (nlh->nlmsg_flags & NLM_F_EXCL)
2547 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2552 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2555 ops = nft_select_set_ops(nla, &desc, policy);
2557 return PTR_ERR(ops);
2560 if (ops->privsize != NULL)
2561 size = ops->privsize(nla);
2564 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2568 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2569 err = nf_tables_set_alloc_name(&ctx, set, name);
2573 INIT_LIST_HEAD(&set->bindings);
2576 set->klen = desc.klen;
2578 set->dlen = desc.dlen;
2580 set->size = desc.size;
2582 err = ops->init(set, &desc, nla);
2586 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2590 list_add_tail(&set->list, &table->sets);
2596 module_put(ops->owner);
2600 static void nft_set_destroy(struct nft_set *set)
2602 set->ops->destroy(set);
2603 module_put(set->ops->owner);
2607 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2609 list_del(&set->list);
2610 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2611 nft_set_destroy(set);
2614 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2615 const struct nlmsghdr *nlh,
2616 const struct nlattr * const nla[])
2618 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2619 struct nft_set *set;
2623 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2624 return -EAFNOSUPPORT;
2625 if (nla[NFTA_SET_TABLE] == NULL)
2628 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2632 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2634 return PTR_ERR(set);
2635 if (set->flags & NFT_SET_INACTIVE)
2637 if (!list_empty(&set->bindings))
2640 err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2644 list_del(&set->list);
2648 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2649 const struct nft_set *set,
2650 const struct nft_set_iter *iter,
2651 const struct nft_set_elem *elem)
2653 enum nft_registers dreg;
2655 dreg = nft_type_to_reg(set->dtype);
2656 return nft_validate_data_load(ctx, dreg, &elem->data,
2657 set->dtype == NFT_DATA_VERDICT ?
2658 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2661 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2662 struct nft_set_binding *binding)
2664 struct nft_set_binding *i;
2665 struct nft_set_iter iter;
2667 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2670 if (set->flags & NFT_SET_MAP) {
2671 /* If the set is already bound to the same chain all
2672 * jumps are already validated for that chain.
2674 list_for_each_entry(i, &set->bindings, list) {
2675 if (i->chain == binding->chain)
2682 iter.fn = nf_tables_bind_check_setelem;
2684 set->ops->walk(ctx, set, &iter);
2686 /* Destroy anonymous sets if binding fails */
2687 if (set->flags & NFT_SET_ANONYMOUS)
2688 nf_tables_set_destroy(ctx, set);
2694 binding->chain = ctx->chain;
2695 list_add_tail(&binding->list, &set->bindings);
2699 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2700 struct nft_set_binding *binding)
2702 list_del(&binding->list);
2704 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2705 !(set->flags & NFT_SET_INACTIVE))
2706 nf_tables_set_destroy(ctx, set);
2713 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2714 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2715 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2716 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2719 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2720 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2721 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2722 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2723 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
2726 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2727 const struct sk_buff *skb,
2728 const struct nlmsghdr *nlh,
2729 const struct nlattr * const nla[],
2732 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2733 struct nft_af_info *afi;
2734 struct nft_table *table;
2735 struct net *net = sock_net(skb->sk);
2737 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2739 return PTR_ERR(afi);
2741 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2743 return PTR_ERR(table);
2744 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2747 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2751 static int nf_tables_fill_setelem(struct sk_buff *skb,
2752 const struct nft_set *set,
2753 const struct nft_set_elem *elem)
2755 unsigned char *b = skb_tail_pointer(skb);
2756 struct nlattr *nest;
2758 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2760 goto nla_put_failure;
2762 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2764 goto nla_put_failure;
2766 if (set->flags & NFT_SET_MAP &&
2767 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2768 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2769 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2771 goto nla_put_failure;
2773 if (elem->flags != 0)
2774 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2775 goto nla_put_failure;
2777 nla_nest_end(skb, nest);
2785 struct nft_set_dump_args {
2786 const struct netlink_callback *cb;
2787 struct nft_set_iter iter;
2788 struct sk_buff *skb;
2791 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2792 const struct nft_set *set,
2793 const struct nft_set_iter *iter,
2794 const struct nft_set_elem *elem)
2796 struct nft_set_dump_args *args;
2798 args = container_of(iter, struct nft_set_dump_args, iter);
2799 return nf_tables_fill_setelem(args->skb, set, elem);
2802 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2804 const struct nft_set *set;
2805 struct nft_set_dump_args args;
2807 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2808 struct nfgenmsg *nfmsg;
2809 struct nlmsghdr *nlh;
2810 struct nlattr *nest;
2814 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2815 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2819 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2824 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2826 return PTR_ERR(set);
2827 if (set->flags & NFT_SET_INACTIVE)
2830 event = NFT_MSG_NEWSETELEM;
2831 event |= NFNL_SUBSYS_NFTABLES << 8;
2832 portid = NETLINK_CB(cb->skb).portid;
2833 seq = cb->nlh->nlmsg_seq;
2835 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2838 goto nla_put_failure;
2840 nfmsg = nlmsg_data(nlh);
2841 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2842 nfmsg->version = NFNETLINK_V0;
2845 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2846 goto nla_put_failure;
2847 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2848 goto nla_put_failure;
2850 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2852 goto nla_put_failure;
2856 args.iter.skip = cb->args[0];
2857 args.iter.count = 0;
2859 args.iter.fn = nf_tables_dump_setelem;
2860 set->ops->walk(&ctx, set, &args.iter);
2862 nla_nest_end(skb, nest);
2863 nlmsg_end(skb, nlh);
2865 if (args.iter.err && args.iter.err != -EMSGSIZE)
2866 return args.iter.err;
2867 if (args.iter.count == cb->args[0])
2870 cb->args[0] = args.iter.count;
2877 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2878 const struct nlmsghdr *nlh,
2879 const struct nlattr * const nla[])
2881 const struct nft_set *set;
2885 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2889 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2891 return PTR_ERR(set);
2892 if (set->flags & NFT_SET_INACTIVE)
2895 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2896 struct netlink_dump_control c = {
2897 .dump = nf_tables_dump_set,
2899 return netlink_dump_start(nlsk, skb, nlh, &c);
2904 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2905 const struct nft_ctx *ctx, u32 seq,
2906 u32 portid, int event, u16 flags,
2907 const struct nft_set *set,
2908 const struct nft_set_elem *elem)
2910 struct nfgenmsg *nfmsg;
2911 struct nlmsghdr *nlh;
2912 struct nlattr *nest;
2915 event |= NFNL_SUBSYS_NFTABLES << 8;
2916 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2919 goto nla_put_failure;
2921 nfmsg = nlmsg_data(nlh);
2922 nfmsg->nfgen_family = ctx->afi->family;
2923 nfmsg->version = NFNETLINK_V0;
2926 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2927 goto nla_put_failure;
2928 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2929 goto nla_put_failure;
2931 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2933 goto nla_put_failure;
2935 err = nf_tables_fill_setelem(skb, set, elem);
2937 goto nla_put_failure;
2939 nla_nest_end(skb, nest);
2941 return nlmsg_end(skb, nlh);
2944 nlmsg_trim(skb, nlh);
2948 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2949 const struct nft_set *set,
2950 const struct nft_set_elem *elem,
2951 int event, u16 flags)
2953 struct net *net = ctx->net;
2954 u32 portid = ctx->portid;
2955 struct sk_buff *skb;
2958 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2962 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2966 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2973 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
2977 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2981 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
2983 struct nft_set *set)
2985 struct nft_trans *trans;
2987 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
2991 nft_trans_elem_set(trans) = set;
2995 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
2996 const struct nlattr *attr)
2998 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2999 struct nft_data_desc d1, d2;
3000 struct nft_set_elem elem;
3001 struct nft_set_binding *binding;
3002 enum nft_registers dreg;
3003 struct nft_trans *trans;
3006 if (set->size && set->nelems == set->size)
3009 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3010 nft_set_elem_policy);
3014 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3018 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3019 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3020 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3024 if (set->flags & NFT_SET_MAP) {
3025 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3026 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3028 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3029 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3032 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3036 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3040 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3044 if (set->ops->get(set, &elem) == 0)
3047 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3048 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3053 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3056 dreg = nft_type_to_reg(set->dtype);
3057 list_for_each_entry(binding, &set->bindings, list) {
3058 struct nft_ctx bind_ctx = {
3060 .table = ctx->table,
3061 .chain = (struct nft_chain *)binding->chain,
3064 err = nft_validate_data_load(&bind_ctx, dreg,
3065 &elem.data, d2.type);
3071 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3075 err = set->ops->insert(set, &elem);
3079 nft_trans_elem(trans) = elem;
3080 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3086 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3087 nft_data_uninit(&elem.data, d2.type);
3089 nft_data_uninit(&elem.key, d1.type);
3094 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3095 const struct nlmsghdr *nlh,
3096 const struct nlattr * const nla[])
3098 struct net *net = sock_net(skb->sk);
3099 const struct nlattr *attr;
3100 struct nft_set *set;
3104 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3108 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3110 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3111 set = nf_tables_set_lookup_byid(net,
3112 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3115 return PTR_ERR(set);
3118 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3121 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3122 err = nft_add_set_elem(&ctx, set, attr);
3129 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3130 const struct nlattr *attr)
3132 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3133 struct nft_data_desc desc;
3134 struct nft_set_elem elem;
3135 struct nft_trans *trans;
3138 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3139 nft_set_elem_policy);
3144 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3147 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3152 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3155 err = set->ops->get(set, &elem);
3159 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3163 nft_trans_elem(trans) = elem;
3164 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3166 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3167 if (set->flags & NFT_SET_MAP)
3168 nft_data_uninit(&elem.data, set->dtype);
3171 nft_data_uninit(&elem.key, desc.type);
3176 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3177 const struct nlmsghdr *nlh,
3178 const struct nlattr * const nla[])
3180 const struct nlattr *attr;
3181 struct nft_set *set;
3185 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3189 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3191 return PTR_ERR(set);
3192 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3195 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3196 err = nft_del_setelem(&ctx, set, attr);
3203 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3204 [NFT_MSG_NEWTABLE] = {
3205 .call_batch = nf_tables_newtable,
3206 .attr_count = NFTA_TABLE_MAX,
3207 .policy = nft_table_policy,
3209 [NFT_MSG_GETTABLE] = {
3210 .call = nf_tables_gettable,
3211 .attr_count = NFTA_TABLE_MAX,
3212 .policy = nft_table_policy,
3214 [NFT_MSG_DELTABLE] = {
3215 .call_batch = nf_tables_deltable,
3216 .attr_count = NFTA_TABLE_MAX,
3217 .policy = nft_table_policy,
3219 [NFT_MSG_NEWCHAIN] = {
3220 .call_batch = nf_tables_newchain,
3221 .attr_count = NFTA_CHAIN_MAX,
3222 .policy = nft_chain_policy,
3224 [NFT_MSG_GETCHAIN] = {
3225 .call = nf_tables_getchain,
3226 .attr_count = NFTA_CHAIN_MAX,
3227 .policy = nft_chain_policy,
3229 [NFT_MSG_DELCHAIN] = {
3230 .call_batch = nf_tables_delchain,
3231 .attr_count = NFTA_CHAIN_MAX,
3232 .policy = nft_chain_policy,
3234 [NFT_MSG_NEWRULE] = {
3235 .call_batch = nf_tables_newrule,
3236 .attr_count = NFTA_RULE_MAX,
3237 .policy = nft_rule_policy,
3239 [NFT_MSG_GETRULE] = {
3240 .call = nf_tables_getrule,
3241 .attr_count = NFTA_RULE_MAX,
3242 .policy = nft_rule_policy,
3244 [NFT_MSG_DELRULE] = {
3245 .call_batch = nf_tables_delrule,
3246 .attr_count = NFTA_RULE_MAX,
3247 .policy = nft_rule_policy,
3249 [NFT_MSG_NEWSET] = {
3250 .call_batch = nf_tables_newset,
3251 .attr_count = NFTA_SET_MAX,
3252 .policy = nft_set_policy,
3254 [NFT_MSG_GETSET] = {
3255 .call = nf_tables_getset,
3256 .attr_count = NFTA_SET_MAX,
3257 .policy = nft_set_policy,
3259 [NFT_MSG_DELSET] = {
3260 .call_batch = nf_tables_delset,
3261 .attr_count = NFTA_SET_MAX,
3262 .policy = nft_set_policy,
3264 [NFT_MSG_NEWSETELEM] = {
3265 .call_batch = nf_tables_newsetelem,
3266 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3267 .policy = nft_set_elem_list_policy,
3269 [NFT_MSG_GETSETELEM] = {
3270 .call = nf_tables_getsetelem,
3271 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3272 .policy = nft_set_elem_list_policy,
3274 [NFT_MSG_DELSETELEM] = {
3275 .call_batch = nf_tables_delsetelem,
3276 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3277 .policy = nft_set_elem_list_policy,
3281 static void nft_chain_commit_update(struct nft_trans *trans)
3283 struct nft_base_chain *basechain;
3285 if (nft_trans_chain_name(trans)[0])
3286 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3288 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3291 basechain = nft_base_chain(trans->ctx.chain);
3292 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3294 switch (nft_trans_chain_policy(trans)) {
3297 basechain->policy = nft_trans_chain_policy(trans);
3302 /* Schedule objects for release via rcu to make sure no packets are accesing
3305 static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3307 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3309 switch (trans->msg_type) {
3310 case NFT_MSG_DELTABLE:
3311 nf_tables_table_destroy(&trans->ctx);
3313 case NFT_MSG_DELCHAIN:
3314 nf_tables_chain_destroy(trans->ctx.chain);
3316 case NFT_MSG_DELRULE:
3317 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3319 case NFT_MSG_DELSET:
3320 nft_set_destroy(nft_trans_set(trans));
3326 static int nf_tables_commit(struct sk_buff *skb)
3328 struct net *net = sock_net(skb->sk);
3329 struct nft_trans *trans, *next;
3330 struct nft_set *set;
3332 /* Bump generation counter, invalidate any dump in progress */
3335 /* A new generation has just started */
3336 net->nft.gencursor = gencursor_next(net);
3338 /* Make sure all packets have left the previous generation before
3339 * purging old rules.
3343 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3344 switch (trans->msg_type) {
3345 case NFT_MSG_NEWTABLE:
3346 if (nft_trans_table_update(trans)) {
3347 if (!nft_trans_table_enable(trans)) {
3348 nf_tables_table_disable(trans->ctx.afi,
3350 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3353 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3355 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3356 nft_trans_destroy(trans);
3358 case NFT_MSG_DELTABLE:
3359 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3361 case NFT_MSG_NEWCHAIN:
3362 if (nft_trans_chain_update(trans))
3363 nft_chain_commit_update(trans);
3365 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3366 trans->ctx.table->use++;
3368 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3369 nft_trans_destroy(trans);
3371 case NFT_MSG_DELCHAIN:
3372 trans->ctx.table->use--;
3373 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3374 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3375 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3376 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3377 trans->ctx.afi->nops);
3380 case NFT_MSG_NEWRULE:
3381 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3382 nf_tables_rule_notify(&trans->ctx,
3383 nft_trans_rule(trans),
3385 nft_trans_destroy(trans);
3387 case NFT_MSG_DELRULE:
3388 list_del_rcu(&nft_trans_rule(trans)->list);
3389 nf_tables_rule_notify(&trans->ctx,
3390 nft_trans_rule(trans),
3393 case NFT_MSG_NEWSET:
3394 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3395 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3397 nft_trans_destroy(trans);
3399 case NFT_MSG_DELSET:
3400 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3403 case NFT_MSG_NEWSETELEM:
3404 nft_trans_elem_set(trans)->nelems++;
3405 nf_tables_setelem_notify(&trans->ctx,
3406 nft_trans_elem_set(trans),
3407 &nft_trans_elem(trans),
3408 NFT_MSG_NEWSETELEM, 0);
3409 nft_trans_destroy(trans);
3411 case NFT_MSG_DELSETELEM:
3412 nft_trans_elem_set(trans)->nelems--;
3413 nf_tables_setelem_notify(&trans->ctx,
3414 nft_trans_elem_set(trans),
3415 &nft_trans_elem(trans),
3416 NFT_MSG_DELSETELEM, 0);
3417 set = nft_trans_elem_set(trans);
3418 set->ops->get(set, &nft_trans_elem(trans));
3419 set->ops->remove(set, &nft_trans_elem(trans));
3420 nft_trans_destroy(trans);
3425 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3426 list_del(&trans->list);
3427 trans->ctx.nla = NULL;
3428 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
3434 /* Schedule objects for release via rcu to make sure no packets are accesing
3437 static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3439 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3441 switch (trans->msg_type) {
3442 case NFT_MSG_NEWTABLE:
3443 nf_tables_table_destroy(&trans->ctx);
3445 case NFT_MSG_NEWCHAIN:
3446 nf_tables_chain_destroy(trans->ctx.chain);
3448 case NFT_MSG_NEWRULE:
3449 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3451 case NFT_MSG_NEWSET:
3452 nft_set_destroy(nft_trans_set(trans));
3458 static int nf_tables_abort(struct sk_buff *skb)
3460 struct net *net = sock_net(skb->sk);
3461 struct nft_trans *trans, *next;
3462 struct nft_set *set;
3464 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3465 switch (trans->msg_type) {
3466 case NFT_MSG_NEWTABLE:
3467 if (nft_trans_table_update(trans)) {
3468 if (nft_trans_table_enable(trans)) {
3469 nf_tables_table_disable(trans->ctx.afi,
3471 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3473 nft_trans_destroy(trans);
3475 list_del(&trans->ctx.table->list);
3478 case NFT_MSG_DELTABLE:
3479 list_add_tail(&trans->ctx.table->list,
3480 &trans->ctx.afi->tables);
3481 nft_trans_destroy(trans);
3483 case NFT_MSG_NEWCHAIN:
3484 if (nft_trans_chain_update(trans)) {
3485 if (nft_trans_chain_stats(trans))
3486 free_percpu(nft_trans_chain_stats(trans));
3488 nft_trans_destroy(trans);
3490 list_del(&trans->ctx.chain->list);
3491 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3492 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3493 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3494 trans->ctx.afi->nops);
3498 case NFT_MSG_DELCHAIN:
3499 list_add_tail(&trans->ctx.chain->list,
3500 &trans->ctx.table->chains);
3501 nft_trans_destroy(trans);
3503 case NFT_MSG_NEWRULE:
3504 list_del_rcu(&nft_trans_rule(trans)->list);
3506 case NFT_MSG_DELRULE:
3507 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3508 nft_trans_destroy(trans);
3510 case NFT_MSG_NEWSET:
3511 list_del(&nft_trans_set(trans)->list);
3513 case NFT_MSG_DELSET:
3514 list_add_tail(&nft_trans_set(trans)->list,
3515 &trans->ctx.table->sets);
3516 nft_trans_destroy(trans);
3518 case NFT_MSG_NEWSETELEM:
3519 set = nft_trans_elem_set(trans);
3520 set->ops->get(set, &nft_trans_elem(trans));
3521 set->ops->remove(set, &nft_trans_elem(trans));
3522 nft_trans_destroy(trans);
3524 case NFT_MSG_DELSETELEM:
3525 nft_trans_destroy(trans);
3530 list_for_each_entry_safe_reverse(trans, next,
3531 &net->nft.commit_list, list) {
3532 list_del(&trans->list);
3533 trans->ctx.nla = NULL;
3534 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
3540 static const struct nfnetlink_subsystem nf_tables_subsys = {
3541 .name = "nf_tables",
3542 .subsys_id = NFNL_SUBSYS_NFTABLES,
3543 .cb_count = NFT_MSG_MAX,
3545 .commit = nf_tables_commit,
3546 .abort = nf_tables_abort,
3550 * Loop detection - walk through the ruleset beginning at the destination chain
3551 * of a new jump until either the source chain is reached (loop) or all
3552 * reachable chains have been traversed.
3554 * The loop check is performed whenever a new jump verdict is added to an
3555 * expression or verdict map or a verdict map is bound to a new chain.
3558 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3559 const struct nft_chain *chain);
3561 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3562 const struct nft_set *set,
3563 const struct nft_set_iter *iter,
3564 const struct nft_set_elem *elem)
3566 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3569 switch (elem->data.verdict) {
3572 return nf_tables_check_loops(ctx, elem->data.chain);
3578 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3579 const struct nft_chain *chain)
3581 const struct nft_rule *rule;
3582 const struct nft_expr *expr, *last;
3583 const struct nft_set *set;
3584 struct nft_set_binding *binding;
3585 struct nft_set_iter iter;
3587 if (ctx->chain == chain)
3590 list_for_each_entry(rule, &chain->rules, list) {
3591 nft_rule_for_each_expr(expr, last, rule) {
3592 const struct nft_data *data = NULL;
3595 if (!expr->ops->validate)
3598 err = expr->ops->validate(ctx, expr, &data);
3605 switch (data->verdict) {
3608 err = nf_tables_check_loops(ctx, data->chain);
3617 list_for_each_entry(set, &ctx->table->sets, list) {
3618 if (!(set->flags & NFT_SET_MAP) ||
3619 set->dtype != NFT_DATA_VERDICT)
3622 list_for_each_entry(binding, &set->bindings, list) {
3623 if (binding->chain != chain)
3629 iter.fn = nf_tables_loop_check_setelem;
3631 set->ops->walk(ctx, set, &iter);
3641 * nft_validate_input_register - validate an expressions' input register
3643 * @reg: the register number
3645 * Validate that the input register is one of the general purpose
3648 int nft_validate_input_register(enum nft_registers reg)
3650 if (reg <= NFT_REG_VERDICT)
3652 if (reg > NFT_REG_MAX)
3656 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3659 * nft_validate_output_register - validate an expressions' output register
3661 * @reg: the register number
3663 * Validate that the output register is one of the general purpose
3664 * registers or the verdict register.
3666 int nft_validate_output_register(enum nft_registers reg)
3668 if (reg < NFT_REG_VERDICT)
3670 if (reg > NFT_REG_MAX)
3674 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3677 * nft_validate_data_load - validate an expressions' data load
3679 * @ctx: context of the expression performing the load
3680 * @reg: the destination register number
3681 * @data: the data to load
3682 * @type: the data type
3684 * Validate that a data load uses the appropriate data type for
3685 * the destination register. A value of NULL for the data means
3686 * that its runtime gathered data, which is always of type
3689 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3690 const struct nft_data *data,
3691 enum nft_data_types type)
3696 case NFT_REG_VERDICT:
3697 if (data == NULL || type != NFT_DATA_VERDICT)
3700 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3701 err = nf_tables_check_loops(ctx, data->chain);
3705 if (ctx->chain->level + 1 > data->chain->level) {
3706 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3708 data->chain->level = ctx->chain->level + 1;
3714 if (data != NULL && type != NFT_DATA_VALUE)
3719 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3721 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3722 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3723 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3724 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3727 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3728 struct nft_data_desc *desc, const struct nlattr *nla)
3730 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3731 struct nft_chain *chain;
3734 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3738 if (!tb[NFTA_VERDICT_CODE])
3740 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3742 switch (data->verdict) {
3744 switch (data->verdict & NF_VERDICT_MASK) {
3756 desc->len = sizeof(data->verdict);
3760 if (!tb[NFTA_VERDICT_CHAIN])
3762 chain = nf_tables_chain_lookup(ctx->table,
3763 tb[NFTA_VERDICT_CHAIN]);
3765 return PTR_ERR(chain);
3766 if (chain->flags & NFT_BASE_CHAIN)
3770 data->chain = chain;
3771 desc->len = sizeof(data);
3775 desc->type = NFT_DATA_VERDICT;
3779 static void nft_verdict_uninit(const struct nft_data *data)
3781 switch (data->verdict) {
3789 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3791 struct nlattr *nest;
3793 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3795 goto nla_put_failure;
3797 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3798 goto nla_put_failure;
3800 switch (data->verdict) {
3803 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3804 goto nla_put_failure;
3806 nla_nest_end(skb, nest);
3813 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3814 struct nft_data_desc *desc, const struct nlattr *nla)
3821 if (len > sizeof(data->data))
3824 nla_memcpy(data->data, nla, sizeof(data->data));
3825 desc->type = NFT_DATA_VALUE;
3830 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3833 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3836 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3837 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3838 .len = FIELD_SIZEOF(struct nft_data, data) },
3839 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3843 * nft_data_init - parse nf_tables data netlink attributes
3845 * @ctx: context of the expression using the data
3846 * @data: destination struct nft_data
3847 * @desc: data description
3848 * @nla: netlink attribute containing data
3850 * Parse the netlink data attributes and initialize a struct nft_data.
3851 * The type and length of data are returned in the data description.
3853 * The caller can indicate that it only wants to accept data of type
3854 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3856 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3857 struct nft_data_desc *desc, const struct nlattr *nla)
3859 struct nlattr *tb[NFTA_DATA_MAX + 1];
3862 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3866 if (tb[NFTA_DATA_VALUE])
3867 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3868 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3869 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3872 EXPORT_SYMBOL_GPL(nft_data_init);
3875 * nft_data_uninit - release a nft_data item
3877 * @data: struct nft_data to release
3878 * @type: type of data
3880 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3881 * all others need to be released by calling this function.
3883 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3886 case NFT_DATA_VALUE:
3888 case NFT_DATA_VERDICT:
3889 return nft_verdict_uninit(data);
3894 EXPORT_SYMBOL_GPL(nft_data_uninit);
3896 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3897 enum nft_data_types type, unsigned int len)
3899 struct nlattr *nest;
3902 nest = nla_nest_start(skb, attr);
3907 case NFT_DATA_VALUE:
3908 err = nft_value_dump(skb, data, len);
3910 case NFT_DATA_VERDICT:
3911 err = nft_verdict_dump(skb, data);
3918 nla_nest_end(skb, nest);
3921 EXPORT_SYMBOL_GPL(nft_data_dump);
3923 static int nf_tables_init_net(struct net *net)
3925 INIT_LIST_HEAD(&net->nft.af_info);
3926 INIT_LIST_HEAD(&net->nft.commit_list);
3930 static struct pernet_operations nf_tables_net_ops = {
3931 .init = nf_tables_init_net,
3934 static int __init nf_tables_module_init(void)
3938 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3945 err = nf_tables_core_module_init();
3949 err = nfnetlink_subsys_register(&nf_tables_subsys);
3953 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3954 return register_pernet_subsys(&nf_tables_net_ops);
3956 nf_tables_core_module_exit();
3963 static void __exit nf_tables_module_exit(void)
3965 unregister_pernet_subsys(&nf_tables_net_ops);
3966 nfnetlink_subsys_unregister(&nf_tables_subsys);
3967 nf_tables_core_module_exit();
3971 module_init(nf_tables_module_init);
3972 module_exit(nf_tables_module_exit);
3974 MODULE_LICENSE("GPL");
3975 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3976 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);