2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
24 static LIST_HEAD(nf_tables_expressions);
27 * nft_register_afinfo - register nf_tables address family info
29 * @afi: address family info to register
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
34 int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail(&afi->list, &net->nft.af_info);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
45 * nft_unregister_afinfo - unregister nf_tables address family info
47 * @afi: address family info to unregister
49 * Unregister the address family for use with nf_tables.
51 void nft_unregister_afinfo(struct nft_af_info *afi)
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
59 static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
61 struct nft_af_info *afi;
63 list_for_each_entry(afi, &net->nft.af_info, list) {
64 if (afi->family == family)
70 static struct nft_af_info *
71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
73 struct nft_af_info *afi;
75 afi = nft_afinfo_lookup(net, family);
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
83 afi = nft_afinfo_lookup(net, family);
85 return ERR_PTR(-EAGAIN);
88 return ERR_PTR(-EAFNOSUPPORT);
95 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96 const struct nlattr *nla)
98 struct nft_table *table;
100 list_for_each_entry(table, &afi->tables, list) {
101 if (!nla_strcmp(nla, table->name))
107 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
108 const struct nlattr *nla)
110 struct nft_table *table;
113 return ERR_PTR(-EINVAL);
115 table = nft_table_lookup(afi, nla);
119 return ERR_PTR(-ENOENT);
122 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
124 return ++table->hgenerator;
127 static struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
129 static int __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
133 for (i=0; i<NFT_CHAIN_T_MAX; i++) {
134 if (chain_type[family][i] != NULL &&
135 !nla_strcmp(nla, chain_type[family][i]->name))
141 static int nf_tables_chain_type_lookup(const struct nft_af_info *afi,
142 const struct nlattr *nla,
147 type = __nf_tables_chain_type_lookup(afi->family, nla);
148 #ifdef CONFIG_MODULES
149 if (type < 0 && autoload) {
150 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
151 request_module("nft-chain-%u-%*.s", afi->family,
152 nla_len(nla)-1, (const char *)nla_data(nla));
153 nfnl_lock(NFNL_SUBSYS_NFTABLES);
154 type = __nf_tables_chain_type_lookup(afi->family, nla);
160 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
161 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
162 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
165 static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
166 int event, u32 flags, int family,
167 const struct nft_table *table)
169 struct nlmsghdr *nlh;
170 struct nfgenmsg *nfmsg;
172 event |= NFNL_SUBSYS_NFTABLES << 8;
173 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
175 goto nla_put_failure;
177 nfmsg = nlmsg_data(nlh);
178 nfmsg->nfgen_family = family;
179 nfmsg->version = NFNETLINK_V0;
182 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
183 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
184 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
185 goto nla_put_failure;
187 return nlmsg_end(skb, nlh);
190 nlmsg_trim(skb, nlh);
194 static int nf_tables_table_notify(const struct sk_buff *oskb,
195 const struct nlmsghdr *nlh,
196 const struct nft_table *table,
197 int event, int family)
200 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
201 u32 seq = nlh ? nlh->nlmsg_seq : 0;
202 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
206 report = nlh ? nlmsg_report(nlh) : false;
207 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
211 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
215 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
222 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
226 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
230 static int nf_tables_dump_tables(struct sk_buff *skb,
231 struct netlink_callback *cb)
233 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
234 const struct nft_af_info *afi;
235 const struct nft_table *table;
236 unsigned int idx = 0, s_idx = cb->args[0];
237 struct net *net = sock_net(skb->sk);
238 int family = nfmsg->nfgen_family;
240 list_for_each_entry(afi, &net->nft.af_info, list) {
241 if (family != NFPROTO_UNSPEC && family != afi->family)
244 list_for_each_entry(table, &afi->tables, list) {
248 memset(&cb->args[1], 0,
249 sizeof(cb->args) - sizeof(cb->args[0]));
250 if (nf_tables_fill_table_info(skb,
251 NETLINK_CB(cb->skb).portid,
255 afi->family, table) < 0)
266 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
267 const struct nlmsghdr *nlh,
268 const struct nlattr * const nla[])
270 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
271 const struct nft_af_info *afi;
272 const struct nft_table *table;
273 struct sk_buff *skb2;
274 struct net *net = sock_net(skb->sk);
275 int family = nfmsg->nfgen_family;
278 if (nlh->nlmsg_flags & NLM_F_DUMP) {
279 struct netlink_dump_control c = {
280 .dump = nf_tables_dump_tables,
282 return netlink_dump_start(nlsk, skb, nlh, &c);
285 afi = nf_tables_afinfo_lookup(net, family, false);
289 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
291 return PTR_ERR(table);
293 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
297 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
298 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
303 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
310 static int nf_tables_table_enable(struct nft_table *table)
312 struct nft_chain *chain;
315 list_for_each_entry(chain, &table->chains, list) {
316 err = nf_register_hook(&nft_base_chain(chain)->ops);
324 list_for_each_entry(chain, &table->chains, list) {
328 nf_unregister_hook(&nft_base_chain(chain)->ops);
333 static int nf_tables_table_disable(struct nft_table *table)
335 struct nft_chain *chain;
337 list_for_each_entry(chain, &table->chains, list)
338 nf_unregister_hook(&nft_base_chain(chain)->ops);
343 static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
344 const struct nlmsghdr *nlh,
345 const struct nlattr * const nla[],
346 struct nft_af_info *afi, struct nft_table *table)
348 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
349 int family = nfmsg->nfgen_family, ret = 0;
351 if (nla[NFTA_TABLE_FLAGS]) {
354 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
355 if (flags & ~NFT_TABLE_F_DORMANT)
358 if ((flags & NFT_TABLE_F_DORMANT) &&
359 !(table->flags & NFT_TABLE_F_DORMANT)) {
360 ret = nf_tables_table_disable(table);
362 table->flags |= NFT_TABLE_F_DORMANT;
363 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
364 table->flags & NFT_TABLE_F_DORMANT) {
365 ret = nf_tables_table_enable(table);
367 table->flags &= ~NFT_TABLE_F_DORMANT;
373 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
378 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
379 const struct nlmsghdr *nlh,
380 const struct nlattr * const nla[])
382 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
383 const struct nlattr *name;
384 struct nft_af_info *afi;
385 struct nft_table *table;
386 struct net *net = sock_net(skb->sk);
387 int family = nfmsg->nfgen_family;
389 afi = nf_tables_afinfo_lookup(net, family, true);
393 name = nla[NFTA_TABLE_NAME];
394 table = nf_tables_table_lookup(afi, name);
396 if (PTR_ERR(table) != -ENOENT)
397 return PTR_ERR(table);
402 if (nlh->nlmsg_flags & NLM_F_EXCL)
404 if (nlh->nlmsg_flags & NLM_F_REPLACE)
406 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
409 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
413 nla_strlcpy(table->name, name, nla_len(name));
414 INIT_LIST_HEAD(&table->chains);
415 INIT_LIST_HEAD(&table->sets);
417 if (nla[NFTA_TABLE_FLAGS]) {
420 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
421 if (flags & ~NFT_TABLE_F_DORMANT) {
426 table->flags |= flags;
429 list_add_tail(&table->list, &afi->tables);
430 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
434 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
435 const struct nlmsghdr *nlh,
436 const struct nlattr * const nla[])
438 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
439 struct nft_af_info *afi;
440 struct nft_table *table;
441 struct net *net = sock_net(skb->sk);
442 int family = nfmsg->nfgen_family;
444 afi = nf_tables_afinfo_lookup(net, family, false);
448 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
450 return PTR_ERR(table);
455 list_del(&table->list);
456 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
461 int nft_register_chain_type(struct nf_chain_type *ctype)
465 nfnl_lock(NFNL_SUBSYS_NFTABLES);
466 if (chain_type[ctype->family][ctype->type] != NULL) {
471 if (!try_module_get(ctype->me))
474 chain_type[ctype->family][ctype->type] = ctype;
476 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
479 EXPORT_SYMBOL_GPL(nft_register_chain_type);
481 void nft_unregister_chain_type(struct nf_chain_type *ctype)
483 nfnl_lock(NFNL_SUBSYS_NFTABLES);
484 chain_type[ctype->family][ctype->type] = NULL;
485 module_put(ctype->me);
486 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
488 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
494 static struct nft_chain *
495 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
497 struct nft_chain *chain;
499 list_for_each_entry(chain, &table->chains, list) {
500 if (chain->handle == handle)
504 return ERR_PTR(-ENOENT);
507 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
508 const struct nlattr *nla)
510 struct nft_chain *chain;
513 return ERR_PTR(-EINVAL);
515 list_for_each_entry(chain, &table->chains, list) {
516 if (!nla_strcmp(nla, chain->name))
520 return ERR_PTR(-ENOENT);
523 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
524 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
525 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
526 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
527 .len = NFT_CHAIN_MAXNAMELEN - 1 },
528 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
529 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
530 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
531 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
534 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
535 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
536 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
539 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
541 struct nft_stats *cpu_stats, total;
545 memset(&total, 0, sizeof(total));
546 for_each_possible_cpu(cpu) {
547 cpu_stats = per_cpu_ptr(stats, cpu);
548 total.pkts += cpu_stats->pkts;
549 total.bytes += cpu_stats->bytes;
551 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
553 goto nla_put_failure;
555 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
556 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
557 goto nla_put_failure;
559 nla_nest_end(skb, nest);
566 static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
567 int event, u32 flags, int family,
568 const struct nft_table *table,
569 const struct nft_chain *chain)
571 struct nlmsghdr *nlh;
572 struct nfgenmsg *nfmsg;
574 event |= NFNL_SUBSYS_NFTABLES << 8;
575 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
577 goto nla_put_failure;
579 nfmsg = nlmsg_data(nlh);
580 nfmsg->nfgen_family = family;
581 nfmsg->version = NFNETLINK_V0;
584 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
585 goto nla_put_failure;
586 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
587 goto nla_put_failure;
588 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
589 goto nla_put_failure;
591 if (chain->flags & NFT_BASE_CHAIN) {
592 const struct nft_base_chain *basechain = nft_base_chain(chain);
593 const struct nf_hook_ops *ops = &basechain->ops;
596 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
598 goto nla_put_failure;
599 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
600 goto nla_put_failure;
601 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
602 goto nla_put_failure;
603 nla_nest_end(skb, nest);
605 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
606 htonl(basechain->policy)))
607 goto nla_put_failure;
609 if (nla_put_string(skb, NFTA_CHAIN_TYPE,
610 chain_type[ops->pf][nft_base_chain(chain)->type]->name))
611 goto nla_put_failure;
613 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
614 goto nla_put_failure;
617 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
618 goto nla_put_failure;
620 return nlmsg_end(skb, nlh);
623 nlmsg_trim(skb, nlh);
627 static int nf_tables_chain_notify(const struct sk_buff *oskb,
628 const struct nlmsghdr *nlh,
629 const struct nft_table *table,
630 const struct nft_chain *chain,
631 int event, int family)
634 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
635 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
636 u32 seq = nlh ? nlh->nlmsg_seq : 0;
640 report = nlh ? nlmsg_report(nlh) : false;
641 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
645 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
649 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
656 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
660 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
664 static int nf_tables_dump_chains(struct sk_buff *skb,
665 struct netlink_callback *cb)
667 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
668 const struct nft_af_info *afi;
669 const struct nft_table *table;
670 const struct nft_chain *chain;
671 unsigned int idx = 0, s_idx = cb->args[0];
672 struct net *net = sock_net(skb->sk);
673 int family = nfmsg->nfgen_family;
675 list_for_each_entry(afi, &net->nft.af_info, list) {
676 if (family != NFPROTO_UNSPEC && family != afi->family)
679 list_for_each_entry(table, &afi->tables, list) {
680 list_for_each_entry(chain, &table->chains, list) {
684 memset(&cb->args[1], 0,
685 sizeof(cb->args) - sizeof(cb->args[0]));
686 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
690 afi->family, table, chain) < 0)
703 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
704 const struct nlmsghdr *nlh,
705 const struct nlattr * const nla[])
707 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
708 const struct nft_af_info *afi;
709 const struct nft_table *table;
710 const struct nft_chain *chain;
711 struct sk_buff *skb2;
712 struct net *net = sock_net(skb->sk);
713 int family = nfmsg->nfgen_family;
716 if (nlh->nlmsg_flags & NLM_F_DUMP) {
717 struct netlink_dump_control c = {
718 .dump = nf_tables_dump_chains,
720 return netlink_dump_start(nlsk, skb, nlh, &c);
723 afi = nf_tables_afinfo_lookup(net, family, false);
727 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
729 return PTR_ERR(table);
731 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
733 return PTR_ERR(chain);
735 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
739 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
740 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
741 family, table, chain);
745 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
753 nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
755 switch (ntohl(nla_get_be32(attr))) {
757 chain->policy = NF_DROP;
760 chain->policy = NF_ACCEPT;
768 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
769 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
770 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
774 nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
776 struct nlattr *tb[NFTA_COUNTER_MAX+1];
777 struct nft_stats __percpu *newstats;
778 struct nft_stats *stats;
781 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
785 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
788 newstats = alloc_percpu(struct nft_stats);
789 if (newstats == NULL)
792 /* Restore old counters on this cpu, no problem. Per-cpu statistics
793 * are not exposed to userspace.
795 stats = this_cpu_ptr(newstats);
796 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
797 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
800 /* nfnl_lock is held, add some nfnl function for this, later */
801 struct nft_stats __percpu *oldstats =
802 rcu_dereference_protected(chain->stats, 1);
804 rcu_assign_pointer(chain->stats, newstats);
806 free_percpu(oldstats);
808 rcu_assign_pointer(chain->stats, newstats);
813 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
814 const struct nlmsghdr *nlh,
815 const struct nlattr * const nla[])
817 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
818 const struct nlattr * uninitialized_var(name);
819 const struct nft_af_info *afi;
820 struct nft_table *table;
821 struct nft_chain *chain;
822 struct nft_base_chain *basechain = NULL;
823 struct nlattr *ha[NFTA_HOOK_MAX + 1];
824 struct net *net = sock_net(skb->sk);
825 int family = nfmsg->nfgen_family;
830 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
832 afi = nf_tables_afinfo_lookup(net, family, true);
836 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
838 return PTR_ERR(table);
840 if (table->use == UINT_MAX)
844 name = nla[NFTA_CHAIN_NAME];
846 if (nla[NFTA_CHAIN_HANDLE]) {
847 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
848 chain = nf_tables_chain_lookup_byhandle(table, handle);
850 return PTR_ERR(chain);
852 chain = nf_tables_chain_lookup(table, name);
854 if (PTR_ERR(chain) != -ENOENT)
855 return PTR_ERR(chain);
861 if (nlh->nlmsg_flags & NLM_F_EXCL)
863 if (nlh->nlmsg_flags & NLM_F_REPLACE)
866 if (nla[NFTA_CHAIN_HANDLE] && name &&
867 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
870 if (nla[NFTA_CHAIN_POLICY]) {
871 if (!(chain->flags & NFT_BASE_CHAIN))
874 err = nf_tables_chain_policy(nft_base_chain(chain),
875 nla[NFTA_CHAIN_POLICY]);
880 if (nla[NFTA_CHAIN_COUNTERS]) {
881 if (!(chain->flags & NFT_BASE_CHAIN))
884 err = nf_tables_counters(nft_base_chain(chain),
885 nla[NFTA_CHAIN_COUNTERS]);
890 if (nla[NFTA_CHAIN_HANDLE] && name)
891 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
896 if (nla[NFTA_CHAIN_HOOK]) {
897 struct nf_hook_ops *ops;
900 int type = NFT_CHAIN_T_DEFAULT;
902 if (nla[NFTA_CHAIN_TYPE]) {
903 type = nf_tables_chain_type_lookup(afi,
904 nla[NFTA_CHAIN_TYPE],
910 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
914 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
915 ha[NFTA_HOOK_PRIORITY] == NULL)
918 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
919 if (hooknum >= afi->nhooks)
922 hookfn = chain_type[family][type]->fn[hooknum];
926 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
927 if (basechain == NULL)
930 basechain->type = type;
931 chain = &basechain->chain;
933 ops = &basechain->ops;
935 ops->owner = afi->owner;
936 ops->hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
937 ops->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
940 if (afi->hooks[ops->hooknum])
941 ops->hook = afi->hooks[ops->hooknum];
943 chain->flags |= NFT_BASE_CHAIN;
945 if (nla[NFTA_CHAIN_POLICY]) {
946 err = nf_tables_chain_policy(basechain,
947 nla[NFTA_CHAIN_POLICY]);
949 free_percpu(basechain->stats);
954 basechain->policy = NF_ACCEPT;
956 if (nla[NFTA_CHAIN_COUNTERS]) {
957 err = nf_tables_counters(basechain,
958 nla[NFTA_CHAIN_COUNTERS]);
960 free_percpu(basechain->stats);
965 struct nft_stats __percpu *newstats;
967 newstats = alloc_percpu(struct nft_stats);
968 if (newstats == NULL)
971 rcu_assign_pointer(nft_base_chain(chain)->stats,
975 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
980 INIT_LIST_HEAD(&chain->rules);
981 chain->handle = nf_tables_alloc_handle(table);
983 chain->table = table;
984 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
986 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
987 chain->flags & NFT_BASE_CHAIN) {
988 err = nf_register_hook(&nft_base_chain(chain)->ops);
990 free_percpu(basechain->stats);
995 list_add_tail(&chain->list, &table->chains);
998 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1003 static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1005 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1007 BUG_ON(chain->use > 0);
1009 if (chain->flags & NFT_BASE_CHAIN) {
1010 free_percpu(nft_base_chain(chain)->stats);
1011 kfree(nft_base_chain(chain));
1016 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1017 const struct nlmsghdr *nlh,
1018 const struct nlattr * const nla[])
1020 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1021 const struct nft_af_info *afi;
1022 struct nft_table *table;
1023 struct nft_chain *chain;
1024 struct net *net = sock_net(skb->sk);
1025 int family = nfmsg->nfgen_family;
1027 afi = nf_tables_afinfo_lookup(net, family, false);
1029 return PTR_ERR(afi);
1031 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1033 return PTR_ERR(table);
1035 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1037 return PTR_ERR(chain);
1039 if (!list_empty(&chain->rules))
1042 list_del(&chain->list);
1045 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1046 chain->flags & NFT_BASE_CHAIN)
1047 nf_unregister_hook(&nft_base_chain(chain)->ops);
1049 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1052 /* Make sure all rule references are gone before this is released */
1053 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1057 static void nft_ctx_init(struct nft_ctx *ctx,
1058 const struct sk_buff *skb,
1059 const struct nlmsghdr *nlh,
1060 const struct nft_af_info *afi,
1061 const struct nft_table *table,
1062 const struct nft_chain *chain,
1063 const struct nlattr * const *nla)
1065 ctx->net = sock_net(skb->sk);
1079 * nft_register_expr - register nf_tables expr type
1082 * Registers the expr type for use with nf_tables. Returns zero on
1083 * success or a negative errno code otherwise.
1085 int nft_register_expr(struct nft_expr_type *type)
1087 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1088 list_add_tail(&type->list, &nf_tables_expressions);
1089 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1092 EXPORT_SYMBOL_GPL(nft_register_expr);
1095 * nft_unregister_expr - unregister nf_tables expr type
1098 * Unregisters the expr typefor use with nf_tables.
1100 void nft_unregister_expr(struct nft_expr_type *type)
1102 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1103 list_del(&type->list);
1104 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1106 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1108 static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
1110 const struct nft_expr_type *type;
1112 list_for_each_entry(type, &nf_tables_expressions, list) {
1113 if (!nla_strcmp(nla, type->name))
1119 static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
1121 const struct nft_expr_type *type;
1124 return ERR_PTR(-EINVAL);
1126 type = __nft_expr_type_get(nla);
1127 if (type != NULL && try_module_get(type->owner))
1130 #ifdef CONFIG_MODULES
1132 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1133 request_module("nft-expr-%.*s",
1134 nla_len(nla), (char *)nla_data(nla));
1135 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1136 if (__nft_expr_type_get(nla))
1137 return ERR_PTR(-EAGAIN);
1140 return ERR_PTR(-ENOENT);
1143 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1144 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1145 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1148 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1149 const struct nft_expr *expr)
1151 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1152 goto nla_put_failure;
1154 if (expr->ops->dump) {
1155 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1157 goto nla_put_failure;
1158 if (expr->ops->dump(skb, expr) < 0)
1159 goto nla_put_failure;
1160 nla_nest_end(skb, data);
1169 struct nft_expr_info {
1170 const struct nft_expr_ops *ops;
1171 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1174 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1175 const struct nlattr *nla,
1176 struct nft_expr_info *info)
1178 const struct nft_expr_type *type;
1179 const struct nft_expr_ops *ops;
1180 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1183 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1187 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1189 return PTR_ERR(type);
1191 if (tb[NFTA_EXPR_DATA]) {
1192 err = nla_parse_nested(info->tb, type->maxattr,
1193 tb[NFTA_EXPR_DATA], type->policy);
1197 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1199 if (type->select_ops != NULL) {
1200 ops = type->select_ops(ctx,
1201 (const struct nlattr * const *)info->tb);
1213 module_put(type->owner);
1217 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1218 const struct nft_expr_info *info,
1219 struct nft_expr *expr)
1221 const struct nft_expr_ops *ops = info->ops;
1226 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1238 static void nf_tables_expr_destroy(struct nft_expr *expr)
1240 if (expr->ops->destroy)
1241 expr->ops->destroy(expr);
1242 module_put(expr->ops->type->owner);
1249 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1252 struct nft_rule *rule;
1254 // FIXME: this sucks
1255 list_for_each_entry(rule, &chain->rules, list) {
1256 if (handle == rule->handle)
1260 return ERR_PTR(-ENOENT);
1263 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1264 const struct nlattr *nla)
1267 return ERR_PTR(-EINVAL);
1269 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1272 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1273 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1274 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1275 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1276 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1277 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1278 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1279 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1282 static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1283 int event, u32 flags, int family,
1284 const struct nft_table *table,
1285 const struct nft_chain *chain,
1286 const struct nft_rule *rule)
1288 struct nlmsghdr *nlh;
1289 struct nfgenmsg *nfmsg;
1290 const struct nft_expr *expr, *next;
1291 struct nlattr *list;
1292 const struct nft_rule *prule;
1293 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1295 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1298 goto nla_put_failure;
1300 nfmsg = nlmsg_data(nlh);
1301 nfmsg->nfgen_family = family;
1302 nfmsg->version = NFNETLINK_V0;
1305 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1306 goto nla_put_failure;
1307 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1308 goto nla_put_failure;
1309 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1310 goto nla_put_failure;
1312 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1313 prule = list_entry(rule->list.prev, struct nft_rule, list);
1314 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1315 cpu_to_be64(prule->handle)))
1316 goto nla_put_failure;
1319 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1321 goto nla_put_failure;
1322 nft_rule_for_each_expr(expr, next, rule) {
1323 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1325 goto nla_put_failure;
1326 if (nf_tables_fill_expr_info(skb, expr) < 0)
1327 goto nla_put_failure;
1328 nla_nest_end(skb, elem);
1330 nla_nest_end(skb, list);
1332 return nlmsg_end(skb, nlh);
1335 nlmsg_trim(skb, nlh);
1339 static int nf_tables_rule_notify(const struct sk_buff *oskb,
1340 const struct nlmsghdr *nlh,
1341 const struct nft_table *table,
1342 const struct nft_chain *chain,
1343 const struct nft_rule *rule,
1344 int event, u32 flags, int family)
1346 struct sk_buff *skb;
1347 u32 portid = NETLINK_CB(oskb).portid;
1348 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1349 u32 seq = nlh->nlmsg_seq;
1353 report = nlmsg_report(nlh);
1354 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1358 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1362 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1363 family, table, chain, rule);
1369 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1373 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1378 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1380 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1383 static inline int gencursor_next(struct net *net)
1385 return net->nft.gencursor+1 == 1 ? 1 : 0;
1389 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1391 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1395 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1397 /* Now inactive, will be active in the future */
1398 rule->genmask = (1 << net->nft.gencursor);
1402 nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1404 rule->genmask = (1 << gencursor_next(net));
1407 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1412 static int nf_tables_dump_rules(struct sk_buff *skb,
1413 struct netlink_callback *cb)
1415 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1416 const struct nft_af_info *afi;
1417 const struct nft_table *table;
1418 const struct nft_chain *chain;
1419 const struct nft_rule *rule;
1420 unsigned int idx = 0, s_idx = cb->args[0];
1421 struct net *net = sock_net(skb->sk);
1422 int family = nfmsg->nfgen_family;
1423 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1424 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1426 list_for_each_entry(afi, &net->nft.af_info, list) {
1427 if (family != NFPROTO_UNSPEC && family != afi->family)
1430 list_for_each_entry(table, &afi->tables, list) {
1431 list_for_each_entry(chain, &table->chains, list) {
1432 list_for_each_entry(rule, &chain->rules, list) {
1433 if (!nft_rule_is_active(net, rule))
1438 memset(&cb->args[1], 0,
1439 sizeof(cb->args) - sizeof(cb->args[0]));
1440 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1443 NLM_F_MULTI | NLM_F_APPEND,
1444 afi->family, table, chain, rule) < 0)
1453 /* Invalidate this dump, a transition to the new generation happened */
1454 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1461 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1462 const struct nlmsghdr *nlh,
1463 const struct nlattr * const nla[])
1465 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1466 const struct nft_af_info *afi;
1467 const struct nft_table *table;
1468 const struct nft_chain *chain;
1469 const struct nft_rule *rule;
1470 struct sk_buff *skb2;
1471 struct net *net = sock_net(skb->sk);
1472 int family = nfmsg->nfgen_family;
1475 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1476 struct netlink_dump_control c = {
1477 .dump = nf_tables_dump_rules,
1479 return netlink_dump_start(nlsk, skb, nlh, &c);
1482 afi = nf_tables_afinfo_lookup(net, family, false);
1484 return PTR_ERR(afi);
1486 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1488 return PTR_ERR(table);
1490 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1492 return PTR_ERR(chain);
1494 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1496 return PTR_ERR(rule);
1498 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1502 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1503 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1504 family, table, chain, rule);
1508 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1515 static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1517 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1518 struct nft_expr *expr;
1521 * Careful: some expressions might not be initialized in case this
1522 * is called on error from nf_tables_newrule().
1524 expr = nft_expr_first(rule);
1525 while (expr->ops && expr != nft_expr_last(rule)) {
1526 nf_tables_expr_destroy(expr);
1527 expr = nft_expr_next(expr);
1532 static void nf_tables_rule_destroy(struct nft_rule *rule)
1534 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1537 #define NFT_RULE_MAXEXPRS 128
1539 static struct nft_expr_info *info;
1541 static struct nft_rule_trans *
1542 nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1544 struct nft_rule_trans *rupd;
1546 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1550 rupd->chain = ctx->chain;
1551 rupd->table = ctx->table;
1553 rupd->family = ctx->afi->family;
1554 rupd->nlh = ctx->nlh;
1555 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1560 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1561 const struct nlmsghdr *nlh,
1562 const struct nlattr * const nla[])
1564 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1565 const struct nft_af_info *afi;
1566 struct net *net = sock_net(skb->sk);
1567 struct nft_table *table;
1568 struct nft_chain *chain;
1569 struct nft_rule *rule, *old_rule = NULL;
1570 struct nft_rule_trans *repl = NULL;
1571 struct nft_expr *expr;
1574 unsigned int size, i, n;
1577 u64 handle, pos_handle;
1579 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1581 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1583 return PTR_ERR(afi);
1585 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1587 return PTR_ERR(table);
1589 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1591 return PTR_ERR(chain);
1593 if (nla[NFTA_RULE_HANDLE]) {
1594 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1595 rule = __nf_tables_rule_lookup(chain, handle);
1597 return PTR_ERR(rule);
1599 if (nlh->nlmsg_flags & NLM_F_EXCL)
1601 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1606 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1608 handle = nf_tables_alloc_handle(table);
1611 if (nla[NFTA_RULE_POSITION]) {
1612 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1615 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1616 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1617 if (IS_ERR(old_rule))
1618 return PTR_ERR(old_rule);
1621 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1625 if (nla[NFTA_RULE_EXPRESSIONS]) {
1626 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1628 if (nla_type(tmp) != NFTA_LIST_ELEM)
1630 if (n == NFT_RULE_MAXEXPRS)
1632 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1635 size += info[n].ops->size;
1641 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1645 nft_rule_activate_next(net, rule);
1647 rule->handle = handle;
1650 expr = nft_expr_first(rule);
1651 for (i = 0; i < n; i++) {
1652 err = nf_tables_newexpr(&ctx, &info[i], expr);
1656 expr = nft_expr_next(expr);
1659 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1660 if (nft_rule_is_active_next(net, old_rule)) {
1661 repl = nf_tables_trans_add(old_rule, &ctx);
1666 nft_rule_disactivate_next(net, old_rule);
1667 list_add_tail(&rule->list, &old_rule->list);
1672 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1674 list_add_rcu(&rule->list, &old_rule->list);
1676 list_add_tail_rcu(&rule->list, &chain->rules);
1679 list_add_tail_rcu(&rule->list, &old_rule->list);
1681 list_add_rcu(&rule->list, &chain->rules);
1684 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1691 list_del_rcu(&rule->list);
1693 list_del_rcu(&repl->rule->list);
1694 list_del(&repl->list);
1695 nft_rule_clear(net, repl->rule);
1699 nf_tables_rule_destroy(rule);
1701 for (i = 0; i < n; i++) {
1702 if (info[i].ops != NULL)
1703 module_put(info[i].ops->type->owner);
1709 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1711 /* You cannot delete the same rule twice */
1712 if (nft_rule_is_active_next(ctx->net, rule)) {
1713 if (nf_tables_trans_add(rule, ctx) == NULL)
1715 nft_rule_disactivate_next(ctx->net, rule);
1721 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1722 const struct nlmsghdr *nlh,
1723 const struct nlattr * const nla[])
1725 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1726 const struct nft_af_info *afi;
1727 struct net *net = sock_net(skb->sk);
1728 const struct nft_table *table;
1729 struct nft_chain *chain;
1730 struct nft_rule *rule, *tmp;
1731 int family = nfmsg->nfgen_family, err = 0;
1734 afi = nf_tables_afinfo_lookup(net, family, false);
1736 return PTR_ERR(afi);
1738 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1740 return PTR_ERR(table);
1742 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1744 return PTR_ERR(chain);
1746 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1748 if (nla[NFTA_RULE_HANDLE]) {
1749 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1751 return PTR_ERR(rule);
1753 err = nf_tables_delrule_one(&ctx, rule);
1755 /* Remove all rules in this chain */
1756 list_for_each_entry_safe(rule, tmp, &chain->rules, list) {
1757 err = nf_tables_delrule_one(&ctx, rule);
1766 static int nf_tables_commit(struct sk_buff *skb)
1768 struct net *net = sock_net(skb->sk);
1769 struct nft_rule_trans *rupd, *tmp;
1771 /* Bump generation counter, invalidate any dump in progress */
1774 /* A new generation has just started */
1775 net->nft.gencursor = gencursor_next(net);
1777 /* Make sure all packets have left the previous generation before
1778 * purging old rules.
1782 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1783 /* Delete this rule from the dirty list */
1784 list_del(&rupd->list);
1786 /* This rule was inactive in the past and just became active.
1787 * Clear the next bit of the genmask since its meaning has
1788 * changed, now it is the future.
1790 if (nft_rule_is_active(net, rupd->rule)) {
1791 nft_rule_clear(net, rupd->rule);
1792 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1793 rupd->chain, rupd->rule,
1800 /* This rule is in the past, get rid of it */
1801 list_del_rcu(&rupd->rule->list);
1802 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1803 rupd->rule, NFT_MSG_DELRULE, 0,
1805 nf_tables_rule_destroy(rupd->rule);
1812 static int nf_tables_abort(struct sk_buff *skb)
1814 struct net *net = sock_net(skb->sk);
1815 struct nft_rule_trans *rupd, *tmp;
1817 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1818 /* Delete all rules from the dirty list */
1819 list_del(&rupd->list);
1821 if (!nft_rule_is_active_next(net, rupd->rule)) {
1822 nft_rule_clear(net, rupd->rule);
1827 /* This rule is inactive, get rid of it */
1828 list_del_rcu(&rupd->rule->list);
1829 nf_tables_rule_destroy(rupd->rule);
1839 static LIST_HEAD(nf_tables_set_ops);
1841 int nft_register_set(struct nft_set_ops *ops)
1843 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1844 list_add_tail(&ops->list, &nf_tables_set_ops);
1845 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1848 EXPORT_SYMBOL_GPL(nft_register_set);
1850 void nft_unregister_set(struct nft_set_ops *ops)
1852 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1853 list_del(&ops->list);
1854 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1856 EXPORT_SYMBOL_GPL(nft_unregister_set);
1858 static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1860 const struct nft_set_ops *ops;
1863 #ifdef CONFIG_MODULES
1864 if (list_empty(&nf_tables_set_ops)) {
1865 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1866 request_module("nft-set");
1867 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1868 if (!list_empty(&nf_tables_set_ops))
1869 return ERR_PTR(-EAGAIN);
1873 if (nla[NFTA_SET_FLAGS] != NULL) {
1874 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1875 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1878 // FIXME: implement selection properly
1879 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1880 if ((ops->features & features) != features)
1882 if (!try_module_get(ops->owner))
1887 return ERR_PTR(-EOPNOTSUPP);
1890 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1891 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1892 [NFTA_SET_NAME] = { .type = NLA_STRING },
1893 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1894 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1895 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1896 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1897 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1900 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1901 const struct sk_buff *skb,
1902 const struct nlmsghdr *nlh,
1903 const struct nlattr * const nla[])
1905 struct net *net = sock_net(skb->sk);
1906 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1907 const struct nft_af_info *afi;
1908 const struct nft_table *table = NULL;
1910 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1912 return PTR_ERR(afi);
1914 if (nla[NFTA_SET_TABLE] != NULL) {
1915 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1917 return PTR_ERR(table);
1920 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1924 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1925 const struct nlattr *nla)
1927 struct nft_set *set;
1930 return ERR_PTR(-EINVAL);
1932 list_for_each_entry(set, &table->sets, list) {
1933 if (!nla_strcmp(nla, set->name))
1936 return ERR_PTR(-ENOENT);
1939 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1942 const struct nft_set *i;
1944 unsigned long *inuse;
1947 p = strnchr(name, IFNAMSIZ, '%');
1949 if (p[1] != 'd' || strchr(p + 2, '%'))
1952 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1956 list_for_each_entry(i, &ctx->table->sets, list) {
1959 if (!sscanf(i->name, name, &tmp))
1961 if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
1964 set_bit(tmp, inuse);
1967 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1968 free_page((unsigned long)inuse);
1971 snprintf(set->name, sizeof(set->name), name, n);
1972 list_for_each_entry(i, &ctx->table->sets, list) {
1973 if (!strcmp(set->name, i->name))
1979 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
1980 const struct nft_set *set, u16 event, u16 flags)
1982 struct nfgenmsg *nfmsg;
1983 struct nlmsghdr *nlh;
1984 u32 portid = NETLINK_CB(ctx->skb).portid;
1985 u32 seq = ctx->nlh->nlmsg_seq;
1987 event |= NFNL_SUBSYS_NFTABLES << 8;
1988 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1991 goto nla_put_failure;
1993 nfmsg = nlmsg_data(nlh);
1994 nfmsg->nfgen_family = ctx->afi->family;
1995 nfmsg->version = NFNETLINK_V0;
1998 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
1999 goto nla_put_failure;
2000 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2001 goto nla_put_failure;
2002 if (set->flags != 0)
2003 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2004 goto nla_put_failure;
2006 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2007 goto nla_put_failure;
2008 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2009 goto nla_put_failure;
2010 if (set->flags & NFT_SET_MAP) {
2011 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2012 goto nla_put_failure;
2013 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2014 goto nla_put_failure;
2017 return nlmsg_end(skb, nlh);
2020 nlmsg_trim(skb, nlh);
2024 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2025 const struct nft_set *set,
2028 struct sk_buff *skb;
2029 u32 portid = NETLINK_CB(ctx->skb).portid;
2033 report = nlmsg_report(ctx->nlh);
2034 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2038 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2042 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2048 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2052 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2056 static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2057 struct netlink_callback *cb)
2059 const struct nft_set *set;
2060 unsigned int idx = 0, s_idx = cb->args[0];
2065 list_for_each_entry(set, &ctx->table->sets, list) {
2068 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2081 static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2082 struct netlink_callback *cb)
2084 const struct nft_set *set;
2085 unsigned int idx = 0, s_idx = cb->args[0];
2086 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2091 list_for_each_entry(table, &ctx->afi->tables, list) {
2092 if (cur_table && cur_table != table)
2096 list_for_each_entry(set, &ctx->table->sets, list) {
2099 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2102 cb->args[2] = (unsigned long) table;
2114 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2116 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2117 struct nlattr *nla[NFTA_SET_MAX + 1];
2121 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2126 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2130 if (ctx.table == NULL)
2131 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2133 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2138 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2139 const struct nlmsghdr *nlh,
2140 const struct nlattr * const nla[])
2142 const struct nft_set *set;
2144 struct sk_buff *skb2;
2147 /* Verify existance before starting dump */
2148 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2152 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2153 struct netlink_dump_control c = {
2154 .dump = nf_tables_dump_sets,
2156 return netlink_dump_start(nlsk, skb, nlh, &c);
2159 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2161 return PTR_ERR(set);
2163 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2167 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2171 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2178 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2179 const struct nlmsghdr *nlh,
2180 const struct nlattr * const nla[])
2182 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2183 const struct nft_set_ops *ops;
2184 const struct nft_af_info *afi;
2185 struct net *net = sock_net(skb->sk);
2186 struct nft_table *table;
2187 struct nft_set *set;
2189 char name[IFNAMSIZ];
2192 u32 ktype, klen, dlen, dtype, flags;
2195 if (nla[NFTA_SET_TABLE] == NULL ||
2196 nla[NFTA_SET_NAME] == NULL ||
2197 nla[NFTA_SET_KEY_LEN] == NULL)
2200 ktype = NFT_DATA_VALUE;
2201 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2202 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2203 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2207 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2208 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2212 if (nla[NFTA_SET_FLAGS] != NULL) {
2213 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2214 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2215 NFT_SET_INTERVAL | NFT_SET_MAP))
2221 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2222 if (!(flags & NFT_SET_MAP))
2225 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2226 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2227 dtype != NFT_DATA_VERDICT)
2230 if (dtype != NFT_DATA_VERDICT) {
2231 if (nla[NFTA_SET_DATA_LEN] == NULL)
2233 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2235 dlen > FIELD_SIZEOF(struct nft_data, data))
2238 dlen = sizeof(struct nft_data);
2239 } else if (flags & NFT_SET_MAP)
2242 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2244 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2246 return PTR_ERR(afi);
2248 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2250 return PTR_ERR(table);
2252 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2254 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2256 if (PTR_ERR(set) != -ENOENT)
2257 return PTR_ERR(set);
2262 if (nlh->nlmsg_flags & NLM_F_EXCL)
2264 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2269 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2272 ops = nft_select_set_ops(nla);
2274 return PTR_ERR(ops);
2277 if (ops->privsize != NULL)
2278 size = ops->privsize(nla);
2281 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2285 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2286 err = nf_tables_set_alloc_name(&ctx, set, name);
2290 INIT_LIST_HEAD(&set->bindings);
2298 err = ops->init(set, nla);
2302 list_add_tail(&set->list, &table->sets);
2303 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2309 module_put(ops->owner);
2313 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2315 list_del(&set->list);
2316 if (!(set->flags & NFT_SET_ANONYMOUS))
2317 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2319 set->ops->destroy(set);
2320 module_put(set->ops->owner);
2324 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2325 const struct nlmsghdr *nlh,
2326 const struct nlattr * const nla[])
2328 struct nft_set *set;
2332 if (nla[NFTA_SET_TABLE] == NULL)
2335 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2339 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2341 return PTR_ERR(set);
2342 if (!list_empty(&set->bindings))
2345 nf_tables_set_destroy(&ctx, set);
2349 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2350 const struct nft_set *set,
2351 const struct nft_set_iter *iter,
2352 const struct nft_set_elem *elem)
2354 enum nft_registers dreg;
2356 dreg = nft_type_to_reg(set->dtype);
2357 return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
2360 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2361 struct nft_set_binding *binding)
2363 struct nft_set_binding *i;
2364 struct nft_set_iter iter;
2366 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2369 if (set->flags & NFT_SET_MAP) {
2370 /* If the set is already bound to the same chain all
2371 * jumps are already validated for that chain.
2373 list_for_each_entry(i, &set->bindings, list) {
2374 if (i->chain == binding->chain)
2381 iter.fn = nf_tables_bind_check_setelem;
2383 set->ops->walk(ctx, set, &iter);
2385 /* Destroy anonymous sets if binding fails */
2386 if (set->flags & NFT_SET_ANONYMOUS)
2387 nf_tables_set_destroy(ctx, set);
2393 binding->chain = ctx->chain;
2394 list_add_tail(&binding->list, &set->bindings);
2398 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2399 struct nft_set_binding *binding)
2401 list_del(&binding->list);
2403 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2404 nf_tables_set_destroy(ctx, set);
2411 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2412 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2413 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2414 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2417 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2418 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2419 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2420 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2423 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2424 const struct sk_buff *skb,
2425 const struct nlmsghdr *nlh,
2426 const struct nlattr * const nla[])
2428 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2429 const struct nft_af_info *afi;
2430 const struct nft_table *table;
2431 struct net *net = sock_net(skb->sk);
2433 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2435 return PTR_ERR(afi);
2437 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2439 return PTR_ERR(table);
2441 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2445 static int nf_tables_fill_setelem(struct sk_buff *skb,
2446 const struct nft_set *set,
2447 const struct nft_set_elem *elem)
2449 unsigned char *b = skb_tail_pointer(skb);
2450 struct nlattr *nest;
2452 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2454 goto nla_put_failure;
2456 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2458 goto nla_put_failure;
2460 if (set->flags & NFT_SET_MAP &&
2461 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2462 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2463 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2465 goto nla_put_failure;
2467 if (elem->flags != 0)
2468 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2469 goto nla_put_failure;
2471 nla_nest_end(skb, nest);
2479 struct nft_set_dump_args {
2480 const struct netlink_callback *cb;
2481 struct nft_set_iter iter;
2482 struct sk_buff *skb;
2485 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2486 const struct nft_set *set,
2487 const struct nft_set_iter *iter,
2488 const struct nft_set_elem *elem)
2490 struct nft_set_dump_args *args;
2492 args = container_of(iter, struct nft_set_dump_args, iter);
2493 return nf_tables_fill_setelem(args->skb, set, elem);
2496 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2498 const struct nft_set *set;
2499 struct nft_set_dump_args args;
2501 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2502 struct nfgenmsg *nfmsg;
2503 struct nlmsghdr *nlh;
2504 struct nlattr *nest;
2508 nfmsg = nlmsg_data(cb->nlh);
2509 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2510 nft_set_elem_list_policy);
2514 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2518 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2520 return PTR_ERR(set);
2522 event = NFT_MSG_NEWSETELEM;
2523 event |= NFNL_SUBSYS_NFTABLES << 8;
2524 portid = NETLINK_CB(cb->skb).portid;
2525 seq = cb->nlh->nlmsg_seq;
2527 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2530 goto nla_put_failure;
2532 nfmsg = nlmsg_data(nlh);
2533 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2534 nfmsg->version = NFNETLINK_V0;
2537 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2538 goto nla_put_failure;
2539 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2540 goto nla_put_failure;
2542 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2544 goto nla_put_failure;
2548 args.iter.skip = cb->args[0];
2549 args.iter.count = 0;
2551 args.iter.fn = nf_tables_dump_setelem;
2552 set->ops->walk(&ctx, set, &args.iter);
2554 nla_nest_end(skb, nest);
2555 nlmsg_end(skb, nlh);
2557 if (args.iter.err && args.iter.err != -EMSGSIZE)
2558 return args.iter.err;
2559 if (args.iter.count == cb->args[0])
2562 cb->args[0] = args.iter.count;
2569 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2570 const struct nlmsghdr *nlh,
2571 const struct nlattr * const nla[])
2573 const struct nft_set *set;
2577 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2581 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2583 return PTR_ERR(set);
2585 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2586 struct netlink_dump_control c = {
2587 .dump = nf_tables_dump_set,
2589 return netlink_dump_start(nlsk, skb, nlh, &c);
2594 static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2595 const struct nlattr *attr)
2597 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2598 struct nft_data_desc d1, d2;
2599 struct nft_set_elem elem;
2600 struct nft_set_binding *binding;
2601 enum nft_registers dreg;
2604 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2605 nft_set_elem_policy);
2609 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2613 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2614 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2615 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2619 if (set->flags & NFT_SET_MAP) {
2620 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2621 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2624 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2628 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2632 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2636 if (set->ops->get(set, &elem) == 0)
2639 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2640 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2645 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2648 dreg = nft_type_to_reg(set->dtype);
2649 list_for_each_entry(binding, &set->bindings, list) {
2650 struct nft_ctx bind_ctx = {
2652 .table = ctx->table,
2653 .chain = binding->chain,
2656 err = nft_validate_data_load(&bind_ctx, dreg,
2657 &elem.data, d2.type);
2663 err = set->ops->insert(set, &elem);
2670 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2671 nft_data_uninit(&elem.data, d2.type);
2673 nft_data_uninit(&elem.key, d1.type);
2678 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2679 const struct nlmsghdr *nlh,
2680 const struct nlattr * const nla[])
2682 const struct nlattr *attr;
2683 struct nft_set *set;
2687 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2691 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2693 return PTR_ERR(set);
2694 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2697 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2698 err = nft_add_set_elem(&ctx, set, attr);
2705 static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2706 const struct nlattr *attr)
2708 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2709 struct nft_data_desc desc;
2710 struct nft_set_elem elem;
2713 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2714 nft_set_elem_policy);
2719 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2722 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2727 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2730 err = set->ops->get(set, &elem);
2734 set->ops->remove(set, &elem);
2736 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2737 if (set->flags & NFT_SET_MAP)
2738 nft_data_uninit(&elem.data, set->dtype);
2741 nft_data_uninit(&elem.key, desc.type);
2746 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2747 const struct nlmsghdr *nlh,
2748 const struct nlattr * const nla[])
2750 const struct nlattr *attr;
2751 struct nft_set *set;
2755 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2759 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2761 return PTR_ERR(set);
2762 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2765 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2766 err = nft_del_setelem(&ctx, set, attr);
2773 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2774 [NFT_MSG_NEWTABLE] = {
2775 .call = nf_tables_newtable,
2776 .attr_count = NFTA_TABLE_MAX,
2777 .policy = nft_table_policy,
2779 [NFT_MSG_GETTABLE] = {
2780 .call = nf_tables_gettable,
2781 .attr_count = NFTA_TABLE_MAX,
2782 .policy = nft_table_policy,
2784 [NFT_MSG_DELTABLE] = {
2785 .call = nf_tables_deltable,
2786 .attr_count = NFTA_TABLE_MAX,
2787 .policy = nft_table_policy,
2789 [NFT_MSG_NEWCHAIN] = {
2790 .call = nf_tables_newchain,
2791 .attr_count = NFTA_CHAIN_MAX,
2792 .policy = nft_chain_policy,
2794 [NFT_MSG_GETCHAIN] = {
2795 .call = nf_tables_getchain,
2796 .attr_count = NFTA_CHAIN_MAX,
2797 .policy = nft_chain_policy,
2799 [NFT_MSG_DELCHAIN] = {
2800 .call = nf_tables_delchain,
2801 .attr_count = NFTA_CHAIN_MAX,
2802 .policy = nft_chain_policy,
2804 [NFT_MSG_NEWRULE] = {
2805 .call_batch = nf_tables_newrule,
2806 .attr_count = NFTA_RULE_MAX,
2807 .policy = nft_rule_policy,
2809 [NFT_MSG_GETRULE] = {
2810 .call = nf_tables_getrule,
2811 .attr_count = NFTA_RULE_MAX,
2812 .policy = nft_rule_policy,
2814 [NFT_MSG_DELRULE] = {
2815 .call_batch = nf_tables_delrule,
2816 .attr_count = NFTA_RULE_MAX,
2817 .policy = nft_rule_policy,
2819 [NFT_MSG_NEWSET] = {
2820 .call = nf_tables_newset,
2821 .attr_count = NFTA_SET_MAX,
2822 .policy = nft_set_policy,
2824 [NFT_MSG_GETSET] = {
2825 .call = nf_tables_getset,
2826 .attr_count = NFTA_SET_MAX,
2827 .policy = nft_set_policy,
2829 [NFT_MSG_DELSET] = {
2830 .call = nf_tables_delset,
2831 .attr_count = NFTA_SET_MAX,
2832 .policy = nft_set_policy,
2834 [NFT_MSG_NEWSETELEM] = {
2835 .call = nf_tables_newsetelem,
2836 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2837 .policy = nft_set_elem_list_policy,
2839 [NFT_MSG_GETSETELEM] = {
2840 .call = nf_tables_getsetelem,
2841 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2842 .policy = nft_set_elem_list_policy,
2844 [NFT_MSG_DELSETELEM] = {
2845 .call = nf_tables_delsetelem,
2846 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2847 .policy = nft_set_elem_list_policy,
2851 static const struct nfnetlink_subsystem nf_tables_subsys = {
2852 .name = "nf_tables",
2853 .subsys_id = NFNL_SUBSYS_NFTABLES,
2854 .cb_count = NFT_MSG_MAX,
2856 .commit = nf_tables_commit,
2857 .abort = nf_tables_abort,
2861 * Loop detection - walk through the ruleset beginning at the destination chain
2862 * of a new jump until either the source chain is reached (loop) or all
2863 * reachable chains have been traversed.
2865 * The loop check is performed whenever a new jump verdict is added to an
2866 * expression or verdict map or a verdict map is bound to a new chain.
2869 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2870 const struct nft_chain *chain);
2872 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2873 const struct nft_set *set,
2874 const struct nft_set_iter *iter,
2875 const struct nft_set_elem *elem)
2877 switch (elem->data.verdict) {
2880 return nf_tables_check_loops(ctx, elem->data.chain);
2886 static int nf_tables_check_loops(const struct nft_ctx *ctx,
2887 const struct nft_chain *chain)
2889 const struct nft_rule *rule;
2890 const struct nft_expr *expr, *last;
2891 const struct nft_set *set;
2892 struct nft_set_binding *binding;
2893 struct nft_set_iter iter;
2895 if (ctx->chain == chain)
2898 list_for_each_entry(rule, &chain->rules, list) {
2899 nft_rule_for_each_expr(expr, last, rule) {
2900 const struct nft_data *data = NULL;
2903 if (!expr->ops->validate)
2906 err = expr->ops->validate(ctx, expr, &data);
2913 switch (data->verdict) {
2916 err = nf_tables_check_loops(ctx, data->chain);
2925 list_for_each_entry(set, &ctx->table->sets, list) {
2926 if (!(set->flags & NFT_SET_MAP) ||
2927 set->dtype != NFT_DATA_VERDICT)
2930 list_for_each_entry(binding, &set->bindings, list) {
2931 if (binding->chain != chain)
2937 iter.fn = nf_tables_loop_check_setelem;
2939 set->ops->walk(ctx, set, &iter);
2949 * nft_validate_input_register - validate an expressions' input register
2951 * @reg: the register number
2953 * Validate that the input register is one of the general purpose
2956 int nft_validate_input_register(enum nft_registers reg)
2958 if (reg <= NFT_REG_VERDICT)
2960 if (reg > NFT_REG_MAX)
2964 EXPORT_SYMBOL_GPL(nft_validate_input_register);
2967 * nft_validate_output_register - validate an expressions' output register
2969 * @reg: the register number
2971 * Validate that the output register is one of the general purpose
2972 * registers or the verdict register.
2974 int nft_validate_output_register(enum nft_registers reg)
2976 if (reg < NFT_REG_VERDICT)
2978 if (reg > NFT_REG_MAX)
2982 EXPORT_SYMBOL_GPL(nft_validate_output_register);
2985 * nft_validate_data_load - validate an expressions' data load
2987 * @ctx: context of the expression performing the load
2988 * @reg: the destination register number
2989 * @data: the data to load
2990 * @type: the data type
2992 * Validate that a data load uses the appropriate data type for
2993 * the destination register. A value of NULL for the data means
2994 * that its runtime gathered data, which is always of type
2997 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
2998 const struct nft_data *data,
2999 enum nft_data_types type)
3004 case NFT_REG_VERDICT:
3005 if (data == NULL || type != NFT_DATA_VERDICT)
3008 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3009 err = nf_tables_check_loops(ctx, data->chain);
3013 if (ctx->chain->level + 1 > data->chain->level) {
3014 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3016 data->chain->level = ctx->chain->level + 1;
3022 if (data != NULL && type != NFT_DATA_VALUE)
3027 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3029 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3030 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3031 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3032 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3035 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3036 struct nft_data_desc *desc, const struct nlattr *nla)
3038 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3039 struct nft_chain *chain;
3042 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3046 if (!tb[NFTA_VERDICT_CODE])
3048 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3050 switch (data->verdict) {
3057 desc->len = sizeof(data->verdict);
3061 if (!tb[NFTA_VERDICT_CHAIN])
3063 chain = nf_tables_chain_lookup(ctx->table,
3064 tb[NFTA_VERDICT_CHAIN]);
3066 return PTR_ERR(chain);
3067 if (chain->flags & NFT_BASE_CHAIN)
3071 data->chain = chain;
3072 desc->len = sizeof(data);
3078 desc->type = NFT_DATA_VERDICT;
3082 static void nft_verdict_uninit(const struct nft_data *data)
3084 switch (data->verdict) {
3092 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3094 struct nlattr *nest;
3096 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3098 goto nla_put_failure;
3100 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3101 goto nla_put_failure;
3103 switch (data->verdict) {
3106 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3107 goto nla_put_failure;
3109 nla_nest_end(skb, nest);
3116 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3117 struct nft_data_desc *desc, const struct nlattr *nla)
3124 if (len > sizeof(data->data))
3127 nla_memcpy(data->data, nla, sizeof(data->data));
3128 desc->type = NFT_DATA_VALUE;
3133 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3136 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3139 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3140 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3141 .len = FIELD_SIZEOF(struct nft_data, data) },
3142 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3146 * nft_data_init - parse nf_tables data netlink attributes
3148 * @ctx: context of the expression using the data
3149 * @data: destination struct nft_data
3150 * @desc: data description
3151 * @nla: netlink attribute containing data
3153 * Parse the netlink data attributes and initialize a struct nft_data.
3154 * The type and length of data are returned in the data description.
3156 * The caller can indicate that it only wants to accept data of type
3157 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3159 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3160 struct nft_data_desc *desc, const struct nlattr *nla)
3162 struct nlattr *tb[NFTA_DATA_MAX + 1];
3165 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3169 if (tb[NFTA_DATA_VALUE])
3170 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3171 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3172 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3175 EXPORT_SYMBOL_GPL(nft_data_init);
3178 * nft_data_uninit - release a nft_data item
3180 * @data: struct nft_data to release
3181 * @type: type of data
3183 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3184 * all others need to be released by calling this function.
3186 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3189 case NFT_DATA_VALUE:
3191 case NFT_DATA_VERDICT:
3192 return nft_verdict_uninit(data);
3197 EXPORT_SYMBOL_GPL(nft_data_uninit);
3199 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3200 enum nft_data_types type, unsigned int len)
3202 struct nlattr *nest;
3205 nest = nla_nest_start(skb, attr);
3210 case NFT_DATA_VALUE:
3211 err = nft_value_dump(skb, data, len);
3213 case NFT_DATA_VERDICT:
3214 err = nft_verdict_dump(skb, data);
3221 nla_nest_end(skb, nest);
3224 EXPORT_SYMBOL_GPL(nft_data_dump);
3226 static int nf_tables_init_net(struct net *net)
3228 INIT_LIST_HEAD(&net->nft.af_info);
3229 INIT_LIST_HEAD(&net->nft.commit_list);
3233 static struct pernet_operations nf_tables_net_ops = {
3234 .init = nf_tables_init_net,
3237 static int __init nf_tables_module_init(void)
3241 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3248 err = nf_tables_core_module_init();
3252 err = nfnetlink_subsys_register(&nf_tables_subsys);
3256 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3257 return register_pernet_subsys(&nf_tables_net_ops);
3259 nf_tables_core_module_exit();
3266 static void __exit nf_tables_module_exit(void)
3268 unregister_pernet_subsys(&nf_tables_net_ops);
3269 nfnetlink_subsys_unregister(&nf_tables_subsys);
3270 nf_tables_core_module_exit();
3274 module_init(nf_tables_module_init);
3275 module_exit(nf_tables_module_exit);
3277 MODULE_LICENSE("GPL");
3278 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3279 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);