xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision f1c454b4)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains routines that manipulate Internet Routing Entries (IREs).
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/strsubr.h>
36 #include <sys/ddi.h>
37 #include <sys/cmn_err.h>
38 #include <sys/policy.h>
39 
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <net/if_dl.h>
48 #include <netinet/ip6.h>
49 #include <netinet/icmp6.h>
50 
51 #include <inet/common.h>
52 #include <inet/mi.h>
53 #include <inet/ip.h>
54 #include <inet/ip6.h>
55 #include <inet/ip_ndp.h>
56 #include <inet/arp.h>
57 #include <inet/ip_if.h>
58 #include <inet/ip_ire.h>
59 #include <inet/ip_ftable.h>
60 #include <inet/ip_rts.h>
61 #include <inet/nd.h>
62 
63 #include <inet/tcp.h>
64 #include <inet/ipclassifier.h>
65 #include <sys/zone.h>
66 #include <sys/cpuvar.h>
67 
68 #include <sys/tsol/label.h>
69 #include <sys/tsol/tnet.h>
70 
71 struct kmem_cache *rt_entry_cache;
72 
73 typedef struct nce_clookup_s {
74 	ipaddr_t ncecl_addr;
75 	boolean_t ncecl_found;
76 } nce_clookup_t;
77 
78 /*
79  * Synchronization notes:
80  *
81  * The fields of the ire_t struct are protected in the following way :
82  *
83  * ire_next/ire_ptpn
84  *
85  *	- bucket lock of the forwarding table in which is ire stored.
86  *
87  * ire_ill, ire_u *except* ire_gateway_addr[v6], ire_mask,
88  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags,
89  * ire_bucket
90  *
91  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
92  *	  we don't need a lock whenever these fields are accessed.
93  *
94  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
95  *        ire_add before inserting in the bucket and never
96  *        changes after that. Thus we don't need a lock whenever these
97  *	  fields are accessed.
98  *
99  * ire_gateway_addr_v4[v6]
100  *
101  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
102  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
103  *	  it assumed to be atomic and hence the other parts of the code
104  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
105  *	  and hence any access to it uses ire_lock to get/set the right value.
106  *
107  * ire_refcnt, ire_identical_ref
108  *
109  *	- Updated atomically using atomic_add_32
110  *
111  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
112  *
113  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
114  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
115  *
116  * ire_generation
117  *	- Under ire_lock
118  *
119  * ire_nce_cache
120  *	- Under ire_lock
121  *
122  * ire_dep_parent (To next IRE in recursive lookup chain)
123  *	- Under ips_ire_dep_lock. Write held when modifying. Read held when
124  *	  walking. We also hold ire_lock when modifying to allow the data path
125  *	  to only acquire ire_lock.
126  *
127  * ire_dep_parent_generation (Generation number from ire_dep_parent)
128  *	- Under ips_ire_dep_lock and/or ire_lock. (A read claim on the dep_lock
129  *	  and ire_lock held when modifying)
130  *
131  * ire_dep_children (From parent to first child)
132  * ire_dep_sib_next (linked list of siblings)
133  * ire_dep_sib_ptpn (linked list of siblings)
134  *	- Under ips_ire_dep_lock. Write held when modifying. Read held when
135  *	  walking.
136  *
137  * As we always hold the bucket locks in all the places while accessing
138  * the above values, it is natural to use them for protecting them.
139  *
140  * We have a forwarding table for IPv4 and IPv6. The IPv6 forwarding table
141  * (ip_forwarding_table_v6) is an array of pointers to arrays of irb_t
142  * structures. ip_forwarding_table_v6 is allocated dynamically in
143  * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads
144  * initializing the same bucket. Once a bucket is initialized, it is never
145  * de-alloacted. This assumption enables us to access
146  * ip_forwarding_table_v6[i] without any locks.
147  *
148  * The forwarding table for IPv4 is a radix tree whose leaves
149  * are rt_entry structures containing the irb_t for the rt_dst. The irb_t
150  * for IPv4 is dynamically allocated and freed.
151  *
152  * Each irb_t - ire bucket structure has a lock to protect
153  * a bucket and the ires residing in the bucket have a back pointer to
154  * the bucket structure. It also has a reference count for the number
155  * of threads walking the bucket - irb_refcnt which is bumped up
156  * using the irb_refhold function. The flags irb_marks can be
157  * set to IRB_MARK_CONDEMNED indicating that there are some ires
158  * in this bucket that are IRE_IS_CONDEMNED and the
159  * last thread to leave the bucket should delete the ires. Usually
160  * this is done by the irb_refrele function which is used to decrement
161  * the reference count on a bucket. See comments above irb_t structure
162  * definition in ip.h for further details.
163  *
164  * The ire_refhold/ire_refrele functions operate on the ire which increments/
165  * decrements the reference count, ire_refcnt, atomically on the ire.
166  * ire_refcnt is modified only using those functions. Operations on the IRE
167  * could be described as follows :
168  *
169  * CREATE an ire with reference count initialized to 1.
170  *
171  * ADDITION of an ire holds the bucket lock, checks for duplicates
172  * and then adds the ire. ire_add returns the ire after
173  * bumping up once more i.e the reference count is 2. This is to avoid
174  * an extra lookup in the functions calling ire_add which wants to
175  * work with the ire after adding.
176  *
177  * LOOKUP of an ire bumps up the reference count using ire_refhold
178  * function. It is valid to bump up the referece count of the IRE,
179  * after the lookup has returned an ire. Following are the lookup
180  * functions that return an HELD ire :
181  *
182  * ire_ftable_lookup[_v6], ire_lookup_multi_ill[_v6]
183  *
184  * DELETION of an ire holds the bucket lock, removes it from the list
185  * and then decrements the reference count for having removed from the list
186  * by using the ire_refrele function. If some other thread has looked up
187  * the ire, the reference count would have been bumped up and hence
188  * this ire will not be freed once deleted. It will be freed once the
189  * reference count drops to zero.
190  *
191  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
192  * lookups acquire the bucket lock as RW_READER.
193  *
194  * The general rule is to do the ire_refrele in the function
195  * that is passing the ire as an argument.
196  *
197  * In trying to locate ires the following points are to be noted.
198  *
199  * IRE_IS_CONDEMNED signifies that the ire has been logically deleted and is
200  * to be ignored when walking the ires using ire_next.
201  *
202  * Zones note:
203  *	Walking IREs within a given zone also walks certain ires in other
204  *	zones.  This is done intentionally.  IRE walks with a specified
205  *	zoneid are used only when doing informational reports, and
206  *	zone users want to see things that they can access. See block
207  *	comment in ire_walk_ill_match().
208  */
209 
210 /*
211  * The size of the forwarding table.  We will make sure that it is a
212  * power of 2 in ip_ire_init().
213  * Setable in /etc/system
214  */
215 uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
216 
217 struct	kmem_cache	*ire_cache;
218 struct	kmem_cache	*ncec_cache;
219 struct	kmem_cache	*nce_cache;
220 
221 static ire_t	ire_null;
222 
223 static ire_t	*ire_add_v4(ire_t *ire);
224 static void	ire_delete_v4(ire_t *ire);
225 static void	ire_dep_invalidate_children(ire_t *child);
226 static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
227     zoneid_t zoneid, ip_stack_t *);
228 static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
229     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
230 #ifdef DEBUG
231 static void	ire_trace_cleanup(const ire_t *);
232 #endif
233 static void	ire_dep_incr_generation_locked(ire_t *);
234 
235 /*
236  * Following are the functions to increment/decrement the reference
237  * count of the IREs and IRBs (ire bucket).
238  *
239  * 1) We bump up the reference count of an IRE to make sure that
240  *    it does not get deleted and freed while we are using it.
241  *    Typically all the lookup functions hold the bucket lock,
242  *    and look for the IRE. If it finds an IRE, it bumps up the
243  *    reference count before dropping the lock. Sometimes we *may* want
244  *    to bump up the reference count after we *looked* up i.e without
245  *    holding the bucket lock. So, the ire_refhold function does not assert
246  *    on the bucket lock being held. Any thread trying to delete from
247  *    the hash bucket can still do so but cannot free the IRE if
248  *    ire_refcnt is not 0.
249  *
250  * 2) We bump up the reference count on the bucket where the IRE resides
251  *    (IRB), when we want to prevent the IREs getting deleted from a given
252  *    hash bucket. This makes life easier for ire_walk type functions which
253  *    wants to walk the IRE list, call a function, but needs to drop
254  *    the bucket lock to prevent recursive rw_enters. While the
255  *    lock is dropped, the list could be changed by other threads or
256  *    the same thread could end up deleting the ire or the ire pointed by
257  *    ire_next. ire_refholding the ire or ire_next is not sufficient as
258  *    a delete will still remove the ire from the bucket while we have
259  *    dropped the lock and hence the ire_next would be NULL. Thus, we
260  *    need a mechanism to prevent deletions from a given bucket.
261  *
262  *    To prevent deletions, we bump up the reference count on the
263  *    bucket. If the bucket is held, ire_delete just marks both
264  *    the ire and irb as CONDEMNED. When the
265  *    reference count on the bucket drops to zero, all the CONDEMNED ires
266  *    are deleted. We don't have to bump up the reference count on the
267  *    bucket if we are walking the bucket and never have to drop the bucket
268  *    lock. Note that irb_refhold does not prevent addition of new ires
269  *    in the list. It is okay because addition of new ires will not cause
270  *    ire_next to point to freed memory. We do irb_refhold only when
271  *    all of the 3 conditions are true :
272  *
273  *    1) The code needs to walk the IRE bucket from start to end.
274  *    2) It may have to drop the bucket lock sometimes while doing (1)
275  *    3) It does not want any ires to be deleted meanwhile.
276  */
277 
278 /*
279  * Bump up the reference count on the hash bucket - IRB to
280  * prevent ires from being deleted in this bucket.
281  */
282 void
283 irb_refhold(irb_t *irb)
284 {
285 	rw_enter(&irb->irb_lock, RW_WRITER);
286 	irb->irb_refcnt++;
287 	ASSERT(irb->irb_refcnt != 0);
288 	rw_exit(&irb->irb_lock);
289 }
290 
291 void
292 irb_refhold_locked(irb_t *irb)
293 {
294 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
295 	irb->irb_refcnt++;
296 	ASSERT(irb->irb_refcnt != 0);
297 }
298 
299 /*
300  * Note: when IRB_MARK_DYNAMIC is not set the irb_t
301  * is statically allocated, so that when the irb_refcnt goes to 0,
302  * we simply clean up the ire list and continue.
303  */
304 void
305 irb_refrele(irb_t *irb)
306 {
307 	if (irb->irb_marks & IRB_MARK_DYNAMIC) {
308 		irb_refrele_ftable(irb);
309 	} else {
310 		rw_enter(&irb->irb_lock, RW_WRITER);
311 		ASSERT(irb->irb_refcnt != 0);
312 		if (--irb->irb_refcnt	== 0 &&
313 		    (irb->irb_marks & IRB_MARK_CONDEMNED)) {
314 			ire_t *ire_list;
315 
316 			ire_list = ire_unlink(irb);
317 			rw_exit(&irb->irb_lock);
318 			ASSERT(ire_list != NULL);
319 			ire_cleanup(ire_list);
320 		} else {
321 			rw_exit(&irb->irb_lock);
322 		}
323 	}
324 }
325 
326 
327 /*
328  * Bump up the reference count on the IRE. We cannot assert that the
329  * bucket lock is being held as it is legal to bump up the reference
330  * count after the first lookup has returned the IRE without
331  * holding the lock.
332  */
333 void
334 ire_refhold(ire_t *ire)
335 {
336 	atomic_add_32(&(ire)->ire_refcnt, 1);
337 	ASSERT((ire)->ire_refcnt != 0);
338 #ifdef DEBUG
339 	ire_trace_ref(ire);
340 #endif
341 }
342 
343 void
344 ire_refhold_notr(ire_t *ire)
345 {
346 	atomic_add_32(&(ire)->ire_refcnt, 1);
347 	ASSERT((ire)->ire_refcnt != 0);
348 }
349 
350 void
351 ire_refhold_locked(ire_t *ire)
352 {
353 #ifdef DEBUG
354 	ire_trace_ref(ire);
355 #endif
356 	ire->ire_refcnt++;
357 }
358 
359 /*
360  * Release a ref on an IRE.
361  *
362  * Must not be called while holding any locks. Otherwise if this is
363  * the last reference to be released there is a chance of recursive mutex
364  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
365  * to restart an ioctl. The one exception is when the caller is sure that
366  * this is not the last reference to be released. Eg. if the caller is
367  * sure that the ire has not been deleted and won't be deleted.
368  *
369  * In architectures e.g sun4u, where atomic_add_32_nv is just
370  * a cas, we need to maintain the right memory barrier semantics
371  * as that of mutex_exit i.e all the loads and stores should complete
372  * before the cas is executed. membar_exit() does that here.
373  */
374 void
375 ire_refrele(ire_t *ire)
376 {
377 #ifdef DEBUG
378 	ire_untrace_ref(ire);
379 #endif
380 	ASSERT((ire)->ire_refcnt != 0);
381 	membar_exit();
382 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)
383 		ire_inactive(ire);
384 }
385 
386 void
387 ire_refrele_notr(ire_t *ire)
388 {
389 	ASSERT((ire)->ire_refcnt != 0);
390 	membar_exit();
391 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)
392 		ire_inactive(ire);
393 }
394 
395 /*
396  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
397  * IOCTL[s].  The NO_REPLY form is used by TCP to tell IP that it is
398  * having problems reaching a particular destination.
399  * This will make IP consider alternate routes (e.g., when there are
400  * muliple default routes), and it will also make IP discard any (potentially)
401  * stale redirect.
402  * Management processes may want to use the version that generates a reply.
403  *
404  * With the use of NUD like behavior for IPv4/ARP in addition to IPv6
405  * this function shouldn't be necessary for IP to recover from a bad redirect,
406  * a bad default router (when there are multiple default routers), or
407  * a stale ND/ARP entry. But we retain it in any case.
408  * For instance, this is helpful when TCP suspects a failure before NUD does.
409  */
410 int
411 ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
412 {
413 	uchar_t		*addr_ucp;
414 	uint_t		ipversion;
415 	sin_t		*sin;
416 	sin6_t		*sin6;
417 	ipaddr_t	v4addr;
418 	in6_addr_t	v6addr;
419 	ire_t		*ire;
420 	ipid_t		*ipid;
421 	zoneid_t	zoneid;
422 	ip_stack_t	*ipst;
423 
424 	ASSERT(q->q_next == NULL);
425 	zoneid = IPCL_ZONEID(Q_TO_CONN(q));
426 	ipst = CONNQ_TO_IPST(q);
427 
428 	/*
429 	 * Check privilege using the ioctl credential; if it is NULL
430 	 * then this is a kernel message and therefor privileged.
431 	 */
432 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
433 		return (EPERM);
434 
435 	ipid = (ipid_t *)mp->b_rptr;
436 
437 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
438 	    ipid->ipid_addr_length);
439 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
440 		return (EINVAL);
441 	switch (ipid->ipid_addr_length) {
442 	case sizeof (sin_t):
443 		/*
444 		 * got complete (sockaddr) address - increment addr_ucp to point
445 		 * at the ip_addr field.
446 		 */
447 		sin = (sin_t *)addr_ucp;
448 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
449 		ipversion = IPV4_VERSION;
450 		break;
451 	case sizeof (sin6_t):
452 		/*
453 		 * got complete (sockaddr) address - increment addr_ucp to point
454 		 * at the ip_addr field.
455 		 */
456 		sin6 = (sin6_t *)addr_ucp;
457 		addr_ucp = (uchar_t *)&sin6->sin6_addr;
458 		ipversion = IPV6_VERSION;
459 		break;
460 	default:
461 		return (EINVAL);
462 	}
463 	if (ipversion == IPV4_VERSION) {
464 		/* Extract the destination address. */
465 		bcopy(addr_ucp, &v4addr, IP_ADDR_LEN);
466 
467 		ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
468 		    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
469 	} else {
470 		/* Extract the destination address. */
471 		bcopy(addr_ucp, &v6addr, IPV6_ADDR_LEN);
472 
473 		ire = ire_ftable_lookup_v6(&v6addr, NULL, NULL, 0, NULL,
474 		    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
475 	}
476 	if (ire != NULL) {
477 		if (ipversion == IPV4_VERSION) {
478 			ip_rts_change(RTM_LOSING, ire->ire_addr,
479 			    ire->ire_gateway_addr, ire->ire_mask,
480 			    (Q_TO_CONN(q))->conn_laddr_v4,  0, 0, 0,
481 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA),
482 			    ire->ire_ipst);
483 		}
484 		(void) ire_no_good(ire);
485 		ire_refrele(ire);
486 	}
487 	return (0);
488 }
489 
490 /*
491  * Initialize the ire that is specific to IPv4 part and call
492  * ire_init_common to finish it.
493  * Returns zero or errno.
494  */
495 int
496 ire_init_v4(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *gateway,
497     ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags,
498     tsol_gc_t *gc, ip_stack_t *ipst)
499 {
500 	int error;
501 
502 	/*
503 	 * Reject IRE security attribute creation/initialization
504 	 * if system is not running in Trusted mode.
505 	 */
506 	if (gc != NULL && !is_system_labeled())
507 		return (EINVAL);
508 
509 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
510 
511 	if (addr != NULL)
512 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
513 	if (gateway != NULL)
514 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
515 
516 	/* Make sure we don't have stray values in some fields */
517 	switch (type) {
518 	case IRE_LOOPBACK:
519 		bcopy(&ire->ire_addr, &ire->ire_gateway_addr, IP_ADDR_LEN);
520 		/* FALLTHRU */
521 	case IRE_HOST:
522 	case IRE_BROADCAST:
523 	case IRE_LOCAL:
524 	case IRE_IF_CLONE:
525 		ire->ire_mask = IP_HOST_MASK;
526 		ire->ire_masklen = IPV4_ABITS;
527 		break;
528 	case IRE_PREFIX:
529 	case IRE_DEFAULT:
530 	case IRE_IF_RESOLVER:
531 	case IRE_IF_NORESOLVER:
532 		if (mask != NULL) {
533 			bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
534 			ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
535 		}
536 		break;
537 	case IRE_MULTICAST:
538 	case IRE_NOROUTE:
539 		ASSERT(mask == NULL);
540 		break;
541 	default:
542 		ASSERT(0);
543 		return (EINVAL);
544 	}
545 
546 	error = ire_init_common(ire, type, ill, zoneid, flags, IPV4_VERSION,
547 	    gc, ipst);
548 	if (error != NULL)
549 		return (error);
550 
551 	/* Determine which function pointers to use */
552 	ire->ire_postfragfn = ip_xmit;		/* Common case */
553 
554 	switch (ire->ire_type) {
555 	case IRE_LOCAL:
556 		ire->ire_sendfn = ire_send_local_v4;
557 		ire->ire_recvfn = ire_recv_local_v4;
558 		ASSERT(ire->ire_ill != NULL);
559 		if (ire->ire_ill->ill_flags & ILLF_NOACCEPT)
560 			ire->ire_recvfn = ire_recv_noaccept_v6;
561 		break;
562 	case IRE_LOOPBACK:
563 		ire->ire_sendfn = ire_send_local_v4;
564 		ire->ire_recvfn = ire_recv_loopback_v4;
565 		break;
566 	case IRE_BROADCAST:
567 		ire->ire_postfragfn = ip_postfrag_loopcheck;
568 		ire->ire_sendfn = ire_send_broadcast_v4;
569 		ire->ire_recvfn = ire_recv_broadcast_v4;
570 		break;
571 	case IRE_MULTICAST:
572 		ire->ire_postfragfn = ip_postfrag_loopcheck;
573 		ire->ire_sendfn = ire_send_multicast_v4;
574 		ire->ire_recvfn = ire_recv_multicast_v4;
575 		break;
576 	default:
577 		/*
578 		 * For IRE_IF_ALL and IRE_OFFLINK we forward received
579 		 * packets by default.
580 		 */
581 		ire->ire_sendfn = ire_send_wire_v4;
582 		ire->ire_recvfn = ire_recv_forward_v4;
583 		break;
584 	}
585 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
586 		ire->ire_sendfn = ire_send_noroute_v4;
587 		ire->ire_recvfn = ire_recv_noroute_v4;
588 	} else if (ire->ire_flags & RTF_MULTIRT) {
589 		ire->ire_postfragfn = ip_postfrag_multirt_v4;
590 		ire->ire_sendfn = ire_send_multirt_v4;
591 		/* Multirt receive of broadcast uses ire_recv_broadcast_v4 */
592 		if (ire->ire_type != IRE_BROADCAST)
593 			ire->ire_recvfn = ire_recv_multirt_v4;
594 	}
595 	ire->ire_nce_capable = ire_determine_nce_capable(ire);
596 	return (0);
597 }
598 
599 /*
600  * Determine ire_nce_capable
601  */
602 boolean_t
603 ire_determine_nce_capable(ire_t *ire)
604 {
605 	int max_masklen;
606 
607 	if ((ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
608 	    (ire->ire_type & IRE_MULTICAST))
609 		return (B_TRUE);
610 
611 	if (ire->ire_ipversion == IPV4_VERSION)
612 		max_masklen = IPV4_ABITS;
613 	else
614 		max_masklen = IPV6_ABITS;
615 
616 	if ((ire->ire_type & IRE_ONLINK) && ire->ire_masklen == max_masklen)
617 		return (B_TRUE);
618 	return (B_FALSE);
619 }
620 
621 /*
622  * ire_create is called to allocate and initialize a new IRE.
623  *
624  * NOTE : This is called as writer sometimes though not required
625  * by this function.
626  */
627 ire_t *
628 ire_create(uchar_t *addr, uchar_t *mask, uchar_t *gateway,
629     ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags, tsol_gc_t *gc,
630     ip_stack_t *ipst)
631 {
632 	ire_t	*ire;
633 	int	error;
634 
635 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
636 	if (ire == NULL) {
637 		DTRACE_PROBE(kmem__cache__alloc);
638 		return (NULL);
639 	}
640 	*ire = ire_null;
641 
642 	error = ire_init_v4(ire, addr, mask, gateway, type, ill, zoneid, flags,
643 	    gc, ipst);
644 	if (error != 0) {
645 		DTRACE_PROBE2(ire__init, ire_t *, ire, int, error);
646 		kmem_cache_free(ire_cache, ire);
647 		return (NULL);
648 	}
649 	return (ire);
650 }
651 
652 /*
653  * Common to IPv4 and IPv6
654  * Returns zero or errno.
655  */
656 int
657 ire_init_common(ire_t *ire, ushort_t type, ill_t *ill, zoneid_t zoneid,
658     uint_t flags, uchar_t ipversion, tsol_gc_t *gc, ip_stack_t *ipst)
659 {
660 	int error;
661 
662 #ifdef DEBUG
663 	if (ill != NULL) {
664 		if (ill->ill_isv6)
665 			ASSERT(ipversion == IPV6_VERSION);
666 		else
667 			ASSERT(ipversion == IPV4_VERSION);
668 	}
669 #endif /* DEBUG */
670 
671 	/*
672 	 * Create/initialize IRE security attribute only in Trusted mode;
673 	 * if the passed in gc is non-NULL, we expect that the caller
674 	 * has held a reference to it and will release it when this routine
675 	 * returns a failure, otherwise we own the reference.  We do this
676 	 * prior to initializing the rest IRE fields.
677 	 */
678 	if (is_system_labeled()) {
679 		if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST |
680 		    IRE_IF_ALL | IRE_MULTICAST | IRE_NOROUTE)) != 0) {
681 			/* release references on behalf of caller */
682 			if (gc != NULL)
683 				GC_REFRELE(gc);
684 		} else {
685 			error = tsol_ire_init_gwattr(ire, ipversion, gc);
686 			if (error != 0)
687 				return (error);
688 		}
689 	}
690 
691 	ire->ire_type = type;
692 	ire->ire_flags = RTF_UP | flags;
693 	ire->ire_create_time = (uint32_t)gethrestime_sec();
694 	ire->ire_generation = IRE_GENERATION_INITIAL;
695 
696 	/*
697 	 * The ill_ire_cnt isn't increased until
698 	 * the IRE is added to ensure that a walker will find
699 	 * all IREs that hold a reference on an ill.
700 	 *
701 	 * Note that ill_ire_multicast doesn't hold a ref on the ill since
702 	 * ire_add() is not called for the IRE_MULTICAST.
703 	 */
704 	ire->ire_ill = ill;
705 	ire->ire_zoneid = zoneid;
706 	ire->ire_ipversion = ipversion;
707 
708 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
709 	ire->ire_refcnt = 1;
710 	ire->ire_identical_ref = 1;	/* Number of ire_delete's needed */
711 	ire->ire_ipst = ipst;	/* No netstack_hold */
712 	ire->ire_trace_disable = B_FALSE;
713 
714 	return (0);
715 }
716 
717 /*
718  * This creates an IRE_BROADCAST based on the arguments.
719  * A mirror is ire_lookup_bcast().
720  *
721  * Any supression of unneeded ones is done in ire_add_v4.
722  * We add one IRE_BROADCAST per address. ire_send_broadcast_v4()
723  * takes care of generating a loopback copy of the packet.
724  */
725 ire_t **
726 ire_create_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid, ire_t **irep)
727 {
728 	ip_stack_t	*ipst = ill->ill_ipst;
729 
730 	ASSERT(IAM_WRITER_ILL(ill));
731 
732 	*irep++ = ire_create(
733 	    (uchar_t *)&addr,			/* dest addr */
734 	    (uchar_t *)&ip_g_all_ones,		/* mask */
735 	    NULL,				/* no gateway */
736 	    IRE_BROADCAST,
737 	    ill,
738 	    zoneid,
739 	    RTF_KERNEL,
740 	    NULL,
741 	    ipst);
742 
743 	return (irep);
744 }
745 
746 /*
747  * This looks up an IRE_BROADCAST based on the arguments.
748  * Mirrors ire_create_bcast().
749  */
750 ire_t *
751 ire_lookup_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
752 {
753 	ire_t		*ire;
754 	int		match_args;
755 
756 	match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_GW |
757 	    MATCH_IRE_MASK | MATCH_IRE_ZONEONLY;
758 
759 	if (IS_UNDER_IPMP(ill))
760 		match_args |= MATCH_IRE_TESTHIDDEN;
761 
762 	ire = ire_ftable_lookup_v4(
763 	    addr,				/* dest addr */
764 	    ip_g_all_ones,			/* mask */
765 	    0,					/* no gateway */
766 	    IRE_BROADCAST,
767 	    ill,
768 	    zoneid,
769 	    NULL,
770 	    match_args,
771 	    0,
772 	    ill->ill_ipst,
773 	    NULL);
774 	return (ire);
775 }
776 
777 /* Arrange to call the specified function for every IRE in the world. */
778 void
779 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
780 {
781 	ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
782 }
783 
784 void
785 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
786 {
787 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
788 }
789 
790 void
791 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
792 {
793 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
794 }
795 
796 /*
797  * Walk a particular version. version == 0 means both v4 and v6.
798  */
799 static void
800 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
801     ip_stack_t *ipst)
802 {
803 	if (vers != IPV6_VERSION) {
804 		/*
805 		 * ip_forwarding_table variable doesn't matter for IPv4 since
806 		 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
807 		 */
808 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
809 		    0, NULL,
810 		    NULL, zoneid, ipst);
811 	}
812 	if (vers != IPV4_VERSION) {
813 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
814 		    ipst->ips_ip6_ftable_hash_size,
815 		    ipst->ips_ip_forwarding_table_v6,
816 		    NULL, zoneid, ipst);
817 	}
818 }
819 
820 /*
821  * Arrange to call the specified function for every IRE that matches the ill.
822  */
823 void
824 ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
825     ill_t *ill)
826 {
827 	uchar_t vers = (ill->ill_isv6 ? IPV6_VERSION : IPV4_VERSION);
828 
829 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, vers, ill);
830 }
831 
832 /*
833  * Walk a particular ill and version.
834  */
835 static void
836 ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
837     void *arg, uchar_t vers, ill_t *ill)
838 {
839 	ip_stack_t	*ipst = ill->ill_ipst;
840 
841 	if (vers == IPV4_VERSION) {
842 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
843 		    IP_MASK_TABLE_SIZE,
844 		    0, NULL,
845 		    ill, ALL_ZONES, ipst);
846 	}
847 	if (vers != IPV4_VERSION) {
848 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
849 		    IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
850 		    ipst->ips_ip_forwarding_table_v6,
851 		    ill, ALL_ZONES, ipst);
852 	}
853 }
854 
855 /*
856  * Do the specific matching of IREs to shared-IP zones.
857  *
858  * We have the same logic as in ire_match_args but implemented slightly
859  * differently.
860  */
861 boolean_t
862 ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
863     ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
864 {
865 	ill_t *dst_ill = ire->ire_ill;
866 
867 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
868 
869 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
870 	    ire->ire_zoneid != ALL_ZONES) {
871 		/*
872 		 * We're walking the IREs for a specific zone. The only relevant
873 		 * IREs are:
874 		 * - all IREs with a matching ire_zoneid
875 		 * - IRE_IF_ALL IREs for interfaces with a usable source addr
876 		 *   with a matching zone
877 		 * - IRE_OFFLINK with a gateway reachable from the zone
878 		 * Note that ealier we only did the IRE_OFFLINK check for
879 		 * IRE_DEFAULT (and only when we had multiple IRE_DEFAULTs).
880 		 */
881 		if (ire->ire_type & IRE_ONLINK) {
882 			uint_t	ifindex;
883 
884 			/*
885 			 * Note there is no IRE_INTERFACE on vniN thus
886 			 * can't do an IRE lookup for a matching route.
887 			 */
888 			ifindex = dst_ill->ill_usesrc_ifindex;
889 			if (ifindex == 0)
890 				return (B_FALSE);
891 
892 			/*
893 			 * If there is a usable source address in the
894 			 * zone, then it's ok to return an
895 			 * IRE_INTERFACE
896 			 */
897 			if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
898 			    zoneid, ipst)) {
899 				return (B_FALSE);
900 			}
901 		}
902 		if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
903 			ipif_t	*tipif;
904 
905 			mutex_enter(&dst_ill->ill_lock);
906 			for (tipif = dst_ill->ill_ipif;
907 			    tipif != NULL; tipif = tipif->ipif_next) {
908 				if (!IPIF_IS_CONDEMNED(tipif) &&
909 				    (tipif->ipif_flags & IPIF_UP) &&
910 				    (tipif->ipif_zoneid == zoneid ||
911 				    tipif->ipif_zoneid == ALL_ZONES))
912 					break;
913 			}
914 			mutex_exit(&dst_ill->ill_lock);
915 			if (tipif == NULL) {
916 				return (B_FALSE);
917 			}
918 		}
919 	}
920 	/*
921 	 * Except for ALL_ZONES, we only match the offlink routes
922 	 * where ire_gateway_addr has an IRE_INTERFACE for the zoneid.
923 	 * Since we can have leftover routes after the IP addresses have
924 	 * changed, the global zone will also match offlink routes where the
925 	 * gateway is unreachable from any zone.
926 	 */
927 	if ((ire->ire_type & IRE_OFFLINK) && zoneid != ALL_ZONES) {
928 		in6_addr_t gw_addr_v6;
929 		boolean_t reach;
930 
931 		if (ire->ire_ipversion == IPV4_VERSION) {
932 			reach = ire_gateway_ok_zone_v4(ire->ire_gateway_addr,
933 			    zoneid, dst_ill, NULL, ipst, B_FALSE);
934 		} else {
935 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
936 			mutex_enter(&ire->ire_lock);
937 			gw_addr_v6 = ire->ire_gateway_addr_v6;
938 			mutex_exit(&ire->ire_lock);
939 
940 			reach = ire_gateway_ok_zone_v6(&gw_addr_v6, zoneid,
941 			    dst_ill, NULL, ipst, B_FALSE);
942 		}
943 		if (!reach) {
944 			if (zoneid != GLOBAL_ZONEID)
945 				return (B_FALSE);
946 
947 			/*
948 			 * Check if ALL_ZONES reachable - if not then let the
949 			 * global zone see it.
950 			 */
951 			if (ire->ire_ipversion == IPV4_VERSION) {
952 				reach = ire_gateway_ok_zone_v4(
953 				    ire->ire_gateway_addr, ALL_ZONES,
954 				    dst_ill, NULL, ipst, B_FALSE);
955 			} else {
956 				reach = ire_gateway_ok_zone_v6(&gw_addr_v6,
957 				    ALL_ZONES, dst_ill, NULL, ipst, B_FALSE);
958 			}
959 			if (reach) {
960 				/*
961 				 * Some other zone could see it, hence hide it
962 				 * in the global zone.
963 				 */
964 				return (B_FALSE);
965 			}
966 		}
967 	}
968 
969 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
970 	    (ire->ire_type & ire_type)) &&
971 	    ((!(match_flags & MATCH_IRE_ILL)) ||
972 	    (dst_ill == ill ||
973 	    dst_ill != NULL && IS_IN_SAME_ILLGRP(dst_ill, ill)))) {
974 		return (B_TRUE);
975 	}
976 	return (B_FALSE);
977 }
978 
979 int
980 rtfunc(struct radix_node *rn, void *arg)
981 {
982 	struct rtfuncarg *rtf = arg;
983 	struct rt_entry *rt;
984 	irb_t *irb;
985 	ire_t *ire;
986 	boolean_t ret;
987 
988 	rt = (struct rt_entry *)rn;
989 	ASSERT(rt != NULL);
990 	irb = &rt->rt_irb;
991 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
992 		if ((rtf->rt_match_flags != 0) ||
993 		    (rtf->rt_zoneid != ALL_ZONES)) {
994 			ret = ire_walk_ill_match(rtf->rt_match_flags,
995 			    rtf->rt_ire_type, ire,
996 			    rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
997 		} else {
998 			ret = B_TRUE;
999 		}
1000 		if (ret)
1001 			(*rtf->rt_func)(ire, rtf->rt_arg);
1002 	}
1003 	return (0);
1004 }
1005 
1006 /*
1007  * Walk the ftable entries that match the ill.
1008  */
1009 void
1010 ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
1011     void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
1012     ill_t *ill, zoneid_t zoneid,
1013     ip_stack_t *ipst)
1014 {
1015 	irb_t	*irb_ptr;
1016 	irb_t	*irb;
1017 	ire_t	*ire;
1018 	int i, j;
1019 	boolean_t ret;
1020 	struct rtfuncarg rtfarg;
1021 
1022 	ASSERT((!(match_flags & MATCH_IRE_ILL)) || (ill != NULL));
1023 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
1024 
1025 	/* knobs such that routine is called only for v6 case */
1026 	if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
1027 		for (i = (ftbl_sz - 1);  i >= 0; i--) {
1028 			if ((irb_ptr = ipftbl[i]) == NULL)
1029 				continue;
1030 			for (j = 0; j < htbl_sz; j++) {
1031 				irb = &irb_ptr[j];
1032 				if (irb->irb_ire == NULL)
1033 					continue;
1034 
1035 				irb_refhold(irb);
1036 				for (ire = irb->irb_ire; ire != NULL;
1037 				    ire = ire->ire_next) {
1038 					if (match_flags == 0 &&
1039 					    zoneid == ALL_ZONES) {
1040 						ret = B_TRUE;
1041 					} else {
1042 						ret =
1043 						    ire_walk_ill_match(
1044 						    match_flags,
1045 						    ire_type, ire, ill,
1046 						    zoneid, ipst);
1047 					}
1048 					if (ret)
1049 						(*func)(ire, arg);
1050 				}
1051 				irb_refrele(irb);
1052 			}
1053 		}
1054 	} else {
1055 		bzero(&rtfarg, sizeof (rtfarg));
1056 		rtfarg.rt_func = func;
1057 		rtfarg.rt_arg = arg;
1058 		if (match_flags != 0) {
1059 			rtfarg.rt_match_flags = match_flags;
1060 		}
1061 		rtfarg.rt_ire_type = ire_type;
1062 		rtfarg.rt_ill = ill;
1063 		rtfarg.rt_zoneid = zoneid;
1064 		rtfarg.rt_ipst = ipst;	/* No netstack_hold */
1065 		(void) ipst->ips_ip_ftable->rnh_walktree_mt(
1066 		    ipst->ips_ip_ftable,
1067 		    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
1068 	}
1069 }
1070 
1071 /*
1072  * This function takes a mask and returns
1073  * number of bits set in the mask. If no
1074  * bit is set it returns 0.
1075  * Assumes a contiguous mask.
1076  */
1077 int
1078 ip_mask_to_plen(ipaddr_t mask)
1079 {
1080 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
1081 }
1082 
1083 /*
1084  * Convert length for a mask to the mask.
1085  */
1086 ipaddr_t
1087 ip_plen_to_mask(uint_t masklen)
1088 {
1089 	if (masklen == 0)
1090 		return (0);
1091 
1092 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
1093 }
1094 
1095 void
1096 ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
1097 {
1098 	ill_t		*ill;
1099 
1100 	ill = ire->ire_ill;
1101 	if (ill != NULL)
1102 		mutex_exit(&ill->ill_lock);
1103 	rw_exit(&irb_ptr->irb_lock);
1104 }
1105 
1106 /*
1107  * ire_add_v[46] atomically make sure that the ill associated
1108  * with the new ire is not going away i.e., we check ILL_CONDEMNED.
1109  */
1110 int
1111 ire_atomic_start(irb_t *irb_ptr, ire_t *ire)
1112 {
1113 	ill_t		*ill;
1114 
1115 	ill = ire->ire_ill;
1116 
1117 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
1118 	if (ill != NULL) {
1119 		mutex_enter(&ill->ill_lock);
1120 
1121 		/*
1122 		 * Don't allow IRE's to be created on dying ills.
1123 		 */
1124 		if (ill->ill_state_flags & ILL_CONDEMNED) {
1125 			ire_atomic_end(irb_ptr, ire);
1126 			return (ENXIO);
1127 		}
1128 
1129 		if (IS_UNDER_IPMP(ill)) {
1130 			int	error = 0;
1131 			mutex_enter(&ill->ill_phyint->phyint_lock);
1132 			if (!ipmp_ill_is_active(ill) &&
1133 			    IRE_HIDDEN_TYPE(ire->ire_type) &&
1134 			    !ire->ire_testhidden) {
1135 				error = EINVAL;
1136 			}
1137 			mutex_exit(&ill->ill_phyint->phyint_lock);
1138 			if (error != 0) {
1139 				ire_atomic_end(irb_ptr, ire);
1140 				return (error);
1141 			}
1142 		}
1143 
1144 	}
1145 	return (0);
1146 }
1147 
1148 /*
1149  * Add a fully initialized IRE to the forwarding table.
1150  * This returns NULL on failure, or a held IRE on success.
1151  * Normally the returned IRE is the same as the argument. But a different
1152  * IRE will be returned if the added IRE is deemed identical to an existing
1153  * one. In that case ire_identical_ref will be increased.
1154  * The caller always needs to do an ire_refrele() on the returned IRE.
1155  */
1156 ire_t *
1157 ire_add(ire_t *ire)
1158 {
1159 	if (IRE_HIDDEN_TYPE(ire->ire_type) &&
1160 	    ire->ire_ill != NULL && IS_UNDER_IPMP(ire->ire_ill)) {
1161 		/*
1162 		 * IREs hosted on interfaces that are under IPMP
1163 		 * should be hidden so that applications don't
1164 		 * accidentally end up sending packets with test
1165 		 * addresses as their source addresses, or
1166 		 * sending out interfaces that are e.g. IFF_INACTIVE.
1167 		 * Hide them here.
1168 		 */
1169 		ire->ire_testhidden = B_TRUE;
1170 	}
1171 
1172 	if (ire->ire_ipversion == IPV6_VERSION)
1173 		return (ire_add_v6(ire));
1174 	else
1175 		return (ire_add_v4(ire));
1176 }
1177 
1178 /*
1179  * Add a fully initialized IPv4 IRE to the forwarding table.
1180  * This returns NULL on failure, or a held IRE on success.
1181  * Normally the returned IRE is the same as the argument. But a different
1182  * IRE will be returned if the added IRE is deemed identical to an existing
1183  * one. In that case ire_identical_ref will be increased.
1184  * The caller always needs to do an ire_refrele() on the returned IRE.
1185  */
1186 static ire_t *
1187 ire_add_v4(ire_t *ire)
1188 {
1189 	ire_t	*ire1;
1190 	irb_t	*irb_ptr;
1191 	ire_t	**irep;
1192 	int	match_flags;
1193 	int	error;
1194 	ip_stack_t	*ipst = ire->ire_ipst;
1195 
1196 	if (ire->ire_ill != NULL)
1197 		ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
1198 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1199 
1200 	/* Make sure the address is properly masked. */
1201 	ire->ire_addr &= ire->ire_mask;
1202 
1203 	match_flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
1204 
1205 	if (ire->ire_ill != NULL) {
1206 		match_flags |= MATCH_IRE_ILL;
1207 	}
1208 	irb_ptr = ire_get_bucket(ire);
1209 	if (irb_ptr == NULL) {
1210 		printf("no bucket for %p\n", (void *)ire);
1211 		ire_delete(ire);
1212 		return (NULL);
1213 	}
1214 
1215 	/*
1216 	 * Start the atomic add of the ire. Grab the ill lock,
1217 	 * the bucket lock. Check for condemned.
1218 	 */
1219 	error = ire_atomic_start(irb_ptr, ire);
1220 	if (error != 0) {
1221 		printf("no ire_atomic_start for %p\n", (void *)ire);
1222 		ire_delete(ire);
1223 		irb_refrele(irb_ptr);
1224 		return (NULL);
1225 	}
1226 	/*
1227 	 * If we are creating a hidden IRE, make sure we search for
1228 	 * hidden IREs when searching for duplicates below.
1229 	 * Otherwise, we might find an IRE on some other interface
1230 	 * that's not marked hidden.
1231 	 */
1232 	if (ire->ire_testhidden)
1233 		match_flags |= MATCH_IRE_TESTHIDDEN;
1234 
1235 	/*
1236 	 * Atomically check for duplicate and insert in the table.
1237 	 */
1238 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
1239 		if (IRE_IS_CONDEMNED(ire1))
1240 			continue;
1241 		/*
1242 		 * Here we need an exact match on zoneid, i.e.,
1243 		 * ire_match_args doesn't fit.
1244 		 */
1245 		if (ire1->ire_zoneid != ire->ire_zoneid)
1246 			continue;
1247 
1248 		if (ire1->ire_type != ire->ire_type)
1249 			continue;
1250 
1251 		/*
1252 		 * Note: We do not allow multiple routes that differ only
1253 		 * in the gateway security attributes; such routes are
1254 		 * considered duplicates.
1255 		 * To change that we explicitly have to treat them as
1256 		 * different here.
1257 		 */
1258 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
1259 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ill,
1260 		    ire->ire_zoneid, NULL, match_flags)) {
1261 			/*
1262 			 * Return the old ire after doing a REFHOLD.
1263 			 * As most of the callers continue to use the IRE
1264 			 * after adding, we return a held ire. This will
1265 			 * avoid a lookup in the caller again. If the callers
1266 			 * don't want to use it, they need to do a REFRELE.
1267 			 */
1268 			atomic_add_32(&ire1->ire_identical_ref, 1);
1269 			DTRACE_PROBE2(ire__add__exist, ire_t *, ire1,
1270 			    ire_t *, ire);
1271 			ire_refhold(ire1);
1272 			ire_atomic_end(irb_ptr, ire);
1273 			ire_delete(ire);
1274 			irb_refrele(irb_ptr);
1275 			return (ire1);
1276 		}
1277 	}
1278 
1279 	/*
1280 	 * Normally we do head insertion since most things do not care about
1281 	 * the order of the IREs in the bucket. Note that ip_cgtp_bcast_add
1282 	 * assumes we at least do head insertion so that its IRE_BROADCAST
1283 	 * arrive ahead of existing IRE_HOST for the same address.
1284 	 * However, due to shared-IP zones (and restrict_interzone_loopback)
1285 	 * we can have an IRE_LOCAL as well as IRE_IF_CLONE for the same
1286 	 * address. For that reason we do tail insertion for IRE_IF_CLONE.
1287 	 * Due to the IRE_BROADCAST on cgtp0, which must be last in the bucket,
1288 	 * we do tail insertion of IRE_BROADCASTs that do not have RTF_MULTIRT
1289 	 * set.
1290 	 */
1291 	irep = (ire_t **)irb_ptr;
1292 	if ((ire->ire_type & IRE_IF_CLONE) ||
1293 	    ((ire->ire_type & IRE_BROADCAST) &&
1294 	    !(ire->ire_flags & RTF_MULTIRT))) {
1295 		while ((ire1 = *irep) != NULL)
1296 			irep = &ire1->ire_next;
1297 	}
1298 	/* Insert at *irep */
1299 	ire1 = *irep;
1300 	if (ire1 != NULL)
1301 		ire1->ire_ptpn = &ire->ire_next;
1302 	ire->ire_next = ire1;
1303 	/* Link the new one in. */
1304 	ire->ire_ptpn = irep;
1305 
1306 	/*
1307 	 * ire_walk routines de-reference ire_next without holding
1308 	 * a lock. Before we point to the new ire, we want to make
1309 	 * sure the store that sets the ire_next of the new ire
1310 	 * reaches global visibility, so that ire_walk routines
1311 	 * don't see a truncated list of ires i.e if the ire_next
1312 	 * of the new ire gets set after we do "*irep = ire" due
1313 	 * to re-ordering, the ire_walk thread will see a NULL
1314 	 * once it accesses the ire_next of the new ire.
1315 	 * membar_producer() makes sure that the following store
1316 	 * happens *after* all of the above stores.
1317 	 */
1318 	membar_producer();
1319 	*irep = ire;
1320 	ire->ire_bucket = irb_ptr;
1321 	/*
1322 	 * We return a bumped up IRE above. Keep it symmetrical
1323 	 * so that the callers will always have to release. This
1324 	 * helps the callers of this function because they continue
1325 	 * to use the IRE after adding and hence they don't have to
1326 	 * lookup again after we return the IRE.
1327 	 *
1328 	 * NOTE : We don't have to use atomics as this is appearing
1329 	 * in the list for the first time and no one else can bump
1330 	 * up the reference count on this yet.
1331 	 */
1332 	ire_refhold_locked(ire);
1333 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
1334 
1335 	irb_ptr->irb_ire_cnt++;
1336 	if (irb_ptr->irb_marks & IRB_MARK_DYNAMIC)
1337 		irb_ptr->irb_nire++;
1338 
1339 	if (ire->ire_ill != NULL) {
1340 		ire->ire_ill->ill_ire_cnt++;
1341 		ASSERT(ire->ire_ill->ill_ire_cnt != 0);	/* Wraparound */
1342 	}
1343 
1344 	ire_atomic_end(irb_ptr, ire);
1345 
1346 	/* Make any caching of the IREs be notified or updated */
1347 	ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
1348 
1349 	if (ire->ire_ill != NULL)
1350 		ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
1351 	irb_refrele(irb_ptr);
1352 	return (ire);
1353 }
1354 
1355 /*
1356  * irb_refrele is the only caller of the function. ire_unlink calls to
1357  * do the final cleanup for this ire.
1358  */
1359 void
1360 ire_cleanup(ire_t *ire)
1361 {
1362 	ire_t *ire_next;
1363 	ip_stack_t *ipst = ire->ire_ipst;
1364 
1365 	ASSERT(ire != NULL);
1366 
1367 	while (ire != NULL) {
1368 		ire_next = ire->ire_next;
1369 		if (ire->ire_ipversion == IPV4_VERSION) {
1370 			ire_delete_v4(ire);
1371 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
1372 			    ire_stats_deleted);
1373 		} else {
1374 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
1375 			ire_delete_v6(ire);
1376 			BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
1377 			    ire_stats_deleted);
1378 		}
1379 		/*
1380 		 * Now it's really out of the list. Before doing the
1381 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
1382 		 * so.
1383 		 */
1384 		ire->ire_next = NULL;
1385 		ire_refrele_notr(ire);
1386 		ire = ire_next;
1387 	}
1388 }
1389 
1390 /*
1391  * irb_refrele is the only caller of the function. It calls to unlink
1392  * all the CONDEMNED ires from this bucket.
1393  */
1394 ire_t *
1395 ire_unlink(irb_t *irb)
1396 {
1397 	ire_t *ire;
1398 	ire_t *ire1;
1399 	ire_t **ptpn;
1400 	ire_t *ire_list = NULL;
1401 
1402 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
1403 	ASSERT(((irb->irb_marks & IRB_MARK_DYNAMIC) && irb->irb_refcnt == 1) ||
1404 	    (irb->irb_refcnt == 0));
1405 	ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
1406 	ASSERT(irb->irb_ire != NULL);
1407 
1408 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
1409 		ire1 = ire->ire_next;
1410 		if (IRE_IS_CONDEMNED(ire)) {
1411 			ptpn = ire->ire_ptpn;
1412 			ire1 = ire->ire_next;
1413 			if (ire1)
1414 				ire1->ire_ptpn = ptpn;
1415 			*ptpn = ire1;
1416 			ire->ire_ptpn = NULL;
1417 			ire->ire_next = NULL;
1418 
1419 			/*
1420 			 * We need to call ire_delete_v4 or ire_delete_v6 to
1421 			 * clean up dependents and the redirects pointing at
1422 			 * the default gateway. We need to drop the lock
1423 			 * as ire_flush_cache/ire_delete_host_redircts require
1424 			 * so. But we can't drop the lock, as ire_unlink needs
1425 			 * to atomically remove the ires from the list.
1426 			 * So, create a temporary list of CONDEMNED ires
1427 			 * for doing ire_delete_v4/ire_delete_v6 operations
1428 			 * later on.
1429 			 */
1430 			ire->ire_next = ire_list;
1431 			ire_list = ire;
1432 		}
1433 	}
1434 	irb->irb_marks &= ~IRB_MARK_CONDEMNED;
1435 	return (ire_list);
1436 }
1437 
1438 /*
1439  * Clean up the radix node for this ire. Must be called by irb_refrele
1440  * when there are no ire's left in the bucket. Returns TRUE if the bucket
1441  * is deleted and freed.
1442  */
1443 boolean_t
1444 irb_inactive(irb_t *irb)
1445 {
1446 	struct rt_entry *rt;
1447 	struct radix_node *rn;
1448 	ip_stack_t *ipst = irb->irb_ipst;
1449 
1450 	ASSERT(irb->irb_ipst != NULL);
1451 
1452 	rt = IRB2RT(irb);
1453 	rn = (struct radix_node *)rt;
1454 
1455 	/* first remove it from the radix tree. */
1456 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1457 	rw_enter(&irb->irb_lock, RW_WRITER);
1458 	if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
1459 		rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
1460 		    ipst->ips_ip_ftable);
1461 		DTRACE_PROBE1(irb__free, rt_t *,  rt);
1462 		ASSERT((void *)rn == (void *)rt);
1463 		Free(rt, rt_entry_cache);
1464 		/* irb_lock is freed */
1465 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1466 		return (B_TRUE);
1467 	}
1468 	rw_exit(&irb->irb_lock);
1469 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1470 	return (B_FALSE);
1471 }
1472 
1473 /*
1474  * Delete the specified IRE.
1475  * We assume that if ire_bucket is not set then ire_ill->ill_ire_cnt was
1476  * not incremented i.e., that the insertion in the bucket and the increment
1477  * of that counter is done atomically.
1478  */
1479 void
1480 ire_delete(ire_t *ire)
1481 {
1482 	ire_t	*ire1;
1483 	ire_t	**ptpn;
1484 	irb_t	*irb;
1485 	ip_stack_t	*ipst = ire->ire_ipst;
1486 
1487 	if ((irb = ire->ire_bucket) == NULL) {
1488 		/*
1489 		 * It was never inserted in the list. Should call REFRELE
1490 		 * to free this IRE.
1491 		 */
1492 		ire_make_condemned(ire);
1493 		ire_refrele_notr(ire);
1494 		return;
1495 	}
1496 
1497 	/*
1498 	 * Move the use counts from an IRE_IF_CLONE to its parent
1499 	 * IRE_INTERFACE.
1500 	 * We need to do this before acquiring irb_lock.
1501 	 */
1502 	if (ire->ire_type & IRE_IF_CLONE) {
1503 		ire_t *parent;
1504 
1505 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1506 		if ((parent = ire->ire_dep_parent) != NULL) {
1507 			parent->ire_ob_pkt_count += ire->ire_ob_pkt_count;
1508 			parent->ire_ib_pkt_count += ire->ire_ib_pkt_count;
1509 			ire->ire_ob_pkt_count = 0;
1510 			ire->ire_ib_pkt_count = 0;
1511 		}
1512 		rw_exit(&ipst->ips_ire_dep_lock);
1513 	}
1514 
1515 	rw_enter(&irb->irb_lock, RW_WRITER);
1516 	if (ire->ire_ptpn == NULL) {
1517 		/*
1518 		 * Some other thread has removed us from the list.
1519 		 * It should have done the REFRELE for us.
1520 		 */
1521 		rw_exit(&irb->irb_lock);
1522 		return;
1523 	}
1524 
1525 	if (!IRE_IS_CONDEMNED(ire)) {
1526 		/* Is this an IRE representing multiple duplicate entries? */
1527 		ASSERT(ire->ire_identical_ref >= 1);
1528 		if (atomic_add_32_nv(&ire->ire_identical_ref, -1) != 0) {
1529 			/* Removed one of the identical parties */
1530 			rw_exit(&irb->irb_lock);
1531 			return;
1532 		}
1533 
1534 		irb->irb_ire_cnt--;
1535 		ire_make_condemned(ire);
1536 	}
1537 
1538 	if (irb->irb_refcnt != 0) {
1539 		/*
1540 		 * The last thread to leave this bucket will
1541 		 * delete this ire.
1542 		 */
1543 		irb->irb_marks |= IRB_MARK_CONDEMNED;
1544 		rw_exit(&irb->irb_lock);
1545 		return;
1546 	}
1547 
1548 	/*
1549 	 * Normally to delete an ire, we walk the bucket. While we
1550 	 * walk the bucket, we normally bump up irb_refcnt and hence
1551 	 * we return from above where we mark CONDEMNED and the ire
1552 	 * gets deleted from ire_unlink. This case is where somebody
1553 	 * knows the ire e.g by doing a lookup, and wants to delete the
1554 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
1555 	 * the bucket.
1556 	 */
1557 	ptpn = ire->ire_ptpn;
1558 	ire1 = ire->ire_next;
1559 	if (ire1 != NULL)
1560 		ire1->ire_ptpn = ptpn;
1561 	ASSERT(ptpn != NULL);
1562 	*ptpn = ire1;
1563 	ire->ire_ptpn = NULL;
1564 	ire->ire_next = NULL;
1565 	if (ire->ire_ipversion == IPV6_VERSION) {
1566 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
1567 	} else {
1568 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
1569 	}
1570 	rw_exit(&irb->irb_lock);
1571 
1572 	/* Cleanup dependents and related stuff */
1573 	if (ire->ire_ipversion == IPV6_VERSION) {
1574 		ire_delete_v6(ire);
1575 	} else {
1576 		ire_delete_v4(ire);
1577 	}
1578 	/*
1579 	 * We removed it from the list. Decrement the
1580 	 * reference count.
1581 	 */
1582 	ire_refrele_notr(ire);
1583 }
1584 
1585 /*
1586  * Delete the specified IRE.
1587  * All calls should use ire_delete().
1588  * Sometimes called as writer though not required by this function.
1589  *
1590  * NOTE : This function is called only if the ire was added
1591  * in the list.
1592  */
1593 static void
1594 ire_delete_v4(ire_t *ire)
1595 {
1596 	ip_stack_t	*ipst = ire->ire_ipst;
1597 
1598 	ASSERT(ire->ire_refcnt >= 1);
1599 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1600 
1601 	ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
1602 	if (ire->ire_type == IRE_DEFAULT) {
1603 		/*
1604 		 * when a default gateway is going away
1605 		 * delete all the host redirects pointing at that
1606 		 * gateway.
1607 		 */
1608 		ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
1609 	}
1610 
1611 	/*
1612 	 * If we are deleting an IRE_INTERFACE then we make sure we also
1613 	 * delete any IRE_IF_CLONE that has been created from it.
1614 	 * Those are always in ire_dep_children.
1615 	 */
1616 	if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != NULL)
1617 		ire_dep_delete_if_clone(ire);
1618 
1619 	/* Remove from parent dependencies and child */
1620 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
1621 	if (ire->ire_dep_parent != NULL)
1622 		ire_dep_remove(ire);
1623 
1624 	while (ire->ire_dep_children != NULL)
1625 		ire_dep_remove(ire->ire_dep_children);
1626 	rw_exit(&ipst->ips_ire_dep_lock);
1627 }
1628 
1629 /*
1630  * ire_refrele is the only caller of the function. It calls
1631  * to free the ire when the reference count goes to zero.
1632  */
1633 void
1634 ire_inactive(ire_t *ire)
1635 {
1636 	ill_t	*ill;
1637 	irb_t 	*irb;
1638 	ip_stack_t	*ipst = ire->ire_ipst;
1639 
1640 	ASSERT(ire->ire_refcnt == 0);
1641 	ASSERT(ire->ire_ptpn == NULL);
1642 	ASSERT(ire->ire_next == NULL);
1643 
1644 	/* Count how many condemned ires for kmem_cache callback */
1645 	ASSERT(IRE_IS_CONDEMNED(ire));
1646 	atomic_add_32(&ipst->ips_num_ire_condemned, -1);
1647 
1648 	if (ire->ire_gw_secattr != NULL) {
1649 		ire_gw_secattr_free(ire->ire_gw_secattr);
1650 		ire->ire_gw_secattr = NULL;
1651 	}
1652 
1653 	/*
1654 	 * ire_nce_cache is cleared in ire_delete, and we make sure we don't
1655 	 * set it once the ire is marked condemned.
1656 	 */
1657 	ASSERT(ire->ire_nce_cache == NULL);
1658 
1659 	/*
1660 	 * Since any parent would have a refhold on us they would already
1661 	 * have been removed.
1662 	 */
1663 	ASSERT(ire->ire_dep_parent == NULL);
1664 	ASSERT(ire->ire_dep_sib_next == NULL);
1665 	ASSERT(ire->ire_dep_sib_ptpn == NULL);
1666 
1667 	/*
1668 	 * Since any children would have a refhold on us they should have
1669 	 * already been removed.
1670 	 */
1671 	ASSERT(ire->ire_dep_children == NULL);
1672 
1673 	/*
1674 	 * ill_ire_ref is increased when the IRE is inserted in the
1675 	 * bucket - not when the IRE is created.
1676 	 */
1677 	irb = ire->ire_bucket;
1678 	ill = ire->ire_ill;
1679 	if (irb != NULL && ill != NULL) {
1680 		mutex_enter(&ill->ill_lock);
1681 		ASSERT(ill->ill_ire_cnt != 0);
1682 		DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
1683 		    (char *), "ire", (void *), ire);
1684 		ill->ill_ire_cnt--;
1685 		if (ILL_DOWN_OK(ill)) {
1686 			/* Drops the ill lock */
1687 			ipif_ill_refrele_tail(ill);
1688 		} else {
1689 			mutex_exit(&ill->ill_lock);
1690 		}
1691 	}
1692 	ire->ire_ill = NULL;
1693 
1694 	/* This should be true for both V4 and V6 */
1695 	if (irb != NULL && (irb->irb_marks & IRB_MARK_DYNAMIC)) {
1696 		rw_enter(&irb->irb_lock, RW_WRITER);
1697 		irb->irb_nire--;
1698 		/*
1699 		 * Instead of examining the conditions for freeing
1700 		 * the radix node here, we do it by calling
1701 		 * irb_refrele which is a single point in the code
1702 		 * that embeds that logic. Bump up the refcnt to
1703 		 * be able to call irb_refrele
1704 		 */
1705 		irb_refhold_locked(irb);
1706 		rw_exit(&irb->irb_lock);
1707 		irb_refrele(irb);
1708 	}
1709 
1710 #ifdef DEBUG
1711 	ire_trace_cleanup(ire);
1712 #endif
1713 	mutex_destroy(&ire->ire_lock);
1714 	if (ire->ire_ipversion == IPV6_VERSION) {
1715 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
1716 	} else {
1717 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
1718 	}
1719 	kmem_cache_free(ire_cache, ire);
1720 }
1721 
1722 /*
1723  * ire_update_generation is the callback function provided by
1724  * ire_get_bucket() to update the generation number of any
1725  * matching shorter route when a new route is added.
1726  *
1727  * This fucntion always returns a failure return (B_FALSE)
1728  * to force the caller (rn_matchaddr_args)
1729  * to back-track up the tree looking for shorter matches.
1730  */
1731 /* ARGSUSED */
1732 static boolean_t
1733 ire_update_generation(struct radix_node *rn, void *arg)
1734 {
1735 	struct rt_entry *rt = (struct rt_entry *)rn;
1736 
1737 	/* We need to handle all in the same bucket */
1738 	irb_increment_generation(&rt->rt_irb);
1739 	return (B_FALSE);
1740 }
1741 
1742 /*
1743  * Take care of all the generation numbers in the bucket.
1744  */
1745 void
1746 irb_increment_generation(irb_t *irb)
1747 {
1748 	ire_t *ire;
1749 	ip_stack_t *ipst;
1750 
1751 	if (irb == NULL || irb->irb_ire_cnt == 0)
1752 		return;
1753 
1754 	ipst = irb->irb_ipst;
1755 	/*
1756 	 * we cannot do an irb_refhold/irb_refrele here as the caller
1757 	 * already has the global RADIX_NODE_HEAD_WLOCK, and the irb_refrele
1758 	 * may result in an attempt to free the irb_t, which also needs
1759 	 * the RADIX_NODE_HEAD lock. However, since we want to traverse the
1760 	 * irb_ire list without fear of having a condemned ire removed from
1761 	 * the list, we acquire the irb_lock as WRITER. Moreover, since
1762 	 * the ire_generation increments are done under the ire_dep_lock,
1763 	 * acquire the locks in the prescribed lock order first.
1764 	 */
1765 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1766 	rw_enter(&irb->irb_lock, RW_WRITER);
1767 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
1768 		if (!IRE_IS_CONDEMNED(ire))
1769 			ire_increment_generation(ire);	/* Ourselves */
1770 		ire_dep_incr_generation_locked(ire);	/* Dependants */
1771 	}
1772 	rw_exit(&irb->irb_lock);
1773 	rw_exit(&ipst->ips_ire_dep_lock);
1774 }
1775 
1776 /*
1777  * When an IRE is added or deleted this routine is called to make sure
1778  * any caching of IRE information is notified or updated.
1779  *
1780  * The flag argument indicates if the flush request is due to addition
1781  * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE),
1782  * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE).
1783  */
1784 void
1785 ire_flush_cache_v4(ire_t *ire, int flag)
1786 {
1787 	irb_t *irb = ire->ire_bucket;
1788 	struct rt_entry *rt = IRB2RT(irb);
1789 	ip_stack_t *ipst = ire->ire_ipst;
1790 
1791 	/*
1792 	 * IRE_IF_CLONE ire's don't provide any new information
1793 	 * than the parent from which they are cloned, so don't
1794 	 * perturb the generation numbers.
1795 	 */
1796 	if (ire->ire_type & IRE_IF_CLONE)
1797 		return;
1798 
1799 	/*
1800 	 * Ensure that an ire_add during a lookup serializes the updates of the
1801 	 * generation numbers under the radix head lock so that the lookup gets
1802 	 * either the old ire and old generation number, or a new ire and new
1803 	 * generation number.
1804 	 */
1805 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1806 
1807 	/*
1808 	 * If a route was just added, we need to notify everybody that
1809 	 * has cached an IRE_NOROUTE since there might now be a better
1810 	 * route for them.
1811 	 */
1812 	if (flag == IRE_FLUSH_ADD) {
1813 		ire_increment_generation(ipst->ips_ire_reject_v4);
1814 		ire_increment_generation(ipst->ips_ire_blackhole_v4);
1815 	}
1816 
1817 	/* Adding a default can't otherwise provide a better route */
1818 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) {
1819 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1820 		return;
1821 	}
1822 
1823 	switch (flag) {
1824 	case IRE_FLUSH_DELETE:
1825 	case IRE_FLUSH_GWCHANGE:
1826 		/*
1827 		 * Update ire_generation for all ire_dep_children chains
1828 		 * starting with this IRE
1829 		 */
1830 		ire_dep_incr_generation(ire);
1831 		break;
1832 	case IRE_FLUSH_ADD:
1833 		/*
1834 		 * Update the generation numbers of all shorter matching routes.
1835 		 * ire_update_generation takes care of the dependants by
1836 		 * using ire_dep_incr_generation.
1837 		 */
1838 		(void) ipst->ips_ip_ftable->rnh_matchaddr_args(&rt->rt_dst,
1839 		    ipst->ips_ip_ftable, ire_update_generation, NULL);
1840 		break;
1841 	}
1842 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1843 }
1844 
1845 /*
1846  * Matches the arguments passed with the values in the ire.
1847  *
1848  * Note: for match types that match using "ill" passed in, ill
1849  * must be checked for non-NULL before calling this routine.
1850  */
1851 boolean_t
1852 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
1853     int type, const ill_t *ill, zoneid_t zoneid,
1854     const ts_label_t *tsl, int match_flags)
1855 {
1856 	ill_t *ire_ill = NULL, *dst_ill;
1857 	ip_stack_t *ipst = ire->ire_ipst;
1858 
1859 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1860 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
1861 	ASSERT((!(match_flags & MATCH_IRE_ILL)) ||
1862 	    (ill != NULL && !ill->ill_isv6));
1863 
1864 	/*
1865 	 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it is
1866 	 * in fact hidden, to ensure the caller gets the right one.
1867 	 */
1868 	if (ire->ire_testhidden) {
1869 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))
1870 			return (B_FALSE);
1871 	}
1872 
1873 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
1874 	    ire->ire_zoneid != ALL_ZONES) {
1875 		/*
1876 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid
1877 		 * does not match that of ire_zoneid, a failure to
1878 		 * match is reported at this point. Otherwise, since some IREs
1879 		 * that are available in the global zone can be used in local
1880 		 * zones, additional checks need to be performed:
1881 		 *
1882 		 * IRE_LOOPBACK
1883 		 *	entries should never be matched in this situation.
1884 		 *	Each zone has its own IRE_LOOPBACK.
1885 		 *
1886 		 * IRE_LOCAL
1887 		 *	We allow them for any zoneid. ire_route_recursive
1888 		 *	does additional checks when
1889 		 *	ip_restrict_interzone_loopback is set.
1890 		 *
1891 		 * If ill_usesrc_ifindex is set
1892 		 *	Then we check if the zone has a valid source address
1893 		 *	on the usesrc ill.
1894 		 *
1895 		 * If ire_ill is set, then check that the zone has an ipif
1896 		 *	on that ill.
1897 		 *
1898 		 * Outside of this function (in ire_round_robin) we check
1899 		 * that any IRE_OFFLINK has a gateway that reachable from the
1900 		 * zone when we have multiple choices (ECMP).
1901 		 */
1902 		if (match_flags & MATCH_IRE_ZONEONLY)
1903 			return (B_FALSE);
1904 		if (ire->ire_type & IRE_LOOPBACK)
1905 			return (B_FALSE);
1906 
1907 		if (ire->ire_type & IRE_LOCAL)
1908 			goto matchit;
1909 
1910 		/*
1911 		 * The normal case of IRE_ONLINK has a matching zoneid.
1912 		 * Here we handle the case when shared-IP zones have been
1913 		 * configured with IP addresses on vniN. In that case it
1914 		 * is ok for traffic from a zone to use IRE_ONLINK routes
1915 		 * if the ill has a usesrc pointing at vniN
1916 		 */
1917 		dst_ill = ire->ire_ill;
1918 		if (ire->ire_type & IRE_ONLINK) {
1919 			uint_t	ifindex;
1920 
1921 			/*
1922 			 * Note there is no IRE_INTERFACE on vniN thus
1923 			 * can't do an IRE lookup for a matching route.
1924 			 */
1925 			ifindex = dst_ill->ill_usesrc_ifindex;
1926 			if (ifindex == 0)
1927 				return (B_FALSE);
1928 
1929 			/*
1930 			 * If there is a usable source address in the
1931 			 * zone, then it's ok to return this IRE_INTERFACE
1932 			 */
1933 			if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
1934 			    zoneid, ipst)) {
1935 				ip3dbg(("ire_match_args: no usrsrc for zone"
1936 				    " dst_ill %p\n", (void *)dst_ill));
1937 				return (B_FALSE);
1938 			}
1939 		}
1940 		/*
1941 		 * For exampe, with
1942 		 * route add 11.0.0.0 gw1 -ifp bge0
1943 		 * route add 11.0.0.0 gw2 -ifp bge1
1944 		 * this code would differentiate based on
1945 		 * where the sending zone has addresses.
1946 		 * Only if the zone has an address on bge0 can it use the first
1947 		 * route. It isn't clear if this behavior is documented
1948 		 * anywhere.
1949 		 */
1950 		if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
1951 			ipif_t	*tipif;
1952 
1953 			mutex_enter(&dst_ill->ill_lock);
1954 			for (tipif = dst_ill->ill_ipif;
1955 			    tipif != NULL; tipif = tipif->ipif_next) {
1956 				if (!IPIF_IS_CONDEMNED(tipif) &&
1957 				    (tipif->ipif_flags & IPIF_UP) &&
1958 				    (tipif->ipif_zoneid == zoneid ||
1959 				    tipif->ipif_zoneid == ALL_ZONES))
1960 					break;
1961 			}
1962 			mutex_exit(&dst_ill->ill_lock);
1963 			if (tipif == NULL) {
1964 				return (B_FALSE);
1965 			}
1966 		}
1967 	}
1968 
1969 matchit:
1970 	if (match_flags & MATCH_IRE_ILL) {
1971 		ire_ill = ire->ire_ill;
1972 
1973 		/*
1974 		 * If asked to match an ill, we *must* match
1975 		 * on the ire_ill for ipmp test addresses, or
1976 		 * any of the ill in the group for data addresses.
1977 		 * If we don't, we may as well fail.
1978 		 * However, we need an exception for IRE_LOCALs to ensure
1979 		 * we loopback packets even sent to test addresses on different
1980 		 * interfaces in the group.
1981 		 */
1982 		if ((match_flags & MATCH_IRE_TESTHIDDEN) &&
1983 		    !(ire->ire_type & IRE_LOCAL)) {
1984 			if (ire->ire_ill != ill)
1985 				return (B_FALSE);
1986 		} else  {
1987 			match_flags &= ~MATCH_IRE_TESTHIDDEN;
1988 			/*
1989 			 * We know that ill is not NULL, but ire_ill could be
1990 			 * NULL
1991 			 */
1992 			if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill))
1993 				return (B_FALSE);
1994 		}
1995 	}
1996 
1997 	if ((ire->ire_addr == (addr & mask)) &&
1998 	    ((!(match_flags & MATCH_IRE_GW)) ||
1999 	    (ire->ire_gateway_addr == gateway)) &&
2000 	    ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) &&
2001 	    ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
2002 	    ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask)) &&
2003 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
2004 	    (!is_system_labeled()) ||
2005 	    (tsol_ire_match_gwattr(ire, tsl) == 0))) {
2006 		/* We found the matched IRE */
2007 		return (B_TRUE);
2008 	}
2009 	return (B_FALSE);
2010 }
2011 
2012 /*
2013  * Check if the IRE_LOCAL uses the same ill as another route would use.
2014  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
2015  * then we don't allow this IRE_LOCAL to be used.
2016  * We always return an IRE; will be RTF_REJECT if no route available.
2017  */
2018 ire_t *
2019 ire_alt_local(ire_t *ire, zoneid_t zoneid, const ts_label_t *tsl,
2020     const ill_t *ill, uint_t *generationp)
2021 {
2022 	ip_stack_t	*ipst = ire->ire_ipst;
2023 	ire_t		*alt_ire;
2024 	uint_t		ire_type;
2025 	uint_t		generation;
2026 	uint_t		match_flags;
2027 
2028 	ASSERT(ire->ire_type & IRE_LOCAL);
2029 	ASSERT(ire->ire_ill != NULL);
2030 
2031 	/*
2032 	 * Need to match on everything but local.
2033 	 * This might result in the creation of a IRE_IF_CLONE for the
2034 	 * same address as the IRE_LOCAL when restrict_interzone_loopback is
2035 	 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted
2036 	 * to make sure the IRE_LOCAL is always found first.
2037 	 */
2038 	ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK);
2039 	match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
2040 	if (ill != NULL)
2041 		match_flags |= MATCH_IRE_ILL;
2042 
2043 	if (ire->ire_ipversion == IPV4_VERSION) {
2044 		alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type,
2045 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2046 		    NULL, &generation);
2047 	} else {
2048 		alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type,
2049 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2050 		    NULL, &generation);
2051 	}
2052 	ASSERT(alt_ire != NULL);
2053 
2054 	if (alt_ire->ire_ill == ire->ire_ill) {
2055 		/* Going out the same ILL - ok to send to IRE_LOCAL */
2056 		ire_refrele(alt_ire);
2057 	} else {
2058 		/* Different ill - ignore IRE_LOCAL */
2059 		ire_refrele(ire);
2060 		ire = alt_ire;
2061 		if (generationp != NULL)
2062 			*generationp = generation;
2063 	}
2064 	return (ire);
2065 }
2066 
2067 boolean_t
2068 ire_find_zoneid(struct radix_node *rn, void *arg)
2069 {
2070 	struct rt_entry *rt = (struct rt_entry *)rn;
2071 	irb_t *irb;
2072 	ire_t *ire;
2073 	ire_ftable_args_t *margs = arg;
2074 
2075 	ASSERT(rt != NULL);
2076 
2077 	irb = &rt->rt_irb;
2078 
2079 	if (irb->irb_ire_cnt == 0)
2080 		return (B_FALSE);
2081 
2082 	rw_enter(&irb->irb_lock, RW_READER);
2083 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2084 		if (IRE_IS_CONDEMNED(ire))
2085 			continue;
2086 
2087 		if (!(ire->ire_type & IRE_INTERFACE))
2088 			continue;
2089 
2090 		if (ire->ire_zoneid != ALL_ZONES &&
2091 		    ire->ire_zoneid != margs->ift_zoneid)
2092 			continue;
2093 
2094 		if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill)
2095 			continue;
2096 
2097 		if (is_system_labeled() &&
2098 		    tsol_ire_match_gwattr(ire, margs->ift_tsl) != 0)
2099 			continue;
2100 
2101 		rw_exit(&irb->irb_lock);
2102 		return (B_TRUE);
2103 	}
2104 	rw_exit(&irb->irb_lock);
2105 	return (B_FALSE);
2106 }
2107 
2108 /*
2109  * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
2110  * gateway address. If ill is non-NULL we also match on it.
2111  * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
2112  */
2113 boolean_t
2114 ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill,
2115     const ts_label_t *tsl, ip_stack_t *ipst, boolean_t lock_held)
2116 {
2117 	struct rt_sockaddr rdst;
2118 	struct rt_entry *rt;
2119 	ire_ftable_args_t margs;
2120 
2121 	ASSERT(ill == NULL || !ill->ill_isv6);
2122 	if (lock_held)
2123 		ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock));
2124 	else
2125 		RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
2126 
2127 	bzero(&rdst, sizeof (rdst));
2128 	rdst.rt_sin_len = sizeof (rdst);
2129 	rdst.rt_sin_family = AF_INET;
2130 	rdst.rt_sin_addr.s_addr = gateway;
2131 
2132 	/*
2133 	 * We only use margs for ill, zoneid, and tsl matching in
2134 	 * ire_find_zoneid
2135 	 */
2136 	bzero(&margs, sizeof (margs));
2137 	margs.ift_ill = ill;
2138 	margs.ift_zoneid = zoneid;
2139 	margs.ift_tsl = tsl;
2140 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
2141 	    ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs);
2142 
2143 	if (!lock_held)
2144 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
2145 
2146 	return (rt != NULL);
2147 }
2148 
2149 /*
2150  * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs.
2151  * The fraction argument tells us what fraction of the IREs to delete.
2152  * Common for IPv4 and IPv6.
2153  * Used when memory backpressure.
2154  */
2155 static void
2156 ire_delete_reclaim(ire_t *ire, char *arg)
2157 {
2158 	ip_stack_t	*ipst = ire->ire_ipst;
2159 	uint_t		fraction = *(uint_t *)arg;
2160 	uint_t		rand;
2161 
2162 	if ((ire->ire_flags & RTF_DYNAMIC) ||
2163 	    (ire->ire_type & IRE_IF_CLONE)) {
2164 
2165 		/* Pick a random number */
2166 		rand = (uint_t)ddi_get_lbolt() +
2167 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256);
2168 
2169 		/* Use truncation */
2170 		if ((rand/fraction)*fraction == rand) {
2171 			IP_STAT(ipst, ip_ire_reclaim_deleted);
2172 			ire_delete(ire);
2173 		}
2174 	}
2175 
2176 }
2177 
2178 /*
2179  * kmem_cache callback to free up memory.
2180  *
2181  * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically
2182  * (RTF_DYNAMIC and IRE_IF_CLONE).
2183  */
2184 static void
2185 ip_ire_reclaim_stack(ip_stack_t *ipst)
2186 {
2187 	uint_t	fraction = ipst->ips_ip_ire_reclaim_fraction;
2188 
2189 	IP_STAT(ipst, ip_ire_reclaim_calls);
2190 
2191 	ire_walk(ire_delete_reclaim, &fraction, ipst);
2192 
2193 	/*
2194 	 * Walk all CONNs that can have a reference on an ire, nce or dce.
2195 	 * Get them to update any stale references to drop any refholds they
2196 	 * have.
2197 	 */
2198 	ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst);
2199 }
2200 
2201 /*
2202  * Called by the memory allocator subsystem directly, when the system
2203  * is running low on memory.
2204  */
2205 /* ARGSUSED */
2206 void
2207 ip_ire_reclaim(void *args)
2208 {
2209 	netstack_handle_t nh;
2210 	netstack_t *ns;
2211 
2212 	netstack_next_init(&nh);
2213 	while ((ns = netstack_next(&nh)) != NULL) {
2214 		ip_ire_reclaim_stack(ns->netstack_ip);
2215 		netstack_rele(ns);
2216 	}
2217 	netstack_next_fini(&nh);
2218 }
2219 
2220 static void
2221 power2_roundup(uint32_t *value)
2222 {
2223 	int i;
2224 
2225 	for (i = 1; i < 31; i++) {
2226 		if (*value <= (1 << i))
2227 			break;
2228 	}
2229 	*value = (1 << i);
2230 }
2231 
2232 /* Global init for all zones */
2233 void
2234 ip_ire_g_init()
2235 {
2236 	/*
2237 	 * Create kmem_caches.  ip_ire_reclaim() and ip_nce_reclaim()
2238 	 * will give disposable IREs back to system when needed.
2239 	 * This needs to be done here before anything else, since
2240 	 * ire_add() expects the cache to be created.
2241 	 */
2242 	ire_cache = kmem_cache_create("ire_cache",
2243 	    sizeof (ire_t), 0, NULL, NULL,
2244 	    ip_ire_reclaim, NULL, NULL, 0);
2245 
2246 	ncec_cache = kmem_cache_create("ncec_cache",
2247 	    sizeof (ncec_t), 0, NULL, NULL,
2248 	    ip_nce_reclaim, NULL, NULL, 0);
2249 	nce_cache = kmem_cache_create("nce_cache",
2250 	    sizeof (nce_t), 0, NULL, NULL,
2251 	    NULL, NULL, NULL, 0);
2252 
2253 	rt_entry_cache = kmem_cache_create("rt_entry",
2254 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
2255 
2256 	/*
2257 	 * Have radix code setup kmem caches etc.
2258 	 */
2259 	rn_init();
2260 }
2261 
2262 void
2263 ip_ire_init(ip_stack_t *ipst)
2264 {
2265 	ire_t	*ire;
2266 	int	error;
2267 
2268 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
2269 
2270 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
2271 
2272 	/*
2273 	 * Make sure that the forwarding table size is a power of 2.
2274 	 * The IRE*_ADDR_HASH() macroes depend on that.
2275 	 */
2276 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
2277 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
2278 
2279 	/*
2280 	 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6.
2281 	 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has
2282 	 * RTF_BLACKHOLE set. We use the latter for transient errors such
2283 	 * as memory allocation failures and tripping on IRE_IS_CONDEMNED
2284 	 * entries.
2285 	 */
2286 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2287 	*ire = ire_null;
2288 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2289 	    RTF_REJECT|RTF_UP, NULL, ipst);
2290 	ASSERT(error == 0);
2291 	ipst->ips_ire_reject_v4 = ire;
2292 
2293 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2294 	*ire = ire_null;
2295 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2296 	    RTF_REJECT|RTF_UP, NULL, ipst);
2297 	ASSERT(error == 0);
2298 	ipst->ips_ire_reject_v6 = ire;
2299 
2300 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2301 	*ire = ire_null;
2302 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2303 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
2304 	ASSERT(error == 0);
2305 	ipst->ips_ire_blackhole_v4 = ire;
2306 
2307 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2308 	*ire = ire_null;
2309 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2310 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
2311 	ASSERT(error == 0);
2312 	ipst->ips_ire_blackhole_v6 = ire;
2313 
2314 	rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL);
2315 	rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL);
2316 }
2317 
2318 void
2319 ip_ire_g_fini(void)
2320 {
2321 	kmem_cache_destroy(ire_cache);
2322 	kmem_cache_destroy(ncec_cache);
2323 	kmem_cache_destroy(nce_cache);
2324 	kmem_cache_destroy(rt_entry_cache);
2325 
2326 	rn_fini();
2327 }
2328 
2329 void
2330 ip_ire_fini(ip_stack_t *ipst)
2331 {
2332 	int i;
2333 
2334 	rw_destroy(&ipst->ips_ire_dep_lock);
2335 	rw_destroy(&ipst->ips_ip6_ire_head_lock);
2336 
2337 	ire_refrele_notr(ipst->ips_ire_reject_v6);
2338 	ipst->ips_ire_reject_v6 = NULL;
2339 	ire_refrele_notr(ipst->ips_ire_reject_v4);
2340 	ipst->ips_ire_reject_v4 = NULL;
2341 	ire_refrele_notr(ipst->ips_ire_blackhole_v6);
2342 	ipst->ips_ire_blackhole_v6 = NULL;
2343 	ire_refrele_notr(ipst->ips_ire_blackhole_v4);
2344 	ipst->ips_ire_blackhole_v4 = NULL;
2345 
2346 	/*
2347 	 * Delete all IREs - assumes that the ill/ipifs have
2348 	 * been removed so what remains are just the ftable to handle.
2349 	 */
2350 	ire_walk(ire_delete, NULL, ipst);
2351 
2352 	rn_freehead(ipst->ips_ip_ftable);
2353 	ipst->ips_ip_ftable = NULL;
2354 
2355 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
2356 
2357 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
2358 		irb_t *ptr;
2359 		int j;
2360 
2361 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
2362 			continue;
2363 
2364 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
2365 			ASSERT(ptr[j].irb_ire == NULL);
2366 			rw_destroy(&ptr[j].irb_lock);
2367 		}
2368 		mi_free(ptr);
2369 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
2370 	}
2371 }
2372 
2373 #ifdef DEBUG
2374 void
2375 ire_trace_ref(ire_t *ire)
2376 {
2377 	mutex_enter(&ire->ire_lock);
2378 	if (ire->ire_trace_disable) {
2379 		mutex_exit(&ire->ire_lock);
2380 		return;
2381 	}
2382 
2383 	if (th_trace_ref(ire, ire->ire_ipst)) {
2384 		mutex_exit(&ire->ire_lock);
2385 	} else {
2386 		ire->ire_trace_disable = B_TRUE;
2387 		mutex_exit(&ire->ire_lock);
2388 		ire_trace_cleanup(ire);
2389 	}
2390 }
2391 
2392 void
2393 ire_untrace_ref(ire_t *ire)
2394 {
2395 	mutex_enter(&ire->ire_lock);
2396 	if (!ire->ire_trace_disable)
2397 		th_trace_unref(ire);
2398 	mutex_exit(&ire->ire_lock);
2399 }
2400 
2401 static void
2402 ire_trace_cleanup(const ire_t *ire)
2403 {
2404 	th_trace_cleanup(ire, ire->ire_trace_disable);
2405 }
2406 #endif /* DEBUG */
2407 
2408 /*
2409  * Find, or create if needed, the nce_t pointer to the neighbor cache
2410  * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
2411  * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
2412  * on the next available under-ill (selected by the IPMP rotor) in the
2413  * unicast IPMP case.
2414  *
2415  * If a neighbor-cache entry has to be created (i.e., one does not already
2416  * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
2417  * entry are initialized in nce_add_v4(). The broadcast, multicast, and
2418  * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
2419  * the ncec_t created. The ncec_lladdr is non-null for all link types with
2420  * non-zero ill_phys_addr_length, though the contents may be zero in cases
2421  * where the link-layer type is not known at the time of creation
2422  * (e.g., IRE_IFRESOLVER links)
2423  *
2424  * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
2425  * has the physical broadcast address of the outgoing interface.
2426  * For unicast ire entries,
2427  *   - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
2428  *     ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
2429  *   - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
2430  *     layer resolution is necessary, so that the ncec_t will be in the
2431  *     ND_REACHABLE state
2432  *
2433  * The link layer information needed for broadcast addresses, and for
2434  * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
2435  * never needs re-verification for the lifetime of the ncec_t. These are
2436  * therefore marked NCE_F_NONUD.
2437  *
2438  * The nce returned will be created such that the nce_ill == ill that
2439  * is passed in. Note that the nce itself may not have ncec_ill == ill
2440  * where IPMP links are involved.
2441  */
2442 static nce_t *
2443 ire_nce_init(ill_t *ill, const void *addr, int ire_type)
2444 {
2445 	int		err;
2446 	nce_t		*nce = NULL;
2447 	uint16_t	ncec_flags;
2448 	uchar_t		*hwaddr;
2449 	boolean_t	need_refrele = B_FALSE;
2450 	ill_t		*in_ill = ill;
2451 	boolean_t	is_unicast;
2452 	uint_t		hwaddr_len;
2453 
2454 	is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
2455 	if (IS_IPMP(ill) ||
2456 	    ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
2457 		if ((ill = ipmp_ill_get_xmit_ill(ill, is_unicast)) == NULL)
2458 			return (NULL);
2459 		need_refrele = B_TRUE;
2460 	}
2461 	ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
2462 
2463 	switch (ire_type) {
2464 	case IRE_BROADCAST:
2465 		ASSERT(!ill->ill_isv6);
2466 		ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
2467 		break;
2468 	case IRE_MULTICAST:
2469 		ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
2470 		break;
2471 	}
2472 
2473 	if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
2474 		hwaddr = ill->ill_dest_addr;
2475 	} else {
2476 		hwaddr = NULL;
2477 	}
2478 	hwaddr_len = ill->ill_phys_addr_length;
2479 
2480 retry:
2481 	/* nce_state will be computed by nce_add_common() */
2482 	if (!ill->ill_isv6) {
2483 		err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
2484 		    ncec_flags, ND_UNCHANGED, &nce);
2485 	} else {
2486 		err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
2487 		    ncec_flags, ND_UNCHANGED, &nce);
2488 	}
2489 
2490 	switch (err) {
2491 	case 0:
2492 		break;
2493 	case EEXIST:
2494 		/*
2495 		 * When subnets change or partially overlap what was once
2496 		 * a broadcast address could now be a unicast, or vice versa.
2497 		 */
2498 		if (((ncec_flags ^ nce->nce_common->ncec_flags) &
2499 		    NCE_F_BCAST) != 0) {
2500 			ASSERT(!ill->ill_isv6);
2501 			ncec_delete(nce->nce_common);
2502 			nce_refrele(nce);
2503 			goto retry;
2504 		}
2505 		break;
2506 	default:
2507 		DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
2508 		if (need_refrele)
2509 			ill_refrele(ill);
2510 		return (NULL);
2511 	}
2512 	/*
2513 	 * If the ill was an under-ill of an IPMP group, we need to verify
2514 	 * that it is still active so that we select an active interface in
2515 	 * the group. However, since ipmp_ill_is_active ASSERTs for
2516 	 * IS_UNDER_IPMP(), we first need to verify that the ill is an
2517 	 * under-ill, and since this is being done in the data path, the
2518 	 * only way to ascertain this is by holding the ill_g_lock.
2519 	 */
2520 	rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
2521 	mutex_enter(&ill->ill_lock);
2522 	mutex_enter(&ill->ill_phyint->phyint_lock);
2523 	if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
2524 		/*
2525 		 * need_refrele implies that the under ill was selected by
2526 		 * ipmp_ill_get_xmit_ill() because either the in_ill was an
2527 		 * ipmp_ill, or we are sending a non-unicast packet on
2528 		 * an under_ill. However, when we get here, the ill selected by
2529 		 * ipmp_ill_get_xmit_ill  was pulled out of the active set
2530 		 * (for unicast)  or cast_ill nomination (for
2531 		 * !unicast) after it was  picked as the outgoing ill.
2532 		 * We have to pick an active interface and/or cast_ill in the
2533 		 * group.
2534 		 */
2535 		mutex_exit(&ill->ill_phyint->phyint_lock);
2536 		nce_delete(nce);
2537 		mutex_exit(&ill->ill_lock);
2538 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2539 		nce_refrele(nce);
2540 		ill_refrele(ill);
2541 		if ((ill = ipmp_ill_get_xmit_ill(in_ill, is_unicast)) == NULL)
2542 			return (NULL);
2543 		goto retry;
2544 	} else {
2545 		mutex_exit(&ill->ill_phyint->phyint_lock);
2546 		mutex_exit(&ill->ill_lock);
2547 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2548 	}
2549 done:
2550 	ASSERT(nce->nce_ill == ill);
2551 	if (need_refrele)
2552 		ill_refrele(ill);
2553 	return (nce);
2554 }
2555 
2556 nce_t *
2557 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
2558 {
2559 	return (ire_nce_init(ill, &addr4, ire_type));
2560 }
2561 
2562 nce_t *
2563 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
2564 {
2565 	ASSERT((ire_type & IRE_BROADCAST) == 0);
2566 	return (ire_nce_init(ill, addr6, ire_type));
2567 }
2568 
2569 /*
2570  * The caller should hold irb_lock as a writer if the ire is in a bucket.
2571  * This routine will clear ire_nce_cache, and we make sure that we can never
2572  * set ire_nce_cache after the ire is marked condemned.
2573  */
2574 void
2575 ire_make_condemned(ire_t *ire)
2576 {
2577 	ip_stack_t	*ipst = ire->ire_ipst;
2578 	nce_t		*nce;
2579 
2580 	mutex_enter(&ire->ire_lock);
2581 	ASSERT(ire->ire_bucket == NULL ||
2582 	    RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
2583 	ASSERT(!IRE_IS_CONDEMNED(ire));
2584 	ire->ire_generation = IRE_GENERATION_CONDEMNED;
2585 	/* Count how many condemned ires for kmem_cache callback */
2586 	atomic_add_32(&ipst->ips_num_ire_condemned, 1);
2587 	nce = ire->ire_nce_cache;
2588 	ire->ire_nce_cache = NULL;
2589 	mutex_exit(&ire->ire_lock);
2590 	if (nce != NULL)
2591 		nce_refrele(nce);
2592 }
2593 
2594 /*
2595  * Increment the generation avoiding the special condemned value
2596  */
2597 void
2598 ire_increment_generation(ire_t *ire)
2599 {
2600 	uint_t generation;
2601 
2602 	mutex_enter(&ire->ire_lock);
2603 	/*
2604 	 * Even though the caller has a hold it can't prevent a concurrent
2605 	 * ire_delete marking the IRE condemned
2606 	 */
2607 	if (!IRE_IS_CONDEMNED(ire)) {
2608 		generation = ire->ire_generation + 1;
2609 		if (generation == IRE_GENERATION_CONDEMNED)
2610 			generation = IRE_GENERATION_INITIAL;
2611 		ASSERT(generation != IRE_GENERATION_VERIFY);
2612 		ire->ire_generation = generation;
2613 	}
2614 	mutex_exit(&ire->ire_lock);
2615 }
2616 
2617 /*
2618  * Increment ire_generation on all the IRE_MULTICASTs
2619  * Used when the default multicast interface (as determined by
2620  * ill_lookup_multicast) might have changed.
2621  *
2622  * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
2623  * ill unplumb.
2624  */
2625 void
2626 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
2627 {
2628 	ill_t	*ill;
2629 	ill_walk_context_t ctx;
2630 
2631 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2632 	if (isv6)
2633 		ill = ILL_START_WALK_V6(&ctx, ipst);
2634 	else
2635 		ill = ILL_START_WALK_V4(&ctx, ipst);
2636 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2637 		if (ILL_IS_CONDEMNED(ill))
2638 			continue;
2639 		if (ill->ill_ire_multicast != NULL)
2640 			ire_increment_generation(ill->ill_ire_multicast);
2641 	}
2642 	rw_exit(&ipst->ips_ill_g_lock);
2643 }
2644 
2645 /*
2646  * Return a held IRE_NOROUTE with RTF_REJECT set
2647  */
2648 ire_t *
2649 ire_reject(ip_stack_t *ipst, boolean_t isv6)
2650 {
2651 	ire_t *ire;
2652 
2653 	if (isv6)
2654 		ire = ipst->ips_ire_reject_v6;
2655 	else
2656 		ire = ipst->ips_ire_reject_v4;
2657 
2658 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2659 	ire_refhold(ire);
2660 	return (ire);
2661 }
2662 
2663 /*
2664  * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
2665  */
2666 ire_t *
2667 ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
2668 {
2669 	ire_t *ire;
2670 
2671 	if (isv6)
2672 		ire = ipst->ips_ire_blackhole_v6;
2673 	else
2674 		ire = ipst->ips_ire_blackhole_v4;
2675 
2676 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2677 	ire_refhold(ire);
2678 	return (ire);
2679 }
2680 
2681 /*
2682  * Return a held IRE_MULTICAST.
2683  */
2684 ire_t *
2685 ire_multicast(ill_t *ill)
2686 {
2687 	ire_t *ire = ill->ill_ire_multicast;
2688 
2689 	ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
2690 	if (ire == NULL)
2691 		ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
2692 	else
2693 		ire_refhold(ire);
2694 	return (ire);
2695 }
2696 
2697 /*
2698  * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
2699  * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
2700  * This can return an RTF_REJECT|RTF_BLACKHOLE.
2701  * The returned IRE is held.
2702  * The assumption is that ip_select_route() has been called and returned the
2703  * IRE (thus ip_select_route would have set up the ire_dep* information.)
2704  * If some IRE is deleteted then ire_dep_remove() will have been called and
2705  * we might not find a nexthop IRE, in which case we return NULL.
2706  */
2707 ire_t *
2708 ire_nexthop(ire_t *ire)
2709 {
2710 	ip_stack_t	*ipst = ire->ire_ipst;
2711 
2712 	/* Acquire lock to walk ire_dep_parent */
2713 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
2714 	while (ire != NULL) {
2715 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2716 			goto done;
2717 		}
2718 		/*
2719 		 * If we find an IRE_ONLINK we are done. This includes
2720 		 * the case of IRE_MULTICAST.
2721 		 * Note that in order to send packets we need a host-specific
2722 		 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
2723 		 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
2724 		 * was not host specific.
2725 		 * However, ip_rts_request doesn't want to send packets
2726 		 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
2727 		 * it needs an IRE_IF_ALL to get to the ill. Thus
2728 		 * we return IRE_IF_ALL that are not host specific here.
2729 		 */
2730 		if (ire->ire_type & IRE_ONLINK)
2731 			goto done;
2732 		ire = ire->ire_dep_parent;
2733 	}
2734 	rw_exit(&ipst->ips_ire_dep_lock);
2735 	return (NULL);
2736 
2737 done:
2738 	ire_refhold(ire);
2739 	rw_exit(&ipst->ips_ire_dep_lock);
2740 	return (ire);
2741 }
2742 
2743 /*
2744  * Find the ill used to send packets. This will be NULL in case
2745  * of a reject or blackhole.
2746  * The returned ill is held; caller needs to do ill_refrele when done.
2747  */
2748 ill_t *
2749 ire_nexthop_ill(ire_t *ire)
2750 {
2751 	ill_t		*ill;
2752 
2753 	ire = ire_nexthop(ire);
2754 	if (ire == NULL)
2755 		return (NULL);
2756 
2757 	/* ire_ill can not change for an existing ire */
2758 	ill = ire->ire_ill;
2759 	if (ill != NULL)
2760 		ill_refhold(ill);
2761 	ire_refrele(ire);
2762 	return (ill);
2763 }
2764 
2765 #ifdef DEBUG
2766 static boolean_t
2767 parent_has_child(ire_t *parent, ire_t *child)
2768 {
2769 	ire_t	*ire;
2770 	ire_t	*prev;
2771 
2772 	ire = parent->ire_dep_children;
2773 	prev = NULL;
2774 	while (ire != NULL) {
2775 		if (prev == NULL) {
2776 			ASSERT(ire->ire_dep_sib_ptpn ==
2777 			    &(parent->ire_dep_children));
2778 		} else {
2779 			ASSERT(ire->ire_dep_sib_ptpn ==
2780 			    &(prev->ire_dep_sib_next));
2781 		}
2782 		if (ire == child)
2783 			return (B_TRUE);
2784 		prev = ire;
2785 		ire = ire->ire_dep_sib_next;
2786 	}
2787 	return (B_FALSE);
2788 }
2789 
2790 static void
2791 ire_dep_verify(ire_t *ire)
2792 {
2793 	ire_t		*parent = ire->ire_dep_parent;
2794 	ire_t		*child = ire->ire_dep_children;
2795 
2796 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
2797 	    ire->ire_ipversion == IPV6_VERSION);
2798 	if (parent != NULL) {
2799 		ASSERT(parent->ire_ipversion == IPV4_VERSION ||
2800 		    parent->ire_ipversion == IPV6_VERSION);
2801 		ASSERT(parent->ire_refcnt >= 1);
2802 		ASSERT(parent_has_child(parent, ire));
2803 	}
2804 	if (child != NULL) {
2805 		ASSERT(child->ire_ipversion == IPV4_VERSION ||
2806 		    child->ire_ipversion == IPV6_VERSION);
2807 		ASSERT(child->ire_dep_parent == ire);
2808 		ASSERT(child->ire_dep_sib_ptpn != NULL);
2809 		ASSERT(parent_has_child(ire, child));
2810 	}
2811 }
2812 #endif /* DEBUG */
2813 
2814 /*
2815  * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
2816  */
2817 void
2818 ire_dep_remove(ire_t *ire)
2819 {
2820 	ip_stack_t	*ipst = ire->ire_ipst;
2821 	ire_t		*parent = ire->ire_dep_parent;
2822 	ire_t		*next;
2823 	nce_t		*nce;
2824 
2825 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2826 	ASSERT(ire->ire_dep_parent != NULL);
2827 	ASSERT(ire->ire_dep_sib_ptpn != NULL);
2828 
2829 #ifdef DEBUG
2830 	ire_dep_verify(ire);
2831 	ire_dep_verify(parent);
2832 #endif
2833 
2834 	next = ire->ire_dep_sib_next;
2835 	if (next != NULL)
2836 		next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
2837 
2838 	ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
2839 	*(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
2840 
2841 	ire->ire_dep_sib_ptpn = NULL;
2842 	ire->ire_dep_sib_next = NULL;
2843 
2844 	mutex_enter(&ire->ire_lock);
2845 	parent = ire->ire_dep_parent;
2846 	ire->ire_dep_parent = NULL;
2847 	mutex_exit(&ire->ire_lock);
2848 
2849 	/*
2850 	 * Make sure all our children, grandchildren, etc set
2851 	 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
2852 	 * we can no longer guarantee than the children have a current
2853 	 * ire_nce_cache and ire_nexthop_ill().
2854 	 */
2855 	if (ire->ire_dep_children != NULL)
2856 		ire_dep_invalidate_children(ire->ire_dep_children);
2857 
2858 	/*
2859 	 * Since the parent is gone we make sure we clear ire_nce_cache.
2860 	 * We can clear it under ire_lock even if the IRE is used
2861 	 */
2862 	mutex_enter(&ire->ire_lock);
2863 	nce = ire->ire_nce_cache;
2864 	ire->ire_nce_cache = NULL;
2865 	mutex_exit(&ire->ire_lock);
2866 	if (nce != NULL)
2867 		nce_refrele(nce);
2868 
2869 #ifdef DEBUG
2870 	ire_dep_verify(ire);
2871 	ire_dep_verify(parent);
2872 #endif
2873 
2874 	ire_refrele_notr(parent);
2875 	ire_refrele_notr(ire);
2876 }
2877 
2878 /*
2879  * Insert the child in the linkage of the parent
2880  */
2881 static void
2882 ire_dep_parent_insert(ire_t *child, ire_t *parent)
2883 {
2884 	ip_stack_t	*ipst = child->ire_ipst;
2885 	ire_t		*next;
2886 
2887 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2888 	ASSERT(child->ire_dep_parent == NULL);
2889 
2890 #ifdef DEBUG
2891 	ire_dep_verify(child);
2892 	ire_dep_verify(parent);
2893 #endif
2894 	/* No parents => no siblings */
2895 	ASSERT(child->ire_dep_sib_ptpn == NULL);
2896 	ASSERT(child->ire_dep_sib_next == NULL);
2897 
2898 	ire_refhold_notr(parent);
2899 	ire_refhold_notr(child);
2900 
2901 	/* Head insertion */
2902 	next = parent->ire_dep_children;
2903 	if (next != NULL) {
2904 		ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
2905 		child->ire_dep_sib_next = next;
2906 		next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
2907 	}
2908 	parent->ire_dep_children = child;
2909 	child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
2910 
2911 	mutex_enter(&child->ire_lock);
2912 	child->ire_dep_parent = parent;
2913 	mutex_exit(&child->ire_lock);
2914 
2915 #ifdef DEBUG
2916 	ire_dep_verify(child);
2917 	ire_dep_verify(parent);
2918 #endif
2919 }
2920 
2921 
2922 /*
2923  * Given count worth of ires and generations, build ire_dep_* relationships
2924  * from ires[0] to ires[count-1]. Record generations[i+1] in
2925  * ire_dep_parent_generation for ires[i].
2926  * We graft onto an existing parent chain by making sure that we don't
2927  * touch ire_dep_parent for ires[count-1].
2928  *
2929  * We check for any condemned ire_generation count and return B_FALSE in
2930  * that case so that the caller can tear it apart.
2931  *
2932  * Note that generations[0] is not used. Caller handles that.
2933  */
2934 boolean_t
2935 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
2936 {
2937 	ire_t		*ire = ires[0];
2938 	ip_stack_t	*ipst;
2939 	uint_t		i;
2940 
2941 	ASSERT(count > 0);
2942 	if (count == 1) {
2943 		/* No work to do */
2944 		return (B_TRUE);
2945 	}
2946 	ipst = ire->ire_ipst;
2947 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2948 	/*
2949 	 * Do not remove the linkage for any existing parent chain i.e.,
2950 	 * ires[count-1] is left alone.
2951 	 */
2952 	for (i = 0; i < count-1; i++) {
2953 		/* Remove existing parent if we need to change it */
2954 		if (ires[i]->ire_dep_parent != NULL &&
2955 		    ires[i]->ire_dep_parent != ires[i+1])
2956 			ire_dep_remove(ires[i]);
2957 	}
2958 
2959 	for (i = 0; i < count - 1; i++) {
2960 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2961 		    ires[i]->ire_ipversion == IPV6_VERSION);
2962 		/* Does it need to change? */
2963 		if (ires[i]->ire_dep_parent != ires[i+1])
2964 			ire_dep_parent_insert(ires[i], ires[i+1]);
2965 
2966 		mutex_enter(&ires[i+1]->ire_lock);
2967 		if (IRE_IS_CONDEMNED(ires[i+1])) {
2968 			mutex_exit(&ires[i+1]->ire_lock);
2969 			rw_exit(&ipst->ips_ire_dep_lock);
2970 			return (B_FALSE);
2971 		}
2972 		mutex_exit(&ires[i+1]->ire_lock);
2973 
2974 		mutex_enter(&ires[i]->ire_lock);
2975 		ires[i]->ire_dep_parent_generation = generations[i+1];
2976 		mutex_exit(&ires[i]->ire_lock);
2977 	}
2978 	rw_exit(&ipst->ips_ire_dep_lock);
2979 	return (B_TRUE);
2980 }
2981 
2982 /*
2983  * Given count worth of ires, unbuild ire_dep_* relationships
2984  * from ires[0] to ires[count-1].
2985  */
2986 void
2987 ire_dep_unbuild(ire_t *ires[], uint_t count)
2988 {
2989 	ip_stack_t	*ipst;
2990 	uint_t		i;
2991 
2992 	if (count == 0) {
2993 		/* No work to do */
2994 		return;
2995 	}
2996 	ipst = ires[0]->ire_ipst;
2997 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2998 	for (i = 0; i < count; i++) {
2999 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
3000 		    ires[i]->ire_ipversion == IPV6_VERSION);
3001 		if (ires[i]->ire_dep_parent != NULL)
3002 			ire_dep_remove(ires[i]);
3003 		mutex_enter(&ires[i]->ire_lock);
3004 		ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3005 		mutex_exit(&ires[i]->ire_lock);
3006 	}
3007 	rw_exit(&ipst->ips_ire_dep_lock);
3008 }
3009 
3010 /*
3011  * Both the forwarding and the outbound code paths can trip on
3012  * a condemned NCE, in which case we call this function.
3013  * We have two different behaviors: if the NCE was UNREACHABLE
3014  * it is an indication that something failed. In that case
3015  * we see if we should look for a different IRE (for example,
3016  * delete any matching redirect IRE, or try a different
3017  * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
3018  * different IRE will be picked next time we send/forward.
3019  *
3020  * If we are called by the output path then fail_if_better is set
3021  * and we return NULL if there could be a better IRE. This is because the
3022  * output path retries the IRE lookup. (The input/forward path can not retry.)
3023  *
3024  * If the NCE was not unreachable then we pick/allocate a
3025  * new (most likely ND_INITIAL) NCE and proceed with it.
3026  *
3027  * ipha/ip6h are needed for multicast packets; ipha needs to be
3028  * set for IPv4 and ip6h needs to be set for IPv6 packets.
3029  */
3030 nce_t *
3031 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
3032     boolean_t fail_if_better)
3033 {
3034 	if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
3035 		if (ire_no_good(ire) && fail_if_better) {
3036 			/*
3037 			 * Did some changes, or ECMP likely to exist.
3038 			 * Make ip_output look for a different IRE
3039 			 */
3040 			return (NULL);
3041 		}
3042 	}
3043 	if (ire_revalidate_nce(ire) == ENETUNREACH) {
3044 		/* The ire_dep_parent chain went bad, or no memory? */
3045 		(void) ire_no_good(ire);
3046 		return (NULL);
3047 	}
3048 	if (ire->ire_ipversion == IPV4_VERSION) {
3049 		ASSERT(ipha != NULL);
3050 		nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
3051 	} else {
3052 		ASSERT(ip6h != NULL);
3053 		nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
3054 	}
3055 
3056 	if (nce == NULL)
3057 		return (NULL);
3058 	if (nce->nce_is_condemned) {
3059 		nce_refrele(nce);
3060 		return (NULL);
3061 	}
3062 	return (nce);
3063 }
3064 
3065 /*
3066  * The caller has found that the ire is bad, either due to a reference to an NCE
3067  * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved.
3068  * We update things so a subsequent attempt to send to the destination
3069  * is likely to find different IRE, or that a new NCE would be created.
3070  *
3071  * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
3072  * find a different route (either due to having deleted a redirect, or there
3073  * being ECMP routes.)
3074  *
3075  * If we have a redirect (RTF_DYNAMIC) we delete it.
3076  * Otherwise we increment ire_badcnt and increment the generation number so
3077  * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
3078  * into account in the route selection when we have multiple choices (multiple
3079  * default routes or ECMP in general).
3080  * Any time ip_select_route find an ire with a condemned ire_nce_cache
3081  * (e.g., if no equal cost route to the bad one) ip_select_route will make
3082  * sure the NCE is revalidated to avoid getting stuck on a
3083  * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
3084  */
3085 boolean_t
3086 ire_no_good(ire_t *ire)
3087 {
3088 	ip_stack_t	*ipst = ire->ire_ipst;
3089 	ire_t		*ire2;
3090 	nce_t		*nce;
3091 
3092 	if (ire->ire_flags & RTF_DYNAMIC) {
3093 		ire_delete(ire);
3094 		return (B_TRUE);
3095 	}
3096 	if (ire->ire_flags & RTF_INDIRECT) {
3097 		/* Check if next IRE is a redirect */
3098 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3099 		if (ire->ire_dep_parent != NULL &&
3100 		    (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
3101 			ire2 = ire->ire_dep_parent;
3102 			ire_refhold(ire2);
3103 		} else {
3104 			ire2 = NULL;
3105 		}
3106 		rw_exit(&ipst->ips_ire_dep_lock);
3107 		if (ire2 != NULL) {
3108 			ire_delete(ire2);
3109 			ire_refrele(ire2);
3110 			return (B_TRUE);
3111 		}
3112 	}
3113 	/*
3114 	 * No redirect involved. Increment badcnt so that if we have ECMP
3115 	 * routes we are likely to pick a different one for the next packet.
3116 	 *
3117 	 * If the NCE is unreachable and condemned we should drop the reference
3118 	 * to it so that a new NCE can be created.
3119 	 *
3120 	 * Finally we increment the generation number so that any ixa_ire
3121 	 * cache will be revalidated.
3122 	 */
3123 	mutex_enter(&ire->ire_lock);
3124 	ire->ire_badcnt++;
3125 	ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
3126 	nce = ire->ire_nce_cache;
3127 	if (nce != NULL && nce->nce_is_condemned &&
3128 	    nce->nce_common->ncec_state == ND_UNREACHABLE)
3129 		ire->ire_nce_cache = NULL;
3130 	else
3131 		nce = NULL;
3132 	mutex_exit(&ire->ire_lock);
3133 	if (nce != NULL)
3134 		nce_refrele(nce);
3135 
3136 	ire_increment_generation(ire);
3137 	ire_dep_incr_generation(ire);
3138 
3139 	return (ire->ire_bucket->irb_ire_cnt > 1);
3140 }
3141 
3142 /*
3143  * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
3144  * ire_dep_parent_generation.
3145  * If they all match we just return ire_generation from the topmost IRE.
3146  * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
3147  * above the mismatch to IRE_GENERATION_VERIFY and also returning
3148  * IRE_GENERATION_VERIFY.
3149  */
3150 uint_t
3151 ire_dep_validate_generations(ire_t *ire)
3152 {
3153 	ip_stack_t	*ipst = ire->ire_ipst;
3154 	uint_t		generation;
3155 	ire_t		*ire1;
3156 
3157 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3158 	generation = ire->ire_generation;	/* Assuming things match */
3159 	for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
3160 		ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
3161 		    ire1->ire_ipversion == IPV6_VERSION);
3162 		if (ire1->ire_dep_parent == NULL)
3163 			break;
3164 		if (ire1->ire_dep_parent_generation !=
3165 		    ire1->ire_dep_parent->ire_generation)
3166 			goto mismatch;
3167 	}
3168 	rw_exit(&ipst->ips_ire_dep_lock);
3169 	return (generation);
3170 
3171 mismatch:
3172 	generation = IRE_GENERATION_VERIFY;
3173 	/* Fill from top down to the mismatch with _VERIFY */
3174 	while (ire != ire1) {
3175 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3176 		    ire->ire_ipversion == IPV6_VERSION);
3177 		mutex_enter(&ire->ire_lock);
3178 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3179 		mutex_exit(&ire->ire_lock);
3180 		ire = ire->ire_dep_parent;
3181 	}
3182 	rw_exit(&ipst->ips_ire_dep_lock);
3183 	return (generation);
3184 }
3185 
3186 /*
3187  * Used when we need to return an ire with ire_dep_parent, but we
3188  * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
3189  * Using IRE_GENERATION_VERIFY means that next time we'll redo the
3190  * recursive lookup.
3191  */
3192 void
3193 ire_dep_invalidate_generations(ire_t *ire)
3194 {
3195 	ip_stack_t	*ipst = ire->ire_ipst;
3196 
3197 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3198 	while (ire != NULL) {
3199 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3200 		    ire->ire_ipversion == IPV6_VERSION);
3201 		mutex_enter(&ire->ire_lock);
3202 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3203 		mutex_exit(&ire->ire_lock);
3204 		ire = ire->ire_dep_parent;
3205 	}
3206 	rw_exit(&ipst->ips_ire_dep_lock);
3207 }
3208 
3209 /* Set _VERIFY ire_dep_parent_generation for all children recursively */
3210 static void
3211 ire_dep_invalidate_children(ire_t *child)
3212 {
3213 	ip_stack_t	*ipst = child->ire_ipst;
3214 
3215 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
3216 	/* Depth first */
3217 	if (child->ire_dep_children != NULL)
3218 		ire_dep_invalidate_children(child->ire_dep_children);
3219 
3220 	while (child != NULL) {
3221 		mutex_enter(&child->ire_lock);
3222 		child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3223 		mutex_exit(&child->ire_lock);
3224 		child = child->ire_dep_sib_next;
3225 	}
3226 }
3227 
3228 static void
3229 ire_dep_increment_children(ire_t *child)
3230 {
3231 	ip_stack_t	*ipst = child->ire_ipst;
3232 
3233 	ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
3234 	/* Depth first */
3235 	if (child->ire_dep_children != NULL)
3236 		ire_dep_increment_children(child->ire_dep_children);
3237 
3238 	while (child != NULL) {
3239 		if (!IRE_IS_CONDEMNED(child))
3240 			ire_increment_generation(child);
3241 		child = child->ire_dep_sib_next;
3242 	}
3243 }
3244 
3245 /*
3246  * Walk all the children of this ire recursively and increment their
3247  * generation number.
3248  */
3249 static void
3250 ire_dep_incr_generation_locked(ire_t *parent)
3251 {
3252 	ASSERT(RW_READ_HELD(&parent->ire_ipst->ips_ire_dep_lock));
3253 	if (parent->ire_dep_children != NULL)
3254 		ire_dep_increment_children(parent->ire_dep_children);
3255 }
3256 
3257 void
3258 ire_dep_incr_generation(ire_t *parent)
3259 {
3260 	ip_stack_t	*ipst = parent->ire_ipst;
3261 
3262 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3263 	ire_dep_incr_generation_locked(parent);
3264 	rw_exit(&ipst->ips_ire_dep_lock);
3265 }
3266 
3267 /*
3268  * Get a new ire_nce_cache for this IRE as well as its nexthop.
3269  * Returns zero if it succeeds. Can fail due to lack of memory or when
3270  * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
3271  * cases.
3272  *
3273  * In the in.mpathd case, the ire will have ire_testhidden
3274  * set; so we should create the ncec for the underlying ill.
3275  *
3276  * Note that the error returned by ire_revalidate_nce() is ignored by most
3277  * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
3278  * error to mark potentially bad ire's. For all the other callers, an
3279  * error return could indicate a transient condition like ENOMEM, or could
3280  * be the result of an interface that is going down/unplumbing. In the former
3281  * case (transient error), we would leave the old stale ire/ire_nce_cache
3282  * in place, and possibly use incorrect link-layer information to send packets
3283  * but would eventually recover. In the latter case (ill down/replumb),
3284  * ire_revalidate_nce() might return a condemned nce back, but we would then
3285  * recover in the packet output path.
3286  */
3287 int
3288 ire_revalidate_nce(ire_t *ire)
3289 {
3290 	nce_t		*nce, *old_nce;
3291 	ire_t		*nexthop;
3292 
3293 	/*
3294 	 * For multicast we conceptually have an NCE but we don't store it
3295 	 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
3296 	 */
3297 	if (ire->ire_type & IRE_MULTICAST)
3298 		return (0);
3299 
3300 	/* ire_testhidden should only be set on under-interfaces */
3301 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3302 
3303 	nexthop = ire_nexthop(ire);
3304 	if (nexthop == NULL) {
3305 		/* The route is potentially bad */
3306 		(void) ire_no_good(ire);
3307 		return (ENETUNREACH);
3308 	}
3309 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3310 		ASSERT(ire->ire_ill != NULL);
3311 
3312 		if (ire->ire_ipversion == IPV4_VERSION)
3313 			nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
3314 		else
3315 			nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
3316 	} else {
3317 		ASSERT(nexthop->ire_type & IRE_ONLINK);
3318 		if (ire->ire_ipversion == IPV4_VERSION) {
3319 			nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
3320 			    nexthop->ire_type);
3321 		} else {
3322 			nce = ndp_nce_init(nexthop->ire_ill,
3323 			    &nexthop->ire_addr_v6, nexthop->ire_type);
3324 		}
3325 	}
3326 	if (nce == NULL) {
3327 		/*
3328 		 * Leave the old stale one in place to avoid a NULL
3329 		 * ire_nce_cache.
3330 		 */
3331 		ire_refrele(nexthop);
3332 		return (ENOMEM);
3333 	}
3334 
3335 	if (nexthop != ire) {
3336 		/* Update the nexthop ire */
3337 		mutex_enter(&nexthop->ire_lock);
3338 		old_nce = nexthop->ire_nce_cache;
3339 		if (!IRE_IS_CONDEMNED(nexthop)) {
3340 			nce_refhold(nce);
3341 			nexthop->ire_nce_cache = nce;
3342 		} else {
3343 			nexthop->ire_nce_cache = NULL;
3344 		}
3345 		mutex_exit(&nexthop->ire_lock);
3346 		if (old_nce != NULL)
3347 			nce_refrele(old_nce);
3348 	}
3349 	ire_refrele(nexthop);
3350 
3351 	mutex_enter(&ire->ire_lock);
3352 	old_nce = ire->ire_nce_cache;
3353 	if (!IRE_IS_CONDEMNED(ire)) {
3354 		nce_refhold(nce);
3355 		ire->ire_nce_cache = nce;
3356 	} else {
3357 		ire->ire_nce_cache = NULL;
3358 	}
3359 	mutex_exit(&ire->ire_lock);
3360 	if (old_nce != NULL)
3361 		nce_refrele(old_nce);
3362 
3363 	nce_refrele(nce);
3364 	return (0);
3365 }
3366 
3367 /*
3368  * Get a held nce for a given ire.
3369  * In the common case this is just from ire_nce_cache.
3370  * For IRE_MULTICAST this needs to do an explicit lookup since we do not
3371  * have an IRE_MULTICAST per address.
3372  * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
3373  * so they can check whether the NCE went unreachable (as opposed to was
3374  * condemned for some other reason).
3375  */
3376 nce_t *
3377 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
3378 {
3379 	nce_t	*nce;
3380 
3381 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
3382 		return (NULL);
3383 
3384 	/* ire_testhidden should only be set on under-interfaces */
3385 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3386 
3387 	mutex_enter(&ire->ire_lock);
3388 	nce = ire->ire_nce_cache;
3389 	if (nce != NULL) {
3390 		nce_refhold(nce);
3391 		mutex_exit(&ire->ire_lock);
3392 		return (nce);
3393 	}
3394 	mutex_exit(&ire->ire_lock);
3395 
3396 	if (ire->ire_type & IRE_MULTICAST) {
3397 		ASSERT(ire->ire_ill != NULL);
3398 
3399 		if (ire->ire_ipversion == IPV4_VERSION) {
3400 			ASSERT(v6nexthop == NULL);
3401 
3402 			nce = arp_nce_init(ire->ire_ill, v4nexthop,
3403 			    ire->ire_type);
3404 		} else {
3405 			ASSERT(v6nexthop != NULL);
3406 			ASSERT(v4nexthop == 0);
3407 			nce = ndp_nce_init(ire->ire_ill, v6nexthop,
3408 			    ire->ire_type);
3409 		}
3410 		return (nce);
3411 	}
3412 	return (NULL);
3413 }
3414 
3415 nce_t *
3416 ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
3417 {
3418 	ipha_t		*ipha;
3419 	ip6_t		*ip6h;
3420 
3421 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3422 		ipha = (ipha_t *)mp->b_rptr;
3423 		return (ire_to_nce(ire, ipha->ipha_dst, NULL));
3424 	} else {
3425 		ip6h = (ip6_t *)mp->b_rptr;
3426 		return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
3427 	}
3428 }
3429 
3430 /*
3431  * Given an IRE_INTERFACE (that matches more than one address) create
3432  * and return an IRE_IF_CLONE for the specific address.
3433  * Return the generation number.
3434  * Returns NULL is no memory for the IRE.
3435  * Handles both IPv4 and IPv6.
3436  */
3437 ire_t *
3438 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
3439 {
3440 	ire_t		*ire;
3441 	ire_t		*nire;
3442 
3443 	if (ire_if->ire_ipversion == IPV4_VERSION) {
3444 		ipaddr_t	v4addr;
3445 		ipaddr_t	mask = IP_HOST_MASK;
3446 
3447 		ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
3448 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
3449 
3450 		ire = ire_create(
3451 		    (uchar_t *)&v4addr,			/* dest address */
3452 		    (uchar_t *)&mask,			/* mask */
3453 		    (uchar_t *)&ire_if->ire_gateway_addr,
3454 		    IRE_IF_CLONE,			/* IRE type */
3455 		    ire_if->ire_ill,
3456 		    ire_if->ire_zoneid,
3457 		    ire_if->ire_flags | RTF_HOST,
3458 		    NULL,		/* No security attr for IRE_IF_ALL */
3459 		    ire_if->ire_ipst);
3460 	} else {
3461 		ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
3462 		ire = ire_create_v6(
3463 		    addr,				/* dest address */
3464 		    &ipv6_all_ones,			/* mask */
3465 		    &ire_if->ire_gateway_addr_v6,	/* gateway addr */
3466 		    IRE_IF_CLONE,			/* IRE type */
3467 		    ire_if->ire_ill,
3468 		    ire_if->ire_zoneid,
3469 		    ire_if->ire_flags | RTF_HOST,
3470 		    NULL,		/* No security attr for IRE_IF_ALL */
3471 		    ire_if->ire_ipst);
3472 	}
3473 	if (ire == NULL)
3474 		return (NULL);
3475 
3476 	/* Take the metrics, in particular the mtu, from the IRE_IF */
3477 	ire->ire_metrics = ire_if->ire_metrics;
3478 
3479 	nire = ire_add(ire);
3480 	if (nire == NULL) /* Some failure */
3481 		return (NULL);
3482 
3483 	if (generationp != NULL)
3484 		*generationp = nire->ire_generation;
3485 
3486 	/*
3487 	 * Make sure races don't add a duplicate by
3488 	 * catching the case when an identical was returned.
3489 	 */
3490 	if (nire != ire) {
3491 		ASSERT(nire->ire_identical_ref > 1);
3492 		ire_delete(nire);
3493 	}
3494 	return (nire);
3495 }
3496 
3497 /*
3498  * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
3499  * ire_dep_children (just walk the ire_dep_sib_next since they are all
3500  * immediate children.)
3501  * Since we hold a lock while we remove them we need to defer the actual
3502  * calls to ire_delete() until we have dropped the lock. This makes things
3503  * less efficient since we restart at the top after dropping the lock. But
3504  * we only run when an IRE_INTERFACE is deleted which is infrquent.
3505  *
3506  * Note that ire_dep_children can be any mixture of offlink routes and
3507  * IRE_IF_CLONE entries.
3508  */
3509 void
3510 ire_dep_delete_if_clone(ire_t *parent)
3511 {
3512 	ip_stack_t	*ipst = parent->ire_ipst;
3513 	ire_t		*child, *next;
3514 
3515 restart:
3516 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3517 	if (parent->ire_dep_children == NULL) {
3518 		rw_exit(&ipst->ips_ire_dep_lock);
3519 		return;
3520 	}
3521 	child = parent->ire_dep_children;
3522 	while (child != NULL) {
3523 		next = child->ire_dep_sib_next;
3524 		if ((child->ire_type & IRE_IF_CLONE) &&
3525 		    !IRE_IS_CONDEMNED(child)) {
3526 			ire_refhold(child);
3527 			rw_exit(&ipst->ips_ire_dep_lock);
3528 			ire_delete(child);
3529 			ASSERT(IRE_IS_CONDEMNED(child));
3530 			ire_refrele(child);
3531 			goto restart;
3532 		}
3533 		child = next;
3534 	}
3535 	rw_exit(&ipst->ips_ire_dep_lock);
3536 }
3537 
3538 /*
3539  * ire_pref() is used in recursive route-resolution for a destination to
3540  * determine the preference of an ire, where "preference" is determined
3541  * based on the level of indirection to the destination of the ire.
3542  * A higher preference indicates that fewer lookups are needed to complete
3543  * recursive route lookup. Thus
3544  * ire_pref(RTF_INDIRECT) < ire_pref(IRE_IF_RESOLVER) < ire_pref(IRE_PREF_CLONE)
3545  */
3546 int
3547 ire_pref(ire_t *ire)
3548 {
3549 	if (ire->ire_flags & RTF_INDIRECT)
3550 		return (1);
3551 	if (ire->ire_type & IRE_OFFLINK)
3552 		return (2);
3553 	if (ire->ire_type & (IRE_IF_RESOLVER|IRE_IF_NORESOLVER))
3554 		return (3);
3555 	if (ire->ire_type & IRE_IF_CLONE)
3556 		return (4);
3557 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST))
3558 		return (5);
3559 	return (-1); /* unknown ire_type */
3560 }
3561