[PATCH net-next v2 0/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl

From: Kalash Nainwal via B4 Relay

Date: Wed Nov 25 2026 - 16:18:16 EST


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@xxxxxxxxxx

To: "David S. Miller" <davem@xxxxxxxxxxxxx>
To: Eric Dumazet <edumazet@xxxxxxxxxx>
To: Jakub Kicinski <kuba@xxxxxxxxxx>
To: Paolo Abeni <pabeni@xxxxxxxxxx>
To: Simon Horman <horms@xxxxxxxxxx>
To: Jonathan Corbet <corbet@xxxxxxx>
To: Shuah Khan <skhan@xxxxxxxxxxxxxxxxxxx>
To: Randy Dunlap <rdunlap@xxxxxxxxxxxxx>
To: David Ahern <dsahern@xxxxxxxxxx>
To: Ido Schimmel <idosch@xxxxxxxxxx>
To: Neal Cardwell <ncardwell@xxxxxxxxxx>
To: Kuniyuki Iwashima <kuniyu@xxxxxxxxxx>
To: Shuah Khan <shuah@xxxxxxxxxx>
Cc: netdev@xxxxxxxxxxxxxxx
Cc: linux-doc@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: linux-kselftest@xxxxxxxxxxxxxxx

---
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@xxxxxxxxxx>