1 /* $NetBSD: mcast.c,v 1.3 2015/05/28 10:19:17 ozaki-r Exp $ */ 2 3 /*- 4 * Copyright (c) 2014 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 #ifdef __RCSID 33 __RCSID("$NetBSD: mcast.c,v 1.3 2015/05/28 10:19:17 ozaki-r Exp $"); 34 #else 35 extern const char *__progname; 36 #define getprogname() __progname 37 #endif 38 39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <sys/wait.h> 42 #include <sys/time.h> 43 #include <netinet/in.h> 44 45 #include <assert.h> 46 #include <netdb.h> 47 #include <time.h> 48 #include <signal.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 #include <err.h> 54 #include <errno.h> 55 #include <poll.h> 56 #include <stdbool.h> 57 58 #ifdef ATF 59 #include <atf-c.h> 60 61 #define ERRX(ev, msg, ...) ATF_REQUIRE_MSG(0, msg, __VA_ARGS__) 62 #define ERRX0(ev, msg) ATF_REQUIRE_MSG(0, msg) 63 64 #define SKIPX(ev, msg, ...) do { \ 65 atf_tc_skip(msg, __VA_ARGS__); \ 66 return; \ 67 } while(/*CONSTCOND*/0) 68 69 #else 70 #define ERRX(ev, msg, ...) errx(ev, msg, __VA_ARGS__) 71 #define ERRX0(ev, msg) errx(ev, msg) 72 #define SKIPX(ev, msg, ...) errx(ev, msg, __VA_ARGS__) 73 #endif 74 75 static int debug; 76 77 #define TOTAL 10 78 #define PORT_V4MAPPED "6666" 79 #define HOST_V4MAPPED "::FFFF:239.1.1.1" 80 #define PORT_V4 "6666" 81 #define HOST_V4 "239.1.1.1" 82 #define PORT_V6 "6666" 83 #define HOST_V6 "FF05:1:0:0:0:0:0:1" 84 85 struct message { 86 size_t seq; 87 struct timespec ts; 88 }; 89 90 static int 91 addmc(int s, struct addrinfo *ai, bool bug) 92 { 93 struct ip_mreq m4; 94 struct ipv6_mreq m6; 95 struct sockaddr_in *s4; 96 struct sockaddr_in6 *s6; 97 unsigned int ifc; 98 99 switch (ai->ai_family) { 100 case AF_INET: 101 s4 = (void *)ai->ai_addr; 102 assert(sizeof(*s4) == ai->ai_addrlen); 103 m4.imr_multiaddr = s4->sin_addr; 104 m4.imr_interface.s_addr = htonl(INADDR_ANY); 105 return setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 106 &m4, sizeof(m4)); 107 case AF_INET6: 108 s6 = (void *)ai->ai_addr; 109 /* 110 * Linux: Does not support the v6 ioctls on v4 mapped 111 * sockets but it does support the v4 ones and 112 * it works. 113 * MacOS/X: Supports the v6 ioctls on v4 mapped sockets, 114 * but does not work and also does not support 115 * the v4 ioctls. So no way to make multicasting 116 * work with mapped addresses. 117 * NetBSD: Supports both and works for both. 118 */ 119 if (bug && IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) { 120 memcpy(&m4.imr_multiaddr, &s6->sin6_addr.s6_addr[12], 121 sizeof(m4.imr_multiaddr)); 122 m4.imr_interface.s_addr = htonl(INADDR_ANY); 123 return setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 124 &m4, sizeof(m4)); 125 } 126 assert(sizeof(*s6) == ai->ai_addrlen); 127 memset(&m6, 0, sizeof(m6)); 128 #if 0 129 ifc = 1; 130 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 131 &ifc, sizeof(ifc)) == -1) 132 return -1; 133 ifc = 224; 134 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 135 &ifc, sizeof(ifc)) == -1) 136 return -1; 137 ifc = 1; /* XXX should pick a proper interface */ 138 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifc, 139 sizeof(ifc)) == -1) 140 return -1; 141 #else 142 ifc = 0; /* Let pick an appropriate interface */ 143 #endif 144 m6.ipv6mr_interface = ifc; 145 m6.ipv6mr_multiaddr = s6->sin6_addr; 146 return setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, 147 &m6, sizeof(m6)); 148 default: 149 errno = EOPNOTSUPP; 150 return -1; 151 } 152 } 153 154 static int 155 allowv4mapped(int s, struct addrinfo *ai) 156 { 157 struct sockaddr_in6 *s6; 158 int zero = 0; 159 160 if (ai->ai_family != AF_INET6) 161 return 0; 162 163 s6 = (void *)ai->ai_addr; 164 165 if (!IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) 166 return 0; 167 return setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)); 168 } 169 170 static struct sockaddr_storage ss; 171 static int 172 connector(int fd, const struct sockaddr *sa, socklen_t slen) 173 { 174 assert(sizeof(ss) > slen); 175 memcpy(&ss, sa, slen); 176 return 0; 177 } 178 179 static void 180 show(const char *prefix, const struct message *msg) 181 { 182 printf("%10.10s: %zu [%jd.%ld]\n", prefix, msg->seq, (intmax_t) 183 msg->ts.tv_sec, msg->ts.tv_nsec); 184 } 185 186 static int 187 getsocket(const char *host, const char *port, 188 int (*f)(int, const struct sockaddr *, socklen_t), socklen_t *slen, 189 bool bug) 190 { 191 int e, s, lasterrno = 0; 192 struct addrinfo hints, *ai0, *ai; 193 const char *cause = "?"; 194 195 memset(&hints, 0, sizeof(hints)); 196 hints.ai_family = AF_UNSPEC; 197 hints.ai_socktype = SOCK_DGRAM; 198 e = getaddrinfo(host, port, &hints, &ai0); 199 if (e) 200 ERRX(EXIT_FAILURE, "Can't resolve %s:%s (%s)", host, port, 201 gai_strerror(e)); 202 203 s = -1; 204 for (ai = ai0; ai; ai = ai->ai_next) { 205 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 206 if (s == -1) { 207 lasterrno = errno; 208 cause = "socket"; 209 continue; 210 } 211 if (allowv4mapped(s, ai) == -1) { 212 cause = "allow v4 mapped"; 213 goto out; 214 } 215 if ((*f)(s, ai->ai_addr, ai->ai_addrlen) == -1) { 216 cause = f == bind ? "bind" : "connect"; 217 goto out; 218 } 219 if ((f == bind || f == connector) && addmc(s, ai, bug) == -1) { 220 cause = "join group"; 221 goto out; 222 } 223 *slen = ai->ai_addrlen; 224 break; 225 out: 226 lasterrno = errno; 227 close(s); 228 s = -1; 229 continue; 230 } 231 freeaddrinfo(ai0); 232 if (s == -1) 233 ERRX(EXIT_FAILURE, "%s (%s)", cause, strerror(lasterrno)); 234 return s; 235 } 236 237 static int 238 synchronize(const int fd, bool waiter) 239 { 240 int syncmsg = 0; 241 int r; 242 struct pollfd pfd; 243 244 if (waiter) { 245 pfd.fd = fd; 246 pfd.events = POLLIN; 247 248 /* We use poll to avoid lock up when the peer died unexpectedly */ 249 r = poll(&pfd, 1, 10000); 250 if (r == -1) 251 ERRX(EXIT_FAILURE, "poll (%s)", strerror(errno)); 252 if (r == 0) 253 /* Timed out */ 254 return -1; 255 256 if (read(fd, &syncmsg, sizeof(syncmsg)) == -1) 257 ERRX(EXIT_FAILURE, "read (%s)", strerror(errno)); 258 } else { 259 if (write(fd, &syncmsg, sizeof(syncmsg)) == -1) 260 ERRX(EXIT_FAILURE, "write (%s)", strerror(errno)); 261 } 262 263 return 0; 264 } 265 266 static int 267 sender(const int fd, const char *host, const char *port, size_t n, bool conn, 268 bool bug) 269 { 270 int s; 271 ssize_t l; 272 struct message msg; 273 274 socklen_t slen; 275 276 s = getsocket(host, port, conn ? connect : connector, &slen, bug); 277 278 /* Wait until receiver gets ready. */ 279 if (synchronize(fd, true) == -1) 280 return -1; 281 282 for (msg.seq = 0; msg.seq < n; msg.seq++) { 283 #ifdef CLOCK_MONOTONIC 284 if (clock_gettime(CLOCK_MONOTONIC, &msg.ts) == -1) 285 ERRX(EXIT_FAILURE, "clock (%s)", strerror(errno)); 286 #else 287 struct timeval tv; 288 if (gettimeofday(&tv, NULL) == -1) 289 ERRX(EXIT_FAILURE, "clock (%s)", strerror(errno)); 290 msg.ts.tv_sec = tv.tv_sec; 291 msg.ts.tv_nsec = tv.tv_usec * 1000; 292 #endif 293 if (debug) 294 show("sending", &msg); 295 l = conn ? send(s, &msg, sizeof(msg), 0) : 296 sendto(s, &msg, sizeof(msg), 0, (void *)&ss, slen); 297 if (l == -1) 298 ERRX(EXIT_FAILURE, "send (%s)", strerror(errno)); 299 usleep(100); 300 } 301 302 /* Wait until receiver finishes its work. */ 303 if (synchronize(fd, true) == -1) 304 return -1; 305 306 return 0; 307 } 308 309 static void 310 receiver(const int fd, const char *host, const char *port, size_t n, bool conn, 311 bool bug) 312 { 313 int s; 314 ssize_t l; 315 size_t seq; 316 struct message msg; 317 struct pollfd pfd; 318 socklen_t slen; 319 320 s = getsocket(host, port, bind, &slen, bug); 321 pfd.fd = s; 322 pfd.events = POLLIN; 323 324 /* Tell I'm ready */ 325 synchronize(fd, false); 326 327 for (seq = 0; seq < n; seq++) { 328 if (poll(&pfd, 1, 10000) == -1) 329 ERRX(EXIT_FAILURE, "poll (%s)", strerror(errno)); 330 l = conn ? recv(s, &msg, sizeof(msg), 0) : 331 recvfrom(s, &msg, sizeof(msg), 0, (void *)&ss, &slen); 332 if (l == -1) 333 ERRX(EXIT_FAILURE, "recv (%s)", strerror(errno)); 334 if (debug) 335 show("got", &msg); 336 if (seq != msg.seq) 337 ERRX(EXIT_FAILURE, "seq: expect=%zu actual=%zu", 338 seq, msg.seq); 339 } 340 341 /* Tell I'm finished */ 342 synchronize(fd, false); 343 } 344 345 static void 346 run(const char *host, const char *port, size_t n, bool conn, bool bug) 347 { 348 pid_t pid; 349 int status; 350 int syncfds[2]; 351 int error; 352 353 if (socketpair(AF_UNIX, SOCK_STREAM, 0, syncfds) == -1) 354 ERRX(EXIT_FAILURE, "socketpair (%s)", strerror(errno)); 355 356 switch ((pid = fork())) { 357 case 0: 358 receiver(syncfds[0], host, port, n, conn, bug); 359 return; 360 case -1: 361 ERRX(EXIT_FAILURE, "fork (%s)", strerror(errno)); 362 default: 363 error = sender(syncfds[1], host, port, n, conn, bug); 364 again: 365 switch (waitpid(pid, &status, WNOHANG)) { 366 case -1: 367 ERRX(EXIT_FAILURE, "wait (%s)", strerror(errno)); 368 case 0: 369 if (error == 0) 370 /* 371 * Receiver is still alive, but we know 372 * it will exit soon. 373 */ 374 goto again; 375 376 if (kill(pid, SIGTERM) == -1) 377 ERRX(EXIT_FAILURE, "kill (%s)", 378 strerror(errno)); 379 goto again; 380 default: 381 if (WIFSIGNALED(status)) { 382 if (WTERMSIG(status) == SIGTERM) 383 ERRX0(EXIT_FAILURE, 384 "receiver failed and was killed" \ 385 "by sender"); 386 else 387 ERRX(EXIT_FAILURE, 388 "receiver got signaled (%s)", 389 strsignal(WTERMSIG(status))); 390 } else if (WIFEXITED(status)) { 391 if (WEXITSTATUS(status) != 0) 392 ERRX(EXIT_FAILURE, 393 "receiver exited with status %d", 394 WEXITSTATUS(status)); 395 } else { 396 ERRX(EXIT_FAILURE, 397 "receiver exited with unexpected status %d", 398 status); 399 } 400 break; 401 } 402 return; 403 } 404 } 405 406 #ifndef ATF 407 int 408 main(int argc, char *argv[]) 409 { 410 const char *host, *port; 411 int c; 412 size_t n; 413 bool conn, bug; 414 415 host = HOST_V4; 416 port = PORT_V4; 417 n = TOTAL; 418 bug = conn = false; 419 420 while ((c = getopt(argc, argv, "46bcdmn:")) != -1) 421 switch (c) { 422 case '4': 423 host = HOST_V4; 424 port = PORT_V4; 425 break; 426 case '6': 427 host = HOST_V6; 428 port = PORT_V6; 429 break; 430 case 'b': 431 bug = true; 432 break; 433 case 'c': 434 conn = true; 435 break; 436 case 'd': 437 debug++; 438 break; 439 case 'm': 440 host = HOST_V4MAPPED; 441 port = PORT_V4MAPPED; 442 break; 443 case 'n': 444 n = atoi(optarg); 445 break; 446 default: 447 fprintf(stderr, "Usage: %s [-cdm46] [-n <tot>]", 448 getprogname()); 449 return 1; 450 } 451 452 run(host, port, n, conn, bug); 453 return 0; 454 } 455 #else 456 457 ATF_TC(conninet4); 458 ATF_TC_HEAD(conninet4, tc) 459 { 460 atf_tc_set_md_var(tc, "descr", "Checks connected multicast for ipv4"); 461 } 462 463 ATF_TC_BODY(conninet4, tc) 464 { 465 run(HOST_V4, PORT_V4, TOTAL, true, false); 466 } 467 468 ATF_TC(connmappedinet4); 469 ATF_TC_HEAD(connmappedinet4, tc) 470 { 471 atf_tc_set_md_var(tc, "descr", "Checks connected multicast for mapped ipv4"); 472 } 473 474 ATF_TC_BODY(connmappedinet4, tc) 475 { 476 run(HOST_V4MAPPED, PORT_V4MAPPED, TOTAL, true, false); 477 } 478 479 ATF_TC(connmappedbuginet4); 480 ATF_TC_HEAD(connmappedbuginet4, tc) 481 { 482 atf_tc_set_md_var(tc, "descr", "Checks connected multicast for mapped ipv4 using the v4 ioctls"); 483 } 484 485 ATF_TC_BODY(connmappedbuginet4, tc) 486 { 487 run(HOST_V4MAPPED, PORT_V4MAPPED, TOTAL, true, true); 488 } 489 490 ATF_TC(conninet6); 491 ATF_TC_HEAD(conninet6, tc) 492 { 493 atf_tc_set_md_var(tc, "descr", "Checks connected multicast for ipv6"); 494 } 495 496 ATF_TC_BODY(conninet6, tc) 497 { 498 run(HOST_V6, PORT_V6, TOTAL, true, false); 499 } 500 501 ATF_TC(unconninet4); 502 ATF_TC_HEAD(unconninet4, tc) 503 { 504 atf_tc_set_md_var(tc, "descr", "Checks unconnected multicast for ipv4"); 505 } 506 507 ATF_TC_BODY(unconninet4, tc) 508 { 509 run(HOST_V4, PORT_V4, TOTAL, false, false); 510 } 511 512 ATF_TC(unconnmappedinet4); 513 ATF_TC_HEAD(unconnmappedinet4, tc) 514 { 515 atf_tc_set_md_var(tc, "descr", "Checks unconnected multicast for mapped ipv4"); 516 } 517 518 ATF_TC_BODY(unconnmappedinet4, tc) 519 { 520 run(HOST_V4MAPPED, PORT_V4MAPPED, TOTAL, false, false); 521 } 522 523 ATF_TC(unconnmappedbuginet4); 524 ATF_TC_HEAD(unconnmappedbuginet4, tc) 525 { 526 atf_tc_set_md_var(tc, "descr", "Checks unconnected multicast for mapped ipv4 using the v4 ioctls"); 527 } 528 529 ATF_TC_BODY(unconnmappedbuginet4, tc) 530 { 531 run(HOST_V4MAPPED, PORT_V4MAPPED, TOTAL, false, true); 532 } 533 534 ATF_TC(unconninet6); 535 ATF_TC_HEAD(unconninet6, tc) 536 { 537 atf_tc_set_md_var(tc, "descr", "Checks unconnected multicast for ipv6"); 538 } 539 540 ATF_TC_BODY(unconninet6, tc) 541 { 542 run(HOST_V6, PORT_V6, TOTAL, false, false); 543 } 544 545 ATF_TP_ADD_TCS(tp) 546 { 547 debug++; 548 ATF_TP_ADD_TC(tp, conninet4); 549 ATF_TP_ADD_TC(tp, connmappedinet4); 550 ATF_TP_ADD_TC(tp, connmappedbuginet4); 551 ATF_TP_ADD_TC(tp, conninet6); 552 ATF_TP_ADD_TC(tp, unconninet4); 553 ATF_TP_ADD_TC(tp, unconnmappedinet4); 554 ATF_TP_ADD_TC(tp, unconnmappedbuginet4); 555 ATF_TP_ADD_TC(tp, unconninet6); 556 557 return atf_no_error(); 558 } 559 #endif 560