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