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