xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/in.ndpd/main.c (revision 12805:288ae711509e)
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
208485SPeter.Memishian@Sun.COM  *
21*12805SDarren.Reed@Oracle.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
220Sstevel@tonic-gate  */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "defs.h"
250Sstevel@tonic-gate #include "tables.h"
260Sstevel@tonic-gate #include <fcntl.h>
273284Sapersson #include <sys/un.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate static void	initlog(void);
300Sstevel@tonic-gate static void	run_timeouts(void);
310Sstevel@tonic-gate 
320Sstevel@tonic-gate static void	advertise(struct sockaddr_in6 *sin6, struct phyint *pi,
330Sstevel@tonic-gate 		    boolean_t no_prefixes);
340Sstevel@tonic-gate static void	solicit(struct sockaddr_in6 *sin6, struct phyint *pi);
350Sstevel@tonic-gate static void	initifs(boolean_t first);
360Sstevel@tonic-gate static void	check_if_removed(struct phyint *pi);
370Sstevel@tonic-gate static void	loopback_ra_enqueue(struct phyint *pi,
380Sstevel@tonic-gate 		    struct nd_router_advert *ra, int len);
390Sstevel@tonic-gate static void	loopback_ra_dequeue(void);
400Sstevel@tonic-gate static void	check_daemonize(void);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0,
430Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
440Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
450Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x1 } };
460Sstevel@tonic-gate 
470Sstevel@tonic-gate struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0,
480Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
490Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
500Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x2 } };
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0,
530Sstevel@tonic-gate 				    { 0xff, 0x2, 0x0, 0x0,
540Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
550Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
560Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x1 } };
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0,
590Sstevel@tonic-gate 				    { 0xff, 0x2, 0x0, 0x0,
600Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
610Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x0,
620Sstevel@tonic-gate 				    0x0, 0x0, 0x0, 0x2 } };
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static char **argv0;		/* Saved for re-exec on SIGHUP */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static uint64_t packet[(IP_MAXPACKET + 1)/8];
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int	show_ifs = 0;
690Sstevel@tonic-gate static boolean_t	already_daemonized = _B_FALSE;
700Sstevel@tonic-gate int		debug = 0;
710Sstevel@tonic-gate int		no_loopback = 0; /* Do not send RA packets to ourselves */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Size of routing socket message used by in.ndpd which includes the header,
750Sstevel@tonic-gate  * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6)
760Sstevel@tonic-gate  * plus space for the RTA_IFP (a sockaddr_dl).
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate #define	NDP_RTM_MSGLEN	sizeof (struct rt_msghdr) +	\
790Sstevel@tonic-gate 			sizeof (struct sockaddr_in6) +	\
800Sstevel@tonic-gate 			sizeof (struct sockaddr_in6) +	\
810Sstevel@tonic-gate 			sizeof (struct sockaddr_in6) +	\
820Sstevel@tonic-gate 			sizeof (struct sockaddr_dl)
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * These are referenced externally in tables.c in order to fill in the
860Sstevel@tonic-gate  * dynamic portions of the routing socket message and then to send the message
870Sstevel@tonic-gate  * itself.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate int	rtsock = -1;			/* Routing socket */
900Sstevel@tonic-gate struct	rt_msghdr	*rt_msg;	/* Routing socket message */
910Sstevel@tonic-gate struct	sockaddr_in6	*rta_gateway;	/* RTA_GATEWAY sockaddr */
920Sstevel@tonic-gate struct	sockaddr_dl	*rta_ifp;	/* RTA_IFP sockaddr */
9312016SGirish.Moodalbail@Sun.COM 
9412016SGirish.Moodalbail@Sun.COM /*
9512016SGirish.Moodalbail@Sun.COM  * These sockets are used internally in this file.
9612016SGirish.Moodalbail@Sun.COM  */
9712016SGirish.Moodalbail@Sun.COM static int	mibsock = -1;			/* mib request socket */
9812016SGirish.Moodalbail@Sun.COM static int	cmdsock = -1;			/* command socket */
9912016SGirish.Moodalbail@Sun.COM 
10012016SGirish.Moodalbail@Sun.COM static	int	ndpd_setup_cmd_listener(void);
10112016SGirish.Moodalbail@Sun.COM static	void	ndpd_cmd_handler(int);
10212016SGirish.Moodalbail@Sun.COM static	int	ndpd_process_cmd(int, ipadm_ndpd_msg_t *);
10312016SGirish.Moodalbail@Sun.COM static	int	ndpd_send_error(int, int);
10412016SGirish.Moodalbail@Sun.COM static	int	ndpd_set_autoconf(const char *, boolean_t);
10512016SGirish.Moodalbail@Sun.COM static	int	ndpd_create_addrs(const char *, struct sockaddr_in6, int,
10612016SGirish.Moodalbail@Sun.COM     boolean_t, boolean_t, char *);
10712016SGirish.Moodalbail@Sun.COM static	int	ndpd_delete_addrs(const char *);
10812016SGirish.Moodalbail@Sun.COM static	int	phyint_check_ipadm_intfid(struct phyint *);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * Return the current time in milliseconds truncated to
1120Sstevel@tonic-gate  * fit in an integer.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate uint_t
getcurrenttime(void)1150Sstevel@tonic-gate getcurrenttime(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	struct timeval tp;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (gettimeofday(&tp, NULL) < 0) {
1200Sstevel@tonic-gate 		logperror("getcurrenttime: gettimeofday failed");
1210Sstevel@tonic-gate 		exit(1);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	return (tp.tv_sec * 1000 + tp.tv_usec / 1000);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Output a preformated packet from the packet[] buffer.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static void
sendpacket(struct sockaddr_in6 * sin6,int sock,int size,int flags)1300Sstevel@tonic-gate sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	int cc;
1330Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	cc = sendto(sock, (char *)packet, size, flags,
1368485SPeter.Memishian@Sun.COM 	    (struct sockaddr *)sin6, sizeof (*sin6));
1370Sstevel@tonic-gate 	if (cc < 0 || cc != size) {
1380Sstevel@tonic-gate 		if (cc < 0) {
1390Sstevel@tonic-gate 			logperror("sendpacket: sendto");
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 		logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n",
1420Sstevel@tonic-gate 		    inet_ntop(sin6->sin6_family,
1430Sstevel@tonic-gate 		    (void *)&sin6->sin6_addr,
1440Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
1450Sstevel@tonic-gate 		    size, cc);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1498485SPeter.Memishian@Sun.COM /*
1508485SPeter.Memishian@Sun.COM  * If possible, place an ND_OPT_SOURCE_LINKADDR option at `optp'.
1518485SPeter.Memishian@Sun.COM  * Return the number of bytes placed in the option.
1528485SPeter.Memishian@Sun.COM  */
1538485SPeter.Memishian@Sun.COM static uint_t
add_opt_lla(struct phyint * pi,struct nd_opt_lla * optp)1548485SPeter.Memishian@Sun.COM add_opt_lla(struct phyint *pi, struct nd_opt_lla *optp)
1558485SPeter.Memishian@Sun.COM {
1568485SPeter.Memishian@Sun.COM 	uint_t optlen;
1578485SPeter.Memishian@Sun.COM 	uint_t hwaddrlen;
1588485SPeter.Memishian@Sun.COM 	struct lifreq lifr;
1598485SPeter.Memishian@Sun.COM 
1608485SPeter.Memishian@Sun.COM 	/* If this phyint doesn't have a link-layer address, bail */
1618485SPeter.Memishian@Sun.COM 	if (phyint_get_lla(pi, &lifr) == -1)
1628485SPeter.Memishian@Sun.COM 		return (0);
1638485SPeter.Memishian@Sun.COM 
1648485SPeter.Memishian@Sun.COM 	hwaddrlen = lifr.lifr_nd.lnr_hdw_len;
1658485SPeter.Memishian@Sun.COM 	/* roundup to multiple of 8 and make padding zero */
1668485SPeter.Memishian@Sun.COM 	optlen = ((sizeof (struct nd_opt_hdr) + hwaddrlen + 7) / 8) * 8;
1678485SPeter.Memishian@Sun.COM 	bzero(optp, optlen);
1688485SPeter.Memishian@Sun.COM 	optp->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR;
1698485SPeter.Memishian@Sun.COM 	optp->nd_opt_lla_len = optlen / 8;
1708485SPeter.Memishian@Sun.COM 	bcopy(lifr.lifr_nd.lnr_hdw_addr, optp->nd_opt_lla_hdw_addr, hwaddrlen);
1718485SPeter.Memishian@Sun.COM 
1728485SPeter.Memishian@Sun.COM 	return (optlen);
1738485SPeter.Memishian@Sun.COM }
1748485SPeter.Memishian@Sun.COM 
1750Sstevel@tonic-gate /* Send a Router Solicitation */
1760Sstevel@tonic-gate static void
solicit(struct sockaddr_in6 * sin6,struct phyint * pi)1770Sstevel@tonic-gate solicit(struct sockaddr_in6 *sin6, struct phyint *pi)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	int packetlen = 0;
1800Sstevel@tonic-gate 	struct	nd_router_solicit *rs = (struct nd_router_solicit *)packet;
1810Sstevel@tonic-gate 	char *pptr = (char *)packet;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	rs->nd_rs_type = ND_ROUTER_SOLICIT;
1840Sstevel@tonic-gate 	rs->nd_rs_code = 0;
1850Sstevel@tonic-gate 	rs->nd_rs_cksum = htons(0);
1860Sstevel@tonic-gate 	rs->nd_rs_reserved = htonl(0);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	packetlen += sizeof (*rs);
1890Sstevel@tonic-gate 	pptr += sizeof (*rs);
1900Sstevel@tonic-gate 
1918485SPeter.Memishian@Sun.COM 	/* add options */
1928485SPeter.Memishian@Sun.COM 	packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (debug & D_PKTOUT) {
1950Sstevel@tonic-gate 		print_route_sol("Sending solicitation to ", pi, rs, packetlen,
1960Sstevel@tonic-gate 		    sin6);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	sendpacket(sin6, pi->pi_sock, packetlen, 0);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Send a (set of) Router Advertisements and feed them back to ourselves
2030Sstevel@tonic-gate  * for processing. Unless no_prefixes is set all prefixes are included.
2040Sstevel@tonic-gate  * If there are too many prefix options to fit in one packet multiple
2050Sstevel@tonic-gate  * packets will be sent - each containing a subset of the prefix options.
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate static void
advertise(struct sockaddr_in6 * sin6,struct phyint * pi,boolean_t no_prefixes)2080Sstevel@tonic-gate advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	struct	nd_opt_prefix_info *po;
2110Sstevel@tonic-gate 	char *pptr = (char *)packet;
2120Sstevel@tonic-gate 	struct nd_router_advert *ra;
2130Sstevel@tonic-gate 	struct adv_prefix *adv_pr;
2140Sstevel@tonic-gate 	int packetlen = 0;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	ra = (struct nd_router_advert *)pptr;
2170Sstevel@tonic-gate 	ra->nd_ra_type = ND_ROUTER_ADVERT;
2180Sstevel@tonic-gate 	ra->nd_ra_code = 0;
2190Sstevel@tonic-gate 	ra->nd_ra_cksum = htons(0);
2200Sstevel@tonic-gate 	ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit;
2210Sstevel@tonic-gate 	ra->nd_ra_flags_reserved = 0;
2220Sstevel@tonic-gate 	if (pi->pi_AdvManagedFlag)
2230Sstevel@tonic-gate 		ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
2240Sstevel@tonic-gate 	if (pi->pi_AdvOtherConfigFlag)
2250Sstevel@tonic-gate 		ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (pi->pi_adv_state == FINAL_ADV)
2280Sstevel@tonic-gate 		ra->nd_ra_router_lifetime = htons(0);
2290Sstevel@tonic-gate 	else
2300Sstevel@tonic-gate 		ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime);
2310Sstevel@tonic-gate 	ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime);
2320Sstevel@tonic-gate 	ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	packetlen = sizeof (*ra);
2350Sstevel@tonic-gate 	pptr += sizeof (*ra);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (pi->pi_adv_state == FINAL_ADV) {
2380Sstevel@tonic-gate 		if (debug & D_PKTOUT) {
2390Sstevel@tonic-gate 			print_route_adv("Sending advert (FINAL) to ", pi,
2400Sstevel@tonic-gate 			    ra, packetlen, sin6);
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 		sendpacket(sin6, pi->pi_sock, packetlen, 0);
2430Sstevel@tonic-gate 		/* Feed packet back in for router operation */
2440Sstevel@tonic-gate 		loopback_ra_enqueue(pi, ra, packetlen);
2450Sstevel@tonic-gate 		return;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2488485SPeter.Memishian@Sun.COM 	/* add options */
2498485SPeter.Memishian@Sun.COM 	packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
2508485SPeter.Memishian@Sun.COM 	pptr = (char *)packet + packetlen;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	if (pi->pi_AdvLinkMTU != 0) {
2530Sstevel@tonic-gate 		struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		mo->nd_opt_mtu_type = ND_OPT_MTU;
2560Sstevel@tonic-gate 		mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8;
2570Sstevel@tonic-gate 		mo->nd_opt_mtu_reserved = 0;
2580Sstevel@tonic-gate 		mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		packetlen += sizeof (struct nd_opt_mtu);
2610Sstevel@tonic-gate 		pptr += sizeof (struct nd_opt_mtu);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (no_prefixes) {
2650Sstevel@tonic-gate 		if (debug & D_PKTOUT) {
2660Sstevel@tonic-gate 			print_route_adv("Sending advert to ", pi,
2670Sstevel@tonic-gate 			    ra, packetlen, sin6);
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		sendpacket(sin6, pi->pi_sock, packetlen, 0);
2700Sstevel@tonic-gate 		/* Feed packet back in for router operation */
2710Sstevel@tonic-gate 		loopback_ra_enqueue(pi, ra, packetlen);
2720Sstevel@tonic-gate 		return;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	po = (struct nd_opt_prefix_info *)pptr;
2760Sstevel@tonic-gate 	for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
2770Sstevel@tonic-gate 	    adv_pr = adv_pr->adv_pr_next) {
2780Sstevel@tonic-gate 		if (!adv_pr->adv_pr_AdvOnLinkFlag &&
2790Sstevel@tonic-gate 		    !adv_pr->adv_pr_AdvAutonomousFlag) {
2800Sstevel@tonic-gate 			continue;
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		/*
2840Sstevel@tonic-gate 		 * If the prefix doesn't fit in packet send
2850Sstevel@tonic-gate 		 * what we have so far and start with new packet.
2860Sstevel@tonic-gate 		 */
2870Sstevel@tonic-gate 		if (packetlen + sizeof (*po) >
2880Sstevel@tonic-gate 		    pi->pi_LinkMTU - sizeof (struct ip6_hdr)) {
2890Sstevel@tonic-gate 			if (debug & D_PKTOUT) {
2900Sstevel@tonic-gate 				print_route_adv("Sending advert "
2910Sstevel@tonic-gate 				    "(FRAG) to ",
2920Sstevel@tonic-gate 				    pi, ra, packetlen, sin6);
2930Sstevel@tonic-gate 			}
2940Sstevel@tonic-gate 			sendpacket(sin6, pi->pi_sock, packetlen, 0);
2950Sstevel@tonic-gate 			/* Feed packet back in for router operation */
2960Sstevel@tonic-gate 			loopback_ra_enqueue(pi, ra, packetlen);
2970Sstevel@tonic-gate 			packetlen = sizeof (*ra);
2980Sstevel@tonic-gate 			pptr = (char *)packet + sizeof (*ra);
2990Sstevel@tonic-gate 			po = (struct nd_opt_prefix_info *)pptr;
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 		po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
3020Sstevel@tonic-gate 		po->nd_opt_pi_len = sizeof (*po)/8;
3030Sstevel@tonic-gate 		po->nd_opt_pi_flags_reserved = 0;
3040Sstevel@tonic-gate 		if (adv_pr->adv_pr_AdvOnLinkFlag) {
3050Sstevel@tonic-gate 			po->nd_opt_pi_flags_reserved |=
3060Sstevel@tonic-gate 			    ND_OPT_PI_FLAG_ONLINK;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 		if (adv_pr->adv_pr_AdvAutonomousFlag) {
3090Sstevel@tonic-gate 			po->nd_opt_pi_flags_reserved |=
3100Sstevel@tonic-gate 			    ND_OPT_PI_FLAG_AUTO;
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 		po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len;
3130Sstevel@tonic-gate 		/*
3140Sstevel@tonic-gate 		 * If both Adv*Expiration and Adv*Lifetime are
3150Sstevel@tonic-gate 		 * set we prefer the former and make the lifetime
3160Sstevel@tonic-gate 		 * decrement in real time.
3170Sstevel@tonic-gate 		 */
3180Sstevel@tonic-gate 		if (adv_pr->adv_pr_AdvValidRealTime) {
3190Sstevel@tonic-gate 			po->nd_opt_pi_valid_time =
3200Sstevel@tonic-gate 			    htonl(adv_pr->adv_pr_AdvValidExpiration);
3210Sstevel@tonic-gate 		} else {
3220Sstevel@tonic-gate 			po->nd_opt_pi_valid_time =
3230Sstevel@tonic-gate 			    htonl(adv_pr->adv_pr_AdvValidLifetime);
3240Sstevel@tonic-gate 		}
3250Sstevel@tonic-gate 		if (adv_pr->adv_pr_AdvPreferredRealTime) {
3260Sstevel@tonic-gate 			po->nd_opt_pi_preferred_time =
3270Sstevel@tonic-gate 			    htonl(adv_pr->adv_pr_AdvPreferredExpiration);
3280Sstevel@tonic-gate 		} else {
3290Sstevel@tonic-gate 			po->nd_opt_pi_preferred_time =
3300Sstevel@tonic-gate 			    htonl(adv_pr->adv_pr_AdvPreferredLifetime);
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 		po->nd_opt_pi_reserved2 = htonl(0);
3330Sstevel@tonic-gate 		po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		po++;
3360Sstevel@tonic-gate 		packetlen += sizeof (*po);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 	if (debug & D_PKTOUT) {
3390Sstevel@tonic-gate 		print_route_adv("Sending advert to ", pi,
3400Sstevel@tonic-gate 		    ra, packetlen, sin6);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	sendpacket(sin6, pi->pi_sock, packetlen, 0);
3430Sstevel@tonic-gate 	/* Feed packet back in for router operation */
3440Sstevel@tonic-gate 	loopback_ra_enqueue(pi, ra, packetlen);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /* Poll support */
3480Sstevel@tonic-gate static int		pollfd_num = 0;	/* Allocated and initialized */
3490Sstevel@tonic-gate static struct pollfd	*pollfds = NULL;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * Add fd to the set being polled. Returns 0 if ok; -1 if failed.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate int
poll_add(int fd)3550Sstevel@tonic-gate poll_add(int fd)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	int i;
3580Sstevel@tonic-gate 	int new_num;
3590Sstevel@tonic-gate 	struct pollfd *newfds;
3603284Sapersson 
3610Sstevel@tonic-gate 	/* Check if already present */
3620Sstevel@tonic-gate 	for (i = 0; i < pollfd_num; i++) {
3630Sstevel@tonic-gate 		if (pollfds[i].fd == fd)
3640Sstevel@tonic-gate 			return (0);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 	/* Check for empty spot already present */
3670Sstevel@tonic-gate 	for (i = 0; i < pollfd_num; i++) {
3680Sstevel@tonic-gate 		if (pollfds[i].fd == -1) {
3690Sstevel@tonic-gate 			pollfds[i].fd = fd;
3700Sstevel@tonic-gate 			return (0);
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/* Allocate space for 32 more fds and initialize to -1 */
3750Sstevel@tonic-gate 	new_num = pollfd_num + 32;
3760Sstevel@tonic-gate 	newfds = realloc(pollfds, new_num * sizeof (struct pollfd));
3770Sstevel@tonic-gate 	if (newfds == NULL) {
37812016SGirish.Moodalbail@Sun.COM 		logperror("realloc");
3790Sstevel@tonic-gate 		return (-1);
3800Sstevel@tonic-gate 	}
3813284Sapersson 
3823284Sapersson 	newfds[pollfd_num].fd = fd;
3833284Sapersson 	newfds[pollfd_num++].events = POLLIN;
3843284Sapersson 
3850Sstevel@tonic-gate 	for (i = pollfd_num; i < new_num; i++) {
3860Sstevel@tonic-gate 		newfds[i].fd = -1;
3870Sstevel@tonic-gate 		newfds[i].events = POLLIN;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	pollfd_num = new_num;
3900Sstevel@tonic-gate 	pollfds = newfds;
3913284Sapersson 	return (0);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * Remove fd from the set being polled. Returns 0 if ok; -1 if failed.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate int
poll_remove(int fd)3980Sstevel@tonic-gate poll_remove(int fd)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	int i;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/* Check if already present */
4030Sstevel@tonic-gate 	for (i = 0; i < pollfd_num; i++) {
4040Sstevel@tonic-gate 		if (pollfds[i].fd == fd) {
4050Sstevel@tonic-gate 			pollfds[i].fd = -1;
4060Sstevel@tonic-gate 			return (0);
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	return (-1);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * Extract information about the ifname (either a physical interface and
4140Sstevel@tonic-gate  * the ":0" logical interface or just a logical interface).
4150Sstevel@tonic-gate  * If the interface (still) exists in kernel set pr_in_use
4160Sstevel@tonic-gate  * for caller to be able to detect interfaces that are removed.
4170Sstevel@tonic-gate  * Starts sending advertisements/solicitations when new physical interfaces
4180Sstevel@tonic-gate  * are detected.
4190Sstevel@tonic-gate  */
4200Sstevel@tonic-gate static void
if_process(int s,char * ifname,boolean_t first)4210Sstevel@tonic-gate if_process(int s, char *ifname, boolean_t first)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	struct lifreq lifr;
4240Sstevel@tonic-gate 	struct phyint *pi;
4250Sstevel@tonic-gate 	struct prefix *pr;
4260Sstevel@tonic-gate 	char *cp;
4270Sstevel@tonic-gate 	char phyintname[LIFNAMSIZ + 1];
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (debug & D_IFSCAN)
4300Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "if_process(%s)\n", ifname);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
4330Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
4340Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
4350Sstevel@tonic-gate 		if (errno == ENXIO) {
4360Sstevel@tonic-gate 			/*
4370Sstevel@tonic-gate 			 * Interface has disappeared
4380Sstevel@tonic-gate 			 */
4390Sstevel@tonic-gate 			return;
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 		logperror("if_process: ioctl (get interface flags)");
4420Sstevel@tonic-gate 		return;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
44611076SCathy.Zhou@Sun.COM 	 * Ignore loopback, point-to-multipoint and VRRP interfaces.
44711076SCathy.Zhou@Sun.COM 	 * The IP addresses over VRRP interfaces cannot be auto-configured.
4480Sstevel@tonic-gate 	 * Point-to-point interfaces always have IFF_MULTICAST set.
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	if (!(lifr.lifr_flags & IFF_MULTICAST) ||
45111076SCathy.Zhou@Sun.COM 	    (lifr.lifr_flags & (IFF_LOOPBACK|IFF_VRRP))) {
4520Sstevel@tonic-gate 		return;
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (!(lifr.lifr_flags & IFF_IPV6))
4560Sstevel@tonic-gate 		return;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	(void) strncpy(phyintname, ifname, sizeof (phyintname));
4590Sstevel@tonic-gate 	phyintname[sizeof (phyintname) - 1] = '\0';
4600Sstevel@tonic-gate 	if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) {
4610Sstevel@tonic-gate 		*cp = '\0';
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	pi = phyint_lookup(phyintname);
4650Sstevel@tonic-gate 	if (pi == NULL) {
4660Sstevel@tonic-gate 		pi = phyint_create(phyintname);
4670Sstevel@tonic-gate 		if (pi == NULL) {
4680Sstevel@tonic-gate 			logmsg(LOG_ERR, "if_process: out of memory\n");
4690Sstevel@tonic-gate 			return;
4700Sstevel@tonic-gate 		}
47112016SGirish.Moodalbail@Sun.COM 		/*
47212016SGirish.Moodalbail@Sun.COM 		 * if in.ndpd is restarted, check with ipmgmtd if there is any
47312016SGirish.Moodalbail@Sun.COM 		 * interface id to be configured for this interface.
47412016SGirish.Moodalbail@Sun.COM 		 */
47512016SGirish.Moodalbail@Sun.COM 		if (first) {
47612016SGirish.Moodalbail@Sun.COM 			if (phyint_check_ipadm_intfid(pi) == -1)
47712016SGirish.Moodalbail@Sun.COM 				logmsg(LOG_ERR, "Could not get ipadm info\n");
47812016SGirish.Moodalbail@Sun.COM 		}
47912016SGirish.Moodalbail@Sun.COM 	} else {
48012016SGirish.Moodalbail@Sun.COM 		/*
48112016SGirish.Moodalbail@Sun.COM 		 * if the phyint already exists, synchronize it with
48212016SGirish.Moodalbail@Sun.COM 		 * the kernel state. For a newly created phyint, phyint_create
48312016SGirish.Moodalbail@Sun.COM 		 * calls phyint_init_from_k().
48412016SGirish.Moodalbail@Sun.COM 		 */
48512016SGirish.Moodalbail@Sun.COM 		(void) phyint_init_from_k(pi);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) {
4880Sstevel@tonic-gate 		/* Interface is not yet present */
4890Sstevel@tonic-gate 		if (debug & D_PHYINT) {
4900Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "if_process: interface not yet "
4910Sstevel@tonic-gate 			    "present %s\n", pi->pi_name);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		return;
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (pi->pi_sock != -1) {
4970Sstevel@tonic-gate 		if (poll_add(pi->pi_sock) == -1) {
4980Sstevel@tonic-gate 			/*
4990Sstevel@tonic-gate 			 * reset state.
5000Sstevel@tonic-gate 			 */
5010Sstevel@tonic-gate 			phyint_cleanup(pi);
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	/*
5060Sstevel@tonic-gate 	 * Check if IFF_ROUTER has been turned off in kernel in which
5070Sstevel@tonic-gate 	 * case we have to turn off AdvSendAdvertisements.
5080Sstevel@tonic-gate 	 * The kernel will automatically turn off IFF_ROUTER if
5090Sstevel@tonic-gate 	 * ip6_forwarding is turned off.
5100Sstevel@tonic-gate 	 * Note that we do not switch back should IFF_ROUTER be turned on.
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	if (!first &&
5130Sstevel@tonic-gate 	    pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) {
5140Sstevel@tonic-gate 		logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name);
5150Sstevel@tonic-gate 		check_to_advertise(pi, START_FINAL_ADV);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		pi->pi_AdvSendAdvertisements = 0;
5180Sstevel@tonic-gate 		pi->pi_sol_state = NO_SOLICIT;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * Send advertisments and solicitation only if the interface is
5230Sstevel@tonic-gate 	 * present in the kernel.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	if (pi->pi_kernel_state & PI_PRESENT) {
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		if (pi->pi_AdvSendAdvertisements) {
5280Sstevel@tonic-gate 			if (pi->pi_adv_state == NO_ADV)
5290Sstevel@tonic-gate 				check_to_advertise(pi, START_INIT_ADV);
5300Sstevel@tonic-gate 		} else {
5310Sstevel@tonic-gate 			if (pi->pi_sol_state == NO_SOLICIT)
5320Sstevel@tonic-gate 				check_to_solicit(pi, START_INIT_SOLICIT);
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * Track static kernel prefixes to prevent in.ndpd from clobbering
5380Sstevel@tonic-gate 	 * them by creating a struct prefix for each prefix detected in the
5390Sstevel@tonic-gate 	 * kernel.
5400Sstevel@tonic-gate 	 */
5410Sstevel@tonic-gate 	pr = prefix_lookup_name(pi, ifname);
5420Sstevel@tonic-gate 	if (pr == NULL) {
5430Sstevel@tonic-gate 		pr = prefix_create_name(pi, ifname);
5440Sstevel@tonic-gate 		if (pr == NULL) {
5450Sstevel@tonic-gate 			logmsg(LOG_ERR, "if_process: out of memory\n");
5460Sstevel@tonic-gate 			return;
5470Sstevel@tonic-gate 		}
5480Sstevel@tonic-gate 		if (prefix_init_from_k(pr) == -1) {
5490Sstevel@tonic-gate 			prefix_delete(pr);
5500Sstevel@tonic-gate 			return;
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 	/* Detect prefixes which are removed */
5540Sstevel@tonic-gate 	if (pr->pr_kernel_state != 0)
5550Sstevel@tonic-gate 		pr->pr_in_use = _B_TRUE;
5562546Scarlsonj 
5572546Scarlsonj 	if ((lifr.lifr_flags & IFF_DUPLICATE) &&
5583431Scarlsonj 	    !(lifr.lifr_flags & IFF_DHCPRUNNING) &&
5592546Scarlsonj 	    (pr->pr_flags & IFF_TEMPORARY)) {
5602546Scarlsonj 		in6_addr_t *token;
5612546Scarlsonj 		int i;
5622546Scarlsonj 		char abuf[INET6_ADDRSTRLEN];
5632546Scarlsonj 
5642546Scarlsonj 		if (++pr->pr_attempts >= MAX_DAD_FAILURES) {
5652546Scarlsonj 			logmsg(LOG_ERR, "%s: token %s is duplicate after %d "
5662546Scarlsonj 			    "attempts; disabling temporary addresses on %s",
5672546Scarlsonj 			    pr->pr_name, inet_ntop(AF_INET6,
5682546Scarlsonj 			    (void *)&pi->pi_tmp_token, abuf, sizeof (abuf)),
5692546Scarlsonj 			    pr->pr_attempts, pi->pi_name);
5702546Scarlsonj 			pi->pi_TmpAddrsEnabled = 0;
5712546Scarlsonj 			tmptoken_delete(pi);
5722546Scarlsonj 			prefix_delete(pr);
5732546Scarlsonj 			return;
5742546Scarlsonj 		}
5752546Scarlsonj 		logmsg(LOG_WARNING, "%s: token %s is duplicate; trying again",
5762546Scarlsonj 		    pr->pr_name, inet_ntop(AF_INET6, (void *)&pi->pi_tmp_token,
5772546Scarlsonj 		    abuf, sizeof (abuf)));
5782546Scarlsonj 		if (!tmptoken_create(pi)) {
5792546Scarlsonj 			prefix_delete(pr);
5802546Scarlsonj 			return;
5812546Scarlsonj 		}
5822546Scarlsonj 		token = &pi->pi_tmp_token;
5832546Scarlsonj 		for (i = 0; i < 16; i++) {
5842546Scarlsonj 			/*
5852546Scarlsonj 			 * prefix_create ensures that pr_prefix has all-zero
5862546Scarlsonj 			 * bits after prefixlen.
5872546Scarlsonj 			 */
5882546Scarlsonj 			pr->pr_address.s6_addr[i] = pr->pr_prefix.s6_addr[i] |
5892546Scarlsonj 			    token->s6_addr[i];
5902546Scarlsonj 		}
5912546Scarlsonj 		if (prefix_lookup_addr_match(pr) != NULL) {
5922546Scarlsonj 			prefix_delete(pr);
5932546Scarlsonj 			return;
5942546Scarlsonj 		}
5952546Scarlsonj 		pr->pr_CreateTime = getcurrenttime() / MILLISEC;
5962546Scarlsonj 		/*
5972546Scarlsonj 		 * We've got a new token.  Clearing PR_AUTO causes
5982546Scarlsonj 		 * prefix_update_k to bring the interface up and set the
5992546Scarlsonj 		 * address.
6002546Scarlsonj 		 */
6012546Scarlsonj 		pr->pr_kernel_state &= ~PR_AUTO;
6022546Scarlsonj 		prefix_update_k(pr);
6032546Scarlsonj 	}
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate static int ifsock = -1;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate  * Scan all interfaces to detect changes as well as new and deleted intefaces
6100Sstevel@tonic-gate  * 'first' is set for the initial call only. Do not effect anything.
6110Sstevel@tonic-gate  */
6120Sstevel@tonic-gate static void
initifs(boolean_t first)6130Sstevel@tonic-gate initifs(boolean_t first)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	char *buf;
6160Sstevel@tonic-gate 	int bufsize;
6170Sstevel@tonic-gate 	int numifs;
6180Sstevel@tonic-gate 	int n;
6190Sstevel@tonic-gate 	struct lifnum lifn;
6200Sstevel@tonic-gate 	struct lifconf lifc;
6210Sstevel@tonic-gate 	struct lifreq *lifr;
6220Sstevel@tonic-gate 	struct phyint *pi;
6230Sstevel@tonic-gate 	struct phyint *next_pi;
6240Sstevel@tonic-gate 	struct prefix *pr;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if (debug & D_IFSCAN)
6270Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "Reading interface configuration\n");
6280Sstevel@tonic-gate 	if (ifsock < 0) {
6290Sstevel@tonic-gate 		ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
6300Sstevel@tonic-gate 		if (ifsock < 0) {
6310Sstevel@tonic-gate 			logperror("initifs: socket");
6320Sstevel@tonic-gate 			return;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 	lifn.lifn_family = AF_INET6;
6360Sstevel@tonic-gate 	lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
6370Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) {
6380Sstevel@tonic-gate 		logperror("initifs: ioctl (get interface numbers)");
6390Sstevel@tonic-gate 		return;
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 	numifs = lifn.lifn_count;
6420Sstevel@tonic-gate 	bufsize = numifs * sizeof (struct lifreq);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	buf = (char *)malloc(bufsize);
6450Sstevel@tonic-gate 	if (buf == NULL) {
6460Sstevel@tonic-gate 		logmsg(LOG_ERR, "initifs: out of memory\n");
6470Sstevel@tonic-gate 		return;
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/*
6510Sstevel@tonic-gate 	 * Mark the interfaces so that we can find phyints and prefixes
6520Sstevel@tonic-gate 	 * which have disappeared from the kernel.
6530Sstevel@tonic-gate 	 * if_process will set pr_in_use when it finds the interface
6540Sstevel@tonic-gate 	 * in the kernel.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
6570Sstevel@tonic-gate 		/*
6580Sstevel@tonic-gate 		 * Before re-examining the state of the interfaces,
6590Sstevel@tonic-gate 		 * PI_PRESENT should be cleared from pi_kernel_state.
6600Sstevel@tonic-gate 		 */
6610Sstevel@tonic-gate 		pi->pi_kernel_state &= ~PI_PRESENT;
6620Sstevel@tonic-gate 		for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
6630Sstevel@tonic-gate 			pr->pr_in_use = _B_FALSE;
6640Sstevel@tonic-gate 		}
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	lifc.lifc_family = AF_INET6;
6680Sstevel@tonic-gate 	lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
6690Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
6700Sstevel@tonic-gate 	lifc.lifc_buf = buf;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) {
6730Sstevel@tonic-gate 		logperror("initifs: ioctl (get interface configuration)");
6740Sstevel@tonic-gate 		free(buf);
6750Sstevel@tonic-gate 		return;
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	lifr = (struct lifreq *)lifc.lifc_req;
6790Sstevel@tonic-gate 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++)
6800Sstevel@tonic-gate 		if_process(ifsock, lifr->lifr_name, first);
6810Sstevel@tonic-gate 	free(buf);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * Detect phyints that have been removed from the kernel.
6850Sstevel@tonic-gate 	 * Since we can't recreate it here (would require ifconfig plumb
6860Sstevel@tonic-gate 	 * logic) we just terminate use of that phyint.
6870Sstevel@tonic-gate 	 */
6880Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = next_pi) {
6890Sstevel@tonic-gate 		next_pi = pi->pi_next;
6900Sstevel@tonic-gate 		/*
6910Sstevel@tonic-gate 		 * If interface (still) exists in kernel, set
6920Sstevel@tonic-gate 		 * pi_state to indicate that.
6930Sstevel@tonic-gate 		 */
6940Sstevel@tonic-gate 		if (pi->pi_kernel_state & PI_PRESENT) {
6950Sstevel@tonic-gate 			pi->pi_state |= PI_PRESENT;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		check_if_removed(pi);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	if (show_ifs)
7010Sstevel@tonic-gate 		phyint_print_all();
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate  * Router advertisement state machine. Used for everything but timer
7070Sstevel@tonic-gate  * events which use advertise_event directly.
7080Sstevel@tonic-gate  */
7090Sstevel@tonic-gate void
check_to_advertise(struct phyint * pi,enum adv_events event)7100Sstevel@tonic-gate check_to_advertise(struct phyint *pi, enum adv_events event)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	uint_t delay;
7130Sstevel@tonic-gate 	enum adv_states old_state = pi->pi_adv_state;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (debug & D_STATE) {
7160Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n",
7170Sstevel@tonic-gate 		    pi->pi_name, (int)event, (int)old_state);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 	delay = advertise_event(pi, event, 0);
7200Sstevel@tonic-gate 	if (delay != TIMER_INFINITY) {
7210Sstevel@tonic-gate 		/* Make sure the global next event is updated */
7220Sstevel@tonic-gate 		timer_schedule(delay);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	if (debug & D_STATE) {
7260Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n",
7270Sstevel@tonic-gate 		    pi->pi_name, (int)event, (int)old_state,
7280Sstevel@tonic-gate 		    (int)pi->pi_adv_state);
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate  * Router advertisement state machine.
7340Sstevel@tonic-gate  * Return the number of milliseconds until next timeout (TIMER_INFINITY
7350Sstevel@tonic-gate  * if never).
7360Sstevel@tonic-gate  * For the ADV_TIMER event the caller passes in the number of milliseconds
7370Sstevel@tonic-gate  * since the last timer event in the 'elapsed' parameter.
7380Sstevel@tonic-gate  */
7390Sstevel@tonic-gate uint_t
advertise_event(struct phyint * pi,enum adv_events event,uint_t elapsed)7400Sstevel@tonic-gate advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	uint_t delay;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (debug & D_STATE) {
7450Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n",
7460Sstevel@tonic-gate 		    pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 	check_daemonize();
7490Sstevel@tonic-gate 	if (!pi->pi_AdvSendAdvertisements)
7500Sstevel@tonic-gate 		return (TIMER_INFINITY);
7510Sstevel@tonic-gate 	if (pi->pi_flags & IFF_NORTEXCH) {
7520Sstevel@tonic-gate 		if (debug & D_PKTOUT) {
7530Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "Suppress sending RA packet on %s "
7540Sstevel@tonic-gate 			    "(no route exchange on interface)\n",
7550Sstevel@tonic-gate 			    pi->pi_name);
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		return (TIMER_INFINITY);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	switch (event) {
7610Sstevel@tonic-gate 	case ADV_OFF:
7620Sstevel@tonic-gate 		pi->pi_adv_state = NO_ADV;
7630Sstevel@tonic-gate 		return (TIMER_INFINITY);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	case START_INIT_ADV:
7660Sstevel@tonic-gate 		if (pi->pi_adv_state == INIT_ADV)
7670Sstevel@tonic-gate 			return (pi->pi_adv_time_left);
7680Sstevel@tonic-gate 		pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS;
7690Sstevel@tonic-gate 		pi->pi_adv_time_left = 0;
7700Sstevel@tonic-gate 		pi->pi_adv_state = INIT_ADV;
7710Sstevel@tonic-gate 		break;	/* send advertisement */
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	case START_FINAL_ADV:
7740Sstevel@tonic-gate 		if (pi->pi_adv_state == NO_ADV)
7750Sstevel@tonic-gate 			return (TIMER_INFINITY);
7760Sstevel@tonic-gate 		if (pi->pi_adv_state == FINAL_ADV)
7770Sstevel@tonic-gate 			return (pi->pi_adv_time_left);
7780Sstevel@tonic-gate 		pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS;
7790Sstevel@tonic-gate 		pi->pi_adv_time_left = 0;
7800Sstevel@tonic-gate 		pi->pi_adv_state = FINAL_ADV;
7810Sstevel@tonic-gate 		break;	/* send advertisement */
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	case RECEIVED_SOLICIT:
7840Sstevel@tonic-gate 		if (pi->pi_adv_state == NO_ADV)
7850Sstevel@tonic-gate 			return (TIMER_INFINITY);
7860Sstevel@tonic-gate 		if (pi->pi_adv_state == SOLICIT_ADV) {
7870Sstevel@tonic-gate 			if (pi->pi_adv_time_left != 0)
7880Sstevel@tonic-gate 				return (pi->pi_adv_time_left);
7890Sstevel@tonic-gate 			break;
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 		delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME);
7920Sstevel@tonic-gate 		if (delay < pi->pi_adv_time_left)
7930Sstevel@tonic-gate 			pi->pi_adv_time_left = delay;
7940Sstevel@tonic-gate 		if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) {
7950Sstevel@tonic-gate 			/*
7960Sstevel@tonic-gate 			 * Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS
7970Sstevel@tonic-gate 			 * plus random delay) after the previous
7980Sstevel@tonic-gate 			 * advertisement was sent.
7990Sstevel@tonic-gate 			 */
8000Sstevel@tonic-gate 			pi->pi_adv_time_left = delay +
8010Sstevel@tonic-gate 			    ND_MIN_DELAY_BETWEEN_RAS -
8020Sstevel@tonic-gate 			    pi->pi_adv_time_since_sent;
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 		pi->pi_adv_state = SOLICIT_ADV;
8050Sstevel@tonic-gate 		break;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	case ADV_TIMER:
8080Sstevel@tonic-gate 		if (pi->pi_adv_state == NO_ADV)
8090Sstevel@tonic-gate 			return (TIMER_INFINITY);
8100Sstevel@tonic-gate 		/* Decrease time left */
8110Sstevel@tonic-gate 		if (pi->pi_adv_time_left >= elapsed)
8120Sstevel@tonic-gate 			pi->pi_adv_time_left -= elapsed;
8130Sstevel@tonic-gate 		else
8140Sstevel@tonic-gate 			pi->pi_adv_time_left = 0;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		/* Increase time since last advertisement was sent */
8170Sstevel@tonic-gate 		pi->pi_adv_time_since_sent += elapsed;
8180Sstevel@tonic-gate 		break;
8190Sstevel@tonic-gate 	default:
8200Sstevel@tonic-gate 		logmsg(LOG_ERR, "advertise_event: Unknown event %d\n",
8210Sstevel@tonic-gate 		    (int)event);
8220Sstevel@tonic-gate 		return (TIMER_INFINITY);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if (pi->pi_adv_time_left != 0)
8260Sstevel@tonic-gate 		return (pi->pi_adv_time_left);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/* Send advertisement and calculate next time to send */
8290Sstevel@tonic-gate 	if (pi->pi_adv_state == FINAL_ADV) {
8300Sstevel@tonic-gate 		/* Omit the prefixes */
8310Sstevel@tonic-gate 		advertise(&v6allnodes, pi, _B_TRUE);
8320Sstevel@tonic-gate 	} else {
8330Sstevel@tonic-gate 		advertise(&v6allnodes, pi, _B_FALSE);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	pi->pi_adv_time_since_sent = 0;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	switch (pi->pi_adv_state) {
8380Sstevel@tonic-gate 	case SOLICIT_ADV:
8390Sstevel@tonic-gate 		/*
8400Sstevel@tonic-gate 		 * The solicited advertisement has been sent.
8410Sstevel@tonic-gate 		 * Revert to periodic advertisements.
8420Sstevel@tonic-gate 		 */
8430Sstevel@tonic-gate 		pi->pi_adv_state = REG_ADV;
8440Sstevel@tonic-gate 		/* FALLTHRU */
8450Sstevel@tonic-gate 	case REG_ADV:
8460Sstevel@tonic-gate 		pi->pi_adv_time_left =
8470Sstevel@tonic-gate 		    GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
8480Sstevel@tonic-gate 		    1000 * pi->pi_MaxRtrAdvInterval);
8490Sstevel@tonic-gate 		break;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	case INIT_ADV:
8520Sstevel@tonic-gate 		if (--pi->pi_adv_count > 0) {
8530Sstevel@tonic-gate 			delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
8540Sstevel@tonic-gate 			    1000 * pi->pi_MaxRtrAdvInterval);
8550Sstevel@tonic-gate 			if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL)
8560Sstevel@tonic-gate 				delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
8570Sstevel@tonic-gate 			pi->pi_adv_time_left = delay;
8580Sstevel@tonic-gate 		} else {
8590Sstevel@tonic-gate 			pi->pi_adv_time_left =
8600Sstevel@tonic-gate 			    GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
8610Sstevel@tonic-gate 			    1000 * pi->pi_MaxRtrAdvInterval);
8620Sstevel@tonic-gate 			pi->pi_adv_state = REG_ADV;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 		break;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	case FINAL_ADV:
8670Sstevel@tonic-gate 		if (--pi->pi_adv_count > 0) {
8680Sstevel@tonic-gate 			pi->pi_adv_time_left =
8690Sstevel@tonic-gate 			    ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
8700Sstevel@tonic-gate 		} else {
8710Sstevel@tonic-gate 			pi->pi_adv_state = NO_ADV;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		break;
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 	if (pi->pi_adv_state != NO_ADV)
8760Sstevel@tonic-gate 		return (pi->pi_adv_time_left);
8770Sstevel@tonic-gate 	else
8780Sstevel@tonic-gate 		return (TIMER_INFINITY);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate /*
8820Sstevel@tonic-gate  * Router solicitation state machine. Used for everything but timer
8830Sstevel@tonic-gate  * events which use solicit_event directly.
8840Sstevel@tonic-gate  */
8850Sstevel@tonic-gate void
check_to_solicit(struct phyint * pi,enum solicit_events event)8860Sstevel@tonic-gate check_to_solicit(struct phyint *pi, enum solicit_events event)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	uint_t delay;
8890Sstevel@tonic-gate 	enum solicit_states old_state = pi->pi_sol_state;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (debug & D_STATE) {
8920Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n",
8930Sstevel@tonic-gate 		    pi->pi_name, (int)event, (int)old_state);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 	delay = solicit_event(pi, event, 0);
8960Sstevel@tonic-gate 	if (delay != TIMER_INFINITY) {
8970Sstevel@tonic-gate 		/* Make sure the global next event is updated */
8980Sstevel@tonic-gate 		timer_schedule(delay);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	if (debug & D_STATE) {
9020Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
9030Sstevel@tonic-gate 		    pi->pi_name, (int)event, (int)old_state,
9040Sstevel@tonic-gate 		    (int)pi->pi_sol_state);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate static void
daemonize_ndpd(void)9090Sstevel@tonic-gate daemonize_ndpd(void)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 	FILE *pidfp;
9120Sstevel@tonic-gate 	mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
9130Sstevel@tonic-gate 	struct itimerval it;
9140Sstevel@tonic-gate 	boolean_t timerval = _B_TRUE;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/*
9170Sstevel@tonic-gate 	 * Need to get current timer settings so they can be restored
9180Sstevel@tonic-gate 	 * after the fork(), as the it_value and it_interval values for
9190Sstevel@tonic-gate 	 * the ITIMER_REAL timer are reset to 0 in the child process.
9200Sstevel@tonic-gate 	 */
9210Sstevel@tonic-gate 	if (getitimer(ITIMER_REAL, &it) < 0) {
9220Sstevel@tonic-gate 		if (debug & D_TIMER)
9230Sstevel@tonic-gate 			logmsg(LOG_DEBUG,
9240Sstevel@tonic-gate 			    "daemonize_ndpd: failed to get itimerval\n");
9250Sstevel@tonic-gate 		timerval = _B_FALSE;
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/* Daemonize. */
9290Sstevel@tonic-gate 	switch (fork()) {
9300Sstevel@tonic-gate 	case 0:
9310Sstevel@tonic-gate 		/* Child */
9320Sstevel@tonic-gate 		break;
9330Sstevel@tonic-gate 	case -1:
9340Sstevel@tonic-gate 		logperror("fork");
9350Sstevel@tonic-gate 		exit(1);
9360Sstevel@tonic-gate 	default:
9370Sstevel@tonic-gate 		/* Parent */
9380Sstevel@tonic-gate 		_exit(0);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/* Store our process id, blow away any existing file if it exists. */
9420Sstevel@tonic-gate 	if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
9430Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
9440Sstevel@tonic-gate 		    argv0[0], strerror(errno));
9450Sstevel@tonic-gate 	} else {
9460Sstevel@tonic-gate 		(void) fprintf(pidfp, "%ld\n", getpid());
9470Sstevel@tonic-gate 		(void) fclose(pidfp);
9480Sstevel@tonic-gate 		(void) chmod(PATH_PID, pidmode);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	(void) close(0);
9520Sstevel@tonic-gate 	(void) close(1);
9530Sstevel@tonic-gate 	(void) close(2);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	(void) chdir("/");
9560Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR);
9570Sstevel@tonic-gate 	(void) dup2(0, 1);
9580Sstevel@tonic-gate 	(void) dup2(0, 2);
9590Sstevel@tonic-gate 	(void) setsid();
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	already_daemonized = _B_TRUE;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * Restore timer values, if we were able to save them; if not,
9650Sstevel@tonic-gate 	 * check and set the right value by calling run_timeouts().
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	if (timerval) {
9680Sstevel@tonic-gate 		if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
9690Sstevel@tonic-gate 			logperror("daemonize_ndpd: setitimer");
9700Sstevel@tonic-gate 			exit(2);
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 	} else {
9730Sstevel@tonic-gate 		run_timeouts();
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * Check to see if the time is right to daemonize.  The right time is when:
9790Sstevel@tonic-gate  *
9800Sstevel@tonic-gate  * 1.  We haven't already daemonized.
9810Sstevel@tonic-gate  * 2.  We are not in debug mode.
9820Sstevel@tonic-gate  * 3.  All interfaces are marked IFF_NOXMIT.
9830Sstevel@tonic-gate  * 4.  All non-router interfaces have their prefixes set up and we're
9840Sstevel@tonic-gate  *     done sending router solicitations on those interfaces without
9850Sstevel@tonic-gate  *     prefixes.
9860Sstevel@tonic-gate  */
9870Sstevel@tonic-gate static void
check_daemonize(void)9880Sstevel@tonic-gate check_daemonize(void)
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	struct phyint		*pi;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	if (already_daemonized || debug != 0)
9930Sstevel@tonic-gate 		return;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
9960Sstevel@tonic-gate 		if (!(pi->pi_flags & IFF_NOXMIT))
9970Sstevel@tonic-gate 			break;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/*
10010Sstevel@tonic-gate 	 * If we can't transmit on any of the interfaces there is no reason
10020Sstevel@tonic-gate 	 * to hold up progress.
10030Sstevel@tonic-gate 	 */
10040Sstevel@tonic-gate 	if (pi == NULL) {
10050Sstevel@tonic-gate 		daemonize_ndpd();
10060Sstevel@tonic-gate 		return;
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/* Check all interfaces.  If any are still soliciting, just return. */
10100Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
10110Sstevel@tonic-gate 		if (pi->pi_AdvSendAdvertisements ||
10120Sstevel@tonic-gate 		    !(pi->pi_kernel_state & PI_PRESENT))
10130Sstevel@tonic-gate 			continue;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 		if (pi->pi_sol_state == INIT_SOLICIT)
10160Sstevel@tonic-gate 			return;
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	daemonize_ndpd();
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate /*
10230Sstevel@tonic-gate  * Router solicitation state machine.
10240Sstevel@tonic-gate  * Return the number of milliseconds until next timeout (TIMER_INFINITY
10250Sstevel@tonic-gate  * if never).
10260Sstevel@tonic-gate  * For the SOL_TIMER event the caller passes in the number of milliseconds
10270Sstevel@tonic-gate  * since the last timer event in the 'elapsed' parameter.
10280Sstevel@tonic-gate  */
10290Sstevel@tonic-gate uint_t
solicit_event(struct phyint * pi,enum solicit_events event,uint_t elapsed)10300Sstevel@tonic-gate solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate 	if (debug & D_STATE) {
10330Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n",
10340Sstevel@tonic-gate 		    pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state);
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (pi->pi_AdvSendAdvertisements)
10380Sstevel@tonic-gate 		return (TIMER_INFINITY);
10390Sstevel@tonic-gate 	if (pi->pi_flags & IFF_NORTEXCH) {
10400Sstevel@tonic-gate 		if (debug & D_PKTOUT) {
10410Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "Suppress sending RS packet on %s "
10420Sstevel@tonic-gate 			    "(no route exchange on interface)\n",
10430Sstevel@tonic-gate 			    pi->pi_name);
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 		return (TIMER_INFINITY);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	switch (event) {
10490Sstevel@tonic-gate 	case SOLICIT_OFF:
10500Sstevel@tonic-gate 		pi->pi_sol_state = NO_SOLICIT;
10510Sstevel@tonic-gate 		check_daemonize();
10520Sstevel@tonic-gate 		return (TIMER_INFINITY);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	case SOLICIT_DONE:
10550Sstevel@tonic-gate 		pi->pi_sol_state = DONE_SOLICIT;
10560Sstevel@tonic-gate 		check_daemonize();
10570Sstevel@tonic-gate 		return (TIMER_INFINITY);
10580Sstevel@tonic-gate 
10593431Scarlsonj 	case RESTART_INIT_SOLICIT:
10603431Scarlsonj 		/*
10613431Scarlsonj 		 * This event allows us to start solicitation over again
10623431Scarlsonj 		 * without losing the RA flags.  We start solicitation over
10633431Scarlsonj 		 * when we are missing an interface prefix for a newly-
10643431Scarlsonj 		 * encountered DHCP interface.
10653431Scarlsonj 		 */
10663431Scarlsonj 		if (pi->pi_sol_state == INIT_SOLICIT)
10673431Scarlsonj 			return (pi->pi_sol_time_left);
10683431Scarlsonj 		pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
10693431Scarlsonj 		pi->pi_sol_time_left =
10703431Scarlsonj 		    GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
10713431Scarlsonj 		pi->pi_sol_state = INIT_SOLICIT;
10723431Scarlsonj 		break;
10733431Scarlsonj 
10740Sstevel@tonic-gate 	case START_INIT_SOLICIT:
10750Sstevel@tonic-gate 		if (pi->pi_sol_state == INIT_SOLICIT)
10760Sstevel@tonic-gate 			return (pi->pi_sol_time_left);
10773431Scarlsonj 		pi->pi_ra_flags = 0;
10780Sstevel@tonic-gate 		pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
10790Sstevel@tonic-gate 		pi->pi_sol_time_left =
10800Sstevel@tonic-gate 		    GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
10810Sstevel@tonic-gate 		pi->pi_sol_state = INIT_SOLICIT;
10820Sstevel@tonic-gate 		break;
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	case SOL_TIMER:
10850Sstevel@tonic-gate 		if (pi->pi_sol_state == NO_SOLICIT)
10860Sstevel@tonic-gate 			return (TIMER_INFINITY);
10870Sstevel@tonic-gate 		/* Decrease time left */
10880Sstevel@tonic-gate 		if (pi->pi_sol_time_left >= elapsed)
10890Sstevel@tonic-gate 			pi->pi_sol_time_left -= elapsed;
10900Sstevel@tonic-gate 		else
10910Sstevel@tonic-gate 			pi->pi_sol_time_left = 0;
10920Sstevel@tonic-gate 		break;
10930Sstevel@tonic-gate 	default:
10940Sstevel@tonic-gate 		logmsg(LOG_ERR, "solicit_event: Unknown event %d\n",
10950Sstevel@tonic-gate 		    (int)event);
10960Sstevel@tonic-gate 		return (TIMER_INFINITY);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	if (pi->pi_sol_time_left != 0)
11000Sstevel@tonic-gate 		return (pi->pi_sol_time_left);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/* Send solicitation and calculate next time */
11030Sstevel@tonic-gate 	switch (pi->pi_sol_state) {
11040Sstevel@tonic-gate 	case INIT_SOLICIT:
11050Sstevel@tonic-gate 		solicit(&v6allrouters, pi);
11060Sstevel@tonic-gate 		if (--pi->pi_sol_count == 0) {
11074106Scarlsonj 			if (debug & D_STATE) {
11084106Scarlsonj 				logmsg(LOG_DEBUG, "solicit_event: no routers "
11094106Scarlsonj 				    "found on %s; assuming default flags\n",
11104106Scarlsonj 				    pi->pi_name);
11114106Scarlsonj 			}
111212016SGirish.Moodalbail@Sun.COM 			if (pi->pi_autoconf && pi->pi_StatefulAddrConf) {
11133431Scarlsonj 				pi->pi_ra_flags |= ND_RA_FLAG_MANAGED |
11143431Scarlsonj 				    ND_RA_FLAG_OTHER;
11153431Scarlsonj 				start_dhcp(pi);
11163431Scarlsonj 			}
11170Sstevel@tonic-gate 			pi->pi_sol_state = DONE_SOLICIT;
11180Sstevel@tonic-gate 			check_daemonize();
11190Sstevel@tonic-gate 			return (TIMER_INFINITY);
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate 		pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL;
11220Sstevel@tonic-gate 		return (pi->pi_sol_time_left);
11230Sstevel@tonic-gate 	case NO_SOLICIT:
11240Sstevel@tonic-gate 	case DONE_SOLICIT:
11250Sstevel@tonic-gate 		return (TIMER_INFINITY);
11260Sstevel@tonic-gate 	default:
11270Sstevel@tonic-gate 		return (pi->pi_sol_time_left);
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate  * Timer mechanism using relative time (in milliseconds) from the
11330Sstevel@tonic-gate  * previous timer event. Timers exceeding TIMER_INFINITY milliseconds
11340Sstevel@tonic-gate  * will fire after TIMER_INFINITY milliseconds.
11350Sstevel@tonic-gate  */
11360Sstevel@tonic-gate static uint_t timer_previous;	/* When last SIGALRM occurred */
11370Sstevel@tonic-gate static uint_t timer_next;	/* Currently scheduled timeout */
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate static void
timer_init(void)11400Sstevel@tonic-gate timer_init(void)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	timer_previous = getcurrenttime();
11430Sstevel@tonic-gate 	timer_next = TIMER_INFINITY;
11440Sstevel@tonic-gate 	run_timeouts();
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * Make sure the next SIGALRM occurs delay milliseconds from the current
11490Sstevel@tonic-gate  * time if not earlier.
11500Sstevel@tonic-gate  * Handles getcurrenttime (32 bit integer holding milliseconds) wraparound
11510Sstevel@tonic-gate  * by treating differences greater than 0x80000000 as negative.
11520Sstevel@tonic-gate  */
11530Sstevel@tonic-gate void
timer_schedule(uint_t delay)11540Sstevel@tonic-gate timer_schedule(uint_t delay)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	uint_t now;
11570Sstevel@tonic-gate 	struct itimerval itimerval;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	now = getcurrenttime();
11600Sstevel@tonic-gate 	if (debug & D_TIMER) {
11610Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n",
11620Sstevel@tonic-gate 		    delay, now, timer_next);
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	/* Will this timer occur before the currently scheduled SIGALRM? */
11650Sstevel@tonic-gate 	if (delay >= timer_next - now) {
11660Sstevel@tonic-gate 		if (debug & D_TIMER) {
11670Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "timer_schedule(%u): no action - "
11680Sstevel@tonic-gate 			    "next in %u ms\n",
11690Sstevel@tonic-gate 			    delay, timer_next - now);
11700Sstevel@tonic-gate 		}
11710Sstevel@tonic-gate 		return;
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 	if (delay == 0) {
11740Sstevel@tonic-gate 		/* Minimum allowed delay */
11750Sstevel@tonic-gate 		delay = 1;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	timer_next = now + delay;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	itimerval.it_value.tv_sec = delay / 1000;
11800Sstevel@tonic-gate 	itimerval.it_value.tv_usec = (delay % 1000) * 1000;
11810Sstevel@tonic-gate 	itimerval.it_interval.tv_sec = 0;
11820Sstevel@tonic-gate 	itimerval.it_interval.tv_usec = 0;
11830Sstevel@tonic-gate 	if (debug & D_TIMER) {
11840Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n",
11850Sstevel@tonic-gate 		    delay,
11860Sstevel@tonic-gate 		    itimerval.it_value.tv_sec, itimerval.it_value.tv_usec);
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 	if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) {
11890Sstevel@tonic-gate 		logperror("timer_schedule: setitimer");
11900Sstevel@tonic-gate 		exit(2);
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate /*
11950Sstevel@tonic-gate  * Conditional running of timer. If more than 'minimal_time' millseconds
11960Sstevel@tonic-gate  * since the timer routines were last run we run them.
11970Sstevel@tonic-gate  * Used when packets arrive.
11980Sstevel@tonic-gate  */
11990Sstevel@tonic-gate static void
conditional_run_timeouts(uint_t minimal_time)12000Sstevel@tonic-gate conditional_run_timeouts(uint_t minimal_time)
12010Sstevel@tonic-gate {
12020Sstevel@tonic-gate 	uint_t now;
12030Sstevel@tonic-gate 	uint_t elapsed;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	now = getcurrenttime();
12060Sstevel@tonic-gate 	elapsed = now - timer_previous;
12070Sstevel@tonic-gate 	if (elapsed > minimal_time) {
12080Sstevel@tonic-gate 		if (debug & D_TIMER) {
12090Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "conditional_run_timeouts: "
12100Sstevel@tonic-gate 			    "elapsed %d\n", elapsed);
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 		run_timeouts();
12130Sstevel@tonic-gate 	}
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate /*
12170Sstevel@tonic-gate  * Timer has fired.
12180Sstevel@tonic-gate  * Determine when the next timer event will occur by asking all
12190Sstevel@tonic-gate  * the timer routines.
12200Sstevel@tonic-gate  * Should not be called from a timer routine but in some cases this is
12210Sstevel@tonic-gate  * done because the code doesn't know that e.g. it was called from
12220Sstevel@tonic-gate  * ifconfig_timer(). In this case the nested run_timeouts will just return but
12230Sstevel@tonic-gate  * the running run_timeouts will ensure to call all the timer functions by
12240Sstevel@tonic-gate  * looping once more.
12250Sstevel@tonic-gate  */
12260Sstevel@tonic-gate static void
run_timeouts(void)12270Sstevel@tonic-gate run_timeouts(void)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	uint_t now;
12300Sstevel@tonic-gate 	uint_t elapsed;
12310Sstevel@tonic-gate 	uint_t next;
12320Sstevel@tonic-gate 	uint_t nexti;
12330Sstevel@tonic-gate 	struct phyint *pi;
12340Sstevel@tonic-gate 	struct phyint *next_pi;
12350Sstevel@tonic-gate 	struct prefix *pr;
12360Sstevel@tonic-gate 	struct prefix *next_pr;
12370Sstevel@tonic-gate 	struct adv_prefix *adv_pr;
12380Sstevel@tonic-gate 	struct adv_prefix *next_adv_pr;
12390Sstevel@tonic-gate 	struct router *dr;
12400Sstevel@tonic-gate 	struct router *next_dr;
12410Sstevel@tonic-gate 	static boolean_t timeout_running;
12420Sstevel@tonic-gate 	static boolean_t do_retry;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (timeout_running) {
12450Sstevel@tonic-gate 		if (debug & D_TIMER)
12460Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "run_timeouts: nested call\n");
12470Sstevel@tonic-gate 		do_retry = _B_TRUE;
12480Sstevel@tonic-gate 		return;
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 	timeout_running = _B_TRUE;
12510Sstevel@tonic-gate retry:
12520Sstevel@tonic-gate 	/* How much time since the last time we were called? */
12530Sstevel@tonic-gate 	now = getcurrenttime();
12540Sstevel@tonic-gate 	elapsed = now - timer_previous;
12550Sstevel@tonic-gate 	timer_previous = now;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	if (debug & D_TIMER)
12580Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	next = TIMER_INFINITY;
12610Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = next_pi) {
12620Sstevel@tonic-gate 		next_pi = pi->pi_next;
12630Sstevel@tonic-gate 		nexti = phyint_timer(pi, elapsed);
12640Sstevel@tonic-gate 		if (nexti != TIMER_INFINITY && nexti < next)
12650Sstevel@tonic-gate 			next = nexti;
12660Sstevel@tonic-gate 		if (debug & D_TIMER) {
12670Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n",
12680Sstevel@tonic-gate 			    pi->pi_name, nexti, next);
12690Sstevel@tonic-gate 		}
12700Sstevel@tonic-gate 		for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
12710Sstevel@tonic-gate 			next_pr = pr->pr_next;
12720Sstevel@tonic-gate 			nexti = prefix_timer(pr, elapsed);
12730Sstevel@tonic-gate 			if (nexti != TIMER_INFINITY && nexti < next)
12740Sstevel@tonic-gate 				next = nexti;
12750Sstevel@tonic-gate 			if (debug & D_TIMER) {
12760Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "run_timeouts (pr %s): "
12770Sstevel@tonic-gate 				    "%d -> %u ms\n", pr->pr_name, nexti, next);
12780Sstevel@tonic-gate 			}
12790Sstevel@tonic-gate 		}
12800Sstevel@tonic-gate 		for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
12810Sstevel@tonic-gate 		    adv_pr = next_adv_pr) {
12820Sstevel@tonic-gate 			next_adv_pr = adv_pr->adv_pr_next;
12830Sstevel@tonic-gate 			nexti = adv_prefix_timer(adv_pr, elapsed);
12840Sstevel@tonic-gate 			if (nexti != TIMER_INFINITY && nexti < next)
12850Sstevel@tonic-gate 				next = nexti;
12860Sstevel@tonic-gate 			if (debug & D_TIMER) {
12870Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "run_timeouts "
12880Sstevel@tonic-gate 				    "(adv pr on %s): %d -> %u ms\n",
12890Sstevel@tonic-gate 				    adv_pr->adv_pr_physical->pi_name,
12900Sstevel@tonic-gate 				    nexti, next);
12910Sstevel@tonic-gate 			}
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 		for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) {
12940Sstevel@tonic-gate 			next_dr = dr->dr_next;
12950Sstevel@tonic-gate 			nexti = router_timer(dr, elapsed);
12960Sstevel@tonic-gate 			if (nexti != TIMER_INFINITY && nexti < next)
12970Sstevel@tonic-gate 				next = nexti;
12980Sstevel@tonic-gate 			if (debug & D_TIMER) {
12990Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "run_timeouts (dr): "
13000Sstevel@tonic-gate 				    "%d -> %u ms\n", nexti, next);
13010Sstevel@tonic-gate 			}
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 		if (pi->pi_TmpAddrsEnabled) {
13040Sstevel@tonic-gate 			nexti = tmptoken_timer(pi, elapsed);
13050Sstevel@tonic-gate 			if (nexti != TIMER_INFINITY && nexti < next)
13060Sstevel@tonic-gate 				next = nexti;
13070Sstevel@tonic-gate 			if (debug & D_TIMER) {
13080Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): "
13090Sstevel@tonic-gate 				    "%d -> %u ms\n", pi->pi_name, nexti, next);
13100Sstevel@tonic-gate 			}
13110Sstevel@tonic-gate 		}
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 	/*
13140Sstevel@tonic-gate 	 * Make sure the timer functions are run at least once
13150Sstevel@tonic-gate 	 * an hour.
13160Sstevel@tonic-gate 	 */
13170Sstevel@tonic-gate 	if (next == TIMER_INFINITY)
13180Sstevel@tonic-gate 		next = 3600 * 1000;	/* 1 hour */
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	if (debug & D_TIMER)
13210Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next);
13220Sstevel@tonic-gate 	timer_schedule(next);
13230Sstevel@tonic-gate 	if (do_retry) {
13240Sstevel@tonic-gate 		if (debug & D_TIMER)
13250Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "run_timeouts: retry\n");
13260Sstevel@tonic-gate 		do_retry = _B_FALSE;
13270Sstevel@tonic-gate 		goto retry;
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 	timeout_running = _B_FALSE;
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate static int eventpipe_read = -1;	/* Used for synchronous signal delivery */
13330Sstevel@tonic-gate static int eventpipe_write = -1;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate /*
13360Sstevel@tonic-gate  * Ensure that signals are processed synchronously with the rest of
13370Sstevel@tonic-gate  * the code by just writing a one character signal number on the pipe.
13380Sstevel@tonic-gate  * The poll loop will pick this up and process the signal event.
13390Sstevel@tonic-gate  */
13400Sstevel@tonic-gate static void
sig_handler(int signo)13410Sstevel@tonic-gate sig_handler(int signo)
13420Sstevel@tonic-gate {
13430Sstevel@tonic-gate 	uchar_t buf = (uchar_t)signo;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (eventpipe_write == -1) {
13460Sstevel@tonic-gate 		logmsg(LOG_ERR, "sig_handler: no pipe\n");
13470Sstevel@tonic-gate 		return;
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 	if (write(eventpipe_write, &buf, sizeof (buf)) < 0)
13500Sstevel@tonic-gate 		logperror("sig_handler: write");
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate  * Pick up a signal "byte" from the pipe and process it.
13550Sstevel@tonic-gate  */
13560Sstevel@tonic-gate static void
in_signal(int fd)13570Sstevel@tonic-gate in_signal(int fd)
13580Sstevel@tonic-gate {
13590Sstevel@tonic-gate 	uchar_t buf;
13600Sstevel@tonic-gate 	struct phyint *pi;
13610Sstevel@tonic-gate 	struct phyint *next_pi;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	switch (read(fd, &buf, sizeof (buf))) {
13640Sstevel@tonic-gate 	case -1:
13650Sstevel@tonic-gate 		logperror("in_signal: read");
13660Sstevel@tonic-gate 		exit(1);
13670Sstevel@tonic-gate 		/* NOTREACHED */
13680Sstevel@tonic-gate 	case 1:
13690Sstevel@tonic-gate 		break;
13700Sstevel@tonic-gate 	case 0:
13710Sstevel@tonic-gate 		logmsg(LOG_ERR, "in_signal: read eof\n");
13720Sstevel@tonic-gate 		exit(1);
13730Sstevel@tonic-gate 		/* NOTREACHED */
13740Sstevel@tonic-gate 	default:
13750Sstevel@tonic-gate 		logmsg(LOG_ERR, "in_signal: read > 1\n");
13760Sstevel@tonic-gate 		exit(1);
13770Sstevel@tonic-gate 	}
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	if (debug & D_TIMER)
13800Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "in_signal() got %d\n", buf);
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	switch (buf) {
13830Sstevel@tonic-gate 	case SIGALRM:
13840Sstevel@tonic-gate 		if (debug & D_TIMER) {
13850Sstevel@tonic-gate 			uint_t now = getcurrenttime();
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n",
13880Sstevel@tonic-gate 			    now - timer_next);
13890Sstevel@tonic-gate 		}
13900Sstevel@tonic-gate 		timer_next = TIMER_INFINITY;
13910Sstevel@tonic-gate 		run_timeouts();
13920Sstevel@tonic-gate 		break;
13930Sstevel@tonic-gate 	case SIGHUP:
13940Sstevel@tonic-gate 		/* Re-read config file by exec'ing ourselves */
13950Sstevel@tonic-gate 		for (pi = phyints; pi != NULL; pi = next_pi) {
13960Sstevel@tonic-gate 			next_pi = pi->pi_next;
13970Sstevel@tonic-gate 			if (pi->pi_AdvSendAdvertisements)
13980Sstevel@tonic-gate 				check_to_advertise(pi, START_FINAL_ADV);
13990Sstevel@tonic-gate 
140012016SGirish.Moodalbail@Sun.COM 			/*
140112016SGirish.Moodalbail@Sun.COM 			 * Remove all the configured addresses.
140212016SGirish.Moodalbail@Sun.COM 			 * Remove the addrobj names created with ipmgmtd.
140312016SGirish.Moodalbail@Sun.COM 			 * Release the dhcpv6 addresses if any.
140412016SGirish.Moodalbail@Sun.COM 			 * Cleanup the phyints.
140512016SGirish.Moodalbail@Sun.COM 			 */
14060Sstevel@tonic-gate 			phyint_delete(pi);
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 		/*
14100Sstevel@tonic-gate 		 * Prevent fd leaks.  Everything gets re-opened at start-up
14110Sstevel@tonic-gate 		 * time.  0, 1, and 2 are closed and re-opened as
14120Sstevel@tonic-gate 		 * /dev/null, so we'll leave those open.
14130Sstevel@tonic-gate 		 */
14140Sstevel@tonic-gate 		closefrom(3);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 		logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
14170Sstevel@tonic-gate 		(void) execv(argv0[0], argv0);
14180Sstevel@tonic-gate 		(void) unlink(PATH_PID);
14190Sstevel@tonic-gate 		_exit(0177);
14200Sstevel@tonic-gate 		/* NOTREACHED */
14210Sstevel@tonic-gate 	case SIGUSR1:
14220Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "Printing configuration:\n");
14230Sstevel@tonic-gate 		phyint_print_all();
14240Sstevel@tonic-gate 		break;
14250Sstevel@tonic-gate 	case SIGINT:
14260Sstevel@tonic-gate 	case SIGTERM:
14270Sstevel@tonic-gate 	case SIGQUIT:
14280Sstevel@tonic-gate 		for (pi = phyints; pi != NULL; pi = next_pi) {
14290Sstevel@tonic-gate 			next_pi = pi->pi_next;
14300Sstevel@tonic-gate 			if (pi->pi_AdvSendAdvertisements)
14310Sstevel@tonic-gate 				check_to_advertise(pi, START_FINAL_ADV);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 			phyint_delete(pi);
14340Sstevel@tonic-gate 		}
14353284Sapersson 		(void) unlink(NDPD_SNMP_SOCKET);
14360Sstevel@tonic-gate 		(void) unlink(PATH_PID);
14370Sstevel@tonic-gate 		exit(0);
14380Sstevel@tonic-gate 		/* NOTREACHED */
14390Sstevel@tonic-gate 	case 255:
14400Sstevel@tonic-gate 		/*
144112016SGirish.Moodalbail@Sun.COM 		 * Special "signal" from loopback_ra_enqueue.
14420Sstevel@tonic-gate 		 * Handle any queued loopback router advertisements.
14430Sstevel@tonic-gate 		 */
14440Sstevel@tonic-gate 		loopback_ra_dequeue();
14450Sstevel@tonic-gate 		break;
14460Sstevel@tonic-gate 	default:
14470Sstevel@tonic-gate 		logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf);
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate /*
14520Sstevel@tonic-gate  * Create pipe for signal delivery and set up signal handlers.
14530Sstevel@tonic-gate  */
14540Sstevel@tonic-gate static void
setup_eventpipe(void)14550Sstevel@tonic-gate setup_eventpipe(void)
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	int fds[2];
14580Sstevel@tonic-gate 	struct sigaction act;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	if ((pipe(fds)) < 0) {
14610Sstevel@tonic-gate 		logperror("setup_eventpipe: pipe");
14620Sstevel@tonic-gate 		exit(1);
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 	eventpipe_read = fds[0];
14650Sstevel@tonic-gate 	eventpipe_write = fds[1];
14660Sstevel@tonic-gate 	if (poll_add(eventpipe_read) == -1) {
14670Sstevel@tonic-gate 		exit(1);
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 	act.sa_handler = sig_handler;
14700Sstevel@tonic-gate 	act.sa_flags = SA_RESTART;
14710Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	(void) sigset(SIGHUP, sig_handler);
14740Sstevel@tonic-gate 	(void) sigset(SIGUSR1, sig_handler);
14750Sstevel@tonic-gate 	(void) sigset(SIGTERM, sig_handler);
14760Sstevel@tonic-gate 	(void) sigset(SIGINT, sig_handler);
14770Sstevel@tonic-gate 	(void) sigset(SIGQUIT, sig_handler);
14780Sstevel@tonic-gate }
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate /*
14810Sstevel@tonic-gate  * Create a routing socket for receiving RTM_IFINFO messages and initialize
14820Sstevel@tonic-gate  * the routing socket message header and as much of the sockaddrs as possible.
14830Sstevel@tonic-gate  */
14840Sstevel@tonic-gate static int
setup_rtsock(void)14850Sstevel@tonic-gate setup_rtsock(void)
14860Sstevel@tonic-gate {
14870Sstevel@tonic-gate 	int s;
14880Sstevel@tonic-gate 	int ret;
14890Sstevel@tonic-gate 	char *cp;
14900Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	s = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
14930Sstevel@tonic-gate 	if (s == -1) {
14940Sstevel@tonic-gate 		logperror("socket(PF_ROUTE)");
14950Sstevel@tonic-gate 		exit(1);
14960Sstevel@tonic-gate 	}
14970Sstevel@tonic-gate 	ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK);
14980Sstevel@tonic-gate 	if (ret < 0) {
14990Sstevel@tonic-gate 		logperror("fcntl(O_NDELAY)");
15000Sstevel@tonic-gate 		exit(1);
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 	if (poll_add(s) == -1) {
15030Sstevel@tonic-gate 		exit(1);
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	/*
15070Sstevel@tonic-gate 	 * Allocate storage for the routing socket message.
15080Sstevel@tonic-gate 	 */
15090Sstevel@tonic-gate 	rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN);
15100Sstevel@tonic-gate 	if (rt_msg == NULL) {
15110Sstevel@tonic-gate 		logperror("malloc");
15120Sstevel@tonic-gate 		exit(1);
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * Initialize the routing socket message by zero-filling it and then
15170Sstevel@tonic-gate 	 * setting the fields where are constant through the lifetime of the
15180Sstevel@tonic-gate 	 * process.
15190Sstevel@tonic-gate 	 */
15200Sstevel@tonic-gate 	bzero(rt_msg, NDP_RTM_MSGLEN);
15210Sstevel@tonic-gate 	rt_msg->rtm_msglen = NDP_RTM_MSGLEN;
15220Sstevel@tonic-gate 	rt_msg->rtm_version = RTM_VERSION;
15230Sstevel@tonic-gate 	rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP;
15240Sstevel@tonic-gate 	rt_msg->rtm_pid = getpid();
15250Sstevel@tonic-gate 	if (rt_msg->rtm_pid < 0) {
15260Sstevel@tonic-gate 		logperror("getpid");
15270Sstevel@tonic-gate 		exit(1);
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	/*
15310Sstevel@tonic-gate 	 * The RTA_DST sockaddr does not change during the lifetime of the
15320Sstevel@tonic-gate 	 * process so it can be completely initialized at this time.
15330Sstevel@tonic-gate 	 */
15340Sstevel@tonic-gate 	cp = (char *)rt_msg + sizeof (struct rt_msghdr);
15350Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)cp;
15360Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
15370Sstevel@tonic-gate 	sin6->sin6_addr = in6addr_any;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/*
15400Sstevel@tonic-gate 	 * Initialize the constant portion of the RTA_GATEWAY sockaddr.
15410Sstevel@tonic-gate 	 */
15420Sstevel@tonic-gate 	cp += sizeof (struct sockaddr_in6);
15430Sstevel@tonic-gate 	rta_gateway = (struct sockaddr_in6 *)cp;
15440Sstevel@tonic-gate 	rta_gateway->sin6_family = AF_INET6;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	/*
15470Sstevel@tonic-gate 	 * The RTA_NETMASK sockaddr does not change during the lifetime of the
15480Sstevel@tonic-gate 	 * process so it can be completely initialized at this time.
15490Sstevel@tonic-gate 	 */
15500Sstevel@tonic-gate 	cp += sizeof (struct sockaddr_in6);
15510Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)cp;
15520Sstevel@tonic-gate 	sin6->sin6_family = AF_INET6;
15530Sstevel@tonic-gate 	sin6->sin6_addr = in6addr_any;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	/*
15560Sstevel@tonic-gate 	 * Initialize the constant portion of the RTA_IFP sockaddr.
15570Sstevel@tonic-gate 	 */
15580Sstevel@tonic-gate 	cp += sizeof (struct sockaddr_in6);
15590Sstevel@tonic-gate 	rta_ifp = (struct sockaddr_dl *)cp;
15600Sstevel@tonic-gate 	rta_ifp->sdl_family = AF_LINK;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	return (s);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate 
15653284Sapersson static int
setup_mibsock(void)15663284Sapersson setup_mibsock(void)
15673284Sapersson {
15683284Sapersson 	int sock;
15693284Sapersson 	int ret;
15703284Sapersson 	int len;
15713284Sapersson 	struct sockaddr_un laddr;
15723284Sapersson 
15733284Sapersson 	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
15743284Sapersson 	if (sock == -1) {
15753284Sapersson 		logperror("setup_mibsock: socket(AF_UNIX)");
15763284Sapersson 		exit(1);
15773284Sapersson 	}
15783284Sapersson 
15793284Sapersson 	bzero(&laddr, sizeof (laddr));
15803284Sapersson 	laddr.sun_family = AF_UNIX;
15813284Sapersson 
15823284Sapersson 	(void) strncpy(laddr.sun_path, NDPD_SNMP_SOCKET,
15833284Sapersson 	    sizeof (laddr.sun_path));
15843284Sapersson 	len = sizeof (struct sockaddr_un);
15853284Sapersson 
15863284Sapersson 	(void) unlink(NDPD_SNMP_SOCKET);
15873284Sapersson 	ret = bind(sock, (struct sockaddr *)&laddr, len);
15883284Sapersson 	if (ret < 0) {
15893284Sapersson 		logperror("setup_mibsock: bind\n");
15903284Sapersson 		exit(1);
15913284Sapersson 	}
15923284Sapersson 
15933284Sapersson 	ret = fcntl(sock, F_SETFL, O_NONBLOCK);
15943284Sapersson 	if (ret < 0) {
15953284Sapersson 		logperror("fcntl(O_NONBLOCK)");
15963284Sapersson 		exit(1);
15973284Sapersson 	}
15983284Sapersson 	if (poll_add(sock) == -1) {
15993284Sapersson 		exit(1);
16003284Sapersson 	}
16013284Sapersson 	return (sock);
16023284Sapersson }
16033284Sapersson 
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate  * Retrieve one routing socket message. If RTM_IFINFO indicates
16060Sstevel@tonic-gate  * new phyint do a full scan of the interfaces. If RTM_IFINFO
16072546Scarlsonj  * indicates an existing phyint, only scan that phyint and associated
16080Sstevel@tonic-gate  * prefixes.
16090Sstevel@tonic-gate  */
16100Sstevel@tonic-gate static void
process_rtsock(int rtsock)16110Sstevel@tonic-gate process_rtsock(int rtsock)
16120Sstevel@tonic-gate {
16130Sstevel@tonic-gate 	int n;
16140Sstevel@tonic-gate #define	MSG_SIZE	2048/8
16150Sstevel@tonic-gate 	int64_t msg[MSG_SIZE];
16160Sstevel@tonic-gate 	struct rt_msghdr *rtm;
16170Sstevel@tonic-gate 	struct if_msghdr *ifm;
16180Sstevel@tonic-gate 	struct phyint *pi;
16190Sstevel@tonic-gate 	struct prefix *pr;
16200Sstevel@tonic-gate 	boolean_t need_initifs = _B_FALSE;
16210Sstevel@tonic-gate 	boolean_t need_ifscan = _B_FALSE;
16220Sstevel@tonic-gate 	int64_t	ifscan_msg[10][MSG_SIZE];
16230Sstevel@tonic-gate 	int ifscan_index = 0;
16240Sstevel@tonic-gate 	int i;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	/* Empty the rtsock and coealesce all the work that we have */
16270Sstevel@tonic-gate 	while (ifscan_index < 10) {
16280Sstevel@tonic-gate 		n = read(rtsock, msg, sizeof (msg));
16290Sstevel@tonic-gate 		if (n <= 0) {
16300Sstevel@tonic-gate 			/* No more messages */
16310Sstevel@tonic-gate 			break;
16320Sstevel@tonic-gate 		}
16330Sstevel@tonic-gate 		rtm = (struct rt_msghdr *)msg;
16340Sstevel@tonic-gate 		if (rtm->rtm_version != RTM_VERSION) {
16350Sstevel@tonic-gate 			logmsg(LOG_ERR,
16360Sstevel@tonic-gate 			    "process_rtsock: version %d not understood\n",
16370Sstevel@tonic-gate 			    rtm->rtm_version);
16380Sstevel@tonic-gate 			return;
16390Sstevel@tonic-gate 		}
16400Sstevel@tonic-gate 		switch (rtm->rtm_type) {
16410Sstevel@tonic-gate 		case RTM_NEWADDR:
16420Sstevel@tonic-gate 		case RTM_DELADDR:
16430Sstevel@tonic-gate 			/*
16440Sstevel@tonic-gate 			 * Some logical interface has changed - have to scan
16450Sstevel@tonic-gate 			 * everything to determine what actually changed.
16460Sstevel@tonic-gate 			 */
16470Sstevel@tonic-gate 			if (debug & D_IFSCAN) {
16480Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "process_rtsock: "
16490Sstevel@tonic-gate 				    "message %d\n", rtm->rtm_type);
16500Sstevel@tonic-gate 			}
16510Sstevel@tonic-gate 			need_initifs = _B_TRUE;
16520Sstevel@tonic-gate 			break;
16530Sstevel@tonic-gate 		case RTM_IFINFO:
16540Sstevel@tonic-gate 			need_ifscan = _B_TRUE;
16550Sstevel@tonic-gate 			(void) memcpy(ifscan_msg[ifscan_index], rtm,
16560Sstevel@tonic-gate 			    sizeof (msg));
16570Sstevel@tonic-gate 			ifscan_index++;
16580Sstevel@tonic-gate 			/* Handled below */
16590Sstevel@tonic-gate 			break;
16600Sstevel@tonic-gate 		default:
16610Sstevel@tonic-gate 			/* Not interesting */
16620Sstevel@tonic-gate 			break;
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 	/*
16660Sstevel@tonic-gate 	 * If we do full scan i.e initifs, we don't need to
16670Sstevel@tonic-gate 	 * scan a particular interface as we should have
16680Sstevel@tonic-gate 	 * done that as part of initifs.
16690Sstevel@tonic-gate 	 */
16700Sstevel@tonic-gate 	if (need_initifs) {
16710Sstevel@tonic-gate 		initifs(_B_FALSE);
16720Sstevel@tonic-gate 		return;
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	if (!need_ifscan)
16760Sstevel@tonic-gate 		return;
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	for (i = 0; i < ifscan_index; i++) {
16790Sstevel@tonic-gate 		ifm = (struct if_msghdr *)ifscan_msg[i];
16800Sstevel@tonic-gate 		if (debug & D_IFSCAN)
16810Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "process_rtsock: index %d\n",
16820Sstevel@tonic-gate 			    ifm->ifm_index);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 		pi = phyint_lookup_on_index(ifm->ifm_index);
16850Sstevel@tonic-gate 		if (pi == NULL) {
16860Sstevel@tonic-gate 			/*
16870Sstevel@tonic-gate 			 * A new physical interface. Do a full scan of the
16880Sstevel@tonic-gate 			 * to catch any new logical interfaces.
16890Sstevel@tonic-gate 			 */
16900Sstevel@tonic-gate 			initifs(_B_FALSE);
16910Sstevel@tonic-gate 			return;
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 
16948485SPeter.Memishian@Sun.COM 		if (ifm->ifm_flags != (uint_t)pi->pi_flags) {
16950Sstevel@tonic-gate 			if (debug & D_IFSCAN) {
16960Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "process_rtsock: clr for "
16978485SPeter.Memishian@Sun.COM 				    "%s old flags 0x%llx new flags 0x%x\n",
16980Sstevel@tonic-gate 				    pi->pi_name, pi->pi_flags, ifm->ifm_flags);
16990Sstevel@tonic-gate 			}
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 		/*
17040Sstevel@tonic-gate 		 * Mark the interfaces so that we can find phyints and prefixes
17050Sstevel@tonic-gate 		 * which have disappeared from the kernel.
17060Sstevel@tonic-gate 		 * if_process will set pr_in_use when it finds the
17070Sstevel@tonic-gate 		 * interface in the kernel.
17080Sstevel@tonic-gate 		 * Before re-examining the state of the interfaces,
17090Sstevel@tonic-gate 		 * PI_PRESENT should be cleared from pi_kernel_state.
17100Sstevel@tonic-gate 		 */
17110Sstevel@tonic-gate 		pi->pi_kernel_state &= ~PI_PRESENT;
17120Sstevel@tonic-gate 		for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
17130Sstevel@tonic-gate 			pr->pr_in_use = _B_FALSE;
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 		if (ifsock < 0) {
17170Sstevel@tonic-gate 			ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
17180Sstevel@tonic-gate 			if (ifsock < 0) {
17190Sstevel@tonic-gate 				logperror("process_rtsock: socket");
17200Sstevel@tonic-gate 				return;
17210Sstevel@tonic-gate 			}
17220Sstevel@tonic-gate 		}
17230Sstevel@tonic-gate 		if_process(ifsock, pi->pi_name, _B_FALSE);
17240Sstevel@tonic-gate 		for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
17250Sstevel@tonic-gate 			if_process(ifsock, pr->pr_name, _B_FALSE);
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 		/*
17280Sstevel@tonic-gate 		 * If interface (still) exists in kernel, set
17290Sstevel@tonic-gate 		 * pi_state to indicate that.
17300Sstevel@tonic-gate 		 */
17310Sstevel@tonic-gate 		if (pi->pi_kernel_state & PI_PRESENT) {
17320Sstevel@tonic-gate 			pi->pi_state |= PI_PRESENT;
17330Sstevel@tonic-gate 		}
17340Sstevel@tonic-gate 		check_if_removed(pi);
17350Sstevel@tonic-gate 		if (show_ifs)
17360Sstevel@tonic-gate 			phyint_print_all();
17370Sstevel@tonic-gate 	}
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
17403284Sapersson static void
process_mibsock(int mibsock)17413284Sapersson process_mibsock(int mibsock)
17423284Sapersson {
17433284Sapersson 	struct phyint *pi;
17443284Sapersson 	socklen_t fromlen;
17453284Sapersson 	struct sockaddr_un from;
17463284Sapersson 	ndpd_info_t ndpd_info;
17473284Sapersson 	ssize_t len;
17483284Sapersson 	int command;
17493284Sapersson 
17503284Sapersson 	fromlen = (socklen_t)sizeof (from);
17513284Sapersson 	len = recvfrom(mibsock, &command, sizeof (int), 0,
17523284Sapersson 	    (struct sockaddr *)&from, &fromlen);
17533284Sapersson 
17543284Sapersson 	if (len < sizeof (int) || command != NDPD_SNMP_INFO_REQ) {
17553284Sapersson 		logperror("process_mibsock: bad command \n");
17563284Sapersson 		return;
17573284Sapersson 	}
17583284Sapersson 
17593284Sapersson 	ndpd_info.info_type = NDPD_SNMP_INFO_RESPONSE;
17603284Sapersson 	ndpd_info.info_version = NDPD_SNMP_INFO_VER;
17613284Sapersson 	ndpd_info.info_num_of_phyints = num_of_phyints;
17623284Sapersson 
17633284Sapersson 	(void) sendto(mibsock, &ndpd_info, sizeof (ndpd_info_t), 0,
17643284Sapersson 	    (struct sockaddr *)&from, fromlen);
17653284Sapersson 
17663284Sapersson 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
17673284Sapersson 		int prefixes;
17683284Sapersson 		int routers;
17693284Sapersson 		struct prefix   *prefix_list;
17703284Sapersson 		struct router   *router_list;
17713284Sapersson 		ndpd_phyint_info_t phyint;
17723284Sapersson 		ndpd_prefix_info_t prefix;
17733284Sapersson 		ndpd_router_info_t router;
17743284Sapersson 		/*
17753284Sapersson 		 * get number of prefixes
17763284Sapersson 		 */
17773284Sapersson 		routers = 0;
17783284Sapersson 		prefixes = 0;
17793284Sapersson 		prefix_list = pi->pi_prefix_list;
17803284Sapersson 		while (prefix_list != NULL) {
17813284Sapersson 			prefixes++;
17823284Sapersson 			prefix_list = prefix_list->pr_next;
17833284Sapersson 		}
17843284Sapersson 
17853284Sapersson 		/*
17863284Sapersson 		 * get number of routers
17873284Sapersson 		 */
17883284Sapersson 		router_list = pi->pi_router_list;
17893284Sapersson 		while (router_list != NULL) {
17903284Sapersson 			routers++;
17913284Sapersson 			router_list = router_list->dr_next;
17923284Sapersson 		}
17933284Sapersson 
17943284Sapersson 		phyint.phyint_info_type = NDPD_PHYINT_INFO;
17953284Sapersson 		phyint.phyint_info_version = NDPD_PHYINT_INFO_VER;
17963284Sapersson 		phyint.phyint_index = pi->pi_index;
17973284Sapersson 		bcopy(pi->pi_config,
17983284Sapersson 		    phyint.phyint_config, I_IFSIZE);
17993284Sapersson 		phyint.phyint_num_of_prefixes = prefixes;
18003284Sapersson 		phyint.phyint_num_of_routers = routers;
18013284Sapersson 		(void) sendto(mibsock, &phyint, sizeof (phyint), 0,
18023284Sapersson 		    (struct sockaddr *)&from, fromlen);
18033284Sapersson 
18043284Sapersson 		/*
18053284Sapersson 		 * Copy prefix information
18063284Sapersson 		 */
18073284Sapersson 
18083284Sapersson 		prefix_list = pi->pi_prefix_list;
18093284Sapersson 		while (prefix_list != NULL) {
18103284Sapersson 			prefix.prefix_info_type = NDPD_PREFIX_INFO;
18113284Sapersson 			prefix.prefix_info_version = NDPD_PREFIX_INFO_VER;
18123284Sapersson 			prefix.prefix_prefix = prefix_list->pr_prefix;
18133284Sapersson 			prefix.prefix_len = prefix_list->pr_prefix_len;
18143284Sapersson 			prefix.prefix_flags = prefix_list->pr_flags;
18153284Sapersson 			prefix.prefix_phyint_index = pi->pi_index;
18163284Sapersson 			prefix.prefix_ValidLifetime =
18173284Sapersson 			    prefix_list->pr_ValidLifetime;
18183284Sapersson 			prefix.prefix_PreferredLifetime =
18193284Sapersson 			    prefix_list->pr_PreferredLifetime;
18203284Sapersson 			prefix.prefix_OnLinkLifetime =
18213284Sapersson 			    prefix_list->pr_OnLinkLifetime;
18223284Sapersson 			prefix.prefix_OnLinkFlag =
18233284Sapersson 			    prefix_list->pr_OnLinkFlag;
18243284Sapersson 			prefix.prefix_AutonomousFlag =
18253284Sapersson 			    prefix_list->pr_AutonomousFlag;
18263284Sapersson 			(void) sendto(mibsock, &prefix, sizeof (prefix), 0,
18273284Sapersson 			    (struct sockaddr *)&from, fromlen);
18283284Sapersson 			prefix_list = prefix_list->pr_next;
18293284Sapersson 		}
18303284Sapersson 		/*
18313284Sapersson 		 * Copy router information
18323284Sapersson 		 */
18333284Sapersson 		router_list = pi->pi_router_list;
18343284Sapersson 		while (router_list != NULL) {
18353284Sapersson 			router.router_info_type = NDPD_ROUTER_INFO;
18363284Sapersson 			router.router_info_version = NDPD_ROUTER_INFO_VER;
18373284Sapersson 			router.router_address = router_list->dr_address;
18383284Sapersson 			router.router_lifetime = router_list->dr_lifetime;
18393284Sapersson 			router.router_phyint_index = pi->pi_index;
18403284Sapersson 			(void) sendto(mibsock, &router, sizeof (router), 0,
18413284Sapersson 			    (struct sockaddr *)&from, fromlen);
18423284Sapersson 			router_list = router_list->dr_next;
18433284Sapersson 		}
18443284Sapersson 	}
18453284Sapersson }
18463284Sapersson 
18470Sstevel@tonic-gate /*
18480Sstevel@tonic-gate  * Look if the phyint or one of its prefixes have been removed from
18490Sstevel@tonic-gate  * the kernel and take appropriate action.
18508485SPeter.Memishian@Sun.COM  * Uses pr_in_use and pi{,_kernel}_state.
18510Sstevel@tonic-gate  */
18520Sstevel@tonic-gate static void
check_if_removed(struct phyint * pi)18530Sstevel@tonic-gate check_if_removed(struct phyint *pi)
18540Sstevel@tonic-gate {
18558485SPeter.Memishian@Sun.COM 	struct prefix *pr, *next_pr;
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	/*
18588485SPeter.Memishian@Sun.COM 	 * Detect prefixes which are removed.
18598485SPeter.Memishian@Sun.COM 	 * Static prefixes are just removed from our tables.
18608485SPeter.Memishian@Sun.COM 	 * Non-static prefixes are recreated i.e. in.ndpd takes precedence
18618485SPeter.Memishian@Sun.COM 	 * over manually removing prefixes via ifconfig.
18628485SPeter.Memishian@Sun.COM 	 */
18638485SPeter.Memishian@Sun.COM 	for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
18648485SPeter.Memishian@Sun.COM 		next_pr = pr->pr_next;
18658485SPeter.Memishian@Sun.COM 		if (!pr->pr_in_use) {
18668485SPeter.Memishian@Sun.COM 			/* Clear everything except PR_STATIC */
18678485SPeter.Memishian@Sun.COM 			pr->pr_kernel_state &= PR_STATIC;
186812016SGirish.Moodalbail@Sun.COM 			if (pr->pr_state & PR_STATIC)
186912016SGirish.Moodalbail@Sun.COM 				prefix_update_ipadm_addrobj(pr, _B_FALSE);
18708485SPeter.Memishian@Sun.COM 			pr->pr_name[0] = '\0';
18718485SPeter.Memishian@Sun.COM 			if (pr->pr_state & PR_STATIC) {
18728485SPeter.Memishian@Sun.COM 				prefix_delete(pr);
18738485SPeter.Memishian@Sun.COM 			} else if (!(pi->pi_kernel_state & PI_PRESENT)) {
18748485SPeter.Memishian@Sun.COM 				/*
18758485SPeter.Memishian@Sun.COM 				 * Ensure that there are no future attempts to
18768485SPeter.Memishian@Sun.COM 				 * run prefix_update_k since the phyint is gone.
18778485SPeter.Memishian@Sun.COM 				 */
18788485SPeter.Memishian@Sun.COM 				pr->pr_state = pr->pr_kernel_state;
18798485SPeter.Memishian@Sun.COM 			} else if (pr->pr_state != pr->pr_kernel_state) {
18808485SPeter.Memishian@Sun.COM 				logmsg(LOG_INFO, "Prefix manually removed "
18818485SPeter.Memishian@Sun.COM 				    "on %s; recreating\n", pi->pi_name);
18828485SPeter.Memishian@Sun.COM 				prefix_update_k(pr);
18838485SPeter.Memishian@Sun.COM 			}
18848485SPeter.Memishian@Sun.COM 		}
18858485SPeter.Memishian@Sun.COM 	}
18868485SPeter.Memishian@Sun.COM 
18878485SPeter.Memishian@Sun.COM 	/*
18888485SPeter.Memishian@Sun.COM 	 * Detect phyints that have been removed from the kernel, and tear
18898485SPeter.Memishian@Sun.COM 	 * down any prefixes we created that are associated with that phyint.
18908485SPeter.Memishian@Sun.COM 	 * (NOTE: IPMP depends on in.ndpd tearing down these prefixes so an
18918485SPeter.Memishian@Sun.COM 	 * administrator can easily place an IP interface with ADDRCONF'd
18928485SPeter.Memishian@Sun.COM 	 * addresses into an IPMP group.)
18930Sstevel@tonic-gate 	 */
18940Sstevel@tonic-gate 	if (!(pi->pi_kernel_state & PI_PRESENT) &&
18950Sstevel@tonic-gate 	    (pi->pi_state & PI_PRESENT)) {
18960Sstevel@tonic-gate 		logmsg(LOG_ERR, "Interface %s has been removed from kernel. "
18970Sstevel@tonic-gate 		    "in.ndpd will no longer use it\n", pi->pi_name);
18988485SPeter.Memishian@Sun.COM 
18998485SPeter.Memishian@Sun.COM 		for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
19008485SPeter.Memishian@Sun.COM 			next_pr = pr->pr_next;
19018485SPeter.Memishian@Sun.COM 			if (pr->pr_state & PR_AUTO)
190212016SGirish.Moodalbail@Sun.COM 				prefix_update_ipadm_addrobj(pr, _B_FALSE);
19038485SPeter.Memishian@Sun.COM 				prefix_delete(pr);
19048485SPeter.Memishian@Sun.COM 		}
19058485SPeter.Memishian@Sun.COM 
19060Sstevel@tonic-gate 		/*
19078485SPeter.Memishian@Sun.COM 		 * Clear state so that should the phyint reappear we will
19088485SPeter.Memishian@Sun.COM 		 * start with initial advertisements or solicitations.
19090Sstevel@tonic-gate 		 */
19100Sstevel@tonic-gate 		phyint_cleanup(pi);
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate /*
19160Sstevel@tonic-gate  * Queuing mechanism for router advertisements that are sent by in.ndpd
19170Sstevel@tonic-gate  * and that also need to be processed by in.ndpd.
19180Sstevel@tonic-gate  * Uses "signal number" 255 to indicate to the main poll loop
19190Sstevel@tonic-gate  * that there is something to dequeue and send to incomining_ra().
19200Sstevel@tonic-gate  */
19210Sstevel@tonic-gate struct raq {
19220Sstevel@tonic-gate 	struct raq	*raq_next;
19230Sstevel@tonic-gate 	struct phyint	*raq_pi;
19240Sstevel@tonic-gate 	int		raq_packetlen;
19250Sstevel@tonic-gate 	uchar_t		*raq_packet;
19260Sstevel@tonic-gate };
19270Sstevel@tonic-gate static struct raq *raq_head = NULL;
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate /*
19300Sstevel@tonic-gate  * Allocate a struct raq and memory for the packet.
19310Sstevel@tonic-gate  * Send signal 255 to have poll dequeue.
19320Sstevel@tonic-gate  */
19330Sstevel@tonic-gate static void
loopback_ra_enqueue(struct phyint * pi,struct nd_router_advert * ra,int len)19340Sstevel@tonic-gate loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len)
19350Sstevel@tonic-gate {
19360Sstevel@tonic-gate 	struct raq *raq;
19370Sstevel@tonic-gate 	struct raq **raqp;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	if (no_loopback)
19400Sstevel@tonic-gate 		return;
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	if (debug & D_PKTOUT)
19430Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name);
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	raq = calloc(sizeof (struct raq), 1);
19460Sstevel@tonic-gate 	if (raq == NULL) {
19470Sstevel@tonic-gate 		logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
19480Sstevel@tonic-gate 		return;
19490Sstevel@tonic-gate 	}
19500Sstevel@tonic-gate 	raq->raq_packet = malloc(len);
19510Sstevel@tonic-gate 	if (raq->raq_packet == NULL) {
19520Sstevel@tonic-gate 		free(raq);
19530Sstevel@tonic-gate 		logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
19540Sstevel@tonic-gate 		return;
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 	bcopy(ra, raq->raq_packet, len);
19570Sstevel@tonic-gate 	raq->raq_packetlen = len;
19580Sstevel@tonic-gate 	raq->raq_pi = pi;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	/* Tail insert */
19610Sstevel@tonic-gate 	raqp = &raq_head;
19620Sstevel@tonic-gate 	while (*raqp != NULL)
19630Sstevel@tonic-gate 		raqp = &((*raqp)->raq_next);
19640Sstevel@tonic-gate 	*raqp = raq;
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	/* Signal for poll loop */
19670Sstevel@tonic-gate 	sig_handler(255);
19680Sstevel@tonic-gate }
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate /*
19710Sstevel@tonic-gate  * Dequeue and process all queued advertisements.
19720Sstevel@tonic-gate  */
19730Sstevel@tonic-gate static void
loopback_ra_dequeue(void)19740Sstevel@tonic-gate loopback_ra_dequeue(void)
19750Sstevel@tonic-gate {
19760Sstevel@tonic-gate 	struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT;
19770Sstevel@tonic-gate 	struct raq *raq;
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	if (debug & D_PKTIN)
19800Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n");
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	while ((raq = raq_head) != NULL) {
19830Sstevel@tonic-gate 		raq_head = raq->raq_next;
19840Sstevel@tonic-gate 		raq->raq_next = NULL;
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 		if (debug & D_PKTIN) {
19870Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n",
19880Sstevel@tonic-gate 			    raq->raq_pi->pi_name);
19890Sstevel@tonic-gate 		}
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 		incoming_ra(raq->raq_pi,
19920Sstevel@tonic-gate 		    (struct nd_router_advert *)raq->raq_packet,
19930Sstevel@tonic-gate 		    raq->raq_packetlen, &from, _B_TRUE);
19940Sstevel@tonic-gate 		free(raq->raq_packet);
19950Sstevel@tonic-gate 		free(raq);
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate }
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate static void
usage(char * cmd)20010Sstevel@tonic-gate usage(char *cmd)
20020Sstevel@tonic-gate {
20030Sstevel@tonic-gate 	(void) fprintf(stderr,
20040Sstevel@tonic-gate 	    "usage: %s [ -adt ] [-f <config file>]\n", cmd);
20050Sstevel@tonic-gate }
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate int
main(int argc,char * argv[])20080Sstevel@tonic-gate main(int argc, char *argv[])
20090Sstevel@tonic-gate {
20100Sstevel@tonic-gate 	int i;
20110Sstevel@tonic-gate 	struct phyint *pi;
20120Sstevel@tonic-gate 	int c;
20130Sstevel@tonic-gate 	char *config_file = PATH_NDPD_CONF;
20140Sstevel@tonic-gate 	boolean_t file_required = _B_FALSE;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	argv0 = argv;
20170Sstevel@tonic-gate 	srandom(gethostid());
20180Sstevel@tonic-gate 	(void) umask(0022);
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) {
20210Sstevel@tonic-gate 		switch (c) {
20220Sstevel@tonic-gate 		case 'a':
20230Sstevel@tonic-gate 			/*
20240Sstevel@tonic-gate 			 * The StatelessAddrConf variable in ndpd.conf, if
20250Sstevel@tonic-gate 			 * present, will override this setting.
20260Sstevel@tonic-gate 			 */
20270Sstevel@tonic-gate 			ifdefaults[I_StatelessAddrConf].cf_value = 0;
20280Sstevel@tonic-gate 			break;
20290Sstevel@tonic-gate 		case 'd':
20300Sstevel@tonic-gate 			debug = D_ALL;
20310Sstevel@tonic-gate 			break;
20320Sstevel@tonic-gate 		case 'D':
20330Sstevel@tonic-gate 			i = strtol((char *)optarg, NULL, 0);
20340Sstevel@tonic-gate 			if (i == 0) {
20350Sstevel@tonic-gate 				(void) fprintf(stderr, "Bad debug flags: %s\n",
20360Sstevel@tonic-gate 				    (char *)optarg);
20370Sstevel@tonic-gate 				exit(1);
20380Sstevel@tonic-gate 			}
20390Sstevel@tonic-gate 			debug |= i;
20400Sstevel@tonic-gate 			break;
20410Sstevel@tonic-gate 		case 'n':
20420Sstevel@tonic-gate 			no_loopback = 1;
20430Sstevel@tonic-gate 			break;
20440Sstevel@tonic-gate 		case 'I':
20450Sstevel@tonic-gate 			show_ifs = 1;
20460Sstevel@tonic-gate 			break;
20470Sstevel@tonic-gate 		case 't':
20480Sstevel@tonic-gate 			debug |= D_PKTIN | D_PKTOUT | D_PKTBAD;
20490Sstevel@tonic-gate 			break;
20500Sstevel@tonic-gate 		case 'f':
20510Sstevel@tonic-gate 			config_file = (char *)optarg;
20520Sstevel@tonic-gate 			file_required = _B_TRUE;
20530Sstevel@tonic-gate 			break;
20540Sstevel@tonic-gate 		case '?':
20550Sstevel@tonic-gate 			usage(argv[0]);
20560Sstevel@tonic-gate 			exit(1);
20570Sstevel@tonic-gate 		}
20580Sstevel@tonic-gate 	}
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	if (parse_config(config_file, file_required) == -1)
20610Sstevel@tonic-gate 		exit(2);
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	if (show_ifs)
20640Sstevel@tonic-gate 		phyint_print_all();
20650Sstevel@tonic-gate 
206612016SGirish.Moodalbail@Sun.COM 	if (debug == 0)
20670Sstevel@tonic-gate 		initlog();
20680Sstevel@tonic-gate 
206912016SGirish.Moodalbail@Sun.COM 	cmdsock = ndpd_setup_cmd_listener();
20700Sstevel@tonic-gate 	setup_eventpipe();
20710Sstevel@tonic-gate 	rtsock = setup_rtsock();
20723284Sapersson 	mibsock = setup_mibsock();
20730Sstevel@tonic-gate 	timer_init();
20740Sstevel@tonic-gate 	initifs(_B_TRUE);
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate 	check_daemonize();
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	for (;;) {
20790Sstevel@tonic-gate 		if (poll(pollfds, pollfd_num, -1) < 0) {
20800Sstevel@tonic-gate 			if (errno == EINTR)
20810Sstevel@tonic-gate 				continue;
20820Sstevel@tonic-gate 			logperror("main: poll");
20830Sstevel@tonic-gate 			exit(1);
20840Sstevel@tonic-gate 		}
20850Sstevel@tonic-gate 		for (i = 0; i < pollfd_num; i++) {
20860Sstevel@tonic-gate 			if (!(pollfds[i].revents & POLLIN))
20870Sstevel@tonic-gate 				continue;
20880Sstevel@tonic-gate 			if (pollfds[i].fd == eventpipe_read) {
20890Sstevel@tonic-gate 				in_signal(eventpipe_read);
20900Sstevel@tonic-gate 				break;
20910Sstevel@tonic-gate 			}
20920Sstevel@tonic-gate 			if (pollfds[i].fd == rtsock) {
20930Sstevel@tonic-gate 				process_rtsock(rtsock);
20940Sstevel@tonic-gate 				break;
20950Sstevel@tonic-gate 			}
20963284Sapersson 			if (pollfds[i].fd == mibsock) {
20973284Sapersson 				process_mibsock(mibsock);
20983284Sapersson 				break;
20993284Sapersson 			}
210012016SGirish.Moodalbail@Sun.COM 			if (pollfds[i].fd == cmdsock) {
210112016SGirish.Moodalbail@Sun.COM 				ndpd_cmd_handler(cmdsock);
210212016SGirish.Moodalbail@Sun.COM 				break;
210312016SGirish.Moodalbail@Sun.COM 			}
21040Sstevel@tonic-gate 			/*
21050Sstevel@tonic-gate 			 * Run timer routine to advance clock if more than
21060Sstevel@tonic-gate 			 * half a second since the clock was advanced.
21070Sstevel@tonic-gate 			 * This limits CPU usage under severe packet
21080Sstevel@tonic-gate 			 * arrival rates but it creates a slight inaccuracy
21090Sstevel@tonic-gate 			 * in the timer mechanism.
21100Sstevel@tonic-gate 			 */
21110Sstevel@tonic-gate 			conditional_run_timeouts(500U);
21120Sstevel@tonic-gate 			for (pi = phyints; pi != NULL; pi = pi->pi_next) {
21130Sstevel@tonic-gate 				if (pollfds[i].fd == pi->pi_sock) {
21140Sstevel@tonic-gate 					in_data(pi);
21150Sstevel@tonic-gate 					break;
21160Sstevel@tonic-gate 				}
21170Sstevel@tonic-gate 			}
21180Sstevel@tonic-gate 		}
21190Sstevel@tonic-gate 	}
21200Sstevel@tonic-gate 	/* NOTREACHED */
21210Sstevel@tonic-gate 	return (0);
21220Sstevel@tonic-gate }
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate /*
21250Sstevel@tonic-gate  * LOGGER
21260Sstevel@tonic-gate  */
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate static boolean_t logging = _B_FALSE;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate static void
initlog(void)21310Sstevel@tonic-gate initlog(void)
21320Sstevel@tonic-gate {
21330Sstevel@tonic-gate 	logging = _B_TRUE;
21340Sstevel@tonic-gate 	openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON);
21350Sstevel@tonic-gate }
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate /* Print the date/time without a trailing carridge return */
21380Sstevel@tonic-gate static void
fprintdate(FILE * file)21390Sstevel@tonic-gate fprintdate(FILE *file)
21400Sstevel@tonic-gate {
21410Sstevel@tonic-gate 	char buf[BUFSIZ];
21420Sstevel@tonic-gate 	struct tm tms;
21430Sstevel@tonic-gate 	time_t now;
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	now = time(NULL);
21460Sstevel@tonic-gate 	(void) localtime_r(&now, &tms);
21470Sstevel@tonic-gate 	(void) strftime(buf, sizeof (buf), "%h %d %X", &tms);
21480Sstevel@tonic-gate 	(void) fprintf(file, "%s ", buf);
21490Sstevel@tonic-gate }
21500Sstevel@tonic-gate 
21512546Scarlsonj /* PRINTFLIKE2 */
21520Sstevel@tonic-gate void
logmsg(int level,const char * fmt,...)21532428Smh138676 logmsg(int level, const char *fmt, ...)
21540Sstevel@tonic-gate {
21550Sstevel@tonic-gate 	va_list ap;
21560Sstevel@tonic-gate 	va_start(ap, fmt);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	if (logging) {
21590Sstevel@tonic-gate 		vsyslog(level, fmt, ap);
21600Sstevel@tonic-gate 	} else {
21610Sstevel@tonic-gate 		fprintdate(stderr);
21620Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
21630Sstevel@tonic-gate 	}
21640Sstevel@tonic-gate 	va_end(ap);
21650Sstevel@tonic-gate }
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate void
logperror(const char * str)21682428Smh138676 logperror(const char *str)
21690Sstevel@tonic-gate {
21700Sstevel@tonic-gate 	if (logging) {
21710Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %m\n", str);
21720Sstevel@tonic-gate 	} else {
21730Sstevel@tonic-gate 		fprintdate(stderr);
21740Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
21750Sstevel@tonic-gate 	}
21760Sstevel@tonic-gate }
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate void
logperror_pi(const struct phyint * pi,const char * str)21792428Smh138676 logperror_pi(const struct phyint *pi, const char *str)
21800Sstevel@tonic-gate {
21810Sstevel@tonic-gate 	if (logging) {
21820Sstevel@tonic-gate 		syslog(LOG_ERR, "%s (interface %s): %m\n",
21830Sstevel@tonic-gate 		    str, pi->pi_name);
21840Sstevel@tonic-gate 	} else {
21850Sstevel@tonic-gate 		fprintdate(stderr);
21860Sstevel@tonic-gate 		(void) fprintf(stderr, "%s (interface %s): %s\n",
21870Sstevel@tonic-gate 		    str, pi->pi_name, strerror(errno));
21880Sstevel@tonic-gate 	}
21890Sstevel@tonic-gate }
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate void
logperror_pr(const struct prefix * pr,const char * str)21922428Smh138676 logperror_pr(const struct prefix *pr, const char *str)
21930Sstevel@tonic-gate {
21940Sstevel@tonic-gate 	if (logging) {
21950Sstevel@tonic-gate 		syslog(LOG_ERR, "%s (prefix %s if %s): %m\n",
21960Sstevel@tonic-gate 		    str, pr->pr_name, pr->pr_physical->pi_name);
21970Sstevel@tonic-gate 	} else {
21980Sstevel@tonic-gate 		fprintdate(stderr);
21990Sstevel@tonic-gate 		(void) fprintf(stderr, "%s (prefix %s if %s): %s\n",
22000Sstevel@tonic-gate 		    str, pr->pr_name, pr->pr_physical->pi_name,
22010Sstevel@tonic-gate 		    strerror(errno));
22020Sstevel@tonic-gate 	}
22030Sstevel@tonic-gate }
220412016SGirish.Moodalbail@Sun.COM 
220512016SGirish.Moodalbail@Sun.COM static int
ndpd_setup_cmd_listener(void)220612016SGirish.Moodalbail@Sun.COM ndpd_setup_cmd_listener(void)
220712016SGirish.Moodalbail@Sun.COM {
220812016SGirish.Moodalbail@Sun.COM 	int sock;
220912016SGirish.Moodalbail@Sun.COM 	int ret;
221012016SGirish.Moodalbail@Sun.COM 	struct sockaddr_un servaddr;
221112016SGirish.Moodalbail@Sun.COM 
221212016SGirish.Moodalbail@Sun.COM 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
221312016SGirish.Moodalbail@Sun.COM 	if (sock < 0) {
221412016SGirish.Moodalbail@Sun.COM 		logperror("socket");
221512016SGirish.Moodalbail@Sun.COM 		exit(1);
221612016SGirish.Moodalbail@Sun.COM 	}
221712016SGirish.Moodalbail@Sun.COM 
221812016SGirish.Moodalbail@Sun.COM 	bzero(&servaddr, sizeof (servaddr));
221912016SGirish.Moodalbail@Sun.COM 	servaddr.sun_family = AF_UNIX;
222012016SGirish.Moodalbail@Sun.COM 	(void) strlcpy(servaddr.sun_path, IPADM_UDS_PATH,
222112016SGirish.Moodalbail@Sun.COM 	    sizeof (servaddr.sun_path));
222212016SGirish.Moodalbail@Sun.COM 	(void) unlink(servaddr.sun_path);
222312016SGirish.Moodalbail@Sun.COM 	ret = bind(sock, (struct sockaddr *)&servaddr, sizeof (servaddr));
222412016SGirish.Moodalbail@Sun.COM 	if (ret < 0) {
222512016SGirish.Moodalbail@Sun.COM 		logperror("bind");
222612016SGirish.Moodalbail@Sun.COM 		exit(1);
222712016SGirish.Moodalbail@Sun.COM 	}
222812016SGirish.Moodalbail@Sun.COM 	if (listen(sock, 30) < 0) {
222912016SGirish.Moodalbail@Sun.COM 		logperror("listen");
223012016SGirish.Moodalbail@Sun.COM 		exit(1);
223112016SGirish.Moodalbail@Sun.COM 	}
223212016SGirish.Moodalbail@Sun.COM 	if (poll_add(sock) == -1) {
223312016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "command socket could not be added to the "
223412016SGirish.Moodalbail@Sun.COM 		    "polling set\n");
223512016SGirish.Moodalbail@Sun.COM 		exit(1);
223612016SGirish.Moodalbail@Sun.COM 	}
223712016SGirish.Moodalbail@Sun.COM 
223812016SGirish.Moodalbail@Sun.COM 	return (sock);
223912016SGirish.Moodalbail@Sun.COM }
224012016SGirish.Moodalbail@Sun.COM 
224112016SGirish.Moodalbail@Sun.COM /*
224212016SGirish.Moodalbail@Sun.COM  * Commands received over the command socket come here
224312016SGirish.Moodalbail@Sun.COM  */
224412016SGirish.Moodalbail@Sun.COM static void
ndpd_cmd_handler(int sock)224512016SGirish.Moodalbail@Sun.COM ndpd_cmd_handler(int sock)
224612016SGirish.Moodalbail@Sun.COM {
224712016SGirish.Moodalbail@Sun.COM 	int			newfd;
224812016SGirish.Moodalbail@Sun.COM 	struct sockaddr_storage	peer;
224912016SGirish.Moodalbail@Sun.COM 	socklen_t		peerlen;
225012016SGirish.Moodalbail@Sun.COM 	ipadm_ndpd_msg_t	ndpd_msg;
225112016SGirish.Moodalbail@Sun.COM 	int			retval;
225212016SGirish.Moodalbail@Sun.COM 
225312016SGirish.Moodalbail@Sun.COM 	peerlen = sizeof (peer);
225412016SGirish.Moodalbail@Sun.COM 	newfd = accept(sock, (struct sockaddr *)&peer, &peerlen);
225512016SGirish.Moodalbail@Sun.COM 	if (newfd < 0) {
225612016SGirish.Moodalbail@Sun.COM 		logperror("accept");
225712016SGirish.Moodalbail@Sun.COM 		return;
225812016SGirish.Moodalbail@Sun.COM 	}
225912016SGirish.Moodalbail@Sun.COM 
226012016SGirish.Moodalbail@Sun.COM 	retval = ipadm_ndpd_read(newfd, &ndpd_msg, sizeof (ndpd_msg));
226112016SGirish.Moodalbail@Sun.COM 	if (retval != 0)
226212016SGirish.Moodalbail@Sun.COM 		logperror("Could not read ndpd command");
226312016SGirish.Moodalbail@Sun.COM 
226412016SGirish.Moodalbail@Sun.COM 	retval = ndpd_process_cmd(newfd, &ndpd_msg);
226512016SGirish.Moodalbail@Sun.COM 	if (retval != 0) {
226612016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "ndpd command on interface %s failed with "
226712016SGirish.Moodalbail@Sun.COM 		    "error %s\n", ndpd_msg.inm_ifname, strerror(retval));
226812016SGirish.Moodalbail@Sun.COM 	}
226912016SGirish.Moodalbail@Sun.COM 	(void) close(newfd);
227012016SGirish.Moodalbail@Sun.COM }
227112016SGirish.Moodalbail@Sun.COM 
227212016SGirish.Moodalbail@Sun.COM /*
227312016SGirish.Moodalbail@Sun.COM  * Process the commands received from the cmd listener socket.
227412016SGirish.Moodalbail@Sun.COM  */
227512016SGirish.Moodalbail@Sun.COM static int
ndpd_process_cmd(int newfd,ipadm_ndpd_msg_t * msg)227612016SGirish.Moodalbail@Sun.COM ndpd_process_cmd(int newfd, ipadm_ndpd_msg_t *msg)
227712016SGirish.Moodalbail@Sun.COM {
227812016SGirish.Moodalbail@Sun.COM 	int err;
227912016SGirish.Moodalbail@Sun.COM 
228012016SGirish.Moodalbail@Sun.COM 	if (!ipadm_check_auth()) {
228112016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "User not authorized to send the command\n");
228212016SGirish.Moodalbail@Sun.COM 		(void) ndpd_send_error(newfd, EPERM);
228312016SGirish.Moodalbail@Sun.COM 		return (EPERM);
228412016SGirish.Moodalbail@Sun.COM 	}
228512016SGirish.Moodalbail@Sun.COM 	switch (msg->inm_cmd) {
228612016SGirish.Moodalbail@Sun.COM 	case IPADM_DISABLE_AUTOCONF:
228712016SGirish.Moodalbail@Sun.COM 		err = ndpd_set_autoconf(msg->inm_ifname, _B_FALSE);
228812016SGirish.Moodalbail@Sun.COM 		break;
228912016SGirish.Moodalbail@Sun.COM 
229012016SGirish.Moodalbail@Sun.COM 	case IPADM_ENABLE_AUTOCONF:
229112016SGirish.Moodalbail@Sun.COM 		err = ndpd_set_autoconf(msg->inm_ifname, _B_TRUE);
229212016SGirish.Moodalbail@Sun.COM 		break;
229312016SGirish.Moodalbail@Sun.COM 
229412016SGirish.Moodalbail@Sun.COM 	case IPADM_CREATE_ADDRS:
229512016SGirish.Moodalbail@Sun.COM 		err = ndpd_create_addrs(msg->inm_ifname, msg->inm_intfid,
229612016SGirish.Moodalbail@Sun.COM 		    msg->inm_intfidlen, msg->inm_stateless,
229712016SGirish.Moodalbail@Sun.COM 		    msg->inm_stateful, msg->inm_aobjname);
229812016SGirish.Moodalbail@Sun.COM 		break;
229912016SGirish.Moodalbail@Sun.COM 
230012016SGirish.Moodalbail@Sun.COM 	case IPADM_DELETE_ADDRS:
230112016SGirish.Moodalbail@Sun.COM 		err = ndpd_delete_addrs(msg->inm_ifname);
230212016SGirish.Moodalbail@Sun.COM 		break;
230312016SGirish.Moodalbail@Sun.COM 
230412016SGirish.Moodalbail@Sun.COM 	default:
230512016SGirish.Moodalbail@Sun.COM 		err = EINVAL;
230612016SGirish.Moodalbail@Sun.COM 		break;
230712016SGirish.Moodalbail@Sun.COM 	}
230812016SGirish.Moodalbail@Sun.COM 
230912016SGirish.Moodalbail@Sun.COM 	(void) ndpd_send_error(newfd, err);
231012016SGirish.Moodalbail@Sun.COM 
231112016SGirish.Moodalbail@Sun.COM 	return (err);
231212016SGirish.Moodalbail@Sun.COM }
231312016SGirish.Moodalbail@Sun.COM 
231412016SGirish.Moodalbail@Sun.COM static int
ndpd_send_error(int fd,int error)231512016SGirish.Moodalbail@Sun.COM ndpd_send_error(int fd, int error)
231612016SGirish.Moodalbail@Sun.COM {
231712016SGirish.Moodalbail@Sun.COM 	return (ipadm_ndpd_write(fd, &error, sizeof (error)));
231812016SGirish.Moodalbail@Sun.COM }
231912016SGirish.Moodalbail@Sun.COM 
232012016SGirish.Moodalbail@Sun.COM /*
232112016SGirish.Moodalbail@Sun.COM  * Disables/Enables autoconfiguration of addresses on the
232212016SGirish.Moodalbail@Sun.COM  * given physical interface.
232312016SGirish.Moodalbail@Sun.COM  * This is provided to support the legacy method of configuring IPv6
232412016SGirish.Moodalbail@Sun.COM  * addresses. i.e. `ifconfig bge0 inet6 plumb` will plumb the interface
232512016SGirish.Moodalbail@Sun.COM  * and start stateless and stateful autoconfiguration. If this function is
232612016SGirish.Moodalbail@Sun.COM  * not called with enable=_B_FALSE, no autoconfiguration will be done until
232712016SGirish.Moodalbail@Sun.COM  * ndpd_create_addrs() is called with an Interface ID.
232812016SGirish.Moodalbail@Sun.COM  */
232912016SGirish.Moodalbail@Sun.COM static int
ndpd_set_autoconf(const char * ifname,boolean_t enable)233012016SGirish.Moodalbail@Sun.COM ndpd_set_autoconf(const char *ifname, boolean_t enable)
233112016SGirish.Moodalbail@Sun.COM {
233212016SGirish.Moodalbail@Sun.COM 	struct phyint *pi;
233312016SGirish.Moodalbail@Sun.COM 
233412016SGirish.Moodalbail@Sun.COM 	pi = phyint_lookup((char *)ifname);
233512016SGirish.Moodalbail@Sun.COM 	if (pi == NULL) {
233612016SGirish.Moodalbail@Sun.COM 		/*
233712016SGirish.Moodalbail@Sun.COM 		 * If the physical interface was plumbed but no
233812016SGirish.Moodalbail@Sun.COM 		 * addresses were configured yet, phyint will not exist.
233912016SGirish.Moodalbail@Sun.COM 		 */
234012016SGirish.Moodalbail@Sun.COM 		pi = phyint_create((char *)ifname);
234112016SGirish.Moodalbail@Sun.COM 		if (pi == NULL) {
234212016SGirish.Moodalbail@Sun.COM 			logmsg(LOG_ERR, "could not create phyint for "
234312016SGirish.Moodalbail@Sun.COM 			    "interface %s", ifname);
234412016SGirish.Moodalbail@Sun.COM 			return (ENOMEM);
234512016SGirish.Moodalbail@Sun.COM 		}
234612016SGirish.Moodalbail@Sun.COM 	}
234712016SGirish.Moodalbail@Sun.COM 	pi->pi_autoconf = enable;
234812016SGirish.Moodalbail@Sun.COM 
234912016SGirish.Moodalbail@Sun.COM 	if (debug & D_PHYINT) {
235012016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_DEBUG, "ndpd_set_autoconf: %s autoconf for "
235112016SGirish.Moodalbail@Sun.COM 		    "interface %s\n", (enable ? "enabled" : "disabled"),
235212016SGirish.Moodalbail@Sun.COM 		    pi->pi_name);
235312016SGirish.Moodalbail@Sun.COM 	}
235412016SGirish.Moodalbail@Sun.COM 	return (0);
235512016SGirish.Moodalbail@Sun.COM }
235612016SGirish.Moodalbail@Sun.COM 
235712016SGirish.Moodalbail@Sun.COM /*
235812016SGirish.Moodalbail@Sun.COM  * Create auto-configured addresses on the given interface using
235912016SGirish.Moodalbail@Sun.COM  * the given token as the interface id during the next Router Advertisement.
236012016SGirish.Moodalbail@Sun.COM  * Currently, only one token per interface is supported.
236112016SGirish.Moodalbail@Sun.COM  */
236212016SGirish.Moodalbail@Sun.COM static int
ndpd_create_addrs(const char * ifname,struct sockaddr_in6 intfid,int intfidlen,boolean_t stateless,boolean_t stateful,char * addrobj)236312016SGirish.Moodalbail@Sun.COM ndpd_create_addrs(const char *ifname, struct sockaddr_in6 intfid, int intfidlen,
236412016SGirish.Moodalbail@Sun.COM     boolean_t stateless, boolean_t stateful, char *addrobj)
236512016SGirish.Moodalbail@Sun.COM {
236612016SGirish.Moodalbail@Sun.COM 	struct phyint *pi;
236712016SGirish.Moodalbail@Sun.COM 	struct lifreq lifr;
236812016SGirish.Moodalbail@Sun.COM 	struct sockaddr_in6 *sin6;
236912016SGirish.Moodalbail@Sun.COM 	int err;
237012016SGirish.Moodalbail@Sun.COM 
237112016SGirish.Moodalbail@Sun.COM 	pi = phyint_lookup((char *)ifname);
237212016SGirish.Moodalbail@Sun.COM 	if (pi == NULL) {
237312016SGirish.Moodalbail@Sun.COM 		/*
237412016SGirish.Moodalbail@Sun.COM 		 * If the physical interface was plumbed but no
237512016SGirish.Moodalbail@Sun.COM 		 * addresses were configured yet, phyint will not exist.
237612016SGirish.Moodalbail@Sun.COM 		 */
237712016SGirish.Moodalbail@Sun.COM 		pi = phyint_create((char *)ifname);
237812016SGirish.Moodalbail@Sun.COM 		if (pi == NULL) {
237912016SGirish.Moodalbail@Sun.COM 			if (debug & D_PHYINT)
238012016SGirish.Moodalbail@Sun.COM 				logmsg(LOG_ERR, "could not create phyint "
238112016SGirish.Moodalbail@Sun.COM 				    "for interface %s", ifname);
238212016SGirish.Moodalbail@Sun.COM 			return (ENOMEM);
238312016SGirish.Moodalbail@Sun.COM 		}
238412016SGirish.Moodalbail@Sun.COM 	} else if (pi->pi_autoconf) {
238512016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "autoconfiguration already in progress\n");
238612016SGirish.Moodalbail@Sun.COM 		return (EEXIST);
238712016SGirish.Moodalbail@Sun.COM 	}
238812016SGirish.Moodalbail@Sun.COM 	check_autoconf_var_consistency(pi, stateless, stateful);
238912016SGirish.Moodalbail@Sun.COM 
239012016SGirish.Moodalbail@Sun.COM 	if (intfidlen == 0) {
239112016SGirish.Moodalbail@Sun.COM 		pi->pi_default_token = _B_TRUE;
239212016SGirish.Moodalbail@Sun.COM 		if (ifsock < 0) {
239312016SGirish.Moodalbail@Sun.COM 			ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
239412016SGirish.Moodalbail@Sun.COM 			if (ifsock < 0) {
239512016SGirish.Moodalbail@Sun.COM 				err = errno;
239612016SGirish.Moodalbail@Sun.COM 				logperror("ndpd_create_addrs: socket");
239712016SGirish.Moodalbail@Sun.COM 				return (err);
239812016SGirish.Moodalbail@Sun.COM 			}
239912016SGirish.Moodalbail@Sun.COM 		}
240012016SGirish.Moodalbail@Sun.COM 		(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
240112016SGirish.Moodalbail@Sun.COM 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
240212016SGirish.Moodalbail@Sun.COM 		if (ioctl(ifsock, SIOCGLIFTOKEN, (char *)&lifr) < 0) {
240312016SGirish.Moodalbail@Sun.COM 			err = errno;
240412016SGirish.Moodalbail@Sun.COM 			logperror("SIOCGLIFTOKEN");
240512016SGirish.Moodalbail@Sun.COM 			return (err);
240612016SGirish.Moodalbail@Sun.COM 		}
240712016SGirish.Moodalbail@Sun.COM 		pi->pi_token = sin6->sin6_addr;
240812016SGirish.Moodalbail@Sun.COM 		pi->pi_token_length = lifr.lifr_addrlen;
240912016SGirish.Moodalbail@Sun.COM 	} else {
241012016SGirish.Moodalbail@Sun.COM 		pi->pi_default_token = _B_FALSE;
241112016SGirish.Moodalbail@Sun.COM 		pi->pi_token = intfid.sin6_addr;
241212016SGirish.Moodalbail@Sun.COM 		pi->pi_token_length = intfidlen;
241312016SGirish.Moodalbail@Sun.COM 	}
241412016SGirish.Moodalbail@Sun.COM 	pi->pi_stateless = stateless;
241512016SGirish.Moodalbail@Sun.COM 	pi->pi_stateful = stateful;
241612016SGirish.Moodalbail@Sun.COM 	(void) strlcpy(pi->pi_ipadm_aobjname, addrobj,
241712016SGirish.Moodalbail@Sun.COM 	    sizeof (pi->pi_ipadm_aobjname));
241812016SGirish.Moodalbail@Sun.COM 
241912016SGirish.Moodalbail@Sun.COM 	/* We can allow autoconfiguration now. */
242012016SGirish.Moodalbail@Sun.COM 	pi->pi_autoconf = _B_TRUE;
242112016SGirish.Moodalbail@Sun.COM 
242212016SGirish.Moodalbail@Sun.COM 	/* Restart the solicitations. */
242312016SGirish.Moodalbail@Sun.COM 	if (pi->pi_sol_state == DONE_SOLICIT)
242412016SGirish.Moodalbail@Sun.COM 		pi->pi_sol_state = NO_SOLICIT;
242512016SGirish.Moodalbail@Sun.COM 	if (pi->pi_sol_state == NO_SOLICIT)
242612016SGirish.Moodalbail@Sun.COM 		check_to_solicit(pi, START_INIT_SOLICIT);
242712016SGirish.Moodalbail@Sun.COM 	if (debug & D_PHYINT)
242812016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_DEBUG, "ndpd_create_addrs: "
242912016SGirish.Moodalbail@Sun.COM 		    "added token to interface %s\n", pi->pi_name);
243012016SGirish.Moodalbail@Sun.COM 	return (0);
243112016SGirish.Moodalbail@Sun.COM }
243212016SGirish.Moodalbail@Sun.COM 
243312016SGirish.Moodalbail@Sun.COM /*
243412016SGirish.Moodalbail@Sun.COM  * This function deletes all addresses on the given interface
243512016SGirish.Moodalbail@Sun.COM  * with the given Interface ID.
243612016SGirish.Moodalbail@Sun.COM  */
243712016SGirish.Moodalbail@Sun.COM static int
ndpd_delete_addrs(const char * ifname)243812016SGirish.Moodalbail@Sun.COM ndpd_delete_addrs(const char *ifname)
243912016SGirish.Moodalbail@Sun.COM {
244012016SGirish.Moodalbail@Sun.COM 	struct phyint *pi;
244112016SGirish.Moodalbail@Sun.COM 	struct prefix *pr, *next_pr;
244212016SGirish.Moodalbail@Sun.COM 	struct lifreq lifr;
244312016SGirish.Moodalbail@Sun.COM 	int err;
244412016SGirish.Moodalbail@Sun.COM 
244512016SGirish.Moodalbail@Sun.COM 	pi = phyint_lookup((char *)ifname);
244612016SGirish.Moodalbail@Sun.COM 	if (pi == NULL) {
244712016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "no phyint found for %s", ifname);
244812016SGirish.Moodalbail@Sun.COM 		return (ENXIO);
244912016SGirish.Moodalbail@Sun.COM 	}
245012016SGirish.Moodalbail@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&pi->pi_token)) {
245112016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "token does not exist for %s", ifname);
245212016SGirish.Moodalbail@Sun.COM 		return (EINVAL);
245312016SGirish.Moodalbail@Sun.COM 	}
245412016SGirish.Moodalbail@Sun.COM 
245512016SGirish.Moodalbail@Sun.COM 	if (ifsock < 0) {
245612016SGirish.Moodalbail@Sun.COM 		ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
245712016SGirish.Moodalbail@Sun.COM 		if (ifsock < 0) {
245812016SGirish.Moodalbail@Sun.COM 			err = errno;
245912016SGirish.Moodalbail@Sun.COM 			logperror("ndpd_create_addrs: socket");
246012016SGirish.Moodalbail@Sun.COM 			return (err);
246112016SGirish.Moodalbail@Sun.COM 		}
246212016SGirish.Moodalbail@Sun.COM 	}
246312016SGirish.Moodalbail@Sun.COM 	/* Remove the prefixes for this phyint if they exist */
246412016SGirish.Moodalbail@Sun.COM 	for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
246512016SGirish.Moodalbail@Sun.COM 		next_pr = pr->pr_next;
246612016SGirish.Moodalbail@Sun.COM 		if (pr->pr_name[0] == '\0') {
246712016SGirish.Moodalbail@Sun.COM 			prefix_delete(pr);
246812016SGirish.Moodalbail@Sun.COM 			continue;
246912016SGirish.Moodalbail@Sun.COM 		}
247012016SGirish.Moodalbail@Sun.COM 		/*
247112016SGirish.Moodalbail@Sun.COM 		 * Delete all the prefixes for the auto-configured
247212016SGirish.Moodalbail@Sun.COM 		 * addresses as well as the DHCPv6 addresses.
247312016SGirish.Moodalbail@Sun.COM 		 */
247412016SGirish.Moodalbail@Sun.COM 		(void) strncpy(lifr.lifr_name, pr->pr_name,
247512016SGirish.Moodalbail@Sun.COM 		    sizeof (lifr.lifr_name));
247612016SGirish.Moodalbail@Sun.COM 		if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
247712016SGirish.Moodalbail@Sun.COM 			err = errno;
247812016SGirish.Moodalbail@Sun.COM 			logperror("SIOCGLIFFLAGS");
247912016SGirish.Moodalbail@Sun.COM 			return (err);
248012016SGirish.Moodalbail@Sun.COM 		}
248112016SGirish.Moodalbail@Sun.COM 		if ((lifr.lifr_flags & IFF_ADDRCONF) ||
248212016SGirish.Moodalbail@Sun.COM 		    (lifr.lifr_flags & IFF_DHCPRUNNING)) {
248312016SGirish.Moodalbail@Sun.COM 			prefix_update_ipadm_addrobj(pr, _B_FALSE);
248412016SGirish.Moodalbail@Sun.COM 		}
248512016SGirish.Moodalbail@Sun.COM 		prefix_delete(pr);
248612016SGirish.Moodalbail@Sun.COM 	}
248712016SGirish.Moodalbail@Sun.COM 
248812016SGirish.Moodalbail@Sun.COM 	/*
248912016SGirish.Moodalbail@Sun.COM 	 * If we had started dhcpagent, we need to release the leases
249012016SGirish.Moodalbail@Sun.COM 	 * if any are required.
249112016SGirish.Moodalbail@Sun.COM 	 */
249212016SGirish.Moodalbail@Sun.COM 	if (pi->pi_stateful) {
249312016SGirish.Moodalbail@Sun.COM 		(void) strncpy(lifr.lifr_name, pi->pi_name,
249412016SGirish.Moodalbail@Sun.COM 		    sizeof (lifr.lifr_name));
249512016SGirish.Moodalbail@Sun.COM 		if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
249612016SGirish.Moodalbail@Sun.COM 			err = errno;
249712016SGirish.Moodalbail@Sun.COM 			logperror("SIOCGLIFFLAGS");
249812016SGirish.Moodalbail@Sun.COM 			return (err);
249912016SGirish.Moodalbail@Sun.COM 		}
250012016SGirish.Moodalbail@Sun.COM 		if (lifr.lifr_flags & IFF_DHCPRUNNING)
250112016SGirish.Moodalbail@Sun.COM 			release_dhcp(pi);
250212016SGirish.Moodalbail@Sun.COM 	}
250312016SGirish.Moodalbail@Sun.COM 
250412016SGirish.Moodalbail@Sun.COM 	/*
250512016SGirish.Moodalbail@Sun.COM 	 * Reset the Interface ID on this phyint and stop autoconfigurations
250612016SGirish.Moodalbail@Sun.COM 	 * until a new interface ID is provided.
250712016SGirish.Moodalbail@Sun.COM 	 */
250812016SGirish.Moodalbail@Sun.COM 	pi->pi_token = in6addr_any;
250912016SGirish.Moodalbail@Sun.COM 	pi->pi_token_length = 0;
251012016SGirish.Moodalbail@Sun.COM 	pi->pi_autoconf = _B_FALSE;
251112016SGirish.Moodalbail@Sun.COM 	pi->pi_ipadm_aobjname[0] = '\0';
251212016SGirish.Moodalbail@Sun.COM 
251312016SGirish.Moodalbail@Sun.COM 	/* Reset the stateless and stateful settings to default. */
251412016SGirish.Moodalbail@Sun.COM 	pi->pi_stateless = pi->pi_StatelessAddrConf;
251512016SGirish.Moodalbail@Sun.COM 	pi->pi_stateful = pi->pi_StatefulAddrConf;
251612016SGirish.Moodalbail@Sun.COM 
251712016SGirish.Moodalbail@Sun.COM 	if (debug & D_PHYINT) {
251812016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_DEBUG, "ndpd_delete_addrs: "
251912016SGirish.Moodalbail@Sun.COM 		    "removed token from interface %s\n", pi->pi_name);
252012016SGirish.Moodalbail@Sun.COM 	}
252112016SGirish.Moodalbail@Sun.COM 	return (0);
252212016SGirish.Moodalbail@Sun.COM }
252312016SGirish.Moodalbail@Sun.COM 
252412016SGirish.Moodalbail@Sun.COM void
check_autoconf_var_consistency(struct phyint * pi,boolean_t stateless,boolean_t stateful)252512016SGirish.Moodalbail@Sun.COM check_autoconf_var_consistency(struct phyint *pi, boolean_t stateless,
252612016SGirish.Moodalbail@Sun.COM     boolean_t stateful)
252712016SGirish.Moodalbail@Sun.COM {
252812016SGirish.Moodalbail@Sun.COM 	/*
252912016SGirish.Moodalbail@Sun.COM 	 * If StatelessAddrConf and StatelessAddrConf are set in
253012016SGirish.Moodalbail@Sun.COM 	 * /etc/inet/ndpd.conf, check if the new values override those
253112016SGirish.Moodalbail@Sun.COM 	 * settings. If so, log a warning.
253212016SGirish.Moodalbail@Sun.COM 	 */
253312016SGirish.Moodalbail@Sun.COM 	if ((pi->pi_StatelessAddrConf !=
253412016SGirish.Moodalbail@Sun.COM 	    ifdefaults[I_StatelessAddrConf].cf_value &&
253512016SGirish.Moodalbail@Sun.COM 	    stateless != pi->pi_StatelessAddrConf) ||
253612016SGirish.Moodalbail@Sun.COM 	    (pi->pi_StatefulAddrConf !=
253712016SGirish.Moodalbail@Sun.COM 	    ifdefaults[I_StatefulAddrConf].cf_value &&
253812016SGirish.Moodalbail@Sun.COM 	    stateful != pi->pi_StatefulAddrConf)) {
253912016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "check_autoconf_var_consistency: "
254012016SGirish.Moodalbail@Sun.COM 		    "Overriding the StatelessAddrConf or StatefulAddrConf "
254112016SGirish.Moodalbail@Sun.COM 		    "settings in ndpd.conf with the new values for "
254212016SGirish.Moodalbail@Sun.COM 		    "interface %s\n", pi->pi_name);
254312016SGirish.Moodalbail@Sun.COM 	}
254412016SGirish.Moodalbail@Sun.COM }
254512016SGirish.Moodalbail@Sun.COM 
254612016SGirish.Moodalbail@Sun.COM /*
254712016SGirish.Moodalbail@Sun.COM  * If ipadm was used to start autoconfiguration and in.ndpd was restarted
254812016SGirish.Moodalbail@Sun.COM  * for some reason, in.ndpd has to resume autoconfiguration when it comes up.
254912016SGirish.Moodalbail@Sun.COM  * In this function, it scans the ipadm_addr_info() output to find a link-local
255012016SGirish.Moodalbail@Sun.COM  * on this interface with address type "addrconf" and extracts the interface id.
255112016SGirish.Moodalbail@Sun.COM  * It also stores the addrobj name to be used later when new addresses are
255212016SGirish.Moodalbail@Sun.COM  * created for the prefixes advertised by the router.
255312016SGirish.Moodalbail@Sun.COM  * If autoconfiguration was never started on this interface before in.ndpd
255412016SGirish.Moodalbail@Sun.COM  * was killed, then in.ndpd should refrain from configuring prefixes, even if
255512016SGirish.Moodalbail@Sun.COM  * there is a valid link-local on this interface, created by ipadm (identified
255612016SGirish.Moodalbail@Sun.COM  * if there is a valid addrobj name).
255712016SGirish.Moodalbail@Sun.COM  */
255812016SGirish.Moodalbail@Sun.COM static int
phyint_check_ipadm_intfid(struct phyint * pi)255912016SGirish.Moodalbail@Sun.COM phyint_check_ipadm_intfid(struct phyint *pi)
256012016SGirish.Moodalbail@Sun.COM {
256112016SGirish.Moodalbail@Sun.COM 	ipadm_status_t		status;
256212016SGirish.Moodalbail@Sun.COM 	ipadm_addr_info_t	*addrinfo;
256312016SGirish.Moodalbail@Sun.COM 	struct ifaddrs		*ifap;
256412016SGirish.Moodalbail@Sun.COM 	ipadm_addr_info_t	*ainfop;
256512016SGirish.Moodalbail@Sun.COM 	struct sockaddr_in6	*sin6;
256612016SGirish.Moodalbail@Sun.COM 	ipadm_handle_t		iph;
256712016SGirish.Moodalbail@Sun.COM 
256812016SGirish.Moodalbail@Sun.COM 	if (ipadm_open(&iph, 0) != IPADM_SUCCESS) {
256912016SGirish.Moodalbail@Sun.COM 		logmsg(LOG_ERR, "could not open handle to libipadm\n");
257012016SGirish.Moodalbail@Sun.COM 		return (-1);
257112016SGirish.Moodalbail@Sun.COM 	}
257212016SGirish.Moodalbail@Sun.COM 
257312016SGirish.Moodalbail@Sun.COM 	status = ipadm_addr_info(iph, pi->pi_name, &addrinfo,
257412016SGirish.Moodalbail@Sun.COM 	    IPADM_OPT_ZEROADDR, LIFC_NOXMIT|LIFC_TEMPORARY);
257512016SGirish.Moodalbail@Sun.COM 	if (status != IPADM_SUCCESS) {
257612016SGirish.Moodalbail@Sun.COM 		ipadm_close(iph);
257712016SGirish.Moodalbail@Sun.COM 		return (-1);
257812016SGirish.Moodalbail@Sun.COM 	}
257912016SGirish.Moodalbail@Sun.COM 	pi->pi_autoconf = _B_TRUE;
258012016SGirish.Moodalbail@Sun.COM 	for (ainfop = addrinfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
258112016SGirish.Moodalbail@Sun.COM 		ifap = &ainfop->ia_ifa;
2582*12805SDarren.Reed@Oracle.COM 		if (ifap->ifa_addr->sa_family != AF_INET6 ||
258312016SGirish.Moodalbail@Sun.COM 		    ainfop->ia_state == IFA_DISABLED)
258412016SGirish.Moodalbail@Sun.COM 			continue;
258512016SGirish.Moodalbail@Sun.COM 		sin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
258612016SGirish.Moodalbail@Sun.COM 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
258712016SGirish.Moodalbail@Sun.COM 			if (ainfop->ia_atype == IPADM_ADDR_IPV6_ADDRCONF) {
258812016SGirish.Moodalbail@Sun.COM 				pi->pi_token = sin6->sin6_addr;
258912016SGirish.Moodalbail@Sun.COM 				pi->pi_token._S6_un._S6_u32[0] = 0;
259012016SGirish.Moodalbail@Sun.COM 				pi->pi_token._S6_un._S6_u32[1] = 0;
259112016SGirish.Moodalbail@Sun.COM 				pi->pi_autoconf = _B_TRUE;
259212016SGirish.Moodalbail@Sun.COM 				(void) strlcpy(pi->pi_ipadm_aobjname,
259312016SGirish.Moodalbail@Sun.COM 				    ainfop->ia_aobjname,
259412016SGirish.Moodalbail@Sun.COM 				    sizeof (pi->pi_ipadm_aobjname));
259512016SGirish.Moodalbail@Sun.COM 				break;
259612016SGirish.Moodalbail@Sun.COM 			}
259712016SGirish.Moodalbail@Sun.COM 			/*
259812016SGirish.Moodalbail@Sun.COM 			 * If IFF_NOLINKLOCAL is set, then the link-local
259912016SGirish.Moodalbail@Sun.COM 			 * was created using ipadm. Do not autoconfigure until
260012016SGirish.Moodalbail@Sun.COM 			 * ipadm is explicitly used for autoconfiguration.
260112016SGirish.Moodalbail@Sun.COM 			 */
260212016SGirish.Moodalbail@Sun.COM 			if (ifap->ifa_flags & IFF_NOLINKLOCAL)
260312016SGirish.Moodalbail@Sun.COM 				pi->pi_autoconf = _B_FALSE;
260412016SGirish.Moodalbail@Sun.COM 		} else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
260512016SGirish.Moodalbail@Sun.COM 		    strrchr(ifap->ifa_name, ':') == NULL) {
260612016SGirish.Moodalbail@Sun.COM 			/* The interface was created using ipadm. */
260712016SGirish.Moodalbail@Sun.COM 			pi->pi_autoconf = _B_FALSE;
260812016SGirish.Moodalbail@Sun.COM 		}
260912016SGirish.Moodalbail@Sun.COM 	}
261012016SGirish.Moodalbail@Sun.COM 	ipadm_free_addr_info(addrinfo);
261112016SGirish.Moodalbail@Sun.COM 	if (!pi->pi_autoconf) {
261212016SGirish.Moodalbail@Sun.COM 		pi->pi_token = in6addr_any;
261312016SGirish.Moodalbail@Sun.COM 		pi->pi_token_length = 0;
261412016SGirish.Moodalbail@Sun.COM 	}
261512016SGirish.Moodalbail@Sun.COM 	ipadm_close(iph);
261612016SGirish.Moodalbail@Sun.COM 	return (0);
261712016SGirish.Moodalbail@Sun.COM }
2618