revisiting nf_tables ruleset validation
Subsystems (2)
Pablo Neira Ayuso
maintainercover revisiting nf_tables ruleset validation
Hi,
The following patchset adds a new infrastructure to validate incremental updates on rulesets.
This new validation approach is based on a the following observations:
- New rules always require validation since they could come with expressions that are not supported by the basechain type, eg. masquerade on the filter chain.
- New rules that jump/goto target chain result in the validation of such target chain and subsequent chains that are connected this target chain. Same applies to new elements that jump/goto target chain.
Such target chains that need validation are stored in a validation list. Then, for each chain, the binding graph is used to perform the validation from the commit/abort phase. Sets and chains provide a list of bindings backwards (to reach the basechain) and forward (to validate subsequent chains that are connected to the target chain.
The new binding object stores is composed of a tuple that describes the origin set/chain and the target set/chain, valid binding combinations are:
1) chain-to-chain, eg. rule performs jump/goto target chain
2) chain-to-set, eg. rule performs lookup to set (verdict map).
3) set-to-chain, eg. set element performs jump/goto target chain (verdict map)
Adding bindings for 1) and 2) is relatively easy, 3) requires a bit more new code to deal with set deletion and aborting new set element, as well as netlink/netns exit path, to release bindings.
This patchset uses a hashtable of bindings to look up for existing bindings when adding new jump/goto chain from rules and set elements.
Patches #1 and #2 prepare for this infrastructure by disabling chain validation from preparation phase when validation state is _SKIP as well as avoiding a replay of the transaction on EINTR.
Patches #3 to #6 add the infrastructure to collect chains that need valudation, add the new binding infrastructure and finally use it.
This is passing tests/shell with debugging instrumentation.
On my TODO list:
- If the hashtable of bindings is per-netns (instead of per table as in this series), then I think that limiting the maximum number of jump/goto chain should be relatively trivial by adding a counter of total existing bindings in this per-netns (as counterproposal to Shaun Brady's patch to limit the number of jumps per-netns).
- I did not explore yet the replacement of the (old) set binding logic (struct nft_set_binding) by this new infrastructure.
Comments welcome, thanks.
Pablo Neira Ayuso (6):
netfilter: nf_tables: honor EINTR in ruleset validation from commit/abort path
netfilter: nf_tables: honor validation state in preparation phase
netfilter: nf_tables: add infrastructure for chain validation on updates
netfilter: nf_tables: add new binding infrastructure
netfilter: nf_tables: use new binding infrastructure
netfilter: nf_tables: add support for validating incremental ruleset updates
include/net/netfilter/nf_tables.h | 52 +- net/netfilter/nf_tables_api.c | 800 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 +- 3 files changed, 844 insertions(+), 33 deletions(-)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 1/6 netfilter: nf_tables: honor EINTR in ruleset validation from commit/abort path
Do not return EAGAIN to replay the transaction if table validation reports EINTR. Abort the transaction and report EINTR error instead.
--- net/netfilter/nf_tables_api.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
net/netfilter/nf_tables_api.c +22 -6
@@ -9885,6 +9885,7 @@ static int nf_tables_validate(struct net *net) {struct nftables_pernet *nft_net =nft_pernet (net);struct nft_table *table; +int err;list_for_each_entry (table, &nft_net->tables, list) {switch (table->validate_state) {
@@ -9894,15 +9895,24 @@ static int nf_tables_validate(struct net *net)nft_validate_state_update (table,NFT_VALIDATE_DO ); fallthrough;case NFT_VALIDATE_DO : -if (nft_table_validate (net, table) <0 ) -return -EAGAIN ; + err =nft_table_validate (net, table); +if (err <0 ) { +if (err ==EINTR ) + goto err_eintr; +return -EAGAIN ; + }nft_validate_state_update (table,NFT_VALIDATE_SKIP );break ; } }return 0 ; +err_eintr: +list_for_each_entry (table, &nft_net->tables, list) +nft_validate_state_update (table,NFT_VALIDATE_SKIP ); + +return -EINTR ; }/* a drop policy has to be deferred until all rules have been activated,
@@ -10710,7 +10720,11 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) }/* 0. Validate ruleset, otherwise roll back for error reporting. */ -if (nf_tables_validate (net) <0 ) { + err =nf_tables_validate (net); +if (err <0 ) { +if (err == -EINTR ) +return -EINTR ; + nft_net->validate_state =NFT_VALIDATE_DO ;return -EAGAIN ; }
@@ -11054,9 +11068,11 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) };int err =0 ; -if (action ==NFNL_ABORT_VALIDATE && -nf_tables_validate (net) <0 ) - err = -EAGAIN ; +if (action ==NFNL_ABORT_VALIDATE ) { + err =nf_tables_validate (net); +if (err <0 && err != -EINTR ) + err = -EAGAIN ; + }list_for_each_entry_safe_reverse (trans, next, &nft_net->commit_list, list) {
Signature · 1 line
2.30.2
Phil Sutter
reviewerRe: [PATCH nf-next,v1 1/6] netfilter: nf_tables: honor EINTR in ruleset validation from commit/abort path
Quoted 22 lines · Pablo Neira Ayuso
Do not return EAGAIN to replay the transaction if table validation reports EINTR. Abort the transaction and report EINTR error instead.
Fixes 169384fbe851 ("netfilter: nf_tables: allow loop termination for pending fatal signal")Signed-off-by Pablo Neira Ayuso <[email protected]>--- net/netfilter/nf_tables_api.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)net/netfilter/nf_tables_api.c +1
-0@@ -9885,6 +9885,7 @@ static int nf_tables_validate(struct net *net) {struct nftables_pernet *nft_net =nft_pernet (net);struct nft_table *table; +int err;list_for_each_entry (table, &nft_net->tables, list) {switch (table->validate_state) {
net/netfilter/nf_tables_api.c static int nf_tables_validate(struct net *net) · line 9900
Quoted 8 lines
net/netfilter/nf_tables_api.c +3
-2@@ -9894,15 +9895,24 @@ static int nf_tables_validate(struct net *net)nft_validate_state_update (table,NFT_VALIDATE_DO ); fallthrough;case NFT_VALIDATE_DO : -if (nft_table_validate (net, table) <0 ) -return -EAGAIN ; + err =nft_table_validate (net, table); +if (err <0 ) { +if (err ==EINTR )
This should be -EINTR here, I guess.
Cheers, Phil
Quoted 51 lines
net/netfilter/nf_tables_api.c +18
-4+ goto err_eintr; +return -EAGAIN ; + }nft_validate_state_update (table,NFT_VALIDATE_SKIP );break ; } }return 0 ; +err_eintr: +list_for_each_entry (table, &nft_net->tables, list) +nft_validate_state_update (table,NFT_VALIDATE_SKIP ); + +return -EINTR ; }/* a drop policy has to be deferred until all rules have been activated, @@ -10710,7 +10720,11 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) }/* 0. Validate ruleset, otherwise roll back for error reporting. */ -if (nf_tables_validate (net) <0 ) { + err =nf_tables_validate (net); +if (err <0 ) { +if (err == -EINTR ) +return -EINTR ; + nft_net->validate_state =NFT_VALIDATE_DO ;return -EAGAIN ; }@@ -11054,9 +11068,11 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) };int err =0 ; -if (action ==NFNL_ABORT_VALIDATE && -nf_tables_validate (net) <0 ) - err = -EAGAIN ; +if (action ==NFNL_ABORT_VALIDATE ) { + err =nf_tables_validate (net); +if (err <0 && err != -EINTR ) + err = -EAGAIN ; + }list_for_each_entry_safe_reverse (trans, next, &nft_net->commit_list, list) {Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/6 netfilter: nf_tables: honor validation state in preparation phase
In reply to Pablo Neira AyusoThere are three states for table validation:
- SKIP, this is the initial state, skip validation from the preparation phase and the commit/abort path.
- NEED, this state is entered from the preparation phase, to validate the table from the commit/abort path. In case that validation fails, this enters the DO state and the transaction is replayed.
- DO, this state validates updates incremental from the preparation step for error reporting.
Currently the NEED state is set on if:
- A new rule contains expressions that require validation, this includes jump/goto chain.
- A new set element with jump/goto chain.
However, there are two issues:
- Validation is performed incrementally with new rules and elements regardless the states. This patch updates the logic to perform the validation only in the DO state.
- Reset validate state in case the transaction is finally aborted with with NFNL_ABORT_NONE. Otherwise, the next batch can observe the DO state and it performs the validation incrementally.
--- net/netfilter/nf_tables_api.c | 50 ++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-)
net/netfilter/nf_tables_api.c +38 -12
@@ -3997,16 +3997,8 @@ static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rnf_tables_rule_destroy (ctx, rule); } -/** nft_chain_validate - loop detection and hook validation -* -* @ctx: context containing call depth and base chain -* @chain: chain to validate -* -* Walk through the rules of the given chain and chase all jumps/gotos -* and set lookups until either the jump limit is hit or all reachable -* chains have been validated. -*/ -int nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain) +static int __nft_chain_validate (const struct nft_ctx *ctx, +const struct nft_chain *chain) {struct nft_expr *expr, *last;struct nft_rule *rule;
@@ -4037,6 +4029,30 @@ int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)return 0 ; } + +/** nft_chain_validate - loop detection and hook validation +* +* @ctx: context containing call depth and base chain +* @chain: chain to validate +* +* Walk through the rules of the given chain and chase all jumps/gotos and set +* lookups until either the jump limit is hit or all reachable chains have been +* validated. +* +* This function is called from preparation phase: initial state is SKIP which +* means that validation can be skipped entirely. However, in case of a new rule +* or set element needs validation, then the NEED state is entered and the +* validation is performed from the commit/abort phase. In case this fails, the +* transaction is aborted and it is replayed in the DO validation state, then +* incremental validation is performed for error reporting. +*/ +int nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain) +{ +if (ctx->table->validate_state !=NFT_VALIDATE_DO ) +return 0 ; + +return __nft_chain_validate (ctx, chain); +}EXPORT_SYMBOL_GPL (nft_chain_validate);static int nft_table_validate (struct net *net,const struct nft_table *table)
@@ -4044,6 +4060,7 @@ static int nft_table_validate(struct net *net, const struct nft_table *table)struct nft_chain *chain;struct nft_ctx ctx = { .net = net, + .table = (struct nft_table *)table, .family = table->family, };int err;
@@ -4053,7 +4070,7 @@ static int nft_table_validate(struct net *net, const struct nft_table *table)continue ; ctx.chain = chain; - err =nft_chain_validate (&ctx, chain); + err =__nft_chain_validate (&ctx, chain);if (err <0 )return err;
@@ -11063,15 +11080,24 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)struct nft_trans *trans, *next;LIST_HEAD (set_update_list);struct nft_trans_elem *te; +struct nft_table *table;struct nft_ctx ctx = { .net = net, };int err =0 ; -if (action ==NFNL_ABORT_VALIDATE ) { +switch (action) { +case NFNL_ABORT_NONE : +list_for_each_entry (table, &nft_net->tables, list) +nft_validate_state_update (table,NFT_VALIDATE_SKIP ); +break ; +case NFNL_ABORT_AUTOLOAD : +break ; +case NFNL_ABORT_VALIDATE : err =nf_tables_validate (net);if (err <0 && err != -EINTR ) err = -EAGAIN ; +break ; } list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/6 netfilter: nf_tables: add infrastructure for chain validation on updates
In reply to Pablo Neira AyusoAdd infrastructure to validate rulesets at chain granularity to improve the situation for incremental rule and set element updates.
Instead of fully validating the table on updates, annotate chains in the table that needs to be validated again after the updates in this batch.
Add a validation list per netns that contains chains that are pending to be validated. A chain is added to the validation list under the following circunstances:
- A new rule is added, then add the chain that contains this rule. This allows to validate if the rule expressions are supported from this chain.
- A new rule performs a jump/goto another chain. The destination chain is added to the validation list.
- A new set element is added, then add the jump/goto chain via element (verdict maps).
Add the chain that need validation to the validation list, then from the commit/abort path, remove from the validation list. The validation list becomes empty after the commit/abort phase.
This is a preparation patch, note that full table validation is still in place, the next patch adds more infrastructure to enable chain validation.
--- include/net/netfilter/nf_tables.h | 5 +++- net/netfilter/nf_tables_api.c | 45 ++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-)
include/net/netfilter/nf_tables.h +4 -1
@@ -1119,11 +1119,13 @@ struct nft_chain {struct nft_rule_blob __rcu *blob_gen_1;struct list_head rules;struct list_head list; +struct list_head validate_list;struct rhlist_head rhlhead;struct nft_table *table;u64 handle;u32 use; - u8 flags:5, + u8 flags:4, + validate:1, bound:1, genmask:2;char *name;
@@ -1910,6 +1912,7 @@ struct nftables_pernet {struct list_head binding_list;struct list_head module_list;struct list_head notify_list; +struct list_head validate_list;struct mutex commit_mutex;u64 table_handle;u64 tstamp;
net/netfilter/nf_tables_api.c +42 -3
117 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/6 netfilter: nf_tables: add new binding infrastructure
In reply to Pablo Neira AyusoAdd new binding infrastructure to build a graph that relates chains and sets via jump/goto such as:
- chain to chain, ie. rule jump/goto chain.
- set to chain, ie. set element jump/goto chain.
- chain to set, ie. rule lookup to set.
The binding is composed of two tuples that describe [ from, to ], each component of this tuple is defined as the object type and the pointer to the object. This patch adds a hashtable for bindings per table to allow for lookups of existing bindings in the preparation phase.
The bindings allows to backtrack to the basechain that refers to this object for validation, and to go forward to check for loops and to perform the rule validation.
This patch adds interfaces to deactivate/activate these bindings during the preparation phase. Reference counter of zero tells us this binding will be removed after this transaction.
This is still a preparation patch, a follow patch uses this infrastructure.
--- include/net/netfilter/nf_tables.h | 45 ++++ net/netfilter/nf_tables_api.c | 352 ++++++++++++++++++++++++++++++ 2 files changed, 397 insertions(+)
include/net/netfilter/nf_tables.h +45 -0
@@ -585,6 +585,8 @@ struct nft_set_elem_expr {struct nft_set {struct list_head list;struct list_head bindings; +struct list_head binding_list; +struct list_head backbinding_list;refcount_t refs;struct nft_table *table;possible_net_t net;
@@ -1117,6 +1119,8 @@ struct nft_rule_blob {struct nft_chain {struct nft_rule_blob __rcu *blob_gen_0;struct nft_rule_blob __rcu *blob_gen_1; +struct list_head binding_list; +struct list_head backbinding_list;struct list_head rules;struct list_head list;struct list_head validate_list;
@@ -1293,6 +1297,7 @@ struct nft_table {struct list_head sets;struct list_head objects;struct list_head flowtables; +struct rhashtable bindings_ht;u64 hgenerator;u64 handle;u32 use;
@@ -1628,6 +1633,46 @@ static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)return test_bit (NFT_SET_ELEM_DEAD_BIT , word); } +enum nft_binding_type { +NFT_BIND_CHAIN =0 , +NFT_BIND_SET , +}; + +/* Bindings. */ +struct nft_binding_key { +union { +const struct nft_chain *chain; +const struct nft_set *set; +const void *ptr; + }; +enum nft_binding_type type; +}; + +struct nft_binding { +struct rhash_head node; +struct list_head list; +struct list_head backlist; +struct nft_binding_key from; +struct nft_binding_key to; +uint32_t use; +}; + +struct nft_chain ; +struct nft_set ; + +int nft_add_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +void nft_activate_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +void nft_deactivate_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +void nft_del_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +int nft_add_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +void nft_deactivate_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +void nft_activate_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +void nft_del_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +int nft_add_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +void nft_deactivate_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +void nft_activate_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +void nft_del_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +/** * struct nft_trans - nf_tables object update in transaction *
net/netfilter/nf_tables_api.c +352 -0
388 lines not on this page · raw message
Signature · 1 line
2.30.2
Phil Sutter
reviewerRe: [PATCH nf-next,v1 4/6] netfilter: nf_tables: add new binding infrastructure
Quoted 42 lines · Pablo Neira Ayuso
Add new binding infrastructure to build a graph that relates chains and sets via jump/goto such as:
- chain to chain, ie. rule jump/goto chain.
- set to chain, ie. set element jump/goto chain.
- chain to set, ie. rule lookup to set.The binding is composed of two tuples that describe [ from, to ], each component of this tuple is defined as the object type and the pointer to the object. This patch adds a hashtable for bindings per table to allow for lookups of existing bindings in the preparation phase.
The bindings allows to backtrack to the basechain that refers to this object for validation, and to go forward to check for loops and to perform the rule validation.
This patch adds interfaces to deactivate/activate these bindings during the preparation phase. Reference counter of zero tells us this binding will be removed after this transaction.
This is still a preparation patch, a follow patch uses this infrastructure.
Signed-off-by Pablo Neira Ayuso <[email protected]>--- include/net/netfilter/nf_tables.h | 45 ++++ net/netfilter/nf_tables_api.c | 352 ++++++++++++++++++++++++++++++ 2 files changed, 397 insertions(+)include/net/netfilter/nf_tables.h +2
-0@@ -585,6 +585,8 @@ struct nft_set_elem_expr {struct nft_set {struct list_head list;struct list_head bindings; +struct list_head binding_list; +struct list_head backbinding_list;refcount_t refs;struct nft_table *table;possible_net_t net;
include/net/netfilter/nf_tables.h struct nft_rule_blob { · line 1126
Quoted 8 lines
include/net/netfilter/nf_tables.h +2
-0@@ -1117,6 +1119,8 @@ struct nft_rule_blob {struct nft_chain {struct nft_rule_blob __rcu *blob_gen_0;struct nft_rule_blob __rcu *blob_gen_1; +struct list_head binding_list; +struct list_head backbinding_list;struct list_head rules;struct list_head list;struct list_head validate_list;
One could get away with just a single list by inserting the reverse ones up front and appending the forward ones. While traversing, one would either traverse forward or reverse and stop once the 'to' or 'from' pointer doesn't match the current chain/set anymore. Maybe not worth to save the 16B per set and chain, though.
Quoted 120 lines
include/net/netfilter/nf_tables.h +41
-0@@ -1293,6 +1297,7 @@ struct nft_table {struct list_head sets;struct list_head objects;struct list_head flowtables; +struct rhashtable bindings_ht;u64 hgenerator;u64 handle;u32 use;@@ -1628,6 +1633,46 @@ static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)return test_bit (NFT_SET_ELEM_DEAD_BIT , word); } +enum nft_binding_type { +NFT_BIND_CHAIN =0 , +NFT_BIND_SET , +}; + +/* Bindings. */ +struct nft_binding_key { +union { +const struct nft_chain *chain; +const struct nft_set *set; +const void *ptr; + }; +enum nft_binding_type type; +}; + +struct nft_binding { +struct rhash_head node; +struct list_head list; +struct list_head backlist; +struct nft_binding_key from; +struct nft_binding_key to; +uint32_t use; +}; + +struct nft_chain ; +struct nft_set ; + +int nft_add_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +void nft_activate_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +void nft_deactivate_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +void nft_del_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2); +int nft_add_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +void nft_deactivate_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +void nft_activate_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +void nft_del_chain_set_binding (struct nft_chain *chain,struct nft_set *set); +int nft_add_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +void nft_deactivate_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +void nft_activate_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +void nft_del_set_chain_binding (struct nft_set *set,struct nft_chain *chain); +/** * struct nft_trans - nf_tables object update in transaction * net/netfilter/nf_tables_api.c +57
-0@@ -954,6 +954,347 @@ void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) }EXPORT_SYMBOL_GPL (__nft_reg_track_cancel); +struct nft_binding_cmp_key { +const struct nft_binding_key *from; +const struct nft_binding_key *to; +}; + +static u32 nft_binding_hash (const void *data,u32 len,u32 seed) +{ +const struct nft_binding_cmp_key *key = data; +unsigned long tuple[4 ]; + + tuple[0 ] = (unsigned long )key->from->ptr; + tuple[1 ] = (unsigned long )key->from->type; + tuple[2 ] = (unsigned long )key->to->ptr; + tuple[3 ] = (unsigned long )key->to->type; + +return jhash (tuple,sizeof (tuple), seed); +} + +static u32 nft_binding_hash_obj (const void *data,u32 len,u32 seed) +{ +const struct nft_binding *binding = data; +unsigned long tuple[4 ]; + + tuple[0 ] = (unsigned long )binding->from.ptr; + tuple[1 ] = (unsigned long )binding->from.type; + tuple[2 ] = (unsigned long )binding->to.ptr; + tuple[3 ] = (unsigned long )binding->to.type; + +return jhash (tuple,sizeof (tuple), seed); +} + +static int nft_binding_hash_cmp (struct rhashtable_compare_arg *arg, +const void *ptr) +{ +const struct nft_binding_cmp_key *key = arg->key; +const struct nft_binding *binding = ptr; + +return key->from->ptr != binding->from.ptr || + key->from->type != binding->from.type || + key->to->ptr != binding->to.ptr || + key->to->type != binding->to.type; +} + +static const struct rhashtable_params nft_binding_ht_params = { + .head_offset = offsetof(struct nft_binding , node), + .hashfn = nft_binding_hash, + .obj_hashfn = nft_binding_hash_obj, + .obj_cmpfn = nft_binding_hash_cmp, + .automatic_shrinking = true, +}; + +static struct nft_binding *nft_binding_lookup (struct nft_table *table, +const struct nft_binding_key *from, +const struct nft_binding_key *to) +{ +struct nft_binding_cmp_key key = { + .from = from,
net/netfilter/nf_tables_api.c void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) · line 1023
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+ .to = to, + }; + +return rhashtable_lookup_fast (&table->bindings_ht, &key, + nft_binding_ht_params); +} + +static void nft_deactivate_binding (struct nft_table *table, +const struct nft_binding_key *from, +const struct nft_binding_key *to)
Changing the signature to:
| static void nft_deactivate_binding(struct nft_table *table, | void *from, enum nft_binding_type typef, | void *to, enum nft_binding_type typet)
net/netfilter/nf_tables_api.c void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) · line 1025
Quoted 2 lines
net/netfilter/nf_tables_api.c +2
-0+{ +struct nft_binding *binding;
And creating the keys here:
| struct nft_binding_key from = { .ptr = from, .type = typef };
| struct nft_binding_key to = { .ptr = to, .type = typet };
Quoted 14 lines
net/netfilter/nf_tables_api.c +14
-0+ + binding =nft_binding_lookup (table, from, to); +if (WARN_ON_ONCE (!binding)) +return ; +if (WARN_ON_ONCE (binding->use ==0 )) +return ; + + binding->use--; +} + +void nft_deactivate_chain_binding (struct nft_chain *chain1, +struct nft_chain *chain2) +{ +struct nft_binding_key from = {
net/netfilter/nf_tables_api.c void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) · line 1049
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+ .ptr = chain1, + .type = NFT_BIND_CHAIN, + }; +struct nft_binding_key to = { + .ptr = chain2, + .type =NFT_BIND_CHAIN , + }; + +nft_deactivate_binding (chain1->table, &from, &to); +}
Would reduce the above and following functions to one-line wrappers, like:
| {
| nft_deactivate_binding(chain1->table,
| chain1, NFT_BIND_CHAIN,
| chain2, NFT_BIND_CHAIN);
| }This could also be just a macro, too.
Quoted 95 lines
net/netfilter/nf_tables_api.c +95
-0+ +void nft_deactivate_chain_set_binding (struct nft_chain *chain, +struct nft_set *set) +{ +struct nft_binding_key from = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = set, + .type =NFT_BIND_SET , + }; + +nft_deactivate_binding (chain->table, &from, &to); +} + +void nft_deactivate_set_chain_binding (struct nft_set *set, +struct nft_chain *chain) +{ +struct nft_binding_key from = { + .ptr = set, + .type =NFT_BIND_SET , + }; +struct nft_binding_key to = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; + +nft_deactivate_binding (chain->table, &from, &to); +} + +static void nft_activate_binding (struct nft_table *table, +const struct nft_binding_key *from, +const struct nft_binding_key *to) +{ +struct nft_binding *binding; + + binding =nft_binding_lookup (table, from, to); +if (WARN_ON_ONCE (!binding)) +return ; + + binding->use++; +} + +void nft_activate_chain_binding (struct nft_chain *chain1, +struct nft_chain *chain2) +{ +struct nft_binding_key from = { + .ptr = chain1, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = chain2, + .type =NFT_BIND_CHAIN , + }; + +nft_activate_binding (chain1->table, &from, &to); +} + +void nft_activate_chain_set_binding (struct nft_chain *chain, +struct nft_set *set) +{ +struct nft_binding_key from = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = set, + .type =NFT_BIND_SET , + }; + +nft_activate_binding (chain->table, &from, &to); +} + +void nft_activate_set_chain_binding (struct nft_set *set, +struct nft_chain *chain) +{ +struct nft_binding_key from = { + .ptr = set, + .type =NFT_BIND_SET , + }; +struct nft_binding_key to = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; + +nft_activate_binding (chain->table, &from, &to); +} + +static void nft_del_binding (struct nft_table *table, +const struct nft_binding_key *from, +const struct nft_binding_key *to) +{ +struct nft_binding *binding; +int err;
net/netfilter/nf_tables_api.c void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) · line 1154
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+ + binding =nft_binding_lookup (table, from, to); +/* With several references to object, deactivate deals to zero use, +* then first delete binding call remove it. +*/ +if (!binding) +return ; + +if (binding->use !=0 ) +return ;
The asymmetry between add and del in that add increments the use-counter while del does not decrement is a bit confusing. I believe it is an optimization since add is always followed by enable otherwise?
I also wonder, does the code expectedly call del for enabled bindings or is this a bug and there should be a warning above?
Cheers, Phil
Quoted 197 lines
net/netfilter/nf_tables_api.c +154
-0+ +list_del (&binding->list); +list_del (&binding->backlist); + + err =rhashtable_remove_fast (&table->bindings_ht, + &binding->node, nft_binding_ht_params); +if (WARN_ON_ONCE (err <0 )) +return ; + +kfree (binding); +} + +void nft_del_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2) +{ +struct nft_binding_key from = { + .ptr = chain1, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = chain2, + .type =NFT_BIND_CHAIN , + }; + +nft_del_binding (chain1->table, &from, &to); +} + +void nft_del_chain_set_binding (struct nft_chain *chain,struct nft_set *set) +{ +struct nft_binding_key from = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = set, + .type =NFT_BIND_SET , + }; + +nft_del_binding (chain->table, &from, &to); +} + +void nft_del_set_chain_binding (struct nft_set *set,struct nft_chain *chain) +{ +struct nft_binding_key from = { + .ptr = set, + .type =NFT_BIND_SET , + }; +struct nft_binding_key to = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; + +nft_del_binding (chain->table, &from, &to); +} + +static int __nft_add_binding (struct nft_table *table, +const struct nft_binding_key *from, +const struct nft_binding_key *to, +struct list_head *binding_list, +struct list_head *backbinding_list) +{ +struct nft_binding *binding; + + binding =kzalloc (sizeof (struct nft_binding ),GFP_KERNEL ); +if (!binding) +return -ENOMEM ; + + binding->from = *from; + binding->to = *to; + binding->use++; + +list_add_tail (&binding->list, binding_list); +list_add_tail (&binding->backlist, backbinding_list); + +return rhashtable_insert_fast (&table->bindings_ht, &binding->node, + nft_binding_ht_params); +} + +static int nft_add_binding (struct nft_table *table, +const struct nft_binding_key *from, +const struct nft_binding_key *to, +struct list_head *binding_list, +struct list_head *backbinding_list) +{ +struct nft_binding *binding; + + binding =nft_binding_lookup (table, from, to); +if (!binding) +return __nft_add_binding (table, from, to, + binding_list, backbinding_list); + +if (binding->use ==UINT_MAX ) +return -EOVERFLOW ; + + binding->use++; + +return 0 ; +} + +int nft_add_chain_binding (struct nft_chain *chain1,struct nft_chain *chain2) +{ +struct nft_binding_key from = { + .ptr = chain1, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = chain2, + .type =NFT_BIND_CHAIN , + }; + +return nft_add_binding (chain1->table, &from, &to, + &chain1->binding_list, &chain2->backbinding_list); +} + +int nft_add_chain_set_binding (struct nft_chain *chain,struct nft_set *set) +{ +struct nft_binding_key from = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; +struct nft_binding_key to = { + .ptr = set, + .type =NFT_BIND_SET , + }; + +return nft_add_binding (chain->table, &from, &to, + &chain->binding_list, &set->backbinding_list); +} + +int nft_add_set_chain_binding (struct nft_set *set,struct nft_chain *chain) +{ +struct nft_binding_key from = { + .ptr = set, + .type =NFT_BIND_SET , + }; +struct nft_binding_key to = { + .ptr = chain, + .type =NFT_BIND_CHAIN , + }; + +return nft_add_binding (chain->table, &from, &to, + &set->binding_list, &chain->backbinding_list); +} +/* * Tables */ @@ -1611,6 +1952,10 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,if (err) goto err_chain_ht; + err =rhashtable_init (&table->bindings_ht, &nft_binding_ht_params); +if (err) + goto err_binding_ht; +INIT_LIST_HEAD (&table->chains);INIT_LIST_HEAD (&table->sets);INIT_LIST_HEAD (&table->objects);@@ -1629,6 +1974,8 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,list_add_tail_rcu (&table->list, &nft_net->tables);return 0 ; err_trans: +rhashtable_destroy (&table->bindings_ht); +err_binding_ht:rhltable_destroy (&table->chains_ht); err_chain_ht:kfree (table->udata);@@ -1794,6 +2141,7 @@ static void nf_tables_table_destroy(struct nft_table *table)if (WARN_ON (table->use >0 ))return ; +rhashtable_destroy (&table->bindings_ht);rhltable_destroy (&table->chains_ht);kfree (table->name);kfree (table->udata);@@ -2691,6 +3039,8 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 policy, ctx->chain = chain;INIT_LIST_HEAD (&chain->rules); +INIT_LIST_HEAD (&chain->binding_list); +INIT_LIST_HEAD (&chain->backbinding_list); chain->handle =nf_tables_alloc_handle (table); chain->table = table;@@ -5523,6 +5873,8 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, }INIT_LIST_HEAD (&set->bindings); +INIT_LIST_HEAD (&set->binding_list); +INIT_LIST_HEAD (&set->backbinding_list);INIT_LIST_HEAD (&set->catchall_list);refcount_set (&set->refs,1 ); set->table = table;Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/6 netfilter: nf_tables: use new binding infrastructure
In reply to Pablo Neira AyusoThis patch uses the new binding infrastructure to link chains and sets:
- Chain-to-chain and chain-to-set bindings already exists, so it is relatively trivial to add the code to add, deactivate, activate and delete the bindings.
- Set-to-chain is a new binding required by verdict maps, this requires to release this new type of binding from the commit/abort path. Special cases are the deletion of sets with elements and netns/netlink release path, that require the removal of all set-to-chain bindings. Another special case is anonymous sets, that also require to delete all set-to-chain bindings when unbound.
The binding graph is not yet used to validate rulesets, this is enabled in the follow up patch.
--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 149 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 ++++- 3 files changed, 164 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
@@ -1929,7 +1929,7 @@ struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,struct nft_trans_gc *nft_trans_gc_catchall_sync (struct nft_trans_gc *gc);void nft_setelem_data_deactivate (const struct net *net, -const struct nft_set *set, +struct nft_set *set,struct nft_elem_priv *elem_priv);int __init nft_chain_filter_init (void );
net/netfilter/nf_tables_api.c +139 -10
294 lines not on this page · raw message
net/netfilter/nft_immediate.c +24 -1
@@ -76,9 +76,16 @@ static int nft_immediate_init(const struct nft_ctx *ctx,switch (priv->data.verdict.code) {case NFT_JUMP :case NFT_GOTO : - err =nf_tables_bind_chain (ctx, chain); + err =nft_add_chain_binding (ctx->chain, chain);if (err <0 ) goto err1; + + err =nf_tables_bind_chain (ctx, chain); +if (err <0 ) { +nft_deactivate_chain_binding (ctx->chain, chain); +nft_del_chain_binding (ctx->chain, chain); + goto err1; + }break ;default :break ;
@@ -106,6 +113,8 @@ static void nft_immediate_activate(const struct nft_ctx *ctx,case NFT_JUMP :case NFT_GOTO : chain = data->verdict.chain; +nft_activate_chain_binding (ctx->chain, chain); +if (!nft_chain_binding (chain))break ;
@@ -152,6 +161,20 @@ static void nft_immediate_deactivate(const struct nft_ctx *ctx,case NFT_JUMP :case NFT_GOTO : chain = data->verdict.chain; +switch (phase) { +case NFT_TRANS_PREPARE_ERROR : +case NFT_TRANS_PREPARE : +nft_deactivate_chain_binding (ctx->chain, chain); +break ; +case NFT_TRANS_ABORT : +case NFT_TRANS_RELEASE : +nft_deactivate_chain_binding (ctx->chain, chain); + fallthrough; +case NFT_TRANS_COMMIT : +nft_del_chain_binding (ctx->chain, chain); +break ; + } +if (!nft_chain_binding (chain))break ;
Signature · 1 line
2.30.2
Phil Sutter
reviewerRe: [PATCH nf-next,v1 5/6] netfilter: nf_tables: use new binding infrastructure
Quoted 132 lines · Pablo Neira Ayuso
This patch uses the new binding infrastructure to link chains and sets:
- Chain-to-chain and chain-to-set bindings already exists, so it is relatively trivial to add the code to add, deactivate, activate and delete the bindings.
- Set-to-chain is a new binding required by verdict maps, this requires to release this new type of binding from the commit/abort path. Special cases are the deletion of sets with elements and netns/netlink release path, that require the removal of all set-to-chain bindings. Another special case is anonymous sets, that also require to delete all set-to-chain bindings when unbound.
The binding graph is not yet used to validate rulesets, this is enabled in the follow up patch.
Signed-off-by Pablo Neira Ayuso <[email protected]>--- include/net/netfilter/nf_tables.h | 2 +- net/netfilter/nf_tables_api.c | 149 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 ++++- 3 files changed, 164 insertions(+), 12 deletions(-)include/net/netfilter/nf_tables.h +1
-1@@ -1929,7 +1929,7 @@ struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,struct nft_trans_gc *nft_trans_gc_catchall_sync (struct nft_trans_gc *gc);void nft_setelem_data_deactivate (const struct net *net, -const struct nft_set *set, +struct nft_set *set,struct nft_elem_priv *elem_priv);int __init nft_chain_filter_init (void );net/netfilter/nf_tables_api.c +26
-1@@ -6077,6 +6077,7 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, {struct nft_set_binding *i;struct nft_set_iter iter; +int err;if (!list_empty (&set->bindings) &&nft_set_is_anonymous (set))return -EBUSY ;@@ -6109,6 +6110,12 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,if (!nft_use_inc (&set->use))return -EMFILE ; + err =nft_add_chain_set_binding (ctx->chain, set); +if (err <0 ) { +nft_use_dec_restore (&set->use); +return err; + } + binding->chain = ctx->chain;list_add_tail_rcu (&binding->list, &set->bindings);nft_set_trans_bind (ctx, set);@@ -6117,14 +6124,27 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, }EXPORT_SYMBOL_GPL (nf_tables_bind_set); +static void nft_del_set_chain_binding_all (struct nft_set *set) +{ +struct nft_binding *binding, *next; +struct nft_chain *chain; + +list_for_each_entry_safe (binding, next, &set->binding_list, list) { + chain = (struct nft_chain *)binding->to.chain; +nft_del_set_chain_binding (set, chain); + } +} +static void nf_tables_unbind_set (const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_binding *binding,bool event) {list_del_rcu (&binding->list); +nft_del_chain_set_binding (ctx->chain, set);if (list_empty (&set->bindings) &&nft_set_is_anonymous (set)) {list_del_rcu (&set->list); set->dead =1 ; +nft_del_set_chain_binding_all (set);if (event)nf_tables_set_notify (ctx, set,NFT_MSG_DELSET ,GFP_KERNEL );@@ -6132,7 +6152,7 @@ static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, }static void nft_setelem_data_activate (const struct net *net, -const struct nft_set *set, +struct nft_set *set,struct nft_elem_priv *elem_priv); static int nft_mapelem_activate(const struct nft_ctx *ctx,@@ -6193,6 +6213,7 @@ void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set)nft_clear (ctx->net, set); } +nft_activate_chain_set_binding (ctx->chain, set);nft_use_inc_restore (&set->use); }EXPORT_SYMBOL_GPL (nf_tables_activate_set);@@ -6211,6 +6232,8 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,else list_del_rcu (&binding->list); +nft_deactivate_chain_set_binding (ctx->chain, set); +nft_del_chain_set_binding (ctx->chain, set);nft_use_dec (&set->use);break ;case NFT_TRANS_PREPARE :@@ -6220,6 +6243,7 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,nft_deactivate_next (ctx->net, set); } +nft_deactivate_chain_set_binding (ctx->chain, set);nft_use_dec (&set->use);return ;case NFT_TRANS_ABORT :@@ -6228,6 +6252,7 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))nft_map_deactivate (ctx, set); +nft_deactivate_chain_set_binding (ctx->chain, set);nft_use_dec (&set->use); fallthrough;default :
net/netfilter/nf_tables_api.c static void __nft_set_elem_destroy(const struct nft_ctx *ctx, · line 7152
Quoted 4 lines
net/netfilter/nf_tables_api.c +1
-0@@ -7124,6 +7149,7 @@ static void __nft_set_elem_destroy(const struct nft_ctx *ctx,nft_data_release (nft_set_ext_key (ext),NFT_DATA_VALUE );if (nft_set_ext_exists (ext,NFT_SET_EXT_DATA ))nft_data_release (nft_set_ext_data (ext), set->dtype); +// XXX gc
Leftover?
Cheers, Phil
Quoted 274 lines
net/netfilter/nf_tables_api.c +112
-9if (destroy_expr &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPRESSIONS ))nft_set_elem_expr_destroy (ctx,nft_set_ext_expr (ext));if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF ))@@ -7454,6 +7480,27 @@ static void nft_setelem_remove(const struct net *net, set->ops->remove (net, set, elem_priv); } +static void nft_setelem_data_binding_remove (struct nft_set *set, +struct nft_elem_priv *elem_priv) +{ +struct nft_set_ext *ext; + + ext =nft_set_elem_ext (set, elem_priv); +if (set->dtype ==NFT_DATA_VERDICT && +nft_set_ext_exists (ext,NFT_SET_EXT_DATA )) { +struct nft_data *data =nft_set_ext_data (ext); +struct nft_chain *chain; + +switch (data->verdict.code) { +case NFT_JUMP : +case NFT_GOTO : + chain = data->verdict.chain; +nft_del_set_chain_binding (set, chain); +break ; + } + } +} +static void nft_trans_elems_remove (const struct nft_ctx *ctx,const struct nft_trans_elem *te) {@@ -7471,6 +7518,8 @@ static void nft_trans_elems_remove(const struct nft_ctx *ctx,atomic_dec (&te->set->nelems); te->set->ndeact--; } + +nft_setelem_data_binding_remove (te->set, te->elems[i].priv); } }@@ -7768,9 +7817,17 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,nft_validate_chain_need (ctx, elem.data.val.verdict.chain); } +if (desc.type ==NFT_DATA_VERDICT && + (elem.data.val.verdict.code ==NFT_GOTO || + elem.data.val.verdict.code ==NFT_JUMP )) { + err =nft_add_set_chain_binding (set, elem.data.val.verdict.chain); +if (err <0 ) + goto err_parse_data; + } + err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_DATA , desc.len);if (err <0 ) - goto err_parse_data; + goto err_binding; }/* The full maximum length of userdata can exceed the maximum @@ -7784,7 +7841,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, err =nft_set_ext_add_length (&tmpl,NFT_SET_EXT_USERDATA , ulen);if (err <0 ) - goto err_parse_data; + goto err_binding; } }@@ -7793,7 +7850,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, timeout, expiration, GFP_KERNEL_ACCOUNT);if (IS_ERR (elem.priv)) { err =PTR_ERR (elem.priv); - goto err_parse_data; + goto err_binding; } ext =nft_set_elem_ext (set, elem.priv);@@ -7903,6 +7960,15 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,kfree (trans); err_elem_free:nf_tables_set_elem_destroy (ctx, set, elem.priv); +err_binding: +if (nla[NFTA_SET_ELEM_DATA ] !=NULL ) { +if (desc.type ==NFT_DATA_VERDICT && + (elem.data.val.verdict.code ==NFT_GOTO || + elem.data.val.verdict.code ==NFT_JUMP )) { +nft_deactivate_set_chain_binding (set, elem.data.val.verdict.chain); +nft_del_set_chain_binding (set, elem.data.val.verdict.chain); + } + } err_parse_data:if (nla[NFTA_SET_ELEM_DATA ] !=NULL )nft_data_release (&elem.data.val, desc.type);@@ -8007,30 +8073,90 @@ static int nft_setelem_active_next(const struct net *net,return nft_set_elem_active (ext, genmask); } +static void nft_setelem_data_hold (const struct net *net, +const struct nft_set *set, +const struct nft_set_ext *ext) +{ +if (set->dtype ==NFT_DATA_VERDICT ) { +struct nft_data *data =nft_set_ext_data (ext); +struct nft_chain *chain; + +switch (data->verdict.code) { +case NFT_JUMP : +case NFT_GOTO : + chain = data->verdict.chain; +nft_activate_set_chain_binding ((struct nft_set *)set, chain); +nft_use_inc_restore (&chain->use); +break ; + } + } +} +static void nft_setelem_data_activate (const struct net *net, -const struct nft_set *set, +struct nft_set *set,struct nft_elem_priv *elem_priv) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem_priv);if (nft_set_ext_exists (ext,NFT_SET_EXT_DATA )) -nft_data_hold (nft_set_ext_data (ext), set->dtype); +nft_setelem_data_hold (net, set, ext);if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF ))nft_use_inc_restore (&(*nft_set_ext_obj (ext))->use); } +static void nft_setelem_data_release (struct nft_set *set, +const struct nft_set_ext *ext) +{ +struct nft_data *data =nft_set_ext_data (ext); + +if (set->dtype ==NFT_DATA_VERDICT ) { +struct nft_chain *chain; + +switch (data->verdict.code) { +case NFT_JUMP : +case NFT_GOTO : + chain = data->verdict.chain; +nft_deactivate_set_chain_binding (set, chain); +nft_use_dec (&chain->use); +break ; + } + } +} +void nft_setelem_data_deactivate (const struct net *net, -const struct nft_set *set, +struct nft_set *set,struct nft_elem_priv *elem_priv) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem_priv);if (nft_set_ext_exists (ext,NFT_SET_EXT_DATA )) -nft_data_release (nft_set_ext_data (ext), set->dtype); +nft_setelem_data_release (set, ext);if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF ))nft_use_dec (&(*nft_set_ext_obj (ext))->use); } +static void nft_setelem_data_abort (struct nft_set *set, +struct nft_elem_priv *elem_priv) +{ +struct nft_set_ext *ext; + + ext =nft_set_elem_ext (set, elem_priv); +if (set->dtype ==NFT_DATA_VERDICT && +nft_set_ext_exists (ext,NFT_SET_EXT_DATA )) { +struct nft_data *data =nft_set_ext_data (ext); +struct nft_chain *chain; + +switch (data->verdict.code) { +case NFT_JUMP : +case NFT_GOTO : + chain = data->verdict.chain; +nft_deactivate_set_chain_binding (set, chain); +nft_del_set_chain_binding (set, chain); +break ; + } + } +} +/* similar to nft_trans_elems_remove, but called from abort path to undo newsetelem. * No notifications and no ndeact changes. * @@ -8057,6 +8183,7 @@ static bool nft_trans_elems_new_abort(const struct nft_ctx *ctx,if (!nft_setelem_is_catchall (te->set, te->elems[i].priv))atomic_dec (&te->set->nelems); +nft_setelem_data_abort (te->set, te->elems[i].priv); removed = true; }@@ -11290,6 +11417,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)case NFT_MSG_DESTROYSET :nft_trans_set (trans)->dead =1 ;list_del_rcu (&nft_trans_set (trans)->list); +nft_del_set_chain_binding_all (nft_trans_set (trans));nf_tables_set_notify (&ctx,nft_trans_set (trans), trans->msg_type,GFP_KERNEL );break ;@@ -12258,9 +12386,10 @@ static void __nft_release_table(struct net *net, struct nft_table *table)list_for_each_entry_safe (set, ns, &table->sets, list) {list_del (&set->list);nft_use_dec (&table->use); -if (set->flags & (NFT_SET_MAP |NFT_SET_OBJECT )) +if (set->flags & (NFT_SET_MAP |NFT_SET_OBJECT )) {nft_map_deactivate (&ctx, set); - +nft_del_set_chain_binding_all (set); + }nft_set_destroy (&ctx, set); }list_for_each_entry_safe (obj, ne, &table->objects, list) {net/netfilter/nft_immediate.c +24
-1@@ -76,9 +76,16 @@ static int nft_immediate_init(const struct nft_ctx *ctx,switch (priv->data.verdict.code) {case NFT_JUMP :case NFT_GOTO : - err =nf_tables_bind_chain (ctx, chain); + err =nft_add_chain_binding (ctx->chain, chain);if (err <0 ) goto err1; + + err =nf_tables_bind_chain (ctx, chain); +if (err <0 ) { +nft_deactivate_chain_binding (ctx->chain, chain); +nft_del_chain_binding (ctx->chain, chain); + goto err1; + }break ;default :break ;@@ -106,6 +113,8 @@ static void nft_immediate_activate(const struct nft_ctx *ctx,case NFT_JUMP :case NFT_GOTO : chain = data->verdict.chain; +nft_activate_chain_binding (ctx->chain, chain); +if (!nft_chain_binding (chain))break ;@@ -152,6 +161,20 @@ static void nft_immediate_deactivate(const struct nft_ctx *ctx,case NFT_JUMP :case NFT_GOTO : chain = data->verdict.chain; +switch (phase) { +case NFT_TRANS_PREPARE_ERROR : +case NFT_TRANS_PREPARE : +nft_deactivate_chain_binding (ctx->chain, chain); +break ; +case NFT_TRANS_ABORT : +case NFT_TRANS_RELEASE : +nft_deactivate_chain_binding (ctx->chain, chain); + fallthrough; +case NFT_TRANS_COMMIT : +nft_del_chain_binding (ctx->chain, chain); +break ; + } +if (!nft_chain_binding (chain))break ;Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/6 netfilter: nf_tables: add support for validating incremental ruleset updates
In reply to Pablo Neira AyusoUse the new binding graph to validate incremental ruleset updates.
Perform full validation if the table is new, which is the case of the initial ruleset reload. Subsequent incremental ruleset updates use the validation list which contains chains with new rules or chains that can be now reached via jump/goto from another rule/set element.
When validating a chain from commit/abort phase, backtrack to collect the basechains that can reach this chain, then perform the validation of rules in this chain. If no basechains can be reached, then skip validation for this chain. However, if basechains are off the jump stack limit, then resort to full ruleset validation. This is to prevent inconsistent validation between the preparation and commit/abort phase validations.
As for loop checking, stick to the existing approach which uses the jump stack limit to detect cycles.
--- net/netfilter/nf_tables_api.c | 188 +++++++++++++++++++++++++++++++++- 1 file changed, 183 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +183 -5
@@ -10397,6 +10397,160 @@ static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { }, }; +struct basechain_array { +const struct nft_chain **basechain; +uint32_t num_basechains; +uint32_t max_basechains; +int max_level; +}; + +static int basechain_array_add (struct basechain_array *array, +const struct nft_chain *chain,int level) +{ +const struct nft_chain **new_basechain; +uint32_t new_max_basechains; + +if (array->num_basechains == array->max_basechains) { + new_max_basechains = array->max_basechains +16 ; + new_basechain =krealloc_array (array->basechain, new_max_basechains,sizeof (struct nft_chain *),GFP_KERNEL ); +if (!new_basechain) +return -ENOMEM ; + + array->basechain = new_basechain; + array->max_basechains = new_max_basechains; + } + array->basechain[array->num_basechains++] = chain; + +if (level > array->max_level) + array->max_level = level; + +return 0 ; +} + +static int nft_chain_validate_backtrack (struct basechain_array *array, +const struct list_head *backbinding_list, +int *level) +{ +struct nft_binding *binding; +int err; + +/* Basechain is unreachable, fall back to slow path validation. */ +if (*level >=NFT_JUMP_STACK_SIZE ) +return -ENOENT ; + +list_for_each_entry (binding, backbinding_list, backlist) { +if (binding->from.type ==NFT_BIND_CHAIN && + binding->from.chain->flags &NFT_CHAIN_BASE && + binding->use >0 ) { +if (basechain_array_add (array, binding->from.chain, *level) <0 ) +return -ENOMEM ; + +continue ; + } + +switch (binding->from.type) { +case NFT_BIND_CHAIN : +if (binding->use ==0 ) +break ; + + (*level)++; + err =nft_chain_validate_backtrack (array, + &binding->from.chain->backbinding_list, + level); +if (err <0 ) +return err; + + (*level)--; +break ; +case NFT_BIND_SET : +if (binding->use ==0 ) +break ; + +/* no level update for sets. */ + err =nft_chain_validate_backtrack (array, + &binding->from.set->backbinding_list, + level); +if (err <0 ) +return err; + +break ; + } + } + +return 0 ; +} + +static int nft_chain_validate_incremental (struct net *net, +const struct nft_chain *chain) +{ +struct basechain_array array = {}; +uint32_t i, level =1 ; +int err; + + array.max_basechains =16 ; + array.basechain =kcalloc (16 ,sizeof (struct nft_chain *),GFP_KERNEL ); +if (!array.basechain) +return -ENOMEM ; + +if (nft_is_base_chain (chain)) { + err =basechain_array_add (&array, chain,0 ); +if (err <0 ) { +kfree (array.basechain); +return -ENOMEM ; + } + }else { + err =nft_chain_validate_backtrack (&array, + &chain->backbinding_list, + &level); +if (err <0 ) { +kfree (array.basechain); +return err; + } + } + +for (i =0 ; i < array.num_basechains; i++) { +struct nft_ctx ctx = { + .net = net, + .family = chain->table->family, + .table = chain->table, + .chain = (struct nft_chain *)array.basechain[i], + .level = array.max_level, + }; + +if (WARN_ON_ONCE (!nft_is_base_chain (array.basechain[i]))) +continue ; + + err =nft_chain_validate (&ctx, chain); +if (err <0 ) +break ; + } + +kfree (array.basechain); + +return err; +} + +static int nft_validate_incremental (struct net *net,struct nft_table *table) +{ +struct nftables_pernet *nft_net =nft_pernet (net); +struct nft_chain *chain, *next; +int err; + + err =0 ; +list_for_each_entry_safe (chain, next, &nft_net->validate_list, validate_list) { +if (chain->table != table) +continue ; + +if (err >=0 ) + err =nft_chain_validate_incremental (net, chain); + +list_del (&chain->validate_list); + chain->validate =0 ; + } + +return err; +} +static void nft_validate_chain_release (struct net *net) {struct nftables_pernet *nft_net =nft_pernet (net);
@@ -10422,12 +10576,36 @@ static int nf_tables_validate(struct net *net)nft_validate_state_update (table,NFT_VALIDATE_DO ); fallthrough;case NFT_VALIDATE_DO : - err =nft_table_validate (net, table); -if (err <0 ) { -if (err ==EINTR ) - goto err_eintr; +/* If this table is new, then this is the initial +* ruleset restore, perform full table validation, +* otherwise, perform incremental validation. +*/ +if (!nft_is_active (net, table)) { + err =nft_table_validate (net, table); +if (err <0 ) { +if (err ==EINTR ) + goto err_eintr; -return -EAGAIN ; +return -EAGAIN ; + } + }else { + err =nft_validate_incremental (net, table); +if (err <0 ) { +if (err != -ENOMEM && err != -ENOENT ) +return -EAGAIN ; + +/* Either no memory or it cannot reach +* basechain, then fallback to full +* validation. +*/ + err =nft_table_validate (net, table); +if (err <0 ) { +if (err ==EINTR ) + goto err_eintr; + +return -EAGAIN ; + } + } }nft_validate_state_update (table,NFT_VALIDATE_SKIP );break ;
Signature · 1 line
2.30.2
Phil Sutter
reviewerRe: [PATCH nf-next,v1 6/6] netfilter: nf_tables: add support for validating incremental ruleset updates
Quoted 85 lines · Pablo Neira Ayuso
Use the new binding graph to validate incremental ruleset updates.
Perform full validation if the table is new, which is the case of the initial ruleset reload. Subsequent incremental ruleset updates use the validation list which contains chains with new rules or chains that can be now reached via jump/goto from another rule/set element.
When validating a chain from commit/abort phase, backtrack to collect the basechains that can reach this chain, then perform the validation of rules in this chain. If no basechains can be reached, then skip validation for this chain. However, if basechains are off the jump stack limit, then resort to full ruleset validation. This is to prevent inconsistent validation between the preparation and commit/abort phase validations.
As for loop checking, stick to the existing approach which uses the jump stack limit to detect cycles.
Signed-off-by Pablo Neira Ayuso <[email protected]>--- net/netfilter/nf_tables_api.c | 188 +++++++++++++++++++++++++++++++++- 1 file changed, 183 insertions(+), 5 deletions(-)net/netfilter/nf_tables_api.c +54
-0@@ -10397,6 +10397,160 @@ static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { }, }; +struct basechain_array { +const struct nft_chain **basechain; +uint32_t num_basechains; +uint32_t max_basechains; +int max_level; +}; + +static int basechain_array_add (struct basechain_array *array, +const struct nft_chain *chain,int level) +{ +const struct nft_chain **new_basechain; +uint32_t new_max_basechains; + +if (array->num_basechains == array->max_basechains) { + new_max_basechains = array->max_basechains +16 ; + new_basechain =krealloc_array (array->basechain, new_max_basechains,sizeof (struct nft_chain *),GFP_KERNEL ); +if (!new_basechain) +return -ENOMEM ; + + array->basechain = new_basechain; + array->max_basechains = new_max_basechains; + } + array->basechain[array->num_basechains++] = chain; + +if (level > array->max_level) + array->max_level = level; + +return 0 ; +} + +static int nft_chain_validate_backtrack (struct basechain_array *array, +const struct list_head *backbinding_list, +int *level) +{ +struct nft_binding *binding; +int err; + +/* Basechain is unreachable, fall back to slow path validation. */ +if (*level >=NFT_JUMP_STACK_SIZE ) +return -ENOENT ; + +list_for_each_entry (binding, backbinding_list, backlist) { +if (binding->from.type ==NFT_BIND_CHAIN && + binding->from.chain->flags &NFT_CHAIN_BASE && + binding->use >0 ) { +if (basechain_array_add (array, binding->from.chain, *level) <0 ) +return -ENOMEM ; + +continue ; + } + +switch (binding->from.type) { +case NFT_BIND_CHAIN : +if (binding->use ==0 )
net/netfilter/nf_tables_api.c static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { · line 10463
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+break ; + + (*level)++; + err =nft_chain_validate_backtrack (array, + &binding->from.chain->backbinding_list, + level); +if (err <0 ) +return err; + + (*level)--;
It looks like you're trying to record the max level value for error path, but I don't see it used in callers. Is this a leftover from before introduction of basechain_array::max_level? If so, one may just pass 'level + 1' by value to the recursive call, right?
Quoted 72 lines
net/netfilter/nf_tables_api.c +72
-0+break ; +case NFT_BIND_SET : +if (binding->use ==0 ) +break ; + +/* no level update for sets. */ + err =nft_chain_validate_backtrack (array, + &binding->from.set->backbinding_list, + level); +if (err <0 ) +return err; + +break ; + } + } + +return 0 ; +} + +static int nft_chain_validate_incremental (struct net *net, +const struct nft_chain *chain) +{ +struct basechain_array array = {}; +uint32_t i, level =1 ; +int err; + + array.max_basechains =16 ; + array.basechain =kcalloc (16 ,sizeof (struct nft_chain *),GFP_KERNEL ); +if (!array.basechain) +return -ENOMEM ; + +if (nft_is_base_chain (chain)) { + err =basechain_array_add (&array, chain,0 ); +if (err <0 ) { +kfree (array.basechain); +return -ENOMEM ; + } + }else { + err =nft_chain_validate_backtrack (&array, + &chain->backbinding_list, + &level); +if (err <0 ) { +kfree (array.basechain); +return err; + } + } + +for (i =0 ; i < array.num_basechains; i++) { +struct nft_ctx ctx = { + .net = net, + .family = chain->table->family, + .table = chain->table, + .chain = (struct nft_chain *)array.basechain[i], + .level = array.max_level, + }; + +if (WARN_ON_ONCE (!nft_is_base_chain (array.basechain[i]))) +continue ; + + err =nft_chain_validate (&ctx, chain); +if (err <0 ) +break ; + } + +kfree (array.basechain); + +return err; +} + +static int nft_validate_incremental (struct net *net,struct nft_table *table) +{ +struct nftables_pernet *nft_net =nft_pernet (net);
net/netfilter/nf_tables_api.c static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { · line 10545
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+struct nft_chain *chain, *next; +int err; + + err =0 ; +list_for_each_entry_safe (chain, next, &nft_net->validate_list, validate_list) { +if (chain->table != table) +continue ; + +if (err >=0 ) + err =nft_chain_validate_incremental (net, chain);
Is there a future use-case for err > 0? I don't see where that function would return a positive value. If this is not to change, I guess just checking err || !err would simplify things a bit and also avoid the EINTR != !EINTR pitfall (see below).
Quoted 12 lines
net/netfilter/nf_tables_api.c +8
-0+ +list_del (&chain->validate_list); + chain->validate =0 ; + } + +return err; +} +static void nft_validate_chain_release (struct net *net) {struct nftables_pernet *nft_net =nft_pernet (net);
net/netfilter/nf_tables_api.c static int nf_tables_validate(struct net *net) · line 10427
Quoted 6 lines
net/netfilter/nf_tables_api.c +0
-3@@ -10422,12 +10576,36 @@ static int nf_tables_validate(struct net *net)nft_validate_state_update (table,NFT_VALIDATE_DO ); fallthrough;case NFT_VALIDATE_DO : - err =nft_table_validate (net, table); -if (err <0 ) { -if (err ==EINTR )
This should be -EINTR, right?
net/netfilter/nf_tables_api.c static int nf_tables_validate(struct net *net) · line 10586
Quoted 9 lines
net/netfilter/nf_tables_api.c +8
-1- goto err_eintr; +/* If this table is new, then this is the initial +* ruleset restore, perform full table validation, +* otherwise, perform incremental validation. +*/ +if (!nft_is_active (net, table)) { + err =nft_table_validate (net, table); +if (err <0 ) { +if (err ==EINTR )
Same here?
Quoted 8 lines
net/netfilter/nf_tables_api.c +6
-1+ goto err_eintr; -return -EAGAIN ; +return -EAGAIN ; + } + }else { + err =nft_validate_incremental (net, table); +if (err <0 ) {
net/netfilter/nf_tables_api.c static int nf_tables_validate(struct net *net) · line 10603
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+if (err != -ENOMEM && err != -ENOENT ) +return -EAGAIN ; + +/* Either no memory or it cannot reach +* basechain, then fallback to full +* validation. +*/ + err =nft_table_validate (net, table); +if (err <0 ) { +if (err ==EINTR )
And here as well?
Cheers, Phil
Quoted 13 lines
net/netfilter/nf_tables_api.c +5
-0+ goto err_eintr; + +return -EAGAIN ; + } + } }nft_validate_state_update (table,NFT_VALIDATE_SKIP );break ;Signature · 1 line
2.30.2
Florian Westphal
maintainerQuoted 2 lines · Pablo Neira Ayuso
Pablo Neira Ayuso (6):
netfilter:nf_tables: honor EINTR in ruleset validation from commit/abort path
Do this via nf.git?
Quoted 10 lines
netfilter:
nf_tables: honor validation state in preparation phase
netfilter:nf_tables: add infrastructure for chain validation on updates
netfilter:nf_tables: add new binding infrastructure
netfilter:nf_tables: use new binding infrastructure
netfilter:nf_tables: add support for validating incremental ruleset updatesinclude/net/netfilter/nf_tables.h | 52 +- net/netfilter/nf_tables_api.c | 800 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 +- 3 files changed, 844 insertions(+), 33 deletions(-)
This is a lot of new code but no explanation as to why is given.
Does this fix bugs with the existing scheme?
Or is this an optimization? If so, how big is the speedup?
Pablo Neira Ayuso
maintainerHi Florian,
Quoted 5 lines · Florian Westphal
Quoted 2 lines · Pablo Neira Ayuso
Pablo Neira Ayuso (6):
netfilter:nf_tables: honor EINTR in ruleset validation from commit/abort pathDo this via nf.git?
nf-next.git should be fine, this late in the development cycle.
Quoted 6 lines
Quoted 6 lines
netfilter:
nf_tables: honor validation state in preparation phase
netfilter:nf_tables: add infrastructure for chain validation on updates
netfilter:nf_tables: add new binding infrastructure
netfilter:nf_tables: use new binding infrastructure
netfilter:nf_tables: add support for validating incremental ruleset updates
Quoted 6 lines
Quoted 4 lines
include/net/netfilter/nf_tables.h | 52 +- net/netfilter/nf_tables_api.c | 800 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 +- 3 files changed, 844 insertions(+), 33 deletions(-)This is a lot of new code but no explanation as to why is given.
These are the results of a test program I made, which is incrementally adding elements to an already populated verdict map with 100k elements.
The ruleset validation shows in the perf callgraph, for each new element that is added:
55.06% nft-buffer [kernel.kallsyms] [k] nft_chain_validate
7.68% nft-buffer [kernel.kallsyms] [k] nf_tables_commit
7.19% nft-buffer [kernel.kallsyms] [k] nft_table_validate
5.34% nft-buffer [kernel.kallsyms] [k] nft_rhash_walk
2.82% swapper [kernel.kallsyms] [k] mwait_idle_with_hints.constprop.0
1.26% nft-buffer [kernel.kallsyms] [k] __rhashtable_walk_find_next
1.09% nft-buffer [kernel.kallsyms] [k] nft_setelem_validate
0.94% nft-buffer [kernel.kallsyms] [k] rhashtable_walk_next
0.54% nft-buffer libnftables.so.1.1.0 [.] nft_parseThis is a test adding 100 new elements including jump to chain in a existing 100k verdict map.
With this approach, there is no need to fully validate the chain graph.
Quoted 1 line
Does this fix bugs with the existing scheme?
The two initial patches are targetted at fixing minor issues. The remaining patches are new code, they are passing tests/shell with CONFIG_KASAN and CONFIG_KMEMLEAK. I will post v2 but I would like to run more fuzz test on the error path.
Quoted 1 line
Or is this an optimization? If so, how big is the speedup?
Optimization. After this series, validation does not show up near the top 10; this is the first symbol in the perf call graph:
0.03% nft-buffer [nf_tables] [k] __nft_chain_validate
and it is far from the top 10.
I can include this information in v2 so they can sit in the mailing list for a while, there is a few bugs in v1 that I have addressed. Phil has spotted one in them.
Moreover, I can move the bindings hashtable to make it per-net to control the maximum number of jump/goto per family. This is a lot larger than Shaun's update, you have to tell me your preference on this.
Old binding code can possibly go away, but that requires closer look too.
Let me know.
Thanks.
Florian Westphal
maintainerQuoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Florian Westphal
Quoted 1 line
netfilter:
nf_tables: honor EINTR in ruleset validation from commit/abort pathDo this via nf.git?
nf-next.git should be fine, this late in the development cycle.
Right, agreed.
Quoted 51 lines
Quoted 6 lines
Quoted 6 lines
netfilter:
nf_tables: honor validation state in preparation phase
netfilter:nf_tables: add infrastructure for chain validation on updates
netfilter:nf_tables: add new binding infrastructure
netfilter:nf_tables: use new binding infrastructure
netfilter:nf_tables: add support for validating incremental ruleset updatesQuoted 6 lines
Quoted 4 lines
include/net/netfilter/nf_tables.h | 52 +- net/netfilter/nf_tables_api.c | 800 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 +- 3 files changed, 844 insertions(+), 33 deletions(-)This is a lot of new code but no explanation as to why is given.
These are the results of a test program I made, which is incrementally adding elements to an already populated verdict map with 100k elements.
The ruleset validation shows in the perf callgraph, for each new element that is added:
55.06% nft-buffer [kernel.kallsyms] [k] nft_chain_validate 7.68% nft-buffer [kernel.kallsyms] [k] nf_tables_commit 7.19% nft-buffer [kernel.kallsyms] [k] nft_table_validate 5.34% nft-buffer [kernel.kallsyms] [k] nft_rhash_walk 2.82% swapper [kernel.kallsyms] [k] mwait_idle_with_hints.constprop.0 1.26% nft-buffer [kernel.kallsyms] [k] __rhashtable_walk_find_next 1.09% nft-buffer [kernel.kallsyms] [k] nft_setelem_validate 0.94% nft-buffer [kernel.kallsyms] [k] rhashtable_walk_next 0.54% nft-buffer libnftables.so.1.1.0 [.] nft_parseThis is a test adding 100 new elements including jump to chain in a existing 100k verdict map.
With this approach, there is no need to fully validate the chain graph.
Quoted 1 line
Does this fix bugs with the existing scheme?
The two initial patches are targetted at fixing minor issues. The remaining patches are new code, they are passing tests/shell with
CONFIG_KASANandCONFIG_KMEMLEAK. I will post v2 but I would like to run more fuzz test on the error path.Quoted 1 line
Or is this an optimization? If so, how big is the speedup?
Optimization. After this series, validation does not show up near the top 10; this is the first symbol in the perf call graph:
0.03% nft-buffer [
nf_tables] [k]__nft_chain_validateand it is far from the top 10.
Quoted 8 lines
I can include this information in v2 so they can sit in the mailing list for a while, there is a few bugs in v1 that I have addressed. Phil has spotted one in them.
Moreover, I can move the bindings hashtable to make it per-net to control the maximum number of jump/goto per family. This is a lot larger than Shaun's update, you have to tell me your preference on this.
I think Shauns approach is better due to backporting reasons.
Once the new code is stable enough you can move the jump limitation check there.
Makes sense?
Quoted 2 lines
Old binding code can possibly go away, but that requires closer look too.
Yes, lets keep it for now until there is confidence that the split approach catches all issues.
I suggest to do this by adding a WARN_ON_ONCE() in the old path once you think the new code should have prevented cycles in 100% of cases, then remove it after e.g. 6-12 months or so.
Pablo Neira Ayuso
maintainerQuoted 2 lines · Florian Westphal
Quoted 1 line · Pablo Neira Ayuso
On Thu, May 15, 2025 at 08:03:13AM +0200, Florian Westphal wrote:
[...]
Quoted 55 lines
Quoted 55 lines
Quoted 6 lines
Quoted 6 lines
netfilter:
nf_tables: honor validation state in preparation phase
netfilter:nf_tables: add infrastructure for chain validation on updates
netfilter:nf_tables: add new binding infrastructure
netfilter:nf_tables: use new binding infrastructure
netfilter:nf_tables: add support for validating incremental ruleset updatesQuoted 6 lines
Quoted 4 lines
include/net/netfilter/nf_tables.h | 52 +- net/netfilter/nf_tables_api.c | 800 ++++++++++++++++++++++++++++-- net/netfilter/nft_immediate.c | 25 +- 3 files changed, 844 insertions(+), 33 deletions(-)This is a lot of new code but no explanation as to why is given.
These are the results of a test program I made, which is incrementally adding elements to an already populated verdict map with 100k elements.
The ruleset validation shows in the perf callgraph, for each new element that is added:
55.06% nft-buffer [kernel.kallsyms] [k] nft_chain_validate 7.68% nft-buffer [kernel.kallsyms] [k] nf_tables_commit 7.19% nft-buffer [kernel.kallsyms] [k] nft_table_validate 5.34% nft-buffer [kernel.kallsyms] [k] nft_rhash_walk 2.82% swapper [kernel.kallsyms] [k] mwait_idle_with_hints.constprop.0 1.26% nft-buffer [kernel.kallsyms] [k] __rhashtable_walk_find_next 1.09% nft-buffer [kernel.kallsyms] [k] nft_setelem_validate 0.94% nft-buffer [kernel.kallsyms] [k] rhashtable_walk_next 0.54% nft-buffer libnftables.so.1.1.0 [.] nft_parseThis is a test adding 100 new elements including jump to chain in a existing 100k verdict map.
With this approach, there is no need to fully validate the chain graph.
Quoted 1 line
Does this fix bugs with the existing scheme?
The two initial patches are targetted at fixing minor issues. The remaining patches are new code, they are passing tests/shell with
CONFIG_KASANandCONFIG_KMEMLEAK. I will post v2 but I would like to run more fuzz test on the error path.Quoted 1 line
Or is this an optimization? If so, how big is the speedup?
Optimization. After this series, validation does not show up near the top 10; this is the first symbol in the perf call graph:
0.03% nft-buffer [
nf_tables] [k]__nft_chain_validateand it is far from the top 10.
I can include this information in v2 so they can sit in the mailing list for a while, there is a few bugs in v1 that I have addressed. Phil has spotted one in them.
Quoted 6 lines
Quoted 4 lines
Moreover, I can move the bindings hashtable to make it per-net to control the maximum number of jump/goto per family. This is a lot larger than Shaun's update, you have to tell me your preference on this.
I think Shauns approach is better due to backporting reasons.
Yes, it is small.
I could not find a way to extend the existing loop and validation routines to make it incremental.
Quoted 4 lines
Once the new code is stable enough you can move the jump limitation check there.
Makes sense?
OK.
Quoted 9 lines
Quoted 2 lines
Old binding code can possibly go away, but that requires closer look too.
Yes, lets keep it for now until there is confidence that the split approach catches all issues.
I suggest to do this by adding a
WARN_ON_ONCE()in the old path once you think the new code should have prevented cycles in 100% of cases, then remove it after e.g. 6-12 months or so.
This is how it works in v2:
On new table, the classic full ruleset validation from basechain is done.
On existing tables, from the chain that has been updated (either new rule or new jump/goto to reach it), the incremental routine backtracks to find the basechain. If chain callstack is too deep, then it falls back to the classic full ruleset validation from basechains. If it detects a loop when backtracking, it also falls back to classic full ruleset validation (it could be a loop in chains that are not connected to basechain, this is currently allowed). Then, from the chain that has been updated, call the existing nft_validate_chain() using the collected basechains that can reach such updated chain. The nft_validate_chain() function already relies on too deep callstack to detect loops.
So the backtracking is used to find and collect the basechains that can reach this chain.
Loop and checking if expression is accepted from this chain (eg. masquerade from filter chain type) is done in one single pass.
Incremental is a fast path, but in case backtracking cannot collect basechains, then it fallbacks to classic ruleset validation.
I will look into the WARN_ON_ONCE() you suggest.