diff --git a/Documentation/networking/nf_flowtable.rst b/Documentation/networking/nf_flowtable.rst
index d757c21c10f2..b9b444435775 100644
--- a/Documentation/networking/nf_flowtable.rst
+++ b/Documentation/networking/nf_flowtable.rst
@@ -205,6 +205,19 @@ refers to the software offload mode, so there is a distinction between [OFFLOAD]
which refers to the software flowtable fastpath and [HW_OFFLOAD] which refers
to the hardware offload datapath being used by the flow.
+During teardown, software fastpath lookups immediately stop matching the
+flow. Packets reaching software follow the classic forwarding path, while
+hardware deletion may still be pending. The OFFLOAD status
+bit remains set until hash removal and asynchronous hardware deletion are
+complete, preventing a new owner from racing the old owner's cleanup. As a
+result, conntrack dumps can temporarily retain [OFFLOAD] or [HW_OFFLOAD]
+and omit the TCP state after a FIN or RST has returned to the slow path.
+TCP state and timeout repair still run at first teardown. The transient
+status describes ownership pending cleanup, rather than guaranteeing that
+packets are still accelerated. It clears when garbage collection finishes
+the deletion; packet captures and forwarding counters identify the actual
+forwarding path during that interval.
+
The flowtable hardware offload infrastructure also supports for the DSA
(Distributed Switch Architecture).
diff --git a/MAINTAINERS b/MAINTAINERS
index c583367c88bc..56212dbc9a0d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19396,6 +19396,7 @@ F: net/*/netfilter.c
F: net/*/netfilter/
F: net/bridge/br_netfilter*.c
F: net/netfilter/
+F: tools/testing/kunit/configs/nf_flow_table.config
F: tools/testing/selftests/net/netfilter/
NETRONIX EMBEDDED CONTROLLER
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index 09874c26fd13..f565f87b3a6c 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -718,6 +718,16 @@ config NF_FLOW_TABLE
To compile it as a module, choose M here.
+config NF_FLOW_TABLE_KUNIT_TEST
+ bool "KUnit tests for flowtable ownership" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y && NF_FLOW_TABLE
+ default KUNIT_ALL_TESTS
+ help
+ Test conntrack offload ownership during flowtable teardown.
+ Require hash and asynchronous hardware deletion before re-enrolment.
+ Verify that late notifications cannot release a replacement owner.
+ These private tests are embedded in the flowtable core.
+
config NF_FLOW_TABLE_PROCFS
bool "Supply flow table statistics in procfs"
depends on NF_FLOW_TABLE
diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 2aa2647ed9df..4c82097cd50b 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -393,12 +393,15 @@ static void flow_offload_del(struct nf_flowtable *flow_table,
rhashtable_remove_fast(&flow_table->rhashtable,
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
nf_flow_offload_rhash_params);
+ /* Release admission only after both hashes and async hardware state
+ * have gone. An older owner must not clear its replacement's status.
+ */
+ clear_bit_unlock(IPS_OFFLOAD_BIT, &flow->ct->status);
flow_offload_free(flow);
}
void flow_offload_teardown(struct flow_offload *flow)
{
- clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
if (!test_and_set_bit(NF_FLOW_TEARDOWN, &flow->flags))
flow_offload_fixup_ct(flow);
}@@ -876,3 +879,7 @@ module_exit(nf_flow_table_module_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
MODULE_DESCRIPTION("Netfilter flow table module");
+
+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE_KUNIT_TEST)
+#include "nf_flow_table_core_test.c"
+#endifdiff --git a/net/netfilter/nf_flow_table_core_test.c b/net/netfilter/nf_flow_table_core_test.c
new file mode 100644
index 000000000000..378a1c60b50b
--- /dev/null
+++ b/net/netfilter/nf_flow_table_core_test.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <kunit/test.h>
+
+static void nf_flow_test_teardown_repeat(struct kunit *test)
+{
+ struct flow_offload flow = {};
+ struct nf_conn *ct;
+
+ ct = kunit_kzalloc(test, sizeof(*ct), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ct);
+ ct->tuplehash[0].tuple.dst.protonum = IPPROTO_ICMP;
+ flow.ct = ct;
+ set_bit(IPS_OFFLOAD_BIT, &ct->status);
+ flow_offload_teardown(&flow);
+ KUNIT_EXPECT_TRUE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+ clear_bit(IPS_OFFLOAD_BIT, &ct->status);
+ /* A late notification for this retired flow cannot clear a new claim. */
+ set_bit(IPS_OFFLOAD_BIT, &ct->status);
+ flow_offload_teardown(&flow);
+ KUNIT_EXPECT_TRUE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+}
+
+static void nf_flow_test_hardware_retirement(struct kunit *test)
+{
+ struct nf_flowtable table = {};
+ struct flow_offload *flow;
+ struct nf_conn *ct;
+ unsigned int dir;
+ int err;
+
+ ct = kunit_kzalloc(test, sizeof(*ct), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ct);
+ err = rhashtable_init(&table.rhashtable, &nf_flow_offload_rhash_params);
+ KUNIT_ASSERT_EQ(test, err, 0);
+ flow = kzalloc_obj(*flow);
+ if (!flow) {
+ rhashtable_destroy(&table.rhashtable);
+ KUNIT_FAIL(test, "flow allocation failed");
+ return;
+ }
+ ct->tuplehash[0].tuple.dst.protonum = IPPROTO_ICMP;
+ refcount_set(&ct->ct_general.use, 2);
+ flow->ct = ct;
+ set_bit(NF_FLOW_HW, &flow->flags);
+ set_bit(IPS_OFFLOAD_BIT, &ct->status);
+ set_bit(IPS_HW_OFFLOAD_BIT, &ct->status);
+ for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++) {
+ flow->tuplehash[dir].tuple.src_port = htons(10000 + dir);
+ err = rhashtable_insert_fast(&table.rhashtable,
+ &flow->tuplehash[dir].node,
+ nf_flow_offload_rhash_params);
+ KUNIT_EXPECT_EQ(test, err, 0);
+ }
+ flow_offload_teardown(flow);
+ /* nft_flow_offload must not admit another owner during async DESTROY. */
+ KUNIT_EXPECT_TRUE(test, test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status));
+ flow_offload_teardown(flow);
+ KUNIT_EXPECT_TRUE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+ /* Model completed hardware deletion before the GC removes the hashes. */
+ clear_bit(IPS_HW_OFFLOAD_BIT, &ct->status);
+ set_bit(NF_FLOW_HW_DEAD, &flow->flags);
+ rcu_read_lock();
+ flow_offload_del(&table, flow);
+ KUNIT_EXPECT_FALSE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+ /* Re-enrolment is safe now; an old RCU reader still cannot undo it. */
+ set_bit(IPS_OFFLOAD_BIT, &ct->status);
+ flow_offload_teardown(flow);
+ KUNIT_EXPECT_TRUE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+ rcu_read_unlock();
+ rcu_barrier();
+ KUNIT_EXPECT_EQ(test, refcount_read(&ct->ct_general.use), 1);
+ rhashtable_destroy(&table.rhashtable);
+}
+
+static struct kunit_case nf_flow_lifetime_cases[] = {
+ KUNIT_CASE(nf_flow_test_teardown_repeat),
+ KUNIT_CASE(nf_flow_test_hardware_retirement),
+ {}
+};
+
+static struct kunit_suite nf_flow_lifetime_suite = {
+ .name = "nf-flow-lifetime",
+ .test_cases = nf_flow_lifetime_cases,
+};
+
+kunit_test_suite(nf_flow_lifetime_suite);diff --git a/tools/testing/kunit/configs/nf_flow_table.config b/tools/testing/kunit/configs/nf_flow_table.config
new file mode 100644
index 000000000000..7f2aa55190c2
--- /dev/null
+++ b/tools/testing/kunit/configs/nf_flow_table.config
@@ -0,0 +1,9 @@
+CONFIG_KUNIT=y
+CONFIG_NET=y
+CONFIG_INET=y
+CONFIG_NETFILTER=y
+CONFIG_NETFILTER_INGRESS=y
+CONFIG_NF_CONNTRACK=y
+CONFIG_NF_TABLES=y
+CONFIG_NF_FLOW_TABLE=y
+CONFIG_NF_FLOW_TABLE_KUNIT_TEST=y
--
2.53.0