netfilter: nf_tables: move ruleset validation state to table
Subsystems (2)
Florian Westphal
maintainercover netfilter: nf_tables: move ruleset validation state to table
At this time nf_tables will valiate all tables before committing the new rules.
Unfortunately this has two drawbacks:
1. Since addition of the transaction mutex pernet state gets written to outside of the locked section from the cleanup callback, this is wrong so do this cleanup directly after table has passed all checks.
2. We revalidate tables that saw no changes.
This can be avoided by keeping the validation state per table, not per net.
Florian Westphal (2):
netfilter: nf_tables: don't write table validation state without mutex
netfilter: nf_tables: make validation state per table
include/linux/netfilter/nfnetlink.h | 1 - include/net/netfilter/nf_tables.h | 3 +- net/netfilter/nf_tables_api.c | 44 +++++++++++++---------------- net/netfilter/nfnetlink.c | 2 -- 4 files changed, 21 insertions(+), 29 deletions(-)
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH 1/2 netfilter: nf_tables: don't write table validation state without mutex
The ->cleanup callback needs to be removed, this doesn't work anymore as the transaction mutex is already released in the ->abort function.
Just do it after a successful validation pass, this either happens from commit or abort phases where transaction mutex is held.
--- include/linux/netfilter/nfnetlink.h | 1 - net/netfilter/nf_tables_api.c | 8 ++------ net/netfilter/nfnetlink.c | 2 -- 3 files changed, 2 insertions(+), 9 deletions(-)
include/linux/netfilter/nfnetlink.h +0 -1
@@ -45,7 +45,6 @@ struct nfnetlink_subsystem {int (*commit)(struct net *net,struct sk_buff *skb);int (*abort)(struct net *net,struct sk_buff *skb,enum nfnl_abort_action action); -void (*cleanup)(struct net *net);bool (*valid_genid)(struct net *net,u32 genid); };
net/netfilter/nf_tables_api.c +2 -6
@@ -8592,6 +8592,8 @@ static int nf_tables_validate(struct net *net)if (nft_table_validate (net, table) <0 )return -EAGAIN ; } + +nft_validate_state_update (net,NFT_VALIDATE_SKIP );break ; }
@@ -9533,11 +9535,6 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)return 0 ; } -static void nf_tables_cleanup (struct net *net) -{ -nft_validate_state_update (net,NFT_VALIDATE_SKIP ); -} -static int nf_tables_abort (struct net *net,struct sk_buff *skb,enum nfnl_abort_action action) {
@@ -9571,7 +9568,6 @@ static const struct nfnetlink_subsystem nf_tables_subsys = { .cb = nf_tables_cb, .commit = nf_tables_commit, .abort = nf_tables_abort, - .cleanup = nf_tables_cleanup, .valid_genid = nf_tables_valid_genid, .owner = THIS_MODULE, };
net/netfilter/nfnetlink.c +0 -2
@@ -590,8 +590,6 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, goto replay_abort; } } -if (ss->cleanup) - ss->cleanup (net);nfnl_err_deliver (&err_list, oskb);kfree_skb (skb);
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH 2/2 netfilter: nf_tables: make validation state per table
In reply to Florian WestphalWe only need to validate tables that saw changes in the current transaction.
The existing code revalidates all tables, but this isn't needed as cross-table jumps are not allowed (chains have table scope).
--- include/net/netfilter/nf_tables.h | 3 ++- net/netfilter/nf_tables_api.c | 38 +++++++++++++++---------------- 2 files changed, 20 insertions(+), 21 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
@@ -1193,6 +1193,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);* @genmask: generation mask * @afinfo: address family info * @name: name of the table +* @validate_state: internal, set when transaction adds jumps */ struct nft_table {struct list_head list;
@@ -1211,6 +1212,7 @@ struct nft_table {char *name;u16 udlen;u8 *udata; +u8 validate_state; };static inline bool nft_table_has_owner (const struct nft_table *table)
@@ -1684,7 +1686,6 @@ struct nftables_pernet {struct mutex commit_mutex;u64 table_handle;unsigned int base_seq; -u8 validate_state; };extern unsigned int nf_tables_net_id;
net/netfilter/nf_tables_api.c +18 -20
@@ -102,11 +102,9 @@ static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, }; -static void nft_validate_state_update (struct net *net,u8 new_validate_state) +static void nft_validate_state_update (struct nft_table *table,u8 new_validate_state) { -struct nftables_pernet *nft_net =nft_pernet (net); - -switch (nft_net->validate_state) { +switch (table->validate_state) {case NFT_VALIDATE_SKIP :WARN_ON_ONCE (new_validate_state ==NFT_VALIDATE_DO );break ;
@@ -117,7 +115,7 @@ static void nft_validate_state_update(struct net *net, u8 new_validate_state)return ; } - nft_net->validate_state = new_validate_state; + table->validate_state = new_validate_state; }static void nf_tables_trans_destroy_work (struct work_struct *w); static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
@@ -1224,6 +1222,7 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,if (table ==NULL ) goto err_kzalloc; + table->validate_state =NFT_VALIDATE_SKIP ; table->name =nla_strdup (attr,GFP_KERNEL_ACCOUNT );if (table->name ==NULL ) goto err_strdup;
@@ -3608,7 +3607,7 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, }if (expr_info[i].ops->validate) -nft_validate_state_update (net,NFT_VALIDATE_NEED ); +nft_validate_state_update (table,NFT_VALIDATE_NEED ); expr_info[i].ops =NULL ; expr =nft_expr_next (expr);
@@ -3658,7 +3657,7 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,if (flow)nft_trans_flow_rule (trans) = flow; -if (nft_net->validate_state ==NFT_VALIDATE_DO ) +if (table->validate_state ==NFT_VALIDATE_DO )return nft_table_validate (net, table);return 0 ;
@@ -6265,7 +6264,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (desc.type ==NFT_DATA_VERDICT && (elem.data.val.verdict.code ==NFT_GOTO || elem.data.val.verdict.code ==NFT_JUMP )) -nft_validate_state_update (ctx->net, +nft_validate_state_update (ctx->table,NFT_VALIDATE_NEED ); }
@@ -6390,7 +6389,6 @@ static int nf_tables_newsetelem(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const nla[]) { - struct nftables_pernet *nft_net = nft_pernet(info->net); struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_next (info->net);u8 family = info->nfmsg->nfgen_family;
@@ -6429,7 +6427,7 @@ static int nf_tables_newsetelem(struct sk_buff *skb, } } -if (nft_net->validate_state ==NFT_VALIDATE_DO ) +if (table->validate_state ==NFT_VALIDATE_DO )return nft_table_validate (net, table);return 0 ;
@@ -8581,19 +8579,20 @@ static int nf_tables_validate(struct net *net)struct nftables_pernet *nft_net =nft_pernet (net);struct nft_table *table; -switch (nft_net->validate_state) { -case NFT_VALIDATE_SKIP : -break ; -case NFT_VALIDATE_NEED : -nft_validate_state_update (net,NFT_VALIDATE_DO ); - fallthrough; -case NFT_VALIDATE_DO : -list_for_each_entry (table, &nft_net->tables, list) { +list_for_each_entry (table, &nft_net->tables, list) { +switch (table->validate_state) { +case NFT_VALIDATE_SKIP : +continue ; +case NFT_VALIDATE_NEED : +nft_validate_state_update (table,NFT_VALIDATE_DO ); + fallthrough; +case NFT_VALIDATE_DO :if (nft_table_validate (net, table) <0 )return -EAGAIN ; + +nft_validate_state_update (table,NFT_VALIDATE_SKIP ); } -nft_validate_state_update (net,NFT_VALIDATE_SKIP );break ; }
@@ -10310,7 +10309,6 @@ static int __net_init nf_tables_init_net(struct net *net)INIT_LIST_HEAD (&nft_net->notify_list);mutex_init (&nft_net->commit_mutex); nft_net->base_seq =1 ; - nft_net->validate_state =NFT_VALIDATE_SKIP ;return 0 ; }
Signature · 1 line
2.39.2