Skip to content

Commit 8d6c414

Browse files
mikebcomkuba-moo
authored andcommitted
net: prefer socket bound to interface when not in VRF
The commit 6da5b0f ("net: ensure unbound datagram socket to be chosen when not in a VRF") modified compute_score() so that a device match is always made, not just in the case of an l3mdev skb, then increments the score also for unbound sockets. This ensures that sockets bound to an l3mdev are never selected when not in a VRF. But as unbound and bound sockets are now scored equally, this results in the last opened socket being selected if there are matches in the default VRF for an unbound socket and a socket bound to a dev that is not an l3mdev. However, handling prior to this commit was to always select the bound socket in this case. Reinstate this handling by incrementing the score only for bound sockets. The required isolation due to choosing between an unbound socket and a socket bound to an l3mdev remains in place due to the device match always being made. The same approach is taken for compute_score() for stream sockets. Fixes: 6da5b0f ("net: ensure unbound datagram socket to be chosen when not in a VRF") Fixes: e781905 ("net: ensure unbound stream socket to be chosen when not in a VRF") Signed-off-by: Mike Manning <[email protected]> Reviewed-by: David Ahern <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 7671b02 commit 8d6c414

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

net/ipv4/inet_hashtables.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ static inline int compute_score(struct sock *sk, struct net *net,
242242

243243
if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
244244
return -1;
245+
score = sk->sk_bound_dev_if ? 2 : 1;
245246

246-
score = sk->sk_family == PF_INET ? 2 : 1;
247+
if (sk->sk_family == PF_INET)
248+
score++;
247249
if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
248250
score++;
249251
}

net/ipv4/udp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ static int compute_score(struct sock *sk, struct net *net,
390390
dif, sdif);
391391
if (!dev_match)
392392
return -1;
393-
score += 4;
393+
if (sk->sk_bound_dev_if)
394+
score += 4;
394395

395396
if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
396397
score++;

net/ipv6/inet6_hashtables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static inline int compute_score(struct sock *sk, struct net *net,
106106
if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
107107
return -1;
108108

109-
score = 1;
109+
score = sk->sk_bound_dev_if ? 2 : 1;
110110
if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
111111
score++;
112112
}

net/ipv6/udp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ static int compute_score(struct sock *sk, struct net *net,
133133
dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
134134
if (!dev_match)
135135
return -1;
136-
score++;
136+
if (sk->sk_bound_dev_if)
137+
score++;
137138

138139
if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
139140
score++;

0 commit comments

Comments
 (0)