xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_if.c (revision 9743:792abd8168ea)
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 /*
228485SPeter.Memishian@Sun.COM  * Copyright 2009 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 /*
300Sstevel@tonic-gate  * This file contains the interface control functions for IPv6.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/stream.h>
360Sstevel@tonic-gate #include <sys/dlpi.h>
370Sstevel@tonic-gate #include <sys/stropts.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/kstat.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/zone.h>
433448Sdh155122 #include <sys/policy.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/systm.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/socket.h>
480Sstevel@tonic-gate #include <sys/isa_defs.h>
490Sstevel@tonic-gate #include <net/if.h>
500Sstevel@tonic-gate #include <net/if_dl.h>
510Sstevel@tonic-gate #include <net/route.h>
520Sstevel@tonic-gate #include <netinet/in.h>
530Sstevel@tonic-gate #include <netinet/igmp_var.h>
540Sstevel@tonic-gate #include <netinet/ip6.h>
550Sstevel@tonic-gate #include <netinet/icmp6.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <inet/common.h>
580Sstevel@tonic-gate #include <inet/nd.h>
590Sstevel@tonic-gate #include <inet/mib2.h>
600Sstevel@tonic-gate #include <inet/ip.h>
610Sstevel@tonic-gate #include <inet/ip6.h>
620Sstevel@tonic-gate #include <inet/ip_multi.h>
630Sstevel@tonic-gate #include <inet/ip_ire.h>
640Sstevel@tonic-gate #include <inet/ip_rts.h>
650Sstevel@tonic-gate #include <inet/ip_ndp.h>
660Sstevel@tonic-gate #include <inet/ip_if.h>
670Sstevel@tonic-gate #include <inet/ip6_asp.h>
680Sstevel@tonic-gate #include <inet/tun.h>
690Sstevel@tonic-gate #include <inet/ipclassifier.h>
700Sstevel@tonic-gate #include <inet/sctp_ip.h>
710Sstevel@tonic-gate 
721676Sjpk #include <sys/tsol/tndb.h>
731676Sjpk #include <sys/tsol/tnet.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static in6_addr_t	ipv6_ll_template =
760Sstevel@tonic-gate 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static ipif_t *
790Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
803448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
834459Skcpoon  * These two functions, ipif_lookup_group_v6() and ill_lookup_group_v6(),
844459Skcpoon  * are called when an application does not specify an interface to be
854459Skcpoon  * used for multicast traffic.  It calls ire_lookup_multi_v6() to look
864459Skcpoon  * for an interface route for the specified multicast group.  Doing
874459Skcpoon  * this allows the administrator to add prefix routes for multicast to
884459Skcpoon  * indicate which interface to be used for multicast traffic in the above
894459Skcpoon  * scenario.  The route could be for all multicast (ff00::/8), for a single
904459Skcpoon  * multicast group (a /128 route) or anything in between.  If there is no
914459Skcpoon  * such multicast route, we just find any multicast capable interface and
924459Skcpoon  * return it.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate ipif_t *
953448Sdh155122 ipif_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	ire_t	*ire;
980Sstevel@tonic-gate 	ipif_t	*ipif;
990Sstevel@tonic-gate 
1003448Sdh155122 	ire = ire_lookup_multi_v6(group, zoneid, ipst);
1014459Skcpoon 	if (ire != NULL) {
1024459Skcpoon 		ipif = ire->ire_ipif;
1034459Skcpoon 		ipif_refhold(ipif);
1044459Skcpoon 		ire_refrele(ire);
1054459Skcpoon 		return (ipif);
1064459Skcpoon 	}
1074459Skcpoon 
1084459Skcpoon 	return (ipif_lookup_multicast(ipst, zoneid, B_TRUE));
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate ill_t *
1123448Sdh155122 ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	ire_t	*ire;
1150Sstevel@tonic-gate 	ill_t	*ill;
1164459Skcpoon 	ipif_t	*ipif;
1170Sstevel@tonic-gate 
1183448Sdh155122 	ire = ire_lookup_multi_v6(group, zoneid, ipst);
1194459Skcpoon 	if (ire != NULL) {
1204459Skcpoon 		ill = ire->ire_ipif->ipif_ill;
1214459Skcpoon 		ill_refhold(ill);
1224459Skcpoon 		ire_refrele(ire);
1234459Skcpoon 		return (ill);
1244459Skcpoon 	}
1254459Skcpoon 
1264459Skcpoon 	ipif = ipif_lookup_multicast(ipst, zoneid, B_TRUE);
1274459Skcpoon 	if (ipif == NULL)
1280Sstevel@tonic-gate 		return (NULL);
1294459Skcpoon 
1304459Skcpoon 	ill = ipif->ipif_ill;
1310Sstevel@tonic-gate 	ill_refhold(ill);
1324459Skcpoon 	ipif_refrele(ipif);
1330Sstevel@tonic-gate 	return (ill);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Look for an ipif with the specified interface address and destination.
1380Sstevel@tonic-gate  * The destination address is used only for matching point-to-point interfaces.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate static ipif_t *
1410Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
1423448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	ipif_t	*ipif;
1450Sstevel@tonic-gate 	ill_t	*ill;
1460Sstevel@tonic-gate 	ipsq_t	*ipsq;
1470Sstevel@tonic-gate 	ill_walk_context_t ctx;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (error != NULL)
1500Sstevel@tonic-gate 		*error = 0;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * First match all the point-to-point interfaces
1540Sstevel@tonic-gate 	 * before looking at non-point-to-point interfaces.
1550Sstevel@tonic-gate 	 * This is done to avoid returning non-point-to-point
1560Sstevel@tonic-gate 	 * ipif instead of unnumbered point-to-point ipif.
1570Sstevel@tonic-gate 	 */
1583448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1593448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
1600Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1610Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
1620Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
1632733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
1642733Snordmark 		    ipif = ipif->ipif_next) {
1650Sstevel@tonic-gate 			/* Allow the ipif to be down */
1660Sstevel@tonic-gate 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
1670Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1680Sstevel@tonic-gate 			    if_addr)) &&
1690Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
1700Sstevel@tonic-gate 			    dst))) {
1710Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
1720Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
1730Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1740Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1753448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1760Sstevel@tonic-gate 					return (ipif);
1770Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
1780Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
1790Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
1808485SPeter.Memishian@Sun.COM 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
1810Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1823448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1830Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
1844459Skcpoon 					    ill);
1858485SPeter.Memishian@Sun.COM 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
1860Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
1870Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1885335Ssowmini 					if (error != NULL)
1895335Ssowmini 						*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 /*
2068485SPeter.Memishian@Sun.COM  * Common function for ipif_lookup_addr_v6() and ipif_lookup_addr_exact_v6().
2070Sstevel@tonic-gate  */
2088485SPeter.Memishian@Sun.COM static ipif_t *
2098485SPeter.Memishian@Sun.COM ipif_lookup_addr_common_v6(const in6_addr_t *addr, ill_t *match_ill,
2108485SPeter.Memishian@Sun.COM     boolean_t match_illgrp, zoneid_t zoneid, queue_t *q, mblk_t *mp,
2118485SPeter.Memishian@Sun.COM     ipsq_func_t func, int *error, ip_stack_t *ipst)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	ipif_t	*ipif;
2140Sstevel@tonic-gate 	ill_t	*ill;
2150Sstevel@tonic-gate 	boolean_t  ptp = B_FALSE;
2160Sstevel@tonic-gate 	ipsq_t	*ipsq;
2170Sstevel@tonic-gate 	ill_walk_context_t ctx;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (error != NULL)
2200Sstevel@tonic-gate 		*error = 0;
2210Sstevel@tonic-gate 
2223448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * Repeat twice, first based on local addresses and
2250Sstevel@tonic-gate 	 * next time for pointopoint.
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate repeat:
2283448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
2290Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2308485SPeter.Memishian@Sun.COM 		if (match_ill != NULL && ill != match_ill &&
2318485SPeter.Memishian@Sun.COM 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
2320Sstevel@tonic-gate 			continue;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
2350Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
2362733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
2372733Snordmark 		    ipif = ipif->ipif_next) {
2381676Sjpk 			if (zoneid != ALL_ZONES &&
2391676Sjpk 			    ipif->ipif_zoneid != zoneid &&
2401676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
2410Sstevel@tonic-gate 				continue;
2420Sstevel@tonic-gate 			/* Allow the ipif to be down */
2430Sstevel@tonic-gate 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
2440Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, addr) &&
2450Sstevel@tonic-gate 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
2460Sstevel@tonic-gate 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
2470Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
2480Sstevel@tonic-gate 			    addr))) {
2490Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
2500Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
2510Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2520Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2533448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2540Sstevel@tonic-gate 					return (ipif);
2550Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
2560Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
2570Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
2588485SPeter.Memishian@Sun.COM 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
2590Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2603448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2610Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
2624459Skcpoon 					    ill);
2638485SPeter.Memishian@Sun.COM 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
2640Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
2650Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2665335Ssowmini 					if (error != NULL)
2675335Ssowmini 						*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 
2878348SEric.Yu@Sun.COM boolean_t
2888348SEric.Yu@Sun.COM ip_addr_exists_v6(const in6_addr_t *addr, zoneid_t zoneid,
2898348SEric.Yu@Sun.COM     ip_stack_t *ipst)
2908348SEric.Yu@Sun.COM {
2918348SEric.Yu@Sun.COM 	ipif_t	*ipif;
2928348SEric.Yu@Sun.COM 	ill_t	*ill;
2938348SEric.Yu@Sun.COM 	ill_walk_context_t ctx;
2948348SEric.Yu@Sun.COM 
2958348SEric.Yu@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2968348SEric.Yu@Sun.COM 
2978348SEric.Yu@Sun.COM 	ill = ILL_START_WALK_V6(&ctx, ipst);
2988348SEric.Yu@Sun.COM 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2998348SEric.Yu@Sun.COM 		mutex_enter(&ill->ill_lock);
3008348SEric.Yu@Sun.COM 		for (ipif = ill->ill_ipif; ipif != NULL;
3018348SEric.Yu@Sun.COM 		    ipif = ipif->ipif_next) {
3028348SEric.Yu@Sun.COM 			if (zoneid != ALL_ZONES &&
3038348SEric.Yu@Sun.COM 			    ipif->ipif_zoneid != zoneid &&
3048348SEric.Yu@Sun.COM 			    ipif->ipif_zoneid != ALL_ZONES)
3058348SEric.Yu@Sun.COM 				continue;
3068348SEric.Yu@Sun.COM 			/* Allow the ipif to be down */
3078348SEric.Yu@Sun.COM 			if (((IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
3088348SEric.Yu@Sun.COM 			    addr) &&
3098348SEric.Yu@Sun.COM 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
3108348SEric.Yu@Sun.COM 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
3118348SEric.Yu@Sun.COM 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
3128348SEric.Yu@Sun.COM 			    addr))) {
3138348SEric.Yu@Sun.COM 				mutex_exit(&ill->ill_lock);
3148348SEric.Yu@Sun.COM 				rw_exit(&ipst->ips_ill_g_lock);
3158348SEric.Yu@Sun.COM 				return (B_TRUE);
3168348SEric.Yu@Sun.COM 			}
3178348SEric.Yu@Sun.COM 		}
3188348SEric.Yu@Sun.COM 		mutex_exit(&ill->ill_lock);
3198348SEric.Yu@Sun.COM 	}
3208348SEric.Yu@Sun.COM 
3218348SEric.Yu@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
3228348SEric.Yu@Sun.COM 	return (B_FALSE);
3238348SEric.Yu@Sun.COM }
3248348SEric.Yu@Sun.COM 
3250Sstevel@tonic-gate /*
3268485SPeter.Memishian@Sun.COM  * Lookup an ipif with the specified address.  For point-to-point links we
3278485SPeter.Memishian@Sun.COM  * look for matches on either the destination address or the local address,
3288485SPeter.Memishian@Sun.COM  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
3298485SPeter.Memishian@Sun.COM  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
3308485SPeter.Memishian@Sun.COM  * (or illgrp if `match_ill' is in an IPMP group).
3318485SPeter.Memishian@Sun.COM  */
3328485SPeter.Memishian@Sun.COM ipif_t *
3338485SPeter.Memishian@Sun.COM ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
3348485SPeter.Memishian@Sun.COM     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
3358485SPeter.Memishian@Sun.COM {
3368485SPeter.Memishian@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill, B_TRUE, zoneid, q,
3378485SPeter.Memishian@Sun.COM 	    mp, func, error, ipst));
3388485SPeter.Memishian@Sun.COM }
3398485SPeter.Memishian@Sun.COM 
3408485SPeter.Memishian@Sun.COM /*
3418485SPeter.Memishian@Sun.COM  * Special abbreviated version of ipif_lookup_addr_v6() that doesn't match
3428485SPeter.Memishian@Sun.COM  * `match_ill' across the IPMP group.  This function is only needed in some
3438485SPeter.Memishian@Sun.COM  * corner-cases; almost everything should use ipif_lookup_addr_v6().
3448485SPeter.Memishian@Sun.COM  */
3458485SPeter.Memishian@Sun.COM ipif_t *
3468485SPeter.Memishian@Sun.COM ipif_lookup_addr_exact_v6(const in6_addr_t *addr, ill_t *match_ill,
3478485SPeter.Memishian@Sun.COM     ip_stack_t *ipst)
3488485SPeter.Memishian@Sun.COM {
3498485SPeter.Memishian@Sun.COM 	ASSERT(match_ill != NULL);
3508485SPeter.Memishian@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill, B_FALSE, ALL_ZONES,
3518485SPeter.Memishian@Sun.COM 	    NULL, NULL, NULL, NULL, ipst));
3528485SPeter.Memishian@Sun.COM }
3538485SPeter.Memishian@Sun.COM 
3548485SPeter.Memishian@Sun.COM /*
3552733Snordmark  * Look for an ipif with the specified address. For point-point links
3562733Snordmark  * we look for matches on either the destination address and the local
3572733Snordmark  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
3582733Snordmark  * is set.
3598485SPeter.Memishian@Sun.COM  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
3608485SPeter.Memishian@Sun.COM  * ill (or illgrp if `match_ill' is in an IPMP group).
3612733Snordmark  * Return the zoneid for the ipif. ALL_ZONES if none found.
3622733Snordmark  */
3632733Snordmark zoneid_t
3643448Sdh155122 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill,
3653448Sdh155122     ip_stack_t *ipst)
3662733Snordmark {
3672733Snordmark 	ipif_t	*ipif;
3682733Snordmark 	ill_t	*ill;
3692733Snordmark 	boolean_t  ptp = B_FALSE;
3702733Snordmark 	ill_walk_context_t ctx;
3712733Snordmark 	zoneid_t	zoneid;
3722733Snordmark 
3733448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3742733Snordmark 	/*
3752733Snordmark 	 * Repeat twice, first based on local addresses and
3762733Snordmark 	 * next time for pointopoint.
3772733Snordmark 	 */
3782733Snordmark repeat:
3793448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
3802733Snordmark 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
3818485SPeter.Memishian@Sun.COM 		if (match_ill != NULL && ill != match_ill &&
3828485SPeter.Memishian@Sun.COM 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
3832733Snordmark 			continue;
3842733Snordmark 		}
3852733Snordmark 		mutex_enter(&ill->ill_lock);
3862733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
3872733Snordmark 		    ipif = ipif->ipif_next) {
3882733Snordmark 			/* Allow the ipif to be down */
3892733Snordmark 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
3902733Snordmark 			    &ipif->ipif_v6lcl_addr, addr) &&
3912733Snordmark 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
3922733Snordmark 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
3932733Snordmark 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
3942733Snordmark 			    addr)) &&
3952733Snordmark 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
3962733Snordmark 				zoneid = ipif->ipif_zoneid;
3972733Snordmark 				mutex_exit(&ill->ill_lock);
3983448Sdh155122 				rw_exit(&ipst->ips_ill_g_lock);
3992733Snordmark 				/*
4002733Snordmark 				 * If ipif_zoneid was ALL_ZONES then we have
4012733Snordmark 				 * a trusted extensions shared IP address.
4022733Snordmark 				 * In that case GLOBAL_ZONEID works to send.
4032733Snordmark 				 */
4042733Snordmark 				if (zoneid == ALL_ZONES)
4052733Snordmark 					zoneid = GLOBAL_ZONEID;
4062733Snordmark 				return (zoneid);
4072733Snordmark 			}
4082733Snordmark 		}
4092733Snordmark 		mutex_exit(&ill->ill_lock);
4102733Snordmark 	}
4112733Snordmark 
4122733Snordmark 	/* If we already did the ptp case, then we are done */
4132733Snordmark 	if (ptp) {
4143448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
4152733Snordmark 		return (ALL_ZONES);
4162733Snordmark 	}
4172733Snordmark 	ptp = B_TRUE;
4182733Snordmark 	goto repeat;
4192733Snordmark }
4202733Snordmark 
4212733Snordmark /*
4220Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a local
4230Sstevel@tonic-gate  * interface address.  This is currently only called when an attempt is made
4240Sstevel@tonic-gate  * to set a local address.
4250Sstevel@tonic-gate  *
4260Sstevel@tonic-gate  * Does not allow a v4-mapped address, an address that equals the subnet
4270Sstevel@tonic-gate  * anycast address, ... a multicast address, ...
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate boolean_t
4300Sstevel@tonic-gate ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	in6_addr_t subnet;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
4350Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * Don't allow all zeroes or host part, but allow
4390Sstevel@tonic-gate 	 * all ones netmask.
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
4420Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
4430Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
4440Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
4450Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
4460Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr))
4470Sstevel@tonic-gate 		return (B_FALSE);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	return (B_TRUE);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a
4540Sstevel@tonic-gate  * remote/subnet interface address.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate boolean_t
4570Sstevel@tonic-gate ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	in6_addr_t subnet;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
4620Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
4650Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
4660Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
4670Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
4680Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr) ||
4690Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
4700Sstevel@tonic-gate 		return (B_FALSE);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	return (B_TRUE);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
4770Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
4780Sstevel@tonic-gate  * (for link-local destinations and gateways).
4790Sstevel@tonic-gate  */
4800Sstevel@tonic-gate /* ARGSUSED1 */
4810Sstevel@tonic-gate int
4820Sstevel@tonic-gate ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
4830Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
4841676Sjpk     ipif_t *ipif_arg, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
4853448Sdh155122     struct rtsa_s *sp, ip_stack_t *ipst)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	ire_t	*ire;
4880Sstevel@tonic-gate 	ire_t	*gw_ire = NULL;
4890Sstevel@tonic-gate 	ipif_t	*ipif;
4900Sstevel@tonic-gate 	boolean_t ipif_refheld = B_FALSE;
4910Sstevel@tonic-gate 	uint_t	type;
4920Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
4930Sstevel@tonic-gate 	int	error;
4941676Sjpk 	tsol_gc_t *gc = NULL;
4951676Sjpk 	tsol_gcgrp_t *gcgrp = NULL;
4961676Sjpk 	boolean_t gcgrp_xtraref = B_FALSE;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (ire_arg != NULL)
4990Sstevel@tonic-gate 		*ire_arg = NULL;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * Prevent routes with a zero gateway from being created (since
5030Sstevel@tonic-gate 	 * interfaces can currently be plumbed and brought up with no assigned
5040Sstevel@tonic-gate 	 * address).
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
5070Sstevel@tonic-gate 		return (ENETUNREACH);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
5110Sstevel@tonic-gate 	 * to all ones (regardless if one was supplied).
5120Sstevel@tonic-gate 	 */
5130Sstevel@tonic-gate 	if (flags & RTF_HOST)
5140Sstevel@tonic-gate 		mask = &ipv6_all_ones;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * Get the ipif, if any, corresponding to the gw_addr
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func,
5203448Sdh155122 	    &error, ipst);
5210Sstevel@tonic-gate 	if (ipif != NULL)
5220Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
5230Sstevel@tonic-gate 	else if (error == EINPROGRESS) {
5240Sstevel@tonic-gate 		ip1dbg(("ip_rt_add_v6: null and EINPROGRESS"));
5250Sstevel@tonic-gate 		return (error);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/*
5290Sstevel@tonic-gate 	 * GateD will attempt to create routes with a loopback interface
5300Sstevel@tonic-gate 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5310Sstevel@tonic-gate 	 * these routes to be added, but create them as interface routes
5320Sstevel@tonic-gate 	 * since the gateway is an interface address.
5330Sstevel@tonic-gate 	 */
5341822Srk129064 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5350Sstevel@tonic-gate 		flags &= ~RTF_GATEWAY;
5361822Srk129064 		if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) &&
5371822Srk129064 		    IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) &&
5381822Srk129064 		    IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) {
5391822Srk129064 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
5403448Sdh155122 			    ipif, ALL_ZONES, NULL, match_flags, ipst);
5411822Srk129064 			if (ire != NULL) {
5421822Srk129064 				ire_refrele(ire);
5431822Srk129064 				if (ipif_refheld)
5441822Srk129064 					ipif_refrele(ipif);
5451822Srk129064 				return (EEXIST);
5461822Srk129064 			}
5471822Srk129064 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
5481822Srk129064 			    "for 0x%x\n", (void *)ipif,
5491822Srk129064 			    ipif->ipif_ire_type,
5501822Srk129064 			    ntohl(ipif->ipif_lcl_addr)));
5511822Srk129064 			ire = ire_create_v6(
5521822Srk129064 			    dst_addr,
5531822Srk129064 			    mask,
5541822Srk129064 			    &ipif->ipif_v6src_addr,
5551822Srk129064 			    NULL,
5561822Srk129064 			    &ipif->ipif_mtu,
5571822Srk129064 			    NULL,
5581822Srk129064 			    NULL,
5591822Srk129064 			    NULL,
5601822Srk129064 			    ipif->ipif_net_type,
5611822Srk129064 			    ipif,
5621822Srk129064 			    NULL,
5631822Srk129064 			    0,
5641822Srk129064 			    0,
5651822Srk129064 			    flags,
5661822Srk129064 			    &ire_uinfo_null,
5671822Srk129064 			    NULL,
5683448Sdh155122 			    NULL,
5693448Sdh155122 			    ipst);
5701822Srk129064 			if (ire == NULL) {
5711822Srk129064 				if (ipif_refheld)
5721822Srk129064 					ipif_refrele(ipif);
5731822Srk129064 				return (ENOMEM);
5741822Srk129064 			}
5752535Ssangeeta 			error = ire_add(&ire, q, mp, func, B_FALSE);
5761822Srk129064 			if (error == 0)
5771822Srk129064 				goto save_ire;
5781822Srk129064 			/*
5791822Srk129064 			 * In the result of failure, ire_add() will have already
5801822Srk129064 			 * deleted the ire in question, so there is no need to
5811822Srk129064 			 * do that here.
5821822Srk129064 			 */
5831822Srk129064 			if (ipif_refheld)
5841822Srk129064 				ipif_refrele(ipif);
5851822Srk129064 			return (error);
5861822Srk129064 		}
5871822Srk129064 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5910Sstevel@tonic-gate 	 * and the gateway address provided is one of the system's interface
5920Sstevel@tonic-gate 	 * addresses.  By using the routing socket interface and supplying an
5930Sstevel@tonic-gate 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5940Sstevel@tonic-gate 	 * specifying an interface route to be created is available which uses
5950Sstevel@tonic-gate 	 * the interface index that specifies the outgoing interface rather than
5960Sstevel@tonic-gate 	 * the address of an outgoing interface (which may not be able to
5970Sstevel@tonic-gate 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5980Sstevel@tonic-gate 	 * flag, routes can be specified which not only specify the next-hop to
5990Sstevel@tonic-gate 	 * be used when routing to a certain prefix, but also which outgoing
6000Sstevel@tonic-gate 	 * interface should be used.
6010Sstevel@tonic-gate 	 *
6020Sstevel@tonic-gate 	 * Previously, interfaces would have unique addresses assigned to them
6030Sstevel@tonic-gate 	 * and so the address assigned to a particular interface could be used
6040Sstevel@tonic-gate 	 * to identify a particular interface.  One exception to this was the
6050Sstevel@tonic-gate 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6060Sstevel@tonic-gate 	 *
6070Sstevel@tonic-gate 	 * With the advent of IPv6 and its link-local addresses, this
6080Sstevel@tonic-gate 	 * restriction was relaxed and interfaces could share addresses between
6090Sstevel@tonic-gate 	 * themselves.  In fact, typically all of the link-local interfaces on
6100Sstevel@tonic-gate 	 * an IPv6 node or router will have the same link-local address.  In
6110Sstevel@tonic-gate 	 * order to differentiate between these interfaces, the use of an
6120Sstevel@tonic-gate 	 * interface index is necessary and this index can be carried inside a
6130Sstevel@tonic-gate 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6140Sstevel@tonic-gate 	 * of using the interface index, however, is that all of the ipif's that
6150Sstevel@tonic-gate 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6160Sstevel@tonic-gate 	 * cannot be used to differentiate between ipif's (or logical
6170Sstevel@tonic-gate 	 * interfaces) that belong to the same ill (physical interface).
6180Sstevel@tonic-gate 	 *
6190Sstevel@tonic-gate 	 * For example, in the following case involving IPv4 interfaces and
6200Sstevel@tonic-gate 	 * logical interfaces
6210Sstevel@tonic-gate 	 *
6220Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6230Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6240Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6250Sstevel@tonic-gate 	 *
6260Sstevel@tonic-gate 	 * the ipif's corresponding to each of these interface routes can be
6270Sstevel@tonic-gate 	 * uniquely identified by the "gateway" (actually interface address).
6280Sstevel@tonic-gate 	 *
6290Sstevel@tonic-gate 	 * In this case involving multiple IPv6 default routes to a particular
6300Sstevel@tonic-gate 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6310Sstevel@tonic-gate 	 * default route is of interest:
6320Sstevel@tonic-gate 	 *
6330Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6340Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6350Sstevel@tonic-gate 	 */
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/* RTF_GATEWAY not set */
6380Sstevel@tonic-gate 	if (!(flags & RTF_GATEWAY)) {
6390Sstevel@tonic-gate 		queue_t	*stq;
6400Sstevel@tonic-gate 
6411676Sjpk 		if (sp != NULL) {
6421676Sjpk 			ip2dbg(("ip_rt_add_v6: gateway security attributes "
6431676Sjpk 			    "cannot be set with interface route\n"));
6441676Sjpk 			if (ipif_refheld)
6451676Sjpk 				ipif_refrele(ipif);
6461676Sjpk 			return (EINVAL);
6471676Sjpk 		}
6481676Sjpk 
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * As the interface index specified with the RTA_IFP sockaddr is
6510Sstevel@tonic-gate 		 * the same for all ipif's off of an ill, the matching logic
6520Sstevel@tonic-gate 		 * below uses MATCH_IRE_ILL if such an index was specified.
6530Sstevel@tonic-gate 		 * This means that routes sharing the same prefix when added
6540Sstevel@tonic-gate 		 * using a RTA_IFP sockaddr must have distinct interface
6550Sstevel@tonic-gate 		 * indices (namely, they must be on distinct ill's).
6560Sstevel@tonic-gate 		 *
6570Sstevel@tonic-gate 		 * On the other hand, since the gateway address will usually be
6580Sstevel@tonic-gate 		 * different for each ipif on the system, the matching logic
6590Sstevel@tonic-gate 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6600Sstevel@tonic-gate 		 * route.  This means that interface routes for the same prefix
6610Sstevel@tonic-gate 		 * can be created if they belong to distinct ipif's and if a
6620Sstevel@tonic-gate 		 * RTA_IFP sockaddr is not present.
6630Sstevel@tonic-gate 		 */
6640Sstevel@tonic-gate 		if (ipif_arg != NULL) {
6650Sstevel@tonic-gate 			if (ipif_refheld) {
6660Sstevel@tonic-gate 				ipif_refrele(ipif);
6670Sstevel@tonic-gate 				ipif_refheld = B_FALSE;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 			ipif = ipif_arg;
6700Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
6710Sstevel@tonic-gate 		} else {
6720Sstevel@tonic-gate 			/*
6730Sstevel@tonic-gate 			 * Check the ipif corresponding to the gw_addr
6740Sstevel@tonic-gate 			 */
6750Sstevel@tonic-gate 			if (ipif == NULL)
6760Sstevel@tonic-gate 				return (ENETUNREACH);
6770Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		ASSERT(ipif != NULL);
6810Sstevel@tonic-gate 		/*
6820Sstevel@tonic-gate 		 * We check for an existing entry at this point.
6830Sstevel@tonic-gate 		 */
6840Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
6850Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, 0, IRE_INTERFACE,
6863448Sdh155122 		    ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6870Sstevel@tonic-gate 		if (ire != NULL) {
6880Sstevel@tonic-gate 			ire_refrele(ire);
6890Sstevel@tonic-gate 			if (ipif_refheld)
6900Sstevel@tonic-gate 				ipif_refrele(ipif);
6910Sstevel@tonic-gate 			return (EEXIST);
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6950Sstevel@tonic-gate 		    ? ipif->ipif_rq : ipif->ipif_wq;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
6990Sstevel@tonic-gate 		 * IRE_IF_RESOLVER with the modified address and netmask.
7000Sstevel@tonic-gate 		 */
7010Sstevel@tonic-gate 		ire = ire_create_v6(
7020Sstevel@tonic-gate 		    dst_addr,
7030Sstevel@tonic-gate 		    mask,
7040Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr,
7050Sstevel@tonic-gate 		    NULL,
7060Sstevel@tonic-gate 		    &ipif->ipif_mtu,
7070Sstevel@tonic-gate 		    NULL,
7080Sstevel@tonic-gate 		    NULL,
7090Sstevel@tonic-gate 		    stq,
7100Sstevel@tonic-gate 		    ipif->ipif_net_type,
7110Sstevel@tonic-gate 		    ipif,
7120Sstevel@tonic-gate 		    NULL,
7130Sstevel@tonic-gate 		    0,
7140Sstevel@tonic-gate 		    0,
7150Sstevel@tonic-gate 		    flags,
7161676Sjpk 		    &ire_uinfo_null,
7171676Sjpk 		    NULL,
7183448Sdh155122 		    NULL,
7193448Sdh155122 		    ipst);
7200Sstevel@tonic-gate 		if (ire == NULL) {
7210Sstevel@tonic-gate 			if (ipif_refheld)
7220Sstevel@tonic-gate 				ipif_refrele(ipif);
7230Sstevel@tonic-gate 			return (ENOMEM);
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		/*
7270Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
7280Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
7290Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
7300Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
7315907Sja97890 		 * when generating aggregate routes). We also OR in the
7325907Sja97890 		 * RTF_BLACKHOLE flag as these interface routes, by
7335907Sja97890 		 * definition, can only be that.
7340Sstevel@tonic-gate 		 *
7350Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7360Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
7370Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
7380Sstevel@tonic-gate 		 *
7390Sstevel@tonic-gate 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7400Sstevel@tonic-gate 		 * routine, but rather using ire_create_v6() directly.
7410Sstevel@tonic-gate 		 */
7425907Sja97890 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7430Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
7445907Sja97890 			ire->ire_flags |= RTF_BLACKHOLE;
7455907Sja97890 		}
7462535Ssangeeta 		error = ire_add(&ire, q, mp, func, B_FALSE);
7470Sstevel@tonic-gate 		if (error == 0)
7480Sstevel@tonic-gate 			goto save_ire;
7490Sstevel@tonic-gate 		/*
7500Sstevel@tonic-gate 		 * In the result of failure, ire_add() will have already
7510Sstevel@tonic-gate 		 * deleted the ire in question, so there is no need to
7520Sstevel@tonic-gate 		 * do that here.
7530Sstevel@tonic-gate 		 */
7540Sstevel@tonic-gate 		if (ipif_refheld)
7550Sstevel@tonic-gate 			ipif_refrele(ipif);
7560Sstevel@tonic-gate 		return (error);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 	if (ipif_refheld) {
7590Sstevel@tonic-gate 		ipif_refrele(ipif);
7600Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/*
7640Sstevel@tonic-gate 	 * Get an interface IRE for the specified gateway.
7650Sstevel@tonic-gate 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7660Sstevel@tonic-gate 	 * gateway, it is currently unreachable and we fail the request
7670Sstevel@tonic-gate 	 * accordingly.
7680Sstevel@tonic-gate 	 */
7690Sstevel@tonic-gate 	ipif = ipif_arg;
7700Sstevel@tonic-gate 	if (ipif_arg != NULL)
7710Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
7720Sstevel@tonic-gate 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg,
7733448Sdh155122 	    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7740Sstevel@tonic-gate 	if (gw_ire == NULL)
7750Sstevel@tonic-gate 		return (ENETUNREACH);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/*
7780Sstevel@tonic-gate 	 * We create one of three types of IREs as a result of this request
7790Sstevel@tonic-gate 	 * based on the netmask.  A netmask of all ones (which is automatically
7800Sstevel@tonic-gate 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7810Sstevel@tonic-gate 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7820Sstevel@tonic-gate 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7830Sstevel@tonic-gate 	 * destination prefix.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
7860Sstevel@tonic-gate 		type = IRE_HOST;
7870Sstevel@tonic-gate 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
7880Sstevel@tonic-gate 		type = IRE_DEFAULT;
7890Sstevel@tonic-gate 	else
7900Sstevel@tonic-gate 		type = IRE_PREFIX;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/* check for a duplicate entry */
7930Sstevel@tonic-gate 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ipif_arg,
7941676Sjpk 	    NULL, ALL_ZONES, 0, NULL,
7953448Sdh155122 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7960Sstevel@tonic-gate 	if (ire != NULL) {
7970Sstevel@tonic-gate 		ire_refrele(gw_ire);
7980Sstevel@tonic-gate 		ire_refrele(ire);
7990Sstevel@tonic-gate 		return (EEXIST);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8021676Sjpk 	/* Security attribute exists */
8031676Sjpk 	if (sp != NULL) {
8041676Sjpk 		tsol_gcgrp_addr_t ga;
8051676Sjpk 
8061676Sjpk 		/* find or create the gateway credentials group */
8071676Sjpk 		ga.ga_af = AF_INET6;
8081676Sjpk 		ga.ga_addr = *gw_addr;
8091676Sjpk 
8101676Sjpk 		/* we hold reference to it upon success */
8111676Sjpk 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
8121676Sjpk 		if (gcgrp == NULL) {
8131676Sjpk 			ire_refrele(gw_ire);
8141676Sjpk 			return (ENOMEM);
8151676Sjpk 		}
8161676Sjpk 
8171676Sjpk 		/*
8181676Sjpk 		 * Create and add the security attribute to the group; a
8191676Sjpk 		 * reference to the group is made upon allocating a new
8201676Sjpk 		 * entry successfully.  If it finds an already-existing
8211676Sjpk 		 * entry for the security attribute in the group, it simply
8221676Sjpk 		 * returns it and no new reference is made to the group.
8231676Sjpk 		 */
8241676Sjpk 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
8251676Sjpk 		if (gc == NULL) {
8261676Sjpk 			/* release reference held by gcgrp_lookup */
8271676Sjpk 			GCGRP_REFRELE(gcgrp);
8281676Sjpk 			ire_refrele(gw_ire);
8291676Sjpk 			return (ENOMEM);
8301676Sjpk 		}
8311676Sjpk 	}
8321676Sjpk 
8330Sstevel@tonic-gate 	/* Create the IRE. */
8340Sstevel@tonic-gate 	ire = ire_create_v6(
8350Sstevel@tonic-gate 	    dst_addr,				/* dest address */
8360Sstevel@tonic-gate 	    mask,				/* mask */
8370Sstevel@tonic-gate 	    /* src address assigned by the caller? */
8380Sstevel@tonic-gate 	    (((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) ?
8394459Skcpoon 	    src_addr : NULL),
8400Sstevel@tonic-gate 	    gw_addr,				/* gateway address */
8410Sstevel@tonic-gate 	    &gw_ire->ire_max_frag,
8424714Ssowmini 	    NULL,				/* no src nce */
8430Sstevel@tonic-gate 	    NULL,				/* no recv-from queue */
8440Sstevel@tonic-gate 	    NULL,				/* no send-to queue */
8450Sstevel@tonic-gate 	    (ushort_t)type,			/* IRE type */
8460Sstevel@tonic-gate 	    ipif_arg,
8470Sstevel@tonic-gate 	    NULL,
8480Sstevel@tonic-gate 	    0,
8490Sstevel@tonic-gate 	    0,
8500Sstevel@tonic-gate 	    flags,
8511676Sjpk 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
8521676Sjpk 	    gc,					/* security attribute */
8533448Sdh155122 	    NULL,
8543448Sdh155122 	    ipst);
8553448Sdh155122 
8561676Sjpk 	/*
8571676Sjpk 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
8581676Sjpk 	 * reference to the 'gcgrp'. We can now release the extra reference
8591676Sjpk 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
8601676Sjpk 	 */
8611676Sjpk 	if (gcgrp_xtraref)
8621676Sjpk 		GCGRP_REFRELE(gcgrp);
8630Sstevel@tonic-gate 	if (ire == NULL) {
8641676Sjpk 		if (gc != NULL)
8651676Sjpk 			GC_REFRELE(gc);
8660Sstevel@tonic-gate 		ire_refrele(gw_ire);
8670Sstevel@tonic-gate 		return (ENOMEM);
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
8720Sstevel@tonic-gate 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* Add the new IRE. */
8762535Ssangeeta 	error = ire_add(&ire, q, mp, func, B_FALSE);
8770Sstevel@tonic-gate 	/*
8780Sstevel@tonic-gate 	 * In the result of failure, ire_add() will have already
8790Sstevel@tonic-gate 	 * deleted the ire in question, so there is no need to
8800Sstevel@tonic-gate 	 * do that here.
8810Sstevel@tonic-gate 	 */
8820Sstevel@tonic-gate 	if (error != 0) {
8830Sstevel@tonic-gate 		ire_refrele(gw_ire);
8840Sstevel@tonic-gate 		return (error);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if (flags & RTF_MULTIRT) {
8880Sstevel@tonic-gate 		/*
8890Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
8900Sstevel@tonic-gate 		 * to add the dst address in the filtering database.
8910Sstevel@tonic-gate 		 * Replicated inbound packets coming from that address
8920Sstevel@tonic-gate 		 * will be filtered to discard the duplicates.
8930Sstevel@tonic-gate 		 * It is not necessary to call the CGTP filter hook
8940Sstevel@tonic-gate 		 * when the dst address is a multicast, because an
8950Sstevel@tonic-gate 		 * IP source address cannot be a multicast.
8960Sstevel@tonic-gate 		 */
8974961Snordmark 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
8980Sstevel@tonic-gate 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
8994961Snordmark 			int res;
9004961Snordmark 
9014961Snordmark 			res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v6(
9024961Snordmark 			    ipst->ips_netstack->netstack_stackid,
9030Sstevel@tonic-gate 			    &ire->ire_addr_v6,
9040Sstevel@tonic-gate 			    &ire->ire_gateway_addr_v6,
9050Sstevel@tonic-gate 			    &ire->ire_src_addr_v6,
9060Sstevel@tonic-gate 			    &gw_ire->ire_src_addr_v6);
9070Sstevel@tonic-gate 			if (res != 0) {
9080Sstevel@tonic-gate 				ire_refrele(gw_ire);
9090Sstevel@tonic-gate 				ire_delete(ire);
9100Sstevel@tonic-gate 				return (res);
9110Sstevel@tonic-gate 			}
9120Sstevel@tonic-gate 		}
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 
9151676Sjpk 	/*
9161676Sjpk 	 * Now that the prefix IRE entry has been created, delete any
9171676Sjpk 	 * existing gateway IRE cache entries as well as any IRE caches
9181676Sjpk 	 * using the gateway, and force them to be created through
9191676Sjpk 	 * ip_newroute_v6.
9201676Sjpk 	 */
9211676Sjpk 	if (gc != NULL) {
9221676Sjpk 		ASSERT(gcgrp != NULL);
9233448Sdh155122 		ire_clookup_delete_cache_gw_v6(gw_addr, ALL_ZONES, ipst);
9241676Sjpk 	}
9251676Sjpk 
9260Sstevel@tonic-gate save_ire:
9270Sstevel@tonic-gate 	if (gw_ire != NULL) {
9280Sstevel@tonic-gate 		ire_refrele(gw_ire);
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 	if (ipif != NULL) {
9310Sstevel@tonic-gate 		mblk_t	*save_mp;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 		/*
9340Sstevel@tonic-gate 		 * Save enough information so that we can recreate the IRE if
9350Sstevel@tonic-gate 		 * the interface goes down and then up.  The metrics associated
9360Sstevel@tonic-gate 		 * with the route will be saved as well when rts_setmetrics() is
9370Sstevel@tonic-gate 		 * called after the IRE has been created.  In the case where
9380Sstevel@tonic-gate 		 * memory cannot be allocated, none of this information will be
9390Sstevel@tonic-gate 		 * saved.
9400Sstevel@tonic-gate 		 */
9410Sstevel@tonic-gate 		save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
9420Sstevel@tonic-gate 		if (save_mp != NULL) {
9430Sstevel@tonic-gate 			ifrt_t	*ifrt;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 			save_mp->b_wptr += sizeof (ifrt_t);
9460Sstevel@tonic-gate 			ifrt = (ifrt_t *)save_mp->b_rptr;
9470Sstevel@tonic-gate 			bzero(ifrt, sizeof (ifrt_t));
9480Sstevel@tonic-gate 			ifrt->ifrt_type = ire->ire_type;
9490Sstevel@tonic-gate 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
9500Sstevel@tonic-gate 			mutex_enter(&ire->ire_lock);
9510Sstevel@tonic-gate 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
9520Sstevel@tonic-gate 			ifrt->ifrt_v6src_addr = ire->ire_src_addr_v6;
9530Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
9540Sstevel@tonic-gate 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
9550Sstevel@tonic-gate 			ifrt->ifrt_flags = ire->ire_flags;
9560Sstevel@tonic-gate 			ifrt->ifrt_max_frag = ire->ire_max_frag;
9570Sstevel@tonic-gate 			mutex_enter(&ipif->ipif_saved_ire_lock);
9580Sstevel@tonic-gate 			save_mp->b_cont = ipif->ipif_saved_ire_mp;
9590Sstevel@tonic-gate 			ipif->ipif_saved_ire_mp = save_mp;
9600Sstevel@tonic-gate 			ipif->ipif_saved_ire_cnt++;
9610Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 	if (ire_arg != NULL) {
9650Sstevel@tonic-gate 		/*
9660Sstevel@tonic-gate 		 * Store the ire that was successfully added into where ire_arg
9670Sstevel@tonic-gate 		 * points to so that callers don't have to look it up
9680Sstevel@tonic-gate 		 * themselves (but they are responsible for ire_refrele()ing
9690Sstevel@tonic-gate 		 * the ire when they are finished with it).
9700Sstevel@tonic-gate 		 */
9710Sstevel@tonic-gate 		*ire_arg = ire;
9720Sstevel@tonic-gate 	} else {
9730Sstevel@tonic-gate 		ire_refrele(ire);		/* Held in ire_add */
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 	if (ipif_refheld)
9760Sstevel@tonic-gate 		ipif_refrele(ipif);
9770Sstevel@tonic-gate 	return (0);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * ip_rt_delete_v6 is called to delete an IPv6 route.
9820Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
9830Sstevel@tonic-gate  * (for link-local destinations and gateways).
9840Sstevel@tonic-gate  */
9850Sstevel@tonic-gate /* ARGSUSED4 */
9860Sstevel@tonic-gate int
9870Sstevel@tonic-gate ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
9880Sstevel@tonic-gate     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ipif_t *ipif_arg,
9893448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	ire_t	*ire = NULL;
9920Sstevel@tonic-gate 	ipif_t	*ipif;
9930Sstevel@tonic-gate 	uint_t	type;
9940Sstevel@tonic-gate 	uint_t	match_flags = MATCH_IRE_TYPE;
9950Sstevel@tonic-gate 	int	err = 0;
9960Sstevel@tonic-gate 	boolean_t	ipif_refheld = B_FALSE;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
10000Sstevel@tonic-gate 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	if (flags & RTF_HOST) {
10030Sstevel@tonic-gate 		mask = &ipv6_all_ones;
10040Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
10050Sstevel@tonic-gate 	} else if (rtm_addrs & RTA_NETMASK) {
10060Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * Note that RTF_GATEWAY is never set on a delete, therefore
10110Sstevel@tonic-gate 	 * we check if the gateway address is one of our interfaces first,
10120Sstevel@tonic-gate 	 * and fall back on RTF_GATEWAY routes.
10130Sstevel@tonic-gate 	 *
10140Sstevel@tonic-gate 	 * This makes it possible to delete an original
10150Sstevel@tonic-gate 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
10160Sstevel@tonic-gate 	 *
10170Sstevel@tonic-gate 	 * As the interface index specified with the RTA_IFP sockaddr is the
10180Sstevel@tonic-gate 	 * same for all ipif's off of an ill, the matching logic below uses
10190Sstevel@tonic-gate 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
10200Sstevel@tonic-gate 	 * sharing the same prefix and interface index as the the route
10210Sstevel@tonic-gate 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
10220Sstevel@tonic-gate 	 * is specified in the request.
10230Sstevel@tonic-gate 	 *
10240Sstevel@tonic-gate 	 * On the other hand, since the gateway address will usually be
10250Sstevel@tonic-gate 	 * different for each ipif on the system, the matching logic
10260Sstevel@tonic-gate 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
10270Sstevel@tonic-gate 	 * route.  This means that interface routes for the same prefix can be
10280Sstevel@tonic-gate 	 * uniquely identified if they belong to distinct ipif's and if a
10290Sstevel@tonic-gate 	 * RTA_IFP sockaddr is not present.
10300Sstevel@tonic-gate 	 *
10310Sstevel@tonic-gate 	 * For more detail on specifying routes by gateway address and by
10320Sstevel@tonic-gate 	 * interface index, see the comments in ip_rt_add_v6().
10330Sstevel@tonic-gate 	 */
10343448Sdh155122 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, &err,
10353448Sdh155122 	    ipst);
10360Sstevel@tonic-gate 	if (ipif != NULL) {
10370Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
10380Sstevel@tonic-gate 		if (ipif_arg != NULL) {
10390Sstevel@tonic-gate 			ipif_refrele(ipif);
10400Sstevel@tonic-gate 			ipif_refheld = B_FALSE;
10410Sstevel@tonic-gate 			ipif = ipif_arg;
10420Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
10430Sstevel@tonic-gate 		} else {
10440Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
10450Sstevel@tonic-gate 		}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		if (ipif->ipif_ire_type == IRE_LOOPBACK)
10480Sstevel@tonic-gate 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
10493448Sdh155122 			    ipif, ALL_ZONES, NULL, match_flags, ipst);
10500Sstevel@tonic-gate 		if (ire == NULL)
10510Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, 0,
10521676Sjpk 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
10533448Sdh155122 			    match_flags, ipst);
10540Sstevel@tonic-gate 	} else if (err == EINPROGRESS) {
10550Sstevel@tonic-gate 		return (err);
10560Sstevel@tonic-gate 	} else {
10570Sstevel@tonic-gate 		err = 0;
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 	if (ire == NULL) {
10600Sstevel@tonic-gate 		/*
10610Sstevel@tonic-gate 		 * At this point, the gateway address is not one of our own
10620Sstevel@tonic-gate 		 * addresses or a matching interface route was not found.  We
10630Sstevel@tonic-gate 		 * set the IRE type to lookup based on whether
10640Sstevel@tonic-gate 		 * this is a host route, a default route or just a prefix.
10650Sstevel@tonic-gate 		 *
10660Sstevel@tonic-gate 		 * If an ipif_arg was passed in, then the lookup is based on an
10670Sstevel@tonic-gate 		 * interface index so MATCH_IRE_ILL is added to match_flags.
10680Sstevel@tonic-gate 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
10690Sstevel@tonic-gate 		 * set as the route being looked up is not a traditional
10700Sstevel@tonic-gate 		 * interface route.
10710Sstevel@tonic-gate 		 */
10720Sstevel@tonic-gate 		match_flags &= ~MATCH_IRE_IPIF;
10730Sstevel@tonic-gate 		match_flags |= MATCH_IRE_GW;
10740Sstevel@tonic-gate 		if (ipif_arg != NULL)
10750Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
10760Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
10770Sstevel@tonic-gate 			type = IRE_HOST;
10780Sstevel@tonic-gate 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
10790Sstevel@tonic-gate 			type = IRE_DEFAULT;
10800Sstevel@tonic-gate 		else
10810Sstevel@tonic-gate 			type = IRE_PREFIX;
10820Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
10833448Sdh155122 		    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if (ipif_refheld) {
10870Sstevel@tonic-gate 		ipif_refrele(ipif);
10880Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	if (ire == NULL)
10910Sstevel@tonic-gate 		return (ESRCH);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (ire->ire_flags & RTF_MULTIRT) {
10940Sstevel@tonic-gate 		/*
10950Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
10960Sstevel@tonic-gate 		 * to remove the dst address from the filtering database.
10970Sstevel@tonic-gate 		 * Packets coming from that address will no longer be
10980Sstevel@tonic-gate 		 * filtered to remove duplicates.
10990Sstevel@tonic-gate 		 */
11004961Snordmark 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
11014961Snordmark 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v6(
11024961Snordmark 			    ipst->ips_netstack->netstack_stackid,
11030Sstevel@tonic-gate 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	ipif = ire->ire_ipif;
11080Sstevel@tonic-gate 	if (ipif != NULL) {
11090Sstevel@tonic-gate 		mblk_t		**mpp;
11100Sstevel@tonic-gate 		mblk_t		*mp;
11110Sstevel@tonic-gate 		ifrt_t		*ifrt;
11120Sstevel@tonic-gate 		in6_addr_t	gw_addr_v6;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 		/* Remove from ipif_saved_ire_mp list if it is there */
11150Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
11160Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
11170Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
11180Sstevel@tonic-gate 		mutex_enter(&ipif->ipif_saved_ire_lock);
11190Sstevel@tonic-gate 		for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
11200Sstevel@tonic-gate 		    mpp = &(*mpp)->b_cont) {
11210Sstevel@tonic-gate 			/*
11220Sstevel@tonic-gate 			 * On a given ipif, the triple of address, gateway and
11230Sstevel@tonic-gate 			 * mask is unique for each saved IRE (in the case of
11240Sstevel@tonic-gate 			 * ordinary interface routes, the gateway address is
11250Sstevel@tonic-gate 			 * all-zeroes).
11260Sstevel@tonic-gate 			 */
11270Sstevel@tonic-gate 			mp = *mpp;
11280Sstevel@tonic-gate 			ifrt = (ifrt_t *)mp->b_rptr;
11290Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
11300Sstevel@tonic-gate 			    &ire->ire_addr_v6) &&
11310Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
11320Sstevel@tonic-gate 			    &gw_addr_v6) &&
11330Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
11340Sstevel@tonic-gate 			    &ire->ire_mask_v6)) {
11350Sstevel@tonic-gate 				*mpp = mp->b_cont;
11360Sstevel@tonic-gate 				ipif->ipif_saved_ire_cnt--;
11370Sstevel@tonic-gate 				freeb(mp);
11380Sstevel@tonic-gate 				break;
11390Sstevel@tonic-gate 			}
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	ire_delete(ire);
11440Sstevel@tonic-gate 	ire_refrele(ire);
11450Sstevel@tonic-gate 	return (err);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate  * Derive a token from the link layer address.
11500Sstevel@tonic-gate  */
11510Sstevel@tonic-gate boolean_t
11520Sstevel@tonic-gate ill_setdefaulttoken(ill_t *ill)
11530Sstevel@tonic-gate {
11548485SPeter.Memishian@Sun.COM 	int		i;
11550Sstevel@tonic-gate 	in6_addr_t	v6addr, v6mask;
11560Sstevel@tonic-gate 
11578485SPeter.Memishian@Sun.COM 	if (!MEDIA_V6INTFID(ill->ill_media, ill, &v6addr))
11580Sstevel@tonic-gate 		return (B_FALSE);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_TOKEN_LEN, &v6mask);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
11630Sstevel@tonic-gate 		v6mask.s6_addr32[i] = v6mask.s6_addr32[i] ^
11640Sstevel@tonic-gate 		    (uint32_t)0xffffffff;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11670Sstevel@tonic-gate 	ill->ill_token_length = IPV6_TOKEN_LEN;
11680Sstevel@tonic-gate 	return (B_TRUE);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate  * Create a link-local address from a token.
11730Sstevel@tonic-gate  */
11740Sstevel@tonic-gate static void
11750Sstevel@tonic-gate ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	int i;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
11800Sstevel@tonic-gate 		dest->s6_addr32[i] =
11810Sstevel@tonic-gate 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
11820Sstevel@tonic-gate 	}
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate /*
11860Sstevel@tonic-gate  * Set a nice default address for either automatic tunnels tsrc/96 or
11870Sstevel@tonic-gate  * 6to4 tunnels 2002:<tsrc>::1/64
11880Sstevel@tonic-gate  */
11890Sstevel@tonic-gate static void
11900Sstevel@tonic-gate ipif_set_tun_auto_addr(ipif_t *ipif, struct iftun_req *ta)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	sin6_t	sin6;
11930Sstevel@tonic-gate 	sin_t	*sin;
11948485SPeter.Memishian@Sun.COM 	ill_t	*ill = ipif->ipif_ill;
11950Sstevel@tonic-gate 	tun_t *tp = (tun_t *)ill->ill_wq->q_next->q_ptr;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	if (ta->ifta_saddr.ss_family != AF_INET ||
11980Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_UP) || !ipif->ipif_isv6 ||
11990Sstevel@tonic-gate 	    (ta->ifta_flags & IFTUN_SRC) == 0)
12000Sstevel@tonic-gate 		return;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	/*
12030Sstevel@tonic-gate 	 * Check the tunnel type by examining q_next->q_ptr
12040Sstevel@tonic-gate 	 */
12050Sstevel@tonic-gate 	if (tp->tun_flags & TUN_AUTOMATIC) {
12060Sstevel@tonic-gate 		/* this is an automatic tunnel */
12070Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_ABITS - IP_ABITS,
12080Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
12090Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
12100Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
12110Sstevel@tonic-gate 		V4_PART_OF_V6(sin6.sin6_addr) = sin->sin_addr.s_addr;
12120Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
12130Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
12140Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
12150Sstevel@tonic-gate 	} else if (tp->tun_flags & TUN_6TO4) {
12160Sstevel@tonic-gate 		/* this is a 6to4 tunnel */
12170Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_PREFIX_LEN,
12180Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
12190Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
12200Sstevel@tonic-gate 		/* create a 6to4 address from the IPv4 tsrc */
12210Sstevel@tonic-gate 		IN6_V4ADDR_TO_6TO4(&sin->sin_addr, &sin6.sin6_addr);
12220Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
12230Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
12240Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
12250Sstevel@tonic-gate 	} else {
12260Sstevel@tonic-gate 		ip1dbg(("ipif_set_tun_auto_addr: Unknown tunnel type"));
12270Sstevel@tonic-gate 		return;
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate  * Set link local for ipif_id 0 of a configured tunnel based on the
12330Sstevel@tonic-gate  * tsrc or tdst parameter
12340Sstevel@tonic-gate  * For tunnels over IPv4 use the IPv4 address prepended with 32 zeros as
12350Sstevel@tonic-gate  * the token.
12360Sstevel@tonic-gate  * For tunnels over IPv6 use the low-order 64 bits of the "inner" IPv6 address
12370Sstevel@tonic-gate  * as the token for the "outer" link.
12380Sstevel@tonic-gate  */
12390Sstevel@tonic-gate void
12400Sstevel@tonic-gate ipif_set_tun_llink(ill_t *ill, struct iftun_req *ta)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	ipif_t		*ipif;
12430Sstevel@tonic-gate 	sin_t		*sin;
12440Sstevel@tonic-gate 	in6_addr_t	*s6addr;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/* The first ipif must be id zero. */
12490Sstevel@tonic-gate 	ipif = ill->ill_ipif;
12500Sstevel@tonic-gate 	ASSERT(ipif->ipif_id == 0);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	/* no link local for automatic tunnels */
12530Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_POINTOPOINT)) {
12540Sstevel@tonic-gate 		ipif_set_tun_auto_addr(ipif, ta);
12550Sstevel@tonic-gate 		return;
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_DST) &&
12590Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
12608485SPeter.Memishian@Sun.COM 		sin6_t  sin6;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
12630Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
12640Sstevel@tonic-gate 		if ((ta->ifta_saddr.ss_family == AF_INET)) {
12650Sstevel@tonic-gate 			sin = (sin_t *)&ta->ifta_daddr;
12660Sstevel@tonic-gate 			V4_PART_OF_V6(sin6.sin6_addr) =
12670Sstevel@tonic-gate 			    sin->sin_addr.s_addr;
12680Sstevel@tonic-gate 		} else {
12690Sstevel@tonic-gate 			s6addr =
12700Sstevel@tonic-gate 			    &((sin6_t *)&ta->ifta_daddr)->sin6_addr;
12710Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[3] = s6addr->s6_addr32[3];
12720Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[2] = s6addr->s6_addr32[2];
12730Sstevel@tonic-gate 		}
12740Sstevel@tonic-gate 		ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr,
12750Sstevel@tonic-gate 		    &sin6.sin6_addr);
12760Sstevel@tonic-gate 		ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_SRC)) {
12790Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		/* Set the token if it isn't already set */
12820Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) {
12830Sstevel@tonic-gate 			if ((ta->ifta_saddr.ss_family == AF_INET)) {
12840Sstevel@tonic-gate 				sin = (sin_t *)&ta->ifta_saddr;
12850Sstevel@tonic-gate 				V4_PART_OF_V6(ill->ill_token) =
12860Sstevel@tonic-gate 				    sin->sin_addr.s_addr;
12870Sstevel@tonic-gate 			} else {
12880Sstevel@tonic-gate 				s6addr =
12890Sstevel@tonic-gate 				    &((sin6_t *)&ta->ifta_saddr)->sin6_addr;
12900Sstevel@tonic-gate 				ill->ill_token.s6_addr32[3] =
12910Sstevel@tonic-gate 				    s6addr->s6_addr32[3];
12920Sstevel@tonic-gate 				ill->ill_token.s6_addr32[2] =
12930Sstevel@tonic-gate 				    s6addr->s6_addr32[2];
12940Sstevel@tonic-gate 			}
12950Sstevel@tonic-gate 			ill->ill_token_length = IPV6_TOKEN_LEN;
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 		/*
12980Sstevel@tonic-gate 		 * Attempt to set the link local address if it isn't set.
12990Sstevel@tonic-gate 		 */
13000Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13010Sstevel@tonic-gate 			(void) ipif_setlinklocal(ipif);
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate /*
13060Sstevel@tonic-gate  * Is it not possible to set the link local address?
13070Sstevel@tonic-gate  * The address can be set if the token is set, and the token
13080Sstevel@tonic-gate  * isn't too long.
13090Sstevel@tonic-gate  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate boolean_t
13120Sstevel@tonic-gate ipif_cant_setlinklocal(ipif_t *ipif)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
13170Sstevel@tonic-gate 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
13180Sstevel@tonic-gate 		return (B_TRUE);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	return (B_FALSE);
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate /*
13240Sstevel@tonic-gate  * Generate a link-local address from the token.
13250Sstevel@tonic-gate  * Return zero if the address was set, or non-zero if it couldn't be set.
13260Sstevel@tonic-gate  */
13270Sstevel@tonic-gate int
13280Sstevel@tonic-gate ipif_setlinklocal(ipif_t *ipif)
13290Sstevel@tonic-gate {
13303706Svi117747 	ill_t		*ill = ipif->ipif_ill;
13313706Svi117747 	in6_addr_t	ov6addr;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	if (ipif_cant_setlinklocal(ipif))
13360Sstevel@tonic-gate 		return (-1);
13370Sstevel@tonic-gate 
13383706Svi117747 	ov6addr = ipif->ipif_v6lcl_addr;
13390Sstevel@tonic-gate 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
13403706Svi117747 	sctp_update_ipif_addr(ipif, ov6addr);
13410Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
13420Sstevel@tonic-gate 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
13430Sstevel@tonic-gate 	    ipif->ipif_v6subnet);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (ipif->ipif_flags & IPIF_NOLOCAL) {
13460Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipv6_all_zeros;
13470Sstevel@tonic-gate 	} else {
13480Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 	return (0);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate  * This function sets up the multicast mappings in NDP.
13550Sstevel@tonic-gate  * Unlike ARP, there are no mapping_mps here. We delete the
13560Sstevel@tonic-gate  * mapping nces and add a new one.
13570Sstevel@tonic-gate  *
13580Sstevel@tonic-gate  * Returns non-zero on error and 0 on success.
13590Sstevel@tonic-gate  */
13600Sstevel@tonic-gate int
13610Sstevel@tonic-gate ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce)
13620Sstevel@tonic-gate {
13630Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
13640Sstevel@tonic-gate 	in6_addr_t	v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0};
13650Sstevel@tonic-gate 	in6_addr_t	v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0};
13660Sstevel@tonic-gate 	in6_addr_t	v6_extract_mask;
13670Sstevel@tonic-gate 	uchar_t		*phys_addr, *bphys_addr, *alloc_phys;
13680Sstevel@tonic-gate 	nce_t		*mnce = NULL;
13690Sstevel@tonic-gate 	int		err = 0;
13700Sstevel@tonic-gate 	phyint_t	*phyi = ill->ill_phyint;
13710Sstevel@tonic-gate 	uint32_t	hw_extract_start;
13720Sstevel@tonic-gate 	dl_unitdata_req_t *dlur;
13733448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	if (ret_nce != NULL)
13760Sstevel@tonic-gate 		*ret_nce = NULL;
13778485SPeter.Memishian@Sun.COM 
13788485SPeter.Memishian@Sun.COM 	/*
13798485SPeter.Memishian@Sun.COM 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13808485SPeter.Memishian@Sun.COM 	 * and instead use the ones on the underlying interfaces.
13818485SPeter.Memishian@Sun.COM 	 */
13828485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill))
13838485SPeter.Memishian@Sun.COM 		return (0);
13848485SPeter.Memishian@Sun.COM 
13850Sstevel@tonic-gate 	/*
13860Sstevel@tonic-gate 	 * Delete the mapping nce. Normally these should not exist
13870Sstevel@tonic-gate 	 * as a previous ipif_down -> ipif_ndp_down should have deleted
13880Sstevel@tonic-gate 	 * all the nces. But they can exist if ip_rput_dlpi_writer
13898485SPeter.Memishian@Sun.COM 	 * calls this when PHYI_MULTI_BCAST is set.  Mappings are always
13908485SPeter.Memishian@Sun.COM 	 * tied to the underlying ill, so don't match across the illgrp.
13910Sstevel@tonic-gate 	 */
13928485SPeter.Memishian@Sun.COM 	mnce = ndp_lookup_v6(ill, B_FALSE, &v6_mcast_addr, B_FALSE);
13930Sstevel@tonic-gate 	if (mnce != NULL) {
13940Sstevel@tonic-gate 		ndp_delete(mnce);
13950Sstevel@tonic-gate 		NCE_REFRELE(mnce);
13960Sstevel@tonic-gate 		mnce = NULL;
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	/*
14000Sstevel@tonic-gate 	 * Get media specific v6 mapping information. Note that
14010Sstevel@tonic-gate 	 * nd_lla_len can be 0 for tunnels.
14020Sstevel@tonic-gate 	 */
14030Sstevel@tonic-gate 	alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
14040Sstevel@tonic-gate 	if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0))
14050Sstevel@tonic-gate 		return (ENOMEM);
14060Sstevel@tonic-gate 	/*
14070Sstevel@tonic-gate 	 * Determine the broadcast address.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
14100Sstevel@tonic-gate 	if (ill->ill_sap_length < 0)
14110Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
14120Sstevel@tonic-gate 	else
14130Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur +
14140Sstevel@tonic-gate 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	/*
14170Sstevel@tonic-gate 	 * Check PHYI_MULTI_BCAST and possible length of physical
14180Sstevel@tonic-gate 	 * address to determine if we use the mapping or the
14190Sstevel@tonic-gate 	 * broadcast address.
14200Sstevel@tonic-gate 	 */
14210Sstevel@tonic-gate 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
14220Sstevel@tonic-gate 	    (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len,
14230Sstevel@tonic-gate 	    bphys_addr, alloc_phys, &hw_extract_start,
14240Sstevel@tonic-gate 	    &v6_extract_mask))) {
14250Sstevel@tonic-gate 		if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) {
14260Sstevel@tonic-gate 			kmem_free(alloc_phys, ill->ill_nd_lla_len);
14270Sstevel@tonic-gate 			return (E2BIG);
14280Sstevel@tonic-gate 		}
14290Sstevel@tonic-gate 		/* Use the link-layer broadcast address for MULTI_BCAST */
14300Sstevel@tonic-gate 		phys_addr = bphys_addr;
14310Sstevel@tonic-gate 		bzero(&v6_extract_mask, sizeof (v6_extract_mask));
14320Sstevel@tonic-gate 		hw_extract_start = ill->ill_nd_lla_len;
14330Sstevel@tonic-gate 	} else {
14340Sstevel@tonic-gate 		phys_addr = alloc_phys;
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_BROADCAST) ||
14370Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST) ||
14380Sstevel@tonic-gate 	    (phyi->phyint_flags & PHYI_MULTI_BCAST)) {
14393448Sdh155122 		mutex_enter(&ipst->ips_ndp6->ndp_g_lock);
14404714Ssowmini 		err = ndp_add_v6(ill,
14410Sstevel@tonic-gate 		    phys_addr,
14420Sstevel@tonic-gate 		    &v6_mcast_addr,	/* v6 address */
14430Sstevel@tonic-gate 		    &v6_mcast_mask,	/* v6 mask */
14440Sstevel@tonic-gate 		    &v6_extract_mask,
14450Sstevel@tonic-gate 		    hw_extract_start,
14460Sstevel@tonic-gate 		    NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD,
14470Sstevel@tonic-gate 		    ND_REACHABLE,
14484714Ssowmini 		    &mnce);
14493448Sdh155122 		mutex_exit(&ipst->ips_ndp6->ndp_g_lock);
14500Sstevel@tonic-gate 		if (err == 0) {
14510Sstevel@tonic-gate 			if (ret_nce != NULL) {
14520Sstevel@tonic-gate 				*ret_nce = mnce;
14530Sstevel@tonic-gate 			} else {
14540Sstevel@tonic-gate 				NCE_REFRELE(mnce);
14550Sstevel@tonic-gate 			}
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 	kmem_free(alloc_phys, ill->ill_nd_lla_len);
14590Sstevel@tonic-gate 	return (err);
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate /*
14634972Smeem  * Get the resolver set up for a new ipif.  (Always called as writer.)
14640Sstevel@tonic-gate  */
14650Sstevel@tonic-gate int
14668485SPeter.Memishian@Sun.COM ipif_ndp_up(ipif_t *ipif, boolean_t initial)
14670Sstevel@tonic-gate {
14680Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
14690Sstevel@tonic-gate 	int		err = 0;
14700Sstevel@tonic-gate 	nce_t		*nce = NULL;
14710Sstevel@tonic-gate 	nce_t		*mnce = NULL;
14728485SPeter.Memishian@Sun.COM 	boolean_t	added_ipif = B_FALSE;
14738485SPeter.Memishian@Sun.COM 
14748485SPeter.Memishian@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
14754972Smeem 	ip1dbg(("ipif_ndp_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	/*
14780Sstevel@tonic-gate 	 * ND not supported on XRESOLV interfaces. If ND support (multicast)
14790Sstevel@tonic-gate 	 * added later, take out this check.
14800Sstevel@tonic-gate 	 */
14812546Scarlsonj 	if ((ill->ill_flags & ILLF_XRESOLV) ||
14824972Smeem 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) ||
14832546Scarlsonj 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
14842546Scarlsonj 		ipif->ipif_addr_ready = 1;
14850Sstevel@tonic-gate 		return (0);
14862546Scarlsonj 	}
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	/*
14890Sstevel@tonic-gate 	 * Need to setup multicast mapping only when the first
14900Sstevel@tonic-gate 	 * interface is coming UP.
14910Sstevel@tonic-gate 	 */
14920Sstevel@tonic-gate 	if (ill->ill_ipif_up_count == 0 &&
14930Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST)) {
14940Sstevel@tonic-gate 		/*
14950Sstevel@tonic-gate 		 * We set the multicast before setting up the mapping for
14960Sstevel@tonic-gate 		 * local address because ipif_ndp_setup_multicast does
14970Sstevel@tonic-gate 		 * ndp_walk to delete nces which will delete the mapping
14980Sstevel@tonic-gate 		 * for local address also if we added the mapping for
14990Sstevel@tonic-gate 		 * local address first.
15000Sstevel@tonic-gate 		 */
15010Sstevel@tonic-gate 		err = ipif_ndp_setup_multicast(ipif, &mnce);
15020Sstevel@tonic-gate 		if (err != 0)
15030Sstevel@tonic-gate 			return (err);
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
15070Sstevel@tonic-gate 		uint16_t	flags;
15088485SPeter.Memishian@Sun.COM 		uint16_t	state;
15098485SPeter.Memishian@Sun.COM 		uchar_t		*hw_addr = NULL;
15108485SPeter.Memishian@Sun.COM 		ill_t		*bound_ill;
15118485SPeter.Memishian@Sun.COM 		ipmp_illgrp_t	*illg = ill->ill_grp;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 		/* Permanent entries don't need NUD */
15143340Smeem 		flags = NCE_F_PERMANENT | NCE_F_NONUD;
15150Sstevel@tonic-gate 		if (ill->ill_flags & ILLF_ROUTER)
15160Sstevel@tonic-gate 			flags |= NCE_F_ISROUTER;
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_ANYCAST)
15190Sstevel@tonic-gate 			flags |= NCE_F_ANYCAST;
15200Sstevel@tonic-gate 
15218485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill)) {
15228485SPeter.Memishian@Sun.COM 			ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
15238485SPeter.Memishian@Sun.COM 			/*
15248485SPeter.Memishian@Sun.COM 			 * If we're here via ipif_up(), then the ipif won't be
15258485SPeter.Memishian@Sun.COM 			 * bound yet -- add it to the group, which will bind
15268485SPeter.Memishian@Sun.COM 			 * it if possible.  (We would add it in ipif_up(), but
15278485SPeter.Memishian@Sun.COM 			 * deleting on failure there is gruesome.)  If we're
15288485SPeter.Memishian@Sun.COM 			 * here via ipmp_ill_bind_ipif(), then the ipif has
15298485SPeter.Memishian@Sun.COM 			 * already been added to the group and we just need to
15308485SPeter.Memishian@Sun.COM 			 * use the binding.
15318485SPeter.Memishian@Sun.COM 			 */
15328485SPeter.Memishian@Sun.COM 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
15338485SPeter.Memishian@Sun.COM 				bound_ill = ipmp_illgrp_add_ipif(illg, ipif);
15348485SPeter.Memishian@Sun.COM 				if (bound_ill == NULL) {
15358485SPeter.Memishian@Sun.COM 					/*
15368485SPeter.Memishian@Sun.COM 					 * We couldn't bind the ipif to an ill
15378485SPeter.Memishian@Sun.COM 					 * yet, so we have nothing to publish.
15388485SPeter.Memishian@Sun.COM 					 * Set ipif_addr_ready so that this
15398485SPeter.Memishian@Sun.COM 					 * address can be used locally for now.
15408485SPeter.Memishian@Sun.COM 					 * The routing socket message will be
15418485SPeter.Memishian@Sun.COM 					 * sent from ipif_up_done_v6().
15428485SPeter.Memishian@Sun.COM 					 */
15438485SPeter.Memishian@Sun.COM 					ipif->ipif_addr_ready = 1;
15448485SPeter.Memishian@Sun.COM 					return (0);
15458485SPeter.Memishian@Sun.COM 				}
15468485SPeter.Memishian@Sun.COM 				added_ipif = B_TRUE;
15470Sstevel@tonic-gate 			}
15488485SPeter.Memishian@Sun.COM 			hw_addr = bound_ill->ill_nd_lla;
15498485SPeter.Memishian@Sun.COM 		} else {
15508485SPeter.Memishian@Sun.COM 			bound_ill = ill;
15518485SPeter.Memishian@Sun.COM 			if (ill->ill_net_type == IRE_IF_RESOLVER)
15528485SPeter.Memishian@Sun.COM 				hw_addr = ill->ill_nd_lla;
15530Sstevel@tonic-gate 		}
15548485SPeter.Memishian@Sun.COM 
15558485SPeter.Memishian@Sun.COM 		/*
15568485SPeter.Memishian@Sun.COM 		 * If this is an initial bring-up (or the ipif was never
15578485SPeter.Memishian@Sun.COM 		 * completely brought up), do DAD.  Otherwise, we're here
15588485SPeter.Memishian@Sun.COM 		 * because IPMP has rebound an address to this ill: send
15598485SPeter.Memishian@Sun.COM 		 * unsolicited advertisements to inform others.
15608485SPeter.Memishian@Sun.COM 		 */
15618485SPeter.Memishian@Sun.COM 		if (initial || !ipif->ipif_addr_ready) {
15628485SPeter.Memishian@Sun.COM 			state = ND_PROBE;
15638485SPeter.Memishian@Sun.COM 		} else {
15648485SPeter.Memishian@Sun.COM 			state = ND_REACHABLE;
15658485SPeter.Memishian@Sun.COM 			flags |= NCE_F_UNSOL_ADV;
15668485SPeter.Memishian@Sun.COM 		}
15678485SPeter.Memishian@Sun.COM 		/*
15689287SSowmini.Varadhan@Sun.COM 		 * Create an nce for the local address. We pass a match_illgrp
15699287SSowmini.Varadhan@Sun.COM 		 * of B_TRUE because the local address must be unique across
15709287SSowmini.Varadhan@Sun.COM 		 * the illgrp, and the existence of an nce with nce_ill set
15719287SSowmini.Varadhan@Sun.COM 		 * to any ill in the group is indicative of a duplicate address
15728485SPeter.Memishian@Sun.COM 		 */
15738485SPeter.Memishian@Sun.COM 		err = ndp_lookup_then_add_v6(bound_ill,
15749287SSowmini.Varadhan@Sun.COM 		    B_TRUE,
15750Sstevel@tonic-gate 		    hw_addr,
15764972Smeem 		    &ipif->ipif_v6lcl_addr,
15770Sstevel@tonic-gate 		    &ipv6_all_ones,
15780Sstevel@tonic-gate 		    &ipv6_all_zeros,
15790Sstevel@tonic-gate 		    0,
15800Sstevel@tonic-gate 		    flags,
15818485SPeter.Memishian@Sun.COM 		    state,
15824714Ssowmini 		    &nce);
15830Sstevel@tonic-gate 		switch (err) {
15840Sstevel@tonic-gate 		case 0:
15850Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
15860Sstevel@tonic-gate 			    ill->ill_name));
15872546Scarlsonj 			ipif->ipif_addr_ready = 1;
15889287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
15899571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
15902546Scarlsonj 			break;
15912546Scarlsonj 		case EINPROGRESS:
15922546Scarlsonj 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
15932546Scarlsonj 			    ill->ill_name));
15949287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
15959571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
15960Sstevel@tonic-gate 			break;
15970Sstevel@tonic-gate 		case EEXIST:
15980Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
15990Sstevel@tonic-gate 			    ill->ill_name));
16009571SSowmini.Varadhan@Sun.COM 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
16019571SSowmini.Varadhan@Sun.COM 				NCE_REFRELE(nce);
16029571SSowmini.Varadhan@Sun.COM 				goto fail;
16039571SSowmini.Varadhan@Sun.COM 			}
16049571SSowmini.Varadhan@Sun.COM 			/*
16059571SSowmini.Varadhan@Sun.COM 			 * Duplicate local addresses are permissible for
16069571SSowmini.Varadhan@Sun.COM 			 * IPIF_POINTOPOINT interfaces which will get marked
16079571SSowmini.Varadhan@Sun.COM 			 * IPIF_UNNUMBERED later in
16089571SSowmini.Varadhan@Sun.COM 			 * ip_addr_availability_check().
16099571SSowmini.Varadhan@Sun.COM 			 *
16109571SSowmini.Varadhan@Sun.COM 			 * The nce_ipif_cnt field tracks the number of
16119571SSowmini.Varadhan@Sun.COM 			 * ipifs that have nce_addr as their local address.
16129571SSowmini.Varadhan@Sun.COM 			 */
16139571SSowmini.Varadhan@Sun.COM 			ipif->ipif_addr_ready = 1;
16149571SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
16159571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
16169571SSowmini.Varadhan@Sun.COM 			break;
16170Sstevel@tonic-gate 		default:
16188485SPeter.Memishian@Sun.COM 			ip1dbg(("ipif_ndp_up: NCE creation failed for %s\n",
16190Sstevel@tonic-gate 			    ill->ill_name));
16208485SPeter.Memishian@Sun.COM 			goto fail;
16210Sstevel@tonic-gate 		}
16222546Scarlsonj 	} else {
16232546Scarlsonj 		/* No local NCE for this entry */
16242546Scarlsonj 		ipif->ipif_addr_ready = 1;
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 	if (nce != NULL)
16270Sstevel@tonic-gate 		NCE_REFRELE(nce);
16280Sstevel@tonic-gate 	if (mnce != NULL)
16290Sstevel@tonic-gate 		NCE_REFRELE(mnce);
16300Sstevel@tonic-gate 	return (0);
16318485SPeter.Memishian@Sun.COM fail:
16328485SPeter.Memishian@Sun.COM 	if (mnce != NULL) {
16338485SPeter.Memishian@Sun.COM 		ndp_delete(mnce);
16348485SPeter.Memishian@Sun.COM 		NCE_REFRELE(mnce);
16358485SPeter.Memishian@Sun.COM 	}
16368485SPeter.Memishian@Sun.COM 	if (added_ipif)
16378485SPeter.Memishian@Sun.COM 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
16388485SPeter.Memishian@Sun.COM 
16398485SPeter.Memishian@Sun.COM 	return (err);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate /* Remove all cache entries for this logical interface */
16430Sstevel@tonic-gate void
16440Sstevel@tonic-gate ipif_ndp_down(ipif_t *ipif)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	nce_t	*nce;
16478485SPeter.Memishian@Sun.COM 	ill_t	*ill = ipif->ipif_ill;
16488485SPeter.Memishian@Sun.COM 
16498485SPeter.Memishian@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
16500Sstevel@tonic-gate 
16512535Ssangeeta 	if (ipif->ipif_isv6) {
16528485SPeter.Memishian@Sun.COM 		ill_t *bound_ill;
16538485SPeter.Memishian@Sun.COM 
16548485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill))
16558485SPeter.Memishian@Sun.COM 			bound_ill = ipmp_ipif_bound_ill(ipif);
16568485SPeter.Memishian@Sun.COM 		else
16578485SPeter.Memishian@Sun.COM 			bound_ill = ill;
16588485SPeter.Memishian@Sun.COM 
16599287SSowmini.Varadhan@Sun.COM 		if (bound_ill != NULL && ipif->ipif_added_nce) {
16608485SPeter.Memishian@Sun.COM 			nce = ndp_lookup_v6(bound_ill,
16619287SSowmini.Varadhan@Sun.COM 			    B_TRUE,
16628485SPeter.Memishian@Sun.COM 			    &ipif->ipif_v6lcl_addr,
16638485SPeter.Memishian@Sun.COM 			    B_FALSE);
16649571SSowmini.Varadhan@Sun.COM 			if (nce == NULL)
16659571SSowmini.Varadhan@Sun.COM 				goto no_nce;
16669571SSowmini.Varadhan@Sun.COM 			if (--nce->nce_ipif_cnt == 0)
16679571SSowmini.Varadhan@Sun.COM 				ndp_delete(nce); /* last ipif for nce */
16689287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 0;
16699571SSowmini.Varadhan@Sun.COM 			NCE_REFRELE(nce);
16702535Ssangeeta 		}
16719571SSowmini.Varadhan@Sun.COM no_nce:
16728485SPeter.Memishian@Sun.COM 		/*
16738485SPeter.Memishian@Sun.COM 		 * Make IPMP aware of the deleted data address.
16748485SPeter.Memishian@Sun.COM 		 */
16758485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill))
16768485SPeter.Memishian@Sun.COM 			ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
16770Sstevel@tonic-gate 	}
16788485SPeter.Memishian@Sun.COM 
16790Sstevel@tonic-gate 	/*
16800Sstevel@tonic-gate 	 * Remove mapping and all other nces dependent on this ill
16810Sstevel@tonic-gate 	 * when the last ipif is going away.
16820Sstevel@tonic-gate 	 */
16838485SPeter.Memishian@Sun.COM 	if (ill->ill_ipif_up_count == 0)
16848485SPeter.Memishian@Sun.COM 		ndp_walk(ill, (pfi_t)ndp_delete_per_ill, ill, ill->ill_ipst);
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * Used when an interface comes up to recreate any extra routes on this
16890Sstevel@tonic-gate  * interface.
16900Sstevel@tonic-gate  */
16910Sstevel@tonic-gate static ire_t **
16920Sstevel@tonic-gate ipif_recover_ire_v6(ipif_t *ipif)
16930Sstevel@tonic-gate {
16940Sstevel@tonic-gate 	mblk_t	*mp;
16950Sstevel@tonic-gate 	ire_t   **ipif_saved_irep;
16960Sstevel@tonic-gate 	ire_t   **irep;
16973448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name,
17000Sstevel@tonic-gate 	    ipif->ipif_id));
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_saved_ire_lock);
17050Sstevel@tonic-gate 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
17060Sstevel@tonic-gate 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
17070Sstevel@tonic-gate 	if (ipif_saved_irep == NULL) {
17080Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
17090Sstevel@tonic-gate 		return (NULL);
17100Sstevel@tonic-gate 	}
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	irep = ipif_saved_irep;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
17150Sstevel@tonic-gate 		ire_t		*ire;
17160Sstevel@tonic-gate 		queue_t		*rfq;
17170Sstevel@tonic-gate 		queue_t		*stq;
17180Sstevel@tonic-gate 		ifrt_t		*ifrt;
17190Sstevel@tonic-gate 		in6_addr_t	*src_addr;
17200Sstevel@tonic-gate 		in6_addr_t	*gateway_addr;
17210Sstevel@tonic-gate 		char		buf[INET6_ADDRSTRLEN];
17220Sstevel@tonic-gate 		ushort_t	type;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 		/*
17250Sstevel@tonic-gate 		 * When the ire was initially created and then added in
17260Sstevel@tonic-gate 		 * ip_rt_add_v6(), it was created either using
17270Sstevel@tonic-gate 		 * ipif->ipif_net_type in the case of a traditional interface
17280Sstevel@tonic-gate 		 * route, or as one of the IRE_OFFSUBNET types (with the
17293004Sdd193516 		 * exception of IRE_HOST type redirect ire which is created by
17300Sstevel@tonic-gate 		 * icmp_redirect_v6() and which we don't need to save or
17310Sstevel@tonic-gate 		 * recover).  In the case where ipif->ipif_net_type was
17320Sstevel@tonic-gate 		 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to
17330Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy
17340Sstevel@tonic-gate 		 * software like GateD and Sun Cluster which creates routes
17350Sstevel@tonic-gate 		 * using the the loopback interface's address as a gateway.
17360Sstevel@tonic-gate 		 *
17374714Ssowmini 		 * As ifrt->ifrt_type reflects the already updated ire_type,
17384714Ssowmini 		 * ire_create_v6() will be called in the same way here as in
17394714Ssowmini 		 * ip_rt_add_v6(), namely using ipif->ipif_net_type when the
17404714Ssowmini 		 * route looks like a traditional interface route (where
17414714Ssowmini 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise
17424714Ssowmini 		 * using the saved ifrt->ifrt_type.  This means that in
17434714Ssowmini 		 * the case where ipif->ipif_net_type is IRE_LOOPBACK,
17444714Ssowmini 		 * the ire created by ire_create_v6() will be an IRE_LOOPBACK,
17454714Ssowmini 		 * it will then be turned into an IRE_IF_NORESOLVER and then
17464714Ssowmini 		 * added by ire_add_v6().
17470Sstevel@tonic-gate 		 */
17480Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
17490Sstevel@tonic-gate 		if (ifrt->ifrt_type & IRE_INTERFACE) {
17500Sstevel@tonic-gate 			rfq = NULL;
17510Sstevel@tonic-gate 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
17520Sstevel@tonic-gate 			    ? ipif->ipif_rq : ipif->ipif_wq;
17530Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
17540Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr
17550Sstevel@tonic-gate 			    : &ipif->ipif_v6src_addr;
17560Sstevel@tonic-gate 			gateway_addr = NULL;
17570Sstevel@tonic-gate 			type = ipif->ipif_net_type;
17580Sstevel@tonic-gate 		} else {
17590Sstevel@tonic-gate 			rfq = NULL;
17600Sstevel@tonic-gate 			stq = NULL;
17610Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
17620Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr : NULL;
17630Sstevel@tonic-gate 			gateway_addr = &ifrt->ifrt_v6gateway_addr;
17640Sstevel@tonic-gate 			type = ifrt->ifrt_type;
17650Sstevel@tonic-gate 		}
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 		/*
17680Sstevel@tonic-gate 		 * Create a copy of the IRE with the saved address and netmask.
17690Sstevel@tonic-gate 		 */
17700Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n",
17710Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
17720Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)),
17730Sstevel@tonic-gate 		    ip_mask_to_plen_v6(&ifrt->ifrt_v6mask)));
17740Sstevel@tonic-gate 		ire = ire_create_v6(
17750Sstevel@tonic-gate 		    &ifrt->ifrt_v6addr,
17760Sstevel@tonic-gate 		    &ifrt->ifrt_v6mask,
17770Sstevel@tonic-gate 		    src_addr,
17780Sstevel@tonic-gate 		    gateway_addr,
17790Sstevel@tonic-gate 		    &ifrt->ifrt_max_frag,
17800Sstevel@tonic-gate 		    NULL,
17810Sstevel@tonic-gate 		    rfq,
17820Sstevel@tonic-gate 		    stq,
17830Sstevel@tonic-gate 		    type,
17840Sstevel@tonic-gate 		    ipif,
17850Sstevel@tonic-gate 		    NULL,
17860Sstevel@tonic-gate 		    0,
17870Sstevel@tonic-gate 		    0,
17880Sstevel@tonic-gate 		    ifrt->ifrt_flags,
17891676Sjpk 		    &ifrt->ifrt_iulp_info,
17901676Sjpk 		    NULL,
17913448Sdh155122 		    NULL,
17923448Sdh155122 		    ipst);
17930Sstevel@tonic-gate 		if (ire == NULL) {
17940Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
17950Sstevel@tonic-gate 			kmem_free(ipif_saved_irep,
17960Sstevel@tonic-gate 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
17970Sstevel@tonic-gate 			return (NULL);
17980Sstevel@tonic-gate 		}
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		/*
18010Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
18020Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
18030Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
18040Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
18050Sstevel@tonic-gate 		 * when generating aggregate routes.)
18060Sstevel@tonic-gate 		 *
18070Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18080Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
18090Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
18100Sstevel@tonic-gate 		 */
18110Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18120Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
18130Sstevel@tonic-gate 		/*
18140Sstevel@tonic-gate 		 * ire held by ire_add, will be refreled' in ipif_up_done
18150Sstevel@tonic-gate 		 * towards the end
18160Sstevel@tonic-gate 		 */
18172535Ssangeeta 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
18180Sstevel@tonic-gate 		*irep = ire;
18190Sstevel@tonic-gate 		irep++;
18200Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire));
18210Sstevel@tonic-gate 	}
18220Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_saved_ire_lock);
18230Sstevel@tonic-gate 	return (ipif_saved_irep);
18240Sstevel@tonic-gate }
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate /*
18270Sstevel@tonic-gate  * Return the scope of the given IPv6 address.  If the address is an
18280Sstevel@tonic-gate  * IPv4 mapped IPv6 address, return the scope of the corresponding
18290Sstevel@tonic-gate  * IPv4 address.
18300Sstevel@tonic-gate  */
18310Sstevel@tonic-gate in6addr_scope_t
18320Sstevel@tonic-gate ip_addr_scope_v6(const in6_addr_t *addr)
18330Sstevel@tonic-gate {
18340Sstevel@tonic-gate 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
18370Sstevel@tonic-gate 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
18380Sstevel@tonic-gate 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
18390Sstevel@tonic-gate 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
18400Sstevel@tonic-gate 			return (IP6_SCOPE_LINKLOCAL);
18410Sstevel@tonic-gate 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
18420Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
18430Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
18440Sstevel@tonic-gate 			return (IP6_SCOPE_SITELOCAL);
18450Sstevel@tonic-gate 		return (IP6_SCOPE_GLOBAL);
18460Sstevel@tonic-gate 	}
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	if (IN6_IS_ADDR_MULTICAST(addr))
18490Sstevel@tonic-gate 		return (IN6_ADDR_MC_SCOPE(addr));
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	/* link-local and loopback addresses are of link-local scope */
18520Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
18530Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
18540Sstevel@tonic-gate 		return (IP6_SCOPE_LINKLOCAL);
18550Sstevel@tonic-gate 	if (IN6_IS_ADDR_SITELOCAL(addr))
18560Sstevel@tonic-gate 		return (IP6_SCOPE_SITELOCAL);
18570Sstevel@tonic-gate 	return (IP6_SCOPE_GLOBAL);
18580Sstevel@tonic-gate }
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate /*
18623431Scarlsonj  * Returns the length of the common prefix of a1 and a2, as per
18633431Scarlsonj  * CommonPrefixLen() defined in RFC 3484.
18640Sstevel@tonic-gate  */
18653431Scarlsonj static int
18663431Scarlsonj ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2)
18670Sstevel@tonic-gate {
18680Sstevel@tonic-gate 	int i;
18693431Scarlsonj 	uint32_t a1val, a2val, mask;
18700Sstevel@tonic-gate 
18713431Scarlsonj 	for (i = 0; i < 4; i++) {
18723431Scarlsonj 		if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) {
18733431Scarlsonj 			a1val ^= a2val;
18743431Scarlsonj 			i *= 32;
18753431Scarlsonj 			mask = 0x80000000u;
18763431Scarlsonj 			while (!(a1val & mask)) {
18773431Scarlsonj 				mask >>= 1;
18783431Scarlsonj 				i++;
18793431Scarlsonj 			}
18803431Scarlsonj 			return (i);
18813431Scarlsonj 		}
18823431Scarlsonj 	}
18833431Scarlsonj 	return (IPV6_ABITS);
18840Sstevel@tonic-gate }
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate #define	IPIF_VALID_IPV6_SOURCE(ipif) \
18870Sstevel@tonic-gate 	(((ipif)->ipif_flags & IPIF_UP) && \
18882546Scarlsonj 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
18892546Scarlsonj 	(ipif)->ipif_addr_ready)
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate /* source address candidate */
18920Sstevel@tonic-gate typedef struct candidate {
18930Sstevel@tonic-gate 	ipif_t		*cand_ipif;
18940Sstevel@tonic-gate 	/* The properties of this candidate */
18950Sstevel@tonic-gate 	boolean_t	cand_isdst;
18960Sstevel@tonic-gate 	boolean_t	cand_isdst_set;
18970Sstevel@tonic-gate 	in6addr_scope_t	cand_scope;
18980Sstevel@tonic-gate 	boolean_t	cand_scope_set;
18990Sstevel@tonic-gate 	boolean_t	cand_isdeprecated;
19000Sstevel@tonic-gate 	boolean_t	cand_isdeprecated_set;
19010Sstevel@tonic-gate 	boolean_t	cand_ispreferred;
19020Sstevel@tonic-gate 	boolean_t	cand_ispreferred_set;
19030Sstevel@tonic-gate 	boolean_t	cand_matchedinterface;
19040Sstevel@tonic-gate 	boolean_t	cand_matchedinterface_set;
19050Sstevel@tonic-gate 	boolean_t	cand_matchedlabel;
19060Sstevel@tonic-gate 	boolean_t	cand_matchedlabel_set;
19070Sstevel@tonic-gate 	boolean_t	cand_istmp;
19080Sstevel@tonic-gate 	boolean_t	cand_istmp_set;
19093431Scarlsonj 	int		cand_common_pref;
19103431Scarlsonj 	boolean_t	cand_common_pref_set;
19113431Scarlsonj 	boolean_t	cand_pref_eq;
19123431Scarlsonj 	boolean_t	cand_pref_eq_set;
19133431Scarlsonj 	int		cand_pref_len;
19143431Scarlsonj 	boolean_t	cand_pref_len_set;
19150Sstevel@tonic-gate } cand_t;
19160Sstevel@tonic-gate #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
19173431Scarlsonj #define	cand_mask	cand_ipif->ipif_v6net_mask
19180Sstevel@tonic-gate #define	cand_flags	cand_ipif->ipif_flags
19190Sstevel@tonic-gate #define	cand_ill	cand_ipif->ipif_ill
19201676Sjpk #define	cand_zoneid	cand_ipif->ipif_zoneid
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate /* information about the destination for source address selection */
19230Sstevel@tonic-gate typedef struct dstinfo {
19240Sstevel@tonic-gate 	const in6_addr_t	*dst_addr;
19250Sstevel@tonic-gate 	ill_t			*dst_ill;
19262202Srk129064 	uint_t			dst_restrict_ill;
19270Sstevel@tonic-gate 	boolean_t		dst_prefer_src_tmp;
19280Sstevel@tonic-gate 	in6addr_scope_t		dst_scope;
19290Sstevel@tonic-gate 	char			*dst_label;
19300Sstevel@tonic-gate } dstinfo_t;
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate /*
19330Sstevel@tonic-gate  * The following functions are rules used to select a source address in
19340Sstevel@tonic-gate  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
19350Sstevel@tonic-gate  * against the best candidate (bc).  Each rule has three possible outcomes;
19360Sstevel@tonic-gate  * the candidate is preferred over the best candidate (CAND_PREFER), the
19370Sstevel@tonic-gate  * candidate is not preferred over the best candidate (CAND_AVOID), or the
19380Sstevel@tonic-gate  * candidate is of equal value as the best candidate (CAND_TIE).
19390Sstevel@tonic-gate  *
19400Sstevel@tonic-gate  * These rules are part of a greater "Default Address Selection for IPv6"
19410Sstevel@tonic-gate  * sheme, which is standards based work coming out of the IETF ipv6 working
19420Sstevel@tonic-gate  * group.  The IETF document defines both IPv6 source address selection and
19430Sstevel@tonic-gate  * destination address ordering.  The rules defined here implement the IPv6
19440Sstevel@tonic-gate  * source address selection.  Destination address ordering is done by
19450Sstevel@tonic-gate  * libnsl, and uses a similar set of rules to implement the sorting.
19463431Scarlsonj  *
19473431Scarlsonj  * Most of the rules are defined by the RFC and are not typically altered.  The
19483431Scarlsonj  * last rule, number 8, has language that allows for local preferences.  In the
19493431Scarlsonj  * scheme below, this means that new Solaris rules should normally go between
19503431Scarlsonj  * rule_ifprefix and rule_prefix.
19510Sstevel@tonic-gate  */
19520Sstevel@tonic-gate typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
19533448Sdh155122 typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *,
19543448Sdh155122     ip_stack_t *);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate /* Prefer an address if it is equal to the destination address. */
19573448Sdh155122 /* ARGSUSED3 */
19580Sstevel@tonic-gate static rule_res_t
19593448Sdh155122 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
19600Sstevel@tonic-gate {
19610Sstevel@tonic-gate 	if (!bc->cand_isdst_set) {
19620Sstevel@tonic-gate 		bc->cand_isdst =
19630Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
19640Sstevel@tonic-gate 		bc->cand_isdst_set = B_TRUE;
19650Sstevel@tonic-gate 	}
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	cc->cand_isdst =
19680Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
19690Sstevel@tonic-gate 	cc->cand_isdst_set = B_TRUE;
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	if (cc->cand_isdst == bc->cand_isdst)
19720Sstevel@tonic-gate 		return (CAND_TIE);
19730Sstevel@tonic-gate 	else if (cc->cand_isdst)
19740Sstevel@tonic-gate 		return (CAND_PREFER);
19750Sstevel@tonic-gate 	else
19760Sstevel@tonic-gate 		return (CAND_AVOID);
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate /*
19800Sstevel@tonic-gate  * Prefer addresses that are of closest scope to the destination.  Always
19810Sstevel@tonic-gate  * prefer addresses that are of greater scope than the destination over
19820Sstevel@tonic-gate  * those that are of lesser scope than the destination.
19830Sstevel@tonic-gate  */
19843448Sdh155122 /* ARGSUSED3 */
19850Sstevel@tonic-gate static rule_res_t
19863448Sdh155122 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
19870Sstevel@tonic-gate {
19880Sstevel@tonic-gate 	if (!bc->cand_scope_set) {
19890Sstevel@tonic-gate 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
19900Sstevel@tonic-gate 		bc->cand_scope_set = B_TRUE;
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
19940Sstevel@tonic-gate 	cc->cand_scope_set = B_TRUE;
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	if (cc->cand_scope < bc->cand_scope) {
19970Sstevel@tonic-gate 		if (cc->cand_scope < dstinfo->dst_scope)
19980Sstevel@tonic-gate 			return (CAND_AVOID);
19990Sstevel@tonic-gate 		else
20000Sstevel@tonic-gate 			return (CAND_PREFER);
20010Sstevel@tonic-gate 	} else if (bc->cand_scope < cc->cand_scope) {
20020Sstevel@tonic-gate 		if (bc->cand_scope < dstinfo->dst_scope)
20030Sstevel@tonic-gate 			return (CAND_PREFER);
20040Sstevel@tonic-gate 		else
20050Sstevel@tonic-gate 			return (CAND_AVOID);
20060Sstevel@tonic-gate 	} else {
20070Sstevel@tonic-gate 		return (CAND_TIE);
20080Sstevel@tonic-gate 	}
20090Sstevel@tonic-gate }
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate /*
20120Sstevel@tonic-gate  * Prefer non-deprecated source addresses.
20130Sstevel@tonic-gate  */
20140Sstevel@tonic-gate /* ARGSUSED2 */
20150Sstevel@tonic-gate static rule_res_t
20163448Sdh155122 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20173448Sdh155122     ip_stack_t *ipst)
20180Sstevel@tonic-gate {
20190Sstevel@tonic-gate 	if (!bc->cand_isdeprecated_set) {
20200Sstevel@tonic-gate 		bc->cand_isdeprecated =
20210Sstevel@tonic-gate 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
20220Sstevel@tonic-gate 		bc->cand_isdeprecated_set = B_TRUE;
20230Sstevel@tonic-gate 	}
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
20260Sstevel@tonic-gate 	cc->cand_isdeprecated_set = B_TRUE;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
20290Sstevel@tonic-gate 		return (CAND_TIE);
20300Sstevel@tonic-gate 	else if (cc->cand_isdeprecated)
20310Sstevel@tonic-gate 		return (CAND_AVOID);
20320Sstevel@tonic-gate 	else
20330Sstevel@tonic-gate 		return (CAND_PREFER);
20340Sstevel@tonic-gate }
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate /*
20370Sstevel@tonic-gate  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
20380Sstevel@tonic-gate  * rule must be before rule_interface because the flag could be set on any
20390Sstevel@tonic-gate  * interface, not just the interface being used for outgoing packets (for
20400Sstevel@tonic-gate  * example, the IFF_PREFERRED could be set on an address assigned to the
20410Sstevel@tonic-gate  * loopback interface).
20420Sstevel@tonic-gate  */
20430Sstevel@tonic-gate /* ARGSUSED2 */
20440Sstevel@tonic-gate static rule_res_t
20453448Sdh155122 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20463448Sdh155122     ip_stack_t *ipst)
20470Sstevel@tonic-gate {
20480Sstevel@tonic-gate 	if (!bc->cand_ispreferred_set) {
20490Sstevel@tonic-gate 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
20500Sstevel@tonic-gate 		bc->cand_ispreferred_set = B_TRUE;
20510Sstevel@tonic-gate 	}
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
20540Sstevel@tonic-gate 	cc->cand_ispreferred_set = B_TRUE;
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	if (bc->cand_ispreferred == cc->cand_ispreferred)
20570Sstevel@tonic-gate 		return (CAND_TIE);
20580Sstevel@tonic-gate 	else if (cc->cand_ispreferred)
20590Sstevel@tonic-gate 		return (CAND_PREFER);
20600Sstevel@tonic-gate 	else
20610Sstevel@tonic-gate 		return (CAND_AVOID);
20620Sstevel@tonic-gate }
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate /*
20658485SPeter.Memishian@Sun.COM  * Prefer source addresses that are assigned to the outgoing interface.
20660Sstevel@tonic-gate  */
20673448Sdh155122 /* ARGSUSED3 */
20680Sstevel@tonic-gate static rule_res_t
20693448Sdh155122 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20703448Sdh155122     ip_stack_t *ipst)
20710Sstevel@tonic-gate {
20720Sstevel@tonic-gate 	ill_t *dstill = dstinfo->dst_ill;
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	/*
20750Sstevel@tonic-gate 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
20760Sstevel@tonic-gate 	 * since we know all candidates will be on the same link.
20770Sstevel@tonic-gate 	 */
20780Sstevel@tonic-gate 	if (dstinfo->dst_restrict_ill)
20790Sstevel@tonic-gate 		return (CAND_TIE);
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	if (!bc->cand_matchedinterface_set) {
20828485SPeter.Memishian@Sun.COM 		bc->cand_matchedinterface = bc->cand_ill == dstill;
20830Sstevel@tonic-gate 		bc->cand_matchedinterface_set = B_TRUE;
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 
20868485SPeter.Memishian@Sun.COM 	cc->cand_matchedinterface = cc->cand_ill == dstill;
20870Sstevel@tonic-gate 	cc->cand_matchedinterface_set = B_TRUE;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
20900Sstevel@tonic-gate 		return (CAND_TIE);
20910Sstevel@tonic-gate 	else if (cc->cand_matchedinterface)
20920Sstevel@tonic-gate 		return (CAND_PREFER);
20930Sstevel@tonic-gate 	else
20940Sstevel@tonic-gate 		return (CAND_AVOID);
20950Sstevel@tonic-gate }
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate /*
20980Sstevel@tonic-gate  * Prefer source addresses whose label matches the destination's label.
20990Sstevel@tonic-gate  */
21000Sstevel@tonic-gate static rule_res_t
21013448Sdh155122 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
21020Sstevel@tonic-gate {
21030Sstevel@tonic-gate 	char *label;
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	if (!bc->cand_matchedlabel_set) {
21063448Sdh155122 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst);
21070Sstevel@tonic-gate 		bc->cand_matchedlabel =
21080Sstevel@tonic-gate 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
21090Sstevel@tonic-gate 		bc->cand_matchedlabel_set = B_TRUE;
21100Sstevel@tonic-gate 	}
21110Sstevel@tonic-gate 
21123448Sdh155122 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst);
21130Sstevel@tonic-gate 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
21140Sstevel@tonic-gate 	cc->cand_matchedlabel_set = B_TRUE;
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
21170Sstevel@tonic-gate 		return (CAND_TIE);
21180Sstevel@tonic-gate 	else if (cc->cand_matchedlabel)
21190Sstevel@tonic-gate 		return (CAND_PREFER);
21200Sstevel@tonic-gate 	else
21210Sstevel@tonic-gate 		return (CAND_AVOID);
21220Sstevel@tonic-gate }
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate /*
21250Sstevel@tonic-gate  * Prefer public addresses over temporary ones.  An application can reverse
21260Sstevel@tonic-gate  * the logic of this rule and prefer temporary addresses by using the
21270Sstevel@tonic-gate  * IPV6_SRC_PREFERENCES socket option.
21280Sstevel@tonic-gate  */
21293448Sdh155122 /* ARGSUSED3 */
21300Sstevel@tonic-gate static rule_res_t
21313448Sdh155122 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
21323448Sdh155122     ip_stack_t *ipst)
21330Sstevel@tonic-gate {
21340Sstevel@tonic-gate 	if (!bc->cand_istmp_set) {
21350Sstevel@tonic-gate 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
21360Sstevel@tonic-gate 		bc->cand_istmp_set = B_TRUE;
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
21400Sstevel@tonic-gate 	cc->cand_istmp_set = B_TRUE;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	if (bc->cand_istmp == cc->cand_istmp)
21430Sstevel@tonic-gate 		return (CAND_TIE);
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
21460Sstevel@tonic-gate 		return (CAND_PREFER);
21470Sstevel@tonic-gate 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
21480Sstevel@tonic-gate 		return (CAND_PREFER);
21490Sstevel@tonic-gate 	else
21500Sstevel@tonic-gate 		return (CAND_AVOID);
21510Sstevel@tonic-gate }
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate /*
21543431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination
21553431Scarlsonj  * under the interface mask.  This gets us on the same subnet before applying
21563431Scarlsonj  * any Solaris-specific rules.
21570Sstevel@tonic-gate  */
21583448Sdh155122 /* ARGSUSED3 */
21590Sstevel@tonic-gate static rule_res_t
21603448Sdh155122 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
21613448Sdh155122     ip_stack_t *ipst)
21620Sstevel@tonic-gate {
21633431Scarlsonj 	if (!bc->cand_pref_eq_set) {
21643431Scarlsonj 		bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr,
21653431Scarlsonj 		    bc->cand_mask, *dstinfo->dst_addr);
21663431Scarlsonj 		bc->cand_pref_eq_set = B_TRUE;
21670Sstevel@tonic-gate 	}
21680Sstevel@tonic-gate 
21693431Scarlsonj 	cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask,
21703431Scarlsonj 	    *dstinfo->dst_addr);
21713431Scarlsonj 	cc->cand_pref_eq_set = B_TRUE;
21720Sstevel@tonic-gate 
21733431Scarlsonj 	if (bc->cand_pref_eq) {
21743431Scarlsonj 		if (cc->cand_pref_eq) {
21753431Scarlsonj 			if (!bc->cand_pref_len_set) {
21763431Scarlsonj 				bc->cand_pref_len =
21773431Scarlsonj 				    ip_mask_to_plen_v6(&bc->cand_mask);
21783431Scarlsonj 				bc->cand_pref_len_set = B_TRUE;
21793431Scarlsonj 			}
21803431Scarlsonj 			cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask);
21813431Scarlsonj 			cc->cand_pref_len_set = B_TRUE;
21823431Scarlsonj 			if (bc->cand_pref_len == cc->cand_pref_len)
21833431Scarlsonj 				return (CAND_TIE);
21843431Scarlsonj 			else if (bc->cand_pref_len > cc->cand_pref_len)
21853431Scarlsonj 				return (CAND_AVOID);
21863431Scarlsonj 			else
21873431Scarlsonj 				return (CAND_PREFER);
21883431Scarlsonj 		} else {
21893431Scarlsonj 			return (CAND_AVOID);
21903431Scarlsonj 		}
21913431Scarlsonj 	} else {
21923431Scarlsonj 		if (cc->cand_pref_eq)
21933431Scarlsonj 			return (CAND_PREFER);
21943431Scarlsonj 		else
21953431Scarlsonj 			return (CAND_TIE);
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate }
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate /*
22001676Sjpk  * Prefer to use zone-specific addresses when possible instead of all-zones
22011676Sjpk  * addresses.
22021676Sjpk  */
22031676Sjpk /* ARGSUSED2 */
22041676Sjpk static rule_res_t
22053448Sdh155122 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
22063448Sdh155122     ip_stack_t *ipst)
22071676Sjpk {
22081676Sjpk 	if ((bc->cand_zoneid == ALL_ZONES) ==
22091676Sjpk 	    (cc->cand_zoneid == ALL_ZONES))
22101676Sjpk 		return (CAND_TIE);
22111676Sjpk 	else if (cc->cand_zoneid == ALL_ZONES)
22121676Sjpk 		return (CAND_AVOID);
22131676Sjpk 	else
22141676Sjpk 		return (CAND_PREFER);
22151676Sjpk }
22161676Sjpk 
22171676Sjpk /*
22183431Scarlsonj  * Prefer to use DHCPv6 (first) and static addresses (second) when possible
22193431Scarlsonj  * instead of statelessly autoconfigured addresses.
22203431Scarlsonj  *
22213431Scarlsonj  * This is done after trying all other preferences (and before the final tie
22223431Scarlsonj  * breaker) so that, if all else is equal, we select addresses configured by
22233431Scarlsonj  * DHCPv6 over other addresses.  We presume that DHCPv6 addresses, unlike
22243431Scarlsonj  * stateless autoconfigured addresses, are deliberately configured by an
22253431Scarlsonj  * administrator, and thus are correctly set up in DNS and network packet
22263431Scarlsonj  * filters.
22273431Scarlsonj  */
22283431Scarlsonj /* ARGSUSED2 */
22293431Scarlsonj static rule_res_t
22303448Sdh155122 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
22313448Sdh155122     ip_stack_t *ipst)
22323431Scarlsonj {
22333431Scarlsonj #define	ATYPE(x)	\
22343431Scarlsonj 	((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2
22353431Scarlsonj 	int bcval = ATYPE(bc->cand_flags);
22363431Scarlsonj 	int ccval = ATYPE(cc->cand_flags);
22373431Scarlsonj #undef ATYPE
22383431Scarlsonj 
22393431Scarlsonj 	if (bcval == ccval)
22403431Scarlsonj 		return (CAND_TIE);
22413431Scarlsonj 	else if (ccval < bcval)
22423431Scarlsonj 		return (CAND_PREFER);
22433431Scarlsonj 	else
22443431Scarlsonj 		return (CAND_AVOID);
22453431Scarlsonj }
22463431Scarlsonj 
22473431Scarlsonj /*
22483431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination.
22493431Scarlsonj  * We do the longest matching prefix calculation by doing an xor of both
22503431Scarlsonj  * addresses with the destination, and pick the address with the longest string
22513431Scarlsonj  * of leading zeros, as per CommonPrefixLen() defined in RFC 3484.
22523431Scarlsonj  */
22533448Sdh155122 /* ARGSUSED3 */
22543431Scarlsonj static rule_res_t
22553448Sdh155122 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
22563431Scarlsonj {
22578485SPeter.Memishian@Sun.COM 	/*
22588485SPeter.Memishian@Sun.COM 	 * For IPMP, we always want to choose a random source address from
22598485SPeter.Memishian@Sun.COM 	 * among any equally usable addresses, so always report a tie.
22608485SPeter.Memishian@Sun.COM 	 */
22618485SPeter.Memishian@Sun.COM 	if (IS_IPMP(dstinfo->dst_ill))
22628485SPeter.Memishian@Sun.COM 		return (CAND_TIE);
22638485SPeter.Memishian@Sun.COM 
22643431Scarlsonj 	if (!bc->cand_common_pref_set) {
22653431Scarlsonj 		bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr,
22663431Scarlsonj 		    dstinfo->dst_addr);
22673431Scarlsonj 		bc->cand_common_pref_set = B_TRUE;
22683431Scarlsonj 	}
22693431Scarlsonj 
22703431Scarlsonj 	cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr,
22713431Scarlsonj 	    dstinfo->dst_addr);
22723431Scarlsonj 	cc->cand_common_pref_set = B_TRUE;
22733431Scarlsonj 
22743431Scarlsonj 	if (bc->cand_common_pref == cc->cand_common_pref)
22753431Scarlsonj 		return (CAND_TIE);
22763431Scarlsonj 	else if (bc->cand_common_pref > cc->cand_common_pref)
22773431Scarlsonj 		return (CAND_AVOID);
22783431Scarlsonj 	else
22793431Scarlsonj 		return (CAND_PREFER);
22803431Scarlsonj }
22813431Scarlsonj 
22823431Scarlsonj /*
22833431Scarlsonj  * Last rule: we must pick something, so just prefer the current best
22843431Scarlsonj  * candidate.
22853431Scarlsonj  */
22863431Scarlsonj /* ARGSUSED */
22873431Scarlsonj static rule_res_t
22883448Sdh155122 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
22893448Sdh155122     ip_stack_t *ipst)
22903431Scarlsonj {
22913431Scarlsonj 	return (CAND_AVOID);
22923431Scarlsonj }
22933431Scarlsonj 
22943431Scarlsonj /*
22950Sstevel@tonic-gate  * Determine the best source address given a destination address and a
22960Sstevel@tonic-gate  * destination ill.  If no suitable source address is found, it returns
22970Sstevel@tonic-gate  * NULL. If there is a usable address pointed to by the usesrc
22980Sstevel@tonic-gate  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
22990Sstevel@tonic-gate  * fine grained (i.e per interface)
23000Sstevel@tonic-gate  *
23010Sstevel@tonic-gate  * This implementation is based on the "Default Address Selection for IPv6"
23020Sstevel@tonic-gate  * specification produced by the IETF IPv6 working group.  It has been
23030Sstevel@tonic-gate  * implemented so that the list of addresses is only traversed once (the
23040Sstevel@tonic-gate  * specification's algorithm could traverse the list of addresses once for
23050Sstevel@tonic-gate  * every rule).
23060Sstevel@tonic-gate  *
23078485SPeter.Memishian@Sun.COM  * The restrict_ill argument restricts the algorithm to choose a source
23088485SPeter.Memishian@Sun.COM  * address that is assigned to the destination ill.  This is used when
23098485SPeter.Memishian@Sun.COM  * the destination address is a link-local or multicast address, and when
23100Sstevel@tonic-gate  * ipv6_strict_dst_multihoming is turned on.
23110Sstevel@tonic-gate  *
23120Sstevel@tonic-gate  * src_prefs is the caller's set of source address preferences.  If source
23130Sstevel@tonic-gate  * address selection is being called to determine the source address of a
23140Sstevel@tonic-gate  * connected socket (from ip_bind_connected_v6()), then the preferences are
23150Sstevel@tonic-gate  * taken from conn_src_preferences.  These preferences can be set on a
23160Sstevel@tonic-gate  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
23170Sstevel@tonic-gate  * preference currently implemented is for rfc3041 temporary addresses.
23180Sstevel@tonic-gate  */
23190Sstevel@tonic-gate ipif_t *
23200Sstevel@tonic-gate ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
23218485SPeter.Memishian@Sun.COM     boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid)
23220Sstevel@tonic-gate {
23230Sstevel@tonic-gate 	dstinfo_t	dstinfo;
23240Sstevel@tonic-gate 	char		dstr[INET6_ADDRSTRLEN];
23250Sstevel@tonic-gate 	char		sstr[INET6_ADDRSTRLEN];
23268485SPeter.Memishian@Sun.COM 	ipif_t		*ipif, *start_ipif, *next_ipif;
23278485SPeter.Memishian@Sun.COM 	ill_t		*ill, *usesrc_ill = NULL, *ipmp_ill = NULL;
23280Sstevel@tonic-gate 	ill_walk_context_t	ctx;
23290Sstevel@tonic-gate 	cand_t		best_c;	/* The best candidate */
23300Sstevel@tonic-gate 	cand_t		curr_c;	/* The current candidate */
23310Sstevel@tonic-gate 	uint_t		index;
23320Sstevel@tonic-gate 	boolean_t	first_candidate = B_TRUE;
23330Sstevel@tonic-gate 	rule_res_t	rule_result;
23341676Sjpk 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
23353448Sdh155122 	ip_stack_t	*ipst = dstill->ill_ipst;
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 	/*
23380Sstevel@tonic-gate 	 * The list of ordering rules.  They are applied in the order they
23390Sstevel@tonic-gate 	 * appear in the list.
23400Sstevel@tonic-gate 	 *
23413431Scarlsonj 	 * Solaris doesn't currently support Mobile IPv6, so there's no
23423431Scarlsonj 	 * rule_mipv6 corresponding to rule 4 in the specification.
23430Sstevel@tonic-gate 	 */
23440Sstevel@tonic-gate 	rulef_t	rules[] = {
23450Sstevel@tonic-gate 		rule_isdst,
23460Sstevel@tonic-gate 		rule_scope,
23470Sstevel@tonic-gate 		rule_deprecated,
23480Sstevel@tonic-gate 		rule_preferred,
23490Sstevel@tonic-gate 		rule_interface,
23500Sstevel@tonic-gate 		rule_label,
23510Sstevel@tonic-gate 		rule_temporary,
23523431Scarlsonj 		rule_ifprefix,			/* local rules after this */
23531676Sjpk 		rule_zone_specific,
23543431Scarlsonj 		rule_addr_type,
23553431Scarlsonj 		rule_prefix,			/* local rules before this */
23563431Scarlsonj 		rule_must_be_last,		/* must always be last */
23570Sstevel@tonic-gate 		NULL
23580Sstevel@tonic-gate 	};
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	ASSERT(dstill->ill_isv6);
23610Sstevel@tonic-gate 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	/*
23640Sstevel@tonic-gate 	 * Check if there is a usable src address pointed to by the
23650Sstevel@tonic-gate 	 * usesrc ifindex. This has higher precedence since it is
23660Sstevel@tonic-gate 	 * finer grained (i.e per interface) v/s being system wide.
23670Sstevel@tonic-gate 	 */
23680Sstevel@tonic-gate 	if (dstill->ill_usesrc_ifindex != 0) {
23690Sstevel@tonic-gate 		if ((usesrc_ill =
23700Sstevel@tonic-gate 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
23713448Sdh155122 		    NULL, NULL, NULL, NULL, ipst)) != NULL) {
23720Sstevel@tonic-gate 			dstinfo.dst_ill = usesrc_ill;
23730Sstevel@tonic-gate 		} else {
23740Sstevel@tonic-gate 			return (NULL);
23750Sstevel@tonic-gate 		}
23768485SPeter.Memishian@Sun.COM 	} else if (IS_UNDER_IPMP(dstill)) {
23778485SPeter.Memishian@Sun.COM 		/*
23788485SPeter.Memishian@Sun.COM 		 * Test addresses should never be used for source address
23798485SPeter.Memishian@Sun.COM 		 * selection, so if we were passed an underlying ill, switch
23808485SPeter.Memishian@Sun.COM 		 * to the IPMP meta-interface.
23818485SPeter.Memishian@Sun.COM 		 */
23828485SPeter.Memishian@Sun.COM 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(dstill)) != NULL)
23838485SPeter.Memishian@Sun.COM 			dstinfo.dst_ill = ipmp_ill;
23848485SPeter.Memishian@Sun.COM 		else
23858485SPeter.Memishian@Sun.COM 			return (NULL);
23860Sstevel@tonic-gate 	} else {
23870Sstevel@tonic-gate 		dstinfo.dst_ill = dstill;
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23901676Sjpk 	/*
23911676Sjpk 	 * If we're dealing with an unlabeled destination on a labeled system,
23921676Sjpk 	 * make sure that we ignore source addresses that are incompatible with
23931676Sjpk 	 * the destination's default label.  That destination's default label
23941676Sjpk 	 * must dominate the minimum label on the source address.
23951676Sjpk 	 *
23961676Sjpk 	 * (Note that this has to do with Trusted Solaris.  It's not related to
23971676Sjpk 	 * the labels described by ip6_asp_lookup.)
23981676Sjpk 	 */
23991676Sjpk 	dst_rhtp = NULL;
24001676Sjpk 	if (is_system_labeled()) {
24011676Sjpk 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
24021676Sjpk 		if (dst_rhtp == NULL)
24031676Sjpk 			return (NULL);
24041676Sjpk 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
24051676Sjpk 			TPC_RELE(dst_rhtp);
24061676Sjpk 			dst_rhtp = NULL;
24071676Sjpk 		}
24081676Sjpk 	}
24091676Sjpk 
24100Sstevel@tonic-gate 	dstinfo.dst_addr = dst;
24110Sstevel@tonic-gate 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
24123448Sdh155122 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst);
24130Sstevel@tonic-gate 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
24143448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24150Sstevel@tonic-gate 	/*
24160Sstevel@tonic-gate 	 * Section three of the I-D states that for multicast and
24170Sstevel@tonic-gate 	 * link-local destinations, the candidate set must be restricted to
24180Sstevel@tonic-gate 	 * an interface that is on the same link as the outgoing interface.
24190Sstevel@tonic-gate 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
24200Sstevel@tonic-gate 	 * restrict the source address to the destination link as doing
24210Sstevel@tonic-gate 	 * otherwise will almost certainly cause problems.
24220Sstevel@tonic-gate 	 */
24230Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
24243448Sdh155122 	    ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
24258485SPeter.Memishian@Sun.COM 		dstinfo.dst_restrict_ill = B_TRUE;
24262202Srk129064 	} else {
24270Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = restrict_ill;
24282202Srk129064 	}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	bzero(&best_c, sizeof (cand_t));
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	/*
24338485SPeter.Memishian@Sun.COM 	 * Take a pass through the list of IPv6 interfaces to choose the best
24348485SPeter.Memishian@Sun.COM 	 * possible source address.  If restrict_ill is set, just use dst_ill.
24350Sstevel@tonic-gate 	 */
24368485SPeter.Memishian@Sun.COM 	if (dstinfo.dst_restrict_ill)
24378485SPeter.Memishian@Sun.COM 		ill = dstinfo.dst_ill;
24388485SPeter.Memishian@Sun.COM 	else
24393448Sdh155122 		ill = ILL_START_WALK_V6(&ctx, ipst);
24408485SPeter.Memishian@Sun.COM 
24418485SPeter.Memishian@Sun.COM 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
24420Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
24430Sstevel@tonic-gate 
24442202Srk129064 		/*
24458485SPeter.Memishian@Sun.COM 		 * Test addresses should never be used for source address
24468485SPeter.Memishian@Sun.COM 		 * selection, so ignore underlying ills.
24472202Srk129064 		 */
24488485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ill))
24498485SPeter.Memishian@Sun.COM 			continue;
24508485SPeter.Memishian@Sun.COM 
24519658SSowmini.Varadhan@Sun.COM 		if (ill->ill_ipif == NULL)
24529658SSowmini.Varadhan@Sun.COM 			continue;
24538485SPeter.Memishian@Sun.COM 		/*
24548485SPeter.Memishian@Sun.COM 		 * For source address selection, we treat the ipif list as
24558485SPeter.Memishian@Sun.COM 		 * circular and continue until we get back to where we
24568485SPeter.Memishian@Sun.COM 		 * started.  This allows IPMP to vary source address selection
24578485SPeter.Memishian@Sun.COM 		 * (which improves inbound load spreading) by caching its last
24588485SPeter.Memishian@Sun.COM 		 * ending point and starting from there.  NOTE: we don't have
24598485SPeter.Memishian@Sun.COM 		 * to worry about ill_src_ipif changing ills since that can't
24608485SPeter.Memishian@Sun.COM 		 * happen on the IPMP ill.
24618485SPeter.Memishian@Sun.COM 		 */
24628485SPeter.Memishian@Sun.COM 		start_ipif = ill->ill_ipif;
24638485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
24648485SPeter.Memishian@Sun.COM 			start_ipif = ill->ill_src_ipif;
24658485SPeter.Memishian@Sun.COM 
24668485SPeter.Memishian@Sun.COM 		ipif = start_ipif;
24678485SPeter.Memishian@Sun.COM 		do {
24688485SPeter.Memishian@Sun.COM 			if ((next_ipif = ipif->ipif_next) == NULL)
24698485SPeter.Memishian@Sun.COM 				next_ipif = ill->ill_ipif;
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
24720Sstevel@tonic-gate 				continue;
24730Sstevel@tonic-gate 
24741676Sjpk 			if (zoneid != ALL_ZONES &&
24751676Sjpk 			    ipif->ipif_zoneid != zoneid &&
24761676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
24770Sstevel@tonic-gate 				continue;
24780Sstevel@tonic-gate 
24791676Sjpk 			/*
24801676Sjpk 			 * Check compatibility of local address for
24811676Sjpk 			 * destination's default label if we're on a labeled
24821676Sjpk 			 * system.  Incompatible addresses can't be used at
24831676Sjpk 			 * all and must be skipped over.
24841676Sjpk 			 */
24851676Sjpk 			if (dst_rhtp != NULL) {
24861676Sjpk 				boolean_t incompat;
24871676Sjpk 
24881676Sjpk 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
24891676Sjpk 				    IPV6_VERSION, B_FALSE);
24901676Sjpk 				if (src_rhtp == NULL)
24911676Sjpk 					continue;
24921676Sjpk 				incompat =
24931676Sjpk 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
24941676Sjpk 				    src_rhtp->tpc_tp.tp_doi !=
24951676Sjpk 				    dst_rhtp->tpc_tp.tp_doi ||
24961676Sjpk 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
24971676Sjpk 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
24981676Sjpk 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
24991676Sjpk 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
25001676Sjpk 				TPC_RELE(src_rhtp);
25011676Sjpk 				if (incompat)
25021676Sjpk 					continue;
25031676Sjpk 			}
25041676Sjpk 
25050Sstevel@tonic-gate 			if (first_candidate) {
25060Sstevel@tonic-gate 				/*
25070Sstevel@tonic-gate 				 * This is first valid address in the list.
25080Sstevel@tonic-gate 				 * It is automatically the best candidate
25090Sstevel@tonic-gate 				 * so far.
25100Sstevel@tonic-gate 				 */
25110Sstevel@tonic-gate 				best_c.cand_ipif = ipif;
25120Sstevel@tonic-gate 				first_candidate = B_FALSE;
25130Sstevel@tonic-gate 				continue;
25140Sstevel@tonic-gate 			}
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 			bzero(&curr_c, sizeof (cand_t));
25170Sstevel@tonic-gate 			curr_c.cand_ipif = ipif;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 			/*
25200Sstevel@tonic-gate 			 * Compare this current candidate (curr_c) with the
25210Sstevel@tonic-gate 			 * best candidate (best_c) by applying the
25220Sstevel@tonic-gate 			 * comparison rules in order until one breaks the
25230Sstevel@tonic-gate 			 * tie.
25240Sstevel@tonic-gate 			 */
25250Sstevel@tonic-gate 			for (index = 0; rules[index] != NULL; index++) {
25260Sstevel@tonic-gate 				/* Apply a comparison rule. */
25278485SPeter.Memishian@Sun.COM 				rule_result = (rules[index])(&best_c, &curr_c,
25288485SPeter.Memishian@Sun.COM 				    &dstinfo, ipst);
25290Sstevel@tonic-gate 				if (rule_result == CAND_AVOID) {
25300Sstevel@tonic-gate 					/*
25310Sstevel@tonic-gate 					 * The best candidate is still the
25320Sstevel@tonic-gate 					 * best candidate.  Forget about
25330Sstevel@tonic-gate 					 * this current candidate and go on
25340Sstevel@tonic-gate 					 * to the next one.
25350Sstevel@tonic-gate 					 */
25360Sstevel@tonic-gate 					break;
25370Sstevel@tonic-gate 				} else if (rule_result == CAND_PREFER) {
25380Sstevel@tonic-gate 					/*
25390Sstevel@tonic-gate 					 * This candidate is prefered.  It
25400Sstevel@tonic-gate 					 * becomes the best candidate so
25410Sstevel@tonic-gate 					 * far.  Go on to the next address.
25420Sstevel@tonic-gate 					 */
25430Sstevel@tonic-gate 					best_c = curr_c;
25440Sstevel@tonic-gate 					break;
25450Sstevel@tonic-gate 				}
25460Sstevel@tonic-gate 				/* We have a tie, apply the next rule. */
25470Sstevel@tonic-gate 			}
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 			/*
25500Sstevel@tonic-gate 			 * The last rule must be a tie breaker rule and
25510Sstevel@tonic-gate 			 * must never produce a tie.  At this point, the
25520Sstevel@tonic-gate 			 * candidate should have either been rejected, or
25530Sstevel@tonic-gate 			 * have been prefered as the best candidate so far.
25540Sstevel@tonic-gate 			 */
25550Sstevel@tonic-gate 			ASSERT(rule_result != CAND_TIE);
25568485SPeter.Memishian@Sun.COM 		} while ((ipif = next_ipif) != start_ipif);
25578485SPeter.Memishian@Sun.COM 
25588485SPeter.Memishian@Sun.COM 		/*
25598485SPeter.Memishian@Sun.COM 		 * For IPMP, update the source ipif rotor to the next ipif,
25608485SPeter.Memishian@Sun.COM 		 * provided we can look it up.  (We must not use it if it's
25618485SPeter.Memishian@Sun.COM 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
25628485SPeter.Memishian@Sun.COM 		 * ipif_free() checked ill_src_ipif.)
25638485SPeter.Memishian@Sun.COM 		 */
25648485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill) && ipif != NULL) {
25658485SPeter.Memishian@Sun.COM 			mutex_enter(&ipif->ipif_ill->ill_lock);
25668485SPeter.Memishian@Sun.COM 			next_ipif = ipif->ipif_next;
25678485SPeter.Memishian@Sun.COM 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
25688485SPeter.Memishian@Sun.COM 				ill->ill_src_ipif = next_ipif;
25698485SPeter.Memishian@Sun.COM 			else
25708485SPeter.Memishian@Sun.COM 				ill->ill_src_ipif = NULL;
25718485SPeter.Memishian@Sun.COM 			mutex_exit(&ipif->ipif_ill->ill_lock);
25720Sstevel@tonic-gate 		}
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 		/*
25758485SPeter.Memishian@Sun.COM 		 * Only one ill to consider if dst_restrict_ill is set.
25760Sstevel@tonic-gate 		 */
25778485SPeter.Memishian@Sun.COM 		if (dstinfo.dst_restrict_ill)
25788485SPeter.Memishian@Sun.COM 			break;
25790Sstevel@tonic-gate 	}
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	ipif = best_c.cand_ipif;
25820Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
25830Sstevel@tonic-gate 	    dstinfo.dst_ill->ill_name,
25840Sstevel@tonic-gate 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
25850Sstevel@tonic-gate 	    (ipif == NULL ? "NULL" :
25860Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	if (usesrc_ill != NULL)
25890Sstevel@tonic-gate 		ill_refrele(usesrc_ill);
25900Sstevel@tonic-gate 
25918485SPeter.Memishian@Sun.COM 	if (ipmp_ill != NULL)
25928485SPeter.Memishian@Sun.COM 		ill_refrele(ipmp_ill);
25938485SPeter.Memishian@Sun.COM 
25941676Sjpk 	if (dst_rhtp != NULL)
25951676Sjpk 		TPC_RELE(dst_rhtp);
25961676Sjpk 
25970Sstevel@tonic-gate 	if (ipif == NULL) {
25983448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
25990Sstevel@tonic-gate 		return (NULL);
26000Sstevel@tonic-gate 	}
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_ill->ill_lock);
26030Sstevel@tonic-gate 	if (IPIF_CAN_LOOKUP(ipif)) {
26040Sstevel@tonic-gate 		ipif_refhold_locked(ipif);
26050Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_ill->ill_lock);
26063448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
26070Sstevel@tonic-gate 		return (ipif);
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_ill->ill_lock);
26103448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
26110Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
26120Sstevel@tonic-gate 	    " returning null \n", (void *)ipif));
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	return (NULL);
26150Sstevel@tonic-gate }
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate /*
26180Sstevel@tonic-gate  * If old_ipif is not NULL, see if ipif was derived from old
26190Sstevel@tonic-gate  * ipif and if so, recreate the interface route by re-doing
26200Sstevel@tonic-gate  * source address selection. This happens when ipif_down ->
26210Sstevel@tonic-gate  * ipif_update_other_ipifs calls us.
26220Sstevel@tonic-gate  *
26230Sstevel@tonic-gate  * If old_ipif is NULL, just redo the source address selection
26248485SPeter.Memishian@Sun.COM  * if needed. This happens when ipif_up_done_v6 calls us.
26250Sstevel@tonic-gate  */
26260Sstevel@tonic-gate void
26270Sstevel@tonic-gate ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif)
26280Sstevel@tonic-gate {
26290Sstevel@tonic-gate 	ire_t *ire;
26300Sstevel@tonic-gate 	ire_t *ipif_ire;
26310Sstevel@tonic-gate 	queue_t *stq;
26320Sstevel@tonic-gate 	ill_t *ill;
26330Sstevel@tonic-gate 	ipif_t *nipif = NULL;
26340Sstevel@tonic-gate 	boolean_t nipif_refheld = B_FALSE;
26350Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
26363448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	ill = ipif->ipif_ill;
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	if (!(ipif->ipif_flags &
26410Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
26420Sstevel@tonic-gate 		/*
26430Sstevel@tonic-gate 		 * Can't possibly have borrowed the source
26440Sstevel@tonic-gate 		 * from old_ipif.
26450Sstevel@tonic-gate 		 */
26460Sstevel@tonic-gate 		return;
26470Sstevel@tonic-gate 	}
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	/*
26500Sstevel@tonic-gate 	 * Is there any work to be done? No work if the address
26510Sstevel@tonic-gate 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
26520Sstevel@tonic-gate 	 * ipif_select_source_v6() does not borrow addresses from
26530Sstevel@tonic-gate 	 * NOLOCAL and ANYCAST interfaces).
26540Sstevel@tonic-gate 	 */
26550Sstevel@tonic-gate 	if ((old_ipif != NULL) &&
26560Sstevel@tonic-gate 	    ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) ||
26570Sstevel@tonic-gate 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
26580Sstevel@tonic-gate 	    (old_ipif->ipif_flags &
26590Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
26600Sstevel@tonic-gate 		return;
26610Sstevel@tonic-gate 	}
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	/*
26640Sstevel@tonic-gate 	 * Perform the same checks as when creating the
26650Sstevel@tonic-gate 	 * IRE_INTERFACE in ipif_up_done_v6.
26660Sstevel@tonic-gate 	 */
26670Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_UP))
26680Sstevel@tonic-gate 		return;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_NOXMIT))
26710Sstevel@tonic-gate 		return;
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
26740Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
26750Sstevel@tonic-gate 		return;
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	/*
26780Sstevel@tonic-gate 	 * We know that ipif uses some other source for its
26790Sstevel@tonic-gate 	 * IRE_INTERFACE. Is it using the source of this
26800Sstevel@tonic-gate 	 * old_ipif?
26810Sstevel@tonic-gate 	 */
26820Sstevel@tonic-gate 	ipif_ire = ipif_to_ire_v6(ipif);
26830Sstevel@tonic-gate 	if (ipif_ire == NULL)
26840Sstevel@tonic-gate 		return;
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	if (old_ipif != NULL &&
26870Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr,
26880Sstevel@tonic-gate 	    &ipif_ire->ire_src_addr_v6)) {
26890Sstevel@tonic-gate 		ire_refrele(ipif_ire);
26900Sstevel@tonic-gate 		return;
26910Sstevel@tonic-gate 	}
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	if (ip_debug > 2) {
26940Sstevel@tonic-gate 		/* ip1dbg */
26950Sstevel@tonic-gate 		pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE"
26960Sstevel@tonic-gate 		    " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6);
26970Sstevel@tonic-gate 	}
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 	stq = ipif_ire->ire_stq;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 	/*
27020Sstevel@tonic-gate 	 * Can't use our source address. Select a different source address
27030Sstevel@tonic-gate 	 * for the IRE_INTERFACE.  We restrict interface route source
27040Sstevel@tonic-gate 	 * address selection to ipif's assigned to the same link as the
27050Sstevel@tonic-gate 	 * interface.
27060Sstevel@tonic-gate 	 */
27073448Sdh155122 	if (ip6_asp_can_lookup(ipst)) {
27080Sstevel@tonic-gate 		ip6_asp_table_held = B_TRUE;
27090Sstevel@tonic-gate 		nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet,
27108485SPeter.Memishian@Sun.COM 		    B_TRUE, IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
27110Sstevel@tonic-gate 	}
27120Sstevel@tonic-gate 	if (nipif == NULL) {
27130Sstevel@tonic-gate 		/* Last resort - all ipif's have IPIF_NOLOCAL */
27140Sstevel@tonic-gate 		nipif = ipif;
27150Sstevel@tonic-gate 	} else {
27160Sstevel@tonic-gate 		nipif_refheld = B_TRUE;
27170Sstevel@tonic-gate 	}
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	ire = ire_create_v6(
27200Sstevel@tonic-gate 	    &ipif->ipif_v6subnet,	/* dest pref */
27210Sstevel@tonic-gate 	    &ipif->ipif_v6net_mask,	/* mask */
27220Sstevel@tonic-gate 	    &nipif->ipif_v6src_addr,	/* src addr */
27230Sstevel@tonic-gate 	    NULL,			/* no gateway */
27240Sstevel@tonic-gate 	    &ipif->ipif_mtu,		/* max frag */
27254714Ssowmini 	    NULL,			/* no src nce */
27260Sstevel@tonic-gate 	    NULL,			/* no recv from queue */
27270Sstevel@tonic-gate 	    stq,			/* send-to queue */
27280Sstevel@tonic-gate 	    ill->ill_net_type,		/* IF_[NO]RESOLVER */
27290Sstevel@tonic-gate 	    ipif,
27300Sstevel@tonic-gate 	    NULL,
27310Sstevel@tonic-gate 	    0,
27320Sstevel@tonic-gate 	    0,
27330Sstevel@tonic-gate 	    0,
27341676Sjpk 	    &ire_uinfo_null,
27351676Sjpk 	    NULL,
27363448Sdh155122 	    NULL,
27373448Sdh155122 	    ipst);
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	if (ire != NULL) {
27400Sstevel@tonic-gate 		ire_t *ret_ire;
27410Sstevel@tonic-gate 		int   error;
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 		/*
27440Sstevel@tonic-gate 		 * We don't need ipif_ire anymore. We need to delete
27450Sstevel@tonic-gate 		 * before we add so that ire_add does not detect
27460Sstevel@tonic-gate 		 * duplicates.
27470Sstevel@tonic-gate 		 */
27480Sstevel@tonic-gate 		ire_delete(ipif_ire);
27490Sstevel@tonic-gate 		ret_ire = ire;
27502535Ssangeeta 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
27510Sstevel@tonic-gate 		ASSERT(error == 0);
27520Sstevel@tonic-gate 		ASSERT(ret_ire == ire);
27530Sstevel@tonic-gate 		if (ret_ire != NULL) {
27540Sstevel@tonic-gate 			/* Held in ire_add */
27550Sstevel@tonic-gate 			ire_refrele(ret_ire);
27560Sstevel@tonic-gate 		}
27570Sstevel@tonic-gate 	}
27580Sstevel@tonic-gate 	/*
27590Sstevel@tonic-gate 	 * Either we are falling through from above or could not
27600Sstevel@tonic-gate 	 * allocate a replacement.
27610Sstevel@tonic-gate 	 */
27620Sstevel@tonic-gate 	ire_refrele(ipif_ire);
27630Sstevel@tonic-gate 	if (ip6_asp_table_held)
27643448Sdh155122 		ip6_asp_table_refrele(ipst);
27650Sstevel@tonic-gate 	if (nipif_refheld)
27660Sstevel@tonic-gate 		ipif_refrele(nipif);
27670Sstevel@tonic-gate }
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate /*
27700Sstevel@tonic-gate  * This old_ipif is going away.
27710Sstevel@tonic-gate  *
27720Sstevel@tonic-gate  * Determine if any other ipif's are using our address as
27730Sstevel@tonic-gate  * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
27740Sstevel@tonic-gate  * IPIF_DEPRECATED).
27750Sstevel@tonic-gate  * Find the IRE_INTERFACE for such ipif's and recreate them
27760Sstevel@tonic-gate  * to use an different source address following the rules in
27770Sstevel@tonic-gate  * ipif_up_done_v6.
27780Sstevel@tonic-gate  */
27790Sstevel@tonic-gate void
27808485SPeter.Memishian@Sun.COM ipif_update_other_ipifs_v6(ipif_t *old_ipif)
27810Sstevel@tonic-gate {
27820Sstevel@tonic-gate 	ipif_t	*ipif;
27830Sstevel@tonic-gate 	ill_t	*ill;
27840Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	ASSERT(IAM_WRITER_IPIF(old_ipif));
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	ill = old_ipif->ipif_ill;
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 	ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n",
27910Sstevel@tonic-gate 	    ill->ill_name,
27920Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr,
27930Sstevel@tonic-gate 	    buf, sizeof (buf))));
27940Sstevel@tonic-gate 
27958485SPeter.Memishian@Sun.COM 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
27968485SPeter.Memishian@Sun.COM 		if (ipif != old_ipif)
27970Sstevel@tonic-gate 			ipif_recreate_interface_routes_v6(old_ipif, ipif);
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate }
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate /*
28020Sstevel@tonic-gate  * Perform an attach and bind to get phys addr plus info_req for
28030Sstevel@tonic-gate  * the physical device.
28040Sstevel@tonic-gate  * q and mp represents an ioctl which will be queued waiting for
28050Sstevel@tonic-gate  * completion of the DLPI message exchange.
28060Sstevel@tonic-gate  * MUST be called on an ill queue. Can not set conn_pending_ill for that
28070Sstevel@tonic-gate  * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q.
28080Sstevel@tonic-gate  *
28090Sstevel@tonic-gate  * Returns EINPROGRESS when mp has been consumed by queueing it on
28100Sstevel@tonic-gate  * ill_pending_mp and the ioctl will complete in ip_rput.
28110Sstevel@tonic-gate  */
28120Sstevel@tonic-gate int
28130Sstevel@tonic-gate ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate 	mblk_t	*v6token_mp = NULL;
28160Sstevel@tonic-gate 	mblk_t	*v6lla_mp = NULL;
28170Sstevel@tonic-gate 	mblk_t	*phys_mp = NULL;
28180Sstevel@tonic-gate 	mblk_t	*info_mp = NULL;
28190Sstevel@tonic-gate 	mblk_t	*attach_mp = NULL;
28200Sstevel@tonic-gate 	mblk_t	*bind_mp = NULL;
28210Sstevel@tonic-gate 	mblk_t	*unbind_mp = NULL;
28220Sstevel@tonic-gate 	mblk_t	*notify_mp = NULL;
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
28250Sstevel@tonic-gate 	ASSERT(ill->ill_dlpi_style_set);
28260Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	if (ill->ill_isv6) {
28290Sstevel@tonic-gate 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
28300Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
28310Sstevel@tonic-gate 		if (v6token_mp == NULL)
28320Sstevel@tonic-gate 			goto bad;
28330Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
28340Sstevel@tonic-gate 		    DL_IPV6_TOKEN;
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
28370Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
28380Sstevel@tonic-gate 		if (v6lla_mp == NULL)
28390Sstevel@tonic-gate 			goto bad;
28400Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
28410Sstevel@tonic-gate 		    DL_IPV6_LINK_LAYER_ADDR;
28420Sstevel@tonic-gate 	}
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	/*
28450Sstevel@tonic-gate 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
28460Sstevel@tonic-gate 	 */
28470Sstevel@tonic-gate 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
28480Sstevel@tonic-gate 	    DL_NOTIFY_REQ);
28490Sstevel@tonic-gate 	if (notify_mp == NULL)
28500Sstevel@tonic-gate 		goto bad;
28510Sstevel@tonic-gate 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
28520Sstevel@tonic-gate 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
28539073SCathy.Zhou@Sun.COM 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG |
2854*9743SGirish.Moodalbail@Sun.COM 	    DL_NOTE_PROMISC_ON_PHYS | DL_NOTE_PROMISC_OFF_PHYS |
28559073SCathy.Zhou@Sun.COM 	    DL_NOTE_REPLUMB);
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
28580Sstevel@tonic-gate 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
28590Sstevel@tonic-gate 	if (phys_mp == NULL)
28600Sstevel@tonic-gate 		goto bad;
28610Sstevel@tonic-gate 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
28620Sstevel@tonic-gate 	    DL_CURR_PHYS_ADDR;
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	info_mp = ip_dlpi_alloc(
28650Sstevel@tonic-gate 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
28660Sstevel@tonic-gate 	    DL_INFO_REQ);
28670Sstevel@tonic-gate 	if (info_mp == NULL)
28680Sstevel@tonic-gate 		goto bad;
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
28710Sstevel@tonic-gate 	    DL_BIND_REQ);
28720Sstevel@tonic-gate 	if (bind_mp == NULL)
28730Sstevel@tonic-gate 		goto bad;
28740Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
28750Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
28780Sstevel@tonic-gate 	if (unbind_mp == NULL)
28790Sstevel@tonic-gate 		goto bad;
28800Sstevel@tonic-gate 
28814360Smeem 	/* If we need to attach, pre-alloc and initialize the mblk */
28820Sstevel@tonic-gate 	if (ill->ill_needs_attach) {
28830Sstevel@tonic-gate 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
28840Sstevel@tonic-gate 		    DL_ATTACH_REQ);
28850Sstevel@tonic-gate 		if (attach_mp == NULL)
28860Sstevel@tonic-gate 			goto bad;
28870Sstevel@tonic-gate 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
28880Sstevel@tonic-gate 	}
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	/*
28910Sstevel@tonic-gate 	 * Here we are going to delay the ioctl ack until after
28920Sstevel@tonic-gate 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
28930Sstevel@tonic-gate 	 * original ioctl message before sending the requests
28940Sstevel@tonic-gate 	 */
28950Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
28960Sstevel@tonic-gate 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
28970Sstevel@tonic-gate 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
28980Sstevel@tonic-gate 	/*
28990Sstevel@tonic-gate 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
29000Sstevel@tonic-gate 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
29010Sstevel@tonic-gate 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
29020Sstevel@tonic-gate 	 */
29030Sstevel@tonic-gate 	ill->ill_phys_addr_pend = 0;
29040Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	if (attach_mp != NULL) {
29070Sstevel@tonic-gate 		ip1dbg(("ill_dl_phys: attach\n"));
29080Sstevel@tonic-gate 		ill_dlpi_send(ill, attach_mp);
29090Sstevel@tonic-gate 	}
29100Sstevel@tonic-gate 	ill_dlpi_send(ill, bind_mp);
29110Sstevel@tonic-gate 	ill_dlpi_send(ill, info_mp);
29120Sstevel@tonic-gate 	if (ill->ill_isv6) {
29130Sstevel@tonic-gate 		ill_dlpi_send(ill, v6token_mp);
29140Sstevel@tonic-gate 		ill_dlpi_send(ill, v6lla_mp);
29150Sstevel@tonic-gate 	}
29160Sstevel@tonic-gate 	ill_dlpi_send(ill, phys_mp);
29170Sstevel@tonic-gate 	ill_dlpi_send(ill, notify_mp);
29180Sstevel@tonic-gate 	ill_dlpi_send(ill, unbind_mp);
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	/*
29210Sstevel@tonic-gate 	 * This operation will complete in ip_rput_dlpi_writer with either
29220Sstevel@tonic-gate 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
29230Sstevel@tonic-gate 	 */
29240Sstevel@tonic-gate 	return (EINPROGRESS);
29250Sstevel@tonic-gate bad:
29264360Smeem 	freemsg(v6token_mp);
29274360Smeem 	freemsg(v6lla_mp);
29284360Smeem 	freemsg(phys_mp);
29294360Smeem 	freemsg(info_mp);
29304360Smeem 	freemsg(attach_mp);
29314360Smeem 	freemsg(bind_mp);
29324360Smeem 	freemsg(unbind_mp);
29334360Smeem 	freemsg(notify_mp);
29340Sstevel@tonic-gate 	return (ENOMEM);
29350Sstevel@tonic-gate }
29360Sstevel@tonic-gate 
29370Sstevel@tonic-gate uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate /*
29400Sstevel@tonic-gate  * DLPI is up.
29410Sstevel@tonic-gate  * Create all the IREs associated with an interface bring up multicast.
29420Sstevel@tonic-gate  * Set the interface flag and finish other initialization
29430Sstevel@tonic-gate  * that potentially had to be differed to after DL_BIND_ACK.
29440Sstevel@tonic-gate  */
29450Sstevel@tonic-gate int
29460Sstevel@tonic-gate ipif_up_done_v6(ipif_t *ipif)
29470Sstevel@tonic-gate {
29480Sstevel@tonic-gate 	ire_t	*ire_array[20];
29490Sstevel@tonic-gate 	ire_t	**irep = ire_array;
29500Sstevel@tonic-gate 	ire_t	**irep1;
29510Sstevel@tonic-gate 	ill_t	*ill = ipif->ipif_ill;
29520Sstevel@tonic-gate 	queue_t	*stq;
29530Sstevel@tonic-gate 	in6_addr_t	v6addr;
29540Sstevel@tonic-gate 	in6_addr_t	route_mask;
29550Sstevel@tonic-gate 	ipif_t	 *src_ipif = NULL;
29560Sstevel@tonic-gate 	ipif_t   *tmp_ipif;
29570Sstevel@tonic-gate 	boolean_t	flush_ire_cache = B_TRUE;
29580Sstevel@tonic-gate 	int	err;
29590Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
29600Sstevel@tonic-gate 	ire_t	**ipif_saved_irep = NULL;
29610Sstevel@tonic-gate 	int ipif_saved_ire_cnt;
29620Sstevel@tonic-gate 	int cnt;
29630Sstevel@tonic-gate 	boolean_t src_ipif_held = B_FALSE;
29640Sstevel@tonic-gate 	boolean_t loopback = B_FALSE;
29650Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
29663448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
29694459Skcpoon 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	/* Check if this is a loopback interface */
29720Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_wq == NULL)
29730Sstevel@tonic-gate 		loopback = B_TRUE;
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
29760Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	/*
29790Sstevel@tonic-gate 	 * If all other interfaces for this ill are down or DEPRECATED,
29800Sstevel@tonic-gate 	 * or otherwise unsuitable for source address selection, remove
29810Sstevel@tonic-gate 	 * any IRE_CACHE entries for this ill to make sure source
29820Sstevel@tonic-gate 	 * address selection gets to take this new ipif into account.
29830Sstevel@tonic-gate 	 * No need to hold ill_lock while traversing the ipif list since
29840Sstevel@tonic-gate 	 * we are writer
29850Sstevel@tonic-gate 	 */
29860Sstevel@tonic-gate 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
29874459Skcpoon 	    tmp_ipif = tmp_ipif->ipif_next) {
29880Sstevel@tonic-gate 		if (((tmp_ipif->ipif_flags &
29890Sstevel@tonic-gate 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
29900Sstevel@tonic-gate 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
29910Sstevel@tonic-gate 		    (tmp_ipif == ipif))
29920Sstevel@tonic-gate 			continue;
29930Sstevel@tonic-gate 		/* first useable pre-existing interface */
29940Sstevel@tonic-gate 		flush_ire_cache = B_FALSE;
29950Sstevel@tonic-gate 		break;
29960Sstevel@tonic-gate 	}
29970Sstevel@tonic-gate 	if (flush_ire_cache)
29988485SPeter.Memishian@Sun.COM 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
29998485SPeter.Memishian@Sun.COM 		    IRE_CACHE, ill_ipif_cache_delete, ill, ill);
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	/*
30020Sstevel@tonic-gate 	 * Figure out which way the send-to queue should go.  Only
30030Sstevel@tonic-gate 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here.
30040Sstevel@tonic-gate 	 */
30050Sstevel@tonic-gate 	switch (ill->ill_net_type) {
30060Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
30070Sstevel@tonic-gate 		stq = ill->ill_rq;
30080Sstevel@tonic-gate 		break;
30090Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
30100Sstevel@tonic-gate 	case IRE_LOOPBACK:
30110Sstevel@tonic-gate 		stq = ill->ill_wq;
30120Sstevel@tonic-gate 		break;
30130Sstevel@tonic-gate 	default:
30140Sstevel@tonic-gate 		return (EINVAL);
30150Sstevel@tonic-gate 	}
30160Sstevel@tonic-gate 
30174459Skcpoon 	if (IS_LOOPBACK(ill)) {
30180Sstevel@tonic-gate 		/*
30190Sstevel@tonic-gate 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
30200Sstevel@tonic-gate 		 * ipif_lookup_on_name(), but in the case of zones we can have
30210Sstevel@tonic-gate 		 * several loopback addresses on lo0. So all the interfaces with
30220Sstevel@tonic-gate 		 * loopback addresses need to be marked IRE_LOOPBACK.
30230Sstevel@tonic-gate 		 */
30240Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
30250Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOOPBACK;
30260Sstevel@tonic-gate 		else
30270Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOCAL;
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate 
30308485SPeter.Memishian@Sun.COM 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
30318485SPeter.Memishian@Sun.COM 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
30328485SPeter.Memishian@Sun.COM 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
30330Sstevel@tonic-gate 		/*
30340Sstevel@tonic-gate 		 * Can't use our source address. Select a different
30350Sstevel@tonic-gate 		 * source address for the IRE_INTERFACE and IRE_LOCAL
30360Sstevel@tonic-gate 		 */
30373448Sdh155122 		if (ip6_asp_can_lookup(ipst)) {
30380Sstevel@tonic-gate 			ip6_asp_table_held = B_TRUE;
30390Sstevel@tonic-gate 			src_ipif = ipif_select_source_v6(ipif->ipif_ill,
30408485SPeter.Memishian@Sun.COM 			    &ipif->ipif_v6subnet, B_FALSE,
30410Sstevel@tonic-gate 			    IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
30420Sstevel@tonic-gate 		}
30430Sstevel@tonic-gate 		if (src_ipif == NULL)
30440Sstevel@tonic-gate 			src_ipif = ipif;	/* Last resort */
30450Sstevel@tonic-gate 		else
30460Sstevel@tonic-gate 			src_ipif_held = B_TRUE;
30470Sstevel@tonic-gate 	} else {
30480Sstevel@tonic-gate 		src_ipif = ipif;
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
30520Sstevel@tonic-gate 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
30531676Sjpk 
30541676Sjpk 		/*
30551676Sjpk 		 * If we're on a labeled system then make sure that zone-
30561676Sjpk 		 * private addresses have proper remote host database entries.
30571676Sjpk 		 */
30581676Sjpk 		if (is_system_labeled() &&
30591676Sjpk 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
30601676Sjpk 			if (ip6opt_ls == 0) {
30611676Sjpk 				cmn_err(CE_WARN, "IPv6 not enabled "
30621676Sjpk 				    "via /etc/system");
30631676Sjpk 				return (EINVAL);
30641676Sjpk 			}
30651676Sjpk 			if (!tsol_check_interface_address(ipif))
30661676Sjpk 				return (EINVAL);
30671676Sjpk 		}
30681676Sjpk 
30690Sstevel@tonic-gate 		/* Register the source address for __sin6_src_id */
30700Sstevel@tonic-gate 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
30713448Sdh155122 		    ipif->ipif_zoneid, ipst);
30720Sstevel@tonic-gate 		if (err != 0) {
30730Sstevel@tonic-gate 			ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err));
30740Sstevel@tonic-gate 			if (src_ipif_held)
30750Sstevel@tonic-gate 				ipif_refrele(src_ipif);
30760Sstevel@tonic-gate 			if (ip6_asp_table_held)
30773448Sdh155122 				ip6_asp_table_refrele(ipst);
30780Sstevel@tonic-gate 			return (err);
30790Sstevel@tonic-gate 		}
30800Sstevel@tonic-gate 		/*
30810Sstevel@tonic-gate 		 * If the interface address is set, create the LOCAL
30820Sstevel@tonic-gate 		 * or LOOPBACK IRE.
30830Sstevel@tonic-gate 		 */
30840Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n",
30850Sstevel@tonic-gate 		    ipif->ipif_ire_type,
30860Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
30870Sstevel@tonic-gate 		    buf, sizeof (buf))));
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 		*irep++ = ire_create_v6(
30900Sstevel@tonic-gate 		    &ipif->ipif_v6lcl_addr,		/* dest address */
30910Sstevel@tonic-gate 		    &ipv6_all_ones,			/* mask */
30920Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,		/* source address */
30930Sstevel@tonic-gate 		    NULL,				/* no gateway */
30940Sstevel@tonic-gate 		    &ip_loopback_mtu_v6plus,		/* max frag size */
30950Sstevel@tonic-gate 		    NULL,
30960Sstevel@tonic-gate 		    ipif->ipif_rq,			/* recv-from queue */
30970Sstevel@tonic-gate 		    NULL,				/* no send-to queue */
30980Sstevel@tonic-gate 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
30990Sstevel@tonic-gate 		    ipif,				/* interface */
31000Sstevel@tonic-gate 		    NULL,
31010Sstevel@tonic-gate 		    0,
31020Sstevel@tonic-gate 		    0,
31030Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
31041676Sjpk 		    &ire_uinfo_null,
31051676Sjpk 		    NULL,
31063448Sdh155122 		    NULL,
31073448Sdh155122 		    ipst);
31080Sstevel@tonic-gate 	}
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	/*
31110Sstevel@tonic-gate 	 * Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate.
31120Sstevel@tonic-gate 	 * Note that atun interfaces have an all-zero ipif_v6subnet.
31130Sstevel@tonic-gate 	 * Thus we allow a zero subnet as long as the mask is non-zero.
31140Sstevel@tonic-gate 	 */
31150Sstevel@tonic-gate 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
31160Sstevel@tonic-gate 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
31170Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
31180Sstevel@tonic-gate 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
31190Sstevel@tonic-gate 		v6addr = ipif->ipif_v6subnet;
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
31220Sstevel@tonic-gate 			route_mask = ipv6_all_ones;
31230Sstevel@tonic-gate 		} else {
31240Sstevel@tonic-gate 			route_mask = ipif->ipif_v6net_mask;
31250Sstevel@tonic-gate 		}
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n",
31280Sstevel@tonic-gate 		    ill->ill_net_type,
31290Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 		*irep++ = ire_create_v6(
31320Sstevel@tonic-gate 		    &v6addr,			/* dest pref */
31330Sstevel@tonic-gate 		    &route_mask,		/* mask */
31340Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,	/* src addr */
31350Sstevel@tonic-gate 		    NULL,			/* no gateway */
31360Sstevel@tonic-gate 		    &ipif->ipif_mtu,		/* max frag */
31374714Ssowmini 		    NULL,			/* no src nce */
31380Sstevel@tonic-gate 		    NULL,			/* no recv from queue */
31390Sstevel@tonic-gate 		    stq,			/* send-to queue */
31400Sstevel@tonic-gate 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
31410Sstevel@tonic-gate 		    ipif,
31420Sstevel@tonic-gate 		    NULL,
31430Sstevel@tonic-gate 		    0,
31440Sstevel@tonic-gate 		    0,
31450Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
31461676Sjpk 		    &ire_uinfo_null,
31471676Sjpk 		    NULL,
31483448Sdh155122 		    NULL,
31493448Sdh155122 		    ipst);
31500Sstevel@tonic-gate 	}
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	/*
31530Sstevel@tonic-gate 	 * Setup 2002::/16 route, if this interface is a 6to4 tunnel
31540Sstevel@tonic-gate 	 */
31550Sstevel@tonic-gate 	if (IN6_IS_ADDR_6TO4(&ipif->ipif_v6lcl_addr) &&
31560Sstevel@tonic-gate 	    (ill->ill_is_6to4tun)) {
31570Sstevel@tonic-gate 		/*
31580Sstevel@tonic-gate 		 * Destination address is 2002::/16
31590Sstevel@tonic-gate 		 */
31600Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
31610Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x20020000U, 0, 0, 0 };
31620Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0xffff0000U, 0, 0, 0 };
31630Sstevel@tonic-gate #else
31640Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x00000220U, 0, 0, 0 };
31650Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0x0000ffffU, 0, 0, 0 };
31660Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
31670Sstevel@tonic-gate 		char	buf2[INET6_ADDRSTRLEN];
31680Sstevel@tonic-gate 		ire_t *isdup;
31690Sstevel@tonic-gate 		in6_addr_t *first_addr = &ill->ill_ipif->ipif_v6lcl_addr;
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 		/*
31720Sstevel@tonic-gate 		 * check to see if this route has already been added for
31730Sstevel@tonic-gate 		 * this tunnel interface.
31740Sstevel@tonic-gate 		 */
31750Sstevel@tonic-gate 		isdup = ire_ftable_lookup_v6(first_addr, &prefix_mask, 0,
31761676Sjpk 		    IRE_IF_NORESOLVER, ill->ill_ipif, NULL, ALL_ZONES, 0, NULL,
31773448Sdh155122 		    (MATCH_IRE_SRC | MATCH_IRE_MASK), ipst);
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 		if (isdup == NULL) {
31800Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s",
31810Sstevel@tonic-gate 			    IRE_IF_NORESOLVER, inet_ntop(AF_INET6, &v6addr,
31824459Skcpoon 			    buf2, sizeof (buf2))));
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate 			*irep++ = ire_create_v6(
31850Sstevel@tonic-gate 			    &prefix_addr,		/* 2002:: */
31860Sstevel@tonic-gate 			    &prefix_mask,		/* ffff:: */
31870Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, 	/* src addr */
31880Sstevel@tonic-gate 			    NULL, 			/* gateway */
31890Sstevel@tonic-gate 			    &ipif->ipif_mtu, 		/* max_frag */
31904714Ssowmini 			    NULL, 			/* no src nce */
31910Sstevel@tonic-gate 			    NULL, 			/* no rfq */
31920Sstevel@tonic-gate 			    ill->ill_wq, 		/* stq */
31930Sstevel@tonic-gate 			    IRE_IF_NORESOLVER,		/* type */
31940Sstevel@tonic-gate 			    ipif,			/* interface */
31950Sstevel@tonic-gate 			    NULL,			/* v6cmask */
31960Sstevel@tonic-gate 			    0,
31970Sstevel@tonic-gate 			    0,
31980Sstevel@tonic-gate 			    RTF_UP,
31991676Sjpk 			    &ire_uinfo_null,
32001676Sjpk 			    NULL,
32013448Sdh155122 			    NULL,
32023448Sdh155122 			    ipst);
32030Sstevel@tonic-gate 		} else {
32040Sstevel@tonic-gate 			ire_refrele(isdup);
32050Sstevel@tonic-gate 		}
32060Sstevel@tonic-gate 	}
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	/* If an earlier ire_create failed, get out now */
32090Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
32100Sstevel@tonic-gate 		irep1--;
32110Sstevel@tonic-gate 		if (*irep1 == NULL) {
32120Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: NULL ire found in"
32130Sstevel@tonic-gate 			    " ire_array\n"));
32140Sstevel@tonic-gate 			err = ENOMEM;
32150Sstevel@tonic-gate 			goto bad;
32160Sstevel@tonic-gate 		}
32170Sstevel@tonic-gate 	}
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	/*
32228485SPeter.Memishian@Sun.COM 	 * Need to atomically check for IP address availability under
32238485SPeter.Memishian@Sun.COM 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
32248485SPeter.Memishian@Sun.COM 	 * ills or new ipifs can be added while we are checking availability.
32250Sstevel@tonic-gate 	 */
32263448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
32273448Sdh155122 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
32280Sstevel@tonic-gate 	ill->ill_ipif_up_count++;
32290Sstevel@tonic-gate 	ipif->ipif_flags |= IPIF_UP;
32300Sstevel@tonic-gate 	err = ip_addr_availability_check(ipif);
32313448Sdh155122 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
32323448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	if (err != 0) {
32350Sstevel@tonic-gate 		/*
32360Sstevel@tonic-gate 		 * Our address may already be up on the same ill. In this case,
32370Sstevel@tonic-gate 		 * the external resolver entry for our ipif replaced the one for
32380Sstevel@tonic-gate 		 * the other ipif. So we don't want to delete it (otherwise the
32390Sstevel@tonic-gate 		 * other ipif would be unable to send packets).
32400Sstevel@tonic-gate 		 * ip_addr_availability_check() identifies this case for us and
32410Sstevel@tonic-gate 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
32420Sstevel@tonic-gate 		 * which is the expected error code.
32439287SSowmini.Varadhan@Sun.COM 		 *
32449287SSowmini.Varadhan@Sun.COM 		 * Note that, for the non-XRESOLV case, ipif_ndp_down() will
32459571SSowmini.Varadhan@Sun.COM 		 * only delete the nce in the case when the nce_ipif_cnt drops
32469571SSowmini.Varadhan@Sun.COM 		 * to 0.
32470Sstevel@tonic-gate 		 */
32480Sstevel@tonic-gate 		if (err == EADDRINUSE) {
32490Sstevel@tonic-gate 			if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) {
32500Sstevel@tonic-gate 				freemsg(ipif->ipif_arp_del_mp);
32510Sstevel@tonic-gate 				ipif->ipif_arp_del_mp = NULL;
32520Sstevel@tonic-gate 			}
32530Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
32540Sstevel@tonic-gate 		}
32550Sstevel@tonic-gate 		ill->ill_ipif_up_count--;
32560Sstevel@tonic-gate 		ipif->ipif_flags &= ~IPIF_UP;
32570Sstevel@tonic-gate 		goto bad;
32580Sstevel@tonic-gate 	}
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate 	/*
32618485SPeter.Memishian@Sun.COM 	 * Add in all newly created IREs.
32620Sstevel@tonic-gate 	 *
32630Sstevel@tonic-gate 	 * NOTE : We refrele the ire though we may branch to "bad"
32640Sstevel@tonic-gate 	 *	  later on where we do ire_delete. This is okay
32650Sstevel@tonic-gate 	 *	  because nobody can delete it as we are running
32660Sstevel@tonic-gate 	 *	  exclusively.
32670Sstevel@tonic-gate 	 */
32680Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
32690Sstevel@tonic-gate 		irep1--;
32700Sstevel@tonic-gate 		/* Shouldn't be adding any bcast ire's */
32710Sstevel@tonic-gate 		ASSERT((*irep1)->ire_type != IRE_BROADCAST);
32720Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
32730Sstevel@tonic-gate 		/*
32740Sstevel@tonic-gate 		 * refheld by ire_add. refele towards the end of the func
32750Sstevel@tonic-gate 		 */
32762535Ssangeeta 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
32770Sstevel@tonic-gate 	}
32780Sstevel@tonic-gate 	if (ip6_asp_table_held) {
32793448Sdh155122 		ip6_asp_table_refrele(ipst);
32800Sstevel@tonic-gate 		ip6_asp_table_held = B_FALSE;
32810Sstevel@tonic-gate 	}
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
32840Sstevel@tonic-gate 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
32850Sstevel@tonic-gate 	ipif_saved_irep = ipif_recover_ire_v6(ipif);
32860Sstevel@tonic-gate 
32878023SPhil.Kirk@Sun.COM 	if (ill->ill_need_recover_multicast) {
32880Sstevel@tonic-gate 		/*
32890Sstevel@tonic-gate 		 * Need to recover all multicast memberships in the driver.
32900Sstevel@tonic-gate 		 * This had to be deferred until we had attached.
32910Sstevel@tonic-gate 		 */
32920Sstevel@tonic-gate 		ill_recover_multicast(ill);
32930Sstevel@tonic-gate 	}
32948485SPeter.Memishian@Sun.COM 
32958485SPeter.Memishian@Sun.COM 	if (ill->ill_ipif_up_count == 1) {
32968485SPeter.Memishian@Sun.COM 		/*
32978485SPeter.Memishian@Sun.COM 		 * Since the interface is now up, it may now be active.
32988485SPeter.Memishian@Sun.COM 		 */
32998485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ill))
33008485SPeter.Memishian@Sun.COM 			ipmp_ill_refresh_active(ill);
33018485SPeter.Memishian@Sun.COM 	}
33028485SPeter.Memishian@Sun.COM 
33030Sstevel@tonic-gate 	/* Join the allhosts multicast address and the solicited node MC */
33040Sstevel@tonic-gate 	ipif_multicast_up(ipif);
33050Sstevel@tonic-gate 
33068485SPeter.Memishian@Sun.COM 	/*
33078485SPeter.Memishian@Sun.COM 	 * See if anybody else would benefit from our new ipif.
33088485SPeter.Memishian@Sun.COM 	 */
33098485SPeter.Memishian@Sun.COM 	if (!loopback &&
33108485SPeter.Memishian@Sun.COM 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
33110Sstevel@tonic-gate 		ill_update_source_selection(ill);
33120Sstevel@tonic-gate 	}
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
33150Sstevel@tonic-gate 		irep1--;
33160Sstevel@tonic-gate 		if (*irep1 != NULL) {
33170Sstevel@tonic-gate 			/* was held in ire_add */
33180Sstevel@tonic-gate 			ire_refrele(*irep1);
33190Sstevel@tonic-gate 		}
33200Sstevel@tonic-gate 	}
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 	cnt = ipif_saved_ire_cnt;
33230Sstevel@tonic-gate 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
33240Sstevel@tonic-gate 		if (*irep1 != NULL) {
33250Sstevel@tonic-gate 			/* was held in ire_add */
33260Sstevel@tonic-gate 			ire_refrele(*irep1);
33270Sstevel@tonic-gate 		}
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate 
33308023SPhil.Kirk@Sun.COM 	if (ipif->ipif_addr_ready)
33318023SPhil.Kirk@Sun.COM 		ipif_up_notify(ipif);
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
33340Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
33350Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
33360Sstevel@tonic-gate 	}
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 	if (src_ipif_held)
33390Sstevel@tonic-gate 		ipif_refrele(src_ipif);
33408023SPhil.Kirk@Sun.COM 
33410Sstevel@tonic-gate 	return (0);
33420Sstevel@tonic-gate 
33430Sstevel@tonic-gate bad:
33440Sstevel@tonic-gate 	if (ip6_asp_table_held)
33453448Sdh155122 		ip6_asp_table_refrele(ipst);
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 	while (irep > ire_array) {
33480Sstevel@tonic-gate 		irep--;
33498485SPeter.Memishian@Sun.COM 		if (*irep != NULL)
33500Sstevel@tonic-gate 			ire_delete(*irep);
33510Sstevel@tonic-gate 	}
33523448Sdh155122 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
33550Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
33560Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
33570Sstevel@tonic-gate 	}
33580Sstevel@tonic-gate 	if (src_ipif_held)
33590Sstevel@tonic-gate 		ipif_refrele(src_ipif);
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	ipif_ndp_down(ipif);
33628485SPeter.Memishian@Sun.COM 	ipif_resolver_down(ipif);
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 	return (err);
33650Sstevel@tonic-gate }
33660Sstevel@tonic-gate 
33670Sstevel@tonic-gate /*
33680Sstevel@tonic-gate  * Delete an ND entry and the corresponding IRE_CACHE entry if it exists.
33690Sstevel@tonic-gate  */
33700Sstevel@tonic-gate /* ARGSUSED */
33710Sstevel@tonic-gate int
33720Sstevel@tonic-gate ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
33730Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
33740Sstevel@tonic-gate {
33750Sstevel@tonic-gate 	sin6_t		*sin6;
33760Sstevel@tonic-gate 	nce_t		*nce;
33770Sstevel@tonic-gate 	struct lifreq	*lifr;
33780Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
33798485SPeter.Memishian@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
33808485SPeter.Memishian@Sun.COM 	ire_t		*ire;
33818485SPeter.Memishian@Sun.COM 
33828485SPeter.Memishian@Sun.COM 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
33830Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
33840Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
33850Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
33860Sstevel@tonic-gate 		return (EINVAL);
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
33890Sstevel@tonic-gate 		return (EINVAL);
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
33920Sstevel@tonic-gate 		return (EAFNOSUPPORT);
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	sin6 = (sin6_t *)&lnr->lnr_addr;
33958485SPeter.Memishian@Sun.COM 
33968485SPeter.Memishian@Sun.COM 	/*
33978485SPeter.Memishian@Sun.COM 	 * Since ND mappings must be consistent across an IPMP group, prohibit
33988485SPeter.Memishian@Sun.COM 	 * deleting ND mappings on underlying interfaces.  Also, since ND
33998485SPeter.Memishian@Sun.COM 	 * mappings for IPMP data addresses are owned by IP itself, prohibit
34008485SPeter.Memishian@Sun.COM 	 * deleting them.
34018485SPeter.Memishian@Sun.COM 	 */
34028485SPeter.Memishian@Sun.COM 	if (IS_UNDER_IPMP(ill))
34038485SPeter.Memishian@Sun.COM 		return (EPERM);
34048485SPeter.Memishian@Sun.COM 
34058485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill)) {
34068485SPeter.Memishian@Sun.COM 		ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, IRE_LOCAL,
34078485SPeter.Memishian@Sun.COM 		    ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL,
34088485SPeter.Memishian@Sun.COM 		    ill->ill_ipst);
34098485SPeter.Memishian@Sun.COM 		if (ire != NULL) {
34108485SPeter.Memishian@Sun.COM 			ire_refrele(ire);
34118485SPeter.Memishian@Sun.COM 			return (EPERM);
34128485SPeter.Memishian@Sun.COM 		}
34138485SPeter.Memishian@Sun.COM 	}
34148485SPeter.Memishian@Sun.COM 
34158485SPeter.Memishian@Sun.COM 	/* See comment in ndp_query() regarding IS_IPMP(ill) usage */
34168485SPeter.Memishian@Sun.COM 	nce = ndp_lookup_v6(ill, IS_IPMP(ill), &sin6->sin6_addr, B_FALSE);
34170Sstevel@tonic-gate 	if (nce == NULL)
34180Sstevel@tonic-gate 		return (ESRCH);
34190Sstevel@tonic-gate 	ndp_delete(nce);
34200Sstevel@tonic-gate 	NCE_REFRELE(nce);
34210Sstevel@tonic-gate 	return (0);
34220Sstevel@tonic-gate }
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate /*
34250Sstevel@tonic-gate  * Return nbr cache info.
34260Sstevel@tonic-gate  */
34270Sstevel@tonic-gate /* ARGSUSED */
34280Sstevel@tonic-gate int
34290Sstevel@tonic-gate ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
34300Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
34310Sstevel@tonic-gate {
34320Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
34330Sstevel@tonic-gate 	struct lifreq	*lifr;
34340Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
34370Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
34380Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
34390Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
34400Sstevel@tonic-gate 		return (EINVAL);
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
34430Sstevel@tonic-gate 		return (EINVAL);
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
34460Sstevel@tonic-gate 		return (EAFNOSUPPORT);
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
34490Sstevel@tonic-gate 		return (EINVAL);
34500Sstevel@tonic-gate 
34510Sstevel@tonic-gate 	return (ndp_query(ill, lnr));
34520Sstevel@tonic-gate }
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate /*
34550Sstevel@tonic-gate  * Perform an update of the nd entry for the specified address.
34560Sstevel@tonic-gate  */
34570Sstevel@tonic-gate /* ARGSUSED */
34580Sstevel@tonic-gate int
34590Sstevel@tonic-gate ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
34600Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
34610Sstevel@tonic-gate {
34628485SPeter.Memishian@Sun.COM 	sin6_t		*sin6;
34630Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
34640Sstevel@tonic-gate 	struct	lifreq	*lifr;
34650Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
34668485SPeter.Memishian@Sun.COM 	ire_t		*ire;
34673448Sdh155122 
34680Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
34690Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
34700Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
34710Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
34720Sstevel@tonic-gate 		return (EINVAL);
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
34750Sstevel@tonic-gate 		return (EINVAL);
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
34780Sstevel@tonic-gate 		return (EAFNOSUPPORT);
34790Sstevel@tonic-gate 
34808485SPeter.Memishian@Sun.COM 	sin6 = (sin6_t *)&lnr->lnr_addr;
34818485SPeter.Memishian@Sun.COM 
34828485SPeter.Memishian@Sun.COM 	/*
34838485SPeter.Memishian@Sun.COM 	 * Since ND mappings must be consistent across an IPMP group, prohibit
34848485SPeter.Memishian@Sun.COM 	 * updating ND mappings on underlying interfaces.  Also, since ND
34858485SPeter.Memishian@Sun.COM 	 * mappings for IPMP data addresses are owned by IP itself, prohibit
34868485SPeter.Memishian@Sun.COM 	 * updating them.
34878485SPeter.Memishian@Sun.COM 	 */
34888485SPeter.Memishian@Sun.COM 	if (IS_UNDER_IPMP(ill))
34898485SPeter.Memishian@Sun.COM 		return (EPERM);
34908485SPeter.Memishian@Sun.COM 
34918485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill)) {
34928485SPeter.Memishian@Sun.COM 		ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, IRE_LOCAL,
34938485SPeter.Memishian@Sun.COM 		    ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL,
34948485SPeter.Memishian@Sun.COM 		    ill->ill_ipst);
34958485SPeter.Memishian@Sun.COM 		if (ire != NULL) {
34968485SPeter.Memishian@Sun.COM 			ire_refrele(ire);
34978485SPeter.Memishian@Sun.COM 			return (EPERM);
34988485SPeter.Memishian@Sun.COM 		}
34998485SPeter.Memishian@Sun.COM 	}
35008485SPeter.Memishian@Sun.COM 
35010Sstevel@tonic-gate 	return (ndp_sioc_update(ill, lnr));
35020Sstevel@tonic-gate }
3503