1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include "defs.h" 29 #include "tables.h" 30 #include <fcntl.h> 31 32 static void initlog(void); 33 static void run_timeouts(void); 34 35 static void advertise(struct sockaddr_in6 *sin6, struct phyint *pi, 36 boolean_t no_prefixes); 37 static void solicit(struct sockaddr_in6 *sin6, struct phyint *pi); 38 static void initifs(boolean_t first); 39 static void check_if_removed(struct phyint *pi); 40 static void loopback_ra_enqueue(struct phyint *pi, 41 struct nd_router_advert *ra, int len); 42 static void loopback_ra_dequeue(void); 43 static void check_daemonize(void); 44 45 struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0, 46 0x0, 0x0, 0x0, 0x0, 47 0x0, 0x0, 0x0, 0x0, 48 0x0, 0x0, 0x0, 0x1 } }; 49 50 struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0, 51 0x0, 0x0, 0x0, 0x0, 52 0x0, 0x0, 0x0, 0x0, 53 0x0, 0x0, 0x0, 0x2 } }; 54 55 static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0, 56 { 0xff, 0x2, 0x0, 0x0, 57 0x0, 0x0, 0x0, 0x0, 58 0x0, 0x0, 0x0, 0x0, 59 0x0, 0x0, 0x0, 0x1 } }; 60 61 static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0, 62 { 0xff, 0x2, 0x0, 0x0, 63 0x0, 0x0, 0x0, 0x0, 64 0x0, 0x0, 0x0, 0x0, 65 0x0, 0x0, 0x0, 0x2 } }; 66 67 static char **argv0; /* Saved for re-exec on SIGHUP */ 68 69 static uint64_t packet[(IP_MAXPACKET + 1)/8]; 70 71 static int show_ifs = 0; 72 static boolean_t already_daemonized = _B_FALSE; 73 int debug = 0; 74 int no_loopback = 0; /* Do not send RA packets to ourselves */ 75 76 /* 77 * Size of routing socket message used by in.ndpd which includes the header, 78 * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6) 79 * plus space for the RTA_IFP (a sockaddr_dl). 80 */ 81 #define NDP_RTM_MSGLEN sizeof (struct rt_msghdr) + \ 82 sizeof (struct sockaddr_in6) + \ 83 sizeof (struct sockaddr_in6) + \ 84 sizeof (struct sockaddr_in6) + \ 85 sizeof (struct sockaddr_dl) 86 87 /* 88 * These are referenced externally in tables.c in order to fill in the 89 * dynamic portions of the routing socket message and then to send the message 90 * itself. 91 */ 92 int rtsock = -1; /* Routing socket */ 93 struct rt_msghdr *rt_msg; /* Routing socket message */ 94 struct sockaddr_in6 *rta_gateway; /* RTA_GATEWAY sockaddr */ 95 struct sockaddr_dl *rta_ifp; /* RTA_IFP sockaddr */ 96 97 /* 98 * Return the current time in milliseconds truncated to 99 * fit in an integer. 100 */ 101 uint_t 102 getcurrenttime(void) 103 { 104 struct timeval tp; 105 106 if (gettimeofday(&tp, NULL) < 0) { 107 logperror("getcurrenttime: gettimeofday failed"); 108 exit(1); 109 } 110 return (tp.tv_sec * 1000 + tp.tv_usec / 1000); 111 } 112 113 /* 114 * Output a preformated packet from the packet[] buffer. 115 */ 116 static void 117 sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags) 118 { 119 int cc; 120 char abuf[INET6_ADDRSTRLEN]; 121 122 cc = sendto(sock, (char *)packet, size, flags, 123 (struct sockaddr *)sin6, sizeof (*sin6)); 124 if (cc < 0 || cc != size) { 125 if (cc < 0) { 126 logperror("sendpacket: sendto"); 127 } 128 logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n", 129 inet_ntop(sin6->sin6_family, 130 (void *)&sin6->sin6_addr, 131 abuf, sizeof (abuf)), 132 size, cc); 133 } 134 } 135 136 /* Send a Router Solicitation */ 137 static void 138 solicit(struct sockaddr_in6 *sin6, struct phyint *pi) 139 { 140 int packetlen = 0; 141 struct nd_router_solicit *rs = (struct nd_router_solicit *)packet; 142 char *pptr = (char *)packet; 143 144 rs->nd_rs_type = ND_ROUTER_SOLICIT; 145 rs->nd_rs_code = 0; 146 rs->nd_rs_cksum = htons(0); 147 rs->nd_rs_reserved = htonl(0); 148 149 packetlen += sizeof (*rs); 150 pptr += sizeof (*rs); 151 152 /* Attach any options */ 153 if (pi->pi_hdw_addr_len != 0) { 154 struct nd_opt_lla *lo = (struct nd_opt_lla *)pptr; 155 int optlen; 156 157 /* roundup to multiple of 8 and make padding zero */ 158 optlen = ((sizeof (struct nd_opt_hdr) + 159 pi->pi_hdw_addr_len + 7) / 8) * 8; 160 bzero(pptr, optlen); 161 162 lo->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR; 163 lo->nd_opt_lla_len = optlen / 8; 164 bcopy((char *)pi->pi_hdw_addr, 165 (char *)lo->nd_opt_lla_hdw_addr, 166 pi->pi_hdw_addr_len); 167 packetlen += optlen; 168 pptr += optlen; 169 } 170 171 if (debug & D_PKTOUT) { 172 print_route_sol("Sending solicitation to ", pi, rs, packetlen, 173 sin6); 174 } 175 sendpacket(sin6, pi->pi_sock, packetlen, 0); 176 } 177 178 /* 179 * Send a (set of) Router Advertisements and feed them back to ourselves 180 * for processing. Unless no_prefixes is set all prefixes are included. 181 * If there are too many prefix options to fit in one packet multiple 182 * packets will be sent - each containing a subset of the prefix options. 183 */ 184 static void 185 advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes) 186 { 187 struct nd_opt_prefix_info *po; 188 char *pptr = (char *)packet; 189 struct nd_router_advert *ra; 190 struct adv_prefix *adv_pr; 191 int packetlen = 0; 192 193 ra = (struct nd_router_advert *)pptr; 194 ra->nd_ra_type = ND_ROUTER_ADVERT; 195 ra->nd_ra_code = 0; 196 ra->nd_ra_cksum = htons(0); 197 ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit; 198 ra->nd_ra_flags_reserved = 0; 199 if (pi->pi_AdvManagedFlag) 200 ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED; 201 if (pi->pi_AdvOtherConfigFlag) 202 ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER; 203 204 if (pi->pi_adv_state == FINAL_ADV) 205 ra->nd_ra_router_lifetime = htons(0); 206 else 207 ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime); 208 ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime); 209 ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer); 210 211 packetlen = sizeof (*ra); 212 pptr += sizeof (*ra); 213 214 if (pi->pi_adv_state == FINAL_ADV) { 215 if (debug & D_PKTOUT) { 216 print_route_adv("Sending advert (FINAL) to ", pi, 217 ra, packetlen, sin6); 218 } 219 sendpacket(sin6, pi->pi_sock, packetlen, 0); 220 /* Feed packet back in for router operation */ 221 loopback_ra_enqueue(pi, ra, packetlen); 222 return; 223 } 224 225 /* Attach any options */ 226 if (pi->pi_hdw_addr_len != 0) { 227 struct nd_opt_lla *lo = (struct nd_opt_lla *)pptr; 228 int optlen; 229 230 /* roundup to multiple of 8 and make padding zero */ 231 optlen = ((sizeof (struct nd_opt_hdr) + 232 pi->pi_hdw_addr_len + 7) / 8) * 8; 233 bzero(pptr, optlen); 234 235 lo->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR; 236 lo->nd_opt_lla_len = optlen / 8; 237 bcopy((char *)pi->pi_hdw_addr, 238 (char *)lo->nd_opt_lla_hdw_addr, 239 pi->pi_hdw_addr_len); 240 packetlen += optlen; 241 pptr += optlen; 242 } 243 244 if (pi->pi_AdvLinkMTU != 0) { 245 struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr; 246 247 mo->nd_opt_mtu_type = ND_OPT_MTU; 248 mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8; 249 mo->nd_opt_mtu_reserved = 0; 250 mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU); 251 252 packetlen += sizeof (struct nd_opt_mtu); 253 pptr += sizeof (struct nd_opt_mtu); 254 } 255 256 if (no_prefixes) { 257 if (debug & D_PKTOUT) { 258 print_route_adv("Sending advert to ", pi, 259 ra, packetlen, sin6); 260 } 261 sendpacket(sin6, pi->pi_sock, packetlen, 0); 262 /* Feed packet back in for router operation */ 263 loopback_ra_enqueue(pi, ra, packetlen); 264 return; 265 } 266 267 po = (struct nd_opt_prefix_info *)pptr; 268 for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL; 269 adv_pr = adv_pr->adv_pr_next) { 270 if (!adv_pr->adv_pr_AdvOnLinkFlag && 271 !adv_pr->adv_pr_AdvAutonomousFlag) { 272 continue; 273 } 274 275 /* 276 * If the prefix doesn't fit in packet send 277 * what we have so far and start with new packet. 278 */ 279 if (packetlen + sizeof (*po) > 280 pi->pi_LinkMTU - sizeof (struct ip6_hdr)) { 281 if (debug & D_PKTOUT) { 282 print_route_adv("Sending advert " 283 "(FRAG) to ", 284 pi, ra, packetlen, sin6); 285 } 286 sendpacket(sin6, pi->pi_sock, packetlen, 0); 287 /* Feed packet back in for router operation */ 288 loopback_ra_enqueue(pi, ra, packetlen); 289 packetlen = sizeof (*ra); 290 pptr = (char *)packet + sizeof (*ra); 291 po = (struct nd_opt_prefix_info *)pptr; 292 } 293 po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 294 po->nd_opt_pi_len = sizeof (*po)/8; 295 po->nd_opt_pi_flags_reserved = 0; 296 if (adv_pr->adv_pr_AdvOnLinkFlag) { 297 po->nd_opt_pi_flags_reserved |= 298 ND_OPT_PI_FLAG_ONLINK; 299 } 300 if (adv_pr->adv_pr_AdvAutonomousFlag) { 301 po->nd_opt_pi_flags_reserved |= 302 ND_OPT_PI_FLAG_AUTO; 303 } 304 po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len; 305 /* 306 * If both Adv*Expiration and Adv*Lifetime are 307 * set we prefer the former and make the lifetime 308 * decrement in real time. 309 */ 310 if (adv_pr->adv_pr_AdvValidRealTime) { 311 po->nd_opt_pi_valid_time = 312 htonl(adv_pr->adv_pr_AdvValidExpiration); 313 } else { 314 po->nd_opt_pi_valid_time = 315 htonl(adv_pr->adv_pr_AdvValidLifetime); 316 } 317 if (adv_pr->adv_pr_AdvPreferredRealTime) { 318 po->nd_opt_pi_preferred_time = 319 htonl(adv_pr->adv_pr_AdvPreferredExpiration); 320 } else { 321 po->nd_opt_pi_preferred_time = 322 htonl(adv_pr->adv_pr_AdvPreferredLifetime); 323 } 324 po->nd_opt_pi_reserved2 = htonl(0); 325 po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix; 326 327 po++; 328 packetlen += sizeof (*po); 329 } 330 if (debug & D_PKTOUT) { 331 print_route_adv("Sending advert to ", pi, 332 ra, packetlen, sin6); 333 } 334 sendpacket(sin6, pi->pi_sock, packetlen, 0); 335 /* Feed packet back in for router operation */ 336 loopback_ra_enqueue(pi, ra, packetlen); 337 } 338 339 /* Poll support */ 340 static int pollfd_num = 0; /* Allocated and initialized */ 341 static struct pollfd *pollfds = NULL; 342 343 /* 344 * Add fd to the set being polled. Returns 0 if ok; -1 if failed. 345 */ 346 int 347 poll_add(int fd) 348 { 349 int i; 350 int new_num; 351 struct pollfd *newfds; 352 retry: 353 /* Check if already present */ 354 for (i = 0; i < pollfd_num; i++) { 355 if (pollfds[i].fd == fd) 356 return (0); 357 } 358 /* Check for empty spot already present */ 359 for (i = 0; i < pollfd_num; i++) { 360 if (pollfds[i].fd == -1) { 361 pollfds[i].fd = fd; 362 return (0); 363 } 364 } 365 366 /* Allocate space for 32 more fds and initialize to -1 */ 367 new_num = pollfd_num + 32; 368 newfds = realloc(pollfds, new_num * sizeof (struct pollfd)); 369 if (newfds == NULL) { 370 logperror("poll_add: realloc"); 371 return (-1); 372 } 373 for (i = pollfd_num; i < new_num; i++) { 374 newfds[i].fd = -1; 375 newfds[i].events = POLLIN; 376 } 377 pollfd_num = new_num; 378 pollfds = newfds; 379 goto retry; 380 } 381 382 /* 383 * Remove fd from the set being polled. Returns 0 if ok; -1 if failed. 384 */ 385 int 386 poll_remove(int fd) 387 { 388 int i; 389 390 /* Check if already present */ 391 for (i = 0; i < pollfd_num; i++) { 392 if (pollfds[i].fd == fd) { 393 pollfds[i].fd = -1; 394 return (0); 395 } 396 } 397 return (-1); 398 } 399 400 /* 401 * Extract information about the ifname (either a physical interface and 402 * the ":0" logical interface or just a logical interface). 403 * If the interface (still) exists in kernel set pr_in_use 404 * for caller to be able to detect interfaces that are removed. 405 * Starts sending advertisements/solicitations when new physical interfaces 406 * are detected. 407 */ 408 static void 409 if_process(int s, char *ifname, boolean_t first) 410 { 411 struct lifreq lifr; 412 struct phyint *pi; 413 struct prefix *pr; 414 char *cp; 415 char phyintname[LIFNAMSIZ + 1]; 416 417 if (debug & D_IFSCAN) 418 logmsg(LOG_DEBUG, "if_process(%s)\n", ifname); 419 420 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 421 lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0'; 422 if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) { 423 if (errno == ENXIO) { 424 /* 425 * Interface has disappeared 426 */ 427 return; 428 } 429 logperror("if_process: ioctl (get interface flags)"); 430 return; 431 } 432 433 /* 434 * Ignore loopback and point-to-multipoint interfaces. 435 * Point-to-point interfaces always have IFF_MULTICAST set. 436 */ 437 if (!(lifr.lifr_flags & IFF_MULTICAST) || 438 (lifr.lifr_flags & IFF_LOOPBACK)) { 439 return; 440 } 441 442 if (!(lifr.lifr_flags & IFF_IPV6)) 443 return; 444 445 (void) strncpy(phyintname, ifname, sizeof (phyintname)); 446 phyintname[sizeof (phyintname) - 1] = '\0'; 447 if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) { 448 *cp = '\0'; 449 } 450 451 pi = phyint_lookup(phyintname); 452 if (pi == NULL) { 453 /* 454 * Do not add anything for new interfaces until they are UP. 455 * For existing interfaces we track the up flag. 456 */ 457 if (!(lifr.lifr_flags & IFF_UP)) 458 return; 459 460 pi = phyint_create(phyintname); 461 if (pi == NULL) { 462 logmsg(LOG_ERR, "if_process: out of memory\n"); 463 return; 464 } 465 } 466 (void) phyint_init_from_k(pi); 467 if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) { 468 /* Interface is not yet present */ 469 if (debug & D_PHYINT) { 470 logmsg(LOG_DEBUG, "if_process: interface not yet " 471 "present %s\n", pi->pi_name); 472 } 473 return; 474 } 475 476 if (pi->pi_sock != -1) { 477 if (poll_add(pi->pi_sock) == -1) { 478 /* 479 * reset state. 480 */ 481 phyint_cleanup(pi); 482 } 483 } 484 485 /* 486 * Check if IFF_ROUTER has been turned off in kernel in which 487 * case we have to turn off AdvSendAdvertisements. 488 * The kernel will automatically turn off IFF_ROUTER if 489 * ip6_forwarding is turned off. 490 * Note that we do not switch back should IFF_ROUTER be turned on. 491 */ 492 if (!first && 493 pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) { 494 logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name); 495 check_to_advertise(pi, START_FINAL_ADV); 496 497 pi->pi_AdvSendAdvertisements = 0; 498 pi->pi_sol_state = NO_SOLICIT; 499 } 500 501 /* 502 * Send advertisments and solicitation only if the interface is 503 * present in the kernel. 504 */ 505 if (pi->pi_kernel_state & PI_PRESENT) { 506 507 if (pi->pi_AdvSendAdvertisements) { 508 if (pi->pi_adv_state == NO_ADV) 509 check_to_advertise(pi, START_INIT_ADV); 510 } else { 511 if (pi->pi_sol_state == NO_SOLICIT) 512 check_to_solicit(pi, START_INIT_SOLICIT); 513 } 514 } 515 516 /* 517 * Track static kernel prefixes to prevent in.ndpd from clobbering 518 * them by creating a struct prefix for each prefix detected in the 519 * kernel. 520 */ 521 pr = prefix_lookup_name(pi, ifname); 522 if (pr == NULL) { 523 pr = prefix_create_name(pi, ifname); 524 if (pr == NULL) { 525 logmsg(LOG_ERR, "if_process: out of memory\n"); 526 return; 527 } 528 if (prefix_init_from_k(pr) == -1) { 529 prefix_delete(pr); 530 return; 531 } 532 } 533 /* Detect prefixes which are removed */ 534 if (pr->pr_kernel_state != 0) 535 pr->pr_in_use = _B_TRUE; 536 } 537 538 static int ifsock = -1; 539 540 /* 541 * Scan all interfaces to detect changes as well as new and deleted intefaces 542 * 'first' is set for the initial call only. Do not effect anything. 543 */ 544 static void 545 initifs(boolean_t first) 546 { 547 char *buf; 548 int bufsize; 549 int numifs; 550 int n; 551 struct lifnum lifn; 552 struct lifconf lifc; 553 struct lifreq *lifr; 554 struct phyint *pi; 555 struct phyint *next_pi; 556 struct prefix *pr; 557 558 if (debug & D_IFSCAN) 559 logmsg(LOG_DEBUG, "Reading interface configuration\n"); 560 if (ifsock < 0) { 561 ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 562 if (ifsock < 0) { 563 logperror("initifs: socket"); 564 return; 565 } 566 } 567 lifn.lifn_family = AF_INET6; 568 lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY; 569 if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) { 570 logperror("initifs: ioctl (get interface numbers)"); 571 return; 572 } 573 numifs = lifn.lifn_count; 574 bufsize = numifs * sizeof (struct lifreq); 575 576 buf = (char *)malloc(bufsize); 577 if (buf == NULL) { 578 logmsg(LOG_ERR, "initifs: out of memory\n"); 579 return; 580 } 581 582 /* 583 * Mark the interfaces so that we can find phyints and prefixes 584 * which have disappeared from the kernel. 585 * if_process will set pr_in_use when it finds the interface 586 * in the kernel. 587 */ 588 for (pi = phyints; pi != NULL; pi = pi->pi_next) { 589 /* 590 * Before re-examining the state of the interfaces, 591 * PI_PRESENT should be cleared from pi_kernel_state. 592 */ 593 pi->pi_kernel_state &= ~PI_PRESENT; 594 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 595 pr->pr_in_use = _B_FALSE; 596 } 597 } 598 599 lifc.lifc_family = AF_INET6; 600 lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY; 601 lifc.lifc_len = bufsize; 602 lifc.lifc_buf = buf; 603 604 if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) { 605 logperror("initifs: ioctl (get interface configuration)"); 606 free(buf); 607 return; 608 } 609 610 lifr = (struct lifreq *)lifc.lifc_req; 611 for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++) 612 if_process(ifsock, lifr->lifr_name, first); 613 free(buf); 614 615 /* 616 * Detect phyints that have been removed from the kernel. 617 * Since we can't recreate it here (would require ifconfig plumb 618 * logic) we just terminate use of that phyint. 619 */ 620 for (pi = phyints; pi != NULL; pi = next_pi) { 621 next_pi = pi->pi_next; 622 /* 623 * If interface (still) exists in kernel, set 624 * pi_state to indicate that. 625 */ 626 if (pi->pi_kernel_state & PI_PRESENT) { 627 pi->pi_state |= PI_PRESENT; 628 } 629 630 check_if_removed(pi); 631 } 632 if (show_ifs) 633 phyint_print_all(); 634 } 635 636 637 /* 638 * Router advertisement state machine. Used for everything but timer 639 * events which use advertise_event directly. 640 */ 641 void 642 check_to_advertise(struct phyint *pi, enum adv_events event) 643 { 644 uint_t delay; 645 enum adv_states old_state = pi->pi_adv_state; 646 647 if (debug & D_STATE) { 648 logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n", 649 pi->pi_name, (int)event, (int)old_state); 650 } 651 delay = advertise_event(pi, event, 0); 652 if (delay != TIMER_INFINITY) { 653 /* Make sure the global next event is updated */ 654 timer_schedule(delay); 655 } 656 657 if (debug & D_STATE) { 658 logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n", 659 pi->pi_name, (int)event, (int)old_state, 660 (int)pi->pi_adv_state); 661 } 662 } 663 664 /* 665 * Router advertisement state machine. 666 * Return the number of milliseconds until next timeout (TIMER_INFINITY 667 * if never). 668 * For the ADV_TIMER event the caller passes in the number of milliseconds 669 * since the last timer event in the 'elapsed' parameter. 670 */ 671 uint_t 672 advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed) 673 { 674 uint_t delay; 675 676 if (debug & D_STATE) { 677 logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n", 678 pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state); 679 } 680 check_daemonize(); 681 if (!pi->pi_AdvSendAdvertisements) 682 return (TIMER_INFINITY); 683 if (pi->pi_flags & IFF_NORTEXCH) { 684 if (debug & D_PKTOUT) { 685 logmsg(LOG_DEBUG, "Suppress sending RA packet on %s " 686 "(no route exchange on interface)\n", 687 pi->pi_name); 688 } 689 return (TIMER_INFINITY); 690 } 691 692 switch (event) { 693 case ADV_OFF: 694 pi->pi_adv_state = NO_ADV; 695 return (TIMER_INFINITY); 696 697 case START_INIT_ADV: 698 if (pi->pi_adv_state == INIT_ADV) 699 return (pi->pi_adv_time_left); 700 pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS; 701 pi->pi_adv_time_left = 0; 702 pi->pi_adv_state = INIT_ADV; 703 break; /* send advertisement */ 704 705 case START_FINAL_ADV: 706 if (pi->pi_adv_state == NO_ADV) 707 return (TIMER_INFINITY); 708 if (pi->pi_adv_state == FINAL_ADV) 709 return (pi->pi_adv_time_left); 710 pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS; 711 pi->pi_adv_time_left = 0; 712 pi->pi_adv_state = FINAL_ADV; 713 break; /* send advertisement */ 714 715 case RECEIVED_SOLICIT: 716 if (pi->pi_adv_state == NO_ADV) 717 return (TIMER_INFINITY); 718 if (pi->pi_adv_state == SOLICIT_ADV) { 719 if (pi->pi_adv_time_left != 0) 720 return (pi->pi_adv_time_left); 721 break; 722 } 723 delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME); 724 if (delay < pi->pi_adv_time_left) 725 pi->pi_adv_time_left = delay; 726 if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) { 727 /* 728 * Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS 729 * plus random delay) after the previous 730 * advertisement was sent. 731 */ 732 pi->pi_adv_time_left = delay + 733 ND_MIN_DELAY_BETWEEN_RAS - 734 pi->pi_adv_time_since_sent; 735 } 736 pi->pi_adv_state = SOLICIT_ADV; 737 break; 738 739 case ADV_TIMER: 740 if (pi->pi_adv_state == NO_ADV) 741 return (TIMER_INFINITY); 742 /* Decrease time left */ 743 if (pi->pi_adv_time_left >= elapsed) 744 pi->pi_adv_time_left -= elapsed; 745 else 746 pi->pi_adv_time_left = 0; 747 748 /* Increase time since last advertisement was sent */ 749 pi->pi_adv_time_since_sent += elapsed; 750 break; 751 default: 752 logmsg(LOG_ERR, "advertise_event: Unknown event %d\n", 753 (int)event); 754 return (TIMER_INFINITY); 755 } 756 757 if (pi->pi_adv_time_left != 0) 758 return (pi->pi_adv_time_left); 759 760 /* Send advertisement and calculate next time to send */ 761 if (pi->pi_adv_state == FINAL_ADV) { 762 /* Omit the prefixes */ 763 advertise(&v6allnodes, pi, _B_TRUE); 764 } else { 765 advertise(&v6allnodes, pi, _B_FALSE); 766 } 767 pi->pi_adv_time_since_sent = 0; 768 769 switch (pi->pi_adv_state) { 770 case SOLICIT_ADV: 771 /* 772 * The solicited advertisement has been sent. 773 * Revert to periodic advertisements. 774 */ 775 pi->pi_adv_state = REG_ADV; 776 /* FALLTHRU */ 777 case REG_ADV: 778 pi->pi_adv_time_left = 779 GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 780 1000 * pi->pi_MaxRtrAdvInterval); 781 break; 782 783 case INIT_ADV: 784 if (--pi->pi_adv_count > 0) { 785 delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 786 1000 * pi->pi_MaxRtrAdvInterval); 787 if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL) 788 delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL; 789 pi->pi_adv_time_left = delay; 790 } else { 791 pi->pi_adv_time_left = 792 GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 793 1000 * pi->pi_MaxRtrAdvInterval); 794 pi->pi_adv_state = REG_ADV; 795 } 796 break; 797 798 case FINAL_ADV: 799 if (--pi->pi_adv_count > 0) { 800 pi->pi_adv_time_left = 801 ND_MAX_INITIAL_RTR_ADVERT_INTERVAL; 802 } else { 803 pi->pi_adv_state = NO_ADV; 804 } 805 break; 806 } 807 if (pi->pi_adv_state != NO_ADV) 808 return (pi->pi_adv_time_left); 809 else 810 return (TIMER_INFINITY); 811 } 812 813 /* 814 * Router solicitation state machine. Used for everything but timer 815 * events which use solicit_event directly. 816 */ 817 void 818 check_to_solicit(struct phyint *pi, enum solicit_events event) 819 { 820 uint_t delay; 821 enum solicit_states old_state = pi->pi_sol_state; 822 823 if (debug & D_STATE) { 824 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n", 825 pi->pi_name, (int)event, (int)old_state); 826 } 827 delay = solicit_event(pi, event, 0); 828 if (delay != TIMER_INFINITY) { 829 /* Make sure the global next event is updated */ 830 timer_schedule(delay); 831 } 832 833 if (debug & D_STATE) { 834 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n", 835 pi->pi_name, (int)event, (int)old_state, 836 (int)pi->pi_sol_state); 837 } 838 } 839 840 static void 841 daemonize_ndpd(void) 842 { 843 FILE *pidfp; 844 mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */ 845 struct itimerval it; 846 boolean_t timerval = _B_TRUE; 847 848 /* 849 * Need to get current timer settings so they can be restored 850 * after the fork(), as the it_value and it_interval values for 851 * the ITIMER_REAL timer are reset to 0 in the child process. 852 */ 853 if (getitimer(ITIMER_REAL, &it) < 0) { 854 if (debug & D_TIMER) 855 logmsg(LOG_DEBUG, 856 "daemonize_ndpd: failed to get itimerval\n"); 857 timerval = _B_FALSE; 858 } 859 860 /* Daemonize. */ 861 switch (fork()) { 862 case 0: 863 /* Child */ 864 break; 865 case -1: 866 logperror("fork"); 867 exit(1); 868 default: 869 /* Parent */ 870 _exit(0); 871 } 872 873 /* Store our process id, blow away any existing file if it exists. */ 874 if ((pidfp = fopen(PATH_PID, "w")) == NULL) { 875 (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n", 876 argv0[0], strerror(errno)); 877 } else { 878 (void) fprintf(pidfp, "%ld\n", getpid()); 879 (void) fclose(pidfp); 880 (void) chmod(PATH_PID, pidmode); 881 } 882 883 (void) close(0); 884 (void) close(1); 885 (void) close(2); 886 887 (void) chdir("/"); 888 (void) open("/dev/null", O_RDWR); 889 (void) dup2(0, 1); 890 (void) dup2(0, 2); 891 (void) setsid(); 892 893 already_daemonized = _B_TRUE; 894 895 /* 896 * Restore timer values, if we were able to save them; if not, 897 * check and set the right value by calling run_timeouts(). 898 */ 899 if (timerval) { 900 if (setitimer(ITIMER_REAL, &it, NULL) < 0) { 901 logperror("daemonize_ndpd: setitimer"); 902 exit(2); 903 } 904 } else { 905 run_timeouts(); 906 } 907 } 908 909 /* 910 * Check to see if the time is right to daemonize. The right time is when: 911 * 912 * 1. We haven't already daemonized. 913 * 2. We are not in debug mode. 914 * 3. All interfaces are marked IFF_NOXMIT. 915 * 4. All non-router interfaces have their prefixes set up and we're 916 * done sending router solicitations on those interfaces without 917 * prefixes. 918 */ 919 static void 920 check_daemonize(void) 921 { 922 struct phyint *pi; 923 924 if (already_daemonized || debug != 0) 925 return; 926 927 for (pi = phyints; pi != NULL; pi = pi->pi_next) { 928 if (!(pi->pi_flags & IFF_NOXMIT)) 929 break; 930 } 931 932 /* 933 * If we can't transmit on any of the interfaces there is no reason 934 * to hold up progress. 935 */ 936 if (pi == NULL) { 937 daemonize_ndpd(); 938 return; 939 } 940 941 /* Check all interfaces. If any are still soliciting, just return. */ 942 for (pi = phyints; pi != NULL; pi = pi->pi_next) { 943 if (pi->pi_AdvSendAdvertisements || 944 !(pi->pi_kernel_state & PI_PRESENT)) 945 continue; 946 947 if (pi->pi_sol_state == INIT_SOLICIT) 948 return; 949 } 950 951 daemonize_ndpd(); 952 } 953 954 /* 955 * Router solicitation state machine. 956 * Return the number of milliseconds until next timeout (TIMER_INFINITY 957 * if never). 958 * For the SOL_TIMER event the caller passes in the number of milliseconds 959 * since the last timer event in the 'elapsed' parameter. 960 */ 961 uint_t 962 solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed) 963 { 964 if (debug & D_STATE) { 965 logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n", 966 pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state); 967 } 968 969 if (pi->pi_AdvSendAdvertisements) 970 return (TIMER_INFINITY); 971 if (pi->pi_flags & IFF_NORTEXCH) { 972 if (debug & D_PKTOUT) { 973 logmsg(LOG_DEBUG, "Suppress sending RS packet on %s " 974 "(no route exchange on interface)\n", 975 pi->pi_name); 976 } 977 return (TIMER_INFINITY); 978 } 979 980 switch (event) { 981 case SOLICIT_OFF: 982 pi->pi_sol_state = NO_SOLICIT; 983 check_daemonize(); 984 return (TIMER_INFINITY); 985 986 case SOLICIT_DONE: 987 pi->pi_sol_state = DONE_SOLICIT; 988 check_daemonize(); 989 return (TIMER_INFINITY); 990 991 case START_INIT_SOLICIT: 992 if (pi->pi_sol_state == INIT_SOLICIT) 993 return (pi->pi_sol_time_left); 994 pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS; 995 pi->pi_sol_time_left = 996 GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY); 997 pi->pi_sol_state = INIT_SOLICIT; 998 break; 999 1000 case SOL_TIMER: 1001 if (pi->pi_sol_state == NO_SOLICIT) 1002 return (TIMER_INFINITY); 1003 /* Decrease time left */ 1004 if (pi->pi_sol_time_left >= elapsed) 1005 pi->pi_sol_time_left -= elapsed; 1006 else 1007 pi->pi_sol_time_left = 0; 1008 break; 1009 default: 1010 logmsg(LOG_ERR, "solicit_event: Unknown event %d\n", 1011 (int)event); 1012 return (TIMER_INFINITY); 1013 } 1014 1015 if (pi->pi_sol_time_left != 0) 1016 return (pi->pi_sol_time_left); 1017 1018 /* Send solicitation and calculate next time */ 1019 switch (pi->pi_sol_state) { 1020 case INIT_SOLICIT: 1021 solicit(&v6allrouters, pi); 1022 if (--pi->pi_sol_count == 0) { 1023 pi->pi_sol_state = DONE_SOLICIT; 1024 check_daemonize(); 1025 return (TIMER_INFINITY); 1026 } 1027 pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL; 1028 return (pi->pi_sol_time_left); 1029 case NO_SOLICIT: 1030 case DONE_SOLICIT: 1031 return (TIMER_INFINITY); 1032 default: 1033 return (pi->pi_sol_time_left); 1034 } 1035 } 1036 1037 /* 1038 * Timer mechanism using relative time (in milliseconds) from the 1039 * previous timer event. Timers exceeding TIMER_INFINITY milliseconds 1040 * will fire after TIMER_INFINITY milliseconds. 1041 */ 1042 static uint_t timer_previous; /* When last SIGALRM occurred */ 1043 static uint_t timer_next; /* Currently scheduled timeout */ 1044 1045 static void 1046 timer_init(void) 1047 { 1048 timer_previous = getcurrenttime(); 1049 timer_next = TIMER_INFINITY; 1050 run_timeouts(); 1051 } 1052 1053 /* 1054 * Make sure the next SIGALRM occurs delay milliseconds from the current 1055 * time if not earlier. 1056 * Handles getcurrenttime (32 bit integer holding milliseconds) wraparound 1057 * by treating differences greater than 0x80000000 as negative. 1058 */ 1059 void 1060 timer_schedule(uint_t delay) 1061 { 1062 uint_t now; 1063 struct itimerval itimerval; 1064 1065 now = getcurrenttime(); 1066 if (debug & D_TIMER) { 1067 logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n", 1068 delay, now, timer_next); 1069 } 1070 /* Will this timer occur before the currently scheduled SIGALRM? */ 1071 if (delay >= timer_next - now) { 1072 if (debug & D_TIMER) { 1073 logmsg(LOG_DEBUG, "timer_schedule(%u): no action - " 1074 "next in %u ms\n", 1075 delay, timer_next - now); 1076 } 1077 return; 1078 } 1079 if (delay == 0) { 1080 /* Minimum allowed delay */ 1081 delay = 1; 1082 } 1083 timer_next = now + delay; 1084 1085 itimerval.it_value.tv_sec = delay / 1000; 1086 itimerval.it_value.tv_usec = (delay % 1000) * 1000; 1087 itimerval.it_interval.tv_sec = 0; 1088 itimerval.it_interval.tv_usec = 0; 1089 if (debug & D_TIMER) { 1090 logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n", 1091 delay, 1092 itimerval.it_value.tv_sec, itimerval.it_value.tv_usec); 1093 } 1094 if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) { 1095 logperror("timer_schedule: setitimer"); 1096 exit(2); 1097 } 1098 } 1099 1100 /* 1101 * Conditional running of timer. If more than 'minimal_time' millseconds 1102 * since the timer routines were last run we run them. 1103 * Used when packets arrive. 1104 */ 1105 static void 1106 conditional_run_timeouts(uint_t minimal_time) 1107 { 1108 uint_t now; 1109 uint_t elapsed; 1110 1111 now = getcurrenttime(); 1112 elapsed = now - timer_previous; 1113 if (elapsed > minimal_time) { 1114 if (debug & D_TIMER) { 1115 logmsg(LOG_DEBUG, "conditional_run_timeouts: " 1116 "elapsed %d\n", elapsed); 1117 } 1118 run_timeouts(); 1119 } 1120 } 1121 1122 /* 1123 * Timer has fired. 1124 * Determine when the next timer event will occur by asking all 1125 * the timer routines. 1126 * Should not be called from a timer routine but in some cases this is 1127 * done because the code doesn't know that e.g. it was called from 1128 * ifconfig_timer(). In this case the nested run_timeouts will just return but 1129 * the running run_timeouts will ensure to call all the timer functions by 1130 * looping once more. 1131 */ 1132 static void 1133 run_timeouts(void) 1134 { 1135 uint_t now; 1136 uint_t elapsed; 1137 uint_t next; 1138 uint_t nexti; 1139 struct phyint *pi; 1140 struct phyint *next_pi; 1141 struct prefix *pr; 1142 struct prefix *next_pr; 1143 struct adv_prefix *adv_pr; 1144 struct adv_prefix *next_adv_pr; 1145 struct router *dr; 1146 struct router *next_dr; 1147 static boolean_t timeout_running; 1148 static boolean_t do_retry; 1149 1150 if (timeout_running) { 1151 if (debug & D_TIMER) 1152 logmsg(LOG_DEBUG, "run_timeouts: nested call\n"); 1153 do_retry = _B_TRUE; 1154 return; 1155 } 1156 timeout_running = _B_TRUE; 1157 retry: 1158 /* How much time since the last time we were called? */ 1159 now = getcurrenttime(); 1160 elapsed = now - timer_previous; 1161 timer_previous = now; 1162 1163 if (debug & D_TIMER) 1164 logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed); 1165 1166 next = TIMER_INFINITY; 1167 for (pi = phyints; pi != NULL; pi = next_pi) { 1168 next_pi = pi->pi_next; 1169 nexti = phyint_timer(pi, elapsed); 1170 if (nexti != TIMER_INFINITY && nexti < next) 1171 next = nexti; 1172 if (debug & D_TIMER) { 1173 logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n", 1174 pi->pi_name, nexti, next); 1175 } 1176 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 1177 next_pr = pr->pr_next; 1178 nexti = prefix_timer(pr, elapsed); 1179 if (nexti != TIMER_INFINITY && nexti < next) 1180 next = nexti; 1181 if (debug & D_TIMER) { 1182 logmsg(LOG_DEBUG, "run_timeouts (pr %s): " 1183 "%d -> %u ms\n", pr->pr_name, nexti, next); 1184 } 1185 } 1186 for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL; 1187 adv_pr = next_adv_pr) { 1188 next_adv_pr = adv_pr->adv_pr_next; 1189 nexti = adv_prefix_timer(adv_pr, elapsed); 1190 if (nexti != TIMER_INFINITY && nexti < next) 1191 next = nexti; 1192 if (debug & D_TIMER) { 1193 logmsg(LOG_DEBUG, "run_timeouts " 1194 "(adv pr on %s): %d -> %u ms\n", 1195 adv_pr->adv_pr_physical->pi_name, 1196 nexti, next); 1197 } 1198 } 1199 for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) { 1200 next_dr = dr->dr_next; 1201 nexti = router_timer(dr, elapsed); 1202 if (nexti != TIMER_INFINITY && nexti < next) 1203 next = nexti; 1204 if (debug & D_TIMER) { 1205 logmsg(LOG_DEBUG, "run_timeouts (dr): " 1206 "%d -> %u ms\n", nexti, next); 1207 } 1208 } 1209 if (pi->pi_TmpAddrsEnabled) { 1210 nexti = tmptoken_timer(pi, elapsed); 1211 if (nexti != TIMER_INFINITY && nexti < next) 1212 next = nexti; 1213 if (debug & D_TIMER) { 1214 logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): " 1215 "%d -> %u ms\n", pi->pi_name, nexti, next); 1216 } 1217 } 1218 } 1219 /* 1220 * Make sure the timer functions are run at least once 1221 * an hour. 1222 */ 1223 if (next == TIMER_INFINITY) 1224 next = 3600 * 1000; /* 1 hour */ 1225 1226 if (debug & D_TIMER) 1227 logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next); 1228 timer_schedule(next); 1229 if (do_retry) { 1230 if (debug & D_TIMER) 1231 logmsg(LOG_DEBUG, "run_timeouts: retry\n"); 1232 do_retry = _B_FALSE; 1233 goto retry; 1234 } 1235 timeout_running = _B_FALSE; 1236 } 1237 1238 static int eventpipe_read = -1; /* Used for synchronous signal delivery */ 1239 static int eventpipe_write = -1; 1240 1241 /* 1242 * Ensure that signals are processed synchronously with the rest of 1243 * the code by just writing a one character signal number on the pipe. 1244 * The poll loop will pick this up and process the signal event. 1245 */ 1246 static void 1247 sig_handler(int signo) 1248 { 1249 uchar_t buf = (uchar_t)signo; 1250 1251 if (eventpipe_write == -1) { 1252 logmsg(LOG_ERR, "sig_handler: no pipe\n"); 1253 return; 1254 } 1255 if (write(eventpipe_write, &buf, sizeof (buf)) < 0) 1256 logperror("sig_handler: write"); 1257 } 1258 1259 /* 1260 * Pick up a signal "byte" from the pipe and process it. 1261 */ 1262 static void 1263 in_signal(int fd) 1264 { 1265 uchar_t buf; 1266 struct phyint *pi; 1267 struct phyint *next_pi; 1268 1269 switch (read(fd, &buf, sizeof (buf))) { 1270 case -1: 1271 logperror("in_signal: read"); 1272 exit(1); 1273 /* NOTREACHED */ 1274 case 1: 1275 break; 1276 case 0: 1277 logmsg(LOG_ERR, "in_signal: read eof\n"); 1278 exit(1); 1279 /* NOTREACHED */ 1280 default: 1281 logmsg(LOG_ERR, "in_signal: read > 1\n"); 1282 exit(1); 1283 } 1284 1285 if (debug & D_TIMER) 1286 logmsg(LOG_DEBUG, "in_signal() got %d\n", buf); 1287 1288 switch (buf) { 1289 case SIGALRM: 1290 if (debug & D_TIMER) { 1291 uint_t now = getcurrenttime(); 1292 1293 logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n", 1294 now - timer_next); 1295 } 1296 timer_next = TIMER_INFINITY; 1297 run_timeouts(); 1298 break; 1299 case SIGHUP: 1300 /* Re-read config file by exec'ing ourselves */ 1301 for (pi = phyints; pi != NULL; pi = next_pi) { 1302 next_pi = pi->pi_next; 1303 if (pi->pi_AdvSendAdvertisements) 1304 check_to_advertise(pi, START_FINAL_ADV); 1305 1306 phyint_delete(pi); 1307 } 1308 1309 /* 1310 * Prevent fd leaks. Everything gets re-opened at start-up 1311 * time. 0, 1, and 2 are closed and re-opened as 1312 * /dev/null, so we'll leave those open. 1313 */ 1314 closefrom(3); 1315 1316 logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n"); 1317 (void) execv(argv0[0], argv0); 1318 (void) unlink(PATH_PID); 1319 _exit(0177); 1320 /* NOTREACHED */ 1321 case SIGUSR1: 1322 logmsg(LOG_DEBUG, "Printing configuration:\n"); 1323 phyint_print_all(); 1324 break; 1325 case SIGINT: 1326 case SIGTERM: 1327 case SIGQUIT: 1328 for (pi = phyints; pi != NULL; pi = next_pi) { 1329 next_pi = pi->pi_next; 1330 if (pi->pi_AdvSendAdvertisements) 1331 check_to_advertise(pi, START_FINAL_ADV); 1332 1333 phyint_delete(pi); 1334 } 1335 (void) unlink(PATH_PID); 1336 exit(0); 1337 /* NOTREACHED */ 1338 case 255: 1339 /* 1340 * Special "signal" from looback_ra_enqueue. 1341 * Handle any queued loopback router advertisements. 1342 */ 1343 loopback_ra_dequeue(); 1344 break; 1345 default: 1346 logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf); 1347 } 1348 } 1349 1350 /* 1351 * Create pipe for signal delivery and set up signal handlers. 1352 */ 1353 static void 1354 setup_eventpipe(void) 1355 { 1356 int fds[2]; 1357 struct sigaction act; 1358 1359 if ((pipe(fds)) < 0) { 1360 logperror("setup_eventpipe: pipe"); 1361 exit(1); 1362 } 1363 eventpipe_read = fds[0]; 1364 eventpipe_write = fds[1]; 1365 if (poll_add(eventpipe_read) == -1) { 1366 exit(1); 1367 } 1368 act.sa_handler = sig_handler; 1369 act.sa_flags = SA_RESTART; 1370 (void) sigaction(SIGALRM, &act, NULL); 1371 1372 (void) sigset(SIGHUP, sig_handler); 1373 (void) sigset(SIGUSR1, sig_handler); 1374 (void) sigset(SIGTERM, sig_handler); 1375 (void) sigset(SIGINT, sig_handler); 1376 (void) sigset(SIGQUIT, sig_handler); 1377 } 1378 1379 /* 1380 * Create a routing socket for receiving RTM_IFINFO messages and initialize 1381 * the routing socket message header and as much of the sockaddrs as possible. 1382 */ 1383 static int 1384 setup_rtsock(void) 1385 { 1386 int s; 1387 int ret; 1388 char *cp; 1389 struct sockaddr_in6 *sin6; 1390 1391 s = socket(PF_ROUTE, SOCK_RAW, AF_INET6); 1392 if (s == -1) { 1393 logperror("socket(PF_ROUTE)"); 1394 exit(1); 1395 } 1396 ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK); 1397 if (ret < 0) { 1398 logperror("fcntl(O_NDELAY)"); 1399 exit(1); 1400 } 1401 if (poll_add(s) == -1) { 1402 exit(1); 1403 } 1404 1405 /* 1406 * Allocate storage for the routing socket message. 1407 */ 1408 rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN); 1409 if (rt_msg == NULL) { 1410 logperror("malloc"); 1411 exit(1); 1412 } 1413 1414 /* 1415 * Initialize the routing socket message by zero-filling it and then 1416 * setting the fields where are constant through the lifetime of the 1417 * process. 1418 */ 1419 bzero(rt_msg, NDP_RTM_MSGLEN); 1420 rt_msg->rtm_msglen = NDP_RTM_MSGLEN; 1421 rt_msg->rtm_version = RTM_VERSION; 1422 rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP; 1423 rt_msg->rtm_pid = getpid(); 1424 if (rt_msg->rtm_pid < 0) { 1425 logperror("getpid"); 1426 exit(1); 1427 } 1428 1429 /* 1430 * The RTA_DST sockaddr does not change during the lifetime of the 1431 * process so it can be completely initialized at this time. 1432 */ 1433 cp = (char *)rt_msg + sizeof (struct rt_msghdr); 1434 sin6 = (struct sockaddr_in6 *)cp; 1435 sin6->sin6_family = AF_INET6; 1436 sin6->sin6_addr = in6addr_any; 1437 1438 /* 1439 * Initialize the constant portion of the RTA_GATEWAY sockaddr. 1440 */ 1441 cp += sizeof (struct sockaddr_in6); 1442 rta_gateway = (struct sockaddr_in6 *)cp; 1443 rta_gateway->sin6_family = AF_INET6; 1444 1445 /* 1446 * The RTA_NETMASK sockaddr does not change during the lifetime of the 1447 * process so it can be completely initialized at this time. 1448 */ 1449 cp += sizeof (struct sockaddr_in6); 1450 sin6 = (struct sockaddr_in6 *)cp; 1451 sin6->sin6_family = AF_INET6; 1452 sin6->sin6_addr = in6addr_any; 1453 1454 /* 1455 * Initialize the constant portion of the RTA_IFP sockaddr. 1456 */ 1457 cp += sizeof (struct sockaddr_in6); 1458 rta_ifp = (struct sockaddr_dl *)cp; 1459 rta_ifp->sdl_family = AF_LINK; 1460 1461 return (s); 1462 } 1463 1464 /* 1465 * Retrieve one routing socket message. If RTM_IFINFO indicates 1466 * new phyint do a full scan of the interfaces. If RTM_IFINFO 1467 * indicates an existing phyint only scan that phyint and asociated 1468 * prefixes. 1469 */ 1470 static void 1471 process_rtsock(int rtsock) 1472 { 1473 int n; 1474 #define MSG_SIZE 2048/8 1475 int64_t msg[MSG_SIZE]; 1476 struct rt_msghdr *rtm; 1477 struct if_msghdr *ifm; 1478 struct phyint *pi; 1479 struct prefix *pr; 1480 boolean_t need_initifs = _B_FALSE; 1481 boolean_t need_ifscan = _B_FALSE; 1482 int64_t ifscan_msg[10][MSG_SIZE]; 1483 int ifscan_index = 0; 1484 int i; 1485 1486 /* Empty the rtsock and coealesce all the work that we have */ 1487 while (ifscan_index < 10) { 1488 n = read(rtsock, msg, sizeof (msg)); 1489 if (n <= 0) { 1490 /* No more messages */ 1491 break; 1492 } 1493 rtm = (struct rt_msghdr *)msg; 1494 if (rtm->rtm_version != RTM_VERSION) { 1495 logmsg(LOG_ERR, 1496 "process_rtsock: version %d not understood\n", 1497 rtm->rtm_version); 1498 return; 1499 } 1500 switch (rtm->rtm_type) { 1501 case RTM_NEWADDR: 1502 case RTM_DELADDR: 1503 /* 1504 * Some logical interface has changed - have to scan 1505 * everything to determine what actually changed. 1506 */ 1507 if (debug & D_IFSCAN) { 1508 logmsg(LOG_DEBUG, "process_rtsock: " 1509 "message %d\n", rtm->rtm_type); 1510 } 1511 need_initifs = _B_TRUE; 1512 break; 1513 case RTM_IFINFO: 1514 need_ifscan = _B_TRUE; 1515 (void) memcpy(ifscan_msg[ifscan_index], rtm, 1516 sizeof (msg)); 1517 ifscan_index++; 1518 /* Handled below */ 1519 break; 1520 default: 1521 /* Not interesting */ 1522 break; 1523 } 1524 } 1525 /* 1526 * If we do full scan i.e initifs, we don't need to 1527 * scan a particular interface as we should have 1528 * done that as part of initifs. 1529 */ 1530 if (need_initifs) { 1531 initifs(_B_FALSE); 1532 return; 1533 } 1534 1535 if (!need_ifscan) 1536 return; 1537 1538 for (i = 0; i < ifscan_index; i++) { 1539 ifm = (struct if_msghdr *)ifscan_msg[i]; 1540 if (debug & D_IFSCAN) 1541 logmsg(LOG_DEBUG, "process_rtsock: index %d\n", 1542 ifm->ifm_index); 1543 1544 pi = phyint_lookup_on_index(ifm->ifm_index); 1545 if (pi == NULL) { 1546 /* 1547 * A new physical interface. Do a full scan of the 1548 * to catch any new logical interfaces. 1549 */ 1550 initifs(_B_FALSE); 1551 return; 1552 } 1553 1554 if (ifm->ifm_flags != pi->pi_flags) { 1555 if (debug & D_IFSCAN) { 1556 logmsg(LOG_DEBUG, "process_rtsock: clr for " 1557 "%s old flags 0x%x new flags 0x%x\n", 1558 pi->pi_name, pi->pi_flags, ifm->ifm_flags); 1559 } 1560 } 1561 1562 1563 /* 1564 * Mark the interfaces so that we can find phyints and prefixes 1565 * which have disappeared from the kernel. 1566 * if_process will set pr_in_use when it finds the 1567 * interface in the kernel. 1568 * Before re-examining the state of the interfaces, 1569 * PI_PRESENT should be cleared from pi_kernel_state. 1570 */ 1571 pi->pi_kernel_state &= ~PI_PRESENT; 1572 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 1573 pr->pr_in_use = _B_FALSE; 1574 } 1575 1576 if (ifsock < 0) { 1577 ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 1578 if (ifsock < 0) { 1579 logperror("process_rtsock: socket"); 1580 return; 1581 } 1582 } 1583 if_process(ifsock, pi->pi_name, _B_FALSE); 1584 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 1585 if_process(ifsock, pr->pr_name, _B_FALSE); 1586 } 1587 /* 1588 * If interface (still) exists in kernel, set 1589 * pi_state to indicate that. 1590 */ 1591 if (pi->pi_kernel_state & PI_PRESENT) { 1592 pi->pi_state |= PI_PRESENT; 1593 } 1594 check_if_removed(pi); 1595 if (show_ifs) 1596 phyint_print_all(); 1597 } 1598 } 1599 1600 /* 1601 * Check whether the address formed by pr->pr_prefix and pi_token 1602 * exists in the kernel. Cannot call SIOCTMYADDR/ONLINK as it 1603 * does not check for down addresses. This function should not 1604 * be called for onlink prefixes. 1605 */ 1606 static boolean_t 1607 is_address_present(struct phyint *pi, struct prefix *pr, uint64_t flags) 1608 { 1609 int s; 1610 in6_addr_t addr, *token; 1611 int i; 1612 int ret; 1613 struct sockaddr_in6 sin6; 1614 1615 s = socket(AF_INET6, SOCK_DGRAM, 0); 1616 if (s < 0) { 1617 logperror("is_address_present: socket"); 1618 /* 1619 * By returning B_TRUE, we make the caller delete 1620 * the prefix from the internal table. In the worst 1621 * case the next RA will create the prefix. 1622 */ 1623 return (_B_TRUE); 1624 } 1625 if (flags & IFF_TEMPORARY) 1626 token = &pi->pi_tmp_token; 1627 else 1628 token = &pi->pi_token; 1629 for (i = 0; i < 16; i++) { 1630 /* 1631 * prefix_create ensures that pr_prefix has all-zero 1632 * bits after prefixlen. 1633 */ 1634 addr.s6_addr[i] = pr->pr_prefix.s6_addr[i] | token->s6_addr[i]; 1635 } 1636 (void) memset(&sin6, 0, sizeof (struct sockaddr_in6)); 1637 sin6.sin6_family = AF_INET6; 1638 sin6.sin6_addr = addr; 1639 ret = bind(s, (struct sockaddr *)&sin6, sizeof (struct sockaddr_in6)); 1640 (void) close(s); 1641 if (ret < 0 && errno == EADDRNOTAVAIL) 1642 return (_B_FALSE); 1643 else 1644 return (_B_TRUE); 1645 } 1646 1647 /* 1648 * Look if the phyint or one of its prefixes have been removed from 1649 * the kernel and take appropriate action. 1650 * Uses {pi,pr}_in_use. 1651 */ 1652 static void 1653 check_if_removed(struct phyint *pi) 1654 { 1655 struct prefix *pr; 1656 struct prefix *next_pr; 1657 1658 /* 1659 * Detect phyints that have been removed from the kernel. 1660 * Since we can't recreate it here (would require ifconfig plumb 1661 * logic) we just terminate use of that phyint. 1662 */ 1663 if (!(pi->pi_kernel_state & PI_PRESENT) && 1664 (pi->pi_state & PI_PRESENT)) { 1665 logmsg(LOG_ERR, "Interface %s has been removed from kernel. " 1666 "in.ndpd will no longer use it\n", pi->pi_name); 1667 /* 1668 * Clear state so that should the phyint reappear 1669 * we will start with initial advertisements or 1670 * solicitations. 1671 */ 1672 phyint_cleanup(pi); 1673 } 1674 /* 1675 * Detect prefixes which are removed. 1676 * 1677 * We remove the prefix in all of the following cases : 1678 * 1679 * 1) Static prefixes are not the ones we create. So, 1680 * just remove it from our tables. 1681 * 1682 * 2) On-link prefixes potentially move to a different 1683 * phyint during failover. As it does not have 1684 * an address, we can't use the logic in is_address_present 1685 * to detect whether it is present in the kernel or not. 1686 * Thus when it is manually removed we don't recreate it. 1687 * 1688 * 3) If there is a token mis-match and this prefix is not 1689 * in the kernel, it means we don't need this prefix on 1690 * this interface anymore. It must have been moved to a 1691 * different interface by in.mpathd. This normally 1692 * happens after a failover followed by a failback (or 1693 * another failover) and we re-read the network 1694 * configuration. For the failover from A to B, we would 1695 * have created state on B about A's address, which will 1696 * not be in use after the subsequent failback. So, we 1697 * remove that prefix here. 1698 * 1699 * 4) If the physical interface is not present, then remove 1700 * the prefix. In the cases where we are advertising 1701 * prefixes, the state is kept in advertisement prefix and 1702 * hence we can delete the prefix. 1703 * 1704 * 5) Similar to case (3), when we failover from A to B, the 1705 * prefix in A will not be in use as it has been moved to B. 1706 * We will delete it from our tables and recreate it when 1707 * it fails back. is_address_present makes sure that the 1708 * address is still valid in kernel. 1709 * 1710 * If none of the above is true, we recreate the prefix as it 1711 * has been manually removed. We do it only when the interface 1712 * is not FAILED or INACTIVE or OFFLINE. 1713 */ 1714 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 1715 next_pr = pr->pr_next; 1716 if (!pr->pr_in_use) { 1717 /* Clear PR_AUTO and PR_ONLINK */ 1718 pr->pr_kernel_state &= PR_STATIC; 1719 if ((pr->pr_state & PR_STATIC) || 1720 !(pr->pr_state & PR_AUTO) || 1721 !(prefix_token_match(pi, pr, pr->pr_flags)) || 1722 (!(pi->pi_kernel_state & PI_PRESENT)) || 1723 (is_address_present(pi, pr, pr->pr_flags))) { 1724 prefix_delete(pr); 1725 } else if (!(pi->pi_flags & 1726 (IFF_FAILED|IFF_INACTIVE|IFF_OFFLINE)) && 1727 pr->pr_state != pr->pr_kernel_state) { 1728 pr->pr_name[0] = '\0'; 1729 logmsg(LOG_INFO, "Prefix manually removed " 1730 "on %s - recreating it!\n", 1731 pi->pi_name); 1732 prefix_update_k(pr); 1733 } 1734 } 1735 } 1736 } 1737 1738 1739 /* 1740 * Queuing mechanism for router advertisements that are sent by in.ndpd 1741 * and that also need to be processed by in.ndpd. 1742 * Uses "signal number" 255 to indicate to the main poll loop 1743 * that there is something to dequeue and send to incomining_ra(). 1744 */ 1745 struct raq { 1746 struct raq *raq_next; 1747 struct phyint *raq_pi; 1748 int raq_packetlen; 1749 uchar_t *raq_packet; 1750 }; 1751 static struct raq *raq_head = NULL; 1752 1753 /* 1754 * Allocate a struct raq and memory for the packet. 1755 * Send signal 255 to have poll dequeue. 1756 */ 1757 static void 1758 loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len) 1759 { 1760 struct raq *raq; 1761 struct raq **raqp; 1762 1763 if (no_loopback) 1764 return; 1765 1766 if (debug & D_PKTOUT) 1767 logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name); 1768 1769 raq = calloc(sizeof (struct raq), 1); 1770 if (raq == NULL) { 1771 logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 1772 return; 1773 } 1774 raq->raq_packet = malloc(len); 1775 if (raq->raq_packet == NULL) { 1776 free(raq); 1777 logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 1778 return; 1779 } 1780 bcopy(ra, raq->raq_packet, len); 1781 raq->raq_packetlen = len; 1782 raq->raq_pi = pi; 1783 1784 /* Tail insert */ 1785 raqp = &raq_head; 1786 while (*raqp != NULL) 1787 raqp = &((*raqp)->raq_next); 1788 *raqp = raq; 1789 1790 /* Signal for poll loop */ 1791 sig_handler(255); 1792 } 1793 1794 /* 1795 * Dequeue and process all queued advertisements. 1796 */ 1797 static void 1798 loopback_ra_dequeue(void) 1799 { 1800 struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT; 1801 struct raq *raq; 1802 1803 if (debug & D_PKTIN) 1804 logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n"); 1805 1806 while ((raq = raq_head) != NULL) { 1807 raq_head = raq->raq_next; 1808 raq->raq_next = NULL; 1809 1810 if (debug & D_PKTIN) { 1811 logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n", 1812 raq->raq_pi->pi_name); 1813 } 1814 1815 incoming_ra(raq->raq_pi, 1816 (struct nd_router_advert *)raq->raq_packet, 1817 raq->raq_packetlen, &from, _B_TRUE); 1818 free(raq->raq_packet); 1819 free(raq); 1820 } 1821 } 1822 1823 1824 static void 1825 usage(char *cmd) 1826 { 1827 (void) fprintf(stderr, 1828 "usage: %s [ -adt ] [-f <config file>]\n", cmd); 1829 } 1830 1831 int 1832 main(int argc, char *argv[]) 1833 { 1834 int i; 1835 struct phyint *pi; 1836 int c; 1837 char *config_file = PATH_NDPD_CONF; 1838 boolean_t file_required = _B_FALSE; 1839 1840 argv0 = argv; 1841 srandom(gethostid()); 1842 (void) umask(0022); 1843 1844 while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) { 1845 switch (c) { 1846 case 'a': 1847 /* 1848 * The StatelessAddrConf variable in ndpd.conf, if 1849 * present, will override this setting. 1850 */ 1851 ifdefaults[I_StatelessAddrConf].cf_value = 0; 1852 break; 1853 case 'd': 1854 debug = D_ALL; 1855 break; 1856 case 'D': 1857 i = strtol((char *)optarg, NULL, 0); 1858 if (i == 0) { 1859 (void) fprintf(stderr, "Bad debug flags: %s\n", 1860 (char *)optarg); 1861 exit(1); 1862 } 1863 debug |= i; 1864 break; 1865 case 'n': 1866 no_loopback = 1; 1867 break; 1868 case 'I': 1869 show_ifs = 1; 1870 break; 1871 case 't': 1872 debug |= D_PKTIN | D_PKTOUT | D_PKTBAD; 1873 break; 1874 case 'f': 1875 config_file = (char *)optarg; 1876 file_required = _B_TRUE; 1877 break; 1878 case '?': 1879 usage(argv[0]); 1880 exit(1); 1881 } 1882 } 1883 1884 if (parse_config(config_file, file_required) == -1) 1885 exit(2); 1886 1887 if (show_ifs) 1888 phyint_print_all(); 1889 1890 if (debug == 0) { 1891 initlog(); 1892 } 1893 1894 setup_eventpipe(); 1895 rtsock = setup_rtsock(); 1896 timer_init(); 1897 initifs(_B_TRUE); 1898 1899 check_daemonize(); 1900 1901 for (;;) { 1902 if (poll(pollfds, pollfd_num, -1) < 0) { 1903 if (errno == EINTR) 1904 continue; 1905 logperror("main: poll"); 1906 exit(1); 1907 } 1908 for (i = 0; i < pollfd_num; i++) { 1909 if (!(pollfds[i].revents & POLLIN)) 1910 continue; 1911 if (pollfds[i].fd == eventpipe_read) { 1912 in_signal(eventpipe_read); 1913 break; 1914 } 1915 if (pollfds[i].fd == rtsock) { 1916 process_rtsock(rtsock); 1917 break; 1918 } 1919 /* 1920 * Run timer routine to advance clock if more than 1921 * half a second since the clock was advanced. 1922 * This limits CPU usage under severe packet 1923 * arrival rates but it creates a slight inaccuracy 1924 * in the timer mechanism. 1925 */ 1926 conditional_run_timeouts(500U); 1927 for (pi = phyints; pi != NULL; pi = pi->pi_next) { 1928 if (pollfds[i].fd == pi->pi_sock) { 1929 in_data(pi); 1930 break; 1931 } 1932 } 1933 } 1934 } 1935 /* NOTREACHED */ 1936 return (0); 1937 } 1938 1939 /* 1940 * LOGGER 1941 */ 1942 1943 static boolean_t logging = _B_FALSE; 1944 1945 static void 1946 initlog(void) 1947 { 1948 logging = _B_TRUE; 1949 openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON); 1950 } 1951 1952 /* Print the date/time without a trailing carridge return */ 1953 static void 1954 fprintdate(FILE *file) 1955 { 1956 char buf[BUFSIZ]; 1957 struct tm tms; 1958 time_t now; 1959 1960 now = time(NULL); 1961 (void) localtime_r(&now, &tms); 1962 (void) strftime(buf, sizeof (buf), "%h %d %X", &tms); 1963 (void) fprintf(file, "%s ", buf); 1964 } 1965 1966 /* PRINTFLIKE1 */ 1967 void 1968 logmsg(int level, char *fmt, ...) 1969 { 1970 va_list ap; 1971 va_start(ap, fmt); 1972 1973 if (logging) { 1974 vsyslog(level, fmt, ap); 1975 } else { 1976 fprintdate(stderr); 1977 (void) vfprintf(stderr, fmt, ap); 1978 } 1979 va_end(ap); 1980 } 1981 1982 void 1983 logperror(char *str) 1984 { 1985 if (logging) { 1986 syslog(LOG_ERR, "%s: %m\n", str); 1987 } else { 1988 fprintdate(stderr); 1989 (void) fprintf(stderr, "%s: %s\n", str, strerror(errno)); 1990 } 1991 } 1992 1993 void 1994 logperror_pi(struct phyint *pi, char *str) 1995 { 1996 if (logging) { 1997 syslog(LOG_ERR, "%s (interface %s): %m\n", 1998 str, pi->pi_name); 1999 } else { 2000 fprintdate(stderr); 2001 (void) fprintf(stderr, "%s (interface %s): %s\n", 2002 str, pi->pi_name, strerror(errno)); 2003 } 2004 } 2005 2006 void 2007 logperror_pr(struct prefix *pr, char *str) 2008 { 2009 if (logging) { 2010 syslog(LOG_ERR, "%s (prefix %s if %s): %m\n", 2011 str, pr->pr_name, pr->pr_physical->pi_name); 2012 } else { 2013 fprintdate(stderr); 2014 (void) fprintf(stderr, "%s (prefix %s if %s): %s\n", 2015 str, pr->pr_name, pr->pr_physical->pi_name, 2016 strerror(errno)); 2017 } 2018 } 2019