Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next v2 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
@ 2026-11-25 21:17 Kalash Nainwal via B4 Relay
  2026-11-25 21:17 ` [PATCH net-next v2 1/2] " Kalash Nainwal via B4 Relay
  2026-11-25 21:17 ` [PATCH net-next v2 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-25 21:17 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: Shuah Khan, Joel Granados, 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.

Why a sysctl rather than changing the default lookup: with both
listeners present, existing setups get a result that is stable but
depends on creation order and address family. Making the VRF-bound
listener always win would change that for every tcp_l3mdev_accept
user, so this series keeps the default and makes the new behavior
opt-in per port. If changing the default is preferred, I can rework it
that way.

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

---
Changes in v2:
- Patch 1: only build the sysctl with CONFIG_NET_L3_MASTER_DEV, next to
  tcp_l3mdev_accept.
- Patch 1: say in the docs that only listener lookup changes, and that
  a listener bound to the receiving device also matches.
- Patch 1: add the field to net_cachelines/netns_ipv4_sysctl.rst.
- Patch 2: wait for the listeners with ss instead of a fixed sleep.
- Patch 2: skip if VRF is not supported; fail if setup fails.
- Patch 2: require a timeout from the listener that must not get the
  connection.
- Patch 2: use ALL_TESTS and check_gen_prog; fix shellcheck warnings.
- Wrap lines at 80 columns.
- Link to v1: https://patch.msgid.link/20261120-tcp-l3mdev-exempt-ports-v1-0-ccc2a3a6cd09@nexthop.ai

To: "David S. Miller" <davem@davemloft.net>
To: Eric Dumazet <edumazet@kernel.org>
To: Jakub Kicinski <kuba@kernel.org>
To: Paolo Abeni <pabeni@redhat.com>
To: Simon Horman <horms@kernel.org>
To: Jonathan Corbet <corbet@lwn.net>
To: Shuah Khan <skhan@linuxfoundation.org>
To: Randy Dunlap <rdunlap@infradead.org>
To: David Ahern <dsahern@kernel.org>
To: Ido Schimmel <idosch@nvidia.com>
To: Neal Cardwell <ncardwell@google.com>
To: Kuniyuki Iwashima <kuniyu@google.com>
To: Shuah Khan <shuah@kernel.org>
Cc: netdev@vger.kernel.org
Cc: linux-doc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org

---
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             |  24 ++
 .../net_cachelines/netns_ipv4_sysctl.rst           |   1 +
 include/net/ip.h                                   |  15 ++
 include/net/netns/ipv4.h                           |   3 +
 net/ipv4/inet_hashtables.c                         |   5 +
 net/ipv4/sysctl_net_ipv4.c                         |  21 ++
 net/ipv6/inet6_hashtables.c                        |   4 +
 tools/testing/selftests/net/Makefile               |   1 +
 .../selftests/net/tcp_l3mdev_exempt_ports.sh       | 260 +++++++++++++++++++++
 9 files changed, 334 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 v2 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
  2026-11-25 21:17 [PATCH net-next v2 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
@ 2026-11-25 21:17 ` Kalash Nainwal via B4 Relay
  2026-11-26 21:20   ` netdev-bot+sashiko
  2026-11-25 21:17 ` [PATCH net-next v2 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-25 21:17 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: Shuah Khan, Joel Granados, 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
listener does not match connections that arrive in a VRF, so only a
listener bound to the VRF or to the receiving device can take them.
Only listener lookup changes. 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             | 24 ++++++++++++++++++++++
 .../net_cachelines/netns_ipv4_sysctl.rst           |  1 +
 include/net/ip.h                                   | 15 ++++++++++++++
 include/net/netns/ipv4.h                           |  3 +++
 net/ipv4/inet_hashtables.c                         |  5 +++++
 net/ipv4/sysctl_net_ipv4.c                         | 21 +++++++++++++++++++
 net/ipv6/inet6_hashtables.c                        |  4 ++++
 7 files changed, 73 insertions(+)

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index 3cc80d9b024..c66b9420cdf 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -619,6 +619,30 @@ tcp_l3mdev_accept - BOOLEAN
 
 	Default: 0 (disabled)
 
+tcp_l3mdev_exempt_ports - list of comma separated ranges
+	TCP ports for which tcp_l3mdev_accept does not apply to listener
+	lookup. When tcp_l3mdev_accept is enabled, a listen socket that is
+	not bound to a 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 or to the receiving device can accept them.
+	Connections that do not arrive in an L3 domain are not affected.
+	This applies to both IPv4 and IPv6.
+
+	Only the choice of listen socket changes. Lookup of established
+	sockets and bind() port conflict checks still follow
+	tcp_l3mdev_accept.
+
+	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/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst b/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
index 2833ce6cba1..062153c2793 100644
--- a/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
+++ b/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
@@ -150,6 +150,7 @@ struct_ping_group_range         ping_group_range
 atomic_t                        dev_addr_genid
 unsigned_int                    sysctl_udp_child_hash_entries
 unsigned_long*                  sysctl_local_reserved_ports
+unsigned_long*                  sysctl_tcp_l3mdev_exempt_ports                                   read_mostly         __inet_lookup_listener/inet6_lookup_listener
 int                             sysctl_ip_prot_sock
 struct_mr_table*                mrt
 struct_list_head                mr_tables
diff --git a/include/net/ip.h b/include/net/ip.h
index 5035b313a26..a5f9efe15b2 100644
--- a/include/net/ip.h
+++ b/include/net/ip.h
@@ -396,6 +396,21 @@ static inline bool inet_port_requires_bind_service(struct net *net, unsigned sho
 }
 #endif
 
+/* For ports listed in net.ipv4.tcp_l3mdev_exempt_ports, a listener that
+ * is not bound to a device does not match connections received on an L3
+ * master device slave, even with tcp_l3mdev_accept=1. Only listener
+ * lookup uses this. Applies to IPv4 and IPv6.
+ */
+static inline bool tcp_port_l3mdev_exempt(const struct net *net,
+					  unsigned short port)
+{
+#if defined(CONFIG_SYSCTL) && defined(CONFIG_NET_L3_MASTER_DEV)
+	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..bff91c8b30c 100644
--- a/include/net/netns/ipv4.h
+++ b/include/net/netns/ipv4.h
@@ -271,6 +271,9 @@ struct netns_ipv4 {
 
 #ifdef CONFIG_SYSCTL
 	unsigned long *sysctl_local_reserved_ports;
+#ifdef CONFIG_NET_L3_MASTER_DEV
+	unsigned long *sysctl_tcp_l3mdev_exempt_ports;
+#endif
 	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..153b21f607a 100644
--- a/net/ipv4/sysctl_net_ipv4.c
+++ b/net/ipv4/sysctl_net_ipv4.c
@@ -903,6 +903,13 @@ static const struct ctl_table ipv4_net_table[] = {
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= SYSCTL_ONE,
 	},
+	{
+		.procname	= "tcp_l3mdev_exempt_ports",
+		.data		= &init_net.ipv4.sysctl_tcp_l3mdev_exempt_ports,
+		.maxlen		= 65536,
+		.mode		= 0644,
+		.proc_handler	= proc_do_large_bitmap,
+	},
 #endif
 	{
 		.procname	= "tcp_mtu_probing",
@@ -1708,10 +1715,21 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
 	if (!net->ipv4.sysctl_local_reserved_ports)
 		goto err_ports;
 
+#ifdef CONFIG_NET_L3_MASTER_DEV
+	net->ipv4.sysctl_tcp_l3mdev_exempt_ports =
+		kzalloc(65536 / 8, GFP_KERNEL);
+	if (!net->ipv4.sysctl_tcp_l3mdev_exempt_ports)
+		goto err_l3mdev_ports;
+#endif
+
 	proc_fib_multipath_hash_set_seed(net, 0);
 
 	return 0;
 
+#ifdef CONFIG_NET_L3_MASTER_DEV
+err_l3mdev_ports:
+	kfree(net->ipv4.sysctl_local_reserved_ports);
+#endif
 err_ports:
 	unregister_net_sysctl_table(net->ipv4.ipv4_hdr);
 err_reg:
@@ -1729,6 +1747,9 @@ 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);
+#ifdef CONFIG_NET_L3_MASTER_DEV
+	kfree(net->ipv4.sysctl_tcp_l3mdev_exempt_ports);
+#endif
 }
 
 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 v2 2/2] selftests: net: add tcp_l3mdev_exempt_ports test
  2026-11-25 21:17 [PATCH net-next v2 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
  2026-11-25 21:17 ` [PATCH net-next v2 1/2] " Kalash Nainwal via B4 Relay
@ 2026-11-25 21:17 ` Kalash Nainwal via B4 Relay
  2026-11-26 21:20   ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-25 21:17 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: Shuah Khan, Joel Granados, 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       | 260 +++++++++++++++++++++
 2 files changed, 261 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..da7f20bf2e9
--- /dev/null
+++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
@@ -0,0 +1,260 @@
+#!/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, default VRF)
+#      veth0 ------------- veth_vrf [vrf red]
+#                             veth_def ----------------- veth0
+
+# ns_s, ns_c and ns_d are assigned by setup_ns, and the test functions
+# are called through ALL_TESTS.
+# shellcheck disable=SC2154,SC2329
+
+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
+
+# nettest server exit code when no connection arrives before its timeout
+SRV_TIMEOUT=2
+
+ALL_TESTS="
+	unbound_listener
+	vrf_and_unbound_listeners
+	default_vrf_unaffected
+"
+
+cleanup()
+{
+	cleanup_all_ns
+}
+
+setup_fail()
+{
+	echo "FAIL: setup"
+	exit "$ksft_fail"
+}
+
+setup()
+{
+	setup_ns ns_s ns_c ns_d || exit "$ksft_skip"
+
+	if ! ip -n "$ns_s" link add "$VRF" type vrf table "$VRF_TABLE"; then
+		echo "SKIP: VRF not supported"
+		exit "$ksft_skip"
+	fi
+
+	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
+
+	ip -n "$ns_s" -batch - <<-EOF || setup_fail
+		link set $VRF up
+		link add veth_vrf type veth peer name veth0 netns $ns_c
+		link set veth_vrf master $VRF
+		link set veth_vrf up
+		addr add $VRF_SRV4/24 dev veth_vrf
+		addr add $VRF_SRV6/64 dev veth_vrf nodad
+		link add veth_def type veth peer name veth0 netns $ns_d
+		link set veth_def up
+		addr add $DEF_SRV4/24 dev veth_def
+		addr add $DEF_SRV6/64 dev veth_def nodad
+	EOF
+
+	ip -n "$ns_c" -batch - <<-EOF || setup_fail
+		link set veth0 up
+		addr add $VRF_CLI4/24 dev veth0
+		addr add $VRF_CLI6/64 dev veth0 nodad
+	EOF
+
+	ip -n "$ns_d" -batch - <<-EOF || setup_fail
+		link set veth0 up
+		addr add $DEF_CLI4/24 dev veth0
+		addr add $DEF_CLI6/64 dev veth0 nodad
+	EOF
+
+	ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_accept=1 ||
+		setup_fail
+}
+
+set_exempt()
+{
+	ip netns exec "$ns_s" sysctl -wq net.ipv4.tcp_l3mdev_exempt_ports="$1"
+}
+
+# Start a server in the background. nettest sets SO_REUSEPORT on its
+# listeners, so an unbound and a VRF-bound listener can share the port.
+# $1: IP version, $2: "vrf" or "unbound"
+start_server()
+{
+	local args=()
+
+	[ "$1" = 6 ] && args+=(-6)
+	[ "$2" = vrf ] && args+=(-I "$VRF")
+	ip netns exec "$ns_s" nettest "${args[@]}" -s -p "$PORT" -t 3 -q &
+}
+
+# $1: client netns, $2: IP version, $3: server address
+connect()
+{
+	local args=()
+
+	[ "$2" = 6 ] && args+=(-6)
+	ip netns exec "$1" nettest "${args[@]}" -r "$3" -p "$PORT" -t 2 -q
+}
+
+listeners_are()
+{
+	local n
+
+	n=$(ip netns exec "$ns_s" ss -Hltn "sport = :$PORT" | wc -l)
+	[ "$n" -eq "$1" ]
+}
+
+# $1: number of listeners to wait for on $PORT
+wait_listeners()
+{
+	slowwait 2 listeners_are "$1"
+}
+
+# $1: exit code of a server that must not have received a connection
+check_timeout()
+{
+	[ "$1" -eq "$SRV_TIMEOUT" ]
+	check_err $? "$2 exited with $1, expected timeout"
+}
+
+unbound_listener()
+{
+	local ver addr pid rc
+
+	for ver in 4 6; do
+		addr=$VRF_SRV4
+		[ "$ver" = 6 ] && addr=$VRF_SRV6
+
+		RET=0
+		set_exempt "$OTHER_PORT"
+		start_server "$ver" unbound
+		pid=$!
+		wait_listeners 1
+		check_err $? "listener did not start"
+		connect "$ns_c" "$ver" "$addr"
+		check_err $? "VRF connection failed"
+		wait "$pid"
+		check_err $? "unbound listener did not get the connection"
+		log_test "IPv$ver: not exempt, unbound listener gets VRF conn"
+
+		RET=0
+		set_exempt "$PORT"
+		start_server "$ver" unbound
+		pid=$!
+		wait_listeners 1
+		check_err $? "listener did not start"
+		connect "$ns_c" "$ver" "$addr"
+		check_fail $? "VRF connection succeeded"
+		wait "$pid"
+		rc=$?
+		check_timeout "$rc" "unbound listener"
+		log_test "IPv$ver: exempt, unbound listener gets no VRF conn"
+	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 ver addr order first second pid_first pid_second
+	local rc_first rc_second rc_vrf rc_unbound
+
+	set_exempt "$PORT"
+	for ver in 4 6; do
+		addr=$VRF_SRV4
+		[ "$ver" = 6 ] && addr=$VRF_SRV6
+
+		for order in "unbound vrf" "vrf unbound"; do
+			read -r first second <<< "$order"
+			RET=0
+
+			start_server "$ver" "$first"
+			pid_first=$!
+			wait_listeners 1
+			check_err $? "first listener did not start"
+			start_server "$ver" "$second"
+			pid_second=$!
+			wait_listeners 2
+			check_err $? "second listener did not start"
+
+			connect "$ns_c" "$ver" "$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 listener did not get the conn"
+			check_timeout "$rc_unbound" "unbound listener"
+
+			log_test "IPv$ver: exempt, $first first, VRF one wins"
+		done
+	done
+	set_exempt ""
+}
+
+default_vrf_unaffected()
+{
+	local ver addr pid
+
+	set_exempt "$PORT"
+	for ver in 4 6; do
+		addr=$DEF_SRV4
+		[ "$ver" = 6 ] && addr=$DEF_SRV6
+
+		RET=0
+		start_server "$ver" unbound
+		pid=$!
+		wait_listeners 1
+		check_err $? "listener did not start"
+		connect "$ns_d" "$ver" "$addr"
+		check_err $? "default VRF connection failed"
+		wait "$pid"
+		check_err $? "unbound listener did not get the connection"
+		log_test "IPv$ver: exempt, unbound listener gets default conn"
+	done
+	set_exempt ""
+}
+
+trap cleanup EXIT
+
+check_gen_prog nettest
+setup
+tests_run
+
+exit "$EXIT_STATUS"

-- 
2.43.0



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

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

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

Critical: 0 · High: 0 · Medium: 1 · Low: 1

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 names the real cause of the misselection:
  the lookup returns whichever reuseport listener is first in the listen…
- [Low] The new net_cachelines documentation row lists
  sysctl_tcp_l3mdev_exempt_ports as an RX fast-path field:…

--

tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl

This commit adds a per-netns port bitmap sysctl,
net.ipv4.tcp_l3mdev_exempt_ports. For listed ports, an unbound listener
does not match connections that arrive through an L3 master device
(sdif != 0) during listener lookup, even with tcp_l3mdev_accept=1. The
goal is for VRF connections to reach the VRF-bound listener when both
listeners use SO_REUSEPORT.

> diff --git a/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst b/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
> index 2833ce6cba1d4..062153c2793c7 100644
> --- a/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
> +++ b/Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst
> @@ -150,6 +150,7 @@ struct_ping_group_range         ping_group_range
>  atomic_t                        dev_addr_genid
>  unsigned_int                    sysctl_udp_child_hash_entries
>  unsigned_long*                  sysctl_local_reserved_ports
> +unsigned_long*                  sysctl_tcp_l3mdev_exempt_ports                                   read_mostly         __inet_lookup_listener/inet6_lookup_listener

[Severity: Low]
This row marks sysctl_tcp_l3mdev_exempt_ports as read_mostly on the RX
fast path. In struct netns_ipv4, though, the field is added to the
CONFIG_SYSCTL area next to sysctl_local_reserved_ports. That is outside
the netns_ipv4_read_rx group:

include/net/netns/ipv4.h:
	__cacheline_group_begin(netns_ipv4_read_rx);
	u8 sysctl_tcp_moderate_rcvbuf;
	u8 sysctl_ip_early_demux;
	u8 sysctl_tcp_early_demux;
	u8 sysctl_tcp_l3mdev_accept;
	...
	__cacheline_group_end(netns_ipv4_read_rx);

netns_ipv4_struct_check() in net/core/net_namespace.c also has no
CACHELINE_ASSERT_GROUP_MEMBER() for it. Other read_mostly RX fields,
such as sysctl_tcp_l3mdev_accept, do have one.

Should the document leave the fast path columns empty for this field?
Or should the field move into netns_ipv4_read_rx with a matching
assertion?

>  int                             sysctl_ip_prot_sock
>  struct_mr_table*                mrt
>  struct_list_head                mr_tables

[ ... ]

> 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;
> +
>  		score =  sk->sk_bound_dev_if ? 2 : 1;

[Severity: Medium]
The commit message gives the cause of the misselection: "the lookup
returns whichever is first in the listen bucket, not the VRF-bound one".
Is a per-port opt-in sysctl the right fix for that, rather than a change
to the lookup itself?

The early return is in inet_lhash2_lookup():

	sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
		score = compute_score(sk, net, hnum, daddr, dif, sdif);
		if (score > hiscore) {
			result = inet_lookup_reuseport(net, sk, skb, doff,
						       saddr, sport, daddr, hnum, inet_ehashfn);
			if (result)
				return result;

The first reuseport socket that scores above zero is returned at once,
and the rest of the bucket is never scored. So the higher score for a
device-bound listener (sk->sk_bound_dev_if ? 2 : 1) has no effect.
inet6_lhash2_lookup() has the same early return.

inet_reuseport_add_sock() requires sk2->sk_bound_dev_if ==
sk->sk_bound_dev_if, so the unbound and bound listeners are always in
separate reuseport groups.

inet_hash() puts AF_INET6 reuseport sockets at the tail of the bucket
and all other sockets at the head. That explains the IPv4/IPv6
difference.

With this patch the default list is empty. For every port that is not
listed, the IPv4/IPv6 inconsistency and the TCP-MD5/AO bypass from the
commit message still happen.

The new check is also gated on sdif. What about the same setup without
a VRF? For example:

  - an unbound SO_REUSEPORT listener and an SO_BINDTODEVICE(eth0)
    SO_REUSEPORT listener on the same port
  - a SYN arrives on eth0, so sdif == 0

inet_bound_dev_eq() matches both sockets. The unbound listener can still
win when it is first in the bucket, whatever the sysctl is set to.

Selecting purely by score has caveats of its own. The sk_incoming_cpu
bonus can raise an unbound IPv6 listener to the score of a bound one
(1+1 vs 2). Changing the default lookup behavior could also affect
existing deployments.

Could the commit message explain why a new UAPI knob was chosen instead
of fixing the selection order in inet_lhash2_lookup() and
inet6_lhash2_lookup()?

[ ... ]

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

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

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

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The header comment of the new test script
  tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh (lines 8-9)…
- [Low] In vrf_and_unbound_listeners() in
  tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh, the first…

--

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 the VRF-bound listener in either creation order and
is refused by an unbound-only listener. It also checks that default-VRF
connections still reach the unbound listener, for both 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..da7f20bf2e9e6
> --- /dev/null
> +++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
> @@ -0,0 +1,260 @@
> +#!/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.

[Severity: Low]
This isn't a bug, but is this comment narrower than what the kernel
does?

Documentation/networking/ip-sysctl.rst describes tcp_l3mdev_exempt_ports
as:

    only a listen socket bound to the L3
    master device or to the receiving device can accept them.

The check in compute_score() (net/ipv4/inet_hashtables.c, and its twin
in net/ipv6/inet6_hashtables.c) only rejects sockets with no bound
device:

    if (!sk->sk_bound_dev_if && sdif &&
        tcp_port_l3mdev_exempt(net, hnum))
            return -1;

A listener bound to the slave device (veth_vrf here) still matches
through the sdif comparison in inet_sk_bound_dev_eq().

Could the comment say "bound to the VRF or to the receiving device"?

Also, none of the cases uses a listener bound to the slave device, so
that documented behaviour has no test coverage. Would it be worth adding
one?

[ ... ]

> +# Start a server in the background. nettest sets SO_REUSEPORT on its
> +# listeners, so an unbound and a VRF-bound listener can share the port.
> +# $1: IP version, $2: "vrf" or "unbound"
> +start_server()
> +{
> +	local args=()
> +
> +	[ "$1" = 6 ] && args+=(-6)
> +	[ "$2" = vrf ] && args+=(-I "$VRF")
> +	ip netns exec "$ns_s" nettest "${args[@]}" -s -p "$PORT" -t 3 -q &
> +}

[ ... ]

> +		for order in "unbound vrf" "vrf unbound"; do
> +			read -r first second <<< "$order"
> +			RET=0
> +
> +			start_server "$ver" "$first"
> +			pid_first=$!
> +			wait_listeners 1
> +			check_err $? "first listener did not start"
> +			start_server "$ver" "$second"
> +			pid_second=$!
> +			wait_listeners 2
> +			check_err $? "second listener did not start"
> +
> +			connect "$ns_c" "$ver" "$addr"
> +			check_err $? "VRF connection failed"

[Severity: Low]
Can the first server time out before the client's SYN arrives here?

start_server() gives each nettest a fixed -t 3. In nettest.c, do_server()
does a single select() with that budget right after listen(), and closes
the listener when it expires:

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

The first server's 3 second timer is already running while the script
does all of this:

  - finishes the slowwait poll
  - starts the second server through ip netns exec
  - polls until wait_listeners 2 sees two listeners (up to 2s)
  - starts the client through ip netns exec

On a slow or debug kernel (KASAN, lockdep) these steps can add up to
about 3s.

With order "vrf unbound", the VRF listener would then be gone. The
exempt port makes the unbound listener refuse the connection, so the
connect fails and rc_vrf is 2, even though the kernel behaved correctly.

With order "unbound vrf", the unbound listener exits early. The
check_timeout() on rc_unbound then passes trivially, and the subtest no
longer shows that the unbound listener was skipped.

If wait_listeners itself times out, check_err records it, but the
subtest still goes on to connect.

Would a server -t larger than the total wait budget avoid this?

> +
> +			wait "$pid_first"
> +			rc_first=$?
> +			wait "$pid_second"
> +			rc_second=$?

[ ... ]

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

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

end of thread, other threads:[~2026-11-26 21:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 21:17 [PATCH net-next v2 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
2026-11-25 21:17 ` [PATCH net-next v2 1/2] " Kalash Nainwal via B4 Relay
2026-11-26 21:20   ` netdev-bot+sashiko
2026-11-25 21:17 ` [PATCH net-next v2 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
2026-11-26 21:20   ` 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