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