udp: fix auto-selected port colliding with an existing SO_REUSEPORT socket
Subsystems (4)
PATCH v2 1/2 udp: fix auto-selected port colliding with an existing SO_REUSEPORT socket
Observed with two independent servers in the same process:
fd1 = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, ...); bind(fd1, port 0); /* got 40000 */ fd2 = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, ...); bind(fd2, port 0); /* got 40000 as well */
Both sockets end up on the same port and join the same reuseport group, so each of them takes part of the other's datagrams. The same port sharing happens with SO_REUSEADDR, without the reuseport group.
udp_lib_lport_inuse() keeps the reuse rules when it scans for a free
port: a socket with SO_REUSEADDR set, or one with SO_REUSEPORT and the same uid, is not a conflict, so its port is never marked in the bitmap and the scan can hand it out again. Those rules only make sense when the user asks for a specific port. bind(0) wants a free port and has no way to know whose port it lands on.
TCP already does this: inet_csk_find_open_port() passes relax=false and reuseport_ok=false, so the scan treats every port in use as a conflict whatever options the sockets have. Commit aacd9289af8b ("tcp: bind() use stronger condition for bind_conflict") did it for SO_REUSEADDR and commit 0643ee4fd1b7 ("inet: Fix get port to handle zero port number with soreuseport set") for SO_REUSEPORT.
Do the same for UDP: ignore both options during a scan, keep them for an explicit port.
This changes bind(0) for SO_REUSEADDR sockets once the port range is
full: it used to share a port and now fails with EADDRINUSE, like TCP. Sharing a port on purpose when the range is full is a feature, TCP has net.ipv4.ip_autobind_reuse for it, off by default. UDP can get the same knob later, this patch only fixes the scan.
---
v1 -> v2: also fix reuseaddr suggested by Eric.
v1: https://lore.kernel.org/netdev/CANn89iKHH1s+kXqBXVKmDxmELU0fL-SwU4N8qHRUJjaNECMt_g@mail.gmail.com/
--- net/ipv4/udp.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
net/ipv4/udp.c +11 -10
@@ -139,25 +139,26 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,kuid_t uid =sk_uid (sk);struct sock *sk2; +/* With @bitmap we are scanning for a free port: every port in use +* is marked, whatever reuse options the sockets have. Without it +* we are checking a specific port and honour the reuse options. +*/ sk_for_each (sk2, &hslot->head) {if (net_eq (sock_net (sk2), net) && sk2 != sk && (bitmap ||udp_sk (sk2)->udp_port_hash == num) && - (!sk2->sk_reuse || !sk->sk_reuse) && + (bitmap || !sk2->sk_reuse || !sk->sk_reuse) && (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&inet_rcv_saddr_equal (sk, sk2, true)) { -if (sk2->sk_reuseport && sk->sk_reuseport && - !rcu_access_pointer (sk->sk_reuseport_cb) && -uid_eq (uid,sk_uid (sk2))) { -if (!bitmap) +if (!bitmap) { +if (sk2->sk_reuseport && sk->sk_reuseport && + !rcu_access_pointer (sk->sk_reuseport_cb) && +uid_eq (uid,sk_uid (sk2)))return 0 ; - }else { -if (!bitmap) -return 1 ; -__set_bit (udp_sk (sk2)->udp_port_hash >> log, - bitmap); +return 1 ; } +__set_bit (udp_sk (sk2)->udp_port_hash >> log, bitmap); } }return 0 ;
Signature · 1 line
2.43.0
PATCH v2 2/2 selftests/net: check auto-selected port with reuse options
Use IP_LOCAL_PORT_RANGE to limit a socket to 8 ports, bind 8 sockets to port 0 and check they get 8 distinct ports and the 9th bind fails with EADDRINUSE. Run it for the four combinations of reuse options on the sockets:
none (existing test)
SO_REUSEPORT
SO_REUSEADDR
SO_REUSEADDR + SO_REUSEPORT
TCP passes all four. Before the udp fix UDP only passes the first
one: the scan hands out a port already taken by a socket with the same reuse options, so fewer than 8 distinct ports come back and the 9th bind succeeds.
# ./ip_local_port_range.sh
Before:
ok 13 ip_local_port_range.ip4_udp.exhaust_8_port_range
not ok 14 ip_local_port_range.ip4_udp.exhaust_8_port_range_reuseport not ok 15 ip_local_port_range.ip4_udp.exhaust_8_port_range_reuseaddr not ok 16 ip_local_port_range.ip4_udp.exhaust_8_port_range_reuseaddr_reuseport ok 49 ip_local_port_range.ip6_udp.exhaust_8_port_range
not ok 50 ip_local_port_range.ip6_udp.exhaust_8_port_range_reuseport not ok 51 ip_local_port_range.ip6_udp.exhaust_8_port_range_reuseaddr not ok 52 ip_local_port_range.ip6_udp.exhaust_8_port_range_reuseaddr_reuseport
After:
ok 13 ip_local_port_range.ip4_udp.exhaust_8_port_range
ok 14 ip_local_port_range.ip4_udp.exhaust_8_port_range_reuseport ok 15 ip_local_port_range.ip4_udp.exhaust_8_port_range_reuseaddr ok 16 ip_local_port_range.ip4_udp.exhaust_8_port_range_reuseaddr_reuseport ok 49 ip_local_port_range.ip6_udp.exhaust_8_port_range
ok 50 ip_local_port_range.ip6_udp.exhaust_8_port_range_reuseport ok 51 ip_local_port_range.ip6_udp.exhaust_8_port_range_reuseaddr ok 52 ip_local_port_range.ip6_udp.exhaust_8_port_range_reuseaddr_reuseport
--- .../selftests/net/ip_local_port_range.c | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-)
tools/testing/selftests/net/ip_local_port_range.c +51 -1
@@ -301,7 +301,27 @@ TEST_F(ip_local_port_range, single_port_range) } } -TEST_F(ip_local_port_range, exhaust_8_port_range) +#define REUSEADDR 0x1 +#define REUSEPORT 0x2 + +static int set_reuse_opts (int fd,unsigned int flags) +{ +int one =1 ; + +if ((flags &REUSEADDR ) && +setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , &one,sizeof (one))) +return -1 ; + +if ((flags &REUSEPORT ) && +setsockopt (fd,SOL_SOCKET ,SO_REUSEPORT , &one,sizeof (one))) +return -1 ; + +return 0 ; +} + +static void exhaust_8_port_range (struct __test_metadata *_metadata, +const FIXTURE_VARIANT (ip_local_port_range) *variant, +unsigned int flags) {__u8 port_set =0 ;int i, fd, err;
@@ -313,6 +333,9 @@ TEST_F(ip_local_port_range, exhaust_8_port_range) fd =socket (variant->so_domain, variant->so_type, variant->so_protocol); ASSERT_GE(fd, 0) TH_LOG("socket failed"); + err =set_reuse_opts (fd, flags); + ASSERT_TRUE(!err) TH_LOG("setsockopt(SO_REUSE*) failed"); + range =pack_port_range (40000 ,40007 ); err =setsockopt (fd,SOL_IP ,IP_LOCAL_PORT_RANGE , &range,sizeof (range)); ASSERT_TRUE(!err) TH_LOG("setsockopt(IP_LOCAL_PORT_RANGE) failed");
@@ -335,6 +358,9 @@ TEST_F(ip_local_port_range, exhaust_8_port_range) fd =socket (variant->so_domain, variant->so_type, variant->so_protocol); ASSERT_GE(fd, 0) TH_LOG("socket failed"); + err =set_reuse_opts (fd, flags); + ASSERT_TRUE(!err) TH_LOG("setsockopt(SO_REUSE*) failed"); + range =pack_port_range (40000 ,40007 ); err =setsockopt (fd,SOL_IP ,IP_LOCAL_PORT_RANGE , &range,sizeof (range)); ASSERT_TRUE(!err) TH_LOG("setsockopt(IP_LOCAL_PORT_RANGE) failed");
@@ -352,6 +378,30 @@ TEST_F(ip_local_port_range, exhaust_8_port_range) } } +TEST_F(ip_local_port_range, exhaust_8_port_range) +{ +exhaust_8_port_range (_metadata, variant,0 ); +} + +/* Auto-selected port must not land on a port already taken by another +* socket, whatever reuse options both of them have, so all 8 sockets +* must get distinct ports. +*/ +TEST_F(ip_local_port_range, exhaust_8_port_range_reuseport) +{ +exhaust_8_port_range (_metadata, variant,REUSEPORT ); +} + +TEST_F(ip_local_port_range, exhaust_8_port_range_reuseaddr) +{ +exhaust_8_port_range (_metadata, variant,REUSEADDR ); +} + +TEST_F(ip_local_port_range, exhaust_8_port_range_reuseaddr_reuseport) +{ +exhaust_8_port_range (_metadata, variant,REUSEADDR |REUSEPORT ); +} + TEST_F(ip_local_port_range, late_bind) {union {
Signature · 1 line
2.43.0
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Mon, 28 Sep 2026 10:31:41 +0800 you wrote:
Observed with two independent servers in the same process:
fd1 = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, ...); bind(fd1, port 0); /* got 40000 */ fd2 = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, ...); bind(fd2, port 0); /* got 40000 as well */[...]
Here is the summary with links:
- [net-next,v2,1/2] udp: fix auto-selected port colliding with an existing SO_REUSEPORT socket
https://git.kernel.org/netdev/net-next/c/20d020a07536 - [net-next,v2,2/2] selftests/net: check auto-selected port with reuse options https://git.kernel.org/netdev/net-next/c/883c308be425
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html