xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_if.c (revision 10773:434ca1ce22f9)
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/ipclassifier.h>
690Sstevel@tonic-gate #include <inet/sctp_ip.h>
700Sstevel@tonic-gate 
711676Sjpk #include <sys/tsol/tndb.h>
721676Sjpk #include <sys/tsol/tnet.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static in6_addr_t	ipv6_ll_template =
750Sstevel@tonic-gate 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static ipif_t *
780Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
793448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
824459Skcpoon  * These two functions, ipif_lookup_group_v6() and ill_lookup_group_v6(),
834459Skcpoon  * are called when an application does not specify an interface to be
844459Skcpoon  * used for multicast traffic.  It calls ire_lookup_multi_v6() to look
854459Skcpoon  * for an interface route for the specified multicast group.  Doing
864459Skcpoon  * this allows the administrator to add prefix routes for multicast to
874459Skcpoon  * indicate which interface to be used for multicast traffic in the above
884459Skcpoon  * scenario.  The route could be for all multicast (ff00::/8), for a single
894459Skcpoon  * multicast group (a /128 route) or anything in between.  If there is no
904459Skcpoon  * such multicast route, we just find any multicast capable interface and
914459Skcpoon  * return it.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate ipif_t *
943448Sdh155122 ipif_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	ire_t	*ire;
970Sstevel@tonic-gate 	ipif_t	*ipif;
980Sstevel@tonic-gate 
993448Sdh155122 	ire = ire_lookup_multi_v6(group, zoneid, ipst);
1004459Skcpoon 	if (ire != NULL) {
1014459Skcpoon 		ipif = ire->ire_ipif;
1024459Skcpoon 		ipif_refhold(ipif);
1034459Skcpoon 		ire_refrele(ire);
1044459Skcpoon 		return (ipif);
1054459Skcpoon 	}
1064459Skcpoon 
1074459Skcpoon 	return (ipif_lookup_multicast(ipst, zoneid, B_TRUE));
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate ill_t *
1113448Sdh155122 ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	ire_t	*ire;
1140Sstevel@tonic-gate 	ill_t	*ill;
1154459Skcpoon 	ipif_t	*ipif;
1160Sstevel@tonic-gate 
1173448Sdh155122 	ire = ire_lookup_multi_v6(group, zoneid, ipst);
1184459Skcpoon 	if (ire != NULL) {
1194459Skcpoon 		ill = ire->ire_ipif->ipif_ill;
1204459Skcpoon 		ill_refhold(ill);
1214459Skcpoon 		ire_refrele(ire);
1224459Skcpoon 		return (ill);
1234459Skcpoon 	}
1244459Skcpoon 
1254459Skcpoon 	ipif = ipif_lookup_multicast(ipst, zoneid, B_TRUE);
1264459Skcpoon 	if (ipif == NULL)
1270Sstevel@tonic-gate 		return (NULL);
1284459Skcpoon 
1294459Skcpoon 	ill = ipif->ipif_ill;
1300Sstevel@tonic-gate 	ill_refhold(ill);
1314459Skcpoon 	ipif_refrele(ipif);
1320Sstevel@tonic-gate 	return (ill);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * Look for an ipif with the specified interface address and destination.
1370Sstevel@tonic-gate  * The destination address is used only for matching point-to-point interfaces.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate static ipif_t *
1400Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
1413448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	ipif_t	*ipif;
1440Sstevel@tonic-gate 	ill_t	*ill;
1450Sstevel@tonic-gate 	ipsq_t	*ipsq;
1460Sstevel@tonic-gate 	ill_walk_context_t ctx;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (error != NULL)
1490Sstevel@tonic-gate 		*error = 0;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * First match all the point-to-point interfaces
1530Sstevel@tonic-gate 	 * before looking at non-point-to-point interfaces.
1540Sstevel@tonic-gate 	 * This is done to avoid returning non-point-to-point
1550Sstevel@tonic-gate 	 * ipif instead of unnumbered point-to-point ipif.
1560Sstevel@tonic-gate 	 */
1573448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1583448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
1590Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1600Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
1610Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
1622733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
1632733Snordmark 		    ipif = ipif->ipif_next) {
1640Sstevel@tonic-gate 			/* Allow the ipif to be down */
1650Sstevel@tonic-gate 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
1660Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1670Sstevel@tonic-gate 			    if_addr)) &&
1680Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
1690Sstevel@tonic-gate 			    dst))) {
1700Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
1710Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
1720Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1730Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1743448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1750Sstevel@tonic-gate 					return (ipif);
1760Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
1770Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
1780Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
1798485SPeter.Memishian@Sun.COM 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
1800Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1813448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1820Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
1834459Skcpoon 					    ill);
1848485SPeter.Memishian@Sun.COM 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
1850Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
1860Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
1875335Ssowmini 					if (error != NULL)
1885335Ssowmini 						*error = EINPROGRESS;
1890Sstevel@tonic-gate 					return (NULL);
1900Sstevel@tonic-gate 				}
1910Sstevel@tonic-gate 			}
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
1940Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
1950Sstevel@tonic-gate 	}
1963448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
1970Sstevel@tonic-gate 	/* lookup the ipif based on interface address */
1980Sstevel@tonic-gate 	ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, q, mp, func,
1993448Sdh155122 	    error, ipst);
2000Sstevel@tonic-gate 	ASSERT(ipif == NULL || ipif->ipif_isv6);
2010Sstevel@tonic-gate 	return (ipif);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /*
2058485SPeter.Memishian@Sun.COM  * Common function for ipif_lookup_addr_v6() and ipif_lookup_addr_exact_v6().
2060Sstevel@tonic-gate  */
2078485SPeter.Memishian@Sun.COM static ipif_t *
2088485SPeter.Memishian@Sun.COM ipif_lookup_addr_common_v6(const in6_addr_t *addr, ill_t *match_ill,
2098485SPeter.Memishian@Sun.COM     boolean_t match_illgrp, zoneid_t zoneid, queue_t *q, mblk_t *mp,
2108485SPeter.Memishian@Sun.COM     ipsq_func_t func, int *error, ip_stack_t *ipst)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	ipif_t	*ipif;
2130Sstevel@tonic-gate 	ill_t	*ill;
2140Sstevel@tonic-gate 	boolean_t  ptp = B_FALSE;
2150Sstevel@tonic-gate 	ipsq_t	*ipsq;
2160Sstevel@tonic-gate 	ill_walk_context_t ctx;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (error != NULL)
2190Sstevel@tonic-gate 		*error = 0;
2200Sstevel@tonic-gate 
2213448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Repeat twice, first based on local addresses and
2240Sstevel@tonic-gate 	 * next time for pointopoint.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate repeat:
2273448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
2280Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2298485SPeter.Memishian@Sun.COM 		if (match_ill != NULL && ill != match_ill &&
2308485SPeter.Memishian@Sun.COM 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
2310Sstevel@tonic-gate 			continue;
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
2340Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
2352733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
2362733Snordmark 		    ipif = ipif->ipif_next) {
2371676Sjpk 			if (zoneid != ALL_ZONES &&
2381676Sjpk 			    ipif->ipif_zoneid != zoneid &&
2391676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
2400Sstevel@tonic-gate 				continue;
2410Sstevel@tonic-gate 			/* Allow the ipif to be down */
2420Sstevel@tonic-gate 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
2430Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, addr) &&
2440Sstevel@tonic-gate 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
2450Sstevel@tonic-gate 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
2460Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
2470Sstevel@tonic-gate 			    addr))) {
2480Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
2490Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
2500Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2510Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2523448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2530Sstevel@tonic-gate 					return (ipif);
2540Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
2550Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
2560Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
2578485SPeter.Memishian@Sun.COM 					mutex_enter(&ipsq->ipsq_xop->ipx_lock);
2580Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2593448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2600Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
2614459Skcpoon 					    ill);
2628485SPeter.Memishian@Sun.COM 					mutex_exit(&ipsq->ipsq_xop->ipx_lock);
2630Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
2640Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
2655335Ssowmini 					if (error != NULL)
2665335Ssowmini 						*error = EINPROGRESS;
2670Sstevel@tonic-gate 					return (NULL);
2680Sstevel@tonic-gate 				}
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2720Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2752733Snordmark 	/* If we already did the ptp case, then we are done */
2760Sstevel@tonic-gate 	if (ptp) {
2773448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
2780Sstevel@tonic-gate 		if (error != NULL)
2790Sstevel@tonic-gate 			*error = ENXIO;
2800Sstevel@tonic-gate 		return (NULL);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	ptp = B_TRUE;
2830Sstevel@tonic-gate 	goto repeat;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2868348SEric.Yu@Sun.COM boolean_t
2878348SEric.Yu@Sun.COM ip_addr_exists_v6(const in6_addr_t *addr, zoneid_t zoneid,
2888348SEric.Yu@Sun.COM     ip_stack_t *ipst)
2898348SEric.Yu@Sun.COM {
2908348SEric.Yu@Sun.COM 	ipif_t	*ipif;
2918348SEric.Yu@Sun.COM 	ill_t	*ill;
2928348SEric.Yu@Sun.COM 	ill_walk_context_t ctx;
2938348SEric.Yu@Sun.COM 
2948348SEric.Yu@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2958348SEric.Yu@Sun.COM 
2968348SEric.Yu@Sun.COM 	ill = ILL_START_WALK_V6(&ctx, ipst);
2978348SEric.Yu@Sun.COM 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2988348SEric.Yu@Sun.COM 		mutex_enter(&ill->ill_lock);
2998348SEric.Yu@Sun.COM 		for (ipif = ill->ill_ipif; ipif != NULL;
3008348SEric.Yu@Sun.COM 		    ipif = ipif->ipif_next) {
3018348SEric.Yu@Sun.COM 			if (zoneid != ALL_ZONES &&
3028348SEric.Yu@Sun.COM 			    ipif->ipif_zoneid != zoneid &&
3038348SEric.Yu@Sun.COM 			    ipif->ipif_zoneid != ALL_ZONES)
3048348SEric.Yu@Sun.COM 				continue;
3058348SEric.Yu@Sun.COM 			/* Allow the ipif to be down */
3068348SEric.Yu@Sun.COM 			if (((IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
3078348SEric.Yu@Sun.COM 			    addr) &&
3088348SEric.Yu@Sun.COM 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
3098348SEric.Yu@Sun.COM 			    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
3108348SEric.Yu@Sun.COM 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
3118348SEric.Yu@Sun.COM 			    addr))) {
3128348SEric.Yu@Sun.COM 				mutex_exit(&ill->ill_lock);
3138348SEric.Yu@Sun.COM 				rw_exit(&ipst->ips_ill_g_lock);
3148348SEric.Yu@Sun.COM 				return (B_TRUE);
3158348SEric.Yu@Sun.COM 			}
3168348SEric.Yu@Sun.COM 		}
3178348SEric.Yu@Sun.COM 		mutex_exit(&ill->ill_lock);
3188348SEric.Yu@Sun.COM 	}
3198348SEric.Yu@Sun.COM 
3208348SEric.Yu@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
3218348SEric.Yu@Sun.COM 	return (B_FALSE);
3228348SEric.Yu@Sun.COM }
3238348SEric.Yu@Sun.COM 
3240Sstevel@tonic-gate /*
3258485SPeter.Memishian@Sun.COM  * Lookup an ipif with the specified address.  For point-to-point links we
3268485SPeter.Memishian@Sun.COM  * look for matches on either the destination address or the local address,
3278485SPeter.Memishian@Sun.COM  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
3288485SPeter.Memishian@Sun.COM  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
3298485SPeter.Memishian@Sun.COM  * (or illgrp if `match_ill' is in an IPMP group).
3308485SPeter.Memishian@Sun.COM  */
3318485SPeter.Memishian@Sun.COM ipif_t *
3328485SPeter.Memishian@Sun.COM ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
3338485SPeter.Memishian@Sun.COM     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
3348485SPeter.Memishian@Sun.COM {
3358485SPeter.Memishian@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill, B_TRUE, zoneid, q,
3368485SPeter.Memishian@Sun.COM 	    mp, func, error, ipst));
3378485SPeter.Memishian@Sun.COM }
3388485SPeter.Memishian@Sun.COM 
3398485SPeter.Memishian@Sun.COM /*
3408485SPeter.Memishian@Sun.COM  * Special abbreviated version of ipif_lookup_addr_v6() that doesn't match
3418485SPeter.Memishian@Sun.COM  * `match_ill' across the IPMP group.  This function is only needed in some
3428485SPeter.Memishian@Sun.COM  * corner-cases; almost everything should use ipif_lookup_addr_v6().
3438485SPeter.Memishian@Sun.COM  */
3448485SPeter.Memishian@Sun.COM ipif_t *
3458485SPeter.Memishian@Sun.COM ipif_lookup_addr_exact_v6(const in6_addr_t *addr, ill_t *match_ill,
3468485SPeter.Memishian@Sun.COM     ip_stack_t *ipst)
3478485SPeter.Memishian@Sun.COM {
3488485SPeter.Memishian@Sun.COM 	ASSERT(match_ill != NULL);
3498485SPeter.Memishian@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill, B_FALSE, ALL_ZONES,
3508485SPeter.Memishian@Sun.COM 	    NULL, NULL, NULL, NULL, ipst));
3518485SPeter.Memishian@Sun.COM }
3528485SPeter.Memishian@Sun.COM 
3538485SPeter.Memishian@Sun.COM /*
3542733Snordmark  * Look for an ipif with the specified address. For point-point links
3552733Snordmark  * we look for matches on either the destination address and the local
3562733Snordmark  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
3572733Snordmark  * is set.
3588485SPeter.Memishian@Sun.COM  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
3598485SPeter.Memishian@Sun.COM  * ill (or illgrp if `match_ill' is in an IPMP group).
3602733Snordmark  * Return the zoneid for the ipif. ALL_ZONES if none found.
3612733Snordmark  */
3622733Snordmark zoneid_t
3633448Sdh155122 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill,
3643448Sdh155122     ip_stack_t *ipst)
3652733Snordmark {
3662733Snordmark 	ipif_t	*ipif;
3672733Snordmark 	ill_t	*ill;
3682733Snordmark 	boolean_t  ptp = B_FALSE;
3692733Snordmark 	ill_walk_context_t ctx;
3702733Snordmark 	zoneid_t	zoneid;
3712733Snordmark 
3723448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3732733Snordmark 	/*
3742733Snordmark 	 * Repeat twice, first based on local addresses and
3752733Snordmark 	 * next time for pointopoint.
3762733Snordmark 	 */
3772733Snordmark repeat:
3783448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
3792733Snordmark 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
3808485SPeter.Memishian@Sun.COM 		if (match_ill != NULL && ill != match_ill &&
3818485SPeter.Memishian@Sun.COM 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
3822733Snordmark 			continue;
3832733Snordmark 		}
3842733Snordmark 		mutex_enter(&ill->ill_lock);
3852733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
3862733Snordmark 		    ipif = ipif->ipif_next) {
3872733Snordmark 			/* Allow the ipif to be down */
3882733Snordmark 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
3892733Snordmark 			    &ipif->ipif_v6lcl_addr, addr) &&
3902733Snordmark 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
3912733Snordmark 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
3922733Snordmark 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
3932733Snordmark 			    addr)) &&
3942733Snordmark 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
3952733Snordmark 				zoneid = ipif->ipif_zoneid;
3962733Snordmark 				mutex_exit(&ill->ill_lock);
3973448Sdh155122 				rw_exit(&ipst->ips_ill_g_lock);
3982733Snordmark 				/*
3992733Snordmark 				 * If ipif_zoneid was ALL_ZONES then we have
4002733Snordmark 				 * a trusted extensions shared IP address.
4012733Snordmark 				 * In that case GLOBAL_ZONEID works to send.
4022733Snordmark 				 */
4032733Snordmark 				if (zoneid == ALL_ZONES)
4042733Snordmark 					zoneid = GLOBAL_ZONEID;
4052733Snordmark 				return (zoneid);
4062733Snordmark 			}
4072733Snordmark 		}
4082733Snordmark 		mutex_exit(&ill->ill_lock);
4092733Snordmark 	}
4102733Snordmark 
4112733Snordmark 	/* If we already did the ptp case, then we are done */
4122733Snordmark 	if (ptp) {
4133448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
4142733Snordmark 		return (ALL_ZONES);
4152733Snordmark 	}
4162733Snordmark 	ptp = B_TRUE;
4172733Snordmark 	goto repeat;
4182733Snordmark }
4192733Snordmark 
4202733Snordmark /*
4210Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a local
4220Sstevel@tonic-gate  * interface address.  This is currently only called when an attempt is made
4230Sstevel@tonic-gate  * to set a local address.
4240Sstevel@tonic-gate  *
4250Sstevel@tonic-gate  * Does not allow a v4-mapped address, an address that equals the subnet
4260Sstevel@tonic-gate  * anycast address, ... a multicast address, ...
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate boolean_t
4290Sstevel@tonic-gate ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	in6_addr_t subnet;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
4340Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	/*
4370Sstevel@tonic-gate 	 * Don't allow all zeroes or host part, but allow
4380Sstevel@tonic-gate 	 * all ones netmask.
4390Sstevel@tonic-gate 	 */
4400Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
4410Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
4420Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
4430Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
4440Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
4450Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr))
4460Sstevel@tonic-gate 		return (B_FALSE);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	return (B_TRUE);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a
4530Sstevel@tonic-gate  * remote/subnet interface address.
4540Sstevel@tonic-gate  */
4550Sstevel@tonic-gate boolean_t
4560Sstevel@tonic-gate ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	in6_addr_t subnet;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
4610Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
4640Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
4650Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
4660Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
4670Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr) ||
4680Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
4690Sstevel@tonic-gate 		return (B_FALSE);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	return (B_TRUE);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
4760Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
4770Sstevel@tonic-gate  * (for link-local destinations and gateways).
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate /* ARGSUSED1 */
4800Sstevel@tonic-gate int
4810Sstevel@tonic-gate ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
4820Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
4831676Sjpk     ipif_t *ipif_arg, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func,
4843448Sdh155122     struct rtsa_s *sp, ip_stack_t *ipst)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	ire_t	*ire;
4870Sstevel@tonic-gate 	ire_t	*gw_ire = NULL;
4880Sstevel@tonic-gate 	ipif_t	*ipif;
4890Sstevel@tonic-gate 	boolean_t ipif_refheld = B_FALSE;
4900Sstevel@tonic-gate 	uint_t	type;
4910Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
4920Sstevel@tonic-gate 	int	error;
4931676Sjpk 	tsol_gc_t *gc = NULL;
4941676Sjpk 	tsol_gcgrp_t *gcgrp = NULL;
4951676Sjpk 	boolean_t gcgrp_xtraref = B_FALSE;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (ire_arg != NULL)
4980Sstevel@tonic-gate 		*ire_arg = NULL;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * Prevent routes with a zero gateway from being created (since
5020Sstevel@tonic-gate 	 * interfaces can currently be plumbed and brought up with no assigned
5030Sstevel@tonic-gate 	 * address).
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
5060Sstevel@tonic-gate 		return (ENETUNREACH);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
5100Sstevel@tonic-gate 	 * to all ones (regardless if one was supplied).
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	if (flags & RTF_HOST)
5130Sstevel@tonic-gate 		mask = &ipv6_all_ones;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	/*
5160Sstevel@tonic-gate 	 * Get the ipif, if any, corresponding to the gw_addr
5170Sstevel@tonic-gate 	 */
5180Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func,
5193448Sdh155122 	    &error, ipst);
5200Sstevel@tonic-gate 	if (ipif != NULL)
5210Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
5220Sstevel@tonic-gate 	else if (error == EINPROGRESS) {
5230Sstevel@tonic-gate 		ip1dbg(("ip_rt_add_v6: null and EINPROGRESS"));
5240Sstevel@tonic-gate 		return (error);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * GateD will attempt to create routes with a loopback interface
5290Sstevel@tonic-gate 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5300Sstevel@tonic-gate 	 * these routes to be added, but create them as interface routes
5310Sstevel@tonic-gate 	 * since the gateway is an interface address.
5320Sstevel@tonic-gate 	 */
5331822Srk129064 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5340Sstevel@tonic-gate 		flags &= ~RTF_GATEWAY;
5351822Srk129064 		if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) &&
5361822Srk129064 		    IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) &&
5371822Srk129064 		    IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) {
5381822Srk129064 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
5393448Sdh155122 			    ipif, ALL_ZONES, NULL, match_flags, ipst);
5401822Srk129064 			if (ire != NULL) {
5411822Srk129064 				ire_refrele(ire);
5421822Srk129064 				if (ipif_refheld)
5431822Srk129064 					ipif_refrele(ipif);
5441822Srk129064 				return (EEXIST);
5451822Srk129064 			}
5461822Srk129064 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
5471822Srk129064 			    "for 0x%x\n", (void *)ipif,
5481822Srk129064 			    ipif->ipif_ire_type,
5491822Srk129064 			    ntohl(ipif->ipif_lcl_addr)));
5501822Srk129064 			ire = ire_create_v6(
5511822Srk129064 			    dst_addr,
5521822Srk129064 			    mask,
5531822Srk129064 			    &ipif->ipif_v6src_addr,
5541822Srk129064 			    NULL,
5551822Srk129064 			    &ipif->ipif_mtu,
5561822Srk129064 			    NULL,
5571822Srk129064 			    NULL,
5581822Srk129064 			    NULL,
5591822Srk129064 			    ipif->ipif_net_type,
5601822Srk129064 			    ipif,
5611822Srk129064 			    NULL,
5621822Srk129064 			    0,
5631822Srk129064 			    0,
5641822Srk129064 			    flags,
5651822Srk129064 			    &ire_uinfo_null,
5661822Srk129064 			    NULL,
5673448Sdh155122 			    NULL,
5683448Sdh155122 			    ipst);
5691822Srk129064 			if (ire == NULL) {
5701822Srk129064 				if (ipif_refheld)
5711822Srk129064 					ipif_refrele(ipif);
5721822Srk129064 				return (ENOMEM);
5731822Srk129064 			}
5742535Ssangeeta 			error = ire_add(&ire, q, mp, func, B_FALSE);
5751822Srk129064 			if (error == 0)
5761822Srk129064 				goto save_ire;
5771822Srk129064 			/*
5781822Srk129064 			 * In the result of failure, ire_add() will have already
5791822Srk129064 			 * deleted the ire in question, so there is no need to
5801822Srk129064 			 * do that here.
5811822Srk129064 			 */
5821822Srk129064 			if (ipif_refheld)
5831822Srk129064 				ipif_refrele(ipif);
5841822Srk129064 			return (error);
5851822Srk129064 		}
5861822Srk129064 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5900Sstevel@tonic-gate 	 * and the gateway address provided is one of the system's interface
5910Sstevel@tonic-gate 	 * addresses.  By using the routing socket interface and supplying an
5920Sstevel@tonic-gate 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5930Sstevel@tonic-gate 	 * specifying an interface route to be created is available which uses
5940Sstevel@tonic-gate 	 * the interface index that specifies the outgoing interface rather than
5950Sstevel@tonic-gate 	 * the address of an outgoing interface (which may not be able to
5960Sstevel@tonic-gate 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5970Sstevel@tonic-gate 	 * flag, routes can be specified which not only specify the next-hop to
5980Sstevel@tonic-gate 	 * be used when routing to a certain prefix, but also which outgoing
5990Sstevel@tonic-gate 	 * interface should be used.
6000Sstevel@tonic-gate 	 *
6010Sstevel@tonic-gate 	 * Previously, interfaces would have unique addresses assigned to them
6020Sstevel@tonic-gate 	 * and so the address assigned to a particular interface could be used
6030Sstevel@tonic-gate 	 * to identify a particular interface.  One exception to this was the
6040Sstevel@tonic-gate 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6050Sstevel@tonic-gate 	 *
6060Sstevel@tonic-gate 	 * With the advent of IPv6 and its link-local addresses, this
6070Sstevel@tonic-gate 	 * restriction was relaxed and interfaces could share addresses between
6080Sstevel@tonic-gate 	 * themselves.  In fact, typically all of the link-local interfaces on
6090Sstevel@tonic-gate 	 * an IPv6 node or router will have the same link-local address.  In
6100Sstevel@tonic-gate 	 * order to differentiate between these interfaces, the use of an
6110Sstevel@tonic-gate 	 * interface index is necessary and this index can be carried inside a
6120Sstevel@tonic-gate 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6130Sstevel@tonic-gate 	 * of using the interface index, however, is that all of the ipif's that
6140Sstevel@tonic-gate 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6150Sstevel@tonic-gate 	 * cannot be used to differentiate between ipif's (or logical
6160Sstevel@tonic-gate 	 * interfaces) that belong to the same ill (physical interface).
6170Sstevel@tonic-gate 	 *
6180Sstevel@tonic-gate 	 * For example, in the following case involving IPv4 interfaces and
6190Sstevel@tonic-gate 	 * logical interfaces
6200Sstevel@tonic-gate 	 *
6210Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6220Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6230Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6240Sstevel@tonic-gate 	 *
6250Sstevel@tonic-gate 	 * the ipif's corresponding to each of these interface routes can be
6260Sstevel@tonic-gate 	 * uniquely identified by the "gateway" (actually interface address).
6270Sstevel@tonic-gate 	 *
6280Sstevel@tonic-gate 	 * In this case involving multiple IPv6 default routes to a particular
6290Sstevel@tonic-gate 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6300Sstevel@tonic-gate 	 * default route is of interest:
6310Sstevel@tonic-gate 	 *
6320Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6330Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/* RTF_GATEWAY not set */
6370Sstevel@tonic-gate 	if (!(flags & RTF_GATEWAY)) {
6380Sstevel@tonic-gate 		queue_t	*stq;
6390Sstevel@tonic-gate 
6401676Sjpk 		if (sp != NULL) {
6411676Sjpk 			ip2dbg(("ip_rt_add_v6: gateway security attributes "
6421676Sjpk 			    "cannot be set with interface route\n"));
6431676Sjpk 			if (ipif_refheld)
6441676Sjpk 				ipif_refrele(ipif);
6451676Sjpk 			return (EINVAL);
6461676Sjpk 		}
6471676Sjpk 
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * As the interface index specified with the RTA_IFP sockaddr is
6500Sstevel@tonic-gate 		 * the same for all ipif's off of an ill, the matching logic
6510Sstevel@tonic-gate 		 * below uses MATCH_IRE_ILL if such an index was specified.
6520Sstevel@tonic-gate 		 * This means that routes sharing the same prefix when added
6530Sstevel@tonic-gate 		 * using a RTA_IFP sockaddr must have distinct interface
6540Sstevel@tonic-gate 		 * indices (namely, they must be on distinct ill's).
6550Sstevel@tonic-gate 		 *
6560Sstevel@tonic-gate 		 * On the other hand, since the gateway address will usually be
6570Sstevel@tonic-gate 		 * different for each ipif on the system, the matching logic
6580Sstevel@tonic-gate 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6590Sstevel@tonic-gate 		 * route.  This means that interface routes for the same prefix
6600Sstevel@tonic-gate 		 * can be created if they belong to distinct ipif's and if a
6610Sstevel@tonic-gate 		 * RTA_IFP sockaddr is not present.
6620Sstevel@tonic-gate 		 */
6630Sstevel@tonic-gate 		if (ipif_arg != NULL) {
6640Sstevel@tonic-gate 			if (ipif_refheld) {
6650Sstevel@tonic-gate 				ipif_refrele(ipif);
6660Sstevel@tonic-gate 				ipif_refheld = B_FALSE;
6670Sstevel@tonic-gate 			}
6680Sstevel@tonic-gate 			ipif = ipif_arg;
6690Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
6700Sstevel@tonic-gate 		} else {
6710Sstevel@tonic-gate 			/*
6720Sstevel@tonic-gate 			 * Check the ipif corresponding to the gw_addr
6730Sstevel@tonic-gate 			 */
6740Sstevel@tonic-gate 			if (ipif == NULL)
6750Sstevel@tonic-gate 				return (ENETUNREACH);
6760Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		ASSERT(ipif != NULL);
6800Sstevel@tonic-gate 		/*
6810Sstevel@tonic-gate 		 * We check for an existing entry at this point.
6820Sstevel@tonic-gate 		 */
6830Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
6840Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, 0, IRE_INTERFACE,
6853448Sdh155122 		    ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6860Sstevel@tonic-gate 		if (ire != NULL) {
6870Sstevel@tonic-gate 			ire_refrele(ire);
6880Sstevel@tonic-gate 			if (ipif_refheld)
6890Sstevel@tonic-gate 				ipif_refrele(ipif);
6900Sstevel@tonic-gate 			return (EEXIST);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6940Sstevel@tonic-gate 		    ? ipif->ipif_rq : ipif->ipif_wq;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		/*
6970Sstevel@tonic-gate 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
6980Sstevel@tonic-gate 		 * IRE_IF_RESOLVER with the modified address and netmask.
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		ire = ire_create_v6(
7010Sstevel@tonic-gate 		    dst_addr,
7020Sstevel@tonic-gate 		    mask,
7030Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr,
7040Sstevel@tonic-gate 		    NULL,
7050Sstevel@tonic-gate 		    &ipif->ipif_mtu,
7060Sstevel@tonic-gate 		    NULL,
7070Sstevel@tonic-gate 		    NULL,
7080Sstevel@tonic-gate 		    stq,
7090Sstevel@tonic-gate 		    ipif->ipif_net_type,
7100Sstevel@tonic-gate 		    ipif,
7110Sstevel@tonic-gate 		    NULL,
7120Sstevel@tonic-gate 		    0,
7130Sstevel@tonic-gate 		    0,
7140Sstevel@tonic-gate 		    flags,
7151676Sjpk 		    &ire_uinfo_null,
7161676Sjpk 		    NULL,
7173448Sdh155122 		    NULL,
7183448Sdh155122 		    ipst);
7190Sstevel@tonic-gate 		if (ire == NULL) {
7200Sstevel@tonic-gate 			if (ipif_refheld)
7210Sstevel@tonic-gate 				ipif_refrele(ipif);
7220Sstevel@tonic-gate 			return (ENOMEM);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		/*
7260Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
7270Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
7280Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
7290Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
7305907Sja97890 		 * when generating aggregate routes). We also OR in the
7315907Sja97890 		 * RTF_BLACKHOLE flag as these interface routes, by
7325907Sja97890 		 * definition, can only be that.
7330Sstevel@tonic-gate 		 *
7340Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7350Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
7360Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
7370Sstevel@tonic-gate 		 *
7380Sstevel@tonic-gate 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7390Sstevel@tonic-gate 		 * routine, but rather using ire_create_v6() directly.
7400Sstevel@tonic-gate 		 */
7415907Sja97890 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7420Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
7435907Sja97890 			ire->ire_flags |= RTF_BLACKHOLE;
7445907Sja97890 		}
7452535Ssangeeta 		error = ire_add(&ire, q, mp, func, B_FALSE);
7460Sstevel@tonic-gate 		if (error == 0)
7470Sstevel@tonic-gate 			goto save_ire;
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * In the result of failure, ire_add() will have already
7500Sstevel@tonic-gate 		 * deleted the ire in question, so there is no need to
7510Sstevel@tonic-gate 		 * do that here.
7520Sstevel@tonic-gate 		 */
7530Sstevel@tonic-gate 		if (ipif_refheld)
7540Sstevel@tonic-gate 			ipif_refrele(ipif);
7550Sstevel@tonic-gate 		return (error);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	if (ipif_refheld) {
7580Sstevel@tonic-gate 		ipif_refrele(ipif);
7590Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	/*
7630Sstevel@tonic-gate 	 * Get an interface IRE for the specified gateway.
7640Sstevel@tonic-gate 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7650Sstevel@tonic-gate 	 * gateway, it is currently unreachable and we fail the request
7660Sstevel@tonic-gate 	 * accordingly.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	ipif = ipif_arg;
7690Sstevel@tonic-gate 	if (ipif_arg != NULL)
7700Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
7710Sstevel@tonic-gate 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg,
7723448Sdh155122 	    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7730Sstevel@tonic-gate 	if (gw_ire == NULL)
7740Sstevel@tonic-gate 		return (ENETUNREACH);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	/*
7770Sstevel@tonic-gate 	 * We create one of three types of IREs as a result of this request
7780Sstevel@tonic-gate 	 * based on the netmask.  A netmask of all ones (which is automatically
7790Sstevel@tonic-gate 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7800Sstevel@tonic-gate 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7810Sstevel@tonic-gate 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7820Sstevel@tonic-gate 	 * destination prefix.
7830Sstevel@tonic-gate 	 */
7840Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
7850Sstevel@tonic-gate 		type = IRE_HOST;
7860Sstevel@tonic-gate 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
7870Sstevel@tonic-gate 		type = IRE_DEFAULT;
7880Sstevel@tonic-gate 	else
7890Sstevel@tonic-gate 		type = IRE_PREFIX;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/* check for a duplicate entry */
7920Sstevel@tonic-gate 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ipif_arg,
7931676Sjpk 	    NULL, ALL_ZONES, 0, NULL,
7943448Sdh155122 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7950Sstevel@tonic-gate 	if (ire != NULL) {
7960Sstevel@tonic-gate 		ire_refrele(gw_ire);
7970Sstevel@tonic-gate 		ire_refrele(ire);
7980Sstevel@tonic-gate 		return (EEXIST);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8011676Sjpk 	/* Security attribute exists */
8021676Sjpk 	if (sp != NULL) {
8031676Sjpk 		tsol_gcgrp_addr_t ga;
8041676Sjpk 
8051676Sjpk 		/* find or create the gateway credentials group */
8061676Sjpk 		ga.ga_af = AF_INET6;
8071676Sjpk 		ga.ga_addr = *gw_addr;
8081676Sjpk 
8091676Sjpk 		/* we hold reference to it upon success */
8101676Sjpk 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
8111676Sjpk 		if (gcgrp == NULL) {
8121676Sjpk 			ire_refrele(gw_ire);
8131676Sjpk 			return (ENOMEM);
8141676Sjpk 		}
8151676Sjpk 
8161676Sjpk 		/*
8171676Sjpk 		 * Create and add the security attribute to the group; a
8181676Sjpk 		 * reference to the group is made upon allocating a new
8191676Sjpk 		 * entry successfully.  If it finds an already-existing
8201676Sjpk 		 * entry for the security attribute in the group, it simply
8211676Sjpk 		 * returns it and no new reference is made to the group.
8221676Sjpk 		 */
8231676Sjpk 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
8241676Sjpk 		if (gc == NULL) {
8251676Sjpk 			/* release reference held by gcgrp_lookup */
8261676Sjpk 			GCGRP_REFRELE(gcgrp);
8271676Sjpk 			ire_refrele(gw_ire);
8281676Sjpk 			return (ENOMEM);
8291676Sjpk 		}
8301676Sjpk 	}
8311676Sjpk 
8320Sstevel@tonic-gate 	/* Create the IRE. */
8330Sstevel@tonic-gate 	ire = ire_create_v6(
8340Sstevel@tonic-gate 	    dst_addr,				/* dest address */
8350Sstevel@tonic-gate 	    mask,				/* mask */
8360Sstevel@tonic-gate 	    /* src address assigned by the caller? */
8370Sstevel@tonic-gate 	    (((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) ?
8384459Skcpoon 	    src_addr : NULL),
8390Sstevel@tonic-gate 	    gw_addr,				/* gateway address */
8400Sstevel@tonic-gate 	    &gw_ire->ire_max_frag,
8414714Ssowmini 	    NULL,				/* no src nce */
8420Sstevel@tonic-gate 	    NULL,				/* no recv-from queue */
8430Sstevel@tonic-gate 	    NULL,				/* no send-to queue */
8440Sstevel@tonic-gate 	    (ushort_t)type,			/* IRE type */
8450Sstevel@tonic-gate 	    ipif_arg,
8460Sstevel@tonic-gate 	    NULL,
8470Sstevel@tonic-gate 	    0,
8480Sstevel@tonic-gate 	    0,
8490Sstevel@tonic-gate 	    flags,
8501676Sjpk 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
8511676Sjpk 	    gc,					/* security attribute */
8523448Sdh155122 	    NULL,
8533448Sdh155122 	    ipst);
8543448Sdh155122 
8551676Sjpk 	/*
8561676Sjpk 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
8571676Sjpk 	 * reference to the 'gcgrp'. We can now release the extra reference
8581676Sjpk 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
8591676Sjpk 	 */
8601676Sjpk 	if (gcgrp_xtraref)
8611676Sjpk 		GCGRP_REFRELE(gcgrp);
8620Sstevel@tonic-gate 	if (ire == NULL) {
8631676Sjpk 		if (gc != NULL)
8641676Sjpk 			GC_REFRELE(gc);
8650Sstevel@tonic-gate 		ire_refrele(gw_ire);
8660Sstevel@tonic-gate 		return (ENOMEM);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	/*
8700Sstevel@tonic-gate 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
8710Sstevel@tonic-gate 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/* Add the new IRE. */
8752535Ssangeeta 	error = ire_add(&ire, q, mp, func, B_FALSE);
8760Sstevel@tonic-gate 	/*
8770Sstevel@tonic-gate 	 * In the result of failure, ire_add() will have already
8780Sstevel@tonic-gate 	 * deleted the ire in question, so there is no need to
8790Sstevel@tonic-gate 	 * do that here.
8800Sstevel@tonic-gate 	 */
8810Sstevel@tonic-gate 	if (error != 0) {
8820Sstevel@tonic-gate 		ire_refrele(gw_ire);
8830Sstevel@tonic-gate 		return (error);
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (flags & RTF_MULTIRT) {
8870Sstevel@tonic-gate 		/*
8880Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
8890Sstevel@tonic-gate 		 * to add the dst address in the filtering database.
8900Sstevel@tonic-gate 		 * Replicated inbound packets coming from that address
8910Sstevel@tonic-gate 		 * will be filtered to discard the duplicates.
8920Sstevel@tonic-gate 		 * It is not necessary to call the CGTP filter hook
8930Sstevel@tonic-gate 		 * when the dst address is a multicast, because an
8940Sstevel@tonic-gate 		 * IP source address cannot be a multicast.
8950Sstevel@tonic-gate 		 */
8964961Snordmark 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
8970Sstevel@tonic-gate 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
8984961Snordmark 			int res;
8994961Snordmark 
9004961Snordmark 			res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v6(
9014961Snordmark 			    ipst->ips_netstack->netstack_stackid,
9020Sstevel@tonic-gate 			    &ire->ire_addr_v6,
9030Sstevel@tonic-gate 			    &ire->ire_gateway_addr_v6,
9040Sstevel@tonic-gate 			    &ire->ire_src_addr_v6,
9050Sstevel@tonic-gate 			    &gw_ire->ire_src_addr_v6);
9060Sstevel@tonic-gate 			if (res != 0) {
9070Sstevel@tonic-gate 				ire_refrele(gw_ire);
9080Sstevel@tonic-gate 				ire_delete(ire);
9090Sstevel@tonic-gate 				return (res);
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate 
9141676Sjpk 	/*
9151676Sjpk 	 * Now that the prefix IRE entry has been created, delete any
9161676Sjpk 	 * existing gateway IRE cache entries as well as any IRE caches
9171676Sjpk 	 * using the gateway, and force them to be created through
9181676Sjpk 	 * ip_newroute_v6.
9191676Sjpk 	 */
9201676Sjpk 	if (gc != NULL) {
9211676Sjpk 		ASSERT(gcgrp != NULL);
9223448Sdh155122 		ire_clookup_delete_cache_gw_v6(gw_addr, ALL_ZONES, ipst);
9231676Sjpk 	}
9241676Sjpk 
9250Sstevel@tonic-gate save_ire:
9260Sstevel@tonic-gate 	if (gw_ire != NULL) {
9270Sstevel@tonic-gate 		ire_refrele(gw_ire);
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 	if (ipif != NULL) {
9300Sstevel@tonic-gate 		mblk_t	*save_mp;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		/*
9330Sstevel@tonic-gate 		 * Save enough information so that we can recreate the IRE if
9340Sstevel@tonic-gate 		 * the interface goes down and then up.  The metrics associated
9350Sstevel@tonic-gate 		 * with the route will be saved as well when rts_setmetrics() is
9360Sstevel@tonic-gate 		 * called after the IRE has been created.  In the case where
9370Sstevel@tonic-gate 		 * memory cannot be allocated, none of this information will be
9380Sstevel@tonic-gate 		 * saved.
9390Sstevel@tonic-gate 		 */
9400Sstevel@tonic-gate 		save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
9410Sstevel@tonic-gate 		if (save_mp != NULL) {
9420Sstevel@tonic-gate 			ifrt_t	*ifrt;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 			save_mp->b_wptr += sizeof (ifrt_t);
9450Sstevel@tonic-gate 			ifrt = (ifrt_t *)save_mp->b_rptr;
9460Sstevel@tonic-gate 			bzero(ifrt, sizeof (ifrt_t));
9470Sstevel@tonic-gate 			ifrt->ifrt_type = ire->ire_type;
9480Sstevel@tonic-gate 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
9490Sstevel@tonic-gate 			mutex_enter(&ire->ire_lock);
9500Sstevel@tonic-gate 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
9510Sstevel@tonic-gate 			ifrt->ifrt_v6src_addr = ire->ire_src_addr_v6;
9520Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
9530Sstevel@tonic-gate 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
9540Sstevel@tonic-gate 			ifrt->ifrt_flags = ire->ire_flags;
9550Sstevel@tonic-gate 			ifrt->ifrt_max_frag = ire->ire_max_frag;
9560Sstevel@tonic-gate 			mutex_enter(&ipif->ipif_saved_ire_lock);
9570Sstevel@tonic-gate 			save_mp->b_cont = ipif->ipif_saved_ire_mp;
9580Sstevel@tonic-gate 			ipif->ipif_saved_ire_mp = save_mp;
9590Sstevel@tonic-gate 			ipif->ipif_saved_ire_cnt++;
9600Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 	if (ire_arg != NULL) {
9640Sstevel@tonic-gate 		/*
9650Sstevel@tonic-gate 		 * Store the ire that was successfully added into where ire_arg
9660Sstevel@tonic-gate 		 * points to so that callers don't have to look it up
9670Sstevel@tonic-gate 		 * themselves (but they are responsible for ire_refrele()ing
9680Sstevel@tonic-gate 		 * the ire when they are finished with it).
9690Sstevel@tonic-gate 		 */
9700Sstevel@tonic-gate 		*ire_arg = ire;
9710Sstevel@tonic-gate 	} else {
9720Sstevel@tonic-gate 		ire_refrele(ire);		/* Held in ire_add */
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	if (ipif_refheld)
9750Sstevel@tonic-gate 		ipif_refrele(ipif);
9760Sstevel@tonic-gate 	return (0);
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate /*
9800Sstevel@tonic-gate  * ip_rt_delete_v6 is called to delete an IPv6 route.
9810Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
9820Sstevel@tonic-gate  * (for link-local destinations and gateways).
9830Sstevel@tonic-gate  */
9840Sstevel@tonic-gate /* ARGSUSED4 */
9850Sstevel@tonic-gate int
9860Sstevel@tonic-gate ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
9870Sstevel@tonic-gate     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ipif_t *ipif_arg,
9883448Sdh155122     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	ire_t	*ire = NULL;
9910Sstevel@tonic-gate 	ipif_t	*ipif;
9920Sstevel@tonic-gate 	uint_t	type;
9930Sstevel@tonic-gate 	uint_t	match_flags = MATCH_IRE_TYPE;
9940Sstevel@tonic-gate 	int	err = 0;
9950Sstevel@tonic-gate 	boolean_t	ipif_refheld = B_FALSE;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
9990Sstevel@tonic-gate 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	if (flags & RTF_HOST) {
10020Sstevel@tonic-gate 		mask = &ipv6_all_ones;
10030Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
10040Sstevel@tonic-gate 	} else if (rtm_addrs & RTA_NETMASK) {
10050Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Note that RTF_GATEWAY is never set on a delete, therefore
10100Sstevel@tonic-gate 	 * we check if the gateway address is one of our interfaces first,
10110Sstevel@tonic-gate 	 * and fall back on RTF_GATEWAY routes.
10120Sstevel@tonic-gate 	 *
10130Sstevel@tonic-gate 	 * This makes it possible to delete an original
10140Sstevel@tonic-gate 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
10150Sstevel@tonic-gate 	 *
10160Sstevel@tonic-gate 	 * As the interface index specified with the RTA_IFP sockaddr is the
10170Sstevel@tonic-gate 	 * same for all ipif's off of an ill, the matching logic below uses
10180Sstevel@tonic-gate 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
10190Sstevel@tonic-gate 	 * sharing the same prefix and interface index as the the route
10200Sstevel@tonic-gate 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
10210Sstevel@tonic-gate 	 * is specified in the request.
10220Sstevel@tonic-gate 	 *
10230Sstevel@tonic-gate 	 * On the other hand, since the gateway address will usually be
10240Sstevel@tonic-gate 	 * different for each ipif on the system, the matching logic
10250Sstevel@tonic-gate 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
10260Sstevel@tonic-gate 	 * route.  This means that interface routes for the same prefix can be
10270Sstevel@tonic-gate 	 * uniquely identified if they belong to distinct ipif's and if a
10280Sstevel@tonic-gate 	 * RTA_IFP sockaddr is not present.
10290Sstevel@tonic-gate 	 *
10300Sstevel@tonic-gate 	 * For more detail on specifying routes by gateway address and by
10310Sstevel@tonic-gate 	 * interface index, see the comments in ip_rt_add_v6().
10320Sstevel@tonic-gate 	 */
10333448Sdh155122 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, &err,
10343448Sdh155122 	    ipst);
10350Sstevel@tonic-gate 	if (ipif != NULL) {
10360Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
10370Sstevel@tonic-gate 		if (ipif_arg != NULL) {
10380Sstevel@tonic-gate 			ipif_refrele(ipif);
10390Sstevel@tonic-gate 			ipif_refheld = B_FALSE;
10400Sstevel@tonic-gate 			ipif = ipif_arg;
10410Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
10420Sstevel@tonic-gate 		} else {
10430Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		if (ipif->ipif_ire_type == IRE_LOOPBACK)
10470Sstevel@tonic-gate 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
10483448Sdh155122 			    ipif, ALL_ZONES, NULL, match_flags, ipst);
10490Sstevel@tonic-gate 		if (ire == NULL)
10500Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, 0,
10511676Sjpk 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
10523448Sdh155122 			    match_flags, ipst);
10530Sstevel@tonic-gate 	} else if (err == EINPROGRESS) {
10540Sstevel@tonic-gate 		return (err);
10550Sstevel@tonic-gate 	} else {
10560Sstevel@tonic-gate 		err = 0;
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 	if (ire == NULL) {
10590Sstevel@tonic-gate 		/*
10600Sstevel@tonic-gate 		 * At this point, the gateway address is not one of our own
10610Sstevel@tonic-gate 		 * addresses or a matching interface route was not found.  We
10620Sstevel@tonic-gate 		 * set the IRE type to lookup based on whether
10630Sstevel@tonic-gate 		 * this is a host route, a default route or just a prefix.
10640Sstevel@tonic-gate 		 *
10650Sstevel@tonic-gate 		 * If an ipif_arg was passed in, then the lookup is based on an
10660Sstevel@tonic-gate 		 * interface index so MATCH_IRE_ILL is added to match_flags.
10670Sstevel@tonic-gate 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
10680Sstevel@tonic-gate 		 * set as the route being looked up is not a traditional
10690Sstevel@tonic-gate 		 * interface route.
10700Sstevel@tonic-gate 		 */
10710Sstevel@tonic-gate 		match_flags &= ~MATCH_IRE_IPIF;
10720Sstevel@tonic-gate 		match_flags |= MATCH_IRE_GW;
10730Sstevel@tonic-gate 		if (ipif_arg != NULL)
10740Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
10750Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
10760Sstevel@tonic-gate 			type = IRE_HOST;
10770Sstevel@tonic-gate 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
10780Sstevel@tonic-gate 			type = IRE_DEFAULT;
10790Sstevel@tonic-gate 		else
10800Sstevel@tonic-gate 			type = IRE_PREFIX;
10810Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
10823448Sdh155122 		    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if (ipif_refheld) {
10860Sstevel@tonic-gate 		ipif_refrele(ipif);
10870Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	if (ire == NULL)
10900Sstevel@tonic-gate 		return (ESRCH);
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	if (ire->ire_flags & RTF_MULTIRT) {
10930Sstevel@tonic-gate 		/*
10940Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
10950Sstevel@tonic-gate 		 * to remove the dst address from the filtering database.
10960Sstevel@tonic-gate 		 * Packets coming from that address will no longer be
10970Sstevel@tonic-gate 		 * filtered to remove duplicates.
10980Sstevel@tonic-gate 		 */
10994961Snordmark 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
11004961Snordmark 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v6(
11014961Snordmark 			    ipst->ips_netstack->netstack_stackid,
11020Sstevel@tonic-gate 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
11030Sstevel@tonic-gate 		}
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	ipif = ire->ire_ipif;
11070Sstevel@tonic-gate 	if (ipif != NULL) {
11080Sstevel@tonic-gate 		mblk_t		**mpp;
11090Sstevel@tonic-gate 		mblk_t		*mp;
11100Sstevel@tonic-gate 		ifrt_t		*ifrt;
11110Sstevel@tonic-gate 		in6_addr_t	gw_addr_v6;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		/* Remove from ipif_saved_ire_mp list if it is there */
11140Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
11150Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
11160Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
11170Sstevel@tonic-gate 		mutex_enter(&ipif->ipif_saved_ire_lock);
11180Sstevel@tonic-gate 		for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
11190Sstevel@tonic-gate 		    mpp = &(*mpp)->b_cont) {
11200Sstevel@tonic-gate 			/*
11210Sstevel@tonic-gate 			 * On a given ipif, the triple of address, gateway and
11220Sstevel@tonic-gate 			 * mask is unique for each saved IRE (in the case of
11230Sstevel@tonic-gate 			 * ordinary interface routes, the gateway address is
11240Sstevel@tonic-gate 			 * all-zeroes).
11250Sstevel@tonic-gate 			 */
11260Sstevel@tonic-gate 			mp = *mpp;
11270Sstevel@tonic-gate 			ifrt = (ifrt_t *)mp->b_rptr;
11280Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
11290Sstevel@tonic-gate 			    &ire->ire_addr_v6) &&
11300Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
11310Sstevel@tonic-gate 			    &gw_addr_v6) &&
11320Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
11330Sstevel@tonic-gate 			    &ire->ire_mask_v6)) {
11340Sstevel@tonic-gate 				*mpp = mp->b_cont;
11350Sstevel@tonic-gate 				ipif->ipif_saved_ire_cnt--;
11360Sstevel@tonic-gate 				freeb(mp);
11370Sstevel@tonic-gate 				break;
11380Sstevel@tonic-gate 			}
11390Sstevel@tonic-gate 		}
11400Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 	ire_delete(ire);
11430Sstevel@tonic-gate 	ire_refrele(ire);
11440Sstevel@tonic-gate 	return (err);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
114810616SSebastien.Roy@Sun.COM  * Derive an interface id from the link layer address.
11490Sstevel@tonic-gate  */
115010616SSebastien.Roy@Sun.COM void
11510Sstevel@tonic-gate ill_setdefaulttoken(ill_t *ill)
11520Sstevel@tonic-gate {
1153*10773SSebastien.Roy@Sun.COM 	if (!ill->ill_manual_token) {
115410616SSebastien.Roy@Sun.COM 		bzero(&ill->ill_token, sizeof (ill->ill_token));
115510616SSebastien.Roy@Sun.COM 		MEDIA_V6INTFID(ill->ill_media, ill, &ill->ill_token);
115610616SSebastien.Roy@Sun.COM 		ill->ill_token_length = IPV6_TOKEN_LEN;
115710616SSebastien.Roy@Sun.COM 	}
115810616SSebastien.Roy@Sun.COM }
115910616SSebastien.Roy@Sun.COM 
116010616SSebastien.Roy@Sun.COM void
116110616SSebastien.Roy@Sun.COM ill_setdesttoken(ill_t *ill)
116210616SSebastien.Roy@Sun.COM {
116310616SSebastien.Roy@Sun.COM 	bzero(&ill->ill_dest_token, sizeof (ill->ill_dest_token));
116410616SSebastien.Roy@Sun.COM 	MEDIA_V6DESTINTFID(ill->ill_media, ill, &ill->ill_dest_token);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate  * Create a link-local address from a token.
11690Sstevel@tonic-gate  */
11700Sstevel@tonic-gate static void
11710Sstevel@tonic-gate ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate 	int i;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
11760Sstevel@tonic-gate 		dest->s6_addr32[i] =
11770Sstevel@tonic-gate 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
118210616SSebastien.Roy@Sun.COM  * Set a default IPv6 address for a 6to4 tunnel interface 2002:<tsrc>::1/16
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate static void
118510616SSebastien.Roy@Sun.COM ipif_set6to4addr(ipif_t *ipif)
11860Sstevel@tonic-gate {
118710616SSebastien.Roy@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
118810616SSebastien.Roy@Sun.COM 	struct in_addr	v4phys;
118910616SSebastien.Roy@Sun.COM 
119010616SSebastien.Roy@Sun.COM 	ASSERT(ill->ill_mactype == DL_6TO4);
119110616SSebastien.Roy@Sun.COM 	ASSERT(ill->ill_phys_addr_length == sizeof (struct in_addr));
119210616SSebastien.Roy@Sun.COM 	ASSERT(ipif->ipif_isv6);
119310616SSebastien.Roy@Sun.COM 
119410616SSebastien.Roy@Sun.COM 	if (ipif->ipif_flags & IPIF_UP)
11950Sstevel@tonic-gate 		return;
119610616SSebastien.Roy@Sun.COM 
119710616SSebastien.Roy@Sun.COM 	(void) ip_plen_to_mask_v6(16, &ipif->ipif_v6net_mask);
119810616SSebastien.Roy@Sun.COM 	bcopy(ill->ill_phys_addr, &v4phys, sizeof (struct in_addr));
119910616SSebastien.Roy@Sun.COM 	IN6_V4ADDR_TO_6TO4(&v4phys, &ipif->ipif_v6lcl_addr);
120010616SSebastien.Roy@Sun.COM 	ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
120110616SSebastien.Roy@Sun.COM 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
120210616SSebastien.Roy@Sun.COM 	    ipif->ipif_v6subnet);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate /*
12060Sstevel@tonic-gate  * Is it not possible to set the link local address?
12070Sstevel@tonic-gate  * The address can be set if the token is set, and the token
12080Sstevel@tonic-gate  * isn't too long.
12090Sstevel@tonic-gate  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
12100Sstevel@tonic-gate  */
12110Sstevel@tonic-gate boolean_t
12120Sstevel@tonic-gate ipif_cant_setlinklocal(ipif_t *ipif)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
12170Sstevel@tonic-gate 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
12180Sstevel@tonic-gate 		return (B_TRUE);
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	return (B_FALSE);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate /*
12240Sstevel@tonic-gate  * Generate a link-local address from the token.
12250Sstevel@tonic-gate  */
122610616SSebastien.Roy@Sun.COM void
12270Sstevel@tonic-gate ipif_setlinklocal(ipif_t *ipif)
12280Sstevel@tonic-gate {
12293706Svi117747 	ill_t		*ill = ipif->ipif_ill;
12303706Svi117747 	in6_addr_t	ov6addr;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
12330Sstevel@tonic-gate 
123410616SSebastien.Roy@Sun.COM 	/*
123510616SSebastien.Roy@Sun.COM 	 * ill_manual_linklocal is set when the link-local address was
123610616SSebastien.Roy@Sun.COM 	 * manually configured.
123710616SSebastien.Roy@Sun.COM 	 */
123810616SSebastien.Roy@Sun.COM 	if (ill->ill_manual_linklocal)
123910616SSebastien.Roy@Sun.COM 		return;
124010616SSebastien.Roy@Sun.COM 
124110616SSebastien.Roy@Sun.COM 	/*
124210616SSebastien.Roy@Sun.COM 	 * IPv6 interfaces over 6to4 tunnels are special.  They do not have
124310616SSebastien.Roy@Sun.COM 	 * link-local addresses, but instead have a single automatically
124410616SSebastien.Roy@Sun.COM 	 * generated global address.
124510616SSebastien.Roy@Sun.COM 	 */
124610616SSebastien.Roy@Sun.COM 	if (ill->ill_mactype == DL_6TO4) {
124710616SSebastien.Roy@Sun.COM 		ipif_set6to4addr(ipif);
124810616SSebastien.Roy@Sun.COM 		return;
124910616SSebastien.Roy@Sun.COM 	}
125010616SSebastien.Roy@Sun.COM 
12510Sstevel@tonic-gate 	if (ipif_cant_setlinklocal(ipif))
125210616SSebastien.Roy@Sun.COM 		return;
12530Sstevel@tonic-gate 
12543706Svi117747 	ov6addr = ipif->ipif_v6lcl_addr;
12550Sstevel@tonic-gate 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
12563706Svi117747 	sctp_update_ipif_addr(ipif, ov6addr);
12570Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
125810616SSebastien.Roy@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
125910616SSebastien.Roy@Sun.COM 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
126010616SSebastien.Roy@Sun.COM 		    ipif->ipif_v6subnet);
126110616SSebastien.Roy@Sun.COM 	}
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	if (ipif->ipif_flags & IPIF_NOLOCAL) {
12640Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12650Sstevel@tonic-gate 	} else {
12660Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12670Sstevel@tonic-gate 	}
126810616SSebastien.Roy@Sun.COM }
126910616SSebastien.Roy@Sun.COM 
127010616SSebastien.Roy@Sun.COM /*
127110616SSebastien.Roy@Sun.COM  * Set the destination link-local address for a point-to-point IPv6
127210616SSebastien.Roy@Sun.COM  * interface with a destination interface id (IP tunnels are such
127310616SSebastien.Roy@Sun.COM  * interfaces).
127410616SSebastien.Roy@Sun.COM  */
127510616SSebastien.Roy@Sun.COM void
127610616SSebastien.Roy@Sun.COM ipif_setdestlinklocal(ipif_t *ipif)
127710616SSebastien.Roy@Sun.COM {
127810616SSebastien.Roy@Sun.COM 	ill_t	*ill = ipif->ipif_ill;
127910616SSebastien.Roy@Sun.COM 
128010616SSebastien.Roy@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
128110616SSebastien.Roy@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_dest_token))
128210616SSebastien.Roy@Sun.COM 		return;
128310616SSebastien.Roy@Sun.COM 	ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr, &ill->ill_dest_token);
128410616SSebastien.Roy@Sun.COM 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate /*
12880Sstevel@tonic-gate  * This function sets up the multicast mappings in NDP.
12890Sstevel@tonic-gate  * Unlike ARP, there are no mapping_mps here. We delete the
12900Sstevel@tonic-gate  * mapping nces and add a new one.
12910Sstevel@tonic-gate  *
12920Sstevel@tonic-gate  * Returns non-zero on error and 0 on success.
12930Sstevel@tonic-gate  */
12940Sstevel@tonic-gate int
12950Sstevel@tonic-gate ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
12980Sstevel@tonic-gate 	in6_addr_t	v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0};
12990Sstevel@tonic-gate 	in6_addr_t	v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0};
13000Sstevel@tonic-gate 	in6_addr_t	v6_extract_mask;
13010Sstevel@tonic-gate 	uchar_t		*phys_addr, *bphys_addr, *alloc_phys;
13020Sstevel@tonic-gate 	nce_t		*mnce = NULL;
13030Sstevel@tonic-gate 	int		err = 0;
13040Sstevel@tonic-gate 	phyint_t	*phyi = ill->ill_phyint;
13050Sstevel@tonic-gate 	uint32_t	hw_extract_start;
13060Sstevel@tonic-gate 	dl_unitdata_req_t *dlur;
13073448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	if (ret_nce != NULL)
13100Sstevel@tonic-gate 		*ret_nce = NULL;
13118485SPeter.Memishian@Sun.COM 
131210616SSebastien.Roy@Sun.COM 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
131310616SSebastien.Roy@Sun.COM 		return (0);
131410616SSebastien.Roy@Sun.COM 
13158485SPeter.Memishian@Sun.COM 	/*
13168485SPeter.Memishian@Sun.COM 	 * IPMP meta-interfaces don't have any inherent multicast mappings,
13178485SPeter.Memishian@Sun.COM 	 * and instead use the ones on the underlying interfaces.
13188485SPeter.Memishian@Sun.COM 	 */
13198485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill))
13208485SPeter.Memishian@Sun.COM 		return (0);
13218485SPeter.Memishian@Sun.COM 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * Delete the mapping nce. Normally these should not exist
13240Sstevel@tonic-gate 	 * as a previous ipif_down -> ipif_ndp_down should have deleted
13250Sstevel@tonic-gate 	 * all the nces. But they can exist if ip_rput_dlpi_writer
13268485SPeter.Memishian@Sun.COM 	 * calls this when PHYI_MULTI_BCAST is set.  Mappings are always
13278485SPeter.Memishian@Sun.COM 	 * tied to the underlying ill, so don't match across the illgrp.
13280Sstevel@tonic-gate 	 */
13298485SPeter.Memishian@Sun.COM 	mnce = ndp_lookup_v6(ill, B_FALSE, &v6_mcast_addr, B_FALSE);
13300Sstevel@tonic-gate 	if (mnce != NULL) {
13310Sstevel@tonic-gate 		ndp_delete(mnce);
13320Sstevel@tonic-gate 		NCE_REFRELE(mnce);
13330Sstevel@tonic-gate 		mnce = NULL;
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * Get media specific v6 mapping information. Note that
13380Sstevel@tonic-gate 	 * nd_lla_len can be 0 for tunnels.
13390Sstevel@tonic-gate 	 */
13400Sstevel@tonic-gate 	alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
13410Sstevel@tonic-gate 	if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0))
13420Sstevel@tonic-gate 		return (ENOMEM);
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * Determine the broadcast address.
13450Sstevel@tonic-gate 	 */
13460Sstevel@tonic-gate 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13470Sstevel@tonic-gate 	if (ill->ill_sap_length < 0)
13480Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13490Sstevel@tonic-gate 	else
13500Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur +
13510Sstevel@tonic-gate 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * Check PHYI_MULTI_BCAST and possible length of physical
13550Sstevel@tonic-gate 	 * address to determine if we use the mapping or the
13560Sstevel@tonic-gate 	 * broadcast address.
13570Sstevel@tonic-gate 	 */
13580Sstevel@tonic-gate 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13590Sstevel@tonic-gate 	    (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len,
13600Sstevel@tonic-gate 	    bphys_addr, alloc_phys, &hw_extract_start,
13610Sstevel@tonic-gate 	    &v6_extract_mask))) {
13620Sstevel@tonic-gate 		if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) {
13630Sstevel@tonic-gate 			kmem_free(alloc_phys, ill->ill_nd_lla_len);
13640Sstevel@tonic-gate 			return (E2BIG);
13650Sstevel@tonic-gate 		}
13660Sstevel@tonic-gate 		/* Use the link-layer broadcast address for MULTI_BCAST */
13670Sstevel@tonic-gate 		phys_addr = bphys_addr;
13680Sstevel@tonic-gate 		bzero(&v6_extract_mask, sizeof (v6_extract_mask));
13690Sstevel@tonic-gate 		hw_extract_start = ill->ill_nd_lla_len;
13700Sstevel@tonic-gate 	} else {
13710Sstevel@tonic-gate 		phys_addr = alloc_phys;
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_BROADCAST) ||
13740Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST) ||
13750Sstevel@tonic-gate 	    (phyi->phyint_flags & PHYI_MULTI_BCAST)) {
13763448Sdh155122 		mutex_enter(&ipst->ips_ndp6->ndp_g_lock);
13774714Ssowmini 		err = ndp_add_v6(ill,
13780Sstevel@tonic-gate 		    phys_addr,
13790Sstevel@tonic-gate 		    &v6_mcast_addr,	/* v6 address */
13800Sstevel@tonic-gate 		    &v6_mcast_mask,	/* v6 mask */
13810Sstevel@tonic-gate 		    &v6_extract_mask,
13820Sstevel@tonic-gate 		    hw_extract_start,
13830Sstevel@tonic-gate 		    NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD,
13840Sstevel@tonic-gate 		    ND_REACHABLE,
13854714Ssowmini 		    &mnce);
13863448Sdh155122 		mutex_exit(&ipst->ips_ndp6->ndp_g_lock);
13870Sstevel@tonic-gate 		if (err == 0) {
13880Sstevel@tonic-gate 			if (ret_nce != NULL) {
13890Sstevel@tonic-gate 				*ret_nce = mnce;
13900Sstevel@tonic-gate 			} else {
13910Sstevel@tonic-gate 				NCE_REFRELE(mnce);
13920Sstevel@tonic-gate 			}
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 	kmem_free(alloc_phys, ill->ill_nd_lla_len);
13960Sstevel@tonic-gate 	return (err);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate /*
14004972Smeem  * Get the resolver set up for a new ipif.  (Always called as writer.)
14010Sstevel@tonic-gate  */
14020Sstevel@tonic-gate int
14038485SPeter.Memishian@Sun.COM ipif_ndp_up(ipif_t *ipif, boolean_t initial)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
14060Sstevel@tonic-gate 	int		err = 0;
14070Sstevel@tonic-gate 	nce_t		*nce = NULL;
14080Sstevel@tonic-gate 	nce_t		*mnce = NULL;
14098485SPeter.Memishian@Sun.COM 	boolean_t	added_ipif = B_FALSE;
14108485SPeter.Memishian@Sun.COM 
14118485SPeter.Memishian@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
14124972Smeem 	ip1dbg(("ipif_ndp_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	/*
14150Sstevel@tonic-gate 	 * ND not supported on XRESOLV interfaces. If ND support (multicast)
14160Sstevel@tonic-gate 	 * added later, take out this check.
14170Sstevel@tonic-gate 	 */
14182546Scarlsonj 	if ((ill->ill_flags & ILLF_XRESOLV) ||
14194972Smeem 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) ||
14202546Scarlsonj 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
14212546Scarlsonj 		ipif->ipif_addr_ready = 1;
14220Sstevel@tonic-gate 		return (0);
14232546Scarlsonj 	}
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	/*
14260Sstevel@tonic-gate 	 * Need to setup multicast mapping only when the first
14270Sstevel@tonic-gate 	 * interface is coming UP.
14280Sstevel@tonic-gate 	 */
14290Sstevel@tonic-gate 	if (ill->ill_ipif_up_count == 0 &&
14300Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST)) {
14310Sstevel@tonic-gate 		/*
14320Sstevel@tonic-gate 		 * We set the multicast before setting up the mapping for
14330Sstevel@tonic-gate 		 * local address because ipif_ndp_setup_multicast does
14340Sstevel@tonic-gate 		 * ndp_walk to delete nces which will delete the mapping
14350Sstevel@tonic-gate 		 * for local address also if we added the mapping for
14360Sstevel@tonic-gate 		 * local address first.
14370Sstevel@tonic-gate 		 */
14380Sstevel@tonic-gate 		err = ipif_ndp_setup_multicast(ipif, &mnce);
14390Sstevel@tonic-gate 		if (err != 0)
14400Sstevel@tonic-gate 			return (err);
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
14440Sstevel@tonic-gate 		uint16_t	flags;
14458485SPeter.Memishian@Sun.COM 		uint16_t	state;
14468485SPeter.Memishian@Sun.COM 		uchar_t		*hw_addr = NULL;
14478485SPeter.Memishian@Sun.COM 		ill_t		*bound_ill;
14488485SPeter.Memishian@Sun.COM 		ipmp_illgrp_t	*illg = ill->ill_grp;
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		/* Permanent entries don't need NUD */
14513340Smeem 		flags = NCE_F_PERMANENT | NCE_F_NONUD;
14520Sstevel@tonic-gate 		if (ill->ill_flags & ILLF_ROUTER)
14530Sstevel@tonic-gate 			flags |= NCE_F_ISROUTER;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_ANYCAST)
14560Sstevel@tonic-gate 			flags |= NCE_F_ANYCAST;
14570Sstevel@tonic-gate 
14588485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill)) {
14598485SPeter.Memishian@Sun.COM 			ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
14608485SPeter.Memishian@Sun.COM 			/*
14618485SPeter.Memishian@Sun.COM 			 * If we're here via ipif_up(), then the ipif won't be
14628485SPeter.Memishian@Sun.COM 			 * bound yet -- add it to the group, which will bind
14638485SPeter.Memishian@Sun.COM 			 * it if possible.  (We would add it in ipif_up(), but
14648485SPeter.Memishian@Sun.COM 			 * deleting on failure there is gruesome.)  If we're
14658485SPeter.Memishian@Sun.COM 			 * here via ipmp_ill_bind_ipif(), then the ipif has
14668485SPeter.Memishian@Sun.COM 			 * already been added to the group and we just need to
14678485SPeter.Memishian@Sun.COM 			 * use the binding.
14688485SPeter.Memishian@Sun.COM 			 */
14698485SPeter.Memishian@Sun.COM 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
14708485SPeter.Memishian@Sun.COM 				bound_ill = ipmp_illgrp_add_ipif(illg, ipif);
14718485SPeter.Memishian@Sun.COM 				if (bound_ill == NULL) {
14728485SPeter.Memishian@Sun.COM 					/*
14738485SPeter.Memishian@Sun.COM 					 * We couldn't bind the ipif to an ill
14748485SPeter.Memishian@Sun.COM 					 * yet, so we have nothing to publish.
14758485SPeter.Memishian@Sun.COM 					 * Set ipif_addr_ready so that this
14768485SPeter.Memishian@Sun.COM 					 * address can be used locally for now.
14778485SPeter.Memishian@Sun.COM 					 * The routing socket message will be
14788485SPeter.Memishian@Sun.COM 					 * sent from ipif_up_done_v6().
14798485SPeter.Memishian@Sun.COM 					 */
14808485SPeter.Memishian@Sun.COM 					ipif->ipif_addr_ready = 1;
14818485SPeter.Memishian@Sun.COM 					return (0);
14828485SPeter.Memishian@Sun.COM 				}
14838485SPeter.Memishian@Sun.COM 				added_ipif = B_TRUE;
14840Sstevel@tonic-gate 			}
14858485SPeter.Memishian@Sun.COM 			hw_addr = bound_ill->ill_nd_lla;
14868485SPeter.Memishian@Sun.COM 		} else {
14878485SPeter.Memishian@Sun.COM 			bound_ill = ill;
14888485SPeter.Memishian@Sun.COM 			if (ill->ill_net_type == IRE_IF_RESOLVER)
14898485SPeter.Memishian@Sun.COM 				hw_addr = ill->ill_nd_lla;
14900Sstevel@tonic-gate 		}
14918485SPeter.Memishian@Sun.COM 
14928485SPeter.Memishian@Sun.COM 		/*
14938485SPeter.Memishian@Sun.COM 		 * If this is an initial bring-up (or the ipif was never
14948485SPeter.Memishian@Sun.COM 		 * completely brought up), do DAD.  Otherwise, we're here
14958485SPeter.Memishian@Sun.COM 		 * because IPMP has rebound an address to this ill: send
14968485SPeter.Memishian@Sun.COM 		 * unsolicited advertisements to inform others.
14978485SPeter.Memishian@Sun.COM 		 */
14988485SPeter.Memishian@Sun.COM 		if (initial || !ipif->ipif_addr_ready) {
14998485SPeter.Memishian@Sun.COM 			state = ND_PROBE;
15008485SPeter.Memishian@Sun.COM 		} else {
15018485SPeter.Memishian@Sun.COM 			state = ND_REACHABLE;
15028485SPeter.Memishian@Sun.COM 			flags |= NCE_F_UNSOL_ADV;
15038485SPeter.Memishian@Sun.COM 		}
15049814SSowmini.Varadhan@Sun.COM retry:
15058485SPeter.Memishian@Sun.COM 		/*
15069287SSowmini.Varadhan@Sun.COM 		 * Create an nce for the local address. We pass a match_illgrp
15079287SSowmini.Varadhan@Sun.COM 		 * of B_TRUE because the local address must be unique across
15089287SSowmini.Varadhan@Sun.COM 		 * the illgrp, and the existence of an nce with nce_ill set
15099287SSowmini.Varadhan@Sun.COM 		 * to any ill in the group is indicative of a duplicate address
15108485SPeter.Memishian@Sun.COM 		 */
15118485SPeter.Memishian@Sun.COM 		err = ndp_lookup_then_add_v6(bound_ill,
15129287SSowmini.Varadhan@Sun.COM 		    B_TRUE,
15130Sstevel@tonic-gate 		    hw_addr,
15144972Smeem 		    &ipif->ipif_v6lcl_addr,
15150Sstevel@tonic-gate 		    &ipv6_all_ones,
15160Sstevel@tonic-gate 		    &ipv6_all_zeros,
15170Sstevel@tonic-gate 		    0,
15180Sstevel@tonic-gate 		    flags,
15198485SPeter.Memishian@Sun.COM 		    state,
15204714Ssowmini 		    &nce);
15210Sstevel@tonic-gate 		switch (err) {
15220Sstevel@tonic-gate 		case 0:
15230Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
15240Sstevel@tonic-gate 			    ill->ill_name));
15252546Scarlsonj 			ipif->ipif_addr_ready = 1;
15269287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
15279571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
15282546Scarlsonj 			break;
15292546Scarlsonj 		case EINPROGRESS:
15302546Scarlsonj 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
15312546Scarlsonj 			    ill->ill_name));
15329287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
15339571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
15340Sstevel@tonic-gate 			break;
15350Sstevel@tonic-gate 		case EEXIST:
15360Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
15370Sstevel@tonic-gate 			    ill->ill_name));
15389814SSowmini.Varadhan@Sun.COM 			if (!(nce->nce_flags & NCE_F_PERMANENT)) {
15399814SSowmini.Varadhan@Sun.COM 				ndp_delete(nce);
15409814SSowmini.Varadhan@Sun.COM 				NCE_REFRELE(nce);
15419814SSowmini.Varadhan@Sun.COM 				nce = NULL;
15429814SSowmini.Varadhan@Sun.COM 				goto retry;
15439814SSowmini.Varadhan@Sun.COM 			}
15449571SSowmini.Varadhan@Sun.COM 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
15459571SSowmini.Varadhan@Sun.COM 				NCE_REFRELE(nce);
15469571SSowmini.Varadhan@Sun.COM 				goto fail;
15479571SSowmini.Varadhan@Sun.COM 			}
15489571SSowmini.Varadhan@Sun.COM 			/*
15499571SSowmini.Varadhan@Sun.COM 			 * Duplicate local addresses are permissible for
15509571SSowmini.Varadhan@Sun.COM 			 * IPIF_POINTOPOINT interfaces which will get marked
15519571SSowmini.Varadhan@Sun.COM 			 * IPIF_UNNUMBERED later in
15529571SSowmini.Varadhan@Sun.COM 			 * ip_addr_availability_check().
15539571SSowmini.Varadhan@Sun.COM 			 *
15549571SSowmini.Varadhan@Sun.COM 			 * The nce_ipif_cnt field tracks the number of
15559571SSowmini.Varadhan@Sun.COM 			 * ipifs that have nce_addr as their local address.
15569571SSowmini.Varadhan@Sun.COM 			 */
15579571SSowmini.Varadhan@Sun.COM 			ipif->ipif_addr_ready = 1;
15589571SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
15599571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
15609571SSowmini.Varadhan@Sun.COM 			break;
15610Sstevel@tonic-gate 		default:
15628485SPeter.Memishian@Sun.COM 			ip1dbg(("ipif_ndp_up: NCE creation failed for %s\n",
15630Sstevel@tonic-gate 			    ill->ill_name));
15648485SPeter.Memishian@Sun.COM 			goto fail;
15650Sstevel@tonic-gate 		}
15662546Scarlsonj 	} else {
15672546Scarlsonj 		/* No local NCE for this entry */
15682546Scarlsonj 		ipif->ipif_addr_ready = 1;
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 	if (nce != NULL)
15710Sstevel@tonic-gate 		NCE_REFRELE(nce);
15720Sstevel@tonic-gate 	if (mnce != NULL)
15730Sstevel@tonic-gate 		NCE_REFRELE(mnce);
15740Sstevel@tonic-gate 	return (0);
15758485SPeter.Memishian@Sun.COM fail:
15768485SPeter.Memishian@Sun.COM 	if (mnce != NULL) {
15778485SPeter.Memishian@Sun.COM 		ndp_delete(mnce);
15788485SPeter.Memishian@Sun.COM 		NCE_REFRELE(mnce);
15798485SPeter.Memishian@Sun.COM 	}
15808485SPeter.Memishian@Sun.COM 	if (added_ipif)
15818485SPeter.Memishian@Sun.COM 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
15828485SPeter.Memishian@Sun.COM 
15838485SPeter.Memishian@Sun.COM 	return (err);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate /* Remove all cache entries for this logical interface */
15870Sstevel@tonic-gate void
15880Sstevel@tonic-gate ipif_ndp_down(ipif_t *ipif)
15890Sstevel@tonic-gate {
15900Sstevel@tonic-gate 	nce_t	*nce;
15918485SPeter.Memishian@Sun.COM 	ill_t	*ill = ipif->ipif_ill;
15928485SPeter.Memishian@Sun.COM 
15938485SPeter.Memishian@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
15940Sstevel@tonic-gate 
15952535Ssangeeta 	if (ipif->ipif_isv6) {
15969859SPeter.Memishian@Sun.COM 		if (ipif->ipif_added_nce) {
15979859SPeter.Memishian@Sun.COM 			/*
15989859SPeter.Memishian@Sun.COM 			 * For IPMP, `ill' can be the IPMP ill but the NCE will
15999859SPeter.Memishian@Sun.COM 			 * always be tied to an underlying IP interface, so we
16009859SPeter.Memishian@Sun.COM 			 * match across the illgrp.  This is safe since we
16019859SPeter.Memishian@Sun.COM 			 * ensure uniqueness across the group in ipif_ndp_up().
16029859SPeter.Memishian@Sun.COM 			 */
16039859SPeter.Memishian@Sun.COM 			nce = ndp_lookup_v6(ill, B_TRUE, &ipif->ipif_v6lcl_addr,
16048485SPeter.Memishian@Sun.COM 			    B_FALSE);
16059859SPeter.Memishian@Sun.COM 			if (nce != NULL) {
16069859SPeter.Memishian@Sun.COM 				if (--nce->nce_ipif_cnt == 0)
16079859SPeter.Memishian@Sun.COM 					ndp_delete(nce); /* last ipif for nce */
16089859SPeter.Memishian@Sun.COM 				NCE_REFRELE(nce);
16099859SPeter.Memishian@Sun.COM 			}
16109287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 0;
16112535Ssangeeta 		}
16129859SPeter.Memishian@Sun.COM 
16138485SPeter.Memishian@Sun.COM 		/*
16148485SPeter.Memishian@Sun.COM 		 * Make IPMP aware of the deleted data address.
16158485SPeter.Memishian@Sun.COM 		 */
16168485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill))
16178485SPeter.Memishian@Sun.COM 			ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
16180Sstevel@tonic-gate 	}
16198485SPeter.Memishian@Sun.COM 
16200Sstevel@tonic-gate 	/*
16210Sstevel@tonic-gate 	 * Remove mapping and all other nces dependent on this ill
16220Sstevel@tonic-gate 	 * when the last ipif is going away.
16230Sstevel@tonic-gate 	 */
16248485SPeter.Memishian@Sun.COM 	if (ill->ill_ipif_up_count == 0)
16258485SPeter.Memishian@Sun.COM 		ndp_walk(ill, (pfi_t)ndp_delete_per_ill, ill, ill->ill_ipst);
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate /*
16290Sstevel@tonic-gate  * Used when an interface comes up to recreate any extra routes on this
16300Sstevel@tonic-gate  * interface.
16310Sstevel@tonic-gate  */
16320Sstevel@tonic-gate static ire_t **
16330Sstevel@tonic-gate ipif_recover_ire_v6(ipif_t *ipif)
16340Sstevel@tonic-gate {
16350Sstevel@tonic-gate 	mblk_t	*mp;
16360Sstevel@tonic-gate 	ire_t   **ipif_saved_irep;
16370Sstevel@tonic-gate 	ire_t   **irep;
16383448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name,
16410Sstevel@tonic-gate 	    ipif->ipif_id));
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_saved_ire_lock);
16460Sstevel@tonic-gate 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
16470Sstevel@tonic-gate 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
16480Sstevel@tonic-gate 	if (ipif_saved_irep == NULL) {
16490Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
16500Sstevel@tonic-gate 		return (NULL);
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	irep = ipif_saved_irep;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
16560Sstevel@tonic-gate 		ire_t		*ire;
16570Sstevel@tonic-gate 		queue_t		*rfq;
16580Sstevel@tonic-gate 		queue_t		*stq;
16590Sstevel@tonic-gate 		ifrt_t		*ifrt;
16600Sstevel@tonic-gate 		in6_addr_t	*src_addr;
16610Sstevel@tonic-gate 		in6_addr_t	*gateway_addr;
16620Sstevel@tonic-gate 		char		buf[INET6_ADDRSTRLEN];
16630Sstevel@tonic-gate 		ushort_t	type;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 		/*
16660Sstevel@tonic-gate 		 * When the ire was initially created and then added in
16670Sstevel@tonic-gate 		 * ip_rt_add_v6(), it was created either using
16680Sstevel@tonic-gate 		 * ipif->ipif_net_type in the case of a traditional interface
16690Sstevel@tonic-gate 		 * route, or as one of the IRE_OFFSUBNET types (with the
16703004Sdd193516 		 * exception of IRE_HOST type redirect ire which is created by
16710Sstevel@tonic-gate 		 * icmp_redirect_v6() and which we don't need to save or
16720Sstevel@tonic-gate 		 * recover).  In the case where ipif->ipif_net_type was
16730Sstevel@tonic-gate 		 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to
16740Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy
16750Sstevel@tonic-gate 		 * software like GateD and Sun Cluster which creates routes
16760Sstevel@tonic-gate 		 * using the the loopback interface's address as a gateway.
16770Sstevel@tonic-gate 		 *
16784714Ssowmini 		 * As ifrt->ifrt_type reflects the already updated ire_type,
16794714Ssowmini 		 * ire_create_v6() will be called in the same way here as in
16804714Ssowmini 		 * ip_rt_add_v6(), namely using ipif->ipif_net_type when the
16814714Ssowmini 		 * route looks like a traditional interface route (where
16824714Ssowmini 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise
16834714Ssowmini 		 * using the saved ifrt->ifrt_type.  This means that in
16844714Ssowmini 		 * the case where ipif->ipif_net_type is IRE_LOOPBACK,
16854714Ssowmini 		 * the ire created by ire_create_v6() will be an IRE_LOOPBACK,
16864714Ssowmini 		 * it will then be turned into an IRE_IF_NORESOLVER and then
16874714Ssowmini 		 * added by ire_add_v6().
16880Sstevel@tonic-gate 		 */
16890Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
16900Sstevel@tonic-gate 		if (ifrt->ifrt_type & IRE_INTERFACE) {
16910Sstevel@tonic-gate 			rfq = NULL;
16920Sstevel@tonic-gate 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
16930Sstevel@tonic-gate 			    ? ipif->ipif_rq : ipif->ipif_wq;
16940Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
16950Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr
16960Sstevel@tonic-gate 			    : &ipif->ipif_v6src_addr;
16970Sstevel@tonic-gate 			gateway_addr = NULL;
16980Sstevel@tonic-gate 			type = ipif->ipif_net_type;
16990Sstevel@tonic-gate 		} else {
17000Sstevel@tonic-gate 			rfq = NULL;
17010Sstevel@tonic-gate 			stq = NULL;
17020Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
17030Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr : NULL;
17040Sstevel@tonic-gate 			gateway_addr = &ifrt->ifrt_v6gateway_addr;
17050Sstevel@tonic-gate 			type = ifrt->ifrt_type;
17060Sstevel@tonic-gate 		}
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 		/*
17090Sstevel@tonic-gate 		 * Create a copy of the IRE with the saved address and netmask.
17100Sstevel@tonic-gate 		 */
17110Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n",
17120Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
17130Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)),
17140Sstevel@tonic-gate 		    ip_mask_to_plen_v6(&ifrt->ifrt_v6mask)));
17150Sstevel@tonic-gate 		ire = ire_create_v6(
17160Sstevel@tonic-gate 		    &ifrt->ifrt_v6addr,
17170Sstevel@tonic-gate 		    &ifrt->ifrt_v6mask,
17180Sstevel@tonic-gate 		    src_addr,
17190Sstevel@tonic-gate 		    gateway_addr,
17200Sstevel@tonic-gate 		    &ifrt->ifrt_max_frag,
17210Sstevel@tonic-gate 		    NULL,
17220Sstevel@tonic-gate 		    rfq,
17230Sstevel@tonic-gate 		    stq,
17240Sstevel@tonic-gate 		    type,
17250Sstevel@tonic-gate 		    ipif,
17260Sstevel@tonic-gate 		    NULL,
17270Sstevel@tonic-gate 		    0,
17280Sstevel@tonic-gate 		    0,
17290Sstevel@tonic-gate 		    ifrt->ifrt_flags,
17301676Sjpk 		    &ifrt->ifrt_iulp_info,
17311676Sjpk 		    NULL,
17323448Sdh155122 		    NULL,
17333448Sdh155122 		    ipst);
17340Sstevel@tonic-gate 		if (ire == NULL) {
17350Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
17360Sstevel@tonic-gate 			kmem_free(ipif_saved_irep,
17370Sstevel@tonic-gate 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
17380Sstevel@tonic-gate 			return (NULL);
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 		/*
17420Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
17430Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
17440Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
17450Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
17460Sstevel@tonic-gate 		 * when generating aggregate routes.)
17470Sstevel@tonic-gate 		 *
17480Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
17490Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
17500Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
17510Sstevel@tonic-gate 		 */
17520Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
17530Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
17540Sstevel@tonic-gate 		/*
17550Sstevel@tonic-gate 		 * ire held by ire_add, will be refreled' in ipif_up_done
17560Sstevel@tonic-gate 		 * towards the end
17570Sstevel@tonic-gate 		 */
17582535Ssangeeta 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
17590Sstevel@tonic-gate 		*irep = ire;
17600Sstevel@tonic-gate 		irep++;
17610Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire));
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_saved_ire_lock);
17640Sstevel@tonic-gate 	return (ipif_saved_irep);
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate /*
17680Sstevel@tonic-gate  * Return the scope of the given IPv6 address.  If the address is an
17690Sstevel@tonic-gate  * IPv4 mapped IPv6 address, return the scope of the corresponding
17700Sstevel@tonic-gate  * IPv4 address.
17710Sstevel@tonic-gate  */
17720Sstevel@tonic-gate in6addr_scope_t
17730Sstevel@tonic-gate ip_addr_scope_v6(const in6_addr_t *addr)
17740Sstevel@tonic-gate {
17750Sstevel@tonic-gate 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
17780Sstevel@tonic-gate 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
17790Sstevel@tonic-gate 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
17800Sstevel@tonic-gate 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
17810Sstevel@tonic-gate 			return (IP6_SCOPE_LINKLOCAL);
17820Sstevel@tonic-gate 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
17830Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
17840Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
17850Sstevel@tonic-gate 			return (IP6_SCOPE_SITELOCAL);
17860Sstevel@tonic-gate 		return (IP6_SCOPE_GLOBAL);
17870Sstevel@tonic-gate 	}
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	if (IN6_IS_ADDR_MULTICAST(addr))
17900Sstevel@tonic-gate 		return (IN6_ADDR_MC_SCOPE(addr));
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	/* link-local and loopback addresses are of link-local scope */
17930Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
17940Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
17950Sstevel@tonic-gate 		return (IP6_SCOPE_LINKLOCAL);
17960Sstevel@tonic-gate 	if (IN6_IS_ADDR_SITELOCAL(addr))
17970Sstevel@tonic-gate 		return (IP6_SCOPE_SITELOCAL);
17980Sstevel@tonic-gate 	return (IP6_SCOPE_GLOBAL);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate /*
18033431Scarlsonj  * Returns the length of the common prefix of a1 and a2, as per
18043431Scarlsonj  * CommonPrefixLen() defined in RFC 3484.
18050Sstevel@tonic-gate  */
18063431Scarlsonj static int
18073431Scarlsonj ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2)
18080Sstevel@tonic-gate {
18090Sstevel@tonic-gate 	int i;
18103431Scarlsonj 	uint32_t a1val, a2val, mask;
18110Sstevel@tonic-gate 
18123431Scarlsonj 	for (i = 0; i < 4; i++) {
18133431Scarlsonj 		if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) {
18143431Scarlsonj 			a1val ^= a2val;
18153431Scarlsonj 			i *= 32;
18163431Scarlsonj 			mask = 0x80000000u;
18173431Scarlsonj 			while (!(a1val & mask)) {
18183431Scarlsonj 				mask >>= 1;
18193431Scarlsonj 				i++;
18203431Scarlsonj 			}
18213431Scarlsonj 			return (i);
18223431Scarlsonj 		}
18233431Scarlsonj 	}
18243431Scarlsonj 	return (IPV6_ABITS);
18250Sstevel@tonic-gate }
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate #define	IPIF_VALID_IPV6_SOURCE(ipif) \
18280Sstevel@tonic-gate 	(((ipif)->ipif_flags & IPIF_UP) && \
18292546Scarlsonj 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
18302546Scarlsonj 	(ipif)->ipif_addr_ready)
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate /* source address candidate */
18330Sstevel@tonic-gate typedef struct candidate {
18340Sstevel@tonic-gate 	ipif_t		*cand_ipif;
18350Sstevel@tonic-gate 	/* The properties of this candidate */
18360Sstevel@tonic-gate 	boolean_t	cand_isdst;
18370Sstevel@tonic-gate 	boolean_t	cand_isdst_set;
18380Sstevel@tonic-gate 	in6addr_scope_t	cand_scope;
18390Sstevel@tonic-gate 	boolean_t	cand_scope_set;
18400Sstevel@tonic-gate 	boolean_t	cand_isdeprecated;
18410Sstevel@tonic-gate 	boolean_t	cand_isdeprecated_set;
18420Sstevel@tonic-gate 	boolean_t	cand_ispreferred;
18430Sstevel@tonic-gate 	boolean_t	cand_ispreferred_set;
18440Sstevel@tonic-gate 	boolean_t	cand_matchedinterface;
18450Sstevel@tonic-gate 	boolean_t	cand_matchedinterface_set;
18460Sstevel@tonic-gate 	boolean_t	cand_matchedlabel;
18470Sstevel@tonic-gate 	boolean_t	cand_matchedlabel_set;
18480Sstevel@tonic-gate 	boolean_t	cand_istmp;
18490Sstevel@tonic-gate 	boolean_t	cand_istmp_set;
18503431Scarlsonj 	int		cand_common_pref;
18513431Scarlsonj 	boolean_t	cand_common_pref_set;
18523431Scarlsonj 	boolean_t	cand_pref_eq;
18533431Scarlsonj 	boolean_t	cand_pref_eq_set;
18543431Scarlsonj 	int		cand_pref_len;
18553431Scarlsonj 	boolean_t	cand_pref_len_set;
18560Sstevel@tonic-gate } cand_t;
18570Sstevel@tonic-gate #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
18583431Scarlsonj #define	cand_mask	cand_ipif->ipif_v6net_mask
18590Sstevel@tonic-gate #define	cand_flags	cand_ipif->ipif_flags
18600Sstevel@tonic-gate #define	cand_ill	cand_ipif->ipif_ill
18611676Sjpk #define	cand_zoneid	cand_ipif->ipif_zoneid
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate /* information about the destination for source address selection */
18640Sstevel@tonic-gate typedef struct dstinfo {
18650Sstevel@tonic-gate 	const in6_addr_t	*dst_addr;
18660Sstevel@tonic-gate 	ill_t			*dst_ill;
18672202Srk129064 	uint_t			dst_restrict_ill;
18680Sstevel@tonic-gate 	boolean_t		dst_prefer_src_tmp;
18690Sstevel@tonic-gate 	in6addr_scope_t		dst_scope;
18700Sstevel@tonic-gate 	char			*dst_label;
18710Sstevel@tonic-gate } dstinfo_t;
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate /*
18740Sstevel@tonic-gate  * The following functions are rules used to select a source address in
18750Sstevel@tonic-gate  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
18760Sstevel@tonic-gate  * against the best candidate (bc).  Each rule has three possible outcomes;
18770Sstevel@tonic-gate  * the candidate is preferred over the best candidate (CAND_PREFER), the
18780Sstevel@tonic-gate  * candidate is not preferred over the best candidate (CAND_AVOID), or the
18790Sstevel@tonic-gate  * candidate is of equal value as the best candidate (CAND_TIE).
18800Sstevel@tonic-gate  *
18810Sstevel@tonic-gate  * These rules are part of a greater "Default Address Selection for IPv6"
18820Sstevel@tonic-gate  * sheme, which is standards based work coming out of the IETF ipv6 working
18830Sstevel@tonic-gate  * group.  The IETF document defines both IPv6 source address selection and
18840Sstevel@tonic-gate  * destination address ordering.  The rules defined here implement the IPv6
18850Sstevel@tonic-gate  * source address selection.  Destination address ordering is done by
18860Sstevel@tonic-gate  * libnsl, and uses a similar set of rules to implement the sorting.
18873431Scarlsonj  *
18883431Scarlsonj  * Most of the rules are defined by the RFC and are not typically altered.  The
18893431Scarlsonj  * last rule, number 8, has language that allows for local preferences.  In the
18903431Scarlsonj  * scheme below, this means that new Solaris rules should normally go between
18913431Scarlsonj  * rule_ifprefix and rule_prefix.
18920Sstevel@tonic-gate  */
18930Sstevel@tonic-gate typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
18943448Sdh155122 typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *,
18953448Sdh155122     ip_stack_t *);
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate /* Prefer an address if it is equal to the destination address. */
18983448Sdh155122 /* ARGSUSED3 */
18990Sstevel@tonic-gate static rule_res_t
19003448Sdh155122 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
19010Sstevel@tonic-gate {
19020Sstevel@tonic-gate 	if (!bc->cand_isdst_set) {
19030Sstevel@tonic-gate 		bc->cand_isdst =
19040Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
19050Sstevel@tonic-gate 		bc->cand_isdst_set = B_TRUE;
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	cc->cand_isdst =
19090Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
19100Sstevel@tonic-gate 	cc->cand_isdst_set = B_TRUE;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	if (cc->cand_isdst == bc->cand_isdst)
19130Sstevel@tonic-gate 		return (CAND_TIE);
19140Sstevel@tonic-gate 	else if (cc->cand_isdst)
19150Sstevel@tonic-gate 		return (CAND_PREFER);
19160Sstevel@tonic-gate 	else
19170Sstevel@tonic-gate 		return (CAND_AVOID);
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate /*
19210Sstevel@tonic-gate  * Prefer addresses that are of closest scope to the destination.  Always
19220Sstevel@tonic-gate  * prefer addresses that are of greater scope than the destination over
19230Sstevel@tonic-gate  * those that are of lesser scope than the destination.
19240Sstevel@tonic-gate  */
19253448Sdh155122 /* ARGSUSED3 */
19260Sstevel@tonic-gate static rule_res_t
19273448Sdh155122 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
19280Sstevel@tonic-gate {
19290Sstevel@tonic-gate 	if (!bc->cand_scope_set) {
19300Sstevel@tonic-gate 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
19310Sstevel@tonic-gate 		bc->cand_scope_set = B_TRUE;
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
19350Sstevel@tonic-gate 	cc->cand_scope_set = B_TRUE;
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	if (cc->cand_scope < bc->cand_scope) {
19380Sstevel@tonic-gate 		if (cc->cand_scope < dstinfo->dst_scope)
19390Sstevel@tonic-gate 			return (CAND_AVOID);
19400Sstevel@tonic-gate 		else
19410Sstevel@tonic-gate 			return (CAND_PREFER);
19420Sstevel@tonic-gate 	} else if (bc->cand_scope < cc->cand_scope) {
19430Sstevel@tonic-gate 		if (bc->cand_scope < dstinfo->dst_scope)
19440Sstevel@tonic-gate 			return (CAND_PREFER);
19450Sstevel@tonic-gate 		else
19460Sstevel@tonic-gate 			return (CAND_AVOID);
19470Sstevel@tonic-gate 	} else {
19480Sstevel@tonic-gate 		return (CAND_TIE);
19490Sstevel@tonic-gate 	}
19500Sstevel@tonic-gate }
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate /*
19530Sstevel@tonic-gate  * Prefer non-deprecated source addresses.
19540Sstevel@tonic-gate  */
19550Sstevel@tonic-gate /* ARGSUSED2 */
19560Sstevel@tonic-gate static rule_res_t
19573448Sdh155122 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
19583448Sdh155122     ip_stack_t *ipst)
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	if (!bc->cand_isdeprecated_set) {
19610Sstevel@tonic-gate 		bc->cand_isdeprecated =
19620Sstevel@tonic-gate 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
19630Sstevel@tonic-gate 		bc->cand_isdeprecated_set = B_TRUE;
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
19670Sstevel@tonic-gate 	cc->cand_isdeprecated_set = B_TRUE;
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
19700Sstevel@tonic-gate 		return (CAND_TIE);
19710Sstevel@tonic-gate 	else if (cc->cand_isdeprecated)
19720Sstevel@tonic-gate 		return (CAND_AVOID);
19730Sstevel@tonic-gate 	else
19740Sstevel@tonic-gate 		return (CAND_PREFER);
19750Sstevel@tonic-gate }
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate /*
19780Sstevel@tonic-gate  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
19790Sstevel@tonic-gate  * rule must be before rule_interface because the flag could be set on any
19800Sstevel@tonic-gate  * interface, not just the interface being used for outgoing packets (for
19810Sstevel@tonic-gate  * example, the IFF_PREFERRED could be set on an address assigned to the
19820Sstevel@tonic-gate  * loopback interface).
19830Sstevel@tonic-gate  */
19840Sstevel@tonic-gate /* ARGSUSED2 */
19850Sstevel@tonic-gate static rule_res_t
19863448Sdh155122 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
19873448Sdh155122     ip_stack_t *ipst)
19880Sstevel@tonic-gate {
19890Sstevel@tonic-gate 	if (!bc->cand_ispreferred_set) {
19900Sstevel@tonic-gate 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
19910Sstevel@tonic-gate 		bc->cand_ispreferred_set = B_TRUE;
19920Sstevel@tonic-gate 	}
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
19950Sstevel@tonic-gate 	cc->cand_ispreferred_set = B_TRUE;
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if (bc->cand_ispreferred == cc->cand_ispreferred)
19980Sstevel@tonic-gate 		return (CAND_TIE);
19990Sstevel@tonic-gate 	else if (cc->cand_ispreferred)
20000Sstevel@tonic-gate 		return (CAND_PREFER);
20010Sstevel@tonic-gate 	else
20020Sstevel@tonic-gate 		return (CAND_AVOID);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate /*
20068485SPeter.Memishian@Sun.COM  * Prefer source addresses that are assigned to the outgoing interface.
20070Sstevel@tonic-gate  */
20083448Sdh155122 /* ARGSUSED3 */
20090Sstevel@tonic-gate static rule_res_t
20103448Sdh155122 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20113448Sdh155122     ip_stack_t *ipst)
20120Sstevel@tonic-gate {
20130Sstevel@tonic-gate 	ill_t *dstill = dstinfo->dst_ill;
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	/*
20160Sstevel@tonic-gate 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
20170Sstevel@tonic-gate 	 * since we know all candidates will be on the same link.
20180Sstevel@tonic-gate 	 */
20190Sstevel@tonic-gate 	if (dstinfo->dst_restrict_ill)
20200Sstevel@tonic-gate 		return (CAND_TIE);
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	if (!bc->cand_matchedinterface_set) {
20238485SPeter.Memishian@Sun.COM 		bc->cand_matchedinterface = bc->cand_ill == dstill;
20240Sstevel@tonic-gate 		bc->cand_matchedinterface_set = B_TRUE;
20250Sstevel@tonic-gate 	}
20260Sstevel@tonic-gate 
20278485SPeter.Memishian@Sun.COM 	cc->cand_matchedinterface = cc->cand_ill == dstill;
20280Sstevel@tonic-gate 	cc->cand_matchedinterface_set = B_TRUE;
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
20310Sstevel@tonic-gate 		return (CAND_TIE);
20320Sstevel@tonic-gate 	else if (cc->cand_matchedinterface)
20330Sstevel@tonic-gate 		return (CAND_PREFER);
20340Sstevel@tonic-gate 	else
20350Sstevel@tonic-gate 		return (CAND_AVOID);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate /*
20390Sstevel@tonic-gate  * Prefer source addresses whose label matches the destination's label.
20400Sstevel@tonic-gate  */
20410Sstevel@tonic-gate static rule_res_t
20423448Sdh155122 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
20430Sstevel@tonic-gate {
20440Sstevel@tonic-gate 	char *label;
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	if (!bc->cand_matchedlabel_set) {
20473448Sdh155122 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst);
20480Sstevel@tonic-gate 		bc->cand_matchedlabel =
20490Sstevel@tonic-gate 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
20500Sstevel@tonic-gate 		bc->cand_matchedlabel_set = B_TRUE;
20510Sstevel@tonic-gate 	}
20520Sstevel@tonic-gate 
20533448Sdh155122 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst);
20540Sstevel@tonic-gate 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
20550Sstevel@tonic-gate 	cc->cand_matchedlabel_set = B_TRUE;
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
20580Sstevel@tonic-gate 		return (CAND_TIE);
20590Sstevel@tonic-gate 	else if (cc->cand_matchedlabel)
20600Sstevel@tonic-gate 		return (CAND_PREFER);
20610Sstevel@tonic-gate 	else
20620Sstevel@tonic-gate 		return (CAND_AVOID);
20630Sstevel@tonic-gate }
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate /*
20660Sstevel@tonic-gate  * Prefer public addresses over temporary ones.  An application can reverse
20670Sstevel@tonic-gate  * the logic of this rule and prefer temporary addresses by using the
20680Sstevel@tonic-gate  * IPV6_SRC_PREFERENCES socket option.
20690Sstevel@tonic-gate  */
20703448Sdh155122 /* ARGSUSED3 */
20710Sstevel@tonic-gate static rule_res_t
20723448Sdh155122 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
20733448Sdh155122     ip_stack_t *ipst)
20740Sstevel@tonic-gate {
20750Sstevel@tonic-gate 	if (!bc->cand_istmp_set) {
20760Sstevel@tonic-gate 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
20770Sstevel@tonic-gate 		bc->cand_istmp_set = B_TRUE;
20780Sstevel@tonic-gate 	}
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
20810Sstevel@tonic-gate 	cc->cand_istmp_set = B_TRUE;
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	if (bc->cand_istmp == cc->cand_istmp)
20840Sstevel@tonic-gate 		return (CAND_TIE);
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
20870Sstevel@tonic-gate 		return (CAND_PREFER);
20880Sstevel@tonic-gate 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
20890Sstevel@tonic-gate 		return (CAND_PREFER);
20900Sstevel@tonic-gate 	else
20910Sstevel@tonic-gate 		return (CAND_AVOID);
20920Sstevel@tonic-gate }
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate /*
20953431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination
20963431Scarlsonj  * under the interface mask.  This gets us on the same subnet before applying
20973431Scarlsonj  * any Solaris-specific rules.
20980Sstevel@tonic-gate  */
20993448Sdh155122 /* ARGSUSED3 */
21000Sstevel@tonic-gate static rule_res_t
21013448Sdh155122 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
21023448Sdh155122     ip_stack_t *ipst)
21030Sstevel@tonic-gate {
21043431Scarlsonj 	if (!bc->cand_pref_eq_set) {
21053431Scarlsonj 		bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr,
21063431Scarlsonj 		    bc->cand_mask, *dstinfo->dst_addr);
21073431Scarlsonj 		bc->cand_pref_eq_set = B_TRUE;
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate 
21103431Scarlsonj 	cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask,
21113431Scarlsonj 	    *dstinfo->dst_addr);
21123431Scarlsonj 	cc->cand_pref_eq_set = B_TRUE;
21130Sstevel@tonic-gate 
21143431Scarlsonj 	if (bc->cand_pref_eq) {
21153431Scarlsonj 		if (cc->cand_pref_eq) {
21163431Scarlsonj 			if (!bc->cand_pref_len_set) {
21173431Scarlsonj 				bc->cand_pref_len =
21183431Scarlsonj 				    ip_mask_to_plen_v6(&bc->cand_mask);
21193431Scarlsonj 				bc->cand_pref_len_set = B_TRUE;
21203431Scarlsonj 			}
21213431Scarlsonj 			cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask);
21223431Scarlsonj 			cc->cand_pref_len_set = B_TRUE;
21233431Scarlsonj 			if (bc->cand_pref_len == cc->cand_pref_len)
21243431Scarlsonj 				return (CAND_TIE);
21253431Scarlsonj 			else if (bc->cand_pref_len > cc->cand_pref_len)
21263431Scarlsonj 				return (CAND_AVOID);
21273431Scarlsonj 			else
21283431Scarlsonj 				return (CAND_PREFER);
21293431Scarlsonj 		} else {
21303431Scarlsonj 			return (CAND_AVOID);
21313431Scarlsonj 		}
21323431Scarlsonj 	} else {
21333431Scarlsonj 		if (cc->cand_pref_eq)
21343431Scarlsonj 			return (CAND_PREFER);
21353431Scarlsonj 		else
21363431Scarlsonj 			return (CAND_TIE);
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate }
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate /*
21411676Sjpk  * Prefer to use zone-specific addresses when possible instead of all-zones
21421676Sjpk  * addresses.
21431676Sjpk  */
21441676Sjpk /* ARGSUSED2 */
21451676Sjpk static rule_res_t
21463448Sdh155122 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
21473448Sdh155122     ip_stack_t *ipst)
21481676Sjpk {
21491676Sjpk 	if ((bc->cand_zoneid == ALL_ZONES) ==
21501676Sjpk 	    (cc->cand_zoneid == ALL_ZONES))
21511676Sjpk 		return (CAND_TIE);
21521676Sjpk 	else if (cc->cand_zoneid == ALL_ZONES)
21531676Sjpk 		return (CAND_AVOID);
21541676Sjpk 	else
21551676Sjpk 		return (CAND_PREFER);
21561676Sjpk }
21571676Sjpk 
21581676Sjpk /*
21593431Scarlsonj  * Prefer to use DHCPv6 (first) and static addresses (second) when possible
21603431Scarlsonj  * instead of statelessly autoconfigured addresses.
21613431Scarlsonj  *
21623431Scarlsonj  * This is done after trying all other preferences (and before the final tie
21633431Scarlsonj  * breaker) so that, if all else is equal, we select addresses configured by
21643431Scarlsonj  * DHCPv6 over other addresses.  We presume that DHCPv6 addresses, unlike
21653431Scarlsonj  * stateless autoconfigured addresses, are deliberately configured by an
21663431Scarlsonj  * administrator, and thus are correctly set up in DNS and network packet
21673431Scarlsonj  * filters.
21683431Scarlsonj  */
21693431Scarlsonj /* ARGSUSED2 */
21703431Scarlsonj static rule_res_t
21713448Sdh155122 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
21723448Sdh155122     ip_stack_t *ipst)
21733431Scarlsonj {
21743431Scarlsonj #define	ATYPE(x)	\
21753431Scarlsonj 	((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2
21763431Scarlsonj 	int bcval = ATYPE(bc->cand_flags);
21773431Scarlsonj 	int ccval = ATYPE(cc->cand_flags);
21783431Scarlsonj #undef ATYPE
21793431Scarlsonj 
21803431Scarlsonj 	if (bcval == ccval)
21813431Scarlsonj 		return (CAND_TIE);
21823431Scarlsonj 	else if (ccval < bcval)
21833431Scarlsonj 		return (CAND_PREFER);
21843431Scarlsonj 	else
21853431Scarlsonj 		return (CAND_AVOID);
21863431Scarlsonj }
21873431Scarlsonj 
21883431Scarlsonj /*
21893431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination.
21903431Scarlsonj  * We do the longest matching prefix calculation by doing an xor of both
21913431Scarlsonj  * addresses with the destination, and pick the address with the longest string
21923431Scarlsonj  * of leading zeros, as per CommonPrefixLen() defined in RFC 3484.
21933431Scarlsonj  */
21943448Sdh155122 /* ARGSUSED3 */
21953431Scarlsonj static rule_res_t
21963448Sdh155122 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
21973431Scarlsonj {
21988485SPeter.Memishian@Sun.COM 	/*
21998485SPeter.Memishian@Sun.COM 	 * For IPMP, we always want to choose a random source address from
22008485SPeter.Memishian@Sun.COM 	 * among any equally usable addresses, so always report a tie.
22018485SPeter.Memishian@Sun.COM 	 */
22028485SPeter.Memishian@Sun.COM 	if (IS_IPMP(dstinfo->dst_ill))
22038485SPeter.Memishian@Sun.COM 		return (CAND_TIE);
22048485SPeter.Memishian@Sun.COM 
22053431Scarlsonj 	if (!bc->cand_common_pref_set) {
22063431Scarlsonj 		bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr,
22073431Scarlsonj 		    dstinfo->dst_addr);
22083431Scarlsonj 		bc->cand_common_pref_set = B_TRUE;
22093431Scarlsonj 	}
22103431Scarlsonj 
22113431Scarlsonj 	cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr,
22123431Scarlsonj 	    dstinfo->dst_addr);
22133431Scarlsonj 	cc->cand_common_pref_set = B_TRUE;
22143431Scarlsonj 
22153431Scarlsonj 	if (bc->cand_common_pref == cc->cand_common_pref)
22163431Scarlsonj 		return (CAND_TIE);
22173431Scarlsonj 	else if (bc->cand_common_pref > cc->cand_common_pref)
22183431Scarlsonj 		return (CAND_AVOID);
22193431Scarlsonj 	else
22203431Scarlsonj 		return (CAND_PREFER);
22213431Scarlsonj }
22223431Scarlsonj 
22233431Scarlsonj /*
22243431Scarlsonj  * Last rule: we must pick something, so just prefer the current best
22253431Scarlsonj  * candidate.
22263431Scarlsonj  */
22273431Scarlsonj /* ARGSUSED */
22283431Scarlsonj static rule_res_t
22293448Sdh155122 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
22303448Sdh155122     ip_stack_t *ipst)
22313431Scarlsonj {
22323431Scarlsonj 	return (CAND_AVOID);
22333431Scarlsonj }
22343431Scarlsonj 
22353431Scarlsonj /*
22360Sstevel@tonic-gate  * Determine the best source address given a destination address and a
22370Sstevel@tonic-gate  * destination ill.  If no suitable source address is found, it returns
22380Sstevel@tonic-gate  * NULL. If there is a usable address pointed to by the usesrc
22390Sstevel@tonic-gate  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
22400Sstevel@tonic-gate  * fine grained (i.e per interface)
22410Sstevel@tonic-gate  *
22420Sstevel@tonic-gate  * This implementation is based on the "Default Address Selection for IPv6"
22430Sstevel@tonic-gate  * specification produced by the IETF IPv6 working group.  It has been
22440Sstevel@tonic-gate  * implemented so that the list of addresses is only traversed once (the
22450Sstevel@tonic-gate  * specification's algorithm could traverse the list of addresses once for
22460Sstevel@tonic-gate  * every rule).
22470Sstevel@tonic-gate  *
22488485SPeter.Memishian@Sun.COM  * The restrict_ill argument restricts the algorithm to choose a source
22498485SPeter.Memishian@Sun.COM  * address that is assigned to the destination ill.  This is used when
22508485SPeter.Memishian@Sun.COM  * the destination address is a link-local or multicast address, and when
22510Sstevel@tonic-gate  * ipv6_strict_dst_multihoming is turned on.
22520Sstevel@tonic-gate  *
22530Sstevel@tonic-gate  * src_prefs is the caller's set of source address preferences.  If source
22540Sstevel@tonic-gate  * address selection is being called to determine the source address of a
22550Sstevel@tonic-gate  * connected socket (from ip_bind_connected_v6()), then the preferences are
22560Sstevel@tonic-gate  * taken from conn_src_preferences.  These preferences can be set on a
22570Sstevel@tonic-gate  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
22580Sstevel@tonic-gate  * preference currently implemented is for rfc3041 temporary addresses.
22590Sstevel@tonic-gate  */
22600Sstevel@tonic-gate ipif_t *
22610Sstevel@tonic-gate ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
22628485SPeter.Memishian@Sun.COM     boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	dstinfo_t	dstinfo;
22650Sstevel@tonic-gate 	char		dstr[INET6_ADDRSTRLEN];
22660Sstevel@tonic-gate 	char		sstr[INET6_ADDRSTRLEN];
22678485SPeter.Memishian@Sun.COM 	ipif_t		*ipif, *start_ipif, *next_ipif;
22688485SPeter.Memishian@Sun.COM 	ill_t		*ill, *usesrc_ill = NULL, *ipmp_ill = NULL;
22690Sstevel@tonic-gate 	ill_walk_context_t	ctx;
22700Sstevel@tonic-gate 	cand_t		best_c;	/* The best candidate */
22710Sstevel@tonic-gate 	cand_t		curr_c;	/* The current candidate */
22720Sstevel@tonic-gate 	uint_t		index;
22730Sstevel@tonic-gate 	boolean_t	first_candidate = B_TRUE;
22740Sstevel@tonic-gate 	rule_res_t	rule_result;
22751676Sjpk 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
22763448Sdh155122 	ip_stack_t	*ipst = dstill->ill_ipst;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	/*
22790Sstevel@tonic-gate 	 * The list of ordering rules.  They are applied in the order they
22800Sstevel@tonic-gate 	 * appear in the list.
22810Sstevel@tonic-gate 	 *
22823431Scarlsonj 	 * Solaris doesn't currently support Mobile IPv6, so there's no
22833431Scarlsonj 	 * rule_mipv6 corresponding to rule 4 in the specification.
22840Sstevel@tonic-gate 	 */
22850Sstevel@tonic-gate 	rulef_t	rules[] = {
22860Sstevel@tonic-gate 		rule_isdst,
22870Sstevel@tonic-gate 		rule_scope,
22880Sstevel@tonic-gate 		rule_deprecated,
22890Sstevel@tonic-gate 		rule_preferred,
22900Sstevel@tonic-gate 		rule_interface,
22910Sstevel@tonic-gate 		rule_label,
22920Sstevel@tonic-gate 		rule_temporary,
22933431Scarlsonj 		rule_ifprefix,			/* local rules after this */
22941676Sjpk 		rule_zone_specific,
22953431Scarlsonj 		rule_addr_type,
22963431Scarlsonj 		rule_prefix,			/* local rules before this */
22973431Scarlsonj 		rule_must_be_last,		/* must always be last */
22980Sstevel@tonic-gate 		NULL
22990Sstevel@tonic-gate 	};
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	ASSERT(dstill->ill_isv6);
23020Sstevel@tonic-gate 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	/*
23050Sstevel@tonic-gate 	 * Check if there is a usable src address pointed to by the
23060Sstevel@tonic-gate 	 * usesrc ifindex. This has higher precedence since it is
23070Sstevel@tonic-gate 	 * finer grained (i.e per interface) v/s being system wide.
23080Sstevel@tonic-gate 	 */
23090Sstevel@tonic-gate 	if (dstill->ill_usesrc_ifindex != 0) {
23100Sstevel@tonic-gate 		if ((usesrc_ill =
23110Sstevel@tonic-gate 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
23123448Sdh155122 		    NULL, NULL, NULL, NULL, ipst)) != NULL) {
23130Sstevel@tonic-gate 			dstinfo.dst_ill = usesrc_ill;
23140Sstevel@tonic-gate 		} else {
23150Sstevel@tonic-gate 			return (NULL);
23160Sstevel@tonic-gate 		}
23178485SPeter.Memishian@Sun.COM 	} else if (IS_UNDER_IPMP(dstill)) {
23188485SPeter.Memishian@Sun.COM 		/*
23198485SPeter.Memishian@Sun.COM 		 * Test addresses should never be used for source address
23208485SPeter.Memishian@Sun.COM 		 * selection, so if we were passed an underlying ill, switch
23218485SPeter.Memishian@Sun.COM 		 * to the IPMP meta-interface.
23228485SPeter.Memishian@Sun.COM 		 */
23238485SPeter.Memishian@Sun.COM 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(dstill)) != NULL)
23248485SPeter.Memishian@Sun.COM 			dstinfo.dst_ill = ipmp_ill;
23258485SPeter.Memishian@Sun.COM 		else
23268485SPeter.Memishian@Sun.COM 			return (NULL);
23270Sstevel@tonic-gate 	} else {
23280Sstevel@tonic-gate 		dstinfo.dst_ill = dstill;
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 
23311676Sjpk 	/*
23321676Sjpk 	 * If we're dealing with an unlabeled destination on a labeled system,
23331676Sjpk 	 * make sure that we ignore source addresses that are incompatible with
23341676Sjpk 	 * the destination's default label.  That destination's default label
23351676Sjpk 	 * must dominate the minimum label on the source address.
23361676Sjpk 	 *
23371676Sjpk 	 * (Note that this has to do with Trusted Solaris.  It's not related to
23381676Sjpk 	 * the labels described by ip6_asp_lookup.)
23391676Sjpk 	 */
23401676Sjpk 	dst_rhtp = NULL;
23411676Sjpk 	if (is_system_labeled()) {
23421676Sjpk 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
23431676Sjpk 		if (dst_rhtp == NULL)
23441676Sjpk 			return (NULL);
23451676Sjpk 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
23461676Sjpk 			TPC_RELE(dst_rhtp);
23471676Sjpk 			dst_rhtp = NULL;
23481676Sjpk 		}
23491676Sjpk 	}
23501676Sjpk 
23510Sstevel@tonic-gate 	dstinfo.dst_addr = dst;
23520Sstevel@tonic-gate 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
23533448Sdh155122 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst);
23540Sstevel@tonic-gate 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
23553448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23560Sstevel@tonic-gate 	/*
23570Sstevel@tonic-gate 	 * Section three of the I-D states that for multicast and
23580Sstevel@tonic-gate 	 * link-local destinations, the candidate set must be restricted to
23590Sstevel@tonic-gate 	 * an interface that is on the same link as the outgoing interface.
23600Sstevel@tonic-gate 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
23610Sstevel@tonic-gate 	 * restrict the source address to the destination link as doing
23620Sstevel@tonic-gate 	 * otherwise will almost certainly cause problems.
23630Sstevel@tonic-gate 	 */
23640Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
23653448Sdh155122 	    ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
23668485SPeter.Memishian@Sun.COM 		dstinfo.dst_restrict_ill = B_TRUE;
23672202Srk129064 	} else {
23680Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = restrict_ill;
23692202Srk129064 	}
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	bzero(&best_c, sizeof (cand_t));
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	/*
23748485SPeter.Memishian@Sun.COM 	 * Take a pass through the list of IPv6 interfaces to choose the best
23758485SPeter.Memishian@Sun.COM 	 * possible source address.  If restrict_ill is set, just use dst_ill.
23760Sstevel@tonic-gate 	 */
23778485SPeter.Memishian@Sun.COM 	if (dstinfo.dst_restrict_ill)
23788485SPeter.Memishian@Sun.COM 		ill = dstinfo.dst_ill;
23798485SPeter.Memishian@Sun.COM 	else
23803448Sdh155122 		ill = ILL_START_WALK_V6(&ctx, ipst);
23818485SPeter.Memishian@Sun.COM 
23828485SPeter.Memishian@Sun.COM 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23830Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
23840Sstevel@tonic-gate 
23852202Srk129064 		/*
23868485SPeter.Memishian@Sun.COM 		 * Test addresses should never be used for source address
23878485SPeter.Memishian@Sun.COM 		 * selection, so ignore underlying ills.
23882202Srk129064 		 */
23898485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ill))
23908485SPeter.Memishian@Sun.COM 			continue;
23918485SPeter.Memishian@Sun.COM 
23929658SSowmini.Varadhan@Sun.COM 		if (ill->ill_ipif == NULL)
23939658SSowmini.Varadhan@Sun.COM 			continue;
23948485SPeter.Memishian@Sun.COM 		/*
23958485SPeter.Memishian@Sun.COM 		 * For source address selection, we treat the ipif list as
23968485SPeter.Memishian@Sun.COM 		 * circular and continue until we get back to where we
23978485SPeter.Memishian@Sun.COM 		 * started.  This allows IPMP to vary source address selection
23988485SPeter.Memishian@Sun.COM 		 * (which improves inbound load spreading) by caching its last
23998485SPeter.Memishian@Sun.COM 		 * ending point and starting from there.  NOTE: we don't have
24008485SPeter.Memishian@Sun.COM 		 * to worry about ill_src_ipif changing ills since that can't
24018485SPeter.Memishian@Sun.COM 		 * happen on the IPMP ill.
24028485SPeter.Memishian@Sun.COM 		 */
24038485SPeter.Memishian@Sun.COM 		start_ipif = ill->ill_ipif;
24048485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
24058485SPeter.Memishian@Sun.COM 			start_ipif = ill->ill_src_ipif;
24068485SPeter.Memishian@Sun.COM 
24078485SPeter.Memishian@Sun.COM 		ipif = start_ipif;
24088485SPeter.Memishian@Sun.COM 		do {
24098485SPeter.Memishian@Sun.COM 			if ((next_ipif = ipif->ipif_next) == NULL)
24108485SPeter.Memishian@Sun.COM 				next_ipif = ill->ill_ipif;
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
24130Sstevel@tonic-gate 				continue;
24140Sstevel@tonic-gate 
24151676Sjpk 			if (zoneid != ALL_ZONES &&
24161676Sjpk 			    ipif->ipif_zoneid != zoneid &&
24171676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
24180Sstevel@tonic-gate 				continue;
24190Sstevel@tonic-gate 
24201676Sjpk 			/*
24211676Sjpk 			 * Check compatibility of local address for
24221676Sjpk 			 * destination's default label if we're on a labeled
24231676Sjpk 			 * system.  Incompatible addresses can't be used at
24241676Sjpk 			 * all and must be skipped over.
24251676Sjpk 			 */
24261676Sjpk 			if (dst_rhtp != NULL) {
24271676Sjpk 				boolean_t incompat;
24281676Sjpk 
24291676Sjpk 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
24301676Sjpk 				    IPV6_VERSION, B_FALSE);
24311676Sjpk 				if (src_rhtp == NULL)
24321676Sjpk 					continue;
24331676Sjpk 				incompat =
24341676Sjpk 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
24351676Sjpk 				    src_rhtp->tpc_tp.tp_doi !=
24361676Sjpk 				    dst_rhtp->tpc_tp.tp_doi ||
24371676Sjpk 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
24381676Sjpk 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
24391676Sjpk 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
24401676Sjpk 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
24411676Sjpk 				TPC_RELE(src_rhtp);
24421676Sjpk 				if (incompat)
24431676Sjpk 					continue;
24441676Sjpk 			}
24451676Sjpk 
24460Sstevel@tonic-gate 			if (first_candidate) {
24470Sstevel@tonic-gate 				/*
24480Sstevel@tonic-gate 				 * This is first valid address in the list.
24490Sstevel@tonic-gate 				 * It is automatically the best candidate
24500Sstevel@tonic-gate 				 * so far.
24510Sstevel@tonic-gate 				 */
24520Sstevel@tonic-gate 				best_c.cand_ipif = ipif;
24530Sstevel@tonic-gate 				first_candidate = B_FALSE;
24540Sstevel@tonic-gate 				continue;
24550Sstevel@tonic-gate 			}
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 			bzero(&curr_c, sizeof (cand_t));
24580Sstevel@tonic-gate 			curr_c.cand_ipif = ipif;
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 			/*
24610Sstevel@tonic-gate 			 * Compare this current candidate (curr_c) with the
24620Sstevel@tonic-gate 			 * best candidate (best_c) by applying the
24630Sstevel@tonic-gate 			 * comparison rules in order until one breaks the
24640Sstevel@tonic-gate 			 * tie.
24650Sstevel@tonic-gate 			 */
24660Sstevel@tonic-gate 			for (index = 0; rules[index] != NULL; index++) {
24670Sstevel@tonic-gate 				/* Apply a comparison rule. */
24688485SPeter.Memishian@Sun.COM 				rule_result = (rules[index])(&best_c, &curr_c,
24698485SPeter.Memishian@Sun.COM 				    &dstinfo, ipst);
24700Sstevel@tonic-gate 				if (rule_result == CAND_AVOID) {
24710Sstevel@tonic-gate 					/*
24720Sstevel@tonic-gate 					 * The best candidate is still the
24730Sstevel@tonic-gate 					 * best candidate.  Forget about
24740Sstevel@tonic-gate 					 * this current candidate and go on
24750Sstevel@tonic-gate 					 * to the next one.
24760Sstevel@tonic-gate 					 */
24770Sstevel@tonic-gate 					break;
24780Sstevel@tonic-gate 				} else if (rule_result == CAND_PREFER) {
24790Sstevel@tonic-gate 					/*
24800Sstevel@tonic-gate 					 * This candidate is prefered.  It
24810Sstevel@tonic-gate 					 * becomes the best candidate so
24820Sstevel@tonic-gate 					 * far.  Go on to the next address.
24830Sstevel@tonic-gate 					 */
24840Sstevel@tonic-gate 					best_c = curr_c;
24850Sstevel@tonic-gate 					break;
24860Sstevel@tonic-gate 				}
24870Sstevel@tonic-gate 				/* We have a tie, apply the next rule. */
24880Sstevel@tonic-gate 			}
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 			/*
24910Sstevel@tonic-gate 			 * The last rule must be a tie breaker rule and
24920Sstevel@tonic-gate 			 * must never produce a tie.  At this point, the
24930Sstevel@tonic-gate 			 * candidate should have either been rejected, or
24940Sstevel@tonic-gate 			 * have been prefered as the best candidate so far.
24950Sstevel@tonic-gate 			 */
24960Sstevel@tonic-gate 			ASSERT(rule_result != CAND_TIE);
24978485SPeter.Memishian@Sun.COM 		} while ((ipif = next_ipif) != start_ipif);
24988485SPeter.Memishian@Sun.COM 
24998485SPeter.Memishian@Sun.COM 		/*
25008485SPeter.Memishian@Sun.COM 		 * For IPMP, update the source ipif rotor to the next ipif,
25018485SPeter.Memishian@Sun.COM 		 * provided we can look it up.  (We must not use it if it's
25028485SPeter.Memishian@Sun.COM 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
25038485SPeter.Memishian@Sun.COM 		 * ipif_free() checked ill_src_ipif.)
25048485SPeter.Memishian@Sun.COM 		 */
25058485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill) && ipif != NULL) {
25068485SPeter.Memishian@Sun.COM 			mutex_enter(&ipif->ipif_ill->ill_lock);
25078485SPeter.Memishian@Sun.COM 			next_ipif = ipif->ipif_next;
25088485SPeter.Memishian@Sun.COM 			if (next_ipif != NULL && IPIF_CAN_LOOKUP(next_ipif))
25098485SPeter.Memishian@Sun.COM 				ill->ill_src_ipif = next_ipif;
25108485SPeter.Memishian@Sun.COM 			else
25118485SPeter.Memishian@Sun.COM 				ill->ill_src_ipif = NULL;
25128485SPeter.Memishian@Sun.COM 			mutex_exit(&ipif->ipif_ill->ill_lock);
25130Sstevel@tonic-gate 		}
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 		/*
25168485SPeter.Memishian@Sun.COM 		 * Only one ill to consider if dst_restrict_ill is set.
25170Sstevel@tonic-gate 		 */
25188485SPeter.Memishian@Sun.COM 		if (dstinfo.dst_restrict_ill)
25198485SPeter.Memishian@Sun.COM 			break;
25200Sstevel@tonic-gate 	}
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 	ipif = best_c.cand_ipif;
25230Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
25240Sstevel@tonic-gate 	    dstinfo.dst_ill->ill_name,
25250Sstevel@tonic-gate 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
25260Sstevel@tonic-gate 	    (ipif == NULL ? "NULL" :
25270Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	if (usesrc_ill != NULL)
25300Sstevel@tonic-gate 		ill_refrele(usesrc_ill);
25310Sstevel@tonic-gate 
25328485SPeter.Memishian@Sun.COM 	if (ipmp_ill != NULL)
25338485SPeter.Memishian@Sun.COM 		ill_refrele(ipmp_ill);
25348485SPeter.Memishian@Sun.COM 
25351676Sjpk 	if (dst_rhtp != NULL)
25361676Sjpk 		TPC_RELE(dst_rhtp);
25371676Sjpk 
25380Sstevel@tonic-gate 	if (ipif == NULL) {
25393448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
25400Sstevel@tonic-gate 		return (NULL);
25410Sstevel@tonic-gate 	}
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_ill->ill_lock);
25440Sstevel@tonic-gate 	if (IPIF_CAN_LOOKUP(ipif)) {
25450Sstevel@tonic-gate 		ipif_refhold_locked(ipif);
25460Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_ill->ill_lock);
25473448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
25480Sstevel@tonic-gate 		return (ipif);
25490Sstevel@tonic-gate 	}
25500Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_ill->ill_lock);
25513448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
25520Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
25530Sstevel@tonic-gate 	    " returning null \n", (void *)ipif));
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 	return (NULL);
25560Sstevel@tonic-gate }
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate /*
25590Sstevel@tonic-gate  * If old_ipif is not NULL, see if ipif was derived from old
25600Sstevel@tonic-gate  * ipif and if so, recreate the interface route by re-doing
25610Sstevel@tonic-gate  * source address selection. This happens when ipif_down ->
25620Sstevel@tonic-gate  * ipif_update_other_ipifs calls us.
25630Sstevel@tonic-gate  *
25640Sstevel@tonic-gate  * If old_ipif is NULL, just redo the source address selection
25658485SPeter.Memishian@Sun.COM  * if needed. This happens when ipif_up_done_v6 calls us.
25660Sstevel@tonic-gate  */
25670Sstevel@tonic-gate void
25680Sstevel@tonic-gate ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif)
25690Sstevel@tonic-gate {
25700Sstevel@tonic-gate 	ire_t *ire;
25710Sstevel@tonic-gate 	ire_t *ipif_ire;
25720Sstevel@tonic-gate 	queue_t *stq;
25730Sstevel@tonic-gate 	ill_t *ill;
25740Sstevel@tonic-gate 	ipif_t *nipif = NULL;
25750Sstevel@tonic-gate 	boolean_t nipif_refheld = B_FALSE;
25760Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
25773448Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 	ill = ipif->ipif_ill;
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	if (!(ipif->ipif_flags &
25820Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
25830Sstevel@tonic-gate 		/*
25840Sstevel@tonic-gate 		 * Can't possibly have borrowed the source
25850Sstevel@tonic-gate 		 * from old_ipif.
25860Sstevel@tonic-gate 		 */
25870Sstevel@tonic-gate 		return;
25880Sstevel@tonic-gate 	}
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	/*
25910Sstevel@tonic-gate 	 * Is there any work to be done? No work if the address
25920Sstevel@tonic-gate 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
25930Sstevel@tonic-gate 	 * ipif_select_source_v6() does not borrow addresses from
25940Sstevel@tonic-gate 	 * NOLOCAL and ANYCAST interfaces).
25950Sstevel@tonic-gate 	 */
25960Sstevel@tonic-gate 	if ((old_ipif != NULL) &&
25970Sstevel@tonic-gate 	    ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) ||
25980Sstevel@tonic-gate 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
25990Sstevel@tonic-gate 	    (old_ipif->ipif_flags &
26000Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
26010Sstevel@tonic-gate 		return;
26020Sstevel@tonic-gate 	}
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	/*
26050Sstevel@tonic-gate 	 * Perform the same checks as when creating the
26060Sstevel@tonic-gate 	 * IRE_INTERFACE in ipif_up_done_v6.
26070Sstevel@tonic-gate 	 */
26080Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_UP))
26090Sstevel@tonic-gate 		return;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_NOXMIT))
26120Sstevel@tonic-gate 		return;
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
26150Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
26160Sstevel@tonic-gate 		return;
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	/*
26190Sstevel@tonic-gate 	 * We know that ipif uses some other source for its
26200Sstevel@tonic-gate 	 * IRE_INTERFACE. Is it using the source of this
26210Sstevel@tonic-gate 	 * old_ipif?
26220Sstevel@tonic-gate 	 */
26230Sstevel@tonic-gate 	ipif_ire = ipif_to_ire_v6(ipif);
26240Sstevel@tonic-gate 	if (ipif_ire == NULL)
26250Sstevel@tonic-gate 		return;
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	if (old_ipif != NULL &&
26280Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr,
26290Sstevel@tonic-gate 	    &ipif_ire->ire_src_addr_v6)) {
26300Sstevel@tonic-gate 		ire_refrele(ipif_ire);
26310Sstevel@tonic-gate 		return;
26320Sstevel@tonic-gate 	}
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	if (ip_debug > 2) {
26350Sstevel@tonic-gate 		/* ip1dbg */
26360Sstevel@tonic-gate 		pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE"
26370Sstevel@tonic-gate 		    " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6);
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	stq = ipif_ire->ire_stq;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	/*
26430Sstevel@tonic-gate 	 * Can't use our source address. Select a different source address
26440Sstevel@tonic-gate 	 * for the IRE_INTERFACE.  We restrict interface route source
26450Sstevel@tonic-gate 	 * address selection to ipif's assigned to the same link as the
26460Sstevel@tonic-gate 	 * interface.
26470Sstevel@tonic-gate 	 */
26483448Sdh155122 	if (ip6_asp_can_lookup(ipst)) {
26490Sstevel@tonic-gate 		ip6_asp_table_held = B_TRUE;
26500Sstevel@tonic-gate 		nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet,
26518485SPeter.Memishian@Sun.COM 		    B_TRUE, IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate 	if (nipif == NULL) {
26540Sstevel@tonic-gate 		/* Last resort - all ipif's have IPIF_NOLOCAL */
26550Sstevel@tonic-gate 		nipif = ipif;
26560Sstevel@tonic-gate 	} else {
26570Sstevel@tonic-gate 		nipif_refheld = B_TRUE;
26580Sstevel@tonic-gate 	}
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	ire = ire_create_v6(
26610Sstevel@tonic-gate 	    &ipif->ipif_v6subnet,	/* dest pref */
26620Sstevel@tonic-gate 	    &ipif->ipif_v6net_mask,	/* mask */
26630Sstevel@tonic-gate 	    &nipif->ipif_v6src_addr,	/* src addr */
26640Sstevel@tonic-gate 	    NULL,			/* no gateway */
26650Sstevel@tonic-gate 	    &ipif->ipif_mtu,		/* max frag */
26664714Ssowmini 	    NULL,			/* no src nce */
26670Sstevel@tonic-gate 	    NULL,			/* no recv from queue */
26680Sstevel@tonic-gate 	    stq,			/* send-to queue */
26690Sstevel@tonic-gate 	    ill->ill_net_type,		/* IF_[NO]RESOLVER */
26700Sstevel@tonic-gate 	    ipif,
26710Sstevel@tonic-gate 	    NULL,
26720Sstevel@tonic-gate 	    0,
26730Sstevel@tonic-gate 	    0,
26740Sstevel@tonic-gate 	    0,
26751676Sjpk 	    &ire_uinfo_null,
26761676Sjpk 	    NULL,
26773448Sdh155122 	    NULL,
26783448Sdh155122 	    ipst);
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate 	if (ire != NULL) {
26810Sstevel@tonic-gate 		ire_t *ret_ire;
26820Sstevel@tonic-gate 		int   error;
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 		/*
26850Sstevel@tonic-gate 		 * We don't need ipif_ire anymore. We need to delete
26860Sstevel@tonic-gate 		 * before we add so that ire_add does not detect
26870Sstevel@tonic-gate 		 * duplicates.
26880Sstevel@tonic-gate 		 */
26890Sstevel@tonic-gate 		ire_delete(ipif_ire);
26900Sstevel@tonic-gate 		ret_ire = ire;
26912535Ssangeeta 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
26920Sstevel@tonic-gate 		ASSERT(error == 0);
26930Sstevel@tonic-gate 		ASSERT(ret_ire == ire);
26940Sstevel@tonic-gate 		if (ret_ire != NULL) {
26950Sstevel@tonic-gate 			/* Held in ire_add */
26960Sstevel@tonic-gate 			ire_refrele(ret_ire);
26970Sstevel@tonic-gate 		}
26980Sstevel@tonic-gate 	}
26990Sstevel@tonic-gate 	/*
27000Sstevel@tonic-gate 	 * Either we are falling through from above or could not
27010Sstevel@tonic-gate 	 * allocate a replacement.
27020Sstevel@tonic-gate 	 */
27030Sstevel@tonic-gate 	ire_refrele(ipif_ire);
27040Sstevel@tonic-gate 	if (ip6_asp_table_held)
27053448Sdh155122 		ip6_asp_table_refrele(ipst);
27060Sstevel@tonic-gate 	if (nipif_refheld)
27070Sstevel@tonic-gate 		ipif_refrele(nipif);
27080Sstevel@tonic-gate }
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate /*
27110Sstevel@tonic-gate  * This old_ipif is going away.
27120Sstevel@tonic-gate  *
27130Sstevel@tonic-gate  * Determine if any other ipif's are using our address as
27140Sstevel@tonic-gate  * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
27150Sstevel@tonic-gate  * IPIF_DEPRECATED).
27160Sstevel@tonic-gate  * Find the IRE_INTERFACE for such ipif's and recreate them
27170Sstevel@tonic-gate  * to use an different source address following the rules in
27180Sstevel@tonic-gate  * ipif_up_done_v6.
27190Sstevel@tonic-gate  */
27200Sstevel@tonic-gate void
27218485SPeter.Memishian@Sun.COM ipif_update_other_ipifs_v6(ipif_t *old_ipif)
27220Sstevel@tonic-gate {
27230Sstevel@tonic-gate 	ipif_t	*ipif;
27240Sstevel@tonic-gate 	ill_t	*ill;
27250Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate 	ASSERT(IAM_WRITER_IPIF(old_ipif));
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	ill = old_ipif->ipif_ill;
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n",
27320Sstevel@tonic-gate 	    ill->ill_name,
27330Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr,
27340Sstevel@tonic-gate 	    buf, sizeof (buf))));
27350Sstevel@tonic-gate 
27368485SPeter.Memishian@Sun.COM 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
27378485SPeter.Memishian@Sun.COM 		if (ipif != old_ipif)
27380Sstevel@tonic-gate 			ipif_recreate_interface_routes_v6(old_ipif, ipif);
27390Sstevel@tonic-gate 	}
27400Sstevel@tonic-gate }
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate /*
27430Sstevel@tonic-gate  * Perform an attach and bind to get phys addr plus info_req for
27440Sstevel@tonic-gate  * the physical device.
27450Sstevel@tonic-gate  * q and mp represents an ioctl which will be queued waiting for
27460Sstevel@tonic-gate  * completion of the DLPI message exchange.
27470Sstevel@tonic-gate  * MUST be called on an ill queue. Can not set conn_pending_ill for that
27480Sstevel@tonic-gate  * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q.
27490Sstevel@tonic-gate  *
27500Sstevel@tonic-gate  * Returns EINPROGRESS when mp has been consumed by queueing it on
27510Sstevel@tonic-gate  * ill_pending_mp and the ioctl will complete in ip_rput.
27520Sstevel@tonic-gate  */
27530Sstevel@tonic-gate int
27540Sstevel@tonic-gate ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
27550Sstevel@tonic-gate {
27560Sstevel@tonic-gate 	mblk_t	*v6token_mp = NULL;
27570Sstevel@tonic-gate 	mblk_t	*v6lla_mp = NULL;
275810616SSebastien.Roy@Sun.COM 	mblk_t	*dest_mp = NULL;
27590Sstevel@tonic-gate 	mblk_t	*phys_mp = NULL;
27600Sstevel@tonic-gate 	mblk_t	*info_mp = NULL;
27610Sstevel@tonic-gate 	mblk_t	*attach_mp = NULL;
27620Sstevel@tonic-gate 	mblk_t	*bind_mp = NULL;
27630Sstevel@tonic-gate 	mblk_t	*unbind_mp = NULL;
27640Sstevel@tonic-gate 	mblk_t	*notify_mp = NULL;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
27670Sstevel@tonic-gate 	ASSERT(ill->ill_dlpi_style_set);
27680Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 	if (ill->ill_isv6) {
27710Sstevel@tonic-gate 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
27720Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
27730Sstevel@tonic-gate 		if (v6token_mp == NULL)
27740Sstevel@tonic-gate 			goto bad;
27750Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
27760Sstevel@tonic-gate 		    DL_IPV6_TOKEN;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
27790Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
27800Sstevel@tonic-gate 		if (v6lla_mp == NULL)
27810Sstevel@tonic-gate 			goto bad;
27820Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
27830Sstevel@tonic-gate 		    DL_IPV6_LINK_LAYER_ADDR;
27840Sstevel@tonic-gate 	}
27850Sstevel@tonic-gate 
278610616SSebastien.Roy@Sun.COM 	if (ill->ill_mactype == DL_IPV4 || ill->ill_mactype == DL_IPV6) {
278710616SSebastien.Roy@Sun.COM 		dest_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
278810616SSebastien.Roy@Sun.COM 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
278910616SSebastien.Roy@Sun.COM 		if (dest_mp == NULL)
279010616SSebastien.Roy@Sun.COM 			goto bad;
279110616SSebastien.Roy@Sun.COM 		((dl_phys_addr_req_t *)dest_mp->b_rptr)->dl_addr_type =
279210616SSebastien.Roy@Sun.COM 		    DL_CURR_DEST_ADDR;
279310616SSebastien.Roy@Sun.COM 	}
279410616SSebastien.Roy@Sun.COM 
27950Sstevel@tonic-gate 	/*
27960Sstevel@tonic-gate 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
27970Sstevel@tonic-gate 	 */
27980Sstevel@tonic-gate 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
27990Sstevel@tonic-gate 	    DL_NOTIFY_REQ);
28000Sstevel@tonic-gate 	if (notify_mp == NULL)
28010Sstevel@tonic-gate 		goto bad;
28020Sstevel@tonic-gate 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
28030Sstevel@tonic-gate 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
28049073SCathy.Zhou@Sun.COM 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG |
28059743SGirish.Moodalbail@Sun.COM 	    DL_NOTE_PROMISC_ON_PHYS | DL_NOTE_PROMISC_OFF_PHYS |
28069073SCathy.Zhou@Sun.COM 	    DL_NOTE_REPLUMB);
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
28090Sstevel@tonic-gate 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
28100Sstevel@tonic-gate 	if (phys_mp == NULL)
28110Sstevel@tonic-gate 		goto bad;
28120Sstevel@tonic-gate 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
28130Sstevel@tonic-gate 	    DL_CURR_PHYS_ADDR;
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate 	info_mp = ip_dlpi_alloc(
28160Sstevel@tonic-gate 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
28170Sstevel@tonic-gate 	    DL_INFO_REQ);
28180Sstevel@tonic-gate 	if (info_mp == NULL)
28190Sstevel@tonic-gate 		goto bad;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
28220Sstevel@tonic-gate 	    DL_BIND_REQ);
28230Sstevel@tonic-gate 	if (bind_mp == NULL)
28240Sstevel@tonic-gate 		goto bad;
28250Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
28260Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
28290Sstevel@tonic-gate 	if (unbind_mp == NULL)
28300Sstevel@tonic-gate 		goto bad;
28310Sstevel@tonic-gate 
28324360Smeem 	/* If we need to attach, pre-alloc and initialize the mblk */
28330Sstevel@tonic-gate 	if (ill->ill_needs_attach) {
28340Sstevel@tonic-gate 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
28350Sstevel@tonic-gate 		    DL_ATTACH_REQ);
28360Sstevel@tonic-gate 		if (attach_mp == NULL)
28370Sstevel@tonic-gate 			goto bad;
28380Sstevel@tonic-gate 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
28390Sstevel@tonic-gate 	}
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	/*
28420Sstevel@tonic-gate 	 * Here we are going to delay the ioctl ack until after
28430Sstevel@tonic-gate 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
28440Sstevel@tonic-gate 	 * original ioctl message before sending the requests
28450Sstevel@tonic-gate 	 */
28460Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
28470Sstevel@tonic-gate 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
28480Sstevel@tonic-gate 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
28490Sstevel@tonic-gate 	/*
28500Sstevel@tonic-gate 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
28510Sstevel@tonic-gate 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
28520Sstevel@tonic-gate 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
28530Sstevel@tonic-gate 	 */
28540Sstevel@tonic-gate 	ill->ill_phys_addr_pend = 0;
28550Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	if (attach_mp != NULL) {
28580Sstevel@tonic-gate 		ip1dbg(("ill_dl_phys: attach\n"));
28590Sstevel@tonic-gate 		ill_dlpi_send(ill, attach_mp);
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 	ill_dlpi_send(ill, bind_mp);
28620Sstevel@tonic-gate 	ill_dlpi_send(ill, info_mp);
286310616SSebastien.Roy@Sun.COM 	if (v6token_mp != NULL)
28640Sstevel@tonic-gate 		ill_dlpi_send(ill, v6token_mp);
286510616SSebastien.Roy@Sun.COM 	if (v6lla_mp != NULL)
28660Sstevel@tonic-gate 		ill_dlpi_send(ill, v6lla_mp);
286710616SSebastien.Roy@Sun.COM 	if (dest_mp != NULL)
286810616SSebastien.Roy@Sun.COM 		ill_dlpi_send(ill, dest_mp);
28690Sstevel@tonic-gate 	ill_dlpi_send(ill, phys_mp);
28700Sstevel@tonic-gate 	ill_dlpi_send(ill, notify_mp);
28710Sstevel@tonic-gate 	ill_dlpi_send(ill, unbind_mp);
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	/*
28740Sstevel@tonic-gate 	 * This operation will complete in ip_rput_dlpi_writer with either
28750Sstevel@tonic-gate 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
28760Sstevel@tonic-gate 	 */
28770Sstevel@tonic-gate 	return (EINPROGRESS);
28780Sstevel@tonic-gate bad:
28794360Smeem 	freemsg(v6token_mp);
28804360Smeem 	freemsg(v6lla_mp);
288110616SSebastien.Roy@Sun.COM 	freemsg(dest_mp);
28824360Smeem 	freemsg(phys_mp);
28834360Smeem 	freemsg(info_mp);
28844360Smeem 	freemsg(attach_mp);
28854360Smeem 	freemsg(bind_mp);
28864360Smeem 	freemsg(unbind_mp);
28874360Smeem 	freemsg(notify_mp);
28880Sstevel@tonic-gate 	return (ENOMEM);
28890Sstevel@tonic-gate }
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
28920Sstevel@tonic-gate 
28930Sstevel@tonic-gate /*
28940Sstevel@tonic-gate  * DLPI is up.
28950Sstevel@tonic-gate  * Create all the IREs associated with an interface bring up multicast.
28960Sstevel@tonic-gate  * Set the interface flag and finish other initialization
28970Sstevel@tonic-gate  * that potentially had to be differed to after DL_BIND_ACK.
28980Sstevel@tonic-gate  */
28990Sstevel@tonic-gate int
29000Sstevel@tonic-gate ipif_up_done_v6(ipif_t *ipif)
29010Sstevel@tonic-gate {
29020Sstevel@tonic-gate 	ire_t	*ire_array[20];
29030Sstevel@tonic-gate 	ire_t	**irep = ire_array;
29040Sstevel@tonic-gate 	ire_t	**irep1;
29050Sstevel@tonic-gate 	ill_t	*ill = ipif->ipif_ill;
29060Sstevel@tonic-gate 	queue_t	*stq;
29070Sstevel@tonic-gate 	in6_addr_t	v6addr;
29080Sstevel@tonic-gate 	in6_addr_t	route_mask;
29090Sstevel@tonic-gate 	ipif_t	 *src_ipif = NULL;
29100Sstevel@tonic-gate 	ipif_t   *tmp_ipif;
29110Sstevel@tonic-gate 	boolean_t	flush_ire_cache = B_TRUE;
29120Sstevel@tonic-gate 	int	err;
29130Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
29140Sstevel@tonic-gate 	ire_t	**ipif_saved_irep = NULL;
29150Sstevel@tonic-gate 	int ipif_saved_ire_cnt;
29160Sstevel@tonic-gate 	int cnt;
29170Sstevel@tonic-gate 	boolean_t src_ipif_held = B_FALSE;
29180Sstevel@tonic-gate 	boolean_t loopback = B_FALSE;
29190Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
29203448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
29234459Skcpoon 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	/* Check if this is a loopback interface */
29260Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_wq == NULL)
29270Sstevel@tonic-gate 		loopback = B_TRUE;
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
29300Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 	/*
29330Sstevel@tonic-gate 	 * If all other interfaces for this ill are down or DEPRECATED,
29340Sstevel@tonic-gate 	 * or otherwise unsuitable for source address selection, remove
29350Sstevel@tonic-gate 	 * any IRE_CACHE entries for this ill to make sure source
29360Sstevel@tonic-gate 	 * address selection gets to take this new ipif into account.
29370Sstevel@tonic-gate 	 * No need to hold ill_lock while traversing the ipif list since
29380Sstevel@tonic-gate 	 * we are writer
29390Sstevel@tonic-gate 	 */
29400Sstevel@tonic-gate 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
29414459Skcpoon 	    tmp_ipif = tmp_ipif->ipif_next) {
29420Sstevel@tonic-gate 		if (((tmp_ipif->ipif_flags &
29430Sstevel@tonic-gate 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
29440Sstevel@tonic-gate 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
29450Sstevel@tonic-gate 		    (tmp_ipif == ipif))
29460Sstevel@tonic-gate 			continue;
29470Sstevel@tonic-gate 		/* first useable pre-existing interface */
29480Sstevel@tonic-gate 		flush_ire_cache = B_FALSE;
29490Sstevel@tonic-gate 		break;
29500Sstevel@tonic-gate 	}
29510Sstevel@tonic-gate 	if (flush_ire_cache)
29528485SPeter.Memishian@Sun.COM 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
29538485SPeter.Memishian@Sun.COM 		    IRE_CACHE, ill_ipif_cache_delete, ill, ill);
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	/*
29560Sstevel@tonic-gate 	 * Figure out which way the send-to queue should go.  Only
29570Sstevel@tonic-gate 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here.
29580Sstevel@tonic-gate 	 */
29590Sstevel@tonic-gate 	switch (ill->ill_net_type) {
29600Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
29610Sstevel@tonic-gate 		stq = ill->ill_rq;
29620Sstevel@tonic-gate 		break;
29630Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
29640Sstevel@tonic-gate 	case IRE_LOOPBACK:
29650Sstevel@tonic-gate 		stq = ill->ill_wq;
29660Sstevel@tonic-gate 		break;
29670Sstevel@tonic-gate 	default:
29680Sstevel@tonic-gate 		return (EINVAL);
29690Sstevel@tonic-gate 	}
29700Sstevel@tonic-gate 
29714459Skcpoon 	if (IS_LOOPBACK(ill)) {
29720Sstevel@tonic-gate 		/*
29730Sstevel@tonic-gate 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
29740Sstevel@tonic-gate 		 * ipif_lookup_on_name(), but in the case of zones we can have
29750Sstevel@tonic-gate 		 * several loopback addresses on lo0. So all the interfaces with
29760Sstevel@tonic-gate 		 * loopback addresses need to be marked IRE_LOOPBACK.
29770Sstevel@tonic-gate 		 */
29780Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
29790Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOOPBACK;
29800Sstevel@tonic-gate 		else
29810Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOCAL;
29820Sstevel@tonic-gate 	}
29830Sstevel@tonic-gate 
29848485SPeter.Memishian@Sun.COM 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST) ||
29858485SPeter.Memishian@Sun.COM 	    ((ipif->ipif_flags & IPIF_DEPRECATED) &&
29868485SPeter.Memishian@Sun.COM 	    !(ipif->ipif_flags & IPIF_NOFAILOVER))) {
29870Sstevel@tonic-gate 		/*
29880Sstevel@tonic-gate 		 * Can't use our source address. Select a different
29890Sstevel@tonic-gate 		 * source address for the IRE_INTERFACE and IRE_LOCAL
29900Sstevel@tonic-gate 		 */
29913448Sdh155122 		if (ip6_asp_can_lookup(ipst)) {
29920Sstevel@tonic-gate 			ip6_asp_table_held = B_TRUE;
29930Sstevel@tonic-gate 			src_ipif = ipif_select_source_v6(ipif->ipif_ill,
29948485SPeter.Memishian@Sun.COM 			    &ipif->ipif_v6subnet, B_FALSE,
29950Sstevel@tonic-gate 			    IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
29960Sstevel@tonic-gate 		}
29970Sstevel@tonic-gate 		if (src_ipif == NULL)
29980Sstevel@tonic-gate 			src_ipif = ipif;	/* Last resort */
29990Sstevel@tonic-gate 		else
30000Sstevel@tonic-gate 			src_ipif_held = B_TRUE;
30010Sstevel@tonic-gate 	} else {
30020Sstevel@tonic-gate 		src_ipif = ipif;
30030Sstevel@tonic-gate 	}
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
30060Sstevel@tonic-gate 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
30071676Sjpk 
30081676Sjpk 		/*
30091676Sjpk 		 * If we're on a labeled system then make sure that zone-
30101676Sjpk 		 * private addresses have proper remote host database entries.
30111676Sjpk 		 */
30121676Sjpk 		if (is_system_labeled() &&
30131676Sjpk 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
30141676Sjpk 			if (ip6opt_ls == 0) {
30151676Sjpk 				cmn_err(CE_WARN, "IPv6 not enabled "
30161676Sjpk 				    "via /etc/system");
30171676Sjpk 				return (EINVAL);
30181676Sjpk 			}
30191676Sjpk 			if (!tsol_check_interface_address(ipif))
30201676Sjpk 				return (EINVAL);
30211676Sjpk 		}
30221676Sjpk 
30230Sstevel@tonic-gate 		/* Register the source address for __sin6_src_id */
30240Sstevel@tonic-gate 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
30253448Sdh155122 		    ipif->ipif_zoneid, ipst);
30260Sstevel@tonic-gate 		if (err != 0) {
30270Sstevel@tonic-gate 			ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err));
30280Sstevel@tonic-gate 			if (src_ipif_held)
30290Sstevel@tonic-gate 				ipif_refrele(src_ipif);
30300Sstevel@tonic-gate 			if (ip6_asp_table_held)
30313448Sdh155122 				ip6_asp_table_refrele(ipst);
30320Sstevel@tonic-gate 			return (err);
30330Sstevel@tonic-gate 		}
30340Sstevel@tonic-gate 		/*
30350Sstevel@tonic-gate 		 * If the interface address is set, create the LOCAL
30360Sstevel@tonic-gate 		 * or LOOPBACK IRE.
30370Sstevel@tonic-gate 		 */
30380Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n",
30390Sstevel@tonic-gate 		    ipif->ipif_ire_type,
30400Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
30410Sstevel@tonic-gate 		    buf, sizeof (buf))));
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 		*irep++ = ire_create_v6(
30440Sstevel@tonic-gate 		    &ipif->ipif_v6lcl_addr,		/* dest address */
30450Sstevel@tonic-gate 		    &ipv6_all_ones,			/* mask */
30460Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,		/* source address */
30470Sstevel@tonic-gate 		    NULL,				/* no gateway */
30480Sstevel@tonic-gate 		    &ip_loopback_mtu_v6plus,		/* max frag size */
30490Sstevel@tonic-gate 		    NULL,
30500Sstevel@tonic-gate 		    ipif->ipif_rq,			/* recv-from queue */
30510Sstevel@tonic-gate 		    NULL,				/* no send-to queue */
30520Sstevel@tonic-gate 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
30530Sstevel@tonic-gate 		    ipif,				/* interface */
30540Sstevel@tonic-gate 		    NULL,
30550Sstevel@tonic-gate 		    0,
30560Sstevel@tonic-gate 		    0,
30570Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
30581676Sjpk 		    &ire_uinfo_null,
30591676Sjpk 		    NULL,
30603448Sdh155122 		    NULL,
30613448Sdh155122 		    ipst);
30620Sstevel@tonic-gate 	}
30630Sstevel@tonic-gate 
306410616SSebastien.Roy@Sun.COM 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
30650Sstevel@tonic-gate 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
30660Sstevel@tonic-gate 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
30670Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
30680Sstevel@tonic-gate 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
30690Sstevel@tonic-gate 		v6addr = ipif->ipif_v6subnet;
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
30720Sstevel@tonic-gate 			route_mask = ipv6_all_ones;
30730Sstevel@tonic-gate 		} else {
30740Sstevel@tonic-gate 			route_mask = ipif->ipif_v6net_mask;
30750Sstevel@tonic-gate 		}
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n",
30780Sstevel@tonic-gate 		    ill->ill_net_type,
30790Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 		*irep++ = ire_create_v6(
30820Sstevel@tonic-gate 		    &v6addr,			/* dest pref */
30830Sstevel@tonic-gate 		    &route_mask,		/* mask */
30840Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,	/* src addr */
30850Sstevel@tonic-gate 		    NULL,			/* no gateway */
30860Sstevel@tonic-gate 		    &ipif->ipif_mtu,		/* max frag */
30874714Ssowmini 		    NULL,			/* no src nce */
30880Sstevel@tonic-gate 		    NULL,			/* no recv from queue */
30890Sstevel@tonic-gate 		    stq,			/* send-to queue */
30900Sstevel@tonic-gate 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
30910Sstevel@tonic-gate 		    ipif,
30920Sstevel@tonic-gate 		    NULL,
30930Sstevel@tonic-gate 		    0,
30940Sstevel@tonic-gate 		    0,
30950Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
30961676Sjpk 		    &ire_uinfo_null,
30971676Sjpk 		    NULL,
30983448Sdh155122 		    NULL,
30993448Sdh155122 		    ipst);
31000Sstevel@tonic-gate 	}
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	/* If an earlier ire_create failed, get out now */
31030Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
31040Sstevel@tonic-gate 		irep1--;
31050Sstevel@tonic-gate 		if (*irep1 == NULL) {
31060Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: NULL ire found in"
31070Sstevel@tonic-gate 			    " ire_array\n"));
31080Sstevel@tonic-gate 			err = ENOMEM;
31090Sstevel@tonic-gate 			goto bad;
31100Sstevel@tonic-gate 		}
31110Sstevel@tonic-gate 	}
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	/*
31168485SPeter.Memishian@Sun.COM 	 * Need to atomically check for IP address availability under
31178485SPeter.Memishian@Sun.COM 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
31188485SPeter.Memishian@Sun.COM 	 * ills or new ipifs can be added while we are checking availability.
31190Sstevel@tonic-gate 	 */
31203448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
31213448Sdh155122 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
31220Sstevel@tonic-gate 	ill->ill_ipif_up_count++;
31230Sstevel@tonic-gate 	ipif->ipif_flags |= IPIF_UP;
31240Sstevel@tonic-gate 	err = ip_addr_availability_check(ipif);
31253448Sdh155122 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
31263448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	if (err != 0) {
31290Sstevel@tonic-gate 		/*
31300Sstevel@tonic-gate 		 * Our address may already be up on the same ill. In this case,
31310Sstevel@tonic-gate 		 * the external resolver entry for our ipif replaced the one for
31320Sstevel@tonic-gate 		 * the other ipif. So we don't want to delete it (otherwise the
31330Sstevel@tonic-gate 		 * other ipif would be unable to send packets).
31340Sstevel@tonic-gate 		 * ip_addr_availability_check() identifies this case for us and
31350Sstevel@tonic-gate 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
31360Sstevel@tonic-gate 		 * which is the expected error code.
31379287SSowmini.Varadhan@Sun.COM 		 *
31389287SSowmini.Varadhan@Sun.COM 		 * Note that, for the non-XRESOLV case, ipif_ndp_down() will
31399571SSowmini.Varadhan@Sun.COM 		 * only delete the nce in the case when the nce_ipif_cnt drops
31409571SSowmini.Varadhan@Sun.COM 		 * to 0.
31410Sstevel@tonic-gate 		 */
31420Sstevel@tonic-gate 		if (err == EADDRINUSE) {
31430Sstevel@tonic-gate 			if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) {
31440Sstevel@tonic-gate 				freemsg(ipif->ipif_arp_del_mp);
31450Sstevel@tonic-gate 				ipif->ipif_arp_del_mp = NULL;
31460Sstevel@tonic-gate 			}
31470Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
31480Sstevel@tonic-gate 		}
31490Sstevel@tonic-gate 		ill->ill_ipif_up_count--;
31500Sstevel@tonic-gate 		ipif->ipif_flags &= ~IPIF_UP;
31510Sstevel@tonic-gate 		goto bad;
31520Sstevel@tonic-gate 	}
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	/*
31558485SPeter.Memishian@Sun.COM 	 * Add in all newly created IREs.
31560Sstevel@tonic-gate 	 *
31570Sstevel@tonic-gate 	 * NOTE : We refrele the ire though we may branch to "bad"
31580Sstevel@tonic-gate 	 *	  later on where we do ire_delete. This is okay
31590Sstevel@tonic-gate 	 *	  because nobody can delete it as we are running
31600Sstevel@tonic-gate 	 *	  exclusively.
31610Sstevel@tonic-gate 	 */
31620Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
31630Sstevel@tonic-gate 		irep1--;
31640Sstevel@tonic-gate 		/* Shouldn't be adding any bcast ire's */
31650Sstevel@tonic-gate 		ASSERT((*irep1)->ire_type != IRE_BROADCAST);
31660Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
31670Sstevel@tonic-gate 		/*
31680Sstevel@tonic-gate 		 * refheld by ire_add. refele towards the end of the func
31690Sstevel@tonic-gate 		 */
31702535Ssangeeta 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
31710Sstevel@tonic-gate 	}
31720Sstevel@tonic-gate 	if (ip6_asp_table_held) {
31733448Sdh155122 		ip6_asp_table_refrele(ipst);
31740Sstevel@tonic-gate 		ip6_asp_table_held = B_FALSE;
31750Sstevel@tonic-gate 	}
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
31780Sstevel@tonic-gate 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
31790Sstevel@tonic-gate 	ipif_saved_irep = ipif_recover_ire_v6(ipif);
31800Sstevel@tonic-gate 
31818023SPhil.Kirk@Sun.COM 	if (ill->ill_need_recover_multicast) {
31820Sstevel@tonic-gate 		/*
31830Sstevel@tonic-gate 		 * Need to recover all multicast memberships in the driver.
31840Sstevel@tonic-gate 		 * This had to be deferred until we had attached.
31850Sstevel@tonic-gate 		 */
31860Sstevel@tonic-gate 		ill_recover_multicast(ill);
31870Sstevel@tonic-gate 	}
31888485SPeter.Memishian@Sun.COM 
31898485SPeter.Memishian@Sun.COM 	if (ill->ill_ipif_up_count == 1) {
31908485SPeter.Memishian@Sun.COM 		/*
31918485SPeter.Memishian@Sun.COM 		 * Since the interface is now up, it may now be active.
31928485SPeter.Memishian@Sun.COM 		 */
31938485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ill))
31948485SPeter.Memishian@Sun.COM 			ipmp_ill_refresh_active(ill);
31958485SPeter.Memishian@Sun.COM 	}
31968485SPeter.Memishian@Sun.COM 
31970Sstevel@tonic-gate 	/* Join the allhosts multicast address and the solicited node MC */
31980Sstevel@tonic-gate 	ipif_multicast_up(ipif);
31990Sstevel@tonic-gate 
32008485SPeter.Memishian@Sun.COM 	/*
32018485SPeter.Memishian@Sun.COM 	 * See if anybody else would benefit from our new ipif.
32028485SPeter.Memishian@Sun.COM 	 */
32038485SPeter.Memishian@Sun.COM 	if (!loopback &&
32048485SPeter.Memishian@Sun.COM 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
32050Sstevel@tonic-gate 		ill_update_source_selection(ill);
32060Sstevel@tonic-gate 	}
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
32090Sstevel@tonic-gate 		irep1--;
32100Sstevel@tonic-gate 		if (*irep1 != NULL) {
32110Sstevel@tonic-gate 			/* was held in ire_add */
32120Sstevel@tonic-gate 			ire_refrele(*irep1);
32130Sstevel@tonic-gate 		}
32140Sstevel@tonic-gate 	}
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 	cnt = ipif_saved_ire_cnt;
32170Sstevel@tonic-gate 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
32180Sstevel@tonic-gate 		if (*irep1 != NULL) {
32190Sstevel@tonic-gate 			/* was held in ire_add */
32200Sstevel@tonic-gate 			ire_refrele(*irep1);
32210Sstevel@tonic-gate 		}
32220Sstevel@tonic-gate 	}
32230Sstevel@tonic-gate 
32248023SPhil.Kirk@Sun.COM 	if (ipif->ipif_addr_ready)
32258023SPhil.Kirk@Sun.COM 		ipif_up_notify(ipif);
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
32280Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
32290Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
32300Sstevel@tonic-gate 	}
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	if (src_ipif_held)
32330Sstevel@tonic-gate 		ipif_refrele(src_ipif);
32348023SPhil.Kirk@Sun.COM 
32350Sstevel@tonic-gate 	return (0);
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate bad:
32380Sstevel@tonic-gate 	if (ip6_asp_table_held)
32393448Sdh155122 		ip6_asp_table_refrele(ipst);
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 	while (irep > ire_array) {
32420Sstevel@tonic-gate 		irep--;
32438485SPeter.Memishian@Sun.COM 		if (*irep != NULL)
32440Sstevel@tonic-gate 			ire_delete(*irep);
32450Sstevel@tonic-gate 	}
32463448Sdh155122 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
32490Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
32500Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
32510Sstevel@tonic-gate 	}
32520Sstevel@tonic-gate 	if (src_ipif_held)
32530Sstevel@tonic-gate 		ipif_refrele(src_ipif);
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	ipif_ndp_down(ipif);
32568485SPeter.Memishian@Sun.COM 	ipif_resolver_down(ipif);
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 	return (err);
32590Sstevel@tonic-gate }
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate /*
32620Sstevel@tonic-gate  * Delete an ND entry and the corresponding IRE_CACHE entry if it exists.
32630Sstevel@tonic-gate  */
32640Sstevel@tonic-gate /* ARGSUSED */
32650Sstevel@tonic-gate int
32660Sstevel@tonic-gate ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
32670Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
32680Sstevel@tonic-gate {
32690Sstevel@tonic-gate 	sin6_t		*sin6;
32700Sstevel@tonic-gate 	nce_t		*nce;
32710Sstevel@tonic-gate 	struct lifreq	*lifr;
32720Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
32738485SPeter.Memishian@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
32748485SPeter.Memishian@Sun.COM 	ire_t		*ire;
32758485SPeter.Memishian@Sun.COM 
32768485SPeter.Memishian@Sun.COM 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
32770Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
32780Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
32790Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
32800Sstevel@tonic-gate 		return (EINVAL);
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
32830Sstevel@tonic-gate 		return (EINVAL);
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
32860Sstevel@tonic-gate 		return (EAFNOSUPPORT);
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate 	sin6 = (sin6_t *)&lnr->lnr_addr;
32898485SPeter.Memishian@Sun.COM 
32908485SPeter.Memishian@Sun.COM 	/*
32918485SPeter.Memishian@Sun.COM 	 * Since ND mappings must be consistent across an IPMP group, prohibit
32928485SPeter.Memishian@Sun.COM 	 * deleting ND mappings on underlying interfaces.  Also, since ND
32938485SPeter.Memishian@Sun.COM 	 * mappings for IPMP data addresses are owned by IP itself, prohibit
32948485SPeter.Memishian@Sun.COM 	 * deleting them.
32958485SPeter.Memishian@Sun.COM 	 */
32968485SPeter.Memishian@Sun.COM 	if (IS_UNDER_IPMP(ill))
32978485SPeter.Memishian@Sun.COM 		return (EPERM);
32988485SPeter.Memishian@Sun.COM 
32998485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill)) {
33008485SPeter.Memishian@Sun.COM 		ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, IRE_LOCAL,
33018485SPeter.Memishian@Sun.COM 		    ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL,
33028485SPeter.Memishian@Sun.COM 		    ill->ill_ipst);
33038485SPeter.Memishian@Sun.COM 		if (ire != NULL) {
33048485SPeter.Memishian@Sun.COM 			ire_refrele(ire);
33058485SPeter.Memishian@Sun.COM 			return (EPERM);
33068485SPeter.Memishian@Sun.COM 		}
33078485SPeter.Memishian@Sun.COM 	}
33088485SPeter.Memishian@Sun.COM 
33098485SPeter.Memishian@Sun.COM 	/* See comment in ndp_query() regarding IS_IPMP(ill) usage */
33108485SPeter.Memishian@Sun.COM 	nce = ndp_lookup_v6(ill, IS_IPMP(ill), &sin6->sin6_addr, B_FALSE);
33110Sstevel@tonic-gate 	if (nce == NULL)
33120Sstevel@tonic-gate 		return (ESRCH);
33130Sstevel@tonic-gate 	ndp_delete(nce);
33140Sstevel@tonic-gate 	NCE_REFRELE(nce);
33150Sstevel@tonic-gate 	return (0);
33160Sstevel@tonic-gate }
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate /*
33190Sstevel@tonic-gate  * Return nbr cache info.
33200Sstevel@tonic-gate  */
33210Sstevel@tonic-gate /* ARGSUSED */
33220Sstevel@tonic-gate int
33230Sstevel@tonic-gate ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
33240Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
33250Sstevel@tonic-gate {
33260Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
33270Sstevel@tonic-gate 	struct lifreq	*lifr;
33280Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
33310Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
33320Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
33330Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
33340Sstevel@tonic-gate 		return (EINVAL);
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
33370Sstevel@tonic-gate 		return (EINVAL);
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
33400Sstevel@tonic-gate 		return (EAFNOSUPPORT);
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
33430Sstevel@tonic-gate 		return (EINVAL);
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	return (ndp_query(ill, lnr));
33460Sstevel@tonic-gate }
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate /*
33490Sstevel@tonic-gate  * Perform an update of the nd entry for the specified address.
33500Sstevel@tonic-gate  */
33510Sstevel@tonic-gate /* ARGSUSED */
33520Sstevel@tonic-gate int
33530Sstevel@tonic-gate ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
33540Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
33550Sstevel@tonic-gate {
33568485SPeter.Memishian@Sun.COM 	sin6_t		*sin6;
33570Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
33580Sstevel@tonic-gate 	struct	lifreq	*lifr;
33590Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
33608485SPeter.Memishian@Sun.COM 	ire_t		*ire;
33613448Sdh155122 
33620Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
33630Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
33640Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
33650Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
33660Sstevel@tonic-gate 		return (EINVAL);
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
33690Sstevel@tonic-gate 		return (EINVAL);
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
33720Sstevel@tonic-gate 		return (EAFNOSUPPORT);
33730Sstevel@tonic-gate 
33748485SPeter.Memishian@Sun.COM 	sin6 = (sin6_t *)&lnr->lnr_addr;
33758485SPeter.Memishian@Sun.COM 
33768485SPeter.Memishian@Sun.COM 	/*
33778485SPeter.Memishian@Sun.COM 	 * Since ND mappings must be consistent across an IPMP group, prohibit
33788485SPeter.Memishian@Sun.COM 	 * updating ND mappings on underlying interfaces.  Also, since ND
33798485SPeter.Memishian@Sun.COM 	 * mappings for IPMP data addresses are owned by IP itself, prohibit
33808485SPeter.Memishian@Sun.COM 	 * updating them.
33818485SPeter.Memishian@Sun.COM 	 */
33828485SPeter.Memishian@Sun.COM 	if (IS_UNDER_IPMP(ill))
33838485SPeter.Memishian@Sun.COM 		return (EPERM);
33848485SPeter.Memishian@Sun.COM 
33858485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill)) {
33868485SPeter.Memishian@Sun.COM 		ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, IRE_LOCAL,
33878485SPeter.Memishian@Sun.COM 		    ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL,
33888485SPeter.Memishian@Sun.COM 		    ill->ill_ipst);
33898485SPeter.Memishian@Sun.COM 		if (ire != NULL) {
33908485SPeter.Memishian@Sun.COM 			ire_refrele(ire);
33918485SPeter.Memishian@Sun.COM 			return (EPERM);
33928485SPeter.Memishian@Sun.COM 		}
33938485SPeter.Memishian@Sun.COM 	}
33948485SPeter.Memishian@Sun.COM 
33950Sstevel@tonic-gate 	return (ndp_sioc_update(ill, lnr));
33960Sstevel@tonic-gate }
3397