Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.70.190.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796122763; b=gJcx1BAOEVlvHZvFxZiiLd65sqINKbVx+rZgKWMego4ysrUbiBeDYQfGLM44AvQLOZ3k ZUD9Wa8iTu1OWjjNx0qvFgBGFHxmW114OwdGuF3P3dn6B3vLCXt/CcK3ZY6Pvv/X06dIu /SD/4e99KXc6Rt9NETn0QNbxZBsbyghn+isaxocw39XbF12OK5O0ksEW2dewdpYRV04KK cA5l/S9WytTNpIptmbeb8H1M4fSWSLnPgTGNl1ljXCIwzPVG2/dW8RgWDKDfbkNMFPWfV H8cXUMmql8IjkS3VgPhaDobdvjois7T8zm88mw9rQ4NbWINMVhBZqeS5ESL8rohAGIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796122763; h=DMARC-Filter:DKIM-Signature:Received:From:To:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Spam-Status:X-Spam-Checker-Version; bh=TfQ64A5DtfEig0OFQWCL7PKfpcmov+UYUMdsDmz3QnQ=; b=5TTMr0XoAXcxAlR3gUZAxhQ+7O7aiCkDsRrKCc0vhpzKl4cCdPZrY9LfnE8fBQVA73cw tIvoAD77PZSYQtbZUwye10ezC0nNKi+5ESNoOJuVxzGcmgGc0+y57E5JadM+HJdSxVyT1 KYBU5FgXCSbc4CzZahaXdCxWwR0+3qTEq5VK3SnwLdPgTdZFGemUb/b+VB48EGSw2CaPw MSPMx8vZrTw8egAv9K+XGfz2cDbGYddF4fC8aMckQtzDxH8hU2XGZXKCxGRveXUPU15CI 4V0vRqJ3NAMORLu7CFa++W/1NJyvB6XyPZcXQWP7/XLG70/AW4EL8zt5kmJkQdjkKLA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; dkim=pass header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b="A/akN75d"; arc=none smtp.remote-ip=217.70.190.124 Received: from mail.netfilter.org (mail.netfilter.org [217.70.190.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5C9C606A32E for ; Tue, 1 Dec 2026 10:59:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B5C9C606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=A/akN75d DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netfilter.org; s=2025; t=1796122760; bh=TfQ64A5DtfEig0OFQWCL7PKfpcmov+UYUMdsDmz3QnQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=A/akN75dSQnAGSRpaZ82An+CMk0odEzoVRBE9qciu/FyxFYDiKRL/LzS0vaXIdZn/ xwcs1D7fej1gtvMJ9UE8EubEeZ97y28GQ7jVKPxEenrwRsQo4q34r+eUAvU+Mr0L/O 5Hx/UXH65ATzp2VNzSDQt5LOYRte6eixb32ZT0rYGOpOhczHEiLa8YqVlDu73eiWm+ tKO58f2bEyWMz1wRE/z+589YGyeQXjjYHdNAf9zSa6grOnbsREm7d/vAkCYiTT+gdF pL1DZQbiCO5OkFKUoXzOR7NvUIm7n5cM5fhKq7da2wD4lu1wmBOZNAIbLRMuanBBQG EYM2GEPCg71xA== Received: from localhost.localdomain (mail-agni [217.70.190.124]) by mail.netfilter.org (Postfix) with ESMTPSA id 951886007C for ; Tue, 1 Dec 2026 11:59:20 +0100 (CET) From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Subject: [PATCH nf,v5 4/5] netfilter: nf_tables: clean up flowtable entries in hardware on updates Date: Tue, 1 Dec 2026 11:59:11 +0100 Message-ID: <20261201105912.1111000-4-pablo@netfilter.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201105912.1111000-1-pablo@netfilter.org> References: <20261201105912.1111000-1-pablo@netfilter.org> X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Flowtable updates adding or deleting net_devices result in leaving entries behind in the hardware. This patch completes the flowtable update support by cleaning up stale entries per net_device in two scenarios: - If an existing net_device is deleted from the flowtable. - If a new net_device is added, but then transaction is aborted. Fixes: 78d9f48f7f44 ("netfilter: nf_tables: add devices to existing flowtable") Fixes: abadb2f865d7 ("netfilter: nf_tables: delete devices from flowtable") Signed-off-by: Pablo Neira Ayuso --- v5: new in the series. include/net/netfilter/nf_flow_table.h | 5 +++ net/netfilter/nf_flow_table_inet.c | 3 ++ net/netfilter/nf_flow_table_offload.c | 11 +++++++ net/netfilter/nf_tables_api.c | 44 +++++++++++++++++++++++---- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/include/net/netfilter/nf_flow_table.h b/include/net/netfilter/nf_flow_table.h index 5b22d31cb140..a4750882c115 100644 --- a/include/net/netfilter/nf_flow_table.h +++ b/include/net/netfilter/nf_flow_table.h @@ -57,6 +57,8 @@ struct nf_flowtable_type { int (*setup)(struct nf_flowtable *ft, struct net_device *dev, enum flow_block_command cmd); + void (*cleanup)(struct nf_flowtable *ft, + struct net_device *dev); int (*action)(struct net *net, struct flow_offload *flow, enum flow_offload_tuple_dir dir, @@ -380,6 +382,9 @@ void nf_flow_table_offload_flush_cleanup(struct nf_flowtable *flowtable); int nf_flow_table_offload_setup(struct nf_flowtable *flowtable, struct net_device *dev, enum flow_block_command cmd); +void nf_flow_table_offload_cleanup(struct nf_flowtable *flowtable, + struct net_device *dev); + int nf_flow_rule_route_ipv4(struct net *net, struct flow_offload *flow, enum flow_offload_tuple_dir dir, struct nf_flow_rule *flow_rule); diff --git a/net/netfilter/nf_flow_table_inet.c b/net/netfilter/nf_flow_table_inet.c index b0f199171932..c296249812fe 100644 --- a/net/netfilter/nf_flow_table_inet.c +++ b/net/netfilter/nf_flow_table_inet.c @@ -69,6 +69,7 @@ static struct nf_flowtable_type flowtable_inet = { .family = NFPROTO_INET, .init = nf_flow_table_init, .setup = nf_flow_table_offload_setup, + .cleanup = nf_flow_table_offload_cleanup, .action = nf_flow_rule_route_inet, .free = nf_flow_table_free, .hook = nf_flow_offload_inet_hook, @@ -79,6 +80,7 @@ static struct nf_flowtable_type flowtable_ipv4 = { .family = NFPROTO_IPV4, .init = nf_flow_table_init, .setup = nf_flow_table_offload_setup, + .cleanup = nf_flow_table_offload_cleanup, .action = nf_flow_rule_route_ipv4, .free = nf_flow_table_free, .hook = nf_flow_offload_ip_hook, @@ -89,6 +91,7 @@ static struct nf_flowtable_type flowtable_ipv6 = { .family = NFPROTO_IPV6, .init = nf_flow_table_init, .setup = nf_flow_table_offload_setup, + .cleanup = nf_flow_table_offload_cleanup, .action = nf_flow_rule_route_ipv6, .free = nf_flow_table_free, .hook = nf_flow_offload_ipv6_hook, diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c index 4365859220e6..89a2e310776c 100644 --- a/net/netfilter/nf_flow_table_offload.c +++ b/net/netfilter/nf_flow_table_offload.c @@ -1268,6 +1268,17 @@ static int nf_flow_table_offload_cmd(struct flow_block_offload *bo, return 0; } +void nf_flow_table_offload_cleanup(struct nf_flowtable *flowtable, + struct net_device *dev) +{ + if (!nf_flowtable_hw_offload(flowtable)) + return; + + nf_flow_table_offload_flush(flowtable); + nf_flow_table_gc_cleanup(flowtable, dev); +} +EXPORT_SYMBOL_GPL(nf_flow_table_offload_cleanup); + int nf_flow_table_offload_setup(struct nf_flowtable *flowtable, struct net_device *dev, enum flow_block_command cmd) diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index 4f48a11aeb9f..f856b1daff37 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -9149,17 +9149,14 @@ static void nft_flowtable_unregister_trans_hook(struct net *net, struct nft_flowtable *flowtable, struct list_head *hook_list) { - struct nft_trans_hook *trans_hook, *next; + struct nft_trans_hook *trans_hook; struct nf_hook_ops *ops; struct nft_hook *hook; - list_for_each_entry_safe(trans_hook, next, hook_list, list) { + list_for_each_entry(trans_hook, hook_list, list) { hook = trans_hook->hook; list_for_each_entry(ops, &hook->ops_list, list) nft_unregister_flowtable_ops(net, ops); - - nft_flowtable_hook_unlink_free_rcu(flowtable, trans_hook->hook); - nft_trans_hook_destroy(trans_hook); } } @@ -9769,6 +9766,24 @@ static void nf_tables_flowtable_notify(struct nft_ctx *ctx, nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); } +static void nft_flowtable_offload_cleanup(struct nft_flowtable *flowtable, + struct nft_hook *hook) +{ + struct nf_hook_ops *ops; + + list_for_each_entry(ops, &hook->ops_list, list) + flowtable->data.type->cleanup(&flowtable->data, ops->dev); +} + +static void nft_flowtable_hooks_cleanup(struct nft_flowtable *flowtable, + struct list_head *hook_list) +{ + struct nft_hook *hook; + + list_for_each_entry(hook, hook_list, list) + nft_flowtable_offload_cleanup(flowtable, hook); +} + static void nft_flowtable_hooks_destroy(struct nft_flowtable *flowtable, struct list_head *hook_list) { @@ -10290,6 +10305,18 @@ static void nft_obj_commit_update(const struct nft_ctx *ctx, nft_obj_destroy(ctx, newobj); } +static void nft_trans_flowtable_destroy(struct nft_flowtable *flowtable, + struct list_head *trans_hook_list) +{ + struct nft_trans_hook *trans_hook, *next; + + list_for_each_entry_safe(trans_hook, next, trans_hook_list, list) { + nft_flowtable_offload_cleanup(flowtable, trans_hook->hook); + nft_flowtable_hook_unlink_free_rcu(flowtable, trans_hook->hook); + nft_trans_hook_destroy(trans_hook); + } +} + static void nft_commit_release(struct nft_trans *trans) { struct nft_ctx ctx = { @@ -10330,7 +10357,10 @@ static void nft_commit_release(struct nft_trans *trans) break; case NFT_MSG_DELFLOWTABLE: case NFT_MSG_DESTROYFLOWTABLE: - if (!nft_trans_flowtable_update(trans)) + if (nft_trans_flowtable_update(trans)) + nft_trans_flowtable_destroy(nft_trans_flowtable(trans), + &nft_trans_flowtable_hooks(trans)); + else nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); break; } @@ -11345,6 +11375,8 @@ static void nf_tables_abort_release(struct nft_trans *trans) break; case NFT_MSG_NEWFLOWTABLE: if (nft_trans_flowtable_update(trans)) { + nft_flowtable_hooks_cleanup(nft_trans_flowtable(trans), + &nft_trans_flowtable_hooks(trans)); nft_flowtable_hooks_destroy(nft_trans_flowtable(trans), &nft_trans_flowtable_hooks(trans)); } else { -- 2.47.3