Article View: linux.kernel.netdev
Article #24015[PATCH net 11/14] netfilter: nf_tables: add NFT_HOOK_UNREGISTERED and use it
From: Pablo Neira Ayuso
Date: Sun, 29 Nov 2026 00:07
Date: Sun, 29 Nov 2026 00:07
219 lines
16144 bytes
16144 bytes
Commit f9a43007d3f7 ("netfilter: nf_tables: double hook unregistration
in netns path") fixes a race in the netns exit path with netdev device
event handler. The netns pre_exit handler unregisters the hooks, then
exit release them. In between this two calls, the netdev event might
show up and try to unregister the hooks again. This patch revisit this
fix by adding a new NFT_HOOK_UNREGISTERED flag which allows us to skip
the hook from the netdevice and flowtable event handlers.
This patch is a prerequisit for another follow up fix for the flowtable
hardware offload which allows to unregister the flow_block once the
entries in hardware have been removed.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
include/net/netfilter/nf_tables.h | 3 +-
net/netfilter/nf_tables_api.c | 53 +++++++++++++++----------------
net/netfilter/nft_chain_filter.c | 6 ++--
3 files changed, 31 insertions(+), 31 deletions(-)
diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
index 9d597482363d..5edff5d7dacd 100644
--- a/include/net/netfilter/nf_tables.h
+++ b/include/net/netfilter/nf_tables.h
@@ -1209,7 +1209,8 @@ struct nft_stats {
struct u64_stats_sync syncp;
};
-#define NFT_HOOK_REMOVE (1 << 0)
+#define NFT_HOOK_REMOVE (1 << 0)
+#define NFT_HOOK_UNREGISTERED (1 << 1)
struct nft_hook {
struct list_head list;
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index b184c739cb67..c19cf6d8cd0b 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -329,6 +329,7 @@ static int nft_netdev_register_hooks(struct net *net,
j++;
}
+ hook->flags &= ~NFT_HOOK_UNREGISTERED;
}
return 0;
@@ -340,6 +341,7 @@ static int nft_netdev_register_hooks(struct net *net,
nf_unregister_net_hook(net, ops);
}
+ hook->flags |= NFT_HOOK_UNREGISTERED;
}
return err;
}
@@ -406,19 +408,17 @@ static void nft_netdev_unregister_trans_hook(struct net *net,
static void nft_netdev_unregister_hooks(struct net *net,
const struct nft_table *table,
- struct list_head *hook_list,
- bool release_netdev)
+ struct list_head *hook_list)
{
- struct nft_hook *hook, *next;
+ struct nft_hook *hook;
struct nf_hook_ops *ops;
- list_for_each_entry_safe(hook, next, hook_list, list) {
+ list_for_each_entry(hook, hook_list, list) {
if (!(table->flags & NFT_TABLE_F_DORMANT)) {
list_for_each_entry(ops, &hook->ops_list, list)
nf_unregister_net_hook(net, ops);
}
- if (release_netdev)
- nft_netdev_hook_unlink_free_rcu(hook);
+ hook->flags |= NFT_HOOK_UNREGISTERED;
}
}
@@ -447,8 +447,7 @@ static int nf_tables_register_hook(struct net *net,
static void __nf_tables_unregister_hook(struct net *net,
const struct nft_table *table,
- struct nft_chain *chain,
- bool release_netdev)
+ struct nft_chain *chain)
{
struct nft_base_chain *basechain;
const struct nf_hook_ops *ops;
@@ -460,8 +459,7 @@ static void __nf_tables_unregister_hook(struct net *net,
/* must also be called for dormant tables */
if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
- nft_netdev_unregister_hooks(net, table, &basechain->hook_list,
- release_netdev);
+ nft_netdev_unregister_hooks(net, table, &basechain->hook_list);
return;
}
@@ -478,7 +476,7 @@ static void nf_tables_unregister_hook(struct net *net,
const struct nft_table *table,
struct nft_chain *chain)
{
- return __nf_tables_unregister_hook(net, table, chain, false);
+ return __nf_tables_unregister_hook(net, table, chain);
}
static bool nft_trans_collapse_set_elem_allowed(const struct nft_trans_elem *a, const struct nft_trans_elem *b)
@@ -9038,8 +9036,7 @@ static void nft_unregister_flowtable_ops(struct net *net,
static void __nft_unregister_flowtable_net_hooks(struct net *net,
struct nft_flowtable *flowtable,
- struct list_head *hook_list,
- bool release_netdev)
+ struct list_head *hook_list)
{
struct nft_hook *hook, *next;
struct nf_hook_ops *ops;
@@ -9047,8 +9044,8 @@ static void __nft_unregister_flowtable_net_hooks(struct net *net,
list_for_each_entry_safe(hook, next, hook_list, list) {
list_for_each_entry(ops, &hook->ops_list, list)
nft_unregister_flowtable_ops(net, flowtable, ops);
- if (release_netdev)
- nft_netdev_hook_unlink_free_rcu(hook);
+
+ hook->flags |= NFT_HOOK_UNREGISTERED;
}
}
@@ -9056,7 +9053,7 @@ static void nft_unregister_flowtable_net_hooks(struct net *net,
struct nft_flowtable *flowtable,
struct list_head *hook_list)
{
- __nft_unregister_flowtable_net_hooks(net, flowtable, hook_list, false);
+ __nft_unregister_flowtable_net_hooks(net, flowtable, hook_list);
}
static int nft_register_flowtable_ops(struct net *net,
@@ -9833,8 +9830,10 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,
continue;
/* flow_offload_netdev_event() cleans up entries for us. */
- nft_unregister_flowtable_ops(dev_net(dev),
- flowtable, ops);
+ if (!(hook->flags & NFT_HOOK_UNREGISTERED)) {
+ nft_unregister_flowtable_ops(dev_net(dev),
+ flowtable, ops);
+ }
list_del_rcu(&ops->list);
kfree_rcu(ops, rcu);
break;
@@ -11291,10 +11290,13 @@ static void nf_tables_abort_release(struct nft_trans *trans)
nf_tables_table_destroy(trans->table);
break;
case NFT_MSG_NEWCHAIN:
- if (nft_trans_chain_update(trans))
+ if (nft_trans_chain_update(trans)) {
nft_hooks_destroy(&nft_trans_chain_hooks(trans));
- else
+ free_percpu(nft_trans_chain_stats(trans));
+ kfree(nft_trans_chain_name(trans));
+ } else {
nf_tables_chain_destroy(nft_trans_chain(trans));
+ }
break;
case NFT_MSG_NEWRULE:
nf_tables_rule_destroy(&ctx, nft_trans_rule(trans));
@@ -11383,11 +11385,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
case NFT_MSG_NEWCHAIN:
if (nft_trans_chain_update(trans)) {
nft_netdev_unregister_hooks(net, table,
- &nft_trans_chain_hooks(trans),
- true);
- free_percpu(nft_trans_chain_stats(trans));
- kfree(nft_trans_chain_name(trans));
- nft_trans_destroy(trans);
+ &nft_trans_chain_hooks(trans));
} else {
if (nft_trans_chain_bound(trans)) {
nft_trans_destroy(trans);
@@ -12048,11 +12046,10 @@ static void __nft_release_hook(struct net *net, struct nft_table *table)
struct nft_chain *chain;
list_for_each_entry(chain, &table->chains, list)
- __nf_tables_unregister_hook(net, table, chain, true);
+ __nf_tables_unregister_hook(net, table, chain);
list_for_each_entry(flowtable, &table->flowtables, list)
__nft_unregister_flowtable_net_hooks(net, flowtable,
- &flowtable->hook_list,
- true);
+ &flowtable->hook_list);
}
static void __nft_release_hooks(struct net *net)
diff --git a/net/netfilter/nft_chain_filter.c b/net/netfilter/nft_chain_filter.c
index e04e689f2228..ffbf5dad3139 100644
--- a/net/netfilter/nft_chain_filter.c
+++ b/net/netfilter/nft_chain_filter.c
@@ -337,7 +337,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
if (!ops || (changename && match))
continue;
- if (!(table->flags & NFT_TABLE_F_DORMANT))
+ if (!(table->flags & NFT_TABLE_F_DORMANT) &&
+ !(hook->flags & NFT_HOOK_UNREGISTERED))
nf_unregister_net_hook(dev_net(dev), ops);
list_del_rcu(&ops->list);
@@ -356,7 +357,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
ops->dev = dev;
- if (!(table->flags & NFT_TABLE_F_DORMANT) &&
+ if ((!(table->flags & NFT_TABLE_F_DORMANT) &&
+ !(hook->flags & NFT_HOOK_UNREGISTERED)) &&
nf_register_net_hook(dev_net(dev), ops)) {
kfree(ops);
return 1;
--
2.47.3
Technical Details
Message-ID:
<20261129000724.797425-12-pablo@netfilter.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373308-linux-kernel-netdev=dewy.ruReferences:
<20261129000724.797425-1-pablo@netfilter.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373308-linux-kernel-netdev=dewy.ru From: pablo@netfilter.org (Pablo Neira Ayuso) Newsgroups: linux.kernel.netdev Subject: [PATCH net 11/14] netfilter: nf_tables: add NFT_HOOK_UNREGISTERED and use it Date: Sun, 29 Nov 2026 01:07:21 +0100 Organization: Usenet Network Message-ID: <20261129000724.797425-12-pablo@netfilter.org> References: <20261129000724.797425-1-pablo@netfilter.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1064679"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:SOb7gwPldEzTj+VeNzB9TF3g1Co= sha256:i/FlWJG0JiSh855S9bzGpYYqGUlFRBKZBmmSsHIfvr0= sha1:oK/PBJXqavkMjZGoUZLI3pDy/Fk= sha256:Q+epL2N03ItH21taozJ5M1KyEEjM7xIuvW1TNNMQR6g= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id CFA58125F8E; Sun, 29 Nov 2026 03:08:29 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=netfilter.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eG0X5-taY_M4; Sun, 29 Nov 2026 03:08:26 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-373308-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=netfilter.org X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id A8AC4125F8A; Sun, 29 Nov 2026 03:08:18 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id C91C5295EF; Sun, 29 Nov 2026 00:07:56 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail; t=1795910869; b=sXwfgzMhLJ7HhqgPoqONuW0Z9UsEPsVNt/4rc+utgtHGv7/XNkXU6MB7iQZS6XOuLMkF AWjG0rqjiZHiGVhIytCLMPggYDU6qOLJoZ5iKVMByQ5JY6JoT+3OvijQ1TCCStUUY0Vfs cy675gog6qgDvDrZrfSxL/oxC9rnM/aPbOu7KHvl6wOk2fpTdeUpi+YSpaV8prZPBleCV mkc2a+DWbdYCO11yIv5KBRS3aIoMEz70EiMR6LkpHV3pV3ltAoDK639shaRrASjbVvrMB ofRTjXOKdZzK9U5hd1bYZebgstpYhImbmGGrNnhykXyKo5zWxG3Ttt5nf83zIkOTCqw== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910869; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Received:From:To: Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=y1uSuP3U2jBDK8Zs0H7U9AjM4GOL5QOTrRtyApwLSwg=; b=gDxovdNHZG6BJV9Dy1D0KF7AV/ygXHhZ3pIkmCYESbFXT7rF6ebtK6CXo0i2UAEMYUs+ h8XWufiLSFlMRwGuEE1kvIbWMDH+SPjvgWlGNkyFfIDzXcw/I+eldyFGerEP8mPp/XLTp HEgR3KohrnYAQ2tSNC+4PRoqsXjX2vxWQtELriX4jFBc5rFyiW9sQIN7xnV8deQd9j388 E/mQE745umhDyBb2esd20GB1xTHWaWDyFO1vmIa9WPfxKGpXrpzhmNMtyInud0+2EL6lb W9Qo7q4Z9yaevF8PRm8RpyMjNJVay43HkPlSOHKAIfdbfgv9tDGokhoxW60eWW3i1Eg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=WXQl7YWw; dmarc=none header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; arc=fail smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13C0A606A32E; Sun, 29 Nov 2026 00:07:49 +0000 (UTC) X-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=WXQl7YWw X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.70.190.124 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795910868; b=Ml6kM26nSj+JmxyAnHCEFBxlobrG86ay6Bn6xTvgKHDxI0WrT31CRy1gmybBwbAM5cuB asQKwwc7/I7mv4TCmfj2sTskp9nHNc+7B4Uy6J/Wl171aGAV6qWToOSJbnTvaN4P/PJbG ULtmg3YtmLxBP5wiMUfMvSGx+Ci7ENQDizTJDLzsYeQBSUpVSZC82z9SiMlkLdF7oEgZH oZSd3GxKdPWP5XOGvnC1PXGyvYAlXEiPn9tHBVdvy9MB5UuVj4ztnaP0DpNMVYTbJVlEV /nfs4cokHLTpPP4pfFeeWhRZDG+DluYjq75PoFrmVSqXyQ0WWYCcmKK7VGwjAjHTXag== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910868; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Spam-Status:X-Spam-Checker-Version; bh=y1uSuP3U2jBDK8Zs0H7U9AjM4GOL5QOTrRtyApwLSwg=; b=SF+laSdGdjFDEyx9PlI7Ow6PRJvmRaZDoLrBIFlJsyutJU0bj3sZjBinvE9X7cXIPOSL 2HNLBuNPR94NxH/90glvPT5WOd6ClYfYU+Ya87xqmNV/TiseyfewNDJxd5pdJ+/ON5lgm YMSvkLiOJuJ9wa1I1o9XmIMzAMbBgk8W0KV/59eUGDabgudJQ8l9z4KVs7bM2ugRfZydt cT1FUmgfzzautIdSQ0uw3nQ2pfI8hXlM+qAR0vNpi9fZHEsV2lfGsUs501DhaI6iZfo/7 psOKiAx06g4u540UblgsOzHPe+BC98Lp/EmN9HnDybj7zMbHkvaOwnXu/PWwMP8/E5A== X-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=WXQl7YWw; arc=none smtp.remote-ip=217.70.190.124 X-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 B48EC606A681; Sun, 29 Nov 2026 00:07:47 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B48EC606A681 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netfilter.org; s=2025; t=1795910865; bh=y1uSuP3U2jBDK8Zs0H7U9AjM4GOL5QOTrRtyApwLSwg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WXQl7YWwg57ZmhIp4K8avBa8hU99bwwKI/EzIhcWMhTdCzSQ2IQWkrlzhSbOLQ2xk szSGPcnQmEfST4chR9+rXNKVMt5Q/BVqRHt9yw9NRINvX7PJAcQN+SioqWpbeUtg5b 9/hOukRv+ruLf003oAAKPq6CL42bDALh82OtdwKL1j1ASN+s5co2pbQwXm2tsVyytq GR18IHEPpv6he+oSGPiMhPRzpkjUU0Hu2/3QHCMY7/dbNChuNkv77FJw+7ld9ysvpZ xaC6tj/lhekICfocEHyjUxvkT4j0g0EJLYkPhZNZ95p759bwwuZ/33mlfH7nmZ7jsd mydgyQxA2NjHg== X-Received: from localhost.localdomain (mail-agni [217.70.190.124]) by mail.netfilter.org (Postfix) with ESMTPSA id 32D9A601B7; Sun, 29 Nov 2026 01:07:45 +0100 (CET) X-Cc: davem@davemloft.net, netdev@vger.kernel.org, kuba@kernel.org, pabeni@redhat.com, edumazet@kernel.org, horms@kernel.org, fw@strlen.de, ja@ssi.bg X-Mailer: git-send-email 2.47.3 X-In-Reply-To: <20261129000724.797425-1-pablo@netfilter.org> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list