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 static const __u32 in4addr_any = INADDR_ANY; 10 static const __u32 in4addr_loopback = INADDR_LOOPBACK; 11 static const struct in6_addr in6addr_v4mapped_any = { 12 .s6_addr = { 13 0, 0, 0, 0, 14 0, 0, 0, 0, 15 0, 0, 255, 255, 16 0, 0, 0, 0 17 } 18 }; 19 static const struct in6_addr in6addr_v4mapped_loopback = { 20 .s6_addr = { 21 0, 0, 0, 0, 22 0, 0, 0, 0, 23 0, 0, 255, 255, 24 127, 0, 0, 1 25 } 26 }; 27 28 #define NR_SOCKETS 8 29 30 FIXTURE(bind_wildcard) 31 { 32 int fd[NR_SOCKETS]; 33 socklen_t addrlen[NR_SOCKETS]; 34 union { 35 struct sockaddr addr; 36 struct sockaddr_in addr4; 37 struct sockaddr_in6 addr6; 38 } addr[NR_SOCKETS]; 39 }; 40 41 FIXTURE_VARIANT(bind_wildcard) 42 { 43 sa_family_t family[2]; 44 const void *addr[2]; 45 46 /* 6 bind() calls below follow two bind() for the defined 2 addresses: 47 * 48 * 0.0.0.0 49 * 127.0.0.1 50 * :: 51 * ::1 52 * ::ffff:0.0.0.0 53 * ::ffff:127.0.0.1 54 */ 55 int expected_errno[NR_SOCKETS]; 56 }; 57 58 /* (IPv4, IPv4) */ 59 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v4_local) 60 { 61 .family = {AF_INET, AF_INET}, 62 .addr = {&in4addr_any, &in4addr_loopback}, 63 .expected_errno = {0, EADDRINUSE, 64 EADDRINUSE, EADDRINUSE, 65 EADDRINUSE, 0, 66 EADDRINUSE, EADDRINUSE}, 67 }; 68 69 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v4_any) 70 { 71 .family = {AF_INET, AF_INET}, 72 .addr = {&in4addr_loopback, &in4addr_any}, 73 .expected_errno = {0, EADDRINUSE, 74 EADDRINUSE, EADDRINUSE, 75 EADDRINUSE, 0, 76 EADDRINUSE, EADDRINUSE}, 77 }; 78 79 /* (IPv4, IPv6) */ 80 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any) 81 { 82 .family = {AF_INET, AF_INET6}, 83 .addr = {&in4addr_any, &in6addr_any}, 84 .expected_errno = {0, EADDRINUSE, 85 EADDRINUSE, EADDRINUSE, 86 EADDRINUSE, 0, 87 EADDRINUSE, EADDRINUSE}, 88 }; 89 90 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local) 91 { 92 .family = {AF_INET, AF_INET6}, 93 .addr = {&in4addr_any, &in6addr_loopback}, 94 .expected_errno = {0, 0, 95 EADDRINUSE, EADDRINUSE, 96 EADDRINUSE, EADDRINUSE, 97 EADDRINUSE, EADDRINUSE}, 98 }; 99 100 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any) 101 { 102 .family = {AF_INET, AF_INET6}, 103 .addr = {&in4addr_any, &in6addr_v4mapped_any}, 104 .expected_errno = {0, EADDRINUSE, 105 EADDRINUSE, EADDRINUSE, 106 EADDRINUSE, 0, 107 EADDRINUSE, EADDRINUSE}, 108 }; 109 110 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local) 111 { 112 .family = {AF_INET, AF_INET6}, 113 .addr = {&in4addr_any, &in6addr_v4mapped_loopback}, 114 .expected_errno = {0, EADDRINUSE, 115 EADDRINUSE, EADDRINUSE, 116 EADDRINUSE, 0, 117 EADDRINUSE, EADDRINUSE}, 118 }; 119 120 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any) 121 { 122 .family = {AF_INET, AF_INET6}, 123 .addr = {&in4addr_loopback, &in6addr_any}, 124 .expected_errno = {0, EADDRINUSE, 125 EADDRINUSE, EADDRINUSE, 126 EADDRINUSE, 0, 127 EADDRINUSE, EADDRINUSE}, 128 }; 129 130 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local) 131 { 132 .family = {AF_INET, AF_INET6}, 133 .addr = {&in4addr_loopback, &in6addr_loopback}, 134 .expected_errno = {0, 0, 135 EADDRINUSE, EADDRINUSE, 136 EADDRINUSE, EADDRINUSE, 137 EADDRINUSE, EADDRINUSE}, 138 }; 139 140 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any) 141 { 142 .family = {AF_INET, AF_INET6}, 143 .addr = {&in4addr_loopback, &in6addr_v4mapped_any}, 144 .expected_errno = {0, EADDRINUSE, 145 EADDRINUSE, EADDRINUSE, 146 EADDRINUSE, 0, 147 EADDRINUSE, EADDRINUSE}, 148 }; 149 150 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local) 151 { 152 .family = {AF_INET, AF_INET6}, 153 .addr = {&in4addr_loopback, &in6addr_v4mapped_loopback}, 154 .expected_errno = {0, EADDRINUSE, 155 EADDRINUSE, EADDRINUSE, 156 EADDRINUSE, 0, 157 EADDRINUSE, EADDRINUSE}, 158 }; 159 160 /* (IPv6, IPv4) */ 161 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_any) 162 { 163 .family = {AF_INET6, AF_INET}, 164 .addr = {&in6addr_any, &in4addr_any}, 165 .expected_errno = {0, EADDRINUSE, 166 EADDRINUSE, EADDRINUSE, 167 EADDRINUSE, EADDRINUSE, 168 EADDRINUSE, EADDRINUSE}, 169 }; 170 171 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_local) 172 { 173 .family = {AF_INET6, AF_INET}, 174 .addr = {&in6addr_any, &in4addr_loopback}, 175 .expected_errno = {0, EADDRINUSE, 176 EADDRINUSE, EADDRINUSE, 177 EADDRINUSE, EADDRINUSE, 178 EADDRINUSE, EADDRINUSE}, 179 }; 180 181 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_any) 182 { 183 .family = {AF_INET6, AF_INET}, 184 .addr = {&in6addr_loopback, &in4addr_any}, 185 .expected_errno = {0, 0, 186 EADDRINUSE, EADDRINUSE, 187 EADDRINUSE, EADDRINUSE, 188 EADDRINUSE, EADDRINUSE}, 189 }; 190 191 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_local) 192 { 193 .family = {AF_INET6, AF_INET}, 194 .addr = {&in6addr_loopback, &in4addr_loopback}, 195 .expected_errno = {0, 0, 196 EADDRINUSE, EADDRINUSE, 197 EADDRINUSE, EADDRINUSE, 198 EADDRINUSE, EADDRINUSE}, 199 }; 200 201 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_any) 202 { 203 .family = {AF_INET6, AF_INET}, 204 .addr = {&in6addr_v4mapped_any, &in4addr_any}, 205 .expected_errno = {0, EADDRINUSE, 206 EADDRINUSE, EADDRINUSE, 207 EADDRINUSE, 0, 208 EADDRINUSE, EADDRINUSE}, 209 }; 210 211 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_local) 212 { 213 .family = {AF_INET6, AF_INET}, 214 .addr = {&in6addr_v4mapped_any, &in4addr_loopback}, 215 .expected_errno = {0, EADDRINUSE, 216 EADDRINUSE, EADDRINUSE, 217 EADDRINUSE, 0, 218 EADDRINUSE, EADDRINUSE}, 219 }; 220 221 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_any) 222 { 223 .family = {AF_INET6, AF_INET}, 224 .addr = {&in6addr_v4mapped_loopback, &in4addr_any}, 225 .expected_errno = {0, EADDRINUSE, 226 EADDRINUSE, EADDRINUSE, 227 EADDRINUSE, 0, 228 EADDRINUSE, EADDRINUSE}, 229 }; 230 231 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_local) 232 { 233 .family = {AF_INET6, AF_INET}, 234 .addr = {&in6addr_v4mapped_loopback, &in4addr_loopback}, 235 .expected_errno = {0, EADDRINUSE, 236 EADDRINUSE, EADDRINUSE, 237 EADDRINUSE, 0, 238 EADDRINUSE, EADDRINUSE}, 239 }; 240 241 /* (IPv6, IPv6) */ 242 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_local) 243 { 244 .family = {AF_INET6, AF_INET6}, 245 .addr = {&in6addr_any, &in6addr_loopback}, 246 .expected_errno = {0, EADDRINUSE, 247 EADDRINUSE, EADDRINUSE, 248 EADDRINUSE, EADDRINUSE, 249 EADDRINUSE, EADDRINUSE}, 250 }; 251 252 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_any) 253 { 254 .family = {AF_INET6, AF_INET6}, 255 .addr = {&in6addr_any, &in6addr_v4mapped_any}, 256 .expected_errno = {0, EADDRINUSE, 257 EADDRINUSE, EADDRINUSE, 258 EADDRINUSE, EADDRINUSE, 259 EADDRINUSE, EADDRINUSE}, 260 }; 261 262 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_local) 263 { 264 .family = {AF_INET6, AF_INET6}, 265 .addr = {&in6addr_any, &in6addr_v4mapped_loopback}, 266 .expected_errno = {0, EADDRINUSE, 267 EADDRINUSE, EADDRINUSE, 268 EADDRINUSE, EADDRINUSE, 269 EADDRINUSE, EADDRINUSE}, 270 }; 271 272 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any) 273 { 274 .family = {AF_INET6, AF_INET6}, 275 .addr = {&in6addr_loopback, &in6addr_any}, 276 .expected_errno = {0, EADDRINUSE, 277 0, EADDRINUSE, 278 EADDRINUSE, EADDRINUSE, 279 EADDRINUSE, EADDRINUSE}, 280 }; 281 282 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_any) 283 { 284 .family = {AF_INET6, AF_INET6}, 285 .addr = {&in6addr_loopback, &in6addr_v4mapped_any}, 286 .expected_errno = {0, 0, 287 EADDRINUSE, EADDRINUSE, 288 EADDRINUSE, EADDRINUSE, 289 EADDRINUSE, EADDRINUSE}, 290 }; 291 292 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_local) 293 { 294 .family = {AF_INET6, AF_INET6}, 295 .addr = {&in6addr_loopback, &in6addr_v4mapped_loopback}, 296 .expected_errno = {0, 0, 297 EADDRINUSE, EADDRINUSE, 298 EADDRINUSE, EADDRINUSE, 299 EADDRINUSE, EADDRINUSE}, 300 }; 301 302 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any) 303 { 304 .family = {AF_INET6, AF_INET6}, 305 .addr = {&in6addr_v4mapped_any, &in6addr_any}, 306 .expected_errno = {0, EADDRINUSE, 307 EADDRINUSE, EADDRINUSE, 308 EADDRINUSE, 0, 309 EADDRINUSE, EADDRINUSE}, 310 }; 311 312 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_local) 313 { 314 .family = {AF_INET6, AF_INET6}, 315 .addr = {&in6addr_v4mapped_any, &in6addr_loopback}, 316 .expected_errno = {0, 0, 317 EADDRINUSE, EADDRINUSE, 318 EADDRINUSE, EADDRINUSE, 319 EADDRINUSE, EADDRINUSE}, 320 }; 321 322 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_v4mapped_local) 323 { 324 .family = {AF_INET6, AF_INET6}, 325 .addr = {&in6addr_v4mapped_any, &in6addr_v4mapped_loopback}, 326 .expected_errno = {0, EADDRINUSE, 327 EADDRINUSE, EADDRINUSE, 328 EADDRINUSE, 0, 329 EADDRINUSE, EADDRINUSE}, 330 }; 331 332 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any) 333 { 334 .family = {AF_INET6, AF_INET6}, 335 .addr = {&in6addr_v4mapped_loopback, &in6addr_any}, 336 .expected_errno = {0, EADDRINUSE, 337 EADDRINUSE, EADDRINUSE, 338 EADDRINUSE, 0, 339 EADDRINUSE, EADDRINUSE}, 340 }; 341 342 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_local) 343 { 344 .family = {AF_INET6, AF_INET6}, 345 .addr = {&in6addr_v4mapped_loopback, &in6addr_loopback}, 346 .expected_errno = {0, 0, 347 EADDRINUSE, EADDRINUSE, 348 EADDRINUSE, EADDRINUSE, 349 EADDRINUSE, EADDRINUSE}, 350 }; 351 352 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_v4mapped_any) 353 { 354 .family = {AF_INET6, AF_INET6}, 355 .addr = {&in6addr_v4mapped_loopback, &in6addr_v4mapped_any}, 356 .expected_errno = {0, EADDRINUSE, 357 EADDRINUSE, EADDRINUSE, 358 EADDRINUSE, 0, 359 EADDRINUSE, EADDRINUSE}, 360 }; 361 362 static void setup_addr(FIXTURE_DATA(bind_wildcard) *self, int i, 363 int family, const void *addr_const) 364 { 365 if (family == AF_INET) { 366 struct sockaddr_in *addr4 = &self->addr[i].addr4; 367 const __u32 *addr4_const = addr_const; 368 369 addr4->sin_family = AF_INET; 370 addr4->sin_port = htons(0); 371 addr4->sin_addr.s_addr = htonl(*addr4_const); 372 373 self->addrlen[i] = sizeof(struct sockaddr_in); 374 } else { 375 struct sockaddr_in6 *addr6 = &self->addr[i].addr6; 376 const struct in6_addr *addr6_const = addr_const; 377 378 addr6->sin6_family = AF_INET6; 379 addr6->sin6_port = htons(0); 380 addr6->sin6_addr = *addr6_const; 381 382 self->addrlen[i] = sizeof(struct sockaddr_in6); 383 } 384 } 385 386 FIXTURE_SETUP(bind_wildcard) 387 { 388 setup_addr(self, 0, variant->family[0], variant->addr[0]); 389 setup_addr(self, 1, variant->family[1], variant->addr[1]); 390 391 setup_addr(self, 2, AF_INET, &in4addr_any); 392 setup_addr(self, 3, AF_INET, &in4addr_loopback); 393 394 setup_addr(self, 4, AF_INET6, &in6addr_any); 395 setup_addr(self, 5, AF_INET6, &in6addr_loopback); 396 setup_addr(self, 6, AF_INET6, &in6addr_v4mapped_any); 397 setup_addr(self, 7, AF_INET6, &in6addr_v4mapped_loopback); 398 } 399 400 FIXTURE_TEARDOWN(bind_wildcard) 401 { 402 int i; 403 404 for (i = 0; i < NR_SOCKETS; i++) 405 close(self->fd[i]); 406 } 407 408 void bind_socket(struct __test_metadata *_metadata, 409 FIXTURE_DATA(bind_wildcard) *self, 410 const FIXTURE_VARIANT(bind_wildcard) *variant, 411 int i) 412 { 413 int ret; 414 415 self->fd[i] = socket(self->addr[i].addr.sa_family, SOCK_STREAM, 0); 416 ASSERT_GT(self->fd[i], 0); 417 418 self->addr[i].addr4.sin_port = self->addr[0].addr4.sin_port; 419 420 ret = bind(self->fd[i], &self->addr[i].addr, self->addrlen[i]); 421 if (variant->expected_errno[i]) { 422 ASSERT_EQ(ret, -1); 423 ASSERT_EQ(errno, variant->expected_errno[i]); 424 } else { 425 ASSERT_EQ(ret, 0); 426 } 427 428 if (i == 0) { 429 ret = getsockname(self->fd[0], &self->addr[0].addr, &self->addrlen[0]); 430 ASSERT_EQ(ret, 0); 431 } 432 } 433 434 TEST_F(bind_wildcard, plain) 435 { 436 int i; 437 438 for (i = 0; i < NR_SOCKETS; i++) 439 bind_socket(_metadata, self, variant, i); 440 } 441 442 TEST_HARNESS_MAIN 443