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 int expected_reuse_errno[NR_SOCKETS]; 58 }; 59 60 /* (IPv4, IPv4) */ 61 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v4_local) 62 { 63 .family = {AF_INET, AF_INET}, 64 .addr = {&in4addr_any, &in4addr_loopback}, 65 .expected_errno = {0, EADDRINUSE, 66 EADDRINUSE, EADDRINUSE, 67 EADDRINUSE, 0, 68 EADDRINUSE, EADDRINUSE}, 69 .expected_reuse_errno = {0, 0, 70 EADDRINUSE, EADDRINUSE, 71 EADDRINUSE, 0, 72 EADDRINUSE, EADDRINUSE}, 73 }; 74 75 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v4_any) 76 { 77 .family = {AF_INET, AF_INET}, 78 .addr = {&in4addr_loopback, &in4addr_any}, 79 .expected_errno = {0, EADDRINUSE, 80 EADDRINUSE, EADDRINUSE, 81 EADDRINUSE, 0, 82 EADDRINUSE, EADDRINUSE}, 83 .expected_reuse_errno = {0, 0, 84 EADDRINUSE, EADDRINUSE, 85 EADDRINUSE, 0, 86 EADDRINUSE, EADDRINUSE}, 87 }; 88 89 /* (IPv4, IPv6) */ 90 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any) 91 { 92 .family = {AF_INET, AF_INET6}, 93 .addr = {&in4addr_any, &in6addr_any}, 94 .expected_errno = {0, EADDRINUSE, 95 EADDRINUSE, EADDRINUSE, 96 EADDRINUSE, 0, 97 EADDRINUSE, EADDRINUSE}, 98 .expected_reuse_errno = {0, 0, 99 EADDRINUSE, EADDRINUSE, 100 EADDRINUSE, EADDRINUSE, 101 EADDRINUSE, EADDRINUSE}, 102 }; 103 104 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_any_only) 105 { 106 .family = {AF_INET, AF_INET6}, 107 .addr = {&in4addr_any, &in6addr_any}, 108 .ipv6_only = {false, true}, 109 .expected_errno = {0, 0, 110 EADDRINUSE, EADDRINUSE, 111 EADDRINUSE, EADDRINUSE, 112 EADDRINUSE, EADDRINUSE}, 113 .expected_reuse_errno = {0, 0, 114 EADDRINUSE, EADDRINUSE, 115 EADDRINUSE, EADDRINUSE, 116 EADDRINUSE, EADDRINUSE}, 117 }; 118 119 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_local) 120 { 121 .family = {AF_INET, AF_INET6}, 122 .addr = {&in4addr_any, &in6addr_loopback}, 123 .expected_errno = {0, 0, 124 EADDRINUSE, EADDRINUSE, 125 EADDRINUSE, EADDRINUSE, 126 EADDRINUSE, EADDRINUSE}, 127 .expected_reuse_errno = {0, 0, 128 EADDRINUSE, EADDRINUSE, 129 EADDRINUSE, EADDRINUSE, 130 EADDRINUSE, EADDRINUSE}, 131 }; 132 133 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_any) 134 { 135 .family = {AF_INET, AF_INET6}, 136 .addr = {&in4addr_any, &in6addr_v4mapped_any}, 137 .expected_errno = {0, EADDRINUSE, 138 EADDRINUSE, EADDRINUSE, 139 EADDRINUSE, 0, 140 EADDRINUSE, EADDRINUSE}, 141 .expected_reuse_errno = {0, 0, 142 EADDRINUSE, EADDRINUSE, 143 EADDRINUSE, 0, 144 EADDRINUSE, EADDRINUSE}, 145 }; 146 147 FIXTURE_VARIANT_ADD(bind_wildcard, v4_any_v6_v4mapped_local) 148 { 149 .family = {AF_INET, AF_INET6}, 150 .addr = {&in4addr_any, &in6addr_v4mapped_loopback}, 151 .expected_errno = {0, EADDRINUSE, 152 EADDRINUSE, EADDRINUSE, 153 EADDRINUSE, 0, 154 EADDRINUSE, EADDRINUSE}, 155 .expected_reuse_errno = {0, 0, 156 EADDRINUSE, EADDRINUSE, 157 EADDRINUSE, 0, 158 EADDRINUSE, EADDRINUSE}, 159 }; 160 161 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any) 162 { 163 .family = {AF_INET, AF_INET6}, 164 .addr = {&in4addr_loopback, &in6addr_any}, 165 .expected_errno = {0, EADDRINUSE, 166 EADDRINUSE, EADDRINUSE, 167 EADDRINUSE, 0, 168 EADDRINUSE, EADDRINUSE}, 169 .expected_reuse_errno = {0, 0, 170 EADDRINUSE, EADDRINUSE, 171 EADDRINUSE, EADDRINUSE, 172 EADDRINUSE, EADDRINUSE}, 173 }; 174 175 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_any_only) 176 { 177 .family = {AF_INET, AF_INET6}, 178 .addr = {&in4addr_loopback, &in6addr_any}, 179 .ipv6_only = {false, true}, 180 .expected_errno = {0, 0, 181 EADDRINUSE, EADDRINUSE, 182 EADDRINUSE, EADDRINUSE, 183 EADDRINUSE, EADDRINUSE}, 184 .expected_reuse_errno = {0, 0, 185 EADDRINUSE, EADDRINUSE, 186 EADDRINUSE, EADDRINUSE, 187 EADDRINUSE, EADDRINUSE}, 188 }; 189 190 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_local) 191 { 192 .family = {AF_INET, AF_INET6}, 193 .addr = {&in4addr_loopback, &in6addr_loopback}, 194 .expected_errno = {0, 0, 195 EADDRINUSE, EADDRINUSE, 196 EADDRINUSE, EADDRINUSE, 197 EADDRINUSE, EADDRINUSE}, 198 .expected_reuse_errno = {0, 0, 199 EADDRINUSE, EADDRINUSE, 200 EADDRINUSE, EADDRINUSE, 201 EADDRINUSE, EADDRINUSE}, 202 }; 203 204 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_any) 205 { 206 .family = {AF_INET, AF_INET6}, 207 .addr = {&in4addr_loopback, &in6addr_v4mapped_any}, 208 .expected_errno = {0, EADDRINUSE, 209 EADDRINUSE, EADDRINUSE, 210 EADDRINUSE, 0, 211 EADDRINUSE, EADDRINUSE}, 212 .expected_reuse_errno = {0, 0, 213 EADDRINUSE, EADDRINUSE, 214 EADDRINUSE, 0, 215 EADDRINUSE, EADDRINUSE}, 216 }; 217 218 FIXTURE_VARIANT_ADD(bind_wildcard, v4_local_v6_v4mapped_local) 219 { 220 .family = {AF_INET, AF_INET6}, 221 .addr = {&in4addr_loopback, &in6addr_v4mapped_loopback}, 222 .expected_errno = {0, EADDRINUSE, 223 EADDRINUSE, EADDRINUSE, 224 EADDRINUSE, 0, 225 EADDRINUSE, EADDRINUSE}, 226 .expected_reuse_errno = {0, 0, 227 EADDRINUSE, EADDRINUSE, 228 EADDRINUSE, 0, 229 EADDRINUSE, EADDRINUSE}, 230 }; 231 232 /* (IPv6, IPv4) */ 233 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_any) 234 { 235 .family = {AF_INET6, AF_INET}, 236 .addr = {&in6addr_any, &in4addr_any}, 237 .expected_errno = {0, EADDRINUSE, 238 EADDRINUSE, EADDRINUSE, 239 EADDRINUSE, EADDRINUSE, 240 EADDRINUSE, EADDRINUSE}, 241 .expected_reuse_errno = {0, 0, 242 EADDRINUSE, EADDRINUSE, 243 EADDRINUSE, EADDRINUSE, 244 EADDRINUSE, EADDRINUSE}, 245 }; 246 247 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v4_any) 248 { 249 .family = {AF_INET6, AF_INET}, 250 .addr = {&in6addr_any, &in4addr_any}, 251 .ipv6_only = {true, false}, 252 .expected_errno = {0, 0, 253 EADDRINUSE, EADDRINUSE, 254 EADDRINUSE, EADDRINUSE, 255 EADDRINUSE, EADDRINUSE}, 256 .expected_reuse_errno = {0, 0, 257 EADDRINUSE, EADDRINUSE, 258 EADDRINUSE, EADDRINUSE, 259 EADDRINUSE, EADDRINUSE}, 260 }; 261 262 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v4_local) 263 { 264 .family = {AF_INET6, AF_INET}, 265 .addr = {&in6addr_any, &in4addr_loopback}, 266 .expected_errno = {0, EADDRINUSE, 267 EADDRINUSE, EADDRINUSE, 268 EADDRINUSE, EADDRINUSE, 269 EADDRINUSE, EADDRINUSE}, 270 .expected_reuse_errno = {0, 0, 271 EADDRINUSE, EADDRINUSE, 272 EADDRINUSE, EADDRINUSE, 273 EADDRINUSE, EADDRINUSE}, 274 }; 275 276 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v4_local) 277 { 278 .family = {AF_INET6, AF_INET}, 279 .addr = {&in6addr_any, &in4addr_loopback}, 280 .ipv6_only = {true, false}, 281 .expected_errno = {0, 0, 282 EADDRINUSE, EADDRINUSE, 283 EADDRINUSE, EADDRINUSE, 284 EADDRINUSE, EADDRINUSE}, 285 .expected_reuse_errno = {0, 0, 286 EADDRINUSE, EADDRINUSE, 287 EADDRINUSE, EADDRINUSE, 288 EADDRINUSE, EADDRINUSE}, 289 }; 290 291 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_any) 292 { 293 .family = {AF_INET6, AF_INET}, 294 .addr = {&in6addr_loopback, &in4addr_any}, 295 .expected_errno = {0, 0, 296 EADDRINUSE, EADDRINUSE, 297 EADDRINUSE, EADDRINUSE, 298 EADDRINUSE, EADDRINUSE}, 299 .expected_reuse_errno = {0, 0, 300 EADDRINUSE, EADDRINUSE, 301 EADDRINUSE, EADDRINUSE, 302 EADDRINUSE, EADDRINUSE}, 303 }; 304 305 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v4_local) 306 { 307 .family = {AF_INET6, AF_INET}, 308 .addr = {&in6addr_loopback, &in4addr_loopback}, 309 .expected_errno = {0, 0, 310 EADDRINUSE, EADDRINUSE, 311 EADDRINUSE, EADDRINUSE, 312 EADDRINUSE, EADDRINUSE}, 313 .expected_reuse_errno = {0, 0, 314 EADDRINUSE, EADDRINUSE, 315 EADDRINUSE, EADDRINUSE, 316 EADDRINUSE, EADDRINUSE}, 317 }; 318 319 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_any) 320 { 321 .family = {AF_INET6, AF_INET}, 322 .addr = {&in6addr_v4mapped_any, &in4addr_any}, 323 .expected_errno = {0, EADDRINUSE, 324 EADDRINUSE, EADDRINUSE, 325 EADDRINUSE, 0, 326 EADDRINUSE, EADDRINUSE}, 327 .expected_reuse_errno = {0, 0, 328 EADDRINUSE, EADDRINUSE, 329 EADDRINUSE, 0, 330 EADDRINUSE, EADDRINUSE}, 331 }; 332 333 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v4_local) 334 { 335 .family = {AF_INET6, AF_INET}, 336 .addr = {&in6addr_v4mapped_any, &in4addr_loopback}, 337 .expected_errno = {0, EADDRINUSE, 338 EADDRINUSE, EADDRINUSE, 339 EADDRINUSE, 0, 340 EADDRINUSE, EADDRINUSE}, 341 .expected_reuse_errno = {0, 0, 342 EADDRINUSE, EADDRINUSE, 343 EADDRINUSE, 0, 344 EADDRINUSE, EADDRINUSE}, 345 }; 346 347 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_any) 348 { 349 .family = {AF_INET6, AF_INET}, 350 .addr = {&in6addr_v4mapped_loopback, &in4addr_any}, 351 .expected_errno = {0, EADDRINUSE, 352 EADDRINUSE, EADDRINUSE, 353 EADDRINUSE, 0, 354 EADDRINUSE, EADDRINUSE}, 355 .expected_reuse_errno = {0, 0, 356 EADDRINUSE, EADDRINUSE, 357 EADDRINUSE, 0, 358 EADDRINUSE, EADDRINUSE}, 359 }; 360 361 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_local_v4_local) 362 { 363 .family = {AF_INET6, AF_INET}, 364 .addr = {&in6addr_v4mapped_loopback, &in4addr_loopback}, 365 .expected_errno = {0, EADDRINUSE, 366 EADDRINUSE, EADDRINUSE, 367 EADDRINUSE, 0, 368 EADDRINUSE, EADDRINUSE}, 369 .expected_reuse_errno = {0, 0, 370 EADDRINUSE, EADDRINUSE, 371 EADDRINUSE, 0, 372 EADDRINUSE, EADDRINUSE}, 373 }; 374 375 /* (IPv6, IPv6) */ 376 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_any) 377 { 378 .family = {AF_INET6, AF_INET6}, 379 .addr = {&in6addr_any, &in6addr_any}, 380 .expected_errno = {0, EADDRINUSE, 381 EADDRINUSE, EADDRINUSE, 382 EADDRINUSE, EADDRINUSE, 383 EADDRINUSE, EADDRINUSE}, 384 .expected_reuse_errno = {0, 0, 385 EADDRINUSE, EADDRINUSE, 386 EADDRINUSE, EADDRINUSE, 387 EADDRINUSE, EADDRINUSE}, 388 }; 389 390 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_any) 391 { 392 .family = {AF_INET6, AF_INET6}, 393 .addr = {&in6addr_any, &in6addr_any}, 394 .ipv6_only = {true, false}, 395 .expected_errno = {0, EADDRINUSE, 396 0, EADDRINUSE, 397 EADDRINUSE, EADDRINUSE, 398 EADDRINUSE, EADDRINUSE}, 399 .expected_reuse_errno = {0, 0, 400 EADDRINUSE, EADDRINUSE, 401 EADDRINUSE, EADDRINUSE, 402 EADDRINUSE, EADDRINUSE}, 403 }; 404 405 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_any_only) 406 { 407 .family = {AF_INET6, AF_INET6}, 408 .addr = {&in6addr_any, &in6addr_any}, 409 .ipv6_only = {false, true}, 410 .expected_errno = {0, EADDRINUSE, 411 EADDRINUSE, EADDRINUSE, 412 EADDRINUSE, EADDRINUSE, 413 EADDRINUSE, EADDRINUSE}, 414 .expected_reuse_errno = {0, 0, 415 EADDRINUSE, EADDRINUSE, 416 EADDRINUSE, EADDRINUSE, 417 EADDRINUSE, EADDRINUSE}, 418 }; 419 420 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_any_only) 421 { 422 .family = {AF_INET6, AF_INET6}, 423 .addr = {&in6addr_any, &in6addr_any}, 424 .ipv6_only = {true, true}, 425 .expected_errno = {0, EADDRINUSE, 426 0, EADDRINUSE, 427 EADDRINUSE, EADDRINUSE, 428 EADDRINUSE, EADDRINUSE}, 429 .expected_reuse_errno = {0, 0, 430 0, EADDRINUSE, 431 EADDRINUSE, EADDRINUSE, 432 EADDRINUSE, EADDRINUSE}, 433 }; 434 435 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_local) 436 { 437 .family = {AF_INET6, AF_INET6}, 438 .addr = {&in6addr_any, &in6addr_loopback}, 439 .expected_errno = {0, EADDRINUSE, 440 EADDRINUSE, EADDRINUSE, 441 EADDRINUSE, EADDRINUSE, 442 EADDRINUSE, EADDRINUSE}, 443 .expected_reuse_errno = {0, 0, 444 EADDRINUSE, EADDRINUSE, 445 EADDRINUSE, EADDRINUSE, 446 EADDRINUSE, EADDRINUSE}, 447 }; 448 449 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_local) 450 { 451 .family = {AF_INET6, AF_INET6}, 452 .addr = {&in6addr_any, &in6addr_loopback}, 453 .ipv6_only = {true, false}, 454 .expected_errno = {0, EADDRINUSE, 455 0, EADDRINUSE, 456 EADDRINUSE, EADDRINUSE, 457 EADDRINUSE, EADDRINUSE}, 458 .expected_reuse_errno = {0, 0, 459 0, EADDRINUSE, 460 EADDRINUSE, EADDRINUSE, 461 EADDRINUSE, EADDRINUSE}, 462 }; 463 464 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_any) 465 { 466 .family = {AF_INET6, AF_INET6}, 467 .addr = {&in6addr_any, &in6addr_v4mapped_any}, 468 .expected_errno = {0, EADDRINUSE, 469 EADDRINUSE, EADDRINUSE, 470 EADDRINUSE, EADDRINUSE, 471 EADDRINUSE, EADDRINUSE}, 472 .expected_reuse_errno = {0, 0, 473 EADDRINUSE, EADDRINUSE, 474 EADDRINUSE, EADDRINUSE, 475 EADDRINUSE, EADDRINUSE}, 476 }; 477 478 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_v4mapped_any) 479 { 480 .family = {AF_INET6, AF_INET6}, 481 .addr = {&in6addr_any, &in6addr_v4mapped_any}, 482 .ipv6_only = {true, false}, 483 .expected_errno = {0, 0, 484 EADDRINUSE, EADDRINUSE, 485 EADDRINUSE, EADDRINUSE, 486 EADDRINUSE, EADDRINUSE}, 487 .expected_reuse_errno = {0, 0, 488 EADDRINUSE, EADDRINUSE, 489 EADDRINUSE, EADDRINUSE, 490 EADDRINUSE, EADDRINUSE}, 491 }; 492 493 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_v6_v4mapped_local) 494 { 495 .family = {AF_INET6, AF_INET6}, 496 .addr = {&in6addr_any, &in6addr_v4mapped_loopback}, 497 .expected_errno = {0, EADDRINUSE, 498 EADDRINUSE, EADDRINUSE, 499 EADDRINUSE, EADDRINUSE, 500 EADDRINUSE, EADDRINUSE}, 501 .expected_reuse_errno = {0, 0, 502 EADDRINUSE, EADDRINUSE, 503 EADDRINUSE, EADDRINUSE, 504 EADDRINUSE, EADDRINUSE}, 505 }; 506 507 FIXTURE_VARIANT_ADD(bind_wildcard, v6_any_only_v6_v4mapped_local) 508 { 509 .family = {AF_INET6, AF_INET6}, 510 .addr = {&in6addr_any, &in6addr_v4mapped_loopback}, 511 .ipv6_only = {true, false}, 512 .expected_errno = {0, 0, 513 EADDRINUSE, EADDRINUSE, 514 EADDRINUSE, EADDRINUSE, 515 EADDRINUSE, EADDRINUSE}, 516 .expected_reuse_errno = {0, 0, 517 EADDRINUSE, EADDRINUSE, 518 EADDRINUSE, EADDRINUSE, 519 EADDRINUSE, EADDRINUSE}, 520 }; 521 522 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any) 523 { 524 .family = {AF_INET6, AF_INET6}, 525 .addr = {&in6addr_loopback, &in6addr_any}, 526 .expected_errno = {0, EADDRINUSE, 527 0, EADDRINUSE, 528 EADDRINUSE, EADDRINUSE, 529 EADDRINUSE, EADDRINUSE}, 530 .expected_reuse_errno = {0, 0, 531 EADDRINUSE, EADDRINUSE, 532 EADDRINUSE, EADDRINUSE, 533 EADDRINUSE, EADDRINUSE}, 534 }; 535 536 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_any_only) 537 { 538 .family = {AF_INET6, AF_INET6}, 539 .addr = {&in6addr_loopback, &in6addr_any}, 540 .ipv6_only = {false, true}, 541 .expected_errno = {0, EADDRINUSE, 542 0, EADDRINUSE, 543 EADDRINUSE, EADDRINUSE, 544 EADDRINUSE, EADDRINUSE}, 545 .expected_reuse_errno = {0, 0, 546 0, EADDRINUSE, 547 EADDRINUSE, EADDRINUSE, 548 EADDRINUSE, EADDRINUSE}, 549 }; 550 551 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_any) 552 { 553 .family = {AF_INET6, AF_INET6}, 554 .addr = {&in6addr_loopback, &in6addr_v4mapped_any}, 555 .expected_errno = {0, 0, 556 EADDRINUSE, EADDRINUSE, 557 EADDRINUSE, EADDRINUSE, 558 EADDRINUSE, EADDRINUSE}, 559 .expected_reuse_errno = {0, 0, 560 EADDRINUSE, EADDRINUSE, 561 EADDRINUSE, EADDRINUSE, 562 EADDRINUSE, EADDRINUSE}, 563 }; 564 565 FIXTURE_VARIANT_ADD(bind_wildcard, v6_local_v6_v4mapped_local) 566 { 567 .family = {AF_INET6, AF_INET6}, 568 .addr = {&in6addr_loopback, &in6addr_v4mapped_loopback}, 569 .expected_errno = {0, 0, 570 EADDRINUSE, EADDRINUSE, 571 EADDRINUSE, EADDRINUSE, 572 EADDRINUSE, EADDRINUSE}, 573 .expected_reuse_errno = {0, 0, 574 EADDRINUSE, EADDRINUSE, 575 EADDRINUSE, EADDRINUSE, 576 EADDRINUSE, EADDRINUSE}, 577 }; 578 579 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any) 580 { 581 .family = {AF_INET6, AF_INET6}, 582 .addr = {&in6addr_v4mapped_any, &in6addr_any}, 583 .expected_errno = {0, EADDRINUSE, 584 EADDRINUSE, EADDRINUSE, 585 EADDRINUSE, 0, 586 EADDRINUSE, EADDRINUSE}, 587 .expected_reuse_errno = {0, 0, 588 EADDRINUSE, EADDRINUSE, 589 EADDRINUSE, EADDRINUSE, 590 EADDRINUSE, EADDRINUSE}, 591 }; 592 593 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_any_only) 594 { 595 .family = {AF_INET6, AF_INET6}, 596 .addr = {&in6addr_v4mapped_any, &in6addr_any}, 597 .ipv6_only = {false, true}, 598 .expected_errno = {0, 0, 599 EADDRINUSE, EADDRINUSE, 600 EADDRINUSE, EADDRINUSE, 601 EADDRINUSE, EADDRINUSE}, 602 .expected_reuse_errno = {0, 0, 603 EADDRINUSE, EADDRINUSE, 604 EADDRINUSE, EADDRINUSE, 605 EADDRINUSE, EADDRINUSE}, 606 }; 607 608 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_local) 609 { 610 .family = {AF_INET6, AF_INET6}, 611 .addr = {&in6addr_v4mapped_any, &in6addr_loopback}, 612 .expected_errno = {0, 0, 613 EADDRINUSE, EADDRINUSE, 614 EADDRINUSE, EADDRINUSE, 615 EADDRINUSE, EADDRINUSE}, 616 .expected_reuse_errno = {0, 0, 617 EADDRINUSE, EADDRINUSE, 618 EADDRINUSE, EADDRINUSE, 619 EADDRINUSE, EADDRINUSE}, 620 }; 621 622 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_any_v6_v4mapped_local) 623 { 624 .family = {AF_INET6, AF_INET6}, 625 .addr = {&in6addr_v4mapped_any, &in6addr_v4mapped_loopback}, 626 .expected_errno = {0, EADDRINUSE, 627 EADDRINUSE, EADDRINUSE, 628 EADDRINUSE, 0, 629 EADDRINUSE, EADDRINUSE}, 630 .expected_reuse_errno = {0, 0, 631 EADDRINUSE, EADDRINUSE, 632 EADDRINUSE, 0, 633 EADDRINUSE, EADDRINUSE}, 634 }; 635 636 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any) 637 { 638 .family = {AF_INET6, AF_INET6}, 639 .addr = {&in6addr_v4mapped_loopback, &in6addr_any}, 640 .expected_errno = {0, EADDRINUSE, 641 EADDRINUSE, EADDRINUSE, 642 EADDRINUSE, 0, 643 EADDRINUSE, EADDRINUSE}, 644 .expected_reuse_errno = {0, 0, 645 EADDRINUSE, EADDRINUSE, 646 EADDRINUSE, EADDRINUSE, 647 EADDRINUSE, EADDRINUSE}, 648 }; 649 650 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_any_only) 651 { 652 .family = {AF_INET6, AF_INET6}, 653 .addr = {&in6addr_v4mapped_loopback, &in6addr_any}, 654 .ipv6_only = {false, true}, 655 .expected_errno = {0, 0, 656 EADDRINUSE, EADDRINUSE, 657 EADDRINUSE, EADDRINUSE, 658 EADDRINUSE, EADDRINUSE}, 659 .expected_reuse_errno = {0, 0, 660 EADDRINUSE, EADDRINUSE, 661 EADDRINUSE, EADDRINUSE, 662 EADDRINUSE, EADDRINUSE}, 663 }; 664 665 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_local) 666 { 667 .family = {AF_INET6, AF_INET6}, 668 .addr = {&in6addr_v4mapped_loopback, &in6addr_loopback}, 669 .expected_errno = {0, 0, 670 EADDRINUSE, EADDRINUSE, 671 EADDRINUSE, EADDRINUSE, 672 EADDRINUSE, EADDRINUSE}, 673 .expected_reuse_errno = {0, 0, 674 EADDRINUSE, EADDRINUSE, 675 EADDRINUSE, EADDRINUSE, 676 EADDRINUSE, EADDRINUSE}, 677 }; 678 679 FIXTURE_VARIANT_ADD(bind_wildcard, v6_v4mapped_loopback_v6_v4mapped_any) 680 { 681 .family = {AF_INET6, AF_INET6}, 682 .addr = {&in6addr_v4mapped_loopback, &in6addr_v4mapped_any}, 683 .expected_errno = {0, EADDRINUSE, 684 EADDRINUSE, EADDRINUSE, 685 EADDRINUSE, 0, 686 EADDRINUSE, EADDRINUSE}, 687 .expected_reuse_errno = {0, 0, 688 EADDRINUSE, EADDRINUSE, 689 EADDRINUSE, 0, 690 EADDRINUSE, EADDRINUSE}, 691 }; 692 693 static void setup_addr(FIXTURE_DATA(bind_wildcard) *self, int i, 694 int family, const void *addr_const) 695 { 696 if (family == AF_INET) { 697 struct sockaddr_in *addr4 = &self->addr[i].addr4; 698 const __u32 *addr4_const = addr_const; 699 700 addr4->sin_family = AF_INET; 701 addr4->sin_port = htons(0); 702 addr4->sin_addr.s_addr = htonl(*addr4_const); 703 704 self->addrlen[i] = sizeof(struct sockaddr_in); 705 } else { 706 struct sockaddr_in6 *addr6 = &self->addr[i].addr6; 707 const struct in6_addr *addr6_const = addr_const; 708 709 addr6->sin6_family = AF_INET6; 710 addr6->sin6_port = htons(0); 711 addr6->sin6_addr = *addr6_const; 712 713 self->addrlen[i] = sizeof(struct sockaddr_in6); 714 } 715 } 716 717 FIXTURE_SETUP(bind_wildcard) 718 { 719 setup_addr(self, 0, variant->family[0], variant->addr[0]); 720 setup_addr(self, 1, variant->family[1], variant->addr[1]); 721 722 setup_addr(self, 2, AF_INET, &in4addr_any); 723 setup_addr(self, 3, AF_INET, &in4addr_loopback); 724 725 setup_addr(self, 4, AF_INET6, &in6addr_any); 726 setup_addr(self, 5, AF_INET6, &in6addr_loopback); 727 setup_addr(self, 6, AF_INET6, &in6addr_v4mapped_any); 728 setup_addr(self, 7, AF_INET6, &in6addr_v4mapped_loopback); 729 } 730 731 FIXTURE_TEARDOWN(bind_wildcard) 732 { 733 int i; 734 735 for (i = 0; i < NR_SOCKETS; i++) 736 close(self->fd[i]); 737 } 738 739 void bind_socket(struct __test_metadata *_metadata, 740 FIXTURE_DATA(bind_wildcard) *self, 741 const FIXTURE_VARIANT(bind_wildcard) *variant, 742 int i, int reuse) 743 { 744 int ret; 745 746 self->fd[i] = socket(self->addr[i].addr.sa_family, SOCK_STREAM, 0); 747 ASSERT_GT(self->fd[i], 0); 748 749 if (i < 2 && variant->ipv6_only[i]) { 750 ret = setsockopt(self->fd[i], SOL_IPV6, IPV6_V6ONLY, &(int){1}, sizeof(int)); 751 ASSERT_EQ(ret, 0); 752 } 753 754 if (i < 2 && reuse) { 755 ret = setsockopt(self->fd[i], SOL_SOCKET, reuse, &(int){1}, sizeof(int)); 756 ASSERT_EQ(ret, 0); 757 } 758 759 self->addr[i].addr4.sin_port = self->addr[0].addr4.sin_port; 760 761 ret = bind(self->fd[i], &self->addr[i].addr, self->addrlen[i]); 762 763 if (reuse) { 764 if (variant->expected_reuse_errno[i]) { 765 ASSERT_EQ(ret, -1); 766 ASSERT_EQ(errno, variant->expected_reuse_errno[i]); 767 } else { 768 ASSERT_EQ(ret, 0); 769 } 770 } else { 771 if (variant->expected_errno[i]) { 772 ASSERT_EQ(ret, -1); 773 ASSERT_EQ(errno, variant->expected_errno[i]); 774 } else { 775 ASSERT_EQ(ret, 0); 776 } 777 } 778 779 if (i == 0) { 780 ret = getsockname(self->fd[0], &self->addr[0].addr, &self->addrlen[0]); 781 ASSERT_EQ(ret, 0); 782 } 783 } 784 785 TEST_F(bind_wildcard, plain) 786 { 787 int i; 788 789 for (i = 0; i < NR_SOCKETS; i++) 790 bind_socket(_metadata, self, variant, i, 0); 791 } 792 793 TEST_F(bind_wildcard, reuseaddr) 794 { 795 int i; 796 797 for (i = 0; i < NR_SOCKETS; i++) 798 bind_socket(_metadata, self, variant, i, SO_REUSEADDR); 799 } 800 801 TEST_F(bind_wildcard, reuseport) 802 { 803 int i; 804 805 for (i = 0; i < NR_SOCKETS; i++) 806 bind_socket(_metadata, self, variant, i, SO_REUSEPORT); 807 } 808 809 TEST_HARNESS_MAIN 810
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.