Skip to content

Commit cf18d55

Browse files
committed
Merge branch 'tcp-fix-bind-regression-for-dual-stack-wildcard-address'
Kuniyuki Iwashima says: ==================== tcp: Fix bind() regression for dual-stack wildcard address. The first patch fixes the regression reported in [0], and the second patch adds a test for similar cases to catch future regression. [0]: https://lore.kernel.org/netdev/[email protected]/ ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents c22c3bb + 13715ac commit cf18d55

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

net/ipv4/inet_hashtables.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,14 @@ bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, const
828828
#if IS_ENABLED(CONFIG_IPV6)
829829
struct in6_addr addr_any = {};
830830

831-
if (sk->sk_family != tb->family)
831+
if (sk->sk_family != tb->family) {
832+
if (sk->sk_family == AF_INET)
833+
return net_eq(ib2_net(tb), net) && tb->port == port &&
834+
tb->l3mdev == l3mdev &&
835+
ipv6_addr_equal(&tb->v6_rcv_saddr, &addr_any);
836+
832837
return false;
838+
}
833839

834840
if (sk->sk_family == AF_INET6)
835841
return net_eq(ib2_net(tb), net) && tb->port == port &&

tools/testing/selftests/net/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
bind_bhash
33
bind_timewait
4+
bind_wildcard
45
csum
56
cmsg_sender
67
diag_uid

tools/testing/selftests/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ TEST_GEN_FILES += sctp_hello
8080
TEST_GEN_FILES += csum
8181
TEST_GEN_FILES += nat6to4.o
8282
TEST_GEN_FILES += ip_local_port_range
83+
TEST_GEN_FILES += bind_wildcard
8384

8485
TEST_FILES := settings
8586

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright Amazon.com Inc. or its affiliates. */
3+
4+
#include <sys/socket.h>
5+
#include <netinet/in.h>
6+
7+
#include "../kselftest_harness.h"
8+
9+
FIXTURE(bind_wildcard)
10+
{
11+
struct sockaddr_in addr4;
12+
struct sockaddr_in6 addr6;
13+
int expected_errno;
14+
};
15+
16+
FIXTURE_VARIANT(bind_wildcard)
17+
{
18+
const __u32 addr4_const;
19+
const struct in6_addr *addr6_const;
20+
};
21+
22+
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any)
23+
{
24+
.addr4_const = INADDR_ANY,
25+
.addr6_const = &in6addr_any,
26+
};
27+
28+
FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local)
29+
{
30+
.addr4_const = INADDR_ANY,
31+
.addr6_const = &in6addr_loopback,
32+
};
33+
34+
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any)
35+
{
36+
.addr4_const = INADDR_LOOPBACK,
37+
.addr6_const = &in6addr_any,
38+
};
39+
40+
FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local)
41+
{
42+
.addr4_const = INADDR_LOOPBACK,
43+
.addr6_const = &in6addr_loopback,
44+
};
45+
46+
FIXTURE_SETUP(bind_wildcard)
47+
{
48+
self->addr4.sin_family = AF_INET;
49+
self->addr4.sin_port = htons(0);
50+
self->addr4.sin_addr.s_addr = htonl(variant->addr4_const);
51+
52+
self->addr6.sin6_family = AF_INET6;
53+
self->addr6.sin6_port = htons(0);
54+
self->addr6.sin6_addr = *variant->addr6_const;
55+
56+
if (variant->addr6_const == &in6addr_any)
57+
self->expected_errno = EADDRINUSE;
58+
else
59+
self->expected_errno = 0;
60+
}
61+
62+
FIXTURE_TEARDOWN(bind_wildcard)
63+
{
64+
}
65+
66+
void bind_sockets(struct __test_metadata *_metadata,
67+
FIXTURE_DATA(bind_wildcard) *self,
68+
struct sockaddr *addr1, socklen_t addrlen1,
69+
struct sockaddr *addr2, socklen_t addrlen2)
70+
{
71+
int fd[2];
72+
int ret;
73+
74+
fd[0] = socket(addr1->sa_family, SOCK_STREAM, 0);
75+
ASSERT_GT(fd[0], 0);
76+
77+
ret = bind(fd[0], addr1, addrlen1);
78+
ASSERT_EQ(ret, 0);
79+
80+
ret = getsockname(fd[0], addr1, &addrlen1);
81+
ASSERT_EQ(ret, 0);
82+
83+
((struct sockaddr_in *)addr2)->sin_port = ((struct sockaddr_in *)addr1)->sin_port;
84+
85+
fd[1] = socket(addr2->sa_family, SOCK_STREAM, 0);
86+
ASSERT_GT(fd[1], 0);
87+
88+
ret = bind(fd[1], addr2, addrlen2);
89+
if (self->expected_errno) {
90+
ASSERT_EQ(ret, -1);
91+
ASSERT_EQ(errno, self->expected_errno);
92+
} else {
93+
ASSERT_EQ(ret, 0);
94+
}
95+
96+
close(fd[1]);
97+
close(fd[0]);
98+
}
99+
100+
TEST_F(bind_wildcard, v4_v6)
101+
{
102+
bind_sockets(_metadata, self,
103+
(struct sockaddr *)&self->addr4, sizeof(self->addr6),
104+
(struct sockaddr *)&self->addr6, sizeof(self->addr6));
105+
}
106+
107+
TEST_F(bind_wildcard, v6_v4)
108+
{
109+
bind_sockets(_metadata, self,
110+
(struct sockaddr *)&self->addr6, sizeof(self->addr6),
111+
(struct sockaddr *)&self->addr4, sizeof(self->addr4));
112+
}
113+
114+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)