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