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 struct in6_addr in6addr_v4mapped_any = {
10 	.s6_addr = {
11 		0, 0, 0, 0,
12 		0, 0, 0, 0,
13 		0, 0, 255, 255,
14 		0, 0, 0, 0
15 	}
16 };
17 
18 struct in6_addr in6addr_v4mapped_loopback = {
19 	.s6_addr = {
20 		0, 0, 0, 0,
21 		0, 0, 0, 0,
22 		0, 0, 255, 255,
23 		127, 0, 0, 1
24 	}
25 };
26 
27 FIXTURE(bind_wildcard)
28 {
29 	struct sockaddr_in addr4;
30 	struct sockaddr_in6 addr6;
31 };
32 
33 FIXTURE_VARIANT(bind_wildcard)
34 {
35 	const __u32 addr4_const;
36 	const struct in6_addr *addr6_const;
37 	int expected_errno;
38 };
39 
40 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any)
41 {
42 	.addr4_const = INADDR_ANY,
43 	.addr6_const = &in6addr_any,
44 	.expected_errno = EADDRINUSE,
45 };
46 
47 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local)
48 {
49 	.addr4_const = INADDR_ANY,
50 	.addr6_const = &in6addr_loopback,
51 	.expected_errno = 0,
52 };
53 
54 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any)
55 {
56 	.addr4_const = INADDR_ANY,
57 	.addr6_const = &in6addr_v4mapped_any,
58 	.expected_errno = EADDRINUSE,
59 };
60 
61 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local)
62 {
63 	.addr4_const = INADDR_ANY,
64 	.addr6_const = &in6addr_v4mapped_loopback,
65 	.expected_errno = EADDRINUSE,
66 };
67 
68 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any)
69 {
70 	.addr4_const = INADDR_LOOPBACK,
71 	.addr6_const = &in6addr_any,
72 	.expected_errno = EADDRINUSE,
73 };
74 
75 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local)
76 {
77 	.addr4_const = INADDR_LOOPBACK,
78 	.addr6_const = &in6addr_loopback,
79 	.expected_errno = 0,
80 };
81 
82 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any)
83 {
84 	.addr4_const = INADDR_LOOPBACK,
85 	.addr6_const = &in6addr_v4mapped_any,
86 	.expected_errno = EADDRINUSE,
87 };
88 
89 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local)
90 {
91 	.addr4_const = INADDR_LOOPBACK,
92 	.addr6_const = &in6addr_v4mapped_loopback,
93 	.expected_errno = EADDRINUSE,
94 };
95 
96 FIXTURE_SETUP(bind_wildcard)
97 {
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);
101 
102 	self->addr6.sin6_family = AF_INET6;
103 	self->addr6.sin6_port = htons(0);
104 	self->addr6.sin6_addr = *variant->addr6_const;
105 }
106 
107 FIXTURE_TEARDOWN(bind_wildcard)
108 {
109 }
110 
111 void bind_sockets(struct __test_metadata *_metadata,
112 		  FIXTURE_DATA(bind_wildcard) *self,
113 		  int expected_errno,
114 		  struct sockaddr *addr1, socklen_t addrlen1,
115 		  struct sockaddr *addr2, socklen_t addrlen2)
116 {
117 	int fd[2];
118 	int ret;
119 
120 	fd[0] = socket(addr1->sa_family, SOCK_STREAM, 0);
121 	ASSERT_GT(fd[0], 0);
122 
123 	ret = bind(fd[0], addr1, addrlen1);
124 	ASSERT_EQ(ret, 0);
125 
126 	ret = getsockname(fd[0], addr1, &addrlen1);
127 	ASSERT_EQ(ret, 0);
128 
129 	((struct sockaddr_in *)addr2)->sin_port = ((struct sockaddr_in *)addr1)->sin_port;
130 
131 	fd[1] = socket(addr2->sa_family, SOCK_STREAM, 0);
132 	ASSERT_GT(fd[1], 0);
133 
134 	ret = bind(fd[1], addr2, addrlen2);
135 	if (expected_errno) {
136 		ASSERT_EQ(ret, -1);
137 		ASSERT_EQ(errno, expected_errno);
138 	} else {
139 		ASSERT_EQ(ret, 0);
140 	}
141 
142 	close(fd[1]);
143 	close(fd[0]);
144 }
145 
146 TEST_F(bind_wildcard, v4_v6)
147 {
148 	bind_sockets(_metadata, self, variant->expected_errno,
149 		     (struct sockaddr *)&self->addr4, sizeof(self->addr4),
150 		     (struct sockaddr *)&self->addr6, sizeof(self->addr6));
151 }
152 
153 TEST_F(bind_wildcard, v6_v4)
154 {
155 	bind_sockets(_metadata, self, variant->expected_errno,
156 		     (struct sockaddr *)&self->addr6, sizeof(self->addr6),
157 		     (struct sockaddr *)&self->addr4, sizeof(self->addr4));
158 }
159 
160 TEST_HARNESS_MAIN
161