xref: /illumos-gate/usr/src/uts/common/inet/ip/ip6_ire.c (revision e9af4bc0b1cc30cea75d6ad4aa2fde97d985e9be)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 1990 Mentat Inc.
27  */
28 
29 /*
30  * This file contains routines that manipulate Internet Routing Entries (IREs).
31  */
32 #include <sys/types.h>
33 #include <sys/stream.h>
34 #include <sys/stropts.h>
35 #include <sys/ddi.h>
36 #include <sys/cmn_err.h>
37 
38 #include <sys/systm.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <net/if_dl.h>
45 #include <netinet/ip6.h>
46 #include <netinet/icmp6.h>
47 
48 #include <inet/common.h>
49 #include <inet/mi.h>
50 #include <inet/ip.h>
51 #include <inet/ip6.h>
52 #include <inet/ip_ndp.h>
53 #include <inet/ip_if.h>
54 #include <inet/ip_ire.h>
55 #include <inet/ipclassifier.h>
56 #include <inet/nd.h>
57 #include <sys/kmem.h>
58 #include <sys/zone.h>
59 
60 #include <sys/tsol/label.h>
61 #include <sys/tsol/tnet.h>
62 
63 #define	IS_DEFAULT_ROUTE_V6(ire)	\
64 	(((ire)->ire_type & IRE_DEFAULT) || \
65 	    (((ire)->ire_type & IRE_INTERFACE) && \
66 	    (IN6_IS_ADDR_UNSPECIFIED(&(ire)->ire_addr_v6))))
67 
68 static	ire_t	ire_null;
69 
70 static ire_t *
71 ire_ftable_lookup_impl_v6(const in6_addr_t *addr, const in6_addr_t *mask,
72     const in6_addr_t *gateway, int type, const ill_t *ill,
73     zoneid_t zoneid, const ts_label_t *tsl, int flags,
74     ip_stack_t *ipst);
75 
76 /*
77  * Initialize the ire that is specific to IPv6 part and call
78  * ire_init_common to finish it.
79  * Returns zero or errno.
80  */
81 int
82 ire_init_v6(ire_t *ire, const in6_addr_t *v6addr, const in6_addr_t *v6mask,
83     const in6_addr_t *v6gateway, ushort_t type, ill_t *ill,
84     zoneid_t zoneid, uint_t flags, tsol_gc_t *gc, ip_stack_t *ipst)
85 {
86 	int error;
87 
88 	/*
89 	 * Reject IRE security attmakeribute creation/initialization
90 	 * if system is not running in Trusted mode.
91 	 */
92 	if (gc != NULL && !is_system_labeled())
93 		return (EINVAL);
94 
95 	BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_alloced);
96 	if (v6addr != NULL)
97 		ire->ire_addr_v6 = *v6addr;
98 	if (v6gateway != NULL)
99 		ire->ire_gateway_addr_v6 = *v6gateway;
100 
101 	/* Make sure we don't have stray values in some fields */
102 	switch (type) {
103 	case IRE_LOOPBACK:
104 		ire->ire_gateway_addr_v6 = ire->ire_addr_v6;
105 		/* FALLTHRU */
106 	case IRE_HOST:
107 	case IRE_LOCAL:
108 	case IRE_IF_CLONE:
109 		ire->ire_mask_v6 = ipv6_all_ones;
110 		ire->ire_masklen = IPV6_ABITS;
111 		break;
112 	case IRE_PREFIX:
113 	case IRE_DEFAULT:
114 	case IRE_IF_RESOLVER:
115 	case IRE_IF_NORESOLVER:
116 		if (v6mask != NULL) {
117 			ire->ire_mask_v6 = *v6mask;
118 			ire->ire_masklen =
119 			    ip_mask_to_plen_v6(&ire->ire_mask_v6);
120 		}
121 		break;
122 	case IRE_MULTICAST:
123 	case IRE_NOROUTE:
124 		ASSERT(v6mask == NULL);
125 		break;
126 	default:
127 		ASSERT(0);
128 		return (EINVAL);
129 	}
130 
131 	error = ire_init_common(ire, type, ill, zoneid, flags, IPV6_VERSION,
132 	    gc, ipst);
133 	if (error != NULL)
134 		return (error);
135 
136 	/* Determine which function pointers to use */
137 	ire->ire_postfragfn = ip_xmit;		/* Common case */
138 
139 	switch (ire->ire_type) {
140 	case IRE_LOCAL:
141 		ire->ire_sendfn = ire_send_local_v6;
142 		ire->ire_recvfn = ire_recv_local_v6;
143 		ASSERT(ire->ire_ill != NULL);
144 		if (ire->ire_ill->ill_flags & ILLF_NOACCEPT)
145 			ire->ire_recvfn = ire_recv_noaccept_v6;
146 		break;
147 	case IRE_LOOPBACK:
148 		ire->ire_sendfn = ire_send_local_v6;
149 		ire->ire_recvfn = ire_recv_loopback_v6;
150 		break;
151 	case IRE_MULTICAST:
152 		ire->ire_postfragfn = ip_postfrag_loopcheck;
153 		ire->ire_sendfn = ire_send_multicast_v6;
154 		ire->ire_recvfn = ire_recv_multicast_v6;
155 		break;
156 	default:
157 		/*
158 		 * For IRE_IF_ALL and IRE_OFFLINK we forward received
159 		 * packets by default.
160 		 */
161 		ire->ire_sendfn = ire_send_wire_v6;
162 		ire->ire_recvfn = ire_recv_forward_v6;
163 		break;
164 	}
165 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
166 		ire->ire_sendfn = ire_send_noroute_v6;
167 		ire->ire_recvfn = ire_recv_noroute_v6;
168 	} else if (ire->ire_flags & RTF_MULTIRT) {
169 		ire->ire_postfragfn = ip_postfrag_multirt_v6;
170 		ire->ire_sendfn = ire_send_multirt_v6;
171 		ire->ire_recvfn = ire_recv_multirt_v6;
172 	}
173 	ire->ire_nce_capable = ire_determine_nce_capable(ire);
174 	return (0);
175 }
176 
177 /*
178  * ire_create_v6 is called to allocate and initialize a new IRE.
179  *
180  * NOTE : This is called as writer sometimes though not required
181  * by this function.
182  */
183 /* ARGSUSED */
184 ire_t *
185 ire_create_v6(const in6_addr_t *v6addr, const in6_addr_t *v6mask,
186     const in6_addr_t *v6gateway, ushort_t type, ill_t *ill, zoneid_t zoneid,
187     uint_t flags, tsol_gc_t *gc, ip_stack_t *ipst)
188 {
189 	ire_t	*ire;
190 	int	error;
191 
192 	ASSERT(!IN6_IS_ADDR_V4MAPPED(v6addr));
193 
194 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
195 	if (ire == NULL) {
196 		DTRACE_PROBE(kmem__cache__alloc);
197 		return (NULL);
198 	}
199 	*ire = ire_null;
200 
201 	error = ire_init_v6(ire, v6addr, v6mask, v6gateway,
202 	    type, ill, zoneid, flags, gc, ipst);
203 
204 	if (error != 0) {
205 		DTRACE_PROBE2(ire__init__v6, ire_t *, ire, int, error);
206 		kmem_cache_free(ire_cache, ire);
207 		return (NULL);
208 	}
209 	return (ire);
210 }
211 
212 /*
213  * Find the ill matching a multicast group.
214  * Allows different routes for multicast addresses
215  * in the unicast routing table (akin to FF::0/8 but could be more specific)
216  * which point at different interfaces. This is used when IPV6_MULTICAST_IF
217  * isn't specified (when sending) and when IPV6_JOIN_GROUP doesn't
218  * specify the interface to join on.
219  *
220  * Supports link-local addresses by using ire_route_recursive which follows
221  * the ill when recursing.
222  *
223  * To handle CGTP, since we don't have a separate IRE_MULTICAST for each group
224  * and the MULTIRT property can be different for different groups, we
225  * extract RTF_MULTIRT from the special unicast route added for a group
226  * with CGTP and pass that back in the multirtp argument.
227  * This is used in ip_set_destination etc to set ixa_postfragfn for multicast.
228  * We have a setsrcp argument for the same reason.
229  */
230 ill_t *
231 ire_lookup_multi_ill_v6(const in6_addr_t *group, zoneid_t zoneid,
232     ip_stack_t *ipst, boolean_t *multirtp, in6_addr_t *setsrcp)
233 {
234 	ire_t	*ire;
235 	ill_t	*ill;
236 
237 	ire = ire_route_recursive_v6(group, 0, NULL, zoneid, NULL,
238 	    MATCH_IRE_DSTONLY, B_FALSE, 0, ipst, setsrcp, NULL, NULL);
239 	ASSERT(ire != NULL);
240 
241 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
242 		ire_refrele(ire);
243 		return (NULL);
244 	}
245 
246 	if (multirtp != NULL)
247 		*multirtp = (ire->ire_flags & RTF_MULTIRT) != 0;
248 
249 	ill = ire_nexthop_ill(ire);
250 	ire_refrele(ire);
251 	return (ill);
252 }
253 
254 /*
255  * This function takes a mask and returns number of bits set in the
256  * mask (the represented prefix length).  Assumes a contiguous mask.
257  */
258 int
259 ip_mask_to_plen_v6(const in6_addr_t *v6mask)
260 {
261 	int		bits;
262 	int		plen = IPV6_ABITS;
263 	int		i;
264 
265 	for (i = 3; i >= 0; i--) {
266 		if (v6mask->s6_addr32[i] == 0) {
267 			plen -= 32;
268 			continue;
269 		}
270 		bits = ffs(ntohl(v6mask->s6_addr32[i])) - 1;
271 		if (bits == 0)
272 			break;
273 		plen -= bits;
274 	}
275 
276 	return (plen);
277 }
278 
279 /*
280  * Convert a prefix length to the mask for that prefix.
281  * Returns the argument bitmask.
282  */
283 in6_addr_t *
284 ip_plen_to_mask_v6(uint_t plen, in6_addr_t *bitmask)
285 {
286 	uint32_t *ptr;
287 
288 	if (plen < 0 || plen > IPV6_ABITS)
289 		return (NULL);
290 	*bitmask = ipv6_all_zeros;
291 	if (plen == 0)
292 		return (bitmask);
293 
294 	ptr = (uint32_t *)bitmask;
295 	while (plen > 32) {
296 		*ptr++ = 0xffffffffU;
297 		plen -= 32;
298 	}
299 	*ptr = htonl(0xffffffffU << (32 - plen));
300 	return (bitmask);
301 }
302 
303 /*
304  * Add a fully initialized IPv6 IRE to the forwarding table.
305  * This returns NULL on failure, or a held IRE on success.
306  * Normally the returned IRE is the same as the argument. But a different
307  * IRE will be returned if the added IRE is deemed identical to an existing
308  * one. In that case ire_identical_ref will be increased.
309  * The caller always needs to do an ire_refrele() on the returned IRE.
310  */
311 ire_t *
312 ire_add_v6(ire_t *ire)
313 {
314 	ire_t	*ire1;
315 	int	mask_table_index;
316 	irb_t	*irb_ptr;
317 	ire_t	**irep;
318 	int	match_flags;
319 	int	error;
320 	ip_stack_t	*ipst = ire->ire_ipst;
321 
322 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
323 
324 	/* Make sure the address is properly masked. */
325 	V6_MASK_COPY(ire->ire_addr_v6, ire->ire_mask_v6, ire->ire_addr_v6);
326 
327 	mask_table_index = ip_mask_to_plen_v6(&ire->ire_mask_v6);
328 	if ((ipst->ips_ip_forwarding_table_v6[mask_table_index]) == NULL) {
329 		irb_t *ptr;
330 		int i;
331 
332 		ptr = (irb_t *)mi_zalloc((ipst->ips_ip6_ftable_hash_size *
333 		    sizeof (irb_t)));
334 		if (ptr == NULL) {
335 			ire_delete(ire);
336 			return (NULL);
337 		}
338 		for (i = 0; i < ipst->ips_ip6_ftable_hash_size; i++) {
339 			rw_init(&ptr[i].irb_lock, NULL, RW_DEFAULT, NULL);
340 		}
341 		mutex_enter(&ipst->ips_ire_ft_init_lock);
342 		if (ipst->ips_ip_forwarding_table_v6[mask_table_index] ==
343 		    NULL) {
344 			ipst->ips_ip_forwarding_table_v6[mask_table_index] =
345 			    ptr;
346 			mutex_exit(&ipst->ips_ire_ft_init_lock);
347 		} else {
348 			/*
349 			 * Some other thread won the race in
350 			 * initializing the forwarding table at the
351 			 * same index.
352 			 */
353 			mutex_exit(&ipst->ips_ire_ft_init_lock);
354 			for (i = 0; i < ipst->ips_ip6_ftable_hash_size; i++) {
355 				rw_destroy(&ptr[i].irb_lock);
356 			}
357 			mi_free(ptr);
358 		}
359 	}
360 	irb_ptr = &(ipst->ips_ip_forwarding_table_v6[mask_table_index][
361 	    IRE_ADDR_MASK_HASH_V6(ire->ire_addr_v6, ire->ire_mask_v6,
362 	    ipst->ips_ip6_ftable_hash_size)]);
363 
364 	match_flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
365 	if (ire->ire_ill != NULL)
366 		match_flags |= MATCH_IRE_ILL;
367 	/*
368 	 * Start the atomic add of the ire. Grab the bucket lock and the
369 	 * ill lock. Check for condemned.
370 	 */
371 	error = ire_atomic_start(irb_ptr, ire);
372 	if (error != 0) {
373 		ire_delete(ire);
374 		return (NULL);
375 	}
376 
377 	/*
378 	 * If we are creating a hidden IRE, make sure we search for
379 	 * hidden IREs when searching for duplicates below.
380 	 * Otherwise, we might find an IRE on some other interface
381 	 * that's not marked hidden.
382 	 */
383 	if (ire->ire_testhidden)
384 		match_flags |= MATCH_IRE_TESTHIDDEN;
385 
386 	/*
387 	 * Atomically check for duplicate and insert in the table.
388 	 */
389 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
390 		if (IRE_IS_CONDEMNED(ire1))
391 			continue;
392 		/*
393 		 * Here we need an exact match on zoneid, i.e.,
394 		 * ire_match_args doesn't fit.
395 		 */
396 		if (ire1->ire_zoneid != ire->ire_zoneid)
397 			continue;
398 
399 		if (ire1->ire_type != ire->ire_type)
400 			continue;
401 
402 		/*
403 		 * Note: We do not allow multiple routes that differ only
404 		 * in the gateway security attributes; such routes are
405 		 * considered duplicates.
406 		 * To change that we explicitly have to treat them as
407 		 * different here.
408 		 */
409 		if (ire_match_args_v6(ire1, &ire->ire_addr_v6,
410 		    &ire->ire_mask_v6, &ire->ire_gateway_addr_v6,
411 		    ire->ire_type, ire->ire_ill, ire->ire_zoneid, NULL,
412 		    match_flags)) {
413 			/*
414 			 * Return the old ire after doing a REFHOLD.
415 			 * As most of the callers continue to use the IRE
416 			 * after adding, we return a held ire. This will
417 			 * avoid a lookup in the caller again. If the callers
418 			 * don't want to use it, they need to do a REFRELE.
419 			 */
420 			ip1dbg(("found dup ire existing %p new %p",
421 			    (void *)ire1, (void *)ire));
422 			ire_refhold(ire1);
423 			atomic_add_32(&ire1->ire_identical_ref, 1);
424 			ire_atomic_end(irb_ptr, ire);
425 			ire_delete(ire);
426 			return (ire1);
427 		}
428 	}
429 
430 	/*
431 	 * Normally we do head insertion since most things do not care about
432 	 * the order of the IREs in the bucket.
433 	 * However, due to shared-IP zones (and restrict_interzone_loopback)
434 	 * we can have an IRE_LOCAL as well as IRE_IF_CLONE for the same
435 	 * address. For that reason we do tail insertion for IRE_IF_CLONE.
436 	 */
437 	irep = (ire_t **)irb_ptr;
438 	if (ire->ire_type & IRE_IF_CLONE) {
439 		while ((ire1 = *irep) != NULL)
440 			irep = &ire1->ire_next;
441 	}
442 	/* Insert at *irep */
443 	ire1 = *irep;
444 	if (ire1 != NULL)
445 		ire1->ire_ptpn = &ire->ire_next;
446 	ire->ire_next = ire1;
447 	/* Link the new one in. */
448 	ire->ire_ptpn = irep;
449 	/*
450 	 * ire_walk routines de-reference ire_next without holding
451 	 * a lock. Before we point to the new ire, we want to make
452 	 * sure the store that sets the ire_next of the new ire
453 	 * reaches global visibility, so that ire_walk routines
454 	 * don't see a truncated list of ires i.e if the ire_next
455 	 * of the new ire gets set after we do "*irep = ire" due
456 	 * to re-ordering, the ire_walk thread will see a NULL
457 	 * once it accesses the ire_next of the new ire.
458 	 * membar_producer() makes sure that the following store
459 	 * happens *after* all of the above stores.
460 	 */
461 	membar_producer();
462 	*irep = ire;
463 	ire->ire_bucket = irb_ptr;
464 	/*
465 	 * We return a bumped up IRE above. Keep it symmetrical
466 	 * so that the callers will always have to release. This
467 	 * helps the callers of this function because they continue
468 	 * to use the IRE after adding and hence they don't have to
469 	 * lookup again after we return the IRE.
470 	 *
471 	 * NOTE : We don't have to use atomics as this is appearing
472 	 * in the list for the first time and no one else can bump
473 	 * up the reference count on this yet.
474 	 */
475 	ire_refhold_locked(ire);
476 	BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_inserted);
477 	irb_ptr->irb_ire_cnt++;
478 
479 	if (ire->ire_ill != NULL) {
480 		DTRACE_PROBE3(ill__incr__cnt, (ill_t *), ire->ire_ill,
481 		    (char *), "ire", (void *), ire);
482 		ire->ire_ill->ill_ire_cnt++;
483 		ASSERT(ire->ire_ill->ill_ire_cnt != 0);	/* Wraparound */
484 	}
485 	ire_atomic_end(irb_ptr, ire);
486 
487 	/* Make any caching of the IREs be notified or updated */
488 	ire_flush_cache_v6(ire, IRE_FLUSH_ADD);
489 
490 	return (ire);
491 }
492 
493 /*
494  * Search for all HOST REDIRECT routes that are
495  * pointing at the specified gateway and
496  * delete them. This routine is called only
497  * when a default gateway is going away.
498  */
499 static void
500 ire_delete_host_redirects_v6(const in6_addr_t *gateway, ip_stack_t *ipst)
501 {
502 	irb_t *irb_ptr;
503 	irb_t *irb;
504 	ire_t *ire;
505 	in6_addr_t gw_addr_v6;
506 	int i;
507 
508 	/* get the hash table for HOST routes */
509 	irb_ptr = ipst->ips_ip_forwarding_table_v6[(IP6_MASK_TABLE_SIZE - 1)];
510 	if (irb_ptr == NULL)
511 		return;
512 	for (i = 0; (i < ipst->ips_ip6_ftable_hash_size); i++) {
513 		irb = &irb_ptr[i];
514 		irb_refhold(irb);
515 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
516 			if (!(ire->ire_flags & RTF_DYNAMIC))
517 				continue;
518 			mutex_enter(&ire->ire_lock);
519 			gw_addr_v6 = ire->ire_gateway_addr_v6;
520 			mutex_exit(&ire->ire_lock);
521 			if (IN6_ARE_ADDR_EQUAL(&gw_addr_v6, gateway))
522 				ire_delete(ire);
523 		}
524 		irb_refrele(irb);
525 	}
526 }
527 
528 /*
529  * Delete the specified IRE.
530  * All calls should use ire_delete().
531  * Sometimes called as writer though not required by this function.
532  *
533  * NOTE : This function is called only if the ire was added
534  * in the list.
535  */
536 void
537 ire_delete_v6(ire_t *ire)
538 {
539 	in6_addr_t gw_addr_v6;
540 	ip_stack_t	*ipst = ire->ire_ipst;
541 
542 	/*
543 	 * Make sure ire_generation increases from ire_flush_cache happen
544 	 * after any lookup/reader has read ire_generation.
545 	 * Since the rw_enter makes us wait until any lookup/reader has
546 	 * completed we can exit the lock immediately.
547 	 */
548 	rw_enter(&ipst->ips_ip6_ire_head_lock, RW_WRITER);
549 	rw_exit(&ipst->ips_ip6_ire_head_lock);
550 
551 	ASSERT(ire->ire_refcnt >= 1);
552 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
553 
554 	ire_flush_cache_v6(ire, IRE_FLUSH_DELETE);
555 
556 	if (ire->ire_type == IRE_DEFAULT) {
557 		/*
558 		 * when a default gateway is going away
559 		 * delete all the host redirects pointing at that
560 		 * gateway.
561 		 */
562 		mutex_enter(&ire->ire_lock);
563 		gw_addr_v6 = ire->ire_gateway_addr_v6;
564 		mutex_exit(&ire->ire_lock);
565 		ire_delete_host_redirects_v6(&gw_addr_v6, ipst);
566 	}
567 
568 	/*
569 	 * If we are deleting an IRE_INTERFACE then we make sure we also
570 	 * delete any IRE_IF_CLONE that has been created from it.
571 	 * Those are always in ire_dep_children.
572 	 */
573 	if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != 0)
574 		ire_dep_delete_if_clone(ire);
575 
576 	/* Remove from parent dependencies and child */
577 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
578 	if (ire->ire_dep_parent != NULL) {
579 		ire_dep_remove(ire);
580 	}
581 	while (ire->ire_dep_children != NULL)
582 		ire_dep_remove(ire->ire_dep_children);
583 	rw_exit(&ipst->ips_ire_dep_lock);
584 }
585 
586 /*
587  * When an IRE is added or deleted this routine is called to make sure
588  * any caching of IRE information is notified or updated.
589  *
590  * The flag argument indicates if the flush request is due to addition
591  * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE),
592  * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE).
593  */
594 void
595 ire_flush_cache_v6(ire_t *ire, int flag)
596 {
597 	ip_stack_t *ipst = ire->ire_ipst;
598 
599 	/*
600 	 * IRE_IF_CLONE ire's don't provide any new information
601 	 * than the parent from which they are cloned, so don't
602 	 * perturb the generation numbers.
603 	 */
604 	if (ire->ire_type & IRE_IF_CLONE)
605 		return;
606 
607 	/*
608 	 * Ensure that an ire_add during a lookup serializes the updates of
609 	 * the generation numbers under ire_head_lock so that the lookup gets
610 	 * either the old ire and old generation number, or a new ire and new
611 	 * generation number.
612 	 */
613 	rw_enter(&ipst->ips_ip6_ire_head_lock, RW_WRITER);
614 
615 	/*
616 	 * If a route was just added, we need to notify everybody that
617 	 * has cached an IRE_NOROUTE since there might now be a better
618 	 * route for them.
619 	 */
620 	if (flag == IRE_FLUSH_ADD) {
621 		ire_increment_generation(ipst->ips_ire_reject_v6);
622 		ire_increment_generation(ipst->ips_ire_blackhole_v6);
623 	}
624 
625 	/* Adding a default can't otherwise provide a better route */
626 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) {
627 		rw_exit(&ipst->ips_ip6_ire_head_lock);
628 		return;
629 	}
630 
631 	switch (flag) {
632 	case IRE_FLUSH_DELETE:
633 	case IRE_FLUSH_GWCHANGE:
634 		/*
635 		 * Update ire_generation for all ire_dep_children chains
636 		 * starting with this IRE
637 		 */
638 		ire_dep_incr_generation(ire);
639 		break;
640 	case IRE_FLUSH_ADD: {
641 		in6_addr_t	addr;
642 		in6_addr_t	mask;
643 		ip_stack_t	*ipst = ire->ire_ipst;
644 		uint_t		masklen;
645 
646 		/*
647 		 * Find an IRE which is a shorter match than the ire to be added
648 		 * For any such IRE (which we repeat) we update the
649 		 * ire_generation the same way as in the delete case.
650 		 */
651 		addr = ire->ire_addr_v6;
652 		mask = ire->ire_mask_v6;
653 		masklen = ip_mask_to_plen_v6(&mask);
654 
655 		ire = ire_ftable_lookup_impl_v6(&addr, &mask, NULL, 0, NULL,
656 		    ALL_ZONES, NULL, MATCH_IRE_SHORTERMASK, ipst);
657 		while (ire != NULL) {
658 			/* We need to handle all in the same bucket */
659 			irb_increment_generation(ire->ire_bucket);
660 
661 			mask = ire->ire_mask_v6;
662 			ASSERT(masklen > ip_mask_to_plen_v6(&mask));
663 			masklen = ip_mask_to_plen_v6(&mask);
664 			ire_refrele(ire);
665 			ire = ire_ftable_lookup_impl_v6(&addr, &mask, NULL, 0,
666 			    NULL, ALL_ZONES, NULL, MATCH_IRE_SHORTERMASK, ipst);
667 		}
668 		}
669 		break;
670 	}
671 	rw_exit(&ipst->ips_ip6_ire_head_lock);
672 }
673 
674 /*
675  * Matches the arguments passed with the values in the ire.
676  *
677  * Note: for match types that match using "ill" passed in, ill
678  * must be checked for non-NULL before calling this routine.
679  */
680 boolean_t
681 ire_match_args_v6(ire_t *ire, const in6_addr_t *addr, const in6_addr_t *mask,
682     const in6_addr_t *gateway, int type, const ill_t *ill, zoneid_t zoneid,
683     const ts_label_t *tsl, int match_flags)
684 {
685 	in6_addr_t masked_addr;
686 	in6_addr_t gw_addr_v6;
687 	ill_t *ire_ill = NULL, *dst_ill;
688 	ip_stack_t *ipst = ire->ire_ipst;
689 
690 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
691 	ASSERT(addr != NULL);
692 	ASSERT(mask != NULL);
693 	ASSERT((!(match_flags & MATCH_IRE_GW)) || gateway != NULL);
694 	ASSERT((!(match_flags & MATCH_IRE_ILL)) ||
695 	    (ill != NULL && ill->ill_isv6));
696 
697 	/*
698 	 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it
699 	 * is in fact hidden, to ensure the caller gets the right one.
700 	 */
701 	if (ire->ire_testhidden) {
702 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))
703 			return (B_FALSE);
704 	}
705 
706 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
707 	    ire->ire_zoneid != ALL_ZONES) {
708 		/*
709 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid
710 		 * does not match that of ire_zoneid, a failure to
711 		 * match is reported at this point. Otherwise, since some IREs
712 		 * that are available in the global zone can be used in local
713 		 * zones, additional checks need to be performed:
714 		 *
715 		 * IRE_LOOPBACK
716 		 *	entries should never be matched in this situation.
717 		 *	Each zone has its own IRE_LOOPBACK.
718 		 *
719 		 * IRE_LOCAL
720 		 *	We allow them for any zoneid. ire_route_recursive
721 		 *	does additional checks when
722 		 *	ip_restrict_interzone_loopback is set.
723 		 *
724 		 * If ill_usesrc_ifindex is set
725 		 *	Then we check if the zone has a valid source address
726 		 *	on the usesrc ill.
727 		 *
728 		 * If ire_ill is set, then check that the zone has an ipif
729 		 *	on that ill.
730 		 *
731 		 * Outside of this function (in ire_round_robin) we check
732 		 * that any IRE_OFFLINK has a gateway that reachable from the
733 		 * zone when we have multiple choices (ECMP).
734 		 */
735 		if (match_flags & MATCH_IRE_ZONEONLY)
736 			return (B_FALSE);
737 		if (ire->ire_type & IRE_LOOPBACK)
738 			return (B_FALSE);
739 
740 		if (ire->ire_type & IRE_LOCAL)
741 			goto matchit;
742 
743 		/*
744 		 * The normal case of IRE_ONLINK has a matching zoneid.
745 		 * Here we handle the case when shared-IP zones have been
746 		 * configured with IP addresses on vniN. In that case it
747 		 * is ok for traffic from a zone to use IRE_ONLINK routes
748 		 * if the ill has a usesrc pointing at vniN
749 		 * Applies to IRE_INTERFACE.
750 		 */
751 		dst_ill = ire->ire_ill;
752 		if (ire->ire_type & IRE_ONLINK) {
753 			uint_t	ifindex;
754 
755 			/*
756 			 * Note there is no IRE_INTERFACE on vniN thus
757 			 * can't do an IRE lookup for a matching route.
758 			 */
759 			ifindex = dst_ill->ill_usesrc_ifindex;
760 			if (ifindex == 0)
761 				return (B_FALSE);
762 
763 			/*
764 			 * If there is a usable source address in the
765 			 * zone, then it's ok to return this IRE_INTERFACE
766 			 */
767 			if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
768 			    zoneid, ipst)) {
769 				ip3dbg(("ire_match_args: no usrsrc for zone"
770 				    " dst_ill %p\n", (void *)dst_ill));
771 				return (B_FALSE);
772 			}
773 		}
774 		/*
775 		 * For exampe, with
776 		 * route add 11.0.0.0 gw1 -ifp bge0
777 		 * route add 11.0.0.0 gw2 -ifp bge1
778 		 * this code would differentiate based on
779 		 * where the sending zone has addresses.
780 		 * Only if the zone has an address on bge0 can it use the first
781 		 * route. It isn't clear if this behavior is documented
782 		 * anywhere.
783 		 */
784 		if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
785 			ipif_t	*tipif;
786 
787 			mutex_enter(&dst_ill->ill_lock);
788 			for (tipif = dst_ill->ill_ipif;
789 			    tipif != NULL; tipif = tipif->ipif_next) {
790 				if (!IPIF_IS_CONDEMNED(tipif) &&
791 				    (tipif->ipif_flags & IPIF_UP) &&
792 				    (tipif->ipif_zoneid == zoneid ||
793 				    tipif->ipif_zoneid == ALL_ZONES))
794 					break;
795 			}
796 			mutex_exit(&dst_ill->ill_lock);
797 			if (tipif == NULL)
798 				return (B_FALSE);
799 		}
800 	}
801 
802 matchit:
803 	if (match_flags & MATCH_IRE_GW) {
804 		mutex_enter(&ire->ire_lock);
805 		gw_addr_v6 = ire->ire_gateway_addr_v6;
806 		mutex_exit(&ire->ire_lock);
807 	}
808 	if (match_flags & MATCH_IRE_ILL) {
809 		ire_ill = ire->ire_ill;
810 
811 		/*
812 		 * If asked to match an ill, we *must* match
813 		 * on the ire_ill for ipmp test addresses, or
814 		 * any of the ill in the group for data addresses.
815 		 * If we don't, we may as well fail.
816 		 * However, we need an exception for IRE_LOCALs to ensure
817 		 * we loopback packets even sent to test addresses on different
818 		 * interfaces in the group.
819 		 */
820 		if ((match_flags & MATCH_IRE_TESTHIDDEN) &&
821 		    !(ire->ire_type & IRE_LOCAL)) {
822 			if (ire->ire_ill != ill)
823 				return (B_FALSE);
824 		} else  {
825 			match_flags &= ~MATCH_IRE_TESTHIDDEN;
826 			/*
827 			 * We know that ill is not NULL, but ire_ill could be
828 			 * NULL
829 			 */
830 			if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill))
831 				return (B_FALSE);
832 		}
833 	}
834 	/* No ire_addr_v6 bits set past the mask */
835 	ASSERT(V6_MASK_EQ(ire->ire_addr_v6, ire->ire_mask_v6,
836 	    ire->ire_addr_v6));
837 	V6_MASK_COPY(*addr, *mask, masked_addr);
838 	if (V6_MASK_EQ(*addr, *mask, ire->ire_addr_v6) &&
839 	    ((!(match_flags & MATCH_IRE_GW)) ||
840 	    IN6_ARE_ADDR_EQUAL(&gw_addr_v6, gateway)) &&
841 	    ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) &&
842 	    ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
843 	    ((!(match_flags & MATCH_IRE_MASK)) ||
844 	    (IN6_ARE_ADDR_EQUAL(&ire->ire_mask_v6, mask))) &&
845 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
846 	    (!is_system_labeled()) ||
847 	    (tsol_ire_match_gwattr(ire, tsl) == 0))) {
848 		/* We found the matched IRE */
849 		return (B_TRUE);
850 	}
851 	return (B_FALSE);
852 }
853 
854 /*
855  * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
856  * gateway address. If ill is non-NULL we also match on it.
857  * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
858  */
859 boolean_t
860 ire_gateway_ok_zone_v6(const in6_addr_t *gateway, zoneid_t zoneid, ill_t *ill,
861     const ts_label_t *tsl, ip_stack_t *ipst, boolean_t lock_held)
862 {
863 	ire_t	*ire;
864 	uint_t	match_flags;
865 
866 	if (lock_held)
867 		ASSERT(RW_READ_HELD(&ipst->ips_ip6_ire_head_lock));
868 	else
869 		rw_enter(&ipst->ips_ip6_ire_head_lock, RW_READER);
870 
871 	match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
872 	if (ill != NULL)
873 		match_flags |= MATCH_IRE_ILL;
874 
875 	ire = ire_ftable_lookup_impl_v6(gateway, &ipv6_all_zeros,
876 	    &ipv6_all_zeros, IRE_INTERFACE, ill, zoneid, tsl, match_flags,
877 	    ipst);
878 
879 	if (!lock_held)
880 		rw_exit(&ipst->ips_ip6_ire_head_lock);
881 	if (ire != NULL) {
882 		ire_refrele(ire);
883 		return (B_TRUE);
884 	} else {
885 		return (B_FALSE);
886 	}
887 }
888 
889 /*
890  * Lookup a route in forwarding table.
891  * specific lookup is indicated by passing the
892  * required parameters and indicating the
893  * match required in flag field.
894  *
895  * Supports link-local addresses by following the ipif/ill when recursing.
896  */
897 ire_t *
898 ire_ftable_lookup_v6(const in6_addr_t *addr, const in6_addr_t *mask,
899     const in6_addr_t *gateway, int type, const ill_t *ill,
900     zoneid_t zoneid, const ts_label_t *tsl, int flags,
901     uint32_t xmit_hint, ip_stack_t *ipst, uint_t *generationp)
902 {
903 	ire_t *ire = NULL;
904 
905 	ASSERT(addr != NULL);
906 	ASSERT((!(flags & MATCH_IRE_MASK)) || mask != NULL);
907 	ASSERT((!(flags & MATCH_IRE_GW)) || gateway != NULL);
908 	ASSERT(ill == NULL || ill->ill_isv6);
909 
910 	ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
911 
912 	/*
913 	 * ire_match_args_v6() will dereference ill if MATCH_IRE_ILL
914 	 * is set.
915 	 */
916 	if ((flags & (MATCH_IRE_ILL)) && (ill == NULL))
917 		return (NULL);
918 
919 	rw_enter(&ipst->ips_ip6_ire_head_lock, RW_READER);
920 	ire = ire_ftable_lookup_impl_v6(addr, mask, gateway, type, ill, zoneid,
921 	    tsl, flags, ipst);
922 	if (ire == NULL) {
923 		rw_exit(&ipst->ips_ip6_ire_head_lock);
924 		return (NULL);
925 	}
926 
927 	/*
928 	 * round-robin only if we have more than one route in the bucket.
929 	 * ips_ip_ecmp_behavior controls when we do ECMP
930 	 *	2:	always
931 	 *	1:	for IRE_DEFAULT and /0 IRE_INTERFACE
932 	 *	0:	never
933 	 *
934 	 * Note: if we found an IRE_IF_CLONE we won't look at the bucket with
935 	 * other ECMP IRE_INTERFACEs since the IRE_IF_CLONE is a /128 match
936 	 * and the IRE_INTERFACESs are likely to be shorter matches.
937 	 */
938 	if (ire->ire_bucket->irb_ire_cnt > 1 && !(flags & MATCH_IRE_GW)) {
939 		if (ipst->ips_ip_ecmp_behavior == 2 ||
940 		    (ipst->ips_ip_ecmp_behavior == 1 &&
941 		    IS_DEFAULT_ROUTE_V6(ire))) {
942 			ire_t	*next_ire;
943 			ire_ftable_args_t margs;
944 
945 			bzero(&margs, sizeof (margs));
946 			margs.ift_addr_v6 = *addr;
947 			if (mask != NULL)
948 				margs.ift_mask_v6 = *mask;
949 			if (gateway != NULL)
950 				margs.ift_gateway_v6 = *gateway;
951 			margs.ift_type = type;
952 			margs.ift_ill = ill;
953 			margs.ift_zoneid = zoneid;
954 			margs.ift_tsl = tsl;
955 			margs.ift_flags = flags;
956 
957 			next_ire = ire_round_robin(ire->ire_bucket, &margs,
958 			    xmit_hint, ire, ipst);
959 			if (next_ire == NULL) {
960 				/* keep ire if next_ire is null */
961 				goto done;
962 			}
963 			ire_refrele(ire);
964 			ire = next_ire;
965 		}
966 	}
967 
968 done:
969 	/* Return generation before dropping lock */
970 	if (generationp != NULL)
971 		*generationp = ire->ire_generation;
972 
973 	rw_exit(&ipst->ips_ip6_ire_head_lock);
974 
975 	/*
976 	 * For shared-IP zones we need additional checks to what was
977 	 * done in ire_match_args to make sure IRE_LOCALs are handled.
978 	 *
979 	 * When ip_restrict_interzone_loopback is set, then
980 	 * we ensure that IRE_LOCAL are only used for loopback
981 	 * between zones when the logical "Ethernet" would
982 	 * have looped them back. That is, if in the absense of
983 	 * the IRE_LOCAL we would have sent to packet out the
984 	 * same ill.
985 	 */
986 	if ((ire->ire_type & IRE_LOCAL) && zoneid != ALL_ZONES &&
987 	    ire->ire_zoneid != zoneid && ire->ire_zoneid != ALL_ZONES &&
988 	    ipst->ips_ip_restrict_interzone_loopback) {
989 		ire = ire_alt_local(ire, zoneid, tsl, ill, generationp);
990 		ASSERT(ire != NULL);
991 	}
992 
993 	return (ire);
994 }
995 
996 /*
997  * Look up a single ire. The caller holds either the read or write lock.
998  */
999 ire_t *
1000 ire_ftable_lookup_impl_v6(const in6_addr_t *addr, const in6_addr_t *mask,
1001     const in6_addr_t *gateway, int type, const ill_t *ill,
1002     zoneid_t zoneid, const ts_label_t *tsl, int flags,
1003     ip_stack_t *ipst)
1004 {
1005 	irb_t *irb_ptr;
1006 	ire_t *ire = NULL;
1007 	int i;
1008 
1009 	ASSERT(RW_LOCK_HELD(&ipst->ips_ip6_ire_head_lock));
1010 
1011 	/*
1012 	 * If the mask is known, the lookup
1013 	 * is simple, if the mask is not known
1014 	 * we need to search.
1015 	 */
1016 	if (flags & MATCH_IRE_MASK) {
1017 		uint_t masklen;
1018 
1019 		masklen = ip_mask_to_plen_v6(mask);
1020 		if (ipst->ips_ip_forwarding_table_v6[masklen] == NULL) {
1021 			return (NULL);
1022 		}
1023 		irb_ptr = &(ipst->ips_ip_forwarding_table_v6[masklen][
1024 		    IRE_ADDR_MASK_HASH_V6(*addr, *mask,
1025 		    ipst->ips_ip6_ftable_hash_size)]);
1026 		rw_enter(&irb_ptr->irb_lock, RW_READER);
1027 		for (ire = irb_ptr->irb_ire; ire != NULL;
1028 		    ire = ire->ire_next) {
1029 			if (IRE_IS_CONDEMNED(ire))
1030 				continue;
1031 			if (ire_match_args_v6(ire, addr, mask, gateway, type,
1032 			    ill, zoneid, tsl, flags))
1033 				goto found_ire;
1034 		}
1035 		rw_exit(&irb_ptr->irb_lock);
1036 	} else {
1037 		uint_t masklen;
1038 
1039 		/*
1040 		 * In this case we don't know the mask, we need to
1041 		 * search the table assuming different mask sizes.
1042 		 */
1043 		if (flags & MATCH_IRE_SHORTERMASK) {
1044 			masklen = ip_mask_to_plen_v6(mask);
1045 			if (masklen == 0) {
1046 				/* Nothing shorter than zero */
1047 				return (NULL);
1048 			}
1049 			masklen--;
1050 		} else {
1051 			masklen = IP6_MASK_TABLE_SIZE - 1;
1052 		}
1053 
1054 		for (i = masklen; i >= 0; i--) {
1055 			in6_addr_t tmpmask;
1056 
1057 			if ((ipst->ips_ip_forwarding_table_v6[i]) == NULL)
1058 				continue;
1059 			(void) ip_plen_to_mask_v6(i, &tmpmask);
1060 			irb_ptr = &ipst->ips_ip_forwarding_table_v6[i][
1061 			    IRE_ADDR_MASK_HASH_V6(*addr, tmpmask,
1062 			    ipst->ips_ip6_ftable_hash_size)];
1063 			rw_enter(&irb_ptr->irb_lock, RW_READER);
1064 			for (ire = irb_ptr->irb_ire; ire != NULL;
1065 			    ire = ire->ire_next) {
1066 				if (IRE_IS_CONDEMNED(ire))
1067 					continue;
1068 				if (ire_match_args_v6(ire, addr,
1069 				    &ire->ire_mask_v6, gateway, type, ill,
1070 				    zoneid, tsl, flags))
1071 					goto found_ire;
1072 			}
1073 			rw_exit(&irb_ptr->irb_lock);
1074 		}
1075 	}
1076 	ASSERT(ire == NULL);
1077 	ip1dbg(("ire_ftable_lookup_v6: returning NULL ire"));
1078 	return (NULL);
1079 
1080 found_ire:
1081 	ire_refhold(ire);
1082 	rw_exit(&irb_ptr->irb_lock);
1083 	return (ire);
1084 }
1085 
1086 
1087 /*
1088  * This function is called by
1089  * ip_input/ire_route_recursive when doing a route lookup on only the
1090  * destination address.
1091  *
1092  * The optimizations of this function over ire_ftable_lookup are:
1093  *	o removing unnecessary flag matching
1094  *	o doing longest prefix match instead of overloading it further
1095  *	  with the unnecessary "best_prefix_match"
1096  *
1097  * If no route is found we return IRE_NOROUTE.
1098  */
1099 ire_t *
1100 ire_ftable_lookup_simple_v6(const in6_addr_t *addr, uint32_t xmit_hint,
1101     ip_stack_t *ipst, uint_t *generationp)
1102 {
1103 	ire_t	*ire;
1104 
1105 	ire = ire_ftable_lookup_v6(addr, NULL, NULL, 0, NULL, ALL_ZONES, NULL,
1106 	    MATCH_IRE_DSTONLY, xmit_hint, ipst, generationp);
1107 	if (ire == NULL) {
1108 		ire = ire_reject(ipst, B_TRUE);
1109 		if (generationp != NULL)
1110 			*generationp = IRE_GENERATION_VERIFY;
1111 	}
1112 	/* ftable_lookup did round robin */
1113 	return (ire);
1114 }
1115 
1116 ire_t *
1117 ip_select_route_v6(const in6_addr_t *dst, ip_xmit_attr_t *ixa,
1118     uint_t *generationp, in6_addr_t *setsrcp, int *errorp, boolean_t *multirtp)
1119 {
1120 	ASSERT(!(ixa->ixa_flags & IXAF_IS_IPV4));
1121 
1122 	return (ip_select_route(dst, ixa, generationp, setsrcp, errorp,
1123 	    multirtp));
1124 }
1125 
1126 /*
1127  * Recursively look for a route to the destination. Can also match on
1128  * the zoneid, ill, and label. Used for the data paths. See also
1129  * ire_route_recursive_dstonly.
1130  *
1131  * If ill is set this means we will match it by adding MATCH_IRE_ILL.
1132  *
1133  * If allocate is not set then we will only inspect the existing IREs; never
1134  * create an IRE_IF_CLONE. This is used on the receive side when we are not
1135  * forwarding.
1136  *
1137  * Note that this function never returns NULL. It returns an IRE_NOROUTE
1138  * instead.
1139  *
1140  * If we find any IRE_LOCAL|BROADCAST etc past the first iteration it
1141  * is an error.
1142  * Allow at most one RTF_INDIRECT.
1143  */
1144 ire_t *
1145 ire_route_recursive_impl_v6(ire_t *ire,
1146     const in6_addr_t *nexthop, uint_t ire_type, const ill_t *ill_arg,
1147     zoneid_t zoneid, const ts_label_t *tsl, uint_t match_args,
1148     boolean_t allocate, uint32_t xmit_hint, ip_stack_t *ipst,
1149     in6_addr_t *setsrcp, tsol_ire_gw_secattr_t **gwattrp, uint_t *generationp)
1150 {
1151 	int		i, j;
1152 	in6_addr_t	v6nexthop = *nexthop;
1153 	ire_t		*ires[MAX_IRE_RECURSION];
1154 	uint_t		generation;
1155 	uint_t		generations[MAX_IRE_RECURSION];
1156 	boolean_t	need_refrele = B_FALSE;
1157 	boolean_t	invalidate = B_FALSE;
1158 	int		prefs[MAX_IRE_RECURSION];
1159 	ill_t		*ill = NULL;
1160 
1161 	if (setsrcp != NULL)
1162 		ASSERT(IN6_IS_ADDR_UNSPECIFIED(setsrcp));
1163 	if (gwattrp != NULL)
1164 		ASSERT(*gwattrp == NULL);
1165 
1166 	if (ill_arg != NULL)
1167 		match_args |= MATCH_IRE_ILL;
1168 
1169 	/*
1170 	 * We iterate up to three times to resolve a route, even though
1171 	 * we have four slots in the array. The extra slot is for an
1172 	 * IRE_IF_CLONE we might need to create.
1173 	 */
1174 	i = 0;
1175 	while (i < MAX_IRE_RECURSION - 1) {
1176 		/* ire_ftable_lookup handles round-robin/ECMP */
1177 		if (ire == NULL) {
1178 			ire = ire_ftable_lookup_v6(&v6nexthop, 0, 0, ire_type,
1179 			    (ill_arg != NULL ? ill_arg : ill), zoneid, tsl,
1180 			    match_args, xmit_hint, ipst, &generation);
1181 		} else {
1182 			/* Caller passed it; extra hold since we will rele */
1183 			ire_refhold(ire);
1184 			if (generationp != NULL)
1185 				generation = *generationp;
1186 			else
1187 				generation = IRE_GENERATION_VERIFY;
1188 		}
1189 
1190 		if (ire == NULL)
1191 			ire = ire_reject(ipst, B_TRUE);
1192 
1193 		/* Need to return the ire with RTF_REJECT|BLACKHOLE */
1194 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
1195 			goto error;
1196 
1197 		ASSERT(!(ire->ire_type & IRE_MULTICAST)); /* Not in ftable */
1198 
1199 		if (i != 0) {
1200 			prefs[i] = ire_pref(ire);
1201 			/*
1202 			 * Don't allow anything unusual past the first
1203 			 * iteration.
1204 			 */
1205 			if ((ire->ire_type &
1206 			    (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST)) ||
1207 			    prefs[i] <= prefs[i-1]) {
1208 				ire_refrele(ire);
1209 				ire = ire_reject(ipst, B_TRUE);
1210 				goto error;
1211 			}
1212 		}
1213 		/* We have a usable IRE */
1214 		ires[i] = ire;
1215 		generations[i] = generation;
1216 		i++;
1217 
1218 		/* The first RTF_SETSRC address is passed back if setsrcp */
1219 		if ((ire->ire_flags & RTF_SETSRC) &&
1220 		    setsrcp != NULL && IN6_IS_ADDR_UNSPECIFIED(setsrcp)) {
1221 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(
1222 			    &ire->ire_setsrc_addr_v6));
1223 			*setsrcp = ire->ire_setsrc_addr_v6;
1224 		}
1225 
1226 		/* The first ire_gw_secattr is passed back if gwattrp */
1227 		if (ire->ire_gw_secattr != NULL &&
1228 		    gwattrp != NULL && *gwattrp == NULL)
1229 			*gwattrp = ire->ire_gw_secattr;
1230 
1231 		/*
1232 		 * Check if we have a short-cut pointer to an IRE for this
1233 		 * destination, and that the cached dependency isn't stale.
1234 		 * In that case we've rejoined an existing tree towards a
1235 		 * parent, thus we don't need to continue the loop to
1236 		 * discover the rest of the tree.
1237 		 */
1238 		mutex_enter(&ire->ire_lock);
1239 		if (ire->ire_dep_parent != NULL &&
1240 		    ire->ire_dep_parent->ire_generation ==
1241 		    ire->ire_dep_parent_generation) {
1242 			mutex_exit(&ire->ire_lock);
1243 			ire = NULL;
1244 			goto done;
1245 		}
1246 		mutex_exit(&ire->ire_lock);
1247 
1248 		/*
1249 		 * If this type should have an ire_nce_cache (even if it
1250 		 * doesn't yet have one) then we are done. Includes
1251 		 * IRE_INTERFACE with a full 128 bit mask.
1252 		 */
1253 		if (ire->ire_nce_capable) {
1254 			ire = NULL;
1255 			goto done;
1256 		}
1257 		ASSERT(!(ire->ire_type & IRE_IF_CLONE));
1258 		/*
1259 		 * For an IRE_INTERFACE we create an IRE_IF_CLONE for this
1260 		 * particular destination
1261 		 */
1262 		if (ire->ire_type & IRE_INTERFACE) {
1263 			ire_t		*clone;
1264 
1265 			ASSERT(ire->ire_masklen != IPV6_ABITS);
1266 
1267 			/*
1268 			 * In the case of ip_input and ILLF_FORWARDING not
1269 			 * being set, and in the case of RTM_GET,
1270 			 * there is no point in allocating
1271 			 * an IRE_IF_CLONE. We return the IRE_INTERFACE.
1272 			 * Note that !allocate can result in a ire_dep_parent
1273 			 * which is IRE_IF_* without an IRE_IF_CLONE.
1274 			 * We recover from that when we need to send packets
1275 			 * by ensuring that the generations become
1276 			 * IRE_GENERATION_VERIFY in this case.
1277 			 */
1278 			if (!allocate) {
1279 				invalidate = B_TRUE;
1280 				ire = NULL;
1281 				goto done;
1282 			}
1283 
1284 			clone = ire_create_if_clone(ire, &v6nexthop,
1285 			    &generation);
1286 			if (clone == NULL) {
1287 				/*
1288 				 * Temporary failure - no memory.
1289 				 * Don't want caller to cache IRE_NOROUTE.
1290 				 */
1291 				invalidate = B_TRUE;
1292 				ire = ire_blackhole(ipst, B_TRUE);
1293 				goto error;
1294 			}
1295 			/*
1296 			 * Make clone next to last entry and the
1297 			 * IRE_INTERFACE the last in the dependency
1298 			 * chain since the clone depends on the
1299 			 * IRE_INTERFACE.
1300 			 */
1301 			ASSERT(i >= 1);
1302 			ASSERT(i < MAX_IRE_RECURSION);
1303 
1304 			ires[i] = ires[i-1];
1305 			generations[i] = generations[i-1];
1306 			ires[i-1] = clone;
1307 			generations[i-1] = generation;
1308 			i++;
1309 
1310 			ire = NULL;
1311 			goto done;
1312 		}
1313 
1314 		/*
1315 		 * We only match on the type and optionally ILL when
1316 		 * recursing. The type match is used by some callers
1317 		 * to exclude certain types (such as IRE_IF_CLONE or
1318 		 * IRE_LOCAL|IRE_LOOPBACK).
1319 		 */
1320 		match_args &= MATCH_IRE_TYPE;
1321 		v6nexthop = ire->ire_gateway_addr_v6;
1322 		if (ill == NULL && ire->ire_ill != NULL) {
1323 			ill = ire->ire_ill;
1324 			need_refrele = B_TRUE;
1325 			ill_refhold(ill);
1326 			match_args |= MATCH_IRE_ILL;
1327 		}
1328 		/*
1329 		 * We set the prefs[i] value above if i > 0. We've already
1330 		 * done i++ so i is one in the case of the first time around.
1331 		 */
1332 		if (i == 1)
1333 			prefs[0] = ire_pref(ire);
1334 		ire = NULL;
1335 	}
1336 	ASSERT(ire == NULL);
1337 	ire = ire_reject(ipst, B_TRUE);
1338 
1339 error:
1340 	ASSERT(ire != NULL);
1341 	if (need_refrele)
1342 		ill_refrele(ill);
1343 
1344 	/*
1345 	 * In the case of MULTIRT we want to try a different IRE the next
1346 	 * time. We let the next packet retry in that case.
1347 	 */
1348 	if (i > 0 && (ires[0]->ire_flags & RTF_MULTIRT))
1349 		(void) ire_no_good(ires[0]);
1350 
1351 cleanup:
1352 	/* cleanup ires[i] */
1353 	ire_dep_unbuild(ires, i);
1354 	for (j = 0; j < i; j++)
1355 		ire_refrele(ires[j]);
1356 
1357 	ASSERT(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE));
1358 	/*
1359 	 * Use IRE_GENERATION_VERIFY to ensure that ip_output will redo the
1360 	 * ip_select_route since the reject or lack of memory might be gone.
1361 	 */
1362 	if (generationp != NULL)
1363 		*generationp = IRE_GENERATION_VERIFY;
1364 	return (ire);
1365 
1366 done:
1367 	ASSERT(ire == NULL);
1368 	if (need_refrele)
1369 		ill_refrele(ill);
1370 
1371 	/* Build dependencies */
1372 	if (i > 1 && !ire_dep_build(ires, generations, i)) {
1373 		/* Something in chain was condemned; tear it apart */
1374 		ire = ire_blackhole(ipst, B_TRUE);
1375 		goto cleanup;
1376 	}
1377 
1378 	/*
1379 	 * Release all refholds except the one for ires[0] that we
1380 	 * will return to the caller.
1381 	 */
1382 	for (j = 1; j < i; j++)
1383 		ire_refrele(ires[j]);
1384 
1385 	if (invalidate) {
1386 		/*
1387 		 * Since we needed to allocate but couldn't we need to make
1388 		 * sure that the dependency chain is rebuilt the next time.
1389 		 */
1390 		ire_dep_invalidate_generations(ires[0]);
1391 		generation = IRE_GENERATION_VERIFY;
1392 	} else {
1393 		/*
1394 		 * IREs can have been added or deleted while we did the
1395 		 * recursive lookup and we can't catch those until we've built
1396 		 * the dependencies. We verify the stored
1397 		 * ire_dep_parent_generation to catch any such changes and
1398 		 * return IRE_GENERATION_VERIFY (which will cause
1399 		 * ip_select_route to be called again so we can redo the
1400 		 * recursive lookup next time we send a packet.
1401 		 */
1402 		if (ires[0]->ire_dep_parent == NULL)
1403 			generation = ires[0]->ire_generation;
1404 		else
1405 			generation = ire_dep_validate_generations(ires[0]);
1406 		if (generations[0] != ires[0]->ire_generation) {
1407 			/* Something changed at the top */
1408 			generation = IRE_GENERATION_VERIFY;
1409 		}
1410 	}
1411 	if (generationp != NULL)
1412 		*generationp = generation;
1413 
1414 	return (ires[0]);
1415 }
1416 
1417 ire_t *
1418 ire_route_recursive_v6(const in6_addr_t *nexthop, uint_t ire_type,
1419     const ill_t *ill, zoneid_t zoneid, const ts_label_t *tsl, uint_t match_args,
1420     boolean_t allocate, uint32_t xmit_hint, ip_stack_t *ipst,
1421     in6_addr_t *setsrcp, tsol_ire_gw_secattr_t **gwattrp, uint_t *generationp)
1422 {
1423 	return (ire_route_recursive_impl_v6(NULL, nexthop, ire_type, ill,
1424 	    zoneid, tsl, match_args, allocate, xmit_hint, ipst, setsrcp,
1425 	    gwattrp, generationp));
1426 }
1427 
1428 /*
1429  * Recursively look for a route to the destination.
1430  * We only handle a destination match here, yet we have the same arguments
1431  * as the full match to allow function pointers to select between the two.
1432  *
1433  * Note that this function never returns NULL. It returns an IRE_NOROUTE
1434  * instead.
1435  *
1436  * If we find any IRE_LOCAL|BROADCAST etc past the first iteration it
1437  * is an error.
1438  * Allow at most one RTF_INDIRECT.
1439  */
1440 ire_t *
1441 ire_route_recursive_dstonly_v6(const in6_addr_t *nexthop, boolean_t allocate,
1442     uint32_t xmit_hint, ip_stack_t *ipst)
1443 {
1444 	ire_t	*ire;
1445 	ire_t	*ire1;
1446 	uint_t	generation;
1447 
1448 	/* ire_ftable_lookup handles round-robin/ECMP */
1449 	ire = ire_ftable_lookup_simple_v6(nexthop, xmit_hint, ipst,
1450 	    &generation);
1451 	ASSERT(ire != NULL);
1452 
1453 	/*
1454 	 * If this type should have an ire_nce_cache (even if it
1455 	 * doesn't yet have one) then we are done. Includes
1456 	 * IRE_INTERFACE with a full 128 bit mask.
1457 	 */
1458 	if (ire->ire_nce_capable)
1459 		return (ire);
1460 
1461 	/*
1462 	 * If the IRE has a current cached parent we know that the whole
1463 	 * parent chain is current, hence we don't need to discover and
1464 	 * build any dependencies by doing a recursive lookup.
1465 	 */
1466 	mutex_enter(&ire->ire_lock);
1467 	if (ire->ire_dep_parent != NULL &&
1468 	    ire->ire_dep_parent->ire_generation ==
1469 	    ire->ire_dep_parent_generation) {
1470 		mutex_exit(&ire->ire_lock);
1471 		return (ire);
1472 	}
1473 	mutex_exit(&ire->ire_lock);
1474 
1475 	/*
1476 	 * Fallback to loop in the normal code starting with the ire
1477 	 * we found. Normally this would return the same ire.
1478 	 */
1479 	ire1 = ire_route_recursive_impl_v6(ire, nexthop, 0, NULL, ALL_ZONES,
1480 	    NULL, MATCH_IRE_DSTONLY, allocate, xmit_hint, ipst, NULL, NULL,
1481 	    &generation);
1482 	ire_refrele(ire);
1483 	return (ire1);
1484 }
1485