Skip to contentSkip to conversation

selftests: netfilter: conntrack respect reject rules

PATCH Superseded Antonio Ojea 3 participants 9 messages Mar 13, 2025 – Mar 23, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: netfilter: conntrack respect reject rules

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test then performs the following steps:

1. Establishes a connection from ns1 to ns2 (direct connection).
2. Establishes a persistent connection from ns1 to the virtual IP

    (DNAT to ns2).

3. Establishes another connection from ns1 to the virtual IP.
4. Adds an nftables rule in nsrouter to reject traffic destined to ns2 on the service port.
5. Verifies that subsequent connections from ns1 to ns2 and the virtual IP are rejected.
6. Verifies that the established persistent connection from ns1 to the virtual IP is also closed.

---
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 tools/testing/selftests/net/netfilter/config  |   1 +
 .../nft_conntrack_reject_established.sh       | 272 ++++++++++++++++++
 3 files changed, 274 insertions(+)
 create mode 100755 tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
index ffe161fac8b5..c276b8ac2383 100644
@@ -21,6 +21,7 @@ TEST_PROGS += nf_nat_edemux.sh
 TEST_PROGS += nft_audit.sh
 TEST_PROGS += nft_concat_range.sh
 TEST_PROGS += nft_conntrack_helper.sh
+TEST_PROGS += nft_conntrack_reject_established.sh
 TEST_PROGS += nft_fib.sh
 TEST_PROGS += nft_flowtable.sh
 TEST_PROGS += nft_meta.sh
tools/testing/selftests/net/netfilter/config +1 -0
index 43d8b500d391..44ed1a7eb0b5 100644
@@ -81,6 +81,7 @@ CONFIG_NFT_NUMGEN=m
 CONFIG_NFT_QUEUE=m
 CONFIG_NFT_QUOTA=m
 CONFIG_NFT_REDIR=m
+CONFIG_NFT_REJECT=m
 CONFIG_NFT_SYNPROXY=m
 CONFIG_NFT_TPROXY=m
 CONFIG_VETH=m
tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh new file +272 -0

272 lines not on this page · raw message

Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
#1 lore raw

PATCH v2 1/1 selftests: netfilter: conntrack respect reject rules

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test validates that is possible to filter and reject new and established connections to the DNATed IP in the prerouting and forward filters.

---
V1 -> V2:
* Modified the test function to accept a third argument which contains the nftables rules to be applied.

* Add a new test case to filter and reject in the prerouting hook.

---
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 tools/testing/selftests/net/netfilter/config  |   1 +
 .../nft_conntrack_reject_established.sh       | 294 ++++++++++++++++++
 3 files changed, 296 insertions(+)
 create mode 100755 tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
index ffe161fac8b5..c276b8ac2383 100644
@@ -21,6 +21,7 @@ TEST_PROGS += nf_nat_edemux.sh
 TEST_PROGS += nft_audit.sh
 TEST_PROGS += nft_concat_range.sh
 TEST_PROGS += nft_conntrack_helper.sh
+TEST_PROGS += nft_conntrack_reject_established.sh
 TEST_PROGS += nft_fib.sh
 TEST_PROGS += nft_flowtable.sh
 TEST_PROGS += nft_meta.sh
tools/testing/selftests/net/netfilter/config +1 -0
index 43d8b500d391..44ed1a7eb0b5 100644
@@ -81,6 +81,7 @@ CONFIG_NFT_NUMGEN=m
 CONFIG_NFT_QUEUE=m
 CONFIG_NFT_QUOTA=m
 CONFIG_NFT_REDIR=m
+CONFIG_NFT_REJECT=m
 CONFIG_NFT_SYNPROXY=m
 CONFIG_NFT_TPROXY=m
 CONFIG_VETH=m
tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh new file +294 -0

294 lines not on this page · raw message

Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
maintainer
#2 lore raw

tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh line 177

Quoted 4 lines · Antonio Ojea
tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh new file +4 -0
+	# request from ns1 to vip (DNAT to ns2) on an existing connection
+	# if we don't read from the pipe the traffic loops forever
+	echo PING2 >> "$TMPFILEIN"
+	sleep 0.5

This doesn't really work, 0.5 is too low for my machine and the tests fail most of the time.

Can you rework this to use the busywait helper
instead of sleep?

The checks are also very repetitive, so there is potential for code reuse.

Thanks.

#3 lore raw

PATCH v3 1/1 selftests: netfilter: conntrack respect reject rules

In reply to Antonio Ojea

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test validates that is possible to filter and reject new and established connections to the DNATed IP in the prerouting and forward filters.

