[PATCH net-next v4 0/2] tcp: prefer device-bound listeners over unbound SO_REUSEPORT ones

From: Kalash Nainwal via B4 Relay

Date: Sat Dec 05 2026 - 00:06:33 EST


When an unbound TCP listener and a listener bound to a device share a
port with SO_REUSEPORT, the listener lookup returns whichever is first
in the listen bucket, so the more specific device-bound listener can
lose. AF_INET and AF_INET6 reuseport listeners are added at opposite
ends of the bucket, so the same setup picks different listeners for
IPv4 and IPv6.

We hit this with a BGP daemon in a VRF with tcp_l3mdev_accept=1: only
the VRF-bound :179 listener had TCP-MD5 keys, and IPv6 peers in the VRF
reached the unbound listener and came up without MD5. The same happens
without a VRF, with SO_BINDTODEVICE.

v1-v3 added a sysctl to exempt ports from tcp_l3mdev_accept in the
listener lookup. Following review, v4 drops it and fixes the lookup
instead (patch 1). Patch 2 replaces the selftest.

Notes:
- The fast reuseport return is kept unless the listen bucket holds a
device-bound listener. Each bucket counts its device-bound listeners
under the bucket lock, so a bucket without one behaves as before. With
one, after a pick from an unbound reuseport group, the lookup walks
the rest of the bucket for a device-bound socket, skipping unbound
sockets without scoring them. This follows the has_conns check that
commit acdcecc61285 added for UDP.
- If the SK_REUSEPORT program of an unbound group refuses a connection,
a matching device-bound listener still gets it. Without one, the
connection is refused as before.
- UDP (udp4_lib_lookup2() and udp6_lib_lookup2()) has the same early
return. I can send a follow-up for it if this approach is acceptable.

Testing:
- New selftest in virtme-ng: all 20 cases pass. Without patch 1, 7
cases fail in the expected pattern: IPv4 when the device-bound
listener is created first, IPv6 when the unbound one is.
- Without the SO_BINDTODEVICE update of the count, the IPv4 case with
a listener bound after listen() fails.
- fcnal-test.sh ipv4_tcp (134 tests) and ipv6_tcp (132 tests),
reuseport_addr_any, reuseport_dualstack, reuseport_bpf,
reuseport_bpf_cpu and so_incoming_cpu pass.

---
Changes in v4:
- Drop the tcp_l3mdev_exempt_ports sysctl and fix the listener lookup
instead: prefer a matching device-bound listener over an unbound one
whatever their scores, and after a pick from an unbound reuseport
group, keep looking for one when the bucket has device-bound
listeners.
- Update Documentation/networking/vrf.rst, which described the listener
choice for VRF traffic as unspecified.
- Replace the selftest with tcp_bound_dev_listener.sh, which also covers
SO_BINDTODEVICE without a VRF, and a listener bound to the VRF after
listen() (new nettest --bind-after-listen option).
- Rebase on current net-next.
- Link to v3: https://patch.msgid.link/20261127-tcp-l3mdev-exempt-ports-v3-0-f875b1184f26@xxxxxxxxxx

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

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

---
Kalash Nainwal (2):
tcp: prefer a device-bound listener over an unbound SO_REUSEPORT one
selftests: net: add tcp_bound_dev_listener test

Documentation/networking/vrf.rst | 24 +-
include/net/inet_hashtables.h | 3 +
net/core/sock.c | 5 +
net/ipv4/inet_hashtables.c | 42 ++-
net/ipv6/inet6_hashtables.c | 21 +-
tools/testing/selftests/net/Makefile | 1 +
tools/testing/selftests/net/nettest.c | 17 +-
.../selftests/net/tcp_bound_dev_listener.sh | 308 +++++++++++++++++++++
8 files changed, 401 insertions(+), 20 deletions(-)
---
base-commit: d5cf954cebe4f175dfd834e1dc471a3aa203ec7d
change-id: 20261120-tcp-l3mdev-exempt-ports-c9f5dfebe356

Best regards,
--
Kalash Nainwal <kalash@xxxxxxxxxx>