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
51534Spwernau  * Common Development and Distribution License (the "License").
61534Spwernau  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221534Spwernau  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "defs.h"
290Sstevel@tonic-gate #include "tables.h"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <time.h>
32*3284Sapersson #include <assert.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate struct phyint *phyints = NULL;
35*3284Sapersson int num_of_phyints = 0;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static void	phyint_print(struct phyint *pi);
380Sstevel@tonic-gate static void	phyint_insert(struct phyint *pi);
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static boolean_t tmptoken_isvalid(struct in6_addr *token);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static void	prefix_print(struct prefix *pr);
430Sstevel@tonic-gate static void	prefix_insert(struct phyint *pi, struct prefix *pr);
440Sstevel@tonic-gate static char	*prefix_print_state(int state, char *buf, int buflen);
450Sstevel@tonic-gate static void	prefix_set(struct in6_addr *prefix, struct in6_addr addr,
460Sstevel@tonic-gate 		    int bits);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static void	adv_prefix_print(struct adv_prefix *adv_pr);
490Sstevel@tonic-gate static void	adv_prefix_insert(struct phyint *pi, struct adv_prefix *adv_pr);
500Sstevel@tonic-gate static void	adv_prefix_delete(struct adv_prefix *adv_pr);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void	router_print(struct router *dr);
530Sstevel@tonic-gate static void	router_insert(struct phyint *pi, struct router *dr);
540Sstevel@tonic-gate static void	router_delete(struct router *dr);
550Sstevel@tonic-gate static void	router_add_k(struct router *dr);
560Sstevel@tonic-gate static void	router_delete_k(struct router *dr);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int	rtmseq;				/* rtm_seq sequence number */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* 1 week in ms */
610Sstevel@tonic-gate #define	NDP_PREFIX_DEFAULT_LIFETIME	(7*24*60*60*1000)
620Sstevel@tonic-gate struct phyint *
630Sstevel@tonic-gate phyint_lookup(char *name)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	struct phyint *pi;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (debug & D_PHYINT)
680Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "phyint_lookup(%s)\n", name);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
710Sstevel@tonic-gate 		if (strcmp(pi->pi_name, name) == 0)
720Sstevel@tonic-gate 			break;
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 	return (pi);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate struct phyint *
780Sstevel@tonic-gate phyint_lookup_on_index(uint_t ifindex)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	struct phyint *pi;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (debug & D_PHYINT)
830Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "phyint_lookup_on_index(%d)\n", ifindex);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
860Sstevel@tonic-gate 		if (pi->pi_index == ifindex)
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 	return (pi);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate struct phyint *
930Sstevel@tonic-gate phyint_create(char *name)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	struct phyint *pi;
960Sstevel@tonic-gate 	int i;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (debug & D_PHYINT)
990Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "phyint_create(%s)\n", name);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	pi = (struct phyint *)calloc(sizeof (struct phyint), 1);
1020Sstevel@tonic-gate 	if (pi == NULL) {
1030Sstevel@tonic-gate 		logmsg(LOG_ERR, "phyint_create: out of memory\n");
1040Sstevel@tonic-gate 		return (NULL);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	(void) strncpy(pi->pi_name, name, sizeof (pi->pi_name));
1070Sstevel@tonic-gate 	pi->pi_name[sizeof (pi->pi_name) - 1] = '\0';
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * Copy the defaults from the defaults array.
1110Sstevel@tonic-gate 	 * Do not copy the cf_notdefault fields since these have not
1120Sstevel@tonic-gate 	 * been explicitly set for the phyint.
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	for (i = 0; i < I_IFSIZE; i++)
1150Sstevel@tonic-gate 		pi->pi_config[i].cf_value = ifdefaults[i].cf_value;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * TmpDesyncFactor is used to desynchronize temporary token
1190Sstevel@tonic-gate 	 * generation among systems; the actual preferred lifetime value
1200Sstevel@tonic-gate 	 * of a temporary address will be (TmpPreferredLifetime -
1210Sstevel@tonic-gate 	 * TmpDesyncFactor).  It's a random value, with a user-configurable
1220Sstevel@tonic-gate 	 * maximum value.  The value is constant throughout the lifetime
1230Sstevel@tonic-gate 	 * of the in.ndpd process, but can change if the daemon is restarted,
1240Sstevel@tonic-gate 	 * per RFC3041.
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	if (pi->pi_TmpMaxDesyncFactor != 0) {
1270Sstevel@tonic-gate 		time_t seed = time(NULL);
1280Sstevel@tonic-gate 		srand((uint_t)seed);
1290Sstevel@tonic-gate 		pi->pi_TmpDesyncFactor = rand() % pi->pi_TmpMaxDesyncFactor;
1300Sstevel@tonic-gate 		/* we actually want [1,max], not [0,(max-1)] */
1310Sstevel@tonic-gate 		pi->pi_TmpDesyncFactor++;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	pi->pi_TmpRegenCountdown = TIMER_INFINITY;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	pi->pi_sock = -1;
1360Sstevel@tonic-gate 	if (phyint_init_from_k(pi) == -1) {
1370Sstevel@tonic-gate 		if (pi->pi_group_name != NULL)
1380Sstevel@tonic-gate 			free(pi->pi_group_name);
1390Sstevel@tonic-gate 		free(pi);
1400Sstevel@tonic-gate 		return (NULL);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	phyint_insert(pi);
1430Sstevel@tonic-gate 	if (pi->pi_sock != -1) {
1440Sstevel@tonic-gate 		if (poll_add(pi->pi_sock) == -1) {
1450Sstevel@tonic-gate 			phyint_delete(pi);
1460Sstevel@tonic-gate 			return (NULL);
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	return (pi);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /* Insert in linked list */
1530Sstevel@tonic-gate static void
1540Sstevel@tonic-gate phyint_insert(struct phyint *pi)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	/* Insert in list */
1570Sstevel@tonic-gate 	pi->pi_next = phyints;
1580Sstevel@tonic-gate 	pi->pi_prev = NULL;
1590Sstevel@tonic-gate 	if (phyints)
1600Sstevel@tonic-gate 		phyints->pi_prev = pi;
1610Sstevel@tonic-gate 	phyints = pi;
162*3284Sapersson 	num_of_phyints++;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * Initialize both the phyint data structure and the pi_sock for
1670Sstevel@tonic-gate  * sending and receving on the interface.
1680Sstevel@tonic-gate  * Extract information from the kernel (if present) and set pi_kernel_state.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate int
1710Sstevel@tonic-gate phyint_init_from_k(struct phyint *pi)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	struct ipv6_mreq v6mcastr;
1740Sstevel@tonic-gate 	struct lifreq lifr;
1750Sstevel@tonic-gate 	int fd;
1760Sstevel@tonic-gate 	boolean_t newsock;
1770Sstevel@tonic-gate 	uint_t ttl;
1780Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (debug & D_PHYINT)
1810Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "phyint_init_from_k(%s)\n", pi->pi_name);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate start_over:
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (pi->pi_sock < 0) {
1860Sstevel@tonic-gate 		pi->pi_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
1870Sstevel@tonic-gate 		if (pi->pi_sock < 0) {
1880Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: socket");
1890Sstevel@tonic-gate 			return (-1);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 		newsock = _B_TRUE;
1920Sstevel@tonic-gate 	} else {
1930Sstevel@tonic-gate 		newsock = _B_FALSE;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	fd = pi->pi_sock;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
1980Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
1990Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFINDEX, (char *)&lifr) < 0) {
2000Sstevel@tonic-gate 		if (errno == ENXIO) {
2010Sstevel@tonic-gate 			if (newsock) {
2020Sstevel@tonic-gate 				(void) close(pi->pi_sock);
2030Sstevel@tonic-gate 				pi->pi_sock = -1;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 			if (debug & D_PHYINT) {
2060Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "phyint_init_from_k(%s): "
2070Sstevel@tonic-gate 				    "not exist\n", pi->pi_name);
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 			return (0);
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: SIOCGLIFINDEX");
2120Sstevel@tonic-gate 		goto error;
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (!newsock && (pi->pi_index != lifr.lifr_index)) {
2160Sstevel@tonic-gate 		/*
2170Sstevel@tonic-gate 		 * Interface has been re-plumbed, lets open a new socket.
2180Sstevel@tonic-gate 		 * This situation can occur if plumb/unplumb are happening
2190Sstevel@tonic-gate 		 * quite frequently.
2200Sstevel@tonic-gate 		 */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		phyint_cleanup(pi);
2230Sstevel@tonic-gate 		goto start_over;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	pi->pi_index = lifr.lifr_index;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
2290Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: ioctl (get flags)");
2300Sstevel@tonic-gate 		goto error;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	pi->pi_flags = lifr.lifr_flags;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2352546Scarlsonj 	 * If the link local interface is not up yet or it's IFF_UP and the
2362546Scarlsonj 	 * IFF_NOLOCAL flag is set, then ignore the interface.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	if (!(pi->pi_flags & IFF_UP) || (pi->pi_flags & IFF_NOLOCAL)) {
2390Sstevel@tonic-gate 		if (newsock) {
2400Sstevel@tonic-gate 			(void) close(pi->pi_sock);
2410Sstevel@tonic-gate 			pi->pi_sock = -1;
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 		if (debug & D_PHYINT) {
2440Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "phyint_init_from_k(%s): "
2452546Scarlsonj 			    "IFF_NOLOCAL or not IFF_UP\n", pi->pi_name);
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 		return (0);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	pi->pi_kernel_state |= PI_PRESENT;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	bzero(lifr.lifr_groupname, sizeof (lifr.lifr_groupname));
2520Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFGROUPNAME, (caddr_t)&lifr) < 0) {
2530Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: ioctl (get group name)");
2540Sstevel@tonic-gate 		goto error;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (lifr.lifr_groupname != NULL && strlen(lifr.lifr_groupname) != 0) {
2580Sstevel@tonic-gate 		if (pi->pi_group_name == NULL) {
2590Sstevel@tonic-gate 			pi->pi_group_name = malloc(
2600Sstevel@tonic-gate 			    sizeof (lifr.lifr_groupname));
2610Sstevel@tonic-gate 			if (pi->pi_group_name == NULL) {
2620Sstevel@tonic-gate 				logperror_pi(pi, "phyint_init_from_k:"
2630Sstevel@tonic-gate 				    " malloc(group name)");
2640Sstevel@tonic-gate 				goto error;
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 		}
2670Sstevel@tonic-gate 		/*
2680Sstevel@tonic-gate 		 * Size of the group name can only be LIFNAMESZ -1 characters
2690Sstevel@tonic-gate 		 * which is ensured by kernel. Thus, we don't need strncpy.
2700Sstevel@tonic-gate 		 */
2710Sstevel@tonic-gate 		(void) strncpy(pi->pi_group_name, lifr.lifr_groupname,
2720Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
2730Sstevel@tonic-gate 		pi->pi_group_name[sizeof (pi->pi_group_name) - 1] = '\0';
2740Sstevel@tonic-gate 	} else if (pi->pi_group_name != NULL) {
2750Sstevel@tonic-gate 		free(pi->pi_group_name);
2760Sstevel@tonic-gate 		pi->pi_group_name = NULL;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFMTU, (caddr_t)&lifr) < 0) {
2800Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: ioctl (get mtu)");
2810Sstevel@tonic-gate 		goto error;
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 	pi->pi_mtu = lifr.lifr_mtu;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFADDR, (char *)&lifr) < 0) {
2860Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: SIOCGLIFADDR");
2870Sstevel@tonic-gate 		goto error;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
2900Sstevel@tonic-gate 	pi->pi_ifaddr = sin6->sin6_addr;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFTOKEN, (char *)&lifr) < 0) {
2930Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: SIOCGLIFTOKEN");
2940Sstevel@tonic-gate 		goto error;
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	/* Ignore interface if the token is all zeros */
2970Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_token;
2980Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2990Sstevel@tonic-gate 		logmsg(LOG_ERR, "ignoring interface %s: zero token\n",
3000Sstevel@tonic-gate 		    pi->pi_name);
3010Sstevel@tonic-gate 		goto error;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	pi->pi_token = sin6->sin6_addr;
3040Sstevel@tonic-gate 	pi->pi_token_length = lifr.lifr_addrlen;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Guess a remote token for POINTOPOINT by looking at
3080Sstevel@tonic-gate 	 * the link-local destination address.
3090Sstevel@tonic-gate 	 */
3100Sstevel@tonic-gate 	if (pi->pi_flags & IFF_POINTOPOINT) {
3110Sstevel@tonic-gate 		if (ioctl(fd, SIOCGLIFDSTADDR, (char *)&lifr) < 0) {
3120Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: SIOCGLIFDSTADDR");
3130Sstevel@tonic-gate 			goto error;
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3160Sstevel@tonic-gate 		if (sin6->sin6_family != AF_INET6 ||
3170Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
3180Sstevel@tonic-gate 		    !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3190Sstevel@tonic-gate 			pi->pi_dst_token = in6addr_any;
3200Sstevel@tonic-gate 		} else {
3210Sstevel@tonic-gate 			pi->pi_dst_token = sin6->sin6_addr;
3220Sstevel@tonic-gate 			/* Clear link-local prefix (first 10 bits) */
3230Sstevel@tonic-gate 			pi->pi_dst_token.s6_addr[0] = 0;
3240Sstevel@tonic-gate 			pi->pi_dst_token.s6_addr[1] &= 0x3f;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 	} else {
3270Sstevel@tonic-gate 		pi->pi_dst_token = in6addr_any;
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* Get link-layer address */
3310Sstevel@tonic-gate 	if (!(pi->pi_flags & IFF_MULTICAST) ||
3320Sstevel@tonic-gate 	    (pi->pi_flags & IFF_POINTOPOINT)) {
3330Sstevel@tonic-gate 		pi->pi_hdw_addr_len = 0;
3340Sstevel@tonic-gate 	} else {
3350Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_nd.lnr_addr;
3360Sstevel@tonic-gate 		bzero(sin6, sizeof (struct sockaddr_in6));
3370Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
3380Sstevel@tonic-gate 		sin6->sin6_addr = pi->pi_ifaddr;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		if (ioctl(fd, SIOCLIFGETND, (char *)&lifr) < 0) {
3410Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: SIOCLIFGETND");
3420Sstevel@tonic-gate 			goto error;
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		pi->pi_hdw_addr_len = lifr.lifr_nd.lnr_hdw_len;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		if (lifr.lifr_nd.lnr_hdw_len != 0) {
3480Sstevel@tonic-gate 			bcopy((char *)lifr.lifr_nd.lnr_hdw_addr,
3490Sstevel@tonic-gate 			    (char *)pi->pi_hdw_addr,
3500Sstevel@tonic-gate 			    lifr.lifr_nd.lnr_hdw_len);
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (newsock) {
3550Sstevel@tonic-gate 		icmp6_filter_t filter;
3560Sstevel@tonic-gate 		int on = 1;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		/* Set default values */
3590Sstevel@tonic-gate 		pi->pi_LinkMTU = pi->pi_mtu;
3600Sstevel@tonic-gate 		pi->pi_CurHopLimit = 0;
3610Sstevel@tonic-gate 		pi->pi_BaseReachableTime = ND_REACHABLE_TIME;
3620Sstevel@tonic-gate 		phyint_reach_random(pi, _B_FALSE);
3630Sstevel@tonic-gate 		pi->pi_RetransTimer = ND_RETRANS_TIMER;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		/* Setup socket for transmission and reception */
3660Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6,
3670Sstevel@tonic-gate 		    IPV6_BOUND_IF, (char *)&pi->pi_index,
3680Sstevel@tonic-gate 		    sizeof (pi->pi_index)) < 0) {
3690Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
3700Sstevel@tonic-gate 			    "IPV6_BOUND_IF");
3710Sstevel@tonic-gate 			goto error;
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		ttl = IPV6_MAX_HOPS;
3750Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
3760Sstevel@tonic-gate 		    (char *)&ttl, sizeof (ttl)) < 0) {
3770Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
3780Sstevel@tonic-gate 			    "IPV6_UNICAST_HOPS");
3790Sstevel@tonic-gate 			goto error;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
3830Sstevel@tonic-gate 		    (char *)&ttl, sizeof (ttl)) < 0) {
3840Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
3850Sstevel@tonic-gate 			    "IPV6_MULTICAST_HOPS");
3860Sstevel@tonic-gate 			goto error;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		v6mcastr.ipv6mr_multiaddr = all_nodes_mcast;
3900Sstevel@tonic-gate 		v6mcastr.ipv6mr_interface = pi->pi_index;
3910Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
3920Sstevel@tonic-gate 		    (char *)&v6mcastr, sizeof (v6mcastr)) < 0) {
3930Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: "
3940Sstevel@tonic-gate 			    "setsockopt IPV6_JOIN_GROUP");
3950Sstevel@tonic-gate 			goto error;
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 		pi->pi_state |= PI_JOINED_ALLNODES;
3980Sstevel@tonic-gate 		pi->pi_kernel_state |= PI_JOINED_ALLNODES;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		/*
4010Sstevel@tonic-gate 		 * Filter out so that we only receive router advertisements and
4020Sstevel@tonic-gate 		 * router solicitations.
4030Sstevel@tonic-gate 		 */
4040Sstevel@tonic-gate 		ICMP6_FILTER_SETBLOCKALL(&filter);
4050Sstevel@tonic-gate 		ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
4060Sstevel@tonic-gate 		ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER,
4090Sstevel@tonic-gate 		    (char *)&filter, sizeof (filter)) < 0) {
4100Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
4110Sstevel@tonic-gate 			    "ICMP6_FILTER");
4120Sstevel@tonic-gate 			goto error;
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		/* Enable receipt of ancillary data */
4160Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
4170Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
4180Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
4190Sstevel@tonic-gate 			    "IPV6_RECVHOPLIMIT");
4200Sstevel@tonic-gate 			goto error;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVRTHDR,
4230Sstevel@tonic-gate 		    (char *)&on, sizeof (on)) < 0) {
4240Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
4250Sstevel@tonic-gate 			    "IPV6_RECVRTHDR");
4260Sstevel@tonic-gate 			goto error;
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (pi->pi_AdvSendAdvertisements &&
4310Sstevel@tonic-gate 	    !(pi->pi_kernel_state & PI_JOINED_ALLROUTERS)) {
4320Sstevel@tonic-gate 		v6mcastr.ipv6mr_multiaddr = all_routers_mcast;
4330Sstevel@tonic-gate 		v6mcastr.ipv6mr_interface = pi->pi_index;
4340Sstevel@tonic-gate 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
4350Sstevel@tonic-gate 		    (char *)&v6mcastr, sizeof (v6mcastr)) < 0) {
4360Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: setsockopt "
4370Sstevel@tonic-gate 			    "IPV6_JOIN_GROUP");
4380Sstevel@tonic-gate 			goto error;
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		pi->pi_state |= PI_JOINED_ALLROUTERS;
4410Sstevel@tonic-gate 		pi->pi_kernel_state |= PI_JOINED_ALLROUTERS;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * If not already set, set the IFF_ROUTER interface flag based on
4450Sstevel@tonic-gate 	 * AdvSendAdvertisements.  Note that this will also enable IPv6
4460Sstevel@tonic-gate 	 * forwarding on the interface.  We don't clear IFF_ROUTER if we're
4470Sstevel@tonic-gate 	 * not advertising on an interface, because we could still be
4480Sstevel@tonic-gate 	 * forwarding on those interfaces.
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
4510Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
4520Sstevel@tonic-gate 	if (ioctl(fd, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
4530Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: SIOCGLIFFLAGS");
4540Sstevel@tonic-gate 		goto error;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	if (!(lifr.lifr_flags & IFF_ROUTER) && pi->pi_AdvSendAdvertisements) {
4570Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_ROUTER;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		if (ioctl(fd, SIOCSLIFFLAGS, (char *)&lifr) < 0) {
4600Sstevel@tonic-gate 			logperror_pi(pi, "phyint_init_from_k: SIOCSLIFFLAGS");
4610Sstevel@tonic-gate 			goto error;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 		pi->pi_flags = lifr.lifr_flags;
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* Set linkinfo parameters */
4670Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
4680Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
4690Sstevel@tonic-gate 	lifr.lifr_ifinfo.lir_maxhops = pi->pi_CurHopLimit;
4700Sstevel@tonic-gate 	lifr.lifr_ifinfo.lir_reachtime = pi->pi_ReachableTime;
4710Sstevel@tonic-gate 	lifr.lifr_ifinfo.lir_reachretrans = pi->pi_RetransTimer;
4721534Spwernau 	/* Setting maxmtu to 0 means that we're leaving the MTU alone */
4731534Spwernau 	lifr.lifr_ifinfo.lir_maxmtu = 0;
4740Sstevel@tonic-gate 	if (ioctl(fd, SIOCSLIFLNKINFO, (char *)&lifr) < 0) {
4750Sstevel@tonic-gate 		logperror_pi(pi, "phyint_init_from_k: SIOCSLIFLNKINFO");
4760Sstevel@tonic-gate 		goto error;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	if (debug & D_PHYINT) {
4790Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "phyint_init_from_k(%s): done\n",
4800Sstevel@tonic-gate 		    pi->pi_name);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	return (0);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate error:
4850Sstevel@tonic-gate 	/* Pretend the interface does not exist in the kernel */
4860Sstevel@tonic-gate 	pi->pi_kernel_state &= ~PI_PRESENT;
4870Sstevel@tonic-gate 	if (newsock) {
4880Sstevel@tonic-gate 		(void) close(pi->pi_sock);
4890Sstevel@tonic-gate 		pi->pi_sock = -1;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 	return (-1);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * Delete (unlink and free).
4960Sstevel@tonic-gate  * Handles delete of things that have not yet been inserted in the list.
4970Sstevel@tonic-gate  */
4980Sstevel@tonic-gate void
4990Sstevel@tonic-gate phyint_delete(struct phyint *pi)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	if (debug & D_PHYINT)
5020Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "phyint_delete(%s)\n", pi->pi_name);
5030Sstevel@tonic-gate 
504*3284Sapersson 	assert(num_of_phyints > 0);
505*3284Sapersson 
5060Sstevel@tonic-gate 	while (pi->pi_router_list)
5070Sstevel@tonic-gate 		router_delete(pi->pi_router_list);
5080Sstevel@tonic-gate 	while (pi->pi_prefix_list)
5090Sstevel@tonic-gate 		prefix_delete(pi->pi_prefix_list);
5100Sstevel@tonic-gate 	while (pi->pi_adv_prefix_list)
5110Sstevel@tonic-gate 		adv_prefix_delete(pi->pi_adv_prefix_list);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (pi->pi_sock != -1) {
5140Sstevel@tonic-gate 		(void) poll_remove(pi->pi_sock);
5150Sstevel@tonic-gate 		if (close(pi->pi_sock) < 0) {
5160Sstevel@tonic-gate 			logperror_pi(pi, "phyint_delete: close");
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 		pi->pi_sock = -1;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (pi->pi_prev == NULL) {
5220Sstevel@tonic-gate 		if (phyints == pi)
5230Sstevel@tonic-gate 			phyints = pi->pi_next;
5240Sstevel@tonic-gate 	} else {
5250Sstevel@tonic-gate 		pi->pi_prev->pi_next = pi->pi_next;
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 	if (pi->pi_next != NULL)
5280Sstevel@tonic-gate 		pi->pi_next->pi_prev = pi->pi_prev;
5290Sstevel@tonic-gate 	pi->pi_next = pi->pi_prev = NULL;
5300Sstevel@tonic-gate 	if (pi->pi_group_name != NULL)
5310Sstevel@tonic-gate 		free(pi->pi_group_name);
5320Sstevel@tonic-gate 	free(pi);
533*3284Sapersson 	num_of_phyints--;
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate /*
5370Sstevel@tonic-gate  * Called with the number of millseconds elapsed since the last call.
5380Sstevel@tonic-gate  * Determines if any timeout event has occurred and
5390Sstevel@tonic-gate  * returns the number of milliseconds until the next timeout event
5400Sstevel@tonic-gate  * for the phyint iself (excluding prefixes and routers).
5410Sstevel@tonic-gate  * Returns TIMER_INFINITY for "never".
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate uint_t
5440Sstevel@tonic-gate phyint_timer(struct phyint *pi, uint_t elapsed)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	uint_t next = TIMER_INFINITY;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if (pi->pi_AdvSendAdvertisements) {
5490Sstevel@tonic-gate 		if (pi->pi_adv_state != NO_ADV) {
5500Sstevel@tonic-gate 			int old_state = pi->pi_adv_state;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 			if (debug & (D_STATE|D_PHYINT)) {
5530Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "phyint_timer ADV(%s) "
5540Sstevel@tonic-gate 				    "state %d\n", pi->pi_name, (int)old_state);
5550Sstevel@tonic-gate 			}
5560Sstevel@tonic-gate 			next = advertise_event(pi, ADV_TIMER, elapsed);
5570Sstevel@tonic-gate 			if (debug & D_STATE) {
5580Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "phyint_timer ADV(%s) "
5590Sstevel@tonic-gate 				    "state %d -> %d\n",
5600Sstevel@tonic-gate 				    pi->pi_name, (int)old_state,
5610Sstevel@tonic-gate 				    (int)pi->pi_adv_state);
5620Sstevel@tonic-gate 			}
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 	} else {
5650Sstevel@tonic-gate 		if (pi->pi_sol_state != NO_SOLICIT) {
5660Sstevel@tonic-gate 			int old_state = pi->pi_sol_state;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 			if (debug & (D_STATE|D_PHYINT)) {
5690Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "phyint_timer SOL(%s) "
5700Sstevel@tonic-gate 				    "state %d\n", pi->pi_name, (int)old_state);
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 			next = solicit_event(pi, SOL_TIMER, elapsed);
5730Sstevel@tonic-gate 			if (debug & D_STATE) {
5740Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "phyint_timer SOL(%s) "
5750Sstevel@tonic-gate 				    "state %d -> %d\n",
5760Sstevel@tonic-gate 				    pi->pi_name, (int)old_state,
5770Sstevel@tonic-gate 				    (int)pi->pi_sol_state);
5780Sstevel@tonic-gate 			}
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	/*
5830Sstevel@tonic-gate 	 * If the phyint has been unplumbed, we don't want to call
5840Sstevel@tonic-gate 	 * phyint_reach_random. We will be in the NO_ADV or NO_SOLICIT state.
5850Sstevel@tonic-gate 	 */
5860Sstevel@tonic-gate 	if ((pi->pi_AdvSendAdvertisements && (pi->pi_adv_state != NO_ADV)) ||
5870Sstevel@tonic-gate 	    (!pi->pi_AdvSendAdvertisements &&
5880Sstevel@tonic-gate 	    (pi->pi_sol_state != NO_SOLICIT))) {
5890Sstevel@tonic-gate 		pi->pi_reach_time_since_random += elapsed;
5900Sstevel@tonic-gate 		if (pi->pi_reach_time_since_random >= MAX_REACH_RANDOM_INTERVAL)
5910Sstevel@tonic-gate 			phyint_reach_random(pi, _B_TRUE);
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	return (next);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate static void
5980Sstevel@tonic-gate phyint_print(struct phyint *pi)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	struct prefix *pr;
6010Sstevel@tonic-gate 	struct adv_prefix *adv_pr;
6020Sstevel@tonic-gate 	struct router *dr;
6030Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
6040Sstevel@tonic-gate 	char llabuf[BUFSIZ];
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "Phyint %s index %d state %x, kernel %x, "
6071577Sseb 	    "num routers %d\n",
6081577Sseb 	    pi->pi_name, pi->pi_index, pi->pi_state, pi->pi_kernel_state,
6090Sstevel@tonic-gate 	    pi->pi_num_k_routers);
6100Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\taddress: %s flags %x\n",
6110Sstevel@tonic-gate 	    inet_ntop(AF_INET6, (void *)&pi->pi_ifaddr,
6120Sstevel@tonic-gate 	    abuf, sizeof (abuf)), pi->pi_flags);
6130Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\tsock %d mtu %d hdw_addr len %d <%s>\n",
6140Sstevel@tonic-gate 	    pi->pi_sock, pi->pi_mtu, pi->pi_hdw_addr_len,
6150Sstevel@tonic-gate 	    ((pi->pi_hdw_addr_len != 0) ?
6160Sstevel@tonic-gate 	    fmt_lla(llabuf, sizeof (llabuf), pi->pi_hdw_addr,
6170Sstevel@tonic-gate 	    pi->pi_hdw_addr_len) : "none"));
6180Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\ttoken: len %d %s\n",
6190Sstevel@tonic-gate 	    pi->pi_token_length,
6200Sstevel@tonic-gate 	    inet_ntop(AF_INET6, (void *)&pi->pi_token,
6210Sstevel@tonic-gate 	    abuf, sizeof (abuf)));
6220Sstevel@tonic-gate 	if (pi->pi_TmpAddrsEnabled) {
6230Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "\ttmp_token: %s\n",
6240Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pi->pi_tmp_token,
6250Sstevel@tonic-gate 			abuf, sizeof (abuf)));
6260Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "\ttmp config: pref %d valid %d "
6270Sstevel@tonic-gate 		    "maxdesync %d desync %d regen %d\n",
6280Sstevel@tonic-gate 		    pi->pi_TmpPreferredLifetime, pi->pi_TmpValidLifetime,
6290Sstevel@tonic-gate 		    pi->pi_TmpMaxDesyncFactor, pi->pi_TmpDesyncFactor,
6300Sstevel@tonic-gate 		    pi->pi_TmpRegenAdvance);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	if (pi->pi_flags & IFF_POINTOPOINT) {
6330Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "\tdst_token: %s\n",
6340Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pi->pi_dst_token,
6350Sstevel@tonic-gate 			abuf, sizeof (abuf)));
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\tLinkMTU %d CurHopLimit %d "
6380Sstevel@tonic-gate 	    "BaseReachableTime %d\n\tReachableTime %d RetransTimer %d\n",
6390Sstevel@tonic-gate 	    pi->pi_LinkMTU, pi->pi_CurHopLimit, pi->pi_BaseReachableTime,
6400Sstevel@tonic-gate 	    pi->pi_ReachableTime, pi->pi_RetransTimer);
6410Sstevel@tonic-gate 	if (!pi->pi_AdvSendAdvertisements) {
6420Sstevel@tonic-gate 		/* Solicit state */
6430Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "\tSOLICIT: time_left %d state %d count %d\n",
6440Sstevel@tonic-gate 		    pi->pi_sol_time_left, pi->pi_sol_state, pi->pi_sol_count);
6450Sstevel@tonic-gate 	} else {
6460Sstevel@tonic-gate 		/* Advertise state */
6470Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "\tADVERT: time_left %d state %d count %d "
6480Sstevel@tonic-gate 		    "since last %d\n",
6490Sstevel@tonic-gate 		    pi->pi_adv_time_left, pi->pi_adv_state, pi->pi_adv_count,
6500Sstevel@tonic-gate 		    pi->pi_adv_time_since_sent);
6510Sstevel@tonic-gate 		print_iflist(pi->pi_config);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 	for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next)
6540Sstevel@tonic-gate 		prefix_print(pr);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
6570Sstevel@tonic-gate 	    adv_pr = adv_pr->adv_pr_next) {
6580Sstevel@tonic-gate 		adv_prefix_print(adv_pr);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	for (dr = pi->pi_router_list; dr != NULL; dr = dr->dr_next)
6620Sstevel@tonic-gate 		router_print(dr);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\n");
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  * Randomize pi->pi_ReachableTime.
6690Sstevel@tonic-gate  * Done periodically when there are no RAs and at a maximum frequency when
6700Sstevel@tonic-gate  * RA's arrive.
6710Sstevel@tonic-gate  * Assumes that caller has determined that it is time to generate
6720Sstevel@tonic-gate  * a new random ReachableTime.
6730Sstevel@tonic-gate  */
6740Sstevel@tonic-gate void
6750Sstevel@tonic-gate phyint_reach_random(struct phyint *pi, boolean_t set_needed)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	pi->pi_ReachableTime = GET_RANDOM(
6780Sstevel@tonic-gate 	    (int)(ND_MIN_RANDOM_FACTOR * pi->pi_BaseReachableTime),
6790Sstevel@tonic-gate 	    (int)(ND_MAX_RANDOM_FACTOR * pi->pi_BaseReachableTime));
6800Sstevel@tonic-gate 	if (set_needed) {
6810Sstevel@tonic-gate 		struct lifreq lifr;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pi->pi_name,
6840Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
6850Sstevel@tonic-gate 		pi->pi_name[sizeof (pi->pi_name) - 1] = '\0';
6860Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCGLIFLNKINFO, (char *)&lifr) < 0) {
6870Sstevel@tonic-gate 			logperror_pi(pi,
6880Sstevel@tonic-gate 			    "phyint_reach_random: SIOCGLIFLNKINFO");
6890Sstevel@tonic-gate 			return;
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 		lifr.lifr_ifinfo.lir_reachtime = pi->pi_ReachableTime;
6920Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCSLIFLNKINFO, (char *)&lifr) < 0) {
6930Sstevel@tonic-gate 			logperror_pi(pi,
6940Sstevel@tonic-gate 			    "phyint_reach_random: SIOCSLIFLNKINFO");
6950Sstevel@tonic-gate 			return;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 	pi->pi_reach_time_since_random = 0;
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /*
7020Sstevel@tonic-gate  * Validate a temporary token against a list of known bad values.
7030Sstevel@tonic-gate  * Currently assumes that token is 8 bytes long!  Current known
7040Sstevel@tonic-gate  * bad values include 0, reserved anycast tokens (RFC 2526), tokens
7050Sstevel@tonic-gate  * used by ISATAP (draft-ietf-ngtrans-isatap-N), any token already
7060Sstevel@tonic-gate  * assigned to this interface, or any token for which the global
7070Sstevel@tonic-gate  * bit is set.
7080Sstevel@tonic-gate  *
7090Sstevel@tonic-gate  * Called by tmptoken_create().
7100Sstevel@tonic-gate  *
7110Sstevel@tonic-gate  * Return _B_TRUE if token is valid (no match), _B_FALSE if not.
7120Sstevel@tonic-gate  */
7130Sstevel@tonic-gate static boolean_t
7140Sstevel@tonic-gate tmptoken_isvalid(struct in6_addr *token)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	struct phyint *pi;
7170Sstevel@tonic-gate 	struct in6_addr mask;
7180Sstevel@tonic-gate 	struct in6_addr isatap = { 0, 0, 0, 0, 0, 0, 0, 0, \
7190Sstevel@tonic-gate 				    0, 0, 0x5e, 0xfe, 0, 0, 0, 0 };
7200Sstevel@tonic-gate 	struct in6_addr anycast = { 0, 0, 0, 0, \
7210Sstevel@tonic-gate 				    0, 0, 0, 0, \
7220Sstevel@tonic-gate 				    0xfd, 0xff, 0xff, 0xff, \
7230Sstevel@tonic-gate 				    0xff, 0xff, 0xff, 0x80 };
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(token))
7260Sstevel@tonic-gate 		return (_B_FALSE);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	if (token->s6_addr[8] & 0x2)
7290Sstevel@tonic-gate 		return (_B_FALSE);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	(void) memcpy(&mask, token, sizeof (mask));
7320Sstevel@tonic-gate 	mask._S6_un._S6_u32[3] = 0;
7330Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(&isatap, token))
7340Sstevel@tonic-gate 		return (_B_FALSE);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	mask._S6_un._S6_u32[3] = token->_S6_un._S6_u32[3] & 0xffffff80;
7370Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(&anycast, token))
7380Sstevel@tonic-gate 		return (_B_FALSE);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
7410Sstevel@tonic-gate 		if (((pi->pi_token_length == TMP_TOKEN_BITS) &&
7420Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&pi->pi_token, token)) ||
7430Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&pi->pi_tmp_token, token))
7440Sstevel@tonic-gate 			return (_B_FALSE);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/* none of our tests failed, must be a good one! */
7480Sstevel@tonic-gate 	return (_B_TRUE);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate /*
7520Sstevel@tonic-gate  * Generate a temporary token and set up its timer
7530Sstevel@tonic-gate  *
7540Sstevel@tonic-gate  * Called from incoming_prefix_addrconf_process() (when token is first
7550Sstevel@tonic-gate  * needed) and from tmptoken_timer() (when current token expires).
7560Sstevel@tonic-gate  *
7570Sstevel@tonic-gate  * Returns _B_TRUE if a token was successfully generated, _B_FALSE if not.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate boolean_t
7600Sstevel@tonic-gate tmptoken_create(struct phyint *pi)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	int fd, i = 0, max_tries = 15;
7630Sstevel@tonic-gate 	struct in6_addr token;
7640Sstevel@tonic-gate 	uint32_t *tokenp = &(token._S6_un._S6_u32[2]);
7650Sstevel@tonic-gate 	char buf[INET6_ADDRSTRLEN];
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
7680Sstevel@tonic-gate 		perror("open /dev/urandom");
7690Sstevel@tonic-gate 		goto no_token;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	bzero((char *)&token, sizeof (token));
7730Sstevel@tonic-gate 	do {
7740Sstevel@tonic-gate 		if (read(fd, (void *)tokenp, TMP_TOKEN_BYTES) == -1) {
7750Sstevel@tonic-gate 			perror("read /dev/urandom");
7760Sstevel@tonic-gate 			(void) close(fd);
7770Sstevel@tonic-gate 			goto no_token;
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		/*
7810Sstevel@tonic-gate 		 * Assume EUI-64 formatting, and thus 64-bit
7820Sstevel@tonic-gate 		 * token len; need to clear global bit.
7830Sstevel@tonic-gate 		 */
7840Sstevel@tonic-gate 		token.s6_addr[8] &= 0xfd;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		i++;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	} while (!tmptoken_isvalid(&token) && i < max_tries);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	(void) close(fd);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (i == max_tries) {
7930Sstevel@tonic-gate no_token:
7940Sstevel@tonic-gate 		logmsg(LOG_WARNING, "tmptoken_create(%s): failed to create "
7950Sstevel@tonic-gate 		    "token; disabling temporary addresses on %s\n",
7960Sstevel@tonic-gate 		    pi->pi_name, pi->pi_name);
7970Sstevel@tonic-gate 		pi->pi_TmpAddrsEnabled = 0;
7980Sstevel@tonic-gate 		return (_B_FALSE);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	pi->pi_tmp_token = token;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (debug & D_TMP)
8040Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "tmptoken_create(%s): created temporary "
8050Sstevel@tonic-gate 		    "token %s\n", pi->pi_name,
8060Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &pi->pi_tmp_token, buf, sizeof (buf)));
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	pi->pi_TmpRegenCountdown = (pi->pi_TmpPreferredLifetime -
8090Sstevel@tonic-gate 	    pi->pi_TmpDesyncFactor - pi->pi_TmpRegenAdvance) * MILLISEC;
8100Sstevel@tonic-gate 	if (pi->pi_TmpRegenCountdown != 0)
8110Sstevel@tonic-gate 		timer_schedule(pi->pi_TmpRegenCountdown);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	return (_B_TRUE);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate  * Delete a temporary token.  This is outside the normal timeout process,
8180Sstevel@tonic-gate  * so mark any existing addresses based on this token DEPRECATED and set
8190Sstevel@tonic-gate  * their preferred lifetime to 0.  Don't tamper with valid lifetime, that
8200Sstevel@tonic-gate  * will be used to eventually remove the address.  Also reset the current
8210Sstevel@tonic-gate  * pi_tmp_token value to 0.
8220Sstevel@tonic-gate  *
8230Sstevel@tonic-gate  * Called from incoming_prefix_addrconf_process() if DAD fails on a temp
8240Sstevel@tonic-gate  * addr.
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate void
8270Sstevel@tonic-gate tmptoken_delete(struct phyint *pi)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate 	struct prefix *pr;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
8320Sstevel@tonic-gate 		if (!(pr->pr_flags & IFF_TEMPORARY) ||
8330Sstevel@tonic-gate 		    (pr->pr_flags & IFF_DEPRECATED) ||
8340Sstevel@tonic-gate 		    (!token_equal(pr->pr_address, pi->pi_tmp_token,
8350Sstevel@tonic-gate 		    TMP_TOKEN_BITS))) {
8360Sstevel@tonic-gate 			continue;
8370Sstevel@tonic-gate 		}
8380Sstevel@tonic-gate 		pr->pr_PreferredLifetime = 0;
8390Sstevel@tonic-gate 		pr->pr_state |= PR_DEPRECATED;
8400Sstevel@tonic-gate 		prefix_update_k(pr);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	(void) memset(&pi->pi_tmp_token, 0, sizeof (pi->pi_tmp_token));
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * Called from run_timeouts() with the number of milliseconds elapsed
8480Sstevel@tonic-gate  * since the last call.  Determines if any timeout event has occurred
8490Sstevel@tonic-gate  * and returns the number of milliseconds until the next timeout event
8500Sstevel@tonic-gate  * for the tmp token.  Returns TIMER_INFINITY for "never".
8510Sstevel@tonic-gate  */
8520Sstevel@tonic-gate uint_t
8530Sstevel@tonic-gate tmptoken_timer(struct phyint *pi, uint_t elapsed)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	struct nd_opt_prefix_info opt;
8560Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
8570Sstevel@tonic-gate 	struct prefix *pr, *newpr;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (debug & D_TMP) {
8600Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "tmptoken_timer(%s, %d) regencountdown %d\n",
8610Sstevel@tonic-gate 		    pi->pi_name, (int)elapsed, pi->pi_TmpRegenCountdown);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 	if (!pi->pi_TmpAddrsEnabled ||
8640Sstevel@tonic-gate 	    (pi->pi_TmpRegenCountdown == TIMER_INFINITY))
8650Sstevel@tonic-gate 		return (TIMER_INFINITY);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if (pi->pi_TmpRegenCountdown > elapsed) {
8680Sstevel@tonic-gate 		pi->pi_TmpRegenCountdown -= elapsed;
8690Sstevel@tonic-gate 		return (pi->pi_TmpRegenCountdown);
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	/*
8730Sstevel@tonic-gate 	 * Tmp token timer has expired.  Start by generating a new token.
8740Sstevel@tonic-gate 	 * If we can't get a new token, tmp addrs are disabled on this
8750Sstevel@tonic-gate 	 * interface, so there's no need to continue, or to set a timer.
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	if (!tmptoken_create(pi))
8780Sstevel@tonic-gate 		return (TIMER_INFINITY);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * Now that we have a new token, walk the list of prefixes to
8820Sstevel@tonic-gate 	 * find which ones need a corresponding tmp addr generated.
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 	for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		if (!(pr->pr_state & PR_AUTO) || pr->pr_state & PR_STATIC ||
8870Sstevel@tonic-gate 		    pr->pr_state & PR_DEPRECATED ||
8880Sstevel@tonic-gate 		    pr->pr_flags & IFF_TEMPORARY)
8890Sstevel@tonic-gate 			continue;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 		newpr = prefix_create(pi, pr->pr_prefix, pr->pr_prefix_len,
8920Sstevel@tonic-gate 		    IFF_TEMPORARY);
8930Sstevel@tonic-gate 		if (newpr == NULL) {
8940Sstevel@tonic-gate 			char pbuf[INET6_ADDRSTRLEN];
8950Sstevel@tonic-gate 			char tbuf[INET6_ADDRSTRLEN];
8960Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, &pr->pr_prefix, pbuf,
8970Sstevel@tonic-gate 			    sizeof (pbuf));
8980Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, &pi->pi_tmp_token, tbuf,
8990Sstevel@tonic-gate 			    sizeof (tbuf));
9000Sstevel@tonic-gate 			logmsg(LOG_ERR, "can't create new tmp addr "
9010Sstevel@tonic-gate 			    "(%s, %s, %s)\n", pi->pi_name, pbuf, tbuf);
9020Sstevel@tonic-gate 			continue;
9030Sstevel@tonic-gate 		}
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 		/*
9060Sstevel@tonic-gate 		 * We want to use incoming_prefix_*_process() functions to
9070Sstevel@tonic-gate 		 * set up the new tmp addr, so cobble together a prefix
9080Sstevel@tonic-gate 		 * info option struct based on the existing prefix to pass
9090Sstevel@tonic-gate 		 * in.  The lifetimes will be based on the current time
9100Sstevel@tonic-gate 		 * remaining.
9110Sstevel@tonic-gate 		 *
9120Sstevel@tonic-gate 		 * The "from" param is only used for messages; pass in
9130Sstevel@tonic-gate 		 * ::0 for that.
9140Sstevel@tonic-gate 		 */
9150Sstevel@tonic-gate 		opt.nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
9160Sstevel@tonic-gate 		opt.nd_opt_pi_len = sizeof (opt) / 8;
9170Sstevel@tonic-gate 		opt.nd_opt_pi_prefix_len = pr->pr_prefix_len;
9180Sstevel@tonic-gate 		opt.nd_opt_pi_flags_reserved = ND_OPT_PI_FLAG_AUTO;
9190Sstevel@tonic-gate 		opt.nd_opt_pi_valid_time =
9200Sstevel@tonic-gate 		    htonl(pr->pr_ValidLifetime / 1000);
9210Sstevel@tonic-gate 		opt.nd_opt_pi_preferred_time =
9220Sstevel@tonic-gate 		    htonl(pr->pr_PreferredLifetime / 1000);
9230Sstevel@tonic-gate 		if (pr->pr_state & PR_ONLINK)
9240Sstevel@tonic-gate 			opt.nd_opt_pi_flags_reserved &= ND_OPT_PI_FLAG_ONLINK;
9250Sstevel@tonic-gate 		opt.nd_opt_pi_prefix = pr->pr_prefix;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		(void) memset(&sin6, 0, sizeof (sin6));
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		if (!incoming_prefix_addrconf_process(pi, newpr,
9300Sstevel@tonic-gate 		    (uchar_t *)&opt, &sin6, _B_FALSE, _B_TRUE)) {
9310Sstevel@tonic-gate 			char pbuf[INET6_ADDRSTRLEN];
9320Sstevel@tonic-gate 			char tbuf[INET6_ADDRSTRLEN];
9330Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, &pr->pr_prefix, pbuf,
9340Sstevel@tonic-gate 			    sizeof (pbuf));
9350Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, &pi->pi_tmp_token, tbuf,
9360Sstevel@tonic-gate 			    sizeof (tbuf));
9370Sstevel@tonic-gate 			logmsg(LOG_ERR, "can't create new tmp addr "
9380Sstevel@tonic-gate 			    "(%s, %s, %s)\n", pi->pi_name, pbuf, tbuf);
9390Sstevel@tonic-gate 			continue;
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		if (pr->pr_state & PR_ONLINK) {
9430Sstevel@tonic-gate 			incoming_prefix_onlink_process(newpr, (uchar_t *)&opt);
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/*
9480Sstevel@tonic-gate 	 * appropriate timers were scheduled when
9490Sstevel@tonic-gate 	 * the token and addresses were created.
9500Sstevel@tonic-gate 	 */
9510Sstevel@tonic-gate 	return (TIMER_INFINITY);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate /*
9550Sstevel@tonic-gate  * tlen specifies the token length in bits.  Compares the lower
9560Sstevel@tonic-gate  * tlen bits of the two addresses provided and returns _B_TRUE if
9570Sstevel@tonic-gate  * they match, _B_FALSE if not.  Also returns _B_FALSE for invalid
9580Sstevel@tonic-gate  * values of tlen.
9590Sstevel@tonic-gate  */
9600Sstevel@tonic-gate boolean_t
9610Sstevel@tonic-gate token_equal(struct in6_addr t1, struct in6_addr t2, int tlen)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	uchar_t mask;
9640Sstevel@tonic-gate 	int j, abytes, tbytes, tbits;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (tlen < 0 || tlen > IPV6_ABITS)
9670Sstevel@tonic-gate 		return (_B_FALSE);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	abytes = IPV6_ABITS >> 3;
9700Sstevel@tonic-gate 	tbytes = tlen >> 3;
9710Sstevel@tonic-gate 	tbits = tlen & 7;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	for (j = abytes - 1; j >= abytes - tbytes; j--)
9740Sstevel@tonic-gate 		if (t1.s6_addr[j] != t2.s6_addr[j])
9750Sstevel@tonic-gate 			return (_B_FALSE);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	if (tbits == 0)
9780Sstevel@tonic-gate 		return (_B_TRUE);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/* We only care about the tbits rightmost bits */
9810Sstevel@tonic-gate 	mask = 0xff >> (8 - tbits);
9820Sstevel@tonic-gate 	if ((t1.s6_addr[j] & mask) != (t2.s6_addr[j] & mask))
9830Sstevel@tonic-gate 		return (_B_FALSE);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	return (_B_TRUE);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate  * Lookup prefix structure that matches the prefix and prefix length.
9900Sstevel@tonic-gate  * Assumes that the bits after prefixlen might not be zero.
9910Sstevel@tonic-gate  */
9920Sstevel@tonic-gate static struct prefix *
9930Sstevel@tonic-gate prefix_lookup(struct phyint *pi, struct in6_addr prefix, int prefixlen)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	struct prefix *pr;
9960Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	if (debug & D_PREFIX) {
9990Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_lookup(%s, %s/%u)\n", pi->pi_name,
10000Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&prefix,
10010Sstevel@tonic-gate 		    abuf, sizeof (abuf)), prefixlen);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
10050Sstevel@tonic-gate 		if (pr->pr_prefix_len == prefixlen &&
10060Sstevel@tonic-gate 		    prefix_equal(prefix, pr->pr_prefix, prefixlen))
10070Sstevel@tonic-gate 			return (pr);
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 	return (NULL);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate /*
10130Sstevel@tonic-gate  * Compare two prefixes that have the same prefix length.
10140Sstevel@tonic-gate  * Fails if the prefix length is unreasonable.
10150Sstevel@tonic-gate  */
10160Sstevel@tonic-gate boolean_t
10170Sstevel@tonic-gate prefix_equal(struct in6_addr p1, struct in6_addr p2, int plen)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	uchar_t mask;
10200Sstevel@tonic-gate 	int j, pbytes, pbits;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (plen < 0 || plen > IPV6_ABITS)
10230Sstevel@tonic-gate 		return (_B_FALSE);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	pbytes = plen >> 3;
10260Sstevel@tonic-gate 	pbits = plen & 7;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	for (j = 0; j < pbytes; j++)
10290Sstevel@tonic-gate 		if (p1.s6_addr[j] != p2.s6_addr[j])
10300Sstevel@tonic-gate 			return (_B_FALSE);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	if (pbits == 0)
10330Sstevel@tonic-gate 		return (_B_TRUE);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	/* Make the N leftmost bits one */
10360Sstevel@tonic-gate 	mask = 0xff << (8 - pbits);
10370Sstevel@tonic-gate 	if ((p1.s6_addr[j] & mask) != (p2.s6_addr[j] & mask))
10380Sstevel@tonic-gate 		return (_B_FALSE);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	return (_B_TRUE);
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate /*
10440Sstevel@tonic-gate  * Set a prefix from an address and a prefix length.
10450Sstevel@tonic-gate  * Force all the bits after the prefix length to be zero.
10460Sstevel@tonic-gate  */
10470Sstevel@tonic-gate void
10480Sstevel@tonic-gate prefix_set(struct in6_addr *prefix, struct in6_addr addr, int prefix_len)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	uchar_t mask;
10510Sstevel@tonic-gate 	int j;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	if (prefix_len < 0 || prefix_len > IPV6_ABITS)
10540Sstevel@tonic-gate 		return;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	bzero((char *)prefix, sizeof (*prefix));
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	for (j = 0; prefix_len > 8; prefix_len -= 8, j++)
10590Sstevel@tonic-gate 		prefix->s6_addr[j] = addr.s6_addr[j];
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/* Make the N leftmost bits one */
10620Sstevel@tonic-gate 	mask = 0xff << (8 - prefix_len);
10630Sstevel@tonic-gate 	prefix->s6_addr[j] = addr.s6_addr[j] & mask;
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate /*
10670Sstevel@tonic-gate  * Lookup a prefix based on the kernel's interface name.
10680Sstevel@tonic-gate  */
10690Sstevel@tonic-gate struct prefix *
10700Sstevel@tonic-gate prefix_lookup_name(struct phyint *pi, char *name)
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate 	struct prefix *pr;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (debug & D_PREFIX) {
10750Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_lookup_name(%s, %s)\n",
10760Sstevel@tonic-gate 		    pi->pi_name, name);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 	if (name[0] == '\0')
10790Sstevel@tonic-gate 		return (NULL);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
10820Sstevel@tonic-gate 		if (strcmp(name, pr->pr_name) == 0)
10830Sstevel@tonic-gate 			return (pr);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 	return (NULL);
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate /*
10890Sstevel@tonic-gate  * Search the phyints list to make sure that this new prefix does
10900Sstevel@tonic-gate  * not already exist in any  other physical interfaces that have
10910Sstevel@tonic-gate  * the same address as this one
10920Sstevel@tonic-gate  */
10930Sstevel@tonic-gate struct prefix *
10940Sstevel@tonic-gate prefix_lookup_addr_match(struct prefix *pr)
10950Sstevel@tonic-gate {
10960Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
10970Sstevel@tonic-gate 	struct phyint *pi;
10980Sstevel@tonic-gate 	struct prefix *otherpr = NULL;
10990Sstevel@tonic-gate 	struct in6_addr prefix;
11000Sstevel@tonic-gate 	int	prefixlen;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	if (debug & D_PREFIX) {
11030Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_lookup_addr_match(%s/%u)\n",
11040Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pr->pr_address,
11050Sstevel@tonic-gate 		    abuf, sizeof (abuf)), pr->pr_prefix_len);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 	prefix = pr->pr_prefix;
11080Sstevel@tonic-gate 	prefixlen = pr->pr_prefix_len;
11090Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
11100Sstevel@tonic-gate 		otherpr = prefix_lookup(pi, prefix, prefixlen);
11110Sstevel@tonic-gate 		if (otherpr == pr)
11120Sstevel@tonic-gate 			continue;
11130Sstevel@tonic-gate 		if (otherpr != NULL && (otherpr->pr_state & PR_AUTO) &&
11140Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&pr->pr_address,
11150Sstevel@tonic-gate 		    &otherpr->pr_address))
11160Sstevel@tonic-gate 			return (otherpr);
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 	return (NULL);
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate /*
11220Sstevel@tonic-gate  * Initialize a new prefix without setting lifetimes etc.
11230Sstevel@tonic-gate  */
11240Sstevel@tonic-gate struct prefix *
11250Sstevel@tonic-gate prefix_create(struct phyint *pi, struct in6_addr prefix, int prefixlen,
11260Sstevel@tonic-gate     uint64_t flags)
11270Sstevel@tonic-gate {
11280Sstevel@tonic-gate 	struct prefix *pr;
11290Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (debug & D_PREFIX) {
11320Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_create(%s, %s/%u, 0x%llx)\n",
11330Sstevel@tonic-gate 		    pi->pi_name, inet_ntop(AF_INET6, (void *)&prefix,
11340Sstevel@tonic-gate 		    abuf, sizeof (abuf)), prefixlen, flags);
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 	pr = (struct prefix *)calloc(sizeof (struct prefix), 1);
11370Sstevel@tonic-gate 	if (pr == NULL) {
11380Sstevel@tonic-gate 		logmsg(LOG_ERR, "prefix_create: out of memory\n");
11390Sstevel@tonic-gate 		return (NULL);
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 	/*
11420Sstevel@tonic-gate 	 * The prefix might have non-zero bits after the prefix len bits.
11430Sstevel@tonic-gate 	 * Force them to be zero.
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	prefix_set(&pr->pr_prefix, prefix, prefixlen);
11460Sstevel@tonic-gate 	pr->pr_prefix_len = prefixlen;
11470Sstevel@tonic-gate 	pr->pr_PreferredLifetime = PREFIX_INFINITY;
11480Sstevel@tonic-gate 	pr->pr_ValidLifetime = PREFIX_INFINITY;
11490Sstevel@tonic-gate 	pr->pr_OnLinkLifetime = PREFIX_INFINITY;
11500Sstevel@tonic-gate 	pr->pr_kernel_state = 0;
11510Sstevel@tonic-gate 	pr->pr_flags |= flags;
11520Sstevel@tonic-gate 	prefix_insert(pi, pr);
11530Sstevel@tonic-gate 	return (pr);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate /*
11570Sstevel@tonic-gate  * Create a new named prefix. Caller should use prefix_init_from_k
11580Sstevel@tonic-gate  * to initialize the content.
11590Sstevel@tonic-gate  */
11600Sstevel@tonic-gate struct prefix *
11610Sstevel@tonic-gate prefix_create_name(struct phyint *pi, char *name)
11620Sstevel@tonic-gate {
11630Sstevel@tonic-gate 	struct prefix *pr;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (debug & D_PREFIX) {
11660Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_create_name(%s, %s)\n",
11670Sstevel@tonic-gate 		    pi->pi_name, name);
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	pr = (struct prefix *)calloc(sizeof (struct prefix), 1);
11700Sstevel@tonic-gate 	if (pr == NULL) {
11710Sstevel@tonic-gate 		logmsg(LOG_ERR, "prefix_create_name: out of memory\n");
11720Sstevel@tonic-gate 		return (NULL);
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 	(void) strncpy(pr->pr_name, name, sizeof (pr->pr_name));
11750Sstevel@tonic-gate 	pr->pr_name[sizeof (pr->pr_name) - 1] = '\0';
11760Sstevel@tonic-gate 	prefix_insert(pi, pr);
11770Sstevel@tonic-gate 	return (pr);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate /* Insert in linked list */
11810Sstevel@tonic-gate static void
11820Sstevel@tonic-gate prefix_insert(struct phyint *pi, struct prefix *pr)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	pr->pr_next = pi->pi_prefix_list;
11850Sstevel@tonic-gate 	pr->pr_prev = NULL;
11860Sstevel@tonic-gate 	if (pi->pi_prefix_list != NULL)
11870Sstevel@tonic-gate 		pi->pi_prefix_list->pr_prev = pr;
11880Sstevel@tonic-gate 	pi->pi_prefix_list = pr;
11890Sstevel@tonic-gate 	pr->pr_physical = pi;
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * Initialize the prefix from the content of the kernel.
11940Sstevel@tonic-gate  * If IFF_ADDRCONF is set we treat it as PR_AUTO (i.e. an addrconf
11950Sstevel@tonic-gate  * prefix). However, we not derive the lifetimes from
11960Sstevel@tonic-gate  * the kernel thus they are set to 1 week.
11970Sstevel@tonic-gate  * Ignore the prefix if the interface is not IFF_UP.
11980Sstevel@tonic-gate  */
11990Sstevel@tonic-gate int
12000Sstevel@tonic-gate prefix_init_from_k(struct prefix *pr)
12010Sstevel@tonic-gate {
12020Sstevel@tonic-gate 	struct lifreq lifr;
12030Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
12040Sstevel@tonic-gate 	int sock = pr->pr_physical->pi_sock;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pr->pr_name, sizeof (lifr.lifr_name));
12070Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
12080Sstevel@tonic-gate 	if (ioctl(sock, SIOCGLIFADDR, (char *)&lifr) < 0) {
12090Sstevel@tonic-gate 		logperror_pr(pr, "prefix_init_from_k: ioctl (get addr)");
12100Sstevel@tonic-gate 		goto error;
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 	if (lifr.lifr_addr.ss_family != AF_INET6) {
12130Sstevel@tonic-gate 		logmsg(LOG_ERR, "ignoring interface %s: not AF_INET6\n",
12140Sstevel@tonic-gate 		    pr->pr_name);
12150Sstevel@tonic-gate 		goto error;
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
12180Sstevel@tonic-gate 	pr->pr_address = sin6->sin6_addr;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	if (ioctl(sock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
12210Sstevel@tonic-gate 		logperror_pr(pr, "prefix_init_from_k: ioctl (get flags)");
12220Sstevel@tonic-gate 		goto error;
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 	pr->pr_flags = lifr.lifr_flags;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	if (ioctl(sock, SIOCGLIFSUBNET, (char *)&lifr) < 0) {
12270Sstevel@tonic-gate 		logperror_pr(pr, "prefix_init_from_k: ioctl (get subnet)");
12280Sstevel@tonic-gate 		goto error;
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 	if (lifr.lifr_subnet.ss_family != AF_INET6) {
12310Sstevel@tonic-gate 		logmsg(LOG_ERR, "ignoring interface %s: not AF_INET6\n",
12320Sstevel@tonic-gate 		    pr->pr_name);
12330Sstevel@tonic-gate 		goto error;
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate 	/*
12360Sstevel@tonic-gate 	 * Guard against the prefix having non-zero bits after the prefix
12370Sstevel@tonic-gate 	 * len bits.
12380Sstevel@tonic-gate 	 */
12390Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_subnet;
12400Sstevel@tonic-gate 	pr->pr_prefix_len = lifr.lifr_addrlen;
12410Sstevel@tonic-gate 	prefix_set(&pr->pr_prefix, sin6->sin6_addr, pr->pr_prefix_len);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	if (pr->pr_prefix_len != IPV6_ABITS && (pr->pr_flags & IFF_UP) &&
12440Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&pr->pr_address, &pr->pr_prefix)) {
12450Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 		logmsg(LOG_ERR, "ingoring interface %s: it appears to be "
12480Sstevel@tonic-gate 		    "configured with an invalid interface id (%s/%u)\n",
12490Sstevel@tonic-gate 		    pr->pr_name,
12500Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pr->pr_address,
12510Sstevel@tonic-gate 			abuf, sizeof (abuf)), pr->pr_prefix_len);
12520Sstevel@tonic-gate 		goto error;
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 	pr->pr_kernel_state = 0;
12550Sstevel@tonic-gate 	if (pr->pr_prefix_len != IPV6_ABITS)
12560Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_ONLINK;
12570Sstevel@tonic-gate 	if (!(pr->pr_flags & IFF_NOLOCAL))
12580Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_AUTO;
12590Sstevel@tonic-gate 	if ((pr->pr_flags & IFF_DEPRECATED) && (pr->pr_kernel_state & PR_AUTO))
12600Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_DEPRECATED;
12610Sstevel@tonic-gate 	if (!(pr->pr_flags & IFF_ADDRCONF)) {
12620Sstevel@tonic-gate 		/* Prevent ndpd from stepping on this prefix */
12630Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_STATIC;
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 	pr->pr_state = pr->pr_kernel_state;
12660Sstevel@tonic-gate 	/* Adjust pr_prefix_len based if PR_AUTO is set */
12670Sstevel@tonic-gate 	if (pr->pr_state & PR_AUTO) {
12680Sstevel@tonic-gate 		pr->pr_prefix_len =
12690Sstevel@tonic-gate 		    IPV6_ABITS - pr->pr_physical->pi_token_length;
12700Sstevel@tonic-gate 		prefix_set(&pr->pr_prefix, pr->pr_prefix, pr->pr_prefix_len);
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/* Can't extract lifetimes from the kernel - use 1 week */
12740Sstevel@tonic-gate 	pr->pr_ValidLifetime = NDP_PREFIX_DEFAULT_LIFETIME;
12750Sstevel@tonic-gate 	pr->pr_PreferredLifetime = NDP_PREFIX_DEFAULT_LIFETIME;
12760Sstevel@tonic-gate 	pr->pr_OnLinkLifetime = NDP_PREFIX_DEFAULT_LIFETIME;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * If this is a temp addr, the creation time needs to be set.
12800Sstevel@tonic-gate 	 * Though it won't be entirely accurate, the current time is
12810Sstevel@tonic-gate 	 * an okay approximation.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	if (pr->pr_flags & IFF_TEMPORARY)
12840Sstevel@tonic-gate 		pr->pr_CreateTime = getcurrenttime() / MILLISEC;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	if (pr->pr_kernel_state == 0)
12870Sstevel@tonic-gate 		pr->pr_name[0] = '\0';
12880Sstevel@tonic-gate 	return (0);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate error:
12910Sstevel@tonic-gate 	/* Pretend that the prefix does not exist in the kernel */
12920Sstevel@tonic-gate 	pr->pr_kernel_state = 0;
12930Sstevel@tonic-gate 	pr->pr_name[0] = '\0';
12940Sstevel@tonic-gate 	return (-1);
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate /*
12980Sstevel@tonic-gate  * Delete (unlink and free) and remove from kernel if the prefix
12990Sstevel@tonic-gate  * was added by in.ndpd (i.e. PR_STATIC is not set).
13000Sstevel@tonic-gate  * Handles delete of things that have not yet been inserted in the list
13010Sstevel@tonic-gate  * i.e. pr_physical is NULL.
13020Sstevel@tonic-gate  */
13030Sstevel@tonic-gate void
13040Sstevel@tonic-gate prefix_delete(struct prefix *pr)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	struct phyint *pi;
13070Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	if (debug & D_PREFIX) {
13100Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_delete(%s, %s, %s/%u)\n",
13110Sstevel@tonic-gate 		    pr->pr_physical->pi_name, pr->pr_name,
13120Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pr->pr_prefix,
13130Sstevel@tonic-gate 		    abuf, sizeof (abuf)), pr->pr_prefix_len);
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 	/* Remove non-static prefixes from the kernel. */
13160Sstevel@tonic-gate 	pr->pr_state &= PR_STATIC;
13170Sstevel@tonic-gate 	pi = pr->pr_physical;
13180Sstevel@tonic-gate 	if (pr->pr_kernel_state != pr->pr_state)
13190Sstevel@tonic-gate 		prefix_update_k(pr);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	if (pr->pr_prev == NULL) {
13220Sstevel@tonic-gate 		if (pi != NULL)
13230Sstevel@tonic-gate 			pi->pi_prefix_list = pr->pr_next;
13240Sstevel@tonic-gate 	} else {
13250Sstevel@tonic-gate 		pr->pr_prev->pr_next = pr->pr_next;
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 	if (pr->pr_next != NULL)
13280Sstevel@tonic-gate 		pr->pr_next->pr_prev = pr->pr_prev;
13290Sstevel@tonic-gate 	pr->pr_next = pr->pr_prev = NULL;
13300Sstevel@tonic-gate 	free(pr);
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate /*
13340Sstevel@tonic-gate  * Toggle one or more IFF_ flags for a prefix. Turn on 'onflags' and
13350Sstevel@tonic-gate  * turn off 'offflags'.
13360Sstevel@tonic-gate  */
13370Sstevel@tonic-gate static int
13380Sstevel@tonic-gate prefix_modify_flags(struct prefix *pr, uint64_t onflags, uint64_t offflags)
13390Sstevel@tonic-gate {
13400Sstevel@tonic-gate 	struct lifreq lifr;
13410Sstevel@tonic-gate 	struct phyint *pi = pr->pr_physical;
13420Sstevel@tonic-gate 	uint64_t old_flags;
13430Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (debug & D_PREFIX) {
13460Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_modify_flags(%s, %s, %s/%u) "
13470Sstevel@tonic-gate 		    "flags %llx on %llx off %llx\n",
13480Sstevel@tonic-gate 		    pr->pr_physical->pi_name,
13490Sstevel@tonic-gate 		    pr->pr_name,
13500Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pr->pr_prefix,
13510Sstevel@tonic-gate 		    abuf, sizeof (abuf)), pr->pr_prefix_len,
13520Sstevel@tonic-gate 		    pr->pr_flags, onflags, offflags);
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 	/* Assumes that only the PR_STATIC link-local matches the pi_name */
13550Sstevel@tonic-gate 	if (!(pr->pr_state & PR_STATIC) &&
13560Sstevel@tonic-gate 	    strcmp(pr->pr_name, pi->pi_name) == 0) {
13570Sstevel@tonic-gate 		logmsg(LOG_ERR, "prefix_modify_flags(%s, on %llx, off %llx): "
13580Sstevel@tonic-gate 		    "name matches interface name\n",
13590Sstevel@tonic-gate 		    pi->pi_name, onflags, offflags);
13600Sstevel@tonic-gate 		return (-1);
13610Sstevel@tonic-gate 	}
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pr->pr_name, sizeof (lifr.lifr_name));
13640Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
13650Sstevel@tonic-gate 	if (ioctl(pi->pi_sock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
13660Sstevel@tonic-gate 		logperror_pr(pr, "prefix_modify_flags: SIOCGLIFFLAGS");
13670Sstevel@tonic-gate 		logmsg(LOG_ERR, "prefix_modify_flags(%s, %s) old 0x%llx "
13680Sstevel@tonic-gate 		    "on 0x%llx off 0x%llx\n",
13690Sstevel@tonic-gate 		    pr->pr_physical->pi_name,
13700Sstevel@tonic-gate 		    pr->pr_name,
13710Sstevel@tonic-gate 		    pr->pr_flags, onflags, offflags);
13720Sstevel@tonic-gate 		return (-1);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	old_flags = lifr.lifr_flags;
13750Sstevel@tonic-gate 	lifr.lifr_flags |= onflags;
13760Sstevel@tonic-gate 	lifr.lifr_flags &= ~offflags;
13770Sstevel@tonic-gate 	pr->pr_flags = lifr.lifr_flags;
13780Sstevel@tonic-gate 	if (ioctl(pi->pi_sock, SIOCSLIFFLAGS, (char *)&lifr) < 0) {
13790Sstevel@tonic-gate 		logperror_pr(pr, "prefix_modify_flags: SIOCSLIFFLAGS");
13800Sstevel@tonic-gate 		logmsg(LOG_ERR, "prefix_modify_flags(%s, %s) old 0x%llx "
13810Sstevel@tonic-gate 		    "new 0x%llx on 0x%llx off 0x%llx\n",
13820Sstevel@tonic-gate 		    pr->pr_physical->pi_name,
13830Sstevel@tonic-gate 		    pr->pr_name,
13840Sstevel@tonic-gate 		    old_flags, lifr.lifr_flags, onflags, offflags);
13850Sstevel@tonic-gate 		return (-1);
13860Sstevel@tonic-gate 	}
13870Sstevel@tonic-gate 	return (0);
13880Sstevel@tonic-gate }
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate /*
13910Sstevel@tonic-gate  * Make the kernel state match what is in the prefix structure.
13920Sstevel@tonic-gate  * This includes creating the prefix (allocating a new interface name)
13930Sstevel@tonic-gate  * as well as setting the local address and on-link subnet prefix
13940Sstevel@tonic-gate  * and controlling the IFF_ADDRCONF and IFF_DEPRECATED flags.
13950Sstevel@tonic-gate  */
13960Sstevel@tonic-gate void
13970Sstevel@tonic-gate prefix_update_k(struct prefix *pr)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	struct lifreq lifr;
14000Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
14010Sstevel@tonic-gate 	char buf1[PREFIX_STATESTRLEN], buf2[PREFIX_STATESTRLEN];
14020Sstevel@tonic-gate 	struct phyint *pi = pr->pr_physical;
14030Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	if (debug & D_PREFIX) {
14060Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_update_k(%s, %s, %s/%u) "
14070Sstevel@tonic-gate 		    "from %s to %s\n", pr->pr_physical->pi_name, pr->pr_name,
14080Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pr->pr_prefix,
14090Sstevel@tonic-gate 		    abuf, sizeof (abuf)), pr->pr_prefix_len,
14100Sstevel@tonic-gate 		    prefix_print_state(pr->pr_kernel_state, buf1,
14110Sstevel@tonic-gate 		    sizeof (buf1)),
14120Sstevel@tonic-gate 		    prefix_print_state(pr->pr_state, buf2, sizeof (buf2)));
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	if (pr->pr_kernel_state == pr->pr_state)
14160Sstevel@tonic-gate 		return;		/* No changes */
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	/* Skip static prefixes */
14190Sstevel@tonic-gate 	if (pr->pr_state & PR_STATIC)
14200Sstevel@tonic-gate 		return;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	if (pr->pr_kernel_state == 0) {
14230Sstevel@tonic-gate 		uint64_t onflags;
14240Sstevel@tonic-gate 		/*
14250Sstevel@tonic-gate 		 * Create a new logical interface name and store in pr_name.
14260Sstevel@tonic-gate 		 * Set IFF_ADDRCONF. Do not set an address (yet).
14270Sstevel@tonic-gate 		 */
14280Sstevel@tonic-gate 		if (pr->pr_name[0] != '\0') {
14290Sstevel@tonic-gate 			/* Name already set! */
14300Sstevel@tonic-gate 			logmsg(LOG_ERR, "prefix_update_k(%s, %s, %s/%u) "
14310Sstevel@tonic-gate 			    "from %s to %s name is already allocated\n",
14320Sstevel@tonic-gate 			    pr->pr_physical->pi_name, pr->pr_name,
14330Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&pr->pr_prefix,
14340Sstevel@tonic-gate 			    abuf, sizeof (abuf)), pr->pr_prefix_len,
14350Sstevel@tonic-gate 			    prefix_print_state(pr->pr_kernel_state, buf1,
14360Sstevel@tonic-gate 			    sizeof (buf1)),
14370Sstevel@tonic-gate 			    prefix_print_state(pr->pr_state, buf2,
14380Sstevel@tonic-gate 			    sizeof (buf2)));
14390Sstevel@tonic-gate 			return;
14400Sstevel@tonic-gate 		}
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pi->pi_name,
14430Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14440Sstevel@tonic-gate 		lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
14450Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = AF_UNSPEC;
14460Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCLIFADDIF, (char *)&lifr) < 0) {
14470Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCLIFADDIF");
14480Sstevel@tonic-gate 			return;
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 		(void) strncpy(pr->pr_name, lifr.lifr_name,
14510Sstevel@tonic-gate 		    sizeof (pr->pr_name));
14520Sstevel@tonic-gate 		pr->pr_name[sizeof (pr->pr_name) - 1] = '\0';
14530Sstevel@tonic-gate 		if (debug & D_PREFIX) {
14540Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k: new name %s\n",
14550Sstevel@tonic-gate 			    pr->pr_name);
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 		/*
14580Sstevel@tonic-gate 		 * The IFF_TEMPORARY flag might have already been set; if
14590Sstevel@tonic-gate 		 * so, it needs to be or'd into the flags we're turning on.
14600Sstevel@tonic-gate 		 * But be careful, we might be re-creating a manually
14610Sstevel@tonic-gate 		 * removed interface, in which case we don't want to try
14620Sstevel@tonic-gate 		 * to set *all* the flags we might have in our copy of the
14630Sstevel@tonic-gate 		 * flags yet.
14640Sstevel@tonic-gate 		 */
14650Sstevel@tonic-gate 		onflags = IFF_ADDRCONF;
14660Sstevel@tonic-gate 		if (pr->pr_flags & IFF_TEMPORARY)
14670Sstevel@tonic-gate 			onflags |= IFF_TEMPORARY;
14680Sstevel@tonic-gate 		if (prefix_modify_flags(pr, onflags, 0) == -1)
14690Sstevel@tonic-gate 			return;
14700Sstevel@tonic-gate 	}
14710Sstevel@tonic-gate 	if ((pr->pr_state & (PR_ONLINK|PR_AUTO)) == 0) {
14720Sstevel@tonic-gate 		/* Remove the interface */
14730Sstevel@tonic-gate 		if (prefix_modify_flags(pr, 0, IFF_UP|IFF_DEPRECATED) == -1)
14740Sstevel@tonic-gate 			return;
14750Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pr->pr_name,
14760Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14770Sstevel@tonic-gate 		lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		if (debug & D_PREFIX) {
14800Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k: remove name %s\n",
14810Sstevel@tonic-gate 			    pr->pr_name);
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		/*
14850Sstevel@tonic-gate 		 * Assumes that only the PR_STATIC link-local matches
14860Sstevel@tonic-gate 		 * the pi_name
14870Sstevel@tonic-gate 		 */
14880Sstevel@tonic-gate 		if (!(pr->pr_state & PR_STATIC) &&
14890Sstevel@tonic-gate 		    strcmp(pr->pr_name, pi->pi_name) == 0) {
14900Sstevel@tonic-gate 			logmsg(LOG_ERR, "prefix_update_k(%s): "
14910Sstevel@tonic-gate 			    "name matches if\n", pi->pi_name);
14920Sstevel@tonic-gate 			return;
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 		/* Remove logical interface based on pr_name */
14960Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = AF_UNSPEC;
14970Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCLIFREMOVEIF, (char *)&lifr) < 0) {
14980Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCLIFREMOVEIF");
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 		pr->pr_kernel_state = 0;
15010Sstevel@tonic-gate 		pr->pr_name[0] = '\0';
15020Sstevel@tonic-gate 		return;
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 	if ((pr->pr_state & PR_AUTO) && !(pr->pr_kernel_state & PR_AUTO)) {
15050Sstevel@tonic-gate 		/*
15060Sstevel@tonic-gate 		 * Set local address and set the prefix length to 128.
15070Sstevel@tonic-gate 		 * Turn off IFF_NOLOCAL in case it was set.
15080Sstevel@tonic-gate 		 * Turn on IFF_UP.
15090Sstevel@tonic-gate 		 */
15100Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pr->pr_name,
15110Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
15120Sstevel@tonic-gate 		lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
15130Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
15140Sstevel@tonic-gate 		bzero(sin6, sizeof (struct sockaddr_in6));
15150Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
15160Sstevel@tonic-gate 		sin6->sin6_addr = pr->pr_address;
15170Sstevel@tonic-gate 		if (debug & D_PREFIX) {
15180Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k(%s) set addr %s "
15190Sstevel@tonic-gate 			    "for PR_AUTO on\n",
15200Sstevel@tonic-gate 			    pr->pr_name,
15210Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&pr->pr_address,
15220Sstevel@tonic-gate 				abuf, sizeof (abuf)));
15230Sstevel@tonic-gate 		}
15240Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCSLIFADDR, (char *)&lifr) < 0) {
15250Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCSLIFADDR");
15260Sstevel@tonic-gate 			return;
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 		if (pr->pr_state & PR_ONLINK) {
15290Sstevel@tonic-gate 			sin6->sin6_addr = pr->pr_prefix;
15300Sstevel@tonic-gate 			lifr.lifr_addrlen = pr->pr_prefix_len;
15310Sstevel@tonic-gate 		} else {
15320Sstevel@tonic-gate 			sin6->sin6_addr = pr->pr_address;
15330Sstevel@tonic-gate 			lifr.lifr_addrlen = IPV6_ABITS;
15340Sstevel@tonic-gate 		}
15350Sstevel@tonic-gate 		if (debug & D_PREFIX) {
15360Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k(%s) set subnet "
15370Sstevel@tonic-gate 			    "%s/%u for PR_AUTO on\n", pr->pr_name,
15380Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
15390Sstevel@tonic-gate 				abuf, sizeof (abuf)), lifr.lifr_addrlen);
15400Sstevel@tonic-gate 		}
15410Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCSLIFSUBNET, (char *)&lifr) < 0) {
15420Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCSLIFSUBNET");
15430Sstevel@tonic-gate 			return;
15440Sstevel@tonic-gate 		}
15450Sstevel@tonic-gate 		/*
15460Sstevel@tonic-gate 		 * For ptp interfaces, create a destination based on
15470Sstevel@tonic-gate 		 * prefix and prefix len together with the remote token
15480Sstevel@tonic-gate 		 * extracted from the remote pt-pt address.  This is used by
15490Sstevel@tonic-gate 		 * ip to choose a proper source for outgoing packets.
15500Sstevel@tonic-gate 		 */
15510Sstevel@tonic-gate 		if (pi->pi_flags & IFF_POINTOPOINT) {
15520Sstevel@tonic-gate 			int i;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
15550Sstevel@tonic-gate 			bzero(sin6, sizeof (struct sockaddr_in6));
15560Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
15570Sstevel@tonic-gate 			sin6->sin6_addr = pr->pr_prefix;
15580Sstevel@tonic-gate 			for (i = 0; i < 16; i++) {
15590Sstevel@tonic-gate 				sin6->sin6_addr.s6_addr[i] |=
15600Sstevel@tonic-gate 				    pi->pi_dst_token.s6_addr[i];
15610Sstevel@tonic-gate 			}
15620Sstevel@tonic-gate 			if (debug & D_PREFIX) {
15630Sstevel@tonic-gate 				logmsg(LOG_DEBUG, "prefix_update_k(%s) "
15640Sstevel@tonic-gate 				    "set dstaddr %s for PR_AUTO on\n",
15650Sstevel@tonic-gate 				    pr->pr_name, inet_ntop(AF_INET6,
15660Sstevel@tonic-gate 				    (void *)&sin6->sin6_addr,
15670Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
15680Sstevel@tonic-gate 			}
15690Sstevel@tonic-gate 			if (ioctl(pi->pi_sock, SIOCSLIFDSTADDR,
15700Sstevel@tonic-gate 			    (char *)&lifr) < 0) {
15710Sstevel@tonic-gate 				logperror_pr(pr,
15720Sstevel@tonic-gate 				    "prefix_update_k: SIOCSLIFDSTADDR");
15730Sstevel@tonic-gate 				return;
15740Sstevel@tonic-gate 			}
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 		if (prefix_modify_flags(pr, IFF_UP, IFF_NOLOCAL) == -1)
15770Sstevel@tonic-gate 			return;
15780Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_AUTO;
15790Sstevel@tonic-gate 		if (pr->pr_state & PR_ONLINK)
15800Sstevel@tonic-gate 			pr->pr_kernel_state |= PR_ONLINK;
15810Sstevel@tonic-gate 		else
15820Sstevel@tonic-gate 			pr->pr_kernel_state &= ~PR_ONLINK;
15830Sstevel@tonic-gate 	}
15840Sstevel@tonic-gate 	if (!(pr->pr_state & PR_AUTO) && (pr->pr_kernel_state & PR_AUTO)) {
15850Sstevel@tonic-gate 		/* Turn on IFF_NOLOCAL and set the local address to all zero */
15860Sstevel@tonic-gate 		if (prefix_modify_flags(pr, IFF_NOLOCAL, 0) == -1)
15870Sstevel@tonic-gate 			return;
15880Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pr->pr_name,
15890Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
15900Sstevel@tonic-gate 		lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
15910Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
15920Sstevel@tonic-gate 		bzero(sin6, sizeof (struct sockaddr_in6));
15930Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
15940Sstevel@tonic-gate 		if (debug & D_PREFIX) {
15950Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k(%s) set addr %s "
15960Sstevel@tonic-gate 			    "for PR_AUTO off\n", pr->pr_name,
15970Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
15980Sstevel@tonic-gate 				abuf, sizeof (abuf)));
15990Sstevel@tonic-gate 		}
16000Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCSLIFADDR, (char *)&lifr) < 0) {
16010Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCSLIFADDR");
16020Sstevel@tonic-gate 			return;
16030Sstevel@tonic-gate 		}
16040Sstevel@tonic-gate 		pr->pr_kernel_state &= ~PR_AUTO;
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate 	if ((pr->pr_state & PR_DEPRECATED) &&
16070Sstevel@tonic-gate 	    !(pr->pr_kernel_state & PR_DEPRECATED) &&
16080Sstevel@tonic-gate 	    (pr->pr_kernel_state & PR_AUTO)) {
16090Sstevel@tonic-gate 		/* Only applies if PR_AUTO */
16100Sstevel@tonic-gate 		if (prefix_modify_flags(pr, IFF_DEPRECATED, 0) == -1)
16110Sstevel@tonic-gate 			return;
16120Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_DEPRECATED;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 	if (!(pr->pr_state & PR_DEPRECATED) &&
16150Sstevel@tonic-gate 	    (pr->pr_kernel_state & PR_DEPRECATED)) {
16160Sstevel@tonic-gate 		if (prefix_modify_flags(pr, 0, IFF_DEPRECATED) == -1)
16170Sstevel@tonic-gate 			return;
16180Sstevel@tonic-gate 		pr->pr_kernel_state &= ~PR_DEPRECATED;
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 	if ((pr->pr_state & PR_ONLINK) && !(pr->pr_kernel_state & PR_ONLINK)) {
16210Sstevel@tonic-gate 		/* Set the subnet and set IFF_UP */
16220Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pr->pr_name,
16230Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
16240Sstevel@tonic-gate 		lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
16250Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
16260Sstevel@tonic-gate 		bzero(sin6, sizeof (struct sockaddr_in6));
16270Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
16280Sstevel@tonic-gate 		sin6->sin6_addr = pr->pr_prefix;
16290Sstevel@tonic-gate 		lifr.lifr_addrlen = pr->pr_prefix_len;
16300Sstevel@tonic-gate 		if (debug & D_PREFIX) {
16310Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k(%s) set subnet "
16320Sstevel@tonic-gate 			    "%s/%d for PR_ONLINK on\n", pr->pr_name,
16330Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
16340Sstevel@tonic-gate 				abuf, sizeof (abuf)), lifr.lifr_addrlen);
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCSLIFSUBNET, (char *)&lifr) < 0) {
16370Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCSLIFSUBNET");
16380Sstevel@tonic-gate 			return;
16390Sstevel@tonic-gate 		}
16400Sstevel@tonic-gate 		if (!(pr->pr_state & PR_AUTO)) {
16410Sstevel@tonic-gate 			if (prefix_modify_flags(pr, IFF_NOLOCAL, 0) == -1)
16420Sstevel@tonic-gate 				return;
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 		if (prefix_modify_flags(pr, IFF_UP, 0) == -1)
16450Sstevel@tonic-gate 			return;
16460Sstevel@tonic-gate 		pr->pr_kernel_state |= PR_ONLINK;
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 	if (!(pr->pr_state & PR_ONLINK) && (pr->pr_kernel_state & PR_ONLINK)) {
16490Sstevel@tonic-gate 		/* Set the prefixlen to 128 */
16500Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, pr->pr_name,
16510Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
16520Sstevel@tonic-gate 		lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
16530Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
16540Sstevel@tonic-gate 		bzero(sin6, sizeof (struct sockaddr_in6));
16550Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
16560Sstevel@tonic-gate 		sin6->sin6_addr = pr->pr_address;
16570Sstevel@tonic-gate 		lifr.lifr_addrlen = IPV6_ABITS;
16580Sstevel@tonic-gate 		if (debug & D_PREFIX) {
16590Sstevel@tonic-gate 			logmsg(LOG_DEBUG, "prefix_update_k(%s) set subnet "
16600Sstevel@tonic-gate 			    "%s/%d for PR_ONLINK off\n", pr->pr_name,
16610Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
16620Sstevel@tonic-gate 				abuf, sizeof (abuf)), lifr.lifr_addrlen);
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 		if (ioctl(pi->pi_sock, SIOCSLIFSUBNET, (char *)&lifr) < 0) {
16650Sstevel@tonic-gate 			logperror_pr(pr, "prefix_update_k: SIOCSLIFSUBNET");
16660Sstevel@tonic-gate 			return;
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 		pr->pr_kernel_state &= ~PR_ONLINK;
16690Sstevel@tonic-gate 	}
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate /*
16730Sstevel@tonic-gate  * Called with the number of millseconds elapsed since the last call.
16740Sstevel@tonic-gate  * Determines if any timeout event has occurred and
16750Sstevel@tonic-gate  * returns the number of milliseconds until the next timeout event.
16760Sstevel@tonic-gate  * Returns TIMER_INFINITY for "never".
16770Sstevel@tonic-gate  */
16780Sstevel@tonic-gate uint_t
16790Sstevel@tonic-gate prefix_timer(struct prefix *pr, uint_t elapsed)
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate 	uint_t next = TIMER_INFINITY;
16820Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	if (debug & (D_PREFIX|D_TMP)) {
16850Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "prefix_timer(%s, %s/%u, %d) "
16860Sstevel@tonic-gate 		    "valid %d pref %d onlink %d\n",
16870Sstevel@tonic-gate 		    pr->pr_name,
16880Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&pr->pr_prefix,
16890Sstevel@tonic-gate 		    abuf, sizeof (abuf)), pr->pr_prefix_len,
16900Sstevel@tonic-gate 		    elapsed, pr->pr_ValidLifetime, pr->pr_PreferredLifetime,
16910Sstevel@tonic-gate 		    pr->pr_OnLinkLifetime);
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	/* Exclude static prefixes */
16950Sstevel@tonic-gate 	if (pr->pr_state & PR_STATIC)
16960Sstevel@tonic-gate 		return (next);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	if (pr->pr_AutonomousFlag &&
16990Sstevel@tonic-gate 	    (pr->pr_PreferredLifetime != PREFIX_INFINITY)) {
17000Sstevel@tonic-gate 		if (pr->pr_PreferredLifetime <= elapsed) {
17010Sstevel@tonic-gate 			pr->pr_PreferredLifetime = 0;
17020Sstevel@tonic-gate 		} else {
17030Sstevel@tonic-gate 			pr->pr_PreferredLifetime -= elapsed;
17040Sstevel@tonic-gate 			if (pr->pr_PreferredLifetime < next)
17050Sstevel@tonic-gate 				next = pr->pr_PreferredLifetime;
17060Sstevel@tonic-gate 		}
17070Sstevel@tonic-gate 	}
17080Sstevel@tonic-gate 	if (pr->pr_AutonomousFlag &&
17090Sstevel@tonic-gate 	    (pr->pr_ValidLifetime != PREFIX_INFINITY)) {
17100Sstevel@tonic-gate 		if (pr->pr_ValidLifetime <= elapsed) {
17110Sstevel@tonic-gate 			pr->pr_ValidLifetime = 0;
17120Sstevel@tonic-gate 		} else {
17130Sstevel@tonic-gate 			pr->pr_ValidLifetime -= elapsed;
17140Sstevel@tonic-gate 			if (pr->pr_ValidLifetime < next)
17150Sstevel@tonic-gate 				next = pr->pr_ValidLifetime;
17160Sstevel@tonic-gate 		}
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	if (pr->pr_OnLinkFlag &&
17190Sstevel@tonic-gate 	    (pr->pr_OnLinkLifetime != PREFIX_INFINITY)) {
17200Sstevel@tonic-gate 		if (pr->pr_OnLinkLifetime <= elapsed) {
17210Sstevel@tonic-gate 			pr->pr_OnLinkLifetime = 0;
17220Sstevel@tonic-gate 		} else {
17230Sstevel@tonic-gate 			pr->pr_OnLinkLifetime -= elapsed;
17240Sstevel@tonic-gate 			if (pr->pr_OnLinkLifetime < next)
17250Sstevel@tonic-gate 				next = pr->pr_OnLinkLifetime;
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 	if (pr->pr_AutonomousFlag && pr->pr_ValidLifetime == 0)
17290Sstevel@tonic-gate 		pr->pr_state &= ~(PR_AUTO|PR_DEPRECATED);
17300Sstevel@tonic-gate 	if (pr->pr_AutonomousFlag && pr->pr_PreferredLifetime == 0 &&
17310Sstevel@tonic-gate 	    (pr->pr_state & PR_AUTO)) {
17320Sstevel@tonic-gate 		pr->pr_state |= PR_DEPRECATED;
17330Sstevel@tonic-gate 		if (debug & D_TMP)
17340Sstevel@tonic-gate 			logmsg(LOG_WARNING, "prefix_timer: deprecated "
17350Sstevel@tonic-gate 			    "prefix(%s)\n", pr->pr_name);
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 	if (pr->pr_OnLinkFlag && pr->pr_OnLinkLifetime == 0)
17380Sstevel@tonic-gate 		pr->pr_state &= ~PR_ONLINK;
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	if (pr->pr_state != pr->pr_kernel_state) {
17410Sstevel@tonic-gate 		/* Might cause prefix to be deleted! */
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 		/* Log a message when an addrconf prefix goes away */
17440Sstevel@tonic-gate 		if ((pr->pr_kernel_state & PR_AUTO) &&
17450Sstevel@tonic-gate 		    !(pr->pr_state & PR_AUTO)) {
17460Sstevel@tonic-gate 			char abuf[INET6_ADDRSTRLEN];
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 			logmsg(LOG_WARNING,
17490Sstevel@tonic-gate 			    "Address removed due to timeout %s\n",
17500Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&pr->pr_address,
17510Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
17520Sstevel@tonic-gate 		}
17530Sstevel@tonic-gate 		prefix_update_k(pr);
17540Sstevel@tonic-gate 	}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	return (next);
17570Sstevel@tonic-gate }
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate static char *
17600Sstevel@tonic-gate prefix_print_state(int state, char *buf, int buflen)
17610Sstevel@tonic-gate {
17620Sstevel@tonic-gate 	char *cp;
17630Sstevel@tonic-gate 	int cplen = buflen;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	cp = buf;
17660Sstevel@tonic-gate 	cp[0] = '\0';
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	if (state & PR_ONLINK) {
17690Sstevel@tonic-gate 		if (strlcat(cp, "ONLINK ", cplen) >= cplen)
17700Sstevel@tonic-gate 			return (buf);
17710Sstevel@tonic-gate 		cp += strlen(cp);
17720Sstevel@tonic-gate 		cplen = buflen - (cp - buf);
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 	if (state & PR_AUTO) {
17750Sstevel@tonic-gate 		if (strlcat(cp, "AUTO ", cplen) >= cplen)
17760Sstevel@tonic-gate 			return (buf);
17770Sstevel@tonic-gate 		cp += strlen(cp);
17780Sstevel@tonic-gate 		cplen = buflen - (cp - buf);
17790Sstevel@tonic-gate 	}
17800Sstevel@tonic-gate 	if (state & PR_DEPRECATED) {
17810Sstevel@tonic-gate 		if (strlcat(cp, "DEPRECATED ", cplen) >= cplen)
17820Sstevel@tonic-gate 			return (buf);
17830Sstevel@tonic-gate 		cp += strlen(cp);
17840Sstevel@tonic-gate 		cplen = buflen - (cp - buf);
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate 	if (state & PR_STATIC) {
17870Sstevel@tonic-gate 		if (strlcat(cp, "STATIC ", cplen) >= cplen)
17880Sstevel@tonic-gate 			return (buf);
17890Sstevel@tonic-gate 		cp += strlen(cp);
17900Sstevel@tonic-gate 		cplen = buflen - (cp - buf);
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 	return (buf);
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate static void
17960Sstevel@tonic-gate prefix_print(struct prefix *pr)
17970Sstevel@tonic-gate {
17980Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
17990Sstevel@tonic-gate 	char buf1[PREFIX_STATESTRLEN], buf2[PREFIX_STATESTRLEN];
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "Prefix name: %s prefix %s/%u state %s "
18020Sstevel@tonic-gate 	    "kernel_state %s\n", pr->pr_name,
18030Sstevel@tonic-gate 	    inet_ntop(AF_INET6, (void *)&pr->pr_prefix, abuf, sizeof (abuf)),
18040Sstevel@tonic-gate 	    pr->pr_prefix_len,
18050Sstevel@tonic-gate 	    prefix_print_state(pr->pr_state, buf2, sizeof (buf2)),
18060Sstevel@tonic-gate 	    prefix_print_state(pr->pr_kernel_state, buf1, sizeof (buf1)));
18070Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\tAddress: %s flags %llx in_use %d\n",
18080Sstevel@tonic-gate 	    inet_ntop(AF_INET6, (void *)&pr->pr_address, abuf, sizeof (abuf)),
18090Sstevel@tonic-gate 	    pr->pr_flags, pr->pr_in_use);
18100Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\tValidLifetime %u PreferredLifetime %u "
18110Sstevel@tonic-gate 	    "OnLinkLifetime %u\n", pr->pr_ValidLifetime,
18120Sstevel@tonic-gate 	    pr->pr_PreferredLifetime, pr->pr_OnLinkLifetime);
18130Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\tOnLink %d Auto %d\n",
18140Sstevel@tonic-gate 	    pr->pr_OnLinkFlag, pr->pr_AutonomousFlag);
18150Sstevel@tonic-gate 	logmsg(LOG_DEBUG, "\n");
18160Sstevel@tonic-gate }
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate  * Does the address formed by pr->pr_prefix and pi->pi_token match
18200Sstevel@tonic-gate  * pr->pr_address. It does not match if a failover has happened
18210Sstevel@tonic-gate  * earlier (done by in.mpathd) from a different pi. Should not
18220Sstevel@tonic-gate  * be called for onlink prefixes.
18230Sstevel@tonic-gate  */
18240Sstevel@tonic-gate boolean_t
18250Sstevel@tonic-gate prefix_token_match(struct phyint *pi, struct prefix *pr, uint64_t flags)
18260Sstevel@tonic-gate {
18270Sstevel@tonic-gate 	int i;
18280Sstevel@tonic-gate 	in6_addr_t addr, *token;
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	if (flags & IFF_TEMPORARY)
18310Sstevel@tonic-gate 		token = &pi->pi_tmp_token;
18320Sstevel@tonic-gate 	else
18330Sstevel@tonic-gate 		token = &pi->pi_token;
18340Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
18350Sstevel@tonic-gate 		/*
18360Sstevel@tonic-gate 		 * prefix_create ensures that pr_prefix has all-zero
18370Sstevel@tonic-gate 		 * bits after prefixlen.
18380Sstevel@tonic-gate 		 */
18390Sstevel@tonic-gate 		addr.s6_addr[i] = pr->pr_prefix.s6_addr[i] | token->s6_addr[i];
18400Sstevel@tonic-gate 	}
18410Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(&pr->pr_address, &addr)) {
18420Sstevel@tonic-gate 		return (_B_TRUE);
18430Sstevel@tonic-gate 	} else {
18440Sstevel@tonic-gate 		return (_B_FALSE);
18450Sstevel@tonic-gate 	}
18460Sstevel@tonic-gate }
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate /*
18490Sstevel@tonic-gate  * Lookup advertisement prefix structure that matches the prefix and
18500Sstevel@tonic-gate  * prefix length.
18510Sstevel@tonic-gate  * Assumes that the bits after prefixlen might not be zero.
18520Sstevel@tonic-gate  */
18530Sstevel@tonic-gate struct adv_prefix *
18540Sstevel@tonic-gate adv_prefix_lookup(struct phyint *pi, struct in6_addr prefix, int prefixlen)
18550Sstevel@tonic-gate {
18560Sstevel@tonic-gate 	struct adv_prefix *adv_pr;
18570Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 	if (debug & D_PREFIX) {
18600Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "adv_prefix_lookup(%s, %s/%u)\n",
18610Sstevel@tonic-gate 		    pi->pi_name, inet_ntop(AF_INET6, (void *)&prefix,
18620Sstevel@tonic-gate 		    abuf, sizeof (abuf)), prefixlen);
18630Sstevel@tonic-gate 	}
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
18660Sstevel@tonic-gate 	    adv_pr = adv_pr->adv_pr_next) {
18670Sstevel@tonic-gate 		if (adv_pr->adv_pr_prefix_len == prefixlen &&
18680Sstevel@tonic-gate 		    prefix_equal(prefix, adv_pr->adv_pr_prefix, prefixlen))
18690Sstevel@tonic-gate 			return (adv_pr);
18700Sstevel@tonic-gate 	}
18710Sstevel@tonic-gate 	return (NULL);
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate /*
18750Sstevel@tonic-gate  * Initialize a new advertisement prefix.
18760Sstevel@tonic-gate  */
18770Sstevel@tonic-gate struct adv_prefix *
18780Sstevel@tonic-gate adv_prefix_create(struct phyint *pi, struct in6_addr prefix, int prefixlen)
18790Sstevel@tonic-gate {
18800Sstevel@tonic-gate 	struct adv_prefix *adv_pr;
18810Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	if (debug & D_PREFIX) {
18840Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "adv_prefix_create(%s, %s/%u)\n",
18850Sstevel@tonic-gate 		    pi->pi_name, inet_ntop(AF_INET6, (void *)&prefix,
18860Sstevel@tonic-gate 		    abuf, sizeof (abuf)), prefixlen);
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate 	adv_pr = (struct adv_prefix *)calloc(sizeof (struct adv_prefix), 1);
18890Sstevel@tonic-gate 	if (adv_pr == NULL) {
18900Sstevel@tonic-gate 		logmsg(LOG_ERR, "adv_prefix_create: calloc\n");
18910Sstevel@tonic-gate 		return (NULL);
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 	/*
18940Sstevel@tonic-gate 	 * The prefix might have non-zero bits after the prefix len bits.
18950Sstevel@tonic-gate 	 * Force them to be zero.
18960Sstevel@tonic-gate 	 */
18970Sstevel@tonic-gate 	prefix_set(&adv_pr->adv_pr_prefix, prefix, prefixlen);
18980Sstevel@tonic-gate 	adv_pr->adv_pr_prefix_len = prefixlen;
18990Sstevel@tonic-gate 	adv_prefix_insert(pi, adv_pr);
19000Sstevel@tonic-gate 	return (adv_pr);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate /* Insert in linked list */
19040Sstevel@tonic-gate static void
19050Sstevel@tonic-gate adv_prefix_insert(struct phyint *pi, struct adv_prefix *adv_pr)
19060Sstevel@tonic-gate {
19070Sstevel@tonic-gate 	adv_pr->adv_pr_next = pi->pi_adv_prefix_list;
19080Sstevel@tonic-gate 	adv_pr->adv_pr_prev = NULL;
19090Sstevel@tonic-gate 	if (pi->pi_adv_prefix_list != NULL)
19100Sstevel@tonic-gate 		pi->pi_adv_prefix_list->adv_pr_prev = adv_pr;
19110Sstevel@tonic-gate 	pi->pi_adv_prefix_list = adv_pr;
19120Sstevel@tonic-gate 	adv_pr->adv_pr_physical = pi;
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate /*
19160Sstevel@tonic-gate  * Delete (unlink and free) from our tables. There should be
19170Sstevel@tonic-gate  * a corresponding "struct prefix *" which will clean up the kernel
19180Sstevel@tonic-gate  * if necessary. adv_prefix is just used for sending out advertisements.
19190Sstevel@tonic-gate  */
19200Sstevel@tonic-gate static void
19210Sstevel@tonic-gate adv_prefix_delete(struct adv_prefix *adv_pr)
19220Sstevel@tonic-gate {
19230Sstevel@tonic-gate 	struct phyint *pi;
19240Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 	if (debug & D_PREFIX) {
19270Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "adv_prefix_delete(%s, %s/%u)\n",
19280Sstevel@tonic-gate 		    adv_pr->adv_pr_physical->pi_name,
19290Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&adv_pr->adv_pr_prefix,
19300Sstevel@tonic-gate 		    abuf, sizeof (abuf)), adv_pr->adv_pr_prefix_len);
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 	pi = adv_pr->adv_pr_physical;
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	if (adv_pr->adv_pr_prev == NULL) {
19350Sstevel@tonic-gate 		if (pi != NULL)
19360Sstevel@tonic-gate 			pi->pi_adv_prefix_list = adv_pr->adv_pr_next;
19370Sstevel@tonic-gate 	} else {
19380Sstevel@tonic-gate 		adv_pr->adv_pr_prev->adv_pr_next = adv_pr->adv_pr_next;
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate 	if (adv_pr->adv_pr_next != NULL)
19410Sstevel@tonic-gate 		adv_pr->adv_pr_next->adv_pr_prev = adv_pr->adv_pr_prev;
19420Sstevel@tonic-gate 	adv_pr->adv_pr_next = adv_pr->adv_pr_prev = NULL;
19430Sstevel@tonic-gate 	free(adv_pr);
19440Sstevel@tonic-gate }
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate /*
19470Sstevel@tonic-gate  * Called with the number of millseconds elapsed since the last call.
19480Sstevel@tonic-gate  * Determines if any timeout event has occurred and
19490Sstevel@tonic-gate  * returns the number of milliseconds until the next timeout event.
19500Sstevel@tonic-gate  * Returns TIMER_INFINITY for "never".
19510Sstevel@tonic-gate  */
19520Sstevel@tonic-gate uint_t
19530Sstevel@tonic-gate adv_prefix_timer(struct adv_prefix *adv_pr, uint_t elapsed)
19540Sstevel@tonic-gate {
19550Sstevel@tonic-gate 	int seconds_elapsed = (elapsed + 500) / 1000;	/* Rounded */
19560Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	if (debug & D_PREFIX) {
19590Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "adv_prefix_timer(%s, %s/%u, %d)\n",
19600Sstevel@tonic-gate 		    adv_pr->adv_pr_physical->pi_name,
19610Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&adv_pr->adv_pr_prefix,
19620Sstevel@tonic-gate 		    abuf, sizeof (abuf)), adv_pr->adv_pr_prefix_len,
19630Sstevel@tonic-gate 		    elapsed);
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	/* Decrement Expire time left for real-time lifetimes */
19670Sstevel@tonic-gate 	if (adv_pr->adv_pr_AdvValidRealTime) {
19680Sstevel@tonic-gate 		if (adv_pr->adv_pr_AdvValidExpiration > seconds_elapsed)
19690Sstevel@tonic-gate 			adv_pr->adv_pr_AdvValidExpiration -= seconds_elapsed;
19700Sstevel@tonic-gate 		else
19710Sstevel@tonic-gate 			adv_pr->adv_pr_AdvValidExpiration = 0;
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 	if (adv_pr->adv_pr_AdvPreferredRealTime) {
19740Sstevel@tonic-gate 		if (adv_pr->adv_pr_AdvPreferredExpiration > seconds_elapsed) {
19750Sstevel@tonic-gate 			adv_pr->adv_pr_AdvPreferredExpiration -=
19760Sstevel@tonic-gate 			    seconds_elapsed;
19770Sstevel@tonic-gate 		} else {
19780Sstevel@tonic-gate 			adv_pr->adv_pr_AdvPreferredExpiration = 0;
19790Sstevel@tonic-gate 		}
19800Sstevel@tonic-gate 	}
19810Sstevel@tonic-gate 	return (TIMER_INFINITY);
19820Sstevel@tonic-gate }
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate static void
19850Sstevel@tonic-gate adv_prefix_print(struct adv_prefix *adv_pr)
19860Sstevel@tonic-gate {
19870Sstevel@tonic-gate 	print_prefixlist(adv_pr->adv_pr_config);
19880Sstevel@tonic-gate }
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate /* Lookup router on its link-local IPv6 address */
19910Sstevel@tonic-gate struct router *
19920Sstevel@tonic-gate router_lookup(struct phyint *pi, struct in6_addr addr)
19930Sstevel@tonic-gate {
19940Sstevel@tonic-gate 	struct router *dr;
19950Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if (debug & D_ROUTER) {
19980Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_lookup(%s, %s)\n", pi->pi_name,
19990Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&addr,
20000Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	for (dr = pi->pi_router_list; dr != NULL; dr = dr->dr_next) {
20040Sstevel@tonic-gate 		if (bcmp((char *)&addr, (char *)&dr->dr_address,
20050Sstevel@tonic-gate 		    sizeof (addr)) == 0)
20060Sstevel@tonic-gate 			return (dr);
20070Sstevel@tonic-gate 	}
20080Sstevel@tonic-gate 	return (NULL);
20090Sstevel@tonic-gate }
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate /*
20120Sstevel@tonic-gate  * Create a default router entry.
20130Sstevel@tonic-gate  * The lifetime parameter is in seconds.
20140Sstevel@tonic-gate  */
20150Sstevel@tonic-gate struct router *
20160Sstevel@tonic-gate router_create(struct phyint *pi, struct in6_addr addr, uint_t lifetime)
20170Sstevel@tonic-gate {
20180Sstevel@tonic-gate 	struct router *dr;
20190Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	if (debug & D_ROUTER) {
20220Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_create(%s, %s, %u)\n", pi->pi_name,
20230Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&addr,
20240Sstevel@tonic-gate 		    abuf, sizeof (abuf)), lifetime);
20250Sstevel@tonic-gate 	}
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	dr = (struct router *)calloc(sizeof (struct router), 1);
20280Sstevel@tonic-gate 	if (dr == NULL) {
20290Sstevel@tonic-gate 		logmsg(LOG_ERR, "router_create: out of memory\n");
20300Sstevel@tonic-gate 		return (NULL);
20310Sstevel@tonic-gate 	}
20320Sstevel@tonic-gate 	dr->dr_address = addr;
20330Sstevel@tonic-gate 	dr->dr_lifetime = lifetime;
20340Sstevel@tonic-gate 	router_insert(pi, dr);
20351577Sseb 	if (dr->dr_lifetime != 0)
20360Sstevel@tonic-gate 		router_add_k(dr);
20370Sstevel@tonic-gate 	return (dr);
20380Sstevel@tonic-gate }
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate /* Insert in linked list */
20410Sstevel@tonic-gate static void
20420Sstevel@tonic-gate router_insert(struct phyint *pi, struct router *dr)
20430Sstevel@tonic-gate {
20440Sstevel@tonic-gate 	dr->dr_next = pi->pi_router_list;
20450Sstevel@tonic-gate 	dr->dr_prev = NULL;
20460Sstevel@tonic-gate 	if (pi->pi_router_list != NULL)
20470Sstevel@tonic-gate 		pi->pi_router_list->dr_prev = dr;
20480Sstevel@tonic-gate 	pi->pi_router_list = dr;
20490Sstevel@tonic-gate 	dr->dr_physical = pi;
20500Sstevel@tonic-gate }
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate /*
20530Sstevel@tonic-gate  * Delete (unlink and free).
20540Sstevel@tonic-gate  * Handles delete of things that have not yet been inserted in the list
20550Sstevel@tonic-gate  * i.e. dr_physical is NULL.
20560Sstevel@tonic-gate  */
20570Sstevel@tonic-gate static void
20580Sstevel@tonic-gate router_delete(struct router *dr)
20590Sstevel@tonic-gate {
20600Sstevel@tonic-gate 	struct phyint *pi;
20610Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	if (debug & D_ROUTER) {
20640Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_delete(%s, %s, %u)\n",
20650Sstevel@tonic-gate 		    dr->dr_physical->pi_name,
20660Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&dr->dr_address,
20670Sstevel@tonic-gate 		    abuf, sizeof (abuf)), dr->dr_lifetime);
20680Sstevel@tonic-gate 	}
20690Sstevel@tonic-gate 	pi = dr->dr_physical;
20701577Sseb 	if (dr->dr_inkernel && (pi->pi_kernel_state & PI_PRESENT))
20711577Sseb 		router_delete_k(dr);
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	if (dr->dr_prev == NULL) {
20740Sstevel@tonic-gate 		if (pi != NULL)
20750Sstevel@tonic-gate 			pi->pi_router_list = dr->dr_next;
20760Sstevel@tonic-gate 	} else {
20770Sstevel@tonic-gate 		dr->dr_prev->dr_next = dr->dr_next;
20780Sstevel@tonic-gate 	}
20790Sstevel@tonic-gate 	if (dr->dr_next != NULL)
20800Sstevel@tonic-gate 		dr->dr_next->dr_prev = dr->dr_prev;
20810Sstevel@tonic-gate 	dr->dr_next = dr->dr_prev = NULL;
20820Sstevel@tonic-gate 	free(dr);
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate /*
20860Sstevel@tonic-gate  * Update the kernel to match dr_lifetime
20870Sstevel@tonic-gate  */
20880Sstevel@tonic-gate void
20890Sstevel@tonic-gate router_update_k(struct router *dr)
20900Sstevel@tonic-gate {
20910Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	if (debug & D_ROUTER) {
20940Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_update_k(%s, %s, %u)\n",
20950Sstevel@tonic-gate 		    dr->dr_physical->pi_name,
20960Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&dr->dr_address,
20970Sstevel@tonic-gate 		    abuf, sizeof (abuf)), dr->dr_lifetime);
20980Sstevel@tonic-gate 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	if (dr->dr_lifetime == 0 && dr->dr_inkernel) {
21010Sstevel@tonic-gate 		/* Log a message when last router goes away */
21020Sstevel@tonic-gate 		if (dr->dr_physical->pi_num_k_routers == 1) {
21030Sstevel@tonic-gate 			logmsg(LOG_WARNING,
21040Sstevel@tonic-gate 			    "Last default router (%s) removed on %s\n",
21050Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&dr->dr_address,
21060Sstevel@tonic-gate 			    abuf, sizeof (abuf)), dr->dr_physical->pi_name);
21070Sstevel@tonic-gate 		}
21080Sstevel@tonic-gate 		router_delete(dr);
21091577Sseb 	} else if (dr->dr_lifetime != 0 && !dr->dr_inkernel)
21100Sstevel@tonic-gate 		router_add_k(dr);
21110Sstevel@tonic-gate }
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate /*
21150Sstevel@tonic-gate  * Called with the number of millseconds elapsed since the last call.
21160Sstevel@tonic-gate  * Determines if any timeout event has occurred and
21170Sstevel@tonic-gate  * returns the number of milliseconds until the next timeout event.
21180Sstevel@tonic-gate  * Returns TIMER_INFINITY for "never".
21190Sstevel@tonic-gate  */
21200Sstevel@tonic-gate uint_t
21210Sstevel@tonic-gate router_timer(struct router *dr, uint_t elapsed)
21220Sstevel@tonic-gate {
21230Sstevel@tonic-gate 	uint_t next = TIMER_INFINITY;
21240Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	if (debug & D_ROUTER) {
21270Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_timer(%s, %s, %u, %d)\n",
21280Sstevel@tonic-gate 		    dr->dr_physical->pi_name,
21290Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&dr->dr_address,
21300Sstevel@tonic-gate 		    abuf, sizeof (abuf)), dr->dr_lifetime, elapsed);
21310Sstevel@tonic-gate 	}
21320Sstevel@tonic-gate 	if (dr->dr_lifetime <= elapsed) {
21330Sstevel@tonic-gate 		dr->dr_lifetime = 0;
21340Sstevel@tonic-gate 	} else {
21350Sstevel@tonic-gate 		dr->dr_lifetime -= elapsed;
21360Sstevel@tonic-gate 		if (dr->dr_lifetime < next)
21370Sstevel@tonic-gate 			next = dr->dr_lifetime;
21380Sstevel@tonic-gate 	}
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if (dr->dr_lifetime == 0) {
21410Sstevel@tonic-gate 		/* Log a message when last router goes away */
21420Sstevel@tonic-gate 		if (dr->dr_physical->pi_num_k_routers == 1) {
21430Sstevel@tonic-gate 			logmsg(LOG_WARNING,
21440Sstevel@tonic-gate 			    "Last default router (%s) timed out on %s\n",
21450Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&dr->dr_address,
21460Sstevel@tonic-gate 			    abuf, sizeof (abuf)), dr->dr_physical->pi_name);
21470Sstevel@tonic-gate 		}
21480Sstevel@tonic-gate 		router_delete(dr);
21490Sstevel@tonic-gate 	}
21500Sstevel@tonic-gate 	return (next);
21510Sstevel@tonic-gate }
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate /*
21540Sstevel@tonic-gate  * Add a default route to the kernel (unless the lifetime is zero)
21550Sstevel@tonic-gate  * Handles onlink default routes.
21560Sstevel@tonic-gate  */
21570Sstevel@tonic-gate static void
21580Sstevel@tonic-gate router_add_k(struct router *dr)
21590Sstevel@tonic-gate {
21600Sstevel@tonic-gate 	struct phyint *pi = dr->dr_physical;
21610Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
21620Sstevel@tonic-gate 	int rlen;
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	if (debug & D_ROUTER) {
21650Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_add_k(%s, %s, %u)\n",
21660Sstevel@tonic-gate 		    dr->dr_physical->pi_name,
21670Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&dr->dr_address,
21680Sstevel@tonic-gate 		    abuf, sizeof (abuf)), dr->dr_lifetime);
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	rta_gateway->sin6_addr = dr->dr_address;
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	rta_ifp->sdl_index = if_nametoindex(pi->pi_name);
21740Sstevel@tonic-gate 	if (rta_ifp->sdl_index == 0) {
21750Sstevel@tonic-gate 		logperror_pi(pi, "router_add_k: if_nametoindex");
21760Sstevel@tonic-gate 		return;
21770Sstevel@tonic-gate 	}
21780Sstevel@tonic-gate 
21791577Sseb 	rt_msg->rtm_flags = RTF_GATEWAY;
21800Sstevel@tonic-gate 	rt_msg->rtm_type = RTM_ADD;
21810Sstevel@tonic-gate 	rt_msg->rtm_seq = ++rtmseq;
21820Sstevel@tonic-gate 	rlen = write(rtsock, rt_msg, rt_msg->rtm_msglen);
21830Sstevel@tonic-gate 	if (rlen < 0) {
21840Sstevel@tonic-gate 		if (errno != EEXIST) {
21850Sstevel@tonic-gate 			logperror_pi(pi, "router_add_k: RTM_ADD");
21860Sstevel@tonic-gate 			return;
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 	} else if (rlen < rt_msg->rtm_msglen) {
21890Sstevel@tonic-gate 		logmsg(LOG_ERR, "router_add_k: write to routing socket got "
21900Sstevel@tonic-gate 		    "only %d for rlen (interface %s)\n", rlen, pi->pi_name);
21910Sstevel@tonic-gate 		return;
21920Sstevel@tonic-gate 	}
21930Sstevel@tonic-gate 	dr->dr_inkernel = _B_TRUE;
21941577Sseb 	pi->pi_num_k_routers++;
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate /*
21980Sstevel@tonic-gate  * Delete a route from the kernel.
21990Sstevel@tonic-gate  * Handles onlink default routes.
22000Sstevel@tonic-gate  */
22010Sstevel@tonic-gate static void
22020Sstevel@tonic-gate router_delete_k(struct router *dr)
22030Sstevel@tonic-gate {
22040Sstevel@tonic-gate 	struct phyint *pi = dr->dr_physical;
22050Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
22060Sstevel@tonic-gate 	int rlen;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	if (debug & D_ROUTER) {
22090Sstevel@tonic-gate 		logmsg(LOG_DEBUG, "router_delete_k(%s, %s, %u)\n",
22100Sstevel@tonic-gate 		    dr->dr_physical->pi_name,
22110Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&dr->dr_address,
22120Sstevel@tonic-gate 		    abuf, sizeof (abuf)), dr->dr_lifetime);
22130Sstevel@tonic-gate 	}
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	rta_gateway->sin6_addr = dr->dr_address;
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	rta_ifp->sdl_index = if_nametoindex(pi->pi_name);
22180Sstevel@tonic-gate 	if (rta_ifp->sdl_index == 0) {
22190Sstevel@tonic-gate 		logperror_pi(pi, "router_delete_k: if_nametoindex");
22200Sstevel@tonic-gate 		return;
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
22231577Sseb 	rt_msg->rtm_flags = RTF_GATEWAY;
22240Sstevel@tonic-gate 	rt_msg->rtm_type = RTM_DELETE;
22250Sstevel@tonic-gate 	rt_msg->rtm_seq = ++rtmseq;
22260Sstevel@tonic-gate 	rlen = write(rtsock, rt_msg, rt_msg->rtm_msglen);
22270Sstevel@tonic-gate 	if (rlen < 0) {
22280Sstevel@tonic-gate 		if (errno != ESRCH) {
22290Sstevel@tonic-gate 			logperror_pi(pi, "router_delete_k: RTM_DELETE");
22300Sstevel@tonic-gate 		}
22310Sstevel@tonic-gate 	} else if (rlen < rt_msg->rtm_msglen) {
22320Sstevel@tonic-gate 		logmsg(LOG_ERR, "router_delete_k: write to routing socket got "
22330Sstevel@tonic-gate 		    "only %d for rlen (interface %s)\n", rlen, pi->pi_name);
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 	dr->dr_inkernel = _B_FALSE;
22361577Sseb 	pi->pi_num_k_routers--;
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate static void
22410Sstevel@tonic-gate router_print(struct router *dr)
22420Sstevel@tonic-gate {
22430Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
22440Sstevel@tonic-gate 
22451577Sseb 	logmsg(LOG_DEBUG, "Router %s on %s inkernel %d lifetime %u\n",
22461577Sseb 	    inet_ntop(AF_INET6, (void *)&dr->dr_address, abuf, sizeof (abuf)),
22471577Sseb 	    dr->dr_physical->pi_name, dr->dr_inkernel, dr->dr_lifetime);
22480Sstevel@tonic-gate }
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate void
22520Sstevel@tonic-gate phyint_print_all(void)
22530Sstevel@tonic-gate {
22540Sstevel@tonic-gate 	struct phyint *pi;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
22570Sstevel@tonic-gate 		phyint_print(pi);
22580Sstevel@tonic-gate 	}
22590Sstevel@tonic-gate }
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate void
22620Sstevel@tonic-gate phyint_cleanup(pi)
22630Sstevel@tonic-gate 	struct phyint *pi;
22640Sstevel@tonic-gate {
22650Sstevel@tonic-gate 	pi->pi_state = 0;
22660Sstevel@tonic-gate 	pi->pi_kernel_state = 0;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	if (pi->pi_AdvSendAdvertisements) {
22690Sstevel@tonic-gate 		check_to_advertise(pi, ADV_OFF);
22700Sstevel@tonic-gate 	} else {
22710Sstevel@tonic-gate 		check_to_solicit(pi, SOLICIT_OFF);
22720Sstevel@tonic-gate 	}
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	while (pi->pi_router_list)
22750Sstevel@tonic-gate 		router_delete(pi->pi_router_list);
22760Sstevel@tonic-gate 	(void) poll_remove(pi->pi_sock);
22770Sstevel@tonic-gate 	(void) close(pi->pi_sock);
22780Sstevel@tonic-gate 	pi->pi_sock = -1;
22790Sstevel@tonic-gate }
2280