Re: [PATCH net-next v3 1/2] tcp: add net.ipv4.tcp_l3mdev_exempt_ports sysctl

From: Kalash Nainwal

Date: Fri Dec 04 2026 - 11:27:55 EST


On Thu, Dec 3, 2026 at 9:03 PM <willemb@xxxxxxxxxx> wrote:
>
> Kalash Nainwal via B4 Relay wrote:
> > From: Kalash Nainwal <kalash@xxxxxxxxxx>
>
> > 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()

Thanks Willem, really appreciate your inputs here.

Ack, I will rework this series. I wasn't too excited about introducing this
exception knob, but I was trying to err on the side of caution to ensure I
don't break some application implicitly relying on this incorrect lookup.

-Kalash