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