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