xref: /titanic_51/usr/src/uts/common/inet/ip/ip_ire.c (revision 3a634bfc9a31448c742688c603d3e76b83b041a0)
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_make_condemned(ipst->ips_ire_reject_v6);
2338 	ire_refrele_notr(ipst->ips_ire_reject_v6);
2339 	ipst->ips_ire_reject_v6 = NULL;
2340 
2341 	ire_make_condemned(ipst->ips_ire_reject_v4);
2342 	ire_refrele_notr(ipst->ips_ire_reject_v4);
2343 	ipst->ips_ire_reject_v4 = NULL;
2344 
2345 	ire_make_condemned(ipst->ips_ire_blackhole_v6);
2346 	ire_refrele_notr(ipst->ips_ire_blackhole_v6);
2347 	ipst->ips_ire_blackhole_v6 = NULL;
2348 
2349 	ire_make_condemned(ipst->ips_ire_blackhole_v4);
2350 	ire_refrele_notr(ipst->ips_ire_blackhole_v4);
2351 	ipst->ips_ire_blackhole_v4 = NULL;
2352 
2353 	/*
2354 	 * Delete all IREs - assumes that the ill/ipifs have
2355 	 * been removed so what remains are just the ftable to handle.
2356 	 */
2357 	ire_walk(ire_delete, NULL, ipst);
2358 
2359 	rn_freehead(ipst->ips_ip_ftable);
2360 	ipst->ips_ip_ftable = NULL;
2361 
2362 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
2363 
2364 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
2365 		irb_t *ptr;
2366 		int j;
2367 
2368 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
2369 			continue;
2370 
2371 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
2372 			ASSERT(ptr[j].irb_ire == NULL);
2373 			rw_destroy(&ptr[j].irb_lock);
2374 		}
2375 		mi_free(ptr);
2376 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
2377 	}
2378 }
2379 
2380 #ifdef DEBUG
2381 void
2382 ire_trace_ref(ire_t *ire)
2383 {
2384 	mutex_enter(&ire->ire_lock);
2385 	if (ire->ire_trace_disable) {
2386 		mutex_exit(&ire->ire_lock);
2387 		return;
2388 	}
2389 
2390 	if (th_trace_ref(ire, ire->ire_ipst)) {
2391 		mutex_exit(&ire->ire_lock);
2392 	} else {
2393 		ire->ire_trace_disable = B_TRUE;
2394 		mutex_exit(&ire->ire_lock);
2395 		ire_trace_cleanup(ire);
2396 	}
2397 }
2398 
2399 void
2400 ire_untrace_ref(ire_t *ire)
2401 {
2402 	mutex_enter(&ire->ire_lock);
2403 	if (!ire->ire_trace_disable)
2404 		th_trace_unref(ire);
2405 	mutex_exit(&ire->ire_lock);
2406 }
2407 
2408 static void
2409 ire_trace_cleanup(const ire_t *ire)
2410 {
2411 	th_trace_cleanup(ire, ire->ire_trace_disable);
2412 }
2413 #endif /* DEBUG */
2414 
2415 /*
2416  * Find, or create if needed, the nce_t pointer to the neighbor cache
2417  * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
2418  * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
2419  * on the next available under-ill (selected by the IPMP rotor) in the
2420  * unicast IPMP case.
2421  *
2422  * If a neighbor-cache entry has to be created (i.e., one does not already
2423  * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
2424  * entry are initialized in nce_add_v4(). The broadcast, multicast, and
2425  * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
2426  * the ncec_t created. The ncec_lladdr is non-null for all link types with
2427  * non-zero ill_phys_addr_length, though the contents may be zero in cases
2428  * where the link-layer type is not known at the time of creation
2429  * (e.g., IRE_IFRESOLVER links)
2430  *
2431  * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
2432  * has the physical broadcast address of the outgoing interface.
2433  * For unicast ire entries,
2434  *   - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
2435  *     ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
2436  *   - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
2437  *     layer resolution is necessary, so that the ncec_t will be in the
2438  *     ND_REACHABLE state
2439  *
2440  * The link layer information needed for broadcast addresses, and for
2441  * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
2442  * never needs re-verification for the lifetime of the ncec_t. These are
2443  * therefore marked NCE_F_NONUD.
2444  *
2445  * The nce returned will be created such that the nce_ill == ill that
2446  * is passed in. Note that the nce itself may not have ncec_ill == ill
2447  * where IPMP links are involved.
2448  */
2449 static nce_t *
2450 ire_nce_init(ill_t *ill, const void *addr, int ire_type)
2451 {
2452 	int		err;
2453 	nce_t		*nce = NULL;
2454 	uint16_t	ncec_flags;
2455 	uchar_t		*hwaddr;
2456 	boolean_t	need_refrele = B_FALSE;
2457 	ill_t		*in_ill = ill;
2458 	boolean_t	is_unicast;
2459 	uint_t		hwaddr_len;
2460 
2461 	is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
2462 	if (IS_IPMP(ill) ||
2463 	    ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
2464 		if ((ill = ipmp_ill_get_xmit_ill(ill, is_unicast)) == NULL)
2465 			return (NULL);
2466 		need_refrele = B_TRUE;
2467 	}
2468 	ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
2469 
2470 	switch (ire_type) {
2471 	case IRE_BROADCAST:
2472 		ASSERT(!ill->ill_isv6);
2473 		ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
2474 		break;
2475 	case IRE_MULTICAST:
2476 		ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
2477 		break;
2478 	}
2479 
2480 	if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
2481 		hwaddr = ill->ill_dest_addr;
2482 	} else {
2483 		hwaddr = NULL;
2484 	}
2485 	hwaddr_len = ill->ill_phys_addr_length;
2486 
2487 retry:
2488 	/* nce_state will be computed by nce_add_common() */
2489 	if (!ill->ill_isv6) {
2490 		err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
2491 		    ncec_flags, ND_UNCHANGED, &nce);
2492 	} else {
2493 		err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
2494 		    ncec_flags, ND_UNCHANGED, &nce);
2495 	}
2496 
2497 	switch (err) {
2498 	case 0:
2499 		break;
2500 	case EEXIST:
2501 		/*
2502 		 * When subnets change or partially overlap what was once
2503 		 * a broadcast address could now be a unicast, or vice versa.
2504 		 */
2505 		if (((ncec_flags ^ nce->nce_common->ncec_flags) &
2506 		    NCE_F_BCAST) != 0) {
2507 			ASSERT(!ill->ill_isv6);
2508 			ncec_delete(nce->nce_common);
2509 			nce_refrele(nce);
2510 			goto retry;
2511 		}
2512 		break;
2513 	default:
2514 		DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
2515 		if (need_refrele)
2516 			ill_refrele(ill);
2517 		return (NULL);
2518 	}
2519 	/*
2520 	 * If the ill was an under-ill of an IPMP group, we need to verify
2521 	 * that it is still active so that we select an active interface in
2522 	 * the group. However, since ipmp_ill_is_active ASSERTs for
2523 	 * IS_UNDER_IPMP(), we first need to verify that the ill is an
2524 	 * under-ill, and since this is being done in the data path, the
2525 	 * only way to ascertain this is by holding the ill_g_lock.
2526 	 */
2527 	rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
2528 	mutex_enter(&ill->ill_lock);
2529 	mutex_enter(&ill->ill_phyint->phyint_lock);
2530 	if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
2531 		/*
2532 		 * need_refrele implies that the under ill was selected by
2533 		 * ipmp_ill_get_xmit_ill() because either the in_ill was an
2534 		 * ipmp_ill, or we are sending a non-unicast packet on
2535 		 * an under_ill. However, when we get here, the ill selected by
2536 		 * ipmp_ill_get_xmit_ill  was pulled out of the active set
2537 		 * (for unicast)  or cast_ill nomination (for
2538 		 * !unicast) after it was  picked as the outgoing ill.
2539 		 * We have to pick an active interface and/or cast_ill in the
2540 		 * group.
2541 		 */
2542 		mutex_exit(&ill->ill_phyint->phyint_lock);
2543 		nce_delete(nce);
2544 		mutex_exit(&ill->ill_lock);
2545 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2546 		nce_refrele(nce);
2547 		ill_refrele(ill);
2548 		if ((ill = ipmp_ill_get_xmit_ill(in_ill, is_unicast)) == NULL)
2549 			return (NULL);
2550 		goto retry;
2551 	} else {
2552 		mutex_exit(&ill->ill_phyint->phyint_lock);
2553 		mutex_exit(&ill->ill_lock);
2554 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2555 	}
2556 done:
2557 	ASSERT(nce->nce_ill == ill);
2558 	if (need_refrele)
2559 		ill_refrele(ill);
2560 	return (nce);
2561 }
2562 
2563 nce_t *
2564 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
2565 {
2566 	return (ire_nce_init(ill, &addr4, ire_type));
2567 }
2568 
2569 nce_t *
2570 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
2571 {
2572 	ASSERT((ire_type & IRE_BROADCAST) == 0);
2573 	return (ire_nce_init(ill, addr6, ire_type));
2574 }
2575 
2576 /*
2577  * The caller should hold irb_lock as a writer if the ire is in a bucket.
2578  * This routine will clear ire_nce_cache, and we make sure that we can never
2579  * set ire_nce_cache after the ire is marked condemned.
2580  */
2581 void
2582 ire_make_condemned(ire_t *ire)
2583 {
2584 	ip_stack_t	*ipst = ire->ire_ipst;
2585 	nce_t		*nce;
2586 
2587 	mutex_enter(&ire->ire_lock);
2588 	ASSERT(ire->ire_bucket == NULL ||
2589 	    RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
2590 	ASSERT(!IRE_IS_CONDEMNED(ire));
2591 	ire->ire_generation = IRE_GENERATION_CONDEMNED;
2592 	/* Count how many condemned ires for kmem_cache callback */
2593 	atomic_add_32(&ipst->ips_num_ire_condemned, 1);
2594 	nce = ire->ire_nce_cache;
2595 	ire->ire_nce_cache = NULL;
2596 	mutex_exit(&ire->ire_lock);
2597 	if (nce != NULL)
2598 		nce_refrele(nce);
2599 }
2600 
2601 /*
2602  * Increment the generation avoiding the special condemned value
2603  */
2604 void
2605 ire_increment_generation(ire_t *ire)
2606 {
2607 	uint_t generation;
2608 
2609 	mutex_enter(&ire->ire_lock);
2610 	/*
2611 	 * Even though the caller has a hold it can't prevent a concurrent
2612 	 * ire_delete marking the IRE condemned
2613 	 */
2614 	if (!IRE_IS_CONDEMNED(ire)) {
2615 		generation = ire->ire_generation + 1;
2616 		if (generation == IRE_GENERATION_CONDEMNED)
2617 			generation = IRE_GENERATION_INITIAL;
2618 		ASSERT(generation != IRE_GENERATION_VERIFY);
2619 		ire->ire_generation = generation;
2620 	}
2621 	mutex_exit(&ire->ire_lock);
2622 }
2623 
2624 /*
2625  * Increment ire_generation on all the IRE_MULTICASTs
2626  * Used when the default multicast interface (as determined by
2627  * ill_lookup_multicast) might have changed.
2628  *
2629  * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
2630  * ill unplumb.
2631  */
2632 void
2633 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
2634 {
2635 	ill_t	*ill;
2636 	ill_walk_context_t ctx;
2637 
2638 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2639 	if (isv6)
2640 		ill = ILL_START_WALK_V6(&ctx, ipst);
2641 	else
2642 		ill = ILL_START_WALK_V4(&ctx, ipst);
2643 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2644 		if (ILL_IS_CONDEMNED(ill))
2645 			continue;
2646 		if (ill->ill_ire_multicast != NULL)
2647 			ire_increment_generation(ill->ill_ire_multicast);
2648 	}
2649 	rw_exit(&ipst->ips_ill_g_lock);
2650 }
2651 
2652 /*
2653  * Return a held IRE_NOROUTE with RTF_REJECT set
2654  */
2655 ire_t *
2656 ire_reject(ip_stack_t *ipst, boolean_t isv6)
2657 {
2658 	ire_t *ire;
2659 
2660 	if (isv6)
2661 		ire = ipst->ips_ire_reject_v6;
2662 	else
2663 		ire = ipst->ips_ire_reject_v4;
2664 
2665 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2666 	ire_refhold(ire);
2667 	return (ire);
2668 }
2669 
2670 /*
2671  * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
2672  */
2673 ire_t *
2674 ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
2675 {
2676 	ire_t *ire;
2677 
2678 	if (isv6)
2679 		ire = ipst->ips_ire_blackhole_v6;
2680 	else
2681 		ire = ipst->ips_ire_blackhole_v4;
2682 
2683 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2684 	ire_refhold(ire);
2685 	return (ire);
2686 }
2687 
2688 /*
2689  * Return a held IRE_MULTICAST.
2690  */
2691 ire_t *
2692 ire_multicast(ill_t *ill)
2693 {
2694 	ire_t *ire = ill->ill_ire_multicast;
2695 
2696 	ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
2697 	if (ire == NULL)
2698 		ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
2699 	else
2700 		ire_refhold(ire);
2701 	return (ire);
2702 }
2703 
2704 /*
2705  * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
2706  * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
2707  * This can return an RTF_REJECT|RTF_BLACKHOLE.
2708  * The returned IRE is held.
2709  * The assumption is that ip_select_route() has been called and returned the
2710  * IRE (thus ip_select_route would have set up the ire_dep* information.)
2711  * If some IRE is deleteted then ire_dep_remove() will have been called and
2712  * we might not find a nexthop IRE, in which case we return NULL.
2713  */
2714 ire_t *
2715 ire_nexthop(ire_t *ire)
2716 {
2717 	ip_stack_t	*ipst = ire->ire_ipst;
2718 
2719 	/* Acquire lock to walk ire_dep_parent */
2720 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
2721 	while (ire != NULL) {
2722 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2723 			goto done;
2724 		}
2725 		/*
2726 		 * If we find an IRE_ONLINK we are done. This includes
2727 		 * the case of IRE_MULTICAST.
2728 		 * Note that in order to send packets we need a host-specific
2729 		 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
2730 		 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
2731 		 * was not host specific.
2732 		 * However, ip_rts_request doesn't want to send packets
2733 		 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
2734 		 * it needs an IRE_IF_ALL to get to the ill. Thus
2735 		 * we return IRE_IF_ALL that are not host specific here.
2736 		 */
2737 		if (ire->ire_type & IRE_ONLINK)
2738 			goto done;
2739 		ire = ire->ire_dep_parent;
2740 	}
2741 	rw_exit(&ipst->ips_ire_dep_lock);
2742 	return (NULL);
2743 
2744 done:
2745 	ire_refhold(ire);
2746 	rw_exit(&ipst->ips_ire_dep_lock);
2747 	return (ire);
2748 }
2749 
2750 /*
2751  * Find the ill used to send packets. This will be NULL in case
2752  * of a reject or blackhole.
2753  * The returned ill is held; caller needs to do ill_refrele when done.
2754  */
2755 ill_t *
2756 ire_nexthop_ill(ire_t *ire)
2757 {
2758 	ill_t		*ill;
2759 
2760 	ire = ire_nexthop(ire);
2761 	if (ire == NULL)
2762 		return (NULL);
2763 
2764 	/* ire_ill can not change for an existing ire */
2765 	ill = ire->ire_ill;
2766 	if (ill != NULL)
2767 		ill_refhold(ill);
2768 	ire_refrele(ire);
2769 	return (ill);
2770 }
2771 
2772 #ifdef DEBUG
2773 static boolean_t
2774 parent_has_child(ire_t *parent, ire_t *child)
2775 {
2776 	ire_t	*ire;
2777 	ire_t	*prev;
2778 
2779 	ire = parent->ire_dep_children;
2780 	prev = NULL;
2781 	while (ire != NULL) {
2782 		if (prev == NULL) {
2783 			ASSERT(ire->ire_dep_sib_ptpn ==
2784 			    &(parent->ire_dep_children));
2785 		} else {
2786 			ASSERT(ire->ire_dep_sib_ptpn ==
2787 			    &(prev->ire_dep_sib_next));
2788 		}
2789 		if (ire == child)
2790 			return (B_TRUE);
2791 		prev = ire;
2792 		ire = ire->ire_dep_sib_next;
2793 	}
2794 	return (B_FALSE);
2795 }
2796 
2797 static void
2798 ire_dep_verify(ire_t *ire)
2799 {
2800 	ire_t		*parent = ire->ire_dep_parent;
2801 	ire_t		*child = ire->ire_dep_children;
2802 
2803 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
2804 	    ire->ire_ipversion == IPV6_VERSION);
2805 	if (parent != NULL) {
2806 		ASSERT(parent->ire_ipversion == IPV4_VERSION ||
2807 		    parent->ire_ipversion == IPV6_VERSION);
2808 		ASSERT(parent->ire_refcnt >= 1);
2809 		ASSERT(parent_has_child(parent, ire));
2810 	}
2811 	if (child != NULL) {
2812 		ASSERT(child->ire_ipversion == IPV4_VERSION ||
2813 		    child->ire_ipversion == IPV6_VERSION);
2814 		ASSERT(child->ire_dep_parent == ire);
2815 		ASSERT(child->ire_dep_sib_ptpn != NULL);
2816 		ASSERT(parent_has_child(ire, child));
2817 	}
2818 }
2819 #endif /* DEBUG */
2820 
2821 /*
2822  * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
2823  */
2824 void
2825 ire_dep_remove(ire_t *ire)
2826 {
2827 	ip_stack_t	*ipst = ire->ire_ipst;
2828 	ire_t		*parent = ire->ire_dep_parent;
2829 	ire_t		*next;
2830 	nce_t		*nce;
2831 
2832 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2833 	ASSERT(ire->ire_dep_parent != NULL);
2834 	ASSERT(ire->ire_dep_sib_ptpn != NULL);
2835 
2836 #ifdef DEBUG
2837 	ire_dep_verify(ire);
2838 	ire_dep_verify(parent);
2839 #endif
2840 
2841 	next = ire->ire_dep_sib_next;
2842 	if (next != NULL)
2843 		next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
2844 
2845 	ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
2846 	*(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
2847 
2848 	ire->ire_dep_sib_ptpn = NULL;
2849 	ire->ire_dep_sib_next = NULL;
2850 
2851 	mutex_enter(&ire->ire_lock);
2852 	parent = ire->ire_dep_parent;
2853 	ire->ire_dep_parent = NULL;
2854 	mutex_exit(&ire->ire_lock);
2855 
2856 	/*
2857 	 * Make sure all our children, grandchildren, etc set
2858 	 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
2859 	 * we can no longer guarantee than the children have a current
2860 	 * ire_nce_cache and ire_nexthop_ill().
2861 	 */
2862 	if (ire->ire_dep_children != NULL)
2863 		ire_dep_invalidate_children(ire->ire_dep_children);
2864 
2865 	/*
2866 	 * Since the parent is gone we make sure we clear ire_nce_cache.
2867 	 * We can clear it under ire_lock even if the IRE is used
2868 	 */
2869 	mutex_enter(&ire->ire_lock);
2870 	nce = ire->ire_nce_cache;
2871 	ire->ire_nce_cache = NULL;
2872 	mutex_exit(&ire->ire_lock);
2873 	if (nce != NULL)
2874 		nce_refrele(nce);
2875 
2876 #ifdef DEBUG
2877 	ire_dep_verify(ire);
2878 	ire_dep_verify(parent);
2879 #endif
2880 
2881 	ire_refrele_notr(parent);
2882 	ire_refrele_notr(ire);
2883 }
2884 
2885 /*
2886  * Insert the child in the linkage of the parent
2887  */
2888 static void
2889 ire_dep_parent_insert(ire_t *child, ire_t *parent)
2890 {
2891 	ip_stack_t	*ipst = child->ire_ipst;
2892 	ire_t		*next;
2893 
2894 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2895 	ASSERT(child->ire_dep_parent == NULL);
2896 
2897 #ifdef DEBUG
2898 	ire_dep_verify(child);
2899 	ire_dep_verify(parent);
2900 #endif
2901 	/* No parents => no siblings */
2902 	ASSERT(child->ire_dep_sib_ptpn == NULL);
2903 	ASSERT(child->ire_dep_sib_next == NULL);
2904 
2905 	ire_refhold_notr(parent);
2906 	ire_refhold_notr(child);
2907 
2908 	/* Head insertion */
2909 	next = parent->ire_dep_children;
2910 	if (next != NULL) {
2911 		ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
2912 		child->ire_dep_sib_next = next;
2913 		next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
2914 	}
2915 	parent->ire_dep_children = child;
2916 	child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
2917 
2918 	mutex_enter(&child->ire_lock);
2919 	child->ire_dep_parent = parent;
2920 	mutex_exit(&child->ire_lock);
2921 
2922 #ifdef DEBUG
2923 	ire_dep_verify(child);
2924 	ire_dep_verify(parent);
2925 #endif
2926 }
2927 
2928 
2929 /*
2930  * Given count worth of ires and generations, build ire_dep_* relationships
2931  * from ires[0] to ires[count-1]. Record generations[i+1] in
2932  * ire_dep_parent_generation for ires[i].
2933  * We graft onto an existing parent chain by making sure that we don't
2934  * touch ire_dep_parent for ires[count-1].
2935  *
2936  * We check for any condemned ire_generation count and return B_FALSE in
2937  * that case so that the caller can tear it apart.
2938  *
2939  * Note that generations[0] is not used. Caller handles that.
2940  */
2941 boolean_t
2942 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
2943 {
2944 	ire_t		*ire = ires[0];
2945 	ip_stack_t	*ipst;
2946 	uint_t		i;
2947 
2948 	ASSERT(count > 0);
2949 	if (count == 1) {
2950 		/* No work to do */
2951 		return (B_TRUE);
2952 	}
2953 	ipst = ire->ire_ipst;
2954 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2955 	/*
2956 	 * Do not remove the linkage for any existing parent chain i.e.,
2957 	 * ires[count-1] is left alone.
2958 	 */
2959 	for (i = 0; i < count-1; i++) {
2960 		/* Remove existing parent if we need to change it */
2961 		if (ires[i]->ire_dep_parent != NULL &&
2962 		    ires[i]->ire_dep_parent != ires[i+1])
2963 			ire_dep_remove(ires[i]);
2964 	}
2965 
2966 	for (i = 0; i < count - 1; i++) {
2967 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2968 		    ires[i]->ire_ipversion == IPV6_VERSION);
2969 		/* Does it need to change? */
2970 		if (ires[i]->ire_dep_parent != ires[i+1])
2971 			ire_dep_parent_insert(ires[i], ires[i+1]);
2972 
2973 		mutex_enter(&ires[i+1]->ire_lock);
2974 		if (IRE_IS_CONDEMNED(ires[i+1])) {
2975 			mutex_exit(&ires[i+1]->ire_lock);
2976 			rw_exit(&ipst->ips_ire_dep_lock);
2977 			return (B_FALSE);
2978 		}
2979 		mutex_exit(&ires[i+1]->ire_lock);
2980 
2981 		mutex_enter(&ires[i]->ire_lock);
2982 		ires[i]->ire_dep_parent_generation = generations[i+1];
2983 		mutex_exit(&ires[i]->ire_lock);
2984 	}
2985 	rw_exit(&ipst->ips_ire_dep_lock);
2986 	return (B_TRUE);
2987 }
2988 
2989 /*
2990  * Given count worth of ires, unbuild ire_dep_* relationships
2991  * from ires[0] to ires[count-1].
2992  */
2993 void
2994 ire_dep_unbuild(ire_t *ires[], uint_t count)
2995 {
2996 	ip_stack_t	*ipst;
2997 	uint_t		i;
2998 
2999 	if (count == 0) {
3000 		/* No work to do */
3001 		return;
3002 	}
3003 	ipst = ires[0]->ire_ipst;
3004 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
3005 	for (i = 0; i < count; i++) {
3006 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
3007 		    ires[i]->ire_ipversion == IPV6_VERSION);
3008 		if (ires[i]->ire_dep_parent != NULL)
3009 			ire_dep_remove(ires[i]);
3010 		mutex_enter(&ires[i]->ire_lock);
3011 		ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3012 		mutex_exit(&ires[i]->ire_lock);
3013 	}
3014 	rw_exit(&ipst->ips_ire_dep_lock);
3015 }
3016 
3017 /*
3018  * Both the forwarding and the outbound code paths can trip on
3019  * a condemned NCE, in which case we call this function.
3020  * We have two different behaviors: if the NCE was UNREACHABLE
3021  * it is an indication that something failed. In that case
3022  * we see if we should look for a different IRE (for example,
3023  * delete any matching redirect IRE, or try a different
3024  * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
3025  * different IRE will be picked next time we send/forward.
3026  *
3027  * If we are called by the output path then fail_if_better is set
3028  * and we return NULL if there could be a better IRE. This is because the
3029  * output path retries the IRE lookup. (The input/forward path can not retry.)
3030  *
3031  * If the NCE was not unreachable then we pick/allocate a
3032  * new (most likely ND_INITIAL) NCE and proceed with it.
3033  *
3034  * ipha/ip6h are needed for multicast packets; ipha needs to be
3035  * set for IPv4 and ip6h needs to be set for IPv6 packets.
3036  */
3037 nce_t *
3038 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
3039     boolean_t fail_if_better)
3040 {
3041 	if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
3042 		if (ire_no_good(ire) && fail_if_better) {
3043 			/*
3044 			 * Did some changes, or ECMP likely to exist.
3045 			 * Make ip_output look for a different IRE
3046 			 */
3047 			return (NULL);
3048 		}
3049 	}
3050 	if (ire_revalidate_nce(ire) == ENETUNREACH) {
3051 		/* The ire_dep_parent chain went bad, or no memory? */
3052 		(void) ire_no_good(ire);
3053 		return (NULL);
3054 	}
3055 	if (ire->ire_ipversion == IPV4_VERSION) {
3056 		ASSERT(ipha != NULL);
3057 		nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
3058 	} else {
3059 		ASSERT(ip6h != NULL);
3060 		nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
3061 	}
3062 
3063 	if (nce == NULL)
3064 		return (NULL);
3065 	if (nce->nce_is_condemned) {
3066 		nce_refrele(nce);
3067 		return (NULL);
3068 	}
3069 	return (nce);
3070 }
3071 
3072 /*
3073  * The caller has found that the ire is bad, either due to a reference to an NCE
3074  * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved.
3075  * We update things so a subsequent attempt to send to the destination
3076  * is likely to find different IRE, or that a new NCE would be created.
3077  *
3078  * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
3079  * find a different route (either due to having deleted a redirect, or there
3080  * being ECMP routes.)
3081  *
3082  * If we have a redirect (RTF_DYNAMIC) we delete it.
3083  * Otherwise we increment ire_badcnt and increment the generation number so
3084  * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
3085  * into account in the route selection when we have multiple choices (multiple
3086  * default routes or ECMP in general).
3087  * Any time ip_select_route find an ire with a condemned ire_nce_cache
3088  * (e.g., if no equal cost route to the bad one) ip_select_route will make
3089  * sure the NCE is revalidated to avoid getting stuck on a
3090  * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
3091  */
3092 boolean_t
3093 ire_no_good(ire_t *ire)
3094 {
3095 	ip_stack_t	*ipst = ire->ire_ipst;
3096 	ire_t		*ire2;
3097 	nce_t		*nce;
3098 
3099 	if (ire->ire_flags & RTF_DYNAMIC) {
3100 		ire_delete(ire);
3101 		return (B_TRUE);
3102 	}
3103 	if (ire->ire_flags & RTF_INDIRECT) {
3104 		/* Check if next IRE is a redirect */
3105 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3106 		if (ire->ire_dep_parent != NULL &&
3107 		    (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
3108 			ire2 = ire->ire_dep_parent;
3109 			ire_refhold(ire2);
3110 		} else {
3111 			ire2 = NULL;
3112 		}
3113 		rw_exit(&ipst->ips_ire_dep_lock);
3114 		if (ire2 != NULL) {
3115 			ire_delete(ire2);
3116 			ire_refrele(ire2);
3117 			return (B_TRUE);
3118 		}
3119 	}
3120 	/*
3121 	 * No redirect involved. Increment badcnt so that if we have ECMP
3122 	 * routes we are likely to pick a different one for the next packet.
3123 	 *
3124 	 * If the NCE is unreachable and condemned we should drop the reference
3125 	 * to it so that a new NCE can be created.
3126 	 *
3127 	 * Finally we increment the generation number so that any ixa_ire
3128 	 * cache will be revalidated.
3129 	 */
3130 	mutex_enter(&ire->ire_lock);
3131 	ire->ire_badcnt++;
3132 	ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
3133 	nce = ire->ire_nce_cache;
3134 	if (nce != NULL && nce->nce_is_condemned &&
3135 	    nce->nce_common->ncec_state == ND_UNREACHABLE)
3136 		ire->ire_nce_cache = NULL;
3137 	else
3138 		nce = NULL;
3139 	mutex_exit(&ire->ire_lock);
3140 	if (nce != NULL)
3141 		nce_refrele(nce);
3142 
3143 	ire_increment_generation(ire);
3144 	ire_dep_incr_generation(ire);
3145 
3146 	return (ire->ire_bucket->irb_ire_cnt > 1);
3147 }
3148 
3149 /*
3150  * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
3151  * ire_dep_parent_generation.
3152  * If they all match we just return ire_generation from the topmost IRE.
3153  * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
3154  * above the mismatch to IRE_GENERATION_VERIFY and also returning
3155  * IRE_GENERATION_VERIFY.
3156  */
3157 uint_t
3158 ire_dep_validate_generations(ire_t *ire)
3159 {
3160 	ip_stack_t	*ipst = ire->ire_ipst;
3161 	uint_t		generation;
3162 	ire_t		*ire1;
3163 
3164 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3165 	generation = ire->ire_generation;	/* Assuming things match */
3166 	for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
3167 		ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
3168 		    ire1->ire_ipversion == IPV6_VERSION);
3169 		if (ire1->ire_dep_parent == NULL)
3170 			break;
3171 		if (ire1->ire_dep_parent_generation !=
3172 		    ire1->ire_dep_parent->ire_generation)
3173 			goto mismatch;
3174 	}
3175 	rw_exit(&ipst->ips_ire_dep_lock);
3176 	return (generation);
3177 
3178 mismatch:
3179 	generation = IRE_GENERATION_VERIFY;
3180 	/* Fill from top down to the mismatch with _VERIFY */
3181 	while (ire != ire1) {
3182 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3183 		    ire->ire_ipversion == IPV6_VERSION);
3184 		mutex_enter(&ire->ire_lock);
3185 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3186 		mutex_exit(&ire->ire_lock);
3187 		ire = ire->ire_dep_parent;
3188 	}
3189 	rw_exit(&ipst->ips_ire_dep_lock);
3190 	return (generation);
3191 }
3192 
3193 /*
3194  * Used when we need to return an ire with ire_dep_parent, but we
3195  * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
3196  * Using IRE_GENERATION_VERIFY means that next time we'll redo the
3197  * recursive lookup.
3198  */
3199 void
3200 ire_dep_invalidate_generations(ire_t *ire)
3201 {
3202 	ip_stack_t	*ipst = ire->ire_ipst;
3203 
3204 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3205 	while (ire != NULL) {
3206 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3207 		    ire->ire_ipversion == IPV6_VERSION);
3208 		mutex_enter(&ire->ire_lock);
3209 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3210 		mutex_exit(&ire->ire_lock);
3211 		ire = ire->ire_dep_parent;
3212 	}
3213 	rw_exit(&ipst->ips_ire_dep_lock);
3214 }
3215 
3216 /* Set _VERIFY ire_dep_parent_generation for all children recursively */
3217 static void
3218 ire_dep_invalidate_children(ire_t *child)
3219 {
3220 	ip_stack_t	*ipst = child->ire_ipst;
3221 
3222 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
3223 	/* Depth first */
3224 	if (child->ire_dep_children != NULL)
3225 		ire_dep_invalidate_children(child->ire_dep_children);
3226 
3227 	while (child != NULL) {
3228 		mutex_enter(&child->ire_lock);
3229 		child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3230 		mutex_exit(&child->ire_lock);
3231 		child = child->ire_dep_sib_next;
3232 	}
3233 }
3234 
3235 static void
3236 ire_dep_increment_children(ire_t *child)
3237 {
3238 	ip_stack_t	*ipst = child->ire_ipst;
3239 
3240 	ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
3241 	/* Depth first */
3242 	if (child->ire_dep_children != NULL)
3243 		ire_dep_increment_children(child->ire_dep_children);
3244 
3245 	while (child != NULL) {
3246 		if (!IRE_IS_CONDEMNED(child))
3247 			ire_increment_generation(child);
3248 		child = child->ire_dep_sib_next;
3249 	}
3250 }
3251 
3252 /*
3253  * Walk all the children of this ire recursively and increment their
3254  * generation number.
3255  */
3256 static void
3257 ire_dep_incr_generation_locked(ire_t *parent)
3258 {
3259 	ASSERT(RW_READ_HELD(&parent->ire_ipst->ips_ire_dep_lock));
3260 	if (parent->ire_dep_children != NULL)
3261 		ire_dep_increment_children(parent->ire_dep_children);
3262 }
3263 
3264 void
3265 ire_dep_incr_generation(ire_t *parent)
3266 {
3267 	ip_stack_t	*ipst = parent->ire_ipst;
3268 
3269 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3270 	ire_dep_incr_generation_locked(parent);
3271 	rw_exit(&ipst->ips_ire_dep_lock);
3272 }
3273 
3274 /*
3275  * Get a new ire_nce_cache for this IRE as well as its nexthop.
3276  * Returns zero if it succeeds. Can fail due to lack of memory or when
3277  * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
3278  * cases.
3279  *
3280  * In the in.mpathd case, the ire will have ire_testhidden
3281  * set; so we should create the ncec for the underlying ill.
3282  *
3283  * Note that the error returned by ire_revalidate_nce() is ignored by most
3284  * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
3285  * error to mark potentially bad ire's. For all the other callers, an
3286  * error return could indicate a transient condition like ENOMEM, or could
3287  * be the result of an interface that is going down/unplumbing. In the former
3288  * case (transient error), we would leave the old stale ire/ire_nce_cache
3289  * in place, and possibly use incorrect link-layer information to send packets
3290  * but would eventually recover. In the latter case (ill down/replumb),
3291  * ire_revalidate_nce() might return a condemned nce back, but we would then
3292  * recover in the packet output path.
3293  */
3294 int
3295 ire_revalidate_nce(ire_t *ire)
3296 {
3297 	nce_t		*nce, *old_nce;
3298 	ire_t		*nexthop;
3299 
3300 	/*
3301 	 * For multicast we conceptually have an NCE but we don't store it
3302 	 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
3303 	 */
3304 	if (ire->ire_type & IRE_MULTICAST)
3305 		return (0);
3306 
3307 	/* ire_testhidden should only be set on under-interfaces */
3308 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3309 
3310 	nexthop = ire_nexthop(ire);
3311 	if (nexthop == NULL) {
3312 		/* The route is potentially bad */
3313 		(void) ire_no_good(ire);
3314 		return (ENETUNREACH);
3315 	}
3316 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3317 		ASSERT(ire->ire_ill != NULL);
3318 
3319 		if (ire->ire_ipversion == IPV4_VERSION)
3320 			nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
3321 		else
3322 			nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
3323 	} else {
3324 		ASSERT(nexthop->ire_type & IRE_ONLINK);
3325 		if (ire->ire_ipversion == IPV4_VERSION) {
3326 			nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
3327 			    nexthop->ire_type);
3328 		} else {
3329 			nce = ndp_nce_init(nexthop->ire_ill,
3330 			    &nexthop->ire_addr_v6, nexthop->ire_type);
3331 		}
3332 	}
3333 	if (nce == NULL) {
3334 		/*
3335 		 * Leave the old stale one in place to avoid a NULL
3336 		 * ire_nce_cache.
3337 		 */
3338 		ire_refrele(nexthop);
3339 		return (ENOMEM);
3340 	}
3341 
3342 	if (nexthop != ire) {
3343 		/* Update the nexthop ire */
3344 		mutex_enter(&nexthop->ire_lock);
3345 		old_nce = nexthop->ire_nce_cache;
3346 		if (!IRE_IS_CONDEMNED(nexthop)) {
3347 			nce_refhold(nce);
3348 			nexthop->ire_nce_cache = nce;
3349 		} else {
3350 			nexthop->ire_nce_cache = NULL;
3351 		}
3352 		mutex_exit(&nexthop->ire_lock);
3353 		if (old_nce != NULL)
3354 			nce_refrele(old_nce);
3355 	}
3356 	ire_refrele(nexthop);
3357 
3358 	mutex_enter(&ire->ire_lock);
3359 	old_nce = ire->ire_nce_cache;
3360 	if (!IRE_IS_CONDEMNED(ire)) {
3361 		nce_refhold(nce);
3362 		ire->ire_nce_cache = nce;
3363 	} else {
3364 		ire->ire_nce_cache = NULL;
3365 	}
3366 	mutex_exit(&ire->ire_lock);
3367 	if (old_nce != NULL)
3368 		nce_refrele(old_nce);
3369 
3370 	nce_refrele(nce);
3371 	return (0);
3372 }
3373 
3374 /*
3375  * Get a held nce for a given ire.
3376  * In the common case this is just from ire_nce_cache.
3377  * For IRE_MULTICAST this needs to do an explicit lookup since we do not
3378  * have an IRE_MULTICAST per address.
3379  * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
3380  * so they can check whether the NCE went unreachable (as opposed to was
3381  * condemned for some other reason).
3382  */
3383 nce_t *
3384 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
3385 {
3386 	nce_t	*nce;
3387 
3388 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
3389 		return (NULL);
3390 
3391 	/* ire_testhidden should only be set on under-interfaces */
3392 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3393 
3394 	mutex_enter(&ire->ire_lock);
3395 	nce = ire->ire_nce_cache;
3396 	if (nce != NULL) {
3397 		nce_refhold(nce);
3398 		mutex_exit(&ire->ire_lock);
3399 		return (nce);
3400 	}
3401 	mutex_exit(&ire->ire_lock);
3402 
3403 	if (ire->ire_type & IRE_MULTICAST) {
3404 		ASSERT(ire->ire_ill != NULL);
3405 
3406 		if (ire->ire_ipversion == IPV4_VERSION) {
3407 			ASSERT(v6nexthop == NULL);
3408 
3409 			nce = arp_nce_init(ire->ire_ill, v4nexthop,
3410 			    ire->ire_type);
3411 		} else {
3412 			ASSERT(v6nexthop != NULL);
3413 			ASSERT(v4nexthop == 0);
3414 			nce = ndp_nce_init(ire->ire_ill, v6nexthop,
3415 			    ire->ire_type);
3416 		}
3417 		return (nce);
3418 	}
3419 	return (NULL);
3420 }
3421 
3422 nce_t *
3423 ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
3424 {
3425 	ipha_t		*ipha;
3426 	ip6_t		*ip6h;
3427 
3428 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3429 		ipha = (ipha_t *)mp->b_rptr;
3430 		return (ire_to_nce(ire, ipha->ipha_dst, NULL));
3431 	} else {
3432 		ip6h = (ip6_t *)mp->b_rptr;
3433 		return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
3434 	}
3435 }
3436 
3437 /*
3438  * Given an IRE_INTERFACE (that matches more than one address) create
3439  * and return an IRE_IF_CLONE for the specific address.
3440  * Return the generation number.
3441  * Returns NULL is no memory for the IRE.
3442  * Handles both IPv4 and IPv6.
3443  */
3444 ire_t *
3445 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
3446 {
3447 	ire_t		*ire;
3448 	ire_t		*nire;
3449 
3450 	if (ire_if->ire_ipversion == IPV4_VERSION) {
3451 		ipaddr_t	v4addr;
3452 		ipaddr_t	mask = IP_HOST_MASK;
3453 
3454 		ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
3455 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
3456 
3457 		ire = ire_create(
3458 		    (uchar_t *)&v4addr,			/* dest address */
3459 		    (uchar_t *)&mask,			/* mask */
3460 		    (uchar_t *)&ire_if->ire_gateway_addr,
3461 		    IRE_IF_CLONE,			/* IRE type */
3462 		    ire_if->ire_ill,
3463 		    ire_if->ire_zoneid,
3464 		    ire_if->ire_flags | RTF_HOST,
3465 		    NULL,		/* No security attr for IRE_IF_ALL */
3466 		    ire_if->ire_ipst);
3467 	} else {
3468 		ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
3469 		ire = ire_create_v6(
3470 		    addr,				/* dest address */
3471 		    &ipv6_all_ones,			/* mask */
3472 		    &ire_if->ire_gateway_addr_v6,	/* gateway addr */
3473 		    IRE_IF_CLONE,			/* IRE type */
3474 		    ire_if->ire_ill,
3475 		    ire_if->ire_zoneid,
3476 		    ire_if->ire_flags | RTF_HOST,
3477 		    NULL,		/* No security attr for IRE_IF_ALL */
3478 		    ire_if->ire_ipst);
3479 	}
3480 	if (ire == NULL)
3481 		return (NULL);
3482 
3483 	/* Take the metrics, in particular the mtu, from the IRE_IF */
3484 	ire->ire_metrics = ire_if->ire_metrics;
3485 
3486 	nire = ire_add(ire);
3487 	if (nire == NULL) /* Some failure */
3488 		return (NULL);
3489 
3490 	if (generationp != NULL)
3491 		*generationp = nire->ire_generation;
3492 
3493 	/*
3494 	 * Make sure races don't add a duplicate by
3495 	 * catching the case when an identical was returned.
3496 	 */
3497 	if (nire != ire) {
3498 		ASSERT(nire->ire_identical_ref > 1);
3499 		ire_delete(nire);
3500 	}
3501 	return (nire);
3502 }
3503 
3504 /*
3505  * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
3506  * ire_dep_children (just walk the ire_dep_sib_next since they are all
3507  * immediate children.)
3508  * Since we hold a lock while we remove them we need to defer the actual
3509  * calls to ire_delete() until we have dropped the lock. This makes things
3510  * less efficient since we restart at the top after dropping the lock. But
3511  * we only run when an IRE_INTERFACE is deleted which is infrquent.
3512  *
3513  * Note that ire_dep_children can be any mixture of offlink routes and
3514  * IRE_IF_CLONE entries.
3515  */
3516 void
3517 ire_dep_delete_if_clone(ire_t *parent)
3518 {
3519 	ip_stack_t	*ipst = parent->ire_ipst;
3520 	ire_t		*child, *next;
3521 
3522 restart:
3523 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3524 	if (parent->ire_dep_children == NULL) {
3525 		rw_exit(&ipst->ips_ire_dep_lock);
3526 		return;
3527 	}
3528 	child = parent->ire_dep_children;
3529 	while (child != NULL) {
3530 		next = child->ire_dep_sib_next;
3531 		if ((child->ire_type & IRE_IF_CLONE) &&
3532 		    !IRE_IS_CONDEMNED(child)) {
3533 			ire_refhold(child);
3534 			rw_exit(&ipst->ips_ire_dep_lock);
3535 			ire_delete(child);
3536 			ASSERT(IRE_IS_CONDEMNED(child));
3537 			ire_refrele(child);
3538 			goto restart;
3539 		}
3540 		child = next;
3541 	}
3542 	rw_exit(&ipst->ips_ire_dep_lock);
3543 }
3544 
3545 /*
3546  * ire_pref() is used in recursive route-resolution for a destination to
3547  * determine the preference of an ire, where "preference" is determined
3548  * based on the level of indirection to the destination of the ire.
3549  * A higher preference indicates that fewer lookups are needed to complete
3550  * recursive route lookup. Thus
3551  * ire_pref(RTF_INDIRECT) < ire_pref(IRE_IF_RESOLVER) < ire_pref(IRE_PREF_CLONE)
3552  */
3553 int
3554 ire_pref(ire_t *ire)
3555 {
3556 	if (ire->ire_flags & RTF_INDIRECT)
3557 		return (1);
3558 	if (ire->ire_type & IRE_OFFLINK)
3559 		return (2);
3560 	if (ire->ire_type & (IRE_IF_RESOLVER|IRE_IF_NORESOLVER))
3561 		return (3);
3562 	if (ire->ire_type & IRE_IF_CLONE)
3563 		return (4);
3564 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST))
3565 		return (5);
3566 	return (-1); /* unknown ire_type */
3567 }
3568