xref: /titanic_50/usr/src/uts/common/inet/ip/ip6_if.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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 	/*
1059 	 * Though we execute on the ipsq, we need to hold the ill_lock
1060 	 * to prevent readers from seeing partially updated values
1061 	 * while we do the update.
1062 	 */
1063 	mutex_enter(&ill->ill_lock);
1064 	if (!MEDIA_V6INTFID(ill->ill_media, ill->ill_phys_addr_length,
1065 	    ill->ill_phys_addr, &v6addr)) {
1066 		mutex_exit(&ill->ill_lock);
1067 		return (B_FALSE);
1068 	}
1069 
1070 	(void) ip_plen_to_mask_v6(IPV6_TOKEN_LEN, &v6mask);
1071 
1072 	for (i = 0; i < 4; i++)
1073 		v6mask.s6_addr32[i] = v6mask.s6_addr32[i] ^
1074 		    (uint32_t)0xffffffff;
1075 
1076 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
1077 	ill->ill_token_length = IPV6_TOKEN_LEN;
1078 	mutex_exit(&ill->ill_lock);
1079 	return (B_TRUE);
1080 }
1081 
1082 /*
1083  * Create a link-local address from a token.
1084  */
1085 static void
1086 ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
1087 {
1088 	int i;
1089 
1090 	for (i = 0; i < 4; i++) {
1091 		dest->s6_addr32[i] =
1092 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
1093 	}
1094 }
1095 
1096 /*
1097  * Set a nice default address for either automatic tunnels tsrc/96 or
1098  * 6to4 tunnels 2002:<tsrc>::1/64
1099  */
1100 static void
1101 ipif_set_tun_auto_addr(ipif_t *ipif, struct iftun_req *ta)
1102 {
1103 	sin6_t	sin6;
1104 	sin_t	*sin;
1105 	ill_t 	*ill = ipif->ipif_ill;
1106 	tun_t *tp = (tun_t *)ill->ill_wq->q_next->q_ptr;
1107 
1108 	if (ta->ifta_saddr.ss_family != AF_INET ||
1109 	    (ipif->ipif_flags & IPIF_UP) || !ipif->ipif_isv6 ||
1110 	    (ta->ifta_flags & IFTUN_SRC) == 0)
1111 		return;
1112 
1113 	/*
1114 	 * Check the tunnel type by examining q_next->q_ptr
1115 	 */
1116 	if (tp->tun_flags & TUN_AUTOMATIC) {
1117 		/* this is an automatic tunnel */
1118 		(void) ip_plen_to_mask_v6(IPV6_ABITS - IP_ABITS,
1119 		    &ipif->ipif_v6net_mask);
1120 		bzero(&sin6, sizeof (sin6_t));
1121 		sin = (sin_t *)&ta->ifta_saddr;
1122 		V4_PART_OF_V6(sin6.sin6_addr) = sin->sin_addr.s_addr;
1123 		sin6.sin6_family = AF_INET6;
1124 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
1125 		    NULL, NULL, NULL, NULL);
1126 	} else if (tp->tun_flags & TUN_6TO4) {
1127 		/* this is a 6to4 tunnel */
1128 		(void) ip_plen_to_mask_v6(IPV6_PREFIX_LEN,
1129 		    &ipif->ipif_v6net_mask);
1130 		sin = (sin_t *)&ta->ifta_saddr;
1131 		/* create a 6to4 address from the IPv4 tsrc */
1132 		IN6_V4ADDR_TO_6TO4(&sin->sin_addr, &sin6.sin6_addr);
1133 		sin6.sin6_family = AF_INET6;
1134 		(void) ip_sioctl_addr(ipif, (sin_t *)&sin6,
1135 		    NULL, NULL, NULL, NULL);
1136 	} else {
1137 		ip1dbg(("ipif_set_tun_auto_addr: Unknown tunnel type"));
1138 		return;
1139 	}
1140 }
1141 
1142 /*
1143  * Set link local for ipif_id 0 of a configured tunnel based on the
1144  * tsrc or tdst parameter
1145  * For tunnels over IPv4 use the IPv4 address prepended with 32 zeros as
1146  * the token.
1147  * For tunnels over IPv6 use the low-order 64 bits of the "inner" IPv6 address
1148  * as the token for the "outer" link.
1149  */
1150 void
1151 ipif_set_tun_llink(ill_t *ill, struct iftun_req *ta)
1152 {
1153 	ipif_t		*ipif;
1154 	sin_t		*sin;
1155 	in6_addr_t	*s6addr;
1156 
1157 	ASSERT(IAM_WRITER_ILL(ill));
1158 
1159 	/* The first ipif must be id zero. */
1160 	ipif = ill->ill_ipif;
1161 	ASSERT(ipif->ipif_id == 0);
1162 
1163 	/* no link local for automatic tunnels */
1164 	if (!(ipif->ipif_flags & IPIF_POINTOPOINT)) {
1165 		ipif_set_tun_auto_addr(ipif, ta);
1166 		return;
1167 	}
1168 
1169 	if ((ta->ifta_flags & IFTUN_DST) &&
1170 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
1171 		sin6_t	sin6;
1172 
1173 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
1174 		bzero(&sin6, sizeof (sin6_t));
1175 		if ((ta->ifta_saddr.ss_family == AF_INET)) {
1176 			sin = (sin_t *)&ta->ifta_daddr;
1177 			V4_PART_OF_V6(sin6.sin6_addr) =
1178 			    sin->sin_addr.s_addr;
1179 		} else {
1180 			s6addr =
1181 			    &((sin6_t *)&ta->ifta_daddr)->sin6_addr;
1182 			sin6.sin6_addr.s6_addr32[3] = s6addr->s6_addr32[3];
1183 			sin6.sin6_addr.s6_addr32[2] = s6addr->s6_addr32[2];
1184 		}
1185 		ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr,
1186 		    &sin6.sin6_addr);
1187 		ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
1188 	}
1189 	if ((ta->ifta_flags & IFTUN_SRC)) {
1190 		ASSERT(!(ipif->ipif_flags & IPIF_UP));
1191 
1192 		/* Set the token if it isn't already set */
1193 		if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) {
1194 			if ((ta->ifta_saddr.ss_family == AF_INET)) {
1195 				sin = (sin_t *)&ta->ifta_saddr;
1196 				V4_PART_OF_V6(ill->ill_token) =
1197 				    sin->sin_addr.s_addr;
1198 			} else {
1199 				s6addr =
1200 				    &((sin6_t *)&ta->ifta_saddr)->sin6_addr;
1201 				ill->ill_token.s6_addr32[3] =
1202 				    s6addr->s6_addr32[3];
1203 				ill->ill_token.s6_addr32[2] =
1204 				    s6addr->s6_addr32[2];
1205 			}
1206 			ill->ill_token_length = IPV6_TOKEN_LEN;
1207 		}
1208 		/*
1209 		 * Attempt to set the link local address if it isn't set.
1210 		 */
1211 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
1212 			(void) ipif_setlinklocal(ipif);
1213 	}
1214 }
1215 
1216 /*
1217  * Is it not possible to set the link local address?
1218  * The address can be set if the token is set, and the token
1219  * isn't too long.
1220  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
1221  */
1222 boolean_t
1223 ipif_cant_setlinklocal(ipif_t *ipif)
1224 {
1225 	ill_t *ill = ipif->ipif_ill;
1226 
1227 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
1228 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
1229 		return (B_TRUE);
1230 
1231 	return (B_FALSE);
1232 }
1233 
1234 /*
1235  * Generate a link-local address from the token.
1236  * Return zero if the address was set, or non-zero if it couldn't be set.
1237  */
1238 int
1239 ipif_setlinklocal(ipif_t *ipif)
1240 {
1241 	ill_t *ill = ipif->ipif_ill;
1242 
1243 	ASSERT(IAM_WRITER_ILL(ill));
1244 
1245 	if (ipif_cant_setlinklocal(ipif))
1246 		return (-1);
1247 
1248 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
1249 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
1250 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
1251 	    ipif->ipif_v6subnet);
1252 
1253 	if (ipif->ipif_flags & IPIF_NOLOCAL) {
1254 		ipif->ipif_v6src_addr = ipv6_all_zeros;
1255 	} else {
1256 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
1257 	}
1258 	return (0);
1259 }
1260 
1261 /*
1262  * This function sets up the multicast mappings in NDP.
1263  * Unlike ARP, there are no mapping_mps here. We delete the
1264  * mapping nces and add a new one.
1265  *
1266  * Returns non-zero on error and 0 on success.
1267  */
1268 int
1269 ipif_ndp_setup_multicast(ipif_t *ipif, nce_t **ret_nce)
1270 {
1271 	ill_t		*ill = ipif->ipif_ill;
1272 	in6_addr_t	v6_mcast_addr = {(uint32_t)V6_MCAST, 0, 0, 0};
1273 	in6_addr_t	v6_mcast_mask = {(uint32_t)V6_MCAST, 0, 0, 0};
1274 	in6_addr_t	v6_extract_mask;
1275 	uchar_t		*phys_addr, *bphys_addr, *alloc_phys;
1276 	nce_t		*mnce = NULL;
1277 	int		err = 0;
1278 	phyint_t	*phyi = ill->ill_phyint;
1279 	uint32_t	hw_extract_start;
1280 	dl_unitdata_req_t *dlur;
1281 
1282 	if (ret_nce != NULL)
1283 		*ret_nce = NULL;
1284 	/*
1285 	 * Delete the mapping nce. Normally these should not exist
1286 	 * as a previous ipif_down -> ipif_ndp_down should have deleted
1287 	 * all the nces. But they can exist if ip_rput_dlpi_writer
1288 	 * calls this when PHYI_MULTI_BCAST is set.
1289 	 */
1290 	mnce = ndp_lookup_v6(ill, &v6_mcast_addr, B_FALSE);
1291 	if (mnce != NULL) {
1292 		ndp_delete(mnce);
1293 		NCE_REFRELE(mnce);
1294 		mnce = NULL;
1295 	}
1296 
1297 	/*
1298 	 * Get media specific v6 mapping information. Note that
1299 	 * nd_lla_len can be 0 for tunnels.
1300 	 */
1301 	alloc_phys = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
1302 	if ((alloc_phys == NULL) && (ill->ill_nd_lla_len != 0))
1303 		return (ENOMEM);
1304 	/*
1305 	 * Determine the broadcast address.
1306 	 */
1307 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
1308 	if (ill->ill_sap_length < 0)
1309 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
1310 	else
1311 		bphys_addr = (uchar_t *)dlur +
1312 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
1313 
1314 	/*
1315 	 * Check PHYI_MULTI_BCAST and possible length of physical
1316 	 * address to determine if we use the mapping or the
1317 	 * broadcast address.
1318 	 */
1319 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
1320 	    (!MEDIA_V6MINFO(ill->ill_media, ill->ill_nd_lla_len,
1321 	    bphys_addr, alloc_phys, &hw_extract_start,
1322 	    &v6_extract_mask))) {
1323 		if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) {
1324 			kmem_free(alloc_phys, ill->ill_nd_lla_len);
1325 			return (E2BIG);
1326 		}
1327 		/* Use the link-layer broadcast address for MULTI_BCAST */
1328 		phys_addr = bphys_addr;
1329 		bzero(&v6_extract_mask, sizeof (v6_extract_mask));
1330 		hw_extract_start = ill->ill_nd_lla_len;
1331 	} else {
1332 		phys_addr = alloc_phys;
1333 	}
1334 	if ((ipif->ipif_flags & IPIF_BROADCAST) ||
1335 	    (ill->ill_flags & ILLF_MULTICAST) ||
1336 	    (phyi->phyint_flags & PHYI_MULTI_BCAST)) {
1337 		mutex_enter(&ndp6.ndp_g_lock);
1338 		err = ndp_add(ill,
1339 		    phys_addr,
1340 		    &v6_mcast_addr,	/* v6 address */
1341 		    &v6_mcast_mask,	/* v6 mask */
1342 		    &v6_extract_mask,
1343 		    hw_extract_start,
1344 		    NCE_F_MAPPING | NCE_F_PERMANENT | NCE_F_NONUD,
1345 		    ND_REACHABLE,
1346 		    &mnce,
1347 		    NULL,
1348 		    NULL);
1349 		mutex_exit(&ndp6.ndp_g_lock);
1350 		if (err == 0) {
1351 			if (ret_nce != NULL) {
1352 				*ret_nce = mnce;
1353 			} else {
1354 				NCE_REFRELE(mnce);
1355 			}
1356 		}
1357 	}
1358 	kmem_free(alloc_phys, ill->ill_nd_lla_len);
1359 	return (err);
1360 }
1361 
1362 /*
1363  * Get the resolver set up for a new interface address.  (Always called
1364  * as writer.)
1365  */
1366 int
1367 ipif_ndp_up(ipif_t *ipif, const in6_addr_t *addr, boolean_t macaddr_change)
1368 {
1369 	ill_t		*ill = ipif->ipif_ill;
1370 	int		err = 0;
1371 	nce_t		*nce = NULL;
1372 	nce_t		*mnce = NULL;
1373 
1374 	ip1dbg(("ipif_ndp_up(%s:%u)\n",
1375 		ipif->ipif_ill->ill_name, ipif->ipif_id));
1376 
1377 	/*
1378 	 * ND not supported on XRESOLV interfaces. If ND support (multicast)
1379 	 * added later, take out this check.
1380 	 */
1381 	if ((ill->ill_flags & ILLF_XRESOLV) ||
1382 	    IN6_IS_ADDR_UNSPECIFIED(addr) ||
1383 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
1384 		ipif->ipif_addr_ready = 1;
1385 		return (0);
1386 	}
1387 
1388 	/*
1389 	 * Need to setup multicast mapping only when the first
1390 	 * interface is coming UP.
1391 	 */
1392 	if (ill->ill_ipif_up_count == 0 &&
1393 	    (ill->ill_flags & ILLF_MULTICAST)) {
1394 		/*
1395 		 * We set the multicast before setting up the mapping for
1396 		 * local address because ipif_ndp_setup_multicast does
1397 		 * ndp_walk to delete nces which will delete the mapping
1398 		 * for local address also if we added the mapping for
1399 		 * local address first.
1400 		 */
1401 		err = ipif_ndp_setup_multicast(ipif, &mnce);
1402 		if (err != 0)
1403 			return (err);
1404 	}
1405 
1406 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
1407 		uint16_t	flags;
1408 		uchar_t	*hw_addr = NULL;
1409 
1410 		/* Permanent entries don't need NUD */
1411 		flags = NCE_F_PERMANENT;
1412 		flags |= NCE_F_NONUD;
1413 		if (ill->ill_flags & ILLF_ROUTER)
1414 			flags |= NCE_F_ISROUTER;
1415 
1416 		if (ipif->ipif_flags & IPIF_ANYCAST)
1417 			flags |= NCE_F_ANYCAST;
1418 
1419 		if (ill->ill_net_type == IRE_IF_RESOLVER) {
1420 			hw_addr = ill->ill_nd_lla;
1421 
1422 			if (ill->ill_move_in_progress || macaddr_change) {
1423 				/*
1424 				 * Addresses are failing over to this ill.
1425 				 * Don't wait for NUD to see this change.
1426 				 * Publish our new link-layer address.
1427 				 */
1428 				flags |= NCE_F_UNSOL_ADV;
1429 			}
1430 		}
1431 		err = ndp_lookup_then_add(ill,
1432 		    hw_addr,
1433 		    addr,
1434 		    &ipv6_all_ones,
1435 		    &ipv6_all_zeros,
1436 		    0,
1437 		    flags,
1438 		    ND_PROBE,	/* Causes Duplicate Address Detection to run */
1439 		    &nce,
1440 		    NULL,
1441 		    NULL);
1442 		switch (err) {
1443 		case 0:
1444 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
1445 			    ill->ill_name));
1446 			ipif->ipif_addr_ready = 1;
1447 			break;
1448 		case EINPROGRESS:
1449 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
1450 			    ill->ill_name));
1451 			break;
1452 		case EEXIST:
1453 			NCE_REFRELE(nce);
1454 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
1455 			    ill->ill_name));
1456 			if (mnce != NULL) {
1457 				ndp_delete(mnce);
1458 				NCE_REFRELE(mnce);
1459 			}
1460 			return (err);
1461 		default:
1462 			ip1dbg(("ipif_ndp_up: NCE creation failed %s\n",
1463 			    ill->ill_name));
1464 			if (mnce != NULL) {
1465 				ndp_delete(mnce);
1466 				NCE_REFRELE(mnce);
1467 			}
1468 			return (err);
1469 		}
1470 	} else {
1471 		/* No local NCE for this entry */
1472 		ipif->ipif_addr_ready = 1;
1473 	}
1474 	if (nce != NULL)
1475 		NCE_REFRELE(nce);
1476 	if (mnce != NULL)
1477 		NCE_REFRELE(mnce);
1478 	return (0);
1479 }
1480 
1481 /* Remove all cache entries for this logical interface */
1482 void
1483 ipif_ndp_down(ipif_t *ipif)
1484 {
1485 	nce_t	*nce;
1486 
1487 	if (ipif->ipif_isv6) {
1488 		nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr,
1489 		    B_FALSE);
1490 		if (nce != NULL) {
1491 			ndp_delete(nce);
1492 			NCE_REFRELE(nce);
1493 		}
1494 	}
1495 	/*
1496 	 * Remove mapping and all other nces dependent on this ill
1497 	 * when the last ipif is going away.
1498 	 */
1499 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
1500 		ndp_walk(ipif->ipif_ill, (pfi_t)ndp_delete_per_ill,
1501 		    (uchar_t *)ipif->ipif_ill);
1502 	}
1503 }
1504 
1505 /*
1506  * Used when an interface comes up to recreate any extra routes on this
1507  * interface.
1508  */
1509 static ire_t **
1510 ipif_recover_ire_v6(ipif_t *ipif)
1511 {
1512 	mblk_t	*mp;
1513 	ire_t   **ipif_saved_irep;
1514 	ire_t   **irep;
1515 
1516 	ip1dbg(("ipif_recover_ire_v6(%s:%u)", ipif->ipif_ill->ill_name,
1517 	    ipif->ipif_id));
1518 
1519 	ASSERT(ipif->ipif_isv6);
1520 
1521 	mutex_enter(&ipif->ipif_saved_ire_lock);
1522 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
1523 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
1524 	if (ipif_saved_irep == NULL) {
1525 		mutex_exit(&ipif->ipif_saved_ire_lock);
1526 		return (NULL);
1527 	}
1528 
1529 	irep = ipif_saved_irep;
1530 
1531 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
1532 		ire_t		*ire;
1533 		queue_t		*rfq;
1534 		queue_t		*stq;
1535 		ifrt_t		*ifrt;
1536 		in6_addr_t	*src_addr;
1537 		in6_addr_t	*gateway_addr;
1538 		mblk_t		*resolver_mp;
1539 		char		buf[INET6_ADDRSTRLEN];
1540 		ushort_t	type;
1541 
1542 		/*
1543 		 * When the ire was initially created and then added in
1544 		 * ip_rt_add_v6(), it was created either using
1545 		 * ipif->ipif_net_type in the case of a traditional interface
1546 		 * route, or as one of the IRE_OFFSUBNET types (with the
1547 		 * exception of IRE_HOST type redirect ire which is created by
1548 		 * icmp_redirect_v6() and which we don't need to save or
1549 		 * recover).  In the case where ipif->ipif_net_type was
1550 		 * IRE_LOOPBACK, ip_rt_add_v6() will update the ire_type to
1551 		 * IRE_IF_NORESOLVER before calling ire_add_v6() to satisfy
1552 		 * software like GateD and Sun Cluster which creates routes
1553 		 * using the the loopback interface's address as a gateway.
1554 		 *
1555 		 * As ifrt->ifrt_type reflects the already updated ire_type and
1556 		 * since ire_create_v6() expects that IRE_IF_NORESOLVER will
1557 		 * have a valid ire_dlureq_mp field (which doesn't make sense
1558 		 * for a IRE_LOOPBACK), ire_create_v6() will be called in the
1559 		 * same way here as in ip_rt_add_v6(), namely using
1560 		 * ipif->ipif_net_type when the route looks like a traditional
1561 		 * interface route (where ifrt->ifrt_type & IRE_INTERFACE is
1562 		 * true) and otherwise using the saved ifrt->ifrt_type.  This
1563 		 * means that in the case where ipif->ipif_net_type is
1564 		 * IRE_LOOPBACK, the ire created by ire_create_v6() will be an
1565 		 * IRE_LOOPBACK, it will then be turned into an
1566 		 * IRE_IF_NORESOLVER and then added by ire_add_v6().
1567 		 */
1568 		ifrt = (ifrt_t *)mp->b_rptr;
1569 		if (ifrt->ifrt_type & IRE_INTERFACE) {
1570 			rfq = NULL;
1571 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
1572 			    ? ipif->ipif_rq : ipif->ipif_wq;
1573 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
1574 			    ? &ifrt->ifrt_v6src_addr
1575 			    : &ipif->ipif_v6src_addr;
1576 			gateway_addr = NULL;
1577 			resolver_mp = ipif->ipif_resolver_mp;
1578 			type = ipif->ipif_net_type;
1579 		} else {
1580 			rfq = NULL;
1581 			stq = NULL;
1582 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
1583 			    ? &ifrt->ifrt_v6src_addr : NULL;
1584 			gateway_addr = &ifrt->ifrt_v6gateway_addr;
1585 			resolver_mp = NULL;
1586 			type = ifrt->ifrt_type;
1587 		}
1588 
1589 		/*
1590 		 * Create a copy of the IRE with the saved address and netmask.
1591 		 */
1592 		ip1dbg(("ipif_recover_ire_v6: creating IRE %s (%d) for %s/%d\n",
1593 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
1594 		    inet_ntop(AF_INET6, &ifrt->ifrt_v6addr, buf, sizeof (buf)),
1595 		    ip_mask_to_plen_v6(&ifrt->ifrt_v6mask)));
1596 		ire = ire_create_v6(
1597 		    &ifrt->ifrt_v6addr,
1598 		    &ifrt->ifrt_v6mask,
1599 		    src_addr,
1600 		    gateway_addr,
1601 		    &ifrt->ifrt_max_frag,
1602 		    NULL,
1603 		    rfq,
1604 		    stq,
1605 		    type,
1606 		    resolver_mp,
1607 		    ipif,
1608 		    NULL,
1609 		    0,
1610 		    0,
1611 		    ifrt->ifrt_flags,
1612 		    &ifrt->ifrt_iulp_info,
1613 		    NULL,
1614 		    NULL);
1615 		if (ire == NULL) {
1616 			mutex_exit(&ipif->ipif_saved_ire_lock);
1617 			kmem_free(ipif_saved_irep,
1618 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
1619 			return (NULL);
1620 		}
1621 
1622 		/*
1623 		 * Some software (for example, GateD and Sun Cluster) attempts
1624 		 * to create (what amount to) IRE_PREFIX routes with the
1625 		 * loopback address as the gateway.  This is primarily done to
1626 		 * set up prefixes with the RTF_REJECT flag set (for example,
1627 		 * when generating aggregate routes.)
1628 		 *
1629 		 * If the IRE type (as defined by ipif->ipif_net_type) is
1630 		 * IRE_LOOPBACK, then we map the request into a
1631 		 * IRE_IF_NORESOLVER.
1632 		 */
1633 		if (ipif->ipif_net_type == IRE_LOOPBACK)
1634 			ire->ire_type = IRE_IF_NORESOLVER;
1635 		/*
1636 		 * ire held by ire_add, will be refreled' in ipif_up_done
1637 		 * towards the end
1638 		 */
1639 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
1640 		*irep = ire;
1641 		irep++;
1642 		ip1dbg(("ipif_recover_ire_v6: added ire %p\n", (void *)ire));
1643 	}
1644 	mutex_exit(&ipif->ipif_saved_ire_lock);
1645 	return (ipif_saved_irep);
1646 }
1647 
1648 /*
1649  * Return the scope of the given IPv6 address.  If the address is an
1650  * IPv4 mapped IPv6 address, return the scope of the corresponding
1651  * IPv4 address.
1652  */
1653 in6addr_scope_t
1654 ip_addr_scope_v6(const in6_addr_t *addr)
1655 {
1656 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
1657 
1658 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
1659 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
1660 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
1661 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
1662 			return (IP6_SCOPE_LINKLOCAL);
1663 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
1664 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
1665 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
1666 			return (IP6_SCOPE_SITELOCAL);
1667 		return (IP6_SCOPE_GLOBAL);
1668 	}
1669 
1670 	if (IN6_IS_ADDR_MULTICAST(addr))
1671 		return (IN6_ADDR_MC_SCOPE(addr));
1672 
1673 	/* link-local and loopback addresses are of link-local scope */
1674 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
1675 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
1676 		return (IP6_SCOPE_LINKLOCAL);
1677 	if (IN6_IS_ADDR_SITELOCAL(addr))
1678 		return (IP6_SCOPE_SITELOCAL);
1679 	return (IP6_SCOPE_GLOBAL);
1680 }
1681 
1682 
1683 /*
1684  * Calculates the xor of a1 and a2, and stores the result in res.
1685  */
1686 static void
1687 ip_addr_xor_v6(const in6_addr_t *a1, const in6_addr_t *a2, in6_addr_t *res)
1688 {
1689 	int i;
1690 
1691 	for (i = 0; i < 4; i++)
1692 		res->s6_addr32[i] = a1->s6_addr32[i] ^ a2->s6_addr32[i];
1693 }
1694 
1695 #define	IPIF_VALID_IPV6_SOURCE(ipif) \
1696 	(((ipif)->ipif_flags & IPIF_UP) && \
1697 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
1698 	(ipif)->ipif_addr_ready)
1699 
1700 /* source address candidate */
1701 typedef struct candidate {
1702 	ipif_t		*cand_ipif;
1703 	/* The properties of this candidate */
1704 	boolean_t	cand_isdst;
1705 	boolean_t	cand_isdst_set;
1706 	in6addr_scope_t	cand_scope;
1707 	boolean_t	cand_scope_set;
1708 	boolean_t	cand_isdeprecated;
1709 	boolean_t	cand_isdeprecated_set;
1710 	boolean_t	cand_ispreferred;
1711 	boolean_t	cand_ispreferred_set;
1712 	boolean_t	cand_matchedinterface;
1713 	boolean_t	cand_matchedinterface_set;
1714 	boolean_t	cand_matchedlabel;
1715 	boolean_t	cand_matchedlabel_set;
1716 	boolean_t	cand_istmp;
1717 	boolean_t	cand_istmp_set;
1718 	in6_addr_t	cand_xor;
1719 	boolean_t	cand_xor_set;
1720 } cand_t;
1721 #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
1722 #define	cand_flags	cand_ipif->ipif_flags
1723 #define	cand_ill	cand_ipif->ipif_ill
1724 #define	cand_zoneid	cand_ipif->ipif_zoneid
1725 
1726 /* information about the destination for source address selection */
1727 typedef struct dstinfo {
1728 	const in6_addr_t	*dst_addr;
1729 	ill_t			*dst_ill;
1730 	uint_t			dst_restrict_ill;
1731 	boolean_t		dst_prefer_src_tmp;
1732 	in6addr_scope_t		dst_scope;
1733 	char			*dst_label;
1734 } dstinfo_t;
1735 
1736 /*
1737  * The following functions are rules used to select a source address in
1738  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
1739  * against the best candidate (bc).  Each rule has three possible outcomes;
1740  * the candidate is preferred over the best candidate (CAND_PREFER), the
1741  * candidate is not preferred over the best candidate (CAND_AVOID), or the
1742  * candidate is of equal value as the best candidate (CAND_TIE).
1743  *
1744  * These rules are part of a greater "Default Address Selection for IPv6"
1745  * sheme, which is standards based work coming out of the IETF ipv6 working
1746  * group.  The IETF document defines both IPv6 source address selection and
1747  * destination address ordering.  The rules defined here implement the IPv6
1748  * source address selection.  Destination address ordering is done by
1749  * libnsl, and uses a similar set of rules to implement the sorting.
1750  */
1751 typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
1752 typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *);
1753 
1754 /* Prefer an address if it is equal to the destination address. */
1755 static rule_res_t
1756 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1757 {
1758 	if (!bc->cand_isdst_set) {
1759 		bc->cand_isdst =
1760 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
1761 		bc->cand_isdst_set = B_TRUE;
1762 	}
1763 
1764 	cc->cand_isdst =
1765 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
1766 	cc->cand_isdst_set = B_TRUE;
1767 
1768 	if (cc->cand_isdst == bc->cand_isdst)
1769 		return (CAND_TIE);
1770 	else if (cc->cand_isdst)
1771 		return (CAND_PREFER);
1772 	else
1773 		return (CAND_AVOID);
1774 }
1775 
1776 /*
1777  * Prefer addresses that are of closest scope to the destination.  Always
1778  * prefer addresses that are of greater scope than the destination over
1779  * those that are of lesser scope than the destination.
1780  */
1781 static rule_res_t
1782 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1783 {
1784 	if (!bc->cand_scope_set) {
1785 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
1786 		bc->cand_scope_set = B_TRUE;
1787 	}
1788 
1789 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
1790 	cc->cand_scope_set = B_TRUE;
1791 
1792 	if (cc->cand_scope < bc->cand_scope) {
1793 		if (cc->cand_scope < dstinfo->dst_scope)
1794 			return (CAND_AVOID);
1795 		else
1796 			return (CAND_PREFER);
1797 	} else if (bc->cand_scope < cc->cand_scope) {
1798 		if (bc->cand_scope < dstinfo->dst_scope)
1799 			return (CAND_PREFER);
1800 		else
1801 			return (CAND_AVOID);
1802 	} else {
1803 		return (CAND_TIE);
1804 	}
1805 }
1806 
1807 /*
1808  * Prefer non-deprecated source addresses.
1809  */
1810 /* ARGSUSED2 */
1811 static rule_res_t
1812 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1813 {
1814 	if (!bc->cand_isdeprecated_set) {
1815 		bc->cand_isdeprecated =
1816 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
1817 		bc->cand_isdeprecated_set = B_TRUE;
1818 	}
1819 
1820 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
1821 	cc->cand_isdeprecated_set = B_TRUE;
1822 
1823 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
1824 		return (CAND_TIE);
1825 	else if (cc->cand_isdeprecated)
1826 		return (CAND_AVOID);
1827 	else
1828 		return (CAND_PREFER);
1829 }
1830 
1831 /*
1832  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
1833  * rule must be before rule_interface because the flag could be set on any
1834  * interface, not just the interface being used for outgoing packets (for
1835  * example, the IFF_PREFERRED could be set on an address assigned to the
1836  * loopback interface).
1837  */
1838 /* ARGSUSED2 */
1839 static rule_res_t
1840 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1841 {
1842 	if (!bc->cand_ispreferred_set) {
1843 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
1844 		bc->cand_ispreferred_set = B_TRUE;
1845 	}
1846 
1847 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
1848 	cc->cand_ispreferred_set = B_TRUE;
1849 
1850 	if (bc->cand_ispreferred == cc->cand_ispreferred)
1851 		return (CAND_TIE);
1852 	else if (cc->cand_ispreferred)
1853 		return (CAND_PREFER);
1854 	else
1855 		return (CAND_AVOID);
1856 }
1857 
1858 /*
1859  * Prefer source addresses that are assigned to the outgoing interface, or
1860  * to an interface that is in the same IPMP group as the outgoing
1861  * interface.
1862  */
1863 static rule_res_t
1864 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1865 {
1866 	ill_t *dstill = dstinfo->dst_ill;
1867 
1868 	/*
1869 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
1870 	 * since we know all candidates will be on the same link.
1871 	 */
1872 	if (dstinfo->dst_restrict_ill)
1873 		return (CAND_TIE);
1874 
1875 	if (!bc->cand_matchedinterface_set) {
1876 		bc->cand_matchedinterface = (bc->cand_ill == dstill ||
1877 		    (dstill->ill_group != NULL &&
1878 		    dstill->ill_group == bc->cand_ill->ill_group));
1879 		bc->cand_matchedinterface_set = B_TRUE;
1880 	}
1881 
1882 	cc->cand_matchedinterface = (cc->cand_ill == dstill ||
1883 	    (dstill->ill_group != NULL &&
1884 		dstill->ill_group == cc->cand_ill->ill_group));
1885 	cc->cand_matchedinterface_set = B_TRUE;
1886 
1887 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
1888 		return (CAND_TIE);
1889 	else if (cc->cand_matchedinterface)
1890 		return (CAND_PREFER);
1891 	else
1892 		return (CAND_AVOID);
1893 }
1894 
1895 /*
1896  * Prefer source addresses whose label matches the destination's label.
1897  */
1898 static rule_res_t
1899 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1900 {
1901 	char *label;
1902 
1903 	if (!bc->cand_matchedlabel_set) {
1904 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL);
1905 		bc->cand_matchedlabel =
1906 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
1907 		bc->cand_matchedlabel_set = B_TRUE;
1908 	}
1909 
1910 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL);
1911 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
1912 	cc->cand_matchedlabel_set = B_TRUE;
1913 
1914 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
1915 		return (CAND_TIE);
1916 	else if (cc->cand_matchedlabel)
1917 		return (CAND_PREFER);
1918 	else
1919 		return (CAND_AVOID);
1920 }
1921 
1922 /*
1923  * Prefer public addresses over temporary ones.  An application can reverse
1924  * the logic of this rule and prefer temporary addresses by using the
1925  * IPV6_SRC_PREFERENCES socket option.
1926  */
1927 static rule_res_t
1928 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1929 {
1930 	if (!bc->cand_istmp_set) {
1931 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
1932 		bc->cand_istmp_set = B_TRUE;
1933 	}
1934 
1935 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
1936 	cc->cand_istmp_set = B_TRUE;
1937 
1938 	if (bc->cand_istmp == cc->cand_istmp)
1939 		return (CAND_TIE);
1940 
1941 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
1942 		return (CAND_PREFER);
1943 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
1944 		return (CAND_PREFER);
1945 	else
1946 		return (CAND_AVOID);
1947 }
1948 
1949 /*
1950  * Prefer source addresses with longer matching prefix with the
1951  * destination.  Since this is the last rule, it must not produce a tie.
1952  * We do the longest matching prefix calculation and the tie break in one
1953  * calculation by doing an xor of both addresses with the destination, and
1954  * pick the address with the smallest xor value.  That way, we're both
1955  * picking the address with the longest matching prefix, and breaking the
1956  * tie if they happen to have both have equal mathing prefixes.
1957  */
1958 static rule_res_t
1959 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1960 {
1961 	int i;
1962 
1963 	if (!bc->cand_xor_set) {
1964 		ip_addr_xor_v6(&bc->cand_srcaddr,
1965 		    dstinfo->dst_addr, &bc->cand_xor);
1966 		bc->cand_xor_set = B_TRUE;
1967 	}
1968 
1969 	ip_addr_xor_v6(&cc->cand_srcaddr, dstinfo->dst_addr, &cc->cand_xor);
1970 	cc->cand_xor_set = B_TRUE;
1971 
1972 	for (i = 0; i < 4; i++) {
1973 		if (bc->cand_xor.s6_addr32[i] != cc->cand_xor.s6_addr32[i])
1974 			break;
1975 	}
1976 
1977 	if (cc->cand_xor.s6_addr32[i] < bc->cand_xor.s6_addr32[i])
1978 		return (CAND_PREFER);
1979 	else
1980 		return (CAND_AVOID);
1981 }
1982 
1983 /*
1984  * Prefer to use zone-specific addresses when possible instead of all-zones
1985  * addresses.
1986  */
1987 /* ARGSUSED2 */
1988 static rule_res_t
1989 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo)
1990 {
1991 	if ((bc->cand_zoneid == ALL_ZONES) ==
1992 	    (cc->cand_zoneid == ALL_ZONES))
1993 		return (CAND_TIE);
1994 	else if (cc->cand_zoneid == ALL_ZONES)
1995 		return (CAND_AVOID);
1996 	else
1997 		return (CAND_PREFER);
1998 }
1999 
2000 /*
2001  * Determine the best source address given a destination address and a
2002  * destination ill.  If no suitable source address is found, it returns
2003  * NULL. If there is a usable address pointed to by the usesrc
2004  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
2005  * fine grained (i.e per interface)
2006  *
2007  * This implementation is based on the "Default Address Selection for IPv6"
2008  * specification produced by the IETF IPv6 working group.  It has been
2009  * implemented so that the list of addresses is only traversed once (the
2010  * specification's algorithm could traverse the list of addresses once for
2011  * every rule).
2012  *
2013  * The restrict_ill argument restricts the algorithm to chose a source
2014  * address that is assigned to the destination ill or an ill in the same
2015  * IPMP group as the destination ill.  This is used when the destination
2016  * address is a link-local or multicast address, and when
2017  * ipv6_strict_dst_multihoming is turned on.
2018  *
2019  * src_prefs is the caller's set of source address preferences.  If source
2020  * address selection is being called to determine the source address of a
2021  * connected socket (from ip_bind_connected_v6()), then the preferences are
2022  * taken from conn_src_preferences.  These preferences can be set on a
2023  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
2024  * preference currently implemented is for rfc3041 temporary addresses.
2025  */
2026 ipif_t *
2027 ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
2028     uint_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid)
2029 {
2030 	dstinfo_t	dstinfo;
2031 	char		dstr[INET6_ADDRSTRLEN];
2032 	char		sstr[INET6_ADDRSTRLEN];
2033 	ipif_t		*ipif;
2034 	ill_t		*ill, *usesrc_ill = NULL;
2035 	ill_walk_context_t	ctx;
2036 	cand_t		best_c;	/* The best candidate */
2037 	cand_t		curr_c;	/* The current candidate */
2038 	uint_t		index;
2039 	boolean_t	first_candidate = B_TRUE;
2040 	rule_res_t	rule_result;
2041 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
2042 
2043 	/*
2044 	 * The list of ordering rules.  They are applied in the order they
2045 	 * appear in the list.
2046 	 *
2047 	 * XXX rule_mipv6 will need to be implemented (the specification's
2048 	 * rules 4) if a mobile IPv6 node is ever implemented.
2049 	 */
2050 	rulef_t	rules[] = {
2051 		rule_isdst,
2052 		rule_scope,
2053 		rule_deprecated,
2054 		rule_preferred,
2055 		rule_interface,
2056 		rule_label,
2057 		rule_temporary,
2058 		rule_prefix,
2059 		rule_zone_specific,
2060 		NULL
2061 	};
2062 
2063 	ASSERT(dstill->ill_isv6);
2064 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
2065 
2066 	/*
2067 	 * Check if there is a usable src address pointed to by the
2068 	 * usesrc ifindex. This has higher precedence since it is
2069 	 * finer grained (i.e per interface) v/s being system wide.
2070 	 */
2071 	if (dstill->ill_usesrc_ifindex != 0) {
2072 		if ((usesrc_ill =
2073 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
2074 		    NULL, NULL, NULL, NULL)) != NULL) {
2075 			dstinfo.dst_ill = usesrc_ill;
2076 		} else {
2077 			return (NULL);
2078 		}
2079 	} else {
2080 		dstinfo.dst_ill = dstill;
2081 	}
2082 
2083 	/*
2084 	 * If we're dealing with an unlabeled destination on a labeled system,
2085 	 * make sure that we ignore source addresses that are incompatible with
2086 	 * the destination's default label.  That destination's default label
2087 	 * must dominate the minimum label on the source address.
2088 	 *
2089 	 * (Note that this has to do with Trusted Solaris.  It's not related to
2090 	 * the labels described by ip6_asp_lookup.)
2091 	 */
2092 	dst_rhtp = NULL;
2093 	if (is_system_labeled()) {
2094 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
2095 		if (dst_rhtp == NULL)
2096 			return (NULL);
2097 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
2098 			TPC_RELE(dst_rhtp);
2099 			dst_rhtp = NULL;
2100 		}
2101 	}
2102 
2103 	dstinfo.dst_addr = dst;
2104 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
2105 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL);
2106 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
2107 
2108 	rw_enter(&ill_g_lock, RW_READER);
2109 	/*
2110 	 * Section three of the I-D states that for multicast and
2111 	 * link-local destinations, the candidate set must be restricted to
2112 	 * an interface that is on the same link as the outgoing interface.
2113 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
2114 	 * restrict the source address to the destination link as doing
2115 	 * otherwise will almost certainly cause problems.
2116 	 */
2117 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
2118 	    ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
2119 		if (restrict_ill == RESTRICT_TO_NONE)
2120 			dstinfo.dst_restrict_ill = RESTRICT_TO_GROUP;
2121 		else
2122 			dstinfo.dst_restrict_ill = restrict_ill;
2123 	} else {
2124 		dstinfo.dst_restrict_ill = restrict_ill;
2125 	}
2126 
2127 	bzero(&best_c, sizeof (cand_t));
2128 
2129 	/*
2130 	 * Take a pass through the list of IPv6 interfaces to chose the
2131 	 * best possible source address.  If restrict_ill is true, we only
2132 	 * iterate through the ill's that are in the same IPMP group as the
2133 	 * destination's outgoing ill.  If restrict_ill is false, we walk
2134 	 * the entire list of IPv6 ill's.
2135 	 */
2136 	if (dstinfo.dst_restrict_ill != RESTRICT_TO_NONE) {
2137 		if (dstinfo.dst_ill->ill_group != NULL &&
2138 		    dstinfo.dst_restrict_ill == RESTRICT_TO_GROUP) {
2139 			ill = dstinfo.dst_ill->ill_group->illgrp_ill;
2140 		} else {
2141 			ill = dstinfo.dst_ill;
2142 		}
2143 	} else {
2144 		ill = ILL_START_WALK_V6(&ctx);
2145 	}
2146 
2147 	while (ill != NULL) {
2148 		ASSERT(ill->ill_isv6);
2149 
2150 		/*
2151 		 * Avoid FAILED/OFFLINE ills.
2152 		 * Global and site local addresses will failover and
2153 		 * will be available on the new ill.
2154 		 * But link local addresses don't move.
2155 		 */
2156 		if (dstinfo.dst_restrict_ill != RESTRICT_TO_ILL &&
2157 		    ill->ill_phyint->phyint_flags &
2158 		    (PHYI_OFFLINE | PHYI_FAILED))
2159 			goto next_ill;
2160 
2161 		for (ipif = ill->ill_ipif; ipif != NULL;
2162 		    ipif = ipif->ipif_next) {
2163 
2164 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
2165 				continue;
2166 
2167 			if (zoneid != ALL_ZONES &&
2168 			    ipif->ipif_zoneid != zoneid &&
2169 			    ipif->ipif_zoneid != ALL_ZONES)
2170 				continue;
2171 
2172 			/*
2173 			 * Check compatibility of local address for
2174 			 * destination's default label if we're on a labeled
2175 			 * system.  Incompatible addresses can't be used at
2176 			 * all and must be skipped over.
2177 			 */
2178 			if (dst_rhtp != NULL) {
2179 				boolean_t incompat;
2180 
2181 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
2182 				    IPV6_VERSION, B_FALSE);
2183 				if (src_rhtp == NULL)
2184 					continue;
2185 				incompat =
2186 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
2187 				    src_rhtp->tpc_tp.tp_doi !=
2188 				    dst_rhtp->tpc_tp.tp_doi ||
2189 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
2190 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
2191 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
2192 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
2193 				TPC_RELE(src_rhtp);
2194 				if (incompat)
2195 					continue;
2196 			}
2197 
2198 			if (first_candidate) {
2199 				/*
2200 				 * This is first valid address in the list.
2201 				 * It is automatically the best candidate
2202 				 * so far.
2203 				 */
2204 				best_c.cand_ipif = ipif;
2205 				first_candidate = B_FALSE;
2206 				continue;
2207 			}
2208 
2209 			bzero(&curr_c, sizeof (cand_t));
2210 			curr_c.cand_ipif = ipif;
2211 
2212 			/*
2213 			 * Compare this current candidate (curr_c) with the
2214 			 * best candidate (best_c) by applying the
2215 			 * comparison rules in order until one breaks the
2216 			 * tie.
2217 			 */
2218 			for (index = 0; rules[index] != NULL; index++) {
2219 				/* Apply a comparison rule. */
2220 				rule_result =
2221 				    (rules[index])(&best_c, &curr_c, &dstinfo);
2222 				if (rule_result == CAND_AVOID) {
2223 					/*
2224 					 * The best candidate is still the
2225 					 * best candidate.  Forget about
2226 					 * this current candidate and go on
2227 					 * to the next one.
2228 					 */
2229 					break;
2230 				} else if (rule_result == CAND_PREFER) {
2231 					/*
2232 					 * This candidate is prefered.  It
2233 					 * becomes the best candidate so
2234 					 * far.  Go on to the next address.
2235 					 */
2236 					best_c = curr_c;
2237 					break;
2238 				}
2239 				/* We have a tie, apply the next rule. */
2240 			}
2241 
2242 			/*
2243 			 * The last rule must be a tie breaker rule and
2244 			 * must never produce a tie.  At this point, the
2245 			 * candidate should have either been rejected, or
2246 			 * have been prefered as the best candidate so far.
2247 			 */
2248 			ASSERT(rule_result != CAND_TIE);
2249 		}
2250 
2251 		/*
2252 		 * We may be walking the linked-list of ill's in an
2253 		 * IPMP group or traversing the IPv6 ill avl tree. If it is a
2254 		 * usesrc ILL then it can't be part of IPMP group and we
2255 		 * will exit the while loop.
2256 		 */
2257 next_ill:
2258 		if (dstinfo.dst_restrict_ill == RESTRICT_TO_ILL)
2259 			ill = NULL;
2260 		else if (dstinfo.dst_restrict_ill == RESTRICT_TO_GROUP)
2261 			ill = ill->ill_group_next;
2262 		else
2263 			ill = ill_next(&ctx, ill);
2264 	}
2265 
2266 	ipif = best_c.cand_ipif;
2267 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
2268 	    dstinfo.dst_ill->ill_name,
2269 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
2270 	    (ipif == NULL ? "NULL" :
2271 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
2272 
2273 	if (usesrc_ill != NULL)
2274 		ill_refrele(usesrc_ill);
2275 
2276 	if (dst_rhtp != NULL)
2277 		TPC_RELE(dst_rhtp);
2278 
2279 	if (ipif == NULL) {
2280 		rw_exit(&ill_g_lock);
2281 		return (NULL);
2282 	}
2283 
2284 	mutex_enter(&ipif->ipif_ill->ill_lock);
2285 	if (IPIF_CAN_LOOKUP(ipif)) {
2286 		ipif_refhold_locked(ipif);
2287 		mutex_exit(&ipif->ipif_ill->ill_lock);
2288 		rw_exit(&ill_g_lock);
2289 		return (ipif);
2290 	}
2291 	mutex_exit(&ipif->ipif_ill->ill_lock);
2292 	rw_exit(&ill_g_lock);
2293 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
2294 	    " returning null \n", (void *)ipif));
2295 
2296 	return (NULL);
2297 }
2298 
2299 /*
2300  * If old_ipif is not NULL, see if ipif was derived from old
2301  * ipif and if so, recreate the interface route by re-doing
2302  * source address selection. This happens when ipif_down ->
2303  * ipif_update_other_ipifs calls us.
2304  *
2305  * If old_ipif is NULL, just redo the source address selection
2306  * if needed. This happens when illgrp_insert or ipif_up_done_v6
2307  * calls us.
2308  */
2309 void
2310 ipif_recreate_interface_routes_v6(ipif_t *old_ipif, ipif_t *ipif)
2311 {
2312 	ire_t *ire;
2313 	ire_t *ipif_ire;
2314 	queue_t *stq;
2315 	ill_t *ill;
2316 	ipif_t *nipif = NULL;
2317 	boolean_t nipif_refheld = B_FALSE;
2318 	boolean_t ip6_asp_table_held = B_FALSE;
2319 
2320 	ill = ipif->ipif_ill;
2321 
2322 	if (!(ipif->ipif_flags &
2323 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
2324 		/*
2325 		 * Can't possibly have borrowed the source
2326 		 * from old_ipif.
2327 		 */
2328 		return;
2329 	}
2330 
2331 	/*
2332 	 * Is there any work to be done? No work if the address
2333 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
2334 	 * ipif_select_source_v6() does not borrow addresses from
2335 	 * NOLOCAL and ANYCAST interfaces).
2336 	 */
2337 	if ((old_ipif != NULL) &&
2338 	    ((IN6_IS_ADDR_UNSPECIFIED(&old_ipif->ipif_v6lcl_addr)) ||
2339 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
2340 	    (old_ipif->ipif_flags &
2341 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
2342 		return;
2343 	}
2344 
2345 	/*
2346 	 * Perform the same checks as when creating the
2347 	 * IRE_INTERFACE in ipif_up_done_v6.
2348 	 */
2349 	if (!(ipif->ipif_flags & IPIF_UP))
2350 		return;
2351 
2352 	if ((ipif->ipif_flags & IPIF_NOXMIT))
2353 		return;
2354 
2355 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
2356 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
2357 		return;
2358 
2359 	/*
2360 	 * We know that ipif uses some other source for its
2361 	 * IRE_INTERFACE. Is it using the source of this
2362 	 * old_ipif?
2363 	 */
2364 	ipif_ire = ipif_to_ire_v6(ipif);
2365 	if (ipif_ire == NULL)
2366 		return;
2367 
2368 	if (old_ipif != NULL &&
2369 	    !IN6_ARE_ADDR_EQUAL(&old_ipif->ipif_v6lcl_addr,
2370 	    &ipif_ire->ire_src_addr_v6)) {
2371 		ire_refrele(ipif_ire);
2372 		return;
2373 	}
2374 
2375 	if (ip_debug > 2) {
2376 		/* ip1dbg */
2377 		pr_addr_dbg("ipif_recreate_interface_routes_v6: deleting IRE"
2378 		    " for src %s\n", AF_INET6, &ipif_ire->ire_src_addr_v6);
2379 	}
2380 
2381 	stq = ipif_ire->ire_stq;
2382 
2383 	/*
2384 	 * Can't use our source address. Select a different source address
2385 	 * for the IRE_INTERFACE.  We restrict interface route source
2386 	 * address selection to ipif's assigned to the same link as the
2387 	 * interface.
2388 	 */
2389 	if (ip6_asp_can_lookup()) {
2390 		ip6_asp_table_held = B_TRUE;
2391 		nipif = ipif_select_source_v6(ill, &ipif->ipif_v6subnet,
2392 		    RESTRICT_TO_GROUP, IPV6_PREFER_SRC_DEFAULT,
2393 		    ipif->ipif_zoneid);
2394 	}
2395 	if (nipif == NULL) {
2396 		/* Last resort - all ipif's have IPIF_NOLOCAL */
2397 		nipif = ipif;
2398 	} else {
2399 		nipif_refheld = B_TRUE;
2400 	}
2401 
2402 	ire = ire_create_v6(
2403 	    &ipif->ipif_v6subnet,	/* dest pref */
2404 	    &ipif->ipif_v6net_mask,	/* mask */
2405 	    &nipif->ipif_v6src_addr,	/* src addr */
2406 	    NULL,			/* no gateway */
2407 	    &ipif->ipif_mtu,		/* max frag */
2408 	    NULL,			/* no Fast path header */
2409 	    NULL,			/* no recv from queue */
2410 	    stq,			/* send-to queue */
2411 	    ill->ill_net_type,		/* IF_[NO]RESOLVER */
2412 	    ill->ill_resolver_mp,	/* xmit header */
2413 	    ipif,
2414 	    NULL,
2415 	    0,
2416 	    0,
2417 	    0,
2418 	    &ire_uinfo_null,
2419 	    NULL,
2420 	    NULL);
2421 
2422 	if (ire != NULL) {
2423 		ire_t *ret_ire;
2424 		int   error;
2425 
2426 		/*
2427 		 * We don't need ipif_ire anymore. We need to delete
2428 		 * before we add so that ire_add does not detect
2429 		 * duplicates.
2430 		 */
2431 		ire_delete(ipif_ire);
2432 		ret_ire = ire;
2433 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
2434 		ASSERT(error == 0);
2435 		ASSERT(ret_ire == ire);
2436 		if (ret_ire != NULL) {
2437 			/* Held in ire_add */
2438 			ire_refrele(ret_ire);
2439 		}
2440 	}
2441 	/*
2442 	 * Either we are falling through from above or could not
2443 	 * allocate a replacement.
2444 	 */
2445 	ire_refrele(ipif_ire);
2446 	if (ip6_asp_table_held)
2447 		ip6_asp_table_refrele();
2448 	if (nipif_refheld)
2449 		ipif_refrele(nipif);
2450 }
2451 
2452 /*
2453  * This old_ipif is going away.
2454  *
2455  * Determine if any other ipif's are using our address as
2456  * ipif_v6lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
2457  * IPIF_DEPRECATED).
2458  * Find the IRE_INTERFACE for such ipif's and recreate them
2459  * to use an different source address following the rules in
2460  * ipif_up_done_v6.
2461  *
2462  * This function takes an illgrp as an argument so that illgrp_delete
2463  * can call this to update source address even after deleting the
2464  * old_ipif->ipif_ill from the ill group.
2465  */
2466 void
2467 ipif_update_other_ipifs_v6(ipif_t *old_ipif, ill_group_t *illgrp)
2468 {
2469 	ipif_t	*ipif;
2470 	ill_t	*ill;
2471 	char	buf[INET6_ADDRSTRLEN];
2472 
2473 	ASSERT(IAM_WRITER_IPIF(old_ipif));
2474 
2475 	ill = old_ipif->ipif_ill;
2476 
2477 	ip1dbg(("ipif_update_other_ipifs_v6(%s, %s)\n",
2478 	    ill->ill_name,
2479 	    inet_ntop(AF_INET6, &old_ipif->ipif_v6lcl_addr,
2480 	    buf, sizeof (buf))));
2481 
2482 	/*
2483 	 * If this part of a group, look at all ills as ipif_select_source
2484 	 * borrows a source address across all the ills in the group.
2485 	 */
2486 	if (illgrp != NULL)
2487 		ill = illgrp->illgrp_ill;
2488 
2489 	/* Don't need a lock since this is a writer */
2490 	for (; ill != NULL; ill = ill->ill_group_next) {
2491 		for (ipif = ill->ill_ipif; ipif != NULL;
2492 		    ipif = ipif->ipif_next) {
2493 
2494 			if (ipif == old_ipif)
2495 				continue;
2496 
2497 			ipif_recreate_interface_routes_v6(old_ipif, ipif);
2498 		}
2499 	}
2500 }
2501 
2502 /*
2503  * Perform an attach and bind to get phys addr plus info_req for
2504  * the physical device.
2505  * q and mp represents an ioctl which will be queued waiting for
2506  * completion of the DLPI message exchange.
2507  * MUST be called on an ill queue. Can not set conn_pending_ill for that
2508  * reason thus the DL_PHYS_ADDR_ACK code does not assume ill_pending_q.
2509  *
2510  * Returns EINPROGRESS when mp has been consumed by queueing it on
2511  * ill_pending_mp and the ioctl will complete in ip_rput.
2512  */
2513 int
2514 ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
2515 {
2516 	mblk_t	*v6token_mp = NULL;
2517 	mblk_t	*v6lla_mp = NULL;
2518 	mblk_t	*phys_mp = NULL;
2519 	mblk_t	*info_mp = NULL;
2520 	mblk_t	*attach_mp = NULL;
2521 	mblk_t	*detach_mp = NULL;
2522 	mblk_t	*bind_mp = NULL;
2523 	mblk_t	*unbind_mp = NULL;
2524 	mblk_t	*notify_mp = NULL;
2525 
2526 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
2527 	ASSERT(ill->ill_dlpi_style_set);
2528 	ASSERT(WR(q)->q_next != NULL);
2529 
2530 	if (ill->ill_isv6) {
2531 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2532 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2533 		if (v6token_mp == NULL)
2534 			goto bad;
2535 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
2536 		    DL_IPV6_TOKEN;
2537 
2538 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2539 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2540 		if (v6lla_mp == NULL)
2541 			goto bad;
2542 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
2543 		    DL_IPV6_LINK_LAYER_ADDR;
2544 	}
2545 
2546 	/*
2547 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
2548 	 */
2549 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
2550 	    DL_NOTIFY_REQ);
2551 	if (notify_mp == NULL)
2552 		goto bad;
2553 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
2554 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
2555 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG);
2556 
2557 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2558 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2559 	if (phys_mp == NULL)
2560 		goto bad;
2561 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
2562 	    DL_CURR_PHYS_ADDR;
2563 
2564 	info_mp = ip_dlpi_alloc(
2565 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
2566 	    DL_INFO_REQ);
2567 	if (info_mp == NULL)
2568 		goto bad;
2569 
2570 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
2571 	    DL_BIND_REQ);
2572 	if (bind_mp == NULL)
2573 		goto bad;
2574 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
2575 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
2576 
2577 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
2578 	if (unbind_mp == NULL)
2579 		goto bad;
2580 
2581 	/* If we need to attach/detach, pre-alloc and initialize the mblks */
2582 	if (ill->ill_needs_attach) {
2583 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
2584 		    DL_ATTACH_REQ);
2585 		if (attach_mp == NULL)
2586 			goto bad;
2587 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
2588 
2589 		detach_mp = ip_dlpi_alloc(sizeof (dl_detach_req_t),
2590 		    DL_DETACH_REQ);
2591 		if (detach_mp == NULL)
2592 			goto bad;
2593 	}
2594 
2595 	/*
2596 	 * Here we are going to delay the ioctl ack until after
2597 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
2598 	 * original ioctl message before sending the requests
2599 	 */
2600 	mutex_enter(&ill->ill_lock);
2601 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
2602 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
2603 	/*
2604 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
2605 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
2606 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
2607 	 */
2608 	ill->ill_phys_addr_pend = 0;
2609 	mutex_exit(&ill->ill_lock);
2610 
2611 	if (attach_mp != NULL) {
2612 		ip1dbg(("ill_dl_phys: attach\n"));
2613 		ill_dlpi_send(ill, attach_mp);
2614 	}
2615 	ill_dlpi_send(ill, bind_mp);
2616 	ill_dlpi_send(ill, info_mp);
2617 	if (ill->ill_isv6) {
2618 		ill_dlpi_send(ill, v6token_mp);
2619 		ill_dlpi_send(ill, v6lla_mp);
2620 	}
2621 	ill_dlpi_send(ill, phys_mp);
2622 	ill_dlpi_send(ill, notify_mp);
2623 	ill_dlpi_send(ill, unbind_mp);
2624 
2625 	/*
2626 	 * Save the DL_DETACH_REQ (if there is one) for use in ill_delete().
2627 	 */
2628 	ASSERT(ill->ill_detach_mp == NULL);
2629 	ill->ill_detach_mp = detach_mp;
2630 
2631 	/*
2632 	 * This operation will complete in ip_rput_dlpi_writer with either
2633 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
2634 	 */
2635 	return (EINPROGRESS);
2636 bad:
2637 	if (v6token_mp != NULL)
2638 		freemsg(v6token_mp);
2639 	if (v6lla_mp != NULL)
2640 		freemsg(v6lla_mp);
2641 	if (phys_mp != NULL)
2642 		freemsg(phys_mp);
2643 	if (info_mp != NULL)
2644 		freemsg(info_mp);
2645 	if (attach_mp != NULL)
2646 		freemsg(attach_mp);
2647 	if (detach_mp != NULL)
2648 		freemsg(detach_mp);
2649 	if (bind_mp != NULL)
2650 		freemsg(bind_mp);
2651 	if (unbind_mp != NULL)
2652 		freemsg(unbind_mp);
2653 	if (notify_mp != NULL)
2654 		freemsg(notify_mp);
2655 	return (ENOMEM);
2656 }
2657 
2658 uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
2659 
2660 /*
2661  * DLPI is up.
2662  * Create all the IREs associated with an interface bring up multicast.
2663  * Set the interface flag and finish other initialization
2664  * that potentially had to be differed to after DL_BIND_ACK.
2665  */
2666 int
2667 ipif_up_done_v6(ipif_t *ipif)
2668 {
2669 	ire_t	*ire_array[20];
2670 	ire_t	**irep = ire_array;
2671 	ire_t	**irep1;
2672 	ill_t	*ill = ipif->ipif_ill;
2673 	queue_t	*stq;
2674 	in6_addr_t	v6addr;
2675 	in6_addr_t	route_mask;
2676 	ipif_t	 *src_ipif = NULL;
2677 	ipif_t   *tmp_ipif;
2678 	boolean_t	flush_ire_cache = B_TRUE;
2679 	int	err;
2680 	char	buf[INET6_ADDRSTRLEN];
2681 	phyint_t *phyi;
2682 	ire_t	**ipif_saved_irep = NULL;
2683 	int ipif_saved_ire_cnt;
2684 	int cnt;
2685 	boolean_t src_ipif_held = B_FALSE;
2686 	boolean_t ire_added = B_FALSE;
2687 	boolean_t loopback = B_FALSE;
2688 	boolean_t ip6_asp_table_held = B_FALSE;
2689 
2690 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
2691 		ipif->ipif_ill->ill_name, ipif->ipif_id));
2692 
2693 	/* Check if this is a loopback interface */
2694 	if (ipif->ipif_ill->ill_wq == NULL)
2695 		loopback = B_TRUE;
2696 
2697 	ASSERT(ipif->ipif_isv6);
2698 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2699 
2700 	/*
2701 	 * If all other interfaces for this ill are down or DEPRECATED,
2702 	 * or otherwise unsuitable for source address selection, remove
2703 	 * any IRE_CACHE entries for this ill to make sure source
2704 	 * address selection gets to take this new ipif into account.
2705 	 * No need to hold ill_lock while traversing the ipif list since
2706 	 * we are writer
2707 	 */
2708 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
2709 		tmp_ipif = tmp_ipif->ipif_next) {
2710 		if (((tmp_ipif->ipif_flags &
2711 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
2712 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
2713 		    (tmp_ipif == ipif))
2714 			continue;
2715 		/* first useable pre-existing interface */
2716 		flush_ire_cache = B_FALSE;
2717 		break;
2718 	}
2719 	if (flush_ire_cache)
2720 		ire_walk_ill_v6(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
2721 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
2722 
2723 	/*
2724 	 * Figure out which way the send-to queue should go.  Only
2725 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER should show up here.
2726 	 */
2727 	switch (ill->ill_net_type) {
2728 	case IRE_IF_RESOLVER:
2729 		stq = ill->ill_rq;
2730 		break;
2731 	case IRE_IF_NORESOLVER:
2732 	case IRE_LOOPBACK:
2733 		stq = ill->ill_wq;
2734 		break;
2735 	default:
2736 		return (EINVAL);
2737 	}
2738 
2739 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
2740 		/*
2741 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
2742 		 * ipif_lookup_on_name(), but in the case of zones we can have
2743 		 * several loopback addresses on lo0. So all the interfaces with
2744 		 * loopback addresses need to be marked IRE_LOOPBACK.
2745 		 */
2746 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
2747 			ipif->ipif_ire_type = IRE_LOOPBACK;
2748 		else
2749 			ipif->ipif_ire_type = IRE_LOCAL;
2750 	}
2751 
2752 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
2753 		/*
2754 		 * Can't use our source address. Select a different
2755 		 * source address for the IRE_INTERFACE and IRE_LOCAL
2756 		 */
2757 		if (ip6_asp_can_lookup()) {
2758 			ip6_asp_table_held = B_TRUE;
2759 			src_ipif = ipif_select_source_v6(ipif->ipif_ill,
2760 			    &ipif->ipif_v6subnet, RESTRICT_TO_NONE,
2761 			    IPV6_PREFER_SRC_DEFAULT, ipif->ipif_zoneid);
2762 		}
2763 		if (src_ipif == NULL)
2764 			src_ipif = ipif;	/* Last resort */
2765 		else
2766 			src_ipif_held = B_TRUE;
2767 	} else {
2768 		src_ipif = ipif;
2769 	}
2770 
2771 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
2772 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
2773 
2774 		/*
2775 		 * If we're on a labeled system then make sure that zone-
2776 		 * private addresses have proper remote host database entries.
2777 		 */
2778 		if (is_system_labeled() &&
2779 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
2780 			if (ip6opt_ls == 0) {
2781 				cmn_err(CE_WARN, "IPv6 not enabled "
2782 				    "via /etc/system");
2783 				return (EINVAL);
2784 			}
2785 			if (!tsol_check_interface_address(ipif))
2786 				return (EINVAL);
2787 		}
2788 
2789 		/* Register the source address for __sin6_src_id */
2790 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
2791 		    ipif->ipif_zoneid);
2792 		if (err != 0) {
2793 			ip0dbg(("ipif_up_done_v6: srcid_insert %d\n", err));
2794 			if (src_ipif_held)
2795 				ipif_refrele(src_ipif);
2796 			if (ip6_asp_table_held)
2797 				ip6_asp_table_refrele();
2798 			return (err);
2799 		}
2800 		/*
2801 		 * If the interface address is set, create the LOCAL
2802 		 * or LOOPBACK IRE.
2803 		 */
2804 		ip1dbg(("ipif_up_done_v6: creating IRE %d for %s\n",
2805 		    ipif->ipif_ire_type,
2806 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
2807 		    buf, sizeof (buf))));
2808 
2809 		*irep++ = ire_create_v6(
2810 		    &ipif->ipif_v6lcl_addr,		/* dest address */
2811 		    &ipv6_all_ones,			/* mask */
2812 		    &src_ipif->ipif_v6src_addr,		/* source address */
2813 		    NULL,				/* no gateway */
2814 		    &ip_loopback_mtu_v6plus,		/* max frag size */
2815 		    NULL,
2816 		    ipif->ipif_rq,			/* recv-from queue */
2817 		    NULL,				/* no send-to queue */
2818 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
2819 		    NULL,
2820 		    ipif,				/* interface */
2821 		    NULL,
2822 		    0,
2823 		    0,
2824 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
2825 		    &ire_uinfo_null,
2826 		    NULL,
2827 		    NULL);
2828 	}
2829 
2830 	/*
2831 	 * Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate.
2832 	 * Note that atun interfaces have an all-zero ipif_v6subnet.
2833 	 * Thus we allow a zero subnet as long as the mask is non-zero.
2834 	 */
2835 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
2836 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
2837 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
2838 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
2839 		v6addr = ipif->ipif_v6subnet;
2840 
2841 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
2842 			route_mask = ipv6_all_ones;
2843 		} else {
2844 			route_mask = ipif->ipif_v6net_mask;
2845 		}
2846 
2847 		ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s\n",
2848 		    ill->ill_net_type,
2849 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
2850 
2851 		*irep++ = ire_create_v6(
2852 		    &v6addr,			/* dest pref */
2853 		    &route_mask,		/* mask */
2854 		    &src_ipif->ipif_v6src_addr,	/* src addr */
2855 		    NULL,			/* no gateway */
2856 		    &ipif->ipif_mtu,		/* max frag */
2857 		    NULL,			/* no Fast path header */
2858 		    NULL,			/* no recv from queue */
2859 		    stq,			/* send-to queue */
2860 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
2861 		    ill->ill_resolver_mp,	/* xmit header */
2862 		    ipif,
2863 		    NULL,
2864 		    0,
2865 		    0,
2866 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
2867 		    &ire_uinfo_null,
2868 		    NULL,
2869 		    NULL);
2870 	}
2871 
2872 	/*
2873 	 * Setup 2002::/16 route, if this interface is a 6to4 tunnel
2874 	 */
2875 	if (IN6_IS_ADDR_6TO4(&ipif->ipif_v6lcl_addr) &&
2876 	    (ill->ill_is_6to4tun)) {
2877 		/*
2878 		 * Destination address is 2002::/16
2879 		 */
2880 #ifdef	_BIG_ENDIAN
2881 		const in6_addr_t prefix_addr = { 0x20020000U, 0, 0, 0 };
2882 		const in6_addr_t prefix_mask = { 0xffff0000U, 0, 0, 0 };
2883 #else
2884 		const in6_addr_t prefix_addr = { 0x00000220U, 0, 0, 0 };
2885 		const in6_addr_t prefix_mask = { 0x0000ffffU, 0, 0, 0 };
2886 #endif /* _BIG_ENDIAN */
2887 		char	buf2[INET6_ADDRSTRLEN];
2888 		ire_t *isdup;
2889 		in6_addr_t *first_addr = &ill->ill_ipif->ipif_v6lcl_addr;
2890 
2891 		/*
2892 		 * check to see if this route has already been added for
2893 		 * this tunnel interface.
2894 		 */
2895 		isdup = ire_ftable_lookup_v6(first_addr, &prefix_mask, 0,
2896 		    IRE_IF_NORESOLVER, ill->ill_ipif, NULL, ALL_ZONES, 0, NULL,
2897 		    (MATCH_IRE_SRC | MATCH_IRE_MASK));
2898 
2899 		if (isdup == NULL) {
2900 			ip1dbg(("ipif_up_done_v6: creating if IRE %d for %s",
2901 			    IRE_IF_NORESOLVER, inet_ntop(AF_INET6, &v6addr,
2902 				buf2, sizeof (buf2))));
2903 
2904 			*irep++ = ire_create_v6(
2905 			    &prefix_addr,		/* 2002:: */
2906 			    &prefix_mask,		/* ffff:: */
2907 			    &ipif->ipif_v6lcl_addr, 	/* src addr */
2908 			    NULL, 			/* gateway */
2909 			    &ipif->ipif_mtu, 		/* max_frag */
2910 			    NULL, 			/* no Fast Path hdr */
2911 			    NULL, 			/* no rfq */
2912 			    ill->ill_wq, 		/* stq */
2913 			    IRE_IF_NORESOLVER,		/* type */
2914 			    ill->ill_resolver_mp,	/* dlureq_mp */
2915 			    ipif,			/* interface */
2916 			    NULL,			/* v6cmask */
2917 			    0,
2918 			    0,
2919 			    RTF_UP,
2920 			    &ire_uinfo_null,
2921 			    NULL,
2922 			    NULL);
2923 		} else {
2924 			ire_refrele(isdup);
2925 		}
2926 	}
2927 
2928 	/* If an earlier ire_create failed, get out now */
2929 	for (irep1 = irep; irep1 > ire_array; ) {
2930 		irep1--;
2931 		if (*irep1 == NULL) {
2932 			ip1dbg(("ipif_up_done_v6: NULL ire found in"
2933 			    " ire_array\n"));
2934 			err = ENOMEM;
2935 			goto bad;
2936 		}
2937 	}
2938 
2939 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2940 
2941 	/*
2942 	 * Need to atomically check for ip_addr_availablity_check
2943 	 * now under ill_g_lock, and if it fails got bad, and remove
2944 	 * from group also
2945 	 */
2946 	rw_enter(&ill_g_lock, RW_READER);
2947 	mutex_enter(&ip_addr_avail_lock);
2948 	ill->ill_ipif_up_count++;
2949 	ipif->ipif_flags |= IPIF_UP;
2950 	err = ip_addr_availability_check(ipif);
2951 	mutex_exit(&ip_addr_avail_lock);
2952 	rw_exit(&ill_g_lock);
2953 
2954 	if (err != 0) {
2955 		/*
2956 		 * Our address may already be up on the same ill. In this case,
2957 		 * the external resolver entry for our ipif replaced the one for
2958 		 * the other ipif. So we don't want to delete it (otherwise the
2959 		 * other ipif would be unable to send packets).
2960 		 * ip_addr_availability_check() identifies this case for us and
2961 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
2962 		 * which is the expected error code.
2963 		 */
2964 		if (err == EADDRINUSE) {
2965 			if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV) {
2966 				freemsg(ipif->ipif_arp_del_mp);
2967 				ipif->ipif_arp_del_mp = NULL;
2968 			}
2969 			err = EADDRNOTAVAIL;
2970 		}
2971 		ill->ill_ipif_up_count--;
2972 		ipif->ipif_flags &= ~IPIF_UP;
2973 		goto bad;
2974 	}
2975 
2976 	/*
2977 	 * Add in all newly created IREs. We want to add before
2978 	 * we call ifgrp_insert which wants to know whether
2979 	 * IRE_IF_RESOLVER exists or not.
2980 	 *
2981 	 * NOTE : We refrele the ire though we may branch to "bad"
2982 	 *	  later on where we do ire_delete. This is okay
2983 	 *	  because nobody can delete it as we are running
2984 	 *	  exclusively.
2985 	 */
2986 	for (irep1 = irep; irep1 > ire_array; ) {
2987 		irep1--;
2988 		/* Shouldn't be adding any bcast ire's */
2989 		ASSERT((*irep1)->ire_type != IRE_BROADCAST);
2990 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2991 		/*
2992 		 * refheld by ire_add. refele towards the end of the func
2993 		 */
2994 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
2995 	}
2996 	if (ip6_asp_table_held) {
2997 		ip6_asp_table_refrele();
2998 		ip6_asp_table_held = B_FALSE;
2999 	}
3000 	ire_added = B_TRUE;
3001 
3002 	/*
3003 	 * Form groups if possible.
3004 	 *
3005 	 * If we are supposed to be in a ill_group with a name, insert it
3006 	 * now as we know that at least one ipif is UP. Otherwise form
3007 	 * nameless groups.
3008 	 *
3009 	 * If ip_enable_group_ifs is set and ipif address is not ::0, insert
3010 	 * this ipif into the appropriate interface group, or create a
3011 	 * new one. If this is already in a nameless group, we try to form
3012 	 * a bigger group looking at other ills potentially sharing this
3013 	 * ipif's prefix.
3014 	 */
3015 	phyi = ill->ill_phyint;
3016 	if (phyi->phyint_groupname_len != 0) {
3017 		ASSERT(phyi->phyint_groupname != NULL);
3018 		if (ill->ill_ipif_up_count == 1) {
3019 			ASSERT(ill->ill_group == NULL);
3020 			err = illgrp_insert(&illgrp_head_v6, ill,
3021 			    phyi->phyint_groupname, NULL, B_TRUE);
3022 			if (err != 0) {
3023 				ip1dbg(("ipif_up_done_v6: illgrp allocation "
3024 				    "failed, error %d\n", err));
3025 				goto bad;
3026 			}
3027 		}
3028 		ASSERT(ill->ill_group != NULL);
3029 	}
3030 
3031 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
3032 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
3033 	ipif_saved_irep = ipif_recover_ire_v6(ipif);
3034 
3035 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
3036 		/*
3037 		 * Need to recover all multicast memberships in the driver.
3038 		 * This had to be deferred until we had attached.
3039 		 */
3040 		ill_recover_multicast(ill);
3041 	}
3042 	/* Join the allhosts multicast address and the solicited node MC */
3043 	ipif_multicast_up(ipif);
3044 
3045 	if (!loopback) {
3046 		/*
3047 		 * See whether anybody else would benefit from the
3048 		 * new ipif that we added. We call this always rather
3049 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
3050 		 * ipif for the benefit of illgrp_insert (done above)
3051 		 * which does not do source address selection as it does
3052 		 * not want to re-create interface routes that we are
3053 		 * having reference to it here.
3054 		 */
3055 		ill_update_source_selection(ill);
3056 	}
3057 
3058 	for (irep1 = irep; irep1 > ire_array; ) {
3059 		irep1--;
3060 		if (*irep1 != NULL) {
3061 			/* was held in ire_add */
3062 			ire_refrele(*irep1);
3063 		}
3064 	}
3065 
3066 	cnt = ipif_saved_ire_cnt;
3067 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
3068 		if (*irep1 != NULL) {
3069 			/* was held in ire_add */
3070 			ire_refrele(*irep1);
3071 		}
3072 	}
3073 
3074 	if (ipif->ipif_addr_ready) {
3075 		ip_rts_ifmsg(ipif);
3076 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
3077 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
3078 	}
3079 
3080 	if (ipif_saved_irep != NULL) {
3081 		kmem_free(ipif_saved_irep,
3082 		    ipif_saved_ire_cnt * sizeof (ire_t *));
3083 	}
3084 
3085 	if (src_ipif_held)
3086 		ipif_refrele(src_ipif);
3087 	return (0);
3088 
3089 bad:
3090 	if (ip6_asp_table_held)
3091 		ip6_asp_table_refrele();
3092 	/*
3093 	 * We don't have to bother removing from ill groups because
3094 	 *
3095 	 * 1) For groups with names, we insert only when the first ipif
3096 	 *    comes up. In that case if it fails, it will not be in any
3097 	 *    group. So, we need not try to remove for that case.
3098 	 *
3099 	 * 2) For groups without names, either we tried to insert ipif_ill
3100 	 *    in a group as singleton or found some other group to become
3101 	 *    a bigger group. For the former, if it fails we don't have
3102 	 *    anything to do as ipif_ill is not in the group and for the
3103 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
3104 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
3105 	 */
3106 
3107 	while (irep > ire_array) {
3108 		irep--;
3109 		if (*irep != NULL) {
3110 			ire_delete(*irep);
3111 			if (ire_added)
3112 				ire_refrele(*irep);
3113 		}
3114 
3115 	}
3116 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
3117 
3118 	if (ipif_saved_irep != NULL) {
3119 		kmem_free(ipif_saved_irep,
3120 		    ipif_saved_ire_cnt * sizeof (ire_t *));
3121 	}
3122 	if (src_ipif_held)
3123 		ipif_refrele(src_ipif);
3124 
3125 	ipif_ndp_down(ipif);
3126 	if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
3127 		ipif_arp_down(ipif);
3128 
3129 	return (err);
3130 }
3131 
3132 /*
3133  * Delete an ND entry and the corresponding IRE_CACHE entry if it exists.
3134  */
3135 /* ARGSUSED */
3136 int
3137 ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
3138     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
3139 {
3140 	in6_addr_t	addr;
3141 	sin6_t		*sin6;
3142 	nce_t		*nce;
3143 	struct lifreq	*lifr;
3144 	lif_nd_req_t	*lnr;
3145 	mblk_t	*mp1;
3146 
3147 	mp1 = mp->b_cont->b_cont;
3148 	lifr = (struct lifreq *)mp1->b_rptr;
3149 	lnr = &lifr->lifr_nd;
3150 	/* Only allow for logical unit zero i.e. not on "le0:17" */
3151 	if (ipif->ipif_id != 0)
3152 		return (EINVAL);
3153 
3154 	if (!ipif->ipif_isv6)
3155 		return (EINVAL);
3156 
3157 	if (lnr->lnr_addr.ss_family != AF_INET6)
3158 		return (EAFNOSUPPORT);
3159 
3160 	sin6 = (sin6_t *)&lnr->lnr_addr;
3161 	addr = sin6->sin6_addr;
3162 	nce = ndp_lookup_v6(ipif->ipif_ill, &addr, B_FALSE);
3163 	if (nce == NULL)
3164 		return (ESRCH);
3165 	ndp_delete(nce);
3166 	NCE_REFRELE(nce);
3167 	return (0);
3168 }
3169 
3170 /*
3171  * Return nbr cache info.
3172  */
3173 /* ARGSUSED */
3174 int
3175 ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
3176     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
3177 {
3178 	ill_t		*ill = ipif->ipif_ill;
3179 	struct lifreq	*lifr;
3180 	lif_nd_req_t	*lnr;
3181 
3182 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
3183 	lnr = &lifr->lifr_nd;
3184 	/* Only allow for logical unit zero i.e. not on "le0:17" */
3185 	if (ipif->ipif_id != 0)
3186 		return (EINVAL);
3187 
3188 	if (!ipif->ipif_isv6)
3189 		return (EINVAL);
3190 
3191 	if (lnr->lnr_addr.ss_family != AF_INET6)
3192 		return (EAFNOSUPPORT);
3193 
3194 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
3195 		return (EINVAL);
3196 
3197 	return (ndp_query(ill, lnr));
3198 }
3199 
3200 /*
3201  * Perform an update of the nd entry for the specified address.
3202  */
3203 /* ARGSUSED */
3204 int
3205 ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
3206     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
3207 {
3208 	ill_t		*ill = ipif->ipif_ill;
3209 	struct	lifreq	*lifr;
3210 	lif_nd_req_t	*lnr;
3211 
3212 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
3213 	lnr = &lifr->lifr_nd;
3214 	/* Only allow for logical unit zero i.e. not on "le0:17" */
3215 	if (ipif->ipif_id != 0)
3216 		return (EINVAL);
3217 
3218 	if (!ipif->ipif_isv6)
3219 		return (EINVAL);
3220 
3221 	if (lnr->lnr_addr.ss_family != AF_INET6)
3222 		return (EAFNOSUPPORT);
3223 
3224 	return (ndp_sioc_update(ill, lnr));
3225 }
3226