xref: /onnv-gate/usr/src/uts/common/inet/ip/ip6_if.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1990 Mentat Inc.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * This file contains the interface control functions for IPv6.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <sys/sysmacros.h>
38*0Sstevel@tonic-gate #include <sys/stream.h>
39*0Sstevel@tonic-gate #include <sys/dlpi.h>
40*0Sstevel@tonic-gate #include <sys/stropts.h>
41*0Sstevel@tonic-gate #include <sys/strlog.h>
42*0Sstevel@tonic-gate #include <sys/ddi.h>
43*0Sstevel@tonic-gate #include <sys/cmn_err.h>
44*0Sstevel@tonic-gate #include <sys/kstat.h>
45*0Sstevel@tonic-gate #include <sys/debug.h>
46*0Sstevel@tonic-gate #include <sys/zone.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <sys/systm.h>
49*0Sstevel@tonic-gate #include <sys/param.h>
50*0Sstevel@tonic-gate #include <sys/socket.h>
51*0Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
52*0Sstevel@tonic-gate #include <sys/tihdr.h>
53*0Sstevel@tonic-gate #include <sys/isa_defs.h>
54*0Sstevel@tonic-gate #include <net/if.h>
55*0Sstevel@tonic-gate #include <net/if_types.h>
56*0Sstevel@tonic-gate #include <net/if_dl.h>
57*0Sstevel@tonic-gate #include <net/route.h>
58*0Sstevel@tonic-gate #include <sys/sockio.h>
59*0Sstevel@tonic-gate #include <netinet/in.h>
60*0Sstevel@tonic-gate #include <netinet/igmp_var.h>
61*0Sstevel@tonic-gate #include <netinet/ip6.h>
62*0Sstevel@tonic-gate #include <netinet/icmp6.h>
63*0Sstevel@tonic-gate #include <netinet/in.h>
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #include <inet/common.h>
66*0Sstevel@tonic-gate #include <inet/mi.h>
67*0Sstevel@tonic-gate #include <inet/nd.h>
68*0Sstevel@tonic-gate #include <inet/mib2.h>
69*0Sstevel@tonic-gate #include <inet/arp.h>
70*0Sstevel@tonic-gate #include <inet/ip.h>
71*0Sstevel@tonic-gate #include <inet/ip6.h>
72*0Sstevel@tonic-gate #include <inet/ip_multi.h>
73*0Sstevel@tonic-gate #include <inet/ip_ire.h>
74*0Sstevel@tonic-gate #include <inet/ip_rts.h>
75*0Sstevel@tonic-gate #include <inet/ip_ndp.h>
76*0Sstevel@tonic-gate #include <inet/ip_if.h>
77*0Sstevel@tonic-gate #include <inet/ip6_asp.h>
78*0Sstevel@tonic-gate #include <inet/tun.h>
79*0Sstevel@tonic-gate #include <inet/ipclassifier.h>
80*0Sstevel@tonic-gate #include <inet/sctp_ip.h>
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #include <netinet/igmp.h>
83*0Sstevel@tonic-gate #include <netinet/ip_mroute.h>
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static in6_addr_t	ipv6_ll_template =
86*0Sstevel@tonic-gate 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static ipif_t *
89*0Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
90*0Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate  * ipif_lookup_group_v6
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate ipif_t *
96*0Sstevel@tonic-gate ipif_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	ire_t	*ire;
99*0Sstevel@tonic-gate 	ipif_t	*ipif;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	ire = ire_lookup_multi_v6(group, zoneid);
102*0Sstevel@tonic-gate 	if (ire == NULL)
103*0Sstevel@tonic-gate 		return (NULL);
104*0Sstevel@tonic-gate 	ipif = ire->ire_ipif;
105*0Sstevel@tonic-gate 	ipif_refhold(ipif);
106*0Sstevel@tonic-gate 	ire_refrele(ire);
107*0Sstevel@tonic-gate 	return (ipif);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate  * ill_lookup_group_v6
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate ill_t *
114*0Sstevel@tonic-gate ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	ire_t	*ire;
117*0Sstevel@tonic-gate 	ill_t	*ill;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	ire = ire_lookup_multi_v6(group, zoneid);
120*0Sstevel@tonic-gate 	if (ire == NULL)
121*0Sstevel@tonic-gate 		return (NULL);
122*0Sstevel@tonic-gate 	ill = ire->ire_ipif->ipif_ill;
123*0Sstevel@tonic-gate 	ill_refhold(ill);
124*0Sstevel@tonic-gate 	ire_refrele(ire);
125*0Sstevel@tonic-gate 	return (ill);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate  * Look for an ipif with the specified interface address and destination.
130*0Sstevel@tonic-gate  * The destination address is used only for matching point-to-point interfaces.
131*0Sstevel@tonic-gate  */
132*0Sstevel@tonic-gate static ipif_t *
133*0Sstevel@tonic-gate ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
134*0Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	ipif_t	*ipif;
137*0Sstevel@tonic-gate 	ill_t	*ill;
138*0Sstevel@tonic-gate 	ipsq_t	*ipsq;
139*0Sstevel@tonic-gate 	ill_walk_context_t ctx;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (error != NULL)
142*0Sstevel@tonic-gate 		*error = 0;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/*
145*0Sstevel@tonic-gate 	 * First match all the point-to-point interfaces
146*0Sstevel@tonic-gate 	 * before looking at non-point-to-point interfaces.
147*0Sstevel@tonic-gate 	 * This is done to avoid returning non-point-to-point
148*0Sstevel@tonic-gate 	 * ipif instead of unnumbered point-to-point ipif.
149*0Sstevel@tonic-gate 	 */
150*0Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
151*0Sstevel@tonic-gate 	ill = ILL_START_WALK_V6(&ctx);
152*0Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
153*0Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
154*0Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
155*0Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
156*0Sstevel@tonic-gate 			/* Allow the ipif to be down */
157*0Sstevel@tonic-gate 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
158*0Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
159*0Sstevel@tonic-gate 			    if_addr)) &&
160*0Sstevel@tonic-gate 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
161*0Sstevel@tonic-gate 			    dst))) {
162*0Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
163*0Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
164*0Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
165*0Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
166*0Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
167*0Sstevel@tonic-gate 					return (ipif);
168*0Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
169*0Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
170*0Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
171*0Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
172*0Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
173*0Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
174*0Sstevel@tonic-gate 						ill);
175*0Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
176*0Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
177*0Sstevel@tonic-gate 					*error = EINPROGRESS;
178*0Sstevel@tonic-gate 					return (NULL);
179*0Sstevel@tonic-gate 				}
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
183*0Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
186*0Sstevel@tonic-gate 	/* lookup the ipif based on interface address */
187*0Sstevel@tonic-gate 	ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, q, mp, func,
188*0Sstevel@tonic-gate 	    error);
189*0Sstevel@tonic-gate 	ASSERT(ipif == NULL || ipif->ipif_isv6);
190*0Sstevel@tonic-gate 	return (ipif);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate  * Look for an ipif with the specified address. For point-point links
195*0Sstevel@tonic-gate  * we look for matches on either the destination address and the local
196*0Sstevel@tonic-gate  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
197*0Sstevel@tonic-gate  * is set.
198*0Sstevel@tonic-gate  * Matches on a specific ill if match_ill is set.
199*0Sstevel@tonic-gate  */
200*0Sstevel@tonic-gate /* ARGSUSED */
201*0Sstevel@tonic-gate ipif_t *
202*0Sstevel@tonic-gate ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
203*0Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error)
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	ipif_t	*ipif;
206*0Sstevel@tonic-gate 	ill_t	*ill;
207*0Sstevel@tonic-gate 	boolean_t  ptp = B_FALSE;
208*0Sstevel@tonic-gate 	ipsq_t	*ipsq;
209*0Sstevel@tonic-gate 	ill_walk_context_t ctx;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	if (error != NULL)
212*0Sstevel@tonic-gate 		*error = 0;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
215*0Sstevel@tonic-gate 	/*
216*0Sstevel@tonic-gate 	 * Repeat twice, first based on local addresses and
217*0Sstevel@tonic-gate 	 * next time for pointopoint.
218*0Sstevel@tonic-gate 	 */
219*0Sstevel@tonic-gate repeat:
220*0Sstevel@tonic-gate 	ill = ILL_START_WALK_V6(&ctx);
221*0Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
222*0Sstevel@tonic-gate 		if (match_ill != NULL && ill != match_ill) {
223*0Sstevel@tonic-gate 			continue;
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 		GRAB_CONN_LOCK(q);
226*0Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
227*0Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
228*0Sstevel@tonic-gate 			if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
229*0Sstevel@tonic-gate 				continue;
230*0Sstevel@tonic-gate 			/* Allow the ipif to be down */
231*0Sstevel@tonic-gate 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
232*0Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, addr) &&
233*0Sstevel@tonic-gate 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
234*0Sstevel@tonic-gate 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
235*0Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
236*0Sstevel@tonic-gate 			    addr))) {
237*0Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(ipif)) {
238*0Sstevel@tonic-gate 					ipif_refhold_locked(ipif);
239*0Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
240*0Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
241*0Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
242*0Sstevel@tonic-gate 					return (ipif);
243*0Sstevel@tonic-gate 				} else if (IPIF_CAN_WAIT(ipif, q)) {
244*0Sstevel@tonic-gate 					ipsq = ill->ill_phyint->phyint_ipsq;
245*0Sstevel@tonic-gate 					mutex_enter(&ipsq->ipsq_lock);
246*0Sstevel@tonic-gate 					mutex_exit(&ill->ill_lock);
247*0Sstevel@tonic-gate 					rw_exit(&ill_g_lock);
248*0Sstevel@tonic-gate 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
249*0Sstevel@tonic-gate 						ill);
250*0Sstevel@tonic-gate 					mutex_exit(&ipsq->ipsq_lock);
251*0Sstevel@tonic-gate 					RELEASE_CONN_LOCK(q);
252*0Sstevel@tonic-gate 					*error = EINPROGRESS;
253*0Sstevel@tonic-gate 					return (NULL);
254*0Sstevel@tonic-gate 				}
255*0Sstevel@tonic-gate 			}
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
258*0Sstevel@tonic-gate 		RELEASE_CONN_LOCK(q);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	/* Repeat once more if needed */
262*0Sstevel@tonic-gate 	if (ptp) {
263*0Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
264*0Sstevel@tonic-gate 		if (error != NULL)
265*0Sstevel@tonic-gate 			*error = ENXIO;
266*0Sstevel@tonic-gate 		return (NULL);
267*0Sstevel@tonic-gate 	}
268*0Sstevel@tonic-gate 	ptp = B_TRUE;
269*0Sstevel@tonic-gate 	goto repeat;
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*
273*0Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a local
274*0Sstevel@tonic-gate  * interface address.  This is currently only called when an attempt is made
275*0Sstevel@tonic-gate  * to set a local address.
276*0Sstevel@tonic-gate  *
277*0Sstevel@tonic-gate  * Does not allow a v4-mapped address, an address that equals the subnet
278*0Sstevel@tonic-gate  * anycast address, ... a multicast address, ...
279*0Sstevel@tonic-gate  */
280*0Sstevel@tonic-gate boolean_t
281*0Sstevel@tonic-gate ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	in6_addr_t subnet;
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
286*0Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	/*
289*0Sstevel@tonic-gate 	 * Don't allow all zeroes or host part, but allow
290*0Sstevel@tonic-gate 	 * all ones netmask.
291*0Sstevel@tonic-gate 	 */
292*0Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
293*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
294*0Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
295*0Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
296*0Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
297*0Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr))
298*0Sstevel@tonic-gate 		return (B_FALSE);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	return (B_TRUE);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * Perform various checks to verify that an address would make sense as a
305*0Sstevel@tonic-gate  * remote/subnet interface address.
306*0Sstevel@tonic-gate  */
307*0Sstevel@tonic-gate boolean_t
308*0Sstevel@tonic-gate ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate 	in6_addr_t subnet;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
313*0Sstevel@tonic-gate 		return (B_TRUE);	/* Allow all zeros */
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
316*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
317*0Sstevel@tonic-gate 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
318*0Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
319*0Sstevel@tonic-gate 	    IN6_IS_ADDR_MULTICAST(addr) ||
320*0Sstevel@tonic-gate 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
321*0Sstevel@tonic-gate 		return (B_FALSE);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	return (B_TRUE);
324*0Sstevel@tonic-gate }
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate /*
327*0Sstevel@tonic-gate  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
328*0Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
329*0Sstevel@tonic-gate  * (for link-local destinations and gateways).
330*0Sstevel@tonic-gate  */
331*0Sstevel@tonic-gate /* ARGSUSED1 */
332*0Sstevel@tonic-gate int
333*0Sstevel@tonic-gate ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
334*0Sstevel@tonic-gate     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
335*0Sstevel@tonic-gate     ipif_t *ipif_arg, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate 	ire_t	*ire;
338*0Sstevel@tonic-gate 	ire_t	*gw_ire = NULL;
339*0Sstevel@tonic-gate 	ipif_t	*ipif;
340*0Sstevel@tonic-gate 	boolean_t ipif_refheld = B_FALSE;
341*0Sstevel@tonic-gate 	uint_t	type;
342*0Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
343*0Sstevel@tonic-gate 	int	error;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	if (ire_arg != NULL)
346*0Sstevel@tonic-gate 		*ire_arg = NULL;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	/*
349*0Sstevel@tonic-gate 	 * Prevent routes with a zero gateway from being created (since
350*0Sstevel@tonic-gate 	 * interfaces can currently be plumbed and brought up with no assigned
351*0Sstevel@tonic-gate 	 * address).
352*0Sstevel@tonic-gate 	 */
353*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
354*0Sstevel@tonic-gate 		return (ENETUNREACH);
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	/*
357*0Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
358*0Sstevel@tonic-gate 	 * to all ones (regardless if one was supplied).
359*0Sstevel@tonic-gate 	 */
360*0Sstevel@tonic-gate 	if (flags & RTF_HOST)
361*0Sstevel@tonic-gate 		mask = &ipv6_all_ones;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	/*
364*0Sstevel@tonic-gate 	 * Get the ipif, if any, corresponding to the gw_addr
365*0Sstevel@tonic-gate 	 */
366*0Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func,
367*0Sstevel@tonic-gate 	    &error);
368*0Sstevel@tonic-gate 	if (ipif != NULL)
369*0Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
370*0Sstevel@tonic-gate 	else if (error == EINPROGRESS) {
371*0Sstevel@tonic-gate 		ip1dbg(("ip_rt_add_v6: null and EINPROGRESS"));
372*0Sstevel@tonic-gate 		return (error);
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	/*
376*0Sstevel@tonic-gate 	 * GateD will attempt to create routes with a loopback interface
377*0Sstevel@tonic-gate 	 * address as the gateway and with RTF_GATEWAY set.  We allow
378*0Sstevel@tonic-gate 	 * these routes to be added, but create them as interface routes
379*0Sstevel@tonic-gate 	 * since the gateway is an interface address.
380*0Sstevel@tonic-gate 	 */
381*0Sstevel@tonic-gate 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
382*0Sstevel@tonic-gate 		flags &= ~RTF_GATEWAY;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	/*
385*0Sstevel@tonic-gate 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
386*0Sstevel@tonic-gate 	 * and the gateway address provided is one of the system's interface
387*0Sstevel@tonic-gate 	 * addresses.  By using the routing socket interface and supplying an
388*0Sstevel@tonic-gate 	 * RTA_IFP sockaddr with an interface index, an alternate method of
389*0Sstevel@tonic-gate 	 * specifying an interface route to be created is available which uses
390*0Sstevel@tonic-gate 	 * the interface index that specifies the outgoing interface rather than
391*0Sstevel@tonic-gate 	 * the address of an outgoing interface (which may not be able to
392*0Sstevel@tonic-gate 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
393*0Sstevel@tonic-gate 	 * flag, routes can be specified which not only specify the next-hop to
394*0Sstevel@tonic-gate 	 * be used when routing to a certain prefix, but also which outgoing
395*0Sstevel@tonic-gate 	 * interface should be used.
396*0Sstevel@tonic-gate 	 *
397*0Sstevel@tonic-gate 	 * Previously, interfaces would have unique addresses assigned to them
398*0Sstevel@tonic-gate 	 * and so the address assigned to a particular interface could be used
399*0Sstevel@tonic-gate 	 * to identify a particular interface.  One exception to this was the
400*0Sstevel@tonic-gate 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
401*0Sstevel@tonic-gate 	 *
402*0Sstevel@tonic-gate 	 * With the advent of IPv6 and its link-local addresses, this
403*0Sstevel@tonic-gate 	 * restriction was relaxed and interfaces could share addresses between
404*0Sstevel@tonic-gate 	 * themselves.  In fact, typically all of the link-local interfaces on
405*0Sstevel@tonic-gate 	 * an IPv6 node or router will have the same link-local address.  In
406*0Sstevel@tonic-gate 	 * order to differentiate between these interfaces, the use of an
407*0Sstevel@tonic-gate 	 * interface index is necessary and this index can be carried inside a
408*0Sstevel@tonic-gate 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
409*0Sstevel@tonic-gate 	 * of using the interface index, however, is that all of the ipif's that
410*0Sstevel@tonic-gate 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
411*0Sstevel@tonic-gate 	 * cannot be used to differentiate between ipif's (or logical
412*0Sstevel@tonic-gate 	 * interfaces) that belong to the same ill (physical interface).
413*0Sstevel@tonic-gate 	 *
414*0Sstevel@tonic-gate 	 * For example, in the following case involving IPv4 interfaces and
415*0Sstevel@tonic-gate 	 * logical interfaces
416*0Sstevel@tonic-gate 	 *
417*0Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
418*0Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
419*0Sstevel@tonic-gate 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
420*0Sstevel@tonic-gate 	 *
421*0Sstevel@tonic-gate 	 * the ipif's corresponding to each of these interface routes can be
422*0Sstevel@tonic-gate 	 * uniquely identified by the "gateway" (actually interface address).
423*0Sstevel@tonic-gate 	 *
424*0Sstevel@tonic-gate 	 * In this case involving multiple IPv6 default routes to a particular
425*0Sstevel@tonic-gate 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
426*0Sstevel@tonic-gate 	 * default route is of interest:
427*0Sstevel@tonic-gate 	 *
428*0Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if0
429*0Sstevel@tonic-gate 	 *	default		fe80::123:4567:89ab:cdef	U	if1
430*0Sstevel@tonic-gate 	 */
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	/* RTF_GATEWAY not set */
433*0Sstevel@tonic-gate 	if (!(flags & RTF_GATEWAY)) {
434*0Sstevel@tonic-gate 		queue_t	*stq;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 		/*
437*0Sstevel@tonic-gate 		 * As the interface index specified with the RTA_IFP sockaddr is
438*0Sstevel@tonic-gate 		 * the same for all ipif's off of an ill, the matching logic
439*0Sstevel@tonic-gate 		 * below uses MATCH_IRE_ILL if such an index was specified.
440*0Sstevel@tonic-gate 		 * This means that routes sharing the same prefix when added
441*0Sstevel@tonic-gate 		 * using a RTA_IFP sockaddr must have distinct interface
442*0Sstevel@tonic-gate 		 * indices (namely, they must be on distinct ill's).
443*0Sstevel@tonic-gate 		 *
444*0Sstevel@tonic-gate 		 * On the other hand, since the gateway address will usually be
445*0Sstevel@tonic-gate 		 * different for each ipif on the system, the matching logic
446*0Sstevel@tonic-gate 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
447*0Sstevel@tonic-gate 		 * route.  This means that interface routes for the same prefix
448*0Sstevel@tonic-gate 		 * can be created if they belong to distinct ipif's and if a
449*0Sstevel@tonic-gate 		 * RTA_IFP sockaddr is not present.
450*0Sstevel@tonic-gate 		 */
451*0Sstevel@tonic-gate 		if (ipif_arg != NULL) {
452*0Sstevel@tonic-gate 			if (ipif_refheld) {
453*0Sstevel@tonic-gate 				ipif_refrele(ipif);
454*0Sstevel@tonic-gate 				ipif_refheld = B_FALSE;
455*0Sstevel@tonic-gate 			}
456*0Sstevel@tonic-gate 			ipif = ipif_arg;
457*0Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
458*0Sstevel@tonic-gate 		} else {
459*0Sstevel@tonic-gate 			/*
460*0Sstevel@tonic-gate 			 * Check the ipif corresponding to the gw_addr
461*0Sstevel@tonic-gate 			 */
462*0Sstevel@tonic-gate 			if (ipif == NULL)
463*0Sstevel@tonic-gate 				return (ENETUNREACH);
464*0Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
465*0Sstevel@tonic-gate 		}
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 		ASSERT(ipif != NULL);
468*0Sstevel@tonic-gate 		/*
469*0Sstevel@tonic-gate 		 * We check for an existing entry at this point.
470*0Sstevel@tonic-gate 		 */
471*0Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
472*0Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, 0, IRE_INTERFACE,
473*0Sstevel@tonic-gate 		    ipif, NULL, ALL_ZONES, 0, match_flags);
474*0Sstevel@tonic-gate 		if (ire != NULL) {
475*0Sstevel@tonic-gate 			ire_refrele(ire);
476*0Sstevel@tonic-gate 			if (ipif_refheld)
477*0Sstevel@tonic-gate 				ipif_refrele(ipif);
478*0Sstevel@tonic-gate 			return (EEXIST);
479*0Sstevel@tonic-gate 		}
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
482*0Sstevel@tonic-gate 		    ? ipif->ipif_rq : ipif->ipif_wq;
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 		/*
485*0Sstevel@tonic-gate 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
486*0Sstevel@tonic-gate 		 * IRE_IF_RESOLVER with the modified address and netmask.
487*0Sstevel@tonic-gate 		 */
488*0Sstevel@tonic-gate 		ire = ire_create_v6(
489*0Sstevel@tonic-gate 		    dst_addr,
490*0Sstevel@tonic-gate 		    mask,
491*0Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr,
492*0Sstevel@tonic-gate 		    NULL,
493*0Sstevel@tonic-gate 		    &ipif->ipif_mtu,
494*0Sstevel@tonic-gate 		    NULL,
495*0Sstevel@tonic-gate 		    NULL,
496*0Sstevel@tonic-gate 		    stq,
497*0Sstevel@tonic-gate 		    ipif->ipif_net_type,
498*0Sstevel@tonic-gate 		    ipif->ipif_resolver_mp,
499*0Sstevel@tonic-gate 		    ipif,
500*0Sstevel@tonic-gate 		    NULL,
501*0Sstevel@tonic-gate 		    0,
502*0Sstevel@tonic-gate 		    0,
503*0Sstevel@tonic-gate 		    flags,
504*0Sstevel@tonic-gate 		    &ire_uinfo_null);
505*0Sstevel@tonic-gate 		if (ire == NULL) {
506*0Sstevel@tonic-gate 			if (ipif_refheld)
507*0Sstevel@tonic-gate 				ipif_refrele(ipif);
508*0Sstevel@tonic-gate 			return (ENOMEM);
509*0Sstevel@tonic-gate 		}
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 		/*
512*0Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
513*0Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
514*0Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
515*0Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
516*0Sstevel@tonic-gate 		 * when generating aggregate routes.)
517*0Sstevel@tonic-gate 		 *
518*0Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
519*0Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
520*0Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
521*0Sstevel@tonic-gate 		 *
522*0Sstevel@tonic-gate 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
523*0Sstevel@tonic-gate 		 * routine, but rather using ire_create_v6() directly.
524*0Sstevel@tonic-gate 		 */
525*0Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
526*0Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
527*0Sstevel@tonic-gate 		error = ire_add(&ire, q, mp, func);
528*0Sstevel@tonic-gate 		if (error == 0)
529*0Sstevel@tonic-gate 			goto save_ire;
530*0Sstevel@tonic-gate 		/*
531*0Sstevel@tonic-gate 		 * In the result of failure, ire_add() will have already
532*0Sstevel@tonic-gate 		 * deleted the ire in question, so there is no need to
533*0Sstevel@tonic-gate 		 * do that here.
534*0Sstevel@tonic-gate 		 */
535*0Sstevel@tonic-gate 		if (ipif_refheld)
536*0Sstevel@tonic-gate 			ipif_refrele(ipif);
537*0Sstevel@tonic-gate 		return (error);
538*0Sstevel@tonic-gate 	}
539*0Sstevel@tonic-gate 	if (ipif_refheld) {
540*0Sstevel@tonic-gate 		ipif_refrele(ipif);
541*0Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	/*
545*0Sstevel@tonic-gate 	 * Get an interface IRE for the specified gateway.
546*0Sstevel@tonic-gate 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
547*0Sstevel@tonic-gate 	 * gateway, it is currently unreachable and we fail the request
548*0Sstevel@tonic-gate 	 * accordingly.
549*0Sstevel@tonic-gate 	 */
550*0Sstevel@tonic-gate 	ipif = ipif_arg;
551*0Sstevel@tonic-gate 	if (ipif_arg != NULL)
552*0Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL;
553*0Sstevel@tonic-gate 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg,
554*0Sstevel@tonic-gate 	    NULL, ALL_ZONES, 0, match_flags);
555*0Sstevel@tonic-gate 	if (gw_ire == NULL)
556*0Sstevel@tonic-gate 		return (ENETUNREACH);
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	/*
559*0Sstevel@tonic-gate 	 * We create one of three types of IREs as a result of this request
560*0Sstevel@tonic-gate 	 * based on the netmask.  A netmask of all ones (which is automatically
561*0Sstevel@tonic-gate 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
562*0Sstevel@tonic-gate 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
563*0Sstevel@tonic-gate 	 * created.  Otherwise, an IRE_PREFIX route is created for the
564*0Sstevel@tonic-gate 	 * destination prefix.
565*0Sstevel@tonic-gate 	 */
566*0Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
567*0Sstevel@tonic-gate 		type = IRE_HOST;
568*0Sstevel@tonic-gate 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
569*0Sstevel@tonic-gate 		type = IRE_DEFAULT;
570*0Sstevel@tonic-gate 	else
571*0Sstevel@tonic-gate 		type = IRE_PREFIX;
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	/* check for a duplicate entry */
574*0Sstevel@tonic-gate 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ipif_arg,
575*0Sstevel@tonic-gate 	    NULL, ALL_ZONES, 0, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
576*0Sstevel@tonic-gate 	if (ire != NULL) {
577*0Sstevel@tonic-gate 		ire_refrele(gw_ire);
578*0Sstevel@tonic-gate 		ire_refrele(ire);
579*0Sstevel@tonic-gate 		return (EEXIST);
580*0Sstevel@tonic-gate 	}
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	/* Create the IRE. */
583*0Sstevel@tonic-gate 	ire = ire_create_v6(
584*0Sstevel@tonic-gate 	    dst_addr,				/* dest address */
585*0Sstevel@tonic-gate 	    mask,				/* mask */
586*0Sstevel@tonic-gate 	    /* src address assigned by the caller? */
587*0Sstevel@tonic-gate 	    (((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr)) ?
588*0Sstevel@tonic-gate 		src_addr : NULL),
589*0Sstevel@tonic-gate 	    gw_addr,				/* gateway address */
590*0Sstevel@tonic-gate 	    &gw_ire->ire_max_frag,
591*0Sstevel@tonic-gate 	    NULL,				/* no Fast Path header */
592*0Sstevel@tonic-gate 	    NULL,				/* no recv-from queue */
593*0Sstevel@tonic-gate 	    NULL,				/* no send-to queue */
594*0Sstevel@tonic-gate 	    (ushort_t)type,			/* IRE type */
595*0Sstevel@tonic-gate 	    NULL,
596*0Sstevel@tonic-gate 	    ipif_arg,
597*0Sstevel@tonic-gate 	    NULL,
598*0Sstevel@tonic-gate 	    0,
599*0Sstevel@tonic-gate 	    0,
600*0Sstevel@tonic-gate 	    flags,
601*0Sstevel@tonic-gate 	    &gw_ire->ire_uinfo);		/* Inherit ULP info from gw */
602*0Sstevel@tonic-gate 	if (ire == NULL) {
603*0Sstevel@tonic-gate 		ire_refrele(gw_ire);
604*0Sstevel@tonic-gate 		return (ENOMEM);
605*0Sstevel@tonic-gate 	}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	/*
608*0Sstevel@tonic-gate 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
609*0Sstevel@tonic-gate 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
610*0Sstevel@tonic-gate 	 */
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	/* Add the new IRE. */
613*0Sstevel@tonic-gate 	error = ire_add(&ire, q, mp, func);
614*0Sstevel@tonic-gate 	/*
615*0Sstevel@tonic-gate 	 * In the result of failure, ire_add() will have already
616*0Sstevel@tonic-gate 	 * deleted the ire in question, so there is no need to
617*0Sstevel@tonic-gate 	 * do that here.
618*0Sstevel@tonic-gate 	 */
619*0Sstevel@tonic-gate 	if (error != 0) {
620*0Sstevel@tonic-gate 		ire_refrele(gw_ire);
621*0Sstevel@tonic-gate 		return (error);
622*0Sstevel@tonic-gate 	}
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	if (flags & RTF_MULTIRT) {
625*0Sstevel@tonic-gate 		/*
626*0Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
627*0Sstevel@tonic-gate 		 * to add the dst address in the filtering database.
628*0Sstevel@tonic-gate 		 * Replicated inbound packets coming from that address
629*0Sstevel@tonic-gate 		 * will be filtered to discard the duplicates.
630*0Sstevel@tonic-gate 		 * It is not necessary to call the CGTP filter hook
631*0Sstevel@tonic-gate 		 * when the dst address is a multicast, because an
632*0Sstevel@tonic-gate 		 * IP source address cannot be a multicast.
633*0Sstevel@tonic-gate 		 */
634*0Sstevel@tonic-gate 		if ((ip_cgtp_filter_ops != NULL) &&
635*0Sstevel@tonic-gate 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
636*0Sstevel@tonic-gate 			int res = ip_cgtp_filter_ops->cfo_add_dest_v6(
637*0Sstevel@tonic-gate 			    &ire->ire_addr_v6,
638*0Sstevel@tonic-gate 			    &ire->ire_gateway_addr_v6,
639*0Sstevel@tonic-gate 			    &ire->ire_src_addr_v6,
640*0Sstevel@tonic-gate 			    &gw_ire->ire_src_addr_v6);
641*0Sstevel@tonic-gate 			if (res != 0) {
642*0Sstevel@tonic-gate 				ire_refrele(gw_ire);
643*0Sstevel@tonic-gate 				ire_delete(ire);
644*0Sstevel@tonic-gate 				return (res);
645*0Sstevel@tonic-gate 			}
646*0Sstevel@tonic-gate 		}
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate save_ire:
650*0Sstevel@tonic-gate 	if (gw_ire != NULL) {
651*0Sstevel@tonic-gate 		ire_refrele(gw_ire);
652*0Sstevel@tonic-gate 	}
653*0Sstevel@tonic-gate 	if (ipif != NULL) {
654*0Sstevel@tonic-gate 		mblk_t	*save_mp;
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 		/*
657*0Sstevel@tonic-gate 		 * Save enough information so that we can recreate the IRE if
658*0Sstevel@tonic-gate 		 * the interface goes down and then up.  The metrics associated
659*0Sstevel@tonic-gate 		 * with the route will be saved as well when rts_setmetrics() is
660*0Sstevel@tonic-gate 		 * called after the IRE has been created.  In the case where
661*0Sstevel@tonic-gate 		 * memory cannot be allocated, none of this information will be
662*0Sstevel@tonic-gate 		 * saved.
663*0Sstevel@tonic-gate 		 */
664*0Sstevel@tonic-gate 		save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
665*0Sstevel@tonic-gate 		if (save_mp != NULL) {
666*0Sstevel@tonic-gate 			ifrt_t	*ifrt;
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 			save_mp->b_wptr += sizeof (ifrt_t);
669*0Sstevel@tonic-gate 			ifrt = (ifrt_t *)save_mp->b_rptr;
670*0Sstevel@tonic-gate 			bzero(ifrt, sizeof (ifrt_t));
671*0Sstevel@tonic-gate 			ifrt->ifrt_type = ire->ire_type;
672*0Sstevel@tonic-gate 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
673*0Sstevel@tonic-gate 			mutex_enter(&ire->ire_lock);
674*0Sstevel@tonic-gate 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
675*0Sstevel@tonic-gate 			ifrt->ifrt_v6src_addr = ire->ire_src_addr_v6;
676*0Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
677*0Sstevel@tonic-gate 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
678*0Sstevel@tonic-gate 			ifrt->ifrt_flags = ire->ire_flags;
679*0Sstevel@tonic-gate 			ifrt->ifrt_max_frag = ire->ire_max_frag;
680*0Sstevel@tonic-gate 			mutex_enter(&ipif->ipif_saved_ire_lock);
681*0Sstevel@tonic-gate 			save_mp->b_cont = ipif->ipif_saved_ire_mp;
682*0Sstevel@tonic-gate 			ipif->ipif_saved_ire_mp = save_mp;
683*0Sstevel@tonic-gate 			ipif->ipif_saved_ire_cnt++;
684*0Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
685*0Sstevel@tonic-gate 		}
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 	if (ire_arg != NULL) {
688*0Sstevel@tonic-gate 		/*
689*0Sstevel@tonic-gate 		 * Store the ire that was successfully added into where ire_arg
690*0Sstevel@tonic-gate 		 * points to so that callers don't have to look it up
691*0Sstevel@tonic-gate 		 * themselves (but they are responsible for ire_refrele()ing
692*0Sstevel@tonic-gate 		 * the ire when they are finished with it).
693*0Sstevel@tonic-gate 		 */
694*0Sstevel@tonic-gate 		*ire_arg = ire;
695*0Sstevel@tonic-gate 	} else {
696*0Sstevel@tonic-gate 		ire_refrele(ire);		/* Held in ire_add */
697*0Sstevel@tonic-gate 	}
698*0Sstevel@tonic-gate 	if (ipif_refheld)
699*0Sstevel@tonic-gate 		ipif_refrele(ipif);
700*0Sstevel@tonic-gate 	return (0);
701*0Sstevel@tonic-gate }
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate /*
704*0Sstevel@tonic-gate  * ip_rt_delete_v6 is called to delete an IPv6 route.
705*0Sstevel@tonic-gate  * ipif_arg is passed in to associate it with the correct interface
706*0Sstevel@tonic-gate  * (for link-local destinations and gateways).
707*0Sstevel@tonic-gate  */
708*0Sstevel@tonic-gate /* ARGSUSED4 */
709*0Sstevel@tonic-gate int
710*0Sstevel@tonic-gate ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
711*0Sstevel@tonic-gate     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ipif_t *ipif_arg,
712*0Sstevel@tonic-gate     queue_t *q, mblk_t *mp, ipsq_func_t func)
713*0Sstevel@tonic-gate {
714*0Sstevel@tonic-gate 	ire_t	*ire = NULL;
715*0Sstevel@tonic-gate 	ipif_t	*ipif;
716*0Sstevel@tonic-gate 	uint_t	type;
717*0Sstevel@tonic-gate 	uint_t	match_flags = MATCH_IRE_TYPE;
718*0Sstevel@tonic-gate 	int	err = 0;
719*0Sstevel@tonic-gate 	boolean_t	ipif_refheld = B_FALSE;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	/*
722*0Sstevel@tonic-gate 	 * If this is the case of RTF_HOST being set, then we set the netmask
723*0Sstevel@tonic-gate 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
724*0Sstevel@tonic-gate 	 */
725*0Sstevel@tonic-gate 	if (flags & RTF_HOST) {
726*0Sstevel@tonic-gate 		mask = &ipv6_all_ones;
727*0Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
728*0Sstevel@tonic-gate 	} else if (rtm_addrs & RTA_NETMASK) {
729*0Sstevel@tonic-gate 		match_flags |= MATCH_IRE_MASK;
730*0Sstevel@tonic-gate 	}
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 	/*
733*0Sstevel@tonic-gate 	 * Note that RTF_GATEWAY is never set on a delete, therefore
734*0Sstevel@tonic-gate 	 * we check if the gateway address is one of our interfaces first,
735*0Sstevel@tonic-gate 	 * and fall back on RTF_GATEWAY routes.
736*0Sstevel@tonic-gate 	 *
737*0Sstevel@tonic-gate 	 * This makes it possible to delete an original
738*0Sstevel@tonic-gate 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
739*0Sstevel@tonic-gate 	 *
740*0Sstevel@tonic-gate 	 * As the interface index specified with the RTA_IFP sockaddr is the
741*0Sstevel@tonic-gate 	 * same for all ipif's off of an ill, the matching logic below uses
742*0Sstevel@tonic-gate 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
743*0Sstevel@tonic-gate 	 * sharing the same prefix and interface index as the the route
744*0Sstevel@tonic-gate 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
745*0Sstevel@tonic-gate 	 * is specified in the request.
746*0Sstevel@tonic-gate 	 *
747*0Sstevel@tonic-gate 	 * On the other hand, since the gateway address will usually be
748*0Sstevel@tonic-gate 	 * different for each ipif on the system, the matching logic
749*0Sstevel@tonic-gate 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
750*0Sstevel@tonic-gate 	 * route.  This means that interface routes for the same prefix can be
751*0Sstevel@tonic-gate 	 * uniquely identified if they belong to distinct ipif's and if a
752*0Sstevel@tonic-gate 	 * RTA_IFP sockaddr is not present.
753*0Sstevel@tonic-gate 	 *
754*0Sstevel@tonic-gate 	 * For more detail on specifying routes by gateway address and by
755*0Sstevel@tonic-gate 	 * interface index, see the comments in ip_rt_add_v6().
756*0Sstevel@tonic-gate 	 */
757*0Sstevel@tonic-gate 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, q, mp, func, &err);
758*0Sstevel@tonic-gate 	if (ipif != NULL) {
759*0Sstevel@tonic-gate 		ipif_refheld = B_TRUE;
760*0Sstevel@tonic-gate 		if (ipif_arg != NULL) {
761*0Sstevel@tonic-gate 			ipif_refrele(ipif);
762*0Sstevel@tonic-gate 			ipif_refheld = B_FALSE;
763*0Sstevel@tonic-gate 			ipif = ipif_arg;
764*0Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
765*0Sstevel@tonic-gate 		} else {
766*0Sstevel@tonic-gate 			match_flags |= MATCH_IRE_IPIF;
767*0Sstevel@tonic-gate 		}
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 		if (ipif->ipif_ire_type == IRE_LOOPBACK)
770*0Sstevel@tonic-gate 			ire = ire_ctable_lookup_v6(dst_addr, 0, IRE_LOOPBACK,
771*0Sstevel@tonic-gate 			    ipif, ALL_ZONES, match_flags);
772*0Sstevel@tonic-gate 		if (ire == NULL)
773*0Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, 0,
774*0Sstevel@tonic-gate 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
775*0Sstevel@tonic-gate 			    match_flags);
776*0Sstevel@tonic-gate 	} else if (err == EINPROGRESS) {
777*0Sstevel@tonic-gate 		return (err);
778*0Sstevel@tonic-gate 	} else {
779*0Sstevel@tonic-gate 		err = 0;
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate 	if (ire == NULL) {
782*0Sstevel@tonic-gate 		/*
783*0Sstevel@tonic-gate 		 * At this point, the gateway address is not one of our own
784*0Sstevel@tonic-gate 		 * addresses or a matching interface route was not found.  We
785*0Sstevel@tonic-gate 		 * set the IRE type to lookup based on whether
786*0Sstevel@tonic-gate 		 * this is a host route, a default route or just a prefix.
787*0Sstevel@tonic-gate 		 *
788*0Sstevel@tonic-gate 		 * If an ipif_arg was passed in, then the lookup is based on an
789*0Sstevel@tonic-gate 		 * interface index so MATCH_IRE_ILL is added to match_flags.
790*0Sstevel@tonic-gate 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
791*0Sstevel@tonic-gate 		 * set as the route being looked up is not a traditional
792*0Sstevel@tonic-gate 		 * interface route.
793*0Sstevel@tonic-gate 		 */
794*0Sstevel@tonic-gate 		match_flags &= ~MATCH_IRE_IPIF;
795*0Sstevel@tonic-gate 		match_flags |= MATCH_IRE_GW;
796*0Sstevel@tonic-gate 		if (ipif_arg != NULL)
797*0Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL;
798*0Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
799*0Sstevel@tonic-gate 			type = IRE_HOST;
800*0Sstevel@tonic-gate 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
801*0Sstevel@tonic-gate 			type = IRE_DEFAULT;
802*0Sstevel@tonic-gate 		else
803*0Sstevel@tonic-gate 			type = IRE_PREFIX;
804*0Sstevel@tonic-gate 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
805*0Sstevel@tonic-gate 		    ipif_arg, NULL, ALL_ZONES, 0, match_flags);
806*0Sstevel@tonic-gate 		if (ire == NULL && type == IRE_HOST) {
807*0Sstevel@tonic-gate 			ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr,
808*0Sstevel@tonic-gate 			    IRE_HOST_REDIRECT, ipif_arg, NULL, ALL_ZONES, 0,
809*0Sstevel@tonic-gate 			    match_flags);
810*0Sstevel@tonic-gate 		}
811*0Sstevel@tonic-gate 	}
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	if (ipif_refheld) {
814*0Sstevel@tonic-gate 		ipif_refrele(ipif);
815*0Sstevel@tonic-gate 		ipif_refheld = B_FALSE;
816*0Sstevel@tonic-gate 	}
817*0Sstevel@tonic-gate 	if (ire == NULL)
818*0Sstevel@tonic-gate 		return (ESRCH);
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	if (ire->ire_flags & RTF_MULTIRT) {
821*0Sstevel@tonic-gate 		/*
822*0Sstevel@tonic-gate 		 * Invoke the CGTP (multirouting) filtering module
823*0Sstevel@tonic-gate 		 * to remove the dst address from the filtering database.
824*0Sstevel@tonic-gate 		 * Packets coming from that address will no longer be
825*0Sstevel@tonic-gate 		 * filtered to remove duplicates.
826*0Sstevel@tonic-gate 		 */
827*0Sstevel@tonic-gate 		if (ip_cgtp_filter_ops != NULL) {
828*0Sstevel@tonic-gate 			err = ip_cgtp_filter_ops->cfo_del_dest_v6(
829*0Sstevel@tonic-gate 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
830*0Sstevel@tonic-gate 		}
831*0Sstevel@tonic-gate 	}
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 	ipif = ire->ire_ipif;
834*0Sstevel@tonic-gate 	if (ipif != NULL) {
835*0Sstevel@tonic-gate 		mblk_t		**mpp;
836*0Sstevel@tonic-gate 		mblk_t		*mp;
837*0Sstevel@tonic-gate 		ifrt_t		*ifrt;
838*0Sstevel@tonic-gate 		in6_addr_t	gw_addr_v6;
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 		/* Remove from ipif_saved_ire_mp list if it is there */
841*0Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
842*0Sstevel@tonic-gate 		gw_addr_v6 = ire->ire_gateway_addr_v6;
843*0Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
844*0Sstevel@tonic-gate 		mutex_enter(&ipif->ipif_saved_ire_lock);
845*0Sstevel@tonic-gate 		for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
846*0Sstevel@tonic-gate 		    mpp = &(*mpp)->b_cont) {
847*0Sstevel@tonic-gate 			/*
848*0Sstevel@tonic-gate 			 * On a given ipif, the triple of address, gateway and
849*0Sstevel@tonic-gate 			 * mask is unique for each saved IRE (in the case of
850*0Sstevel@tonic-gate 			 * ordinary interface routes, the gateway address is
851*0Sstevel@tonic-gate 			 * all-zeroes).
852*0Sstevel@tonic-gate 			 */
853*0Sstevel@tonic-gate 			mp = *mpp;
854*0Sstevel@tonic-gate 			ifrt = (ifrt_t *)mp->b_rptr;
855*0Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
856*0Sstevel@tonic-gate 			    &ire->ire_addr_v6) &&
857*0Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
858*0Sstevel@tonic-gate 			    &gw_addr_v6) &&
859*0Sstevel@tonic-gate 			    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
860*0Sstevel@tonic-gate 			    &ire->ire_mask_v6)) {
861*0Sstevel@tonic-gate 				*mpp = mp->b_cont;
862*0Sstevel@tonic-gate 				ipif->ipif_saved_ire_cnt--;
863*0Sstevel@tonic-gate 				freeb(mp);
864*0Sstevel@tonic-gate 				break;
865*0Sstevel@tonic-gate 			}
866*0Sstevel@tonic-gate 		}
867*0Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
868*0Sstevel@tonic-gate 	}
869*0Sstevel@tonic-gate 	ire_delete(ire);
870*0Sstevel@tonic-gate 	ire_refrele(ire);
871*0Sstevel@tonic-gate 	return (err);
872*0Sstevel@tonic-gate }
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate /*
875*0Sstevel@tonic-gate  * Derive a token from the link layer address.
876*0Sstevel@tonic-gate  */
877*0Sstevel@tonic-gate boolean_t
878*0Sstevel@tonic-gate ill_setdefaulttoken(ill_t *ill)
879*0Sstevel@tonic-gate {
880*0Sstevel@tonic-gate 	int 		i;
881*0Sstevel@tonic-gate 	in6_addr_t	v6addr, v6mask;
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	/*
884*0Sstevel@tonic-gate 	 * Though we execute on the ipsq, we need to hold the ill_lock
885*0Sstevel@tonic-gate 	 * to prevent readers from seeing partially updated values
886*0Sstevel@tonic-gate 	 * while we do the update.
887*0Sstevel@tonic-gate 	 */
888*0Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
889*0Sstevel@tonic-gate 	if (!MEDIA_V6INTFID(ill->ill_media, ill->ill_phys_addr_length,
890*0Sstevel@tonic-gate 	    ill->ill_phys_addr, &v6addr)) {
891*0Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
892*0Sstevel@tonic-gate 		return (B_FALSE);
893*0Sstevel@tonic-gate 	}
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_TOKEN_LEN, &v6mask);
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
898*0Sstevel@tonic-gate 		v6mask.s6_addr32[i] = v6mask.s6_addr32[i] ^
899*0Sstevel@tonic-gate 		    (uint32_t)0xffffffff;
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
902*0Sstevel@tonic-gate 	ill->ill_token_length = IPV6_TOKEN_LEN;
903*0Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
904*0Sstevel@tonic-gate 	return (B_TRUE);
905*0Sstevel@tonic-gate }
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate /*
908*0Sstevel@tonic-gate  * Create a link-local address from a token.
909*0Sstevel@tonic-gate  */
910*0Sstevel@tonic-gate static void
911*0Sstevel@tonic-gate ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
912*0Sstevel@tonic-gate {
913*0Sstevel@tonic-gate 	int i;
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
916*0Sstevel@tonic-gate 		dest->s6_addr32[i] =
917*0Sstevel@tonic-gate 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
918*0Sstevel@tonic-gate 	}
919*0Sstevel@tonic-gate }
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate /*
922*0Sstevel@tonic-gate  * Set a nice default address for either automatic tunnels tsrc/96 or
923*0Sstevel@tonic-gate  * 6to4 tunnels 2002:<tsrc>::1/64
924*0Sstevel@tonic-gate  */
925*0Sstevel@tonic-gate static void
926*0Sstevel@tonic-gate ipif_set_tun_auto_addr(ipif_t *ipif, struct iftun_req *ta)
927*0Sstevel@tonic-gate {
928*0Sstevel@tonic-gate 	sin6_t	sin6;
929*0Sstevel@tonic-gate 	sin_t	*sin;
930*0Sstevel@tonic-gate 	ill_t 	*ill = ipif->ipif_ill;
931*0Sstevel@tonic-gate 	tun_t *tp = (tun_t *)ill->ill_wq->q_next->q_ptr;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 	if (ta->ifta_saddr.ss_family != AF_INET ||
934*0Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_UP) || !ipif->ipif_isv6 ||
935*0Sstevel@tonic-gate 	    (ta->ifta_flags & IFTUN_SRC) == 0)
936*0Sstevel@tonic-gate 		return;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 	/*
939*0Sstevel@tonic-gate 	 * Check the tunnel type by examining q_next->q_ptr
940*0Sstevel@tonic-gate 	 */
941*0Sstevel@tonic-gate 	if (tp->tun_flags & TUN_AUTOMATIC) {
942*0Sstevel@tonic-gate 		/* this is an automatic tunnel */
943*0Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_ABITS - IP_ABITS,
944*0Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
945*0Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
946*0Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
947*0Sstevel@tonic-gate 		V4_PART_OF_V6(sin6.sin6_addr) = sin->sin_addr.s_addr;
948*0Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
949*0Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
950*0Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
951*0Sstevel@tonic-gate 	} else if (tp->tun_flags & TUN_6TO4) {
952*0Sstevel@tonic-gate 		/* this is a 6to4 tunnel */
953*0Sstevel@tonic-gate 		(void) ip_plen_to_mask_v6(IPV6_PREFIX_LEN,
954*0Sstevel@tonic-gate 		    &ipif->ipif_v6net_mask);
955*0Sstevel@tonic-gate 		sin = (sin_t *)&ta->ifta_saddr;
956*0Sstevel@tonic-gate 		/* create a 6to4 address from the IPv4 tsrc */
957*0Sstevel@tonic-gate 		IN6_V4ADDR_TO_6TO4(&sin->sin_addr, &sin6.sin6_addr);
958*0Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
959*0Sstevel@tonic-gate 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
960*0Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
961*0Sstevel@tonic-gate 	} else {
962*0Sstevel@tonic-gate 		ip1dbg(("ipif_set_tun_auto_addr: Unknown tunnel type"));
963*0Sstevel@tonic-gate 		return;
964*0Sstevel@tonic-gate 	}
965*0Sstevel@tonic-gate }
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate /*
968*0Sstevel@tonic-gate  * Set link local for ipif_id 0 of a configured tunnel based on the
969*0Sstevel@tonic-gate  * tsrc or tdst parameter
970*0Sstevel@tonic-gate  * For tunnels over IPv4 use the IPv4 address prepended with 32 zeros as
971*0Sstevel@tonic-gate  * the token.
972*0Sstevel@tonic-gate  * For tunnels over IPv6 use the low-order 64 bits of the "inner" IPv6 address
973*0Sstevel@tonic-gate  * as the token for the "outer" link.
974*0Sstevel@tonic-gate  */
975*0Sstevel@tonic-gate void
976*0Sstevel@tonic-gate ipif_set_tun_llink(ill_t *ill, struct iftun_req *ta)
977*0Sstevel@tonic-gate {
978*0Sstevel@tonic-gate 	ipif_t		*ipif;
979*0Sstevel@tonic-gate 	sin_t		*sin;
980*0Sstevel@tonic-gate 	in6_addr_t	*s6addr;
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 	/* The first ipif must be id zero. */
985*0Sstevel@tonic-gate 	ipif = ill->ill_ipif;
986*0Sstevel@tonic-gate 	ASSERT(ipif->ipif_id == 0);
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 	/* no link local for automatic tunnels */
989*0Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_POINTOPOINT)) {
990*0Sstevel@tonic-gate 		ipif_set_tun_auto_addr(ipif, ta);
991*0Sstevel@tonic-gate 		return;
992*0Sstevel@tonic-gate 	}
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_DST) &&
995*0Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
996*0Sstevel@tonic-gate 		sin6_t	sin6;
997*0Sstevel@tonic-gate 
998*0Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
999*0Sstevel@tonic-gate 		bzero(&sin6, sizeof (sin6_t));
1000*0Sstevel@tonic-gate 		if ((ta->ifta_saddr.ss_family == AF_INET)) {
1001*0Sstevel@tonic-gate 			sin = (sin_t *)&ta->ifta_daddr;
1002*0Sstevel@tonic-gate 			V4_PART_OF_V6(sin6.sin6_addr) =
1003*0Sstevel@tonic-gate 			    sin->sin_addr.s_addr;
1004*0Sstevel@tonic-gate 		} else {
1005*0Sstevel@tonic-gate 			s6addr =
1006*0Sstevel@tonic-gate 			    &((sin6_t *)&ta->ifta_daddr)->sin6_addr;
1007*0Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[3] = s6addr->s6_addr32[3];
1008*0Sstevel@tonic-gate 			sin6.sin6_addr.s6_addr32[2] = s6addr->s6_addr32[2];
1009*0Sstevel@tonic-gate 		}
1010*0Sstevel@tonic-gate 		ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr,
1011*0Sstevel@tonic-gate 		    &sin6.sin6_addr);
1012*0Sstevel@tonic-gate 		ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
1013*0Sstevel@tonic-gate 	}
1014*0Sstevel@tonic-gate 	if ((ta->ifta_flags & IFTUN_SRC)) {
1015*0Sstevel@tonic-gate 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 		/* Set the token if it isn't already set */
1018*0Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) {
1019*0Sstevel@tonic-gate 			if ((ta->ifta_saddr.ss_family == AF_INET)) {
1020*0Sstevel@tonic-gate 				sin = (sin_t *)&ta->ifta_saddr;
1021*0Sstevel@tonic-gate 				V4_PART_OF_V6(ill->ill_token) =
1022*0Sstevel@tonic-gate 				    sin->sin_addr.s_addr;
1023*0Sstevel@tonic-gate 			} else {
1024*0Sstevel@tonic-gate 				s6addr =
1025*0Sstevel@tonic-gate 				    &((sin6_t *)&ta->ifta_saddr)->sin6_addr;
1026*0Sstevel@tonic-gate 				ill->ill_token.s6_addr32[3] =
1027*0Sstevel@tonic-gate 				    s6addr->s6_addr32[3];
1028*0Sstevel@tonic-gate 				ill->ill_token.s6_addr32[2] =
1029*0Sstevel@tonic-gate 				    s6addr->s6_addr32[2];
1030*0Sstevel@tonic-gate 			}
1031*0Sstevel@tonic-gate 			ill->ill_token_length = IPV6_TOKEN_LEN;
1032*0Sstevel@tonic-gate 		}
1033*0Sstevel@tonic-gate 		/*
1034*0Sstevel@tonic-gate 		 * Attempt to set the link local address if it isn't set.
1035*0Sstevel@tonic-gate 		 */
1036*0Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
1037*0Sstevel@tonic-gate 			(void) ipif_setlinklocal(ipif);
1038*0Sstevel@tonic-gate 	}
1039*0Sstevel@tonic-gate }
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate /*
1042*0Sstevel@tonic-gate  * Is it not possible to set the link local address?
1043*0Sstevel@tonic-gate  * The address can be set if the token is set, and the token
1044*0Sstevel@tonic-gate  * isn't too long.
1045*0Sstevel@tonic-gate  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
1046*0Sstevel@tonic-gate  */
1047*0Sstevel@tonic-gate boolean_t
1048*0Sstevel@tonic-gate ipif_cant_setlinklocal(ipif_t *ipif)
1049*0Sstevel@tonic-gate {
1050*0Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
1053*0Sstevel@tonic-gate 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
1054*0Sstevel@tonic-gate 		return (B_TRUE);
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	return (B_FALSE);
1057*0Sstevel@tonic-gate }
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate /*
1060*0Sstevel@tonic-gate  * Generate a link-local address from the token.
1061*0Sstevel@tonic-gate  * Return zero if the address was set, or non-zero if it couldn't be set.
1062*0Sstevel@tonic-gate  */
1063*0Sstevel@tonic-gate int
1064*0Sstevel@tonic-gate ipif_setlinklocal(ipif_t *ipif)
1065*0Sstevel@tonic-gate {
1066*0Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate 	if (ipif_cant_setlinklocal(ipif))
1071*0Sstevel@tonic-gate 		return (-1);
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
1074*0Sstevel@tonic-gate 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
1075*0Sstevel@tonic-gate 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
1076*0Sstevel@tonic-gate 	    ipif->ipif_v6subnet);
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate 	if (ipif->ipif_flags & IPIF_NOLOCAL) {
1079*0Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipv6_all_zeros;
1080*0Sstevel@tonic-gate 	} else {
1081*0Sstevel@tonic-gate 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
1082*0Sstevel@tonic-gate 	}
1083*0Sstevel@tonic-gate 	return (0);
1084*0Sstevel@tonic-gate }
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate /*
1087*0Sstevel@tonic-gate  * This function sets up the multicast mappings in NDP.
1088*0Sstevel@tonic-gate  * Unlike ARP, there are no mapping_mps here. We delete the
1089*0Sstevel@tonic-gate  * mapping nces and add a new one.
1090*0Sstevel@tonic-gate  *
1091*0Sstevel@tonic-gate  * Returns non-zero on error and 0 on success.
1092*0Sstevel@tonic-gate  */
1093*0Sstevel@tonic-gate int
1094*0Sstevel@tonic-gate ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce)
1095*0Sstevel@tonic-gate {
1096*0Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
1097*0Sstevel@tonic-gate 	in6_addr_t	v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0};
1098*0Sstevel@tonic-gate 	in6_addr_t	v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0};
1099*0Sstevel@tonic-gate 	in6_addr_t	v6_extract_mask;
1100*0Sstevel@tonic-gate 	uchar_t		*phys_addr, *bphys_addr, *alloc_phys;
1101*0Sstevel@tonic-gate 	nce_t		*mnce = NULL;
1102*0Sstevel@tonic-gate 	int		err = 0;
1103*0Sstevel@tonic-gate 	phyint_t	*phyi = ill->ill_phyint;
1104*0Sstevel@tonic-gate 	uint32_t	hw_extract_start;
1105*0Sstevel@tonic-gate 	dl_unitdata_req_t *dlur;
1106*0Sstevel@tonic-gate 
1107*0Sstevel@tonic-gate 	if (ret_nce != NULL)
1108*0Sstevel@tonic-gate 		*ret_nce = NULL;
1109*0Sstevel@tonic-gate 	/*
1110*0Sstevel@tonic-gate 	 * Delete the mapping nce. Normally these should not exist
1111*0Sstevel@tonic-gate 	 * as a previous ipif_down -> ipif_ndp_down should have deleted
1112*0Sstevel@tonic-gate 	 * all the nces. But they can exist if ip_rput_dlpi_writer
1113*0Sstevel@tonic-gate 	 * calls this when PHYI_MULTI_BCAST is set.
1114*0Sstevel@tonic-gate 	 */
1115*0Sstevel@tonic-gate 	mnce = ndp_lookup(ill, &v6_mcast_addr, B_FALSE);
1116*0Sstevel@tonic-gate 	if (mnce != NULL) {
1117*0Sstevel@tonic-gate 		ndp_delete(mnce);
1118*0Sstevel@tonic-gate 		NCE_REFRELE(mnce);
1119*0Sstevel@tonic-gate 		mnce = NULL;
1120*0Sstevel@tonic-gate 	}
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 	/*
1123*0Sstevel@tonic-gate 	 * Get media specific v6 mapping information. Note that
1124*0Sstevel@tonic-gate 	 * nd_lla_len can be 0 for tunnels.
1125*0Sstevel@tonic-gate 	 */
1126*0Sstevel@tonic-gate 	alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
1127*0Sstevel@tonic-gate 	if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0))
1128*0Sstevel@tonic-gate 		return (ENOMEM);
1129*0Sstevel@tonic-gate 	/*
1130*0Sstevel@tonic-gate 	 * Determine the broadcast address.
1131*0Sstevel@tonic-gate 	 */
1132*0Sstevel@tonic-gate 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
1133*0Sstevel@tonic-gate 	if (ill->ill_sap_length < 0)
1134*0Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
1135*0Sstevel@tonic-gate 	else
1136*0Sstevel@tonic-gate 		bphys_addr = (uchar_t *)dlur +
1137*0Sstevel@tonic-gate 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate 	/*
1140*0Sstevel@tonic-gate 	 * Check PHYI_MULTI_BCAST and possible length of physical
1141*0Sstevel@tonic-gate 	 * address to determine if we use the mapping or the
1142*0Sstevel@tonic-gate 	 * broadcast address.
1143*0Sstevel@tonic-gate 	 */
1144*0Sstevel@tonic-gate 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
1145*0Sstevel@tonic-gate 	    (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len,
1146*0Sstevel@tonic-gate 	    bphys_addr, alloc_phys, &hw_extract_start,
1147*0Sstevel@tonic-gate 	    &v6_extract_mask))) {
1148*0Sstevel@tonic-gate 		if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) {
1149*0Sstevel@tonic-gate 			kmem_free(alloc_phys, ill->ill_nd_lla_len);
1150*0Sstevel@tonic-gate 			return (E2BIG);
1151*0Sstevel@tonic-gate 		}
1152*0Sstevel@tonic-gate 		/* Use the link-layer broadcast address for MULTI_BCAST */
1153*0Sstevel@tonic-gate 		phys_addr = bphys_addr;
1154*0Sstevel@tonic-gate 		bzero(&v6_extract_mask, sizeof (v6_extract_mask));
1155*0Sstevel@tonic-gate 		hw_extract_start = ill->ill_nd_lla_len;
1156*0Sstevel@tonic-gate 	} else {
1157*0Sstevel@tonic-gate 		phys_addr = alloc_phys;
1158*0Sstevel@tonic-gate 	}
1159*0Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_BROADCAST) ||
1160*0Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST) ||
1161*0Sstevel@tonic-gate 	    (phyi->phyint_flags & PHYI_MULTI_BCAST)) {
1162*0Sstevel@tonic-gate 		mutex_enter(&ndp_g_lock);
1163*0Sstevel@tonic-gate 		err = ndp_add(ill,
1164*0Sstevel@tonic-gate 		    phys_addr,
1165*0Sstevel@tonic-gate 		    &v6_mcast_addr,	/* v6 address */
1166*0Sstevel@tonic-gate 		    &v6_mcast_mask,	/* v6 mask */
1167*0Sstevel@tonic-gate 		    &v6_extract_mask,
1168*0Sstevel@tonic-gate 		    hw_extract_start,
1169*0Sstevel@tonic-gate 		    NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD,
1170*0Sstevel@tonic-gate 		    ND_REACHABLE,
1171*0Sstevel@tonic-gate 		    &mnce);
1172*0Sstevel@tonic-gate 		mutex_exit(&ndp_g_lock);
1173*0Sstevel@tonic-gate 		if (err == 0) {
1174*0Sstevel@tonic-gate 			if (ret_nce != NULL) {
1175*0Sstevel@tonic-gate 				*ret_nce = mnce;
1176*0Sstevel@tonic-gate 			} else {
1177*0Sstevel@tonic-gate 				NCE_REFRELE(mnce);
1178*0Sstevel@tonic-gate 			}
1179*0Sstevel@tonic-gate 		}
1180*0Sstevel@tonic-gate 	}
1181*0Sstevel@tonic-gate 	kmem_free(alloc_phys, ill->ill_nd_lla_len);
1182*0Sstevel@tonic-gate 	return (err);
1183*0Sstevel@tonic-gate }
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate /*
1186*0Sstevel@tonic-gate  * Get the resolver set up for a new interface address.  (Always called
1187*0Sstevel@tonic-gate  * as writer.)
1188*0Sstevel@tonic-gate  */
1189*0Sstevel@tonic-gate int
1190*0Sstevel@tonic-gate ipif_ndp_up(ipif_t *ipif, const in6_addr_t *addr, boolean_t macaddr_change)
1191*0Sstevel@tonic-gate {
1192*0Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
1193*0Sstevel@tonic-gate 	int		err = 0;
1194*0Sstevel@tonic-gate 	nce_t		*nce = NULL;
1195*0Sstevel@tonic-gate 	nce_t		*mnce = NULL;
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate 	ip1dbg(("ipif_ndp_up(%s:%u)\n",
1198*0Sstevel@tonic-gate 		ipif->ipif_ill->ill_name, ipif->ipif_id));
1199*0Sstevel@tonic-gate 
1200*0Sstevel@tonic-gate 	/*
1201*0Sstevel@tonic-gate 	 * ND not supported on XRESOLV interfaces. If ND support (multicast)
1202*0Sstevel@tonic-gate 	 * added later, take out this check.
1203*0Sstevel@tonic-gate 	 */
1204*0Sstevel@tonic-gate 	if (ill->ill_flags & ILLF_XRESOLV)
1205*0Sstevel@tonic-gate 		return (0);
1206*0Sstevel@tonic-gate 
1207*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr) ||
1208*0Sstevel@tonic-gate 	    (!(ill->ill_net_type & IRE_INTERFACE)))
1209*0Sstevel@tonic-gate 		return (0);
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 	/*
1212*0Sstevel@tonic-gate 	 * Need to setup multicast mapping only when the first
1213*0Sstevel@tonic-gate 	 * interface is coming UP.
1214*0Sstevel@tonic-gate 	 */
1215*0Sstevel@tonic-gate 	if (ill->ill_ipif_up_count == 0 &&
1216*0Sstevel@tonic-gate 	    (ill->ill_flags & ILLF_MULTICAST)) {
1217*0Sstevel@tonic-gate 		/*
1218*0Sstevel@tonic-gate 		 * We set the multicast before setting up the mapping for
1219*0Sstevel@tonic-gate 		 * local address because ipif_ndp_setup_multicast does
1220*0Sstevel@tonic-gate 		 * ndp_walk to delete nces which will delete the mapping
1221*0Sstevel@tonic-gate 		 * for local address also if we added the mapping for
1222*0Sstevel@tonic-gate 		 * local address first.
1223*0Sstevel@tonic-gate 		 */
1224*0Sstevel@tonic-gate 		err = ipif_ndp_setup_multicast(ipif, &mnce);
1225*0Sstevel@tonic-gate 		if (err != 0)
1226*0Sstevel@tonic-gate 			return (err);
1227*0Sstevel@tonic-gate 	}
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
1230*0Sstevel@tonic-gate 		uint16_t	flags;
1231*0Sstevel@tonic-gate 		uchar_t	*hw_addr = NULL;
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate 		/* Permanent entries don't need NUD */
1234*0Sstevel@tonic-gate 		flags = NCE_F_PERMANENT;
1235*0Sstevel@tonic-gate 		flags |= NCE_F_NONUD;
1236*0Sstevel@tonic-gate 		if (ill->ill_flags & ILLF_ROUTER)
1237*0Sstevel@tonic-gate 			flags |= NCE_F_ISROUTER;
1238*0Sstevel@tonic-gate 
1239*0Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_ANYCAST)
1240*0Sstevel@tonic-gate 			flags |= NCE_F_ANYCAST;
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 		if (ill->ill_net_type == IRE_IF_RESOLVER) {
1243*0Sstevel@tonic-gate 			hw_addr = ill->ill_nd_lla;
1244*0Sstevel@tonic-gate 
1245*0Sstevel@tonic-gate 			if (ill->ill_move_in_progress || macaddr_change) {
1246*0Sstevel@tonic-gate 				/*
1247*0Sstevel@tonic-gate 				 * Addresses are failing over to this ill.
1248*0Sstevel@tonic-gate 				 * Don't wait for NUD to see this change.
1249*0Sstevel@tonic-gate 				 * Publish our new link-layer address.
1250*0Sstevel@tonic-gate 				 */
1251*0Sstevel@tonic-gate 				flags |= NCE_F_UNSOL_ADV;
1252*0Sstevel@tonic-gate 			}
1253*0Sstevel@tonic-gate 		}
1254*0Sstevel@tonic-gate 		err = ndp_lookup_then_add(ill,
1255*0Sstevel@tonic-gate 		    hw_addr,
1256*0Sstevel@tonic-gate 		    addr,
1257*0Sstevel@tonic-gate 		    &ipv6_all_ones,
1258*0Sstevel@tonic-gate 		    &ipv6_all_zeros,
1259*0Sstevel@tonic-gate 		    0,
1260*0Sstevel@tonic-gate 		    flags,
1261*0Sstevel@tonic-gate 		    ND_REACHABLE,
1262*0Sstevel@tonic-gate 		    &nce);
1263*0Sstevel@tonic-gate 		switch (err) {
1264*0Sstevel@tonic-gate 		case 0:
1265*0Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
1266*0Sstevel@tonic-gate 			    ill->ill_name));
1267*0Sstevel@tonic-gate 			break;
1268*0Sstevel@tonic-gate 		case EEXIST:
1269*0Sstevel@tonic-gate 			NCE_REFRELE(nce);
1270*0Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
1271*0Sstevel@tonic-gate 			    ill->ill_name));
1272*0Sstevel@tonic-gate 			if (mnce != NULL) {
1273*0Sstevel@tonic-gate 				ndp_delete(mnce);
1274*0Sstevel@tonic-gate 				NCE_REFRELE(mnce);
1275*0Sstevel@tonic-gate 			}
1276*0Sstevel@tonic-gate 			return (err);
1277*0Sstevel@tonic-gate 		default:
1278*0Sstevel@tonic-gate 			ip1dbg(("ipif_ndp_up: NCE creation failed %s\n",
1279*0Sstevel@tonic-gate 			    ill->ill_name));
1280*0Sstevel@tonic-gate 			if (mnce != NULL) {
1281*0Sstevel@tonic-gate 				ndp_delete(mnce);
1282*0Sstevel@tonic-gate 				NCE_REFRELE(mnce);
1283*0Sstevel@tonic-gate 			}
1284*0Sstevel@tonic-gate 			return (err);
1285*0Sstevel@tonic-gate 		}
1286*0Sstevel@tonic-gate 	}
1287*0Sstevel@tonic-gate 	if (nce != NULL)
1288*0Sstevel@tonic-gate 		NCE_REFRELE(nce);
1289*0Sstevel@tonic-gate 	if (mnce != NULL)
1290*0Sstevel@tonic-gate 		NCE_REFRELE(mnce);
1291*0Sstevel@tonic-gate 	return (0);
1292*0Sstevel@tonic-gate }
1293*0Sstevel@tonic-gate 
1294*0Sstevel@tonic-gate /* Remove all cache entries for this logical interface */
1295*0Sstevel@tonic-gate void
1296*0Sstevel@tonic-gate ipif_ndp_down(ipif_t *ipif)
1297*0Sstevel@tonic-gate {
1298*0Sstevel@tonic-gate 	nce_t	*nce;
1299*0Sstevel@tonic-gate 
1300*0Sstevel@tonic-gate 	nce = ndp_lookup(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
1301*0Sstevel@tonic-gate 	if (nce != NULL) {
1302*0Sstevel@tonic-gate 		ndp_delete(nce);
1303*0Sstevel@tonic-gate 		NCE_REFRELE(nce);
1304*0Sstevel@tonic-gate 	}
1305*0Sstevel@tonic-gate 	/*
1306*0Sstevel@tonic-gate 	 * Remove mapping and all other nces dependent on this ill
1307*0Sstevel@tonic-gate 	 * when the last ipif is going away.
1308*0Sstevel@tonic-gate 	 */
1309*0Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
1310*0Sstevel@tonic-gate 		ndp_walk(ipif->ipif_ill, (pfi_t)ndp_delete_per_ill,
1311*0Sstevel@tonic-gate 		    (uchar_t *)ipif->ipif_ill);
1312*0Sstevel@tonic-gate 	}
1313*0Sstevel@tonic-gate }
1314*0Sstevel@tonic-gate 
1315*0Sstevel@tonic-gate /*
1316*0Sstevel@tonic-gate  * Used when an interface comes up to recreate any extra routes on this
1317*0Sstevel@tonic-gate  * interface.
1318*0Sstevel@tonic-gate  */
1319*0Sstevel@tonic-gate static ire_t **
1320*0Sstevel@tonic-gate ipif_recover_ire_v6(ipif_t *ipif)
1321*0Sstevel@tonic-gate {
1322*0Sstevel@tonic-gate 	mblk_t	*mp;
1323*0Sstevel@tonic-gate 	ire_t   **ipif_saved_irep;
1324*0Sstevel@tonic-gate 	ire_t   **irep;
1325*0Sstevel@tonic-gate 
1326*0Sstevel@tonic-gate 	ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name,
1327*0Sstevel@tonic-gate 	    ipif->ipif_id));
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
1330*0Sstevel@tonic-gate 
1331*0Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_saved_ire_lock);
1332*0Sstevel@tonic-gate 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
1333*0Sstevel@tonic-gate 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
1334*0Sstevel@tonic-gate 	if (ipif_saved_irep == NULL) {
1335*0Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_saved_ire_lock);
1336*0Sstevel@tonic-gate 		return (NULL);
1337*0Sstevel@tonic-gate 	}
1338*0Sstevel@tonic-gate 
1339*0Sstevel@tonic-gate 	irep = ipif_saved_irep;
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
1342*0Sstevel@tonic-gate 		ire_t		*ire;
1343*0Sstevel@tonic-gate 		queue_t		*rfq;
1344*0Sstevel@tonic-gate 		queue_t		*stq;
1345*0Sstevel@tonic-gate 		ifrt_t		*ifrt;
1346*0Sstevel@tonic-gate 		in6_addr_t	*src_addr;
1347*0Sstevel@tonic-gate 		in6_addr_t	*gateway_addr;
1348*0Sstevel@tonic-gate 		mblk_t		*resolver_mp;
1349*0Sstevel@tonic-gate 		char		buf[INET6_ADDRSTRLEN];
1350*0Sstevel@tonic-gate 		ushort_t	type;
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate 		/*
1353*0Sstevel@tonic-gate 		 * When the ire was initially created and then added in
1354*0Sstevel@tonic-gate 		 * ip_rt_add_v6(), it was created either using
1355*0Sstevel@tonic-gate 		 * ipif->ipif_net_type in the case of a traditional interface
1356*0Sstevel@tonic-gate 		 * route, or as one of the IRE_OFFSUBNET types (with the
1357*0Sstevel@tonic-gate 		 * exception of IRE_HOST_REDIRECT which is created by
1358*0Sstevel@tonic-gate 		 * icmp_redirect_v6() and which we don't need to save or
1359*0Sstevel@tonic-gate 		 * recover).  In the case where ipif->ipif_net_type was
1360*0Sstevel@tonic-gate 		 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to
1361*0Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy
1362*0Sstevel@tonic-gate 		 * software like GateD and Sun Cluster which creates routes
1363*0Sstevel@tonic-gate 		 * using the the loopback interface's address as a gateway.
1364*0Sstevel@tonic-gate 		 *
1365*0Sstevel@tonic-gate 		 * As ifrt->ifrt_type reflects the already updated ire_type and
1366*0Sstevel@tonic-gate 		 * since ire_create_v6() expects that IRE_IF_NORESOLVER will
1367*0Sstevel@tonic-gate 		 * have a valid ire_dlureq_mp field (which doesn't make sense
1368*0Sstevel@tonic-gate 		 * for a IRE_LOOPBACK), ire_create_v6() will be called in the
1369*0Sstevel@tonic-gate 		 * same way here as in ip_rt_add_v6(), namely using
1370*0Sstevel@tonic-gate 		 * ipif->ipif_net_type when the route looks like a traditional
1371*0Sstevel@tonic-gate 		 * interface route (where ifrt->ifrt_type & IRE_INTERFACE is
1372*0Sstevel@tonic-gate 		 * true) and otherwise using the saved ifrt->ifrt_type.  This
1373*0Sstevel@tonic-gate 		 * means that in the case where ipif->ipif_net_type is
1374*0Sstevel@tonic-gate 		 * IRE_LOOPBACK, the ire created by ire_create_v6() will be an
1375*0Sstevel@tonic-gate 		 * IRE_LOOPBACK, it will then be turned into an
1376*0Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER and then added by ire_add_v6().
1377*0Sstevel@tonic-gate 		 */
1378*0Sstevel@tonic-gate 		ifrt = (ifrt_t *)mp->b_rptr;
1379*0Sstevel@tonic-gate 		if (ifrt->ifrt_type & IRE_INTERFACE) {
1380*0Sstevel@tonic-gate 			rfq = NULL;
1381*0Sstevel@tonic-gate 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
1382*0Sstevel@tonic-gate 			    ? ipif->ipif_rq : ipif->ipif_wq;
1383*0Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
1384*0Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr
1385*0Sstevel@tonic-gate 			    : &ipif->ipif_v6src_addr;
1386*0Sstevel@tonic-gate 			gateway_addr = NULL;
1387*0Sstevel@tonic-gate 			resolver_mp = ipif->ipif_resolver_mp;
1388*0Sstevel@tonic-gate 			type = ipif->ipif_net_type;
1389*0Sstevel@tonic-gate 		} else {
1390*0Sstevel@tonic-gate 			rfq = NULL;
1391*0Sstevel@tonic-gate 			stq = NULL;
1392*0Sstevel@tonic-gate 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
1393*0Sstevel@tonic-gate 			    ? &ifrt->ifrt_v6src_addr : NULL;
1394*0Sstevel@tonic-gate 			gateway_addr = &ifrt->ifrt_v6gateway_addr;
1395*0Sstevel@tonic-gate 			resolver_mp = NULL;
1396*0Sstevel@tonic-gate 			type = ifrt->ifrt_type;
1397*0Sstevel@tonic-gate 		}
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 		/*
1400*0Sstevel@tonic-gate 		 * Create a copy of the IRE with the saved address and netmask.
1401*0Sstevel@tonic-gate 		 */
1402*0Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n",
1403*0Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
1404*0Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)),
1405*0Sstevel@tonic-gate 		    ip_mask_to_plen_v6(&ifrt->ifrt_v6mask)));
1406*0Sstevel@tonic-gate 		ire = ire_create_v6(
1407*0Sstevel@tonic-gate 		    &ifrt->ifrt_v6addr,
1408*0Sstevel@tonic-gate 		    &ifrt->ifrt_v6mask,
1409*0Sstevel@tonic-gate 		    src_addr,
1410*0Sstevel@tonic-gate 		    gateway_addr,
1411*0Sstevel@tonic-gate 		    &ifrt->ifrt_max_frag,
1412*0Sstevel@tonic-gate 		    NULL,
1413*0Sstevel@tonic-gate 		    rfq,
1414*0Sstevel@tonic-gate 		    stq,
1415*0Sstevel@tonic-gate 		    type,
1416*0Sstevel@tonic-gate 		    resolver_mp,
1417*0Sstevel@tonic-gate 		    ipif,
1418*0Sstevel@tonic-gate 		    NULL,
1419*0Sstevel@tonic-gate 		    0,
1420*0Sstevel@tonic-gate 		    0,
1421*0Sstevel@tonic-gate 		    ifrt->ifrt_flags,
1422*0Sstevel@tonic-gate 		    &ifrt->ifrt_iulp_info);
1423*0Sstevel@tonic-gate 		if (ire == NULL) {
1424*0Sstevel@tonic-gate 			mutex_exit(&ipif->ipif_saved_ire_lock);
1425*0Sstevel@tonic-gate 			kmem_free(ipif_saved_irep,
1426*0Sstevel@tonic-gate 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
1427*0Sstevel@tonic-gate 			return (NULL);
1428*0Sstevel@tonic-gate 		}
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 		/*
1431*0Sstevel@tonic-gate 		 * Some software (for example, GateD and Sun Cluster) attempts
1432*0Sstevel@tonic-gate 		 * to create (what amount to) IRE_PREFIX routes with the
1433*0Sstevel@tonic-gate 		 * loopback address as the gateway.  This is primarily done to
1434*0Sstevel@tonic-gate 		 * set up prefixes with the RTF_REJECT flag set (for example,
1435*0Sstevel@tonic-gate 		 * when generating aggregate routes.)
1436*0Sstevel@tonic-gate 		 *
1437*0Sstevel@tonic-gate 		 * If the IRE type (as defined by ipif->ipif_net_type) is
1438*0Sstevel@tonic-gate 		 * IRE_LOOPBACK, then we map the request into a
1439*0Sstevel@tonic-gate 		 * IRE_IF_NORESOLVER.
1440*0Sstevel@tonic-gate 		 */
1441*0Sstevel@tonic-gate 		if (ipif->ipif_net_type == IRE_LOOPBACK)
1442*0Sstevel@tonic-gate 			ire->ire_type = IRE_IF_NORESOLVER;
1443*0Sstevel@tonic-gate 		/*
1444*0Sstevel@tonic-gate 		 * ire held by ire_add, will be refreled' in ipif_up_done
1445*0Sstevel@tonic-gate 		 * towards the end
1446*0Sstevel@tonic-gate 		 */
1447*0Sstevel@tonic-gate 		(void) ire_add(&ire, NULL, NULL, NULL);
1448*0Sstevel@tonic-gate 		*irep = ire;
1449*0Sstevel@tonic-gate 		irep++;
1450*0Sstevel@tonic-gate 		ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire));
1451*0Sstevel@tonic-gate 	}
1452*0Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_saved_ire_lock);
1453*0Sstevel@tonic-gate 	return (ipif_saved_irep);
1454*0Sstevel@tonic-gate }
1455*0Sstevel@tonic-gate 
1456*0Sstevel@tonic-gate /*
1457*0Sstevel@tonic-gate  * Return the scope of the given IPv6 address.  If the address is an
1458*0Sstevel@tonic-gate  * IPv4 mapped IPv6 address, return the scope of the corresponding
1459*0Sstevel@tonic-gate  * IPv4 address.
1460*0Sstevel@tonic-gate  */
1461*0Sstevel@tonic-gate in6addr_scope_t
1462*0Sstevel@tonic-gate ip_addr_scope_v6(const in6_addr_t *addr)
1463*0Sstevel@tonic-gate {
1464*0Sstevel@tonic-gate 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
1465*0Sstevel@tonic-gate 
1466*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
1467*0Sstevel@tonic-gate 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
1468*0Sstevel@tonic-gate 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
1469*0Sstevel@tonic-gate 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
1470*0Sstevel@tonic-gate 			return (IP6_SCOPE_LINKLOCAL);
1471*0Sstevel@tonic-gate 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
1472*0Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
1473*0Sstevel@tonic-gate 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
1474*0Sstevel@tonic-gate 			return (IP6_SCOPE_SITELOCAL);
1475*0Sstevel@tonic-gate 		return (IP6_SCOPE_GLOBAL);
1476*0Sstevel@tonic-gate 	}
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_MULTICAST(addr))
1479*0Sstevel@tonic-gate 		return (IN6_ADDR_MC_SCOPE(addr));
1480*0Sstevel@tonic-gate 
1481*0Sstevel@tonic-gate 	/* link-local and loopback addresses are of link-local scope */
1482*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
1483*0Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
1484*0Sstevel@tonic-gate 		return (IP6_SCOPE_LINKLOCAL);
1485*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_SITELOCAL(addr))
1486*0Sstevel@tonic-gate 		return (IP6_SCOPE_SITELOCAL);
1487*0Sstevel@tonic-gate 	return (IP6_SCOPE_GLOBAL);
1488*0Sstevel@tonic-gate }
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate /*
1492*0Sstevel@tonic-gate  * Calculates the xor of a1 and a2, and stores the result in res.
1493*0Sstevel@tonic-gate  */
1494*0Sstevel@tonic-gate static void
1495*0Sstevel@tonic-gate ip_addr_xor_v6(const in6_addr_t *a1, const in6_addr_t *a2, in6_addr_t *res)
1496*0Sstevel@tonic-gate {
1497*0Sstevel@tonic-gate 	int i;
1498*0Sstevel@tonic-gate 
1499*0Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
1500*0Sstevel@tonic-gate 		res->s6_addr32[i] = a1->s6_addr32[i] ^ a2->s6_addr32[i];
1501*0Sstevel@tonic-gate }
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate #define	IPIF_VALID_IPV6_SOURCE(ipif) \
1504*0Sstevel@tonic-gate 	(((ipif)->ipif_flags & IPIF_UP) && \
1505*0Sstevel@tonic-gate 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)))
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate /* source address candidate */
1508*0Sstevel@tonic-gate typedef struct candidate {
1509*0Sstevel@tonic-gate 	ipif_t		*cand_ipif;
1510*0Sstevel@tonic-gate 	/* The properties of this candidate */
1511*0Sstevel@tonic-gate 	boolean_t	cand_isdst;
1512*0Sstevel@tonic-gate 	boolean_t	cand_isdst_set;
1513*0Sstevel@tonic-gate 	in6addr_scope_t	cand_scope;
1514*0Sstevel@tonic-gate 	boolean_t	cand_scope_set;
1515*0Sstevel@tonic-gate 	boolean_t	cand_isdeprecated;
1516*0Sstevel@tonic-gate 	boolean_t	cand_isdeprecated_set;
1517*0Sstevel@tonic-gate 	boolean_t	cand_ispreferred;
1518*0Sstevel@tonic-gate 	boolean_t	cand_ispreferred_set;
1519*0Sstevel@tonic-gate 	boolean_t	cand_matchedinterface;
1520*0Sstevel@tonic-gate 	boolean_t	cand_matchedinterface_set;
1521*0Sstevel@tonic-gate 	boolean_t	cand_matchedlabel;
1522*0Sstevel@tonic-gate 	boolean_t	cand_matchedlabel_set;
1523*0Sstevel@tonic-gate 	boolean_t	cand_istmp;
1524*0Sstevel@tonic-gate 	boolean_t	cand_istmp_set;
1525*0Sstevel@tonic-gate 	in6_addr_t	cand_xor;
1526*0Sstevel@tonic-gate 	boolean_t	cand_xor_set;
1527*0Sstevel@tonic-gate } cand_t;
1528*0Sstevel@tonic-gate #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
1529*0Sstevel@tonic-gate #define	cand_flags	cand_ipif->ipif_flags
1530*0Sstevel@tonic-gate #define	cand_ill	cand_ipif->ipif_ill
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate /* information about the destination for source address selection */
1533*0Sstevel@tonic-gate typedef struct dstinfo {
1534*0Sstevel@tonic-gate 	const in6_addr_t	*dst_addr;
1535*0Sstevel@tonic-gate 	ill_t			*dst_ill;
1536*0Sstevel@tonic-gate 	boolean_t		dst_restrict_ill;
1537*0Sstevel@tonic-gate 	boolean_t		dst_prefer_src_tmp;
1538*0Sstevel@tonic-gate 	in6addr_scope_t		dst_scope;
1539*0Sstevel@tonic-gate 	char			*dst_label;
1540*0Sstevel@tonic-gate } dstinfo_t;
1541*0Sstevel@tonic-gate 
1542*0Sstevel@tonic-gate /*
1543*0Sstevel@tonic-gate  * The following functions are rules used to select a source address in
1544*0Sstevel@tonic-gate  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
1545*0Sstevel@tonic-gate  * against the best candidate (bc).  Each rule has three possible outcomes;
1546*0Sstevel@tonic-gate  * the candidate is preferred over the best candidate (CAND_PREFER), the
1547*0Sstevel@tonic-gate  * candidate is not preferred over the best candidate (CAND_AVOID), or the
1548*0Sstevel@tonic-gate  * candidate is of equal value as the best candidate (CAND_TIE).
1549*0Sstevel@tonic-gate  *
1550*0Sstevel@tonic-gate  * These rules are part of a greater "Default Address Selection for IPv6"
1551*0Sstevel@tonic-gate  * sheme, which is standards based work coming out of the IETF ipv6 working
1552*0Sstevel@tonic-gate  * group.  The IETF document defines both IPv6 source address selection and
1553*0Sstevel@tonic-gate  * destination address ordering.  The rules defined here implement the IPv6
1554*0Sstevel@tonic-gate  * source address selection.  Destination address ordering is done by
1555*0Sstevel@tonic-gate  * libnsl, and uses a similar set of rules to implement the sorting.
1556*0Sstevel@tonic-gate  */
1557*0Sstevel@tonic-gate typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
1558*0Sstevel@tonic-gate typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *);
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate /* Prefer an address if it is equal to the destination address. */
1561*0Sstevel@tonic-gate static rule_res_t
1562*0Sstevel@tonic-gate rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1563*0Sstevel@tonic-gate {
1564*0Sstevel@tonic-gate 	if (!bc->cand_isdst_set) {
1565*0Sstevel@tonic-gate 		bc->cand_isdst =
1566*0Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
1567*0Sstevel@tonic-gate 		bc->cand_isdst_set = B_TRUE;
1568*0Sstevel@tonic-gate 	}
1569*0Sstevel@tonic-gate 
1570*0Sstevel@tonic-gate 	cc->cand_isdst =
1571*0Sstevel@tonic-gate 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
1572*0Sstevel@tonic-gate 	cc->cand_isdst_set = B_TRUE;
1573*0Sstevel@tonic-gate 
1574*0Sstevel@tonic-gate 	if (cc->cand_isdst == bc->cand_isdst)
1575*0Sstevel@tonic-gate 		return (CAND_TIE);
1576*0Sstevel@tonic-gate 	else if (cc->cand_isdst)
1577*0Sstevel@tonic-gate 		return (CAND_PREFER);
1578*0Sstevel@tonic-gate 	else
1579*0Sstevel@tonic-gate 		return (CAND_AVOID);
1580*0Sstevel@tonic-gate }
1581*0Sstevel@tonic-gate 
1582*0Sstevel@tonic-gate /*
1583*0Sstevel@tonic-gate  * Prefer addresses that are of closest scope to the destination.  Always
1584*0Sstevel@tonic-gate  * prefer addresses that are of greater scope than the destination over
1585*0Sstevel@tonic-gate  * those that are of lesser scope than the destination.
1586*0Sstevel@tonic-gate  */
1587*0Sstevel@tonic-gate static rule_res_t
1588*0Sstevel@tonic-gate rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1589*0Sstevel@tonic-gate {
1590*0Sstevel@tonic-gate 	if (!bc->cand_scope_set) {
1591*0Sstevel@tonic-gate 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
1592*0Sstevel@tonic-gate 		bc->cand_scope_set = B_TRUE;
1593*0Sstevel@tonic-gate 	}
1594*0Sstevel@tonic-gate 
1595*0Sstevel@tonic-gate 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
1596*0Sstevel@tonic-gate 	cc->cand_scope_set = B_TRUE;
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 	if (cc->cand_scope < bc->cand_scope) {
1599*0Sstevel@tonic-gate 		if (cc->cand_scope < dstinfo->dst_scope)
1600*0Sstevel@tonic-gate 			return (CAND_AVOID);
1601*0Sstevel@tonic-gate 		else
1602*0Sstevel@tonic-gate 			return (CAND_PREFER);
1603*0Sstevel@tonic-gate 	} else if (bc->cand_scope < cc->cand_scope) {
1604*0Sstevel@tonic-gate 		if (bc->cand_scope < dstinfo->dst_scope)
1605*0Sstevel@tonic-gate 			return (CAND_PREFER);
1606*0Sstevel@tonic-gate 		else
1607*0Sstevel@tonic-gate 			return (CAND_AVOID);
1608*0Sstevel@tonic-gate 	} else {
1609*0Sstevel@tonic-gate 		return (CAND_TIE);
1610*0Sstevel@tonic-gate 	}
1611*0Sstevel@tonic-gate }
1612*0Sstevel@tonic-gate 
1613*0Sstevel@tonic-gate /*
1614*0Sstevel@tonic-gate  * Prefer non-deprecated source addresses.
1615*0Sstevel@tonic-gate  */
1616*0Sstevel@tonic-gate /* ARGSUSED2 */
1617*0Sstevel@tonic-gate static rule_res_t
1618*0Sstevel@tonic-gate rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1619*0Sstevel@tonic-gate {
1620*0Sstevel@tonic-gate 	if (!bc->cand_isdeprecated_set) {
1621*0Sstevel@tonic-gate 		bc->cand_isdeprecated =
1622*0Sstevel@tonic-gate 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
1623*0Sstevel@tonic-gate 		bc->cand_isdeprecated_set = B_TRUE;
1624*0Sstevel@tonic-gate 	}
1625*0Sstevel@tonic-gate 
1626*0Sstevel@tonic-gate 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
1627*0Sstevel@tonic-gate 	cc->cand_isdeprecated_set = B_TRUE;
1628*0Sstevel@tonic-gate 
1629*0Sstevel@tonic-gate 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
1630*0Sstevel@tonic-gate 		return (CAND_TIE);
1631*0Sstevel@tonic-gate 	else if (cc->cand_isdeprecated)
1632*0Sstevel@tonic-gate 		return (CAND_AVOID);
1633*0Sstevel@tonic-gate 	else
1634*0Sstevel@tonic-gate 		return (CAND_PREFER);
1635*0Sstevel@tonic-gate }
1636*0Sstevel@tonic-gate 
1637*0Sstevel@tonic-gate /*
1638*0Sstevel@tonic-gate  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
1639*0Sstevel@tonic-gate  * rule must be before rule_interface because the flag could be set on any
1640*0Sstevel@tonic-gate  * interface, not just the interface being used for outgoing packets (for
1641*0Sstevel@tonic-gate  * example, the IFF_PREFERRED could be set on an address assigned to the
1642*0Sstevel@tonic-gate  * loopback interface).
1643*0Sstevel@tonic-gate  */
1644*0Sstevel@tonic-gate /* ARGSUSED2 */
1645*0Sstevel@tonic-gate static rule_res_t
1646*0Sstevel@tonic-gate rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1647*0Sstevel@tonic-gate {
1648*0Sstevel@tonic-gate 	if (!bc->cand_ispreferred_set) {
1649*0Sstevel@tonic-gate 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
1650*0Sstevel@tonic-gate 		bc->cand_ispreferred_set = B_TRUE;
1651*0Sstevel@tonic-gate 	}
1652*0Sstevel@tonic-gate 
1653*0Sstevel@tonic-gate 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
1654*0Sstevel@tonic-gate 	cc->cand_ispreferred_set = B_TRUE;
1655*0Sstevel@tonic-gate 
1656*0Sstevel@tonic-gate 	if (bc->cand_ispreferred == cc->cand_ispreferred)
1657*0Sstevel@tonic-gate 		return (CAND_TIE);
1658*0Sstevel@tonic-gate 	else if (cc->cand_ispreferred)
1659*0Sstevel@tonic-gate 		return (CAND_PREFER);
1660*0Sstevel@tonic-gate 	else
1661*0Sstevel@tonic-gate 		return (CAND_AVOID);
1662*0Sstevel@tonic-gate }
1663*0Sstevel@tonic-gate 
1664*0Sstevel@tonic-gate /*
1665*0Sstevel@tonic-gate  * Prefer source addresses that are assigned to the outgoing interface, or
1666*0Sstevel@tonic-gate  * to an interface that is in the same IPMP group as the outgoing
1667*0Sstevel@tonic-gate  * interface.
1668*0Sstevel@tonic-gate  */
1669*0Sstevel@tonic-gate static rule_res_t
1670*0Sstevel@tonic-gate rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1671*0Sstevel@tonic-gate {
1672*0Sstevel@tonic-gate 	ill_t *dstill = dstinfo->dst_ill;
1673*0Sstevel@tonic-gate 
1674*0Sstevel@tonic-gate 	/*
1675*0Sstevel@tonic-gate 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
1676*0Sstevel@tonic-gate 	 * since we know all candidates will be on the same link.
1677*0Sstevel@tonic-gate 	 */
1678*0Sstevel@tonic-gate 	if (dstinfo->dst_restrict_ill)
1679*0Sstevel@tonic-gate 		return (CAND_TIE);
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate 	if (!bc->cand_matchedinterface_set) {
1682*0Sstevel@tonic-gate 		bc->cand_matchedinterface = (bc->cand_ill == dstill ||
1683*0Sstevel@tonic-gate 		    (dstill->ill_group != NULL &&
1684*0Sstevel@tonic-gate 		    dstill->ill_group == bc->cand_ill->ill_group));
1685*0Sstevel@tonic-gate 		bc->cand_matchedinterface_set = B_TRUE;
1686*0Sstevel@tonic-gate 	}
1687*0Sstevel@tonic-gate 
1688*0Sstevel@tonic-gate 	cc->cand_matchedinterface = (cc->cand_ill == dstill ||
1689*0Sstevel@tonic-gate 	    (dstill->ill_group != NULL &&
1690*0Sstevel@tonic-gate 		dstill->ill_group == cc->cand_ill->ill_group));
1691*0Sstevel@tonic-gate 	cc->cand_matchedinterface_set = B_TRUE;
1692*0Sstevel@tonic-gate 
1693*0Sstevel@tonic-gate 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
1694*0Sstevel@tonic-gate 		return (CAND_TIE);
1695*0Sstevel@tonic-gate 	else if (cc->cand_matchedinterface)
1696*0Sstevel@tonic-gate 		return (CAND_PREFER);
1697*0Sstevel@tonic-gate 	else
1698*0Sstevel@tonic-gate 		return (CAND_AVOID);
1699*0Sstevel@tonic-gate }
1700*0Sstevel@tonic-gate 
1701*0Sstevel@tonic-gate /*
1702*0Sstevel@tonic-gate  * Prefer source addresses whose label matches the destination's label.
1703*0Sstevel@tonic-gate  */
1704*0Sstevel@tonic-gate static rule_res_t
1705*0Sstevel@tonic-gate rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1706*0Sstevel@tonic-gate {
1707*0Sstevel@tonic-gate 	char *label;
1708*0Sstevel@tonic-gate 
1709*0Sstevel@tonic-gate 	if (!bc->cand_matchedlabel_set) {
1710*0Sstevel@tonic-gate 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL);
1711*0Sstevel@tonic-gate 		bc->cand_matchedlabel =
1712*0Sstevel@tonic-gate 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
1713*0Sstevel@tonic-gate 		bc->cand_matchedlabel_set = B_TRUE;
1714*0Sstevel@tonic-gate 	}
1715*0Sstevel@tonic-gate 
1716*0Sstevel@tonic-gate 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL);
1717*0Sstevel@tonic-gate 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
1718*0Sstevel@tonic-gate 	cc->cand_matchedlabel_set = B_TRUE;
1719*0Sstevel@tonic-gate 
1720*0Sstevel@tonic-gate 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
1721*0Sstevel@tonic-gate 		return (CAND_TIE);
1722*0Sstevel@tonic-gate 	else if (cc->cand_matchedlabel)
1723*0Sstevel@tonic-gate 		return (CAND_PREFER);
1724*0Sstevel@tonic-gate 	else
1725*0Sstevel@tonic-gate 		return (CAND_AVOID);
1726*0Sstevel@tonic-gate }
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate /*
1729*0Sstevel@tonic-gate  * Prefer public addresses over temporary ones.  An application can reverse
1730*0Sstevel@tonic-gate  * the logic of this rule and prefer temporary addresses by using the
1731*0Sstevel@tonic-gate  * IPV6_SRC_PREFERENCES socket option.
1732*0Sstevel@tonic-gate  */
1733*0Sstevel@tonic-gate static rule_res_t
1734*0Sstevel@tonic-gate rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1735*0Sstevel@tonic-gate {
1736*0Sstevel@tonic-gate 	if (!bc->cand_istmp_set) {
1737*0Sstevel@tonic-gate 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
1738*0Sstevel@tonic-gate 		bc->cand_istmp_set = B_TRUE;
1739*0Sstevel@tonic-gate 	}
1740*0Sstevel@tonic-gate 
1741*0Sstevel@tonic-gate 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
1742*0Sstevel@tonic-gate 	cc->cand_istmp_set = B_TRUE;
1743*0Sstevel@tonic-gate 
1744*0Sstevel@tonic-gate 	if (bc->cand_istmp == cc->cand_istmp)
1745*0Sstevel@tonic-gate 		return (CAND_TIE);
1746*0Sstevel@tonic-gate 
1747*0Sstevel@tonic-gate 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
1748*0Sstevel@tonic-gate 		return (CAND_PREFER);
1749*0Sstevel@tonic-gate 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
1750*0Sstevel@tonic-gate 		return (CAND_PREFER);
1751*0Sstevel@tonic-gate 	else
1752*0Sstevel@tonic-gate 		return (CAND_AVOID);
1753*0Sstevel@tonic-gate }
1754*0Sstevel@tonic-gate 
1755*0Sstevel@tonic-gate /*
1756*0Sstevel@tonic-gate  * Prefer source addresses with longer matching prefix with the
1757*0Sstevel@tonic-gate  * destination.  Since this is the last rule, it must not produce a tie.
1758*0Sstevel@tonic-gate  * We do the longest matching prefix calculation and the tie break in one
1759*0Sstevel@tonic-gate  * calculation by doing an xor of both addresses with the destination, and
1760*0Sstevel@tonic-gate  * pick the address with the smallest xor value.  That way, we're both
1761*0Sstevel@tonic-gate  * picking the address with the longest matching prefix, and breaking the
1762*0Sstevel@tonic-gate  * tie if they happen to have both have equal mathing prefixes.
1763*0Sstevel@tonic-gate  */
1764*0Sstevel@tonic-gate static rule_res_t
1765*0Sstevel@tonic-gate rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1766*0Sstevel@tonic-gate {
1767*0Sstevel@tonic-gate 	int i;
1768*0Sstevel@tonic-gate 
1769*0Sstevel@tonic-gate 	if (!bc->cand_xor_set) {
1770*0Sstevel@tonic-gate 		ip_addr_xor_v6(&bc->cand_srcaddr,
1771*0Sstevel@tonic-gate 		    dstinfo->dst_addr, &bc->cand_xor);
1772*0Sstevel@tonic-gate 		bc->cand_xor_set = B_TRUE;
1773*0Sstevel@tonic-gate 	}
1774*0Sstevel@tonic-gate 
1775*0Sstevel@tonic-gate 	ip_addr_xor_v6(&cc->cand_srcaddr, dstinfo->dst_addr, &cc->cand_xor);
1776*0Sstevel@tonic-gate 	cc->cand_xor_set = B_TRUE;
1777*0Sstevel@tonic-gate 
1778*0Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
1779*0Sstevel@tonic-gate 		if (bc->cand_xor.s6_addr32[i] != cc->cand_xor.s6_addr32[i])
1780*0Sstevel@tonic-gate 			break;
1781*0Sstevel@tonic-gate 	}
1782*0Sstevel@tonic-gate 
1783*0Sstevel@tonic-gate 	if (cc->cand_xor.s6_addr32[i] < bc->cand_xor.s6_addr32[i])
1784*0Sstevel@tonic-gate 		return (CAND_PREFER);
1785*0Sstevel@tonic-gate 	else
1786*0Sstevel@tonic-gate 		return (CAND_AVOID);
1787*0Sstevel@tonic-gate }
1788*0Sstevel@tonic-gate 
1789*0Sstevel@tonic-gate /*
1790*0Sstevel@tonic-gate  * Determine the best source address given a destination address and a
1791*0Sstevel@tonic-gate  * destination ill.  If no suitable source address is found, it returns
1792*0Sstevel@tonic-gate  * NULL. If there is a usable address pointed to by the usesrc
1793*0Sstevel@tonic-gate  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
1794*0Sstevel@tonic-gate  * fine grained (i.e per interface)
1795*0Sstevel@tonic-gate  *
1796*0Sstevel@tonic-gate  * This implementation is based on the "Default Address Selection for IPv6"
1797*0Sstevel@tonic-gate  * specification produced by the IETF IPv6 working group.  It has been
1798*0Sstevel@tonic-gate  * implemented so that the list of addresses is only traversed once (the
1799*0Sstevel@tonic-gate  * specification's algorithm could traverse the list of addresses once for
1800*0Sstevel@tonic-gate  * every rule).
1801*0Sstevel@tonic-gate  *
1802*0Sstevel@tonic-gate  * The restrict_ill argument restricts the algorithm to chose a source
1803*0Sstevel@tonic-gate  * address that is assigned to the destination ill or an ill in the same
1804*0Sstevel@tonic-gate  * IPMP group as the destination ill.  This is used when the destination
1805*0Sstevel@tonic-gate  * address is a link-local or multicast address, and when
1806*0Sstevel@tonic-gate  * ipv6_strict_dst_multihoming is turned on.
1807*0Sstevel@tonic-gate  *
1808*0Sstevel@tonic-gate  * src_prefs is the caller's set of source address preferences.  If source
1809*0Sstevel@tonic-gate  * address selection is being called to determine the source address of a
1810*0Sstevel@tonic-gate  * connected socket (from ip_bind_connected_v6()), then the preferences are
1811*0Sstevel@tonic-gate  * taken from conn_src_preferences.  These preferences can be set on a
1812*0Sstevel@tonic-gate  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
1813*0Sstevel@tonic-gate  * preference currently implemented is for rfc3041 temporary addresses.
1814*0Sstevel@tonic-gate  */
1815*0Sstevel@tonic-gate ipif_t *
1816*0Sstevel@tonic-gate ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
1817*0Sstevel@tonic-gate     boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid)
1818*0Sstevel@tonic-gate {
1819*0Sstevel@tonic-gate 	dstinfo_t	dstinfo;
1820*0Sstevel@tonic-gate 	char		dstr[INET6_ADDRSTRLEN];
1821*0Sstevel@tonic-gate 	char		sstr[INET6_ADDRSTRLEN];
1822*0Sstevel@tonic-gate 	ipif_t		*ipif;
1823*0Sstevel@tonic-gate 	ill_t		*ill, *usesrc_ill = NULL;
1824*0Sstevel@tonic-gate 	ill_walk_context_t	ctx;
1825*0Sstevel@tonic-gate 	cand_t		best_c;	/* The best candidate */
1826*0Sstevel@tonic-gate 	cand_t		curr_c;	/* The current candidate */
1827*0Sstevel@tonic-gate 	uint_t		index;
1828*0Sstevel@tonic-gate 	boolean_t	first_candidate = B_TRUE;
1829*0Sstevel@tonic-gate 	rule_res_t	rule_result;
1830*0Sstevel@tonic-gate 	phyint_t	*phyi;
1831*0Sstevel@tonic-gate 
1832*0Sstevel@tonic-gate 	/*
1833*0Sstevel@tonic-gate 	 * The list of ordering rules.  They are applied in the order they
1834*0Sstevel@tonic-gate 	 * appear in the list.
1835*0Sstevel@tonic-gate 	 *
1836*0Sstevel@tonic-gate 	 * XXX rule_mipv6 will need to be implemented (the specification's
1837*0Sstevel@tonic-gate 	 * rules 4) if a mobile IPv6 node is ever implemented.
1838*0Sstevel@tonic-gate 	 */
1839*0Sstevel@tonic-gate 	rulef_t	rules[] = {
1840*0Sstevel@tonic-gate 		rule_isdst,
1841*0Sstevel@tonic-gate 		rule_scope,
1842*0Sstevel@tonic-gate 		rule_deprecated,
1843*0Sstevel@tonic-gate 		rule_preferred,
1844*0Sstevel@tonic-gate 		rule_interface,
1845*0Sstevel@tonic-gate 		rule_label,
1846*0Sstevel@tonic-gate 		rule_temporary,
1847*0Sstevel@tonic-gate 		rule_prefix,
1848*0Sstevel@tonic-gate 		NULL
1849*0Sstevel@tonic-gate 	};
1850*0Sstevel@tonic-gate 
1851*0Sstevel@tonic-gate 	ASSERT(dstill->ill_isv6);
1852*0Sstevel@tonic-gate 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
1853*0Sstevel@tonic-gate 
1854*0Sstevel@tonic-gate 	/*
1855*0Sstevel@tonic-gate 	 * Check if there is a usable src address pointed to by the
1856*0Sstevel@tonic-gate 	 * usesrc ifindex. This has higher precedence since it is
1857*0Sstevel@tonic-gate 	 * finer grained (i.e per interface) v/s being system wide.
1858*0Sstevel@tonic-gate 	 */
1859*0Sstevel@tonic-gate 	if (dstill->ill_usesrc_ifindex != 0) {
1860*0Sstevel@tonic-gate 		if ((usesrc_ill =
1861*0Sstevel@tonic-gate 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
1862*0Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL)) != NULL) {
1863*0Sstevel@tonic-gate 			dstinfo.dst_ill = usesrc_ill;
1864*0Sstevel@tonic-gate 		} else {
1865*0Sstevel@tonic-gate 			return (NULL);
1866*0Sstevel@tonic-gate 		}
1867*0Sstevel@tonic-gate 	} else {
1868*0Sstevel@tonic-gate 		dstinfo.dst_ill = dstill;
1869*0Sstevel@tonic-gate 	}
1870*0Sstevel@tonic-gate 
1871*0Sstevel@tonic-gate 	dstinfo.dst_addr = dst;
1872*0Sstevel@tonic-gate 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
1873*0Sstevel@tonic-gate 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL);
1874*0Sstevel@tonic-gate 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
1877*0Sstevel@tonic-gate 	/*
1878*0Sstevel@tonic-gate 	 * Section three of the I-D states that for multicast and
1879*0Sstevel@tonic-gate 	 * link-local destinations, the candidate set must be restricted to
1880*0Sstevel@tonic-gate 	 * an interface that is on the same link as the outgoing interface.
1881*0Sstevel@tonic-gate 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
1882*0Sstevel@tonic-gate 	 * restrict the source address to the destination link as doing
1883*0Sstevel@tonic-gate 	 * otherwise will almost certainly cause problems.
1884*0Sstevel@tonic-gate 	 */
1885*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
1886*0Sstevel@tonic-gate 	    ipv6_strict_dst_multihoming || usesrc_ill != NULL)
1887*0Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = B_TRUE;
1888*0Sstevel@tonic-gate 	else
1889*0Sstevel@tonic-gate 		dstinfo.dst_restrict_ill = restrict_ill;
1890*0Sstevel@tonic-gate 
1891*0Sstevel@tonic-gate 	bzero(&best_c, sizeof (cand_t));
1892*0Sstevel@tonic-gate 
1893*0Sstevel@tonic-gate 	/*
1894*0Sstevel@tonic-gate 	 * Take a pass through the list of IPv6 interfaces to chose the
1895*0Sstevel@tonic-gate 	 * best possible source address.  If restrict_ill is true, we only
1896*0Sstevel@tonic-gate 	 * iterate through the ill's that are in the same IPMP group as the
1897*0Sstevel@tonic-gate 	 * destination's outgoing ill.  If restrict_ill is false, we walk
1898*0Sstevel@tonic-gate 	 * the entire list of IPv6 ill's.
1899*0Sstevel@tonic-gate 	 */
1900*0Sstevel@tonic-gate 	if (dstinfo.dst_restrict_ill) {
1901*0Sstevel@tonic-gate 		if (dstinfo.dst_ill->ill_group != NULL) {
1902*0Sstevel@tonic-gate 			/*
1903*0Sstevel@tonic-gate 			 * Try to avoid FAILED/OFFLINE ills. Global and
1904*0Sstevel@tonic-gate 			 * site local addresses will failover and are not
1905*0Sstevel@tonic-gate 			 * an issue even if we select them. (i.e. this is
1906*0Sstevel@tonic-gate 			 * a race where we hit this path before in.mpathd
1907*0Sstevel@tonic-gate 			 * moves them. But link local addresses don't move.
1908*0Sstevel@tonic-gate 			 * This creates a problem for NUD. If NUD ends up
1909*0Sstevel@tonic-gate 			 * (nce_xmit) using the src addr from a failed
1910*0Sstevel@tonic-gate 			 * interface NUD will fail and end up deleting the nce
1911*0Sstevel@tonic-gate 			 * This will cause performance issues where ires
1912*0Sstevel@tonic-gate 			 * are frequently created and deleted every few secs.
1913*0Sstevel@tonic-gate 			 */
1914*0Sstevel@tonic-gate 			for (ill = dstinfo.dst_ill->ill_group->illgrp_ill;
1915*0Sstevel@tonic-gate 			    ill != NULL; ill = ill->ill_group_next) {
1916*0Sstevel@tonic-gate 				phyi = ill->ill_phyint;
1917*0Sstevel@tonic-gate 				if (!(phyi->phyint_flags &
1918*0Sstevel@tonic-gate 				    (PHYI_OFFLINE | PHYI_FAILED)))
1919*0Sstevel@tonic-gate 					break;
1920*0Sstevel@tonic-gate 			}
1921*0Sstevel@tonic-gate 			if (ill == NULL)
1922*0Sstevel@tonic-gate 				ill = dstinfo.dst_ill->ill_group->illgrp_ill;
1923*0Sstevel@tonic-gate 		} else {
1924*0Sstevel@tonic-gate 			ill = dstinfo.dst_ill;
1925*0Sstevel@tonic-gate 		}
1926*0Sstevel@tonic-gate 	} else {
1927*0Sstevel@tonic-gate 		ill = ILL_START_WALK_V6(&ctx);
1928*0Sstevel@tonic-gate 	}
1929*0Sstevel@tonic-gate 
1930*0Sstevel@tonic-gate 	while (ill != NULL) {
1931*0Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
1932*0Sstevel@tonic-gate 
1933*0Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
1934*0Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
1935*0Sstevel@tonic-gate 
1936*0Sstevel@tonic-gate 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
1937*0Sstevel@tonic-gate 				continue;
1938*0Sstevel@tonic-gate 
1939*0Sstevel@tonic-gate 			if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid)
1940*0Sstevel@tonic-gate 				continue;
1941*0Sstevel@tonic-gate 
1942*0Sstevel@tonic-gate 			if (first_candidate) {
1943*0Sstevel@tonic-gate 				/*
1944*0Sstevel@tonic-gate 				 * This is first valid address in the list.
1945*0Sstevel@tonic-gate 				 * It is automatically the best candidate
1946*0Sstevel@tonic-gate 				 * so far.
1947*0Sstevel@tonic-gate 				 */
1948*0Sstevel@tonic-gate 				best_c.cand_ipif = ipif;
1949*0Sstevel@tonic-gate 				first_candidate = B_FALSE;
1950*0Sstevel@tonic-gate 				continue;
1951*0Sstevel@tonic-gate 			}
1952*0Sstevel@tonic-gate 
1953*0Sstevel@tonic-gate 			bzero(&curr_c, sizeof (cand_t));
1954*0Sstevel@tonic-gate 			curr_c.cand_ipif = ipif;
1955*0Sstevel@tonic-gate 
1956*0Sstevel@tonic-gate 			/*
1957*0Sstevel@tonic-gate 			 * Compare this current candidate (curr_c) with the
1958*0Sstevel@tonic-gate 			 * best candidate (best_c) by applying the
1959*0Sstevel@tonic-gate 			 * comparison rules in order until one breaks the
1960*0Sstevel@tonic-gate 			 * tie.
1961*0Sstevel@tonic-gate 			 */
1962*0Sstevel@tonic-gate 			for (index = 0; rules[index] != NULL; index++) {
1963*0Sstevel@tonic-gate 				/* Apply a comparison rule. */
1964*0Sstevel@tonic-gate 				rule_result =
1965*0Sstevel@tonic-gate 				    (rules[index])(&best_c, &curr_c, &dstinfo);
1966*0Sstevel@tonic-gate 				if (rule_result == CAND_AVOID) {
1967*0Sstevel@tonic-gate 					/*
1968*0Sstevel@tonic-gate 					 * The best candidate is still the
1969*0Sstevel@tonic-gate 					 * best candidate.  Forget about
1970*0Sstevel@tonic-gate 					 * this current candidate and go on
1971*0Sstevel@tonic-gate 					 * to the next one.
1972*0Sstevel@tonic-gate 					 */
1973*0Sstevel@tonic-gate 					break;
1974*0Sstevel@tonic-gate 				} else if (rule_result == CAND_PREFER) {
1975*0Sstevel@tonic-gate 					/*
1976*0Sstevel@tonic-gate 					 * This candidate is prefered.  It
1977*0Sstevel@tonic-gate 					 * becomes the best candidate so
1978*0Sstevel@tonic-gate 					 * far.  Go on to the next address.
1979*0Sstevel@tonic-gate 					 */
1980*0Sstevel@tonic-gate 					best_c = curr_c;
1981*0Sstevel@tonic-gate 					break;
1982*0Sstevel@tonic-gate 				}
1983*0Sstevel@tonic-gate 				/* We have a tie, apply the next rule. */
1984*0Sstevel@tonic-gate 			}
1985*0Sstevel@tonic-gate 
1986*0Sstevel@tonic-gate 			/*
1987*0Sstevel@tonic-gate 			 * The last rule must be a tie breaker rule and
1988*0Sstevel@tonic-gate 			 * must never produce a tie.  At this point, the
1989*0Sstevel@tonic-gate 			 * candidate should have either been rejected, or
1990*0Sstevel@tonic-gate 			 * have been prefered as the best candidate so far.
1991*0Sstevel@tonic-gate 			 */
1992*0Sstevel@tonic-gate 			ASSERT(rule_result != CAND_TIE);
1993*0Sstevel@tonic-gate 		}
1994*0Sstevel@tonic-gate 
1995*0Sstevel@tonic-gate 		/*
1996*0Sstevel@tonic-gate 		 * We may be walking the linked-list of ill's in an
1997*0Sstevel@tonic-gate 		 * IPMP group or traversing the IPv6 ill avl tree. If it is a
1998*0Sstevel@tonic-gate 		 * usesrc ILL then it can't be part of IPMP group and we
1999*0Sstevel@tonic-gate 		 * will exit the while loop.
2000*0Sstevel@tonic-gate 		 */
2001*0Sstevel@tonic-gate 		if (dstinfo.dst_restrict_ill)
2002*0Sstevel@tonic-gate 			ill = ill->ill_group_next;
2003*0Sstevel@tonic-gate 		else
2004*0Sstevel@tonic-gate 			ill = ill_next(&ctx, ill);
2005*0Sstevel@tonic-gate 	}
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate 	ipif = best_c.cand_ipif;
2008*0Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
2009*0Sstevel@tonic-gate 	    dstinfo.dst_ill->ill_name,
2010*0Sstevel@tonic-gate 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
2011*0Sstevel@tonic-gate 	    (ipif == NULL ? "NULL" :
2012*0Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
2013*0Sstevel@tonic-gate 
2014*0Sstevel@tonic-gate 	if (usesrc_ill != NULL)
2015*0Sstevel@tonic-gate 		ill_refrele(usesrc_ill);
2016*0Sstevel@tonic-gate 
2017*0Sstevel@tonic-gate 	if (ipif == NULL) {
2018*0Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
2019*0Sstevel@tonic-gate 		return (NULL);
2020*0Sstevel@tonic-gate 	}
2021*0Sstevel@tonic-gate 
2022*0Sstevel@tonic-gate 	mutex_enter(&ipif->ipif_ill->ill_lock);
2023*0Sstevel@tonic-gate 	if (IPIF_CAN_LOOKUP(ipif)) {
2024*0Sstevel@tonic-gate 		ipif_refhold_locked(ipif);
2025*0Sstevel@tonic-gate 		mutex_exit(&ipif->ipif_ill->ill_lock);
2026*0Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
2027*0Sstevel@tonic-gate 		return (ipif);
2028*0Sstevel@tonic-gate 	}
2029*0Sstevel@tonic-gate 	mutex_exit(&ipif->ipif_ill->ill_lock);
2030*0Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
2031*0Sstevel@tonic-gate 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
2032*0Sstevel@tonic-gate 	    " returning null \n", (void *)ipif));
2033*0Sstevel@tonic-gate 
2034*0Sstevel@tonic-gate 	return (NULL);
2035*0Sstevel@tonic-gate }
2036*0Sstevel@tonic-gate 
2037*0Sstevel@tonic-gate /*
2038*0Sstevel@tonic-gate  * If old_ipif is not NULL, see if ipif was derived from old
2039*0Sstevel@tonic-gate  * ipif and if so, recreate the interface route by re-doing
2040*0Sstevel@tonic-gate  * source address selection. This happens when ipif_down ->
2041*0Sstevel@tonic-gate  * ipif_update_other_ipifs calls us.
2042*0Sstevel@tonic-gate  *
2043*0Sstevel@tonic-gate  * If old_ipif is NULL, just redo the source address selection
2044*0Sstevel@tonic-gate  * if needed. This happens when illgrp_insert or ipif_up_done_v6
2045*0Sstevel@tonic-gate  * calls us.
2046*0Sstevel@tonic-gate  */
2047*0Sstevel@tonic-gate void
2048*0Sstevel@tonic-gate ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif)
2049*0Sstevel@tonic-gate {
2050*0Sstevel@tonic-gate 	ire_t *ire;
2051*0Sstevel@tonic-gate 	ire_t *ipif_ire;
2052*0Sstevel@tonic-gate 	queue_t *stq;
2053*0Sstevel@tonic-gate 	ill_t *ill;
2054*0Sstevel@tonic-gate 	ipif_t *nipif = NULL;
2055*0Sstevel@tonic-gate 	boolean_t nipif_refheld = B_FALSE;
2056*0Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
2057*0Sstevel@tonic-gate 
2058*0Sstevel@tonic-gate 	ill = ipif->ipif_ill;
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate 	if (!(ipif->ipif_flags &
2061*0Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
2062*0Sstevel@tonic-gate 		/*
2063*0Sstevel@tonic-gate 		 * Can't possibly have borrowed the source
2064*0Sstevel@tonic-gate 		 * from old_ipif.
2065*0Sstevel@tonic-gate 		 */
2066*0Sstevel@tonic-gate 		return;
2067*0Sstevel@tonic-gate 	}
2068*0Sstevel@tonic-gate 
2069*0Sstevel@tonic-gate 	/*
2070*0Sstevel@tonic-gate 	 * Is there any work to be done? No work if the address
2071*0Sstevel@tonic-gate 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
2072*0Sstevel@tonic-gate 	 * ipif_select_source_v6() does not borrow addresses from
2073*0Sstevel@tonic-gate 	 * NOLOCAL and ANYCAST interfaces).
2074*0Sstevel@tonic-gate 	 */
2075*0Sstevel@tonic-gate 	if ((old_ipif != NULL) &&
2076*0Sstevel@tonic-gate 	    ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) ||
2077*0Sstevel@tonic-gate 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
2078*0Sstevel@tonic-gate 	    (old_ipif->ipif_flags &
2079*0Sstevel@tonic-gate 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
2080*0Sstevel@tonic-gate 		return;
2081*0Sstevel@tonic-gate 	}
2082*0Sstevel@tonic-gate 
2083*0Sstevel@tonic-gate 	/*
2084*0Sstevel@tonic-gate 	 * Perform the same checks as when creating the
2085*0Sstevel@tonic-gate 	 * IRE_INTERFACE in ipif_up_done_v6.
2086*0Sstevel@tonic-gate 	 */
2087*0Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_UP))
2088*0Sstevel@tonic-gate 		return;
2089*0Sstevel@tonic-gate 
2090*0Sstevel@tonic-gate 	if ((ipif->ipif_flags & IPIF_NOXMIT))
2091*0Sstevel@tonic-gate 		return;
2092*0Sstevel@tonic-gate 
2093*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
2094*0Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
2095*0Sstevel@tonic-gate 		return;
2096*0Sstevel@tonic-gate 
2097*0Sstevel@tonic-gate 	/*
2098*0Sstevel@tonic-gate 	 * We know that ipif uses some other source for its
2099*0Sstevel@tonic-gate 	 * IRE_INTERFACE. Is it using the source of this
2100*0Sstevel@tonic-gate 	 * old_ipif?
2101*0Sstevel@tonic-gate 	 */
2102*0Sstevel@tonic-gate 	ipif_ire = ipif_to_ire_v6(ipif);
2103*0Sstevel@tonic-gate 	if (ipif_ire == NULL)
2104*0Sstevel@tonic-gate 		return;
2105*0Sstevel@tonic-gate 
2106*0Sstevel@tonic-gate 	if (old_ipif != NULL &&
2107*0Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr,
2108*0Sstevel@tonic-gate 	    &ipif_ire->ire_src_addr_v6)) {
2109*0Sstevel@tonic-gate 		ire_refrele(ipif_ire);
2110*0Sstevel@tonic-gate 		return;
2111*0Sstevel@tonic-gate 	}
2112*0Sstevel@tonic-gate 
2113*0Sstevel@tonic-gate 	if (ip_debug > 2) {
2114*0Sstevel@tonic-gate 		/* ip1dbg */
2115*0Sstevel@tonic-gate 		pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE"
2116*0Sstevel@tonic-gate 		    " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6);
2117*0Sstevel@tonic-gate 	}
2118*0Sstevel@tonic-gate 
2119*0Sstevel@tonic-gate 	stq = ipif_ire->ire_stq;
2120*0Sstevel@tonic-gate 
2121*0Sstevel@tonic-gate 	/*
2122*0Sstevel@tonic-gate 	 * Can't use our source address. Select a different source address
2123*0Sstevel@tonic-gate 	 * for the IRE_INTERFACE.  We restrict interface route source
2124*0Sstevel@tonic-gate 	 * address selection to ipif's assigned to the same link as the
2125*0Sstevel@tonic-gate 	 * interface.
2126*0Sstevel@tonic-gate 	 */
2127*0Sstevel@tonic-gate 	if (ip6_asp_can_lookup()) {
2128*0Sstevel@tonic-gate 		ip6_asp_table_held = B_TRUE;
2129*0Sstevel@tonic-gate 		nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet,
2130*0Sstevel@tonic-gate 		    B_TRUE, IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
2131*0Sstevel@tonic-gate 	}
2132*0Sstevel@tonic-gate 	if (nipif == NULL) {
2133*0Sstevel@tonic-gate 		/* Last resort - all ipif's have IPIF_NOLOCAL */
2134*0Sstevel@tonic-gate 		nipif = ipif;
2135*0Sstevel@tonic-gate 	} else {
2136*0Sstevel@tonic-gate 		nipif_refheld = B_TRUE;
2137*0Sstevel@tonic-gate 	}
2138*0Sstevel@tonic-gate 
2139*0Sstevel@tonic-gate 	ire = ire_create_v6(
2140*0Sstevel@tonic-gate 	    &ipif->ipif_v6subnet,	/* dest pref */
2141*0Sstevel@tonic-gate 	    &ipif->ipif_v6net_mask,	/* mask */
2142*0Sstevel@tonic-gate 	    &nipif->ipif_v6src_addr,	/* src addr */
2143*0Sstevel@tonic-gate 	    NULL,			/* no gateway */
2144*0Sstevel@tonic-gate 	    &ipif->ipif_mtu,		/* max frag */
2145*0Sstevel@tonic-gate 	    NULL,			/* no Fast path header */
2146*0Sstevel@tonic-gate 	    NULL,			/* no recv from queue */
2147*0Sstevel@tonic-gate 	    stq,			/* send-to queue */
2148*0Sstevel@tonic-gate 	    ill->ill_net_type,		/* IF_[NO]RESOLVER */
2149*0Sstevel@tonic-gate 	    ill->ill_resolver_mp,	/* xmit header */
2150*0Sstevel@tonic-gate 	    ipif,
2151*0Sstevel@tonic-gate 	    NULL,
2152*0Sstevel@tonic-gate 	    0,
2153*0Sstevel@tonic-gate 	    0,
2154*0Sstevel@tonic-gate 	    0,
2155*0Sstevel@tonic-gate 	    &ire_uinfo_null);
2156*0Sstevel@tonic-gate 
2157*0Sstevel@tonic-gate 	if (ire != NULL) {
2158*0Sstevel@tonic-gate 		ire_t *ret_ire;
2159*0Sstevel@tonic-gate 		int   error;
2160*0Sstevel@tonic-gate 
2161*0Sstevel@tonic-gate 		/*
2162*0Sstevel@tonic-gate 		 * We don't need ipif_ire anymore. We need to delete
2163*0Sstevel@tonic-gate 		 * before we add so that ire_add does not detect
2164*0Sstevel@tonic-gate 		 * duplicates.
2165*0Sstevel@tonic-gate 		 */
2166*0Sstevel@tonic-gate 		ire_delete(ipif_ire);
2167*0Sstevel@tonic-gate 		ret_ire = ire;
2168*0Sstevel@tonic-gate 		error = ire_add(&ret_ire, NULL, NULL, NULL);
2169*0Sstevel@tonic-gate 		ASSERT(error == 0);
2170*0Sstevel@tonic-gate 		ASSERT(ret_ire == ire);
2171*0Sstevel@tonic-gate 		if (ret_ire != NULL) {
2172*0Sstevel@tonic-gate 			/* Held in ire_add */
2173*0Sstevel@tonic-gate 			ire_refrele(ret_ire);
2174*0Sstevel@tonic-gate 		}
2175*0Sstevel@tonic-gate 	}
2176*0Sstevel@tonic-gate 	/*
2177*0Sstevel@tonic-gate 	 * Either we are falling through from above or could not
2178*0Sstevel@tonic-gate 	 * allocate a replacement.
2179*0Sstevel@tonic-gate 	 */
2180*0Sstevel@tonic-gate 	ire_refrele(ipif_ire);
2181*0Sstevel@tonic-gate 	if (ip6_asp_table_held)
2182*0Sstevel@tonic-gate 		ip6_asp_table_refrele();
2183*0Sstevel@tonic-gate 	if (nipif_refheld)
2184*0Sstevel@tonic-gate 		ipif_refrele(nipif);
2185*0Sstevel@tonic-gate }
2186*0Sstevel@tonic-gate 
2187*0Sstevel@tonic-gate /*
2188*0Sstevel@tonic-gate  * This old_ipif is going away.
2189*0Sstevel@tonic-gate  *
2190*0Sstevel@tonic-gate  * Determine if any other ipif's are using our address as
2191*0Sstevel@tonic-gate  * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
2192*0Sstevel@tonic-gate  * IPIF_DEPRECATED).
2193*0Sstevel@tonic-gate  * Find the IRE_INTERFACE for such ipif's and recreate them
2194*0Sstevel@tonic-gate  * to use an different source address following the rules in
2195*0Sstevel@tonic-gate  * ipif_up_done_v6.
2196*0Sstevel@tonic-gate  *
2197*0Sstevel@tonic-gate  * This function takes an illgrp as an argument so that illgrp_delete
2198*0Sstevel@tonic-gate  * can call this to update source address even after deleting the
2199*0Sstevel@tonic-gate  * old_ipif->ipif_ill from the ill group.
2200*0Sstevel@tonic-gate  */
2201*0Sstevel@tonic-gate void
2202*0Sstevel@tonic-gate ipif_update_other_ipifs_v6(ipif_t *old_ipif, ill_group_t *illgrp)
2203*0Sstevel@tonic-gate {
2204*0Sstevel@tonic-gate 	ipif_t	*ipif;
2205*0Sstevel@tonic-gate 	ill_t	*ill;
2206*0Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
2207*0Sstevel@tonic-gate 
2208*0Sstevel@tonic-gate 	ASSERT(IAM_WRITER_IPIF(old_ipif));
2209*0Sstevel@tonic-gate 
2210*0Sstevel@tonic-gate 	ill = old_ipif->ipif_ill;
2211*0Sstevel@tonic-gate 
2212*0Sstevel@tonic-gate 	ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n",
2213*0Sstevel@tonic-gate 	    ill->ill_name,
2214*0Sstevel@tonic-gate 	    inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr,
2215*0Sstevel@tonic-gate 	    buf, sizeof (buf))));
2216*0Sstevel@tonic-gate 
2217*0Sstevel@tonic-gate 	/*
2218*0Sstevel@tonic-gate 	 * If this part of a group, look at all ills as ipif_select_source
2219*0Sstevel@tonic-gate 	 * borrows a source address across all the ills in the group.
2220*0Sstevel@tonic-gate 	 */
2221*0Sstevel@tonic-gate 	if (illgrp != NULL)
2222*0Sstevel@tonic-gate 		ill = illgrp->illgrp_ill;
2223*0Sstevel@tonic-gate 
2224*0Sstevel@tonic-gate 	/* Don't need a lock since this is a writer */
2225*0Sstevel@tonic-gate 	for (; ill != NULL; ill = ill->ill_group_next) {
2226*0Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
2227*0Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
2228*0Sstevel@tonic-gate 
2229*0Sstevel@tonic-gate 			if (ipif == old_ipif)
2230*0Sstevel@tonic-gate 				continue;
2231*0Sstevel@tonic-gate 
2232*0Sstevel@tonic-gate 			ipif_recreate_interface_routes_v6(old_ipif, ipif);
2233*0Sstevel@tonic-gate 		}
2234*0Sstevel@tonic-gate 	}
2235*0Sstevel@tonic-gate }
2236*0Sstevel@tonic-gate 
2237*0Sstevel@tonic-gate /*
2238*0Sstevel@tonic-gate  * Perform an attach and bind to get phys addr plus info_req for
2239*0Sstevel@tonic-gate  * the physical device.
2240*0Sstevel@tonic-gate  * q and mp represents an ioctl which will be queued waiting for
2241*0Sstevel@tonic-gate  * completion of the DLPI message exchange.
2242*0Sstevel@tonic-gate  * MUST be called on an ill queue. Can not set conn_pending_ill for that
2243*0Sstevel@tonic-gate  * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q.
2244*0Sstevel@tonic-gate  *
2245*0Sstevel@tonic-gate  * Returns EINPROGRESS when mp has been consumed by queueing it on
2246*0Sstevel@tonic-gate  * ill_pending_mp and the ioctl will complete in ip_rput.
2247*0Sstevel@tonic-gate  */
2248*0Sstevel@tonic-gate int
2249*0Sstevel@tonic-gate ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
2250*0Sstevel@tonic-gate {
2251*0Sstevel@tonic-gate 	mblk_t	*v6token_mp = NULL;
2252*0Sstevel@tonic-gate 	mblk_t	*v6lla_mp = NULL;
2253*0Sstevel@tonic-gate 	mblk_t	*phys_mp = NULL;
2254*0Sstevel@tonic-gate 	mblk_t	*info_mp = NULL;
2255*0Sstevel@tonic-gate 	mblk_t	*attach_mp = NULL;
2256*0Sstevel@tonic-gate 	mblk_t	*detach_mp = NULL;
2257*0Sstevel@tonic-gate 	mblk_t	*bind_mp = NULL;
2258*0Sstevel@tonic-gate 	mblk_t	*unbind_mp = NULL;
2259*0Sstevel@tonic-gate 	mblk_t	*notify_mp = NULL;
2260*0Sstevel@tonic-gate 
2261*0Sstevel@tonic-gate 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
2262*0Sstevel@tonic-gate 	ASSERT(ill->ill_dlpi_style_set);
2263*0Sstevel@tonic-gate 	ASSERT(WR(q)->q_next != NULL);
2264*0Sstevel@tonic-gate 
2265*0Sstevel@tonic-gate 	if (ill->ill_isv6) {
2266*0Sstevel@tonic-gate 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2267*0Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2268*0Sstevel@tonic-gate 		if (v6token_mp == NULL)
2269*0Sstevel@tonic-gate 			goto bad;
2270*0Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
2271*0Sstevel@tonic-gate 		    DL_IPV6_TOKEN;
2272*0Sstevel@tonic-gate 
2273*0Sstevel@tonic-gate 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2274*0Sstevel@tonic-gate 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2275*0Sstevel@tonic-gate 		if (v6lla_mp == NULL)
2276*0Sstevel@tonic-gate 			goto bad;
2277*0Sstevel@tonic-gate 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
2278*0Sstevel@tonic-gate 		    DL_IPV6_LINK_LAYER_ADDR;
2279*0Sstevel@tonic-gate 	}
2280*0Sstevel@tonic-gate 
2281*0Sstevel@tonic-gate 	/*
2282*0Sstevel@tonic-gate 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
2283*0Sstevel@tonic-gate 	 */
2284*0Sstevel@tonic-gate 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
2285*0Sstevel@tonic-gate 	    DL_NOTIFY_REQ);
2286*0Sstevel@tonic-gate 	if (notify_mp == NULL)
2287*0Sstevel@tonic-gate 		goto bad;
2288*0Sstevel@tonic-gate 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
2289*0Sstevel@tonic-gate 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
2290*0Sstevel@tonic-gate 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG);
2291*0Sstevel@tonic-gate 
2292*0Sstevel@tonic-gate 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2293*0Sstevel@tonic-gate 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2294*0Sstevel@tonic-gate 	if (phys_mp == NULL)
2295*0Sstevel@tonic-gate 		goto bad;
2296*0Sstevel@tonic-gate 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
2297*0Sstevel@tonic-gate 	    DL_CURR_PHYS_ADDR;
2298*0Sstevel@tonic-gate 
2299*0Sstevel@tonic-gate 	info_mp = ip_dlpi_alloc(
2300*0Sstevel@tonic-gate 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
2301*0Sstevel@tonic-gate 	    DL_INFO_REQ);
2302*0Sstevel@tonic-gate 	if (info_mp == NULL)
2303*0Sstevel@tonic-gate 		goto bad;
2304*0Sstevel@tonic-gate 
2305*0Sstevel@tonic-gate 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
2306*0Sstevel@tonic-gate 	    DL_BIND_REQ);
2307*0Sstevel@tonic-gate 	if (bind_mp == NULL)
2308*0Sstevel@tonic-gate 		goto bad;
2309*0Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
2310*0Sstevel@tonic-gate 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
2311*0Sstevel@tonic-gate 
2312*0Sstevel@tonic-gate 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
2313*0Sstevel@tonic-gate 	if (unbind_mp == NULL)
2314*0Sstevel@tonic-gate 		goto bad;
2315*0Sstevel@tonic-gate 
2316*0Sstevel@tonic-gate 	/* If we need to attach/detach, pre-alloc and initialize the mblks */
2317*0Sstevel@tonic-gate 	if (ill->ill_needs_attach) {
2318*0Sstevel@tonic-gate 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
2319*0Sstevel@tonic-gate 		    DL_ATTACH_REQ);
2320*0Sstevel@tonic-gate 		if (attach_mp == NULL)
2321*0Sstevel@tonic-gate 			goto bad;
2322*0Sstevel@tonic-gate 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
2323*0Sstevel@tonic-gate 
2324*0Sstevel@tonic-gate 		detach_mp = ip_dlpi_alloc(sizeof (dl_detach_req_t),
2325*0Sstevel@tonic-gate 		    DL_DETACH_REQ);
2326*0Sstevel@tonic-gate 		if (detach_mp == NULL)
2327*0Sstevel@tonic-gate 			goto bad;
2328*0Sstevel@tonic-gate 	}
2329*0Sstevel@tonic-gate 
2330*0Sstevel@tonic-gate 	/*
2331*0Sstevel@tonic-gate 	 * Here we are going to delay the ioctl ack until after
2332*0Sstevel@tonic-gate 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
2333*0Sstevel@tonic-gate 	 * original ioctl message before sending the requests
2334*0Sstevel@tonic-gate 	 */
2335*0Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
2336*0Sstevel@tonic-gate 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
2337*0Sstevel@tonic-gate 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
2338*0Sstevel@tonic-gate 	/*
2339*0Sstevel@tonic-gate 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
2340*0Sstevel@tonic-gate 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
2341*0Sstevel@tonic-gate 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
2342*0Sstevel@tonic-gate 	 */
2343*0Sstevel@tonic-gate 	ill->ill_phys_addr_pend = 0;
2344*0Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
2345*0Sstevel@tonic-gate 
2346*0Sstevel@tonic-gate 	if (attach_mp != NULL) {
2347*0Sstevel@tonic-gate 		ip1dbg(("ill_dl_phys: attach\n"));
2348*0Sstevel@tonic-gate 		ill_dlpi_send(ill, attach_mp);
2349*0Sstevel@tonic-gate 	}
2350*0Sstevel@tonic-gate 	ill_dlpi_send(ill, bind_mp);
2351*0Sstevel@tonic-gate 	ill_dlpi_send(ill, info_mp);
2352*0Sstevel@tonic-gate 	if (ill->ill_isv6) {
2353*0Sstevel@tonic-gate 		ill_dlpi_send(ill, v6token_mp);
2354*0Sstevel@tonic-gate 		ill_dlpi_send(ill, v6lla_mp);
2355*0Sstevel@tonic-gate 	}
2356*0Sstevel@tonic-gate 	ill_dlpi_send(ill, phys_mp);
2357*0Sstevel@tonic-gate 	ill_dlpi_send(ill, notify_mp);
2358*0Sstevel@tonic-gate 	ill_dlpi_send(ill, unbind_mp);
2359*0Sstevel@tonic-gate 
2360*0Sstevel@tonic-gate 	/*
2361*0Sstevel@tonic-gate 	 * Save the DL_DETACH_REQ (if there is one) for use in ill_delete().
2362*0Sstevel@tonic-gate 	 */
2363*0Sstevel@tonic-gate 	ASSERT(ill->ill_detach_mp == NULL);
2364*0Sstevel@tonic-gate 	ill->ill_detach_mp = detach_mp;
2365*0Sstevel@tonic-gate 
2366*0Sstevel@tonic-gate 	/*
2367*0Sstevel@tonic-gate 	 * This operation will complete in ip_rput_dlpi_writer with either
2368*0Sstevel@tonic-gate 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
2369*0Sstevel@tonic-gate 	 */
2370*0Sstevel@tonic-gate 	return (EINPROGRESS);
2371*0Sstevel@tonic-gate bad:
2372*0Sstevel@tonic-gate 	if (v6token_mp != NULL)
2373*0Sstevel@tonic-gate 		freemsg(v6token_mp);
2374*0Sstevel@tonic-gate 	if (v6lla_mp != NULL)
2375*0Sstevel@tonic-gate 		freemsg(v6lla_mp);
2376*0Sstevel@tonic-gate 	if (phys_mp != NULL)
2377*0Sstevel@tonic-gate 		freemsg(phys_mp);
2378*0Sstevel@tonic-gate 	if (info_mp != NULL)
2379*0Sstevel@tonic-gate 		freemsg(info_mp);
2380*0Sstevel@tonic-gate 	if (attach_mp != NULL)
2381*0Sstevel@tonic-gate 		freemsg(attach_mp);
2382*0Sstevel@tonic-gate 	if (detach_mp != NULL)
2383*0Sstevel@tonic-gate 		freemsg(detach_mp);
2384*0Sstevel@tonic-gate 	if (bind_mp != NULL)
2385*0Sstevel@tonic-gate 		freemsg(bind_mp);
2386*0Sstevel@tonic-gate 	if (unbind_mp != NULL)
2387*0Sstevel@tonic-gate 		freemsg(unbind_mp);
2388*0Sstevel@tonic-gate 	if (notify_mp != NULL)
2389*0Sstevel@tonic-gate 		freemsg(notify_mp);
2390*0Sstevel@tonic-gate 	return (ENOMEM);
2391*0Sstevel@tonic-gate }
2392*0Sstevel@tonic-gate 
2393*0Sstevel@tonic-gate uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
2394*0Sstevel@tonic-gate 
2395*0Sstevel@tonic-gate /*
2396*0Sstevel@tonic-gate  * DLPI is up.
2397*0Sstevel@tonic-gate  * Create all the IREs associated with an interface bring up multicast.
2398*0Sstevel@tonic-gate  * Set the interface flag and finish other initialization
2399*0Sstevel@tonic-gate  * that potentially had to be differed to after DL_BIND_ACK.
2400*0Sstevel@tonic-gate  */
2401*0Sstevel@tonic-gate int
2402*0Sstevel@tonic-gate ipif_up_done_v6(ipif_t *ipif)
2403*0Sstevel@tonic-gate {
2404*0Sstevel@tonic-gate 	ire_t	*ire_array[20];
2405*0Sstevel@tonic-gate 	ire_t	**irep = ire_array;
2406*0Sstevel@tonic-gate 	ire_t	**irep1;
2407*0Sstevel@tonic-gate 	ill_t	*ill = ipif->ipif_ill;
2408*0Sstevel@tonic-gate 	queue_t	*stq;
2409*0Sstevel@tonic-gate 	in6_addr_t	v6addr;
2410*0Sstevel@tonic-gate 	in6_addr_t	route_mask;
2411*0Sstevel@tonic-gate 	ipif_t	 *src_ipif = NULL;
2412*0Sstevel@tonic-gate 	ipif_t   *tmp_ipif;
2413*0Sstevel@tonic-gate 	boolean_t	flush_ire_cache = B_TRUE;
2414*0Sstevel@tonic-gate 	int	err;
2415*0Sstevel@tonic-gate 	char	buf[INET6_ADDRSTRLEN];
2416*0Sstevel@tonic-gate 	phyint_t *phyi;
2417*0Sstevel@tonic-gate 	ire_t	**ipif_saved_irep = NULL;
2418*0Sstevel@tonic-gate 	int ipif_saved_ire_cnt;
2419*0Sstevel@tonic-gate 	int cnt;
2420*0Sstevel@tonic-gate 	boolean_t src_ipif_held = B_FALSE;
2421*0Sstevel@tonic-gate 	boolean_t ire_added = B_FALSE;
2422*0Sstevel@tonic-gate 	boolean_t loopback = B_FALSE;
2423*0Sstevel@tonic-gate 	boolean_t ip6_asp_table_held = B_FALSE;
2424*0Sstevel@tonic-gate 
2425*0Sstevel@tonic-gate 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
2426*0Sstevel@tonic-gate 		ipif->ipif_ill->ill_name, ipif->ipif_id));
2427*0Sstevel@tonic-gate 
2428*0Sstevel@tonic-gate 	/* Check if this is a loopback interface */
2429*0Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_wq == NULL)
2430*0Sstevel@tonic-gate 		loopback = B_TRUE;
2431*0Sstevel@tonic-gate 
2432*0Sstevel@tonic-gate 	ASSERT(ipif->ipif_isv6);
2433*0Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2434*0Sstevel@tonic-gate 
2435*0Sstevel@tonic-gate 	/*
2436*0Sstevel@tonic-gate 	 * If all other interfaces for this ill are down or DEPRECATED,
2437*0Sstevel@tonic-gate 	 * or otherwise unsuitable for source address selection, remove
2438*0Sstevel@tonic-gate 	 * any IRE_CACHE entries for this ill to make sure source
2439*0Sstevel@tonic-gate 	 * address selection gets to take this new ipif into account.
2440*0Sstevel@tonic-gate 	 * No need to hold ill_lock while traversing the ipif list since
2441*0Sstevel@tonic-gate 	 * we are writer
2442*0Sstevel@tonic-gate 	 */
2443*0Sstevel@tonic-gate 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
2444*0Sstevel@tonic-gate 		tmp_ipif = tmp_ipif->ipif_next) {
2445*0Sstevel@tonic-gate 		if (((tmp_ipif->ipif_flags &
2446*0Sstevel@tonic-gate 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
2447*0Sstevel@tonic-gate 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
2448*0Sstevel@tonic-gate 		    (tmp_ipif == ipif))
2449*0Sstevel@tonic-gate 			continue;
2450*0Sstevel@tonic-gate 		/* first useable pre-existing interface */
2451*0Sstevel@tonic-gate 		flush_ire_cache = B_FALSE;
2452*0Sstevel@tonic-gate 		break;
2453*0Sstevel@tonic-gate 	}
2454*0Sstevel@tonic-gate 	if (flush_ire_cache)
2455*0Sstevel@tonic-gate 		ire_walk_ill_v6(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
2456*0Sstevel@tonic-gate 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
2457*0Sstevel@tonic-gate 
2458*0Sstevel@tonic-gate 	/*
2459*0Sstevel@tonic-gate 	 * Figure out which way the send-to queue should go.  Only
2460*0Sstevel@tonic-gate 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here.
2461*0Sstevel@tonic-gate 	 */
2462*0Sstevel@tonic-gate 	switch (ill->ill_net_type) {
2463*0Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
2464*0Sstevel@tonic-gate 		stq = ill->ill_rq;
2465*0Sstevel@tonic-gate 		break;
2466*0Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
2467*0Sstevel@tonic-gate 	case IRE_LOOPBACK:
2468*0Sstevel@tonic-gate 		stq = ill->ill_wq;
2469*0Sstevel@tonic-gate 		break;
2470*0Sstevel@tonic-gate 	default:
2471*0Sstevel@tonic-gate 		return (EINVAL);
2472*0Sstevel@tonic-gate 	}
2473*0Sstevel@tonic-gate 
2474*0Sstevel@tonic-gate 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
2475*0Sstevel@tonic-gate 		/*
2476*0Sstevel@tonic-gate 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
2477*0Sstevel@tonic-gate 		 * ipif_lookup_on_name(), but in the case of zones we can have
2478*0Sstevel@tonic-gate 		 * several loopback addresses on lo0. So all the interfaces with
2479*0Sstevel@tonic-gate 		 * loopback addresses need to be marked IRE_LOOPBACK.
2480*0Sstevel@tonic-gate 		 */
2481*0Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
2482*0Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOOPBACK;
2483*0Sstevel@tonic-gate 		else
2484*0Sstevel@tonic-gate 			ipif->ipif_ire_type = IRE_LOCAL;
2485*0Sstevel@tonic-gate 	}
2486*0Sstevel@tonic-gate 
2487*0Sstevel@tonic-gate 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
2488*0Sstevel@tonic-gate 		/*
2489*0Sstevel@tonic-gate 		 * Can't use our source address. Select a different
2490*0Sstevel@tonic-gate 		 * source address for the IRE_INTERFACE and IRE_LOCAL
2491*0Sstevel@tonic-gate 		 */
2492*0Sstevel@tonic-gate 		if (ip6_asp_can_lookup()) {
2493*0Sstevel@tonic-gate 			ip6_asp_table_held = B_TRUE;
2494*0Sstevel@tonic-gate 			src_ipif = ipif_select_source_v6(ipif->ipif_ill,
2495*0Sstevel@tonic-gate 			    &ipif->ipif_v6subnet, B_FALSE,
2496*0Sstevel@tonic-gate 			    IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
2497*0Sstevel@tonic-gate 		}
2498*0Sstevel@tonic-gate 		if (src_ipif == NULL)
2499*0Sstevel@tonic-gate 			src_ipif = ipif;	/* Last resort */
2500*0Sstevel@tonic-gate 		else
2501*0Sstevel@tonic-gate 			src_ipif_held = B_TRUE;
2502*0Sstevel@tonic-gate 	} else {
2503*0Sstevel@tonic-gate 		src_ipif = ipif;
2504*0Sstevel@tonic-gate 	}
2505*0Sstevel@tonic-gate 
2506*0Sstevel@tonic-gate 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
2507*0Sstevel@tonic-gate 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
2508*0Sstevel@tonic-gate 		/* Register the source address for __sin6_src_id */
2509*0Sstevel@tonic-gate 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
2510*0Sstevel@tonic-gate 		    ipif->ipif_zoneid);
2511*0Sstevel@tonic-gate 		if (err != 0) {
2512*0Sstevel@tonic-gate 			ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err));
2513*0Sstevel@tonic-gate 			if (src_ipif_held)
2514*0Sstevel@tonic-gate 				ipif_refrele(src_ipif);
2515*0Sstevel@tonic-gate 			if (ip6_asp_table_held)
2516*0Sstevel@tonic-gate 				ip6_asp_table_refrele();
2517*0Sstevel@tonic-gate 			return (err);
2518*0Sstevel@tonic-gate 		}
2519*0Sstevel@tonic-gate 		/*
2520*0Sstevel@tonic-gate 		 * If the interface address is set, create the LOCAL
2521*0Sstevel@tonic-gate 		 * or LOOPBACK IRE.
2522*0Sstevel@tonic-gate 		 */
2523*0Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n",
2524*0Sstevel@tonic-gate 		    ipif->ipif_ire_type,
2525*0Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
2526*0Sstevel@tonic-gate 		    buf, sizeof (buf))));
2527*0Sstevel@tonic-gate 
2528*0Sstevel@tonic-gate 		*irep++ = ire_create_v6(
2529*0Sstevel@tonic-gate 		    &ipif->ipif_v6lcl_addr,		/* dest address */
2530*0Sstevel@tonic-gate 		    &ipv6_all_ones,			/* mask */
2531*0Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,		/* source address */
2532*0Sstevel@tonic-gate 		    NULL,				/* no gateway */
2533*0Sstevel@tonic-gate 		    &ip_loopback_mtu_v6plus,		/* max frag size */
2534*0Sstevel@tonic-gate 		    NULL,
2535*0Sstevel@tonic-gate 		    ipif->ipif_rq,			/* recv-from queue */
2536*0Sstevel@tonic-gate 		    NULL,				/* no send-to queue */
2537*0Sstevel@tonic-gate 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
2538*0Sstevel@tonic-gate 		    NULL,
2539*0Sstevel@tonic-gate 		    ipif,				/* interface */
2540*0Sstevel@tonic-gate 		    NULL,
2541*0Sstevel@tonic-gate 		    0,
2542*0Sstevel@tonic-gate 		    0,
2543*0Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
2544*0Sstevel@tonic-gate 		    &ire_uinfo_null);
2545*0Sstevel@tonic-gate 	}
2546*0Sstevel@tonic-gate 
2547*0Sstevel@tonic-gate 	/*
2548*0Sstevel@tonic-gate 	 * Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate.
2549*0Sstevel@tonic-gate 	 * Note that atun interfaces have an all-zero ipif_v6subnet.
2550*0Sstevel@tonic-gate 	 * Thus we allow a zero subnet as long as the mask is non-zero.
2551*0Sstevel@tonic-gate 	 */
2552*0Sstevel@tonic-gate 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
2553*0Sstevel@tonic-gate 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
2554*0Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
2555*0Sstevel@tonic-gate 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
2556*0Sstevel@tonic-gate 		v6addr = ipif->ipif_v6subnet;
2557*0Sstevel@tonic-gate 
2558*0Sstevel@tonic-gate 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
2559*0Sstevel@tonic-gate 			route_mask = ipv6_all_ones;
2560*0Sstevel@tonic-gate 		} else {
2561*0Sstevel@tonic-gate 			route_mask = ipif->ipif_v6net_mask;
2562*0Sstevel@tonic-gate 		}
2563*0Sstevel@tonic-gate 
2564*0Sstevel@tonic-gate 		ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n",
2565*0Sstevel@tonic-gate 		    ill->ill_net_type,
2566*0Sstevel@tonic-gate 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
2567*0Sstevel@tonic-gate 
2568*0Sstevel@tonic-gate 		*irep++ = ire_create_v6(
2569*0Sstevel@tonic-gate 		    &v6addr,			/* dest pref */
2570*0Sstevel@tonic-gate 		    &route_mask,		/* mask */
2571*0Sstevel@tonic-gate 		    &src_ipif->ipif_v6src_addr,	/* src addr */
2572*0Sstevel@tonic-gate 		    NULL,			/* no gateway */
2573*0Sstevel@tonic-gate 		    &ipif->ipif_mtu,		/* max frag */
2574*0Sstevel@tonic-gate 		    NULL,			/* no Fast path header */
2575*0Sstevel@tonic-gate 		    NULL,			/* no recv from queue */
2576*0Sstevel@tonic-gate 		    stq,			/* send-to queue */
2577*0Sstevel@tonic-gate 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
2578*0Sstevel@tonic-gate 		    ill->ill_resolver_mp,	/* xmit header */
2579*0Sstevel@tonic-gate 		    ipif,
2580*0Sstevel@tonic-gate 		    NULL,
2581*0Sstevel@tonic-gate 		    0,
2582*0Sstevel@tonic-gate 		    0,
2583*0Sstevel@tonic-gate 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
2584*0Sstevel@tonic-gate 		    &ire_uinfo_null);
2585*0Sstevel@tonic-gate 	}
2586*0Sstevel@tonic-gate 
2587*0Sstevel@tonic-gate 	/*
2588*0Sstevel@tonic-gate 	 * Setup 2002::/16 route, if this interface is a 6to4 tunnel
2589*0Sstevel@tonic-gate 	 */
2590*0Sstevel@tonic-gate 	if (IN6_IS_ADDR_6TO4(&ipif->ipif_v6lcl_addr) &&
2591*0Sstevel@tonic-gate 	    (ill->ill_is_6to4tun)) {
2592*0Sstevel@tonic-gate 		/*
2593*0Sstevel@tonic-gate 		 * Destination address is 2002::/16
2594*0Sstevel@tonic-gate 		 */
2595*0Sstevel@tonic-gate #ifdef	_BIG_ENDIAN
2596*0Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x20020000U, 0, 0, 0 };
2597*0Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0xffff0000U, 0, 0, 0 };
2598*0Sstevel@tonic-gate #else
2599*0Sstevel@tonic-gate 		const in6_addr_t prefix_addr = { 0x00000220U, 0, 0, 0 };
2600*0Sstevel@tonic-gate 		const in6_addr_t prefix_mask = { 0x0000ffffU, 0, 0, 0 };
2601*0Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
2602*0Sstevel@tonic-gate 		char	buf2[INET6_ADDRSTRLEN];
2603*0Sstevel@tonic-gate 		ire_t *isdup;
2604*0Sstevel@tonic-gate 		in6_addr_t *first_addr = &ill->ill_ipif->ipif_v6lcl_addr;
2605*0Sstevel@tonic-gate 
2606*0Sstevel@tonic-gate 		/*
2607*0Sstevel@tonic-gate 		 * check to see if this route has already been added for
2608*0Sstevel@tonic-gate 		 * this tunnel interface.
2609*0Sstevel@tonic-gate 		 */
2610*0Sstevel@tonic-gate 		isdup = ire_ftable_lookup_v6(first_addr, &prefix_mask, 0,
2611*0Sstevel@tonic-gate 		    IRE_IF_NORESOLVER, ill->ill_ipif, NULL, ALL_ZONES, 0,
2612*0Sstevel@tonic-gate 		    (MATCH_IRE_SRC | MATCH_IRE_MASK));
2613*0Sstevel@tonic-gate 
2614*0Sstevel@tonic-gate 		if (isdup == NULL) {
2615*0Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s",
2616*0Sstevel@tonic-gate 			    IRE_IF_NORESOLVER, inet_ntop(AF_INET6, &v6addr,
2617*0Sstevel@tonic-gate 				buf2, sizeof (buf2))));
2618*0Sstevel@tonic-gate 
2619*0Sstevel@tonic-gate 			*irep++ = ire_create_v6(
2620*0Sstevel@tonic-gate 			    &prefix_addr,		/* 2002:: */
2621*0Sstevel@tonic-gate 			    &prefix_mask,		/* ffff:: */
2622*0Sstevel@tonic-gate 			    &ipif->ipif_v6lcl_addr, 	/* src addr */
2623*0Sstevel@tonic-gate 			    NULL, 			/* gateway */
2624*0Sstevel@tonic-gate 			    &ipif->ipif_mtu, 		/* max_frag */
2625*0Sstevel@tonic-gate 			    NULL, 			/* no Fast Path hdr */
2626*0Sstevel@tonic-gate 			    NULL, 			/* no rfq */
2627*0Sstevel@tonic-gate 			    ill->ill_wq, 		/* stq */
2628*0Sstevel@tonic-gate 			    IRE_IF_NORESOLVER,		/* type */
2629*0Sstevel@tonic-gate 			    ill->ill_resolver_mp,	/* dlureq_mp */
2630*0Sstevel@tonic-gate 			    ipif,			/* interface */
2631*0Sstevel@tonic-gate 			    NULL,			/* v6cmask */
2632*0Sstevel@tonic-gate 			    0,
2633*0Sstevel@tonic-gate 			    0,
2634*0Sstevel@tonic-gate 			    RTF_UP,
2635*0Sstevel@tonic-gate 			    &ire_uinfo_null);
2636*0Sstevel@tonic-gate 		} else {
2637*0Sstevel@tonic-gate 			ire_refrele(isdup);
2638*0Sstevel@tonic-gate 		}
2639*0Sstevel@tonic-gate 	}
2640*0Sstevel@tonic-gate 
2641*0Sstevel@tonic-gate 	/* If an earlier ire_create failed, get out now */
2642*0Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
2643*0Sstevel@tonic-gate 		irep1--;
2644*0Sstevel@tonic-gate 		if (*irep1 == NULL) {
2645*0Sstevel@tonic-gate 			ip1dbg(("ipif_up_done_v6: NULL ire found in"
2646*0Sstevel@tonic-gate 			    " ire_array\n"));
2647*0Sstevel@tonic-gate 			err = ENOMEM;
2648*0Sstevel@tonic-gate 			goto bad;
2649*0Sstevel@tonic-gate 		}
2650*0Sstevel@tonic-gate 	}
2651*0Sstevel@tonic-gate 
2652*0Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2653*0Sstevel@tonic-gate 
2654*0Sstevel@tonic-gate 	/*
2655*0Sstevel@tonic-gate 	 * Need to atomically check for ip_addr_availablity_check
2656*0Sstevel@tonic-gate 	 * now under ill_g_lock, and if it fails got bad, and remove
2657*0Sstevel@tonic-gate 	 * from group also
2658*0Sstevel@tonic-gate 	 */
2659*0Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
2660*0Sstevel@tonic-gate 	mutex_enter(&ip_addr_avail_lock);
2661*0Sstevel@tonic-gate 	ill->ill_ipif_up_count++;
2662*0Sstevel@tonic-gate 	ipif->ipif_flags |= IPIF_UP;
2663*0Sstevel@tonic-gate 	err = ip_addr_availability_check(ipif);
2664*0Sstevel@tonic-gate 	mutex_exit(&ip_addr_avail_lock);
2665*0Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
2666*0Sstevel@tonic-gate 
2667*0Sstevel@tonic-gate 	if (err != 0) {
2668*0Sstevel@tonic-gate 		/*
2669*0Sstevel@tonic-gate 		 * Our address may already be up on the same ill. In this case,
2670*0Sstevel@tonic-gate 		 * the external resolver entry for our ipif replaced the one for
2671*0Sstevel@tonic-gate 		 * the other ipif. So we don't want to delete it (otherwise the
2672*0Sstevel@tonic-gate 		 * other ipif would be unable to send packets).
2673*0Sstevel@tonic-gate 		 * ip_addr_availability_check() identifies this case for us and
2674*0Sstevel@tonic-gate 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
2675*0Sstevel@tonic-gate 		 * which is the expected error code.
2676*0Sstevel@tonic-gate 		 */
2677*0Sstevel@tonic-gate 		if (err == EADDRINUSE) {
2678*0Sstevel@tonic-gate 			if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) {
2679*0Sstevel@tonic-gate 				freemsg(ipif->ipif_arp_del_mp);
2680*0Sstevel@tonic-gate 				ipif->ipif_arp_del_mp = NULL;
2681*0Sstevel@tonic-gate 			}
2682*0Sstevel@tonic-gate 			err = EADDRNOTAVAIL;
2683*0Sstevel@tonic-gate 		}
2684*0Sstevel@tonic-gate 		ill->ill_ipif_up_count--;
2685*0Sstevel@tonic-gate 		ipif->ipif_flags &= ~IPIF_UP;
2686*0Sstevel@tonic-gate 		goto bad;
2687*0Sstevel@tonic-gate 	}
2688*0Sstevel@tonic-gate 
2689*0Sstevel@tonic-gate 	/*
2690*0Sstevel@tonic-gate 	 * Add in all newly created IREs. We want to add before
2691*0Sstevel@tonic-gate 	 * we call ifgrp_insert which wants to know whether
2692*0Sstevel@tonic-gate 	 * IRE_IF_RESOLVER exists or not.
2693*0Sstevel@tonic-gate 	 *
2694*0Sstevel@tonic-gate 	 * NOTE : We refrele the ire though we may branch to "bad"
2695*0Sstevel@tonic-gate 	 *	  later on where we do ire_delete. This is okay
2696*0Sstevel@tonic-gate 	 *	  because nobody can delete it as we are running
2697*0Sstevel@tonic-gate 	 *	  exclusively.
2698*0Sstevel@tonic-gate 	 */
2699*0Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
2700*0Sstevel@tonic-gate 		irep1--;
2701*0Sstevel@tonic-gate 		/* Shouldn't be adding any bcast ire's */
2702*0Sstevel@tonic-gate 		ASSERT((*irep1)->ire_type != IRE_BROADCAST);
2703*0Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2704*0Sstevel@tonic-gate 		/*
2705*0Sstevel@tonic-gate 		 * refheld by ire_add. refele towards the end of the func
2706*0Sstevel@tonic-gate 		 */
2707*0Sstevel@tonic-gate 		(void) ire_add(irep1, NULL, NULL, NULL);
2708*0Sstevel@tonic-gate 	}
2709*0Sstevel@tonic-gate 	if (ip6_asp_table_held) {
2710*0Sstevel@tonic-gate 		ip6_asp_table_refrele();
2711*0Sstevel@tonic-gate 		ip6_asp_table_held = B_FALSE;
2712*0Sstevel@tonic-gate 	}
2713*0Sstevel@tonic-gate 	ire_added = B_TRUE;
2714*0Sstevel@tonic-gate 
2715*0Sstevel@tonic-gate 	/*
2716*0Sstevel@tonic-gate 	 * Form groups if possible.
2717*0Sstevel@tonic-gate 	 *
2718*0Sstevel@tonic-gate 	 * If we are supposed to be in a ill_group with a name, insert it
2719*0Sstevel@tonic-gate 	 * now as we know that at least one ipif is UP. Otherwise form
2720*0Sstevel@tonic-gate 	 * nameless groups.
2721*0Sstevel@tonic-gate 	 *
2722*0Sstevel@tonic-gate 	 * If ip_enable_group_ifs is set and ipif address is not ::0, insert
2723*0Sstevel@tonic-gate 	 * this ipif into the appropriate interface group, or create a
2724*0Sstevel@tonic-gate 	 * new one. If this is already in a nameless group, we try to form
2725*0Sstevel@tonic-gate 	 * a bigger group looking at other ills potentially sharing this
2726*0Sstevel@tonic-gate 	 * ipif's prefix.
2727*0Sstevel@tonic-gate 	 */
2728*0Sstevel@tonic-gate 	phyi = ill->ill_phyint;
2729*0Sstevel@tonic-gate 	if (phyi->phyint_groupname_len != 0) {
2730*0Sstevel@tonic-gate 		ASSERT(phyi->phyint_groupname != NULL);
2731*0Sstevel@tonic-gate 		if (ill->ill_ipif_up_count == 1) {
2732*0Sstevel@tonic-gate 			ASSERT(ill->ill_group == NULL);
2733*0Sstevel@tonic-gate 			err = illgrp_insert(&illgrp_head_v6, ill,
2734*0Sstevel@tonic-gate 			    phyi->phyint_groupname, NULL, B_TRUE);
2735*0Sstevel@tonic-gate 			if (err != 0) {
2736*0Sstevel@tonic-gate 				ip1dbg(("ipif_up_done_v6: illgrp allocation "
2737*0Sstevel@tonic-gate 				    "failed, error %d\n", err));
2738*0Sstevel@tonic-gate 				goto bad;
2739*0Sstevel@tonic-gate 			}
2740*0Sstevel@tonic-gate 		}
2741*0Sstevel@tonic-gate 		ASSERT(ill->ill_group != NULL);
2742*0Sstevel@tonic-gate 	}
2743*0Sstevel@tonic-gate 
2744*0Sstevel@tonic-gate 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
2745*0Sstevel@tonic-gate 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
2746*0Sstevel@tonic-gate 	ipif_saved_irep = ipif_recover_ire_v6(ipif);
2747*0Sstevel@tonic-gate 
2748*0Sstevel@tonic-gate 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
2749*0Sstevel@tonic-gate 		/*
2750*0Sstevel@tonic-gate 		 * Need to recover all multicast memberships in the driver.
2751*0Sstevel@tonic-gate 		 * This had to be deferred until we had attached.
2752*0Sstevel@tonic-gate 		 */
2753*0Sstevel@tonic-gate 		ill_recover_multicast(ill);
2754*0Sstevel@tonic-gate 	}
2755*0Sstevel@tonic-gate 	/* Join the allhosts multicast address and the solicited node MC */
2756*0Sstevel@tonic-gate 	ipif_multicast_up(ipif);
2757*0Sstevel@tonic-gate 
2758*0Sstevel@tonic-gate 	if (!loopback) {
2759*0Sstevel@tonic-gate 		/*
2760*0Sstevel@tonic-gate 		 * See whether anybody else would benefit from the
2761*0Sstevel@tonic-gate 		 * new ipif that we added. We call this always rather
2762*0Sstevel@tonic-gate 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
2763*0Sstevel@tonic-gate 		 * ipif for the benefit of illgrp_insert (done above)
2764*0Sstevel@tonic-gate 		 * which does not do source address selection as it does
2765*0Sstevel@tonic-gate 		 * not want to re-create interface routes that we are
2766*0Sstevel@tonic-gate 		 * having reference to it here.
2767*0Sstevel@tonic-gate 		 */
2768*0Sstevel@tonic-gate 		ill_update_source_selection(ill);
2769*0Sstevel@tonic-gate 	}
2770*0Sstevel@tonic-gate 
2771*0Sstevel@tonic-gate 	for (irep1 = irep; irep1 > ire_array; ) {
2772*0Sstevel@tonic-gate 		irep1--;
2773*0Sstevel@tonic-gate 		if (*irep1 != NULL) {
2774*0Sstevel@tonic-gate 			/* was held in ire_add */
2775*0Sstevel@tonic-gate 			ire_refrele(*irep1);
2776*0Sstevel@tonic-gate 		}
2777*0Sstevel@tonic-gate 	}
2778*0Sstevel@tonic-gate 
2779*0Sstevel@tonic-gate 	cnt = ipif_saved_ire_cnt;
2780*0Sstevel@tonic-gate 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
2781*0Sstevel@tonic-gate 		if (*irep1 != NULL) {
2782*0Sstevel@tonic-gate 			/* was held in ire_add */
2783*0Sstevel@tonic-gate 			ire_refrele(*irep1);
2784*0Sstevel@tonic-gate 		}
2785*0Sstevel@tonic-gate 	}
2786*0Sstevel@tonic-gate 
2787*0Sstevel@tonic-gate 
2788*0Sstevel@tonic-gate 	ip_rts_ifmsg(ipif);
2789*0Sstevel@tonic-gate 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
2790*0Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
2791*0Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
2792*0Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
2793*0Sstevel@tonic-gate 	}
2794*0Sstevel@tonic-gate 
2795*0Sstevel@tonic-gate 	if (src_ipif_held)
2796*0Sstevel@tonic-gate 		ipif_refrele(src_ipif);
2797*0Sstevel@tonic-gate 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
2798*0Sstevel@tonic-gate 	return (0);
2799*0Sstevel@tonic-gate 
2800*0Sstevel@tonic-gate bad:
2801*0Sstevel@tonic-gate 	if (ip6_asp_table_held)
2802*0Sstevel@tonic-gate 		ip6_asp_table_refrele();
2803*0Sstevel@tonic-gate 	/*
2804*0Sstevel@tonic-gate 	 * We don't have to bother removing from ill groups because
2805*0Sstevel@tonic-gate 	 *
2806*0Sstevel@tonic-gate 	 * 1) For groups with names, we insert only when the first ipif
2807*0Sstevel@tonic-gate 	 *    comes up. In that case if it fails, it will not be in any
2808*0Sstevel@tonic-gate 	 *    group. So, we need not try to remove for that case.
2809*0Sstevel@tonic-gate 	 *
2810*0Sstevel@tonic-gate 	 * 2) For groups without names, either we tried to insert ipif_ill
2811*0Sstevel@tonic-gate 	 *    in a group as singleton or found some other group to become
2812*0Sstevel@tonic-gate 	 *    a bigger group. For the former, if it fails we don't have
2813*0Sstevel@tonic-gate 	 *    anything to do as ipif_ill is not in the group and for the
2814*0Sstevel@tonic-gate 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
2815*0Sstevel@tonic-gate 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
2816*0Sstevel@tonic-gate 	 */
2817*0Sstevel@tonic-gate 
2818*0Sstevel@tonic-gate 	while (irep > ire_array) {
2819*0Sstevel@tonic-gate 		irep--;
2820*0Sstevel@tonic-gate 		if (*irep != NULL) {
2821*0Sstevel@tonic-gate 			ire_delete(*irep);
2822*0Sstevel@tonic-gate 			if (ire_added)
2823*0Sstevel@tonic-gate 				ire_refrele(*irep);
2824*0Sstevel@tonic-gate 		}
2825*0Sstevel@tonic-gate 
2826*0Sstevel@tonic-gate 	}
2827*0Sstevel@tonic-gate 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
2828*0Sstevel@tonic-gate 
2829*0Sstevel@tonic-gate 	if (ipif_saved_irep != NULL) {
2830*0Sstevel@tonic-gate 		kmem_free(ipif_saved_irep,
2831*0Sstevel@tonic-gate 		    ipif_saved_ire_cnt * sizeof (ire_t *));
2832*0Sstevel@tonic-gate 	}
2833*0Sstevel@tonic-gate 	if (src_ipif_held)
2834*0Sstevel@tonic-gate 		ipif_refrele(src_ipif);
2835*0Sstevel@tonic-gate 
2836*0Sstevel@tonic-gate 	ipif_ndp_down(ipif);
2837*0Sstevel@tonic-gate 	if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
2838*0Sstevel@tonic-gate 		ipif_arp_down(ipif);
2839*0Sstevel@tonic-gate 
2840*0Sstevel@tonic-gate 	return (err);
2841*0Sstevel@tonic-gate }
2842*0Sstevel@tonic-gate 
2843*0Sstevel@tonic-gate /*
2844*0Sstevel@tonic-gate  * Delete an ND entry and the corresponding IRE_CACHE entry if it exists.
2845*0Sstevel@tonic-gate  */
2846*0Sstevel@tonic-gate /* ARGSUSED */
2847*0Sstevel@tonic-gate int
2848*0Sstevel@tonic-gate ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2849*0Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
2850*0Sstevel@tonic-gate {
2851*0Sstevel@tonic-gate 	in6_addr_t	addr;
2852*0Sstevel@tonic-gate 	sin6_t		*sin6;
2853*0Sstevel@tonic-gate 	nce_t		*nce;
2854*0Sstevel@tonic-gate 	struct lifreq	*lifr;
2855*0Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
2856*0Sstevel@tonic-gate 	mblk_t	*mp1;
2857*0Sstevel@tonic-gate 
2858*0Sstevel@tonic-gate 	mp1 = mp->b_cont->b_cont;
2859*0Sstevel@tonic-gate 	lifr = (struct lifreq *)mp1->b_rptr;
2860*0Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
2861*0Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
2862*0Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
2863*0Sstevel@tonic-gate 		return (EINVAL);
2864*0Sstevel@tonic-gate 
2865*0Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
2866*0Sstevel@tonic-gate 		return (EINVAL);
2867*0Sstevel@tonic-gate 
2868*0Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
2869*0Sstevel@tonic-gate 		return (EAFNOSUPPORT);
2870*0Sstevel@tonic-gate 
2871*0Sstevel@tonic-gate 	sin6 = (sin6_t *)&lnr->lnr_addr;
2872*0Sstevel@tonic-gate 	addr = sin6->sin6_addr;
2873*0Sstevel@tonic-gate 	nce = ndp_lookup(ipif->ipif_ill, &addr, B_FALSE);
2874*0Sstevel@tonic-gate 	if (nce == NULL)
2875*0Sstevel@tonic-gate 		return (ESRCH);
2876*0Sstevel@tonic-gate 	ndp_delete(nce);
2877*0Sstevel@tonic-gate 	NCE_REFRELE(nce);
2878*0Sstevel@tonic-gate 	return (0);
2879*0Sstevel@tonic-gate }
2880*0Sstevel@tonic-gate 
2881*0Sstevel@tonic-gate /*
2882*0Sstevel@tonic-gate  * Return nbr cache info.
2883*0Sstevel@tonic-gate  */
2884*0Sstevel@tonic-gate /* ARGSUSED */
2885*0Sstevel@tonic-gate int
2886*0Sstevel@tonic-gate ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2887*0Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
2888*0Sstevel@tonic-gate {
2889*0Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
2890*0Sstevel@tonic-gate 	struct lifreq	*lifr;
2891*0Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
2892*0Sstevel@tonic-gate 
2893*0Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
2894*0Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
2895*0Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
2896*0Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
2897*0Sstevel@tonic-gate 		return (EINVAL);
2898*0Sstevel@tonic-gate 
2899*0Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
2900*0Sstevel@tonic-gate 		return (EINVAL);
2901*0Sstevel@tonic-gate 
2902*0Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
2903*0Sstevel@tonic-gate 		return (EAFNOSUPPORT);
2904*0Sstevel@tonic-gate 
2905*0Sstevel@tonic-gate 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
2906*0Sstevel@tonic-gate 		return (EINVAL);
2907*0Sstevel@tonic-gate 
2908*0Sstevel@tonic-gate 	return (ndp_query(ill, lnr));
2909*0Sstevel@tonic-gate }
2910*0Sstevel@tonic-gate 
2911*0Sstevel@tonic-gate /*
2912*0Sstevel@tonic-gate  * Perform an update of the nd entry for the specified address.
2913*0Sstevel@tonic-gate  */
2914*0Sstevel@tonic-gate /* ARGSUSED */
2915*0Sstevel@tonic-gate int
2916*0Sstevel@tonic-gate ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2917*0Sstevel@tonic-gate     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
2918*0Sstevel@tonic-gate {
2919*0Sstevel@tonic-gate 	ill_t		*ill = ipif->ipif_ill;
2920*0Sstevel@tonic-gate 	struct	lifreq	*lifr;
2921*0Sstevel@tonic-gate 	lif_nd_req_t	*lnr;
2922*0Sstevel@tonic-gate 
2923*0Sstevel@tonic-gate 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
2924*0Sstevel@tonic-gate 	lnr = &lifr->lifr_nd;
2925*0Sstevel@tonic-gate 	/* Only allow for logical unit zero i.e. not on "le0:17" */
2926*0Sstevel@tonic-gate 	if (ipif->ipif_id != 0)
2927*0Sstevel@tonic-gate 		return (EINVAL);
2928*0Sstevel@tonic-gate 
2929*0Sstevel@tonic-gate 	if (!ipif->ipif_isv6)
2930*0Sstevel@tonic-gate 		return (EINVAL);
2931*0Sstevel@tonic-gate 
2932*0Sstevel@tonic-gate 	if (lnr->lnr_addr.ss_family != AF_INET6)
2933*0Sstevel@tonic-gate 		return (EAFNOSUPPORT);
2934*0Sstevel@tonic-gate 
2935*0Sstevel@tonic-gate 	return (ndp_sioc_update(ill, lnr));
2936*0Sstevel@tonic-gate }
2937