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 logmsg(LOG_ERR, "terminated\n"); 1337 exit(0); 1338 /* NOTREACHED */ 1339 case 255: 1340 /* 1341 * Special "signal" from looback_ra_enqueue. 1342 * Handle any queued loopback router advertisements. 1343 */ 1344 loopback_ra_dequeue(); 1345 break; 1346 default: 1347 logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf); 1348 } 1349 } 1350 1351 /* 1352 * Create pipe for signal delivery and set up signal handlers. 1353 */ 1354 static void 1355 setup_eventpipe(void) 1356 { 1357 int fds[2]; 1358 struct sigaction act; 1359 1360 if ((pipe(fds)) < 0) { 1361 logperror("setup_eventpipe: pipe"); 1362 exit(1); 1363 } 1364 eventpipe_read = fds[0]; 1365 eventpipe_write = fds[1]; 1366 if (poll_add(eventpipe_read) == -1) { 1367 exit(1); 1368 } 1369 act.sa_handler = sig_handler; 1370 act.sa_flags = SA_RESTART; 1371 (void) sigaction(SIGALRM, &act, NULL); 1372 1373 (void) sigset(SIGHUP, sig_handler); 1374 (void) sigset(SIGUSR1, sig_handler); 1375 (void) sigset(SIGTERM, sig_handler); 1376 (void) sigset(SIGINT, sig_handler); 1377 (void) sigset(SIGQUIT, sig_handler); 1378 } 1379 1380 /* 1381 * Create a routing socket for receiving RTM_IFINFO messages and initialize 1382 * the routing socket message header and as much of the sockaddrs as possible. 1383 */ 1384 static int 1385 setup_rtsock(void) 1386 { 1387 int s; 1388 int ret; 1389 char *cp; 1390 struct sockaddr_in6 *sin6; 1391 1392 s = socket(PF_ROUTE, SOCK_RAW, AF_INET6); 1393 if (s == -1) { 1394 logperror("socket(PF_ROUTE)"); 1395 exit(1); 1396 } 1397 ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK); 1398 if (ret < 0) { 1399 logperror("fcntl(O_NDELAY)"); 1400 exit(1); 1401 } 1402 if (poll_add(s) == -1) { 1403 exit(1); 1404 } 1405 1406 /* 1407 * Allocate storage for the routing socket message. 1408 */ 1409 rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN); 1410 if (rt_msg == NULL) { 1411 logperror("malloc"); 1412 exit(1); 1413 } 1414 1415 /* 1416 * Initialize the routing socket message by zero-filling it and then 1417 * setting the fields where are constant through the lifetime of the 1418 * process. 1419 */ 1420 bzero(rt_msg, NDP_RTM_MSGLEN); 1421 rt_msg->rtm_msglen = NDP_RTM_MSGLEN; 1422 rt_msg->rtm_version = RTM_VERSION; 1423 rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP; 1424 rt_msg->rtm_pid = getpid(); 1425 if (rt_msg->rtm_pid < 0) { 1426 logperror("getpid"); 1427 exit(1); 1428 } 1429 1430 /* 1431 * The RTA_DST sockaddr does not change during the lifetime of the 1432 * process so it can be completely initialized at this time. 1433 */ 1434 cp = (char *)rt_msg + sizeof (struct rt_msghdr); 1435 sin6 = (struct sockaddr_in6 *)cp; 1436 sin6->sin6_family = AF_INET6; 1437 sin6->sin6_addr = in6addr_any; 1438 1439 /* 1440 * Initialize the constant portion of the RTA_GATEWAY sockaddr. 1441 */ 1442 cp += sizeof (struct sockaddr_in6); 1443 rta_gateway = (struct sockaddr_in6 *)cp; 1444 rta_gateway->sin6_family = AF_INET6; 1445 1446 /* 1447 * The RTA_NETMASK sockaddr does not change during the lifetime of the 1448 * process so it can be completely initialized at this time. 1449 */ 1450 cp += sizeof (struct sockaddr_in6); 1451 sin6 = (struct sockaddr_in6 *)cp; 1452 sin6->sin6_family = AF_INET6; 1453 sin6->sin6_addr = in6addr_any; 1454 1455 /* 1456 * Initialize the constant portion of the RTA_IFP sockaddr. 1457 */ 1458 cp += sizeof (struct sockaddr_in6); 1459 rta_ifp = (struct sockaddr_dl *)cp; 1460 rta_ifp->sdl_family = AF_LINK; 1461 1462 return (s); 1463 } 1464 1465 /* 1466 * Retrieve one routing socket message. If RTM_IFINFO indicates 1467 * new phyint do a full scan of the interfaces. If RTM_IFINFO 1468 * indicates an existing phyint only scan that phyint and asociated 1469 * prefixes. 1470 */ 1471 static void 1472 process_rtsock(int rtsock) 1473 { 1474 int n; 1475 #define MSG_SIZE 2048/8 1476 int64_t msg[MSG_SIZE]; 1477 struct rt_msghdr *rtm; 1478 struct if_msghdr *ifm; 1479 struct phyint *pi; 1480 struct prefix *pr; 1481 boolean_t need_initifs = _B_FALSE; 1482 boolean_t need_ifscan = _B_FALSE; 1483 int64_t ifscan_msg[10][MSG_SIZE]; 1484 int ifscan_index = 0; 1485 int i; 1486 1487 /* Empty the rtsock and coealesce all the work that we have */ 1488 while (ifscan_index < 10) { 1489 n = read(rtsock, msg, sizeof (msg)); 1490 if (n <= 0) { 1491 /* No more messages */ 1492 break; 1493 } 1494 rtm = (struct rt_msghdr *)msg; 1495 if (rtm->rtm_version != RTM_VERSION) { 1496 logmsg(LOG_ERR, 1497 "process_rtsock: version %d not understood\n", 1498 rtm->rtm_version); 1499 return; 1500 } 1501 switch (rtm->rtm_type) { 1502 case RTM_NEWADDR: 1503 case RTM_DELADDR: 1504 /* 1505 * Some logical interface has changed - have to scan 1506 * everything to determine what actually changed. 1507 */ 1508 if (debug & D_IFSCAN) { 1509 logmsg(LOG_DEBUG, "process_rtsock: " 1510 "message %d\n", rtm->rtm_type); 1511 } 1512 need_initifs = _B_TRUE; 1513 break; 1514 case RTM_IFINFO: 1515 need_ifscan = _B_TRUE; 1516 (void) memcpy(ifscan_msg[ifscan_index], rtm, 1517 sizeof (msg)); 1518 ifscan_index++; 1519 /* Handled below */ 1520 break; 1521 default: 1522 /* Not interesting */ 1523 break; 1524 } 1525 } 1526 /* 1527 * If we do full scan i.e initifs, we don't need to 1528 * scan a particular interface as we should have 1529 * done that as part of initifs. 1530 */ 1531 if (need_initifs) { 1532 initifs(_B_FALSE); 1533 return; 1534 } 1535 1536 if (!need_ifscan) 1537 return; 1538 1539 for (i = 0; i < ifscan_index; i++) { 1540 ifm = (struct if_msghdr *)ifscan_msg[i]; 1541 if (debug & D_IFSCAN) 1542 logmsg(LOG_DEBUG, "process_rtsock: index %d\n", 1543 ifm->ifm_index); 1544 1545 pi = phyint_lookup_on_index(ifm->ifm_index); 1546 if (pi == NULL) { 1547 /* 1548 * A new physical interface. Do a full scan of the 1549 * to catch any new logical interfaces. 1550 */ 1551 initifs(_B_FALSE); 1552 return; 1553 } 1554 1555 if (ifm->ifm_flags != pi->pi_flags) { 1556 if (debug & D_IFSCAN) { 1557 logmsg(LOG_DEBUG, "process_rtsock: clr for " 1558 "%s old flags 0x%x new flags 0x%x\n", 1559 pi->pi_name, pi->pi_flags, ifm->ifm_flags); 1560 } 1561 } 1562 1563 1564 /* 1565 * Mark the interfaces so that we can find phyints and prefixes 1566 * which have disappeared from the kernel. 1567 * if_process will set pr_in_use when it finds the 1568 * interface in the kernel. 1569 * Before re-examining the state of the interfaces, 1570 * PI_PRESENT should be cleared from pi_kernel_state. 1571 */ 1572 pi->pi_kernel_state &= ~PI_PRESENT; 1573 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 1574 pr->pr_in_use = _B_FALSE; 1575 } 1576 1577 if (ifsock < 0) { 1578 ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 1579 if (ifsock < 0) { 1580 logperror("process_rtsock: socket"); 1581 return; 1582 } 1583 } 1584 if_process(ifsock, pi->pi_name, _B_FALSE); 1585 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 1586 if_process(ifsock, pr->pr_name, _B_FALSE); 1587 } 1588 /* 1589 * If interface (still) exists in kernel, set 1590 * pi_state to indicate that. 1591 */ 1592 if (pi->pi_kernel_state & PI_PRESENT) { 1593 pi->pi_state |= PI_PRESENT; 1594 } 1595 check_if_removed(pi); 1596 if (show_ifs) 1597 phyint_print_all(); 1598 } 1599 } 1600 1601 /* 1602 * Check whether the address formed by pr->pr_prefix and pi_token 1603 * exists in the kernel. Cannot call SIOCTMYADDR/ONLINK as it 1604 * does not check for down addresses. This function should not 1605 * be called for onlink prefixes. 1606 */ 1607 static boolean_t 1608 is_address_present(struct phyint *pi, struct prefix *pr, uint64_t flags) 1609 { 1610 int s; 1611 in6_addr_t addr, *token; 1612 int i; 1613 int ret; 1614 struct sockaddr_in6 sin6; 1615 1616 s = socket(AF_INET6, SOCK_DGRAM, 0); 1617 if (s < 0) { 1618 logperror("is_address_present: socket"); 1619 /* 1620 * By returning B_TRUE, we make the caller delete 1621 * the prefix from the internal table. In the worst 1622 * case the next RA will create the prefix. 1623 */ 1624 return (_B_TRUE); 1625 } 1626 if (flags & IFF_TEMPORARY) 1627 token = &pi->pi_tmp_token; 1628 else 1629 token = &pi->pi_token; 1630 for (i = 0; i < 16; i++) { 1631 /* 1632 * prefix_create ensures that pr_prefix has all-zero 1633 * bits after prefixlen. 1634 */ 1635 addr.s6_addr[i] = pr->pr_prefix.s6_addr[i] | token->s6_addr[i]; 1636 } 1637 (void) memset(&sin6, 0, sizeof (struct sockaddr_in6)); 1638 sin6.sin6_family = AF_INET6; 1639 sin6.sin6_addr = addr; 1640 ret = bind(s, (struct sockaddr *)&sin6, sizeof (struct sockaddr_in6)); 1641 (void) close(s); 1642 if (ret < 0 && errno == EADDRNOTAVAIL) 1643 return (_B_FALSE); 1644 else 1645 return (_B_TRUE); 1646 } 1647 1648 /* 1649 * Look if the phyint or one of its prefixes have been removed from 1650 * the kernel and take appropriate action. 1651 * Uses {pi,pr}_in_use. 1652 */ 1653 static void 1654 check_if_removed(struct phyint *pi) 1655 { 1656 struct prefix *pr; 1657 struct prefix *next_pr; 1658 1659 /* 1660 * Detect phyints that have been removed from the kernel. 1661 * Since we can't recreate it here (would require ifconfig plumb 1662 * logic) we just terminate use of that phyint. 1663 */ 1664 if (!(pi->pi_kernel_state & PI_PRESENT) && 1665 (pi->pi_state & PI_PRESENT)) { 1666 logmsg(LOG_ERR, "Interface %s has been removed from kernel. " 1667 "in.ndpd will no longer use it\n", pi->pi_name); 1668 /* 1669 * Clear state so that should the phyint reappear 1670 * we will start with initial advertisements or 1671 * solicitations. 1672 */ 1673 phyint_cleanup(pi); 1674 } 1675 /* 1676 * Detect prefixes which are removed. 1677 * 1678 * We remove the prefix in all of the following cases : 1679 * 1680 * 1) Static prefixes are not the ones we create. So, 1681 * just remove it from our tables. 1682 * 1683 * 2) On-link prefixes potentially move to a different 1684 * phyint during failover. As it does not have 1685 * an address, we can't use the logic in is_address_present 1686 * to detect whether it is present in the kernel or not. 1687 * Thus when it is manually removed we don't recreate it. 1688 * 1689 * 3) If there is a token mis-match and this prefix is not 1690 * in the kernel, it means we don't need this prefix on 1691 * this interface anymore. It must have been moved to a 1692 * different interface by in.mpathd. This normally 1693 * happens after a failover followed by a failback (or 1694 * another failover) and we re-read the network 1695 * configuration. For the failover from A to B, we would 1696 * have created state on B about A's address, which will 1697 * not be in use after the subsequent failback. So, we 1698 * remove that prefix here. 1699 * 1700 * 4) If the physical interface is not present, then remove 1701 * the prefix. In the cases where we are advertising 1702 * prefixes, the state is kept in advertisement prefix and 1703 * hence we can delete the prefix. 1704 * 1705 * 5) Similar to case (3), when we failover from A to B, the 1706 * prefix in A will not be in use as it has been moved to B. 1707 * We will delete it from our tables and recreate it when 1708 * it fails back. is_address_present makes sure that the 1709 * address is still valid in kernel. 1710 * 1711 * If none of the above is true, we recreate the prefix as it 1712 * has been manually removed. We do it only when the interface 1713 * is not FAILED or INACTIVE or OFFLINE. 1714 */ 1715 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 1716 next_pr = pr->pr_next; 1717 if (!pr->pr_in_use) { 1718 /* Clear PR_AUTO and PR_ONLINK */ 1719 pr->pr_kernel_state &= PR_STATIC; 1720 if ((pr->pr_state & PR_STATIC) || 1721 !(pr->pr_state & PR_AUTO) || 1722 !(prefix_token_match(pi, pr, pr->pr_flags)) || 1723 (!(pi->pi_kernel_state & PI_PRESENT)) || 1724 (is_address_present(pi, pr, pr->pr_flags))) { 1725 prefix_delete(pr); 1726 } else if (!(pi->pi_flags & 1727 (IFF_FAILED|IFF_INACTIVE|IFF_OFFLINE)) && 1728 pr->pr_state != pr->pr_kernel_state) { 1729 pr->pr_name[0] = '\0'; 1730 logmsg(LOG_INFO, "Prefix manually removed " 1731 "on %s - recreating it!\n", 1732 pi->pi_name); 1733 prefix_update_k(pr); 1734 } 1735 } 1736 } 1737 } 1738 1739 1740 /* 1741 * Queuing mechanism for router advertisements that are sent by in.ndpd 1742 * and that also need to be processed by in.ndpd. 1743 * Uses "signal number" 255 to indicate to the main poll loop 1744 * that there is something to dequeue and send to incomining_ra(). 1745 */ 1746 struct raq { 1747 struct raq *raq_next; 1748 struct phyint *raq_pi; 1749 int raq_packetlen; 1750 uchar_t *raq_packet; 1751 }; 1752 static struct raq *raq_head = NULL; 1753 1754 /* 1755 * Allocate a struct raq and memory for the packet. 1756 * Send signal 255 to have poll dequeue. 1757 */ 1758 static void 1759 loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len) 1760 { 1761 struct raq *raq; 1762 struct raq **raqp; 1763 1764 if (no_loopback) 1765 return; 1766 1767 if (debug & D_PKTOUT) 1768 logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name); 1769 1770 raq = calloc(sizeof (struct raq), 1); 1771 if (raq == NULL) { 1772 logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 1773 return; 1774 } 1775 raq->raq_packet = malloc(len); 1776 if (raq->raq_packet == NULL) { 1777 free(raq); 1778 logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 1779 return; 1780 } 1781 bcopy(ra, raq->raq_packet, len); 1782 raq->raq_packetlen = len; 1783 raq->raq_pi = pi; 1784 1785 /* Tail insert */ 1786 raqp = &raq_head; 1787 while (*raqp != NULL) 1788 raqp = &((*raqp)->raq_next); 1789 *raqp = raq; 1790 1791 /* Signal for poll loop */ 1792 sig_handler(255); 1793 } 1794 1795 /* 1796 * Dequeue and process all queued advertisements. 1797 */ 1798 static void 1799 loopback_ra_dequeue(void) 1800 { 1801 struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT; 1802 struct raq *raq; 1803 1804 if (debug & D_PKTIN) 1805 logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n"); 1806 1807 while ((raq = raq_head) != NULL) { 1808 raq_head = raq->raq_next; 1809 raq->raq_next = NULL; 1810 1811 if (debug & D_PKTIN) { 1812 logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n", 1813 raq->raq_pi->pi_name); 1814 } 1815 1816 incoming_ra(raq->raq_pi, 1817 (struct nd_router_advert *)raq->raq_packet, 1818 raq->raq_packetlen, &from, _B_TRUE); 1819 free(raq->raq_packet); 1820 free(raq); 1821 } 1822 } 1823 1824 1825 static void 1826 usage(char *cmd) 1827 { 1828 (void) fprintf(stderr, 1829 "usage: %s [ -adt ] [-f <config file>]\n", cmd); 1830 } 1831 1832 int 1833 main(int argc, char *argv[]) 1834 { 1835 int i; 1836 struct phyint *pi; 1837 int c; 1838 char *config_file = PATH_NDPD_CONF; 1839 boolean_t file_required = _B_FALSE; 1840 1841 argv0 = argv; 1842 srandom(gethostid()); 1843 (void) umask(0022); 1844 1845 while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) { 1846 switch (c) { 1847 case 'a': 1848 /* 1849 * The StatelessAddrConf variable in ndpd.conf, if 1850 * present, will override this setting. 1851 */ 1852 ifdefaults[I_StatelessAddrConf].cf_value = 0; 1853 break; 1854 case 'd': 1855 debug = D_ALL; 1856 break; 1857 case 'D': 1858 i = strtol((char *)optarg, NULL, 0); 1859 if (i == 0) { 1860 (void) fprintf(stderr, "Bad debug flags: %s\n", 1861 (char *)optarg); 1862 exit(1); 1863 } 1864 debug |= i; 1865 break; 1866 case 'n': 1867 no_loopback = 1; 1868 break; 1869 case 'I': 1870 show_ifs = 1; 1871 break; 1872 case 't': 1873 debug |= D_PKTIN | D_PKTOUT | D_PKTBAD; 1874 break; 1875 case 'f': 1876 config_file = (char *)optarg; 1877 file_required = _B_TRUE; 1878 break; 1879 case '?': 1880 usage(argv[0]); 1881 exit(1); 1882 } 1883 } 1884 1885 if (parse_config(config_file, file_required) == -1) 1886 exit(2); 1887 1888 if (show_ifs) 1889 phyint_print_all(); 1890 1891 if (debug == 0) { 1892 initlog(); 1893 } 1894 1895 setup_eventpipe(); 1896 rtsock = setup_rtsock(); 1897 timer_init(); 1898 initifs(_B_TRUE); 1899 1900 check_daemonize(); 1901 1902 for (;;) { 1903 if (poll(pollfds, pollfd_num, -1) < 0) { 1904 if (errno == EINTR) 1905 continue; 1906 logperror("main: poll"); 1907 exit(1); 1908 } 1909 for (i = 0; i < pollfd_num; i++) { 1910 if (!(pollfds[i].revents & POLLIN)) 1911 continue; 1912 if (pollfds[i].fd == eventpipe_read) { 1913 in_signal(eventpipe_read); 1914 break; 1915 } 1916 if (pollfds[i].fd == rtsock) { 1917 process_rtsock(rtsock); 1918 break; 1919 } 1920 /* 1921 * Run timer routine to advance clock if more than 1922 * half a second since the clock was advanced. 1923 * This limits CPU usage under severe packet 1924 * arrival rates but it creates a slight inaccuracy 1925 * in the timer mechanism. 1926 */ 1927 conditional_run_timeouts(500U); 1928 for (pi = phyints; pi != NULL; pi = pi->pi_next) { 1929 if (pollfds[i].fd == pi->pi_sock) { 1930 in_data(pi); 1931 break; 1932 } 1933 } 1934 } 1935 } 1936 /* NOTREACHED */ 1937 return (0); 1938 } 1939 1940 /* 1941 * LOGGER 1942 */ 1943 1944 static boolean_t logging = _B_FALSE; 1945 1946 static void 1947 initlog(void) 1948 { 1949 logging = _B_TRUE; 1950 openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON); 1951 } 1952 1953 /* Print the date/time without a trailing carridge return */ 1954 static void 1955 fprintdate(FILE *file) 1956 { 1957 char buf[BUFSIZ]; 1958 struct tm tms; 1959 time_t now; 1960 1961 now = time(NULL); 1962 (void) localtime_r(&now, &tms); 1963 (void) strftime(buf, sizeof (buf), "%h %d %X", &tms); 1964 (void) fprintf(file, "%s ", buf); 1965 } 1966 1967 /* PRINTFLIKE1 */ 1968 void 1969 logmsg(int level, char *fmt, ...) 1970 { 1971 va_list ap; 1972 va_start(ap, fmt); 1973 1974 if (logging) { 1975 vsyslog(level, fmt, ap); 1976 } else { 1977 fprintdate(stderr); 1978 (void) vfprintf(stderr, fmt, ap); 1979 } 1980 va_end(ap); 1981 } 1982 1983 void 1984 logperror(char *str) 1985 { 1986 if (logging) { 1987 syslog(LOG_ERR, "%s: %m\n", str); 1988 } else { 1989 fprintdate(stderr); 1990 (void) fprintf(stderr, "%s: %s\n", str, strerror(errno)); 1991 } 1992 } 1993 1994 void 1995 logperror_pi(struct phyint *pi, char *str) 1996 { 1997 if (logging) { 1998 syslog(LOG_ERR, "%s (interface %s): %m\n", 1999 str, pi->pi_name); 2000 } else { 2001 fprintdate(stderr); 2002 (void) fprintf(stderr, "%s (interface %s): %s\n", 2003 str, pi->pi_name, strerror(errno)); 2004 } 2005 } 2006 2007 void 2008 logperror_pr(struct prefix *pr, char *str) 2009 { 2010 if (logging) { 2011 syslog(LOG_ERR, "%s (prefix %s if %s): %m\n", 2012 str, pr->pr_name, pr->pr_physical->pi_name); 2013 } else { 2014 fprintdate(stderr); 2015 (void) fprintf(stderr, "%s (prefix %s if %s): %s\n", 2016 str, pr->pr_name, pr->pr_physical->pi_name, 2017 strerror(errno)); 2018 } 2019 } 2020