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