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