From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796015267; b=4PLUXcxMU/WeOxcQcCLY7KFa8lTMq4qvn8/+E4t5AqrxhFW5uXaygqVsXMp2S2OLI5XW X9WEtkJEqH6TiFP6uFCcdaDP2b/VtLlVqpwdCTpC8z9E1t7R0hGx73Q07Uacpa+mLaVOa L+l68dI+5rw87OO4dNl07aVnCjZy9vEmhVeUjvgwTOAd/iNcYwRtYSMNVieKTZRPMucWl 4Q0Xa4DUruZKx4R6yvcojlMQFN1Mh27Q4+1pH+hAwrDB6Do4kfhjttUudRGosyjDFjf4b o6zu02QH1YpqV0Qmz/NMbdZiVe7v3JZJ65ys9rECagfCGJcTBmdeDCls8l4gafmrarA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796015267; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=MECISgZH1tgsIkN/iqA5WqGfOTmKIW/6dINKcP7W5ZY=; b=qLBMHgwuPUrV6/jRe/Kxxc3EypZojxjY70BZPKPpNKB/25ckAhuy9FtIFE/2LAcIFjUg s/y/qx9OJq3AhB5MeCnkjFRa8YfDvMQb0b7uXNaTXSIHf2PoBa0x3OLURZcsI3D0dU02D +9erix9t2zVrIZJP90tIo8rygis5kFQ3zgzpAYfGPCRuo1wlbWNc8o+Au5eMlc7ed/00F yungNq7SCMLif3g5kc4pu8gOH9XRaIMcdcpxqFGRQNq2HjRu62K4vxbsWWOiAxzcNdnSC 6SKlBPtav62XRxDGy0Clckj6I6Z7Mzem7DqmnpUui6lwEJ5kOWpoCm+NETVzm1teVBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="F4/BS9F2"; arc=none smtp.remote-ip=209.85.128.177 Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88FF8606A32A for ; Mon, 30 Nov 2026 05:07:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88FF8606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=F4/BS9F2 Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-8b5981c4834so20378407b3.2 for ; Sun, 29 Nov 2026 21:07:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796015266; x=1796620066; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MECISgZH1tgsIkN/iqA5WqGfOTmKIW/6dINKcP7W5ZY=; b=F4/BS9F2qKVlf+E+h4DaZyUFqaNTCMhojyr8DhJdFZYrbVkeYG8SiYc6PeteCB+qQ+ EW7btyXU422yvkvqwz+/ErcxMjlC6c9Edn4olynMzPn86ZUbEAQFZMHIGeX0+z3eF/XX zVuwUclmiUa63v/AjmOy4GxMSr+uh3MUlrpeD82JF2oJyAMIJ4YfeojySQovW8Ez3VD1 KwK6DHAa+DJYT1zj6Ch54Gr5U+qWXVkTyTJBXu79neJDs5nAh06KiC2oOM0DKftIVVI4 rA0gjCT+i6LYarKZL9KrmeHY04gwNfYDTHlP9mzVEuDswksr8UMntukB1kI60xBnCs/f 78UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796015266; x=1796620066; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=MECISgZH1tgsIkN/iqA5WqGfOTmKIW/6dINKcP7W5ZY=; b=dWLK+tJUjJIxOJQ1rn2jXRTp+99tsAdZMkyQ1fZ1CvUymoCGUpha/JMRG+6/3GHAy0 gvEb8PmcNS5sUsyeGVg2MvArTQuKsYjMg3SCVnxt0WK1yasGE/g58e6ONBXgYnIS0ojt wtyla1Ta5q3W488TuRmOFm6e99rpgTuWqelZMnFKLMHwAo7jzS7C3QVtWhNUMvpW7/uF GIqyq5rWd4902fXwcWllFuW/ep7Q8+A4/ZnbEkK2x34VcPKS2Gf2aVj55Chpc0L7Lz2/ ZQZznyGLPI4eqiQTAiQHCWY1Sn2wMx0kkHJUv9WTt0ZvuNZSoxe5O3FOfQ+/dtmnp0YS V5VA== X-Forwarded-Encrypted: i=1; AL+hErHXpvoqlo9HiMw9CnX3bEEFmIUUUdbbXHELtCu/Rqi0hibLpe05qA5bEP/keoe4YtaTd/CvTgI=@vger.kernel.org X-Gm-Message-State: APuU4Z/ZVNosQNzRgBVBx+oTXu8zoGzKu1PswuEwAwM6AEZmQg8uyF05 EahkYNp4IjZ/5G9MUdxCgqNcD4rjuCmeakKyzdrf2/jKlPNBC96KmR+pQ27lR9OZm4I= X-Gm-Gg: AZFdTZdSpehbX9AxRTjIb/b7Sh2LYolJTIr0FGe3SoVR3pddghAXxSMarFQc2gkH+uX ngUcW0YIuc7waIcTC4Rg2lnoYs7EoBNIJz6YG3pIQhfOKuCAebmF7zZz7yYjSKfneH3wix2/4Uj ONRlVBlgF3CZatI7AcpcWspHMFmD5xwugcC/CPEOW4lWs98upbMP/whM82DE3s+6hRVEmHJ5KY8 yhsVVEk9rf0cm+H2QD+Tl5vUU4napBSmYSMatoUY+K5taRDBiZuxrD02EI5RrM4Scxu67mkww8k 2Ypgw+u38mz6M+qEAomOZ3yL+deCHvjB2iRbwvuSR1s0rPqVClGuyIHQs0GP8DM5pQpAZZuNEZ0 qvkEYF2BVjl+BnI3pL9t4QXUzzc0GHLUjh1CdyfCPbfWDxwa4XPDbpQxPeRVhssldfRgwXX64Xw 3BR+iORqaQmSy5yZi7+prEHgSn/fFcovFeH8YQ90Uba9ekytH12aaZI+KcBLuw6U3lgqwCeUCt9 qfelNbWXM354ZmMMmSMyWjKSEctQW9v2AjXHA3+ns90jlIPD5AhqOeHnV3/+c7n3k4EW1Yk9Sow T0umrzcFSAeBTsRTqINVnFLKU6Oh/LRKat/PGkeRpduTrVFJtCP0 X-Received: by 2002:a05:690c:4a06:b0:8b4:d8c6:4d9c with SMTP id 00721157ae682-8b5204a113emr126384537b3.34.1796015266472; Sun, 29 Nov 2026 21:07:46 -0800 (PST) Received: from VPN-156-19.vpn.usf.edu ([2607:fe50:0:f802::4046]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b52028c7f5sm126540357b3.12.2026.11.29.21.07.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 29 Nov 2026 21:07:45 -0800 (PST) From: Juan Agustin Niccolini To: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com Cc: horms@kernel.org, kuniyu@google.com, willemb@google.com, willemdebruijn.kernel@gmail.com, dsahern@kernel.org, idosch@nvidia.com, shuah@kernel.org, tpluszz77@gmail.com, brauner@kernel.org, alice.kernel@fastmail.im, kylebot@openai.com, joe@dama.to, leitao@debian.org, xudu@redhat.com, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Date: Mon, 30 Nov 2026 00:07:29 -0500 Message-ID: <20261130050729.893-4-juanagusnicco@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261130050729.893-1-juanagusnicco@gmail.com> References: <20261130050729.893-1-juanagusnicco@gmail.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add tests for the two bind conflict fixes in this series and for the SO_REUSEPORT one in commit d77700039d09 ("udp: only quit inuse scan early on identical SO_REUSEPORT sockets"): - reuseport_scan_{address,device,hash2}: a SO_REUSEPORT bind is refused when a conflicting socket sits behind a compatible one, on the primary and on the secondary hash. - disconnect_keeps_{device,scoped_device}: connect(AF_UNSPEC) keeps a device set with SO_BINDTOIFINDEX or with the scope id of bind(). - connect_device_ignored: a device set by connect() does not let a socket on another device bind next to it. reuseport_share and disconnect_clears_connect_device check that the sharing and the clearing that should still happen still do. Signed-off-by: Juan Agustin Niccolini --- v2: - Keep the SO_REUSEPORT tests, now for commit d77700039d09, and update the commit message (Willem). - v1: https://lore.kernel.org/netdev/20261120174341.35873-5-juanagusnicco@gmail.com/ tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 1 + .../testing/selftests/net/udp_bind_conflict.c | 228 ++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 tools/testing/selftests/net/udp_bind_conflict.c diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore index 27761239d069..ff75923ef369 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -58,6 +58,7 @@ tools tun txring_overwrite txtimestamp +udp_bind_conflict udp_splice_checksum udpgso udpgso_bench_rx diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index baea3e7f0f8f..fc8fe8465038 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -206,6 +206,7 @@ TEST_GEN_PROGS := \ tap \ tcp_port_share \ tls \ + udp_bind_conflict \ udp_splice_checksum \ # end of TEST_GEN_PROGS diff --git a/tools/testing/selftests/net/udp_bind_conflict.c b/tools/testing/selftests/net/udp_bind_conflict.c new file mode 100644 index 000000000000..9a67f58c348a --- /dev/null +++ b/tools/testing/selftests/net/udp_bind_conflict.c @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Check that bind() refuses a UDP socket that overlaps another one in + * the cases that SO_REUSEPORT, disconnect and connect() used to let + * through. + */ + +#include +#include +#include +#include +#include + +#include "kselftest_harness.h" + +#define PORT 34567 +#define MAX_FDS 32 + +FIXTURE(udp_bind_conflict) +{ + int fd[MAX_FDS]; + int nr_fds; + int lo; + int d0; +}; + +FIXTURE_SETUP(udp_bind_conflict) +{ + ASSERT_EQ(unshare(CLONE_NEWNET), 0); + ASSERT_EQ(system("ip link set dev lo up"), 0); + ASSERT_EQ(system("ip link add d0 type dummy"), 0); + ASSERT_EQ(system("ip link set dev d0 up"), 0); + ASSERT_EQ(system("ip addr add fe80::1/64 dev d0 nodad"), 0); + + self->lo = if_nametoindex("lo"); + self->d0 = if_nametoindex("d0"); + ASSERT_NE(self->lo, 0); + ASSERT_NE(self->d0, 0); + self->nr_fds = 0; +} + +FIXTURE_TEARDOWN(udp_bind_conflict) +{ + while (self->nr_fds) + close(self->fd[--self->nr_fds]); +} + +/* Bind a UDP socket to addr:PORT, on device @ifindex if not 0: through + * the scope id for a link-local address, SO_BINDTOIFINDEX otherwise. + * Returns the socket, or -errno. + */ +static int udp_bind(FIXTURE_DATA(udp_bind_conflict) *self, const char *addr, + int ifindex, bool reuseport) +{ + union { + struct sockaddr sa; + struct sockaddr_in v4; + struct sockaddr_in6 v6; + } sa = {}; + socklen_t len; + int one = 1; + int fd, err; + + if (self->nr_fds == MAX_FDS) + return -ENOSPC; + + if (inet_pton(AF_INET, addr, &sa.v4.sin_addr) == 1) { + sa.v4.sin_family = AF_INET; + sa.v4.sin_port = htons(PORT); + len = sizeof(sa.v4); + } else if (inet_pton(AF_INET6, addr, &sa.v6.sin6_addr) == 1) { + sa.v6.sin6_family = AF_INET6; + sa.v6.sin6_port = htons(PORT); + len = sizeof(sa.v6); + if (IN6_IS_ADDR_LINKLOCAL(&sa.v6.sin6_addr)) { + sa.v6.sin6_scope_id = ifindex; + ifindex = 0; + } + } else { + return -EINVAL; + } + + fd = socket(sa.sa.sa_family, SOCK_DGRAM, 0); + if (fd < 0) + return -errno; + + if ((reuseport && + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) || + (ifindex && + setsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX, + &ifindex, sizeof(ifindex))) || + bind(fd, &sa.sa, len)) { + err = -errno; + close(fd); + return err; + } + + self->fd[self->nr_fds++] = fd; + return fd; +} + +static int disconnect(int fd) +{ + struct sockaddr sa = { .sa_family = AF_UNSPEC }; + + return connect(fd, &sa, sizeof(sa)); +} + +/* connect() to a link-local peer, which sets the device to @ifindex */ +static int connect_link_local(int fd, int ifindex) +{ + struct sockaddr_in6 sa = { + .sin6_family = AF_INET6, + .sin6_port = htons(PORT), + .sin6_scope_id = ifindex, + }; + + inet_pton(AF_INET6, "fe80::2", &sa.sin6_addr); + return connect(fd, (struct sockaddr *)&sa, sizeof(sa)); +} + +static int bound_ifindex(int fd) +{ + socklen_t len = sizeof(int); + int ifindex; + + if (getsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX, &ifindex, &len)) + return -errno; + return ifindex; +} + +/* The conflict scan used to stop at the first SO_REUSEPORT socket of the + * same owner that overlapped the new one, before reaching 127.0.0.2. + */ +TEST_F(udp_bind_conflict, reuseport_scan_address) +{ + ASSERT_LE(0, udp_bind(self, "127.0.0.2", 0, false)); + ASSERT_LE(0, udp_bind(self, "127.0.0.3", 0, true)); + ASSERT_EQ(-EADDRINUSE, udp_bind(self, "0.0.0.0", 0, true)); +} + +TEST_F(udp_bind_conflict, reuseport_scan_device) +{ + ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, false)); + ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->d0, true)); + ASSERT_EQ(-EADDRINUSE, udp_bind(self, "0.0.0.0", 0, true)); +} + +/* More than 10 sockets on the port: the secondary hash is scanned. */ +TEST_F(udp_bind_conflict, reuseport_scan_hash2) +{ + char addr[INET_ADDRSTRLEN]; + int i; + + ASSERT_LE(0, udp_bind(self, "127.0.0.2", self->lo, false)); + for (i = 3; i < 14; i++) { + snprintf(addr, sizeof(addr), "127.0.0.%d", i); + ASSERT_LE(0, udp_bind(self, addr, 0, true)); + } + ASSERT_LE(0, udp_bind(self, "127.0.0.2", self->d0, true)); + ASSERT_EQ(-EADDRINUSE, udp_bind(self, "127.0.0.2", 0, true)); +} + +/* Sockets of one owner that SO_REUSEPORT lets share a port still can. */ +TEST_F(udp_bind_conflict, reuseport_share) +{ + int i; + + for (i = 0; i < 16; i++) + ASSERT_LE(0, udp_bind(self, "127.0.0.1", 0, true)); + ASSERT_LE(0, udp_bind(self, "0.0.0.0", 0, true)); + ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, true)); + ASSERT_LE(0, udp_bind(self, "127.0.0.1", self->d0, true)); +} + +/* Disconnect used to clear the device the socket was bound to, leaving it + * on top of 0.0.0.0%lo where bind() had refused it. + */ +TEST_F(udp_bind_conflict, disconnect_keeps_device) +{ + int fd; + + ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, false)); + ASSERT_EQ(-EADDRINUSE, udp_bind(self, "127.0.0.1", 0, false)); + + fd = udp_bind(self, "127.0.0.1", self->d0, false); + ASSERT_LE(0, fd); + ASSERT_EQ(0, disconnect(fd)); + ASSERT_EQ(self->d0, bound_ifindex(fd)); +} + +TEST_F(udp_bind_conflict, disconnect_keeps_scoped_device) +{ + int fd; + + fd = udp_bind(self, "fe80::1", self->d0, false); + ASSERT_LE(0, fd); + ASSERT_EQ(0, disconnect(fd)); + ASSERT_EQ(self->d0, bound_ifindex(fd)); +} + +/* A device that connect() set is still cleared by disconnect. */ +TEST_F(udp_bind_conflict, disconnect_clears_connect_device) +{ + int fd; + + fd = udp_bind(self, "::", 0, false); + ASSERT_LE(0, fd); + ASSERT_EQ(0, connect_link_local(fd, self->d0)); + ASSERT_EQ(self->d0, bound_ifindex(fd)); + ASSERT_EQ(0, disconnect(fd)); + ASSERT_EQ(0, bound_ifindex(fd)); +} + +/* A device that connect() set used to count in the conflict check, so a + * socket on another device could bind next to [::]:PORT and overlap it + * once it disconnected. + */ +TEST_F(udp_bind_conflict, connect_device_ignored) +{ + int fd; + + fd = udp_bind(self, "::", 0, false); + ASSERT_LE(0, fd); + ASSERT_EQ(0, connect_link_local(fd, self->d0)); + ASSERT_EQ(-EADDRINUSE, udp_bind(self, "::", self->lo, false)); +} + +TEST_HARNESS_MAIN -- 2.43.0