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