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 20*8485SPeter.Memishian@Sun.COM * 21*8485SPeter.Memishian@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 220Sstevel@tonic-gate * Use is subject to license terms. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate #include "defs.h" 260Sstevel@tonic-gate #include "tables.h" 270Sstevel@tonic-gate #include <fcntl.h> 283284Sapersson #include <sys/un.h> 290Sstevel@tonic-gate 300Sstevel@tonic-gate static void initlog(void); 310Sstevel@tonic-gate static void run_timeouts(void); 320Sstevel@tonic-gate 330Sstevel@tonic-gate static void advertise(struct sockaddr_in6 *sin6, struct phyint *pi, 340Sstevel@tonic-gate boolean_t no_prefixes); 350Sstevel@tonic-gate static void solicit(struct sockaddr_in6 *sin6, struct phyint *pi); 360Sstevel@tonic-gate static void initifs(boolean_t first); 370Sstevel@tonic-gate static void check_if_removed(struct phyint *pi); 380Sstevel@tonic-gate static void loopback_ra_enqueue(struct phyint *pi, 390Sstevel@tonic-gate struct nd_router_advert *ra, int len); 400Sstevel@tonic-gate static void loopback_ra_dequeue(void); 410Sstevel@tonic-gate static void check_daemonize(void); 420Sstevel@tonic-gate 430Sstevel@tonic-gate struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0, 440Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 450Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 460Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x1 } }; 470Sstevel@tonic-gate 480Sstevel@tonic-gate struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0, 490Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 500Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 510Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x2 } }; 520Sstevel@tonic-gate 530Sstevel@tonic-gate static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0, 540Sstevel@tonic-gate { 0xff, 0x2, 0x0, 0x0, 550Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 560Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 570Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x1 } }; 580Sstevel@tonic-gate 590Sstevel@tonic-gate static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0, 600Sstevel@tonic-gate { 0xff, 0x2, 0x0, 0x0, 610Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 620Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x0, 630Sstevel@tonic-gate 0x0, 0x0, 0x0, 0x2 } }; 640Sstevel@tonic-gate 650Sstevel@tonic-gate static char **argv0; /* Saved for re-exec on SIGHUP */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8]; 680Sstevel@tonic-gate 690Sstevel@tonic-gate static int show_ifs = 0; 700Sstevel@tonic-gate static boolean_t already_daemonized = _B_FALSE; 710Sstevel@tonic-gate int debug = 0; 720Sstevel@tonic-gate int no_loopback = 0; /* Do not send RA packets to ourselves */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Size of routing socket message used by in.ndpd which includes the header, 760Sstevel@tonic-gate * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6) 770Sstevel@tonic-gate * plus space for the RTA_IFP (a sockaddr_dl). 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate #define NDP_RTM_MSGLEN sizeof (struct rt_msghdr) + \ 800Sstevel@tonic-gate sizeof (struct sockaddr_in6) + \ 810Sstevel@tonic-gate sizeof (struct sockaddr_in6) + \ 820Sstevel@tonic-gate sizeof (struct sockaddr_in6) + \ 830Sstevel@tonic-gate sizeof (struct sockaddr_dl) 840Sstevel@tonic-gate 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * These are referenced externally in tables.c in order to fill in the 870Sstevel@tonic-gate * dynamic portions of the routing socket message and then to send the message 880Sstevel@tonic-gate * itself. 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate int rtsock = -1; /* Routing socket */ 910Sstevel@tonic-gate struct rt_msghdr *rt_msg; /* Routing socket message */ 920Sstevel@tonic-gate struct sockaddr_in6 *rta_gateway; /* RTA_GATEWAY sockaddr */ 930Sstevel@tonic-gate struct sockaddr_dl *rta_ifp; /* RTA_IFP sockaddr */ 943284Sapersson int mibsock = -1; /* mib request socket */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * Return the current time in milliseconds truncated to 980Sstevel@tonic-gate * fit in an integer. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate uint_t 1010Sstevel@tonic-gate getcurrenttime(void) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate struct timeval tp; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if (gettimeofday(&tp, NULL) < 0) { 1060Sstevel@tonic-gate logperror("getcurrenttime: gettimeofday failed"); 1070Sstevel@tonic-gate exit(1); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate return (tp.tv_sec * 1000 + tp.tv_usec / 1000); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Output a preformated packet from the packet[] buffer. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate static void 1160Sstevel@tonic-gate sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags) 1170Sstevel@tonic-gate { 1180Sstevel@tonic-gate int cc; 1190Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate cc = sendto(sock, (char *)packet, size, flags, 122*8485SPeter.Memishian@Sun.COM (struct sockaddr *)sin6, sizeof (*sin6)); 1230Sstevel@tonic-gate if (cc < 0 || cc != size) { 1240Sstevel@tonic-gate if (cc < 0) { 1250Sstevel@tonic-gate logperror("sendpacket: sendto"); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n", 1280Sstevel@tonic-gate inet_ntop(sin6->sin6_family, 1290Sstevel@tonic-gate (void *)&sin6->sin6_addr, 1300Sstevel@tonic-gate abuf, sizeof (abuf)), 1310Sstevel@tonic-gate size, cc); 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 135*8485SPeter.Memishian@Sun.COM /* 136*8485SPeter.Memishian@Sun.COM * If possible, place an ND_OPT_SOURCE_LINKADDR option at `optp'. 137*8485SPeter.Memishian@Sun.COM * Return the number of bytes placed in the option. 138*8485SPeter.Memishian@Sun.COM */ 139*8485SPeter.Memishian@Sun.COM static uint_t 140*8485SPeter.Memishian@Sun.COM add_opt_lla(struct phyint *pi, struct nd_opt_lla *optp) 141*8485SPeter.Memishian@Sun.COM { 142*8485SPeter.Memishian@Sun.COM uint_t optlen; 143*8485SPeter.Memishian@Sun.COM uint_t hwaddrlen; 144*8485SPeter.Memishian@Sun.COM struct lifreq lifr; 145*8485SPeter.Memishian@Sun.COM 146*8485SPeter.Memishian@Sun.COM /* If this phyint doesn't have a link-layer address, bail */ 147*8485SPeter.Memishian@Sun.COM if (phyint_get_lla(pi, &lifr) == -1) 148*8485SPeter.Memishian@Sun.COM return (0); 149*8485SPeter.Memishian@Sun.COM 150*8485SPeter.Memishian@Sun.COM hwaddrlen = lifr.lifr_nd.lnr_hdw_len; 151*8485SPeter.Memishian@Sun.COM /* roundup to multiple of 8 and make padding zero */ 152*8485SPeter.Memishian@Sun.COM optlen = ((sizeof (struct nd_opt_hdr) + hwaddrlen + 7) / 8) * 8; 153*8485SPeter.Memishian@Sun.COM bzero(optp, optlen); 154*8485SPeter.Memishian@Sun.COM optp->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR; 155*8485SPeter.Memishian@Sun.COM optp->nd_opt_lla_len = optlen / 8; 156*8485SPeter.Memishian@Sun.COM bcopy(lifr.lifr_nd.lnr_hdw_addr, optp->nd_opt_lla_hdw_addr, hwaddrlen); 157*8485SPeter.Memishian@Sun.COM 158*8485SPeter.Memishian@Sun.COM return (optlen); 159*8485SPeter.Memishian@Sun.COM } 160*8485SPeter.Memishian@Sun.COM 1610Sstevel@tonic-gate /* Send a Router Solicitation */ 1620Sstevel@tonic-gate static void 1630Sstevel@tonic-gate solicit(struct sockaddr_in6 *sin6, struct phyint *pi) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate int packetlen = 0; 1660Sstevel@tonic-gate struct nd_router_solicit *rs = (struct nd_router_solicit *)packet; 1670Sstevel@tonic-gate char *pptr = (char *)packet; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate rs->nd_rs_type = ND_ROUTER_SOLICIT; 1700Sstevel@tonic-gate rs->nd_rs_code = 0; 1710Sstevel@tonic-gate rs->nd_rs_cksum = htons(0); 1720Sstevel@tonic-gate rs->nd_rs_reserved = htonl(0); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate packetlen += sizeof (*rs); 1750Sstevel@tonic-gate pptr += sizeof (*rs); 1760Sstevel@tonic-gate 177*8485SPeter.Memishian@Sun.COM /* add options */ 178*8485SPeter.Memishian@Sun.COM packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (debug & D_PKTOUT) { 1810Sstevel@tonic-gate print_route_sol("Sending solicitation to ", pi, rs, packetlen, 1820Sstevel@tonic-gate sin6); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* 1880Sstevel@tonic-gate * Send a (set of) Router Advertisements and feed them back to ourselves 1890Sstevel@tonic-gate * for processing. Unless no_prefixes is set all prefixes are included. 1900Sstevel@tonic-gate * If there are too many prefix options to fit in one packet multiple 1910Sstevel@tonic-gate * packets will be sent - each containing a subset of the prefix options. 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate static void 1940Sstevel@tonic-gate advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes) 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate struct nd_opt_prefix_info *po; 1970Sstevel@tonic-gate char *pptr = (char *)packet; 1980Sstevel@tonic-gate struct nd_router_advert *ra; 1990Sstevel@tonic-gate struct adv_prefix *adv_pr; 2000Sstevel@tonic-gate int packetlen = 0; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate ra = (struct nd_router_advert *)pptr; 2030Sstevel@tonic-gate ra->nd_ra_type = ND_ROUTER_ADVERT; 2040Sstevel@tonic-gate ra->nd_ra_code = 0; 2050Sstevel@tonic-gate ra->nd_ra_cksum = htons(0); 2060Sstevel@tonic-gate ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit; 2070Sstevel@tonic-gate ra->nd_ra_flags_reserved = 0; 2080Sstevel@tonic-gate if (pi->pi_AdvManagedFlag) 2090Sstevel@tonic-gate ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED; 2100Sstevel@tonic-gate if (pi->pi_AdvOtherConfigFlag) 2110Sstevel@tonic-gate ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) 2140Sstevel@tonic-gate ra->nd_ra_router_lifetime = htons(0); 2150Sstevel@tonic-gate else 2160Sstevel@tonic-gate ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime); 2170Sstevel@tonic-gate ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime); 2180Sstevel@tonic-gate ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate packetlen = sizeof (*ra); 2210Sstevel@tonic-gate pptr += sizeof (*ra); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) { 2240Sstevel@tonic-gate if (debug & D_PKTOUT) { 2250Sstevel@tonic-gate print_route_adv("Sending advert (FINAL) to ", pi, 2260Sstevel@tonic-gate ra, packetlen, sin6); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 2290Sstevel@tonic-gate /* Feed packet back in for router operation */ 2300Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 2310Sstevel@tonic-gate return; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 234*8485SPeter.Memishian@Sun.COM /* add options */ 235*8485SPeter.Memishian@Sun.COM packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr); 236*8485SPeter.Memishian@Sun.COM pptr = (char *)packet + packetlen; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate if (pi->pi_AdvLinkMTU != 0) { 2390Sstevel@tonic-gate struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate mo->nd_opt_mtu_type = ND_OPT_MTU; 2420Sstevel@tonic-gate mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8; 2430Sstevel@tonic-gate mo->nd_opt_mtu_reserved = 0; 2440Sstevel@tonic-gate mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate packetlen += sizeof (struct nd_opt_mtu); 2470Sstevel@tonic-gate pptr += sizeof (struct nd_opt_mtu); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate if (no_prefixes) { 2510Sstevel@tonic-gate if (debug & D_PKTOUT) { 2520Sstevel@tonic-gate print_route_adv("Sending advert to ", pi, 2530Sstevel@tonic-gate ra, packetlen, sin6); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 2560Sstevel@tonic-gate /* Feed packet back in for router operation */ 2570Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 2580Sstevel@tonic-gate return; 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate po = (struct nd_opt_prefix_info *)pptr; 2620Sstevel@tonic-gate for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL; 2630Sstevel@tonic-gate adv_pr = adv_pr->adv_pr_next) { 2640Sstevel@tonic-gate if (!adv_pr->adv_pr_AdvOnLinkFlag && 2650Sstevel@tonic-gate !adv_pr->adv_pr_AdvAutonomousFlag) { 2660Sstevel@tonic-gate continue; 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * If the prefix doesn't fit in packet send 2710Sstevel@tonic-gate * what we have so far and start with new packet. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate if (packetlen + sizeof (*po) > 2740Sstevel@tonic-gate pi->pi_LinkMTU - sizeof (struct ip6_hdr)) { 2750Sstevel@tonic-gate if (debug & D_PKTOUT) { 2760Sstevel@tonic-gate print_route_adv("Sending advert " 2770Sstevel@tonic-gate "(FRAG) to ", 2780Sstevel@tonic-gate pi, ra, packetlen, sin6); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 2810Sstevel@tonic-gate /* Feed packet back in for router operation */ 2820Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 2830Sstevel@tonic-gate packetlen = sizeof (*ra); 2840Sstevel@tonic-gate pptr = (char *)packet + sizeof (*ra); 2850Sstevel@tonic-gate po = (struct nd_opt_prefix_info *)pptr; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 2880Sstevel@tonic-gate po->nd_opt_pi_len = sizeof (*po)/8; 2890Sstevel@tonic-gate po->nd_opt_pi_flags_reserved = 0; 2900Sstevel@tonic-gate if (adv_pr->adv_pr_AdvOnLinkFlag) { 2910Sstevel@tonic-gate po->nd_opt_pi_flags_reserved |= 2920Sstevel@tonic-gate ND_OPT_PI_FLAG_ONLINK; 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate if (adv_pr->adv_pr_AdvAutonomousFlag) { 2950Sstevel@tonic-gate po->nd_opt_pi_flags_reserved |= 2960Sstevel@tonic-gate ND_OPT_PI_FLAG_AUTO; 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len; 2990Sstevel@tonic-gate /* 3000Sstevel@tonic-gate * If both Adv*Expiration and Adv*Lifetime are 3010Sstevel@tonic-gate * set we prefer the former and make the lifetime 3020Sstevel@tonic-gate * decrement in real time. 3030Sstevel@tonic-gate */ 3040Sstevel@tonic-gate if (adv_pr->adv_pr_AdvValidRealTime) { 3050Sstevel@tonic-gate po->nd_opt_pi_valid_time = 3060Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvValidExpiration); 3070Sstevel@tonic-gate } else { 3080Sstevel@tonic-gate po->nd_opt_pi_valid_time = 3090Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvValidLifetime); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate if (adv_pr->adv_pr_AdvPreferredRealTime) { 3120Sstevel@tonic-gate po->nd_opt_pi_preferred_time = 3130Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvPreferredExpiration); 3140Sstevel@tonic-gate } else { 3150Sstevel@tonic-gate po->nd_opt_pi_preferred_time = 3160Sstevel@tonic-gate htonl(adv_pr->adv_pr_AdvPreferredLifetime); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate po->nd_opt_pi_reserved2 = htonl(0); 3190Sstevel@tonic-gate po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate po++; 3220Sstevel@tonic-gate packetlen += sizeof (*po); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate if (debug & D_PKTOUT) { 3250Sstevel@tonic-gate print_route_adv("Sending advert to ", pi, 3260Sstevel@tonic-gate ra, packetlen, sin6); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate sendpacket(sin6, pi->pi_sock, packetlen, 0); 3290Sstevel@tonic-gate /* Feed packet back in for router operation */ 3300Sstevel@tonic-gate loopback_ra_enqueue(pi, ra, packetlen); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate /* Poll support */ 3340Sstevel@tonic-gate static int pollfd_num = 0; /* Allocated and initialized */ 3350Sstevel@tonic-gate static struct pollfd *pollfds = NULL; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Add fd to the set being polled. Returns 0 if ok; -1 if failed. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate int 3410Sstevel@tonic-gate poll_add(int fd) 3420Sstevel@tonic-gate { 3430Sstevel@tonic-gate int i; 3440Sstevel@tonic-gate int new_num; 3450Sstevel@tonic-gate struct pollfd *newfds; 3463284Sapersson 3470Sstevel@tonic-gate /* Check if already present */ 3480Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 3490Sstevel@tonic-gate if (pollfds[i].fd == fd) 3500Sstevel@tonic-gate return (0); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate /* Check for empty spot already present */ 3530Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 3540Sstevel@tonic-gate if (pollfds[i].fd == -1) { 3550Sstevel@tonic-gate pollfds[i].fd = fd; 3560Sstevel@tonic-gate return (0); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate /* Allocate space for 32 more fds and initialize to -1 */ 3610Sstevel@tonic-gate new_num = pollfd_num + 32; 3620Sstevel@tonic-gate newfds = realloc(pollfds, new_num * sizeof (struct pollfd)); 3630Sstevel@tonic-gate if (newfds == NULL) { 3640Sstevel@tonic-gate logperror("poll_add: realloc"); 3650Sstevel@tonic-gate return (-1); 3660Sstevel@tonic-gate } 3673284Sapersson 3683284Sapersson newfds[pollfd_num].fd = fd; 3693284Sapersson newfds[pollfd_num++].events = POLLIN; 3703284Sapersson 3710Sstevel@tonic-gate for (i = pollfd_num; i < new_num; i++) { 3720Sstevel@tonic-gate newfds[i].fd = -1; 3730Sstevel@tonic-gate newfds[i].events = POLLIN; 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate pollfd_num = new_num; 3760Sstevel@tonic-gate pollfds = newfds; 3773284Sapersson return (0); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * Remove fd from the set being polled. Returns 0 if ok; -1 if failed. 3820Sstevel@tonic-gate */ 3830Sstevel@tonic-gate int 3840Sstevel@tonic-gate poll_remove(int fd) 3850Sstevel@tonic-gate { 3860Sstevel@tonic-gate int i; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* Check if already present */ 3890Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 3900Sstevel@tonic-gate if (pollfds[i].fd == fd) { 3910Sstevel@tonic-gate pollfds[i].fd = -1; 3920Sstevel@tonic-gate return (0); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate return (-1); 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * Extract information about the ifname (either a physical interface and 4000Sstevel@tonic-gate * the ":0" logical interface or just a logical interface). 4010Sstevel@tonic-gate * If the interface (still) exists in kernel set pr_in_use 4020Sstevel@tonic-gate * for caller to be able to detect interfaces that are removed. 4030Sstevel@tonic-gate * Starts sending advertisements/solicitations when new physical interfaces 4040Sstevel@tonic-gate * are detected. 4050Sstevel@tonic-gate */ 4060Sstevel@tonic-gate static void 4070Sstevel@tonic-gate if_process(int s, char *ifname, boolean_t first) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate struct lifreq lifr; 4100Sstevel@tonic-gate struct phyint *pi; 4110Sstevel@tonic-gate struct prefix *pr; 4120Sstevel@tonic-gate char *cp; 4130Sstevel@tonic-gate char phyintname[LIFNAMSIZ + 1]; 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if (debug & D_IFSCAN) 4160Sstevel@tonic-gate logmsg(LOG_DEBUG, "if_process(%s)\n", ifname); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 4190Sstevel@tonic-gate lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0'; 4200Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) { 4210Sstevel@tonic-gate if (errno == ENXIO) { 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * Interface has disappeared 4240Sstevel@tonic-gate */ 4250Sstevel@tonic-gate return; 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate logperror("if_process: ioctl (get interface flags)"); 4280Sstevel@tonic-gate return; 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * Ignore loopback and point-to-multipoint interfaces. 4330Sstevel@tonic-gate * Point-to-point interfaces always have IFF_MULTICAST set. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate if (!(lifr.lifr_flags & IFF_MULTICAST) || 4360Sstevel@tonic-gate (lifr.lifr_flags & IFF_LOOPBACK)) { 4370Sstevel@tonic-gate return; 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate if (!(lifr.lifr_flags & IFF_IPV6)) 4410Sstevel@tonic-gate return; 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate (void) strncpy(phyintname, ifname, sizeof (phyintname)); 4440Sstevel@tonic-gate phyintname[sizeof (phyintname) - 1] = '\0'; 4450Sstevel@tonic-gate if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) { 4460Sstevel@tonic-gate *cp = '\0'; 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate pi = phyint_lookup(phyintname); 4500Sstevel@tonic-gate if (pi == NULL) { 4510Sstevel@tonic-gate /* 4520Sstevel@tonic-gate * Do not add anything for new interfaces until they are UP. 4530Sstevel@tonic-gate * For existing interfaces we track the up flag. 4540Sstevel@tonic-gate */ 4550Sstevel@tonic-gate if (!(lifr.lifr_flags & IFF_UP)) 4560Sstevel@tonic-gate return; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate pi = phyint_create(phyintname); 4590Sstevel@tonic-gate if (pi == NULL) { 4600Sstevel@tonic-gate logmsg(LOG_ERR, "if_process: out of memory\n"); 4610Sstevel@tonic-gate return; 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate (void) phyint_init_from_k(pi); 4650Sstevel@tonic-gate if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) { 4660Sstevel@tonic-gate /* Interface is not yet present */ 4670Sstevel@tonic-gate if (debug & D_PHYINT) { 4680Sstevel@tonic-gate logmsg(LOG_DEBUG, "if_process: interface not yet " 4690Sstevel@tonic-gate "present %s\n", pi->pi_name); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate return; 4720Sstevel@tonic-gate } 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate if (pi->pi_sock != -1) { 4750Sstevel@tonic-gate if (poll_add(pi->pi_sock) == -1) { 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * reset state. 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate phyint_cleanup(pi); 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate /* 4840Sstevel@tonic-gate * Check if IFF_ROUTER has been turned off in kernel in which 4850Sstevel@tonic-gate * case we have to turn off AdvSendAdvertisements. 4860Sstevel@tonic-gate * The kernel will automatically turn off IFF_ROUTER if 4870Sstevel@tonic-gate * ip6_forwarding is turned off. 4880Sstevel@tonic-gate * Note that we do not switch back should IFF_ROUTER be turned on. 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate if (!first && 4910Sstevel@tonic-gate pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) { 4920Sstevel@tonic-gate logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name); 4930Sstevel@tonic-gate check_to_advertise(pi, START_FINAL_ADV); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate pi->pi_AdvSendAdvertisements = 0; 4960Sstevel@tonic-gate pi->pi_sol_state = NO_SOLICIT; 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* 5000Sstevel@tonic-gate * Send advertisments and solicitation only if the interface is 5010Sstevel@tonic-gate * present in the kernel. 5020Sstevel@tonic-gate */ 5030Sstevel@tonic-gate if (pi->pi_kernel_state & PI_PRESENT) { 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) { 5060Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 5070Sstevel@tonic-gate check_to_advertise(pi, START_INIT_ADV); 5080Sstevel@tonic-gate } else { 5090Sstevel@tonic-gate if (pi->pi_sol_state == NO_SOLICIT) 5100Sstevel@tonic-gate check_to_solicit(pi, START_INIT_SOLICIT); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Track static kernel prefixes to prevent in.ndpd from clobbering 5160Sstevel@tonic-gate * them by creating a struct prefix for each prefix detected in the 5170Sstevel@tonic-gate * kernel. 5180Sstevel@tonic-gate */ 5190Sstevel@tonic-gate pr = prefix_lookup_name(pi, ifname); 5200Sstevel@tonic-gate if (pr == NULL) { 5210Sstevel@tonic-gate pr = prefix_create_name(pi, ifname); 5220Sstevel@tonic-gate if (pr == NULL) { 5230Sstevel@tonic-gate logmsg(LOG_ERR, "if_process: out of memory\n"); 5240Sstevel@tonic-gate return; 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate if (prefix_init_from_k(pr) == -1) { 5270Sstevel@tonic-gate prefix_delete(pr); 5280Sstevel@tonic-gate return; 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate /* Detect prefixes which are removed */ 5320Sstevel@tonic-gate if (pr->pr_kernel_state != 0) 5330Sstevel@tonic-gate pr->pr_in_use = _B_TRUE; 5342546Scarlsonj 5352546Scarlsonj if ((lifr.lifr_flags & IFF_DUPLICATE) && 5363431Scarlsonj !(lifr.lifr_flags & IFF_DHCPRUNNING) && 5372546Scarlsonj (pr->pr_flags & IFF_TEMPORARY)) { 5382546Scarlsonj in6_addr_t *token; 5392546Scarlsonj int i; 5402546Scarlsonj char abuf[INET6_ADDRSTRLEN]; 5412546Scarlsonj 5422546Scarlsonj if (++pr->pr_attempts >= MAX_DAD_FAILURES) { 5432546Scarlsonj logmsg(LOG_ERR, "%s: token %s is duplicate after %d " 5442546Scarlsonj "attempts; disabling temporary addresses on %s", 5452546Scarlsonj pr->pr_name, inet_ntop(AF_INET6, 5462546Scarlsonj (void *)&pi->pi_tmp_token, abuf, sizeof (abuf)), 5472546Scarlsonj pr->pr_attempts, pi->pi_name); 5482546Scarlsonj pi->pi_TmpAddrsEnabled = 0; 5492546Scarlsonj tmptoken_delete(pi); 5502546Scarlsonj prefix_delete(pr); 5512546Scarlsonj return; 5522546Scarlsonj } 5532546Scarlsonj logmsg(LOG_WARNING, "%s: token %s is duplicate; trying again", 5542546Scarlsonj pr->pr_name, inet_ntop(AF_INET6, (void *)&pi->pi_tmp_token, 5552546Scarlsonj abuf, sizeof (abuf))); 5562546Scarlsonj if (!tmptoken_create(pi)) { 5572546Scarlsonj prefix_delete(pr); 5582546Scarlsonj return; 5592546Scarlsonj } 5602546Scarlsonj token = &pi->pi_tmp_token; 5612546Scarlsonj for (i = 0; i < 16; i++) { 5622546Scarlsonj /* 5632546Scarlsonj * prefix_create ensures that pr_prefix has all-zero 5642546Scarlsonj * bits after prefixlen. 5652546Scarlsonj */ 5662546Scarlsonj pr->pr_address.s6_addr[i] = pr->pr_prefix.s6_addr[i] | 5672546Scarlsonj token->s6_addr[i]; 5682546Scarlsonj } 5692546Scarlsonj if (prefix_lookup_addr_match(pr) != NULL) { 5702546Scarlsonj prefix_delete(pr); 5712546Scarlsonj return; 5722546Scarlsonj } 5732546Scarlsonj pr->pr_CreateTime = getcurrenttime() / MILLISEC; 5742546Scarlsonj /* 5752546Scarlsonj * We've got a new token. Clearing PR_AUTO causes 5762546Scarlsonj * prefix_update_k to bring the interface up and set the 5772546Scarlsonj * address. 5782546Scarlsonj */ 5792546Scarlsonj pr->pr_kernel_state &= ~PR_AUTO; 5802546Scarlsonj prefix_update_k(pr); 5812546Scarlsonj } 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate static int ifsock = -1; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* 5870Sstevel@tonic-gate * Scan all interfaces to detect changes as well as new and deleted intefaces 5880Sstevel@tonic-gate * 'first' is set for the initial call only. Do not effect anything. 5890Sstevel@tonic-gate */ 5900Sstevel@tonic-gate static void 5910Sstevel@tonic-gate initifs(boolean_t first) 5920Sstevel@tonic-gate { 5930Sstevel@tonic-gate char *buf; 5940Sstevel@tonic-gate int bufsize; 5950Sstevel@tonic-gate int numifs; 5960Sstevel@tonic-gate int n; 5970Sstevel@tonic-gate struct lifnum lifn; 5980Sstevel@tonic-gate struct lifconf lifc; 5990Sstevel@tonic-gate struct lifreq *lifr; 6000Sstevel@tonic-gate struct phyint *pi; 6010Sstevel@tonic-gate struct phyint *next_pi; 6020Sstevel@tonic-gate struct prefix *pr; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if (debug & D_IFSCAN) 6050Sstevel@tonic-gate logmsg(LOG_DEBUG, "Reading interface configuration\n"); 6060Sstevel@tonic-gate if (ifsock < 0) { 6070Sstevel@tonic-gate ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 6080Sstevel@tonic-gate if (ifsock < 0) { 6090Sstevel@tonic-gate logperror("initifs: socket"); 6100Sstevel@tonic-gate return; 6110Sstevel@tonic-gate } 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate lifn.lifn_family = AF_INET6; 6140Sstevel@tonic-gate lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY; 6150Sstevel@tonic-gate if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) { 6160Sstevel@tonic-gate logperror("initifs: ioctl (get interface numbers)"); 6170Sstevel@tonic-gate return; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate numifs = lifn.lifn_count; 6200Sstevel@tonic-gate bufsize = numifs * sizeof (struct lifreq); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate buf = (char *)malloc(bufsize); 6230Sstevel@tonic-gate if (buf == NULL) { 6240Sstevel@tonic-gate logmsg(LOG_ERR, "initifs: out of memory\n"); 6250Sstevel@tonic-gate return; 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate /* 6290Sstevel@tonic-gate * Mark the interfaces so that we can find phyints and prefixes 6300Sstevel@tonic-gate * which have disappeared from the kernel. 6310Sstevel@tonic-gate * if_process will set pr_in_use when it finds the interface 6320Sstevel@tonic-gate * in the kernel. 6330Sstevel@tonic-gate */ 6340Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 6350Sstevel@tonic-gate /* 6360Sstevel@tonic-gate * Before re-examining the state of the interfaces, 6370Sstevel@tonic-gate * PI_PRESENT should be cleared from pi_kernel_state. 6380Sstevel@tonic-gate */ 6390Sstevel@tonic-gate pi->pi_kernel_state &= ~PI_PRESENT; 6400Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 6410Sstevel@tonic-gate pr->pr_in_use = _B_FALSE; 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate lifc.lifc_family = AF_INET6; 6460Sstevel@tonic-gate lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY; 6470Sstevel@tonic-gate lifc.lifc_len = bufsize; 6480Sstevel@tonic-gate lifc.lifc_buf = buf; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) { 6510Sstevel@tonic-gate logperror("initifs: ioctl (get interface configuration)"); 6520Sstevel@tonic-gate free(buf); 6530Sstevel@tonic-gate return; 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate lifr = (struct lifreq *)lifc.lifc_req; 6570Sstevel@tonic-gate for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++) 6580Sstevel@tonic-gate if_process(ifsock, lifr->lifr_name, first); 6590Sstevel@tonic-gate free(buf); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* 6620Sstevel@tonic-gate * Detect phyints that have been removed from the kernel. 6630Sstevel@tonic-gate * Since we can't recreate it here (would require ifconfig plumb 6640Sstevel@tonic-gate * logic) we just terminate use of that phyint. 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 6670Sstevel@tonic-gate next_pi = pi->pi_next; 6680Sstevel@tonic-gate /* 6690Sstevel@tonic-gate * If interface (still) exists in kernel, set 6700Sstevel@tonic-gate * pi_state to indicate that. 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate if (pi->pi_kernel_state & PI_PRESENT) { 6730Sstevel@tonic-gate pi->pi_state |= PI_PRESENT; 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate check_if_removed(pi); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate if (show_ifs) 6790Sstevel@tonic-gate phyint_print_all(); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * Router advertisement state machine. Used for everything but timer 6850Sstevel@tonic-gate * events which use advertise_event directly. 6860Sstevel@tonic-gate */ 6870Sstevel@tonic-gate void 6880Sstevel@tonic-gate check_to_advertise(struct phyint *pi, enum adv_events event) 6890Sstevel@tonic-gate { 6900Sstevel@tonic-gate uint_t delay; 6910Sstevel@tonic-gate enum adv_states old_state = pi->pi_adv_state; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate if (debug & D_STATE) { 6940Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n", 6950Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state); 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate delay = advertise_event(pi, event, 0); 6980Sstevel@tonic-gate if (delay != TIMER_INFINITY) { 6990Sstevel@tonic-gate /* Make sure the global next event is updated */ 7000Sstevel@tonic-gate timer_schedule(delay); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate if (debug & D_STATE) { 7040Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n", 7050Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state, 7060Sstevel@tonic-gate (int)pi->pi_adv_state); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate /* 7110Sstevel@tonic-gate * Router advertisement state machine. 7120Sstevel@tonic-gate * Return the number of milliseconds until next timeout (TIMER_INFINITY 7130Sstevel@tonic-gate * if never). 7140Sstevel@tonic-gate * For the ADV_TIMER event the caller passes in the number of milliseconds 7150Sstevel@tonic-gate * since the last timer event in the 'elapsed' parameter. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate uint_t 7180Sstevel@tonic-gate advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed) 7190Sstevel@tonic-gate { 7200Sstevel@tonic-gate uint_t delay; 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate if (debug & D_STATE) { 7230Sstevel@tonic-gate logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n", 7240Sstevel@tonic-gate pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate check_daemonize(); 7270Sstevel@tonic-gate if (!pi->pi_AdvSendAdvertisements) 7280Sstevel@tonic-gate return (TIMER_INFINITY); 7290Sstevel@tonic-gate if (pi->pi_flags & IFF_NORTEXCH) { 7300Sstevel@tonic-gate if (debug & D_PKTOUT) { 7310Sstevel@tonic-gate logmsg(LOG_DEBUG, "Suppress sending RA packet on %s " 7320Sstevel@tonic-gate "(no route exchange on interface)\n", 7330Sstevel@tonic-gate pi->pi_name); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate return (TIMER_INFINITY); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate switch (event) { 7390Sstevel@tonic-gate case ADV_OFF: 7400Sstevel@tonic-gate pi->pi_adv_state = NO_ADV; 7410Sstevel@tonic-gate return (TIMER_INFINITY); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate case START_INIT_ADV: 7440Sstevel@tonic-gate if (pi->pi_adv_state == INIT_ADV) 7450Sstevel@tonic-gate return (pi->pi_adv_time_left); 7460Sstevel@tonic-gate pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS; 7470Sstevel@tonic-gate pi->pi_adv_time_left = 0; 7480Sstevel@tonic-gate pi->pi_adv_state = INIT_ADV; 7490Sstevel@tonic-gate break; /* send advertisement */ 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate case START_FINAL_ADV: 7520Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 7530Sstevel@tonic-gate return (TIMER_INFINITY); 7540Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) 7550Sstevel@tonic-gate return (pi->pi_adv_time_left); 7560Sstevel@tonic-gate pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS; 7570Sstevel@tonic-gate pi->pi_adv_time_left = 0; 7580Sstevel@tonic-gate pi->pi_adv_state = FINAL_ADV; 7590Sstevel@tonic-gate break; /* send advertisement */ 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate case RECEIVED_SOLICIT: 7620Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 7630Sstevel@tonic-gate return (TIMER_INFINITY); 7640Sstevel@tonic-gate if (pi->pi_adv_state == SOLICIT_ADV) { 7650Sstevel@tonic-gate if (pi->pi_adv_time_left != 0) 7660Sstevel@tonic-gate return (pi->pi_adv_time_left); 7670Sstevel@tonic-gate break; 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME); 7700Sstevel@tonic-gate if (delay < pi->pi_adv_time_left) 7710Sstevel@tonic-gate pi->pi_adv_time_left = delay; 7720Sstevel@tonic-gate if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) { 7730Sstevel@tonic-gate /* 7740Sstevel@tonic-gate * Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS 7750Sstevel@tonic-gate * plus random delay) after the previous 7760Sstevel@tonic-gate * advertisement was sent. 7770Sstevel@tonic-gate */ 7780Sstevel@tonic-gate pi->pi_adv_time_left = delay + 7790Sstevel@tonic-gate ND_MIN_DELAY_BETWEEN_RAS - 7800Sstevel@tonic-gate pi->pi_adv_time_since_sent; 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate pi->pi_adv_state = SOLICIT_ADV; 7830Sstevel@tonic-gate break; 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate case ADV_TIMER: 7860Sstevel@tonic-gate if (pi->pi_adv_state == NO_ADV) 7870Sstevel@tonic-gate return (TIMER_INFINITY); 7880Sstevel@tonic-gate /* Decrease time left */ 7890Sstevel@tonic-gate if (pi->pi_adv_time_left >= elapsed) 7900Sstevel@tonic-gate pi->pi_adv_time_left -= elapsed; 7910Sstevel@tonic-gate else 7920Sstevel@tonic-gate pi->pi_adv_time_left = 0; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* Increase time since last advertisement was sent */ 7950Sstevel@tonic-gate pi->pi_adv_time_since_sent += elapsed; 7960Sstevel@tonic-gate break; 7970Sstevel@tonic-gate default: 7980Sstevel@tonic-gate logmsg(LOG_ERR, "advertise_event: Unknown event %d\n", 7990Sstevel@tonic-gate (int)event); 8000Sstevel@tonic-gate return (TIMER_INFINITY); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate if (pi->pi_adv_time_left != 0) 8040Sstevel@tonic-gate return (pi->pi_adv_time_left); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate /* Send advertisement and calculate next time to send */ 8070Sstevel@tonic-gate if (pi->pi_adv_state == FINAL_ADV) { 8080Sstevel@tonic-gate /* Omit the prefixes */ 8090Sstevel@tonic-gate advertise(&v6allnodes, pi, _B_TRUE); 8100Sstevel@tonic-gate } else { 8110Sstevel@tonic-gate advertise(&v6allnodes, pi, _B_FALSE); 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate pi->pi_adv_time_since_sent = 0; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate switch (pi->pi_adv_state) { 8160Sstevel@tonic-gate case SOLICIT_ADV: 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * The solicited advertisement has been sent. 8190Sstevel@tonic-gate * Revert to periodic advertisements. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate pi->pi_adv_state = REG_ADV; 8220Sstevel@tonic-gate /* FALLTHRU */ 8230Sstevel@tonic-gate case REG_ADV: 8240Sstevel@tonic-gate pi->pi_adv_time_left = 8250Sstevel@tonic-gate GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 8260Sstevel@tonic-gate 1000 * pi->pi_MaxRtrAdvInterval); 8270Sstevel@tonic-gate break; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate case INIT_ADV: 8300Sstevel@tonic-gate if (--pi->pi_adv_count > 0) { 8310Sstevel@tonic-gate delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 8320Sstevel@tonic-gate 1000 * pi->pi_MaxRtrAdvInterval); 8330Sstevel@tonic-gate if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL) 8340Sstevel@tonic-gate delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL; 8350Sstevel@tonic-gate pi->pi_adv_time_left = delay; 8360Sstevel@tonic-gate } else { 8370Sstevel@tonic-gate pi->pi_adv_time_left = 8380Sstevel@tonic-gate GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval, 8390Sstevel@tonic-gate 1000 * pi->pi_MaxRtrAdvInterval); 8400Sstevel@tonic-gate pi->pi_adv_state = REG_ADV; 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate break; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate case FINAL_ADV: 8450Sstevel@tonic-gate if (--pi->pi_adv_count > 0) { 8460Sstevel@tonic-gate pi->pi_adv_time_left = 8470Sstevel@tonic-gate ND_MAX_INITIAL_RTR_ADVERT_INTERVAL; 8480Sstevel@tonic-gate } else { 8490Sstevel@tonic-gate pi->pi_adv_state = NO_ADV; 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate break; 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate if (pi->pi_adv_state != NO_ADV) 8540Sstevel@tonic-gate return (pi->pi_adv_time_left); 8550Sstevel@tonic-gate else 8560Sstevel@tonic-gate return (TIMER_INFINITY); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * Router solicitation state machine. Used for everything but timer 8610Sstevel@tonic-gate * events which use solicit_event directly. 8620Sstevel@tonic-gate */ 8630Sstevel@tonic-gate void 8640Sstevel@tonic-gate check_to_solicit(struct phyint *pi, enum solicit_events event) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate uint_t delay; 8670Sstevel@tonic-gate enum solicit_states old_state = pi->pi_sol_state; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate if (debug & D_STATE) { 8700Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n", 8710Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate delay = solicit_event(pi, event, 0); 8740Sstevel@tonic-gate if (delay != TIMER_INFINITY) { 8750Sstevel@tonic-gate /* Make sure the global next event is updated */ 8760Sstevel@tonic-gate timer_schedule(delay); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate if (debug & D_STATE) { 8800Sstevel@tonic-gate logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n", 8810Sstevel@tonic-gate pi->pi_name, (int)event, (int)old_state, 8820Sstevel@tonic-gate (int)pi->pi_sol_state); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate static void 8870Sstevel@tonic-gate daemonize_ndpd(void) 8880Sstevel@tonic-gate { 8890Sstevel@tonic-gate FILE *pidfp; 8900Sstevel@tonic-gate mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */ 8910Sstevel@tonic-gate struct itimerval it; 8920Sstevel@tonic-gate boolean_t timerval = _B_TRUE; 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * Need to get current timer settings so they can be restored 8960Sstevel@tonic-gate * after the fork(), as the it_value and it_interval values for 8970Sstevel@tonic-gate * the ITIMER_REAL timer are reset to 0 in the child process. 8980Sstevel@tonic-gate */ 8990Sstevel@tonic-gate if (getitimer(ITIMER_REAL, &it) < 0) { 9000Sstevel@tonic-gate if (debug & D_TIMER) 9010Sstevel@tonic-gate logmsg(LOG_DEBUG, 9020Sstevel@tonic-gate "daemonize_ndpd: failed to get itimerval\n"); 9030Sstevel@tonic-gate timerval = _B_FALSE; 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* Daemonize. */ 9070Sstevel@tonic-gate switch (fork()) { 9080Sstevel@tonic-gate case 0: 9090Sstevel@tonic-gate /* Child */ 9100Sstevel@tonic-gate break; 9110Sstevel@tonic-gate case -1: 9120Sstevel@tonic-gate logperror("fork"); 9130Sstevel@tonic-gate exit(1); 9140Sstevel@tonic-gate default: 9150Sstevel@tonic-gate /* Parent */ 9160Sstevel@tonic-gate _exit(0); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate /* Store our process id, blow away any existing file if it exists. */ 9200Sstevel@tonic-gate if ((pidfp = fopen(PATH_PID, "w")) == NULL) { 9210Sstevel@tonic-gate (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n", 9220Sstevel@tonic-gate argv0[0], strerror(errno)); 9230Sstevel@tonic-gate } else { 9240Sstevel@tonic-gate (void) fprintf(pidfp, "%ld\n", getpid()); 9250Sstevel@tonic-gate (void) fclose(pidfp); 9260Sstevel@tonic-gate (void) chmod(PATH_PID, pidmode); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate (void) close(0); 9300Sstevel@tonic-gate (void) close(1); 9310Sstevel@tonic-gate (void) close(2); 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate (void) chdir("/"); 9340Sstevel@tonic-gate (void) open("/dev/null", O_RDWR); 9350Sstevel@tonic-gate (void) dup2(0, 1); 9360Sstevel@tonic-gate (void) dup2(0, 2); 9370Sstevel@tonic-gate (void) setsid(); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate already_daemonized = _B_TRUE; 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * Restore timer values, if we were able to save them; if not, 9430Sstevel@tonic-gate * check and set the right value by calling run_timeouts(). 9440Sstevel@tonic-gate */ 9450Sstevel@tonic-gate if (timerval) { 9460Sstevel@tonic-gate if (setitimer(ITIMER_REAL, &it, NULL) < 0) { 9470Sstevel@tonic-gate logperror("daemonize_ndpd: setitimer"); 9480Sstevel@tonic-gate exit(2); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate } else { 9510Sstevel@tonic-gate run_timeouts(); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * Check to see if the time is right to daemonize. The right time is when: 9570Sstevel@tonic-gate * 9580Sstevel@tonic-gate * 1. We haven't already daemonized. 9590Sstevel@tonic-gate * 2. We are not in debug mode. 9600Sstevel@tonic-gate * 3. All interfaces are marked IFF_NOXMIT. 9610Sstevel@tonic-gate * 4. All non-router interfaces have their prefixes set up and we're 9620Sstevel@tonic-gate * done sending router solicitations on those interfaces without 9630Sstevel@tonic-gate * prefixes. 9640Sstevel@tonic-gate */ 9650Sstevel@tonic-gate static void 9660Sstevel@tonic-gate check_daemonize(void) 9670Sstevel@tonic-gate { 9680Sstevel@tonic-gate struct phyint *pi; 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate if (already_daemonized || debug != 0) 9710Sstevel@tonic-gate return; 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 9740Sstevel@tonic-gate if (!(pi->pi_flags & IFF_NOXMIT)) 9750Sstevel@tonic-gate break; 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate /* 9790Sstevel@tonic-gate * If we can't transmit on any of the interfaces there is no reason 9800Sstevel@tonic-gate * to hold up progress. 9810Sstevel@tonic-gate */ 9820Sstevel@tonic-gate if (pi == NULL) { 9830Sstevel@tonic-gate daemonize_ndpd(); 9840Sstevel@tonic-gate return; 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* Check all interfaces. If any are still soliciting, just return. */ 9880Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 9890Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements || 9900Sstevel@tonic-gate !(pi->pi_kernel_state & PI_PRESENT)) 9910Sstevel@tonic-gate continue; 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate if (pi->pi_sol_state == INIT_SOLICIT) 9940Sstevel@tonic-gate return; 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate daemonize_ndpd(); 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* 10010Sstevel@tonic-gate * Router solicitation state machine. 10020Sstevel@tonic-gate * Return the number of milliseconds until next timeout (TIMER_INFINITY 10030Sstevel@tonic-gate * if never). 10040Sstevel@tonic-gate * For the SOL_TIMER event the caller passes in the number of milliseconds 10050Sstevel@tonic-gate * since the last timer event in the 'elapsed' parameter. 10060Sstevel@tonic-gate */ 10070Sstevel@tonic-gate uint_t 10080Sstevel@tonic-gate solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed) 10090Sstevel@tonic-gate { 10100Sstevel@tonic-gate if (debug & D_STATE) { 10110Sstevel@tonic-gate logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n", 10120Sstevel@tonic-gate pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) 10160Sstevel@tonic-gate return (TIMER_INFINITY); 10170Sstevel@tonic-gate if (pi->pi_flags & IFF_NORTEXCH) { 10180Sstevel@tonic-gate if (debug & D_PKTOUT) { 10190Sstevel@tonic-gate logmsg(LOG_DEBUG, "Suppress sending RS packet on %s " 10200Sstevel@tonic-gate "(no route exchange on interface)\n", 10210Sstevel@tonic-gate pi->pi_name); 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate return (TIMER_INFINITY); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate switch (event) { 10270Sstevel@tonic-gate case SOLICIT_OFF: 10280Sstevel@tonic-gate pi->pi_sol_state = NO_SOLICIT; 10290Sstevel@tonic-gate check_daemonize(); 10300Sstevel@tonic-gate return (TIMER_INFINITY); 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate case SOLICIT_DONE: 10330Sstevel@tonic-gate pi->pi_sol_state = DONE_SOLICIT; 10340Sstevel@tonic-gate check_daemonize(); 10350Sstevel@tonic-gate return (TIMER_INFINITY); 10360Sstevel@tonic-gate 10373431Scarlsonj case RESTART_INIT_SOLICIT: 10383431Scarlsonj /* 10393431Scarlsonj * This event allows us to start solicitation over again 10403431Scarlsonj * without losing the RA flags. We start solicitation over 10413431Scarlsonj * when we are missing an interface prefix for a newly- 10423431Scarlsonj * encountered DHCP interface. 10433431Scarlsonj */ 10443431Scarlsonj if (pi->pi_sol_state == INIT_SOLICIT) 10453431Scarlsonj return (pi->pi_sol_time_left); 10463431Scarlsonj pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS; 10473431Scarlsonj pi->pi_sol_time_left = 10483431Scarlsonj GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY); 10493431Scarlsonj pi->pi_sol_state = INIT_SOLICIT; 10503431Scarlsonj break; 10513431Scarlsonj 10520Sstevel@tonic-gate case START_INIT_SOLICIT: 10530Sstevel@tonic-gate if (pi->pi_sol_state == INIT_SOLICIT) 10540Sstevel@tonic-gate return (pi->pi_sol_time_left); 10553431Scarlsonj pi->pi_ra_flags = 0; 10560Sstevel@tonic-gate pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS; 10570Sstevel@tonic-gate pi->pi_sol_time_left = 10580Sstevel@tonic-gate GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY); 10590Sstevel@tonic-gate pi->pi_sol_state = INIT_SOLICIT; 10600Sstevel@tonic-gate break; 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate case SOL_TIMER: 10630Sstevel@tonic-gate if (pi->pi_sol_state == NO_SOLICIT) 10640Sstevel@tonic-gate return (TIMER_INFINITY); 10650Sstevel@tonic-gate /* Decrease time left */ 10660Sstevel@tonic-gate if (pi->pi_sol_time_left >= elapsed) 10670Sstevel@tonic-gate pi->pi_sol_time_left -= elapsed; 10680Sstevel@tonic-gate else 10690Sstevel@tonic-gate pi->pi_sol_time_left = 0; 10700Sstevel@tonic-gate break; 10710Sstevel@tonic-gate default: 10720Sstevel@tonic-gate logmsg(LOG_ERR, "solicit_event: Unknown event %d\n", 10730Sstevel@tonic-gate (int)event); 10740Sstevel@tonic-gate return (TIMER_INFINITY); 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate if (pi->pi_sol_time_left != 0) 10780Sstevel@tonic-gate return (pi->pi_sol_time_left); 10790Sstevel@tonic-gate 10800Sstevel@tonic-gate /* Send solicitation and calculate next time */ 10810Sstevel@tonic-gate switch (pi->pi_sol_state) { 10820Sstevel@tonic-gate case INIT_SOLICIT: 10830Sstevel@tonic-gate solicit(&v6allrouters, pi); 10840Sstevel@tonic-gate if (--pi->pi_sol_count == 0) { 10854106Scarlsonj if (debug & D_STATE) { 10864106Scarlsonj logmsg(LOG_DEBUG, "solicit_event: no routers " 10874106Scarlsonj "found on %s; assuming default flags\n", 10884106Scarlsonj pi->pi_name); 10894106Scarlsonj } 10903431Scarlsonj if (pi->pi_StatefulAddrConf) { 10913431Scarlsonj pi->pi_ra_flags |= ND_RA_FLAG_MANAGED | 10923431Scarlsonj ND_RA_FLAG_OTHER; 10933431Scarlsonj start_dhcp(pi); 10943431Scarlsonj } 10950Sstevel@tonic-gate pi->pi_sol_state = DONE_SOLICIT; 10960Sstevel@tonic-gate check_daemonize(); 10970Sstevel@tonic-gate return (TIMER_INFINITY); 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL; 11000Sstevel@tonic-gate return (pi->pi_sol_time_left); 11010Sstevel@tonic-gate case NO_SOLICIT: 11020Sstevel@tonic-gate case DONE_SOLICIT: 11030Sstevel@tonic-gate return (TIMER_INFINITY); 11040Sstevel@tonic-gate default: 11050Sstevel@tonic-gate return (pi->pi_sol_time_left); 11060Sstevel@tonic-gate } 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate /* 11100Sstevel@tonic-gate * Timer mechanism using relative time (in milliseconds) from the 11110Sstevel@tonic-gate * previous timer event. Timers exceeding TIMER_INFINITY milliseconds 11120Sstevel@tonic-gate * will fire after TIMER_INFINITY milliseconds. 11130Sstevel@tonic-gate */ 11140Sstevel@tonic-gate static uint_t timer_previous; /* When last SIGALRM occurred */ 11150Sstevel@tonic-gate static uint_t timer_next; /* Currently scheduled timeout */ 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate static void 11180Sstevel@tonic-gate timer_init(void) 11190Sstevel@tonic-gate { 11200Sstevel@tonic-gate timer_previous = getcurrenttime(); 11210Sstevel@tonic-gate timer_next = TIMER_INFINITY; 11220Sstevel@tonic-gate run_timeouts(); 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate /* 11260Sstevel@tonic-gate * Make sure the next SIGALRM occurs delay milliseconds from the current 11270Sstevel@tonic-gate * time if not earlier. 11280Sstevel@tonic-gate * Handles getcurrenttime (32 bit integer holding milliseconds) wraparound 11290Sstevel@tonic-gate * by treating differences greater than 0x80000000 as negative. 11300Sstevel@tonic-gate */ 11310Sstevel@tonic-gate void 11320Sstevel@tonic-gate timer_schedule(uint_t delay) 11330Sstevel@tonic-gate { 11340Sstevel@tonic-gate uint_t now; 11350Sstevel@tonic-gate struct itimerval itimerval; 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate now = getcurrenttime(); 11380Sstevel@tonic-gate if (debug & D_TIMER) { 11390Sstevel@tonic-gate logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n", 11400Sstevel@tonic-gate delay, now, timer_next); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate /* Will this timer occur before the currently scheduled SIGALRM? */ 11430Sstevel@tonic-gate if (delay >= timer_next - now) { 11440Sstevel@tonic-gate if (debug & D_TIMER) { 11450Sstevel@tonic-gate logmsg(LOG_DEBUG, "timer_schedule(%u): no action - " 11460Sstevel@tonic-gate "next in %u ms\n", 11470Sstevel@tonic-gate delay, timer_next - now); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate return; 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate if (delay == 0) { 11520Sstevel@tonic-gate /* Minimum allowed delay */ 11530Sstevel@tonic-gate delay = 1; 11540Sstevel@tonic-gate } 11550Sstevel@tonic-gate timer_next = now + delay; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate itimerval.it_value.tv_sec = delay / 1000; 11580Sstevel@tonic-gate itimerval.it_value.tv_usec = (delay % 1000) * 1000; 11590Sstevel@tonic-gate itimerval.it_interval.tv_sec = 0; 11600Sstevel@tonic-gate itimerval.it_interval.tv_usec = 0; 11610Sstevel@tonic-gate if (debug & D_TIMER) { 11620Sstevel@tonic-gate logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n", 11630Sstevel@tonic-gate delay, 11640Sstevel@tonic-gate itimerval.it_value.tv_sec, itimerval.it_value.tv_usec); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) { 11670Sstevel@tonic-gate logperror("timer_schedule: setitimer"); 11680Sstevel@tonic-gate exit(2); 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * Conditional running of timer. If more than 'minimal_time' millseconds 11740Sstevel@tonic-gate * since the timer routines were last run we run them. 11750Sstevel@tonic-gate * Used when packets arrive. 11760Sstevel@tonic-gate */ 11770Sstevel@tonic-gate static void 11780Sstevel@tonic-gate conditional_run_timeouts(uint_t minimal_time) 11790Sstevel@tonic-gate { 11800Sstevel@tonic-gate uint_t now; 11810Sstevel@tonic-gate uint_t elapsed; 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate now = getcurrenttime(); 11840Sstevel@tonic-gate elapsed = now - timer_previous; 11850Sstevel@tonic-gate if (elapsed > minimal_time) { 11860Sstevel@tonic-gate if (debug & D_TIMER) { 11870Sstevel@tonic-gate logmsg(LOG_DEBUG, "conditional_run_timeouts: " 11880Sstevel@tonic-gate "elapsed %d\n", elapsed); 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate run_timeouts(); 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate } 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate /* 11950Sstevel@tonic-gate * Timer has fired. 11960Sstevel@tonic-gate * Determine when the next timer event will occur by asking all 11970Sstevel@tonic-gate * the timer routines. 11980Sstevel@tonic-gate * Should not be called from a timer routine but in some cases this is 11990Sstevel@tonic-gate * done because the code doesn't know that e.g. it was called from 12000Sstevel@tonic-gate * ifconfig_timer(). In this case the nested run_timeouts will just return but 12010Sstevel@tonic-gate * the running run_timeouts will ensure to call all the timer functions by 12020Sstevel@tonic-gate * looping once more. 12030Sstevel@tonic-gate */ 12040Sstevel@tonic-gate static void 12050Sstevel@tonic-gate run_timeouts(void) 12060Sstevel@tonic-gate { 12070Sstevel@tonic-gate uint_t now; 12080Sstevel@tonic-gate uint_t elapsed; 12090Sstevel@tonic-gate uint_t next; 12100Sstevel@tonic-gate uint_t nexti; 12110Sstevel@tonic-gate struct phyint *pi; 12120Sstevel@tonic-gate struct phyint *next_pi; 12130Sstevel@tonic-gate struct prefix *pr; 12140Sstevel@tonic-gate struct prefix *next_pr; 12150Sstevel@tonic-gate struct adv_prefix *adv_pr; 12160Sstevel@tonic-gate struct adv_prefix *next_adv_pr; 12170Sstevel@tonic-gate struct router *dr; 12180Sstevel@tonic-gate struct router *next_dr; 12190Sstevel@tonic-gate static boolean_t timeout_running; 12200Sstevel@tonic-gate static boolean_t do_retry; 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate if (timeout_running) { 12230Sstevel@tonic-gate if (debug & D_TIMER) 12240Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: nested call\n"); 12250Sstevel@tonic-gate do_retry = _B_TRUE; 12260Sstevel@tonic-gate return; 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate timeout_running = _B_TRUE; 12290Sstevel@tonic-gate retry: 12300Sstevel@tonic-gate /* How much time since the last time we were called? */ 12310Sstevel@tonic-gate now = getcurrenttime(); 12320Sstevel@tonic-gate elapsed = now - timer_previous; 12330Sstevel@tonic-gate timer_previous = now; 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate if (debug & D_TIMER) 12360Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate next = TIMER_INFINITY; 12390Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 12400Sstevel@tonic-gate next_pi = pi->pi_next; 12410Sstevel@tonic-gate nexti = phyint_timer(pi, elapsed); 12420Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12430Sstevel@tonic-gate next = nexti; 12440Sstevel@tonic-gate if (debug & D_TIMER) { 12450Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n", 12460Sstevel@tonic-gate pi->pi_name, nexti, next); 12470Sstevel@tonic-gate } 12480Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 12490Sstevel@tonic-gate next_pr = pr->pr_next; 12500Sstevel@tonic-gate nexti = prefix_timer(pr, elapsed); 12510Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12520Sstevel@tonic-gate next = nexti; 12530Sstevel@tonic-gate if (debug & D_TIMER) { 12540Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (pr %s): " 12550Sstevel@tonic-gate "%d -> %u ms\n", pr->pr_name, nexti, next); 12560Sstevel@tonic-gate } 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL; 12590Sstevel@tonic-gate adv_pr = next_adv_pr) { 12600Sstevel@tonic-gate next_adv_pr = adv_pr->adv_pr_next; 12610Sstevel@tonic-gate nexti = adv_prefix_timer(adv_pr, elapsed); 12620Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12630Sstevel@tonic-gate next = nexti; 12640Sstevel@tonic-gate if (debug & D_TIMER) { 12650Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts " 12660Sstevel@tonic-gate "(adv pr on %s): %d -> %u ms\n", 12670Sstevel@tonic-gate adv_pr->adv_pr_physical->pi_name, 12680Sstevel@tonic-gate nexti, next); 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) { 12720Sstevel@tonic-gate next_dr = dr->dr_next; 12730Sstevel@tonic-gate nexti = router_timer(dr, elapsed); 12740Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12750Sstevel@tonic-gate next = nexti; 12760Sstevel@tonic-gate if (debug & D_TIMER) { 12770Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (dr): " 12780Sstevel@tonic-gate "%d -> %u ms\n", nexti, next); 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate } 12810Sstevel@tonic-gate if (pi->pi_TmpAddrsEnabled) { 12820Sstevel@tonic-gate nexti = tmptoken_timer(pi, elapsed); 12830Sstevel@tonic-gate if (nexti != TIMER_INFINITY && nexti < next) 12840Sstevel@tonic-gate next = nexti; 12850Sstevel@tonic-gate if (debug & D_TIMER) { 12860Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): " 12870Sstevel@tonic-gate "%d -> %u ms\n", pi->pi_name, nexti, next); 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * Make sure the timer functions are run at least once 12930Sstevel@tonic-gate * an hour. 12940Sstevel@tonic-gate */ 12950Sstevel@tonic-gate if (next == TIMER_INFINITY) 12960Sstevel@tonic-gate next = 3600 * 1000; /* 1 hour */ 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate if (debug & D_TIMER) 12990Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next); 13000Sstevel@tonic-gate timer_schedule(next); 13010Sstevel@tonic-gate if (do_retry) { 13020Sstevel@tonic-gate if (debug & D_TIMER) 13030Sstevel@tonic-gate logmsg(LOG_DEBUG, "run_timeouts: retry\n"); 13040Sstevel@tonic-gate do_retry = _B_FALSE; 13050Sstevel@tonic-gate goto retry; 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate timeout_running = _B_FALSE; 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate static int eventpipe_read = -1; /* Used for synchronous signal delivery */ 13110Sstevel@tonic-gate static int eventpipe_write = -1; 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate /* 13140Sstevel@tonic-gate * Ensure that signals are processed synchronously with the rest of 13150Sstevel@tonic-gate * the code by just writing a one character signal number on the pipe. 13160Sstevel@tonic-gate * The poll loop will pick this up and process the signal event. 13170Sstevel@tonic-gate */ 13180Sstevel@tonic-gate static void 13190Sstevel@tonic-gate sig_handler(int signo) 13200Sstevel@tonic-gate { 13210Sstevel@tonic-gate uchar_t buf = (uchar_t)signo; 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate if (eventpipe_write == -1) { 13240Sstevel@tonic-gate logmsg(LOG_ERR, "sig_handler: no pipe\n"); 13250Sstevel@tonic-gate return; 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate if (write(eventpipe_write, &buf, sizeof (buf)) < 0) 13280Sstevel@tonic-gate logperror("sig_handler: write"); 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate /* 13320Sstevel@tonic-gate * Pick up a signal "byte" from the pipe and process it. 13330Sstevel@tonic-gate */ 13340Sstevel@tonic-gate static void 13350Sstevel@tonic-gate in_signal(int fd) 13360Sstevel@tonic-gate { 13370Sstevel@tonic-gate uchar_t buf; 13380Sstevel@tonic-gate struct phyint *pi; 13390Sstevel@tonic-gate struct phyint *next_pi; 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate switch (read(fd, &buf, sizeof (buf))) { 13420Sstevel@tonic-gate case -1: 13430Sstevel@tonic-gate logperror("in_signal: read"); 13440Sstevel@tonic-gate exit(1); 13450Sstevel@tonic-gate /* NOTREACHED */ 13460Sstevel@tonic-gate case 1: 13470Sstevel@tonic-gate break; 13480Sstevel@tonic-gate case 0: 13490Sstevel@tonic-gate logmsg(LOG_ERR, "in_signal: read eof\n"); 13500Sstevel@tonic-gate exit(1); 13510Sstevel@tonic-gate /* NOTREACHED */ 13520Sstevel@tonic-gate default: 13530Sstevel@tonic-gate logmsg(LOG_ERR, "in_signal: read > 1\n"); 13540Sstevel@tonic-gate exit(1); 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate if (debug & D_TIMER) 13580Sstevel@tonic-gate logmsg(LOG_DEBUG, "in_signal() got %d\n", buf); 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate switch (buf) { 13610Sstevel@tonic-gate case SIGALRM: 13620Sstevel@tonic-gate if (debug & D_TIMER) { 13630Sstevel@tonic-gate uint_t now = getcurrenttime(); 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n", 13660Sstevel@tonic-gate now - timer_next); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate timer_next = TIMER_INFINITY; 13690Sstevel@tonic-gate run_timeouts(); 13700Sstevel@tonic-gate break; 13710Sstevel@tonic-gate case SIGHUP: 13720Sstevel@tonic-gate /* Re-read config file by exec'ing ourselves */ 13730Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 13740Sstevel@tonic-gate next_pi = pi->pi_next; 13750Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) 13760Sstevel@tonic-gate check_to_advertise(pi, START_FINAL_ADV); 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate phyint_delete(pi); 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate /* 13820Sstevel@tonic-gate * Prevent fd leaks. Everything gets re-opened at start-up 13830Sstevel@tonic-gate * time. 0, 1, and 2 are closed and re-opened as 13840Sstevel@tonic-gate * /dev/null, so we'll leave those open. 13850Sstevel@tonic-gate */ 13860Sstevel@tonic-gate closefrom(3); 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n"); 13890Sstevel@tonic-gate (void) execv(argv0[0], argv0); 13900Sstevel@tonic-gate (void) unlink(PATH_PID); 13910Sstevel@tonic-gate _exit(0177); 13920Sstevel@tonic-gate /* NOTREACHED */ 13930Sstevel@tonic-gate case SIGUSR1: 13940Sstevel@tonic-gate logmsg(LOG_DEBUG, "Printing configuration:\n"); 13950Sstevel@tonic-gate phyint_print_all(); 13960Sstevel@tonic-gate break; 13970Sstevel@tonic-gate case SIGINT: 13980Sstevel@tonic-gate case SIGTERM: 13990Sstevel@tonic-gate case SIGQUIT: 14000Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = next_pi) { 14010Sstevel@tonic-gate next_pi = pi->pi_next; 14020Sstevel@tonic-gate if (pi->pi_AdvSendAdvertisements) 14030Sstevel@tonic-gate check_to_advertise(pi, START_FINAL_ADV); 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate phyint_delete(pi); 14060Sstevel@tonic-gate } 14073284Sapersson (void) unlink(NDPD_SNMP_SOCKET); 14080Sstevel@tonic-gate (void) unlink(PATH_PID); 14090Sstevel@tonic-gate exit(0); 14100Sstevel@tonic-gate /* NOTREACHED */ 14110Sstevel@tonic-gate case 255: 14120Sstevel@tonic-gate /* 14130Sstevel@tonic-gate * Special "signal" from looback_ra_enqueue. 14140Sstevel@tonic-gate * Handle any queued loopback router advertisements. 14150Sstevel@tonic-gate */ 14160Sstevel@tonic-gate loopback_ra_dequeue(); 14170Sstevel@tonic-gate break; 14180Sstevel@tonic-gate default: 14190Sstevel@tonic-gate logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf); 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate /* 14240Sstevel@tonic-gate * Create pipe for signal delivery and set up signal handlers. 14250Sstevel@tonic-gate */ 14260Sstevel@tonic-gate static void 14270Sstevel@tonic-gate setup_eventpipe(void) 14280Sstevel@tonic-gate { 14290Sstevel@tonic-gate int fds[2]; 14300Sstevel@tonic-gate struct sigaction act; 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate if ((pipe(fds)) < 0) { 14330Sstevel@tonic-gate logperror("setup_eventpipe: pipe"); 14340Sstevel@tonic-gate exit(1); 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate eventpipe_read = fds[0]; 14370Sstevel@tonic-gate eventpipe_write = fds[1]; 14380Sstevel@tonic-gate if (poll_add(eventpipe_read) == -1) { 14390Sstevel@tonic-gate exit(1); 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate act.sa_handler = sig_handler; 14420Sstevel@tonic-gate act.sa_flags = SA_RESTART; 14430Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate (void) sigset(SIGHUP, sig_handler); 14460Sstevel@tonic-gate (void) sigset(SIGUSR1, sig_handler); 14470Sstevel@tonic-gate (void) sigset(SIGTERM, sig_handler); 14480Sstevel@tonic-gate (void) sigset(SIGINT, sig_handler); 14490Sstevel@tonic-gate (void) sigset(SIGQUIT, sig_handler); 14500Sstevel@tonic-gate } 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate /* 14530Sstevel@tonic-gate * Create a routing socket for receiving RTM_IFINFO messages and initialize 14540Sstevel@tonic-gate * the routing socket message header and as much of the sockaddrs as possible. 14550Sstevel@tonic-gate */ 14560Sstevel@tonic-gate static int 14570Sstevel@tonic-gate setup_rtsock(void) 14580Sstevel@tonic-gate { 14590Sstevel@tonic-gate int s; 14600Sstevel@tonic-gate int ret; 14610Sstevel@tonic-gate char *cp; 14620Sstevel@tonic-gate struct sockaddr_in6 *sin6; 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate s = socket(PF_ROUTE, SOCK_RAW, AF_INET6); 14650Sstevel@tonic-gate if (s == -1) { 14660Sstevel@tonic-gate logperror("socket(PF_ROUTE)"); 14670Sstevel@tonic-gate exit(1); 14680Sstevel@tonic-gate } 14690Sstevel@tonic-gate ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK); 14700Sstevel@tonic-gate if (ret < 0) { 14710Sstevel@tonic-gate logperror("fcntl(O_NDELAY)"); 14720Sstevel@tonic-gate exit(1); 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate if (poll_add(s) == -1) { 14750Sstevel@tonic-gate exit(1); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * Allocate storage for the routing socket message. 14800Sstevel@tonic-gate */ 14810Sstevel@tonic-gate rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN); 14820Sstevel@tonic-gate if (rt_msg == NULL) { 14830Sstevel@tonic-gate logperror("malloc"); 14840Sstevel@tonic-gate exit(1); 14850Sstevel@tonic-gate } 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate /* 14880Sstevel@tonic-gate * Initialize the routing socket message by zero-filling it and then 14890Sstevel@tonic-gate * setting the fields where are constant through the lifetime of the 14900Sstevel@tonic-gate * process. 14910Sstevel@tonic-gate */ 14920Sstevel@tonic-gate bzero(rt_msg, NDP_RTM_MSGLEN); 14930Sstevel@tonic-gate rt_msg->rtm_msglen = NDP_RTM_MSGLEN; 14940Sstevel@tonic-gate rt_msg->rtm_version = RTM_VERSION; 14950Sstevel@tonic-gate rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP; 14960Sstevel@tonic-gate rt_msg->rtm_pid = getpid(); 14970Sstevel@tonic-gate if (rt_msg->rtm_pid < 0) { 14980Sstevel@tonic-gate logperror("getpid"); 14990Sstevel@tonic-gate exit(1); 15000Sstevel@tonic-gate } 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate /* 15030Sstevel@tonic-gate * The RTA_DST sockaddr does not change during the lifetime of the 15040Sstevel@tonic-gate * process so it can be completely initialized at this time. 15050Sstevel@tonic-gate */ 15060Sstevel@tonic-gate cp = (char *)rt_msg + sizeof (struct rt_msghdr); 15070Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)cp; 15080Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 15090Sstevel@tonic-gate sin6->sin6_addr = in6addr_any; 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate /* 15120Sstevel@tonic-gate * Initialize the constant portion of the RTA_GATEWAY sockaddr. 15130Sstevel@tonic-gate */ 15140Sstevel@tonic-gate cp += sizeof (struct sockaddr_in6); 15150Sstevel@tonic-gate rta_gateway = (struct sockaddr_in6 *)cp; 15160Sstevel@tonic-gate rta_gateway->sin6_family = AF_INET6; 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate /* 15190Sstevel@tonic-gate * The RTA_NETMASK sockaddr does not change during the lifetime of the 15200Sstevel@tonic-gate * process so it can be completely initialized at this time. 15210Sstevel@tonic-gate */ 15220Sstevel@tonic-gate cp += sizeof (struct sockaddr_in6); 15230Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)cp; 15240Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 15250Sstevel@tonic-gate sin6->sin6_addr = in6addr_any; 15260Sstevel@tonic-gate 15270Sstevel@tonic-gate /* 15280Sstevel@tonic-gate * Initialize the constant portion of the RTA_IFP sockaddr. 15290Sstevel@tonic-gate */ 15300Sstevel@tonic-gate cp += sizeof (struct sockaddr_in6); 15310Sstevel@tonic-gate rta_ifp = (struct sockaddr_dl *)cp; 15320Sstevel@tonic-gate rta_ifp->sdl_family = AF_LINK; 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate return (s); 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate 15373284Sapersson static int 15383284Sapersson setup_mibsock(void) 15393284Sapersson { 15403284Sapersson int sock; 15413284Sapersson int ret; 15423284Sapersson int len; 15433284Sapersson struct sockaddr_un laddr; 15443284Sapersson 15453284Sapersson sock = socket(AF_UNIX, SOCK_DGRAM, 0); 15463284Sapersson if (sock == -1) { 15473284Sapersson logperror("setup_mibsock: socket(AF_UNIX)"); 15483284Sapersson exit(1); 15493284Sapersson } 15503284Sapersson 15513284Sapersson bzero(&laddr, sizeof (laddr)); 15523284Sapersson laddr.sun_family = AF_UNIX; 15533284Sapersson 15543284Sapersson (void) strncpy(laddr.sun_path, NDPD_SNMP_SOCKET, 15553284Sapersson sizeof (laddr.sun_path)); 15563284Sapersson len = sizeof (struct sockaddr_un); 15573284Sapersson 15583284Sapersson (void) unlink(NDPD_SNMP_SOCKET); 15593284Sapersson ret = bind(sock, (struct sockaddr *)&laddr, len); 15603284Sapersson if (ret < 0) { 15613284Sapersson logperror("setup_mibsock: bind\n"); 15623284Sapersson exit(1); 15633284Sapersson } 15643284Sapersson 15653284Sapersson ret = fcntl(sock, F_SETFL, O_NONBLOCK); 15663284Sapersson if (ret < 0) { 15673284Sapersson logperror("fcntl(O_NONBLOCK)"); 15683284Sapersson exit(1); 15693284Sapersson } 15703284Sapersson if (poll_add(sock) == -1) { 15713284Sapersson exit(1); 15723284Sapersson } 15733284Sapersson return (sock); 15743284Sapersson } 15753284Sapersson 15760Sstevel@tonic-gate /* 15770Sstevel@tonic-gate * Retrieve one routing socket message. If RTM_IFINFO indicates 15780Sstevel@tonic-gate * new phyint do a full scan of the interfaces. If RTM_IFINFO 15792546Scarlsonj * indicates an existing phyint, only scan that phyint and associated 15800Sstevel@tonic-gate * prefixes. 15810Sstevel@tonic-gate */ 15820Sstevel@tonic-gate static void 15830Sstevel@tonic-gate process_rtsock(int rtsock) 15840Sstevel@tonic-gate { 15850Sstevel@tonic-gate int n; 15860Sstevel@tonic-gate #define MSG_SIZE 2048/8 15870Sstevel@tonic-gate int64_t msg[MSG_SIZE]; 15880Sstevel@tonic-gate struct rt_msghdr *rtm; 15890Sstevel@tonic-gate struct if_msghdr *ifm; 15900Sstevel@tonic-gate struct phyint *pi; 15910Sstevel@tonic-gate struct prefix *pr; 15920Sstevel@tonic-gate boolean_t need_initifs = _B_FALSE; 15930Sstevel@tonic-gate boolean_t need_ifscan = _B_FALSE; 15940Sstevel@tonic-gate int64_t ifscan_msg[10][MSG_SIZE]; 15950Sstevel@tonic-gate int ifscan_index = 0; 15960Sstevel@tonic-gate int i; 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate /* Empty the rtsock and coealesce all the work that we have */ 15990Sstevel@tonic-gate while (ifscan_index < 10) { 16000Sstevel@tonic-gate n = read(rtsock, msg, sizeof (msg)); 16010Sstevel@tonic-gate if (n <= 0) { 16020Sstevel@tonic-gate /* No more messages */ 16030Sstevel@tonic-gate break; 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate rtm = (struct rt_msghdr *)msg; 16060Sstevel@tonic-gate if (rtm->rtm_version != RTM_VERSION) { 16070Sstevel@tonic-gate logmsg(LOG_ERR, 16080Sstevel@tonic-gate "process_rtsock: version %d not understood\n", 16090Sstevel@tonic-gate rtm->rtm_version); 16100Sstevel@tonic-gate return; 16110Sstevel@tonic-gate } 16120Sstevel@tonic-gate switch (rtm->rtm_type) { 16130Sstevel@tonic-gate case RTM_NEWADDR: 16140Sstevel@tonic-gate case RTM_DELADDR: 16150Sstevel@tonic-gate /* 16160Sstevel@tonic-gate * Some logical interface has changed - have to scan 16170Sstevel@tonic-gate * everything to determine what actually changed. 16180Sstevel@tonic-gate */ 16190Sstevel@tonic-gate if (debug & D_IFSCAN) { 16200Sstevel@tonic-gate logmsg(LOG_DEBUG, "process_rtsock: " 16210Sstevel@tonic-gate "message %d\n", rtm->rtm_type); 16220Sstevel@tonic-gate } 16230Sstevel@tonic-gate need_initifs = _B_TRUE; 16240Sstevel@tonic-gate break; 16250Sstevel@tonic-gate case RTM_IFINFO: 16260Sstevel@tonic-gate need_ifscan = _B_TRUE; 16270Sstevel@tonic-gate (void) memcpy(ifscan_msg[ifscan_index], rtm, 16280Sstevel@tonic-gate sizeof (msg)); 16290Sstevel@tonic-gate ifscan_index++; 16300Sstevel@tonic-gate /* Handled below */ 16310Sstevel@tonic-gate break; 16320Sstevel@tonic-gate default: 16330Sstevel@tonic-gate /* Not interesting */ 16340Sstevel@tonic-gate break; 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate } 16370Sstevel@tonic-gate /* 16380Sstevel@tonic-gate * If we do full scan i.e initifs, we don't need to 16390Sstevel@tonic-gate * scan a particular interface as we should have 16400Sstevel@tonic-gate * done that as part of initifs. 16410Sstevel@tonic-gate */ 16420Sstevel@tonic-gate if (need_initifs) { 16430Sstevel@tonic-gate initifs(_B_FALSE); 16440Sstevel@tonic-gate return; 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate if (!need_ifscan) 16480Sstevel@tonic-gate return; 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate for (i = 0; i < ifscan_index; i++) { 16510Sstevel@tonic-gate ifm = (struct if_msghdr *)ifscan_msg[i]; 16520Sstevel@tonic-gate if (debug & D_IFSCAN) 16530Sstevel@tonic-gate logmsg(LOG_DEBUG, "process_rtsock: index %d\n", 16540Sstevel@tonic-gate ifm->ifm_index); 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate pi = phyint_lookup_on_index(ifm->ifm_index); 16570Sstevel@tonic-gate if (pi == NULL) { 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * A new physical interface. Do a full scan of the 16600Sstevel@tonic-gate * to catch any new logical interfaces. 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate initifs(_B_FALSE); 16630Sstevel@tonic-gate return; 16640Sstevel@tonic-gate } 16650Sstevel@tonic-gate 1666*8485SPeter.Memishian@Sun.COM if (ifm->ifm_flags != (uint_t)pi->pi_flags) { 16670Sstevel@tonic-gate if (debug & D_IFSCAN) { 16680Sstevel@tonic-gate logmsg(LOG_DEBUG, "process_rtsock: clr for " 1669*8485SPeter.Memishian@Sun.COM "%s old flags 0x%llx new flags 0x%x\n", 16700Sstevel@tonic-gate pi->pi_name, pi->pi_flags, ifm->ifm_flags); 16710Sstevel@tonic-gate } 16720Sstevel@tonic-gate } 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate /* 16760Sstevel@tonic-gate * Mark the interfaces so that we can find phyints and prefixes 16770Sstevel@tonic-gate * which have disappeared from the kernel. 16780Sstevel@tonic-gate * if_process will set pr_in_use when it finds the 16790Sstevel@tonic-gate * interface in the kernel. 16800Sstevel@tonic-gate * Before re-examining the state of the interfaces, 16810Sstevel@tonic-gate * PI_PRESENT should be cleared from pi_kernel_state. 16820Sstevel@tonic-gate */ 16830Sstevel@tonic-gate pi->pi_kernel_state &= ~PI_PRESENT; 16840Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 16850Sstevel@tonic-gate pr->pr_in_use = _B_FALSE; 16860Sstevel@tonic-gate } 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate if (ifsock < 0) { 16890Sstevel@tonic-gate ifsock = socket(AF_INET6, SOCK_DGRAM, 0); 16900Sstevel@tonic-gate if (ifsock < 0) { 16910Sstevel@tonic-gate logperror("process_rtsock: socket"); 16920Sstevel@tonic-gate return; 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate } 16950Sstevel@tonic-gate if_process(ifsock, pi->pi_name, _B_FALSE); 16960Sstevel@tonic-gate for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) { 16970Sstevel@tonic-gate if_process(ifsock, pr->pr_name, _B_FALSE); 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate /* 17000Sstevel@tonic-gate * If interface (still) exists in kernel, set 17010Sstevel@tonic-gate * pi_state to indicate that. 17020Sstevel@tonic-gate */ 17030Sstevel@tonic-gate if (pi->pi_kernel_state & PI_PRESENT) { 17040Sstevel@tonic-gate pi->pi_state |= PI_PRESENT; 17050Sstevel@tonic-gate } 17060Sstevel@tonic-gate check_if_removed(pi); 17070Sstevel@tonic-gate if (show_ifs) 17080Sstevel@tonic-gate phyint_print_all(); 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate 17123284Sapersson static void 17133284Sapersson process_mibsock(int mibsock) 17143284Sapersson { 17153284Sapersson struct phyint *pi; 17163284Sapersson socklen_t fromlen; 17173284Sapersson struct sockaddr_un from; 17183284Sapersson ndpd_info_t ndpd_info; 17193284Sapersson ssize_t len; 17203284Sapersson int command; 17213284Sapersson 17223284Sapersson fromlen = (socklen_t)sizeof (from); 17233284Sapersson len = recvfrom(mibsock, &command, sizeof (int), 0, 17243284Sapersson (struct sockaddr *)&from, &fromlen); 17253284Sapersson 17263284Sapersson if (len < sizeof (int) || command != NDPD_SNMP_INFO_REQ) { 17273284Sapersson logperror("process_mibsock: bad command \n"); 17283284Sapersson return; 17293284Sapersson } 17303284Sapersson 17313284Sapersson ndpd_info.info_type = NDPD_SNMP_INFO_RESPONSE; 17323284Sapersson ndpd_info.info_version = NDPD_SNMP_INFO_VER; 17333284Sapersson ndpd_info.info_num_of_phyints = num_of_phyints; 17343284Sapersson 17353284Sapersson (void) sendto(mibsock, &ndpd_info, sizeof (ndpd_info_t), 0, 17363284Sapersson (struct sockaddr *)&from, fromlen); 17373284Sapersson 17383284Sapersson for (pi = phyints; pi != NULL; pi = pi->pi_next) { 17393284Sapersson int prefixes; 17403284Sapersson int routers; 17413284Sapersson struct prefix *prefix_list; 17423284Sapersson struct router *router_list; 17433284Sapersson ndpd_phyint_info_t phyint; 17443284Sapersson ndpd_prefix_info_t prefix; 17453284Sapersson ndpd_router_info_t router; 17463284Sapersson /* 17473284Sapersson * get number of prefixes 17483284Sapersson */ 17493284Sapersson routers = 0; 17503284Sapersson prefixes = 0; 17513284Sapersson prefix_list = pi->pi_prefix_list; 17523284Sapersson while (prefix_list != NULL) { 17533284Sapersson prefixes++; 17543284Sapersson prefix_list = prefix_list->pr_next; 17553284Sapersson } 17563284Sapersson 17573284Sapersson /* 17583284Sapersson * get number of routers 17593284Sapersson */ 17603284Sapersson router_list = pi->pi_router_list; 17613284Sapersson while (router_list != NULL) { 17623284Sapersson routers++; 17633284Sapersson router_list = router_list->dr_next; 17643284Sapersson } 17653284Sapersson 17663284Sapersson phyint.phyint_info_type = NDPD_PHYINT_INFO; 17673284Sapersson phyint.phyint_info_version = NDPD_PHYINT_INFO_VER; 17683284Sapersson phyint.phyint_index = pi->pi_index; 17693284Sapersson bcopy(pi->pi_config, 17703284Sapersson phyint.phyint_config, I_IFSIZE); 17713284Sapersson phyint.phyint_num_of_prefixes = prefixes; 17723284Sapersson phyint.phyint_num_of_routers = routers; 17733284Sapersson (void) sendto(mibsock, &phyint, sizeof (phyint), 0, 17743284Sapersson (struct sockaddr *)&from, fromlen); 17753284Sapersson 17763284Sapersson /* 17773284Sapersson * Copy prefix information 17783284Sapersson */ 17793284Sapersson 17803284Sapersson prefix_list = pi->pi_prefix_list; 17813284Sapersson while (prefix_list != NULL) { 17823284Sapersson prefix.prefix_info_type = NDPD_PREFIX_INFO; 17833284Sapersson prefix.prefix_info_version = NDPD_PREFIX_INFO_VER; 17843284Sapersson prefix.prefix_prefix = prefix_list->pr_prefix; 17853284Sapersson prefix.prefix_len = prefix_list->pr_prefix_len; 17863284Sapersson prefix.prefix_flags = prefix_list->pr_flags; 17873284Sapersson prefix.prefix_phyint_index = pi->pi_index; 17883284Sapersson prefix.prefix_ValidLifetime = 17893284Sapersson prefix_list->pr_ValidLifetime; 17903284Sapersson prefix.prefix_PreferredLifetime = 17913284Sapersson prefix_list->pr_PreferredLifetime; 17923284Sapersson prefix.prefix_OnLinkLifetime = 17933284Sapersson prefix_list->pr_OnLinkLifetime; 17943284Sapersson prefix.prefix_OnLinkFlag = 17953284Sapersson prefix_list->pr_OnLinkFlag; 17963284Sapersson prefix.prefix_AutonomousFlag = 17973284Sapersson prefix_list->pr_AutonomousFlag; 17983284Sapersson (void) sendto(mibsock, &prefix, sizeof (prefix), 0, 17993284Sapersson (struct sockaddr *)&from, fromlen); 18003284Sapersson prefix_list = prefix_list->pr_next; 18013284Sapersson } 18023284Sapersson /* 18033284Sapersson * Copy router information 18043284Sapersson */ 18053284Sapersson router_list = pi->pi_router_list; 18063284Sapersson while (router_list != NULL) { 18073284Sapersson router.router_info_type = NDPD_ROUTER_INFO; 18083284Sapersson router.router_info_version = NDPD_ROUTER_INFO_VER; 18093284Sapersson router.router_address = router_list->dr_address; 18103284Sapersson router.router_lifetime = router_list->dr_lifetime; 18113284Sapersson router.router_phyint_index = pi->pi_index; 18123284Sapersson (void) sendto(mibsock, &router, sizeof (router), 0, 18133284Sapersson (struct sockaddr *)&from, fromlen); 18143284Sapersson router_list = router_list->dr_next; 18153284Sapersson } 18163284Sapersson } 18173284Sapersson } 18183284Sapersson 18190Sstevel@tonic-gate /* 18200Sstevel@tonic-gate * Look if the phyint or one of its prefixes have been removed from 18210Sstevel@tonic-gate * the kernel and take appropriate action. 1822*8485SPeter.Memishian@Sun.COM * Uses pr_in_use and pi{,_kernel}_state. 18230Sstevel@tonic-gate */ 18240Sstevel@tonic-gate static void 18250Sstevel@tonic-gate check_if_removed(struct phyint *pi) 18260Sstevel@tonic-gate { 1827*8485SPeter.Memishian@Sun.COM struct prefix *pr, *next_pr; 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate /* 1830*8485SPeter.Memishian@Sun.COM * Detect prefixes which are removed. 1831*8485SPeter.Memishian@Sun.COM * Static prefixes are just removed from our tables. 1832*8485SPeter.Memishian@Sun.COM * Non-static prefixes are recreated i.e. in.ndpd takes precedence 1833*8485SPeter.Memishian@Sun.COM * over manually removing prefixes via ifconfig. 1834*8485SPeter.Memishian@Sun.COM */ 1835*8485SPeter.Memishian@Sun.COM for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 1836*8485SPeter.Memishian@Sun.COM next_pr = pr->pr_next; 1837*8485SPeter.Memishian@Sun.COM if (!pr->pr_in_use) { 1838*8485SPeter.Memishian@Sun.COM /* Clear everything except PR_STATIC */ 1839*8485SPeter.Memishian@Sun.COM pr->pr_kernel_state &= PR_STATIC; 1840*8485SPeter.Memishian@Sun.COM pr->pr_name[0] = '\0'; 1841*8485SPeter.Memishian@Sun.COM if (pr->pr_state & PR_STATIC) { 1842*8485SPeter.Memishian@Sun.COM prefix_delete(pr); 1843*8485SPeter.Memishian@Sun.COM } else if (!(pi->pi_kernel_state & PI_PRESENT)) { 1844*8485SPeter.Memishian@Sun.COM /* 1845*8485SPeter.Memishian@Sun.COM * Ensure that there are no future attempts to 1846*8485SPeter.Memishian@Sun.COM * run prefix_update_k since the phyint is gone. 1847*8485SPeter.Memishian@Sun.COM */ 1848*8485SPeter.Memishian@Sun.COM pr->pr_state = pr->pr_kernel_state; 1849*8485SPeter.Memishian@Sun.COM } else if (pr->pr_state != pr->pr_kernel_state) { 1850*8485SPeter.Memishian@Sun.COM logmsg(LOG_INFO, "Prefix manually removed " 1851*8485SPeter.Memishian@Sun.COM "on %s; recreating\n", pi->pi_name); 1852*8485SPeter.Memishian@Sun.COM prefix_update_k(pr); 1853*8485SPeter.Memishian@Sun.COM } 1854*8485SPeter.Memishian@Sun.COM } 1855*8485SPeter.Memishian@Sun.COM } 1856*8485SPeter.Memishian@Sun.COM 1857*8485SPeter.Memishian@Sun.COM /* 1858*8485SPeter.Memishian@Sun.COM * Detect phyints that have been removed from the kernel, and tear 1859*8485SPeter.Memishian@Sun.COM * down any prefixes we created that are associated with that phyint. 1860*8485SPeter.Memishian@Sun.COM * (NOTE: IPMP depends on in.ndpd tearing down these prefixes so an 1861*8485SPeter.Memishian@Sun.COM * administrator can easily place an IP interface with ADDRCONF'd 1862*8485SPeter.Memishian@Sun.COM * addresses into an IPMP group.) 18630Sstevel@tonic-gate */ 18640Sstevel@tonic-gate if (!(pi->pi_kernel_state & PI_PRESENT) && 18650Sstevel@tonic-gate (pi->pi_state & PI_PRESENT)) { 18660Sstevel@tonic-gate logmsg(LOG_ERR, "Interface %s has been removed from kernel. " 18670Sstevel@tonic-gate "in.ndpd will no longer use it\n", pi->pi_name); 1868*8485SPeter.Memishian@Sun.COM 1869*8485SPeter.Memishian@Sun.COM for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) { 1870*8485SPeter.Memishian@Sun.COM next_pr = pr->pr_next; 1871*8485SPeter.Memishian@Sun.COM if (pr->pr_state & PR_AUTO) 1872*8485SPeter.Memishian@Sun.COM prefix_delete(pr); 1873*8485SPeter.Memishian@Sun.COM } 1874*8485SPeter.Memishian@Sun.COM 18750Sstevel@tonic-gate /* 1876*8485SPeter.Memishian@Sun.COM * Clear state so that should the phyint reappear we will 1877*8485SPeter.Memishian@Sun.COM * start with initial advertisements or solicitations. 18780Sstevel@tonic-gate */ 18790Sstevel@tonic-gate phyint_cleanup(pi); 18800Sstevel@tonic-gate } 18810Sstevel@tonic-gate } 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate /* 18850Sstevel@tonic-gate * Queuing mechanism for router advertisements that are sent by in.ndpd 18860Sstevel@tonic-gate * and that also need to be processed by in.ndpd. 18870Sstevel@tonic-gate * Uses "signal number" 255 to indicate to the main poll loop 18880Sstevel@tonic-gate * that there is something to dequeue and send to incomining_ra(). 18890Sstevel@tonic-gate */ 18900Sstevel@tonic-gate struct raq { 18910Sstevel@tonic-gate struct raq *raq_next; 18920Sstevel@tonic-gate struct phyint *raq_pi; 18930Sstevel@tonic-gate int raq_packetlen; 18940Sstevel@tonic-gate uchar_t *raq_packet; 18950Sstevel@tonic-gate }; 18960Sstevel@tonic-gate static struct raq *raq_head = NULL; 18970Sstevel@tonic-gate 18980Sstevel@tonic-gate /* 18990Sstevel@tonic-gate * Allocate a struct raq and memory for the packet. 19000Sstevel@tonic-gate * Send signal 255 to have poll dequeue. 19010Sstevel@tonic-gate */ 19020Sstevel@tonic-gate static void 19030Sstevel@tonic-gate loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len) 19040Sstevel@tonic-gate { 19050Sstevel@tonic-gate struct raq *raq; 19060Sstevel@tonic-gate struct raq **raqp; 19070Sstevel@tonic-gate 19080Sstevel@tonic-gate if (no_loopback) 19090Sstevel@tonic-gate return; 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate if (debug & D_PKTOUT) 19120Sstevel@tonic-gate logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name); 19130Sstevel@tonic-gate 19140Sstevel@tonic-gate raq = calloc(sizeof (struct raq), 1); 19150Sstevel@tonic-gate if (raq == NULL) { 19160Sstevel@tonic-gate logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 19170Sstevel@tonic-gate return; 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate raq->raq_packet = malloc(len); 19200Sstevel@tonic-gate if (raq->raq_packet == NULL) { 19210Sstevel@tonic-gate free(raq); 19220Sstevel@tonic-gate logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n"); 19230Sstevel@tonic-gate return; 19240Sstevel@tonic-gate } 19250Sstevel@tonic-gate bcopy(ra, raq->raq_packet, len); 19260Sstevel@tonic-gate raq->raq_packetlen = len; 19270Sstevel@tonic-gate raq->raq_pi = pi; 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate /* Tail insert */ 19300Sstevel@tonic-gate raqp = &raq_head; 19310Sstevel@tonic-gate while (*raqp != NULL) 19320Sstevel@tonic-gate raqp = &((*raqp)->raq_next); 19330Sstevel@tonic-gate *raqp = raq; 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate /* Signal for poll loop */ 19360Sstevel@tonic-gate sig_handler(255); 19370Sstevel@tonic-gate } 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate /* 19400Sstevel@tonic-gate * Dequeue and process all queued advertisements. 19410Sstevel@tonic-gate */ 19420Sstevel@tonic-gate static void 19430Sstevel@tonic-gate loopback_ra_dequeue(void) 19440Sstevel@tonic-gate { 19450Sstevel@tonic-gate struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT; 19460Sstevel@tonic-gate struct raq *raq; 19470Sstevel@tonic-gate 19480Sstevel@tonic-gate if (debug & D_PKTIN) 19490Sstevel@tonic-gate logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n"); 19500Sstevel@tonic-gate 19510Sstevel@tonic-gate while ((raq = raq_head) != NULL) { 19520Sstevel@tonic-gate raq_head = raq->raq_next; 19530Sstevel@tonic-gate raq->raq_next = NULL; 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate if (debug & D_PKTIN) { 19560Sstevel@tonic-gate logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n", 19570Sstevel@tonic-gate raq->raq_pi->pi_name); 19580Sstevel@tonic-gate } 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate incoming_ra(raq->raq_pi, 19610Sstevel@tonic-gate (struct nd_router_advert *)raq->raq_packet, 19620Sstevel@tonic-gate raq->raq_packetlen, &from, _B_TRUE); 19630Sstevel@tonic-gate free(raq->raq_packet); 19640Sstevel@tonic-gate free(raq); 19650Sstevel@tonic-gate } 19660Sstevel@tonic-gate } 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate 19690Sstevel@tonic-gate static void 19700Sstevel@tonic-gate usage(char *cmd) 19710Sstevel@tonic-gate { 19720Sstevel@tonic-gate (void) fprintf(stderr, 19730Sstevel@tonic-gate "usage: %s [ -adt ] [-f <config file>]\n", cmd); 19740Sstevel@tonic-gate } 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate int 19770Sstevel@tonic-gate main(int argc, char *argv[]) 19780Sstevel@tonic-gate { 19790Sstevel@tonic-gate int i; 19800Sstevel@tonic-gate struct phyint *pi; 19810Sstevel@tonic-gate int c; 19820Sstevel@tonic-gate char *config_file = PATH_NDPD_CONF; 19830Sstevel@tonic-gate boolean_t file_required = _B_FALSE; 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate argv0 = argv; 19860Sstevel@tonic-gate srandom(gethostid()); 19870Sstevel@tonic-gate (void) umask(0022); 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) { 19900Sstevel@tonic-gate switch (c) { 19910Sstevel@tonic-gate case 'a': 19920Sstevel@tonic-gate /* 19930Sstevel@tonic-gate * The StatelessAddrConf variable in ndpd.conf, if 19940Sstevel@tonic-gate * present, will override this setting. 19950Sstevel@tonic-gate */ 19960Sstevel@tonic-gate ifdefaults[I_StatelessAddrConf].cf_value = 0; 19970Sstevel@tonic-gate break; 19980Sstevel@tonic-gate case 'd': 19990Sstevel@tonic-gate debug = D_ALL; 20000Sstevel@tonic-gate break; 20010Sstevel@tonic-gate case 'D': 20020Sstevel@tonic-gate i = strtol((char *)optarg, NULL, 0); 20030Sstevel@tonic-gate if (i == 0) { 20040Sstevel@tonic-gate (void) fprintf(stderr, "Bad debug flags: %s\n", 20050Sstevel@tonic-gate (char *)optarg); 20060Sstevel@tonic-gate exit(1); 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate debug |= i; 20090Sstevel@tonic-gate break; 20100Sstevel@tonic-gate case 'n': 20110Sstevel@tonic-gate no_loopback = 1; 20120Sstevel@tonic-gate break; 20130Sstevel@tonic-gate case 'I': 20140Sstevel@tonic-gate show_ifs = 1; 20150Sstevel@tonic-gate break; 20160Sstevel@tonic-gate case 't': 20170Sstevel@tonic-gate debug |= D_PKTIN | D_PKTOUT | D_PKTBAD; 20180Sstevel@tonic-gate break; 20190Sstevel@tonic-gate case 'f': 20200Sstevel@tonic-gate config_file = (char *)optarg; 20210Sstevel@tonic-gate file_required = _B_TRUE; 20220Sstevel@tonic-gate break; 20230Sstevel@tonic-gate case '?': 20240Sstevel@tonic-gate usage(argv[0]); 20250Sstevel@tonic-gate exit(1); 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate if (parse_config(config_file, file_required) == -1) 20300Sstevel@tonic-gate exit(2); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate if (show_ifs) 20330Sstevel@tonic-gate phyint_print_all(); 20340Sstevel@tonic-gate 20350Sstevel@tonic-gate if (debug == 0) { 20360Sstevel@tonic-gate initlog(); 20370Sstevel@tonic-gate } 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate setup_eventpipe(); 20400Sstevel@tonic-gate rtsock = setup_rtsock(); 20413284Sapersson mibsock = setup_mibsock(); 20420Sstevel@tonic-gate timer_init(); 20430Sstevel@tonic-gate initifs(_B_TRUE); 20440Sstevel@tonic-gate 20450Sstevel@tonic-gate check_daemonize(); 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate for (;;) { 20480Sstevel@tonic-gate if (poll(pollfds, pollfd_num, -1) < 0) { 20490Sstevel@tonic-gate if (errno == EINTR) 20500Sstevel@tonic-gate continue; 20510Sstevel@tonic-gate logperror("main: poll"); 20520Sstevel@tonic-gate exit(1); 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 20550Sstevel@tonic-gate if (!(pollfds[i].revents & POLLIN)) 20560Sstevel@tonic-gate continue; 20570Sstevel@tonic-gate if (pollfds[i].fd == eventpipe_read) { 20580Sstevel@tonic-gate in_signal(eventpipe_read); 20590Sstevel@tonic-gate break; 20600Sstevel@tonic-gate } 20610Sstevel@tonic-gate if (pollfds[i].fd == rtsock) { 20620Sstevel@tonic-gate process_rtsock(rtsock); 20630Sstevel@tonic-gate break; 20640Sstevel@tonic-gate } 20653284Sapersson if (pollfds[i].fd == mibsock) { 20663284Sapersson process_mibsock(mibsock); 20673284Sapersson break; 20683284Sapersson } 20690Sstevel@tonic-gate /* 20700Sstevel@tonic-gate * Run timer routine to advance clock if more than 20710Sstevel@tonic-gate * half a second since the clock was advanced. 20720Sstevel@tonic-gate * This limits CPU usage under severe packet 20730Sstevel@tonic-gate * arrival rates but it creates a slight inaccuracy 20740Sstevel@tonic-gate * in the timer mechanism. 20750Sstevel@tonic-gate */ 20760Sstevel@tonic-gate conditional_run_timeouts(500U); 20770Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 20780Sstevel@tonic-gate if (pollfds[i].fd == pi->pi_sock) { 20790Sstevel@tonic-gate in_data(pi); 20800Sstevel@tonic-gate break; 20810Sstevel@tonic-gate } 20820Sstevel@tonic-gate } 20830Sstevel@tonic-gate } 20840Sstevel@tonic-gate } 20850Sstevel@tonic-gate /* NOTREACHED */ 20860Sstevel@tonic-gate return (0); 20870Sstevel@tonic-gate } 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate /* 20900Sstevel@tonic-gate * LOGGER 20910Sstevel@tonic-gate */ 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate static boolean_t logging = _B_FALSE; 20940Sstevel@tonic-gate 20950Sstevel@tonic-gate static void 20960Sstevel@tonic-gate initlog(void) 20970Sstevel@tonic-gate { 20980Sstevel@tonic-gate logging = _B_TRUE; 20990Sstevel@tonic-gate openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON); 21000Sstevel@tonic-gate } 21010Sstevel@tonic-gate 21020Sstevel@tonic-gate /* Print the date/time without a trailing carridge return */ 21030Sstevel@tonic-gate static void 21040Sstevel@tonic-gate fprintdate(FILE *file) 21050Sstevel@tonic-gate { 21060Sstevel@tonic-gate char buf[BUFSIZ]; 21070Sstevel@tonic-gate struct tm tms; 21080Sstevel@tonic-gate time_t now; 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate now = time(NULL); 21110Sstevel@tonic-gate (void) localtime_r(&now, &tms); 21120Sstevel@tonic-gate (void) strftime(buf, sizeof (buf), "%h %d %X", &tms); 21130Sstevel@tonic-gate (void) fprintf(file, "%s ", buf); 21140Sstevel@tonic-gate } 21150Sstevel@tonic-gate 21162546Scarlsonj /* PRINTFLIKE2 */ 21170Sstevel@tonic-gate void 21182428Smh138676 logmsg(int level, const char *fmt, ...) 21190Sstevel@tonic-gate { 21200Sstevel@tonic-gate va_list ap; 21210Sstevel@tonic-gate va_start(ap, fmt); 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate if (logging) { 21240Sstevel@tonic-gate vsyslog(level, fmt, ap); 21250Sstevel@tonic-gate } else { 21260Sstevel@tonic-gate fprintdate(stderr); 21270Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 21280Sstevel@tonic-gate } 21290Sstevel@tonic-gate va_end(ap); 21300Sstevel@tonic-gate } 21310Sstevel@tonic-gate 21320Sstevel@tonic-gate void 21332428Smh138676 logperror(const char *str) 21340Sstevel@tonic-gate { 21350Sstevel@tonic-gate if (logging) { 21360Sstevel@tonic-gate syslog(LOG_ERR, "%s: %m\n", str); 21370Sstevel@tonic-gate } else { 21380Sstevel@tonic-gate fprintdate(stderr); 21390Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", str, strerror(errno)); 21400Sstevel@tonic-gate } 21410Sstevel@tonic-gate } 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate void 21442428Smh138676 logperror_pi(const struct phyint *pi, const char *str) 21450Sstevel@tonic-gate { 21460Sstevel@tonic-gate if (logging) { 21470Sstevel@tonic-gate syslog(LOG_ERR, "%s (interface %s): %m\n", 21480Sstevel@tonic-gate str, pi->pi_name); 21490Sstevel@tonic-gate } else { 21500Sstevel@tonic-gate fprintdate(stderr); 21510Sstevel@tonic-gate (void) fprintf(stderr, "%s (interface %s): %s\n", 21520Sstevel@tonic-gate str, pi->pi_name, strerror(errno)); 21530Sstevel@tonic-gate } 21540Sstevel@tonic-gate } 21550Sstevel@tonic-gate 21560Sstevel@tonic-gate void 21572428Smh138676 logperror_pr(const struct prefix *pr, const char *str) 21580Sstevel@tonic-gate { 21590Sstevel@tonic-gate if (logging) { 21600Sstevel@tonic-gate syslog(LOG_ERR, "%s (prefix %s if %s): %m\n", 21610Sstevel@tonic-gate str, pr->pr_name, pr->pr_physical->pi_name); 21620Sstevel@tonic-gate } else { 21630Sstevel@tonic-gate fprintdate(stderr); 21640Sstevel@tonic-gate (void) fprintf(stderr, "%s (prefix %s if %s): %s\n", 21650Sstevel@tonic-gate str, pr->pr_name, pr->pr_physical->pi_name, 21660Sstevel@tonic-gate strerror(errno)); 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate } 2169