Skip to contentSkip to conversation

netfilter: nf_tables: add infrastructure for chain validation on updates

PATCH New Pablo Neira Ayuso 1 participant 4 messages May 20, 2025

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/4 netfilter: nf_tables: add infrastructure for chain validation on updates

Add 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.

---
v2: no changes

 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
index 803d5f1601f9..d391990d1a96 100644
@@ -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
author maintainer
#1 lore raw

PATCH v2 2/4 netfilter: nf_tables: add new binding infrastructure

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.

---
v2: no changes

 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
index d391990d1a96..807097746d24 100644
@@ -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
author maintainer
#2 lore raw

PATCH v2 3/4 netfilter: nf_tables: use new binding infrastructure

In reply to 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.

---
v2: no changes

 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
index 807097746d24..c33820fc1abd 100644
@@ -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
index 02ee5fb69871..0b886725ed37 100644
@@ -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
author maintainer
#3 lore raw

PATCH v2 4/4 netfilter: nf_tables: add support for validating incremental ruleset updates

In reply to 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.

---
v2: nft_chain_validate_backtrack() bails out if chain stack is too deep
or loop is detected, then fallback to full validation. Loops in chains that are not linked to basechains are allowed by now, so let's fall back to full validation when is loop detected or chain stack is too deep.

 net/netfilter/nf_tables_api.c | 196 +++++++++++++++++++++++++++++++++-
 1 file changed, 191 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +191 -5
index 1a3bc7857f78..ee4bd201ca05 100644
@@ -10397,6 +10397,168 @@ static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
 	},
 };
 
+struct nft_validate_ctx {
+	const struct nft_chain	*chain;
+	int			level;
+
+	const struct nft_chain	**basechain;
+	uint32_t		num_basechains;
+	uint32_t		max_basechains;
+	int			max_level;
+};
+
+static int nft_basechain_array_add(struct nft_validate_ctx *ctx,
+				   const struct nft_chain *chain)
+{
+	const struct nft_chain **new_basechain;
+	uint32_t new_max_basechains;
+
+	if (ctx->num_basechains == ctx->max_basechains) {
+		new_max_basechains = ctx->max_basechains + 16;
+		new_basechain = krealloc_array(ctx->basechain,
+					       new_max_basechains,
+					       sizeof(struct nft_chain *),
+					       GFP_KERNEL);
+		if (!new_basechain)
+			return -ENOMEM;
+
+		ctx->basechain = new_basechain;
+		ctx->max_basechains = new_max_basechains;
+	}
+	ctx->basechain[ctx->num_basechains++] = chain;
+
+	if (ctx->level > ctx->max_level)
+		ctx->max_level = ctx->level;
+
+	return 0;
+}
+
+static int nft_chain_validate_backtrack(struct nft_validate_ctx *ctx,
+					const struct list_head *backbinding_list)
+{
+	struct nft_binding *binding;
+	int err;
+
+	/* Basechain is unreachable, fall back to slow path validation. */
+	if (ctx->level >= NFT_JUMP_STACK_SIZE)
+		return -ELOOP;
+
+	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 (nft_basechain_array_add(ctx, binding->from.chain) < 0)
+				return -ENOMEM;
+
+			continue;
+		}
+
+		switch (binding->from.type) {
+		case NFT_BIND_CHAIN:
+			if (binding->use == 0)
+				break;
+
+			if (ctx->chain == binding->from.chain)
+				return -ELOOP;
+
+			ctx->level++;
+			err = nft_chain_validate_backtrack(ctx,
+							   &binding->from.chain->backbinding_list);
+			if (err < 0)
+				return err;
+
+			ctx->level--;
+			break;
+		case NFT_BIND_SET:
+			if (binding->use == 0)
+				break;
+
+			/* no level update for sets. */
+			err = nft_chain_validate_backtrack(ctx,
+							   &binding->from.set->backbinding_list);
+			if (err < 0)
+				return err;
+
+			break;
+		}
+	}
+
+	return 0;
+}
+
+static int nft_chain_validate_incremental(struct net *net,
+					  const struct nft_chain *chain)
+{
+	struct nft_validate_ctx validate_ctx = {
+		.chain	= chain,
+	};
+	uint32_t i;
+	int err;
+
+	validate_ctx.max_basechains = 16;
+	validate_ctx.basechain = kcalloc(16, sizeof(struct nft_chain *), GFP_KERNEL);
+	if (!validate_ctx.basechain)
+		return -ENOMEM;
+
+	if (nft_is_base_chain(chain)) {
+		err = nft_basechain_array_add(&validate_ctx, chain);
+		if (err < 0) {
+			kfree(validate_ctx.basechain);
+			return -ENOMEM;
+		}
+	} else {
+		validate_ctx.level = 1;
+		err = nft_chain_validate_backtrack(&validate_ctx,
+						   &chain->backbinding_list);
+		if (err < 0) {
+			kfree(validate_ctx.basechain);
+			return err;
+		}
+	}
+
+	for (i = 0; i < validate_ctx.num_basechains; i++) {
+		struct nft_ctx ctx = {
+			.net	= net,
+			.family	= chain->table->family,
+			.table	= chain->table,
+			.chain	= (struct nft_chain *)validate_ctx.basechain[i],
+			.level	= validate_ctx.max_level,
+		};
+
+		if (WARN_ON_ONCE(!nft_is_base_chain(validate_ctx.basechain[i])))
+			continue;
+
+		err = nft_chain_validate(&ctx, chain);
+		if (err < 0)
+			break;
+	}
+
+	kfree(validate_ctx.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 +10584,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 != -ELOOP)
+						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