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