Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
@ 2026-11-20 22:47 Kalash Nainwal via B4 Relay
  2026-11-20 22:47 ` [PATCH net-next 1/2] " Kalash Nainwal via B4 Relay
  2026-11-20 22:47 ` [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
  0 siblings, 2 replies; 5+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-20 22:47 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	David Ahern, Ido Schimmel, Neal Cardwell, Kuniyuki Iwashima
  Cc: netdev, linux-doc, linux-kernel, linux-kselftest, Kalash Nainwal

With net.ipv4.tcp_l3mdev_accept=1, an unbound TCP listener also accepts
connections that arrive in a VRF. When a VRF-bound listener exists on
the same port and both use SO_REUSEPORT, the lookup does not prefer the
VRF-bound one: inet_lhash2_lookup() returns the reuseport pick of the
first socket that beats the current best score, and the two sockets
are in different reuseport groups. The winner depends on bucket order,
and AF_INET6 reuseport listeners are added at the tail while AF_INET
ones are added at the head, so IPv4 and IPv6 behave differently.

We hit this with a BGP daemon that has a TCP-MD5 key on its VRF-bound
:179 listener and none on its unbound :179 listener. IPv6 VRF peers
reached the unbound listener and came up without MD5. Setting
tcp_l3mdev_accept=0 is not an option because other services on the
host need it.

Patch 1 adds a per-netns port list, net.ipv4.tcp_l3mdev_exempt_ports,
modeled on ip_local_reserved_ports. For a listed port, an unbound socket
does not match VRF traffic. The check runs only for VRF traffic
(sdif != 0) on unbound sockets, so other lookups are unchanged.

Patch 2 adds a selftest.

Testing:
- New selftest on net-next in virtme-ng: all 10 cases pass. Without the
  port listed, the first listener in the bucket wins (IPv4 fails when
  the VRF-bound listener is created first, IPv6 when the unbound one
  is).
- The same change on 6.12, with a BGP daemon over a VRF with TCP-MD5 on
  hardware: the IPv6 VRF session now uses the VRF-bound MD5 listener.

Alternatives considered: a BPF sk_lookup program can do this, but needs
a loader attached in every netns that runs the daemon.

---
Kalash Nainwal (2):
      tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
      selftests: net: add tcp_l3mdev_exempt_ports test

 Documentation/networking/ip-sysctl.rst             |  19 ++
 include/net/ip.h                                   |  14 ++
 include/net/netns/ipv4.h                           |   1 +
 net/ipv4/inet_hashtables.c                         |   5 +
 net/ipv4/sysctl_net_ipv4.c                         |  14 ++
 net/ipv6/inet6_hashtables.c                        |   4 +
 tools/testing/selftests/net/Makefile               |   1 +
 .../selftests/net/tcp_l3mdev_exempt_ports.sh       | 213 +++++++++++++++++++++
 8 files changed, 271 insertions(+)
---
base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e
change-id: 20261120-tcp-l3mdev-exempt-ports-c9f5dfebe356

Best regards,
--  
Kalash Nainwal <kalash@nexthop.ai>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net-next 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
  2026-11-20 22:47 [PATCH net-next 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
@ 2026-11-20 22:47 ` Kalash Nainwal via B4 Relay
  2026-11-24 23:25   ` netdev-bot+sashiko
  2026-11-20 22:47 ` [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
  1 sibling, 1 reply; 5+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-20 22:47 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	David Ahern, Ido Schimmel, Neal Cardwell, Kuniyuki Iwashima
  Cc: netdev, linux-doc, linux-kernel, linux-kselftest, Kalash Nainwal

From: Kalash Nainwal <kalash@nexthop.ai>

With tcp_l3mdev_accept=1, an unbound listener also matches connections
that arrive in a VRF. If a VRF-bound listener exists on the same port
and both use SO_REUSEPORT, the lookup returns whichever is first in the
listen bucket, not the VRF-bound one. Since AF_INET and AF_INET6
reuseport sockets are inserted at opposite ends of the bucket, the same
setup can pick the VRF-bound listener for IPv4 and the unbound one for
IPv6.

This breaks, for example, a BGP daemon with a TCP-MD5 key only on its
VRF-bound listener: IPv6 peers in the VRF reach the unbound listener
and the session comes up without MD5.

Add net.ipv4.tcp_l3mdev_exempt_ports, a per-netns port list in the
same format as ip_local_reserved_ports. For a listed port, an unbound
socket does not match connections that arrive in a VRF, so only a
VRF-bound listener can take them. Other ports and default-VRF traffic
are not affected. The default is empty.

Signed-off-by: Kalash Nainwal <kalash@nexthop.ai>
---
 Documentation/networking/ip-sysctl.rst | 19 +++++++++++++++++++
 include/net/ip.h                       | 14 ++++++++++++++
 include/net/netns/ipv4.h               |  1 +
 net/ipv4/inet_hashtables.c             |  5 +++++
 net/ipv4/sysctl_net_ipv4.c             | 14 ++++++++++++++
 net/ipv6/inet6_hashtables.c            |  4 ++++
 6 files changed, 57 insertions(+)

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index 3cc80d9b024..1de0ee5c0d5 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -619,6 +619,25 @@ tcp_l3mdev_accept - BOOLEAN
 
 	Default: 0 (disabled)
 
+tcp_l3mdev_exempt_ports - list of comma separated ranges
+	TCP ports for which tcp_l3mdev_accept is ignored. When
+	tcp_l3mdev_accept is enabled, a listen socket that is not bound to
+	an L3 master device also matches connections that arrive in an L3
+	domain (e.g., a VRF). For a port in this list, such a socket does
+	not match these connections; only a listen socket bound to the L3
+	master device does. Connections that do not arrive in an L3 domain
+	are not affected. This applies to both IPv4 and IPv6.
+
+	This is useful when the same port has both an unbound listener and
+	a listener bound to a VRF, and VRF connections must reach the
+	VRF-bound one (for example, when only it has TCP-MD5 or TCP-AO
+	keys configured).
+
+	The format is the same as ip_local_reserved_ports, e.g.
+	"179,5000-5010". Writing to the file replaces the current list.
+
+	Default: empty
+
 tcp_low_latency - BOOLEAN
 	This is a legacy option, it has no effect anymore.
 
diff --git a/include/net/ip.h b/include/net/ip.h
index 5035b313a26..c6581080a9a 100644
--- a/include/net/ip.h
+++ b/include/net/ip.h
@@ -396,6 +396,20 @@ static inline bool inet_port_requires_bind_service(struct net *net, unsigned sho
 }
 #endif
 
+/* Ports listed in net.ipv4.tcp_l3mdev_exempt_ports behave as if
+ * tcp_l3mdev_accept were 0: an unbound listener does not match traffic
+ * received on an L3 master device slave. Applies to IPv4 and IPv6.
+ */
+static inline bool tcp_port_l3mdev_exempt(const struct net *net,
+					  unsigned short port)
+{
+#ifdef CONFIG_SYSCTL
+	if (net->ipv4.sysctl_tcp_l3mdev_exempt_ports)
+		return test_bit(port, net->ipv4.sysctl_tcp_l3mdev_exempt_ports);
+#endif
+	return false;
+}
+
 __be32 inet_current_timestamp(void);
 
 /* From inetpeer.c */
diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
index 3efbc7f4b4f..899d90f34a7 100644
--- a/include/net/netns/ipv4.h
+++ b/include/net/netns/ipv4.h
@@ -271,6 +271,7 @@ struct netns_ipv4 {
 
 #ifdef CONFIG_SYSCTL
 	unsigned long *sysctl_local_reserved_ports;
+	unsigned long *sysctl_tcp_l3mdev_exempt_ports;
 	int sysctl_ip_prot_sock;
 #endif
 
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 860145d810e..063f6dd00a6 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -362,6 +362,11 @@ static inline int compute_score(struct sock *sk, const struct net *net,
 
 		if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
 			return -1;
+
+		if (!sk->sk_bound_dev_if && sdif &&
+		    tcp_port_l3mdev_exempt(net, hnum))
+			return -1;
+
 		score =  sk->sk_bound_dev_if ? 2 : 1;
 
 		if (sk->sk_family == PF_INET)
diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
index 9df93ad219d..83d0030512a 100644
--- a/net/ipv4/sysctl_net_ipv4.c
+++ b/net/ipv4/sysctl_net_ipv4.c
@@ -840,6 +840,13 @@ static const struct ctl_table ipv4_net_table[] = {
 		.mode		= 0644,
 		.proc_handler	= proc_do_large_bitmap,
 	},
+	{
+		.procname	= "tcp_l3mdev_exempt_ports",
+		.data		= &init_net.ipv4.sysctl_tcp_l3mdev_exempt_ports,
+		.maxlen		= 65536,
+		.mode		= 0644,
+		.proc_handler	= proc_do_large_bitmap,
+	},
 	{
 		.procname	= "ip_no_pmtu_disc",
 		.data		= &init_net.ipv4.sysctl_ip_no_pmtu_disc,
@@ -1708,10 +1715,16 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
 	if (!net->ipv4.sysctl_local_reserved_ports)
 		goto err_ports;
 
+	net->ipv4.sysctl_tcp_l3mdev_exempt_ports = kzalloc(65536 / 8, GFP_KERNEL);
+	if (!net->ipv4.sysctl_tcp_l3mdev_exempt_ports)
+		goto err_l3mdev_ports;
+
 	proc_fib_multipath_hash_set_seed(net, 0);
 
 	return 0;
 
+err_l3mdev_ports:
+	kfree(net->ipv4.sysctl_local_reserved_ports);
 err_ports:
 	unregister_net_sysctl_table(net->ipv4.ipv4_hdr);
 err_reg:
@@ -1729,6 +1742,7 @@ static __net_exit void ipv4_sysctl_exit_net(struct net *net)
 	unregister_net_sysctl_table(net->ipv4.ipv4_hdr);
 	kfree(table);
 	kfree(net->ipv4.sysctl_local_reserved_ports);
+	kfree(net->ipv4.sysctl_tcp_l3mdev_exempt_ports);
 }
 
 static __net_initdata struct pernet_operations ipv4_sysctl_ops = {
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
index 1eea584d80d..43c52b56e8b 100644
--- a/net/ipv6/inet6_hashtables.c
+++ b/net/ipv6/inet6_hashtables.c
@@ -141,6 +141,10 @@ static inline int compute_score(struct sock *sk, const struct net *net,
 		if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
 			return -1;
 
+		if (!sk->sk_bound_dev_if && sdif &&
+		    tcp_port_l3mdev_exempt(net, hnum))
+			return -1;
+
 		score =  sk->sk_bound_dev_if ? 2 : 1;
 		if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
 			score++;

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test
  2026-11-20 22:47 [PATCH net-next 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
  2026-11-20 22:47 ` [PATCH net-next 1/2] " Kalash Nainwal via B4 Relay
@ 2026-11-20 22:47 ` Kalash Nainwal via B4 Relay
  2026-11-24 23:25   ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-20 22:47 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	David Ahern, Ido Schimmel, Neal Cardwell, Kuniyuki Iwashima
  Cc: netdev, linux-doc, linux-kernel, linux-kselftest, Kalash Nainwal

From: Kalash Nainwal <kalash@nexthop.ai>

Check that with tcp_l3mdev_accept=1 and the port listed in
tcp_l3mdev_exempt_ports, a VRF connection goes to the VRF-bound
listener (in both listener creation orders) and is refused by an
unbound-only listener, and that default-VRF connections still reach
the unbound listener. Covers IPv4 and IPv6.

Signed-off-by: Kalash Nainwal <kalash@nexthop.ai>
---
 tools/testing/selftests/net/Makefile               |   1 +
 .../selftests/net/tcp_l3mdev_exempt_ports.sh       | 213 +++++++++++++++++++++
 2 files changed, 214 insertions(+)

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index c80dabbcc90..2a0b8453bfd 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -109,6 +109,7 @@ TEST_PROGS := \
 	stress_reuseport_listen.sh \
 	tcp_ecmp_failover.sh \
 	tcp_fastopen_backup_key.sh \
+	tcp_l3mdev_exempt_ports.sh \
 	test_bridge_backup_port.sh \
 	test_bridge_neigh_suppress.sh \
 	test_ingress_egress_chaining.sh \
diff --git a/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
new file mode 100755
index 00000000000..f075abb9e8f
--- /dev/null
+++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
@@ -0,0 +1,213 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test net.ipv4.tcp_l3mdev_exempt_ports.
+#
+# With tcp_l3mdev_accept=1, a listener that is not bound to a device also
+# matches connections that arrive in a VRF. For a port listed in
+# tcp_l3mdev_exempt_ports it must not: only a listener bound to the VRF
+# may take such a connection. Connections in the default VRF are not
+# affected.
+#
+#  ns_c (client in VRF)       ns_s (server)             ns_d (client in default VRF)
+#      veth0 ------------- veth_vrf [vrf red]
+#                             veth_def ----------------- veth0
+
+source lib.sh
+
+PORT=12345
+OTHER_PORT=23456
+VRF=red
+VRF_TABLE=1000
+
+VRF_SRV4=172.16.1.1
+VRF_CLI4=172.16.1.2
+DEF_SRV4=172.16.2.1
+DEF_CLI4=172.16.2.2
+VRF_SRV6=2001:db8:1::1
+VRF_CLI6=2001:db8:1::2
+DEF_SRV6=2001:db8:2::1
+DEF_CLI6=2001:db8:2::2
+
+TESTS="
+	unbound_listener
+	vrf_and_unbound_listeners
+	default_vrf_unaffected
+"
+
+cleanup()
+{
+	cleanup_all_ns
+}
+
+setup()
+{
+	setup_ns ns_s ns_c ns_d || exit $ksft_skip
+
+	ip -n "$ns_s" link add "$VRF" type vrf table "$VRF_TABLE"
+	ip -n "$ns_s" link set "$VRF" up
+
+	ip -n "$ns_s" link add veth_vrf type veth peer name veth0 netns "$ns_c"
+	ip -n "$ns_s" link set veth_vrf master "$VRF"
+	ip -n "$ns_s" link set veth_vrf up
+	ip -n "$ns_s" addr add "$VRF_SRV4/24" dev veth_vrf
+	ip -n "$ns_s" addr add "$VRF_SRV6/64" dev veth_vrf nodad
+	ip -n "$ns_c" link set veth0 up
+	ip -n "$ns_c" addr add "$VRF_CLI4/24" dev veth0
+	ip -n "$ns_c" addr add "$VRF_CLI6/64" dev veth0 nodad
+
+	ip -n "$ns_s" link add veth_def type veth peer name veth0 netns "$ns_d"
+	ip -n "$ns_s" link set veth_def up
+	ip -n "$ns_s" addr add "$DEF_SRV4/24" dev veth_def
+	ip -n "$ns_s" addr add "$DEF_SRV6/64" dev veth_def nodad
+	ip -n "$ns_d" link set veth0 up
+	ip -n "$ns_d" addr add "$DEF_CLI4/24" dev veth0
+	ip -n "$ns_d" addr add "$DEF_CLI6/64" dev veth0 nodad
+
+	ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_accept=1
+}
+
+ipver()
+{
+	[ "$1" = "-6" ] && echo 6 || echo 4
+}
+
+set_exempt()
+{
+	ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_exempt_ports="$1"
+}
+
+# Start a server. nettest sets SO_REUSEPORT on its listeners, so an
+# unbound and a VRF-bound listener can share the port.
+# $1: "-6" or "", $2: "vrf" or "unbound"
+start_server()
+{
+	local dev_arg=""
+
+	[ "$2" = "vrf" ] && dev_arg="-I $VRF"
+	# shellcheck disable=SC2086
+	ip netns exec "$ns_s" ./nettest $1 -s $dev_arg -p "$PORT" -t 3 -q &
+}
+
+# $1: client netns, $2: "-6" or "", $3: server address
+connect()
+{
+	# shellcheck disable=SC2086
+	ip netns exec "$1" ./nettest $2 -r "$3" -p "$PORT" -t 2 -q
+}
+
+unbound_listener()
+{
+	local fam addr pid
+
+	for fam in "" "-6"; do
+		addr=$VRF_SRV4
+		[ "$fam" = "-6" ] && addr=$VRF_SRV6
+
+		RET=0
+		set_exempt "$OTHER_PORT"
+		start_server "$fam" unbound
+		pid=$!
+		wait_local_port_listen "$ns_s" "$PORT" tcp
+		connect "$ns_c" "$fam" "$addr"
+		check_err $? "VRF connection not accepted by unbound listener"
+		wait $pid
+		log_test "IPv$(ipver "$fam"): not exempt, VRF conn to unbound listener accepted"
+
+		RET=0
+		set_exempt "$PORT"
+		start_server "$fam" unbound
+		pid=$!
+		wait_local_port_listen "$ns_s" "$PORT" tcp
+		connect "$ns_c" "$fam" "$addr"
+		check_fail $? "VRF connection accepted by unbound listener"
+		wait $pid
+		log_test "IPv$(ipver "$fam"): exempt, VRF conn to unbound listener rejected"
+	done
+	set_exempt ""
+}
+
+# Both an unbound and a VRF-bound listener on the port. The VRF connection
+# must reach the VRF-bound one, whichever listener was created first.
+vrf_and_unbound_listeners()
+{
+	local fam addr order first second pid_first pid_second
+	local rc_first rc_second rc_vrf rc_unbound
+
+	set_exempt "$PORT"
+	for fam in "" "-6"; do
+		addr=$VRF_SRV4
+		[ "$fam" = "-6" ] && addr=$VRF_SRV6
+
+		for order in "unbound vrf" "vrf unbound"; do
+			read -r first second <<< "$order"
+			RET=0
+
+			start_server "$fam" "$first"
+			pid_first=$!
+			wait_local_port_listen "$ns_s" "$PORT" tcp
+			start_server "$fam" "$second"
+			pid_second=$!
+			sleep 0.2
+
+			connect "$ns_c" "$fam" "$addr"
+			check_err $? "VRF connection failed"
+
+			wait $pid_first
+			rc_first=$?
+			wait $pid_second
+			rc_second=$?
+			if [ "$first" = "vrf" ]; then
+				rc_vrf=$rc_first
+				rc_unbound=$rc_second
+			else
+				rc_vrf=$rc_second
+				rc_unbound=$rc_first
+			fi
+			check_err $rc_vrf "VRF-bound listener did not get the connection"
+			check_fail $rc_unbound "unbound listener got the connection"
+
+			log_test "IPv$(ipver "$fam"): exempt, $first first, VRF-bound listener wins"
+		done
+	done
+	set_exempt ""
+}
+
+default_vrf_unaffected()
+{
+	local fam addr pid
+
+	set_exempt "$PORT"
+	for fam in "" "-6"; do
+		addr=$DEF_SRV4
+		[ "$fam" = "-6" ] && addr=$DEF_SRV6
+
+		RET=0
+		start_server "$fam" unbound
+		pid=$!
+		wait_local_port_listen "$ns_s" "$PORT" tcp
+		connect "$ns_d" "$fam" "$addr"
+		check_err $? "default VRF connection not accepted"
+		wait $pid
+		check_err $? "unbound listener did not get the connection"
+		log_test "IPv$(ipver "$fam"): exempt, default VRF conn to unbound listener accepted"
+	done
+	set_exempt ""
+}
+
+trap cleanup EXIT
+
+if [ ! -x ./nettest ]; then
+	echo "SKIP: nettest not built"
+	exit "$ksft_skip"
+fi
+
+setup
+if ! ip netns exec "$ns_s" test -e /proc/sys/net/ipv4/tcp_l3mdev_exempt_ports; then
+	echo "SKIP: net.ipv4.tcp_l3mdev_exempt_ports not supported"
+	exit "$ksft_skip"
+fi
+
+tests_run
+
+exit "$EXIT_STATUS"

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
  2026-11-20 22:47 ` [PATCH net-next 1/2] " Kalash Nainwal via B4 Relay
@ 2026-11-24 23:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 23:25 UTC (permalink / raw)
  To: kalash
  Cc: davem, edumazet, kuba, pabeni, horms, corbet, skhan, rdunlap,
	dsahern, idosch, ncardwell, kuniyu, netdev, linux-doc,
	linux-kernel, linux-kselftest

Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 4

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The commit message describes a bug in TCP listener lookup, and
  the patch works around it with a new opt-in UAPI knob instead of…
- [Medium] The documentation and code comment promise more than the code
  does.
- [Low] tcp_l3mdev_exempt_ports only changes how tcp_l3mdev_accept
  behaves, but it is not config-gated the way that sysctl is.
- [Low] The patch adds `unsigned long *sysctl_tcp_l3mdev_exempt_ports` to
  struct netns_ipv4 (include/net/netns/ipv4.h:274) but does not update…
- [Low] The new ip-sysctl.rst text says that for a listed port "only a
  listen socket bound to the L3 master device" matches VRF connections.
- [Low] Updates to the new per-netns bitmap are not atomic with respect
  to the TCP listener lookup that reads it.

--

tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl

This adds a per-netns port bitmap sysctl that uses the same format as
ip_local_reserved_ports. When tcp_l3mdev_accept=1, an unbound TCP listener
on a listed port no longer matches connections that arrive on an L3 master
device slave, so only a VRF-bound listener can take them. The check is
added to the IPv4 and IPv6 listener compute_score() functions.

> diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
> index 3cc80d9b024b1..1de0ee5c0d5b5 100644
> --- a/Documentation/networking/ip-sysctl.rst
> +++ b/Documentation/networking/ip-sysctl.rst
> @@ -619,6 +619,25 @@ tcp_l3mdev_accept - BOOLEAN
>  
>  	Default: 0 (disabled)
>  
> +tcp_l3mdev_exempt_ports - list of comma separated ranges
> +	TCP ports for which tcp_l3mdev_accept is ignored. When

[Severity: Medium]
Is "tcp_l3mdev_accept is ignored" accurate? The comment above
tcp_port_l3mdev_exempt() in include/net/ip.h makes a similar claim, that
listed ports "behave as if tcp_l3mdev_accept were 0".

The only new check is in the two listener compute_score() functions. Other
users of sysctl_tcp_l3mdev_accept still apply the global value to listed
ports.

Established and request socket matching in inet_match() and inet6_match()
goes through inet_sk_bound_dev_eq(), which never looks at the bitmap. An
unbound established socket on a listed port still matches segments with
the same 4-tuple that arrive on a VRF slave.

Bind buckets are keyed on inet_sk_bound_l3mdev():

include/net/inet_sock.h:inet_sk_bound_l3mdev() {
    ...
	if (!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
		return l3mdev_master_ifindex_by_index(net,
						      sk->sk_bound_dev_if);
    ...
}

With tcp_l3mdev_accept=1 this returns 0. The callers are
inet_csk_get_port(), inet_csk_find_open_port(), __inet_inherit_port(),
__inet_hash_connect() and __inet_bhash2_update_saddr(). So an unbound
socket and a VRF-bound socket on a listed port still share one bind
bucket. They still fail with EADDRINUSE unless SO_REUSEPORT is set. With
tcp_l3mdev_accept=0 they would land in separate buckets.

inet_request_bound_dev_if() also still uses tcp_l3mdev_accept when it
sets the child's bound device.

Should the documentation and the comment say this only covers listener
lookup? Or should the exemption also apply to these paths?

> +	tcp_l3mdev_accept is enabled, a listen socket that is not bound to
> +	an L3 master device also matches connections that arrive in an L3
> +	domain (e.g., a VRF). For a port in this list, such a socket does
> +	not match these connections; only a listen socket bound to the L3
> +	master device does. Connections that do not arrive in an L3 domain

[Severity: Low]
Is "only a listen socket bound to the L3 master device does" accurate?
The new check only filters out sockets with sk_bound_dev_if == 0. For
bound sockets, inet_bound_dev_eq() still does:

	return bound_dev_if == dif || bound_dev_if == sdif;

That means a listener bound to the enslaved ingress device (dif) can
also match these connections.

> +	are not affected. This applies to both IPv4 and IPv6.
> +

[ ... ]

> diff --git a/include/net/ip.h b/include/net/ip.h
> index 5035b313a26b0..c6581080a9a47 100644
> --- a/include/net/ip.h
> +++ b/include/net/ip.h
> @@ -396,6 +396,20 @@ static inline bool inet_port_requires_bind_service(struct net *net, unsigned sho
>  }
>  #endif
>  
> +/* Ports listed in net.ipv4.tcp_l3mdev_exempt_ports behave as if
> + * tcp_l3mdev_accept were 0: an unbound listener does not match traffic
> + * received on an L3 master device slave. Applies to IPv4 and IPv6.
> + */
> +static inline bool tcp_port_l3mdev_exempt(const struct net *net,
> +					  unsigned short port)
> +{
> +#ifdef CONFIG_SYSCTL
> +	if (net->ipv4.sysctl_tcp_l3mdev_exempt_ports)
> +		return test_bit(port, net->ipv4.sysctl_tcp_l3mdev_exempt_ports);

[Severity: Low]
compute_score() reads this bitmap without a lock in softirq context. The
writer, proc_do_large_bitmap(), takes no lock either and updates the
bitmap in place:

kernel/sysctl.c:proc_do_large_bitmap() {
    ...
			if (*ppos)
				bitmap_or(bitmap, bitmap, tmp_bitmap, bitmap_len);
			else
				bitmap_copy(bitmap, tmp_bitmap, bitmap_len);
    ...
}

If the written list is longer than PAGE_SIZE-1 bytes, only the first
chunk is parsed. That chunk replaces the whole bitmap and the write
returns short. The rest comes in a later write() at *ppos > 0 and is
ORed in.

Consider a port that is in both the old and the new list but appears late
in the string. Between those two write() calls, could a SYN for that port
arriving in a VRF be accepted by the unbound listener, without the TCP-MD5
or TCP-AO key that the VRF-bound listener has?

Two concurrent writers could also interleave and leave a mix of both
lists.

> +#endif
> +	return false;
> +}

[ ... ]

> diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
> index 3efbc7f4b4f7d..899d90f34a716 100644
> --- a/include/net/netns/ipv4.h
> +++ b/include/net/netns/ipv4.h
> @@ -271,6 +271,7 @@ struct netns_ipv4 {
>  
>  #ifdef CONFIG_SYSCTL
>  	unsigned long *sysctl_local_reserved_ports;
> +	unsigned long *sysctl_tcp_l3mdev_exempt_ports;

[Severity: Low]
This isn't a bug, but should
Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst be updated
for the new field? That file lists the neighbouring
sysctl_local_reserved_ports and sysctl_ip_prot_sock fields, and lists
sysctl_tcp_l3mdev_accept as RX read_mostly.

The new pointer is read on the TCP RX listener lookup path. It sits in
the CONFIG_SYSCTL area, not in the netns_ipv4_read_rx group, which is
where net_namespace.c asserts sysctl_tcp_l3mdev_accept lives.

>  	int sysctl_ip_prot_sock;
>  #endif
>  
> diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
> index 860145d810ed4..063f6dd00a64d 100644
> --- a/net/ipv4/inet_hashtables.c
> +++ b/net/ipv4/inet_hashtables.c
> @@ -362,6 +362,11 @@ static inline int compute_score(struct sock *sk, const struct net *net,
>  
>  		if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
>  			return -1;
> +
> +		if (!sk->sk_bound_dev_if && sdif &&
> +		    tcp_port_l3mdev_exempt(net, hnum))
> +			return -1;

[Severity: Medium]
The commit message describes a bug in the listener lookup itself. Would
it be better to fix the lookup than to add an opt-in knob?

In inet_lhash2_lookup(), and likewise in inet6_lhash2_lookup(), the
first socket with score > hiscore that belongs to a reuseport group is
returned immediately:

		if (score > hiscore) {
			result = inet_lookup_reuseport(net, sk, skb, doff,
						       saddr, sport, daddr, hnum, inet_ehashfn);
			if (result)
				return result;

Sockets later in the bucket are never checked, even if they would score
higher.

inet_reuseport_add_sock() only groups sockets with the same bound device:

		    sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&

So an unbound listener and a VRF-bound listener are always in different
reuseport groups, and whichever group comes first in the bucket wins.

__inet_hash() adds AF_INET6 reuseport sockets at the tail and everything
else at the head. That is why IPv4 and IPv6 end up picking different
listeners.

The new sysctl is empty by default. Unless the admin knows to list the
port, the IPv4/IPv6 inconsistency remains. So does the TCP-MD5/AO bypass
for IPv6 VRF peers that the commit message describes.

The check is also broader than the problem requires. For a listed port,
it rejects the unbound listener for traffic from every VRF, including
VRFs that have no bound listener of their own. A setup with a dedicated
listener for VRF A and an unbound listener serving the other VRFs on the
same port can't be configured.

Could the commit message explain why the lookup wasn't fixed instead?

A lookup fix would also need a scoring change. The sk_incoming_cpu bonus
can tie a bound IPv4 listener (2+1=3) with an unbound IPv4 listener on
the matching CPU (1+1+1=3).

> +
>  		score =  sk->sk_bound_dev_if ? 2 : 1;
>  
>  		if (sk->sk_family == PF_INET)
> diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
> index 9df93ad219db8..83d0030512aa5 100644
> --- a/net/ipv4/sysctl_net_ipv4.c
> +++ b/net/ipv4/sysctl_net_ipv4.c
> @@ -840,6 +840,13 @@ static const struct ctl_table ipv4_net_table[] = {
>  		.mode		= 0644,
>  		.proc_handler	= proc_do_large_bitmap,
>  	},
> +	{
> +		.procname	= "tcp_l3mdev_exempt_ports",
> +		.data		= &init_net.ipv4.sysctl_tcp_l3mdev_exempt_ports,
> +		.maxlen		= 65536,
> +		.mode		= 0644,
> +		.proc_handler	= proc_do_large_bitmap,
> +	},

[Severity: Low]
Should this entry be under #ifdef CONFIG_NET_L3_MASTER_DEV, as
tcp_l3mdev_accept is? Its documentation says it is "Only valid when the
kernel was compiled with CONFIG_NET_L3_MASTER_DEV". The documentation for
tcp_l3mdev_exempt_ports doesn't mention that dependency.

Without CONFIG_NET_L3_MASTER_DEV, inet_sdif(), tcp_v4_sdif() and
tcp_v6_sdif() always return 0. The new check in both compute_score()
functions can then never fire.

ipv4_sysctl_init_net() still allocates the bitmap for every netns:

	net->ipv4.sysctl_tcp_l3mdev_exempt_ports = kzalloc(65536 / 8, GFP_KERNEL);

So on those kernels the knob accepts writes, has no effect, and costs
8 KiB per network namespace.

>  	{
>  		.procname	= "ip_no_pmtu_disc",
>  		.data		= &init_net.ipv4.sysctl_ip_no_pmtu_disc,

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09%40nexthop.ai

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test
  2026-11-20 22:47 ` [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
@ 2026-11-24 23:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 23:25 UTC (permalink / raw)
  To: kalash
  Cc: davem, edumazet, kuba, pabeni, horms, corbet, skhan, rdunlap,
	dsahern, idosch, ncardwell, kuniyu, netdev, linux-doc,
	linux-kernel, linux-kselftest

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] vrf_and_unbound_listeners() in
  tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh waits for the…
- [Medium] selftests: net: missing VRF and l3mdev prerequisite checks in
  setup()
- [Low] The negative checks treat any non-zero exit from nettest as the
  expected 'did not get the connection' result.
- [Low] In unbound_listener()'s 'exempt, VRF conn to unbound listener
  rejected' subtest (lines 117-125), the server is started in the…
- [Low] selftests: net: tcp_l3mdev_exempt_ports.sh bypasses lib.sh
  helpers

--

selftests: net: add tcp_l3mdev_exempt_ports test

This adds a selftest for net.ipv4.tcp_l3mdev_exempt_ports. With
tcp_l3mdev_accept=1 and the port exempted, it checks that a VRF connection
reaches only a VRF-bound listener (in both listener creation orders) and
is refused by an unbound-only listener. It also checks that default-VRF
connections still reach the unbound listener, for IPv4 and IPv6.

> diff --git a/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
> new file mode 100755
> index 0000000000000..f075abb9e8fbf
> --- /dev/null
> +++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
> @@ -0,0 +1,213 @@

[ ... ]

> +unbound_listener()
> +{
> +	local fam addr pid
> +

[ ... ]

> +		RET=0
> +		set_exempt "$PORT"
> +		start_server "$fam" unbound
> +		pid=$!
> +		wait_local_port_listen "$ns_s" "$PORT" tcp
> +		connect "$ns_c" "$fam" "$addr"
> +		check_fail $? "VRF connection accepted by unbound listener"
> +		wait $pid

[Severity: Low]
Can this negative subtest pass even when the exemption does nothing?

wait_local_port_listen() in lib.sh gives up after about one second and
does not return an error:

tools/testing/selftests/net/lib.sh:wait_local_port_listen() {
	...
	for i in $(seq 10); do
		if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \
		   /proc/net/"${protocol}"* | grep -q "${pattern}"; then
			break
		fi
		sleep 0.1
	done
}

If the backgrounded nettest has not reached listen() by then, connect()
still runs. The SYN then gets a RST because nothing is listening on
$PORT, not because of the exemption.

check_fail cannot tell this case apart from a real refusal caused by
tcp_l3mdev_exempt_ports. The server status from the following wait $pid
is not checked either.

Would it make sense to confirm that a LISTEN socket exists, or make the
readiness wait fatal, before the negative check runs?

[ ... ]

> +vrf_and_unbound_listeners()
> +{

[ ... ]

> +		for order in "unbound vrf" "vrf unbound"; do
> +			read -r first second <<< "$order"
> +			RET=0
> +
> +			start_server "$fam" "$first"
> +			pid_first=$!
> +			wait_local_port_listen "$ns_s" "$PORT" tcp
> +			start_server "$fam" "$second"
> +			pid_second=$!
> +			sleep 0.2

[Severity: Medium]
Is a fixed sleep 0.2 enough to make sure the second listener is up before
the client connects?

Only the first listener is synchronized with wait_local_port_listen().
Calling it a second time would not help. It greps /proc/net/tcp* for the
port in LISTEN state and returns as soon as any socket matches, and the
first listener already matches.

On a slow host, for example a debug kernel with KASAN or lockdep or a
nested VM, the second ip netns exec ./nettest may not reach listen()
within 200ms. The two orders then go wrong in different ways.

In the "unbound vrf" order, only the unbound listener exists. The port is
exempt, so the VRF SYN is reset. Both check_err $? "VRF connection failed"
and check_err $rc_vrf then report a failure that has nothing to do with
kernel behaviour.

In the "vrf unbound" order, only the VRF listener exists, so it takes the
connection. The unbound listener later times out with rc=2, which
satisfies check_fail $rc_unbound. The subtest passes without testing
whether a later-created unbound listener can win.

Could the test poll until two LISTEN sockets exist on $PORT instead? For
example, it could wait for ss -Hltn sport = :$PORT | wc -l to reach 2.

> +
> +			connect "$ns_c" "$fam" "$addr"
> +			check_err $? "VRF connection failed"
> +
> +			wait $pid_first
> +			rc_first=$?
> +			wait $pid_second
> +			rc_second=$?

[ ... ]

> +			check_err $rc_vrf "VRF-bound listener did not get the connection"
> +			check_fail $rc_unbound "unbound listener got the connection"

[Severity: Low]
Does check_fail $rc_unbound accept more outcomes than intended?

do_server() in nettest returns 2 only when select() times out with no
connection:

tools/testing/selftests/net/nettest.c:do_server() {
	...
		rc = select(lsd+1, &rfds, NULL, NULL, ptval);
		if (rc == 0) {
			rc = 2;
			break;
		}
	...
err_exit:
	ipc_write(ipc_fd, 0);
	return 1;
}

Setup failures go through err_exit and return 1. That covers device
resolution, address validation, and bind or listen in lsock_init().

check_fail passes for any non-zero value. In the "vrf unbound" order, if
the unbound listener fails to set up, the VRF listener still takes the
connection and the subtest passes without testing that the two listeners
coexist.

The exempt subtest in unbound_listener() has a similar gap. Any client
failure counts as the expected rejection, and the server status is
discarded:

	connect "$ns_c" "$fam" "$addr"
	check_fail $? "VRF connection accepted by unbound listener"
	wait $pid

If the unbound listener never started, nothing is listening and the
connection is refused, yet the subtest still reports "rejected".

Should these checks require rc == 2 from the listener that must not get
the connection? Should the server status also be checked in the
unbound_listener() exempt case?

[ ... ]

> @@ -0,0 +1,213 @@
> +#!/bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# Test net.ipv4.tcp_l3mdev_exempt_ports.
> +#
> +# With tcp_l3mdev_accept=1, a listener that is not bound to a device also
> +# matches connections that arrive in a VRF. For a port listed in
> +# tcp_l3mdev_exempt_ports it must not: only a listener bound to the VRF
> +# may take such a connection. Connections in the default VRF are not
> +# affected.
> +#
> +#  ns_c (client in VRF)       ns_s (server)             ns_d (client in default VRF)
> +#      veth0 ------------- veth_vrf [vrf red]
> +#                             veth_def ----------------- veth0
> +
> +source lib.sh
> +
> +PORT=12345
> +OTHER_PORT=23456
> +VRF=red
> +VRF_TABLE=1000
> +
> +VRF_SRV4=172.16.1.1
> +VRF_CLI4=172.16.1.2
> +DEF_SRV4=172.16.2.1
> +DEF_CLI4=172.16.2.2
> +VRF_SRV6=2001:db8:1::1
> +VRF_CLI6=2001:db8:1::2
> +DEF_SRV6=2001:db8:2::1
> +DEF_CLI6=2001:db8:2::2
> +
> +TESTS="
> +	unbound_listener
> +	vrf_and_unbound_listeners
> +	default_vrf_unaffected
> +"
> +
> +cleanup()
> +{
> +	cleanup_all_ns
> +}
> +
> +setup()
> +{
> +	setup_ns ns_s ns_c ns_d || exit $ksft_skip
> +
> +	ip -n "$ns_s" link add "$VRF" type vrf table "$VRF_TABLE"

[Severity: Medium]
Does this skip correctly on a kernel without VRF support?

setup() creates the VRF device, enslaves veth_vrf to it and writes
net.ipv4.tcp_l3mdev_accept=1. It never checks whether any of these
succeeded. The only capability check is the test -e on
/proc/sys/net/ipv4/tcp_l3mdev_exempt_ports, which runs after setup().

That check does not cover the VRF dependencies. tcp_l3mdev_accept is
only registered under CONFIG_NET_L3_MASTER_DEV, and the vrf link type
needs CONFIG_NET_VRF. The new tcp_l3mdev_exempt_ports sysctl from patch
1/2 appears to be registered whenever CONFIG_SYSCTL is set.

On such a kernel, ip link add ... type vrf fails and veth_vrf stays in
the default VRF. The sysctl write also fails, with only a message on
stderr. The exempt_ports check then succeeds and tests_run() goes ahead.

The exempt cases in unbound_listener() then see the connection accepted,
because it never arrives in a VRF. The nettest -I red listeners in
vrf_and_unbound_listeners() cannot bind to the missing device. The
result is a series of FAILs rather than ksft_skip.

Could setup() bail out with $ksft_skip when the VRF cannot be created?
Alternatively, the script could check for
/proc/sys/net/ipv4/tcp_l3mdev_accept alongside the exempt_ports sysctl.
Checking the return of the remaining setup commands would also make
other setup failures visible, instead of having them show up as
unrelated test failures.

> +	ip -n "$ns_s" link set "$VRF" up
> +
> +	ip -n "$ns_s" link add veth_vrf type veth peer name veth0 netns "$ns_c"
> +	ip -n "$ns_s" link set veth_vrf master "$VRF"
> +	ip -n "$ns_s" link set veth_vrf up
> +	ip -n "$ns_s" addr add "$VRF_SRV4/24" dev veth_vrf
> +	ip -n "$ns_s" addr add "$VRF_SRV6/64" dev veth_vrf nodad
> +	ip -n "$ns_c" link set veth0 up
> +	ip -n "$ns_c" addr add "$VRF_CLI4/24" dev veth0
> +	ip -n "$ns_c" addr add "$VRF_CLI6/64" dev veth0 nodad
> +
> +	ip -n "$ns_s" link add veth_def type veth peer name veth0 netns "$ns_d"
> +	ip -n "$ns_s" link set veth_def up
> +	ip -n "$ns_s" addr add "$DEF_SRV4/24" dev veth_def
> +	ip -n "$ns_s" addr add "$DEF_SRV6/64" dev veth_def nodad
> +	ip -n "$ns_d" link set veth0 up
> +	ip -n "$ns_d" addr add "$DEF_CLI4/24" dev veth0
> +	ip -n "$ns_d" addr add "$DEF_CLI6/64" dev veth0 nodad
> +
> +	ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_accept=1
> +}
> +
> +ipver()
> +{
> +	[ "$1" = "-6" ] && echo 6 || echo 4
> +}
> +
> +set_exempt()
> +{
> +	ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_exempt_ports="$1"
> +}
> +
> +# Start a server. nettest sets SO_REUSEPORT on its listeners, so an
> +# unbound and a VRF-bound listener can share the port.
> +# $1: "-6" or "", $2: "vrf" or "unbound"
> +start_server()
> +{
> +	local dev_arg=""
> +
> +	[ "$2" = "vrf" ] && dev_arg="-I $VRF"
> +	# shellcheck disable=SC2086
> +	ip netns exec "$ns_s" ./nettest $1 -s $dev_arg -p "$PORT" -t 3 -q &
> +}
> +
> +# $1: client netns, $2: "-6" or "", $3: server address
> +connect()
> +{
> +	# shellcheck disable=SC2086
> +	ip netns exec "$1" ./nettest $2 -r "$3" -p "$PORT" -t 2 -q
> +}
> +
> +unbound_listener()
> +{
> +	local fam addr pid
> +
> +	for fam in "" "-6"; do
> +		addr=$VRF_SRV4
> +		[ "$fam" = "-6" ] && addr=$VRF_SRV6
> +
> +		RET=0
> +		set_exempt "$OTHER_PORT"
> +		start_server "$fam" unbound
> +		pid=$!
> +		wait_local_port_listen "$ns_s" "$PORT" tcp
> +		connect "$ns_c" "$fam" "$addr"
> +		check_err $? "VRF connection not accepted by unbound listener"
> +		wait $pid
> +		log_test "IPv$(ipver "$fam"): not exempt, VRF conn to unbound listener accepted"
> +
> +		RET=0
> +		set_exempt "$PORT"
> +		start_server "$fam" unbound
> +		pid=$!
> +		wait_local_port_listen "$ns_s" "$PORT" tcp
> +		connect "$ns_c" "$fam" "$addr"
> +		check_fail $? "VRF connection accepted by unbound listener"
> +		wait $pid
> +		log_test "IPv$(ipver "$fam"): exempt, VRF conn to unbound listener rejected"
> +	done
> +	set_exempt ""
> +}
> +
> +# Both an unbound and a VRF-bound listener on the port. The VRF connection
> +# must reach the VRF-bound one, whichever listener was created first.
> +vrf_and_unbound_listeners()
> +{
> +	local fam addr order first second pid_first pid_second
> +	local rc_first rc_second rc_vrf rc_unbound
> +
> +	set_exempt "$PORT"
> +	for fam in "" "-6"; do
> +		addr=$VRF_SRV4
> +		[ "$fam" = "-6" ] && addr=$VRF_SRV6
> +
> +		for order in "unbound vrf" "vrf unbound"; do
> +			read -r first second <<< "$order"
> +			RET=0
> +
> +			start_server "$fam" "$first"
> +			pid_first=$!
> +			wait_local_port_listen "$ns_s" "$PORT" tcp
> +			start_server "$fam" "$second"
> +			pid_second=$!
> +			sleep 0.2
> +
> +			connect "$ns_c" "$fam" "$addr"
> +			check_err $? "VRF connection failed"
> +
> +			wait $pid_first
> +			rc_first=$?
> +			wait $pid_second
> +			rc_second=$?
> +			if [ "$first" = "vrf" ]; then
> +				rc_vrf=$rc_first
> +				rc_unbound=$rc_second
> +			else
> +				rc_vrf=$rc_second
> +				rc_unbound=$rc_first
> +			fi
> +			check_err $rc_vrf "VRF-bound listener did not get the connection"
> +			check_fail $rc_unbound "unbound listener got the connection"
> +
> +			log_test "IPv$(ipver "$fam"): exempt, $first first, VRF-bound listener wins"
> +		done
> +	done
> +	set_exempt ""
> +}
> +
> +default_vrf_unaffected()
> +{
> +	local fam addr pid
> +
> +	set_exempt "$PORT"
> +	for fam in "" "-6"; do
> +		addr=$DEF_SRV4
> +		[ "$fam" = "-6" ] && addr=$DEF_SRV6
> +
> +		RET=0
> +		start_server "$fam" unbound
> +		pid=$!
> +		wait_local_port_listen "$ns_s" "$PORT" tcp
> +		connect "$ns_d" "$fam" "$addr"
> +		check_err $? "default VRF connection not accepted"
> +		wait $pid
> +		check_err $? "unbound listener did not get the connection"
> +		log_test "IPv$(ipver "$fam"): exempt, default VRF conn to unbound listener accepted"
> +	done
> +	set_exempt ""
> +}
> +
> +trap cleanup EXIT
> +
> +if [ ! -x ./nettest ]; then
> +	echo "SKIP: nettest not built"
> +	exit "$ksft_skip"
> +fi
> +
> +setup
> +if ! ip netns exec "$ns_s" test -e /proc/sys/net/ipv4/tcp_l3mdev_exempt_ports; then
> +	echo "SKIP: net.ipv4.tcp_l3mdev_exempt_ports not supported"
> +	exit "$ksft_skip"
> +fi
> +
> +tests_run
> +
> +exit "$EXIT_STATUS"
>

[Severity: Low]
Should this use the lib.sh helpers instead of open-coding them?

The script sources lib.sh, but it checks for ./nettest by hand and then
hardcodes ./nettest in start_server() and connect(). lib.sh already
provides check_gen_prog(). That helper looks the program up in PATH and
falls back to prepending $PWD. Other nettest users in this directory,
such as fcnal-test.sh, pmtu.sh and vrf_route_leaking.sh, call
check_gen_prog nettest and then invoke plain nettest.

With the open-coded check, running the script from any directory other
than tools/testing/selftests/net skips the whole test. This happens even
when nettest is installed and in PATH.

Separately, the script assigns TESTS unconditionally near the top.
tests_run() in lib.sh iterates over TESTS with ALL_TESTS as the
fallback:

tools/testing/selftests/net/lib.sh:tests_run() {
	...
	for current_test in ${TESTS:-$ALL_TESTS}; do
	...
}

Scripts are expected to define ALL_TESTS, so that a developer can run a
single case with something like:

  TESTS=default_vrf_unaffected ./tcp_l3mdev_exempt_ports.sh

Here that override is silently discarded and all three cases always
run.

Could this switch to ALL_TESTS and check_gen_prog nettest, with nettest
invoked through PATH?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09%40nexthop.ai

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-24 23:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 22:47 [PATCH net-next 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
2026-11-20 22:47 ` [PATCH net-next 1/2] " Kalash Nainwal via B4 Relay
2026-11-24 23:25   ` netdev-bot+sashiko
2026-11-20 22:47 ` [PATCH net-next 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
2026-11-24 23:25   ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox