blob: 1b866917530b608511ed9eded23338497be07a12 [file] [log] [blame]
From 486722ec5964283c5e0d0340a96846a61b677692 Mon Sep 17 00:00:00 2001
From: Willem de Bruijn <>
Date: Wed, 19 Feb 2020 14:16:32 -0500
Subject: [PATCH] udp: rehash on disconnect
commit 303d0403b8c25e994e4a6e45389e173cf8706fb5 upstream.
As of the below commit, udp sockets bound to a specific address can
coexist with one bound to the any addr for the same port.
The commit also phased out the use of socket hashing based only on
port (hslot), in favor of always hashing on {addr, port} (hslot2).
The change broke the following behavior with disconnect (AF_UNSPEC):
server binds to
server connects to
server disconnects
client connects to
client sends "hello"
server reads "hello" // times out, packet did not find sk
On connect the server acquires a specific source addr suitable for
routing to its destination. On disconnect it reverts to the any addr.
The connect call triggers a rehash to a different hslot2. On
disconnect, add the same to return to the original hslot2.
Skip this step if the socket is going to be unhashed completely.
Fixes: 4cdeeee9252a ("net: udp: prefer listeners bound to an address")
Reported-by: Pavel Roskin <>
Signed-off-by: Willem de Bruijn <>
Reviewed-by: Eric Dumazet <>
Signed-off-by: David S. Miller <>
Signed-off-by: Paul Gortmaker <>
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 6fe14862e55b..6455e0875fa2 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1851,8 +1851,12 @@ int __udp_disconnect(struct sock *sk, int flags)
inet->inet_dport = 0;
sk->sk_bound_dev_if = 0;
- if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
+ if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
+ if (sk->sk_prot->rehash &&
+ (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
+ sk->sk_prot->rehash(sk);
+ }
if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {