[PATCH net-next v4 1/2] tcp: prefer a device-bound listener over an unbound SO_REUSEPORT one

From: Kalash Nainwal via B4 Relay

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


From: Kalash Nainwal <kalash@xxxxxxxxxx>

compute_score() scores a listener bound to a device higher than an
unbound one, but inet_lhash2_lookup() and inet6_lhash2_lookup() return
the SO_REUSEPORT pick of the first socket that beats the current best
score. An unbound and a device-bound listener are never in the same
reuseport group, so when the unbound one is first in the bucket it
takes the connections the device-bound one should get. AF_INET
reuseport listeners are added at the head of the bucket and AF_INET6
ones at the tail, so the result also differs between IPv4 and IPv6.

This affects connections that arrive in a VRF with tcp_l3mdev_accept=1,
for example when only the VRF-bound listener has TCP-MD5 keys, and
connections that arrive on a device that a listener is bound to with
SO_BINDTODEVICE.

Prefer a matching device-bound socket over an unbound one whatever
their scores, and use the score only between sockets of the same kind.
After a pick from an unbound reuseport group, keep scanning the bucket
for a device-bound socket instead of returning the pick.

To keep the fast reuseport return for everyone else, count the
device-bound listeners in each listening bucket, and only keep
scanning when the count is not zero. The count is updated under the
bucket lock when a listener is hashed or unhashed, and when
SO_BINDTODEVICE changes the device of a hashed listener. This follows
commit acdcecc61285 ("udp: correct reuseport selection with connected
sockets"), which falls back to scoring only for reuseport groups that
have connected sockets.

Update Documentation/networking/vrf.rst, which described the listener
choice for VRF traffic as unspecified.

Signed-off-by: Kalash Nainwal <kalash@xxxxxxxxxx>
---
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 +++++++++++++++++---
5 files changed, 77 insertions(+), 18 deletions(-)

diff --git a/Documentation/networking/vrf.rst b/Documentation/networking/vrf.rst
index 0a9a6f968cb9..e49ce85c235e 100644
--- a/Documentation/networking/vrf.rst
+++ b/Documentation/networking/vrf.rst
@@ -144,18 +144,18 @@ default VRF are only handled by a socket not bound to any VRF::
netfilter rules on the VRF device can be used to limit access to services
running in the default VRF context as well.

-Using VRF-aware applications (applications which simultaneously create sockets
-outside and inside VRFs) in conjunction with ``net.ipv4.tcp_l3mdev_accept=1``
-is possible but may lead to problems in some situations. With that sysctl
-value, it is unspecified which listening socket will be selected to handle
-connections for VRF traffic; ie. either a socket bound to the VRF or an unbound
-socket may be used to accept new connections from a VRF. This somewhat
-unexpected behavior can lead to problems if sockets are configured with extra
-options (ex. TCP MD5 keys) with the expectation that VRF traffic will
-exclusively be handled by sockets bound to VRFs, as would be the case with
-``net.ipv4.tcp_l3mdev_accept=0``. Finally and as a reminder, regardless of
-which listening socket is selected, established sockets will be created in the
-VRF based on the ingress interface, as documented earlier.
+VRF-aware applications (applications which simultaneously create sockets
+outside and inside VRFs) can be used in conjunction with
+``net.ipv4.tcp_l3mdev_accept=1``. With that sysctl value, both a listening
+socket bound to the VRF and an unbound one match connections from the VRF.
+Among listening sockets bound to the same local address, the one bound to the
+VRF (or to the ingress device) is selected, also when the sockets use
+SO_REUSEPORT; the unbound socket only accepts connections from VRFs that have
+no listening socket of their own. A listening socket bound to a specific local
+address is still selected before one bound to the wildcard address, whether or
+not it is bound to a device. Finally and as a reminder, regardless of which
+listening socket is selected, established sockets will be created in the VRF
+based on the ingress interface, as documented earlier.

--------------------------------------------------------------------------------

diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h
index 44b7e39a1245..4638bf292942 100644
--- a/include/net/inet_hashtables.h
+++ b/include/net/inet_hashtables.h
@@ -140,6 +140,8 @@ struct inet_bind_hashbucket {
#define LISTENING_NULLS_BASE (1U << 29)
struct inet_listen_hashbucket {
spinlock_t lock;
+ /* Number of listeners in nulls_head bound to a device */
+ unsigned int bound;
struct hlist_nulls_head nulls_head;
};

@@ -308,6 +310,7 @@ bool inet_ehash_nolisten(struct sock *sk, struct sock *osk,
bool *found_dup_sk);
int inet_hash(struct sock *sk);
void inet_unhash(struct sock *sk);
+void inet_listen_bound_change(struct sock *sk, int delta);

struct sock *__inet_lookup_listener(const struct net *net,
struct sk_buff *skb, int doff,
diff --git a/net/core/sock.c b/net/core/sock.c
index aa2fc17b78ef..9ec8a9727be6 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -644,6 +644,11 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
if (ifindex < 0)
return -EINVAL;

+ /* Keep the count of device-bound TCP listeners up to date */
+ if (IS_ENABLED(CONFIG_INET) && sk_is_tcp(sk) &&
+ !sk->sk_bound_dev_if != !ifindex)
+ inet_listen_bound_change(sk, ifindex ? 1 : -1);
+
/* Paired with all READ_ONCE() done locklessly. */
WRITE_ONCE(sk->sk_bound_dev_if, ifindex);

diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 860145d810ed..86c58dcc16ea 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -421,18 +421,33 @@ static struct sock *inet_lhash2_lookup(const struct net *net,
const int dif, const int sdif)
{
struct sock *sk, *result = NULL;
+ bool result_bound = false, unbound_reuse = false;
struct hlist_nulls_node *node;
int score, hiscore = 0;

sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
+ bool bound = sk->sk_bound_dev_if;
+
+ /* A matching device-bound socket is always preferred over an
+ * unbound one.
+ */
+ if (!bound && (result_bound || unbound_reuse))
+ continue;
+
score = compute_score(sk, net, hnum, daddr, dif, sdif);
- if (score > hiscore) {
+ if (score > hiscore || (score > 0 && bound && !result_bound)) {
result = inet_lookup_reuseport(net, sk, skb, doff,
saddr, sport, daddr, hnum, inet_ehashfn);
- if (result)
+ /* Without device-bound listeners in the bucket, the
+ * pick of the first matching reuseport group is final.
+ */
+ if (result && (bound || !READ_ONCE(ilb2->bound)))
return result;

- result = sk;
+ unbound_reuse = !!result;
+ if (!result)
+ result = sk;
+ result_bound = bound;
hiscore = score;
}
}
@@ -818,6 +833,8 @@ int inet_hash(struct sock *sk)
goto unlock;
}
sock_set_flag(sk, SOCK_RCU_FREE);
+ if (sk->sk_bound_dev_if)
+ WRITE_ONCE(ilb2->bound, ilb2->bound + 1);
if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
sk->sk_family == AF_INET6)
__sk_nulls_add_node_tail_rcu(sk, &ilb2->nulls_head);
@@ -853,6 +870,8 @@ void inet_unhash(struct sock *sk)
reuseport_stop_listen_sock(sk);

__sk_nulls_del_node_init_rcu(sk);
+ if (sk->sk_bound_dev_if)
+ WRITE_ONCE(ilb2->bound, ilb2->bound - 1);
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
spin_unlock(&ilb2->lock);
} else {
@@ -865,6 +884,22 @@ void inet_unhash(struct sock *sk)
}
}