---
V1 -> V2:
* Modified the test function to accept a third argument which contains the nftables rules to be applied.
* Add a new test case to filter and reject in the prerouting hook. V2 -> V3:
* Add helper functions to remove code duplication
* Use busywait instead of hardcoded sleeps

---
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 tools/testing/selftests/net/netfilter/config  |   1 +
 .../nft_conntrack_reject_established.sh       | 312 ++++++++++++++++++
 3 files changed, 314 insertions(+)
 create mode 100755 tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
index ffe161fac8b5..c276b8ac2383 100644
@@ -21,6 +21,7 @@ TEST_PROGS += nf_nat_edemux.sh
 TEST_PROGS += nft_audit.sh
 TEST_PROGS += nft_concat_range.sh
 TEST_PROGS += nft_conntrack_helper.sh
+TEST_PROGS += nft_conntrack_reject_established.sh
 TEST_PROGS += nft_fib.sh
 TEST_PROGS += nft_flowtable.sh
 TEST_PROGS += nft_meta.sh
tools/testing/selftests/net/netfilter/config +1 -0
index 43d8b500d391..44ed1a7eb0b5 100644
@@ -81,6 +81,7 @@ CONFIG_NFT_NUMGEN=m
 CONFIG_NFT_QUEUE=m
 CONFIG_NFT_QUOTA=m
 CONFIG_NFT_REDIR=m
+CONFIG_NFT_REJECT=m
 CONFIG_NFT_SYNPROXY=m
 CONFIG_NFT_TPROXY=m
 CONFIG_VETH=m
tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh new file +312 -0

312 lines not on this page · raw message

Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
maintainer
#4 lore raw
Quoted 13 lines · Antonio Ojea

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test validates that is possible to filter and reject new and established connections to the DNATed IP in the prerouting and forward filters.

Quoted 8 lines

---
V1 -> V2:
* Modified the test function to accept a third argument which contains the nftables rules to be applied.
* Add a new test case to filter and reject in the prerouting hook. V2 -> V3:
* Add helper functions to remove code duplication
* Use busywait instead of hardcoded sleeps

You will need to apply the busywait logic to the 'kill -0' too:

# PASS: frontend filter-ip6: fail to connect over the established connection to [dead:4::a]:8080 # [<0>] do_select+0x68e/0x950
# [<0>] core_sys_select+0x1ef/0x4b0
# [<0>] do_pselect.constprop.0+0xe7/0x180
# [<0>] __x64_sys_pselect6+0x58/0x70
# [<0>] do_syscall_64+0x9e/0x1a0
# [<0>] entry_SYSCALL_64_after_hwframe+0x77/0x7f
# ERROR: frontend filter-ip6: persistent connection is not closed as intended
# cat: /proc/756/stack: No such file or directory

The <0> Lines come from cat: /proc/$pid/stack which I added here locally, i.e. the kill -0 works, enters failure, but a retry after sleep 2 and the process is gone.

So, I think you need to give the process a bit more time to wake up, process the rst/eof and exit.

I think you can simply use the busywait helper for this too, pick a short timeout such as 3000ms as upperlimit, that should do the trick and still allow to detect a hanging process.

#5 lore raw

PATCH v4 1/1 selftests: netfilter: conntrack respect reject rules

In reply to Antonio Ojea

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test validates that is possible to filter and reject new and established connections to the DNATed IP in the prerouting and forward filters.

---
V1 -> V2:
* Modified the test function to accept a third argument which contains the nftables rules to be applied.
* Add a new test case to filter and reject in the prerouting hook. V2 -> V3:
* Add helper functions to remove code duplication
* Use busywait instead of hardcoded sleeps
V3 -> V4:
* Add helper functions to detect process does not exist
* Use busywait to wait for the process to exist
* Increase timeout to wait for possible answer from 1ms to 100ms

---
 .../testing/selftests/net/netfilter/Makefile  |   1 +
 tools/testing/selftests/net/netfilter/config  |   1 +
 .../nft_conntrack_reject_established.sh       | 322 ++++++++++++++++++
 3 files changed, 324 insertions(+)
 create mode 100755 tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
index ffe161fac8b5..c276b8ac2383 100644
@@ -21,6 +21,7 @@ TEST_PROGS += nf_nat_edemux.sh
 TEST_PROGS += nft_audit.sh
 TEST_PROGS += nft_concat_range.sh
 TEST_PROGS += nft_conntrack_helper.sh
+TEST_PROGS += nft_conntrack_reject_established.sh
 TEST_PROGS += nft_fib.sh
 TEST_PROGS += nft_flowtable.sh
 TEST_PROGS += nft_meta.sh
