1 /* $FreeBSD$ */ 2 /* $KAME: rtadvd.c,v 1.82 2003/08/05 12:34:23 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the project nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/ioctl.h> 36 #include <sys/socket.h> 37 #include <sys/uio.h> 38 #include <sys/time.h> 39 #include <sys/queue.h> 40 #include <sys/stat.h> 41 #include <sys/sysctl.h> 42 43 #include <net/if.h> 44 #include <net/if_types.h> 45 #include <net/if_media.h> 46 #include <net/if_dl.h> 47 #include <net/route.h> 48 #include <netinet/in.h> 49 #include <netinet/ip6.h> 50 #include <netinet6/ip6_var.h> 51 #include <netinet/icmp6.h> 52 53 #include <arpa/inet.h> 54 55 #include <net/if_var.h> 56 #include <netinet/in_var.h> 57 #include <netinet6/nd6.h> 58 59 #include <time.h> 60 #include <unistd.h> 61 #include <stdio.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <inttypes.h> 65 #include <libutil.h> 66 #include <netdb.h> 67 #include <signal.h> 68 #include <string.h> 69 #include <stdlib.h> 70 #include <syslog.h> 71 #include <poll.h> 72 73 #include "pathnames.h" 74 #include "rtadvd.h" 75 #include "if.h" 76 #include "rrenum.h" 77 #include "advcap.h" 78 #include "timer_subr.h" 79 #include "timer.h" 80 #include "config.h" 81 #include "control.h" 82 #include "control_server.h" 83 84 #define RTADV_TYPE2BITMASK(type) (0x1 << type) 85 86 struct msghdr rcvmhdr; 87 static char *rcvcmsgbuf; 88 static size_t rcvcmsgbuflen; 89 static char *sndcmsgbuf = NULL; 90 static size_t sndcmsgbuflen; 91 struct msghdr sndmhdr; 92 struct iovec rcviov[2]; 93 struct iovec sndiov[2]; 94 struct sockaddr_in6 rcvfrom; 95 static const char *pidfilename = _PATH_RTADVDPID; 96 const char *conffile = _PATH_RTADVDCONF; 97 static struct pidfh *pfh; 98 int dflag = 0, sflag = 0; 99 static int wait_shutdown; 100 101 #define PFD_RAWSOCK 0 102 #define PFD_RTSOCK 1 103 #define PFD_CSOCK 2 104 #define PFD_MAX 3 105 106 struct railist_head_t railist = 107 TAILQ_HEAD_INITIALIZER(railist); 108 struct ifilist_head_t ifilist = 109 TAILQ_HEAD_INITIALIZER(ifilist); 110 111 struct nd_optlist { 112 TAILQ_ENTRY(nd_optlist) nol_next; 113 struct nd_opt_hdr *nol_opt; 114 }; 115 union nd_opt { 116 struct nd_opt_hdr *opt_array[9]; 117 struct { 118 struct nd_opt_hdr *zero; 119 struct nd_opt_hdr *src_lladdr; 120 struct nd_opt_hdr *tgt_lladdr; 121 struct nd_opt_prefix_info *pi; 122 struct nd_opt_rd_hdr *rh; 123 struct nd_opt_mtu *mtu; 124 TAILQ_HEAD(, nd_optlist) opt_list; 125 } nd_opt_each; 126 }; 127 #define opt_src_lladdr nd_opt_each.src_lladdr 128 #define opt_tgt_lladdr nd_opt_each.tgt_lladdr 129 #define opt_pi nd_opt_each.pi 130 #define opt_rh nd_opt_each.rh 131 #define opt_mtu nd_opt_each.mtu 132 #define opt_list nd_opt_each.opt_list 133 134 #define NDOPT_FLAG_SRCLINKADDR (1 << 0) 135 #define NDOPT_FLAG_TGTLINKADDR (1 << 1) 136 #define NDOPT_FLAG_PREFIXINFO (1 << 2) 137 #define NDOPT_FLAG_RDHDR (1 << 3) 138 #define NDOPT_FLAG_MTU (1 << 4) 139 #define NDOPT_FLAG_RDNSS (1 << 5) 140 #define NDOPT_FLAG_DNSSL (1 << 6) 141 142 uint32_t ndopt_flags[] = { 143 [ND_OPT_SOURCE_LINKADDR] = NDOPT_FLAG_SRCLINKADDR, 144 [ND_OPT_TARGET_LINKADDR] = NDOPT_FLAG_TGTLINKADDR, 145 [ND_OPT_PREFIX_INFORMATION] = NDOPT_FLAG_PREFIXINFO, 146 [ND_OPT_REDIRECTED_HEADER] = NDOPT_FLAG_RDHDR, 147 [ND_OPT_MTU] = NDOPT_FLAG_MTU, 148 [ND_OPT_RDNSS] = NDOPT_FLAG_RDNSS, 149 [ND_OPT_DNSSL] = NDOPT_FLAG_DNSSL, 150 }; 151 152 static void rtadvd_shutdown(void); 153 static void sock_open(struct sockinfo *); 154 static void rtsock_open(struct sockinfo *); 155 static void rtadvd_input(struct sockinfo *); 156 static void rs_input(int, struct nd_router_solicit *, 157 struct in6_pktinfo *, struct sockaddr_in6 *); 158 static void ra_input(int, struct nd_router_advert *, 159 struct in6_pktinfo *, struct sockaddr_in6 *); 160 static int prefix_check(struct nd_opt_prefix_info *, struct rainfo *, 161 struct sockaddr_in6 *); 162 static int nd6_options(struct nd_opt_hdr *, int, 163 union nd_opt *, uint32_t); 164 static void free_ndopts(union nd_opt *); 165 static void rtmsg_input(struct sockinfo *); 166 static void set_short_delay(struct ifinfo *); 167 static int check_accept_rtadv(int); 168 169 int 170 main(int argc, char *argv[]) 171 { 172 struct pollfd set[PFD_MAX]; 173 struct timeval *timeout; 174 int i, ch; 175 int fflag = 0, logopt; 176 int error; 177 pid_t pid, otherpid; 178 179 /* get command line options and arguments */ 180 while ((ch = getopt(argc, argv, "c:C:dDfM:p:Rs")) != -1) { 181 switch (ch) { 182 case 'c': 183 conffile = optarg; 184 break; 185 case 'C': 186 ctrlsock.si_name = optarg; 187 break; 188 case 'd': 189 dflag++; 190 break; 191 case 'D': 192 dflag += 3; 193 break; 194 case 'f': 195 fflag = 1; 196 break; 197 case 'M': 198 mcastif = optarg; 199 break; 200 case 'R': 201 fprintf(stderr, "rtadvd: " 202 "the -R option is currently ignored.\n"); 203 /* accept_rr = 1; */ 204 /* run anyway... */ 205 break; 206 case 's': 207 sflag = 1; 208 break; 209 case 'p': 210 pidfilename = optarg; 211 break; 212 } 213 } 214 argc -= optind; 215 argv += optind; 216 if (argc == 0) { 217 fprintf(stderr, 218 "usage: rtadvd [-dDfRs] [-c conffile] " 219 "[-C ctrlsockname] [-M ifname] " 220 "[-p pidfile] interfaces...\n"); 221 exit(1); 222 } 223 224 logopt = LOG_NDELAY | LOG_PID; 225 if (fflag) 226 logopt |= LOG_PERROR; 227 openlog("rtadvd", logopt, LOG_DAEMON); 228 229 /* set log level */ 230 if (dflag > 2) 231 (void)setlogmask(LOG_UPTO(LOG_DEBUG)); 232 else if (dflag > 1) 233 (void)setlogmask(LOG_UPTO(LOG_INFO)); 234 else if (dflag > 0) 235 (void)setlogmask(LOG_UPTO(LOG_NOTICE)); 236 else 237 (void)setlogmask(LOG_UPTO(LOG_ERR)); 238 239 /* timer initialization */ 240 rtadvd_timer_init(); 241 242 #ifndef HAVE_ARC4RANDOM 243 /* random value initialization */ 244 #ifdef __FreeBSD__ 245 srandomdev(); 246 #else 247 srandom((unsigned long)time(NULL)); 248 #endif 249 #endif 250 pfh = pidfile_open(pidfilename, 0600, &otherpid); 251 if (pfh == NULL) { 252 if (errno == EEXIST) 253 errx(1, "%s already running, pid: %d", 254 getprogname(), otherpid); 255 syslog(LOG_ERR, 256 "failed to open the pid file %s, run anyway.", 257 pidfilename); 258 } 259 if (!fflag) 260 daemon(1, 0); 261 262 sock_open(&sock); 263 264 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL); 265 for (i = 0; i < argc; i++) 266 update_persist_ifinfo(&ifilist, argv[i]); 267 268 csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 269 if (ctrlsock.si_fd == -1) { 270 syslog(LOG_ERR, "cannot open control socket: %s", 271 strerror(errno)); 272 exit(1); 273 } 274 275 /* record the current PID */ 276 pid = getpid(); 277 pidfile_write(pfh); 278 279 set[PFD_RAWSOCK].fd = sock.si_fd; 280 set[PFD_RAWSOCK].events = POLLIN; 281 if (sflag == 0) { 282 rtsock_open(&rtsock); 283 set[PFD_RTSOCK].fd = rtsock.si_fd; 284 set[PFD_RTSOCK].events = POLLIN; 285 } else 286 set[PFD_RTSOCK].fd = -1; 287 set[PFD_CSOCK].fd = ctrlsock.si_fd; 288 set[PFD_CSOCK].events = POLLIN; 289 signal(SIGTERM, set_do_shutdown); 290 signal(SIGINT, set_do_shutdown); 291 signal(SIGHUP, set_do_reload); 292 293 error = csock_listen(&ctrlsock); 294 if (error) { 295 syslog(LOG_ERR, "cannot listen control socket: %s", 296 strerror(errno)); 297 exit(1); 298 } 299 300 /* load configuration file */ 301 set_do_reload(0); 302 303 while (1) { 304 if (is_do_shutdown()) 305 rtadvd_shutdown(); 306 307 if (is_do_reload()) { 308 loadconfig_ifname(reload_ifname()); 309 if (reload_ifname() == NULL) 310 syslog(LOG_INFO, 311 "configuration file reloaded."); 312 else 313 syslog(LOG_INFO, 314 "configuration file for %s reloaded.", 315 reload_ifname()); 316 reset_do_reload(); 317 } 318 319 /* timeout handler update for active interfaces */ 320 rtadvd_update_timeout_handler(); 321 322 /* timer expiration check and reset the timer */ 323 timeout = rtadvd_check_timer(); 324 325 if (timeout != NULL) { 326 syslog(LOG_DEBUG, 327 "<%s> set timer to %ld:%ld. waiting for " 328 "inputs or timeout", __func__, 329 (long int)timeout->tv_sec, 330 (long int)timeout->tv_usec); 331 } else { 332 syslog(LOG_DEBUG, 333 "<%s> there's no timer. waiting for inputs", 334 __func__); 335 } 336 if ((i = poll(set, sizeof(set)/sizeof(set[0]), 337 timeout ? (timeout->tv_sec * 1000 + 338 timeout->tv_usec / 1000) : INFTIM)) < 0) { 339 340 /* EINTR would occur if a signal was delivered */ 341 if (errno != EINTR) 342 syslog(LOG_ERR, "poll() failed: %s", 343 strerror(errno)); 344 continue; 345 } 346 if (i == 0) /* timeout */ 347 continue; 348 if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN) 349 rtmsg_input(&rtsock); 350 351 if (set[PFD_RAWSOCK].revents & POLLIN) 352 rtadvd_input(&sock); 353 354 if (set[PFD_CSOCK].revents & POLLIN) { 355 int fd; 356 357 fd = csock_accept(&ctrlsock); 358 if (fd == -1) 359 syslog(LOG_ERR, 360 "cannot accept() control socket: %s", 361 strerror(errno)); 362 else { 363 cm_handler_server(fd); 364 close(fd); 365 } 366 } 367 } 368 exit(0); /* NOTREACHED */ 369 } 370 371 static void 372 rtadvd_shutdown(void) 373 { 374 struct ifinfo *ifi; 375 struct rainfo *rai; 376 struct rdnss *rdn; 377 struct dnssl *dns; 378 379 if (wait_shutdown) { 380 syslog(LOG_INFO, 381 "waiting expiration of the all RA timers."); 382 383 TAILQ_FOREACH(ifi, &ifilist, ifi_next) { 384 if (ifi->ifi_ra_timer != NULL) 385 break; 386 } 387 if (ifi == NULL) { 388 syslog(LOG_NOTICE, "gracefully terminated."); 389 exit(0); 390 } 391 392 sleep(1); 393 return; 394 } 395 396 syslog(LOG_DEBUG, "<%s> cease to be an advertising router", 397 __func__); 398 399 wait_shutdown = 1; 400 401 TAILQ_FOREACH(rai, &railist, rai_next) { 402 rai->rai_lifetime = 0; 403 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) 404 rdn->rd_ltime = 0; 405 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) 406 dns->dn_ltime = 0; 407 } 408 TAILQ_FOREACH(ifi, &ifilist, ifi_next) { 409 if (!ifi->ifi_persist) 410 continue; 411 if (ifi->ifi_state == IFI_STATE_UNCONFIGURED) 412 continue; 413 if (ifi->ifi_ra_timer == NULL) 414 continue; 415 if (ifi->ifi_ra_lastsent.tv_sec == 0 && 416 ifi->ifi_ra_lastsent.tv_usec == 0 && 417 ifi->ifi_ra_timer != NULL) { 418 /* 419 * When RA configured but never sent, 420 * ignore the IF immediately. 421 */ 422 rtadvd_remove_timer(ifi->ifi_ra_timer); 423 ifi->ifi_ra_timer = NULL; 424 ifi->ifi_state = IFI_STATE_UNCONFIGURED; 425 continue; 426 } 427 428 ifi->ifi_state = IFI_STATE_TRANSITIVE; 429 430 /* Mark as the shut-down state. */ 431 ifi->ifi_rainfo_trans = ifi->ifi_rainfo; 432 ifi->ifi_rainfo = NULL; 433 434 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS; 435 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS; 436 437 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 438 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 439 ifi->ifi_ra_timer); 440 } 441 syslog(LOG_NOTICE, "final RA transmission started."); 442 443 pidfile_remove(pfh); 444 csock_close(&ctrlsock); 445 } 446 447 static void 448 rtmsg_input(struct sockinfo *s) 449 { 450 int n, type, ifindex = 0, plen; 451 size_t len; 452 char msg[2048], *next, *lim; 453 char ifname[IFNAMSIZ]; 454 struct if_announcemsghdr *ifan; 455 struct rt_msghdr *rtm; 456 struct prefix *pfx; 457 struct rainfo *rai; 458 struct in6_addr *addr; 459 struct ifinfo *ifi; 460 char addrbuf[INET6_ADDRSTRLEN]; 461 int prefixchange = 0; 462 463 if (s == NULL) { 464 syslog(LOG_ERR, "<%s> internal error", __func__); 465 exit(1); 466 } 467 n = read(s->si_fd, msg, sizeof(msg)); 468 rtm = (struct rt_msghdr *)msg; 469 syslog(LOG_DEBUG, "<%s> received a routing message " 470 "(type = %d, len = %d)", __func__, rtm->rtm_type, n); 471 472 if (n > rtm->rtm_msglen) { 473 /* 474 * This usually won't happen for messages received on 475 * a routing socket. 476 */ 477 syslog(LOG_DEBUG, 478 "<%s> received data length is larger than " 479 "1st routing message len. multiple messages? " 480 "read %d bytes, but 1st msg len = %d", 481 __func__, n, rtm->rtm_msglen); 482 #if 0 483 /* adjust length */ 484 n = rtm->rtm_msglen; 485 #endif 486 } 487 488 lim = msg + n; 489 for (next = msg; next < lim; next += len) { 490 int oldifflags; 491 492 next = get_next_msg(next, lim, 0, &len, 493 RTADV_TYPE2BITMASK(RTM_ADD) | 494 RTADV_TYPE2BITMASK(RTM_DELETE) | 495 RTADV_TYPE2BITMASK(RTM_NEWADDR) | 496 RTADV_TYPE2BITMASK(RTM_DELADDR) | 497 RTADV_TYPE2BITMASK(RTM_IFINFO) | 498 RTADV_TYPE2BITMASK(RTM_IFANNOUNCE)); 499 if (len == 0) 500 break; 501 type = ((struct rt_msghdr *)next)->rtm_type; 502 switch (type) { 503 case RTM_ADD: 504 case RTM_DELETE: 505 ifindex = get_rtm_ifindex(next); 506 break; 507 case RTM_NEWADDR: 508 case RTM_DELADDR: 509 ifindex = (int)((struct ifa_msghdr *)next)->ifam_index; 510 break; 511 case RTM_IFINFO: 512 ifindex = (int)((struct if_msghdr *)next)->ifm_index; 513 break; 514 case RTM_IFANNOUNCE: 515 ifan = (struct if_announcemsghdr *)next; 516 switch (ifan->ifan_what) { 517 case IFAN_ARRIVAL: 518 case IFAN_DEPARTURE: 519 break; 520 default: 521 syslog(LOG_DEBUG, 522 "<%s:%d> unknown ifan msg (ifan_what=%d)", 523 __func__, __LINE__, ifan->ifan_what); 524 continue; 525 } 526 527 syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)", 528 __func__, ifan->ifan_index, ifan->ifan_what); 529 switch (ifan->ifan_what) { 530 case IFAN_ARRIVAL: 531 syslog(LOG_NOTICE, 532 "interface added (idx=%d)", 533 ifan->ifan_index); 534 update_ifinfo(&ifilist, ifan->ifan_index); 535 loadconfig_index(ifan->ifan_index); 536 break; 537 case IFAN_DEPARTURE: 538 syslog(LOG_NOTICE, 539 "interface removed (idx=%d)", 540 ifan->ifan_index); 541 rm_ifinfo_index(ifan->ifan_index); 542 543 /* Clear ifi_ifindex */ 544 TAILQ_FOREACH(ifi, &ifilist, ifi_next) { 545 if (ifi->ifi_ifindex 546 == ifan->ifan_index) { 547 ifi->ifi_ifindex = 0; 548 break; 549 } 550 } 551 update_ifinfo(&ifilist, ifan->ifan_index); 552 break; 553 } 554 continue; 555 default: 556 /* should not reach here */ 557 syslog(LOG_DEBUG, 558 "<%s:%d> unknown rtmsg %d on %s", 559 __func__, __LINE__, type, 560 if_indextoname(ifindex, ifname)); 561 continue; 562 } 563 ifi = if_indextoifinfo(ifindex); 564 if (ifi == NULL) { 565 syslog(LOG_DEBUG, 566 "<%s> ifinfo not found for idx=%d. Why?", 567 __func__, ifindex); 568 continue; 569 } 570 rai = ifi->ifi_rainfo; 571 if (rai == NULL) { 572 syslog(LOG_DEBUG, 573 "<%s> route changed on " 574 "non advertising interface(%s)", 575 __func__, ifi->ifi_ifname); 576 continue; 577 } 578 579 oldifflags = ifi->ifi_flags; 580 /* init ifflags because it may have changed */ 581 update_ifinfo(&ifilist, ifindex); 582 583 switch (type) { 584 case RTM_ADD: 585 if (sflag) 586 break; /* we aren't interested in prefixes */ 587 588 addr = get_addr(msg); 589 plen = get_prefixlen(msg); 590 /* sanity check for plen */ 591 /* as RFC2373, prefixlen is at least 4 */ 592 if (plen < 4 || plen > 127) { 593 syslog(LOG_INFO, "<%s> new interface route's" 594 "plen %d is invalid for a prefix", 595 __func__, plen); 596 break; 597 } 598 pfx = find_prefix(rai, addr, plen); 599 if (pfx) { 600 if (pfx->pfx_timer) { 601 /* 602 * If the prefix has been invalidated, 603 * make it available again. 604 */ 605 update_prefix(pfx); 606 prefixchange = 1; 607 } else 608 syslog(LOG_DEBUG, 609 "<%s> new prefix(%s/%d) " 610 "added on %s, " 611 "but it was already in list", 612 __func__, 613 inet_ntop(AF_INET6, addr, 614 (char *)addrbuf, 615 sizeof(addrbuf)), 616 plen, ifi->ifi_ifname); 617 break; 618 } 619 make_prefix(rai, ifindex, addr, plen); 620 prefixchange = 1; 621 break; 622 case RTM_DELETE: 623 if (sflag) 624 break; 625 626 addr = get_addr(msg); 627 plen = get_prefixlen(msg); 628 /* sanity check for plen */ 629 /* as RFC2373, prefixlen is at least 4 */ 630 if (plen < 4 || plen > 127) { 631 syslog(LOG_INFO, 632 "<%s> deleted interface route's " 633 "plen %d is invalid for a prefix", 634 __func__, plen); 635 break; 636 } 637 pfx = find_prefix(rai, addr, plen); 638 if (pfx == NULL) { 639 syslog(LOG_DEBUG, 640 "<%s> prefix(%s/%d) was deleted on %s, " 641 "but it was not in list", 642 __func__, inet_ntop(AF_INET6, addr, 643 (char *)addrbuf, sizeof(addrbuf)), 644 plen, ifi->ifi_ifname); 645 break; 646 } 647 invalidate_prefix(pfx); 648 prefixchange = 1; 649 break; 650 case RTM_NEWADDR: 651 case RTM_DELADDR: 652 case RTM_IFINFO: 653 break; 654 default: 655 /* should not reach here */ 656 syslog(LOG_DEBUG, 657 "<%s:%d> unknown rtmsg %d on %s", 658 __func__, __LINE__, type, 659 if_indextoname(ifindex, ifname)); 660 return; 661 } 662 663 /* check if an interface flag is changed */ 664 if ((oldifflags & IFF_UP) && /* UP to DOWN */ 665 !(ifi->ifi_flags & IFF_UP)) { 666 syslog(LOG_NOTICE, 667 "<interface %s becomes down. stop timer.", 668 ifi->ifi_ifname); 669 rtadvd_remove_timer(ifi->ifi_ra_timer); 670 ifi->ifi_ra_timer = NULL; 671 } else if (!(oldifflags & IFF_UP) && /* DOWN to UP */ 672 (ifi->ifi_flags & IFF_UP)) { 673 syslog(LOG_NOTICE, 674 "interface %s becomes up. restart timer.", 675 ifi->ifi_ifname); 676 677 ifi->ifi_state = IFI_STATE_TRANSITIVE; 678 ifi->ifi_burstcount = 679 MAX_INITIAL_RTR_ADVERTISEMENTS; 680 ifi->ifi_burstinterval = 681 MAX_INITIAL_RTR_ADVERT_INTERVAL; 682 683 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout, 684 ra_timer_update, ifi, ifi); 685 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 686 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 687 ifi->ifi_ra_timer); 688 } else if (prefixchange && 689 (ifi->ifi_flags & IFF_UP)) { 690 /* 691 * An advertised prefix has been added or invalidated. 692 * Will notice the change in a short delay. 693 */ 694 set_short_delay(ifi); 695 } 696 } 697 698 return; 699 } 700 701 void 702 rtadvd_input(struct sockinfo *s) 703 { 704 ssize_t i; 705 int *hlimp = NULL; 706 #ifdef OLDRAWSOCKET 707 struct ip6_hdr *ip; 708 #endif 709 struct icmp6_hdr *icp; 710 int ifindex = 0; 711 struct cmsghdr *cm; 712 struct in6_pktinfo *pi = NULL; 713 char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 714 struct in6_addr dst = in6addr_any; 715 struct ifinfo *ifi; 716 717 syslog(LOG_DEBUG, "<%s> enter", __func__); 718 719 if (s == NULL) { 720 syslog(LOG_ERR, "<%s> internal error", __func__); 721 exit(1); 722 } 723 /* 724 * Get message. We reset msg_controllen since the field could 725 * be modified if we had received a message before setting 726 * receive options. 727 */ 728 rcvmhdr.msg_controllen = rcvcmsgbuflen; 729 if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0) 730 return; 731 732 /* extract optional information via Advanced API */ 733 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr); 734 cm; 735 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) { 736 if (cm->cmsg_level == IPPROTO_IPV6 && 737 cm->cmsg_type == IPV6_PKTINFO && 738 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) { 739 pi = (struct in6_pktinfo *)(CMSG_DATA(cm)); 740 ifindex = pi->ipi6_ifindex; 741 dst = pi->ipi6_addr; 742 } 743 if (cm->cmsg_level == IPPROTO_IPV6 && 744 cm->cmsg_type == IPV6_HOPLIMIT && 745 cm->cmsg_len == CMSG_LEN(sizeof(int))) 746 hlimp = (int *)CMSG_DATA(cm); 747 } 748 if (ifindex == 0) { 749 syslog(LOG_ERR, "failed to get receiving interface"); 750 return; 751 } 752 if (hlimp == NULL) { 753 syslog(LOG_ERR, "failed to get receiving hop limit"); 754 return; 755 } 756 757 /* 758 * If we happen to receive data on an interface which is now gone 759 * or down, just discard the data. 760 */ 761 ifi = if_indextoifinfo(pi->ipi6_ifindex); 762 if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) { 763 syslog(LOG_INFO, 764 "<%s> received data on a disabled interface (%s)", 765 __func__, 766 (ifi == NULL) ? "[gone]" : ifi->ifi_ifname); 767 return; 768 } 769 770 #ifdef OLDRAWSOCKET 771 if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) { 772 syslog(LOG_ERR, 773 "packet size(%d) is too short", i); 774 return; 775 } 776 777 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base; 778 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */ 779 #else 780 if ((size_t)i < sizeof(struct icmp6_hdr)) { 781 syslog(LOG_ERR, "packet size(%zd) is too short", i); 782 return; 783 } 784 785 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base; 786 #endif 787 788 switch (icp->icmp6_type) { 789 case ND_ROUTER_SOLICIT: 790 /* 791 * Message verification - RFC 4861 6.1.1 792 * XXX: these checks must be done in the kernel as well, 793 * but we can't completely rely on them. 794 */ 795 if (*hlimp != 255) { 796 syslog(LOG_NOTICE, 797 "RS with invalid hop limit(%d) " 798 "received from %s on %s", 799 *hlimp, 800 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf, 801 sizeof(ntopbuf)), 802 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 803 return; 804 } 805 if (icp->icmp6_code) { 806 syslog(LOG_NOTICE, 807 "RS with invalid ICMP6 code(%d) " 808 "received from %s on %s", 809 icp->icmp6_code, 810 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf, 811 sizeof(ntopbuf)), 812 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 813 return; 814 } 815 if ((size_t)i < sizeof(struct nd_router_solicit)) { 816 syslog(LOG_NOTICE, 817 "RS from %s on %s does not have enough " 818 "length (len = %zd)", 819 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf, 820 sizeof(ntopbuf)), 821 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i); 822 return; 823 } 824 rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom); 825 break; 826 case ND_ROUTER_ADVERT: 827 /* 828 * Message verification - RFC 4861 6.1.2 829 * XXX: there's the same dilemma as above... 830 */ 831 if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) { 832 syslog(LOG_NOTICE, 833 "RA witn non-linklocal source address " 834 "received from %s on %s", 835 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, 836 ntopbuf, sizeof(ntopbuf)), 837 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 838 return; 839 } 840 if (*hlimp != 255) { 841 syslog(LOG_NOTICE, 842 "RA with invalid hop limit(%d) " 843 "received from %s on %s", 844 *hlimp, 845 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf, 846 sizeof(ntopbuf)), 847 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 848 return; 849 } 850 if (icp->icmp6_code) { 851 syslog(LOG_NOTICE, 852 "RA with invalid ICMP6 code(%d) " 853 "received from %s on %s", 854 icp->icmp6_code, 855 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf, 856 sizeof(ntopbuf)), 857 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 858 return; 859 } 860 if ((size_t)i < sizeof(struct nd_router_advert)) { 861 syslog(LOG_NOTICE, 862 "RA from %s on %s does not have enough " 863 "length (len = %zd)", 864 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf, 865 sizeof(ntopbuf)), 866 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i); 867 return; 868 } 869 ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom); 870 break; 871 case ICMP6_ROUTER_RENUMBERING: 872 if (mcastif == NULL) { 873 syslog(LOG_ERR, "received a router renumbering " 874 "message, but not allowed to be accepted"); 875 break; 876 } 877 rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom, 878 &dst); 879 break; 880 default: 881 /* 882 * Note that this case is POSSIBLE, especially just 883 * after invocation of the daemon. This is because we 884 * could receive message after opening the socket and 885 * before setting ICMP6 type filter(see sock_open()). 886 */ 887 syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type); 888 return; 889 } 890 891 return; 892 } 893 894 static void 895 rs_input(int len, struct nd_router_solicit *rs, 896 struct in6_pktinfo *pi, struct sockaddr_in6 *from) 897 { 898 char ntopbuf[INET6_ADDRSTRLEN]; 899 char ifnamebuf[IFNAMSIZ]; 900 union nd_opt ndopts; 901 struct rainfo *rai; 902 struct ifinfo *ifi; 903 struct soliciter *sol; 904 905 syslog(LOG_DEBUG, 906 "<%s> RS received from %s on %s", 907 __func__, 908 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)), 909 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 910 911 /* ND option check */ 912 memset(&ndopts, 0, sizeof(ndopts)); 913 TAILQ_INIT(&ndopts.opt_list); 914 if (nd6_options((struct nd_opt_hdr *)(rs + 1), 915 len - sizeof(struct nd_router_solicit), 916 &ndopts, NDOPT_FLAG_SRCLINKADDR)) { 917 syslog(LOG_INFO, 918 "<%s> ND option check failed for an RS from %s on %s", 919 __func__, 920 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 921 sizeof(ntopbuf)), 922 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 923 return; 924 } 925 926 /* 927 * If the IP source address is the unspecified address, there 928 * must be no source link-layer address option in the message. 929 * (RFC 4861 6.1.1) 930 */ 931 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) && 932 ndopts.opt_src_lladdr) { 933 syslog(LOG_INFO, 934 "<%s> RS from unspecified src on %s has a link-layer" 935 " address option", 936 __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 937 goto done; 938 } 939 940 ifi = if_indextoifinfo(pi->ipi6_ifindex); 941 if (ifi == NULL) { 942 syslog(LOG_INFO, 943 "<%s> if (idx=%d) not found. Why?", 944 __func__, pi->ipi6_ifindex); 945 goto done; 946 } 947 rai = ifi->ifi_rainfo; 948 if (rai == NULL) { 949 syslog(LOG_INFO, 950 "<%s> RS received on non advertising interface(%s)", 951 __func__, 952 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 953 goto done; 954 } 955 956 rai->rai_ifinfo->ifi_rsinput++; 957 958 /* 959 * Decide whether to send RA according to the rate-limit 960 * consideration. 961 */ 962 963 /* record sockaddr waiting for RA, if possible */ 964 sol = (struct soliciter *)malloc(sizeof(*sol)); 965 if (sol) { 966 sol->sol_addr = *from; 967 /* XXX RFC 2553 need clarification on flowinfo */ 968 sol->sol_addr.sin6_flowinfo = 0; 969 TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next); 970 } 971 972 /* 973 * If there is already a waiting RS packet, don't 974 * update the timer. 975 */ 976 if (ifi->ifi_rs_waitcount++) 977 goto done; 978 979 set_short_delay(ifi); 980 981 done: 982 free_ndopts(&ndopts); 983 return; 984 } 985 986 static void 987 set_short_delay(struct ifinfo *ifi) 988 { 989 long delay; /* must not be greater than 1000000 */ 990 struct timeval interval, now, min_delay, tm_tmp, *rest; 991 992 /* 993 * Compute a random delay. If the computed value 994 * corresponds to a time later than the time the next 995 * multicast RA is scheduled to be sent, ignore the random 996 * delay and send the advertisement at the 997 * already-scheduled time. RFC 4861 6.2.6 998 */ 999 #ifdef HAVE_ARC4RANDOM 1000 delay = arc4random_uniform(MAX_RA_DELAY_TIME); 1001 #else 1002 delay = random() % MAX_RA_DELAY_TIME; 1003 #endif 1004 interval.tv_sec = 0; 1005 interval.tv_usec = delay; 1006 rest = rtadvd_timer_rest(ifi->ifi_ra_timer); 1007 if (TIMEVAL_LT(rest, &interval)) { 1008 syslog(LOG_DEBUG, "<%s> random delay is larger than " 1009 "the rest of the current timer", __func__); 1010 interval = *rest; 1011 } 1012 1013 /* 1014 * If we sent a multicast Router Advertisement within 1015 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule 1016 * the advertisement to be sent at a time corresponding to 1017 * MIN_DELAY_BETWEEN_RAS plus the random value after the 1018 * previous advertisement was sent. 1019 */ 1020 gettimeofday(&now, NULL); 1021 TIMEVAL_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp); 1022 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS; 1023 min_delay.tv_usec = 0; 1024 if (TIMEVAL_LT(&tm_tmp, &min_delay)) { 1025 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay); 1026 TIMEVAL_ADD(&min_delay, &interval, &interval); 1027 } 1028 rtadvd_set_timer(&interval, ifi->ifi_ra_timer); 1029 } 1030 1031 static int 1032 check_accept_rtadv(int idx) 1033 { 1034 struct ifinfo *ifi; 1035 1036 TAILQ_FOREACH(ifi, &ifilist, ifi_next) { 1037 if (ifi->ifi_ifindex == idx) 1038 break; 1039 } 1040 if (ifi == NULL) { 1041 syslog(LOG_DEBUG, 1042 "<%s> if (idx=%d) not found. Why?", 1043 __func__, idx); 1044 return (0); 1045 } 1046 #if (__FreeBSD_version < 900000) 1047 /* 1048 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv 1049 * RA_SEND: ip6.forwarding 1050 */ 1051 return ((getinet6sysctl(IPV6CTL_FORWARDING) == 0) && 1052 (getinet6sysctl(IPV6CTL_ACCEPT_RTADV) == 1)); 1053 #else 1054 /* 1055 * RA_RECV: ND6_IFF_ACCEPT_RTADV 1056 * RA_SEND: ip6.forwarding 1057 */ 1058 if (update_ifinfo_nd_flags(ifi) != 0) { 1059 syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx); 1060 return (0); 1061 } 1062 1063 return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV); 1064 #endif 1065 } 1066 1067 static void 1068 ra_input(int len, struct nd_router_advert *nra, 1069 struct in6_pktinfo *pi, struct sockaddr_in6 *from) 1070 { 1071 struct rainfo *rai; 1072 struct ifinfo *ifi; 1073 char ntopbuf[INET6_ADDRSTRLEN]; 1074 char ifnamebuf[IFNAMSIZ]; 1075 union nd_opt ndopts; 1076 const char *on_off[] = {"OFF", "ON"}; 1077 uint32_t reachabletime, retranstimer, mtu; 1078 int inconsistent = 0; 1079 int error; 1080 1081 syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__, 1082 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)), 1083 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 1084 1085 /* ND option check */ 1086 memset(&ndopts, 0, sizeof(ndopts)); 1087 TAILQ_INIT(&ndopts.opt_list); 1088 error = nd6_options((struct nd_opt_hdr *)(nra + 1), 1089 len - sizeof(struct nd_router_advert), &ndopts, 1090 NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU | 1091 NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL); 1092 if (error) { 1093 syslog(LOG_INFO, 1094 "<%s> ND option check failed for an RA from %s on %s", 1095 __func__, 1096 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1097 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex, 1098 ifnamebuf)); 1099 return; 1100 } 1101 1102 /* 1103 * RA consistency check according to RFC 4861 6.2.7 1104 */ 1105 ifi = if_indextoifinfo(pi->ipi6_ifindex); 1106 if (ifi->ifi_rainfo == NULL) { 1107 syslog(LOG_INFO, 1108 "<%s> received RA from %s on non-advertising" 1109 " interface(%s)", 1110 __func__, 1111 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1112 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex, 1113 ifnamebuf)); 1114 goto done; 1115 } 1116 rai = ifi->ifi_rainfo; 1117 ifi->ifi_rainput++; 1118 syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__, 1119 ifi->ifi_rainput); 1120 1121 /* Cur Hop Limit value */ 1122 if (nra->nd_ra_curhoplimit && rai->rai_hoplimit && 1123 nra->nd_ra_curhoplimit != rai->rai_hoplimit) { 1124 syslog(LOG_NOTICE, 1125 "CurHopLimit inconsistent on %s:" 1126 " %d from %s, %d from us", 1127 ifi->ifi_ifname, nra->nd_ra_curhoplimit, 1128 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1129 sizeof(ntopbuf)), rai->rai_hoplimit); 1130 inconsistent++; 1131 } 1132 /* M flag */ 1133 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) != 1134 rai->rai_managedflg) { 1135 syslog(LOG_NOTICE, 1136 "M flag inconsistent on %s:" 1137 " %s from %s, %s from us", 1138 ifi->ifi_ifname, on_off[!rai->rai_managedflg], 1139 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1140 sizeof(ntopbuf)), on_off[rai->rai_managedflg]); 1141 inconsistent++; 1142 } 1143 /* O flag */ 1144 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) != 1145 rai->rai_otherflg) { 1146 syslog(LOG_NOTICE, 1147 "O flag inconsistent on %s:" 1148 " %s from %s, %s from us", 1149 ifi->ifi_ifname, on_off[!rai->rai_otherflg], 1150 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1151 sizeof(ntopbuf)), on_off[rai->rai_otherflg]); 1152 inconsistent++; 1153 } 1154 /* Reachable Time */ 1155 reachabletime = ntohl(nra->nd_ra_reachable); 1156 if (reachabletime && rai->rai_reachabletime && 1157 reachabletime != rai->rai_reachabletime) { 1158 syslog(LOG_NOTICE, 1159 "ReachableTime inconsistent on %s:" 1160 " %d from %s, %d from us", 1161 ifi->ifi_ifname, reachabletime, 1162 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1163 sizeof(ntopbuf)), rai->rai_reachabletime); 1164 inconsistent++; 1165 } 1166 /* Retrans Timer */ 1167 retranstimer = ntohl(nra->nd_ra_retransmit); 1168 if (retranstimer && rai->rai_retranstimer && 1169 retranstimer != rai->rai_retranstimer) { 1170 syslog(LOG_NOTICE, 1171 "RetranceTimer inconsistent on %s:" 1172 " %d from %s, %d from us", 1173 ifi->ifi_ifname, retranstimer, 1174 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1175 sizeof(ntopbuf)), rai->rai_retranstimer); 1176 inconsistent++; 1177 } 1178 /* Values in the MTU options */ 1179 if (ndopts.opt_mtu) { 1180 mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu); 1181 if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) { 1182 syslog(LOG_NOTICE, 1183 "MTU option value inconsistent on %s:" 1184 " %d from %s, %d from us", 1185 ifi->ifi_ifname, mtu, 1186 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1187 sizeof(ntopbuf)), rai->rai_linkmtu); 1188 inconsistent++; 1189 } 1190 } 1191 /* Preferred and Valid Lifetimes for prefixes */ 1192 { 1193 struct nd_optlist *nol; 1194 1195 if (ndopts.opt_pi) 1196 if (prefix_check(ndopts.opt_pi, rai, from)) 1197 inconsistent++; 1198 1199 TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next) 1200 if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt, 1201 rai, from)) 1202 inconsistent++; 1203 } 1204 1205 if (inconsistent) 1206 ifi->ifi_rainconsistent++; 1207 1208 done: 1209 free_ndopts(&ndopts); 1210 return; 1211 } 1212 1213 /* return a non-zero value if the received prefix is inconsitent with ours */ 1214 static int 1215 prefix_check(struct nd_opt_prefix_info *pinfo, 1216 struct rainfo *rai, struct sockaddr_in6 *from) 1217 { 1218 struct ifinfo *ifi; 1219 uint32_t preferred_time, valid_time; 1220 struct prefix *pfx; 1221 int inconsistent = 0; 1222 char ntopbuf[INET6_ADDRSTRLEN]; 1223 char prefixbuf[INET6_ADDRSTRLEN]; 1224 struct timeval now; 1225 1226 #if 0 /* impossible */ 1227 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION) 1228 return (0); 1229 #endif 1230 ifi = rai->rai_ifinfo; 1231 /* 1232 * log if the adveritsed prefix has link-local scope(sanity check?) 1233 */ 1234 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) 1235 syslog(LOG_INFO, 1236 "<%s> link-local prefix %s/%d is advertised " 1237 "from %s on %s", 1238 __func__, 1239 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf, 1240 sizeof(prefixbuf)), 1241 pinfo->nd_opt_pi_prefix_len, 1242 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1243 sizeof(ntopbuf)), ifi->ifi_ifname); 1244 1245 if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix, 1246 pinfo->nd_opt_pi_prefix_len)) == NULL) { 1247 syslog(LOG_INFO, 1248 "<%s> prefix %s/%d from %s on %s is not in our list", 1249 __func__, 1250 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf, 1251 sizeof(prefixbuf)), 1252 pinfo->nd_opt_pi_prefix_len, 1253 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1254 sizeof(ntopbuf)), ifi->ifi_ifname); 1255 return (0); 1256 } 1257 1258 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time); 1259 if (pfx->pfx_pltimeexpire) { 1260 /* 1261 * The lifetime is decremented in real time, so we should 1262 * compare the expiration time. 1263 * (RFC 2461 Section 6.2.7.) 1264 * XXX: can we really expect that all routers on the link 1265 * have synchronized clocks? 1266 */ 1267 gettimeofday(&now, NULL); 1268 preferred_time += now.tv_sec; 1269 1270 if (!pfx->pfx_timer && rai->rai_clockskew && 1271 abs(preferred_time - pfx->pfx_pltimeexpire) > rai->rai_clockskew) { 1272 syslog(LOG_INFO, 1273 "<%s> preferred lifetime for %s/%d" 1274 " (decr. in real time) inconsistent on %s:" 1275 " %" PRIu32 " from %s, %" PRIu32 " from us", 1276 __func__, 1277 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf, 1278 sizeof(prefixbuf)), 1279 pinfo->nd_opt_pi_prefix_len, 1280 ifi->ifi_ifname, preferred_time, 1281 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1282 sizeof(ntopbuf)), pfx->pfx_pltimeexpire); 1283 inconsistent++; 1284 } 1285 } else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime) 1286 syslog(LOG_INFO, 1287 "<%s> preferred lifetime for %s/%d" 1288 " inconsistent on %s:" 1289 " %d from %s, %d from us", 1290 __func__, 1291 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf, 1292 sizeof(prefixbuf)), 1293 pinfo->nd_opt_pi_prefix_len, 1294 ifi->ifi_ifname, preferred_time, 1295 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1296 sizeof(ntopbuf)), pfx->pfx_preflifetime); 1297 1298 valid_time = ntohl(pinfo->nd_opt_pi_valid_time); 1299 if (pfx->pfx_vltimeexpire) { 1300 gettimeofday(&now, NULL); 1301 valid_time += now.tv_sec; 1302 1303 if (!pfx->pfx_timer && rai->rai_clockskew && 1304 abs(valid_time - pfx->pfx_vltimeexpire) > rai->rai_clockskew) { 1305 syslog(LOG_INFO, 1306 "<%s> valid lifetime for %s/%d" 1307 " (decr. in real time) inconsistent on %s:" 1308 " %d from %s, %" PRIu32 " from us", 1309 __func__, 1310 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf, 1311 sizeof(prefixbuf)), 1312 pinfo->nd_opt_pi_prefix_len, 1313 ifi->ifi_ifname, preferred_time, 1314 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1315 sizeof(ntopbuf)), pfx->pfx_vltimeexpire); 1316 inconsistent++; 1317 } 1318 } else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) { 1319 syslog(LOG_INFO, 1320 "<%s> valid lifetime for %s/%d" 1321 " inconsistent on %s:" 1322 " %d from %s, %d from us", 1323 __func__, 1324 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf, 1325 sizeof(prefixbuf)), 1326 pinfo->nd_opt_pi_prefix_len, 1327 ifi->ifi_ifname, valid_time, 1328 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, 1329 sizeof(ntopbuf)), pfx->pfx_validlifetime); 1330 inconsistent++; 1331 } 1332 1333 return (inconsistent); 1334 } 1335 1336 struct prefix * 1337 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen) 1338 { 1339 struct prefix *pfx; 1340 int bytelen, bitlen; 1341 char bitmask; 1342 1343 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) { 1344 if (plen != pfx->pfx_prefixlen) 1345 continue; 1346 1347 bytelen = plen / 8; 1348 bitlen = plen % 8; 1349 bitmask = 0xff << (8 - bitlen); 1350 1351 if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen)) 1352 continue; 1353 1354 if (bitlen == 0 || 1355 ((prefix->s6_addr[bytelen] & bitmask) == 1356 (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) { 1357 return (pfx); 1358 } 1359 } 1360 1361 return (NULL); 1362 } 1363 1364 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */ 1365 int 1366 prefix_match(struct in6_addr *p0, int plen0, 1367 struct in6_addr *p1, int plen1) 1368 { 1369 int bytelen, bitlen; 1370 char bitmask; 1371 1372 if (plen0 < plen1) 1373 return (0); 1374 1375 bytelen = plen1 / 8; 1376 bitlen = plen1 % 8; 1377 bitmask = 0xff << (8 - bitlen); 1378 1379 if (memcmp((void *)p0, (void *)p1, bytelen)) 1380 return (0); 1381 1382 if (bitlen == 0 || 1383 ((p0->s6_addr[bytelen] & bitmask) == 1384 (p1->s6_addr[bytelen] & bitmask))) { 1385 return (1); 1386 } 1387 1388 return (0); 1389 } 1390 1391 static int 1392 nd6_options(struct nd_opt_hdr *hdr, int limit, 1393 union nd_opt *ndopts, uint32_t optflags) 1394 { 1395 int optlen = 0; 1396 1397 for (; limit > 0; limit -= optlen) { 1398 if ((size_t)limit < sizeof(struct nd_opt_hdr)) { 1399 syslog(LOG_INFO, "<%s> short option header", __func__); 1400 goto bad; 1401 } 1402 1403 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen); 1404 if (hdr->nd_opt_len == 0) { 1405 syslog(LOG_INFO, 1406 "<%s> bad ND option length(0) (type = %d)", 1407 __func__, hdr->nd_opt_type); 1408 goto bad; 1409 } 1410 optlen = hdr->nd_opt_len << 3; 1411 if (optlen > limit) { 1412 syslog(LOG_INFO, "<%s> short option", __func__); 1413 goto bad; 1414 } 1415 1416 if (hdr->nd_opt_type > ND_OPT_MTU && 1417 hdr->nd_opt_type != ND_OPT_RDNSS && 1418 hdr->nd_opt_type != ND_OPT_DNSSL) { 1419 syslog(LOG_INFO, "<%s> unknown ND option(type %d)", 1420 __func__, hdr->nd_opt_type); 1421 continue; 1422 } 1423 1424 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) { 1425 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)", 1426 __func__, hdr->nd_opt_type); 1427 continue; 1428 } 1429 1430 /* 1431 * Option length check. Do it here for all fixed-length 1432 * options. 1433 */ 1434 switch (hdr->nd_opt_type) { 1435 case ND_OPT_MTU: 1436 if (optlen == sizeof(struct nd_opt_mtu)) 1437 break; 1438 goto skip; 1439 case ND_OPT_RDNSS: 1440 if (optlen >= 24 && 1441 (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0) 1442 break; 1443 goto skip; 1444 case ND_OPT_DNSSL: 1445 if (optlen >= 16 && 1446 (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0) 1447 break; 1448 goto skip; 1449 case ND_OPT_PREFIX_INFORMATION: 1450 if (optlen == sizeof(struct nd_opt_prefix_info)) 1451 break; 1452 skip: 1453 syslog(LOG_INFO, "<%s> invalid option length", 1454 __func__); 1455 continue; 1456 } 1457 1458 switch (hdr->nd_opt_type) { 1459 case ND_OPT_TARGET_LINKADDR: 1460 case ND_OPT_REDIRECTED_HEADER: 1461 case ND_OPT_RDNSS: 1462 case ND_OPT_DNSSL: 1463 break; /* we don't care about these options */ 1464 case ND_OPT_SOURCE_LINKADDR: 1465 case ND_OPT_MTU: 1466 if (ndopts->opt_array[hdr->nd_opt_type]) { 1467 syslog(LOG_INFO, 1468 "<%s> duplicated ND option (type = %d)", 1469 __func__, hdr->nd_opt_type); 1470 } 1471 ndopts->opt_array[hdr->nd_opt_type] = hdr; 1472 break; 1473 case ND_OPT_PREFIX_INFORMATION: 1474 { 1475 struct nd_optlist *nol; 1476 1477 if (ndopts->opt_pi == 0) { 1478 ndopts->opt_pi = 1479 (struct nd_opt_prefix_info *)hdr; 1480 continue; 1481 } 1482 nol = malloc(sizeof(*nol)); 1483 if (nol == NULL) { 1484 syslog(LOG_ERR, "<%s> can't allocate memory", 1485 __func__); 1486 goto bad; 1487 } 1488 nol->nol_opt = hdr; 1489 TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next); 1490 1491 break; 1492 } 1493 default: /* impossible */ 1494 break; 1495 } 1496 } 1497 1498 return (0); 1499 1500 bad: 1501 free_ndopts(ndopts); 1502 1503 return (-1); 1504 } 1505 1506 static void 1507 free_ndopts(union nd_opt *ndopts) 1508 { 1509 struct nd_optlist *nol; 1510 1511 while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) { 1512 TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next); 1513 free(nol); 1514 } 1515 } 1516 1517 void 1518 sock_open(struct sockinfo *s) 1519 { 1520 struct icmp6_filter filt; 1521 int on; 1522 /* XXX: should be max MTU attached to the node */ 1523 static char answer[1500]; 1524 1525 syslog(LOG_DEBUG, "<%s> enter", __func__); 1526 1527 if (s == NULL) { 1528 syslog(LOG_ERR, "<%s> internal error", __func__); 1529 exit(1); 1530 } 1531 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 1532 CMSG_SPACE(sizeof(int)); 1533 rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen); 1534 if (rcvcmsgbuf == NULL) { 1535 syslog(LOG_ERR, "<%s> not enough core", __func__); 1536 exit(1); 1537 } 1538 1539 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 1540 CMSG_SPACE(sizeof(int)); 1541 sndcmsgbuf = (char *)malloc(sndcmsgbuflen); 1542 if (sndcmsgbuf == NULL) { 1543 syslog(LOG_ERR, "<%s> not enough core", __func__); 1544 exit(1); 1545 } 1546 1547 if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 1548 syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno)); 1549 exit(1); 1550 } 1551 /* specify to tell receiving interface */ 1552 on = 1; 1553 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, 1554 sizeof(on)) < 0) { 1555 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__, 1556 strerror(errno)); 1557 exit(1); 1558 } 1559 on = 1; 1560 /* specify to tell value of hoplimit field of received IP6 hdr */ 1561 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, 1562 sizeof(on)) < 0) { 1563 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__, 1564 strerror(errno)); 1565 exit(1); 1566 } 1567 ICMP6_FILTER_SETBLOCKALL(&filt); 1568 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt); 1569 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt); 1570 if (mcastif != NULL) 1571 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt); 1572 1573 if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 1574 sizeof(filt)) < 0) { 1575 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s", 1576 __func__, strerror(errno)); 1577 exit(1); 1578 } 1579 1580 /* initialize msghdr for receiving packets */ 1581 rcviov[0].iov_base = (caddr_t)answer; 1582 rcviov[0].iov_len = sizeof(answer); 1583 rcvmhdr.msg_name = (caddr_t)&rcvfrom; 1584 rcvmhdr.msg_namelen = sizeof(rcvfrom); 1585 rcvmhdr.msg_iov = rcviov; 1586 rcvmhdr.msg_iovlen = 1; 1587 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; 1588 rcvmhdr.msg_controllen = rcvcmsgbuflen; 1589 1590 /* initialize msghdr for sending packets */ 1591 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 1592 sndmhdr.msg_iov = sndiov; 1593 sndmhdr.msg_iovlen = 1; 1594 sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 1595 sndmhdr.msg_controllen = sndcmsgbuflen; 1596 1597 return; 1598 } 1599 1600 /* open a routing socket to watch the routing table */ 1601 static void 1602 rtsock_open(struct sockinfo *s) 1603 { 1604 if (s == NULL) { 1605 syslog(LOG_ERR, "<%s> internal error", __func__); 1606 exit(1); 1607 } 1608 if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 1609 syslog(LOG_ERR, 1610 "<%s> socket: %s", __func__, strerror(errno)); 1611 exit(1); 1612 } 1613 } 1614 1615 struct ifinfo * 1616 if_indextoifinfo(int idx) 1617 { 1618 struct ifinfo *ifi; 1619 1620 TAILQ_FOREACH(ifi, &ifilist, ifi_next) { 1621 if (ifi->ifi_ifindex == idx) 1622 return (ifi); 1623 } 1624 1625 if (ifi != NULL) 1626 syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)", 1627 __func__, idx); 1628 else 1629 syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)", 1630 __func__, idx); 1631 1632 return (NULL); /* search failed */ 1633 } 1634 1635 void 1636 ra_output(struct ifinfo *ifi) 1637 { 1638 int i; 1639 struct cmsghdr *cm; 1640 struct in6_pktinfo *pi; 1641 struct soliciter *sol; 1642 struct rainfo *rai; 1643 1644 switch (ifi->ifi_state) { 1645 case IFI_STATE_CONFIGURED: 1646 rai = ifi->ifi_rainfo; 1647 break; 1648 case IFI_STATE_TRANSITIVE: 1649 rai = ifi->ifi_rainfo_trans; 1650 break; 1651 case IFI_STATE_UNCONFIGURED: 1652 syslog(LOG_DEBUG, "<%s> %s is unconfigured. " 1653 "Skip sending RAs.", 1654 __func__, ifi->ifi_ifname); 1655 return; 1656 default: 1657 rai = NULL; 1658 } 1659 if (rai == NULL) { 1660 syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s." 1661 "Skip sending RAs.", 1662 __func__, ifi->ifi_ifname); 1663 return; 1664 } 1665 if (!(ifi->ifi_flags & IFF_UP)) { 1666 syslog(LOG_DEBUG, "<%s> %s is not up. " 1667 "Skip sending RAs.", 1668 __func__, ifi->ifi_ifname); 1669 return; 1670 } 1671 /* 1672 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding. 1673 * 1674 * (lifetime == 0) = output 1675 * (lifetime != 0 && (check_accept_rtadv()) = no output 1676 * 1677 * Basically, hosts MUST NOT send Router Advertisement 1678 * messages at any time (RFC 4861, Section 6.2.3). However, it 1679 * would sometimes be useful to allow hosts to advertise some 1680 * parameters such as prefix information and link MTU. Thus, 1681 * we allow hosts to invoke rtadvd only when router lifetime 1682 * (on every advertising interface) is explicitly set 1683 * zero. (see also the above section) 1684 */ 1685 syslog(LOG_DEBUG, 1686 "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d " 1687 "on %s", __func__, 1688 rai->rai_lifetime, 1689 check_accept_rtadv(ifi->ifi_ifindex), 1690 getinet6sysctl(IPV6CTL_FORWARDING), 1691 ifi->ifi_ifname); 1692 1693 if (rai->rai_lifetime != 0) { 1694 if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) { 1695 syslog(LOG_ERR, 1696 "non-zero lifetime RA " 1697 "but net.inet6.ip6.forwarding=0. " 1698 "Ignored."); 1699 return; 1700 } 1701 if (check_accept_rtadv(ifi->ifi_ifindex)) { 1702 syslog(LOG_ERR, 1703 "non-zero lifetime RA " 1704 "on RA receiving interface %s." 1705 " Ignored.", ifi->ifi_ifname); 1706 return; 1707 } 1708 } 1709 1710 make_packet(rai); /* XXX: inefficient */ 1711 1712 sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes; 1713 sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data; 1714 sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen; 1715 1716 cm = CMSG_FIRSTHDR(&sndmhdr); 1717 /* specify the outgoing interface */ 1718 cm->cmsg_level = IPPROTO_IPV6; 1719 cm->cmsg_type = IPV6_PKTINFO; 1720 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 1721 pi = (struct in6_pktinfo *)CMSG_DATA(cm); 1722 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/ 1723 pi->ipi6_ifindex = ifi->ifi_ifindex; 1724 1725 /* specify the hop limit of the packet */ 1726 { 1727 int hoplimit = 255; 1728 1729 cm = CMSG_NXTHDR(&sndmhdr, cm); 1730 cm->cmsg_level = IPPROTO_IPV6; 1731 cm->cmsg_type = IPV6_HOPLIMIT; 1732 cm->cmsg_len = CMSG_LEN(sizeof(int)); 1733 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 1734 } 1735 1736 syslog(LOG_DEBUG, 1737 "<%s> send RA on %s, # of RS waitings = %d", 1738 __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount); 1739 1740 i = sendmsg(sock.si_fd, &sndmhdr, 0); 1741 1742 if (i < 0 || (size_t)i != rai->rai_ra_datalen) { 1743 if (i < 0) { 1744 syslog(LOG_ERR, "<%s> sendmsg on %s: %s", 1745 __func__, ifi->ifi_ifname, 1746 strerror(errno)); 1747 } 1748 } 1749 1750 /* 1751 * unicast advertisements 1752 * XXX commented out. reason: though spec does not forbit it, unicast 1753 * advert does not really help 1754 */ 1755 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) { 1756 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next); 1757 free(sol); 1758 } 1759 1760 /* update timestamp */ 1761 gettimeofday(&ifi->ifi_ra_lastsent, NULL); 1762 1763 /* update counter */ 1764 ifi->ifi_rs_waitcount = 0; 1765 ifi->ifi_raoutput++; 1766 1767 switch (ifi->ifi_state) { 1768 case IFI_STATE_CONFIGURED: 1769 if (ifi->ifi_burstcount > 0) 1770 ifi->ifi_burstcount--; 1771 break; 1772 case IFI_STATE_TRANSITIVE: 1773 ifi->ifi_burstcount--; 1774 if (ifi->ifi_burstcount == 0) { 1775 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) { 1776 /* Inital burst finished. */ 1777 if (ifi->ifi_rainfo_trans != NULL) 1778 ifi->ifi_rainfo_trans = NULL; 1779 } 1780 1781 /* Remove burst RA information */ 1782 if (ifi->ifi_rainfo_trans != NULL) { 1783 rm_rainfo(ifi->ifi_rainfo_trans); 1784 ifi->ifi_rainfo_trans = NULL; 1785 } 1786 1787 if (ifi->ifi_rainfo != NULL) { 1788 /* 1789 * TRANSITIVE -> CONFIGURED 1790 * 1791 * After initial burst or transition from 1792 * one configuration to another, 1793 * ifi_rainfo always points to the next RA 1794 * information. 1795 */ 1796 ifi->ifi_state = IFI_STATE_CONFIGURED; 1797 syslog(LOG_DEBUG, 1798 "<%s> ifname=%s marked as " 1799 "CONFIGURED.", __func__, 1800 ifi->ifi_ifname); 1801 } else { 1802 /* 1803 * TRANSITIVE -> UNCONFIGURED 1804 * 1805 * If ifi_rainfo points to NULL, this 1806 * interface is shutting down. 1807 * 1808 */ 1809 int error; 1810 1811 ifi->ifi_state = IFI_STATE_UNCONFIGURED; 1812 syslog(LOG_DEBUG, 1813 "<%s> ifname=%s marked as " 1814 "UNCONFIGURED.", __func__, 1815 ifi->ifi_ifname); 1816 error = sock_mc_leave(&sock, 1817 ifi->ifi_ifindex); 1818 if (error) 1819 exit(1); 1820 } 1821 } 1822 break; 1823 } 1824 } 1825 1826 /* process RA timer */ 1827 struct rtadvd_timer * 1828 ra_timeout(void *arg) 1829 { 1830 struct ifinfo *ifi; 1831 1832 ifi = (struct ifinfo *)arg; 1833 syslog(LOG_DEBUG, "<%s> RA timer on %s is expired", 1834 __func__, ifi->ifi_ifname); 1835 1836 ra_output(ifi); 1837 1838 return (ifi->ifi_ra_timer); 1839 } 1840 1841 /* update RA timer */ 1842 void 1843 ra_timer_update(void *arg, struct timeval *tm) 1844 { 1845 uint16_t interval; 1846 struct rainfo *rai; 1847 struct ifinfo *ifi; 1848 1849 ifi = (struct ifinfo *)arg; 1850 rai = ifi->ifi_rainfo; 1851 interval = 0; 1852 1853 switch (ifi->ifi_state) { 1854 case IFI_STATE_UNCONFIGURED: 1855 return; 1856 break; 1857 case IFI_STATE_CONFIGURED: 1858 /* 1859 * Whenever a multicast advertisement is sent from 1860 * an interface, the timer is reset to a 1861 * uniformly-distributed random value between the 1862 * interface's configured MinRtrAdvInterval and 1863 * MaxRtrAdvInterval (RFC4861 6.2.4). 1864 */ 1865 interval = rai->rai_mininterval; 1866 #ifdef HAVE_ARC4RANDOM 1867 interval += arc4random_uniform(rai->rai_maxinterval - 1868 rai->rai_mininterval); 1869 #else 1870 interval += random() % (rai->rai_maxinterval - 1871 rai->rai_mininterval); 1872 #endif 1873 break; 1874 case IFI_STATE_TRANSITIVE: 1875 /* 1876 * For the first few advertisements (up to 1877 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen 1878 * interval is greater than 1879 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be 1880 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead. (RFC 1881 * 4861 6.2.4) 1882 * 1883 * In such cases, the router SHOULD transmit one or more 1884 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final 1885 * multicast Router Advertisements on the interface with a 1886 * Router Lifetime field of zero. (RFC 4861 6.2.5) 1887 */ 1888 interval = ifi->ifi_burstinterval; 1889 break; 1890 } 1891 1892 tm->tv_sec = interval; 1893 tm->tv_usec = 0; 1894 1895 syslog(LOG_DEBUG, 1896 "<%s> RA timer on %s is set to %ld:%ld", 1897 __func__, ifi->ifi_ifname, 1898 (long int)tm->tv_sec, (long int)tm->tv_usec); 1899 1900 return; 1901 } 1902