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