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