xref: /onnv-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision 12570:4be1b7cb5db7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212069SPeter.Memishian@Sun.COM  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
2312069SPeter.Memishian@Sun.COM  * Copyright (c) 1990 Mentat Inc.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * This file contains routines that manipulate Internet Routing Entries (IREs).
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/stropts.h>
338485SPeter.Memishian@Sun.COM #include <sys/strsun.h>
348778SErik.Nordmark@Sun.COM #include <sys/strsubr.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/policy.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/socket.h>
430Sstevel@tonic-gate #include <net/if.h>
440Sstevel@tonic-gate #include <net/route.h>
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate #include <net/if_dl.h>
470Sstevel@tonic-gate #include <netinet/ip6.h>
480Sstevel@tonic-gate #include <netinet/icmp6.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <inet/common.h>
510Sstevel@tonic-gate #include <inet/mi.h>
520Sstevel@tonic-gate #include <inet/ip.h>
530Sstevel@tonic-gate #include <inet/ip6.h>
540Sstevel@tonic-gate #include <inet/ip_ndp.h>
552535Ssangeeta #include <inet/arp.h>
560Sstevel@tonic-gate #include <inet/ip_if.h>
570Sstevel@tonic-gate #include <inet/ip_ire.h>
582535Ssangeeta #include <inet/ip_ftable.h>
590Sstevel@tonic-gate #include <inet/ip_rts.h>
600Sstevel@tonic-gate #include <inet/nd.h>
6112016SGirish.Moodalbail@Sun.COM #include <inet/tunables.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <inet/tcp.h>
640Sstevel@tonic-gate #include <inet/ipclassifier.h>
650Sstevel@tonic-gate #include <sys/zone.h>
663448Sdh155122 #include <sys/cpuvar.h>
673448Sdh155122 
681676Sjpk #include <sys/tsol/label.h>
691676Sjpk #include <sys/tsol/tnet.h>
701676Sjpk 
712535Ssangeeta struct kmem_cache *rt_entry_cache;
722535Ssangeeta 
7311042SErik.Nordmark@Sun.COM typedef struct nce_clookup_s {
7411042SErik.Nordmark@Sun.COM 	ipaddr_t ncecl_addr;
7511042SErik.Nordmark@Sun.COM 	boolean_t ncecl_found;
7611042SErik.Nordmark@Sun.COM } nce_clookup_t;
7711042SErik.Nordmark@Sun.COM 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Synchronization notes:
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * The fields of the ire_t struct are protected in the following way :
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * ire_next/ire_ptpn
840Sstevel@tonic-gate  *
8511042SErik.Nordmark@Sun.COM  *	- bucket lock of the forwarding table in which is ire stored.
860Sstevel@tonic-gate  *
8711042SErik.Nordmark@Sun.COM  * ire_ill, ire_u *except* ire_gateway_addr[v6], ire_mask,
8811042SErik.Nordmark@Sun.COM  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags,
8911042SErik.Nordmark@Sun.COM  * ire_bucket
900Sstevel@tonic-gate  *
910Sstevel@tonic-gate  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
920Sstevel@tonic-gate  *	  we don't need a lock whenever these fields are accessed.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
9511042SErik.Nordmark@Sun.COM  *        ire_add before inserting in the bucket and never
960Sstevel@tonic-gate  *        changes after that. Thus we don't need a lock whenever these
970Sstevel@tonic-gate  *	  fields are accessed.
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * ire_gateway_addr_v4[v6]
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
1020Sstevel@tonic-gate  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
1030Sstevel@tonic-gate  *	  it assumed to be atomic and hence the other parts of the code
1040Sstevel@tonic-gate  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
1050Sstevel@tonic-gate  *	  and hence any access to it uses ire_lock to get/set the right value.
1060Sstevel@tonic-gate  *
10711042SErik.Nordmark@Sun.COM  * ire_refcnt, ire_identical_ref
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *	- Updated atomically using atomic_add_32
1100Sstevel@tonic-gate  *
1110Sstevel@tonic-gate  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
1140Sstevel@tonic-gate  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
1150Sstevel@tonic-gate  *
11611042SErik.Nordmark@Sun.COM  * ire_generation
11711042SErik.Nordmark@Sun.COM  *	- Under ire_lock
1180Sstevel@tonic-gate  *
11911042SErik.Nordmark@Sun.COM  * ire_nce_cache
12011042SErik.Nordmark@Sun.COM  *	- Under ire_lock
1210Sstevel@tonic-gate  *
12211042SErik.Nordmark@Sun.COM  * ire_dep_parent (To next IRE in recursive lookup chain)
12311042SErik.Nordmark@Sun.COM  *	- Under ips_ire_dep_lock. Write held when modifying. Read held when
12411042SErik.Nordmark@Sun.COM  *	  walking. We also hold ire_lock when modifying to allow the data path
12511042SErik.Nordmark@Sun.COM  *	  to only acquire ire_lock.
1260Sstevel@tonic-gate  *
12711042SErik.Nordmark@Sun.COM  * ire_dep_parent_generation (Generation number from ire_dep_parent)
12811042SErik.Nordmark@Sun.COM  *	- Under ips_ire_dep_lock and/or ire_lock. (A read claim on the dep_lock
12911042SErik.Nordmark@Sun.COM  *	  and ire_lock held when modifying)
1300Sstevel@tonic-gate  *
13111042SErik.Nordmark@Sun.COM  * ire_dep_children (From parent to first child)
13211042SErik.Nordmark@Sun.COM  * ire_dep_sib_next (linked list of siblings)
13311042SErik.Nordmark@Sun.COM  * ire_dep_sib_ptpn (linked list of siblings)
13411042SErik.Nordmark@Sun.COM  *	- Under ips_ire_dep_lock. Write held when modifying. Read held when
13511042SErik.Nordmark@Sun.COM  *	  walking.
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * As we always hold the bucket locks in all the places while accessing
1380Sstevel@tonic-gate  * the above values, it is natural to use them for protecting them.
1390Sstevel@tonic-gate  *
14011042SErik.Nordmark@Sun.COM  * We have a forwarding table for IPv4 and IPv6. The IPv6 forwarding table
1415335Ssowmini  * (ip_forwarding_table_v6) is an array of pointers to arrays of irb_t
14211042SErik.Nordmark@Sun.COM  * structures. ip_forwarding_table_v6 is allocated dynamically in
1433448Sdh155122  * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads
1440Sstevel@tonic-gate  * initializing the same bucket. Once a bucket is initialized, it is never
1453448Sdh155122  * de-alloacted. This assumption enables us to access
1463448Sdh155122  * ip_forwarding_table_v6[i] without any locks.
1470Sstevel@tonic-gate  *
1485335Ssowmini  * The forwarding table for IPv4 is a radix tree whose leaves
1495335Ssowmini  * are rt_entry structures containing the irb_t for the rt_dst. The irb_t
1505335Ssowmini  * for IPv4 is dynamically allocated and freed.
1515335Ssowmini  *
1520Sstevel@tonic-gate  * Each irb_t - ire bucket structure has a lock to protect
1530Sstevel@tonic-gate  * a bucket and the ires residing in the bucket have a back pointer to
1540Sstevel@tonic-gate  * the bucket structure. It also has a reference count for the number
1550Sstevel@tonic-gate  * of threads walking the bucket - irb_refcnt which is bumped up
15611042SErik.Nordmark@Sun.COM  * using the irb_refhold function. The flags irb_marks can be
15711042SErik.Nordmark@Sun.COM  * set to IRB_MARK_CONDEMNED indicating that there are some ires
15811042SErik.Nordmark@Sun.COM  * in this bucket that are IRE_IS_CONDEMNED and the
1590Sstevel@tonic-gate  * last thread to leave the bucket should delete the ires. Usually
16011042SErik.Nordmark@Sun.COM  * this is done by the irb_refrele function which is used to decrement
1615335Ssowmini  * the reference count on a bucket. See comments above irb_t structure
1625335Ssowmini  * definition in ip.h for further details.
1630Sstevel@tonic-gate  *
16411042SErik.Nordmark@Sun.COM  * The ire_refhold/ire_refrele functions operate on the ire which increments/
1650Sstevel@tonic-gate  * decrements the reference count, ire_refcnt, atomically on the ire.
16611042SErik.Nordmark@Sun.COM  * ire_refcnt is modified only using those functions. Operations on the IRE
1670Sstevel@tonic-gate  * could be described as follows :
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * CREATE an ire with reference count initialized to 1.
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  * ADDITION of an ire holds the bucket lock, checks for duplicates
17211042SErik.Nordmark@Sun.COM  * and then adds the ire. ire_add returns the ire after
1730Sstevel@tonic-gate  * bumping up once more i.e the reference count is 2. This is to avoid
1740Sstevel@tonic-gate  * an extra lookup in the functions calling ire_add which wants to
1750Sstevel@tonic-gate  * work with the ire after adding.
1760Sstevel@tonic-gate  *
17711042SErik.Nordmark@Sun.COM  * LOOKUP of an ire bumps up the reference count using ire_refhold
17811042SErik.Nordmark@Sun.COM  * function. It is valid to bump up the referece count of the IRE,
1790Sstevel@tonic-gate  * after the lookup has returned an ire. Following are the lookup
1800Sstevel@tonic-gate  * functions that return an HELD ire :
1810Sstevel@tonic-gate  *
18211042SErik.Nordmark@Sun.COM  * ire_ftable_lookup[_v6], ire_lookup_multi_ill[_v6]
1830Sstevel@tonic-gate  *
1840Sstevel@tonic-gate  * DELETION of an ire holds the bucket lock, removes it from the list
1850Sstevel@tonic-gate  * and then decrements the reference count for having removed from the list
18611042SErik.Nordmark@Sun.COM  * by using the ire_refrele function. If some other thread has looked up
1870Sstevel@tonic-gate  * the ire, the reference count would have been bumped up and hence
1880Sstevel@tonic-gate  * this ire will not be freed once deleted. It will be freed once the
1890Sstevel@tonic-gate  * reference count drops to zero.
1900Sstevel@tonic-gate  *
1910Sstevel@tonic-gate  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
1920Sstevel@tonic-gate  * lookups acquire the bucket lock as RW_READER.
1930Sstevel@tonic-gate  *
19411042SErik.Nordmark@Sun.COM  * The general rule is to do the ire_refrele in the function
1950Sstevel@tonic-gate  * that is passing the ire as an argument.
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  * In trying to locate ires the following points are to be noted.
1980Sstevel@tonic-gate  *
19911042SErik.Nordmark@Sun.COM  * IRE_IS_CONDEMNED signifies that the ire has been logically deleted and is
2000Sstevel@tonic-gate  * to be ignored when walking the ires using ire_next.
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  * Zones note:
2030Sstevel@tonic-gate  *	Walking IREs within a given zone also walks certain ires in other
2040Sstevel@tonic-gate  *	zones.  This is done intentionally.  IRE walks with a specified
2050Sstevel@tonic-gate  *	zoneid are used only when doing informational reports, and
2060Sstevel@tonic-gate  *	zone users want to see things that they can access. See block
2070Sstevel@tonic-gate  *	comment in ire_walk_ill_match().
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * The size of the forwarding table.  We will make sure that it is a
2120Sstevel@tonic-gate  * power of 2 in ip_ire_init().
2133448Sdh155122  * Setable in /etc/system
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate struct	kmem_cache	*ire_cache;
21811042SErik.Nordmark@Sun.COM struct	kmem_cache	*ncec_cache;
21911042SErik.Nordmark@Sun.COM struct	kmem_cache	*nce_cache;
22011042SErik.Nordmark@Sun.COM 
2210Sstevel@tonic-gate static ire_t	ire_null;
2220Sstevel@tonic-gate 
22311042SErik.Nordmark@Sun.COM static ire_t	*ire_add_v4(ire_t *ire);
2240Sstevel@tonic-gate static void	ire_delete_v4(ire_t *ire);
22511042SErik.Nordmark@Sun.COM static void	ire_dep_invalidate_children(ire_t *child);
2261676Sjpk static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
2273448Sdh155122     zoneid_t zoneid, ip_stack_t *);
2280Sstevel@tonic-gate static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
2291676Sjpk     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
2305023Scarlsonj #ifdef DEBUG
2315023Scarlsonj static void	ire_trace_cleanup(const ire_t *);
2320Sstevel@tonic-gate #endif
23311463SSowmini.Varadhan@Sun.COM static void	ire_dep_incr_generation_locked(ire_t *);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
23611042SErik.Nordmark@Sun.COM  * Following are the functions to increment/decrement the reference
23711042SErik.Nordmark@Sun.COM  * count of the IREs and IRBs (ire bucket).
23811042SErik.Nordmark@Sun.COM  *
23911042SErik.Nordmark@Sun.COM  * 1) We bump up the reference count of an IRE to make sure that
24011042SErik.Nordmark@Sun.COM  *    it does not get deleted and freed while we are using it.
24111042SErik.Nordmark@Sun.COM  *    Typically all the lookup functions hold the bucket lock,
24211042SErik.Nordmark@Sun.COM  *    and look for the IRE. If it finds an IRE, it bumps up the
24311042SErik.Nordmark@Sun.COM  *    reference count before dropping the lock. Sometimes we *may* want
24411042SErik.Nordmark@Sun.COM  *    to bump up the reference count after we *looked* up i.e without
24511042SErik.Nordmark@Sun.COM  *    holding the bucket lock. So, the ire_refhold function does not assert
24611042SErik.Nordmark@Sun.COM  *    on the bucket lock being held. Any thread trying to delete from
24711042SErik.Nordmark@Sun.COM  *    the hash bucket can still do so but cannot free the IRE if
24811042SErik.Nordmark@Sun.COM  *    ire_refcnt is not 0.
24911042SErik.Nordmark@Sun.COM  *
25011042SErik.Nordmark@Sun.COM  * 2) We bump up the reference count on the bucket where the IRE resides
25111042SErik.Nordmark@Sun.COM  *    (IRB), when we want to prevent the IREs getting deleted from a given
25211042SErik.Nordmark@Sun.COM  *    hash bucket. This makes life easier for ire_walk type functions which
25311042SErik.Nordmark@Sun.COM  *    wants to walk the IRE list, call a function, but needs to drop
25411042SErik.Nordmark@Sun.COM  *    the bucket lock to prevent recursive rw_enters. While the
25511042SErik.Nordmark@Sun.COM  *    lock is dropped, the list could be changed by other threads or
25611042SErik.Nordmark@Sun.COM  *    the same thread could end up deleting the ire or the ire pointed by
25711042SErik.Nordmark@Sun.COM  *    ire_next. ire_refholding the ire or ire_next is not sufficient as
25811042SErik.Nordmark@Sun.COM  *    a delete will still remove the ire from the bucket while we have
25911042SErik.Nordmark@Sun.COM  *    dropped the lock and hence the ire_next would be NULL. Thus, we
26011042SErik.Nordmark@Sun.COM  *    need a mechanism to prevent deletions from a given bucket.
26111042SErik.Nordmark@Sun.COM  *
26211042SErik.Nordmark@Sun.COM  *    To prevent deletions, we bump up the reference count on the
26311042SErik.Nordmark@Sun.COM  *    bucket. If the bucket is held, ire_delete just marks both
26411042SErik.Nordmark@Sun.COM  *    the ire and irb as CONDEMNED. When the
26511042SErik.Nordmark@Sun.COM  *    reference count on the bucket drops to zero, all the CONDEMNED ires
26611042SErik.Nordmark@Sun.COM  *    are deleted. We don't have to bump up the reference count on the
26711042SErik.Nordmark@Sun.COM  *    bucket if we are walking the bucket and never have to drop the bucket
26811042SErik.Nordmark@Sun.COM  *    lock. Note that irb_refhold does not prevent addition of new ires
26911042SErik.Nordmark@Sun.COM  *    in the list. It is okay because addition of new ires will not cause
27011042SErik.Nordmark@Sun.COM  *    ire_next to point to freed memory. We do irb_refhold only when
27111042SErik.Nordmark@Sun.COM  *    all of the 3 conditions are true :
27211042SErik.Nordmark@Sun.COM  *
27311042SErik.Nordmark@Sun.COM  *    1) The code needs to walk the IRE bucket from start to end.
27411042SErik.Nordmark@Sun.COM  *    2) It may have to drop the bucket lock sometimes while doing (1)
27511042SErik.Nordmark@Sun.COM  *    3) It does not want any ires to be deleted meanwhile.
27611042SErik.Nordmark@Sun.COM  */
27711042SErik.Nordmark@Sun.COM 
27811042SErik.Nordmark@Sun.COM /*
27911042SErik.Nordmark@Sun.COM  * Bump up the reference count on the hash bucket - IRB to
28011042SErik.Nordmark@Sun.COM  * prevent ires from being deleted in this bucket.
28111042SErik.Nordmark@Sun.COM  */
28211042SErik.Nordmark@Sun.COM void
irb_refhold(irb_t * irb)28311042SErik.Nordmark@Sun.COM irb_refhold(irb_t *irb)
28411042SErik.Nordmark@Sun.COM {
28511042SErik.Nordmark@Sun.COM 	rw_enter(&irb->irb_lock, RW_WRITER);
28611042SErik.Nordmark@Sun.COM 	irb->irb_refcnt++;
28711042SErik.Nordmark@Sun.COM 	ASSERT(irb->irb_refcnt != 0);
28811042SErik.Nordmark@Sun.COM 	rw_exit(&irb->irb_lock);
28911042SErik.Nordmark@Sun.COM }
29011042SErik.Nordmark@Sun.COM 
29111042SErik.Nordmark@Sun.COM void
irb_refhold_locked(irb_t * irb)29211042SErik.Nordmark@Sun.COM irb_refhold_locked(irb_t *irb)
29311042SErik.Nordmark@Sun.COM {
29411042SErik.Nordmark@Sun.COM 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
29511042SErik.Nordmark@Sun.COM 	irb->irb_refcnt++;
29611042SErik.Nordmark@Sun.COM 	ASSERT(irb->irb_refcnt != 0);
29711042SErik.Nordmark@Sun.COM }
29811042SErik.Nordmark@Sun.COM 
29911042SErik.Nordmark@Sun.COM /*
30011042SErik.Nordmark@Sun.COM  * Note: when IRB_MARK_DYNAMIC is not set the irb_t
30111042SErik.Nordmark@Sun.COM  * is statically allocated, so that when the irb_refcnt goes to 0,
30211042SErik.Nordmark@Sun.COM  * we simply clean up the ire list and continue.
30311042SErik.Nordmark@Sun.COM  */
30411042SErik.Nordmark@Sun.COM void
irb_refrele(irb_t * irb)30511042SErik.Nordmark@Sun.COM irb_refrele(irb_t *irb)
30611042SErik.Nordmark@Sun.COM {
30711042SErik.Nordmark@Sun.COM 	if (irb->irb_marks & IRB_MARK_DYNAMIC) {
30811042SErik.Nordmark@Sun.COM 		irb_refrele_ftable(irb);
30911042SErik.Nordmark@Sun.COM 	} else {
31011042SErik.Nordmark@Sun.COM 		rw_enter(&irb->irb_lock, RW_WRITER);
31111042SErik.Nordmark@Sun.COM 		ASSERT(irb->irb_refcnt != 0);
31211042SErik.Nordmark@Sun.COM 		if (--irb->irb_refcnt	== 0 &&
31311042SErik.Nordmark@Sun.COM 		    (irb->irb_marks & IRB_MARK_CONDEMNED)) {
31411042SErik.Nordmark@Sun.COM 			ire_t *ire_list;
31511042SErik.Nordmark@Sun.COM 
31611042SErik.Nordmark@Sun.COM 			ire_list = ire_unlink(irb);
31711042SErik.Nordmark@Sun.COM 			rw_exit(&irb->irb_lock);
31811042SErik.Nordmark@Sun.COM 			ASSERT(ire_list != NULL);
31911042SErik.Nordmark@Sun.COM 			ire_cleanup(ire_list);
32011042SErik.Nordmark@Sun.COM 		} else {
32111042SErik.Nordmark@Sun.COM 			rw_exit(&irb->irb_lock);
32211042SErik.Nordmark@Sun.COM 		}
32311042SErik.Nordmark@Sun.COM 	}
32411042SErik.Nordmark@Sun.COM }
32511042SErik.Nordmark@Sun.COM 
32611042SErik.Nordmark@Sun.COM 
32711042SErik.Nordmark@Sun.COM /*
32811042SErik.Nordmark@Sun.COM  * Bump up the reference count on the IRE. We cannot assert that the
32911042SErik.Nordmark@Sun.COM  * bucket lock is being held as it is legal to bump up the reference
33011042SErik.Nordmark@Sun.COM  * count after the first lookup has returned the IRE without
33111042SErik.Nordmark@Sun.COM  * holding the lock.
33211042SErik.Nordmark@Sun.COM  */
33311042SErik.Nordmark@Sun.COM void
ire_refhold(ire_t * ire)33411042SErik.Nordmark@Sun.COM ire_refhold(ire_t *ire)
33511042SErik.Nordmark@Sun.COM {
33611042SErik.Nordmark@Sun.COM 	atomic_add_32(&(ire)->ire_refcnt, 1);
33711042SErik.Nordmark@Sun.COM 	ASSERT((ire)->ire_refcnt != 0);
33811042SErik.Nordmark@Sun.COM #ifdef DEBUG
33911042SErik.Nordmark@Sun.COM 	ire_trace_ref(ire);
34011042SErik.Nordmark@Sun.COM #endif
34111042SErik.Nordmark@Sun.COM }
34211042SErik.Nordmark@Sun.COM 
34311042SErik.Nordmark@Sun.COM void
ire_refhold_notr(ire_t * ire)34411042SErik.Nordmark@Sun.COM ire_refhold_notr(ire_t *ire)
34511042SErik.Nordmark@Sun.COM {
34611042SErik.Nordmark@Sun.COM 	atomic_add_32(&(ire)->ire_refcnt, 1);
34711042SErik.Nordmark@Sun.COM 	ASSERT((ire)->ire_refcnt != 0);
34811042SErik.Nordmark@Sun.COM }
34911042SErik.Nordmark@Sun.COM 
35011042SErik.Nordmark@Sun.COM void
ire_refhold_locked(ire_t * ire)35111042SErik.Nordmark@Sun.COM ire_refhold_locked(ire_t *ire)
35211042SErik.Nordmark@Sun.COM {
35311042SErik.Nordmark@Sun.COM #ifdef DEBUG
35411042SErik.Nordmark@Sun.COM 	ire_trace_ref(ire);
35511042SErik.Nordmark@Sun.COM #endif
35611042SErik.Nordmark@Sun.COM 	ire->ire_refcnt++;
35711042SErik.Nordmark@Sun.COM }
35811042SErik.Nordmark@Sun.COM 
35911042SErik.Nordmark@Sun.COM /*
36011042SErik.Nordmark@Sun.COM  * Release a ref on an IRE.
3610Sstevel@tonic-gate  *
3620Sstevel@tonic-gate  * Must not be called while holding any locks. Otherwise if this is
3630Sstevel@tonic-gate  * the last reference to be released there is a chance of recursive mutex
3640Sstevel@tonic-gate  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
3650Sstevel@tonic-gate  * to restart an ioctl. The one exception is when the caller is sure that
3660Sstevel@tonic-gate  * this is not the last reference to be released. Eg. if the caller is
3670Sstevel@tonic-gate  * sure that the ire has not been deleted and won't be deleted.
36811042SErik.Nordmark@Sun.COM  *
36911042SErik.Nordmark@Sun.COM  * In architectures e.g sun4u, where atomic_add_32_nv is just
37011042SErik.Nordmark@Sun.COM  * a cas, we need to maintain the right memory barrier semantics
37111042SErik.Nordmark@Sun.COM  * as that of mutex_exit i.e all the loads and stores should complete
37211042SErik.Nordmark@Sun.COM  * before the cas is executed. membar_exit() does that here.
3730Sstevel@tonic-gate  */
3740Sstevel@tonic-gate void
ire_refrele(ire_t * ire)3750Sstevel@tonic-gate ire_refrele(ire_t *ire)
3760Sstevel@tonic-gate {
37711042SErik.Nordmark@Sun.COM #ifdef DEBUG
37811042SErik.Nordmark@Sun.COM 	ire_untrace_ref(ire);
37911042SErik.Nordmark@Sun.COM #endif
38011042SErik.Nordmark@Sun.COM 	ASSERT((ire)->ire_refcnt != 0);
38111042SErik.Nordmark@Sun.COM 	membar_exit();
38211042SErik.Nordmark@Sun.COM 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)
38311042SErik.Nordmark@Sun.COM 		ire_inactive(ire);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate void
ire_refrele_notr(ire_t * ire)3870Sstevel@tonic-gate ire_refrele_notr(ire_t *ire)
3880Sstevel@tonic-gate {
38911042SErik.Nordmark@Sun.COM 	ASSERT((ire)->ire_refcnt != 0);
39011042SErik.Nordmark@Sun.COM 	membar_exit();
39111042SErik.Nordmark@Sun.COM 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)
39211042SErik.Nordmark@Sun.COM 		ire_inactive(ire);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
39711042SErik.Nordmark@Sun.COM  * IOCTL[s].  The NO_REPLY form is used by TCP to tell IP that it is
39811042SErik.Nordmark@Sun.COM  * having problems reaching a particular destination.
39911042SErik.Nordmark@Sun.COM  * This will make IP consider alternate routes (e.g., when there are
40011042SErik.Nordmark@Sun.COM  * muliple default routes), and it will also make IP discard any (potentially)
40111042SErik.Nordmark@Sun.COM  * stale redirect.
40211042SErik.Nordmark@Sun.COM  * Management processes may want to use the version that generates a reply.
4030Sstevel@tonic-gate  *
40411042SErik.Nordmark@Sun.COM  * With the use of NUD like behavior for IPv4/ARP in addition to IPv6
40511042SErik.Nordmark@Sun.COM  * this function shouldn't be necessary for IP to recover from a bad redirect,
40611042SErik.Nordmark@Sun.COM  * a bad default router (when there are multiple default routers), or
40711042SErik.Nordmark@Sun.COM  * a stale ND/ARP entry. But we retain it in any case.
40811042SErik.Nordmark@Sun.COM  * For instance, this is helpful when TCP suspects a failure before NUD does.
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate int
ip_ire_delete(queue_t * q,mblk_t * mp,cred_t * ioc_cr)4110Sstevel@tonic-gate ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
4120Sstevel@tonic-gate {
4132535Ssangeeta 	uchar_t		*addr_ucp;
41411042SErik.Nordmark@Sun.COM 	uint_t		ipversion;
41511042SErik.Nordmark@Sun.COM 	sin_t		*sin;
41611042SErik.Nordmark@Sun.COM 	sin6_t		*sin6;
41711042SErik.Nordmark@Sun.COM 	ipaddr_t	v4addr;
41811042SErik.Nordmark@Sun.COM 	in6_addr_t	v6addr;
4192535Ssangeeta 	ire_t		*ire;
4202535Ssangeeta 	ipid_t		*ipid;
4210Sstevel@tonic-gate 	zoneid_t	zoneid;
4223448Sdh155122 	ip_stack_t	*ipst;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
42511042SErik.Nordmark@Sun.COM 	zoneid = IPCL_ZONEID(Q_TO_CONN(q));
4263448Sdh155122 	ipst = CONNQ_TO_IPST(q);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/*
4290Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
4300Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
4310Sstevel@tonic-gate 	 */
4323448Sdh155122 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
4330Sstevel@tonic-gate 		return (EPERM);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	ipid = (ipid_t *)mp->b_rptr;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
4384714Ssowmini 	    ipid->ipid_addr_length);
4390Sstevel@tonic-gate 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
4400Sstevel@tonic-gate 		return (EINVAL);
4410Sstevel@tonic-gate 	switch (ipid->ipid_addr_length) {
44211042SErik.Nordmark@Sun.COM 	case sizeof (sin_t):
4430Sstevel@tonic-gate 		/*
4440Sstevel@tonic-gate 		 * got complete (sockaddr) address - increment addr_ucp to point
4450Sstevel@tonic-gate 		 * at the ip_addr field.
4460Sstevel@tonic-gate 		 */
4470Sstevel@tonic-gate 		sin = (sin_t *)addr_ucp;
4480Sstevel@tonic-gate 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
44911042SErik.Nordmark@Sun.COM 		ipversion = IPV4_VERSION;
4500Sstevel@tonic-gate 		break;
45111042SErik.Nordmark@Sun.COM 	case sizeof (sin6_t):
45211042SErik.Nordmark@Sun.COM 		/*
45311042SErik.Nordmark@Sun.COM 		 * got complete (sockaddr) address - increment addr_ucp to point
45411042SErik.Nordmark@Sun.COM 		 * at the ip_addr field.
45511042SErik.Nordmark@Sun.COM 		 */
45611042SErik.Nordmark@Sun.COM 		sin6 = (sin6_t *)addr_ucp;
45711042SErik.Nordmark@Sun.COM 		addr_ucp = (uchar_t *)&sin6->sin6_addr;
45811042SErik.Nordmark@Sun.COM 		ipversion = IPV6_VERSION;
45911042SErik.Nordmark@Sun.COM 		break;
4600Sstevel@tonic-gate 	default:
4610Sstevel@tonic-gate 		return (EINVAL);
4620Sstevel@tonic-gate 	}
46311042SErik.Nordmark@Sun.COM 	if (ipversion == IPV4_VERSION) {
46411042SErik.Nordmark@Sun.COM 		/* Extract the destination address. */
46511042SErik.Nordmark@Sun.COM 		bcopy(addr_ucp, &v4addr, IP_ADDR_LEN);
46611042SErik.Nordmark@Sun.COM 
46711042SErik.Nordmark@Sun.COM 		ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
46811042SErik.Nordmark@Sun.COM 		    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
46911042SErik.Nordmark@Sun.COM 	} else {
47011042SErik.Nordmark@Sun.COM 		/* Extract the destination address. */
47111042SErik.Nordmark@Sun.COM 		bcopy(addr_ucp, &v6addr, IPV6_ADDR_LEN);
47211042SErik.Nordmark@Sun.COM 
47311042SErik.Nordmark@Sun.COM 		ire = ire_ftable_lookup_v6(&v6addr, NULL, NULL, 0, NULL,
47411042SErik.Nordmark@Sun.COM 		    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
47511042SErik.Nordmark@Sun.COM 	}
47611042SErik.Nordmark@Sun.COM 	if (ire != NULL) {
47711042SErik.Nordmark@Sun.COM 		if (ipversion == IPV4_VERSION) {
47811042SErik.Nordmark@Sun.COM 			ip_rts_change(RTM_LOSING, ire->ire_addr,
47911042SErik.Nordmark@Sun.COM 			    ire->ire_gateway_addr, ire->ire_mask,
48011042SErik.Nordmark@Sun.COM 			    (Q_TO_CONN(q))->conn_laddr_v4,  0, 0, 0,
48111042SErik.Nordmark@Sun.COM 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA),
48211042SErik.Nordmark@Sun.COM 			    ire->ire_ipst);
4830Sstevel@tonic-gate 		}
48411042SErik.Nordmark@Sun.COM 		(void) ire_no_good(ire);
4854714Ssowmini 		ire_refrele(ire);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	return (0);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate /*
4910Sstevel@tonic-gate  * Initialize the ire that is specific to IPv4 part and call
4920Sstevel@tonic-gate  * ire_init_common to finish it.
49311042SErik.Nordmark@Sun.COM  * Returns zero or errno.
4940Sstevel@tonic-gate  */
49511042SErik.Nordmark@Sun.COM int
ire_init_v4(ire_t * ire,uchar_t * addr,uchar_t * mask,uchar_t * gateway,ushort_t type,ill_t * ill,zoneid_t zoneid,uint_t flags,tsol_gc_t * gc,ip_stack_t * ipst)49611042SErik.Nordmark@Sun.COM ire_init_v4(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *gateway,
49711042SErik.Nordmark@Sun.COM     ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags,
49811042SErik.Nordmark@Sun.COM     tsol_gc_t *gc, ip_stack_t *ipst)
4990Sstevel@tonic-gate {
50011042SErik.Nordmark@Sun.COM 	int error;
50111042SErik.Nordmark@Sun.COM 
5021676Sjpk 	/*
5031676Sjpk 	 * Reject IRE security attribute creation/initialization
5041676Sjpk 	 * if system is not running in Trusted mode.
5051676Sjpk 	 */
50611042SErik.Nordmark@Sun.COM 	if (gc != NULL && !is_system_labeled())
50711042SErik.Nordmark@Sun.COM 		return (EINVAL);
5081676Sjpk 
5093448Sdh155122 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (addr != NULL)
5120Sstevel@tonic-gate 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
51311042SErik.Nordmark@Sun.COM 	if (gateway != NULL)
5140Sstevel@tonic-gate 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
51511042SErik.Nordmark@Sun.COM 
51611042SErik.Nordmark@Sun.COM 	/* Make sure we don't have stray values in some fields */
51711042SErik.Nordmark@Sun.COM 	switch (type) {
51811042SErik.Nordmark@Sun.COM 	case IRE_LOOPBACK:
51911042SErik.Nordmark@Sun.COM 	case IRE_HOST:
52011042SErik.Nordmark@Sun.COM 	case IRE_BROADCAST:
52111042SErik.Nordmark@Sun.COM 	case IRE_LOCAL:
52211042SErik.Nordmark@Sun.COM 	case IRE_IF_CLONE:
52311042SErik.Nordmark@Sun.COM 		ire->ire_mask = IP_HOST_MASK;
52411042SErik.Nordmark@Sun.COM 		ire->ire_masklen = IPV4_ABITS;
52511042SErik.Nordmark@Sun.COM 		break;
52611042SErik.Nordmark@Sun.COM 	case IRE_PREFIX:
52711042SErik.Nordmark@Sun.COM 	case IRE_DEFAULT:
52811042SErik.Nordmark@Sun.COM 	case IRE_IF_RESOLVER:
52911042SErik.Nordmark@Sun.COM 	case IRE_IF_NORESOLVER:
53011042SErik.Nordmark@Sun.COM 		if (mask != NULL) {
53111042SErik.Nordmark@Sun.COM 			bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
53211042SErik.Nordmark@Sun.COM 			ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
53311042SErik.Nordmark@Sun.COM 		}
53411042SErik.Nordmark@Sun.COM 		break;
53511042SErik.Nordmark@Sun.COM 	case IRE_MULTICAST:
53611042SErik.Nordmark@Sun.COM 	case IRE_NOROUTE:
53711042SErik.Nordmark@Sun.COM 		ASSERT(mask == NULL);
53811042SErik.Nordmark@Sun.COM 		break;
53911042SErik.Nordmark@Sun.COM 	default:
54011042SErik.Nordmark@Sun.COM 		ASSERT(0);
54111042SErik.Nordmark@Sun.COM 		return (EINVAL);
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
54411042SErik.Nordmark@Sun.COM 	error = ire_init_common(ire, type, ill, zoneid, flags, IPV4_VERSION,
54511042SErik.Nordmark@Sun.COM 	    gc, ipst);
54611042SErik.Nordmark@Sun.COM 	if (error != NULL)
54711042SErik.Nordmark@Sun.COM 		return (error);
54811042SErik.Nordmark@Sun.COM 
54911042SErik.Nordmark@Sun.COM 	/* Determine which function pointers to use */
55011042SErik.Nordmark@Sun.COM 	ire->ire_postfragfn = ip_xmit;		/* Common case */
55111042SErik.Nordmark@Sun.COM 
55211042SErik.Nordmark@Sun.COM 	switch (ire->ire_type) {
55311042SErik.Nordmark@Sun.COM 	case IRE_LOCAL:
55411042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_local_v4;
55511042SErik.Nordmark@Sun.COM 		ire->ire_recvfn = ire_recv_local_v4;
55611042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_ill != NULL);
55711076SCathy.Zhou@Sun.COM 		if (ire->ire_ill->ill_flags & ILLF_NOACCEPT)
55811042SErik.Nordmark@Sun.COM 			ire->ire_recvfn = ire_recv_noaccept_v6;
55911042SErik.Nordmark@Sun.COM 		break;
56011042SErik.Nordmark@Sun.COM 	case IRE_LOOPBACK:
56111042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_local_v4;
56211042SErik.Nordmark@Sun.COM 		ire->ire_recvfn = ire_recv_loopback_v4;
56311042SErik.Nordmark@Sun.COM 		break;
56411042SErik.Nordmark@Sun.COM 	case IRE_BROADCAST:
56511042SErik.Nordmark@Sun.COM 		ire->ire_postfragfn = ip_postfrag_loopcheck;
56611042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_broadcast_v4;
56711042SErik.Nordmark@Sun.COM 		ire->ire_recvfn = ire_recv_broadcast_v4;
56811042SErik.Nordmark@Sun.COM 		break;
56911042SErik.Nordmark@Sun.COM 	case IRE_MULTICAST:
57011042SErik.Nordmark@Sun.COM 		ire->ire_postfragfn = ip_postfrag_loopcheck;
57111042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_multicast_v4;
57211042SErik.Nordmark@Sun.COM 		ire->ire_recvfn = ire_recv_multicast_v4;
57311042SErik.Nordmark@Sun.COM 		break;
57411042SErik.Nordmark@Sun.COM 	default:
57511042SErik.Nordmark@Sun.COM 		/*
57611042SErik.Nordmark@Sun.COM 		 * For IRE_IF_ALL and IRE_OFFLINK we forward received
57711042SErik.Nordmark@Sun.COM 		 * packets by default.
57811042SErik.Nordmark@Sun.COM 		 */
57911042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_wire_v4;
58011042SErik.Nordmark@Sun.COM 		ire->ire_recvfn = ire_recv_forward_v4;
58111042SErik.Nordmark@Sun.COM 		break;
58211042SErik.Nordmark@Sun.COM 	}
58311042SErik.Nordmark@Sun.COM 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
58411042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_noroute_v4;
58511042SErik.Nordmark@Sun.COM 		ire->ire_recvfn = ire_recv_noroute_v4;
58611042SErik.Nordmark@Sun.COM 	} else if (ire->ire_flags & RTF_MULTIRT) {
58711042SErik.Nordmark@Sun.COM 		ire->ire_postfragfn = ip_postfrag_multirt_v4;
58811042SErik.Nordmark@Sun.COM 		ire->ire_sendfn = ire_send_multirt_v4;
58911042SErik.Nordmark@Sun.COM 		/* Multirt receive of broadcast uses ire_recv_broadcast_v4 */
59011042SErik.Nordmark@Sun.COM 		if (ire->ire_type != IRE_BROADCAST)
59111042SErik.Nordmark@Sun.COM 			ire->ire_recvfn = ire_recv_multirt_v4;
59211042SErik.Nordmark@Sun.COM 	}
59311042SErik.Nordmark@Sun.COM 	ire->ire_nce_capable = ire_determine_nce_capable(ire);
59411042SErik.Nordmark@Sun.COM 	return (0);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate /*
59811042SErik.Nordmark@Sun.COM  * Determine ire_nce_capable
5990Sstevel@tonic-gate  */
60011042SErik.Nordmark@Sun.COM boolean_t
ire_determine_nce_capable(ire_t * ire)60111042SErik.Nordmark@Sun.COM ire_determine_nce_capable(ire_t *ire)
6020Sstevel@tonic-gate {
60311042SErik.Nordmark@Sun.COM 	int max_masklen;
60411042SErik.Nordmark@Sun.COM 
60511042SErik.Nordmark@Sun.COM 	if ((ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
60611042SErik.Nordmark@Sun.COM 	    (ire->ire_type & IRE_MULTICAST))
60711042SErik.Nordmark@Sun.COM 		return (B_TRUE);
60811042SErik.Nordmark@Sun.COM 
60911042SErik.Nordmark@Sun.COM 	if (ire->ire_ipversion == IPV4_VERSION)
61011042SErik.Nordmark@Sun.COM 		max_masklen = IPV4_ABITS;
61111042SErik.Nordmark@Sun.COM 	else
61211042SErik.Nordmark@Sun.COM 		max_masklen = IPV6_ABITS;
61311042SErik.Nordmark@Sun.COM 
61411042SErik.Nordmark@Sun.COM 	if ((ire->ire_type & IRE_ONLINK) && ire->ire_masklen == max_masklen)
61511042SErik.Nordmark@Sun.COM 		return (B_TRUE);
61611042SErik.Nordmark@Sun.COM 	return (B_FALSE);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate  * ire_create is called to allocate and initialize a new IRE.
6210Sstevel@tonic-gate  *
6220Sstevel@tonic-gate  * NOTE : This is called as writer sometimes though not required
6230Sstevel@tonic-gate  * by this function.
6240Sstevel@tonic-gate  */
6250Sstevel@tonic-gate ire_t *
ire_create(uchar_t * addr,uchar_t * mask,uchar_t * gateway,ushort_t type,ill_t * ill,zoneid_t zoneid,uint_t flags,tsol_gc_t * gc,ip_stack_t * ipst)62611042SErik.Nordmark@Sun.COM ire_create(uchar_t *addr, uchar_t *mask, uchar_t *gateway,
62711042SErik.Nordmark@Sun.COM     ushort_t type, ill_t *ill, zoneid_t zoneid, uint_t flags, tsol_gc_t *gc,
62811042SErik.Nordmark@Sun.COM     ip_stack_t *ipst)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	ire_t	*ire;
63111042SErik.Nordmark@Sun.COM 	int	error;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
6340Sstevel@tonic-gate 	if (ire == NULL) {
63511042SErik.Nordmark@Sun.COM 		DTRACE_PROBE(kmem__cache__alloc);
6360Sstevel@tonic-gate 		return (NULL);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	*ire = ire_null;
6390Sstevel@tonic-gate 
64011042SErik.Nordmark@Sun.COM 	error = ire_init_v4(ire, addr, mask, gateway, type, ill, zoneid, flags,
64111042SErik.Nordmark@Sun.COM 	    gc, ipst);
64211042SErik.Nordmark@Sun.COM 	if (error != 0) {
64311042SErik.Nordmark@Sun.COM 		DTRACE_PROBE2(ire__init, ire_t *, ire, int, error);
6440Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
6450Sstevel@tonic-gate 		return (NULL);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 	return (ire);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * Common to IPv4 and IPv6
65211042SErik.Nordmark@Sun.COM  * Returns zero or errno.
6530Sstevel@tonic-gate  */
65411042SErik.Nordmark@Sun.COM int
ire_init_common(ire_t * ire,ushort_t type,ill_t * ill,zoneid_t zoneid,uint_t flags,uchar_t ipversion,tsol_gc_t * gc,ip_stack_t * ipst)65511042SErik.Nordmark@Sun.COM ire_init_common(ire_t *ire, ushort_t type, ill_t *ill, zoneid_t zoneid,
65611042SErik.Nordmark@Sun.COM     uint_t flags, uchar_t ipversion, tsol_gc_t *gc, ip_stack_t *ipst)
6570Sstevel@tonic-gate {
65811042SErik.Nordmark@Sun.COM 	int error;
6590Sstevel@tonic-gate 
6601676Sjpk #ifdef DEBUG
66111042SErik.Nordmark@Sun.COM 	if (ill != NULL) {
66211042SErik.Nordmark@Sun.COM 		if (ill->ill_isv6)
6630Sstevel@tonic-gate 			ASSERT(ipversion == IPV6_VERSION);
6640Sstevel@tonic-gate 		else
6650Sstevel@tonic-gate 			ASSERT(ipversion == IPV4_VERSION);
6660Sstevel@tonic-gate 	}
6671676Sjpk #endif /* DEBUG */
6681676Sjpk 
6691676Sjpk 	/*
6701676Sjpk 	 * Create/initialize IRE security attribute only in Trusted mode;
67111042SErik.Nordmark@Sun.COM 	 * if the passed in gc is non-NULL, we expect that the caller
6721676Sjpk 	 * has held a reference to it and will release it when this routine
6731676Sjpk 	 * returns a failure, otherwise we own the reference.  We do this
6741676Sjpk 	 * prior to initializing the rest IRE fields.
6751676Sjpk 	 */
6761676Sjpk 	if (is_system_labeled()) {
6771676Sjpk 		if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST |
67811042SErik.Nordmark@Sun.COM 		    IRE_IF_ALL | IRE_MULTICAST | IRE_NOROUTE)) != 0) {
6791676Sjpk 			/* release references on behalf of caller */
6801676Sjpk 			if (gc != NULL)
6811676Sjpk 				GC_REFRELE(gc);
68211042SErik.Nordmark@Sun.COM 		} else {
68311042SErik.Nordmark@Sun.COM 			error = tsol_ire_init_gwattr(ire, ipversion, gc);
68411042SErik.Nordmark@Sun.COM 			if (error != 0)
68511042SErik.Nordmark@Sun.COM 				return (error);
6861676Sjpk 		}
6871676Sjpk 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	ire->ire_type = type;
6900Sstevel@tonic-gate 	ire->ire_flags = RTF_UP | flags;
6910Sstevel@tonic-gate 	ire->ire_create_time = (uint32_t)gethrestime_sec();
69211042SErik.Nordmark@Sun.COM 	ire->ire_generation = IRE_GENERATION_INITIAL;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	/*
69511042SErik.Nordmark@Sun.COM 	 * The ill_ire_cnt isn't increased until
69611042SErik.Nordmark@Sun.COM 	 * the IRE is added to ensure that a walker will find
69711042SErik.Nordmark@Sun.COM 	 * all IREs that hold a reference on an ill.
6980Sstevel@tonic-gate 	 *
69911042SErik.Nordmark@Sun.COM 	 * Note that ill_ire_multicast doesn't hold a ref on the ill since
70011042SErik.Nordmark@Sun.COM 	 * ire_add() is not called for the IRE_MULTICAST.
7010Sstevel@tonic-gate 	 */
70211042SErik.Nordmark@Sun.COM 	ire->ire_ill = ill;
70311042SErik.Nordmark@Sun.COM 	ire->ire_zoneid = zoneid;
7040Sstevel@tonic-gate 	ire->ire_ipversion = ipversion;
70511042SErik.Nordmark@Sun.COM 
7062535Ssangeeta 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
7070Sstevel@tonic-gate 	ire->ire_refcnt = 1;
70811042SErik.Nordmark@Sun.COM 	ire->ire_identical_ref = 1;	/* Number of ire_delete's needed */
7093448Sdh155122 	ire->ire_ipst = ipst;	/* No netstack_hold */
7105023Scarlsonj 	ire->ire_trace_disable = B_FALSE;
7111676Sjpk 
71211042SErik.Nordmark@Sun.COM 	return (0);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate /*
71611042SErik.Nordmark@Sun.COM  * This creates an IRE_BROADCAST based on the arguments.
71711042SErik.Nordmark@Sun.COM  * A mirror is ire_lookup_bcast().
7180Sstevel@tonic-gate  *
71911042SErik.Nordmark@Sun.COM  * Any supression of unneeded ones is done in ire_add_v4.
72011042SErik.Nordmark@Sun.COM  * We add one IRE_BROADCAST per address. ire_send_broadcast_v4()
72111042SErik.Nordmark@Sun.COM  * takes care of generating a loopback copy of the packet.
7220Sstevel@tonic-gate  */
7230Sstevel@tonic-gate ire_t **
ire_create_bcast(ill_t * ill,ipaddr_t addr,zoneid_t zoneid,ire_t ** irep)72411042SErik.Nordmark@Sun.COM ire_create_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid, ire_t **irep)
7250Sstevel@tonic-gate {
72611042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ill->ill_ipst;
72711042SErik.Nordmark@Sun.COM 
72811042SErik.Nordmark@Sun.COM 	ASSERT(IAM_WRITER_ILL(ill));
7293448Sdh155122 
7300Sstevel@tonic-gate 	*irep++ = ire_create(
7310Sstevel@tonic-gate 	    (uchar_t *)&addr,			/* dest addr */
7320Sstevel@tonic-gate 	    (uchar_t *)&ip_g_all_ones,		/* mask */
7330Sstevel@tonic-gate 	    NULL,				/* no gateway */
7340Sstevel@tonic-gate 	    IRE_BROADCAST,
73511042SErik.Nordmark@Sun.COM 	    ill,
73611042SErik.Nordmark@Sun.COM 	    zoneid,
73711042SErik.Nordmark@Sun.COM 	    RTF_KERNEL,
7384714Ssowmini 	    NULL,
7394714Ssowmini 	    ipst);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	return (irep);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate /*
74511042SErik.Nordmark@Sun.COM  * This looks up an IRE_BROADCAST based on the arguments.
74611042SErik.Nordmark@Sun.COM  * Mirrors ire_create_bcast().
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate ire_t *
ire_lookup_bcast(ill_t * ill,ipaddr_t addr,zoneid_t zoneid)74911042SErik.Nordmark@Sun.COM ire_lookup_bcast(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
7500Sstevel@tonic-gate {
75111042SErik.Nordmark@Sun.COM 	ire_t		*ire;
75211042SErik.Nordmark@Sun.COM 	int		match_args;
75311042SErik.Nordmark@Sun.COM 
75411042SErik.Nordmark@Sun.COM 	match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_GW |
75511042SErik.Nordmark@Sun.COM 	    MATCH_IRE_MASK | MATCH_IRE_ZONEONLY;
75611042SErik.Nordmark@Sun.COM 
75711042SErik.Nordmark@Sun.COM 	if (IS_UNDER_IPMP(ill))
75811042SErik.Nordmark@Sun.COM 		match_args |= MATCH_IRE_TESTHIDDEN;
75911042SErik.Nordmark@Sun.COM 
76011042SErik.Nordmark@Sun.COM 	ire = ire_ftable_lookup_v4(
76111042SErik.Nordmark@Sun.COM 	    addr,				/* dest addr */
76211042SErik.Nordmark@Sun.COM 	    ip_g_all_ones,			/* mask */
76311042SErik.Nordmark@Sun.COM 	    0,					/* no gateway */
76411042SErik.Nordmark@Sun.COM 	    IRE_BROADCAST,
76511042SErik.Nordmark@Sun.COM 	    ill,
76611042SErik.Nordmark@Sun.COM 	    zoneid,
76711042SErik.Nordmark@Sun.COM 	    NULL,
76811042SErik.Nordmark@Sun.COM 	    match_args,
76911042SErik.Nordmark@Sun.COM 	    0,
77011042SErik.Nordmark@Sun.COM 	    ill->ill_ipst,
77111042SErik.Nordmark@Sun.COM 	    NULL);
77211042SErik.Nordmark@Sun.COM 	return (ire);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /* Arrange to call the specified function for every IRE in the world. */
7760Sstevel@tonic-gate void
ire_walk(pfv_t func,void * arg,ip_stack_t * ipst)7773448Sdh155122 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
7780Sstevel@tonic-gate {
7793448Sdh155122 	ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate void
ire_walk_v4(pfv_t func,void * arg,zoneid_t zoneid,ip_stack_t * ipst)7833448Sdh155122 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
7840Sstevel@tonic-gate {
7853448Sdh155122 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate void
ire_walk_v6(pfv_t func,void * arg,zoneid_t zoneid,ip_stack_t * ipst)7893448Sdh155122 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
7900Sstevel@tonic-gate {
7913448Sdh155122 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate /*
7950Sstevel@tonic-gate  * Walk a particular version. version == 0 means both v4 and v6.
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate static void
ire_walk_ipvers(pfv_t func,void * arg,uchar_t vers,zoneid_t zoneid,ip_stack_t * ipst)7983448Sdh155122 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
7993448Sdh155122     ip_stack_t *ipst)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
8022535Ssangeeta 		/*
8032535Ssangeeta 		 * ip_forwarding_table variable doesn't matter for IPv4 since
8043448Sdh155122 		 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
8052535Ssangeeta 		 */
8060Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
8072535Ssangeeta 		    0, NULL,
8083448Sdh155122 		    NULL, zoneid, ipst);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
8110Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
8123448Sdh155122 		    ipst->ips_ip6_ftable_hash_size,
8133448Sdh155122 		    ipst->ips_ip_forwarding_table_v6,
81411042SErik.Nordmark@Sun.COM 		    NULL, zoneid, ipst);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8197216Smeem  * Arrange to call the specified function for every IRE that matches the ill.
8200Sstevel@tonic-gate  */
8210Sstevel@tonic-gate void
ire_walk_ill(uint_t match_flags,uint_t ire_type,pfv_t func,void * arg,ill_t * ill)8221676Sjpk ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
8230Sstevel@tonic-gate     ill_t *ill)
8240Sstevel@tonic-gate {
8257216Smeem 	uchar_t vers = (ill->ill_isv6 ? IPV6_VERSION : IPV4_VERSION);
8267216Smeem 
8277216Smeem 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, vers, ill);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8317216Smeem  * Walk a particular ill and version.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate static void
ire_walk_ill_ipvers(uint_t match_flags,uint_t ire_type,pfv_t func,void * arg,uchar_t vers,ill_t * ill)8340Sstevel@tonic-gate ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
8351676Sjpk     void *arg, uchar_t vers, ill_t *ill)
8360Sstevel@tonic-gate {
8373448Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
8383448Sdh155122 
8397216Smeem 	if (vers == IPV4_VERSION) {
8400Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
84111042SErik.Nordmark@Sun.COM 		    IP_MASK_TABLE_SIZE,
84211042SErik.Nordmark@Sun.COM 		    0, NULL,
84311042SErik.Nordmark@Sun.COM 		    ill, ALL_ZONES, ipst);
84411042SErik.Nordmark@Sun.COM 	}
84511042SErik.Nordmark@Sun.COM 	if (vers != IPV4_VERSION) {
8460Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
8473448Sdh155122 		    IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
8483448Sdh155122 		    ipst->ips_ip_forwarding_table_v6,
84911042SErik.Nordmark@Sun.COM 		    ill, ALL_ZONES, ipst);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
85311042SErik.Nordmark@Sun.COM /*
85411042SErik.Nordmark@Sun.COM  * Do the specific matching of IREs to shared-IP zones.
85511042SErik.Nordmark@Sun.COM  *
85611042SErik.Nordmark@Sun.COM  * We have the same logic as in ire_match_args but implemented slightly
85711042SErik.Nordmark@Sun.COM  * differently.
85811042SErik.Nordmark@Sun.COM  */
8592535Ssangeeta boolean_t
ire_walk_ill_match(uint_t match_flags,uint_t ire_type,ire_t * ire,ill_t * ill,zoneid_t zoneid,ip_stack_t * ipst)8600Sstevel@tonic-gate ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
8613448Sdh155122     ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
8620Sstevel@tonic-gate {
86311131SErik.Nordmark@Sun.COM 	ill_t *dst_ill = ire->ire_ill;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
8660Sstevel@tonic-gate 
86711042SErik.Nordmark@Sun.COM 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
86811042SErik.Nordmark@Sun.COM 	    ire->ire_zoneid != ALL_ZONES) {
8690Sstevel@tonic-gate 		/*
8700Sstevel@tonic-gate 		 * We're walking the IREs for a specific zone. The only relevant
8710Sstevel@tonic-gate 		 * IREs are:
8720Sstevel@tonic-gate 		 * - all IREs with a matching ire_zoneid
87311042SErik.Nordmark@Sun.COM 		 * - IRE_IF_ALL IREs for interfaces with a usable source addr
8740Sstevel@tonic-gate 		 *   with a matching zone
87511042SErik.Nordmark@Sun.COM 		 * - IRE_OFFLINK with a gateway reachable from the zone
87611042SErik.Nordmark@Sun.COM 		 * Note that ealier we only did the IRE_OFFLINK check for
87711042SErik.Nordmark@Sun.COM 		 * IRE_DEFAULT (and only when we had multiple IRE_DEFAULTs).
8780Sstevel@tonic-gate 		 */
87911042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_ONLINK) {
88011042SErik.Nordmark@Sun.COM 			uint_t	ifindex;
88111042SErik.Nordmark@Sun.COM 
8820Sstevel@tonic-gate 			/*
88311042SErik.Nordmark@Sun.COM 			 * Note there is no IRE_INTERFACE on vniN thus
88411042SErik.Nordmark@Sun.COM 			 * can't do an IRE lookup for a matching route.
8850Sstevel@tonic-gate 			 */
88611042SErik.Nordmark@Sun.COM 			ifindex = dst_ill->ill_usesrc_ifindex;
88711042SErik.Nordmark@Sun.COM 			if (ifindex == 0)
88811042SErik.Nordmark@Sun.COM 				return (B_FALSE);
88911042SErik.Nordmark@Sun.COM 
89011042SErik.Nordmark@Sun.COM 			/*
89111042SErik.Nordmark@Sun.COM 			 * If there is a usable source address in the
89211042SErik.Nordmark@Sun.COM 			 * zone, then it's ok to return an
89311042SErik.Nordmark@Sun.COM 			 * IRE_INTERFACE
89411042SErik.Nordmark@Sun.COM 			 */
89511042SErik.Nordmark@Sun.COM 			if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
89611042SErik.Nordmark@Sun.COM 			    zoneid, ipst)) {
89711042SErik.Nordmark@Sun.COM 				return (B_FALSE);
89811042SErik.Nordmark@Sun.COM 			}
89911042SErik.Nordmark@Sun.COM 		}
90011042SErik.Nordmark@Sun.COM 		if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
90111042SErik.Nordmark@Sun.COM 			ipif_t	*tipif;
90211042SErik.Nordmark@Sun.COM 
90311042SErik.Nordmark@Sun.COM 			mutex_enter(&dst_ill->ill_lock);
90411042SErik.Nordmark@Sun.COM 			for (tipif = dst_ill->ill_ipif;
90511042SErik.Nordmark@Sun.COM 			    tipif != NULL; tipif = tipif->ipif_next) {
90611042SErik.Nordmark@Sun.COM 				if (!IPIF_IS_CONDEMNED(tipif) &&
90711042SErik.Nordmark@Sun.COM 				    (tipif->ipif_flags & IPIF_UP) &&
90811042SErik.Nordmark@Sun.COM 				    (tipif->ipif_zoneid == zoneid ||
90911042SErik.Nordmark@Sun.COM 				    tipif->ipif_zoneid == ALL_ZONES))
91011042SErik.Nordmark@Sun.COM 					break;
91111042SErik.Nordmark@Sun.COM 			}
91211042SErik.Nordmark@Sun.COM 			mutex_exit(&dst_ill->ill_lock);
91311042SErik.Nordmark@Sun.COM 			if (tipif == NULL) {
9140Sstevel@tonic-gate 				return (B_FALSE);
9150Sstevel@tonic-gate 			}
9160Sstevel@tonic-gate 		}
91711131SErik.Nordmark@Sun.COM 	}
91811131SErik.Nordmark@Sun.COM 	/*
91911131SErik.Nordmark@Sun.COM 	 * Except for ALL_ZONES, we only match the offlink routes
92011131SErik.Nordmark@Sun.COM 	 * where ire_gateway_addr has an IRE_INTERFACE for the zoneid.
92111457SErik.Nordmark@Sun.COM 	 * Since we can have leftover routes after the IP addresses have
92211457SErik.Nordmark@Sun.COM 	 * changed, the global zone will also match offlink routes where the
92311457SErik.Nordmark@Sun.COM 	 * gateway is unreachable from any zone.
92411131SErik.Nordmark@Sun.COM 	 */
92511131SErik.Nordmark@Sun.COM 	if ((ire->ire_type & IRE_OFFLINK) && zoneid != ALL_ZONES) {
92611131SErik.Nordmark@Sun.COM 		in6_addr_t gw_addr_v6;
92711457SErik.Nordmark@Sun.COM 		boolean_t reach;
92811131SErik.Nordmark@Sun.COM 
92911131SErik.Nordmark@Sun.COM 		if (ire->ire_ipversion == IPV4_VERSION) {
93011457SErik.Nordmark@Sun.COM 			reach = ire_gateway_ok_zone_v4(ire->ire_gateway_addr,
93111457SErik.Nordmark@Sun.COM 			    zoneid, dst_ill, NULL, ipst, B_FALSE);
93211131SErik.Nordmark@Sun.COM 		} else {
93311131SErik.Nordmark@Sun.COM 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
93411131SErik.Nordmark@Sun.COM 			mutex_enter(&ire->ire_lock);
93511131SErik.Nordmark@Sun.COM 			gw_addr_v6 = ire->ire_gateway_addr_v6;
93611131SErik.Nordmark@Sun.COM 			mutex_exit(&ire->ire_lock);
93711131SErik.Nordmark@Sun.COM 
93811457SErik.Nordmark@Sun.COM 			reach = ire_gateway_ok_zone_v6(&gw_addr_v6, zoneid,
93911457SErik.Nordmark@Sun.COM 			    dst_ill, NULL, ipst, B_FALSE);
94011457SErik.Nordmark@Sun.COM 		}
94111457SErik.Nordmark@Sun.COM 		if (!reach) {
94211457SErik.Nordmark@Sun.COM 			if (zoneid != GLOBAL_ZONEID)
94311131SErik.Nordmark@Sun.COM 				return (B_FALSE);
94411457SErik.Nordmark@Sun.COM 
94511457SErik.Nordmark@Sun.COM 			/*
94611457SErik.Nordmark@Sun.COM 			 * Check if ALL_ZONES reachable - if not then let the
94711457SErik.Nordmark@Sun.COM 			 * global zone see it.
94811457SErik.Nordmark@Sun.COM 			 */
94911457SErik.Nordmark@Sun.COM 			if (ire->ire_ipversion == IPV4_VERSION) {
95011457SErik.Nordmark@Sun.COM 				reach = ire_gateway_ok_zone_v4(
95111457SErik.Nordmark@Sun.COM 				    ire->ire_gateway_addr, ALL_ZONES,
95211457SErik.Nordmark@Sun.COM 				    dst_ill, NULL, ipst, B_FALSE);
95311457SErik.Nordmark@Sun.COM 			} else {
95411457SErik.Nordmark@Sun.COM 				reach = ire_gateway_ok_zone_v6(&gw_addr_v6,
95511457SErik.Nordmark@Sun.COM 				    ALL_ZONES, dst_ill, NULL, ipst, B_FALSE);
95611457SErik.Nordmark@Sun.COM 			}
95711457SErik.Nordmark@Sun.COM 			if (reach) {
95811457SErik.Nordmark@Sun.COM 				/*
95911457SErik.Nordmark@Sun.COM 				 * Some other zone could see it, hence hide it
96011457SErik.Nordmark@Sun.COM 				 * in the global zone.
96111457SErik.Nordmark@Sun.COM 				 */
96211457SErik.Nordmark@Sun.COM 				return (B_FALSE);
96311457SErik.Nordmark@Sun.COM 			}
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
9684714Ssowmini 	    (ire->ire_type & ire_type)) &&
9690Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
97011042SErik.Nordmark@Sun.COM 	    (dst_ill == ill ||
97111042SErik.Nordmark@Sun.COM 	    dst_ill != NULL && IS_IN_SAME_ILLGRP(dst_ill, ill)))) {
9720Sstevel@tonic-gate 		return (B_TRUE);
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	return (B_FALSE);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9772535Ssangeeta int
rtfunc(struct radix_node * rn,void * arg)9782535Ssangeeta rtfunc(struct radix_node *rn, void *arg)
9792535Ssangeeta {
9802535Ssangeeta 	struct rtfuncarg *rtf = arg;
9812535Ssangeeta 	struct rt_entry *rt;
9822535Ssangeeta 	irb_t *irb;
9832535Ssangeeta 	ire_t *ire;
9842535Ssangeeta 	boolean_t ret;
9852535Ssangeeta 
9862535Ssangeeta 	rt = (struct rt_entry *)rn;
9872535Ssangeeta 	ASSERT(rt != NULL);
9882535Ssangeeta 	irb = &rt->rt_irb;
9892535Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
9902535Ssangeeta 		if ((rtf->rt_match_flags != 0) ||
9912535Ssangeeta 		    (rtf->rt_zoneid != ALL_ZONES)) {
9922535Ssangeeta 			ret = ire_walk_ill_match(rtf->rt_match_flags,
9932535Ssangeeta 			    rtf->rt_ire_type, ire,
9943448Sdh155122 			    rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
99511042SErik.Nordmark@Sun.COM 		} else {
9962535Ssangeeta 			ret = B_TRUE;
99711042SErik.Nordmark@Sun.COM 		}
9982535Ssangeeta 		if (ret)
9992535Ssangeeta 			(*rtf->rt_func)(ire, rtf->rt_arg);
10002535Ssangeeta 	}
10012535Ssangeeta 	return (0);
10022535Ssangeeta }
10032535Ssangeeta 
10040Sstevel@tonic-gate /*
100511042SErik.Nordmark@Sun.COM  * Walk the ftable entries that match the ill.
10060Sstevel@tonic-gate  */
10072535Ssangeeta void
ire_walk_ill_tables(uint_t match_flags,uint_t ire_type,pfv_t func,void * arg,size_t ftbl_sz,size_t htbl_sz,irb_t ** ipftbl,ill_t * ill,zoneid_t zoneid,ip_stack_t * ipst)10080Sstevel@tonic-gate ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
10091676Sjpk     void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
101011042SErik.Nordmark@Sun.COM     ill_t *ill, zoneid_t zoneid,
10113448Sdh155122     ip_stack_t *ipst)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate 	irb_t	*irb_ptr;
10140Sstevel@tonic-gate 	irb_t	*irb;
10150Sstevel@tonic-gate 	ire_t	*ire;
10160Sstevel@tonic-gate 	int i, j;
10170Sstevel@tonic-gate 	boolean_t ret;
10182535Ssangeeta 	struct rtfuncarg rtfarg;
10190Sstevel@tonic-gate 
10208485SPeter.Memishian@Sun.COM 	ASSERT((!(match_flags & MATCH_IRE_ILL)) || (ill != NULL));
10210Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
102211042SErik.Nordmark@Sun.COM 
102311042SErik.Nordmark@Sun.COM 	/* knobs such that routine is called only for v6 case */
102411042SErik.Nordmark@Sun.COM 	if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
102511042SErik.Nordmark@Sun.COM 		for (i = (ftbl_sz - 1);  i >= 0; i--) {
102611042SErik.Nordmark@Sun.COM 			if ((irb_ptr = ipftbl[i]) == NULL)
102711042SErik.Nordmark@Sun.COM 				continue;
102811042SErik.Nordmark@Sun.COM 			for (j = 0; j < htbl_sz; j++) {
102911042SErik.Nordmark@Sun.COM 				irb = &irb_ptr[j];
103011042SErik.Nordmark@Sun.COM 				if (irb->irb_ire == NULL)
10310Sstevel@tonic-gate 					continue;
103211042SErik.Nordmark@Sun.COM 
103311042SErik.Nordmark@Sun.COM 				irb_refhold(irb);
103411042SErik.Nordmark@Sun.COM 				for (ire = irb->irb_ire; ire != NULL;
103511042SErik.Nordmark@Sun.COM 				    ire = ire->ire_next) {
103611042SErik.Nordmark@Sun.COM 					if (match_flags == 0 &&
103711042SErik.Nordmark@Sun.COM 					    zoneid == ALL_ZONES) {
103811042SErik.Nordmark@Sun.COM 						ret = B_TRUE;
103911042SErik.Nordmark@Sun.COM 					} else {
104011042SErik.Nordmark@Sun.COM 						ret =
104111042SErik.Nordmark@Sun.COM 						    ire_walk_ill_match(
104211042SErik.Nordmark@Sun.COM 						    match_flags,
104311042SErik.Nordmark@Sun.COM 						    ire_type, ire, ill,
104411042SErik.Nordmark@Sun.COM 						    zoneid, ipst);
10450Sstevel@tonic-gate 					}
104611042SErik.Nordmark@Sun.COM 					if (ret)
104711042SErik.Nordmark@Sun.COM 						(*func)(ire, arg);
10480Sstevel@tonic-gate 				}
104911042SErik.Nordmark@Sun.COM 				irb_refrele(irb);
10500Sstevel@tonic-gate 			}
105111042SErik.Nordmark@Sun.COM 		}
105211042SErik.Nordmark@Sun.COM 	} else {
105311131SErik.Nordmark@Sun.COM 		bzero(&rtfarg, sizeof (rtfarg));
105411042SErik.Nordmark@Sun.COM 		rtfarg.rt_func = func;
105511042SErik.Nordmark@Sun.COM 		rtfarg.rt_arg = arg;
105611042SErik.Nordmark@Sun.COM 		if (match_flags != 0) {
105711042SErik.Nordmark@Sun.COM 			rtfarg.rt_match_flags = match_flags;
10580Sstevel@tonic-gate 		}
105911042SErik.Nordmark@Sun.COM 		rtfarg.rt_ire_type = ire_type;
106011042SErik.Nordmark@Sun.COM 		rtfarg.rt_ill = ill;
106111042SErik.Nordmark@Sun.COM 		rtfarg.rt_zoneid = zoneid;
106211042SErik.Nordmark@Sun.COM 		rtfarg.rt_ipst = ipst;	/* No netstack_hold */
106311042SErik.Nordmark@Sun.COM 		(void) ipst->ips_ip_ftable->rnh_walktree_mt(
106411042SErik.Nordmark@Sun.COM 		    ipst->ips_ip_ftable,
106511042SErik.Nordmark@Sun.COM 		    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate /*
10700Sstevel@tonic-gate  * This function takes a mask and returns
10710Sstevel@tonic-gate  * number of bits set in the mask. If no
10720Sstevel@tonic-gate  * bit is set it returns 0.
10730Sstevel@tonic-gate  * Assumes a contiguous mask.
10740Sstevel@tonic-gate  */
10750Sstevel@tonic-gate int
ip_mask_to_plen(ipaddr_t mask)10760Sstevel@tonic-gate ip_mask_to_plen(ipaddr_t mask)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*
10820Sstevel@tonic-gate  * Convert length for a mask to the mask.
10830Sstevel@tonic-gate  */
10840Sstevel@tonic-gate ipaddr_t
ip_plen_to_mask(uint_t masklen)10850Sstevel@tonic-gate ip_plen_to_mask(uint_t masklen)
10860Sstevel@tonic-gate {
108711042SErik.Nordmark@Sun.COM 	if (masklen == 0)
108811042SErik.Nordmark@Sun.COM 		return (0);
108911042SErik.Nordmark@Sun.COM 
10900Sstevel@tonic-gate 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate void
ire_atomic_end(irb_t * irb_ptr,ire_t * ire)10940Sstevel@tonic-gate ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
10950Sstevel@tonic-gate {
109611042SErik.Nordmark@Sun.COM 	ill_t		*ill;
109711042SErik.Nordmark@Sun.COM 
109811042SErik.Nordmark@Sun.COM 	ill = ire->ire_ill;
109911042SErik.Nordmark@Sun.COM 	if (ill != NULL)
110011042SErik.Nordmark@Sun.COM 		mutex_exit(&ill->ill_lock);
11010Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /*
110511042SErik.Nordmark@Sun.COM  * ire_add_v[46] atomically make sure that the ill associated
110611042SErik.Nordmark@Sun.COM  * with the new ire is not going away i.e., we check ILL_CONDEMNED.
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate int
ire_atomic_start(irb_t * irb_ptr,ire_t * ire)110911042SErik.Nordmark@Sun.COM ire_atomic_start(irb_t *irb_ptr, ire_t *ire)
11100Sstevel@tonic-gate {
111111042SErik.Nordmark@Sun.COM 	ill_t		*ill;
111211042SErik.Nordmark@Sun.COM 
111311042SErik.Nordmark@Sun.COM 	ill = ire->ire_ill;
111411042SErik.Nordmark@Sun.COM 
111511042SErik.Nordmark@Sun.COM 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
111611042SErik.Nordmark@Sun.COM 	if (ill != NULL) {
111711042SErik.Nordmark@Sun.COM 		mutex_enter(&ill->ill_lock);
11182416Sjarrett 
11192416Sjarrett 		/*
1120*12570SSowmini.Varadhan@Sun.COM 		 * Don't allow IRE's to be created on dying ills, or on
1121*12570SSowmini.Varadhan@Sun.COM 		 * ill's for which the last ipif is going down, or ones which
1122*12570SSowmini.Varadhan@Sun.COM 		 * don't have even a single UP interface
11232416Sjarrett 		 */
1124*12570SSowmini.Varadhan@Sun.COM 		if ((ill->ill_state_flags &
1125*12570SSowmini.Varadhan@Sun.COM 		    (ILL_CONDEMNED|ILL_DOWN_IN_PROGRESS)) != 0) {
112611042SErik.Nordmark@Sun.COM 			ire_atomic_end(irb_ptr, ire);
1127*12570SSowmini.Varadhan@Sun.COM 			DTRACE_PROBE1(ire__add__on__dying__ill, ire_t *, ire);
112811042SErik.Nordmark@Sun.COM 			return (ENXIO);
112911042SErik.Nordmark@Sun.COM 		}
113011042SErik.Nordmark@Sun.COM 
113111042SErik.Nordmark@Sun.COM 		if (IS_UNDER_IPMP(ill)) {
113211042SErik.Nordmark@Sun.COM 			int	error = 0;
113311042SErik.Nordmark@Sun.COM 			mutex_enter(&ill->ill_phyint->phyint_lock);
113411042SErik.Nordmark@Sun.COM 			if (!ipmp_ill_is_active(ill) &&
113511042SErik.Nordmark@Sun.COM 			    IRE_HIDDEN_TYPE(ire->ire_type) &&
113611042SErik.Nordmark@Sun.COM 			    !ire->ire_testhidden) {
113711042SErik.Nordmark@Sun.COM 				error = EINVAL;
11382416Sjarrett 			}
113911042SErik.Nordmark@Sun.COM 			mutex_exit(&ill->ill_phyint->phyint_lock);
11402416Sjarrett 			if (error != 0) {
114111042SErik.Nordmark@Sun.COM 				ire_atomic_end(irb_ptr, ire);
11422416Sjarrett 				return (error);
11432416Sjarrett 			}
11442416Sjarrett 		}
114511042SErik.Nordmark@Sun.COM 
11460Sstevel@tonic-gate 	}
114711042SErik.Nordmark@Sun.COM 	return (0);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate /*
115111042SErik.Nordmark@Sun.COM  * Add a fully initialized IRE to the forwarding table.
115211042SErik.Nordmark@Sun.COM  * This returns NULL on failure, or a held IRE on success.
115311042SErik.Nordmark@Sun.COM  * Normally the returned IRE is the same as the argument. But a different
115411042SErik.Nordmark@Sun.COM  * IRE will be returned if the added IRE is deemed identical to an existing
115511042SErik.Nordmark@Sun.COM  * one. In that case ire_identical_ref will be increased.
115611042SErik.Nordmark@Sun.COM  * The caller always needs to do an ire_refrele() on the returned IRE.
11570Sstevel@tonic-gate  */
115811042SErik.Nordmark@Sun.COM ire_t *
ire_add(ire_t * ire)115911042SErik.Nordmark@Sun.COM ire_add(ire_t *ire)
116011042SErik.Nordmark@Sun.COM {
116111042SErik.Nordmark@Sun.COM 	if (IRE_HIDDEN_TYPE(ire->ire_type) &&
116211042SErik.Nordmark@Sun.COM 	    ire->ire_ill != NULL && IS_UNDER_IPMP(ire->ire_ill)) {
116311042SErik.Nordmark@Sun.COM 		/*
116411042SErik.Nordmark@Sun.COM 		 * IREs hosted on interfaces that are under IPMP
116511042SErik.Nordmark@Sun.COM 		 * should be hidden so that applications don't
116611042SErik.Nordmark@Sun.COM 		 * accidentally end up sending packets with test
116711042SErik.Nordmark@Sun.COM 		 * addresses as their source addresses, or
116811042SErik.Nordmark@Sun.COM 		 * sending out interfaces that are e.g. IFF_INACTIVE.
116911042SErik.Nordmark@Sun.COM 		 * Hide them here.
117011042SErik.Nordmark@Sun.COM 		 */
117111042SErik.Nordmark@Sun.COM 		ire->ire_testhidden = B_TRUE;
117211042SErik.Nordmark@Sun.COM 	}
117311042SErik.Nordmark@Sun.COM 
117411042SErik.Nordmark@Sun.COM 	if (ire->ire_ipversion == IPV6_VERSION)
117511042SErik.Nordmark@Sun.COM 		return (ire_add_v6(ire));
117611042SErik.Nordmark@Sun.COM 	else
117711042SErik.Nordmark@Sun.COM 		return (ire_add_v4(ire));
117811042SErik.Nordmark@Sun.COM }
117911042SErik.Nordmark@Sun.COM 
118011042SErik.Nordmark@Sun.COM /*
118111042SErik.Nordmark@Sun.COM  * Add a fully initialized IPv4 IRE to the forwarding table.
118211042SErik.Nordmark@Sun.COM  * This returns NULL on failure, or a held IRE on success.
118311042SErik.Nordmark@Sun.COM  * Normally the returned IRE is the same as the argument. But a different
118411042SErik.Nordmark@Sun.COM  * IRE will be returned if the added IRE is deemed identical to an existing
118511042SErik.Nordmark@Sun.COM  * one. In that case ire_identical_ref will be increased.
118611042SErik.Nordmark@Sun.COM  * The caller always needs to do an ire_refrele() on the returned IRE.
118711042SErik.Nordmark@Sun.COM  */
118811042SErik.Nordmark@Sun.COM static ire_t *
ire_add_v4(ire_t * ire)118911042SErik.Nordmark@Sun.COM ire_add_v4(ire_t *ire)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	ire_t	*ire1;
11920Sstevel@tonic-gate 	irb_t	*irb_ptr;
11930Sstevel@tonic-gate 	ire_t	**irep;
119411042SErik.Nordmark@Sun.COM 	int	match_flags;
11950Sstevel@tonic-gate 	int	error;
11963448Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
119711042SErik.Nordmark@Sun.COM 
119811042SErik.Nordmark@Sun.COM 	if (ire->ire_ill != NULL)
119911042SErik.Nordmark@Sun.COM 		ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
12000Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
12030Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
12040Sstevel@tonic-gate 
120511042SErik.Nordmark@Sun.COM 	match_flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
120611042SErik.Nordmark@Sun.COM 
120711042SErik.Nordmark@Sun.COM 	if (ire->ire_ill != NULL) {
120811042SErik.Nordmark@Sun.COM 		match_flags |= MATCH_IRE_ILL;
12090Sstevel@tonic-gate 	}
121011042SErik.Nordmark@Sun.COM 	irb_ptr = ire_get_bucket(ire);
121111042SErik.Nordmark@Sun.COM 	if (irb_ptr == NULL) {
121211042SErik.Nordmark@Sun.COM 		printf("no bucket for %p\n", (void *)ire);
121311042SErik.Nordmark@Sun.COM 		ire_delete(ire);
121411042SErik.Nordmark@Sun.COM 		return (NULL);
12152535Ssangeeta 	}
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	/*
121811042SErik.Nordmark@Sun.COM 	 * Start the atomic add of the ire. Grab the ill lock,
121911042SErik.Nordmark@Sun.COM 	 * the bucket lock. Check for condemned.
12200Sstevel@tonic-gate 	 */
122111042SErik.Nordmark@Sun.COM 	error = ire_atomic_start(irb_ptr, ire);
12220Sstevel@tonic-gate 	if (error != 0) {
122311042SErik.Nordmark@Sun.COM 		printf("no ire_atomic_start for %p\n", (void *)ire);
12240Sstevel@tonic-gate 		ire_delete(ire);
122511042SErik.Nordmark@Sun.COM 		irb_refrele(irb_ptr);
122611042SErik.Nordmark@Sun.COM 		return (NULL);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	/*
122911042SErik.Nordmark@Sun.COM 	 * If we are creating a hidden IRE, make sure we search for
123011042SErik.Nordmark@Sun.COM 	 * hidden IREs when searching for duplicates below.
123111042SErik.Nordmark@Sun.COM 	 * Otherwise, we might find an IRE on some other interface
123211042SErik.Nordmark@Sun.COM 	 * that's not marked hidden.
12330Sstevel@tonic-gate 	 */
123411042SErik.Nordmark@Sun.COM 	if (ire->ire_testhidden)
123511042SErik.Nordmark@Sun.COM 		match_flags |= MATCH_IRE_TESTHIDDEN;
123611042SErik.Nordmark@Sun.COM 
12370Sstevel@tonic-gate 	/*
12380Sstevel@tonic-gate 	 * Atomically check for duplicate and insert in the table.
12390Sstevel@tonic-gate 	 */
12400Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
124111042SErik.Nordmark@Sun.COM 		if (IRE_IS_CONDEMNED(ire1))
12420Sstevel@tonic-gate 			continue;
124311042SErik.Nordmark@Sun.COM 		/*
124411042SErik.Nordmark@Sun.COM 		 * Here we need an exact match on zoneid, i.e.,
124511042SErik.Nordmark@Sun.COM 		 * ire_match_args doesn't fit.
124611042SErik.Nordmark@Sun.COM 		 */
12470Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
12480Sstevel@tonic-gate 			continue;
124911042SErik.Nordmark@Sun.COM 
125011042SErik.Nordmark@Sun.COM 		if (ire1->ire_type != ire->ire_type)
125111042SErik.Nordmark@Sun.COM 			continue;
125211042SErik.Nordmark@Sun.COM 
125311042SErik.Nordmark@Sun.COM 		/*
125411042SErik.Nordmark@Sun.COM 		 * Note: We do not allow multiple routes that differ only
125511042SErik.Nordmark@Sun.COM 		 * in the gateway security attributes; such routes are
125611042SErik.Nordmark@Sun.COM 		 * considered duplicates.
125711042SErik.Nordmark@Sun.COM 		 * To change that we explicitly have to treat them as
125811042SErik.Nordmark@Sun.COM 		 * different here.
125911042SErik.Nordmark@Sun.COM 		 */
12600Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
126111042SErik.Nordmark@Sun.COM 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ill,
126211042SErik.Nordmark@Sun.COM 		    ire->ire_zoneid, NULL, match_flags)) {
12630Sstevel@tonic-gate 			/*
12640Sstevel@tonic-gate 			 * Return the old ire after doing a REFHOLD.
12650Sstevel@tonic-gate 			 * As most of the callers continue to use the IRE
12660Sstevel@tonic-gate 			 * after adding, we return a held ire. This will
12670Sstevel@tonic-gate 			 * avoid a lookup in the caller again. If the callers
12680Sstevel@tonic-gate 			 * don't want to use it, they need to do a REFRELE.
126912328SSowmini.Varadhan@Sun.COM 			 *
127012328SSowmini.Varadhan@Sun.COM 			 * We only allow exactly one IRE_IF_CLONE for any dst,
127112328SSowmini.Varadhan@Sun.COM 			 * so, if the is an IF_CLONE, return the ire without
127212328SSowmini.Varadhan@Sun.COM 			 * an identical_ref, but with an ire_ref held.
12730Sstevel@tonic-gate 			 */
127412328SSowmini.Varadhan@Sun.COM 			if (ire->ire_type != IRE_IF_CLONE) {
127512328SSowmini.Varadhan@Sun.COM 				atomic_add_32(&ire1->ire_identical_ref, 1);
127612328SSowmini.Varadhan@Sun.COM 				DTRACE_PROBE2(ire__add__exist, ire_t *, ire1,
127712328SSowmini.Varadhan@Sun.COM 				    ire_t *, ire);
127812328SSowmini.Varadhan@Sun.COM 			}
127911042SErik.Nordmark@Sun.COM 			ire_refhold(ire1);
12800Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
12810Sstevel@tonic-gate 			ire_delete(ire);
128211042SErik.Nordmark@Sun.COM 			irb_refrele(irb_ptr);
128311042SErik.Nordmark@Sun.COM 			return (ire1);
12842535Ssangeeta 		}
12852535Ssangeeta 	}
128611042SErik.Nordmark@Sun.COM 
12870Sstevel@tonic-gate 	/*
128811042SErik.Nordmark@Sun.COM 	 * Normally we do head insertion since most things do not care about
128911042SErik.Nordmark@Sun.COM 	 * the order of the IREs in the bucket. Note that ip_cgtp_bcast_add
129011042SErik.Nordmark@Sun.COM 	 * assumes we at least do head insertion so that its IRE_BROADCAST
129111042SErik.Nordmark@Sun.COM 	 * arrive ahead of existing IRE_HOST for the same address.
129211042SErik.Nordmark@Sun.COM 	 * However, due to shared-IP zones (and restrict_interzone_loopback)
129311042SErik.Nordmark@Sun.COM 	 * we can have an IRE_LOCAL as well as IRE_IF_CLONE for the same
129411042SErik.Nordmark@Sun.COM 	 * address. For that reason we do tail insertion for IRE_IF_CLONE.
129511042SErik.Nordmark@Sun.COM 	 * Due to the IRE_BROADCAST on cgtp0, which must be last in the bucket,
129611042SErik.Nordmark@Sun.COM 	 * we do tail insertion of IRE_BROADCASTs that do not have RTF_MULTIRT
129711042SErik.Nordmark@Sun.COM 	 * set.
12980Sstevel@tonic-gate 	 */
12990Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
130011042SErik.Nordmark@Sun.COM 	if ((ire->ire_type & IRE_IF_CLONE) ||
130111042SErik.Nordmark@Sun.COM 	    ((ire->ire_type & IRE_BROADCAST) &&
130211042SErik.Nordmark@Sun.COM 	    !(ire->ire_flags & RTF_MULTIRT))) {
130311042SErik.Nordmark@Sun.COM 		while ((ire1 = *irep) != NULL)
13040Sstevel@tonic-gate 			irep = &ire1->ire_next;
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 	/* Insert at *irep */
13070Sstevel@tonic-gate 	ire1 = *irep;
13080Sstevel@tonic-gate 	if (ire1 != NULL)
13090Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
13100Sstevel@tonic-gate 	ire->ire_next = ire1;
13110Sstevel@tonic-gate 	/* Link the new one in. */
13120Sstevel@tonic-gate 	ire->ire_ptpn = irep;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	/*
13150Sstevel@tonic-gate 	 * ire_walk routines de-reference ire_next without holding
13160Sstevel@tonic-gate 	 * a lock. Before we point to the new ire, we want to make
13170Sstevel@tonic-gate 	 * sure the store that sets the ire_next of the new ire
13180Sstevel@tonic-gate 	 * reaches global visibility, so that ire_walk routines
13190Sstevel@tonic-gate 	 * don't see a truncated list of ires i.e if the ire_next
13200Sstevel@tonic-gate 	 * of the new ire gets set after we do "*irep = ire" due
13210Sstevel@tonic-gate 	 * to re-ordering, the ire_walk thread will see a NULL
13220Sstevel@tonic-gate 	 * once it accesses the ire_next of the new ire.
13230Sstevel@tonic-gate 	 * membar_producer() makes sure that the following store
13240Sstevel@tonic-gate 	 * happens *after* all of the above stores.
13250Sstevel@tonic-gate 	 */
13260Sstevel@tonic-gate 	membar_producer();
13270Sstevel@tonic-gate 	*irep = ire;
13280Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
13290Sstevel@tonic-gate 	/*
13300Sstevel@tonic-gate 	 * We return a bumped up IRE above. Keep it symmetrical
13310Sstevel@tonic-gate 	 * so that the callers will always have to release. This
13320Sstevel@tonic-gate 	 * helps the callers of this function because they continue
13330Sstevel@tonic-gate 	 * to use the IRE after adding and hence they don't have to
13340Sstevel@tonic-gate 	 * lookup again after we return the IRE.
13350Sstevel@tonic-gate 	 *
13360Sstevel@tonic-gate 	 * NOTE : We don't have to use atomics as this is appearing
13370Sstevel@tonic-gate 	 * in the list for the first time and no one else can bump
13380Sstevel@tonic-gate 	 * up the reference count on this yet.
13390Sstevel@tonic-gate 	 */
134011042SErik.Nordmark@Sun.COM 	ire_refhold_locked(ire);
13413448Sdh155122 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
13422535Ssangeeta 
13430Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
134411042SErik.Nordmark@Sun.COM 	if (irb_ptr->irb_marks & IRB_MARK_DYNAMIC)
13452535Ssangeeta 		irb_ptr->irb_nire++;
13462535Ssangeeta 
134711042SErik.Nordmark@Sun.COM 	if (ire->ire_ill != NULL) {
134811042SErik.Nordmark@Sun.COM 		ire->ire_ill->ill_ire_cnt++;
134911042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_ill->ill_ire_cnt != 0);	/* Wraparound */
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
135311042SErik.Nordmark@Sun.COM 
135411042SErik.Nordmark@Sun.COM 	/* Make any caching of the IREs be notified or updated */
135511042SErik.Nordmark@Sun.COM 	ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
135611042SErik.Nordmark@Sun.COM 
135711042SErik.Nordmark@Sun.COM 	if (ire->ire_ill != NULL)
135811042SErik.Nordmark@Sun.COM 		ASSERT(!MUTEX_HELD(&ire->ire_ill->ill_lock));
135911042SErik.Nordmark@Sun.COM 	irb_refrele(irb_ptr);
136011042SErik.Nordmark@Sun.COM 	return (ire);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate /*
136411042SErik.Nordmark@Sun.COM  * irb_refrele is the only caller of the function. ire_unlink calls to
13650Sstevel@tonic-gate  * do the final cleanup for this ire.
13660Sstevel@tonic-gate  */
13670Sstevel@tonic-gate void
ire_cleanup(ire_t * ire)13680Sstevel@tonic-gate ire_cleanup(ire_t *ire)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate 	ire_t *ire_next;
13713448Sdh155122 	ip_stack_t *ipst = ire->ire_ipst;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	ASSERT(ire != NULL);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	while (ire != NULL) {
13760Sstevel@tonic-gate 		ire_next = ire->ire_next;
13770Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
13780Sstevel@tonic-gate 			ire_delete_v4(ire);
13793448Sdh155122 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
13803448Sdh155122 			    ire_stats_deleted);
13810Sstevel@tonic-gate 		} else {
13820Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
13830Sstevel@tonic-gate 			ire_delete_v6(ire);
13843448Sdh155122 			BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
13853448Sdh155122 			    ire_stats_deleted);
13860Sstevel@tonic-gate 		}
13870Sstevel@tonic-gate 		/*
13880Sstevel@tonic-gate 		 * Now it's really out of the list. Before doing the
13890Sstevel@tonic-gate 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
13900Sstevel@tonic-gate 		 * so.
13910Sstevel@tonic-gate 		 */
13920Sstevel@tonic-gate 		ire->ire_next = NULL;
139311042SErik.Nordmark@Sun.COM 		ire_refrele_notr(ire);
13940Sstevel@tonic-gate 		ire = ire_next;
13950Sstevel@tonic-gate 	}
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate /*
139911042SErik.Nordmark@Sun.COM  * irb_refrele is the only caller of the function. It calls to unlink
14000Sstevel@tonic-gate  * all the CONDEMNED ires from this bucket.
14010Sstevel@tonic-gate  */
14020Sstevel@tonic-gate ire_t *
ire_unlink(irb_t * irb)14030Sstevel@tonic-gate ire_unlink(irb_t *irb)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate 	ire_t *ire;
14060Sstevel@tonic-gate 	ire_t *ire1;
14070Sstevel@tonic-gate 	ire_t **ptpn;
14080Sstevel@tonic-gate 	ire_t *ire_list = NULL;
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
141111042SErik.Nordmark@Sun.COM 	ASSERT(((irb->irb_marks & IRB_MARK_DYNAMIC) && irb->irb_refcnt == 1) ||
14122535Ssangeeta 	    (irb->irb_refcnt == 0));
14132535Ssangeeta 	ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
14140Sstevel@tonic-gate 	ASSERT(irb->irb_ire != NULL);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
14170Sstevel@tonic-gate 		ire1 = ire->ire_next;
141811042SErik.Nordmark@Sun.COM 		if (IRE_IS_CONDEMNED(ire)) {
14190Sstevel@tonic-gate 			ptpn = ire->ire_ptpn;
14200Sstevel@tonic-gate 			ire1 = ire->ire_next;
14210Sstevel@tonic-gate 			if (ire1)
14220Sstevel@tonic-gate 				ire1->ire_ptpn = ptpn;
14230Sstevel@tonic-gate 			*ptpn = ire1;
14240Sstevel@tonic-gate 			ire->ire_ptpn = NULL;
14250Sstevel@tonic-gate 			ire->ire_next = NULL;
142611042SErik.Nordmark@Sun.COM 
14270Sstevel@tonic-gate 			/*
142811042SErik.Nordmark@Sun.COM 			 * We need to call ire_delete_v4 or ire_delete_v6 to
142911042SErik.Nordmark@Sun.COM 			 * clean up dependents and the redirects pointing at
14300Sstevel@tonic-gate 			 * the default gateway. We need to drop the lock
14310Sstevel@tonic-gate 			 * as ire_flush_cache/ire_delete_host_redircts require
14320Sstevel@tonic-gate 			 * so. But we can't drop the lock, as ire_unlink needs
14330Sstevel@tonic-gate 			 * to atomically remove the ires from the list.
14340Sstevel@tonic-gate 			 * So, create a temporary list of CONDEMNED ires
14350Sstevel@tonic-gate 			 * for doing ire_delete_v4/ire_delete_v6 operations
14360Sstevel@tonic-gate 			 * later on.
14370Sstevel@tonic-gate 			 */
14380Sstevel@tonic-gate 			ire->ire_next = ire_list;
14390Sstevel@tonic-gate 			ire_list = ire;
14400Sstevel@tonic-gate 		}
14410Sstevel@tonic-gate 	}
14422535Ssangeeta 	irb->irb_marks &= ~IRB_MARK_CONDEMNED;
14430Sstevel@tonic-gate 	return (ire_list);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*
144711042SErik.Nordmark@Sun.COM  * Clean up the radix node for this ire. Must be called by irb_refrele
14482535Ssangeeta  * when there are no ire's left in the bucket. Returns TRUE if the bucket
14492535Ssangeeta  * is deleted and freed.
14502535Ssangeeta  */
14512535Ssangeeta boolean_t
irb_inactive(irb_t * irb)14522535Ssangeeta irb_inactive(irb_t *irb)
14532535Ssangeeta {
14542535Ssangeeta 	struct rt_entry *rt;
14552535Ssangeeta 	struct radix_node *rn;
14563448Sdh155122 	ip_stack_t *ipst = irb->irb_ipst;
14573448Sdh155122 
14583448Sdh155122 	ASSERT(irb->irb_ipst != NULL);
14592535Ssangeeta 
14602535Ssangeeta 	rt = IRB2RT(irb);
14612535Ssangeeta 	rn = (struct radix_node *)rt;
14622535Ssangeeta 
14632535Ssangeeta 	/* first remove it from the radix tree. */
14643448Sdh155122 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
14652535Ssangeeta 	rw_enter(&irb->irb_lock, RW_WRITER);
14662535Ssangeeta 	if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
14673448Sdh155122 		rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
14683448Sdh155122 		    ipst->ips_ip_ftable);
14692535Ssangeeta 		DTRACE_PROBE1(irb__free, rt_t *,  rt);
14702535Ssangeeta 		ASSERT((void *)rn == (void *)rt);
14712535Ssangeeta 		Free(rt, rt_entry_cache);
14722535Ssangeeta 		/* irb_lock is freed */
14733448Sdh155122 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
14742535Ssangeeta 		return (B_TRUE);
14752535Ssangeeta 	}
14762535Ssangeeta 	rw_exit(&irb->irb_lock);
14773448Sdh155122 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
14782535Ssangeeta 	return (B_FALSE);
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate /*
14820Sstevel@tonic-gate  * Delete the specified IRE.
148311042SErik.Nordmark@Sun.COM  * We assume that if ire_bucket is not set then ire_ill->ill_ire_cnt was
148411042SErik.Nordmark@Sun.COM  * not incremented i.e., that the insertion in the bucket and the increment
148511042SErik.Nordmark@Sun.COM  * of that counter is done atomically.
14860Sstevel@tonic-gate  */
14870Sstevel@tonic-gate void
ire_delete(ire_t * ire)14880Sstevel@tonic-gate ire_delete(ire_t *ire)
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate 	ire_t	*ire1;
14910Sstevel@tonic-gate 	ire_t	**ptpn;
149211042SErik.Nordmark@Sun.COM 	irb_t	*irb;
14933448Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if ((irb = ire->ire_bucket) == NULL) {
14962535Ssangeeta 		/*
14972535Ssangeeta 		 * It was never inserted in the list. Should call REFRELE
14982535Ssangeeta 		 * to free this IRE.
14992535Ssangeeta 		 */
150011463SSowmini.Varadhan@Sun.COM 		ire_make_condemned(ire);
150111042SErik.Nordmark@Sun.COM 		ire_refrele_notr(ire);
15020Sstevel@tonic-gate 		return;
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 
150511042SErik.Nordmark@Sun.COM 	/*
150611042SErik.Nordmark@Sun.COM 	 * Move the use counts from an IRE_IF_CLONE to its parent
150711042SErik.Nordmark@Sun.COM 	 * IRE_INTERFACE.
150811042SErik.Nordmark@Sun.COM 	 * We need to do this before acquiring irb_lock.
150911042SErik.Nordmark@Sun.COM 	 */
151011042SErik.Nordmark@Sun.COM 	if (ire->ire_type & IRE_IF_CLONE) {
151111042SErik.Nordmark@Sun.COM 		ire_t *parent;
151211042SErik.Nordmark@Sun.COM 
151311042SErik.Nordmark@Sun.COM 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
151411042SErik.Nordmark@Sun.COM 		if ((parent = ire->ire_dep_parent) != NULL) {
151511042SErik.Nordmark@Sun.COM 			parent->ire_ob_pkt_count += ire->ire_ob_pkt_count;
151611042SErik.Nordmark@Sun.COM 			parent->ire_ib_pkt_count += ire->ire_ib_pkt_count;
151711042SErik.Nordmark@Sun.COM 			ire->ire_ob_pkt_count = 0;
151811042SErik.Nordmark@Sun.COM 			ire->ire_ib_pkt_count = 0;
151911042SErik.Nordmark@Sun.COM 		}
152011042SErik.Nordmark@Sun.COM 		rw_exit(&ipst->ips_ire_dep_lock);
152111042SErik.Nordmark@Sun.COM 	}
152211042SErik.Nordmark@Sun.COM 
15230Sstevel@tonic-gate 	rw_enter(&irb->irb_lock, RW_WRITER);
15240Sstevel@tonic-gate 	if (ire->ire_ptpn == NULL) {
15250Sstevel@tonic-gate 		/*
15260Sstevel@tonic-gate 		 * Some other thread has removed us from the list.
15270Sstevel@tonic-gate 		 * It should have done the REFRELE for us.
15280Sstevel@tonic-gate 		 */
15290Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
15300Sstevel@tonic-gate 		return;
15310Sstevel@tonic-gate 	}
15320Sstevel@tonic-gate 
153311042SErik.Nordmark@Sun.COM 	if (!IRE_IS_CONDEMNED(ire)) {
153411042SErik.Nordmark@Sun.COM 		/* Is this an IRE representing multiple duplicate entries? */
153511042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_identical_ref >= 1);
153611042SErik.Nordmark@Sun.COM 		if (atomic_add_32_nv(&ire->ire_identical_ref, -1) != 0) {
153711042SErik.Nordmark@Sun.COM 			/* Removed one of the identical parties */
153811042SErik.Nordmark@Sun.COM 			rw_exit(&irb->irb_lock);
153911042SErik.Nordmark@Sun.COM 			return;
154011042SErik.Nordmark@Sun.COM 		}
154111042SErik.Nordmark@Sun.COM 
15425388Sja97890 		irb->irb_ire_cnt--;
154311042SErik.Nordmark@Sun.COM 		ire_make_condemned(ire);
15445388Sja97890 	}
15455388Sja97890 
15460Sstevel@tonic-gate 	if (irb->irb_refcnt != 0) {
15470Sstevel@tonic-gate 		/*
15480Sstevel@tonic-gate 		 * The last thread to leave this bucket will
15490Sstevel@tonic-gate 		 * delete this ire.
15500Sstevel@tonic-gate 		 */
15512535Ssangeeta 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15520Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
15530Sstevel@tonic-gate 		return;
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	/*
15570Sstevel@tonic-gate 	 * Normally to delete an ire, we walk the bucket. While we
15580Sstevel@tonic-gate 	 * walk the bucket, we normally bump up irb_refcnt and hence
15590Sstevel@tonic-gate 	 * we return from above where we mark CONDEMNED and the ire
15600Sstevel@tonic-gate 	 * gets deleted from ire_unlink. This case is where somebody
15610Sstevel@tonic-gate 	 * knows the ire e.g by doing a lookup, and wants to delete the
15620Sstevel@tonic-gate 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
15630Sstevel@tonic-gate 	 * the bucket.
15640Sstevel@tonic-gate 	 */
15650Sstevel@tonic-gate 	ptpn = ire->ire_ptpn;
15660Sstevel@tonic-gate 	ire1 = ire->ire_next;
15670Sstevel@tonic-gate 	if (ire1 != NULL)
15680Sstevel@tonic-gate 		ire1->ire_ptpn = ptpn;
15690Sstevel@tonic-gate 	ASSERT(ptpn != NULL);
15700Sstevel@tonic-gate 	*ptpn = ire1;
15710Sstevel@tonic-gate 	ire->ire_ptpn = NULL;
15720Sstevel@tonic-gate 	ire->ire_next = NULL;
15730Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
15743448Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
15750Sstevel@tonic-gate 	} else {
15763448Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 	rw_exit(&irb->irb_lock);
15790Sstevel@tonic-gate 
158011042SErik.Nordmark@Sun.COM 	/* Cleanup dependents and related stuff */
15810Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
15820Sstevel@tonic-gate 		ire_delete_v6(ire);
15830Sstevel@tonic-gate 	} else {
15840Sstevel@tonic-gate 		ire_delete_v4(ire);
15850Sstevel@tonic-gate 	}
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * We removed it from the list. Decrement the
15880Sstevel@tonic-gate 	 * reference count.
15890Sstevel@tonic-gate 	 */
159011042SErik.Nordmark@Sun.COM 	ire_refrele_notr(ire);
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate /*
15940Sstevel@tonic-gate  * Delete the specified IRE.
15950Sstevel@tonic-gate  * All calls should use ire_delete().
15960Sstevel@tonic-gate  * Sometimes called as writer though not required by this function.
15970Sstevel@tonic-gate  *
15980Sstevel@tonic-gate  * NOTE : This function is called only if the ire was added
15990Sstevel@tonic-gate  * in the list.
16000Sstevel@tonic-gate  */
16010Sstevel@tonic-gate static void
ire_delete_v4(ire_t * ire)16020Sstevel@tonic-gate ire_delete_v4(ire_t *ire)
16030Sstevel@tonic-gate {
16043448Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
16053448Sdh155122 
16060Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt >= 1);
16070Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
16080Sstevel@tonic-gate 
160911042SErik.Nordmark@Sun.COM 	ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
16100Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
16110Sstevel@tonic-gate 		/*
16120Sstevel@tonic-gate 		 * when a default gateway is going away
16130Sstevel@tonic-gate 		 * delete all the host redirects pointing at that
16140Sstevel@tonic-gate 		 * gateway.
16150Sstevel@tonic-gate 		 */
16163448Sdh155122 		ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
16170Sstevel@tonic-gate 	}
161811042SErik.Nordmark@Sun.COM 
161911042SErik.Nordmark@Sun.COM 	/*
162011042SErik.Nordmark@Sun.COM 	 * If we are deleting an IRE_INTERFACE then we make sure we also
162111042SErik.Nordmark@Sun.COM 	 * delete any IRE_IF_CLONE that has been created from it.
162211042SErik.Nordmark@Sun.COM 	 * Those are always in ire_dep_children.
162311042SErik.Nordmark@Sun.COM 	 */
162411042SErik.Nordmark@Sun.COM 	if ((ire->ire_type & IRE_INTERFACE) && ire->ire_dep_children != NULL)
162511042SErik.Nordmark@Sun.COM 		ire_dep_delete_if_clone(ire);
162611042SErik.Nordmark@Sun.COM 
162711042SErik.Nordmark@Sun.COM 	/* Remove from parent dependencies and child */
162811042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
162911042SErik.Nordmark@Sun.COM 	if (ire->ire_dep_parent != NULL)
163011042SErik.Nordmark@Sun.COM 		ire_dep_remove(ire);
163111042SErik.Nordmark@Sun.COM 
163211042SErik.Nordmark@Sun.COM 	while (ire->ire_dep_children != NULL)
163311042SErik.Nordmark@Sun.COM 		ire_dep_remove(ire->ire_dep_children);
163411042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
16350Sstevel@tonic-gate }
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate /*
163811042SErik.Nordmark@Sun.COM  * ire_refrele is the only caller of the function. It calls
16390Sstevel@tonic-gate  * to free the ire when the reference count goes to zero.
16400Sstevel@tonic-gate  */
16410Sstevel@tonic-gate void
ire_inactive(ire_t * ire)16420Sstevel@tonic-gate ire_inactive(ire_t *ire)
16430Sstevel@tonic-gate {
164411042SErik.Nordmark@Sun.COM 	ill_t	*ill;
16452535Ssangeeta 	irb_t 	*irb;
16463448Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt == 0);
16490Sstevel@tonic-gate 	ASSERT(ire->ire_ptpn == NULL);
16500Sstevel@tonic-gate 	ASSERT(ire->ire_next == NULL);
16510Sstevel@tonic-gate 
165211042SErik.Nordmark@Sun.COM 	/* Count how many condemned ires for kmem_cache callback */
165311463SSowmini.Varadhan@Sun.COM 	ASSERT(IRE_IS_CONDEMNED(ire));
165411463SSowmini.Varadhan@Sun.COM 	atomic_add_32(&ipst->ips_num_ire_condemned, -1);
165511042SErik.Nordmark@Sun.COM 
16562535Ssangeeta 	if (ire->ire_gw_secattr != NULL) {
16572535Ssangeeta 		ire_gw_secattr_free(ire->ire_gw_secattr);
16582535Ssangeeta 		ire->ire_gw_secattr = NULL;
16592535Ssangeeta 	}
16602535Ssangeeta 
166111042SErik.Nordmark@Sun.COM 	/*
166211042SErik.Nordmark@Sun.COM 	 * ire_nce_cache is cleared in ire_delete, and we make sure we don't
166311042SErik.Nordmark@Sun.COM 	 * set it once the ire is marked condemned.
166411042SErik.Nordmark@Sun.COM 	 */
166511042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_nce_cache == NULL);
166611042SErik.Nordmark@Sun.COM 
166711042SErik.Nordmark@Sun.COM 	/*
166811042SErik.Nordmark@Sun.COM 	 * Since any parent would have a refhold on us they would already
166911042SErik.Nordmark@Sun.COM 	 * have been removed.
167011042SErik.Nordmark@Sun.COM 	 */
167111042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_dep_parent == NULL);
167211042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_dep_sib_next == NULL);
167311042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_dep_sib_ptpn == NULL);
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	/*
167611042SErik.Nordmark@Sun.COM 	 * Since any children would have a refhold on us they should have
167711042SErik.Nordmark@Sun.COM 	 * already been removed.
167811042SErik.Nordmark@Sun.COM 	 */
167911042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_dep_children == NULL);
168011042SErik.Nordmark@Sun.COM 
168111042SErik.Nordmark@Sun.COM 	/*
168211042SErik.Nordmark@Sun.COM 	 * ill_ire_ref is increased when the IRE is inserted in the
168311042SErik.Nordmark@Sun.COM 	 * bucket - not when the IRE is created.
16840Sstevel@tonic-gate 	 */
168511042SErik.Nordmark@Sun.COM 	irb = ire->ire_bucket;
168611042SErik.Nordmark@Sun.COM 	ill = ire->ire_ill;
168711042SErik.Nordmark@Sun.COM 	if (irb != NULL && ill != NULL) {
16880Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
168911042SErik.Nordmark@Sun.COM 		ASSERT(ill->ill_ire_cnt != 0);
169011042SErik.Nordmark@Sun.COM 		DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
16916255Ssowmini 		    (char *), "ire", (void *), ire);
169211042SErik.Nordmark@Sun.COM 		ill->ill_ire_cnt--;
169311042SErik.Nordmark@Sun.COM 		if (ILL_DOWN_OK(ill)) {
16940Sstevel@tonic-gate 			/* Drops the ill lock */
16950Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
16960Sstevel@tonic-gate 		} else {
16970Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 	}
170011042SErik.Nordmark@Sun.COM 	ire->ire_ill = NULL;
170111042SErik.Nordmark@Sun.COM 
17020Sstevel@tonic-gate 	/* This should be true for both V4 and V6 */
170311042SErik.Nordmark@Sun.COM 	if (irb != NULL && (irb->irb_marks & IRB_MARK_DYNAMIC)) {
17042535Ssangeeta 		rw_enter(&irb->irb_lock, RW_WRITER);
17052535Ssangeeta 		irb->irb_nire--;
17062535Ssangeeta 		/*
17072535Ssangeeta 		 * Instead of examining the conditions for freeing
17082535Ssangeeta 		 * the radix node here, we do it by calling
170911042SErik.Nordmark@Sun.COM 		 * irb_refrele which is a single point in the code
17102535Ssangeeta 		 * that embeds that logic. Bump up the refcnt to
171111042SErik.Nordmark@Sun.COM 		 * be able to call irb_refrele
17122535Ssangeeta 		 */
171311042SErik.Nordmark@Sun.COM 		irb_refhold_locked(irb);
17142535Ssangeeta 		rw_exit(&irb->irb_lock);
171511042SErik.Nordmark@Sun.COM 		irb_refrele(irb);
17162535Ssangeeta 	}
17170Sstevel@tonic-gate 
17185023Scarlsonj #ifdef DEBUG
17195023Scarlsonj 	ire_trace_cleanup(ire);
17200Sstevel@tonic-gate #endif
17210Sstevel@tonic-gate 	mutex_destroy(&ire->ire_lock);
17220Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
17233448Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
17240Sstevel@tonic-gate 	} else {
17253448Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
17260Sstevel@tonic-gate 	}
17272535Ssangeeta 	kmem_cache_free(ire_cache, ire);
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate /*
173111042SErik.Nordmark@Sun.COM  * ire_update_generation is the callback function provided by
173211042SErik.Nordmark@Sun.COM  * ire_get_bucket() to update the generation number of any
173311042SErik.Nordmark@Sun.COM  * matching shorter route when a new route is added.
173411042SErik.Nordmark@Sun.COM  *
173511042SErik.Nordmark@Sun.COM  * This fucntion always returns a failure return (B_FALSE)
173611042SErik.Nordmark@Sun.COM  * to force the caller (rn_matchaddr_args)
173711042SErik.Nordmark@Sun.COM  * to back-track up the tree looking for shorter matches.
17380Sstevel@tonic-gate  */
173911042SErik.Nordmark@Sun.COM /* ARGSUSED */
174011042SErik.Nordmark@Sun.COM static boolean_t
ire_update_generation(struct radix_node * rn,void * arg)174111042SErik.Nordmark@Sun.COM ire_update_generation(struct radix_node *rn, void *arg)
17420Sstevel@tonic-gate {
174311042SErik.Nordmark@Sun.COM 	struct rt_entry *rt = (struct rt_entry *)rn;
174411042SErik.Nordmark@Sun.COM 
174511042SErik.Nordmark@Sun.COM 	/* We need to handle all in the same bucket */
174611042SErik.Nordmark@Sun.COM 	irb_increment_generation(&rt->rt_irb);
174711042SErik.Nordmark@Sun.COM 	return (B_FALSE);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate /*
175111042SErik.Nordmark@Sun.COM  * Take care of all the generation numbers in the bucket.
175211042SErik.Nordmark@Sun.COM  */
175311042SErik.Nordmark@Sun.COM void
irb_increment_generation(irb_t * irb)175411042SErik.Nordmark@Sun.COM irb_increment_generation(irb_t *irb)
175511042SErik.Nordmark@Sun.COM {
175611042SErik.Nordmark@Sun.COM 	ire_t *ire;
175711463SSowmini.Varadhan@Sun.COM 	ip_stack_t *ipst;
175811042SErik.Nordmark@Sun.COM 
175911042SErik.Nordmark@Sun.COM 	if (irb == NULL || irb->irb_ire_cnt == 0)
176011042SErik.Nordmark@Sun.COM 		return;
176111042SErik.Nordmark@Sun.COM 
176211463SSowmini.Varadhan@Sun.COM 	ipst = irb->irb_ipst;
176311463SSowmini.Varadhan@Sun.COM 	/*
176411463SSowmini.Varadhan@Sun.COM 	 * we cannot do an irb_refhold/irb_refrele here as the caller
176511463SSowmini.Varadhan@Sun.COM 	 * already has the global RADIX_NODE_HEAD_WLOCK, and the irb_refrele
176611463SSowmini.Varadhan@Sun.COM 	 * may result in an attempt to free the irb_t, which also needs
176711463SSowmini.Varadhan@Sun.COM 	 * the RADIX_NODE_HEAD lock. However, since we want to traverse the
176811463SSowmini.Varadhan@Sun.COM 	 * irb_ire list without fear of having a condemned ire removed from
176911463SSowmini.Varadhan@Sun.COM 	 * the list, we acquire the irb_lock as WRITER. Moreover, since
177011463SSowmini.Varadhan@Sun.COM 	 * the ire_generation increments are done under the ire_dep_lock,
177111463SSowmini.Varadhan@Sun.COM 	 * acquire the locks in the prescribed lock order first.
177211463SSowmini.Varadhan@Sun.COM 	 */
177311463SSowmini.Varadhan@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
177411463SSowmini.Varadhan@Sun.COM 	rw_enter(&irb->irb_lock, RW_WRITER);
177511042SErik.Nordmark@Sun.COM 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
177611042SErik.Nordmark@Sun.COM 		if (!IRE_IS_CONDEMNED(ire))
177711042SErik.Nordmark@Sun.COM 			ire_increment_generation(ire);	/* Ourselves */
177811463SSowmini.Varadhan@Sun.COM 		ire_dep_incr_generation_locked(ire);	/* Dependants */
177911042SErik.Nordmark@Sun.COM 	}
178011463SSowmini.Varadhan@Sun.COM 	rw_exit(&irb->irb_lock);
178111463SSowmini.Varadhan@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
178211042SErik.Nordmark@Sun.COM }
178311042SErik.Nordmark@Sun.COM 
178411042SErik.Nordmark@Sun.COM /*
178511042SErik.Nordmark@Sun.COM  * When an IRE is added or deleted this routine is called to make sure
178611042SErik.Nordmark@Sun.COM  * any caching of IRE information is notified or updated.
17870Sstevel@tonic-gate  *
17880Sstevel@tonic-gate  * The flag argument indicates if the flush request is due to addition
178911042SErik.Nordmark@Sun.COM  * of new route (IRE_FLUSH_ADD), deletion of old route (IRE_FLUSH_DELETE),
179011042SErik.Nordmark@Sun.COM  * or a change to ire_gateway_addr (IRE_FLUSH_GWCHANGE).
17910Sstevel@tonic-gate  */
17920Sstevel@tonic-gate void
ire_flush_cache_v4(ire_t * ire,int flag)17930Sstevel@tonic-gate ire_flush_cache_v4(ire_t *ire, int flag)
17940Sstevel@tonic-gate {
179511042SErik.Nordmark@Sun.COM 	irb_t *irb = ire->ire_bucket;
179611042SErik.Nordmark@Sun.COM 	struct rt_entry *rt = IRB2RT(irb);
179711042SErik.Nordmark@Sun.COM 	ip_stack_t *ipst = ire->ire_ipst;
179811042SErik.Nordmark@Sun.COM 
179911042SErik.Nordmark@Sun.COM 	/*
180011042SErik.Nordmark@Sun.COM 	 * IRE_IF_CLONE ire's don't provide any new information
180111042SErik.Nordmark@Sun.COM 	 * than the parent from which they are cloned, so don't
180211042SErik.Nordmark@Sun.COM 	 * perturb the generation numbers.
180311042SErik.Nordmark@Sun.COM 	 */
180411042SErik.Nordmark@Sun.COM 	if (ire->ire_type & IRE_IF_CLONE)
18054714Ssowmini 		return;
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	/*
180811042SErik.Nordmark@Sun.COM 	 * Ensure that an ire_add during a lookup serializes the updates of the
180911042SErik.Nordmark@Sun.COM 	 * generation numbers under the radix head lock so that the lookup gets
181011042SErik.Nordmark@Sun.COM 	 * either the old ire and old generation number, or a new ire and new
181111042SErik.Nordmark@Sun.COM 	 * generation number.
181211042SErik.Nordmark@Sun.COM 	 */
181311042SErik.Nordmark@Sun.COM 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
181411042SErik.Nordmark@Sun.COM 
181511042SErik.Nordmark@Sun.COM 	/*
181611042SErik.Nordmark@Sun.COM 	 * If a route was just added, we need to notify everybody that
181711042SErik.Nordmark@Sun.COM 	 * has cached an IRE_NOROUTE since there might now be a better
181811042SErik.Nordmark@Sun.COM 	 * route for them.
18190Sstevel@tonic-gate 	 */
182011042SErik.Nordmark@Sun.COM 	if (flag == IRE_FLUSH_ADD) {
182111042SErik.Nordmark@Sun.COM 		ire_increment_generation(ipst->ips_ire_reject_v4);
182211042SErik.Nordmark@Sun.COM 		ire_increment_generation(ipst->ips_ire_blackhole_v4);
182311042SErik.Nordmark@Sun.COM 	}
182411042SErik.Nordmark@Sun.COM 
182511042SErik.Nordmark@Sun.COM 	/* Adding a default can't otherwise provide a better route */
182611042SErik.Nordmark@Sun.COM 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD) {
182711042SErik.Nordmark@Sun.COM 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
18280Sstevel@tonic-gate 		return;
182911042SErik.Nordmark@Sun.COM 	}
183011042SErik.Nordmark@Sun.COM 
183111042SErik.Nordmark@Sun.COM 	switch (flag) {
183211042SErik.Nordmark@Sun.COM 	case IRE_FLUSH_DELETE:
183311042SErik.Nordmark@Sun.COM 	case IRE_FLUSH_GWCHANGE:
18340Sstevel@tonic-gate 		/*
183511042SErik.Nordmark@Sun.COM 		 * Update ire_generation for all ire_dep_children chains
183611042SErik.Nordmark@Sun.COM 		 * starting with this IRE
18370Sstevel@tonic-gate 		 */
183811042SErik.Nordmark@Sun.COM 		ire_dep_incr_generation(ire);
183911042SErik.Nordmark@Sun.COM 		break;
184011042SErik.Nordmark@Sun.COM 	case IRE_FLUSH_ADD:
18410Sstevel@tonic-gate 		/*
184211042SErik.Nordmark@Sun.COM 		 * Update the generation numbers of all shorter matching routes.
184311042SErik.Nordmark@Sun.COM 		 * ire_update_generation takes care of the dependants by
184411042SErik.Nordmark@Sun.COM 		 * using ire_dep_incr_generation.
18450Sstevel@tonic-gate 		 */
184611042SErik.Nordmark@Sun.COM 		(void) ipst->ips_ip_ftable->rnh_matchaddr_args(&rt->rt_dst,
184711042SErik.Nordmark@Sun.COM 		    ipst->ips_ip_ftable, ire_update_generation, NULL);
184811042SErik.Nordmark@Sun.COM 		break;
18490Sstevel@tonic-gate 	}
185011042SErik.Nordmark@Sun.COM 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
18510Sstevel@tonic-gate }
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate /*
18540Sstevel@tonic-gate  * Matches the arguments passed with the values in the ire.
18550Sstevel@tonic-gate  *
185611042SErik.Nordmark@Sun.COM  * Note: for match types that match using "ill" passed in, ill
18570Sstevel@tonic-gate  * must be checked for non-NULL before calling this routine.
18580Sstevel@tonic-gate  */
18592535Ssangeeta boolean_t
ire_match_args(ire_t * ire,ipaddr_t addr,ipaddr_t mask,ipaddr_t gateway,int type,const ill_t * ill,zoneid_t zoneid,const ts_label_t * tsl,int match_flags)18600Sstevel@tonic-gate ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
186111042SErik.Nordmark@Sun.COM     int type, const ill_t *ill, zoneid_t zoneid,
186211042SErik.Nordmark@Sun.COM     const ts_label_t *tsl, int match_flags)
18630Sstevel@tonic-gate {
18640Sstevel@tonic-gate 	ill_t *ire_ill = NULL, *dst_ill;
186511042SErik.Nordmark@Sun.COM 	ip_stack_t *ipst = ire->ire_ipst;
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
18680Sstevel@tonic-gate 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
186911681SSowmini.Varadhan@Sun.COM 	ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_SRC_ILL))) ||
187011042SErik.Nordmark@Sun.COM 	    (ill != NULL && !ill->ill_isv6));
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	/*
187311042SErik.Nordmark@Sun.COM 	 * If MATCH_IRE_TESTHIDDEN is set, then only return the IRE if it is
187411042SErik.Nordmark@Sun.COM 	 * in fact hidden, to ensure the caller gets the right one.
18750Sstevel@tonic-gate 	 */
187611042SErik.Nordmark@Sun.COM 	if (ire->ire_testhidden) {
187711042SErik.Nordmark@Sun.COM 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))
18788485SPeter.Memishian@Sun.COM 			return (B_FALSE);
18798485SPeter.Memishian@Sun.COM 	}
18800Sstevel@tonic-gate 
18811676Sjpk 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
18821676Sjpk 	    ire->ire_zoneid != ALL_ZONES) {
18830Sstevel@tonic-gate 		/*
188411042SErik.Nordmark@Sun.COM 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid
188511042SErik.Nordmark@Sun.COM 		 * does not match that of ire_zoneid, a failure to
18860Sstevel@tonic-gate 		 * match is reported at this point. Otherwise, since some IREs
18870Sstevel@tonic-gate 		 * that are available in the global zone can be used in local
18880Sstevel@tonic-gate 		 * zones, additional checks need to be performed:
18890Sstevel@tonic-gate 		 *
189011042SErik.Nordmark@Sun.COM 		 * IRE_LOOPBACK
18910Sstevel@tonic-gate 		 *	entries should never be matched in this situation.
189211042SErik.Nordmark@Sun.COM 		 *	Each zone has its own IRE_LOOPBACK.
18930Sstevel@tonic-gate 		 *
189411042SErik.Nordmark@Sun.COM 		 * IRE_LOCAL
189511042SErik.Nordmark@Sun.COM 		 *	We allow them for any zoneid. ire_route_recursive
189611042SErik.Nordmark@Sun.COM 		 *	does additional checks when
189711042SErik.Nordmark@Sun.COM 		 *	ip_restrict_interzone_loopback is set.
18980Sstevel@tonic-gate 		 *
189911042SErik.Nordmark@Sun.COM 		 * If ill_usesrc_ifindex is set
190011042SErik.Nordmark@Sun.COM 		 *	Then we check if the zone has a valid source address
190111042SErik.Nordmark@Sun.COM 		 *	on the usesrc ill.
19020Sstevel@tonic-gate 		 *
190311042SErik.Nordmark@Sun.COM 		 * If ire_ill is set, then check that the zone has an ipif
190411042SErik.Nordmark@Sun.COM 		 *	on that ill.
190511042SErik.Nordmark@Sun.COM 		 *
190611042SErik.Nordmark@Sun.COM 		 * Outside of this function (in ire_round_robin) we check
190711042SErik.Nordmark@Sun.COM 		 * that any IRE_OFFLINK has a gateway that reachable from the
190811042SErik.Nordmark@Sun.COM 		 * zone when we have multiple choices (ECMP).
19090Sstevel@tonic-gate 		 */
19100Sstevel@tonic-gate 		if (match_flags & MATCH_IRE_ZONEONLY)
19110Sstevel@tonic-gate 			return (B_FALSE);
191211042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_LOOPBACK)
19130Sstevel@tonic-gate 			return (B_FALSE);
191411042SErik.Nordmark@Sun.COM 
191511042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_LOCAL)
191611042SErik.Nordmark@Sun.COM 			goto matchit;
191711042SErik.Nordmark@Sun.COM 
19180Sstevel@tonic-gate 		/*
191911042SErik.Nordmark@Sun.COM 		 * The normal case of IRE_ONLINK has a matching zoneid.
192011042SErik.Nordmark@Sun.COM 		 * Here we handle the case when shared-IP zones have been
192111042SErik.Nordmark@Sun.COM 		 * configured with IP addresses on vniN. In that case it
192211042SErik.Nordmark@Sun.COM 		 * is ok for traffic from a zone to use IRE_ONLINK routes
192311042SErik.Nordmark@Sun.COM 		 * if the ill has a usesrc pointing at vniN
19240Sstevel@tonic-gate 		 */
192511042SErik.Nordmark@Sun.COM 		dst_ill = ire->ire_ill;
192611042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_ONLINK) {
192711042SErik.Nordmark@Sun.COM 			uint_t	ifindex;
192811042SErik.Nordmark@Sun.COM 
192911042SErik.Nordmark@Sun.COM 			/*
193011042SErik.Nordmark@Sun.COM 			 * Note there is no IRE_INTERFACE on vniN thus
193111042SErik.Nordmark@Sun.COM 			 * can't do an IRE lookup for a matching route.
193211042SErik.Nordmark@Sun.COM 			 */
193311042SErik.Nordmark@Sun.COM 			ifindex = dst_ill->ill_usesrc_ifindex;
193411042SErik.Nordmark@Sun.COM 			if (ifindex == 0)
193511042SErik.Nordmark@Sun.COM 				return (B_FALSE);
193611042SErik.Nordmark@Sun.COM 
19370Sstevel@tonic-gate 			/*
19380Sstevel@tonic-gate 			 * If there is a usable source address in the
193911042SErik.Nordmark@Sun.COM 			 * zone, then it's ok to return this IRE_INTERFACE
19400Sstevel@tonic-gate 			 */
194111042SErik.Nordmark@Sun.COM 			if (!ipif_zone_avail(ifindex, dst_ill->ill_isv6,
194211042SErik.Nordmark@Sun.COM 			    zoneid, ipst)) {
194311042SErik.Nordmark@Sun.COM 				ip3dbg(("ire_match_args: no usrsrc for zone"
19440Sstevel@tonic-gate 				    " dst_ill %p\n", (void *)dst_ill));
19450Sstevel@tonic-gate 				return (B_FALSE);
19460Sstevel@tonic-gate 			}
19470Sstevel@tonic-gate 		}
194811042SErik.Nordmark@Sun.COM 		/*
194911681SSowmini.Varadhan@Sun.COM 		 * For example, with
195011042SErik.Nordmark@Sun.COM 		 * route add 11.0.0.0 gw1 -ifp bge0
195111042SErik.Nordmark@Sun.COM 		 * route add 11.0.0.0 gw2 -ifp bge1
195211042SErik.Nordmark@Sun.COM 		 * this code would differentiate based on
195311042SErik.Nordmark@Sun.COM 		 * where the sending zone has addresses.
195411042SErik.Nordmark@Sun.COM 		 * Only if the zone has an address on bge0 can it use the first
195511042SErik.Nordmark@Sun.COM 		 * route. It isn't clear if this behavior is documented
195611042SErik.Nordmark@Sun.COM 		 * anywhere.
195711042SErik.Nordmark@Sun.COM 		 */
195811042SErik.Nordmark@Sun.COM 		if (dst_ill != NULL && (ire->ire_type & IRE_OFFLINK)) {
19590Sstevel@tonic-gate 			ipif_t	*tipif;
19600Sstevel@tonic-gate 
196111042SErik.Nordmark@Sun.COM 			mutex_enter(&dst_ill->ill_lock);
196211042SErik.Nordmark@Sun.COM 			for (tipif = dst_ill->ill_ipif;
19630Sstevel@tonic-gate 			    tipif != NULL; tipif = tipif->ipif_next) {
196411042SErik.Nordmark@Sun.COM 				if (!IPIF_IS_CONDEMNED(tipif) &&
19650Sstevel@tonic-gate 				    (tipif->ipif_flags & IPIF_UP) &&
19661676Sjpk 				    (tipif->ipif_zoneid == zoneid ||
19671676Sjpk 				    tipif->ipif_zoneid == ALL_ZONES))
19680Sstevel@tonic-gate 					break;
19690Sstevel@tonic-gate 			}
197011042SErik.Nordmark@Sun.COM 			mutex_exit(&dst_ill->ill_lock);
19710Sstevel@tonic-gate 			if (tipif == NULL) {
19720Sstevel@tonic-gate 				return (B_FALSE);
19730Sstevel@tonic-gate 			}
19740Sstevel@tonic-gate 		}
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
197711042SErik.Nordmark@Sun.COM matchit:
197811681SSowmini.Varadhan@Sun.COM 	ire_ill = ire->ire_ill;
19798485SPeter.Memishian@Sun.COM 	if (match_flags & MATCH_IRE_ILL) {
198011042SErik.Nordmark@Sun.COM 
198111042SErik.Nordmark@Sun.COM 		/*
198211042SErik.Nordmark@Sun.COM 		 * If asked to match an ill, we *must* match
198311042SErik.Nordmark@Sun.COM 		 * on the ire_ill for ipmp test addresses, or
198411042SErik.Nordmark@Sun.COM 		 * any of the ill in the group for data addresses.
198511042SErik.Nordmark@Sun.COM 		 * If we don't, we may as well fail.
198611042SErik.Nordmark@Sun.COM 		 * However, we need an exception for IRE_LOCALs to ensure
198711042SErik.Nordmark@Sun.COM 		 * we loopback packets even sent to test addresses on different
198811042SErik.Nordmark@Sun.COM 		 * interfaces in the group.
198911042SErik.Nordmark@Sun.COM 		 */
199011042SErik.Nordmark@Sun.COM 		if ((match_flags & MATCH_IRE_TESTHIDDEN) &&
199111042SErik.Nordmark@Sun.COM 		    !(ire->ire_type & IRE_LOCAL)) {
199211042SErik.Nordmark@Sun.COM 			if (ire->ire_ill != ill)
199311042SErik.Nordmark@Sun.COM 				return (B_FALSE);
199411042SErik.Nordmark@Sun.COM 		} else  {
199511042SErik.Nordmark@Sun.COM 			match_flags &= ~MATCH_IRE_TESTHIDDEN;
199611042SErik.Nordmark@Sun.COM 			/*
199711042SErik.Nordmark@Sun.COM 			 * We know that ill is not NULL, but ire_ill could be
199811042SErik.Nordmark@Sun.COM 			 * NULL
199911042SErik.Nordmark@Sun.COM 			 */
200011042SErik.Nordmark@Sun.COM 			if (ire_ill == NULL || !IS_ON_SAME_LAN(ill, ire_ill))
200111042SErik.Nordmark@Sun.COM 				return (B_FALSE);
200211042SErik.Nordmark@Sun.COM 		}
20030Sstevel@tonic-gate 	}
200411681SSowmini.Varadhan@Sun.COM 	if (match_flags & MATCH_IRE_SRC_ILL) {
200511681SSowmini.Varadhan@Sun.COM 		if (ire_ill == NULL)
200611681SSowmini.Varadhan@Sun.COM 			return (B_FALSE);
200711681SSowmini.Varadhan@Sun.COM 		if (!IS_ON_SAME_LAN(ill, ire_ill)) {
200811681SSowmini.Varadhan@Sun.COM 			if (ire_ill->ill_usesrc_ifindex == 0 ||
200911681SSowmini.Varadhan@Sun.COM 			    (ire_ill->ill_usesrc_ifindex !=
201011681SSowmini.Varadhan@Sun.COM 			    ill->ill_phyint->phyint_ifindex))
201111681SSowmini.Varadhan@Sun.COM 				return (B_FALSE);
201211681SSowmini.Varadhan@Sun.COM 		}
201311681SSowmini.Varadhan@Sun.COM 	}
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	if ((ire->ire_addr == (addr & mask)) &&
20160Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_GW)) ||
20174714Ssowmini 	    (ire->ire_gateway_addr == gateway)) &&
201812038SSowmini.Varadhan@Sun.COM 	    ((!(match_flags & MATCH_IRE_DIRECT)) ||
201912038SSowmini.Varadhan@Sun.COM 	    !(ire->ire_flags & RTF_INDIRECT)) &&
202011042SErik.Nordmark@Sun.COM 	    ((!(match_flags & MATCH_IRE_TYPE)) || (ire->ire_type & type)) &&
202111042SErik.Nordmark@Sun.COM 	    ((!(match_flags & MATCH_IRE_TESTHIDDEN)) || ire->ire_testhidden) &&
202211042SErik.Nordmark@Sun.COM 	    ((!(match_flags & MATCH_IRE_MASK)) || (ire->ire_mask == mask)) &&
20231676Sjpk 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
20244714Ssowmini 	    (!is_system_labeled()) ||
20254714Ssowmini 	    (tsol_ire_match_gwattr(ire, tsl) == 0))) {
20260Sstevel@tonic-gate 		/* We found the matched IRE */
20270Sstevel@tonic-gate 		return (B_TRUE);
20280Sstevel@tonic-gate 	}
20290Sstevel@tonic-gate 	return (B_FALSE);
20300Sstevel@tonic-gate }
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate /*
203311042SErik.Nordmark@Sun.COM  * Check if the IRE_LOCAL uses the same ill as another route would use.
203411042SErik.Nordmark@Sun.COM  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
203511042SErik.Nordmark@Sun.COM  * then we don't allow this IRE_LOCAL to be used.
203611042SErik.Nordmark@Sun.COM  * We always return an IRE; will be RTF_REJECT if no route available.
20370Sstevel@tonic-gate  */
20380Sstevel@tonic-gate ire_t *
ire_alt_local(ire_t * ire,zoneid_t zoneid,const ts_label_t * tsl,const ill_t * ill,uint_t * generationp)203911042SErik.Nordmark@Sun.COM ire_alt_local(ire_t *ire, zoneid_t zoneid, const ts_label_t *tsl,
204011042SErik.Nordmark@Sun.COM     const ill_t *ill, uint_t *generationp)
20410Sstevel@tonic-gate {
204211042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
204311042SErik.Nordmark@Sun.COM 	ire_t		*alt_ire;
204411042SErik.Nordmark@Sun.COM 	uint_t		ire_type;
204511042SErik.Nordmark@Sun.COM 	uint_t		generation;
204611042SErik.Nordmark@Sun.COM 	uint_t		match_flags;
204711042SErik.Nordmark@Sun.COM 
204811042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_type & IRE_LOCAL);
204911042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_ill != NULL);
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	/*
205211042SErik.Nordmark@Sun.COM 	 * Need to match on everything but local.
205311042SErik.Nordmark@Sun.COM 	 * This might result in the creation of a IRE_IF_CLONE for the
205411042SErik.Nordmark@Sun.COM 	 * same address as the IRE_LOCAL when restrict_interzone_loopback is
205511042SErik.Nordmark@Sun.COM 	 * set. ire_add_*() ensures that the IRE_IF_CLONE are tail inserted
205611042SErik.Nordmark@Sun.COM 	 * to make sure the IRE_LOCAL is always found first.
20570Sstevel@tonic-gate 	 */
205811042SErik.Nordmark@Sun.COM 	ire_type = (IRE_ONLINK | IRE_OFFLINK) & ~(IRE_LOCAL|IRE_LOOPBACK);
205911042SErik.Nordmark@Sun.COM 	match_flags = MATCH_IRE_TYPE | MATCH_IRE_SECATTR;
206011042SErik.Nordmark@Sun.COM 	if (ill != NULL)
206111042SErik.Nordmark@Sun.COM 		match_flags |= MATCH_IRE_ILL;
206211042SErik.Nordmark@Sun.COM 
206311042SErik.Nordmark@Sun.COM 	if (ire->ire_ipversion == IPV4_VERSION) {
206411042SErik.Nordmark@Sun.COM 		alt_ire = ire_route_recursive_v4(ire->ire_addr, ire_type,
206511457SErik.Nordmark@Sun.COM 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
206611457SErik.Nordmark@Sun.COM 		    NULL, &generation);
206711042SErik.Nordmark@Sun.COM 	} else {
206811042SErik.Nordmark@Sun.COM 		alt_ire = ire_route_recursive_v6(&ire->ire_addr_v6, ire_type,
206911457SErik.Nordmark@Sun.COM 		    ill, zoneid, tsl, match_flags, IRR_ALLOCATE, 0, ipst, NULL,
207011457SErik.Nordmark@Sun.COM 		    NULL, &generation);
20710Sstevel@tonic-gate 	}
207211042SErik.Nordmark@Sun.COM 	ASSERT(alt_ire != NULL);
207311042SErik.Nordmark@Sun.COM 
207411042SErik.Nordmark@Sun.COM 	if (alt_ire->ire_ill == ire->ire_ill) {
207511042SErik.Nordmark@Sun.COM 		/* Going out the same ILL - ok to send to IRE_LOCAL */
207611042SErik.Nordmark@Sun.COM 		ire_refrele(alt_ire);
207711042SErik.Nordmark@Sun.COM 	} else {
207811042SErik.Nordmark@Sun.COM 		/* Different ill - ignore IRE_LOCAL */
207911042SErik.Nordmark@Sun.COM 		ire_refrele(ire);
208011042SErik.Nordmark@Sun.COM 		ire = alt_ire;
208111042SErik.Nordmark@Sun.COM 		if (generationp != NULL)
208211042SErik.Nordmark@Sun.COM 			*generationp = generation;
20830Sstevel@tonic-gate 	}
20840Sstevel@tonic-gate 	return (ire);
20850Sstevel@tonic-gate }
20860Sstevel@tonic-gate 
208711042SErik.Nordmark@Sun.COM boolean_t
ire_find_zoneid(struct radix_node * rn,void * arg)208811042SErik.Nordmark@Sun.COM ire_find_zoneid(struct radix_node *rn, void *arg)
20891676Sjpk {
209011042SErik.Nordmark@Sun.COM 	struct rt_entry *rt = (struct rt_entry *)rn;
20911676Sjpk 	irb_t *irb;
20921676Sjpk 	ire_t *ire;
209311042SErik.Nordmark@Sun.COM 	ire_ftable_args_t *margs = arg;
209411042SErik.Nordmark@Sun.COM 
209511042SErik.Nordmark@Sun.COM 	ASSERT(rt != NULL);
209611042SErik.Nordmark@Sun.COM 
209711042SErik.Nordmark@Sun.COM 	irb = &rt->rt_irb;
209811042SErik.Nordmark@Sun.COM 
209911042SErik.Nordmark@Sun.COM 	if (irb->irb_ire_cnt == 0)
210011042SErik.Nordmark@Sun.COM 		return (B_FALSE);
210111042SErik.Nordmark@Sun.COM 
210211042SErik.Nordmark@Sun.COM 	rw_enter(&irb->irb_lock, RW_READER);
21031676Sjpk 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
210411042SErik.Nordmark@Sun.COM 		if (IRE_IS_CONDEMNED(ire))
21051676Sjpk 			continue;
21061676Sjpk 
210711131SErik.Nordmark@Sun.COM 		if (!(ire->ire_type & IRE_INTERFACE))
210811131SErik.Nordmark@Sun.COM 			continue;
210911131SErik.Nordmark@Sun.COM 
211011042SErik.Nordmark@Sun.COM 		if (ire->ire_zoneid != ALL_ZONES &&
211111042SErik.Nordmark@Sun.COM 		    ire->ire_zoneid != margs->ift_zoneid)
211211042SErik.Nordmark@Sun.COM 			continue;
211311042SErik.Nordmark@Sun.COM 
211411042SErik.Nordmark@Sun.COM 		if (margs->ift_ill != NULL && margs->ift_ill != ire->ire_ill)
211511042SErik.Nordmark@Sun.COM 			continue;
211611042SErik.Nordmark@Sun.COM 
211711042SErik.Nordmark@Sun.COM 		if (is_system_labeled() &&
211811042SErik.Nordmark@Sun.COM 		    tsol_ire_match_gwattr(ire, margs->ift_tsl) != 0)
211911042SErik.Nordmark@Sun.COM 			continue;
212011042SErik.Nordmark@Sun.COM 
212111042SErik.Nordmark@Sun.COM 		rw_exit(&irb->irb_lock);
212211042SErik.Nordmark@Sun.COM 		return (B_TRUE);
21231676Sjpk 	}
212411042SErik.Nordmark@Sun.COM 	rw_exit(&irb->irb_lock);
212511042SErik.Nordmark@Sun.COM 	return (B_FALSE);
21262733Snordmark }
21272733Snordmark 
21282733Snordmark /*
212911042SErik.Nordmark@Sun.COM  * Check if the zoneid (not ALL_ZONES) has an IRE_INTERFACE for the specified
213011042SErik.Nordmark@Sun.COM  * gateway address. If ill is non-NULL we also match on it.
213111042SErik.Nordmark@Sun.COM  * The caller must hold a read lock on RADIX_NODE_HEAD if lock_held is set.
21320Sstevel@tonic-gate  */
213311042SErik.Nordmark@Sun.COM boolean_t
ire_gateway_ok_zone_v4(ipaddr_t gateway,zoneid_t zoneid,ill_t * ill,const ts_label_t * tsl,ip_stack_t * ipst,boolean_t lock_held)213411042SErik.Nordmark@Sun.COM ire_gateway_ok_zone_v4(ipaddr_t gateway, zoneid_t zoneid, ill_t *ill,
213511042SErik.Nordmark@Sun.COM     const ts_label_t *tsl, ip_stack_t *ipst, boolean_t lock_held)
21360Sstevel@tonic-gate {
213711042SErik.Nordmark@Sun.COM 	struct rt_sockaddr rdst;
213811042SErik.Nordmark@Sun.COM 	struct rt_entry *rt;
213911042SErik.Nordmark@Sun.COM 	ire_ftable_args_t margs;
214011042SErik.Nordmark@Sun.COM 
214111042SErik.Nordmark@Sun.COM 	ASSERT(ill == NULL || !ill->ill_isv6);
214211042SErik.Nordmark@Sun.COM 	if (lock_held)
214311042SErik.Nordmark@Sun.COM 		ASSERT(RW_READ_HELD(&ipst->ips_ip_ftable->rnh_lock));
214411042SErik.Nordmark@Sun.COM 	else
214511042SErik.Nordmark@Sun.COM 		RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
214611042SErik.Nordmark@Sun.COM 
214711131SErik.Nordmark@Sun.COM 	bzero(&rdst, sizeof (rdst));
214811042SErik.Nordmark@Sun.COM 	rdst.rt_sin_len = sizeof (rdst);
214911042SErik.Nordmark@Sun.COM 	rdst.rt_sin_family = AF_INET;
215011042SErik.Nordmark@Sun.COM 	rdst.rt_sin_addr.s_addr = gateway;
21518275SEric Cheng 
21528275SEric Cheng 	/*
215311042SErik.Nordmark@Sun.COM 	 * We only use margs for ill, zoneid, and tsl matching in
215411042SErik.Nordmark@Sun.COM 	 * ire_find_zoneid
21558275SEric Cheng 	 */
215611131SErik.Nordmark@Sun.COM 	bzero(&margs, sizeof (margs));
215711042SErik.Nordmark@Sun.COM 	margs.ift_ill = ill;
215811042SErik.Nordmark@Sun.COM 	margs.ift_zoneid = zoneid;
215911042SErik.Nordmark@Sun.COM 	margs.ift_tsl = tsl;
216011042SErik.Nordmark@Sun.COM 	rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
216111042SErik.Nordmark@Sun.COM 	    ipst->ips_ip_ftable, ire_find_zoneid, (void *)&margs);
216211042SErik.Nordmark@Sun.COM 
216311042SErik.Nordmark@Sun.COM 	if (!lock_held)
216411042SErik.Nordmark@Sun.COM 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
216511042SErik.Nordmark@Sun.COM 
216611042SErik.Nordmark@Sun.COM 	return (rt != NULL);
21678275SEric Cheng }
21688275SEric Cheng 
21690Sstevel@tonic-gate /*
217011042SErik.Nordmark@Sun.COM  * ire_walk routine to delete a fraction of redirect IREs and IRE_CLONE_IF IREs.
217111042SErik.Nordmark@Sun.COM  * The fraction argument tells us what fraction of the IREs to delete.
217211042SErik.Nordmark@Sun.COM  * Common for IPv4 and IPv6.
217311042SErik.Nordmark@Sun.COM  * Used when memory backpressure.
21740Sstevel@tonic-gate  */
217511042SErik.Nordmark@Sun.COM static void
ire_delete_reclaim(ire_t * ire,char * arg)217611042SErik.Nordmark@Sun.COM ire_delete_reclaim(ire_t *ire, char *arg)
21770Sstevel@tonic-gate {
217811042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
217911042SErik.Nordmark@Sun.COM 	uint_t		fraction = *(uint_t *)arg;
218011042SErik.Nordmark@Sun.COM 	uint_t		rand;
218111042SErik.Nordmark@Sun.COM 
218211042SErik.Nordmark@Sun.COM 	if ((ire->ire_flags & RTF_DYNAMIC) ||
218311042SErik.Nordmark@Sun.COM 	    (ire->ire_type & IRE_IF_CLONE)) {
218411042SErik.Nordmark@Sun.COM 
218511042SErik.Nordmark@Sun.COM 		/* Pick a random number */
218611066Srafael.vanoni@sun.com 		rand = (uint_t)ddi_get_lbolt() +
218711042SErik.Nordmark@Sun.COM 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6, 256);
218811042SErik.Nordmark@Sun.COM 
218911042SErik.Nordmark@Sun.COM 		/* Use truncation */
219011042SErik.Nordmark@Sun.COM 		if ((rand/fraction)*fraction == rand) {
219111042SErik.Nordmark@Sun.COM 			IP_STAT(ipst, ip_ire_reclaim_deleted);
219211042SErik.Nordmark@Sun.COM 			ire_delete(ire);
219311042SErik.Nordmark@Sun.COM 		}
219411042SErik.Nordmark@Sun.COM 	}
219511042SErik.Nordmark@Sun.COM 
21960Sstevel@tonic-gate }
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate /*
219911042SErik.Nordmark@Sun.COM  * kmem_cache callback to free up memory.
22000Sstevel@tonic-gate  *
220111042SErik.Nordmark@Sun.COM  * Free a fraction (ips_ip_ire_reclaim_fraction) of things IP added dynamically
220211042SErik.Nordmark@Sun.COM  * (RTF_DYNAMIC and IRE_IF_CLONE).
22030Sstevel@tonic-gate  */
220411042SErik.Nordmark@Sun.COM static void
ip_ire_reclaim_stack(ip_stack_t * ipst)220511042SErik.Nordmark@Sun.COM ip_ire_reclaim_stack(ip_stack_t *ipst)
22060Sstevel@tonic-gate {
220711042SErik.Nordmark@Sun.COM 	uint_t	fraction = ipst->ips_ip_ire_reclaim_fraction;
220811042SErik.Nordmark@Sun.COM 
220911042SErik.Nordmark@Sun.COM 	IP_STAT(ipst, ip_ire_reclaim_calls);
221011042SErik.Nordmark@Sun.COM 
221111042SErik.Nordmark@Sun.COM 	ire_walk(ire_delete_reclaim, &fraction, ipst);
22128485SPeter.Memishian@Sun.COM 
22138485SPeter.Memishian@Sun.COM 	/*
221411042SErik.Nordmark@Sun.COM 	 * Walk all CONNs that can have a reference on an ire, nce or dce.
221511042SErik.Nordmark@Sun.COM 	 * Get them to update any stale references to drop any refholds they
221611042SErik.Nordmark@Sun.COM 	 * have.
22178485SPeter.Memishian@Sun.COM 	 */
221811042SErik.Nordmark@Sun.COM 	ipcl_walk(conn_ixa_cleanup, (void *)B_FALSE, ipst);
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate /*
222211042SErik.Nordmark@Sun.COM  * Called by the memory allocator subsystem directly, when the system
222311042SErik.Nordmark@Sun.COM  * is running low on memory.
22240Sstevel@tonic-gate  */
222511042SErik.Nordmark@Sun.COM /* ARGSUSED */
22260Sstevel@tonic-gate void
ip_ire_reclaim(void * args)222711042SErik.Nordmark@Sun.COM ip_ire_reclaim(void *args)
22280Sstevel@tonic-gate {
222911042SErik.Nordmark@Sun.COM 	netstack_handle_t nh;
223011042SErik.Nordmark@Sun.COM 	netstack_t *ns;
223111769SKacheong.Poon@Sun.COM 	ip_stack_t *ipst;
223211042SErik.Nordmark@Sun.COM 
223311042SErik.Nordmark@Sun.COM 	netstack_next_init(&nh);
223411042SErik.Nordmark@Sun.COM 	while ((ns = netstack_next(&nh)) != NULL) {
223511769SKacheong.Poon@Sun.COM 		/*
223611769SKacheong.Poon@Sun.COM 		 * netstack_next() can return a netstack_t with a NULL
223711769SKacheong.Poon@Sun.COM 		 * netstack_ip at boot time.
223811769SKacheong.Poon@Sun.COM 		 */
223911769SKacheong.Poon@Sun.COM 		if ((ipst = ns->netstack_ip) == NULL) {
224011769SKacheong.Poon@Sun.COM 			netstack_rele(ns);
224111769SKacheong.Poon@Sun.COM 			continue;
224211769SKacheong.Poon@Sun.COM 		}
224311769SKacheong.Poon@Sun.COM 		ip_ire_reclaim_stack(ipst);
224411042SErik.Nordmark@Sun.COM 		netstack_rele(ns);
22450Sstevel@tonic-gate 	}
224611042SErik.Nordmark@Sun.COM 	netstack_next_fini(&nh);
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate static void
power2_roundup(uint32_t * value)22500Sstevel@tonic-gate power2_roundup(uint32_t *value)
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate 	int i;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	for (i = 1; i < 31; i++) {
22550Sstevel@tonic-gate 		if (*value <= (1 << i))
22560Sstevel@tonic-gate 			break;
22570Sstevel@tonic-gate 	}
22580Sstevel@tonic-gate 	*value = (1 << i);
22590Sstevel@tonic-gate }
22600Sstevel@tonic-gate 
22613448Sdh155122 /* Global init for all zones */
22620Sstevel@tonic-gate void
ip_ire_g_init()22633448Sdh155122 ip_ire_g_init()
22640Sstevel@tonic-gate {
22650Sstevel@tonic-gate 	/*
226611042SErik.Nordmark@Sun.COM 	 * Create kmem_caches.  ip_ire_reclaim() and ip_nce_reclaim()
226711042SErik.Nordmark@Sun.COM 	 * will give disposable IREs back to system when needed.
22680Sstevel@tonic-gate 	 * This needs to be done here before anything else, since
22690Sstevel@tonic-gate 	 * ire_add() expects the cache to be created.
22700Sstevel@tonic-gate 	 */
22710Sstevel@tonic-gate 	ire_cache = kmem_cache_create("ire_cache",
227211042SErik.Nordmark@Sun.COM 	    sizeof (ire_t), 0, NULL, NULL,
227311042SErik.Nordmark@Sun.COM 	    ip_ire_reclaim, NULL, NULL, 0);
227411042SErik.Nordmark@Sun.COM 
227511042SErik.Nordmark@Sun.COM 	ncec_cache = kmem_cache_create("ncec_cache",
227611042SErik.Nordmark@Sun.COM 	    sizeof (ncec_t), 0, NULL, NULL,
227711042SErik.Nordmark@Sun.COM 	    ip_nce_reclaim, NULL, NULL, 0);
227811042SErik.Nordmark@Sun.COM 	nce_cache = kmem_cache_create("nce_cache",
227911042SErik.Nordmark@Sun.COM 	    sizeof (nce_t), 0, NULL, NULL,
228011042SErik.Nordmark@Sun.COM 	    NULL, NULL, NULL, 0);
22810Sstevel@tonic-gate 
22823448Sdh155122 	rt_entry_cache = kmem_cache_create("rt_entry",
22833448Sdh155122 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
22843448Sdh155122 
22853448Sdh155122 	/*
22863448Sdh155122 	 * Have radix code setup kmem caches etc.
22873448Sdh155122 	 */
22883448Sdh155122 	rn_init();
22893448Sdh155122 }
22903448Sdh155122 
22913448Sdh155122 void
ip_ire_init(ip_stack_t * ipst)22923448Sdh155122 ip_ire_init(ip_stack_t *ipst)
22933448Sdh155122 {
229411042SErik.Nordmark@Sun.COM 	ire_t	*ire;
229511042SErik.Nordmark@Sun.COM 	int	error;
22963448Sdh155122 
22973448Sdh155122 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
22983448Sdh155122 
22993448Sdh155122 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
23003448Sdh155122 
23010Sstevel@tonic-gate 	/*
23020Sstevel@tonic-gate 	 * Make sure that the forwarding table size is a power of 2.
23030Sstevel@tonic-gate 	 * The IRE*_ADDR_HASH() macroes depend on that.
23040Sstevel@tonic-gate 	 */
23053448Sdh155122 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
23063448Sdh155122 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
23073448Sdh155122 
230811042SErik.Nordmark@Sun.COM 	/*
230911042SErik.Nordmark@Sun.COM 	 * Allocate/initialize a pair of IRE_NOROUTEs for each of IPv4 and IPv6.
231011042SErik.Nordmark@Sun.COM 	 * The ire_reject_v* has RTF_REJECT set, and the ire_blackhole_v* has
231111042SErik.Nordmark@Sun.COM 	 * RTF_BLACKHOLE set. We use the latter for transient errors such
231211042SErik.Nordmark@Sun.COM 	 * as memory allocation failures and tripping on IRE_IS_CONDEMNED
231311042SErik.Nordmark@Sun.COM 	 * entries.
231411042SErik.Nordmark@Sun.COM 	 */
231511042SErik.Nordmark@Sun.COM 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
231611042SErik.Nordmark@Sun.COM 	*ire = ire_null;
231711042SErik.Nordmark@Sun.COM 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
231811042SErik.Nordmark@Sun.COM 	    RTF_REJECT|RTF_UP, NULL, ipst);
231911042SErik.Nordmark@Sun.COM 	ASSERT(error == 0);
232011042SErik.Nordmark@Sun.COM 	ipst->ips_ire_reject_v4 = ire;
232111042SErik.Nordmark@Sun.COM 
232211042SErik.Nordmark@Sun.COM 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
232311042SErik.Nordmark@Sun.COM 	*ire = ire_null;
232411042SErik.Nordmark@Sun.COM 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
232511042SErik.Nordmark@Sun.COM 	    RTF_REJECT|RTF_UP, NULL, ipst);
232611042SErik.Nordmark@Sun.COM 	ASSERT(error == 0);
232711042SErik.Nordmark@Sun.COM 	ipst->ips_ire_reject_v6 = ire;
232811042SErik.Nordmark@Sun.COM 
232911042SErik.Nordmark@Sun.COM 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
233011042SErik.Nordmark@Sun.COM 	*ire = ire_null;
233111042SErik.Nordmark@Sun.COM 	error = ire_init_v4(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
233211042SErik.Nordmark@Sun.COM 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
233311042SErik.Nordmark@Sun.COM 	ASSERT(error == 0);
233411042SErik.Nordmark@Sun.COM 	ipst->ips_ire_blackhole_v4 = ire;
233511042SErik.Nordmark@Sun.COM 
233611042SErik.Nordmark@Sun.COM 	ire = kmem_cache_alloc(ire_cache, KM_SLEEP);
233711042SErik.Nordmark@Sun.COM 	*ire = ire_null;
233811042SErik.Nordmark@Sun.COM 	error = ire_init_v6(ire, 0, 0, 0, IRE_NOROUTE, NULL, ALL_ZONES,
233911042SErik.Nordmark@Sun.COM 	    RTF_BLACKHOLE|RTF_UP, NULL, ipst);
234011042SErik.Nordmark@Sun.COM 	ASSERT(error == 0);
234111042SErik.Nordmark@Sun.COM 	ipst->ips_ire_blackhole_v6 = ire;
234211042SErik.Nordmark@Sun.COM 
234311042SErik.Nordmark@Sun.COM 	rw_init(&ipst->ips_ip6_ire_head_lock, NULL, RW_DEFAULT, NULL);
234411042SErik.Nordmark@Sun.COM 	rw_init(&ipst->ips_ire_dep_lock, NULL, RW_DEFAULT, NULL);
23453448Sdh155122 }
23463448Sdh155122 
23473448Sdh155122 void
ip_ire_g_fini(void)23483448Sdh155122 ip_ire_g_fini(void)
23493448Sdh155122 {
23503448Sdh155122 	kmem_cache_destroy(ire_cache);
235111042SErik.Nordmark@Sun.COM 	kmem_cache_destroy(ncec_cache);
235211042SErik.Nordmark@Sun.COM 	kmem_cache_destroy(nce_cache);
23533448Sdh155122 	kmem_cache_destroy(rt_entry_cache);
23543448Sdh155122 
23553448Sdh155122 	rn_fini();
23560Sstevel@tonic-gate }
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate void
ip_ire_fini(ip_stack_t * ipst)23593448Sdh155122 ip_ire_fini(ip_stack_t *ipst)
23600Sstevel@tonic-gate {
23610Sstevel@tonic-gate 	int i;
23620Sstevel@tonic-gate 
236311553SThirumalai.Srinivasan@Sun.COM 	ire_make_condemned(ipst->ips_ire_reject_v6);
236411042SErik.Nordmark@Sun.COM 	ire_refrele_notr(ipst->ips_ire_reject_v6);
236511042SErik.Nordmark@Sun.COM 	ipst->ips_ire_reject_v6 = NULL;
236611553SThirumalai.Srinivasan@Sun.COM 
236711553SThirumalai.Srinivasan@Sun.COM 	ire_make_condemned(ipst->ips_ire_reject_v4);
236811042SErik.Nordmark@Sun.COM 	ire_refrele_notr(ipst->ips_ire_reject_v4);
236911042SErik.Nordmark@Sun.COM 	ipst->ips_ire_reject_v4 = NULL;
237011553SThirumalai.Srinivasan@Sun.COM 
237111553SThirumalai.Srinivasan@Sun.COM 	ire_make_condemned(ipst->ips_ire_blackhole_v6);
237211042SErik.Nordmark@Sun.COM 	ire_refrele_notr(ipst->ips_ire_blackhole_v6);
237311042SErik.Nordmark@Sun.COM 	ipst->ips_ire_blackhole_v6 = NULL;
237411553SThirumalai.Srinivasan@Sun.COM 
237511553SThirumalai.Srinivasan@Sun.COM 	ire_make_condemned(ipst->ips_ire_blackhole_v4);
237611042SErik.Nordmark@Sun.COM 	ire_refrele_notr(ipst->ips_ire_blackhole_v4);
237711042SErik.Nordmark@Sun.COM 	ipst->ips_ire_blackhole_v4 = NULL;
237811042SErik.Nordmark@Sun.COM 
23793448Sdh155122 	/*
23803448Sdh155122 	 * Delete all IREs - assumes that the ill/ipifs have
238111042SErik.Nordmark@Sun.COM 	 * been removed so what remains are just the ftable to handle.
23823448Sdh155122 	 */
23833448Sdh155122 	ire_walk(ire_delete, NULL, ipst);
23843448Sdh155122 
23853448Sdh155122 	rn_freehead(ipst->ips_ip_ftable);
23863448Sdh155122 	ipst->ips_ip_ftable = NULL;
23873448Sdh155122 
238812479SErik.Nordmark@Sun.COM 	rw_destroy(&ipst->ips_ire_dep_lock);
238912479SErik.Nordmark@Sun.COM 	rw_destroy(&ipst->ips_ip6_ire_head_lock);
239012479SErik.Nordmark@Sun.COM 
23913448Sdh155122 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
23923448Sdh155122 
23933448Sdh155122 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
23943448Sdh155122 		irb_t *ptr;
23953448Sdh155122 		int j;
23963448Sdh155122 
23973448Sdh155122 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
23983448Sdh155122 			continue;
23993448Sdh155122 
24003448Sdh155122 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
24013448Sdh155122 			ASSERT(ptr[j].irb_ire == NULL);
24023448Sdh155122 			rw_destroy(&ptr[j].irb_lock);
24033448Sdh155122 		}
24043448Sdh155122 		mi_free(ptr);
24053448Sdh155122 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
24063448Sdh155122 	}
24070Sstevel@tonic-gate }
24080Sstevel@tonic-gate 
24095023Scarlsonj #ifdef DEBUG
24100Sstevel@tonic-gate void
ire_trace_ref(ire_t * ire)24110Sstevel@tonic-gate ire_trace_ref(ire_t *ire)
24120Sstevel@tonic-gate {
24130Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
24145023Scarlsonj 	if (ire->ire_trace_disable) {
24150Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
24160Sstevel@tonic-gate 		return;
24170Sstevel@tonic-gate 	}
24185023Scarlsonj 
24195023Scarlsonj 	if (th_trace_ref(ire, ire->ire_ipst)) {
24205023Scarlsonj 		mutex_exit(&ire->ire_lock);
24215023Scarlsonj 	} else {
24225023Scarlsonj 		ire->ire_trace_disable = B_TRUE;
24235023Scarlsonj 		mutex_exit(&ire->ire_lock);
24245023Scarlsonj 		ire_trace_cleanup(ire);
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate }
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate void
ire_untrace_ref(ire_t * ire)24290Sstevel@tonic-gate ire_untrace_ref(ire_t *ire)
24300Sstevel@tonic-gate {
24310Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
24325023Scarlsonj 	if (!ire->ire_trace_disable)
24335023Scarlsonj 		th_trace_unref(ire);
24340Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate static void
ire_trace_cleanup(const ire_t * ire)24385023Scarlsonj ire_trace_cleanup(const ire_t *ire)
24390Sstevel@tonic-gate {
24405023Scarlsonj 	th_trace_cleanup(ire, ire->ire_trace_disable);
24410Sstevel@tonic-gate }
24425023Scarlsonj #endif /* DEBUG */
24432535Ssangeeta 
24442535Ssangeeta /*
244511042SErik.Nordmark@Sun.COM  * Find, or create if needed, the nce_t pointer to the neighbor cache
244611042SErik.Nordmark@Sun.COM  * entry ncec_t for an IPv4 address. The nce_t will be created on the ill_t
244711042SErik.Nordmark@Sun.COM  * in the non-IPMP case, or on the cast-ill in the IPMP bcast/mcast case, or
244811042SErik.Nordmark@Sun.COM  * on the next available under-ill (selected by the IPMP rotor) in the
244911042SErik.Nordmark@Sun.COM  * unicast IPMP case.
245011042SErik.Nordmark@Sun.COM  *
245111042SErik.Nordmark@Sun.COM  * If a neighbor-cache entry has to be created (i.e., one does not already
245211042SErik.Nordmark@Sun.COM  * exist in the nce list) the ncec_lladdr and ncec_state of the neighbor cache
245311042SErik.Nordmark@Sun.COM  * entry are initialized in nce_add_v4(). The broadcast, multicast, and
245411042SErik.Nordmark@Sun.COM  * link-layer type determine the contents of {ncec_state, ncec_lladdr} of
245511042SErik.Nordmark@Sun.COM  * the ncec_t created. The ncec_lladdr is non-null for all link types with
245611042SErik.Nordmark@Sun.COM  * non-zero ill_phys_addr_length, though the contents may be zero in cases
245711042SErik.Nordmark@Sun.COM  * where the link-layer type is not known at the time of creation
245811042SErik.Nordmark@Sun.COM  * (e.g., IRE_IFRESOLVER links)
245911042SErik.Nordmark@Sun.COM  *
246011042SErik.Nordmark@Sun.COM  * All IRE_BROADCAST entries have ncec_state = ND_REACHABLE, and the nce_lladr
246111042SErik.Nordmark@Sun.COM  * has the physical broadcast address of the outgoing interface.
246211042SErik.Nordmark@Sun.COM  * For unicast ire entries,
246311042SErik.Nordmark@Sun.COM  *   - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
246411042SErik.Nordmark@Sun.COM  *     ncec_t with 0 nce_lladr contents, and will be in the ND_INITIAL state.
246511042SErik.Nordmark@Sun.COM  *   - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
246611042SErik.Nordmark@Sun.COM  *     layer resolution is necessary, so that the ncec_t will be in the
246711042SErik.Nordmark@Sun.COM  *     ND_REACHABLE state
246811042SErik.Nordmark@Sun.COM  *
246911042SErik.Nordmark@Sun.COM  * The link layer information needed for broadcast addresses, and for
247011042SErik.Nordmark@Sun.COM  * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
247111042SErik.Nordmark@Sun.COM  * never needs re-verification for the lifetime of the ncec_t. These are
247211042SErik.Nordmark@Sun.COM  * therefore marked NCE_F_NONUD.
247311042SErik.Nordmark@Sun.COM  *
247411042SErik.Nordmark@Sun.COM  * The nce returned will be created such that the nce_ill == ill that
247511042SErik.Nordmark@Sun.COM  * is passed in. Note that the nce itself may not have ncec_ill == ill
247611042SErik.Nordmark@Sun.COM  * where IPMP links are involved.
247711042SErik.Nordmark@Sun.COM  */
247811042SErik.Nordmark@Sun.COM static nce_t *
ire_nce_init(ill_t * ill,const void * addr,int ire_type)247911042SErik.Nordmark@Sun.COM ire_nce_init(ill_t *ill, const void *addr, int ire_type)
248011042SErik.Nordmark@Sun.COM {
248111042SErik.Nordmark@Sun.COM 	int		err;
248211042SErik.Nordmark@Sun.COM 	nce_t		*nce = NULL;
248311042SErik.Nordmark@Sun.COM 	uint16_t	ncec_flags;
248411042SErik.Nordmark@Sun.COM 	uchar_t		*hwaddr;
248511042SErik.Nordmark@Sun.COM 	boolean_t	need_refrele = B_FALSE;
248611042SErik.Nordmark@Sun.COM 	ill_t		*in_ill = ill;
248711042SErik.Nordmark@Sun.COM 	boolean_t	is_unicast;
248811042SErik.Nordmark@Sun.COM 	uint_t		hwaddr_len;
248911042SErik.Nordmark@Sun.COM 
249011042SErik.Nordmark@Sun.COM 	is_unicast = ((ire_type & (IRE_MULTICAST|IRE_BROADCAST)) == 0);
249111042SErik.Nordmark@Sun.COM 	if (IS_IPMP(ill) ||
249211042SErik.Nordmark@Sun.COM 	    ((ire_type & IRE_BROADCAST) && IS_UNDER_IPMP(ill))) {
249312069SPeter.Memishian@Sun.COM 		if ((ill = ipmp_ill_hold_xmit_ill(ill, is_unicast)) == NULL)
249411042SErik.Nordmark@Sun.COM 			return (NULL);
249511042SErik.Nordmark@Sun.COM 		need_refrele = B_TRUE;
249611042SErik.Nordmark@Sun.COM 	}
249711042SErik.Nordmark@Sun.COM 	ncec_flags = (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0;
249811042SErik.Nordmark@Sun.COM 
249911042SErik.Nordmark@Sun.COM 	switch (ire_type) {
250011042SErik.Nordmark@Sun.COM 	case IRE_BROADCAST:
250111042SErik.Nordmark@Sun.COM 		ASSERT(!ill->ill_isv6);
250211042SErik.Nordmark@Sun.COM 		ncec_flags |= (NCE_F_BCAST|NCE_F_NONUD);
250311042SErik.Nordmark@Sun.COM 		break;
250411042SErik.Nordmark@Sun.COM 	case IRE_MULTICAST:
250511042SErik.Nordmark@Sun.COM 		ncec_flags |= (NCE_F_MCAST|NCE_F_NONUD);
250611042SErik.Nordmark@Sun.COM 		break;
250711042SErik.Nordmark@Sun.COM 	}
250811042SErik.Nordmark@Sun.COM 
250911042SErik.Nordmark@Sun.COM 	if (ill->ill_net_type == IRE_IF_NORESOLVER && is_unicast) {
251011042SErik.Nordmark@Sun.COM 		hwaddr = ill->ill_dest_addr;
251111042SErik.Nordmark@Sun.COM 	} else {
251211042SErik.Nordmark@Sun.COM 		hwaddr = NULL;
251311042SErik.Nordmark@Sun.COM 	}
251411042SErik.Nordmark@Sun.COM 	hwaddr_len = ill->ill_phys_addr_length;
251511042SErik.Nordmark@Sun.COM 
251611042SErik.Nordmark@Sun.COM retry:
251711042SErik.Nordmark@Sun.COM 	/* nce_state will be computed by nce_add_common() */
251811042SErik.Nordmark@Sun.COM 	if (!ill->ill_isv6) {
251911042SErik.Nordmark@Sun.COM 		err = nce_lookup_then_add_v4(ill, hwaddr, hwaddr_len, addr,
252011042SErik.Nordmark@Sun.COM 		    ncec_flags, ND_UNCHANGED, &nce);
252111042SErik.Nordmark@Sun.COM 	} else {
252211042SErik.Nordmark@Sun.COM 		err = nce_lookup_then_add_v6(ill, hwaddr, hwaddr_len, addr,
252311042SErik.Nordmark@Sun.COM 		    ncec_flags, ND_UNCHANGED, &nce);
252411042SErik.Nordmark@Sun.COM 	}
252511042SErik.Nordmark@Sun.COM 
252611042SErik.Nordmark@Sun.COM 	switch (err) {
252711042SErik.Nordmark@Sun.COM 	case 0:
252811042SErik.Nordmark@Sun.COM 		break;
252911042SErik.Nordmark@Sun.COM 	case EEXIST:
253011042SErik.Nordmark@Sun.COM 		/*
253111042SErik.Nordmark@Sun.COM 		 * When subnets change or partially overlap what was once
253211042SErik.Nordmark@Sun.COM 		 * a broadcast address could now be a unicast, or vice versa.
253311042SErik.Nordmark@Sun.COM 		 */
253411042SErik.Nordmark@Sun.COM 		if (((ncec_flags ^ nce->nce_common->ncec_flags) &
253511042SErik.Nordmark@Sun.COM 		    NCE_F_BCAST) != 0) {
253611042SErik.Nordmark@Sun.COM 			ASSERT(!ill->ill_isv6);
253711042SErik.Nordmark@Sun.COM 			ncec_delete(nce->nce_common);
253811042SErik.Nordmark@Sun.COM 			nce_refrele(nce);
253911042SErik.Nordmark@Sun.COM 			goto retry;
254011042SErik.Nordmark@Sun.COM 		}
254111042SErik.Nordmark@Sun.COM 		break;
254211042SErik.Nordmark@Sun.COM 	default:
254311042SErik.Nordmark@Sun.COM 		DTRACE_PROBE2(nce__init__fail, ill_t *, ill, int, err);
254411042SErik.Nordmark@Sun.COM 		if (need_refrele)
254511042SErik.Nordmark@Sun.COM 			ill_refrele(ill);
254611042SErik.Nordmark@Sun.COM 		return (NULL);
254711042SErik.Nordmark@Sun.COM 	}
254811042SErik.Nordmark@Sun.COM 	/*
254911042SErik.Nordmark@Sun.COM 	 * If the ill was an under-ill of an IPMP group, we need to verify
255011042SErik.Nordmark@Sun.COM 	 * that it is still active so that we select an active interface in
255111042SErik.Nordmark@Sun.COM 	 * the group. However, since ipmp_ill_is_active ASSERTs for
255211042SErik.Nordmark@Sun.COM 	 * IS_UNDER_IPMP(), we first need to verify that the ill is an
255311042SErik.Nordmark@Sun.COM 	 * under-ill, and since this is being done in the data path, the
255411042SErik.Nordmark@Sun.COM 	 * only way to ascertain this is by holding the ill_g_lock.
255511042SErik.Nordmark@Sun.COM 	 */
255611042SErik.Nordmark@Sun.COM 	rw_enter(&ill->ill_ipst->ips_ill_g_lock, RW_READER);
255711042SErik.Nordmark@Sun.COM 	mutex_enter(&ill->ill_lock);
255811042SErik.Nordmark@Sun.COM 	mutex_enter(&ill->ill_phyint->phyint_lock);
255911042SErik.Nordmark@Sun.COM 	if (need_refrele && IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
256011042SErik.Nordmark@Sun.COM 		/*
256111042SErik.Nordmark@Sun.COM 		 * need_refrele implies that the under ill was selected by
256212069SPeter.Memishian@Sun.COM 		 * ipmp_ill_hold_xmit_ill() because either the in_ill was an
256312069SPeter.Memishian@Sun.COM 		 * ipmp_ill, or we are sending a non-unicast packet on an
256412069SPeter.Memishian@Sun.COM 		 * under_ill. However, when we get here, the ill selected by
256512069SPeter.Memishian@Sun.COM 		 * ipmp_ill_hold_xmit_ill was pulled out of the active set
256612069SPeter.Memishian@Sun.COM 		 * (for unicast) or cast_ill nomination (for !unicast) after
256712069SPeter.Memishian@Sun.COM 		 * it was picked as the outgoing ill.  We have to pick an
256812069SPeter.Memishian@Sun.COM 		 * active interface and/or cast_ill in the group.
256911042SErik.Nordmark@Sun.COM 		 */
257011042SErik.Nordmark@Sun.COM 		mutex_exit(&ill->ill_phyint->phyint_lock);
257111042SErik.Nordmark@Sun.COM 		nce_delete(nce);
257211042SErik.Nordmark@Sun.COM 		mutex_exit(&ill->ill_lock);
257311042SErik.Nordmark@Sun.COM 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
257411042SErik.Nordmark@Sun.COM 		nce_refrele(nce);
257511042SErik.Nordmark@Sun.COM 		ill_refrele(ill);
257612069SPeter.Memishian@Sun.COM 		if ((ill = ipmp_ill_hold_xmit_ill(in_ill, is_unicast)) == NULL)
257711042SErik.Nordmark@Sun.COM 			return (NULL);
257811042SErik.Nordmark@Sun.COM 		goto retry;
257911042SErik.Nordmark@Sun.COM 	} else {
258011042SErik.Nordmark@Sun.COM 		mutex_exit(&ill->ill_phyint->phyint_lock);
258111042SErik.Nordmark@Sun.COM 		mutex_exit(&ill->ill_lock);
258211042SErik.Nordmark@Sun.COM 		rw_exit(&ill->ill_ipst->ips_ill_g_lock);
258311042SErik.Nordmark@Sun.COM 	}
258411042SErik.Nordmark@Sun.COM done:
258511042SErik.Nordmark@Sun.COM 	ASSERT(nce->nce_ill == ill);
258611042SErik.Nordmark@Sun.COM 	if (need_refrele)
258711042SErik.Nordmark@Sun.COM 		ill_refrele(ill);
258811042SErik.Nordmark@Sun.COM 	return (nce);
258911042SErik.Nordmark@Sun.COM }
259011042SErik.Nordmark@Sun.COM 
259111042SErik.Nordmark@Sun.COM nce_t *
arp_nce_init(ill_t * ill,in_addr_t addr4,int ire_type)259211042SErik.Nordmark@Sun.COM arp_nce_init(ill_t *ill, in_addr_t addr4, int ire_type)
259311042SErik.Nordmark@Sun.COM {
259411042SErik.Nordmark@Sun.COM 	return (ire_nce_init(ill, &addr4, ire_type));
259511042SErik.Nordmark@Sun.COM }
259611042SErik.Nordmark@Sun.COM 
259711042SErik.Nordmark@Sun.COM nce_t *
ndp_nce_init(ill_t * ill,const in6_addr_t * addr6,int ire_type)259811042SErik.Nordmark@Sun.COM ndp_nce_init(ill_t *ill, const in6_addr_t *addr6, int ire_type)
259911042SErik.Nordmark@Sun.COM {
260011042SErik.Nordmark@Sun.COM 	ASSERT((ire_type & IRE_BROADCAST) == 0);
260111042SErik.Nordmark@Sun.COM 	return (ire_nce_init(ill, addr6, ire_type));
260211042SErik.Nordmark@Sun.COM }
260311042SErik.Nordmark@Sun.COM 
260411042SErik.Nordmark@Sun.COM /*
260511042SErik.Nordmark@Sun.COM  * The caller should hold irb_lock as a writer if the ire is in a bucket.
260611463SSowmini.Varadhan@Sun.COM  * This routine will clear ire_nce_cache, and we make sure that we can never
260711463SSowmini.Varadhan@Sun.COM  * set ire_nce_cache after the ire is marked condemned.
260811042SErik.Nordmark@Sun.COM  */
260911042SErik.Nordmark@Sun.COM void
ire_make_condemned(ire_t * ire)261011042SErik.Nordmark@Sun.COM ire_make_condemned(ire_t *ire)
261111042SErik.Nordmark@Sun.COM {
261211042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
261311463SSowmini.Varadhan@Sun.COM 	nce_t		*nce;
261411042SErik.Nordmark@Sun.COM 
261511042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
261611042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_bucket == NULL ||
261711042SErik.Nordmark@Sun.COM 	    RW_WRITE_HELD(&ire->ire_bucket->irb_lock));
261811042SErik.Nordmark@Sun.COM 	ASSERT(!IRE_IS_CONDEMNED(ire));
261911042SErik.Nordmark@Sun.COM 	ire->ire_generation = IRE_GENERATION_CONDEMNED;
262011042SErik.Nordmark@Sun.COM 	/* Count how many condemned ires for kmem_cache callback */
262111042SErik.Nordmark@Sun.COM 	atomic_add_32(&ipst->ips_num_ire_condemned, 1);
262211463SSowmini.Varadhan@Sun.COM 	nce = ire->ire_nce_cache;
262311463SSowmini.Varadhan@Sun.COM 	ire->ire_nce_cache = NULL;
262411042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
262511463SSowmini.Varadhan@Sun.COM 	if (nce != NULL)
262611463SSowmini.Varadhan@Sun.COM 		nce_refrele(nce);
262711042SErik.Nordmark@Sun.COM }
262811042SErik.Nordmark@Sun.COM 
262911042SErik.Nordmark@Sun.COM /*
263011042SErik.Nordmark@Sun.COM  * Increment the generation avoiding the special condemned value
263111042SErik.Nordmark@Sun.COM  */
263211042SErik.Nordmark@Sun.COM void
ire_increment_generation(ire_t * ire)263311042SErik.Nordmark@Sun.COM ire_increment_generation(ire_t *ire)
263411042SErik.Nordmark@Sun.COM {
263511042SErik.Nordmark@Sun.COM 	uint_t generation;
263611042SErik.Nordmark@Sun.COM 
263711042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
263811042SErik.Nordmark@Sun.COM 	/*
263911042SErik.Nordmark@Sun.COM 	 * Even though the caller has a hold it can't prevent a concurrent
264011042SErik.Nordmark@Sun.COM 	 * ire_delete marking the IRE condemned
264111042SErik.Nordmark@Sun.COM 	 */
264211042SErik.Nordmark@Sun.COM 	if (!IRE_IS_CONDEMNED(ire)) {
264311042SErik.Nordmark@Sun.COM 		generation = ire->ire_generation + 1;
264411042SErik.Nordmark@Sun.COM 		if (generation == IRE_GENERATION_CONDEMNED)
264511042SErik.Nordmark@Sun.COM 			generation = IRE_GENERATION_INITIAL;
264611042SErik.Nordmark@Sun.COM 		ASSERT(generation != IRE_GENERATION_VERIFY);
264711042SErik.Nordmark@Sun.COM 		ire->ire_generation = generation;
264811042SErik.Nordmark@Sun.COM 	}
264911042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
265011042SErik.Nordmark@Sun.COM }
265111042SErik.Nordmark@Sun.COM 
265211042SErik.Nordmark@Sun.COM /*
265311042SErik.Nordmark@Sun.COM  * Increment ire_generation on all the IRE_MULTICASTs
265411042SErik.Nordmark@Sun.COM  * Used when the default multicast interface (as determined by
265511042SErik.Nordmark@Sun.COM  * ill_lookup_multicast) might have changed.
265611042SErik.Nordmark@Sun.COM  *
265711042SErik.Nordmark@Sun.COM  * That includes the zoneid, IFF_ flags, the IPv6 scope of the address, and
265811042SErik.Nordmark@Sun.COM  * ill unplumb.
26592535Ssangeeta  */
26602535Ssangeeta void
ire_increment_multicast_generation(ip_stack_t * ipst,boolean_t isv6)266111042SErik.Nordmark@Sun.COM ire_increment_multicast_generation(ip_stack_t *ipst, boolean_t isv6)
266211042SErik.Nordmark@Sun.COM {
266311042SErik.Nordmark@Sun.COM 	ill_t	*ill;
266411042SErik.Nordmark@Sun.COM 	ill_walk_context_t ctx;
266511042SErik.Nordmark@Sun.COM 
266611042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
266711042SErik.Nordmark@Sun.COM 	if (isv6)
266811042SErik.Nordmark@Sun.COM 		ill = ILL_START_WALK_V6(&ctx, ipst);
266911042SErik.Nordmark@Sun.COM 	else
267011042SErik.Nordmark@Sun.COM 		ill = ILL_START_WALK_V4(&ctx, ipst);
267111042SErik.Nordmark@Sun.COM 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
267211042SErik.Nordmark@Sun.COM 		if (ILL_IS_CONDEMNED(ill))
267311042SErik.Nordmark@Sun.COM 			continue;
267411042SErik.Nordmark@Sun.COM 		if (ill->ill_ire_multicast != NULL)
267511042SErik.Nordmark@Sun.COM 			ire_increment_generation(ill->ill_ire_multicast);
267611042SErik.Nordmark@Sun.COM 	}
267711042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ill_g_lock);
267811042SErik.Nordmark@Sun.COM }
267911042SErik.Nordmark@Sun.COM 
268011042SErik.Nordmark@Sun.COM /*
268111042SErik.Nordmark@Sun.COM  * Return a held IRE_NOROUTE with RTF_REJECT set
268211042SErik.Nordmark@Sun.COM  */
268311042SErik.Nordmark@Sun.COM ire_t *
ire_reject(ip_stack_t * ipst,boolean_t isv6)268411042SErik.Nordmark@Sun.COM ire_reject(ip_stack_t *ipst, boolean_t isv6)
268511042SErik.Nordmark@Sun.COM {
268611042SErik.Nordmark@Sun.COM 	ire_t *ire;
268711042SErik.Nordmark@Sun.COM 
268811042SErik.Nordmark@Sun.COM 	if (isv6)
268911042SErik.Nordmark@Sun.COM 		ire = ipst->ips_ire_reject_v6;
269011042SErik.Nordmark@Sun.COM 	else
269111042SErik.Nordmark@Sun.COM 		ire = ipst->ips_ire_reject_v4;
269211042SErik.Nordmark@Sun.COM 
269311042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
269411042SErik.Nordmark@Sun.COM 	ire_refhold(ire);
269511042SErik.Nordmark@Sun.COM 	return (ire);
269611042SErik.Nordmark@Sun.COM }
269711042SErik.Nordmark@Sun.COM 
269811042SErik.Nordmark@Sun.COM /*
269911042SErik.Nordmark@Sun.COM  * Return a held IRE_NOROUTE with RTF_BLACKHOLE set
270011042SErik.Nordmark@Sun.COM  */
270111042SErik.Nordmark@Sun.COM ire_t *
ire_blackhole(ip_stack_t * ipst,boolean_t isv6)270211042SErik.Nordmark@Sun.COM ire_blackhole(ip_stack_t *ipst, boolean_t isv6)
270311042SErik.Nordmark@Sun.COM {
270411042SErik.Nordmark@Sun.COM 	ire_t *ire;
270511042SErik.Nordmark@Sun.COM 
270611042SErik.Nordmark@Sun.COM 	if (isv6)
270711042SErik.Nordmark@Sun.COM 		ire = ipst->ips_ire_blackhole_v6;
270811042SErik.Nordmark@Sun.COM 	else
270911042SErik.Nordmark@Sun.COM 		ire = ipst->ips_ire_blackhole_v4;
271011042SErik.Nordmark@Sun.COM 
271111042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_generation != IRE_GENERATION_CONDEMNED);
271211042SErik.Nordmark@Sun.COM 	ire_refhold(ire);
271311042SErik.Nordmark@Sun.COM 	return (ire);
271411042SErik.Nordmark@Sun.COM }
271511042SErik.Nordmark@Sun.COM 
271611042SErik.Nordmark@Sun.COM /*
271711042SErik.Nordmark@Sun.COM  * Return a held IRE_MULTICAST.
271811042SErik.Nordmark@Sun.COM  */
271911042SErik.Nordmark@Sun.COM ire_t *
ire_multicast(ill_t * ill)272011042SErik.Nordmark@Sun.COM ire_multicast(ill_t *ill)
272111042SErik.Nordmark@Sun.COM {
272211042SErik.Nordmark@Sun.COM 	ire_t *ire = ill->ill_ire_multicast;
272311042SErik.Nordmark@Sun.COM 
272411042SErik.Nordmark@Sun.COM 	ASSERT(ire == NULL || ire->ire_generation != IRE_GENERATION_CONDEMNED);
272511042SErik.Nordmark@Sun.COM 	if (ire == NULL)
272611042SErik.Nordmark@Sun.COM 		ire = ire_blackhole(ill->ill_ipst, ill->ill_isv6);
272711042SErik.Nordmark@Sun.COM 	else
272811042SErik.Nordmark@Sun.COM 		ire_refhold(ire);
272911042SErik.Nordmark@Sun.COM 	return (ire);
273011042SErik.Nordmark@Sun.COM }
273111042SErik.Nordmark@Sun.COM 
273211042SErik.Nordmark@Sun.COM /*
273311042SErik.Nordmark@Sun.COM  * Given an IRE return its nexthop IRE. The nexthop IRE is an IRE_ONLINK
273411042SErik.Nordmark@Sun.COM  * that is an exact match (i.e., a /32 for IPv4 and /128 for IPv6).
273511042SErik.Nordmark@Sun.COM  * This can return an RTF_REJECT|RTF_BLACKHOLE.
273611042SErik.Nordmark@Sun.COM  * The returned IRE is held.
273711042SErik.Nordmark@Sun.COM  * The assumption is that ip_select_route() has been called and returned the
273811042SErik.Nordmark@Sun.COM  * IRE (thus ip_select_route would have set up the ire_dep* information.)
273911042SErik.Nordmark@Sun.COM  * If some IRE is deleteted then ire_dep_remove() will have been called and
274011042SErik.Nordmark@Sun.COM  * we might not find a nexthop IRE, in which case we return NULL.
274111042SErik.Nordmark@Sun.COM  */
274211042SErik.Nordmark@Sun.COM ire_t *
ire_nexthop(ire_t * ire)274311042SErik.Nordmark@Sun.COM ire_nexthop(ire_t *ire)
27442535Ssangeeta {
274511042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
274611042SErik.Nordmark@Sun.COM 
274711042SErik.Nordmark@Sun.COM 	/* Acquire lock to walk ire_dep_parent */
274811042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
274911042SErik.Nordmark@Sun.COM 	while (ire != NULL) {
275011042SErik.Nordmark@Sun.COM 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
275111042SErik.Nordmark@Sun.COM 			goto done;
275211042SErik.Nordmark@Sun.COM 		}
275311042SErik.Nordmark@Sun.COM 		/*
275411042SErik.Nordmark@Sun.COM 		 * If we find an IRE_ONLINK we are done. This includes
275511042SErik.Nordmark@Sun.COM 		 * the case of IRE_MULTICAST.
275611042SErik.Nordmark@Sun.COM 		 * Note that in order to send packets we need a host-specific
275711042SErik.Nordmark@Sun.COM 		 * IRE_IF_ALL first in the ire_dep_parent chain. Normally this
275811042SErik.Nordmark@Sun.COM 		 * is done by inserting an IRE_IF_CLONE if the IRE_INTERFACE
275911042SErik.Nordmark@Sun.COM 		 * was not host specific.
276011042SErik.Nordmark@Sun.COM 		 * However, ip_rts_request doesn't want to send packets
276111042SErik.Nordmark@Sun.COM 		 * hence doesn't want to allocate an IRE_IF_CLONE. Yet
276211042SErik.Nordmark@Sun.COM 		 * it needs an IRE_IF_ALL to get to the ill. Thus
276311042SErik.Nordmark@Sun.COM 		 * we return IRE_IF_ALL that are not host specific here.
276411042SErik.Nordmark@Sun.COM 		 */
276511042SErik.Nordmark@Sun.COM 		if (ire->ire_type & IRE_ONLINK)
276611042SErik.Nordmark@Sun.COM 			goto done;
276711042SErik.Nordmark@Sun.COM 		ire = ire->ire_dep_parent;
276811042SErik.Nordmark@Sun.COM 	}
276911042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
277011042SErik.Nordmark@Sun.COM 	return (NULL);
277111042SErik.Nordmark@Sun.COM 
277211042SErik.Nordmark@Sun.COM done:
277311042SErik.Nordmark@Sun.COM 	ire_refhold(ire);
277411042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
277511042SErik.Nordmark@Sun.COM 	return (ire);
277611042SErik.Nordmark@Sun.COM }
277711042SErik.Nordmark@Sun.COM 
277811042SErik.Nordmark@Sun.COM /*
277911042SErik.Nordmark@Sun.COM  * Find the ill used to send packets. This will be NULL in case
278011042SErik.Nordmark@Sun.COM  * of a reject or blackhole.
278111042SErik.Nordmark@Sun.COM  * The returned ill is held; caller needs to do ill_refrele when done.
278211042SErik.Nordmark@Sun.COM  */
278311042SErik.Nordmark@Sun.COM ill_t *
ire_nexthop_ill(ire_t * ire)278411042SErik.Nordmark@Sun.COM ire_nexthop_ill(ire_t *ire)
278511042SErik.Nordmark@Sun.COM {
278611042SErik.Nordmark@Sun.COM 	ill_t		*ill;
278711042SErik.Nordmark@Sun.COM 
278811042SErik.Nordmark@Sun.COM 	ire = ire_nexthop(ire);
278911042SErik.Nordmark@Sun.COM 	if (ire == NULL)
279011042SErik.Nordmark@Sun.COM 		return (NULL);
279111042SErik.Nordmark@Sun.COM 
279211042SErik.Nordmark@Sun.COM 	/* ire_ill can not change for an existing ire */
279311042SErik.Nordmark@Sun.COM 	ill = ire->ire_ill;
279411042SErik.Nordmark@Sun.COM 	if (ill != NULL)
279511042SErik.Nordmark@Sun.COM 		ill_refhold(ill);
279611042SErik.Nordmark@Sun.COM 	ire_refrele(ire);
279711042SErik.Nordmark@Sun.COM 	return (ill);
279811042SErik.Nordmark@Sun.COM }
279911042SErik.Nordmark@Sun.COM 
280011042SErik.Nordmark@Sun.COM #ifdef DEBUG
280111042SErik.Nordmark@Sun.COM static boolean_t
parent_has_child(ire_t * parent,ire_t * child)280211042SErik.Nordmark@Sun.COM parent_has_child(ire_t *parent, ire_t *child)
280311042SErik.Nordmark@Sun.COM {
280411042SErik.Nordmark@Sun.COM 	ire_t	*ire;
280511042SErik.Nordmark@Sun.COM 	ire_t	*prev;
280611042SErik.Nordmark@Sun.COM 
280711042SErik.Nordmark@Sun.COM 	ire = parent->ire_dep_children;
280811042SErik.Nordmark@Sun.COM 	prev = NULL;
280911042SErik.Nordmark@Sun.COM 	while (ire != NULL) {
281011042SErik.Nordmark@Sun.COM 		if (prev == NULL) {
281111042SErik.Nordmark@Sun.COM 			ASSERT(ire->ire_dep_sib_ptpn ==
281211042SErik.Nordmark@Sun.COM 			    &(parent->ire_dep_children));
281311042SErik.Nordmark@Sun.COM 		} else {
281411042SErik.Nordmark@Sun.COM 			ASSERT(ire->ire_dep_sib_ptpn ==
281511042SErik.Nordmark@Sun.COM 			    &(prev->ire_dep_sib_next));
281611042SErik.Nordmark@Sun.COM 		}
281711042SErik.Nordmark@Sun.COM 		if (ire == child)
281811042SErik.Nordmark@Sun.COM 			return (B_TRUE);
281911042SErik.Nordmark@Sun.COM 		prev = ire;
282011042SErik.Nordmark@Sun.COM 		ire = ire->ire_dep_sib_next;
282111042SErik.Nordmark@Sun.COM 	}
282211042SErik.Nordmark@Sun.COM 	return (B_FALSE);
282311042SErik.Nordmark@Sun.COM }
282411042SErik.Nordmark@Sun.COM 
282511042SErik.Nordmark@Sun.COM static void
ire_dep_verify(ire_t * ire)282611042SErik.Nordmark@Sun.COM ire_dep_verify(ire_t *ire)
282711042SErik.Nordmark@Sun.COM {
282811042SErik.Nordmark@Sun.COM 	ire_t		*parent = ire->ire_dep_parent;
282911042SErik.Nordmark@Sun.COM 	ire_t		*child = ire->ire_dep_children;
283011042SErik.Nordmark@Sun.COM 
283111042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
283211042SErik.Nordmark@Sun.COM 	    ire->ire_ipversion == IPV6_VERSION);
283311042SErik.Nordmark@Sun.COM 	if (parent != NULL) {
283411042SErik.Nordmark@Sun.COM 		ASSERT(parent->ire_ipversion == IPV4_VERSION ||
283511042SErik.Nordmark@Sun.COM 		    parent->ire_ipversion == IPV6_VERSION);
283611042SErik.Nordmark@Sun.COM 		ASSERT(parent->ire_refcnt >= 1);
283711042SErik.Nordmark@Sun.COM 		ASSERT(parent_has_child(parent, ire));
283811042SErik.Nordmark@Sun.COM 	}
283911042SErik.Nordmark@Sun.COM 	if (child != NULL) {
284011042SErik.Nordmark@Sun.COM 		ASSERT(child->ire_ipversion == IPV4_VERSION ||
284111042SErik.Nordmark@Sun.COM 		    child->ire_ipversion == IPV6_VERSION);
284211042SErik.Nordmark@Sun.COM 		ASSERT(child->ire_dep_parent == ire);
284311042SErik.Nordmark@Sun.COM 		ASSERT(child->ire_dep_sib_ptpn != NULL);
284411042SErik.Nordmark@Sun.COM 		ASSERT(parent_has_child(ire, child));
284511042SErik.Nordmark@Sun.COM 	}
284611042SErik.Nordmark@Sun.COM }
284711042SErik.Nordmark@Sun.COM #endif /* DEBUG */
284811042SErik.Nordmark@Sun.COM 
284911042SErik.Nordmark@Sun.COM /*
285011042SErik.Nordmark@Sun.COM  * Assumes ire_dep_parent is set. Remove this child from its parent's linkage.
285111042SErik.Nordmark@Sun.COM  */
285211042SErik.Nordmark@Sun.COM void
ire_dep_remove(ire_t * ire)285311042SErik.Nordmark@Sun.COM ire_dep_remove(ire_t *ire)
285411042SErik.Nordmark@Sun.COM {
285511042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
285611042SErik.Nordmark@Sun.COM 	ire_t		*parent = ire->ire_dep_parent;
285711042SErik.Nordmark@Sun.COM 	ire_t		*next;
285811042SErik.Nordmark@Sun.COM 	nce_t		*nce;
285911042SErik.Nordmark@Sun.COM 
286011042SErik.Nordmark@Sun.COM 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
286111042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_dep_parent != NULL);
286211042SErik.Nordmark@Sun.COM 	ASSERT(ire->ire_dep_sib_ptpn != NULL);
286311042SErik.Nordmark@Sun.COM 
286411042SErik.Nordmark@Sun.COM #ifdef DEBUG
286511042SErik.Nordmark@Sun.COM 	ire_dep_verify(ire);
286611042SErik.Nordmark@Sun.COM 	ire_dep_verify(parent);
286711042SErik.Nordmark@Sun.COM #endif
286811042SErik.Nordmark@Sun.COM 
286911042SErik.Nordmark@Sun.COM 	next = ire->ire_dep_sib_next;
287011042SErik.Nordmark@Sun.COM 	if (next != NULL)
287111042SErik.Nordmark@Sun.COM 		next->ire_dep_sib_ptpn = ire->ire_dep_sib_ptpn;
287211042SErik.Nordmark@Sun.COM 
287311042SErik.Nordmark@Sun.COM 	ASSERT(*(ire->ire_dep_sib_ptpn) == ire);
287411042SErik.Nordmark@Sun.COM 	*(ire->ire_dep_sib_ptpn) = ire->ire_dep_sib_next;
287511042SErik.Nordmark@Sun.COM 
287611042SErik.Nordmark@Sun.COM 	ire->ire_dep_sib_ptpn = NULL;
287711042SErik.Nordmark@Sun.COM 	ire->ire_dep_sib_next = NULL;
287811042SErik.Nordmark@Sun.COM 
287911042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
288011042SErik.Nordmark@Sun.COM 	parent = ire->ire_dep_parent;
288111042SErik.Nordmark@Sun.COM 	ire->ire_dep_parent = NULL;
288211042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
28832535Ssangeeta 
28842535Ssangeeta 	/*
288511042SErik.Nordmark@Sun.COM 	 * Make sure all our children, grandchildren, etc set
288611042SErik.Nordmark@Sun.COM 	 * ire_dep_parent_generation to IRE_GENERATION_VERIFY since
288711042SErik.Nordmark@Sun.COM 	 * we can no longer guarantee than the children have a current
288811042SErik.Nordmark@Sun.COM 	 * ire_nce_cache and ire_nexthop_ill().
28892535Ssangeeta 	 */
289011042SErik.Nordmark@Sun.COM 	if (ire->ire_dep_children != NULL)
289111042SErik.Nordmark@Sun.COM 		ire_dep_invalidate_children(ire->ire_dep_children);
28922535Ssangeeta 
28932535Ssangeeta 	/*
289411042SErik.Nordmark@Sun.COM 	 * Since the parent is gone we make sure we clear ire_nce_cache.
289511042SErik.Nordmark@Sun.COM 	 * We can clear it under ire_lock even if the IRE is used
28962535Ssangeeta 	 */
289711042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
289811042SErik.Nordmark@Sun.COM 	nce = ire->ire_nce_cache;
289911042SErik.Nordmark@Sun.COM 	ire->ire_nce_cache = NULL;
290011042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
290111042SErik.Nordmark@Sun.COM 	if (nce != NULL)
290211042SErik.Nordmark@Sun.COM 		nce_refrele(nce);
290311042SErik.Nordmark@Sun.COM 
290411042SErik.Nordmark@Sun.COM #ifdef DEBUG
290511042SErik.Nordmark@Sun.COM 	ire_dep_verify(ire);
290611042SErik.Nordmark@Sun.COM 	ire_dep_verify(parent);
290711042SErik.Nordmark@Sun.COM #endif
290811042SErik.Nordmark@Sun.COM 
290911042SErik.Nordmark@Sun.COM 	ire_refrele_notr(parent);
291011042SErik.Nordmark@Sun.COM 	ire_refrele_notr(ire);
291111042SErik.Nordmark@Sun.COM }
291211042SErik.Nordmark@Sun.COM 
291311042SErik.Nordmark@Sun.COM /*
291411042SErik.Nordmark@Sun.COM  * Insert the child in the linkage of the parent
291511042SErik.Nordmark@Sun.COM  */
291611042SErik.Nordmark@Sun.COM static void
ire_dep_parent_insert(ire_t * child,ire_t * parent)291711042SErik.Nordmark@Sun.COM ire_dep_parent_insert(ire_t *child, ire_t *parent)
291811042SErik.Nordmark@Sun.COM {
291911042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = child->ire_ipst;
292011042SErik.Nordmark@Sun.COM 	ire_t		*next;
292111042SErik.Nordmark@Sun.COM 
292211042SErik.Nordmark@Sun.COM 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
292311042SErik.Nordmark@Sun.COM 	ASSERT(child->ire_dep_parent == NULL);
292411042SErik.Nordmark@Sun.COM 
292511042SErik.Nordmark@Sun.COM #ifdef DEBUG
292611042SErik.Nordmark@Sun.COM 	ire_dep_verify(child);
292711042SErik.Nordmark@Sun.COM 	ire_dep_verify(parent);
292811042SErik.Nordmark@Sun.COM #endif
292911042SErik.Nordmark@Sun.COM 	/* No parents => no siblings */
293011042SErik.Nordmark@Sun.COM 	ASSERT(child->ire_dep_sib_ptpn == NULL);
293111042SErik.Nordmark@Sun.COM 	ASSERT(child->ire_dep_sib_next == NULL);
293211042SErik.Nordmark@Sun.COM 
293311042SErik.Nordmark@Sun.COM 	ire_refhold_notr(parent);
293411042SErik.Nordmark@Sun.COM 	ire_refhold_notr(child);
293511042SErik.Nordmark@Sun.COM 
293611042SErik.Nordmark@Sun.COM 	/* Head insertion */
293711042SErik.Nordmark@Sun.COM 	next = parent->ire_dep_children;
293811042SErik.Nordmark@Sun.COM 	if (next != NULL) {
293911042SErik.Nordmark@Sun.COM 		ASSERT(next->ire_dep_sib_ptpn == &(parent->ire_dep_children));
294011042SErik.Nordmark@Sun.COM 		child->ire_dep_sib_next = next;
294111042SErik.Nordmark@Sun.COM 		next->ire_dep_sib_ptpn = &(child->ire_dep_sib_next);
294211042SErik.Nordmark@Sun.COM 	}
294311042SErik.Nordmark@Sun.COM 	parent->ire_dep_children = child;
294411042SErik.Nordmark@Sun.COM 	child->ire_dep_sib_ptpn = &(parent->ire_dep_children);
294511042SErik.Nordmark@Sun.COM 
294611042SErik.Nordmark@Sun.COM 	mutex_enter(&child->ire_lock);
294711042SErik.Nordmark@Sun.COM 	child->ire_dep_parent = parent;
294811042SErik.Nordmark@Sun.COM 	mutex_exit(&child->ire_lock);
294911042SErik.Nordmark@Sun.COM 
295011042SErik.Nordmark@Sun.COM #ifdef DEBUG
295111042SErik.Nordmark@Sun.COM 	ire_dep_verify(child);
295211042SErik.Nordmark@Sun.COM 	ire_dep_verify(parent);
295311042SErik.Nordmark@Sun.COM #endif
295411042SErik.Nordmark@Sun.COM }
295511042SErik.Nordmark@Sun.COM 
295611042SErik.Nordmark@Sun.COM 
295711042SErik.Nordmark@Sun.COM /*
295811042SErik.Nordmark@Sun.COM  * Given count worth of ires and generations, build ire_dep_* relationships
295911042SErik.Nordmark@Sun.COM  * from ires[0] to ires[count-1]. Record generations[i+1] in
296011042SErik.Nordmark@Sun.COM  * ire_dep_parent_generation for ires[i].
296111042SErik.Nordmark@Sun.COM  * We graft onto an existing parent chain by making sure that we don't
296211042SErik.Nordmark@Sun.COM  * touch ire_dep_parent for ires[count-1].
296311042SErik.Nordmark@Sun.COM  *
296411042SErik.Nordmark@Sun.COM  * We check for any condemned ire_generation count and return B_FALSE in
296511042SErik.Nordmark@Sun.COM  * that case so that the caller can tear it apart.
296611042SErik.Nordmark@Sun.COM  *
296711042SErik.Nordmark@Sun.COM  * Note that generations[0] is not used. Caller handles that.
296811042SErik.Nordmark@Sun.COM  */
296911042SErik.Nordmark@Sun.COM boolean_t
ire_dep_build(ire_t * ires[],uint_t generations[],uint_t count)297011042SErik.Nordmark@Sun.COM ire_dep_build(ire_t *ires[], uint_t generations[], uint_t count)
297111042SErik.Nordmark@Sun.COM {
297211042SErik.Nordmark@Sun.COM 	ire_t		*ire = ires[0];
297311042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst;
297411042SErik.Nordmark@Sun.COM 	uint_t		i;
297511042SErik.Nordmark@Sun.COM 
297611042SErik.Nordmark@Sun.COM 	ASSERT(count > 0);
297711042SErik.Nordmark@Sun.COM 	if (count == 1) {
297811042SErik.Nordmark@Sun.COM 		/* No work to do */
297911042SErik.Nordmark@Sun.COM 		return (B_TRUE);
298011042SErik.Nordmark@Sun.COM 	}
298111042SErik.Nordmark@Sun.COM 	ipst = ire->ire_ipst;
298211042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
298311042SErik.Nordmark@Sun.COM 	/*
298411042SErik.Nordmark@Sun.COM 	 * Do not remove the linkage for any existing parent chain i.e.,
298511042SErik.Nordmark@Sun.COM 	 * ires[count-1] is left alone.
298611042SErik.Nordmark@Sun.COM 	 */
298711042SErik.Nordmark@Sun.COM 	for (i = 0; i < count-1; i++) {
298811042SErik.Nordmark@Sun.COM 		/* Remove existing parent if we need to change it */
298911042SErik.Nordmark@Sun.COM 		if (ires[i]->ire_dep_parent != NULL &&
299011042SErik.Nordmark@Sun.COM 		    ires[i]->ire_dep_parent != ires[i+1])
299111042SErik.Nordmark@Sun.COM 			ire_dep_remove(ires[i]);
299211042SErik.Nordmark@Sun.COM 	}
299311042SErik.Nordmark@Sun.COM 
299411042SErik.Nordmark@Sun.COM 	for (i = 0; i < count - 1; i++) {
299511042SErik.Nordmark@Sun.COM 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
299611042SErik.Nordmark@Sun.COM 		    ires[i]->ire_ipversion == IPV6_VERSION);
299711042SErik.Nordmark@Sun.COM 		/* Does it need to change? */
299811042SErik.Nordmark@Sun.COM 		if (ires[i]->ire_dep_parent != ires[i+1])
299911042SErik.Nordmark@Sun.COM 			ire_dep_parent_insert(ires[i], ires[i+1]);
300011042SErik.Nordmark@Sun.COM 
300111042SErik.Nordmark@Sun.COM 		mutex_enter(&ires[i+1]->ire_lock);
300211042SErik.Nordmark@Sun.COM 		if (IRE_IS_CONDEMNED(ires[i+1])) {
300311042SErik.Nordmark@Sun.COM 			mutex_exit(&ires[i+1]->ire_lock);
300411042SErik.Nordmark@Sun.COM 			rw_exit(&ipst->ips_ire_dep_lock);
300511042SErik.Nordmark@Sun.COM 			return (B_FALSE);
300611042SErik.Nordmark@Sun.COM 		}
300711042SErik.Nordmark@Sun.COM 		mutex_exit(&ires[i+1]->ire_lock);
300811042SErik.Nordmark@Sun.COM 
300911042SErik.Nordmark@Sun.COM 		mutex_enter(&ires[i]->ire_lock);
301011042SErik.Nordmark@Sun.COM 		ires[i]->ire_dep_parent_generation = generations[i+1];
301111042SErik.Nordmark@Sun.COM 		mutex_exit(&ires[i]->ire_lock);
301211042SErik.Nordmark@Sun.COM 	}
301311042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
301411042SErik.Nordmark@Sun.COM 	return (B_TRUE);
301511042SErik.Nordmark@Sun.COM }
301611042SErik.Nordmark@Sun.COM 
301711042SErik.Nordmark@Sun.COM /*
301811042SErik.Nordmark@Sun.COM  * Given count worth of ires, unbuild ire_dep_* relationships
301911042SErik.Nordmark@Sun.COM  * from ires[0] to ires[count-1].
302011042SErik.Nordmark@Sun.COM  */
302111042SErik.Nordmark@Sun.COM void
ire_dep_unbuild(ire_t * ires[],uint_t count)302211042SErik.Nordmark@Sun.COM ire_dep_unbuild(ire_t *ires[], uint_t count)
302311042SErik.Nordmark@Sun.COM {
302411042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst;
302511042SErik.Nordmark@Sun.COM 	uint_t		i;
302611042SErik.Nordmark@Sun.COM 
302711042SErik.Nordmark@Sun.COM 	if (count == 0) {
302811042SErik.Nordmark@Sun.COM 		/* No work to do */
30292535Ssangeeta 		return;
30302535Ssangeeta 	}
303111042SErik.Nordmark@Sun.COM 	ipst = ires[0]->ire_ipst;
303211042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_WRITER);
303311042SErik.Nordmark@Sun.COM 	for (i = 0; i < count; i++) {
303411042SErik.Nordmark@Sun.COM 		ASSERT(ires[i]->ire_ipversion == IPV4_VERSION ||
303511042SErik.Nordmark@Sun.COM 		    ires[i]->ire_ipversion == IPV6_VERSION);
303611042SErik.Nordmark@Sun.COM 		if (ires[i]->ire_dep_parent != NULL)
303711042SErik.Nordmark@Sun.COM 			ire_dep_remove(ires[i]);
303811042SErik.Nordmark@Sun.COM 		mutex_enter(&ires[i]->ire_lock);
303911042SErik.Nordmark@Sun.COM 		ires[i]->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
304011042SErik.Nordmark@Sun.COM 		mutex_exit(&ires[i]->ire_lock);
304111042SErik.Nordmark@Sun.COM 	}
304211042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
304311042SErik.Nordmark@Sun.COM }
304411042SErik.Nordmark@Sun.COM 
304511042SErik.Nordmark@Sun.COM /*
304611042SErik.Nordmark@Sun.COM  * Both the forwarding and the outbound code paths can trip on
304711042SErik.Nordmark@Sun.COM  * a condemned NCE, in which case we call this function.
304811042SErik.Nordmark@Sun.COM  * We have two different behaviors: if the NCE was UNREACHABLE
304911042SErik.Nordmark@Sun.COM  * it is an indication that something failed. In that case
305011042SErik.Nordmark@Sun.COM  * we see if we should look for a different IRE (for example,
305111042SErik.Nordmark@Sun.COM  * delete any matching redirect IRE, or try a different
305211042SErik.Nordmark@Sun.COM  * IRE_DEFAULT (ECMP)). We mark the ire as bad so a hopefully
305311042SErik.Nordmark@Sun.COM  * different IRE will be picked next time we send/forward.
305411042SErik.Nordmark@Sun.COM  *
305511042SErik.Nordmark@Sun.COM  * If we are called by the output path then fail_if_better is set
305611042SErik.Nordmark@Sun.COM  * and we return NULL if there could be a better IRE. This is because the
305711042SErik.Nordmark@Sun.COM  * output path retries the IRE lookup. (The input/forward path can not retry.)
305811042SErik.Nordmark@Sun.COM  *
305911042SErik.Nordmark@Sun.COM  * If the NCE was not unreachable then we pick/allocate a
306011042SErik.Nordmark@Sun.COM  * new (most likely ND_INITIAL) NCE and proceed with it.
306111042SErik.Nordmark@Sun.COM  *
306211042SErik.Nordmark@Sun.COM  * ipha/ip6h are needed for multicast packets; ipha needs to be
306311042SErik.Nordmark@Sun.COM  * set for IPv4 and ip6h needs to be set for IPv6 packets.
306411042SErik.Nordmark@Sun.COM  */
306511042SErik.Nordmark@Sun.COM nce_t *
ire_handle_condemned_nce(nce_t * nce,ire_t * ire,ipha_t * ipha,ip6_t * ip6h,boolean_t fail_if_better)306611042SErik.Nordmark@Sun.COM ire_handle_condemned_nce(nce_t *nce, ire_t *ire, ipha_t *ipha, ip6_t *ip6h,
306711042SErik.Nordmark@Sun.COM     boolean_t fail_if_better)
306811042SErik.Nordmark@Sun.COM {
306911042SErik.Nordmark@Sun.COM 	if (nce->nce_common->ncec_state == ND_UNREACHABLE) {
307011042SErik.Nordmark@Sun.COM 		if (ire_no_good(ire) && fail_if_better) {
307111042SErik.Nordmark@Sun.COM 			/*
307211042SErik.Nordmark@Sun.COM 			 * Did some changes, or ECMP likely to exist.
307311042SErik.Nordmark@Sun.COM 			 * Make ip_output look for a different IRE
307411042SErik.Nordmark@Sun.COM 			 */
307511042SErik.Nordmark@Sun.COM 			return (NULL);
307611042SErik.Nordmark@Sun.COM 		}
307711042SErik.Nordmark@Sun.COM 	}
307811042SErik.Nordmark@Sun.COM 	if (ire_revalidate_nce(ire) == ENETUNREACH) {
307911042SErik.Nordmark@Sun.COM 		/* The ire_dep_parent chain went bad, or no memory? */
308011042SErik.Nordmark@Sun.COM 		(void) ire_no_good(ire);
308111042SErik.Nordmark@Sun.COM 		return (NULL);
308211042SErik.Nordmark@Sun.COM 	}
308311042SErik.Nordmark@Sun.COM 	if (ire->ire_ipversion == IPV4_VERSION) {
308411042SErik.Nordmark@Sun.COM 		ASSERT(ipha != NULL);
308511042SErik.Nordmark@Sun.COM 		nce = ire_to_nce(ire, ipha->ipha_dst, NULL);
308611042SErik.Nordmark@Sun.COM 	} else {
308711042SErik.Nordmark@Sun.COM 		ASSERT(ip6h != NULL);
308811042SErik.Nordmark@Sun.COM 		nce = ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst);
30892535Ssangeeta 	}
30908485SPeter.Memishian@Sun.COM 
309111042SErik.Nordmark@Sun.COM 	if (nce == NULL)
309211042SErik.Nordmark@Sun.COM 		return (NULL);
309311042SErik.Nordmark@Sun.COM 	if (nce->nce_is_condemned) {
309411042SErik.Nordmark@Sun.COM 		nce_refrele(nce);
309511042SErik.Nordmark@Sun.COM 		return (NULL);
30962535Ssangeeta 	}
309711042SErik.Nordmark@Sun.COM 	return (nce);
309811042SErik.Nordmark@Sun.COM }
309911042SErik.Nordmark@Sun.COM 
310011042SErik.Nordmark@Sun.COM /*
310111042SErik.Nordmark@Sun.COM  * The caller has found that the ire is bad, either due to a reference to an NCE
310211042SErik.Nordmark@Sun.COM  * in ND_UNREACHABLE state, or a MULTIRT route whose gateway can't be resolved.
310311042SErik.Nordmark@Sun.COM  * We update things so a subsequent attempt to send to the destination
310411042SErik.Nordmark@Sun.COM  * is likely to find different IRE, or that a new NCE would be created.
310511042SErik.Nordmark@Sun.COM  *
310611042SErik.Nordmark@Sun.COM  * Returns B_TRUE if it is likely that a subsequent ire_ftable_lookup would
310711042SErik.Nordmark@Sun.COM  * find a different route (either due to having deleted a redirect, or there
310811042SErik.Nordmark@Sun.COM  * being ECMP routes.)
310911042SErik.Nordmark@Sun.COM  *
311011042SErik.Nordmark@Sun.COM  * If we have a redirect (RTF_DYNAMIC) we delete it.
311111042SErik.Nordmark@Sun.COM  * Otherwise we increment ire_badcnt and increment the generation number so
311211042SErik.Nordmark@Sun.COM  * that a cached ixa_ire will redo the route selection. ire_badcnt is taken
311311042SErik.Nordmark@Sun.COM  * into account in the route selection when we have multiple choices (multiple
311411042SErik.Nordmark@Sun.COM  * default routes or ECMP in general).
311511042SErik.Nordmark@Sun.COM  * Any time ip_select_route find an ire with a condemned ire_nce_cache
311611042SErik.Nordmark@Sun.COM  * (e.g., if no equal cost route to the bad one) ip_select_route will make
311711042SErik.Nordmark@Sun.COM  * sure the NCE is revalidated to avoid getting stuck on a
311811042SErik.Nordmark@Sun.COM  * NCE_F_CONDMNED ncec that caused ire_no_good to be called.
311911042SErik.Nordmark@Sun.COM  */
312011042SErik.Nordmark@Sun.COM boolean_t
ire_no_good(ire_t * ire)312111042SErik.Nordmark@Sun.COM ire_no_good(ire_t *ire)
312211042SErik.Nordmark@Sun.COM {
312311042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
312411042SErik.Nordmark@Sun.COM 	ire_t		*ire2;
312511042SErik.Nordmark@Sun.COM 	nce_t		*nce;
312611042SErik.Nordmark@Sun.COM 
312711042SErik.Nordmark@Sun.COM 	if (ire->ire_flags & RTF_DYNAMIC) {
312811042SErik.Nordmark@Sun.COM 		ire_delete(ire);
312911042SErik.Nordmark@Sun.COM 		return (B_TRUE);
313011042SErik.Nordmark@Sun.COM 	}
313111042SErik.Nordmark@Sun.COM 	if (ire->ire_flags & RTF_INDIRECT) {
313211042SErik.Nordmark@Sun.COM 		/* Check if next IRE is a redirect */
313311042SErik.Nordmark@Sun.COM 		rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
313411042SErik.Nordmark@Sun.COM 		if (ire->ire_dep_parent != NULL &&
313511042SErik.Nordmark@Sun.COM 		    (ire->ire_dep_parent->ire_flags & RTF_DYNAMIC)) {
313611042SErik.Nordmark@Sun.COM 			ire2 = ire->ire_dep_parent;
313711042SErik.Nordmark@Sun.COM 			ire_refhold(ire2);
313811042SErik.Nordmark@Sun.COM 		} else {
313911042SErik.Nordmark@Sun.COM 			ire2 = NULL;
314011042SErik.Nordmark@Sun.COM 		}
314111042SErik.Nordmark@Sun.COM 		rw_exit(&ipst->ips_ire_dep_lock);
314211042SErik.Nordmark@Sun.COM 		if (ire2 != NULL) {
314311042SErik.Nordmark@Sun.COM 			ire_delete(ire2);
314411042SErik.Nordmark@Sun.COM 			ire_refrele(ire2);
314511042SErik.Nordmark@Sun.COM 			return (B_TRUE);
314611042SErik.Nordmark@Sun.COM 		}
314711042SErik.Nordmark@Sun.COM 	}
31482535Ssangeeta 	/*
314911042SErik.Nordmark@Sun.COM 	 * No redirect involved. Increment badcnt so that if we have ECMP
315011042SErik.Nordmark@Sun.COM 	 * routes we are likely to pick a different one for the next packet.
315111042SErik.Nordmark@Sun.COM 	 *
315211042SErik.Nordmark@Sun.COM 	 * If the NCE is unreachable and condemned we should drop the reference
315311042SErik.Nordmark@Sun.COM 	 * to it so that a new NCE can be created.
315411042SErik.Nordmark@Sun.COM 	 *
315511042SErik.Nordmark@Sun.COM 	 * Finally we increment the generation number so that any ixa_ire
315611042SErik.Nordmark@Sun.COM 	 * cache will be revalidated.
31572535Ssangeeta 	 */
315811042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
315911042SErik.Nordmark@Sun.COM 	ire->ire_badcnt++;
316011066Srafael.vanoni@sun.com 	ire->ire_last_badcnt = TICK_TO_SEC(ddi_get_lbolt64());
316111042SErik.Nordmark@Sun.COM 	nce = ire->ire_nce_cache;
316211042SErik.Nordmark@Sun.COM 	if (nce != NULL && nce->nce_is_condemned &&
316311042SErik.Nordmark@Sun.COM 	    nce->nce_common->ncec_state == ND_UNREACHABLE)
316411042SErik.Nordmark@Sun.COM 		ire->ire_nce_cache = NULL;
316511042SErik.Nordmark@Sun.COM 	else
316611042SErik.Nordmark@Sun.COM 		nce = NULL;
316711042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
316811042SErik.Nordmark@Sun.COM 	if (nce != NULL)
316911042SErik.Nordmark@Sun.COM 		nce_refrele(nce);
317011042SErik.Nordmark@Sun.COM 
317111042SErik.Nordmark@Sun.COM 	ire_increment_generation(ire);
317211042SErik.Nordmark@Sun.COM 	ire_dep_incr_generation(ire);
317311042SErik.Nordmark@Sun.COM 
317411042SErik.Nordmark@Sun.COM 	return (ire->ire_bucket->irb_ire_cnt > 1);
317511042SErik.Nordmark@Sun.COM }
317611042SErik.Nordmark@Sun.COM 
317711042SErik.Nordmark@Sun.COM /*
317811042SErik.Nordmark@Sun.COM  * Walk ire_dep_parent chain and validate that ire_dep_parent->ire_generation ==
317911042SErik.Nordmark@Sun.COM  * ire_dep_parent_generation.
318011042SErik.Nordmark@Sun.COM  * If they all match we just return ire_generation from the topmost IRE.
318111042SErik.Nordmark@Sun.COM  * Otherwise we propagate the mismatch by setting all ire_dep_parent_generation
318211042SErik.Nordmark@Sun.COM  * above the mismatch to IRE_GENERATION_VERIFY and also returning
318311042SErik.Nordmark@Sun.COM  * IRE_GENERATION_VERIFY.
318411042SErik.Nordmark@Sun.COM  */
318511042SErik.Nordmark@Sun.COM uint_t
ire_dep_validate_generations(ire_t * ire)318611042SErik.Nordmark@Sun.COM ire_dep_validate_generations(ire_t *ire)
318711042SErik.Nordmark@Sun.COM {
318811042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
318911042SErik.Nordmark@Sun.COM 	uint_t		generation;
319011042SErik.Nordmark@Sun.COM 	ire_t		*ire1;
319111042SErik.Nordmark@Sun.COM 
319211042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
319311042SErik.Nordmark@Sun.COM 	generation = ire->ire_generation;	/* Assuming things match */
319411042SErik.Nordmark@Sun.COM 	for (ire1 = ire; ire1 != NULL; ire1 = ire1->ire_dep_parent) {
319511042SErik.Nordmark@Sun.COM 		ASSERT(ire1->ire_ipversion == IPV4_VERSION ||
319611042SErik.Nordmark@Sun.COM 		    ire1->ire_ipversion == IPV6_VERSION);
319711042SErik.Nordmark@Sun.COM 		if (ire1->ire_dep_parent == NULL)
319811042SErik.Nordmark@Sun.COM 			break;
319911042SErik.Nordmark@Sun.COM 		if (ire1->ire_dep_parent_generation !=
320011042SErik.Nordmark@Sun.COM 		    ire1->ire_dep_parent->ire_generation)
320111042SErik.Nordmark@Sun.COM 			goto mismatch;
320211042SErik.Nordmark@Sun.COM 	}
320311042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
320411042SErik.Nordmark@Sun.COM 	return (generation);
320511042SErik.Nordmark@Sun.COM 
320611042SErik.Nordmark@Sun.COM mismatch:
320711042SErik.Nordmark@Sun.COM 	generation = IRE_GENERATION_VERIFY;
320811042SErik.Nordmark@Sun.COM 	/* Fill from top down to the mismatch with _VERIFY */
320911042SErik.Nordmark@Sun.COM 	while (ire != ire1) {
321011042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
321111042SErik.Nordmark@Sun.COM 		    ire->ire_ipversion == IPV6_VERSION);
321211042SErik.Nordmark@Sun.COM 		mutex_enter(&ire->ire_lock);
321311042SErik.Nordmark@Sun.COM 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
321411042SErik.Nordmark@Sun.COM 		mutex_exit(&ire->ire_lock);
321511042SErik.Nordmark@Sun.COM 		ire = ire->ire_dep_parent;
32162535Ssangeeta 	}
321711042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
321811042SErik.Nordmark@Sun.COM 	return (generation);
321911042SErik.Nordmark@Sun.COM }
322011042SErik.Nordmark@Sun.COM 
322111042SErik.Nordmark@Sun.COM /*
322211042SErik.Nordmark@Sun.COM  * Used when we need to return an ire with ire_dep_parent, but we
322311042SErik.Nordmark@Sun.COM  * know the chain is invalid for instance we didn't create an IRE_IF_CLONE
322411042SErik.Nordmark@Sun.COM  * Using IRE_GENERATION_VERIFY means that next time we'll redo the
322511042SErik.Nordmark@Sun.COM  * recursive lookup.
322611042SErik.Nordmark@Sun.COM  */
322711042SErik.Nordmark@Sun.COM void
ire_dep_invalidate_generations(ire_t * ire)322811042SErik.Nordmark@Sun.COM ire_dep_invalidate_generations(ire_t *ire)
322911042SErik.Nordmark@Sun.COM {
323011042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ire->ire_ipst;
323111042SErik.Nordmark@Sun.COM 
323211042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
323311042SErik.Nordmark@Sun.COM 	while (ire != NULL) {
323411042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_ipversion == IPV4_VERSION ||
323511042SErik.Nordmark@Sun.COM 		    ire->ire_ipversion == IPV6_VERSION);
323611042SErik.Nordmark@Sun.COM 		mutex_enter(&ire->ire_lock);
323711042SErik.Nordmark@Sun.COM 		ire->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
323811042SErik.Nordmark@Sun.COM 		mutex_exit(&ire->ire_lock);
323911042SErik.Nordmark@Sun.COM 		ire = ire->ire_dep_parent;
324011042SErik.Nordmark@Sun.COM 	}
324111042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
324211042SErik.Nordmark@Sun.COM }
324311042SErik.Nordmark@Sun.COM 
324411042SErik.Nordmark@Sun.COM /* Set _VERIFY ire_dep_parent_generation for all children recursively */
324511042SErik.Nordmark@Sun.COM static void
ire_dep_invalidate_children(ire_t * child)324611042SErik.Nordmark@Sun.COM ire_dep_invalidate_children(ire_t *child)
324711042SErik.Nordmark@Sun.COM {
324811042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = child->ire_ipst;
324911042SErik.Nordmark@Sun.COM 
325011042SErik.Nordmark@Sun.COM 	ASSERT(RW_WRITE_HELD(&ipst->ips_ire_dep_lock));
325111042SErik.Nordmark@Sun.COM 	/* Depth first */
325211042SErik.Nordmark@Sun.COM 	if (child->ire_dep_children != NULL)
325311042SErik.Nordmark@Sun.COM 		ire_dep_invalidate_children(child->ire_dep_children);
325411042SErik.Nordmark@Sun.COM 
325511042SErik.Nordmark@Sun.COM 	while (child != NULL) {
325611042SErik.Nordmark@Sun.COM 		mutex_enter(&child->ire_lock);
325711042SErik.Nordmark@Sun.COM 		child->ire_dep_parent_generation = IRE_GENERATION_VERIFY;
325811042SErik.Nordmark@Sun.COM 		mutex_exit(&child->ire_lock);
325911042SErik.Nordmark@Sun.COM 		child = child->ire_dep_sib_next;
326011042SErik.Nordmark@Sun.COM 	}
326111042SErik.Nordmark@Sun.COM }
326211042SErik.Nordmark@Sun.COM 
326311042SErik.Nordmark@Sun.COM static void
ire_dep_increment_children(ire_t * child)326411042SErik.Nordmark@Sun.COM ire_dep_increment_children(ire_t *child)
326511042SErik.Nordmark@Sun.COM {
326611042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = child->ire_ipst;
326711042SErik.Nordmark@Sun.COM 
326811042SErik.Nordmark@Sun.COM 	ASSERT(RW_READ_HELD(&ipst->ips_ire_dep_lock));
326911042SErik.Nordmark@Sun.COM 	/* Depth first */
327011042SErik.Nordmark@Sun.COM 	if (child->ire_dep_children != NULL)
327111042SErik.Nordmark@Sun.COM 		ire_dep_increment_children(child->ire_dep_children);
327211042SErik.Nordmark@Sun.COM 
327311042SErik.Nordmark@Sun.COM 	while (child != NULL) {
327411042SErik.Nordmark@Sun.COM 		if (!IRE_IS_CONDEMNED(child))
327511042SErik.Nordmark@Sun.COM 			ire_increment_generation(child);
327611042SErik.Nordmark@Sun.COM 		child = child->ire_dep_sib_next;
32772535Ssangeeta 	}
32782535Ssangeeta }
32792535Ssangeeta 
32802535Ssangeeta /*
328111042SErik.Nordmark@Sun.COM  * Walk all the children of this ire recursively and increment their
328211042SErik.Nordmark@Sun.COM  * generation number.
32832535Ssangeeta  */
328411463SSowmini.Varadhan@Sun.COM static void
ire_dep_incr_generation_locked(ire_t * parent)328511463SSowmini.Varadhan@Sun.COM ire_dep_incr_generation_locked(ire_t *parent)
328611463SSowmini.Varadhan@Sun.COM {
328711463SSowmini.Varadhan@Sun.COM 	ASSERT(RW_READ_HELD(&parent->ire_ipst->ips_ire_dep_lock));
328811463SSowmini.Varadhan@Sun.COM 	if (parent->ire_dep_children != NULL)
328911463SSowmini.Varadhan@Sun.COM 		ire_dep_increment_children(parent->ire_dep_children);
329011463SSowmini.Varadhan@Sun.COM }
329111463SSowmini.Varadhan@Sun.COM 
32922535Ssangeeta void
ire_dep_incr_generation(ire_t * parent)329311042SErik.Nordmark@Sun.COM ire_dep_incr_generation(ire_t *parent)
32942535Ssangeeta {
329511042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = parent->ire_ipst;
329611042SErik.Nordmark@Sun.COM 
329711042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
329811463SSowmini.Varadhan@Sun.COM 	ire_dep_incr_generation_locked(parent);
329911042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
33002535Ssangeeta }
33012535Ssangeeta 
33023772Ssangeeta /*
330311042SErik.Nordmark@Sun.COM  * Get a new ire_nce_cache for this IRE as well as its nexthop.
330411042SErik.Nordmark@Sun.COM  * Returns zero if it succeeds. Can fail due to lack of memory or when
330511042SErik.Nordmark@Sun.COM  * the route has become unreachable. Returns ENOMEM and ENETUNREACH in those
330611042SErik.Nordmark@Sun.COM  * cases.
330711042SErik.Nordmark@Sun.COM  *
330811042SErik.Nordmark@Sun.COM  * In the in.mpathd case, the ire will have ire_testhidden
330911042SErik.Nordmark@Sun.COM  * set; so we should create the ncec for the underlying ill.
33104714Ssowmini  *
331111042SErik.Nordmark@Sun.COM  * Note that the error returned by ire_revalidate_nce() is ignored by most
331211042SErik.Nordmark@Sun.COM  * callers except ire_handle_condemned_nce(), which handles the ENETUNREACH
331311042SErik.Nordmark@Sun.COM  * error to mark potentially bad ire's. For all the other callers, an
331411042SErik.Nordmark@Sun.COM  * error return could indicate a transient condition like ENOMEM, or could
331511042SErik.Nordmark@Sun.COM  * be the result of an interface that is going down/unplumbing. In the former
331611042SErik.Nordmark@Sun.COM  * case (transient error), we would leave the old stale ire/ire_nce_cache
331711042SErik.Nordmark@Sun.COM  * in place, and possibly use incorrect link-layer information to send packets
331811042SErik.Nordmark@Sun.COM  * but would eventually recover. In the latter case (ill down/replumb),
331911042SErik.Nordmark@Sun.COM  * ire_revalidate_nce() might return a condemned nce back, but we would then
332011042SErik.Nordmark@Sun.COM  * recover in the packet output path.
33212535Ssangeeta  */
33222535Ssangeeta int
ire_revalidate_nce(ire_t * ire)332311042SErik.Nordmark@Sun.COM ire_revalidate_nce(ire_t *ire)
33242535Ssangeeta {
332511042SErik.Nordmark@Sun.COM 	nce_t		*nce, *old_nce;
332611042SErik.Nordmark@Sun.COM 	ire_t		*nexthop;
33272535Ssangeeta 
33282535Ssangeeta 	/*
332911042SErik.Nordmark@Sun.COM 	 * For multicast we conceptually have an NCE but we don't store it
333011042SErik.Nordmark@Sun.COM 	 * in ire_nce_cache; when ire_to_nce is called we allocate the nce.
33312535Ssangeeta 	 */
333211042SErik.Nordmark@Sun.COM 	if (ire->ire_type & IRE_MULTICAST)
333311042SErik.Nordmark@Sun.COM 		return (0);
333411042SErik.Nordmark@Sun.COM 
333511042SErik.Nordmark@Sun.COM 	/* ire_testhidden should only be set on under-interfaces */
333611042SErik.Nordmark@Sun.COM 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
333711042SErik.Nordmark@Sun.COM 
333811042SErik.Nordmark@Sun.COM 	nexthop = ire_nexthop(ire);
333911042SErik.Nordmark@Sun.COM 	if (nexthop == NULL) {
334011042SErik.Nordmark@Sun.COM 		/* The route is potentially bad */
334111042SErik.Nordmark@Sun.COM 		(void) ire_no_good(ire);
334211042SErik.Nordmark@Sun.COM 		return (ENETUNREACH);
33434084Ssowmini 	}
334411042SErik.Nordmark@Sun.COM 	if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
334511042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_ill != NULL);
334611042SErik.Nordmark@Sun.COM 
334711042SErik.Nordmark@Sun.COM 		if (ire->ire_ipversion == IPV4_VERSION)
334811042SErik.Nordmark@Sun.COM 			nce = nce_lookup_v4(ire->ire_ill, &ire->ire_addr);
334911042SErik.Nordmark@Sun.COM 		else
335011042SErik.Nordmark@Sun.COM 			nce = nce_lookup_v6(ire->ire_ill, &ire->ire_addr_v6);
335111042SErik.Nordmark@Sun.COM 	} else {
335211042SErik.Nordmark@Sun.COM 		ASSERT(nexthop->ire_type & IRE_ONLINK);
335311042SErik.Nordmark@Sun.COM 		if (ire->ire_ipversion == IPV4_VERSION) {
335411042SErik.Nordmark@Sun.COM 			nce = arp_nce_init(nexthop->ire_ill, nexthop->ire_addr,
335511042SErik.Nordmark@Sun.COM 			    nexthop->ire_type);
335611042SErik.Nordmark@Sun.COM 		} else {
335711042SErik.Nordmark@Sun.COM 			nce = ndp_nce_init(nexthop->ire_ill,
335811042SErik.Nordmark@Sun.COM 			    &nexthop->ire_addr_v6, nexthop->ire_type);
335911042SErik.Nordmark@Sun.COM 		}
33602535Ssangeeta 	}
336111042SErik.Nordmark@Sun.COM 	if (nce == NULL) {
33622535Ssangeeta 		/*
336311042SErik.Nordmark@Sun.COM 		 * Leave the old stale one in place to avoid a NULL
336411042SErik.Nordmark@Sun.COM 		 * ire_nce_cache.
33652535Ssangeeta 		 */
336611042SErik.Nordmark@Sun.COM 		ire_refrele(nexthop);
336711042SErik.Nordmark@Sun.COM 		return (ENOMEM);
336811042SErik.Nordmark@Sun.COM 	}
336911042SErik.Nordmark@Sun.COM 
337011042SErik.Nordmark@Sun.COM 	if (nexthop != ire) {
337111042SErik.Nordmark@Sun.COM 		/* Update the nexthop ire */
337211042SErik.Nordmark@Sun.COM 		mutex_enter(&nexthop->ire_lock);
337311042SErik.Nordmark@Sun.COM 		old_nce = nexthop->ire_nce_cache;
337411042SErik.Nordmark@Sun.COM 		if (!IRE_IS_CONDEMNED(nexthop)) {
337511042SErik.Nordmark@Sun.COM 			nce_refhold(nce);
337611042SErik.Nordmark@Sun.COM 			nexthop->ire_nce_cache = nce;
337711042SErik.Nordmark@Sun.COM 		} else {
337811042SErik.Nordmark@Sun.COM 			nexthop->ire_nce_cache = NULL;
337911042SErik.Nordmark@Sun.COM 		}
338011042SErik.Nordmark@Sun.COM 		mutex_exit(&nexthop->ire_lock);
338111042SErik.Nordmark@Sun.COM 		if (old_nce != NULL)
338211042SErik.Nordmark@Sun.COM 			nce_refrele(old_nce);
338311042SErik.Nordmark@Sun.COM 	}
338411042SErik.Nordmark@Sun.COM 	ire_refrele(nexthop);
338511042SErik.Nordmark@Sun.COM 
338611042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
338711042SErik.Nordmark@Sun.COM 	old_nce = ire->ire_nce_cache;
338811042SErik.Nordmark@Sun.COM 	if (!IRE_IS_CONDEMNED(ire)) {
338911042SErik.Nordmark@Sun.COM 		nce_refhold(nce);
339011042SErik.Nordmark@Sun.COM 		ire->ire_nce_cache = nce;
33912535Ssangeeta 	} else {
339211042SErik.Nordmark@Sun.COM 		ire->ire_nce_cache = NULL;
33932535Ssangeeta 	}
339411042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
339511042SErik.Nordmark@Sun.COM 	if (old_nce != NULL)
339611042SErik.Nordmark@Sun.COM 		nce_refrele(old_nce);
339711042SErik.Nordmark@Sun.COM 
339811042SErik.Nordmark@Sun.COM 	nce_refrele(nce);
33992535Ssangeeta 	return (0);
34002535Ssangeeta }
34017880SJonathan.Anderson@Sun.COM 
34027880SJonathan.Anderson@Sun.COM /*
340311042SErik.Nordmark@Sun.COM  * Get a held nce for a given ire.
340411042SErik.Nordmark@Sun.COM  * In the common case this is just from ire_nce_cache.
340511042SErik.Nordmark@Sun.COM  * For IRE_MULTICAST this needs to do an explicit lookup since we do not
340611042SErik.Nordmark@Sun.COM  * have an IRE_MULTICAST per address.
340711042SErik.Nordmark@Sun.COM  * Note that this explicitly returns CONDEMNED NCEs. The caller needs those
340811042SErik.Nordmark@Sun.COM  * so they can check whether the NCE went unreachable (as opposed to was
340911042SErik.Nordmark@Sun.COM  * condemned for some other reason).
34107880SJonathan.Anderson@Sun.COM  */
341111042SErik.Nordmark@Sun.COM nce_t *
ire_to_nce(ire_t * ire,ipaddr_t v4nexthop,const in6_addr_t * v6nexthop)341211042SErik.Nordmark@Sun.COM ire_to_nce(ire_t *ire, ipaddr_t v4nexthop, const in6_addr_t *v6nexthop)
34137880SJonathan.Anderson@Sun.COM {
341411042SErik.Nordmark@Sun.COM 	nce_t	*nce;
341511042SErik.Nordmark@Sun.COM 
341611042SErik.Nordmark@Sun.COM 	if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
34177880SJonathan.Anderson@Sun.COM 		return (NULL);
341811042SErik.Nordmark@Sun.COM 
341911042SErik.Nordmark@Sun.COM 	/* ire_testhidden should only be set on under-interfaces */
342011042SErik.Nordmark@Sun.COM 	ASSERT(!ire->ire_testhidden || !IS_IPMP(ire->ire_ill));
342111042SErik.Nordmark@Sun.COM 
342211042SErik.Nordmark@Sun.COM 	mutex_enter(&ire->ire_lock);
342311042SErik.Nordmark@Sun.COM 	nce = ire->ire_nce_cache;
342411042SErik.Nordmark@Sun.COM 	if (nce != NULL) {
342511042SErik.Nordmark@Sun.COM 		nce_refhold(nce);
342611042SErik.Nordmark@Sun.COM 		mutex_exit(&ire->ire_lock);
342711042SErik.Nordmark@Sun.COM 		return (nce);
34287880SJonathan.Anderson@Sun.COM 	}
342911042SErik.Nordmark@Sun.COM 	mutex_exit(&ire->ire_lock);
343011042SErik.Nordmark@Sun.COM 
343111042SErik.Nordmark@Sun.COM 	if (ire->ire_type & IRE_MULTICAST) {
343211042SErik.Nordmark@Sun.COM 		ASSERT(ire->ire_ill != NULL);
343311042SErik.Nordmark@Sun.COM 
343411042SErik.Nordmark@Sun.COM 		if (ire->ire_ipversion == IPV4_VERSION) {
343511042SErik.Nordmark@Sun.COM 			ASSERT(v6nexthop == NULL);
343611042SErik.Nordmark@Sun.COM 
343711042SErik.Nordmark@Sun.COM 			nce = arp_nce_init(ire->ire_ill, v4nexthop,
343811042SErik.Nordmark@Sun.COM 			    ire->ire_type);
343911042SErik.Nordmark@Sun.COM 		} else {
344011042SErik.Nordmark@Sun.COM 			ASSERT(v6nexthop != NULL);
344111042SErik.Nordmark@Sun.COM 			ASSERT(v4nexthop == 0);
344211042SErik.Nordmark@Sun.COM 			nce = ndp_nce_init(ire->ire_ill, v6nexthop,
344311042SErik.Nordmark@Sun.COM 			    ire->ire_type);
34447880SJonathan.Anderson@Sun.COM 		}
344511042SErik.Nordmark@Sun.COM 		return (nce);
34467880SJonathan.Anderson@Sun.COM 	}
34477880SJonathan.Anderson@Sun.COM 	return (NULL);
34487880SJonathan.Anderson@Sun.COM }
34497880SJonathan.Anderson@Sun.COM 
345011042SErik.Nordmark@Sun.COM nce_t *
ire_to_nce_pkt(ire_t * ire,mblk_t * mp)345111042SErik.Nordmark@Sun.COM ire_to_nce_pkt(ire_t *ire, mblk_t *mp)
345211042SErik.Nordmark@Sun.COM {
345311042SErik.Nordmark@Sun.COM 	ipha_t		*ipha;
345411042SErik.Nordmark@Sun.COM 	ip6_t		*ip6h;
345511042SErik.Nordmark@Sun.COM 
345611042SErik.Nordmark@Sun.COM 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
345711042SErik.Nordmark@Sun.COM 		ipha = (ipha_t *)mp->b_rptr;
345811042SErik.Nordmark@Sun.COM 		return (ire_to_nce(ire, ipha->ipha_dst, NULL));
345911042SErik.Nordmark@Sun.COM 	} else {
346011042SErik.Nordmark@Sun.COM 		ip6h = (ip6_t *)mp->b_rptr;
346111042SErik.Nordmark@Sun.COM 		return (ire_to_nce(ire, INADDR_ANY, &ip6h->ip6_dst));
346211042SErik.Nordmark@Sun.COM 	}
346311042SErik.Nordmark@Sun.COM }
346411042SErik.Nordmark@Sun.COM 
34657880SJonathan.Anderson@Sun.COM /*
346611042SErik.Nordmark@Sun.COM  * Given an IRE_INTERFACE (that matches more than one address) create
346711042SErik.Nordmark@Sun.COM  * and return an IRE_IF_CLONE for the specific address.
346811042SErik.Nordmark@Sun.COM  * Return the generation number.
346911042SErik.Nordmark@Sun.COM  * Returns NULL is no memory for the IRE.
347011042SErik.Nordmark@Sun.COM  * Handles both IPv4 and IPv6.
347112328SSowmini.Varadhan@Sun.COM  *
347212328SSowmini.Varadhan@Sun.COM  * IRE_IF_CLONE entries may only be created adn added by calling
347312328SSowmini.Varadhan@Sun.COM  * ire_create_if_clone(), and we depend on the fact that ire_add will
347412328SSowmini.Varadhan@Sun.COM  * atomically ensure that attempts to add multiple identical IRE_IF_CLONE
347512328SSowmini.Varadhan@Sun.COM  * entries will not result in duplicate (i.e., ire_identical_ref > 1)
347612328SSowmini.Varadhan@Sun.COM  * CLONE entries, so that a single ire_delete is sufficient to remove the
347712328SSowmini.Varadhan@Sun.COM  * CLONE.
34787880SJonathan.Anderson@Sun.COM  */
34797880SJonathan.Anderson@Sun.COM ire_t *
ire_create_if_clone(ire_t * ire_if,const in6_addr_t * addr,uint_t * generationp)348011042SErik.Nordmark@Sun.COM ire_create_if_clone(ire_t *ire_if, const in6_addr_t *addr, uint_t *generationp)
34817880SJonathan.Anderson@Sun.COM {
348211042SErik.Nordmark@Sun.COM 	ire_t		*ire;
348311042SErik.Nordmark@Sun.COM 	ire_t		*nire;
348411042SErik.Nordmark@Sun.COM 
348511042SErik.Nordmark@Sun.COM 	if (ire_if->ire_ipversion == IPV4_VERSION) {
348611042SErik.Nordmark@Sun.COM 		ipaddr_t	v4addr;
348711042SErik.Nordmark@Sun.COM 		ipaddr_t	mask = IP_HOST_MASK;
348811042SErik.Nordmark@Sun.COM 
348911042SErik.Nordmark@Sun.COM 		ASSERT(IN6_IS_ADDR_V4MAPPED(addr));
349011042SErik.Nordmark@Sun.COM 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
349111042SErik.Nordmark@Sun.COM 
349211042SErik.Nordmark@Sun.COM 		ire = ire_create(
349311042SErik.Nordmark@Sun.COM 		    (uchar_t *)&v4addr,			/* dest address */
349411042SErik.Nordmark@Sun.COM 		    (uchar_t *)&mask,			/* mask */
349511042SErik.Nordmark@Sun.COM 		    (uchar_t *)&ire_if->ire_gateway_addr,
349611042SErik.Nordmark@Sun.COM 		    IRE_IF_CLONE,			/* IRE type */
349711042SErik.Nordmark@Sun.COM 		    ire_if->ire_ill,
349811042SErik.Nordmark@Sun.COM 		    ire_if->ire_zoneid,
349911042SErik.Nordmark@Sun.COM 		    ire_if->ire_flags | RTF_HOST,
350011042SErik.Nordmark@Sun.COM 		    NULL,		/* No security attr for IRE_IF_ALL */
350111042SErik.Nordmark@Sun.COM 		    ire_if->ire_ipst);
350211042SErik.Nordmark@Sun.COM 	} else {
350311042SErik.Nordmark@Sun.COM 		ASSERT(!IN6_IS_ADDR_V4MAPPED(addr));
350411042SErik.Nordmark@Sun.COM 		ire = ire_create_v6(
350511042SErik.Nordmark@Sun.COM 		    addr,				/* dest address */
350611042SErik.Nordmark@Sun.COM 		    &ipv6_all_ones,			/* mask */
350711042SErik.Nordmark@Sun.COM 		    &ire_if->ire_gateway_addr_v6,	/* gateway addr */
350811042SErik.Nordmark@Sun.COM 		    IRE_IF_CLONE,			/* IRE type */
350911042SErik.Nordmark@Sun.COM 		    ire_if->ire_ill,
351011042SErik.Nordmark@Sun.COM 		    ire_if->ire_zoneid,
351111042SErik.Nordmark@Sun.COM 		    ire_if->ire_flags | RTF_HOST,
351211042SErik.Nordmark@Sun.COM 		    NULL,		/* No security attr for IRE_IF_ALL */
351311042SErik.Nordmark@Sun.COM 		    ire_if->ire_ipst);
351411042SErik.Nordmark@Sun.COM 	}
351511042SErik.Nordmark@Sun.COM 	if (ire == NULL)
351611042SErik.Nordmark@Sun.COM 		return (NULL);
351711042SErik.Nordmark@Sun.COM 
351811042SErik.Nordmark@Sun.COM 	/* Take the metrics, in particular the mtu, from the IRE_IF */
351911042SErik.Nordmark@Sun.COM 	ire->ire_metrics = ire_if->ire_metrics;
352011042SErik.Nordmark@Sun.COM 
352111042SErik.Nordmark@Sun.COM 	nire = ire_add(ire);
352211042SErik.Nordmark@Sun.COM 	if (nire == NULL) /* Some failure */
352311042SErik.Nordmark@Sun.COM 		return (NULL);
352411042SErik.Nordmark@Sun.COM 
352511042SErik.Nordmark@Sun.COM 	if (generationp != NULL)
352611042SErik.Nordmark@Sun.COM 		*generationp = nire->ire_generation;
352711042SErik.Nordmark@Sun.COM 
352811042SErik.Nordmark@Sun.COM 	return (nire);
35297880SJonathan.Anderson@Sun.COM }
353011042SErik.Nordmark@Sun.COM 
353111042SErik.Nordmark@Sun.COM /*
353211042SErik.Nordmark@Sun.COM  * The argument is an IRE_INTERFACE. Delete all of IRE_IF_CLONE in the
353311042SErik.Nordmark@Sun.COM  * ire_dep_children (just walk the ire_dep_sib_next since they are all
353411042SErik.Nordmark@Sun.COM  * immediate children.)
353511042SErik.Nordmark@Sun.COM  * Since we hold a lock while we remove them we need to defer the actual
353611042SErik.Nordmark@Sun.COM  * calls to ire_delete() until we have dropped the lock. This makes things
353711042SErik.Nordmark@Sun.COM  * less efficient since we restart at the top after dropping the lock. But
353811042SErik.Nordmark@Sun.COM  * we only run when an IRE_INTERFACE is deleted which is infrquent.
353911042SErik.Nordmark@Sun.COM  *
354011042SErik.Nordmark@Sun.COM  * Note that ire_dep_children can be any mixture of offlink routes and
354111042SErik.Nordmark@Sun.COM  * IRE_IF_CLONE entries.
354211042SErik.Nordmark@Sun.COM  */
354311042SErik.Nordmark@Sun.COM void
ire_dep_delete_if_clone(ire_t * parent)354411042SErik.Nordmark@Sun.COM ire_dep_delete_if_clone(ire_t *parent)
354511042SErik.Nordmark@Sun.COM {
354611042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = parent->ire_ipst;
354711042SErik.Nordmark@Sun.COM 	ire_t		*child, *next;
354811042SErik.Nordmark@Sun.COM 
354911042SErik.Nordmark@Sun.COM restart:
355011042SErik.Nordmark@Sun.COM 	rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
355111042SErik.Nordmark@Sun.COM 	if (parent->ire_dep_children == NULL) {
355211042SErik.Nordmark@Sun.COM 		rw_exit(&ipst->ips_ire_dep_lock);
355311042SErik.Nordmark@Sun.COM 		return;
355411042SErik.Nordmark@Sun.COM 	}
355511042SErik.Nordmark@Sun.COM 	child = parent->ire_dep_children;
355611042SErik.Nordmark@Sun.COM 	while (child != NULL) {
355711042SErik.Nordmark@Sun.COM 		next = child->ire_dep_sib_next;
355811042SErik.Nordmark@Sun.COM 		if ((child->ire_type & IRE_IF_CLONE) &&
355911042SErik.Nordmark@Sun.COM 		    !IRE_IS_CONDEMNED(child)) {
356011042SErik.Nordmark@Sun.COM 			ire_refhold(child);
356111042SErik.Nordmark@Sun.COM 			rw_exit(&ipst->ips_ire_dep_lock);
356211042SErik.Nordmark@Sun.COM 			ire_delete(child);
356311042SErik.Nordmark@Sun.COM 			ASSERT(IRE_IS_CONDEMNED(child));
356411042SErik.Nordmark@Sun.COM 			ire_refrele(child);
356511042SErik.Nordmark@Sun.COM 			goto restart;
356611042SErik.Nordmark@Sun.COM 		}
356711042SErik.Nordmark@Sun.COM 		child = next;
356811042SErik.Nordmark@Sun.COM 	}
356911042SErik.Nordmark@Sun.COM 	rw_exit(&ipst->ips_ire_dep_lock);
357011042SErik.Nordmark@Sun.COM }
357111042SErik.Nordmark@Sun.COM 
357211042SErik.Nordmark@Sun.COM /*
357311681SSowmini.Varadhan@Sun.COM  * In the preferred/strict src multihoming modes, unbound routes (i.e.,
357411681SSowmini.Varadhan@Sun.COM  * ire_t entries with ire_unbound set to B_TRUE) are bound to an interface
357511681SSowmini.Varadhan@Sun.COM  * by selecting the first available interface that has an interface route for
357611681SSowmini.Varadhan@Sun.COM  * the ire_gateway. If that interface is subsequently brought down, ill_downi()
357711681SSowmini.Varadhan@Sun.COM  * will call ire_rebind() so that the unbound route can be bound to some other
357811681SSowmini.Varadhan@Sun.COM  * matching interface thereby preserving the intended reachability information
357911681SSowmini.Varadhan@Sun.COM  * from the original unbound route.
358011681SSowmini.Varadhan@Sun.COM  */
358111681SSowmini.Varadhan@Sun.COM void
ire_rebind(ire_t * ire)358211681SSowmini.Varadhan@Sun.COM ire_rebind(ire_t *ire)
358311681SSowmini.Varadhan@Sun.COM {
358411681SSowmini.Varadhan@Sun.COM 	ire_t	*gw_ire, *new_ire;
358511681SSowmini.Varadhan@Sun.COM 	int	match_flags = MATCH_IRE_TYPE;
358611681SSowmini.Varadhan@Sun.COM 	ill_t	*gw_ill;
358711681SSowmini.Varadhan@Sun.COM 	boolean_t isv6 = (ire->ire_ipversion == IPV6_VERSION);
358811681SSowmini.Varadhan@Sun.COM 	ip_stack_t *ipst = ire->ire_ipst;
358911681SSowmini.Varadhan@Sun.COM 
359011681SSowmini.Varadhan@Sun.COM 	ASSERT(ire->ire_unbound);
359111681SSowmini.Varadhan@Sun.COM again:
359211681SSowmini.Varadhan@Sun.COM 	if (isv6) {
359311681SSowmini.Varadhan@Sun.COM 		gw_ire = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0,
359411681SSowmini.Varadhan@Sun.COM 		    IRE_INTERFACE, NULL, ALL_ZONES, NULL, match_flags, 0,
359511681SSowmini.Varadhan@Sun.COM 		    ipst, NULL);
359611681SSowmini.Varadhan@Sun.COM 	} else {
359711681SSowmini.Varadhan@Sun.COM 		gw_ire = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
359811681SSowmini.Varadhan@Sun.COM 		    IRE_INTERFACE, NULL, ALL_ZONES, NULL, match_flags, 0,
359911681SSowmini.Varadhan@Sun.COM 		    ipst, NULL);
360011681SSowmini.Varadhan@Sun.COM 	}
360111681SSowmini.Varadhan@Sun.COM 	if (gw_ire == NULL) {
360211681SSowmini.Varadhan@Sun.COM 		/* see comments in ip_rt_add[_v6]() for IPMP */
360311681SSowmini.Varadhan@Sun.COM 		if (match_flags & MATCH_IRE_TESTHIDDEN)
360411681SSowmini.Varadhan@Sun.COM 			return;
360511681SSowmini.Varadhan@Sun.COM 
360611681SSowmini.Varadhan@Sun.COM 		match_flags |= MATCH_IRE_TESTHIDDEN;
360711681SSowmini.Varadhan@Sun.COM 		goto again;
360811681SSowmini.Varadhan@Sun.COM 	}
360911681SSowmini.Varadhan@Sun.COM 	gw_ill = gw_ire->ire_ill;
361011681SSowmini.Varadhan@Sun.COM 	if (isv6) {
361111681SSowmini.Varadhan@Sun.COM 		new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
361211681SSowmini.Varadhan@Sun.COM 		    &ire->ire_gateway_addr_v6, ire->ire_type, gw_ill,
361311681SSowmini.Varadhan@Sun.COM 		    ire->ire_zoneid, ire->ire_flags, NULL, ipst);
361411681SSowmini.Varadhan@Sun.COM 	} else {
361511681SSowmini.Varadhan@Sun.COM 		new_ire = ire_create((uchar_t *)&ire->ire_addr,
361611681SSowmini.Varadhan@Sun.COM 		    (uchar_t *)&ire->ire_mask,
361711681SSowmini.Varadhan@Sun.COM 		    (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, gw_ill,
361811681SSowmini.Varadhan@Sun.COM 		    ire->ire_zoneid, ire->ire_flags, NULL, ipst);
361911681SSowmini.Varadhan@Sun.COM 	}
362011681SSowmini.Varadhan@Sun.COM 	ire_refrele(gw_ire);
362111681SSowmini.Varadhan@Sun.COM 	if (new_ire == NULL)
362211681SSowmini.Varadhan@Sun.COM 		return;
362311681SSowmini.Varadhan@Sun.COM 	new_ire->ire_unbound = B_TRUE;
362411681SSowmini.Varadhan@Sun.COM 	new_ire = ire_add(new_ire);
362511681SSowmini.Varadhan@Sun.COM 	if (new_ire != NULL)
362611681SSowmini.Varadhan@Sun.COM 		ire_refrele(new_ire);
362711681SSowmini.Varadhan@Sun.COM }
3628