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