1 /* $KAME: rtsold.c,v 1.31 2001/05/22 06:03:06 jinmei Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD: src/usr.sbin/rtsold/rtsold.c,v 1.1.2.4 2002/04/04 11:07:19 ume Exp $ 32 */ 33 34 #include <sys/types.h> 35 #include <sys/time.h> 36 #include <sys/socket.h> 37 38 #include <net/if.h> 39 #include <net/if_dl.h> 40 41 #include <netinet/in.h> 42 #include <netinet/icmp6.h> 43 44 #include <signal.h> 45 #include <unistd.h> 46 #include <syslog.h> 47 #include <string.h> 48 #include <stdlib.h> 49 #include <stdio.h> 50 #include <errno.h> 51 #include <err.h> 52 #include <stdarg.h> 53 #include <ifaddrs.h> 54 #include "rtsold.h" 55 56 struct ifinfo *iflist; 57 struct timeval tm_max = {0x7fffffff, 0x7fffffff}; 58 int aflag = 0; 59 int dflag = 0; 60 static int log_upto = 999; 61 static int fflag = 0; 62 63 /* protocol constatns */ 64 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */ 65 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */ 66 #define MAX_RTR_SOLICITATIONS 3 /* times */ 67 68 /* implementation dependent constants */ 69 #define PROBE_INTERVAL 60 /* secondes XXX: should be configurable */ 70 71 /* utility macros */ 72 /* a < b */ 73 #define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\ 74 (((a).tv_sec == (b).tv_sec) && \ 75 ((a).tv_usec < (b).tv_usec))) 76 77 /* a <= b */ 78 #define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\ 79 (((a).tv_sec == (b).tv_sec) &&\ 80 ((a).tv_usec <= (b).tv_usec))) 81 82 /* a == b */ 83 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec)) 84 85 /* static variables and functions */ 86 static int mobile_node = 0; 87 static int do_dump; 88 /* 89 * XXX: the following two values should be configurable 90 */ 91 static const char *dumpfilename = "/var/run/rtsold.dump"; 92 static const char *pidfilename = "/var/run/rtsold.pid"; 93 94 static int ifconfig(char *ifname); 95 #if 0 96 static int ifreconfig(char *ifname); 97 #endif 98 static int make_packet(struct ifinfo *ifinfo); 99 static struct timeval *rtsol_check_timer(void); 100 static void TIMEVAL_ADD(struct timeval *a, struct timeval *b, 101 struct timeval *result); 102 static void TIMEVAL_SUB(struct timeval *a, struct timeval *b, 103 struct timeval *result); 104 105 static void rtsold_set_dump_file(int); 106 static void usage(char *progname); 107 static char **autoifprobe(void); 108 109 int 110 main(int argc, char *argv[]) 111 { 112 int s, rtsock, maxfd, ch; 113 int once = 0; 114 struct timeval *timeout; 115 struct fd_set fdset; 116 char *argv0; 117 const char *opts; 118 119 /* 120 * Initialization 121 */ 122 argv0 = argv[0]; 123 124 /* get option */ 125 if (argv0 && argv0[strlen(argv0) - 1] != 'd') { 126 fflag = 1; 127 once = 1; 128 opts = "adD"; 129 } else 130 opts = "adDfm1"; 131 132 while ((ch = getopt(argc, argv, opts)) != -1) { 133 switch (ch) { 134 case 'a': 135 aflag = 1; 136 break; 137 case 'd': 138 dflag = 1; 139 break; 140 case 'D': 141 dflag = 2; 142 break; 143 case 'f': 144 fflag = 1; 145 break; 146 case 'm': 147 mobile_node = 1; 148 break; 149 case '1': 150 once = 1; 151 break; 152 default: 153 usage(argv0); 154 /*NOTREACHED*/ 155 } 156 } 157 argc -= optind; 158 argv += optind; 159 160 if (aflag) { 161 int i; 162 163 if (argc != 0) { 164 usage(argv0); 165 /*NOTREACHED*/ 166 } 167 168 argv = autoifprobe(); 169 if (!argv) { 170 errx(1, "could not autoprobe interface"); 171 /*NOTREACHED*/ 172 } 173 174 for (i = 0; argv[i]; i++) 175 ; 176 argc = i; 177 } 178 if (argc == 0) { 179 usage(argv0); 180 /*NOTREACHED*/ 181 } 182 183 /* set log level */ 184 if (dflag == 0) 185 log_upto = LOG_NOTICE; 186 if (!fflag) { 187 char *ident; 188 ident = strrchr(argv0, '/'); 189 if (!ident) 190 ident = argv0; 191 else 192 ident++; 193 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON); 194 if (log_upto >= 0) 195 setlogmask(LOG_UPTO(log_upto)); 196 } 197 198 #ifndef HAVE_ARC4RANDOM 199 /* random value initilization */ 200 srandom((u_long)time(NULL)); 201 #endif 202 203 /* warn if accept_rtadv is down */ 204 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV)) 205 warnx("kernel is configured not to accept RAs"); 206 /* warn if forwarding is up */ 207 if (getinet6sysctl(IPV6CTL_FORWARDING)) 208 warnx("kernel is configured as a router, not a host"); 209 210 /* initialization to dump internal status to a file */ 211 if (signal(SIGUSR1, rtsold_set_dump_file) == SIG_ERR) { 212 errx(1, "failed to set signal for dump status"); 213 /*NOTREACHED*/ 214 } 215 216 /* 217 * Open a socket for sending RS and receiving RA. 218 * This should be done before calling ifinit(), since the function 219 * uses the socket. 220 */ 221 if ((s = sockopen()) < 0) { 222 errx(1, "failed to open a socket"); 223 /*NOTREACHED*/ 224 } 225 maxfd = s; 226 if ((rtsock = rtsock_open()) < 0) { 227 errx(1, "failed to open a socket"); 228 /*NOTREACHED*/ 229 } 230 if (rtsock > maxfd) 231 maxfd = rtsock; 232 233 /* configuration per interface */ 234 if (ifinit()) { 235 errx(1, "failed to initilizatoin interfaces"); 236 /*NOTREACHED*/ 237 } 238 while (argc--) { 239 if (ifconfig(*argv)) { 240 errx(1, "failed to initialize %s", *argv); 241 /*NOTREACHED*/ 242 } 243 argv++; 244 } 245 246 /* setup for probing default routers */ 247 if (probe_init()) { 248 errx(1, "failed to setup for probing routers"); 249 /*NOTREACHED*/ 250 } 251 252 if (!fflag) 253 daemon(0, 0); /* act as a daemon */ 254 255 /* dump the current pid */ 256 if (!once) { 257 pid_t pid = getpid(); 258 FILE *fp; 259 260 if ((fp = fopen(pidfilename, "w")) == NULL) 261 warnmsg(LOG_ERR, __func__, 262 "failed to open a log file(%s): %s", 263 pidfilename, strerror(errno)); 264 else { 265 fprintf(fp, "%d\n", pid); 266 fclose(fp); 267 } 268 } 269 270 FD_ZERO(&fdset); 271 FD_SET(s, &fdset); 272 FD_SET(rtsock, &fdset); 273 while (1) { /* main loop */ 274 int e; 275 struct fd_set select_fd = fdset; 276 277 if (do_dump) { /* SIGUSR1 */ 278 do_dump = 0; 279 rtsold_dump_file(dumpfilename); 280 } 281 282 timeout = rtsol_check_timer(); 283 284 if (once) { 285 struct ifinfo *ifi; 286 287 /* if we have no timeout, we are done (or failed) */ 288 if (timeout == NULL) 289 break; 290 291 /* if all interfaces have got RA packet, we are done */ 292 for (ifi = iflist; ifi; ifi = ifi->next) { 293 if (ifi->state != IFS_DOWN && ifi->racnt == 0) 294 break; 295 } 296 if (ifi == NULL) 297 break; 298 } 299 e = select(maxfd + 1, &select_fd, NULL, NULL, timeout); 300 if (e < 1) { 301 if (e < 0 && errno != EINTR) { 302 warnmsg(LOG_ERR, __func__, "select: %s", 303 strerror(errno)); 304 } 305 continue; 306 } 307 308 /* packet reception */ 309 if (FD_ISSET(rtsock, &select_fd)) 310 rtsock_input(rtsock); 311 if (FD_ISSET(s, &select_fd)) 312 rtsol_input(s); 313 } 314 /* NOTREACHED */ 315 316 return 0; 317 } 318 319 static int 320 ifconfig(char *ifname) 321 { 322 struct ifinfo *ifinfo; 323 struct sockaddr_dl *sdl; 324 int flags; 325 326 if ((sdl = if_nametosdl(ifname)) == NULL) { 327 warnmsg(LOG_ERR, __func__, 328 "failed to get link layer information for %s", ifname); 329 return(-1); 330 } 331 if (find_ifinfo(sdl->sdl_index)) { 332 warnmsg(LOG_ERR, __func__, 333 "interface %s was already configured", ifname); 334 free(sdl); 335 return(-1); 336 } 337 338 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) { 339 warnmsg(LOG_ERR, __func__, "memory allocation failed"); 340 free(sdl); 341 return(-1); 342 } 343 memset(ifinfo, 0, sizeof(*ifinfo)); 344 ifinfo->sdl = sdl; 345 346 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname)); 347 348 /* construct a router solicitation message */ 349 if (make_packet(ifinfo)) 350 goto bad; 351 352 /* 353 * check if the interface is available. 354 * also check if SIOCGIFMEDIA ioctl is OK on the interface. 355 */ 356 ifinfo->mediareqok = 1; 357 ifinfo->active = interface_status(ifinfo); 358 if (!ifinfo->mediareqok) { 359 /* 360 * probe routers periodically even if the link status 361 * does not change. 362 */ 363 ifinfo->probeinterval = PROBE_INTERVAL; 364 } 365 366 /* activate interface: interface_up returns 0 on success */ 367 flags = interface_up(ifinfo->ifname); 368 if (flags == 0) 369 ifinfo->state = IFS_DELAY; 370 else if (flags == IFS_TENTATIVE) 371 ifinfo->state = IFS_TENTATIVE; 372 else 373 ifinfo->state = IFS_DOWN; 374 375 rtsol_timer_update(ifinfo); 376 377 /* link into chain */ 378 if (iflist) 379 ifinfo->next = iflist; 380 iflist = ifinfo; 381 382 return(0); 383 384 bad: 385 free(ifinfo->sdl); 386 free(ifinfo); 387 return(-1); 388 } 389 390 #if 0 391 static int 392 ifreconfig(char *ifname) 393 { 394 struct ifinfo *ifi, *prev; 395 int rv; 396 397 prev = NULL; 398 for (ifi = iflist; ifi; ifi = ifi->next) { 399 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0) 400 break; 401 prev = ifi; 402 } 403 prev->next = ifi->next; 404 405 rv = ifconfig(ifname); 406 407 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */ 408 if (ifi->rs_data) 409 free(ifi->rs_data); 410 free(ifi->sdl); 411 free(ifi); 412 413 return rv; 414 } 415 #endif 416 417 struct ifinfo * 418 find_ifinfo(int ifindex) 419 { 420 struct ifinfo *ifi; 421 422 for (ifi = iflist; ifi; ifi = ifi->next) 423 if (ifi->sdl->sdl_index == ifindex) 424 return(ifi); 425 426 return(NULL); 427 } 428 429 static int 430 make_packet(struct ifinfo *ifinfo) 431 { 432 char *buf; 433 struct nd_router_solicit *rs; 434 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0; 435 436 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) { 437 warnmsg(LOG_INFO, __func__, 438 "link-layer address option has null length" 439 " on %s. Treat as not included.", ifinfo->ifname); 440 } 441 packlen += lladdroptlen; 442 ifinfo->rs_datalen = packlen; 443 444 /* allocate buffer */ 445 if ((buf = malloc(packlen)) == NULL) { 446 warnmsg(LOG_ERR, __func__, 447 "memory allocation failed for %s", ifinfo->ifname); 448 return(-1); 449 } 450 ifinfo->rs_data = buf; 451 452 /* fill in the message */ 453 rs = (struct nd_router_solicit *)buf; 454 rs->nd_rs_type = ND_ROUTER_SOLICIT; 455 rs->nd_rs_code = 0; 456 rs->nd_rs_cksum = 0; 457 rs->nd_rs_reserved = 0; 458 buf += sizeof(*rs); 459 460 /* fill in source link-layer address option */ 461 if (lladdroptlen) 462 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf); 463 464 return(0); 465 } 466 467 static struct timeval * 468 rtsol_check_timer(void) 469 { 470 static struct timeval returnval; 471 struct timeval now, rtsol_timer; 472 struct ifinfo *ifinfo; 473 int flags; 474 475 gettimeofday(&now, NULL); 476 477 rtsol_timer = tm_max; 478 479 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) { 480 if (TIMEVAL_LEQ(ifinfo->expire, now)) { 481 if (dflag > 1) 482 warnmsg(LOG_DEBUG, __func__, 483 "timer expiration on %s, " 484 "state = %d", ifinfo->ifname, 485 ifinfo->state); 486 487 switch (ifinfo->state) { 488 case IFS_DOWN: 489 case IFS_TENTATIVE: 490 /* interface_up returns 0 on success */ 491 flags = interface_up(ifinfo->ifname); 492 if (flags == 0) 493 ifinfo->state = IFS_DELAY; 494 else if (flags == IFS_TENTATIVE) 495 ifinfo->state = IFS_TENTATIVE; 496 else 497 ifinfo->state = IFS_DOWN; 498 break; 499 case IFS_IDLE: 500 { 501 int oldstatus = ifinfo->active; 502 int probe = 0; 503 504 ifinfo->active = 505 interface_status(ifinfo); 506 507 if (oldstatus != ifinfo->active) { 508 warnmsg(LOG_DEBUG, __func__, 509 "%s status is changed" 510 " from %d to %d", 511 ifinfo->ifname, 512 oldstatus, ifinfo->active); 513 probe = 1; 514 ifinfo->state = IFS_DELAY; 515 } 516 else if (ifinfo->probeinterval && 517 (ifinfo->probetimer -= 518 ifinfo->timer.tv_sec) <= 0) { 519 /* probe timer expired */ 520 ifinfo->probetimer = 521 ifinfo->probeinterval; 522 probe = 1; 523 ifinfo->state = IFS_PROBE; 524 } 525 526 if (probe && mobile_node) 527 defrouter_probe(ifinfo->sdl->sdl_index); 528 break; 529 } 530 case IFS_DELAY: 531 ifinfo->state = IFS_PROBE; 532 sendpacket(ifinfo); 533 break; 534 case IFS_PROBE: 535 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 536 sendpacket(ifinfo); 537 else { 538 warnmsg(LOG_INFO, __func__, 539 "No answer " 540 "after sending %d RSs", 541 ifinfo->probes); 542 ifinfo->probes = 0; 543 ifinfo->state = IFS_IDLE; 544 } 545 break; 546 } 547 rtsol_timer_update(ifinfo); 548 } 549 550 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer)) 551 rtsol_timer = ifinfo->expire; 552 } 553 554 if (TIMEVAL_EQ(rtsol_timer, tm_max)) { 555 warnmsg(LOG_DEBUG, __func__, "there is no timer"); 556 return(NULL); 557 } 558 else if (TIMEVAL_LT(rtsol_timer, now)) 559 /* this may occur when the interval is too small */ 560 returnval.tv_sec = returnval.tv_usec = 0; 561 else 562 TIMEVAL_SUB(&rtsol_timer, &now, &returnval); 563 564 if (dflag > 1) 565 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld", 566 (long)returnval.tv_sec, (long)returnval.tv_usec); 567 568 return(&returnval); 569 } 570 571 void 572 rtsol_timer_update(struct ifinfo *ifinfo) 573 { 574 #define MILLION 1000000 575 #define DADRETRY 10 /* XXX: adhoc */ 576 long interval; 577 struct timeval now; 578 579 bzero(&ifinfo->timer, sizeof(ifinfo->timer)); 580 581 switch (ifinfo->state) { 582 case IFS_DOWN: 583 case IFS_TENTATIVE: 584 if (++ifinfo->dadcount > DADRETRY) { 585 ifinfo->dadcount = 0; 586 ifinfo->timer.tv_sec = PROBE_INTERVAL; 587 } 588 else 589 ifinfo->timer.tv_sec = 1; 590 break; 591 case IFS_IDLE: 592 if (mobile_node) { 593 /* XXX should be configurable */ 594 ifinfo->timer.tv_sec = 3; 595 } 596 else 597 ifinfo->timer = tm_max; /* stop timer(valid?) */ 598 break; 599 case IFS_DELAY: 600 #ifndef HAVE_ARC4RANDOM 601 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 602 #else 603 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 604 #endif 605 ifinfo->timer.tv_sec = interval / MILLION; 606 ifinfo->timer.tv_usec = interval % MILLION; 607 break; 608 case IFS_PROBE: 609 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 610 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL; 611 else { 612 /* 613 * After sending MAX_RTR_SOLICITATIONS solicitations, 614 * we're just waiting for possible replies; there 615 * will be no more solicatation. Thus, we change 616 * the timer value to MAX_RTR_SOLICITATION_DELAY based 617 * on RFC 2461, Section 6.3.7. 618 */ 619 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY; 620 } 621 break; 622 default: 623 warnmsg(LOG_ERR, __func__, 624 "illegal interface state(%d) on %s", 625 ifinfo->state, ifinfo->ifname); 626 return; 627 } 628 629 /* reset the timer */ 630 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) { 631 ifinfo->expire = tm_max; 632 warnmsg(LOG_DEBUG, __func__, 633 "stop timer for %s", ifinfo->ifname); 634 } 635 else { 636 gettimeofday(&now, NULL); 637 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire); 638 639 if (dflag > 1) 640 warnmsg(LOG_DEBUG, __func__, 641 "set timer for %s to %d:%d", ifinfo->ifname, 642 (int)ifinfo->timer.tv_sec, 643 (int)ifinfo->timer.tv_usec); 644 } 645 646 #undef MILLION 647 } 648 649 /* timer related utility functions */ 650 #define MILLION 1000000 651 652 /* result = a + b */ 653 static void 654 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result) 655 { 656 long l; 657 658 if ((l = a->tv_usec + b->tv_usec) < MILLION) { 659 result->tv_usec = l; 660 result->tv_sec = a->tv_sec + b->tv_sec; 661 } 662 else { 663 result->tv_usec = l - MILLION; 664 result->tv_sec = a->tv_sec + b->tv_sec + 1; 665 } 666 } 667 668 /* 669 * result = a - b 670 * XXX: this function assumes that a >= b. 671 */ 672 void 673 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result) 674 { 675 long l; 676 677 if ((l = a->tv_usec - b->tv_usec) >= 0) { 678 result->tv_usec = l; 679 result->tv_sec = a->tv_sec - b->tv_sec; 680 } 681 else { 682 result->tv_usec = MILLION + l; 683 result->tv_sec = a->tv_sec - b->tv_sec - 1; 684 } 685 } 686 687 static void 688 rtsold_set_dump_file(int signo __unused) 689 { 690 do_dump = 1; 691 } 692 693 static void 694 usage(char *progname) 695 { 696 if (progname && progname[strlen(progname) - 1] != 'd') { 697 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n"); 698 fprintf(stderr, "usage: rtsol [-dD] -a\n"); 699 } else { 700 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n"); 701 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n"); 702 } 703 exit(1); 704 } 705 706 void 707 warnmsg(int priority, const char *func, const char *msg, ...) 708 { 709 va_list ap; 710 char buf[BUFSIZ]; 711 712 va_start(ap, msg); 713 if (fflag) { 714 if (priority <= log_upto) { 715 vfprintf(stderr, msg, ap); 716 fprintf(stderr, "\n"); 717 } 718 } else { 719 snprintf(buf, sizeof(buf), "<%s> %s", func, msg); 720 msg = buf; 721 vsyslog(priority, msg, ap); 722 } 723 va_end(ap); 724 } 725 726 static char ** 727 autoifprobe(void) 728 { 729 #ifndef HAVE_GETIFADDRS 730 errx(1, "-a is not available with the configuration"); 731 #else 732 static char ifname[IFNAMSIZ + 1]; 733 static char *argv[2]; 734 struct ifaddrs *ifap, *ifa, *target; 735 736 if (getifaddrs(&ifap) != 0) 737 return NULL; 738 739 target = NULL; 740 /* find an ethernet */ 741 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 742 if ((ifa->ifa_flags & IFF_UP) == 0) 743 continue; 744 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) 745 continue; 746 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 747 continue; 748 if ((ifa->ifa_flags & IFF_MULTICAST) == 0) 749 continue; 750 751 if (ifa->ifa_addr->sa_family != AF_INET6) 752 continue; 753 754 if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0) 755 continue; 756 757 if (!target) 758 target = ifa; 759 else { 760 /* if we find multiple candidates, failure. */ 761 if (dflag > 1) 762 warnx("multiple interfaces found"); 763 target = NULL; 764 break; 765 } 766 } 767 768 if (target) { 769 strncpy(ifname, target->ifa_name, sizeof(ifname) - 1); 770 ifname[sizeof(ifname) - 1] = '\0'; 771 argv[0] = ifname; 772 argv[1] = NULL; 773 774 if (dflag > 0) 775 warnx("probing %s", argv[0]); 776 } 777 freeifaddrs(ifap); 778 if (target) 779 return argv; 780 else 781 return NULL; 782 #endif 783 } 784