From: Florin Badita-Nistor <baditaflorin@gmail.com>
To: netdev@vger.kernel.org
Cc: edumazet@kernel.org, ncardwell@google.com, kuniyu@google.com,
davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com,
horms@kernel.org, shuah@kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH net-next v2 2/2] selftests/net: cover odd-sized local port ranges with one free port
Date: Fri, 6 Nov 2026 19:14:42 +0200 [thread overview]
Message-ID: <20261106171442.31811-3-baditaflorin@gmail.com> (raw)
In-Reply-To: <20261106171442.31811-1-baditaflorin@gmail.com>
Leave each port in three- and five-port socket-local ranges as the only
available candidate and verify that bind(..., 0) can allocate it. Cover
both lower-bound parities and repeat allocation to vary the random
starting offset. Use the existing IPv4/IPv6 TCP, UDP, SCTP and MPTCP
fixture variants.
This catches the false EADDRINUSE caused by rounding an odd-sized range
down during TCP port allocation. Without the preceding fix, the TCP
and MPTCP variants fail on both IP versions when the last port in a
three-port range is the only free one. With the fix, all eight variants
pass.
Allow the loopback bind helper to take a port so the test can hold
explicit binds on every other candidate. Retain the existing helper for
automatic binding and run under the existing isolated-network-namespace
wrapper.
The regression test was developed with Codex assistance and exercised
on booted x86-64 KVM kernels before and after the fix.
Signed-off-by: Florin Badita-Nistor <baditaflorin@gmail.com>
---
.../selftests/net/ip_local_port_range.c | 67 ++++++++++++++++++-
1 file changed, 64 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/net/ip_local_port_range.c b/tools/testing/selftests/net/ip_local_port_range.c
index 3d32b33cb..c17d95045 100644
--- a/tools/testing/selftests/net/ip_local_port_range.c
+++ b/tools/testing/selftests/net/ip_local_port_range.c
@@ -44,7 +44,7 @@ static int get_so_domain(int fd)
return domain;
}
-static int bind_to_loopback_any_port(int fd)
+static int bind_to_loopback_port(int fd, __u16 port)
{
union {
struct sockaddr sa;
@@ -57,13 +57,13 @@ static int bind_to_loopback_any_port(int fd)
switch (get_so_domain(fd)) {
case AF_INET:
addr.v4.sin_family = AF_INET;
- addr.v4.sin_port = htons(0);
+ addr.v4.sin_port = htons(port);
addr.v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr_len = sizeof(addr.v4);
break;
case AF_INET6:
addr.v6.sin6_family = AF_INET6;
- addr.v6.sin6_port = htons(0);
+ addr.v6.sin6_port = htons(port);
addr.v6.sin6_addr = in6addr_loopback;
addr_len = sizeof(addr.v6);
break;
@@ -74,6 +74,11 @@ static int bind_to_loopback_any_port(int fd)
return bind(fd, &addr.sa, addr_len);
}
+static int bind_to_loopback_any_port(int fd)
+{
+ return bind_to_loopback_port(fd, 0);
+}
+
static int get_sock_port(int fd)
{
union {
@@ -301,6 +306,62 @@ TEST_F(ip_local_port_range, single_port_range)
}
}
+/* Leave each port in turn as the only available one. An odd-sized range
+ * must not report exhaustion while a port at either end is still free.
+ */
+TEST_F(ip_local_port_range, odd_port_range)
+{
+ const struct {
+ __u16 low;
+ __u16 high;
+ } ranges[] = {
+ { 40000, 40002 },
+ { 40001, 40003 },
+ { 40000, 40004 },
+ { 40001, 40005 },
+ };
+ int i, j, port, free_port, fd, err;
+ __u32 range;
+ int fds[5];
+
+ for (i = 0; i < ARRAY_SIZE(ranges); i++) {
+ range = pack_port_range(ranges[i].low, ranges[i].high);
+ for (free_port = ranges[i].low; free_port <= ranges[i].high; free_port++) {
+ for (port = ranges[i].low; port <= ranges[i].high; port++) {
+ j = port - ranges[i].low;
+ fds[j] = -1;
+ if (port == free_port)
+ continue;
+ fd = socket(variant->so_domain, variant->so_type,
+ variant->so_protocol);
+ ASSERT_GE(fd, 0) TH_LOG("socket failed");
+ fds[j] = fd;
+ err = bind_to_loopback_port(fd, port);
+ ASSERT_EQ(err, 0) TH_LOG("bind(%d) failed", port);
+ }
+
+ /* Exercise different random starting offsets. */
+ for (j = 0; j < 16; j++) {
+ fd = socket(variant->so_domain, variant->so_type,
+ variant->so_protocol);
+ ASSERT_GE(fd, 0) TH_LOG("socket failed");
+ err = setsockopt(fd, SOL_IP, IP_LOCAL_PORT_RANGE,
+ &range, sizeof(range));
+ ASSERT_EQ(err, 0) TH_LOG("setsockopt(IP_LOCAL_PORT_RANGE) failed");
+ err = bind_to_loopback_any_port(fd);
+ ASSERT_EQ(err, 0) TH_LOG("range %d-%d, free port %d",
+ ranges[i].low, ranges[i].high, free_port);
+ ASSERT_EQ(get_sock_port(fd), free_port);
+ ASSERT_EQ(close(fd), 0);
+ }
+
+ for (j = 0; j <= ranges[i].high - ranges[i].low; j++)
+ if (fds[j] >= 0)
+ ASSERT_EQ(close(fds[j]), 0);
+ }
+ }
+}
+
#define REUSEADDR 0x1
#define REUSEPORT 0x2
--
2.51.0
prev parent reply other threads:[~2026-11-06 17:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 17:14 [PATCH net-next v2 0/2] tcp: fix false exhaustion of odd-sized bind port ranges Florin Badita-Nistor
2026-11-06 17:14 ` [PATCH net-next v2 1/2] tcp: avoid false bind exhaustion with odd-sized " Florin Badita-Nistor
2026-11-06 17:14 ` Florin Badita-Nistor [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261106171442.31811-3-baditaflorin@gmail.com \
--to=baditaflorin@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®