xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_if.c (revision 2546:3e3e9857b7e6)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * 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 /*
221676Sjpk  * 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  * Copyright (c) 1990 Mentat Inc.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * This file contains the interface control functions for IPv6.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/stream.h>
380Sstevel@tonic-gate #include <sys/dlpi.h>
390Sstevel@tonic-gate #include <sys/stropts.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/kstat.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/zone.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/systm.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/socket.h>
490Sstevel@tonic-gate #include <sys/isa_defs.h>
500Sstevel@tonic-gate #include <net/if.h>
510Sstevel@tonic-gate #include <net/if_dl.h>
520Sstevel@tonic-gate #include <net/route.h>
530Sstevel@tonic-gate #include <netinet/in.h>
540Sstevel@tonic-gate #include <netinet/igmp_var.h>
550Sstevel@tonic-gate #include <netinet/ip6.h>
560Sstevel@tonic-gate #include <netinet/icmp6.h>
570Sstevel@tonic-gate #include <netinet/in.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <inet/common.h>
600Sstevel@tonic-gate #include <inet/nd.h>
610Sstevel@tonic-gate #include <inet/mib2.h>
620Sstevel@tonic-gate #include <inet/ip.h>
630Sstevel@tonic-gate #include <inet/ip6.h>
640Sstevel@tonic-gate #include <inet/ip_multi.h>
650Sstevel@tonic-gate #include <inet/ip_ire.h>
660Sstevel@tonic-gate #include <inet/ip_rts.h>
670Sstevel@tonic-gate #include <inet/ip_ndp.h>
680Sstevel@tonic-gate #include <inet/ip_if.h>
690Sstevel@tonic-gate #include <inet/ip6_asp.h>
700Sstevel@tonic-gate #include <inet/tun.h>
710Sstevel@tonic-gate #include <inet/ipclassifier.h>
720Sstevel@tonic-gate #include <inet/sctp_ip.h>
730Sstevel@tonic-gate 
741676Sjpk #include <sys/tsol/tndb.h>
751676Sjpk #include <sys/tsol/tnet.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static in6_addr_t	ipv6_ll_template =
780Sstevel@tonic-gate 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static ipif_t *
810Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
820Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * ipif_lookup_group_v6
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate ipif_t *
880Sstevel@tonic-gate ipif_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	ire_t	*ire;
910Sstevel@tonic-gate 	ipif_t	*ipif;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	ire = ire_lookup_multi_v6(group, zoneid);
940Sstevel@tonic-gate 	if (ire == NULL)
950Sstevel@tonic-gate 		return (NULL);
960Sstevel@tonic-gate 	ipif = ire->ire_ipif;
970Sstevel@tonic-gate 	ipif_refhold(ipif);
980Sstevel@tonic-gate 	ire_refrele(ire);
990Sstevel@tonic-gate 	return (ipif);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * ill_lookup_group_v6
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate ill_t *
1060Sstevel@tonic-gate ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	ire_t	*ire;
1090Sstevel@tonic-gate 	ill_t	*ill;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	ire = ire_lookup_multi_v6(group, zoneid);
1120Sstevel@tonic-gate 	if (ire == NULL)
1130Sstevel@tonic-gate 		return (NULL);
1140Sstevel@tonic-gate 	ill = ire->ire_ipif->ipif_ill;
1150Sstevel@tonic-gate 	ill_refhold(ill);
1160Sstevel@tonic-gate 	ire_refrele(ire);
1170Sstevel@tonic-gate 	return (ill);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Look for an ipif with the specified interface address and destination.
1220Sstevel@tonic-gate  * The destination address is used only for matching point-to-point interfaces.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate static ipif_t *
1250Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
1260Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	ipif_t	*ipif;
1290Sstevel@tonic-gate 	ill_t	*ill;
1300Sstevel@tonic-gate 	ipsq_t	*ipsq;
1310Sstevel@tonic-gate 	ill_walk_context_t ctx;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (error != NULL)
1340Sstevel@tonic-gate 		*error = 0;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * First match all the point-to-point interfaces
1380Sstevel@tonic-gate 	 * before looking at non-point-to-point interfaces.
1390Sstevel@tonic-gate 	 * This is done to avoid returning non-point-to-point
1400Sstevel@tonic-gate 	 * ipif instead of unnumbered point-to-point ipif.
1410Sstevel@tonic-gate 	 */
1420Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
1430Sstevel@tonic-gate 	ill = ILL_START_WALK_V6(&ctx);
1440Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1450Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
1460Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
1470Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
1480Sstevel@tonic-gate 			/* Allow the ipif to be down */
1490Sstevel@tonic-gate 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
1500Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1510Sstevel@tonic-gate 			    if_addr)) &&
1520Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
1530Sstevel@tonic-gate 			    dst))) {
1540Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
1550Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
1560Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1570Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1580Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
1590Sstevel@tonic-gate 					return (ipif);
1600Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
1610Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
1620Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
1630Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1640Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
1650Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
1660Sstevel@tonic-gate 						ill);
1670Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
1680Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1690Sstevel@tonic-gate 					*error = EINPROGRESS;
1700Sstevel@tonic-gate 					return (NULL);
1710Sstevel@tonic-gate 				}
1720Sstevel@tonic-gate 			}
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
1750Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
1780Sstevel@tonic-gate 	/* lookup the ipif based on interface address */
1790Sstevel@tonic-gate 	ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, q, mp, func,
1800Sstevel@tonic-gate 	    error);
1810Sstevel@tonic-gate 	ASSERT(ipif == NULL || ipif->ipif_isv6);
1820Sstevel@tonic-gate 	return (ipif);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Look for an ipif with the specified address. For point-point links
1870Sstevel@tonic-gate  * we look for matches on either the destination address and the local
1880Sstevel@tonic-gate  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
1890Sstevel@tonic-gate  * is set.
1900Sstevel@tonic-gate  * Matches on a specific ill if match_ill is set.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate /* ARGSUSED */
1930Sstevel@tonic-gate ipif_t *
1940Sstevel@tonic-gate ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
1950Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	ipif_t	*ipif;
1980Sstevel@tonic-gate 	ill_t	*ill;
1990Sstevel@tonic-gate 	boolean_t  ptp = B_FALSE;
2000Sstevel@tonic-gate 	ipsq_t	*ipsq;
2010Sstevel@tonic-gate 	ill_walk_context_t ctx;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (error != NULL)
2040Sstevel@tonic-gate 		*error = 0;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
2070Sstevel@tonic-gate 	/*
2080Sstevel@tonic-gate 	 * Repeat twice, first based on local addresses and
2090Sstevel@tonic-gate 	 * next time for pointopoint.
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate repeat:
2120Sstevel@tonic-gate 	ill = ILL_START_WALK_V6(&ctx);
2130Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2140Sstevel@tonic-gate 		if (match_ill != NULL && ill != match_ill) {
2150Sstevel@tonic-gate 			continue;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
2180Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
2190Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
2201676Sjpk 			if (zoneid != ALL_ZONES &&
2211676Sjpk 			    ipif->ipif_zoneid != zoneid &&
2221676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
2230Sstevel@tonic-gate 				continue;
2240Sstevel@tonic-gate 			/* Allow the ipif to be down */
2250Sstevel@tonic-gate 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
2260Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, addr) &&
2270Sstevel@tonic-gate 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
2280Sstevel@tonic-gate 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
2290Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
2300Sstevel@tonic-gate 			    addr))) {
2310Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
2320Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
2330Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2340Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2350Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
2360Sstevel@tonic-gate 					return (ipif);
2370Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
2380Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
2390Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
2400Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2410Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
2420Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
2430Sstevel@tonic-gate 						ill);
2440Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
2450Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2460Sstevel@tonic-gate 					*error = EINPROGRESS;
2470Sstevel@tonic-gate 					return (NULL);
2480Sstevel@tonic-gate 				}
2490Sstevel@tonic-gate 			}
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2520Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* Repeat once more if needed */
2560Sstevel@tonic-gate 	if (ptp) {
2570Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
2580Sstevel@tonic-gate 		if (error != NULL)
2590Sstevel@tonic-gate 			*error = ENXIO;
2600Sstevel@tonic-gate 		return (NULL);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	ptp = B_TRUE;
2630Sstevel@tonic-gate 	goto repeat;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a local
2680Sstevel@tonic-gate  * interface address.  This is currently only called when an attempt is made
2690Sstevel@tonic-gate  * to set a local address.
2700Sstevel@tonic-gate  *
2710Sstevel@tonic-gate  * Does not allow a v4-mapped address, an address that equals the subnet
2720Sstevel@tonic-gate  * anycast address, ... a multicast address, ...
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate boolean_t
2750Sstevel@tonic-gate ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	in6_addr_t subnet;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
2800Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * Don't allow all zeroes or host part, but allow
2840Sstevel@tonic-gate 	 * all ones netmask.
2850Sstevel@tonic-gate 	 */
2860Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
2870Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
2880Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
2890Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
2900Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
2910Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr))
2920Sstevel@tonic-gate 		return (B_FALSE);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (B_TRUE);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a
2990Sstevel@tonic-gate  * remote/subnet interface address.
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate boolean_t
3020Sstevel@tonic-gate ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	in6_addr_t subnet;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
3070Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
3100Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
3110Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
3120Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
3130Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr) ||
3140Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
3150Sstevel@tonic-gate 		return (B_FALSE);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	return (B_TRUE);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
3220Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
3230Sstevel@tonic-gate  * (for link-local destinations and gateways).
3240Sstevel@tonic-gate  */
3250Sstevel@tonic-gate /* ARGSUSED1 */
3260Sstevel@tonic-gate int
3270Sstevel@tonic-gate ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
3280Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
3291676Sjpk     ipif_t *ipif_arg, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
3301676Sjpk     struct rtsa_s *sp)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	ire_t	*ire;
3330Sstevel@tonic-gate 	ire_t	*gw_ire = NULL;
3340Sstevel@tonic-gate 	ipif_t	*ipif;
3350Sstevel@tonic-gate 	boolean_t ipif_refheld = B_FALSE;
3360Sstevel@tonic-gate 	uint_t	type;
3370Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
3380Sstevel@tonic-gate 	int	error;
3391676Sjpk 	tsol_gc_t *gc = NULL;
3401676Sjpk 	tsol_gcgrp_t *gcgrp = NULL;
3411676Sjpk 	boolean_t gcgrp_xtraref = B_FALSE;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (ire_arg != NULL)
3440Sstevel@tonic-gate 		*ire_arg = NULL;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	/*
3470Sstevel@tonic-gate 	 * Prevent routes with a zero gateway from being created (since
3480Sstevel@tonic-gate 	 * interfaces can currently be plumbed and brought up with no assigned
3490Sstevel@tonic-gate 	 * address).
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
3520Sstevel@tonic-gate 		return (ENETUNREACH);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
3560Sstevel@tonic-gate 	 * to all ones (regardless if one was supplied).
3570Sstevel@tonic-gate 	 */
3580Sstevel@tonic-gate 	if (flags & RTF_HOST)
3590Sstevel@tonic-gate 		mask = &ipv6_all_ones;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Get the ipif, if any, corresponding to the gw_addr
3630Sstevel@tonic-gate 	 */
3640Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func,
3650Sstevel@tonic-gate 	    &error);
3660Sstevel@tonic-gate 	if (ipif != NULL)
3670Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
3680Sstevel@tonic-gate 	else if (error == EINPROGRESS) {
3690Sstevel@tonic-gate 		ip1dbg(("ip_rt_add_v6: null and EINPROGRESS"));
3700Sstevel@tonic-gate 		return (error);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * GateD will attempt to create routes with a loopback interface
3750Sstevel@tonic-gate 	 * address as the gateway and with RTF_GATEWAY set.  We allow
3760Sstevel@tonic-gate 	 * these routes to be added, but create them as interface routes
3770Sstevel@tonic-gate 	 * since the gateway is an interface address.
3780Sstevel@tonic-gate 	 */
3791822Srk129064 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
3800Sstevel@tonic-gate 		flags &= ~RTF_GATEWAY;
3811822Srk129064 		if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) &&
3821822Srk129064 		    IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) &&
3831822Srk129064 		    IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) {
3841822Srk129064 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
3851822Srk129064 			    ipif, ALL_ZONES, NULL, match_flags);
3861822Srk129064 			if (ire != NULL) {
3871822Srk129064 				ire_refrele(ire);
3881822Srk129064 				if (ipif_refheld)
3891822Srk129064 					ipif_refrele(ipif);
3901822Srk129064 				return (EEXIST);
3911822Srk129064 			}
3921822Srk129064 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
3931822Srk129064 			    "for 0x%x\n", (void *)ipif,
3941822Srk129064 			    ipif->ipif_ire_type,
3951822Srk129064 			    ntohl(ipif->ipif_lcl_addr)));
3961822Srk129064 			ire = ire_create_v6(
3971822Srk129064 			    dst_addr,
3981822Srk129064 			    mask,
3991822Srk129064 			    &ipif->ipif_v6src_addr,
4001822Srk129064 			    NULL,
4011822Srk129064 			    &ipif->ipif_mtu,
4021822Srk129064 			    NULL,
4031822Srk129064 			    NULL,
4041822Srk129064 			    NULL,
4051822Srk129064 			    ipif->ipif_net_type,
4061822Srk129064 			    ipif->ipif_resolver_mp,
4071822Srk129064 			    ipif,
4081822Srk129064 			    NULL,
4091822Srk129064 			    0,
4101822Srk129064 			    0,
4111822Srk129064 			    flags,
4121822Srk129064 			    &ire_uinfo_null,
4131822Srk129064 			    NULL,
4141822Srk129064 			    NULL);
4151822Srk129064 			if (ire == NULL) {
4161822Srk129064 				if (ipif_refheld)
4171822Srk129064 					ipif_refrele(ipif);
4181822Srk129064 				return (ENOMEM);
4191822Srk129064 			}
4202535Ssangeeta 			error = ire_add(&ire, q, mp, func, B_FALSE);
4211822Srk129064 			if (error == 0)
4221822Srk129064 				goto save_ire;
4231822Srk129064 			/*
4241822Srk129064 			 * In the result of failure, ire_add() will have already
4251822Srk129064 			 * deleted the ire in question, so there is no need to
4261822Srk129064 			 * do that here.
4271822Srk129064 			 */
4281822Srk129064 			if (ipif_refheld)
4291822Srk129064 				ipif_refrele(ipif);
4301822Srk129064 			return (error);
4311822Srk129064 		}
4321822Srk129064 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
4360Sstevel@tonic-gate 	 * and the gateway address provided is one of the system's interface
4370Sstevel@tonic-gate 	 * addresses.  By using the routing socket interface and supplying an
4380Sstevel@tonic-gate 	 * RTA_IFP sockaddr with an interface index, an alternate method of
4390Sstevel@tonic-gate 	 * specifying an interface route to be created is available which uses
4400Sstevel@tonic-gate 	 * the interface index that specifies the outgoing interface rather than
4410Sstevel@tonic-gate 	 * the address of an outgoing interface (which may not be able to
4420Sstevel@tonic-gate 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
4430Sstevel@tonic-gate 	 * flag, routes can be specified which not only specify the next-hop to
4440Sstevel@tonic-gate 	 * be used when routing to a certain prefix, but also which outgoing
4450Sstevel@tonic-gate 	 * interface should be used.
4460Sstevel@tonic-gate 	 *
4470Sstevel@tonic-gate 	 * Previously, interfaces would have unique addresses assigned to them
4480Sstevel@tonic-gate 	 * and so the address assigned to a particular interface could be used
4490Sstevel@tonic-gate 	 * to identify a particular interface.  One exception to this was the
4500Sstevel@tonic-gate 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
4510Sstevel@tonic-gate 	 *
4520Sstevel@tonic-gate 	 * With the advent of IPv6 and its link-local addresses, this
4530Sstevel@tonic-gate 	 * restriction was relaxed and interfaces could share addresses between
4540Sstevel@tonic-gate 	 * themselves.  In fact, typically all of the link-local interfaces on
4550Sstevel@tonic-gate 	 * an IPv6 node or router will have the same link-local address.  In
4560Sstevel@tonic-gate 	 * order to differentiate between these interfaces, the use of an
4570Sstevel@tonic-gate 	 * interface index is necessary and this index can be carried inside a
4580Sstevel@tonic-gate 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
4590Sstevel@tonic-gate 	 * of using the interface index, however, is that all of the ipif's that
4600Sstevel@tonic-gate 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
4610Sstevel@tonic-gate 	 * cannot be used to differentiate between ipif's (or logical
4620Sstevel@tonic-gate 	 * interfaces) that belong to the same ill (physical interface).
4630Sstevel@tonic-gate 	 *
4640Sstevel@tonic-gate 	 * For example, in the following case involving IPv4 interfaces and
4650Sstevel@tonic-gate 	 * logical interfaces
4660Sstevel@tonic-gate 	 *
4670Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
4680Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
4690Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
4700Sstevel@tonic-gate 	 *
4710Sstevel@tonic-gate 	 * the ipif's corresponding to each of these interface routes can be
4720Sstevel@tonic-gate 	 * uniquely identified by the "gateway" (actually interface address).
4730Sstevel@tonic-gate 	 *
4740Sstevel@tonic-gate 	 * In this case involving multiple IPv6 default routes to a particular
4750Sstevel@tonic-gate 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
4760Sstevel@tonic-gate 	 * default route is of interest:
4770Sstevel@tonic-gate 	 *
4780Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if0
4790Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if1
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/* RTF_GATEWAY not set */
4830Sstevel@tonic-gate 	if (!(flags & RTF_GATEWAY)) {
4840Sstevel@tonic-gate 		queue_t	*stq;
4850Sstevel@tonic-gate 
4861676Sjpk 		if (sp != NULL) {
4871676Sjpk 			ip2dbg(("ip_rt_add_v6: gateway security attributes "
4881676Sjpk 			    "cannot be set with interface route\n"));
4891676Sjpk 			if (ipif_refheld)
4901676Sjpk 				ipif_refrele(ipif);
4911676Sjpk 			return (EINVAL);
4921676Sjpk 		}
4931676Sjpk 
4940Sstevel@tonic-gate 		/*
4950Sstevel@tonic-gate 		 * As the interface index specified with the RTA_IFP sockaddr is
4960Sstevel@tonic-gate 		 * the same for all ipif's off of an ill, the matching logic
4970Sstevel@tonic-gate 		 * below uses MATCH_IRE_ILL if such an index was specified.
4980Sstevel@tonic-gate 		 * This means that routes sharing the same prefix when added
4990Sstevel@tonic-gate 		 * using a RTA_IFP sockaddr must have distinct interface
5000Sstevel@tonic-gate 		 * indices (namely, they must be on distinct ill's).
5010Sstevel@tonic-gate 		 *
5020Sstevel@tonic-gate 		 * On the other hand, since the gateway address will usually be
5030Sstevel@tonic-gate 		 * different for each ipif on the system, the matching logic
5040Sstevel@tonic-gate 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
5050Sstevel@tonic-gate 		 * route.  This means that interface routes for the same prefix
5060Sstevel@tonic-gate 		 * can be created if they belong to distinct ipif's and if a
5070Sstevel@tonic-gate 		 * RTA_IFP sockaddr is not present.
5080Sstevel@tonic-gate 		 */
5090Sstevel@tonic-gate 		if (ipif_arg != NULL) {
5100Sstevel@tonic-gate 			if (ipif_refheld) {
5110Sstevel@tonic-gate 				ipif_refrele(ipif);
5120Sstevel@tonic-gate 				ipif_refheld = B_FALSE;
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 			ipif = ipif_arg;
5150Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
5160Sstevel@tonic-gate 		} else {
5170Sstevel@tonic-gate 			/*
5180Sstevel@tonic-gate 			 * Check the ipif corresponding to the gw_addr
5190Sstevel@tonic-gate 			 */
5200Sstevel@tonic-gate 			if (ipif == NULL)
5210Sstevel@tonic-gate 				return (ENETUNREACH);
5220Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		ASSERT(ipif != NULL);
5260Sstevel@tonic-gate 		/*
5270Sstevel@tonic-gate 		 * We check for an existing entry at this point.
5280Sstevel@tonic-gate 		 */
5290Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
5300Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, 0, IRE_INTERFACE,
5311676Sjpk 		    ipif, NULL, ALL_ZONES, 0, NULL, match_flags);
5320Sstevel@tonic-gate 		if (ire != NULL) {
5330Sstevel@tonic-gate 			ire_refrele(ire);
5340Sstevel@tonic-gate 			if (ipif_refheld)
5350Sstevel@tonic-gate 				ipif_refrele(ipif);
5360Sstevel@tonic-gate 			return (EEXIST);
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
5400Sstevel@tonic-gate 		    ? ipif->ipif_rq : ipif->ipif_wq;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		/*
5430Sstevel@tonic-gate 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5440Sstevel@tonic-gate 		 * IRE_IF_RESOLVER with the modified address and netmask.
5450Sstevel@tonic-gate 		 */
5460Sstevel@tonic-gate 		ire = ire_create_v6(
5470Sstevel@tonic-gate 		    dst_addr,
5480Sstevel@tonic-gate 		    mask,
5490Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr,
5500Sstevel@tonic-gate 		    NULL,
5510Sstevel@tonic-gate 		    &ipif->ipif_mtu,
5520Sstevel@tonic-gate 		    NULL,
5530Sstevel@tonic-gate 		    NULL,
5540Sstevel@tonic-gate 		    stq,
5550Sstevel@tonic-gate 		    ipif->ipif_net_type,
5560Sstevel@tonic-gate 		    ipif->ipif_resolver_mp,
5570Sstevel@tonic-gate 		    ipif,
5580Sstevel@tonic-gate 		    NULL,
5590Sstevel@tonic-gate 		    0,
5600Sstevel@tonic-gate 		    0,
5610Sstevel@tonic-gate 		    flags,
5621676Sjpk 		    &ire_uinfo_null,
5631676Sjpk 		    NULL,
5641676Sjpk 		    NULL);
5650Sstevel@tonic-gate 		if (ire == NULL) {
5660Sstevel@tonic-gate 			if (ipif_refheld)
5670Sstevel@tonic-gate 				ipif_refrele(ipif);
5680Sstevel@tonic-gate 			return (ENOMEM);
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		/*
5720Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
5730Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
5740Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
5750Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
5760Sstevel@tonic-gate 		 * when generating aggregate routes.)
5770Sstevel@tonic-gate 		 *
5780Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
5790Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
5800Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
5810Sstevel@tonic-gate 		 *
5820Sstevel@tonic-gate 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5830Sstevel@tonic-gate 		 * routine, but rather using ire_create_v6() directly.
5840Sstevel@tonic-gate 		 */
5850Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
5860Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
5872535Ssangeeta 		error = ire_add(&ire, q, mp, func, B_FALSE);
5880Sstevel@tonic-gate 		if (error == 0)
5890Sstevel@tonic-gate 			goto save_ire;
5900Sstevel@tonic-gate 		/*
5910Sstevel@tonic-gate 		 * In the result of failure, ire_add() will have already
5920Sstevel@tonic-gate 		 * deleted the ire in question, so there is no need to
5930Sstevel@tonic-gate 		 * do that here.
5940Sstevel@tonic-gate 		 */
5950Sstevel@tonic-gate 		if (ipif_refheld)
5960Sstevel@tonic-gate 			ipif_refrele(ipif);
5970Sstevel@tonic-gate 		return (error);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 	if (ipif_refheld) {
6000Sstevel@tonic-gate 		ipif_refrele(ipif);
6010Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/*
6050Sstevel@tonic-gate 	 * Get an interface IRE for the specified gateway.
6060Sstevel@tonic-gate 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6070Sstevel@tonic-gate 	 * gateway, it is currently unreachable and we fail the request
6080Sstevel@tonic-gate 	 * accordingly.
6090Sstevel@tonic-gate 	 */
6100Sstevel@tonic-gate 	ipif = ipif_arg;
6110Sstevel@tonic-gate 	if (ipif_arg != NULL)
6120Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
6130Sstevel@tonic-gate 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg,
6141676Sjpk 	    NULL, ALL_ZONES, 0, NULL, match_flags);
6150Sstevel@tonic-gate 	if (gw_ire == NULL)
6160Sstevel@tonic-gate 		return (ENETUNREACH);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * We create one of three types of IREs as a result of this request
6200Sstevel@tonic-gate 	 * based on the netmask.  A netmask of all ones (which is automatically
6210Sstevel@tonic-gate 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6220Sstevel@tonic-gate 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6230Sstevel@tonic-gate 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6240Sstevel@tonic-gate 	 * destination prefix.
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
6270Sstevel@tonic-gate 		type = IRE_HOST;
6280Sstevel@tonic-gate 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
6290Sstevel@tonic-gate 		type = IRE_DEFAULT;
6300Sstevel@tonic-gate 	else
6310Sstevel@tonic-gate 		type = IRE_PREFIX;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/* check for a duplicate entry */
6340Sstevel@tonic-gate 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ipif_arg,
6351676Sjpk 	    NULL, ALL_ZONES, 0, NULL,
6361676Sjpk 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6370Sstevel@tonic-gate 	if (ire != NULL) {
6380Sstevel@tonic-gate 		ire_refrele(gw_ire);
6390Sstevel@tonic-gate 		ire_refrele(ire);
6400Sstevel@tonic-gate 		return (EEXIST);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6431676Sjpk 	/* Security attribute exists */
6441676Sjpk 	if (sp != NULL) {
6451676Sjpk 		tsol_gcgrp_addr_t ga;
6461676Sjpk 
6471676Sjpk 		/* find or create the gateway credentials group */
6481676Sjpk 		ga.ga_af = AF_INET6;
6491676Sjpk 		ga.ga_addr = *gw_addr;
6501676Sjpk 
6511676Sjpk 		/* we hold reference to it upon success */
6521676Sjpk 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6531676Sjpk 		if (gcgrp == NULL) {
6541676Sjpk 			ire_refrele(gw_ire);
6551676Sjpk 			return (ENOMEM);
6561676Sjpk 		}
6571676Sjpk 
6581676Sjpk 		/*
6591676Sjpk 		 * Create and add the security attribute to the group; a
6601676Sjpk 		 * reference to the group is made upon allocating a new
6611676Sjpk 		 * entry successfully.  If it finds an already-existing
6621676Sjpk 		 * entry for the security attribute in the group, it simply
6631676Sjpk 		 * returns it and no new reference is made to the group.
6641676Sjpk 		 */
6651676Sjpk 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6661676Sjpk 		if (gc == NULL) {
6671676Sjpk 			/* release reference held by gcgrp_lookup */
6681676Sjpk 			GCGRP_REFRELE(gcgrp);
6691676Sjpk 			ire_refrele(gw_ire);
6701676Sjpk 			return (ENOMEM);
6711676Sjpk 		}
6721676Sjpk 	}
6731676Sjpk 
6740Sstevel@tonic-gate 	/* Create the IRE. */
6750Sstevel@tonic-gate 	ire = ire_create_v6(
6760Sstevel@tonic-gate 	    dst_addr,				/* dest address */
6770Sstevel@tonic-gate 	    mask,				/* mask */
6780Sstevel@tonic-gate 	    /* src address assigned by the caller? */
6790Sstevel@tonic-gate 	    (((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) ?
6800Sstevel@tonic-gate 		src_addr : NULL),
6810Sstevel@tonic-gate 	    gw_addr,				/* gateway address */
6820Sstevel@tonic-gate 	    &gw_ire->ire_max_frag,
6830Sstevel@tonic-gate 	    NULL,				/* no Fast Path header */
6840Sstevel@tonic-gate 	    NULL,				/* no recv-from queue */
6850Sstevel@tonic-gate 	    NULL,				/* no send-to queue */
6860Sstevel@tonic-gate 	    (ushort_t)type,			/* IRE type */
6870Sstevel@tonic-gate 	    NULL,
6880Sstevel@tonic-gate 	    ipif_arg,
6890Sstevel@tonic-gate 	    NULL,
6900Sstevel@tonic-gate 	    0,
6910Sstevel@tonic-gate 	    0,
6920Sstevel@tonic-gate 	    flags,
6931676Sjpk 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6941676Sjpk 	    gc,					/* security attribute */
6951676Sjpk 	    NULL);
6961676Sjpk 	/*
6971676Sjpk 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6981676Sjpk 	 * reference to the 'gcgrp'. We can now release the extra reference
6991676Sjpk 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7001676Sjpk 	 */
7011676Sjpk 	if (gcgrp_xtraref)
7021676Sjpk 		GCGRP_REFRELE(gcgrp);
7030Sstevel@tonic-gate 	if (ire == NULL) {
7041676Sjpk 		if (gc != NULL)
7051676Sjpk 			GC_REFRELE(gc);
7060Sstevel@tonic-gate 		ire_refrele(gw_ire);
7070Sstevel@tonic-gate 		return (ENOMEM);
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7120Sstevel@tonic-gate 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	/* Add the new IRE. */
7162535Ssangeeta 	error = ire_add(&ire, q, mp, func, B_FALSE);
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * In the result of failure, ire_add() will have already
7190Sstevel@tonic-gate 	 * deleted the ire in question, so there is no need to
7200Sstevel@tonic-gate 	 * do that here.
7210Sstevel@tonic-gate 	 */
7220Sstevel@tonic-gate 	if (error != 0) {
7230Sstevel@tonic-gate 		ire_refrele(gw_ire);
7240Sstevel@tonic-gate 		return (error);
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (flags & RTF_MULTIRT) {
7280Sstevel@tonic-gate 		/*
7290Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
7300Sstevel@tonic-gate 		 * to add the dst address in the filtering database.
7310Sstevel@tonic-gate 		 * Replicated inbound packets coming from that address
7320Sstevel@tonic-gate 		 * will be filtered to discard the duplicates.
7330Sstevel@tonic-gate 		 * It is not necessary to call the CGTP filter hook
7340Sstevel@tonic-gate 		 * when the dst address is a multicast, because an
7350Sstevel@tonic-gate 		 * IP source address cannot be a multicast.
7360Sstevel@tonic-gate 		 */
7370Sstevel@tonic-gate 		if ((ip_cgtp_filter_ops != NULL) &&
7380Sstevel@tonic-gate 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
7390Sstevel@tonic-gate 			int res = ip_cgtp_filter_ops->cfo_add_dest_v6(
7400Sstevel@tonic-gate 			    &ire->ire_addr_v6,
7410Sstevel@tonic-gate 			    &ire->ire_gateway_addr_v6,
7420Sstevel@tonic-gate 			    &ire->ire_src_addr_v6,
7430Sstevel@tonic-gate 			    &gw_ire->ire_src_addr_v6);
7440Sstevel@tonic-gate 			if (res != 0) {
7450Sstevel@tonic-gate 				ire_refrele(gw_ire);
7460Sstevel@tonic-gate 				ire_delete(ire);
7470Sstevel@tonic-gate 				return (res);
7480Sstevel@tonic-gate 			}
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7521676Sjpk 	/*
7531676Sjpk 	 * Now that the prefix IRE entry has been created, delete any
7541676Sjpk 	 * existing gateway IRE cache entries as well as any IRE caches
7551676Sjpk 	 * using the gateway, and force them to be created through
7561676Sjpk 	 * ip_newroute_v6.
7571676Sjpk 	 */
7581676Sjpk 	if (gc != NULL) {
7591676Sjpk 		ASSERT(gcgrp != NULL);
7601676Sjpk 		ire_clookup_delete_cache_gw_v6(gw_addr, ALL_ZONES);
7611676Sjpk 	}
7621676Sjpk 
7630Sstevel@tonic-gate save_ire:
7640Sstevel@tonic-gate 	if (gw_ire != NULL) {
7650Sstevel@tonic-gate 		ire_refrele(gw_ire);
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 	if (ipif != NULL) {
7680Sstevel@tonic-gate 		mblk_t	*save_mp;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		/*
7710Sstevel@tonic-gate 		 * Save enough information so that we can recreate the IRE if
7720Sstevel@tonic-gate 		 * the interface goes down and then up.  The metrics associated
7730Sstevel@tonic-gate 		 * with the route will be saved as well when rts_setmetrics() is
7740Sstevel@tonic-gate 		 * called after the IRE has been created.  In the case where
7750Sstevel@tonic-gate 		 * memory cannot be allocated, none of this information will be
7760Sstevel@tonic-gate 		 * saved.
7770Sstevel@tonic-gate 		 */
7780Sstevel@tonic-gate 		save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
7790Sstevel@tonic-gate 		if (save_mp != NULL) {
7800Sstevel@tonic-gate 			ifrt_t	*ifrt;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 			save_mp->b_wptr += sizeof (ifrt_t);
7830Sstevel@tonic-gate 			ifrt = (ifrt_t *)save_mp->b_rptr;
7840Sstevel@tonic-gate 			bzero(ifrt, sizeof (ifrt_t));
7850Sstevel@tonic-gate 			ifrt->ifrt_type = ire->ire_type;
7860Sstevel@tonic-gate 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
7870Sstevel@tonic-gate 			mutex_enter(&ire->ire_lock);
7880Sstevel@tonic-gate 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
7890Sstevel@tonic-gate 			ifrt->ifrt_v6src_addr = ire->ire_src_addr_v6;
7900Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
7910Sstevel@tonic-gate 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
7920Sstevel@tonic-gate 			ifrt->ifrt_flags = ire->ire_flags;
7930Sstevel@tonic-gate 			ifrt->ifrt_max_frag = ire->ire_max_frag;
7940Sstevel@tonic-gate 			mutex_enter(&ipif->ipif_saved_ire_lock);
7950Sstevel@tonic-gate 			save_mp->b_cont = ipif->ipif_saved_ire_mp;
7960Sstevel@tonic-gate 			ipif->ipif_saved_ire_mp = save_mp;
7970Sstevel@tonic-gate 			ipif->ipif_saved_ire_cnt++;
7980Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 	if (ire_arg != NULL) {
8020Sstevel@tonic-gate 		/*
8030Sstevel@tonic-gate 		 * Store the ire that was successfully added into where ire_arg
8040Sstevel@tonic-gate 		 * points to so that callers don't have to look it up
8050Sstevel@tonic-gate 		 * themselves (but they are responsible for ire_refrele()ing
8060Sstevel@tonic-gate 		 * the ire when they are finished with it).
8070Sstevel@tonic-gate 		 */
8080Sstevel@tonic-gate 		*ire_arg = ire;
8090Sstevel@tonic-gate 	} else {
8100Sstevel@tonic-gate 		ire_refrele(ire);		/* Held in ire_add */
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 	if (ipif_refheld)
8130Sstevel@tonic-gate 		ipif_refrele(ipif);
8140Sstevel@tonic-gate 	return (0);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate /*
8180Sstevel@tonic-gate  * ip_rt_delete_v6 is called to delete an IPv6 route.
8190Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
8200Sstevel@tonic-gate  * (for link-local destinations and gateways).
8210Sstevel@tonic-gate  */
8220Sstevel@tonic-gate /* ARGSUSED4 */
8230Sstevel@tonic-gate int
8240Sstevel@tonic-gate ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
8250Sstevel@tonic-gate     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ipif_t *ipif_arg,
8260Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate 	ire_t	*ire = NULL;
8290Sstevel@tonic-gate 	ipif_t	*ipif;
8300Sstevel@tonic-gate 	uint_t	type;
8310Sstevel@tonic-gate 	uint_t	match_flags = MATCH_IRE_TYPE;
8320Sstevel@tonic-gate 	int	err = 0;
8330Sstevel@tonic-gate 	boolean_t	ipif_refheld = B_FALSE;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
8370Sstevel@tonic-gate 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	if (flags & RTF_HOST) {
8400Sstevel@tonic-gate 		mask = &ipv6_all_ones;
8410Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
8420Sstevel@tonic-gate 	} else if (rtm_addrs & RTA_NETMASK) {
8430Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * Note that RTF_GATEWAY is never set on a delete, therefore
8480Sstevel@tonic-gate 	 * we check if the gateway address is one of our interfaces first,
8490Sstevel@tonic-gate 	 * and fall back on RTF_GATEWAY routes.
8500Sstevel@tonic-gate 	 *
8510Sstevel@tonic-gate 	 * This makes it possible to delete an original
8520Sstevel@tonic-gate 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
8530Sstevel@tonic-gate 	 *
8540Sstevel@tonic-gate 	 * As the interface index specified with the RTA_IFP sockaddr is the
8550Sstevel@tonic-gate 	 * same for all ipif's off of an ill, the matching logic below uses
8560Sstevel@tonic-gate 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
8570Sstevel@tonic-gate 	 * sharing the same prefix and interface index as the the route
8580Sstevel@tonic-gate 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
8590Sstevel@tonic-gate 	 * is specified in the request.
8600Sstevel@tonic-gate 	 *
8610Sstevel@tonic-gate 	 * On the other hand, since the gateway address will usually be
8620Sstevel@tonic-gate 	 * different for each ipif on the system, the matching logic
8630Sstevel@tonic-gate 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
8640Sstevel@tonic-gate 	 * route.  This means that interface routes for the same prefix can be
8650Sstevel@tonic-gate 	 * uniquely identified if they belong to distinct ipif's and if a
8660Sstevel@tonic-gate 	 * RTA_IFP sockaddr is not present.
8670Sstevel@tonic-gate 	 *
8680Sstevel@tonic-gate 	 * For more detail on specifying routes by gateway address and by
8690Sstevel@tonic-gate 	 * interface index, see the comments in ip_rt_add_v6().
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, &err);
8720Sstevel@tonic-gate 	if (ipif != NULL) {
8730Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
8740Sstevel@tonic-gate 		if (ipif_arg != NULL) {
8750Sstevel@tonic-gate 			ipif_refrele(ipif);
8760Sstevel@tonic-gate 			ipif_refheld = B_FALSE;
8770Sstevel@tonic-gate 			ipif = ipif_arg;
8780Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
8790Sstevel@tonic-gate 		} else {
8800Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		if (ipif->ipif_ire_type == IRE_LOOPBACK)
8840Sstevel@tonic-gate 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
8851676Sjpk 			    ipif, ALL_ZONES, NULL, match_flags);
8860Sstevel@tonic-gate 		if (ire == NULL)
8870Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, 0,
8881676Sjpk 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
8890Sstevel@tonic-gate 			    match_flags);
8900Sstevel@tonic-gate 	} else if (err == EINPROGRESS) {
8910Sstevel@tonic-gate 		return (err);
8920Sstevel@tonic-gate 	} else {
8930Sstevel@tonic-gate 		err = 0;
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 	if (ire == NULL) {
8960Sstevel@tonic-gate 		/*
8970Sstevel@tonic-gate 		 * At this point, the gateway address is not one of our own
8980Sstevel@tonic-gate 		 * addresses or a matching interface route was not found.  We
8990Sstevel@tonic-gate 		 * set the IRE type to lookup based on whether
9000Sstevel@tonic-gate 		 * this is a host route, a default route or just a prefix.
9010Sstevel@tonic-gate 		 *
9020Sstevel@tonic-gate 		 * If an ipif_arg was passed in, then the lookup is based on an
9030Sstevel@tonic-gate 		 * interface index so MATCH_IRE_ILL is added to match_flags.
9040Sstevel@tonic-gate 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
9050Sstevel@tonic-gate 		 * set as the route being looked up is not a traditional
9060Sstevel@tonic-gate 		 * interface route.
9070Sstevel@tonic-gate 		 */
9080Sstevel@tonic-gate 		match_flags &= ~MATCH_IRE_IPIF;
9090Sstevel@tonic-gate 		match_flags |= MATCH_IRE_GW;
9100Sstevel@tonic-gate 		if (ipif_arg != NULL)
9110Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
9120Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
9130Sstevel@tonic-gate 			type = IRE_HOST;
9140Sstevel@tonic-gate 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
9150Sstevel@tonic-gate 			type = IRE_DEFAULT;
9160Sstevel@tonic-gate 		else
9170Sstevel@tonic-gate 			type = IRE_PREFIX;
9180Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
9191676Sjpk 		    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
9200Sstevel@tonic-gate 		if (ire == NULL && type == IRE_HOST) {
9210Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr,
9220Sstevel@tonic-gate 			    IRE_HOST_REDIRECT, ipif_arg, NULL, ALL_ZONES, 0,
9231676Sjpk 			    NULL, match_flags);
9240Sstevel@tonic-gate 		}
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	if (ipif_refheld) {
9280Sstevel@tonic-gate 		ipif_refrele(ipif);
9290Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
9300Sstevel@tonic-gate 	}
9310Sstevel@tonic-gate 	if (ire == NULL)
9320Sstevel@tonic-gate 		return (ESRCH);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (ire->ire_flags & RTF_MULTIRT) {
9350Sstevel@tonic-gate 		/*
9360Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
9370Sstevel@tonic-gate 		 * to remove the dst address from the filtering database.
9380Sstevel@tonic-gate 		 * Packets coming from that address will no longer be
9390Sstevel@tonic-gate 		 * filtered to remove duplicates.
9400Sstevel@tonic-gate 		 */
9410Sstevel@tonic-gate 		if (ip_cgtp_filter_ops != NULL) {
9420Sstevel@tonic-gate 			err = ip_cgtp_filter_ops->cfo_del_dest_v6(
9430Sstevel@tonic-gate 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	ipif = ire->ire_ipif;
9480Sstevel@tonic-gate 	if (ipif != NULL) {
9490Sstevel@tonic-gate 		mblk_t		**mpp;
9500Sstevel@tonic-gate 		mblk_t		*mp;
9510Sstevel@tonic-gate 		ifrt_t		*ifrt;
9520Sstevel@tonic-gate 		in6_addr_t	gw_addr_v6;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		/* Remove from ipif_saved_ire_mp list if it is there */
9550Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
9560Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
9570Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
9580Sstevel@tonic-gate 		mutex_enter(&ipif->ipif_saved_ire_lock);
9590Sstevel@tonic-gate 		for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
9600Sstevel@tonic-gate 		    mpp = &(*mpp)->b_cont) {
9610Sstevel@tonic-gate 			/*
9620Sstevel@tonic-gate 			 * On a given ipif, the triple of address, gateway and
9630Sstevel@tonic-gate 			 * mask is unique for each saved IRE (in the case of
9640Sstevel@tonic-gate 			 * ordinary interface routes, the gateway address is
9650Sstevel@tonic-gate 			 * all-zeroes).
9660Sstevel@tonic-gate 			 */
9670Sstevel@tonic-gate 			mp = *mpp;
9680Sstevel@tonic-gate 			ifrt = (ifrt_t *)mp->b_rptr;
9690Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
9700Sstevel@tonic-gate 			    &ire->ire_addr_v6) &&
9710Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
9720Sstevel@tonic-gate 			    &gw_addr_v6) &&
9730Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
9740Sstevel@tonic-gate 			    &ire->ire_mask_v6)) {
9750Sstevel@tonic-gate 				*mpp = mp->b_cont;
9760Sstevel@tonic-gate 				ipif->ipif_saved_ire_cnt--;
9770Sstevel@tonic-gate 				freeb(mp);
9780Sstevel@tonic-gate 				break;
9790Sstevel@tonic-gate 			}
9800Sstevel@tonic-gate 		}
9810Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 	ire_delete(ire);
9840Sstevel@tonic-gate 	ire_refrele(ire);
9850Sstevel@tonic-gate 	return (err);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate  * Derive a token from the link layer address.
9900Sstevel@tonic-gate  */
9910Sstevel@tonic-gate boolean_t
9920Sstevel@tonic-gate ill_setdefaulttoken(ill_t *ill)
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate 	int 		i;
9950Sstevel@tonic-gate 	in6_addr_t	v6addr, v6mask;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * Though we execute on the ipsq, we need to hold the ill_lock
9990Sstevel@tonic-gate 	 * to prevent readers from seeing partially updated values
10000Sstevel@tonic-gate 	 * while we do the update.
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
10030Sstevel@tonic-gate 	if (!MEDIA_V6INTFID(ill->ill_media, ill->ill_phys_addr_length,
10040Sstevel@tonic-gate 	    ill->ill_phys_addr, &v6addr)) {
10050Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
10060Sstevel@tonic-gate 		return (B_FALSE);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_TOKEN_LEN, &v6mask);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
10120Sstevel@tonic-gate 		v6mask.s6_addr32[i] = v6mask.s6_addr32[i] ^
10130Sstevel@tonic-gate 		    (uint32_t)0xffffffff;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
10160Sstevel@tonic-gate 	ill->ill_token_length = IPV6_TOKEN_LEN;
10170Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
10180Sstevel@tonic-gate 	return (B_TRUE);
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /*
10220Sstevel@tonic-gate  * Create a link-local address from a token.
10230Sstevel@tonic-gate  */
10240Sstevel@tonic-gate static void
10250Sstevel@tonic-gate ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	int i;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
10300Sstevel@tonic-gate 		dest->s6_addr32[i] =
10310Sstevel@tonic-gate 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate /*
10360Sstevel@tonic-gate  * Set a nice default address for either automatic tunnels tsrc/96 or
10370Sstevel@tonic-gate  * 6to4 tunnels 2002:<tsrc>::1/64
10380Sstevel@tonic-gate  */
10390Sstevel@tonic-gate static void
10400Sstevel@tonic-gate ipif_set_tun_auto_addr(ipif_t *ipif, struct iftun_req *ta)
10410Sstevel@tonic-gate {
10420Sstevel@tonic-gate 	sin6_t	sin6;
10430Sstevel@tonic-gate 	sin_t	*sin;
10440Sstevel@tonic-gate 	ill_t 	*ill = ipif->ipif_ill;
10450Sstevel@tonic-gate 	tun_t *tp = (tun_t *)ill->ill_wq->q_next->q_ptr;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	if (ta->ifta_saddr.ss_family != AF_INET ||
10480Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_UP) || !ipif->ipif_isv6 ||
10490Sstevel@tonic-gate 	    (ta->ifta_flags & IFTUN_SRC) == 0)
10500Sstevel@tonic-gate 		return;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Check the tunnel type by examining q_next->q_ptr
10540Sstevel@tonic-gate 	 */
10550Sstevel@tonic-gate 	if (tp->tun_flags & TUN_AUTOMATIC) {
10560Sstevel@tonic-gate 		/* this is an automatic tunnel */
10570Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_ABITS - IP_ABITS,
10580Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
10590Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
10600Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
10610Sstevel@tonic-gate 		V4_PART_OF_V6(sin6.sin6_addr) = sin->sin_addr.s_addr;
10620Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
10630Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
10640Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
10650Sstevel@tonic-gate 	} else if (tp->tun_flags & TUN_6TO4) {
10660Sstevel@tonic-gate 		/* this is a 6to4 tunnel */
10670Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_PREFIX_LEN,
10680Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
10690Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
10700Sstevel@tonic-gate 		/* create a 6to4 address from the IPv4 tsrc */
10710Sstevel@tonic-gate 		IN6_V4ADDR_TO_6TO4(&sin->sin_addr, &sin6.sin6_addr);
10720Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
10730Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
10740Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
10750Sstevel@tonic-gate 	} else {
10760Sstevel@tonic-gate 		ip1dbg(("ipif_set_tun_auto_addr: Unknown tunnel type"));
10770Sstevel@tonic-gate 		return;
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*
10820Sstevel@tonic-gate  * Set link local for ipif_id 0 of a configured tunnel based on the
10830Sstevel@tonic-gate  * tsrc or tdst parameter
10840Sstevel@tonic-gate  * For tunnels over IPv4 use the IPv4 address prepended with 32 zeros as
10850Sstevel@tonic-gate  * the token.
10860Sstevel@tonic-gate  * For tunnels over IPv6 use the low-order 64 bits of the "inner" IPv6 address
10870Sstevel@tonic-gate  * as the token for the "outer" link.
10880Sstevel@tonic-gate  */
10890Sstevel@tonic-gate void
10900Sstevel@tonic-gate ipif_set_tun_llink(ill_t *ill, struct iftun_req *ta)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate 	ipif_t		*ipif;
10930Sstevel@tonic-gate 	sin_t		*sin;
10940Sstevel@tonic-gate 	in6_addr_t	*s6addr;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	/* The first ipif must be id zero. */
10990Sstevel@tonic-gate 	ipif = ill->ill_ipif;
11000Sstevel@tonic-gate 	ASSERT(ipif->ipif_id == 0);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/* no link local for automatic tunnels */
11030Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_POINTOPOINT)) {
11040Sstevel@tonic-gate 		ipif_set_tun_auto_addr(ipif, ta);
11050Sstevel@tonic-gate 		return;
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_DST) &&
11090Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
11100Sstevel@tonic-gate 		sin6_t	sin6;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
11130Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
11140Sstevel@tonic-gate 		if ((ta->ifta_saddr.ss_family == AF_INET)) {
11150Sstevel@tonic-gate 			sin = (sin_t *)&ta->ifta_daddr;
11160Sstevel@tonic-gate 			V4_PART_OF_V6(sin6.sin6_addr) =
11170Sstevel@tonic-gate 			    sin->sin_addr.s_addr;
11180Sstevel@tonic-gate 		} else {
11190Sstevel@tonic-gate 			s6addr =
11200Sstevel@tonic-gate 			    &((sin6_t *)&ta->ifta_daddr)->sin6_addr;
11210Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[3] = s6addr->s6_addr32[3];
11220Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[2] = s6addr->s6_addr32[2];
11230Sstevel@tonic-gate 		}
11240Sstevel@tonic-gate 		ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr,
11250Sstevel@tonic-gate 		    &sin6.sin6_addr);
11260Sstevel@tonic-gate 		ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_SRC)) {
11290Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		/* Set the token if it isn't already set */
11320Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) {
11330Sstevel@tonic-gate 			if ((ta->ifta_saddr.ss_family == AF_INET)) {
11340Sstevel@tonic-gate 				sin = (sin_t *)&ta->ifta_saddr;
11350Sstevel@tonic-gate 				V4_PART_OF_V6(ill->ill_token) =
11360Sstevel@tonic-gate 				    sin->sin_addr.s_addr;
11370Sstevel@tonic-gate 			} else {
11380Sstevel@tonic-gate 				s6addr =
11390Sstevel@tonic-gate 				    &((sin6_t *)&ta->ifta_saddr)->sin6_addr;
11400Sstevel@tonic-gate 				ill->ill_token.s6_addr32[3] =
11410Sstevel@tonic-gate 				    s6addr->s6_addr32[3];
11420Sstevel@tonic-gate 				ill->ill_token.s6_addr32[2] =
11430Sstevel@tonic-gate 				    s6addr->s6_addr32[2];
11440Sstevel@tonic-gate 			}
11450Sstevel@tonic-gate 			ill->ill_token_length = IPV6_TOKEN_LEN;
11460Sstevel@tonic-gate 		}
11470Sstevel@tonic-gate 		/*
11480Sstevel@tonic-gate 		 * Attempt to set the link local address if it isn't set.
11490Sstevel@tonic-gate 		 */
11500Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
11510Sstevel@tonic-gate 			(void) ipif_setlinklocal(ipif);
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate /*
11560Sstevel@tonic-gate  * Is it not possible to set the link local address?
11570Sstevel@tonic-gate  * The address can be set if the token is set, and the token
11580Sstevel@tonic-gate  * isn't too long.
11590Sstevel@tonic-gate  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
11600Sstevel@tonic-gate  */
11610Sstevel@tonic-gate boolean_t
11620Sstevel@tonic-gate ipif_cant_setlinklocal(ipif_t *ipif)
11630Sstevel@tonic-gate {
11640Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
11670Sstevel@tonic-gate 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
11680Sstevel@tonic-gate 		return (B_TRUE);
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	return (B_FALSE);
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate /*
11740Sstevel@tonic-gate  * Generate a link-local address from the token.
11750Sstevel@tonic-gate  * Return zero if the address was set, or non-zero if it couldn't be set.
11760Sstevel@tonic-gate  */
11770Sstevel@tonic-gate int
11780Sstevel@tonic-gate ipif_setlinklocal(ipif_t *ipif)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (ipif_cant_setlinklocal(ipif))
11850Sstevel@tonic-gate 		return (-1);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
11880Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
11890Sstevel@tonic-gate 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11900Sstevel@tonic-gate 	    ipif->ipif_v6subnet);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (ipif->ipif_flags & IPIF_NOLOCAL) {
11930Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11940Sstevel@tonic-gate 	} else {
11950Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 	return (0);
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate /*
12010Sstevel@tonic-gate  * This function sets up the multicast mappings in NDP.
12020Sstevel@tonic-gate  * Unlike ARP, there are no mapping_mps here. We delete the
12030Sstevel@tonic-gate  * mapping nces and add a new one.
12040Sstevel@tonic-gate  *
12050Sstevel@tonic-gate  * Returns non-zero on error and 0 on success.
12060Sstevel@tonic-gate  */
12070Sstevel@tonic-gate int
12080Sstevel@tonic-gate ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
12110Sstevel@tonic-gate 	in6_addr_t	v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0};
12120Sstevel@tonic-gate 	in6_addr_t	v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0};
12130Sstevel@tonic-gate 	in6_addr_t	v6_extract_mask;
12140Sstevel@tonic-gate 	uchar_t		*phys_addr, *bphys_addr, *alloc_phys;
12150Sstevel@tonic-gate 	nce_t		*mnce = NULL;
12160Sstevel@tonic-gate 	int		err = 0;
12170Sstevel@tonic-gate 	phyint_t	*phyi = ill->ill_phyint;
12180Sstevel@tonic-gate 	uint32_t	hw_extract_start;
12190Sstevel@tonic-gate 	dl_unitdata_req_t *dlur;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	if (ret_nce != NULL)
12220Sstevel@tonic-gate 		*ret_nce = NULL;
12230Sstevel@tonic-gate 	/*
12240Sstevel@tonic-gate 	 * Delete the mapping nce. Normally these should not exist
12250Sstevel@tonic-gate 	 * as a previous ipif_down -> ipif_ndp_down should have deleted
12260Sstevel@tonic-gate 	 * all the nces. But they can exist if ip_rput_dlpi_writer
12270Sstevel@tonic-gate 	 * calls this when PHYI_MULTI_BCAST is set.
12280Sstevel@tonic-gate 	 */
12292535Ssangeeta 	mnce = ndp_lookup_v6(ill, &v6_mcast_addr, B_FALSE);
12300Sstevel@tonic-gate 	if (mnce != NULL) {
12310Sstevel@tonic-gate 		ndp_delete(mnce);
12320Sstevel@tonic-gate 		NCE_REFRELE(mnce);
12330Sstevel@tonic-gate 		mnce = NULL;
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * Get media specific v6 mapping information. Note that
12380Sstevel@tonic-gate 	 * nd_lla_len can be 0 for tunnels.
12390Sstevel@tonic-gate 	 */
12400Sstevel@tonic-gate 	alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
12410Sstevel@tonic-gate 	if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0))
12420Sstevel@tonic-gate 		return (ENOMEM);
12430Sstevel@tonic-gate 	/*
12440Sstevel@tonic-gate 	 * Determine the broadcast address.
12450Sstevel@tonic-gate 	 */
12460Sstevel@tonic-gate 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12470Sstevel@tonic-gate 	if (ill->ill_sap_length < 0)
12480Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12490Sstevel@tonic-gate 	else
12500Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur +
12510Sstevel@tonic-gate 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * Check PHYI_MULTI_BCAST and possible length of physical
12550Sstevel@tonic-gate 	 * address to determine if we use the mapping or the
12560Sstevel@tonic-gate 	 * broadcast address.
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12590Sstevel@tonic-gate 	    (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len,
12600Sstevel@tonic-gate 	    bphys_addr, alloc_phys, &hw_extract_start,
12610Sstevel@tonic-gate 	    &v6_extract_mask))) {
12620Sstevel@tonic-gate 		if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) {
12630Sstevel@tonic-gate 			kmem_free(alloc_phys, ill->ill_nd_lla_len);
12640Sstevel@tonic-gate 			return (E2BIG);
12650Sstevel@tonic-gate 		}
12660Sstevel@tonic-gate 		/* Use the link-layer broadcast address for MULTI_BCAST */
12670Sstevel@tonic-gate 		phys_addr = bphys_addr;
12680Sstevel@tonic-gate 		bzero(&v6_extract_mask, sizeof (v6_extract_mask));
12690Sstevel@tonic-gate 		hw_extract_start = ill->ill_nd_lla_len;
12700Sstevel@tonic-gate 	} else {
12710Sstevel@tonic-gate 		phys_addr = alloc_phys;
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_BROADCAST) ||
12740Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST) ||
12750Sstevel@tonic-gate 	    (phyi->phyint_flags & PHYI_MULTI_BCAST)) {
12762535Ssangeeta 		mutex_enter(&ndp6.ndp_g_lock);
12770Sstevel@tonic-gate 		err = ndp_add(ill,
12780Sstevel@tonic-gate 		    phys_addr,
12790Sstevel@tonic-gate 		    &v6_mcast_addr,	/* v6 address */
12800Sstevel@tonic-gate 		    &v6_mcast_mask,	/* v6 mask */
12810Sstevel@tonic-gate 		    &v6_extract_mask,
12820Sstevel@tonic-gate 		    hw_extract_start,
12830Sstevel@tonic-gate 		    NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD,
12840Sstevel@tonic-gate 		    ND_REACHABLE,
12852535Ssangeeta 		    &mnce,
12862535Ssangeeta 		    NULL,
12872535Ssangeeta 		    NULL);
12882535Ssangeeta 		mutex_exit(&ndp6.ndp_g_lock);
12890Sstevel@tonic-gate 		if (err == 0) {
12900Sstevel@tonic-gate 			if (ret_nce != NULL) {
12910Sstevel@tonic-gate 				*ret_nce = mnce;
12920Sstevel@tonic-gate 			} else {
12930Sstevel@tonic-gate 				NCE_REFRELE(mnce);
12940Sstevel@tonic-gate 			}
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 	}
12970Sstevel@tonic-gate 	kmem_free(alloc_phys, ill->ill_nd_lla_len);
12980Sstevel@tonic-gate 	return (err);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate /*
13020Sstevel@tonic-gate  * Get the resolver set up for a new interface address.  (Always called
13030Sstevel@tonic-gate  * as writer.)
13040Sstevel@tonic-gate  */
13050Sstevel@tonic-gate int
13060Sstevel@tonic-gate ipif_ndp_up(ipif_t *ipif, const in6_addr_t *addr, boolean_t macaddr_change)
13070Sstevel@tonic-gate {
13080Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
13090Sstevel@tonic-gate 	int		err = 0;
13100Sstevel@tonic-gate 	nce_t		*nce = NULL;
13110Sstevel@tonic-gate 	nce_t		*mnce = NULL;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	ip1dbg(("ipif_ndp_up(%s:%u)\n",
13140Sstevel@tonic-gate 		ipif->ipif_ill->ill_name, ipif->ipif_id));
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
13170Sstevel@tonic-gate 	 * ND not supported on XRESOLV interfaces. If ND support (multicast)
13180Sstevel@tonic-gate 	 * added later, take out this check.
13190Sstevel@tonic-gate 	 */
1320*2546Scarlsonj 	if ((ill->ill_flags & ILLF_XRESOLV) ||
1321*2546Scarlsonj 	    IN6_IS_ADDR_UNSPECIFIED(addr) ||
1322*2546Scarlsonj 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
1323*2546Scarlsonj 		ipif->ipif_addr_ready = 1;
13240Sstevel@tonic-gate 		return (0);
1325*2546Scarlsonj 	}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/*
13280Sstevel@tonic-gate 	 * Need to setup multicast mapping only when the first
13290Sstevel@tonic-gate 	 * interface is coming UP.
13300Sstevel@tonic-gate 	 */
13310Sstevel@tonic-gate 	if (ill->ill_ipif_up_count == 0 &&
13320Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST)) {
13330Sstevel@tonic-gate 		/*
13340Sstevel@tonic-gate 		 * We set the multicast before setting up the mapping for
13350Sstevel@tonic-gate 		 * local address because ipif_ndp_setup_multicast does
13360Sstevel@tonic-gate 		 * ndp_walk to delete nces which will delete the mapping
13370Sstevel@tonic-gate 		 * for local address also if we added the mapping for
13380Sstevel@tonic-gate 		 * local address first.
13390Sstevel@tonic-gate 		 */
13400Sstevel@tonic-gate 		err = ipif_ndp_setup_multicast(ipif, &mnce);
13410Sstevel@tonic-gate 		if (err != 0)
13420Sstevel@tonic-gate 			return (err);
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
13460Sstevel@tonic-gate 		uint16_t	flags;
13470Sstevel@tonic-gate 		uchar_t	*hw_addr = NULL;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		/* Permanent entries don't need NUD */
13500Sstevel@tonic-gate 		flags = NCE_F_PERMANENT;
13510Sstevel@tonic-gate 		flags |= NCE_F_NONUD;
13520Sstevel@tonic-gate 		if (ill->ill_flags & ILLF_ROUTER)
13530Sstevel@tonic-gate 			flags |= NCE_F_ISROUTER;
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_ANYCAST)
13560Sstevel@tonic-gate 			flags |= NCE_F_ANYCAST;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 		if (ill->ill_net_type == IRE_IF_RESOLVER) {
13590Sstevel@tonic-gate 			hw_addr = ill->ill_nd_lla;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 			if (ill->ill_move_in_progress || macaddr_change) {
13620Sstevel@tonic-gate 				/*
13630Sstevel@tonic-gate 				 * Addresses are failing over to this ill.
13640Sstevel@tonic-gate 				 * Don't wait for NUD to see this change.
13650Sstevel@tonic-gate 				 * Publish our new link-layer address.
13660Sstevel@tonic-gate 				 */
13670Sstevel@tonic-gate 				flags |= NCE_F_UNSOL_ADV;
13680Sstevel@tonic-gate 			}
13690Sstevel@tonic-gate 		}
13700Sstevel@tonic-gate 		err = ndp_lookup_then_add(ill,
13710Sstevel@tonic-gate 		    hw_addr,
13720Sstevel@tonic-gate 		    addr,
13730Sstevel@tonic-gate 		    &ipv6_all_ones,
13740Sstevel@tonic-gate 		    &ipv6_all_zeros,
13750Sstevel@tonic-gate 		    0,
13760Sstevel@tonic-gate 		    flags,
1377*2546Scarlsonj 		    ND_PROBE,	/* Causes Duplicate Address Detection to run */
13782535Ssangeeta 		    &nce,
13792535Ssangeeta 		    NULL,
13802535Ssangeeta 		    NULL);
13810Sstevel@tonic-gate 		switch (err) {
13820Sstevel@tonic-gate 		case 0:
13830Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
13840Sstevel@tonic-gate 			    ill->ill_name));
1385*2546Scarlsonj 			ipif->ipif_addr_ready = 1;
1386*2546Scarlsonj 			break;
1387*2546Scarlsonj 		case EINPROGRESS:
1388*2546Scarlsonj 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
1389*2546Scarlsonj 			    ill->ill_name));
13900Sstevel@tonic-gate 			break;
13910Sstevel@tonic-gate 		case EEXIST:
13920Sstevel@tonic-gate 			NCE_REFRELE(nce);
13930Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
13940Sstevel@tonic-gate 			    ill->ill_name));
13950Sstevel@tonic-gate 			if (mnce != NULL) {
13960Sstevel@tonic-gate 				ndp_delete(mnce);
13970Sstevel@tonic-gate 				NCE_REFRELE(mnce);
13980Sstevel@tonic-gate 			}
13990Sstevel@tonic-gate 			return (err);
14000Sstevel@tonic-gate 		default:
14010Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE creation failed %s\n",
14020Sstevel@tonic-gate 			    ill->ill_name));
14030Sstevel@tonic-gate 			if (mnce != NULL) {
14040Sstevel@tonic-gate 				ndp_delete(mnce);
14050Sstevel@tonic-gate 				NCE_REFRELE(mnce);
14060Sstevel@tonic-gate 			}
14070Sstevel@tonic-gate 			return (err);
14080Sstevel@tonic-gate 		}
1409*2546Scarlsonj 	} else {
1410*2546Scarlsonj 		/* No local NCE for this entry */
1411*2546Scarlsonj 		ipif->ipif_addr_ready = 1;
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 	if (nce != NULL)
14140Sstevel@tonic-gate 		NCE_REFRELE(nce);
14150Sstevel@tonic-gate 	if (mnce != NULL)
14160Sstevel@tonic-gate 		NCE_REFRELE(mnce);
14170Sstevel@tonic-gate 	return (0);
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate /* Remove all cache entries for this logical interface */
14210Sstevel@tonic-gate void
14220Sstevel@tonic-gate ipif_ndp_down(ipif_t *ipif)
14230Sstevel@tonic-gate {
14240Sstevel@tonic-gate 	nce_t	*nce;
14250Sstevel@tonic-gate 
14262535Ssangeeta 	if (ipif->ipif_isv6) {
14272535Ssangeeta 		nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr,
14282535Ssangeeta 		    B_FALSE);
14292535Ssangeeta 		if (nce != NULL) {
14302535Ssangeeta 			ndp_delete(nce);
14312535Ssangeeta 			NCE_REFRELE(nce);
14322535Ssangeeta 		}
14330Sstevel@tonic-gate 	}
14340Sstevel@tonic-gate 	/*
14350Sstevel@tonic-gate 	 * Remove mapping and all other nces dependent on this ill
14360Sstevel@tonic-gate 	 * when the last ipif is going away.
14370Sstevel@tonic-gate 	 */
14380Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
14390Sstevel@tonic-gate 		ndp_walk(ipif->ipif_ill, (pfi_t)ndp_delete_per_ill,
14400Sstevel@tonic-gate 		    (uchar_t *)ipif->ipif_ill);
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate /*
14450Sstevel@tonic-gate  * Used when an interface comes up to recreate any extra routes on this
14460Sstevel@tonic-gate  * interface.
14470Sstevel@tonic-gate  */
14480Sstevel@tonic-gate static ire_t **
14490Sstevel@tonic-gate ipif_recover_ire_v6(ipif_t *ipif)
14500Sstevel@tonic-gate {
14510Sstevel@tonic-gate 	mblk_t	*mp;
14520Sstevel@tonic-gate 	ire_t   **ipif_saved_irep;
14530Sstevel@tonic-gate 	ire_t   **irep;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name,
14560Sstevel@tonic-gate 	    ipif->ipif_id));
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_saved_ire_lock);
14610Sstevel@tonic-gate 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
14620Sstevel@tonic-gate 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
14630Sstevel@tonic-gate 	if (ipif_saved_irep == NULL) {
14640Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
14650Sstevel@tonic-gate 		return (NULL);
14660Sstevel@tonic-gate 	}
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	irep = ipif_saved_irep;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14710Sstevel@tonic-gate 		ire_t		*ire;
14720Sstevel@tonic-gate 		queue_t		*rfq;
14730Sstevel@tonic-gate 		queue_t		*stq;
14740Sstevel@tonic-gate 		ifrt_t		*ifrt;
14750Sstevel@tonic-gate 		in6_addr_t	*src_addr;
14760Sstevel@tonic-gate 		in6_addr_t	*gateway_addr;
14770Sstevel@tonic-gate 		mblk_t		*resolver_mp;
14780Sstevel@tonic-gate 		char		buf[INET6_ADDRSTRLEN];
14790Sstevel@tonic-gate 		ushort_t	type;
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 		/*
14820Sstevel@tonic-gate 		 * When the ire was initially created and then added in
14830Sstevel@tonic-gate 		 * ip_rt_add_v6(), it was created either using
14840Sstevel@tonic-gate 		 * ipif->ipif_net_type in the case of a traditional interface
14850Sstevel@tonic-gate 		 * route, or as one of the IRE_OFFSUBNET types (with the
14860Sstevel@tonic-gate 		 * exception of IRE_HOST_REDIRECT which is created by
14870Sstevel@tonic-gate 		 * icmp_redirect_v6() and which we don't need to save or
14880Sstevel@tonic-gate 		 * recover).  In the case where ipif->ipif_net_type was
14890Sstevel@tonic-gate 		 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to
14900Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy
14910Sstevel@tonic-gate 		 * software like GateD and Sun Cluster which creates routes
14920Sstevel@tonic-gate 		 * using the the loopback interface's address as a gateway.
14930Sstevel@tonic-gate 		 *
14940Sstevel@tonic-gate 		 * As ifrt->ifrt_type reflects the already updated ire_type and
14950Sstevel@tonic-gate 		 * since ire_create_v6() expects that IRE_IF_NORESOLVER will
14960Sstevel@tonic-gate 		 * have a valid ire_dlureq_mp field (which doesn't make sense
14970Sstevel@tonic-gate 		 * for a IRE_LOOPBACK), ire_create_v6() will be called in the
14980Sstevel@tonic-gate 		 * same way here as in ip_rt_add_v6(), namely using
14990Sstevel@tonic-gate 		 * ipif->ipif_net_type when the route looks like a traditional
15000Sstevel@tonic-gate 		 * interface route (where ifrt->ifrt_type & IRE_INTERFACE is
15010Sstevel@tonic-gate 		 * true) and otherwise using the saved ifrt->ifrt_type.  This
15020Sstevel@tonic-gate 		 * means that in the case where ipif->ipif_net_type is
15030Sstevel@tonic-gate 		 * IRE_LOOPBACK, the ire created by ire_create_v6() will be an
15040Sstevel@tonic-gate 		 * IRE_LOOPBACK, it will then be turned into an
15050Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER and then added by ire_add_v6().
15060Sstevel@tonic-gate 		 */
15070Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
15080Sstevel@tonic-gate 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15090Sstevel@tonic-gate 			rfq = NULL;
15100Sstevel@tonic-gate 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15110Sstevel@tonic-gate 			    ? ipif->ipif_rq : ipif->ipif_wq;
15120Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15130Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr
15140Sstevel@tonic-gate 			    : &ipif->ipif_v6src_addr;
15150Sstevel@tonic-gate 			gateway_addr = NULL;
15160Sstevel@tonic-gate 			resolver_mp = ipif->ipif_resolver_mp;
15170Sstevel@tonic-gate 			type = ipif->ipif_net_type;
15180Sstevel@tonic-gate 		} else {
15190Sstevel@tonic-gate 			rfq = NULL;
15200Sstevel@tonic-gate 			stq = NULL;
15210Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15220Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr : NULL;
15230Sstevel@tonic-gate 			gateway_addr = &ifrt->ifrt_v6gateway_addr;
15240Sstevel@tonic-gate 			resolver_mp = NULL;
15250Sstevel@tonic-gate 			type = ifrt->ifrt_type;
15260Sstevel@tonic-gate 		}
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 		/*
15290Sstevel@tonic-gate 		 * Create a copy of the IRE with the saved address and netmask.
15300Sstevel@tonic-gate 		 */
15310Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n",
15320Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
15330Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)),
15340Sstevel@tonic-gate 		    ip_mask_to_plen_v6(&ifrt->ifrt_v6mask)));
15350Sstevel@tonic-gate 		ire = ire_create_v6(
15360Sstevel@tonic-gate 		    &ifrt->ifrt_v6addr,
15370Sstevel@tonic-gate 		    &ifrt->ifrt_v6mask,
15380Sstevel@tonic-gate 		    src_addr,
15390Sstevel@tonic-gate 		    gateway_addr,
15400Sstevel@tonic-gate 		    &ifrt->ifrt_max_frag,
15410Sstevel@tonic-gate 		    NULL,
15420Sstevel@tonic-gate 		    rfq,
15430Sstevel@tonic-gate 		    stq,
15440Sstevel@tonic-gate 		    type,
15450Sstevel@tonic-gate 		    resolver_mp,
15460Sstevel@tonic-gate 		    ipif,
15470Sstevel@tonic-gate 		    NULL,
15480Sstevel@tonic-gate 		    0,
15490Sstevel@tonic-gate 		    0,
15500Sstevel@tonic-gate 		    ifrt->ifrt_flags,
15511676Sjpk 		    &ifrt->ifrt_iulp_info,
15521676Sjpk 		    NULL,
15531676Sjpk 		    NULL);
15540Sstevel@tonic-gate 		if (ire == NULL) {
15550Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
15560Sstevel@tonic-gate 			kmem_free(ipif_saved_irep,
15570Sstevel@tonic-gate 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
15580Sstevel@tonic-gate 			return (NULL);
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		/*
15620Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
15630Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
15640Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
15650Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
15660Sstevel@tonic-gate 		 * when generating aggregate routes.)
15670Sstevel@tonic-gate 		 *
15680Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
15690Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
15700Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
15710Sstevel@tonic-gate 		 */
15720Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
15730Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
15740Sstevel@tonic-gate 		/*
15750Sstevel@tonic-gate 		 * ire held by ire_add, will be refreled' in ipif_up_done
15760Sstevel@tonic-gate 		 * towards the end
15770Sstevel@tonic-gate 		 */
15782535Ssangeeta 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15790Sstevel@tonic-gate 		*irep = ire;
15800Sstevel@tonic-gate 		irep++;
15810Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire));
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_saved_ire_lock);
15840Sstevel@tonic-gate 	return (ipif_saved_irep);
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate /*
15880Sstevel@tonic-gate  * Return the scope of the given IPv6 address.  If the address is an
15890Sstevel@tonic-gate  * IPv4 mapped IPv6 address, return the scope of the corresponding
15900Sstevel@tonic-gate  * IPv4 address.
15910Sstevel@tonic-gate  */
15920Sstevel@tonic-gate in6addr_scope_t
15930Sstevel@tonic-gate ip_addr_scope_v6(const in6_addr_t *addr)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
15980Sstevel@tonic-gate 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
15990Sstevel@tonic-gate 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
16000Sstevel@tonic-gate 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
16010Sstevel@tonic-gate 			return (IP6_SCOPE_LINKLOCAL);
16020Sstevel@tonic-gate 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
16030Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
16040Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
16050Sstevel@tonic-gate 			return (IP6_SCOPE_SITELOCAL);
16060Sstevel@tonic-gate 		return (IP6_SCOPE_GLOBAL);
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if (IN6_IS_ADDR_MULTICAST(addr))
16100Sstevel@tonic-gate 		return (IN6_ADDR_MC_SCOPE(addr));
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/* link-local and loopback addresses are of link-local scope */
16130Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
16140Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
16150Sstevel@tonic-gate 		return (IP6_SCOPE_LINKLOCAL);
16160Sstevel@tonic-gate 	if (IN6_IS_ADDR_SITELOCAL(addr))
16170Sstevel@tonic-gate 		return (IP6_SCOPE_SITELOCAL);
16180Sstevel@tonic-gate 	return (IP6_SCOPE_GLOBAL);
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*
16230Sstevel@tonic-gate  * Calculates the xor of a1 and a2, and stores the result in res.
16240Sstevel@tonic-gate  */
16250Sstevel@tonic-gate static void
16260Sstevel@tonic-gate ip_addr_xor_v6(const in6_addr_t *a1, const in6_addr_t *a2, in6_addr_t *res)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate 	int i;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
16310Sstevel@tonic-gate 		res->s6_addr32[i] = a1->s6_addr32[i] ^ a2->s6_addr32[i];
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate #define	IPIF_VALID_IPV6_SOURCE(ipif) \
16350Sstevel@tonic-gate 	(((ipif)->ipif_flags & IPIF_UP) && \
1636*2546Scarlsonj 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
1637*2546Scarlsonj 	(ipif)->ipif_addr_ready)
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate /* source address candidate */
16400Sstevel@tonic-gate typedef struct candidate {
16410Sstevel@tonic-gate 	ipif_t		*cand_ipif;
16420Sstevel@tonic-gate 	/* The properties of this candidate */
16430Sstevel@tonic-gate 	boolean_t	cand_isdst;
16440Sstevel@tonic-gate 	boolean_t	cand_isdst_set;
16450Sstevel@tonic-gate 	in6addr_scope_t	cand_scope;
16460Sstevel@tonic-gate 	boolean_t	cand_scope_set;
16470Sstevel@tonic-gate 	boolean_t	cand_isdeprecated;
16480Sstevel@tonic-gate 	boolean_t	cand_isdeprecated_set;
16490Sstevel@tonic-gate 	boolean_t	cand_ispreferred;
16500Sstevel@tonic-gate 	boolean_t	cand_ispreferred_set;
16510Sstevel@tonic-gate 	boolean_t	cand_matchedinterface;
16520Sstevel@tonic-gate 	boolean_t	cand_matchedinterface_set;
16530Sstevel@tonic-gate 	boolean_t	cand_matchedlabel;
16540Sstevel@tonic-gate 	boolean_t	cand_matchedlabel_set;
16550Sstevel@tonic-gate 	boolean_t	cand_istmp;
16560Sstevel@tonic-gate 	boolean_t	cand_istmp_set;
16570Sstevel@tonic-gate 	in6_addr_t	cand_xor;
16580Sstevel@tonic-gate 	boolean_t	cand_xor_set;
16590Sstevel@tonic-gate } cand_t;
16600Sstevel@tonic-gate #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
16610Sstevel@tonic-gate #define	cand_flags	cand_ipif->ipif_flags
16620Sstevel@tonic-gate #define	cand_ill	cand_ipif->ipif_ill
16631676Sjpk #define	cand_zoneid	cand_ipif->ipif_zoneid
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate /* information about the destination for source address selection */
16660Sstevel@tonic-gate typedef struct dstinfo {
16670Sstevel@tonic-gate 	const in6_addr_t	*dst_addr;
16680Sstevel@tonic-gate 	ill_t			*dst_ill;
16692202Srk129064 	uint_t			dst_restrict_ill;
16700Sstevel@tonic-gate 	boolean_t		dst_prefer_src_tmp;
16710Sstevel@tonic-gate 	in6addr_scope_t		dst_scope;
16720Sstevel@tonic-gate 	char			*dst_label;
16730Sstevel@tonic-gate } dstinfo_t;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate  * The following functions are rules used to select a source address in
16770Sstevel@tonic-gate  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
16780Sstevel@tonic-gate  * against the best candidate (bc).  Each rule has three possible outcomes;
16790Sstevel@tonic-gate  * the candidate is preferred over the best candidate (CAND_PREFER), the
16800Sstevel@tonic-gate  * candidate is not preferred over the best candidate (CAND_AVOID), or the
16810Sstevel@tonic-gate  * candidate is of equal value as the best candidate (CAND_TIE).
16820Sstevel@tonic-gate  *
16830Sstevel@tonic-gate  * These rules are part of a greater "Default Address Selection for IPv6"
16840Sstevel@tonic-gate  * sheme, which is standards based work coming out of the IETF ipv6 working
16850Sstevel@tonic-gate  * group.  The IETF document defines both IPv6 source address selection and
16860Sstevel@tonic-gate  * destination address ordering.  The rules defined here implement the IPv6
16870Sstevel@tonic-gate  * source address selection.  Destination address ordering is done by
16880Sstevel@tonic-gate  * libnsl, and uses a similar set of rules to implement the sorting.
16890Sstevel@tonic-gate  */
16900Sstevel@tonic-gate typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
16910Sstevel@tonic-gate typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *);
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate /* Prefer an address if it is equal to the destination address. */
16940Sstevel@tonic-gate static rule_res_t
16950Sstevel@tonic-gate rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	if (!bc->cand_isdst_set) {
16980Sstevel@tonic-gate 		bc->cand_isdst =
16990Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
17000Sstevel@tonic-gate 		bc->cand_isdst_set = B_TRUE;
17010Sstevel@tonic-gate 	}
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	cc->cand_isdst =
17040Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
17050Sstevel@tonic-gate 	cc->cand_isdst_set = B_TRUE;
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	if (cc->cand_isdst == bc->cand_isdst)
17080Sstevel@tonic-gate 		return (CAND_TIE);
17090Sstevel@tonic-gate 	else if (cc->cand_isdst)
17100Sstevel@tonic-gate 		return (CAND_PREFER);
17110Sstevel@tonic-gate 	else
17120Sstevel@tonic-gate 		return (CAND_AVOID);
17130Sstevel@tonic-gate }
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate /*
17160Sstevel@tonic-gate  * Prefer addresses that are of closest scope to the destination.  Always
17170Sstevel@tonic-gate  * prefer addresses that are of greater scope than the destination over
17180Sstevel@tonic-gate  * those that are of lesser scope than the destination.
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate static rule_res_t
17210Sstevel@tonic-gate rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	if (!bc->cand_scope_set) {
17240Sstevel@tonic-gate 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
17250Sstevel@tonic-gate 		bc->cand_scope_set = B_TRUE;
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
17290Sstevel@tonic-gate 	cc->cand_scope_set = B_TRUE;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	if (cc->cand_scope < bc->cand_scope) {
17320Sstevel@tonic-gate 		if (cc->cand_scope < dstinfo->dst_scope)
17330Sstevel@tonic-gate 			return (CAND_AVOID);
17340Sstevel@tonic-gate 		else
17350Sstevel@tonic-gate 			return (CAND_PREFER);
17360Sstevel@tonic-gate 	} else if (bc->cand_scope < cc->cand_scope) {
17370Sstevel@tonic-gate 		if (bc->cand_scope < dstinfo->dst_scope)
17380Sstevel@tonic-gate 			return (CAND_PREFER);
17390Sstevel@tonic-gate 		else
17400Sstevel@tonic-gate 			return (CAND_AVOID);
17410Sstevel@tonic-gate 	} else {
17420Sstevel@tonic-gate 		return (CAND_TIE);
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate /*
17470Sstevel@tonic-gate  * Prefer non-deprecated source addresses.
17480Sstevel@tonic-gate  */
17490Sstevel@tonic-gate /* ARGSUSED2 */
17500Sstevel@tonic-gate static rule_res_t
17510Sstevel@tonic-gate rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	if (!bc->cand_isdeprecated_set) {
17540Sstevel@tonic-gate 		bc->cand_isdeprecated =
17550Sstevel@tonic-gate 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
17560Sstevel@tonic-gate 		bc->cand_isdeprecated_set = B_TRUE;
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
17600Sstevel@tonic-gate 	cc->cand_isdeprecated_set = B_TRUE;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
17630Sstevel@tonic-gate 		return (CAND_TIE);
17640Sstevel@tonic-gate 	else if (cc->cand_isdeprecated)
17650Sstevel@tonic-gate 		return (CAND_AVOID);
17660Sstevel@tonic-gate 	else
17670Sstevel@tonic-gate 		return (CAND_PREFER);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
17720Sstevel@tonic-gate  * rule must be before rule_interface because the flag could be set on any
17730Sstevel@tonic-gate  * interface, not just the interface being used for outgoing packets (for
17740Sstevel@tonic-gate  * example, the IFF_PREFERRED could be set on an address assigned to the
17750Sstevel@tonic-gate  * loopback interface).
17760Sstevel@tonic-gate  */
17770Sstevel@tonic-gate /* ARGSUSED2 */
17780Sstevel@tonic-gate static rule_res_t
17790Sstevel@tonic-gate rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
17800Sstevel@tonic-gate {
17810Sstevel@tonic-gate 	if (!bc->cand_ispreferred_set) {
17820Sstevel@tonic-gate 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
17830Sstevel@tonic-gate 		bc->cand_ispreferred_set = B_TRUE;
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
17870Sstevel@tonic-gate 	cc->cand_ispreferred_set = B_TRUE;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	if (bc->cand_ispreferred == cc->cand_ispreferred)
17900Sstevel@tonic-gate 		return (CAND_TIE);
17910Sstevel@tonic-gate 	else if (cc->cand_ispreferred)
17920Sstevel@tonic-gate 		return (CAND_PREFER);
17930Sstevel@tonic-gate 	else
17940Sstevel@tonic-gate 		return (CAND_AVOID);
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate /*
17980Sstevel@tonic-gate  * Prefer source addresses that are assigned to the outgoing interface, or
17990Sstevel@tonic-gate  * to an interface that is in the same IPMP group as the outgoing
18000Sstevel@tonic-gate  * interface.
18010Sstevel@tonic-gate  */
18020Sstevel@tonic-gate static rule_res_t
18030Sstevel@tonic-gate rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
18040Sstevel@tonic-gate {
18050Sstevel@tonic-gate 	ill_t *dstill = dstinfo->dst_ill;
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	/*
18080Sstevel@tonic-gate 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
18090Sstevel@tonic-gate 	 * since we know all candidates will be on the same link.
18100Sstevel@tonic-gate 	 */
18110Sstevel@tonic-gate 	if (dstinfo->dst_restrict_ill)
18120Sstevel@tonic-gate 		return (CAND_TIE);
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	if (!bc->cand_matchedinterface_set) {
18150Sstevel@tonic-gate 		bc->cand_matchedinterface = (bc->cand_ill == dstill ||
18160Sstevel@tonic-gate 		    (dstill->ill_group != NULL &&
18170Sstevel@tonic-gate 		    dstill->ill_group == bc->cand_ill->ill_group));
18180Sstevel@tonic-gate 		bc->cand_matchedinterface_set = B_TRUE;
18190Sstevel@tonic-gate 	}
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	cc->cand_matchedinterface = (cc->cand_ill == dstill ||
18220Sstevel@tonic-gate 	    (dstill->ill_group != NULL &&
18230Sstevel@tonic-gate 		dstill->ill_group == cc->cand_ill->ill_group));
18240Sstevel@tonic-gate 	cc->cand_matchedinterface_set = B_TRUE;
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
18270Sstevel@tonic-gate 		return (CAND_TIE);
18280Sstevel@tonic-gate 	else if (cc->cand_matchedinterface)
18290Sstevel@tonic-gate 		return (CAND_PREFER);
18300Sstevel@tonic-gate 	else
18310Sstevel@tonic-gate 		return (CAND_AVOID);
18320Sstevel@tonic-gate }
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate /*
18350Sstevel@tonic-gate  * Prefer source addresses whose label matches the destination's label.
18360Sstevel@tonic-gate  */
18370Sstevel@tonic-gate static rule_res_t
18380Sstevel@tonic-gate rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
18390Sstevel@tonic-gate {
18400Sstevel@tonic-gate 	char *label;
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	if (!bc->cand_matchedlabel_set) {
18430Sstevel@tonic-gate 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL);
18440Sstevel@tonic-gate 		bc->cand_matchedlabel =
18450Sstevel@tonic-gate 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
18460Sstevel@tonic-gate 		bc->cand_matchedlabel_set = B_TRUE;
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL);
18500Sstevel@tonic-gate 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
18510Sstevel@tonic-gate 	cc->cand_matchedlabel_set = B_TRUE;
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
18540Sstevel@tonic-gate 		return (CAND_TIE);
18550Sstevel@tonic-gate 	else if (cc->cand_matchedlabel)
18560Sstevel@tonic-gate 		return (CAND_PREFER);
18570Sstevel@tonic-gate 	else
18580Sstevel@tonic-gate 		return (CAND_AVOID);
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate /*
18620Sstevel@tonic-gate  * Prefer public addresses over temporary ones.  An application can reverse
18630Sstevel@tonic-gate  * the logic of this rule and prefer temporary addresses by using the
18640Sstevel@tonic-gate  * IPV6_SRC_PREFERENCES socket option.
18650Sstevel@tonic-gate  */
18660Sstevel@tonic-gate static rule_res_t
18670Sstevel@tonic-gate rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
18680Sstevel@tonic-gate {
18690Sstevel@tonic-gate 	if (!bc->cand_istmp_set) {
18700Sstevel@tonic-gate 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
18710Sstevel@tonic-gate 		bc->cand_istmp_set = B_TRUE;
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
18750Sstevel@tonic-gate 	cc->cand_istmp_set = B_TRUE;
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	if (bc->cand_istmp == cc->cand_istmp)
18780Sstevel@tonic-gate 		return (CAND_TIE);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
18810Sstevel@tonic-gate 		return (CAND_PREFER);
18820Sstevel@tonic-gate 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
18830Sstevel@tonic-gate 		return (CAND_PREFER);
18840Sstevel@tonic-gate 	else
18850Sstevel@tonic-gate 		return (CAND_AVOID);
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate /*
18890Sstevel@tonic-gate  * Prefer source addresses with longer matching prefix with the
18900Sstevel@tonic-gate  * destination.  Since this is the last rule, it must not produce a tie.
18910Sstevel@tonic-gate  * We do the longest matching prefix calculation and the tie break in one
18920Sstevel@tonic-gate  * calculation by doing an xor of both addresses with the destination, and
18930Sstevel@tonic-gate  * pick the address with the smallest xor value.  That way, we're both
18940Sstevel@tonic-gate  * picking the address with the longest matching prefix, and breaking the
18950Sstevel@tonic-gate  * tie if they happen to have both have equal mathing prefixes.
18960Sstevel@tonic-gate  */
18970Sstevel@tonic-gate static rule_res_t
18980Sstevel@tonic-gate rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
18990Sstevel@tonic-gate {
19000Sstevel@tonic-gate 	int i;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	if (!bc->cand_xor_set) {
19030Sstevel@tonic-gate 		ip_addr_xor_v6(&bc->cand_srcaddr,
19040Sstevel@tonic-gate 		    dstinfo->dst_addr, &bc->cand_xor);
19050Sstevel@tonic-gate 		bc->cand_xor_set = B_TRUE;
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	ip_addr_xor_v6(&cc->cand_srcaddr, dstinfo->dst_addr, &cc->cand_xor);
19090Sstevel@tonic-gate 	cc->cand_xor_set = B_TRUE;
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
19120Sstevel@tonic-gate 		if (bc->cand_xor.s6_addr32[i] != cc->cand_xor.s6_addr32[i])
19130Sstevel@tonic-gate 			break;
19140Sstevel@tonic-gate 	}
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	if (cc->cand_xor.s6_addr32[i] < bc->cand_xor.s6_addr32[i])
19170Sstevel@tonic-gate 		return (CAND_PREFER);
19180Sstevel@tonic-gate 	else
19190Sstevel@tonic-gate 		return (CAND_AVOID);
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate /*
19231676Sjpk  * Prefer to use zone-specific addresses when possible instead of all-zones
19241676Sjpk  * addresses.
19251676Sjpk  */
19261676Sjpk /* ARGSUSED2 */
19271676Sjpk static rule_res_t
19281676Sjpk rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
19291676Sjpk {
19301676Sjpk 	if ((bc->cand_zoneid == ALL_ZONES) ==
19311676Sjpk 	    (cc->cand_zoneid == ALL_ZONES))
19321676Sjpk 		return (CAND_TIE);
19331676Sjpk 	else if (cc->cand_zoneid == ALL_ZONES)
19341676Sjpk 		return (CAND_AVOID);
19351676Sjpk 	else
19361676Sjpk 		return (CAND_PREFER);
19371676Sjpk }
19381676Sjpk 
19391676Sjpk /*
19400Sstevel@tonic-gate  * Determine the best source address given a destination address and a
19410Sstevel@tonic-gate  * destination ill.  If no suitable source address is found, it returns
19420Sstevel@tonic-gate  * NULL. If there is a usable address pointed to by the usesrc
19430Sstevel@tonic-gate  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
19440Sstevel@tonic-gate  * fine grained (i.e per interface)
19450Sstevel@tonic-gate  *
19460Sstevel@tonic-gate  * This implementation is based on the "Default Address Selection for IPv6"
19470Sstevel@tonic-gate  * specification produced by the IETF IPv6 working group.  It has been
19480Sstevel@tonic-gate  * implemented so that the list of addresses is only traversed once (the
19490Sstevel@tonic-gate  * specification's algorithm could traverse the list of addresses once for
19500Sstevel@tonic-gate  * every rule).
19510Sstevel@tonic-gate  *
19520Sstevel@tonic-gate  * The restrict_ill argument restricts the algorithm to chose a source
19530Sstevel@tonic-gate  * address that is assigned to the destination ill or an ill in the same
19540Sstevel@tonic-gate  * IPMP group as the destination ill.  This is used when the destination
19550Sstevel@tonic-gate  * address is a link-local or multicast address, and when
19560Sstevel@tonic-gate  * ipv6_strict_dst_multihoming is turned on.
19570Sstevel@tonic-gate  *
19580Sstevel@tonic-gate  * src_prefs is the caller's set of source address preferences.  If source
19590Sstevel@tonic-gate  * address selection is being called to determine the source address of a
19600Sstevel@tonic-gate  * connected socket (from ip_bind_connected_v6()), then the preferences are
19610Sstevel@tonic-gate  * taken from conn_src_preferences.  These preferences can be set on a
19620Sstevel@tonic-gate  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
19630Sstevel@tonic-gate  * preference currently implemented is for rfc3041 temporary addresses.
19640Sstevel@tonic-gate  */
19650Sstevel@tonic-gate ipif_t *
19660Sstevel@tonic-gate ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
19672202Srk129064     uint_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid)
19680Sstevel@tonic-gate {
19690Sstevel@tonic-gate 	dstinfo_t	dstinfo;
19700Sstevel@tonic-gate 	char		dstr[INET6_ADDRSTRLEN];
19710Sstevel@tonic-gate 	char		sstr[INET6_ADDRSTRLEN];
19720Sstevel@tonic-gate 	ipif_t		*ipif;
19730Sstevel@tonic-gate 	ill_t		*ill, *usesrc_ill = NULL;
19740Sstevel@tonic-gate 	ill_walk_context_t	ctx;
19750Sstevel@tonic-gate 	cand_t		best_c;	/* The best candidate */
19760Sstevel@tonic-gate 	cand_t		curr_c;	/* The current candidate */
19770Sstevel@tonic-gate 	uint_t		index;
19780Sstevel@tonic-gate 	boolean_t	first_candidate = B_TRUE;
19790Sstevel@tonic-gate 	rule_res_t	rule_result;
19801676Sjpk 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	/*
19830Sstevel@tonic-gate 	 * The list of ordering rules.  They are applied in the order they
19840Sstevel@tonic-gate 	 * appear in the list.
19850Sstevel@tonic-gate 	 *
19860Sstevel@tonic-gate 	 * XXX rule_mipv6 will need to be implemented (the specification's
19870Sstevel@tonic-gate 	 * rules 4) if a mobile IPv6 node is ever implemented.
19880Sstevel@tonic-gate 	 */
19890Sstevel@tonic-gate 	rulef_t	rules[] = {
19900Sstevel@tonic-gate 		rule_isdst,
19910Sstevel@tonic-gate 		rule_scope,
19920Sstevel@tonic-gate 		rule_deprecated,
19930Sstevel@tonic-gate 		rule_preferred,
19940Sstevel@tonic-gate 		rule_interface,
19950Sstevel@tonic-gate 		rule_label,
19960Sstevel@tonic-gate 		rule_temporary,
19970Sstevel@tonic-gate 		rule_prefix,
19981676Sjpk 		rule_zone_specific,
19990Sstevel@tonic-gate 		NULL
20000Sstevel@tonic-gate 	};
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	ASSERT(dstill->ill_isv6);
20030Sstevel@tonic-gate 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	/*
20060Sstevel@tonic-gate 	 * Check if there is a usable src address pointed to by the
20070Sstevel@tonic-gate 	 * usesrc ifindex. This has higher precedence since it is
20080Sstevel@tonic-gate 	 * finer grained (i.e per interface) v/s being system wide.
20090Sstevel@tonic-gate 	 */
20100Sstevel@tonic-gate 	if (dstill->ill_usesrc_ifindex != 0) {
20110Sstevel@tonic-gate 		if ((usesrc_ill =
20120Sstevel@tonic-gate 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
20130Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL)) != NULL) {
20140Sstevel@tonic-gate 			dstinfo.dst_ill = usesrc_ill;
20150Sstevel@tonic-gate 		} else {
20160Sstevel@tonic-gate 			return (NULL);
20170Sstevel@tonic-gate 		}
20180Sstevel@tonic-gate 	} else {
20190Sstevel@tonic-gate 		dstinfo.dst_ill = dstill;
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate 
20221676Sjpk 	/*
20231676Sjpk 	 * If we're dealing with an unlabeled destination on a labeled system,
20241676Sjpk 	 * make sure that we ignore source addresses that are incompatible with
20251676Sjpk 	 * the destination's default label.  That destination's default label
20261676Sjpk 	 * must dominate the minimum label on the source address.
20271676Sjpk 	 *
20281676Sjpk 	 * (Note that this has to do with Trusted Solaris.  It's not related to
20291676Sjpk 	 * the labels described by ip6_asp_lookup.)
20301676Sjpk 	 */
20311676Sjpk 	dst_rhtp = NULL;
20321676Sjpk 	if (is_system_labeled()) {
20331676Sjpk 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
20341676Sjpk 		if (dst_rhtp == NULL)
20351676Sjpk 			return (NULL);
20361676Sjpk 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20371676Sjpk 			TPC_RELE(dst_rhtp);
20381676Sjpk 			dst_rhtp = NULL;
20391676Sjpk 		}
20401676Sjpk 	}
20411676Sjpk 
20420Sstevel@tonic-gate 	dstinfo.dst_addr = dst;
20430Sstevel@tonic-gate 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
20440Sstevel@tonic-gate 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL);
20450Sstevel@tonic-gate 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
20480Sstevel@tonic-gate 	/*
20490Sstevel@tonic-gate 	 * Section three of the I-D states that for multicast and
20500Sstevel@tonic-gate 	 * link-local destinations, the candidate set must be restricted to
20510Sstevel@tonic-gate 	 * an interface that is on the same link as the outgoing interface.
20520Sstevel@tonic-gate 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
20530Sstevel@tonic-gate 	 * restrict the source address to the destination link as doing
20540Sstevel@tonic-gate 	 * otherwise will almost certainly cause problems.
20550Sstevel@tonic-gate 	 */
20560Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
20572202Srk129064 	    ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
20582202Srk129064 		if (restrict_ill == RESTRICT_TO_NONE)
20592202Srk129064 			dstinfo.dst_restrict_ill = RESTRICT_TO_GROUP;
20602202Srk129064 		else
20612202Srk129064 			dstinfo.dst_restrict_ill = restrict_ill;
20622202Srk129064 	} else {
20630Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = restrict_ill;
20642202Srk129064 	}
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	bzero(&best_c, sizeof (cand_t));
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	/*
20690Sstevel@tonic-gate 	 * Take a pass through the list of IPv6 interfaces to chose the
20700Sstevel@tonic-gate 	 * best possible source address.  If restrict_ill is true, we only
20710Sstevel@tonic-gate 	 * iterate through the ill's that are in the same IPMP group as the
20720Sstevel@tonic-gate 	 * destination's outgoing ill.  If restrict_ill is false, we walk
20730Sstevel@tonic-gate 	 * the entire list of IPv6 ill's.
20740Sstevel@tonic-gate 	 */
20752202Srk129064 	if (dstinfo.dst_restrict_ill != RESTRICT_TO_NONE) {
20762202Srk129064 		if (dstinfo.dst_ill->ill_group != NULL &&
20772202Srk129064 		    dstinfo.dst_restrict_ill == RESTRICT_TO_GROUP) {
20782202Srk129064 			ill = dstinfo.dst_ill->ill_group->illgrp_ill;
20790Sstevel@tonic-gate 		} else {
20800Sstevel@tonic-gate 			ill = dstinfo.dst_ill;
20810Sstevel@tonic-gate 		}
20820Sstevel@tonic-gate 	} else {
20830Sstevel@tonic-gate 		ill = ILL_START_WALK_V6(&ctx);
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	while (ill != NULL) {
20870Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
20880Sstevel@tonic-gate 
20892202Srk129064 		/*
20902202Srk129064 		 * Avoid FAILED/OFFLINE ills.
20912202Srk129064 		 * Global and site local addresses will failover and
20922202Srk129064 		 * will be available on the new ill.
20932202Srk129064 		 * But link local addresses don't move.
20942202Srk129064 		 */
20952513Srk129064 		if (dstinfo.dst_restrict_ill != RESTRICT_TO_ILL &&
20962513Srk129064 		    ill->ill_phyint->phyint_flags &
20972202Srk129064 		    (PHYI_OFFLINE | PHYI_FAILED))
20982202Srk129064 			goto next_ill;
20992202Srk129064 
21000Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
21010Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
21040Sstevel@tonic-gate 				continue;
21050Sstevel@tonic-gate 
21061676Sjpk 			if (zoneid != ALL_ZONES &&
21071676Sjpk 			    ipif->ipif_zoneid != zoneid &&
21081676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
21090Sstevel@tonic-gate 				continue;
21100Sstevel@tonic-gate 
21111676Sjpk 			/*
21121676Sjpk 			 * Check compatibility of local address for
21131676Sjpk 			 * destination's default label if we're on a labeled
21141676Sjpk 			 * system.  Incompatible addresses can't be used at
21151676Sjpk 			 * all and must be skipped over.
21161676Sjpk 			 */
21171676Sjpk 			if (dst_rhtp != NULL) {
21181676Sjpk 				boolean_t incompat;
21191676Sjpk 
21201676Sjpk 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
21211676Sjpk 				    IPV6_VERSION, B_FALSE);
21221676Sjpk 				if (src_rhtp == NULL)
21231676Sjpk 					continue;
21241676Sjpk 				incompat =
21251676Sjpk 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
21261676Sjpk 				    src_rhtp->tpc_tp.tp_doi !=
21271676Sjpk 				    dst_rhtp->tpc_tp.tp_doi ||
21281676Sjpk 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
21291676Sjpk 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
21301676Sjpk 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
21311676Sjpk 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
21321676Sjpk 				TPC_RELE(src_rhtp);
21331676Sjpk 				if (incompat)
21341676Sjpk 					continue;
21351676Sjpk 			}
21361676Sjpk 
21370Sstevel@tonic-gate 			if (first_candidate) {
21380Sstevel@tonic-gate 				/*
21390Sstevel@tonic-gate 				 * This is first valid address in the list.
21400Sstevel@tonic-gate 				 * It is automatically the best candidate
21410Sstevel@tonic-gate 				 * so far.
21420Sstevel@tonic-gate 				 */
21430Sstevel@tonic-gate 				best_c.cand_ipif = ipif;
21440Sstevel@tonic-gate 				first_candidate = B_FALSE;
21450Sstevel@tonic-gate 				continue;
21460Sstevel@tonic-gate 			}
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 			bzero(&curr_c, sizeof (cand_t));
21490Sstevel@tonic-gate 			curr_c.cand_ipif = ipif;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 			/*
21520Sstevel@tonic-gate 			 * Compare this current candidate (curr_c) with the
21530Sstevel@tonic-gate 			 * best candidate (best_c) by applying the
21540Sstevel@tonic-gate 			 * comparison rules in order until one breaks the
21550Sstevel@tonic-gate 			 * tie.
21560Sstevel@tonic-gate 			 */
21570Sstevel@tonic-gate 			for (index = 0; rules[index] != NULL; index++) {
21580Sstevel@tonic-gate 				/* Apply a comparison rule. */
21590Sstevel@tonic-gate 				rule_result =
21600Sstevel@tonic-gate 				    (rules[index])(&best_c, &curr_c, &dstinfo);
21610Sstevel@tonic-gate 				if (rule_result == CAND_AVOID) {
21620Sstevel@tonic-gate 					/*
21630Sstevel@tonic-gate 					 * The best candidate is still the
21640Sstevel@tonic-gate 					 * best candidate.  Forget about
21650Sstevel@tonic-gate 					 * this current candidate and go on
21660Sstevel@tonic-gate 					 * to the next one.
21670Sstevel@tonic-gate 					 */
21680Sstevel@tonic-gate 					break;
21690Sstevel@tonic-gate 				} else if (rule_result == CAND_PREFER) {
21700Sstevel@tonic-gate 					/*
21710Sstevel@tonic-gate 					 * This candidate is prefered.  It
21720Sstevel@tonic-gate 					 * becomes the best candidate so
21730Sstevel@tonic-gate 					 * far.  Go on to the next address.
21740Sstevel@tonic-gate 					 */
21750Sstevel@tonic-gate 					best_c = curr_c;
21760Sstevel@tonic-gate 					break;
21770Sstevel@tonic-gate 				}
21780Sstevel@tonic-gate 				/* We have a tie, apply the next rule. */
21790Sstevel@tonic-gate 			}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 			/*
21820Sstevel@tonic-gate 			 * The last rule must be a tie breaker rule and
21830Sstevel@tonic-gate 			 * must never produce a tie.  At this point, the
21840Sstevel@tonic-gate 			 * candidate should have either been rejected, or
21850Sstevel@tonic-gate 			 * have been prefered as the best candidate so far.
21860Sstevel@tonic-gate 			 */
21870Sstevel@tonic-gate 			ASSERT(rule_result != CAND_TIE);
21880Sstevel@tonic-gate 		}
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 		/*
21910Sstevel@tonic-gate 		 * We may be walking the linked-list of ill's in an
21920Sstevel@tonic-gate 		 * IPMP group or traversing the IPv6 ill avl tree. If it is a
21930Sstevel@tonic-gate 		 * usesrc ILL then it can't be part of IPMP group and we
21940Sstevel@tonic-gate 		 * will exit the while loop.
21950Sstevel@tonic-gate 		 */
21962202Srk129064 next_ill:
21972202Srk129064 		if (dstinfo.dst_restrict_ill == RESTRICT_TO_ILL)
21982202Srk129064 			ill = NULL;
21992202Srk129064 		else if (dstinfo.dst_restrict_ill == RESTRICT_TO_GROUP)
22000Sstevel@tonic-gate 			ill = ill->ill_group_next;
22010Sstevel@tonic-gate 		else
22020Sstevel@tonic-gate 			ill = ill_next(&ctx, ill);
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	ipif = best_c.cand_ipif;
22060Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
22070Sstevel@tonic-gate 	    dstinfo.dst_ill->ill_name,
22080Sstevel@tonic-gate 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
22090Sstevel@tonic-gate 	    (ipif == NULL ? "NULL" :
22100Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	if (usesrc_ill != NULL)
22130Sstevel@tonic-gate 		ill_refrele(usesrc_ill);
22140Sstevel@tonic-gate 
22151676Sjpk 	if (dst_rhtp != NULL)
22161676Sjpk 		TPC_RELE(dst_rhtp);
22171676Sjpk 
22180Sstevel@tonic-gate 	if (ipif == NULL) {
22190Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
22200Sstevel@tonic-gate 		return (NULL);
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_ill->ill_lock);
22240Sstevel@tonic-gate 	if (IPIF_CAN_LOOKUP(ipif)) {
22250Sstevel@tonic-gate 		ipif_refhold_locked(ipif);
22260Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_ill->ill_lock);
22270Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
22280Sstevel@tonic-gate 		return (ipif);
22290Sstevel@tonic-gate 	}
22300Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_ill->ill_lock);
22310Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
22320Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
22330Sstevel@tonic-gate 	    " returning null \n", (void *)ipif));
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	return (NULL);
22360Sstevel@tonic-gate }
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate /*
22390Sstevel@tonic-gate  * If old_ipif is not NULL, see if ipif was derived from old
22400Sstevel@tonic-gate  * ipif and if so, recreate the interface route by re-doing
22410Sstevel@tonic-gate  * source address selection. This happens when ipif_down ->
22420Sstevel@tonic-gate  * ipif_update_other_ipifs calls us.
22430Sstevel@tonic-gate  *
22440Sstevel@tonic-gate  * If old_ipif is NULL, just redo the source address selection
22450Sstevel@tonic-gate  * if needed. This happens when illgrp_insert or ipif_up_done_v6
22460Sstevel@tonic-gate  * calls us.
22470Sstevel@tonic-gate  */
22480Sstevel@tonic-gate void
22490Sstevel@tonic-gate ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif)
22500Sstevel@tonic-gate {
22510Sstevel@tonic-gate 	ire_t *ire;
22520Sstevel@tonic-gate 	ire_t *ipif_ire;
22530Sstevel@tonic-gate 	queue_t *stq;
22540Sstevel@tonic-gate 	ill_t *ill;
22550Sstevel@tonic-gate 	ipif_t *nipif = NULL;
22560Sstevel@tonic-gate 	boolean_t nipif_refheld = B_FALSE;
22570Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	ill = ipif->ipif_ill;
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	if (!(ipif->ipif_flags &
22620Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
22630Sstevel@tonic-gate 		/*
22640Sstevel@tonic-gate 		 * Can't possibly have borrowed the source
22650Sstevel@tonic-gate 		 * from old_ipif.
22660Sstevel@tonic-gate 		 */
22670Sstevel@tonic-gate 		return;
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	/*
22710Sstevel@tonic-gate 	 * Is there any work to be done? No work if the address
22720Sstevel@tonic-gate 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
22730Sstevel@tonic-gate 	 * ipif_select_source_v6() does not borrow addresses from
22740Sstevel@tonic-gate 	 * NOLOCAL and ANYCAST interfaces).
22750Sstevel@tonic-gate 	 */
22760Sstevel@tonic-gate 	if ((old_ipif != NULL) &&
22770Sstevel@tonic-gate 	    ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) ||
22780Sstevel@tonic-gate 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
22790Sstevel@tonic-gate 	    (old_ipif->ipif_flags &
22800Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
22810Sstevel@tonic-gate 		return;
22820Sstevel@tonic-gate 	}
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	/*
22850Sstevel@tonic-gate 	 * Perform the same checks as when creating the
22860Sstevel@tonic-gate 	 * IRE_INTERFACE in ipif_up_done_v6.
22870Sstevel@tonic-gate 	 */
22880Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_UP))
22890Sstevel@tonic-gate 		return;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_NOXMIT))
22920Sstevel@tonic-gate 		return;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
22950Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
22960Sstevel@tonic-gate 		return;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	/*
22990Sstevel@tonic-gate 	 * We know that ipif uses some other source for its
23000Sstevel@tonic-gate 	 * IRE_INTERFACE. Is it using the source of this
23010Sstevel@tonic-gate 	 * old_ipif?
23020Sstevel@tonic-gate 	 */
23030Sstevel@tonic-gate 	ipif_ire = ipif_to_ire_v6(ipif);
23040Sstevel@tonic-gate 	if (ipif_ire == NULL)
23050Sstevel@tonic-gate 		return;
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	if (old_ipif != NULL &&
23080Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr,
23090Sstevel@tonic-gate 	    &ipif_ire->ire_src_addr_v6)) {
23100Sstevel@tonic-gate 		ire_refrele(ipif_ire);
23110Sstevel@tonic-gate 		return;
23120Sstevel@tonic-gate 	}
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	if (ip_debug > 2) {
23150Sstevel@tonic-gate 		/* ip1dbg */
23160Sstevel@tonic-gate 		pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE"
23170Sstevel@tonic-gate 		    " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6);
23180Sstevel@tonic-gate 	}
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	stq = ipif_ire->ire_stq;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	/*
23230Sstevel@tonic-gate 	 * Can't use our source address. Select a different source address
23240Sstevel@tonic-gate 	 * for the IRE_INTERFACE.  We restrict interface route source
23250Sstevel@tonic-gate 	 * address selection to ipif's assigned to the same link as the
23260Sstevel@tonic-gate 	 * interface.
23270Sstevel@tonic-gate 	 */
23280Sstevel@tonic-gate 	if (ip6_asp_can_lookup()) {
23290Sstevel@tonic-gate 		ip6_asp_table_held = B_TRUE;
23300Sstevel@tonic-gate 		nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet,
23312202Srk129064 		    RESTRICT_TO_GROUP, IPV6_PREFER_SRC_DEFAULT,
23322202Srk129064 		    ipif->ipif_zoneid);
23330Sstevel@tonic-gate 	}
23340Sstevel@tonic-gate 	if (nipif == NULL) {
23350Sstevel@tonic-gate 		/* Last resort - all ipif's have IPIF_NOLOCAL */
23360Sstevel@tonic-gate 		nipif = ipif;
23370Sstevel@tonic-gate 	} else {
23380Sstevel@tonic-gate 		nipif_refheld = B_TRUE;
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	ire = ire_create_v6(
23420Sstevel@tonic-gate 	    &ipif->ipif_v6subnet,	/* dest pref */
23430Sstevel@tonic-gate 	    &ipif->ipif_v6net_mask,	/* mask */
23440Sstevel@tonic-gate 	    &nipif->ipif_v6src_addr,	/* src addr */
23450Sstevel@tonic-gate 	    NULL,			/* no gateway */
23460Sstevel@tonic-gate 	    &ipif->ipif_mtu,		/* max frag */
23470Sstevel@tonic-gate 	    NULL,			/* no Fast path header */
23480Sstevel@tonic-gate 	    NULL,			/* no recv from queue */
23490Sstevel@tonic-gate 	    stq,			/* send-to queue */
23500Sstevel@tonic-gate 	    ill->ill_net_type,		/* IF_[NO]RESOLVER */
23510Sstevel@tonic-gate 	    ill->ill_resolver_mp,	/* xmit header */
23520Sstevel@tonic-gate 	    ipif,
23530Sstevel@tonic-gate 	    NULL,
23540Sstevel@tonic-gate 	    0,
23550Sstevel@tonic-gate 	    0,
23560Sstevel@tonic-gate 	    0,
23571676Sjpk 	    &ire_uinfo_null,
23581676Sjpk 	    NULL,
23591676Sjpk 	    NULL);
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	if (ire != NULL) {
23620Sstevel@tonic-gate 		ire_t *ret_ire;
23630Sstevel@tonic-gate 		int   error;
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 		/*
23660Sstevel@tonic-gate 		 * We don't need ipif_ire anymore. We need to delete
23670Sstevel@tonic-gate 		 * before we add so that ire_add does not detect
23680Sstevel@tonic-gate 		 * duplicates.
23690Sstevel@tonic-gate 		 */
23700Sstevel@tonic-gate 		ire_delete(ipif_ire);
23710Sstevel@tonic-gate 		ret_ire = ire;
23722535Ssangeeta 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
23730Sstevel@tonic-gate 		ASSERT(error == 0);
23740Sstevel@tonic-gate 		ASSERT(ret_ire == ire);
23750Sstevel@tonic-gate 		if (ret_ire != NULL) {
23760Sstevel@tonic-gate 			/* Held in ire_add */
23770Sstevel@tonic-gate 			ire_refrele(ret_ire);
23780Sstevel@tonic-gate 		}
23790Sstevel@tonic-gate 	}
23800Sstevel@tonic-gate 	/*
23810Sstevel@tonic-gate 	 * Either we are falling through from above or could not
23820Sstevel@tonic-gate 	 * allocate a replacement.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	ire_refrele(ipif_ire);
23850Sstevel@tonic-gate 	if (ip6_asp_table_held)
23860Sstevel@tonic-gate 		ip6_asp_table_refrele();
23870Sstevel@tonic-gate 	if (nipif_refheld)
23880Sstevel@tonic-gate 		ipif_refrele(nipif);
23890Sstevel@tonic-gate }
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate /*
23920Sstevel@tonic-gate  * This old_ipif is going away.
23930Sstevel@tonic-gate  *
23940Sstevel@tonic-gate  * Determine if any other ipif's are using our address as
23950Sstevel@tonic-gate  * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
23960Sstevel@tonic-gate  * IPIF_DEPRECATED).
23970Sstevel@tonic-gate  * Find the IRE_INTERFACE for such ipif's and recreate them
23980Sstevel@tonic-gate  * to use an different source address following the rules in
23990Sstevel@tonic-gate  * ipif_up_done_v6.
24000Sstevel@tonic-gate  *
24010Sstevel@tonic-gate  * This function takes an illgrp as an argument so that illgrp_delete
24020Sstevel@tonic-gate  * can call this to update source address even after deleting the
24030Sstevel@tonic-gate  * old_ipif->ipif_ill from the ill group.
24040Sstevel@tonic-gate  */
24050Sstevel@tonic-gate void
24060Sstevel@tonic-gate ipif_update_other_ipifs_v6(ipif_t *old_ipif, ill_group_t *illgrp)
24070Sstevel@tonic-gate {
24080Sstevel@tonic-gate 	ipif_t	*ipif;
24090Sstevel@tonic-gate 	ill_t	*ill;
24100Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	ASSERT(IAM_WRITER_IPIF(old_ipif));
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	ill = old_ipif->ipif_ill;
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n",
24170Sstevel@tonic-gate 	    ill->ill_name,
24180Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr,
24190Sstevel@tonic-gate 	    buf, sizeof (buf))));
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	/*
24220Sstevel@tonic-gate 	 * If this part of a group, look at all ills as ipif_select_source
24230Sstevel@tonic-gate 	 * borrows a source address across all the ills in the group.
24240Sstevel@tonic-gate 	 */
24250Sstevel@tonic-gate 	if (illgrp != NULL)
24260Sstevel@tonic-gate 		ill = illgrp->illgrp_ill;
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	/* Don't need a lock since this is a writer */
24290Sstevel@tonic-gate 	for (; ill != NULL; ill = ill->ill_group_next) {
24300Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
24310Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate 			if (ipif == old_ipif)
24340Sstevel@tonic-gate 				continue;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 			ipif_recreate_interface_routes_v6(old_ipif, ipif);
24370Sstevel@tonic-gate 		}
24380Sstevel@tonic-gate 	}
24390Sstevel@tonic-gate }
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate /*
24420Sstevel@tonic-gate  * Perform an attach and bind to get phys addr plus info_req for
24430Sstevel@tonic-gate  * the physical device.
24440Sstevel@tonic-gate  * q and mp represents an ioctl which will be queued waiting for
24450Sstevel@tonic-gate  * completion of the DLPI message exchange.
24460Sstevel@tonic-gate  * MUST be called on an ill queue. Can not set conn_pending_ill for that
24470Sstevel@tonic-gate  * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q.
24480Sstevel@tonic-gate  *
24490Sstevel@tonic-gate  * Returns EINPROGRESS when mp has been consumed by queueing it on
24500Sstevel@tonic-gate  * ill_pending_mp and the ioctl will complete in ip_rput.
24510Sstevel@tonic-gate  */
24520Sstevel@tonic-gate int
24530Sstevel@tonic-gate ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
24540Sstevel@tonic-gate {
24550Sstevel@tonic-gate 	mblk_t	*v6token_mp = NULL;
24560Sstevel@tonic-gate 	mblk_t	*v6lla_mp = NULL;
24570Sstevel@tonic-gate 	mblk_t	*phys_mp = NULL;
24580Sstevel@tonic-gate 	mblk_t	*info_mp = NULL;
24590Sstevel@tonic-gate 	mblk_t	*attach_mp = NULL;
24600Sstevel@tonic-gate 	mblk_t	*detach_mp = NULL;
24610Sstevel@tonic-gate 	mblk_t	*bind_mp = NULL;
24620Sstevel@tonic-gate 	mblk_t	*unbind_mp = NULL;
24630Sstevel@tonic-gate 	mblk_t	*notify_mp = NULL;
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
24660Sstevel@tonic-gate 	ASSERT(ill->ill_dlpi_style_set);
24670Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	if (ill->ill_isv6) {
24700Sstevel@tonic-gate 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
24710Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
24720Sstevel@tonic-gate 		if (v6token_mp == NULL)
24730Sstevel@tonic-gate 			goto bad;
24740Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
24750Sstevel@tonic-gate 		    DL_IPV6_TOKEN;
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
24780Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
24790Sstevel@tonic-gate 		if (v6lla_mp == NULL)
24800Sstevel@tonic-gate 			goto bad;
24810Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
24820Sstevel@tonic-gate 		    DL_IPV6_LINK_LAYER_ADDR;
24830Sstevel@tonic-gate 	}
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 	/*
24860Sstevel@tonic-gate 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
24870Sstevel@tonic-gate 	 */
24880Sstevel@tonic-gate 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
24890Sstevel@tonic-gate 	    DL_NOTIFY_REQ);
24900Sstevel@tonic-gate 	if (notify_mp == NULL)
24910Sstevel@tonic-gate 		goto bad;
24920Sstevel@tonic-gate 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
24930Sstevel@tonic-gate 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
24940Sstevel@tonic-gate 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG);
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
24970Sstevel@tonic-gate 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
24980Sstevel@tonic-gate 	if (phys_mp == NULL)
24990Sstevel@tonic-gate 		goto bad;
25000Sstevel@tonic-gate 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
25010Sstevel@tonic-gate 	    DL_CURR_PHYS_ADDR;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	info_mp = ip_dlpi_alloc(
25040Sstevel@tonic-gate 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
25050Sstevel@tonic-gate 	    DL_INFO_REQ);
25060Sstevel@tonic-gate 	if (info_mp == NULL)
25070Sstevel@tonic-gate 		goto bad;
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
25100Sstevel@tonic-gate 	    DL_BIND_REQ);
25110Sstevel@tonic-gate 	if (bind_mp == NULL)
25120Sstevel@tonic-gate 		goto bad;
25130Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
25140Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
25170Sstevel@tonic-gate 	if (unbind_mp == NULL)
25180Sstevel@tonic-gate 		goto bad;
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	/* If we need to attach/detach, pre-alloc and initialize the mblks */
25210Sstevel@tonic-gate 	if (ill->ill_needs_attach) {
25220Sstevel@tonic-gate 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
25230Sstevel@tonic-gate 		    DL_ATTACH_REQ);
25240Sstevel@tonic-gate 		if (attach_mp == NULL)
25250Sstevel@tonic-gate 			goto bad;
25260Sstevel@tonic-gate 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 		detach_mp = ip_dlpi_alloc(sizeof (dl_detach_req_t),
25290Sstevel@tonic-gate 		    DL_DETACH_REQ);
25300Sstevel@tonic-gate 		if (detach_mp == NULL)
25310Sstevel@tonic-gate 			goto bad;
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 	/*
25350Sstevel@tonic-gate 	 * Here we are going to delay the ioctl ack until after
25360Sstevel@tonic-gate 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
25370Sstevel@tonic-gate 	 * original ioctl message before sending the requests
25380Sstevel@tonic-gate 	 */
25390Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
25400Sstevel@tonic-gate 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
25410Sstevel@tonic-gate 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
25420Sstevel@tonic-gate 	/*
25430Sstevel@tonic-gate 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
25440Sstevel@tonic-gate 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
25450Sstevel@tonic-gate 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
25460Sstevel@tonic-gate 	 */
25470Sstevel@tonic-gate 	ill->ill_phys_addr_pend = 0;
25480Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 	if (attach_mp != NULL) {
25510Sstevel@tonic-gate 		ip1dbg(("ill_dl_phys: attach\n"));
25520Sstevel@tonic-gate 		ill_dlpi_send(ill, attach_mp);
25530Sstevel@tonic-gate 	}
25540Sstevel@tonic-gate 	ill_dlpi_send(ill, bind_mp);
25550Sstevel@tonic-gate 	ill_dlpi_send(ill, info_mp);
25560Sstevel@tonic-gate 	if (ill->ill_isv6) {
25570Sstevel@tonic-gate 		ill_dlpi_send(ill, v6token_mp);
25580Sstevel@tonic-gate 		ill_dlpi_send(ill, v6lla_mp);
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 	ill_dlpi_send(ill, phys_mp);
25610Sstevel@tonic-gate 	ill_dlpi_send(ill, notify_mp);
25620Sstevel@tonic-gate 	ill_dlpi_send(ill, unbind_mp);
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	/*
25650Sstevel@tonic-gate 	 * Save the DL_DETACH_REQ (if there is one) for use in ill_delete().
25660Sstevel@tonic-gate 	 */
25670Sstevel@tonic-gate 	ASSERT(ill->ill_detach_mp == NULL);
25680Sstevel@tonic-gate 	ill->ill_detach_mp = detach_mp;
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	/*
25710Sstevel@tonic-gate 	 * This operation will complete in ip_rput_dlpi_writer with either
25720Sstevel@tonic-gate 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
25730Sstevel@tonic-gate 	 */
25740Sstevel@tonic-gate 	return (EINPROGRESS);
25750Sstevel@tonic-gate bad:
25760Sstevel@tonic-gate 	if (v6token_mp != NULL)
25770Sstevel@tonic-gate 		freemsg(v6token_mp);
25780Sstevel@tonic-gate 	if (v6lla_mp != NULL)
25790Sstevel@tonic-gate 		freemsg(v6lla_mp);
25800Sstevel@tonic-gate 	if (phys_mp != NULL)
25810Sstevel@tonic-gate 		freemsg(phys_mp);
25820Sstevel@tonic-gate 	if (info_mp != NULL)
25830Sstevel@tonic-gate 		freemsg(info_mp);
25840Sstevel@tonic-gate 	if (attach_mp != NULL)
25850Sstevel@tonic-gate 		freemsg(attach_mp);
25860Sstevel@tonic-gate 	if (detach_mp != NULL)
25870Sstevel@tonic-gate 		freemsg(detach_mp);
25880Sstevel@tonic-gate 	if (bind_mp != NULL)
25890Sstevel@tonic-gate 		freemsg(bind_mp);
25900Sstevel@tonic-gate 	if (unbind_mp != NULL)
25910Sstevel@tonic-gate 		freemsg(unbind_mp);
25920Sstevel@tonic-gate 	if (notify_mp != NULL)
25930Sstevel@tonic-gate 		freemsg(notify_mp);
25940Sstevel@tonic-gate 	return (ENOMEM);
25950Sstevel@tonic-gate }
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate /*
26000Sstevel@tonic-gate  * DLPI is up.
26010Sstevel@tonic-gate  * Create all the IREs associated with an interface bring up multicast.
26020Sstevel@tonic-gate  * Set the interface flag and finish other initialization
26030Sstevel@tonic-gate  * that potentially had to be differed to after DL_BIND_ACK.
26040Sstevel@tonic-gate  */
26050Sstevel@tonic-gate int
26060Sstevel@tonic-gate ipif_up_done_v6(ipif_t *ipif)
26070Sstevel@tonic-gate {
26080Sstevel@tonic-gate 	ire_t	*ire_array[20];
26090Sstevel@tonic-gate 	ire_t	**irep = ire_array;
26100Sstevel@tonic-gate 	ire_t	**irep1;
26110Sstevel@tonic-gate 	ill_t	*ill = ipif->ipif_ill;
26120Sstevel@tonic-gate 	queue_t	*stq;
26130Sstevel@tonic-gate 	in6_addr_t	v6addr;
26140Sstevel@tonic-gate 	in6_addr_t	route_mask;
26150Sstevel@tonic-gate 	ipif_t	 *src_ipif = NULL;
26160Sstevel@tonic-gate 	ipif_t   *tmp_ipif;
26170Sstevel@tonic-gate 	boolean_t	flush_ire_cache = B_TRUE;
26180Sstevel@tonic-gate 	int	err;
26190Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
26200Sstevel@tonic-gate 	phyint_t *phyi;
26210Sstevel@tonic-gate 	ire_t	**ipif_saved_irep = NULL;
26220Sstevel@tonic-gate 	int ipif_saved_ire_cnt;
26230Sstevel@tonic-gate 	int cnt;
26240Sstevel@tonic-gate 	boolean_t src_ipif_held = B_FALSE;
26250Sstevel@tonic-gate 	boolean_t ire_added = B_FALSE;
26260Sstevel@tonic-gate 	boolean_t loopback = B_FALSE;
26270Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
26300Sstevel@tonic-gate 		ipif->ipif_ill->ill_name, ipif->ipif_id));
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 	/* Check if this is a loopback interface */
26330Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_wq == NULL)
26340Sstevel@tonic-gate 		loopback = B_TRUE;
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
26370Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	/*
26400Sstevel@tonic-gate 	 * If all other interfaces for this ill are down or DEPRECATED,
26410Sstevel@tonic-gate 	 * or otherwise unsuitable for source address selection, remove
26420Sstevel@tonic-gate 	 * any IRE_CACHE entries for this ill to make sure source
26430Sstevel@tonic-gate 	 * address selection gets to take this new ipif into account.
26440Sstevel@tonic-gate 	 * No need to hold ill_lock while traversing the ipif list since
26450Sstevel@tonic-gate 	 * we are writer
26460Sstevel@tonic-gate 	 */
26470Sstevel@tonic-gate 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
26480Sstevel@tonic-gate 		tmp_ipif = tmp_ipif->ipif_next) {
26490Sstevel@tonic-gate 		if (((tmp_ipif->ipif_flags &
26500Sstevel@tonic-gate 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
26510Sstevel@tonic-gate 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
26520Sstevel@tonic-gate 		    (tmp_ipif == ipif))
26530Sstevel@tonic-gate 			continue;
26540Sstevel@tonic-gate 		/* first useable pre-existing interface */
26550Sstevel@tonic-gate 		flush_ire_cache = B_FALSE;
26560Sstevel@tonic-gate 		break;
26570Sstevel@tonic-gate 	}
26580Sstevel@tonic-gate 	if (flush_ire_cache)
26590Sstevel@tonic-gate 		ire_walk_ill_v6(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
26600Sstevel@tonic-gate 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	/*
26630Sstevel@tonic-gate 	 * Figure out which way the send-to queue should go.  Only
26640Sstevel@tonic-gate 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here.
26650Sstevel@tonic-gate 	 */
26660Sstevel@tonic-gate 	switch (ill->ill_net_type) {
26670Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
26680Sstevel@tonic-gate 		stq = ill->ill_rq;
26690Sstevel@tonic-gate 		break;
26700Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
26710Sstevel@tonic-gate 	case IRE_LOOPBACK:
26720Sstevel@tonic-gate 		stq = ill->ill_wq;
26730Sstevel@tonic-gate 		break;
26740Sstevel@tonic-gate 	default:
26750Sstevel@tonic-gate 		return (EINVAL);
26760Sstevel@tonic-gate 	}
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
26790Sstevel@tonic-gate 		/*
26800Sstevel@tonic-gate 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
26810Sstevel@tonic-gate 		 * ipif_lookup_on_name(), but in the case of zones we can have
26820Sstevel@tonic-gate 		 * several loopback addresses on lo0. So all the interfaces with
26830Sstevel@tonic-gate 		 * loopback addresses need to be marked IRE_LOOPBACK.
26840Sstevel@tonic-gate 		 */
26850Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
26860Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOOPBACK;
26870Sstevel@tonic-gate 		else
26880Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOCAL;
26890Sstevel@tonic-gate 	}
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
26920Sstevel@tonic-gate 		/*
26930Sstevel@tonic-gate 		 * Can't use our source address. Select a different
26940Sstevel@tonic-gate 		 * source address for the IRE_INTERFACE and IRE_LOCAL
26950Sstevel@tonic-gate 		 */
26960Sstevel@tonic-gate 		if (ip6_asp_can_lookup()) {
26970Sstevel@tonic-gate 			ip6_asp_table_held = B_TRUE;
26980Sstevel@tonic-gate 			src_ipif = ipif_select_source_v6(ipif->ipif_ill,
26992202Srk129064 			    &ipif->ipif_v6subnet, RESTRICT_TO_NONE,
27000Sstevel@tonic-gate 			    IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
27010Sstevel@tonic-gate 		}
27020Sstevel@tonic-gate 		if (src_ipif == NULL)
27030Sstevel@tonic-gate 			src_ipif = ipif;	/* Last resort */
27040Sstevel@tonic-gate 		else
27050Sstevel@tonic-gate 			src_ipif_held = B_TRUE;
27060Sstevel@tonic-gate 	} else {
27070Sstevel@tonic-gate 		src_ipif = ipif;
27080Sstevel@tonic-gate 	}
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
27110Sstevel@tonic-gate 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
27121676Sjpk 
27131676Sjpk 		/*
27141676Sjpk 		 * If we're on a labeled system then make sure that zone-
27151676Sjpk 		 * private addresses have proper remote host database entries.
27161676Sjpk 		 */
27171676Sjpk 		if (is_system_labeled() &&
27181676Sjpk 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
27191676Sjpk 			if (ip6opt_ls == 0) {
27201676Sjpk 				cmn_err(CE_WARN, "IPv6 not enabled "
27211676Sjpk 				    "via /etc/system");
27221676Sjpk 				return (EINVAL);
27231676Sjpk 			}
27241676Sjpk 			if (!tsol_check_interface_address(ipif))
27251676Sjpk 				return (EINVAL);
27261676Sjpk 		}
27271676Sjpk 
27280Sstevel@tonic-gate 		/* Register the source address for __sin6_src_id */
27290Sstevel@tonic-gate 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
27300Sstevel@tonic-gate 		    ipif->ipif_zoneid);
27310Sstevel@tonic-gate 		if (err != 0) {
27320Sstevel@tonic-gate 			ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err));
27330Sstevel@tonic-gate 			if (src_ipif_held)
27340Sstevel@tonic-gate 				ipif_refrele(src_ipif);
27350Sstevel@tonic-gate 			if (ip6_asp_table_held)
27360Sstevel@tonic-gate 				ip6_asp_table_refrele();
27370Sstevel@tonic-gate 			return (err);
27380Sstevel@tonic-gate 		}
27390Sstevel@tonic-gate 		/*
27400Sstevel@tonic-gate 		 * If the interface address is set, create the LOCAL
27410Sstevel@tonic-gate 		 * or LOOPBACK IRE.
27420Sstevel@tonic-gate 		 */
27430Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n",
27440Sstevel@tonic-gate 		    ipif->ipif_ire_type,
27450Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
27460Sstevel@tonic-gate 		    buf, sizeof (buf))));
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 		*irep++ = ire_create_v6(
27490Sstevel@tonic-gate 		    &ipif->ipif_v6lcl_addr,		/* dest address */
27500Sstevel@tonic-gate 		    &ipv6_all_ones,			/* mask */
27510Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,		/* source address */
27520Sstevel@tonic-gate 		    NULL,				/* no gateway */
27530Sstevel@tonic-gate 		    &ip_loopback_mtu_v6plus,		/* max frag size */
27540Sstevel@tonic-gate 		    NULL,
27550Sstevel@tonic-gate 		    ipif->ipif_rq,			/* recv-from queue */
27560Sstevel@tonic-gate 		    NULL,				/* no send-to queue */
27570Sstevel@tonic-gate 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
27580Sstevel@tonic-gate 		    NULL,
27590Sstevel@tonic-gate 		    ipif,				/* interface */
27600Sstevel@tonic-gate 		    NULL,
27610Sstevel@tonic-gate 		    0,
27620Sstevel@tonic-gate 		    0,
27630Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
27641676Sjpk 		    &ire_uinfo_null,
27651676Sjpk 		    NULL,
27661676Sjpk 		    NULL);
27670Sstevel@tonic-gate 	}
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	/*
27700Sstevel@tonic-gate 	 * Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate.
27710Sstevel@tonic-gate 	 * Note that atun interfaces have an all-zero ipif_v6subnet.
27720Sstevel@tonic-gate 	 * Thus we allow a zero subnet as long as the mask is non-zero.
27730Sstevel@tonic-gate 	 */
27740Sstevel@tonic-gate 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
27750Sstevel@tonic-gate 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
27760Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
27770Sstevel@tonic-gate 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
27780Sstevel@tonic-gate 		v6addr = ipif->ipif_v6subnet;
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
27810Sstevel@tonic-gate 			route_mask = ipv6_all_ones;
27820Sstevel@tonic-gate 		} else {
27830Sstevel@tonic-gate 			route_mask = ipif->ipif_v6net_mask;
27840Sstevel@tonic-gate 		}
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n",
27870Sstevel@tonic-gate 		    ill->ill_net_type,
27880Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 		*irep++ = ire_create_v6(
27910Sstevel@tonic-gate 		    &v6addr,			/* dest pref */
27920Sstevel@tonic-gate 		    &route_mask,		/* mask */
27930Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,	/* src addr */
27940Sstevel@tonic-gate 		    NULL,			/* no gateway */
27950Sstevel@tonic-gate 		    &ipif->ipif_mtu,		/* max frag */
27960Sstevel@tonic-gate 		    NULL,			/* no Fast path header */
27970Sstevel@tonic-gate 		    NULL,			/* no recv from queue */
27980Sstevel@tonic-gate 		    stq,			/* send-to queue */
27990Sstevel@tonic-gate 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
28000Sstevel@tonic-gate 		    ill->ill_resolver_mp,	/* xmit header */
28010Sstevel@tonic-gate 		    ipif,
28020Sstevel@tonic-gate 		    NULL,
28030Sstevel@tonic-gate 		    0,
28040Sstevel@tonic-gate 		    0,
28050Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
28061676Sjpk 		    &ire_uinfo_null,
28071676Sjpk 		    NULL,
28081676Sjpk 		    NULL);
28090Sstevel@tonic-gate 	}
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	/*
28120Sstevel@tonic-gate 	 * Setup 2002::/16 route, if this interface is a 6to4 tunnel
28130Sstevel@tonic-gate 	 */
28140Sstevel@tonic-gate 	if (IN6_IS_ADDR_6TO4(&ipif->ipif_v6lcl_addr) &&
28150Sstevel@tonic-gate 	    (ill->ill_is_6to4tun)) {
28160Sstevel@tonic-gate 		/*
28170Sstevel@tonic-gate 		 * Destination address is 2002::/16
28180Sstevel@tonic-gate 		 */
28190Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
28200Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x20020000U, 0, 0, 0 };
28210Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0xffff0000U, 0, 0, 0 };
28220Sstevel@tonic-gate #else
28230Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x00000220U, 0, 0, 0 };
28240Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0x0000ffffU, 0, 0, 0 };
28250Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
28260Sstevel@tonic-gate 		char	buf2[INET6_ADDRSTRLEN];
28270Sstevel@tonic-gate 		ire_t *isdup;
28280Sstevel@tonic-gate 		in6_addr_t *first_addr = &ill->ill_ipif->ipif_v6lcl_addr;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 		/*
28310Sstevel@tonic-gate 		 * check to see if this route has already been added for
28320Sstevel@tonic-gate 		 * this tunnel interface.
28330Sstevel@tonic-gate 		 */
28340Sstevel@tonic-gate 		isdup = ire_ftable_lookup_v6(first_addr, &prefix_mask, 0,
28351676Sjpk 		    IRE_IF_NORESOLVER, ill->ill_ipif, NULL, ALL_ZONES, 0, NULL,
28360Sstevel@tonic-gate 		    (MATCH_IRE_SRC | MATCH_IRE_MASK));
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 		if (isdup == NULL) {
28390Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s",
28400Sstevel@tonic-gate 			    IRE_IF_NORESOLVER, inet_ntop(AF_INET6, &v6addr,
28410Sstevel@tonic-gate 				buf2, sizeof (buf2))));
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 			*irep++ = ire_create_v6(
28440Sstevel@tonic-gate 			    &prefix_addr,		/* 2002:: */
28450Sstevel@tonic-gate 			    &prefix_mask,		/* ffff:: */
28460Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, 	/* src addr */
28470Sstevel@tonic-gate 			    NULL, 			/* gateway */
28480Sstevel@tonic-gate 			    &ipif->ipif_mtu, 		/* max_frag */
28490Sstevel@tonic-gate 			    NULL, 			/* no Fast Path hdr */
28500Sstevel@tonic-gate 			    NULL, 			/* no rfq */
28510Sstevel@tonic-gate 			    ill->ill_wq, 		/* stq */
28520Sstevel@tonic-gate 			    IRE_IF_NORESOLVER,		/* type */
28530Sstevel@tonic-gate 			    ill->ill_resolver_mp,	/* dlureq_mp */
28540Sstevel@tonic-gate 			    ipif,			/* interface */
28550Sstevel@tonic-gate 			    NULL,			/* v6cmask */
28560Sstevel@tonic-gate 			    0,
28570Sstevel@tonic-gate 			    0,
28580Sstevel@tonic-gate 			    RTF_UP,
28591676Sjpk 			    &ire_uinfo_null,
28601676Sjpk 			    NULL,
28611676Sjpk 			    NULL);
28620Sstevel@tonic-gate 		} else {
28630Sstevel@tonic-gate 			ire_refrele(isdup);
28640Sstevel@tonic-gate 		}
28650Sstevel@tonic-gate 	}
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 	/* If an earlier ire_create failed, get out now */
28680Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
28690Sstevel@tonic-gate 		irep1--;
28700Sstevel@tonic-gate 		if (*irep1 == NULL) {
28710Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: NULL ire found in"
28720Sstevel@tonic-gate 			    " ire_array\n"));
28730Sstevel@tonic-gate 			err = ENOMEM;
28740Sstevel@tonic-gate 			goto bad;
28750Sstevel@tonic-gate 		}
28760Sstevel@tonic-gate 	}
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	/*
28810Sstevel@tonic-gate 	 * Need to atomically check for ip_addr_availablity_check
28820Sstevel@tonic-gate 	 * now under ill_g_lock, and if it fails got bad, and remove
28830Sstevel@tonic-gate 	 * from group also
28840Sstevel@tonic-gate 	 */
28850Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
28860Sstevel@tonic-gate 	mutex_enter(&ip_addr_avail_lock);
28870Sstevel@tonic-gate 	ill->ill_ipif_up_count++;
28880Sstevel@tonic-gate 	ipif->ipif_flags |= IPIF_UP;
28890Sstevel@tonic-gate 	err = ip_addr_availability_check(ipif);
28900Sstevel@tonic-gate 	mutex_exit(&ip_addr_avail_lock);
28910Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
28920Sstevel@tonic-gate 
28930Sstevel@tonic-gate 	if (err != 0) {
28940Sstevel@tonic-gate 		/*
28950Sstevel@tonic-gate 		 * Our address may already be up on the same ill. In this case,
28960Sstevel@tonic-gate 		 * the external resolver entry for our ipif replaced the one for
28970Sstevel@tonic-gate 		 * the other ipif. So we don't want to delete it (otherwise the
28980Sstevel@tonic-gate 		 * other ipif would be unable to send packets).
28990Sstevel@tonic-gate 		 * ip_addr_availability_check() identifies this case for us and
29000Sstevel@tonic-gate 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
29010Sstevel@tonic-gate 		 * which is the expected error code.
29020Sstevel@tonic-gate 		 */
29030Sstevel@tonic-gate 		if (err == EADDRINUSE) {
29040Sstevel@tonic-gate 			if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) {
29050Sstevel@tonic-gate 				freemsg(ipif->ipif_arp_del_mp);
29060Sstevel@tonic-gate 				ipif->ipif_arp_del_mp = NULL;
29070Sstevel@tonic-gate 			}
29080Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
29090Sstevel@tonic-gate 		}
29100Sstevel@tonic-gate 		ill->ill_ipif_up_count--;
29110Sstevel@tonic-gate 		ipif->ipif_flags &= ~IPIF_UP;
29120Sstevel@tonic-gate 		goto bad;
29130Sstevel@tonic-gate 	}
29140Sstevel@tonic-gate 
29150Sstevel@tonic-gate 	/*
29160Sstevel@tonic-gate 	 * Add in all newly created IREs. We want to add before
29170Sstevel@tonic-gate 	 * we call ifgrp_insert which wants to know whether
29180Sstevel@tonic-gate 	 * IRE_IF_RESOLVER exists or not.
29190Sstevel@tonic-gate 	 *
29200Sstevel@tonic-gate 	 * NOTE : We refrele the ire though we may branch to "bad"
29210Sstevel@tonic-gate 	 *	  later on where we do ire_delete. This is okay
29220Sstevel@tonic-gate 	 *	  because nobody can delete it as we are running
29230Sstevel@tonic-gate 	 *	  exclusively.
29240Sstevel@tonic-gate 	 */
29250Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
29260Sstevel@tonic-gate 		irep1--;
29270Sstevel@tonic-gate 		/* Shouldn't be adding any bcast ire's */
29280Sstevel@tonic-gate 		ASSERT((*irep1)->ire_type != IRE_BROADCAST);
29290Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
29300Sstevel@tonic-gate 		/*
29310Sstevel@tonic-gate 		 * refheld by ire_add. refele towards the end of the func
29320Sstevel@tonic-gate 		 */
29332535Ssangeeta 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
29340Sstevel@tonic-gate 	}
29350Sstevel@tonic-gate 	if (ip6_asp_table_held) {
29360Sstevel@tonic-gate 		ip6_asp_table_refrele();
29370Sstevel@tonic-gate 		ip6_asp_table_held = B_FALSE;
29380Sstevel@tonic-gate 	}
29390Sstevel@tonic-gate 	ire_added = B_TRUE;
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	/*
29420Sstevel@tonic-gate 	 * Form groups if possible.
29430Sstevel@tonic-gate 	 *
29440Sstevel@tonic-gate 	 * If we are supposed to be in a ill_group with a name, insert it
29450Sstevel@tonic-gate 	 * now as we know that at least one ipif is UP. Otherwise form
29460Sstevel@tonic-gate 	 * nameless groups.
29470Sstevel@tonic-gate 	 *
29480Sstevel@tonic-gate 	 * If ip_enable_group_ifs is set and ipif address is not ::0, insert
29490Sstevel@tonic-gate 	 * this ipif into the appropriate interface group, or create a
29500Sstevel@tonic-gate 	 * new one. If this is already in a nameless group, we try to form
29510Sstevel@tonic-gate 	 * a bigger group looking at other ills potentially sharing this
29520Sstevel@tonic-gate 	 * ipif's prefix.
29530Sstevel@tonic-gate 	 */
29540Sstevel@tonic-gate 	phyi = ill->ill_phyint;
29550Sstevel@tonic-gate 	if (phyi->phyint_groupname_len != 0) {
29560Sstevel@tonic-gate 		ASSERT(phyi->phyint_groupname != NULL);
29570Sstevel@tonic-gate 		if (ill->ill_ipif_up_count == 1) {
29580Sstevel@tonic-gate 			ASSERT(ill->ill_group == NULL);
29590Sstevel@tonic-gate 			err = illgrp_insert(&illgrp_head_v6, ill,
29600Sstevel@tonic-gate 			    phyi->phyint_groupname, NULL, B_TRUE);
29610Sstevel@tonic-gate 			if (err != 0) {
29620Sstevel@tonic-gate 				ip1dbg(("ipif_up_done_v6: illgrp allocation "
29630Sstevel@tonic-gate 				    "failed, error %d\n", err));
29640Sstevel@tonic-gate 				goto bad;
29650Sstevel@tonic-gate 			}
29660Sstevel@tonic-gate 		}
29670Sstevel@tonic-gate 		ASSERT(ill->ill_group != NULL);
29680Sstevel@tonic-gate 	}
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
29710Sstevel@tonic-gate 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
29720Sstevel@tonic-gate 	ipif_saved_irep = ipif_recover_ire_v6(ipif);
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
29750Sstevel@tonic-gate 		/*
29760Sstevel@tonic-gate 		 * Need to recover all multicast memberships in the driver.
29770Sstevel@tonic-gate 		 * This had to be deferred until we had attached.
29780Sstevel@tonic-gate 		 */
29790Sstevel@tonic-gate 		ill_recover_multicast(ill);
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 	/* Join the allhosts multicast address and the solicited node MC */
29820Sstevel@tonic-gate 	ipif_multicast_up(ipif);
29830Sstevel@tonic-gate 
29840Sstevel@tonic-gate 	if (!loopback) {
29850Sstevel@tonic-gate 		/*
29860Sstevel@tonic-gate 		 * See whether anybody else would benefit from the
29870Sstevel@tonic-gate 		 * new ipif that we added. We call this always rather
29880Sstevel@tonic-gate 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
29890Sstevel@tonic-gate 		 * ipif for the benefit of illgrp_insert (done above)
29900Sstevel@tonic-gate 		 * which does not do source address selection as it does
29910Sstevel@tonic-gate 		 * not want to re-create interface routes that we are
29920Sstevel@tonic-gate 		 * having reference to it here.
29930Sstevel@tonic-gate 		 */
29940Sstevel@tonic-gate 		ill_update_source_selection(ill);
29950Sstevel@tonic-gate 	}
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
29980Sstevel@tonic-gate 		irep1--;
29990Sstevel@tonic-gate 		if (*irep1 != NULL) {
30000Sstevel@tonic-gate 			/* was held in ire_add */
30010Sstevel@tonic-gate 			ire_refrele(*irep1);
30020Sstevel@tonic-gate 		}
30030Sstevel@tonic-gate 	}
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	cnt = ipif_saved_ire_cnt;
30060Sstevel@tonic-gate 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
30070Sstevel@tonic-gate 		if (*irep1 != NULL) {
30080Sstevel@tonic-gate 			/* was held in ire_add */
30090Sstevel@tonic-gate 			ire_refrele(*irep1);
30100Sstevel@tonic-gate 		}
30110Sstevel@tonic-gate 	}
30120Sstevel@tonic-gate 
3013*2546Scarlsonj 	if (ipif->ipif_addr_ready) {
3014*2546Scarlsonj 		ip_rts_ifmsg(ipif);
3015*2546Scarlsonj 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
3016*2546Scarlsonj 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
3017*2546Scarlsonj 	}
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
30200Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
30210Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
30220Sstevel@tonic-gate 	}
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate 	if (src_ipif_held)
30250Sstevel@tonic-gate 		ipif_refrele(src_ipif);
30260Sstevel@tonic-gate 	return (0);
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate bad:
30290Sstevel@tonic-gate 	if (ip6_asp_table_held)
30300Sstevel@tonic-gate 		ip6_asp_table_refrele();
30310Sstevel@tonic-gate 	/*
30320Sstevel@tonic-gate 	 * We don't have to bother removing from ill groups because
30330Sstevel@tonic-gate 	 *
30340Sstevel@tonic-gate 	 * 1) For groups with names, we insert only when the first ipif
30350Sstevel@tonic-gate 	 *    comes up. In that case if it fails, it will not be in any
30360Sstevel@tonic-gate 	 *    group. So, we need not try to remove for that case.
30370Sstevel@tonic-gate 	 *
30380Sstevel@tonic-gate 	 * 2) For groups without names, either we tried to insert ipif_ill
30390Sstevel@tonic-gate 	 *    in a group as singleton or found some other group to become
30400Sstevel@tonic-gate 	 *    a bigger group. For the former, if it fails we don't have
30410Sstevel@tonic-gate 	 *    anything to do as ipif_ill is not in the group and for the
30420Sstevel@tonic-gate 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
30430Sstevel@tonic-gate 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
30440Sstevel@tonic-gate 	 */
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 	while (irep > ire_array) {
30470Sstevel@tonic-gate 		irep--;
30480Sstevel@tonic-gate 		if (*irep != NULL) {
30490Sstevel@tonic-gate 			ire_delete(*irep);
30500Sstevel@tonic-gate 			if (ire_added)
30510Sstevel@tonic-gate 				ire_refrele(*irep);
30520Sstevel@tonic-gate 		}
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
30580Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
30590Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
30600Sstevel@tonic-gate 	}
30610Sstevel@tonic-gate 	if (src_ipif_held)
30620Sstevel@tonic-gate 		ipif_refrele(src_ipif);
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	ipif_ndp_down(ipif);
30650Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
30660Sstevel@tonic-gate 		ipif_arp_down(ipif);
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 	return (err);
30690Sstevel@tonic-gate }
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate /*
30720Sstevel@tonic-gate  * Delete an ND entry and the corresponding IRE_CACHE entry if it exists.
30730Sstevel@tonic-gate  */
30740Sstevel@tonic-gate /* ARGSUSED */
30750Sstevel@tonic-gate int
30760Sstevel@tonic-gate ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
30770Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
30780Sstevel@tonic-gate {
30790Sstevel@tonic-gate 	in6_addr_t	addr;
30800Sstevel@tonic-gate 	sin6_t		*sin6;
30810Sstevel@tonic-gate 	nce_t		*nce;
30820Sstevel@tonic-gate 	struct lifreq	*lifr;
30830Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
30840Sstevel@tonic-gate 	mblk_t	*mp1;
30850Sstevel@tonic-gate 
30860Sstevel@tonic-gate 	mp1 = mp->b_cont->b_cont;
30870Sstevel@tonic-gate 	lifr = (struct lifreq *)mp1->b_rptr;
30880Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
30890Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
30900Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
30910Sstevel@tonic-gate 		return (EINVAL);
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
30940Sstevel@tonic-gate 		return (EINVAL);
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
30970Sstevel@tonic-gate 		return (EAFNOSUPPORT);
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	sin6 = (sin6_t *)&lnr->lnr_addr;
31000Sstevel@tonic-gate 	addr = sin6->sin6_addr;
31012535Ssangeeta 	nce = ndp_lookup_v6(ipif->ipif_ill, &addr, B_FALSE);
31020Sstevel@tonic-gate 	if (nce == NULL)
31030Sstevel@tonic-gate 		return (ESRCH);
31040Sstevel@tonic-gate 	ndp_delete(nce);
31050Sstevel@tonic-gate 	NCE_REFRELE(nce);
31060Sstevel@tonic-gate 	return (0);
31070Sstevel@tonic-gate }
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate /*
31100Sstevel@tonic-gate  * Return nbr cache info.
31110Sstevel@tonic-gate  */
31120Sstevel@tonic-gate /* ARGSUSED */
31130Sstevel@tonic-gate int
31140Sstevel@tonic-gate ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
31150Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
31160Sstevel@tonic-gate {
31170Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
31180Sstevel@tonic-gate 	struct lifreq	*lifr;
31190Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
31220Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
31230Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
31240Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
31250Sstevel@tonic-gate 		return (EINVAL);
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
31280Sstevel@tonic-gate 		return (EINVAL);
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
31310Sstevel@tonic-gate 		return (EAFNOSUPPORT);
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
31340Sstevel@tonic-gate 		return (EINVAL);
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	return (ndp_query(ill, lnr));
31370Sstevel@tonic-gate }
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate /*
31400Sstevel@tonic-gate  * Perform an update of the nd entry for the specified address.
31410Sstevel@tonic-gate  */
31420Sstevel@tonic-gate /* ARGSUSED */
31430Sstevel@tonic-gate int
31440Sstevel@tonic-gate ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
31450Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
31460Sstevel@tonic-gate {
31470Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
31480Sstevel@tonic-gate 	struct	lifreq	*lifr;
31490Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
31520Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
31530Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
31540Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
31550Sstevel@tonic-gate 		return (EINVAL);
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
31580Sstevel@tonic-gate 		return (EINVAL);
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
31610Sstevel@tonic-gate 		return (EAFNOSUPPORT);
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	return (ndp_sioc_update(ill, lnr));
31640Sstevel@tonic-gate }
3165