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);
130 static void nf_tables_unregister_hooks(const struct nft_table *table,
131 const struct nft_chain *chain,
132 unsigned int hook_nops)
134 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
135 chain->flags & NFT_BASE_CHAIN)
136 nf_unregister_hooks(nft_base_chain(chain)->ops, hook_nops);
139 /* Internal table flags */
140 #define NFT_TABLE_INACTIVE (1 << 15)
142 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
144 struct nft_trans *trans;
146 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
150 if (msg_type == NFT_MSG_NEWTABLE)
151 ctx->table->flags |= NFT_TABLE_INACTIVE;
153 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
157 static int nft_deltable(struct nft_ctx *ctx)
161 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
165 list_del_rcu(&ctx->table->list);
169 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
171 struct nft_trans *trans;
173 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
177 if (msg_type == NFT_MSG_NEWCHAIN)
178 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
180 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
184 static int nft_delchain(struct nft_ctx *ctx)
188 err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
193 list_del_rcu(&ctx->chain->list);
199 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
201 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
204 static inline int gencursor_next(struct net *net)
206 return net->nft.gencursor+1 == 1 ? 1 : 0;
210 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
212 return (rule->genmask & (1 << gencursor_next(net))) == 0;
216 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
218 /* Now inactive, will be active in the future */
219 rule->genmask = (1 << net->nft.gencursor);
223 nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
225 rule->genmask = (1 << gencursor_next(net));
228 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
234 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
236 /* You cannot delete the same rule twice */
237 if (nft_rule_is_active_next(ctx->net, rule)) {
238 nft_rule_deactivate_next(ctx->net, rule);
245 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
246 struct nft_rule *rule)
248 struct nft_trans *trans;
250 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
254 nft_trans_rule(trans) = rule;
255 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
260 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
262 struct nft_trans *trans;
265 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
269 err = nf_tables_delrule_deactivate(ctx, rule);
271 nft_trans_destroy(trans);
278 static int nft_delrule_by_chain(struct nft_ctx *ctx)
280 struct nft_rule *rule;
283 list_for_each_entry(rule, &ctx->chain->rules, list) {
284 err = nft_delrule(ctx, rule);
291 /* Internal set flag */
292 #define NFT_SET_INACTIVE (1 << 15)
294 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
297 struct nft_trans *trans;
299 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
303 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
304 nft_trans_set_id(trans) =
305 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
306 set->flags |= NFT_SET_INACTIVE;
308 nft_trans_set(trans) = set;
309 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
314 static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
318 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
322 list_del_rcu(&set->list);
332 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
333 const struct nlattr *nla)
335 struct nft_table *table;
337 list_for_each_entry(table, &afi->tables, list) {
338 if (!nla_strcmp(nla, table->name))
344 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
345 const struct nlattr *nla)
347 struct nft_table *table;
350 return ERR_PTR(-EINVAL);
352 table = nft_table_lookup(afi, nla);
356 return ERR_PTR(-ENOENT);
359 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
361 return ++table->hgenerator;
364 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
366 static const struct nf_chain_type *
367 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
371 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
372 if (chain_type[family][i] != NULL &&
373 !nla_strcmp(nla, chain_type[family][i]->name))
374 return chain_type[family][i];
379 static const struct nf_chain_type *
380 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
381 const struct nlattr *nla,
384 const struct nf_chain_type *type;
386 type = __nf_tables_chain_type_lookup(afi->family, nla);
389 #ifdef CONFIG_MODULES
391 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
392 request_module("nft-chain-%u-%.*s", afi->family,
393 nla_len(nla), (const char *)nla_data(nla));
394 nfnl_lock(NFNL_SUBSYS_NFTABLES);
395 type = __nf_tables_chain_type_lookup(afi->family, nla);
397 return ERR_PTR(-EAGAIN);
400 return ERR_PTR(-ENOENT);
403 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
404 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
405 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
408 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
409 u32 portid, u32 seq, int event, u32 flags,
410 int family, const struct nft_table *table)
412 struct nlmsghdr *nlh;
413 struct nfgenmsg *nfmsg;
415 event |= NFNL_SUBSYS_NFTABLES << 8;
416 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
418 goto nla_put_failure;
420 nfmsg = nlmsg_data(nlh);
421 nfmsg->nfgen_family = family;
422 nfmsg->version = NFNETLINK_V0;
423 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
425 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
426 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
427 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
428 goto nla_put_failure;
434 nlmsg_trim(skb, nlh);
438 static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
444 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
448 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
452 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
453 event, 0, ctx->afi->family, ctx->table);
459 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
460 ctx->report, GFP_KERNEL);
463 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
469 static int nf_tables_dump_tables(struct sk_buff *skb,
470 struct netlink_callback *cb)
472 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
473 const struct nft_af_info *afi;
474 const struct nft_table *table;
475 unsigned int idx = 0, s_idx = cb->args[0];
476 struct net *net = sock_net(skb->sk);
477 int family = nfmsg->nfgen_family;
480 cb->seq = net->nft.base_seq;
482 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
483 if (family != NFPROTO_UNSPEC && family != afi->family)
486 list_for_each_entry_rcu(table, &afi->tables, list) {
490 memset(&cb->args[1], 0,
491 sizeof(cb->args) - sizeof(cb->args[0]));
492 if (nf_tables_fill_table_info(skb, net,
493 NETLINK_CB(cb->skb).portid,
497 afi->family, table) < 0)
500 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
511 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
512 const struct nlmsghdr *nlh,
513 const struct nlattr * const nla[])
515 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
516 const struct nft_af_info *afi;
517 const struct nft_table *table;
518 struct sk_buff *skb2;
519 struct net *net = sock_net(skb->sk);
520 int family = nfmsg->nfgen_family;
523 if (nlh->nlmsg_flags & NLM_F_DUMP) {
524 struct netlink_dump_control c = {
525 .dump = nf_tables_dump_tables,
527 return netlink_dump_start(nlsk, skb, nlh, &c);
530 afi = nf_tables_afinfo_lookup(net, family, false);
534 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
536 return PTR_ERR(table);
537 if (table->flags & NFT_TABLE_INACTIVE)
540 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
544 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
545 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
550 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
557 static int nf_tables_table_enable(const struct nft_af_info *afi,
558 struct nft_table *table)
560 struct nft_chain *chain;
563 list_for_each_entry(chain, &table->chains, list) {
564 if (!(chain->flags & NFT_BASE_CHAIN))
567 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
575 list_for_each_entry(chain, &table->chains, list) {
576 if (!(chain->flags & NFT_BASE_CHAIN))
582 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
587 static void nf_tables_table_disable(const struct nft_af_info *afi,
588 struct nft_table *table)
590 struct nft_chain *chain;
592 list_for_each_entry(chain, &table->chains, list) {
593 if (chain->flags & NFT_BASE_CHAIN)
594 nf_unregister_hooks(nft_base_chain(chain)->ops,
599 static int nf_tables_updtable(struct nft_ctx *ctx)
601 struct nft_trans *trans;
605 if (!ctx->nla[NFTA_TABLE_FLAGS])
608 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
609 if (flags & ~NFT_TABLE_F_DORMANT)
612 if (flags == ctx->table->flags)
615 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
616 sizeof(struct nft_trans_table));
620 if ((flags & NFT_TABLE_F_DORMANT) &&
621 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
622 nft_trans_table_enable(trans) = false;
623 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
624 ctx->table->flags & NFT_TABLE_F_DORMANT) {
625 ret = nf_tables_table_enable(ctx->afi, ctx->table);
627 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
628 nft_trans_table_enable(trans) = true;
634 nft_trans_table_update(trans) = true;
635 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
638 nft_trans_destroy(trans);
642 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
643 const struct nlmsghdr *nlh,
644 const struct nlattr * const nla[])
646 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
647 const struct nlattr *name;
648 struct nft_af_info *afi;
649 struct nft_table *table;
650 struct net *net = sock_net(skb->sk);
651 int family = nfmsg->nfgen_family;
656 afi = nf_tables_afinfo_lookup(net, family, true);
660 name = nla[NFTA_TABLE_NAME];
661 table = nf_tables_table_lookup(afi, name);
663 if (PTR_ERR(table) != -ENOENT)
664 return PTR_ERR(table);
669 if (table->flags & NFT_TABLE_INACTIVE)
671 if (nlh->nlmsg_flags & NLM_F_EXCL)
673 if (nlh->nlmsg_flags & NLM_F_REPLACE)
676 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
677 return nf_tables_updtable(&ctx);
680 if (nla[NFTA_TABLE_FLAGS]) {
681 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
682 if (flags & ~NFT_TABLE_F_DORMANT)
686 if (!try_module_get(afi->owner))
687 return -EAFNOSUPPORT;
689 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
691 module_put(afi->owner);
695 nla_strlcpy(table->name, name, nla_len(name));
696 INIT_LIST_HEAD(&table->chains);
697 INIT_LIST_HEAD(&table->sets);
698 table->flags = flags;
700 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
701 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
704 module_put(afi->owner);
707 list_add_tail_rcu(&table->list, &afi->tables);
711 static int nft_flush_table(struct nft_ctx *ctx)
714 struct nft_chain *chain, *nc;
715 struct nft_set *set, *ns;
717 list_for_each_entry(chain, &ctx->table->chains, list) {
720 err = nft_delrule_by_chain(ctx);
725 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
726 if (set->flags & NFT_SET_ANONYMOUS &&
727 !list_empty(&set->bindings))
730 err = nft_delset(ctx, set);
735 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
738 err = nft_delchain(ctx);
743 err = nft_deltable(ctx);
748 static int nft_flush(struct nft_ctx *ctx, int family)
750 struct nft_af_info *afi;
751 struct nft_table *table, *nt;
752 const struct nlattr * const *nla = ctx->nla;
755 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
756 if (family != AF_UNSPEC && afi->family != family)
760 list_for_each_entry_safe(table, nt, &afi->tables, list) {
761 if (nla[NFTA_TABLE_NAME] &&
762 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
767 err = nft_flush_table(ctx);
776 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
777 const struct nlmsghdr *nlh,
778 const struct nlattr * const nla[])
780 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
781 struct nft_af_info *afi;
782 struct nft_table *table;
783 struct net *net = sock_net(skb->sk);
784 int family = nfmsg->nfgen_family;
787 nft_ctx_init(&ctx, skb, nlh, NULL, NULL, NULL, nla);
788 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
789 return nft_flush(&ctx, family);
791 afi = nf_tables_afinfo_lookup(net, family, false);
795 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
797 return PTR_ERR(table);
798 if (table->flags & NFT_TABLE_INACTIVE)
804 return nft_flush_table(&ctx);
807 static void nf_tables_table_destroy(struct nft_ctx *ctx)
809 BUG_ON(ctx->table->use > 0);
812 module_put(ctx->afi->owner);
815 int nft_register_chain_type(const struct nf_chain_type *ctype)
819 nfnl_lock(NFNL_SUBSYS_NFTABLES);
820 if (chain_type[ctype->family][ctype->type] != NULL) {
824 chain_type[ctype->family][ctype->type] = ctype;
826 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
829 EXPORT_SYMBOL_GPL(nft_register_chain_type);
831 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
833 nfnl_lock(NFNL_SUBSYS_NFTABLES);
834 chain_type[ctype->family][ctype->type] = NULL;
835 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
837 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
843 static struct nft_chain *
844 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
846 struct nft_chain *chain;
848 list_for_each_entry(chain, &table->chains, list) {
849 if (chain->handle == handle)
853 return ERR_PTR(-ENOENT);
856 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
857 const struct nlattr *nla)
859 struct nft_chain *chain;
862 return ERR_PTR(-EINVAL);
864 list_for_each_entry(chain, &table->chains, list) {
865 if (!nla_strcmp(nla, chain->name))
869 return ERR_PTR(-ENOENT);
872 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
873 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
874 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
875 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
876 .len = NFT_CHAIN_MAXNAMELEN - 1 },
877 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
878 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
879 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
880 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
883 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
884 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
885 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
888 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
890 struct nft_stats *cpu_stats, total;
896 memset(&total, 0, sizeof(total));
897 for_each_possible_cpu(cpu) {
898 cpu_stats = per_cpu_ptr(stats, cpu);
900 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
901 pkts = cpu_stats->pkts;
902 bytes = cpu_stats->bytes;
903 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
905 total.bytes += bytes;
907 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
909 goto nla_put_failure;
911 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
912 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
913 goto nla_put_failure;
915 nla_nest_end(skb, nest);
922 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
923 u32 portid, u32 seq, int event, u32 flags,
924 int family, const struct nft_table *table,
925 const struct nft_chain *chain)
927 struct nlmsghdr *nlh;
928 struct nfgenmsg *nfmsg;
930 event |= NFNL_SUBSYS_NFTABLES << 8;
931 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
933 goto nla_put_failure;
935 nfmsg = nlmsg_data(nlh);
936 nfmsg->nfgen_family = family;
937 nfmsg->version = NFNETLINK_V0;
938 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
940 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
941 goto nla_put_failure;
942 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
943 goto nla_put_failure;
944 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
945 goto nla_put_failure;
947 if (chain->flags & NFT_BASE_CHAIN) {
948 const struct nft_base_chain *basechain = nft_base_chain(chain);
949 const struct nf_hook_ops *ops = &basechain->ops[0];
952 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
954 goto nla_put_failure;
955 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
956 goto nla_put_failure;
957 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
958 goto nla_put_failure;
959 nla_nest_end(skb, nest);
961 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
962 htonl(basechain->policy)))
963 goto nla_put_failure;
965 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
966 goto nla_put_failure;
968 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
969 goto nla_put_failure;
972 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
973 goto nla_put_failure;
979 nlmsg_trim(skb, nlh);
983 static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
989 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
993 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
997 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
998 event, 0, ctx->afi->family, ctx->table,
1005 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1006 ctx->report, GFP_KERNEL);
1009 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1015 static int nf_tables_dump_chains(struct sk_buff *skb,
1016 struct netlink_callback *cb)
1018 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1019 const struct nft_af_info *afi;
1020 const struct nft_table *table;
1021 const struct nft_chain *chain;
1022 unsigned int idx = 0, s_idx = cb->args[0];
1023 struct net *net = sock_net(skb->sk);
1024 int family = nfmsg->nfgen_family;
1027 cb->seq = net->nft.base_seq;
1029 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1030 if (family != NFPROTO_UNSPEC && family != afi->family)
1033 list_for_each_entry_rcu(table, &afi->tables, list) {
1034 list_for_each_entry_rcu(chain, &table->chains, list) {
1038 memset(&cb->args[1], 0,
1039 sizeof(cb->args) - sizeof(cb->args[0]));
1040 if (nf_tables_fill_chain_info(skb, net,
1041 NETLINK_CB(cb->skb).portid,
1045 afi->family, table, chain) < 0)
1048 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1060 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
1061 const struct nlmsghdr *nlh,
1062 const struct nlattr * const nla[])
1064 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1065 const struct nft_af_info *afi;
1066 const struct nft_table *table;
1067 const struct nft_chain *chain;
1068 struct sk_buff *skb2;
1069 struct net *net = sock_net(skb->sk);
1070 int family = nfmsg->nfgen_family;
1073 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1074 struct netlink_dump_control c = {
1075 .dump = nf_tables_dump_chains,
1077 return netlink_dump_start(nlsk, skb, nlh, &c);
1080 afi = nf_tables_afinfo_lookup(net, family, false);
1082 return PTR_ERR(afi);
1084 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1086 return PTR_ERR(table);
1087 if (table->flags & NFT_TABLE_INACTIVE)
1090 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1092 return PTR_ERR(chain);
1093 if (chain->flags & NFT_CHAIN_INACTIVE)
1096 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1100 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1101 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1102 family, table, chain);
1106 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1113 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1114 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1115 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1118 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1120 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1121 struct nft_stats __percpu *newstats;
1122 struct nft_stats *stats;
1125 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1127 return ERR_PTR(err);
1129 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1130 return ERR_PTR(-EINVAL);
1132 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1133 if (newstats == NULL)
1134 return ERR_PTR(-ENOMEM);
1136 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1137 * are not exposed to userspace.
1140 stats = this_cpu_ptr(newstats);
1141 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1142 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1148 static void nft_chain_stats_replace(struct nft_base_chain *chain,
1149 struct nft_stats __percpu *newstats)
1151 if (newstats == NULL)
1155 struct nft_stats __percpu *oldstats =
1156 nft_dereference(chain->stats);
1158 rcu_assign_pointer(chain->stats, newstats);
1160 free_percpu(oldstats);
1162 rcu_assign_pointer(chain->stats, newstats);
1165 static void nf_tables_chain_destroy(struct nft_chain *chain)
1167 BUG_ON(chain->use > 0);
1169 if (chain->flags & NFT_BASE_CHAIN) {
1170 module_put(nft_base_chain(chain)->type->owner);
1171 free_percpu(nft_base_chain(chain)->stats);
1172 kfree(nft_base_chain(chain));
1178 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1179 const struct nlmsghdr *nlh,
1180 const struct nlattr * const nla[])
1182 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1183 const struct nlattr * uninitialized_var(name);
1184 struct nft_af_info *afi;
1185 struct nft_table *table;
1186 struct nft_chain *chain;
1187 struct nft_base_chain *basechain = NULL;
1188 struct nlattr *ha[NFTA_HOOK_MAX + 1];
1189 struct net *net = sock_net(skb->sk);
1190 int family = nfmsg->nfgen_family;
1191 u8 policy = NF_ACCEPT;
1194 struct nft_stats __percpu *stats;
1199 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1201 afi = nf_tables_afinfo_lookup(net, family, true);
1203 return PTR_ERR(afi);
1205 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1207 return PTR_ERR(table);
1210 name = nla[NFTA_CHAIN_NAME];
1212 if (nla[NFTA_CHAIN_HANDLE]) {
1213 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1214 chain = nf_tables_chain_lookup_byhandle(table, handle);
1216 return PTR_ERR(chain);
1218 chain = nf_tables_chain_lookup(table, name);
1219 if (IS_ERR(chain)) {
1220 if (PTR_ERR(chain) != -ENOENT)
1221 return PTR_ERR(chain);
1226 if (nla[NFTA_CHAIN_POLICY]) {
1227 if ((chain != NULL &&
1228 !(chain->flags & NFT_BASE_CHAIN)) ||
1229 nla[NFTA_CHAIN_HOOK] == NULL)
1232 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1242 if (chain != NULL) {
1243 struct nft_stats *stats = NULL;
1244 struct nft_trans *trans;
1246 if (chain->flags & NFT_CHAIN_INACTIVE)
1248 if (nlh->nlmsg_flags & NLM_F_EXCL)
1250 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1253 if (nla[NFTA_CHAIN_HANDLE] && name &&
1254 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1257 if (nla[NFTA_CHAIN_COUNTERS]) {
1258 if (!(chain->flags & NFT_BASE_CHAIN))
1261 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1263 return PTR_ERR(stats);
1266 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1267 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1268 sizeof(struct nft_trans_chain));
1269 if (trans == NULL) {
1274 nft_trans_chain_stats(trans) = stats;
1275 nft_trans_chain_update(trans) = true;
1277 if (nla[NFTA_CHAIN_POLICY])
1278 nft_trans_chain_policy(trans) = policy;
1280 nft_trans_chain_policy(trans) = -1;
1282 if (nla[NFTA_CHAIN_HANDLE] && name) {
1283 nla_strlcpy(nft_trans_chain_name(trans), name,
1284 NFT_CHAIN_MAXNAMELEN);
1286 list_add_tail(&trans->list, &net->nft.commit_list);
1290 if (table->use == UINT_MAX)
1293 if (nla[NFTA_CHAIN_HOOK]) {
1294 const struct nf_chain_type *type;
1295 struct nf_hook_ops *ops;
1297 u32 hooknum, priority;
1299 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1300 if (nla[NFTA_CHAIN_TYPE]) {
1301 type = nf_tables_chain_type_lookup(afi,
1302 nla[NFTA_CHAIN_TYPE],
1305 return PTR_ERR(type);
1308 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1312 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1313 ha[NFTA_HOOK_PRIORITY] == NULL)
1316 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1317 if (hooknum >= afi->nhooks)
1319 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1321 if (!(type->hook_mask & (1 << hooknum)))
1323 if (!try_module_get(type->owner))
1325 hookfn = type->hooks[hooknum];
1327 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1328 if (basechain == NULL) {
1329 module_put(type->owner);
1333 if (nla[NFTA_CHAIN_COUNTERS]) {
1334 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1335 if (IS_ERR(stats)) {
1336 module_put(type->owner);
1338 return PTR_ERR(stats);
1340 basechain->stats = stats;
1342 stats = netdev_alloc_pcpu_stats(struct nft_stats);
1343 if (stats == NULL) {
1344 module_put(type->owner);
1348 rcu_assign_pointer(basechain->stats, stats);
1351 basechain->type = type;
1352 chain = &basechain->chain;
1354 for (i = 0; i < afi->nops; i++) {
1355 ops = &basechain->ops[i];
1357 ops->owner = afi->owner;
1358 ops->hooknum = hooknum;
1359 ops->priority = priority;
1361 ops->hook = afi->hooks[ops->hooknum];
1364 if (afi->hook_ops_init)
1365 afi->hook_ops_init(ops, i);
1368 chain->flags |= NFT_BASE_CHAIN;
1369 basechain->policy = policy;
1371 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1376 INIT_LIST_HEAD(&chain->rules);
1377 chain->handle = nf_tables_alloc_handle(table);
1379 chain->table = table;
1380 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1382 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1383 chain->flags & NFT_BASE_CHAIN) {
1384 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1389 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1390 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1395 list_add_tail_rcu(&chain->list, &table->chains);
1398 nf_tables_unregister_hooks(table, chain, afi->nops);
1400 nf_tables_chain_destroy(chain);
1404 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1405 const struct nlmsghdr *nlh,
1406 const struct nlattr * const nla[])
1408 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1409 struct nft_af_info *afi;
1410 struct nft_table *table;
1411 struct nft_chain *chain;
1412 struct net *net = sock_net(skb->sk);
1413 int family = nfmsg->nfgen_family;
1416 afi = nf_tables_afinfo_lookup(net, family, false);
1418 return PTR_ERR(afi);
1420 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1422 return PTR_ERR(table);
1423 if (table->flags & NFT_TABLE_INACTIVE)
1426 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1428 return PTR_ERR(chain);
1429 if (chain->flags & NFT_CHAIN_INACTIVE)
1434 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1436 return nft_delchain(&ctx);
1444 * nft_register_expr - register nf_tables expr type
1447 * Registers the expr type for use with nf_tables. Returns zero on
1448 * success or a negative errno code otherwise.
1450 int nft_register_expr(struct nft_expr_type *type)
1452 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1453 if (type->family == NFPROTO_UNSPEC)
1454 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1456 list_add_rcu(&type->list, &nf_tables_expressions);
1457 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1460 EXPORT_SYMBOL_GPL(nft_register_expr);
1463 * nft_unregister_expr - unregister nf_tables expr type
1466 * Unregisters the expr typefor use with nf_tables.
1468 void nft_unregister_expr(struct nft_expr_type *type)
1470 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1471 list_del_rcu(&type->list);
1472 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1474 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1476 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1479 const struct nft_expr_type *type;
1481 list_for_each_entry(type, &nf_tables_expressions, list) {
1482 if (!nla_strcmp(nla, type->name) &&
1483 (!type->family || type->family == family))
1489 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1492 const struct nft_expr_type *type;
1495 return ERR_PTR(-EINVAL);
1497 type = __nft_expr_type_get(family, nla);
1498 if (type != NULL && try_module_get(type->owner))
1501 #ifdef CONFIG_MODULES
1503 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1504 request_module("nft-expr-%u-%.*s", family,
1505 nla_len(nla), (char *)nla_data(nla));
1506 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1507 if (__nft_expr_type_get(family, nla))
1508 return ERR_PTR(-EAGAIN);
1510 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1511 request_module("nft-expr-%.*s",
1512 nla_len(nla), (char *)nla_data(nla));
1513 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1514 if (__nft_expr_type_get(family, nla))
1515 return ERR_PTR(-EAGAIN);
1518 return ERR_PTR(-ENOENT);
1521 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1522 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1523 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1526 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1527 const struct nft_expr *expr)
1529 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1530 goto nla_put_failure;
1532 if (expr->ops->dump) {
1533 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1535 goto nla_put_failure;
1536 if (expr->ops->dump(skb, expr) < 0)
1537 goto nla_put_failure;
1538 nla_nest_end(skb, data);
1547 struct nft_expr_info {
1548 const struct nft_expr_ops *ops;
1549 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1552 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1553 const struct nlattr *nla,
1554 struct nft_expr_info *info)
1556 const struct nft_expr_type *type;
1557 const struct nft_expr_ops *ops;
1558 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1561 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1565 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1567 return PTR_ERR(type);
1569 if (tb[NFTA_EXPR_DATA]) {
1570 err = nla_parse_nested(info->tb, type->maxattr,
1571 tb[NFTA_EXPR_DATA], type->policy);
1575 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1577 if (type->select_ops != NULL) {
1578 ops = type->select_ops(ctx,
1579 (const struct nlattr * const *)info->tb);
1591 module_put(type->owner);
1595 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1596 const struct nft_expr_info *info,
1597 struct nft_expr *expr)
1599 const struct nft_expr_ops *ops = info->ops;
1604 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1616 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1617 struct nft_expr *expr)
1619 if (expr->ops->destroy)
1620 expr->ops->destroy(ctx, expr);
1621 module_put(expr->ops->type->owner);
1628 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1631 struct nft_rule *rule;
1633 // FIXME: this sucks
1634 list_for_each_entry(rule, &chain->rules, list) {
1635 if (handle == rule->handle)
1639 return ERR_PTR(-ENOENT);
1642 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1643 const struct nlattr *nla)
1646 return ERR_PTR(-EINVAL);
1648 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1651 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1652 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1653 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1654 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1655 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1656 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1657 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1658 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1659 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1660 .len = NFT_USERDATA_MAXLEN },
1663 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1664 u32 portid, u32 seq, int event,
1665 u32 flags, int family,
1666 const struct nft_table *table,
1667 const struct nft_chain *chain,
1668 const struct nft_rule *rule)
1670 struct nlmsghdr *nlh;
1671 struct nfgenmsg *nfmsg;
1672 const struct nft_expr *expr, *next;
1673 struct nlattr *list;
1674 const struct nft_rule *prule;
1675 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1677 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1680 goto nla_put_failure;
1682 nfmsg = nlmsg_data(nlh);
1683 nfmsg->nfgen_family = family;
1684 nfmsg->version = NFNETLINK_V0;
1685 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
1687 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1688 goto nla_put_failure;
1689 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1690 goto nla_put_failure;
1691 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1692 goto nla_put_failure;
1694 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1695 prule = list_entry(rule->list.prev, struct nft_rule, list);
1696 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1697 cpu_to_be64(prule->handle)))
1698 goto nla_put_failure;
1701 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1703 goto nla_put_failure;
1704 nft_rule_for_each_expr(expr, next, rule) {
1705 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1707 goto nla_put_failure;
1708 if (nf_tables_fill_expr_info(skb, expr) < 0)
1709 goto nla_put_failure;
1710 nla_nest_end(skb, elem);
1712 nla_nest_end(skb, list);
1715 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1716 goto nla_put_failure;
1718 nlmsg_end(skb, nlh);
1722 nlmsg_trim(skb, nlh);
1726 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1727 const struct nft_rule *rule,
1730 struct sk_buff *skb;
1734 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1738 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1742 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1743 event, 0, ctx->afi->family, ctx->table,
1750 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1751 ctx->report, GFP_KERNEL);
1754 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1760 static int nf_tables_dump_rules(struct sk_buff *skb,
1761 struct netlink_callback *cb)
1763 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1764 const struct nft_af_info *afi;
1765 const struct nft_table *table;
1766 const struct nft_chain *chain;
1767 const struct nft_rule *rule;
1768 unsigned int idx = 0, s_idx = cb->args[0];
1769 struct net *net = sock_net(skb->sk);
1770 int family = nfmsg->nfgen_family;
1773 cb->seq = net->nft.base_seq;
1775 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1776 if (family != NFPROTO_UNSPEC && family != afi->family)
1779 list_for_each_entry_rcu(table, &afi->tables, list) {
1780 list_for_each_entry_rcu(chain, &table->chains, list) {
1781 list_for_each_entry_rcu(rule, &chain->rules, list) {
1782 if (!nft_rule_is_active(net, rule))
1787 memset(&cb->args[1], 0,
1788 sizeof(cb->args) - sizeof(cb->args[0]));
1789 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
1792 NLM_F_MULTI | NLM_F_APPEND,
1793 afi->family, table, chain, rule) < 0)
1796 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1810 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1811 const struct nlmsghdr *nlh,
1812 const struct nlattr * const nla[])
1814 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1815 const struct nft_af_info *afi;
1816 const struct nft_table *table;
1817 const struct nft_chain *chain;
1818 const struct nft_rule *rule;
1819 struct sk_buff *skb2;
1820 struct net *net = sock_net(skb->sk);
1821 int family = nfmsg->nfgen_family;
1824 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1825 struct netlink_dump_control c = {
1826 .dump = nf_tables_dump_rules,
1828 return netlink_dump_start(nlsk, skb, nlh, &c);
1831 afi = nf_tables_afinfo_lookup(net, family, false);
1833 return PTR_ERR(afi);
1835 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1837 return PTR_ERR(table);
1838 if (table->flags & NFT_TABLE_INACTIVE)
1841 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1843 return PTR_ERR(chain);
1844 if (chain->flags & NFT_CHAIN_INACTIVE)
1847 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1849 return PTR_ERR(rule);
1851 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1855 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
1856 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1857 family, table, chain, rule);
1861 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1868 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1869 struct nft_rule *rule)
1871 struct nft_expr *expr;
1874 * Careful: some expressions might not be initialized in case this
1875 * is called on error from nf_tables_newrule().
1877 expr = nft_expr_first(rule);
1878 while (expr->ops && expr != nft_expr_last(rule)) {
1879 nf_tables_expr_destroy(ctx, expr);
1880 expr = nft_expr_next(expr);
1885 #define NFT_RULE_MAXEXPRS 128
1887 static struct nft_expr_info *info;
1889 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1890 const struct nlmsghdr *nlh,
1891 const struct nlattr * const nla[])
1893 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1894 struct nft_af_info *afi;
1895 struct net *net = sock_net(skb->sk);
1896 struct nft_table *table;
1897 struct nft_chain *chain;
1898 struct nft_rule *rule, *old_rule = NULL;
1899 struct nft_trans *trans = NULL;
1900 struct nft_expr *expr;
1903 unsigned int size, i, n, ulen = 0;
1906 u64 handle, pos_handle;
1908 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1910 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1912 return PTR_ERR(afi);
1914 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1916 return PTR_ERR(table);
1918 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1920 return PTR_ERR(chain);
1922 if (nla[NFTA_RULE_HANDLE]) {
1923 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1924 rule = __nf_tables_rule_lookup(chain, handle);
1926 return PTR_ERR(rule);
1928 if (nlh->nlmsg_flags & NLM_F_EXCL)
1930 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1935 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1937 handle = nf_tables_alloc_handle(table);
1939 if (chain->use == UINT_MAX)
1943 if (nla[NFTA_RULE_POSITION]) {
1944 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1947 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1948 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1949 if (IS_ERR(old_rule))
1950 return PTR_ERR(old_rule);
1953 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1957 if (nla[NFTA_RULE_EXPRESSIONS]) {
1958 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1960 if (nla_type(tmp) != NFTA_LIST_ELEM)
1962 if (n == NFT_RULE_MAXEXPRS)
1964 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1967 size += info[n].ops->size;
1972 if (nla[NFTA_RULE_USERDATA])
1973 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1976 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1980 nft_rule_activate_next(net, rule);
1982 rule->handle = handle;
1987 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1989 expr = nft_expr_first(rule);
1990 for (i = 0; i < n; i++) {
1991 err = nf_tables_newexpr(&ctx, &info[i], expr);
1995 expr = nft_expr_next(expr);
1998 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1999 if (nft_rule_is_active_next(net, old_rule)) {
2000 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
2002 if (trans == NULL) {
2006 nft_rule_deactivate_next(net, old_rule);
2008 list_add_tail_rcu(&rule->list, &old_rule->list);
2013 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
2015 list_add_rcu(&rule->list, &old_rule->list);
2017 list_add_tail_rcu(&rule->list, &chain->rules);
2020 list_add_tail_rcu(&rule->list, &old_rule->list);
2022 list_add_rcu(&rule->list, &chain->rules);
2025 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2033 list_del_rcu(&rule->list);
2035 list_del_rcu(&nft_trans_rule(trans)->list);
2036 nft_rule_clear(net, nft_trans_rule(trans));
2037 nft_trans_destroy(trans);
2041 nf_tables_rule_destroy(&ctx, rule);
2043 for (i = 0; i < n; i++) {
2044 if (info[i].ops != NULL)
2045 module_put(info[i].ops->type->owner);
2050 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2051 const struct nlmsghdr *nlh,
2052 const struct nlattr * const nla[])
2054 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2055 struct nft_af_info *afi;
2056 struct net *net = sock_net(skb->sk);
2057 struct nft_table *table;
2058 struct nft_chain *chain = NULL;
2059 struct nft_rule *rule;
2060 int family = nfmsg->nfgen_family, err = 0;
2063 afi = nf_tables_afinfo_lookup(net, family, false);
2065 return PTR_ERR(afi);
2067 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
2069 return PTR_ERR(table);
2070 if (table->flags & NFT_TABLE_INACTIVE)
2073 if (nla[NFTA_RULE_CHAIN]) {
2074 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2076 return PTR_ERR(chain);
2079 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2082 if (nla[NFTA_RULE_HANDLE]) {
2083 rule = nf_tables_rule_lookup(chain,
2084 nla[NFTA_RULE_HANDLE]);
2086 return PTR_ERR(rule);
2088 err = nft_delrule(&ctx, rule);
2090 err = nft_delrule_by_chain(&ctx);
2093 list_for_each_entry(chain, &table->chains, list) {
2095 err = nft_delrule_by_chain(&ctx);
2108 static LIST_HEAD(nf_tables_set_ops);
2110 int nft_register_set(struct nft_set_ops *ops)
2112 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2113 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
2114 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2117 EXPORT_SYMBOL_GPL(nft_register_set);
2119 void nft_unregister_set(struct nft_set_ops *ops)
2121 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2122 list_del_rcu(&ops->list);
2123 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2125 EXPORT_SYMBOL_GPL(nft_unregister_set);
2128 * Select a set implementation based on the data characteristics and the
2129 * given policy. The total memory use might not be known if no size is
2130 * given, in that case the amount of memory per element is used.
2132 static const struct nft_set_ops *
2133 nft_select_set_ops(const struct nlattr * const nla[],
2134 const struct nft_set_desc *desc,
2135 enum nft_set_policies policy)
2137 const struct nft_set_ops *ops, *bops;
2138 struct nft_set_estimate est, best;
2141 #ifdef CONFIG_MODULES
2142 if (list_empty(&nf_tables_set_ops)) {
2143 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2144 request_module("nft-set");
2145 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2146 if (!list_empty(&nf_tables_set_ops))
2147 return ERR_PTR(-EAGAIN);
2151 if (nla[NFTA_SET_FLAGS] != NULL) {
2152 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2153 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2160 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2161 if ((ops->features & features) != features)
2163 if (!ops->estimate(desc, features, &est))
2167 case NFT_SET_POL_PERFORMANCE:
2168 if (est.class < best.class)
2170 if (est.class == best.class && est.size < best.size)
2173 case NFT_SET_POL_MEMORY:
2174 if (est.size < best.size)
2176 if (est.size == best.size && est.class < best.class)
2183 if (!try_module_get(ops->owner))
2186 module_put(bops->owner);
2195 return ERR_PTR(-EOPNOTSUPP);
2198 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2199 [NFTA_SET_TABLE] = { .type = NLA_STRING },
2200 [NFTA_SET_NAME] = { .type = NLA_STRING,
2201 .len = IFNAMSIZ - 1 },
2202 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2203 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2204 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2205 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2206 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
2207 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2208 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2209 [NFTA_SET_ID] = { .type = NLA_U32 },
2212 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2213 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
2216 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2217 const struct sk_buff *skb,
2218 const struct nlmsghdr *nlh,
2219 const struct nlattr * const nla[])
2221 struct net *net = sock_net(skb->sk);
2222 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2223 struct nft_af_info *afi = NULL;
2224 struct nft_table *table = NULL;
2226 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2227 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2229 return PTR_ERR(afi);
2232 if (nla[NFTA_SET_TABLE] != NULL) {
2234 return -EAFNOSUPPORT;
2236 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2238 return PTR_ERR(table);
2239 if (table->flags & NFT_TABLE_INACTIVE)
2243 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2247 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2248 const struct nlattr *nla)
2250 struct nft_set *set;
2253 return ERR_PTR(-EINVAL);
2255 list_for_each_entry(set, &table->sets, list) {
2256 if (!nla_strcmp(nla, set->name))
2259 return ERR_PTR(-ENOENT);
2262 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2263 const struct nlattr *nla)
2265 struct nft_trans *trans;
2266 u32 id = ntohl(nla_get_be32(nla));
2268 list_for_each_entry(trans, &net->nft.commit_list, list) {
2269 if (trans->msg_type == NFT_MSG_NEWSET &&
2270 id == nft_trans_set_id(trans))
2271 return nft_trans_set(trans);
2273 return ERR_PTR(-ENOENT);
2276 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2279 const struct nft_set *i;
2281 unsigned long *inuse;
2282 unsigned int n = 0, min = 0;
2284 p = strnchr(name, IFNAMSIZ, '%');
2286 if (p[1] != 'd' || strchr(p + 2, '%'))
2289 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2293 list_for_each_entry(i, &ctx->table->sets, list) {
2296 if (!sscanf(i->name, name, &tmp))
2298 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2301 set_bit(tmp - min, inuse);
2304 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2305 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2306 min += BITS_PER_BYTE * PAGE_SIZE;
2307 memset(inuse, 0, PAGE_SIZE);
2310 free_page((unsigned long)inuse);
2313 snprintf(set->name, sizeof(set->name), name, min + n);
2314 list_for_each_entry(i, &ctx->table->sets, list) {
2315 if (!strcmp(set->name, i->name))
2321 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2322 const struct nft_set *set, u16 event, u16 flags)
2324 struct nfgenmsg *nfmsg;
2325 struct nlmsghdr *nlh;
2326 struct nlattr *desc;
2327 u32 portid = ctx->portid;
2330 event |= NFNL_SUBSYS_NFTABLES << 8;
2331 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2334 goto nla_put_failure;
2336 nfmsg = nlmsg_data(nlh);
2337 nfmsg->nfgen_family = ctx->afi->family;
2338 nfmsg->version = NFNETLINK_V0;
2339 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
2341 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2342 goto nla_put_failure;
2343 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2344 goto nla_put_failure;
2345 if (set->flags != 0)
2346 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2347 goto nla_put_failure;
2349 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2350 goto nla_put_failure;
2351 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2352 goto nla_put_failure;
2353 if (set->flags & NFT_SET_MAP) {
2354 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2355 goto nla_put_failure;
2356 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2357 goto nla_put_failure;
2360 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2361 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2362 goto nla_put_failure;
2365 desc = nla_nest_start(skb, NFTA_SET_DESC);
2367 goto nla_put_failure;
2369 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2370 goto nla_put_failure;
2371 nla_nest_end(skb, desc);
2373 nlmsg_end(skb, nlh);
2377 nlmsg_trim(skb, nlh);
2381 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2382 const struct nft_set *set,
2383 int event, gfp_t gfp_flags)
2385 struct sk_buff *skb;
2386 u32 portid = ctx->portid;
2390 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2394 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2398 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2404 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2405 ctx->report, gfp_flags);
2408 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2412 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2414 const struct nft_set *set;
2415 unsigned int idx, s_idx = cb->args[0];
2416 struct nft_af_info *afi;
2417 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2418 struct net *net = sock_net(skb->sk);
2419 int cur_family = cb->args[3];
2420 struct nft_ctx *ctx = cb->data, ctx_set;
2426 cb->seq = net->nft.base_seq;
2428 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2429 if (ctx->afi && ctx->afi != afi)
2433 if (afi->family != cur_family)
2438 list_for_each_entry_rcu(table, &afi->tables, list) {
2439 if (ctx->table && ctx->table != table)
2443 if (cur_table != table)
2449 list_for_each_entry_rcu(set, &table->sets, list) {
2454 ctx_set.table = table;
2456 if (nf_tables_fill_set(skb, &ctx_set, set,
2460 cb->args[2] = (unsigned long) table;
2461 cb->args[3] = afi->family;
2464 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2478 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
2484 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2485 const struct nlmsghdr *nlh,
2486 const struct nlattr * const nla[])
2488 const struct nft_set *set;
2490 struct sk_buff *skb2;
2491 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2494 /* Verify existence before starting dump */
2495 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2499 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2500 struct netlink_dump_control c = {
2501 .dump = nf_tables_dump_sets,
2502 .done = nf_tables_dump_sets_done,
2504 struct nft_ctx *ctx_dump;
2506 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2507 if (ctx_dump == NULL)
2513 return netlink_dump_start(nlsk, skb, nlh, &c);
2516 /* Only accept unspec with dump */
2517 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2518 return -EAFNOSUPPORT;
2520 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2522 return PTR_ERR(set);
2523 if (set->flags & NFT_SET_INACTIVE)
2526 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2530 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2534 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2541 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2542 struct nft_set_desc *desc,
2543 const struct nlattr *nla)
2545 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2548 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2552 if (da[NFTA_SET_DESC_SIZE] != NULL)
2553 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2558 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2559 const struct nlmsghdr *nlh,
2560 const struct nlattr * const nla[])
2562 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2563 const struct nft_set_ops *ops;
2564 struct nft_af_info *afi;
2565 struct net *net = sock_net(skb->sk);
2566 struct nft_table *table;
2567 struct nft_set *set;
2569 char name[IFNAMSIZ];
2572 u32 ktype, dtype, flags, policy;
2573 struct nft_set_desc desc;
2576 if (nla[NFTA_SET_TABLE] == NULL ||
2577 nla[NFTA_SET_NAME] == NULL ||
2578 nla[NFTA_SET_KEY_LEN] == NULL ||
2579 nla[NFTA_SET_ID] == NULL)
2582 memset(&desc, 0, sizeof(desc));
2584 ktype = NFT_DATA_VALUE;
2585 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2586 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2587 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2591 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2592 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2596 if (nla[NFTA_SET_FLAGS] != NULL) {
2597 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2598 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2599 NFT_SET_INTERVAL | NFT_SET_MAP))
2604 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2605 if (!(flags & NFT_SET_MAP))
2608 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2609 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2610 dtype != NFT_DATA_VERDICT)
2613 if (dtype != NFT_DATA_VERDICT) {
2614 if (nla[NFTA_SET_DATA_LEN] == NULL)
2616 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2617 if (desc.dlen == 0 ||
2618 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2621 desc.dlen = sizeof(struct nft_data);
2622 } else if (flags & NFT_SET_MAP)
2625 policy = NFT_SET_POL_PERFORMANCE;
2626 if (nla[NFTA_SET_POLICY] != NULL)
2627 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2629 if (nla[NFTA_SET_DESC] != NULL) {
2630 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2635 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2637 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2639 return PTR_ERR(afi);
2641 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2643 return PTR_ERR(table);
2645 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2647 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2649 if (PTR_ERR(set) != -ENOENT)
2650 return PTR_ERR(set);
2655 if (nlh->nlmsg_flags & NLM_F_EXCL)
2657 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2662 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2665 ops = nft_select_set_ops(nla, &desc, policy);
2667 return PTR_ERR(ops);
2670 if (ops->privsize != NULL)
2671 size = ops->privsize(nla);
2674 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2678 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2679 err = nf_tables_set_alloc_name(&ctx, set, name);
2683 INIT_LIST_HEAD(&set->bindings);
2686 set->klen = desc.klen;
2688 set->dlen = desc.dlen;
2690 set->size = desc.size;
2691 set->policy = policy;
2693 err = ops->init(set, &desc, nla);
2697 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2701 list_add_tail_rcu(&set->list, &table->sets);
2708 module_put(ops->owner);
2712 static void nft_set_destroy(struct nft_set *set)
2714 set->ops->destroy(set);
2715 module_put(set->ops->owner);
2719 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2721 list_del_rcu(&set->list);
2722 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2723 nft_set_destroy(set);
2726 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2727 const struct nlmsghdr *nlh,
2728 const struct nlattr * const nla[])
2730 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2731 struct nft_set *set;
2735 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2736 return -EAFNOSUPPORT;
2737 if (nla[NFTA_SET_TABLE] == NULL)
2740 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2744 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2746 return PTR_ERR(set);
2747 if (set->flags & NFT_SET_INACTIVE)
2749 if (!list_empty(&set->bindings))
2752 return nft_delset(&ctx, set);
2755 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2756 const struct nft_set *set,
2757 const struct nft_set_iter *iter,
2758 const struct nft_set_elem *elem)
2760 enum nft_registers dreg;
2762 dreg = nft_type_to_reg(set->dtype);
2763 return nft_validate_data_load(ctx, dreg, &elem->data,
2764 set->dtype == NFT_DATA_VERDICT ?
2765 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2768 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2769 struct nft_set_binding *binding)
2771 struct nft_set_binding *i;
2772 struct nft_set_iter iter;
2774 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2777 if (set->flags & NFT_SET_MAP) {
2778 /* If the set is already bound to the same chain all
2779 * jumps are already validated for that chain.
2781 list_for_each_entry(i, &set->bindings, list) {
2782 if (i->chain == binding->chain)
2789 iter.fn = nf_tables_bind_check_setelem;
2791 set->ops->walk(ctx, set, &iter);
2793 /* Destroy anonymous sets if binding fails */
2794 if (set->flags & NFT_SET_ANONYMOUS)
2795 nf_tables_set_destroy(ctx, set);
2801 binding->chain = ctx->chain;
2802 list_add_tail_rcu(&binding->list, &set->bindings);
2806 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2807 struct nft_set_binding *binding)
2809 list_del_rcu(&binding->list);
2811 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2812 !(set->flags & NFT_SET_INACTIVE))
2813 nf_tables_set_destroy(ctx, set);
2820 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2821 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2822 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2823 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2826 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2827 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2828 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2829 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2830 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
2833 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2834 const struct sk_buff *skb,
2835 const struct nlmsghdr *nlh,
2836 const struct nlattr * const nla[],
2839 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2840 struct nft_af_info *afi;
2841 struct nft_table *table;
2842 struct net *net = sock_net(skb->sk);
2844 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2846 return PTR_ERR(afi);
2848 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2850 return PTR_ERR(table);
2851 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2854 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2858 static int nf_tables_fill_setelem(struct sk_buff *skb,
2859 const struct nft_set *set,
2860 const struct nft_set_elem *elem)
2862 unsigned char *b = skb_tail_pointer(skb);
2863 struct nlattr *nest;
2865 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2867 goto nla_put_failure;
2869 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2871 goto nla_put_failure;
2873 if (set->flags & NFT_SET_MAP &&
2874 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2875 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2876 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2878 goto nla_put_failure;
2880 if (elem->flags != 0)
2881 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2882 goto nla_put_failure;
2884 nla_nest_end(skb, nest);
2892 struct nft_set_dump_args {
2893 const struct netlink_callback *cb;
2894 struct nft_set_iter iter;
2895 struct sk_buff *skb;
2898 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2899 const struct nft_set *set,
2900 const struct nft_set_iter *iter,
2901 const struct nft_set_elem *elem)
2903 struct nft_set_dump_args *args;
2905 args = container_of(iter, struct nft_set_dump_args, iter);
2906 return nf_tables_fill_setelem(args->skb, set, elem);
2909 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2911 const struct nft_set *set;
2912 struct nft_set_dump_args args;
2914 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2915 struct nfgenmsg *nfmsg;
2916 struct nlmsghdr *nlh;
2917 struct nlattr *nest;
2921 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2922 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2926 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2931 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2933 return PTR_ERR(set);
2934 if (set->flags & NFT_SET_INACTIVE)
2937 event = NFT_MSG_NEWSETELEM;
2938 event |= NFNL_SUBSYS_NFTABLES << 8;
2939 portid = NETLINK_CB(cb->skb).portid;
2940 seq = cb->nlh->nlmsg_seq;
2942 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2945 goto nla_put_failure;
2947 nfmsg = nlmsg_data(nlh);
2948 nfmsg->nfgen_family = ctx.afi->family;
2949 nfmsg->version = NFNETLINK_V0;
2950 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
2952 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2953 goto nla_put_failure;
2954 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2955 goto nla_put_failure;
2957 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2959 goto nla_put_failure;
2963 args.iter.skip = cb->args[0];
2964 args.iter.count = 0;
2966 args.iter.fn = nf_tables_dump_setelem;
2967 set->ops->walk(&ctx, set, &args.iter);
2969 nla_nest_end(skb, nest);
2970 nlmsg_end(skb, nlh);
2972 if (args.iter.err && args.iter.err != -EMSGSIZE)
2973 return args.iter.err;
2974 if (args.iter.count == cb->args[0])
2977 cb->args[0] = args.iter.count;
2984 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2985 const struct nlmsghdr *nlh,
2986 const struct nlattr * const nla[])
2988 const struct nft_set *set;
2992 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2996 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2998 return PTR_ERR(set);
2999 if (set->flags & NFT_SET_INACTIVE)
3002 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3003 struct netlink_dump_control c = {
3004 .dump = nf_tables_dump_set,
3006 return netlink_dump_start(nlsk, skb, nlh, &c);
3011 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3012 const struct nft_ctx *ctx, u32 seq,
3013 u32 portid, int event, u16 flags,
3014 const struct nft_set *set,
3015 const struct nft_set_elem *elem)
3017 struct nfgenmsg *nfmsg;
3018 struct nlmsghdr *nlh;
3019 struct nlattr *nest;
3022 event |= NFNL_SUBSYS_NFTABLES << 8;
3023 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3026 goto nla_put_failure;
3028 nfmsg = nlmsg_data(nlh);
3029 nfmsg->nfgen_family = ctx->afi->family;
3030 nfmsg->version = NFNETLINK_V0;
3031 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
3033 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3034 goto nla_put_failure;
3035 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3036 goto nla_put_failure;
3038 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3040 goto nla_put_failure;
3042 err = nf_tables_fill_setelem(skb, set, elem);
3044 goto nla_put_failure;
3046 nla_nest_end(skb, nest);
3048 nlmsg_end(skb, nlh);
3052 nlmsg_trim(skb, nlh);
3056 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3057 const struct nft_set *set,
3058 const struct nft_set_elem *elem,
3059 int event, u16 flags)
3061 struct net *net = ctx->net;
3062 u32 portid = ctx->portid;
3063 struct sk_buff *skb;
3066 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3070 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3074 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3081 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3085 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3089 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3091 struct nft_set *set)
3093 struct nft_trans *trans;
3095 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3099 nft_trans_elem_set(trans) = set;
3103 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3104 const struct nlattr *attr)
3106 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3107 struct nft_data_desc d1, d2;
3108 struct nft_set_elem elem;
3109 struct nft_set_binding *binding;
3110 enum nft_registers dreg;
3111 struct nft_trans *trans;
3114 if (set->size && set->nelems == set->size)
3117 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3118 nft_set_elem_policy);
3122 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3126 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3127 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3128 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3132 if (set->flags & NFT_SET_MAP) {
3133 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3134 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3136 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3137 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3140 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3144 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3148 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3152 if (set->ops->get(set, &elem) == 0)
3155 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3156 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3161 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3164 dreg = nft_type_to_reg(set->dtype);
3165 list_for_each_entry(binding, &set->bindings, list) {
3166 struct nft_ctx bind_ctx = {
3168 .table = ctx->table,
3169 .chain = (struct nft_chain *)binding->chain,
3172 err = nft_validate_data_load(&bind_ctx, dreg,
3173 &elem.data, d2.type);
3179 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3183 err = set->ops->insert(set, &elem);
3187 nft_trans_elem(trans) = elem;
3188 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3194 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3195 nft_data_uninit(&elem.data, d2.type);
3197 nft_data_uninit(&elem.key, d1.type);
3202 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3203 const struct nlmsghdr *nlh,
3204 const struct nlattr * const nla[])
3206 struct net *net = sock_net(skb->sk);
3207 const struct nlattr *attr;
3208 struct nft_set *set;
3212 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3215 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3219 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3221 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3222 set = nf_tables_set_lookup_byid(net,
3223 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3226 return PTR_ERR(set);
3229 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3232 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3233 err = nft_add_set_elem(&ctx, set, attr);
3242 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3243 const struct nlattr *attr)
3245 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3246 struct nft_data_desc desc;
3247 struct nft_set_elem elem;
3248 struct nft_trans *trans;
3251 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3252 nft_set_elem_policy);
3257 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3260 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3265 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3268 err = set->ops->get(set, &elem);
3272 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3273 if (trans == NULL) {
3278 nft_trans_elem(trans) = elem;
3279 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3282 nft_data_uninit(&elem.key, desc.type);
3287 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3288 const struct nlmsghdr *nlh,
3289 const struct nlattr * const nla[])
3291 const struct nlattr *attr;
3292 struct nft_set *set;
3296 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3299 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3303 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3305 return PTR_ERR(set);
3306 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3309 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3310 err = nft_del_setelem(&ctx, set, attr);
3319 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3320 u32 portid, u32 seq)
3322 struct nlmsghdr *nlh;
3323 struct nfgenmsg *nfmsg;
3324 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3326 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3328 goto nla_put_failure;
3330 nfmsg = nlmsg_data(nlh);
3331 nfmsg->nfgen_family = AF_UNSPEC;
3332 nfmsg->version = NFNETLINK_V0;
3333 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3335 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3336 goto nla_put_failure;
3338 nlmsg_end(skb, nlh);
3342 nlmsg_trim(skb, nlh);
3346 static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3348 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3349 struct sk_buff *skb2;
3352 if (nlmsg_report(nlh) &&
3353 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3357 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3361 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3368 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3369 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3372 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3378 static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3379 const struct nlmsghdr *nlh,
3380 const struct nlattr * const nla[])
3382 struct net *net = sock_net(skb->sk);
3383 struct sk_buff *skb2;
3386 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3390 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3395 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3401 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3402 [NFT_MSG_NEWTABLE] = {
3403 .call_batch = nf_tables_newtable,
3404 .attr_count = NFTA_TABLE_MAX,
3405 .policy = nft_table_policy,
3407 [NFT_MSG_GETTABLE] = {
3408 .call = nf_tables_gettable,
3409 .attr_count = NFTA_TABLE_MAX,
3410 .policy = nft_table_policy,
3412 [NFT_MSG_DELTABLE] = {
3413 .call_batch = nf_tables_deltable,
3414 .attr_count = NFTA_TABLE_MAX,
3415 .policy = nft_table_policy,
3417 [NFT_MSG_NEWCHAIN] = {
3418 .call_batch = nf_tables_newchain,
3419 .attr_count = NFTA_CHAIN_MAX,
3420 .policy = nft_chain_policy,
3422 [NFT_MSG_GETCHAIN] = {
3423 .call = nf_tables_getchain,
3424 .attr_count = NFTA_CHAIN_MAX,
3425 .policy = nft_chain_policy,
3427 [NFT_MSG_DELCHAIN] = {
3428 .call_batch = nf_tables_delchain,
3429 .attr_count = NFTA_CHAIN_MAX,
3430 .policy = nft_chain_policy,
3432 [NFT_MSG_NEWRULE] = {
3433 .call_batch = nf_tables_newrule,
3434 .attr_count = NFTA_RULE_MAX,
3435 .policy = nft_rule_policy,
3437 [NFT_MSG_GETRULE] = {
3438 .call = nf_tables_getrule,
3439 .attr_count = NFTA_RULE_MAX,
3440 .policy = nft_rule_policy,
3442 [NFT_MSG_DELRULE] = {
3443 .call_batch = nf_tables_delrule,
3444 .attr_count = NFTA_RULE_MAX,
3445 .policy = nft_rule_policy,
3447 [NFT_MSG_NEWSET] = {
3448 .call_batch = nf_tables_newset,
3449 .attr_count = NFTA_SET_MAX,
3450 .policy = nft_set_policy,
3452 [NFT_MSG_GETSET] = {
3453 .call = nf_tables_getset,
3454 .attr_count = NFTA_SET_MAX,
3455 .policy = nft_set_policy,
3457 [NFT_MSG_DELSET] = {
3458 .call_batch = nf_tables_delset,
3459 .attr_count = NFTA_SET_MAX,
3460 .policy = nft_set_policy,
3462 [NFT_MSG_NEWSETELEM] = {
3463 .call_batch = nf_tables_newsetelem,
3464 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3465 .policy = nft_set_elem_list_policy,
3467 [NFT_MSG_GETSETELEM] = {
3468 .call = nf_tables_getsetelem,
3469 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3470 .policy = nft_set_elem_list_policy,
3472 [NFT_MSG_DELSETELEM] = {
3473 .call_batch = nf_tables_delsetelem,
3474 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3475 .policy = nft_set_elem_list_policy,
3477 [NFT_MSG_GETGEN] = {
3478 .call = nf_tables_getgen,
3482 static void nft_chain_commit_update(struct nft_trans *trans)
3484 struct nft_base_chain *basechain;
3486 if (nft_trans_chain_name(trans)[0])
3487 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3489 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3492 basechain = nft_base_chain(trans->ctx.chain);
3493 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3495 switch (nft_trans_chain_policy(trans)) {
3498 basechain->policy = nft_trans_chain_policy(trans);
3503 static void nf_tables_commit_release(struct nft_trans *trans)
3505 switch (trans->msg_type) {
3506 case NFT_MSG_DELTABLE:
3507 nf_tables_table_destroy(&trans->ctx);
3509 case NFT_MSG_DELCHAIN:
3510 nf_tables_chain_destroy(trans->ctx.chain);
3512 case NFT_MSG_DELRULE:
3513 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3515 case NFT_MSG_DELSET:
3516 nft_set_destroy(nft_trans_set(trans));
3522 static int nf_tables_commit(struct sk_buff *skb)
3524 struct net *net = sock_net(skb->sk);
3525 struct nft_trans *trans, *next;
3526 struct nft_trans_elem *te;
3528 /* Bump generation counter, invalidate any dump in progress */
3529 while (++net->nft.base_seq == 0);
3531 /* A new generation has just started */
3532 net->nft.gencursor = gencursor_next(net);
3534 /* Make sure all packets have left the previous generation before
3535 * purging old rules.
3539 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3540 switch (trans->msg_type) {
3541 case NFT_MSG_NEWTABLE:
3542 if (nft_trans_table_update(trans)) {
3543 if (!nft_trans_table_enable(trans)) {
3544 nf_tables_table_disable(trans->ctx.afi,
3546 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3549 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3551 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3552 nft_trans_destroy(trans);
3554 case NFT_MSG_DELTABLE:
3555 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3557 case NFT_MSG_NEWCHAIN:
3558 if (nft_trans_chain_update(trans))
3559 nft_chain_commit_update(trans);
3561 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3563 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3564 nft_trans_destroy(trans);
3566 case NFT_MSG_DELCHAIN:
3567 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3568 nf_tables_unregister_hooks(trans->ctx.table,
3570 trans->ctx.afi->nops);
3572 case NFT_MSG_NEWRULE:
3573 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3574 nf_tables_rule_notify(&trans->ctx,
3575 nft_trans_rule(trans),
3577 nft_trans_destroy(trans);
3579 case NFT_MSG_DELRULE:
3580 list_del_rcu(&nft_trans_rule(trans)->list);
3581 nf_tables_rule_notify(&trans->ctx,
3582 nft_trans_rule(trans),
3585 case NFT_MSG_NEWSET:
3586 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3587 /* This avoids hitting -EBUSY when deleting the table
3588 * from the transaction.
3590 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3591 !list_empty(&nft_trans_set(trans)->bindings))
3592 trans->ctx.table->use--;
3594 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3595 NFT_MSG_NEWSET, GFP_KERNEL);
3596 nft_trans_destroy(trans);
3598 case NFT_MSG_DELSET:
3599 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3600 NFT_MSG_DELSET, GFP_KERNEL);
3602 case NFT_MSG_NEWSETELEM:
3603 nf_tables_setelem_notify(&trans->ctx,
3604 nft_trans_elem_set(trans),
3605 &nft_trans_elem(trans),
3606 NFT_MSG_NEWSETELEM, 0);
3607 nft_trans_destroy(trans);
3609 case NFT_MSG_DELSETELEM:
3610 te = (struct nft_trans_elem *)trans->data;
3611 nf_tables_setelem_notify(&trans->ctx, te->set,
3613 NFT_MSG_DELSETELEM, 0);
3614 te->set->ops->get(te->set, &te->elem);
3615 te->set->ops->remove(te->set, &te->elem);
3616 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3617 if (te->elem.flags & NFT_SET_MAP) {
3618 nft_data_uninit(&te->elem.data,
3621 nft_trans_destroy(trans);
3628 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3629 list_del(&trans->list);
3630 nf_tables_commit_release(trans);
3633 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3638 static void nf_tables_abort_release(struct nft_trans *trans)
3640 switch (trans->msg_type) {
3641 case NFT_MSG_NEWTABLE:
3642 nf_tables_table_destroy(&trans->ctx);
3644 case NFT_MSG_NEWCHAIN:
3645 nf_tables_chain_destroy(trans->ctx.chain);
3647 case NFT_MSG_NEWRULE:
3648 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3650 case NFT_MSG_NEWSET:
3651 nft_set_destroy(nft_trans_set(trans));
3657 static int nf_tables_abort(struct sk_buff *skb)
3659 struct net *net = sock_net(skb->sk);
3660 struct nft_trans *trans, *next;
3661 struct nft_set *set;
3663 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3664 switch (trans->msg_type) {
3665 case NFT_MSG_NEWTABLE:
3666 if (nft_trans_table_update(trans)) {
3667 if (nft_trans_table_enable(trans)) {
3668 nf_tables_table_disable(trans->ctx.afi,
3670 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3672 nft_trans_destroy(trans);
3674 list_del_rcu(&trans->ctx.table->list);
3677 case NFT_MSG_DELTABLE:
3678 list_add_tail_rcu(&trans->ctx.table->list,
3679 &trans->ctx.afi->tables);
3680 nft_trans_destroy(trans);
3682 case NFT_MSG_NEWCHAIN:
3683 if (nft_trans_chain_update(trans)) {
3684 free_percpu(nft_trans_chain_stats(trans));
3686 nft_trans_destroy(trans);
3688 trans->ctx.table->use--;
3689 list_del_rcu(&trans->ctx.chain->list);
3690 nf_tables_unregister_hooks(trans->ctx.table,
3692 trans->ctx.afi->nops);
3695 case NFT_MSG_DELCHAIN:
3696 trans->ctx.table->use++;
3697 list_add_tail_rcu(&trans->ctx.chain->list,
3698 &trans->ctx.table->chains);
3699 nft_trans_destroy(trans);
3701 case NFT_MSG_NEWRULE:
3702 trans->ctx.chain->use--;
3703 list_del_rcu(&nft_trans_rule(trans)->list);
3705 case NFT_MSG_DELRULE:
3706 trans->ctx.chain->use++;
3707 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3708 nft_trans_destroy(trans);
3710 case NFT_MSG_NEWSET:
3711 trans->ctx.table->use--;
3712 list_del_rcu(&nft_trans_set(trans)->list);
3714 case NFT_MSG_DELSET:
3715 trans->ctx.table->use++;
3716 list_add_tail_rcu(&nft_trans_set(trans)->list,
3717 &trans->ctx.table->sets);
3718 nft_trans_destroy(trans);
3720 case NFT_MSG_NEWSETELEM:
3721 nft_trans_elem_set(trans)->nelems--;
3722 set = nft_trans_elem_set(trans);
3723 set->ops->get(set, &nft_trans_elem(trans));
3724 set->ops->remove(set, &nft_trans_elem(trans));
3725 nft_trans_destroy(trans);
3727 case NFT_MSG_DELSETELEM:
3728 nft_trans_elem_set(trans)->nelems++;
3729 nft_trans_destroy(trans);
3736 list_for_each_entry_safe_reverse(trans, next,
3737 &net->nft.commit_list, list) {
3738 list_del(&trans->list);
3739 nf_tables_abort_release(trans);
3745 static const struct nfnetlink_subsystem nf_tables_subsys = {
3746 .name = "nf_tables",
3747 .subsys_id = NFNL_SUBSYS_NFTABLES,
3748 .cb_count = NFT_MSG_MAX,
3750 .commit = nf_tables_commit,
3751 .abort = nf_tables_abort,
3754 int nft_chain_validate_dependency(const struct nft_chain *chain,
3755 enum nft_chain_type type)
3757 const struct nft_base_chain *basechain;
3759 if (chain->flags & NFT_BASE_CHAIN) {
3760 basechain = nft_base_chain(chain);
3761 if (basechain->type->type != type)
3766 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
3768 int nft_chain_validate_hooks(const struct nft_chain *chain,
3769 unsigned int hook_flags)
3771 struct nft_base_chain *basechain;
3773 if (chain->flags & NFT_BASE_CHAIN) {
3774 basechain = nft_base_chain(chain);
3776 if ((1 << basechain->ops[0].hooknum) & hook_flags)
3784 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
3787 * Loop detection - walk through the ruleset beginning at the destination chain
3788 * of a new jump until either the source chain is reached (loop) or all
3789 * reachable chains have been traversed.
3791 * The loop check is performed whenever a new jump verdict is added to an
3792 * expression or verdict map or a verdict map is bound to a new chain.
3795 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3796 const struct nft_chain *chain);
3798 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3799 const struct nft_set *set,
3800 const struct nft_set_iter *iter,
3801 const struct nft_set_elem *elem)
3803 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3806 switch (elem->data.verdict) {
3809 return nf_tables_check_loops(ctx, elem->data.chain);
3815 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3816 const struct nft_chain *chain)
3818 const struct nft_rule *rule;
3819 const struct nft_expr *expr, *last;
3820 const struct nft_set *set;
3821 struct nft_set_binding *binding;
3822 struct nft_set_iter iter;
3824 if (ctx->chain == chain)
3827 list_for_each_entry(rule, &chain->rules, list) {
3828 nft_rule_for_each_expr(expr, last, rule) {
3829 const struct nft_data *data = NULL;
3832 if (!expr->ops->validate)
3835 err = expr->ops->validate(ctx, expr, &data);
3842 switch (data->verdict) {
3845 err = nf_tables_check_loops(ctx, data->chain);
3854 list_for_each_entry(set, &ctx->table->sets, list) {
3855 if (!(set->flags & NFT_SET_MAP) ||
3856 set->dtype != NFT_DATA_VERDICT)
3859 list_for_each_entry(binding, &set->bindings, list) {
3860 if (binding->chain != chain)
3866 iter.fn = nf_tables_loop_check_setelem;
3868 set->ops->walk(ctx, set, &iter);
3878 * nft_validate_input_register - validate an expressions' input register
3880 * @reg: the register number
3882 * Validate that the input register is one of the general purpose
3885 int nft_validate_input_register(enum nft_registers reg)
3887 if (reg <= NFT_REG_VERDICT)
3889 if (reg > NFT_REG_MAX)
3893 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3896 * nft_validate_output_register - validate an expressions' output register
3898 * @reg: the register number
3900 * Validate that the output register is one of the general purpose
3901 * registers or the verdict register.
3903 int nft_validate_output_register(enum nft_registers reg)
3905 if (reg < NFT_REG_VERDICT)
3907 if (reg > NFT_REG_MAX)
3911 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3914 * nft_validate_data_load - validate an expressions' data load
3916 * @ctx: context of the expression performing the load
3917 * @reg: the destination register number
3918 * @data: the data to load
3919 * @type: the data type
3921 * Validate that a data load uses the appropriate data type for
3922 * the destination register. A value of NULL for the data means
3923 * that its runtime gathered data, which is always of type
3926 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3927 const struct nft_data *data,
3928 enum nft_data_types type)
3933 case NFT_REG_VERDICT:
3934 if (data == NULL || type != NFT_DATA_VERDICT)
3937 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3938 err = nf_tables_check_loops(ctx, data->chain);
3942 if (ctx->chain->level + 1 > data->chain->level) {
3943 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3945 data->chain->level = ctx->chain->level + 1;
3951 if (data != NULL && type != NFT_DATA_VALUE)
3956 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3958 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3959 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3960 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3961 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3964 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3965 struct nft_data_desc *desc, const struct nlattr *nla)
3967 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3968 struct nft_chain *chain;
3971 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3975 if (!tb[NFTA_VERDICT_CODE])
3977 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3979 switch (data->verdict) {
3981 switch (data->verdict & NF_VERDICT_MASK) {
3993 desc->len = sizeof(data->verdict);
3997 if (!tb[NFTA_VERDICT_CHAIN])
3999 chain = nf_tables_chain_lookup(ctx->table,
4000 tb[NFTA_VERDICT_CHAIN]);
4002 return PTR_ERR(chain);
4003 if (chain->flags & NFT_BASE_CHAIN)
4007 data->chain = chain;
4008 desc->len = sizeof(data);
4012 desc->type = NFT_DATA_VERDICT;
4016 static void nft_verdict_uninit(const struct nft_data *data)
4018 switch (data->verdict) {
4026 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4028 struct nlattr *nest;
4030 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4032 goto nla_put_failure;
4034 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
4035 goto nla_put_failure;
4037 switch (data->verdict) {
4040 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
4041 goto nla_put_failure;
4043 nla_nest_end(skb, nest);
4050 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4051 struct nft_data_desc *desc, const struct nlattr *nla)
4058 if (len > sizeof(data->data))
4061 nla_memcpy(data->data, nla, sizeof(data->data));
4062 desc->type = NFT_DATA_VALUE;
4067 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4070 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4073 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4074 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4075 .len = FIELD_SIZEOF(struct nft_data, data) },
4076 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4080 * nft_data_init - parse nf_tables data netlink attributes
4082 * @ctx: context of the expression using the data
4083 * @data: destination struct nft_data
4084 * @desc: data description
4085 * @nla: netlink attribute containing data
4087 * Parse the netlink data attributes and initialize a struct nft_data.
4088 * The type and length of data are returned in the data description.
4090 * The caller can indicate that it only wants to accept data of type
4091 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4093 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4094 struct nft_data_desc *desc, const struct nlattr *nla)
4096 struct nlattr *tb[NFTA_DATA_MAX + 1];
4099 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4103 if (tb[NFTA_DATA_VALUE])
4104 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4105 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4106 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4109 EXPORT_SYMBOL_GPL(nft_data_init);
4112 * nft_data_uninit - release a nft_data item
4114 * @data: struct nft_data to release
4115 * @type: type of data
4117 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4118 * all others need to be released by calling this function.
4120 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4123 case NFT_DATA_VALUE:
4125 case NFT_DATA_VERDICT:
4126 return nft_verdict_uninit(data);
4131 EXPORT_SYMBOL_GPL(nft_data_uninit);
4133 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4134 enum nft_data_types type, unsigned int len)
4136 struct nlattr *nest;
4139 nest = nla_nest_start(skb, attr);
4144 case NFT_DATA_VALUE:
4145 err = nft_value_dump(skb, data, len);
4147 case NFT_DATA_VERDICT:
4148 err = nft_verdict_dump(skb, data);
4155 nla_nest_end(skb, nest);
4158 EXPORT_SYMBOL_GPL(nft_data_dump);
4160 static int nf_tables_init_net(struct net *net)
4162 INIT_LIST_HEAD(&net->nft.af_info);
4163 INIT_LIST_HEAD(&net->nft.commit_list);
4164 net->nft.base_seq = 1;
4168 static struct pernet_operations nf_tables_net_ops = {
4169 .init = nf_tables_init_net,
4172 static int __init nf_tables_module_init(void)
4176 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4183 err = nf_tables_core_module_init();
4187 err = nfnetlink_subsys_register(&nf_tables_subsys);
4191 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4192 return register_pernet_subsys(&nf_tables_net_ops);
4194 nf_tables_core_module_exit();
4201 static void __exit nf_tables_module_exit(void)
4203 unregister_pernet_subsys(&nf_tables_net_ops);
4204 nfnetlink_subsys_unregister(&nf_tables_subsys);
4206 nf_tables_core_module_exit();
4210 module_init(nf_tables_module_init);
4211 module_exit(nf_tables_module_exit);
4213 MODULE_LICENSE("GPL");
4214 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4215 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);