xref: /titanic_52/usr/src/uts/common/inet/ip/ip_ire.c (revision b72c368a02e0464faeef362bc5a1cf0fc69981da)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains routines that manipulate Internet Routing Entries (IREs).
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/strsubr.h>
36 #include <sys/ddi.h>
37 #include <sys/cmn_err.h>
38 #include <sys/policy.h>
39 
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <net/if_dl.h>
48 #include <netinet/ip6.h>
49 #include <netinet/icmp6.h>
50 
51 #include <inet/common.h>
52 #include <inet/mi.h>
53 #include <inet/ip.h>
54 #include <inet/ip6.h>
55 #include <inet/ip_ndp.h>
56 #include <inet/arp.h>
57 #include <inet/ip_if.h>
58 #include <inet/ip_ire.h>
59 #include <inet/ip_ftable.h>
60 #include <inet/ip_rts.h>
61 #include <inet/nd.h>
62 
63 #include <inet/tcp.h>
64 #include <inet/ipclassifier.h>
65 #include <sys/zone.h>
66 #include <sys/cpuvar.h>
67 
68 #include <sys/tsol/label.h>
69 #include <sys/tsol/tnet.h>
70 
71 struct kmem_cache *rt_entry_cache;
72 
73 typedef struct nce_clookup_s {
74 	ipaddr_t ncecl_addr;
75 	boolean_t ncecl_found;
76 } nce_clookup_t;
77 
78 /*
79  * Synchronization notes:
80  *
81  * The fields of the ire_t struct are protected in the following way :
82  *
83  * ire_next/ire_ptpn
84  *
85  *	- bucket lock of the forwarding table in which is ire stored.
86  *
87  * ire_ill, ire_u *except* ire_gateway_addr[v6], ire_mask,
88  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags,
89  * ire_bucket
90  *
91  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
92  *	  we don't need a lock whenever these fields are accessed.
93  *
94  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
95  *        ire_add before inserting in the bucket and never
96  *        changes after that. Thus we don't need a lock whenever these
97  *	  fields are accessed.
98  *
99  * ire_gateway_addr_v4[v6]
100  *
101  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
102  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
103  *	  it assumed to be atomic and hence the other parts of the code
104  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
105  *	  and hence any access to it uses ire_lock to get/set the right value.
106  *
107  * ire_refcnt, ire_identical_ref
108  *
109  *	- Updated atomically using atomic_add_32
110  *
111  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
112  *
113  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
114  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
115  *
116  * ire_generation
117  *	- Under ire_lock
118  *
119  * ire_nce_cache
120  *	- Under ire_lock
121  *
122  * ire_dep_parent (To next IRE in recursive lookup chain)
123  *	- Under ips_ire_dep_lock. Write held when modifying. Read held when
124  *	  walking. We also hold ire_lock when modifying to allow the data path
125  *	  to only acquire ire_lock.
126  *
127  * ire_dep_parent_generation (Generation number from ire_dep_parent)
128  *	- Under ips_ire_dep_lock and/or ire_lock. (A read claim on the dep_lock
129  *	  and ire_lock held when modifying)
130  *
131  * ire_dep_children (From parent to first child)
132  * ire_dep_sib_next (linked list of siblings)
133  * ire_dep_sib_ptpn (linked list of siblings)
134  *	- Under ips_ire_dep_lock. Write held when modifying. Read held when
135  *	  walking.
136  *
137  * As we always hold the bucket locks in all the places while accessing
138  * the above values, it is natural to use them for protecting them.
139  *
140  * We have a forwarding table for IPv4 and IPv6. The IPv6 forwarding table
141  * (ip_forwarding_table_v6) is an array of pointers to arrays of irb_t
142  * structures. ip_forwarding_table_v6 is allocated dynamically in
143  * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads
144  * initializing the same bucket. Once a bucket is initialized, it is never
145  * de-alloacted. This assumption enables us to access
146  * ip_forwarding_table_v6[i] without any locks.
147  *
148  * The forwarding table for IPv4 is a radix tree whose leaves
149  * are rt_entry structures containing the irb_t for the rt_dst. The irb_t
150  * for IPv4 is dynamically allocated and freed.
151  *
152  * Each irb_t - ire bucket structure has a lock to protect
153  * a bucket and the ires residing in the bucket have a back pointer to
154  * the bucket structure. It also has a reference count for the number
155  * of threads walking the bucket - irb_refcnt which is bumped up
156  * using the irb_refhold function. The flags irb_marks can be
157  * set to IRB_MARK_CONDEMNED indicating that there are some ires
158  * in this bucket that are IRE_IS_CONDEMNED and the
159  * last thread to leave the bucket should delete the ires. Usually
160  * this is done by the irb_refrele function which is used to decrement
161  * the reference count on a bucket. See comments above irb_t structure
162  * definition in ip.h for further details.
163  *
164  * The ire_refhold/ire_refrele functions operate on the ire which increments/
165  * decrements the reference count, ire_refcnt, atomically on the ire.
166  * ire_refcnt is modified only using those functions. Operations on the IRE
167  * could be described as follows :
168  *
169  * CREATE an ire with reference count initialized to 1.
170  *
171  * ADDITION of an ire holds the bucket lock, checks for duplicates
172  * and then adds the ire. ire_add returns the ire after
173  * bumping up once more i.e the reference count is 2. This is to avoid
174  * an extra lookup in the functions calling ire_add which wants to
175  * work with the ire after adding.
176  *
177  * LOOKUP of an ire bumps up the reference count using ire_refhold
178  * function. It is valid to bump up the referece count of the IRE,
179  * after the lookup has returned an ire. Following are the lookup
180  * functions that return an HELD ire :
181  *
182  * ire_ftable_lookup[_v6], ire_lookup_multi_ill[_v6]
183  *
184  * DELETION of an ire holds the bucket lock, removes it from the list
185  * and then decrements the reference count for having removed from the list
186  * by using the ire_refrele function. If some other thread has looked up
187  * the ire, the reference count would have been bumped up and hence
188  * this ire will not be freed once deleted. It will be freed once the
189  * reference count drops to zero.
190  *
191  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
192  * lookups acquire the bucket lock as RW_READER.
193  *
194  * The general rule is to do the ire_refrele in the function
195  * that is passing the ire as an argument.
196  *
197  * In trying to locate ires the following points are to be noted.
198  *
199  * IRE_IS_CONDEMNED signifies that the ire has been logically deleted and is
200  * to be ignored when walking the ires using ire_next.
201  *
202  * Zones note:
203  *	Walking IREs within a given zone also walks certain ires in other
204  *	zones.  This is done intentionally.  IRE walks with a specified
205  *	zoneid are used only when doing informational reports, and
206  *	zone users want to see things that they can access. See block
207  *	comment in ire_walk_ill_match().
208  */
209 
210 /*
211  * The size of the forwarding table.  We will make sure that it is a
212  * power of 2 in ip_ire_init().
213  * Setable in /etc/system
214  */
215 uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
216 
217 struct	kmem_cache	*ire_cache;
218 struct	kmem_cache	*ncec_cache;
219 struct	kmem_cache	*nce_cache;
220 
221 static ire_t	ire_null;
222 
223 static ire_t	*ire_add_v4(ire_t *ire);
224 static void	ire_delete_v4(ire_t *ire);
225 static void	ire_dep_invalidate_children(ire_t *child);
226 static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
227     zoneid_t zoneid, ip_stack_t *);
228 static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
229     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
230 #ifdef DEBUG
231 static void	ire_trace_cleanup(const ire_t *);
232 #endif
233 
234 /*
235  * Following are the functions to increment/decrement the reference
236  * count of the IREs and IRBs (ire bucket).
237  *
238  * 1) We bump up the reference count of an IRE to make sure that
239  *    it does not get deleted and freed while we are using it.
240  *    Typically all the lookup functions hold the bucket lock,
241  *    and look for the IRE. If it finds an IRE, it bumps up the
242  *    reference count before dropping the lock. Sometimes we *may* want
243  *    to bump up the reference count after we *looked* up i.e without
244  *    holding the bucket lock. So, the ire_refhold function does not assert
245  *    on the bucket lock being held. Any thread trying to delete from
246  *    the hash bucket can still do so but cannot free the IRE if
247  *    ire_refcnt is not 0.
248  *
249  * 2) We bump up the reference count on the bucket where the IRE resides
250  *    (IRB), when we want to prevent the IREs getting deleted from a given
251  *    hash bucket. This makes life easier for ire_walk type functions which
252  *    wants to walk the IRE list, call a function, but needs to drop
253  *    the bucket lock to prevent recursive rw_enters. While the
254  *    lock is dropped, the list could be changed by other threads or
255  *    the same thread could end up deleting the ire or the ire pointed by
256  *    ire_next. ire_refholding the ire or ire_next is not sufficient as
257  *    a delete will still remove the ire from the bucket while we have
258  *    dropped the lock and hence the ire_next would be NULL. Thus, we
259  *    need a mechanism to prevent deletions from a given bucket.
260  *
261  *    To prevent deletions, we bump up the reference count on the
262  *    bucket. If the bucket is held, ire_delete just marks both
263  *    the ire and irb as CONDEMNED. When the
264  *    reference count on the bucket drops to zero, all the CONDEMNED ires
265  *    are deleted. We don't have to bump up the reference count on the
266  *    bucket if we are walking the bucket and never have to drop the bucket
267  *    lock. Note that irb_refhold does not prevent addition of new ires
268  *    in the list. It is okay because addition of new ires will not cause
269  *    ire_next to point to freed memory. We do irb_refhold only when
270  *    all of the 3 conditions are true :
271  *
272  *    1) The code needs to walk the IRE bucket from start to end.
273  *    2) It may have to drop the bucket lock sometimes while doing (1)
274  *    3) It does not want any ires to be deleted meanwhile.
275  */
276 
277 /*
278  * Bump up the reference count on the hash bucket - IRB to
279  * prevent ires from being deleted in this bucket.
280  */
281 void
282 irb_refhold(irb_t *irb)
283 {
284 	rw_enter(&irb->irb_lock, RW_WRITER);
285 	irb->irb_refcnt++;
286 	ASSERT(irb->irb_refcnt != 0);
287 	rw_exit(&irb->irb_lock);
288 }
289 
290 void
291 irb_refhold_locked(irb_t *irb)
292 {
293 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
294 	irb->irb_refcnt++;
295 	ASSERT(irb->irb_refcnt != 0);
296 }
297 
298 /*
299  * Note: when IRB_MARK_DYNAMIC is not set the irb_t
300  * is statically allocated, so that when the irb_refcnt goes to 0,
301  * we simply clean up the ire list and continue.
302  */
303 void
304 irb_refrele(irb_t *irb)
305 {
306 	if (irb->irb_marks & IRB_MARK_DYNAMIC) {
307 		irb_refrele_ftable(irb);
308 	} else {
309 		rw_enter(&irb->irb_lock, RW_WRITER);
310 		ASSERT(irb->irb_refcnt != 0);
311 		if (--irb->irb_refcnt	== 0 &&
312 		    (irb->irb_marks & IRB_MARK_CONDEMNED)) {
313 			ire_t *ire_list;
314 
315 			ire_list = ire_unlink(irb);
316 			rw_exit(&irb->irb_lock);
317 			ASSERT(ire_list != NULL);
318 			ire_cleanup(ire_list);
319 		} else {
320 			rw_exit(&irb->irb_lock);
321 		}
322 	}
323 }
324 
325 
326 /*
327  * Bump up the reference count on the IRE. We cannot assert that the
328  * bucket lock is being held as it is legal to bump up the reference
329  * count after the first lookup has returned the IRE without
330  * holding the lock.
331  */
332 void
333 ire_refhold(ire_t *ire)
334 {
335 	atomic_add_32(&(ire)->ire_refcnt, 1);
336 	ASSERT((ire)->ire_refcnt != 0);
337 #ifdef DEBUG
338 	ire_trace_ref(ire);
339 #endif
340 }
341 
342 void
343 ire_refhold_notr(ire_t *ire)
344 {
345 	atomic_add_32(&(ire)->ire_refcnt, 1);
346 	ASSERT((ire)->ire_refcnt != 0);
347 }
348 
349 void
350 ire_refhold_locked(ire_t *ire)
351 {
352 #ifdef DEBUG
353 	ire_trace_ref(ire);
354 #endif
355 	ire->ire_refcnt++;
356 }
357 
358 /*
359  * Release a ref on an IRE.
360  *
361  * Must not be called while holding any locks. Otherwise if this is
362  * the last reference to be released there is a chance of recursive mutex
363  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
364  * to restart an ioctl. The one exception is when the caller is sure that
365  * this is not the last reference to be released. Eg. if the caller is
366  * sure that the ire has not been deleted and won't be deleted.
367  *
368  * In architectures e.g sun4u, where atomic_add_32_nv is just
369  * a cas, we need to maintain the right memory barrier semantics
370  * as that of mutex_exit i.e all the loads and stores should complete
371  * before the cas is executed. membar_exit() does that here.
372  */
373 void
374 ire_refrele(ire_t *ire)
375 {
376 #ifdef DEBUG
377 	ire_untrace_ref(ire);
378 #endif
379 	ASSERT((ire)->ire_refcnt != 0);
380 	membar_exit();
381 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)
382 		ire_inactive(ire);
383 }
384 
385 void
386 ire_refrele_notr(ire_t *ire)
387 {
388 	ASSERT((ire)->ire_refcnt != 0);
389 	membar_exit();
390 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)
391 		ire_inactive(ire);
392 }
393 
394 /*
395  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
396  * IOCTL[s].  The NO_REPLY form is used by TCP to tell IP that it is
397  * having problems reaching a particular destination.
398  * This will make IP consider alternate routes (e.g., when there are
399  * muliple default routes), and it will also make IP discard any (potentially)
400  * stale redirect.
401  * Management processes may want to use the version that generates a reply.
402  *
403  * With the use of NUD like behavior for IPv4/ARP in addition to IPv6
404  * this function shouldn't be necessary for IP to recover from a bad redirect,
405  * a bad default router (when there are multiple default routers), or
406  * a stale ND/ARP entry. But we retain it in any case.
407  * For instance, this is helpful when TCP suspects a failure before NUD does.
408  */
409 int
410 ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
411 {
412 	uchar_t		*addr_ucp;
413 	uint_t		ipversion;
414 	sin_t		*sin;
415 	sin6_t		*sin6;
416 	ipaddr_t	v4addr;
417 	in6_addr_t	v6addr;
418 	ire_t		*ire;
419 	ipid_t		*ipid;
420 	zoneid_t	zoneid;
421 	ip_stack_t	*ipst;
422 
423 	ASSERT(q->q_next == NULL);
424 	zoneid = IPCL_ZONEID(Q_TO_CONN(q));
425 	ipst = CONNQ_TO_IPST(q);
426 
427 	/*
428 	 * Check privilege using the ioctl credential; if it is NULL
429 	 * then this is a kernel message and therefor privileged.
430 	 */
431 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
432 		return (EPERM);
433 
434 	ipid = (ipid_t *)mp->b_rptr;
435 
436 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
437 	    ipid->ipid_addr_length);
438 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
439 		return (EINVAL);
440 	switch (ipid->ipid_addr_length) {
441 	case sizeof (sin_t):
442 		/*
443 		 * got complete (sockaddr) address - increment addr_ucp to point
444 		 * at the ip_addr field.
445 		 */
446 		sin = (sin_t *)addr_ucp;
447 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
448 		ipversion = IPV4_VERSION;
449 		break;
450 	case sizeof (sin6_t):
451 		/*
452 		 * got complete (sockaddr) address - increment addr_ucp to point
453 		 * at the ip_addr field.
454 		 */
455 		sin6 = (sin6_t *)addr_ucp;
456 		addr_ucp = (uchar_t *)&sin6->sin6_addr;
457 		ipversion = IPV6_VERSION;
458 		break;
459 	default:
460 		return (EINVAL);
461 	}
462 	if (ipversion == IPV4_VERSION) {
463 		/* Extract the destination address. */
464 		bcopy(addr_ucp, &v4addr, IP_ADDR_LEN);
465 
466 		ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
467 		    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
468 	} else {
469 		/* Extract the destination address. */
470 		bcopy(addr_ucp, &v6addr, IPV6_ADDR_LEN);
471 
472 		ire = ire_ftable_lookup_v6(&v6addr, NULL, NULL, 0, NULL,
473 		    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
474 	}
475 	if (ire != NULL) {
476 		if (ipversion == IPV4_VERSION) {
477 			ip_rts_change(RTM_LOSING, ire->ire_addr,
478 			    ire->ire_gateway_addr, ire->ire_mask,
479 			    (Q_TO_CONN(q))->conn_laddr_v4,  0, 0, 0,
480 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA),
481 			    ire->ire_ipst);
482 		}
483 		(void) ire_no_good(ire);
484 		ire_refrele(ire);
485 	}
486 	return (0);
487 }
488 
489 /*
490  * Initialize the ire that is specific to IPv4 part and call
491  * ire_init_common to finish it.
492  * Returns zero or errno.
493  */
494 int
495 ire_init_v4(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *gateway,
496     ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags,
497     tsol_gc_t *gc, ip_stack_t *ipst)
498 {
499 	int error;
500 
501 	/*
502 	 * Reject IRE security attribute creation/initialization
503 	 * if system is not running in Trusted mode.
504 	 */
505 	if (gc != NULL && !is_system_labeled())
506 		return (EINVAL);
507 
508 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
509 
510 	if (addr != NULL)
511 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
512 	if (gateway != NULL)
513 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
514 
515 	/* Make sure we don't have stray values in some fields */
516 	switch (type) {
517 	case IRE_LOOPBACK:
518 		bcopy(&ire->ire_addr, &ire->ire_gateway_addr, IP_ADDR_LEN);
519 		/* FALLTHRU */
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 	nce_t	*nce;
1485 	ip_stack_t	*ipst = ire->ire_ipst;
1486 
1487 	/* We can clear ire_nce_cache under ire_lock even if the IRE is used */
1488 	mutex_enter(&ire->ire_lock);
1489 	nce = ire->ire_nce_cache;
1490 	ire->ire_nce_cache = NULL;
1491 	mutex_exit(&ire->ire_lock);
1492 	if (nce != NULL)
1493 		nce_refrele(nce);
1494 
1495 	if ((irb = ire->ire_bucket) == NULL) {
1496 		/*
1497 		 * It was never inserted in the list. Should call REFRELE
1498 		 * to free this IRE.
1499 		 */
1500 		ire_refrele_notr(ire);
1501 		return;
1502 	}
1503 
1504 	/*
1505 	 * Move the use counts from an IRE_IF_CLONE to its parent
1506 	 * IRE_INTERFACE.
1507 	 * We need to do this before acquiring irb_lock.
1508 	 */
1509 	if (ire->ire_type & IRE_IF_CLONE) {
1510 		ire_t *parent;
1511 
1512 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
1513 		if ((parent = ire->ire_dep_parent) != NULL) {
1514 			parent->ire_ob_pkt_count += ire->ire_ob_pkt_count;
1515 			parent->ire_ib_pkt_count += ire->ire_ib_pkt_count;
1516 			ire->ire_ob_pkt_count = 0;
1517 			ire->ire_ib_pkt_count = 0;
1518 		}
1519 		rw_exit(&ipst->ips_ire_dep_lock);
1520 	}
1521 
1522 	rw_enter(&irb->irb_lock, RW_WRITER);
1523 	if (ire->ire_ptpn == NULL) {
1524 		/*
1525 		 * Some other thread has removed us from the list.
1526 		 * It should have done the REFRELE for us.
1527 		 */
1528 		rw_exit(&irb->irb_lock);
1529 		return;
1530 	}
1531 
1532 	if (!IRE_IS_CONDEMNED(ire)) {
1533 		/* Is this an IRE representing multiple duplicate entries? */
1534 		ASSERT(ire->ire_identical_ref >= 1);
1535 		if (atomic_add_32_nv(&ire->ire_identical_ref, -1) != 0) {
1536 			/* Removed one of the identical parties */
1537 			rw_exit(&irb->irb_lock);
1538 			return;
1539 		}
1540 
1541 		irb->irb_ire_cnt--;
1542 		ire_make_condemned(ire);
1543 	}
1544 
1545 	if (irb->irb_refcnt != 0) {
1546 		/*
1547 		 * The last thread to leave this bucket will
1548 		 * delete this ire.
1549 		 */
1550 		irb->irb_marks |= IRB_MARK_CONDEMNED;
1551 		rw_exit(&irb->irb_lock);
1552 		return;
1553 	}
1554 
1555 	/*
1556 	 * Normally to delete an ire, we walk the bucket. While we
1557 	 * walk the bucket, we normally bump up irb_refcnt and hence
1558 	 * we return from above where we mark CONDEMNED and the ire
1559 	 * gets deleted from ire_unlink. This case is where somebody
1560 	 * knows the ire e.g by doing a lookup, and wants to delete the
1561 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
1562 	 * the bucket.
1563 	 */
1564 	ptpn = ire->ire_ptpn;
1565 	ire1 = ire->ire_next;
1566 	if (ire1 != NULL)
1567 		ire1->ire_ptpn = ptpn;
1568 	ASSERT(ptpn != NULL);
1569 	*ptpn = ire1;
1570 	ire->ire_ptpn = NULL;
1571 	ire->ire_next = NULL;
1572 	if (ire->ire_ipversion == IPV6_VERSION) {
1573 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
1574 	} else {
1575 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
1576 	}
1577 	rw_exit(&irb->irb_lock);
1578 
1579 	/* Cleanup dependents and related stuff */
1580 	if (ire->ire_ipversion == IPV6_VERSION) {
1581 		ire_delete_v6(ire);
1582 	} else {
1583 		ire_delete_v4(ire);
1584 	}
1585 	/*
1586 	 * We removed it from the list. Decrement the
1587 	 * reference count.
1588 	 */
1589 	ire_refrele_notr(ire);
1590 }
1591 
1592 /*
1593  * Delete the specified IRE.
1594  * All calls should use ire_delete().
1595  * Sometimes called as writer though not required by this function.
1596  *
1597  * NOTE : This function is called only if the ire was added
1598  * in the list.
1599  */
1600 static void
1601 ire_delete_v4(ire_t *ire)
1602 {
1603 	ip_stack_t	*ipst = ire->ire_ipst;
1604 
1605 	ASSERT(ire->ire_refcnt >= 1);
1606 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1607 
1608 	ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
1609 	if (ire->ire_type == IRE_DEFAULT) {
1610 		/*
1611 		 * when a default gateway is going away
1612 		 * delete all the host redirects pointing at that
1613 		 * gateway.
1614 		 */
1615 		ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
1616 	}
1617 
1618 	/*
1619 	 * If we are deleting an IRE_INTERFACE then we make sure we also
1620 	 * delete any IRE_IF_CLONE that has been created from it.
1621 	 * Those are always in ire_dep_children.
1622 	 */
1623 	if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != NULL)
1624 		ire_dep_delete_if_clone(ire);
1625 
1626 	/* Remove from parent dependencies and child */
1627 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
1628 	if (ire->ire_dep_parent != NULL)
1629 		ire_dep_remove(ire);
1630 
1631 	while (ire->ire_dep_children != NULL)
1632 		ire_dep_remove(ire->ire_dep_children);
1633 	rw_exit(&ipst->ips_ire_dep_lock);
1634 }
1635 
1636 /*
1637  * ire_refrele is the only caller of the function. It calls
1638  * to free the ire when the reference count goes to zero.
1639  */
1640 void
1641 ire_inactive(ire_t *ire)
1642 {
1643 	ill_t	*ill;
1644 	irb_t 	*irb;
1645 	ip_stack_t	*ipst = ire->ire_ipst;
1646 
1647 	ASSERT(ire->ire_refcnt == 0);
1648 	ASSERT(ire->ire_ptpn == NULL);
1649 	ASSERT(ire->ire_next == NULL);
1650 
1651 	/* Count how many condemned ires for kmem_cache callback */
1652 	if (IRE_IS_CONDEMNED(ire))
1653 		atomic_add_32(&ipst->ips_num_ire_condemned, -1);
1654 
1655 	if (ire->ire_gw_secattr != NULL) {
1656 		ire_gw_secattr_free(ire->ire_gw_secattr);
1657 		ire->ire_gw_secattr = NULL;
1658 	}
1659 
1660 	/*
1661 	 * ire_nce_cache is cleared in ire_delete, and we make sure we don't
1662 	 * set it once the ire is marked condemned.
1663 	 */
1664 	ASSERT(ire->ire_nce_cache == NULL);
1665 
1666 	/*
1667 	 * Since any parent would have a refhold on us they would already
1668 	 * have been removed.
1669 	 */
1670 	ASSERT(ire->ire_dep_parent == NULL);
1671 	ASSERT(ire->ire_dep_sib_next == NULL);
1672 	ASSERT(ire->ire_dep_sib_ptpn == NULL);
1673 
1674 	/*
1675 	 * Since any children would have a refhold on us they should have
1676 	 * already been removed.
1677 	 */
1678 	ASSERT(ire->ire_dep_children == NULL);
1679 
1680 	/*
1681 	 * ill_ire_ref is increased when the IRE is inserted in the
1682 	 * bucket - not when the IRE is created.
1683 	 */
1684 	irb = ire->ire_bucket;
1685 	ill = ire->ire_ill;
1686 	if (irb != NULL && ill != NULL) {
1687 		mutex_enter(&ill->ill_lock);
1688 		ASSERT(ill->ill_ire_cnt != 0);
1689 		DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
1690 		    (char *), "ire", (void *), ire);
1691 		ill->ill_ire_cnt--;
1692 		if (ILL_DOWN_OK(ill)) {
1693 			/* Drops the ill lock */
1694 			ipif_ill_refrele_tail(ill);
1695 		} else {
1696 			mutex_exit(&ill->ill_lock);
1697 		}
1698 	}
1699 	ire->ire_ill = NULL;
1700 
1701 	/* This should be true for both V4 and V6 */
1702 	if (irb != NULL && (irb->irb_marks & IRB_MARK_DYNAMIC)) {
1703 		rw_enter(&irb->irb_lock, RW_WRITER);
1704 		irb->irb_nire--;
1705 		/*
1706 		 * Instead of examining the conditions for freeing
1707 		 * the radix node here, we do it by calling
1708 		 * irb_refrele which is a single point in the code
1709 		 * that embeds that logic. Bump up the refcnt to
1710 		 * be able to call irb_refrele
1711 		 */
1712 		irb_refhold_locked(irb);
1713 		rw_exit(&irb->irb_lock);
1714 		irb_refrele(irb);
1715 	}
1716 
1717 #ifdef DEBUG
1718 	ire_trace_cleanup(ire);
1719 #endif
1720 	mutex_destroy(&ire->ire_lock);
1721 	if (ire->ire_ipversion == IPV6_VERSION) {
1722 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
1723 	} else {
1724 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
1725 	}
1726 	kmem_cache_free(ire_cache, ire);
1727 }
1728 
1729 /*
1730  * ire_update_generation is the callback function provided by
1731  * ire_get_bucket() to update the generation number of any
1732  * matching shorter route when a new route is added.
1733  *
1734  * This fucntion always returns a failure return (B_FALSE)
1735  * to force the caller (rn_matchaddr_args)
1736  * to back-track up the tree looking for shorter matches.
1737  */
1738 /* ARGSUSED */
1739 static boolean_t
1740 ire_update_generation(struct radix_node *rn, void *arg)
1741 {
1742 	struct rt_entry *rt = (struct rt_entry *)rn;
1743 
1744 	/* We need to handle all in the same bucket */
1745 	irb_increment_generation(&rt->rt_irb);
1746 	return (B_FALSE);
1747 }
1748 
1749 /*
1750  * Take care of all the generation numbers in the bucket.
1751  */
1752 void
1753 irb_increment_generation(irb_t *irb)
1754 {
1755 	ire_t *ire;
1756 
1757 	if (irb == NULL || irb->irb_ire_cnt == 0)
1758 		return;
1759 
1760 	irb_refhold(irb);
1761 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
1762 		if (!IRE_IS_CONDEMNED(ire))
1763 			ire_increment_generation(ire);	/* Ourselves */
1764 		ire_dep_incr_generation(ire);	/* Dependants */
1765 	}
1766 	irb_refrele(irb);
1767 }
1768 
1769 /*
1770  * When an IRE is added or deleted this routine is called to make sure
1771  * any caching of IRE information is notified or updated.
1772  *
1773  * The flag argument indicates if the flush request is due to addition
1774  * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE),
1775  * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE).
1776  */
1777 void
1778 ire_flush_cache_v4(ire_t *ire, int flag)
1779 {
1780 	irb_t *irb = ire->ire_bucket;
1781 	struct rt_entry *rt = IRB2RT(irb);
1782 	ip_stack_t *ipst = ire->ire_ipst;
1783 
1784 	/*
1785 	 * IRE_IF_CLONE ire's don't provide any new information
1786 	 * than the parent from which they are cloned, so don't
1787 	 * perturb the generation numbers.
1788 	 */
1789 	if (ire->ire_type & IRE_IF_CLONE)
1790 		return;
1791 
1792 	/*
1793 	 * Ensure that an ire_add during a lookup serializes the updates of the
1794 	 * generation numbers under the radix head lock so that the lookup gets
1795 	 * either the old ire and old generation number, or a new ire and new
1796 	 * generation number.
1797 	 */
1798 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
1799 
1800 	/*
1801 	 * If a route was just added, we need to notify everybody that
1802 	 * has cached an IRE_NOROUTE since there might now be a better
1803 	 * route for them.
1804 	 */
1805 	if (flag == IRE_FLUSH_ADD) {
1806 		ire_increment_generation(ipst->ips_ire_reject_v4);
1807 		ire_increment_generation(ipst->ips_ire_blackhole_v4);
1808 	}
1809 
1810 	/* Adding a default can't otherwise provide a better route */
1811 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) {
1812 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1813 		return;
1814 	}
1815 
1816 	switch (flag) {
1817 	case IRE_FLUSH_DELETE:
1818 	case IRE_FLUSH_GWCHANGE:
1819 		/*
1820 		 * Update ire_generation for all ire_dep_children chains
1821 		 * starting with this IRE
1822 		 */
1823 		ire_dep_incr_generation(ire);
1824 		break;
1825 	case IRE_FLUSH_ADD:
1826 		/*
1827 		 * Update the generation numbers of all shorter matching routes.
1828 		 * ire_update_generation takes care of the dependants by
1829 		 * using ire_dep_incr_generation.
1830 		 */
1831 		(void) ipst->ips_ip_ftable->rnh_matchaddr_args(&rt->rt_dst,
1832 		    ipst->ips_ip_ftable, ire_update_generation, NULL);
1833 		break;
1834 	}
1835 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
1836 }
1837 
1838 /*
1839  * Matches the arguments passed with the values in the ire.
1840  *
1841  * Note: for match types that match using "ill" passed in, ill
1842  * must be checked for non-NULL before calling this routine.
1843  */
1844 boolean_t
1845 ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
1846     int type, const ill_t *ill, zoneid_t zoneid,
1847     const ts_label_t *tsl, int match_flags)
1848 {
1849 	ill_t *ire_ill = NULL, *dst_ill;
1850 	ip_stack_t *ipst = ire->ire_ipst;
1851 
1852 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
1853 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
1854 	ASSERT((!(match_flags & MATCH_IRE_ILL)) ||
1855 	    (ill != NULL && !ill->ill_isv6));
1856 
1857 	/*
1858 	 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it is
1859 	 * in fact hidden, to ensure the caller gets the right one.
1860 	 */
1861 	if (ire->ire_testhidden) {
1862 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))
1863 			return (B_FALSE);
1864 	}
1865 
1866 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
1867 	    ire->ire_zoneid != ALL_ZONES) {
1868 		/*
1869 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid
1870 		 * does not match that of ire_zoneid, a failure to
1871 		 * match is reported at this point. Otherwise, since some IREs
1872 		 * that are available in the global zone can be used in local
1873 		 * zones, additional checks need to be performed:
1874 		 *
1875 		 * IRE_LOOPBACK
1876 		 *	entries should never be matched in this situation.
1877 		 *	Each zone has its own IRE_LOOPBACK.
1878 		 *
1879 		 * IRE_LOCAL
1880 		 *	We allow them for any zoneid. ire_route_recursive
1881 		 *	does additional checks when
1882 		 *	ip_restrict_interzone_loopback is set.
1883 		 *
1884 		 * If ill_usesrc_ifindex is set
1885 		 *	Then we check if the zone has a valid source address
1886 		 *	on the usesrc ill.
1887 		 *
1888 		 * If ire_ill is set, then check that the zone has an ipif
1889 		 *	on that ill.
1890 		 *
1891 		 * Outside of this function (in ire_round_robin) we check
1892 		 * that any IRE_OFFLINK has a gateway that reachable from the
1893 		 * zone when we have multiple choices (ECMP).
1894 		 */
1895 		if (match_flags & MATCH_IRE_ZONEONLY)
1896 			return (B_FALSE);
1897 		if (ire->ire_type & IRE_LOOPBACK)
1898 			return (B_FALSE);
1899 
1900 		if (ire->ire_type & IRE_LOCAL)
1901 			goto matchit;
1902 
1903 		/*
1904 		 * The normal case of IRE_ONLINK has a matching zoneid.
1905 		 * Here we handle the case when shared-IP zones have been
1906 		 * configured with IP addresses on vniN. In that case it
1907 		 * is ok for traffic from a zone to use IRE_ONLINK routes
1908 		 * if the ill has a usesrc pointing at vniN
1909 		 */
1910 		dst_ill = ire->ire_ill;
1911 		if (ire->ire_type & IRE_ONLINK) {
1912 			uint_t	ifindex;
1913 
1914 			/*
1915 			 * Note there is no IRE_INTERFACE on vniN thus
1916 			 * can't do an IRE lookup for a matching route.
1917 			 */
1918 			ifindex = dst_ill->ill_usesrc_ifindex;
1919 			if (ifindex == 0)
1920 				return (B_FALSE);
1921 
1922 			/*
1923 			 * If there is a usable source address in the
1924 			 * zone, then it's ok to return this IRE_INTERFACE
1925 			 */
1926 			if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
1927 			    zoneid, ipst)) {
1928 				ip3dbg(("ire_match_args: no usrsrc for zone"
1929 				    " dst_ill %p\n", (void *)dst_ill));
1930 				return (B_FALSE);
1931 			}
1932 		}
1933 		/*
1934 		 * For exampe, with
1935 		 * route add 11.0.0.0 gw1 -ifp bge0
1936 		 * route add 11.0.0.0 gw2 -ifp bge1
1937 		 * this code would differentiate based on
1938 		 * where the sending zone has addresses.
1939 		 * Only if the zone has an address on bge0 can it use the first
1940 		 * route. It isn't clear if this behavior is documented
1941 		 * anywhere.
1942 		 */
1943 		if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
1944 			ipif_t	*tipif;
1945 
1946 			mutex_enter(&dst_ill->ill_lock);
1947 			for (tipif = dst_ill->ill_ipif;
1948 			    tipif != NULL; tipif = tipif->ipif_next) {
1949 				if (!IPIF_IS_CONDEMNED(tipif) &&
1950 				    (tipif->ipif_flags & IPIF_UP) &&
1951 				    (tipif->ipif_zoneid == zoneid ||
1952 				    tipif->ipif_zoneid == ALL_ZONES))
1953 					break;
1954 			}
1955 			mutex_exit(&dst_ill->ill_lock);
1956 			if (tipif == NULL) {
1957 				return (B_FALSE);
1958 			}
1959 		}
1960 	}
1961 
1962 matchit:
1963 	if (match_flags & MATCH_IRE_ILL) {
1964 		ire_ill = ire->ire_ill;
1965 
1966 		/*
1967 		 * If asked to match an ill, we *must* match
1968 		 * on the ire_ill for ipmp test addresses, or
1969 		 * any of the ill in the group for data addresses.
1970 		 * If we don't, we may as well fail.
1971 		 * However, we need an exception for IRE_LOCALs to ensure
1972 		 * we loopback packets even sent to test addresses on different
1973 		 * interfaces in the group.
1974 		 */
1975 		if ((match_flags & MATCH_IRE_TESTHIDDEN) &&
1976 		    !(ire->ire_type & IRE_LOCAL)) {
1977 			if (ire->ire_ill != ill)
1978 				return (B_FALSE);
1979 		} else  {
1980 			match_flags &= ~MATCH_IRE_TESTHIDDEN;
1981 			/*
1982 			 * We know that ill is not NULL, but ire_ill could be
1983 			 * NULL
1984 			 */
1985 			if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill))
1986 				return (B_FALSE);
1987 		}
1988 	}
1989 
1990 	if ((ire->ire_addr == (addr & mask)) &&
1991 	    ((!(match_flags & MATCH_IRE_GW)) ||
1992 	    (ire->ire_gateway_addr == gateway)) &&
1993 	    ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) &&
1994 	    ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
1995 	    ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask)) &&
1996 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
1997 	    (!is_system_labeled()) ||
1998 	    (tsol_ire_match_gwattr(ire, tsl) == 0))) {
1999 		/* We found the matched IRE */
2000 		return (B_TRUE);
2001 	}
2002 	return (B_FALSE);
2003 }
2004 
2005 /*
2006  * Check if the IRE_LOCAL uses the same ill as another route would use.
2007  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
2008  * then we don't allow this IRE_LOCAL to be used.
2009  * We always return an IRE; will be RTF_REJECT if no route available.
2010  */
2011 ire_t *
2012 ire_alt_local(ire_t *ire, zoneid_t zoneid, const ts_label_t *tsl,
2013     const ill_t *ill, uint_t *generationp)
2014 {
2015 	ip_stack_t	*ipst = ire->ire_ipst;
2016 	ire_t		*alt_ire;
2017 	uint_t		ire_type;
2018 	uint_t		generation;
2019 	uint_t		match_flags;
2020 
2021 	ASSERT(ire->ire_type & IRE_LOCAL);
2022 	ASSERT(ire->ire_ill != NULL);
2023 
2024 	/*
2025 	 * Need to match on everything but local.
2026 	 * This might result in the creation of a IRE_IF_CLONE for the
2027 	 * same address as the IRE_LOCAL when restrict_interzone_loopback is
2028 	 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted
2029 	 * to make sure the IRE_LOCAL is always found first.
2030 	 */
2031 	ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK);
2032 	match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
2033 	if (ill != NULL)
2034 		match_flags |= MATCH_IRE_ILL;
2035 
2036 	if (ire->ire_ipversion == IPV4_VERSION) {
2037 		alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type,
2038 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2039 		    NULL, &generation);
2040 	} else {
2041 		alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type,
2042 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
2043 		    NULL, &generation);
2044 	}
2045 	ASSERT(alt_ire != NULL);
2046 
2047 	if (alt_ire->ire_ill == ire->ire_ill) {
2048 		/* Going out the same ILL - ok to send to IRE_LOCAL */
2049 		ire_refrele(alt_ire);
2050 	} else {
2051 		/* Different ill - ignore IRE_LOCAL */
2052 		ire_refrele(ire);
2053 		ire = alt_ire;
2054 		if (generationp != NULL)
2055 			*generationp = generation;
2056 	}
2057 	return (ire);
2058 }
2059 
2060 boolean_t
2061 ire_find_zoneid(struct radix_node *rn, void *arg)
2062 {
2063 	struct rt_entry *rt = (struct rt_entry *)rn;
2064 	irb_t *irb;
2065 	ire_t *ire;
2066 	ire_ftable_args_t *margs = arg;
2067 
2068 	ASSERT(rt != NULL);
2069 
2070 	irb = &rt->rt_irb;
2071 
2072 	if (irb->irb_ire_cnt == 0)
2073 		return (B_FALSE);
2074 
2075 	rw_enter(&irb->irb_lock, RW_READER);
2076 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2077 		if (IRE_IS_CONDEMNED(ire))
2078 			continue;
2079 
2080 		if (!(ire->ire_type & IRE_INTERFACE))
2081 			continue;
2082 
2083 		if (ire->ire_zoneid != ALL_ZONES &&
2084 		    ire->ire_zoneid != margs->ift_zoneid)
2085 			continue;
2086 
2087 		if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill)
2088 			continue;
2089 
2090 		if (is_system_labeled() &&
2091 		    tsol_ire_match_gwattr(ire, margs->ift_tsl) != 0)
2092 			continue;
2093 
2094 		rw_exit(&irb->irb_lock);
2095 		return (B_TRUE);
2096 	}
2097 	rw_exit(&irb->irb_lock);
2098 	return (B_FALSE);
2099 }
2100 
2101 /*
2102  * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
2103  * gateway address. If ill is non-NULL we also match on it.
2104  * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
2105  */
2106 boolean_t
2107 ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill,
2108     const ts_label_t *tsl, ip_stack_t *ipst, boolean_t lock_held)
2109 {
2110 	struct rt_sockaddr rdst;
2111 	struct rt_entry *rt;
2112 	ire_ftable_args_t margs;
2113 
2114 	ASSERT(ill == NULL || !ill->ill_isv6);
2115 	if (lock_held)
2116 		ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock));
2117 	else
2118 		RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
2119 
2120 	bzero(&rdst, sizeof (rdst));
2121 	rdst.rt_sin_len = sizeof (rdst);
2122 	rdst.rt_sin_family = AF_INET;
2123 	rdst.rt_sin_addr.s_addr = gateway;
2124 
2125 	/*
2126 	 * We only use margs for ill, zoneid, and tsl matching in
2127 	 * ire_find_zoneid
2128 	 */
2129 	bzero(&margs, sizeof (margs));
2130 	margs.ift_ill = ill;
2131 	margs.ift_zoneid = zoneid;
2132 	margs.ift_tsl = tsl;
2133 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
2134 	    ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs);
2135 
2136 	if (!lock_held)
2137 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
2138 
2139 	return (rt != NULL);
2140 }
2141 
2142 /*
2143  * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs.
2144  * The fraction argument tells us what fraction of the IREs to delete.
2145  * Common for IPv4 and IPv6.
2146  * Used when memory backpressure.
2147  */
2148 static void
2149 ire_delete_reclaim(ire_t *ire, char *arg)
2150 {
2151 	ip_stack_t	*ipst = ire->ire_ipst;
2152 	uint_t		fraction = *(uint_t *)arg;
2153 	uint_t		rand;
2154 
2155 	if ((ire->ire_flags & RTF_DYNAMIC) ||
2156 	    (ire->ire_type & IRE_IF_CLONE)) {
2157 
2158 		/* Pick a random number */
2159 		rand = (uint_t)ddi_get_lbolt() +
2160 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256);
2161 
2162 		/* Use truncation */
2163 		if ((rand/fraction)*fraction == rand) {
2164 			IP_STAT(ipst, ip_ire_reclaim_deleted);
2165 			ire_delete(ire);
2166 		}
2167 	}
2168 
2169 }
2170 
2171 /*
2172  * kmem_cache callback to free up memory.
2173  *
2174  * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically
2175  * (RTF_DYNAMIC and IRE_IF_CLONE).
2176  */
2177 static void
2178 ip_ire_reclaim_stack(ip_stack_t *ipst)
2179 {
2180 	uint_t	fraction = ipst->ips_ip_ire_reclaim_fraction;
2181 
2182 	IP_STAT(ipst, ip_ire_reclaim_calls);
2183 
2184 	ire_walk(ire_delete_reclaim, &fraction, ipst);
2185 
2186 	/*
2187 	 * Walk all CONNs that can have a reference on an ire, nce or dce.
2188 	 * Get them to update any stale references to drop any refholds they
2189 	 * have.
2190 	 */
2191 	ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst);
2192 }
2193 
2194 /*
2195  * Called by the memory allocator subsystem directly, when the system
2196  * is running low on memory.
2197  */
2198 /* ARGSUSED */
2199 void
2200 ip_ire_reclaim(void *args)
2201 {
2202 	netstack_handle_t nh;
2203 	netstack_t *ns;
2204 
2205 	netstack_next_init(&nh);
2206 	while ((ns = netstack_next(&nh)) != NULL) {
2207 		ip_ire_reclaim_stack(ns->netstack_ip);
2208 		netstack_rele(ns);
2209 	}
2210 	netstack_next_fini(&nh);
2211 }
2212 
2213 static void
2214 power2_roundup(uint32_t *value)
2215 {
2216 	int i;
2217 
2218 	for (i = 1; i < 31; i++) {
2219 		if (*value <= (1 << i))
2220 			break;
2221 	}
2222 	*value = (1 << i);
2223 }
2224 
2225 /* Global init for all zones */
2226 void
2227 ip_ire_g_init()
2228 {
2229 	/*
2230 	 * Create kmem_caches.  ip_ire_reclaim() and ip_nce_reclaim()
2231 	 * will give disposable IREs back to system when needed.
2232 	 * This needs to be done here before anything else, since
2233 	 * ire_add() expects the cache to be created.
2234 	 */
2235 	ire_cache = kmem_cache_create("ire_cache",
2236 	    sizeof (ire_t), 0, NULL, NULL,
2237 	    ip_ire_reclaim, NULL, NULL, 0);
2238 
2239 	ncec_cache = kmem_cache_create("ncec_cache",
2240 	    sizeof (ncec_t), 0, NULL, NULL,
2241 	    ip_nce_reclaim, NULL, NULL, 0);
2242 	nce_cache = kmem_cache_create("nce_cache",
2243 	    sizeof (nce_t), 0, NULL, NULL,
2244 	    NULL, NULL, NULL, 0);
2245 
2246 	rt_entry_cache = kmem_cache_create("rt_entry",
2247 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
2248 
2249 	/*
2250 	 * Have radix code setup kmem caches etc.
2251 	 */
2252 	rn_init();
2253 }
2254 
2255 void
2256 ip_ire_init(ip_stack_t *ipst)
2257 {
2258 	ire_t	*ire;
2259 	int	error;
2260 
2261 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
2262 
2263 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
2264 
2265 	/*
2266 	 * Make sure that the forwarding table size is a power of 2.
2267 	 * The IRE*_ADDR_HASH() macroes depend on that.
2268 	 */
2269 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
2270 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
2271 
2272 	/*
2273 	 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6.
2274 	 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has
2275 	 * RTF_BLACKHOLE set. We use the latter for transient errors such
2276 	 * as memory allocation failures and tripping on IRE_IS_CONDEMNED
2277 	 * entries.
2278 	 */
2279 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2280 	*ire = ire_null;
2281 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2282 	    RTF_REJECT|RTF_UP, NULL, ipst);
2283 	ASSERT(error == 0);
2284 	ipst->ips_ire_reject_v4 = ire;
2285 
2286 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2287 	*ire = ire_null;
2288 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2289 	    RTF_REJECT|RTF_UP, NULL, ipst);
2290 	ASSERT(error == 0);
2291 	ipst->ips_ire_reject_v6 = ire;
2292 
2293 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2294 	*ire = ire_null;
2295 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2296 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
2297 	ASSERT(error == 0);
2298 	ipst->ips_ire_blackhole_v4 = ire;
2299 
2300 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
2301 	*ire = ire_null;
2302 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
2303 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
2304 	ASSERT(error == 0);
2305 	ipst->ips_ire_blackhole_v6 = ire;
2306 
2307 	rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL);
2308 	rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL);
2309 }
2310 
2311 void
2312 ip_ire_g_fini(void)
2313 {
2314 	kmem_cache_destroy(ire_cache);
2315 	kmem_cache_destroy(ncec_cache);
2316 	kmem_cache_destroy(nce_cache);
2317 	kmem_cache_destroy(rt_entry_cache);
2318 
2319 	rn_fini();
2320 }
2321 
2322 void
2323 ip_ire_fini(ip_stack_t *ipst)
2324 {
2325 	int i;
2326 
2327 	rw_destroy(&ipst->ips_ire_dep_lock);
2328 	rw_destroy(&ipst->ips_ip6_ire_head_lock);
2329 
2330 	ire_refrele_notr(ipst->ips_ire_reject_v6);
2331 	ipst->ips_ire_reject_v6 = NULL;
2332 	ire_refrele_notr(ipst->ips_ire_reject_v4);
2333 	ipst->ips_ire_reject_v4 = NULL;
2334 	ire_refrele_notr(ipst->ips_ire_blackhole_v6);
2335 	ipst->ips_ire_blackhole_v6 = NULL;
2336 	ire_refrele_notr(ipst->ips_ire_blackhole_v4);
2337 	ipst->ips_ire_blackhole_v4 = NULL;
2338 
2339 	/*
2340 	 * Delete all IREs - assumes that the ill/ipifs have
2341 	 * been removed so what remains are just the ftable to handle.
2342 	 */
2343 	ire_walk(ire_delete, NULL, ipst);
2344 
2345 	rn_freehead(ipst->ips_ip_ftable);
2346 	ipst->ips_ip_ftable = NULL;
2347 
2348 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
2349 
2350 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
2351 		irb_t *ptr;
2352 		int j;
2353 
2354 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
2355 			continue;
2356 
2357 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
2358 			ASSERT(ptr[j].irb_ire == NULL);
2359 			rw_destroy(&ptr[j].irb_lock);
2360 		}
2361 		mi_free(ptr);
2362 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
2363 	}
2364 }
2365 
2366 #ifdef DEBUG
2367 void
2368 ire_trace_ref(ire_t *ire)
2369 {
2370 	mutex_enter(&ire->ire_lock);
2371 	if (ire->ire_trace_disable) {
2372 		mutex_exit(&ire->ire_lock);
2373 		return;
2374 	}
2375 
2376 	if (th_trace_ref(ire, ire->ire_ipst)) {
2377 		mutex_exit(&ire->ire_lock);
2378 	} else {
2379 		ire->ire_trace_disable = B_TRUE;
2380 		mutex_exit(&ire->ire_lock);
2381 		ire_trace_cleanup(ire);
2382 	}
2383 }
2384 
2385 void
2386 ire_untrace_ref(ire_t *ire)
2387 {
2388 	mutex_enter(&ire->ire_lock);
2389 	if (!ire->ire_trace_disable)
2390 		th_trace_unref(ire);
2391 	mutex_exit(&ire->ire_lock);
2392 }
2393 
2394 static void
2395 ire_trace_cleanup(const ire_t *ire)
2396 {
2397 	th_trace_cleanup(ire, ire->ire_trace_disable);
2398 }
2399 #endif /* DEBUG */
2400 
2401 /*
2402  * Find, or create if needed, the nce_t pointer to the neighbor cache
2403  * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
2404  * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
2405  * on the next available under-ill (selected by the IPMP rotor) in the
2406  * unicast IPMP case.
2407  *
2408  * If a neighbor-cache entry has to be created (i.e., one does not already
2409  * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
2410  * entry are initialized in nce_add_v4(). The broadcast, multicast, and
2411  * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
2412  * the ncec_t created. The ncec_lladdr is non-null for all link types with
2413  * non-zero ill_phys_addr_length, though the contents may be zero in cases
2414  * where the link-layer type is not known at the time of creation
2415  * (e.g., IRE_IFRESOLVER links)
2416  *
2417  * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
2418  * has the physical broadcast address of the outgoing interface.
2419  * For unicast ire entries,
2420  *   - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
2421  *     ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
2422  *   - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
2423  *     layer resolution is necessary, so that the ncec_t will be in the
2424  *     ND_REACHABLE state
2425  *
2426  * The link layer information needed for broadcast addresses, and for
2427  * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
2428  * never needs re-verification for the lifetime of the ncec_t. These are
2429  * therefore marked NCE_F_NONUD.
2430  *
2431  * The nce returned will be created such that the nce_ill == ill that
2432  * is passed in. Note that the nce itself may not have ncec_ill == ill
2433  * where IPMP links are involved.
2434  */
2435 static nce_t *
2436 ire_nce_init(ill_t *ill, const void *addr, int ire_type)
2437 {
2438 	int		err;
2439 	nce_t		*nce = NULL;
2440 	uint16_t	ncec_flags;
2441 	uchar_t		*hwaddr;
2442 	boolean_t	need_refrele = B_FALSE;
2443 	ill_t		*in_ill = ill;
2444 	boolean_t	is_unicast;
2445 	uint_t		hwaddr_len;
2446 
2447 	is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
2448 	if (IS_IPMP(ill) ||
2449 	    ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
2450 		if ((ill = ipmp_ill_get_xmit_ill(ill, is_unicast)) == NULL)
2451 			return (NULL);
2452 		need_refrele = B_TRUE;
2453 	}
2454 	ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
2455 
2456 	switch (ire_type) {
2457 	case IRE_BROADCAST:
2458 		ASSERT(!ill->ill_isv6);
2459 		ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
2460 		break;
2461 	case IRE_MULTICAST:
2462 		ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
2463 		break;
2464 	}
2465 
2466 	if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
2467 		hwaddr = ill->ill_dest_addr;
2468 	} else {
2469 		hwaddr = NULL;
2470 	}
2471 	hwaddr_len = ill->ill_phys_addr_length;
2472 
2473 retry:
2474 	/* nce_state will be computed by nce_add_common() */
2475 	if (!ill->ill_isv6) {
2476 		err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
2477 		    ncec_flags, ND_UNCHANGED, &nce);
2478 	} else {
2479 		err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
2480 		    ncec_flags, ND_UNCHANGED, &nce);
2481 	}
2482 
2483 	switch (err) {
2484 	case 0:
2485 		break;
2486 	case EEXIST:
2487 		/*
2488 		 * When subnets change or partially overlap what was once
2489 		 * a broadcast address could now be a unicast, or vice versa.
2490 		 */
2491 		if (((ncec_flags ^ nce->nce_common->ncec_flags) &
2492 		    NCE_F_BCAST) != 0) {
2493 			ASSERT(!ill->ill_isv6);
2494 			ncec_delete(nce->nce_common);
2495 			nce_refrele(nce);
2496 			goto retry;
2497 		}
2498 		break;
2499 	default:
2500 		DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
2501 		if (need_refrele)
2502 			ill_refrele(ill);
2503 		return (NULL);
2504 	}
2505 	/*
2506 	 * If the ill was an under-ill of an IPMP group, we need to verify
2507 	 * that it is still active so that we select an active interface in
2508 	 * the group. However, since ipmp_ill_is_active ASSERTs for
2509 	 * IS_UNDER_IPMP(), we first need to verify that the ill is an
2510 	 * under-ill, and since this is being done in the data path, the
2511 	 * only way to ascertain this is by holding the ill_g_lock.
2512 	 */
2513 	rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
2514 	mutex_enter(&ill->ill_lock);
2515 	mutex_enter(&ill->ill_phyint->phyint_lock);
2516 	if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
2517 		/*
2518 		 * need_refrele implies that the under ill was selected by
2519 		 * ipmp_ill_get_xmit_ill() because either the in_ill was an
2520 		 * ipmp_ill, or we are sending a non-unicast packet on
2521 		 * an under_ill. However, when we get here, the ill selected by
2522 		 * ipmp_ill_get_xmit_ill  was pulled out of the active set
2523 		 * (for unicast)  or cast_ill nomination (for
2524 		 * !unicast) after it was  picked as the outgoing ill.
2525 		 * We have to pick an active interface and/or cast_ill in the
2526 		 * group.
2527 		 */
2528 		mutex_exit(&ill->ill_phyint->phyint_lock);
2529 		nce_delete(nce);
2530 		mutex_exit(&ill->ill_lock);
2531 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2532 		nce_refrele(nce);
2533 		ill_refrele(ill);
2534 		if ((ill = ipmp_ill_get_xmit_ill(in_ill, is_unicast)) == NULL)
2535 			return (NULL);
2536 		goto retry;
2537 	} else {
2538 		mutex_exit(&ill->ill_phyint->phyint_lock);
2539 		mutex_exit(&ill->ill_lock);
2540 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
2541 	}
2542 done:
2543 	ASSERT(nce->nce_ill == ill);
2544 	if (need_refrele)
2545 		ill_refrele(ill);
2546 	return (nce);
2547 }
2548 
2549 nce_t *
2550 arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
2551 {
2552 	return (ire_nce_init(ill, &addr4, ire_type));
2553 }
2554 
2555 nce_t *
2556 ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
2557 {
2558 	ASSERT((ire_type & IRE_BROADCAST) == 0);
2559 	return (ire_nce_init(ill, addr6, ire_type));
2560 }
2561 
2562 /*
2563  * The caller should hold irb_lock as a writer if the ire is in a bucket.
2564  */
2565 void
2566 ire_make_condemned(ire_t *ire)
2567 {
2568 	ip_stack_t	*ipst = ire->ire_ipst;
2569 
2570 	mutex_enter(&ire->ire_lock);
2571 	ASSERT(ire->ire_bucket == NULL ||
2572 	    RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
2573 	ASSERT(!IRE_IS_CONDEMNED(ire));
2574 	ire->ire_generation = IRE_GENERATION_CONDEMNED;
2575 	/* Count how many condemned ires for kmem_cache callback */
2576 	atomic_add_32(&ipst->ips_num_ire_condemned, 1);
2577 	mutex_exit(&ire->ire_lock);
2578 }
2579 
2580 /*
2581  * Increment the generation avoiding the special condemned value
2582  */
2583 void
2584 ire_increment_generation(ire_t *ire)
2585 {
2586 	uint_t generation;
2587 
2588 	mutex_enter(&ire->ire_lock);
2589 	/*
2590 	 * Even though the caller has a hold it can't prevent a concurrent
2591 	 * ire_delete marking the IRE condemned
2592 	 */
2593 	if (!IRE_IS_CONDEMNED(ire)) {
2594 		generation = ire->ire_generation + 1;
2595 		if (generation == IRE_GENERATION_CONDEMNED)
2596 			generation = IRE_GENERATION_INITIAL;
2597 		ASSERT(generation != IRE_GENERATION_VERIFY);
2598 		ire->ire_generation = generation;
2599 	}
2600 	mutex_exit(&ire->ire_lock);
2601 }
2602 
2603 /*
2604  * Increment ire_generation on all the IRE_MULTICASTs
2605  * Used when the default multicast interface (as determined by
2606  * ill_lookup_multicast) might have changed.
2607  *
2608  * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
2609  * ill unplumb.
2610  */
2611 void
2612 ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
2613 {
2614 	ill_t	*ill;
2615 	ill_walk_context_t ctx;
2616 
2617 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2618 	if (isv6)
2619 		ill = ILL_START_WALK_V6(&ctx, ipst);
2620 	else
2621 		ill = ILL_START_WALK_V4(&ctx, ipst);
2622 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
2623 		if (ILL_IS_CONDEMNED(ill))
2624 			continue;
2625 		if (ill->ill_ire_multicast != NULL)
2626 			ire_increment_generation(ill->ill_ire_multicast);
2627 	}
2628 	rw_exit(&ipst->ips_ill_g_lock);
2629 }
2630 
2631 /*
2632  * Return a held IRE_NOROUTE with RTF_REJECT set
2633  */
2634 ire_t *
2635 ire_reject(ip_stack_t *ipst, boolean_t isv6)
2636 {
2637 	ire_t *ire;
2638 
2639 	if (isv6)
2640 		ire = ipst->ips_ire_reject_v6;
2641 	else
2642 		ire = ipst->ips_ire_reject_v4;
2643 
2644 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2645 	ire_refhold(ire);
2646 	return (ire);
2647 }
2648 
2649 /*
2650  * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
2651  */
2652 ire_t *
2653 ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
2654 {
2655 	ire_t *ire;
2656 
2657 	if (isv6)
2658 		ire = ipst->ips_ire_blackhole_v6;
2659 	else
2660 		ire = ipst->ips_ire_blackhole_v4;
2661 
2662 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
2663 	ire_refhold(ire);
2664 	return (ire);
2665 }
2666 
2667 /*
2668  * Return a held IRE_MULTICAST.
2669  */
2670 ire_t *
2671 ire_multicast(ill_t *ill)
2672 {
2673 	ire_t *ire = ill->ill_ire_multicast;
2674 
2675 	ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
2676 	if (ire == NULL)
2677 		ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
2678 	else
2679 		ire_refhold(ire);
2680 	return (ire);
2681 }
2682 
2683 /*
2684  * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
2685  * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
2686  * This can return an RTF_REJECT|RTF_BLACKHOLE.
2687  * The returned IRE is held.
2688  * The assumption is that ip_select_route() has been called and returned the
2689  * IRE (thus ip_select_route would have set up the ire_dep* information.)
2690  * If some IRE is deleteted then ire_dep_remove() will have been called and
2691  * we might not find a nexthop IRE, in which case we return NULL.
2692  */
2693 ire_t *
2694 ire_nexthop(ire_t *ire)
2695 {
2696 	ip_stack_t	*ipst = ire->ire_ipst;
2697 
2698 	/* Acquire lock to walk ire_dep_parent */
2699 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
2700 	while (ire != NULL) {
2701 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2702 			goto done;
2703 		}
2704 		/*
2705 		 * If we find an IRE_ONLINK we are done. This includes
2706 		 * the case of IRE_MULTICAST.
2707 		 * Note that in order to send packets we need a host-specific
2708 		 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
2709 		 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
2710 		 * was not host specific.
2711 		 * However, ip_rts_request doesn't want to send packets
2712 		 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
2713 		 * it needs an IRE_IF_ALL to get to the ill. Thus
2714 		 * we return IRE_IF_ALL that are not host specific here.
2715 		 */
2716 		if (ire->ire_type & IRE_ONLINK)
2717 			goto done;
2718 		ire = ire->ire_dep_parent;
2719 	}
2720 	rw_exit(&ipst->ips_ire_dep_lock);
2721 	return (NULL);
2722 
2723 done:
2724 	ire_refhold(ire);
2725 	rw_exit(&ipst->ips_ire_dep_lock);
2726 	return (ire);
2727 }
2728 
2729 /*
2730  * Find the ill used to send packets. This will be NULL in case
2731  * of a reject or blackhole.
2732  * The returned ill is held; caller needs to do ill_refrele when done.
2733  */
2734 ill_t *
2735 ire_nexthop_ill(ire_t *ire)
2736 {
2737 	ill_t		*ill;
2738 
2739 	ire = ire_nexthop(ire);
2740 	if (ire == NULL)
2741 		return (NULL);
2742 
2743 	/* ire_ill can not change for an existing ire */
2744 	ill = ire->ire_ill;
2745 	if (ill != NULL)
2746 		ill_refhold(ill);
2747 	ire_refrele(ire);
2748 	return (ill);
2749 }
2750 
2751 #ifdef DEBUG
2752 static boolean_t
2753 parent_has_child(ire_t *parent, ire_t *child)
2754 {
2755 	ire_t	*ire;
2756 	ire_t	*prev;
2757 
2758 	ire = parent->ire_dep_children;
2759 	prev = NULL;
2760 	while (ire != NULL) {
2761 		if (prev == NULL) {
2762 			ASSERT(ire->ire_dep_sib_ptpn ==
2763 			    &(parent->ire_dep_children));
2764 		} else {
2765 			ASSERT(ire->ire_dep_sib_ptpn ==
2766 			    &(prev->ire_dep_sib_next));
2767 		}
2768 		if (ire == child)
2769 			return (B_TRUE);
2770 		prev = ire;
2771 		ire = ire->ire_dep_sib_next;
2772 	}
2773 	return (B_FALSE);
2774 }
2775 
2776 static void
2777 ire_dep_verify(ire_t *ire)
2778 {
2779 	ire_t		*parent = ire->ire_dep_parent;
2780 	ire_t		*child = ire->ire_dep_children;
2781 
2782 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
2783 	    ire->ire_ipversion == IPV6_VERSION);
2784 	if (parent != NULL) {
2785 		ASSERT(parent->ire_ipversion == IPV4_VERSION ||
2786 		    parent->ire_ipversion == IPV6_VERSION);
2787 		ASSERT(parent->ire_refcnt >= 1);
2788 		ASSERT(parent_has_child(parent, ire));
2789 	}
2790 	if (child != NULL) {
2791 		ASSERT(child->ire_ipversion == IPV4_VERSION ||
2792 		    child->ire_ipversion == IPV6_VERSION);
2793 		ASSERT(child->ire_dep_parent == ire);
2794 		ASSERT(child->ire_dep_sib_ptpn != NULL);
2795 		ASSERT(parent_has_child(ire, child));
2796 	}
2797 }
2798 #endif /* DEBUG */
2799 
2800 /*
2801  * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
2802  */
2803 void
2804 ire_dep_remove(ire_t *ire)
2805 {
2806 	ip_stack_t	*ipst = ire->ire_ipst;
2807 	ire_t		*parent = ire->ire_dep_parent;
2808 	ire_t		*next;
2809 	nce_t		*nce;
2810 
2811 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2812 	ASSERT(ire->ire_dep_parent != NULL);
2813 	ASSERT(ire->ire_dep_sib_ptpn != NULL);
2814 
2815 #ifdef DEBUG
2816 	ire_dep_verify(ire);
2817 	ire_dep_verify(parent);
2818 #endif
2819 
2820 	next = ire->ire_dep_sib_next;
2821 	if (next != NULL)
2822 		next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
2823 
2824 	ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
2825 	*(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
2826 
2827 	ire->ire_dep_sib_ptpn = NULL;
2828 	ire->ire_dep_sib_next = NULL;
2829 
2830 	mutex_enter(&ire->ire_lock);
2831 	parent = ire->ire_dep_parent;
2832 	ire->ire_dep_parent = NULL;
2833 	mutex_exit(&ire->ire_lock);
2834 
2835 	/*
2836 	 * Make sure all our children, grandchildren, etc set
2837 	 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
2838 	 * we can no longer guarantee than the children have a current
2839 	 * ire_nce_cache and ire_nexthop_ill().
2840 	 */
2841 	if (ire->ire_dep_children != NULL)
2842 		ire_dep_invalidate_children(ire->ire_dep_children);
2843 
2844 	/*
2845 	 * Since the parent is gone we make sure we clear ire_nce_cache.
2846 	 * We can clear it under ire_lock even if the IRE is used
2847 	 */
2848 	mutex_enter(&ire->ire_lock);
2849 	nce = ire->ire_nce_cache;
2850 	ire->ire_nce_cache = NULL;
2851 	mutex_exit(&ire->ire_lock);
2852 	if (nce != NULL)
2853 		nce_refrele(nce);
2854 
2855 #ifdef DEBUG
2856 	ire_dep_verify(ire);
2857 	ire_dep_verify(parent);
2858 #endif
2859 
2860 	ire_refrele_notr(parent);
2861 	ire_refrele_notr(ire);
2862 }
2863 
2864 /*
2865  * Insert the child in the linkage of the parent
2866  */
2867 static void
2868 ire_dep_parent_insert(ire_t *child, ire_t *parent)
2869 {
2870 	ip_stack_t	*ipst = child->ire_ipst;
2871 	ire_t		*next;
2872 
2873 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
2874 	ASSERT(child->ire_dep_parent == NULL);
2875 
2876 #ifdef DEBUG
2877 	ire_dep_verify(child);
2878 	ire_dep_verify(parent);
2879 #endif
2880 	/* No parents => no siblings */
2881 	ASSERT(child->ire_dep_sib_ptpn == NULL);
2882 	ASSERT(child->ire_dep_sib_next == NULL);
2883 
2884 	ire_refhold_notr(parent);
2885 	ire_refhold_notr(child);
2886 
2887 	/* Head insertion */
2888 	next = parent->ire_dep_children;
2889 	if (next != NULL) {
2890 		ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
2891 		child->ire_dep_sib_next = next;
2892 		next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
2893 	}
2894 	parent->ire_dep_children = child;
2895 	child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
2896 
2897 	mutex_enter(&child->ire_lock);
2898 	child->ire_dep_parent = parent;
2899 	mutex_exit(&child->ire_lock);
2900 
2901 #ifdef DEBUG
2902 	ire_dep_verify(child);
2903 	ire_dep_verify(parent);
2904 #endif
2905 }
2906 
2907 
2908 /*
2909  * Given count worth of ires and generations, build ire_dep_* relationships
2910  * from ires[0] to ires[count-1]. Record generations[i+1] in
2911  * ire_dep_parent_generation for ires[i].
2912  * We graft onto an existing parent chain by making sure that we don't
2913  * touch ire_dep_parent for ires[count-1].
2914  *
2915  * We check for any condemned ire_generation count and return B_FALSE in
2916  * that case so that the caller can tear it apart.
2917  *
2918  * Note that generations[0] is not used. Caller handles that.
2919  */
2920 boolean_t
2921 ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
2922 {
2923 	ire_t		*ire = ires[0];
2924 	ip_stack_t	*ipst;
2925 	uint_t		i;
2926 
2927 	ASSERT(count > 0);
2928 	if (count == 1) {
2929 		/* No work to do */
2930 		return (B_TRUE);
2931 	}
2932 	ipst = ire->ire_ipst;
2933 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2934 	/*
2935 	 * Do not remove the linkage for any existing parent chain i.e.,
2936 	 * ires[count-1] is left alone.
2937 	 */
2938 	for (i = 0; i < count-1; i++) {
2939 		/* Remove existing parent if we need to change it */
2940 		if (ires[i]->ire_dep_parent != NULL &&
2941 		    ires[i]->ire_dep_parent != ires[i+1])
2942 			ire_dep_remove(ires[i]);
2943 	}
2944 
2945 	for (i = 0; i < count - 1; i++) {
2946 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2947 		    ires[i]->ire_ipversion == IPV6_VERSION);
2948 		/* Does it need to change? */
2949 		if (ires[i]->ire_dep_parent != ires[i+1])
2950 			ire_dep_parent_insert(ires[i], ires[i+1]);
2951 
2952 		mutex_enter(&ires[i+1]->ire_lock);
2953 		if (IRE_IS_CONDEMNED(ires[i+1])) {
2954 			mutex_exit(&ires[i+1]->ire_lock);
2955 			rw_exit(&ipst->ips_ire_dep_lock);
2956 			return (B_FALSE);
2957 		}
2958 		mutex_exit(&ires[i+1]->ire_lock);
2959 
2960 		mutex_enter(&ires[i]->ire_lock);
2961 		ires[i]->ire_dep_parent_generation = generations[i+1];
2962 		mutex_exit(&ires[i]->ire_lock);
2963 	}
2964 	rw_exit(&ipst->ips_ire_dep_lock);
2965 	return (B_TRUE);
2966 }
2967 
2968 /*
2969  * Given count worth of ires, unbuild ire_dep_* relationships
2970  * from ires[0] to ires[count-1].
2971  */
2972 void
2973 ire_dep_unbuild(ire_t *ires[], uint_t count)
2974 {
2975 	ip_stack_t	*ipst;
2976 	uint_t		i;
2977 
2978 	if (count == 0) {
2979 		/* No work to do */
2980 		return;
2981 	}
2982 	ipst = ires[0]->ire_ipst;
2983 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
2984 	for (i = 0; i < count; i++) {
2985 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
2986 		    ires[i]->ire_ipversion == IPV6_VERSION);
2987 		if (ires[i]->ire_dep_parent != NULL)
2988 			ire_dep_remove(ires[i]);
2989 		mutex_enter(&ires[i]->ire_lock);
2990 		ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
2991 		mutex_exit(&ires[i]->ire_lock);
2992 	}
2993 	rw_exit(&ipst->ips_ire_dep_lock);
2994 }
2995 
2996 /*
2997  * Both the forwarding and the outbound code paths can trip on
2998  * a condemned NCE, in which case we call this function.
2999  * We have two different behaviors: if the NCE was UNREACHABLE
3000  * it is an indication that something failed. In that case
3001  * we see if we should look for a different IRE (for example,
3002  * delete any matching redirect IRE, or try a different
3003  * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
3004  * different IRE will be picked next time we send/forward.
3005  *
3006  * If we are called by the output path then fail_if_better is set
3007  * and we return NULL if there could be a better IRE. This is because the
3008  * output path retries the IRE lookup. (The input/forward path can not retry.)
3009  *
3010  * If the NCE was not unreachable then we pick/allocate a
3011  * new (most likely ND_INITIAL) NCE and proceed with it.
3012  *
3013  * ipha/ip6h are needed for multicast packets; ipha needs to be
3014  * set for IPv4 and ip6h needs to be set for IPv6 packets.
3015  */
3016 nce_t *
3017 ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
3018     boolean_t fail_if_better)
3019 {
3020 	if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
3021 		if (ire_no_good(ire) && fail_if_better) {
3022 			/*
3023 			 * Did some changes, or ECMP likely to exist.
3024 			 * Make ip_output look for a different IRE
3025 			 */
3026 			return (NULL);
3027 		}
3028 	}
3029 	if (ire_revalidate_nce(ire) == ENETUNREACH) {
3030 		/* The ire_dep_parent chain went bad, or no memory? */
3031 		(void) ire_no_good(ire);
3032 		return (NULL);
3033 	}
3034 	if (ire->ire_ipversion == IPV4_VERSION) {
3035 		ASSERT(ipha != NULL);
3036 		nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
3037 	} else {
3038 		ASSERT(ip6h != NULL);
3039 		nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
3040 	}
3041 
3042 	if (nce == NULL)
3043 		return (NULL);
3044 	if (nce->nce_is_condemned) {
3045 		nce_refrele(nce);
3046 		return (NULL);
3047 	}
3048 	return (nce);
3049 }
3050 
3051 /*
3052  * The caller has found that the ire is bad, either due to a reference to an NCE
3053  * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved.
3054  * We update things so a subsequent attempt to send to the destination
3055  * is likely to find different IRE, or that a new NCE would be created.
3056  *
3057  * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
3058  * find a different route (either due to having deleted a redirect, or there
3059  * being ECMP routes.)
3060  *
3061  * If we have a redirect (RTF_DYNAMIC) we delete it.
3062  * Otherwise we increment ire_badcnt and increment the generation number so
3063  * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
3064  * into account in the route selection when we have multiple choices (multiple
3065  * default routes or ECMP in general).
3066  * Any time ip_select_route find an ire with a condemned ire_nce_cache
3067  * (e.g., if no equal cost route to the bad one) ip_select_route will make
3068  * sure the NCE is revalidated to avoid getting stuck on a
3069  * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
3070  */
3071 boolean_t
3072 ire_no_good(ire_t *ire)
3073 {
3074 	ip_stack_t	*ipst = ire->ire_ipst;
3075 	ire_t		*ire2;
3076 	nce_t		*nce;
3077 
3078 	if (ire->ire_flags & RTF_DYNAMIC) {
3079 		ire_delete(ire);
3080 		return (B_TRUE);
3081 	}
3082 	if (ire->ire_flags & RTF_INDIRECT) {
3083 		/* Check if next IRE is a redirect */
3084 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3085 		if (ire->ire_dep_parent != NULL &&
3086 		    (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
3087 			ire2 = ire->ire_dep_parent;
3088 			ire_refhold(ire2);
3089 		} else {
3090 			ire2 = NULL;
3091 		}
3092 		rw_exit(&ipst->ips_ire_dep_lock);
3093 		if (ire2 != NULL) {
3094 			ire_delete(ire2);
3095 			ire_refrele(ire2);
3096 			return (B_TRUE);
3097 		}
3098 	}
3099 	/*
3100 	 * No redirect involved. Increment badcnt so that if we have ECMP
3101 	 * routes we are likely to pick a different one for the next packet.
3102 	 *
3103 	 * If the NCE is unreachable and condemned we should drop the reference
3104 	 * to it so that a new NCE can be created.
3105 	 *
3106 	 * Finally we increment the generation number so that any ixa_ire
3107 	 * cache will be revalidated.
3108 	 */
3109 	mutex_enter(&ire->ire_lock);
3110 	ire->ire_badcnt++;
3111 	ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
3112 	nce = ire->ire_nce_cache;
3113 	if (nce != NULL && nce->nce_is_condemned &&
3114 	    nce->nce_common->ncec_state == ND_UNREACHABLE)
3115 		ire->ire_nce_cache = NULL;
3116 	else
3117 		nce = NULL;
3118 	mutex_exit(&ire->ire_lock);
3119 	if (nce != NULL)
3120 		nce_refrele(nce);
3121 
3122 	ire_increment_generation(ire);
3123 	ire_dep_incr_generation(ire);
3124 
3125 	return (ire->ire_bucket->irb_ire_cnt > 1);
3126 }
3127 
3128 /*
3129  * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
3130  * ire_dep_parent_generation.
3131  * If they all match we just return ire_generation from the topmost IRE.
3132  * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
3133  * above the mismatch to IRE_GENERATION_VERIFY and also returning
3134  * IRE_GENERATION_VERIFY.
3135  */
3136 uint_t
3137 ire_dep_validate_generations(ire_t *ire)
3138 {
3139 	ip_stack_t	*ipst = ire->ire_ipst;
3140 	uint_t		generation;
3141 	ire_t		*ire1;
3142 
3143 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3144 	generation = ire->ire_generation;	/* Assuming things match */
3145 	for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
3146 		ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
3147 		    ire1->ire_ipversion == IPV6_VERSION);
3148 		if (ire1->ire_dep_parent == NULL)
3149 			break;
3150 		if (ire1->ire_dep_parent_generation !=
3151 		    ire1->ire_dep_parent->ire_generation)
3152 			goto mismatch;
3153 	}
3154 	rw_exit(&ipst->ips_ire_dep_lock);
3155 	return (generation);
3156 
3157 mismatch:
3158 	generation = IRE_GENERATION_VERIFY;
3159 	/* Fill from top down to the mismatch with _VERIFY */
3160 	while (ire != ire1) {
3161 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3162 		    ire->ire_ipversion == IPV6_VERSION);
3163 		mutex_enter(&ire->ire_lock);
3164 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3165 		mutex_exit(&ire->ire_lock);
3166 		ire = ire->ire_dep_parent;
3167 	}
3168 	rw_exit(&ipst->ips_ire_dep_lock);
3169 	return (generation);
3170 }
3171 
3172 /*
3173  * Used when we need to return an ire with ire_dep_parent, but we
3174  * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
3175  * Using IRE_GENERATION_VERIFY means that next time we'll redo the
3176  * recursive lookup.
3177  */
3178 void
3179 ire_dep_invalidate_generations(ire_t *ire)
3180 {
3181 	ip_stack_t	*ipst = ire->ire_ipst;
3182 
3183 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3184 	while (ire != NULL) {
3185 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
3186 		    ire->ire_ipversion == IPV6_VERSION);
3187 		mutex_enter(&ire->ire_lock);
3188 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3189 		mutex_exit(&ire->ire_lock);
3190 		ire = ire->ire_dep_parent;
3191 	}
3192 	rw_exit(&ipst->ips_ire_dep_lock);
3193 }
3194 
3195 /* Set _VERIFY ire_dep_parent_generation for all children recursively */
3196 static void
3197 ire_dep_invalidate_children(ire_t *child)
3198 {
3199 	ip_stack_t	*ipst = child->ire_ipst;
3200 
3201 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
3202 	/* Depth first */
3203 	if (child->ire_dep_children != NULL)
3204 		ire_dep_invalidate_children(child->ire_dep_children);
3205 
3206 	while (child != NULL) {
3207 		mutex_enter(&child->ire_lock);
3208 		child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
3209 		mutex_exit(&child->ire_lock);
3210 		child = child->ire_dep_sib_next;
3211 	}
3212 }
3213 
3214 static void
3215 ire_dep_increment_children(ire_t *child)
3216 {
3217 	ip_stack_t	*ipst = child->ire_ipst;
3218 
3219 	ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
3220 	/* Depth first */
3221 	if (child->ire_dep_children != NULL)
3222 		ire_dep_increment_children(child->ire_dep_children);
3223 
3224 	while (child != NULL) {
3225 		if (!IRE_IS_CONDEMNED(child))
3226 			ire_increment_generation(child);
3227 		child = child->ire_dep_sib_next;
3228 	}
3229 }
3230 
3231 /*
3232  * Walk all the children of this ire recursively and increment their
3233  * generation number.
3234  */
3235 void
3236 ire_dep_incr_generation(ire_t *parent)
3237 {
3238 	ip_stack_t	*ipst = parent->ire_ipst;
3239 
3240 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3241 	if (parent->ire_dep_children != NULL)
3242 		ire_dep_increment_children(parent->ire_dep_children);
3243 	rw_exit(&ipst->ips_ire_dep_lock);
3244 }
3245 
3246 /*
3247  * Get a new ire_nce_cache for this IRE as well as its nexthop.
3248  * Returns zero if it succeeds. Can fail due to lack of memory or when
3249  * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
3250  * cases.
3251  *
3252  * In the in.mpathd case, the ire will have ire_testhidden
3253  * set; so we should create the ncec for the underlying ill.
3254  *
3255  * Note that the error returned by ire_revalidate_nce() is ignored by most
3256  * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
3257  * error to mark potentially bad ire's. For all the other callers, an
3258  * error return could indicate a transient condition like ENOMEM, or could
3259  * be the result of an interface that is going down/unplumbing. In the former
3260  * case (transient error), we would leave the old stale ire/ire_nce_cache
3261  * in place, and possibly use incorrect link-layer information to send packets
3262  * but would eventually recover. In the latter case (ill down/replumb),
3263  * ire_revalidate_nce() might return a condemned nce back, but we would then
3264  * recover in the packet output path.
3265  */
3266 int
3267 ire_revalidate_nce(ire_t *ire)
3268 {
3269 	nce_t		*nce, *old_nce;
3270 	ire_t		*nexthop;
3271 
3272 	/*
3273 	 * For multicast we conceptually have an NCE but we don't store it
3274 	 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
3275 	 */
3276 	if (ire->ire_type & IRE_MULTICAST)
3277 		return (0);
3278 
3279 	/* ire_testhidden should only be set on under-interfaces */
3280 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3281 
3282 	nexthop = ire_nexthop(ire);
3283 	if (nexthop == NULL) {
3284 		/* The route is potentially bad */
3285 		(void) ire_no_good(ire);
3286 		return (ENETUNREACH);
3287 	}
3288 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3289 		ASSERT(ire->ire_ill != NULL);
3290 
3291 		if (ire->ire_ipversion == IPV4_VERSION)
3292 			nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
3293 		else
3294 			nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
3295 	} else {
3296 		ASSERT(nexthop->ire_type & IRE_ONLINK);
3297 		if (ire->ire_ipversion == IPV4_VERSION) {
3298 			nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
3299 			    nexthop->ire_type);
3300 		} else {
3301 			nce = ndp_nce_init(nexthop->ire_ill,
3302 			    &nexthop->ire_addr_v6, nexthop->ire_type);
3303 		}
3304 	}
3305 	if (nce == NULL) {
3306 		/*
3307 		 * Leave the old stale one in place to avoid a NULL
3308 		 * ire_nce_cache.
3309 		 */
3310 		ire_refrele(nexthop);
3311 		return (ENOMEM);
3312 	}
3313 
3314 	if (nexthop != ire) {
3315 		/* Update the nexthop ire */
3316 		mutex_enter(&nexthop->ire_lock);
3317 		old_nce = nexthop->ire_nce_cache;
3318 		if (!IRE_IS_CONDEMNED(nexthop)) {
3319 			nce_refhold(nce);
3320 			nexthop->ire_nce_cache = nce;
3321 		} else {
3322 			nexthop->ire_nce_cache = NULL;
3323 		}
3324 		mutex_exit(&nexthop->ire_lock);
3325 		if (old_nce != NULL)
3326 			nce_refrele(old_nce);
3327 	}
3328 	ire_refrele(nexthop);
3329 
3330 	mutex_enter(&ire->ire_lock);
3331 	old_nce = ire->ire_nce_cache;
3332 	if (!IRE_IS_CONDEMNED(ire)) {
3333 		nce_refhold(nce);
3334 		ire->ire_nce_cache = nce;
3335 	} else {
3336 		ire->ire_nce_cache = NULL;
3337 	}
3338 	mutex_exit(&ire->ire_lock);
3339 	if (old_nce != NULL)
3340 		nce_refrele(old_nce);
3341 
3342 	nce_refrele(nce);
3343 	return (0);
3344 }
3345 
3346 /*
3347  * Get a held nce for a given ire.
3348  * In the common case this is just from ire_nce_cache.
3349  * For IRE_MULTICAST this needs to do an explicit lookup since we do not
3350  * have an IRE_MULTICAST per address.
3351  * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
3352  * so they can check whether the NCE went unreachable (as opposed to was
3353  * condemned for some other reason).
3354  */
3355 nce_t *
3356 ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
3357 {
3358 	nce_t	*nce;
3359 
3360 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
3361 		return (NULL);
3362 
3363 	/* ire_testhidden should only be set on under-interfaces */
3364 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
3365 
3366 	mutex_enter(&ire->ire_lock);
3367 	nce = ire->ire_nce_cache;
3368 	if (nce != NULL) {
3369 		nce_refhold(nce);
3370 		mutex_exit(&ire->ire_lock);
3371 		return (nce);
3372 	}
3373 	mutex_exit(&ire->ire_lock);
3374 
3375 	if (ire->ire_type & IRE_MULTICAST) {
3376 		ASSERT(ire->ire_ill != NULL);
3377 
3378 		if (ire->ire_ipversion == IPV4_VERSION) {
3379 			ASSERT(v6nexthop == NULL);
3380 
3381 			nce = arp_nce_init(ire->ire_ill, v4nexthop,
3382 			    ire->ire_type);
3383 		} else {
3384 			ASSERT(v6nexthop != NULL);
3385 			ASSERT(v4nexthop == 0);
3386 			nce = ndp_nce_init(ire->ire_ill, v6nexthop,
3387 			    ire->ire_type);
3388 		}
3389 		return (nce);
3390 	}
3391 	return (NULL);
3392 }
3393 
3394 nce_t *
3395 ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
3396 {
3397 	ipha_t		*ipha;
3398 	ip6_t		*ip6h;
3399 
3400 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3401 		ipha = (ipha_t *)mp->b_rptr;
3402 		return (ire_to_nce(ire, ipha->ipha_dst, NULL));
3403 	} else {
3404 		ip6h = (ip6_t *)mp->b_rptr;
3405 		return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
3406 	}
3407 }
3408 
3409 /*
3410  * Given an IRE_INTERFACE (that matches more than one address) create
3411  * and return an IRE_IF_CLONE for the specific address.
3412  * Return the generation number.
3413  * Returns NULL is no memory for the IRE.
3414  * Handles both IPv4 and IPv6.
3415  */
3416 ire_t *
3417 ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
3418 {
3419 	ire_t		*ire;
3420 	ire_t		*nire;
3421 
3422 	if (ire_if->ire_ipversion == IPV4_VERSION) {
3423 		ipaddr_t	v4addr;
3424 		ipaddr_t	mask = IP_HOST_MASK;
3425 
3426 		ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
3427 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
3428 
3429 		ire = ire_create(
3430 		    (uchar_t *)&v4addr,			/* dest address */
3431 		    (uchar_t *)&mask,			/* mask */
3432 		    (uchar_t *)&ire_if->ire_gateway_addr,
3433 		    IRE_IF_CLONE,			/* IRE type */
3434 		    ire_if->ire_ill,
3435 		    ire_if->ire_zoneid,
3436 		    ire_if->ire_flags | RTF_HOST,
3437 		    NULL,		/* No security attr for IRE_IF_ALL */
3438 		    ire_if->ire_ipst);
3439 	} else {
3440 		ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
3441 		ire = ire_create_v6(
3442 		    addr,				/* dest address */
3443 		    &ipv6_all_ones,			/* mask */
3444 		    &ire_if->ire_gateway_addr_v6,	/* gateway addr */
3445 		    IRE_IF_CLONE,			/* IRE type */
3446 		    ire_if->ire_ill,
3447 		    ire_if->ire_zoneid,
3448 		    ire_if->ire_flags | RTF_HOST,
3449 		    NULL,		/* No security attr for IRE_IF_ALL */
3450 		    ire_if->ire_ipst);
3451 	}
3452 	if (ire == NULL)
3453 		return (NULL);
3454 
3455 	/* Take the metrics, in particular the mtu, from the IRE_IF */
3456 	ire->ire_metrics = ire_if->ire_metrics;
3457 
3458 	nire = ire_add(ire);
3459 	if (nire == NULL) /* Some failure */
3460 		return (NULL);
3461 
3462 	if (generationp != NULL)
3463 		*generationp = nire->ire_generation;
3464 
3465 	/*
3466 	 * Make sure races don't add a duplicate by
3467 	 * catching the case when an identical was returned.
3468 	 */
3469 	if (nire != ire) {
3470 		ASSERT(nire->ire_identical_ref > 1);
3471 		ire_delete(nire);
3472 	}
3473 	return (nire);
3474 }
3475 
3476 /*
3477  * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
3478  * ire_dep_children (just walk the ire_dep_sib_next since they are all
3479  * immediate children.)
3480  * Since we hold a lock while we remove them we need to defer the actual
3481  * calls to ire_delete() until we have dropped the lock. This makes things
3482  * less efficient since we restart at the top after dropping the lock. But
3483  * we only run when an IRE_INTERFACE is deleted which is infrquent.
3484  *
3485  * Note that ire_dep_children can be any mixture of offlink routes and
3486  * IRE_IF_CLONE entries.
3487  */
3488 void
3489 ire_dep_delete_if_clone(ire_t *parent)
3490 {
3491 	ip_stack_t	*ipst = parent->ire_ipst;
3492 	ire_t		*child, *next;
3493 
3494 restart:
3495 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
3496 	if (parent->ire_dep_children == NULL) {
3497 		rw_exit(&ipst->ips_ire_dep_lock);
3498 		return;
3499 	}
3500 	child = parent->ire_dep_children;
3501 	while (child != NULL) {
3502 		next = child->ire_dep_sib_next;
3503 		if ((child->ire_type & IRE_IF_CLONE) &&
3504 		    !IRE_IS_CONDEMNED(child)) {
3505 			ire_refhold(child);
3506 			rw_exit(&ipst->ips_ire_dep_lock);
3507 			ire_delete(child);
3508 			ASSERT(IRE_IS_CONDEMNED(child));
3509 			ire_refrele(child);
3510 			goto restart;
3511 		}
3512 		child = next;
3513 	}
3514 	rw_exit(&ipst->ips_ire_dep_lock);
3515 }
3516 
3517 /*
3518  * ire_pref() is used in recursive route-resolution for a destination to
3519  * determine the preference of an ire, where "preference" is determined
3520  * based on the level of indirection to the destination of the ire.
3521  * A higher preference indicates that fewer lookups are needed to complete
3522  * recursive route lookup. Thus
3523  * ire_pref(RTF_INDIRECT) < ire_pref(IRE_IF_RESOLVER) < ire_pref(IRE_PREF_CLONE)
3524  */
3525 int
3526 ire_pref(ire_t *ire)
3527 {
3528 	if (ire->ire_flags & RTF_INDIRECT)
3529 		return (1);
3530 	if (ire->ire_type & IRE_OFFLINK)
3531 		return (2);
3532 	if (ire->ire_type & (IRE_IF_RESOLVER|IRE_IF_NORESOLVER))
3533 		return (3);
3534 	if (ire->ire_type & IRE_IF_CLONE)
3535 		return (4);
3536 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_BROADCAST))
3537 		return (5);
3538 	return (-1); /* unknown ire_type */
3539 }
3540