xref: /titanic_51/usr/src/uts/common/inet/ip/ip6_if.c (revision f7a1836a8d16fffa3efd8dbde101c52e5fb6adc7)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 1990 Mentat Inc.
27  */
28 
29 /*
30  * This file contains the interface control functions for IPv6.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/sysmacros.h>
35 #include <sys/stream.h>
36 #include <sys/dlpi.h>
37 #include <sys/stropts.h>
38 #include <sys/ddi.h>
39 #include <sys/cmn_err.h>
40 #include <sys/kstat.h>
41 #include <sys/debug.h>
42 #include <sys/zone.h>
43 #include <sys/policy.h>
44 
45 #include <sys/systm.h>
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <sys/isa_defs.h>
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/igmp_var.h>
54 #include <netinet/ip6.h>
55 #include <netinet/icmp6.h>
56 
57 #include <inet/common.h>
58 #include <inet/nd.h>
59 #include <inet/mib2.h>
60 #include <inet/ip.h>
61 #include <inet/ip6.h>
62 #include <inet/ip_multi.h>
63 #include <inet/ip_ire.h>
64 #include <inet/ip_rts.h>
65 #include <inet/ip_ndp.h>
66 #include <inet/ip_if.h>
67 #include <inet/ip6_asp.h>
68 #include <inet/ipclassifier.h>
69 #include <inet/sctp_ip.h>
70 
71 #include <sys/tsol/tndb.h>
72 #include <sys/tsol/tnet.h>
73 
74 static in6_addr_t	ipv6_ll_template =
75 			{(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
76 
77 static ipif_t *
78 ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
79     ip_stack_t *ipst);
80 
81 static int	ipif_add_ires_v6(ipif_t *, boolean_t);
82 
83 /*
84  * This function is called when an application does not specify an interface
85  * to be used for multicast traffic.  It calls ire_lookup_multi_v6() to look
86  * for an interface route for the specified multicast group.  Doing
87  * this allows the administrator to add prefix routes for multicast to
88  * indicate which interface to be used for multicast traffic in the above
89  * scenario.  The route could be for all multicast (ff00::/8), for a single
90  * multicast group (a /128 route) or anything in between.  If there is no
91  * such multicast route, we just find any multicast capable interface and
92  * return it.
93  *
94  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
95  * unicast table. This is used by CGTP.
96  */
97 ill_t *
98 ill_lookup_group_v6(const in6_addr_t *group, zoneid_t zoneid, ip_stack_t *ipst,
99     boolean_t *multirtp, in6_addr_t *setsrcp)
100 {
101 	ill_t	*ill;
102 
103 	ill = ire_lookup_multi_ill_v6(group, zoneid, ipst, multirtp, setsrcp);
104 	if (ill != NULL)
105 		return (ill);
106 
107 	return (ill_lookup_multicast(ipst, zoneid, B_TRUE));
108 }
109 
110 /*
111  * Look for an ipif with the specified interface address and destination.
112  * The destination address is used only for matching point-to-point interfaces.
113  */
114 static ipif_t *
115 ipif_lookup_interface_v6(const in6_addr_t *if_addr, const in6_addr_t *dst,
116     ip_stack_t *ipst)
117 {
118 	ipif_t	*ipif;
119 	ill_t	*ill;
120 	ill_walk_context_t ctx;
121 
122 	/*
123 	 * First match all the point-to-point interfaces
124 	 * before looking at non-point-to-point interfaces.
125 	 * This is done to avoid returning non-point-to-point
126 	 * ipif instead of unnumbered point-to-point ipif.
127 	 */
128 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
129 	ill = ILL_START_WALK_V6(&ctx, ipst);
130 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
131 		mutex_enter(&ill->ill_lock);
132 		for (ipif = ill->ill_ipif; ipif != NULL;
133 		    ipif = ipif->ipif_next) {
134 			/* Allow the ipif to be down */
135 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
136 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
137 			    if_addr)) &&
138 			    (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
139 			    dst))) {
140 				if (!IPIF_IS_CONDEMNED(ipif)) {
141 					ipif_refhold_locked(ipif);
142 					mutex_exit(&ill->ill_lock);
143 					rw_exit(&ipst->ips_ill_g_lock);
144 					return (ipif);
145 				}
146 			}
147 		}
148 		mutex_exit(&ill->ill_lock);
149 	}
150 	rw_exit(&ipst->ips_ill_g_lock);
151 	/* lookup the ipif based on interface address */
152 	ipif = ipif_lookup_addr_v6(if_addr, NULL, ALL_ZONES, ipst);
153 	ASSERT(ipif == NULL || ipif->ipif_isv6);
154 	return (ipif);
155 }
156 
157 /*
158  * Common function for ipif_lookup_addr_v6() and ipif_lookup_addr_exact_v6().
159  */
160 static ipif_t *
161 ipif_lookup_addr_common_v6(const in6_addr_t *addr, ill_t *match_ill,
162     uint32_t match_flags, zoneid_t zoneid, ip_stack_t *ipst)
163 {
164 	ipif_t	*ipif;
165 	ill_t	*ill;
166 	boolean_t  ptp = B_FALSE;
167 	ill_walk_context_t ctx;
168 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
169 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
170 
171 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
172 	/*
173 	 * Repeat twice, first based on local addresses and
174 	 * next time for pointopoint.
175 	 */
176 repeat:
177 	ill = ILL_START_WALK_V6(&ctx, ipst);
178 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
179 		if (match_ill != NULL && ill != match_ill &&
180 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
181 			continue;
182 		}
183 		mutex_enter(&ill->ill_lock);
184 		for (ipif = ill->ill_ipif; ipif != NULL;
185 		    ipif = ipif->ipif_next) {
186 			if (zoneid != ALL_ZONES &&
187 			    ipif->ipif_zoneid != zoneid &&
188 			    ipif->ipif_zoneid != ALL_ZONES)
189 				continue;
190 
191 			if (no_duplicate &&
192 			    !(ipif->ipif_flags & IPIF_UP)) {
193 				continue;
194 			}
195 
196 			/* Allow the ipif to be down */
197 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
198 			    &ipif->ipif_v6lcl_addr, addr) &&
199 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
200 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
201 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
202 			    addr))) {
203 				if (!IPIF_IS_CONDEMNED(ipif)) {
204 					ipif_refhold_locked(ipif);
205 					mutex_exit(&ill->ill_lock);
206 					rw_exit(&ipst->ips_ill_g_lock);
207 					return (ipif);
208 				}
209 			}
210 		}
211 		mutex_exit(&ill->ill_lock);
212 	}
213 
214 	/* If we already did the ptp case, then we are done */
215 	if (ptp) {
216 		rw_exit(&ipst->ips_ill_g_lock);
217 		return (NULL);
218 	}
219 	ptp = B_TRUE;
220 	goto repeat;
221 }
222 
223 /*
224  * Lookup an ipif with the specified address.  For point-to-point links we
225  * look for matches on either the destination address or the local address,
226  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
227  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
228  * (or illgrp if `match_ill' is in an IPMP group).
229  */
230 ipif_t *
231 ipif_lookup_addr_v6(const in6_addr_t *addr, ill_t *match_ill, zoneid_t zoneid,
232     ip_stack_t *ipst)
233 {
234 	return (ipif_lookup_addr_common_v6(addr, match_ill, IPIF_MATCH_ILLGRP,
235 	    zoneid, ipst));
236 }
237 
238 /*
239  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
240  * except that we will only return an address if it is not marked as
241  * IPIF_DUPLICATE
242  */
243 ipif_t *
244 ipif_lookup_addr_nondup_v6(const in6_addr_t *addr, ill_t *match_ill,
245     zoneid_t zoneid, ip_stack_t *ipst)
246 {
247 	return (ipif_lookup_addr_common_v6(addr, match_ill,
248 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP), zoneid,
249 	    ipst));
250 }
251 
252 /*
253  * Special abbreviated version of ipif_lookup_addr_v6() that doesn't match
254  * `match_ill' across the IPMP group.  This function is only needed in some
255  * corner-cases; almost everything should use ipif_lookup_addr_v6().
256  */
257 ipif_t *
258 ipif_lookup_addr_exact_v6(const in6_addr_t *addr, ill_t *match_ill,
259     ip_stack_t *ipst)
260 {
261 	ASSERT(match_ill != NULL);
262 	return (ipif_lookup_addr_common_v6(addr, match_ill, 0, ALL_ZONES,
263 	    ipst));
264 }
265 
266 /*
267  * Look for an ipif with the specified address. For point-point links
268  * we look for matches on either the destination address and the local
269  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
270  * is set.
271  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
272  * ill (or illgrp if `match_ill' is in an IPMP group).
273  * Return the zoneid for the ipif. ALL_ZONES if none found.
274  */
275 zoneid_t
276 ipif_lookup_addr_zoneid_v6(const in6_addr_t *addr, ill_t *match_ill,
277     ip_stack_t *ipst)
278 {
279 	ipif_t	*ipif;
280 	ill_t	*ill;
281 	boolean_t  ptp = B_FALSE;
282 	ill_walk_context_t ctx;
283 	zoneid_t	zoneid;
284 
285 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
286 	/*
287 	 * Repeat twice, first based on local addresses and
288 	 * next time for pointopoint.
289 	 */
290 repeat:
291 	ill = ILL_START_WALK_V6(&ctx, ipst);
292 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
293 		if (match_ill != NULL && ill != match_ill &&
294 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
295 			continue;
296 		}
297 		mutex_enter(&ill->ill_lock);
298 		for (ipif = ill->ill_ipif; ipif != NULL;
299 		    ipif = ipif->ipif_next) {
300 			/* Allow the ipif to be down */
301 			if ((!ptp && (IN6_ARE_ADDR_EQUAL(
302 			    &ipif->ipif_v6lcl_addr, addr) &&
303 			    (ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
304 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
305 			    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr,
306 			    addr)) &&
307 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
308 				zoneid = ipif->ipif_zoneid;
309 				mutex_exit(&ill->ill_lock);
310 				rw_exit(&ipst->ips_ill_g_lock);
311 				/*
312 				 * If ipif_zoneid was ALL_ZONES then we have
313 				 * a trusted extensions shared IP address.
314 				 * In that case GLOBAL_ZONEID works to send.
315 				 */
316 				if (zoneid == ALL_ZONES)
317 					zoneid = GLOBAL_ZONEID;
318 				return (zoneid);
319 			}
320 		}
321 		mutex_exit(&ill->ill_lock);
322 	}
323 
324 	/* If we already did the ptp case, then we are done */
325 	if (ptp) {
326 		rw_exit(&ipst->ips_ill_g_lock);
327 		return (ALL_ZONES);
328 	}
329 	ptp = B_TRUE;
330 	goto repeat;
331 }
332 
333 /*
334  * Perform various checks to verify that an address would make sense as a local
335  * interface address.  This is currently only called when an attempt is made
336  * to set a local address.
337  *
338  * Does not allow a v4-mapped address, an address that equals the subnet
339  * anycast address, ... a multicast address, ...
340  */
341 boolean_t
342 ip_local_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
343 {
344 	in6_addr_t subnet;
345 
346 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
347 		return (B_TRUE);	/* Allow all zeros */
348 
349 	/*
350 	 * Don't allow all zeroes or host part, but allow
351 	 * all ones netmask.
352 	 */
353 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
354 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
355 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
356 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
357 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))) ||
358 	    IN6_IS_ADDR_MULTICAST(addr))
359 		return (B_FALSE);
360 
361 	return (B_TRUE);
362 }
363 
364 /*
365  * Perform various checks to verify that an address would make sense as a
366  * remote/subnet interface address.
367  */
368 boolean_t
369 ip_remote_addr_ok_v6(const in6_addr_t *addr, const in6_addr_t *subnet_mask)
370 {
371 	in6_addr_t subnet;
372 
373 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
374 		return (B_TRUE);	/* Allow all zeros */
375 
376 	V6_MASK_COPY(*addr, *subnet_mask, subnet);
377 	if (IN6_IS_ADDR_V4MAPPED(addr) ||
378 	    (IN6_ARE_ADDR_EQUAL(addr, &subnet) &&
379 	    !IN6_ARE_ADDR_EQUAL(subnet_mask, &ipv6_all_ones)) ||
380 	    IN6_IS_ADDR_MULTICAST(addr) ||
381 	    (IN6_IS_ADDR_V4COMPAT(addr) && CLASSD(V4_PART_OF_V6((*addr)))))
382 		return (B_FALSE);
383 
384 	return (B_TRUE);
385 }
386 
387 /*
388  * ip_rt_add_v6 is called to add an IPv6 route to the forwarding table.
389  * ill is passed in to associate it with the correct interface
390  * (for link-local destinations and gateways).
391  * If ire_arg is set, then we return the held IRE in that location.
392  */
393 /* ARGSUSED1 */
394 int
395 ip_rt_add_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
396     const in6_addr_t *gw_addr, const in6_addr_t *src_addr, int flags,
397     ill_t *ill, ire_t **ire_arg, struct rtsa_s *sp, ip_stack_t *ipst,
398     zoneid_t zoneid)
399 {
400 	ire_t	*ire, *nire;
401 	ire_t	*gw_ire = NULL;
402 	ipif_t	*ipif;
403 	uint_t	type;
404 	int	match_flags = MATCH_IRE_TYPE;
405 	tsol_gc_t *gc = NULL;
406 	tsol_gcgrp_t *gcgrp = NULL;
407 	boolean_t gcgrp_xtraref = B_FALSE;
408 
409 	if (ire_arg != NULL)
410 		*ire_arg = NULL;
411 
412 	/*
413 	 * Prevent routes with a zero gateway from being created (since
414 	 * interfaces can currently be plumbed and brought up with no assigned
415 	 * address).
416 	 */
417 	if (IN6_IS_ADDR_UNSPECIFIED(gw_addr))
418 		return (ENETUNREACH);
419 
420 	/*
421 	 * If this is the case of RTF_HOST being set, then we set the netmask
422 	 * to all ones (regardless if one was supplied).
423 	 */
424 	if (flags & RTF_HOST)
425 		mask = &ipv6_all_ones;
426 
427 	/*
428 	 * Get the ipif, if any, corresponding to the gw_addr
429 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
430 	 * we match on the gatway and destination to handle unnumbered pt-pt
431 	 * interfaces.
432 	 */
433 	if (ill != NULL)
434 		ipif = ipif_lookup_addr_v6(gw_addr, ill, ALL_ZONES, ipst);
435 	else
436 		ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, ipst);
437 	if (ipif != NULL) {
438 		if (IS_VNI(ipif->ipif_ill)) {
439 			ipif_refrele(ipif);
440 			return (EINVAL);
441 		}
442 	}
443 
444 	/*
445 	 * GateD will attempt to create routes with a loopback interface
446 	 * address as the gateway and with RTF_GATEWAY set.  We allow
447 	 * these routes to be added, but create them as interface routes
448 	 * since the gateway is an interface address.
449 	 */
450 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
451 		flags &= ~RTF_GATEWAY;
452 		if (IN6_ARE_ADDR_EQUAL(gw_addr, &ipv6_loopback) &&
453 		    IN6_ARE_ADDR_EQUAL(dst_addr, &ipv6_loopback) &&
454 		    IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones)) {
455 			ire = ire_ftable_lookup_v6(dst_addr, 0, 0, IRE_LOOPBACK,
456 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
457 			    NULL);
458 			if (ire != NULL) {
459 				ire_refrele(ire);
460 				ipif_refrele(ipif);
461 				return (EEXIST);
462 			}
463 			ip1dbg(("ip_rt_add_v6: 0x%p creating IRE 0x%x"
464 			    "for 0x%x\n", (void *)ipif,
465 			    ipif->ipif_ire_type,
466 			    ntohl(ipif->ipif_lcl_addr)));
467 			ire = ire_create_v6(
468 			    dst_addr,
469 			    mask,
470 			    NULL,
471 			    ipif->ipif_ire_type,	/* LOOPBACK */
472 			    ipif->ipif_ill,
473 			    zoneid,
474 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
475 			    NULL,
476 			    ipst);
477 
478 			if (ire == NULL) {
479 				ipif_refrele(ipif);
480 				return (ENOMEM);
481 			}
482 			/* src address assigned by the caller? */
483 			if ((flags & RTF_SETSRC) &&
484 			    !IN6_IS_ADDR_UNSPECIFIED(src_addr))
485 				ire->ire_setsrc_addr_v6 = *src_addr;
486 
487 			nire = ire_add(ire);
488 			if (nire == NULL) {
489 				/*
490 				 * In the result of failure, ire_add() will have
491 				 * already deleted the ire in question, so there
492 				 * is no need to do that here.
493 				 */
494 				ipif_refrele(ipif);
495 				return (ENOMEM);
496 			}
497 			/*
498 			 * Check if it was a duplicate entry. This handles
499 			 * the case of two racing route adds for the same route
500 			 */
501 			if (nire != ire) {
502 				ASSERT(nire->ire_identical_ref > 1);
503 				ire_delete(nire);
504 				ire_refrele(nire);
505 				ipif_refrele(ipif);
506 				return (EEXIST);
507 			}
508 			ire = nire;
509 			goto save_ire;
510 		}
511 	}
512 
513 	/*
514 	 * The routes for multicast with CGTP are quite special in that
515 	 * the gateway is the local interface address, yet RTF_GATEWAY
516 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
517 	 * this undocumented and unusual use of multicast routes.
518 	 */
519 	if ((flags & RTF_MULTIRT) && ipif != NULL)
520 		flags &= ~RTF_GATEWAY;
521 
522 	/*
523 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
524 	 * and the gateway address provided is one of the system's interface
525 	 * addresses.  By using the routing socket interface and supplying an
526 	 * RTA_IFP sockaddr with an interface index, an alternate method of
527 	 * specifying an interface route to be created is available which uses
528 	 * the interface index that specifies the outgoing interface rather than
529 	 * the address of an outgoing interface (which may not be able to
530 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
531 	 * flag, routes can be specified which not only specify the next-hop to
532 	 * be used when routing to a certain prefix, but also which outgoing
533 	 * interface should be used.
534 	 *
535 	 * Previously, interfaces would have unique addresses assigned to them
536 	 * and so the address assigned to a particular interface could be used
537 	 * to identify a particular interface.  One exception to this was the
538 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
539 	 *
540 	 * With the advent of IPv6 and its link-local addresses, this
541 	 * restriction was relaxed and interfaces could share addresses between
542 	 * themselves.  In fact, typically all of the link-local interfaces on
543 	 * an IPv6 node or router will have the same link-local address.  In
544 	 * order to differentiate between these interfaces, the use of an
545 	 * interface index is necessary and this index can be carried inside a
546 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
547 	 * of using the interface index, however, is that all of the ipif's that
548 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
549 	 * cannot be used to differentiate between ipif's (or logical
550 	 * interfaces) that belong to the same ill (physical interface).
551 	 *
552 	 * For example, in the following case involving IPv4 interfaces and
553 	 * logical interfaces
554 	 *
555 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
556 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
557 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
558 	 *
559 	 * the ipif's corresponding to each of these interface routes can be
560 	 * uniquely identified by the "gateway" (actually interface address).
561 	 *
562 	 * In this case involving multiple IPv6 default routes to a particular
563 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
564 	 * default route is of interest:
565 	 *
566 	 *	default		fe80::123:4567:89ab:cdef	U	if0
567 	 *	default		fe80::123:4567:89ab:cdef	U	if1
568 	 */
569 
570 	/* RTF_GATEWAY not set */
571 	if (!(flags & RTF_GATEWAY)) {
572 		if (sp != NULL) {
573 			ip2dbg(("ip_rt_add_v6: gateway security attributes "
574 			    "cannot be set with interface route\n"));
575 			if (ipif != NULL)
576 				ipif_refrele(ipif);
577 			return (EINVAL);
578 		}
579 
580 		/*
581 		 * Whether or not ill (RTA_IFP) is set, we require that
582 		 * the gateway is one of our local addresses.
583 		 */
584 		if (ipif == NULL)
585 			return (ENETUNREACH);
586 
587 		/*
588 		 * We use MATCH_IRE_ILL here. If the caller specified an
589 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
590 		 * we use the ill derived from the gateway address.
591 		 * We can always match the gateway address since we record it
592 		 * in ire_gateway_addr.
593 		 * We don't allow RTA_IFP to specify a different ill than the
594 		 * one matching the ipif to make sure we can delete the route.
595 		 */
596 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
597 		if (ill == NULL) {
598 			ill = ipif->ipif_ill;
599 		} else if (ill != ipif->ipif_ill) {
600 			ipif_refrele(ipif);
601 			return (EINVAL);
602 		}
603 
604 		/*
605 		 * We check for an existing entry at this point.
606 		 */
607 		match_flags |= MATCH_IRE_MASK;
608 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr,
609 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
610 		    NULL);
611 		if (ire != NULL) {
612 			ire_refrele(ire);
613 			ipif_refrele(ipif);
614 			return (EEXIST);
615 		}
616 
617 		/*
618 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
619 		 * IRE_IF_RESOLVER with the modified address, netmask, and
620 		 * gateway.
621 		 */
622 		ire = ire_create_v6(
623 		    dst_addr,
624 		    mask,
625 		    gw_addr,
626 		    ill->ill_net_type,
627 		    ill,
628 		    zoneid,
629 		    flags,
630 		    NULL,
631 		    ipst);
632 		if (ire == NULL) {
633 			ipif_refrele(ipif);
634 			return (ENOMEM);
635 		}
636 
637 		/*
638 		 * Some software (for example, GateD and Sun Cluster) attempts
639 		 * to create (what amount to) IRE_PREFIX routes with the
640 		 * loopback address as the gateway.  This is primarily done to
641 		 * set up prefixes with the RTF_REJECT flag set (for example,
642 		 * when generating aggregate routes). We also OR in the
643 		 * RTF_BLACKHOLE flag as these interface routes, by
644 		 * definition, can only be that.
645 		 *
646 		 * If the IRE type (as defined by ill->ill_net_type) is
647 		 * IRE_LOOPBACK, then we map the request into a
648 		 * IRE_IF_NORESOLVER.
649 		 *
650 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
651 		 * routine, but rather using ire_create_v6() directly.
652 		 */
653 		if (ill->ill_net_type == IRE_LOOPBACK) {
654 			ire->ire_type = IRE_IF_NORESOLVER;
655 			ire->ire_flags |= RTF_BLACKHOLE;
656 		}
657 		/* src address assigned by the caller? */
658 		if ((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr))
659 			ire->ire_setsrc_addr_v6 = *src_addr;
660 
661 		nire = ire_add(ire);
662 		if (nire == NULL) {
663 			/*
664 			 * In the result of failure, ire_add() will have
665 			 * already deleted the ire in question, so there
666 			 * is no need to do that here.
667 			 */
668 			ipif_refrele(ipif);
669 			return (ENOMEM);
670 		}
671 		/*
672 		 * Check if it was a duplicate entry. This handles
673 		 * the case of two racing route adds for the same route
674 		 */
675 		if (nire != ire) {
676 			ASSERT(nire->ire_identical_ref > 1);
677 			ire_delete(nire);
678 			ire_refrele(nire);
679 			ipif_refrele(ipif);
680 			return (EEXIST);
681 		}
682 		ire = nire;
683 		goto save_ire;
684 	}
685 
686 	/*
687 	 * Get an interface IRE for the specified gateway.
688 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
689 	 * gateway, it is currently unreachable and we fail the request
690 	 * accordingly.
691 	 * If RTA_IFP was specified we look on that particular ill.
692 	 */
693 	if (ill != NULL)
694 		match_flags |= MATCH_IRE_ILL;
695 
696 	/* Check whether the gateway is reachable. */
697 	type = IRE_INTERFACE;
698 	if (flags & RTF_INDIRECT)
699 		type |= IRE_OFFLINK;
700 
701 	gw_ire = ire_ftable_lookup_v6(gw_addr, 0, 0, type, ill,
702 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
703 	if (gw_ire == NULL) {
704 		if (ipif != NULL)
705 			ipif_refrele(ipif);
706 		return (ENETUNREACH);
707 	}
708 
709 	/*
710 	 * We create one of three types of IREs as a result of this request
711 	 * based on the netmask.  A netmask of all ones (which is automatically
712 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
713 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
714 	 * created.  Otherwise, an IRE_PREFIX route is created for the
715 	 * destination prefix.
716 	 */
717 	if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
718 		type = IRE_HOST;
719 	else if (IN6_IS_ADDR_UNSPECIFIED(mask))
720 		type = IRE_DEFAULT;
721 	else
722 		type = IRE_PREFIX;
723 
724 	/* check for a duplicate entry */
725 	ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type, ill,
726 	    ALL_ZONES, NULL,
727 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, 0, ipst, NULL);
728 	if (ire != NULL) {
729 		if (ipif != NULL)
730 			ipif_refrele(ipif);
731 		ire_refrele(gw_ire);
732 		ire_refrele(ire);
733 		return (EEXIST);
734 	}
735 
736 	/* Security attribute exists */
737 	if (sp != NULL) {
738 		tsol_gcgrp_addr_t ga;
739 
740 		/* find or create the gateway credentials group */
741 		ga.ga_af = AF_INET6;
742 		ga.ga_addr = *gw_addr;
743 
744 		/* we hold reference to it upon success */
745 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
746 		if (gcgrp == NULL) {
747 			if (ipif != NULL)
748 				ipif_refrele(ipif);
749 			ire_refrele(gw_ire);
750 			return (ENOMEM);
751 		}
752 
753 		/*
754 		 * Create and add the security attribute to the group; a
755 		 * reference to the group is made upon allocating a new
756 		 * entry successfully.  If it finds an already-existing
757 		 * entry for the security attribute in the group, it simply
758 		 * returns it and no new reference is made to the group.
759 		 */
760 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
761 		if (gc == NULL) {
762 			/* release reference held by gcgrp_lookup */
763 			GCGRP_REFRELE(gcgrp);
764 			if (ipif != NULL)
765 				ipif_refrele(ipif);
766 			ire_refrele(gw_ire);
767 			return (ENOMEM);
768 		}
769 	}
770 
771 	/* Create the IRE. */
772 	ire = ire_create_v6(
773 	    dst_addr,				/* dest address */
774 	    mask,				/* mask */
775 	    gw_addr,				/* gateway address */
776 	    (ushort_t)type,			/* IRE type */
777 	    ill,
778 	    zoneid,
779 	    flags,
780 	    gc,					/* security attribute */
781 	    ipst);
782 
783 	/*
784 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
785 	 * reference to the 'gcgrp'. We can now release the extra reference
786 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
787 	 */
788 	if (gcgrp_xtraref)
789 		GCGRP_REFRELE(gcgrp);
790 	if (ire == NULL) {
791 		if (gc != NULL)
792 			GC_REFRELE(gc);
793 		if (ipif != NULL)
794 			ipif_refrele(ipif);
795 		ire_refrele(gw_ire);
796 		return (ENOMEM);
797 	}
798 
799 	/* src address assigned by the caller? */
800 	if ((flags & RTF_SETSRC) && !IN6_IS_ADDR_UNSPECIFIED(src_addr))
801 		ire->ire_setsrc_addr_v6 = *src_addr;
802 
803 	/*
804 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
805 	 * SUN/OS socket stuff does but do we really want to allow ::0 ?
806 	 */
807 
808 	/* Add the new IRE. */
809 	nire = ire_add(ire);
810 	if (nire == NULL) {
811 		/*
812 		 * In the result of failure, ire_add() will have
813 		 * already deleted the ire in question, so there
814 		 * is no need to do that here.
815 		 */
816 		if (ipif != NULL)
817 			ipif_refrele(ipif);
818 		ire_refrele(gw_ire);
819 		return (ENOMEM);
820 	}
821 	/*
822 	 * Check if it was a duplicate entry. This handles
823 	 * the case of two racing route adds for the same route
824 	 */
825 	if (nire != ire) {
826 		ASSERT(nire->ire_identical_ref > 1);
827 		ire_delete(nire);
828 		ire_refrele(nire);
829 		if (ipif != NULL)
830 			ipif_refrele(ipif);
831 		ire_refrele(gw_ire);
832 		return (EEXIST);
833 	}
834 	ire = nire;
835 
836 	if (flags & RTF_MULTIRT) {
837 		/*
838 		 * Invoke the CGTP (multirouting) filtering module
839 		 * to add the dst address in the filtering database.
840 		 * Replicated inbound packets coming from that address
841 		 * will be filtered to discard the duplicates.
842 		 * It is not necessary to call the CGTP filter hook
843 		 * when the dst address is a multicast, because an
844 		 * IP source address cannot be a multicast.
845 		 */
846 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
847 		    !IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6))) {
848 			int res;
849 			ipif_t *src_ipif;
850 
851 			/* Find the source address corresponding to gw_ire */
852 			src_ipif = ipif_lookup_addr_v6(
853 			    &gw_ire->ire_gateway_addr_v6, NULL, zoneid, ipst);
854 			if (src_ipif != NULL) {
855 				res = ipst->ips_ip_cgtp_filter_ops->
856 				    cfo_add_dest_v6(
857 				    ipst->ips_netstack->netstack_stackid,
858 				    &ire->ire_addr_v6,
859 				    &ire->ire_gateway_addr_v6,
860 				    &ire->ire_setsrc_addr_v6,
861 				    &src_ipif->ipif_v6lcl_addr);
862 				ipif_refrele(src_ipif);
863 			} else {
864 				res = EADDRNOTAVAIL;
865 			}
866 			if (res != 0) {
867 				if (ipif != NULL)
868 					ipif_refrele(ipif);
869 				ire_refrele(gw_ire);
870 				ire_delete(ire);
871 				ire_refrele(ire);	/* Held in ire_add */
872 				return (res);
873 			}
874 		}
875 	}
876 
877 save_ire:
878 	if (gw_ire != NULL) {
879 		ire_refrele(gw_ire);
880 		gw_ire = NULL;
881 	}
882 	if (ire->ire_ill != NULL) {
883 		/*
884 		 * Save enough information so that we can recreate the IRE if
885 		 * the ILL goes down and then up.  The metrics associated
886 		 * with the route will be saved as well when rts_setmetrics() is
887 		 * called after the IRE has been created.  In the case where
888 		 * memory cannot be allocated, none of this information will be
889 		 * saved.
890 		 */
891 		ill_save_ire(ire->ire_ill, ire);
892 	}
893 
894 	if (ire_arg != NULL) {
895 		/*
896 		 * Store the ire that was successfully added into where ire_arg
897 		 * points to so that callers don't have to look it up
898 		 * themselves (but they are responsible for ire_refrele()ing
899 		 * the ire when they are finished with it).
900 		 */
901 		*ire_arg = ire;
902 	} else {
903 		ire_refrele(ire);		/* Held in ire_add */
904 	}
905 	if (ipif != NULL)
906 		ipif_refrele(ipif);
907 	return (0);
908 }
909 
910 /*
911  * ip_rt_delete_v6 is called to delete an IPv6 route.
912  * ill is passed in to associate it with the correct interface.
913  * (for link-local destinations and gateways).
914  */
915 /* ARGSUSED4 */
916 int
917 ip_rt_delete_v6(const in6_addr_t *dst_addr, const in6_addr_t *mask,
918     const in6_addr_t *gw_addr, uint_t rtm_addrs, int flags, ill_t *ill,
919     ip_stack_t *ipst, zoneid_t zoneid)
920 {
921 	ire_t	*ire = NULL;
922 	ipif_t	*ipif;
923 	uint_t	type;
924 	uint_t	match_flags = MATCH_IRE_TYPE;
925 	int	err = 0;
926 
927 	/*
928 	 * If this is the case of RTF_HOST being set, then we set the netmask
929 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
930 	 */
931 	if (flags & RTF_HOST) {
932 		mask = &ipv6_all_ones;
933 		match_flags |= MATCH_IRE_MASK;
934 	} else if (rtm_addrs & RTA_NETMASK) {
935 		match_flags |= MATCH_IRE_MASK;
936 	}
937 
938 	/*
939 	 * Note that RTF_GATEWAY is never set on a delete, therefore
940 	 * we check if the gateway address is one of our interfaces first,
941 	 * and fall back on RTF_GATEWAY routes.
942 	 *
943 	 * This makes it possible to delete an original
944 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
945 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
946 	 * and those can not be deleted here.
947 	 *
948 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
949 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
950 	 * otherwise we use the ill derived from the gateway address.
951 	 * We can always match the gateway address since we record it
952 	 * in ire_gateway_addr.
953 	 *
954 	 * For more detail on specifying routes by gateway address and by
955 	 * interface index, see the comments in ip_rt_add_v6().
956 	 */
957 	ipif = ipif_lookup_interface_v6(gw_addr, dst_addr, ipst);
958 	if (ipif != NULL) {
959 		ill_t	*ill_match;
960 
961 		if (ill != NULL)
962 			ill_match = ill;
963 		else
964 			ill_match = ipif->ipif_ill;
965 
966 		match_flags |= MATCH_IRE_ILL;
967 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
968 			ire = ire_ftable_lookup_v6(dst_addr, 0, 0, IRE_LOOPBACK,
969 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
970 			    NULL);
971 		}
972 		if (ire == NULL) {
973 			match_flags |= MATCH_IRE_GW;
974 			ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr,
975 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
976 			    match_flags, 0, ipst, NULL);
977 		}
978 		/* Avoid deleting routes created by kernel from an ipif */
979 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
980 			ire_refrele(ire);
981 			ire = NULL;
982 		}
983 
984 		/* Restore in case we didn't find a match */
985 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
986 	}
987 
988 	if (ire == NULL) {
989 		/*
990 		 * At this point, the gateway address is not one of our own
991 		 * addresses or a matching interface route was not found.  We
992 		 * set the IRE type to lookup based on whether
993 		 * this is a host route, a default route or just a prefix.
994 		 *
995 		 * If an ill was passed in, then the lookup is based on an
996 		 * interface index so MATCH_IRE_ILL is added to match_flags.
997 		 */
998 		match_flags |= MATCH_IRE_GW;
999 		if (ill != NULL)
1000 			match_flags |= MATCH_IRE_ILL;
1001 		if (IN6_ARE_ADDR_EQUAL(mask, &ipv6_all_ones))
1002 			type = IRE_HOST;
1003 		else if (IN6_IS_ADDR_UNSPECIFIED(mask))
1004 			type = IRE_DEFAULT;
1005 		else
1006 			type = IRE_PREFIX;
1007 		ire = ire_ftable_lookup_v6(dst_addr, mask, gw_addr, type,
1008 		    ill, ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
1009 	}
1010 
1011 	if (ipif != NULL) {
1012 		ipif_refrele(ipif);
1013 		ipif = NULL;
1014 	}
1015 	if (ire == NULL)
1016 		return (ESRCH);
1017 
1018 	if (ire->ire_flags & RTF_MULTIRT) {
1019 		/*
1020 		 * Invoke the CGTP (multirouting) filtering module
1021 		 * to remove the dst address from the filtering database.
1022 		 * Packets coming from that address will no longer be
1023 		 * filtered to remove duplicates.
1024 		 */
1025 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
1026 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v6(
1027 			    ipst->ips_netstack->netstack_stackid,
1028 			    &ire->ire_addr_v6, &ire->ire_gateway_addr_v6);
1029 		}
1030 	}
1031 
1032 	ill = ire->ire_ill;
1033 	if (ill != NULL)
1034 		ill_remove_saved_ire(ill, ire);
1035 	ire_delete(ire);
1036 	ire_refrele(ire);
1037 	return (err);
1038 }
1039 
1040 /*
1041  * Derive an interface id from the link layer address.
1042  */
1043 void
1044 ill_setdefaulttoken(ill_t *ill)
1045 {
1046 	if (!ill->ill_manual_token) {
1047 		bzero(&ill->ill_token, sizeof (ill->ill_token));
1048 		MEDIA_V6INTFID(ill->ill_media, ill, &ill->ill_token);
1049 		ill->ill_token_length = IPV6_TOKEN_LEN;
1050 	}
1051 }
1052 
1053 void
1054 ill_setdesttoken(ill_t *ill)
1055 {
1056 	bzero(&ill->ill_dest_token, sizeof (ill->ill_dest_token));
1057 	MEDIA_V6DESTINTFID(ill->ill_media, ill, &ill->ill_dest_token);
1058 }
1059 
1060 /*
1061  * Create a link-local address from a token.
1062  */
1063 static void
1064 ipif_get_linklocal(in6_addr_t *dest, const in6_addr_t *token)
1065 {
1066 	int i;
1067 
1068 	for (i = 0; i < 4; i++) {
1069 		dest->s6_addr32[i] =
1070 		    token->s6_addr32[i] | ipv6_ll_template.s6_addr32[i];
1071 	}
1072 }
1073 
1074 /*
1075  * Set a default IPv6 address for a 6to4 tunnel interface 2002:<tsrc>::1/16
1076  */
1077 static void
1078 ipif_set6to4addr(ipif_t *ipif)
1079 {
1080 	ill_t		*ill = ipif->ipif_ill;
1081 	struct in_addr	v4phys;
1082 
1083 	ASSERT(ill->ill_mactype == DL_6TO4);
1084 	ASSERT(ill->ill_phys_addr_length == sizeof (struct in_addr));
1085 	ASSERT(ipif->ipif_isv6);
1086 
1087 	if (ipif->ipif_flags & IPIF_UP)
1088 		return;
1089 
1090 	(void) ip_plen_to_mask_v6(16, &ipif->ipif_v6net_mask);
1091 	bcopy(ill->ill_phys_addr, &v4phys, sizeof (struct in_addr));
1092 	IN6_V4ADDR_TO_6TO4(&v4phys, &ipif->ipif_v6lcl_addr);
1093 	V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
1094 	    ipif->ipif_v6subnet);
1095 }
1096 
1097 /*
1098  * Is it not possible to set the link local address?
1099  * The address can be set if the token is set, and the token
1100  * isn't too long.
1101  * Return B_TRUE if the address can't be set, or B_FALSE if it can.
1102  */
1103 boolean_t
1104 ipif_cant_setlinklocal(ipif_t *ipif)
1105 {
1106 	ill_t *ill = ipif->ipif_ill;
1107 
1108 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token) ||
1109 	    ill->ill_token_length > IPV6_ABITS - IPV6_LL_PREFIXLEN)
1110 		return (B_TRUE);
1111 
1112 	return (B_FALSE);
1113 }
1114 
1115 /*
1116  * Generate a link-local address from the token.
1117  */
1118 void
1119 ipif_setlinklocal(ipif_t *ipif)
1120 {
1121 	ill_t		*ill = ipif->ipif_ill;
1122 	in6_addr_t	ov6addr;
1123 
1124 	ASSERT(IAM_WRITER_ILL(ill));
1125 
1126 	/*
1127 	 * ill_manual_linklocal is set when the link-local address was
1128 	 * manually configured.
1129 	 */
1130 	if (ill->ill_manual_linklocal)
1131 		return;
1132 
1133 	/*
1134 	 * IPv6 interfaces over 6to4 tunnels are special.  They do not have
1135 	 * link-local addresses, but instead have a single automatically
1136 	 * generated global address.
1137 	 */
1138 	if (ill->ill_mactype == DL_6TO4) {
1139 		ipif_set6to4addr(ipif);
1140 		return;
1141 	}
1142 
1143 	if (ipif_cant_setlinklocal(ipif))
1144 		return;
1145 
1146 	ov6addr = ipif->ipif_v6lcl_addr;
1147 	ipif_get_linklocal(&ipif->ipif_v6lcl_addr, &ill->ill_token);
1148 	sctp_update_ipif_addr(ipif, ov6addr);
1149 	(void) ip_plen_to_mask_v6(IPV6_LL_PREFIXLEN, &ipif->ipif_v6net_mask);
1150 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)) {
1151 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
1152 		    ipif->ipif_v6subnet);
1153 	}
1154 
1155 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
1156 }
1157 
1158 /*
1159  * Generate a destination link-local address for a point-to-point IPv6
1160  * interface with a destination interface id (IP tunnels are such interfaces)
1161  * based on the destination token.
1162  */
1163 void
1164 ipif_setdestlinklocal(ipif_t *ipif)
1165 {
1166 	ill_t	*ill = ipif->ipif_ill;
1167 
1168 	ASSERT(IAM_WRITER_ILL(ill));
1169 
1170 	if (ill->ill_manual_dst_linklocal)
1171 		return;
1172 
1173 	if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_dest_token))
1174 		return;
1175 
1176 	ipif_get_linklocal(&ipif->ipif_v6pp_dst_addr, &ill->ill_dest_token);
1177 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
1178 }
1179 
1180 /*
1181  * Get the resolver set up for a new ipif.  (Always called as writer.)
1182  */
1183 int
1184 ipif_ndp_up(ipif_t *ipif, boolean_t initial)
1185 {
1186 	ill_t		*ill = ipif->ipif_ill;
1187 	int		err = 0;
1188 	nce_t		*nce = NULL;
1189 	boolean_t	added_ipif = B_FALSE;
1190 
1191 	DTRACE_PROBE3(ipif__downup, char *, "ipif_ndp_up",
1192 	    ill_t *, ill, ipif_t *, ipif);
1193 	ip1dbg(("ipif_ndp_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
1194 
1195 	if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) ||
1196 	    (!(ill->ill_net_type & IRE_INTERFACE))) {
1197 		ipif->ipif_addr_ready = 1;
1198 		return (0);
1199 	}
1200 
1201 	if ((ipif->ipif_flags & (IPIF_UNNUMBERED|IPIF_NOLOCAL)) == 0) {
1202 		uint16_t	flags;
1203 		uint16_t	state;
1204 		uchar_t		*hw_addr;
1205 		ill_t		*bound_ill;
1206 		ipmp_illgrp_t	*illg = ill->ill_grp;
1207 		uint_t		hw_addr_len;
1208 
1209 		flags = NCE_F_MYADDR | NCE_F_NONUD | NCE_F_PUBLISH |
1210 		    NCE_F_AUTHORITY;
1211 		if (ill->ill_flags & ILLF_ROUTER)
1212 			flags |= NCE_F_ISROUTER;
1213 
1214 		if (ipif->ipif_flags & IPIF_ANYCAST)
1215 			flags |= NCE_F_ANYCAST;
1216 
1217 		if (IS_IPMP(ill)) {
1218 			ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
1219 			/*
1220 			 * If we're here via ipif_up(), then the ipif won't be
1221 			 * bound yet -- add it to the group, which will bind
1222 			 * it if possible.  (We would add it in ipif_up(), but
1223 			 * deleting on failure there is gruesome.)  If we're
1224 			 * here via ipmp_ill_bind_ipif(), then the ipif has
1225 			 * already been added to the group and we just need to
1226 			 * use the binding.
1227 			 */
1228 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
1229 				bound_ill = ipmp_illgrp_add_ipif(illg, ipif);
1230 				if (bound_ill == NULL) {
1231 					/*
1232 					 * We couldn't bind the ipif to an ill
1233 					 * yet, so we have nothing to publish.
1234 					 * Set ipif_addr_ready so that this
1235 					 * address can be used locally for now.
1236 					 * The routing socket message will be
1237 					 * sent from ipif_up_done_v6().
1238 					 */
1239 					ipif->ipif_addr_ready = 1;
1240 					return (0);
1241 				}
1242 				added_ipif = B_TRUE;
1243 			}
1244 			hw_addr = bound_ill->ill_nd_lla;
1245 			hw_addr_len = bound_ill->ill_phys_addr_length;
1246 		} else {
1247 			bound_ill = ill;
1248 			hw_addr = ill->ill_nd_lla;
1249 			hw_addr_len = ill->ill_phys_addr_length;
1250 		}
1251 
1252 		/*
1253 		 * If this is an initial bring-up (or the ipif was never
1254 		 * completely brought up), do DAD.  Otherwise, we're here
1255 		 * because IPMP has rebound an address to this ill: send
1256 		 * unsolicited advertisements to inform others.
1257 		 */
1258 		if (initial || !ipif->ipif_addr_ready) {
1259 			/* Causes Duplicate Address Detection to run */
1260 			state = ND_PROBE;
1261 		} else {
1262 			state = ND_REACHABLE;
1263 			flags |= NCE_F_UNSOL_ADV;
1264 		}
1265 
1266 retry:
1267 		err = nce_lookup_then_add_v6(ill, hw_addr, hw_addr_len,
1268 		    &ipif->ipif_v6lcl_addr, flags, state, &nce);
1269 		switch (err) {
1270 		case 0:
1271 			ip1dbg(("ipif_ndp_up: NCE created for %s\n",
1272 			    ill->ill_name));
1273 			ipif->ipif_addr_ready = 1;
1274 			ipif->ipif_added_nce = 1;
1275 			nce->nce_ipif_cnt++;
1276 			break;
1277 		case EINPROGRESS:
1278 			ip1dbg(("ipif_ndp_up: running DAD now for %s\n",
1279 			    ill->ill_name));
1280 			ipif->ipif_added_nce = 1;
1281 			nce->nce_ipif_cnt++;
1282 			break;
1283 		case EEXIST:
1284 			ip1dbg(("ipif_ndp_up: NCE already exists for %s\n",
1285 			    ill->ill_name));
1286 			if (!NCE_MYADDR(nce->nce_common)) {
1287 				/*
1288 				 * A leftover nce from before this address
1289 				 * existed
1290 				 */
1291 				ncec_delete(nce->nce_common);
1292 				nce_refrele(nce);
1293 				nce = NULL;
1294 				goto retry;
1295 			}
1296 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
1297 				nce_refrele(nce);
1298 				nce = NULL;
1299 				ip1dbg(("ipif_ndp_up: NCE already exists "
1300 				    "for %s\n", ill->ill_name));
1301 				goto fail;
1302 			}
1303 			/*
1304 			 * Duplicate local addresses are permissible for
1305 			 * IPIF_POINTOPOINT interfaces which will get marked
1306 			 * IPIF_UNNUMBERED later in
1307 			 * ip_addr_availability_check().
1308 			 *
1309 			 * The nce_ipif_cnt field tracks the number of
1310 			 * ipifs that have nce_addr as their local address.
1311 			 */
1312 			ipif->ipif_addr_ready = 1;
1313 			ipif->ipif_added_nce = 1;
1314 			nce->nce_ipif_cnt++;
1315 			err = 0;
1316 			break;
1317 		default:
1318 			ip1dbg(("ipif_ndp_up: NCE creation failed for %s\n",
1319 			    ill->ill_name));
1320 			goto fail;
1321 		}
1322 	} else {
1323 		/* No local NCE for this entry */
1324 		ipif->ipif_addr_ready = 1;
1325 	}
1326 	if (nce != NULL)
1327 		nce_refrele(nce);
1328 	return (0);
1329 fail:
1330 	if (added_ipif)
1331 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
1332 
1333 	return (err);
1334 }
1335 
1336 /* Remove all cache entries for this logical interface */
1337 void
1338 ipif_ndp_down(ipif_t *ipif)
1339 {
1340 	ipif_nce_down(ipif);
1341 }
1342 
1343 /*
1344  * Return the scope of the given IPv6 address.  If the address is an
1345  * IPv4 mapped IPv6 address, return the scope of the corresponding
1346  * IPv4 address.
1347  */
1348 in6addr_scope_t
1349 ip_addr_scope_v6(const in6_addr_t *addr)
1350 {
1351 	static in6_addr_t ipv6loopback = IN6ADDR_LOOPBACK_INIT;
1352 
1353 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
1354 		in_addr_t v4addr_h = ntohl(V4_PART_OF_V6((*addr)));
1355 		if ((v4addr_h >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
1356 		    (v4addr_h & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
1357 			return (IP6_SCOPE_LINKLOCAL);
1358 		if ((v4addr_h & IN_PRIVATE8_MASK) == IN_PRIVATE8_NET ||
1359 		    (v4addr_h & IN_PRIVATE12_MASK) == IN_PRIVATE12_NET ||
1360 		    (v4addr_h & IN_PRIVATE16_MASK) == IN_PRIVATE16_NET)
1361 			return (IP6_SCOPE_SITELOCAL);
1362 		return (IP6_SCOPE_GLOBAL);
1363 	}
1364 
1365 	if (IN6_IS_ADDR_MULTICAST(addr))
1366 		return (IN6_ADDR_MC_SCOPE(addr));
1367 
1368 	/* link-local and loopback addresses are of link-local scope */
1369 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
1370 	    IN6_ARE_ADDR_EQUAL(addr, &ipv6loopback))
1371 		return (IP6_SCOPE_LINKLOCAL);
1372 	if (IN6_IS_ADDR_SITELOCAL(addr))
1373 		return (IP6_SCOPE_SITELOCAL);
1374 	return (IP6_SCOPE_GLOBAL);
1375 }
1376 
1377 
1378 /*
1379  * Returns the length of the common prefix of a1 and a2, as per
1380  * CommonPrefixLen() defined in RFC 3484.
1381  */
1382 static int
1383 ip_common_prefix_v6(const in6_addr_t *a1, const in6_addr_t *a2)
1384 {
1385 	int i;
1386 	uint32_t a1val, a2val, mask;
1387 
1388 	for (i = 0; i < 4; i++) {
1389 		if ((a1val = a1->s6_addr32[i]) != (a2val = a2->s6_addr32[i])) {
1390 			a1val ^= a2val;
1391 			i *= 32;
1392 			mask = 0x80000000u;
1393 			while (!(a1val & mask)) {
1394 				mask >>= 1;
1395 				i++;
1396 			}
1397 			return (i);
1398 		}
1399 	}
1400 	return (IPV6_ABITS);
1401 }
1402 
1403 #define	IPIF_VALID_IPV6_SOURCE(ipif) \
1404 	(((ipif)->ipif_flags & IPIF_UP) && \
1405 	!((ipif)->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) && \
1406 	!((ipif)->ipif_ill->ill_flags & ILLF_NOACCEPT))
1407 
1408 /* source address candidate */
1409 typedef struct candidate {
1410 	ipif_t		*cand_ipif;
1411 	/* The properties of this candidate */
1412 	boolean_t	cand_isdst;
1413 	boolean_t	cand_isdst_set;
1414 	in6addr_scope_t	cand_scope;
1415 	boolean_t	cand_scope_set;
1416 	boolean_t	cand_isdeprecated;
1417 	boolean_t	cand_isdeprecated_set;
1418 	boolean_t	cand_ispreferred;
1419 	boolean_t	cand_ispreferred_set;
1420 	boolean_t	cand_matchedinterface;
1421 	boolean_t	cand_matchedinterface_set;
1422 	boolean_t	cand_matchedlabel;
1423 	boolean_t	cand_matchedlabel_set;
1424 	boolean_t	cand_istmp;
1425 	boolean_t	cand_istmp_set;
1426 	int		cand_common_pref;
1427 	boolean_t	cand_common_pref_set;
1428 	boolean_t	cand_pref_eq;
1429 	boolean_t	cand_pref_eq_set;
1430 	int		cand_pref_len;
1431 	boolean_t	cand_pref_len_set;
1432 } cand_t;
1433 #define	cand_srcaddr	cand_ipif->ipif_v6lcl_addr
1434 #define	cand_mask	cand_ipif->ipif_v6net_mask
1435 #define	cand_flags	cand_ipif->ipif_flags
1436 #define	cand_ill	cand_ipif->ipif_ill
1437 #define	cand_zoneid	cand_ipif->ipif_zoneid
1438 
1439 /* information about the destination for source address selection */
1440 typedef struct dstinfo {
1441 	const in6_addr_t	*dst_addr;
1442 	ill_t			*dst_ill;
1443 	uint_t			dst_restrict_ill;
1444 	boolean_t		dst_prefer_src_tmp;
1445 	in6addr_scope_t		dst_scope;
1446 	char			*dst_label;
1447 } dstinfo_t;
1448 
1449 /*
1450  * The following functions are rules used to select a source address in
1451  * ipif_select_source_v6().  Each rule compares a current candidate (cc)
1452  * against the best candidate (bc).  Each rule has three possible outcomes;
1453  * the candidate is preferred over the best candidate (CAND_PREFER), the
1454  * candidate is not preferred over the best candidate (CAND_AVOID), or the
1455  * candidate is of equal value as the best candidate (CAND_TIE).
1456  *
1457  * These rules are part of a greater "Default Address Selection for IPv6"
1458  * sheme, which is standards based work coming out of the IETF ipv6 working
1459  * group.  The IETF document defines both IPv6 source address selection and
1460  * destination address ordering.  The rules defined here implement the IPv6
1461  * source address selection.  Destination address ordering is done by
1462  * libnsl, and uses a similar set of rules to implement the sorting.
1463  *
1464  * Most of the rules are defined by the RFC and are not typically altered.  The
1465  * last rule, number 8, has language that allows for local preferences.  In the
1466  * scheme below, this means that new Solaris rules should normally go between
1467  * rule_ifprefix and rule_prefix.
1468  */
1469 typedef enum {CAND_AVOID, CAND_TIE, CAND_PREFER} rule_res_t;
1470 typedef	rule_res_t (*rulef_t)(cand_t *, cand_t *, const dstinfo_t *,
1471     ip_stack_t *);
1472 
1473 /* Prefer an address if it is equal to the destination address. */
1474 /* ARGSUSED3 */
1475 static rule_res_t
1476 rule_isdst(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
1477 {
1478 	if (!bc->cand_isdst_set) {
1479 		bc->cand_isdst =
1480 		    IN6_ARE_ADDR_EQUAL(&bc->cand_srcaddr, dstinfo->dst_addr);
1481 		bc->cand_isdst_set = B_TRUE;
1482 	}
1483 
1484 	cc->cand_isdst =
1485 	    IN6_ARE_ADDR_EQUAL(&cc->cand_srcaddr, dstinfo->dst_addr);
1486 	cc->cand_isdst_set = B_TRUE;
1487 
1488 	if (cc->cand_isdst == bc->cand_isdst)
1489 		return (CAND_TIE);
1490 	else if (cc->cand_isdst)
1491 		return (CAND_PREFER);
1492 	else
1493 		return (CAND_AVOID);
1494 }
1495 
1496 /*
1497  * Prefer addresses that are of closest scope to the destination.  Always
1498  * prefer addresses that are of greater scope than the destination over
1499  * those that are of lesser scope than the destination.
1500  */
1501 /* ARGSUSED3 */
1502 static rule_res_t
1503 rule_scope(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
1504 {
1505 	if (!bc->cand_scope_set) {
1506 		bc->cand_scope = ip_addr_scope_v6(&bc->cand_srcaddr);
1507 		bc->cand_scope_set = B_TRUE;
1508 	}
1509 
1510 	cc->cand_scope = ip_addr_scope_v6(&cc->cand_srcaddr);
1511 	cc->cand_scope_set = B_TRUE;
1512 
1513 	if (cc->cand_scope < bc->cand_scope) {
1514 		if (cc->cand_scope < dstinfo->dst_scope)
1515 			return (CAND_AVOID);
1516 		else
1517 			return (CAND_PREFER);
1518 	} else if (bc->cand_scope < cc->cand_scope) {
1519 		if (bc->cand_scope < dstinfo->dst_scope)
1520 			return (CAND_PREFER);
1521 		else
1522 			return (CAND_AVOID);
1523 	} else {
1524 		return (CAND_TIE);
1525 	}
1526 }
1527 
1528 /*
1529  * Prefer non-deprecated source addresses.
1530  */
1531 /* ARGSUSED2 */
1532 static rule_res_t
1533 rule_deprecated(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1534     ip_stack_t *ipst)
1535 {
1536 	if (!bc->cand_isdeprecated_set) {
1537 		bc->cand_isdeprecated =
1538 		    ((bc->cand_flags & IPIF_DEPRECATED) != 0);
1539 		bc->cand_isdeprecated_set = B_TRUE;
1540 	}
1541 
1542 	cc->cand_isdeprecated = ((cc->cand_flags & IPIF_DEPRECATED) != 0);
1543 	cc->cand_isdeprecated_set = B_TRUE;
1544 
1545 	if (bc->cand_isdeprecated == cc->cand_isdeprecated)
1546 		return (CAND_TIE);
1547 	else if (cc->cand_isdeprecated)
1548 		return (CAND_AVOID);
1549 	else
1550 		return (CAND_PREFER);
1551 }
1552 
1553 /*
1554  * Prefer source addresses that have the IPIF_PREFERRED flag set.  This
1555  * rule must be before rule_interface because the flag could be set on any
1556  * interface, not just the interface being used for outgoing packets (for
1557  * example, the IFF_PREFERRED could be set on an address assigned to the
1558  * loopback interface).
1559  */
1560 /* ARGSUSED2 */
1561 static rule_res_t
1562 rule_preferred(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1563     ip_stack_t *ipst)
1564 {
1565 	if (!bc->cand_ispreferred_set) {
1566 		bc->cand_ispreferred = ((bc->cand_flags & IPIF_PREFERRED) != 0);
1567 		bc->cand_ispreferred_set = B_TRUE;
1568 	}
1569 
1570 	cc->cand_ispreferred = ((cc->cand_flags & IPIF_PREFERRED) != 0);
1571 	cc->cand_ispreferred_set = B_TRUE;
1572 
1573 	if (bc->cand_ispreferred == cc->cand_ispreferred)
1574 		return (CAND_TIE);
1575 	else if (cc->cand_ispreferred)
1576 		return (CAND_PREFER);
1577 	else
1578 		return (CAND_AVOID);
1579 }
1580 
1581 /*
1582  * Prefer source addresses that are assigned to the outgoing interface.
1583  */
1584 /* ARGSUSED3 */
1585 static rule_res_t
1586 rule_interface(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1587     ip_stack_t *ipst)
1588 {
1589 	ill_t *dstill = dstinfo->dst_ill;
1590 
1591 	/*
1592 	 * If dstinfo->dst_restrict_ill is set, this rule is unnecessary
1593 	 * since we know all candidates will be on the same link.
1594 	 */
1595 	if (dstinfo->dst_restrict_ill)
1596 		return (CAND_TIE);
1597 
1598 	if (!bc->cand_matchedinterface_set) {
1599 		bc->cand_matchedinterface = bc->cand_ill == dstill;
1600 		bc->cand_matchedinterface_set = B_TRUE;
1601 	}
1602 
1603 	cc->cand_matchedinterface = cc->cand_ill == dstill;
1604 	cc->cand_matchedinterface_set = B_TRUE;
1605 
1606 	if (bc->cand_matchedinterface == cc->cand_matchedinterface)
1607 		return (CAND_TIE);
1608 	else if (cc->cand_matchedinterface)
1609 		return (CAND_PREFER);
1610 	else
1611 		return (CAND_AVOID);
1612 }
1613 
1614 /*
1615  * Prefer source addresses whose label matches the destination's label.
1616  */
1617 static rule_res_t
1618 rule_label(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
1619 {
1620 	char *label;
1621 
1622 	if (!bc->cand_matchedlabel_set) {
1623 		label = ip6_asp_lookup(&bc->cand_srcaddr, NULL, ipst);
1624 		bc->cand_matchedlabel =
1625 		    ip6_asp_labelcmp(label, dstinfo->dst_label);
1626 		bc->cand_matchedlabel_set = B_TRUE;
1627 	}
1628 
1629 	label = ip6_asp_lookup(&cc->cand_srcaddr, NULL, ipst);
1630 	cc->cand_matchedlabel = ip6_asp_labelcmp(label, dstinfo->dst_label);
1631 	cc->cand_matchedlabel_set = B_TRUE;
1632 
1633 	if (bc->cand_matchedlabel == cc->cand_matchedlabel)
1634 		return (CAND_TIE);
1635 	else if (cc->cand_matchedlabel)
1636 		return (CAND_PREFER);
1637 	else
1638 		return (CAND_AVOID);
1639 }
1640 
1641 /*
1642  * Prefer public addresses over temporary ones.  An application can reverse
1643  * the logic of this rule and prefer temporary addresses by using the
1644  * IPV6_SRC_PREFERENCES socket option.
1645  */
1646 /* ARGSUSED3 */
1647 static rule_res_t
1648 rule_temporary(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1649     ip_stack_t *ipst)
1650 {
1651 	if (!bc->cand_istmp_set) {
1652 		bc->cand_istmp = ((bc->cand_flags & IPIF_TEMPORARY) != 0);
1653 		bc->cand_istmp_set = B_TRUE;
1654 	}
1655 
1656 	cc->cand_istmp = ((cc->cand_flags & IPIF_TEMPORARY) != 0);
1657 	cc->cand_istmp_set = B_TRUE;
1658 
1659 	if (bc->cand_istmp == cc->cand_istmp)
1660 		return (CAND_TIE);
1661 
1662 	if (dstinfo->dst_prefer_src_tmp && cc->cand_istmp)
1663 		return (CAND_PREFER);
1664 	else if (!dstinfo->dst_prefer_src_tmp && !cc->cand_istmp)
1665 		return (CAND_PREFER);
1666 	else
1667 		return (CAND_AVOID);
1668 }
1669 
1670 /*
1671  * Prefer source addresses with longer matching prefix with the destination
1672  * under the interface mask.  This gets us on the same subnet before applying
1673  * any Solaris-specific rules.
1674  */
1675 /* ARGSUSED3 */
1676 static rule_res_t
1677 rule_ifprefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1678     ip_stack_t *ipst)
1679 {
1680 	if (!bc->cand_pref_eq_set) {
1681 		bc->cand_pref_eq = V6_MASK_EQ_2(bc->cand_srcaddr,
1682 		    bc->cand_mask, *dstinfo->dst_addr);
1683 		bc->cand_pref_eq_set = B_TRUE;
1684 	}
1685 
1686 	cc->cand_pref_eq = V6_MASK_EQ_2(cc->cand_srcaddr, cc->cand_mask,
1687 	    *dstinfo->dst_addr);
1688 	cc->cand_pref_eq_set = B_TRUE;
1689 
1690 	if (bc->cand_pref_eq) {
1691 		if (cc->cand_pref_eq) {
1692 			if (!bc->cand_pref_len_set) {
1693 				bc->cand_pref_len =
1694 				    ip_mask_to_plen_v6(&bc->cand_mask);
1695 				bc->cand_pref_len_set = B_TRUE;
1696 			}
1697 			cc->cand_pref_len = ip_mask_to_plen_v6(&cc->cand_mask);
1698 			cc->cand_pref_len_set = B_TRUE;
1699 			if (bc->cand_pref_len == cc->cand_pref_len)
1700 				return (CAND_TIE);
1701 			else if (bc->cand_pref_len > cc->cand_pref_len)
1702 				return (CAND_AVOID);
1703 			else
1704 				return (CAND_PREFER);
1705 		} else {
1706 			return (CAND_AVOID);
1707 		}
1708 	} else {
1709 		if (cc->cand_pref_eq)
1710 			return (CAND_PREFER);
1711 		else
1712 			return (CAND_TIE);
1713 	}
1714 }
1715 
1716 /*
1717  * Prefer to use zone-specific addresses when possible instead of all-zones
1718  * addresses.
1719  */
1720 /* ARGSUSED2 */
1721 static rule_res_t
1722 rule_zone_specific(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1723     ip_stack_t *ipst)
1724 {
1725 	if ((bc->cand_zoneid == ALL_ZONES) ==
1726 	    (cc->cand_zoneid == ALL_ZONES))
1727 		return (CAND_TIE);
1728 	else if (cc->cand_zoneid == ALL_ZONES)
1729 		return (CAND_AVOID);
1730 	else
1731 		return (CAND_PREFER);
1732 }
1733 
1734 /*
1735  * Prefer to use DHCPv6 (first) and static addresses (second) when possible
1736  * instead of statelessly autoconfigured addresses.
1737  *
1738  * This is done after trying all other preferences (and before the final tie
1739  * breaker) so that, if all else is equal, we select addresses configured by
1740  * DHCPv6 over other addresses.  We presume that DHCPv6 addresses, unlike
1741  * stateless autoconfigured addresses, are deliberately configured by an
1742  * administrator, and thus are correctly set up in DNS and network packet
1743  * filters.
1744  */
1745 /* ARGSUSED2 */
1746 static rule_res_t
1747 rule_addr_type(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1748     ip_stack_t *ipst)
1749 {
1750 #define	ATYPE(x)	\
1751 	((x) & IPIF_DHCPRUNNING) ? 1 : ((x) & IPIF_ADDRCONF) ? 3 : 2
1752 	int bcval = ATYPE(bc->cand_flags);
1753 	int ccval = ATYPE(cc->cand_flags);
1754 #undef ATYPE
1755 
1756 	if (bcval == ccval)
1757 		return (CAND_TIE);
1758 	else if (ccval < bcval)
1759 		return (CAND_PREFER);
1760 	else
1761 		return (CAND_AVOID);
1762 }
1763 
1764 /*
1765  * Prefer source addresses with longer matching prefix with the destination.
1766  * We do the longest matching prefix calculation by doing an xor of both
1767  * addresses with the destination, and pick the address with the longest string
1768  * of leading zeros, as per CommonPrefixLen() defined in RFC 3484.
1769  */
1770 /* ARGSUSED3 */
1771 static rule_res_t
1772 rule_prefix(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo, ip_stack_t *ipst)
1773 {
1774 	if (!bc->cand_common_pref_set) {
1775 		bc->cand_common_pref = ip_common_prefix_v6(&bc->cand_srcaddr,
1776 		    dstinfo->dst_addr);
1777 		bc->cand_common_pref_set = B_TRUE;
1778 	}
1779 
1780 	cc->cand_common_pref = ip_common_prefix_v6(&cc->cand_srcaddr,
1781 	    dstinfo->dst_addr);
1782 	cc->cand_common_pref_set = B_TRUE;
1783 
1784 	if (bc->cand_common_pref == cc->cand_common_pref)
1785 		return (CAND_TIE);
1786 	else if (bc->cand_common_pref > cc->cand_common_pref)
1787 		return (CAND_AVOID);
1788 	else
1789 		return (CAND_PREFER);
1790 }
1791 
1792 /*
1793  * Last rule: we must pick something, so just prefer the current best
1794  * candidate.
1795  */
1796 /* ARGSUSED */
1797 static rule_res_t
1798 rule_must_be_last(cand_t *bc, cand_t *cc, const dstinfo_t *dstinfo,
1799     ip_stack_t *ipst)
1800 {
1801 	return (CAND_AVOID);
1802 }
1803 
1804 /*
1805  * Determine the best source address given a destination address and a
1806  * destination ill.  If no suitable source address is found, it returns
1807  * NULL. If there is a usable address pointed to by the usesrc
1808  * (i.e ill_usesrc_ifindex != 0) then return that first since it is more
1809  * fine grained (i.e per interface)
1810  *
1811  * This implementation is based on the "Default Address Selection for IPv6"
1812  * specification produced by the IETF IPv6 working group.  It has been
1813  * implemented so that the list of addresses is only traversed once (the
1814  * specification's algorithm could traverse the list of addresses once for
1815  * every rule).
1816  *
1817  * The restrict_ill argument restricts the algorithm to choose a source
1818  * address that is assigned to the destination ill.  This is used when
1819  * the destination address is a link-local or multicast address, and when
1820  * ipv6_strict_dst_multihoming is turned on.
1821  *
1822  * src_prefs is the caller's set of source address preferences.  If source
1823  * address selection is being called to determine the source address of a
1824  * connected socket (from ip_set_destination_v6()), then the preferences are
1825  * taken from conn_ixa->ixa_src_preferences.  These preferences can be set on a
1826  * per-socket basis using the IPV6_SRC_PREFERENCES socket option.  The only
1827  * preference currently implemented is for rfc3041 temporary addresses.
1828  */
1829 ipif_t *
1830 ipif_select_source_v6(ill_t *dstill, const in6_addr_t *dst,
1831     boolean_t restrict_ill, uint32_t src_prefs, zoneid_t zoneid,
1832     boolean_t allow_usesrc, boolean_t *notreadyp)
1833 {
1834 	dstinfo_t	dstinfo;
1835 	char		dstr[INET6_ADDRSTRLEN];
1836 	char		sstr[INET6_ADDRSTRLEN];
1837 	ipif_t		*ipif, *start_ipif, *next_ipif;
1838 	ill_t		*ill, *usesrc_ill = NULL, *ipmp_ill = NULL;
1839 	ill_walk_context_t	ctx;
1840 	cand_t		best_c;	/* The best candidate */
1841 	cand_t		curr_c;	/* The current candidate */
1842 	uint_t		index;
1843 	boolean_t	first_candidate = B_TRUE;
1844 	rule_res_t	rule_result;
1845 	tsol_tpc_t	*src_rhtp, *dst_rhtp;
1846 	ip_stack_t	*ipst = dstill->ill_ipst;
1847 
1848 	/*
1849 	 * The list of ordering rules.  They are applied in the order they
1850 	 * appear in the list.
1851 	 *
1852 	 * Solaris doesn't currently support Mobile IPv6, so there's no
1853 	 * rule_mipv6 corresponding to rule 4 in the specification.
1854 	 */
1855 	rulef_t	rules[] = {
1856 		rule_isdst,
1857 		rule_scope,
1858 		rule_deprecated,
1859 		rule_preferred,
1860 		rule_interface,
1861 		rule_label,
1862 		rule_temporary,
1863 		rule_ifprefix,			/* local rules after this */
1864 		rule_zone_specific,
1865 		rule_addr_type,
1866 		rule_prefix,			/* local rules before this */
1867 		rule_must_be_last,		/* must always be last */
1868 		NULL
1869 	};
1870 
1871 	ASSERT(dstill->ill_isv6);
1872 	ASSERT(!IN6_IS_ADDR_V4MAPPED(dst));
1873 
1874 	/*
1875 	 * Check if there is a usable src address pointed to by the
1876 	 * usesrc ifindex. This has higher precedence since it is
1877 	 * finer grained (i.e per interface) v/s being system wide.
1878 	 */
1879 	if (dstill->ill_usesrc_ifindex != 0 && allow_usesrc) {
1880 		if ((usesrc_ill =
1881 		    ill_lookup_on_ifindex(dstill->ill_usesrc_ifindex, B_TRUE,
1882 		    ipst)) != NULL) {
1883 			dstinfo.dst_ill = usesrc_ill;
1884 		} else {
1885 			return (NULL);
1886 		}
1887 	} else if (IS_UNDER_IPMP(dstill)) {
1888 		/*
1889 		 * Test addresses should never be used for source address
1890 		 * selection, so if we were passed an underlying ill, switch
1891 		 * to the IPMP meta-interface.
1892 		 */
1893 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(dstill)) != NULL)
1894 			dstinfo.dst_ill = ipmp_ill;
1895 		else
1896 			return (NULL);
1897 	} else {
1898 		dstinfo.dst_ill = dstill;
1899 	}
1900 
1901 	/*
1902 	 * If we're dealing with an unlabeled destination on a labeled system,
1903 	 * make sure that we ignore source addresses that are incompatible with
1904 	 * the destination's default label.  That destination's default label
1905 	 * must dominate the minimum label on the source address.
1906 	 *
1907 	 * (Note that this has to do with Trusted Solaris.  It's not related to
1908 	 * the labels described by ip6_asp_lookup.)
1909 	 */
1910 	dst_rhtp = NULL;
1911 	if (is_system_labeled()) {
1912 		dst_rhtp = find_tpc(dst, IPV6_VERSION, B_FALSE);
1913 		if (dst_rhtp == NULL)
1914 			return (NULL);
1915 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
1916 			TPC_RELE(dst_rhtp);
1917 			dst_rhtp = NULL;
1918 		}
1919 	}
1920 
1921 	dstinfo.dst_addr = dst;
1922 	dstinfo.dst_scope = ip_addr_scope_v6(dst);
1923 	dstinfo.dst_label = ip6_asp_lookup(dst, NULL, ipst);
1924 	dstinfo.dst_prefer_src_tmp = ((src_prefs & IPV6_PREFER_SRC_TMP) != 0);
1925 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1926 	/*
1927 	 * Section three of the I-D states that for multicast and
1928 	 * link-local destinations, the candidate set must be restricted to
1929 	 * an interface that is on the same link as the outgoing interface.
1930 	 * Also, when ipv6_strict_dst_multihoming is turned on, always
1931 	 * restrict the source address to the destination link as doing
1932 	 * otherwise will almost certainly cause problems.
1933 	 */
1934 	if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst) ||
1935 	    ipst->ips_ipv6_strict_dst_multihoming || usesrc_ill != NULL) {
1936 		dstinfo.dst_restrict_ill = B_TRUE;
1937 	} else {
1938 		dstinfo.dst_restrict_ill = restrict_ill;
1939 	}
1940 
1941 	bzero(&best_c, sizeof (cand_t));
1942 
1943 	/*
1944 	 * Take a pass through the list of IPv6 interfaces to choose the best
1945 	 * possible source address.  If restrict_ill is set, just use dst_ill.
1946 	 */
1947 	if (dstinfo.dst_restrict_ill)
1948 		ill = dstinfo.dst_ill;
1949 	else
1950 		ill = ILL_START_WALK_V6(&ctx, ipst);
1951 
1952 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
1953 		ASSERT(ill->ill_isv6);
1954 
1955 		/*
1956 		 * Test addresses should never be used for source address
1957 		 * selection, so ignore underlying ills.
1958 		 */
1959 		if (IS_UNDER_IPMP(ill))
1960 			continue;
1961 
1962 		if (ill->ill_ipif == NULL)
1963 			continue;
1964 		/*
1965 		 * For source address selection, we treat the ipif list as
1966 		 * circular and continue until we get back to where we
1967 		 * started.  This allows IPMP to vary source address selection
1968 		 * (which improves inbound load spreading) by caching its last
1969 		 * ending point and starting from there.  NOTE: we don't have
1970 		 * to worry about ill_src_ipif changing ills since that can't
1971 		 * happen on the IPMP ill.
1972 		 */
1973 		start_ipif = ill->ill_ipif;
1974 		if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
1975 			start_ipif = ill->ill_src_ipif;
1976 
1977 		ipif = start_ipif;
1978 		do {
1979 			if ((next_ipif = ipif->ipif_next) == NULL)
1980 				next_ipif = ill->ill_ipif;
1981 
1982 			if (!IPIF_VALID_IPV6_SOURCE(ipif))
1983 				continue;
1984 
1985 			if (!ipif->ipif_addr_ready) {
1986 				if (notreadyp != NULL)
1987 					*notreadyp = B_TRUE;
1988 				continue;
1989 			}
1990 
1991 			if (zoneid != ALL_ZONES &&
1992 			    ipif->ipif_zoneid != zoneid &&
1993 			    ipif->ipif_zoneid != ALL_ZONES)
1994 				continue;
1995 
1996 			/*
1997 			 * Check compatibility of local address for
1998 			 * destination's default label if we're on a labeled
1999 			 * system.  Incompatible addresses can't be used at
2000 			 * all and must be skipped over.
2001 			 */
2002 			if (dst_rhtp != NULL) {
2003 				boolean_t incompat;
2004 
2005 				src_rhtp = find_tpc(&ipif->ipif_v6lcl_addr,
2006 				    IPV6_VERSION, B_FALSE);
2007 				if (src_rhtp == NULL)
2008 					continue;
2009 				incompat =
2010 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
2011 				    src_rhtp->tpc_tp.tp_doi !=
2012 				    dst_rhtp->tpc_tp.tp_doi ||
2013 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
2014 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
2015 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
2016 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
2017 				TPC_RELE(src_rhtp);
2018 				if (incompat)
2019 					continue;
2020 			}
2021 
2022 			if (first_candidate) {
2023 				/*
2024 				 * This is first valid address in the list.
2025 				 * It is automatically the best candidate
2026 				 * so far.
2027 				 */
2028 				best_c.cand_ipif = ipif;
2029 				first_candidate = B_FALSE;
2030 				continue;
2031 			}
2032 
2033 			bzero(&curr_c, sizeof (cand_t));
2034 			curr_c.cand_ipif = ipif;
2035 
2036 			/*
2037 			 * Compare this current candidate (curr_c) with the
2038 			 * best candidate (best_c) by applying the
2039 			 * comparison rules in order until one breaks the
2040 			 * tie.
2041 			 */
2042 			for (index = 0; rules[index] != NULL; index++) {
2043 				/* Apply a comparison rule. */
2044 				rule_result = (rules[index])(&best_c, &curr_c,
2045 				    &dstinfo, ipst);
2046 				if (rule_result == CAND_AVOID) {
2047 					/*
2048 					 * The best candidate is still the
2049 					 * best candidate.  Forget about
2050 					 * this current candidate and go on
2051 					 * to the next one.
2052 					 */
2053 					break;
2054 				} else if (rule_result == CAND_PREFER) {
2055 					/*
2056 					 * This candidate is prefered.  It
2057 					 * becomes the best candidate so
2058 					 * far.  Go on to the next address.
2059 					 */
2060 					best_c = curr_c;
2061 					break;
2062 				}
2063 				/* We have a tie, apply the next rule. */
2064 			}
2065 
2066 			/*
2067 			 * The last rule must be a tie breaker rule and
2068 			 * must never produce a tie.  At this point, the
2069 			 * candidate should have either been rejected, or
2070 			 * have been prefered as the best candidate so far.
2071 			 */
2072 			ASSERT(rule_result != CAND_TIE);
2073 		} while ((ipif = next_ipif) != start_ipif);
2074 
2075 		/*
2076 		 * For IPMP, update the source ipif rotor to the next ipif,
2077 		 * provided we can look it up.  (We must not use it if it's
2078 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
2079 		 * ipif_free() checked ill_src_ipif.)
2080 		 */
2081 		if (IS_IPMP(ill) && ipif != NULL) {
2082 			mutex_enter(&ipif->ipif_ill->ill_lock);
2083 			next_ipif = ipif->ipif_next;
2084 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
2085 				ill->ill_src_ipif = next_ipif;
2086 			else
2087 				ill->ill_src_ipif = NULL;
2088 			mutex_exit(&ipif->ipif_ill->ill_lock);
2089 		}
2090 
2091 		/*
2092 		 * Only one ill to consider if dst_restrict_ill is set.
2093 		 */
2094 		if (dstinfo.dst_restrict_ill)
2095 			break;
2096 	}
2097 
2098 	ipif = best_c.cand_ipif;
2099 	ip1dbg(("ipif_select_source_v6(%s, %s) -> %s\n",
2100 	    dstinfo.dst_ill->ill_name,
2101 	    inet_ntop(AF_INET6, dstinfo.dst_addr, dstr, sizeof (dstr)),
2102 	    (ipif == NULL ? "NULL" :
2103 	    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr, sstr, sizeof (sstr)))));
2104 
2105 	if (usesrc_ill != NULL)
2106 		ill_refrele(usesrc_ill);
2107 
2108 	if (ipmp_ill != NULL)
2109 		ill_refrele(ipmp_ill);
2110 
2111 	if (dst_rhtp != NULL)
2112 		TPC_RELE(dst_rhtp);
2113 
2114 	if (ipif == NULL) {
2115 		rw_exit(&ipst->ips_ill_g_lock);
2116 		return (NULL);
2117 	}
2118 
2119 	mutex_enter(&ipif->ipif_ill->ill_lock);
2120 	if (!IPIF_IS_CONDEMNED(ipif)) {
2121 		ipif_refhold_locked(ipif);
2122 		mutex_exit(&ipif->ipif_ill->ill_lock);
2123 		rw_exit(&ipst->ips_ill_g_lock);
2124 		return (ipif);
2125 	}
2126 	mutex_exit(&ipif->ipif_ill->ill_lock);
2127 	rw_exit(&ipst->ips_ill_g_lock);
2128 	ip1dbg(("ipif_select_source_v6 cannot lookup ipif %p"
2129 	    " returning null \n", (void *)ipif));
2130 
2131 	return (NULL);
2132 }
2133 
2134 /*
2135  * Pick a source address based on the destination ill and an optional setsrc
2136  * address.
2137  * The result is stored in srcp. If generation is set, then put the source
2138  * generation number there before we look for the source address (to avoid
2139  * missing changes in the set of source addresses.
2140  * If flagsp is set, then us it to pass back ipif_flags.
2141  *
2142  * If the caller wants to cache the returned source address and detect when
2143  * that might be stale, the caller should pass in a generation argument,
2144  * which the caller can later compare against ips_src_generation
2145  *
2146  * The precedence order for selecting an IPv6 source address is:
2147  *  - RTF_SETSRC on the first ire in the recursive lookup always wins.
2148  *  - If usrsrc is set, swap the ill to be the usesrc one.
2149  *  - If IPMP is used on the ill, select a random address from the most
2150  *    preferred ones below:
2151  * That is followed by the long list of IPv6 source address selection rules
2152  * starting with rule_isdst(), rule_scope(), etc.
2153  *
2154  * We have lower preference for ALL_ZONES IP addresses,
2155  * as they pose problems with unlabeled destinations.
2156  *
2157  * Note that when multiple IP addresses match e.g., with rule_scope() we pick
2158  * the first one if IPMP is not in use. With IPMP we randomize.
2159  */
2160 int
2161 ip_select_source_v6(ill_t *ill, const in6_addr_t *setsrc, const in6_addr_t *dst,
2162     zoneid_t zoneid, ip_stack_t *ipst, uint_t restrict_ill, uint32_t src_prefs,
2163     in6_addr_t *srcp, uint32_t *generation, uint64_t *flagsp)
2164 {
2165 	ipif_t *ipif;
2166 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
2167 
2168 	if (flagsp != NULL)
2169 		*flagsp = 0;
2170 
2171 	/*
2172 	 * Need to grab the generation number before we check to
2173 	 * avoid a race with a change to the set of local addresses.
2174 	 * No lock needed since the thread which updates the set of local
2175 	 * addresses use ipif/ill locks and exit those (hence a store memory
2176 	 * barrier) before doing the atomic increase of ips_src_generation.
2177 	 */
2178 	if (generation != NULL) {
2179 		*generation = ipst->ips_src_generation;
2180 	}
2181 
2182 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
2183 	if (setsrc != NULL && !IN6_IS_ADDR_UNSPECIFIED(setsrc)) {
2184 		*srcp = *setsrc;
2185 		return (0);
2186 	}
2187 
2188 	ipif = ipif_select_source_v6(ill, dst, restrict_ill, src_prefs, zoneid,
2189 	    B_TRUE, &notready);
2190 	if (ipif == NULL) {
2191 		if (notready)
2192 			return (ENETDOWN);
2193 		else
2194 			return (EADDRNOTAVAIL);
2195 	}
2196 	*srcp = ipif->ipif_v6lcl_addr;
2197 	if (flagsp != NULL)
2198 		*flagsp = ipif->ipif_flags;
2199 	ipif_refrele(ipif);
2200 	return (0);
2201 }
2202 
2203 /*
2204  * Perform an attach and bind to get phys addr plus info_req for
2205  * the physical device.
2206  * q and mp represents an ioctl which will be queued waiting for
2207  * completion of the DLPI message exchange.
2208  * MUST be called on an ill queue.
2209  *
2210  * Returns EINPROGRESS when mp has been consumed by queueing it.
2211  * The ioctl will complete in ip_rput.
2212  */
2213 int
2214 ill_dl_phys(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
2215 {
2216 	mblk_t	*v6token_mp = NULL;
2217 	mblk_t	*v6lla_mp = NULL;
2218 	mblk_t	*dest_mp = NULL;
2219 	mblk_t	*phys_mp = NULL;
2220 	mblk_t	*info_mp = NULL;
2221 	mblk_t	*attach_mp = NULL;
2222 	mblk_t	*bind_mp = NULL;
2223 	mblk_t	*unbind_mp = NULL;
2224 	mblk_t	*notify_mp = NULL;
2225 	mblk_t  *capab_mp = NULL;
2226 
2227 	ip1dbg(("ill_dl_phys(%s:%u)\n", ill->ill_name, ipif->ipif_id));
2228 	ASSERT(ill->ill_dlpi_style_set);
2229 	ASSERT(WR(q)->q_next != NULL);
2230 
2231 	if (ill->ill_isv6) {
2232 		v6token_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2233 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2234 		if (v6token_mp == NULL)
2235 			goto bad;
2236 		((dl_phys_addr_req_t *)v6token_mp->b_rptr)->dl_addr_type =
2237 		    DL_IPV6_TOKEN;
2238 
2239 		v6lla_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2240 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2241 		if (v6lla_mp == NULL)
2242 			goto bad;
2243 		((dl_phys_addr_req_t *)v6lla_mp->b_rptr)->dl_addr_type =
2244 		    DL_IPV6_LINK_LAYER_ADDR;
2245 	}
2246 
2247 	if (ill->ill_mactype == DL_IPV4 || ill->ill_mactype == DL_IPV6) {
2248 		dest_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2249 		    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2250 		if (dest_mp == NULL)
2251 			goto bad;
2252 		((dl_phys_addr_req_t *)dest_mp->b_rptr)->dl_addr_type =
2253 		    DL_CURR_DEST_ADDR;
2254 	}
2255 
2256 	/*
2257 	 * Allocate a DL_NOTIFY_REQ and set the notifications we want.
2258 	 */
2259 	notify_mp = ip_dlpi_alloc(sizeof (dl_notify_req_t) + sizeof (long),
2260 	    DL_NOTIFY_REQ);
2261 	if (notify_mp == NULL)
2262 		goto bad;
2263 	((dl_notify_req_t *)notify_mp->b_rptr)->dl_notifications =
2264 	    (DL_NOTE_PHYS_ADDR | DL_NOTE_SDU_SIZE | DL_NOTE_FASTPATH_FLUSH |
2265 	    DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_CAPAB_RENEG |
2266 	    DL_NOTE_PROMISC_ON_PHYS | DL_NOTE_PROMISC_OFF_PHYS |
2267 	    DL_NOTE_REPLUMB);
2268 
2269 	phys_mp = ip_dlpi_alloc(sizeof (dl_phys_addr_req_t) +
2270 	    sizeof (t_scalar_t), DL_PHYS_ADDR_REQ);
2271 	if (phys_mp == NULL)
2272 		goto bad;
2273 	((dl_phys_addr_req_t *)phys_mp->b_rptr)->dl_addr_type =
2274 	    DL_CURR_PHYS_ADDR;
2275 
2276 	info_mp = ip_dlpi_alloc(
2277 	    sizeof (dl_info_req_t) + sizeof (dl_info_ack_t),
2278 	    DL_INFO_REQ);
2279 	if (info_mp == NULL)
2280 		goto bad;
2281 
2282 	ASSERT(ill->ill_dlpi_capab_state == IDCS_UNKNOWN);
2283 	capab_mp = ip_dlpi_alloc(sizeof (dl_capability_req_t),
2284 	    DL_CAPABILITY_REQ);
2285 	if (capab_mp == NULL)
2286 		goto bad;
2287 
2288 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
2289 	    DL_BIND_REQ);
2290 	if (bind_mp == NULL)
2291 		goto bad;
2292 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
2293 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
2294 
2295 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
2296 	if (unbind_mp == NULL)
2297 		goto bad;
2298 
2299 	/* If we need to attach, pre-alloc and initialize the mblk */
2300 	if (ill->ill_needs_attach) {
2301 		attach_mp = ip_dlpi_alloc(sizeof (dl_attach_req_t),
2302 		    DL_ATTACH_REQ);
2303 		if (attach_mp == NULL)
2304 			goto bad;
2305 		((dl_attach_req_t *)attach_mp->b_rptr)->dl_ppa = ill->ill_ppa;
2306 	}
2307 
2308 	/*
2309 	 * Here we are going to delay the ioctl ack until after
2310 	 * ACKs from DL_PHYS_ADDR_REQ. So need to save the
2311 	 * original ioctl message before sending the requests
2312 	 */
2313 	mutex_enter(&ill->ill_lock);
2314 	/* ipsq_pending_mp_add won't fail since we pass in a NULL connp */
2315 	(void) ipsq_pending_mp_add(NULL, ipif, ill->ill_wq, mp, 0);
2316 	/*
2317 	 * Set ill_phys_addr_pend to zero. It will be set to the addr_type of
2318 	 * the DL_PHYS_ADDR_REQ in ill_dlpi_send() and ill_dlpi_done(). It will
2319 	 * be used to track which DL_PHYS_ADDR_REQ is being ACK'd/NAK'd.
2320 	 */
2321 	ill->ill_phys_addr_pend = 0;
2322 	mutex_exit(&ill->ill_lock);
2323 
2324 	if (attach_mp != NULL) {
2325 		ip1dbg(("ill_dl_phys: attach\n"));
2326 		ill_dlpi_send(ill, attach_mp);
2327 	}
2328 	ill_dlpi_send(ill, bind_mp);
2329 	ill_dlpi_send(ill, info_mp);
2330 
2331 	/*
2332 	 * Send the capability request to get the VRRP capability information.
2333 	 */
2334 	ill_capability_send(ill, capab_mp);
2335 
2336 	if (v6token_mp != NULL)
2337 		ill_dlpi_send(ill, v6token_mp);
2338 	if (v6lla_mp != NULL)
2339 		ill_dlpi_send(ill, v6lla_mp);
2340 	if (dest_mp != NULL)
2341 		ill_dlpi_send(ill, dest_mp);
2342 	ill_dlpi_send(ill, phys_mp);
2343 	ill_dlpi_send(ill, notify_mp);
2344 	ill_dlpi_send(ill, unbind_mp);
2345 
2346 	/*
2347 	 * This operation will complete in ip_rput_dlpi_writer with either
2348 	 * a DL_PHYS_ADDR_ACK or DL_ERROR_ACK.
2349 	 */
2350 	return (EINPROGRESS);
2351 bad:
2352 	freemsg(v6token_mp);
2353 	freemsg(v6lla_mp);
2354 	freemsg(dest_mp);
2355 	freemsg(phys_mp);
2356 	freemsg(info_mp);
2357 	freemsg(attach_mp);
2358 	freemsg(bind_mp);
2359 	freemsg(capab_mp);
2360 	freemsg(unbind_mp);
2361 	freemsg(notify_mp);
2362 	return (ENOMEM);
2363 }
2364 
2365 /* Add room for tcp+ip headers */
2366 uint_t ip_loopback_mtu_v6plus = IP_LOOPBACK_MTU + IPV6_HDR_LEN + 20;
2367 
2368 /*
2369  * DLPI is up.
2370  * Create all the IREs associated with an interface bring up multicast.
2371  * Set the interface flag and finish other initialization
2372  * that potentially had to be differed to after DL_BIND_ACK.
2373  */
2374 int
2375 ipif_up_done_v6(ipif_t *ipif)
2376 {
2377 	ill_t	*ill = ipif->ipif_ill;
2378 	int	err;
2379 	boolean_t loopback = B_FALSE;
2380 
2381 	ip1dbg(("ipif_up_done_v6(%s:%u)\n",
2382 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
2383 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done_v6",
2384 	    ill_t *, ill, ipif_t *, ipif);
2385 
2386 	/* Check if this is a loopback interface */
2387 	if (ipif->ipif_ill->ill_wq == NULL)
2388 		loopback = B_TRUE;
2389 
2390 	ASSERT(ipif->ipif_isv6);
2391 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
2392 
2393 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
2394 		nce_t *loop_nce = NULL;
2395 		uint16_t flags = (NCE_F_MYADDR | NCE_F_NONUD | NCE_F_AUTHORITY);
2396 
2397 		/*
2398 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
2399 		 * ipif_lookup_on_name(), but in the case of zones we can have
2400 		 * several loopback addresses on lo0. So all the interfaces with
2401 		 * loopback addresses need to be marked IRE_LOOPBACK.
2402 		 */
2403 		if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, &ipv6_loopback))
2404 			ipif->ipif_ire_type = IRE_LOOPBACK;
2405 		else
2406 			ipif->ipif_ire_type = IRE_LOCAL;
2407 		if (ill->ill_net_type != IRE_LOOPBACK)
2408 			flags |= NCE_F_PUBLISH;
2409 		err = nce_lookup_then_add_v6(ill, NULL,
2410 		    ill->ill_phys_addr_length,
2411 		    &ipif->ipif_v6lcl_addr, flags, ND_REACHABLE, &loop_nce);
2412 
2413 		/* A shared-IP zone sees EEXIST for lo0:N */
2414 		if (err == 0 || err == EEXIST) {
2415 			ipif->ipif_added_nce = 1;
2416 			loop_nce->nce_ipif_cnt++;
2417 			nce_refrele(loop_nce);
2418 			err = 0;
2419 		} else {
2420 			ASSERT(loop_nce == NULL);
2421 			return (err);
2422 		}
2423 	}
2424 
2425 	err = ipif_add_ires_v6(ipif, loopback);
2426 	if (err != 0) {
2427 		/*
2428 		 * See comments about return value from
2429 		 * ipif_addr_availability_check() in ipif_add_ires_v6().
2430 		 */
2431 		if (err != EADDRINUSE) {
2432 			ipif_ndp_down(ipif);
2433 		} else {
2434 			/*
2435 			 * Make IPMP aware of the deleted ipif so that
2436 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
2437 			 * can be completed. Note that we do not want to
2438 			 * destroy the nce that was created on the ipmp_ill
2439 			 * for the active copy of the duplicate address in
2440 			 * use.
2441 			 */
2442 			if (IS_IPMP(ill))
2443 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
2444 			err = EADDRNOTAVAIL;
2445 		}
2446 		return (err);
2447 	}
2448 
2449 	if (ill->ill_ipif_up_count == 1 && !loopback) {
2450 		/* Recover any additional IREs entries for this ill */
2451 		(void) ill_recover_saved_ire(ill);
2452 	}
2453 
2454 	if (ill->ill_need_recover_multicast) {
2455 		/*
2456 		 * Need to recover all multicast memberships in the driver.
2457 		 * This had to be deferred until we had attached.
2458 		 */
2459 		ill_recover_multicast(ill);
2460 	}
2461 
2462 	if (ill->ill_ipif_up_count == 1) {
2463 		/*
2464 		 * Since the interface is now up, it may now be active.
2465 		 */
2466 		if (IS_UNDER_IPMP(ill))
2467 			ipmp_ill_refresh_active(ill);
2468 	}
2469 
2470 	/* Join the allhosts multicast address and the solicited node MC */
2471 	ipif_multicast_up(ipif);
2472 
2473 	/* Perhaps ilgs should use this ill */
2474 	update_conn_ill(NULL, ill->ill_ipst);
2475 
2476 	if (ipif->ipif_addr_ready)
2477 		ipif_up_notify(ipif);
2478 
2479 	return (0);
2480 }
2481 
2482 /*
2483  * Add the IREs associated with the ipif.
2484  * Those MUST be explicitly removed in ipif_delete_ires_v6.
2485  */
2486 static int
2487 ipif_add_ires_v6(ipif_t *ipif, boolean_t loopback)
2488 {
2489 	ill_t		*ill = ipif->ipif_ill;
2490 	ip_stack_t	*ipst = ill->ill_ipst;
2491 	in6_addr_t	v6addr;
2492 	in6_addr_t	route_mask;
2493 	int		err;
2494 	char		buf[INET6_ADDRSTRLEN];
2495 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
2496 	ire_t		*ire_if = NULL;
2497 
2498 	if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
2499 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
2500 
2501 		/*
2502 		 * If we're on a labeled system then make sure that zone-
2503 		 * private addresses have proper remote host database entries.
2504 		 */
2505 		if (is_system_labeled() &&
2506 		    ipif->ipif_ire_type != IRE_LOOPBACK) {
2507 			if (ip6opt_ls == 0) {
2508 				cmn_err(CE_WARN, "IPv6 not enabled "
2509 				    "via /etc/system");
2510 				return (EINVAL);
2511 			}
2512 			if (!tsol_check_interface_address(ipif))
2513 				return (EINVAL);
2514 		}
2515 
2516 		/* Register the source address for __sin6_src_id */
2517 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
2518 		    ipif->ipif_zoneid, ipst);
2519 		if (err != 0) {
2520 			ip0dbg(("ipif_add_ires_v6: srcid_insert %d\n", err));
2521 			return (err);
2522 		}
2523 		/*
2524 		 * If the interface address is set, create the LOCAL
2525 		 * or LOOPBACK IRE.
2526 		 */
2527 		ip1dbg(("ipif_add_ires_v6: creating IRE %d for %s\n",
2528 		    ipif->ipif_ire_type,
2529 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
2530 		    buf, sizeof (buf))));
2531 
2532 		ire_local = ire_create_v6(
2533 		    &ipif->ipif_v6lcl_addr,		/* dest address */
2534 		    &ipv6_all_ones,			/* mask */
2535 		    NULL,				/* no gateway */
2536 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
2537 		    ipif->ipif_ill,			/* interface */
2538 		    ipif->ipif_zoneid,
2539 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
2540 		    RTF_PRIVATE : 0) | RTF_KERNEL,
2541 		    NULL,
2542 		    ipst);
2543 		if (ire_local == NULL) {
2544 			ip1dbg(("ipif_up_done_v6: NULL ire_local\n"));
2545 			err = ENOMEM;
2546 			goto bad;
2547 		}
2548 	}
2549 
2550 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
2551 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
2552 	    !(IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet) &&
2553 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))) {
2554 		/* ipif_v6subnet is ipif_v6pp_dst_addr for pt-pt */
2555 		v6addr = ipif->ipif_v6subnet;
2556 
2557 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
2558 			route_mask = ipv6_all_ones;
2559 		} else {
2560 			route_mask = ipif->ipif_v6net_mask;
2561 		}
2562 
2563 		ip1dbg(("ipif_add_ires_v6: creating if IRE %d for %s\n",
2564 		    ill->ill_net_type,
2565 		    inet_ntop(AF_INET6, &v6addr, buf, sizeof (buf))));
2566 
2567 		ire_if = ire_create_v6(
2568 		    &v6addr,			/* dest pref */
2569 		    &route_mask,		/* mask */
2570 		    &ipif->ipif_v6lcl_addr,	/* gateway */
2571 		    ill->ill_net_type,		/* IF_[NO]RESOLVER */
2572 		    ipif->ipif_ill,
2573 		    ipif->ipif_zoneid,
2574 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
2575 		    RTF_PRIVATE : 0) | RTF_KERNEL,
2576 		    NULL,
2577 		    ipst);
2578 		if (ire_if == NULL) {
2579 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
2580 			err = ENOMEM;
2581 			goto bad;
2582 		}
2583 	}
2584 
2585 	/*
2586 	 * Need to atomically check for IP address availability under
2587 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
2588 	 * ills or new ipifs can be added while we are checking availability.
2589 	 */
2590 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2591 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
2592 	ill->ill_ipif_up_count++;
2593 	ipif->ipif_flags |= IPIF_UP;
2594 	err = ip_addr_availability_check(ipif);
2595 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
2596 	rw_exit(&ipst->ips_ill_g_lock);
2597 
2598 	if (err != 0) {
2599 		/*
2600 		 * Our address may already be up on the same ill. In this case,
2601 		 * the external resolver entry for our ipif replaced the one for
2602 		 * the other ipif. So we don't want to delete it (otherwise the
2603 		 * other ipif would be unable to send packets).
2604 		 * ip_addr_availability_check() identifies this case for us and
2605 		 * returns EADDRINUSE; Caller must  turn it into EADDRNOTAVAIL
2606 		 * which is the expected error code.
2607 		 *
2608 		 * Note that ipif_ndp_down() will only delete the nce in the
2609 		 * case when the nce_ipif_cnt drops to 0.
2610 		 */
2611 		ill->ill_ipif_up_count--;
2612 		ipif->ipif_flags &= ~IPIF_UP;
2613 		goto bad;
2614 	}
2615 
2616 	/*
2617 	 * Add in all newly created IREs.
2618 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
2619 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
2620 	 * a /128 route.
2621 	 */
2622 	if (ire_if != NULL) {
2623 		ire_if = ire_add(ire_if);
2624 		if (ire_if == NULL) {
2625 			err = ENOMEM;
2626 			goto bad2;
2627 		}
2628 #ifdef DEBUG
2629 		ire_refhold_notr(ire_if);
2630 		ire_refrele(ire_if);
2631 #endif
2632 	}
2633 	if (ire_local != NULL) {
2634 		ire_local = ire_add(ire_local);
2635 		if (ire_local == NULL) {
2636 			err = ENOMEM;
2637 			goto bad2;
2638 		}
2639 #ifdef DEBUG
2640 		ire_refhold_notr(ire_local);
2641 		ire_refrele(ire_local);
2642 #endif
2643 	}
2644 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2645 	if (ire_local != NULL)
2646 		ipif->ipif_ire_local = ire_local;
2647 	if (ire_if != NULL)
2648 		ipif->ipif_ire_if = ire_if;
2649 	rw_exit(&ipst->ips_ill_g_lock);
2650 	ire_local = NULL;
2651 	ire_if = NULL;
2652 
2653 	if (ipif->ipif_addr_ready)
2654 		ipif_up_notify(ipif);
2655 	return (0);
2656 
2657 bad2:
2658 	ill->ill_ipif_up_count--;
2659 	ipif->ipif_flags &= ~IPIF_UP;
2660 
2661 bad:
2662 	if (ire_local != NULL)
2663 		ire_delete(ire_local);
2664 	if (ire_if != NULL)
2665 		ire_delete(ire_if);
2666 
2667 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2668 	ire_local = ipif->ipif_ire_local;
2669 	ipif->ipif_ire_local = NULL;
2670 	ire_if = ipif->ipif_ire_if;
2671 	ipif->ipif_ire_if = NULL;
2672 	rw_exit(&ipst->ips_ill_g_lock);
2673 	if (ire_local != NULL) {
2674 		ire_delete(ire_local);
2675 		ire_refrele_notr(ire_local);
2676 	}
2677 	if (ire_if != NULL) {
2678 		ire_delete(ire_if);
2679 		ire_refrele_notr(ire_if);
2680 	}
2681 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
2682 
2683 	return (err);
2684 }
2685 
2686 /* Remove all the IREs created by ipif_add_ires_v6 */
2687 void
2688 ipif_delete_ires_v6(ipif_t *ipif)
2689 {
2690 	ill_t		*ill = ipif->ipif_ill;
2691 	ip_stack_t	*ipst = ill->ill_ipst;
2692 	ire_t		*ire;
2693 
2694 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2695 	ire = ipif->ipif_ire_local;
2696 	ipif->ipif_ire_local = NULL;
2697 	rw_exit(&ipst->ips_ill_g_lock);
2698 	if (ire != NULL) {
2699 		/*
2700 		 * Move count to ipif so we don't loose the count due to
2701 		 * a down/up dance.
2702 		 */
2703 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
2704 
2705 		ire_delete(ire);
2706 		ire_refrele_notr(ire);
2707 	}
2708 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2709 	ire = ipif->ipif_ire_if;
2710 	ipif->ipif_ire_if = NULL;
2711 	rw_exit(&ipst->ips_ill_g_lock);
2712 	if (ire != NULL) {
2713 		ire_delete(ire);
2714 		ire_refrele_notr(ire);
2715 	}
2716 }
2717 
2718 /*
2719  * Delete an ND entry if it exists.
2720  */
2721 /* ARGSUSED */
2722 int
2723 ip_siocdelndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2724     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
2725 {
2726 	sin6_t		*sin6;
2727 	struct lifreq	*lifr;
2728 	lif_nd_req_t	*lnr;
2729 	ill_t		*ill = ipif->ipif_ill;
2730 	nce_t		*nce;
2731 
2732 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
2733 	lnr = &lifr->lifr_nd;
2734 	/* Only allow for logical unit zero i.e. not on "le0:17" */
2735 	if (ipif->ipif_id != 0)
2736 		return (EINVAL);
2737 
2738 	if (!ipif->ipif_isv6)
2739 		return (EINVAL);
2740 
2741 	if (lnr->lnr_addr.ss_family != AF_INET6)
2742 		return (EAFNOSUPPORT);
2743 
2744 	sin6 = (sin6_t *)&lnr->lnr_addr;
2745 
2746 	/*
2747 	 * Since ND mappings must be consistent across an IPMP group, prohibit
2748 	 * deleting ND mappings on underlying interfaces.
2749 	 * Don't allow deletion of mappings for local addresses.
2750 	 */
2751 	if (IS_UNDER_IPMP(ill))
2752 		return (EPERM);
2753 
2754 	nce = nce_lookup_v6(ill, &sin6->sin6_addr);
2755 	if (nce == NULL)
2756 		return (ESRCH);
2757 
2758 	if (NCE_MYADDR(nce->nce_common)) {
2759 		nce_refrele(nce);
2760 		return (EPERM);
2761 	}
2762 
2763 	/*
2764 	 * delete the nce_common which will also delete the nces on any
2765 	 * under_ill in the case of ipmp.
2766 	 */
2767 	ncec_delete(nce->nce_common);
2768 	nce_refrele(nce);
2769 	return (0);
2770 }
2771 
2772 /*
2773  * Return nbr cache info.
2774  */
2775 /* ARGSUSED */
2776 int
2777 ip_siocqueryndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2778     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
2779 {
2780 	ill_t		*ill = ipif->ipif_ill;
2781 	struct lifreq	*lifr;
2782 	lif_nd_req_t	*lnr;
2783 
2784 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
2785 	lnr = &lifr->lifr_nd;
2786 	/* Only allow for logical unit zero i.e. not on "le0:17" */
2787 	if (ipif->ipif_id != 0)
2788 		return (EINVAL);
2789 
2790 	if (!ipif->ipif_isv6)
2791 		return (EINVAL);
2792 
2793 	if (lnr->lnr_addr.ss_family != AF_INET6)
2794 		return (EAFNOSUPPORT);
2795 
2796 	if (ill->ill_phys_addr_length > sizeof (lnr->lnr_hdw_addr))
2797 		return (EINVAL);
2798 
2799 	return (ndp_query(ill, lnr));
2800 }
2801 
2802 /*
2803  * Perform an update of the nd entry for the specified address.
2804  */
2805 /* ARGSUSED */
2806 int
2807 ip_siocsetndp_v6(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2808     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
2809 {
2810 	sin6_t		*sin6;
2811 	ill_t		*ill = ipif->ipif_ill;
2812 	struct	lifreq	*lifr;
2813 	lif_nd_req_t	*lnr;
2814 	ire_t		*ire;
2815 
2816 	lifr = (struct lifreq *)mp->b_cont->b_cont->b_rptr;
2817 	lnr = &lifr->lifr_nd;
2818 	/* Only allow for logical unit zero i.e. not on "le0:17" */
2819 	if (ipif->ipif_id != 0)
2820 		return (EINVAL);
2821 
2822 	if (!ipif->ipif_isv6)
2823 		return (EINVAL);
2824 
2825 	if (lnr->lnr_addr.ss_family != AF_INET6)
2826 		return (EAFNOSUPPORT);
2827 
2828 	sin6 = (sin6_t *)&lnr->lnr_addr;
2829 
2830 	/*
2831 	 * Since ND mappings must be consistent across an IPMP group, prohibit
2832 	 * updating ND mappings on underlying interfaces.  Also, since ND
2833 	 * mappings for IPMP data addresses are owned by IP itself, prohibit
2834 	 * updating them.
2835 	 */
2836 	if (IS_UNDER_IPMP(ill))
2837 		return (EPERM);
2838 
2839 	if (IS_IPMP(ill)) {
2840 		ire = ire_ftable_lookup_v6(&sin6->sin6_addr, NULL, NULL,
2841 		    IRE_LOCAL, ill, ALL_ZONES, NULL,
2842 		    MATCH_IRE_TYPE | MATCH_IRE_ILL, 0, ill->ill_ipst, NULL);
2843 		if (ire != NULL) {
2844 			ire_refrele(ire);
2845 			return (EPERM);
2846 		}
2847 	}
2848 
2849 	return (ndp_sioc_update(ill, lnr));
2850 }
2851