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