Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next v3 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
@ 2026-11-27 22:15 Kalash Nainwal via B4 Relay
  2026-11-27 22:15 ` [PATCH net-next v3 1/2] " Kalash Nainwal via B4 Relay
  2026-11-27 22:15 ` [PATCH net-next v3 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
  0 siblings, 2 replies; 4+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-27 22:15 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, 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 14 cases pass. Without the
  port listed, the first listener in the bucket wins (IPv4 fails when
  the 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 v3:
- Patch 1: explain in the commit message why this is opt-in instead of
  a change to the default lookup.
- Patch 1: drop the fast path annotation from the net_cachelines row;
  the field is not in a fast path cacheline group.
- Patch 2: add cases with a listener bound to the VRF slave device, and
  say in the header that such a listener also matches.
- Patch 2: stop the listener that must not get the connection instead
  of waiting for its timeout, so a slow setup cannot make it exit early.
- Patch 2: do not connect if the listeners did not start.
- Rebase on current net-next.
- Link to v2: https://patch.msgid.link/20261125-tcp-l3mdev-exempt-ports-v2-0-b920f3ff8f5e@nexthop.ai

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

---
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       | 297 +++++++++++++++++++++
 9 files changed, 371 insertions(+)
---
base-commit: f89726ffabef1b08c52e6163b7ab94b0d5f15503
change-id: 20261120-tcp-l3mdev-exempt-ports-c9f5dfebe356

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



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

* [PATCH net-next v3 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
  2026-11-27 22:15 [PATCH net-next v3 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
@ 2026-11-27 22:15 ` Kalash Nainwal via B4 Relay
  2026-12-04  4:03   ` willemb
  2026-11-27 22:15 ` [PATCH net-next v3 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay
  1 sibling, 1 reply; 4+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-27 22:15 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, 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.

This is opt-in rather than a change to the lookup itself. With both
listeners present, existing setups get a stable result that depends on
creation order and address family; making the bound listener always
win would change that for every tcp_l3mdev_accept user.

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 468d3df998a..4e0b5a4e77e 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..d531213d69e 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
 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] 4+ messages in thread

* [PATCH net-next v3 2/2] selftests: net: add tcp_l3mdev_exempt_ports test
  2026-11-27 22:15 [PATCH net-next v3 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
  2026-11-27 22:15 ` [PATCH net-next v3 1/2] " Kalash Nainwal via B4 Relay
@ 2026-11-27 22:15 ` Kalash Nainwal via B4 Relay
  1 sibling, 0 replies; 4+ messages in thread
From: Kalash Nainwal via B4 Relay @ 2026-11-27 22:15 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, 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 listener bound to
the VRF or to the receiving device (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       | 297 +++++++++++++++++++++
 2 files changed, 298 insertions(+)

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index fb0a6466205..45b4c3c3738 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -110,6 +110,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..f98774b8ba4
--- /dev/null
+++ b/tools/testing/selftests/net/tcp_l3mdev_exempt_ports.sh
@@ -0,0 +1,297 @@
+#!/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
+# or to the receiving device 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
+
+# Servers that must not get a connection are stopped by the test, so this
+# only needs to be longer than any wait before the client connects.
+SRV_TIMEOUT=20
+
+ALL_TESTS="
+	unbound_listener
+	vrf_and_unbound_listeners
+	slave_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 device-bound listener can share the port.
+# $1: IP version, $2: device to bind to, or "unbound"
+start_server()
+{
+	local args=()
+
+	[ "$1" = 6 ] && args+=(-6)
+	[ "$2" != unbound ] && args+=(-I "$2")
+	ip netns exec "$ns_s" nettest "${args[@]}" -s -p "$PORT" \
+		-t "$SRV_TIMEOUT" -q &
+}
+
+# Stop a server, ignoring its exit code.
+stop_server()
+{
+	kill "$1" 2>/dev/null
+	wait "$1" 2>/dev/null
+}
+
+# Stop a server that must not have received a connection. It must still
+# be running: had it accepted the connection or timed out, it would have
+# exited.
+# $1: pid, $2: name for the error message
+stop_idle_server()
+{
+	kill -0 "$1" 2>/dev/null
+	check_err $? "$2 exited, expected it to still be waiting"
+	stop_server "$1"
+}
+
+# $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"
+}
+
+unbound_listener()
+{
+	local ver addr pid
+
+	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=$!
+		if wait_listeners 1; then
+			connect "$ns_c" "$ver" "$addr"
+			check_err $? "VRF connection failed"
+			wait "$pid"
+			check_err $? "unbound listener did not get the conn"
+		else
+			check_err 1 "listener did not start"
+			stop_server "$pid"
+		fi
+		log_test "IPv$ver: not exempt, unbound listener gets VRF conn"
+
+		RET=0
+		set_exempt "$PORT"
+		start_server "$ver" unbound
+		pid=$!
+		if wait_listeners 1; then
+			connect "$ns_c" "$ver" "$addr"
+			check_fail $? "VRF connection succeeded"
+			stop_idle_server "$pid" "unbound listener"
+		else
+			check_err 1 "listener did not start"
+			stop_server "$pid"
+		fi
+		log_test "IPv$ver: exempt, unbound listener gets no VRF conn"
+	done
+	set_exempt ""
+}
+
+# An unbound listener and a listener bound to $1 on the port. The VRF
+# connection must reach the bound one, whichever was created first.
+# $1: the VRF device, or the VRF slave device the connection arrives on
+bound_and_unbound_listeners()
+{
+	local dev=$1
+	local ver addr order first second pid_first pid_second
+	local pid_bound pid_unbound
+
+	set_exempt "$PORT"
+	for ver in 4 6; do
+		addr=$VRF_SRV4
+		[ "$ver" = 6 ] && addr=$VRF_SRV6
+
+		for order in "unbound $dev" "$dev unbound"; do
+			read -r first second <<< "$order"
+			RET=0
+
+			pid_second=
+			start_server "$ver" "$first"
+			pid_first=$!
+			if wait_listeners 1; then
+				start_server "$ver" "$second"
+				pid_second=$!
+			fi
+			if [ -z "$pid_second" ] || ! wait_listeners 2; then
+				check_err 1 "listeners did not start"
+				stop_server "$pid_first"
+				[ -n "$pid_second" ] &&
+					stop_server "$pid_second"
+				log_test "IPv$ver: exempt, $first first"
+				continue
+			fi
+
+			if [ "$first" = unbound ]; then
+				pid_unbound=$pid_first
+				pid_bound=$pid_second
+			else
+				pid_bound=$pid_first
+				pid_unbound=$pid_second
+			fi
+
+			connect "$ns_c" "$ver" "$addr"
+			check_err $? "VRF connection failed"
+			wait "$pid_bound"
+			check_err $? "$dev listener did not get the connection"
+			stop_idle_server "$pid_unbound" "unbound listener"
+
+			log_test "IPv$ver: exempt, $first first, $dev wins"
+		done
+	done
+	set_exempt ""
+}
+
+vrf_and_unbound_listeners()
+{
+	bound_and_unbound_listeners "$VRF"
+}
+
+slave_and_unbound_listeners()
+{
+	bound_and_unbound_listeners veth_vrf
+}
+
+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=$!
+		if wait_listeners 1; then
+			connect "$ns_d" "$ver" "$addr"
+			check_err $? "default VRF connection failed"
+			wait "$pid"
+			check_err $? "unbound listener did not get the conn"
+		else
+			check_err 1 "listener did not start"
+			stop_server "$pid"
+		fi
+		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] 4+ messages in thread

* Re: [PATCH net-next v3 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl
  2026-11-27 22:15 ` [PATCH net-next v3 1/2] " Kalash Nainwal via B4 Relay
@ 2026-12-04  4:03   ` willemb
  0 siblings, 0 replies; 4+ messages in thread
From: willemb @ 2026-12-04  4:03 UTC (permalink / raw)
  To: Kalash Nainwal via B4 Relay, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, 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

Kalash Nainwal via B4 Relay wrote:
> 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.

> This is opt-in rather than a change to the lookup itself. With both
> listeners present, existing setups get a stable result that depends on
> creation order and address family; making the bound listener always
> win would change that for every tcp_l3mdev_accept user.

     This compatibility argument does not justify adding a permanent  
65536-bit
     port bitmap sysctl (and 8 KiB per network namespace) instead of fixing
     listener lookup:

     1. The current behavior is explicitly documented as unspecified, not a
        UAPI contract. Documentation/networking/vrf.rst (commit b1165777fe0b)
        already warns that with tcp_l3mdev_accept=1 "it is unspecified which
        listening socket will be selected to handle connections for VRF
        traffic" and calls out this exact TCP-MD5 failure mode.

     2. No deployment can meaningfully rely on the current behavior. Because
        inet_hash() prepends AF_INET sockets (LIFO) and appends AF_INET6
        SO_REUSEPORT sockets (FIFO), the same startup order selects opposite
        listeners for IPv4 vs IPv6, and any daemon/VRF restart flips the
        winner. Worse, whenever the unbound listener sits ahead in the  
bucket,
        the VRF-bound listener is completely unreachable (it cannot match
        non-VRF traffic and its own VRF traffic is stolen). For non-reuseport
        listeners, compute_score() already prefers sk_bound_dev_if != 0.

     3. Netdev precedent is to fix SO_REUSEPORT short-circuit shadowing in  
the
        lookup without a sysctl:
        - d894ba18d4e4 ("soreuseport: fix ordering for mixed v4/v6 sockets")
        - d9fbc7f6431f / 0ee58dad5b06 ("net: tcp[6]: prefer listeners bound  
to
          an address")
        - 8d6c414cd2fb ("net: prefer socket bound to interface when not in  
VRF")

     4. The bitmap sysctl leaves the real bug unfixed everywhere else: every
        unlisted port keeps the IPv4/IPv6 divergence and MD5/AO bypass,
        non-VRF SO_BINDTODEVICE + unbound SO_REUSEPORT (sdif == 0) remains
        broken regardless of the sysctl, and UDP (udp_l3mdev_accept=1,
        udp[46]_lib_lookup2() when !reuseport_has_conns()) has the same early
        return.

     Please drop the sysctl and rework the series to fix device-bound
     listener preference in inet_lhash2_lookup() / inet6_lhash2_lookup()

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

end of thread, other threads:[~2026-12-04  4:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 22:15 [PATCH net-next v3 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl Kalash Nainwal via B4 Relay
2026-11-27 22:15 ` [PATCH net-next v3 1/2] " Kalash Nainwal via B4 Relay
2026-12-04  4:03   ` willemb
2026-11-27 22:15 ` [PATCH net-next v3 2/2] selftests: net: add tcp_l3mdev_exempt_ports test Kalash Nainwal via B4 Relay

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