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 bool ipv6_only[2]; 46 47 /* 6 bind() calls below follow two bind() for the defined 2 addresses: 48 * 49 * 0.0.0.0 50 * 127.0.0.1 51 * :: 52 * ::1 53 * ::ffff:0.0.0.0 54 * ::ffff:127.0.0.1 55 */ 56 int expected_errno[NR_SOCKETS]; 57 }; 58 59 /* (IPv4, IPv4) */ 60 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v4_local) 61 { 62 .family = {AF_INET, AF_INET}, 63 .addr = {&in4addr_any, &in4addr_loopback}, 64 .expected_errno = {0, EADDRINUSE, 65 EADDRINUSE, EADDRINUSE, 66 EADDRINUSE, 0, 67 EADDRINUSE, EADDRINUSE}, 68 }; 69 70 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v4_any) 71 { 72 .family = {AF_INET, AF_INET}, 73 .addr = {&in4addr_loopback, &in4addr_any}, 74 .expected_errno = {0, EADDRINUSE, 75 EADDRINUSE, EADDRINUSE, 76 EADDRINUSE, 0, 77 EADDRINUSE, EADDRINUSE}, 78 }; 79 80 /* (IPv4, IPv6) */ 81 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any) 82 { 83 .family = {AF_INET, AF_INET6}, 84 .addr = {&in4addr_any, &in6addr_any}, 85 .expected_errno = {0, EADDRINUSE, 86 EADDRINUSE, EADDRINUSE, 87 EADDRINUSE, 0, 88 EADDRINUSE, EADDRINUSE}, 89 }; 90 91 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any_only) 92 { 93 .family = {AF_INET, AF_INET6}, 94 .addr = {&in4addr_any, &in6addr_any}, 95 .ipv6_only = {false, true}, 96 .expected_errno = {0, 0, 97 EADDRINUSE, EADDRINUSE, 98 EADDRINUSE, EADDRINUSE, 99 EADDRINUSE, EADDRINUSE}, 100 }; 101 102 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local) 103 { 104 .family = {AF_INET, AF_INET6}, 105 .addr = {&in4addr_any, &in6addr_loopback}, 106 .expected_errno = {0, 0, 107 EADDRINUSE, EADDRINUSE, 108 EADDRINUSE, EADDRINUSE, 109 EADDRINUSE, EADDRINUSE}, 110 }; 111 112 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any) 113 { 114 .family = {AF_INET, AF_INET6}, 115 .addr = {&in4addr_any, &in6addr_v4mapped_any}, 116 .expected_errno = {0, EADDRINUSE, 117 EADDRINUSE, EADDRINUSE, 118 EADDRINUSE, 0, 119 EADDRINUSE, EADDRINUSE}, 120 }; 121 122 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local) 123 { 124 .family = {AF_INET, AF_INET6}, 125 .addr = {&in4addr_any, &in6addr_v4mapped_loopback}, 126 .expected_errno = {0, EADDRINUSE, 127 EADDRINUSE, EADDRINUSE, 128 EADDRINUSE, 0, 129 EADDRINUSE, EADDRINUSE}, 130 }; 131 132 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any) 133 { 134 .family = {AF_INET, AF_INET6}, 135 .addr = {&in4addr_loopback, &in6addr_any}, 136 .expected_errno = {0, EADDRINUSE, 137 EADDRINUSE, EADDRINUSE, 138 EADDRINUSE, 0, 139 EADDRINUSE, EADDRINUSE}, 140 }; 141 142 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any_only) 143 { 144 .family = {AF_INET, AF_INET6}, 145 .addr = {&in4addr_loopback, &in6addr_any}, 146 .ipv6_only = {false, true}, 147 .expected_errno = {0, 0, 148 EADDRINUSE, EADDRINUSE, 149 EADDRINUSE, EADDRINUSE, 150 EADDRINUSE, EADDRINUSE}, 151 }; 152 153 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local) 154 { 155 .family = {AF_INET, AF_INET6}, 156 .addr = {&in4addr_loopback, &in6addr_loopback}, 157 .expected_errno = {0, 0, 158 EADDRINUSE, EADDRINUSE, 159 EADDRINUSE, EADDRINUSE, 160 EADDRINUSE, EADDRINUSE}, 161 }; 162 163 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any) 164 { 165 .family = {AF_INET, AF_INET6}, 166 .addr = {&in4addr_loopback, &in6addr_v4mapped_any}, 167 .expected_errno = {0, EADDRINUSE, 168 EADDRINUSE, EADDRINUSE, 169 EADDRINUSE, 0, 170 EADDRINUSE, EADDRINUSE}, 171 }; 172 173 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local) 174 { 175 .family = {AF_INET, AF_INET6}, 176 .addr = {&in4addr_loopback, &in6addr_v4mapped_loopback}, 177 .expected_errno = {0, EADDRINUSE, 178 EADDRINUSE, EADDRINUSE, 179 EADDRINUSE, 0, 180 EADDRINUSE, EADDRINUSE}, 181 }; 182 183 /* (IPv6, IPv4) */ 184 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_any) 185 { 186 .family = {AF_INET6, AF_INET}, 187 .addr = {&in6addr_any, &in4addr_any}, 188 .expected_errno = {0, EADDRINUSE, 189 EADDRINUSE, EADDRINUSE, 190 EADDRINUSE, EADDRINUSE, 191 EADDRINUSE, EADDRINUSE}, 192 }; 193 194 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v4_any) 195 { 196 .family = {AF_INET6, AF_INET}, 197 .addr = {&in6addr_any, &in4addr_any}, 198 .ipv6_only = {true, false}, 199 .expected_errno = {0, 0, 200 EADDRINUSE, EADDRINUSE, 201 EADDRINUSE, EADDRINUSE, 202 EADDRINUSE, EADDRINUSE}, 203 }; 204 205 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_local) 206 { 207 .family = {AF_INET6, AF_INET}, 208 .addr = {&in6addr_any, &in4addr_loopback}, 209 .expected_errno = {0, EADDRINUSE, 210 EADDRINUSE, EADDRINUSE, 211 EADDRINUSE, EADDRINUSE, 212 EADDRINUSE, EADDRINUSE}, 213 }; 214 215 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v4_local) 216 { 217 .family = {AF_INET6, AF_INET}, 218 .addr = {&in6addr_any, &in4addr_loopback}, 219 .ipv6_only = {true, false}, 220 .expected_errno = {0, 0, 221 EADDRINUSE, EADDRINUSE, 222 EADDRINUSE, EADDRINUSE, 223 EADDRINUSE, EADDRINUSE}, 224 }; 225 226 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_any) 227 { 228 .family = {AF_INET6, AF_INET}, 229 .addr = {&in6addr_loopback, &in4addr_any}, 230 .expected_errno = {0, 0, 231 EADDRINUSE, EADDRINUSE, 232 EADDRINUSE, EADDRINUSE, 233 EADDRINUSE, EADDRINUSE}, 234 }; 235 236 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_local) 237 { 238 .family = {AF_INET6, AF_INET}, 239 .addr = {&in6addr_loopback, &in4addr_loopback}, 240 .expected_errno = {0, 0, 241 EADDRINUSE, EADDRINUSE, 242 EADDRINUSE, EADDRINUSE, 243 EADDRINUSE, EADDRINUSE}, 244 }; 245 246 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_any) 247 { 248 .family = {AF_INET6, AF_INET}, 249 .addr = {&in6addr_v4mapped_any, &in4addr_any}, 250 .expected_errno = {0, EADDRINUSE, 251 EADDRINUSE, EADDRINUSE, 252 EADDRINUSE, 0, 253 EADDRINUSE, EADDRINUSE}, 254 }; 255 256 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_local) 257 { 258 .family = {AF_INET6, AF_INET}, 259 .addr = {&in6addr_v4mapped_any, &in4addr_loopback}, 260 .expected_errno = {0, EADDRINUSE, 261 EADDRINUSE, EADDRINUSE, 262 EADDRINUSE, 0, 263 EADDRINUSE, EADDRINUSE}, 264 }; 265 266 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_any) 267 { 268 .family = {AF_INET6, AF_INET}, 269 .addr = {&in6addr_v4mapped_loopback, &in4addr_any}, 270 .expected_errno = {0, EADDRINUSE, 271 EADDRINUSE, EADDRINUSE, 272 EADDRINUSE, 0, 273 EADDRINUSE, EADDRINUSE}, 274 }; 275 276 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_local) 277 { 278 .family = {AF_INET6, AF_INET}, 279 .addr = {&in6addr_v4mapped_loopback, &in4addr_loopback}, 280 .expected_errno = {0, EADDRINUSE, 281 EADDRINUSE, EADDRINUSE, 282 EADDRINUSE, 0, 283 EADDRINUSE, EADDRINUSE}, 284 }; 285 286 /* (IPv6, IPv6) */ 287 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_local) 288 { 289 .family = {AF_INET6, AF_INET6}, 290 .addr = {&in6addr_any, &in6addr_loopback}, 291 .expected_errno = {0, EADDRINUSE, 292 EADDRINUSE, EADDRINUSE, 293 EADDRINUSE, EADDRINUSE, 294 EADDRINUSE, EADDRINUSE}, 295 }; 296 297 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_local) 298 { 299 .family = {AF_INET6, AF_INET6}, 300 .addr = {&in6addr_any, &in6addr_loopback}, 301 .ipv6_only = {true, false}, 302 .expected_errno = {0, EADDRINUSE, 303 0, EADDRINUSE, 304 EADDRINUSE, EADDRINUSE, 305 EADDRINUSE, EADDRINUSE}, 306 }; 307 308 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_any) 309 { 310 .family = {AF_INET6, AF_INET6}, 311 .addr = {&in6addr_any, &in6addr_v4mapped_any}, 312 .expected_errno = {0, EADDRINUSE, 313 EADDRINUSE, EADDRINUSE, 314 EADDRINUSE, EADDRINUSE, 315 EADDRINUSE, EADDRINUSE}, 316 }; 317 318 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_v4mapped_any) 319 { 320 .family = {AF_INET6, AF_INET6}, 321 .addr = {&in6addr_any, &in6addr_v4mapped_any}, 322 .ipv6_only = {true, false}, 323 .expected_errno = {0, 0, 324 EADDRINUSE, EADDRINUSE, 325 EADDRINUSE, EADDRINUSE, 326 EADDRINUSE, EADDRINUSE}, 327 }; 328 329 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_local) 330 { 331 .family = {AF_INET6, AF_INET6}, 332 .addr = {&in6addr_any, &in6addr_v4mapped_loopback}, 333 .expected_errno = {0, EADDRINUSE, 334 EADDRINUSE, EADDRINUSE, 335 EADDRINUSE, EADDRINUSE, 336 EADDRINUSE, EADDRINUSE}, 337 }; 338 339 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_v4mapped_local) 340 { 341 .family = {AF_INET6, AF_INET6}, 342 .addr = {&in6addr_any, &in6addr_v4mapped_loopback}, 343 .ipv6_only = {true, false}, 344 .expected_errno = {0, 0, 345 EADDRINUSE, EADDRINUSE, 346 EADDRINUSE, EADDRINUSE, 347 EADDRINUSE, EADDRINUSE}, 348 }; 349 350 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any) 351 { 352 .family = {AF_INET6, AF_INET6}, 353 .addr = {&in6addr_loopback, &in6addr_any}, 354 .expected_errno = {0, EADDRINUSE, 355 0, EADDRINUSE, 356 EADDRINUSE, EADDRINUSE, 357 EADDRINUSE, EADDRINUSE}, 358 }; 359 360 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any_only) 361 { 362 .family = {AF_INET6, AF_INET6}, 363 .addr = {&in6addr_loopback, &in6addr_any}, 364 .ipv6_only = {false, true}, 365 .expected_errno = {0, EADDRINUSE, 366 0, EADDRINUSE, 367 EADDRINUSE, EADDRINUSE, 368 EADDRINUSE, EADDRINUSE}, 369 }; 370 371 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_any) 372 { 373 .family = {AF_INET6, AF_INET6}, 374 .addr = {&in6addr_loopback, &in6addr_v4mapped_any}, 375 .expected_errno = {0, 0, 376 EADDRINUSE, EADDRINUSE, 377 EADDRINUSE, EADDRINUSE, 378 EADDRINUSE, EADDRINUSE}, 379 }; 380 381 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_local) 382 { 383 .family = {AF_INET6, AF_INET6}, 384 .addr = {&in6addr_loopback, &in6addr_v4mapped_loopback}, 385 .expected_errno = {0, 0, 386 EADDRINUSE, EADDRINUSE, 387 EADDRINUSE, EADDRINUSE, 388 EADDRINUSE, EADDRINUSE}, 389 }; 390 391 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any) 392 { 393 .family = {AF_INET6, AF_INET6}, 394 .addr = {&in6addr_v4mapped_any, &in6addr_any}, 395 .expected_errno = {0, EADDRINUSE, 396 EADDRINUSE, EADDRINUSE, 397 EADDRINUSE, 0, 398 EADDRINUSE, EADDRINUSE}, 399 }; 400 401 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any_only) 402 { 403 .family = {AF_INET6, AF_INET6}, 404 .addr = {&in6addr_v4mapped_any, &in6addr_any}, 405 .ipv6_only = {false, true}, 406 .expected_errno = {0, 0, 407 EADDRINUSE, EADDRINUSE, 408 EADDRINUSE, EADDRINUSE, 409 EADDRINUSE, EADDRINUSE}, 410 }; 411 412 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_local) 413 { 414 .family = {AF_INET6, AF_INET6}, 415 .addr = {&in6addr_v4mapped_any, &in6addr_loopback}, 416 .expected_errno = {0, 0, 417 EADDRINUSE, EADDRINUSE, 418 EADDRINUSE, EADDRINUSE, 419 EADDRINUSE, EADDRINUSE}, 420 }; 421 422 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_v4mapped_local) 423 { 424 .family = {AF_INET6, AF_INET6}, 425 .addr = {&in6addr_v4mapped_any, &in6addr_v4mapped_loopback}, 426 .expected_errno = {0, EADDRINUSE, 427 EADDRINUSE, EADDRINUSE, 428 EADDRINUSE, 0, 429 EADDRINUSE, EADDRINUSE}, 430 }; 431 432 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any) 433 { 434 .family = {AF_INET6, AF_INET6}, 435 .addr = {&in6addr_v4mapped_loopback, &in6addr_any}, 436 .expected_errno = {0, EADDRINUSE, 437 EADDRINUSE, EADDRINUSE, 438 EADDRINUSE, 0, 439 EADDRINUSE, EADDRINUSE}, 440 }; 441 442 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any_only) 443 { 444 .family = {AF_INET6, AF_INET6}, 445 .addr = {&in6addr_v4mapped_loopback, &in6addr_any}, 446 .ipv6_only = {false, true}, 447 .expected_errno = {0, 0, 448 EADDRINUSE, EADDRINUSE, 449 EADDRINUSE, EADDRINUSE, 450 EADDRINUSE, EADDRINUSE}, 451 }; 452 453 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_local) 454 { 455 .family = {AF_INET6, AF_INET6}, 456 .addr = {&in6addr_v4mapped_loopback, &in6addr_loopback}, 457 .expected_errno = {0, 0, 458 EADDRINUSE, EADDRINUSE, 459 EADDRINUSE, EADDRINUSE, 460 EADDRINUSE, EADDRINUSE}, 461 }; 462 463 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_v4mapped_any) 464 { 465 .family = {AF_INET6, AF_INET6}, 466 .addr = {&in6addr_v4mapped_loopback, &in6addr_v4mapped_any}, 467 .expected_errno = {0, EADDRINUSE, 468 EADDRINUSE, EADDRINUSE, 469 EADDRINUSE, 0, 470 EADDRINUSE, EADDRINUSE}, 471 }; 472 473 static void setup_addr(FIXTURE_DATA(bind_wildcard) *self, int i, 474 int family, const void *addr_const) 475 { 476 if (family == AF_INET) { 477 struct sockaddr_in *addr4 = &self->addr[i].addr4; 478 const __u32 *addr4_const = addr_const; 479 480 addr4->sin_family = AF_INET; 481 addr4->sin_port = htons(0); 482 addr4->sin_addr.s_addr = htonl(*addr4_const); 483 484 self->addrlen[i] = sizeof(struct sockaddr_in); 485 } else { 486 struct sockaddr_in6 *addr6 = &self->addr[i].addr6; 487 const struct in6_addr *addr6_const = addr_const; 488 489 addr6->sin6_family = AF_INET6; 490 addr6->sin6_port = htons(0); 491 addr6->sin6_addr = *addr6_const; 492 493 self->addrlen[i] = sizeof(struct sockaddr_in6); 494 } 495 } 496 497 FIXTURE_SETUP(bind_wildcard) 498 { 499 setup_addr(self, 0, variant->family[0], variant->addr[0]); 500 setup_addr(self, 1, variant->family[1], variant->addr[1]); 501 502 setup_addr(self, 2, AF_INET, &in4addr_any); 503 setup_addr(self, 3, AF_INET, &in4addr_loopback); 504 505 setup_addr(self, 4, AF_INET6, &in6addr_any); 506 setup_addr(self, 5, AF_INET6, &in6addr_loopback); 507 setup_addr(self, 6, AF_INET6, &in6addr_v4mapped_any); 508 setup_addr(self, 7, AF_INET6, &in6addr_v4mapped_loopback); 509 } 510 511 FIXTURE_TEARDOWN(bind_wildcard) 512 { 513 int i; 514 515 for (i = 0; i < NR_SOCKETS; i++) 516 close(self->fd[i]); 517 } 518 519 void bind_socket(struct __test_metadata *_metadata, 520 FIXTURE_DATA(bind_wildcard) *self, 521 const FIXTURE_VARIANT(bind_wildcard) *variant, 522 int i) 523 { 524 int ret; 525 526 self->fd[i] = socket(self->addr[i].addr.sa_family, SOCK_STREAM, 0); 527 ASSERT_GT(self->fd[i], 0); 528 529 if (i < 2 && variant->ipv6_only[i]) { 530 ret = setsockopt(self->fd[i], SOL_IPV6, IPV6_V6ONLY, &(int){1}, sizeof(int)); 531 ASSERT_EQ(ret, 0); 532 } 533 534 self->addr[i].addr4.sin_port = self->addr[0].addr4.sin_port; 535 536 ret = bind(self->fd[i], &self->addr[i].addr, self->addrlen[i]); 537 if (variant->expected_errno[i]) { 538 ASSERT_EQ(ret, -1); 539 ASSERT_EQ(errno, variant->expected_errno[i]); 540 } else { 541 ASSERT_EQ(ret, 0); 542 } 543 544 if (i == 0) { 545 ret = getsockname(self->fd[0], &self->addr[0].addr, &self->addrlen[0]); 546 ASSERT_EQ(ret, 0); 547 } 548 } 549 550 TEST_F(bind_wildcard, plain) 551 { 552 int i; 553 554 for (i = 0; i < NR_SOCKETS; i++) 555 bind_socket(_metadata, self, variant, i); 556 } 557 558 TEST_HARNESS_MAIN 559