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
& nft_genmask_cur(net
)) == 0;
205 nft_rule_is_active_next(struct net
*net
, const struct nft_rule
*rule
)
207 return (rule
->genmask
& nft_genmask_next(net
)) == 0;
211 nft_rule_activate_next(struct net
*net
, struct nft_rule
*rule
)
213 /* Now inactive, will be active in the future */
214 rule
->genmask
= nft_genmask_cur(net
);
218 nft_rule_deactivate_next(struct net
*net
, struct nft_rule
*rule
)
220 rule
->genmask
= nft_genmask_next(net
);
223 static inline void nft_rule_clear(struct net
*net
, struct nft_rule
*rule
)
225 rule
->genmask
&= ~nft_genmask_next(net
);
229 nf_tables_delrule_deactivate(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
231 /* You cannot delete the same rule twice */
232 if (nft_rule_is_active_next(ctx
->net
, rule
)) {
233 nft_rule_deactivate_next(ctx
->net
, rule
);
240 static struct nft_trans
*nft_trans_rule_add(struct nft_ctx
*ctx
, int msg_type
,
241 struct nft_rule
*rule
)
243 struct nft_trans
*trans
;
245 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_rule
));
249 nft_trans_rule(trans
) = rule
;
250 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
255 static int nft_delrule(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
257 struct nft_trans
*trans
;
260 trans
= nft_trans_rule_add(ctx
, NFT_MSG_DELRULE
, rule
);
264 err
= nf_tables_delrule_deactivate(ctx
, rule
);
266 nft_trans_destroy(trans
);
273 static int nft_delrule_by_chain(struct nft_ctx
*ctx
)
275 struct nft_rule
*rule
;
278 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
279 err
= nft_delrule(ctx
, rule
);
286 /* Internal set flag */
287 #define NFT_SET_INACTIVE (1 << 15)
289 static int nft_trans_set_add(struct nft_ctx
*ctx
, int msg_type
,
292 struct nft_trans
*trans
;
294 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_set
));
298 if (msg_type
== NFT_MSG_NEWSET
&& ctx
->nla
[NFTA_SET_ID
] != NULL
) {
299 nft_trans_set_id(trans
) =
300 ntohl(nla_get_be32(ctx
->nla
[NFTA_SET_ID
]));
301 set
->flags
|= NFT_SET_INACTIVE
;
303 nft_trans_set(trans
) = set
;
304 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
309 static int nft_delset(struct nft_ctx
*ctx
, struct nft_set
*set
)
313 err
= nft_trans_set_add(ctx
, NFT_MSG_DELSET
, set
);
317 list_del_rcu(&set
->list
);
327 static struct nft_table
*nft_table_lookup(const struct nft_af_info
*afi
,
328 const struct nlattr
*nla
)
330 struct nft_table
*table
;
332 list_for_each_entry(table
, &afi
->tables
, list
) {
333 if (!nla_strcmp(nla
, table
->name
))
339 static struct nft_table
*nf_tables_table_lookup(const struct nft_af_info
*afi
,
340 const struct nlattr
*nla
)
342 struct nft_table
*table
;
345 return ERR_PTR(-EINVAL
);
347 table
= nft_table_lookup(afi
, nla
);
351 return ERR_PTR(-ENOENT
);
354 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
356 return ++table
->hgenerator
;
359 static const struct nf_chain_type
*chain_type
[AF_MAX
][NFT_CHAIN_T_MAX
];
361 static const struct nf_chain_type
*
362 __nf_tables_chain_type_lookup(int family
, const struct nlattr
*nla
)
366 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
367 if (chain_type
[family
][i
] != NULL
&&
368 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
369 return chain_type
[family
][i
];
374 static const struct nf_chain_type
*
375 nf_tables_chain_type_lookup(const struct nft_af_info
*afi
,
376 const struct nlattr
*nla
,
379 const struct nf_chain_type
*type
;
381 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
384 #ifdef CONFIG_MODULES
386 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
387 request_module("nft-chain-%u-%.*s", afi
->family
,
388 nla_len(nla
), (const char *)nla_data(nla
));
389 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
390 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
392 return ERR_PTR(-EAGAIN
);
395 return ERR_PTR(-ENOENT
);
398 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
399 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
,
400 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
401 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
404 static int nf_tables_fill_table_info(struct sk_buff
*skb
, struct net
*net
,
405 u32 portid
, u32 seq
, int event
, u32 flags
,
406 int family
, const struct nft_table
*table
)
408 struct nlmsghdr
*nlh
;
409 struct nfgenmsg
*nfmsg
;
411 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
412 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
414 goto nla_put_failure
;
416 nfmsg
= nlmsg_data(nlh
);
417 nfmsg
->nfgen_family
= family
;
418 nfmsg
->version
= NFNETLINK_V0
;
419 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
421 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
422 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
423 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)))
424 goto nla_put_failure
;
430 nlmsg_trim(skb
, nlh
);
434 static int nf_tables_table_notify(const struct nft_ctx
*ctx
, int event
)
440 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
444 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
448 err
= nf_tables_fill_table_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
449 event
, 0, ctx
->afi
->family
, ctx
->table
);
455 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
456 ctx
->report
, GFP_KERNEL
);
459 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
465 static int nf_tables_dump_tables(struct sk_buff
*skb
,
466 struct netlink_callback
*cb
)
468 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
469 const struct nft_af_info
*afi
;
470 const struct nft_table
*table
;
471 unsigned int idx
= 0, s_idx
= cb
->args
[0];
472 struct net
*net
= sock_net(skb
->sk
);
473 int family
= nfmsg
->nfgen_family
;
476 cb
->seq
= net
->nft
.base_seq
;
478 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
479 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
482 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
486 memset(&cb
->args
[1], 0,
487 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
488 if (nf_tables_fill_table_info(skb
, net
,
489 NETLINK_CB(cb
->skb
).portid
,
493 afi
->family
, table
) < 0)
496 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
507 static int nf_tables_gettable(struct sock
*nlsk
, struct sk_buff
*skb
,
508 const struct nlmsghdr
*nlh
,
509 const struct nlattr
* const nla
[])
511 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
512 const struct nft_af_info
*afi
;
513 const struct nft_table
*table
;
514 struct sk_buff
*skb2
;
515 struct net
*net
= sock_net(skb
->sk
);
516 int family
= nfmsg
->nfgen_family
;
519 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
520 struct netlink_dump_control c
= {
521 .dump
= nf_tables_dump_tables
,
523 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
526 afi
= nf_tables_afinfo_lookup(net
, family
, false);
530 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
532 return PTR_ERR(table
);
533 if (table
->flags
& NFT_TABLE_INACTIVE
)
536 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
540 err
= nf_tables_fill_table_info(skb2
, net
, NETLINK_CB(skb
).portid
,
541 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
546 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
553 static int nf_tables_table_enable(const struct nft_af_info
*afi
,
554 struct nft_table
*table
)
556 struct nft_chain
*chain
;
559 list_for_each_entry(chain
, &table
->chains
, list
) {
560 if (!(chain
->flags
& NFT_BASE_CHAIN
))
563 err
= nf_register_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
571 list_for_each_entry(chain
, &table
->chains
, list
) {
572 if (!(chain
->flags
& NFT_BASE_CHAIN
))
578 nf_unregister_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
583 static void nf_tables_table_disable(const struct nft_af_info
*afi
,
584 struct nft_table
*table
)
586 struct nft_chain
*chain
;
588 list_for_each_entry(chain
, &table
->chains
, list
) {
589 if (chain
->flags
& NFT_BASE_CHAIN
)
590 nf_unregister_hooks(nft_base_chain(chain
)->ops
,
595 static int nf_tables_updtable(struct nft_ctx
*ctx
)
597 struct nft_trans
*trans
;
601 if (!ctx
->nla
[NFTA_TABLE_FLAGS
])
604 flags
= ntohl(nla_get_be32(ctx
->nla
[NFTA_TABLE_FLAGS
]));
605 if (flags
& ~NFT_TABLE_F_DORMANT
)
608 if (flags
== ctx
->table
->flags
)
611 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWTABLE
,
612 sizeof(struct nft_trans_table
));
616 if ((flags
& NFT_TABLE_F_DORMANT
) &&
617 !(ctx
->table
->flags
& NFT_TABLE_F_DORMANT
)) {
618 nft_trans_table_enable(trans
) = false;
619 } else if (!(flags
& NFT_TABLE_F_DORMANT
) &&
620 ctx
->table
->flags
& NFT_TABLE_F_DORMANT
) {
621 ret
= nf_tables_table_enable(ctx
->afi
, ctx
->table
);
623 ctx
->table
->flags
&= ~NFT_TABLE_F_DORMANT
;
624 nft_trans_table_enable(trans
) = true;
630 nft_trans_table_update(trans
) = true;
631 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
634 nft_trans_destroy(trans
);
638 static int nf_tables_newtable(struct sock
*nlsk
, struct sk_buff
*skb
,
639 const struct nlmsghdr
*nlh
,
640 const struct nlattr
* const nla
[])
642 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
643 const struct nlattr
*name
;
644 struct nft_af_info
*afi
;
645 struct nft_table
*table
;
646 struct net
*net
= sock_net(skb
->sk
);
647 int family
= nfmsg
->nfgen_family
;
652 afi
= nf_tables_afinfo_lookup(net
, family
, true);
656 name
= nla
[NFTA_TABLE_NAME
];
657 table
= nf_tables_table_lookup(afi
, name
);
659 if (PTR_ERR(table
) != -ENOENT
)
660 return PTR_ERR(table
);
665 if (table
->flags
& NFT_TABLE_INACTIVE
)
667 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
669 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
672 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
673 return nf_tables_updtable(&ctx
);
676 if (nla
[NFTA_TABLE_FLAGS
]) {
677 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
678 if (flags
& ~NFT_TABLE_F_DORMANT
)
682 if (!try_module_get(afi
->owner
))
683 return -EAFNOSUPPORT
;
686 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
690 nla_strlcpy(table
->name
, name
, NFT_TABLE_MAXNAMELEN
);
691 INIT_LIST_HEAD(&table
->chains
);
692 INIT_LIST_HEAD(&table
->sets
);
693 table
->flags
= flags
;
695 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
696 err
= nft_trans_table_add(&ctx
, NFT_MSG_NEWTABLE
);
700 list_add_tail_rcu(&table
->list
, &afi
->tables
);
705 module_put(afi
->owner
);
709 static int nft_flush_table(struct nft_ctx
*ctx
)
712 struct nft_chain
*chain
, *nc
;
713 struct nft_set
*set
, *ns
;
715 list_for_each_entry(chain
, &ctx
->table
->chains
, list
) {
718 err
= nft_delrule_by_chain(ctx
);
723 list_for_each_entry_safe(set
, ns
, &ctx
->table
->sets
, list
) {
724 if (set
->flags
& NFT_SET_ANONYMOUS
&&
725 !list_empty(&set
->bindings
))
728 err
= nft_delset(ctx
, set
);
733 list_for_each_entry_safe(chain
, nc
, &ctx
->table
->chains
, list
) {
736 err
= nft_delchain(ctx
);
741 err
= nft_deltable(ctx
);
746 static int nft_flush(struct nft_ctx
*ctx
, int family
)
748 struct nft_af_info
*afi
;
749 struct nft_table
*table
, *nt
;
750 const struct nlattr
* const *nla
= ctx
->nla
;
753 list_for_each_entry(afi
, &ctx
->net
->nft
.af_info
, list
) {
754 if (family
!= AF_UNSPEC
&& afi
->family
!= family
)
758 list_for_each_entry_safe(table
, nt
, &afi
->tables
, list
) {
759 if (nla
[NFTA_TABLE_NAME
] &&
760 nla_strcmp(nla
[NFTA_TABLE_NAME
], table
->name
) != 0)
765 err
= nft_flush_table(ctx
);
774 static int nf_tables_deltable(struct sock
*nlsk
, struct sk_buff
*skb
,
775 const struct nlmsghdr
*nlh
,
776 const struct nlattr
* const nla
[])
778 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
779 struct nft_af_info
*afi
;
780 struct nft_table
*table
;
781 struct net
*net
= sock_net(skb
->sk
);
782 int family
= nfmsg
->nfgen_family
;
785 nft_ctx_init(&ctx
, skb
, nlh
, NULL
, NULL
, NULL
, nla
);
786 if (family
== AF_UNSPEC
|| nla
[NFTA_TABLE_NAME
] == NULL
)
787 return nft_flush(&ctx
, family
);
789 afi
= nf_tables_afinfo_lookup(net
, family
, false);
793 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
795 return PTR_ERR(table
);
796 if (table
->flags
& NFT_TABLE_INACTIVE
)
802 return nft_flush_table(&ctx
);
805 static void nf_tables_table_destroy(struct nft_ctx
*ctx
)
807 BUG_ON(ctx
->table
->use
> 0);
810 module_put(ctx
->afi
->owner
);
813 int nft_register_chain_type(const struct nf_chain_type
*ctype
)
817 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
818 if (chain_type
[ctype
->family
][ctype
->type
] != NULL
) {
822 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
824 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
827 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
829 void nft_unregister_chain_type(const struct nf_chain_type
*ctype
)
831 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
832 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
833 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
835 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
841 static struct nft_chain
*
842 nf_tables_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
)
844 struct nft_chain
*chain
;
846 list_for_each_entry(chain
, &table
->chains
, list
) {
847 if (chain
->handle
== handle
)
851 return ERR_PTR(-ENOENT
);
854 static struct nft_chain
*nf_tables_chain_lookup(const struct nft_table
*table
,
855 const struct nlattr
*nla
)
857 struct nft_chain
*chain
;
860 return ERR_PTR(-EINVAL
);
862 list_for_each_entry(chain
, &table
->chains
, list
) {
863 if (!nla_strcmp(nla
, chain
->name
))
867 return ERR_PTR(-ENOENT
);
870 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
871 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
},
872 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
873 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
874 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
875 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
876 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
877 [NFTA_CHAIN_TYPE
] = { .type
= NLA_STRING
},
878 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
881 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
882 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
883 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
886 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
888 struct nft_stats
*cpu_stats
, total
;
894 memset(&total
, 0, sizeof(total
));
895 for_each_possible_cpu(cpu
) {
896 cpu_stats
= per_cpu_ptr(stats
, cpu
);
898 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
899 pkts
= cpu_stats
->pkts
;
900 bytes
= cpu_stats
->bytes
;
901 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
903 total
.bytes
+= bytes
;
905 nest
= nla_nest_start(skb
, NFTA_CHAIN_COUNTERS
);
907 goto nla_put_failure
;
909 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
)) ||
910 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
)))
911 goto nla_put_failure
;
913 nla_nest_end(skb
, nest
);
920 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, struct net
*net
,
921 u32 portid
, u32 seq
, int event
, u32 flags
,
922 int family
, const struct nft_table
*table
,
923 const struct nft_chain
*chain
)
925 struct nlmsghdr
*nlh
;
926 struct nfgenmsg
*nfmsg
;
928 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
929 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
931 goto nla_put_failure
;
933 nfmsg
= nlmsg_data(nlh
);
934 nfmsg
->nfgen_family
= family
;
935 nfmsg
->version
= NFNETLINK_V0
;
936 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
938 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
939 goto nla_put_failure
;
940 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
)))
941 goto nla_put_failure
;
942 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
943 goto nla_put_failure
;
945 if (chain
->flags
& NFT_BASE_CHAIN
) {
946 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
947 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
950 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
952 goto nla_put_failure
;
953 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
954 goto nla_put_failure
;
955 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
956 goto nla_put_failure
;
957 nla_nest_end(skb
, nest
);
959 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
960 htonl(basechain
->policy
)))
961 goto nla_put_failure
;
963 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
964 goto nla_put_failure
;
966 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
967 goto nla_put_failure
;
970 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
971 goto nla_put_failure
;
977 nlmsg_trim(skb
, nlh
);
981 static int nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
987 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
991 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
995 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
996 event
, 0, ctx
->afi
->family
, ctx
->table
,
1003 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1004 ctx
->report
, GFP_KERNEL
);
1007 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1013 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1014 struct netlink_callback
*cb
)
1016 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1017 const struct nft_af_info
*afi
;
1018 const struct nft_table
*table
;
1019 const struct nft_chain
*chain
;
1020 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1021 struct net
*net
= sock_net(skb
->sk
);
1022 int family
= nfmsg
->nfgen_family
;
1025 cb
->seq
= net
->nft
.base_seq
;
1027 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1028 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1031 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1032 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1036 memset(&cb
->args
[1], 0,
1037 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1038 if (nf_tables_fill_chain_info(skb
, net
,
1039 NETLINK_CB(cb
->skb
).portid
,
1043 afi
->family
, table
, chain
) < 0)
1046 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1058 static int nf_tables_getchain(struct sock
*nlsk
, struct sk_buff
*skb
,
1059 const struct nlmsghdr
*nlh
,
1060 const struct nlattr
* const nla
[])
1062 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1063 const struct nft_af_info
*afi
;
1064 const struct nft_table
*table
;
1065 const struct nft_chain
*chain
;
1066 struct sk_buff
*skb2
;
1067 struct net
*net
= sock_net(skb
->sk
);
1068 int family
= nfmsg
->nfgen_family
;
1071 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1072 struct netlink_dump_control c
= {
1073 .dump
= nf_tables_dump_chains
,
1075 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1078 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1080 return PTR_ERR(afi
);
1082 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1084 return PTR_ERR(table
);
1085 if (table
->flags
& NFT_TABLE_INACTIVE
)
1088 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1090 return PTR_ERR(chain
);
1091 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1094 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1098 err
= nf_tables_fill_chain_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1099 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
1100 family
, table
, chain
);
1104 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1111 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
1112 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
1113 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
1116 static struct nft_stats __percpu
*nft_stats_alloc(const struct nlattr
*attr
)
1118 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
1119 struct nft_stats __percpu
*newstats
;
1120 struct nft_stats
*stats
;
1123 err
= nla_parse_nested(tb
, NFTA_COUNTER_MAX
, attr
, nft_counter_policy
);
1125 return ERR_PTR(err
);
1127 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
1128 return ERR_PTR(-EINVAL
);
1130 newstats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1131 if (newstats
== NULL
)
1132 return ERR_PTR(-ENOMEM
);
1134 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1135 * are not exposed to userspace.
1138 stats
= this_cpu_ptr(newstats
);
1139 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
1140 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
1146 static void nft_chain_stats_replace(struct nft_base_chain
*chain
,
1147 struct nft_stats __percpu
*newstats
)
1149 if (newstats
== NULL
)
1153 struct nft_stats __percpu
*oldstats
=
1154 nft_dereference(chain
->stats
);
1156 rcu_assign_pointer(chain
->stats
, newstats
);
1158 free_percpu(oldstats
);
1160 rcu_assign_pointer(chain
->stats
, newstats
);
1163 static void nf_tables_chain_destroy(struct nft_chain
*chain
)
1165 BUG_ON(chain
->use
> 0);
1167 if (chain
->flags
& NFT_BASE_CHAIN
) {
1168 module_put(nft_base_chain(chain
)->type
->owner
);
1169 free_percpu(nft_base_chain(chain
)->stats
);
1170 kfree(nft_base_chain(chain
));
1176 static int nf_tables_newchain(struct sock
*nlsk
, struct sk_buff
*skb
,
1177 const struct nlmsghdr
*nlh
,
1178 const struct nlattr
* const nla
[])
1180 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1181 const struct nlattr
* uninitialized_var(name
);
1182 struct nft_af_info
*afi
;
1183 struct nft_table
*table
;
1184 struct nft_chain
*chain
;
1185 struct nft_base_chain
*basechain
= NULL
;
1186 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
1187 struct net
*net
= sock_net(skb
->sk
);
1188 int family
= nfmsg
->nfgen_family
;
1189 u8 policy
= NF_ACCEPT
;
1192 struct nft_stats __percpu
*stats
;
1197 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1199 afi
= nf_tables_afinfo_lookup(net
, family
, true);
1201 return PTR_ERR(afi
);
1203 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1205 return PTR_ERR(table
);
1208 name
= nla
[NFTA_CHAIN_NAME
];
1210 if (nla
[NFTA_CHAIN_HANDLE
]) {
1211 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
1212 chain
= nf_tables_chain_lookup_byhandle(table
, handle
);
1214 return PTR_ERR(chain
);
1216 chain
= nf_tables_chain_lookup(table
, name
);
1217 if (IS_ERR(chain
)) {
1218 if (PTR_ERR(chain
) != -ENOENT
)
1219 return PTR_ERR(chain
);
1224 if (nla
[NFTA_CHAIN_POLICY
]) {
1225 if ((chain
!= NULL
&&
1226 !(chain
->flags
& NFT_BASE_CHAIN
)))
1229 if (chain
== NULL
&&
1230 nla
[NFTA_CHAIN_HOOK
] == NULL
)
1233 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
1243 if (chain
!= NULL
) {
1244 struct nft_stats
*stats
= NULL
;
1245 struct nft_trans
*trans
;
1247 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1249 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1251 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1254 if (nla
[NFTA_CHAIN_HANDLE
] && name
&&
1255 !IS_ERR(nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
])))
1258 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1259 if (!(chain
->flags
& NFT_BASE_CHAIN
))
1262 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1264 return PTR_ERR(stats
);
1267 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1268 trans
= nft_trans_alloc(&ctx
, NFT_MSG_NEWCHAIN
,
1269 sizeof(struct nft_trans_chain
));
1270 if (trans
== NULL
) {
1275 nft_trans_chain_stats(trans
) = stats
;
1276 nft_trans_chain_update(trans
) = true;
1278 if (nla
[NFTA_CHAIN_POLICY
])
1279 nft_trans_chain_policy(trans
) = policy
;
1281 nft_trans_chain_policy(trans
) = -1;
1283 if (nla
[NFTA_CHAIN_HANDLE
] && name
) {
1284 nla_strlcpy(nft_trans_chain_name(trans
), name
,
1285 NFT_CHAIN_MAXNAMELEN
);
1287 list_add_tail(&trans
->list
, &net
->nft
.commit_list
);
1291 if (table
->use
== UINT_MAX
)
1294 if (nla
[NFTA_CHAIN_HOOK
]) {
1295 const struct nf_chain_type
*type
;
1296 struct nf_hook_ops
*ops
;
1298 u32 hooknum
, priority
;
1300 type
= chain_type
[family
][NFT_CHAIN_T_DEFAULT
];
1301 if (nla
[NFTA_CHAIN_TYPE
]) {
1302 type
= nf_tables_chain_type_lookup(afi
,
1303 nla
[NFTA_CHAIN_TYPE
],
1306 return PTR_ERR(type
);
1309 err
= nla_parse_nested(ha
, NFTA_HOOK_MAX
, nla
[NFTA_CHAIN_HOOK
],
1313 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
1314 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
1317 hooknum
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
1318 if (hooknum
>= afi
->nhooks
)
1320 priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
1322 if (!(type
->hook_mask
& (1 << hooknum
)))
1324 if (!try_module_get(type
->owner
))
1326 hookfn
= type
->hooks
[hooknum
];
1328 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
1329 if (basechain
== NULL
) {
1330 module_put(type
->owner
);
1334 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1335 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1336 if (IS_ERR(stats
)) {
1337 module_put(type
->owner
);
1339 return PTR_ERR(stats
);
1341 basechain
->stats
= stats
;
1343 stats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1344 if (stats
== NULL
) {
1345 module_put(type
->owner
);
1349 rcu_assign_pointer(basechain
->stats
, stats
);
1352 write_pnet(&basechain
->pnet
, net
);
1353 basechain
->type
= type
;
1354 chain
= &basechain
->chain
;
1356 for (i
= 0; i
< afi
->nops
; i
++) {
1357 ops
= &basechain
->ops
[i
];
1359 ops
->owner
= afi
->owner
;
1360 ops
->hooknum
= hooknum
;
1361 ops
->priority
= priority
;
1363 ops
->hook
= afi
->hooks
[ops
->hooknum
];
1366 if (afi
->hook_ops_init
)
1367 afi
->hook_ops_init(ops
, i
);
1370 chain
->flags
|= NFT_BASE_CHAIN
;
1371 basechain
->policy
= policy
;
1373 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
1378 INIT_LIST_HEAD(&chain
->rules
);
1379 chain
->handle
= nf_tables_alloc_handle(table
);
1380 chain
->table
= table
;
1381 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
1383 if (!(table
->flags
& NFT_TABLE_F_DORMANT
) &&
1384 chain
->flags
& NFT_BASE_CHAIN
) {
1385 err
= nf_register_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
1390 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1391 err
= nft_trans_chain_add(&ctx
, NFT_MSG_NEWCHAIN
);
1396 list_add_tail_rcu(&chain
->list
, &table
->chains
);
1399 nf_tables_unregister_hooks(table
, chain
, afi
->nops
);
1401 nf_tables_chain_destroy(chain
);
1405 static int nf_tables_delchain(struct sock
*nlsk
, struct sk_buff
*skb
,
1406 const struct nlmsghdr
*nlh
,
1407 const struct nlattr
* const nla
[])
1409 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1410 struct nft_af_info
*afi
;
1411 struct nft_table
*table
;
1412 struct nft_chain
*chain
;
1413 struct net
*net
= sock_net(skb
->sk
);
1414 int family
= nfmsg
->nfgen_family
;
1417 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1419 return PTR_ERR(afi
);
1421 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1423 return PTR_ERR(table
);
1424 if (table
->flags
& NFT_TABLE_INACTIVE
)
1427 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1429 return PTR_ERR(chain
);
1430 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1435 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1437 return nft_delchain(&ctx
);
1445 * nft_register_expr - register nf_tables expr type
1448 * Registers the expr type for use with nf_tables. Returns zero on
1449 * success or a negative errno code otherwise.
1451 int nft_register_expr(struct nft_expr_type
*type
)
1453 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1454 if (type
->family
== NFPROTO_UNSPEC
)
1455 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
1457 list_add_rcu(&type
->list
, &nf_tables_expressions
);
1458 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1461 EXPORT_SYMBOL_GPL(nft_register_expr
);
1464 * nft_unregister_expr - unregister nf_tables expr type
1467 * Unregisters the expr typefor use with nf_tables.
1469 void nft_unregister_expr(struct nft_expr_type
*type
)
1471 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1472 list_del_rcu(&type
->list
);
1473 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1475 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1477 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
1480 const struct nft_expr_type
*type
;
1482 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1483 if (!nla_strcmp(nla
, type
->name
) &&
1484 (!type
->family
|| type
->family
== family
))
1490 static const struct nft_expr_type
*nft_expr_type_get(u8 family
,
1493 const struct nft_expr_type
*type
;
1496 return ERR_PTR(-EINVAL
);
1498 type
= __nft_expr_type_get(family
, nla
);
1499 if (type
!= NULL
&& try_module_get(type
->owner
))
1502 #ifdef CONFIG_MODULES
1504 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1505 request_module("nft-expr-%u-%.*s", family
,
1506 nla_len(nla
), (char *)nla_data(nla
));
1507 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1508 if (__nft_expr_type_get(family
, nla
))
1509 return ERR_PTR(-EAGAIN
);
1511 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1512 request_module("nft-expr-%.*s",
1513 nla_len(nla
), (char *)nla_data(nla
));
1514 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1515 if (__nft_expr_type_get(family
, nla
))
1516 return ERR_PTR(-EAGAIN
);
1519 return ERR_PTR(-ENOENT
);
1522 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1523 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1524 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1527 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1528 const struct nft_expr
*expr
)
1530 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1531 goto nla_put_failure
;
1533 if (expr
->ops
->dump
) {
1534 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1536 goto nla_put_failure
;
1537 if (expr
->ops
->dump(skb
, expr
) < 0)
1538 goto nla_put_failure
;
1539 nla_nest_end(skb
, data
);
1548 struct nft_expr_info
{
1549 const struct nft_expr_ops
*ops
;
1550 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1553 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1554 const struct nlattr
*nla
,
1555 struct nft_expr_info
*info
)
1557 const struct nft_expr_type
*type
;
1558 const struct nft_expr_ops
*ops
;
1559 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1562 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
);
1566 type
= nft_expr_type_get(ctx
->afi
->family
, tb
[NFTA_EXPR_NAME
]);
1568 return PTR_ERR(type
);
1570 if (tb
[NFTA_EXPR_DATA
]) {
1571 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1572 tb
[NFTA_EXPR_DATA
], type
->policy
);
1576 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1578 if (type
->select_ops
!= NULL
) {
1579 ops
= type
->select_ops(ctx
,
1580 (const struct nlattr
* const *)info
->tb
);
1592 module_put(type
->owner
);
1596 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1597 const struct nft_expr_info
*info
,
1598 struct nft_expr
*expr
)
1600 const struct nft_expr_ops
*ops
= info
->ops
;
1605 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1617 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
1618 struct nft_expr
*expr
)
1620 if (expr
->ops
->destroy
)
1621 expr
->ops
->destroy(ctx
, expr
);
1622 module_put(expr
->ops
->type
->owner
);
1629 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1632 struct nft_rule
*rule
;
1634 // FIXME: this sucks
1635 list_for_each_entry(rule
, &chain
->rules
, list
) {
1636 if (handle
== rule
->handle
)
1640 return ERR_PTR(-ENOENT
);
1643 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1644 const struct nlattr
*nla
)
1647 return ERR_PTR(-EINVAL
);
1649 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1652 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1653 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
},
1654 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1655 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1656 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1657 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1658 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1659 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
1660 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
1661 .len
= NFT_USERDATA_MAXLEN
},
1664 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
1665 u32 portid
, u32 seq
, int event
,
1666 u32 flags
, int family
,
1667 const struct nft_table
*table
,
1668 const struct nft_chain
*chain
,
1669 const struct nft_rule
*rule
)
1671 struct nlmsghdr
*nlh
;
1672 struct nfgenmsg
*nfmsg
;
1673 const struct nft_expr
*expr
, *next
;
1674 struct nlattr
*list
;
1675 const struct nft_rule
*prule
;
1676 int type
= event
| NFNL_SUBSYS_NFTABLES
<< 8;
1678 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
),
1681 goto nla_put_failure
;
1683 nfmsg
= nlmsg_data(nlh
);
1684 nfmsg
->nfgen_family
= family
;
1685 nfmsg
->version
= NFNETLINK_V0
;
1686 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1688 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1689 goto nla_put_failure
;
1690 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1691 goto nla_put_failure
;
1692 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
)))
1693 goto nla_put_failure
;
1695 if ((event
!= NFT_MSG_DELRULE
) && (rule
->list
.prev
!= &chain
->rules
)) {
1696 prule
= list_entry(rule
->list
.prev
, struct nft_rule
, list
);
1697 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
1698 cpu_to_be64(prule
->handle
)))
1699 goto nla_put_failure
;
1702 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1704 goto nla_put_failure
;
1705 nft_rule_for_each_expr(expr
, next
, rule
) {
1706 struct nlattr
*elem
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
1708 goto nla_put_failure
;
1709 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1710 goto nla_put_failure
;
1711 nla_nest_end(skb
, elem
);
1713 nla_nest_end(skb
, list
);
1716 struct nft_userdata
*udata
= nft_userdata(rule
);
1717 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
1719 goto nla_put_failure
;
1722 nlmsg_end(skb
, nlh
);
1726 nlmsg_trim(skb
, nlh
);
1730 static int nf_tables_rule_notify(const struct nft_ctx
*ctx
,
1731 const struct nft_rule
*rule
,
1734 struct sk_buff
*skb
;
1738 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1742 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1746 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1747 event
, 0, ctx
->afi
->family
, ctx
->table
,
1754 err
= nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1755 ctx
->report
, GFP_KERNEL
);
1758 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1764 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1765 struct netlink_callback
*cb
)
1767 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1768 const struct nft_af_info
*afi
;
1769 const struct nft_table
*table
;
1770 const struct nft_chain
*chain
;
1771 const struct nft_rule
*rule
;
1772 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1773 struct net
*net
= sock_net(skb
->sk
);
1774 int family
= nfmsg
->nfgen_family
;
1777 cb
->seq
= net
->nft
.base_seq
;
1779 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1780 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1783 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1784 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1785 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
1786 if (!nft_rule_is_active(net
, rule
))
1791 memset(&cb
->args
[1], 0,
1792 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1793 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
1796 NLM_F_MULTI
| NLM_F_APPEND
,
1797 afi
->family
, table
, chain
, rule
) < 0)
1800 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1814 static int nf_tables_getrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1815 const struct nlmsghdr
*nlh
,
1816 const struct nlattr
* const nla
[])
1818 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1819 const struct nft_af_info
*afi
;
1820 const struct nft_table
*table
;
1821 const struct nft_chain
*chain
;
1822 const struct nft_rule
*rule
;
1823 struct sk_buff
*skb2
;
1824 struct net
*net
= sock_net(skb
->sk
);
1825 int family
= nfmsg
->nfgen_family
;
1828 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1829 struct netlink_dump_control c
= {
1830 .dump
= nf_tables_dump_rules
,
1832 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1835 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1837 return PTR_ERR(afi
);
1839 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1841 return PTR_ERR(table
);
1842 if (table
->flags
& NFT_TABLE_INACTIVE
)
1845 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1847 return PTR_ERR(chain
);
1848 if (chain
->flags
& NFT_CHAIN_INACTIVE
)
1851 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1853 return PTR_ERR(rule
);
1855 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1859 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1860 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
1861 family
, table
, chain
, rule
);
1865 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1872 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
1873 struct nft_rule
*rule
)
1875 struct nft_expr
*expr
;
1878 * Careful: some expressions might not be initialized in case this
1879 * is called on error from nf_tables_newrule().
1881 expr
= nft_expr_first(rule
);
1882 while (expr
->ops
&& expr
!= nft_expr_last(rule
)) {
1883 nf_tables_expr_destroy(ctx
, expr
);
1884 expr
= nft_expr_next(expr
);
1889 #define NFT_RULE_MAXEXPRS 128
1891 static struct nft_expr_info
*info
;
1893 static int nf_tables_newrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1894 const struct nlmsghdr
*nlh
,
1895 const struct nlattr
* const nla
[])
1897 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1898 struct nft_af_info
*afi
;
1899 struct net
*net
= sock_net(skb
->sk
);
1900 struct nft_table
*table
;
1901 struct nft_chain
*chain
;
1902 struct nft_rule
*rule
, *old_rule
= NULL
;
1903 struct nft_userdata
*udata
;
1904 struct nft_trans
*trans
= NULL
;
1905 struct nft_expr
*expr
;
1908 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
1911 u64 handle
, pos_handle
;
1913 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1915 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
1917 return PTR_ERR(afi
);
1919 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1921 return PTR_ERR(table
);
1923 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1925 return PTR_ERR(chain
);
1927 if (nla
[NFTA_RULE_HANDLE
]) {
1928 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
1929 rule
= __nf_tables_rule_lookup(chain
, handle
);
1931 return PTR_ERR(rule
);
1933 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1935 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1940 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1942 handle
= nf_tables_alloc_handle(table
);
1944 if (chain
->use
== UINT_MAX
)
1948 if (nla
[NFTA_RULE_POSITION
]) {
1949 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
1952 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
1953 old_rule
= __nf_tables_rule_lookup(chain
, pos_handle
);
1954 if (IS_ERR(old_rule
))
1955 return PTR_ERR(old_rule
);
1958 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1962 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
1963 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
1965 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
1967 if (n
== NFT_RULE_MAXEXPRS
)
1969 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
1972 size
+= info
[n
].ops
->size
;
1976 /* Check for overflow of dlen field */
1978 if (size
>= 1 << 12)
1981 if (nla
[NFTA_RULE_USERDATA
]) {
1982 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
1984 usize
= sizeof(struct nft_userdata
) + ulen
;
1988 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
1992 nft_rule_activate_next(net
, rule
);
1994 rule
->handle
= handle
;
1996 rule
->udata
= ulen
? 1 : 0;
1999 udata
= nft_userdata(rule
);
2000 udata
->len
= ulen
- 1;
2001 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
2004 expr
= nft_expr_first(rule
);
2005 for (i
= 0; i
< n
; i
++) {
2006 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
2010 expr
= nft_expr_next(expr
);
2013 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2014 if (nft_rule_is_active_next(net
, old_rule
)) {
2015 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_DELRULE
,
2017 if (trans
== NULL
) {
2021 nft_rule_deactivate_next(net
, old_rule
);
2023 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2028 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
2030 list_add_rcu(&rule
->list
, &old_rule
->list
);
2032 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
2035 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2037 list_add_rcu(&rule
->list
, &chain
->rules
);
2040 if (nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
) == NULL
) {
2048 list_del_rcu(&rule
->list
);
2050 nf_tables_rule_destroy(&ctx
, rule
);
2052 for (i
= 0; i
< n
; i
++) {
2053 if (info
[i
].ops
!= NULL
)
2054 module_put(info
[i
].ops
->type
->owner
);
2059 static int nf_tables_delrule(struct sock
*nlsk
, struct sk_buff
*skb
,
2060 const struct nlmsghdr
*nlh
,
2061 const struct nlattr
* const nla
[])
2063 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2064 struct nft_af_info
*afi
;
2065 struct net
*net
= sock_net(skb
->sk
);
2066 struct nft_table
*table
;
2067 struct nft_chain
*chain
= NULL
;
2068 struct nft_rule
*rule
;
2069 int family
= nfmsg
->nfgen_family
, err
= 0;
2072 afi
= nf_tables_afinfo_lookup(net
, family
, false);
2074 return PTR_ERR(afi
);
2076 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
2078 return PTR_ERR(table
);
2079 if (table
->flags
& NFT_TABLE_INACTIVE
)
2082 if (nla
[NFTA_RULE_CHAIN
]) {
2083 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
2085 return PTR_ERR(chain
);
2088 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
2091 if (nla
[NFTA_RULE_HANDLE
]) {
2092 rule
= nf_tables_rule_lookup(chain
,
2093 nla
[NFTA_RULE_HANDLE
]);
2095 return PTR_ERR(rule
);
2097 err
= nft_delrule(&ctx
, rule
);
2099 err
= nft_delrule_by_chain(&ctx
);
2102 list_for_each_entry(chain
, &table
->chains
, list
) {
2104 err
= nft_delrule_by_chain(&ctx
);
2117 static LIST_HEAD(nf_tables_set_ops
);
2119 int nft_register_set(struct nft_set_ops
*ops
)
2121 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2122 list_add_tail_rcu(&ops
->list
, &nf_tables_set_ops
);
2123 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2126 EXPORT_SYMBOL_GPL(nft_register_set
);
2128 void nft_unregister_set(struct nft_set_ops
*ops
)
2130 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2131 list_del_rcu(&ops
->list
);
2132 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2134 EXPORT_SYMBOL_GPL(nft_unregister_set
);
2137 * Select a set implementation based on the data characteristics and the
2138 * given policy. The total memory use might not be known if no size is
2139 * given, in that case the amount of memory per element is used.
2141 static const struct nft_set_ops
*
2142 nft_select_set_ops(const struct nlattr
* const nla
[],
2143 const struct nft_set_desc
*desc
,
2144 enum nft_set_policies policy
)
2146 const struct nft_set_ops
*ops
, *bops
;
2147 struct nft_set_estimate est
, best
;
2150 #ifdef CONFIG_MODULES
2151 if (list_empty(&nf_tables_set_ops
)) {
2152 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2153 request_module("nft-set");
2154 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2155 if (!list_empty(&nf_tables_set_ops
))
2156 return ERR_PTR(-EAGAIN
);
2160 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2161 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2162 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
| NFT_SET_TIMEOUT
;
2169 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
2170 if ((ops
->features
& features
) != features
)
2172 if (!ops
->estimate(desc
, features
, &est
))
2176 case NFT_SET_POL_PERFORMANCE
:
2177 if (est
.class < best
.class)
2179 if (est
.class == best
.class && est
.size
< best
.size
)
2182 case NFT_SET_POL_MEMORY
:
2183 if (est
.size
< best
.size
)
2185 if (est
.size
== best
.size
&& est
.class < best
.class)
2192 if (!try_module_get(ops
->owner
))
2195 module_put(bops
->owner
);
2204 return ERR_PTR(-EOPNOTSUPP
);
2207 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
2208 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
},
2209 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
2210 .len
= IFNAMSIZ
- 1 },
2211 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
2212 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
2213 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
2214 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
2215 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
2216 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
2217 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
2218 [NFTA_SET_ID
] = { .type
= NLA_U32
},
2219 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
2220 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
2223 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
2224 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
2227 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
,
2228 const struct sk_buff
*skb
,
2229 const struct nlmsghdr
*nlh
,
2230 const struct nlattr
* const nla
[])
2232 struct net
*net
= sock_net(skb
->sk
);
2233 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2234 struct nft_af_info
*afi
= NULL
;
2235 struct nft_table
*table
= NULL
;
2237 if (nfmsg
->nfgen_family
!= NFPROTO_UNSPEC
) {
2238 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2240 return PTR_ERR(afi
);
2243 if (nla
[NFTA_SET_TABLE
] != NULL
) {
2245 return -EAFNOSUPPORT
;
2247 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2249 return PTR_ERR(table
);
2250 if (table
->flags
& NFT_TABLE_INACTIVE
)
2254 nft_ctx_init(ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
2258 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
2259 const struct nlattr
*nla
)
2261 struct nft_set
*set
;
2264 return ERR_PTR(-EINVAL
);
2266 list_for_each_entry(set
, &table
->sets
, list
) {
2267 if (!nla_strcmp(nla
, set
->name
))
2270 return ERR_PTR(-ENOENT
);
2273 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
2274 const struct nlattr
*nla
)
2276 struct nft_trans
*trans
;
2277 u32 id
= ntohl(nla_get_be32(nla
));
2279 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2280 if (trans
->msg_type
== NFT_MSG_NEWSET
&&
2281 id
== nft_trans_set_id(trans
))
2282 return nft_trans_set(trans
);
2284 return ERR_PTR(-ENOENT
);
2287 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
2290 const struct nft_set
*i
;
2292 unsigned long *inuse
;
2293 unsigned int n
= 0, min
= 0;
2295 p
= strnchr(name
, IFNAMSIZ
, '%');
2297 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
2300 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
2304 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2307 if (!sscanf(i
->name
, name
, &tmp
))
2309 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
2312 set_bit(tmp
- min
, inuse
);
2315 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
2316 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
2317 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
2318 memset(inuse
, 0, PAGE_SIZE
);
2321 free_page((unsigned long)inuse
);
2324 snprintf(set
->name
, sizeof(set
->name
), name
, min
+ n
);
2325 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2326 if (!strcmp(set
->name
, i
->name
))
2332 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
2333 const struct nft_set
*set
, u16 event
, u16 flags
)
2335 struct nfgenmsg
*nfmsg
;
2336 struct nlmsghdr
*nlh
;
2337 struct nlattr
*desc
;
2338 u32 portid
= ctx
->portid
;
2341 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2342 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2345 goto nla_put_failure
;
2347 nfmsg
= nlmsg_data(nlh
);
2348 nfmsg
->nfgen_family
= ctx
->afi
->family
;
2349 nfmsg
->version
= NFNETLINK_V0
;
2350 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
2352 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
2353 goto nla_put_failure
;
2354 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
2355 goto nla_put_failure
;
2356 if (set
->flags
!= 0)
2357 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
2358 goto nla_put_failure
;
2360 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
2361 goto nla_put_failure
;
2362 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
2363 goto nla_put_failure
;
2364 if (set
->flags
& NFT_SET_MAP
) {
2365 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
2366 goto nla_put_failure
;
2367 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
2368 goto nla_put_failure
;
2372 nla_put_be64(skb
, NFTA_SET_TIMEOUT
, cpu_to_be64(set
->timeout
)))
2373 goto nla_put_failure
;
2375 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
2376 goto nla_put_failure
;
2378 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
2379 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
2380 goto nla_put_failure
;
2383 desc
= nla_nest_start(skb
, NFTA_SET_DESC
);
2385 goto nla_put_failure
;
2387 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
2388 goto nla_put_failure
;
2389 nla_nest_end(skb
, desc
);
2391 nlmsg_end(skb
, nlh
);
2395 nlmsg_trim(skb
, nlh
);
2399 static int nf_tables_set_notify(const struct nft_ctx
*ctx
,
2400 const struct nft_set
*set
,
2401 int event
, gfp_t gfp_flags
)
2403 struct sk_buff
*skb
;
2404 u32 portid
= ctx
->portid
;
2408 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2412 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
2416 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
2422 err
= nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
,
2423 ctx
->report
, gfp_flags
);
2426 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, err
);
2430 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2432 const struct nft_set
*set
;
2433 unsigned int idx
, s_idx
= cb
->args
[0];
2434 struct nft_af_info
*afi
;
2435 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2436 struct net
*net
= sock_net(skb
->sk
);
2437 int cur_family
= cb
->args
[3];
2438 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
2444 cb
->seq
= net
->nft
.base_seq
;
2446 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
2447 if (ctx
->afi
&& ctx
->afi
!= afi
)
2451 if (afi
->family
!= cur_family
)
2456 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
2457 if (ctx
->table
&& ctx
->table
!= table
)
2461 if (cur_table
!= table
)
2467 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
2472 ctx_set
.table
= table
;
2474 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
2478 cb
->args
[2] = (unsigned long) table
;
2479 cb
->args
[3] = afi
->family
;
2482 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2496 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
2502 static int nf_tables_getset(struct sock
*nlsk
, struct sk_buff
*skb
,
2503 const struct nlmsghdr
*nlh
,
2504 const struct nlattr
* const nla
[])
2506 const struct nft_set
*set
;
2508 struct sk_buff
*skb2
;
2509 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2512 /* Verify existence before starting dump */
2513 err
= nft_ctx_init_from_setattr(&ctx
, skb
, nlh
, nla
);
2517 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2518 struct netlink_dump_control c
= {
2519 .dump
= nf_tables_dump_sets
,
2520 .done
= nf_tables_dump_sets_done
,
2522 struct nft_ctx
*ctx_dump
;
2524 ctx_dump
= kmalloc(sizeof(*ctx_dump
), GFP_KERNEL
);
2525 if (ctx_dump
== NULL
)
2531 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2534 /* Only accept unspec with dump */
2535 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2536 return -EAFNOSUPPORT
;
2538 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2540 return PTR_ERR(set
);
2541 if (set
->flags
& NFT_SET_INACTIVE
)
2544 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2548 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
2552 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2559 static int nf_tables_set_desc_parse(const struct nft_ctx
*ctx
,
2560 struct nft_set_desc
*desc
,
2561 const struct nlattr
*nla
)
2563 struct nlattr
*da
[NFTA_SET_DESC_MAX
+ 1];
2566 err
= nla_parse_nested(da
, NFTA_SET_DESC_MAX
, nla
, nft_set_desc_policy
);
2570 if (da
[NFTA_SET_DESC_SIZE
] != NULL
)
2571 desc
->size
= ntohl(nla_get_be32(da
[NFTA_SET_DESC_SIZE
]));
2576 static int nf_tables_newset(struct sock
*nlsk
, struct sk_buff
*skb
,
2577 const struct nlmsghdr
*nlh
,
2578 const struct nlattr
* const nla
[])
2580 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2581 const struct nft_set_ops
*ops
;
2582 struct nft_af_info
*afi
;
2583 struct net
*net
= sock_net(skb
->sk
);
2584 struct nft_table
*table
;
2585 struct nft_set
*set
;
2587 char name
[IFNAMSIZ
];
2591 u32 ktype
, dtype
, flags
, policy
, gc_int
;
2592 struct nft_set_desc desc
;
2595 if (nla
[NFTA_SET_TABLE
] == NULL
||
2596 nla
[NFTA_SET_NAME
] == NULL
||
2597 nla
[NFTA_SET_KEY_LEN
] == NULL
||
2598 nla
[NFTA_SET_ID
] == NULL
)
2601 memset(&desc
, 0, sizeof(desc
));
2603 ktype
= NFT_DATA_VALUE
;
2604 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
2605 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
2606 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
2610 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
2611 if (desc
.klen
== 0 || desc
.klen
> FIELD_SIZEOF(struct nft_data
, data
))
2615 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2616 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2617 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
2618 NFT_SET_INTERVAL
| NFT_SET_MAP
|
2624 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
2625 if (!(flags
& NFT_SET_MAP
))
2628 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
2629 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
2630 dtype
!= NFT_DATA_VERDICT
)
2633 if (dtype
!= NFT_DATA_VERDICT
) {
2634 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
2636 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
2637 if (desc
.dlen
== 0 ||
2638 desc
.dlen
> FIELD_SIZEOF(struct nft_data
, data
))
2641 desc
.dlen
= sizeof(struct nft_data
);
2642 } else if (flags
& NFT_SET_MAP
)
2646 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
2647 if (!(flags
& NFT_SET_TIMEOUT
))
2649 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_TIMEOUT
]));
2652 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
2653 if (!(flags
& NFT_SET_TIMEOUT
))
2655 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
2658 policy
= NFT_SET_POL_PERFORMANCE
;
2659 if (nla
[NFTA_SET_POLICY
] != NULL
)
2660 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
2662 if (nla
[NFTA_SET_DESC
] != NULL
) {
2663 err
= nf_tables_set_desc_parse(&ctx
, &desc
, nla
[NFTA_SET_DESC
]);
2668 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2670 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2672 return PTR_ERR(afi
);
2674 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2676 return PTR_ERR(table
);
2678 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
2680 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
]);
2682 if (PTR_ERR(set
) != -ENOENT
)
2683 return PTR_ERR(set
);
2688 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2690 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2695 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2698 ops
= nft_select_set_ops(nla
, &desc
, policy
);
2700 return PTR_ERR(ops
);
2703 if (ops
->privsize
!= NULL
)
2704 size
= ops
->privsize(nla
);
2707 set
= kzalloc(sizeof(*set
) + size
, GFP_KERNEL
);
2711 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
2712 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
2716 INIT_LIST_HEAD(&set
->bindings
);
2717 write_pnet(&set
->pnet
, net
);
2720 set
->klen
= desc
.klen
;
2722 set
->dlen
= desc
.dlen
;
2724 set
->size
= desc
.size
;
2725 set
->policy
= policy
;
2726 set
->timeout
= timeout
;
2727 set
->gc_int
= gc_int
;
2729 err
= ops
->init(set
, &desc
, nla
);
2733 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
2737 list_add_tail_rcu(&set
->list
, &table
->sets
);
2744 module_put(ops
->owner
);
2748 static void nft_set_destroy(struct nft_set
*set
)
2750 set
->ops
->destroy(set
);
2751 module_put(set
->ops
->owner
);
2755 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
2757 list_del_rcu(&set
->list
);
2758 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
, GFP_ATOMIC
);
2759 nft_set_destroy(set
);
2762 static int nf_tables_delset(struct sock
*nlsk
, struct sk_buff
*skb
,
2763 const struct nlmsghdr
*nlh
,
2764 const struct nlattr
* const nla
[])
2766 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2767 struct nft_set
*set
;
2771 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2772 return -EAFNOSUPPORT
;
2773 if (nla
[NFTA_SET_TABLE
] == NULL
)
2776 err
= nft_ctx_init_from_setattr(&ctx
, skb
, nlh
, nla
);
2780 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2782 return PTR_ERR(set
);
2783 if (set
->flags
& NFT_SET_INACTIVE
)
2785 if (!list_empty(&set
->bindings
))
2788 return nft_delset(&ctx
, set
);
2791 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
2792 const struct nft_set
*set
,
2793 const struct nft_set_iter
*iter
,
2794 const struct nft_set_elem
*elem
)
2796 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
2797 enum nft_registers dreg
;
2799 dreg
= nft_type_to_reg(set
->dtype
);
2800 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
2801 set
->dtype
== NFT_DATA_VERDICT
?
2802 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2806 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2807 struct nft_set_binding
*binding
)
2809 struct nft_set_binding
*i
;
2810 struct nft_set_iter iter
;
2812 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2815 if (binding
->flags
& NFT_SET_MAP
) {
2816 /* If the set is already bound to the same chain all
2817 * jumps are already validated for that chain.
2819 list_for_each_entry(i
, &set
->bindings
, list
) {
2820 if (binding
->flags
& NFT_SET_MAP
&&
2821 i
->chain
== binding
->chain
)
2828 iter
.fn
= nf_tables_bind_check_setelem
;
2830 set
->ops
->walk(ctx
, set
, &iter
);
2832 /* Destroy anonymous sets if binding fails */
2833 if (set
->flags
& NFT_SET_ANONYMOUS
)
2834 nf_tables_set_destroy(ctx
, set
);
2840 binding
->chain
= ctx
->chain
;
2841 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
2845 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2846 struct nft_set_binding
*binding
)
2848 list_del_rcu(&binding
->list
);
2850 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
&&
2851 !(set
->flags
& NFT_SET_INACTIVE
))
2852 nf_tables_set_destroy(ctx
, set
);
2855 const struct nft_set_ext_type nft_set_ext_types
[] = {
2856 [NFT_SET_EXT_KEY
] = {
2857 .len
= sizeof(struct nft_data
),
2858 .align
= __alignof__(struct nft_data
),
2860 [NFT_SET_EXT_DATA
] = {
2861 .len
= sizeof(struct nft_data
),
2862 .align
= __alignof__(struct nft_data
),
2864 [NFT_SET_EXT_FLAGS
] = {
2866 .align
= __alignof__(u8
),
2868 [NFT_SET_EXT_TIMEOUT
] = {
2870 .align
= __alignof__(u64
),
2872 [NFT_SET_EXT_EXPIRATION
] = {
2873 .len
= sizeof(unsigned long),
2874 .align
= __alignof__(unsigned long),
2876 [NFT_SET_EXT_USERDATA
] = {
2877 .len
= sizeof(struct nft_userdata
),
2878 .align
= __alignof__(struct nft_userdata
),
2881 EXPORT_SYMBOL_GPL(nft_set_ext_types
);
2887 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
2888 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
2889 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
2890 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
2891 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
2892 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
2893 .len
= NFT_USERDATA_MAXLEN
},
2896 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
2897 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
},
2898 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
},
2899 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
2900 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
2903 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
,
2904 const struct sk_buff
*skb
,
2905 const struct nlmsghdr
*nlh
,
2906 const struct nlattr
* const nla
[],
2909 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2910 struct nft_af_info
*afi
;
2911 struct nft_table
*table
;
2912 struct net
*net
= sock_net(skb
->sk
);
2914 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2916 return PTR_ERR(afi
);
2918 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
2920 return PTR_ERR(table
);
2921 if (!trans
&& (table
->flags
& NFT_TABLE_INACTIVE
))
2924 nft_ctx_init(ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
2928 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
2929 const struct nft_set
*set
,
2930 const struct nft_set_elem
*elem
)
2932 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
2933 unsigned char *b
= skb_tail_pointer(skb
);
2934 struct nlattr
*nest
;
2936 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
2938 goto nla_put_failure
;
2940 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
2941 NFT_DATA_VALUE
, set
->klen
) < 0)
2942 goto nla_put_failure
;
2944 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
2945 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
2946 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2948 goto nla_put_failure
;
2950 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
2951 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
2952 htonl(*nft_set_ext_flags(ext
))))
2953 goto nla_put_failure
;
2955 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
2956 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
2957 cpu_to_be64(*nft_set_ext_timeout(ext
))))
2958 goto nla_put_failure
;
2960 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
2961 unsigned long expires
, now
= jiffies
;
2963 expires
= *nft_set_ext_expiration(ext
);
2964 if (time_before(now
, expires
))
2969 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
2970 cpu_to_be64(jiffies_to_msecs(expires
))))
2971 goto nla_put_failure
;
2974 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
2975 struct nft_userdata
*udata
;
2977 udata
= nft_set_ext_userdata(ext
);
2978 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
2979 udata
->len
+ 1, udata
->data
))
2980 goto nla_put_failure
;
2983 nla_nest_end(skb
, nest
);
2991 struct nft_set_dump_args
{
2992 const struct netlink_callback
*cb
;
2993 struct nft_set_iter iter
;
2994 struct sk_buff
*skb
;
2997 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
2998 const struct nft_set
*set
,
2999 const struct nft_set_iter
*iter
,
3000 const struct nft_set_elem
*elem
)
3002 struct nft_set_dump_args
*args
;
3004 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
3005 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
3008 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3010 const struct nft_set
*set
;
3011 struct nft_set_dump_args args
;
3013 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
3014 struct nfgenmsg
*nfmsg
;
3015 struct nlmsghdr
*nlh
;
3016 struct nlattr
*nest
;
3020 err
= nlmsg_parse(cb
->nlh
, sizeof(struct nfgenmsg
), nla
,
3021 NFTA_SET_ELEM_LIST_MAX
, nft_set_elem_list_policy
);
3025 err
= nft_ctx_init_from_elemattr(&ctx
, cb
->skb
, cb
->nlh
, (void *)nla
,
3030 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3032 return PTR_ERR(set
);
3033 if (set
->flags
& NFT_SET_INACTIVE
)
3036 event
= NFT_MSG_NEWSETELEM
;
3037 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3038 portid
= NETLINK_CB(cb
->skb
).portid
;
3039 seq
= cb
->nlh
->nlmsg_seq
;
3041 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3044 goto nla_put_failure
;
3046 nfmsg
= nlmsg_data(nlh
);
3047 nfmsg
->nfgen_family
= ctx
.afi
->family
;
3048 nfmsg
->version
= NFNETLINK_V0
;
3049 nfmsg
->res_id
= htons(ctx
.net
->nft
.base_seq
& 0xffff);
3051 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
3052 goto nla_put_failure
;
3053 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
3054 goto nla_put_failure
;
3056 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3058 goto nla_put_failure
;
3062 args
.iter
.skip
= cb
->args
[0];
3063 args
.iter
.count
= 0;
3065 args
.iter
.fn
= nf_tables_dump_setelem
;
3066 set
->ops
->walk(&ctx
, set
, &args
.iter
);
3068 nla_nest_end(skb
, nest
);
3069 nlmsg_end(skb
, nlh
);
3071 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
3072 return args
.iter
.err
;
3073 if (args
.iter
.count
== cb
->args
[0])
3076 cb
->args
[0] = args
.iter
.count
;
3083 static int nf_tables_getsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
3084 const struct nlmsghdr
*nlh
,
3085 const struct nlattr
* const nla
[])
3087 const struct nft_set
*set
;
3091 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
, false);
3095 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3097 return PTR_ERR(set
);
3098 if (set
->flags
& NFT_SET_INACTIVE
)
3101 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3102 struct netlink_dump_control c
= {
3103 .dump
= nf_tables_dump_set
,
3105 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
3110 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
3111 const struct nft_ctx
*ctx
, u32 seq
,
3112 u32 portid
, int event
, u16 flags
,
3113 const struct nft_set
*set
,
3114 const struct nft_set_elem
*elem
)
3116 struct nfgenmsg
*nfmsg
;
3117 struct nlmsghdr
*nlh
;
3118 struct nlattr
*nest
;
3121 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
3122 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3125 goto nla_put_failure
;
3127 nfmsg
= nlmsg_data(nlh
);
3128 nfmsg
->nfgen_family
= ctx
->afi
->family
;
3129 nfmsg
->version
= NFNETLINK_V0
;
3130 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3132 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3133 goto nla_put_failure
;
3134 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3135 goto nla_put_failure
;
3137 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3139 goto nla_put_failure
;
3141 err
= nf_tables_fill_setelem(skb
, set
, elem
);
3143 goto nla_put_failure
;
3145 nla_nest_end(skb
, nest
);
3147 nlmsg_end(skb
, nlh
);
3151 nlmsg_trim(skb
, nlh
);
3155 static int nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
3156 const struct nft_set
*set
,
3157 const struct nft_set_elem
*elem
,
3158 int event
, u16 flags
)
3160 struct net
*net
= ctx
->net
;
3161 u32 portid
= ctx
->portid
;
3162 struct sk_buff
*skb
;
3165 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3169 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3173 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
3180 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
3184 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
3188 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
3190 struct nft_set
*set
)
3192 struct nft_trans
*trans
;
3194 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
3198 nft_trans_elem_set(trans
) = set
;
3202 void *nft_set_elem_init(const struct nft_set
*set
,
3203 const struct nft_set_ext_tmpl
*tmpl
,
3204 const struct nft_data
*key
,
3205 const struct nft_data
*data
,
3206 u64 timeout
, gfp_t gfp
)
3208 struct nft_set_ext
*ext
;
3211 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
3215 ext
= nft_set_elem_ext(set
, elem
);
3216 nft_set_ext_init(ext
, tmpl
);
3218 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
3219 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3220 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
3221 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
))
3222 *nft_set_ext_expiration(ext
) =
3223 jiffies
+ msecs_to_jiffies(timeout
);
3224 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
3225 *nft_set_ext_timeout(ext
) = timeout
;
3230 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
)
3232 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
3234 nft_data_uninit(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
3235 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3236 nft_data_uninit(nft_set_ext_data(ext
), set
->dtype
);
3240 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
3242 static int nft_add_set_elem(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 d1
, d2
;
3247 struct nft_set_ext_tmpl tmpl
;
3248 struct nft_set_ext
*ext
;
3249 struct nft_set_elem elem
;
3250 struct nft_set_binding
*binding
;
3251 struct nft_userdata
*udata
;
3252 struct nft_data data
;
3253 enum nft_registers dreg
;
3254 struct nft_trans
*trans
;
3260 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3261 nft_set_elem_policy
);
3265 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3268 nft_set_ext_prepare(&tmpl
);
3271 if (nla
[NFTA_SET_ELEM_FLAGS
] != NULL
) {
3272 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_ELEM_FLAGS
]));
3273 if (flags
& ~NFT_SET_ELEM_INTERVAL_END
)
3275 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
3276 flags
& NFT_SET_ELEM_INTERVAL_END
)
3279 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3282 if (set
->flags
& NFT_SET_MAP
) {
3283 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
3284 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
3286 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
3287 flags
& NFT_SET_ELEM_INTERVAL_END
)
3290 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3295 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
3296 if (!(set
->flags
& NFT_SET_TIMEOUT
))
3298 timeout
= be64_to_cpu(nla_get_be64(nla
[NFTA_SET_ELEM_TIMEOUT
]));
3299 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
3300 timeout
= set
->timeout
;
3303 err
= nft_data_init(ctx
, &elem
.key
, &d1
, nla
[NFTA_SET_ELEM_KEY
]);
3307 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
3310 nft_set_ext_add(&tmpl
, NFT_SET_EXT_KEY
);
3312 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
3313 if (timeout
!= set
->timeout
)
3314 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
3317 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
3318 err
= nft_data_init(ctx
, &data
, &d2
, nla
[NFTA_SET_ELEM_DATA
]);
3323 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
3326 dreg
= nft_type_to_reg(set
->dtype
);
3327 list_for_each_entry(binding
, &set
->bindings
, list
) {
3328 struct nft_ctx bind_ctx
= {
3330 .table
= ctx
->table
,
3331 .chain
= (struct nft_chain
*)binding
->chain
,
3334 if (!(binding
->flags
& NFT_SET_MAP
))
3337 err
= nft_validate_register_store(&bind_ctx
, dreg
,
3344 nft_set_ext_add(&tmpl
, NFT_SET_EXT_DATA
);
3347 /* The full maximum length of userdata can exceed the maximum
3348 * offset value (U8_MAX) for following extensions, therefor it
3349 * must be the last extension added.
3352 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
3353 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
3355 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
3360 elem
.priv
= nft_set_elem_init(set
, &tmpl
, &elem
.key
, &data
,
3361 timeout
, GFP_KERNEL
);
3362 if (elem
.priv
== NULL
)
3365 ext
= nft_set_elem_ext(set
, elem
.priv
);
3367 *nft_set_ext_flags(ext
) = flags
;
3369 udata
= nft_set_ext_userdata(ext
);
3370 udata
->len
= ulen
- 1;
3371 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
3374 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
3378 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
3379 err
= set
->ops
->insert(set
, &elem
);
3383 nft_trans_elem(trans
) = elem
;
3384 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3392 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3393 nft_data_uninit(&data
, d2
.type
);
3395 nft_data_uninit(&elem
.key
, d1
.type
);
3400 static int nf_tables_newsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
3401 const struct nlmsghdr
*nlh
,
3402 const struct nlattr
* const nla
[])
3404 struct net
*net
= sock_net(skb
->sk
);
3405 const struct nlattr
*attr
;
3406 struct nft_set
*set
;
3410 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3413 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
, true);
3417 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3419 if (nla
[NFTA_SET_ELEM_LIST_SET_ID
]) {
3420 set
= nf_tables_set_lookup_byid(net
,
3421 nla
[NFTA_SET_ELEM_LIST_SET_ID
]);
3424 return PTR_ERR(set
);
3427 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3430 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3432 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
))
3435 err
= nft_add_set_elem(&ctx
, set
, attr
);
3437 atomic_dec(&set
->nelems
);
3444 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3445 const struct nlattr
*attr
)
3447 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3448 struct nft_data_desc desc
;
3449 struct nft_set_elem elem
;
3450 struct nft_trans
*trans
;
3453 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3454 nft_set_elem_policy
);
3459 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3462 err
= nft_data_init(ctx
, &elem
.key
, &desc
, nla
[NFTA_SET_ELEM_KEY
]);
3467 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
3470 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
3471 if (trans
== NULL
) {
3476 elem
.priv
= set
->ops
->deactivate(set
, &elem
);
3477 if (elem
.priv
== NULL
) {
3482 nft_trans_elem(trans
) = elem
;
3483 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3489 nft_data_uninit(&elem
.key
, desc
.type
);
3494 static int nf_tables_delsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
3495 const struct nlmsghdr
*nlh
,
3496 const struct nlattr
* const nla
[])
3498 const struct nlattr
*attr
;
3499 struct nft_set
*set
;
3503 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3506 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
, false);
3510 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
3512 return PTR_ERR(set
);
3513 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3516 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3517 err
= nft_del_setelem(&ctx
, set
, attr
);
3526 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
3528 struct nft_set_gc_batch
*gcb
;
3531 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
3532 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
3533 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
]);
3536 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release
);
3538 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
3541 struct nft_set_gc_batch
*gcb
;
3543 gcb
= kzalloc(sizeof(*gcb
), gfp
);
3546 gcb
->head
.set
= set
;
3549 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc
);
3551 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
3552 u32 portid
, u32 seq
)
3554 struct nlmsghdr
*nlh
;
3555 struct nfgenmsg
*nfmsg
;
3556 int event
= (NFNL_SUBSYS_NFTABLES
<< 8) | NFT_MSG_NEWGEN
;
3558 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
3560 goto nla_put_failure
;
3562 nfmsg
= nlmsg_data(nlh
);
3563 nfmsg
->nfgen_family
= AF_UNSPEC
;
3564 nfmsg
->version
= NFNETLINK_V0
;
3565 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
3567 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)))
3568 goto nla_put_failure
;
3570 nlmsg_end(skb
, nlh
);
3574 nlmsg_trim(skb
, nlh
);
3578 static int nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
, int event
)
3580 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
3581 struct sk_buff
*skb2
;
3584 if (nlmsg_report(nlh
) &&
3585 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3589 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3593 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3600 err
= nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
,
3601 NFNLGRP_NFTABLES
, nlmsg_report(nlh
), GFP_KERNEL
);
3604 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
3610 static int nf_tables_getgen(struct sock
*nlsk
, struct sk_buff
*skb
,
3611 const struct nlmsghdr
*nlh
,
3612 const struct nlattr
* const nla
[])
3614 struct net
*net
= sock_net(skb
->sk
);
3615 struct sk_buff
*skb2
;
3618 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
3622 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
3627 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
3633 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
3634 [NFT_MSG_NEWTABLE
] = {
3635 .call_batch
= nf_tables_newtable
,
3636 .attr_count
= NFTA_TABLE_MAX
,
3637 .policy
= nft_table_policy
,
3639 [NFT_MSG_GETTABLE
] = {
3640 .call
= nf_tables_gettable
,
3641 .attr_count
= NFTA_TABLE_MAX
,
3642 .policy
= nft_table_policy
,
3644 [NFT_MSG_DELTABLE
] = {
3645 .call_batch
= nf_tables_deltable
,
3646 .attr_count
= NFTA_TABLE_MAX
,
3647 .policy
= nft_table_policy
,
3649 [NFT_MSG_NEWCHAIN
] = {
3650 .call_batch
= nf_tables_newchain
,
3651 .attr_count
= NFTA_CHAIN_MAX
,
3652 .policy
= nft_chain_policy
,
3654 [NFT_MSG_GETCHAIN
] = {
3655 .call
= nf_tables_getchain
,
3656 .attr_count
= NFTA_CHAIN_MAX
,
3657 .policy
= nft_chain_policy
,
3659 [NFT_MSG_DELCHAIN
] = {
3660 .call_batch
= nf_tables_delchain
,
3661 .attr_count
= NFTA_CHAIN_MAX
,
3662 .policy
= nft_chain_policy
,
3664 [NFT_MSG_NEWRULE
] = {
3665 .call_batch
= nf_tables_newrule
,
3666 .attr_count
= NFTA_RULE_MAX
,
3667 .policy
= nft_rule_policy
,
3669 [NFT_MSG_GETRULE
] = {
3670 .call
= nf_tables_getrule
,
3671 .attr_count
= NFTA_RULE_MAX
,
3672 .policy
= nft_rule_policy
,
3674 [NFT_MSG_DELRULE
] = {
3675 .call_batch
= nf_tables_delrule
,
3676 .attr_count
= NFTA_RULE_MAX
,
3677 .policy
= nft_rule_policy
,
3679 [NFT_MSG_NEWSET
] = {
3680 .call_batch
= nf_tables_newset
,
3681 .attr_count
= NFTA_SET_MAX
,
3682 .policy
= nft_set_policy
,
3684 [NFT_MSG_GETSET
] = {
3685 .call
= nf_tables_getset
,
3686 .attr_count
= NFTA_SET_MAX
,
3687 .policy
= nft_set_policy
,
3689 [NFT_MSG_DELSET
] = {
3690 .call_batch
= nf_tables_delset
,
3691 .attr_count
= NFTA_SET_MAX
,
3692 .policy
= nft_set_policy
,
3694 [NFT_MSG_NEWSETELEM
] = {
3695 .call_batch
= nf_tables_newsetelem
,
3696 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3697 .policy
= nft_set_elem_list_policy
,
3699 [NFT_MSG_GETSETELEM
] = {
3700 .call
= nf_tables_getsetelem
,
3701 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3702 .policy
= nft_set_elem_list_policy
,
3704 [NFT_MSG_DELSETELEM
] = {
3705 .call_batch
= nf_tables_delsetelem
,
3706 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
3707 .policy
= nft_set_elem_list_policy
,
3709 [NFT_MSG_GETGEN
] = {
3710 .call
= nf_tables_getgen
,
3714 static void nft_chain_commit_update(struct nft_trans
*trans
)
3716 struct nft_base_chain
*basechain
;
3718 if (nft_trans_chain_name(trans
)[0])
3719 strcpy(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
3721 if (!(trans
->ctx
.chain
->flags
& NFT_BASE_CHAIN
))
3724 basechain
= nft_base_chain(trans
->ctx
.chain
);
3725 nft_chain_stats_replace(basechain
, nft_trans_chain_stats(trans
));
3727 switch (nft_trans_chain_policy(trans
)) {
3730 basechain
->policy
= nft_trans_chain_policy(trans
);
3735 static void nf_tables_commit_release(struct nft_trans
*trans
)
3737 switch (trans
->msg_type
) {
3738 case NFT_MSG_DELTABLE
:
3739 nf_tables_table_destroy(&trans
->ctx
);
3741 case NFT_MSG_DELCHAIN
:
3742 nf_tables_chain_destroy(trans
->ctx
.chain
);
3744 case NFT_MSG_DELRULE
:
3745 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
3747 case NFT_MSG_DELSET
:
3748 nft_set_destroy(nft_trans_set(trans
));
3750 case NFT_MSG_DELSETELEM
:
3751 nft_set_elem_destroy(nft_trans_elem_set(trans
),
3752 nft_trans_elem(trans
).priv
);
3758 static int nf_tables_commit(struct sk_buff
*skb
)
3760 struct net
*net
= sock_net(skb
->sk
);
3761 struct nft_trans
*trans
, *next
;
3762 struct nft_trans_elem
*te
;
3764 /* Bump generation counter, invalidate any dump in progress */
3765 while (++net
->nft
.base_seq
== 0);
3767 /* A new generation has just started */
3768 net
->nft
.gencursor
= nft_gencursor_next(net
);
3770 /* Make sure all packets have left the previous generation before
3771 * purging old rules.
3775 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3776 switch (trans
->msg_type
) {
3777 case NFT_MSG_NEWTABLE
:
3778 if (nft_trans_table_update(trans
)) {
3779 if (!nft_trans_table_enable(trans
)) {
3780 nf_tables_table_disable(trans
->ctx
.afi
,
3782 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
3785 trans
->ctx
.table
->flags
&= ~NFT_TABLE_INACTIVE
;
3787 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
3788 nft_trans_destroy(trans
);
3790 case NFT_MSG_DELTABLE
:
3791 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
3793 case NFT_MSG_NEWCHAIN
:
3794 if (nft_trans_chain_update(trans
))
3795 nft_chain_commit_update(trans
);
3797 trans
->ctx
.chain
->flags
&= ~NFT_CHAIN_INACTIVE
;
3799 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
3800 nft_trans_destroy(trans
);
3802 case NFT_MSG_DELCHAIN
:
3803 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
3804 nf_tables_unregister_hooks(trans
->ctx
.table
,
3806 trans
->ctx
.afi
->nops
);
3808 case NFT_MSG_NEWRULE
:
3809 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
3810 nf_tables_rule_notify(&trans
->ctx
,
3811 nft_trans_rule(trans
),
3813 nft_trans_destroy(trans
);
3815 case NFT_MSG_DELRULE
:
3816 list_del_rcu(&nft_trans_rule(trans
)->list
);
3817 nf_tables_rule_notify(&trans
->ctx
,
3818 nft_trans_rule(trans
),
3821 case NFT_MSG_NEWSET
:
3822 nft_trans_set(trans
)->flags
&= ~NFT_SET_INACTIVE
;
3823 /* This avoids hitting -EBUSY when deleting the table
3824 * from the transaction.
3826 if (nft_trans_set(trans
)->flags
& NFT_SET_ANONYMOUS
&&
3827 !list_empty(&nft_trans_set(trans
)->bindings
))
3828 trans
->ctx
.table
->use
--;
3830 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
3831 NFT_MSG_NEWSET
, GFP_KERNEL
);
3832 nft_trans_destroy(trans
);
3834 case NFT_MSG_DELSET
:
3835 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
3836 NFT_MSG_DELSET
, GFP_KERNEL
);
3838 case NFT_MSG_NEWSETELEM
:
3839 te
= (struct nft_trans_elem
*)trans
->data
;
3841 te
->set
->ops
->activate(te
->set
, &te
->elem
);
3842 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
3844 NFT_MSG_NEWSETELEM
, 0);
3845 nft_trans_destroy(trans
);
3847 case NFT_MSG_DELSETELEM
:
3848 te
= (struct nft_trans_elem
*)trans
->data
;
3850 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
3852 NFT_MSG_DELSETELEM
, 0);
3853 te
->set
->ops
->remove(te
->set
, &te
->elem
);
3854 atomic_dec(&te
->set
->nelems
);
3862 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3863 list_del(&trans
->list
);
3864 nf_tables_commit_release(trans
);
3867 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
3872 static void nf_tables_abort_release(struct nft_trans
*trans
)
3874 switch (trans
->msg_type
) {
3875 case NFT_MSG_NEWTABLE
:
3876 nf_tables_table_destroy(&trans
->ctx
);
3878 case NFT_MSG_NEWCHAIN
:
3879 nf_tables_chain_destroy(trans
->ctx
.chain
);
3881 case NFT_MSG_NEWRULE
:
3882 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
3884 case NFT_MSG_NEWSET
:
3885 nft_set_destroy(nft_trans_set(trans
));
3887 case NFT_MSG_NEWSETELEM
:
3888 nft_set_elem_destroy(nft_trans_elem_set(trans
),
3889 nft_trans_elem(trans
).priv
);
3895 static int nf_tables_abort(struct sk_buff
*skb
)
3897 struct net
*net
= sock_net(skb
->sk
);
3898 struct nft_trans
*trans
, *next
;
3899 struct nft_trans_elem
*te
;
3901 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
3902 switch (trans
->msg_type
) {
3903 case NFT_MSG_NEWTABLE
:
3904 if (nft_trans_table_update(trans
)) {
3905 if (nft_trans_table_enable(trans
)) {
3906 nf_tables_table_disable(trans
->ctx
.afi
,
3908 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
3910 nft_trans_destroy(trans
);
3912 list_del_rcu(&trans
->ctx
.table
->list
);
3915 case NFT_MSG_DELTABLE
:
3916 list_add_tail_rcu(&trans
->ctx
.table
->list
,
3917 &trans
->ctx
.afi
->tables
);
3918 nft_trans_destroy(trans
);
3920 case NFT_MSG_NEWCHAIN
:
3921 if (nft_trans_chain_update(trans
)) {
3922 free_percpu(nft_trans_chain_stats(trans
));
3924 nft_trans_destroy(trans
);
3926 trans
->ctx
.table
->use
--;
3927 list_del_rcu(&trans
->ctx
.chain
->list
);
3928 nf_tables_unregister_hooks(trans
->ctx
.table
,
3930 trans
->ctx
.afi
->nops
);
3933 case NFT_MSG_DELCHAIN
:
3934 trans
->ctx
.table
->use
++;
3935 list_add_tail_rcu(&trans
->ctx
.chain
->list
,
3936 &trans
->ctx
.table
->chains
);
3937 nft_trans_destroy(trans
);
3939 case NFT_MSG_NEWRULE
:
3940 trans
->ctx
.chain
->use
--;
3941 list_del_rcu(&nft_trans_rule(trans
)->list
);
3943 case NFT_MSG_DELRULE
:
3944 trans
->ctx
.chain
->use
++;
3945 nft_rule_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
3946 nft_trans_destroy(trans
);
3948 case NFT_MSG_NEWSET
:
3949 trans
->ctx
.table
->use
--;
3950 list_del_rcu(&nft_trans_set(trans
)->list
);
3952 case NFT_MSG_DELSET
:
3953 trans
->ctx
.table
->use
++;
3954 list_add_tail_rcu(&nft_trans_set(trans
)->list
,
3955 &trans
->ctx
.table
->sets
);
3956 nft_trans_destroy(trans
);
3958 case NFT_MSG_NEWSETELEM
:
3959 te
= (struct nft_trans_elem
*)trans
->data
;
3961 te
->set
->ops
->remove(te
->set
, &te
->elem
);
3962 atomic_dec(&te
->set
->nelems
);
3964 case NFT_MSG_DELSETELEM
:
3965 te
= (struct nft_trans_elem
*)trans
->data
;
3967 te
->set
->ops
->activate(te
->set
, &te
->elem
);
3970 nft_trans_destroy(trans
);
3977 list_for_each_entry_safe_reverse(trans
, next
,
3978 &net
->nft
.commit_list
, list
) {
3979 list_del(&trans
->list
);
3980 nf_tables_abort_release(trans
);
3986 static const struct nfnetlink_subsystem nf_tables_subsys
= {
3987 .name
= "nf_tables",
3988 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
3989 .cb_count
= NFT_MSG_MAX
,
3991 .commit
= nf_tables_commit
,
3992 .abort
= nf_tables_abort
,
3995 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
3996 enum nft_chain_type type
)
3998 const struct nft_base_chain
*basechain
;
4000 if (chain
->flags
& NFT_BASE_CHAIN
) {
4001 basechain
= nft_base_chain(chain
);
4002 if (basechain
->type
->type
!= type
)
4007 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
4009 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
4010 unsigned int hook_flags
)
4012 struct nft_base_chain
*basechain
;
4014 if (chain
->flags
& NFT_BASE_CHAIN
) {
4015 basechain
= nft_base_chain(chain
);
4017 if ((1 << basechain
->ops
[0].hooknum
) & hook_flags
)
4025 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
4028 * Loop detection - walk through the ruleset beginning at the destination chain
4029 * of a new jump until either the source chain is reached (loop) or all
4030 * reachable chains have been traversed.
4032 * The loop check is performed whenever a new jump verdict is added to an
4033 * expression or verdict map or a verdict map is bound to a new chain.
4036 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4037 const struct nft_chain
*chain
);
4039 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
4040 const struct nft_set
*set
,
4041 const struct nft_set_iter
*iter
,
4042 const struct nft_set_elem
*elem
)
4044 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
4045 const struct nft_data
*data
;
4047 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
4048 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
4051 data
= nft_set_ext_data(ext
);
4052 switch (data
->verdict
) {
4055 return nf_tables_check_loops(ctx
, data
->chain
);
4061 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
4062 const struct nft_chain
*chain
)
4064 const struct nft_rule
*rule
;
4065 const struct nft_expr
*expr
, *last
;
4066 const struct nft_set
*set
;
4067 struct nft_set_binding
*binding
;
4068 struct nft_set_iter iter
;
4070 if (ctx
->chain
== chain
)
4073 list_for_each_entry(rule
, &chain
->rules
, list
) {
4074 nft_rule_for_each_expr(expr
, last
, rule
) {
4075 const struct nft_data
*data
= NULL
;
4078 if (!expr
->ops
->validate
)
4081 err
= expr
->ops
->validate(ctx
, expr
, &data
);
4088 switch (data
->verdict
) {
4091 err
= nf_tables_check_loops(ctx
, data
->chain
);
4100 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
4101 if (!(set
->flags
& NFT_SET_MAP
) ||
4102 set
->dtype
!= NFT_DATA_VERDICT
)
4105 list_for_each_entry(binding
, &set
->bindings
, list
) {
4106 if (!(binding
->flags
& NFT_SET_MAP
) ||
4107 binding
->chain
!= chain
)
4113 iter
.fn
= nf_tables_loop_check_setelem
;
4115 set
->ops
->walk(ctx
, set
, &iter
);
4125 * nft_validate_register_load - validate a load from a register
4127 * @reg: the register number
4128 * @len: the length of the data
4130 * Validate that the input register is one of the general purpose
4131 * registers and that the length of the load is within the bounds.
4133 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
4135 if (reg
<= NFT_REG_VERDICT
)
4137 if (reg
> NFT_REG_MAX
)
4141 if (len
> FIELD_SIZEOF(struct nft_data
, data
))
4145 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
4148 * nft_validate_register_store - validate an expressions' register store
4150 * @ctx: context of the expression performing the load
4151 * @reg: the destination register number
4152 * @data: the data to load
4153 * @type: the data type
4154 * @len: the length of the data
4156 * Validate that a data load uses the appropriate data type for
4157 * the destination register and the length is within the bounds.
4158 * A value of NULL for the data means that its runtime gathered
4161 int nft_validate_register_store(const struct nft_ctx
*ctx
,
4162 enum nft_registers reg
,
4163 const struct nft_data
*data
,
4164 enum nft_data_types type
, unsigned int len
)
4169 case NFT_REG_VERDICT
:
4170 if (type
!= NFT_DATA_VERDICT
)
4174 (data
->verdict
== NFT_GOTO
|| data
->verdict
== NFT_JUMP
)) {
4175 err
= nf_tables_check_loops(ctx
, data
->chain
);
4179 if (ctx
->chain
->level
+ 1 > data
->chain
->level
) {
4180 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
4182 data
->chain
->level
= ctx
->chain
->level
+ 1;
4188 if (reg
< NFT_REG_1
)
4190 if (reg
> NFT_REG_MAX
)
4194 if (len
> FIELD_SIZEOF(struct nft_data
, data
))
4197 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
4202 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
4204 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
4205 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
4206 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
4207 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
4210 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
4211 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4213 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
4214 struct nft_chain
*chain
;
4217 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
);
4221 if (!tb
[NFTA_VERDICT_CODE
])
4223 data
->verdict
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
4225 switch (data
->verdict
) {
4227 switch (data
->verdict
& NF_VERDICT_MASK
) {
4239 desc
->len
= sizeof(data
->verdict
);
4243 if (!tb
[NFTA_VERDICT_CHAIN
])
4245 chain
= nf_tables_chain_lookup(ctx
->table
,
4246 tb
[NFTA_VERDICT_CHAIN
]);
4248 return PTR_ERR(chain
);
4249 if (chain
->flags
& NFT_BASE_CHAIN
)
4253 data
->chain
= chain
;
4254 desc
->len
= sizeof(data
);
4258 desc
->type
= NFT_DATA_VERDICT
;
4262 static void nft_verdict_uninit(const struct nft_data
*data
)
4264 switch (data
->verdict
) {
4272 static int nft_verdict_dump(struct sk_buff
*skb
, const struct nft_data
*data
)
4274 struct nlattr
*nest
;
4276 nest
= nla_nest_start(skb
, NFTA_DATA_VERDICT
);
4278 goto nla_put_failure
;
4280 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(data
->verdict
)))
4281 goto nla_put_failure
;
4283 switch (data
->verdict
) {
4286 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
, data
->chain
->name
))
4287 goto nla_put_failure
;
4289 nla_nest_end(skb
, nest
);
4296 static int nft_value_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
4297 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4304 if (len
> sizeof(data
->data
))
4307 nla_memcpy(data
->data
, nla
, sizeof(data
->data
));
4308 desc
->type
= NFT_DATA_VALUE
;
4313 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
4316 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
4319 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
4320 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
,
4321 .len
= FIELD_SIZEOF(struct nft_data
, data
) },
4322 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
4326 * nft_data_init - parse nf_tables data netlink attributes
4328 * @ctx: context of the expression using the data
4329 * @data: destination struct nft_data
4330 * @desc: data description
4331 * @nla: netlink attribute containing data
4333 * Parse the netlink data attributes and initialize a struct nft_data.
4334 * The type and length of data are returned in the data description.
4336 * The caller can indicate that it only wants to accept data of type
4337 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4339 int nft_data_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
4340 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
4342 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
4345 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
);
4349 if (tb
[NFTA_DATA_VALUE
])
4350 return nft_value_init(ctx
, data
, desc
, tb
[NFTA_DATA_VALUE
]);
4351 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
4352 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
4355 EXPORT_SYMBOL_GPL(nft_data_init
);
4358 * nft_data_uninit - release a nft_data item
4360 * @data: struct nft_data to release
4361 * @type: type of data
4363 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4364 * all others need to be released by calling this function.
4366 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
4369 case NFT_DATA_VALUE
:
4371 case NFT_DATA_VERDICT
:
4372 return nft_verdict_uninit(data
);
4377 EXPORT_SYMBOL_GPL(nft_data_uninit
);
4379 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
4380 enum nft_data_types type
, unsigned int len
)
4382 struct nlattr
*nest
;
4385 nest
= nla_nest_start(skb
, attr
);
4390 case NFT_DATA_VALUE
:
4391 err
= nft_value_dump(skb
, data
, len
);
4393 case NFT_DATA_VERDICT
:
4394 err
= nft_verdict_dump(skb
, data
);
4401 nla_nest_end(skb
, nest
);
4404 EXPORT_SYMBOL_GPL(nft_data_dump
);
4406 static int nf_tables_init_net(struct net
*net
)
4408 INIT_LIST_HEAD(&net
->nft
.af_info
);
4409 INIT_LIST_HEAD(&net
->nft
.commit_list
);
4410 net
->nft
.base_seq
= 1;
4414 static struct pernet_operations nf_tables_net_ops
= {
4415 .init
= nf_tables_init_net
,
4418 static int __init
nf_tables_module_init(void)
4422 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
4429 err
= nf_tables_core_module_init();
4433 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
4437 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4438 return register_pernet_subsys(&nf_tables_net_ops
);
4440 nf_tables_core_module_exit();
4447 static void __exit
nf_tables_module_exit(void)
4449 unregister_pernet_subsys(&nf_tables_net_ops
);
4450 nfnetlink_subsys_unregister(&nf_tables_subsys
);
4452 nf_tables_core_module_exit();
4456 module_init(nf_tables_module_init
);
4457 module_exit(nf_tables_module_exit
);
4459 MODULE_LICENSE("GPL");
4460 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4461 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);