1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright Amazon.com Inc. or its affiliates. */
4 #include <sys/socket.h>
5 #include <netinet/in.h>
7 #include "../kselftest_harness.h"
9 struct in6_addr in6addr_v4mapped_any = {
18 struct in6_addr in6addr_v4mapped_loopback = {
27 FIXTURE(bind_wildcard)
29 struct sockaddr_in addr4;
30 struct sockaddr_in6 addr6;
33 FIXTURE_VARIANT(bind_wildcard)
35 const __u32 addr4_const;
36 const struct in6_addr *addr6_const;
40 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any)
42 .addr4_const = INADDR_ANY,
43 .addr6_const = &in6addr_any,
44 .expected_errno = EADDRINUSE,
47 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local)
49 .addr4_const = INADDR_ANY,
50 .addr6_const = &in6addr_loopback,
54 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any)
56 .addr4_const = INADDR_ANY,
57 .addr6_const = &in6addr_v4mapped_any,
58 .expected_errno = EADDRINUSE,
61 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local)
63 .addr4_const = INADDR_ANY,
64 .addr6_const = &in6addr_v4mapped_loopback,
65 .expected_errno = EADDRINUSE,
68 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any)
70 .addr4_const = INADDR_LOOPBACK,
71 .addr6_const = &in6addr_any,
72 .expected_errno = EADDRINUSE,
75 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local)
77 .addr4_const = INADDR_LOOPBACK,
78 .addr6_const = &in6addr_loopback,
82 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any)
84 .addr4_const = INADDR_LOOPBACK,
85 .addr6_const = &in6addr_v4mapped_any,
86 .expected_errno = EADDRINUSE,
89 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local)
91 .addr4_const = INADDR_LOOPBACK,
92 .addr6_const = &in6addr_v4mapped_loopback,
93 .expected_errno = EADDRINUSE,
96 FIXTURE_SETUP(bind_wildcard)
98 self->addr4.sin_family = AF_INET;
99 self->addr4.sin_port = htons(0);
100 self->addr4.sin_addr.s_addr = htonl(variant->addr4_const);
102 self->addr6.sin6_family = AF_INET6;
103 self->addr6.sin6_port = htons(0);
104 self->addr6.sin6_addr = *variant->addr6_const;
107 FIXTURE_TEARDOWN(bind_wildcard)
111 void bind_sockets(struct __test_metadata *_metadata,
112 FIXTURE_DATA(bind_wildcard) *self,
114 struct sockaddr *addr1, socklen_t addrlen1,
115 struct sockaddr *addr2, socklen_t addrlen2)
120 fd[0] = socket(addr1->sa_family, SOCK_STREAM, 0);
123 ret = bind(fd[0], addr1, addrlen1);
126 ret = getsockname(fd[0], addr1, &addrlen1);
129 ((struct sockaddr_in *)addr2)->sin_port = ((struct sockaddr_in *)addr1)->sin_port;
131 fd[1] = socket(addr2->sa_family, SOCK_STREAM, 0);
134 ret = bind(fd[1], addr2, addrlen2);
135 if (expected_errno) {
137 ASSERT_EQ(errno, expected_errno);
146 TEST_F(bind_wildcard, v4_v6)
148 bind_sockets(_metadata, self, variant->expected_errno,
149 (struct sockaddr *)&self->addr4, sizeof(self->addr4),
150 (struct sockaddr *)&self->addr6, sizeof(self->addr6));
153 TEST_F(bind_wildcard, v6_v4)
155 bind_sockets(_metadata, self, variant->expected_errno,
156 (struct sockaddr *)&self->addr6, sizeof(self->addr6),
157 (struct sockaddr *)&self->addr4, sizeof(self->addr4));