10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51577Sseb * Common Development and Distribution License (the "License"). 61577Sseb * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221577Sseb * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include "defs.h" 290Sstevel@tonic-gate #include "tables.h" 300Sstevel@tonic-gate #include <fcntl.h> 310Sstevel@tonic-gate 320Sstevel@tonic-gate static void initlog(void); 330Sstevel@tonic-gate static void run_timeouts(void); 340Sstevel@tonic-gate 350Sstevel@tonic-gate static void advertise(struct sockaddr_in6 *sin6, struct phyint *pi, 360Sstevel@tonic-gate boolean_t no_prefixes); 370Sstevel@tonic-gate static void solicit(struct sockaddr_in6 *sin6, struct phyint *pi); 380Sstevel@tonic-gate static void initifs(boolean_t first); 390Sstevel@tonic-gate static void check_if_removed(struct phyint *pi); 400Sstevel@tonic-gate static void loopback_ra_enqueue(struct phyint *pi, 410Sstevel@tonic-gate struct nd_router_advert *ra, int len); 420Sstevel@tonic-gate static void loopback_ra_dequeue(void); 430Sstevel@tonic-gate static void check_daemonize(void); 440Sstevel@tonic-gate 450Sstevel@tonic-gate struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0, 460Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 470Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 480Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x1 } }; 490Sstevel@tonic-gate 500Sstevel@tonic-gate struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0, 510Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 520Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 530Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x2 } }; 540Sstevel@tonic-gate 550Sstevel@tonic-gate static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0, 560Sstevel@tonic-gate { 0xff, 0x2, 0x0, 0x0, 570Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 580Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 590Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x1 } }; 600Sstevel@tonic-gate 610Sstevel@tonic-gate static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0, 620Sstevel@tonic-gate { 0xff, 0x2, 0x0, 0x0, 630Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 640Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 650Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x2 } }; 660Sstevel@tonic-gate 670Sstevel@tonic-gate static char **argv0; /* Saved for re-exec on SIGHUP */ 680Sstevel@tonic-gate 690Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8]; 700Sstevel@tonic-gate 710Sstevel@tonic-gate static int show_ifs = 0; 720Sstevel@tonic-gate static boolean_t already_daemonized = _B_FALSE; 730Sstevel@tonic-gate int debug = 0; 740Sstevel@tonic-gate int no_loopback = 0; /* Do not send RA packets to ourselves */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * Size of routing socket message used by in.ndpd which includes the header, 780Sstevel@tonic-gate * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6) 790Sstevel@tonic-gate * plus space for the RTA_IFP (a sockaddr_dl). 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate #define NDP_RTM_MSGLEN sizeof (struct rt_msghdr) + \ 820Sstevel@tonic-gate sizeof (struct sockaddr_in6) + \ 830Sstevel@tonic-gate sizeof (struct sockaddr_in6) + \ 840Sstevel@tonic-gate sizeof (struct sockaddr_in6) + \ 850Sstevel@tonic-gate sizeof (struct sockaddr_dl) 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * These are referenced externally in tables.c in order to fill in the 890Sstevel@tonic-gate * dynamic portions of the routing socket message and then to send the message 900Sstevel@tonic-gate * itself. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate int rtsock = -1; /* Routing socket */ 930Sstevel@tonic-gate struct rt_msghdr *rt_msg; /* Routing socket message */ 940Sstevel@tonic-gate struct sockaddr_in6 *rta_gateway; /* RTA_GATEWAY sockaddr */ 950Sstevel@tonic-gate struct sockaddr_dl *rta_ifp; /* RTA_IFP sockaddr */ 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Return the current time in milliseconds truncated to 990Sstevel@tonic-gate * fit in an integer. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate uint_t 1020Sstevel@tonic-gate getcurrenttime(void) 1030Sstevel@tonic-gate { 1040Sstevel@tonic-gate struct timeval tp; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate if (gettimeofday(&tp, NULL) < 0) { 1070Sstevel@tonic-gate logperror("getcurrenttime: gettimeofday failed"); 1080Sstevel@tonic-gate exit(1); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate return (tp.tv_sec * 1000 + tp.tv_usec / 1000); 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* 1140Sstevel@tonic-gate * Output a preformated packet from the packet[] buffer. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate static void 1170Sstevel@tonic-gate sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags) 1180Sstevel@tonic-gate { 1190Sstevel@tonic-gate int cc; 1200Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate cc = sendto(sock, (char *)packet, size, flags, 1230Sstevel@tonic-gate (struct sockaddr *)sin6, sizeof (*sin6)); 1240Sstevel@tonic-gate if (cc < 0 || cc != size) { 1250Sstevel@tonic-gate if (cc < 0) { 1260Sstevel@tonic-gate logperror("sendpacket: sendto"); 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n", 1290Sstevel@tonic-gate inet_ntop(sin6->sin6_family, 1300Sstevel@tonic-gate (void *)&sin6->sin6_addr, 1310Sstevel@tonic-gate abuf, sizeof (abuf)), 1320Sstevel@tonic-gate size, cc); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* Send a Router Solicitation */ 1370Sstevel@tonic-gate static void 1380Sstevel@tonic-gate solicit(struct sockaddr_in6 *sin6, struct phyint *pi) 1390Sstevel@tonic-gate { 1400Sstevel@tonic-gate int packetlen = 0; 1410Sstevel@tonic-gate struct nd_router_solicit *rs = (struct nd_router_solicit *)packet; 1420Sstevel@tonic-gate char *pptr = (char *)packet; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate rs->nd_rs_type = ND_ROUTER_SOLICIT; 1450Sstevel@tonic-gate rs->nd_rs_code = 0; 1460Sstevel@tonic-gate rs->nd_rs_cksum = htons(0); 1470Sstevel@tonic-gate rs->nd_rs_reserved = htonl(0); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate packetlen += sizeof (*rs); 1500Sstevel@tonic-gate pptr += sizeof (*rs); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* Attach any options */ 1530Sstevel@tonic-gate if (pi->pi_hdw_addr_len != 0) { 1540Sstevel@tonic-gate struct nd_opt_lla *lo = (struct nd_opt_lla *)pptr; 1550Sstevel@tonic-gate int optlen; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* roundup to multiple of 8 and make padding zero */ 1580Sstevel@tonic-gate optlen = ((sizeof (struct nd_opt_hdr) + 1590Sstevel@tonic-gate pi->pi_hdw_addr_len + 7) / 8) * 8; 1600Sstevel@tonic-gate bzero(pptr, optlen); 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate lo->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR; 1630Sstevel@tonic-gate lo->nd_opt_lla_len = optlen / 8; 1640Sstevel@tonic-gate bcopy((char *)pi->pi_hdw_addr, 1650Sstevel@tonic-gate (char *)lo->nd_opt_lla_hdw_addr, 1660Sstevel@tonic-gate pi->pi_hdw_addr_len); 1670Sstevel@tonic-gate packetlen += optlen; 1680Sstevel@tonic-gate pptr += optlen; 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate if (debug & D_PKTOUT) { 1720Sstevel@tonic-gate print_route_sol("Sending solicitation to ", pi, rs, packetlen, 1730Sstevel@tonic-gate sin6); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* 1790Sstevel@tonic-gate * Send a (set of) Router Advertisements and feed them back to ourselves 1800Sstevel@tonic-gate * for processing. Unless no_prefixes is set all prefixes are included. 1810Sstevel@tonic-gate * If there are too many prefix options to fit in one packet multiple 1820Sstevel@tonic-gate * packets will be sent - each containing a subset of the prefix options. 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate static void 1850Sstevel@tonic-gate advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes) 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate struct nd_opt_prefix_info *po; 1880Sstevel@tonic-gate char *pptr = (char *)packet; 1890Sstevel@tonic-gate struct nd_router_advert *ra; 1900Sstevel@tonic-gate struct adv_prefix *adv_pr; 1910Sstevel@tonic-gate int packetlen = 0; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate ra = (struct nd_router_advert *)pptr; 1940Sstevel@tonic-gate ra->nd_ra_type = ND_ROUTER_ADVERT; 1950Sstevel@tonic-gate ra->nd_ra_code = 0; 1960Sstevel@tonic-gate ra->nd_ra_cksum = htons(0); 1970Sstevel@tonic-gate ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit; 1980Sstevel@tonic-gate ra->nd_ra_flags_reserved = 0; 1990Sstevel@tonic-gate if (pi->pi_AdvManagedFlag) 2000Sstevel@tonic-gate ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED; 2010Sstevel@tonic-gate if (pi->pi_AdvOtherConfigFlag) 2020Sstevel@tonic-gate ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) 2050Sstevel@tonic-gate ra->nd_ra_router_lifetime = htons(0); 2060Sstevel@tonic-gate else 2070Sstevel@tonic-gate ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime); 2080Sstevel@tonic-gate ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime); 2090Sstevel@tonic-gate ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate packetlen = sizeof (*ra); 2120Sstevel@tonic-gate pptr += sizeof (*ra); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) { 2150Sstevel@tonic-gate if (debug & D_PKTOUT) { 2160Sstevel@tonic-gate print_route_adv("Sending advert (FINAL) to ", pi, 2170Sstevel@tonic-gate ra, packetlen, sin6); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 2200Sstevel@tonic-gate /* Feed packet back in for router operation */ 2210Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 2220Sstevel@tonic-gate return; 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* Attach any options */ 2260Sstevel@tonic-gate if (pi->pi_hdw_addr_len != 0) { 2270Sstevel@tonic-gate struct nd_opt_lla *lo = (struct nd_opt_lla *)pptr; 2280Sstevel@tonic-gate int optlen; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* roundup to multiple of 8 and make padding zero */ 2310Sstevel@tonic-gate optlen = ((sizeof (struct nd_opt_hdr) + 2320Sstevel@tonic-gate pi->pi_hdw_addr_len + 7) / 8) * 8; 2330Sstevel@tonic-gate bzero(pptr, optlen); 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate lo->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR; 2360Sstevel@tonic-gate lo->nd_opt_lla_len = optlen / 8; 2370Sstevel@tonic-gate bcopy((char *)pi->pi_hdw_addr, 2380Sstevel@tonic-gate (char *)lo->nd_opt_lla_hdw_addr, 2390Sstevel@tonic-gate pi->pi_hdw_addr_len); 2400Sstevel@tonic-gate packetlen += optlen; 2410Sstevel@tonic-gate pptr += optlen; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate if (pi->pi_AdvLinkMTU != 0) { 2450Sstevel@tonic-gate struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate mo->nd_opt_mtu_type = ND_OPT_MTU; 2480Sstevel@tonic-gate mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8; 2490Sstevel@tonic-gate mo->nd_opt_mtu_reserved = 0; 2500Sstevel@tonic-gate mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate packetlen += sizeof (struct nd_opt_mtu); 2530Sstevel@tonic-gate pptr += sizeof (struct nd_opt_mtu); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if (no_prefixes) { 2570Sstevel@tonic-gate if (debug & D_PKTOUT) { 2580Sstevel@tonic-gate print_route_adv("Sending advert to ", pi, 2590Sstevel@tonic-gate ra, packetlen, sin6); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 2620Sstevel@tonic-gate /* Feed packet back in for router operation */ 2630Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 2640Sstevel@tonic-gate return; 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate po = (struct nd_opt_prefix_info *)pptr; 2680Sstevel@tonic-gate for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL; 2690Sstevel@tonic-gate adv_pr = adv_pr->adv_pr_next) { 2700Sstevel@tonic-gate if (!adv_pr->adv_pr_AdvOnLinkFlag && 2710Sstevel@tonic-gate !adv_pr->adv_pr_AdvAutonomousFlag) { 2720Sstevel@tonic-gate continue; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * If the prefix doesn't fit in packet send 2770Sstevel@tonic-gate * what we have so far and start with new packet. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate if (packetlen + sizeof (*po) > 2800Sstevel@tonic-gate pi->pi_LinkMTU - sizeof (struct ip6_hdr)) { 2810Sstevel@tonic-gate if (debug & D_PKTOUT) { 2820Sstevel@tonic-gate print_route_adv("Sending advert " 2830Sstevel@tonic-gate "(FRAG) to ", 2840Sstevel@tonic-gate pi, ra, packetlen, sin6); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 2870Sstevel@tonic-gate /* Feed packet back in for router operation */ 2880Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 2890Sstevel@tonic-gate packetlen = sizeof (*ra); 2900Sstevel@tonic-gate pptr = (char *)packet + sizeof (*ra); 2910Sstevel@tonic-gate po = (struct nd_opt_prefix_info *)pptr; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 2940Sstevel@tonic-gate po->nd_opt_pi_len = sizeof (*po)/8; 2950Sstevel@tonic-gate po->nd_opt_pi_flags_reserved = 0; 2960Sstevel@tonic-gate if (adv_pr->adv_pr_AdvOnLinkFlag) { 2970Sstevel@tonic-gate po->nd_opt_pi_flags_reserved |= 2980Sstevel@tonic-gate ND_OPT_PI_FLAG_ONLINK; 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate if (adv_pr->adv_pr_AdvAutonomousFlag) { 3010Sstevel@tonic-gate po->nd_opt_pi_flags_reserved |= 3020Sstevel@tonic-gate ND_OPT_PI_FLAG_AUTO; 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len; 3050Sstevel@tonic-gate /* 3060Sstevel@tonic-gate * If both Adv*Expiration and Adv*Lifetime are 3070Sstevel@tonic-gate * set we prefer the former and make the lifetime 3080Sstevel@tonic-gate * decrement in real time. 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate if (adv_pr->adv_pr_AdvValidRealTime) { 3110Sstevel@tonic-gate po->nd_opt_pi_valid_time = 3120Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvValidExpiration); 3130Sstevel@tonic-gate } else { 3140Sstevel@tonic-gate po->nd_opt_pi_valid_time = 3150Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvValidLifetime); 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate if (adv_pr->adv_pr_AdvPreferredRealTime) { 3180Sstevel@tonic-gate po->nd_opt_pi_preferred_time = 3190Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvPreferredExpiration); 3200Sstevel@tonic-gate } else { 3210Sstevel@tonic-gate po->nd_opt_pi_preferred_time = 3220Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvPreferredLifetime); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate po->nd_opt_pi_reserved2 = htonl(0); 3250Sstevel@tonic-gate po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix; 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate po++; 3280Sstevel@tonic-gate packetlen += sizeof (*po); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate if (debug & D_PKTOUT) { 3310Sstevel@tonic-gate print_route_adv("Sending advert to ", pi, 3320Sstevel@tonic-gate ra, packetlen, sin6); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 3350Sstevel@tonic-gate /* Feed packet back in for router operation */ 3360Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* Poll support */ 3400Sstevel@tonic-gate static int pollfd_num = 0; /* Allocated and initialized */ 3410Sstevel@tonic-gate static struct pollfd *pollfds = NULL; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate /* 3440Sstevel@tonic-gate * Add fd to the set being polled. Returns 0 if ok; -1 if failed. 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate int 3470Sstevel@tonic-gate poll_add(int fd) 3480Sstevel@tonic-gate { 3490Sstevel@tonic-gate int i; 3500Sstevel@tonic-gate int new_num; 3510Sstevel@tonic-gate struct pollfd *newfds; 3520Sstevel@tonic-gate retry: 3530Sstevel@tonic-gate /* Check if already present */ 3540Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 3550Sstevel@tonic-gate if (pollfds[i].fd == fd) 3560Sstevel@tonic-gate return (0); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate /* Check for empty spot already present */ 3590Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 3600Sstevel@tonic-gate if (pollfds[i].fd == -1) { 3610Sstevel@tonic-gate pollfds[i].fd = fd; 3620Sstevel@tonic-gate return (0); 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate /* Allocate space for 32 more fds and initialize to -1 */ 3670Sstevel@tonic-gate new_num = pollfd_num + 32; 3680Sstevel@tonic-gate newfds = realloc(pollfds, new_num * sizeof (struct pollfd)); 3690Sstevel@tonic-gate if (newfds == NULL) { 3700Sstevel@tonic-gate logperror("poll_add: realloc"); 3710Sstevel@tonic-gate return (-1); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate for (i = pollfd_num; i < new_num; i++) { 3740Sstevel@tonic-gate newfds[i].fd = -1; 3750Sstevel@tonic-gate newfds[i].events = POLLIN; 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate pollfd_num = new_num; 3780Sstevel@tonic-gate pollfds = newfds; 3790Sstevel@tonic-gate goto retry; 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate /* 3830Sstevel@tonic-gate * Remove fd from the set being polled. Returns 0 if ok; -1 if failed. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate int 3860Sstevel@tonic-gate poll_remove(int fd) 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate int i; 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* Check if already present */ 3910Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 3920Sstevel@tonic-gate if (pollfds[i].fd == fd) { 3930Sstevel@tonic-gate pollfds[i].fd = -1; 3940Sstevel@tonic-gate return (0); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate return (-1); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * Extract information about the ifname (either a physical interface and 4020Sstevel@tonic-gate * the ":0" logical interface or just a logical interface). 4030Sstevel@tonic-gate * If the interface (still) exists in kernel set pr_in_use 4040Sstevel@tonic-gate * for caller to be able to detect interfaces that are removed. 4050Sstevel@tonic-gate * Starts sending advertisements/solicitations when new physical interfaces 4060Sstevel@tonic-gate * are detected. 4070Sstevel@tonic-gate */ 4080Sstevel@tonic-gate static void 4090Sstevel@tonic-gate if_process(int s, char *ifname, boolean_t first) 4100Sstevel@tonic-gate { 4110Sstevel@tonic-gate struct lifreq lifr; 4120Sstevel@tonic-gate struct phyint *pi; 4130Sstevel@tonic-gate struct prefix *pr; 4140Sstevel@tonic-gate char *cp; 4150Sstevel@tonic-gate char phyintname[LIFNAMSIZ + 1]; 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate if (debug & D_IFSCAN) 4180Sstevel@tonic-gate logmsg(LOG_DEBUG, "if_process(%s)\n", ifname); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 4210Sstevel@tonic-gate lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0'; 4220Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) { 4230Sstevel@tonic-gate if (errno == ENXIO) { 4240Sstevel@tonic-gate /* 4250Sstevel@tonic-gate * Interface has disappeared 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate return; 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate logperror("if_process: ioctl (get interface flags)"); 4300Sstevel@tonic-gate return; 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Ignore loopback and point-to-multipoint interfaces. 4350Sstevel@tonic-gate * Point-to-point interfaces always have IFF_MULTICAST set. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate if (!(lifr.lifr_flags & IFF_MULTICAST) || 4380Sstevel@tonic-gate (lifr.lifr_flags & IFF_LOOPBACK)) { 4390Sstevel@tonic-gate return; 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate if (!(lifr.lifr_flags & IFF_IPV6)) 4430Sstevel@tonic-gate return; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate (void) strncpy(phyintname, ifname, sizeof (phyintname)); 4460Sstevel@tonic-gate phyintname[sizeof (phyintname) - 1] = '\0'; 4470Sstevel@tonic-gate if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) { 4480Sstevel@tonic-gate *cp = '\0'; 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate pi = phyint_lookup(phyintname); 4520Sstevel@tonic-gate if (pi == NULL) { 4530Sstevel@tonic-gate /* 4540Sstevel@tonic-gate * Do not add anything for new interfaces until they are UP. 4550Sstevel@tonic-gate * For existing interfaces we track the up flag. 4560Sstevel@tonic-gate */ 4570Sstevel@tonic-gate if (!(lifr.lifr_flags & IFF_UP)) 4580Sstevel@tonic-gate return; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate pi = phyint_create(phyintname); 4610Sstevel@tonic-gate if (pi == NULL) { 4620Sstevel@tonic-gate logmsg(LOG_ERR, "if_process: out of memory\n"); 4630Sstevel@tonic-gate return; 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate (void) phyint_init_from_k(pi); 4670Sstevel@tonic-gate if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) { 4680Sstevel@tonic-gate /* Interface is not yet present */ 4690Sstevel@tonic-gate if (debug & D_PHYINT) { 4700Sstevel@tonic-gate logmsg(LOG_DEBUG, "if_process: interface not yet " 4710Sstevel@tonic-gate "present %s\n", pi->pi_name); 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate return; 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate if (pi->pi_sock != -1) { 4770Sstevel@tonic-gate if (poll_add(pi->pi_sock) == -1) { 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * reset state. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate phyint_cleanup(pi); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* 4860Sstevel@tonic-gate * Check if IFF_ROUTER has been turned off in kernel in which 4870Sstevel@tonic-gate * case we have to turn off AdvSendAdvertisements. 4880Sstevel@tonic-gate * The kernel will automatically turn off IFF_ROUTER if 4890Sstevel@tonic-gate * ip6_forwarding is turned off. 4900Sstevel@tonic-gate * Note that we do not switch back should IFF_ROUTER be turned on. 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate if (!first && 4930Sstevel@tonic-gate pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) { 4940Sstevel@tonic-gate logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name); 4950Sstevel@tonic-gate check_to_advertise(pi, START_FINAL_ADV); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate pi->pi_AdvSendAdvertisements = 0; 4980Sstevel@tonic-gate pi->pi_sol_state = NO_SOLICIT; 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * Send advertisments and solicitation only if the interface is 5030Sstevel@tonic-gate * present in the kernel. 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate if (pi->pi_kernel_state & PI_PRESENT) { 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) { 5080Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 5090Sstevel@tonic-gate check_to_advertise(pi, START_INIT_ADV); 5100Sstevel@tonic-gate } else { 5110Sstevel@tonic-gate if (pi->pi_sol_state == NO_SOLICIT) 5120Sstevel@tonic-gate check_to_solicit(pi, START_INIT_SOLICIT); 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* 5170Sstevel@tonic-gate * Track static kernel prefixes to prevent in.ndpd from clobbering 5180Sstevel@tonic-gate * them by creating a struct prefix for each prefix detected in the 5190Sstevel@tonic-gate * kernel. 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate pr = prefix_lookup_name(pi, ifname); 5220Sstevel@tonic-gate if (pr == NULL) { 5230Sstevel@tonic-gate pr = prefix_create_name(pi, ifname); 5240Sstevel@tonic-gate if (pr == NULL) { 5250Sstevel@tonic-gate logmsg(LOG_ERR, "if_process: out of memory\n"); 5260Sstevel@tonic-gate return; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate if (prefix_init_from_k(pr) == -1) { 5290Sstevel@tonic-gate prefix_delete(pr); 5300Sstevel@tonic-gate return; 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate /* Detect prefixes which are removed */ 5340Sstevel@tonic-gate if (pr->pr_kernel_state != 0) 5350Sstevel@tonic-gate pr->pr_in_use = _B_TRUE; 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate static int ifsock = -1; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* 5410Sstevel@tonic-gate * Scan all interfaces to detect changes as well as new and deleted intefaces 5420Sstevel@tonic-gate * 'first' is set for the initial call only. Do not effect anything. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate static void 5450Sstevel@tonic-gate initifs(boolean_t first) 5460Sstevel@tonic-gate { 5470Sstevel@tonic-gate char *buf; 5480Sstevel@tonic-gate int bufsize; 5490Sstevel@tonic-gate int numifs; 5500Sstevel@tonic-gate int n; 5510Sstevel@tonic-gate struct lifnum lifn; 5520Sstevel@tonic-gate struct lifconf lifc; 5530Sstevel@tonic-gate struct lifreq *lifr; 5540Sstevel@tonic-gate struct phyint *pi; 5550Sstevel@tonic-gate struct phyint *next_pi; 5560Sstevel@tonic-gate struct prefix *pr; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate if (debug & D_IFSCAN) 5590Sstevel@tonic-gate logmsg(LOG_DEBUG, "Reading interface configuration\n"); 5600Sstevel@tonic-gate if (ifsock < 0) { 5610Sstevel@tonic-gate ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 5620Sstevel@tonic-gate if (ifsock < 0) { 5630Sstevel@tonic-gate logperror("initifs: socket"); 5640Sstevel@tonic-gate return; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate lifn.lifn_family = AF_INET6; 5680Sstevel@tonic-gate lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY; 5690Sstevel@tonic-gate if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) { 5700Sstevel@tonic-gate logperror("initifs: ioctl (get interface numbers)"); 5710Sstevel@tonic-gate return; 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate numifs = lifn.lifn_count; 5740Sstevel@tonic-gate bufsize = numifs * sizeof (struct lifreq); 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate buf = (char *)malloc(bufsize); 5770Sstevel@tonic-gate if (buf == NULL) { 5780Sstevel@tonic-gate logmsg(LOG_ERR, "initifs: out of memory\n"); 5790Sstevel@tonic-gate return; 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * Mark the interfaces so that we can find phyints and prefixes 5840Sstevel@tonic-gate * which have disappeared from the kernel. 5850Sstevel@tonic-gate * if_process will set pr_in_use when it finds the interface 5860Sstevel@tonic-gate * in the kernel. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 5890Sstevel@tonic-gate /* 5900Sstevel@tonic-gate * Before re-examining the state of the interfaces, 5910Sstevel@tonic-gate * PI_PRESENT should be cleared from pi_kernel_state. 5920Sstevel@tonic-gate */ 5930Sstevel@tonic-gate pi->pi_kernel_state &= ~PI_PRESENT; 5940Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 5950Sstevel@tonic-gate pr->pr_in_use = _B_FALSE; 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate lifc.lifc_family = AF_INET6; 6000Sstevel@tonic-gate lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY; 6010Sstevel@tonic-gate lifc.lifc_len = bufsize; 6020Sstevel@tonic-gate lifc.lifc_buf = buf; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) { 6050Sstevel@tonic-gate logperror("initifs: ioctl (get interface configuration)"); 6060Sstevel@tonic-gate free(buf); 6070Sstevel@tonic-gate return; 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate lifr = (struct lifreq *)lifc.lifc_req; 6110Sstevel@tonic-gate for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++) 6120Sstevel@tonic-gate if_process(ifsock, lifr->lifr_name, first); 6130Sstevel@tonic-gate free(buf); 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate /* 6160Sstevel@tonic-gate * Detect phyints that have been removed from the kernel. 6170Sstevel@tonic-gate * Since we can't recreate it here (would require ifconfig plumb 6180Sstevel@tonic-gate * logic) we just terminate use of that phyint. 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 6210Sstevel@tonic-gate next_pi = pi->pi_next; 6220Sstevel@tonic-gate /* 6230Sstevel@tonic-gate * If interface (still) exists in kernel, set 6240Sstevel@tonic-gate * pi_state to indicate that. 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate if (pi->pi_kernel_state & PI_PRESENT) { 6270Sstevel@tonic-gate pi->pi_state |= PI_PRESENT; 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate check_if_removed(pi); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate if (show_ifs) 6330Sstevel@tonic-gate phyint_print_all(); 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * Router advertisement state machine. Used for everything but timer 6390Sstevel@tonic-gate * events which use advertise_event directly. 6400Sstevel@tonic-gate */ 6410Sstevel@tonic-gate void 6420Sstevel@tonic-gate check_to_advertise(struct phyint *pi, enum adv_events event) 6430Sstevel@tonic-gate { 6440Sstevel@tonic-gate uint_t delay; 6450Sstevel@tonic-gate enum adv_states old_state = pi->pi_adv_state; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate if (debug & D_STATE) { 6480Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n", 6490Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate delay = advertise_event(pi, event, 0); 6520Sstevel@tonic-gate if (delay != TIMER_INFINITY) { 6530Sstevel@tonic-gate /* Make sure the global next event is updated */ 6540Sstevel@tonic-gate timer_schedule(delay); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (debug & D_STATE) { 6580Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n", 6590Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state, 6600Sstevel@tonic-gate (int)pi->pi_adv_state); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate /* 6650Sstevel@tonic-gate * Router advertisement state machine. 6660Sstevel@tonic-gate * Return the number of milliseconds until next timeout (TIMER_INFINITY 6670Sstevel@tonic-gate * if never). 6680Sstevel@tonic-gate * For the ADV_TIMER event the caller passes in the number of milliseconds 6690Sstevel@tonic-gate * since the last timer event in the 'elapsed' parameter. 6700Sstevel@tonic-gate */ 6710Sstevel@tonic-gate uint_t 6720Sstevel@tonic-gate advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed) 6730Sstevel@tonic-gate { 6740Sstevel@tonic-gate uint_t delay; 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate if (debug & D_STATE) { 6770Sstevel@tonic-gate logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n", 6780Sstevel@tonic-gate pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate check_daemonize(); 6810Sstevel@tonic-gate if (!pi->pi_AdvSendAdvertisements) 6820Sstevel@tonic-gate return (TIMER_INFINITY); 6830Sstevel@tonic-gate if (pi->pi_flags & IFF_NORTEXCH) { 6840Sstevel@tonic-gate if (debug & D_PKTOUT) { 6850Sstevel@tonic-gate logmsg(LOG_DEBUG, "Suppress sending RA packet on %s " 6860Sstevel@tonic-gate "(no route exchange on interface)\n", 6870Sstevel@tonic-gate pi->pi_name); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate return (TIMER_INFINITY); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate switch (event) { 6930Sstevel@tonic-gate case ADV_OFF: 6940Sstevel@tonic-gate pi->pi_adv_state = NO_ADV; 6950Sstevel@tonic-gate return (TIMER_INFINITY); 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate case START_INIT_ADV: 6980Sstevel@tonic-gate if (pi->pi_adv_state == INIT_ADV) 6990Sstevel@tonic-gate return (pi->pi_adv_time_left); 7000Sstevel@tonic-gate pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS; 7010Sstevel@tonic-gate pi->pi_adv_time_left = 0; 7020Sstevel@tonic-gate pi->pi_adv_state = INIT_ADV; 7030Sstevel@tonic-gate break; /* send advertisement */ 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate case START_FINAL_ADV: 7060Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 7070Sstevel@tonic-gate return (TIMER_INFINITY); 7080Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) 7090Sstevel@tonic-gate return (pi->pi_adv_time_left); 7100Sstevel@tonic-gate pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS; 7110Sstevel@tonic-gate pi->pi_adv_time_left = 0; 7120Sstevel@tonic-gate pi->pi_adv_state = FINAL_ADV; 7130Sstevel@tonic-gate break; /* send advertisement */ 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate case RECEIVED_SOLICIT: 7160Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 7170Sstevel@tonic-gate return (TIMER_INFINITY); 7180Sstevel@tonic-gate if (pi->pi_adv_state == SOLICIT_ADV) { 7190Sstevel@tonic-gate if (pi->pi_adv_time_left != 0) 7200Sstevel@tonic-gate return (pi->pi_adv_time_left); 7210Sstevel@tonic-gate break; 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME); 7240Sstevel@tonic-gate if (delay < pi->pi_adv_time_left) 7250Sstevel@tonic-gate pi->pi_adv_time_left = delay; 7260Sstevel@tonic-gate if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) { 7270Sstevel@tonic-gate /* 7280Sstevel@tonic-gate * Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS 7290Sstevel@tonic-gate * plus random delay) after the previous 7300Sstevel@tonic-gate * advertisement was sent. 7310Sstevel@tonic-gate */ 7320Sstevel@tonic-gate pi->pi_adv_time_left = delay + 7330Sstevel@tonic-gate ND_MIN_DELAY_BETWEEN_RAS - 7340Sstevel@tonic-gate pi->pi_adv_time_since_sent; 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate pi->pi_adv_state = SOLICIT_ADV; 7370Sstevel@tonic-gate break; 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate case ADV_TIMER: 7400Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 7410Sstevel@tonic-gate return (TIMER_INFINITY); 7420Sstevel@tonic-gate /* Decrease time left */ 7430Sstevel@tonic-gate if (pi->pi_adv_time_left >= elapsed) 7440Sstevel@tonic-gate pi->pi_adv_time_left -= elapsed; 7450Sstevel@tonic-gate else 7460Sstevel@tonic-gate pi->pi_adv_time_left = 0; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate /* Increase time since last advertisement was sent */ 7490Sstevel@tonic-gate pi->pi_adv_time_since_sent += elapsed; 7500Sstevel@tonic-gate break; 7510Sstevel@tonic-gate default: 7520Sstevel@tonic-gate logmsg(LOG_ERR, "advertise_event: Unknown event %d\n", 7530Sstevel@tonic-gate (int)event); 7540Sstevel@tonic-gate return (TIMER_INFINITY); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate if (pi->pi_adv_time_left != 0) 7580Sstevel@tonic-gate return (pi->pi_adv_time_left); 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate /* Send advertisement and calculate next time to send */ 7610Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) { 7620Sstevel@tonic-gate /* Omit the prefixes */ 7630Sstevel@tonic-gate advertise(&v6allnodes, pi, _B_TRUE); 7640Sstevel@tonic-gate } else { 7650Sstevel@tonic-gate advertise(&v6allnodes, pi, _B_FALSE); 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate pi->pi_adv_time_since_sent = 0; 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate switch (pi->pi_adv_state) { 7700Sstevel@tonic-gate case SOLICIT_ADV: 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * The solicited advertisement has been sent. 7730Sstevel@tonic-gate * Revert to periodic advertisements. 7740Sstevel@tonic-gate */ 7750Sstevel@tonic-gate pi->pi_adv_state = REG_ADV; 7760Sstevel@tonic-gate /* FALLTHRU */ 7770Sstevel@tonic-gate case REG_ADV: 7780Sstevel@tonic-gate pi->pi_adv_time_left = 7790Sstevel@tonic-gate GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 7800Sstevel@tonic-gate 1000 * pi->pi_MaxRtrAdvInterval); 7810Sstevel@tonic-gate break; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate case INIT_ADV: 7840Sstevel@tonic-gate if (--pi->pi_adv_count > 0) { 7850Sstevel@tonic-gate delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 7860Sstevel@tonic-gate 1000 * pi->pi_MaxRtrAdvInterval); 7870Sstevel@tonic-gate if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL) 7880Sstevel@tonic-gate delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL; 7890Sstevel@tonic-gate pi->pi_adv_time_left = delay; 7900Sstevel@tonic-gate } else { 7910Sstevel@tonic-gate pi->pi_adv_time_left = 7920Sstevel@tonic-gate GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 7930Sstevel@tonic-gate 1000 * pi->pi_MaxRtrAdvInterval); 7940Sstevel@tonic-gate pi->pi_adv_state = REG_ADV; 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate break; 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate case FINAL_ADV: 7990Sstevel@tonic-gate if (--pi->pi_adv_count > 0) { 8000Sstevel@tonic-gate pi->pi_adv_time_left = 8010Sstevel@tonic-gate ND_MAX_INITIAL_RTR_ADVERT_INTERVAL; 8020Sstevel@tonic-gate } else { 8030Sstevel@tonic-gate pi->pi_adv_state = NO_ADV; 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate break; 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate if (pi->pi_adv_state != NO_ADV) 8080Sstevel@tonic-gate return (pi->pi_adv_time_left); 8090Sstevel@tonic-gate else 8100Sstevel@tonic-gate return (TIMER_INFINITY); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* 8140Sstevel@tonic-gate * Router solicitation state machine. Used for everything but timer 8150Sstevel@tonic-gate * events which use solicit_event directly. 8160Sstevel@tonic-gate */ 8170Sstevel@tonic-gate void 8180Sstevel@tonic-gate check_to_solicit(struct phyint *pi, enum solicit_events event) 8190Sstevel@tonic-gate { 8200Sstevel@tonic-gate uint_t delay; 8210Sstevel@tonic-gate enum solicit_states old_state = pi->pi_sol_state; 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate if (debug & D_STATE) { 8240Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n", 8250Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate delay = solicit_event(pi, event, 0); 8280Sstevel@tonic-gate if (delay != TIMER_INFINITY) { 8290Sstevel@tonic-gate /* Make sure the global next event is updated */ 8300Sstevel@tonic-gate timer_schedule(delay); 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate if (debug & D_STATE) { 8340Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n", 8350Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state, 8360Sstevel@tonic-gate (int)pi->pi_sol_state); 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate static void 8410Sstevel@tonic-gate daemonize_ndpd(void) 8420Sstevel@tonic-gate { 8430Sstevel@tonic-gate FILE *pidfp; 8440Sstevel@tonic-gate mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */ 8450Sstevel@tonic-gate struct itimerval it; 8460Sstevel@tonic-gate boolean_t timerval = _B_TRUE; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate /* 8490Sstevel@tonic-gate * Need to get current timer settings so they can be restored 8500Sstevel@tonic-gate * after the fork(), as the it_value and it_interval values for 8510Sstevel@tonic-gate * the ITIMER_REAL timer are reset to 0 in the child process. 8520Sstevel@tonic-gate */ 8530Sstevel@tonic-gate if (getitimer(ITIMER_REAL, &it) < 0) { 8540Sstevel@tonic-gate if (debug & D_TIMER) 8550Sstevel@tonic-gate logmsg(LOG_DEBUG, 8560Sstevel@tonic-gate "daemonize_ndpd: failed to get itimerval\n"); 8570Sstevel@tonic-gate timerval = _B_FALSE; 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate /* Daemonize. */ 8610Sstevel@tonic-gate switch (fork()) { 8620Sstevel@tonic-gate case 0: 8630Sstevel@tonic-gate /* Child */ 8640Sstevel@tonic-gate break; 8650Sstevel@tonic-gate case -1: 8660Sstevel@tonic-gate logperror("fork"); 8670Sstevel@tonic-gate exit(1); 8680Sstevel@tonic-gate default: 8690Sstevel@tonic-gate /* Parent */ 8700Sstevel@tonic-gate _exit(0); 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* Store our process id, blow away any existing file if it exists. */ 8740Sstevel@tonic-gate if ((pidfp = fopen(PATH_PID, "w")) == NULL) { 8750Sstevel@tonic-gate (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n", 8760Sstevel@tonic-gate argv0[0], strerror(errno)); 8770Sstevel@tonic-gate } else { 8780Sstevel@tonic-gate (void) fprintf(pidfp, "%ld\n", getpid()); 8790Sstevel@tonic-gate (void) fclose(pidfp); 8800Sstevel@tonic-gate (void) chmod(PATH_PID, pidmode); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate (void) close(0); 8840Sstevel@tonic-gate (void) close(1); 8850Sstevel@tonic-gate (void) close(2); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate (void) chdir("/"); 8880Sstevel@tonic-gate (void) open("/dev/null", O_RDWR); 8890Sstevel@tonic-gate (void) dup2(0, 1); 8900Sstevel@tonic-gate (void) dup2(0, 2); 8910Sstevel@tonic-gate (void) setsid(); 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate already_daemonized = _B_TRUE; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * Restore timer values, if we were able to save them; if not, 8970Sstevel@tonic-gate * check and set the right value by calling run_timeouts(). 8980Sstevel@tonic-gate */ 8990Sstevel@tonic-gate if (timerval) { 9000Sstevel@tonic-gate if (setitimer(ITIMER_REAL, &it, NULL) < 0) { 9010Sstevel@tonic-gate logperror("daemonize_ndpd: setitimer"); 9020Sstevel@tonic-gate exit(2); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate } else { 9050Sstevel@tonic-gate run_timeouts(); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate /* 9100Sstevel@tonic-gate * Check to see if the time is right to daemonize. The right time is when: 9110Sstevel@tonic-gate * 9120Sstevel@tonic-gate * 1. We haven't already daemonized. 9130Sstevel@tonic-gate * 2. We are not in debug mode. 9140Sstevel@tonic-gate * 3. All interfaces are marked IFF_NOXMIT. 9150Sstevel@tonic-gate * 4. All non-router interfaces have their prefixes set up and we're 9160Sstevel@tonic-gate * done sending router solicitations on those interfaces without 9170Sstevel@tonic-gate * prefixes. 9180Sstevel@tonic-gate */ 9190Sstevel@tonic-gate static void 9200Sstevel@tonic-gate check_daemonize(void) 9210Sstevel@tonic-gate { 9220Sstevel@tonic-gate struct phyint *pi; 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate if (already_daemonized || debug != 0) 9250Sstevel@tonic-gate return; 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 9280Sstevel@tonic-gate if (!(pi->pi_flags & IFF_NOXMIT)) 9290Sstevel@tonic-gate break; 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * If we can't transmit on any of the interfaces there is no reason 9340Sstevel@tonic-gate * to hold up progress. 9350Sstevel@tonic-gate */ 9360Sstevel@tonic-gate if (pi == NULL) { 9370Sstevel@tonic-gate daemonize_ndpd(); 9380Sstevel@tonic-gate return; 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate /* Check all interfaces. If any are still soliciting, just return. */ 9420Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 9430Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements || 9440Sstevel@tonic-gate !(pi->pi_kernel_state & PI_PRESENT)) 9450Sstevel@tonic-gate continue; 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate if (pi->pi_sol_state == INIT_SOLICIT) 9480Sstevel@tonic-gate return; 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate daemonize_ndpd(); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate /* 9550Sstevel@tonic-gate * Router solicitation state machine. 9560Sstevel@tonic-gate * Return the number of milliseconds until next timeout (TIMER_INFINITY 9570Sstevel@tonic-gate * if never). 9580Sstevel@tonic-gate * For the SOL_TIMER event the caller passes in the number of milliseconds 9590Sstevel@tonic-gate * since the last timer event in the 'elapsed' parameter. 9600Sstevel@tonic-gate */ 9610Sstevel@tonic-gate uint_t 9620Sstevel@tonic-gate solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed) 9630Sstevel@tonic-gate { 9640Sstevel@tonic-gate if (debug & D_STATE) { 9650Sstevel@tonic-gate logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n", 9660Sstevel@tonic-gate pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state); 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) 9700Sstevel@tonic-gate return (TIMER_INFINITY); 9710Sstevel@tonic-gate if (pi->pi_flags & IFF_NORTEXCH) { 9720Sstevel@tonic-gate if (debug & D_PKTOUT) { 9730Sstevel@tonic-gate logmsg(LOG_DEBUG, "Suppress sending RS packet on %s " 9740Sstevel@tonic-gate "(no route exchange on interface)\n", 9750Sstevel@tonic-gate pi->pi_name); 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate return (TIMER_INFINITY); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate switch (event) { 9810Sstevel@tonic-gate case SOLICIT_OFF: 9820Sstevel@tonic-gate pi->pi_sol_state = NO_SOLICIT; 9830Sstevel@tonic-gate check_daemonize(); 9840Sstevel@tonic-gate return (TIMER_INFINITY); 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate case SOLICIT_DONE: 9870Sstevel@tonic-gate pi->pi_sol_state = DONE_SOLICIT; 9880Sstevel@tonic-gate check_daemonize(); 9890Sstevel@tonic-gate return (TIMER_INFINITY); 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate case START_INIT_SOLICIT: 9920Sstevel@tonic-gate if (pi->pi_sol_state == INIT_SOLICIT) 9930Sstevel@tonic-gate return (pi->pi_sol_time_left); 9940Sstevel@tonic-gate pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS; 9950Sstevel@tonic-gate pi->pi_sol_time_left = 9960Sstevel@tonic-gate GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY); 9970Sstevel@tonic-gate pi->pi_sol_state = INIT_SOLICIT; 9980Sstevel@tonic-gate break; 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate case SOL_TIMER: 10010Sstevel@tonic-gate if (pi->pi_sol_state == NO_SOLICIT) 10020Sstevel@tonic-gate return (TIMER_INFINITY); 10030Sstevel@tonic-gate /* Decrease time left */ 10040Sstevel@tonic-gate if (pi->pi_sol_time_left >= elapsed) 10050Sstevel@tonic-gate pi->pi_sol_time_left -= elapsed; 10060Sstevel@tonic-gate else 10070Sstevel@tonic-gate pi->pi_sol_time_left = 0; 10080Sstevel@tonic-gate break; 10090Sstevel@tonic-gate default: 10100Sstevel@tonic-gate logmsg(LOG_ERR, "solicit_event: Unknown event %d\n", 10110Sstevel@tonic-gate (int)event); 10120Sstevel@tonic-gate return (TIMER_INFINITY); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate if (pi->pi_sol_time_left != 0) 10160Sstevel@tonic-gate return (pi->pi_sol_time_left); 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate /* Send solicitation and calculate next time */ 10190Sstevel@tonic-gate switch (pi->pi_sol_state) { 10200Sstevel@tonic-gate case INIT_SOLICIT: 10210Sstevel@tonic-gate solicit(&v6allrouters, pi); 10220Sstevel@tonic-gate if (--pi->pi_sol_count == 0) { 10230Sstevel@tonic-gate pi->pi_sol_state = DONE_SOLICIT; 10240Sstevel@tonic-gate check_daemonize(); 10250Sstevel@tonic-gate return (TIMER_INFINITY); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL; 10280Sstevel@tonic-gate return (pi->pi_sol_time_left); 10290Sstevel@tonic-gate case NO_SOLICIT: 10300Sstevel@tonic-gate case DONE_SOLICIT: 10310Sstevel@tonic-gate return (TIMER_INFINITY); 10320Sstevel@tonic-gate default: 10330Sstevel@tonic-gate return (pi->pi_sol_time_left); 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate /* 10380Sstevel@tonic-gate * Timer mechanism using relative time (in milliseconds) from the 10390Sstevel@tonic-gate * previous timer event. Timers exceeding TIMER_INFINITY milliseconds 10400Sstevel@tonic-gate * will fire after TIMER_INFINITY milliseconds. 10410Sstevel@tonic-gate */ 10420Sstevel@tonic-gate static uint_t timer_previous; /* When last SIGALRM occurred */ 10430Sstevel@tonic-gate static uint_t timer_next; /* Currently scheduled timeout */ 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate static void 10460Sstevel@tonic-gate timer_init(void) 10470Sstevel@tonic-gate { 10480Sstevel@tonic-gate timer_previous = getcurrenttime(); 10490Sstevel@tonic-gate timer_next = TIMER_INFINITY; 10500Sstevel@tonic-gate run_timeouts(); 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate /* 10540Sstevel@tonic-gate * Make sure the next SIGALRM occurs delay milliseconds from the current 10550Sstevel@tonic-gate * time if not earlier. 10560Sstevel@tonic-gate * Handles getcurrenttime (32 bit integer holding milliseconds) wraparound 10570Sstevel@tonic-gate * by treating differences greater than 0x80000000 as negative. 10580Sstevel@tonic-gate */ 10590Sstevel@tonic-gate void 10600Sstevel@tonic-gate timer_schedule(uint_t delay) 10610Sstevel@tonic-gate { 10620Sstevel@tonic-gate uint_t now; 10630Sstevel@tonic-gate struct itimerval itimerval; 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate now = getcurrenttime(); 10660Sstevel@tonic-gate if (debug & D_TIMER) { 10670Sstevel@tonic-gate logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n", 10680Sstevel@tonic-gate delay, now, timer_next); 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate /* Will this timer occur before the currently scheduled SIGALRM? */ 10710Sstevel@tonic-gate if (delay >= timer_next - now) { 10720Sstevel@tonic-gate if (debug & D_TIMER) { 10730Sstevel@tonic-gate logmsg(LOG_DEBUG, "timer_schedule(%u): no action - " 10740Sstevel@tonic-gate "next in %u ms\n", 10750Sstevel@tonic-gate delay, timer_next - now); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate return; 10780Sstevel@tonic-gate } 10790Sstevel@tonic-gate if (delay == 0) { 10800Sstevel@tonic-gate /* Minimum allowed delay */ 10810Sstevel@tonic-gate delay = 1; 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate timer_next = now + delay; 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate itimerval.it_value.tv_sec = delay / 1000; 10860Sstevel@tonic-gate itimerval.it_value.tv_usec = (delay % 1000) * 1000; 10870Sstevel@tonic-gate itimerval.it_interval.tv_sec = 0; 10880Sstevel@tonic-gate itimerval.it_interval.tv_usec = 0; 10890Sstevel@tonic-gate if (debug & D_TIMER) { 10900Sstevel@tonic-gate logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n", 10910Sstevel@tonic-gate delay, 10920Sstevel@tonic-gate itimerval.it_value.tv_sec, itimerval.it_value.tv_usec); 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) { 10950Sstevel@tonic-gate logperror("timer_schedule: setitimer"); 10960Sstevel@tonic-gate exit(2); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate /* 11010Sstevel@tonic-gate * Conditional running of timer. If more than 'minimal_time' millseconds 11020Sstevel@tonic-gate * since the timer routines were last run we run them. 11030Sstevel@tonic-gate * Used when packets arrive. 11040Sstevel@tonic-gate */ 11050Sstevel@tonic-gate static void 11060Sstevel@tonic-gate conditional_run_timeouts(uint_t minimal_time) 11070Sstevel@tonic-gate { 11080Sstevel@tonic-gate uint_t now; 11090Sstevel@tonic-gate uint_t elapsed; 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate now = getcurrenttime(); 11120Sstevel@tonic-gate elapsed = now - timer_previous; 11130Sstevel@tonic-gate if (elapsed > minimal_time) { 11140Sstevel@tonic-gate if (debug & D_TIMER) { 11150Sstevel@tonic-gate logmsg(LOG_DEBUG, "conditional_run_timeouts: " 11160Sstevel@tonic-gate "elapsed %d\n", elapsed); 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate run_timeouts(); 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate /* 11230Sstevel@tonic-gate * Timer has fired. 11240Sstevel@tonic-gate * Determine when the next timer event will occur by asking all 11250Sstevel@tonic-gate * the timer routines. 11260Sstevel@tonic-gate * Should not be called from a timer routine but in some cases this is 11270Sstevel@tonic-gate * done because the code doesn't know that e.g. it was called from 11280Sstevel@tonic-gate * ifconfig_timer(). In this case the nested run_timeouts will just return but 11290Sstevel@tonic-gate * the running run_timeouts will ensure to call all the timer functions by 11300Sstevel@tonic-gate * looping once more. 11310Sstevel@tonic-gate */ 11320Sstevel@tonic-gate static void 11330Sstevel@tonic-gate run_timeouts(void) 11340Sstevel@tonic-gate { 11350Sstevel@tonic-gate uint_t now; 11360Sstevel@tonic-gate uint_t elapsed; 11370Sstevel@tonic-gate uint_t next; 11380Sstevel@tonic-gate uint_t nexti; 11390Sstevel@tonic-gate struct phyint *pi; 11400Sstevel@tonic-gate struct phyint *next_pi; 11410Sstevel@tonic-gate struct prefix *pr; 11420Sstevel@tonic-gate struct prefix *next_pr; 11430Sstevel@tonic-gate struct adv_prefix *adv_pr; 11440Sstevel@tonic-gate struct adv_prefix *next_adv_pr; 11450Sstevel@tonic-gate struct router *dr; 11460Sstevel@tonic-gate struct router *next_dr; 11470Sstevel@tonic-gate static boolean_t timeout_running; 11480Sstevel@tonic-gate static boolean_t do_retry; 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate if (timeout_running) { 11510Sstevel@tonic-gate if (debug & D_TIMER) 11520Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: nested call\n"); 11530Sstevel@tonic-gate do_retry = _B_TRUE; 11540Sstevel@tonic-gate return; 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate timeout_running = _B_TRUE; 11570Sstevel@tonic-gate retry: 11580Sstevel@tonic-gate /* How much time since the last time we were called? */ 11590Sstevel@tonic-gate now = getcurrenttime(); 11600Sstevel@tonic-gate elapsed = now - timer_previous; 11610Sstevel@tonic-gate timer_previous = now; 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate if (debug & D_TIMER) 11640Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate next = TIMER_INFINITY; 11670Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 11680Sstevel@tonic-gate next_pi = pi->pi_next; 11690Sstevel@tonic-gate nexti = phyint_timer(pi, elapsed); 11700Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 11710Sstevel@tonic-gate next = nexti; 11720Sstevel@tonic-gate if (debug & D_TIMER) { 11730Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n", 11740Sstevel@tonic-gate pi->pi_name, nexti, next); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 11770Sstevel@tonic-gate next_pr = pr->pr_next; 11780Sstevel@tonic-gate nexti = prefix_timer(pr, elapsed); 11790Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 11800Sstevel@tonic-gate next = nexti; 11810Sstevel@tonic-gate if (debug & D_TIMER) { 11820Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (pr %s): " 11830Sstevel@tonic-gate "%d -> %u ms\n", pr->pr_name, nexti, next); 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate } 11860Sstevel@tonic-gate for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL; 11870Sstevel@tonic-gate adv_pr = next_adv_pr) { 11880Sstevel@tonic-gate next_adv_pr = adv_pr->adv_pr_next; 11890Sstevel@tonic-gate nexti = adv_prefix_timer(adv_pr, elapsed); 11900Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 11910Sstevel@tonic-gate next = nexti; 11920Sstevel@tonic-gate if (debug & D_TIMER) { 11930Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts " 11940Sstevel@tonic-gate "(adv pr on %s): %d -> %u ms\n", 11950Sstevel@tonic-gate adv_pr->adv_pr_physical->pi_name, 11960Sstevel@tonic-gate nexti, next); 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate } 11990Sstevel@tonic-gate for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) { 12000Sstevel@tonic-gate next_dr = dr->dr_next; 12010Sstevel@tonic-gate nexti = router_timer(dr, elapsed); 12020Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12030Sstevel@tonic-gate next = nexti; 12040Sstevel@tonic-gate if (debug & D_TIMER) { 12050Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (dr): " 12060Sstevel@tonic-gate "%d -> %u ms\n", nexti, next); 12070Sstevel@tonic-gate } 12080Sstevel@tonic-gate } 12090Sstevel@tonic-gate if (pi->pi_TmpAddrsEnabled) { 12100Sstevel@tonic-gate nexti = tmptoken_timer(pi, elapsed); 12110Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12120Sstevel@tonic-gate next = nexti; 12130Sstevel@tonic-gate if (debug & D_TIMER) { 12140Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): " 12150Sstevel@tonic-gate "%d -> %u ms\n", pi->pi_name, nexti, next); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate /* 12200Sstevel@tonic-gate * Make sure the timer functions are run at least once 12210Sstevel@tonic-gate * an hour. 12220Sstevel@tonic-gate */ 12230Sstevel@tonic-gate if (next == TIMER_INFINITY) 12240Sstevel@tonic-gate next = 3600 * 1000; /* 1 hour */ 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate if (debug & D_TIMER) 12270Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next); 12280Sstevel@tonic-gate timer_schedule(next); 12290Sstevel@tonic-gate if (do_retry) { 12300Sstevel@tonic-gate if (debug & D_TIMER) 12310Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: retry\n"); 12320Sstevel@tonic-gate do_retry = _B_FALSE; 12330Sstevel@tonic-gate goto retry; 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate timeout_running = _B_FALSE; 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate static int eventpipe_read = -1; /* Used for synchronous signal delivery */ 12390Sstevel@tonic-gate static int eventpipe_write = -1; 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate /* 12420Sstevel@tonic-gate * Ensure that signals are processed synchronously with the rest of 12430Sstevel@tonic-gate * the code by just writing a one character signal number on the pipe. 12440Sstevel@tonic-gate * The poll loop will pick this up and process the signal event. 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate static void 12470Sstevel@tonic-gate sig_handler(int signo) 12480Sstevel@tonic-gate { 12490Sstevel@tonic-gate uchar_t buf = (uchar_t)signo; 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate if (eventpipe_write == -1) { 12520Sstevel@tonic-gate logmsg(LOG_ERR, "sig_handler: no pipe\n"); 12530Sstevel@tonic-gate return; 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate if (write(eventpipe_write, &buf, sizeof (buf)) < 0) 12560Sstevel@tonic-gate logperror("sig_handler: write"); 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate /* 12600Sstevel@tonic-gate * Pick up a signal "byte" from the pipe and process it. 12610Sstevel@tonic-gate */ 12620Sstevel@tonic-gate static void 12630Sstevel@tonic-gate in_signal(int fd) 12640Sstevel@tonic-gate { 12650Sstevel@tonic-gate uchar_t buf; 12660Sstevel@tonic-gate struct phyint *pi; 12670Sstevel@tonic-gate struct phyint *next_pi; 12680Sstevel@tonic-gate 12690Sstevel@tonic-gate switch (read(fd, &buf, sizeof (buf))) { 12700Sstevel@tonic-gate case -1: 12710Sstevel@tonic-gate logperror("in_signal: read"); 12720Sstevel@tonic-gate exit(1); 12730Sstevel@tonic-gate /* NOTREACHED */ 12740Sstevel@tonic-gate case 1: 12750Sstevel@tonic-gate break; 12760Sstevel@tonic-gate case 0: 12770Sstevel@tonic-gate logmsg(LOG_ERR, "in_signal: read eof\n"); 12780Sstevel@tonic-gate exit(1); 12790Sstevel@tonic-gate /* NOTREACHED */ 12800Sstevel@tonic-gate default: 12810Sstevel@tonic-gate logmsg(LOG_ERR, "in_signal: read > 1\n"); 12820Sstevel@tonic-gate exit(1); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate if (debug & D_TIMER) 12860Sstevel@tonic-gate logmsg(LOG_DEBUG, "in_signal() got %d\n", buf); 12870Sstevel@tonic-gate 12880Sstevel@tonic-gate switch (buf) { 12890Sstevel@tonic-gate case SIGALRM: 12900Sstevel@tonic-gate if (debug & D_TIMER) { 12910Sstevel@tonic-gate uint_t now = getcurrenttime(); 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n", 12940Sstevel@tonic-gate now - timer_next); 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate timer_next = TIMER_INFINITY; 12970Sstevel@tonic-gate run_timeouts(); 12980Sstevel@tonic-gate break; 12990Sstevel@tonic-gate case SIGHUP: 13000Sstevel@tonic-gate /* Re-read config file by exec'ing ourselves */ 13010Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 13020Sstevel@tonic-gate next_pi = pi->pi_next; 13030Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) 13040Sstevel@tonic-gate check_to_advertise(pi, START_FINAL_ADV); 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate phyint_delete(pi); 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate /* 13100Sstevel@tonic-gate * Prevent fd leaks. Everything gets re-opened at start-up 13110Sstevel@tonic-gate * time. 0, 1, and 2 are closed and re-opened as 13120Sstevel@tonic-gate * /dev/null, so we'll leave those open. 13130Sstevel@tonic-gate */ 13140Sstevel@tonic-gate closefrom(3); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n"); 13170Sstevel@tonic-gate (void) execv(argv0[0], argv0); 13180Sstevel@tonic-gate (void) unlink(PATH_PID); 13190Sstevel@tonic-gate _exit(0177); 13200Sstevel@tonic-gate /* NOTREACHED */ 13210Sstevel@tonic-gate case SIGUSR1: 13220Sstevel@tonic-gate logmsg(LOG_DEBUG, "Printing configuration:\n"); 13230Sstevel@tonic-gate phyint_print_all(); 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate case SIGINT: 13260Sstevel@tonic-gate case SIGTERM: 13270Sstevel@tonic-gate case SIGQUIT: 13280Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 13290Sstevel@tonic-gate next_pi = pi->pi_next; 13300Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) 13310Sstevel@tonic-gate check_to_advertise(pi, START_FINAL_ADV); 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate phyint_delete(pi); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate (void) unlink(PATH_PID); 13360Sstevel@tonic-gate exit(0); 13370Sstevel@tonic-gate /* NOTREACHED */ 13380Sstevel@tonic-gate case 255: 13390Sstevel@tonic-gate /* 13400Sstevel@tonic-gate * Special "signal" from looback_ra_enqueue. 13410Sstevel@tonic-gate * Handle any queued loopback router advertisements. 13420Sstevel@tonic-gate */ 13430Sstevel@tonic-gate loopback_ra_dequeue(); 13440Sstevel@tonic-gate break; 13450Sstevel@tonic-gate default: 13460Sstevel@tonic-gate logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf); 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate } 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate /* 13510Sstevel@tonic-gate * Create pipe for signal delivery and set up signal handlers. 13520Sstevel@tonic-gate */ 13530Sstevel@tonic-gate static void 13540Sstevel@tonic-gate setup_eventpipe(void) 13550Sstevel@tonic-gate { 13560Sstevel@tonic-gate int fds[2]; 13570Sstevel@tonic-gate struct sigaction act; 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate if ((pipe(fds)) < 0) { 13600Sstevel@tonic-gate logperror("setup_eventpipe: pipe"); 13610Sstevel@tonic-gate exit(1); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate eventpipe_read = fds[0]; 13640Sstevel@tonic-gate eventpipe_write = fds[1]; 13650Sstevel@tonic-gate if (poll_add(eventpipe_read) == -1) { 13660Sstevel@tonic-gate exit(1); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate act.sa_handler = sig_handler; 13690Sstevel@tonic-gate act.sa_flags = SA_RESTART; 13700Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate (void) sigset(SIGHUP, sig_handler); 13730Sstevel@tonic-gate (void) sigset(SIGUSR1, sig_handler); 13740Sstevel@tonic-gate (void) sigset(SIGTERM, sig_handler); 13750Sstevel@tonic-gate (void) sigset(SIGINT, sig_handler); 13760Sstevel@tonic-gate (void) sigset(SIGQUIT, sig_handler); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate /* 13800Sstevel@tonic-gate * Create a routing socket for receiving RTM_IFINFO messages and initialize 13810Sstevel@tonic-gate * the routing socket message header and as much of the sockaddrs as possible. 13820Sstevel@tonic-gate */ 13830Sstevel@tonic-gate static int 13840Sstevel@tonic-gate setup_rtsock(void) 13850Sstevel@tonic-gate { 13860Sstevel@tonic-gate int s; 13870Sstevel@tonic-gate int ret; 13880Sstevel@tonic-gate char *cp; 13890Sstevel@tonic-gate struct sockaddr_in6 *sin6; 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate s = socket(PF_ROUTE, SOCK_RAW, AF_INET6); 13920Sstevel@tonic-gate if (s == -1) { 13930Sstevel@tonic-gate logperror("socket(PF_ROUTE)"); 13940Sstevel@tonic-gate exit(1); 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK); 13970Sstevel@tonic-gate if (ret < 0) { 13980Sstevel@tonic-gate logperror("fcntl(O_NDELAY)"); 13990Sstevel@tonic-gate exit(1); 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate if (poll_add(s) == -1) { 14020Sstevel@tonic-gate exit(1); 14030Sstevel@tonic-gate } 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate /* 14060Sstevel@tonic-gate * Allocate storage for the routing socket message. 14070Sstevel@tonic-gate */ 14080Sstevel@tonic-gate rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN); 14090Sstevel@tonic-gate if (rt_msg == NULL) { 14100Sstevel@tonic-gate logperror("malloc"); 14110Sstevel@tonic-gate exit(1); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate /* 14150Sstevel@tonic-gate * Initialize the routing socket message by zero-filling it and then 14160Sstevel@tonic-gate * setting the fields where are constant through the lifetime of the 14170Sstevel@tonic-gate * process. 14180Sstevel@tonic-gate */ 14190Sstevel@tonic-gate bzero(rt_msg, NDP_RTM_MSGLEN); 14200Sstevel@tonic-gate rt_msg->rtm_msglen = NDP_RTM_MSGLEN; 14210Sstevel@tonic-gate rt_msg->rtm_version = RTM_VERSION; 14220Sstevel@tonic-gate rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP; 14230Sstevel@tonic-gate rt_msg->rtm_pid = getpid(); 14240Sstevel@tonic-gate if (rt_msg->rtm_pid < 0) { 14250Sstevel@tonic-gate logperror("getpid"); 14260Sstevel@tonic-gate exit(1); 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate /* 14300Sstevel@tonic-gate * The RTA_DST sockaddr does not change during the lifetime of the 14310Sstevel@tonic-gate * process so it can be completely initialized at this time. 14320Sstevel@tonic-gate */ 14330Sstevel@tonic-gate cp = (char *)rt_msg + sizeof (struct rt_msghdr); 14340Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)cp; 14350Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 14360Sstevel@tonic-gate sin6->sin6_addr = in6addr_any; 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Initialize the constant portion of the RTA_GATEWAY sockaddr. 14400Sstevel@tonic-gate */ 14410Sstevel@tonic-gate cp += sizeof (struct sockaddr_in6); 14420Sstevel@tonic-gate rta_gateway = (struct sockaddr_in6 *)cp; 14430Sstevel@tonic-gate rta_gateway->sin6_family = AF_INET6; 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate /* 14460Sstevel@tonic-gate * The RTA_NETMASK sockaddr does not change during the lifetime of the 14470Sstevel@tonic-gate * process so it can be completely initialized at this time. 14480Sstevel@tonic-gate */ 14490Sstevel@tonic-gate cp += sizeof (struct sockaddr_in6); 14500Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)cp; 14510Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 14520Sstevel@tonic-gate sin6->sin6_addr = in6addr_any; 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate /* 14550Sstevel@tonic-gate * Initialize the constant portion of the RTA_IFP sockaddr. 14560Sstevel@tonic-gate */ 14570Sstevel@tonic-gate cp += sizeof (struct sockaddr_in6); 14580Sstevel@tonic-gate rta_ifp = (struct sockaddr_dl *)cp; 14590Sstevel@tonic-gate rta_ifp->sdl_family = AF_LINK; 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate return (s); 14620Sstevel@tonic-gate } 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate /* 14650Sstevel@tonic-gate * Retrieve one routing socket message. If RTM_IFINFO indicates 14660Sstevel@tonic-gate * new phyint do a full scan of the interfaces. If RTM_IFINFO 14670Sstevel@tonic-gate * indicates an existing phyint only scan that phyint and asociated 14680Sstevel@tonic-gate * prefixes. 14690Sstevel@tonic-gate */ 14700Sstevel@tonic-gate static void 14710Sstevel@tonic-gate process_rtsock(int rtsock) 14720Sstevel@tonic-gate { 14730Sstevel@tonic-gate int n; 14740Sstevel@tonic-gate #define MSG_SIZE 2048/8 14750Sstevel@tonic-gate int64_t msg[MSG_SIZE]; 14760Sstevel@tonic-gate struct rt_msghdr *rtm; 14770Sstevel@tonic-gate struct if_msghdr *ifm; 14780Sstevel@tonic-gate struct phyint *pi; 14790Sstevel@tonic-gate struct prefix *pr; 14800Sstevel@tonic-gate boolean_t need_initifs = _B_FALSE; 14810Sstevel@tonic-gate boolean_t need_ifscan = _B_FALSE; 14820Sstevel@tonic-gate int64_t ifscan_msg[10][MSG_SIZE]; 14830Sstevel@tonic-gate int ifscan_index = 0; 14840Sstevel@tonic-gate int i; 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate /* Empty the rtsock and coealesce all the work that we have */ 14870Sstevel@tonic-gate while (ifscan_index < 10) { 14880Sstevel@tonic-gate n = read(rtsock, msg, sizeof (msg)); 14890Sstevel@tonic-gate if (n <= 0) { 14900Sstevel@tonic-gate /* No more messages */ 14910Sstevel@tonic-gate break; 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate rtm = (struct rt_msghdr *)msg; 14940Sstevel@tonic-gate if (rtm->rtm_version != RTM_VERSION) { 14950Sstevel@tonic-gate logmsg(LOG_ERR, 14960Sstevel@tonic-gate "process_rtsock: version %d not understood\n", 14970Sstevel@tonic-gate rtm->rtm_version); 14980Sstevel@tonic-gate return; 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate switch (rtm->rtm_type) { 15010Sstevel@tonic-gate case RTM_NEWADDR: 15020Sstevel@tonic-gate case RTM_DELADDR: 15030Sstevel@tonic-gate /* 15040Sstevel@tonic-gate * Some logical interface has changed - have to scan 15050Sstevel@tonic-gate * everything to determine what actually changed. 15060Sstevel@tonic-gate */ 15070Sstevel@tonic-gate if (debug & D_IFSCAN) { 15080Sstevel@tonic-gate logmsg(LOG_DEBUG, "process_rtsock: " 15090Sstevel@tonic-gate "message %d\n", rtm->rtm_type); 15100Sstevel@tonic-gate } 15110Sstevel@tonic-gate need_initifs = _B_TRUE; 15120Sstevel@tonic-gate break; 15130Sstevel@tonic-gate case RTM_IFINFO: 15140Sstevel@tonic-gate need_ifscan = _B_TRUE; 15150Sstevel@tonic-gate (void) memcpy(ifscan_msg[ifscan_index], rtm, 15160Sstevel@tonic-gate sizeof (msg)); 15170Sstevel@tonic-gate ifscan_index++; 15180Sstevel@tonic-gate /* Handled below */ 15190Sstevel@tonic-gate break; 15200Sstevel@tonic-gate default: 15210Sstevel@tonic-gate /* Not interesting */ 15220Sstevel@tonic-gate break; 15230Sstevel@tonic-gate } 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate /* 15260Sstevel@tonic-gate * If we do full scan i.e initifs, we don't need to 15270Sstevel@tonic-gate * scan a particular interface as we should have 15280Sstevel@tonic-gate * done that as part of initifs. 15290Sstevel@tonic-gate */ 15300Sstevel@tonic-gate if (need_initifs) { 15310Sstevel@tonic-gate initifs(_B_FALSE); 15320Sstevel@tonic-gate return; 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate if (!need_ifscan) 15360Sstevel@tonic-gate return; 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate for (i = 0; i < ifscan_index; i++) { 15390Sstevel@tonic-gate ifm = (struct if_msghdr *)ifscan_msg[i]; 15400Sstevel@tonic-gate if (debug & D_IFSCAN) 15410Sstevel@tonic-gate logmsg(LOG_DEBUG, "process_rtsock: index %d\n", 15420Sstevel@tonic-gate ifm->ifm_index); 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate pi = phyint_lookup_on_index(ifm->ifm_index); 15450Sstevel@tonic-gate if (pi == NULL) { 15460Sstevel@tonic-gate /* 15470Sstevel@tonic-gate * A new physical interface. Do a full scan of the 15480Sstevel@tonic-gate * to catch any new logical interfaces. 15490Sstevel@tonic-gate */ 15500Sstevel@tonic-gate initifs(_B_FALSE); 15510Sstevel@tonic-gate return; 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate if (ifm->ifm_flags != pi->pi_flags) { 15550Sstevel@tonic-gate if (debug & D_IFSCAN) { 15560Sstevel@tonic-gate logmsg(LOG_DEBUG, "process_rtsock: clr for " 15570Sstevel@tonic-gate "%s old flags 0x%x new flags 0x%x\n", 15580Sstevel@tonic-gate pi->pi_name, pi->pi_flags, ifm->ifm_flags); 15590Sstevel@tonic-gate } 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate /* 15640Sstevel@tonic-gate * Mark the interfaces so that we can find phyints and prefixes 15650Sstevel@tonic-gate * which have disappeared from the kernel. 15660Sstevel@tonic-gate * if_process will set pr_in_use when it finds the 15670Sstevel@tonic-gate * interface in the kernel. 15680Sstevel@tonic-gate * Before re-examining the state of the interfaces, 15690Sstevel@tonic-gate * PI_PRESENT should be cleared from pi_kernel_state. 15700Sstevel@tonic-gate */ 15710Sstevel@tonic-gate pi->pi_kernel_state &= ~PI_PRESENT; 15720Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 15730Sstevel@tonic-gate pr->pr_in_use = _B_FALSE; 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate if (ifsock < 0) { 15770Sstevel@tonic-gate ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 15780Sstevel@tonic-gate if (ifsock < 0) { 15790Sstevel@tonic-gate logperror("process_rtsock: socket"); 15800Sstevel@tonic-gate return; 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate if_process(ifsock, pi->pi_name, _B_FALSE); 15840Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 15850Sstevel@tonic-gate if_process(ifsock, pr->pr_name, _B_FALSE); 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate /* 15880Sstevel@tonic-gate * If interface (still) exists in kernel, set 15890Sstevel@tonic-gate * pi_state to indicate that. 15900Sstevel@tonic-gate */ 15910Sstevel@tonic-gate if (pi->pi_kernel_state & PI_PRESENT) { 15920Sstevel@tonic-gate pi->pi_state |= PI_PRESENT; 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate check_if_removed(pi); 15950Sstevel@tonic-gate if (show_ifs) 15960Sstevel@tonic-gate phyint_print_all(); 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate /* 16010Sstevel@tonic-gate * Check whether the address formed by pr->pr_prefix and pi_token 16020Sstevel@tonic-gate * exists in the kernel. Cannot call SIOCTMYADDR/ONLINK as it 16030Sstevel@tonic-gate * does not check for down addresses. This function should not 16040Sstevel@tonic-gate * be called for onlink prefixes. 16050Sstevel@tonic-gate */ 16060Sstevel@tonic-gate static boolean_t 16070Sstevel@tonic-gate is_address_present(struct phyint *pi, struct prefix *pr, uint64_t flags) 16080Sstevel@tonic-gate { 16090Sstevel@tonic-gate int s; 16100Sstevel@tonic-gate in6_addr_t addr, *token; 16110Sstevel@tonic-gate int i; 16120Sstevel@tonic-gate int ret; 16130Sstevel@tonic-gate struct sockaddr_in6 sin6; 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate s = socket(AF_INET6, SOCK_DGRAM, 0); 16160Sstevel@tonic-gate if (s < 0) { 16170Sstevel@tonic-gate logperror("is_address_present: socket"); 16180Sstevel@tonic-gate /* 16190Sstevel@tonic-gate * By returning B_TRUE, we make the caller delete 16200Sstevel@tonic-gate * the prefix from the internal table. In the worst 16210Sstevel@tonic-gate * case the next RA will create the prefix. 16220Sstevel@tonic-gate */ 16230Sstevel@tonic-gate return (_B_TRUE); 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate if (flags & IFF_TEMPORARY) 16260Sstevel@tonic-gate token = &pi->pi_tmp_token; 16270Sstevel@tonic-gate else 16280Sstevel@tonic-gate token = &pi->pi_token; 16290Sstevel@tonic-gate for (i = 0; i < 16; i++) { 16300Sstevel@tonic-gate /* 16310Sstevel@tonic-gate * prefix_create ensures that pr_prefix has all-zero 16320Sstevel@tonic-gate * bits after prefixlen. 16330Sstevel@tonic-gate */ 16340Sstevel@tonic-gate addr.s6_addr[i] = pr->pr_prefix.s6_addr[i] | token->s6_addr[i]; 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate (void) memset(&sin6, 0, sizeof (struct sockaddr_in6)); 16370Sstevel@tonic-gate sin6.sin6_family = AF_INET6; 16380Sstevel@tonic-gate sin6.sin6_addr = addr; 16390Sstevel@tonic-gate ret = bind(s, (struct sockaddr *)&sin6, sizeof (struct sockaddr_in6)); 16400Sstevel@tonic-gate (void) close(s); 16410Sstevel@tonic-gate if (ret < 0 && errno == EADDRNOTAVAIL) 16420Sstevel@tonic-gate return (_B_FALSE); 16430Sstevel@tonic-gate else 16440Sstevel@tonic-gate return (_B_TRUE); 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate /* 16480Sstevel@tonic-gate * Look if the phyint or one of its prefixes have been removed from 16490Sstevel@tonic-gate * the kernel and take appropriate action. 16500Sstevel@tonic-gate * Uses {pi,pr}_in_use. 16510Sstevel@tonic-gate */ 16520Sstevel@tonic-gate static void 16530Sstevel@tonic-gate check_if_removed(struct phyint *pi) 16540Sstevel@tonic-gate { 16550Sstevel@tonic-gate struct prefix *pr; 16560Sstevel@tonic-gate struct prefix *next_pr; 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * Detect phyints that have been removed from the kernel. 16600Sstevel@tonic-gate * Since we can't recreate it here (would require ifconfig plumb 16610Sstevel@tonic-gate * logic) we just terminate use of that phyint. 16620Sstevel@tonic-gate */ 16630Sstevel@tonic-gate if (!(pi->pi_kernel_state & PI_PRESENT) && 16640Sstevel@tonic-gate (pi->pi_state & PI_PRESENT)) { 16650Sstevel@tonic-gate logmsg(LOG_ERR, "Interface %s has been removed from kernel. " 16660Sstevel@tonic-gate "in.ndpd will no longer use it\n", pi->pi_name); 16670Sstevel@tonic-gate /* 16680Sstevel@tonic-gate * Clear state so that should the phyint reappear 16690Sstevel@tonic-gate * we will start with initial advertisements or 16700Sstevel@tonic-gate * solicitations. 16710Sstevel@tonic-gate */ 16720Sstevel@tonic-gate phyint_cleanup(pi); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate /* 16750Sstevel@tonic-gate * Detect prefixes which are removed. 16760Sstevel@tonic-gate * 16770Sstevel@tonic-gate * We remove the prefix in all of the following cases : 16780Sstevel@tonic-gate * 16790Sstevel@tonic-gate * 1) Static prefixes are not the ones we create. So, 16800Sstevel@tonic-gate * just remove it from our tables. 16810Sstevel@tonic-gate * 16820Sstevel@tonic-gate * 2) On-link prefixes potentially move to a different 16830Sstevel@tonic-gate * phyint during failover. As it does not have 16840Sstevel@tonic-gate * an address, we can't use the logic in is_address_present 16850Sstevel@tonic-gate * to detect whether it is present in the kernel or not. 16860Sstevel@tonic-gate * Thus when it is manually removed we don't recreate it. 16870Sstevel@tonic-gate * 16880Sstevel@tonic-gate * 3) If there is a token mis-match and this prefix is not 16890Sstevel@tonic-gate * in the kernel, it means we don't need this prefix on 16900Sstevel@tonic-gate * this interface anymore. It must have been moved to a 16910Sstevel@tonic-gate * different interface by in.mpathd. This normally 16920Sstevel@tonic-gate * happens after a failover followed by a failback (or 16930Sstevel@tonic-gate * another failover) and we re-read the network 16940Sstevel@tonic-gate * configuration. For the failover from A to B, we would 16950Sstevel@tonic-gate * have created state on B about A's address, which will 16960Sstevel@tonic-gate * not be in use after the subsequent failback. So, we 16970Sstevel@tonic-gate * remove that prefix here. 16980Sstevel@tonic-gate * 16990Sstevel@tonic-gate * 4) If the physical interface is not present, then remove 17000Sstevel@tonic-gate * the prefix. In the cases where we are advertising 17010Sstevel@tonic-gate * prefixes, the state is kept in advertisement prefix and 17020Sstevel@tonic-gate * hence we can delete the prefix. 17030Sstevel@tonic-gate * 17040Sstevel@tonic-gate * 5) Similar to case (3), when we failover from A to B, the 17050Sstevel@tonic-gate * prefix in A will not be in use as it has been moved to B. 17060Sstevel@tonic-gate * We will delete it from our tables and recreate it when 17070Sstevel@tonic-gate * it fails back. is_address_present makes sure that the 17080Sstevel@tonic-gate * address is still valid in kernel. 17090Sstevel@tonic-gate * 17100Sstevel@tonic-gate * If none of the above is true, we recreate the prefix as it 17110Sstevel@tonic-gate * has been manually removed. We do it only when the interface 17120Sstevel@tonic-gate * is not FAILED or INACTIVE or OFFLINE. 17130Sstevel@tonic-gate */ 17140Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 17150Sstevel@tonic-gate next_pr = pr->pr_next; 17160Sstevel@tonic-gate if (!pr->pr_in_use) { 17170Sstevel@tonic-gate /* Clear PR_AUTO and PR_ONLINK */ 17180Sstevel@tonic-gate pr->pr_kernel_state &= PR_STATIC; 17190Sstevel@tonic-gate if ((pr->pr_state & PR_STATIC) || 17200Sstevel@tonic-gate !(pr->pr_state & PR_AUTO) || 17210Sstevel@tonic-gate !(prefix_token_match(pi, pr, pr->pr_flags)) || 17220Sstevel@tonic-gate (!(pi->pi_kernel_state & PI_PRESENT)) || 17230Sstevel@tonic-gate (is_address_present(pi, pr, pr->pr_flags))) { 17240Sstevel@tonic-gate prefix_delete(pr); 17250Sstevel@tonic-gate } else if (!(pi->pi_flags & 17260Sstevel@tonic-gate (IFF_FAILED|IFF_INACTIVE|IFF_OFFLINE)) && 17270Sstevel@tonic-gate pr->pr_state != pr->pr_kernel_state) { 17280Sstevel@tonic-gate pr->pr_name[0] = '\0'; 17290Sstevel@tonic-gate logmsg(LOG_INFO, "Prefix manually removed " 17300Sstevel@tonic-gate "on %s - recreating it!\n", 17310Sstevel@tonic-gate pi->pi_name); 17320Sstevel@tonic-gate prefix_update_k(pr); 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate } 17350Sstevel@tonic-gate } 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate 17390Sstevel@tonic-gate /* 17400Sstevel@tonic-gate * Queuing mechanism for router advertisements that are sent by in.ndpd 17410Sstevel@tonic-gate * and that also need to be processed by in.ndpd. 17420Sstevel@tonic-gate * Uses "signal number" 255 to indicate to the main poll loop 17430Sstevel@tonic-gate * that there is something to dequeue and send to incomining_ra(). 17440Sstevel@tonic-gate */ 17450Sstevel@tonic-gate struct raq { 17460Sstevel@tonic-gate struct raq *raq_next; 17470Sstevel@tonic-gate struct phyint *raq_pi; 17480Sstevel@tonic-gate int raq_packetlen; 17490Sstevel@tonic-gate uchar_t *raq_packet; 17500Sstevel@tonic-gate }; 17510Sstevel@tonic-gate static struct raq *raq_head = NULL; 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate /* 17540Sstevel@tonic-gate * Allocate a struct raq and memory for the packet. 17550Sstevel@tonic-gate * Send signal 255 to have poll dequeue. 17560Sstevel@tonic-gate */ 17570Sstevel@tonic-gate static void 17580Sstevel@tonic-gate loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len) 17590Sstevel@tonic-gate { 17600Sstevel@tonic-gate struct raq *raq; 17610Sstevel@tonic-gate struct raq **raqp; 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate if (no_loopback) 17640Sstevel@tonic-gate return; 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate if (debug & D_PKTOUT) 17670Sstevel@tonic-gate logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name); 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate raq = calloc(sizeof (struct raq), 1); 17700Sstevel@tonic-gate if (raq == NULL) { 17710Sstevel@tonic-gate logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 17720Sstevel@tonic-gate return; 17730Sstevel@tonic-gate } 17740Sstevel@tonic-gate raq->raq_packet = malloc(len); 17750Sstevel@tonic-gate if (raq->raq_packet == NULL) { 17760Sstevel@tonic-gate free(raq); 17770Sstevel@tonic-gate logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 17780Sstevel@tonic-gate return; 17790Sstevel@tonic-gate } 17800Sstevel@tonic-gate bcopy(ra, raq->raq_packet, len); 17810Sstevel@tonic-gate raq->raq_packetlen = len; 17820Sstevel@tonic-gate raq->raq_pi = pi; 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate /* Tail insert */ 17850Sstevel@tonic-gate raqp = &raq_head; 17860Sstevel@tonic-gate while (*raqp != NULL) 17870Sstevel@tonic-gate raqp = &((*raqp)->raq_next); 17880Sstevel@tonic-gate *raqp = raq; 17890Sstevel@tonic-gate 17900Sstevel@tonic-gate /* Signal for poll loop */ 17910Sstevel@tonic-gate sig_handler(255); 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate /* 17950Sstevel@tonic-gate * Dequeue and process all queued advertisements. 17960Sstevel@tonic-gate */ 17970Sstevel@tonic-gate static void 17980Sstevel@tonic-gate loopback_ra_dequeue(void) 17990Sstevel@tonic-gate { 18000Sstevel@tonic-gate struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT; 18010Sstevel@tonic-gate struct raq *raq; 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate if (debug & D_PKTIN) 18040Sstevel@tonic-gate logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n"); 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate while ((raq = raq_head) != NULL) { 18070Sstevel@tonic-gate raq_head = raq->raq_next; 18080Sstevel@tonic-gate raq->raq_next = NULL; 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate if (debug & D_PKTIN) { 18110Sstevel@tonic-gate logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n", 18120Sstevel@tonic-gate raq->raq_pi->pi_name); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate 18150Sstevel@tonic-gate incoming_ra(raq->raq_pi, 18160Sstevel@tonic-gate (struct nd_router_advert *)raq->raq_packet, 18170Sstevel@tonic-gate raq->raq_packetlen, &from, _B_TRUE); 18180Sstevel@tonic-gate free(raq->raq_packet); 18190Sstevel@tonic-gate free(raq); 18200Sstevel@tonic-gate } 18210Sstevel@tonic-gate } 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate 18240Sstevel@tonic-gate static void 18250Sstevel@tonic-gate usage(char *cmd) 18260Sstevel@tonic-gate { 18270Sstevel@tonic-gate (void) fprintf(stderr, 18280Sstevel@tonic-gate "usage: %s [ -adt ] [-f <config file>]\n", cmd); 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate 18310Sstevel@tonic-gate int 18320Sstevel@tonic-gate main(int argc, char *argv[]) 18330Sstevel@tonic-gate { 18340Sstevel@tonic-gate int i; 18350Sstevel@tonic-gate struct phyint *pi; 18360Sstevel@tonic-gate int c; 18370Sstevel@tonic-gate char *config_file = PATH_NDPD_CONF; 18380Sstevel@tonic-gate boolean_t file_required = _B_FALSE; 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate argv0 = argv; 18410Sstevel@tonic-gate srandom(gethostid()); 18420Sstevel@tonic-gate (void) umask(0022); 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) { 18450Sstevel@tonic-gate switch (c) { 18460Sstevel@tonic-gate case 'a': 18470Sstevel@tonic-gate /* 18480Sstevel@tonic-gate * The StatelessAddrConf variable in ndpd.conf, if 18490Sstevel@tonic-gate * present, will override this setting. 18500Sstevel@tonic-gate */ 18510Sstevel@tonic-gate ifdefaults[I_StatelessAddrConf].cf_value = 0; 18520Sstevel@tonic-gate break; 18530Sstevel@tonic-gate case 'd': 18540Sstevel@tonic-gate debug = D_ALL; 18550Sstevel@tonic-gate break; 18560Sstevel@tonic-gate case 'D': 18570Sstevel@tonic-gate i = strtol((char *)optarg, NULL, 0); 18580Sstevel@tonic-gate if (i == 0) { 18590Sstevel@tonic-gate (void) fprintf(stderr, "Bad debug flags: %s\n", 18600Sstevel@tonic-gate (char *)optarg); 18610Sstevel@tonic-gate exit(1); 18620Sstevel@tonic-gate } 18630Sstevel@tonic-gate debug |= i; 18640Sstevel@tonic-gate break; 18650Sstevel@tonic-gate case 'n': 18660Sstevel@tonic-gate no_loopback = 1; 18670Sstevel@tonic-gate break; 18680Sstevel@tonic-gate case 'I': 18690Sstevel@tonic-gate show_ifs = 1; 18700Sstevel@tonic-gate break; 18710Sstevel@tonic-gate case 't': 18720Sstevel@tonic-gate debug |= D_PKTIN | D_PKTOUT | D_PKTBAD; 18730Sstevel@tonic-gate break; 18740Sstevel@tonic-gate case 'f': 18750Sstevel@tonic-gate config_file = (char *)optarg; 18760Sstevel@tonic-gate file_required = _B_TRUE; 18770Sstevel@tonic-gate break; 18780Sstevel@tonic-gate case '?': 18790Sstevel@tonic-gate usage(argv[0]); 18800Sstevel@tonic-gate exit(1); 18810Sstevel@tonic-gate } 18820Sstevel@tonic-gate } 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate if (parse_config(config_file, file_required) == -1) 18850Sstevel@tonic-gate exit(2); 18860Sstevel@tonic-gate 18870Sstevel@tonic-gate if (show_ifs) 18880Sstevel@tonic-gate phyint_print_all(); 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate if (debug == 0) { 18910Sstevel@tonic-gate initlog(); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate setup_eventpipe(); 18950Sstevel@tonic-gate rtsock = setup_rtsock(); 18960Sstevel@tonic-gate timer_init(); 18970Sstevel@tonic-gate initifs(_B_TRUE); 18980Sstevel@tonic-gate 18990Sstevel@tonic-gate check_daemonize(); 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate for (;;) { 19020Sstevel@tonic-gate if (poll(pollfds, pollfd_num, -1) < 0) { 19030Sstevel@tonic-gate if (errno == EINTR) 19040Sstevel@tonic-gate continue; 19050Sstevel@tonic-gate logperror("main: poll"); 19060Sstevel@tonic-gate exit(1); 19070Sstevel@tonic-gate } 19080Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 19090Sstevel@tonic-gate if (!(pollfds[i].revents & POLLIN)) 19100Sstevel@tonic-gate continue; 19110Sstevel@tonic-gate if (pollfds[i].fd == eventpipe_read) { 19120Sstevel@tonic-gate in_signal(eventpipe_read); 19130Sstevel@tonic-gate break; 19140Sstevel@tonic-gate } 19150Sstevel@tonic-gate if (pollfds[i].fd == rtsock) { 19160Sstevel@tonic-gate process_rtsock(rtsock); 19170Sstevel@tonic-gate break; 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate /* 19200Sstevel@tonic-gate * Run timer routine to advance clock if more than 19210Sstevel@tonic-gate * half a second since the clock was advanced. 19220Sstevel@tonic-gate * This limits CPU usage under severe packet 19230Sstevel@tonic-gate * arrival rates but it creates a slight inaccuracy 19240Sstevel@tonic-gate * in the timer mechanism. 19250Sstevel@tonic-gate */ 19260Sstevel@tonic-gate conditional_run_timeouts(500U); 19270Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 19280Sstevel@tonic-gate if (pollfds[i].fd == pi->pi_sock) { 19290Sstevel@tonic-gate in_data(pi); 19300Sstevel@tonic-gate break; 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate } 19330Sstevel@tonic-gate } 19340Sstevel@tonic-gate } 19350Sstevel@tonic-gate /* NOTREACHED */ 19360Sstevel@tonic-gate return (0); 19370Sstevel@tonic-gate } 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate /* 19400Sstevel@tonic-gate * LOGGER 19410Sstevel@tonic-gate */ 19420Sstevel@tonic-gate 19430Sstevel@tonic-gate static boolean_t logging = _B_FALSE; 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate static void 19460Sstevel@tonic-gate initlog(void) 19470Sstevel@tonic-gate { 19480Sstevel@tonic-gate logging = _B_TRUE; 19490Sstevel@tonic-gate openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON); 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate /* Print the date/time without a trailing carridge return */ 19530Sstevel@tonic-gate static void 19540Sstevel@tonic-gate fprintdate(FILE *file) 19550Sstevel@tonic-gate { 19560Sstevel@tonic-gate char buf[BUFSIZ]; 19570Sstevel@tonic-gate struct tm tms; 19580Sstevel@tonic-gate time_t now; 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate now = time(NULL); 19610Sstevel@tonic-gate (void) localtime_r(&now, &tms); 19620Sstevel@tonic-gate (void) strftime(buf, sizeof (buf), "%h %d %X", &tms); 19630Sstevel@tonic-gate (void) fprintf(file, "%s ", buf); 19640Sstevel@tonic-gate } 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate /* PRINTFLIKE1 */ 19670Sstevel@tonic-gate void 1968*2428Smh138676 logmsg(int level, const char *fmt, ...) 19690Sstevel@tonic-gate { 19700Sstevel@tonic-gate va_list ap; 19710Sstevel@tonic-gate va_start(ap, fmt); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate if (logging) { 19740Sstevel@tonic-gate vsyslog(level, fmt, ap); 19750Sstevel@tonic-gate } else { 19760Sstevel@tonic-gate fprintdate(stderr); 19770Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 19780Sstevel@tonic-gate } 19790Sstevel@tonic-gate va_end(ap); 19800Sstevel@tonic-gate } 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate void 1983*2428Smh138676 logperror(const char *str) 19840Sstevel@tonic-gate { 19850Sstevel@tonic-gate if (logging) { 19860Sstevel@tonic-gate syslog(LOG_ERR, "%s: %m\n", str); 19870Sstevel@tonic-gate } else { 19880Sstevel@tonic-gate fprintdate(stderr); 19890Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", str, strerror(errno)); 19900Sstevel@tonic-gate } 19910Sstevel@tonic-gate } 19920Sstevel@tonic-gate 19930Sstevel@tonic-gate void 1994*2428Smh138676 logperror_pi(const struct phyint *pi, const char *str) 19950Sstevel@tonic-gate { 19960Sstevel@tonic-gate if (logging) { 19970Sstevel@tonic-gate syslog(LOG_ERR, "%s (interface %s): %m\n", 19980Sstevel@tonic-gate str, pi->pi_name); 19990Sstevel@tonic-gate } else { 20000Sstevel@tonic-gate fprintdate(stderr); 20010Sstevel@tonic-gate (void) fprintf(stderr, "%s (interface %s): %s\n", 20020Sstevel@tonic-gate str, pi->pi_name, strerror(errno)); 20030Sstevel@tonic-gate } 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate void 2007*2428Smh138676 logperror_pr(const struct prefix *pr, const char *str) 20080Sstevel@tonic-gate { 20090Sstevel@tonic-gate if (logging) { 20100Sstevel@tonic-gate syslog(LOG_ERR, "%s (prefix %s if %s): %m\n", 20110Sstevel@tonic-gate str, pr->pr_name, pr->pr_physical->pi_name); 20120Sstevel@tonic-gate } else { 20130Sstevel@tonic-gate fprintdate(stderr); 20140Sstevel@tonic-gate (void) fprintf(stderr, "%s (prefix %s if %s): %s\n", 20150Sstevel@tonic-gate str, pr->pr_name, pr->pr_physical->pi_name, 20160Sstevel@tonic-gate strerror(errno)); 20170Sstevel@tonic-gate } 20180Sstevel@tonic-gate } 2019