1 /* $KAME: rtsold.c,v 1.67 2003/05/17 18:16:15 itojun 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$ 32 */ 33 34 #include <sys/types.h> 35 #include <sys/time.h> 36 #include <sys/socket.h> 37 #include <sys/param.h> 38 39 #include <net/if.h> 40 #include <net/if_dl.h> 41 42 #include <netinet/in.h> 43 #include <netinet/icmp6.h> 44 45 #include <signal.h> 46 #include <unistd.h> 47 #include <syslog.h> 48 #include <string.h> 49 #include <stdlib.h> 50 #include <stdio.h> 51 #include <errno.h> 52 #include <err.h> 53 #include <stdarg.h> 54 #include <ifaddrs.h> 55 #ifdef HAVE_POLL_H 56 #include <poll.h> 57 #endif 58 59 #include "rtsold.h" 60 61 struct ifinfo *iflist; 62 struct timeval tm_max = {0x7fffffff, 0x7fffffff}; 63 static int log_upto = 999; 64 static int fflag = 0; 65 66 int aflag = 0; 67 int dflag = 0; 68 69 char *otherconf_script; 70 71 /* protocol constatns */ 72 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */ 73 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */ 74 #define MAX_RTR_SOLICITATIONS 3 /* times */ 75 76 /* 77 * implementation dependent constants in seconds 78 * XXX: should be configurable 79 */ 80 #define PROBE_INTERVAL 60 81 82 /* utility macros */ 83 /* a < b */ 84 #define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\ 85 (((a).tv_sec == (b).tv_sec) && \ 86 ((a).tv_usec < (b).tv_usec))) 87 88 /* a <= b */ 89 #define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\ 90 (((a).tv_sec == (b).tv_sec) &&\ 91 ((a).tv_usec <= (b).tv_usec))) 92 93 /* a == b */ 94 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec)) 95 96 int main __P((int, char **)); 97 98 /* static variables and functions */ 99 static int mobile_node = 0; 100 static int do_dump; 101 static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */ 102 static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */ 103 104 #if 0 105 static int ifreconfig __P((char *)); 106 #endif 107 static int make_packet __P((struct ifinfo *)); 108 static struct timeval *rtsol_check_timer __P((void)); 109 static void TIMEVAL_ADD __P((struct timeval *, struct timeval *, 110 struct timeval *)); 111 static void TIMEVAL_SUB __P((struct timeval *, struct timeval *, 112 struct timeval *)); 113 114 static void rtsold_set_dump_file __P((int)); 115 static void usage __P((char *)); 116 117 int 118 main(argc, argv) 119 int argc; 120 char **argv; 121 { 122 int s, ch, once = 0; 123 struct timeval *timeout; 124 char *argv0, *opts; 125 #ifdef HAVE_POLL_H 126 struct pollfd set[2]; 127 #else 128 fd_set *fdsetp, *selectfdp; 129 int fdmasks; 130 int maxfd; 131 #endif 132 int rtsock; 133 134 /* 135 * Initialization 136 */ 137 argv0 = argv[0]; 138 139 /* get option */ 140 if (argv0 && argv0[strlen(argv0) - 1] != 'd') { 141 fflag = 1; 142 once = 1; 143 opts = "adDO:"; 144 } else 145 opts = "adDfm1O:"; 146 147 while ((ch = getopt(argc, argv, opts)) != -1) { 148 switch (ch) { 149 case 'a': 150 aflag = 1; 151 break; 152 case 'd': 153 dflag = 1; 154 break; 155 case 'D': 156 dflag = 2; 157 break; 158 case 'f': 159 fflag = 1; 160 break; 161 case 'm': 162 mobile_node = 1; 163 break; 164 case '1': 165 once = 1; 166 break; 167 case 'O': 168 otherconf_script = optarg; 169 break; 170 default: 171 usage(argv0); 172 /*NOTREACHED*/ 173 } 174 } 175 argc -= optind; 176 argv += optind; 177 178 if ((!aflag && argc == 0) || (aflag && 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 189 ident = strrchr(argv0, '/'); 190 if (!ident) 191 ident = argv0; 192 else 193 ident++; 194 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON); 195 if (log_upto >= 0) 196 setlogmask(LOG_UPTO(log_upto)); 197 } 198 199 if (otherconf_script && *otherconf_script != '/') { 200 errx(1, "configuration script (%s) must be an absolute path", 201 otherconf_script); 202 } 203 204 #ifndef HAVE_ARC4RANDOM 205 /* random value initialization */ 206 srandom((u_long)time(NULL)); 207 #endif 208 209 /* warn if accept_rtadv is down */ 210 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV)) 211 warnx("kernel is configured not to accept RAs"); 212 /* warn if forwarding is up */ 213 if (getinet6sysctl(IPV6CTL_FORWARDING)) 214 warnx("kernel is configured as a router, not a host"); 215 216 /* initialization to dump internal status to a file */ 217 signal(SIGUSR1, rtsold_set_dump_file); 218 219 if (!fflag) 220 daemon(0, 0); /* act as a daemon */ 221 222 /* 223 * Open a socket for sending RS and receiving RA. 224 * This should be done before calling ifinit(), since the function 225 * uses the socket. 226 */ 227 if ((s = sockopen()) < 0) { 228 warnmsg(LOG_ERR, __func__, "failed to open a socket"); 229 exit(1); 230 /*NOTREACHED*/ 231 } 232 #ifdef HAVE_POLL_H 233 set[0].fd = s; 234 set[0].events = POLLIN; 235 #else 236 maxfd = s; 237 #endif 238 239 #ifdef HAVE_POLL_H 240 set[1].fd = -1; 241 #endif 242 243 if ((rtsock = rtsock_open()) < 0) { 244 warnmsg(LOG_ERR, __func__, "failed to open a socket"); 245 exit(1); 246 /*NOTREACHED*/ 247 } 248 #ifdef HAVE_POLL_H 249 set[1].fd = rtsock; 250 set[1].events = POLLIN; 251 #else 252 if (rtsock > maxfd) 253 maxfd = rtsock; 254 #endif 255 256 #ifndef HAVE_POLL_H 257 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask); 258 if ((fdsetp = malloc(fdmasks)) == NULL) { 259 err(1, "malloc"); 260 /*NOTREACHED*/ 261 } 262 if ((selectfdp = malloc(fdmasks)) == NULL) { 263 err(1, "malloc"); 264 /*NOTREACHED*/ 265 } 266 #endif 267 268 /* configuration per interface */ 269 if (ifinit()) { 270 warnmsg(LOG_ERR, __func__, 271 "failed to initilizatoin interfaces"); 272 exit(1); 273 /*NOTREACHED*/ 274 } 275 if (aflag) 276 argv = autoifprobe(); 277 while (argv && *argv) { 278 if (ifconfig(*argv)) { 279 warnmsg(LOG_ERR, __func__, 280 "failed to initialize %s", *argv); 281 exit(1); 282 /*NOTREACHED*/ 283 } 284 argv++; 285 } 286 287 /* setup for probing default routers */ 288 if (probe_init()) { 289 warnmsg(LOG_ERR, __func__, 290 "failed to setup for probing routers"); 291 exit(1); 292 /*NOTREACHED*/ 293 } 294 295 /* dump the current pid */ 296 if (!once) { 297 pid_t pid = getpid(); 298 FILE *fp; 299 300 if ((fp = fopen(pidfilename, "w")) == NULL) 301 warnmsg(LOG_ERR, __func__, 302 "failed to open a pid log file(%s): %s", 303 pidfilename, strerror(errno)); 304 else { 305 fprintf(fp, "%d\n", pid); 306 fclose(fp); 307 } 308 } 309 310 #ifndef HAVE_POLL_H 311 memset(fdsetp, 0, fdmasks); 312 FD_SET(s, fdsetp); 313 FD_SET(rtsock, fdsetp); 314 #endif 315 while (1) { /* main loop */ 316 int e; 317 318 #ifndef HAVE_POLL_H 319 memcpy(selectfdp, fdsetp, fdmasks); 320 #endif 321 322 if (do_dump) { /* SIGUSR1 */ 323 do_dump = 0; 324 rtsold_dump_file(dumpfilename); 325 } 326 327 timeout = rtsol_check_timer(); 328 329 if (once) { 330 struct ifinfo *ifi; 331 332 /* if we have no timeout, we are done (or failed) */ 333 if (timeout == NULL) 334 break; 335 336 /* if all interfaces have got RA packet, we are done */ 337 for (ifi = iflist; ifi; ifi = ifi->next) { 338 if (ifi->state != IFS_DOWN && ifi->racnt == 0) 339 break; 340 } 341 if (ifi == NULL) 342 break; 343 } 344 #ifdef HAVE_POLL_H 345 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM); 346 #else 347 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout); 348 #endif 349 if (e < 1) { 350 if (e < 0 && errno != EINTR) { 351 warnmsg(LOG_ERR, __func__, "select: %s", 352 strerror(errno)); 353 } 354 continue; 355 } 356 357 /* packet reception */ 358 #ifdef HAVE_POLL_H 359 if (set[1].revents & POLLIN) 360 #else 361 if (FD_ISSET(rtsock, selectfdp)) 362 #endif 363 rtsock_input(rtsock); 364 #ifdef HAVE_POLL_H 365 if (set[0].revents & POLLIN) 366 #else 367 if (FD_ISSET(s, selectfdp)) 368 #endif 369 rtsol_input(s); 370 } 371 /* NOTREACHED */ 372 373 return 0; 374 } 375 376 int 377 ifconfig(char *ifname) 378 { 379 struct ifinfo *ifinfo; 380 struct sockaddr_dl *sdl; 381 int flags; 382 383 if ((sdl = if_nametosdl(ifname)) == NULL) { 384 warnmsg(LOG_ERR, __func__, 385 "failed to get link layer information for %s", ifname); 386 return(-1); 387 } 388 if (find_ifinfo(sdl->sdl_index)) { 389 warnmsg(LOG_ERR, __func__, 390 "interface %s was already configured", ifname); 391 free(sdl); 392 return(-1); 393 } 394 395 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) { 396 warnmsg(LOG_ERR, __func__, "memory allocation failed"); 397 free(sdl); 398 return(-1); 399 } 400 memset(ifinfo, 0, sizeof(*ifinfo)); 401 ifinfo->sdl = sdl; 402 403 strlcpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname)); 404 405 /* construct a router solicitation message */ 406 if (make_packet(ifinfo)) 407 goto bad; 408 409 /* set link ID of this interface. */ 410 #ifdef HAVE_SCOPELIB 411 if (inet_zoneid(AF_INET6, 2, ifname, &ifinfo->linkid)) 412 goto bad; 413 #else 414 /* XXX: assume interface IDs as link IDs */ 415 ifinfo->linkid = ifinfo->sdl->sdl_index; 416 #endif 417 418 /* 419 * check if the interface is available. 420 * also check if SIOCGIFMEDIA ioctl is OK on the interface. 421 */ 422 ifinfo->mediareqok = 1; 423 ifinfo->active = interface_status(ifinfo); 424 if (!ifinfo->mediareqok) { 425 /* 426 * probe routers periodically even if the link status 427 * does not change. 428 */ 429 ifinfo->probeinterval = PROBE_INTERVAL; 430 } 431 432 /* activate interface: interface_up returns 0 on success */ 433 flags = interface_up(ifinfo->ifname); 434 if (flags == 0) 435 ifinfo->state = IFS_DELAY; 436 else if (flags == IFS_TENTATIVE) 437 ifinfo->state = IFS_TENTATIVE; 438 else 439 ifinfo->state = IFS_DOWN; 440 441 rtsol_timer_update(ifinfo); 442 443 /* link into chain */ 444 if (iflist) 445 ifinfo->next = iflist; 446 iflist = ifinfo; 447 448 return(0); 449 450 bad: 451 free(ifinfo->sdl); 452 free(ifinfo); 453 return(-1); 454 } 455 456 void 457 iflist_init() 458 { 459 struct ifinfo *ifi, *next; 460 461 for (ifi = iflist; ifi; ifi = next) { 462 next = ifi->next; 463 if (ifi->sdl) 464 free(ifi->sdl); 465 if (ifi->rs_data) 466 free(ifi->rs_data); 467 free(ifi); 468 iflist = NULL; 469 } 470 } 471 472 #if 0 473 static int 474 ifreconfig(char *ifname) 475 { 476 struct ifinfo *ifi, *prev; 477 int rv; 478 479 prev = NULL; 480 for (ifi = iflist; ifi; ifi = ifi->next) { 481 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0) 482 break; 483 prev = ifi; 484 } 485 prev->next = ifi->next; 486 487 rv = ifconfig(ifname); 488 489 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */ 490 if (ifi->rs_data) 491 free(ifi->rs_data); 492 free(ifi->sdl); 493 free(ifi); 494 return rv; 495 } 496 #endif 497 498 struct ifinfo * 499 find_ifinfo(int ifindex) 500 { 501 struct ifinfo *ifi; 502 503 for (ifi = iflist; ifi; ifi = ifi->next) 504 if (ifi->sdl->sdl_index == ifindex) 505 return(ifi); 506 return(NULL); 507 } 508 509 static int 510 make_packet(struct ifinfo *ifinfo) 511 { 512 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0; 513 struct nd_router_solicit *rs; 514 char *buf; 515 516 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) { 517 warnmsg(LOG_INFO, __func__, 518 "link-layer address option has null length" 519 " on %s. Treat as not included.", ifinfo->ifname); 520 } 521 packlen += lladdroptlen; 522 ifinfo->rs_datalen = packlen; 523 524 /* allocate buffer */ 525 if ((buf = malloc(packlen)) == NULL) { 526 warnmsg(LOG_ERR, __func__, 527 "memory allocation failed for %s", ifinfo->ifname); 528 return(-1); 529 } 530 ifinfo->rs_data = buf; 531 532 /* fill in the message */ 533 rs = (struct nd_router_solicit *)buf; 534 rs->nd_rs_type = ND_ROUTER_SOLICIT; 535 rs->nd_rs_code = 0; 536 rs->nd_rs_cksum = 0; 537 rs->nd_rs_reserved = 0; 538 buf += sizeof(*rs); 539 540 /* fill in source link-layer address option */ 541 if (lladdroptlen) 542 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf); 543 544 return(0); 545 } 546 547 static struct timeval * 548 rtsol_check_timer() 549 { 550 static struct timeval returnval; 551 struct timeval now, rtsol_timer; 552 struct ifinfo *ifinfo; 553 int flags; 554 555 gettimeofday(&now, NULL); 556 557 rtsol_timer = tm_max; 558 559 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) { 560 if (TIMEVAL_LEQ(ifinfo->expire, now)) { 561 if (dflag > 1) 562 warnmsg(LOG_DEBUG, __func__, 563 "timer expiration on %s, " 564 "state = %d", ifinfo->ifname, 565 ifinfo->state); 566 567 switch (ifinfo->state) { 568 case IFS_DOWN: 569 case IFS_TENTATIVE: 570 /* interface_up returns 0 on success */ 571 flags = interface_up(ifinfo->ifname); 572 if (flags == 0) 573 ifinfo->state = IFS_DELAY; 574 else if (flags == IFS_TENTATIVE) 575 ifinfo->state = IFS_TENTATIVE; 576 else 577 ifinfo->state = IFS_DOWN; 578 break; 579 case IFS_IDLE: 580 { 581 int oldstatus = ifinfo->active; 582 int probe = 0; 583 584 ifinfo->active = interface_status(ifinfo); 585 586 if (oldstatus != ifinfo->active) { 587 warnmsg(LOG_DEBUG, __func__, 588 "%s status is changed" 589 " from %d to %d", 590 ifinfo->ifname, 591 oldstatus, ifinfo->active); 592 probe = 1; 593 ifinfo->state = IFS_DELAY; 594 } else if (ifinfo->probeinterval && 595 (ifinfo->probetimer -= 596 ifinfo->timer.tv_sec) <= 0) { 597 /* probe timer expired */ 598 ifinfo->probetimer = 599 ifinfo->probeinterval; 600 probe = 1; 601 ifinfo->state = IFS_PROBE; 602 } 603 604 /* 605 * If we need a probe, clear the previous 606 * status wrt the "other" configuration. 607 */ 608 if (probe) 609 ifinfo->otherconfig = 0; 610 611 if (probe && mobile_node) 612 defrouter_probe(ifinfo); 613 break; 614 } 615 case IFS_DELAY: 616 ifinfo->state = IFS_PROBE; 617 sendpacket(ifinfo); 618 break; 619 case IFS_PROBE: 620 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 621 sendpacket(ifinfo); 622 else { 623 warnmsg(LOG_INFO, __func__, 624 "No answer after sending %d RSs", 625 ifinfo->probes); 626 ifinfo->probes = 0; 627 ifinfo->state = IFS_IDLE; 628 } 629 break; 630 } 631 rtsol_timer_update(ifinfo); 632 } 633 634 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer)) 635 rtsol_timer = ifinfo->expire; 636 } 637 638 if (TIMEVAL_EQ(rtsol_timer, tm_max)) { 639 warnmsg(LOG_DEBUG, __func__, "there is no timer"); 640 return(NULL); 641 } else if (TIMEVAL_LT(rtsol_timer, now)) 642 /* this may occur when the interval is too small */ 643 returnval.tv_sec = returnval.tv_usec = 0; 644 else 645 TIMEVAL_SUB(&rtsol_timer, &now, &returnval); 646 647 if (dflag > 1) 648 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld", 649 (long)returnval.tv_sec, (long)returnval.tv_usec); 650 651 return(&returnval); 652 } 653 654 void 655 rtsol_timer_update(struct ifinfo *ifinfo) 656 { 657 #define MILLION 1000000 658 #define DADRETRY 10 /* XXX: adhoc */ 659 long interval; 660 struct timeval now; 661 662 bzero(&ifinfo->timer, sizeof(ifinfo->timer)); 663 664 switch (ifinfo->state) { 665 case IFS_DOWN: 666 case IFS_TENTATIVE: 667 if (++ifinfo->dadcount > DADRETRY) { 668 ifinfo->dadcount = 0; 669 ifinfo->timer.tv_sec = PROBE_INTERVAL; 670 } else 671 ifinfo->timer.tv_sec = 1; 672 break; 673 case IFS_IDLE: 674 if (mobile_node) { 675 /* XXX should be configurable */ 676 ifinfo->timer.tv_sec = 3; 677 } 678 else 679 ifinfo->timer = tm_max; /* stop timer(valid?) */ 680 break; 681 case IFS_DELAY: 682 #ifndef HAVE_ARC4RANDOM 683 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 684 #else 685 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION); 686 #endif 687 ifinfo->timer.tv_sec = interval / MILLION; 688 ifinfo->timer.tv_usec = interval % MILLION; 689 break; 690 case IFS_PROBE: 691 if (ifinfo->probes < MAX_RTR_SOLICITATIONS) 692 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL; 693 else { 694 /* 695 * After sending MAX_RTR_SOLICITATIONS solicitations, 696 * we're just waiting for possible replies; there 697 * will be no more solicatation. Thus, we change 698 * the timer value to MAX_RTR_SOLICITATION_DELAY based 699 * on RFC 2461, Section 6.3.7. 700 */ 701 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY; 702 } 703 break; 704 default: 705 warnmsg(LOG_ERR, __func__, 706 "illegal interface state(%d) on %s", 707 ifinfo->state, ifinfo->ifname); 708 return; 709 } 710 711 /* reset the timer */ 712 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) { 713 ifinfo->expire = tm_max; 714 warnmsg(LOG_DEBUG, __func__, 715 "stop timer for %s", ifinfo->ifname); 716 } else { 717 gettimeofday(&now, NULL); 718 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire); 719 720 if (dflag > 1) 721 warnmsg(LOG_DEBUG, __func__, 722 "set timer for %s to %d:%d", ifinfo->ifname, 723 (int)ifinfo->timer.tv_sec, 724 (int)ifinfo->timer.tv_usec); 725 } 726 727 #undef MILLION 728 } 729 730 /* timer related utility functions */ 731 #define MILLION 1000000 732 733 /* result = a + b */ 734 static void 735 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result) 736 { 737 long l; 738 739 if ((l = a->tv_usec + b->tv_usec) < MILLION) { 740 result->tv_usec = l; 741 result->tv_sec = a->tv_sec + b->tv_sec; 742 } else { 743 result->tv_usec = l - MILLION; 744 result->tv_sec = a->tv_sec + b->tv_sec + 1; 745 } 746 } 747 748 /* 749 * result = a - b 750 * XXX: this function assumes that a >= b. 751 */ 752 void 753 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result) 754 { 755 long l; 756 757 if ((l = a->tv_usec - b->tv_usec) >= 0) { 758 result->tv_usec = l; 759 result->tv_sec = a->tv_sec - b->tv_sec; 760 } else { 761 result->tv_usec = MILLION + l; 762 result->tv_sec = a->tv_sec - b->tv_sec - 1; 763 } 764 } 765 766 static void 767 rtsold_set_dump_file(sig) 768 int sig; 769 { 770 do_dump = 1; 771 } 772 773 static void 774 usage(char *progname) 775 { 776 if (progname && progname[strlen(progname) - 1] != 'd') { 777 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n"); 778 fprintf(stderr, "usage: rtsol [-dD] -a\n"); 779 } else { 780 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n"); 781 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n"); 782 } 783 exit(1); 784 } 785 786 void 787 #if __STDC__ 788 warnmsg(int priority, const char *func, const char *msg, ...) 789 #else 790 warnmsg(priority, func, msg, va_alist) 791 int priority; 792 const char *func; 793 const char *msg; 794 va_dcl 795 #endif 796 { 797 va_list ap; 798 char buf[BUFSIZ]; 799 800 va_start(ap, msg); 801 if (fflag) { 802 if (priority <= log_upto) { 803 (void)vfprintf(stderr, msg, ap); 804 (void)fprintf(stderr, "\n"); 805 } 806 } else { 807 snprintf(buf, sizeof(buf), "<%s> %s", func, msg); 808 msg = buf; 809 vsyslog(priority, msg, ap); 810 } 811 va_end(ap); 812 } 813 814 /* 815 * return a list of interfaces which is suitable to sending an RS. 816 */ 817 char ** 818 autoifprobe() 819 { 820 static char **argv = NULL; 821 static int n = 0; 822 char **a; 823 int i, found; 824 struct ifaddrs *ifap, *ifa, *target; 825 826 /* initialize */ 827 while (n--) 828 free(argv[n]); 829 if (argv) { 830 free(argv); 831 argv = NULL; 832 } 833 n = 0; 834 835 if (getifaddrs(&ifap) != 0) 836 return NULL; 837 838 target = NULL; 839 /* find an ethernet */ 840 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 841 if ((ifa->ifa_flags & IFF_UP) == 0) 842 continue; 843 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) 844 continue; 845 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) 846 continue; 847 if ((ifa->ifa_flags & IFF_MULTICAST) == 0) 848 continue; 849 850 if (ifa->ifa_addr->sa_family != AF_INET6) 851 continue; 852 853 found = 0; 854 for (i = 0; i < n; i++) { 855 if (strcmp(argv[i], ifa->ifa_name) == 0) { 856 found++; 857 break; 858 } 859 } 860 if (found) 861 continue; 862 863 /* if we find multiple candidates, just warn. */ 864 if (n != 0 && dflag > 1) 865 warnx("multiple interfaces found"); 866 867 a = (char **)realloc(argv, (n + 1) * sizeof(char **)); 868 if (a == NULL) 869 err(1, "realloc"); 870 argv = a; 871 argv[n] = strdup(ifa->ifa_name); 872 if (!argv[n]) 873 err(1, "malloc"); 874 n++; 875 argv[n] = NULL; 876 } 877 878 if (n) { 879 a = (char **)realloc(argv, (n + 1) * sizeof(char **)); 880 if (a == NULL) 881 err(1, "realloc"); 882 argv = a; 883 argv[n] = NULL; 884 885 if (dflag > 0) { 886 for (i = 0; i < n; i++) 887 warnx("probing %s", argv[i]); 888 } 889 } 890 freeifaddrs(ifap); 891 return argv; 892 } 893