xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision 6e91bba0)
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_TYPE)) || (ire->ire_type & type)) &&
2010 	    ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
2011 	    ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask)) &&
2012 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
2013 	    (!is_system_labeled()) ||
2014 	    (tsol_ire_match_gwattr(ire, tsl) == 0))) {
2015 		/* We found the matched IRE */
2016 		return (B_TRUE);
2017 	}
2018 	return (B_FALSE);
2019 }
2020 
2021 /*
2022  * Check if the IRE_LOCAL uses the same ill as another route would use.
2023  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
2024  * then we don't allow this IRE_LOCAL to be used.
2025  * We always return an IRE; will be RTF_REJECT if no route available.
2026  */
2027 ire_t *
2028 ire_alt_local(ire_t *ire, zoneid_t zoneid, const ts_label_t *tsl,
2029     const ill_t *ill, uint_t *generationp)
2030 {
2031 	ip_stack_t	*ipst = ire->ire_ipst;
2032 	ire_t		*alt_ire;
2033 	uint_t		ire_type;
2034 	uint_t		generation;
2035 	uint_t		match_flags;
2036 
2037 	ASSERT(ire->ire_type & IRE_LOCAL);
2038 	ASSERT(ire->ire_ill != NULL);
2039 
2040 	/*
2041 	 * Need to match on everything but local.
2042 	 * This might result in the creation of a IRE_IF_CLONE for the
2043 	 * same address as the IRE_LOCAL when restrict_interzone_loopback is
2044 	 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted
2045 	 * to make sure the IRE_LOCAL is always found first.
2046 	 */
2047 	ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK);
2048 	match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
2049 	if (ill != NULL)
2050 		match_flags |= MATCH_IRE_ILL;
2051 
2052 	if (ire->ire_ipversion == IPV4_VERSION) {
2053 		alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type,
2054 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2055 		    NULL, &generation);
2056 	} else {
2057 		alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type,
2058 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2059 		    NULL, &generation);
2060 	}
2061 	ASSERT(alt_ire != NULL);
2062 
2063 	if (alt_ire->ire_ill == ire->ire_ill) {
2064 		/* Going out the same ILL - ok to send to IRE_LOCAL */
2065 		ire_refrele(alt_ire);
2066 	} else {
2067 		/* Different ill - ignore IRE_LOCAL */
2068 		ire_refrele(ire);
2069 		ire = alt_ire;
2070 		if (generationp != NULL)
2071 			*generationp = generation;
2072 	}
2073 	return (ire);
2074 }
2075 
2076 boolean_t
2077 ire_find_zoneid(struct radix_node *rn, void *arg)
2078 {
2079 	struct rt_entry *rt = (struct rt_entry *)rn;
2080 	irb_t *irb;
2081 	ire_t *ire;
2082 	ire_ftable_args_t *margs = arg;
2083 
2084 	ASSERT(rt != NULL);
2085 
2086 	irb = &rt->rt_irb;
2087 
2088 	if (irb->irb_ire_cnt == 0)
2089 		return (B_FALSE);
2090 
2091 	rw_enter(&irb->irb_lock, RW_READER);
2092 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2093 		if (IRE_IS_CONDEMNED(ire))
2094 			continue;
2095 
2096 		if (!(ire->ire_type & IRE_INTERFACE))
2097 			continue;
2098 
2099 		if (ire->ire_zoneid != ALL_ZONES &&
2100 		    ire->ire_zoneid != margs->ift_zoneid)
2101 			continue;
2102 
2103 		if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill)
2104 			continue;
2105 
2106 		if (is_system_labeled() &&
2107 		    tsol_ire_match_gwattr(ire, margs->ift_tsl) != 0)
2108 			continue;
2109 
2110 		rw_exit(&irb->irb_lock);
2111 		return (B_TRUE);
2112 	}
2113 	rw_exit(&irb->irb_lock);
2114 	return (B_FALSE);
2115 }
2116 
2117 /*
2118  * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
2119  * gateway address. If ill is non-NULL we also match on it.
2120  * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
2121  */
2122 boolean_t
2123 ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill,
2124     const ts_label_t *tsl, ip_stack_t *ipst, boolean_t lock_held)
2125 {
2126 	struct rt_sockaddr rdst;
2127 	struct rt_entry *rt;
2128 	ire_ftable_args_t margs;
2129 
2130 	ASSERT(ill == NULL || !ill->ill_isv6);
2131 	if (lock_held)
2132 		ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock));
2133 	else
2134 		RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
2135 
2136 	bzero(&rdst, sizeof (rdst));
2137 	rdst.rt_sin_len = sizeof (rdst);
2138 	rdst.rt_sin_family = AF_INET;
2139 	rdst.rt_sin_addr.s_addr = gateway;
2140 
2141 	/*
2142 	 * We only use margs for ill, zoneid, and tsl matching in
2143 	 * ire_find_zoneid
2144 	 */
2145 	bzero(&margs, sizeof (margs));
2146 	margs.ift_ill = ill;
2147 	margs.ift_zoneid = zoneid;
2148 	margs.ift_tsl = tsl;
2149 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
2150 	    ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs);
2151 
2152 	if (!lock_held)
2153 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
2154 
2155 	return (rt != NULL);
2156 }
2157 
2158 /*
2159  * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs.
2160  * The fraction argument tells us what fraction of the IREs to delete.
2161  * Common for IPv4 and IPv6.
2162  * Used when memory backpressure.
2163  */
2164 static void
2165 ire_delete_reclaim(ire_t *ire, char *arg)
2166 {
2167 	ip_stack_t	*ipst = ire->ire_ipst;
2168 	uint_t		fraction = *(uint_t *)arg;
2169 	uint_t		rand;
2170 
2171 	if ((ire->ire_flags & RTF_DYNAMIC) ||
2172 	    (ire->ire_type & IRE_IF_CLONE)) {
2173 
2174 		/* Pick a random number */
2175 		rand = (uint_t)ddi_get_lbolt() +
2176 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256);
2177 
2178 		/* Use truncation */
2179 		if ((rand/fraction)*fraction == rand) {
2180 			IP_STAT(ipst, ip_ire_reclaim_deleted);
2181 			ire_delete(ire);
2182 		}
2183 	}
2184 
2185 }
2186 
2187 /*
2188  * kmem_cache callback to free up memory.
2189  *
2190  * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically
2191  * (RTF_DYNAMIC and IRE_IF_CLONE).
2192  */
2193 static void
2194 ip_ire_reclaim_stack(ip_stack_t *ipst)
2195 {
2196 	uint_t	fraction = ipst->ips_ip_ire_reclaim_fraction;
2197 
2198 	IP_STAT(ipst, ip_ire_reclaim_calls);
2199 
2200 	ire_walk(ire_delete_reclaim, &fraction, ipst);
2201 
2202 	/*
2203 	 * Walk all CONNs that can have a reference on an ire, nce or dce.
2204 	 * Get them to update any stale references to drop any refholds they
2205 	 * have.
2206 	 */
2207 	ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst);
2208 }
2209 
2210 /*
2211  * Called by the memory allocator subsystem directly, when the system
2212  * is running low on memory.
2213  */
2214 /* ARGSUSED */
2215 void
2216 ip_ire_reclaim(void *args)
2217 {
2218 	netstack_handle_t nh;
2219 	netstack_t *ns;
2220 	ip_stack_t *ipst;
2221 
2222 	netstack_next_init(&nh);
2223 	while ((ns = netstack_next(&nh)) != NULL) {
2224 		/*
2225 		 * netstack_next() can return a netstack_t with a NULL
2226 		 * netstack_ip at boot time.
2227 		 */
2228 		if ((ipst = ns->netstack_ip) == NULL) {
2229 			netstack_rele(ns);
2230 			continue;
2231 		}
2232 		ip_ire_reclaim_stack(ipst);
2233 		netstack_rele(ns);
2234 	}
2235 	netstack_next_fini(&nh);
2236 }
2237 
2238 static void
2239 power2_roundup(uint32_t *value)
2240 {
2241 	int i;
2242 
2243 	for (i = 1; i < 31; i++) {
2244 		if (*value <= (1 << i))
2245 			break;
2246 	}
2247 	*value = (1 << i);
2248 }
2249 
2250 /* Global init for all zones */
2251 void
2252 ip_ire_g_init()
2253 {
2254 	/*
2255 	 * Create kmem_caches.  ip_ire_reclaim() and ip_nce_reclaim()
2256 	 * will give disposable IREs back to system when needed.
2257 	 * This needs to be done here before anything else, since
2258 	 * ire_add() expects the cache to be created.
2259 	 */
2260 	ire_cache = kmem_cache_create("ire_cache",
2261 	    sizeof (ire_t), 0, NULL, NULL,
2262 	    ip_ire_reclaim, NULL, NULL, 0);
2263 
2264 	ncec_cache = kmem_cache_create("ncec_cache",
2265 	    sizeof (ncec_t), 0, NULL, NULL,
2266 	    ip_nce_reclaim, NULL, NULL, 0);
2267 	nce_cache = kmem_cache_create("nce_cache",
2268 	    sizeof (nce_t), 0, NULL, NULL,
2269 	    NULL, NULL, NULL, 0);
2270 
2271 	rt_entry_cache = kmem_cache_create("rt_entry",
2272 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
2273 
2274 	/*
2275 	 * Have radix code setup kmem caches etc.
2276 	 */
2277 	rn_init();
2278 }
2279 
2280 void
2281 ip_ire_init(ip_stack_t *ipst)
2282 {
2283 	ire_t	*ire;
2284 	int	error;
2285 
2286 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
2287 
2288 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
2289 
2290 	/*
2291 	 * Make sure that the forwarding table size is a power of 2.
2292 	 * The IRE*_ADDR_HASH() macroes depend on that.
2293 	 */
2294 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
2295 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
2296 
2297 	/*
2298 	 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6.
2299 	 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has
2300 	 * RTF_BLACKHOLE set. We use the latter for transient errors such
2301 	 * as memory allocation failures and tripping on IRE_IS_CONDEMNED
2302 	 * entries.
2303 	 */
2304 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2305 	*ire = ire_null;
2306 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2307 	    RTF_REJECT|RTF_UP, NULL, ipst);
2308 	ASSERT(error == 0);
2309 	ipst->ips_ire_reject_v4 = ire;
2310 
2311 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2312 	*ire = ire_null;
2313 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2314 	    RTF_REJECT|RTF_UP, NULL, ipst);
2315 	ASSERT(error == 0);
2316 	ipst->ips_ire_reject_v6 = ire;
2317 
2318 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2319 	*ire = ire_null;
2320 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2321 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
2322 	ASSERT(error == 0);
2323 	ipst->ips_ire_blackhole_v4 = ire;
2324 
2325 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2326 	*ire = ire_null;
2327 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2328 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
2329 	ASSERT(error == 0);
2330 	ipst->ips_ire_blackhole_v6 = ire;
2331 
2332 	rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL);
2333 	rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL);
2334 }
2335 
2336 void
2337 ip_ire_g_fini(void)
2338 {
2339 	kmem_cache_destroy(ire_cache);
2340 	kmem_cache_destroy(ncec_cache);
2341 	kmem_cache_destroy(nce_cache);
2342 	kmem_cache_destroy(rt_entry_cache);
2343 
2344 	rn_fini();
2345 }
2346 
2347 void
2348 ip_ire_fini(ip_stack_t *ipst)
2349 {
2350 	int i;
2351 
2352 	rw_destroy(&ipst->ips_ire_dep_lock);
2353 	rw_destroy(&ipst->ips_ip6_ire_head_lock);
2354 
2355 	ire_make_condemned(ipst->ips_ire_reject_v6);
2356 	ire_refrele_notr(ipst->ips_ire_reject_v6);
2357 	ipst->ips_ire_reject_v6 = NULL;
2358 
2359 	ire_make_condemned(ipst->ips_ire_reject_v4);
2360 	ire_refrele_notr(ipst->ips_ire_reject_v4);
2361 	ipst->ips_ire_reject_v4 = NULL;
2362 
2363 	ire_make_condemned(ipst->ips_ire_blackhole_v6);
2364 	ire_refrele_notr(ipst->ips_ire_blackhole_v6);
2365 	ipst->ips_ire_blackhole_v6 = NULL;
2366 
2367 	ire_make_condemned(ipst->ips_ire_blackhole_v4);
2368 	ire_refrele_notr(ipst->ips_ire_blackhole_v4);
2369 	ipst->ips_ire_blackhole_v4 = NULL;
2370 
2371 	/*
2372 	 * Delete all IREs - assumes that the ill/ipifs have
2373 	 * been removed so what remains are just the ftable to handle.
2374 	 */
2375 	ire_walk(ire_delete, NULL, ipst);
2376 
2377 	rn_freehead(ipst->ips_ip_ftable);
2378 	ipst->ips_ip_ftable = NULL;
2379 
2380 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
2381 
2382 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
2383 		irb_t *ptr;
2384 		int j;
2385 
2386 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
2387 			continue;
2388 
2389 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
2390 			ASSERT(ptr[j].irb_ire == NULL);
2391 			rw_destroy(&ptr[j].irb_lock);
2392 		}
2393 		mi_free(ptr);
2394 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
2395 	}
2396 }
2397 
2398 #ifdef DEBUG
2399 void
2400 ire_trace_ref(ire_t *ire)
2401 {
2402 	mutex_enter(&ire->ire_lock);
2403 	if (ire->ire_trace_disable) {
2404 		mutex_exit(&ire->ire_lock);
2405 		return;
2406 	}
2407 
2408 	if (th_trace_ref(ire, ire->ire_ipst)) {
2409 		mutex_exit(&ire->ire_lock);
2410 	} else {
2411 		ire->ire_trace_disable = B_TRUE;
2412 		mutex_exit(&ire->ire_lock);
2413 		ire_trace_cleanup(ire);
2414 	}
2415 }
2416 
2417 void
2418 ire_untrace_ref(ire_t *ire)
2419 {
2420 	mutex_enter(&ire->ire_lock);
2421 	if (!ire->ire_trace_disable)
2422 		th_trace_unref(ire);
2423 	mutex_exit(&ire->ire_lock);
2424 }
2425 
2426 static void
2427 ire_trace_cleanup(const ire_t *ire)
2428 {
2429 	th_trace_cleanup(ire, ire->ire_trace_disable);
2430 }
2431 #endif /* DEBUG */
2432 
2433 /*
2434  * Find, or create if needed, the nce_t pointer to the neighbor cache
2435  * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
2436  * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
2437  * on the next available under-ill (selected by the IPMP rotor) in the
2438  * unicast IPMP case.
2439  *
2440  * If a neighbor-cache entry has to be created (i.e., one does not already
2441  * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
2442  * entry are initialized in nce_add_v4(). The broadcast, multicast, and
2443  * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
2444  * the ncec_t created. The ncec_lladdr is non-null for all link types with
2445  * non-zero ill_phys_addr_length, though the contents may be zero in cases
2446  * where the link-layer type is not known at the time of creation
2447  * (e.g., IRE_IFRESOLVER links)
2448  *
2449  * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
2450  * has the physical broadcast address of the outgoing interface.
2451  * For unicast ire entries,
2452  *   - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
2453  *     ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
2454  *   - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
2455  *     layer resolution is necessary, so that the ncec_t will be in the
2456  *     ND_REACHABLE state
2457  *
2458  * The link layer information needed for broadcast addresses, and for
2459  * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
2460  * never needs re-verification for the lifetime of the ncec_t. These are
2461  * therefore marked NCE_F_NONUD.
2462  *
2463  * The nce returned will be created such that the nce_ill == ill that
2464  * is passed in. Note that the nce itself may not have ncec_ill == ill
2465  * where IPMP links are involved.
2466  */
2467 static nce_t *
2468 ire_nce_init(ill_t *ill, const void *addr, int ire_type)
2469 {
2470 	int		err;
2471 	nce_t		*nce = NULL;
2472 	uint16_t	ncec_flags;
2473 	uchar_t		*hwaddr;
2474 	boolean_t	need_refrele = B_FALSE;
2475 	ill_t		*in_ill = ill;
2476 	boolean_t	is_unicast;
2477 	uint_t		hwaddr_len;
2478 
2479 	is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
2480 	if (IS_IPMP(ill) ||
2481 	    ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
2482 		if ((ill = ipmp_ill_get_xmit_ill(ill, is_unicast)) == NULL)
2483 			return (NULL);
2484 		need_refrele = B_TRUE;
2485 	}
2486 	ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
2487 
2488 	switch (ire_type) {
2489 	case IRE_BROADCAST:
2490 		ASSERT(!ill->ill_isv6);
2491 		ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
2492 		break;
2493 	case IRE_MULTICAST:
2494 		ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
2495 		break;
2496 	}
2497 
2498 	if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
2499 		hwaddr = ill->ill_dest_addr;
2500 	} else {
2501 		hwaddr = NULL;
2502 	}
2503 	hwaddr_len = ill->ill_phys_addr_length;
2504 
2505 retry:
2506 	/* nce_state will be computed by nce_add_common() */
2507 	if (!ill->ill_isv6) {
2508 		err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
2509 		    ncec_flags, ND_UNCHANGED, &nce);
2510 	} else {
2511 		err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
2512 		    ncec_flags, ND_UNCHANGED, &nce);
2513 	}
2514 
2515 	switch (err) {
2516 	case 0:
2517 		break;
2518 	case EEXIST:
2519 		/*
2520 		 * When subnets change or partially overlap what was once
2521 		 * a broadcast address could now be a unicast, or vice versa.
2522 		 */
2523 		if (((ncec_flags ^ nce->nce_common->ncec_flags) &
2524 		    NCE_F_BCAST) != 0) {
2525 			ASSERT(!ill->ill_isv6);
2526 			ncec_delete(nce->nce_common);
2527 			nce_refrele(nce);
2528 			goto retry;
2529 		}
2530 		break;
2531 	default:
2532 		DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
2533 		if (need_refrele)
2534 			ill_refrele(ill);
2535 		return (NULL);
2536 	}
2537 	/*
2538 	 * If the ill was an under-ill of an IPMP group, we need to verify
2539 	 * that it is still active so that we select an active interface in
2540 	 * the group. However, since ipmp_ill_is_active ASSERTs for
2541 	 * IS_UNDER_IPMP(), we first need to verify that the ill is an
2542 	 * under-ill, and since this is being done in the data path, the
2543 	 * only way to ascertain this is by holding the ill_g_lock.
2544 	 */
2545 	rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
2546 	mutex_enter(&ill->ill_lock);
2547 	mutex_enter(&ill->ill_phyint->phyint_lock);
2548 	if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
2549 		/*
2550 		 * need_refrele implies that the under ill was selected by
2551 		 * ipmp_ill_get_xmit_ill() because either the in_ill was an
2552 		 * ipmp_ill, or we are sending a non-unicast packet on
2553 		 * an under_ill. However, when we get here, the ill selected by
2554 		 * ipmp_ill_get_xmit_ill  was pulled out of the active set
2555 		 * (for unicast)  or cast_ill nomination (for
2556 		 * !unicast) after it was  picked as the outgoing ill.
2557 		 * We have to pick an active interface and/or cast_ill in the
2558 		 * group.
2559 		 */
2560 		mutex_exit(&ill->ill_phyint->phyint_lock);
2561 		nce_delete(nce);
2562 		mutex_exit(&ill->ill_lock);
2563 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2564 		nce_refrele(nce);
2565 		ill_refrele(ill);
2566 		if ((ill = ipmp_ill_get_xmit_ill(in_ill, is_unicast)) == NULL)
2567 			return (NULL);
2568 		goto retry;
2569 	} else {
2570 		mutex_exit(&ill->ill_phyint->phyint_lock);
2571 		mutex_exit(&ill->ill_lock);
2572 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2573 	}
2574 done:
2575 	ASSERT(nce->nce_ill == ill);
2576 	if (need_refrele)
2577 		ill_refrele(ill);
2578 	return (nce);
2579 }
2580 
2581 nce_t *
2582 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
2583 {
2584 	return (ire_nce_init(ill, &addr4, ire_type));
2585 }
2586 
2587 nce_t *
2588 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
2589 {
2590 	ASSERT((ire_type & IRE_BROADCAST) == 0);
2591 	return (ire_nce_init(ill, addr6, ire_type));
2592 }
2593 
2594 /*
2595  * The caller should hold irb_lock as a writer if the ire is in a bucket.
2596  * This routine will clear ire_nce_cache, and we make sure that we can never
2597  * set ire_nce_cache after the ire is marked condemned.
2598  */
2599 void
2600 ire_make_condemned(ire_t *ire)
2601 {
2602 	ip_stack_t	*ipst = ire->ire_ipst;
2603 	nce_t		*nce;
2604 
2605 	mutex_enter(&ire->ire_lock);
2606 	ASSERT(ire->ire_bucket == NULL ||
2607 	    RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
2608 	ASSERT(!IRE_IS_CONDEMNED(ire));
2609 	ire->ire_generation = IRE_GENERATION_CONDEMNED;
2610 	/* Count how many condemned ires for kmem_cache callback */
2611 	atomic_add_32(&ipst->ips_num_ire_condemned, 1);
2612 	nce = ire->ire_nce_cache;
2613 	ire->ire_nce_cache = NULL;
2614 	mutex_exit(&ire->ire_lock);
2615 	if (nce != NULL)
2616 		nce_refrele(nce);
2617 }
2618 
2619 /*
2620  * Increment the generation avoiding the special condemned value
2621  */
2622 void
2623 ire_increment_generation(ire_t *ire)
2624 {
2625 	uint_t generation;
2626 
2627 	mutex_enter(&ire->ire_lock);
2628 	/*
2629 	 * Even though the caller has a hold it can't prevent a concurrent
2630 	 * ire_delete marking the IRE condemned
2631 	 */
2632 	if (!IRE_IS_CONDEMNED(ire)) {
2633 		generation = ire->ire_generation + 1;
2634 		if (generation == IRE_GENERATION_CONDEMNED)
2635 			generation = IRE_GENERATION_INITIAL;
2636 		ASSERT(generation != IRE_GENERATION_VERIFY);
2637 		ire->ire_generation = generation;
2638 	}
2639 	mutex_exit(&ire->ire_lock);
2640 }
2641 
2642 /*
2643  * Increment ire_generation on all the IRE_MULTICASTs
2644  * Used when the default multicast interface (as determined by
2645  * ill_lookup_multicast) might have changed.
2646  *
2647  * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
2648  * ill unplumb.
2649  */
2650 void
2651 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
2652 {
2653 	ill_t	*ill;
2654 	ill_walk_context_t ctx;
2655 
2656 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2657 	if (isv6)
2658 		ill = ILL_START_WALK_V6(&ctx, ipst);
2659 	else
2660 		ill = ILL_START_WALK_V4(&ctx, ipst);
2661 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2662 		if (ILL_IS_CONDEMNED(ill))
2663 			continue;
2664 		if (ill->ill_ire_multicast != NULL)
2665 			ire_increment_generation(ill->ill_ire_multicast);
2666 	}
2667 	rw_exit(&ipst->ips_ill_g_lock);
2668 }
2669 
2670 /*
2671  * Return a held IRE_NOROUTE with RTF_REJECT set
2672  */
2673 ire_t *
2674 ire_reject(ip_stack_t *ipst, boolean_t isv6)
2675 {
2676 	ire_t *ire;
2677 
2678 	if (isv6)
2679 		ire = ipst->ips_ire_reject_v6;
2680 	else
2681 		ire = ipst->ips_ire_reject_v4;
2682 
2683 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2684 	ire_refhold(ire);
2685 	return (ire);
2686 }
2687 
2688 /*
2689  * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
2690  */
2691 ire_t *
2692 ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
2693 {
2694 	ire_t *ire;
2695 
2696 	if (isv6)
2697 		ire = ipst->ips_ire_blackhole_v6;
2698 	else
2699 		ire = ipst->ips_ire_blackhole_v4;
2700 
2701 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2702 	ire_refhold(ire);
2703 	return (ire);
2704 }
2705 
2706 /*
2707  * Return a held IRE_MULTICAST.
2708  */
2709 ire_t *
2710 ire_multicast(ill_t *ill)
2711 {
2712 	ire_t *ire = ill->ill_ire_multicast;
2713 
2714 	ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
2715 	if (ire == NULL)
2716 		ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
2717 	else
2718 		ire_refhold(ire);
2719 	return (ire);
2720 }
2721 
2722 /*
2723  * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
2724  * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
2725  * This can return an RTF_REJECT|RTF_BLACKHOLE.
2726  * The returned IRE is held.
2727  * The assumption is that ip_select_route() has been called and returned the
2728  * IRE (thus ip_select_route would have set up the ire_dep* information.)
2729  * If some IRE is deleteted then ire_dep_remove() will have been called and
2730  * we might not find a nexthop IRE, in which case we return NULL.
2731  */
2732 ire_t *
2733 ire_nexthop(ire_t *ire)
2734 {
2735 	ip_stack_t	*ipst = ire->ire_ipst;
2736 
2737 	/* Acquire lock to walk ire_dep_parent */
2738 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
2739 	while (ire != NULL) {
2740 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2741 			goto done;
2742 		}
2743 		/*
2744 		 * If we find an IRE_ONLINK we are done. This includes
2745 		 * the case of IRE_MULTICAST.
2746 		 * Note that in order to send packets we need a host-specific
2747 		 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
2748 		 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
2749 		 * was not host specific.
2750 		 * However, ip_rts_request doesn't want to send packets
2751 		 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
2752 		 * it needs an IRE_IF_ALL to get to the ill. Thus
2753 		 * we return IRE_IF_ALL that are not host specific here.
2754 		 */
2755 		if (ire->ire_type & IRE_ONLINK)
2756 			goto done;
2757 		ire = ire->ire_dep_parent;
2758 	}
2759 	rw_exit(&ipst->ips_ire_dep_lock);
2760 	return (NULL);
2761 
2762 done:
2763 	ire_refhold(ire);
2764 	rw_exit(&ipst->ips_ire_dep_lock);
2765 	return (ire);
2766 }
2767 
2768 /*
2769  * Find the ill used to send packets. This will be NULL in case
2770  * of a reject or blackhole.
2771  * The returned ill is held; caller needs to do ill_refrele when done.
2772  */
2773 ill_t *
2774 ire_nexthop_ill(ire_t *ire)
2775 {
2776 	ill_t		*ill;
2777 
2778 	ire = ire_nexthop(ire);
2779 	if (ire == NULL)
2780 		return (NULL);
2781 
2782 	/* ire_ill can not change for an existing ire */
2783 	ill = ire->ire_ill;
2784 	if (ill != NULL)
2785 		ill_refhold(ill);
2786 	ire_refrele(ire);
2787 	return (ill);
2788 }
2789 
2790 #ifdef DEBUG
2791 static boolean_t
2792 parent_has_child(ire_t *parent, ire_t *child)
2793 {
2794 	ire_t	*ire;
2795 	ire_t	*prev;
2796 
2797 	ire = parent->ire_dep_children;
2798 	prev = NULL;
2799 	while (ire != NULL) {
2800 		if (prev == NULL) {
2801 			ASSERT(ire->ire_dep_sib_ptpn ==
2802 			    &(parent->ire_dep_children));
2803 		} else {
2804 			ASSERT(ire->ire_dep_sib_ptpn ==
2805 			    &(prev->ire_dep_sib_next));
2806 		}
2807 		if (ire == child)
2808 			return (B_TRUE);
2809 		prev = ire;
2810 		ire = ire->ire_dep_sib_next;
2811 	}
2812 	return (B_FALSE);
2813 }
2814 
2815 static void
2816 ire_dep_verify(ire_t *ire)
2817 {
2818 	ire_t		*parent = ire->ire_dep_parent;
2819 	ire_t		*child = ire->ire_dep_children;
2820 
2821 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
2822 	    ire->ire_ipversion == IPV6_VERSION);
2823 	if (parent != NULL) {
2824 		ASSERT(parent->ire_ipversion == IPV4_VERSION ||
2825 		    parent->ire_ipversion == IPV6_VERSION);
2826 		ASSERT(parent->ire_refcnt >= 1);
2827 		ASSERT(parent_has_child(parent, ire));
2828 	}
2829 	if (child != NULL) {
2830 		ASSERT(child->ire_ipversion == IPV4_VERSION ||
2831 		    child->ire_ipversion == IPV6_VERSION);
2832 		ASSERT(child->ire_dep_parent == ire);
2833 		ASSERT(child->ire_dep_sib_ptpn != NULL);
2834 		ASSERT(parent_has_child(ire, child));
2835 	}
2836 }
2837 #endif /* DEBUG */
2838 
2839 /*
2840  * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
2841  */
2842 void
2843 ire_dep_remove(ire_t *ire)
2844 {
2845 	ip_stack_t	*ipst = ire->ire_ipst;
2846 	ire_t		*parent = ire->ire_dep_parent;
2847 	ire_t		*next;
2848 	nce_t		*nce;
2849 
2850 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2851 	ASSERT(ire->ire_dep_parent != NULL);
2852 	ASSERT(ire->ire_dep_sib_ptpn != NULL);
2853 
2854 #ifdef DEBUG
2855 	ire_dep_verify(ire);
2856 	ire_dep_verify(parent);
2857 #endif
2858 
2859 	next = ire->ire_dep_sib_next;
2860 	if (next != NULL)
2861 		next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
2862 
2863 	ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
2864 	*(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
2865 
2866 	ire->ire_dep_sib_ptpn = NULL;
2867 	ire->ire_dep_sib_next = NULL;
2868 
2869 	mutex_enter(&ire->ire_lock);
2870 	parent = ire->ire_dep_parent;
2871 	ire->ire_dep_parent = NULL;
2872 	mutex_exit(&ire->ire_lock);
2873 
2874 	/*
2875 	 * Make sure all our children, grandchildren, etc set
2876 	 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
2877 	 * we can no longer guarantee than the children have a current
2878 	 * ire_nce_cache and ire_nexthop_ill().
2879 	 */
2880 	if (ire->ire_dep_children != NULL)
2881 		ire_dep_invalidate_children(ire->ire_dep_children);
2882 
2883 	/*
2884 	 * Since the parent is gone we make sure we clear ire_nce_cache.
2885 	 * We can clear it under ire_lock even if the IRE is used
2886 	 */
2887 	mutex_enter(&ire->ire_lock);
2888 	nce = ire->ire_nce_cache;
2889 	ire->ire_nce_cache = NULL;
2890 	mutex_exit(&ire->ire_lock);
2891 	if (nce != NULL)
2892 		nce_refrele(nce);
2893 
2894 #ifdef DEBUG
2895 	ire_dep_verify(ire);
2896 	ire_dep_verify(parent);
2897 #endif
2898 
2899 	ire_refrele_notr(parent);
2900 	ire_refrele_notr(ire);
2901 }
2902 
2903 /*
2904  * Insert the child in the linkage of the parent
2905  */
2906 static void
2907 ire_dep_parent_insert(ire_t *child, ire_t *parent)
2908 {
2909 	ip_stack_t	*ipst = child->ire_ipst;
2910 	ire_t		*next;
2911 
2912 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2913 	ASSERT(child->ire_dep_parent == NULL);
2914 
2915 #ifdef DEBUG
2916 	ire_dep_verify(child);
2917 	ire_dep_verify(parent);
2918 #endif
2919 	/* No parents => no siblings */
2920 	ASSERT(child->ire_dep_sib_ptpn == NULL);
2921 	ASSERT(child->ire_dep_sib_next == NULL);
2922 
2923 	ire_refhold_notr(parent);
2924 	ire_refhold_notr(child);
2925 
2926 	/* Head insertion */
2927 	next = parent->ire_dep_children;
2928 	if (next != NULL) {
2929 		ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
2930 		child->ire_dep_sib_next = next;
2931 		next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
2932 	}
2933 	parent->ire_dep_children = child;
2934 	child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
2935 
2936 	mutex_enter(&child->ire_lock);
2937 	child->ire_dep_parent = parent;
2938 	mutex_exit(&child->ire_lock);
2939 
2940 #ifdef DEBUG
2941 	ire_dep_verify(child);
2942 	ire_dep_verify(parent);
2943 #endif
2944 }
2945 
2946 
2947 /*
2948  * Given count worth of ires and generations, build ire_dep_* relationships
2949  * from ires[0] to ires[count-1]. Record generations[i+1] in
2950  * ire_dep_parent_generation for ires[i].
2951  * We graft onto an existing parent chain by making sure that we don't
2952  * touch ire_dep_parent for ires[count-1].
2953  *
2954  * We check for any condemned ire_generation count and return B_FALSE in
2955  * that case so that the caller can tear it apart.
2956  *
2957  * Note that generations[0] is not used. Caller handles that.
2958  */
2959 boolean_t
2960 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
2961 {
2962 	ire_t		*ire = ires[0];
2963 	ip_stack_t	*ipst;
2964 	uint_t		i;
2965 
2966 	ASSERT(count > 0);
2967 	if (count == 1) {
2968 		/* No work to do */
2969 		return (B_TRUE);
2970 	}
2971 	ipst = ire->ire_ipst;
2972 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2973 	/*
2974 	 * Do not remove the linkage for any existing parent chain i.e.,
2975 	 * ires[count-1] is left alone.
2976 	 */
2977 	for (i = 0; i < count-1; i++) {
2978 		/* Remove existing parent if we need to change it */
2979 		if (ires[i]->ire_dep_parent != NULL &&
2980 		    ires[i]->ire_dep_parent != ires[i+1])
2981 			ire_dep_remove(ires[i]);
2982 	}
2983 
2984 	for (i = 0; i < count - 1; i++) {
2985 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2986 		    ires[i]->ire_ipversion == IPV6_VERSION);
2987 		/* Does it need to change? */
2988 		if (ires[i]->ire_dep_parent != ires[i+1])
2989 			ire_dep_parent_insert(ires[i], ires[i+1]);
2990 
2991 		mutex_enter(&ires[i+1]->ire_lock);
2992 		if (IRE_IS_CONDEMNED(ires[i+1])) {
2993 			mutex_exit(&ires[i+1]->ire_lock);
2994 			rw_exit(&ipst->ips_ire_dep_lock);
2995 			return (B_FALSE);
2996 		}
2997 		mutex_exit(&ires[i+1]->ire_lock);
2998 
2999 		mutex_enter(&ires[i]->ire_lock);
3000 		ires[i]->ire_dep_parent_generation = generations[i+1];
3001 		mutex_exit(&ires[i]->ire_lock);
3002 	}
3003 	rw_exit(&ipst->ips_ire_dep_lock);
3004 	return (B_TRUE);
3005 }
3006 
3007 /*
3008  * Given count worth of ires, unbuild ire_dep_* relationships
3009  * from ires[0] to ires[count-1].
3010  */
3011 void
3012 ire_dep_unbuild(ire_t *ires[], uint_t count)
3013 {
3014 	ip_stack_t	*ipst;
3015 	uint_t		i;
3016 
3017 	if (count == 0) {
3018 		/* No work to do */
3019 		return;
3020 	}
3021 	ipst = ires[0]->ire_ipst;
3022 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
3023 	for (i = 0; i < count; i++) {
3024 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
3025 		    ires[i]->ire_ipversion == IPV6_VERSION);
3026 		if (ires[i]->ire_dep_parent != NULL)
3027 			ire_dep_remove(ires[i]);
3028 		mutex_enter(&ires[i]->ire_lock);
3029 		ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3030 		mutex_exit(&ires[i]->ire_lock);
3031 	}
3032 	rw_exit(&ipst->ips_ire_dep_lock);
3033 }
3034 
3035 /*
3036  * Both the forwarding and the outbound code paths can trip on
3037  * a condemned NCE, in which case we call this function.
3038  * We have two different behaviors: if the NCE was UNREACHABLE
3039  * it is an indication that something failed. In that case
3040  * we see if we should look for a different IRE (for example,
3041  * delete any matching redirect IRE, or try a different
3042  * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
3043  * different IRE will be picked next time we send/forward.
3044  *
3045  * If we are called by the output path then fail_if_better is set
3046  * and we return NULL if there could be a better IRE. This is because the
3047  * output path retries the IRE lookup. (The input/forward path can not retry.)
3048  *
3049  * If the NCE was not unreachable then we pick/allocate a
3050  * new (most likely ND_INITIAL) NCE and proceed with it.
3051  *
3052  * ipha/ip6h are needed for multicast packets; ipha needs to be
3053  * set for IPv4 and ip6h needs to be set for IPv6 packets.
3054  */
3055 nce_t *
3056 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
3057     boolean_t fail_if_better)
3058 {
3059 	if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
3060 		if (ire_no_good(ire) && fail_if_better) {
3061 			/*
3062 			 * Did some changes, or ECMP likely to exist.
3063 			 * Make ip_output look for a different IRE
3064 			 */
3065 			return (NULL);
3066 		}
3067 	}
3068 	if (ire_revalidate_nce(ire) == ENETUNREACH) {
3069 		/* The ire_dep_parent chain went bad, or no memory? */
3070 		(void) ire_no_good(ire);
3071 		return (NULL);
3072 	}
3073 	if (ire->ire_ipversion == IPV4_VERSION) {
3074 		ASSERT(ipha != NULL);
3075 		nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
3076 	} else {
3077 		ASSERT(ip6h != NULL);
3078 		nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
3079 	}
3080 
3081 	if (nce == NULL)
3082 		return (NULL);
3083 	if (nce->nce_is_condemned) {
3084 		nce_refrele(nce);
3085 		return (NULL);
3086 	}
3087 	return (nce);
3088 }
3089 
3090 /*
3091  * The caller has found that the ire is bad, either due to a reference to an NCE
3092  * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved.
3093  * We update things so a subsequent attempt to send to the destination
3094  * is likely to find different IRE, or that a new NCE would be created.
3095  *
3096  * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
3097  * find a different route (either due to having deleted a redirect, or there
3098  * being ECMP routes.)
3099  *
3100  * If we have a redirect (RTF_DYNAMIC) we delete it.
3101  * Otherwise we increment ire_badcnt and increment the generation number so
3102  * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
3103  * into account in the route selection when we have multiple choices (multiple
3104  * default routes or ECMP in general).
3105  * Any time ip_select_route find an ire with a condemned ire_nce_cache
3106  * (e.g., if no equal cost route to the bad one) ip_select_route will make
3107  * sure the NCE is revalidated to avoid getting stuck on a
3108  * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
3109  */
3110 boolean_t
3111 ire_no_good(ire_t *ire)
3112 {
3113 	ip_stack_t	*ipst = ire->ire_ipst;
3114 	ire_t		*ire2;
3115 	nce_t		*nce;
3116 
3117 	if (ire->ire_flags & RTF_DYNAMIC) {
3118 		ire_delete(ire);
3119 		return (B_TRUE);
3120 	}
3121 	if (ire->ire_flags & RTF_INDIRECT) {
3122 		/* Check if next IRE is a redirect */
3123 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3124 		if (ire->ire_dep_parent != NULL &&
3125 		    (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
3126 			ire2 = ire->ire_dep_parent;
3127 			ire_refhold(ire2);
3128 		} else {
3129 			ire2 = NULL;
3130 		}
3131 		rw_exit(&ipst->ips_ire_dep_lock);
3132 		if (ire2 != NULL) {
3133 			ire_delete(ire2);
3134 			ire_refrele(ire2);
3135 			return (B_TRUE);
3136 		}
3137 	}
3138 	/*
3139 	 * No redirect involved. Increment badcnt so that if we have ECMP
3140 	 * routes we are likely to pick a different one for the next packet.
3141 	 *
3142 	 * If the NCE is unreachable and condemned we should drop the reference
3143 	 * to it so that a new NCE can be created.
3144 	 *
3145 	 * Finally we increment the generation number so that any ixa_ire
3146 	 * cache will be revalidated.
3147 	 */
3148 	mutex_enter(&ire->ire_lock);
3149 	ire->ire_badcnt++;
3150 	ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
3151 	nce = ire->ire_nce_cache;
3152 	if (nce != NULL && nce->nce_is_condemned &&
3153 	    nce->nce_common->ncec_state == ND_UNREACHABLE)
3154 		ire->ire_nce_cache = NULL;
3155 	else
3156 		nce = NULL;
3157 	mutex_exit(&ire->ire_lock);
3158 	if (nce != NULL)
3159 		nce_refrele(nce);
3160 
3161 	ire_increment_generation(ire);
3162 	ire_dep_incr_generation(ire);
3163 
3164 	return (ire->ire_bucket->irb_ire_cnt > 1);
3165 }
3166 
3167 /*
3168  * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
3169  * ire_dep_parent_generation.
3170  * If they all match we just return ire_generation from the topmost IRE.
3171  * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
3172  * above the mismatch to IRE_GENERATION_VERIFY and also returning
3173  * IRE_GENERATION_VERIFY.
3174  */
3175 uint_t
3176 ire_dep_validate_generations(ire_t *ire)
3177 {
3178 	ip_stack_t	*ipst = ire->ire_ipst;
3179 	uint_t		generation;
3180 	ire_t		*ire1;
3181 
3182 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3183 	generation = ire->ire_generation;	/* Assuming things match */
3184 	for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
3185 		ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
3186 		    ire1->ire_ipversion == IPV6_VERSION);
3187 		if (ire1->ire_dep_parent == NULL)
3188 			break;
3189 		if (ire1->ire_dep_parent_generation !=
3190 		    ire1->ire_dep_parent->ire_generation)
3191 			goto mismatch;
3192 	}
3193 	rw_exit(&ipst->ips_ire_dep_lock);
3194 	return (generation);
3195 
3196 mismatch:
3197 	generation = IRE_GENERATION_VERIFY;
3198 	/* Fill from top down to the mismatch with _VERIFY */
3199 	while (ire != ire1) {
3200 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3201 		    ire->ire_ipversion == IPV6_VERSION);
3202 		mutex_enter(&ire->ire_lock);
3203 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3204 		mutex_exit(&ire->ire_lock);
3205 		ire = ire->ire_dep_parent;
3206 	}
3207 	rw_exit(&ipst->ips_ire_dep_lock);
3208 	return (generation);
3209 }
3210 
3211 /*
3212  * Used when we need to return an ire with ire_dep_parent, but we
3213  * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
3214  * Using IRE_GENERATION_VERIFY means that next time we'll redo the
3215  * recursive lookup.
3216  */
3217 void
3218 ire_dep_invalidate_generations(ire_t *ire)
3219 {
3220 	ip_stack_t	*ipst = ire->ire_ipst;
3221 
3222 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3223 	while (ire != NULL) {
3224 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3225 		    ire->ire_ipversion == IPV6_VERSION);
3226 		mutex_enter(&ire->ire_lock);
3227 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3228 		mutex_exit(&ire->ire_lock);
3229 		ire = ire->ire_dep_parent;
3230 	}
3231 	rw_exit(&ipst->ips_ire_dep_lock);
3232 }
3233 
3234 /* Set _VERIFY ire_dep_parent_generation for all children recursively */
3235 static void
3236 ire_dep_invalidate_children(ire_t *child)
3237 {
3238 	ip_stack_t	*ipst = child->ire_ipst;
3239 
3240 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
3241 	/* Depth first */
3242 	if (child->ire_dep_children != NULL)
3243 		ire_dep_invalidate_children(child->ire_dep_children);
3244 
3245 	while (child != NULL) {
3246 		mutex_enter(&child->ire_lock);
3247 		child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3248 		mutex_exit(&child->ire_lock);
3249 		child = child->ire_dep_sib_next;
3250 	}
3251 }
3252 
3253 static void
3254 ire_dep_increment_children(ire_t *child)
3255 {
3256 	ip_stack_t	*ipst = child->ire_ipst;
3257 
3258 	ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
3259 	/* Depth first */
3260 	if (child->ire_dep_children != NULL)
3261 		ire_dep_increment_children(child->ire_dep_children);
3262 
3263 	while (child != NULL) {
3264 		if (!IRE_IS_CONDEMNED(child))
3265 			ire_increment_generation(child);
3266 		child = child->ire_dep_sib_next;
3267 	}
3268 }
3269 
3270 /*
3271  * Walk all the children of this ire recursively and increment their
3272  * generation number.
3273  */
3274 static void
3275 ire_dep_incr_generation_locked(ire_t *parent)
3276 {
3277 	ASSERT(RW_READ_HELD(&parent->ire_ipst->ips_ire_dep_lock));
3278 	if (parent->ire_dep_children != NULL)
3279 		ire_dep_increment_children(parent->ire_dep_children);
3280 }
3281 
3282 void
3283 ire_dep_incr_generation(ire_t *parent)
3284 {
3285 	ip_stack_t	*ipst = parent->ire_ipst;
3286 
3287 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3288 	ire_dep_incr_generation_locked(parent);
3289 	rw_exit(&ipst->ips_ire_dep_lock);
3290 }
3291 
3292 /*
3293  * Get a new ire_nce_cache for this IRE as well as its nexthop.
3294  * Returns zero if it succeeds. Can fail due to lack of memory or when
3295  * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
3296  * cases.
3297  *
3298  * In the in.mpathd case, the ire will have ire_testhidden
3299  * set; so we should create the ncec for the underlying ill.
3300  *
3301  * Note that the error returned by ire_revalidate_nce() is ignored by most
3302  * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
3303  * error to mark potentially bad ire's. For all the other callers, an
3304  * error return could indicate a transient condition like ENOMEM, or could
3305  * be the result of an interface that is going down/unplumbing. In the former
3306  * case (transient error), we would leave the old stale ire/ire_nce_cache
3307  * in place, and possibly use incorrect link-layer information to send packets
3308  * but would eventually recover. In the latter case (ill down/replumb),
3309  * ire_revalidate_nce() might return a condemned nce back, but we would then
3310  * recover in the packet output path.
3311  */
3312 int
3313 ire_revalidate_nce(ire_t *ire)
3314 {
3315 	nce_t		*nce, *old_nce;
3316 	ire_t		*nexthop;
3317 
3318 	/*
3319 	 * For multicast we conceptually have an NCE but we don't store it
3320 	 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
3321 	 */
3322 	if (ire->ire_type & IRE_MULTICAST)
3323 		return (0);
3324 
3325 	/* ire_testhidden should only be set on under-interfaces */
3326 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3327 
3328 	nexthop = ire_nexthop(ire);
3329 	if (nexthop == NULL) {
3330 		/* The route is potentially bad */
3331 		(void) ire_no_good(ire);
3332 		return (ENETUNREACH);
3333 	}
3334 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3335 		ASSERT(ire->ire_ill != NULL);
3336 
3337 		if (ire->ire_ipversion == IPV4_VERSION)
3338 			nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
3339 		else
3340 			nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
3341 	} else {
3342 		ASSERT(nexthop->ire_type & IRE_ONLINK);
3343 		if (ire->ire_ipversion == IPV4_VERSION) {
3344 			nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
3345 			    nexthop->ire_type);
3346 		} else {
3347 			nce = ndp_nce_init(nexthop->ire_ill,
3348 			    &nexthop->ire_addr_v6, nexthop->ire_type);
3349 		}
3350 	}
3351 	if (nce == NULL) {
3352 		/*
3353 		 * Leave the old stale one in place to avoid a NULL
3354 		 * ire_nce_cache.
3355 		 */
3356 		ire_refrele(nexthop);
3357 		return (ENOMEM);
3358 	}
3359 
3360 	if (nexthop != ire) {
3361 		/* Update the nexthop ire */
3362 		mutex_enter(&nexthop->ire_lock);
3363 		old_nce = nexthop->ire_nce_cache;
3364 		if (!IRE_IS_CONDEMNED(nexthop)) {
3365 			nce_refhold(nce);
3366 			nexthop->ire_nce_cache = nce;
3367 		} else {
3368 			nexthop->ire_nce_cache = NULL;
3369 		}
3370 		mutex_exit(&nexthop->ire_lock);
3371 		if (old_nce != NULL)
3372 			nce_refrele(old_nce);
3373 	}
3374 	ire_refrele(nexthop);
3375 
3376 	mutex_enter(&ire->ire_lock);
3377 	old_nce = ire->ire_nce_cache;
3378 	if (!IRE_IS_CONDEMNED(ire)) {
3379 		nce_refhold(nce);
3380 		ire->ire_nce_cache = nce;
3381 	} else {
3382 		ire->ire_nce_cache = NULL;
3383 	}
3384 	mutex_exit(&ire->ire_lock);
3385 	if (old_nce != NULL)
3386 		nce_refrele(old_nce);
3387 
3388 	nce_refrele(nce);
3389 	return (0);
3390 }
3391 
3392 /*
3393  * Get a held nce for a given ire.
3394  * In the common case this is just from ire_nce_cache.
3395  * For IRE_MULTICAST this needs to do an explicit lookup since we do not
3396  * have an IRE_MULTICAST per address.
3397  * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
3398  * so they can check whether the NCE went unreachable (as opposed to was
3399  * condemned for some other reason).
3400  */
3401 nce_t *
3402 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
3403 {
3404 	nce_t	*nce;
3405 
3406 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
3407 		return (NULL);
3408 
3409 	/* ire_testhidden should only be set on under-interfaces */
3410 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3411 
3412 	mutex_enter(&ire->ire_lock);
3413 	nce = ire->ire_nce_cache;
3414 	if (nce != NULL) {
3415 		nce_refhold(nce);
3416 		mutex_exit(&ire->ire_lock);
3417 		return (nce);
3418 	}
3419 	mutex_exit(&ire->ire_lock);
3420 
3421 	if (ire->ire_type & IRE_MULTICAST) {
3422 		ASSERT(ire->ire_ill != NULL);
3423 
3424 		if (ire->ire_ipversion == IPV4_VERSION) {
3425 			ASSERT(v6nexthop == NULL);
3426 
3427 			nce = arp_nce_init(ire->ire_ill, v4nexthop,
3428 			    ire->ire_type);
3429 		} else {
3430 			ASSERT(v6nexthop != NULL);
3431 			ASSERT(v4nexthop == 0);
3432 			nce = ndp_nce_init(ire->ire_ill, v6nexthop,
3433 			    ire->ire_type);
3434 		}
3435 		return (nce);
3436 	}
3437 	return (NULL);
3438 }
3439 
3440 nce_t *
3441 ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
3442 {
3443 	ipha_t		*ipha;
3444 	ip6_t		*ip6h;
3445 
3446 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3447 		ipha = (ipha_t *)mp->b_rptr;
3448 		return (ire_to_nce(ire, ipha->ipha_dst, NULL));
3449 	} else {
3450 		ip6h = (ip6_t *)mp->b_rptr;
3451 		return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
3452 	}
3453 }
3454 
3455 /*
3456  * Given an IRE_INTERFACE (that matches more than one address) create
3457  * and return an IRE_IF_CLONE for the specific address.
3458  * Return the generation number.
3459  * Returns NULL is no memory for the IRE.
3460  * Handles both IPv4 and IPv6.
3461  */
3462 ire_t *
3463 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
3464 {
3465 	ire_t		*ire;
3466 	ire_t		*nire;
3467 
3468 	if (ire_if->ire_ipversion == IPV4_VERSION) {
3469 		ipaddr_t	v4addr;
3470 		ipaddr_t	mask = IP_HOST_MASK;
3471 
3472 		ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
3473 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
3474 
3475 		ire = ire_create(
3476 		    (uchar_t *)&v4addr,			/* dest address */
3477 		    (uchar_t *)&mask,			/* mask */
3478 		    (uchar_t *)&ire_if->ire_gateway_addr,
3479 		    IRE_IF_CLONE,			/* IRE type */
3480 		    ire_if->ire_ill,
3481 		    ire_if->ire_zoneid,
3482 		    ire_if->ire_flags | RTF_HOST,
3483 		    NULL,		/* No security attr for IRE_IF_ALL */
3484 		    ire_if->ire_ipst);
3485 	} else {
3486 		ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
3487 		ire = ire_create_v6(
3488 		    addr,				/* dest address */
3489 		    &ipv6_all_ones,			/* mask */
3490 		    &ire_if->ire_gateway_addr_v6,	/* gateway addr */
3491 		    IRE_IF_CLONE,			/* IRE type */
3492 		    ire_if->ire_ill,
3493 		    ire_if->ire_zoneid,
3494 		    ire_if->ire_flags | RTF_HOST,
3495 		    NULL,		/* No security attr for IRE_IF_ALL */
3496 		    ire_if->ire_ipst);
3497 	}
3498 	if (ire == NULL)
3499 		return (NULL);
3500 
3501 	/* Take the metrics, in particular the mtu, from the IRE_IF */
3502 	ire->ire_metrics = ire_if->ire_metrics;
3503 
3504 	nire = ire_add(ire);
3505 	if (nire == NULL) /* Some failure */
3506 		return (NULL);
3507 
3508 	if (generationp != NULL)
3509 		*generationp = nire->ire_generation;
3510 
3511 	/*
3512 	 * Make sure races don't add a duplicate by
3513 	 * catching the case when an identical was returned.
3514 	 */
3515 	if (nire != ire) {
3516 		ASSERT(nire->ire_identical_ref > 1);
3517 		ire_delete(nire);
3518 	}
3519 	return (nire);
3520 }
3521 
3522 /*
3523  * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
3524  * ire_dep_children (just walk the ire_dep_sib_next since they are all
3525  * immediate children.)
3526  * Since we hold a lock while we remove them we need to defer the actual
3527  * calls to ire_delete() until we have dropped the lock. This makes things
3528  * less efficient since we restart at the top after dropping the lock. But
3529  * we only run when an IRE_INTERFACE is deleted which is infrquent.
3530  *
3531  * Note that ire_dep_children can be any mixture of offlink routes and
3532  * IRE_IF_CLONE entries.
3533  */
3534 void
3535 ire_dep_delete_if_clone(ire_t *parent)
3536 {
3537 	ip_stack_t	*ipst = parent->ire_ipst;
3538 	ire_t		*child, *next;
3539 
3540 restart:
3541 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3542 	if (parent->ire_dep_children == NULL) {
3543 		rw_exit(&ipst->ips_ire_dep_lock);
3544 		return;
3545 	}
3546 	child = parent->ire_dep_children;
3547 	while (child != NULL) {
3548 		next = child->ire_dep_sib_next;
3549 		if ((child->ire_type & IRE_IF_CLONE) &&
3550 		    !IRE_IS_CONDEMNED(child)) {
3551 			ire_refhold(child);
3552 			rw_exit(&ipst->ips_ire_dep_lock);
3553 			ire_delete(child);
3554 			ASSERT(IRE_IS_CONDEMNED(child));
3555 			ire_refrele(child);
3556 			goto restart;
3557 		}
3558 		child = next;
3559 	}
3560 	rw_exit(&ipst->ips_ire_dep_lock);
3561 }
3562 
3563 /*
3564  * ire_pref() is used in recursive route-resolution for a destination to
3565  * determine the preference of an ire, where "preference" is determined
3566  * based on the level of indirection to the destination of the ire.
3567  * A higher preference indicates that fewer lookups are needed to complete
3568  * recursive route lookup. Thus
3569  * ire_pref(RTF_INDIRECT) < ire_pref(IRE_IF_RESOLVER) < ire_pref(IRE_PREF_CLONE)
3570  */
3571 int
3572 ire_pref(ire_t *ire)
3573 {
3574 	if (ire->ire_flags & RTF_INDIRECT)
3575 		return (1);
3576 	if (ire->ire_type & IRE_OFFLINK)
3577 		return (2);
3578 	if (ire->ire_type & (IRE_IF_RESOLVER|IRE_IF_NORESOLVER))
3579 		return (3);
3580 	if (ire->ire_type & IRE_IF_CLONE)
3581 		return (4);
3582 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST))
3583 		return (5);
3584 	return (-1); /* unknown ire_type */
3585 }
3586 
3587 /*
3588  * In the preferred/strict src multihoming modes, unbound routes (i.e.,
3589  * ire_t entries with ire_unbound set to B_TRUE) are bound to an interface
3590  * by selecting the first available interface that has an interface route for
3591  * the ire_gateway. If that interface is subsequently brought down, ill_downi()
3592  * will call ire_rebind() so that the unbound route can be bound to some other
3593  * matching interface thereby preserving the intended reachability information
3594  * from the original unbound route.
3595  */
3596 void
3597 ire_rebind(ire_t *ire)
3598 {
3599 	ire_t	*gw_ire, *new_ire;
3600 	int	match_flags = MATCH_IRE_TYPE;
3601 	ill_t	*gw_ill;
3602 	boolean_t isv6 = (ire->ire_ipversion == IPV6_VERSION);
3603 	ip_stack_t *ipst = ire->ire_ipst;
3604 
3605 	ASSERT(ire->ire_unbound);
3606 again:
3607 	if (isv6) {
3608 		gw_ire = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0,
3609 		    IRE_INTERFACE, NULL, ALL_ZONES, NULL, match_flags, 0,
3610 		    ipst, NULL);
3611 	} else {
3612 		gw_ire = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
3613 		    IRE_INTERFACE, NULL, ALL_ZONES, NULL, match_flags, 0,
3614 		    ipst, NULL);
3615 	}
3616 	if (gw_ire == NULL) {
3617 		/* see comments in ip_rt_add[_v6]() for IPMP */
3618 		if (match_flags & MATCH_IRE_TESTHIDDEN)
3619 			return;
3620 
3621 		match_flags |= MATCH_IRE_TESTHIDDEN;
3622 		goto again;
3623 	}
3624 	gw_ill = gw_ire->ire_ill;
3625 	if (isv6) {
3626 		new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
3627 		    &ire->ire_gateway_addr_v6, ire->ire_type, gw_ill,
3628 		    ire->ire_zoneid, ire->ire_flags, NULL, ipst);
3629 	} else {
3630 		new_ire = ire_create((uchar_t *)&ire->ire_addr,
3631 		    (uchar_t *)&ire->ire_mask,
3632 		    (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, gw_ill,
3633 		    ire->ire_zoneid, ire->ire_flags, NULL, ipst);
3634 	}
3635 	ire_refrele(gw_ire);
3636 	if (new_ire == NULL)
3637 		return;
3638 	new_ire->ire_unbound = B_TRUE;
3639 	new_ire = ire_add(new_ire);
3640 	if (new_ire != NULL)
3641 		ire_refrele(new_ire);
3642 }
3643