+/* A listener changed between unbound and device-bound: update the count
+ * of device-bound listeners in its bucket.
+ */
+void inet_listen_bound_change(struct sock *sk, int delta)
+{
+ struct inet_listen_hashbucket *ilb2;
+
+ if (sk->sk_state != TCP_LISTEN || sk_unhashed(sk))
+ return;
+
+ ilb2 = inet_lhash2_bucket_sk(tcp_get_hashinfo(sk), sk);
+ spin_lock(&ilb2->lock);
+ WRITE_ONCE(ilb2->bound, ilb2->bound + delta);
+ spin_unlock(&ilb2->lock);
+}
+
static bool inet_bind2_bucket_match(const struct inet_bind2_bucket *tb,
const struct net *net, unsigned short port,
int l3mdev, const struct sock *sk)
@@ -1305,6 +1340,7 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,

for (i = 0; i <= h->lhash2_mask; i++) {
spin_lock_init(&h->lhash2[i].lock);
+ h->lhash2[i].bound = 0;
INIT_HLIST_NULLS_HEAD(&h->lhash2[i].nulls_head,
i + LISTENING_NULLS_BASE);
}
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
index 1eea584d80df..ecebac66ba0c 100644
--- a/net/ipv6/inet6_hashtables.c
+++ b/net/ipv6/inet6_hashtables.c
@@ -192,18 +192,33 @@ static struct sock *inet6_lhash2_lookup(const struct net *net,
const unsigned short hnum, const int dif, const int sdif)
{
struct sock *sk, *result = NULL;
+ bool result_bound = false, unbound_reuse = false;
struct hlist_nulls_node *node;
int score, hiscore = 0;

sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
+ bool bound = sk->sk_bound_dev_if;
+
+ /* A matching device-bound socket is always preferred over an
+ * unbound one.
+ */
+ if (!bound && (result_bound || unbound_reuse))
+ continue;
+
score = compute_score(sk, net, hnum, daddr, dif, sdif);
- if (score > hiscore) {
+ if (score > hiscore || (score > 0 && bound && !result_bound)) {
result = inet6_lookup_reuseport(net, sk, skb, doff,
saddr, sport, daddr, hnum, inet6_ehashfn);
- if (result)
+ /* Without device-bound listeners in the bucket, the
+ * pick of the first matching reuseport group is final.
+ */
+ if (result && (bound || !READ_ONCE(ilb2->bound)))
return result;

- result = sk;
+ unbound_reuse = !!result;
+ if (!result)
+ result = sk;
+ result_bound = bound;
hiscore = score;
}
}

--
2.43.0