xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_if.c (revision 13123:9efd3d43accd)
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 /*
2212748SSowmini.Varadhan@oracle.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  * Copyright (c) 1990 Mentat Inc.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * This file contains the interface control functions for IPv6.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/stream.h>
330Sstevel@tonic-gate #include <sys/dlpi.h>
340Sstevel@tonic-gate #include <sys/stropts.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/kstat.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/zone.h>
403448Sdh155122 #include <sys/policy.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/socket.h>
450Sstevel@tonic-gate #include <sys/isa_defs.h>
460Sstevel@tonic-gate #include <net/if.h>
470Sstevel@tonic-gate #include <net/if_dl.h>
480Sstevel@tonic-gate #include <net/route.h>
490Sstevel@tonic-gate #include <netinet/in.h>
500Sstevel@tonic-gate #include <netinet/igmp_var.h>
510Sstevel@tonic-gate #include <netinet/ip6.h>
520Sstevel@tonic-gate #include <netinet/icmp6.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <inet/common.h>
550Sstevel@tonic-gate #include <inet/nd.h>
5612016SGirish.Moodalbail@Sun.COM #include <inet/tunables.h>
570Sstevel@tonic-gate #include <inet/mib2.h>
580Sstevel@tonic-gate #include <inet/ip.h>
590Sstevel@tonic-gate #include <inet/ip6.h>
600Sstevel@tonic-gate #include <inet/ip_multi.h>
610Sstevel@tonic-gate #include <inet/ip_ire.h>
620Sstevel@tonic-gate #include <inet/ip_rts.h>
630Sstevel@tonic-gate #include <inet/ip_ndp.h>
640Sstevel@tonic-gate #include <inet/ip_if.h>
650Sstevel@tonic-gate #include <inet/ip6_asp.h>
660Sstevel@tonic-gate #include <inet/ipclassifier.h>
670Sstevel@tonic-gate #include <inet/sctp_ip.h>
680Sstevel@tonic-gate 
691676Sjpk #include <sys/tsol/tndb.h>
701676Sjpk #include <sys/tsol/tnet.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static in6_addr_t	ipv6_ll_template =
730Sstevel@tonic-gate 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static ipif_t *
760Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
7711042SErik.Nordmark@Sun.COM     ip_stack_t *ipst);
7811042SErik.Nordmark@Sun.COM 
7911042SErik.Nordmark@Sun.COM static int	ipif_add_ires_v6(ipif_t *, boolean_t);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
8211042SErik.Nordmark@Sun.COM  * This function is called when an application does not specify an interface
8311042SErik.Nordmark@Sun.COM  * to be used for multicast traffic.  It calls ire_lookup_multi_v6() to look
844459Skcpoon  * for an interface route for the specified multicast group.  Doing
854459Skcpoon  * this allows the administrator to add prefix routes for multicast to
864459Skcpoon  * indicate which interface to be used for multicast traffic in the above
874459Skcpoon  * scenario.  The route could be for all multicast (ff00::/8), for a single
884459Skcpoon  * multicast group (a /128 route) or anything in between.  If there is no
894459Skcpoon  * such multicast route, we just find any multicast capable interface and
904459Skcpoon  * return it.
9111042SErik.Nordmark@Sun.COM  *
9211042SErik.Nordmark@Sun.COM  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
9311042SErik.Nordmark@Sun.COM  * unicast table. This is used by CGTP.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate ill_t *
ill_lookup_group_v6(const in6_addr_t * group,zoneid_t zoneid,ip_stack_t * ipst,boolean_t * multirtp,in6_addr_t * setsrcp)9611042SErik.Nordmark@Sun.COM ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst,
9711042SErik.Nordmark@Sun.COM     boolean_t *multirtp, in6_addr_t *setsrcp)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	ill_t	*ill;
1000Sstevel@tonic-gate 
10111042SErik.Nordmark@Sun.COM 	ill = ire_lookup_multi_ill_v6(group, zoneid, ipst, multirtp, setsrcp);
10211042SErik.Nordmark@Sun.COM 	if (ill != NULL)
1034459Skcpoon 		return (ill);
1044459Skcpoon 
10511042SErik.Nordmark@Sun.COM 	return (ill_lookup_multicast(ipst, zoneid, B_TRUE));
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Look for an ipif with the specified interface address and destination.
1100Sstevel@tonic-gate  * The destination address is used only for matching point-to-point interfaces.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate static ipif_t *
ipif_lookup_interface_v6(const in6_addr_t * if_addr,const in6_addr_t * dst,ip_stack_t * ipst)1130Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
11411042SErik.Nordmark@Sun.COM     ip_stack_t *ipst)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	ipif_t	*ipif;
1170Sstevel@tonic-gate 	ill_t	*ill;
1180Sstevel@tonic-gate 	ill_walk_context_t ctx;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/*
1210Sstevel@tonic-gate 	 * First match all the point-to-point interfaces
1220Sstevel@tonic-gate 	 * before looking at non-point-to-point interfaces.
1230Sstevel@tonic-gate 	 * This is done to avoid returning non-point-to-point
1240Sstevel@tonic-gate 	 * ipif instead of unnumbered point-to-point ipif.
1250Sstevel@tonic-gate 	 */
1263448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1273448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
1280Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1290Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
1302733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
1312733Snordmark 		    ipif = ipif->ipif_next) {
1320Sstevel@tonic-gate 			/* Allow the ipif to be down */
1330Sstevel@tonic-gate 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
1340Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1350Sstevel@tonic-gate 			    if_addr)) &&
1360Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
1370Sstevel@tonic-gate 			    dst))) {
13811042SErik.Nordmark@Sun.COM 				if (!IPIF_IS_CONDEMNED(ipif)) {
1390Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
1400Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
1413448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
1420Sstevel@tonic-gate 					return (ipif);
1430Sstevel@tonic-gate 				}
1440Sstevel@tonic-gate 			}
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
1470Sstevel@tonic-gate 	}
1483448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
1490Sstevel@tonic-gate 	/* lookup the ipif based on interface address */
15011042SErik.Nordmark@Sun.COM 	ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, ipst);
1510Sstevel@tonic-gate 	ASSERT(ipif == NULL || ipif->ipif_isv6);
1520Sstevel@tonic-gate 	return (ipif);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1568485SPeter.Memishian@Sun.COM  * Common function for ipif_lookup_addr_v6() and ipif_lookup_addr_exact_v6().
1570Sstevel@tonic-gate  */
1588485SPeter.Memishian@Sun.COM static ipif_t *
ipif_lookup_addr_common_v6(const in6_addr_t * addr,ill_t * match_ill,uint32_t match_flags,zoneid_t zoneid,ip_stack_t * ipst)1598485SPeter.Memishian@Sun.COM ipif_lookup_addr_common_v6(const in6_addr_t *addr, ill_t *match_ill,
16011042SErik.Nordmark@Sun.COM     uint32_t match_flags, zoneid_t zoneid, ip_stack_t *ipst)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	ipif_t	*ipif;
1630Sstevel@tonic-gate 	ill_t	*ill;
1640Sstevel@tonic-gate 	boolean_t  ptp = B_FALSE;
1650Sstevel@tonic-gate 	ill_walk_context_t ctx;
16611042SErik.Nordmark@Sun.COM 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
16711042SErik.Nordmark@Sun.COM 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
1680Sstevel@tonic-gate 
1693448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Repeat twice, first based on local addresses and
1720Sstevel@tonic-gate 	 * next time for pointopoint.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate repeat:
1753448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
1760Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1778485SPeter.Memishian@Sun.COM 		if (match_ill != NULL && ill != match_ill &&
1788485SPeter.Memishian@Sun.COM 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
1790Sstevel@tonic-gate 			continue;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
1822733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
1832733Snordmark 		    ipif = ipif->ipif_next) {
1841676Sjpk 			if (zoneid != ALL_ZONES &&
1851676Sjpk 			    ipif->ipif_zoneid != zoneid &&
1861676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
1870Sstevel@tonic-gate 				continue;
18811042SErik.Nordmark@Sun.COM 
18911042SErik.Nordmark@Sun.COM 			if (no_duplicate &&
19011042SErik.Nordmark@Sun.COM 			    !(ipif->ipif_flags & IPIF_UP)) {
19111042SErik.Nordmark@Sun.COM 				continue;
19211042SErik.Nordmark@Sun.COM 			}
19311042SErik.Nordmark@Sun.COM 
1940Sstevel@tonic-gate 			/* Allow the ipif to be down */
1950Sstevel@tonic-gate 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
1960Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, addr) &&
1970Sstevel@tonic-gate 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
1980Sstevel@tonic-gate 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
1990Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
2000Sstevel@tonic-gate 			    addr))) {
20111042SErik.Nordmark@Sun.COM 				if (!IPIF_IS_CONDEMNED(ipif)) {
2020Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
2030Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
2043448Sdh155122 					rw_exit(&ipst->ips_ill_g_lock);
2050Sstevel@tonic-gate 					return (ipif);
2060Sstevel@tonic-gate 				}
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2122733Snordmark 	/* If we already did the ptp case, then we are done */
2130Sstevel@tonic-gate 	if (ptp) {
2143448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
2150Sstevel@tonic-gate 		return (NULL);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	ptp = B_TRUE;
2180Sstevel@tonic-gate 	goto repeat;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2228485SPeter.Memishian@Sun.COM  * Lookup an ipif with the specified address.  For point-to-point links we
2238485SPeter.Memishian@Sun.COM  * look for matches on either the destination address or the local address,
2248485SPeter.Memishian@Sun.COM  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
2258485SPeter.Memishian@Sun.COM  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
2268485SPeter.Memishian@Sun.COM  * (or illgrp if `match_ill' is in an IPMP group).
2278485SPeter.Memishian@Sun.COM  */
2288485SPeter.Memishian@Sun.COM ipif_t *
ipif_lookup_addr_v6(const in6_addr_t * addr,ill_t * match_ill,zoneid_t zoneid,ip_stack_t * ipst)2298485SPeter.Memishian@Sun.COM ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
23011042SErik.Nordmark@Sun.COM     ip_stack_t *ipst)
2318485SPeter.Memishian@Sun.COM {
23211042SErik.Nordmark@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill, IPIF_MATCH_ILLGRP,
23311042SErik.Nordmark@Sun.COM 	    zoneid, ipst));
23411042SErik.Nordmark@Sun.COM }
23511042SErik.Nordmark@Sun.COM 
23611042SErik.Nordmark@Sun.COM /*
23711042SErik.Nordmark@Sun.COM  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
23811042SErik.Nordmark@Sun.COM  * except that we will only return an address if it is not marked as
23911042SErik.Nordmark@Sun.COM  * IPIF_DUPLICATE
24011042SErik.Nordmark@Sun.COM  */
24111042SErik.Nordmark@Sun.COM ipif_t *
ipif_lookup_addr_nondup_v6(const in6_addr_t * addr,ill_t * match_ill,zoneid_t zoneid,ip_stack_t * ipst)24211042SErik.Nordmark@Sun.COM ipif_lookup_addr_nondup_v6(const in6_addr_t *addr, ill_t *match_ill,
24311042SErik.Nordmark@Sun.COM     zoneid_t zoneid, ip_stack_t *ipst)
24411042SErik.Nordmark@Sun.COM {
24511042SErik.Nordmark@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill,
24611042SErik.Nordmark@Sun.COM 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP), zoneid,
24711042SErik.Nordmark@Sun.COM 	    ipst));
2488485SPeter.Memishian@Sun.COM }
2498485SPeter.Memishian@Sun.COM 
2508485SPeter.Memishian@Sun.COM /*
2518485SPeter.Memishian@Sun.COM  * Special abbreviated version of ipif_lookup_addr_v6() that doesn't match
2528485SPeter.Memishian@Sun.COM  * `match_ill' across the IPMP group.  This function is only needed in some
2538485SPeter.Memishian@Sun.COM  * corner-cases; almost everything should use ipif_lookup_addr_v6().
2548485SPeter.Memishian@Sun.COM  */
2558485SPeter.Memishian@Sun.COM ipif_t *
ipif_lookup_addr_exact_v6(const in6_addr_t * addr,ill_t * match_ill,ip_stack_t * ipst)2568485SPeter.Memishian@Sun.COM ipif_lookup_addr_exact_v6(const in6_addr_t *addr, ill_t *match_ill,
2578485SPeter.Memishian@Sun.COM     ip_stack_t *ipst)
2588485SPeter.Memishian@Sun.COM {
2598485SPeter.Memishian@Sun.COM 	ASSERT(match_ill != NULL);
26011042SErik.Nordmark@Sun.COM 	return (ipif_lookup_addr_common_v6(addr, match_ill, 0, ALL_ZONES,
26111042SErik.Nordmark@Sun.COM 	    ipst));
2628485SPeter.Memishian@Sun.COM }
2638485SPeter.Memishian@Sun.COM 
2648485SPeter.Memishian@Sun.COM /*
2652733Snordmark  * Look for an ipif with the specified address. For point-point links
2662733Snordmark  * we look for matches on either the destination address and the local
2672733Snordmark  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
2682733Snordmark  * is set.
2698485SPeter.Memishian@Sun.COM  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
2708485SPeter.Memishian@Sun.COM  * ill (or illgrp if `match_ill' is in an IPMP group).
2712733Snordmark  * Return the zoneid for the ipif. ALL_ZONES if none found.
2722733Snordmark  */
2732733Snordmark zoneid_t
ipif_lookup_addr_zoneid_v6(const in6_addr_t * addr,ill_t * match_ill,ip_stack_t * ipst)2743448Sdh155122 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill,
2753448Sdh155122     ip_stack_t *ipst)
2762733Snordmark {
2772733Snordmark 	ipif_t	*ipif;
2782733Snordmark 	ill_t	*ill;
2792733Snordmark 	boolean_t  ptp = B_FALSE;
2802733Snordmark 	ill_walk_context_t ctx;
2812733Snordmark 	zoneid_t	zoneid;
2822733Snordmark 
2833448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2842733Snordmark 	/*
2852733Snordmark 	 * Repeat twice, first based on local addresses and
2862733Snordmark 	 * next time for pointopoint.
2872733Snordmark 	 */
2882733Snordmark repeat:
2893448Sdh155122 	ill = ILL_START_WALK_V6(&ctx, ipst);
2902733Snordmark 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2918485SPeter.Memishian@Sun.COM 		if (match_ill != NULL && ill != match_ill &&
2928485SPeter.Memishian@Sun.COM 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
2932733Snordmark 			continue;
2942733Snordmark 		}
2952733Snordmark 		mutex_enter(&ill->ill_lock);
2962733Snordmark 		for (ipif = ill->ill_ipif; ipif != NULL;
2972733Snordmark 		    ipif = ipif->ipif_next) {
2982733Snordmark 			/* Allow the ipif to be down */
2992733Snordmark 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
3002733Snordmark 			    &ipif->ipif_v6lcl_addr, addr) &&
3012733Snordmark 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
3022733Snordmark 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
3032733Snordmark 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
3042733Snordmark 			    addr)) &&
3052733Snordmark 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
3062733Snordmark 				zoneid = ipif->ipif_zoneid;
3072733Snordmark 				mutex_exit(&ill->ill_lock);
3083448Sdh155122 				rw_exit(&ipst->ips_ill_g_lock);
3092733Snordmark 				/*
3102733Snordmark 				 * If ipif_zoneid was ALL_ZONES then we have
3112733Snordmark 				 * a trusted extensions shared IP address.
3122733Snordmark 				 * In that case GLOBAL_ZONEID works to send.
3132733Snordmark 				 */
3142733Snordmark 				if (zoneid == ALL_ZONES)
3152733Snordmark 					zoneid = GLOBAL_ZONEID;
3162733Snordmark 				return (zoneid);
3172733Snordmark 			}
3182733Snordmark 		}
3192733Snordmark 		mutex_exit(&ill->ill_lock);
3202733Snordmark 	}
3212733Snordmark 
3222733Snordmark 	/* If we already did the ptp case, then we are done */
3232733Snordmark 	if (ptp) {
3243448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
3252733Snordmark 		return (ALL_ZONES);
3262733Snordmark 	}
3272733Snordmark 	ptp = B_TRUE;
3282733Snordmark 	goto repeat;
3292733Snordmark }
3302733Snordmark 
3312733Snordmark /*
3320Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a local
3330Sstevel@tonic-gate  * interface address.  This is currently only called when an attempt is made
3340Sstevel@tonic-gate  * to set a local address.
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * Does not allow a v4-mapped address, an address that equals the subnet
3370Sstevel@tonic-gate  * anycast address, ... a multicast address, ...
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate boolean_t
ip_local_addr_ok_v6(const in6_addr_t * addr,const in6_addr_t * subnet_mask)3400Sstevel@tonic-gate ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	in6_addr_t subnet;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
3450Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * Don't allow all zeroes or host part, but allow
3490Sstevel@tonic-gate 	 * all ones netmask.
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
3520Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
3530Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
3540Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
3550Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
3560Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr))
3570Sstevel@tonic-gate 		return (B_FALSE);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	return (B_TRUE);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a
3640Sstevel@tonic-gate  * remote/subnet interface address.
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate boolean_t
ip_remote_addr_ok_v6(const in6_addr_t * addr,const in6_addr_t * subnet_mask)3670Sstevel@tonic-gate ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	in6_addr_t subnet;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
3720Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
3750Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
3760Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
3770Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
3780Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr) ||
3790Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
3800Sstevel@tonic-gate 		return (B_FALSE);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	return (B_TRUE);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
38711042SErik.Nordmark@Sun.COM  * ill is passed in to associate it with the correct interface
3880Sstevel@tonic-gate  * (for link-local destinations and gateways).
38911042SErik.Nordmark@Sun.COM  * If ire_arg is set, then we return the held IRE in that location.
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate /* ARGSUSED1 */
3920Sstevel@tonic-gate int
ip_rt_add_v6(const in6_addr_t * dst_addr,const in6_addr_t * mask,const in6_addr_t * gw_addr,const in6_addr_t * src_addr,int flags,ill_t * ill,ire_t ** ire_arg,struct rtsa_s * sp,ip_stack_t * ipst,zoneid_t zoneid)3930Sstevel@tonic-gate ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
3940Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
39511042SErik.Nordmark@Sun.COM     ill_t *ill, ire_t **ire_arg, struct rtsa_s *sp, ip_stack_t *ipst,
39611042SErik.Nordmark@Sun.COM     zoneid_t zoneid)
3970Sstevel@tonic-gate {
39811042SErik.Nordmark@Sun.COM 	ire_t	*ire, *nire;
3990Sstevel@tonic-gate 	ire_t	*gw_ire = NULL;
4000Sstevel@tonic-gate 	ipif_t	*ipif;
4010Sstevel@tonic-gate 	uint_t	type;
4020Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
4031676Sjpk 	tsol_gc_t *gc = NULL;
4041676Sjpk 	tsol_gcgrp_t *gcgrp = NULL;
4051676Sjpk 	boolean_t gcgrp_xtraref = B_FALSE;
40611681SSowmini.Varadhan@Sun.COM 	boolean_t unbound = B_FALSE;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (ire_arg != NULL)
4090Sstevel@tonic-gate 		*ire_arg = NULL;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * Prevent routes with a zero gateway from being created (since
4130Sstevel@tonic-gate 	 * interfaces can currently be plumbed and brought up with no assigned
4140Sstevel@tonic-gate 	 * address).
4150Sstevel@tonic-gate 	 */
4160Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
4170Sstevel@tonic-gate 		return (ENETUNREACH);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
4210Sstevel@tonic-gate 	 * to all ones (regardless if one was supplied).
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	if (flags & RTF_HOST)
4240Sstevel@tonic-gate 		mask = &ipv6_all_ones;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	/*
4270Sstevel@tonic-gate 	 * Get the ipif, if any, corresponding to the gw_addr
42811042SErik.Nordmark@Sun.COM 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
42911042SErik.Nordmark@Sun.COM 	 * we match on the gatway and destination to handle unnumbered pt-pt
43011042SErik.Nordmark@Sun.COM 	 * interfaces.
4310Sstevel@tonic-gate 	 */
43211042SErik.Nordmark@Sun.COM 	if (ill != NULL)
43311042SErik.Nordmark@Sun.COM 		ipif = ipif_lookup_addr_v6(gw_addr, ill, ALL_ZONES, ipst);
43411042SErik.Nordmark@Sun.COM 	else
43511042SErik.Nordmark@Sun.COM 		ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, ipst);
43611042SErik.Nordmark@Sun.COM 	if (ipif != NULL) {
43711042SErik.Nordmark@Sun.COM 		if (IS_VNI(ipif->ipif_ill)) {
43811042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
43911042SErik.Nordmark@Sun.COM 			return (EINVAL);
44011042SErik.Nordmark@Sun.COM 		}
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * GateD will attempt to create routes with a loopback interface
4450Sstevel@tonic-gate 	 * address as the gateway and with RTF_GATEWAY set.  We allow
4460Sstevel@tonic-gate 	 * these routes to be added, but create them as interface routes
4470Sstevel@tonic-gate 	 * since the gateway is an interface address.
4480Sstevel@tonic-gate 	 */
4491822Srk129064 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
4500Sstevel@tonic-gate 		flags &= ~RTF_GATEWAY;
4511822Srk129064 		if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) &&
4521822Srk129064 		    IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) &&
4531822Srk129064 		    IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) {
45411042SErik.Nordmark@Sun.COM 			ire = ire_ftable_lookup_v6(dst_addr, 0, 0, IRE_LOOPBACK,
45511042SErik.Nordmark@Sun.COM 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
45611042SErik.Nordmark@Sun.COM 			    NULL);
4571822Srk129064 			if (ire != NULL) {
4581822Srk129064 				ire_refrele(ire);
45911042SErik.Nordmark@Sun.COM 				ipif_refrele(ipif);
4601822Srk129064 				return (EEXIST);
4611822Srk129064 			}
46211042SErik.Nordmark@Sun.COM 			ip1dbg(("ip_rt_add_v6: 0x%p creating IRE 0x%x"
4631822Srk129064 			    "for 0x%x\n", (void *)ipif,
4641822Srk129064 			    ipif->ipif_ire_type,
4651822Srk129064 			    ntohl(ipif->ipif_lcl_addr)));
4661822Srk129064 			ire = ire_create_v6(
4671822Srk129064 			    dst_addr,
4681822Srk129064 			    mask,
4691822Srk129064 			    NULL,
47011042SErik.Nordmark@Sun.COM 			    ipif->ipif_ire_type,	/* LOOPBACK */
47111042SErik.Nordmark@Sun.COM 			    ipif->ipif_ill,
47211042SErik.Nordmark@Sun.COM 			    zoneid,
47311042SErik.Nordmark@Sun.COM 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
4743448Sdh155122 			    NULL,
4753448Sdh155122 			    ipst);
47611042SErik.Nordmark@Sun.COM 
4771822Srk129064 			if (ire == NULL) {
47811042SErik.Nordmark@Sun.COM 				ipif_refrele(ipif);
47911042SErik.Nordmark@Sun.COM 				return (ENOMEM);
48011042SErik.Nordmark@Sun.COM 			}
48111042SErik.Nordmark@Sun.COM 			/* src address assigned by the caller? */
48211042SErik.Nordmark@Sun.COM 			if ((flags & RTF_SETSRC) &&
48311042SErik.Nordmark@Sun.COM 			    !IN6_IS_ADDR_UNSPECIFIED(src_addr))
48411042SErik.Nordmark@Sun.COM 				ire->ire_setsrc_addr_v6 = *src_addr;
48511042SErik.Nordmark@Sun.COM 
48611042SErik.Nordmark@Sun.COM 			nire = ire_add(ire);
48711042SErik.Nordmark@Sun.COM 			if (nire == NULL) {
48811042SErik.Nordmark@Sun.COM 				/*
48911042SErik.Nordmark@Sun.COM 				 * In the result of failure, ire_add() will have
49011042SErik.Nordmark@Sun.COM 				 * already deleted the ire in question, so there
49111042SErik.Nordmark@Sun.COM 				 * is no need to do that here.
49211042SErik.Nordmark@Sun.COM 				 */
49311042SErik.Nordmark@Sun.COM 				ipif_refrele(ipif);
4941822Srk129064 				return (ENOMEM);
4951822Srk129064 			}
4961822Srk129064 			/*
49711042SErik.Nordmark@Sun.COM 			 * Check if it was a duplicate entry. This handles
49811042SErik.Nordmark@Sun.COM 			 * the case of two racing route adds for the same route
4991822Srk129064 			 */
50011042SErik.Nordmark@Sun.COM 			if (nire != ire) {
50111042SErik.Nordmark@Sun.COM 				ASSERT(nire->ire_identical_ref > 1);
50211042SErik.Nordmark@Sun.COM 				ire_delete(nire);
50311042SErik.Nordmark@Sun.COM 				ire_refrele(nire);
5041822Srk129064 				ipif_refrele(ipif);
50511042SErik.Nordmark@Sun.COM 				return (EEXIST);
50611042SErik.Nordmark@Sun.COM 			}
50711042SErik.Nordmark@Sun.COM 			ire = nire;
50811042SErik.Nordmark@Sun.COM 			goto save_ire;
5091822Srk129064 		}
5101822Srk129064 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
51311042SErik.Nordmark@Sun.COM 	 * The routes for multicast with CGTP are quite special in that
51411042SErik.Nordmark@Sun.COM 	 * the gateway is the local interface address, yet RTF_GATEWAY
51511042SErik.Nordmark@Sun.COM 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
51611042SErik.Nordmark@Sun.COM 	 * this undocumented and unusual use of multicast routes.
51711042SErik.Nordmark@Sun.COM 	 */
51811042SErik.Nordmark@Sun.COM 	if ((flags & RTF_MULTIRT) && ipif != NULL)
51911042SErik.Nordmark@Sun.COM 		flags &= ~RTF_GATEWAY;
52011042SErik.Nordmark@Sun.COM 
52111042SErik.Nordmark@Sun.COM 	/*
5220Sstevel@tonic-gate 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5230Sstevel@tonic-gate 	 * and the gateway address provided is one of the system's interface
5240Sstevel@tonic-gate 	 * addresses.  By using the routing socket interface and supplying an
5250Sstevel@tonic-gate 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5260Sstevel@tonic-gate 	 * specifying an interface route to be created is available which uses
5270Sstevel@tonic-gate 	 * the interface index that specifies the outgoing interface rather than
5280Sstevel@tonic-gate 	 * the address of an outgoing interface (which may not be able to
5290Sstevel@tonic-gate 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5300Sstevel@tonic-gate 	 * flag, routes can be specified which not only specify the next-hop to
5310Sstevel@tonic-gate 	 * be used when routing to a certain prefix, but also which outgoing
5320Sstevel@tonic-gate 	 * interface should be used.
5330Sstevel@tonic-gate 	 *
5340Sstevel@tonic-gate 	 * Previously, interfaces would have unique addresses assigned to them
5350Sstevel@tonic-gate 	 * and so the address assigned to a particular interface could be used
5360Sstevel@tonic-gate 	 * to identify a particular interface.  One exception to this was the
5370Sstevel@tonic-gate 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5380Sstevel@tonic-gate 	 *
5390Sstevel@tonic-gate 	 * With the advent of IPv6 and its link-local addresses, this
5400Sstevel@tonic-gate 	 * restriction was relaxed and interfaces could share addresses between
5410Sstevel@tonic-gate 	 * themselves.  In fact, typically all of the link-local interfaces on
5420Sstevel@tonic-gate 	 * an IPv6 node or router will have the same link-local address.  In
5430Sstevel@tonic-gate 	 * order to differentiate between these interfaces, the use of an
5440Sstevel@tonic-gate 	 * interface index is necessary and this index can be carried inside a
5450Sstevel@tonic-gate 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5460Sstevel@tonic-gate 	 * of using the interface index, however, is that all of the ipif's that
5470Sstevel@tonic-gate 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5480Sstevel@tonic-gate 	 * cannot be used to differentiate between ipif's (or logical
5490Sstevel@tonic-gate 	 * interfaces) that belong to the same ill (physical interface).
5500Sstevel@tonic-gate 	 *
5510Sstevel@tonic-gate 	 * For example, in the following case involving IPv4 interfaces and
5520Sstevel@tonic-gate 	 * logical interfaces
5530Sstevel@tonic-gate 	 *
5540Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
55511042SErik.Nordmark@Sun.COM 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
55611042SErik.Nordmark@Sun.COM 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5570Sstevel@tonic-gate 	 *
5580Sstevel@tonic-gate 	 * the ipif's corresponding to each of these interface routes can be
5590Sstevel@tonic-gate 	 * uniquely identified by the "gateway" (actually interface address).
5600Sstevel@tonic-gate 	 *
5610Sstevel@tonic-gate 	 * In this case involving multiple IPv6 default routes to a particular
5620Sstevel@tonic-gate 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5630Sstevel@tonic-gate 	 * default route is of interest:
5640Sstevel@tonic-gate 	 *
5650Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5660Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5670Sstevel@tonic-gate 	 */
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/* RTF_GATEWAY not set */
5700Sstevel@tonic-gate 	if (!(flags & RTF_GATEWAY)) {
5711676Sjpk 		if (sp != NULL) {
5721676Sjpk 			ip2dbg(("ip_rt_add_v6: gateway security attributes "
5731676Sjpk 			    "cannot be set with interface route\n"));
57411042SErik.Nordmark@Sun.COM 			if (ipif != NULL)
5751676Sjpk 				ipif_refrele(ipif);
5761676Sjpk 			return (EINVAL);
5771676Sjpk 		}
5781676Sjpk 
5790Sstevel@tonic-gate 		/*
58011042SErik.Nordmark@Sun.COM 		 * Whether or not ill (RTA_IFP) is set, we require that
58111042SErik.Nordmark@Sun.COM 		 * the gateway is one of our local addresses.
5820Sstevel@tonic-gate 		 */
58311042SErik.Nordmark@Sun.COM 		if (ipif == NULL)
58411042SErik.Nordmark@Sun.COM 			return (ENETUNREACH);
58511042SErik.Nordmark@Sun.COM 
58611042SErik.Nordmark@Sun.COM 		/*
58711042SErik.Nordmark@Sun.COM 		 * We use MATCH_IRE_ILL here. If the caller specified an
58811042SErik.Nordmark@Sun.COM 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
58911042SErik.Nordmark@Sun.COM 		 * we use the ill derived from the gateway address.
59011042SErik.Nordmark@Sun.COM 		 * We can always match the gateway address since we record it
59111042SErik.Nordmark@Sun.COM 		 * in ire_gateway_addr.
59211042SErik.Nordmark@Sun.COM 		 * We don't allow RTA_IFP to specify a different ill than the
59311042SErik.Nordmark@Sun.COM 		 * one matching the ipif to make sure we can delete the route.
59411042SErik.Nordmark@Sun.COM 		 */
59511042SErik.Nordmark@Sun.COM 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
59611042SErik.Nordmark@Sun.COM 		if (ill == NULL) {
59711042SErik.Nordmark@Sun.COM 			ill = ipif->ipif_ill;
59811042SErik.Nordmark@Sun.COM 		} else if (ill != ipif->ipif_ill) {
59911042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
60011042SErik.Nordmark@Sun.COM 			return (EINVAL);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		/*
6040Sstevel@tonic-gate 		 * We check for an existing entry at this point.
6050Sstevel@tonic-gate 		 */
6060Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
60711042SErik.Nordmark@Sun.COM 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr,
60811042SErik.Nordmark@Sun.COM 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
60911042SErik.Nordmark@Sun.COM 		    NULL);
6100Sstevel@tonic-gate 		if (ire != NULL) {
6110Sstevel@tonic-gate 			ire_refrele(ire);
61211042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
6130Sstevel@tonic-gate 			return (EEXIST);
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		/*
61711916SErik.Nordmark@Sun.COM 		 * Some software (for example, GateD and Sun Cluster) attempts
61811916SErik.Nordmark@Sun.COM 		 * to create (what amount to) IRE_PREFIX routes with the
61911916SErik.Nordmark@Sun.COM 		 * loopback address as the gateway.  This is primarily done to
62011916SErik.Nordmark@Sun.COM 		 * set up prefixes with the RTF_REJECT flag set (for example,
62111916SErik.Nordmark@Sun.COM 		 * when generating aggregate routes). We also OR in the
62211916SErik.Nordmark@Sun.COM 		 * RTF_BLACKHOLE flag as these interface routes, by
62311916SErik.Nordmark@Sun.COM 		 * definition, can only be that.
62411916SErik.Nordmark@Sun.COM 		 *
62511916SErik.Nordmark@Sun.COM 		 * If the IRE type (as defined by ill->ill_net_type) would be
62611916SErik.Nordmark@Sun.COM 		 * IRE_LOOPBACK, then we map the request into a
62711916SErik.Nordmark@Sun.COM 		 * IRE_IF_NORESOLVER.
62811916SErik.Nordmark@Sun.COM 		 *
62911916SErik.Nordmark@Sun.COM 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
63011916SErik.Nordmark@Sun.COM 		 * routine, but rather using ire_create_v6() directly.
63111916SErik.Nordmark@Sun.COM 		 */
63211916SErik.Nordmark@Sun.COM 		type = ill->ill_net_type;
63311916SErik.Nordmark@Sun.COM 		if (type == IRE_LOOPBACK) {
63411916SErik.Nordmark@Sun.COM 			type = IRE_IF_NORESOLVER;
63511916SErik.Nordmark@Sun.COM 			flags |= RTF_BLACKHOLE;
63611916SErik.Nordmark@Sun.COM 		}
63711916SErik.Nordmark@Sun.COM 
63811916SErik.Nordmark@Sun.COM 		/*
63911916SErik.Nordmark@Sun.COM 		 * Create a copy of the IRE_IF_NORESOLVER or
64011042SErik.Nordmark@Sun.COM 		 * IRE_IF_RESOLVER with the modified address, netmask, and
64111042SErik.Nordmark@Sun.COM 		 * gateway.
6420Sstevel@tonic-gate 		 */
6430Sstevel@tonic-gate 		ire = ire_create_v6(
6440Sstevel@tonic-gate 		    dst_addr,
6450Sstevel@tonic-gate 		    mask,
64611042SErik.Nordmark@Sun.COM 		    gw_addr,
64711916SErik.Nordmark@Sun.COM 		    type,
64811042SErik.Nordmark@Sun.COM 		    ill,
64911042SErik.Nordmark@Sun.COM 		    zoneid,
6500Sstevel@tonic-gate 		    flags,
6513448Sdh155122 		    NULL,
6523448Sdh155122 		    ipst);
6530Sstevel@tonic-gate 		if (ire == NULL) {
65411042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
6550Sstevel@tonic-gate 			return (ENOMEM);
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 
65811042SErik.Nordmark@Sun.COM 		/* src address assigned by the caller? */
65911042SErik.Nordmark@Sun.COM 		if ((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr))
66011042SErik.Nordmark@Sun.COM 			ire->ire_setsrc_addr_v6 = *src_addr;
66111042SErik.Nordmark@Sun.COM 
66211042SErik.Nordmark@Sun.COM 		nire = ire_add(ire);
66311042SErik.Nordmark@Sun.COM 		if (nire == NULL) {
66411042SErik.Nordmark@Sun.COM 			/*
66511042SErik.Nordmark@Sun.COM 			 * In the result of failure, ire_add() will have
66611042SErik.Nordmark@Sun.COM 			 * already deleted the ire in question, so there
66711042SErik.Nordmark@Sun.COM 			 * is no need to do that here.
66811042SErik.Nordmark@Sun.COM 			 */
66911042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
67011042SErik.Nordmark@Sun.COM 			return (ENOMEM);
67111042SErik.Nordmark@Sun.COM 		}
6720Sstevel@tonic-gate 		/*
67311042SErik.Nordmark@Sun.COM 		 * Check if it was a duplicate entry. This handles
67411042SErik.Nordmark@Sun.COM 		 * the case of two racing route adds for the same route
6750Sstevel@tonic-gate 		 */
67611042SErik.Nordmark@Sun.COM 		if (nire != ire) {
67711042SErik.Nordmark@Sun.COM 			ASSERT(nire->ire_identical_ref > 1);
67811042SErik.Nordmark@Sun.COM 			ire_delete(nire);
67911042SErik.Nordmark@Sun.COM 			ire_refrele(nire);
6800Sstevel@tonic-gate 			ipif_refrele(ipif);
68111042SErik.Nordmark@Sun.COM 			return (EEXIST);
68211042SErik.Nordmark@Sun.COM 		}
68311042SErik.Nordmark@Sun.COM 		ire = nire;
68411042SErik.Nordmark@Sun.COM 		goto save_ire;
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * Get an interface IRE for the specified gateway.
6890Sstevel@tonic-gate 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6900Sstevel@tonic-gate 	 * gateway, it is currently unreachable and we fail the request
69111430SErik.Nordmark@Sun.COM 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
69211430SErik.Nordmark@Sun.COM 	 * is an IRE_LOCAL or IRE_LOOPBACK.
69311042SErik.Nordmark@Sun.COM 	 * If RTA_IFP was specified we look on that particular ill.
6940Sstevel@tonic-gate 	 */
69511042SErik.Nordmark@Sun.COM 	if (ill != NULL)
6960Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
69711042SErik.Nordmark@Sun.COM 
69811042SErik.Nordmark@Sun.COM 	/* Check whether the gateway is reachable. */
69911430SErik.Nordmark@Sun.COM again:
70011430SErik.Nordmark@Sun.COM 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
70111042SErik.Nordmark@Sun.COM 	if (flags & RTF_INDIRECT)
70211042SErik.Nordmark@Sun.COM 		type |= IRE_OFFLINK;
70311042SErik.Nordmark@Sun.COM 
70411042SErik.Nordmark@Sun.COM 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, type, ill,
70511042SErik.Nordmark@Sun.COM 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
70611042SErik.Nordmark@Sun.COM 	if (gw_ire == NULL) {
70711430SErik.Nordmark@Sun.COM 		/*
70811430SErik.Nordmark@Sun.COM 		 * With IPMP, we allow host routes to influence in.mpathd's
70911430SErik.Nordmark@Sun.COM 		 * target selection.  However, if the test addresses are on
71011430SErik.Nordmark@Sun.COM 		 * their own network, the above lookup will fail since the
71111430SErik.Nordmark@Sun.COM 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
71211430SErik.Nordmark@Sun.COM 		 * hidden test IREs to be found and try again.
71311430SErik.Nordmark@Sun.COM 		 */
71411430SErik.Nordmark@Sun.COM 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
71511430SErik.Nordmark@Sun.COM 			match_flags |= MATCH_IRE_TESTHIDDEN;
71611430SErik.Nordmark@Sun.COM 			goto again;
71711430SErik.Nordmark@Sun.COM 		}
71811430SErik.Nordmark@Sun.COM 		if (ipif != NULL)
71911430SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
72011430SErik.Nordmark@Sun.COM 		return (ENETUNREACH);
72111430SErik.Nordmark@Sun.COM 	}
72211430SErik.Nordmark@Sun.COM 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
72311430SErik.Nordmark@Sun.COM 		ire_refrele(gw_ire);
72411042SErik.Nordmark@Sun.COM 		if (ipif != NULL)
72511042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
7260Sstevel@tonic-gate 		return (ENETUNREACH);
72711042SErik.Nordmark@Sun.COM 	}
72811681SSowmini.Varadhan@Sun.COM 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
72911681SSowmini.Varadhan@Sun.COM 		unbound = B_TRUE;
73011681SSowmini.Varadhan@Sun.COM 		if (ipst->ips_ipv6_strict_src_multihoming > 0)
73111681SSowmini.Varadhan@Sun.COM 			ill = gw_ire->ire_ill;
73211681SSowmini.Varadhan@Sun.COM 	}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/*
7350Sstevel@tonic-gate 	 * We create one of three types of IREs as a result of this request
7360Sstevel@tonic-gate 	 * based on the netmask.  A netmask of all ones (which is automatically
7370Sstevel@tonic-gate 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7380Sstevel@tonic-gate 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7390Sstevel@tonic-gate 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7400Sstevel@tonic-gate 	 * destination prefix.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
7430Sstevel@tonic-gate 		type = IRE_HOST;
7440Sstevel@tonic-gate 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
7450Sstevel@tonic-gate 		type = IRE_DEFAULT;
7460Sstevel@tonic-gate 	else
7470Sstevel@tonic-gate 		type = IRE_PREFIX;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	/* check for a duplicate entry */
75011042SErik.Nordmark@Sun.COM 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ill,
75111042SErik.Nordmark@Sun.COM 	    ALL_ZONES, NULL,
75211042SErik.Nordmark@Sun.COM 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, 0, ipst, NULL);
7530Sstevel@tonic-gate 	if (ire != NULL) {
75411042SErik.Nordmark@Sun.COM 		if (ipif != NULL)
75511042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
7560Sstevel@tonic-gate 		ire_refrele(gw_ire);
7570Sstevel@tonic-gate 		ire_refrele(ire);
7580Sstevel@tonic-gate 		return (EEXIST);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7611676Sjpk 	/* Security attribute exists */
7621676Sjpk 	if (sp != NULL) {
7631676Sjpk 		tsol_gcgrp_addr_t ga;
7641676Sjpk 
7651676Sjpk 		/* find or create the gateway credentials group */
7661676Sjpk 		ga.ga_af = AF_INET6;
7671676Sjpk 		ga.ga_addr = *gw_addr;
7681676Sjpk 
7691676Sjpk 		/* we hold reference to it upon success */
7701676Sjpk 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7711676Sjpk 		if (gcgrp == NULL) {
77211042SErik.Nordmark@Sun.COM 			if (ipif != NULL)
77311042SErik.Nordmark@Sun.COM 				ipif_refrele(ipif);
7741676Sjpk 			ire_refrele(gw_ire);
7751676Sjpk 			return (ENOMEM);
7761676Sjpk 		}
7771676Sjpk 
7781676Sjpk 		/*
7791676Sjpk 		 * Create and add the security attribute to the group; a
7801676Sjpk 		 * reference to the group is made upon allocating a new
7811676Sjpk 		 * entry successfully.  If it finds an already-existing
7821676Sjpk 		 * entry for the security attribute in the group, it simply
7831676Sjpk 		 * returns it and no new reference is made to the group.
7841676Sjpk 		 */
7851676Sjpk 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7861676Sjpk 		if (gc == NULL) {
7871676Sjpk 			/* release reference held by gcgrp_lookup */
7881676Sjpk 			GCGRP_REFRELE(gcgrp);
78911042SErik.Nordmark@Sun.COM 			if (ipif != NULL)
79011042SErik.Nordmark@Sun.COM 				ipif_refrele(ipif);
7911676Sjpk 			ire_refrele(gw_ire);
7921676Sjpk 			return (ENOMEM);
7931676Sjpk 		}
7941676Sjpk 	}
7951676Sjpk 
7960Sstevel@tonic-gate 	/* Create the IRE. */
7970Sstevel@tonic-gate 	ire = ire_create_v6(
7980Sstevel@tonic-gate 	    dst_addr,				/* dest address */
7990Sstevel@tonic-gate 	    mask,				/* mask */
8000Sstevel@tonic-gate 	    gw_addr,				/* gateway address */
8010Sstevel@tonic-gate 	    (ushort_t)type,			/* IRE type */
80211042SErik.Nordmark@Sun.COM 	    ill,
80311042SErik.Nordmark@Sun.COM 	    zoneid,
8040Sstevel@tonic-gate 	    flags,
8051676Sjpk 	    gc,					/* security attribute */
8063448Sdh155122 	    ipst);
8073448Sdh155122 
8081676Sjpk 	/*
8091676Sjpk 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
8101676Sjpk 	 * reference to the 'gcgrp'. We can now release the extra reference
8111676Sjpk 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
8121676Sjpk 	 */
8131676Sjpk 	if (gcgrp_xtraref)
8141676Sjpk 		GCGRP_REFRELE(gcgrp);
8150Sstevel@tonic-gate 	if (ire == NULL) {
8161676Sjpk 		if (gc != NULL)
8171676Sjpk 			GC_REFRELE(gc);
81811042SErik.Nordmark@Sun.COM 		if (ipif != NULL)
81911042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
8200Sstevel@tonic-gate 		ire_refrele(gw_ire);
8210Sstevel@tonic-gate 		return (ENOMEM);
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
82411042SErik.Nordmark@Sun.COM 	/* src address assigned by the caller? */
82511042SErik.Nordmark@Sun.COM 	if ((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr))
82611042SErik.Nordmark@Sun.COM 		ire->ire_setsrc_addr_v6 = *src_addr;
82711042SErik.Nordmark@Sun.COM 
82811681SSowmini.Varadhan@Sun.COM 	ire->ire_unbound = unbound;
82911681SSowmini.Varadhan@Sun.COM 
8300Sstevel@tonic-gate 	/*
8310Sstevel@tonic-gate 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
8320Sstevel@tonic-gate 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
8330Sstevel@tonic-gate 	 */
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/* Add the new IRE. */
83611042SErik.Nordmark@Sun.COM 	nire = ire_add(ire);
83711042SErik.Nordmark@Sun.COM 	if (nire == NULL) {
83811042SErik.Nordmark@Sun.COM 		/*
83911042SErik.Nordmark@Sun.COM 		 * In the result of failure, ire_add() will have
84011042SErik.Nordmark@Sun.COM 		 * already deleted the ire in question, so there
84111042SErik.Nordmark@Sun.COM 		 * is no need to do that here.
84211042SErik.Nordmark@Sun.COM 		 */
84311042SErik.Nordmark@Sun.COM 		if (ipif != NULL)
84411042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
84511042SErik.Nordmark@Sun.COM 		ire_refrele(gw_ire);
84611042SErik.Nordmark@Sun.COM 		return (ENOMEM);
84711042SErik.Nordmark@Sun.COM 	}
8480Sstevel@tonic-gate 	/*
84911042SErik.Nordmark@Sun.COM 	 * Check if it was a duplicate entry. This handles
85011042SErik.Nordmark@Sun.COM 	 * the case of two racing route adds for the same route
8510Sstevel@tonic-gate 	 */
85211042SErik.Nordmark@Sun.COM 	if (nire != ire) {
85311042SErik.Nordmark@Sun.COM 		ASSERT(nire->ire_identical_ref > 1);
85411042SErik.Nordmark@Sun.COM 		ire_delete(nire);
85511042SErik.Nordmark@Sun.COM 		ire_refrele(nire);
85611042SErik.Nordmark@Sun.COM 		if (ipif != NULL)
85711042SErik.Nordmark@Sun.COM 			ipif_refrele(ipif);
8580Sstevel@tonic-gate 		ire_refrele(gw_ire);
85911042SErik.Nordmark@Sun.COM 		return (EEXIST);
8600Sstevel@tonic-gate 	}
86111042SErik.Nordmark@Sun.COM 	ire = nire;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if (flags & RTF_MULTIRT) {
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
8660Sstevel@tonic-gate 		 * to add the dst address in the filtering database.
8670Sstevel@tonic-gate 		 * Replicated inbound packets coming from that address
8680Sstevel@tonic-gate 		 * will be filtered to discard the duplicates.
8690Sstevel@tonic-gate 		 * It is not necessary to call the CGTP filter hook
8700Sstevel@tonic-gate 		 * when the dst address is a multicast, because an
8710Sstevel@tonic-gate 		 * IP source address cannot be a multicast.
8720Sstevel@tonic-gate 		 */
8734961Snordmark 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
8740Sstevel@tonic-gate 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
8754961Snordmark 			int res;
87611042SErik.Nordmark@Sun.COM 			ipif_t *src_ipif;
8774961Snordmark 
87811042SErik.Nordmark@Sun.COM 			/* Find the source address corresponding to gw_ire */
87911042SErik.Nordmark@Sun.COM 			src_ipif = ipif_lookup_addr_v6(
88011042SErik.Nordmark@Sun.COM 			    &gw_ire->ire_gateway_addr_v6, NULL, zoneid, ipst);
88111042SErik.Nordmark@Sun.COM 			if (src_ipif != NULL) {
88211042SErik.Nordmark@Sun.COM 				res = ipst->ips_ip_cgtp_filter_ops->
88311042SErik.Nordmark@Sun.COM 				    cfo_add_dest_v6(
88411042SErik.Nordmark@Sun.COM 				    ipst->ips_netstack->netstack_stackid,
88511042SErik.Nordmark@Sun.COM 				    &ire->ire_addr_v6,
88611042SErik.Nordmark@Sun.COM 				    &ire->ire_gateway_addr_v6,
88711042SErik.Nordmark@Sun.COM 				    &ire->ire_setsrc_addr_v6,
88811042SErik.Nordmark@Sun.COM 				    &src_ipif->ipif_v6lcl_addr);
88911042SErik.Nordmark@Sun.COM 				ipif_refrele(src_ipif);
89011042SErik.Nordmark@Sun.COM 			} else {
89111042SErik.Nordmark@Sun.COM 				res = EADDRNOTAVAIL;
89211042SErik.Nordmark@Sun.COM 			}
8930Sstevel@tonic-gate 			if (res != 0) {
89411042SErik.Nordmark@Sun.COM 				if (ipif != NULL)
89511042SErik.Nordmark@Sun.COM 					ipif_refrele(ipif);
8960Sstevel@tonic-gate 				ire_refrele(gw_ire);
8970Sstevel@tonic-gate 				ire_delete(ire);
89811042SErik.Nordmark@Sun.COM 				ire_refrele(ire);	/* Held in ire_add */
8990Sstevel@tonic-gate 				return (res);
9000Sstevel@tonic-gate 			}
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate save_ire:
9050Sstevel@tonic-gate 	if (gw_ire != NULL) {
9060Sstevel@tonic-gate 		ire_refrele(gw_ire);
90711042SErik.Nordmark@Sun.COM 		gw_ire = NULL;
9080Sstevel@tonic-gate 	}
90911042SErik.Nordmark@Sun.COM 	if (ire->ire_ill != NULL) {
9100Sstevel@tonic-gate 		/*
9110Sstevel@tonic-gate 		 * Save enough information so that we can recreate the IRE if
91211042SErik.Nordmark@Sun.COM 		 * the ILL goes down and then up.  The metrics associated
9130Sstevel@tonic-gate 		 * with the route will be saved as well when rts_setmetrics() is
9140Sstevel@tonic-gate 		 * called after the IRE has been created.  In the case where
9150Sstevel@tonic-gate 		 * memory cannot be allocated, none of this information will be
9160Sstevel@tonic-gate 		 * saved.
9170Sstevel@tonic-gate 		 */
91811042SErik.Nordmark@Sun.COM 		ill_save_ire(ire->ire_ill, ire);
91911042SErik.Nordmark@Sun.COM 	}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	if (ire_arg != NULL) {
9220Sstevel@tonic-gate 		/*
9230Sstevel@tonic-gate 		 * Store the ire that was successfully added into where ire_arg
9240Sstevel@tonic-gate 		 * points to so that callers don't have to look it up
9250Sstevel@tonic-gate 		 * themselves (but they are responsible for ire_refrele()ing
9260Sstevel@tonic-gate 		 * the ire when they are finished with it).
9270Sstevel@tonic-gate 		 */
9280Sstevel@tonic-gate 		*ire_arg = ire;
9290Sstevel@tonic-gate 	} else {
9300Sstevel@tonic-gate 		ire_refrele(ire);		/* Held in ire_add */
9310Sstevel@tonic-gate 	}
93211042SErik.Nordmark@Sun.COM 	if (ipif != NULL)
9330Sstevel@tonic-gate 		ipif_refrele(ipif);
9340Sstevel@tonic-gate 	return (0);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate  * ip_rt_delete_v6 is called to delete an IPv6 route.
93911042SErik.Nordmark@Sun.COM  * ill is passed in to associate it with the correct interface.
9400Sstevel@tonic-gate  * (for link-local destinations and gateways).
9410Sstevel@tonic-gate  */
9420Sstevel@tonic-gate /* ARGSUSED4 */
9430Sstevel@tonic-gate int
ip_rt_delete_v6(const in6_addr_t * dst_addr,const in6_addr_t * mask,const in6_addr_t * gw_addr,uint_t rtm_addrs,int flags,ill_t * ill,ip_stack_t * ipst,zoneid_t zoneid)9440Sstevel@tonic-gate ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
94511042SErik.Nordmark@Sun.COM     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ill_t *ill,
94611042SErik.Nordmark@Sun.COM     ip_stack_t *ipst, zoneid_t zoneid)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	ire_t	*ire = NULL;
9490Sstevel@tonic-gate 	ipif_t	*ipif;
9500Sstevel@tonic-gate 	uint_t	type;
9510Sstevel@tonic-gate 	uint_t	match_flags = MATCH_IRE_TYPE;
9520Sstevel@tonic-gate 	int	err = 0;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
9560Sstevel@tonic-gate 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 	if (flags & RTF_HOST) {
9590Sstevel@tonic-gate 		mask = &ipv6_all_ones;
9600Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
9610Sstevel@tonic-gate 	} else if (rtm_addrs & RTA_NETMASK) {
9620Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * Note that RTF_GATEWAY is never set on a delete, therefore
9670Sstevel@tonic-gate 	 * we check if the gateway address is one of our interfaces first,
9680Sstevel@tonic-gate 	 * and fall back on RTF_GATEWAY routes.
9690Sstevel@tonic-gate 	 *
9700Sstevel@tonic-gate 	 * This makes it possible to delete an original
9710Sstevel@tonic-gate 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
97211042SErik.Nordmark@Sun.COM 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
97311042SErik.Nordmark@Sun.COM 	 * and those can not be deleted here.
9740Sstevel@tonic-gate 	 *
97511042SErik.Nordmark@Sun.COM 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
97611042SErik.Nordmark@Sun.COM 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
97711042SErik.Nordmark@Sun.COM 	 * otherwise we use the ill derived from the gateway address.
97811042SErik.Nordmark@Sun.COM 	 * We can always match the gateway address since we record it
97911042SErik.Nordmark@Sun.COM 	 * in ire_gateway_addr.
9800Sstevel@tonic-gate 	 *
9810Sstevel@tonic-gate 	 * For more detail on specifying routes by gateway address and by
9820Sstevel@tonic-gate 	 * interface index, see the comments in ip_rt_add_v6().
9830Sstevel@tonic-gate 	 */
98411042SErik.Nordmark@Sun.COM 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, ipst);
9850Sstevel@tonic-gate 	if (ipif != NULL) {
98611042SErik.Nordmark@Sun.COM 		ill_t	*ill_match;
98711042SErik.Nordmark@Sun.COM 
98811042SErik.Nordmark@Sun.COM 		if (ill != NULL)
98911042SErik.Nordmark@Sun.COM 			ill_match = ill;
99011042SErik.Nordmark@Sun.COM 		else
99111042SErik.Nordmark@Sun.COM 			ill_match = ipif->ipif_ill;
99211042SErik.Nordmark@Sun.COM 
99311042SErik.Nordmark@Sun.COM 		match_flags |= MATCH_IRE_ILL;
99411042SErik.Nordmark@Sun.COM 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
99511916SErik.Nordmark@Sun.COM 			ire = ire_ftable_lookup_v6(dst_addr, mask, 0,
99611916SErik.Nordmark@Sun.COM 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
99711916SErik.Nordmark@Sun.COM 			    match_flags, 0, ipst, NULL);
99811042SErik.Nordmark@Sun.COM 		}
99911042SErik.Nordmark@Sun.COM 		if (ire == NULL) {
100011042SErik.Nordmark@Sun.COM 			match_flags |= MATCH_IRE_GW;
100111042SErik.Nordmark@Sun.COM 			ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr,
100211042SErik.Nordmark@Sun.COM 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
100311042SErik.Nordmark@Sun.COM 			    match_flags, 0, ipst, NULL);
100411042SErik.Nordmark@Sun.COM 		}
100511042SErik.Nordmark@Sun.COM 		/* Avoid deleting routes created by kernel from an ipif */
100611042SErik.Nordmark@Sun.COM 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
100711042SErik.Nordmark@Sun.COM 			ire_refrele(ire);
100811042SErik.Nordmark@Sun.COM 			ire = NULL;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 
101111042SErik.Nordmark@Sun.COM 		/* Restore in case we didn't find a match */
101211042SErik.Nordmark@Sun.COM 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
10130Sstevel@tonic-gate 	}
101411042SErik.Nordmark@Sun.COM 
10150Sstevel@tonic-gate 	if (ire == NULL) {
10160Sstevel@tonic-gate 		/*
10170Sstevel@tonic-gate 		 * At this point, the gateway address is not one of our own
10180Sstevel@tonic-gate 		 * addresses or a matching interface route was not found.  We
10190Sstevel@tonic-gate 		 * set the IRE type to lookup based on whether
10200Sstevel@tonic-gate 		 * this is a host route, a default route or just a prefix.
10210Sstevel@tonic-gate 		 *
102211042SErik.Nordmark@Sun.COM 		 * If an ill was passed in, then the lookup is based on an
10230Sstevel@tonic-gate 		 * interface index so MATCH_IRE_ILL is added to match_flags.
10240Sstevel@tonic-gate 		 */
10250Sstevel@tonic-gate 		match_flags |= MATCH_IRE_GW;
102611042SErik.Nordmark@Sun.COM 		if (ill != NULL)
10270Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
10280Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
10290Sstevel@tonic-gate 			type = IRE_HOST;
10300Sstevel@tonic-gate 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
10310Sstevel@tonic-gate 			type = IRE_DEFAULT;
10320Sstevel@tonic-gate 		else
10330Sstevel@tonic-gate 			type = IRE_PREFIX;
10340Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
103511042SErik.Nordmark@Sun.COM 		    ill, ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 
103811042SErik.Nordmark@Sun.COM 	if (ipif != NULL) {
10390Sstevel@tonic-gate 		ipif_refrele(ipif);
104011042SErik.Nordmark@Sun.COM 		ipif = NULL;
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 	if (ire == NULL)
10430Sstevel@tonic-gate 		return (ESRCH);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	if (ire->ire_flags & RTF_MULTIRT) {
10460Sstevel@tonic-gate 		/*
10470Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
10480Sstevel@tonic-gate 		 * to remove the dst address from the filtering database.
10490Sstevel@tonic-gate 		 * Packets coming from that address will no longer be
10500Sstevel@tonic-gate 		 * filtered to remove duplicates.
10510Sstevel@tonic-gate 		 */
10524961Snordmark 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
10534961Snordmark 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v6(
10544961Snordmark 			    ipst->ips_netstack->netstack_stackid,
10550Sstevel@tonic-gate 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
105911042SErik.Nordmark@Sun.COM 	ill = ire->ire_ill;
106011042SErik.Nordmark@Sun.COM 	if (ill != NULL)
106111042SErik.Nordmark@Sun.COM 		ill_remove_saved_ire(ill, ire);
10620Sstevel@tonic-gate 	ire_delete(ire);
10630Sstevel@tonic-gate 	ire_refrele(ire);
10640Sstevel@tonic-gate 	return (err);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate /*
106810616SSebastien.Roy@Sun.COM  * Derive an interface id from the link layer address.
10690Sstevel@tonic-gate  */
107010616SSebastien.Roy@Sun.COM void
ill_setdefaulttoken(ill_t * ill)10710Sstevel@tonic-gate ill_setdefaulttoken(ill_t *ill)
10720Sstevel@tonic-gate {
107310773SSebastien.Roy@Sun.COM 	if (!ill->ill_manual_token) {
107410616SSebastien.Roy@Sun.COM 		bzero(&ill->ill_token, sizeof (ill->ill_token));
107510616SSebastien.Roy@Sun.COM 		MEDIA_V6INTFID(ill->ill_media, ill, &ill->ill_token);
107610616SSebastien.Roy@Sun.COM 		ill->ill_token_length = IPV6_TOKEN_LEN;
107710616SSebastien.Roy@Sun.COM 	}
107810616SSebastien.Roy@Sun.COM }
107910616SSebastien.Roy@Sun.COM 
108010616SSebastien.Roy@Sun.COM void
ill_setdesttoken(ill_t * ill)108110616SSebastien.Roy@Sun.COM ill_setdesttoken(ill_t *ill)
108210616SSebastien.Roy@Sun.COM {
108310616SSebastien.Roy@Sun.COM 	bzero(&ill->ill_dest_token, sizeof (ill->ill_dest_token));
108410616SSebastien.Roy@Sun.COM 	MEDIA_V6DESTINTFID(ill->ill_media, ill, &ill->ill_dest_token);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate /*
10880Sstevel@tonic-gate  * Create a link-local address from a token.
10890Sstevel@tonic-gate  */
10900Sstevel@tonic-gate static void
ipif_get_linklocal(in6_addr_t * dest,const in6_addr_t * token)10910Sstevel@tonic-gate ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	int i;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
10960Sstevel@tonic-gate 		dest->s6_addr32[i] =
10970Sstevel@tonic-gate 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate /*
110210616SSebastien.Roy@Sun.COM  * Set a default IPv6 address for a 6to4 tunnel interface 2002:<tsrc>::1/16
11030Sstevel@tonic-gate  */
11040Sstevel@tonic-gate static void
ipif_set6to4addr(ipif_t * ipif)110510616SSebastien.Roy@Sun.COM ipif_set6to4addr(ipif_t *ipif)
11060Sstevel@tonic-gate {
110710616SSebastien.Roy@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
110810616SSebastien.Roy@Sun.COM 	struct in_addr	v4phys;
110910616SSebastien.Roy@Sun.COM 
111010616SSebastien.Roy@Sun.COM 	ASSERT(ill->ill_mactype == DL_6TO4);
111110616SSebastien.Roy@Sun.COM 	ASSERT(ill->ill_phys_addr_length == sizeof (struct in_addr));
111210616SSebastien.Roy@Sun.COM 	ASSERT(ipif->ipif_isv6);
111310616SSebastien.Roy@Sun.COM 
111410616SSebastien.Roy@Sun.COM 	if (ipif->ipif_flags & IPIF_UP)
11150Sstevel@tonic-gate 		return;
111610616SSebastien.Roy@Sun.COM 
111710616SSebastien.Roy@Sun.COM 	(void) ip_plen_to_mask_v6(16, &ipif->ipif_v6net_mask);
111810616SSebastien.Roy@Sun.COM 	bcopy(ill->ill_phys_addr, &v4phys, sizeof (struct in_addr));
111910616SSebastien.Roy@Sun.COM 	IN6_V4ADDR_TO_6TO4(&v4phys, &ipif->ipif_v6lcl_addr);
112010616SSebastien.Roy@Sun.COM 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
112110616SSebastien.Roy@Sun.COM 	    ipif->ipif_v6subnet);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate /*
11250Sstevel@tonic-gate  * Is it not possible to set the link local address?
11260Sstevel@tonic-gate  * The address can be set if the token is set, and the token
11270Sstevel@tonic-gate  * isn't too long.
11280Sstevel@tonic-gate  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
11290Sstevel@tonic-gate  */
11300Sstevel@tonic-gate boolean_t
ipif_cant_setlinklocal(ipif_t * ipif)11310Sstevel@tonic-gate ipif_cant_setlinklocal(ipif_t *ipif)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
11360Sstevel@tonic-gate 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
11370Sstevel@tonic-gate 		return (B_TRUE);
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	return (B_FALSE);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate /*
11430Sstevel@tonic-gate  * Generate a link-local address from the token.
11440Sstevel@tonic-gate  */
114510616SSebastien.Roy@Sun.COM void
ipif_setlinklocal(ipif_t * ipif)11460Sstevel@tonic-gate ipif_setlinklocal(ipif_t *ipif)
11470Sstevel@tonic-gate {
11483706Svi117747 	ill_t		*ill = ipif->ipif_ill;
11493706Svi117747 	in6_addr_t	ov6addr;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
11520Sstevel@tonic-gate 
115310616SSebastien.Roy@Sun.COM 	/*
115412016SGirish.Moodalbail@Sun.COM 	 * If the interface was created with no link-local address
115512016SGirish.Moodalbail@Sun.COM 	 * on it and the flag ILLF_NOLINKLOCAL was set, then we
115612016SGirish.Moodalbail@Sun.COM 	 * dont want to update the link-local.
115712016SGirish.Moodalbail@Sun.COM 	 */
115812016SGirish.Moodalbail@Sun.COM 	if ((ill->ill_flags & ILLF_NOLINKLOCAL) &&
115912016SGirish.Moodalbail@Sun.COM 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
116012016SGirish.Moodalbail@Sun.COM 		return;
116112016SGirish.Moodalbail@Sun.COM 	/*
116210616SSebastien.Roy@Sun.COM 	 * ill_manual_linklocal is set when the link-local address was
116310616SSebastien.Roy@Sun.COM 	 * manually configured.
116410616SSebastien.Roy@Sun.COM 	 */
116510616SSebastien.Roy@Sun.COM 	if (ill->ill_manual_linklocal)
116610616SSebastien.Roy@Sun.COM 		return;
116710616SSebastien.Roy@Sun.COM 
116810616SSebastien.Roy@Sun.COM 	/*
116910616SSebastien.Roy@Sun.COM 	 * IPv6 interfaces over 6to4 tunnels are special.  They do not have
117010616SSebastien.Roy@Sun.COM 	 * link-local addresses, but instead have a single automatically
117110616SSebastien.Roy@Sun.COM 	 * generated global address.
117210616SSebastien.Roy@Sun.COM 	 */
117310616SSebastien.Roy@Sun.COM 	if (ill->ill_mactype == DL_6TO4) {
117410616SSebastien.Roy@Sun.COM 		ipif_set6to4addr(ipif);
117510616SSebastien.Roy@Sun.COM 		return;
117610616SSebastien.Roy@Sun.COM 	}
117710616SSebastien.Roy@Sun.COM 
11780Sstevel@tonic-gate 	if (ipif_cant_setlinklocal(ipif))
117910616SSebastien.Roy@Sun.COM 		return;
11800Sstevel@tonic-gate 
11813706Svi117747 	ov6addr = ipif->ipif_v6lcl_addr;
11820Sstevel@tonic-gate 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
11833706Svi117747 	sctp_update_ipif_addr(ipif, ov6addr);
11840Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
118510616SSebastien.Roy@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
118610616SSebastien.Roy@Sun.COM 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
118710616SSebastien.Roy@Sun.COM 		    ipif->ipif_v6subnet);
118810616SSebastien.Roy@Sun.COM 	}
11890Sstevel@tonic-gate 
119011076SCathy.Zhou@Sun.COM 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
119110616SSebastien.Roy@Sun.COM }
119210616SSebastien.Roy@Sun.COM 
119310616SSebastien.Roy@Sun.COM /*
119411371SSebastien.Roy@Sun.COM  * Generate a destination link-local address for a point-to-point IPv6
119511371SSebastien.Roy@Sun.COM  * interface with a destination interface id (IP tunnels are such interfaces)
119611371SSebastien.Roy@Sun.COM  * based on the destination token.
119710616SSebastien.Roy@Sun.COM  */
119810616SSebastien.Roy@Sun.COM void
ipif_setdestlinklocal(ipif_t * ipif)119910616SSebastien.Roy@Sun.COM ipif_setdestlinklocal(ipif_t *ipif)
120010616SSebastien.Roy@Sun.COM {
120110616SSebastien.Roy@Sun.COM 	ill_t	*ill = ipif->ipif_ill;
120210616SSebastien.Roy@Sun.COM 
120310616SSebastien.Roy@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
120411371SSebastien.Roy@Sun.COM 
120511371SSebastien.Roy@Sun.COM 	if (ill->ill_manual_dst_linklocal)
120610616SSebastien.Roy@Sun.COM 		return;
120711371SSebastien.Roy@Sun.COM 
120811371SSebastien.Roy@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_dest_token))
120911042SErik.Nordmark@Sun.COM 		return;
121011042SErik.Nordmark@Sun.COM 
121110616SSebastien.Roy@Sun.COM 	ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr, &ill->ill_dest_token);
121210616SSebastien.Roy@Sun.COM 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate /*
12164972Smeem  * Get the resolver set up for a new ipif.  (Always called as writer.)
12170Sstevel@tonic-gate  */
12180Sstevel@tonic-gate int
ipif_ndp_up(ipif_t * ipif,boolean_t initial)12198485SPeter.Memishian@Sun.COM ipif_ndp_up(ipif_t *ipif, boolean_t initial)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
12220Sstevel@tonic-gate 	int		err = 0;
12230Sstevel@tonic-gate 	nce_t		*nce = NULL;
12248485SPeter.Memishian@Sun.COM 	boolean_t	added_ipif = B_FALSE;
12258485SPeter.Memishian@Sun.COM 
122611042SErik.Nordmark@Sun.COM 	DTRACE_PROBE3(ipif__downup, char *, "ipif_ndp_up",
122711042SErik.Nordmark@Sun.COM 	    ill_t *, ill, ipif_t *, ipif);
12284972Smeem 	ip1dbg(("ipif_ndp_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12290Sstevel@tonic-gate 
123011042SErik.Nordmark@Sun.COM 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) ||
12312546Scarlsonj 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
12322546Scarlsonj 		ipif->ipif_addr_ready = 1;
12330Sstevel@tonic-gate 		return (0);
12342546Scarlsonj 	}
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
12370Sstevel@tonic-gate 		uint16_t	flags;
12388485SPeter.Memishian@Sun.COM 		uint16_t	state;
123911042SErik.Nordmark@Sun.COM 		uchar_t		*hw_addr;
12408485SPeter.Memishian@Sun.COM 		ill_t		*bound_ill;
12418485SPeter.Memishian@Sun.COM 		ipmp_illgrp_t	*illg = ill->ill_grp;
124211042SErik.Nordmark@Sun.COM 		uint_t		hw_addr_len;
12430Sstevel@tonic-gate 
124411042SErik.Nordmark@Sun.COM 		flags = NCE_F_MYADDR | NCE_F_NONUD | NCE_F_PUBLISH |
124511042SErik.Nordmark@Sun.COM 		    NCE_F_AUTHORITY;
12460Sstevel@tonic-gate 		if (ill->ill_flags & ILLF_ROUTER)
12470Sstevel@tonic-gate 			flags |= NCE_F_ISROUTER;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_ANYCAST)
12500Sstevel@tonic-gate 			flags |= NCE_F_ANYCAST;
12510Sstevel@tonic-gate 
12528485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill)) {
12538485SPeter.Memishian@Sun.COM 			ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
12548485SPeter.Memishian@Sun.COM 			/*
12558485SPeter.Memishian@Sun.COM 			 * If we're here via ipif_up(), then the ipif won't be
12568485SPeter.Memishian@Sun.COM 			 * bound yet -- add it to the group, which will bind
12578485SPeter.Memishian@Sun.COM 			 * it if possible.  (We would add it in ipif_up(), but
12588485SPeter.Memishian@Sun.COM 			 * deleting on failure there is gruesome.)  If we're
12598485SPeter.Memishian@Sun.COM 			 * here via ipmp_ill_bind_ipif(), then the ipif has
12608485SPeter.Memishian@Sun.COM 			 * already been added to the group and we just need to
12618485SPeter.Memishian@Sun.COM 			 * use the binding.
12628485SPeter.Memishian@Sun.COM 			 */
12638485SPeter.Memishian@Sun.COM 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
12648485SPeter.Memishian@Sun.COM 				bound_ill = ipmp_illgrp_add_ipif(illg, ipif);
12658485SPeter.Memishian@Sun.COM 				if (bound_ill == NULL) {
12668485SPeter.Memishian@Sun.COM 					/*
12678485SPeter.Memishian@Sun.COM 					 * We couldn't bind the ipif to an ill
12688485SPeter.Memishian@Sun.COM 					 * yet, so we have nothing to publish.
12698485SPeter.Memishian@Sun.COM 					 * Set ipif_addr_ready so that this
12708485SPeter.Memishian@Sun.COM 					 * address can be used locally for now.
12718485SPeter.Memishian@Sun.COM 					 * The routing socket message will be
12728485SPeter.Memishian@Sun.COM 					 * sent from ipif_up_done_v6().
12738485SPeter.Memishian@Sun.COM 					 */
12748485SPeter.Memishian@Sun.COM 					ipif->ipif_addr_ready = 1;
12758485SPeter.Memishian@Sun.COM 					return (0);
12768485SPeter.Memishian@Sun.COM 				}
12778485SPeter.Memishian@Sun.COM 				added_ipif = B_TRUE;
12780Sstevel@tonic-gate 			}
12798485SPeter.Memishian@Sun.COM 			hw_addr = bound_ill->ill_nd_lla;
128011042SErik.Nordmark@Sun.COM 			hw_addr_len = bound_ill->ill_phys_addr_length;
12818485SPeter.Memishian@Sun.COM 		} else {
12828485SPeter.Memishian@Sun.COM 			bound_ill = ill;
128311111SSowmini.Varadhan@Sun.COM 			hw_addr = ill->ill_nd_lla;
128411111SSowmini.Varadhan@Sun.COM 			hw_addr_len = ill->ill_phys_addr_length;
12850Sstevel@tonic-gate 		}
12868485SPeter.Memishian@Sun.COM 
12878485SPeter.Memishian@Sun.COM 		/*
12888485SPeter.Memishian@Sun.COM 		 * If this is an initial bring-up (or the ipif was never
12898485SPeter.Memishian@Sun.COM 		 * completely brought up), do DAD.  Otherwise, we're here
12908485SPeter.Memishian@Sun.COM 		 * because IPMP has rebound an address to this ill: send
12918485SPeter.Memishian@Sun.COM 		 * unsolicited advertisements to inform others.
12928485SPeter.Memishian@Sun.COM 		 */
12938485SPeter.Memishian@Sun.COM 		if (initial || !ipif->ipif_addr_ready) {
129411042SErik.Nordmark@Sun.COM 			/* Causes Duplicate Address Detection to run */
12958485SPeter.Memishian@Sun.COM 			state = ND_PROBE;
12968485SPeter.Memishian@Sun.COM 		} else {
12978485SPeter.Memishian@Sun.COM 			state = ND_REACHABLE;
12988485SPeter.Memishian@Sun.COM 			flags |= NCE_F_UNSOL_ADV;
12998485SPeter.Memishian@Sun.COM 		}
130011042SErik.Nordmark@Sun.COM 
13019814SSowmini.Varadhan@Sun.COM retry:
130211042SErik.Nordmark@Sun.COM 		err = nce_lookup_then_add_v6(ill, hw_addr, hw_addr_len,
130311042SErik.Nordmark@Sun.COM 		    &ipif->ipif_v6lcl_addr, flags, state, &nce);
13040Sstevel@tonic-gate 		switch (err) {
13050Sstevel@tonic-gate 		case 0:
13060Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
13070Sstevel@tonic-gate 			    ill->ill_name));
13082546Scarlsonj 			ipif->ipif_addr_ready = 1;
13099287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
13109571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
13112546Scarlsonj 			break;
13122546Scarlsonj 		case EINPROGRESS:
13132546Scarlsonj 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
13142546Scarlsonj 			    ill->ill_name));
13159287SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
13169571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
13170Sstevel@tonic-gate 			break;
13180Sstevel@tonic-gate 		case EEXIST:
13190Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
13200Sstevel@tonic-gate 			    ill->ill_name));
132111042SErik.Nordmark@Sun.COM 			if (!NCE_MYADDR(nce->nce_common)) {
132211042SErik.Nordmark@Sun.COM 				/*
132311042SErik.Nordmark@Sun.COM 				 * A leftover nce from before this address
132411042SErik.Nordmark@Sun.COM 				 * existed
132511042SErik.Nordmark@Sun.COM 				 */
132611042SErik.Nordmark@Sun.COM 				ncec_delete(nce->nce_common);
132711042SErik.Nordmark@Sun.COM 				nce_refrele(nce);
13289814SSowmini.Varadhan@Sun.COM 				nce = NULL;
13299814SSowmini.Varadhan@Sun.COM 				goto retry;
13309814SSowmini.Varadhan@Sun.COM 			}
13319571SSowmini.Varadhan@Sun.COM 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
133211042SErik.Nordmark@Sun.COM 				nce_refrele(nce);
133311042SErik.Nordmark@Sun.COM 				nce = NULL;
133411042SErik.Nordmark@Sun.COM 				ip1dbg(("ipif_ndp_up: NCE already exists "
133511042SErik.Nordmark@Sun.COM 				    "for %s\n", ill->ill_name));
13369571SSowmini.Varadhan@Sun.COM 				goto fail;
13379571SSowmini.Varadhan@Sun.COM 			}
13389571SSowmini.Varadhan@Sun.COM 			/*
13399571SSowmini.Varadhan@Sun.COM 			 * Duplicate local addresses are permissible for
13409571SSowmini.Varadhan@Sun.COM 			 * IPIF_POINTOPOINT interfaces which will get marked
13419571SSowmini.Varadhan@Sun.COM 			 * IPIF_UNNUMBERED later in
13429571SSowmini.Varadhan@Sun.COM 			 * ip_addr_availability_check().
13439571SSowmini.Varadhan@Sun.COM 			 *
13449571SSowmini.Varadhan@Sun.COM 			 * The nce_ipif_cnt field tracks the number of
13459571SSowmini.Varadhan@Sun.COM 			 * ipifs that have nce_addr as their local address.
13469571SSowmini.Varadhan@Sun.COM 			 */
13479571SSowmini.Varadhan@Sun.COM 			ipif->ipif_addr_ready = 1;
13489571SSowmini.Varadhan@Sun.COM 			ipif->ipif_added_nce = 1;
13499571SSowmini.Varadhan@Sun.COM 			nce->nce_ipif_cnt++;
135011042SErik.Nordmark@Sun.COM 			err = 0;
13519571SSowmini.Varadhan@Sun.COM 			break;
13520Sstevel@tonic-gate 		default:
13538485SPeter.Memishian@Sun.COM 			ip1dbg(("ipif_ndp_up: NCE creation failed for %s\n",
13540Sstevel@tonic-gate 			    ill->ill_name));
13558485SPeter.Memishian@Sun.COM 			goto fail;
13560Sstevel@tonic-gate 		}
13572546Scarlsonj 	} else {
13582546Scarlsonj 		/* No local NCE for this entry */
13592546Scarlsonj 		ipif->ipif_addr_ready = 1;
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 	if (nce != NULL)
136211042SErik.Nordmark@Sun.COM 		nce_refrele(nce);
13630Sstevel@tonic-gate 	return (0);
13648485SPeter.Memishian@Sun.COM fail:
13658485SPeter.Memishian@Sun.COM 	if (added_ipif)
13668485SPeter.Memishian@Sun.COM 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
13678485SPeter.Memishian@Sun.COM 
13688485SPeter.Memishian@Sun.COM 	return (err);
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate /* Remove all cache entries for this logical interface */
13720Sstevel@tonic-gate void
ipif_ndp_down(ipif_t * ipif)13730Sstevel@tonic-gate ipif_ndp_down(ipif_t *ipif)
13740Sstevel@tonic-gate {
137511042SErik.Nordmark@Sun.COM 	ipif_nce_down(ipif);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * Return the scope of the given IPv6 address.  If the address is an
13800Sstevel@tonic-gate  * IPv4 mapped IPv6 address, return the scope of the corresponding
13810Sstevel@tonic-gate  * IPv4 address.
13820Sstevel@tonic-gate  */
13830Sstevel@tonic-gate in6addr_scope_t
ip_addr_scope_v6(const in6_addr_t * addr)13840Sstevel@tonic-gate ip_addr_scope_v6(const in6_addr_t *addr)
13850Sstevel@tonic-gate {
13860Sstevel@tonic-gate 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
13890Sstevel@tonic-gate 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
13900Sstevel@tonic-gate 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
13910Sstevel@tonic-gate 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
13920Sstevel@tonic-gate 			return (IP6_SCOPE_LINKLOCAL);
13930Sstevel@tonic-gate 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
13940Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
13950Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
13960Sstevel@tonic-gate 			return (IP6_SCOPE_SITELOCAL);
13970Sstevel@tonic-gate 		return (IP6_SCOPE_GLOBAL);
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	if (IN6_IS_ADDR_MULTICAST(addr))
14010Sstevel@tonic-gate 		return (IN6_ADDR_MC_SCOPE(addr));
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	/* link-local and loopback addresses are of link-local scope */
14040Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
14050Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
14060Sstevel@tonic-gate 		return (IP6_SCOPE_LINKLOCAL);
14070Sstevel@tonic-gate 	if (IN6_IS_ADDR_SITELOCAL(addr))
14080Sstevel@tonic-gate 		return (IP6_SCOPE_SITELOCAL);
14090Sstevel@tonic-gate 	return (IP6_SCOPE_GLOBAL);
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate /*
14143431Scarlsonj  * Returns the length of the common prefix of a1 and a2, as per
14153431Scarlsonj  * CommonPrefixLen() defined in RFC 3484.
14160Sstevel@tonic-gate  */
14173431Scarlsonj static int
ip_common_prefix_v6(const in6_addr_t * a1,const in6_addr_t * a2)14183431Scarlsonj ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2)
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	int i;
14213431Scarlsonj 	uint32_t a1val, a2val, mask;
14220Sstevel@tonic-gate 
14233431Scarlsonj 	for (i = 0; i < 4; i++) {
14243431Scarlsonj 		if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) {
14253431Scarlsonj 			a1val ^= a2val;
14263431Scarlsonj 			i *= 32;
14273431Scarlsonj 			mask = 0x80000000u;
14283431Scarlsonj 			while (!(a1val & mask)) {
14293431Scarlsonj 				mask >>= 1;
14303431Scarlsonj 				i++;
14313431Scarlsonj 			}
14323431Scarlsonj 			return (i);
14333431Scarlsonj 		}
14343431Scarlsonj 	}
14353431Scarlsonj 	return (IPV6_ABITS);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate #define	IPIF_VALID_IPV6_SOURCE(ipif) \
14390Sstevel@tonic-gate 	(((ipif)->ipif_flags & IPIF_UP) && \
144011076SCathy.Zhou@Sun.COM 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
144111076SCathy.Zhou@Sun.COM 	!((ipif)->ipif_ill->ill_flags & ILLF_NOACCEPT))
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate /* source address candidate */
14440Sstevel@tonic-gate typedef struct candidate {
14450Sstevel@tonic-gate 	ipif_t		*cand_ipif;
14460Sstevel@tonic-gate 	/* The properties of this candidate */
14470Sstevel@tonic-gate 	boolean_t	cand_isdst;
14480Sstevel@tonic-gate 	boolean_t	cand_isdst_set;
14490Sstevel@tonic-gate 	in6addr_scope_t	cand_scope;
14500Sstevel@tonic-gate 	boolean_t	cand_scope_set;
14510Sstevel@tonic-gate 	boolean_t	cand_isdeprecated;
14520Sstevel@tonic-gate 	boolean_t	cand_isdeprecated_set;
14530Sstevel@tonic-gate 	boolean_t	cand_ispreferred;
14540Sstevel@tonic-gate 	boolean_t	cand_ispreferred_set;
14550Sstevel@tonic-gate 	boolean_t	cand_matchedinterface;
14560Sstevel@tonic-gate 	boolean_t	cand_matchedinterface_set;
14570Sstevel@tonic-gate 	boolean_t	cand_matchedlabel;
14580Sstevel@tonic-gate 	boolean_t	cand_matchedlabel_set;
14590Sstevel@tonic-gate 	boolean_t	cand_istmp;
14600Sstevel@tonic-gate 	boolean_t	cand_istmp_set;
14613431Scarlsonj 	int		cand_common_pref;
14623431Scarlsonj 	boolean_t	cand_common_pref_set;
14633431Scarlsonj 	boolean_t	cand_pref_eq;
14643431Scarlsonj 	boolean_t	cand_pref_eq_set;
14653431Scarlsonj 	int		cand_pref_len;
14663431Scarlsonj 	boolean_t	cand_pref_len_set;
14670Sstevel@tonic-gate } cand_t;
14680Sstevel@tonic-gate #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
14693431Scarlsonj #define	cand_mask	cand_ipif->ipif_v6net_mask
14700Sstevel@tonic-gate #define	cand_flags	cand_ipif->ipif_flags
14710Sstevel@tonic-gate #define	cand_ill	cand_ipif->ipif_ill
14721676Sjpk #define	cand_zoneid	cand_ipif->ipif_zoneid
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate /* information about the destination for source address selection */
14750Sstevel@tonic-gate typedef struct dstinfo {
14760Sstevel@tonic-gate 	const in6_addr_t	*dst_addr;
14770Sstevel@tonic-gate 	ill_t			*dst_ill;
14782202Srk129064 	uint_t			dst_restrict_ill;
14790Sstevel@tonic-gate 	boolean_t		dst_prefer_src_tmp;
14800Sstevel@tonic-gate 	in6addr_scope_t		dst_scope;
14810Sstevel@tonic-gate 	char			*dst_label;
14820Sstevel@tonic-gate } dstinfo_t;
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate /*
14850Sstevel@tonic-gate  * The following functions are rules used to select a source address in
14860Sstevel@tonic-gate  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
14870Sstevel@tonic-gate  * against the best candidate (bc).  Each rule has three possible outcomes;
14880Sstevel@tonic-gate  * the candidate is preferred over the best candidate (CAND_PREFER), the
14890Sstevel@tonic-gate  * candidate is not preferred over the best candidate (CAND_AVOID), or the
14900Sstevel@tonic-gate  * candidate is of equal value as the best candidate (CAND_TIE).
14910Sstevel@tonic-gate  *
14920Sstevel@tonic-gate  * These rules are part of a greater "Default Address Selection for IPv6"
14930Sstevel@tonic-gate  * sheme, which is standards based work coming out of the IETF ipv6 working
14940Sstevel@tonic-gate  * group.  The IETF document defines both IPv6 source address selection and
14950Sstevel@tonic-gate  * destination address ordering.  The rules defined here implement the IPv6
14960Sstevel@tonic-gate  * source address selection.  Destination address ordering is done by
14970Sstevel@tonic-gate  * libnsl, and uses a similar set of rules to implement the sorting.
14983431Scarlsonj  *
14993431Scarlsonj  * Most of the rules are defined by the RFC and are not typically altered.  The
15003431Scarlsonj  * last rule, number 8, has language that allows for local preferences.  In the
15013431Scarlsonj  * scheme below, this means that new Solaris rules should normally go between
15023431Scarlsonj  * rule_ifprefix and rule_prefix.
15030Sstevel@tonic-gate  */
15040Sstevel@tonic-gate typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
15053448Sdh155122 typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *,
15063448Sdh155122     ip_stack_t *);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate /* Prefer an address if it is equal to the destination address. */
15093448Sdh155122 /* ARGSUSED3 */
15100Sstevel@tonic-gate static rule_res_t
rule_isdst(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)15113448Sdh155122 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate 	if (!bc->cand_isdst_set) {
15140Sstevel@tonic-gate 		bc->cand_isdst =
15150Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
15160Sstevel@tonic-gate 		bc->cand_isdst_set = B_TRUE;
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	cc->cand_isdst =
15200Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
15210Sstevel@tonic-gate 	cc->cand_isdst_set = B_TRUE;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	if (cc->cand_isdst == bc->cand_isdst)
15240Sstevel@tonic-gate 		return (CAND_TIE);
15250Sstevel@tonic-gate 	else if (cc->cand_isdst)
15260Sstevel@tonic-gate 		return (CAND_PREFER);
15270Sstevel@tonic-gate 	else
15280Sstevel@tonic-gate 		return (CAND_AVOID);
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate /*
15320Sstevel@tonic-gate  * Prefer addresses that are of closest scope to the destination.  Always
15330Sstevel@tonic-gate  * prefer addresses that are of greater scope than the destination over
15340Sstevel@tonic-gate  * those that are of lesser scope than the destination.
15350Sstevel@tonic-gate  */
15363448Sdh155122 /* ARGSUSED3 */
15370Sstevel@tonic-gate static rule_res_t
rule_scope(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)15383448Sdh155122 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
15390Sstevel@tonic-gate {
15400Sstevel@tonic-gate 	if (!bc->cand_scope_set) {
15410Sstevel@tonic-gate 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
15420Sstevel@tonic-gate 		bc->cand_scope_set = B_TRUE;
15430Sstevel@tonic-gate 	}
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
15460Sstevel@tonic-gate 	cc->cand_scope_set = B_TRUE;
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	if (cc->cand_scope < bc->cand_scope) {
15490Sstevel@tonic-gate 		if (cc->cand_scope < dstinfo->dst_scope)
15500Sstevel@tonic-gate 			return (CAND_AVOID);
15510Sstevel@tonic-gate 		else
15520Sstevel@tonic-gate 			return (CAND_PREFER);
15530Sstevel@tonic-gate 	} else if (bc->cand_scope < cc->cand_scope) {
15540Sstevel@tonic-gate 		if (bc->cand_scope < dstinfo->dst_scope)
15550Sstevel@tonic-gate 			return (CAND_PREFER);
15560Sstevel@tonic-gate 		else
15570Sstevel@tonic-gate 			return (CAND_AVOID);
15580Sstevel@tonic-gate 	} else {
15590Sstevel@tonic-gate 		return (CAND_TIE);
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate /*
15640Sstevel@tonic-gate  * Prefer non-deprecated source addresses.
15650Sstevel@tonic-gate  */
15660Sstevel@tonic-gate /* ARGSUSED2 */
15670Sstevel@tonic-gate static rule_res_t
rule_deprecated(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)15683448Sdh155122 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
15693448Sdh155122     ip_stack_t *ipst)
15700Sstevel@tonic-gate {
15710Sstevel@tonic-gate 	if (!bc->cand_isdeprecated_set) {
15720Sstevel@tonic-gate 		bc->cand_isdeprecated =
15730Sstevel@tonic-gate 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
15740Sstevel@tonic-gate 		bc->cand_isdeprecated_set = B_TRUE;
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
15780Sstevel@tonic-gate 	cc->cand_isdeprecated_set = B_TRUE;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
15810Sstevel@tonic-gate 		return (CAND_TIE);
15820Sstevel@tonic-gate 	else if (cc->cand_isdeprecated)
15830Sstevel@tonic-gate 		return (CAND_AVOID);
15840Sstevel@tonic-gate 	else
15850Sstevel@tonic-gate 		return (CAND_PREFER);
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate /*
15890Sstevel@tonic-gate  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
15900Sstevel@tonic-gate  * rule must be before rule_interface because the flag could be set on any
15910Sstevel@tonic-gate  * interface, not just the interface being used for outgoing packets (for
15920Sstevel@tonic-gate  * example, the IFF_PREFERRED could be set on an address assigned to the
15930Sstevel@tonic-gate  * loopback interface).
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate /* ARGSUSED2 */
15960Sstevel@tonic-gate static rule_res_t
rule_preferred(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)15973448Sdh155122 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
15983448Sdh155122     ip_stack_t *ipst)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	if (!bc->cand_ispreferred_set) {
16010Sstevel@tonic-gate 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
16020Sstevel@tonic-gate 		bc->cand_ispreferred_set = B_TRUE;
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
16060Sstevel@tonic-gate 	cc->cand_ispreferred_set = B_TRUE;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	if (bc->cand_ispreferred == cc->cand_ispreferred)
16090Sstevel@tonic-gate 		return (CAND_TIE);
16100Sstevel@tonic-gate 	else if (cc->cand_ispreferred)
16110Sstevel@tonic-gate 		return (CAND_PREFER);
16120Sstevel@tonic-gate 	else
16130Sstevel@tonic-gate 		return (CAND_AVOID);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate /*
16178485SPeter.Memishian@Sun.COM  * Prefer source addresses that are assigned to the outgoing interface.
16180Sstevel@tonic-gate  */
16193448Sdh155122 /* ARGSUSED3 */
16200Sstevel@tonic-gate static rule_res_t
rule_interface(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)16213448Sdh155122 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
16223448Sdh155122     ip_stack_t *ipst)
16230Sstevel@tonic-gate {
16240Sstevel@tonic-gate 	ill_t *dstill = dstinfo->dst_ill;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	/*
16270Sstevel@tonic-gate 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
16280Sstevel@tonic-gate 	 * since we know all candidates will be on the same link.
16290Sstevel@tonic-gate 	 */
16300Sstevel@tonic-gate 	if (dstinfo->dst_restrict_ill)
16310Sstevel@tonic-gate 		return (CAND_TIE);
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	if (!bc->cand_matchedinterface_set) {
16348485SPeter.Memishian@Sun.COM 		bc->cand_matchedinterface = bc->cand_ill == dstill;
16350Sstevel@tonic-gate 		bc->cand_matchedinterface_set = B_TRUE;
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 
16388485SPeter.Memishian@Sun.COM 	cc->cand_matchedinterface = cc->cand_ill == dstill;
16390Sstevel@tonic-gate 	cc->cand_matchedinterface_set = B_TRUE;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
16420Sstevel@tonic-gate 		return (CAND_TIE);
16430Sstevel@tonic-gate 	else if (cc->cand_matchedinterface)
16440Sstevel@tonic-gate 		return (CAND_PREFER);
16450Sstevel@tonic-gate 	else
16460Sstevel@tonic-gate 		return (CAND_AVOID);
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate /*
16500Sstevel@tonic-gate  * Prefer source addresses whose label matches the destination's label.
16510Sstevel@tonic-gate  */
16520Sstevel@tonic-gate static rule_res_t
rule_label(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)16533448Sdh155122 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
16540Sstevel@tonic-gate {
16550Sstevel@tonic-gate 	char *label;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	if (!bc->cand_matchedlabel_set) {
16583448Sdh155122 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst);
16590Sstevel@tonic-gate 		bc->cand_matchedlabel =
16600Sstevel@tonic-gate 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
16610Sstevel@tonic-gate 		bc->cand_matchedlabel_set = B_TRUE;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16643448Sdh155122 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst);
16650Sstevel@tonic-gate 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
16660Sstevel@tonic-gate 	cc->cand_matchedlabel_set = B_TRUE;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
16690Sstevel@tonic-gate 		return (CAND_TIE);
16700Sstevel@tonic-gate 	else if (cc->cand_matchedlabel)
16710Sstevel@tonic-gate 		return (CAND_PREFER);
16720Sstevel@tonic-gate 	else
16730Sstevel@tonic-gate 		return (CAND_AVOID);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate /*
16770Sstevel@tonic-gate  * Prefer public addresses over temporary ones.  An application can reverse
16780Sstevel@tonic-gate  * the logic of this rule and prefer temporary addresses by using the
16790Sstevel@tonic-gate  * IPV6_SRC_PREFERENCES socket option.
16800Sstevel@tonic-gate  */
16813448Sdh155122 /* ARGSUSED3 */
16820Sstevel@tonic-gate static rule_res_t
rule_temporary(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)16833448Sdh155122 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
16843448Sdh155122     ip_stack_t *ipst)
16850Sstevel@tonic-gate {
16860Sstevel@tonic-gate 	if (!bc->cand_istmp_set) {
16870Sstevel@tonic-gate 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
16880Sstevel@tonic-gate 		bc->cand_istmp_set = B_TRUE;
16890Sstevel@tonic-gate 	}
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
16920Sstevel@tonic-gate 	cc->cand_istmp_set = B_TRUE;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	if (bc->cand_istmp == cc->cand_istmp)
16950Sstevel@tonic-gate 		return (CAND_TIE);
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
16980Sstevel@tonic-gate 		return (CAND_PREFER);
16990Sstevel@tonic-gate 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
17000Sstevel@tonic-gate 		return (CAND_PREFER);
17010Sstevel@tonic-gate 	else
17020Sstevel@tonic-gate 		return (CAND_AVOID);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate /*
17063431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination
17073431Scarlsonj  * under the interface mask.  This gets us on the same subnet before applying
17083431Scarlsonj  * any Solaris-specific rules.
17090Sstevel@tonic-gate  */
17103448Sdh155122 /* ARGSUSED3 */
17110Sstevel@tonic-gate static rule_res_t
rule_ifprefix(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)17123448Sdh155122 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
17133448Sdh155122     ip_stack_t *ipst)
17140Sstevel@tonic-gate {
17153431Scarlsonj 	if (!bc->cand_pref_eq_set) {
17163431Scarlsonj 		bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr,
17173431Scarlsonj 		    bc->cand_mask, *dstinfo->dst_addr);
17183431Scarlsonj 		bc->cand_pref_eq_set = B_TRUE;
17190Sstevel@tonic-gate 	}
17200Sstevel@tonic-gate 
17213431Scarlsonj 	cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask,
17223431Scarlsonj 	    *dstinfo->dst_addr);
17233431Scarlsonj 	cc->cand_pref_eq_set = B_TRUE;
17240Sstevel@tonic-gate 
17253431Scarlsonj 	if (bc->cand_pref_eq) {
17263431Scarlsonj 		if (cc->cand_pref_eq) {
17273431Scarlsonj 			if (!bc->cand_pref_len_set) {
17283431Scarlsonj 				bc->cand_pref_len =
17293431Scarlsonj 				    ip_mask_to_plen_v6(&bc->cand_mask);
17303431Scarlsonj 				bc->cand_pref_len_set = B_TRUE;
17313431Scarlsonj 			}
17323431Scarlsonj 			cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask);
17333431Scarlsonj 			cc->cand_pref_len_set = B_TRUE;
17343431Scarlsonj 			if (bc->cand_pref_len == cc->cand_pref_len)
17353431Scarlsonj 				return (CAND_TIE);
17363431Scarlsonj 			else if (bc->cand_pref_len > cc->cand_pref_len)
17373431Scarlsonj 				return (CAND_AVOID);
17383431Scarlsonj 			else
17393431Scarlsonj 				return (CAND_PREFER);
17403431Scarlsonj 		} else {
17413431Scarlsonj 			return (CAND_AVOID);
17423431Scarlsonj 		}
17433431Scarlsonj 	} else {
17443431Scarlsonj 		if (cc->cand_pref_eq)
17453431Scarlsonj 			return (CAND_PREFER);
17463431Scarlsonj 		else
17473431Scarlsonj 			return (CAND_TIE);
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate /*
17521676Sjpk  * Prefer to use zone-specific addresses when possible instead of all-zones
17531676Sjpk  * addresses.
17541676Sjpk  */
17551676Sjpk /* ARGSUSED2 */
17561676Sjpk static rule_res_t
rule_zone_specific(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)17573448Sdh155122 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
17583448Sdh155122     ip_stack_t *ipst)
17591676Sjpk {
17601676Sjpk 	if ((bc->cand_zoneid == ALL_ZONES) ==
17611676Sjpk 	    (cc->cand_zoneid == ALL_ZONES))
17621676Sjpk 		return (CAND_TIE);
17631676Sjpk 	else if (cc->cand_zoneid == ALL_ZONES)
17641676Sjpk 		return (CAND_AVOID);
17651676Sjpk 	else
17661676Sjpk 		return (CAND_PREFER);
17671676Sjpk }
17681676Sjpk 
17691676Sjpk /*
17703431Scarlsonj  * Prefer to use DHCPv6 (first) and static addresses (second) when possible
17713431Scarlsonj  * instead of statelessly autoconfigured addresses.
17723431Scarlsonj  *
17733431Scarlsonj  * This is done after trying all other preferences (and before the final tie
17743431Scarlsonj  * breaker) so that, if all else is equal, we select addresses configured by
17753431Scarlsonj  * DHCPv6 over other addresses.  We presume that DHCPv6 addresses, unlike
17763431Scarlsonj  * stateless autoconfigured addresses, are deliberately configured by an
17773431Scarlsonj  * administrator, and thus are correctly set up in DNS and network packet
17783431Scarlsonj  * filters.
17793431Scarlsonj  */
17803431Scarlsonj /* ARGSUSED2 */
17813431Scarlsonj static rule_res_t
rule_addr_type(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)17823448Sdh155122 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
17833448Sdh155122     ip_stack_t *ipst)
17843431Scarlsonj {
17853431Scarlsonj #define	ATYPE(x)	\
17863431Scarlsonj 	((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2
17873431Scarlsonj 	int bcval = ATYPE(bc->cand_flags);
17883431Scarlsonj 	int ccval = ATYPE(cc->cand_flags);
17893431Scarlsonj #undef ATYPE
17903431Scarlsonj 
17913431Scarlsonj 	if (bcval == ccval)
17923431Scarlsonj 		return (CAND_TIE);
17933431Scarlsonj 	else if (ccval < bcval)
17943431Scarlsonj 		return (CAND_PREFER);
17953431Scarlsonj 	else
17963431Scarlsonj 		return (CAND_AVOID);
17973431Scarlsonj }
17983431Scarlsonj 
17993431Scarlsonj /*
18003431Scarlsonj  * Prefer source addresses with longer matching prefix with the destination.
18013431Scarlsonj  * We do the longest matching prefix calculation by doing an xor of both
18023431Scarlsonj  * addresses with the destination, and pick the address with the longest string
18033431Scarlsonj  * of leading zeros, as per CommonPrefixLen() defined in RFC 3484.
18043431Scarlsonj  */
18053448Sdh155122 /* ARGSUSED3 */
18063431Scarlsonj static rule_res_t
rule_prefix(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)18073448Sdh155122 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
18083431Scarlsonj {
18093431Scarlsonj 	if (!bc->cand_common_pref_set) {
18103431Scarlsonj 		bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr,
18113431Scarlsonj 		    dstinfo->dst_addr);
18123431Scarlsonj 		bc->cand_common_pref_set = B_TRUE;
18133431Scarlsonj 	}
18143431Scarlsonj 
18153431Scarlsonj 	cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr,
18163431Scarlsonj 	    dstinfo->dst_addr);
18173431Scarlsonj 	cc->cand_common_pref_set = B_TRUE;
18183431Scarlsonj 
18193431Scarlsonj 	if (bc->cand_common_pref == cc->cand_common_pref)
18203431Scarlsonj 		return (CAND_TIE);
18213431Scarlsonj 	else if (bc->cand_common_pref > cc->cand_common_pref)
18223431Scarlsonj 		return (CAND_AVOID);
18233431Scarlsonj 	else
18243431Scarlsonj 		return (CAND_PREFER);
18253431Scarlsonj }
18263431Scarlsonj 
18273431Scarlsonj /*
18283431Scarlsonj  * Last rule: we must pick something, so just prefer the current best
18293431Scarlsonj  * candidate.
18303431Scarlsonj  */
18313431Scarlsonj /* ARGSUSED */
18323431Scarlsonj static rule_res_t
rule_must_be_last(cand_t * bc,cand_t * cc,const dstinfo_t * dstinfo,ip_stack_t * ipst)18333448Sdh155122 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
18343448Sdh155122     ip_stack_t *ipst)
18353431Scarlsonj {
18363431Scarlsonj 	return (CAND_AVOID);
18373431Scarlsonj }
18383431Scarlsonj 
18393431Scarlsonj /*
18400Sstevel@tonic-gate  * Determine the best source address given a destination address and a
18410Sstevel@tonic-gate  * destination ill.  If no suitable source address is found, it returns
18420Sstevel@tonic-gate  * NULL. If there is a usable address pointed to by the usesrc
18430Sstevel@tonic-gate  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
18440Sstevel@tonic-gate  * fine grained (i.e per interface)
18450Sstevel@tonic-gate  *
18460Sstevel@tonic-gate  * This implementation is based on the "Default Address Selection for IPv6"
18470Sstevel@tonic-gate  * specification produced by the IETF IPv6 working group.  It has been
18480Sstevel@tonic-gate  * implemented so that the list of addresses is only traversed once (the
18490Sstevel@tonic-gate  * specification's algorithm could traverse the list of addresses once for
18500Sstevel@tonic-gate  * every rule).
18510Sstevel@tonic-gate  *
18528485SPeter.Memishian@Sun.COM  * The restrict_ill argument restricts the algorithm to choose a source
18538485SPeter.Memishian@Sun.COM  * address that is assigned to the destination ill.  This is used when
18548485SPeter.Memishian@Sun.COM  * the destination address is a link-local or multicast address, and when
18550Sstevel@tonic-gate  * ipv6_strict_dst_multihoming is turned on.
18560Sstevel@tonic-gate  *
18570Sstevel@tonic-gate  * src_prefs is the caller's set of source address preferences.  If source
18580Sstevel@tonic-gate  * address selection is being called to determine the source address of a
185911042SErik.Nordmark@Sun.COM  * connected socket (from ip_set_destination_v6()), then the preferences are
186011042SErik.Nordmark@Sun.COM  * taken from conn_ixa->ixa_src_preferences.  These preferences can be set on a
18610Sstevel@tonic-gate  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
18620Sstevel@tonic-gate  * preference currently implemented is for rfc3041 temporary addresses.
18630Sstevel@tonic-gate  */
18640Sstevel@tonic-gate ipif_t *
ipif_select_source_v6(ill_t * dstill,const in6_addr_t * dst,boolean_t restrict_ill,uint32_t src_prefs,zoneid_t zoneid,boolean_t allow_usesrc,boolean_t * notreadyp)18650Sstevel@tonic-gate ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
186611042SErik.Nordmark@Sun.COM     boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid,
186711042SErik.Nordmark@Sun.COM     boolean_t allow_usesrc, boolean_t *notreadyp)
18680Sstevel@tonic-gate {
18690Sstevel@tonic-gate 	dstinfo_t	dstinfo;
18700Sstevel@tonic-gate 	char		dstr[INET6_ADDRSTRLEN];
18710Sstevel@tonic-gate 	char		sstr[INET6_ADDRSTRLEN];
18728485SPeter.Memishian@Sun.COM 	ipif_t		*ipif, *start_ipif, *next_ipif;
18738485SPeter.Memishian@Sun.COM 	ill_t		*ill, *usesrc_ill = NULL, *ipmp_ill = NULL;
18740Sstevel@tonic-gate 	ill_walk_context_t	ctx;
18750Sstevel@tonic-gate 	cand_t		best_c;	/* The best candidate */
18760Sstevel@tonic-gate 	cand_t		curr_c;	/* The current candidate */
18770Sstevel@tonic-gate 	uint_t		index;
18780Sstevel@tonic-gate 	boolean_t	first_candidate = B_TRUE;
18790Sstevel@tonic-gate 	rule_res_t	rule_result;
18801676Sjpk 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
18813448Sdh155122 	ip_stack_t	*ipst = dstill->ill_ipst;
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	/*
18840Sstevel@tonic-gate 	 * The list of ordering rules.  They are applied in the order they
18850Sstevel@tonic-gate 	 * appear in the list.
18860Sstevel@tonic-gate 	 *
18873431Scarlsonj 	 * Solaris doesn't currently support Mobile IPv6, so there's no
18883431Scarlsonj 	 * rule_mipv6 corresponding to rule 4 in the specification.
18890Sstevel@tonic-gate 	 */
18900Sstevel@tonic-gate 	rulef_t	rules[] = {
18910Sstevel@tonic-gate 		rule_isdst,
18920Sstevel@tonic-gate 		rule_scope,
18930Sstevel@tonic-gate 		rule_deprecated,
18940Sstevel@tonic-gate 		rule_preferred,
18950Sstevel@tonic-gate 		rule_interface,
18960Sstevel@tonic-gate 		rule_label,
18970Sstevel@tonic-gate 		rule_temporary,
18983431Scarlsonj 		rule_ifprefix,			/* local rules after this */
18991676Sjpk 		rule_zone_specific,
19003431Scarlsonj 		rule_addr_type,
19013431Scarlsonj 		rule_prefix,			/* local rules before this */
19023431Scarlsonj 		rule_must_be_last,		/* must always be last */
19030Sstevel@tonic-gate 		NULL
19040Sstevel@tonic-gate 	};
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	ASSERT(dstill->ill_isv6);
19070Sstevel@tonic-gate 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	/*
19100Sstevel@tonic-gate 	 * Check if there is a usable src address pointed to by the
19110Sstevel@tonic-gate 	 * usesrc ifindex. This has higher precedence since it is
19120Sstevel@tonic-gate 	 * finer grained (i.e per interface) v/s being system wide.
19130Sstevel@tonic-gate 	 */
191411042SErik.Nordmark@Sun.COM 	if (dstill->ill_usesrc_ifindex != 0 && allow_usesrc) {
19150Sstevel@tonic-gate 		if ((usesrc_ill =
19160Sstevel@tonic-gate 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
191711042SErik.Nordmark@Sun.COM 		    ipst)) != NULL) {
19180Sstevel@tonic-gate 			dstinfo.dst_ill = usesrc_ill;
19190Sstevel@tonic-gate 		} else {
19200Sstevel@tonic-gate 			return (NULL);
19210Sstevel@tonic-gate 		}
19228485SPeter.Memishian@Sun.COM 	} else if (IS_UNDER_IPMP(dstill)) {
19238485SPeter.Memishian@Sun.COM 		/*
19248485SPeter.Memishian@Sun.COM 		 * Test addresses should never be used for source address
19258485SPeter.Memishian@Sun.COM 		 * selection, so if we were passed an underlying ill, switch
19268485SPeter.Memishian@Sun.COM 		 * to the IPMP meta-interface.
19278485SPeter.Memishian@Sun.COM 		 */
19288485SPeter.Memishian@Sun.COM 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(dstill)) != NULL)
19298485SPeter.Memishian@Sun.COM 			dstinfo.dst_ill = ipmp_ill;
19308485SPeter.Memishian@Sun.COM 		else
19318485SPeter.Memishian@Sun.COM 			return (NULL);
19320Sstevel@tonic-gate 	} else {
19330Sstevel@tonic-gate 		dstinfo.dst_ill = dstill;
19340Sstevel@tonic-gate 	}
19350Sstevel@tonic-gate 
19361676Sjpk 	/*
19371676Sjpk 	 * If we're dealing with an unlabeled destination on a labeled system,
19381676Sjpk 	 * make sure that we ignore source addresses that are incompatible with
19391676Sjpk 	 * the destination's default label.  That destination's default label
19401676Sjpk 	 * must dominate the minimum label on the source address.
19411676Sjpk 	 *
19421676Sjpk 	 * (Note that this has to do with Trusted Solaris.  It's not related to
19431676Sjpk 	 * the labels described by ip6_asp_lookup.)
19441676Sjpk 	 */
19451676Sjpk 	dst_rhtp = NULL;
19461676Sjpk 	if (is_system_labeled()) {
19471676Sjpk 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
19481676Sjpk 		if (dst_rhtp == NULL)
19491676Sjpk 			return (NULL);
19501676Sjpk 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
19511676Sjpk 			TPC_RELE(dst_rhtp);
19521676Sjpk 			dst_rhtp = NULL;
19531676Sjpk 		}
19541676Sjpk 	}
19551676Sjpk 
19560Sstevel@tonic-gate 	dstinfo.dst_addr = dst;
19570Sstevel@tonic-gate 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
19583448Sdh155122 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst);
19590Sstevel@tonic-gate 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
19603448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19610Sstevel@tonic-gate 	/*
19620Sstevel@tonic-gate 	 * Section three of the I-D states that for multicast and
19630Sstevel@tonic-gate 	 * link-local destinations, the candidate set must be restricted to
19640Sstevel@tonic-gate 	 * an interface that is on the same link as the outgoing interface.
19650Sstevel@tonic-gate 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
19660Sstevel@tonic-gate 	 * restrict the source address to the destination link as doing
19670Sstevel@tonic-gate 	 * otherwise will almost certainly cause problems.
19680Sstevel@tonic-gate 	 */
19690Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
19703448Sdh155122 	    ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
19718485SPeter.Memishian@Sun.COM 		dstinfo.dst_restrict_ill = B_TRUE;
19722202Srk129064 	} else {
19730Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = restrict_ill;
19742202Srk129064 	}
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	bzero(&best_c, sizeof (cand_t));
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	/*
19798485SPeter.Memishian@Sun.COM 	 * Take a pass through the list of IPv6 interfaces to choose the best
19808485SPeter.Memishian@Sun.COM 	 * possible source address.  If restrict_ill is set, just use dst_ill.
19810Sstevel@tonic-gate 	 */
19828485SPeter.Memishian@Sun.COM 	if (dstinfo.dst_restrict_ill)
19838485SPeter.Memishian@Sun.COM 		ill = dstinfo.dst_ill;
19848485SPeter.Memishian@Sun.COM 	else
19853448Sdh155122 		ill = ILL_START_WALK_V6(&ctx, ipst);
19868485SPeter.Memishian@Sun.COM 
19878485SPeter.Memishian@Sun.COM 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19880Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
19890Sstevel@tonic-gate 
19902202Srk129064 		/*
19918485SPeter.Memishian@Sun.COM 		 * Test addresses should never be used for source address
19928485SPeter.Memishian@Sun.COM 		 * selection, so ignore underlying ills.
19932202Srk129064 		 */
19948485SPeter.Memishian@Sun.COM 		if (IS_UNDER_IPMP(ill))
19958485SPeter.Memishian@Sun.COM 			continue;
19968485SPeter.Memishian@Sun.COM 
19979658SSowmini.Varadhan@Sun.COM 		if (ill->ill_ipif == NULL)
19989658SSowmini.Varadhan@Sun.COM 			continue;
19998485SPeter.Memishian@Sun.COM 		/*
20008485SPeter.Memishian@Sun.COM 		 * For source address selection, we treat the ipif list as
20018485SPeter.Memishian@Sun.COM 		 * circular and continue until we get back to where we
20028485SPeter.Memishian@Sun.COM 		 * started.  This allows IPMP to vary source address selection
20038485SPeter.Memishian@Sun.COM 		 * (which improves inbound load spreading) by caching its last
20048485SPeter.Memishian@Sun.COM 		 * ending point and starting from there.  NOTE: we don't have
20058485SPeter.Memishian@Sun.COM 		 * to worry about ill_src_ipif changing ills since that can't
20068485SPeter.Memishian@Sun.COM 		 * happen on the IPMP ill.
20078485SPeter.Memishian@Sun.COM 		 */
20088485SPeter.Memishian@Sun.COM 		start_ipif = ill->ill_ipif;
20098485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
20108485SPeter.Memishian@Sun.COM 			start_ipif = ill->ill_src_ipif;
20118485SPeter.Memishian@Sun.COM 
20128485SPeter.Memishian@Sun.COM 		ipif = start_ipif;
20138485SPeter.Memishian@Sun.COM 		do {
20148485SPeter.Memishian@Sun.COM 			if ((next_ipif = ipif->ipif_next) == NULL)
20158485SPeter.Memishian@Sun.COM 				next_ipif = ill->ill_ipif;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
20180Sstevel@tonic-gate 				continue;
20190Sstevel@tonic-gate 
202011042SErik.Nordmark@Sun.COM 			if (!ipif->ipif_addr_ready) {
202111042SErik.Nordmark@Sun.COM 				if (notreadyp != NULL)
202211042SErik.Nordmark@Sun.COM 					*notreadyp = B_TRUE;
202311042SErik.Nordmark@Sun.COM 				continue;
202411042SErik.Nordmark@Sun.COM 			}
202511042SErik.Nordmark@Sun.COM 
20261676Sjpk 			if (zoneid != ALL_ZONES &&
20271676Sjpk 			    ipif->ipif_zoneid != zoneid &&
20281676Sjpk 			    ipif->ipif_zoneid != ALL_ZONES)
20290Sstevel@tonic-gate 				continue;
20300Sstevel@tonic-gate 
20311676Sjpk 			/*
20321676Sjpk 			 * Check compatibility of local address for
20331676Sjpk 			 * destination's default label if we're on a labeled
20341676Sjpk 			 * system.  Incompatible addresses can't be used at
20351676Sjpk 			 * all and must be skipped over.
20361676Sjpk 			 */
20371676Sjpk 			if (dst_rhtp != NULL) {
20381676Sjpk 				boolean_t incompat;
20391676Sjpk 
20401676Sjpk 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
20411676Sjpk 				    IPV6_VERSION, B_FALSE);
20421676Sjpk 				if (src_rhtp == NULL)
20431676Sjpk 					continue;
20441676Sjpk 				incompat =
20451676Sjpk 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20461676Sjpk 				    src_rhtp->tpc_tp.tp_doi !=
20471676Sjpk 				    dst_rhtp->tpc_tp.tp_doi ||
20481676Sjpk 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20491676Sjpk 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20501676Sjpk 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20511676Sjpk 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20521676Sjpk 				TPC_RELE(src_rhtp);
20531676Sjpk 				if (incompat)
20541676Sjpk 					continue;
20551676Sjpk 			}
20561676Sjpk 
20570Sstevel@tonic-gate 			if (first_candidate) {
20580Sstevel@tonic-gate 				/*
20590Sstevel@tonic-gate 				 * This is first valid address in the list.
20600Sstevel@tonic-gate 				 * It is automatically the best candidate
20610Sstevel@tonic-gate 				 * so far.
20620Sstevel@tonic-gate 				 */
20630Sstevel@tonic-gate 				best_c.cand_ipif = ipif;
20640Sstevel@tonic-gate 				first_candidate = B_FALSE;
20650Sstevel@tonic-gate 				continue;
20660Sstevel@tonic-gate 			}
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 			bzero(&curr_c, sizeof (cand_t));
20690Sstevel@tonic-gate 			curr_c.cand_ipif = ipif;
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 			/*
20720Sstevel@tonic-gate 			 * Compare this current candidate (curr_c) with the
20730Sstevel@tonic-gate 			 * best candidate (best_c) by applying the
20740Sstevel@tonic-gate 			 * comparison rules in order until one breaks the
20750Sstevel@tonic-gate 			 * tie.
20760Sstevel@tonic-gate 			 */
20770Sstevel@tonic-gate 			for (index = 0; rules[index] != NULL; index++) {
20780Sstevel@tonic-gate 				/* Apply a comparison rule. */
20798485SPeter.Memishian@Sun.COM 				rule_result = (rules[index])(&best_c, &curr_c,
20808485SPeter.Memishian@Sun.COM 				    &dstinfo, ipst);
20810Sstevel@tonic-gate 				if (rule_result == CAND_AVOID) {
20820Sstevel@tonic-gate 					/*
20830Sstevel@tonic-gate 					 * The best candidate is still the
20840Sstevel@tonic-gate 					 * best candidate.  Forget about
20850Sstevel@tonic-gate 					 * this current candidate and go on
20860Sstevel@tonic-gate 					 * to the next one.
20870Sstevel@tonic-gate 					 */
20880Sstevel@tonic-gate 					break;
20890Sstevel@tonic-gate 				} else if (rule_result == CAND_PREFER) {
20900Sstevel@tonic-gate 					/*
20910Sstevel@tonic-gate 					 * This candidate is prefered.  It
20920Sstevel@tonic-gate 					 * becomes the best candidate so
20930Sstevel@tonic-gate 					 * far.  Go on to the next address.
20940Sstevel@tonic-gate 					 */
20950Sstevel@tonic-gate 					best_c = curr_c;
20960Sstevel@tonic-gate 					break;
20970Sstevel@tonic-gate 				}
20980Sstevel@tonic-gate 				/* We have a tie, apply the next rule. */
20990Sstevel@tonic-gate 			}
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 			/*
21020Sstevel@tonic-gate 			 * The last rule must be a tie breaker rule and
21030Sstevel@tonic-gate 			 * must never produce a tie.  At this point, the
21040Sstevel@tonic-gate 			 * candidate should have either been rejected, or
21050Sstevel@tonic-gate 			 * have been prefered as the best candidate so far.
21060Sstevel@tonic-gate 			 */
21070Sstevel@tonic-gate 			ASSERT(rule_result != CAND_TIE);
21088485SPeter.Memishian@Sun.COM 		} while ((ipif = next_ipif) != start_ipif);
21098485SPeter.Memishian@Sun.COM 
21108485SPeter.Memishian@Sun.COM 		/*
21118485SPeter.Memishian@Sun.COM 		 * For IPMP, update the source ipif rotor to the next ipif,
21128485SPeter.Memishian@Sun.COM 		 * provided we can look it up.  (We must not use it if it's
21138485SPeter.Memishian@Sun.COM 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
21148485SPeter.Memishian@Sun.COM 		 * ipif_free() checked ill_src_ipif.)
21158485SPeter.Memishian@Sun.COM 		 */
21168485SPeter.Memishian@Sun.COM 		if (IS_IPMP(ill) && ipif != NULL) {
21178485SPeter.Memishian@Sun.COM 			mutex_enter(&ipif->ipif_ill->ill_lock);
21188485SPeter.Memishian@Sun.COM 			next_ipif = ipif->ipif_next;
211911042SErik.Nordmark@Sun.COM 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
21208485SPeter.Memishian@Sun.COM 				ill->ill_src_ipif = next_ipif;
21218485SPeter.Memishian@Sun.COM 			else
21228485SPeter.Memishian@Sun.COM 				ill->ill_src_ipif = NULL;
21238485SPeter.Memishian@Sun.COM 			mutex_exit(&ipif->ipif_ill->ill_lock);
21240Sstevel@tonic-gate 		}
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 		/*
21278485SPeter.Memishian@Sun.COM 		 * Only one ill to consider if dst_restrict_ill is set.
21280Sstevel@tonic-gate 		 */
21298485SPeter.Memishian@Sun.COM 		if (dstinfo.dst_restrict_ill)
21308485SPeter.Memishian@Sun.COM 			break;
21310Sstevel@tonic-gate 	}
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	ipif = best_c.cand_ipif;
21340Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
21350Sstevel@tonic-gate 	    dstinfo.dst_ill->ill_name,
21360Sstevel@tonic-gate 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
21370Sstevel@tonic-gate 	    (ipif == NULL ? "NULL" :
21380Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if (usesrc_ill != NULL)
21410Sstevel@tonic-gate 		ill_refrele(usesrc_ill);
21420Sstevel@tonic-gate 
21438485SPeter.Memishian@Sun.COM 	if (ipmp_ill != NULL)
21448485SPeter.Memishian@Sun.COM 		ill_refrele(ipmp_ill);
21458485SPeter.Memishian@Sun.COM 
21461676Sjpk 	if (dst_rhtp != NULL)
21471676Sjpk 		TPC_RELE(dst_rhtp);
21481676Sjpk 
21490Sstevel@tonic-gate 	if (ipif == NULL) {
21503448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
21510Sstevel@tonic-gate 		return (NULL);
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_ill->ill_lock);
215511042SErik.Nordmark@Sun.COM 	if (!IPIF_IS_CONDEMNED(ipif)) {
21560Sstevel@tonic-gate 		ipif_refhold_locked(ipif);
21570Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_ill->ill_lock);
21583448Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
21590Sstevel@tonic-gate 		return (ipif);
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_ill->ill_lock);
21623448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
21630Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
21640Sstevel@tonic-gate 	    " returning null \n", (void *)ipif));
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	return (NULL);
21670Sstevel@tonic-gate }
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate /*
217011042SErik.Nordmark@Sun.COM  * Pick a source address based on the destination ill and an optional setsrc
217111042SErik.Nordmark@Sun.COM  * address.
217211042SErik.Nordmark@Sun.COM  * The result is stored in srcp. If generation is set, then put the source
217311042SErik.Nordmark@Sun.COM  * generation number there before we look for the source address (to avoid
217411042SErik.Nordmark@Sun.COM  * missing changes in the set of source addresses.
217511042SErik.Nordmark@Sun.COM  * If flagsp is set, then us it to pass back ipif_flags.
217611042SErik.Nordmark@Sun.COM  *
217711042SErik.Nordmark@Sun.COM  * If the caller wants to cache the returned source address and detect when
217811042SErik.Nordmark@Sun.COM  * that might be stale, the caller should pass in a generation argument,
217911042SErik.Nordmark@Sun.COM  * which the caller can later compare against ips_src_generation
21800Sstevel@tonic-gate  *
218111042SErik.Nordmark@Sun.COM  * The precedence order for selecting an IPv6 source address is:
218211042SErik.Nordmark@Sun.COM  *  - RTF_SETSRC on the first ire in the recursive lookup always wins.
218311042SErik.Nordmark@Sun.COM  *  - If usrsrc is set, swap the ill to be the usesrc one.
218411042SErik.Nordmark@Sun.COM  *  - If IPMP is used on the ill, select a random address from the most
218511042SErik.Nordmark@Sun.COM  *    preferred ones below:
218611042SErik.Nordmark@Sun.COM  * That is followed by the long list of IPv6 source address selection rules
218711042SErik.Nordmark@Sun.COM  * starting with rule_isdst(), rule_scope(), etc.
218811042SErik.Nordmark@Sun.COM  *
218911042SErik.Nordmark@Sun.COM  * We have lower preference for ALL_ZONES IP addresses,
219011042SErik.Nordmark@Sun.COM  * as they pose problems with unlabeled destinations.
219111042SErik.Nordmark@Sun.COM  *
219211042SErik.Nordmark@Sun.COM  * Note that when multiple IP addresses match e.g., with rule_scope() we pick
219311042SErik.Nordmark@Sun.COM  * the first one if IPMP is not in use. With IPMP we randomize.
21940Sstevel@tonic-gate  */
219511042SErik.Nordmark@Sun.COM int
ip_select_source_v6(ill_t * ill,const in6_addr_t * setsrc,const in6_addr_t * dst,zoneid_t zoneid,ip_stack_t * ipst,uint_t restrict_ill,uint32_t src_prefs,in6_addr_t * srcp,uint32_t * generation,uint64_t * flagsp)219611042SErik.Nordmark@Sun.COM ip_select_source_v6(ill_t *ill, const in6_addr_t *setsrc, const in6_addr_t *dst,
219711042SErik.Nordmark@Sun.COM     zoneid_t zoneid, ip_stack_t *ipst, uint_t restrict_ill, uint32_t src_prefs,
219811042SErik.Nordmark@Sun.COM     in6_addr_t *srcp, uint32_t *generation, uint64_t *flagsp)
21990Sstevel@tonic-gate {
220011042SErik.Nordmark@Sun.COM 	ipif_t *ipif;
220111042SErik.Nordmark@Sun.COM 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
22020Sstevel@tonic-gate 
220311042SErik.Nordmark@Sun.COM 	if (flagsp != NULL)
220411042SErik.Nordmark@Sun.COM 		*flagsp = 0;
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 	/*
220711042SErik.Nordmark@Sun.COM 	 * Need to grab the generation number before we check to
220811042SErik.Nordmark@Sun.COM 	 * avoid a race with a change to the set of local addresses.
220911042SErik.Nordmark@Sun.COM 	 * No lock needed since the thread which updates the set of local
221011042SErik.Nordmark@Sun.COM 	 * addresses use ipif/ill locks and exit those (hence a store memory
221111042SErik.Nordmark@Sun.COM 	 * barrier) before doing the atomic increase of ips_src_generation.
22120Sstevel@tonic-gate 	 */
221311042SErik.Nordmark@Sun.COM 	if (generation != NULL) {
221411042SErik.Nordmark@Sun.COM 		*generation = ipst->ips_src_generation;
22150Sstevel@tonic-gate 	}
22160Sstevel@tonic-gate 
221711042SErik.Nordmark@Sun.COM 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
221811042SErik.Nordmark@Sun.COM 	if (setsrc != NULL && !IN6_IS_ADDR_UNSPECIFIED(setsrc)) {
221911042SErik.Nordmark@Sun.COM 		*srcp = *setsrc;
222011042SErik.Nordmark@Sun.COM 		return (0);
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
222311042SErik.Nordmark@Sun.COM 	ipif = ipif_select_source_v6(ill, dst, restrict_ill, src_prefs, zoneid,
222411042SErik.Nordmark@Sun.COM 	    B_TRUE, &notready);
222511042SErik.Nordmark@Sun.COM 	if (ipif == NULL) {
222611042SErik.Nordmark@Sun.COM 		if (notready)
222711042SErik.Nordmark@Sun.COM 			return (ENETDOWN);
222811042SErik.Nordmark@Sun.COM 		else
222911042SErik.Nordmark@Sun.COM 			return (EADDRNOTAVAIL);
22300Sstevel@tonic-gate 	}
223111042SErik.Nordmark@Sun.COM 	*srcp = ipif->ipif_v6lcl_addr;
223211042SErik.Nordmark@Sun.COM 	if (flagsp != NULL)
223311042SErik.Nordmark@Sun.COM 		*flagsp = ipif->ipif_flags;
223411042SErik.Nordmark@Sun.COM 	ipif_refrele(ipif);
223511042SErik.Nordmark@Sun.COM 	return (0);
22360Sstevel@tonic-gate }
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate /*
22390Sstevel@tonic-gate  * Perform an attach and bind to get phys addr plus info_req for
22400Sstevel@tonic-gate  * the physical device.
22410Sstevel@tonic-gate  * q and mp represents an ioctl which will be queued waiting for
22420Sstevel@tonic-gate  * completion of the DLPI message exchange.
224311042SErik.Nordmark@Sun.COM  * MUST be called on an ill queue.
22440Sstevel@tonic-gate  *
224511042SErik.Nordmark@Sun.COM  * Returns EINPROGRESS when mp has been consumed by queueing it.
224611042SErik.Nordmark@Sun.COM  * The ioctl will complete in ip_rput.
22470Sstevel@tonic-gate  */
22480Sstevel@tonic-gate int
ill_dl_phys(ill_t * ill,ipif_t * ipif,mblk_t * mp,queue_t * q)22490Sstevel@tonic-gate ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22500Sstevel@tonic-gate {
22510Sstevel@tonic-gate 	mblk_t	*v6token_mp = NULL;
22520Sstevel@tonic-gate 	mblk_t	*v6lla_mp = NULL;
225310616SSebastien.Roy@Sun.COM 	mblk_t	*dest_mp = NULL;
22540Sstevel@tonic-gate 	mblk_t	*phys_mp = NULL;
22550Sstevel@tonic-gate 	mblk_t	*info_mp = NULL;
22560Sstevel@tonic-gate 	mblk_t	*attach_mp = NULL;
22570Sstevel@tonic-gate 	mblk_t	*bind_mp = NULL;
22580Sstevel@tonic-gate 	mblk_t	*unbind_mp = NULL;
22590Sstevel@tonic-gate 	mblk_t	*notify_mp = NULL;
226011076SCathy.Zhou@Sun.COM 	mblk_t  *capab_mp = NULL;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
22630Sstevel@tonic-gate 	ASSERT(ill->ill_dlpi_style_set);
22640Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	if (ill->ill_isv6) {
22670Sstevel@tonic-gate 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
22680Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
22690Sstevel@tonic-gate 		if (v6token_mp == NULL)
22700Sstevel@tonic-gate 			goto bad;
22710Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
22720Sstevel@tonic-gate 		    DL_IPV6_TOKEN;
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
22750Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
22760Sstevel@tonic-gate 		if (v6lla_mp == NULL)
22770Sstevel@tonic-gate 			goto bad;
22780Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
22790Sstevel@tonic-gate 		    DL_IPV6_LINK_LAYER_ADDR;
22800Sstevel@tonic-gate 	}
22810Sstevel@tonic-gate 
228210616SSebastien.Roy@Sun.COM 	if (ill->ill_mactype == DL_IPV4 || ill->ill_mactype == DL_IPV6) {
228310616SSebastien.Roy@Sun.COM 		dest_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
228410616SSebastien.Roy@Sun.COM 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
228510616SSebastien.Roy@Sun.COM 		if (dest_mp == NULL)
228610616SSebastien.Roy@Sun.COM 			goto bad;
228710616SSebastien.Roy@Sun.COM 		((dl_phys_addr_req_t *)dest_mp->b_rptr)->dl_addr_type =
228810616SSebastien.Roy@Sun.COM 		    DL_CURR_DEST_ADDR;
228910616SSebastien.Roy@Sun.COM 	}
229010616SSebastien.Roy@Sun.COM 
22910Sstevel@tonic-gate 	/*
22920Sstevel@tonic-gate 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
22930Sstevel@tonic-gate 	 */
22940Sstevel@tonic-gate 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
22950Sstevel@tonic-gate 	    DL_NOTIFY_REQ);
22960Sstevel@tonic-gate 	if (notify_mp == NULL)
22970Sstevel@tonic-gate 		goto bad;
22980Sstevel@tonic-gate 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
22990Sstevel@tonic-gate 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
23009073SCathy.Zhou@Sun.COM 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG |
23019743SGirish.Moodalbail@Sun.COM 	    DL_NOTE_PROMISC_ON_PHYS | DL_NOTE_PROMISC_OFF_PHYS |
2302*13123SErik.Nordmark@Sun.COM 	    DL_NOTE_REPLUMB | DL_NOTE_ALLOWED_IPS | DL_NOTE_SDU_SIZE2);
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
23050Sstevel@tonic-gate 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
23060Sstevel@tonic-gate 	if (phys_mp == NULL)
23070Sstevel@tonic-gate 		goto bad;
23080Sstevel@tonic-gate 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
23090Sstevel@tonic-gate 	    DL_CURR_PHYS_ADDR;
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	info_mp = ip_dlpi_alloc(
23120Sstevel@tonic-gate 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
23130Sstevel@tonic-gate 	    DL_INFO_REQ);
23140Sstevel@tonic-gate 	if (info_mp == NULL)
23150Sstevel@tonic-gate 		goto bad;
23160Sstevel@tonic-gate 
231711076SCathy.Zhou@Sun.COM 	ASSERT(ill->ill_dlpi_capab_state == IDCS_UNKNOWN);
231811076SCathy.Zhou@Sun.COM 	capab_mp = ip_dlpi_alloc(sizeof (dl_capability_req_t),
231911076SCathy.Zhou@Sun.COM 	    DL_CAPABILITY_REQ);
232011076SCathy.Zhou@Sun.COM 	if (capab_mp == NULL)
232111076SCathy.Zhou@Sun.COM 		goto bad;
232211076SCathy.Zhou@Sun.COM 
23230Sstevel@tonic-gate 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
23240Sstevel@tonic-gate 	    DL_BIND_REQ);
23250Sstevel@tonic-gate 	if (bind_mp == NULL)
23260Sstevel@tonic-gate 		goto bad;
23270Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
23280Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
23310Sstevel@tonic-gate 	if (unbind_mp == NULL)
23320Sstevel@tonic-gate 		goto bad;
23330Sstevel@tonic-gate 
23344360Smeem 	/* If we need to attach, pre-alloc and initialize the mblk */
23350Sstevel@tonic-gate 	if (ill->ill_needs_attach) {
23360Sstevel@tonic-gate 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
23370Sstevel@tonic-gate 		    DL_ATTACH_REQ);
23380Sstevel@tonic-gate 		if (attach_mp == NULL)
23390Sstevel@tonic-gate 			goto bad;
23400Sstevel@tonic-gate 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
23410Sstevel@tonic-gate 	}
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	/*
23440Sstevel@tonic-gate 	 * Here we are going to delay the ioctl ack until after
23450Sstevel@tonic-gate 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
23460Sstevel@tonic-gate 	 * original ioctl message before sending the requests
23470Sstevel@tonic-gate 	 */
23480Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
23490Sstevel@tonic-gate 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
23500Sstevel@tonic-gate 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
23510Sstevel@tonic-gate 	/*
23520Sstevel@tonic-gate 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
23530Sstevel@tonic-gate 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
23540Sstevel@tonic-gate 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
23550Sstevel@tonic-gate 	 */
23560Sstevel@tonic-gate 	ill->ill_phys_addr_pend = 0;
23570Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	if (attach_mp != NULL) {
23600Sstevel@tonic-gate 		ip1dbg(("ill_dl_phys: attach\n"));
23610Sstevel@tonic-gate 		ill_dlpi_send(ill, attach_mp);
23620Sstevel@tonic-gate 	}
23630Sstevel@tonic-gate 	ill_dlpi_send(ill, bind_mp);
23640Sstevel@tonic-gate 	ill_dlpi_send(ill, info_mp);
236511076SCathy.Zhou@Sun.COM 
236611076SCathy.Zhou@Sun.COM 	/*
236711076SCathy.Zhou@Sun.COM 	 * Send the capability request to get the VRRP capability information.
236811076SCathy.Zhou@Sun.COM 	 */
236911076SCathy.Zhou@Sun.COM 	ill_capability_send(ill, capab_mp);
237011076SCathy.Zhou@Sun.COM 
237110616SSebastien.Roy@Sun.COM 	if (v6token_mp != NULL)
23720Sstevel@tonic-gate 		ill_dlpi_send(ill, v6token_mp);
237310616SSebastien.Roy@Sun.COM 	if (v6lla_mp != NULL)
23740Sstevel@tonic-gate 		ill_dlpi_send(ill, v6lla_mp);
237510616SSebastien.Roy@Sun.COM 	if (dest_mp != NULL)
237610616SSebastien.Roy@Sun.COM 		ill_dlpi_send(ill, dest_mp);
23770Sstevel@tonic-gate 	ill_dlpi_send(ill, phys_mp);
23780Sstevel@tonic-gate 	ill_dlpi_send(ill, notify_mp);
23790Sstevel@tonic-gate 	ill_dlpi_send(ill, unbind_mp);
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	/*
23820Sstevel@tonic-gate 	 * This operation will complete in ip_rput_dlpi_writer with either
23830Sstevel@tonic-gate 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
23840Sstevel@tonic-gate 	 */
23850Sstevel@tonic-gate 	return (EINPROGRESS);
23860Sstevel@tonic-gate bad:
23874360Smeem 	freemsg(v6token_mp);
23884360Smeem 	freemsg(v6lla_mp);
238910616SSebastien.Roy@Sun.COM 	freemsg(dest_mp);
23904360Smeem 	freemsg(phys_mp);
23914360Smeem 	freemsg(info_mp);
23924360Smeem 	freemsg(attach_mp);
23934360Smeem 	freemsg(bind_mp);
239411076SCathy.Zhou@Sun.COM 	freemsg(capab_mp);
23954360Smeem 	freemsg(unbind_mp);
23964360Smeem 	freemsg(notify_mp);
23970Sstevel@tonic-gate 	return (ENOMEM);
23980Sstevel@tonic-gate }
23990Sstevel@tonic-gate 
240011042SErik.Nordmark@Sun.COM /* Add room for tcp+ip headers */
24010Sstevel@tonic-gate uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate /*
24040Sstevel@tonic-gate  * DLPI is up.
24050Sstevel@tonic-gate  * Create all the IREs associated with an interface bring up multicast.
24060Sstevel@tonic-gate  * Set the interface flag and finish other initialization
24070Sstevel@tonic-gate  * that potentially had to be differed to after DL_BIND_ACK.
24080Sstevel@tonic-gate  */
24090Sstevel@tonic-gate int
ipif_up_done_v6(ipif_t * ipif)24100Sstevel@tonic-gate ipif_up_done_v6(ipif_t *ipif)
24110Sstevel@tonic-gate {
24120Sstevel@tonic-gate 	ill_t	*ill = ipif->ipif_ill;
24130Sstevel@tonic-gate 	int	err;
24140Sstevel@tonic-gate 	boolean_t loopback = B_FALSE;
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
24174459Skcpoon 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
241811042SErik.Nordmark@Sun.COM 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done_v6",
241911042SErik.Nordmark@Sun.COM 	    ill_t *, ill, ipif_t *, ipif);
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	/* Check if this is a loopback interface */
24220Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_wq == NULL)
24230Sstevel@tonic-gate 		loopback = B_TRUE;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
24260Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
24270Sstevel@tonic-gate 
242811042SErik.Nordmark@Sun.COM 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
242911042SErik.Nordmark@Sun.COM 		nce_t *loop_nce = NULL;
243011042SErik.Nordmark@Sun.COM 		uint16_t flags = (NCE_F_MYADDR | NCE_F_NONUD | NCE_F_AUTHORITY);
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 		/*
24330Sstevel@tonic-gate 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
24340Sstevel@tonic-gate 		 * ipif_lookup_on_name(), but in the case of zones we can have
24350Sstevel@tonic-gate 		 * several loopback addresses on lo0. So all the interfaces with
24360Sstevel@tonic-gate 		 * loopback addresses need to be marked IRE_LOOPBACK.
24370Sstevel@tonic-gate 		 */
24380Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
24390Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOOPBACK;
24400Sstevel@tonic-gate 		else
24410Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOCAL;
244211042SErik.Nordmark@Sun.COM 		if (ill->ill_net_type != IRE_LOOPBACK)
244311042SErik.Nordmark@Sun.COM 			flags |= NCE_F_PUBLISH;
244411042SErik.Nordmark@Sun.COM 		err = nce_lookup_then_add_v6(ill, NULL,
244511042SErik.Nordmark@Sun.COM 		    ill->ill_phys_addr_length,
244611042SErik.Nordmark@Sun.COM 		    &ipif->ipif_v6lcl_addr, flags, ND_REACHABLE, &loop_nce);
244711042SErik.Nordmark@Sun.COM 
244811042SErik.Nordmark@Sun.COM 		/* A shared-IP zone sees EEXIST for lo0:N */
244911042SErik.Nordmark@Sun.COM 		if (err == 0 || err == EEXIST) {
245011042SErik.Nordmark@Sun.COM 			ipif->ipif_added_nce = 1;
245111042SErik.Nordmark@Sun.COM 			loop_nce->nce_ipif_cnt++;
245211042SErik.Nordmark@Sun.COM 			nce_refrele(loop_nce);
245311042SErik.Nordmark@Sun.COM 			err = 0;
245411042SErik.Nordmark@Sun.COM 		} else {
245511042SErik.Nordmark@Sun.COM 			ASSERT(loop_nce == NULL);
245611042SErik.Nordmark@Sun.COM 			return (err);
245711042SErik.Nordmark@Sun.COM 		}
245811042SErik.Nordmark@Sun.COM 	}
245911042SErik.Nordmark@Sun.COM 
246011042SErik.Nordmark@Sun.COM 	err = ipif_add_ires_v6(ipif, loopback);
246111042SErik.Nordmark@Sun.COM 	if (err != 0) {
246211042SErik.Nordmark@Sun.COM 		/*
246311042SErik.Nordmark@Sun.COM 		 * See comments about return value from
246411042SErik.Nordmark@Sun.COM 		 * ipif_addr_availability_check() in ipif_add_ires_v6().
246511042SErik.Nordmark@Sun.COM 		 */
246611042SErik.Nordmark@Sun.COM 		if (err != EADDRINUSE) {
246711042SErik.Nordmark@Sun.COM 			ipif_ndp_down(ipif);
246811042SErik.Nordmark@Sun.COM 		} else {
246911042SErik.Nordmark@Sun.COM 			/*
247011042SErik.Nordmark@Sun.COM 			 * Make IPMP aware of the deleted ipif so that
247111042SErik.Nordmark@Sun.COM 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
247211042SErik.Nordmark@Sun.COM 			 * can be completed. Note that we do not want to
247311042SErik.Nordmark@Sun.COM 			 * destroy the nce that was created on the ipmp_ill
247411042SErik.Nordmark@Sun.COM 			 * for the active copy of the duplicate address in
247511042SErik.Nordmark@Sun.COM 			 * use.
247611042SErik.Nordmark@Sun.COM 			 */
247711042SErik.Nordmark@Sun.COM 			if (IS_IPMP(ill))
247811042SErik.Nordmark@Sun.COM 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
247911042SErik.Nordmark@Sun.COM 			err = EADDRNOTAVAIL;
248011042SErik.Nordmark@Sun.COM 		}
248111042SErik.Nordmark@Sun.COM 		return (err);
24820Sstevel@tonic-gate 	}
24830Sstevel@tonic-gate 
248411042SErik.Nordmark@Sun.COM 	if (ill->ill_ipif_up_count == 1 && !loopback) {
248511042SErik.Nordmark@Sun.COM 		/* Recover any additional IREs entries for this ill */
248611042SErik.Nordmark@Sun.COM 		(void) ill_recover_saved_ire(ill);
248711042SErik.Nordmark@Sun.COM 	}
248811042SErik.Nordmark@Sun.COM 
248911042SErik.Nordmark@Sun.COM 	if (ill->ill_need_recover_multicast) {
24900Sstevel@tonic-gate 		/*
249111042SErik.Nordmark@Sun.COM 		 * Need to recover all multicast memberships in the driver.
249211042SErik.Nordmark@Sun.COM 		 * This had to be deferred until we had attached.
249311042SErik.Nordmark@Sun.COM 		 */
249411042SErik.Nordmark@Sun.COM 		ill_recover_multicast(ill);
249511042SErik.Nordmark@Sun.COM 	}
249611042SErik.Nordmark@Sun.COM 
249711042SErik.Nordmark@Sun.COM 	if (ill->ill_ipif_up_count == 1) {
249811042SErik.Nordmark@Sun.COM 		/*
249911042SErik.Nordmark@Sun.COM 		 * Since the interface is now up, it may now be active.
25000Sstevel@tonic-gate 		 */
250111042SErik.Nordmark@Sun.COM 		if (IS_UNDER_IPMP(ill))
250211042SErik.Nordmark@Sun.COM 			ipmp_ill_refresh_active(ill);
25030Sstevel@tonic-gate 	}
25040Sstevel@tonic-gate 
250511042SErik.Nordmark@Sun.COM 	/* Join the allhosts multicast address and the solicited node MC */
250611042SErik.Nordmark@Sun.COM 	ipif_multicast_up(ipif);
250711042SErik.Nordmark@Sun.COM 
250811042SErik.Nordmark@Sun.COM 	/* Perhaps ilgs should use this ill */
250911042SErik.Nordmark@Sun.COM 	update_conn_ill(NULL, ill->ill_ipst);
251011042SErik.Nordmark@Sun.COM 
251111042SErik.Nordmark@Sun.COM 	if (ipif->ipif_addr_ready)
251211042SErik.Nordmark@Sun.COM 		ipif_up_notify(ipif);
251311042SErik.Nordmark@Sun.COM 
251411042SErik.Nordmark@Sun.COM 	return (0);
251511042SErik.Nordmark@Sun.COM }
251611042SErik.Nordmark@Sun.COM 
251711042SErik.Nordmark@Sun.COM /*
251811042SErik.Nordmark@Sun.COM  * Add the IREs associated with the ipif.
251911042SErik.Nordmark@Sun.COM  * Those MUST be explicitly removed in ipif_delete_ires_v6.
252011042SErik.Nordmark@Sun.COM  */
252111042SErik.Nordmark@Sun.COM static int
ipif_add_ires_v6(ipif_t * ipif,boolean_t loopback)252211042SErik.Nordmark@Sun.COM ipif_add_ires_v6(ipif_t *ipif, boolean_t loopback)
252311042SErik.Nordmark@Sun.COM {
252411042SErik.Nordmark@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
252511042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ill->ill_ipst;
252611042SErik.Nordmark@Sun.COM 	in6_addr_t	v6addr;
252711042SErik.Nordmark@Sun.COM 	in6_addr_t	route_mask;
252811042SErik.Nordmark@Sun.COM 	int		err;
252911042SErik.Nordmark@Sun.COM 	char		buf[INET6_ADDRSTRLEN];
253011042SErik.Nordmark@Sun.COM 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
253111077SErik.Nordmark@Sun.COM 	ire_t		*ire_if = NULL;
253211634SNitin.Hande@Sun.COM 	in6_addr_t	*gw;
253311042SErik.Nordmark@Sun.COM 
25340Sstevel@tonic-gate 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
25350Sstevel@tonic-gate 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
25361676Sjpk 
25371676Sjpk 		/*
25381676Sjpk 		 * If we're on a labeled system then make sure that zone-
25391676Sjpk 		 * private addresses have proper remote host database entries.
25401676Sjpk 		 */
25411676Sjpk 		if (is_system_labeled() &&
25421676Sjpk 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
25431676Sjpk 			if (ip6opt_ls == 0) {
25441676Sjpk 				cmn_err(CE_WARN, "IPv6 not enabled "
25451676Sjpk 				    "via /etc/system");
25461676Sjpk 				return (EINVAL);
25471676Sjpk 			}
25481676Sjpk 			if (!tsol_check_interface_address(ipif))
25491676Sjpk 				return (EINVAL);
25501676Sjpk 		}
25511676Sjpk 
255211634SNitin.Hande@Sun.COM 		if (loopback)
255311634SNitin.Hande@Sun.COM 			gw = &ipif->ipif_v6lcl_addr;
255411634SNitin.Hande@Sun.COM 		else
255511634SNitin.Hande@Sun.COM 			gw = NULL;
255611634SNitin.Hande@Sun.COM 
25570Sstevel@tonic-gate 		/* Register the source address for __sin6_src_id */
25580Sstevel@tonic-gate 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
25593448Sdh155122 		    ipif->ipif_zoneid, ipst);
25600Sstevel@tonic-gate 		if (err != 0) {
256111042SErik.Nordmark@Sun.COM 			ip0dbg(("ipif_add_ires_v6: srcid_insert %d\n", err));
25620Sstevel@tonic-gate 			return (err);
25630Sstevel@tonic-gate 		}
25640Sstevel@tonic-gate 		/*
25650Sstevel@tonic-gate 		 * If the interface address is set, create the LOCAL
25660Sstevel@tonic-gate 		 * or LOOPBACK IRE.
25670Sstevel@tonic-gate 		 */
256811042SErik.Nordmark@Sun.COM 		ip1dbg(("ipif_add_ires_v6: creating IRE %d for %s\n",
25690Sstevel@tonic-gate 		    ipif->ipif_ire_type,
25700Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
25710Sstevel@tonic-gate 		    buf, sizeof (buf))));
25720Sstevel@tonic-gate 
257311042SErik.Nordmark@Sun.COM 		ire_local = ire_create_v6(
25740Sstevel@tonic-gate 		    &ipif->ipif_v6lcl_addr,		/* dest address */
25750Sstevel@tonic-gate 		    &ipv6_all_ones,			/* mask */
257611634SNitin.Hande@Sun.COM 		    gw,					/* gateway */
25770Sstevel@tonic-gate 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
257811042SErik.Nordmark@Sun.COM 		    ipif->ipif_ill,			/* interface */
257911042SErik.Nordmark@Sun.COM 		    ipif->ipif_zoneid,
258011042SErik.Nordmark@Sun.COM 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
258111042SErik.Nordmark@Sun.COM 		    RTF_PRIVATE : 0) | RTF_KERNEL,
25823448Sdh155122 		    NULL,
25833448Sdh155122 		    ipst);
258411042SErik.Nordmark@Sun.COM 		if (ire_local == NULL) {
258511042SErik.Nordmark@Sun.COM 			ip1dbg(("ipif_up_done_v6: NULL ire_local\n"));
258611042SErik.Nordmark@Sun.COM 			err = ENOMEM;
258711042SErik.Nordmark@Sun.COM 			goto bad;
258811042SErik.Nordmark@Sun.COM 		}
25890Sstevel@tonic-gate 	}
25900Sstevel@tonic-gate 
259110616SSebastien.Roy@Sun.COM 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
259211042SErik.Nordmark@Sun.COM 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
25930Sstevel@tonic-gate 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
25940Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
25950Sstevel@tonic-gate 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
25960Sstevel@tonic-gate 		v6addr = ipif->ipif_v6subnet;
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
25990Sstevel@tonic-gate 			route_mask = ipv6_all_ones;
26000Sstevel@tonic-gate 		} else {
26010Sstevel@tonic-gate 			route_mask = ipif->ipif_v6net_mask;
26020Sstevel@tonic-gate 		}
26030Sstevel@tonic-gate 
260411042SErik.Nordmark@Sun.COM 		ip1dbg(("ipif_add_ires_v6: creating if IRE %d for %s\n",
26050Sstevel@tonic-gate 		    ill->ill_net_type,
26060Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
26070Sstevel@tonic-gate 
260811077SErik.Nordmark@Sun.COM 		ire_if = ire_create_v6(
26090Sstevel@tonic-gate 		    &v6addr,			/* dest pref */
26100Sstevel@tonic-gate 		    &route_mask,		/* mask */
261111042SErik.Nordmark@Sun.COM 		    &ipif->ipif_v6lcl_addr,	/* gateway */
26120Sstevel@tonic-gate 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
261311042SErik.Nordmark@Sun.COM 		    ipif->ipif_ill,
261411042SErik.Nordmark@Sun.COM 		    ipif->ipif_zoneid,
261511042SErik.Nordmark@Sun.COM 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
261611042SErik.Nordmark@Sun.COM 		    RTF_PRIVATE : 0) | RTF_KERNEL,
26173448Sdh155122 		    NULL,
26183448Sdh155122 		    ipst);
261911077SErik.Nordmark@Sun.COM 		if (ire_if == NULL) {
262011077SErik.Nordmark@Sun.COM 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
26210Sstevel@tonic-gate 			err = ENOMEM;
26220Sstevel@tonic-gate 			goto bad;
26230Sstevel@tonic-gate 		}
26240Sstevel@tonic-gate 	}
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	/*
26278485SPeter.Memishian@Sun.COM 	 * Need to atomically check for IP address availability under
26288485SPeter.Memishian@Sun.COM 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
26298485SPeter.Memishian@Sun.COM 	 * ills or new ipifs can be added while we are checking availability.
26300Sstevel@tonic-gate 	 */
26313448Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
26323448Sdh155122 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
26330Sstevel@tonic-gate 	ill->ill_ipif_up_count++;
26340Sstevel@tonic-gate 	ipif->ipif_flags |= IPIF_UP;
26350Sstevel@tonic-gate 	err = ip_addr_availability_check(ipif);
26363448Sdh155122 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
26373448Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	if (err != 0) {
26400Sstevel@tonic-gate 		/*
26410Sstevel@tonic-gate 		 * Our address may already be up on the same ill. In this case,
26420Sstevel@tonic-gate 		 * the external resolver entry for our ipif replaced the one for
26430Sstevel@tonic-gate 		 * the other ipif. So we don't want to delete it (otherwise the
26440Sstevel@tonic-gate 		 * other ipif would be unable to send packets).
26450Sstevel@tonic-gate 		 * ip_addr_availability_check() identifies this case for us and
264611042SErik.Nordmark@Sun.COM 		 * returns EADDRINUSE; Caller must  turn it into EADDRNOTAVAIL
26470Sstevel@tonic-gate 		 * which is the expected error code.
26489287SSowmini.Varadhan@Sun.COM 		 *
264911042SErik.Nordmark@Sun.COM 		 * Note that ipif_ndp_down() will only delete the nce in the
265011042SErik.Nordmark@Sun.COM 		 * case when the nce_ipif_cnt drops to 0.
26510Sstevel@tonic-gate 		 */
26520Sstevel@tonic-gate 		ill->ill_ipif_up_count--;
26530Sstevel@tonic-gate 		ipif->ipif_flags &= ~IPIF_UP;
26540Sstevel@tonic-gate 		goto bad;
26550Sstevel@tonic-gate 	}
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 	/*
26588485SPeter.Memishian@Sun.COM 	 * Add in all newly created IREs.
265911077SErik.Nordmark@Sun.COM 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
266011077SErik.Nordmark@Sun.COM 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
266111077SErik.Nordmark@Sun.COM 	 * a /128 route.
26620Sstevel@tonic-gate 	 */
266311077SErik.Nordmark@Sun.COM 	if (ire_if != NULL) {
266411077SErik.Nordmark@Sun.COM 		ire_if = ire_add(ire_if);
266511077SErik.Nordmark@Sun.COM 		if (ire_if == NULL) {
266611077SErik.Nordmark@Sun.COM 			err = ENOMEM;
266711077SErik.Nordmark@Sun.COM 			goto bad2;
266811077SErik.Nordmark@Sun.COM 		}
266911077SErik.Nordmark@Sun.COM #ifdef DEBUG
267011077SErik.Nordmark@Sun.COM 		ire_refhold_notr(ire_if);
267111077SErik.Nordmark@Sun.COM 		ire_refrele(ire_if);
267211077SErik.Nordmark@Sun.COM #endif
267311077SErik.Nordmark@Sun.COM 	}
267411042SErik.Nordmark@Sun.COM 	if (ire_local != NULL) {
267511042SErik.Nordmark@Sun.COM 		ire_local = ire_add(ire_local);
267611077SErik.Nordmark@Sun.COM 		if (ire_local == NULL) {
267711077SErik.Nordmark@Sun.COM 			err = ENOMEM;
267811077SErik.Nordmark@Sun.COM 			goto bad2;
267911077SErik.Nordmark@Sun.COM 		}
268011042SErik.Nordmark@Sun.COM #ifdef DEBUG
268111077SErik.Nordmark@Sun.COM 		ire_refhold_notr(ire_local);
268211077SErik.Nordmark@Sun.COM 		ire_refrele(ire_local);
268311042SErik.Nordmark@Sun.COM #endif
268411042SErik.Nordmark@Sun.COM 	}
268511042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
268611042SErik.Nordmark@Sun.COM 	if (ire_local != NULL)
268711042SErik.Nordmark@Sun.COM 		ipif->ipif_ire_local = ire_local;
268811077SErik.Nordmark@Sun.COM 	if (ire_if != NULL)
268911077SErik.Nordmark@Sun.COM 		ipif->ipif_ire_if = ire_if;
269011042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
269111042SErik.Nordmark@Sun.COM 	ire_local = NULL;
269211077SErik.Nordmark@Sun.COM 	ire_if = NULL;
26930Sstevel@tonic-gate 
26948023SPhil.Kirk@Sun.COM 	if (ipif->ipif_addr_ready)
26958023SPhil.Kirk@Sun.COM 		ipif_up_notify(ipif);
26960Sstevel@tonic-gate 	return (0);
26970Sstevel@tonic-gate 
269811077SErik.Nordmark@Sun.COM bad2:
269911077SErik.Nordmark@Sun.COM 	ill->ill_ipif_up_count--;
270011077SErik.Nordmark@Sun.COM 	ipif->ipif_flags &= ~IPIF_UP;
270111077SErik.Nordmark@Sun.COM 
27020Sstevel@tonic-gate bad:
270311042SErik.Nordmark@Sun.COM 	if (ire_local != NULL)
270411042SErik.Nordmark@Sun.COM 		ire_delete(ire_local);
270511077SErik.Nordmark@Sun.COM 	if (ire_if != NULL)
270611077SErik.Nordmark@Sun.COM 		ire_delete(ire_if);
270711077SErik.Nordmark@Sun.COM 
270811077SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
270911077SErik.Nordmark@Sun.COM 	ire_local = ipif->ipif_ire_local;
271011077SErik.Nordmark@Sun.COM 	ipif->ipif_ire_local = NULL;
271111077SErik.Nordmark@Sun.COM 	ire_if = ipif->ipif_ire_if;
271211077SErik.Nordmark@Sun.COM 	ipif->ipif_ire_if = NULL;
271311077SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
271411077SErik.Nordmark@Sun.COM 	if (ire_local != NULL) {
271511077SErik.Nordmark@Sun.COM 		ire_delete(ire_local);
271611077SErik.Nordmark@Sun.COM 		ire_refrele_notr(ire_local);
271711077SErik.Nordmark@Sun.COM 	}
271811077SErik.Nordmark@Sun.COM 	if (ire_if != NULL) {
271911077SErik.Nordmark@Sun.COM 		ire_delete(ire_if);
272011077SErik.Nordmark@Sun.COM 		ire_refrele_notr(ire_if);
27210Sstevel@tonic-gate 	}
27223448Sdh155122 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	return (err);
27250Sstevel@tonic-gate }
27260Sstevel@tonic-gate 
272711042SErik.Nordmark@Sun.COM /* Remove all the IREs created by ipif_add_ires_v6 */
272811042SErik.Nordmark@Sun.COM void
ipif_delete_ires_v6(ipif_t * ipif)272911042SErik.Nordmark@Sun.COM ipif_delete_ires_v6(ipif_t *ipif)
273011042SErik.Nordmark@Sun.COM {
273111042SErik.Nordmark@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
273211042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ill->ill_ipst;
273311042SErik.Nordmark@Sun.COM 	ire_t		*ire;
273411042SErik.Nordmark@Sun.COM 
273511042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
273611077SErik.Nordmark@Sun.COM 	ire = ipif->ipif_ire_local;
273711077SErik.Nordmark@Sun.COM 	ipif->ipif_ire_local = NULL;
273811077SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
273911077SErik.Nordmark@Sun.COM 	if (ire != NULL) {
274011042SErik.Nordmark@Sun.COM 		/*
274111042SErik.Nordmark@Sun.COM 		 * Move count to ipif so we don't loose the count due to
274211042SErik.Nordmark@Sun.COM 		 * a down/up dance.
274311042SErik.Nordmark@Sun.COM 		 */
274411042SErik.Nordmark@Sun.COM 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
274511042SErik.Nordmark@Sun.COM 
274611042SErik.Nordmark@Sun.COM 		ire_delete(ire);
274711042SErik.Nordmark@Sun.COM 		ire_refrele_notr(ire);
274811042SErik.Nordmark@Sun.COM 	}
274911077SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
275011077SErik.Nordmark@Sun.COM 	ire = ipif->ipif_ire_if;
275111077SErik.Nordmark@Sun.COM 	ipif->ipif_ire_if = NULL;
275211077SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
275311077SErik.Nordmark@Sun.COM 	if (ire != NULL) {
275411042SErik.Nordmark@Sun.COM 		ire_delete(ire);
275511077SErik.Nordmark@Sun.COM 		ire_refrele_notr(ire);
275611042SErik.Nordmark@Sun.COM 	}
275711042SErik.Nordmark@Sun.COM }
275811042SErik.Nordmark@Sun.COM 
27590Sstevel@tonic-gate /*
276011042SErik.Nordmark@Sun.COM  * Delete an ND entry if it exists.
27610Sstevel@tonic-gate  */
27620Sstevel@tonic-gate /* ARGSUSED */
27630Sstevel@tonic-gate int
ip_siocdelndp_v6(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)27640Sstevel@tonic-gate ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
27650Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
27660Sstevel@tonic-gate {
27670Sstevel@tonic-gate 	sin6_t		*sin6;
27680Sstevel@tonic-gate 	struct lifreq	*lifr;
27690Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
27708485SPeter.Memishian@Sun.COM 	ill_t		*ill = ipif->ipif_ill;
277111042SErik.Nordmark@Sun.COM 	nce_t		*nce;
27728485SPeter.Memishian@Sun.COM 
27738485SPeter.Memishian@Sun.COM 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
27740Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
27750Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
27760Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
27770Sstevel@tonic-gate 		return (EINVAL);
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
27800Sstevel@tonic-gate 		return (EINVAL);
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
27830Sstevel@tonic-gate 		return (EAFNOSUPPORT);
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	sin6 = (sin6_t *)&lnr->lnr_addr;
27868485SPeter.Memishian@Sun.COM 
27878485SPeter.Memishian@Sun.COM 	/*
27888485SPeter.Memishian@Sun.COM 	 * Since ND mappings must be consistent across an IPMP group, prohibit
278911042SErik.Nordmark@Sun.COM 	 * deleting ND mappings on underlying interfaces.
279011042SErik.Nordmark@Sun.COM 	 * Don't allow deletion of mappings for local addresses.
27918485SPeter.Memishian@Sun.COM 	 */
27928485SPeter.Memishian@Sun.COM 	if (IS_UNDER_IPMP(ill))
27938485SPeter.Memishian@Sun.COM 		return (EPERM);
27948485SPeter.Memishian@Sun.COM 
279511042SErik.Nordmark@Sun.COM 	nce = nce_lookup_v6(ill, &sin6->sin6_addr);
279611042SErik.Nordmark@Sun.COM 	if (nce == NULL)
279711042SErik.Nordmark@Sun.COM 		return (ESRCH);
279811042SErik.Nordmark@Sun.COM 
279911042SErik.Nordmark@Sun.COM 	if (NCE_MYADDR(nce->nce_common)) {
280011042SErik.Nordmark@Sun.COM 		nce_refrele(nce);
280111042SErik.Nordmark@Sun.COM 		return (EPERM);
28028485SPeter.Memishian@Sun.COM 	}
28038485SPeter.Memishian@Sun.COM 
280411042SErik.Nordmark@Sun.COM 	/*
280511042SErik.Nordmark@Sun.COM 	 * delete the nce_common which will also delete the nces on any
280611042SErik.Nordmark@Sun.COM 	 * under_ill in the case of ipmp.
280711042SErik.Nordmark@Sun.COM 	 */
280811042SErik.Nordmark@Sun.COM 	ncec_delete(nce->nce_common);
280911042SErik.Nordmark@Sun.COM 	nce_refrele(nce);
28100Sstevel@tonic-gate 	return (0);
28110Sstevel@tonic-gate }
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate /*
28140Sstevel@tonic-gate  * Return nbr cache info.
28150Sstevel@tonic-gate  */
28160Sstevel@tonic-gate /* ARGSUSED */
28170Sstevel@tonic-gate int
ip_siocqueryndp_v6(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)28180Sstevel@tonic-gate ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
28190Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
28200Sstevel@tonic-gate {
28210Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
28220Sstevel@tonic-gate 	struct lifreq	*lifr;
28230Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
28260Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
28270Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
28280Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
28290Sstevel@tonic-gate 		return (EINVAL);
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
28320Sstevel@tonic-gate 		return (EINVAL);
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
28350Sstevel@tonic-gate 		return (EAFNOSUPPORT);
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
28380Sstevel@tonic-gate 		return (EINVAL);
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	return (ndp_query(ill, lnr));
28410Sstevel@tonic-gate }
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate /*
28440Sstevel@tonic-gate  * Perform an update of the nd entry for the specified address.
28450Sstevel@tonic-gate  */
28460Sstevel@tonic-gate /* ARGSUSED */
28470Sstevel@tonic-gate int
ip_siocsetndp_v6(ipif_t * ipif,sin_t * dummy_sin,queue_t * q,mblk_t * mp,ip_ioctl_cmd_t * ipip,void * dummy_ifreq)28480Sstevel@tonic-gate ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
28490Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
28500Sstevel@tonic-gate {
28518485SPeter.Memishian@Sun.COM 	sin6_t		*sin6;
28520Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
28530Sstevel@tonic-gate 	struct	lifreq	*lifr;
28540Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
28558485SPeter.Memishian@Sun.COM 	ire_t		*ire;
28563448Sdh155122 
28570Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
28580Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
28590Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
28600Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
28610Sstevel@tonic-gate 		return (EINVAL);
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
28640Sstevel@tonic-gate 		return (EINVAL);
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
28670Sstevel@tonic-gate 		return (EAFNOSUPPORT);
28680Sstevel@tonic-gate 
28698485SPeter.Memishian@Sun.COM 	sin6 = (sin6_t *)&lnr->lnr_addr;
28708485SPeter.Memishian@Sun.COM 
28718485SPeter.Memishian@Sun.COM 	/*
28728485SPeter.Memishian@Sun.COM 	 * Since ND mappings must be consistent across an IPMP group, prohibit
28738485SPeter.Memishian@Sun.COM 	 * updating ND mappings on underlying interfaces.  Also, since ND
28748485SPeter.Memishian@Sun.COM 	 * mappings for IPMP data addresses are owned by IP itself, prohibit
28758485SPeter.Memishian@Sun.COM 	 * updating them.
28768485SPeter.Memishian@Sun.COM 	 */
28778485SPeter.Memishian@Sun.COM 	if (IS_UNDER_IPMP(ill))
28788485SPeter.Memishian@Sun.COM 		return (EPERM);
28798485SPeter.Memishian@Sun.COM 
28808485SPeter.Memishian@Sun.COM 	if (IS_IPMP(ill)) {
288111042SErik.Nordmark@Sun.COM 		ire = ire_ftable_lookup_v6(&sin6->sin6_addr, NULL, NULL,
288211042SErik.Nordmark@Sun.COM 		    IRE_LOCAL, ill, ALL_ZONES, NULL,
288311042SErik.Nordmark@Sun.COM 		    MATCH_IRE_TYPE | MATCH_IRE_ILL, 0, ill->ill_ipst, NULL);
28848485SPeter.Memishian@Sun.COM 		if (ire != NULL) {
28858485SPeter.Memishian@Sun.COM 			ire_refrele(ire);
28868485SPeter.Memishian@Sun.COM 			return (EPERM);
28878485SPeter.Memishian@Sun.COM 		}
28888485SPeter.Memishian@Sun.COM 	}
28898485SPeter.Memishian@Sun.COM 
28900Sstevel@tonic-gate 	return (ndp_sioc_update(ill, lnr));
28910Sstevel@tonic-gate }
2892