// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause // Copyright (c) 2023 Cloudflare
/* Test IP_LOCAL_PORT_RANGE socket option: IPv4 + IPv6, TCP + UDP. * * Tests assume that net.ipv4.ip_local_port_range is [40000, 49999]. * Don't run these directly but with ip_local_port_range.sh script.
*/
TEST_F(ip_local_port_range, port_range_out_of_netns_range)
{ conststruct test {
__u16 range_lo;
__u16 range_hi;
} tests[] = {
{ 30000, 39999 }, /* socket range below netns range */
{ 50000, 59999 }, /* socket range above netns range */
}; conststruct test *t;
for (t = tests; t < tests + ARRAY_SIZE(tests); t++) { /* Bind a couple of sockets, not just one, to check * that the range wasn't clamped to a single port from * the netns range. That is [40000, 40000] or [49999, * 49999], respectively for each test case.
*/ int fds[2], i;
TH_LOG("lo %5hu, hi %5hu", t->range_lo, t->range_hi);
for (i = 0; i < ARRAY_SIZE(fds); i++) { int fd, err, port;
__u32 range;
/* Check that socket port range outside of ephemeral range is ignored */
port = get_sock_port(fd);
ASSERT_GE(port, 40000) TH_LOG("#%d: expected port within netns range", i);
ASSERT_LE(port, 49999) TH_LOG("#%d: expected port within netns range", i);
fds[i] = fd;
}
for (i = 0; i < ARRAY_SIZE(fds); i++)
ASSERT_TRUE(close(fds[i]) == 0) TH_LOG("#%d: close failed", i);
}
}
TEST_F(ip_local_port_range, single_port_range)
{ conststruct test {
__u16 range_lo;
__u16 range_hi;
__u16 expected;
} tests[] = { /* single port range within ephemeral range */
{ 45000, 45000, 45000 }, /* first port in the ephemeral range (clamp from above) */
{ 0, 40000, 40000 }, /* last port in the ephemeral range (clamp from below) */
{ 49999, 0, 49999 },
}; conststruct test *t;
for (t = tests; t < tests + ARRAY_SIZE(tests); t++) { int fd, err, port;
__u32 range;
TH_LOG("lo %5hu, hi %5hu, expected %5hu",
t->range_lo, t->range_hi, t->expected);
port = get_sock_port(fd);
ASSERT_GE(port, 40000) TH_LOG("expected port within sockopt range");
ASSERT_LE(port, 40007) TH_LOG("expected port within sockopt range");
port_set |= 1 << (port - 40000);
fds[i] = fd;
}
/* Check that all every port from the test range is in use */
ASSERT_EQ(port_set, 0xff) TH_LOG("expected all ports to be busy");
/* Check that bind() fails because the whole range is busy */
fd = socket(variant->so_domain, variant->so_type, variant->so_protocol);
ASSERT_GE(fd, 0) TH_LOG("socket failed");
/* Unset the port range */
range = pack_port_range(0, 0);
err = setsockopt(fd, SOL_IP, IP_LOCAL_PORT_RANGE, &range, sizeof(range));
ASSERT_TRUE(!err) TH_LOG("setsockopt(IP_LOCAL_PORT_RANGE) failed");
/* Get range after it has been unset */
err = get_ip_local_port_range(fd, &range);
ASSERT_TRUE(!err) TH_LOG("getsockopt(IP_LOCAL_PORT_RANGE) failed");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.