tools/testing/selftests/net/netfilter/config +1 -0
index 43d8b500d391..44ed1a7eb0b5 100644
@@ -81,6 +81,7 @@ CONFIG_NFT_NUMGEN=m
 CONFIG_NFT_QUEUE=m
 CONFIG_NFT_QUOTA=m
 CONFIG_NFT_REDIR=m
+CONFIG_NFT_REJECT=m
 CONFIG_NFT_SYNPROXY=m
 CONFIG_NFT_TPROXY=m
 CONFIG_VETH=m
tools/testing/selftests/net/netfilter/nft_conntrack_reject_established.sh new file +322 -0

322 lines not on this page · raw message

Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
maintainer Reviewed-by Tested-by on v4 1/1
#6 lore raw
Quoted 11 lines · Antonio Ojea

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test validates that is possible to filter and reject new and established connections to the DNATed IP in the prerouting and forward filters.

#7 lore raw
In reply to Antonio Ojea

Hi Antonio,

Quoted 11 lines · Antonio Ojea

This test ensures that conntrack correctly applies reject rules to established connections after DNAT, even when those connections are persistent.

The test sets up three network namespaces: ns1, ns2, and nsrouter. nsrouter acts as a router with DNAT, exposing a service running in ns2 via a virtual IP.

The test validates that is possible to filter and reject new and established connections to the DNATed IP in the prerouting and forward filters.

I am testing with different stable kernels to uncover timing issues.

With nf and nf-next kernels with instrumentions, **this works just fine**.

But I triggered a weird issue with Debian's 6.1.0-31-amd64:

# ./nft_conntrack_reject_established.sh
...
ERROR: backend filter-ip6: fail to connect to [dead:2::99]:8080
ERROR: backend filter-ip6: fail to connect over the established connection to [dead:4::a]:8080
ERROR: backend filter-ip6: fail to connect to [dead:4::a]:8080
ERROR: backend filter-ip6: fail to connect over the established connection to [dead:4::a]:8080
ERROR: backend filter-ip6: fail to connect to [dead:2::99]:8080

interestingly if I reversed the order, ie. I run ipv6 before ipv4 test, then ipv4 fails:

for testname in "${!testcases[@]}"; do
- test_conntrack_reject_established "ip" "$testname" "${testcases[$testname]}"

       test_conntrack_reject_established "ip6" "$testname" "${testcases[$testname]}"

+ test_conntrack_reject_established "ip" "$testname" "${testcases[$testname]}"
done

also, running standalone ipv4 test, ie.:

for testname in "${!testcases[@]}"; do

      test_conntrack_reject_established "ip" "$testname" "${testcases[$testname]}"

done

or ipv6 test, ie.:

for testname in "${!testcases[@]}"; do

      test_conntrack_reject_established "ip6" "$testname" "${testcases[$testname]}"

done

works perfectly fine.

Hm, where is the issue? I have to double check, maybe -stable 6.1 is missing a backport fix.

Thanks.

#8 lore raw
Quoted 33 lines

I am testing with different stable kernels to uncover timing issues.

With nf and nf-next kernels with instrumentions, **this works just fine**.

But I triggered a weird issue with Debian's 6.1.0-31-amd64:

# ./nft_conntrack_reject_established.sh
...
ERROR: backend filter-ip6: fail to connect to [dead:2::99]:8080
ERROR: backend filter-ip6: fail to connect over the established connection to [dead:4::a]:8080
ERROR: backend filter-ip6: fail to connect to [dead:4::a]:8080
ERROR: backend filter-ip6: fail to connect over the established connection to [dead:4::a]:8080
ERROR: backend filter-ip6: fail to connect to [dead:2::99]:8080

interestingly if I reversed the order, ie. I run ipv6 before ipv4 test, then ipv4 fails:

for testname in "${!testcases[@]}"; do

(quoted diff) +1 -1
-      test_conntrack_reject_established "ip" "$testname" "${testcases[$testname]}"
       test_conntrack_reject_established "ip6" "$testname" "${testcases[$testname]}"
+      test_conntrack_reject_established "ip" "$testname" "${testcases[$testname]}"

done

also, running standalone ipv4 test, ie.:

for testname in "${!testcases[@]}"; do

      test_conntrack_reject_established "ip" "$testname" "${testcases[$testname]}"

done

or ipv6 test, ie.:

Quoted 10 lines

for testname in "${!testcases[@]}"; do

      test_conntrack_reject_established "ip6" "$testname" "${testcases[$testname]}"

done

works perfectly fine.

Hm, where is the issue? I have to double check, maybe -stable 6.1 is missing a backport fix.

Naive question, the nft client used on the tests is the same in all environments?

The fact that individually works but together doesn't and that the test is using "inet" tables can point to something related to that dual stack support?