From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795196631; b=1P1vTeAZ8xJTnidu7PsAIhX/05EsQ2YSvON42Qck8fQ6drLXmkifA89bcEb4Be7hZmLZ QzmCtOeyIxjOWvcDfEr17WnjqcTL+zwx5PTrjLO1Yd4W5EER6jzAT/Se+NyduzITpbAWI DhgUhkgnASg5HxTWm6VHddi9stTPkJbqPXtX0uopLaH8lupwszZJS2+Tz6JbNbgEeiiQn CiidTvKloPEmifkA+fsdwrzbdXz3jbXn1A17X5Z31tPc9pnP9Zl2WXE2nQIS+bxLHGZvc 6CBYvI2/qVUIQylB0qk0R52uJIHxgmLLjf28CU4sWCmasLRDHqdJh6gofREvOrQamAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795196631; 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=Lk7Qvdty/kP051BKcCJgvdEe/6/wWSs6Fi35IE/5fU4=; b=Pyo4q84XniNtmCnIVZcuhx/34mJQONNp85Rh5ZmG7NbG04pglVoh9psSeE6PvLF8GtdI k1sdeG2XZM9x5FJ0Y6UbXL3oxMC9F9PEp4UUgPKK88lF+gWnQ5sM/IBnj0kLmGojwAaoa XPWSK0El+g35M1JutJtWmgGRvzs9ra+k6FkhHNrC/tSxajCMMQ7txNB/LGNithOi1hki7 +tGUiBsLm6WNaO4L59D7Pb5t5ijqDFr8Wj155MZKxclqA0mMKizIOJepcP/2ARnZ4PPdx oQeDN9Y/jo8UeSxFBVHGW63q1/KFNI3y26THD5zy8I5r3viN6ok+m0ysCY9uveYTmRA== 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=cXhwszKz; arc=none smtp.remote-ip=209.85.222.47 Received: from mail-ua1-f47.google.com (mail-ua1-f47.google.com [209.85.222.47]) (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 EDEBC60A261E for ; Fri, 20 Nov 2026 17:43:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EDEBC60A261E 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=cXhwszKz Received: by mail-ua1-f47.google.com with SMTP id a1e0cc1a2514c-99e1babd683so599264241.0 for ; Fri, 20 Nov 2026 09:43:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795196628; x=1795801428; 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=Lk7Qvdty/kP051BKcCJgvdEe/6/wWSs6Fi35IE/5fU4=; b=cXhwszKzmi1uRYcxKJ7V8hxhEioP8bAdekNIhlMKyG0EqFYvPG3AlJsX987pmURtlV NHDTiFkp89uslxjmENfjeHiiXwFHdQmrsMOWuTgTYbQYjxFYfUqF6x1lkDbxOV4nJgfn L2SCtwuOtfucDNTpFxKZyEvJ0JU9lDco0gd26MoeBlmAeGZbvLxrJjrI+axboUV6hFW+ mbk6fhkbleUXrB7PeVdg64yH5waz74ZIPbyI8xatcH9dzfUzlGBWgF+TgCw58YMXu/1a q3/wd+ThUp+ANBNrCo0Z8Idxe64USQvfjvt4EQAvb7RFHa6cDoz/N2ddiGVvtRLIBm8T 2Ssw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795196628; x=1795801428; 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=Lk7Qvdty/kP051BKcCJgvdEe/6/wWSs6Fi35IE/5fU4=; b=dFitSSbal7u6P3OhV14i9L6sT9E2MET+SlNBtf/hDjZoc8L7zuxKG9ww+QsAdKMSk0 0rj7IgwCviaud4XtOsG3MYLmQdJ8i+EnfDV7N7MAIYktF2KL6OIZTjo4KHJEQHDomXI0 edha4anVy1XbMUfrzxJ1dM6r+hogJ0RhkeOgpgfRIaci0CD1k8qEzkcYBd3PcsbLmb8G vxACiJ9cuhtZzCM2HxpmCxCsmpTuEUYxf0yexDm9vlj6EUd/dUyqii+pcfLbgAH76pfV NxpsU1fGP1IDB0ZvQ46IJ2XqcdUIqlGMcCG2hFNExAPds2SeR2/KjhczRhnhxXRibFQQ 1KSw== X-Forwarded-Encrypted: i=1; AL+hErFjRv2oG1w+KS1wsXowfcyPttIu2XucqawVIZuUvGtswBTAbqiyWCWAhdbWsMJTeQ6PUz8a8Rl7qDDZzkE=@vger.kernel.org X-Gm-Message-State: APuU4Z9SDMUgHqTvB8OxpB0YEzQN/Nr5mR8Q4YSP0LndxSrm6XKyP3VH I9w00vVvre1jFUNATlx2Zil7bP5/31y5G+O1rlrn02L6BJKsrKfyYBHP5pnQTD5nwME= X-Gm-Gg: AZFdTZe+HRI9x6B2lrACqU5PALB1FTmHuG2dmzP2k4vzT7ahD2atVIYzCElEKqvfZST Toao0q8jP0PfHBV6hiO1F61EBpw3vrUhZ4UuT2rqOMAbJDM/JeKWsspcpTH1oGvYkg22FljuPi/ Xq3+t5dKyXHl/jKOmaW3aT9sOc04XPimmSogJI7l126jEwHMWJ7gaorOWvCkWejAGC1wwOphpK7 awNtMNdaKaLKuDLAjGFuB3d4lC6sqD7Kq51w/hUy7u8n/d+WvRd2iAo0Mn5aT7FE+rs0vIOvXYZ KhFG+GK1qmgoCuuGeimWDEd61f+kX2DTd3K6eN+3yeISolioECMRo7RYqAairPGF/VL9Flvoif0 Na7UojZhJCqO8xyIG/+81zx3k8MNZKR0XmgYptLBS3PaEY5dKiwHMaN20J1gt/jCJ8928sUiozZ ry714cPw148bOCWMQ2RTQLS1VcvMW6/3Ovy0ln8AUM0+f0C0yEzEWPYY0jnDZWyWQeSYvcFCBny Qkg2myBdSZA140IWVwbOohGlYiCDLEOs2RnaA5kKcVv+tiw4Rn9fxa2AO1fY0D2V5GHqfVRzVkm vcOYj5uEEv6bOgQoP/JPUuDofqTh5Cq2RzxvtVbzmO1rInV3wXv/cOlPTT4Jnt/ArhXO6g== X-Received: by 2002:a05:6102:512a:b0:7d6:bae0:e431 with SMTP id ada2fe7eead31-7d73945d06amr2075398137.17.1795196627836; Fri, 20 Nov 2026 09:43:47 -0800 (PST) Received: from localhost.localdomain ([2603:9000:d440:53a:a9cf:4432:7fb7:e8]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-7d6e7d784b3sm3571363137.2.2026.11.20.09.43.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 20 Nov 2026 09:43:47 -0800 (PST) From: Juan Agustin Niccolini To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Simon Horman , Kuniyuki Iwashima , Willem de Bruijn , Willem de Bruijn , Eric Garver , Shuah Khan , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net 4/4] selftests: net: Add UDP bind conflict tests Date: Fri, 20 Nov 2026 12:43:41 -0500 Message-ID: <20261120174341.35873-5-juanagusnicco@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261120174341.35873-1-juanagusnicco@gmail.com> References: <20261120174341.35873-1-juanagusnicco@gmail.com> X-Mailing-List: linux-kernel@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 three bind conflict fixes in this series: - 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 --- 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 21412510ac26..35f6d5220156 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -56,6 +56,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 9180b9bec515..16b804559051 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -202,6 +202,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