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