xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_if.c (revision 4714:0e5ef817bcec)
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 /*
223431Scarlsonj  * Copyright 2007 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>
453448Sdh155122 #include <sys/policy.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/systm.h>
480Sstevel@tonic-gate #include <sys/param.h>
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <sys/isa_defs.h>
510Sstevel@tonic-gate #include <net/if.h>
520Sstevel@tonic-gate #include <net/if_dl.h>
530Sstevel@tonic-gate #include <net/route.h>
540Sstevel@tonic-gate #include <netinet/in.h>
550Sstevel@tonic-gate #include <netinet/igmp_var.h>
560Sstevel@tonic-gate #include <netinet/ip6.h>
570Sstevel@tonic-gate #include <netinet/icmp6.h>
580Sstevel@tonic-gate #include <netinet/in.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <inet/common.h>
610Sstevel@tonic-gate #include <inet/nd.h>
620Sstevel@tonic-gate #include <inet/mib2.h>
630Sstevel@tonic-gate #include <inet/ip.h>
640Sstevel@tonic-gate #include <inet/ip6.h>
650Sstevel@tonic-gate #include <inet/ip_multi.h>
660Sstevel@tonic-gate #include <inet/ip_ire.h>
670Sstevel@tonic-gate #include <inet/ip_rts.h>
680Sstevel@tonic-gate #include <inet/ip_ndp.h>
690Sstevel@tonic-gate #include <inet/ip_if.h>
700Sstevel@tonic-gate #include <inet/ip6_asp.h>
710Sstevel@tonic-gate #include <inet/tun.h>
720Sstevel@tonic-gate #include <inet/ipclassifier.h>
730Sstevel@tonic-gate #include <inet/sctp_ip.h>
740Sstevel@tonic-gate 
751676Sjpk #include <sys/tsol/tndb.h>
761676Sjpk #include <sys/tsol/tnet.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static in6_addr_t	ipv6_ll_template =
790Sstevel@tonic-gate 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static ipif_t *
820Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
833448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
864459Skcpoon  * These two functions, ipif_lookup_group_v6() and ill_lookup_group_v6(),
874459Skcpoon  * are called when an application does not specify an interface to be
884459Skcpoon  * used for multicast traffic.  It calls ire_lookup_multi_v6() to look
894459Skcpoon  * for an interface route for the specified multicast group.  Doing
904459Skcpoon  * this allows the administrator to add prefix routes for multicast to
914459Skcpoon  * indicate which interface to be used for multicast traffic in the above
924459Skcpoon  * scenario.  The route could be for all multicast (ff00::/8), for a single
934459Skcpoon  * multicast group (a /128 route) or anything in between.  If there is no
944459Skcpoon  * such multicast route, we just find any multicast capable interface and
954459Skcpoon  * return it.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate ipif_t *
983448Sdh155122 ipif_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	ire_t	*ire;
1010Sstevel@tonic-gate 	ipif_t	*ipif;
1020Sstevel@tonic-gate 
1033448Sdh155122 	ire = ire_lookup_multi_v6(group, zoneid, ipst);
1044459Skcpoon 	if (ire != NULL) {
1054459Skcpoon 		ipif = ire->ire_ipif;
1064459Skcpoon 		ipif_refhold(ipif);
1074459Skcpoon 		ire_refrele(ire);
1084459Skcpoon 		return (ipif);
1094459Skcpoon 	}
1104459Skcpoon 
1114459Skcpoon 	return (ipif_lookup_multicast(ipst, zoneid, B_TRUE));
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate ill_t *
1153448Sdh155122 ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	ire_t	*ire;
1180Sstevel@tonic-gate 	ill_t	*ill;
1194459Skcpoon 	ipif_t	*ipif;
1200Sstevel@tonic-gate 
1213448Sdh155122 	ire = ire_lookup_multi_v6(group, zoneid, ipst);
1224459Skcpoon 	if (ire != NULL) {
1234459Skcpoon 		ill = ire->ire_ipif->ipif_ill;
1244459Skcpoon 		ill_refhold(ill);
1254459Skcpoon 		ire_refrele(ire);
1264459Skcpoon 		return (ill);
1274459Skcpoon 	}
1284459Skcpoon 
1294459Skcpoon 	ipif = ipif_lookup_multicast(ipst, zoneid, B_TRUE);
1304459Skcpoon 	if (ipif == NULL)
1310Sstevel@tonic-gate 		return (NULL);
1324459Skcpoon 
1334459Skcpoon 	ill = ipif->ipif_ill;
1340Sstevel@tonic-gate 	ill_refhold(ill);
1354459Skcpoon 	ipif_refrele(ipif);
1360Sstevel@tonic-gate 	return (ill);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * Look for an ipif with the specified interface address and destination.
1410Sstevel@tonic-gate  * The destination address is used only for matching point-to-point interfaces.
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate static ipif_t *
1440Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
1453448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	ipif_t	*ipif;
1480Sstevel@tonic-gate 	ill_t	*ill;
1490Sstevel@tonic-gate 	ipsq_t	*ipsq;
1500Sstevel@tonic-gate 	ill_walk_context_t ctx;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (error != NULL)
1530Sstevel@tonic-gate 		*error = 0;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * First match all the point-to-point interfaces
1570Sstevel@tonic-gate 	 * before looking at non-point-to-point interfaces.
1580Sstevel@tonic-gate 	 * This is done to avoid returning non-point-to-point
1590Sstevel@tonic-gate 	 * ipif instead of unnumbered point-to-point ipif.
1600Sstevel@tonic-gate 	 */
1613448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1623448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
1630Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1640Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
1650Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
1662733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
1672733Snordmark 		    ipif = ipif->ipif_next) {
1680Sstevel@tonic-gate 			/* Allow the ipif to be down */
1690Sstevel@tonic-gate 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
1700Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1710Sstevel@tonic-gate 			    if_addr)) &&
1720Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
1730Sstevel@tonic-gate 			    dst))) {
1740Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
1750Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
1760Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1770Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1783448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1790Sstevel@tonic-gate 					return (ipif);
1800Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
1810Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
1820Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
1830Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1843448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1850Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
1864459Skcpoon 					    ill);
1870Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
1880Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1890Sstevel@tonic-gate 					*error = EINPROGRESS;
1900Sstevel@tonic-gate 					return (NULL);
1910Sstevel@tonic-gate 				}
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
1950Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
1960Sstevel@tonic-gate 	}
1973448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
1980Sstevel@tonic-gate 	/* lookup the ipif based on interface address */
1990Sstevel@tonic-gate 	ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, q, mp, func,
2003448Sdh155122 	    error, ipst);
2010Sstevel@tonic-gate 	ASSERT(ipif == NULL || ipif->ipif_isv6);
2020Sstevel@tonic-gate 	return (ipif);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * Look for an ipif with the specified address. For point-point links
2070Sstevel@tonic-gate  * we look for matches on either the destination address and the local
2080Sstevel@tonic-gate  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
2090Sstevel@tonic-gate  * is set.
2100Sstevel@tonic-gate  * Matches on a specific ill if match_ill is set.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate /* ARGSUSED */
2130Sstevel@tonic-gate ipif_t *
2140Sstevel@tonic-gate ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
2153448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	ipif_t	*ipif;
2180Sstevel@tonic-gate 	ill_t	*ill;
2190Sstevel@tonic-gate 	boolean_t  ptp = B_FALSE;
2200Sstevel@tonic-gate 	ipsq_t	*ipsq;
2210Sstevel@tonic-gate 	ill_walk_context_t ctx;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (error != NULL)
2240Sstevel@tonic-gate 		*error = 0;
2250Sstevel@tonic-gate 
2263448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * Repeat twice, first based on local addresses and
2290Sstevel@tonic-gate 	 * next time for pointopoint.
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate repeat:
2323448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
2330Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2340Sstevel@tonic-gate 		if (match_ill != NULL && ill != match_ill) {
2350Sstevel@tonic-gate 			continue;
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
2380Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
2392733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
2402733Snordmark 		    ipif = ipif->ipif_next) {
2411676Sjpk 			if (zoneid != ALL_ZONES &&
2421676Sjpk 			    ipif->ipif_zoneid != zoneid &&
2431676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
2440Sstevel@tonic-gate 				continue;
2450Sstevel@tonic-gate 			/* Allow the ipif to be down */
2460Sstevel@tonic-gate 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
2470Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, addr) &&
2480Sstevel@tonic-gate 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
2490Sstevel@tonic-gate 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
2500Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
2510Sstevel@tonic-gate 			    addr))) {
2520Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
2530Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
2540Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2550Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2563448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2570Sstevel@tonic-gate 					return (ipif);
2580Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
2590Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
2600Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
2610Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2623448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2630Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
2644459Skcpoon 					    ill);
2650Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
2660Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2670Sstevel@tonic-gate 					*error = EINPROGRESS;
2680Sstevel@tonic-gate 					return (NULL);
2690Sstevel@tonic-gate 				}
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2730Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2762733Snordmark 	/* If we already did the ptp case, then we are done */
2770Sstevel@tonic-gate 	if (ptp) {
2783448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
2790Sstevel@tonic-gate 		if (error != NULL)
2800Sstevel@tonic-gate 			*error = ENXIO;
2810Sstevel@tonic-gate 		return (NULL);
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 	ptp = B_TRUE;
2840Sstevel@tonic-gate 	goto repeat;
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2882733Snordmark  * Look for an ipif with the specified address. For point-point links
2892733Snordmark  * we look for matches on either the destination address and the local
2902733Snordmark  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
2912733Snordmark  * is set.
2922733Snordmark  * Matches on a specific ill if match_ill is set.
2932733Snordmark  * Return the zoneid for the ipif. ALL_ZONES if none found.
2942733Snordmark  */
2952733Snordmark zoneid_t
2963448Sdh155122 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill,
2973448Sdh155122     ip_stack_t *ipst)
2982733Snordmark {
2992733Snordmark 	ipif_t	*ipif;
3002733Snordmark 	ill_t	*ill;
3012733Snordmark 	boolean_t  ptp = B_FALSE;
3022733Snordmark 	ill_walk_context_t ctx;
3032733Snordmark 	zoneid_t	zoneid;
3042733Snordmark 
3053448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3062733Snordmark 	/*
3072733Snordmark 	 * Repeat twice, first based on local addresses and
3082733Snordmark 	 * next time for pointopoint.
3092733Snordmark 	 */
3102733Snordmark repeat:
3113448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
3122733Snordmark 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
3132733Snordmark 		if (match_ill != NULL && ill != match_ill) {
3142733Snordmark 			continue;
3152733Snordmark 		}
3162733Snordmark 		mutex_enter(&ill->ill_lock);
3172733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
3182733Snordmark 		    ipif = ipif->ipif_next) {
3192733Snordmark 			/* Allow the ipif to be down */
3202733Snordmark 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
3212733Snordmark 			    &ipif->ipif_v6lcl_addr, addr) &&
3222733Snordmark 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
3232733Snordmark 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
3242733Snordmark 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
3252733Snordmark 			    addr)) &&
3262733Snordmark 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
3272733Snordmark 				zoneid = ipif->ipif_zoneid;
3282733Snordmark 				mutex_exit(&ill->ill_lock);
3293448Sdh155122 				rw_exit(&ipst->ips_ill_g_lock);
3302733Snordmark 				/*
3312733Snordmark 				 * If ipif_zoneid was ALL_ZONES then we have
3322733Snordmark 				 * a trusted extensions shared IP address.
3332733Snordmark 				 * In that case GLOBAL_ZONEID works to send.
3342733Snordmark 				 */
3352733Snordmark 				if (zoneid == ALL_ZONES)
3362733Snordmark 					zoneid = GLOBAL_ZONEID;
3372733Snordmark 				return (zoneid);
3382733Snordmark 			}
3392733Snordmark 		}
3402733Snordmark 		mutex_exit(&ill->ill_lock);
3412733Snordmark 	}
3422733Snordmark 
3432733Snordmark 	/* If we already did the ptp case, then we are done */
3442733Snordmark 	if (ptp) {
3453448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
3462733Snordmark 		return (ALL_ZONES);
3472733Snordmark 	}
3482733Snordmark 	ptp = B_TRUE;
3492733Snordmark 	goto repeat;
3502733Snordmark }
3512733Snordmark 
3522733Snordmark /*
3530Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a local
3540Sstevel@tonic-gate  * interface address.  This is currently only called when an attempt is made
3550Sstevel@tonic-gate  * to set a local address.
3560Sstevel@tonic-gate  *
3570Sstevel@tonic-gate  * Does not allow a v4-mapped address, an address that equals the subnet
3580Sstevel@tonic-gate  * anycast address, ... a multicast address, ...
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate boolean_t
3610Sstevel@tonic-gate ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	in6_addr_t subnet;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
3660Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * Don't allow all zeroes or host part, but allow
3700Sstevel@tonic-gate 	 * all ones netmask.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
3730Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
3740Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
3750Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
3760Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
3770Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr))
3780Sstevel@tonic-gate 		return (B_FALSE);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	return (B_TRUE);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a
3850Sstevel@tonic-gate  * remote/subnet interface address.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate boolean_t
3880Sstevel@tonic-gate ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	in6_addr_t subnet;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
3930Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
3960Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
3970Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
3980Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
3990Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr) ||
4000Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
4010Sstevel@tonic-gate 		return (B_FALSE);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	return (B_TRUE);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
4080Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
4090Sstevel@tonic-gate  * (for link-local destinations and gateways).
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate /* ARGSUSED1 */
4120Sstevel@tonic-gate int
4130Sstevel@tonic-gate ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
4140Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
4151676Sjpk     ipif_t *ipif_arg, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
4163448Sdh155122     struct rtsa_s *sp, ip_stack_t *ipst)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	ire_t	*ire;
4190Sstevel@tonic-gate 	ire_t	*gw_ire = NULL;
4200Sstevel@tonic-gate 	ipif_t	*ipif;
4210Sstevel@tonic-gate 	boolean_t ipif_refheld = B_FALSE;
4220Sstevel@tonic-gate 	uint_t	type;
4230Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
4240Sstevel@tonic-gate 	int	error;
4251676Sjpk 	tsol_gc_t *gc = NULL;
4261676Sjpk 	tsol_gcgrp_t *gcgrp = NULL;
4271676Sjpk 	boolean_t gcgrp_xtraref = B_FALSE;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (ire_arg != NULL)
4300Sstevel@tonic-gate 		*ire_arg = NULL;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * Prevent routes with a zero gateway from being created (since
4340Sstevel@tonic-gate 	 * interfaces can currently be plumbed and brought up with no assigned
4350Sstevel@tonic-gate 	 * address).
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
4380Sstevel@tonic-gate 		return (ENETUNREACH);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
4420Sstevel@tonic-gate 	 * to all ones (regardless if one was supplied).
4430Sstevel@tonic-gate 	 */
4440Sstevel@tonic-gate 	if (flags & RTF_HOST)
4450Sstevel@tonic-gate 		mask = &ipv6_all_ones;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	/*
4480Sstevel@tonic-gate 	 * Get the ipif, if any, corresponding to the gw_addr
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func,
4513448Sdh155122 	    &error, ipst);
4520Sstevel@tonic-gate 	if (ipif != NULL)
4530Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
4540Sstevel@tonic-gate 	else if (error == EINPROGRESS) {
4550Sstevel@tonic-gate 		ip1dbg(("ip_rt_add_v6: null and EINPROGRESS"));
4560Sstevel@tonic-gate 		return (error);
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * GateD will attempt to create routes with a loopback interface
4610Sstevel@tonic-gate 	 * address as the gateway and with RTF_GATEWAY set.  We allow
4620Sstevel@tonic-gate 	 * these routes to be added, but create them as interface routes
4630Sstevel@tonic-gate 	 * since the gateway is an interface address.
4640Sstevel@tonic-gate 	 */
4651822Srk129064 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
4660Sstevel@tonic-gate 		flags &= ~RTF_GATEWAY;
4671822Srk129064 		if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) &&
4681822Srk129064 		    IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) &&
4691822Srk129064 		    IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) {
4701822Srk129064 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
4713448Sdh155122 			    ipif, ALL_ZONES, NULL, match_flags, ipst);
4721822Srk129064 			if (ire != NULL) {
4731822Srk129064 				ire_refrele(ire);
4741822Srk129064 				if (ipif_refheld)
4751822Srk129064 					ipif_refrele(ipif);
4761822Srk129064 				return (EEXIST);
4771822Srk129064 			}
4781822Srk129064 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
4791822Srk129064 			    "for 0x%x\n", (void *)ipif,
4801822Srk129064 			    ipif->ipif_ire_type,
4811822Srk129064 			    ntohl(ipif->ipif_lcl_addr)));
4821822Srk129064 			ire = ire_create_v6(
4831822Srk129064 			    dst_addr,
4841822Srk129064 			    mask,
4851822Srk129064 			    &ipif->ipif_v6src_addr,
4861822Srk129064 			    NULL,
4871822Srk129064 			    &ipif->ipif_mtu,
4881822Srk129064 			    NULL,
4891822Srk129064 			    NULL,
4901822Srk129064 			    NULL,
4911822Srk129064 			    ipif->ipif_net_type,
4921822Srk129064 			    ipif,
4931822Srk129064 			    NULL,
4941822Srk129064 			    0,
4951822Srk129064 			    0,
4961822Srk129064 			    flags,
4971822Srk129064 			    &ire_uinfo_null,
4981822Srk129064 			    NULL,
4993448Sdh155122 			    NULL,
5003448Sdh155122 			    ipst);
5011822Srk129064 			if (ire == NULL) {
5021822Srk129064 				if (ipif_refheld)
5031822Srk129064 					ipif_refrele(ipif);
5041822Srk129064 				return (ENOMEM);
5051822Srk129064 			}
5062535Ssangeeta 			error = ire_add(&ire, q, mp, func, B_FALSE);
5071822Srk129064 			if (error == 0)
5081822Srk129064 				goto save_ire;
5091822Srk129064 			/*
5101822Srk129064 			 * In the result of failure, ire_add() will have already
5111822Srk129064 			 * deleted the ire in question, so there is no need to
5121822Srk129064 			 * do that here.
5131822Srk129064 			 */
5141822Srk129064 			if (ipif_refheld)
5151822Srk129064 				ipif_refrele(ipif);
5161822Srk129064 			return (error);
5171822Srk129064 		}
5181822Srk129064 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5210Sstevel@tonic-gate 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5220Sstevel@tonic-gate 	 * and the gateway address provided is one of the system's interface
5230Sstevel@tonic-gate 	 * addresses.  By using the routing socket interface and supplying an
5240Sstevel@tonic-gate 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5250Sstevel@tonic-gate 	 * specifying an interface route to be created is available which uses
5260Sstevel@tonic-gate 	 * the interface index that specifies the outgoing interface rather than
5270Sstevel@tonic-gate 	 * the address of an outgoing interface (which may not be able to
5280Sstevel@tonic-gate 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5290Sstevel@tonic-gate 	 * flag, routes can be specified which not only specify the next-hop to
5300Sstevel@tonic-gate 	 * be used when routing to a certain prefix, but also which outgoing
5310Sstevel@tonic-gate 	 * interface should be used.
5320Sstevel@tonic-gate 	 *
5330Sstevel@tonic-gate 	 * Previously, interfaces would have unique addresses assigned to them
5340Sstevel@tonic-gate 	 * and so the address assigned to a particular interface could be used
5350Sstevel@tonic-gate 	 * to identify a particular interface.  One exception to this was the
5360Sstevel@tonic-gate 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5370Sstevel@tonic-gate 	 *
5380Sstevel@tonic-gate 	 * With the advent of IPv6 and its link-local addresses, this
5390Sstevel@tonic-gate 	 * restriction was relaxed and interfaces could share addresses between
5400Sstevel@tonic-gate 	 * themselves.  In fact, typically all of the link-local interfaces on
5410Sstevel@tonic-gate 	 * an IPv6 node or router will have the same link-local address.  In
5420Sstevel@tonic-gate 	 * order to differentiate between these interfaces, the use of an
5430Sstevel@tonic-gate 	 * interface index is necessary and this index can be carried inside a
5440Sstevel@tonic-gate 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5450Sstevel@tonic-gate 	 * of using the interface index, however, is that all of the ipif's that
5460Sstevel@tonic-gate 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5470Sstevel@tonic-gate 	 * cannot be used to differentiate between ipif's (or logical
5480Sstevel@tonic-gate 	 * interfaces) that belong to the same ill (physical interface).
5490Sstevel@tonic-gate 	 *
5500Sstevel@tonic-gate 	 * For example, in the following case involving IPv4 interfaces and
5510Sstevel@tonic-gate 	 * logical interfaces
5520Sstevel@tonic-gate 	 *
5530Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5540Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
5550Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
5560Sstevel@tonic-gate 	 *
5570Sstevel@tonic-gate 	 * the ipif's corresponding to each of these interface routes can be
5580Sstevel@tonic-gate 	 * uniquely identified by the "gateway" (actually interface address).
5590Sstevel@tonic-gate 	 *
5600Sstevel@tonic-gate 	 * In this case involving multiple IPv6 default routes to a particular
5610Sstevel@tonic-gate 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5620Sstevel@tonic-gate 	 * default route is of interest:
5630Sstevel@tonic-gate 	 *
5640Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5650Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/* RTF_GATEWAY not set */
5690Sstevel@tonic-gate 	if (!(flags & RTF_GATEWAY)) {
5700Sstevel@tonic-gate 		queue_t	*stq;
5710Sstevel@tonic-gate 
5721676Sjpk 		if (sp != NULL) {
5731676Sjpk 			ip2dbg(("ip_rt_add_v6: gateway security attributes "
5741676Sjpk 			    "cannot be set with interface route\n"));
5751676Sjpk 			if (ipif_refheld)
5761676Sjpk 				ipif_refrele(ipif);
5771676Sjpk 			return (EINVAL);
5781676Sjpk 		}
5791676Sjpk 
5800Sstevel@tonic-gate 		/*
5810Sstevel@tonic-gate 		 * As the interface index specified with the RTA_IFP sockaddr is
5820Sstevel@tonic-gate 		 * the same for all ipif's off of an ill, the matching logic
5830Sstevel@tonic-gate 		 * below uses MATCH_IRE_ILL if such an index was specified.
5840Sstevel@tonic-gate 		 * This means that routes sharing the same prefix when added
5850Sstevel@tonic-gate 		 * using a RTA_IFP sockaddr must have distinct interface
5860Sstevel@tonic-gate 		 * indices (namely, they must be on distinct ill's).
5870Sstevel@tonic-gate 		 *
5880Sstevel@tonic-gate 		 * On the other hand, since the gateway address will usually be
5890Sstevel@tonic-gate 		 * different for each ipif on the system, the matching logic
5900Sstevel@tonic-gate 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
5910Sstevel@tonic-gate 		 * route.  This means that interface routes for the same prefix
5920Sstevel@tonic-gate 		 * can be created if they belong to distinct ipif's and if a
5930Sstevel@tonic-gate 		 * RTA_IFP sockaddr is not present.
5940Sstevel@tonic-gate 		 */
5950Sstevel@tonic-gate 		if (ipif_arg != NULL) {
5960Sstevel@tonic-gate 			if (ipif_refheld) {
5970Sstevel@tonic-gate 				ipif_refrele(ipif);
5980Sstevel@tonic-gate 				ipif_refheld = B_FALSE;
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 			ipif = ipif_arg;
6010Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
6020Sstevel@tonic-gate 		} else {
6030Sstevel@tonic-gate 			/*
6040Sstevel@tonic-gate 			 * Check the ipif corresponding to the gw_addr
6050Sstevel@tonic-gate 			 */
6060Sstevel@tonic-gate 			if (ipif == NULL)
6070Sstevel@tonic-gate 				return (ENETUNREACH);
6080Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		ASSERT(ipif != NULL);
6120Sstevel@tonic-gate 		/*
6130Sstevel@tonic-gate 		 * We check for an existing entry at this point.
6140Sstevel@tonic-gate 		 */
6150Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
6160Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, 0, IRE_INTERFACE,
6173448Sdh155122 		    ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6180Sstevel@tonic-gate 		if (ire != NULL) {
6190Sstevel@tonic-gate 			ire_refrele(ire);
6200Sstevel@tonic-gate 			if (ipif_refheld)
6210Sstevel@tonic-gate 				ipif_refrele(ipif);
6220Sstevel@tonic-gate 			return (EEXIST);
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6260Sstevel@tonic-gate 		    ? ipif->ipif_rq : ipif->ipif_wq;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 		/*
6290Sstevel@tonic-gate 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
6300Sstevel@tonic-gate 		 * IRE_IF_RESOLVER with the modified address and netmask.
6310Sstevel@tonic-gate 		 */
6320Sstevel@tonic-gate 		ire = ire_create_v6(
6330Sstevel@tonic-gate 		    dst_addr,
6340Sstevel@tonic-gate 		    mask,
6350Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr,
6360Sstevel@tonic-gate 		    NULL,
6370Sstevel@tonic-gate 		    &ipif->ipif_mtu,
6380Sstevel@tonic-gate 		    NULL,
6390Sstevel@tonic-gate 		    NULL,
6400Sstevel@tonic-gate 		    stq,
6410Sstevel@tonic-gate 		    ipif->ipif_net_type,
6420Sstevel@tonic-gate 		    ipif,
6430Sstevel@tonic-gate 		    NULL,
6440Sstevel@tonic-gate 		    0,
6450Sstevel@tonic-gate 		    0,
6460Sstevel@tonic-gate 		    flags,
6471676Sjpk 		    &ire_uinfo_null,
6481676Sjpk 		    NULL,
6493448Sdh155122 		    NULL,
6503448Sdh155122 		    ipst);
6510Sstevel@tonic-gate 		if (ire == NULL) {
6520Sstevel@tonic-gate 			if (ipif_refheld)
6530Sstevel@tonic-gate 				ipif_refrele(ipif);
6540Sstevel@tonic-gate 			return (ENOMEM);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		/*
6580Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
6590Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
6600Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
6610Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
6620Sstevel@tonic-gate 		 * when generating aggregate routes.)
6630Sstevel@tonic-gate 		 *
6640Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6650Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
6660Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
6670Sstevel@tonic-gate 		 *
6680Sstevel@tonic-gate 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6690Sstevel@tonic-gate 		 * routine, but rather using ire_create_v6() directly.
6700Sstevel@tonic-gate 		 */
6710Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6720Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
6732535Ssangeeta 		error = ire_add(&ire, q, mp, func, B_FALSE);
6740Sstevel@tonic-gate 		if (error == 0)
6750Sstevel@tonic-gate 			goto save_ire;
6760Sstevel@tonic-gate 		/*
6770Sstevel@tonic-gate 		 * In the result of failure, ire_add() will have already
6780Sstevel@tonic-gate 		 * deleted the ire in question, so there is no need to
6790Sstevel@tonic-gate 		 * do that here.
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		if (ipif_refheld)
6820Sstevel@tonic-gate 			ipif_refrele(ipif);
6830Sstevel@tonic-gate 		return (error);
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 	if (ipif_refheld) {
6860Sstevel@tonic-gate 		ipif_refrele(ipif);
6870Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * Get an interface IRE for the specified gateway.
6920Sstevel@tonic-gate 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6930Sstevel@tonic-gate 	 * gateway, it is currently unreachable and we fail the request
6940Sstevel@tonic-gate 	 * accordingly.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	ipif = ipif_arg;
6970Sstevel@tonic-gate 	if (ipif_arg != NULL)
6980Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
6990Sstevel@tonic-gate 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg,
7003448Sdh155122 	    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7010Sstevel@tonic-gate 	if (gw_ire == NULL)
7020Sstevel@tonic-gate 		return (ENETUNREACH);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	/*
7050Sstevel@tonic-gate 	 * We create one of three types of IREs as a result of this request
7060Sstevel@tonic-gate 	 * based on the netmask.  A netmask of all ones (which is automatically
7070Sstevel@tonic-gate 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7080Sstevel@tonic-gate 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7090Sstevel@tonic-gate 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7100Sstevel@tonic-gate 	 * destination prefix.
7110Sstevel@tonic-gate 	 */
7120Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
7130Sstevel@tonic-gate 		type = IRE_HOST;
7140Sstevel@tonic-gate 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
7150Sstevel@tonic-gate 		type = IRE_DEFAULT;
7160Sstevel@tonic-gate 	else
7170Sstevel@tonic-gate 		type = IRE_PREFIX;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/* check for a duplicate entry */
7200Sstevel@tonic-gate 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ipif_arg,
7211676Sjpk 	    NULL, ALL_ZONES, 0, NULL,
7223448Sdh155122 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7230Sstevel@tonic-gate 	if (ire != NULL) {
7240Sstevel@tonic-gate 		ire_refrele(gw_ire);
7250Sstevel@tonic-gate 		ire_refrele(ire);
7260Sstevel@tonic-gate 		return (EEXIST);
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 
7291676Sjpk 	/* Security attribute exists */
7301676Sjpk 	if (sp != NULL) {
7311676Sjpk 		tsol_gcgrp_addr_t ga;
7321676Sjpk 
7331676Sjpk 		/* find or create the gateway credentials group */
7341676Sjpk 		ga.ga_af = AF_INET6;
7351676Sjpk 		ga.ga_addr = *gw_addr;
7361676Sjpk 
7371676Sjpk 		/* we hold reference to it upon success */
7381676Sjpk 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7391676Sjpk 		if (gcgrp == NULL) {
7401676Sjpk 			ire_refrele(gw_ire);
7411676Sjpk 			return (ENOMEM);
7421676Sjpk 		}
7431676Sjpk 
7441676Sjpk 		/*
7451676Sjpk 		 * Create and add the security attribute to the group; a
7461676Sjpk 		 * reference to the group is made upon allocating a new
7471676Sjpk 		 * entry successfully.  If it finds an already-existing
7481676Sjpk 		 * entry for the security attribute in the group, it simply
7491676Sjpk 		 * returns it and no new reference is made to the group.
7501676Sjpk 		 */
7511676Sjpk 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7521676Sjpk 		if (gc == NULL) {
7531676Sjpk 			/* release reference held by gcgrp_lookup */
7541676Sjpk 			GCGRP_REFRELE(gcgrp);
7551676Sjpk 			ire_refrele(gw_ire);
7561676Sjpk 			return (ENOMEM);
7571676Sjpk 		}
7581676Sjpk 	}
7591676Sjpk 
7600Sstevel@tonic-gate 	/* Create the IRE. */
7610Sstevel@tonic-gate 	ire = ire_create_v6(
7620Sstevel@tonic-gate 	    dst_addr,				/* dest address */
7630Sstevel@tonic-gate 	    mask,				/* mask */
7640Sstevel@tonic-gate 	    /* src address assigned by the caller? */
7650Sstevel@tonic-gate 	    (((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) ?
7664459Skcpoon 	    src_addr : NULL),
7670Sstevel@tonic-gate 	    gw_addr,				/* gateway address */
7680Sstevel@tonic-gate 	    &gw_ire->ire_max_frag,
769*4714Ssowmini 	    NULL,				/* no src nce */
7700Sstevel@tonic-gate 	    NULL,				/* no recv-from queue */
7710Sstevel@tonic-gate 	    NULL,				/* no send-to queue */
7720Sstevel@tonic-gate 	    (ushort_t)type,			/* IRE type */
7730Sstevel@tonic-gate 	    ipif_arg,
7740Sstevel@tonic-gate 	    NULL,
7750Sstevel@tonic-gate 	    0,
7760Sstevel@tonic-gate 	    0,
7770Sstevel@tonic-gate 	    flags,
7781676Sjpk 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7791676Sjpk 	    gc,					/* security attribute */
7803448Sdh155122 	    NULL,
7813448Sdh155122 	    ipst);
7823448Sdh155122 
7831676Sjpk 	/*
7841676Sjpk 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7851676Sjpk 	 * reference to the 'gcgrp'. We can now release the extra reference
7861676Sjpk 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7871676Sjpk 	 */
7881676Sjpk 	if (gcgrp_xtraref)
7891676Sjpk 		GCGRP_REFRELE(gcgrp);
7900Sstevel@tonic-gate 	if (ire == NULL) {
7911676Sjpk 		if (gc != NULL)
7921676Sjpk 			GC_REFRELE(gc);
7930Sstevel@tonic-gate 		ire_refrele(gw_ire);
7940Sstevel@tonic-gate 		return (ENOMEM);
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/*
7980Sstevel@tonic-gate 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7990Sstevel@tonic-gate 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	/* Add the new IRE. */
8032535Ssangeeta 	error = ire_add(&ire, q, mp, func, B_FALSE);
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * In the result of failure, ire_add() will have already
8060Sstevel@tonic-gate 	 * deleted the ire in question, so there is no need to
8070Sstevel@tonic-gate 	 * do that here.
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	if (error != 0) {
8100Sstevel@tonic-gate 		ire_refrele(gw_ire);
8110Sstevel@tonic-gate 		return (error);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (flags & RTF_MULTIRT) {
8150Sstevel@tonic-gate 		/*
8160Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
8170Sstevel@tonic-gate 		 * to add the dst address in the filtering database.
8180Sstevel@tonic-gate 		 * Replicated inbound packets coming from that address
8190Sstevel@tonic-gate 		 * will be filtered to discard the duplicates.
8200Sstevel@tonic-gate 		 * It is not necessary to call the CGTP filter hook
8210Sstevel@tonic-gate 		 * when the dst address is a multicast, because an
8220Sstevel@tonic-gate 		 * IP source address cannot be a multicast.
8230Sstevel@tonic-gate 		 */
8240Sstevel@tonic-gate 		if ((ip_cgtp_filter_ops != NULL) &&
8253448Sdh155122 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID &&
8260Sstevel@tonic-gate 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
8270Sstevel@tonic-gate 			int res = ip_cgtp_filter_ops->cfo_add_dest_v6(
8280Sstevel@tonic-gate 			    &ire->ire_addr_v6,
8290Sstevel@tonic-gate 			    &ire->ire_gateway_addr_v6,
8300Sstevel@tonic-gate 			    &ire->ire_src_addr_v6,
8310Sstevel@tonic-gate 			    &gw_ire->ire_src_addr_v6);
8320Sstevel@tonic-gate 			if (res != 0) {
8330Sstevel@tonic-gate 				ire_refrele(gw_ire);
8340Sstevel@tonic-gate 				ire_delete(ire);
8350Sstevel@tonic-gate 				return (res);
8360Sstevel@tonic-gate 			}
8370Sstevel@tonic-gate 		}
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8401676Sjpk 	/*
8411676Sjpk 	 * Now that the prefix IRE entry has been created, delete any
8421676Sjpk 	 * existing gateway IRE cache entries as well as any IRE caches
8431676Sjpk 	 * using the gateway, and force them to be created through
8441676Sjpk 	 * ip_newroute_v6.
8451676Sjpk 	 */
8461676Sjpk 	if (gc != NULL) {
8471676Sjpk 		ASSERT(gcgrp != NULL);
8483448Sdh155122 		ire_clookup_delete_cache_gw_v6(gw_addr, ALL_ZONES, ipst);
8491676Sjpk 	}
8501676Sjpk 
8510Sstevel@tonic-gate save_ire:
8520Sstevel@tonic-gate 	if (gw_ire != NULL) {
8530Sstevel@tonic-gate 		ire_refrele(gw_ire);
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate 	if (ipif != NULL) {
8560Sstevel@tonic-gate 		mblk_t	*save_mp;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		/*
8590Sstevel@tonic-gate 		 * Save enough information so that we can recreate the IRE if
8600Sstevel@tonic-gate 		 * the interface goes down and then up.  The metrics associated
8610Sstevel@tonic-gate 		 * with the route will be saved as well when rts_setmetrics() is
8620Sstevel@tonic-gate 		 * called after the IRE has been created.  In the case where
8630Sstevel@tonic-gate 		 * memory cannot be allocated, none of this information will be
8640Sstevel@tonic-gate 		 * saved.
8650Sstevel@tonic-gate 		 */
8660Sstevel@tonic-gate 		save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
8670Sstevel@tonic-gate 		if (save_mp != NULL) {
8680Sstevel@tonic-gate 			ifrt_t	*ifrt;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 			save_mp->b_wptr += sizeof (ifrt_t);
8710Sstevel@tonic-gate 			ifrt = (ifrt_t *)save_mp->b_rptr;
8720Sstevel@tonic-gate 			bzero(ifrt, sizeof (ifrt_t));
8730Sstevel@tonic-gate 			ifrt->ifrt_type = ire->ire_type;
8740Sstevel@tonic-gate 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
8750Sstevel@tonic-gate 			mutex_enter(&ire->ire_lock);
8760Sstevel@tonic-gate 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
8770Sstevel@tonic-gate 			ifrt->ifrt_v6src_addr = ire->ire_src_addr_v6;
8780Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
8790Sstevel@tonic-gate 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
8800Sstevel@tonic-gate 			ifrt->ifrt_flags = ire->ire_flags;
8810Sstevel@tonic-gate 			ifrt->ifrt_max_frag = ire->ire_max_frag;
8820Sstevel@tonic-gate 			mutex_enter(&ipif->ipif_saved_ire_lock);
8830Sstevel@tonic-gate 			save_mp->b_cont = ipif->ipif_saved_ire_mp;
8840Sstevel@tonic-gate 			ipif->ipif_saved_ire_mp = save_mp;
8850Sstevel@tonic-gate 			ipif->ipif_saved_ire_cnt++;
8860Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 	if (ire_arg != NULL) {
8900Sstevel@tonic-gate 		/*
8910Sstevel@tonic-gate 		 * Store the ire that was successfully added into where ire_arg
8920Sstevel@tonic-gate 		 * points to so that callers don't have to look it up
8930Sstevel@tonic-gate 		 * themselves (but they are responsible for ire_refrele()ing
8940Sstevel@tonic-gate 		 * the ire when they are finished with it).
8950Sstevel@tonic-gate 		 */
8960Sstevel@tonic-gate 		*ire_arg = ire;
8970Sstevel@tonic-gate 	} else {
8980Sstevel@tonic-gate 		ire_refrele(ire);		/* Held in ire_add */
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 	if (ipif_refheld)
9010Sstevel@tonic-gate 		ipif_refrele(ipif);
9020Sstevel@tonic-gate 	return (0);
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate /*
9060Sstevel@tonic-gate  * ip_rt_delete_v6 is called to delete an IPv6 route.
9070Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
9080Sstevel@tonic-gate  * (for link-local destinations and gateways).
9090Sstevel@tonic-gate  */
9100Sstevel@tonic-gate /* ARGSUSED4 */
9110Sstevel@tonic-gate int
9120Sstevel@tonic-gate ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
9130Sstevel@tonic-gate     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ipif_t *ipif_arg,
9143448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	ire_t	*ire = NULL;
9170Sstevel@tonic-gate 	ipif_t	*ipif;
9180Sstevel@tonic-gate 	uint_t	type;
9190Sstevel@tonic-gate 	uint_t	match_flags = MATCH_IRE_TYPE;
9200Sstevel@tonic-gate 	int	err = 0;
9210Sstevel@tonic-gate 	boolean_t	ipif_refheld = B_FALSE;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
9250Sstevel@tonic-gate 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
9260Sstevel@tonic-gate 	 */
9270Sstevel@tonic-gate 	if (flags & RTF_HOST) {
9280Sstevel@tonic-gate 		mask = &ipv6_all_ones;
9290Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
9300Sstevel@tonic-gate 	} else if (rtm_addrs & RTA_NETMASK) {
9310Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * Note that RTF_GATEWAY is never set on a delete, therefore
9360Sstevel@tonic-gate 	 * we check if the gateway address is one of our interfaces first,
9370Sstevel@tonic-gate 	 * and fall back on RTF_GATEWAY routes.
9380Sstevel@tonic-gate 	 *
9390Sstevel@tonic-gate 	 * This makes it possible to delete an original
9400Sstevel@tonic-gate 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
9410Sstevel@tonic-gate 	 *
9420Sstevel@tonic-gate 	 * As the interface index specified with the RTA_IFP sockaddr is the
9430Sstevel@tonic-gate 	 * same for all ipif's off of an ill, the matching logic below uses
9440Sstevel@tonic-gate 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
9450Sstevel@tonic-gate 	 * sharing the same prefix and interface index as the the route
9460Sstevel@tonic-gate 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
9470Sstevel@tonic-gate 	 * is specified in the request.
9480Sstevel@tonic-gate 	 *
9490Sstevel@tonic-gate 	 * On the other hand, since the gateway address will usually be
9500Sstevel@tonic-gate 	 * different for each ipif on the system, the matching logic
9510Sstevel@tonic-gate 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
9520Sstevel@tonic-gate 	 * route.  This means that interface routes for the same prefix can be
9530Sstevel@tonic-gate 	 * uniquely identified if they belong to distinct ipif's and if a
9540Sstevel@tonic-gate 	 * RTA_IFP sockaddr is not present.
9550Sstevel@tonic-gate 	 *
9560Sstevel@tonic-gate 	 * For more detail on specifying routes by gateway address and by
9570Sstevel@tonic-gate 	 * interface index, see the comments in ip_rt_add_v6().
9580Sstevel@tonic-gate 	 */
9593448Sdh155122 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, &err,
9603448Sdh155122 	    ipst);
9610Sstevel@tonic-gate 	if (ipif != NULL) {
9620Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
9630Sstevel@tonic-gate 		if (ipif_arg != NULL) {
9640Sstevel@tonic-gate 			ipif_refrele(ipif);
9650Sstevel@tonic-gate 			ipif_refheld = B_FALSE;
9660Sstevel@tonic-gate 			ipif = ipif_arg;
9670Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
9680Sstevel@tonic-gate 		} else {
9690Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		if (ipif->ipif_ire_type == IRE_LOOPBACK)
9730Sstevel@tonic-gate 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
9743448Sdh155122 			    ipif, ALL_ZONES, NULL, match_flags, ipst);
9750Sstevel@tonic-gate 		if (ire == NULL)
9760Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, 0,
9771676Sjpk 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
9783448Sdh155122 			    match_flags, ipst);
9790Sstevel@tonic-gate 	} else if (err == EINPROGRESS) {
9800Sstevel@tonic-gate 		return (err);
9810Sstevel@tonic-gate 	} else {
9820Sstevel@tonic-gate 		err = 0;
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 	if (ire == NULL) {
9850Sstevel@tonic-gate 		/*
9860Sstevel@tonic-gate 		 * At this point, the gateway address is not one of our own
9870Sstevel@tonic-gate 		 * addresses or a matching interface route was not found.  We
9880Sstevel@tonic-gate 		 * set the IRE type to lookup based on whether
9890Sstevel@tonic-gate 		 * this is a host route, a default route or just a prefix.
9900Sstevel@tonic-gate 		 *
9910Sstevel@tonic-gate 		 * If an ipif_arg was passed in, then the lookup is based on an
9920Sstevel@tonic-gate 		 * interface index so MATCH_IRE_ILL is added to match_flags.
9930Sstevel@tonic-gate 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
9940Sstevel@tonic-gate 		 * set as the route being looked up is not a traditional
9950Sstevel@tonic-gate 		 * interface route.
9960Sstevel@tonic-gate 		 */
9970Sstevel@tonic-gate 		match_flags &= ~MATCH_IRE_IPIF;
9980Sstevel@tonic-gate 		match_flags |= MATCH_IRE_GW;
9990Sstevel@tonic-gate 		if (ipif_arg != NULL)
10000Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
10010Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
10020Sstevel@tonic-gate 			type = IRE_HOST;
10030Sstevel@tonic-gate 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
10040Sstevel@tonic-gate 			type = IRE_DEFAULT;
10050Sstevel@tonic-gate 		else
10060Sstevel@tonic-gate 			type = IRE_PREFIX;
10070Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
10083448Sdh155122 		    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	if (ipif_refheld) {
10120Sstevel@tonic-gate 		ipif_refrele(ipif);
10130Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	if (ire == NULL)
10160Sstevel@tonic-gate 		return (ESRCH);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (ire->ire_flags & RTF_MULTIRT) {
10190Sstevel@tonic-gate 		/*
10200Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
10210Sstevel@tonic-gate 		 * to remove the dst address from the filtering database.
10220Sstevel@tonic-gate 		 * Packets coming from that address will no longer be
10230Sstevel@tonic-gate 		 * filtered to remove duplicates.
10240Sstevel@tonic-gate 		 */
10253448Sdh155122 		if (ip_cgtp_filter_ops != NULL &&
10263448Sdh155122 		    ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) {
10270Sstevel@tonic-gate 			err = ip_cgtp_filter_ops->cfo_del_dest_v6(
10280Sstevel@tonic-gate 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	ipif = ire->ire_ipif;
10330Sstevel@tonic-gate 	if (ipif != NULL) {
10340Sstevel@tonic-gate 		mblk_t		**mpp;
10350Sstevel@tonic-gate 		mblk_t		*mp;
10360Sstevel@tonic-gate 		ifrt_t		*ifrt;
10370Sstevel@tonic-gate 		in6_addr_t	gw_addr_v6;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		/* Remove from ipif_saved_ire_mp list if it is there */
10400Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
10410Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
10420Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
10430Sstevel@tonic-gate 		mutex_enter(&ipif->ipif_saved_ire_lock);
10440Sstevel@tonic-gate 		for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
10450Sstevel@tonic-gate 		    mpp = &(*mpp)->b_cont) {
10460Sstevel@tonic-gate 			/*
10470Sstevel@tonic-gate 			 * On a given ipif, the triple of address, gateway and
10480Sstevel@tonic-gate 			 * mask is unique for each saved IRE (in the case of
10490Sstevel@tonic-gate 			 * ordinary interface routes, the gateway address is
10500Sstevel@tonic-gate 			 * all-zeroes).
10510Sstevel@tonic-gate 			 */
10520Sstevel@tonic-gate 			mp = *mpp;
10530Sstevel@tonic-gate 			ifrt = (ifrt_t *)mp->b_rptr;
10540Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
10550Sstevel@tonic-gate 			    &ire->ire_addr_v6) &&
10560Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
10570Sstevel@tonic-gate 			    &gw_addr_v6) &&
10580Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
10590Sstevel@tonic-gate 			    &ire->ire_mask_v6)) {
10600Sstevel@tonic-gate 				*mpp = mp->b_cont;
10610Sstevel@tonic-gate 				ipif->ipif_saved_ire_cnt--;
10620Sstevel@tonic-gate 				freeb(mp);
10630Sstevel@tonic-gate 				break;
10640Sstevel@tonic-gate 			}
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 	ire_delete(ire);
10690Sstevel@tonic-gate 	ire_refrele(ire);
10700Sstevel@tonic-gate 	return (err);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate  * Derive a token from the link layer address.
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate boolean_t
10770Sstevel@tonic-gate ill_setdefaulttoken(ill_t *ill)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	int 		i;
10800Sstevel@tonic-gate 	in6_addr_t	v6addr, v6mask;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (!MEDIA_V6INTFID(ill->ill_media, ill->ill_phys_addr_length,
10833340Smeem 	    ill->ill_phys_addr, &v6addr))
10840Sstevel@tonic-gate 		return (B_FALSE);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_TOKEN_LEN, &v6mask);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
10890Sstevel@tonic-gate 		v6mask.s6_addr32[i] = v6mask.s6_addr32[i] ^
10900Sstevel@tonic-gate 		    (uint32_t)0xffffffff;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
10930Sstevel@tonic-gate 	ill->ill_token_length = IPV6_TOKEN_LEN;
10940Sstevel@tonic-gate 	return (B_TRUE);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate /*
10980Sstevel@tonic-gate  * Create a link-local address from a token.
10990Sstevel@tonic-gate  */
11000Sstevel@tonic-gate static void
11010Sstevel@tonic-gate ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate 	int i;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
11060Sstevel@tonic-gate 		dest->s6_addr32[i] =
11070Sstevel@tonic-gate 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate /*
11120Sstevel@tonic-gate  * Set a nice default address for either automatic tunnels tsrc/96 or
11130Sstevel@tonic-gate  * 6to4 tunnels 2002:<tsrc>::1/64
11140Sstevel@tonic-gate  */
11150Sstevel@tonic-gate static void
11160Sstevel@tonic-gate ipif_set_tun_auto_addr(ipif_t *ipif, struct iftun_req *ta)
11170Sstevel@tonic-gate {
11180Sstevel@tonic-gate 	sin6_t	sin6;
11190Sstevel@tonic-gate 	sin_t	*sin;
11200Sstevel@tonic-gate 	ill_t 	*ill = ipif->ipif_ill;
11210Sstevel@tonic-gate 	tun_t *tp = (tun_t *)ill->ill_wq->q_next->q_ptr;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (ta->ifta_saddr.ss_family != AF_INET ||
11240Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_UP) || !ipif->ipif_isv6 ||
11250Sstevel@tonic-gate 	    (ta->ifta_flags & IFTUN_SRC) == 0)
11260Sstevel@tonic-gate 		return;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * Check the tunnel type by examining q_next->q_ptr
11300Sstevel@tonic-gate 	 */
11310Sstevel@tonic-gate 	if (tp->tun_flags & TUN_AUTOMATIC) {
11320Sstevel@tonic-gate 		/* this is an automatic tunnel */
11330Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_ABITS - IP_ABITS,
11340Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
11350Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
11360Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
11370Sstevel@tonic-gate 		V4_PART_OF_V6(sin6.sin6_addr) = sin->sin_addr.s_addr;
11380Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
11390Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
11400Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
11410Sstevel@tonic-gate 	} else if (tp->tun_flags & TUN_6TO4) {
11420Sstevel@tonic-gate 		/* this is a 6to4 tunnel */
11430Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_PREFIX_LEN,
11440Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
11450Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
11460Sstevel@tonic-gate 		/* create a 6to4 address from the IPv4 tsrc */
11470Sstevel@tonic-gate 		IN6_V4ADDR_TO_6TO4(&sin->sin_addr, &sin6.sin6_addr);
11480Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
11490Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
11500Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
11510Sstevel@tonic-gate 	} else {
11520Sstevel@tonic-gate 		ip1dbg(("ipif_set_tun_auto_addr: Unknown tunnel type"));
11530Sstevel@tonic-gate 		return;
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate /*
11580Sstevel@tonic-gate  * Set link local for ipif_id 0 of a configured tunnel based on the
11590Sstevel@tonic-gate  * tsrc or tdst parameter
11600Sstevel@tonic-gate  * For tunnels over IPv4 use the IPv4 address prepended with 32 zeros as
11610Sstevel@tonic-gate  * the token.
11620Sstevel@tonic-gate  * For tunnels over IPv6 use the low-order 64 bits of the "inner" IPv6 address
11630Sstevel@tonic-gate  * as the token for the "outer" link.
11640Sstevel@tonic-gate  */
11650Sstevel@tonic-gate void
11660Sstevel@tonic-gate ipif_set_tun_llink(ill_t *ill, struct iftun_req *ta)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	ipif_t		*ipif;
11690Sstevel@tonic-gate 	sin_t		*sin;
11700Sstevel@tonic-gate 	in6_addr_t	*s6addr;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	/* The first ipif must be id zero. */
11750Sstevel@tonic-gate 	ipif = ill->ill_ipif;
11760Sstevel@tonic-gate 	ASSERT(ipif->ipif_id == 0);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/* no link local for automatic tunnels */
11790Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_POINTOPOINT)) {
11800Sstevel@tonic-gate 		ipif_set_tun_auto_addr(ipif, ta);
11810Sstevel@tonic-gate 		return;
11820Sstevel@tonic-gate 	}
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_DST) &&
11850Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
11860Sstevel@tonic-gate 		sin6_t	sin6;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
11890Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
11900Sstevel@tonic-gate 		if ((ta->ifta_saddr.ss_family == AF_INET)) {
11910Sstevel@tonic-gate 			sin = (sin_t *)&ta->ifta_daddr;
11920Sstevel@tonic-gate 			V4_PART_OF_V6(sin6.sin6_addr) =
11930Sstevel@tonic-gate 			    sin->sin_addr.s_addr;
11940Sstevel@tonic-gate 		} else {
11950Sstevel@tonic-gate 			s6addr =
11960Sstevel@tonic-gate 			    &((sin6_t *)&ta->ifta_daddr)->sin6_addr;
11970Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[3] = s6addr->s6_addr32[3];
11980Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[2] = s6addr->s6_addr32[2];
11990Sstevel@tonic-gate 		}
12000Sstevel@tonic-gate 		ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr,
12010Sstevel@tonic-gate 		    &sin6.sin6_addr);
12020Sstevel@tonic-gate 		ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_SRC)) {
12050Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 		/* Set the token if it isn't already set */
12080Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) {
12090Sstevel@tonic-gate 			if ((ta->ifta_saddr.ss_family == AF_INET)) {
12100Sstevel@tonic-gate 				sin = (sin_t *)&ta->ifta_saddr;
12110Sstevel@tonic-gate 				V4_PART_OF_V6(ill->ill_token) =
12120Sstevel@tonic-gate 				    sin->sin_addr.s_addr;
12130Sstevel@tonic-gate 			} else {
12140Sstevel@tonic-gate 				s6addr =
12150Sstevel@tonic-gate 				    &((sin6_t *)&ta->ifta_saddr)->sin6_addr;
12160Sstevel@tonic-gate 				ill->ill_token.s6_addr32[3] =
12170Sstevel@tonic-gate 				    s6addr->s6_addr32[3];
12180Sstevel@tonic-gate 				ill->ill_token.s6_addr32[2] =
12190Sstevel@tonic-gate 				    s6addr->s6_addr32[2];
12200Sstevel@tonic-gate 			}
12210Sstevel@tonic-gate 			ill->ill_token_length = IPV6_TOKEN_LEN;
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 		/*
12240Sstevel@tonic-gate 		 * Attempt to set the link local address if it isn't set.
12250Sstevel@tonic-gate 		 */
12260Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
12270Sstevel@tonic-gate 			(void) ipif_setlinklocal(ipif);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate  * Is it not possible to set the link local address?
12330Sstevel@tonic-gate  * The address can be set if the token is set, and the token
12340Sstevel@tonic-gate  * isn't too long.
12350Sstevel@tonic-gate  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
12360Sstevel@tonic-gate  */
12370Sstevel@tonic-gate boolean_t
12380Sstevel@tonic-gate ipif_cant_setlinklocal(ipif_t *ipif)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
12430Sstevel@tonic-gate 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
12440Sstevel@tonic-gate 		return (B_TRUE);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	return (B_FALSE);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate /*
12500Sstevel@tonic-gate  * Generate a link-local address from the token.
12510Sstevel@tonic-gate  * Return zero if the address was set, or non-zero if it couldn't be set.
12520Sstevel@tonic-gate  */
12530Sstevel@tonic-gate int
12540Sstevel@tonic-gate ipif_setlinklocal(ipif_t *ipif)
12550Sstevel@tonic-gate {
12563706Svi117747 	ill_t		*ill = ipif->ipif_ill;
12573706Svi117747 	in6_addr_t	ov6addr;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	if (ipif_cant_setlinklocal(ipif))
12620Sstevel@tonic-gate 		return (-1);
12630Sstevel@tonic-gate 
12643706Svi117747 	ov6addr = ipif->ipif_v6lcl_addr;
12650Sstevel@tonic-gate 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
12663706Svi117747 	sctp_update_ipif_addr(ipif, ov6addr);
12670Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
12680Sstevel@tonic-gate 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12690Sstevel@tonic-gate 	    ipif->ipif_v6subnet);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if (ipif->ipif_flags & IPIF_NOLOCAL) {
12720Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12730Sstevel@tonic-gate 	} else {
12740Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate 	return (0);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate  * This function sets up the multicast mappings in NDP.
12810Sstevel@tonic-gate  * Unlike ARP, there are no mapping_mps here. We delete the
12820Sstevel@tonic-gate  * mapping nces and add a new one.
12830Sstevel@tonic-gate  *
12840Sstevel@tonic-gate  * Returns non-zero on error and 0 on success.
12850Sstevel@tonic-gate  */
12860Sstevel@tonic-gate int
12870Sstevel@tonic-gate ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce)
12880Sstevel@tonic-gate {
12890Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
12900Sstevel@tonic-gate 	in6_addr_t	v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0};
12910Sstevel@tonic-gate 	in6_addr_t	v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0};
12920Sstevel@tonic-gate 	in6_addr_t	v6_extract_mask;
12930Sstevel@tonic-gate 	uchar_t		*phys_addr, *bphys_addr, *alloc_phys;
12940Sstevel@tonic-gate 	nce_t		*mnce = NULL;
12950Sstevel@tonic-gate 	int		err = 0;
12960Sstevel@tonic-gate 	phyint_t	*phyi = ill->ill_phyint;
12970Sstevel@tonic-gate 	uint32_t	hw_extract_start;
12980Sstevel@tonic-gate 	dl_unitdata_req_t *dlur;
12993448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	if (ret_nce != NULL)
13020Sstevel@tonic-gate 		*ret_nce = NULL;
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * Delete the mapping nce. Normally these should not exist
13050Sstevel@tonic-gate 	 * as a previous ipif_down -> ipif_ndp_down should have deleted
13060Sstevel@tonic-gate 	 * all the nces. But they can exist if ip_rput_dlpi_writer
13070Sstevel@tonic-gate 	 * calls this when PHYI_MULTI_BCAST is set.
13080Sstevel@tonic-gate 	 */
13092535Ssangeeta 	mnce = ndp_lookup_v6(ill, &v6_mcast_addr, B_FALSE);
13100Sstevel@tonic-gate 	if (mnce != NULL) {
13110Sstevel@tonic-gate 		ndp_delete(mnce);
13120Sstevel@tonic-gate 		NCE_REFRELE(mnce);
13130Sstevel@tonic-gate 		mnce = NULL;
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
13170Sstevel@tonic-gate 	 * Get media specific v6 mapping information. Note that
13180Sstevel@tonic-gate 	 * nd_lla_len can be 0 for tunnels.
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
13210Sstevel@tonic-gate 	if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0))
13220Sstevel@tonic-gate 		return (ENOMEM);
13230Sstevel@tonic-gate 	/*
13240Sstevel@tonic-gate 	 * Determine the broadcast address.
13250Sstevel@tonic-gate 	 */
13260Sstevel@tonic-gate 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13270Sstevel@tonic-gate 	if (ill->ill_sap_length < 0)
13280Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13290Sstevel@tonic-gate 	else
13300Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur +
13310Sstevel@tonic-gate 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	/*
13340Sstevel@tonic-gate 	 * Check PHYI_MULTI_BCAST and possible length of physical
13350Sstevel@tonic-gate 	 * address to determine if we use the mapping or the
13360Sstevel@tonic-gate 	 * broadcast address.
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13390Sstevel@tonic-gate 	    (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len,
13400Sstevel@tonic-gate 	    bphys_addr, alloc_phys, &hw_extract_start,
13410Sstevel@tonic-gate 	    &v6_extract_mask))) {
13420Sstevel@tonic-gate 		if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) {
13430Sstevel@tonic-gate 			kmem_free(alloc_phys, ill->ill_nd_lla_len);
13440Sstevel@tonic-gate 			return (E2BIG);
13450Sstevel@tonic-gate 		}
13460Sstevel@tonic-gate 		/* Use the link-layer broadcast address for MULTI_BCAST */
13470Sstevel@tonic-gate 		phys_addr = bphys_addr;
13480Sstevel@tonic-gate 		bzero(&v6_extract_mask, sizeof (v6_extract_mask));
13490Sstevel@tonic-gate 		hw_extract_start = ill->ill_nd_lla_len;
13500Sstevel@tonic-gate 	} else {
13510Sstevel@tonic-gate 		phys_addr = alloc_phys;
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_BROADCAST) ||
13540Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST) ||
13550Sstevel@tonic-gate 	    (phyi->phyint_flags & PHYI_MULTI_BCAST)) {
13563448Sdh155122 		mutex_enter(&ipst->ips_ndp6->ndp_g_lock);
1357*4714Ssowmini 		err = ndp_add_v6(ill,
13580Sstevel@tonic-gate 		    phys_addr,
13590Sstevel@tonic-gate 		    &v6_mcast_addr,	/* v6 address */
13600Sstevel@tonic-gate 		    &v6_mcast_mask,	/* v6 mask */
13610Sstevel@tonic-gate 		    &v6_extract_mask,
13620Sstevel@tonic-gate 		    hw_extract_start,
13630Sstevel@tonic-gate 		    NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD,
13640Sstevel@tonic-gate 		    ND_REACHABLE,
1365*4714Ssowmini 		    &mnce);
13663448Sdh155122 		mutex_exit(&ipst->ips_ndp6->ndp_g_lock);
13670Sstevel@tonic-gate 		if (err == 0) {
13680Sstevel@tonic-gate 			if (ret_nce != NULL) {
13690Sstevel@tonic-gate 				*ret_nce = mnce;
13700Sstevel@tonic-gate 			} else {
13710Sstevel@tonic-gate 				NCE_REFRELE(mnce);
13720Sstevel@tonic-gate 			}
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 	kmem_free(alloc_phys, ill->ill_nd_lla_len);
13760Sstevel@tonic-gate 	return (err);
13770Sstevel@tonic-gate }
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate /*
13800Sstevel@tonic-gate  * Get the resolver set up for a new interface address.  (Always called
13810Sstevel@tonic-gate  * as writer.)
13820Sstevel@tonic-gate  */
13830Sstevel@tonic-gate int
13843340Smeem ipif_ndp_up(ipif_t *ipif, const in6_addr_t *addr)
13850Sstevel@tonic-gate {
13860Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
13870Sstevel@tonic-gate 	int		err = 0;
13880Sstevel@tonic-gate 	nce_t		*nce = NULL;
13890Sstevel@tonic-gate 	nce_t		*mnce = NULL;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	ip1dbg(("ipif_ndp_up(%s:%u)\n",
13924459Skcpoon 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * ND not supported on XRESOLV interfaces. If ND support (multicast)
13960Sstevel@tonic-gate 	 * added later, take out this check.
13970Sstevel@tonic-gate 	 */
13982546Scarlsonj 	if ((ill->ill_flags & ILLF_XRESOLV) ||
13992546Scarlsonj 	    IN6_IS_ADDR_UNSPECIFIED(addr) ||
14002546Scarlsonj 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
14012546Scarlsonj 		ipif->ipif_addr_ready = 1;
14020Sstevel@tonic-gate 		return (0);
14032546Scarlsonj 	}
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * Need to setup multicast mapping only when the first
14070Sstevel@tonic-gate 	 * interface is coming UP.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	if (ill->ill_ipif_up_count == 0 &&
14100Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST)) {
14110Sstevel@tonic-gate 		/*
14120Sstevel@tonic-gate 		 * We set the multicast before setting up the mapping for
14130Sstevel@tonic-gate 		 * local address because ipif_ndp_setup_multicast does
14140Sstevel@tonic-gate 		 * ndp_walk to delete nces which will delete the mapping
14150Sstevel@tonic-gate 		 * for local address also if we added the mapping for
14160Sstevel@tonic-gate 		 * local address first.
14170Sstevel@tonic-gate 		 */
14180Sstevel@tonic-gate 		err = ipif_ndp_setup_multicast(ipif, &mnce);
14190Sstevel@tonic-gate 		if (err != 0)
14200Sstevel@tonic-gate 			return (err);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
14240Sstevel@tonic-gate 		uint16_t	flags;
14250Sstevel@tonic-gate 		uchar_t	*hw_addr = NULL;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 		/* Permanent entries don't need NUD */
14283340Smeem 		flags = NCE_F_PERMANENT | NCE_F_NONUD;
14290Sstevel@tonic-gate 		if (ill->ill_flags & ILLF_ROUTER)
14300Sstevel@tonic-gate 			flags |= NCE_F_ISROUTER;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_ANYCAST)
14330Sstevel@tonic-gate 			flags |= NCE_F_ANYCAST;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 		if (ill->ill_net_type == IRE_IF_RESOLVER) {
14360Sstevel@tonic-gate 			hw_addr = ill->ill_nd_lla;
14370Sstevel@tonic-gate 
14383340Smeem 			if (ill->ill_move_in_progress) {
14390Sstevel@tonic-gate 				/*
14400Sstevel@tonic-gate 				 * Addresses are failing over to this ill.
14410Sstevel@tonic-gate 				 * Don't wait for NUD to see this change.
14420Sstevel@tonic-gate 				 * Publish our new link-layer address.
14430Sstevel@tonic-gate 				 */
14440Sstevel@tonic-gate 				flags |= NCE_F_UNSOL_ADV;
14450Sstevel@tonic-gate 			}
14460Sstevel@tonic-gate 		}
1447*4714Ssowmini 		err = ndp_lookup_then_add_v6(ill,
14480Sstevel@tonic-gate 		    hw_addr,
14490Sstevel@tonic-gate 		    addr,
14500Sstevel@tonic-gate 		    &ipv6_all_ones,
14510Sstevel@tonic-gate 		    &ipv6_all_zeros,
14520Sstevel@tonic-gate 		    0,
14530Sstevel@tonic-gate 		    flags,
14542546Scarlsonj 		    ND_PROBE,	/* Causes Duplicate Address Detection to run */
1455*4714Ssowmini 		    &nce);
14560Sstevel@tonic-gate 		switch (err) {
14570Sstevel@tonic-gate 		case 0:
14580Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
14590Sstevel@tonic-gate 			    ill->ill_name));
14602546Scarlsonj 			ipif->ipif_addr_ready = 1;
14612546Scarlsonj 			break;
14622546Scarlsonj 		case EINPROGRESS:
14632546Scarlsonj 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
14642546Scarlsonj 			    ill->ill_name));
14650Sstevel@tonic-gate 			break;
14660Sstevel@tonic-gate 		case EEXIST:
14670Sstevel@tonic-gate 			NCE_REFRELE(nce);
14680Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
14690Sstevel@tonic-gate 			    ill->ill_name));
14700Sstevel@tonic-gate 			if (mnce != NULL) {
14710Sstevel@tonic-gate 				ndp_delete(mnce);
14720Sstevel@tonic-gate 				NCE_REFRELE(mnce);
14730Sstevel@tonic-gate 			}
14740Sstevel@tonic-gate 			return (err);
14750Sstevel@tonic-gate 		default:
14760Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE creation failed %s\n",
14770Sstevel@tonic-gate 			    ill->ill_name));
14780Sstevel@tonic-gate 			if (mnce != NULL) {
14790Sstevel@tonic-gate 				ndp_delete(mnce);
14800Sstevel@tonic-gate 				NCE_REFRELE(mnce);
14810Sstevel@tonic-gate 			}
14820Sstevel@tonic-gate 			return (err);
14830Sstevel@tonic-gate 		}
14842546Scarlsonj 	} else {
14852546Scarlsonj 		/* No local NCE for this entry */
14862546Scarlsonj 		ipif->ipif_addr_ready = 1;
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 	if (nce != NULL)
14890Sstevel@tonic-gate 		NCE_REFRELE(nce);
14900Sstevel@tonic-gate 	if (mnce != NULL)
14910Sstevel@tonic-gate 		NCE_REFRELE(mnce);
14920Sstevel@tonic-gate 	return (0);
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate /* Remove all cache entries for this logical interface */
14960Sstevel@tonic-gate void
14970Sstevel@tonic-gate ipif_ndp_down(ipif_t *ipif)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	nce_t	*nce;
15000Sstevel@tonic-gate 
15012535Ssangeeta 	if (ipif->ipif_isv6) {
15022535Ssangeeta 		nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr,
15032535Ssangeeta 		    B_FALSE);
15042535Ssangeeta 		if (nce != NULL) {
15052535Ssangeeta 			ndp_delete(nce);
15062535Ssangeeta 			NCE_REFRELE(nce);
15072535Ssangeeta 		}
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 	/*
15100Sstevel@tonic-gate 	 * Remove mapping and all other nces dependent on this ill
15110Sstevel@tonic-gate 	 * when the last ipif is going away.
15120Sstevel@tonic-gate 	 */
15130Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
15140Sstevel@tonic-gate 		ndp_walk(ipif->ipif_ill, (pfi_t)ndp_delete_per_ill,
15153448Sdh155122 		    (uchar_t *)ipif->ipif_ill, ipif->ipif_ill->ill_ipst);
15160Sstevel@tonic-gate 	}
15170Sstevel@tonic-gate }
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate /*
15200Sstevel@tonic-gate  * Used when an interface comes up to recreate any extra routes on this
15210Sstevel@tonic-gate  * interface.
15220Sstevel@tonic-gate  */
15230Sstevel@tonic-gate static ire_t **
15240Sstevel@tonic-gate ipif_recover_ire_v6(ipif_t *ipif)
15250Sstevel@tonic-gate {
15260Sstevel@tonic-gate 	mblk_t	*mp;
15270Sstevel@tonic-gate 	ire_t   **ipif_saved_irep;
15280Sstevel@tonic-gate 	ire_t   **irep;
15293448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name,
15320Sstevel@tonic-gate 	    ipif->ipif_id));
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_saved_ire_lock);
15370Sstevel@tonic-gate 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
15380Sstevel@tonic-gate 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
15390Sstevel@tonic-gate 	if (ipif_saved_irep == NULL) {
15400Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
15410Sstevel@tonic-gate 		return (NULL);
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	irep = ipif_saved_irep;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
15470Sstevel@tonic-gate 		ire_t		*ire;
15480Sstevel@tonic-gate 		queue_t		*rfq;
15490Sstevel@tonic-gate 		queue_t		*stq;
15500Sstevel@tonic-gate 		ifrt_t		*ifrt;
15510Sstevel@tonic-gate 		in6_addr_t	*src_addr;
15520Sstevel@tonic-gate 		in6_addr_t	*gateway_addr;
15530Sstevel@tonic-gate 		char		buf[INET6_ADDRSTRLEN];
15540Sstevel@tonic-gate 		ushort_t	type;
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 		/*
15570Sstevel@tonic-gate 		 * When the ire was initially created and then added in
15580Sstevel@tonic-gate 		 * ip_rt_add_v6(), it was created either using
15590Sstevel@tonic-gate 		 * ipif->ipif_net_type in the case of a traditional interface
15600Sstevel@tonic-gate 		 * route, or as one of the IRE_OFFSUBNET types (with the
15613004Sdd193516 		 * exception of IRE_HOST type redirect ire which is created by
15620Sstevel@tonic-gate 		 * icmp_redirect_v6() and which we don't need to save or
15630Sstevel@tonic-gate 		 * recover).  In the case where ipif->ipif_net_type was
15640Sstevel@tonic-gate 		 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to
15650Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy
15660Sstevel@tonic-gate 		 * software like GateD and Sun Cluster which creates routes
15670Sstevel@tonic-gate 		 * using the the loopback interface's address as a gateway.
15680Sstevel@tonic-gate 		 *
1569*4714Ssowmini 		 * As ifrt->ifrt_type reflects the already updated ire_type,
1570*4714Ssowmini 		 * ire_create_v6() will be called in the same way here as in
1571*4714Ssowmini 		 * ip_rt_add_v6(), namely using ipif->ipif_net_type when the
1572*4714Ssowmini 		 * route looks like a traditional interface route (where
1573*4714Ssowmini 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise
1574*4714Ssowmini 		 * using the saved ifrt->ifrt_type.  This means that in
1575*4714Ssowmini 		 * the case where ipif->ipif_net_type is IRE_LOOPBACK,
1576*4714Ssowmini 		 * the ire created by ire_create_v6() will be an IRE_LOOPBACK,
1577*4714Ssowmini 		 * it will then be turned into an IRE_IF_NORESOLVER and then
1578*4714Ssowmini 		 * added by ire_add_v6().
15790Sstevel@tonic-gate 		 */
15800Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
15810Sstevel@tonic-gate 		if (ifrt->ifrt_type & IRE_INTERFACE) {
15820Sstevel@tonic-gate 			rfq = NULL;
15830Sstevel@tonic-gate 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
15840Sstevel@tonic-gate 			    ? ipif->ipif_rq : ipif->ipif_wq;
15850Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15860Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr
15870Sstevel@tonic-gate 			    : &ipif->ipif_v6src_addr;
15880Sstevel@tonic-gate 			gateway_addr = NULL;
15890Sstevel@tonic-gate 			type = ipif->ipif_net_type;
15900Sstevel@tonic-gate 		} else {
15910Sstevel@tonic-gate 			rfq = NULL;
15920Sstevel@tonic-gate 			stq = NULL;
15930Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
15940Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr : NULL;
15950Sstevel@tonic-gate 			gateway_addr = &ifrt->ifrt_v6gateway_addr;
15960Sstevel@tonic-gate 			type = ifrt->ifrt_type;
15970Sstevel@tonic-gate 		}
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 		/*
16000Sstevel@tonic-gate 		 * Create a copy of the IRE with the saved address and netmask.
16010Sstevel@tonic-gate 		 */
16020Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n",
16030Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
16040Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)),
16050Sstevel@tonic-gate 		    ip_mask_to_plen_v6(&ifrt->ifrt_v6mask)));
16060Sstevel@tonic-gate 		ire = ire_create_v6(
16070Sstevel@tonic-gate 		    &ifrt->ifrt_v6addr,
16080Sstevel@tonic-gate 		    &ifrt->ifrt_v6mask,
16090Sstevel@tonic-gate 		    src_addr,
16100Sstevel@tonic-gate 		    gateway_addr,
16110Sstevel@tonic-gate 		    &ifrt->ifrt_max_frag,
16120Sstevel@tonic-gate 		    NULL,
16130Sstevel@tonic-gate 		    rfq,
16140Sstevel@tonic-gate 		    stq,
16150Sstevel@tonic-gate 		    type,
16160Sstevel@tonic-gate 		    ipif,
16170Sstevel@tonic-gate 		    NULL,
16180Sstevel@tonic-gate 		    0,
16190Sstevel@tonic-gate 		    0,
16200Sstevel@tonic-gate 		    ifrt->ifrt_flags,
16211676Sjpk 		    &ifrt->ifrt_iulp_info,
16221676Sjpk 		    NULL,
16233448Sdh155122 		    NULL,
16243448Sdh155122 		    ipst);
16250Sstevel@tonic-gate 		if (ire == NULL) {
16260Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
16270Sstevel@tonic-gate 			kmem_free(ipif_saved_irep,
16280Sstevel@tonic-gate 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
16290Sstevel@tonic-gate 			return (NULL);
16300Sstevel@tonic-gate 		}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 		/*
16330Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
16340Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
16350Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
16360Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
16370Sstevel@tonic-gate 		 * when generating aggregate routes.)
16380Sstevel@tonic-gate 		 *
16390Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
16400Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
16410Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
16420Sstevel@tonic-gate 		 */
16430Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
16440Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
16450Sstevel@tonic-gate 		/*
16460Sstevel@tonic-gate 		 * ire held by ire_add, will be refreled' in ipif_up_done
16470Sstevel@tonic-gate 		 * towards the end
16480Sstevel@tonic-gate 		 */
16492535Ssangeeta 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
16500Sstevel@tonic-gate 		*irep = ire;
16510Sstevel@tonic-gate 		irep++;
16520Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire));
16530Sstevel@tonic-gate 	}
16540Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_saved_ire_lock);
16550Sstevel@tonic-gate 	return (ipif_saved_irep);
16560Sstevel@tonic-gate }
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate /*
16590Sstevel@tonic-gate  * Return the scope of the given IPv6 address.  If the address is an
16600Sstevel@tonic-gate  * IPv4 mapped IPv6 address, return the scope of the corresponding
16610Sstevel@tonic-gate  * IPv4 address.
16620Sstevel@tonic-gate  */
16630Sstevel@tonic-gate in6addr_scope_t
16640Sstevel@tonic-gate ip_addr_scope_v6(const in6_addr_t *addr)
16650Sstevel@tonic-gate {
16660Sstevel@tonic-gate 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
16690Sstevel@tonic-gate 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
16700Sstevel@tonic-gate 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
16710Sstevel@tonic-gate 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
16720Sstevel@tonic-gate 			return (IP6_SCOPE_LINKLOCAL);
16730Sstevel@tonic-gate 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
16740Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
16750Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
16760Sstevel@tonic-gate 			return (IP6_SCOPE_SITELOCAL);
16770Sstevel@tonic-gate 		return (IP6_SCOPE_GLOBAL);
16780Sstevel@tonic-gate 	}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	if (IN6_IS_ADDR_MULTICAST(addr))
16810Sstevel@tonic-gate 		return (IN6_ADDR_MC_SCOPE(addr));
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/* link-local and loopback addresses are of link-local scope */
16840Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
16850Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
16860Sstevel@tonic-gate 		return (IP6_SCOPE_LINKLOCAL);
16870Sstevel@tonic-gate 	if (IN6_IS_ADDR_SITELOCAL(addr))
16880Sstevel@tonic-gate 		return (IP6_SCOPE_SITELOCAL);
16890Sstevel@tonic-gate 	return (IP6_SCOPE_GLOBAL);
16900Sstevel@tonic-gate }
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate /*
16943431Scarlsonj  * Returns the length of the common prefix of a1 and a2, as per
16953431Scarlsonj  * CommonPrefixLen() defined in RFC 3484.
16960Sstevel@tonic-gate  */
16973431Scarlsonj static int
16983431Scarlsonj ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2)
16990Sstevel@tonic-gate {
17000Sstevel@tonic-gate 	int i;
17013431Scarlsonj 	uint32_t a1val, a2val, mask;
17020Sstevel@tonic-gate 
17033431Scarlsonj 	for (i = 0; i < 4; i++) {
17043431Scarlsonj 		if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) {
17053431Scarlsonj 			a1val ^= a2val;
17063431Scarlsonj 			i *= 32;
17073431Scarlsonj 			mask = 0x80000000u;
17083431Scarlsonj 			while (!(a1val & mask)) {
17093431Scarlsonj 				mask >>= 1;
17103431Scarlsonj 				i++;
17113431Scarlsonj 			}
17123431Scarlsonj 			return (i);
17133431Scarlsonj 		}
17143431Scarlsonj 	}
17153431Scarlsonj 	return (IPV6_ABITS);
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate #define	IPIF_VALID_IPV6_SOURCE(ipif) \
17190Sstevel@tonic-gate 	(((ipif)->ipif_flags & IPIF_UP) && \
17202546Scarlsonj 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
17212546Scarlsonj 	(ipif)->ipif_addr_ready)
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate /* source address candidate */
17240Sstevel@tonic-gate typedef struct candidate {
17250Sstevel@tonic-gate 	ipif_t		*cand_ipif;
17260Sstevel@tonic-gate 	/* The properties of this candidate */
17270Sstevel@tonic-gate 	boolean_t	cand_isdst;
17280Sstevel@tonic-gate 	boolean_t	cand_isdst_set;
17290Sstevel@tonic-gate 	in6addr_scope_t	cand_scope;
17300Sstevel@tonic-gate 	boolean_t	cand_scope_set;
17310Sstevel@tonic-gate 	boolean_t	cand_isdeprecated;
17320Sstevel@tonic-gate 	boolean_t	cand_isdeprecated_set;
17330Sstevel@tonic-gate 	boolean_t	cand_ispreferred;
17340Sstevel@tonic-gate 	boolean_t	cand_ispreferred_set;
17350Sstevel@tonic-gate 	boolean_t	cand_matchedinterface;
17360Sstevel@tonic-gate 	boolean_t	cand_matchedinterface_set;
17370Sstevel@tonic-gate 	boolean_t	cand_matchedlabel;
17380Sstevel@tonic-gate 	boolean_t	cand_matchedlabel_set;
17390Sstevel@tonic-gate 	boolean_t	cand_istmp;
17400Sstevel@tonic-gate 	boolean_t	cand_istmp_set;
17413431Scarlsonj 	int		cand_common_pref;
17423431Scarlsonj 	boolean_t	cand_common_pref_set;
17433431Scarlsonj 	boolean_t	cand_pref_eq;
17443431Scarlsonj 	boolean_t	cand_pref_eq_set;
17453431Scarlsonj 	int		cand_pref_len;
17463431Scarlsonj 	boolean_t	cand_pref_len_set;
17470Sstevel@tonic-gate } cand_t;
17480Sstevel@tonic-gate #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
17493431Scarlsonj #define	cand_mask	cand_ipif->ipif_v6net_mask
17500Sstevel@tonic-gate #define	cand_flags	cand_ipif->ipif_flags
17510Sstevel@tonic-gate #define	cand_ill	cand_ipif->ipif_ill
17521676Sjpk #define	cand_zoneid	cand_ipif->ipif_zoneid
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate /* information about the destination for source address selection */
17550Sstevel@tonic-gate typedef struct dstinfo {
17560Sstevel@tonic-gate 	const in6_addr_t	*dst_addr;
17570Sstevel@tonic-gate 	ill_t			*dst_ill;
17582202Srk129064 	uint_t			dst_restrict_ill;
17590Sstevel@tonic-gate 	boolean_t		dst_prefer_src_tmp;
17600Sstevel@tonic-gate 	in6addr_scope_t		dst_scope;
17610Sstevel@tonic-gate 	char			*dst_label;
17620Sstevel@tonic-gate } dstinfo_t;
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate /*
17650Sstevel@tonic-gate  * The following functions are rules used to select a source address in
17660Sstevel@tonic-gate  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
17670Sstevel@tonic-gate  * against the best candidate (bc).  Each rule has three possible outcomes;
17680Sstevel@tonic-gate  * the candidate is preferred over the best candidate (CAND_PREFER), the
17690Sstevel@tonic-gate  * candidate is not preferred over the best candidate (CAND_AVOID), or the
17700Sstevel@tonic-gate  * candidate is of equal value as the best candidate (CAND_TIE).
17710Sstevel@tonic-gate  *
17720Sstevel@tonic-gate  * These rules are part of a greater "Default Address Selection for IPv6"
17730Sstevel@tonic-gate  * sheme, which is standards based work coming out of the IETF ipv6 working
17740Sstevel@tonic-gate  * group.  The IETF document defines both IPv6 source address selection and
17750Sstevel@tonic-gate  * destination address ordering.  The rules defined here implement the IPv6
17760Sstevel@tonic-gate  * source address selection.  Destination address ordering is done by
17770Sstevel@tonic-gate  * libnsl, and uses a similar set of rules to implement the sorting.
17783431Scarlsonj  *
17793431Scarlsonj  * Most of the rules are defined by the RFC and are not typically altered.  The
17803431Scarlsonj  * last rule, number 8, has language that allows for local preferences.  In the
17813431Scarlsonj  * scheme below, this means that new Solaris rules should normally go between
17823431Scarlsonj  * rule_ifprefix and rule_prefix.
17830Sstevel@tonic-gate  */
17840Sstevel@tonic-gate typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
17853448Sdh155122 typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *,
17863448Sdh155122     ip_stack_t *);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate /* Prefer an address if it is equal to the destination address. */
17893448Sdh155122 /* ARGSUSED3 */
17900Sstevel@tonic-gate static rule_res_t
17913448Sdh155122 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
17920Sstevel@tonic-gate {
17930Sstevel@tonic-gate 	if (!bc->cand_isdst_set) {
17940Sstevel@tonic-gate 		bc->cand_isdst =
17950Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
17960Sstevel@tonic-gate 		bc->cand_isdst_set = B_TRUE;
17970Sstevel@tonic-gate 	}
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	cc->cand_isdst =
18000Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
18010Sstevel@tonic-gate 	cc->cand_isdst_set = B_TRUE;
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	if (cc->cand_isdst == bc->cand_isdst)
18040Sstevel@tonic-gate 		return (CAND_TIE);
18050Sstevel@tonic-gate 	else if (cc->cand_isdst)
18060Sstevel@tonic-gate 		return (CAND_PREFER);
18070Sstevel@tonic-gate 	else
18080Sstevel@tonic-gate 		return (CAND_AVOID);
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate /*
18120Sstevel@tonic-gate  * Prefer addresses that are of closest scope to the destination.  Always
18130Sstevel@tonic-gate  * prefer addresses that are of greater scope than the destination over
18140Sstevel@tonic-gate  * those that are of lesser scope than the destination.
18150Sstevel@tonic-gate  */
18163448Sdh155122 /* ARGSUSED3 */
18170Sstevel@tonic-gate static rule_res_t
18183448Sdh155122 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
18190Sstevel@tonic-gate {
18200Sstevel@tonic-gate 	if (!bc->cand_scope_set) {
18210Sstevel@tonic-gate 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
18220Sstevel@tonic-gate 		bc->cand_scope_set = B_TRUE;
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
18260Sstevel@tonic-gate 	cc->cand_scope_set = B_TRUE;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	if (cc->cand_scope < bc->cand_scope) {
18290Sstevel@tonic-gate 		if (cc->cand_scope < dstinfo->dst_scope)
18300Sstevel@tonic-gate 			return (CAND_AVOID);
18310Sstevel@tonic-gate 		else
18320Sstevel@tonic-gate 			return (CAND_PREFER);
18330Sstevel@tonic-gate 	} else if (bc->cand_scope < cc->cand_scope) {
18340Sstevel@tonic-gate 		if (bc->cand_scope < dstinfo->dst_scope)
18350Sstevel@tonic-gate 			return (CAND_PREFER);
18360Sstevel@tonic-gate 		else
18370Sstevel@tonic-gate 			return (CAND_AVOID);
18380Sstevel@tonic-gate 	} else {
18390Sstevel@tonic-gate 		return (CAND_TIE);
18400Sstevel@tonic-gate 	}
18410Sstevel@tonic-gate }
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate /*
18440Sstevel@tonic-gate  * Prefer non-deprecated source addresses.
18450Sstevel@tonic-gate  */
18460Sstevel@tonic-gate /* ARGSUSED2 */
18470Sstevel@tonic-gate static rule_res_t
18483448Sdh155122 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
18493448Sdh155122     ip_stack_t *ipst)
18500Sstevel@tonic-gate {
18510Sstevel@tonic-gate 	if (!bc->cand_isdeprecated_set) {
18520Sstevel@tonic-gate 		bc->cand_isdeprecated =
18530Sstevel@tonic-gate 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
18540Sstevel@tonic-gate 		bc->cand_isdeprecated_set = B_TRUE;
18550Sstevel@tonic-gate 	}
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
18580Sstevel@tonic-gate 	cc->cand_isdeprecated_set = B_TRUE;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
18610Sstevel@tonic-gate 		return (CAND_TIE);
18620Sstevel@tonic-gate 	else if (cc->cand_isdeprecated)
18630Sstevel@tonic-gate 		return (CAND_AVOID);
18640Sstevel@tonic-gate 	else
18650Sstevel@tonic-gate 		return (CAND_PREFER);
18660Sstevel@tonic-gate }
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate /*
18690Sstevel@tonic-gate  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
18700Sstevel@tonic-gate  * rule must be before rule_interface because the flag could be set on any
18710Sstevel@tonic-gate  * interface, not just the interface being used for outgoing packets (for
18720Sstevel@tonic-gate  * example, the IFF_PREFERRED could be set on an address assigned to the
18730Sstevel@tonic-gate  * loopback interface).
18740Sstevel@tonic-gate  */
18750Sstevel@tonic-gate /* ARGSUSED2 */
18760Sstevel@tonic-gate static rule_res_t
18773448Sdh155122 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
18783448Sdh155122     ip_stack_t *ipst)
18790Sstevel@tonic-gate {
18800Sstevel@tonic-gate 	if (!bc->cand_ispreferred_set) {
18810Sstevel@tonic-gate 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
18820Sstevel@tonic-gate 		bc->cand_ispreferred_set = B_TRUE;
18830Sstevel@tonic-gate 	}
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
18860Sstevel@tonic-gate 	cc->cand_ispreferred_set = B_TRUE;
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	if (bc->cand_ispreferred == cc->cand_ispreferred)
18890Sstevel@tonic-gate 		return (CAND_TIE);
18900Sstevel@tonic-gate 	else if (cc->cand_ispreferred)
18910Sstevel@tonic-gate 		return (CAND_PREFER);
18920Sstevel@tonic-gate 	else
18930Sstevel@tonic-gate 		return (CAND_AVOID);
18940Sstevel@tonic-gate }
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate /*
18970Sstevel@tonic-gate  * Prefer source addresses that are assigned to the outgoing interface, or
18980Sstevel@tonic-gate  * to an interface that is in the same IPMP group as the outgoing
18990Sstevel@tonic-gate  * interface.
19000Sstevel@tonic-gate  */
19013448Sdh155122 /* ARGSUSED3 */
19020Sstevel@tonic-gate static rule_res_t
19033448Sdh155122 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
19043448Sdh155122     ip_stack_t *ipst)
19050Sstevel@tonic-gate {
19060Sstevel@tonic-gate 	ill_t *dstill = dstinfo->dst_ill;
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	/*
19090Sstevel@tonic-gate 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
19100Sstevel@tonic-gate 	 * since we know all candidates will be on the same link.
19110Sstevel@tonic-gate 	 */
19120Sstevel@tonic-gate 	if (dstinfo->dst_restrict_ill)
19130Sstevel@tonic-gate 		return (CAND_TIE);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	if (!bc->cand_matchedinterface_set) {
19160Sstevel@tonic-gate 		bc->cand_matchedinterface = (bc->cand_ill == dstill ||
19170Sstevel@tonic-gate 		    (dstill->ill_group != NULL &&
19180Sstevel@tonic-gate 		    dstill->ill_group == bc->cand_ill->ill_group));
19190Sstevel@tonic-gate 		bc->cand_matchedinterface_set = B_TRUE;
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	cc->cand_matchedinterface = (cc->cand_ill == dstill ||
19230Sstevel@tonic-gate 	    (dstill->ill_group != NULL &&
19244459Skcpoon 	    dstill->ill_group == cc->cand_ill->ill_group));
19250Sstevel@tonic-gate 	cc->cand_matchedinterface_set = B_TRUE;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
19280Sstevel@tonic-gate 		return (CAND_TIE);
19290Sstevel@tonic-gate 	else if (cc->cand_matchedinterface)
19300Sstevel@tonic-gate 		return (CAND_PREFER);
19310Sstevel@tonic-gate 	else
19320Sstevel@tonic-gate 		return (CAND_AVOID);
19330Sstevel@tonic-gate }
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate /*
19360Sstevel@tonic-gate  * Prefer source addresses whose label matches the destination's label.
19370Sstevel@tonic-gate  */
19380Sstevel@tonic-gate static rule_res_t
19393448Sdh155122 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	char *label;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	if (!bc->cand_matchedlabel_set) {
19443448Sdh155122 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst);
19450Sstevel@tonic-gate 		bc->cand_matchedlabel =
19460Sstevel@tonic-gate 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
19470Sstevel@tonic-gate 		bc->cand_matchedlabel_set = B_TRUE;
19480Sstevel@tonic-gate 	}
19490Sstevel@tonic-gate 
19503448Sdh155122 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst);
19510Sstevel@tonic-gate 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
19520Sstevel@tonic-gate 	cc->cand_matchedlabel_set = B_TRUE;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
19550Sstevel@tonic-gate 		return (CAND_TIE);
19560Sstevel@tonic-gate 	else if (cc->cand_matchedlabel)
19570Sstevel@tonic-gate 		return (CAND_PREFER);
19580Sstevel@tonic-gate 	else
19590Sstevel@tonic-gate 		return (CAND_AVOID);
19600Sstevel@tonic-gate }
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate /*
19630Sstevel@tonic-gate  * Prefer public addresses over temporary ones.  An application can reverse
19640Sstevel@tonic-gate  * the logic of this rule and prefer temporary addresses by using the
19650Sstevel@tonic-gate  * IPV6_SRC_PREFERENCES socket option.
19660Sstevel@tonic-gate  */
19673448Sdh155122 /* ARGSUSED3 */
19680Sstevel@tonic-gate static rule_res_t
19693448Sdh155122 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
19703448Sdh155122     ip_stack_t *ipst)
19710Sstevel@tonic-gate {
19720Sstevel@tonic-gate 	if (!bc->cand_istmp_set) {
19730Sstevel@tonic-gate 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
19740Sstevel@tonic-gate 		bc->cand_istmp_set = B_TRUE;
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
19780Sstevel@tonic-gate 	cc->cand_istmp_set = B_TRUE;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	if (bc->cand_istmp == cc->cand_istmp)
19810Sstevel@tonic-gate 		return (CAND_TIE);
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
19840Sstevel@tonic-gate 		return (CAND_PREFER);
19850Sstevel@tonic-gate 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
19860Sstevel@tonic-gate 		return (CAND_PREFER);
19870Sstevel@tonic-gate 	else
19880Sstevel@tonic-gate 		return (CAND_AVOID);
19890Sstevel@tonic-gate }
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate /*
19923431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination
19933431Scarlsonj  * under the interface mask.  This gets us on the same subnet before applying
19943431Scarlsonj  * any Solaris-specific rules.
19950Sstevel@tonic-gate  */
19963448Sdh155122 /* ARGSUSED3 */
19970Sstevel@tonic-gate static rule_res_t
19983448Sdh155122 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
19993448Sdh155122     ip_stack_t *ipst)
20000Sstevel@tonic-gate {
20013431Scarlsonj 	if (!bc->cand_pref_eq_set) {
20023431Scarlsonj 		bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr,
20033431Scarlsonj 		    bc->cand_mask, *dstinfo->dst_addr);
20043431Scarlsonj 		bc->cand_pref_eq_set = B_TRUE;
20050Sstevel@tonic-gate 	}
20060Sstevel@tonic-gate 
20073431Scarlsonj 	cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask,
20083431Scarlsonj 	    *dstinfo->dst_addr);
20093431Scarlsonj 	cc->cand_pref_eq_set = B_TRUE;
20100Sstevel@tonic-gate 
20113431Scarlsonj 	if (bc->cand_pref_eq) {
20123431Scarlsonj 		if (cc->cand_pref_eq) {
20133431Scarlsonj 			if (!bc->cand_pref_len_set) {
20143431Scarlsonj 				bc->cand_pref_len =
20153431Scarlsonj 				    ip_mask_to_plen_v6(&bc->cand_mask);
20163431Scarlsonj 				bc->cand_pref_len_set = B_TRUE;
20173431Scarlsonj 			}
20183431Scarlsonj 			cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask);
20193431Scarlsonj 			cc->cand_pref_len_set = B_TRUE;
20203431Scarlsonj 			if (bc->cand_pref_len == cc->cand_pref_len)
20213431Scarlsonj 				return (CAND_TIE);
20223431Scarlsonj 			else if (bc->cand_pref_len > cc->cand_pref_len)
20233431Scarlsonj 				return (CAND_AVOID);
20243431Scarlsonj 			else
20253431Scarlsonj 				return (CAND_PREFER);
20263431Scarlsonj 		} else {
20273431Scarlsonj 			return (CAND_AVOID);
20283431Scarlsonj 		}
20293431Scarlsonj 	} else {
20303431Scarlsonj 		if (cc->cand_pref_eq)
20313431Scarlsonj 			return (CAND_PREFER);
20323431Scarlsonj 		else
20333431Scarlsonj 			return (CAND_TIE);
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate }
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate /*
20381676Sjpk  * Prefer to use zone-specific addresses when possible instead of all-zones
20391676Sjpk  * addresses.
20401676Sjpk  */
20411676Sjpk /* ARGSUSED2 */
20421676Sjpk static rule_res_t
20433448Sdh155122 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20443448Sdh155122     ip_stack_t *ipst)
20451676Sjpk {
20461676Sjpk 	if ((bc->cand_zoneid == ALL_ZONES) ==
20471676Sjpk 	    (cc->cand_zoneid == ALL_ZONES))
20481676Sjpk 		return (CAND_TIE);
20491676Sjpk 	else if (cc->cand_zoneid == ALL_ZONES)
20501676Sjpk 		return (CAND_AVOID);
20511676Sjpk 	else
20521676Sjpk 		return (CAND_PREFER);
20531676Sjpk }
20541676Sjpk 
20551676Sjpk /*
20563431Scarlsonj  * Prefer to use DHCPv6 (first) and static addresses (second) when possible
20573431Scarlsonj  * instead of statelessly autoconfigured addresses.
20583431Scarlsonj  *
20593431Scarlsonj  * This is done after trying all other preferences (and before the final tie
20603431Scarlsonj  * breaker) so that, if all else is equal, we select addresses configured by
20613431Scarlsonj  * DHCPv6 over other addresses.  We presume that DHCPv6 addresses, unlike
20623431Scarlsonj  * stateless autoconfigured addresses, are deliberately configured by an
20633431Scarlsonj  * administrator, and thus are correctly set up in DNS and network packet
20643431Scarlsonj  * filters.
20653431Scarlsonj  */
20663431Scarlsonj /* ARGSUSED2 */
20673431Scarlsonj static rule_res_t
20683448Sdh155122 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20693448Sdh155122     ip_stack_t *ipst)
20703431Scarlsonj {
20713431Scarlsonj #define	ATYPE(x)	\
20723431Scarlsonj 	((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2
20733431Scarlsonj 	int bcval = ATYPE(bc->cand_flags);
20743431Scarlsonj 	int ccval = ATYPE(cc->cand_flags);
20753431Scarlsonj #undef ATYPE
20763431Scarlsonj 
20773431Scarlsonj 	if (bcval == ccval)
20783431Scarlsonj 		return (CAND_TIE);
20793431Scarlsonj 	else if (ccval < bcval)
20803431Scarlsonj 		return (CAND_PREFER);
20813431Scarlsonj 	else
20823431Scarlsonj 		return (CAND_AVOID);
20833431Scarlsonj }
20843431Scarlsonj 
20853431Scarlsonj /*
20863431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination.
20873431Scarlsonj  * We do the longest matching prefix calculation by doing an xor of both
20883431Scarlsonj  * addresses with the destination, and pick the address with the longest string
20893431Scarlsonj  * of leading zeros, as per CommonPrefixLen() defined in RFC 3484.
20903431Scarlsonj  */
20913448Sdh155122 /* ARGSUSED3 */
20923431Scarlsonj static rule_res_t
20933448Sdh155122 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
20943431Scarlsonj {
20953431Scarlsonj 	if (!bc->cand_common_pref_set) {
20963431Scarlsonj 		bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr,
20973431Scarlsonj 		    dstinfo->dst_addr);
20983431Scarlsonj 		bc->cand_common_pref_set = B_TRUE;
20993431Scarlsonj 	}
21003431Scarlsonj 
21013431Scarlsonj 	cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr,
21023431Scarlsonj 	    dstinfo->dst_addr);
21033431Scarlsonj 	cc->cand_common_pref_set = B_TRUE;
21043431Scarlsonj 
21053431Scarlsonj 	if (bc->cand_common_pref == cc->cand_common_pref)
21063431Scarlsonj 		return (CAND_TIE);
21073431Scarlsonj 	else if (bc->cand_common_pref > cc->cand_common_pref)
21083431Scarlsonj 		return (CAND_AVOID);
21093431Scarlsonj 	else
21103431Scarlsonj 		return (CAND_PREFER);
21113431Scarlsonj }
21123431Scarlsonj 
21133431Scarlsonj /*
21143431Scarlsonj  * Last rule: we must pick something, so just prefer the current best
21153431Scarlsonj  * candidate.
21163431Scarlsonj  */
21173431Scarlsonj /* ARGSUSED */
21183431Scarlsonj static rule_res_t
21193448Sdh155122 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
21203448Sdh155122     ip_stack_t *ipst)
21213431Scarlsonj {
21223431Scarlsonj 	return (CAND_AVOID);
21233431Scarlsonj }
21243431Scarlsonj 
21253431Scarlsonj /*
21260Sstevel@tonic-gate  * Determine the best source address given a destination address and a
21270Sstevel@tonic-gate  * destination ill.  If no suitable source address is found, it returns
21280Sstevel@tonic-gate  * NULL. If there is a usable address pointed to by the usesrc
21290Sstevel@tonic-gate  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
21300Sstevel@tonic-gate  * fine grained (i.e per interface)
21310Sstevel@tonic-gate  *
21320Sstevel@tonic-gate  * This implementation is based on the "Default Address Selection for IPv6"
21330Sstevel@tonic-gate  * specification produced by the IETF IPv6 working group.  It has been
21340Sstevel@tonic-gate  * implemented so that the list of addresses is only traversed once (the
21350Sstevel@tonic-gate  * specification's algorithm could traverse the list of addresses once for
21360Sstevel@tonic-gate  * every rule).
21370Sstevel@tonic-gate  *
21380Sstevel@tonic-gate  * The restrict_ill argument restricts the algorithm to chose a source
21390Sstevel@tonic-gate  * address that is assigned to the destination ill or an ill in the same
21400Sstevel@tonic-gate  * IPMP group as the destination ill.  This is used when the destination
21410Sstevel@tonic-gate  * address is a link-local or multicast address, and when
21420Sstevel@tonic-gate  * ipv6_strict_dst_multihoming is turned on.
21430Sstevel@tonic-gate  *
21440Sstevel@tonic-gate  * src_prefs is the caller's set of source address preferences.  If source
21450Sstevel@tonic-gate  * address selection is being called to determine the source address of a
21460Sstevel@tonic-gate  * connected socket (from ip_bind_connected_v6()), then the preferences are
21470Sstevel@tonic-gate  * taken from conn_src_preferences.  These preferences can be set on a
21480Sstevel@tonic-gate  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
21490Sstevel@tonic-gate  * preference currently implemented is for rfc3041 temporary addresses.
21500Sstevel@tonic-gate  */
21510Sstevel@tonic-gate ipif_t *
21520Sstevel@tonic-gate ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
21532202Srk129064     uint_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid)
21540Sstevel@tonic-gate {
21550Sstevel@tonic-gate 	dstinfo_t	dstinfo;
21560Sstevel@tonic-gate 	char		dstr[INET6_ADDRSTRLEN];
21570Sstevel@tonic-gate 	char		sstr[INET6_ADDRSTRLEN];
21580Sstevel@tonic-gate 	ipif_t		*ipif;
21590Sstevel@tonic-gate 	ill_t		*ill, *usesrc_ill = NULL;
21600Sstevel@tonic-gate 	ill_walk_context_t	ctx;
21610Sstevel@tonic-gate 	cand_t		best_c;	/* The best candidate */
21620Sstevel@tonic-gate 	cand_t		curr_c;	/* The current candidate */
21630Sstevel@tonic-gate 	uint_t		index;
21640Sstevel@tonic-gate 	boolean_t	first_candidate = B_TRUE;
21650Sstevel@tonic-gate 	rule_res_t	rule_result;
21661676Sjpk 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
21673448Sdh155122 	ip_stack_t	*ipst = dstill->ill_ipst;
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	/*
21700Sstevel@tonic-gate 	 * The list of ordering rules.  They are applied in the order they
21710Sstevel@tonic-gate 	 * appear in the list.
21720Sstevel@tonic-gate 	 *
21733431Scarlsonj 	 * Solaris doesn't currently support Mobile IPv6, so there's no
21743431Scarlsonj 	 * rule_mipv6 corresponding to rule 4 in the specification.
21750Sstevel@tonic-gate 	 */
21760Sstevel@tonic-gate 	rulef_t	rules[] = {
21770Sstevel@tonic-gate 		rule_isdst,
21780Sstevel@tonic-gate 		rule_scope,
21790Sstevel@tonic-gate 		rule_deprecated,
21800Sstevel@tonic-gate 		rule_preferred,
21810Sstevel@tonic-gate 		rule_interface,
21820Sstevel@tonic-gate 		rule_label,
21830Sstevel@tonic-gate 		rule_temporary,
21843431Scarlsonj 		rule_ifprefix,			/* local rules after this */
21851676Sjpk 		rule_zone_specific,
21863431Scarlsonj 		rule_addr_type,
21873431Scarlsonj 		rule_prefix,			/* local rules before this */
21883431Scarlsonj 		rule_must_be_last,		/* must always be last */
21890Sstevel@tonic-gate 		NULL
21900Sstevel@tonic-gate 	};
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	ASSERT(dstill->ill_isv6);
21930Sstevel@tonic-gate 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	/*
21960Sstevel@tonic-gate 	 * Check if there is a usable src address pointed to by the
21970Sstevel@tonic-gate 	 * usesrc ifindex. This has higher precedence since it is
21980Sstevel@tonic-gate 	 * finer grained (i.e per interface) v/s being system wide.
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 	if (dstill->ill_usesrc_ifindex != 0) {
22010Sstevel@tonic-gate 		if ((usesrc_ill =
22020Sstevel@tonic-gate 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
22033448Sdh155122 		    NULL, NULL, NULL, NULL, ipst)) != NULL) {
22040Sstevel@tonic-gate 			dstinfo.dst_ill = usesrc_ill;
22050Sstevel@tonic-gate 		} else {
22060Sstevel@tonic-gate 			return (NULL);
22070Sstevel@tonic-gate 		}
22080Sstevel@tonic-gate 	} else {
22090Sstevel@tonic-gate 		dstinfo.dst_ill = dstill;
22100Sstevel@tonic-gate 	}
22110Sstevel@tonic-gate 
22121676Sjpk 	/*
22131676Sjpk 	 * If we're dealing with an unlabeled destination on a labeled system,
22141676Sjpk 	 * make sure that we ignore source addresses that are incompatible with
22151676Sjpk 	 * the destination's default label.  That destination's default label
22161676Sjpk 	 * must dominate the minimum label on the source address.
22171676Sjpk 	 *
22181676Sjpk 	 * (Note that this has to do with Trusted Solaris.  It's not related to
22191676Sjpk 	 * the labels described by ip6_asp_lookup.)
22201676Sjpk 	 */
22211676Sjpk 	dst_rhtp = NULL;
22221676Sjpk 	if (is_system_labeled()) {
22231676Sjpk 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
22241676Sjpk 		if (dst_rhtp == NULL)
22251676Sjpk 			return (NULL);
22261676Sjpk 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
22271676Sjpk 			TPC_RELE(dst_rhtp);
22281676Sjpk 			dst_rhtp = NULL;
22291676Sjpk 		}
22301676Sjpk 	}
22311676Sjpk 
22320Sstevel@tonic-gate 	dstinfo.dst_addr = dst;
22330Sstevel@tonic-gate 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
22343448Sdh155122 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst);
22350Sstevel@tonic-gate 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
22360Sstevel@tonic-gate 
22373448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22380Sstevel@tonic-gate 	/*
22390Sstevel@tonic-gate 	 * Section three of the I-D states that for multicast and
22400Sstevel@tonic-gate 	 * link-local destinations, the candidate set must be restricted to
22410Sstevel@tonic-gate 	 * an interface that is on the same link as the outgoing interface.
22420Sstevel@tonic-gate 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
22430Sstevel@tonic-gate 	 * restrict the source address to the destination link as doing
22440Sstevel@tonic-gate 	 * otherwise will almost certainly cause problems.
22450Sstevel@tonic-gate 	 */
22460Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
22473448Sdh155122 	    ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
22482202Srk129064 		if (restrict_ill == RESTRICT_TO_NONE)
22492202Srk129064 			dstinfo.dst_restrict_ill = RESTRICT_TO_GROUP;
22502202Srk129064 		else
22512202Srk129064 			dstinfo.dst_restrict_ill = restrict_ill;
22522202Srk129064 	} else {
22530Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = restrict_ill;
22542202Srk129064 	}
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 	bzero(&best_c, sizeof (cand_t));
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	/*
22590Sstevel@tonic-gate 	 * Take a pass through the list of IPv6 interfaces to chose the
22600Sstevel@tonic-gate 	 * best possible source address.  If restrict_ill is true, we only
22610Sstevel@tonic-gate 	 * iterate through the ill's that are in the same IPMP group as the
22620Sstevel@tonic-gate 	 * destination's outgoing ill.  If restrict_ill is false, we walk
22630Sstevel@tonic-gate 	 * the entire list of IPv6 ill's.
22640Sstevel@tonic-gate 	 */
22652202Srk129064 	if (dstinfo.dst_restrict_ill != RESTRICT_TO_NONE) {
22662202Srk129064 		if (dstinfo.dst_ill->ill_group != NULL &&
22672202Srk129064 		    dstinfo.dst_restrict_ill == RESTRICT_TO_GROUP) {
22682202Srk129064 			ill = dstinfo.dst_ill->ill_group->illgrp_ill;
22690Sstevel@tonic-gate 		} else {
22700Sstevel@tonic-gate 			ill = dstinfo.dst_ill;
22710Sstevel@tonic-gate 		}
22720Sstevel@tonic-gate 	} else {
22733448Sdh155122 		ill = ILL_START_WALK_V6(&ctx, ipst);
22740Sstevel@tonic-gate 	}
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	while (ill != NULL) {
22770Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
22780Sstevel@tonic-gate 
22792202Srk129064 		/*
22802202Srk129064 		 * Avoid FAILED/OFFLINE ills.
22812202Srk129064 		 * Global and site local addresses will failover and
22822202Srk129064 		 * will be available on the new ill.
22832202Srk129064 		 * But link local addresses don't move.
22842202Srk129064 		 */
22852513Srk129064 		if (dstinfo.dst_restrict_ill != RESTRICT_TO_ILL &&
22862513Srk129064 		    ill->ill_phyint->phyint_flags &
22872202Srk129064 		    (PHYI_OFFLINE | PHYI_FAILED))
22882202Srk129064 			goto next_ill;
22892202Srk129064 
22900Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
22910Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
22940Sstevel@tonic-gate 				continue;
22950Sstevel@tonic-gate 
22961676Sjpk 			if (zoneid != ALL_ZONES &&
22971676Sjpk 			    ipif->ipif_zoneid != zoneid &&
22981676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
22990Sstevel@tonic-gate 				continue;
23000Sstevel@tonic-gate 
23011676Sjpk 			/*
23021676Sjpk 			 * Check compatibility of local address for
23031676Sjpk 			 * destination's default label if we're on a labeled
23041676Sjpk 			 * system.  Incompatible addresses can't be used at
23051676Sjpk 			 * all and must be skipped over.
23061676Sjpk 			 */
23071676Sjpk 			if (dst_rhtp != NULL) {
23081676Sjpk 				boolean_t incompat;
23091676Sjpk 
23101676Sjpk 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
23111676Sjpk 				    IPV6_VERSION, B_FALSE);
23121676Sjpk 				if (src_rhtp == NULL)
23131676Sjpk 					continue;
23141676Sjpk 				incompat =
23151676Sjpk 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
23161676Sjpk 				    src_rhtp->tpc_tp.tp_doi !=
23171676Sjpk 				    dst_rhtp->tpc_tp.tp_doi ||
23181676Sjpk 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
23191676Sjpk 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
23201676Sjpk 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
23211676Sjpk 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
23221676Sjpk 				TPC_RELE(src_rhtp);
23231676Sjpk 				if (incompat)
23241676Sjpk 					continue;
23251676Sjpk 			}
23261676Sjpk 
23270Sstevel@tonic-gate 			if (first_candidate) {
23280Sstevel@tonic-gate 				/*
23290Sstevel@tonic-gate 				 * This is first valid address in the list.
23300Sstevel@tonic-gate 				 * It is automatically the best candidate
23310Sstevel@tonic-gate 				 * so far.
23320Sstevel@tonic-gate 				 */
23330Sstevel@tonic-gate 				best_c.cand_ipif = ipif;
23340Sstevel@tonic-gate 				first_candidate = B_FALSE;
23350Sstevel@tonic-gate 				continue;
23360Sstevel@tonic-gate 			}
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 			bzero(&curr_c, sizeof (cand_t));
23390Sstevel@tonic-gate 			curr_c.cand_ipif = ipif;
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 			/*
23420Sstevel@tonic-gate 			 * Compare this current candidate (curr_c) with the
23430Sstevel@tonic-gate 			 * best candidate (best_c) by applying the
23440Sstevel@tonic-gate 			 * comparison rules in order until one breaks the
23450Sstevel@tonic-gate 			 * tie.
23460Sstevel@tonic-gate 			 */
23470Sstevel@tonic-gate 			for (index = 0; rules[index] != NULL; index++) {
23480Sstevel@tonic-gate 				/* Apply a comparison rule. */
23490Sstevel@tonic-gate 				rule_result =
23503448Sdh155122 				    (rules[index])(&best_c, &curr_c, &dstinfo,
23513448Sdh155122 				    ipst);
23520Sstevel@tonic-gate 				if (rule_result == CAND_AVOID) {
23530Sstevel@tonic-gate 					/*
23540Sstevel@tonic-gate 					 * The best candidate is still the
23550Sstevel@tonic-gate 					 * best candidate.  Forget about
23560Sstevel@tonic-gate 					 * this current candidate and go on
23570Sstevel@tonic-gate 					 * to the next one.
23580Sstevel@tonic-gate 					 */
23590Sstevel@tonic-gate 					break;
23600Sstevel@tonic-gate 				} else if (rule_result == CAND_PREFER) {
23610Sstevel@tonic-gate 					/*
23620Sstevel@tonic-gate 					 * This candidate is prefered.  It
23630Sstevel@tonic-gate 					 * becomes the best candidate so
23640Sstevel@tonic-gate 					 * far.  Go on to the next address.
23650Sstevel@tonic-gate 					 */
23660Sstevel@tonic-gate 					best_c = curr_c;
23670Sstevel@tonic-gate 					break;
23680Sstevel@tonic-gate 				}
23690Sstevel@tonic-gate 				/* We have a tie, apply the next rule. */
23700Sstevel@tonic-gate 			}
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 			/*
23730Sstevel@tonic-gate 			 * The last rule must be a tie breaker rule and
23740Sstevel@tonic-gate 			 * must never produce a tie.  At this point, the
23750Sstevel@tonic-gate 			 * candidate should have either been rejected, or
23760Sstevel@tonic-gate 			 * have been prefered as the best candidate so far.
23770Sstevel@tonic-gate 			 */
23780Sstevel@tonic-gate 			ASSERT(rule_result != CAND_TIE);
23790Sstevel@tonic-gate 		}
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 		/*
23820Sstevel@tonic-gate 		 * We may be walking the linked-list of ill's in an
23830Sstevel@tonic-gate 		 * IPMP group or traversing the IPv6 ill avl tree. If it is a
23840Sstevel@tonic-gate 		 * usesrc ILL then it can't be part of IPMP group and we
23850Sstevel@tonic-gate 		 * will exit the while loop.
23860Sstevel@tonic-gate 		 */
23872202Srk129064 next_ill:
23882202Srk129064 		if (dstinfo.dst_restrict_ill == RESTRICT_TO_ILL)
23892202Srk129064 			ill = NULL;
23902202Srk129064 		else if (dstinfo.dst_restrict_ill == RESTRICT_TO_GROUP)
23910Sstevel@tonic-gate 			ill = ill->ill_group_next;
23920Sstevel@tonic-gate 		else
23930Sstevel@tonic-gate 			ill = ill_next(&ctx, ill);
23940Sstevel@tonic-gate 	}
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	ipif = best_c.cand_ipif;
23970Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
23980Sstevel@tonic-gate 	    dstinfo.dst_ill->ill_name,
23990Sstevel@tonic-gate 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
24000Sstevel@tonic-gate 	    (ipif == NULL ? "NULL" :
24010Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	if (usesrc_ill != NULL)
24040Sstevel@tonic-gate 		ill_refrele(usesrc_ill);
24050Sstevel@tonic-gate 
24061676Sjpk 	if (dst_rhtp != NULL)
24071676Sjpk 		TPC_RELE(dst_rhtp);
24081676Sjpk 
24090Sstevel@tonic-gate 	if (ipif == NULL) {
24103448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
24110Sstevel@tonic-gate 		return (NULL);
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_ill->ill_lock);
24150Sstevel@tonic-gate 	if (IPIF_CAN_LOOKUP(ipif)) {
24160Sstevel@tonic-gate 		ipif_refhold_locked(ipif);
24170Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_ill->ill_lock);
24183448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
24190Sstevel@tonic-gate 		return (ipif);
24200Sstevel@tonic-gate 	}
24210Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_ill->ill_lock);
24223448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
24230Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
24240Sstevel@tonic-gate 	    " returning null \n", (void *)ipif));
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	return (NULL);
24270Sstevel@tonic-gate }
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate /*
24300Sstevel@tonic-gate  * If old_ipif is not NULL, see if ipif was derived from old
24310Sstevel@tonic-gate  * ipif and if so, recreate the interface route by re-doing
24320Sstevel@tonic-gate  * source address selection. This happens when ipif_down ->
24330Sstevel@tonic-gate  * ipif_update_other_ipifs calls us.
24340Sstevel@tonic-gate  *
24350Sstevel@tonic-gate  * If old_ipif is NULL, just redo the source address selection
24360Sstevel@tonic-gate  * if needed. This happens when illgrp_insert or ipif_up_done_v6
24370Sstevel@tonic-gate  * calls us.
24380Sstevel@tonic-gate  */
24390Sstevel@tonic-gate void
24400Sstevel@tonic-gate ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif)
24410Sstevel@tonic-gate {
24420Sstevel@tonic-gate 	ire_t *ire;
24430Sstevel@tonic-gate 	ire_t *ipif_ire;
24440Sstevel@tonic-gate 	queue_t *stq;
24450Sstevel@tonic-gate 	ill_t *ill;
24460Sstevel@tonic-gate 	ipif_t *nipif = NULL;
24470Sstevel@tonic-gate 	boolean_t nipif_refheld = B_FALSE;
24480Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
24493448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	ill = ipif->ipif_ill;
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	if (!(ipif->ipif_flags &
24540Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
24550Sstevel@tonic-gate 		/*
24560Sstevel@tonic-gate 		 * Can't possibly have borrowed the source
24570Sstevel@tonic-gate 		 * from old_ipif.
24580Sstevel@tonic-gate 		 */
24590Sstevel@tonic-gate 		return;
24600Sstevel@tonic-gate 	}
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	/*
24630Sstevel@tonic-gate 	 * Is there any work to be done? No work if the address
24640Sstevel@tonic-gate 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
24650Sstevel@tonic-gate 	 * ipif_select_source_v6() does not borrow addresses from
24660Sstevel@tonic-gate 	 * NOLOCAL and ANYCAST interfaces).
24670Sstevel@tonic-gate 	 */
24680Sstevel@tonic-gate 	if ((old_ipif != NULL) &&
24690Sstevel@tonic-gate 	    ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) ||
24700Sstevel@tonic-gate 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
24710Sstevel@tonic-gate 	    (old_ipif->ipif_flags &
24720Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
24730Sstevel@tonic-gate 		return;
24740Sstevel@tonic-gate 	}
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 	/*
24770Sstevel@tonic-gate 	 * Perform the same checks as when creating the
24780Sstevel@tonic-gate 	 * IRE_INTERFACE in ipif_up_done_v6.
24790Sstevel@tonic-gate 	 */
24800Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_UP))
24810Sstevel@tonic-gate 		return;
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_NOXMIT))
24840Sstevel@tonic-gate 		return;
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
24870Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
24880Sstevel@tonic-gate 		return;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	/*
24910Sstevel@tonic-gate 	 * We know that ipif uses some other source for its
24920Sstevel@tonic-gate 	 * IRE_INTERFACE. Is it using the source of this
24930Sstevel@tonic-gate 	 * old_ipif?
24940Sstevel@tonic-gate 	 */
24950Sstevel@tonic-gate 	ipif_ire = ipif_to_ire_v6(ipif);
24960Sstevel@tonic-gate 	if (ipif_ire == NULL)
24970Sstevel@tonic-gate 		return;
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 	if (old_ipif != NULL &&
25000Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr,
25010Sstevel@tonic-gate 	    &ipif_ire->ire_src_addr_v6)) {
25020Sstevel@tonic-gate 		ire_refrele(ipif_ire);
25030Sstevel@tonic-gate 		return;
25040Sstevel@tonic-gate 	}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	if (ip_debug > 2) {
25070Sstevel@tonic-gate 		/* ip1dbg */
25080Sstevel@tonic-gate 		pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE"
25090Sstevel@tonic-gate 		    " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6);
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	stq = ipif_ire->ire_stq;
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	/*
25150Sstevel@tonic-gate 	 * Can't use our source address. Select a different source address
25160Sstevel@tonic-gate 	 * for the IRE_INTERFACE.  We restrict interface route source
25170Sstevel@tonic-gate 	 * address selection to ipif's assigned to the same link as the
25180Sstevel@tonic-gate 	 * interface.
25190Sstevel@tonic-gate 	 */
25203448Sdh155122 	if (ip6_asp_can_lookup(ipst)) {
25210Sstevel@tonic-gate 		ip6_asp_table_held = B_TRUE;
25220Sstevel@tonic-gate 		nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet,
25232202Srk129064 		    RESTRICT_TO_GROUP, IPV6_PREFER_SRC_DEFAULT,
25242202Srk129064 		    ipif->ipif_zoneid);
25250Sstevel@tonic-gate 	}
25260Sstevel@tonic-gate 	if (nipif == NULL) {
25270Sstevel@tonic-gate 		/* Last resort - all ipif's have IPIF_NOLOCAL */
25280Sstevel@tonic-gate 		nipif = ipif;
25290Sstevel@tonic-gate 	} else {
25300Sstevel@tonic-gate 		nipif_refheld = B_TRUE;
25310Sstevel@tonic-gate 	}
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	ire = ire_create_v6(
25340Sstevel@tonic-gate 	    &ipif->ipif_v6subnet,	/* dest pref */
25350Sstevel@tonic-gate 	    &ipif->ipif_v6net_mask,	/* mask */
25360Sstevel@tonic-gate 	    &nipif->ipif_v6src_addr,	/* src addr */
25370Sstevel@tonic-gate 	    NULL,			/* no gateway */
25380Sstevel@tonic-gate 	    &ipif->ipif_mtu,		/* max frag */
2539*4714Ssowmini 	    NULL,			/* no src nce */
25400Sstevel@tonic-gate 	    NULL,			/* no recv from queue */
25410Sstevel@tonic-gate 	    stq,			/* send-to queue */
25420Sstevel@tonic-gate 	    ill->ill_net_type,		/* IF_[NO]RESOLVER */
25430Sstevel@tonic-gate 	    ipif,
25440Sstevel@tonic-gate 	    NULL,
25450Sstevel@tonic-gate 	    0,
25460Sstevel@tonic-gate 	    0,
25470Sstevel@tonic-gate 	    0,
25481676Sjpk 	    &ire_uinfo_null,
25491676Sjpk 	    NULL,
25503448Sdh155122 	    NULL,
25513448Sdh155122 	    ipst);
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	if (ire != NULL) {
25540Sstevel@tonic-gate 		ire_t *ret_ire;
25550Sstevel@tonic-gate 		int   error;
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 		/*
25580Sstevel@tonic-gate 		 * We don't need ipif_ire anymore. We need to delete
25590Sstevel@tonic-gate 		 * before we add so that ire_add does not detect
25600Sstevel@tonic-gate 		 * duplicates.
25610Sstevel@tonic-gate 		 */
25620Sstevel@tonic-gate 		ire_delete(ipif_ire);
25630Sstevel@tonic-gate 		ret_ire = ire;
25642535Ssangeeta 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
25650Sstevel@tonic-gate 		ASSERT(error == 0);
25660Sstevel@tonic-gate 		ASSERT(ret_ire == ire);
25670Sstevel@tonic-gate 		if (ret_ire != NULL) {
25680Sstevel@tonic-gate 			/* Held in ire_add */
25690Sstevel@tonic-gate 			ire_refrele(ret_ire);
25700Sstevel@tonic-gate 		}
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 	/*
25730Sstevel@tonic-gate 	 * Either we are falling through from above or could not
25740Sstevel@tonic-gate 	 * allocate a replacement.
25750Sstevel@tonic-gate 	 */
25760Sstevel@tonic-gate 	ire_refrele(ipif_ire);
25770Sstevel@tonic-gate 	if (ip6_asp_table_held)
25783448Sdh155122 		ip6_asp_table_refrele(ipst);
25790Sstevel@tonic-gate 	if (nipif_refheld)
25800Sstevel@tonic-gate 		ipif_refrele(nipif);
25810Sstevel@tonic-gate }
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate /*
25840Sstevel@tonic-gate  * This old_ipif is going away.
25850Sstevel@tonic-gate  *
25860Sstevel@tonic-gate  * Determine if any other ipif's are using our address as
25870Sstevel@tonic-gate  * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
25880Sstevel@tonic-gate  * IPIF_DEPRECATED).
25890Sstevel@tonic-gate  * Find the IRE_INTERFACE for such ipif's and recreate them
25900Sstevel@tonic-gate  * to use an different source address following the rules in
25910Sstevel@tonic-gate  * ipif_up_done_v6.
25920Sstevel@tonic-gate  *
25930Sstevel@tonic-gate  * This function takes an illgrp as an argument so that illgrp_delete
25940Sstevel@tonic-gate  * can call this to update source address even after deleting the
25950Sstevel@tonic-gate  * old_ipif->ipif_ill from the ill group.
25960Sstevel@tonic-gate  */
25970Sstevel@tonic-gate void
25980Sstevel@tonic-gate ipif_update_other_ipifs_v6(ipif_t *old_ipif, ill_group_t *illgrp)
25990Sstevel@tonic-gate {
26000Sstevel@tonic-gate 	ipif_t	*ipif;
26010Sstevel@tonic-gate 	ill_t	*ill;
26020Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	ASSERT(IAM_WRITER_IPIF(old_ipif));
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	ill = old_ipif->ipif_ill;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n",
26090Sstevel@tonic-gate 	    ill->ill_name,
26100Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr,
26110Sstevel@tonic-gate 	    buf, sizeof (buf))));
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	/*
26140Sstevel@tonic-gate 	 * If this part of a group, look at all ills as ipif_select_source
26150Sstevel@tonic-gate 	 * borrows a source address across all the ills in the group.
26160Sstevel@tonic-gate 	 */
26170Sstevel@tonic-gate 	if (illgrp != NULL)
26180Sstevel@tonic-gate 		ill = illgrp->illgrp_ill;
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	/* Don't need a lock since this is a writer */
26210Sstevel@tonic-gate 	for (; ill != NULL; ill = ill->ill_group_next) {
26220Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
26230Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 			if (ipif == old_ipif)
26260Sstevel@tonic-gate 				continue;
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 			ipif_recreate_interface_routes_v6(old_ipif, ipif);
26290Sstevel@tonic-gate 		}
26300Sstevel@tonic-gate 	}
26310Sstevel@tonic-gate }
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate /*
26340Sstevel@tonic-gate  * Perform an attach and bind to get phys addr plus info_req for
26350Sstevel@tonic-gate  * the physical device.
26360Sstevel@tonic-gate  * q and mp represents an ioctl which will be queued waiting for
26370Sstevel@tonic-gate  * completion of the DLPI message exchange.
26380Sstevel@tonic-gate  * MUST be called on an ill queue. Can not set conn_pending_ill for that
26390Sstevel@tonic-gate  * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q.
26400Sstevel@tonic-gate  *
26410Sstevel@tonic-gate  * Returns EINPROGRESS when mp has been consumed by queueing it on
26420Sstevel@tonic-gate  * ill_pending_mp and the ioctl will complete in ip_rput.
26430Sstevel@tonic-gate  */
26440Sstevel@tonic-gate int
26450Sstevel@tonic-gate ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
26460Sstevel@tonic-gate {
26470Sstevel@tonic-gate 	mblk_t	*v6token_mp = NULL;
26480Sstevel@tonic-gate 	mblk_t	*v6lla_mp = NULL;
26490Sstevel@tonic-gate 	mblk_t	*phys_mp = NULL;
26500Sstevel@tonic-gate 	mblk_t	*info_mp = NULL;
26510Sstevel@tonic-gate 	mblk_t	*attach_mp = NULL;
26520Sstevel@tonic-gate 	mblk_t	*bind_mp = NULL;
26530Sstevel@tonic-gate 	mblk_t	*unbind_mp = NULL;
26540Sstevel@tonic-gate 	mblk_t	*notify_mp = NULL;
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
26570Sstevel@tonic-gate 	ASSERT(ill->ill_dlpi_style_set);
26580Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	if (ill->ill_isv6) {
26610Sstevel@tonic-gate 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
26620Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
26630Sstevel@tonic-gate 		if (v6token_mp == NULL)
26640Sstevel@tonic-gate 			goto bad;
26650Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
26660Sstevel@tonic-gate 		    DL_IPV6_TOKEN;
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
26690Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
26700Sstevel@tonic-gate 		if (v6lla_mp == NULL)
26710Sstevel@tonic-gate 			goto bad;
26720Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
26730Sstevel@tonic-gate 		    DL_IPV6_LINK_LAYER_ADDR;
26740Sstevel@tonic-gate 	}
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	/*
26770Sstevel@tonic-gate 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
26780Sstevel@tonic-gate 	 */
26790Sstevel@tonic-gate 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
26800Sstevel@tonic-gate 	    DL_NOTIFY_REQ);
26810Sstevel@tonic-gate 	if (notify_mp == NULL)
26820Sstevel@tonic-gate 		goto bad;
26830Sstevel@tonic-gate 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
26840Sstevel@tonic-gate 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
26850Sstevel@tonic-gate 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG);
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
26880Sstevel@tonic-gate 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
26890Sstevel@tonic-gate 	if (phys_mp == NULL)
26900Sstevel@tonic-gate 		goto bad;
26910Sstevel@tonic-gate 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
26920Sstevel@tonic-gate 	    DL_CURR_PHYS_ADDR;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	info_mp = ip_dlpi_alloc(
26950Sstevel@tonic-gate 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
26960Sstevel@tonic-gate 	    DL_INFO_REQ);
26970Sstevel@tonic-gate 	if (info_mp == NULL)
26980Sstevel@tonic-gate 		goto bad;
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
27010Sstevel@tonic-gate 	    DL_BIND_REQ);
27020Sstevel@tonic-gate 	if (bind_mp == NULL)
27030Sstevel@tonic-gate 		goto bad;
27040Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
27050Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
27080Sstevel@tonic-gate 	if (unbind_mp == NULL)
27090Sstevel@tonic-gate 		goto bad;
27100Sstevel@tonic-gate 
27114360Smeem 	/* If we need to attach, pre-alloc and initialize the mblk */
27120Sstevel@tonic-gate 	if (ill->ill_needs_attach) {
27130Sstevel@tonic-gate 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
27140Sstevel@tonic-gate 		    DL_ATTACH_REQ);
27150Sstevel@tonic-gate 		if (attach_mp == NULL)
27160Sstevel@tonic-gate 			goto bad;
27170Sstevel@tonic-gate 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
27180Sstevel@tonic-gate 	}
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	/*
27210Sstevel@tonic-gate 	 * Here we are going to delay the ioctl ack until after
27220Sstevel@tonic-gate 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
27230Sstevel@tonic-gate 	 * original ioctl message before sending the requests
27240Sstevel@tonic-gate 	 */
27250Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
27260Sstevel@tonic-gate 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
27270Sstevel@tonic-gate 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
27280Sstevel@tonic-gate 	/*
27290Sstevel@tonic-gate 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
27300Sstevel@tonic-gate 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
27310Sstevel@tonic-gate 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
27320Sstevel@tonic-gate 	 */
27330Sstevel@tonic-gate 	ill->ill_phys_addr_pend = 0;
27340Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	if (attach_mp != NULL) {
27370Sstevel@tonic-gate 		ip1dbg(("ill_dl_phys: attach\n"));
27380Sstevel@tonic-gate 		ill_dlpi_send(ill, attach_mp);
27390Sstevel@tonic-gate 	}
27400Sstevel@tonic-gate 	ill_dlpi_send(ill, bind_mp);
27410Sstevel@tonic-gate 	ill_dlpi_send(ill, info_mp);
27420Sstevel@tonic-gate 	if (ill->ill_isv6) {
27430Sstevel@tonic-gate 		ill_dlpi_send(ill, v6token_mp);
27440Sstevel@tonic-gate 		ill_dlpi_send(ill, v6lla_mp);
27450Sstevel@tonic-gate 	}
27460Sstevel@tonic-gate 	ill_dlpi_send(ill, phys_mp);
27470Sstevel@tonic-gate 	ill_dlpi_send(ill, notify_mp);
27480Sstevel@tonic-gate 	ill_dlpi_send(ill, unbind_mp);
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 	/*
27510Sstevel@tonic-gate 	 * This operation will complete in ip_rput_dlpi_writer with either
27520Sstevel@tonic-gate 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
27530Sstevel@tonic-gate 	 */
27540Sstevel@tonic-gate 	return (EINPROGRESS);
27550Sstevel@tonic-gate bad:
27564360Smeem 	freemsg(v6token_mp);
27574360Smeem 	freemsg(v6lla_mp);
27584360Smeem 	freemsg(phys_mp);
27594360Smeem 	freemsg(info_mp);
27604360Smeem 	freemsg(attach_mp);
27614360Smeem 	freemsg(bind_mp);
27624360Smeem 	freemsg(unbind_mp);
27634360Smeem 	freemsg(notify_mp);
27640Sstevel@tonic-gate 	return (ENOMEM);
27650Sstevel@tonic-gate }
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate /*
27700Sstevel@tonic-gate  * DLPI is up.
27710Sstevel@tonic-gate  * Create all the IREs associated with an interface bring up multicast.
27720Sstevel@tonic-gate  * Set the interface flag and finish other initialization
27730Sstevel@tonic-gate  * that potentially had to be differed to after DL_BIND_ACK.
27740Sstevel@tonic-gate  */
27750Sstevel@tonic-gate int
27760Sstevel@tonic-gate ipif_up_done_v6(ipif_t *ipif)
27770Sstevel@tonic-gate {
27780Sstevel@tonic-gate 	ire_t	*ire_array[20];
27790Sstevel@tonic-gate 	ire_t	**irep = ire_array;
27800Sstevel@tonic-gate 	ire_t	**irep1;
27810Sstevel@tonic-gate 	ill_t	*ill = ipif->ipif_ill;
27820Sstevel@tonic-gate 	queue_t	*stq;
27830Sstevel@tonic-gate 	in6_addr_t	v6addr;
27840Sstevel@tonic-gate 	in6_addr_t	route_mask;
27850Sstevel@tonic-gate 	ipif_t	 *src_ipif = NULL;
27860Sstevel@tonic-gate 	ipif_t   *tmp_ipif;
27870Sstevel@tonic-gate 	boolean_t	flush_ire_cache = B_TRUE;
27880Sstevel@tonic-gate 	int	err;
27890Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
27900Sstevel@tonic-gate 	phyint_t *phyi;
27910Sstevel@tonic-gate 	ire_t	**ipif_saved_irep = NULL;
27920Sstevel@tonic-gate 	int ipif_saved_ire_cnt;
27930Sstevel@tonic-gate 	int cnt;
27940Sstevel@tonic-gate 	boolean_t src_ipif_held = B_FALSE;
27950Sstevel@tonic-gate 	boolean_t ire_added = B_FALSE;
27960Sstevel@tonic-gate 	boolean_t loopback = B_FALSE;
27970Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
27983448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
28014459Skcpoon 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	/* Check if this is a loopback interface */
28040Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_wq == NULL)
28050Sstevel@tonic-gate 		loopback = B_TRUE;
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
28080Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	/*
28110Sstevel@tonic-gate 	 * If all other interfaces for this ill are down or DEPRECATED,
28120Sstevel@tonic-gate 	 * or otherwise unsuitable for source address selection, remove
28130Sstevel@tonic-gate 	 * any IRE_CACHE entries for this ill to make sure source
28140Sstevel@tonic-gate 	 * address selection gets to take this new ipif into account.
28150Sstevel@tonic-gate 	 * No need to hold ill_lock while traversing the ipif list since
28160Sstevel@tonic-gate 	 * we are writer
28170Sstevel@tonic-gate 	 */
28180Sstevel@tonic-gate 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
28194459Skcpoon 	    tmp_ipif = tmp_ipif->ipif_next) {
28200Sstevel@tonic-gate 		if (((tmp_ipif->ipif_flags &
28210Sstevel@tonic-gate 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
28220Sstevel@tonic-gate 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
28230Sstevel@tonic-gate 		    (tmp_ipif == ipif))
28240Sstevel@tonic-gate 			continue;
28250Sstevel@tonic-gate 		/* first useable pre-existing interface */
28260Sstevel@tonic-gate 		flush_ire_cache = B_FALSE;
28270Sstevel@tonic-gate 		break;
28280Sstevel@tonic-gate 	}
28290Sstevel@tonic-gate 	if (flush_ire_cache)
28300Sstevel@tonic-gate 		ire_walk_ill_v6(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
28310Sstevel@tonic-gate 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	/*
28340Sstevel@tonic-gate 	 * Figure out which way the send-to queue should go.  Only
28350Sstevel@tonic-gate 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here.
28360Sstevel@tonic-gate 	 */
28370Sstevel@tonic-gate 	switch (ill->ill_net_type) {
28380Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
28390Sstevel@tonic-gate 		stq = ill->ill_rq;
28400Sstevel@tonic-gate 		break;
28410Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
28420Sstevel@tonic-gate 	case IRE_LOOPBACK:
28430Sstevel@tonic-gate 		stq = ill->ill_wq;
28440Sstevel@tonic-gate 		break;
28450Sstevel@tonic-gate 	default:
28460Sstevel@tonic-gate 		return (EINVAL);
28470Sstevel@tonic-gate 	}
28480Sstevel@tonic-gate 
28494459Skcpoon 	if (IS_LOOPBACK(ill)) {
28500Sstevel@tonic-gate 		/*
28510Sstevel@tonic-gate 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
28520Sstevel@tonic-gate 		 * ipif_lookup_on_name(), but in the case of zones we can have
28530Sstevel@tonic-gate 		 * several loopback addresses on lo0. So all the interfaces with
28540Sstevel@tonic-gate 		 * loopback addresses need to be marked IRE_LOOPBACK.
28550Sstevel@tonic-gate 		 */
28560Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
28570Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOOPBACK;
28580Sstevel@tonic-gate 		else
28590Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOCAL;
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
28630Sstevel@tonic-gate 		/*
28640Sstevel@tonic-gate 		 * Can't use our source address. Select a different
28650Sstevel@tonic-gate 		 * source address for the IRE_INTERFACE and IRE_LOCAL
28660Sstevel@tonic-gate 		 */
28673448Sdh155122 		if (ip6_asp_can_lookup(ipst)) {
28680Sstevel@tonic-gate 			ip6_asp_table_held = B_TRUE;
28690Sstevel@tonic-gate 			src_ipif = ipif_select_source_v6(ipif->ipif_ill,
28702202Srk129064 			    &ipif->ipif_v6subnet, RESTRICT_TO_NONE,
28710Sstevel@tonic-gate 			    IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
28720Sstevel@tonic-gate 		}
28730Sstevel@tonic-gate 		if (src_ipif == NULL)
28740Sstevel@tonic-gate 			src_ipif = ipif;	/* Last resort */
28750Sstevel@tonic-gate 		else
28760Sstevel@tonic-gate 			src_ipif_held = B_TRUE;
28770Sstevel@tonic-gate 	} else {
28780Sstevel@tonic-gate 		src_ipif = ipif;
28790Sstevel@tonic-gate 	}
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
28820Sstevel@tonic-gate 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
28831676Sjpk 
28841676Sjpk 		/*
28851676Sjpk 		 * If we're on a labeled system then make sure that zone-
28861676Sjpk 		 * private addresses have proper remote host database entries.
28871676Sjpk 		 */
28881676Sjpk 		if (is_system_labeled() &&
28891676Sjpk 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
28901676Sjpk 			if (ip6opt_ls == 0) {
28911676Sjpk 				cmn_err(CE_WARN, "IPv6 not enabled "
28921676Sjpk 				    "via /etc/system");
28931676Sjpk 				return (EINVAL);
28941676Sjpk 			}
28951676Sjpk 			if (!tsol_check_interface_address(ipif))
28961676Sjpk 				return (EINVAL);
28971676Sjpk 		}
28981676Sjpk 
28990Sstevel@tonic-gate 		/* Register the source address for __sin6_src_id */
29000Sstevel@tonic-gate 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
29013448Sdh155122 		    ipif->ipif_zoneid, ipst);
29020Sstevel@tonic-gate 		if (err != 0) {
29030Sstevel@tonic-gate 			ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err));
29040Sstevel@tonic-gate 			if (src_ipif_held)
29050Sstevel@tonic-gate 				ipif_refrele(src_ipif);
29060Sstevel@tonic-gate 			if (ip6_asp_table_held)
29073448Sdh155122 				ip6_asp_table_refrele(ipst);
29080Sstevel@tonic-gate 			return (err);
29090Sstevel@tonic-gate 		}
29100Sstevel@tonic-gate 		/*
29110Sstevel@tonic-gate 		 * If the interface address is set, create the LOCAL
29120Sstevel@tonic-gate 		 * or LOOPBACK IRE.
29130Sstevel@tonic-gate 		 */
29140Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n",
29150Sstevel@tonic-gate 		    ipif->ipif_ire_type,
29160Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
29170Sstevel@tonic-gate 		    buf, sizeof (buf))));
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 		*irep++ = ire_create_v6(
29200Sstevel@tonic-gate 		    &ipif->ipif_v6lcl_addr,		/* dest address */
29210Sstevel@tonic-gate 		    &ipv6_all_ones,			/* mask */
29220Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,		/* source address */
29230Sstevel@tonic-gate 		    NULL,				/* no gateway */
29240Sstevel@tonic-gate 		    &ip_loopback_mtu_v6plus,		/* max frag size */
29250Sstevel@tonic-gate 		    NULL,
29260Sstevel@tonic-gate 		    ipif->ipif_rq,			/* recv-from queue */
29270Sstevel@tonic-gate 		    NULL,				/* no send-to queue */
29280Sstevel@tonic-gate 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
29290Sstevel@tonic-gate 		    ipif,				/* interface */
29300Sstevel@tonic-gate 		    NULL,
29310Sstevel@tonic-gate 		    0,
29320Sstevel@tonic-gate 		    0,
29330Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
29341676Sjpk 		    &ire_uinfo_null,
29351676Sjpk 		    NULL,
29363448Sdh155122 		    NULL,
29373448Sdh155122 		    ipst);
29380Sstevel@tonic-gate 	}
29390Sstevel@tonic-gate 
29400Sstevel@tonic-gate 	/*
29410Sstevel@tonic-gate 	 * Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate.
29420Sstevel@tonic-gate 	 * Note that atun interfaces have an all-zero ipif_v6subnet.
29430Sstevel@tonic-gate 	 * Thus we allow a zero subnet as long as the mask is non-zero.
29440Sstevel@tonic-gate 	 */
29450Sstevel@tonic-gate 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
29460Sstevel@tonic-gate 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
29470Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
29480Sstevel@tonic-gate 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
29490Sstevel@tonic-gate 		v6addr = ipif->ipif_v6subnet;
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
29520Sstevel@tonic-gate 			route_mask = ipv6_all_ones;
29530Sstevel@tonic-gate 		} else {
29540Sstevel@tonic-gate 			route_mask = ipif->ipif_v6net_mask;
29550Sstevel@tonic-gate 		}
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n",
29580Sstevel@tonic-gate 		    ill->ill_net_type,
29590Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 		*irep++ = ire_create_v6(
29620Sstevel@tonic-gate 		    &v6addr,			/* dest pref */
29630Sstevel@tonic-gate 		    &route_mask,		/* mask */
29640Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,	/* src addr */
29650Sstevel@tonic-gate 		    NULL,			/* no gateway */
29660Sstevel@tonic-gate 		    &ipif->ipif_mtu,		/* max frag */
2967*4714Ssowmini 		    NULL,			/* no src nce */
29680Sstevel@tonic-gate 		    NULL,			/* no recv from queue */
29690Sstevel@tonic-gate 		    stq,			/* send-to queue */
29700Sstevel@tonic-gate 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
29710Sstevel@tonic-gate 		    ipif,
29720Sstevel@tonic-gate 		    NULL,
29730Sstevel@tonic-gate 		    0,
29740Sstevel@tonic-gate 		    0,
29750Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
29761676Sjpk 		    &ire_uinfo_null,
29771676Sjpk 		    NULL,
29783448Sdh155122 		    NULL,
29793448Sdh155122 		    ipst);
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	/*
29830Sstevel@tonic-gate 	 * Setup 2002::/16 route, if this interface is a 6to4 tunnel
29840Sstevel@tonic-gate 	 */
29850Sstevel@tonic-gate 	if (IN6_IS_ADDR_6TO4(&ipif->ipif_v6lcl_addr) &&
29860Sstevel@tonic-gate 	    (ill->ill_is_6to4tun)) {
29870Sstevel@tonic-gate 		/*
29880Sstevel@tonic-gate 		 * Destination address is 2002::/16
29890Sstevel@tonic-gate 		 */
29900Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
29910Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x20020000U, 0, 0, 0 };
29920Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0xffff0000U, 0, 0, 0 };
29930Sstevel@tonic-gate #else
29940Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x00000220U, 0, 0, 0 };
29950Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0x0000ffffU, 0, 0, 0 };
29960Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
29970Sstevel@tonic-gate 		char	buf2[INET6_ADDRSTRLEN];
29980Sstevel@tonic-gate 		ire_t *isdup;
29990Sstevel@tonic-gate 		in6_addr_t *first_addr = &ill->ill_ipif->ipif_v6lcl_addr;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 		/*
30020Sstevel@tonic-gate 		 * check to see if this route has already been added for
30030Sstevel@tonic-gate 		 * this tunnel interface.
30040Sstevel@tonic-gate 		 */
30050Sstevel@tonic-gate 		isdup = ire_ftable_lookup_v6(first_addr, &prefix_mask, 0,
30061676Sjpk 		    IRE_IF_NORESOLVER, ill->ill_ipif, NULL, ALL_ZONES, 0, NULL,
30073448Sdh155122 		    (MATCH_IRE_SRC | MATCH_IRE_MASK), ipst);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 		if (isdup == NULL) {
30100Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s",
30110Sstevel@tonic-gate 			    IRE_IF_NORESOLVER, inet_ntop(AF_INET6, &v6addr,
30124459Skcpoon 			    buf2, sizeof (buf2))));
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 			*irep++ = ire_create_v6(
30150Sstevel@tonic-gate 			    &prefix_addr,		/* 2002:: */
30160Sstevel@tonic-gate 			    &prefix_mask,		/* ffff:: */
30170Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, 	/* src addr */
30180Sstevel@tonic-gate 			    NULL, 			/* gateway */
30190Sstevel@tonic-gate 			    &ipif->ipif_mtu, 		/* max_frag */
3020*4714Ssowmini 			    NULL, 			/* no src nce */
30210Sstevel@tonic-gate 			    NULL, 			/* no rfq */
30220Sstevel@tonic-gate 			    ill->ill_wq, 		/* stq */
30230Sstevel@tonic-gate 			    IRE_IF_NORESOLVER,		/* type */
30240Sstevel@tonic-gate 			    ipif,			/* interface */
30250Sstevel@tonic-gate 			    NULL,			/* v6cmask */
30260Sstevel@tonic-gate 			    0,
30270Sstevel@tonic-gate 			    0,
30280Sstevel@tonic-gate 			    RTF_UP,
30291676Sjpk 			    &ire_uinfo_null,
30301676Sjpk 			    NULL,
30313448Sdh155122 			    NULL,
30323448Sdh155122 			    ipst);
30330Sstevel@tonic-gate 		} else {
30340Sstevel@tonic-gate 			ire_refrele(isdup);
30350Sstevel@tonic-gate 		}
30360Sstevel@tonic-gate 	}
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	/* If an earlier ire_create failed, get out now */
30390Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
30400Sstevel@tonic-gate 		irep1--;
30410Sstevel@tonic-gate 		if (*irep1 == NULL) {
30420Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: NULL ire found in"
30430Sstevel@tonic-gate 			    " ire_array\n"));
30440Sstevel@tonic-gate 			err = ENOMEM;
30450Sstevel@tonic-gate 			goto bad;
30460Sstevel@tonic-gate 		}
30470Sstevel@tonic-gate 	}
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	/*
30520Sstevel@tonic-gate 	 * Need to atomically check for ip_addr_availablity_check
30530Sstevel@tonic-gate 	 * now under ill_g_lock, and if it fails got bad, and remove
30540Sstevel@tonic-gate 	 * from group also
30550Sstevel@tonic-gate 	 */
30563448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
30573448Sdh155122 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
30580Sstevel@tonic-gate 	ill->ill_ipif_up_count++;
30590Sstevel@tonic-gate 	ipif->ipif_flags |= IPIF_UP;
30600Sstevel@tonic-gate 	err = ip_addr_availability_check(ipif);
30613448Sdh155122 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
30623448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	if (err != 0) {
30650Sstevel@tonic-gate 		/*
30660Sstevel@tonic-gate 		 * Our address may already be up on the same ill. In this case,
30670Sstevel@tonic-gate 		 * the external resolver entry for our ipif replaced the one for
30680Sstevel@tonic-gate 		 * the other ipif. So we don't want to delete it (otherwise the
30690Sstevel@tonic-gate 		 * other ipif would be unable to send packets).
30700Sstevel@tonic-gate 		 * ip_addr_availability_check() identifies this case for us and
30710Sstevel@tonic-gate 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
30720Sstevel@tonic-gate 		 * which is the expected error code.
30730Sstevel@tonic-gate 		 */
30740Sstevel@tonic-gate 		if (err == EADDRINUSE) {
30750Sstevel@tonic-gate 			if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) {
30760Sstevel@tonic-gate 				freemsg(ipif->ipif_arp_del_mp);
30770Sstevel@tonic-gate 				ipif->ipif_arp_del_mp = NULL;
30780Sstevel@tonic-gate 			}
30790Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
30800Sstevel@tonic-gate 		}
30810Sstevel@tonic-gate 		ill->ill_ipif_up_count--;
30820Sstevel@tonic-gate 		ipif->ipif_flags &= ~IPIF_UP;
30830Sstevel@tonic-gate 		goto bad;
30840Sstevel@tonic-gate 	}
30850Sstevel@tonic-gate 
30860Sstevel@tonic-gate 	/*
30870Sstevel@tonic-gate 	 * Add in all newly created IREs. We want to add before
30880Sstevel@tonic-gate 	 * we call ifgrp_insert which wants to know whether
30890Sstevel@tonic-gate 	 * IRE_IF_RESOLVER exists or not.
30900Sstevel@tonic-gate 	 *
30910Sstevel@tonic-gate 	 * NOTE : We refrele the ire though we may branch to "bad"
30920Sstevel@tonic-gate 	 *	  later on where we do ire_delete. This is okay
30930Sstevel@tonic-gate 	 *	  because nobody can delete it as we are running
30940Sstevel@tonic-gate 	 *	  exclusively.
30950Sstevel@tonic-gate 	 */
30960Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
30970Sstevel@tonic-gate 		irep1--;
30980Sstevel@tonic-gate 		/* Shouldn't be adding any bcast ire's */
30990Sstevel@tonic-gate 		ASSERT((*irep1)->ire_type != IRE_BROADCAST);
31000Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
31010Sstevel@tonic-gate 		/*
31020Sstevel@tonic-gate 		 * refheld by ire_add. refele towards the end of the func
31030Sstevel@tonic-gate 		 */
31042535Ssangeeta 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
31050Sstevel@tonic-gate 	}
31060Sstevel@tonic-gate 	if (ip6_asp_table_held) {
31073448Sdh155122 		ip6_asp_table_refrele(ipst);
31080Sstevel@tonic-gate 		ip6_asp_table_held = B_FALSE;
31090Sstevel@tonic-gate 	}
31100Sstevel@tonic-gate 	ire_added = B_TRUE;
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 	/*
31130Sstevel@tonic-gate 	 * Form groups if possible.
31140Sstevel@tonic-gate 	 *
31150Sstevel@tonic-gate 	 * If we are supposed to be in a ill_group with a name, insert it
31160Sstevel@tonic-gate 	 * now as we know that at least one ipif is UP. Otherwise form
31170Sstevel@tonic-gate 	 * nameless groups.
31180Sstevel@tonic-gate 	 *
31190Sstevel@tonic-gate 	 * If ip_enable_group_ifs is set and ipif address is not ::0, insert
31200Sstevel@tonic-gate 	 * this ipif into the appropriate interface group, or create a
31210Sstevel@tonic-gate 	 * new one. If this is already in a nameless group, we try to form
31220Sstevel@tonic-gate 	 * a bigger group looking at other ills potentially sharing this
31230Sstevel@tonic-gate 	 * ipif's prefix.
31240Sstevel@tonic-gate 	 */
31250Sstevel@tonic-gate 	phyi = ill->ill_phyint;
31260Sstevel@tonic-gate 	if (phyi->phyint_groupname_len != 0) {
31270Sstevel@tonic-gate 		ASSERT(phyi->phyint_groupname != NULL);
31280Sstevel@tonic-gate 		if (ill->ill_ipif_up_count == 1) {
31290Sstevel@tonic-gate 			ASSERT(ill->ill_group == NULL);
31303448Sdh155122 			err = illgrp_insert(&ipst->ips_illgrp_head_v6, ill,
31310Sstevel@tonic-gate 			    phyi->phyint_groupname, NULL, B_TRUE);
31320Sstevel@tonic-gate 			if (err != 0) {
31330Sstevel@tonic-gate 				ip1dbg(("ipif_up_done_v6: illgrp allocation "
31340Sstevel@tonic-gate 				    "failed, error %d\n", err));
31350Sstevel@tonic-gate 				goto bad;
31360Sstevel@tonic-gate 			}
31370Sstevel@tonic-gate 		}
31380Sstevel@tonic-gate 		ASSERT(ill->ill_group != NULL);
31390Sstevel@tonic-gate 	}
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
31420Sstevel@tonic-gate 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
31430Sstevel@tonic-gate 	ipif_saved_irep = ipif_recover_ire_v6(ipif);
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
31460Sstevel@tonic-gate 		/*
31470Sstevel@tonic-gate 		 * Need to recover all multicast memberships in the driver.
31480Sstevel@tonic-gate 		 * This had to be deferred until we had attached.
31490Sstevel@tonic-gate 		 */
31500Sstevel@tonic-gate 		ill_recover_multicast(ill);
31510Sstevel@tonic-gate 	}
31520Sstevel@tonic-gate 	/* Join the allhosts multicast address and the solicited node MC */
31530Sstevel@tonic-gate 	ipif_multicast_up(ipif);
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 	if (!loopback) {
31560Sstevel@tonic-gate 		/*
31570Sstevel@tonic-gate 		 * See whether anybody else would benefit from the
31580Sstevel@tonic-gate 		 * new ipif that we added. We call this always rather
31590Sstevel@tonic-gate 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
31600Sstevel@tonic-gate 		 * ipif for the benefit of illgrp_insert (done above)
31610Sstevel@tonic-gate 		 * which does not do source address selection as it does
31620Sstevel@tonic-gate 		 * not want to re-create interface routes that we are
31630Sstevel@tonic-gate 		 * having reference to it here.
31640Sstevel@tonic-gate 		 */
31650Sstevel@tonic-gate 		ill_update_source_selection(ill);
31660Sstevel@tonic-gate 	}
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
31690Sstevel@tonic-gate 		irep1--;
31700Sstevel@tonic-gate 		if (*irep1 != NULL) {
31710Sstevel@tonic-gate 			/* was held in ire_add */
31720Sstevel@tonic-gate 			ire_refrele(*irep1);
31730Sstevel@tonic-gate 		}
31740Sstevel@tonic-gate 	}
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	cnt = ipif_saved_ire_cnt;
31770Sstevel@tonic-gate 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
31780Sstevel@tonic-gate 		if (*irep1 != NULL) {
31790Sstevel@tonic-gate 			/* was held in ire_add */
31800Sstevel@tonic-gate 			ire_refrele(*irep1);
31810Sstevel@tonic-gate 		}
31820Sstevel@tonic-gate 	}
31830Sstevel@tonic-gate 
31842546Scarlsonj 	if (ipif->ipif_addr_ready) {
31852546Scarlsonj 		ip_rts_ifmsg(ipif);
31862546Scarlsonj 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
31872546Scarlsonj 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
31882546Scarlsonj 	}
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
31910Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
31920Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
31930Sstevel@tonic-gate 	}
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 	if (src_ipif_held)
31960Sstevel@tonic-gate 		ipif_refrele(src_ipif);
31970Sstevel@tonic-gate 	return (0);
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate bad:
32000Sstevel@tonic-gate 	if (ip6_asp_table_held)
32013448Sdh155122 		ip6_asp_table_refrele(ipst);
32020Sstevel@tonic-gate 	/*
32030Sstevel@tonic-gate 	 * We don't have to bother removing from ill groups because
32040Sstevel@tonic-gate 	 *
32050Sstevel@tonic-gate 	 * 1) For groups with names, we insert only when the first ipif
32060Sstevel@tonic-gate 	 *    comes up. In that case if it fails, it will not be in any
32070Sstevel@tonic-gate 	 *    group. So, we need not try to remove for that case.
32080Sstevel@tonic-gate 	 *
32090Sstevel@tonic-gate 	 * 2) For groups without names, either we tried to insert ipif_ill
32100Sstevel@tonic-gate 	 *    in a group as singleton or found some other group to become
32110Sstevel@tonic-gate 	 *    a bigger group. For the former, if it fails we don't have
32120Sstevel@tonic-gate 	 *    anything to do as ipif_ill is not in the group and for the
32130Sstevel@tonic-gate 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
32140Sstevel@tonic-gate 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
32150Sstevel@tonic-gate 	 */
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	while (irep > ire_array) {
32180Sstevel@tonic-gate 		irep--;
32190Sstevel@tonic-gate 		if (*irep != NULL) {
32200Sstevel@tonic-gate 			ire_delete(*irep);
32210Sstevel@tonic-gate 			if (ire_added)
32220Sstevel@tonic-gate 				ire_refrele(*irep);
32230Sstevel@tonic-gate 		}
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	}
32263448Sdh155122 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
32290Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
32300Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
32310Sstevel@tonic-gate 	}
32320Sstevel@tonic-gate 	if (src_ipif_held)
32330Sstevel@tonic-gate 		ipif_refrele(src_ipif);
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 	ipif_ndp_down(ipif);
32360Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
32370Sstevel@tonic-gate 		ipif_arp_down(ipif);
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 	return (err);
32400Sstevel@tonic-gate }
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate /*
32430Sstevel@tonic-gate  * Delete an ND entry and the corresponding IRE_CACHE entry if it exists.
32440Sstevel@tonic-gate  */
32450Sstevel@tonic-gate /* ARGSUSED */
32460Sstevel@tonic-gate int
32470Sstevel@tonic-gate ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
32480Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
32490Sstevel@tonic-gate {
32500Sstevel@tonic-gate 	in6_addr_t	addr;
32510Sstevel@tonic-gate 	sin6_t		*sin6;
32520Sstevel@tonic-gate 	nce_t		*nce;
32530Sstevel@tonic-gate 	struct lifreq	*lifr;
32540Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
32550Sstevel@tonic-gate 	mblk_t	*mp1;
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate 	mp1 = mp->b_cont->b_cont;
32580Sstevel@tonic-gate 	lifr = (struct lifreq *)mp1->b_rptr;
32590Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
32600Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
32610Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
32620Sstevel@tonic-gate 		return (EINVAL);
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
32650Sstevel@tonic-gate 		return (EINVAL);
32660Sstevel@tonic-gate 
32670Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
32680Sstevel@tonic-gate 		return (EAFNOSUPPORT);
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate 	sin6 = (sin6_t *)&lnr->lnr_addr;
32710Sstevel@tonic-gate 	addr = sin6->sin6_addr;
32722535Ssangeeta 	nce = ndp_lookup_v6(ipif->ipif_ill, &addr, B_FALSE);
32730Sstevel@tonic-gate 	if (nce == NULL)
32740Sstevel@tonic-gate 		return (ESRCH);
32750Sstevel@tonic-gate 	ndp_delete(nce);
32760Sstevel@tonic-gate 	NCE_REFRELE(nce);
32770Sstevel@tonic-gate 	return (0);
32780Sstevel@tonic-gate }
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate /*
32810Sstevel@tonic-gate  * Return nbr cache info.
32820Sstevel@tonic-gate  */
32830Sstevel@tonic-gate /* ARGSUSED */
32840Sstevel@tonic-gate int
32850Sstevel@tonic-gate ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
32860Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
32870Sstevel@tonic-gate {
32880Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
32890Sstevel@tonic-gate 	struct lifreq	*lifr;
32900Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
32930Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
32940Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
32950Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
32960Sstevel@tonic-gate 		return (EINVAL);
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
32990Sstevel@tonic-gate 		return (EINVAL);
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
33020Sstevel@tonic-gate 		return (EAFNOSUPPORT);
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
33050Sstevel@tonic-gate 		return (EINVAL);
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	return (ndp_query(ill, lnr));
33080Sstevel@tonic-gate }
33090Sstevel@tonic-gate 
33100Sstevel@tonic-gate /*
33110Sstevel@tonic-gate  * Perform an update of the nd entry for the specified address.
33120Sstevel@tonic-gate  */
33130Sstevel@tonic-gate /* ARGSUSED */
33140Sstevel@tonic-gate int
33150Sstevel@tonic-gate ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
33160Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
33170Sstevel@tonic-gate {
33180Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
33190Sstevel@tonic-gate 	struct	lifreq	*lifr;
33200Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
33210Sstevel@tonic-gate 
33223448Sdh155122 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
33233448Sdh155122 
33240Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
33250Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
33260Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
33270Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
33280Sstevel@tonic-gate 		return (EINVAL);
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
33310Sstevel@tonic-gate 		return (EINVAL);
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
33340Sstevel@tonic-gate 		return (EAFNOSUPPORT);
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 	return (ndp_sioc_update(ill, lnr));
33370Sstevel@tonic-gate }
3338