xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision 5c0b7edee9bd9fad49038456b16972ff28fa4187)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22107875b0Ssangeeta  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * This file contains routines that manipulate Internet Routing Entries (IREs).
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stream.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
3745916cd2Sjpk #include <sys/strsun.h>
387c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/policy.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/socket.h>
467c478bd9Sstevel@tonic-gate #include <net/if.h>
477c478bd9Sstevel@tonic-gate #include <net/route.h>
487c478bd9Sstevel@tonic-gate #include <netinet/in.h>
497c478bd9Sstevel@tonic-gate #include <net/if_dl.h>
507c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
517c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <inet/common.h>
547c478bd9Sstevel@tonic-gate #include <inet/mi.h>
557c478bd9Sstevel@tonic-gate #include <inet/ip.h>
567c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
577c478bd9Sstevel@tonic-gate #include <inet/ip_ndp.h>
58c793af95Ssangeeta #include <inet/arp.h>
597c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
607c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
61c793af95Ssangeeta #include <inet/ip_ftable.h>
627c478bd9Sstevel@tonic-gate #include <inet/ip_rts.h>
637c478bd9Sstevel@tonic-gate #include <inet/nd.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
667c478bd9Sstevel@tonic-gate #include <inet/ipsec_info.h>
677c478bd9Sstevel@tonic-gate #include <inet/sadb.h>
687c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
697c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
707c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
717c478bd9Sstevel@tonic-gate #include <sys/zone.h>
72f4b3ec61Sdh155122 #include <sys/cpuvar.h>
73f4b3ec61Sdh155122 
7445916cd2Sjpk #include <sys/tsol/label.h>
7545916cd2Sjpk #include <sys/tsol/tnet.h>
76c0861880Ssangeeta #include <sys/dlpi.h>
7745916cd2Sjpk 
78c793af95Ssangeeta struct kmem_cache *rt_entry_cache;
79c793af95Ssangeeta 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Synchronization notes:
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * The fields of the ire_t struct are protected in the following way :
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  * ire_next/ire_ptpn
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  *	- bucket lock of the respective tables (cache or forwarding tables).
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  * ire_mp, ire_rfq, ire_stq, ire_u *except* ire_gateway_addr[v6], ire_mask,
907c478bd9Sstevel@tonic-gate  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags, ire_ipif,
917c478bd9Sstevel@tonic-gate  * ire_ihandle, ire_phandle, ire_nce, ire_bucket, ire_in_ill, ire_in_src_addr
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
947c478bd9Sstevel@tonic-gate  *	  we don't need a lock whenever these fields are accessed.
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
977c478bd9Sstevel@tonic-gate  *        ire_add_v4/ire_add_v6 before inserting in the bucket and never
987c478bd9Sstevel@tonic-gate  *        changes after that. Thus we don't need a lock whenever these
997c478bd9Sstevel@tonic-gate  *	  fields are accessed.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  * ire_gateway_addr_v4[v6]
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
1047c478bd9Sstevel@tonic-gate  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
1057c478bd9Sstevel@tonic-gate  *	  it assumed to be atomic and hence the other parts of the code
1067c478bd9Sstevel@tonic-gate  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
1077c478bd9Sstevel@tonic-gate  *	  and hence any access to it uses ire_lock to get/set the right value.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * ire_ident, ire_refcnt
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *	- Updated atomically using atomic_add_32
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
1167c478bd9Sstevel@tonic-gate  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * ire_max_frag, ire_frag_flag
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  *	- ire_lock is used to set/read both of them together.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * ire_tire_mark
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  *	- Set in ire_create and updated in ire_expire, which is called
1257c478bd9Sstevel@tonic-gate  *	  by only one function namely ip_trash_timer_expire. Thus only
1267c478bd9Sstevel@tonic-gate  *	  one function updates and examines the value.
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  * ire_marks
1297c478bd9Sstevel@tonic-gate  *	- bucket lock protects this.
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * ire_ipsec_overhead/ire_ll_hdr_length
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  *	- Place holder for returning the information to the upper layers
1347c478bd9Sstevel@tonic-gate  *	  when IRE_DB_REQ comes down.
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * ipv6_ire_default_count is protected by the bucket lock of
1387c478bd9Sstevel@tonic-gate  * ip_forwarding_table_v6[0][0].
1397c478bd9Sstevel@tonic-gate  *
140c793af95Ssangeeta  * ipv6_ire_default_index is not protected as it  is just a hint
141c793af95Ssangeeta  * at which default gateway to use. There is nothing
1427c478bd9Sstevel@tonic-gate  * wrong in using the same gateway for two different connections.
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * As we always hold the bucket locks in all the places while accessing
1457c478bd9Sstevel@tonic-gate  * the above values, it is natural to use them for protecting them.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * We have a separate cache table and forwarding table for IPv4 and IPv6.
1487c478bd9Sstevel@tonic-gate  * Cache table (ip_cache_table/ip_cache_table_v6) is a pointer to an
1497c478bd9Sstevel@tonic-gate  * array of irb_t structure and forwarding table (ip_forwarding_table/
1507c478bd9Sstevel@tonic-gate  * ip_forwarding_table_v6) is an array of pointers to array of irb_t
151f4b3ec61Sdh155122  * structure. ip_forwarding_table_v6 is allocated dynamically in
152f4b3ec61Sdh155122  * ire_add_v6. ire_ft_init_lock is used to serialize multiple threads
1537c478bd9Sstevel@tonic-gate  * initializing the same bucket. Once a bucket is initialized, it is never
154f4b3ec61Sdh155122  * de-alloacted. This assumption enables us to access
155f4b3ec61Sdh155122  * ip_forwarding_table_v6[i] without any locks.
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  * Each irb_t - ire bucket structure has a lock to protect
1587c478bd9Sstevel@tonic-gate  * a bucket and the ires residing in the bucket have a back pointer to
1597c478bd9Sstevel@tonic-gate  * the bucket structure. It also has a reference count for the number
1607c478bd9Sstevel@tonic-gate  * of threads walking the bucket - irb_refcnt which is bumped up
1617c478bd9Sstevel@tonic-gate  * using the macro IRB_REFHOLD macro. The flags irb_flags can be
1627c478bd9Sstevel@tonic-gate  * set to IRE_MARK_CONDEMNED indicating that there are some ires
1637c478bd9Sstevel@tonic-gate  * in this bucket that are marked with IRE_MARK_CONDEMNED and the
1647c478bd9Sstevel@tonic-gate  * last thread to leave the bucket should delete the ires. Usually
1657c478bd9Sstevel@tonic-gate  * this is done by the IRB_REFRELE macro which is used to decrement
1667c478bd9Sstevel@tonic-gate  * the reference count on a bucket.
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  * IRE_REFHOLD/IRE_REFRELE macros operate on the ire which increments/
1697c478bd9Sstevel@tonic-gate  * decrements the reference count, ire_refcnt, atomically on the ire.
1707c478bd9Sstevel@tonic-gate  * ire_refcnt is modified only using this macro. Operations on the IRE
1717c478bd9Sstevel@tonic-gate  * could be described as follows :
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * CREATE an ire with reference count initialized to 1.
1747c478bd9Sstevel@tonic-gate  *
1757c478bd9Sstevel@tonic-gate  * ADDITION of an ire holds the bucket lock, checks for duplicates
1767c478bd9Sstevel@tonic-gate  * and then adds the ire. ire_add_v4/ire_add_v6 returns the ire after
1777c478bd9Sstevel@tonic-gate  * bumping up once more i.e the reference count is 2. This is to avoid
1787c478bd9Sstevel@tonic-gate  * an extra lookup in the functions calling ire_add which wants to
1797c478bd9Sstevel@tonic-gate  * work with the ire after adding.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  * LOOKUP of an ire bumps up the reference count using IRE_REFHOLD
1827c478bd9Sstevel@tonic-gate  * macro. It is valid to bump up the referece count of the IRE,
1837c478bd9Sstevel@tonic-gate  * after the lookup has returned an ire. Following are the lookup
1847c478bd9Sstevel@tonic-gate  * functions that return an HELD ire :
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * ire_lookup_local[_v6], ire_ctable_lookup[_v6], ire_ftable_lookup[_v6],
1877c478bd9Sstevel@tonic-gate  * ire_cache_lookup[_v6], ire_lookup_multi[_v6], ire_route_lookup[_v6],
188*5c0b7edeSseb  * ipif_to_ire[_v6].
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * DELETION of an ire holds the bucket lock, removes it from the list
1917c478bd9Sstevel@tonic-gate  * and then decrements the reference count for having removed from the list
1927c478bd9Sstevel@tonic-gate  * by using the IRE_REFRELE macro. If some other thread has looked up
1937c478bd9Sstevel@tonic-gate  * the ire, the reference count would have been bumped up and hence
1947c478bd9Sstevel@tonic-gate  * this ire will not be freed once deleted. It will be freed once the
1957c478bd9Sstevel@tonic-gate  * reference count drops to zero.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
1987c478bd9Sstevel@tonic-gate  * lookups acquire the bucket lock as RW_READER.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * NOTE : The only functions that does the IRE_REFRELE when an ire is
2017c478bd9Sstevel@tonic-gate  *	  passed as an argument are :
2027c478bd9Sstevel@tonic-gate  *
2037c478bd9Sstevel@tonic-gate  *	  1) ip_wput_ire : This is because it IRE_REFHOLD/RELEs the
2047c478bd9Sstevel@tonic-gate  *			   broadcast ires it looks up internally within
2057c478bd9Sstevel@tonic-gate  *			   the function. Currently, for simplicity it does
2067c478bd9Sstevel@tonic-gate  *			   not differentiate the one that is passed in and
2077c478bd9Sstevel@tonic-gate  *			   the ones it looks up internally. It always
2087c478bd9Sstevel@tonic-gate  *			   IRE_REFRELEs.
2097c478bd9Sstevel@tonic-gate  *	  2) ire_send
2107c478bd9Sstevel@tonic-gate  *	     ire_send_v6 : As ire_send calls ip_wput_ire and other functions
2117c478bd9Sstevel@tonic-gate  *			   that take ire as an argument, it has to selectively
2127c478bd9Sstevel@tonic-gate  *			   IRE_REFRELE the ire. To maintain symmetry,
2137c478bd9Sstevel@tonic-gate  *			   ire_send_v6 does the same.
2147c478bd9Sstevel@tonic-gate  *
2157c478bd9Sstevel@tonic-gate  * Otherwise, the general rule is to do the IRE_REFRELE in the function
2167c478bd9Sstevel@tonic-gate  * that is passing the ire as an argument.
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * In trying to locate ires the following points are to be noted.
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  * IRE_MARK_CONDEMNED signifies that the ire has been logically deleted and is
2217c478bd9Sstevel@tonic-gate  * to be ignored when walking the ires using ire_next.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * IRE_MARK_HIDDEN signifies that the ire is a special ire typically for the
2247c478bd9Sstevel@tonic-gate  * benefit of in.mpathd which needs to probe interfaces for failures. Normal
2257c478bd9Sstevel@tonic-gate  * applications should not be seeing this ire and hence this ire is ignored
2267c478bd9Sstevel@tonic-gate  * in most cases in the search using ire_next.
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * Zones note:
2297c478bd9Sstevel@tonic-gate  *	Walking IREs within a given zone also walks certain ires in other
2307c478bd9Sstevel@tonic-gate  *	zones.  This is done intentionally.  IRE walks with a specified
2317c478bd9Sstevel@tonic-gate  *	zoneid are used only when doing informational reports, and
2327c478bd9Sstevel@tonic-gate  *	zone users want to see things that they can access. See block
2337c478bd9Sstevel@tonic-gate  *	comment in ire_walk_ill_match().
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * The minimum size of IRE cache table.  It will be recalcuated in
2387c478bd9Sstevel@tonic-gate  * ip_ire_init().
239f4b3ec61Sdh155122  * Setable in /etc/system
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate uint32_t ip_cache_table_size = IP_CACHE_TABLE_SIZE;
2427c478bd9Sstevel@tonic-gate uint32_t ip6_cache_table_size = IP6_CACHE_TABLE_SIZE;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * The size of the forwarding table.  We will make sure that it is a
2467c478bd9Sstevel@tonic-gate  * power of 2 in ip_ire_init().
247f4b3ec61Sdh155122  * Setable in /etc/system
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate struct	kmem_cache	*ire_cache;
2527c478bd9Sstevel@tonic-gate static ire_t	ire_null;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * The threshold number of IRE in a bucket when the IREs are
2567c478bd9Sstevel@tonic-gate  * cleaned up.  This threshold is calculated later in ip_open()
2577c478bd9Sstevel@tonic-gate  * based on the speed of CPU and available memory.  This default
2587c478bd9Sstevel@tonic-gate  * value is the maximum.
2597c478bd9Sstevel@tonic-gate  *
2607c478bd9Sstevel@tonic-gate  * We have two kinds of cached IRE, temporary and
2617c478bd9Sstevel@tonic-gate  * non-temporary.  Temporary IREs are marked with
2627c478bd9Sstevel@tonic-gate  * IRE_MARK_TEMPORARY.  They are IREs created for non
2637c478bd9Sstevel@tonic-gate  * TCP traffic and for forwarding purposes.  All others
2647c478bd9Sstevel@tonic-gate  * are non-temporary IREs.  We don't mark IRE created for
2657c478bd9Sstevel@tonic-gate  * TCP as temporary because TCP is stateful and there are
2667c478bd9Sstevel@tonic-gate  * info stored in the IRE which can be shared by other TCP
2677c478bd9Sstevel@tonic-gate  * connections to the same destination.  For connected
2687c478bd9Sstevel@tonic-gate  * endpoint, we also don't want to mark the IRE used as
2697c478bd9Sstevel@tonic-gate  * temporary because the same IRE will be used frequently,
2707c478bd9Sstevel@tonic-gate  * otherwise, the app should not do a connect().  We change
2717c478bd9Sstevel@tonic-gate  * the marking at ip_bind_connected_*() if necessary.
2727c478bd9Sstevel@tonic-gate  *
2737c478bd9Sstevel@tonic-gate  * We want to keep the cache IRE hash bucket length reasonably
2747c478bd9Sstevel@tonic-gate  * short, otherwise IRE lookup functions will take "forever."
2757c478bd9Sstevel@tonic-gate  * We use the "crude" function that the IRE bucket
2767c478bd9Sstevel@tonic-gate  * length should be based on the CPU speed, which is 1 entry
2777c478bd9Sstevel@tonic-gate  * per x MHz, depending on the shift factor ip_ire_cpu_ratio
2787c478bd9Sstevel@tonic-gate  * (n).  This means that with a 750MHz CPU, the max bucket
2797c478bd9Sstevel@tonic-gate  * length can be (750 >> n) entries.
2807c478bd9Sstevel@tonic-gate  *
2817c478bd9Sstevel@tonic-gate  * Note that this threshold is separate for temp and non-temp
2827c478bd9Sstevel@tonic-gate  * IREs.  This means that the actual bucket length can be
2837c478bd9Sstevel@tonic-gate  * twice as that.  And while we try to keep temporary IRE
2847c478bd9Sstevel@tonic-gate  * length at most at the threshold value, we do not attempt to
2857c478bd9Sstevel@tonic-gate  * make the length for non-temporary IREs fixed, for the
2867c478bd9Sstevel@tonic-gate  * reason stated above.  Instead, we start trying to find
2877c478bd9Sstevel@tonic-gate  * "unused" non-temporary IREs when the bucket length reaches
2887c478bd9Sstevel@tonic-gate  * this threshold and clean them up.
2897c478bd9Sstevel@tonic-gate  *
2907c478bd9Sstevel@tonic-gate  * We also want to limit the amount of memory used by
2917c478bd9Sstevel@tonic-gate  * IREs.  So if we are allowed to use ~3% of memory (M)
2927c478bd9Sstevel@tonic-gate  * for those IREs, each bucket should not have more than
2937c478bd9Sstevel@tonic-gate  *
2947c478bd9Sstevel@tonic-gate  * 	M / num of cache bucket / sizeof (ire_t)
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * Again the above memory uses are separate for temp and
2977c478bd9Sstevel@tonic-gate  * non-temp cached IREs.
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * We may also want the limit to be a function of the number
3007c478bd9Sstevel@tonic-gate  * of interfaces and number of CPUs.  Doing the initialization
3017c478bd9Sstevel@tonic-gate  * in ip_open() means that every time an interface is plumbed,
3027c478bd9Sstevel@tonic-gate  * the max is re-calculated.  Right now, we don't do anything
3037c478bd9Sstevel@tonic-gate  * different.  In future, when we have more experience, we
3047c478bd9Sstevel@tonic-gate  * may want to change this behavior.
3057c478bd9Sstevel@tonic-gate  */
306f4b3ec61Sdh155122 uint32_t ip_ire_max_bucket_cnt = 10;	/* Setable in /etc/system */
3077c478bd9Sstevel@tonic-gate uint32_t ip6_ire_max_bucket_cnt = 10;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * The minimum of the temporary IRE bucket count.  We do not want
3117c478bd9Sstevel@tonic-gate  * the length of each bucket to be too short.  This may hurt
3127c478bd9Sstevel@tonic-gate  * performance of some apps as the temporary IREs are removed too
3137c478bd9Sstevel@tonic-gate  * often.
3147c478bd9Sstevel@tonic-gate  */
315f4b3ec61Sdh155122 uint32_t ip_ire_min_bucket_cnt = 3;	/* /etc/system - not used */
3167c478bd9Sstevel@tonic-gate uint32_t ip6_ire_min_bucket_cnt = 3;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * The ratio of memory consumed by IRE used for temporary to available
3207c478bd9Sstevel@tonic-gate  * memory.  This is a shift factor, so 6 means the ratio 1 to 64.  This
3217c478bd9Sstevel@tonic-gate  * value can be changed in /etc/system.  6 is a reasonable number.
3227c478bd9Sstevel@tonic-gate  */
323f4b3ec61Sdh155122 uint32_t ip_ire_mem_ratio = 6;	/* /etc/system */
3247c478bd9Sstevel@tonic-gate /* The shift factor for CPU speed to calculate the max IRE bucket length. */
325f4b3ec61Sdh155122 uint32_t ip_ire_cpu_ratio = 7;	/* /etc/system */
3267c478bd9Sstevel@tonic-gate 
327c793af95Ssangeeta typedef struct nce_clookup_s {
328c793af95Ssangeeta 	ipaddr_t ncecl_addr;
329c793af95Ssangeeta 	boolean_t ncecl_found;
330c793af95Ssangeeta } nce_clookup_t;
331c793af95Ssangeeta 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * The maximum number of buckets in IRE cache table.  In future, we may
3347c478bd9Sstevel@tonic-gate  * want to make it a dynamic hash table.  For the moment, we fix the
3357c478bd9Sstevel@tonic-gate  * size and allocate the table in ip_ire_init() when IP is first loaded.
3367c478bd9Sstevel@tonic-gate  * We take into account the amount of memory a system has.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate #define	IP_MAX_CACHE_TABLE_SIZE	4096
3397c478bd9Sstevel@tonic-gate 
340f4b3ec61Sdh155122 /* Setable in /etc/system */
3417c478bd9Sstevel@tonic-gate static uint32_t	ip_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
3427c478bd9Sstevel@tonic-gate static uint32_t	ip6_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
3437c478bd9Sstevel@tonic-gate 
344*5c0b7edeSseb #define	NUM_ILLS	2	/* To build the ILL list to unlock */
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /* Zero iulp_t for initialization. */
3477c478bd9Sstevel@tonic-gate const iulp_t	ire_uinfo_null = { 0 };
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static int	ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
350c793af95Ssangeeta     ipsq_func_t func, boolean_t);
3517c478bd9Sstevel@tonic-gate static void	ire_delete_v4(ire_t *ire);
3527c478bd9Sstevel@tonic-gate static void	ire_report_ctable(ire_t *ire, char *mp);
35345916cd2Sjpk static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
354f4b3ec61Sdh155122     zoneid_t zoneid, ip_stack_t *);
3557c478bd9Sstevel@tonic-gate static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
35645916cd2Sjpk     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
3577c478bd9Sstevel@tonic-gate static void	ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt);
3587c478bd9Sstevel@tonic-gate extern void	ill_unlock_ills(ill_t **list, int cnt);
359c793af95Ssangeeta static	void	ip_nce_clookup_and_delete(nce_t *nce, void *arg);
3607c478bd9Sstevel@tonic-gate extern void	th_trace_rrecord(th_trace_t *);
3617c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
3627c478bd9Sstevel@tonic-gate static void	ire_trace_inactive(ire_t *);
3637c478bd9Sstevel@tonic-gate #endif
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * To avoid bloating the code, we call this function instead of
3677c478bd9Sstevel@tonic-gate  * using the macro IRE_REFRELE. Use macro only in performance
3687c478bd9Sstevel@tonic-gate  * critical paths.
3697c478bd9Sstevel@tonic-gate  *
3707c478bd9Sstevel@tonic-gate  * Must not be called while holding any locks. Otherwise if this is
3717c478bd9Sstevel@tonic-gate  * the last reference to be released there is a chance of recursive mutex
3727c478bd9Sstevel@tonic-gate  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
3737c478bd9Sstevel@tonic-gate  * to restart an ioctl. The one exception is when the caller is sure that
3747c478bd9Sstevel@tonic-gate  * this is not the last reference to be released. Eg. if the caller is
3757c478bd9Sstevel@tonic-gate  * sure that the ire has not been deleted and won't be deleted.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate void
3787c478bd9Sstevel@tonic-gate ire_refrele(ire_t *ire)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	IRE_REFRELE(ire);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate void
3847c478bd9Sstevel@tonic-gate ire_refrele_notr(ire_t *ire)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * kmem_cache_alloc constructor for IRE in kma space.
3917c478bd9Sstevel@tonic-gate  * Note that when ire_mp is set the IRE is stored in that mblk and
3927c478bd9Sstevel@tonic-gate  * not in this cache.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate /* ARGSUSED */
3957c478bd9Sstevel@tonic-gate static int
3967c478bd9Sstevel@tonic-gate ip_ire_constructor(void *buf, void *cdrarg, int kmflags)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
3997c478bd9Sstevel@tonic-gate 
400c793af95Ssangeeta 	ire->ire_nce = NULL;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	return (0);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
4067c478bd9Sstevel@tonic-gate static void
4077c478bd9Sstevel@tonic-gate ip_ire_destructor(void *buf, void *cdrarg)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
4107c478bd9Sstevel@tonic-gate 
411c793af95Ssangeeta 	ASSERT(ire->ire_nce == NULL);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_ADVISE_NO_REPLY
4167c478bd9Sstevel@tonic-gate  * IOCTL.  It is used by TCP (or other ULPs) to supply revised information
4177c478bd9Sstevel@tonic-gate  * for an existing CACHED IRE.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate /* ARGSUSED */
4207c478bd9Sstevel@tonic-gate int
4217c478bd9Sstevel@tonic-gate ip_ire_advise(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	uchar_t	*addr_ucp;
4247c478bd9Sstevel@tonic-gate 	ipic_t	*ipic;
4257c478bd9Sstevel@tonic-gate 	ire_t	*ire;
4267c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
4277c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
4287c478bd9Sstevel@tonic-gate 	irb_t	*irb;
4297c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
430f4b3ec61Sdh155122 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
4337c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
4377c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
4387c478bd9Sstevel@tonic-gate 	 */
439f4b3ec61Sdh155122 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
4407c478bd9Sstevel@tonic-gate 		return (EPERM);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	ipic = (ipic_t *)mp->b_rptr;
4437c478bd9Sstevel@tonic-gate 	if (!(addr_ucp = mi_offset_param(mp, ipic->ipic_addr_offset,
4447c478bd9Sstevel@tonic-gate 	    ipic->ipic_addr_length))) {
4457c478bd9Sstevel@tonic-gate 		return (EINVAL);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	if (!OK_32PTR(addr_ucp))
4487c478bd9Sstevel@tonic-gate 		return (EINVAL);
4497c478bd9Sstevel@tonic-gate 	switch (ipic->ipic_addr_length) {
4507c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN: {
4517c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
4527c478bd9Sstevel@tonic-gate 		addr = *(ipaddr_t *)addr_ucp;
4537c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
454f4b3ec61Sdh155122 		ire = ire_cache_lookup(addr, zoneid, NULL, ipst);
4557c478bd9Sstevel@tonic-gate 		break;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	case IPV6_ADDR_LEN: {
4587c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
4597c478bd9Sstevel@tonic-gate 		v6addr = *(in6_addr_t *)addr_ucp;
4607c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
461f4b3ec61Sdh155122 		ire = ire_cache_lookup_v6(&v6addr, zoneid, NULL, ipst);
4627c478bd9Sstevel@tonic-gate 		break;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	default:
4657c478bd9Sstevel@tonic-gate 		return (EINVAL);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (ire == NULL)
4697c478bd9Sstevel@tonic-gate 		return (ENOENT);
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Update the round trip time estimate and/or the max frag size
4727c478bd9Sstevel@tonic-gate 	 * and/or the slow start threshold.
4737c478bd9Sstevel@tonic-gate 	 *
4747c478bd9Sstevel@tonic-gate 	 * We serialize multiple advises using ire_lock.
4757c478bd9Sstevel@tonic-gate 	 */
4767c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
4777c478bd9Sstevel@tonic-gate 	if (ipic->ipic_rtt) {
4787c478bd9Sstevel@tonic-gate 		/*
4797c478bd9Sstevel@tonic-gate 		 * If there is no old cached values, initialize them
4807c478bd9Sstevel@tonic-gate 		 * conservatively.  Set them to be (1.5 * new value).
4817c478bd9Sstevel@tonic-gate 		 */
4827c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt != 0) {
4837c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
4847c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt) >> 1;
4857c478bd9Sstevel@tonic-gate 		} else {
4867c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = ipic->ipic_rtt +
4877c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt >> 1);
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
4907c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd =
4917c478bd9Sstevel@tonic-gate 			    (ire->ire_uinfo.iulp_rtt_sd +
4927c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt_sd) >> 1;
4937c478bd9Sstevel@tonic-gate 		} else {
4947c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd = ipic->ipic_rtt_sd +
4957c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt_sd >> 1);
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	if (ipic->ipic_max_frag)
4997c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = MIN(ipic->ipic_max_frag, IP_MAXPACKET);
5007c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ssthresh != 0) {
5017c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_ssthresh != 0)
5027c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh =
5037c478bd9Sstevel@tonic-gate 			    (ipic->ipic_ssthresh +
5047c478bd9Sstevel@tonic-gate 			    ire->ire_uinfo.iulp_ssthresh) >> 1;
5057c478bd9Sstevel@tonic-gate 		else
5067c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh = ipic->ipic_ssthresh;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Don't need the ire_lock below this. ire_type does not change
5107c478bd9Sstevel@tonic-gate 	 * after initialization. ire_marks is protected by irb_lock.
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ire_marks != 0 && ire->ire_type == IRE_CACHE) {
5157c478bd9Sstevel@tonic-gate 		/*
5167c478bd9Sstevel@tonic-gate 		 * Only increment the temporary IRE count if the original
5177c478bd9Sstevel@tonic-gate 		 * IRE is not already marked temporary.
5187c478bd9Sstevel@tonic-gate 		 */
5197c478bd9Sstevel@tonic-gate 		irb = ire->ire_bucket;
5207c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_WRITER);
5217c478bd9Sstevel@tonic-gate 		if ((ipic->ipic_ire_marks & IRE_MARK_TEMPORARY) &&
5227c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_TEMPORARY)) {
5237c478bd9Sstevel@tonic-gate 			irb->irb_tmp_ire_cnt++;
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 		ire->ire_marks |= ipic->ipic_ire_marks;
5267c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
5307c478bd9Sstevel@tonic-gate 	return (0);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
5357c478bd9Sstevel@tonic-gate  * IOCTL[s].  The NO_REPLY form is used by TCP to delete a route IRE
5367c478bd9Sstevel@tonic-gate  * for a host that is not responding.  This will force an attempt to
537dc041e83Scarlsonj  * establish a new route, if available, and flush out the ARP entry so
538dc041e83Scarlsonj  * it will re-resolve.  Management processes may want to use the
539dc041e83Scarlsonj  * version that generates a reply.
5407c478bd9Sstevel@tonic-gate  *
5417c478bd9Sstevel@tonic-gate  * This function does not support IPv6 since Neighbor Unreachability Detection
5427c478bd9Sstevel@tonic-gate  * means that negative advise like this is useless.
5437c478bd9Sstevel@tonic-gate  */
5447c478bd9Sstevel@tonic-gate /* ARGSUSED */
5457c478bd9Sstevel@tonic-gate int
5467c478bd9Sstevel@tonic-gate ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	uchar_t		*addr_ucp;
5497c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
5507c478bd9Sstevel@tonic-gate 	ire_t		*ire;
5517c478bd9Sstevel@tonic-gate 	ipid_t		*ipid;
5527c478bd9Sstevel@tonic-gate 	boolean_t	routing_sock_info = B_FALSE;	/* Sent info? */
5537c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
554c793af95Ssangeeta 	ire_t		*gire = NULL;
555dc041e83Scarlsonj 	ill_t		*ill;
556dc041e83Scarlsonj 	mblk_t		*arp_mp;
557f4b3ec61Sdh155122 	ip_stack_t	*ipst;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
5607c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
561f4b3ec61Sdh155122 	ipst = CONNQ_TO_IPST(q);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/*
5647c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
5657c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
5667c478bd9Sstevel@tonic-gate 	 */
567f4b3ec61Sdh155122 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
5687c478bd9Sstevel@tonic-gate 		return (EPERM);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	ipid = (ipid_t *)mp->b_rptr;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/* Only actions on IRE_CACHEs are acceptable at present. */
5737c478bd9Sstevel@tonic-gate 	if (ipid->ipid_ire_type != IRE_CACHE)
5747c478bd9Sstevel@tonic-gate 		return (EINVAL);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
5777c478bd9Sstevel@tonic-gate 	    ipid->ipid_addr_length);
5787c478bd9Sstevel@tonic-gate 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
5797c478bd9Sstevel@tonic-gate 		return (EINVAL);
5807c478bd9Sstevel@tonic-gate 	switch (ipid->ipid_addr_length) {
5817c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN:
5827c478bd9Sstevel@tonic-gate 		/* addr_ucp points at IP addr */
5837c478bd9Sstevel@tonic-gate 		break;
5847c478bd9Sstevel@tonic-gate 	case sizeof (sin_t): {
5857c478bd9Sstevel@tonic-gate 		sin_t	*sin;
5867c478bd9Sstevel@tonic-gate 		/*
5877c478bd9Sstevel@tonic-gate 		 * got complete (sockaddr) address - increment addr_ucp to point
5887c478bd9Sstevel@tonic-gate 		 * at the ip_addr field.
5897c478bd9Sstevel@tonic-gate 		 */
5907c478bd9Sstevel@tonic-gate 		sin = (sin_t *)addr_ucp;
5917c478bd9Sstevel@tonic-gate 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
5927c478bd9Sstevel@tonic-gate 		break;
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	default:
5957c478bd9Sstevel@tonic-gate 		return (EINVAL);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	/* Extract the destination address. */
5987c478bd9Sstevel@tonic-gate 	bcopy(addr_ucp, &addr, IP_ADDR_LEN);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/* Try to find the CACHED IRE. */
601f4b3ec61Sdh155122 	ire = ire_cache_lookup(addr, zoneid, NULL, ipst);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/* Nail it. */
6047c478bd9Sstevel@tonic-gate 	if (ire) {
6057c478bd9Sstevel@tonic-gate 		/* Allow delete only on CACHE entries */
6067c478bd9Sstevel@tonic-gate 		if (ire->ire_type != IRE_CACHE) {
6077c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
6087c478bd9Sstevel@tonic-gate 			return (EINVAL);
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/*
6127c478bd9Sstevel@tonic-gate 		 * Verify that the IRE has been around for a while.
6137c478bd9Sstevel@tonic-gate 		 * This is to protect against transport protocols
6147c478bd9Sstevel@tonic-gate 		 * that are too eager in sending delete messages.
6157c478bd9Sstevel@tonic-gate 		 */
6167c478bd9Sstevel@tonic-gate 		if (gethrestime_sec() <
617f4b3ec61Sdh155122 		    ire->ire_create_time + ipst->ips_ip_ignore_delete_time) {
6187c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
6197c478bd9Sstevel@tonic-gate 			return (EINVAL);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * Now we have a potentially dead cache entry. We need
6237c478bd9Sstevel@tonic-gate 		 * to remove it.
624c793af95Ssangeeta 		 * If this cache entry is generated from a
625c793af95Ssangeeta 		 * default route (i.e., ire_cmask == 0),
6267c478bd9Sstevel@tonic-gate 		 * search the default list and mark it dead and some
6277c478bd9Sstevel@tonic-gate 		 * background process will try to activate it.
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		if ((ire->ire_gateway_addr != 0) && (ire->ire_cmask == 0)) {
6307c478bd9Sstevel@tonic-gate 			/*
6317c478bd9Sstevel@tonic-gate 			 * Make sure that we pick a different
6327c478bd9Sstevel@tonic-gate 			 * IRE_DEFAULT next time.
6337c478bd9Sstevel@tonic-gate 			 */
6347c478bd9Sstevel@tonic-gate 			ire_t *gw_ire;
635c793af95Ssangeeta 			irb_t *irb = NULL;
636c793af95Ssangeeta 			uint_t match_flags;
6377c478bd9Sstevel@tonic-gate 
638c793af95Ssangeeta 			match_flags = (MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE);
639c793af95Ssangeeta 
640c793af95Ssangeeta 			gire = ire_ftable_lookup(ire->ire_addr,
641c793af95Ssangeeta 			    ire->ire_cmask, 0, 0,
642f4b3ec61Sdh155122 			    ire->ire_ipif, NULL, zoneid, 0, NULL, match_flags,
643f4b3ec61Sdh155122 			    ipst);
644c793af95Ssangeeta 
645c793af95Ssangeeta 			ip3dbg(("ire_ftable_lookup() returned gire %p\n",
646c793af95Ssangeeta 			    (void *)gire));
647c793af95Ssangeeta 
648c793af95Ssangeeta 			if (gire != NULL) {
649c793af95Ssangeeta 				irb = gire->ire_bucket;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 				/*
6527c478bd9Sstevel@tonic-gate 				 * We grab it as writer just to serialize
6537c478bd9Sstevel@tonic-gate 				 * multiple threads trying to bump up
654c793af95Ssangeeta 				 * irb_rr_origin
6557c478bd9Sstevel@tonic-gate 				 */
6567c478bd9Sstevel@tonic-gate 				rw_enter(&irb->irb_lock, RW_WRITER);
657c793af95Ssangeeta 				if ((gw_ire = irb->irb_rr_origin) == NULL) {
6587c478bd9Sstevel@tonic-gate 					rw_exit(&irb->irb_lock);
6597c478bd9Sstevel@tonic-gate 					goto done;
6607c478bd9Sstevel@tonic-gate 				}
661c793af95Ssangeeta 
6620ad1ccddSsowmini 				DTRACE_PROBE1(ip__ire__del__origin,
6630ad1ccddSsowmini 				    (ire_t *), gw_ire);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 				/* Skip past the potentially bad gateway */
6667c478bd9Sstevel@tonic-gate 				if (ire->ire_gateway_addr ==
6670ad1ccddSsowmini 				    gw_ire->ire_gateway_addr) {
6680ad1ccddSsowmini 					ire_t *next = gw_ire->ire_next;
6697c478bd9Sstevel@tonic-gate 
6700ad1ccddSsowmini 					DTRACE_PROBE2(ip__ire__del,
6710ad1ccddSsowmini 					    (ire_t *), gw_ire, (irb_t *), irb);
6720ad1ccddSsowmini 					IRE_FIND_NEXT_ORIGIN(next);
6730ad1ccddSsowmini 					irb->irb_rr_origin = next;
6740ad1ccddSsowmini 				}
6757c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
6767c478bd9Sstevel@tonic-gate 			}
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate done:
679c793af95Ssangeeta 		if (gire != NULL)
680c793af95Ssangeeta 			IRE_REFRELE(gire);
6817c478bd9Sstevel@tonic-gate 		/* report the bad route to routing sockets */
6827c478bd9Sstevel@tonic-gate 		ip_rts_change(RTM_LOSING, ire->ire_addr, ire->ire_gateway_addr,
6837c478bd9Sstevel@tonic-gate 		    ire->ire_mask, ire->ire_src_addr, 0, 0, 0,
684f4b3ec61Sdh155122 		    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA), ipst);
6857c478bd9Sstevel@tonic-gate 		routing_sock_info = B_TRUE;
686dc041e83Scarlsonj 
687dc041e83Scarlsonj 		/*
688dc041e83Scarlsonj 		 * TCP is really telling us to start over completely, and it
689dc041e83Scarlsonj 		 * expects that we'll resend the ARP query.  Tell ARP to
690dc041e83Scarlsonj 		 * discard the entry, if this is a local destination.
691dc041e83Scarlsonj 		 */
692dc041e83Scarlsonj 		ill = ire->ire_stq->q_ptr;
693dc041e83Scarlsonj 		if (ire->ire_gateway_addr == 0 &&
694dc041e83Scarlsonj 		    (arp_mp = ill_ared_alloc(ill, addr)) != NULL) {
695dc041e83Scarlsonj 			putnext(ill->ill_rq, arp_mp);
696dc041e83Scarlsonj 		}
697dc041e83Scarlsonj 
6987c478bd9Sstevel@tonic-gate 		ire_delete(ire);
6997c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
7007c478bd9Sstevel@tonic-gate 	}
7016bdb8e66Sdd193516 	/*
7026bdb8e66Sdd193516 	 * Also look for an IRE_HOST type redirect ire and
7036bdb8e66Sdd193516 	 * remove it if present.
7046bdb8e66Sdd193516 	 */
7056bdb8e66Sdd193516 	ire = ire_route_lookup(addr, 0, 0, IRE_HOST, NULL, NULL,
706f4b3ec61Sdh155122 	    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/* Nail it. */
7096bdb8e66Sdd193516 	if (ire != NULL) {
7106bdb8e66Sdd193516 		if (ire->ire_flags & RTF_DYNAMIC) {
7117c478bd9Sstevel@tonic-gate 			if (!routing_sock_info) {
7127c478bd9Sstevel@tonic-gate 				ip_rts_change(RTM_LOSING, ire->ire_addr,
7137c478bd9Sstevel@tonic-gate 				    ire->ire_gateway_addr, ire->ire_mask,
7147c478bd9Sstevel@tonic-gate 				    ire->ire_src_addr, 0, 0, 0,
71554da8755Ssowmini 				    (RTA_DST | RTA_GATEWAY |
71654da8755Ssowmini 				    RTA_NETMASK | RTA_IFA),
717f4b3ec61Sdh155122 				    ipst);
7187c478bd9Sstevel@tonic-gate 			}
7197c478bd9Sstevel@tonic-gate 			ire_delete(ire);
7206bdb8e66Sdd193516 		}
7217c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 	return (0);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * Named Dispatch routine to produce a formatted report on all IREs.
7287c478bd9Sstevel@tonic-gate  * This report is accessed by using the ndd utility to "get" ND variable
7297c478bd9Sstevel@tonic-gate  * "ipv4_ire_status".
7307c478bd9Sstevel@tonic-gate  */
7317c478bd9Sstevel@tonic-gate /* ARGSUSED */
7327c478bd9Sstevel@tonic-gate int
7337c478bd9Sstevel@tonic-gate ip_ire_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
736f4b3ec61Sdh155122 	ip_stack_t	*ipst;
737f4b3ec61Sdh155122 
738f4b3ec61Sdh155122 	if (CONN_Q(q))
739f4b3ec61Sdh155122 		ipst = CONNQ_TO_IPST(q);
740f4b3ec61Sdh155122 	else
741f4b3ec61Sdh155122 		ipst = ILLQ_TO_IPST(q);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
7447c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
7457c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7467c478bd9Sstevel@tonic-gate 	    "rfq      " MI_COL_HDRPAD_STR
7477c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7487c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
7497c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7507c478bd9Sstevel@tonic-gate 	    " zone "
7517c478bd9Sstevel@tonic-gate 	/*   12345 */
7527c478bd9Sstevel@tonic-gate 	    "addr            mask            "
7537c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 */
7547c478bd9Sstevel@tonic-gate 	    "src             gateway         mxfrg rtt   rtt_sd ssthresh ref "
7557c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 12345 12345 123456 12345678 123 */
7567c478bd9Sstevel@tonic-gate 	    "rtomax tstamp_ok wscale_ok ecn_ok pmtud_ok sack sendpipe "
7577c478bd9Sstevel@tonic-gate 	/*   123456 123456789 123456789 123456 12345678 1234 12345678 */
7587c478bd9Sstevel@tonic-gate 	    "recvpipe in/out/forward type");
7597c478bd9Sstevel@tonic-gate 	/*   12345678 in/out/forward xxxxxxxxxx */
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * Because of the ndd constraint, at most we can have 64K buffer
7637c478bd9Sstevel@tonic-gate 	 * to put in all IRE info.  So to be more efficient, just
7647c478bd9Sstevel@tonic-gate 	 * allocate a 64K buffer here, assuming we need that large buffer.
7657c478bd9Sstevel@tonic-gate 	 * This should be OK as only root can do ndd /dev/ip.
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
7687c478bd9Sstevel@tonic-gate 		/* The following may work even if we cannot get a large buf. */
7697c478bd9Sstevel@tonic-gate 		(void) mi_mpprintf(mp, "<< Out of buffer >>\n");
7707c478bd9Sstevel@tonic-gate 		return (0);
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7747c478bd9Sstevel@tonic-gate 	if (zoneid == GLOBAL_ZONEID)
7757c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
7767c478bd9Sstevel@tonic-gate 
777f4b3ec61Sdh155122 	ire_walk_v4(ire_report_ftable, mp->b_cont, zoneid, ipst);
778f4b3ec61Sdh155122 	ire_walk_v4(ire_report_ctable, mp->b_cont, zoneid, ipst);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate /* ire_walk routine invoked for ip_ire_report for each cached IRE. */
7857c478bd9Sstevel@tonic-gate static void
7867c478bd9Sstevel@tonic-gate ire_report_ctable(ire_t *ire, char *mp)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	char	buf1[16];
7897c478bd9Sstevel@tonic-gate 	char	buf2[16];
7907c478bd9Sstevel@tonic-gate 	char	buf3[16];
7917c478bd9Sstevel@tonic-gate 	char	buf4[16];
7927c478bd9Sstevel@tonic-gate 	uint_t	fo_pkt_count;
7937c478bd9Sstevel@tonic-gate 	uint_t	ib_pkt_count;
7947c478bd9Sstevel@tonic-gate 	int	ref;
7957c478bd9Sstevel@tonic-gate 	uint_t	print_len, buf_len;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHETABLE) == 0)
7987c478bd9Sstevel@tonic-gate 		return;
7997c478bd9Sstevel@tonic-gate 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
8007c478bd9Sstevel@tonic-gate 	if (buf_len <= 0)
8017c478bd9Sstevel@tonic-gate 		return;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
8047c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
8057c478bd9Sstevel@tonic-gate 	/* "inbound" to a non local address is a forward */
8067c478bd9Sstevel@tonic-gate 	ib_pkt_count = ire->ire_ib_pkt_count;
8077c478bd9Sstevel@tonic-gate 	fo_pkt_count = 0;
8087c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
8097c478bd9Sstevel@tonic-gate 		fo_pkt_count = ib_pkt_count;
8107c478bd9Sstevel@tonic-gate 		ib_pkt_count = 0;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	print_len =  snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
8137c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
8147c478bd9Sstevel@tonic-gate 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
8157c478bd9Sstevel@tonic-gate 	    "%04d %08d %08d %d/%d/%d %s\n",
8167c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
8177c478bd9Sstevel@tonic-gate 	    (int)ire->ire_zoneid,
8187c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
8197c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_src_addr, buf3),
8207c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
8217c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
8227c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtt_sd, ire->ire_uinfo.iulp_ssthresh, ref,
8237c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtomax,
8247c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
8257c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
8267c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
8277c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
8287c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_sack,
8297c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
8307c478bd9Sstevel@tonic-gate 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
8317c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
8327c478bd9Sstevel@tonic-gate 	if (print_len < buf_len) {
8337c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += print_len;
8347c478bd9Sstevel@tonic-gate 	} else {
8357c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += buf_len;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * ip_ire_req is called by ip_wput when an IRE_DB_REQ_TYPE message is handed
8417c478bd9Sstevel@tonic-gate  * down from the Upper Level Protocol to request a copy of the IRE (to check
8427c478bd9Sstevel@tonic-gate  * its type or to extract information like round-trip time estimates or the
8437c478bd9Sstevel@tonic-gate  * MTU.)
8447c478bd9Sstevel@tonic-gate  * The address is assumed to be in the ire_addr field. If no IRE is found
8457c478bd9Sstevel@tonic-gate  * an IRE is returned with ire_type being zero.
8467c478bd9Sstevel@tonic-gate  * Note that the upper lavel protocol has to check for broadcast
8477c478bd9Sstevel@tonic-gate  * (IRE_BROADCAST) and multicast (CLASSD(addr)).
8487c478bd9Sstevel@tonic-gate  * If there is a b_cont the resulting IRE_DB_TYPE mblk is placed at the
8497c478bd9Sstevel@tonic-gate  * end of the returned message.
8507c478bd9Sstevel@tonic-gate  *
8517c478bd9Sstevel@tonic-gate  * TCP sends down a message of this type with a connection request packet
8527c478bd9Sstevel@tonic-gate  * chained on. UDP and ICMP send it down to verify that a route exists for
8537c478bd9Sstevel@tonic-gate  * the destination address when they get connected.
8547c478bd9Sstevel@tonic-gate  */
8557c478bd9Sstevel@tonic-gate void
8567c478bd9Sstevel@tonic-gate ip_ire_req(queue_t *q, mblk_t *mp)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	ire_t	*inire;
8597c478bd9Sstevel@tonic-gate 	ire_t	*ire;
8607c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
8617c478bd9Sstevel@tonic-gate 	ire_t	*sire = NULL;
8627c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid;
863f4b3ec61Sdh155122 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
864f4b3ec61Sdh155122 
865f4b3ec61Sdh155122 	ASSERT(q->q_next == NULL);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (ire_t) ||
8687c478bd9Sstevel@tonic-gate 	    !OK_32PTR(mp->b_rptr)) {
8697c478bd9Sstevel@tonic-gate 		freemsg(mp);
8707c478bd9Sstevel@tonic-gate 		return;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	inire = (ire_t *)mp->b_rptr;
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * Got it, now take our best shot at an IRE.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 	if (inire->ire_ipversion == IPV6_VERSION) {
8777c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup_v6(&inire->ire_addr_v6, 0, 0, 0,
87845916cd2Sjpk 		    NULL, &sire, zoneid, NULL,
879f4b3ec61Sdh155122 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst);
8807c478bd9Sstevel@tonic-gate 	} else {
8817c478bd9Sstevel@tonic-gate 		ASSERT(inire->ire_ipversion == IPV4_VERSION);
8827c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(inire->ire_addr, 0, 0, 0,
88345916cd2Sjpk 		    NULL, &sire, zoneid, NULL,
884f4b3ec61Sdh155122 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst);
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	/*
8887c478bd9Sstevel@tonic-gate 	 * We prevent returning IRES with source address INADDR_ANY
8897c478bd9Sstevel@tonic-gate 	 * as these were temporarily created for sending packets
8907c478bd9Sstevel@tonic-gate 	 * from endpoints that have conn_unspec_src set.
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	if (ire == NULL ||
8937c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV4_VERSION &&
8947c478bd9Sstevel@tonic-gate 	    ire->ire_src_addr == INADDR_ANY) ||
8957c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV6_VERSION &&
8967c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6))) {
8977c478bd9Sstevel@tonic-gate 		inire->ire_type = 0;
8987c478bd9Sstevel@tonic-gate 	} else {
8997c478bd9Sstevel@tonic-gate 		bcopy(ire, inire, sizeof (ire_t));
9007c478bd9Sstevel@tonic-gate 		/* Copy the route metrics from the parent. */
9017c478bd9Sstevel@tonic-gate 		if (sire != NULL) {
9027c478bd9Sstevel@tonic-gate 			bcopy(&(sire->ire_uinfo), &(inire->ire_uinfo),
9037c478bd9Sstevel@tonic-gate 			    sizeof (iulp_t));
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		/*
9077c478bd9Sstevel@tonic-gate 		 * As we don't lookup global policy here, we may not
9087c478bd9Sstevel@tonic-gate 		 * pass the right size if per-socket policy is not
9097c478bd9Sstevel@tonic-gate 		 * present. For these cases, path mtu discovery will
9107c478bd9Sstevel@tonic-gate 		 * do the right thing.
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate 		inire->ire_ipsec_overhead = conn_ipsec_length(Q_TO_CONN(q));
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		/* Pass the latest setting of the ip_path_mtu_discovery */
915f4b3ec61Sdh155122 		inire->ire_frag_flag |=
916f4b3ec61Sdh155122 		    (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 	if (ire != NULL)
9197c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
9207c478bd9Sstevel@tonic-gate 	if (sire != NULL)
9217c478bd9Sstevel@tonic-gate 		ire_refrele(sire);
9227c478bd9Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[sizeof (ire_t)];
9237c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/* Put the IRE_DB_TYPE mblk last in the chain */
9267c478bd9Sstevel@tonic-gate 	mp1 = mp->b_cont;
9277c478bd9Sstevel@tonic-gate 	if (mp1 != NULL) {
9287c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
9297c478bd9Sstevel@tonic-gate 		linkb(mp1, mp);
9307c478bd9Sstevel@tonic-gate 		mp = mp1;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 	qreply(q, mp);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate /*
9367c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
9377c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
9387c478bd9Sstevel@tonic-gate  * the packet has been sent.
9397c478bd9Sstevel@tonic-gate  */
9407c478bd9Sstevel@tonic-gate static void
9417c478bd9Sstevel@tonic-gate ire_send(queue_t *q, mblk_t *pkt, ire_t *ire)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
9447c478bd9Sstevel@tonic-gate 	boolean_t is_secure;
9457c478bd9Sstevel@tonic-gate 	uint_t ifindex;
9467c478bd9Sstevel@tonic-gate 	ill_t	*ill;
9475597b60aSnordmark 	zoneid_t zoneid = ire->ire_zoneid;
948f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
9515597b60aSnordmark 	ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */
9527c478bd9Sstevel@tonic-gate 	ipsec_mp = pkt;
9537c478bd9Sstevel@tonic-gate 	is_secure = (pkt->b_datap->db_type == M_CTL);
9545597b60aSnordmark 	if (is_secure) {
9555597b60aSnordmark 		ipsec_out_t *io;
9565597b60aSnordmark 
9577c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
9585597b60aSnordmark 		io = (ipsec_out_t *)ipsec_mp->b_rptr;
9595597b60aSnordmark 		if (io->ipsec_out_type == IPSEC_OUT)
9605597b60aSnordmark 			zoneid = io->ipsec_out_zoneid;
9615597b60aSnordmark 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
9647c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
9657c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
9667c478bd9Sstevel@tonic-gate 		/*
9677c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_noire).
9687c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
9697c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from ARP)
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
9727c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
973f4b3ec61Sdh155122 		    NULL, NULL, NULL, NULL, ipst);
9747c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
9757c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
9767c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
9777c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
9787c478bd9Sstevel@tonic-gate 			return;
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
9817c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
9827c478bd9Sstevel@tonic-gate 		/*
9837c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
9847c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
9857c478bd9Sstevel@tonic-gate 		 * prepended.
9867c478bd9Sstevel@tonic-gate 		 */
9877c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
988c793af95Ssangeeta 		put(q, pkt);
9897c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
9907c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
9917c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
9927c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
9937c478bd9Sstevel@tonic-gate 		/*
9947c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
9957c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
9967c478bd9Sstevel@tonic-gate 		 */
9977c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
9987c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, ipsec_mp, NULL);
9997c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
10007c478bd9Sstevel@tonic-gate 	} else {
10017c478bd9Sstevel@tonic-gate 		/* Locally originated packets */
10027c478bd9Sstevel@tonic-gate 		boolean_t is_inaddr_any;
10037c478bd9Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)pkt->b_rptr;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		/*
10067c478bd9Sstevel@tonic-gate 		 * We need to do an ire_delete below for which
10077c478bd9Sstevel@tonic-gate 		 * we need to make sure that the IRE will be
10087c478bd9Sstevel@tonic-gate 		 * around even after calling ip_wput_ire -
10097c478bd9Sstevel@tonic-gate 		 * which does ire_refrele. Otherwise somebody
10107c478bd9Sstevel@tonic-gate 		 * could potentially delete this ire and hence
10117c478bd9Sstevel@tonic-gate 		 * free this ire and we will be calling ire_delete
10127c478bd9Sstevel@tonic-gate 		 * on a freed ire below.
10137c478bd9Sstevel@tonic-gate 		 */
10147c478bd9Sstevel@tonic-gate 		is_inaddr_any = (ire->ire_src_addr == INADDR_ANY);
10157c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
10167c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		/*
10197c478bd9Sstevel@tonic-gate 		 * If we were resolving a router we can not use the
10207c478bd9Sstevel@tonic-gate 		 * routers IRE for sending the packet (since it would
10217c478bd9Sstevel@tonic-gate 		 * violate the uniqness of the IP idents) thus we
10227c478bd9Sstevel@tonic-gate 		 * make another pass through ip_wput to create the IRE_CACHE
10237c478bd9Sstevel@tonic-gate 		 * for the destination.
10247c478bd9Sstevel@tonic-gate 		 * When IRE_MARK_NOADD is set, ire_add() is not called.
10257c478bd9Sstevel@tonic-gate 		 * Thus ip_wput() will never find a ire and result in an
10267c478bd9Sstevel@tonic-gate 		 * infinite loop. Thus we check whether IRE_MARK_NOADD is
10277c478bd9Sstevel@tonic-gate 		 * is set. This also implies that IRE_MARK_NOADD can only be
10287c478bd9Sstevel@tonic-gate 		 * used to send packets to directly connected hosts.
10297c478bd9Sstevel@tonic-gate 		 */
10307c478bd9Sstevel@tonic-gate 		if (ipha->ipha_dst != ire->ire_addr &&
10317c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_NOADD)) {
10327c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
10335597b60aSnordmark 			if (CONN_Q(q)) {
10345597b60aSnordmark 				(void) ip_output(Q_TO_CONN(q), ipsec_mp, q,
10355597b60aSnordmark 				    IRE_SEND);
10365597b60aSnordmark 			} else {
10375597b60aSnordmark 				(void) ip_output((void *)(uintptr_t)zoneid,
10385597b60aSnordmark 				    ipsec_mp, q, IRE_SEND);
10395597b60aSnordmark 			}
10407c478bd9Sstevel@tonic-gate 		} else {
10417c478bd9Sstevel@tonic-gate 			if (is_secure) {
10427c478bd9Sstevel@tonic-gate 				ipsec_out_t *oi;
10437c478bd9Sstevel@tonic-gate 				ipha_t *ipha;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 				oi = (ipsec_out_t *)ipsec_mp->b_rptr;
10467c478bd9Sstevel@tonic-gate 				ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
10477c478bd9Sstevel@tonic-gate 				if (oi->ipsec_out_proc_begin) {
10487c478bd9Sstevel@tonic-gate 					/*
10497c478bd9Sstevel@tonic-gate 					 * This is the case where
10507c478bd9Sstevel@tonic-gate 					 * ip_wput_ipsec_out could not find
10517c478bd9Sstevel@tonic-gate 					 * the IRE and recreated a new one.
10527c478bd9Sstevel@tonic-gate 					 * As ip_wput_ipsec_out does ire
10537c478bd9Sstevel@tonic-gate 					 * lookups, ire_refrele for the extra
10547c478bd9Sstevel@tonic-gate 					 * bump in ire_add.
10557c478bd9Sstevel@tonic-gate 					 */
10567c478bd9Sstevel@tonic-gate 					ire_refrele(ire);
10577c478bd9Sstevel@tonic-gate 					ip_wput_ipsec_out(q, ipsec_mp, ipha,
10587c478bd9Sstevel@tonic-gate 					    NULL, NULL);
10597c478bd9Sstevel@tonic-gate 				} else {
10607c478bd9Sstevel@tonic-gate 					/*
10617c478bd9Sstevel@tonic-gate 					 * IRE_REFRELE will be done in
10627c478bd9Sstevel@tonic-gate 					 * ip_wput_ire.
10637c478bd9Sstevel@tonic-gate 					 */
10647c478bd9Sstevel@tonic-gate 					ip_wput_ire(q, ipsec_mp, ire, NULL,
10655597b60aSnordmark 					    IRE_SEND, zoneid);
10667c478bd9Sstevel@tonic-gate 				}
10677c478bd9Sstevel@tonic-gate 			} else {
10687c478bd9Sstevel@tonic-gate 				/*
10697c478bd9Sstevel@tonic-gate 				 * IRE_REFRELE will be done in ip_wput_ire.
10707c478bd9Sstevel@tonic-gate 				 */
10717c478bd9Sstevel@tonic-gate 				ip_wput_ire(q, ipsec_mp, ire, NULL,
10725597b60aSnordmark 				    IRE_SEND, zoneid);
10737c478bd9Sstevel@tonic-gate 			}
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 		/*
10767c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
10777c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
10787c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
10797c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
10807c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
10817c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
10827c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
10837c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append, ip_ire_req and ip_bind_connected.
10847c478bd9Sstevel@tonic-gate 		 * Though, this does not completely prevent other threads
10857c478bd9Sstevel@tonic-gate 		 * from using this ire, this should not cause any problems.
10867c478bd9Sstevel@tonic-gate 		 *
10877c478bd9Sstevel@tonic-gate 		 * NOTE : We use is_inaddr_any instead of using ire_src_addr
10887c478bd9Sstevel@tonic-gate 		 * because for the normal case i.e !is_inaddr_any, ire_refrele
10897c478bd9Sstevel@tonic-gate 		 * above could have potentially freed the ire.
10907c478bd9Sstevel@tonic-gate 		 */
10917c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
10927c478bd9Sstevel@tonic-gate 			/*
10937c478bd9Sstevel@tonic-gate 			 * If this IRE has been deleted by another thread, then
10947c478bd9Sstevel@tonic-gate 			 * ire_bucket won't be NULL, but ire_ptpn will be NULL.
10957c478bd9Sstevel@tonic-gate 			 * Thus, ire_delete will do nothing.  This check
10967c478bd9Sstevel@tonic-gate 			 * guards against calling ire_delete when the IRE was
10977c478bd9Sstevel@tonic-gate 			 * never inserted in the table, which is handled by
10987c478bd9Sstevel@tonic-gate 			 * ire_delete as dropping another reference.
10997c478bd9Sstevel@tonic-gate 			 */
11007c478bd9Sstevel@tonic-gate 			if (ire->ire_bucket != NULL) {
11017c478bd9Sstevel@tonic-gate 				ip1dbg(("ire_send: delete IRE\n"));
11027c478bd9Sstevel@tonic-gate 				ire_delete(ire);
11037c478bd9Sstevel@tonic-gate 			}
11047c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held above */
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
11117c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
11127c478bd9Sstevel@tonic-gate  * the packet has been sent.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate static void
11157c478bd9Sstevel@tonic-gate ire_send_v6(queue_t *q, mblk_t *pkt, ire_t *ire)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
11187c478bd9Sstevel@tonic-gate 	boolean_t secure;
11197c478bd9Sstevel@tonic-gate 	uint_t ifindex;
11205597b60aSnordmark 	zoneid_t zoneid = ire->ire_zoneid;
1121f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
11245597b60aSnordmark 	ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */
11257c478bd9Sstevel@tonic-gate 	if (pkt->b_datap->db_type == M_CTL) {
11265597b60aSnordmark 		ipsec_out_t *io;
11275597b60aSnordmark 
11287c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
11297c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
11307c478bd9Sstevel@tonic-gate 		secure = B_TRUE;
11315597b60aSnordmark 		io = (ipsec_out_t *)ipsec_mp->b_rptr;
11325597b60aSnordmark 		if (io->ipsec_out_type == IPSEC_OUT)
11335597b60aSnordmark 			zoneid = io->ipsec_out_zoneid;
11347c478bd9Sstevel@tonic-gate 	} else {
11357c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
11367c478bd9Sstevel@tonic-gate 		secure = B_FALSE;
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
11407c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
11417c478bd9Sstevel@tonic-gate 		ill_t	*ill;
11427c478bd9Sstevel@tonic-gate 		/*
11437c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_data_v6).
11447c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
11457c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from the
1146c793af95Ssangeeta 		 * resolver).
11477c478bd9Sstevel@tonic-gate 		 */
11487c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
11497c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_TRUE,
1150f4b3ec61Sdh155122 		    NULL, NULL, NULL, NULL, ipst);
11517c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
11527c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
11537c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
11547c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
11557c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
11567c478bd9Sstevel@tonic-gate 			return;
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
11597c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
11607c478bd9Sstevel@tonic-gate 		/*
11617c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
11627c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
11637c478bd9Sstevel@tonic-gate 		 * prepended.
11647c478bd9Sstevel@tonic-gate 		 */
11657c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
11667c478bd9Sstevel@tonic-gate 		put(q, pkt);
11677c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
11687c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
11697c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
11707c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
11717c478bd9Sstevel@tonic-gate 		/*
11727c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
11737c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
11747c478bd9Sstevel@tonic-gate 		 */
11757c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
11767c478bd9Sstevel@tonic-gate 		/*
11777c478bd9Sstevel@tonic-gate 		 * XXX TODO IPv6.
11787c478bd9Sstevel@tonic-gate 		 */
11797c478bd9Sstevel@tonic-gate 		freemsg(pkt);
11807c478bd9Sstevel@tonic-gate #ifdef XXX
11817c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, pkt, NULL);
11827c478bd9Sstevel@tonic-gate #endif
11837c478bd9Sstevel@tonic-gate 	} else {
11847c478bd9Sstevel@tonic-gate 		if (secure) {
11857c478bd9Sstevel@tonic-gate 			ipsec_out_t *oi;
11867c478bd9Sstevel@tonic-gate 			ip6_t *ip6h;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 			oi = (ipsec_out_t *)ipsec_mp->b_rptr;
11897c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)ipsec_mp->b_cont->b_rptr;
11907c478bd9Sstevel@tonic-gate 			if (oi->ipsec_out_proc_begin) {
11917c478bd9Sstevel@tonic-gate 				/*
11927c478bd9Sstevel@tonic-gate 				 * This is the case where
11937c478bd9Sstevel@tonic-gate 				 * ip_wput_ipsec_out could not find
11947c478bd9Sstevel@tonic-gate 				 * the IRE and recreated a new one.
11957c478bd9Sstevel@tonic-gate 				 */
11967c478bd9Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h,
11977c478bd9Sstevel@tonic-gate 				    NULL, NULL);
11987c478bd9Sstevel@tonic-gate 			} else {
11995597b60aSnordmark 				if (CONN_Q(q)) {
12005597b60aSnordmark 					(void) ip_output_v6(Q_TO_CONN(q),
12015597b60aSnordmark 					    ipsec_mp, q, IRE_SEND);
12025597b60aSnordmark 				} else {
12035597b60aSnordmark 					(void) ip_output_v6(
12045597b60aSnordmark 					    (void *)(uintptr_t)zoneid,
12055597b60aSnordmark 					    ipsec_mp, q, IRE_SEND);
12065597b60aSnordmark 				}
12077c478bd9Sstevel@tonic-gate 			}
12087c478bd9Sstevel@tonic-gate 		} else {
12097c478bd9Sstevel@tonic-gate 			/*
12107c478bd9Sstevel@tonic-gate 			 * Send packets through ip_output_v6 so that any
12117c478bd9Sstevel@tonic-gate 			 * ip6_info header can be processed again.
12127c478bd9Sstevel@tonic-gate 			 */
12135597b60aSnordmark 			if (CONN_Q(q)) {
12147c478bd9Sstevel@tonic-gate 				(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp, q,
12157c478bd9Sstevel@tonic-gate 				    IRE_SEND);
12165597b60aSnordmark 			} else {
12175597b60aSnordmark 				(void) ip_output_v6((void *)(uintptr_t)zoneid,
12185597b60aSnordmark 				    ipsec_mp, q, IRE_SEND);
12195597b60aSnordmark 			}
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		/*
12227c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
12237c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
12247c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
12257c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
12267c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
12277c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
12287c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
12297c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append_v6, ip_ire_req and
12307c478bd9Sstevel@tonic-gate 		 * ip_bind_connected_v6. Though, this does not completely
12317c478bd9Sstevel@tonic-gate 		 * prevent other threads from using this ire, this should
12327c478bd9Sstevel@tonic-gate 		 * not cause any problems.
12337c478bd9Sstevel@tonic-gate 		 */
12347c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6)) {
12357c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_send_v6: delete IRE\n"));
12367c478bd9Sstevel@tonic-gate 			ire_delete(ire);
12377c478bd9Sstevel@tonic-gate 		}
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 	ire_refrele(ire);	/* Held in ire_add */
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate /*
12437c478bd9Sstevel@tonic-gate  * Make sure that IRE bucket does not get too long.
12447c478bd9Sstevel@tonic-gate  * This can cause lock up because ire_cache_lookup()
12457c478bd9Sstevel@tonic-gate  * may take "forever" to finish.
12467c478bd9Sstevel@tonic-gate  *
12477c478bd9Sstevel@tonic-gate  * We just remove cnt IREs each time.  This means that
12487c478bd9Sstevel@tonic-gate  * the bucket length will stay approximately constant,
12497c478bd9Sstevel@tonic-gate  * depending on cnt.  This should be enough to defend
12507c478bd9Sstevel@tonic-gate  * against DoS attack based on creating temporary IREs
12517c478bd9Sstevel@tonic-gate  * (for forwarding and non-TCP traffic).
12527c478bd9Sstevel@tonic-gate  *
12537c478bd9Sstevel@tonic-gate  * Note that new IRE is normally added at the tail of the
12547c478bd9Sstevel@tonic-gate  * bucket.  This means that we are removing the "oldest"
12557c478bd9Sstevel@tonic-gate  * temporary IRE added.  Only if there are IREs with
12567c478bd9Sstevel@tonic-gate  * the same ire_addr, do we not add it at the tail.  Refer
12577c478bd9Sstevel@tonic-gate  * to ire_add_v*().  It should be OK for our purpose.
12587c478bd9Sstevel@tonic-gate  *
12597c478bd9Sstevel@tonic-gate  * For non-temporary cached IREs, we make sure that they
12607c478bd9Sstevel@tonic-gate  * have not been used for some time (defined below), they
12617c478bd9Sstevel@tonic-gate  * are non-local destinations, and there is no one using
12627c478bd9Sstevel@tonic-gate  * them at the moment (refcnt == 1).
12637c478bd9Sstevel@tonic-gate  *
12647c478bd9Sstevel@tonic-gate  * The above means that the IRE bucket length may become
12657c478bd9Sstevel@tonic-gate  * very long, consisting of mostly non-temporary IREs.
12667c478bd9Sstevel@tonic-gate  * This can happen when the hash function does a bad job
12677c478bd9Sstevel@tonic-gate  * so that most TCP connections cluster to a specific bucket.
12687c478bd9Sstevel@tonic-gate  * This "hopefully" should never happen.  It can also
12697c478bd9Sstevel@tonic-gate  * happen if most TCP connections have very long lives.
12707c478bd9Sstevel@tonic-gate  * Even with the minimal hash table size of 256, there
12717c478bd9Sstevel@tonic-gate  * has to be a lot of such connections to make the bucket
12727c478bd9Sstevel@tonic-gate  * length unreasonably long.  This should probably not
12737c478bd9Sstevel@tonic-gate  * happen either.  The third can when this can happen is
12747c478bd9Sstevel@tonic-gate  * when the machine is under attack, such as SYN flooding.
12757c478bd9Sstevel@tonic-gate  * TCP should already have the proper mechanism to protect
12767c478bd9Sstevel@tonic-gate  * that.  So we should be safe.
12777c478bd9Sstevel@tonic-gate  *
12787c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send() after
12797c478bd9Sstevel@tonic-gate  * a new IRE is added and the packet is sent.
12807c478bd9Sstevel@tonic-gate  *
12817c478bd9Sstevel@tonic-gate  * The idle cutoff interval is set to 60s.  It can be
12827c478bd9Sstevel@tonic-gate  * changed using /etc/system.
12837c478bd9Sstevel@tonic-gate  */
12847c478bd9Sstevel@tonic-gate uint32_t ire_idle_cutoff_interval = 60000;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate static void
12877c478bd9Sstevel@tonic-gate ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt)
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate 	ire_t *ire;
12907c478bd9Sstevel@tonic-gate 	int tmp_cnt = cnt;
12917c478bd9Sstevel@tonic-gate 	clock_t cut_off = drv_usectohz(ire_idle_cutoff_interval * 1000);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	/*
12947c478bd9Sstevel@tonic-gate 	 * irb is NULL if the IRE is not added to the hash.  This
1295*5c0b7edeSseb 	 * happens when IRE_MARK_NOADD is set in ire_add_then_send().
12967c478bd9Sstevel@tonic-gate 	 */
12977c478bd9Sstevel@tonic-gate 	if (irb == NULL)
12987c478bd9Sstevel@tonic-gate 		return;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
13017c478bd9Sstevel@tonic-gate 	if (irb->irb_tmp_ire_cnt > threshold) {
13027c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && tmp_cnt > 0;
13037c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
13047c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
13057c478bd9Sstevel@tonic-gate 				continue;
13067c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY) {
13077c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_type == IRE_CACHE);
13087c478bd9Sstevel@tonic-gate 				ire_delete(ire);
13097c478bd9Sstevel@tonic-gate 				tmp_cnt--;
13107c478bd9Sstevel@tonic-gate 			}
13117c478bd9Sstevel@tonic-gate 		}
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 	if (irb->irb_ire_cnt - irb->irb_tmp_ire_cnt > threshold) {
13147c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && cnt > 0;
13157c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
1316f4b3ec61Sdh155122 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
1317f4b3ec61Sdh155122 				continue;
1318f4b3ec61Sdh155122 			if (ire->ire_ipversion == IPV4_VERSION) {
1319f4b3ec61Sdh155122 				if (ire->ire_gateway_addr == 0)
1320f4b3ec61Sdh155122 					continue;
1321f4b3ec61Sdh155122 			} else {
1322f4b3ec61Sdh155122 				if (IN6_IS_ADDR_UNSPECIFIED(
1323f4b3ec61Sdh155122 				    &ire->ire_gateway_addr_v6))
13247c478bd9Sstevel@tonic-gate 					continue;
13257c478bd9Sstevel@tonic-gate 			}
13267c478bd9Sstevel@tonic-gate 			if ((ire->ire_type == IRE_CACHE) &&
13277c478bd9Sstevel@tonic-gate 			    (lbolt - ire->ire_last_used_time > cut_off) &&
13287c478bd9Sstevel@tonic-gate 			    (ire->ire_refcnt == 1)) {
13297c478bd9Sstevel@tonic-gate 				ire_delete(ire);
13307c478bd9Sstevel@tonic-gate 				cnt--;
13317c478bd9Sstevel@tonic-gate 			}
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate /*
13387c478bd9Sstevel@tonic-gate  * ire_add_then_send is called when a new IRE has been created in order to
13397c478bd9Sstevel@tonic-gate  * route an outgoing packet.  Typically, it is called from ip_wput when
13407c478bd9Sstevel@tonic-gate  * a response comes back down from a resolver.  We add the IRE, and then
13417c478bd9Sstevel@tonic-gate  * possibly run the packet through ip_wput or ip_rput, as appropriate.
13427c478bd9Sstevel@tonic-gate  * However, we do not add the newly created IRE in the cache when
13437c478bd9Sstevel@tonic-gate  * IRE_MARK_NOADD is set in the IRE. IRE_MARK_NOADD is set at
1344*5c0b7edeSseb  * ip_newroute_ipif(). The ires with IRE_MARK_NOADD are ire_refrele'd by
1345*5c0b7edeSseb  * ip_wput_ire() and get deleted.
13467c478bd9Sstevel@tonic-gate  * Multirouting support: the packet is silently discarded when the new IRE
13477c478bd9Sstevel@tonic-gate  * holds the RTF_MULTIRT flag, but is not the first IRE to be added with the
13487c478bd9Sstevel@tonic-gate  * RTF_MULTIRT flag for the same destination address.
13497c478bd9Sstevel@tonic-gate  * In this case, we just want to register this additional ire without
13507c478bd9Sstevel@tonic-gate  * sending the packet, as it has already been replicated through
13517c478bd9Sstevel@tonic-gate  * existing multirt routes in ip_wput().
13527c478bd9Sstevel@tonic-gate  */
13537c478bd9Sstevel@tonic-gate void
13547c478bd9Sstevel@tonic-gate ire_add_then_send(queue_t *q, ire_t *ire, mblk_t *mp)
13557c478bd9Sstevel@tonic-gate {
13567c478bd9Sstevel@tonic-gate 	irb_t *irb;
13577c478bd9Sstevel@tonic-gate 	boolean_t drop = B_FALSE;
13587c478bd9Sstevel@tonic-gate 	/* LINTED : set but not used in function */
13597c478bd9Sstevel@tonic-gate 	boolean_t mctl_present;
13607c478bd9Sstevel@tonic-gate 	mblk_t *first_mp = NULL;
13617c478bd9Sstevel@tonic-gate 	mblk_t *save_mp = NULL;
13627c478bd9Sstevel@tonic-gate 	ire_t *dst_ire;
13637c478bd9Sstevel@tonic-gate 	ipha_t *ipha;
13647c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
1365f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
13687c478bd9Sstevel@tonic-gate 		/*
13697c478bd9Sstevel@tonic-gate 		 * We first have to retrieve the destination address carried
13707c478bd9Sstevel@tonic-gate 		 * by the packet.
13717c478bd9Sstevel@tonic-gate 		 * We can't rely on ire as it can be related to a gateway.
13727c478bd9Sstevel@tonic-gate 		 * The destination address will help in determining if
13737c478bd9Sstevel@tonic-gate 		 * other RTF_MULTIRT ires are already registered.
13747c478bd9Sstevel@tonic-gate 		 *
13757c478bd9Sstevel@tonic-gate 		 * We first need to know where we are going : v4 or V6.
13767c478bd9Sstevel@tonic-gate 		 * the ire version is enough, as there is no risk that
13777c478bd9Sstevel@tonic-gate 		 * we resolve an IPv6 address with an IPv4 ire
13787c478bd9Sstevel@tonic-gate 		 * or vice versa.
13797c478bd9Sstevel@tonic-gate 		 */
13807c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
13817c478bd9Sstevel@tonic-gate 			EXTRACT_PKT_MP(mp, first_mp, mctl_present);
13827c478bd9Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
13837c478bd9Sstevel@tonic-gate 			save_mp = mp;
13847c478bd9Sstevel@tonic-gate 			mp = first_mp;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup(ipha->ipha_dst,
1387f4b3ec61Sdh155122 			    ire->ire_zoneid, MBLK_GETLABEL(mp), ipst);
13887c478bd9Sstevel@tonic-gate 		} else {
1389c793af95Ssangeeta 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
13907c478bd9Sstevel@tonic-gate 			/*
13917c478bd9Sstevel@tonic-gate 			 * Get a pointer to the beginning of the IPv6 header.
13927c478bd9Sstevel@tonic-gate 			 * Ignore leading IPsec control mblks.
13937c478bd9Sstevel@tonic-gate 			 */
13947c478bd9Sstevel@tonic-gate 			first_mp = mp;
13957c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_CTL) {
13967c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
13977c478bd9Sstevel@tonic-gate 			}
13987c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)mp->b_rptr;
13997c478bd9Sstevel@tonic-gate 			save_mp = mp;
14007c478bd9Sstevel@tonic-gate 			mp = first_mp;
14017c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup_v6(&ip6h->ip6_dst,
1402f4b3ec61Sdh155122 			    ire->ire_zoneid, MBLK_GETLABEL(mp), ipst);
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate 		if (dst_ire != NULL) {
14057c478bd9Sstevel@tonic-gate 			if (dst_ire->ire_flags & RTF_MULTIRT) {
14067c478bd9Sstevel@tonic-gate 				/*
14077c478bd9Sstevel@tonic-gate 				 * At least one resolved multirt route
14087c478bd9Sstevel@tonic-gate 				 * already exists for the destination,
14097c478bd9Sstevel@tonic-gate 				 * don't sent this packet: either drop it
14107c478bd9Sstevel@tonic-gate 				 * or complete the pending resolution,
14117c478bd9Sstevel@tonic-gate 				 * depending on the ire.
14127c478bd9Sstevel@tonic-gate 				 */
14137c478bd9Sstevel@tonic-gate 				drop = B_TRUE;
14147c478bd9Sstevel@tonic-gate 			}
14157c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_then_send: dst_ire %p "
14167c478bd9Sstevel@tonic-gate 			    "[dst %08x, gw %08x], drop %d\n",
14177c478bd9Sstevel@tonic-gate 			    (void *)dst_ire,
14187c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
14197c478bd9Sstevel@tonic-gate 			    ntohl(dst_ire->ire_addr) : \
14207c478bd9Sstevel@tonic-gate 			    ntohl(V4_PART_OF_V6(dst_ire->ire_addr_v6)),
14217c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
14227c478bd9Sstevel@tonic-gate 			    ntohl(dst_ire->ire_gateway_addr) : \
14237c478bd9Sstevel@tonic-gate 			    ntohl(V4_PART_OF_V6(
14247c478bd9Sstevel@tonic-gate 			    dst_ire->ire_gateway_addr_v6)),
14257c478bd9Sstevel@tonic-gate 			    drop));
14267c478bd9Sstevel@tonic-gate 			ire_refrele(dst_ire);
14277c478bd9Sstevel@tonic-gate 		}
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (!(ire->ire_marks & IRE_MARK_NOADD)) {
1431*5c0b7edeSseb 		/* Regular packets with cache bound ires are here. */
1432c793af95Ssangeeta 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
14357c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
14367c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
14377c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
14387c478bd9Sstevel@tonic-gate 			freemsg(mp);
14397c478bd9Sstevel@tonic-gate 			return;
14407c478bd9Sstevel@tonic-gate 		}
14417c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
14427c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add_v4/v6 */
14437c478bd9Sstevel@tonic-gate 			return;
14447c478bd9Sstevel@tonic-gate 		}
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 	if (drop) {
14477c478bd9Sstevel@tonic-gate 		/*
14487c478bd9Sstevel@tonic-gate 		 * If we're adding an RTF_MULTIRT ire, the resolution
14497c478bd9Sstevel@tonic-gate 		 * is over: we just drop the packet.
14507c478bd9Sstevel@tonic-gate 		 */
14517c478bd9Sstevel@tonic-gate 		if (ire->ire_flags & RTF_MULTIRT) {
14527c478bd9Sstevel@tonic-gate 			if (save_mp) {
14537c478bd9Sstevel@tonic-gate 				save_mp->b_prev = NULL;
14547c478bd9Sstevel@tonic-gate 				save_mp->b_next = NULL;
14557c478bd9Sstevel@tonic-gate 			}
14567c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
14577c478bd9Sstevel@tonic-gate 			freemsg(mp);
14587c478bd9Sstevel@tonic-gate 		} else {
14597c478bd9Sstevel@tonic-gate 			/*
14607c478bd9Sstevel@tonic-gate 			 * Otherwise, we're adding the ire to a gateway
14617c478bd9Sstevel@tonic-gate 			 * for a multirt route.
14627c478bd9Sstevel@tonic-gate 			 * Invoke ip_newroute() to complete the resolution
14637c478bd9Sstevel@tonic-gate 			 * of the route. We will then come back here and
14647c478bd9Sstevel@tonic-gate 			 * finally drop this packet in the above code.
14657c478bd9Sstevel@tonic-gate 			 */
14667c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
14677c478bd9Sstevel@tonic-gate 				/*
14687c478bd9Sstevel@tonic-gate 				 * TODO: in order for CGTP to work in non-global
14697c478bd9Sstevel@tonic-gate 				 * zones, ip_newroute() must create the IRE
14707c478bd9Sstevel@tonic-gate 				 * cache in the zone indicated by
14717c478bd9Sstevel@tonic-gate 				 * ire->ire_zoneid.
14727c478bd9Sstevel@tonic-gate 				 */
1473*5c0b7edeSseb 				ip_newroute(q, mp, ipha->ipha_dst,
14745597b60aSnordmark 				    (CONN_Q(q) ? Q_TO_CONN(q) : NULL),
1475f4b3ec61Sdh155122 				    ire->ire_zoneid, ipst);
14767c478bd9Sstevel@tonic-gate 			} else {
1477c793af95Ssangeeta 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
14787c478bd9Sstevel@tonic-gate 				ip_newroute_v6(q, mp, &ip6h->ip6_dst, NULL,
1479f4b3ec61Sdh155122 				    NULL, ire->ire_zoneid, ipst);
14807c478bd9Sstevel@tonic-gate 			}
14817c478bd9Sstevel@tonic-gate 		}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		ire_refrele(ire); /* As done by ire_send(). */
14847c478bd9Sstevel@tonic-gate 		return;
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 	/*
14877c478bd9Sstevel@tonic-gate 	 * Need to remember ire_bucket here as ire_send*() may delete
14887c478bd9Sstevel@tonic-gate 	 * the ire so we cannot reference it after that.
14897c478bd9Sstevel@tonic-gate 	 */
14907c478bd9Sstevel@tonic-gate 	irb = ire->ire_bucket;
14917c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
14927c478bd9Sstevel@tonic-gate 		ire_send_v6(q, mp, ire);
14937c478bd9Sstevel@tonic-gate 		/*
14947c478bd9Sstevel@tonic-gate 		 * Clean up more than 1 IRE so that the clean up does not
14957c478bd9Sstevel@tonic-gate 		 * need to be done every time when a new IRE is added and
14967c478bd9Sstevel@tonic-gate 		 * the threshold is reached.
14977c478bd9Sstevel@tonic-gate 		 */
14987c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip6_ire_max_bucket_cnt, 2);
14997c478bd9Sstevel@tonic-gate 	} else {
15007c478bd9Sstevel@tonic-gate 		ire_send(q, mp, ire);
15017c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip_ire_max_bucket_cnt, 2);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate /*
15067c478bd9Sstevel@tonic-gate  * Initialize the ire that is specific to IPv4 part and call
15077c478bd9Sstevel@tonic-gate  * ire_init_common to finish it.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate ire_t *
15107c478bd9Sstevel@tonic-gate ire_init(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *src_addr,
1511*5c0b7edeSseb     uchar_t *gateway, uint_t *max_fragp, nce_t *src_nce, queue_t *rfq,
1512*5c0b7edeSseb     queue_t *stq, ushort_t type, ipif_t *ipif, ipaddr_t cmask, uint32_t phandle,
1513*5c0b7edeSseb     uint32_t ihandle, uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc,
1514*5c0b7edeSseb     tsol_gcgrp_t *gcgrp, ip_stack_t *ipst)
15157c478bd9Sstevel@tonic-gate {
151645916cd2Sjpk 	/*
151745916cd2Sjpk 	 * Reject IRE security attribute creation/initialization
151845916cd2Sjpk 	 * if system is not running in Trusted mode.
151945916cd2Sjpk 	 */
152045916cd2Sjpk 	if ((gc != NULL || gcgrp != NULL) && !is_system_labeled())
152145916cd2Sjpk 		return (NULL);
152245916cd2Sjpk 
15237c478bd9Sstevel@tonic-gate 
1524f4b3ec61Sdh155122 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	if (addr != NULL)
15277c478bd9Sstevel@tonic-gate 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
15287c478bd9Sstevel@tonic-gate 	if (src_addr != NULL)
15297c478bd9Sstevel@tonic-gate 		bcopy(src_addr, &ire->ire_src_addr, IP_ADDR_LEN);
15307c478bd9Sstevel@tonic-gate 	if (mask != NULL) {
15317c478bd9Sstevel@tonic-gate 		bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
15327c478bd9Sstevel@tonic-gate 		ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 	if (gateway != NULL) {
15357c478bd9Sstevel@tonic-gate 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (type == IRE_CACHE)
15397c478bd9Sstevel@tonic-gate 		ire->ire_cmask = cmask;
15407c478bd9Sstevel@tonic-gate 
154145916cd2Sjpk 	/* ire_init_common will free the mblks upon encountering any failure */
1542*5c0b7edeSseb 	if (!ire_init_common(ire, max_fragp, src_nce, rfq, stq, type, ipif,
1543*5c0b7edeSseb 	    phandle, ihandle, flags, IPV4_VERSION, ulp_info, gc, gcgrp, ipst))
154445916cd2Sjpk 		return (NULL);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	return (ire);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate  * Similar to ire_create except that it is called only when
15517c478bd9Sstevel@tonic-gate  * we want to allocate ire as an mblk e.g. we have an external
15527c478bd9Sstevel@tonic-gate  * resolver ARP.
15537c478bd9Sstevel@tonic-gate  */
15547c478bd9Sstevel@tonic-gate ire_t *
15557c478bd9Sstevel@tonic-gate ire_create_mp(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
1556*5c0b7edeSseb     uint_t max_frag, nce_t *src_nce, queue_t *rfq, queue_t *stq, ushort_t type,
1557*5c0b7edeSseb     ipif_t *ipif, ipaddr_t cmask, uint32_t phandle, uint32_t ihandle,
1558*5c0b7edeSseb     uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1559f4b3ec61Sdh155122     ip_stack_t *ipst)
15607c478bd9Sstevel@tonic-gate {
1561c793af95Ssangeeta 	ire_t	*ire, *buf;
15627c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
15637c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
1564c793af95Ssangeeta 	size_t	bufsize;
1565c793af95Ssangeeta 	frtn_t	*frtnp;
1566c793af95Ssangeeta 	ill_t	*ill;
15677c478bd9Sstevel@tonic-gate 
1568c793af95Ssangeeta 	bufsize = sizeof (ire_t) + sizeof (frtn_t);
1569c793af95Ssangeeta 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
1570c793af95Ssangeeta 	if (buf == NULL) {
15717c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create_mp: alloc failed\n"));
15727c478bd9Sstevel@tonic-gate 		return (NULL);
15737c478bd9Sstevel@tonic-gate 	}
1574c793af95Ssangeeta 	frtnp = (frtn_t *)(buf + 1);
1575c793af95Ssangeeta 	frtnp->free_arg = (caddr_t)buf;
1576c793af95Ssangeeta 	frtnp->free_func = ire_freemblk;
15777c478bd9Sstevel@tonic-gate 
1578c793af95Ssangeeta 	/*
1579c793af95Ssangeeta 	 * Allocate the new IRE. The ire created will hold a ref on
1580c793af95Ssangeeta 	 * an nce_t after ire_nce_init, and this ref must either be
1581c793af95Ssangeeta 	 * (a)  transferred to the ire_cache entry created when ire_add_v4
1582c793af95Ssangeeta 	 *	is called after successful arp resolution, or,
1583c793af95Ssangeeta 	 * (b)  released, when arp resolution fails
1584c793af95Ssangeeta 	 * Case (b) is handled in ire_freemblk() which will be called
1585c793af95Ssangeeta 	 * when mp is freed as a result of failed arp.
1586c793af95Ssangeeta 	 */
1587c793af95Ssangeeta 	mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp);
1588c793af95Ssangeeta 	if (mp == NULL) {
1589c793af95Ssangeeta 		ip1dbg(("ire_create_mp: alloc failed\n"));
1590c793af95Ssangeeta 		kmem_free(buf, bufsize);
1591c793af95Ssangeeta 		return (NULL);
1592c793af95Ssangeeta 	}
15937c478bd9Sstevel@tonic-gate 	ire = (ire_t *)mp->b_rptr;
15947c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&ire[1];
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/* Start clean. */
15977c478bd9Sstevel@tonic-gate 	*ire = ire_null;
15987c478bd9Sstevel@tonic-gate 	ire->ire_mp = mp;
15997c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
1600c793af95Ssangeeta 	ire->ire_marks |= IRE_MARK_UNCACHED;
16017c478bd9Sstevel@tonic-gate 
1602*5c0b7edeSseb 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, NULL, src_nce,
1603*5c0b7edeSseb 	    rfq, stq, type, ipif, cmask, phandle, ihandle, flags, ulp_info, gc,
1604*5c0b7edeSseb 	    gcgrp, ipst);
16057c478bd9Sstevel@tonic-gate 
160653bec8caSsowmini 	ill = (ill_t *)(stq->q_ptr);
16077c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
1608f4b3ec61Sdh155122 		/* ire_freemblk needs these set */
160953bec8caSsowmini 		ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
1610f4b3ec61Sdh155122 		ire->ire_ipst = ipst;
16117c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
16127c478bd9Sstevel@tonic-gate 		return (NULL);
16137c478bd9Sstevel@tonic-gate 	}
1614c793af95Ssangeeta 	ret_ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
16157c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
16167c478bd9Sstevel@tonic-gate 	/*
16177c478bd9Sstevel@tonic-gate 	 * ire_max_frag is normally zero here and is atomically set
16187c478bd9Sstevel@tonic-gate 	 * under the irebucket lock in ire_add_v[46] except for the
16197c478bd9Sstevel@tonic-gate 	 * case of IRE_MARK_NOADD. In that event the the ire_max_frag
16207c478bd9Sstevel@tonic-gate 	 * is non-zero here.
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = max_frag;
16237c478bd9Sstevel@tonic-gate 	return (ire);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate /*
16277c478bd9Sstevel@tonic-gate  * ire_create is called to allocate and initialize a new IRE.
16287c478bd9Sstevel@tonic-gate  *
16297c478bd9Sstevel@tonic-gate  * NOTE : This is called as writer sometimes though not required
16307c478bd9Sstevel@tonic-gate  * by this function.
16317c478bd9Sstevel@tonic-gate  */
16327c478bd9Sstevel@tonic-gate ire_t *
16337c478bd9Sstevel@tonic-gate ire_create(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
1634*5c0b7edeSseb     uint_t *max_fragp, nce_t *src_nce, queue_t *rfq, queue_t *stq,
1635*5c0b7edeSseb     ushort_t type, ipif_t *ipif, ipaddr_t cmask, uint32_t phandle,
1636*5c0b7edeSseb     uint32_t ihandle, uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc,
1637*5c0b7edeSseb     tsol_gcgrp_t *gcgrp, ip_stack_t *ipst)
16387c478bd9Sstevel@tonic-gate {
16397c478bd9Sstevel@tonic-gate 	ire_t	*ire;
16407c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
16437c478bd9Sstevel@tonic-gate 	if (ire == NULL) {
16447c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create: alloc failed\n"));
16457c478bd9Sstevel@tonic-gate 		return (NULL);
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate 	*ire = ire_null;
16487c478bd9Sstevel@tonic-gate 
1649*5c0b7edeSseb 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, max_fragp,
1650*5c0b7edeSseb 	    src_nce, rfq, stq, type, ipif, cmask, phandle, ihandle, flags,
1651*5c0b7edeSseb 	    ulp_info, gc, gcgrp, ipst);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
16547c478bd9Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
16557c478bd9Sstevel@tonic-gate 		return (NULL);
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
16587c478bd9Sstevel@tonic-gate 	return (ire);
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate /*
16637c478bd9Sstevel@tonic-gate  * Common to IPv4 and IPv6
16647c478bd9Sstevel@tonic-gate  */
166545916cd2Sjpk boolean_t
166654da8755Ssowmini ire_init_common(ire_t *ire, uint_t *max_fragp, nce_t *src_nce, queue_t *rfq,
1667*5c0b7edeSseb     queue_t *stq, ushort_t type, ipif_t *ipif, uint32_t phandle,
166854da8755Ssowmini     uint32_t ihandle, uint32_t flags, uchar_t ipversion, const iulp_t *ulp_info,
166954da8755Ssowmini     tsol_gc_t *gc, tsol_gcgrp_t *gcgrp, ip_stack_t *ipst)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = max_fragp;
1672f4b3ec61Sdh155122 	ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0;
16737c478bd9Sstevel@tonic-gate 
167445916cd2Sjpk #ifdef DEBUG
167545916cd2Sjpk 	if (ipif != NULL) {
16767c478bd9Sstevel@tonic-gate 		if (ipif->ipif_isv6)
16777c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV6_VERSION);
16787c478bd9Sstevel@tonic-gate 		else
16797c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV4_VERSION);
16807c478bd9Sstevel@tonic-gate 	}
168145916cd2Sjpk #endif /* DEBUG */
168245916cd2Sjpk 
168345916cd2Sjpk 	/*
168445916cd2Sjpk 	 * Create/initialize IRE security attribute only in Trusted mode;
168545916cd2Sjpk 	 * if the passed in gc/gcgrp is non-NULL, we expect that the caller
168645916cd2Sjpk 	 * has held a reference to it and will release it when this routine
168745916cd2Sjpk 	 * returns a failure, otherwise we own the reference.  We do this
168845916cd2Sjpk 	 * prior to initializing the rest IRE fields.
1689dc3879f9Sjarrett 	 *
1690dc3879f9Sjarrett 	 * Don't allocate ire_gw_secattr for the resolver case to prevent
1691dc3879f9Sjarrett 	 * memory leak (in case of external resolution failure). We'll
1692dc3879f9Sjarrett 	 * allocate it after a successful external resolution, in ire_add().
1693dc3879f9Sjarrett 	 * Note that ire->ire_mp != NULL here means this ire is headed
1694dc3879f9Sjarrett 	 * to an external resolver.
169545916cd2Sjpk 	 */
169645916cd2Sjpk 	if (is_system_labeled()) {
169745916cd2Sjpk 		if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST |
169845916cd2Sjpk 		    IRE_INTERFACE)) != 0) {
169945916cd2Sjpk 			/* release references on behalf of caller */
170045916cd2Sjpk 			if (gc != NULL)
170145916cd2Sjpk 				GC_REFRELE(gc);
170245916cd2Sjpk 			if (gcgrp != NULL)
170345916cd2Sjpk 				GCGRP_REFRELE(gcgrp);
1704dc3879f9Sjarrett 		} else if ((ire->ire_mp == NULL) &&
1705dc3879f9Sjarrett 		    tsol_ire_init_gwattr(ire, ipversion, gc, gcgrp) != 0) {
170645916cd2Sjpk 			return (B_FALSE);
170745916cd2Sjpk 		}
170845916cd2Sjpk 	}
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	ire->ire_stq = stq;
17117c478bd9Sstevel@tonic-gate 	ire->ire_rfq = rfq;
17127c478bd9Sstevel@tonic-gate 	ire->ire_type = type;
17137c478bd9Sstevel@tonic-gate 	ire->ire_flags = RTF_UP | flags;
17147c478bd9Sstevel@tonic-gate 	ire->ire_ident = TICK_TO_MSEC(lbolt);
17157c478bd9Sstevel@tonic-gate 	bcopy(ulp_info, &ire->ire_uinfo, sizeof (iulp_t));
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
17187c478bd9Sstevel@tonic-gate 	ire->ire_last_used_time = lbolt;
17197c478bd9Sstevel@tonic-gate 	ire->ire_create_time = (uint32_t)gethrestime_sec();
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	/*
17227c478bd9Sstevel@tonic-gate 	 * If this IRE is an IRE_CACHE, inherit the handles from the
17237c478bd9Sstevel@tonic-gate 	 * parent IREs. For others in the forwarding table, assign appropriate
17247c478bd9Sstevel@tonic-gate 	 * new ones.
17257c478bd9Sstevel@tonic-gate 	 *
17267c478bd9Sstevel@tonic-gate 	 * The mutex protecting ire_handle is because ire_create is not always
17277c478bd9Sstevel@tonic-gate 	 * called as a writer.
17287c478bd9Sstevel@tonic-gate 	 */
17297c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_OFFSUBNET) {
1730f4b3ec61Sdh155122 		mutex_enter(&ipst->ips_ire_handle_lock);
1731f4b3ec61Sdh155122 		ire->ire_phandle = (uint32_t)ipst->ips_ire_handle++;
1732f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_handle_lock);
17337c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type & IRE_INTERFACE) {
1734f4b3ec61Sdh155122 		mutex_enter(&ipst->ips_ire_handle_lock);
1735f4b3ec61Sdh155122 		ire->ire_ihandle = (uint32_t)ipst->ips_ire_handle++;
1736f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_handle_lock);
17377c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_CACHE) {
17387c478bd9Sstevel@tonic-gate 		ire->ire_phandle = phandle;
17397c478bd9Sstevel@tonic-gate 		ire->ire_ihandle = ihandle;
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 	ire->ire_ipif = ipif;
17427c478bd9Sstevel@tonic-gate 	if (ipif != NULL) {
17437c478bd9Sstevel@tonic-gate 		ire->ire_ipif_seqid = ipif->ipif_seqid;
17447c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = ipif->ipif_zoneid;
17457c478bd9Sstevel@tonic-gate 	} else {
17467c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = GLOBAL_ZONEID;
17477c478bd9Sstevel@tonic-gate 	}
17487c478bd9Sstevel@tonic-gate 	ire->ire_ipversion = ipversion;
17497c478bd9Sstevel@tonic-gate 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
1750c793af95Ssangeeta 	if (ipversion == IPV4_VERSION) {
175154da8755Ssowmini 		/*
175254da8755Ssowmini 		 * IPv6 initializes the ire_nce in ire_add_v6, which expects
175354da8755Ssowmini 		 * to find the ire_nce to be null when it is called.
175454da8755Ssowmini 		 */
175554da8755Ssowmini 		if (ire_nce_init(ire, src_nce) != 0) {
1756c793af95Ssangeeta 			/* some failure occurred. propagate error back */
1757c793af95Ssangeeta 			return (B_FALSE);
1758c793af95Ssangeeta 		}
1759c793af95Ssangeeta 	}
1760c793af95Ssangeeta 	ire->ire_refcnt = 1;
1761f4b3ec61Sdh155122 	ire->ire_ipst = ipst;	/* No netstack_hold */
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
17647c478bd9Sstevel@tonic-gate 	bzero(ire->ire_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
17657c478bd9Sstevel@tonic-gate #endif
176645916cd2Sjpk 
176745916cd2Sjpk 	return (B_TRUE);
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate /*
17717c478bd9Sstevel@tonic-gate  * This routine is called repeatedly by ipif_up to create broadcast IREs.
17727c478bd9Sstevel@tonic-gate  * It is passed a pointer to a slot in an IRE pointer array into which to
17737c478bd9Sstevel@tonic-gate  * place the pointer to the new IRE, if indeed we create one.  If the
17747c478bd9Sstevel@tonic-gate  * IRE corresponding to the address passed in would be a duplicate of an
17757c478bd9Sstevel@tonic-gate  * existing one, we don't create the new one.  irep is incremented before
17767c478bd9Sstevel@tonic-gate  * return only if we do create a new IRE.  (Always called as writer.)
17777c478bd9Sstevel@tonic-gate  *
17787c478bd9Sstevel@tonic-gate  * Note that with the "match_flags" parameter, we can match on either
17797c478bd9Sstevel@tonic-gate  * a particular logical interface (MATCH_IRE_IPIF) or for all logical
17807c478bd9Sstevel@tonic-gate  * interfaces for a given physical interface (MATCH_IRE_ILL).  Currently,
17817c478bd9Sstevel@tonic-gate  * we only create broadcast ire's on a per physical interface basis. If
17827c478bd9Sstevel@tonic-gate  * someone is going to be mucking with logical interfaces, it is important
17837c478bd9Sstevel@tonic-gate  * to call "ipif_check_bcast_ires()" to make sure that any change to a
17847c478bd9Sstevel@tonic-gate  * logical interface will not cause critical broadcast IRE's to be deleted.
17857c478bd9Sstevel@tonic-gate  */
17867c478bd9Sstevel@tonic-gate ire_t **
17877c478bd9Sstevel@tonic-gate ire_check_and_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep,
17887c478bd9Sstevel@tonic-gate     int match_flags)
17897c478bd9Sstevel@tonic-gate {
17907c478bd9Sstevel@tonic-gate 	ire_t *ire;
17917c478bd9Sstevel@tonic-gate 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
1792f4b3ec61Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	/*
17957c478bd9Sstevel@tonic-gate 	 * No broadcast IREs for the LOOPBACK interface
17967c478bd9Sstevel@tonic-gate 	 * or others such as point to point and IPIF_NOXMIT.
17977c478bd9Sstevel@tonic-gate 	 */
17987c478bd9Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_BROADCAST) ||
17997c478bd9Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_NOXMIT))
18007c478bd9Sstevel@tonic-gate 		return (irep);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/* If this would be a duplicate, don't bother. */
18037c478bd9Sstevel@tonic-gate 	if ((ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ipif,
1804f4b3ec61Sdh155122 	    ipif->ipif_zoneid, NULL, match_flags, ipst)) != NULL) {
18057c478bd9Sstevel@tonic-gate 		/*
18067c478bd9Sstevel@tonic-gate 		 * We look for non-deprecated (and non-anycast, non-nolocal)
18077c478bd9Sstevel@tonic-gate 		 * ipifs as the best choice. ipifs with check_flags matching
18087c478bd9Sstevel@tonic-gate 		 * (deprecated, etc) are used only if non-deprecated ipifs
18097c478bd9Sstevel@tonic-gate 		 * are not available. if the existing ire's ipif is deprecated
18107c478bd9Sstevel@tonic-gate 		 * and the new ipif is non-deprecated, switch to the new ipif
18117c478bd9Sstevel@tonic-gate 		 */
18127c478bd9Sstevel@tonic-gate 		if ((!(ire->ire_ipif->ipif_flags & check_flags)) ||
18137c478bd9Sstevel@tonic-gate 		    (ipif->ipif_flags & check_flags)) {
18147c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
18157c478bd9Sstevel@tonic-gate 			return (irep);
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 		/*
18187c478bd9Sstevel@tonic-gate 		 * Bcast ires exist in pairs. Both have to be deleted,
18197c478bd9Sstevel@tonic-gate 		 * Since we are exclusive we can make the above assertion.
18207c478bd9Sstevel@tonic-gate 		 * The 1st has to be refrele'd since it was ctable_lookup'd.
18217c478bd9Sstevel@tonic-gate 		 */
18227c478bd9Sstevel@tonic-gate 		ASSERT(IAM_WRITER_IPIF(ipif));
18237c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_next->ire_addr == ire->ire_addr);
18247c478bd9Sstevel@tonic-gate 		ire_delete(ire->ire_next);
18257c478bd9Sstevel@tonic-gate 		ire_delete(ire);
18267c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	irep = ire_create_bcast(ipif, addr, irep);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	return (irep);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate uint_t ip_loopback_mtu = IP_LOOPBACK_MTU;
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate /*
18377c478bd9Sstevel@tonic-gate  * This routine is called from ipif_check_bcast_ires and ire_check_bcast.
18387c478bd9Sstevel@tonic-gate  * It leaves all the verifying and deleting to those routines. So it always
18397c478bd9Sstevel@tonic-gate  * creates 2 bcast ires and chains them into the ire array passed in.
18407c478bd9Sstevel@tonic-gate  */
18417c478bd9Sstevel@tonic-gate ire_t **
18427c478bd9Sstevel@tonic-gate ire_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep)
18437c478bd9Sstevel@tonic-gate {
1844f4b3ec61Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
1845f4b3ec61Sdh155122 
18467c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
18477c478bd9Sstevel@tonic-gate 	    (uchar_t *)&addr,			/* dest addr */
18487c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ip_g_all_ones,		/* mask */
18497c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ipif->ipif_src_addr,	/* source addr */
18507c478bd9Sstevel@tonic-gate 	    NULL,				/* no gateway */
18517c478bd9Sstevel@tonic-gate 	    &ipif->ipif_mtu,			/* max frag */
185254da8755Ssowmini 	    NULL,				/* no src nce */
18537c478bd9Sstevel@tonic-gate 	    ipif->ipif_rq,			/* recv-from queue */
18547c478bd9Sstevel@tonic-gate 	    ipif->ipif_wq,			/* send-to queue */
18557c478bd9Sstevel@tonic-gate 	    IRE_BROADCAST,
18567c478bd9Sstevel@tonic-gate 	    ipif,
18577c478bd9Sstevel@tonic-gate 	    0,
18587c478bd9Sstevel@tonic-gate 	    0,
18597c478bd9Sstevel@tonic-gate 	    0,
18607c478bd9Sstevel@tonic-gate 	    0,
186145916cd2Sjpk 	    &ire_uinfo_null,
186245916cd2Sjpk 	    NULL,
1863f4b3ec61Sdh155122 	    NULL,
1864f4b3ec61Sdh155122 	    ipst);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
18677c478bd9Sstevel@tonic-gate 	    (uchar_t *)&addr,			/* dest address */
18687c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ip_g_all_ones,		/* mask */
18697c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ipif->ipif_src_addr,	/* source address */
18707c478bd9Sstevel@tonic-gate 	    NULL,				/* no gateway */
18717c478bd9Sstevel@tonic-gate 	    &ip_loopback_mtu,			/* max frag size */
187254da8755Ssowmini 	    NULL,				/* no src_nce */
18737c478bd9Sstevel@tonic-gate 	    ipif->ipif_rq,			/* recv-from queue */
18747c478bd9Sstevel@tonic-gate 	    NULL,				/* no send-to queue */
18757c478bd9Sstevel@tonic-gate 	    IRE_BROADCAST,			/* Needed for fanout in wput */
18767c478bd9Sstevel@tonic-gate 	    ipif,
18777c478bd9Sstevel@tonic-gate 	    0,
18787c478bd9Sstevel@tonic-gate 	    0,
18797c478bd9Sstevel@tonic-gate 	    0,
18807c478bd9Sstevel@tonic-gate 	    0,
188145916cd2Sjpk 	    &ire_uinfo_null,
188245916cd2Sjpk 	    NULL,
1883f4b3ec61Sdh155122 	    NULL,
1884f4b3ec61Sdh155122 	    ipst);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	return (irep);
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate /*
18907c478bd9Sstevel@tonic-gate  * ire_walk routine to delete or update any IRE_CACHE that might contain
18917c478bd9Sstevel@tonic-gate  * stale information.
18927c478bd9Sstevel@tonic-gate  * The flags state which entries to delete or update.
18937c478bd9Sstevel@tonic-gate  * Garbage collection is done separately using kmem alloc callbacks to
18947c478bd9Sstevel@tonic-gate  * ip_trash_ire_reclaim.
18957c478bd9Sstevel@tonic-gate  * Used for both IPv4 and IPv6. However, IPv6 only uses FLUSH_MTU_TIME
18967c478bd9Sstevel@tonic-gate  * since other stale information is cleaned up using NUD.
18977c478bd9Sstevel@tonic-gate  */
18987c478bd9Sstevel@tonic-gate void
18997c478bd9Sstevel@tonic-gate ire_expire(ire_t *ire, char *arg)
19007c478bd9Sstevel@tonic-gate {
1901f4b3ec61Sdh155122 	ire_expire_arg_t	*ieap = (ire_expire_arg_t *)(uintptr_t)arg;
19027c478bd9Sstevel@tonic-gate 	ill_t			*stq_ill;
1903f4b3ec61Sdh155122 	int			flush_flags = ieap->iea_flush_flag;
1904f4b3ec61Sdh155122 	ip_stack_t		*ipst = ieap->iea_ipst;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if ((flush_flags & FLUSH_REDIRECT_TIME) &&
19076bdb8e66Sdd193516 	    (ire->ire_flags & RTF_DYNAMIC)) {
19087c478bd9Sstevel@tonic-gate 		/* Make sure we delete the corresponding IRE_CACHE */
19097c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_expire: all redirects\n"));
1910f4b3ec61Sdh155122 		ip_rts_rtmsg(RTM_DELETE, ire, 0, ipst);
19117c478bd9Sstevel@tonic-gate 		ire_delete(ire);
1912f4b3ec61Sdh155122 		atomic_dec_32(&ipst->ips_ip_redirect_cnt);
19137c478bd9Sstevel@tonic-gate 		return;
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
19167c478bd9Sstevel@tonic-gate 		return;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	if (flush_flags & FLUSH_ARP_TIME) {
19197c478bd9Sstevel@tonic-gate 		/*
19207c478bd9Sstevel@tonic-gate 		 * Remove all IRE_CACHE.
19217c478bd9Sstevel@tonic-gate 		 * Verify that create time is more than
19227c478bd9Sstevel@tonic-gate 		 * ip_ire_arp_interval milliseconds ago.
19237c478bd9Sstevel@tonic-gate 		 */
1924f4b3ec61Sdh155122 		if (NCE_EXPIRED(ire->ire_nce, ipst)) {
19257c478bd9Sstevel@tonic-gate 			ire_delete(ire);
19267c478bd9Sstevel@tonic-gate 			return;
19277c478bd9Sstevel@tonic-gate 		}
19287c478bd9Sstevel@tonic-gate 	}
19297c478bd9Sstevel@tonic-gate 
1930f4b3ec61Sdh155122 	if (ipst->ips_ip_path_mtu_discovery && (flush_flags & FLUSH_MTU_TIME) &&
19317c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif != NULL)) {
19327c478bd9Sstevel@tonic-gate 		/* Increase pmtu if it is less than the interface mtu */
19337c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
19347c478bd9Sstevel@tonic-gate 		/*
19357c478bd9Sstevel@tonic-gate 		 * If the ipif is a vni (whose mtu is 0, since it's virtual)
19367c478bd9Sstevel@tonic-gate 		 * get the mtu from the sending interfaces' ipif
19377c478bd9Sstevel@tonic-gate 		 */
19387c478bd9Sstevel@tonic-gate 		if (IS_VNI(ire->ire_ipif->ipif_ill)) {
19397c478bd9Sstevel@tonic-gate 			stq_ill = ire->ire_stq->q_ptr;
19407c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(stq_ill->ill_ipif->ipif_mtu,
19417c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
19427c478bd9Sstevel@tonic-gate 		} else {
19437c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
19447c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 		ire->ire_frag_flag |= IPH_DF;
19477c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate }
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate /*
19527c478bd9Sstevel@tonic-gate  * Return any local address.  We use this to target ourselves
19537c478bd9Sstevel@tonic-gate  * when the src address was specified as 'default'.
19547c478bd9Sstevel@tonic-gate  * Preference for IRE_LOCAL entries.
19557c478bd9Sstevel@tonic-gate  */
19567c478bd9Sstevel@tonic-gate ire_t *
1957f4b3ec61Sdh155122 ire_lookup_local(zoneid_t zoneid, ip_stack_t *ipst)
19587c478bd9Sstevel@tonic-gate {
19597c478bd9Sstevel@tonic-gate 	ire_t	*ire;
19607c478bd9Sstevel@tonic-gate 	irb_t	*irb;
19617c478bd9Sstevel@tonic-gate 	ire_t	*maybe = NULL;
19627c478bd9Sstevel@tonic-gate 	int i;
19637c478bd9Sstevel@tonic-gate 
1964f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip_cache_table_size;  i++) {
1965f4b3ec61Sdh155122 		irb = &ipst->ips_ip_cache_table[i];
19667c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
19677c478bd9Sstevel@tonic-gate 			continue;
19687c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_READER);
19697c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
19707c478bd9Sstevel@tonic-gate 			if ((ire->ire_marks & IRE_MARK_CONDEMNED) ||
197145916cd2Sjpk 			    (ire->ire_zoneid != zoneid &&
197245916cd2Sjpk 			    ire->ire_zoneid != ALL_ZONES))
19737c478bd9Sstevel@tonic-gate 				continue;
19747c478bd9Sstevel@tonic-gate 			switch (ire->ire_type) {
19757c478bd9Sstevel@tonic-gate 			case IRE_LOOPBACK:
19767c478bd9Sstevel@tonic-gate 				if (maybe == NULL) {
19777c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
19787c478bd9Sstevel@tonic-gate 					maybe = ire;
19797c478bd9Sstevel@tonic-gate 				}
19807c478bd9Sstevel@tonic-gate 				break;
19817c478bd9Sstevel@tonic-gate 			case IRE_LOCAL:
19827c478bd9Sstevel@tonic-gate 				if (maybe != NULL) {
19837c478bd9Sstevel@tonic-gate 					ire_refrele(maybe);
19847c478bd9Sstevel@tonic-gate 				}
19857c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
19867c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
19877c478bd9Sstevel@tonic-gate 				return (ire);
19887c478bd9Sstevel@tonic-gate 			}
19897c478bd9Sstevel@tonic-gate 		}
19907c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 	return (maybe);
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate /*
19967c478bd9Sstevel@tonic-gate  * If the specified IRE is associated with a particular ILL, return
19977c478bd9Sstevel@tonic-gate  * that ILL pointer (May be called as writer.).
19987c478bd9Sstevel@tonic-gate  *
19997c478bd9Sstevel@tonic-gate  * NOTE : This is not a generic function that can be used always.
20007c478bd9Sstevel@tonic-gate  * This function always returns the ill of the outgoing packets
20017c478bd9Sstevel@tonic-gate  * if this ire is used.
20027c478bd9Sstevel@tonic-gate  */
20037c478bd9Sstevel@tonic-gate ill_t *
200445916cd2Sjpk ire_to_ill(const ire_t *ire)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	ill_t *ill = NULL;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	/*
20097c478bd9Sstevel@tonic-gate 	 * 1) For an IRE_CACHE, ire_ipif is the one where it obtained
20107c478bd9Sstevel@tonic-gate 	 *    the source address from. ire_stq is the one where the
20117c478bd9Sstevel@tonic-gate 	 *    packets will be sent out on. We return that here.
20127c478bd9Sstevel@tonic-gate 	 *
20137c478bd9Sstevel@tonic-gate 	 * 2) IRE_BROADCAST normally has a loopback and a non-loopback
20147c478bd9Sstevel@tonic-gate 	 *    copy and they always exist next to each other with loopback
20157c478bd9Sstevel@tonic-gate 	 *    copy being the first one. If we are called on the non-loopback
20167c478bd9Sstevel@tonic-gate 	 *    copy, return the one pointed by ire_stq. If it was called on
20177c478bd9Sstevel@tonic-gate 	 *    a loopback copy, we still return the one pointed by the next
20187c478bd9Sstevel@tonic-gate 	 *    ire's ire_stq pointer i.e the one pointed by the non-loopback
20197c478bd9Sstevel@tonic-gate 	 *    copy. We don't want use ire_ipif as it might represent the
20207c478bd9Sstevel@tonic-gate 	 *    source address (if we borrow source addresses for
20217c478bd9Sstevel@tonic-gate 	 *    IRE_BROADCASTS in the future).
20227c478bd9Sstevel@tonic-gate 	 *    However if an interface is currently coming up, the above
20237c478bd9Sstevel@tonic-gate 	 *    condition may not hold during that period since the ires
20247c478bd9Sstevel@tonic-gate 	 *    are added one at a time. Thus one of the pair could have been
20257c478bd9Sstevel@tonic-gate 	 *    added and the other not yet added.
20262bc4236aSnordmark 	 * 3) For many other IREs (e.g., IRE_LOCAL), ire_rfq indicates the ill.
20272bc4236aSnordmark 	 * 4) For all others return the ones pointed by ire_ipif->ipif_ill.
20282bc4236aSnordmark 	 *    That handles IRE_LOOPBACK.
20297c478bd9Sstevel@tonic-gate 	 */
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
20327c478bd9Sstevel@tonic-gate 		ill = (ill_t *)ire->ire_stq->q_ptr;
20337c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_BROADCAST) {
20347c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
20357c478bd9Sstevel@tonic-gate 			ill = (ill_t *)ire->ire_stq->q_ptr;
20367c478bd9Sstevel@tonic-gate 		} else {
20377c478bd9Sstevel@tonic-gate 			ire_t  *ire_next;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 			ire_next = ire->ire_next;
20407c478bd9Sstevel@tonic-gate 			if (ire_next != NULL &&
20417c478bd9Sstevel@tonic-gate 			    ire_next->ire_type == IRE_BROADCAST &&
20427c478bd9Sstevel@tonic-gate 			    ire_next->ire_addr == ire->ire_addr &&
20437c478bd9Sstevel@tonic-gate 			    ire_next->ire_ipif == ire->ire_ipif) {
20447c478bd9Sstevel@tonic-gate 				ill = (ill_t *)ire_next->ire_stq->q_ptr;
20457c478bd9Sstevel@tonic-gate 			}
20467c478bd9Sstevel@tonic-gate 		}
20472bc4236aSnordmark 	} else if (ire->ire_rfq != NULL) {
20482bc4236aSnordmark 		ill = ire->ire_rfq->q_ptr;
20497c478bd9Sstevel@tonic-gate 	} else if (ire->ire_ipif != NULL) {
20507c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 	return (ill);
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate /* Arrange to call the specified function for every IRE in the world. */
20567c478bd9Sstevel@tonic-gate void
2057f4b3ec61Sdh155122 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
20587c478bd9Sstevel@tonic-gate {
2059f4b3ec61Sdh155122 	ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
20607c478bd9Sstevel@tonic-gate }
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate void
2063f4b3ec61Sdh155122 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
20647c478bd9Sstevel@tonic-gate {
2065f4b3ec61Sdh155122 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
20667c478bd9Sstevel@tonic-gate }
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate void
2069f4b3ec61Sdh155122 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
20707c478bd9Sstevel@tonic-gate {
2071f4b3ec61Sdh155122 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
20727c478bd9Sstevel@tonic-gate }
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate /*
20757c478bd9Sstevel@tonic-gate  * Walk a particular version. version == 0 means both v4 and v6.
20767c478bd9Sstevel@tonic-gate  */
20777c478bd9Sstevel@tonic-gate static void
2078f4b3ec61Sdh155122 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
2079f4b3ec61Sdh155122     ip_stack_t *ipst)
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
2082c793af95Ssangeeta 		/*
2083c793af95Ssangeeta 		 * ip_forwarding_table variable doesn't matter for IPv4 since
2084f4b3ec61Sdh155122 		 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
2085c793af95Ssangeeta 		 */
20867c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
2087c793af95Ssangeeta 		    0, NULL,
2088f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table,
2089f4b3ec61Sdh155122 		    NULL, zoneid, ipst);
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
20927c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
2093f4b3ec61Sdh155122 		    ipst->ips_ip6_ftable_hash_size,
2094f4b3ec61Sdh155122 		    ipst->ips_ip_forwarding_table_v6,
2095f4b3ec61Sdh155122 		    ipst->ips_ip6_cache_table_size,
2096f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table_v6, NULL, zoneid, ipst);
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate }
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate /*
21017c478bd9Sstevel@tonic-gate  * Arrange to call the specified
21027c478bd9Sstevel@tonic-gate  * function for every IRE that matches the ill.
21037c478bd9Sstevel@tonic-gate  */
21047c478bd9Sstevel@tonic-gate void
210545916cd2Sjpk ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
21067c478bd9Sstevel@tonic-gate     ill_t *ill)
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, 0, ill);
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate void
211245916cd2Sjpk ire_walk_ill_v4(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
21137c478bd9Sstevel@tonic-gate     ill_t *ill)
21147c478bd9Sstevel@tonic-gate {
21157c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV4_VERSION,
21167c478bd9Sstevel@tonic-gate 	    ill);
21177c478bd9Sstevel@tonic-gate }
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate void
212045916cd2Sjpk ire_walk_ill_v6(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
21217c478bd9Sstevel@tonic-gate     ill_t *ill)
21227c478bd9Sstevel@tonic-gate {
21237c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV6_VERSION,
21247c478bd9Sstevel@tonic-gate 	    ill);
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate /*
21287c478bd9Sstevel@tonic-gate  * Walk a particular ill and version. version == 0 means both v4 and v6.
21297c478bd9Sstevel@tonic-gate  */
21307c478bd9Sstevel@tonic-gate static void
21317c478bd9Sstevel@tonic-gate ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
213245916cd2Sjpk     void *arg, uchar_t vers, ill_t *ill)
21337c478bd9Sstevel@tonic-gate {
2134f4b3ec61Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
2135f4b3ec61Sdh155122 
21367c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
21377c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2138c793af95Ssangeeta 		    IP_MASK_TABLE_SIZE, 0,
2139f4b3ec61Sdh155122 		    NULL, ipst->ips_ip_cache_table_size,
2140f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table, ill, ALL_ZONES, ipst);
21417c478bd9Sstevel@tonic-gate 	}
21427c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
21437c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2144f4b3ec61Sdh155122 		    IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
2145f4b3ec61Sdh155122 		    ipst->ips_ip_forwarding_table_v6,
2146f4b3ec61Sdh155122 		    ipst->ips_ip6_cache_table_size,
2147f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table_v6, ill, ALL_ZONES, ipst);
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate }
21507c478bd9Sstevel@tonic-gate 
2151c793af95Ssangeeta boolean_t
21527c478bd9Sstevel@tonic-gate ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
2153f4b3ec61Sdh155122     ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
21547c478bd9Sstevel@tonic-gate {
21557c478bd9Sstevel@tonic-gate 	ill_t *ire_stq_ill = NULL;
21567c478bd9Sstevel@tonic-gate 	ill_t *ire_ipif_ill = NULL;
21577c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
21607c478bd9Sstevel@tonic-gate 	/*
21617c478bd9Sstevel@tonic-gate 	 * 1) MATCH_IRE_WQ : Used specifically to match on ire_stq.
21627c478bd9Sstevel@tonic-gate 	 *    The fast path update uses this to make sure it does not
21637c478bd9Sstevel@tonic-gate 	 *    update the fast path header of interface X with the fast
21647c478bd9Sstevel@tonic-gate 	 *    path updates it recieved on interface Y.  It is similar
21657c478bd9Sstevel@tonic-gate 	 *    in handling DL_NOTE_FASTPATH_FLUSH.
21667c478bd9Sstevel@tonic-gate 	 *
21677c478bd9Sstevel@tonic-gate 	 * 2) MATCH_IRE_ILL/MATCH_IRE_ILL_GROUP : We match both on ill
21687c478bd9Sstevel@tonic-gate 	 *    pointed by ire_stq and ire_ipif. Only in the case of
21697c478bd9Sstevel@tonic-gate 	 *    IRE_CACHEs can ire_stq and ire_ipif be pointing to
21707c478bd9Sstevel@tonic-gate 	 *    different ills. But we want to keep this function generic
21717c478bd9Sstevel@tonic-gate 	 *    enough for future use. So, we always try to match on both.
21727c478bd9Sstevel@tonic-gate 	 *    The only caller of this function ire_walk_ill_tables, will
21737c478bd9Sstevel@tonic-gate 	 *    call "func" after we return from this function. We expect
21747c478bd9Sstevel@tonic-gate 	 *    "func" to do the right filtering of ires in this case.
21757c478bd9Sstevel@tonic-gate 	 *
21767c478bd9Sstevel@tonic-gate 	 * NOTE : In the case of MATCH_IRE_ILL_GROUP, groups
21777c478bd9Sstevel@tonic-gate 	 * pointed by ire_stq and ire_ipif should always be the same.
21787c478bd9Sstevel@tonic-gate 	 * So, we just match on only one of them.
21797c478bd9Sstevel@tonic-gate 	 */
21807c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
21817c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL)
21827c478bd9Sstevel@tonic-gate 			ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
21837c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL)
21847c478bd9Sstevel@tonic-gate 			ire_ipif_ill = ire->ire_ipif->ipif_ill;
21857c478bd9Sstevel@tonic-gate 		if (ire_stq_ill != NULL)
21867c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_stq_ill->ill_group;
21877c478bd9Sstevel@tonic-gate 		if ((ire_ill_group == NULL) && (ire_ipif_ill != NULL))
21887c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ipif_ill->ill_group;
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES) {
21927c478bd9Sstevel@tonic-gate 		/*
21937c478bd9Sstevel@tonic-gate 		 * We're walking the IREs for a specific zone. The only relevant
21947c478bd9Sstevel@tonic-gate 		 * IREs are:
21957c478bd9Sstevel@tonic-gate 		 * - all IREs with a matching ire_zoneid
21967c478bd9Sstevel@tonic-gate 		 * - all IRE_OFFSUBNETs as they're shared across all zones
21977c478bd9Sstevel@tonic-gate 		 * - IRE_INTERFACE IREs for interfaces with a usable source addr
21987c478bd9Sstevel@tonic-gate 		 *   with a matching zone
21997c478bd9Sstevel@tonic-gate 		 * - IRE_DEFAULTs with a gateway reachable from the zone
22007c478bd9Sstevel@tonic-gate 		 * We should really match on IRE_OFFSUBNETs and IRE_DEFAULTs
22017c478bd9Sstevel@tonic-gate 		 * using the same rule; but the above rules are consistent with
22027c478bd9Sstevel@tonic-gate 		 * the behavior of ire_ftable_lookup[_v6]() so that all the
22037c478bd9Sstevel@tonic-gate 		 * routes that can be matched during lookup are also matched
22047c478bd9Sstevel@tonic-gate 		 * here.
22057c478bd9Sstevel@tonic-gate 		 */
220645916cd2Sjpk 		if (zoneid != ire->ire_zoneid && ire->ire_zoneid != ALL_ZONES) {
22077c478bd9Sstevel@tonic-gate 			/*
22087c478bd9Sstevel@tonic-gate 			 * Note, IRE_INTERFACE can have the stq as NULL. For
22097c478bd9Sstevel@tonic-gate 			 * example, if the default multicast route is tied to
22107c478bd9Sstevel@tonic-gate 			 * the loopback address.
22117c478bd9Sstevel@tonic-gate 			 */
22127c478bd9Sstevel@tonic-gate 			if ((ire->ire_type & IRE_INTERFACE) &&
22137c478bd9Sstevel@tonic-gate 			    (ire->ire_stq != NULL)) {
22147c478bd9Sstevel@tonic-gate 				ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
22157c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV4_VERSION) {
22167c478bd9Sstevel@tonic-gate 					if (!ipif_usesrc_avail(ire_stq_ill,
22177c478bd9Sstevel@tonic-gate 					    zoneid))
22187c478bd9Sstevel@tonic-gate 						/* No usable src addr in zone */
22197c478bd9Sstevel@tonic-gate 						return (B_FALSE);
22207c478bd9Sstevel@tonic-gate 				} else if (ire_stq_ill->ill_usesrc_ifindex
22217c478bd9Sstevel@tonic-gate 				    != 0) {
22227c478bd9Sstevel@tonic-gate 					/*
22237c478bd9Sstevel@tonic-gate 					 * For IPv6 use ipif_select_source_v6()
22247c478bd9Sstevel@tonic-gate 					 * so the right scope selection is done
22257c478bd9Sstevel@tonic-gate 					 */
22267c478bd9Sstevel@tonic-gate 					ipif_t *src_ipif;
22277c478bd9Sstevel@tonic-gate 					src_ipif =
22287c478bd9Sstevel@tonic-gate 					    ipif_select_source_v6(ire_stq_ill,
2229309fa174Srk129064 					    &ire->ire_addr_v6, RESTRICT_TO_NONE,
22307c478bd9Sstevel@tonic-gate 					    IPV6_PREFER_SRC_DEFAULT,
22317c478bd9Sstevel@tonic-gate 					    zoneid);
22327c478bd9Sstevel@tonic-gate 					if (src_ipif != NULL) {
22337c478bd9Sstevel@tonic-gate 						ipif_refrele(src_ipif);
22347c478bd9Sstevel@tonic-gate 					} else {
22357c478bd9Sstevel@tonic-gate 						return (B_FALSE);
22367c478bd9Sstevel@tonic-gate 					}
22377c478bd9Sstevel@tonic-gate 				} else {
22387c478bd9Sstevel@tonic-gate 					return (B_FALSE);
22397c478bd9Sstevel@tonic-gate 				}
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 			} else if (!(ire->ire_type & IRE_OFFSUBNET)) {
22427c478bd9Sstevel@tonic-gate 				return (B_FALSE);
22437c478bd9Sstevel@tonic-gate 			}
22447c478bd9Sstevel@tonic-gate 		}
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 		/*
22477c478bd9Sstevel@tonic-gate 		 * Match all default routes from the global zone, irrespective
22485597b60aSnordmark 		 * of reachability. For a non-global zone only match those
22495597b60aSnordmark 		 * where ire_gateway_addr has a IRE_INTERFACE for the zoneid.
22507c478bd9Sstevel@tonic-gate 		 */
22517c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_DEFAULT && zoneid != GLOBAL_ZONEID) {
22527c478bd9Sstevel@tonic-gate 			int ire_match_flags = 0;
22537c478bd9Sstevel@tonic-gate 			in6_addr_t gw_addr_v6;
22547c478bd9Sstevel@tonic-gate 			ire_t *rire;
22557c478bd9Sstevel@tonic-gate 
22565597b60aSnordmark 			ire_match_flags |= MATCH_IRE_TYPE;
22577c478bd9Sstevel@tonic-gate 			if (ire->ire_ipif != NULL) {
22587c478bd9Sstevel@tonic-gate 				ire_match_flags |= MATCH_IRE_ILL_GROUP;
22597c478bd9Sstevel@tonic-gate 			}
22607c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
22617c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup(ire->ire_gateway_addr,
22625597b60aSnordmark 				    0, 0, IRE_INTERFACE, ire->ire_ipif, NULL,
2263f4b3ec61Sdh155122 				    zoneid, NULL, ire_match_flags, ipst);
22647c478bd9Sstevel@tonic-gate 			} else {
22657c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
22667c478bd9Sstevel@tonic-gate 				mutex_enter(&ire->ire_lock);
22677c478bd9Sstevel@tonic-gate 				gw_addr_v6 = ire->ire_gateway_addr_v6;
22687c478bd9Sstevel@tonic-gate 				mutex_exit(&ire->ire_lock);
22697c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup_v6(&gw_addr_v6,
22705597b60aSnordmark 				    NULL, NULL, IRE_INTERFACE, ire->ire_ipif,
2271f4b3ec61Sdh155122 				    NULL, zoneid, NULL, ire_match_flags, ipst);
22727c478bd9Sstevel@tonic-gate 			}
22737c478bd9Sstevel@tonic-gate 			if (rire == NULL) {
22747c478bd9Sstevel@tonic-gate 				return (B_FALSE);
22757c478bd9Sstevel@tonic-gate 			}
22767c478bd9Sstevel@tonic-gate 			ire_refrele(rire);
22777c478bd9Sstevel@tonic-gate 		}
22787c478bd9Sstevel@tonic-gate 	}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
22817c478bd9Sstevel@tonic-gate 	    (ire->ire_type & ire_type)) &&
22827c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_WQ)) ||
22837c478bd9Sstevel@tonic-gate 	    (ire->ire_stq == ill->ill_wq)) &&
22847c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
22857c478bd9Sstevel@tonic-gate 	    (ire_stq_ill == ill || ire_ipif_ill == ill)) &&
22867c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
22877c478bd9Sstevel@tonic-gate 	    (ire_stq_ill == ill) || (ire_ipif_ill == ill) ||
22887c478bd9Sstevel@tonic-gate 	    (ire_ill_group != NULL &&
22897c478bd9Sstevel@tonic-gate 	    ire_ill_group == ill->ill_group))) {
22907c478bd9Sstevel@tonic-gate 		return (B_TRUE);
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 	return (B_FALSE);
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
2295c793af95Ssangeeta int
2296c793af95Ssangeeta rtfunc(struct radix_node *rn, void *arg)
2297c793af95Ssangeeta {
2298c793af95Ssangeeta 	struct rtfuncarg *rtf = arg;
2299c793af95Ssangeeta 	struct rt_entry *rt;
2300c793af95Ssangeeta 	irb_t *irb;
2301c793af95Ssangeeta 	ire_t *ire;
2302c793af95Ssangeeta 	boolean_t ret;
2303c793af95Ssangeeta 
2304c793af95Ssangeeta 	rt = (struct rt_entry *)rn;
2305c793af95Ssangeeta 	ASSERT(rt != NULL);
2306c793af95Ssangeeta 	irb = &rt->rt_irb;
2307c793af95Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2308c793af95Ssangeeta 		if ((rtf->rt_match_flags != 0) ||
2309c793af95Ssangeeta 		    (rtf->rt_zoneid != ALL_ZONES)) {
2310c793af95Ssangeeta 			ret = ire_walk_ill_match(rtf->rt_match_flags,
2311c793af95Ssangeeta 			    rtf->rt_ire_type, ire,
2312f4b3ec61Sdh155122 			    rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
2313c793af95Ssangeeta 		} else
2314c793af95Ssangeeta 			ret = B_TRUE;
2315c793af95Ssangeeta 		if (ret)
2316c793af95Ssangeeta 			(*rtf->rt_func)(ire, rtf->rt_arg);
2317c793af95Ssangeeta 	}
2318c793af95Ssangeeta 	return (0);
2319c793af95Ssangeeta }
2320c793af95Ssangeeta 
23217c478bd9Sstevel@tonic-gate /*
23227c478bd9Sstevel@tonic-gate  * Walk the ftable and the ctable entries that match the ill.
23237c478bd9Sstevel@tonic-gate  */
2324c793af95Ssangeeta void
23257c478bd9Sstevel@tonic-gate ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
232645916cd2Sjpk     void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
2327f4b3ec61Sdh155122     size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid,
2328f4b3ec61Sdh155122     ip_stack_t *ipst)
23297c478bd9Sstevel@tonic-gate {
23307c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
23317c478bd9Sstevel@tonic-gate 	irb_t	*irb;
23327c478bd9Sstevel@tonic-gate 	ire_t	*ire;
23337c478bd9Sstevel@tonic-gate 	int i, j;
23347c478bd9Sstevel@tonic-gate 	boolean_t ret;
2335c793af95Ssangeeta 	struct rtfuncarg rtfarg;
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
23387c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
23397c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
23407c478bd9Sstevel@tonic-gate 	/*
23417c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the forwarding table if there
23427c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_FORWARDTABLE
23437c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
23447c478bd9Sstevel@tonic-gate 	 */
23457c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
23467c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_FORWARDTABLE) != 0)) {
2347c793af95Ssangeeta 		/* knobs such that routine is called only for v6 case */
2348f4b3ec61Sdh155122 		if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
23497c478bd9Sstevel@tonic-gate 			for (i = (ftbl_sz - 1);  i >= 0; i--) {
23507c478bd9Sstevel@tonic-gate 				if ((irb_ptr = ipftbl[i]) == NULL)
23517c478bd9Sstevel@tonic-gate 					continue;
23527c478bd9Sstevel@tonic-gate 				for (j = 0; j < htbl_sz; j++) {
23537c478bd9Sstevel@tonic-gate 					irb = &irb_ptr[j];
23547c478bd9Sstevel@tonic-gate 					if (irb->irb_ire == NULL)
23557c478bd9Sstevel@tonic-gate 						continue;
2356c793af95Ssangeeta 
23577c478bd9Sstevel@tonic-gate 					IRB_REFHOLD(irb);
23587c478bd9Sstevel@tonic-gate 					for (ire = irb->irb_ire; ire != NULL;
23597c478bd9Sstevel@tonic-gate 					    ire = ire->ire_next) {
23607c478bd9Sstevel@tonic-gate 						if (match_flags == 0 &&
23617c478bd9Sstevel@tonic-gate 						    zoneid == ALL_ZONES) {
23627c478bd9Sstevel@tonic-gate 							ret = B_TRUE;
23637c478bd9Sstevel@tonic-gate 						} else {
2364c793af95Ssangeeta 							ret =
2365c793af95Ssangeeta 							    ire_walk_ill_match(
2366c793af95Ssangeeta 							    match_flags,
2367c793af95Ssangeeta 							    ire_type, ire, ill,
2368f4b3ec61Sdh155122 							    zoneid, ipst);
23697c478bd9Sstevel@tonic-gate 						}
23707c478bd9Sstevel@tonic-gate 						if (ret)
23717c478bd9Sstevel@tonic-gate 							(*func)(ire, arg);
23727c478bd9Sstevel@tonic-gate 					}
23737c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb);
23747c478bd9Sstevel@tonic-gate 				}
23757c478bd9Sstevel@tonic-gate 			}
2376c793af95Ssangeeta 		} else {
2377c793af95Ssangeeta 			(void) memset(&rtfarg, 0, sizeof (rtfarg));
2378c793af95Ssangeeta 			rtfarg.rt_func = func;
2379c793af95Ssangeeta 			rtfarg.rt_arg = arg;
2380c793af95Ssangeeta 			if (match_flags != 0) {
2381c793af95Ssangeeta 				rtfarg.rt_match_flags = match_flags;
2382c793af95Ssangeeta 			}
2383c793af95Ssangeeta 			rtfarg.rt_ire_type = ire_type;
2384c793af95Ssangeeta 			rtfarg.rt_ill = ill;
2385c793af95Ssangeeta 			rtfarg.rt_zoneid = zoneid;
2386f4b3ec61Sdh155122 			rtfarg.rt_ipst = ipst;	/* No netstack_hold */
2387f4b3ec61Sdh155122 			(void) ipst->ips_ip_ftable->rnh_walktree_mt(
2388f4b3ec61Sdh155122 			    ipst->ips_ip_ftable,
2389f4b3ec61Sdh155122 			    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
2390c793af95Ssangeeta 		}
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	/*
23947c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the cache table if there
23957c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_CACHETABLE
23967c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
23977c478bd9Sstevel@tonic-gate 	 */
23987c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
23997c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_CACHETABLE) != 0)) {
24007c478bd9Sstevel@tonic-gate 		for (i = 0; i < ctbl_sz;  i++) {
24017c478bd9Sstevel@tonic-gate 			irb = &ipctbl[i];
24027c478bd9Sstevel@tonic-gate 			if (irb->irb_ire == NULL)
24037c478bd9Sstevel@tonic-gate 				continue;
24047c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
24057c478bd9Sstevel@tonic-gate 			for (ire = irb->irb_ire; ire != NULL;
24067c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
24077c478bd9Sstevel@tonic-gate 				if (match_flags == 0 && zoneid == ALL_ZONES) {
24087c478bd9Sstevel@tonic-gate 					ret = B_TRUE;
24097c478bd9Sstevel@tonic-gate 				} else {
24107c478bd9Sstevel@tonic-gate 					ret = ire_walk_ill_match(
24117c478bd9Sstevel@tonic-gate 					    match_flags, ire_type,
2412f4b3ec61Sdh155122 					    ire, ill, zoneid, ipst);
24137c478bd9Sstevel@tonic-gate 				}
24147c478bd9Sstevel@tonic-gate 				if (ret)
24157c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
24167c478bd9Sstevel@tonic-gate 			}
24177c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
24187c478bd9Sstevel@tonic-gate 		}
24197c478bd9Sstevel@tonic-gate 	}
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate /*
24237c478bd9Sstevel@tonic-gate  * This function takes a mask and returns
24247c478bd9Sstevel@tonic-gate  * number of bits set in the mask. If no
24257c478bd9Sstevel@tonic-gate  * bit is set it returns 0.
24267c478bd9Sstevel@tonic-gate  * Assumes a contiguous mask.
24277c478bd9Sstevel@tonic-gate  */
24287c478bd9Sstevel@tonic-gate int
24297c478bd9Sstevel@tonic-gate ip_mask_to_plen(ipaddr_t mask)
24307c478bd9Sstevel@tonic-gate {
24317c478bd9Sstevel@tonic-gate 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
24327c478bd9Sstevel@tonic-gate }
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate /*
24357c478bd9Sstevel@tonic-gate  * Convert length for a mask to the mask.
24367c478bd9Sstevel@tonic-gate  */
24377c478bd9Sstevel@tonic-gate ipaddr_t
24387c478bd9Sstevel@tonic-gate ip_plen_to_mask(uint_t masklen)
24397c478bd9Sstevel@tonic-gate {
24407c478bd9Sstevel@tonic-gate 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
24417c478bd9Sstevel@tonic-gate }
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate void
24447c478bd9Sstevel@tonic-gate ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
2447f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	ill_list[0] = ire->ire_stq != NULL ? ire->ire_stq->q_ptr : NULL;
24507c478bd9Sstevel@tonic-gate 	ill_list[1] = ire->ire_ipif != NULL ? ire->ire_ipif->ipif_ill : NULL;
24517c478bd9Sstevel@tonic-gate 	ill_unlock_ills(ill_list, NUM_ILLS);
24527c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
2453f4b3ec61Sdh155122 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
24547c478bd9Sstevel@tonic-gate }
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate /*
24577c478bd9Sstevel@tonic-gate  * ire_add_v[46] atomically make sure that the ipif or ill associated
24587c478bd9Sstevel@tonic-gate  * with the new ire being added is stable and not IPIF_CHANGING or ILL_CHANGING
24597c478bd9Sstevel@tonic-gate  * before adding the ire to the table. This ensures that we don't create
24607c478bd9Sstevel@tonic-gate  * new IRE_CACHEs with stale values for parameters that are passed to
24617c478bd9Sstevel@tonic-gate  * ire_create such as ire_max_frag. Note that ire_create() is passed a pointer
24627c478bd9Sstevel@tonic-gate  * to the ipif_mtu, and not the value. The actual value is derived from the
24637c478bd9Sstevel@tonic-gate  * parent ire or ipif under the bucket lock.
24647c478bd9Sstevel@tonic-gate  */
24657c478bd9Sstevel@tonic-gate int
24667c478bd9Sstevel@tonic-gate ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q, mblk_t *mp,
24677c478bd9Sstevel@tonic-gate     ipsq_func_t func)
24687c478bd9Sstevel@tonic-gate {
24697c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
24707c478bd9Sstevel@tonic-gate 	ill_t	*ipif_ill;
24717c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
24727c478bd9Sstevel@tonic-gate 	int	cnt = NUM_ILLS;
24737c478bd9Sstevel@tonic-gate 	int	error = 0;
24747c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
2475f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	ill_list[0] = stq_ill = ire->ire_stq !=
24787c478bd9Sstevel@tonic-gate 	    NULL ? ire->ire_stq->q_ptr : NULL;
24797c478bd9Sstevel@tonic-gate 	ill_list[1] = ipif_ill = ire->ire_ipif !=
24807c478bd9Sstevel@tonic-gate 	    NULL ? ire->ire_ipif->ipif_ill : NULL;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	ASSERT((q != NULL && mp != NULL && func != NULL) ||
24837c478bd9Sstevel@tonic-gate 	    (q == NULL && mp == NULL && func == NULL));
2484f4b3ec61Sdh155122 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
24857c478bd9Sstevel@tonic-gate 	GRAB_CONN_LOCK(q);
24867c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
24877c478bd9Sstevel@tonic-gate 	ill_lock_ills(ill_list, cnt);
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	/*
24907c478bd9Sstevel@tonic-gate 	 * While the IRE is in the process of being added, a user may have
24917c478bd9Sstevel@tonic-gate 	 * invoked the ifconfig usesrc option on the stq_ill to make it a
24927c478bd9Sstevel@tonic-gate 	 * usesrc client ILL. Check for this possibility here, if it is true
24937c478bd9Sstevel@tonic-gate 	 * then we fail adding the IRE_CACHE. Another check is to make sure
24947c478bd9Sstevel@tonic-gate 	 * that an ipif_ill of an IRE_CACHE being added is not part of a usesrc
24957c478bd9Sstevel@tonic-gate 	 * group. The ill_g_usesrc_lock is released in ire_atomic_end
24967c478bd9Sstevel@tonic-gate 	 */
24977c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHE) &&
24987c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_USESRC_CHECK)) {
24997c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_usesrc_ifindex != 0) {
25007c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_usesrc_grp_next != NULL);
25017c478bd9Sstevel@tonic-gate 			if ((ipif_ill->ill_phyint->phyint_ifindex !=
25027c478bd9Sstevel@tonic-gate 			    stq_ill->ill_usesrc_ifindex) ||
25037c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_grp_next == NULL) ||
25047c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_ifindex != 0)) {
25057c478bd9Sstevel@tonic-gate 				error = EINVAL;
25067c478bd9Sstevel@tonic-gate 				goto done;
25077c478bd9Sstevel@tonic-gate 			}
25087c478bd9Sstevel@tonic-gate 		} else if (ipif_ill->ill_usesrc_grp_next != NULL) {
25097c478bd9Sstevel@tonic-gate 			error = EINVAL;
25107c478bd9Sstevel@tonic-gate 			goto done;
25117c478bd9Sstevel@tonic-gate 		}
25127c478bd9Sstevel@tonic-gate 	}
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	/*
25157c478bd9Sstevel@tonic-gate 	 * IPMP flag settings happen without taking the exclusive route
25167c478bd9Sstevel@tonic-gate 	 * in ip_sioctl_flags. So we need to make an atomic check here
25177c478bd9Sstevel@tonic-gate 	 * for FAILED/OFFLINE/INACTIVE flags or if it has hit the
25187c478bd9Sstevel@tonic-gate 	 * FAILBACK=no case.
25197c478bd9Sstevel@tonic-gate 	 */
25207c478bd9Sstevel@tonic-gate 	if ((stq_ill != NULL) && !IAM_WRITER_ILL(stq_ill)) {
25217c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_state_flags & ILL_CHANGING) {
25227c478bd9Sstevel@tonic-gate 			ill = stq_ill;
25237c478bd9Sstevel@tonic-gate 			error = EAGAIN;
25247c478bd9Sstevel@tonic-gate 		} else if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
25257c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
25267c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
25277c478bd9Sstevel@tonic-gate 			error = EINVAL;
25287c478bd9Sstevel@tonic-gate 		}
25297c478bd9Sstevel@tonic-gate 		goto done;
25307c478bd9Sstevel@tonic-gate 	}
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	/*
25337c478bd9Sstevel@tonic-gate 	 * We don't check for OFFLINE/FAILED in this case because
25347c478bd9Sstevel@tonic-gate 	 * the source address selection logic (ipif_select_source)
25357c478bd9Sstevel@tonic-gate 	 * may still select a source address from such an ill. The
25367c478bd9Sstevel@tonic-gate 	 * assumption is that these addresses will be moved by in.mpathd
25377c478bd9Sstevel@tonic-gate 	 * soon. (i.e. this is a race). However link local addresses
25387c478bd9Sstevel@tonic-gate 	 * will not move and hence ipif_select_source_v6 tries to avoid
25397c478bd9Sstevel@tonic-gate 	 * FAILED ills. Please see ipif_select_source_v6 for more info
25407c478bd9Sstevel@tonic-gate 	 */
25417c478bd9Sstevel@tonic-gate 	if ((ipif_ill != NULL) && !IAM_WRITER_ILL(ipif_ill) &&
25427c478bd9Sstevel@tonic-gate 	    (ipif_ill->ill_state_flags & ILL_CHANGING)) {
25437c478bd9Sstevel@tonic-gate 		ill = ipif_ill;
25447c478bd9Sstevel@tonic-gate 		error = EAGAIN;
25457c478bd9Sstevel@tonic-gate 		goto done;
25467c478bd9Sstevel@tonic-gate 	}
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	if ((ire->ire_ipif != NULL) && !IAM_WRITER_IPIF(ire->ire_ipif) &&
25497c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif->ipif_state_flags & IPIF_CHANGING)) {
25507c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
25517c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
25527c478bd9Sstevel@tonic-gate 		error = EAGAIN;
25537c478bd9Sstevel@tonic-gate 		goto done;
25547c478bd9Sstevel@tonic-gate 	}
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate done:
25577c478bd9Sstevel@tonic-gate 	if (error == EAGAIN && ILL_CAN_WAIT(ill, q)) {
25587c478bd9Sstevel@tonic-gate 		ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
25597c478bd9Sstevel@tonic-gate 		mutex_enter(&ipsq->ipsq_lock);
25607c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
25617c478bd9Sstevel@tonic-gate 		ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
25627c478bd9Sstevel@tonic-gate 		mutex_exit(&ipsq->ipsq_lock);
25637c478bd9Sstevel@tonic-gate 		error = EINPROGRESS;
25647c478bd9Sstevel@tonic-gate 	} else if (error != 0) {
25657c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
25667c478bd9Sstevel@tonic-gate 	}
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	RELEASE_CONN_LOCK(q);
25697c478bd9Sstevel@tonic-gate 	return (error);
25707c478bd9Sstevel@tonic-gate }
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate /*
25737c478bd9Sstevel@tonic-gate  * Add a fully initialized IRE to an appropriate table based on
25747c478bd9Sstevel@tonic-gate  * ire_type.
2575c793af95Ssangeeta  *
2576c793af95Ssangeeta  * allow_unresolved == B_FALSE indicates a legacy code-path call
2577c793af95Ssangeeta  * that has prohibited the addition of incomplete ire's. If this
2578c793af95Ssangeeta  * parameter is set, and we find an nce that is in a state other
2579c793af95Ssangeeta  * than ND_REACHABLE, we fail the add. Note that nce_state could be
2580b9c344b3Ssowmini  * something other than ND_REACHABLE if the nce had just expired and
2581b9c344b3Ssowmini  * the ire_create preceding the ire_add added a new ND_INITIAL nce.
25827c478bd9Sstevel@tonic-gate  */
25837c478bd9Sstevel@tonic-gate int
2584c793af95Ssangeeta ire_add(ire_t **irep, queue_t *q, mblk_t *mp, ipsq_func_t func,
2585c793af95Ssangeeta     boolean_t allow_unresolved)
25867c478bd9Sstevel@tonic-gate {
25877c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
25887c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
25897c478bd9Sstevel@tonic-gate 	ill_t	*ill;
25907c478bd9Sstevel@tonic-gate 	ipif_t	*ipif = NULL;
25917c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
25927c478bd9Sstevel@tonic-gate 	ire_t	*ire = *irep;
25937c478bd9Sstevel@tonic-gate 	int	error;
2594dc3879f9Sjarrett 	boolean_t ire_is_mblk = B_FALSE;
2595dc3879f9Sjarrett 	tsol_gcgrp_t *gcgrp = NULL;
2596dc3879f9Sjarrett 	tsol_gcgrp_addr_t ga;
2597f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	/* get ready for the day when original ire is not created as mblk */
26007c478bd9Sstevel@tonic-gate 	if (ire->ire_mp != NULL) {
2601dc3879f9Sjarrett 		ire_is_mblk = B_TRUE;
26027c478bd9Sstevel@tonic-gate 		/* Copy the ire to a kmem_alloc'ed area */
26037c478bd9Sstevel@tonic-gate 		ire1 = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
26047c478bd9Sstevel@tonic-gate 		if (ire1 == NULL) {
26057c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add: alloc failed\n"));
26067c478bd9Sstevel@tonic-gate 			ire_delete(ire);
26077c478bd9Sstevel@tonic-gate 			*irep = NULL;
26087c478bd9Sstevel@tonic-gate 			return (ENOMEM);
26097c478bd9Sstevel@tonic-gate 		}
2610c793af95Ssangeeta 		ire->ire_marks &= ~IRE_MARK_UNCACHED;
26117c478bd9Sstevel@tonic-gate 		*ire1 = *ire;
26127c478bd9Sstevel@tonic-gate 		ire1->ire_mp = NULL;
2613c793af95Ssangeeta 		ire1->ire_stq_ifindex = 0;
26147c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
26157c478bd9Sstevel@tonic-gate 		ire = ire1;
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
26187c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
26217c478bd9Sstevel@tonic-gate 		/*
26227c478bd9Sstevel@tonic-gate 		 * If this interface is FAILED, or INACTIVE or has hit
26237c478bd9Sstevel@tonic-gate 		 * the FAILBACK=no case, we create IRE_CACHES marked
26247c478bd9Sstevel@tonic-gate 		 * HIDDEN for some special cases e.g. bind to
26257c478bd9Sstevel@tonic-gate 		 * IPIF_NOFAILOVER address etc. So, if this interface
26267c478bd9Sstevel@tonic-gate 		 * is FAILED/INACTIVE/hit FAILBACK=no case, and we are
26277c478bd9Sstevel@tonic-gate 		 * not creating hidden ires, we should not allow that.
26287c478bd9Sstevel@tonic-gate 		 * This happens because the state of the interface
26297c478bd9Sstevel@tonic-gate 		 * changed while we were waiting in ARP. If this is the
26307c478bd9Sstevel@tonic-gate 		 * daemon sending probes, the next probe will create
26317c478bd9Sstevel@tonic-gate 		 * HIDDEN ires and we will create an ire then. This
26327c478bd9Sstevel@tonic-gate 		 * cannot happen with NDP currently because IRE is
26337c478bd9Sstevel@tonic-gate 		 * never queued in NDP. But it can happen in the
26347c478bd9Sstevel@tonic-gate 		 * future when we have external resolvers with IPv6.
26357c478bd9Sstevel@tonic-gate 		 * If the interface gets marked with OFFLINE while we
26367c478bd9Sstevel@tonic-gate 		 * are waiting in ARP, don't add the ire.
26377c478bd9Sstevel@tonic-gate 		 */
26387c478bd9Sstevel@tonic-gate 		if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
26397c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
26407c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
26417c478bd9Sstevel@tonic-gate 			/*
26427c478bd9Sstevel@tonic-gate 			 * We don't know whether it is a valid ipif or not.
26437c478bd9Sstevel@tonic-gate 			 * unless we do the check below. So, set it to NULL.
26447c478bd9Sstevel@tonic-gate 			 */
26457c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
26467c478bd9Sstevel@tonic-gate 			ire_delete(ire);
26477c478bd9Sstevel@tonic-gate 			*irep = NULL;
26487c478bd9Sstevel@tonic-gate 			return (EINVAL);
26497c478bd9Sstevel@tonic-gate 		}
26507c478bd9Sstevel@tonic-gate 	}
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 	if (stq_ill != NULL && ire->ire_type == IRE_CACHE &&
26537c478bd9Sstevel@tonic-gate 	    stq_ill->ill_net_type == IRE_IF_RESOLVER) {
2654f4b3ec61Sdh155122 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2655f4b3ec61Sdh155122 		ill = ILL_START_WALK_ALL(&ctx, ipst);
26567c478bd9Sstevel@tonic-gate 		for (; ill != NULL; ill = ill_next(&ctx, ill)) {
26577c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
26587c478bd9Sstevel@tonic-gate 			if (ill->ill_state_flags & ILL_CONDEMNED) {
26597c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
26607c478bd9Sstevel@tonic-gate 				continue;
26617c478bd9Sstevel@tonic-gate 			}
26627c478bd9Sstevel@tonic-gate 			/*
26637c478bd9Sstevel@tonic-gate 			 * We need to make sure that the ipif is a valid one
26647c478bd9Sstevel@tonic-gate 			 * before adding the IRE_CACHE. This happens only
26657c478bd9Sstevel@tonic-gate 			 * with IRE_CACHE when there is an external resolver.
26667c478bd9Sstevel@tonic-gate 			 *
26677c478bd9Sstevel@tonic-gate 			 * We can unplumb a logical interface while the
26687c478bd9Sstevel@tonic-gate 			 * packet is waiting in ARP with the IRE. Then,
26697c478bd9Sstevel@tonic-gate 			 * later on when we feed the IRE back, the ipif
26707c478bd9Sstevel@tonic-gate 			 * has to be re-checked. This can't happen with
26717c478bd9Sstevel@tonic-gate 			 * NDP currently, as we never queue the IRE with
26727c478bd9Sstevel@tonic-gate 			 * the packet. We always try to recreate the IRE
26737c478bd9Sstevel@tonic-gate 			 * when the resolution is completed. But, we do
26747c478bd9Sstevel@tonic-gate 			 * it for IPv6 also here so that in future if
26757c478bd9Sstevel@tonic-gate 			 * we have external resolvers, it will work without
26767c478bd9Sstevel@tonic-gate 			 * any change.
26777c478bd9Sstevel@tonic-gate 			 */
26787c478bd9Sstevel@tonic-gate 			ipif = ipif_lookup_seqid(ill, ire->ire_ipif_seqid);
26797c478bd9Sstevel@tonic-gate 			if (ipif != NULL) {
26807c478bd9Sstevel@tonic-gate 				ipif_refhold_locked(ipif);
26817c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
26827c478bd9Sstevel@tonic-gate 				break;
26837c478bd9Sstevel@tonic-gate 			}
26847c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
26857c478bd9Sstevel@tonic-gate 		}
2686f4b3ec61Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
26877c478bd9Sstevel@tonic-gate 		if (ipif == NULL ||
26887c478bd9Sstevel@tonic-gate 		    (ipif->ipif_isv6 &&
26897c478bd9Sstevel@tonic-gate 		    !IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
26907c478bd9Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr)) ||
26917c478bd9Sstevel@tonic-gate 		    (!ipif->ipif_isv6 &&
26927c478bd9Sstevel@tonic-gate 		    ire->ire_src_addr != ipif->ipif_src_addr) ||
269345916cd2Sjpk 		    ire->ire_zoneid != ipif->ipif_zoneid) {
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 			if (ipif != NULL)
26967c478bd9Sstevel@tonic-gate 				ipif_refrele(ipif);
26977c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
26987c478bd9Sstevel@tonic-gate 			ire_delete(ire);
26997c478bd9Sstevel@tonic-gate 			*irep = NULL;
27007c478bd9Sstevel@tonic-gate 			return (EINVAL);
27017c478bd9Sstevel@tonic-gate 		}
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
27057c478bd9Sstevel@tonic-gate 		/*
27067c478bd9Sstevel@tonic-gate 		 * If this group was dismantled while this packets was
27077c478bd9Sstevel@tonic-gate 		 * queued in ARP, don't add it here.
27087c478bd9Sstevel@tonic-gate 		 */
27097c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif->ipif_ill->ill_group != ill->ill_group) {
27107c478bd9Sstevel@tonic-gate 			/* We don't want ire_inactive bump stats for this */
27117c478bd9Sstevel@tonic-gate 			ipif_refrele(ipif);
27127c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
27137c478bd9Sstevel@tonic-gate 			ire_delete(ire);
27147c478bd9Sstevel@tonic-gate 			*irep = NULL;
27157c478bd9Sstevel@tonic-gate 			return (EINVAL);
27167c478bd9Sstevel@tonic-gate 		}
2717dc3879f9Sjarrett 
2718dc3879f9Sjarrett 		/*
2719dc3879f9Sjarrett 		 * Since we didn't attach label security attributes to the
2720dc3879f9Sjarrett 		 * ire for the resolver case, we need to add it now. (only
2721dc3879f9Sjarrett 		 * for v4 resolver and v6 xresolv case).
2722dc3879f9Sjarrett 		 */
2723dc3879f9Sjarrett 		if (is_system_labeled() && ire_is_mblk) {
2724dc3879f9Sjarrett 			if (ire->ire_ipversion == IPV4_VERSION) {
2725dc3879f9Sjarrett 				ga.ga_af = AF_INET;
2726dc3879f9Sjarrett 				IN6_IPADDR_TO_V4MAPPED(ire->ire_gateway_addr !=
2727dc3879f9Sjarrett 				    INADDR_ANY ? ire->ire_gateway_addr :
2728dc3879f9Sjarrett 				    ire->ire_addr, &ga.ga_addr);
2729dc3879f9Sjarrett 			} else {
2730dc3879f9Sjarrett 				ga.ga_af = AF_INET6;
2731dc3879f9Sjarrett 				ga.ga_addr = IN6_IS_ADDR_UNSPECIFIED(
2732dc3879f9Sjarrett 				    &ire->ire_gateway_addr_v6) ?
2733dc3879f9Sjarrett 				    ire->ire_addr_v6 :
2734dc3879f9Sjarrett 				    ire->ire_gateway_addr_v6;
2735dc3879f9Sjarrett 			}
2736dc3879f9Sjarrett 			gcgrp = gcgrp_lookup(&ga, B_FALSE);
2737dc3879f9Sjarrett 			error = tsol_ire_init_gwattr(ire, ire->ire_ipversion,
2738dc3879f9Sjarrett 			    NULL, gcgrp);
2739dc3879f9Sjarrett 			if (error != 0) {
2740dc3879f9Sjarrett 				if (gcgrp != NULL) {
2741dc3879f9Sjarrett 					GCGRP_REFRELE(gcgrp);
2742dc3879f9Sjarrett 					gcgrp = NULL;
2743dc3879f9Sjarrett 				}
2744dc3879f9Sjarrett 				ipif_refrele(ipif);
2745dc3879f9Sjarrett 				ire->ire_ipif = NULL;
2746dc3879f9Sjarrett 				ire_delete(ire);
2747dc3879f9Sjarrett 				*irep = NULL;
2748dc3879f9Sjarrett 				return (error);
2749dc3879f9Sjarrett 			}
2750dc3879f9Sjarrett 		}
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	/*
27547c478bd9Sstevel@tonic-gate 	 * In case ire was changed
27557c478bd9Sstevel@tonic-gate 	 */
27567c478bd9Sstevel@tonic-gate 	*irep = ire;
2757*5c0b7edeSseb 	if (ire->ire_ipversion == IPV6_VERSION)
27587c478bd9Sstevel@tonic-gate 		error = ire_add_v6(irep, q, mp, func);
27597c478bd9Sstevel@tonic-gate 	else
2760*5c0b7edeSseb 		error = ire_add_v4(irep, q, mp, func, allow_unresolved);
27617c478bd9Sstevel@tonic-gate 	if (ipif != NULL)
27627c478bd9Sstevel@tonic-gate 		ipif_refrele(ipif);
27637c478bd9Sstevel@tonic-gate 	return (error);
27647c478bd9Sstevel@tonic-gate }
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate /*
2767dc3879f9Sjarrett  * Add an initialized IRE to an appropriate table based on ire_type.
27687c478bd9Sstevel@tonic-gate  *
27696bdb8e66Sdd193516  * The forward table contains IRE_PREFIX/IRE_HOST and
27707c478bd9Sstevel@tonic-gate  * IRE_IF_RESOLVER/IRE_IF_NORESOLVER and IRE_DEFAULT.
27717c478bd9Sstevel@tonic-gate  *
27727c478bd9Sstevel@tonic-gate  * The cache table contains IRE_BROADCAST/IRE_LOCAL/IRE_LOOPBACK
27737c478bd9Sstevel@tonic-gate  * and IRE_CACHE.
27747c478bd9Sstevel@tonic-gate  *
27757c478bd9Sstevel@tonic-gate  * NOTE : This function is called as writer though not required
27767c478bd9Sstevel@tonic-gate  * by this function.
27777c478bd9Sstevel@tonic-gate  */
27787c478bd9Sstevel@tonic-gate static int
2779c793af95Ssangeeta ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func,
2780c793af95Ssangeeta     boolean_t allow_unresolved)
27817c478bd9Sstevel@tonic-gate {
27827c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
27837c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
27847c478bd9Sstevel@tonic-gate 	ire_t	**irep;
27857c478bd9Sstevel@tonic-gate 	int	flags;
27867c478bd9Sstevel@tonic-gate 	ire_t	*pire = NULL;
27877c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
27887c478bd9Sstevel@tonic-gate 	ire_t	*ire = *ire_p;
27897c478bd9Sstevel@tonic-gate 	int	error;
2790c793af95Ssangeeta 	boolean_t need_refrele = B_FALSE;
2791c793af95Ssangeeta 	nce_t	*nce;
2792f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
27957c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
27967c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
27977c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(
27987c478bd9Sstevel@tonic-gate 		    &((ill_t *)(ire->ire_stq->q_ptr))->ill_lock));
27997c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
28007c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_mp == NULL); /* Calls should go through ire_add */
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	/* Find the appropriate list head. */
28037c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
28047c478bd9Sstevel@tonic-gate 	case IRE_HOST:
28057c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
28067c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
28077c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
28087c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
28097c478bd9Sstevel@tonic-gate 		break;
28107c478bd9Sstevel@tonic-gate 	case IRE_CACHE:
28117c478bd9Sstevel@tonic-gate 	case IRE_BROADCAST:
28127c478bd9Sstevel@tonic-gate 	case IRE_LOCAL:
28137c478bd9Sstevel@tonic-gate 	case IRE_LOOPBACK:
28147c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
28157c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
28167c478bd9Sstevel@tonic-gate 		break;
28177c478bd9Sstevel@tonic-gate 	case IRE_PREFIX:
28187c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
28197c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
28207c478bd9Sstevel@tonic-gate 		break;
28217c478bd9Sstevel@tonic-gate 	case IRE_DEFAULT:
28227c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
28237c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
28247c478bd9Sstevel@tonic-gate 		break;
28257c478bd9Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
28267c478bd9Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
28277c478bd9Sstevel@tonic-gate 		break;
28287c478bd9Sstevel@tonic-gate 	default:
2829c793af95Ssangeeta 		ip0dbg(("ire_add_v4: ire %p has unrecognized IRE type (%d)\n",
2830c793af95Ssangeeta 		    (void *)ire, ire->ire_type));
28317c478bd9Sstevel@tonic-gate 		ire_delete(ire);
28327c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
28337c478bd9Sstevel@tonic-gate 		return (EINVAL);
28347c478bd9Sstevel@tonic-gate 	}
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
28377c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	/*
28407c478bd9Sstevel@tonic-gate 	 * ip_newroute/ip_newroute_multi are unable to prevent the deletion
28417c478bd9Sstevel@tonic-gate 	 * of the interface route while adding an IRE_CACHE for an on-link
28427c478bd9Sstevel@tonic-gate 	 * destination in the IRE_IF_RESOLVER case, since the ire has to
28437c478bd9Sstevel@tonic-gate 	 * go to ARP and return. We can't do a REFHOLD on the
28447c478bd9Sstevel@tonic-gate 	 * associated interface ire for fear of ARP freeing the message.
28457c478bd9Sstevel@tonic-gate 	 * Here we look up the interface ire in the forwarding table and
28467c478bd9Sstevel@tonic-gate 	 * make sure that the interface route has not been deleted.
28477c478bd9Sstevel@tonic-gate 	 */
28487c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE && ire->ire_gateway_addr == 0 &&
28497c478bd9Sstevel@tonic-gate 	    ((ill_t *)ire->ire_stq->q_ptr)->ill_net_type == IRE_IF_RESOLVER) {
2850c793af95Ssangeeta 
28517c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp == NULL);
28527c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr) && !(ire->ire_flags & RTF_SETSRC)) {
28537c478bd9Sstevel@tonic-gate 			/*
28547c478bd9Sstevel@tonic-gate 			 * The ihandle that we used in ip_newroute_multi
28557c478bd9Sstevel@tonic-gate 			 * comes from the interface route corresponding
28567c478bd9Sstevel@tonic-gate 			 * to ire_ipif. Lookup here to see if it exists
28577c478bd9Sstevel@tonic-gate 			 * still.
28587c478bd9Sstevel@tonic-gate 			 * If the ire has a source address assigned using
28597c478bd9Sstevel@tonic-gate 			 * RTF_SETSRC, ire_ipif is the logical interface holding
28607c478bd9Sstevel@tonic-gate 			 * this source address, so we can't use it to check for
28617c478bd9Sstevel@tonic-gate 			 * the existence of the interface route. Instead we rely
28627c478bd9Sstevel@tonic-gate 			 * on the brute force ihandle search in
28637c478bd9Sstevel@tonic-gate 			 * ire_ihandle_lookup_onlink() below.
28647c478bd9Sstevel@tonic-gate 			 */
28657c478bd9Sstevel@tonic-gate 			pire = ipif_to_ire(ire->ire_ipif);
28667c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
28677c478bd9Sstevel@tonic-gate 				ire_delete(ire);
28687c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
28697c478bd9Sstevel@tonic-gate 				return (EINVAL);
28707c478bd9Sstevel@tonic-gate 			} else if (pire->ire_ihandle != ire->ire_ihandle) {
28717c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
28727c478bd9Sstevel@tonic-gate 				ire_delete(ire);
28737c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
28747c478bd9Sstevel@tonic-gate 				return (EINVAL);
28757c478bd9Sstevel@tonic-gate 			}
28767c478bd9Sstevel@tonic-gate 		} else {
28777c478bd9Sstevel@tonic-gate 			pire = ire_ihandle_lookup_onlink(ire);
28787c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
28797c478bd9Sstevel@tonic-gate 				ire_delete(ire);
28807c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
28817c478bd9Sstevel@tonic-gate 				return (EINVAL);
28827c478bd9Sstevel@tonic-gate 			}
28837c478bd9Sstevel@tonic-gate 		}
28847c478bd9Sstevel@tonic-gate 		/* Prevent pire from getting deleted */
28857c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(pire->ire_bucket);
28867c478bd9Sstevel@tonic-gate 		/* Has it been removed already ? */
28877c478bd9Sstevel@tonic-gate 		if (pire->ire_marks & IRE_MARK_CONDEMNED) {
28887c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
28897c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
28907c478bd9Sstevel@tonic-gate 			ire_delete(ire);
28917c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
28927c478bd9Sstevel@tonic-gate 			return (EINVAL);
28937c478bd9Sstevel@tonic-gate 		}
28947c478bd9Sstevel@tonic-gate 	} else {
28957c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp != NULL);
28967c478bd9Sstevel@tonic-gate 	}
28977c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
29007c478bd9Sstevel@tonic-gate 		/*
29017c478bd9Sstevel@tonic-gate 		 * We use MATCH_IRE_IPIF while adding IRE_CACHES only
29027c478bd9Sstevel@tonic-gate 		 * for historic reasons and to maintain symmetry with
29037c478bd9Sstevel@tonic-gate 		 * IPv6 code path. Historically this was used by
29047c478bd9Sstevel@tonic-gate 		 * multicast code to create multiple IRE_CACHES on
29057c478bd9Sstevel@tonic-gate 		 * a single ill with different ipifs. This was used
29067c478bd9Sstevel@tonic-gate 		 * so that multicast packets leaving the node had the
29077c478bd9Sstevel@tonic-gate 		 * right source address. This is no longer needed as
29087c478bd9Sstevel@tonic-gate 		 * ip_wput initializes the address correctly.
29097c478bd9Sstevel@tonic-gate 		 */
29107c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_IPIF;
29117c478bd9Sstevel@tonic-gate 		/*
29127c478bd9Sstevel@tonic-gate 		 * If we are creating hidden ires, make sure we search on
29137c478bd9Sstevel@tonic-gate 		 * this ill (MATCH_IRE_ILL) and a hidden ire,
29147c478bd9Sstevel@tonic-gate 		 * while we are searching for duplicates below. Otherwise we
29157c478bd9Sstevel@tonic-gate 		 * could potentially find an IRE on some other interface
29167c478bd9Sstevel@tonic-gate 		 * and it may not be a IRE marked with IRE_MARK_HIDDEN. We
29177c478bd9Sstevel@tonic-gate 		 * shouldn't do this as this will lead to an infinite loop
29187c478bd9Sstevel@tonic-gate 		 * (if we get to ip_wput again) eventually we need an hidden
29197c478bd9Sstevel@tonic-gate 		 * ire for this packet to go out. MATCH_IRE_ILL is explicitly
29207c478bd9Sstevel@tonic-gate 		 * done below.
29217c478bd9Sstevel@tonic-gate 		 */
29227c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_CACHE &&
29237c478bd9Sstevel@tonic-gate 		    (ire->ire_marks & IRE_MARK_HIDDEN))
29247c478bd9Sstevel@tonic-gate 			flags |= (MATCH_IRE_MARK_HIDDEN);
29257c478bd9Sstevel@tonic-gate 	}
2926c793af95Ssangeeta 	if ((ire->ire_type & IRE_CACHETABLE) == 0) {
2927c793af95Ssangeeta 		irb_ptr = ire_get_bucket(ire);
2928c793af95Ssangeeta 		need_refrele = B_TRUE;
2929c793af95Ssangeeta 		if (irb_ptr == NULL) {
2930c793af95Ssangeeta 			/*
2931c793af95Ssangeeta 			 * This assumes that the ire has not added
2932c793af95Ssangeeta 			 * a reference to the ipif.
2933c793af95Ssangeeta 			 */
2934c793af95Ssangeeta 			ire->ire_ipif = NULL;
2935c793af95Ssangeeta 			ire_delete(ire);
2936c793af95Ssangeeta 			if (pire != NULL) {
2937c793af95Ssangeeta 				IRB_REFRELE(pire->ire_bucket);
2938c793af95Ssangeeta 				ire_refrele(pire);
2939c793af95Ssangeeta 			}
2940c793af95Ssangeeta 			*ire_p = NULL;
2941c793af95Ssangeeta 			return (EINVAL);
2942c793af95Ssangeeta 		}
2943c793af95Ssangeeta 	} else {
2944f4b3ec61Sdh155122 		irb_ptr = &(ipst->ips_ip_cache_table[IRE_ADDR_HASH(
2945f4b3ec61Sdh155122 		    ire->ire_addr, ipst->ips_ip_cache_table_size)]);
2946c793af95Ssangeeta 	}
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	/*
29497c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
29507c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock. Check for condemned
29517c478bd9Sstevel@tonic-gate 	 *
29527c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
29537c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
2954f4b3ec61Sdh155122 	 * To avoid lock order problems, get the ndp4->ndp_g_lock.
29557c478bd9Sstevel@tonic-gate 	 */
2956f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ndp4->ndp_g_lock);
29577c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
29587c478bd9Sstevel@tonic-gate 	if (error != 0) {
2959f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
29607c478bd9Sstevel@tonic-gate 		/*
29617c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
29627c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
29637c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
29647c478bd9Sstevel@tonic-gate 		 */
29657c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
29667c478bd9Sstevel@tonic-gate 		ire_delete(ire);
29677c478bd9Sstevel@tonic-gate 		if (pire != NULL) {
29687c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
29697c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
29707c478bd9Sstevel@tonic-gate 		}
29717c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
2972c793af95Ssangeeta 		if (need_refrele)
2973c793af95Ssangeeta 			IRB_REFRELE(irb_ptr);
29747c478bd9Sstevel@tonic-gate 		return (error);
29757c478bd9Sstevel@tonic-gate 	}
29767c478bd9Sstevel@tonic-gate 	/*
29777c478bd9Sstevel@tonic-gate 	 * To avoid creating ires having stale values for the ire_max_frag
29787c478bd9Sstevel@tonic-gate 	 * we get the latest value atomically here. For more details
29797c478bd9Sstevel@tonic-gate 	 * see the block comment in ip_sioctl_mtu and in DL_NOTE_SDU_CHANGE
29807c478bd9Sstevel@tonic-gate 	 * in ip_rput_dlpi_writer
29817c478bd9Sstevel@tonic-gate 	 */
29827c478bd9Sstevel@tonic-gate 	if (ire->ire_max_fragp == NULL) {
29837c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr))
29847c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
29857c478bd9Sstevel@tonic-gate 		else
29867c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = pire->ire_max_frag;
29877c478bd9Sstevel@tonic-gate 	} else {
29887c478bd9Sstevel@tonic-gate 		uint_t	max_frag;
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 		max_frag = *ire->ire_max_fragp;
29917c478bd9Sstevel@tonic-gate 		ire->ire_max_fragp = NULL;
29927c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = max_frag;
29937c478bd9Sstevel@tonic-gate 	}
29947c478bd9Sstevel@tonic-gate 	/*
29957c478bd9Sstevel@tonic-gate 	 * Atomically check for duplicate and insert in the table.
29967c478bd9Sstevel@tonic-gate 	 */
29977c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
29987c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
29997c478bd9Sstevel@tonic-gate 			continue;
30007c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL) {
30017c478bd9Sstevel@tonic-gate 			/*
30027c478bd9Sstevel@tonic-gate 			 * We do MATCH_IRE_ILL implicitly here for IREs
30037c478bd9Sstevel@tonic-gate 			 * with a non-null ire_ipif, including IRE_CACHEs.
30047c478bd9Sstevel@tonic-gate 			 * As ire_ipif and ire_stq could point to two
30057c478bd9Sstevel@tonic-gate 			 * different ills, we can't pass just ire_ipif to
30067c478bd9Sstevel@tonic-gate 			 * ire_match_args and get a match on both ills.
30077c478bd9Sstevel@tonic-gate 			 * This is just needed for duplicate checks here and
30087c478bd9Sstevel@tonic-gate 			 * so we don't add an extra argument to
30097c478bd9Sstevel@tonic-gate 			 * ire_match_args for this. Do it locally.
30107c478bd9Sstevel@tonic-gate 			 *
30117c478bd9Sstevel@tonic-gate 			 * NOTE : Currently there is no part of the code
30127c478bd9Sstevel@tonic-gate 			 * that asks for both MATH_IRE_IPIF and MATCH_IRE_ILL
30137c478bd9Sstevel@tonic-gate 			 * match for IRE_CACHEs. Thus we don't want to
30147c478bd9Sstevel@tonic-gate 			 * extend the arguments to ire_match_args.
30157c478bd9Sstevel@tonic-gate 			 */
30167c478bd9Sstevel@tonic-gate 			if (ire1->ire_stq != ire->ire_stq)
30177c478bd9Sstevel@tonic-gate 				continue;
30187c478bd9Sstevel@tonic-gate 			/*
30197c478bd9Sstevel@tonic-gate 			 * Multiroute IRE_CACHEs for a given destination can
30207c478bd9Sstevel@tonic-gate 			 * have the same ire_ipif, typically if their source
30217c478bd9Sstevel@tonic-gate 			 * address is forced using RTF_SETSRC, and the same
30227c478bd9Sstevel@tonic-gate 			 * send-to queue. We differentiate them using the parent
30237c478bd9Sstevel@tonic-gate 			 * handle.
30247c478bd9Sstevel@tonic-gate 			 */
30257c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_CACHE &&
30267c478bd9Sstevel@tonic-gate 			    (ire1->ire_flags & RTF_MULTIRT) &&
30277c478bd9Sstevel@tonic-gate 			    (ire->ire_flags & RTF_MULTIRT) &&
30287c478bd9Sstevel@tonic-gate 			    (ire1->ire_phandle != ire->ire_phandle))
30297c478bd9Sstevel@tonic-gate 				continue;
30307c478bd9Sstevel@tonic-gate 		}
30317c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
30327c478bd9Sstevel@tonic-gate 			continue;
30337c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
30347c478bd9Sstevel@tonic-gate 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ipif,
303545916cd2Sjpk 		    ire->ire_zoneid, 0, NULL, flags)) {
30367c478bd9Sstevel@tonic-gate 			/*
30377c478bd9Sstevel@tonic-gate 			 * Return the old ire after doing a REFHOLD.
30387c478bd9Sstevel@tonic-gate 			 * As most of the callers continue to use the IRE
30397c478bd9Sstevel@tonic-gate 			 * after adding, we return a held ire. This will
30407c478bd9Sstevel@tonic-gate 			 * avoid a lookup in the caller again. If the callers
30417c478bd9Sstevel@tonic-gate 			 * don't want to use it, they need to do a REFRELE.
30427c478bd9Sstevel@tonic-gate 			 */
30437c478bd9Sstevel@tonic-gate 			ip1dbg(("found dup ire existing %p new %p",
30447c478bd9Sstevel@tonic-gate 			    (void *)ire1, (void *)ire));
30457c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
30467c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
3047f4b3ec61Sdh155122 			mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
30487c478bd9Sstevel@tonic-gate 			ire_delete(ire);
30497c478bd9Sstevel@tonic-gate 			if (pire != NULL) {
30507c478bd9Sstevel@tonic-gate 				/*
30517c478bd9Sstevel@tonic-gate 				 * Assert that it is not removed from the
30527c478bd9Sstevel@tonic-gate 				 * list yet.
30537c478bd9Sstevel@tonic-gate 				 */
30547c478bd9Sstevel@tonic-gate 				ASSERT(pire->ire_ptpn != NULL);
30557c478bd9Sstevel@tonic-gate 				IRB_REFRELE(pire->ire_bucket);
30567c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
30577c478bd9Sstevel@tonic-gate 			}
30587c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
3059c793af95Ssangeeta 			if (need_refrele)
3060c793af95Ssangeeta 				IRB_REFRELE(irb_ptr);
30617c478bd9Sstevel@tonic-gate 			return (0);
30627c478bd9Sstevel@tonic-gate 		}
30637c478bd9Sstevel@tonic-gate 	}
3064c793af95Ssangeeta 	if (ire->ire_type & IRE_CACHE) {
3065c793af95Ssangeeta 		ASSERT(ire->ire_stq != NULL);
3066c793af95Ssangeeta 		nce = ndp_lookup_v4(ire_to_ill(ire),
3067c793af95Ssangeeta 		    ((ire->ire_gateway_addr != INADDR_ANY) ?
3068c793af95Ssangeeta 		    &ire->ire_gateway_addr : &ire->ire_addr),
3069c793af95Ssangeeta 		    B_TRUE);
3070c793af95Ssangeeta 		if (nce != NULL)
3071c793af95Ssangeeta 			mutex_enter(&nce->nce_lock);
3072c793af95Ssangeeta 		/*
3073c793af95Ssangeeta 		 * if the nce is NCE_F_CONDEMNED, or if it is not ND_REACHABLE
3074c793af95Ssangeeta 		 * and the caller has prohibited the addition of incomplete
3075c793af95Ssangeeta 		 * ire's, we fail the add. Note that nce_state could be
3076b9c344b3Ssowmini 		 * something other than ND_REACHABLE if the nce had
3077b9c344b3Ssowmini 		 * just expired and the ire_create preceding the
3078b9c344b3Ssowmini 		 * ire_add added a new ND_INITIAL nce.
3079c793af95Ssangeeta 		 */
3080c793af95Ssangeeta 		if ((nce == NULL) ||
3081c793af95Ssangeeta 		    (nce->nce_flags & NCE_F_CONDEMNED) ||
3082c793af95Ssangeeta 		    (!allow_unresolved &&
3083107875b0Ssangeeta 		    (nce->nce_state != ND_REACHABLE))) {
3084b9c344b3Ssowmini 			if (nce != NULL) {
3085b9c344b3Ssowmini 				DTRACE_PROBE1(ire__bad__nce, nce_t *, nce);
3086c793af95Ssangeeta 				mutex_exit(&nce->nce_lock);
3087b9c344b3Ssowmini 			}
3088c793af95Ssangeeta 			ire_atomic_end(irb_ptr, ire);
3089f4b3ec61Sdh155122 			mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
3090c793af95Ssangeeta 			if (nce != NULL)
3091c793af95Ssangeeta 				NCE_REFRELE(nce);
3092c793af95Ssangeeta 			DTRACE_PROBE1(ire__no__nce, ire_t *, ire);
3093c793af95Ssangeeta 			ire_delete(ire);
3094c793af95Ssangeeta 			if (pire != NULL) {
3095c793af95Ssangeeta 				IRB_REFRELE(pire->ire_bucket);
3096c793af95Ssangeeta 				ire_refrele(pire);
3097c793af95Ssangeeta 			}
3098c793af95Ssangeeta 			*ire_p = NULL;
3099c793af95Ssangeeta 			if (need_refrele)
3100c793af95Ssangeeta 				IRB_REFRELE(irb_ptr);
3101c793af95Ssangeeta 			return (EINVAL);
3102c793af95Ssangeeta 		} else {
3103c793af95Ssangeeta 			ire->ire_nce = nce;
3104c793af95Ssangeeta 			mutex_exit(&nce->nce_lock);
3105c793af95Ssangeeta 			/*
3106c793af95Ssangeeta 			 * We are associating this nce to the ire, so
3107c793af95Ssangeeta 			 * change the nce ref taken in ndp_lookup_v4() from
3108c793af95Ssangeeta 			 * NCE_REFHOLD to NCE_REFHOLD_NOTR
3109c793af95Ssangeeta 			 */
3110c793af95Ssangeeta 			NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
3111c793af95Ssangeeta 		}
3112c793af95Ssangeeta 	}
31137c478bd9Sstevel@tonic-gate 	/*
31147c478bd9Sstevel@tonic-gate 	 * Make it easy for ip_wput_ire() to hit multiple broadcast ires by
31157c478bd9Sstevel@tonic-gate 	 * grouping identical addresses together on the hash chain. We also
31167c478bd9Sstevel@tonic-gate 	 * don't want to send multiple copies out if there are two ills part
31177c478bd9Sstevel@tonic-gate 	 * of the same group. Thus we group the ires with same addr and same
31187c478bd9Sstevel@tonic-gate 	 * ill group together so that ip_wput_ire can easily skip all the
31197c478bd9Sstevel@tonic-gate 	 * ires with same addr and same group after sending the first copy.
31207c478bd9Sstevel@tonic-gate 	 * We do this only for IRE_BROADCASTs as ip_wput_ire is currently
31217c478bd9Sstevel@tonic-gate 	 * interested in such groupings only for broadcasts.
31227c478bd9Sstevel@tonic-gate 	 *
31237c478bd9Sstevel@tonic-gate 	 * NOTE : If the interfaces are brought up first and then grouped,
31247c478bd9Sstevel@tonic-gate 	 * illgrp_insert will handle it. We come here when the interfaces
31257c478bd9Sstevel@tonic-gate 	 * are already in group and we are bringing them UP.
31267c478bd9Sstevel@tonic-gate 	 *
31277c478bd9Sstevel@tonic-gate 	 * Find the first entry that matches ire_addr. *irep will be null
31287c478bd9Sstevel@tonic-gate 	 * if no match.
312901802382Ssowmini 	 *
313001802382Ssowmini 	 * Note: the loopback and non-loopback broadcast entries for an
313101802382Ssowmini 	 * interface MUST be added before any MULTIRT entries.
31327c478bd9Sstevel@tonic-gate 	 */
31337c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
31347c478bd9Sstevel@tonic-gate 	while ((ire1 = *irep) != NULL && ire->ire_addr != ire1->ire_addr)
31357c478bd9Sstevel@tonic-gate 		irep = &ire1->ire_next;
31367c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_BROADCAST && *irep != NULL) {
31377c478bd9Sstevel@tonic-gate 		/*
31387c478bd9Sstevel@tonic-gate 		 * We found some ire (i.e *irep) with a matching addr. We
31397c478bd9Sstevel@tonic-gate 		 * want to group ires with same addr and same ill group
31407c478bd9Sstevel@tonic-gate 		 * together.
31417c478bd9Sstevel@tonic-gate 		 *
31427c478bd9Sstevel@tonic-gate 		 * First get to the entry that matches our address and
31437c478bd9Sstevel@tonic-gate 		 * ill group i.e stop as soon as we find the first ire
31447c478bd9Sstevel@tonic-gate 		 * matching the ill group and address. If there is only
31457c478bd9Sstevel@tonic-gate 		 * an address match, we should walk and look for some
31467c478bd9Sstevel@tonic-gate 		 * group match. These are some of the possible scenarios :
31477c478bd9Sstevel@tonic-gate 		 *
31487c478bd9Sstevel@tonic-gate 		 * 1) There are no groups at all i.e all ire's ill_group
31497c478bd9Sstevel@tonic-gate 		 *    are NULL. In that case we will essentially group
31507c478bd9Sstevel@tonic-gate 		 *    all the ires with the same addr together. Same as
31517c478bd9Sstevel@tonic-gate 		 *    the "else" block of this "if".
31527c478bd9Sstevel@tonic-gate 		 *
31537c478bd9Sstevel@tonic-gate 		 * 2) There are some groups and this ire's ill_group is
31547c478bd9Sstevel@tonic-gate 		 *    NULL. In this case, we will first find the group
31557c478bd9Sstevel@tonic-gate 		 *    that matches the address and a NULL group. Then
31567c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
31577c478bd9Sstevel@tonic-gate 		 *
31587c478bd9Sstevel@tonic-gate 		 * 3) There are some groups and this ires's ill_group is
31597c478bd9Sstevel@tonic-gate 		 *    non-NULL. In this case we will first find the group
31607c478bd9Sstevel@tonic-gate 		 *    that matches the address and the ill_group. Then
31617c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
31627c478bd9Sstevel@tonic-gate 		 */
316301802382Ssowmini 		for (;;) {
31647c478bd9Sstevel@tonic-gate 			ire1 = *irep;
31657c478bd9Sstevel@tonic-gate 			if ((ire1->ire_next == NULL) ||
31667c478bd9Sstevel@tonic-gate 			    (ire1->ire_next->ire_addr != ire->ire_addr) ||
31677c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
316801802382Ssowmini 			    (ire1->ire_flags & RTF_MULTIRT) ||
31697c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group ==
31707c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
31717c478bd9Sstevel@tonic-gate 				break;
31727c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
31737c478bd9Sstevel@tonic-gate 		}
31747c478bd9Sstevel@tonic-gate 		ASSERT(*irep != NULL);
317501802382Ssowmini 		/*
317601802382Ssowmini 		 * The ire will be added before *irep, so
317701802382Ssowmini 		 * if irep is a MULTIRT ire, just break to
317801802382Ssowmini 		 * ire insertion code.
317901802382Ssowmini 		 */
318001802382Ssowmini 		if (((*irep)->ire_flags & RTF_MULTIRT) != 0)
318101802382Ssowmini 			goto insert_ire;
318201802382Ssowmini 
31837c478bd9Sstevel@tonic-gate 		irep = &((*irep)->ire_next);
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 		/*
31867c478bd9Sstevel@tonic-gate 		 * Either we have hit the end of the list or the address
31877c478bd9Sstevel@tonic-gate 		 * did not match or the group *matched*. If we found
31887c478bd9Sstevel@tonic-gate 		 * a match on the group, skip to the end of the group.
31897c478bd9Sstevel@tonic-gate 		 */
31907c478bd9Sstevel@tonic-gate 		while (*irep != NULL) {
31917c478bd9Sstevel@tonic-gate 			ire1 = *irep;
31927c478bd9Sstevel@tonic-gate 			if ((ire1->ire_addr != ire->ire_addr) ||
31937c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
31947c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group !=
31957c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
31967c478bd9Sstevel@tonic-gate 				break;
31977c478bd9Sstevel@tonic-gate 			if (ire1->ire_ipif->ipif_ill->ill_group == NULL &&
31987c478bd9Sstevel@tonic-gate 			    ire1->ire_ipif == ire->ire_ipif) {
31997c478bd9Sstevel@tonic-gate 				irep = &ire1->ire_next;
32007c478bd9Sstevel@tonic-gate 				break;
32017c478bd9Sstevel@tonic-gate 			}
32027c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
32037c478bd9Sstevel@tonic-gate 		}
32047c478bd9Sstevel@tonic-gate 	} else if (*irep != NULL) {
32057c478bd9Sstevel@tonic-gate 		/*
32067c478bd9Sstevel@tonic-gate 		 * Find the last ire which matches ire_addr.
32077c478bd9Sstevel@tonic-gate 		 * Needed to do tail insertion among entries with the same
32087c478bd9Sstevel@tonic-gate 		 * ire_addr.
32097c478bd9Sstevel@tonic-gate 		 */
32107c478bd9Sstevel@tonic-gate 		while (ire->ire_addr == ire1->ire_addr) {
32117c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
32127c478bd9Sstevel@tonic-gate 			ire1 = *irep;
32137c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
32147c478bd9Sstevel@tonic-gate 				break;
32157c478bd9Sstevel@tonic-gate 		}
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate 
321801802382Ssowmini insert_ire:
32197c478bd9Sstevel@tonic-gate 	/* Insert at *irep */
32207c478bd9Sstevel@tonic-gate 	ire1 = *irep;
32217c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
32227c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
32237c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
32247c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
32257c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	/*
32287c478bd9Sstevel@tonic-gate 	 * ire_walk routines de-reference ire_next without holding
32297c478bd9Sstevel@tonic-gate 	 * a lock. Before we point to the new ire, we want to make
32307c478bd9Sstevel@tonic-gate 	 * sure the store that sets the ire_next of the new ire
32317c478bd9Sstevel@tonic-gate 	 * reaches global visibility, so that ire_walk routines
32327c478bd9Sstevel@tonic-gate 	 * don't see a truncated list of ires i.e if the ire_next
32337c478bd9Sstevel@tonic-gate 	 * of the new ire gets set after we do "*irep = ire" due
32347c478bd9Sstevel@tonic-gate 	 * to re-ordering, the ire_walk thread will see a NULL
32357c478bd9Sstevel@tonic-gate 	 * once it accesses the ire_next of the new ire.
32367c478bd9Sstevel@tonic-gate 	 * membar_producer() makes sure that the following store
32377c478bd9Sstevel@tonic-gate 	 * happens *after* all of the above stores.
32387c478bd9Sstevel@tonic-gate 	 */
32397c478bd9Sstevel@tonic-gate 	membar_producer();
32407c478bd9Sstevel@tonic-gate 	*irep = ire;
32417c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
32427c478bd9Sstevel@tonic-gate 	/*
32437c478bd9Sstevel@tonic-gate 	 * We return a bumped up IRE above. Keep it symmetrical
32447c478bd9Sstevel@tonic-gate 	 * so that the callers will always have to release. This
32457c478bd9Sstevel@tonic-gate 	 * helps the callers of this function because they continue
32467c478bd9Sstevel@tonic-gate 	 * to use the IRE after adding and hence they don't have to
32477c478bd9Sstevel@tonic-gate 	 * lookup again after we return the IRE.
32487c478bd9Sstevel@tonic-gate 	 *
32497c478bd9Sstevel@tonic-gate 	 * NOTE : We don't have to use atomics as this is appearing
32507c478bd9Sstevel@tonic-gate 	 * in the list for the first time and no one else can bump
32517c478bd9Sstevel@tonic-gate 	 * up the reference count on this yet.
32527c478bd9Sstevel@tonic-gate 	 */
32537c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
3254f4b3ec61Sdh155122 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
3255c793af95Ssangeeta 
32567c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
3257c793af95Ssangeeta 	if (irb_ptr->irb_marks & IRB_MARK_FTABLE)
3258c793af95Ssangeeta 		irb_ptr->irb_nire++;
3259c793af95Ssangeeta 
32607c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
32617c478bd9Sstevel@tonic-gate 		irb_ptr->irb_tmp_ire_cnt++;
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
32647c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
32657c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
32667c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
32677c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
32687c478bd9Sstevel@tonic-gate 		}
32697c478bd9Sstevel@tonic-gate 	} else {
32707c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
32717c478bd9Sstevel@tonic-gate 	}
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
3274f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	if (pire != NULL) {
32777c478bd9Sstevel@tonic-gate 		/* Assert that it is not removed from the list yet */
32787c478bd9Sstevel@tonic-gate 		ASSERT(pire->ire_ptpn != NULL);
32797c478bd9Sstevel@tonic-gate 		IRB_REFRELE(pire->ire_bucket);
32807c478bd9Sstevel@tonic-gate 		ire_refrele(pire);
32817c478bd9Sstevel@tonic-gate 	}
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE) {
32847c478bd9Sstevel@tonic-gate 		/*
3285c793af95Ssangeeta 		 * For ire's with host mask see if there is an entry
32867c478bd9Sstevel@tonic-gate 		 * in the cache. If there is one flush the whole cache as
32877c478bd9Sstevel@tonic-gate 		 * there might be multiple entries due to RTF_MULTIRT (CGTP).
32887c478bd9Sstevel@tonic-gate 		 * If no entry is found than there is no need to flush the
32897c478bd9Sstevel@tonic-gate 		 * cache.
32907c478bd9Sstevel@tonic-gate 		 */
32917c478bd9Sstevel@tonic-gate 		if (ire->ire_mask == IP_HOST_MASK) {
32927c478bd9Sstevel@tonic-gate 			ire_t *lire;
32937c478bd9Sstevel@tonic-gate 			lire = ire_ctable_lookup(ire->ire_addr, NULL, IRE_CACHE,
3294f4b3ec61Sdh155122 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
32957c478bd9Sstevel@tonic-gate 			if (lire != NULL) {
32967c478bd9Sstevel@tonic-gate 				ire_refrele(lire);
32977c478bd9Sstevel@tonic-gate 				ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
32987c478bd9Sstevel@tonic-gate 			}
32997c478bd9Sstevel@tonic-gate 		} else {
33007c478bd9Sstevel@tonic-gate 			ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
33017c478bd9Sstevel@tonic-gate 		}
33027c478bd9Sstevel@tonic-gate 	}
33037c478bd9Sstevel@tonic-gate 	/*
33047c478bd9Sstevel@tonic-gate 	 * We had to delay the fast path probe until the ire is inserted
33057c478bd9Sstevel@tonic-gate 	 * in the list. Otherwise the fast path ack won't find the ire in
33067c478bd9Sstevel@tonic-gate 	 * the table.
33077c478bd9Sstevel@tonic-gate 	 */
3308516bda92Ssowmini 	if (ire->ire_type == IRE_CACHE ||
3309516bda92Ssowmini 	    (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL)) {
3310516bda92Ssowmini 		ASSERT(ire->ire_nce != NULL);
331154da8755Ssowmini 		if (ire->ire_nce->nce_state == ND_REACHABLE)
3312516bda92Ssowmini 			nce_fastpath(ire->ire_nce);
3313516bda92Ssowmini 	}
33147c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
33157c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
33167c478bd9Sstevel@tonic-gate 	*ire_p = ire;
3317c793af95Ssangeeta 	if (need_refrele) {
3318c793af95Ssangeeta 		IRB_REFRELE(irb_ptr);
3319c793af95Ssangeeta 	}
33207c478bd9Sstevel@tonic-gate 	return (0);
33217c478bd9Sstevel@tonic-gate }
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate /*
33247c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. ire_unlink calls to
33257c478bd9Sstevel@tonic-gate  * do the final cleanup for this ire.
33267c478bd9Sstevel@tonic-gate  */
33277c478bd9Sstevel@tonic-gate void
33287c478bd9Sstevel@tonic-gate ire_cleanup(ire_t *ire)
33297c478bd9Sstevel@tonic-gate {
33307c478bd9Sstevel@tonic-gate 	ire_t *ire_next;
3331f4b3ec61Sdh155122 	ip_stack_t *ipst = ire->ire_ipst;
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 	while (ire != NULL) {
33367c478bd9Sstevel@tonic-gate 		ire_next = ire->ire_next;
33377c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
33387c478bd9Sstevel@tonic-gate 			ire_delete_v4(ire);
3339f4b3ec61Sdh155122 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
3340f4b3ec61Sdh155122 			    ire_stats_deleted);
33417c478bd9Sstevel@tonic-gate 		} else {
33427c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
33437c478bd9Sstevel@tonic-gate 			ire_delete_v6(ire);
3344f4b3ec61Sdh155122 			BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
3345f4b3ec61Sdh155122 			    ire_stats_deleted);
33467c478bd9Sstevel@tonic-gate 		}
33477c478bd9Sstevel@tonic-gate 		/*
33487c478bd9Sstevel@tonic-gate 		 * Now it's really out of the list. Before doing the
33497c478bd9Sstevel@tonic-gate 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
33507c478bd9Sstevel@tonic-gate 		 * so.
33517c478bd9Sstevel@tonic-gate 		 */
33527c478bd9Sstevel@tonic-gate 		ire->ire_next = NULL;
33537c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
33547c478bd9Sstevel@tonic-gate 		ire = ire_next;
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate }
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate /*
33597c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. It calls to unlink
33607c478bd9Sstevel@tonic-gate  * all the CONDEMNED ires from this bucket.
33617c478bd9Sstevel@tonic-gate  */
33627c478bd9Sstevel@tonic-gate ire_t *
33637c478bd9Sstevel@tonic-gate ire_unlink(irb_t *irb)
33647c478bd9Sstevel@tonic-gate {
33657c478bd9Sstevel@tonic-gate 	ire_t *ire;
33667c478bd9Sstevel@tonic-gate 	ire_t *ire1;
33677c478bd9Sstevel@tonic-gate 	ire_t **ptpn;
33687c478bd9Sstevel@tonic-gate 	ire_t *ire_list = NULL;
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
3371c793af95Ssangeeta 	ASSERT(((irb->irb_marks & IRB_MARK_FTABLE) && irb->irb_refcnt == 1) ||
3372c793af95Ssangeeta 	    (irb->irb_refcnt == 0));
3373c793af95Ssangeeta 	ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
33747c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_ire != NULL);
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
3377f4b3ec61Sdh155122 		ip_stack_t	*ipst = ire->ire_ipst;
3378f4b3ec61Sdh155122 
33797c478bd9Sstevel@tonic-gate 		ire1 = ire->ire_next;
33807c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
33817c478bd9Sstevel@tonic-gate 			ptpn = ire->ire_ptpn;
33827c478bd9Sstevel@tonic-gate 			ire1 = ire->ire_next;
33837c478bd9Sstevel@tonic-gate 			if (ire1)
33847c478bd9Sstevel@tonic-gate 				ire1->ire_ptpn = ptpn;
33857c478bd9Sstevel@tonic-gate 			*ptpn = ire1;
33867c478bd9Sstevel@tonic-gate 			ire->ire_ptpn = NULL;
33877c478bd9Sstevel@tonic-gate 			ire->ire_next = NULL;
33887c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_DEFAULT) {
33897c478bd9Sstevel@tonic-gate 				/*
33907c478bd9Sstevel@tonic-gate 				 * IRE is out of the list. We need to adjust
33917c478bd9Sstevel@tonic-gate 				 * the accounting before the caller drops
33927c478bd9Sstevel@tonic-gate 				 * the lock.
33937c478bd9Sstevel@tonic-gate 				 */
33947c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV6_VERSION) {
3395f4b3ec61Sdh155122 					ASSERT(ipst->
3396f4b3ec61Sdh155122 					    ips_ipv6_ire_default_count !=
3397f4b3ec61Sdh155122 					    0);
3398f4b3ec61Sdh155122 					ipst->ips_ipv6_ire_default_count--;
33997c478bd9Sstevel@tonic-gate 				}
34007c478bd9Sstevel@tonic-gate 			}
34017c478bd9Sstevel@tonic-gate 			/*
34027c478bd9Sstevel@tonic-gate 			 * We need to call ire_delete_v4 or ire_delete_v6
34037c478bd9Sstevel@tonic-gate 			 * to clean up the cache or the redirects pointing at
34047c478bd9Sstevel@tonic-gate 			 * the default gateway. We need to drop the lock
34057c478bd9Sstevel@tonic-gate 			 * as ire_flush_cache/ire_delete_host_redircts require
34067c478bd9Sstevel@tonic-gate 			 * so. But we can't drop the lock, as ire_unlink needs
34077c478bd9Sstevel@tonic-gate 			 * to atomically remove the ires from the list.
34087c478bd9Sstevel@tonic-gate 			 * So, create a temporary list of CONDEMNED ires
34097c478bd9Sstevel@tonic-gate 			 * for doing ire_delete_v4/ire_delete_v6 operations
34107c478bd9Sstevel@tonic-gate 			 * later on.
34117c478bd9Sstevel@tonic-gate 			 */
34127c478bd9Sstevel@tonic-gate 			ire->ire_next = ire_list;
34137c478bd9Sstevel@tonic-gate 			ire_list = ire;
34147c478bd9Sstevel@tonic-gate 		}
34157c478bd9Sstevel@tonic-gate 	}
3416c793af95Ssangeeta 	irb->irb_marks &= ~IRB_MARK_CONDEMNED;
34177c478bd9Sstevel@tonic-gate 	return (ire_list);
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate /*
34217c478bd9Sstevel@tonic-gate  * Delete all the cache entries with this 'addr'.  When IP gets a gratuitous
3422c793af95Ssangeeta  * ARP message on any of its interface queue, it scans the nce table and
3423c793af95Ssangeeta  * deletes and calls ndp_delete() for the appropriate nce. This action
3424c793af95Ssangeeta  * also deletes all the neighbor/ire cache entries for that address.
3425c793af95Ssangeeta  * This function is called from ip_arp_news in ip.c and also for
3426c793af95Ssangeeta  * ARP ioctl processing in ip_if.c. ip_ire_clookup_and_delete returns
3427c793af95Ssangeeta  * true if it finds a nce entry which is used by ip_arp_news to determine if
3428c793af95Ssangeeta  * it needs to do an ire_walk_v4. The return value is also  used for the
3429c793af95Ssangeeta  * same purpose by ARP IOCTL processing * in ip_if.c when deleting
3430c793af95Ssangeeta  * ARP entries. For SIOC*IFARP ioctls in addition to the address,
3431c793af95Ssangeeta  * ip_if->ipif_ill also needs to be matched.
34327c478bd9Sstevel@tonic-gate  */
34337c478bd9Sstevel@tonic-gate boolean_t
3434f4b3ec61Sdh155122 ip_ire_clookup_and_delete(ipaddr_t addr, ipif_t *ipif, ip_stack_t *ipst)
34357c478bd9Sstevel@tonic-gate {
34367c478bd9Sstevel@tonic-gate 	ill_t	*ill;
3437c793af95Ssangeeta 	nce_t	*nce;
34387c478bd9Sstevel@tonic-gate 
3439c793af95Ssangeeta 	ill = (ipif ? ipif->ipif_ill : NULL);
34407c478bd9Sstevel@tonic-gate 
3441c793af95Ssangeeta 	if (ill != NULL) {
3442c793af95Ssangeeta 		/*
3443c793af95Ssangeeta 		 * clean up the nce (and any relevant ire's) that matches
3444c793af95Ssangeeta 		 * on addr and ill.
3445c793af95Ssangeeta 		 */
3446c793af95Ssangeeta 		nce = ndp_lookup_v4(ill, &addr, B_FALSE);
3447c793af95Ssangeeta 		if (nce != NULL) {
3448c793af95Ssangeeta 			ndp_delete(nce);
3449c793af95Ssangeeta 			return (B_TRUE);
34507c478bd9Sstevel@tonic-gate 		}
3451c793af95Ssangeeta 	} else {
3452c793af95Ssangeeta 		/*
3453c793af95Ssangeeta 		 * ill is wildcard. clean up all nce's and
3454c793af95Ssangeeta 		 * ire's that match on addr
3455c793af95Ssangeeta 		 */
3456c793af95Ssangeeta 		nce_clookup_t cl;
34577c478bd9Sstevel@tonic-gate 
3458c793af95Ssangeeta 		cl.ncecl_addr = addr;
3459c793af95Ssangeeta 		cl.ncecl_found = B_FALSE;
34607c478bd9Sstevel@tonic-gate 
3461f4b3ec61Sdh155122 		ndp_walk_common(ipst->ips_ndp4, NULL,
3462c793af95Ssangeeta 		    (pfi_t)ip_nce_clookup_and_delete, (uchar_t *)&cl, B_TRUE);
3463c793af95Ssangeeta 
3464c793af95Ssangeeta 		/*
3465c793af95Ssangeeta 		 *  ncecl_found would be set by ip_nce_clookup_and_delete if
3466c793af95Ssangeeta 		 *  we found a matching nce.
3467c793af95Ssangeeta 		 */
3468c793af95Ssangeeta 		return (cl.ncecl_found);
3469c793af95Ssangeeta 	}
3470c793af95Ssangeeta 	return (B_FALSE);
3471c793af95Ssangeeta 
3472c793af95Ssangeeta }
3473c793af95Ssangeeta 
3474c793af95Ssangeeta /* Delete the supplied nce if its nce_addr matches the supplied address */
3475c793af95Ssangeeta static void
3476c793af95Ssangeeta ip_nce_clookup_and_delete(nce_t *nce, void *arg)
3477c793af95Ssangeeta {
3478c793af95Ssangeeta 	nce_clookup_t *cl = (nce_clookup_t *)arg;
3479c793af95Ssangeeta 	ipaddr_t nce_addr;
3480c793af95Ssangeeta 
3481c793af95Ssangeeta 	IN6_V4MAPPED_TO_IPADDR(&nce->nce_addr, nce_addr);
3482c793af95Ssangeeta 	if (nce_addr == cl->ncecl_addr) {
3483c793af95Ssangeeta 		cl->ncecl_found = B_TRUE;
3484c793af95Ssangeeta 		/* clean up the nce (and any relevant ire's) */
3485c793af95Ssangeeta 		ndp_delete(nce);
3486c793af95Ssangeeta 	}
3487c793af95Ssangeeta }
3488c793af95Ssangeeta 
3489c793af95Ssangeeta /*
3490c793af95Ssangeeta  * Clean up the radix node for this ire. Must be called by IRB_REFRELE
3491c793af95Ssangeeta  * when there are no ire's left in the bucket. Returns TRUE if the bucket
3492c793af95Ssangeeta  * is deleted and freed.
3493c793af95Ssangeeta  */
3494c793af95Ssangeeta boolean_t
3495c793af95Ssangeeta irb_inactive(irb_t *irb)
3496c793af95Ssangeeta {
3497c793af95Ssangeeta 	struct rt_entry *rt;
3498c793af95Ssangeeta 	struct radix_node *rn;
3499f4b3ec61Sdh155122 	ip_stack_t *ipst = irb->irb_ipst;
3500f4b3ec61Sdh155122 
3501f4b3ec61Sdh155122 	ASSERT(irb->irb_ipst != NULL);
3502c793af95Ssangeeta 
3503c793af95Ssangeeta 	rt = IRB2RT(irb);
3504c793af95Ssangeeta 	rn = (struct radix_node *)rt;
3505c793af95Ssangeeta 
3506c793af95Ssangeeta 	/* first remove it from the radix tree. */
3507f4b3ec61Sdh155122 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
3508c793af95Ssangeeta 	rw_enter(&irb->irb_lock, RW_WRITER);
3509c793af95Ssangeeta 	if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
3510f4b3ec61Sdh155122 		rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
3511f4b3ec61Sdh155122 		    ipst->ips_ip_ftable);
3512c793af95Ssangeeta 		DTRACE_PROBE1(irb__free, rt_t *,  rt);
3513c793af95Ssangeeta 		ASSERT((void *)rn == (void *)rt);
3514c793af95Ssangeeta 		Free(rt, rt_entry_cache);
3515c793af95Ssangeeta 		/* irb_lock is freed */
3516f4b3ec61Sdh155122 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
3517c793af95Ssangeeta 		return (B_TRUE);
3518c793af95Ssangeeta 	}
3519c793af95Ssangeeta 	rw_exit(&irb->irb_lock);
3520f4b3ec61Sdh155122 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
3521c793af95Ssangeeta 	return (B_FALSE);
35227c478bd9Sstevel@tonic-gate }
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate /*
35257c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
35267c478bd9Sstevel@tonic-gate  */
35277c478bd9Sstevel@tonic-gate void
35287c478bd9Sstevel@tonic-gate ire_delete(ire_t *ire)
35297c478bd9Sstevel@tonic-gate {
35307c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
35317c478bd9Sstevel@tonic-gate 	ire_t	**ptpn;
35327c478bd9Sstevel@tonic-gate 	irb_t *irb;
3533f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
35347c478bd9Sstevel@tonic-gate 
3535c793af95Ssangeeta 	if ((irb = ire->ire_bucket) == NULL) {
35367c478bd9Sstevel@tonic-gate 		/*
35377c478bd9Sstevel@tonic-gate 		 * It was never inserted in the list. Should call REFRELE
35387c478bd9Sstevel@tonic-gate 		 * to free this IRE.
35397c478bd9Sstevel@tonic-gate 		 */
35407c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
35417c478bd9Sstevel@tonic-gate 		return;
35427c478bd9Sstevel@tonic-gate 	}
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	rw_enter(&irb->irb_lock, RW_WRITER);
35457c478bd9Sstevel@tonic-gate 
3546c793af95Ssangeeta 	if (irb->irb_rr_origin == ire) {
3547c793af95Ssangeeta 		irb->irb_rr_origin = NULL;
3548c793af95Ssangeeta 	}
3549c793af95Ssangeeta 
35507c478bd9Sstevel@tonic-gate 	/*
35517c478bd9Sstevel@tonic-gate 	 * In case of V4 we might still be waiting for fastpath ack.
35527c478bd9Sstevel@tonic-gate 	 */
3553516bda92Ssowmini 	if (ire->ire_ipversion == IPV4_VERSION &&
3554516bda92Ssowmini 	    (ire->ire_type == IRE_CACHE ||
3555516bda92Ssowmini 	    (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL))) {
3556516bda92Ssowmini 		ASSERT(ire->ire_nce != NULL);
3557516bda92Ssowmini 		nce_fastpath_list_delete(ire->ire_nce);
35587c478bd9Sstevel@tonic-gate 	}
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	if (ire->ire_ptpn == NULL) {
35617c478bd9Sstevel@tonic-gate 		/*
35627c478bd9Sstevel@tonic-gate 		 * Some other thread has removed us from the list.
35637c478bd9Sstevel@tonic-gate 		 * It should have done the REFRELE for us.
35647c478bd9Sstevel@tonic-gate 		 */
35657c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
35667c478bd9Sstevel@tonic-gate 		return;
35677c478bd9Sstevel@tonic-gate 	}
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate 	if (irb->irb_refcnt != 0) {
35707c478bd9Sstevel@tonic-gate 		/*
35717c478bd9Sstevel@tonic-gate 		 * The last thread to leave this bucket will
35727c478bd9Sstevel@tonic-gate 		 * delete this ire.
35737c478bd9Sstevel@tonic-gate 		 */
35747c478bd9Sstevel@tonic-gate 		if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) {
35757c478bd9Sstevel@tonic-gate 			irb->irb_ire_cnt--;
35767c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY)
35777c478bd9Sstevel@tonic-gate 				irb->irb_tmp_ire_cnt--;
35787c478bd9Sstevel@tonic-gate 			ire->ire_marks |= IRE_MARK_CONDEMNED;
35797c478bd9Sstevel@tonic-gate 		}
3580c793af95Ssangeeta 		irb->irb_marks |= IRB_MARK_CONDEMNED;
35817c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
35827c478bd9Sstevel@tonic-gate 		return;
35837c478bd9Sstevel@tonic-gate 	}
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	/*
35867c478bd9Sstevel@tonic-gate 	 * Normally to delete an ire, we walk the bucket. While we
35877c478bd9Sstevel@tonic-gate 	 * walk the bucket, we normally bump up irb_refcnt and hence
35887c478bd9Sstevel@tonic-gate 	 * we return from above where we mark CONDEMNED and the ire
35897c478bd9Sstevel@tonic-gate 	 * gets deleted from ire_unlink. This case is where somebody
35907c478bd9Sstevel@tonic-gate 	 * knows the ire e.g by doing a lookup, and wants to delete the
35917c478bd9Sstevel@tonic-gate 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
35927c478bd9Sstevel@tonic-gate 	 * the bucket.
35937c478bd9Sstevel@tonic-gate 	 */
35947c478bd9Sstevel@tonic-gate 	ptpn = ire->ire_ptpn;
35957c478bd9Sstevel@tonic-gate 	ire1 = ire->ire_next;
35967c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
35977c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = ptpn;
35987c478bd9Sstevel@tonic-gate 	ASSERT(ptpn != NULL);
35997c478bd9Sstevel@tonic-gate 	*ptpn = ire1;
36007c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = NULL;
36017c478bd9Sstevel@tonic-gate 	ire->ire_next = NULL;
36027c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
3603f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
36047c478bd9Sstevel@tonic-gate 	} else {
3605f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
36067c478bd9Sstevel@tonic-gate 	}
36077c478bd9Sstevel@tonic-gate 	/*
36087c478bd9Sstevel@tonic-gate 	 * ip_wput/ip_wput_v6 checks this flag to see whether
36097c478bd9Sstevel@tonic-gate 	 * it should still use the cached ire or not.
36107c478bd9Sstevel@tonic-gate 	 */
36117c478bd9Sstevel@tonic-gate 	ire->ire_marks |= IRE_MARK_CONDEMNED;
36127c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
36137c478bd9Sstevel@tonic-gate 		/*
36147c478bd9Sstevel@tonic-gate 		 * IRE is out of the list. We need to adjust the
36157c478bd9Sstevel@tonic-gate 		 * accounting before we drop the lock.
36167c478bd9Sstevel@tonic-gate 		 */
36177c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV6_VERSION) {
3618f4b3ec61Sdh155122 			ASSERT(ipst->ips_ipv6_ire_default_count != 0);
3619f4b3ec61Sdh155122 			ipst->ips_ipv6_ire_default_count--;
36207c478bd9Sstevel@tonic-gate 		}
36217c478bd9Sstevel@tonic-gate 	}
36227c478bd9Sstevel@tonic-gate 	irb->irb_ire_cnt--;
3623c793af95Ssangeeta 
36247c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
36257c478bd9Sstevel@tonic-gate 		irb->irb_tmp_ire_cnt--;
36267c478bd9Sstevel@tonic-gate 	rw_exit(&irb->irb_lock);
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
36297c478bd9Sstevel@tonic-gate 		ire_delete_v6(ire);
36307c478bd9Sstevel@tonic-gate 	} else {
36317c478bd9Sstevel@tonic-gate 		ire_delete_v4(ire);
36327c478bd9Sstevel@tonic-gate 	}
36337c478bd9Sstevel@tonic-gate 	/*
36347c478bd9Sstevel@tonic-gate 	 * We removed it from the list. Decrement the
36357c478bd9Sstevel@tonic-gate 	 * reference count.
36367c478bd9Sstevel@tonic-gate 	 */
36377c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
36387c478bd9Sstevel@tonic-gate }
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate /*
36417c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
36427c478bd9Sstevel@tonic-gate  * All calls should use ire_delete().
36437c478bd9Sstevel@tonic-gate  * Sometimes called as writer though not required by this function.
36447c478bd9Sstevel@tonic-gate  *
36457c478bd9Sstevel@tonic-gate  * NOTE : This function is called only if the ire was added
36467c478bd9Sstevel@tonic-gate  * in the list.
36477c478bd9Sstevel@tonic-gate  */
36487c478bd9Sstevel@tonic-gate static void
36497c478bd9Sstevel@tonic-gate ire_delete_v4(ire_t *ire)
36507c478bd9Sstevel@tonic-gate {
3651f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
3652f4b3ec61Sdh155122 
36537c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt >= 1);
36547c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
36577c478bd9Sstevel@tonic-gate 		ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
36587c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
36597c478bd9Sstevel@tonic-gate 		/*
36607c478bd9Sstevel@tonic-gate 		 * when a default gateway is going away
36617c478bd9Sstevel@tonic-gate 		 * delete all the host redirects pointing at that
36627c478bd9Sstevel@tonic-gate 		 * gateway.
36637c478bd9Sstevel@tonic-gate 		 */
3664f4b3ec61Sdh155122 		ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
36657c478bd9Sstevel@tonic-gate 	}
36667c478bd9Sstevel@tonic-gate }
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate /*
36697c478bd9Sstevel@tonic-gate  * IRE_REFRELE/ire_refrele are the only caller of the function. It calls
36707c478bd9Sstevel@tonic-gate  * to free the ire when the reference count goes to zero.
36717c478bd9Sstevel@tonic-gate  */
36727c478bd9Sstevel@tonic-gate void
36737c478bd9Sstevel@tonic-gate ire_inactive(ire_t *ire)
36747c478bd9Sstevel@tonic-gate {
36757c478bd9Sstevel@tonic-gate 	nce_t	*nce;
36767c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
36777c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
36787c478bd9Sstevel@tonic-gate 	ipif_t	*ipif;
36797c478bd9Sstevel@tonic-gate 	boolean_t	need_wakeup = B_FALSE;
3680c793af95Ssangeeta 	irb_t 	*irb;
3681f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt == 0);
36847c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ptpn == NULL);
36857c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_next == NULL);
36867c478bd9Sstevel@tonic-gate 
3687c793af95Ssangeeta 	if (ire->ire_gw_secattr != NULL) {
3688c793af95Ssangeeta 		ire_gw_secattr_free(ire->ire_gw_secattr);
3689c793af95Ssangeeta 		ire->ire_gw_secattr = NULL;
3690c793af95Ssangeeta 	}
3691c793af95Ssangeeta 
3692c793af95Ssangeeta 	if (ire->ire_mp != NULL) {
3693c793af95Ssangeeta 		ASSERT(ire->ire_bucket == NULL);
3694c793af95Ssangeeta 		mutex_destroy(&ire->ire_lock);
3695f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
3696c793af95Ssangeeta 		if (ire->ire_nce != NULL)
3697c793af95Ssangeeta 			NCE_REFRELE_NOTR(ire->ire_nce);
3698c793af95Ssangeeta 		freeb(ire->ire_mp);
3699c793af95Ssangeeta 		return;
3700c793af95Ssangeeta 	}
3701c793af95Ssangeeta 
37027c478bd9Sstevel@tonic-gate 	if ((nce = ire->ire_nce) != NULL) {
37037c478bd9Sstevel@tonic-gate 		NCE_REFRELE_NOTR(nce);
37047c478bd9Sstevel@tonic-gate 		ire->ire_nce = NULL;
37057c478bd9Sstevel@tonic-gate 	}
3706c793af95Ssangeeta 
37077c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif == NULL)
37087c478bd9Sstevel@tonic-gate 		goto end;
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate 	ipif = ire->ire_ipif;
37117c478bd9Sstevel@tonic-gate 	ill = ipif->ipif_ill;
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	if (ire->ire_bucket == NULL) {
37147c478bd9Sstevel@tonic-gate 		/* The ire was never inserted in the table. */
37157c478bd9Sstevel@tonic-gate 		goto end;
37167c478bd9Sstevel@tonic-gate 	}
37177c478bd9Sstevel@tonic-gate 
37187c478bd9Sstevel@tonic-gate 	/*
37197c478bd9Sstevel@tonic-gate 	 * ipif_ire_cnt on this ipif goes down by 1. If the ire_stq is
3720*5c0b7edeSseb 	 * non-null ill_ire_count also goes down by 1.
37217c478bd9Sstevel@tonic-gate 	 *
37227c478bd9Sstevel@tonic-gate 	 * The ipif that is associated with an ire is ire->ire_ipif and
37237c478bd9Sstevel@tonic-gate 	 * hence when the ire->ire_ipif->ipif_ire_cnt drops to zero we call
37247c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail. Usually stq_ill is null or the same as
37257c478bd9Sstevel@tonic-gate 	 * ire->ire_ipif->ipif_ill. So nothing more needs to be done. Only
37267c478bd9Sstevel@tonic-gate 	 * in the case of IRE_CACHES when IPMP is used, stq_ill can be
37277c478bd9Sstevel@tonic-gate 	 * different. If this is different from ire->ire_ipif->ipif_ill and
37287c478bd9Sstevel@tonic-gate 	 * if the ill_ire_cnt on the stq_ill also has dropped to zero, we call
3729*5c0b7edeSseb 	 * ipif_ill_refrele_tail on the stq_ill.
37307c478bd9Sstevel@tonic-gate 	 */
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
37337c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
37347c478bd9Sstevel@tonic-gate 
3735*5c0b7edeSseb 	if (stq_ill == NULL || stq_ill == ill) {
37367c478bd9Sstevel@tonic-gate 		/* Optimize the most common case */
37377c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
37387c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
37397c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
37407c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0)
37417c478bd9Sstevel@tonic-gate 			need_wakeup = B_TRUE;
37427c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
37437c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
37447c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
37457c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)
37467c478bd9Sstevel@tonic-gate 				need_wakeup = B_TRUE;
37477c478bd9Sstevel@tonic-gate 		}
37487c478bd9Sstevel@tonic-gate 		if (need_wakeup) {
37497c478bd9Sstevel@tonic-gate 			/* Drops the ill lock */
37507c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
37517c478bd9Sstevel@tonic-gate 		} else {
37527c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
37537c478bd9Sstevel@tonic-gate 		}
37547c478bd9Sstevel@tonic-gate 	} else {
37557c478bd9Sstevel@tonic-gate 		/*
37567c478bd9Sstevel@tonic-gate 		 * We can't grab all the ill locks at the same time.
37577c478bd9Sstevel@tonic-gate 		 * It can lead to recursive lock enter in the call to
37587c478bd9Sstevel@tonic-gate 		 * ipif_ill_refrele_tail and later. Instead do it 1 at
37597c478bd9Sstevel@tonic-gate 		 * a time.
37607c478bd9Sstevel@tonic-gate 		 */
37617c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
37627c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
37637c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
37647c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0) {
37657c478bd9Sstevel@tonic-gate 			/* Drops the lock */
37667c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
37677c478bd9Sstevel@tonic-gate 		} else {
37687c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
37697c478bd9Sstevel@tonic-gate 		}
37707c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
37717c478bd9Sstevel@tonic-gate 			mutex_enter(&stq_ill->ill_lock);
37727c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
37737c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
37747c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)  {
37757c478bd9Sstevel@tonic-gate 				/* Drops the ill lock */
37767c478bd9Sstevel@tonic-gate 				ipif_ill_refrele_tail(stq_ill);
37777c478bd9Sstevel@tonic-gate 			} else {
37787c478bd9Sstevel@tonic-gate 				mutex_exit(&stq_ill->ill_lock);
37797c478bd9Sstevel@tonic-gate 			}
37807c478bd9Sstevel@tonic-gate 		}
37817c478bd9Sstevel@tonic-gate 	}
37827c478bd9Sstevel@tonic-gate end:
37837c478bd9Sstevel@tonic-gate 	/* This should be true for both V4 and V6 */
37847c478bd9Sstevel@tonic-gate 
3785c793af95Ssangeeta 	if ((ire->ire_type & IRE_FORWARDTABLE) &&
3786c793af95Ssangeeta 	    (ire->ire_ipversion == IPV4_VERSION) &&
3787c793af95Ssangeeta 	    ((irb = ire->ire_bucket) != NULL)) {
3788c793af95Ssangeeta 		rw_enter(&irb->irb_lock, RW_WRITER);
3789c793af95Ssangeeta 		irb->irb_nire--;
3790c793af95Ssangeeta 		/*
3791c793af95Ssangeeta 		 * Instead of examining the conditions for freeing
3792c793af95Ssangeeta 		 * the radix node here, we do it by calling
3793c793af95Ssangeeta 		 * IRB_REFRELE which is a single point in the code
3794c793af95Ssangeeta 		 * that embeds that logic. Bump up the refcnt to
3795c793af95Ssangeeta 		 * be able to call IRB_REFRELE
3796c793af95Ssangeeta 		 */
3797c793af95Ssangeeta 		IRB_REFHOLD_LOCKED(irb);
3798c793af95Ssangeeta 		rw_exit(&irb->irb_lock);
3799c793af95Ssangeeta 		IRB_REFRELE(irb);
3800c793af95Ssangeeta 	}
38017c478bd9Sstevel@tonic-gate 	ire->ire_ipif = NULL;
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
38047c478bd9Sstevel@tonic-gate 	ire_trace_inactive(ire);
38057c478bd9Sstevel@tonic-gate #endif
38067c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire->ire_lock);
38077c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
3808f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
38097c478bd9Sstevel@tonic-gate 	} else {
3810f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
38117c478bd9Sstevel@tonic-gate 	}
3812c793af95Ssangeeta 	ASSERT(ire->ire_mp == NULL);
38137c478bd9Sstevel@tonic-gate 	/* Has been allocated out of the cache */
38147c478bd9Sstevel@tonic-gate 	kmem_cache_free(ire_cache, ire);
38157c478bd9Sstevel@tonic-gate }
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate /*
38186bdb8e66Sdd193516  * ire_walk routine to delete all IRE_CACHE/IRE_HOST types redirect
38196bdb8e66Sdd193516  * entries that have a given gateway address.
38207c478bd9Sstevel@tonic-gate  */
38217c478bd9Sstevel@tonic-gate void
38227c478bd9Sstevel@tonic-gate ire_delete_cache_gw(ire_t *ire, char *cp)
38237c478bd9Sstevel@tonic-gate {
38247c478bd9Sstevel@tonic-gate 	ipaddr_t	gw_addr;
38257c478bd9Sstevel@tonic-gate 
38266bdb8e66Sdd193516 	if (!(ire->ire_type & IRE_CACHE) &&
38276bdb8e66Sdd193516 	    !(ire->ire_flags & RTF_DYNAMIC))
38287c478bd9Sstevel@tonic-gate 		return;
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 	bcopy(cp, &gw_addr, sizeof (gw_addr));
38317c478bd9Sstevel@tonic-gate 	if (ire->ire_gateway_addr == gw_addr) {
38327c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_delete_cache_gw: deleted 0x%x type %d to 0x%x\n",
38337c478bd9Sstevel@tonic-gate 		    (int)ntohl(ire->ire_addr), ire->ire_type,
38347c478bd9Sstevel@tonic-gate 		    (int)ntohl(ire->ire_gateway_addr)));
38357c478bd9Sstevel@tonic-gate 		ire_delete(ire);
38367c478bd9Sstevel@tonic-gate 	}
38377c478bd9Sstevel@tonic-gate }
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate /*
38407c478bd9Sstevel@tonic-gate  * Remove all IRE_CACHE entries that match the ire specified.
38417c478bd9Sstevel@tonic-gate  *
38427c478bd9Sstevel@tonic-gate  * The flag argument indicates if the flush request is due to addition
38437c478bd9Sstevel@tonic-gate  * of new route (IRE_FLUSH_ADD) or deletion of old route (IRE_FLUSH_DELETE).
38447c478bd9Sstevel@tonic-gate  *
38457c478bd9Sstevel@tonic-gate  * This routine takes only the IREs from the forwarding table and flushes
38467c478bd9Sstevel@tonic-gate  * the corresponding entries from the cache table.
38477c478bd9Sstevel@tonic-gate  *
38487c478bd9Sstevel@tonic-gate  * When flushing due to the deletion of an old route, it
38497c478bd9Sstevel@tonic-gate  * just checks the cache handles (ire_phandle and ire_ihandle) and
38507c478bd9Sstevel@tonic-gate  * deletes the ones that match.
38517c478bd9Sstevel@tonic-gate  *
38527c478bd9Sstevel@tonic-gate  * When flushing due to the creation of a new route, it checks
38537c478bd9Sstevel@tonic-gate  * if a cache entry's address matches the one in the IRE and
38547c478bd9Sstevel@tonic-gate  * that the cache entry's parent has a less specific mask than the
38557c478bd9Sstevel@tonic-gate  * one in IRE. The destination of such a cache entry could be the
38567c478bd9Sstevel@tonic-gate  * gateway for other cache entries, so we need to flush those as
38577c478bd9Sstevel@tonic-gate  * well by looking for gateway addresses matching the IRE's address.
38587c478bd9Sstevel@tonic-gate  */
38597c478bd9Sstevel@tonic-gate void
38607c478bd9Sstevel@tonic-gate ire_flush_cache_v4(ire_t *ire, int flag)
38617c478bd9Sstevel@tonic-gate {
38627c478bd9Sstevel@tonic-gate 	int i;
38637c478bd9Sstevel@tonic-gate 	ire_t *cire;
38647c478bd9Sstevel@tonic-gate 	irb_t *irb;
3865f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_CACHE)
38687c478bd9Sstevel@tonic-gate 		return;
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 	/*
38717c478bd9Sstevel@tonic-gate 	 * If a default is just created, there is no point
38727c478bd9Sstevel@tonic-gate 	 * in going through the cache, as there will not be any
38737c478bd9Sstevel@tonic-gate 	 * cached ires.
38747c478bd9Sstevel@tonic-gate 	 */
38757c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD)
38767c478bd9Sstevel@tonic-gate 		return;
38777c478bd9Sstevel@tonic-gate 	if (flag == IRE_FLUSH_ADD) {
38787c478bd9Sstevel@tonic-gate 		/*
38797c478bd9Sstevel@tonic-gate 		 * This selective flush is due to the addition of
38807c478bd9Sstevel@tonic-gate 		 * new IRE.
38817c478bd9Sstevel@tonic-gate 		 */
3882f4b3ec61Sdh155122 		for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
3883f4b3ec61Sdh155122 			irb = &ipst->ips_ip_cache_table[i];
38847c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
38857c478bd9Sstevel@tonic-gate 				continue;
38867c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
38877c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
38887c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
38897c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
38907c478bd9Sstevel@tonic-gate 					continue;
38917c478bd9Sstevel@tonic-gate 				/*
38927c478bd9Sstevel@tonic-gate 				 * If 'cire' belongs to the same subnet
38937c478bd9Sstevel@tonic-gate 				 * as the new ire being added, and 'cire'
38947c478bd9Sstevel@tonic-gate 				 * is derived from a prefix that is less
38957c478bd9Sstevel@tonic-gate 				 * specific than the new ire being added,
38967c478bd9Sstevel@tonic-gate 				 * we need to flush 'cire'; for instance,
38977c478bd9Sstevel@tonic-gate 				 * when a new interface comes up.
38987c478bd9Sstevel@tonic-gate 				 */
38997c478bd9Sstevel@tonic-gate 				if (((cire->ire_addr & ire->ire_mask) ==
39007c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) &&
39017c478bd9Sstevel@tonic-gate 				    (ip_mask_to_plen(cire->ire_cmask) <=
39027c478bd9Sstevel@tonic-gate 				    ire->ire_masklen)) {
39037c478bd9Sstevel@tonic-gate 					ire_delete(cire);
39047c478bd9Sstevel@tonic-gate 					continue;
39057c478bd9Sstevel@tonic-gate 				}
39067c478bd9Sstevel@tonic-gate 				/*
39077c478bd9Sstevel@tonic-gate 				 * This is the case when the ire_gateway_addr
39087c478bd9Sstevel@tonic-gate 				 * of 'cire' belongs to the same subnet as
39097c478bd9Sstevel@tonic-gate 				 * the new ire being added.
39107c478bd9Sstevel@tonic-gate 				 * Flushing such ires is sometimes required to
39117c478bd9Sstevel@tonic-gate 				 * avoid misrouting: say we have a machine with
39127c478bd9Sstevel@tonic-gate 				 * two interfaces (I1 and I2), a default router
39137c478bd9Sstevel@tonic-gate 				 * R on the I1 subnet, and a host route to an
39147c478bd9Sstevel@tonic-gate 				 * off-link destination D with a gateway G on
39157c478bd9Sstevel@tonic-gate 				 * the I2 subnet.
39167c478bd9Sstevel@tonic-gate 				 * Under normal operation, we will have an
39177c478bd9Sstevel@tonic-gate 				 * on-link cache entry for G and an off-link
39187c478bd9Sstevel@tonic-gate 				 * cache entry for D with G as ire_gateway_addr,
39197c478bd9Sstevel@tonic-gate 				 * traffic to D will reach its destination
39207c478bd9Sstevel@tonic-gate 				 * through gateway G.
39217c478bd9Sstevel@tonic-gate 				 * If the administrator does 'ifconfig I2 down',
39227c478bd9Sstevel@tonic-gate 				 * the cache entries for D and G will be
39237c478bd9Sstevel@tonic-gate 				 * flushed. However, G will now be resolved as
39247c478bd9Sstevel@tonic-gate 				 * an off-link destination using R (the default
39257c478bd9Sstevel@tonic-gate 				 * router) as gateway. Then D will also be
39267c478bd9Sstevel@tonic-gate 				 * resolved as an off-link destination using G
39277c478bd9Sstevel@tonic-gate 				 * as gateway - this behavior is due to
39287c478bd9Sstevel@tonic-gate 				 * compatibility reasons, see comment in
39297c478bd9Sstevel@tonic-gate 				 * ire_ihandle_lookup_offlink(). Traffic to D
39307c478bd9Sstevel@tonic-gate 				 * will go to the router R and probably won't
39317c478bd9Sstevel@tonic-gate 				 * reach the destination.
39327c478bd9Sstevel@tonic-gate 				 * The administrator then does 'ifconfig I2 up'.
39337c478bd9Sstevel@tonic-gate 				 * Since G is on the I2 subnet, this routine
39347c478bd9Sstevel@tonic-gate 				 * will flush its cache entry. It must also
39357c478bd9Sstevel@tonic-gate 				 * flush the cache entry for D, otherwise
39367c478bd9Sstevel@tonic-gate 				 * traffic will stay misrouted until the IRE
39377c478bd9Sstevel@tonic-gate 				 * times out.
39387c478bd9Sstevel@tonic-gate 				 */
39397c478bd9Sstevel@tonic-gate 				if ((cire->ire_gateway_addr & ire->ire_mask) ==
39407c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) {
39417c478bd9Sstevel@tonic-gate 					ire_delete(cire);
39427c478bd9Sstevel@tonic-gate 					continue;
39437c478bd9Sstevel@tonic-gate 				}
39447c478bd9Sstevel@tonic-gate 			}
39457c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
39467c478bd9Sstevel@tonic-gate 		}
39477c478bd9Sstevel@tonic-gate 	} else {
39487c478bd9Sstevel@tonic-gate 		/*
39497c478bd9Sstevel@tonic-gate 		 * delete the cache entries based on
39507c478bd9Sstevel@tonic-gate 		 * handle in the IRE as this IRE is
39517c478bd9Sstevel@tonic-gate 		 * being deleted/changed.
39527c478bd9Sstevel@tonic-gate 		 */
3953f4b3ec61Sdh155122 		for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
3954f4b3ec61Sdh155122 			irb = &ipst->ips_ip_cache_table[i];
39557c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
39567c478bd9Sstevel@tonic-gate 				continue;
39577c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
39587c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
39597c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
39607c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
39617c478bd9Sstevel@tonic-gate 					continue;
39627c478bd9Sstevel@tonic-gate 				if ((cire->ire_phandle == 0 ||
39637c478bd9Sstevel@tonic-gate 				    cire->ire_phandle != ire->ire_phandle) &&
39647c478bd9Sstevel@tonic-gate 				    (cire->ire_ihandle == 0 ||
39657c478bd9Sstevel@tonic-gate 				    cire->ire_ihandle != ire->ire_ihandle))
39667c478bd9Sstevel@tonic-gate 					continue;
39677c478bd9Sstevel@tonic-gate 				ire_delete(cire);
39687c478bd9Sstevel@tonic-gate 			}
39697c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
39707c478bd9Sstevel@tonic-gate 		}
39717c478bd9Sstevel@tonic-gate 	}
39727c478bd9Sstevel@tonic-gate }
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate /*
39757c478bd9Sstevel@tonic-gate  * Matches the arguments passed with the values in the ire.
39767c478bd9Sstevel@tonic-gate  *
39777c478bd9Sstevel@tonic-gate  * Note: for match types that match using "ipif" passed in, ipif
39787c478bd9Sstevel@tonic-gate  * must be checked for non-NULL before calling this routine.
39797c478bd9Sstevel@tonic-gate  */
3980c793af95Ssangeeta boolean_t
39817c478bd9Sstevel@tonic-gate ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
398245916cd2Sjpk     int type, const ipif_t *ipif, zoneid_t zoneid, uint32_t ihandle,
398345916cd2Sjpk     const ts_label_t *tsl, int match_flags)
39847c478bd9Sstevel@tonic-gate {
39857c478bd9Sstevel@tonic-gate 	ill_t *ire_ill = NULL, *dst_ill;
39867c478bd9Sstevel@tonic-gate 	ill_t *ipif_ill = NULL;
39877c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
39887c478bd9Sstevel@tonic-gate 	ill_group_t *ipif_ill_group = NULL;
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
39917c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
39927c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP))) ||
39937c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
39947c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_WQ));
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 	/*
39977c478bd9Sstevel@tonic-gate 	 * HIDDEN cache entries have to be looked up specifically with
39987c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_MARK_HIDDEN. MATCH_IRE_MARK_HIDDEN is usually set
39997c478bd9Sstevel@tonic-gate 	 * when the interface is FAILED or INACTIVE. In that case,
40007c478bd9Sstevel@tonic-gate 	 * any IRE_CACHES that exists should be marked with
40017c478bd9Sstevel@tonic-gate 	 * IRE_MARK_HIDDEN. So, we don't really need to match below
40027c478bd9Sstevel@tonic-gate 	 * for IRE_MARK_HIDDEN. But we do so for consistency.
40037c478bd9Sstevel@tonic-gate 	 */
40047c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_MARK_HIDDEN) &&
40057c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_HIDDEN))
40067c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40077c478bd9Sstevel@tonic-gate 
400843d18f1cSpriyanka 	/*
400943d18f1cSpriyanka 	 * MATCH_IRE_MARK_PRIVATE_ADDR is set when IP_NEXTHOP option
401043d18f1cSpriyanka 	 * is used. In that case the routing table is bypassed and the
401143d18f1cSpriyanka 	 * packets are sent directly to the specified nexthop. The
401243d18f1cSpriyanka 	 * IRE_CACHE entry representing this route should be marked
401343d18f1cSpriyanka 	 * with IRE_MARK_PRIVATE_ADDR.
401443d18f1cSpriyanka 	 */
401543d18f1cSpriyanka 
401643d18f1cSpriyanka 	if (!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR) &&
401743d18f1cSpriyanka 	    (ire->ire_marks & IRE_MARK_PRIVATE_ADDR))
401843d18f1cSpriyanka 		return (B_FALSE);
401943d18f1cSpriyanka 
402045916cd2Sjpk 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
402145916cd2Sjpk 	    ire->ire_zoneid != ALL_ZONES) {
40227c478bd9Sstevel@tonic-gate 		/*
40237c478bd9Sstevel@tonic-gate 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid is
40247c478bd9Sstevel@tonic-gate 		 * valid and does not match that of ire_zoneid, a failure to
40257c478bd9Sstevel@tonic-gate 		 * match is reported at this point. Otherwise, since some IREs
40267c478bd9Sstevel@tonic-gate 		 * that are available in the global zone can be used in local
40277c478bd9Sstevel@tonic-gate 		 * zones, additional checks need to be performed:
40287c478bd9Sstevel@tonic-gate 		 *
40297c478bd9Sstevel@tonic-gate 		 *	IRE_BROADCAST, IRE_CACHE and IRE_LOOPBACK
40307c478bd9Sstevel@tonic-gate 		 *	entries should never be matched in this situation.
40317c478bd9Sstevel@tonic-gate 		 *
40327c478bd9Sstevel@tonic-gate 		 *	IRE entries that have an interface associated with them
40337c478bd9Sstevel@tonic-gate 		 *	should in general not match unless they are an IRE_LOCAL
40347c478bd9Sstevel@tonic-gate 		 *	or in the case when MATCH_IRE_DEFAULT has been set in
40357c478bd9Sstevel@tonic-gate 		 *	the caller.  In the case of the former, checking of the
40367c478bd9Sstevel@tonic-gate 		 *	other fields supplied should take place.
40377c478bd9Sstevel@tonic-gate 		 *
40387c478bd9Sstevel@tonic-gate 		 *	In the case where MATCH_IRE_DEFAULT has been set,
40397c478bd9Sstevel@tonic-gate 		 *	all of the ipif's associated with the IRE's ill are
40407c478bd9Sstevel@tonic-gate 		 *	checked to see if there is a matching zoneid.  If any
40417c478bd9Sstevel@tonic-gate 		 *	one ipif has a matching zoneid, this IRE is a
40427c478bd9Sstevel@tonic-gate 		 *	potential candidate so checking of the other fields
40437c478bd9Sstevel@tonic-gate 		 *	takes place.
40447c478bd9Sstevel@tonic-gate 		 *
40457c478bd9Sstevel@tonic-gate 		 *	In the case where the IRE_INTERFACE has a usable source
40467c478bd9Sstevel@tonic-gate 		 *	address (indicated by ill_usesrc_ifindex) in the
40477c478bd9Sstevel@tonic-gate 		 *	correct zone then it's permitted to return this IRE
40487c478bd9Sstevel@tonic-gate 		 */
40497c478bd9Sstevel@tonic-gate 		if (match_flags & MATCH_IRE_ZONEONLY)
40507c478bd9Sstevel@tonic-gate 			return (B_FALSE);
40517c478bd9Sstevel@tonic-gate 		if (ire->ire_type & (IRE_BROADCAST | IRE_CACHE | IRE_LOOPBACK))
40527c478bd9Sstevel@tonic-gate 			return (B_FALSE);
40537c478bd9Sstevel@tonic-gate 		/*
40547c478bd9Sstevel@tonic-gate 		 * Note, IRE_INTERFACE can have the stq as NULL. For
40557c478bd9Sstevel@tonic-gate 		 * example, if the default multicast route is tied to
40567c478bd9Sstevel@tonic-gate 		 * the loopback address.
40577c478bd9Sstevel@tonic-gate 		 */
40587c478bd9Sstevel@tonic-gate 		if ((ire->ire_type & IRE_INTERFACE) &&
40597c478bd9Sstevel@tonic-gate 		    (ire->ire_stq != NULL)) {
40607c478bd9Sstevel@tonic-gate 			dst_ill = (ill_t *)ire->ire_stq->q_ptr;
40617c478bd9Sstevel@tonic-gate 			/*
40627c478bd9Sstevel@tonic-gate 			 * If there is a usable source address in the
40637c478bd9Sstevel@tonic-gate 			 * zone, then it's ok to return an
40647c478bd9Sstevel@tonic-gate 			 * IRE_INTERFACE
40657c478bd9Sstevel@tonic-gate 			 */
40667c478bd9Sstevel@tonic-gate 			if (ipif_usesrc_avail(dst_ill, zoneid)) {
40677c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: dst_ill %p match %d\n",
40687c478bd9Sstevel@tonic-gate 				    (void *)dst_ill,
40697c478bd9Sstevel@tonic-gate 				    (ire->ire_addr == (addr & mask))));
40707c478bd9Sstevel@tonic-gate 			} else {
40717c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: src_ipif NULL"
40727c478bd9Sstevel@tonic-gate 				    " dst_ill %p\n", (void *)dst_ill));
40737c478bd9Sstevel@tonic-gate 				return (B_FALSE);
40747c478bd9Sstevel@tonic-gate 			}
40757c478bd9Sstevel@tonic-gate 		}
40767c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL && ire->ire_type != IRE_LOCAL &&
40777c478bd9Sstevel@tonic-gate 		    !(ire->ire_type & IRE_INTERFACE)) {
40787c478bd9Sstevel@tonic-gate 			ipif_t	*tipif;
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate 			if ((match_flags & MATCH_IRE_DEFAULT) == 0) {
40817c478bd9Sstevel@tonic-gate 				return (B_FALSE);
40827c478bd9Sstevel@tonic-gate 			}
40837c478bd9Sstevel@tonic-gate 			mutex_enter(&ire->ire_ipif->ipif_ill->ill_lock);
40847c478bd9Sstevel@tonic-gate 			for (tipif = ire->ire_ipif->ipif_ill->ill_ipif;
40857c478bd9Sstevel@tonic-gate 			    tipif != NULL; tipif = tipif->ipif_next) {
40867c478bd9Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(tipif) &&
40877c478bd9Sstevel@tonic-gate 				    (tipif->ipif_flags & IPIF_UP) &&
408845916cd2Sjpk 				    (tipif->ipif_zoneid == zoneid ||
408945916cd2Sjpk 				    tipif->ipif_zoneid == ALL_ZONES))
40907c478bd9Sstevel@tonic-gate 					break;
40917c478bd9Sstevel@tonic-gate 			}
40927c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_ipif->ipif_ill->ill_lock);
40937c478bd9Sstevel@tonic-gate 			if (tipif == NULL) {
40947c478bd9Sstevel@tonic-gate 				return (B_FALSE);
40957c478bd9Sstevel@tonic-gate 			}
40967c478bd9Sstevel@tonic-gate 		}
40977c478bd9Sstevel@tonic-gate 	}
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate 	/*
41007c478bd9Sstevel@tonic-gate 	 * For IRE_CACHES, MATCH_IRE_ILL/ILL_GROUP really means that
41017c478bd9Sstevel@tonic-gate 	 * somebody wants to send out on a particular interface which
41027c478bd9Sstevel@tonic-gate 	 * is given by ire_stq and hence use ire_stq to derive the ill
41037c478bd9Sstevel@tonic-gate 	 * value. ire_ipif for IRE_CACHES is just the means of getting
41047c478bd9Sstevel@tonic-gate 	 * a source address i.e ire_src_addr = ire->ire_ipif->ipif_src_addr.
41057c478bd9Sstevel@tonic-gate 	 * ire_to_ill does the right thing for this.
41067c478bd9Sstevel@tonic-gate 	 */
41077c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
41087c478bd9Sstevel@tonic-gate 		ire_ill = ire_to_ill(ire);
41097c478bd9Sstevel@tonic-gate 		if (ire_ill != NULL)
41107c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ill->ill_group;
41117c478bd9Sstevel@tonic-gate 		ipif_ill = ipif->ipif_ill;
41127c478bd9Sstevel@tonic-gate 		ipif_ill_group = ipif_ill->ill_group;
41137c478bd9Sstevel@tonic-gate 	}
41147c478bd9Sstevel@tonic-gate 
41157c478bd9Sstevel@tonic-gate 	if ((ire->ire_addr == (addr & mask)) &&
41167c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_GW)) ||
41177c478bd9Sstevel@tonic-gate 	    (ire->ire_gateway_addr == gateway)) &&
41187c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_TYPE)) ||
41197c478bd9Sstevel@tonic-gate 	    (ire->ire_type & type)) &&
41207c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_SRC)) ||
41217c478bd9Sstevel@tonic-gate 	    (ire->ire_src_addr == ipif->ipif_src_addr)) &&
41227c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IPIF)) ||
41237c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif == ipif)) &&
41247c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_MARK_HIDDEN)) ||
41257c478bd9Sstevel@tonic-gate 	    (ire->ire_type != IRE_CACHE ||
41267c478bd9Sstevel@tonic-gate 	    ire->ire_marks & IRE_MARK_HIDDEN)) &&
412743d18f1cSpriyanka 	    ((!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR)) ||
412843d18f1cSpriyanka 	    (ire->ire_type != IRE_CACHE ||
412943d18f1cSpriyanka 	    ire->ire_marks & IRE_MARK_PRIVATE_ADDR)) &&
41307c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
41317c478bd9Sstevel@tonic-gate 	    (ire_ill == ipif_ill)) &&
41327c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IHANDLE)) ||
41337c478bd9Sstevel@tonic-gate 	    (ire->ire_ihandle == ihandle)) &&
4134c793af95Ssangeeta 	    ((!(match_flags & MATCH_IRE_MASK)) ||
4135c793af95Ssangeeta 	    (ire->ire_mask == mask)) &&
41367c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
41377c478bd9Sstevel@tonic-gate 	    (ire_ill == ipif_ill) ||
41387c478bd9Sstevel@tonic-gate 	    (ire_ill_group != NULL &&
413945916cd2Sjpk 	    ire_ill_group == ipif_ill_group)) &&
414045916cd2Sjpk 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
414145916cd2Sjpk 	    (!is_system_labeled()) ||
414245916cd2Sjpk 	    (tsol_ire_match_gwattr(ire, tsl) == 0))) {
41437c478bd9Sstevel@tonic-gate 		/* We found the matched IRE */
41447c478bd9Sstevel@tonic-gate 		return (B_TRUE);
41457c478bd9Sstevel@tonic-gate 	}
41467c478bd9Sstevel@tonic-gate 	return (B_FALSE);
41477c478bd9Sstevel@tonic-gate }
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate /*
41517c478bd9Sstevel@tonic-gate  * Lookup for a route in all the tables
41527c478bd9Sstevel@tonic-gate  */
41537c478bd9Sstevel@tonic-gate ire_t *
41547c478bd9Sstevel@tonic-gate ire_route_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
415545916cd2Sjpk     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
4156f4b3ec61Sdh155122     const ts_label_t *tsl, int flags, ip_stack_t *ipst)
41577c478bd9Sstevel@tonic-gate {
41587c478bd9Sstevel@tonic-gate 	ire_t *ire = NULL;
41597c478bd9Sstevel@tonic-gate 
41607c478bd9Sstevel@tonic-gate 	/*
41617c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
41627c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
41637c478bd9Sstevel@tonic-gate 	 */
41647c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
41657c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
41667c478bd9Sstevel@tonic-gate 		return (NULL);
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	/*
41697c478bd9Sstevel@tonic-gate 	 * might be asking for a cache lookup,
41707c478bd9Sstevel@tonic-gate 	 * This is not best way to lookup cache,
41717c478bd9Sstevel@tonic-gate 	 * user should call ire_cache_lookup directly.
41727c478bd9Sstevel@tonic-gate 	 *
41737c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_TYPE was set, first lookup in the cache table and then
41747c478bd9Sstevel@tonic-gate 	 * in the forwarding table, if the applicable type flags were set.
41757c478bd9Sstevel@tonic-gate 	 */
41767c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_CACHETABLE) != 0) {
41777c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(addr, gateway, type, ipif, zoneid,
4178f4b3ec61Sdh155122 		    tsl, flags, ipst);
41797c478bd9Sstevel@tonic-gate 		if (ire != NULL)
41807c478bd9Sstevel@tonic-gate 			return (ire);
41817c478bd9Sstevel@tonic-gate 	}
41827c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_FORWARDTABLE) != 0) {
41837c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(addr, mask, gateway, type, ipif, pire,
4184f4b3ec61Sdh155122 		    zoneid, 0, tsl, flags, ipst);
41857c478bd9Sstevel@tonic-gate 	}
41867c478bd9Sstevel@tonic-gate 	return (ire);
41877c478bd9Sstevel@tonic-gate }
41887c478bd9Sstevel@tonic-gate 
41897c478bd9Sstevel@tonic-gate 
41907c478bd9Sstevel@tonic-gate /*
419145916cd2Sjpk  * Delete the IRE cache for the gateway and all IRE caches whose
419245916cd2Sjpk  * ire_gateway_addr points to this gateway, and allow them to
419345916cd2Sjpk  * be created on demand by ip_newroute.
419445916cd2Sjpk  */
419545916cd2Sjpk void
4196f4b3ec61Sdh155122 ire_clookup_delete_cache_gw(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
419745916cd2Sjpk {
419845916cd2Sjpk 	irb_t *irb;
419945916cd2Sjpk 	ire_t *ire;
420045916cd2Sjpk 
4201f4b3ec61Sdh155122 	irb = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4202f4b3ec61Sdh155122 	    ipst->ips_ip_cache_table_size)];
420345916cd2Sjpk 	IRB_REFHOLD(irb);
420445916cd2Sjpk 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
420545916cd2Sjpk 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
420645916cd2Sjpk 			continue;
420745916cd2Sjpk 
420845916cd2Sjpk 		ASSERT(ire->ire_mask == IP_HOST_MASK);
420945916cd2Sjpk 		if (ire_match_args(ire, addr, ire->ire_mask, 0, IRE_CACHE,
421045916cd2Sjpk 		    NULL, zoneid, 0, NULL, MATCH_IRE_TYPE)) {
421145916cd2Sjpk 			ire_delete(ire);
421245916cd2Sjpk 		}
421345916cd2Sjpk 	}
421445916cd2Sjpk 	IRB_REFRELE(irb);
421545916cd2Sjpk 
4216f4b3ec61Sdh155122 	ire_walk_v4(ire_delete_cache_gw, &addr, zoneid, ipst);
421745916cd2Sjpk }
421845916cd2Sjpk 
421945916cd2Sjpk /*
42207c478bd9Sstevel@tonic-gate  * Looks up cache table for a route.
42217c478bd9Sstevel@tonic-gate  * specific lookup can be indicated by
42227c478bd9Sstevel@tonic-gate  * passing the MATCH_* flags and the
42237c478bd9Sstevel@tonic-gate  * necessary parameters.
42247c478bd9Sstevel@tonic-gate  */
42257c478bd9Sstevel@tonic-gate ire_t *
422645916cd2Sjpk ire_ctable_lookup(ipaddr_t addr, ipaddr_t gateway, int type, const ipif_t *ipif,
4227f4b3ec61Sdh155122     zoneid_t zoneid, const ts_label_t *tsl, int flags, ip_stack_t *ipst)
42287c478bd9Sstevel@tonic-gate {
42297c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
42307c478bd9Sstevel@tonic-gate 	ire_t *ire;
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate 	/*
42337c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
42347c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
42357c478bd9Sstevel@tonic-gate 	 */
42367c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
42377c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
42387c478bd9Sstevel@tonic-gate 		return (NULL);
42397c478bd9Sstevel@tonic-gate 
4240f4b3ec61Sdh155122 	irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4241f4b3ec61Sdh155122 	    ipst->ips_ip_cache_table_size)];
42427c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
42437c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
42447c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
42457c478bd9Sstevel@tonic-gate 			continue;
42467c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_mask == IP_HOST_MASK);
42477c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, addr, ire->ire_mask, gateway, type,
424845916cd2Sjpk 		    ipif, zoneid, 0, tsl, flags)) {
42497c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
42507c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
42517c478bd9Sstevel@tonic-gate 			return (ire);
42527c478bd9Sstevel@tonic-gate 		}
42537c478bd9Sstevel@tonic-gate 	}
42547c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
42557c478bd9Sstevel@tonic-gate 	return (NULL);
42567c478bd9Sstevel@tonic-gate }
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate /*
42595597b60aSnordmark  * Check whether the IRE_LOCAL and the IRE potentially used to transmit
42605597b60aSnordmark  * (could be an IRE_CACHE, IRE_BROADCAST, or IRE_INTERFACE) are part of
42615597b60aSnordmark  * the same ill group.
42625597b60aSnordmark  */
42635597b60aSnordmark boolean_t
42645597b60aSnordmark ire_local_same_ill_group(ire_t *ire_local, ire_t *xmit_ire)
42655597b60aSnordmark {
42665597b60aSnordmark 	ill_t		*recv_ill, *xmit_ill;
42675597b60aSnordmark 	ill_group_t	*recv_group, *xmit_group;
42685597b60aSnordmark 
42692bc4236aSnordmark 	ASSERT(ire_local->ire_type & (IRE_LOCAL|IRE_LOOPBACK));
4270aa2eaee6Snordmark 	ASSERT(xmit_ire->ire_type & (IRE_CACHETABLE|IRE_INTERFACE));
42715597b60aSnordmark 
42722bc4236aSnordmark 	recv_ill = ire_to_ill(ire_local);
42732bc4236aSnordmark 	xmit_ill = ire_to_ill(xmit_ire);
42742bc4236aSnordmark 
42752bc4236aSnordmark 	ASSERT(recv_ill != NULL);
42762bc4236aSnordmark 	ASSERT(xmit_ill != NULL);
42775597b60aSnordmark 
42785597b60aSnordmark 	if (recv_ill == xmit_ill)
42795597b60aSnordmark 		return (B_TRUE);
42805597b60aSnordmark 
42815597b60aSnordmark 	recv_group = recv_ill->ill_group;
42825597b60aSnordmark 	xmit_group = xmit_ill->ill_group;
42835597b60aSnordmark 
42845597b60aSnordmark 	if (recv_group != NULL && recv_group == xmit_group)
42855597b60aSnordmark 		return (B_TRUE);
42865597b60aSnordmark 
42875597b60aSnordmark 	return (B_FALSE);
42885597b60aSnordmark }
42895597b60aSnordmark 
42905597b60aSnordmark /*
42915597b60aSnordmark  * Check if the IRE_LOCAL uses the same ill (group) as another route would use.
4292aa2eaee6Snordmark  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
4293aa2eaee6Snordmark  * then we don't allow this IRE_LOCAL to be used.
42945597b60aSnordmark  */
42955597b60aSnordmark boolean_t
42965597b60aSnordmark ire_local_ok_across_zones(ire_t *ire_local, zoneid_t zoneid, void *addr,
4297f4b3ec61Sdh155122     const ts_label_t *tsl, ip_stack_t *ipst)
42985597b60aSnordmark {
42995597b60aSnordmark 	ire_t		*alt_ire;
43005597b60aSnordmark 	boolean_t	rval;
43015597b60aSnordmark 
43025597b60aSnordmark 	if (ire_local->ire_ipversion == IPV4_VERSION) {
43035597b60aSnordmark 		alt_ire = ire_ftable_lookup(*((ipaddr_t *)addr), 0, 0, 0, NULL,
43045597b60aSnordmark 		    NULL, zoneid, 0, tsl,
43055597b60aSnordmark 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4306f4b3ec61Sdh155122 		    MATCH_IRE_RJ_BHOLE, ipst);
43075597b60aSnordmark 	} else {
43085597b60aSnordmark 		alt_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
43095597b60aSnordmark 		    0, NULL, NULL, zoneid, 0, tsl,
43105597b60aSnordmark 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4311f4b3ec61Sdh155122 		    MATCH_IRE_RJ_BHOLE, ipst);
43125597b60aSnordmark 	}
43135597b60aSnordmark 
43145597b60aSnordmark 	if (alt_ire == NULL)
43155597b60aSnordmark 		return (B_FALSE);
43165597b60aSnordmark 
4317aa2eaee6Snordmark 	if (alt_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
4318aa2eaee6Snordmark 		ire_refrele(alt_ire);
4319aa2eaee6Snordmark 		return (B_FALSE);
4320aa2eaee6Snordmark 	}
43215597b60aSnordmark 	rval = ire_local_same_ill_group(ire_local, alt_ire);
43225597b60aSnordmark 
43235597b60aSnordmark 	ire_refrele(alt_ire);
43245597b60aSnordmark 	return (rval);
43255597b60aSnordmark }
43265597b60aSnordmark 
43275597b60aSnordmark /*
43287c478bd9Sstevel@tonic-gate  * Lookup cache. Don't return IRE_MARK_HIDDEN entries. Callers
43297c478bd9Sstevel@tonic-gate  * should use ire_ctable_lookup with MATCH_IRE_MARK_HIDDEN to get
43307c478bd9Sstevel@tonic-gate  * to the hidden ones.
43315597b60aSnordmark  *
43325597b60aSnordmark  * In general the zoneid has to match (where ALL_ZONES match all of them).
43335597b60aSnordmark  * But for IRE_LOCAL we also need to handle the case where L2 should
43345597b60aSnordmark  * conceptually loop back the packet. This is necessary since neither
43355597b60aSnordmark  * Ethernet drivers nor Ethernet hardware loops back packets sent to their
43365597b60aSnordmark  * own MAC address. This loopback is needed when the normal
43375597b60aSnordmark  * routes (ignoring IREs with different zoneids) would send out the packet on
43385597b60aSnordmark  * the same ill (or ill group) as the ill with which this IRE_LOCAL is
43395597b60aSnordmark  * associated.
43405597b60aSnordmark  *
43415597b60aSnordmark  * Earlier versions of this code always matched an IRE_LOCAL independently of
43425597b60aSnordmark  * the zoneid. We preserve that earlier behavior when
43435597b60aSnordmark  * ip_restrict_interzone_loopback is turned off.
43447c478bd9Sstevel@tonic-gate  */
43457c478bd9Sstevel@tonic-gate ire_t *
4346f4b3ec61Sdh155122 ire_cache_lookup(ipaddr_t addr, zoneid_t zoneid, const ts_label_t *tsl,
4347f4b3ec61Sdh155122     ip_stack_t *ipst)
43487c478bd9Sstevel@tonic-gate {
43497c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
43507c478bd9Sstevel@tonic-gate 	ire_t *ire;
43517c478bd9Sstevel@tonic-gate 
4352f4b3ec61Sdh155122 	irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4353f4b3ec61Sdh155122 	    ipst->ips_ip_cache_table_size)];
43547c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
43557c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
435643d18f1cSpriyanka 		if (ire->ire_marks & (IRE_MARK_CONDEMNED |
435743d18f1cSpriyanka 		    IRE_MARK_HIDDEN | IRE_MARK_PRIVATE_ADDR)) {
43587c478bd9Sstevel@tonic-gate 			continue;
435943d18f1cSpriyanka 		}
43607c478bd9Sstevel@tonic-gate 		if (ire->ire_addr == addr) {
436145916cd2Sjpk 			/*
436245916cd2Sjpk 			 * Finally, check if the security policy has any
436345916cd2Sjpk 			 * restriction on using this route for the specified
436445916cd2Sjpk 			 * message.
436545916cd2Sjpk 			 */
436645916cd2Sjpk 			if (tsl != NULL &&
436745916cd2Sjpk 			    ire->ire_gw_secattr != NULL &&
436845916cd2Sjpk 			    tsol_ire_match_gwattr(ire, tsl) != 0) {
436945916cd2Sjpk 				continue;
437045916cd2Sjpk 			}
437145916cd2Sjpk 
43727c478bd9Sstevel@tonic-gate 			if (zoneid == ALL_ZONES || ire->ire_zoneid == zoneid ||
43735597b60aSnordmark 			    ire->ire_zoneid == ALL_ZONES) {
43745597b60aSnordmark 				IRE_REFHOLD(ire);
43755597b60aSnordmark 				rw_exit(&irb_ptr->irb_lock);
43765597b60aSnordmark 				return (ire);
43775597b60aSnordmark 			}
43785597b60aSnordmark 
43795597b60aSnordmark 			if (ire->ire_type == IRE_LOCAL) {
4380f4b3ec61Sdh155122 				if (ipst->ips_ip_restrict_interzone_loopback &&
43815597b60aSnordmark 				    !ire_local_ok_across_zones(ire, zoneid,
4382f4b3ec61Sdh155122 				    &addr, tsl, ipst))
43835597b60aSnordmark 					continue;
43845597b60aSnordmark 
43857c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
43867c478bd9Sstevel@tonic-gate 				rw_exit(&irb_ptr->irb_lock);
43877c478bd9Sstevel@tonic-gate 				return (ire);
43887c478bd9Sstevel@tonic-gate 			}
43897c478bd9Sstevel@tonic-gate 		}
43907c478bd9Sstevel@tonic-gate 	}
43917c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
43927c478bd9Sstevel@tonic-gate 	return (NULL);
43937c478bd9Sstevel@tonic-gate }
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate /*
43967c478bd9Sstevel@tonic-gate  * Locate the interface ire that is tied to the cache ire 'cire' via
43977c478bd9Sstevel@tonic-gate  * cire->ire_ihandle.
43987c478bd9Sstevel@tonic-gate  *
43997c478bd9Sstevel@tonic-gate  * We are trying to create the cache ire for an offlink destn based
44007c478bd9Sstevel@tonic-gate  * on the cache ire of the gateway in 'cire'. 'pire' is the prefix ire
44017c478bd9Sstevel@tonic-gate  * as found by ip_newroute(). We are called from ip_newroute() in
44027c478bd9Sstevel@tonic-gate  * the IRE_CACHE case.
44037c478bd9Sstevel@tonic-gate  */
44047c478bd9Sstevel@tonic-gate ire_t *
44057c478bd9Sstevel@tonic-gate ire_ihandle_lookup_offlink(ire_t *cire, ire_t *pire)
44067c478bd9Sstevel@tonic-gate {
44077c478bd9Sstevel@tonic-gate 	ire_t	*ire;
44087c478bd9Sstevel@tonic-gate 	int	match_flags;
44097c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
44107c478bd9Sstevel@tonic-gate 	ipif_t	*gw_ipif;
4411f4b3ec61Sdh155122 	ip_stack_t	*ipst = cire->ire_ipst;
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 	ASSERT(cire != NULL && pire != NULL);
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	/*
44167c478bd9Sstevel@tonic-gate 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
44177c478bd9Sstevel@tonic-gate 	 * because the ihandle refers to an ipif which can be in only one zone.
44187c478bd9Sstevel@tonic-gate 	 */
44197c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
44207c478bd9Sstevel@tonic-gate 	/*
44217c478bd9Sstevel@tonic-gate 	 * ip_newroute calls ire_ftable_lookup with MATCH_IRE_ILL only
44227c478bd9Sstevel@tonic-gate 	 * for on-link hosts. We should never be here for onlink.
44237c478bd9Sstevel@tonic-gate 	 * Thus, use MATCH_IRE_ILL_GROUP.
44247c478bd9Sstevel@tonic-gate 	 */
44257c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
44267c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
44277c478bd9Sstevel@tonic-gate 	/*
44287c478bd9Sstevel@tonic-gate 	 * We know that the mask of the interface ire equals cire->ire_cmask.
44297c478bd9Sstevel@tonic-gate 	 * (When ip_newroute() created 'cire' for the gateway it set its
44307c478bd9Sstevel@tonic-gate 	 * cmask from the interface ire's mask)
44317c478bd9Sstevel@tonic-gate 	 */
44327c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
44337c478bd9Sstevel@tonic-gate 	    IRE_INTERFACE, pire->ire_ipif, NULL, ALL_ZONES, cire->ire_ihandle,
4434f4b3ec61Sdh155122 	    NULL, match_flags, ipst);
44357c478bd9Sstevel@tonic-gate 	if (ire != NULL)
44367c478bd9Sstevel@tonic-gate 		return (ire);
44377c478bd9Sstevel@tonic-gate 	/*
44387c478bd9Sstevel@tonic-gate 	 * If we didn't find an interface ire above, we can't declare failure.
44397c478bd9Sstevel@tonic-gate 	 * For backwards compatibility, we need to support prefix routes
44407c478bd9Sstevel@tonic-gate 	 * pointing to next hop gateways that are not on-link.
44417c478bd9Sstevel@tonic-gate 	 *
44427c478bd9Sstevel@tonic-gate 	 * Assume we are trying to ping some offlink destn, and we have the
44437c478bd9Sstevel@tonic-gate 	 * routing table below.
44447c478bd9Sstevel@tonic-gate 	 *
44457c478bd9Sstevel@tonic-gate 	 * Eg.	default	- gw1		<--- pire	(line 1)
44467c478bd9Sstevel@tonic-gate 	 *	gw1	- gw2				(line 2)
44477c478bd9Sstevel@tonic-gate 	 *	gw2	- hme0				(line 3)
44487c478bd9Sstevel@tonic-gate 	 *
44497c478bd9Sstevel@tonic-gate 	 * If we already have a cache ire for gw1 in 'cire', the
44507c478bd9Sstevel@tonic-gate 	 * ire_ftable_lookup above would have failed, since there is no
44517c478bd9Sstevel@tonic-gate 	 * interface ire to reach gw1. We will fallthru below.
44527c478bd9Sstevel@tonic-gate 	 *
44537c478bd9Sstevel@tonic-gate 	 * Here we duplicate the steps that ire_ftable_lookup() did in
44547c478bd9Sstevel@tonic-gate 	 * getting 'cire' from 'pire', in the MATCH_IRE_RECURSIVE case.
44557c478bd9Sstevel@tonic-gate 	 * The differences are the following
44567c478bd9Sstevel@tonic-gate 	 * i.   We want the interface ire only, so we call ire_ftable_lookup()
44577c478bd9Sstevel@tonic-gate 	 *	instead of ire_route_lookup()
44587c478bd9Sstevel@tonic-gate 	 * ii.  We look for only prefix routes in the 1st call below.
44597c478bd9Sstevel@tonic-gate 	 * ii.  We want to match on the ihandle in the 2nd call below.
44607c478bd9Sstevel@tonic-gate 	 */
44617c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE;
44627c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
44637c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
44647c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(pire->ire_gateway_addr, 0, 0, IRE_OFFSUBNET,
4465f4b3ec61Sdh155122 	    pire->ire_ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
44667c478bd9Sstevel@tonic-gate 	if (ire == NULL)
44677c478bd9Sstevel@tonic-gate 		return (NULL);
44687c478bd9Sstevel@tonic-gate 	/*
44697c478bd9Sstevel@tonic-gate 	 * At this point 'ire' corresponds to the entry shown in line 2.
44707c478bd9Sstevel@tonic-gate 	 * gw_addr is 'gw2' in the example above.
44717c478bd9Sstevel@tonic-gate 	 */
44727c478bd9Sstevel@tonic-gate 	gw_addr = ire->ire_gateway_addr;
44737c478bd9Sstevel@tonic-gate 	gw_ipif = ire->ire_ipif;
44747c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	match_flags |= MATCH_IRE_IHANDLE;
44777c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
4478f4b3ec61Sdh155122 	    gw_ipif, NULL, ALL_ZONES, cire->ire_ihandle, NULL, match_flags,
4479f4b3ec61Sdh155122 	    ipst);
44807c478bd9Sstevel@tonic-gate 	return (ire);
44817c478bd9Sstevel@tonic-gate }
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate /*
44847c478bd9Sstevel@tonic-gate  * Return the IRE_LOOPBACK, IRE_IF_RESOLVER or IRE_IF_NORESOLVER
44857c478bd9Sstevel@tonic-gate  * ire associated with the specified ipif.
44867c478bd9Sstevel@tonic-gate  *
44877c478bd9Sstevel@tonic-gate  * This might occasionally be called when IPIF_UP is not set since
44887c478bd9Sstevel@tonic-gate  * the IP_MULTICAST_IF as well as creating interface routes
44897c478bd9Sstevel@tonic-gate  * allows specifying a down ipif (ipif_lookup* match ipifs that are down).
44907c478bd9Sstevel@tonic-gate  *
44917c478bd9Sstevel@tonic-gate  * Note that if IPIF_NOLOCAL, IPIF_NOXMIT, or IPIF_DEPRECATED is set on
44927c478bd9Sstevel@tonic-gate  * the ipif, this routine might return NULL.
44937c478bd9Sstevel@tonic-gate  */
44947c478bd9Sstevel@tonic-gate ire_t *
449545916cd2Sjpk ipif_to_ire(const ipif_t *ipif)
44967c478bd9Sstevel@tonic-gate {
44977c478bd9Sstevel@tonic-gate 	ire_t	*ire;
4498f4b3ec61Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	ASSERT(!ipif->ipif_isv6);
45017c478bd9Sstevel@tonic-gate 	if (ipif->ipif_ire_type == IRE_LOOPBACK) {
45027c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(ipif->ipif_lcl_addr, 0, IRE_LOOPBACK,
4503f4b3ec61Sdh155122 		    ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
4504f4b3ec61Sdh155122 		    ipst);
45057c478bd9Sstevel@tonic-gate 	} else if (ipif->ipif_flags & IPIF_POINTOPOINT) {
45067c478bd9Sstevel@tonic-gate 		/* In this case we need to lookup destination address. */
45077c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_pp_dst_addr, IP_HOST_MASK, 0,
450845916cd2Sjpk 		    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
4509f4b3ec61Sdh155122 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF | MATCH_IRE_MASK), ipst);
45107c478bd9Sstevel@tonic-gate 	} else {
45117c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_subnet,
45127c478bd9Sstevel@tonic-gate 		    ipif->ipif_net_mask, 0, IRE_INTERFACE, ipif, NULL,
451345916cd2Sjpk 		    ALL_ZONES, 0, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF |
4514f4b3ec61Sdh155122 		    MATCH_IRE_MASK), ipst);
45157c478bd9Sstevel@tonic-gate 	}
45167c478bd9Sstevel@tonic-gate 	return (ire);
45177c478bd9Sstevel@tonic-gate }
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate /*
45207c478bd9Sstevel@tonic-gate  * ire_walk function.
45217c478bd9Sstevel@tonic-gate  * Count the number of IRE_CACHE entries in different categories.
45227c478bd9Sstevel@tonic-gate  */
45237c478bd9Sstevel@tonic-gate void
45247c478bd9Sstevel@tonic-gate ire_cache_count(ire_t *ire, char *arg)
45257c478bd9Sstevel@tonic-gate {
45267c478bd9Sstevel@tonic-gate 	ire_cache_count_t *icc = (ire_cache_count_t *)arg;
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
45297c478bd9Sstevel@tonic-gate 		return;
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate 	icc->icc_total++;
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
45347c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
45357c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
45367c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
45377c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
45387c478bd9Sstevel@tonic-gate 			return;
45397c478bd9Sstevel@tonic-gate 		}
45407c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
45417c478bd9Sstevel@tonic-gate 	} else {
45427c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
45437c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
45447c478bd9Sstevel@tonic-gate 			return;
45457c478bd9Sstevel@tonic-gate 		}
45467c478bd9Sstevel@tonic-gate 	}
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
45497c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu)
45507c478bd9Sstevel@tonic-gate 		icc->icc_pmtu++;
45517c478bd9Sstevel@tonic-gate 	else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
45527c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count)
45537c478bd9Sstevel@tonic-gate 		icc->icc_offlink++;
45547c478bd9Sstevel@tonic-gate 	else
45557c478bd9Sstevel@tonic-gate 		icc->icc_unused++;
45567c478bd9Sstevel@tonic-gate }
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate /*
45597c478bd9Sstevel@tonic-gate  * ire_walk function called by ip_trash_ire_reclaim().
45607c478bd9Sstevel@tonic-gate  * Free a fraction of the IRE_CACHE cache entries. The fractions are
45617c478bd9Sstevel@tonic-gate  * different for different categories of IRE_CACHE entries.
45627c478bd9Sstevel@tonic-gate  * A fraction of zero means to not free any in that category.
45637c478bd9Sstevel@tonic-gate  * Use the hash bucket id plus lbolt as a random number. Thus if the fraction
45647c478bd9Sstevel@tonic-gate  * is N then every Nth hash bucket chain will be freed.
45657c478bd9Sstevel@tonic-gate  */
45667c478bd9Sstevel@tonic-gate void
45677c478bd9Sstevel@tonic-gate ire_cache_reclaim(ire_t *ire, char *arg)
45687c478bd9Sstevel@tonic-gate {
45697c478bd9Sstevel@tonic-gate 	ire_cache_reclaim_t *icr = (ire_cache_reclaim_t *)arg;
45707c478bd9Sstevel@tonic-gate 	uint_t rand;
4571f4b3ec61Sdh155122 	ip_stack_t	*ipst = icr->icr_ipst;
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
45747c478bd9Sstevel@tonic-gate 		return;
45757c478bd9Sstevel@tonic-gate 
45767c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
45777c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
4578f4b3ec61Sdh155122 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6,
4579f4b3ec61Sdh155122 		    ipst->ips_ip6_cache_table_size);
45807c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
45817c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
45827c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
45837c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
45847c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
45857c478bd9Sstevel@tonic-gate 				ire_delete(ire);
45867c478bd9Sstevel@tonic-gate 				return;
45877c478bd9Sstevel@tonic-gate 			}
45887c478bd9Sstevel@tonic-gate 			goto done;
45897c478bd9Sstevel@tonic-gate 		}
45907c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
45917c478bd9Sstevel@tonic-gate 	} else {
45927c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
4593f4b3ec61Sdh155122 		    IRE_ADDR_HASH(ire->ire_addr, ipst->ips_ip_cache_table_size);
45947c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
45957c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
45967c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
45977c478bd9Sstevel@tonic-gate 				ire_delete(ire);
45987c478bd9Sstevel@tonic-gate 				return;
45997c478bd9Sstevel@tonic-gate 			}
46007c478bd9Sstevel@tonic-gate 			goto done;
46017c478bd9Sstevel@tonic-gate 		}
46027c478bd9Sstevel@tonic-gate 	}
46037c478bd9Sstevel@tonic-gate 	/* Not onlink IRE */
46047c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
46057c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) {
46067c478bd9Sstevel@tonic-gate 		/* Use ptmu fraction */
46077c478bd9Sstevel@tonic-gate 		if (icr->icr_pmtu != 0 &&
46087c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_pmtu)*icr->icr_pmtu == rand) {
46097c478bd9Sstevel@tonic-gate 			ire_delete(ire);
46107c478bd9Sstevel@tonic-gate 			return;
46117c478bd9Sstevel@tonic-gate 		}
46127c478bd9Sstevel@tonic-gate 	} else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
46137c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count) {
46147c478bd9Sstevel@tonic-gate 		/* Use offlink fraction */
46157c478bd9Sstevel@tonic-gate 		if (icr->icr_offlink != 0 &&
46167c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_offlink)*icr->icr_offlink == rand) {
46177c478bd9Sstevel@tonic-gate 			ire_delete(ire);
46187c478bd9Sstevel@tonic-gate 			return;
46197c478bd9Sstevel@tonic-gate 		}
46207c478bd9Sstevel@tonic-gate 	} else {
46217c478bd9Sstevel@tonic-gate 		/* Use unused fraction */
46227c478bd9Sstevel@tonic-gate 		if (icr->icr_unused != 0 &&
46237c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_unused)*icr->icr_unused == rand) {
46247c478bd9Sstevel@tonic-gate 			ire_delete(ire);
46257c478bd9Sstevel@tonic-gate 			return;
46267c478bd9Sstevel@tonic-gate 		}
46277c478bd9Sstevel@tonic-gate 	}
46287c478bd9Sstevel@tonic-gate done:
46297c478bd9Sstevel@tonic-gate 	/*
46307c478bd9Sstevel@tonic-gate 	 * Update tire_mark so that those that haven't been used since this
46317c478bd9Sstevel@tonic-gate 	 * reclaim will be considered unused next time we reclaim.
46327c478bd9Sstevel@tonic-gate 	 */
46337c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
46347c478bd9Sstevel@tonic-gate }
46357c478bd9Sstevel@tonic-gate 
46367c478bd9Sstevel@tonic-gate static void
46377c478bd9Sstevel@tonic-gate power2_roundup(uint32_t *value)
46387c478bd9Sstevel@tonic-gate {
46397c478bd9Sstevel@tonic-gate 	int i;
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate 	for (i = 1; i < 31; i++) {
46427c478bd9Sstevel@tonic-gate 		if (*value <= (1 << i))
46437c478bd9Sstevel@tonic-gate 			break;
46447c478bd9Sstevel@tonic-gate 	}
46457c478bd9Sstevel@tonic-gate 	*value = (1 << i);
46467c478bd9Sstevel@tonic-gate }
46477c478bd9Sstevel@tonic-gate 
4648f4b3ec61Sdh155122 /* Global init for all zones */
46497c478bd9Sstevel@tonic-gate void
4650f4b3ec61Sdh155122 ip_ire_g_init()
46517c478bd9Sstevel@tonic-gate {
46527c478bd9Sstevel@tonic-gate 	/*
46537c478bd9Sstevel@tonic-gate 	 * Create ire caches, ire_reclaim()
46547c478bd9Sstevel@tonic-gate 	 * will give IRE_CACHE back to system when needed.
46557c478bd9Sstevel@tonic-gate 	 * This needs to be done here before anything else, since
46567c478bd9Sstevel@tonic-gate 	 * ire_add() expects the cache to be created.
46577c478bd9Sstevel@tonic-gate 	 */
46587c478bd9Sstevel@tonic-gate 	ire_cache = kmem_cache_create("ire_cache",
46597c478bd9Sstevel@tonic-gate 	    sizeof (ire_t), 0, ip_ire_constructor,
46607c478bd9Sstevel@tonic-gate 	    ip_ire_destructor, ip_trash_ire_reclaim, NULL, NULL, 0);
46617c478bd9Sstevel@tonic-gate 
4662f4b3ec61Sdh155122 	rt_entry_cache = kmem_cache_create("rt_entry",
4663f4b3ec61Sdh155122 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
4664f4b3ec61Sdh155122 
4665f4b3ec61Sdh155122 	/*
4666f4b3ec61Sdh155122 	 * Have radix code setup kmem caches etc.
4667f4b3ec61Sdh155122 	 */
4668f4b3ec61Sdh155122 	rn_init();
4669f4b3ec61Sdh155122 }
4670f4b3ec61Sdh155122 
4671f4b3ec61Sdh155122 void
4672f4b3ec61Sdh155122 ip_ire_init(ip_stack_t *ipst)
4673f4b3ec61Sdh155122 {
4674f4b3ec61Sdh155122 	int i;
4675f4b3ec61Sdh155122 	uint32_t mem_cnt;
4676f4b3ec61Sdh155122 	uint32_t cpu_cnt;
4677f4b3ec61Sdh155122 	uint32_t min_cnt;
4678f4b3ec61Sdh155122 	pgcnt_t mem_avail;
4679f4b3ec61Sdh155122 
4680f4b3ec61Sdh155122 	/*
4681f4b3ec61Sdh155122 	 * ip_ire_max_bucket_cnt is sized below based on the memory
4682f4b3ec61Sdh155122 	 * size and the cpu speed of the machine. This is upper
4683f4b3ec61Sdh155122 	 * bounded by the compile time value of ip_ire_max_bucket_cnt
4684f4b3ec61Sdh155122 	 * and is lower bounded by the compile time value of
4685f4b3ec61Sdh155122 	 * ip_ire_min_bucket_cnt.  Similar logic applies to
4686f4b3ec61Sdh155122 	 * ip6_ire_max_bucket_cnt.
4687f4b3ec61Sdh155122 	 *
4688f4b3ec61Sdh155122 	 * We calculate this for each IP Instances in order to use
4689f4b3ec61Sdh155122 	 * the kmem_avail and ip_ire_{min,max}_bucket_cnt that are
4690f4b3ec61Sdh155122 	 * in effect when the zone is booted.
4691f4b3ec61Sdh155122 	 */
4692f4b3ec61Sdh155122 	mem_avail = kmem_avail();
4693f4b3ec61Sdh155122 	mem_cnt = (mem_avail >> ip_ire_mem_ratio) /
4694f4b3ec61Sdh155122 	    ip_cache_table_size / sizeof (ire_t);
4695f4b3ec61Sdh155122 	cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio;
4696f4b3ec61Sdh155122 
4697f4b3ec61Sdh155122 	min_cnt = MIN(cpu_cnt, mem_cnt);
4698f4b3ec61Sdh155122 	if (min_cnt < ip_ire_min_bucket_cnt)
4699f4b3ec61Sdh155122 		min_cnt = ip_ire_min_bucket_cnt;
4700f4b3ec61Sdh155122 	if (ip_ire_max_bucket_cnt > min_cnt) {
4701f4b3ec61Sdh155122 		ip_ire_max_bucket_cnt = min_cnt;
4702f4b3ec61Sdh155122 	}
4703f4b3ec61Sdh155122 
4704f4b3ec61Sdh155122 	mem_cnt = (mem_avail >> ip_ire_mem_ratio) /
4705f4b3ec61Sdh155122 	    ip6_cache_table_size / sizeof (ire_t);
4706f4b3ec61Sdh155122 	min_cnt = MIN(cpu_cnt, mem_cnt);
4707f4b3ec61Sdh155122 	if (min_cnt < ip6_ire_min_bucket_cnt)
4708f4b3ec61Sdh155122 		min_cnt = ip6_ire_min_bucket_cnt;
4709f4b3ec61Sdh155122 	if (ip6_ire_max_bucket_cnt > min_cnt) {
4710f4b3ec61Sdh155122 		ip6_ire_max_bucket_cnt = min_cnt;
4711f4b3ec61Sdh155122 	}
4712f4b3ec61Sdh155122 
4713f4b3ec61Sdh155122 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
4714f4b3ec61Sdh155122 	mutex_init(&ipst->ips_ire_handle_lock, NULL, MUTEX_DEFAULT, NULL);
4715f4b3ec61Sdh155122 
4716f4b3ec61Sdh155122 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
4717f4b3ec61Sdh155122 
4718f4b3ec61Sdh155122 
4719f4b3ec61Sdh155122 	/* Calculate the IPv4 cache table size. */
4720f4b3ec61Sdh155122 	ipst->ips_ip_cache_table_size = MAX(ip_cache_table_size,
4721f4b3ec61Sdh155122 	    ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) /
4722f4b3ec61Sdh155122 	    ip_ire_max_bucket_cnt));
4723f4b3ec61Sdh155122 	if (ipst->ips_ip_cache_table_size > ip_max_cache_table_size)
4724f4b3ec61Sdh155122 		ipst->ips_ip_cache_table_size = ip_max_cache_table_size;
4725f4b3ec61Sdh155122 	/*
4726f4b3ec61Sdh155122 	 * Make sure that the table size is always a power of 2.  The
4727f4b3ec61Sdh155122 	 * hash macro IRE_ADDR_HASH() depends on that.
4728f4b3ec61Sdh155122 	 */
4729f4b3ec61Sdh155122 	power2_roundup(&ipst->ips_ip_cache_table_size);
4730f4b3ec61Sdh155122 
4731f4b3ec61Sdh155122 	ipst->ips_ip_cache_table = kmem_zalloc(ipst->ips_ip_cache_table_size *
4732f4b3ec61Sdh155122 	    sizeof (irb_t), KM_SLEEP);
4733f4b3ec61Sdh155122 
4734f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
4735f4b3ec61Sdh155122 		rw_init(&ipst->ips_ip_cache_table[i].irb_lock, NULL,
4736f4b3ec61Sdh155122 		    RW_DEFAULT, NULL);
4737f4b3ec61Sdh155122 	}
4738f4b3ec61Sdh155122 
4739f4b3ec61Sdh155122 	/* Calculate the IPv6 cache table size. */
4740f4b3ec61Sdh155122 	ipst->ips_ip6_cache_table_size = MAX(ip6_cache_table_size,
4741f4b3ec61Sdh155122 	    ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) /
4742f4b3ec61Sdh155122 	    ip6_ire_max_bucket_cnt));
4743f4b3ec61Sdh155122 	if (ipst->ips_ip6_cache_table_size > ip6_max_cache_table_size)
4744f4b3ec61Sdh155122 		ipst->ips_ip6_cache_table_size = ip6_max_cache_table_size;
4745f4b3ec61Sdh155122 	/*
4746f4b3ec61Sdh155122 	 * Make sure that the table size is always a power of 2.  The
4747f4b3ec61Sdh155122 	 * hash macro IRE_ADDR_HASH_V6() depends on that.
4748f4b3ec61Sdh155122 	 */
4749f4b3ec61Sdh155122 	power2_roundup(&ipst->ips_ip6_cache_table_size);
4750f4b3ec61Sdh155122 
4751f4b3ec61Sdh155122 	ipst->ips_ip_cache_table_v6 = kmem_zalloc(
4752f4b3ec61Sdh155122 	    ipst->ips_ip6_cache_table_size * sizeof (irb_t), KM_SLEEP);
4753f4b3ec61Sdh155122 
4754f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) {
4755f4b3ec61Sdh155122 		rw_init(&ipst->ips_ip_cache_table_v6[i].irb_lock, NULL,
4756f4b3ec61Sdh155122 		    RW_DEFAULT, NULL);
4757f4b3ec61Sdh155122 	}
4758f4b3ec61Sdh155122 
47597c478bd9Sstevel@tonic-gate 	/*
47607c478bd9Sstevel@tonic-gate 	 * Make sure that the forwarding table size is a power of 2.
47617c478bd9Sstevel@tonic-gate 	 * The IRE*_ADDR_HASH() macroes depend on that.
47627c478bd9Sstevel@tonic-gate 	 */
4763f4b3ec61Sdh155122 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
4764f4b3ec61Sdh155122 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
4765f4b3ec61Sdh155122 
4766f4b3ec61Sdh155122 	ipst->ips_ire_handle = 1;
47677c478bd9Sstevel@tonic-gate }
47687c478bd9Sstevel@tonic-gate 
47697c478bd9Sstevel@tonic-gate void
4770f4b3ec61Sdh155122 ip_ire_g_fini(void)
4771f4b3ec61Sdh155122 {
4772f4b3ec61Sdh155122 	kmem_cache_destroy(ire_cache);
4773f4b3ec61Sdh155122 	kmem_cache_destroy(rt_entry_cache);
4774f4b3ec61Sdh155122 
4775f4b3ec61Sdh155122 	rn_fini();
4776f4b3ec61Sdh155122 }
4777f4b3ec61Sdh155122 
4778f4b3ec61Sdh155122 void
4779f4b3ec61Sdh155122 ip_ire_fini(ip_stack_t *ipst)
47807c478bd9Sstevel@tonic-gate {
47817c478bd9Sstevel@tonic-gate 	int i;
47827c478bd9Sstevel@tonic-gate 
4783f4b3ec61Sdh155122 	/*
4784f4b3ec61Sdh155122 	 * Delete all IREs - assumes that the ill/ipifs have
4785f4b3ec61Sdh155122 	 * been removed so what remains are just the ftable and IRE_CACHE.
4786f4b3ec61Sdh155122 	 */
4787f4b3ec61Sdh155122 	ire_walk(ire_delete, NULL, ipst);
4788c793af95Ssangeeta 
4789f4b3ec61Sdh155122 	rn_freehead(ipst->ips_ip_ftable);
4790f4b3ec61Sdh155122 	ipst->ips_ip_ftable = NULL;
47917c478bd9Sstevel@tonic-gate 
4792f4b3ec61Sdh155122 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
4793f4b3ec61Sdh155122 	mutex_destroy(&ipst->ips_ire_handle_lock);
4794f4b3ec61Sdh155122 
4795f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
4796f4b3ec61Sdh155122 		ASSERT(ipst->ips_ip_cache_table[i].irb_ire == NULL);
4797f4b3ec61Sdh155122 		rw_destroy(&ipst->ips_ip_cache_table[i].irb_lock);
47987c478bd9Sstevel@tonic-gate 	}
4799f4b3ec61Sdh155122 	kmem_free(ipst->ips_ip_cache_table,
4800f4b3ec61Sdh155122 	    ipst->ips_ip_cache_table_size * sizeof (irb_t));
4801f4b3ec61Sdh155122 	ipst->ips_ip_cache_table = NULL;
48027c478bd9Sstevel@tonic-gate 
4803f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) {
4804f4b3ec61Sdh155122 		ASSERT(ipst->ips_ip_cache_table_v6[i].irb_ire == NULL);
4805f4b3ec61Sdh155122 		rw_destroy(&ipst->ips_ip_cache_table_v6[i].irb_lock);
48067c478bd9Sstevel@tonic-gate 	}
4807f4b3ec61Sdh155122 	kmem_free(ipst->ips_ip_cache_table_v6,
4808f4b3ec61Sdh155122 	    ipst->ips_ip6_cache_table_size * sizeof (irb_t));
4809f4b3ec61Sdh155122 	ipst->ips_ip_cache_table_v6 = NULL;
48107c478bd9Sstevel@tonic-gate 
4811f4b3ec61Sdh155122 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
4812f4b3ec61Sdh155122 		irb_t *ptr;
4813f4b3ec61Sdh155122 		int j;
4814f4b3ec61Sdh155122 
4815f4b3ec61Sdh155122 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
4816f4b3ec61Sdh155122 			continue;
4817f4b3ec61Sdh155122 
4818f4b3ec61Sdh155122 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
4819f4b3ec61Sdh155122 			ASSERT(ptr[j].irb_ire == NULL);
4820f4b3ec61Sdh155122 			rw_destroy(&ptr[j].irb_lock);
4821f4b3ec61Sdh155122 		}
4822f4b3ec61Sdh155122 		mi_free(ptr);
4823f4b3ec61Sdh155122 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
4824f4b3ec61Sdh155122 	}
48257c478bd9Sstevel@tonic-gate }
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate /*
48287c478bd9Sstevel@tonic-gate  * Check if another multirt route resolution is needed.
48297c478bd9Sstevel@tonic-gate  * B_TRUE is returned is there remain a resolvable route,
48307c478bd9Sstevel@tonic-gate  * or if no route for that dst is resolved yet.
48317c478bd9Sstevel@tonic-gate  * B_FALSE is returned if all routes for that dst are resolved
48327c478bd9Sstevel@tonic-gate  * or if the remaining unresolved routes are actually not
48337c478bd9Sstevel@tonic-gate  * resolvable.
48347c478bd9Sstevel@tonic-gate  * This only works in the global zone.
48357c478bd9Sstevel@tonic-gate  */
48367c478bd9Sstevel@tonic-gate boolean_t
4837f4b3ec61Sdh155122 ire_multirt_need_resolve(ipaddr_t dst, const ts_label_t *tsl, ip_stack_t *ipst)
48387c478bd9Sstevel@tonic-gate {
48397c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
48407c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
48417c478bd9Sstevel@tonic-gate 	ire_t	*fire;
48427c478bd9Sstevel@tonic-gate 	ire_t	*cire;
48437c478bd9Sstevel@tonic-gate 	irb_t	*firb;
48447c478bd9Sstevel@tonic-gate 	irb_t	*cirb;
48457c478bd9Sstevel@tonic-gate 	int	unres_cnt = 0;
48467c478bd9Sstevel@tonic-gate 	boolean_t resolvable = B_FALSE;
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_HOST that matches the destination */
48497c478bd9Sstevel@tonic-gate 	first_fire = ire_ftable_lookup(dst, IP_HOST_MASK, 0, IRE_HOST, NULL,
485045916cd2Sjpk 	    NULL, ALL_ZONES, 0, tsl,
4851f4b3ec61Sdh155122 	    MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst);
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	/* No route at all */
48547c478bd9Sstevel@tonic-gate 	if (first_fire == NULL) {
48557c478bd9Sstevel@tonic-gate 		return (B_TRUE);
48567c478bd9Sstevel@tonic-gate 	}
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
48597c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_CACHE ire for that destination. */
4862f4b3ec61Sdh155122 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst);
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 	/* No resolved route. */
48657c478bd9Sstevel@tonic-gate 	if (first_cire == NULL) {
48667c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
48677c478bd9Sstevel@tonic-gate 		return (B_TRUE);
48687c478bd9Sstevel@tonic-gate 	}
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate 	/*
48717c478bd9Sstevel@tonic-gate 	 * At least one route is resolved. Here we look through the forward
48727c478bd9Sstevel@tonic-gate 	 * and cache tables, to compare the number of declared routes
48737c478bd9Sstevel@tonic-gate 	 * with the number of resolved routes. The search for a resolvable
48747c478bd9Sstevel@tonic-gate 	 * route is performed only if at least one route remains
48757c478bd9Sstevel@tonic-gate 	 * unresolved.
48767c478bd9Sstevel@tonic-gate 	 */
48777c478bd9Sstevel@tonic-gate 	cirb = first_cire->ire_bucket;
48787c478bd9Sstevel@tonic-gate 	ASSERT(cirb != NULL);
48797c478bd9Sstevel@tonic-gate 
48807c478bd9Sstevel@tonic-gate 	/* Count the number of routes to that dest that are declared. */
48817c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
48827c478bd9Sstevel@tonic-gate 	for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
48837c478bd9Sstevel@tonic-gate 		if (!(fire->ire_flags & RTF_MULTIRT))
48847c478bd9Sstevel@tonic-gate 			continue;
48857c478bd9Sstevel@tonic-gate 		if (fire->ire_addr != dst)
48867c478bd9Sstevel@tonic-gate 			continue;
48877c478bd9Sstevel@tonic-gate 		unres_cnt++;
48887c478bd9Sstevel@tonic-gate 	}
48897c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate 	/* Then subtract the number of routes to that dst that are resolved */
48927c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(cirb);
48937c478bd9Sstevel@tonic-gate 	for (cire = first_cire; cire != NULL; cire = cire->ire_next) {
48947c478bd9Sstevel@tonic-gate 		if (!(cire->ire_flags & RTF_MULTIRT))
48957c478bd9Sstevel@tonic-gate 			continue;
48967c478bd9Sstevel@tonic-gate 		if (cire->ire_addr != dst)
48977c478bd9Sstevel@tonic-gate 			continue;
48987c478bd9Sstevel@tonic-gate 		if (cire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))
48997c478bd9Sstevel@tonic-gate 			continue;
49007c478bd9Sstevel@tonic-gate 		unres_cnt--;
49017c478bd9Sstevel@tonic-gate 	}
49027c478bd9Sstevel@tonic-gate 	IRB_REFRELE(cirb);
49037c478bd9Sstevel@tonic-gate 
49047c478bd9Sstevel@tonic-gate 	/* At least one route is unresolved; search for a resolvable route. */
49057c478bd9Sstevel@tonic-gate 	if (unres_cnt > 0)
49067c478bd9Sstevel@tonic-gate 		resolvable = ire_multirt_lookup(&first_cire, &first_fire,
4907f4b3ec61Sdh155122 		    MULTIRT_USESTAMP | MULTIRT_CACHEGW, tsl, ipst);
49087c478bd9Sstevel@tonic-gate 
49097c478bd9Sstevel@tonic-gate 	if (first_fire != NULL)
49107c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
49117c478bd9Sstevel@tonic-gate 
49127c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
49137c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
49147c478bd9Sstevel@tonic-gate 
49157c478bd9Sstevel@tonic-gate 	return (resolvable);
49167c478bd9Sstevel@tonic-gate }
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 
49197c478bd9Sstevel@tonic-gate /*
49207c478bd9Sstevel@tonic-gate  * Explore a forward_table bucket, starting from fire_arg.
49217c478bd9Sstevel@tonic-gate  * fire_arg MUST be an IRE_HOST entry.
49227c478bd9Sstevel@tonic-gate  *
49237c478bd9Sstevel@tonic-gate  * Return B_TRUE and update *ire_arg and *fire_arg
49247c478bd9Sstevel@tonic-gate  * if at least one resolvable route is found. *ire_arg
49257c478bd9Sstevel@tonic-gate  * is the IRE entry for *fire_arg's gateway.
49267c478bd9Sstevel@tonic-gate  *
49277c478bd9Sstevel@tonic-gate  * Return B_FALSE otherwise (all routes are resolved or
49287c478bd9Sstevel@tonic-gate  * the remaining unresolved routes are all unresolvable).
49297c478bd9Sstevel@tonic-gate  *
49307c478bd9Sstevel@tonic-gate  * The IRE selection relies on a priority mechanism
49317c478bd9Sstevel@tonic-gate  * driven by the flags passed in by the caller.
49327c478bd9Sstevel@tonic-gate  * The caller, such as ip_newroute_ipif(), can get the most
49337c478bd9Sstevel@tonic-gate  * relevant ire at each stage of a multiple route resolution.
49347c478bd9Sstevel@tonic-gate  *
49357c478bd9Sstevel@tonic-gate  * The rules are:
49367c478bd9Sstevel@tonic-gate  *
49377c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is specified in flags, IRE_CACHETABLE
49387c478bd9Sstevel@tonic-gate  *   ires are preferred for the gateway. This gives the highest
49397c478bd9Sstevel@tonic-gate  *   priority to routes that can be resolved without using
49407c478bd9Sstevel@tonic-gate  *   a resolver.
49417c478bd9Sstevel@tonic-gate  *
49427c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is not specified, or if MULTIRT_CACHEGW
49437c478bd9Sstevel@tonic-gate  *   is specified but no IRE_CACHETABLE ire entry for the gateway
49447c478bd9Sstevel@tonic-gate  *   is found, the following rules apply.
49457c478bd9Sstevel@tonic-gate  *
49467c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is specified in flags, IRE_INTERFACE
49477c478bd9Sstevel@tonic-gate  *   ires for the gateway, that have not been tried since
49487c478bd9Sstevel@tonic-gate  *   a configurable amount of time, are preferred.
49497c478bd9Sstevel@tonic-gate  *   This applies when a resolver must be invoked for
49507c478bd9Sstevel@tonic-gate  *   a missing route, but we don't want to use the resolver
49517c478bd9Sstevel@tonic-gate  *   upon each packet emission. If no such resolver is found,
49527c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
49537c478bd9Sstevel@tonic-gate  *   The MULTIRT_USESTAMP flag can be combined with
49547c478bd9Sstevel@tonic-gate  *   MULTIRT_CACHEGW.
49557c478bd9Sstevel@tonic-gate  *
49567c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is not specified in flags, the first
49577c478bd9Sstevel@tonic-gate  *   unresolved but resolvable route is selected.
49587c478bd9Sstevel@tonic-gate  *
49597c478bd9Sstevel@tonic-gate  * - Otherwise, there is no resolvalble route, and
49607c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
49617c478bd9Sstevel@tonic-gate  *
49627c478bd9Sstevel@tonic-gate  * At last, MULTIRT_SETSTAMP can be specified in flags to
49637c478bd9Sstevel@tonic-gate  * request the timestamp of unresolvable routes to
49647c478bd9Sstevel@tonic-gate  * be refreshed. This prevents the useless exploration
49657c478bd9Sstevel@tonic-gate  * of those routes for a while, when MULTIRT_USESTAMP is used.
49667c478bd9Sstevel@tonic-gate  *
49677c478bd9Sstevel@tonic-gate  * This only works in the global zone.
49687c478bd9Sstevel@tonic-gate  */
49697c478bd9Sstevel@tonic-gate boolean_t
497045916cd2Sjpk ire_multirt_lookup(ire_t **ire_arg, ire_t **fire_arg, uint32_t flags,
4971f4b3ec61Sdh155122     const ts_label_t *tsl, ip_stack_t *ipst)
49727c478bd9Sstevel@tonic-gate {
49737c478bd9Sstevel@tonic-gate 	clock_t	delta;
49747c478bd9Sstevel@tonic-gate 	ire_t	*best_fire = NULL;
49757c478bd9Sstevel@tonic-gate 	ire_t	*best_cire = NULL;
49767c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
49777c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
49787c478bd9Sstevel@tonic-gate 	ire_t	*fire;
49797c478bd9Sstevel@tonic-gate 	ire_t	*cire;
49807c478bd9Sstevel@tonic-gate 	irb_t	*firb = NULL;
49817c478bd9Sstevel@tonic-gate 	irb_t	*cirb = NULL;
49827c478bd9Sstevel@tonic-gate 	ire_t	*gw_ire;
49837c478bd9Sstevel@tonic-gate 	boolean_t	already_resolved;
49847c478bd9Sstevel@tonic-gate 	boolean_t	res;
49857c478bd9Sstevel@tonic-gate 	ipaddr_t	dst;
49867c478bd9Sstevel@tonic-gate 	ipaddr_t	gw;
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: *ire_arg %p, *fire_arg %p, flags %04x\n",
49897c478bd9Sstevel@tonic-gate 	    (void *)*ire_arg, (void *)*fire_arg, flags));
49907c478bd9Sstevel@tonic-gate 
49917c478bd9Sstevel@tonic-gate 	ASSERT(ire_arg != NULL);
49927c478bd9Sstevel@tonic-gate 	ASSERT(fire_arg != NULL);
49937c478bd9Sstevel@tonic-gate 
49947c478bd9Sstevel@tonic-gate 	/* Not an IRE_HOST ire; give up. */
49957c478bd9Sstevel@tonic-gate 	if ((*fire_arg == NULL) || ((*fire_arg)->ire_type != IRE_HOST)) {
49967c478bd9Sstevel@tonic-gate 		return (B_FALSE);
49977c478bd9Sstevel@tonic-gate 	}
49987c478bd9Sstevel@tonic-gate 
49997c478bd9Sstevel@tonic-gate 	/* This is the first IRE_HOST ire for that destination. */
50007c478bd9Sstevel@tonic-gate 	first_fire = *fire_arg;
50017c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
50027c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
50037c478bd9Sstevel@tonic-gate 
50047c478bd9Sstevel@tonic-gate 	dst = first_fire->ire_addr;
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: dst %08x\n", ntohl(dst)));
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate 	/*
50097c478bd9Sstevel@tonic-gate 	 * Retrieve the first IRE_CACHE ire for that destination;
50107c478bd9Sstevel@tonic-gate 	 * if we don't find one, no route for that dest is
50117c478bd9Sstevel@tonic-gate 	 * resolved yet.
50127c478bd9Sstevel@tonic-gate 	 */
5013f4b3ec61Sdh155122 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst);
50147c478bd9Sstevel@tonic-gate 	if (first_cire != NULL) {
50157c478bd9Sstevel@tonic-gate 		cirb = first_cire->ire_bucket;
50167c478bd9Sstevel@tonic-gate 	}
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: first_cire %p\n", (void *)first_cire));
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 	/*
50217c478bd9Sstevel@tonic-gate 	 * Search for a resolvable route, giving the top priority
50227c478bd9Sstevel@tonic-gate 	 * to routes that can be resolved without any call to the resolver.
50237c478bd9Sstevel@tonic-gate 	 */
50247c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate 	if (!CLASSD(dst)) {
50277c478bd9Sstevel@tonic-gate 		/*
50287c478bd9Sstevel@tonic-gate 		 * For all multiroute IRE_HOST ires for that destination,
50297c478bd9Sstevel@tonic-gate 		 * check if the route via the IRE_HOST's gateway is
50307c478bd9Sstevel@tonic-gate 		 * resolved yet.
50317c478bd9Sstevel@tonic-gate 		 */
50327c478bd9Sstevel@tonic-gate 		for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
50337c478bd9Sstevel@tonic-gate 
50347c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
50357c478bd9Sstevel@tonic-gate 				continue;
50367c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
50377c478bd9Sstevel@tonic-gate 				continue;
50387c478bd9Sstevel@tonic-gate 
503945916cd2Sjpk 			if (fire->ire_gw_secattr != NULL &&
504045916cd2Sjpk 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
504145916cd2Sjpk 				continue;
504245916cd2Sjpk 			}
504345916cd2Sjpk 
50447c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, "
50477c478bd9Sstevel@tonic-gate 			    "ire_addr %08x, ire_gateway_addr %08x\n",
50487c478bd9Sstevel@tonic-gate 			    (void *)fire, ntohl(fire->ire_addr), ntohl(gw)));
50497c478bd9Sstevel@tonic-gate 
50507c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
50537c478bd9Sstevel@tonic-gate 				ASSERT(cirb != NULL);
50547c478bd9Sstevel@tonic-gate 
50557c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
50567c478bd9Sstevel@tonic-gate 				/*
50577c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
50587c478bd9Sstevel@tonic-gate 				 * destination.
50597c478bd9Sstevel@tonic-gate 				 */
50607c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
50617c478bd9Sstevel@tonic-gate 				    cire != NULL;
50627c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
50657c478bd9Sstevel@tonic-gate 						continue;
50667c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
50677c478bd9Sstevel@tonic-gate 						continue;
50687c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
50697c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
50707c478bd9Sstevel@tonic-gate 					    IRE_MARK_HIDDEN))
50717c478bd9Sstevel@tonic-gate 						continue;
507245916cd2Sjpk 
507345916cd2Sjpk 					if (cire->ire_gw_secattr != NULL &&
507445916cd2Sjpk 					    tsol_ire_match_gwattr(cire,
507545916cd2Sjpk 					    tsl) != 0) {
507645916cd2Sjpk 						continue;
507745916cd2Sjpk 					}
507845916cd2Sjpk 
50797c478bd9Sstevel@tonic-gate 					/*
50807c478bd9Sstevel@tonic-gate 					 * Check if the IRE_CACHE's gateway
50817c478bd9Sstevel@tonic-gate 					 * matches the IRE_HOST's gateway.
50827c478bd9Sstevel@tonic-gate 					 */
50837c478bd9Sstevel@tonic-gate 					if (cire->ire_gateway_addr == gw) {
50847c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
50857c478bd9Sstevel@tonic-gate 						break;
50867c478bd9Sstevel@tonic-gate 					}
50877c478bd9Sstevel@tonic-gate 				}
50887c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
50897c478bd9Sstevel@tonic-gate 			}
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 			/*
50927c478bd9Sstevel@tonic-gate 			 * This route is already resolved;
50937c478bd9Sstevel@tonic-gate 			 * proceed with next one.
50947c478bd9Sstevel@tonic-gate 			 */
50957c478bd9Sstevel@tonic-gate 			if (already_resolved) {
50967c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found cire %p, "
50977c478bd9Sstevel@tonic-gate 				    "already resolved\n", (void *)cire));
50987c478bd9Sstevel@tonic-gate 				continue;
50997c478bd9Sstevel@tonic-gate 			}
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate 			/*
51027c478bd9Sstevel@tonic-gate 			 * The route is unresolved; is it actually
51037c478bd9Sstevel@tonic-gate 			 * resolvable, i.e. is there a cache or a resolver
51047c478bd9Sstevel@tonic-gate 			 * for the gateway?
51057c478bd9Sstevel@tonic-gate 			 */
51067c478bd9Sstevel@tonic-gate 			gw_ire = ire_route_lookup(gw, 0, 0, 0, NULL, NULL,
510745916cd2Sjpk 			    ALL_ZONES, tsl,
5108f4b3ec61Sdh155122 			    MATCH_IRE_RECURSIVE | MATCH_IRE_SECATTR, ipst);
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: looked up gw_ire %p\n",
51117c478bd9Sstevel@tonic-gate 			    (void *)gw_ire));
51127c478bd9Sstevel@tonic-gate 
51137c478bd9Sstevel@tonic-gate 			/*
51147c478bd9Sstevel@tonic-gate 			 * If gw_ire is typed IRE_CACHETABLE,
51157c478bd9Sstevel@tonic-gate 			 * this route can be resolved without any call to the
51167c478bd9Sstevel@tonic-gate 			 * resolver. If the MULTIRT_CACHEGW flag is set,
51177c478bd9Sstevel@tonic-gate 			 * give the top priority to this ire and exit the
51187c478bd9Sstevel@tonic-gate 			 * loop.
51197c478bd9Sstevel@tonic-gate 			 * This is typically the case when an ARP reply
51207c478bd9Sstevel@tonic-gate 			 * is processed through ip_wput_nondata().
51217c478bd9Sstevel@tonic-gate 			 */
51227c478bd9Sstevel@tonic-gate 			if ((flags & MULTIRT_CACHEGW) &&
51237c478bd9Sstevel@tonic-gate 			    (gw_ire != NULL) &&
51247c478bd9Sstevel@tonic-gate 			    (gw_ire->ire_type & IRE_CACHETABLE)) {
5125c793af95Ssangeeta 				ASSERT(gw_ire->ire_nce == NULL ||
5126c793af95Ssangeeta 				    gw_ire->ire_nce->nce_state == ND_REACHABLE);
51277c478bd9Sstevel@tonic-gate 				/*
51287c478bd9Sstevel@tonic-gate 				 * Release the resolver associated to the
51297c478bd9Sstevel@tonic-gate 				 * previous candidate best ire, if any.
51307c478bd9Sstevel@tonic-gate 				 */
51317c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
51327c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
51337c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
51347c478bd9Sstevel@tonic-gate 				}
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 				best_fire = fire;
51377c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found top prio "
51407c478bd9Sstevel@tonic-gate 				    "best_fire %p, best_cire %p\n",
51417c478bd9Sstevel@tonic-gate 				    (void *)best_fire, (void *)best_cire));
51427c478bd9Sstevel@tonic-gate 				break;
51437c478bd9Sstevel@tonic-gate 			}
51447c478bd9Sstevel@tonic-gate 
51457c478bd9Sstevel@tonic-gate 			/*
51467c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
51477c478bd9Sstevel@tonic-gate 			 * attempt to  resolve that route.
51487c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take that
51497c478bd9Sstevel@tonic-gate 			 * route into account only if this time interval
51507c478bd9Sstevel@tonic-gate 			 * exceeds ip_multirt_resolution_interval;
51517c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
51527c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
51537c478bd9Sstevel@tonic-gate 			 */
51547c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
51557c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
51567c478bd9Sstevel@tonic-gate 
5157f4b3ec61Sdh155122 			res = (boolean_t)((delta >
5158f4b3ec61Sdh155122 			    ipst->ips_ip_multirt_resolution_interval) ||
51597c478bd9Sstevel@tonic-gate 			    (!(flags & MULTIRT_USESTAMP)));
51607c478bd9Sstevel@tonic-gate 
51617c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, delta %lu, "
51627c478bd9Sstevel@tonic-gate 			    "res %d\n",
51637c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, res));
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 			if (res) {
51667c478bd9Sstevel@tonic-gate 				/*
51677c478bd9Sstevel@tonic-gate 				 * We are here if MULTIRT_USESTAMP flag is set
51687c478bd9Sstevel@tonic-gate 				 * and the resolver for fire's gateway
51697c478bd9Sstevel@tonic-gate 				 * has not been tried since
51707c478bd9Sstevel@tonic-gate 				 * ip_multirt_resolution_interval, or if
51717c478bd9Sstevel@tonic-gate 				 * MULTIRT_USESTAMP is not set but gw_ire did
51727c478bd9Sstevel@tonic-gate 				 * not fill the conditions for MULTIRT_CACHEGW,
51737c478bd9Sstevel@tonic-gate 				 * or if neither MULTIRT_USESTAMP nor
51747c478bd9Sstevel@tonic-gate 				 * MULTIRT_CACHEGW are set.
51757c478bd9Sstevel@tonic-gate 				 */
51767c478bd9Sstevel@tonic-gate 				if (gw_ire != NULL) {
51777c478bd9Sstevel@tonic-gate 					if (best_fire == NULL) {
51787c478bd9Sstevel@tonic-gate 						ASSERT(best_cire == NULL);
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 						best_fire = fire;
51817c478bd9Sstevel@tonic-gate 						best_cire = gw_ire;
51827c478bd9Sstevel@tonic-gate 
51837c478bd9Sstevel@tonic-gate 						ip2dbg(("ire_multirt_lookup:"
51847c478bd9Sstevel@tonic-gate 						    "found candidate "
51857c478bd9Sstevel@tonic-gate 						    "best_fire %p, "
51867c478bd9Sstevel@tonic-gate 						    "best_cire %p\n",
51877c478bd9Sstevel@tonic-gate 						    (void *)best_fire,
51887c478bd9Sstevel@tonic-gate 						    (void *)best_cire));
51897c478bd9Sstevel@tonic-gate 
51907c478bd9Sstevel@tonic-gate 						/*
51917c478bd9Sstevel@tonic-gate 						 * If MULTIRT_CACHEGW is not
51927c478bd9Sstevel@tonic-gate 						 * set, we ignore the top
51937c478bd9Sstevel@tonic-gate 						 * priority ires that can
51947c478bd9Sstevel@tonic-gate 						 * be resolved without any
51957c478bd9Sstevel@tonic-gate 						 * call to the resolver;
51967c478bd9Sstevel@tonic-gate 						 * In that case, there is
51977c478bd9Sstevel@tonic-gate 						 * actually no need
51987c478bd9Sstevel@tonic-gate 						 * to continue the loop.
51997c478bd9Sstevel@tonic-gate 						 */
52007c478bd9Sstevel@tonic-gate 						if (!(flags &
52017c478bd9Sstevel@tonic-gate 						    MULTIRT_CACHEGW)) {
52027c478bd9Sstevel@tonic-gate 							break;
52037c478bd9Sstevel@tonic-gate 						}
52047c478bd9Sstevel@tonic-gate 						continue;
52057c478bd9Sstevel@tonic-gate 					}
52067c478bd9Sstevel@tonic-gate 				} else {
52077c478bd9Sstevel@tonic-gate 					/*
52087c478bd9Sstevel@tonic-gate 					 * No resolver for the gateway: the
52097c478bd9Sstevel@tonic-gate 					 * route is not resolvable.
52107c478bd9Sstevel@tonic-gate 					 * If the MULTIRT_SETSTAMP flag is
52117c478bd9Sstevel@tonic-gate 					 * set, we stamp the IRE_HOST ire,
52127c478bd9Sstevel@tonic-gate 					 * so we will not select it again
52137c478bd9Sstevel@tonic-gate 					 * during this resolution interval.
52147c478bd9Sstevel@tonic-gate 					 */
52157c478bd9Sstevel@tonic-gate 					if (flags & MULTIRT_SETSTAMP)
52167c478bd9Sstevel@tonic-gate 						fire->ire_last_used_time =
52177c478bd9Sstevel@tonic-gate 						    lbolt;
52187c478bd9Sstevel@tonic-gate 				}
52197c478bd9Sstevel@tonic-gate 			}
52207c478bd9Sstevel@tonic-gate 
52217c478bd9Sstevel@tonic-gate 			if (gw_ire != NULL)
52227c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
52237c478bd9Sstevel@tonic-gate 		}
52247c478bd9Sstevel@tonic-gate 	} else { /* CLASSD(dst) */
52257c478bd9Sstevel@tonic-gate 
52267c478bd9Sstevel@tonic-gate 		for (fire = first_fire;
52277c478bd9Sstevel@tonic-gate 		    fire != NULL;
52287c478bd9Sstevel@tonic-gate 		    fire = fire->ire_next) {
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
52317c478bd9Sstevel@tonic-gate 				continue;
52327c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
52337c478bd9Sstevel@tonic-gate 				continue;
52347c478bd9Sstevel@tonic-gate 
523545916cd2Sjpk 			if (fire->ire_gw_secattr != NULL &&
523645916cd2Sjpk 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
523745916cd2Sjpk 				continue;
523845916cd2Sjpk 			}
523945916cd2Sjpk 
52407c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
52417c478bd9Sstevel@tonic-gate 
52427c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
52437c478bd9Sstevel@tonic-gate 
52447c478bd9Sstevel@tonic-gate 			gw_ire = ire_ftable_lookup(gw, 0, 0, IRE_INTERFACE,
524545916cd2Sjpk 			    NULL, NULL, ALL_ZONES, 0, tsl,
524645916cd2Sjpk 			    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE |
5247f4b3ec61Sdh155122 			    MATCH_IRE_SECATTR, ipst);
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 			/* No resolver for the gateway; we skip this ire. */
52507c478bd9Sstevel@tonic-gate 			if (gw_ire == NULL) {
52517c478bd9Sstevel@tonic-gate 				continue;
52527c478bd9Sstevel@tonic-gate 			}
5253c793af95Ssangeeta 			ASSERT(gw_ire->ire_nce == NULL ||
5254c793af95Ssangeeta 			    gw_ire->ire_nce->nce_state == ND_REACHABLE);
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
52577c478bd9Sstevel@tonic-gate 
52587c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
52597c478bd9Sstevel@tonic-gate 				/*
52607c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
52617c478bd9Sstevel@tonic-gate 				 * destination.
52627c478bd9Sstevel@tonic-gate 				 */
52637c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
52647c478bd9Sstevel@tonic-gate 				    cire != NULL;
52657c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
52667c478bd9Sstevel@tonic-gate 
52677c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
52687c478bd9Sstevel@tonic-gate 						continue;
52697c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
52707c478bd9Sstevel@tonic-gate 						continue;
52717c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
52727c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
52737c478bd9Sstevel@tonic-gate 					    IRE_MARK_HIDDEN))
52747c478bd9Sstevel@tonic-gate 						continue;
52757c478bd9Sstevel@tonic-gate 
527645916cd2Sjpk 					if (cire->ire_gw_secattr != NULL &&
527745916cd2Sjpk 					    tsol_ire_match_gwattr(cire,
527845916cd2Sjpk 					    tsl) != 0) {
527945916cd2Sjpk 						continue;
528045916cd2Sjpk 					}
528145916cd2Sjpk 
52827c478bd9Sstevel@tonic-gate 					/*
52837c478bd9Sstevel@tonic-gate 					 * Cache entries are linked to the
52847c478bd9Sstevel@tonic-gate 					 * parent routes using the parent handle
52857c478bd9Sstevel@tonic-gate 					 * (ire_phandle). If no cache entry has
52867c478bd9Sstevel@tonic-gate 					 * the same handle as fire, fire is
52877c478bd9Sstevel@tonic-gate 					 * still unresolved.
52887c478bd9Sstevel@tonic-gate 					 */
52897c478bd9Sstevel@tonic-gate 					ASSERT(cire->ire_phandle != 0);
52907c478bd9Sstevel@tonic-gate 					if (cire->ire_phandle ==
52917c478bd9Sstevel@tonic-gate 					    fire->ire_phandle) {
52927c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
52937c478bd9Sstevel@tonic-gate 						break;
52947c478bd9Sstevel@tonic-gate 					}
52957c478bd9Sstevel@tonic-gate 				}
52967c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
52977c478bd9Sstevel@tonic-gate 			}
52987c478bd9Sstevel@tonic-gate 
52997c478bd9Sstevel@tonic-gate 			/*
53007c478bd9Sstevel@tonic-gate 			 * This route is already resolved; proceed with
53017c478bd9Sstevel@tonic-gate 			 * next one.
53027c478bd9Sstevel@tonic-gate 			 */
53037c478bd9Sstevel@tonic-gate 			if (already_resolved) {
53047c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
53057c478bd9Sstevel@tonic-gate 				continue;
53067c478bd9Sstevel@tonic-gate 			}
53077c478bd9Sstevel@tonic-gate 
53087c478bd9Sstevel@tonic-gate 			/*
53097c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
53107c478bd9Sstevel@tonic-gate 			 * attempt to resolve that route.
53117c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take
53127c478bd9Sstevel@tonic-gate 			 * that route into account only if this time
53137c478bd9Sstevel@tonic-gate 			 * interval exceeds ip_multirt_resolution_interval;
53147c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
53157c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
53167c478bd9Sstevel@tonic-gate 			 */
53177c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
53187c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
53197c478bd9Sstevel@tonic-gate 
5320f4b3ec61Sdh155122 			res = (boolean_t)((delta >
5321f4b3ec61Sdh155122 			    ipst->ips_ip_multirt_resolution_interval) ||
53227c478bd9Sstevel@tonic-gate 			    (!(flags & MULTIRT_USESTAMP)));
53237c478bd9Sstevel@tonic-gate 
53247c478bd9Sstevel@tonic-gate 			ip3dbg(("ire_multirt_lookup: fire %p, delta %lx, "
53257c478bd9Sstevel@tonic-gate 			    "flags %04x, res %d\n",
53267c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, flags, res));
53277c478bd9Sstevel@tonic-gate 
53287c478bd9Sstevel@tonic-gate 			if (res) {
53297c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
53307c478bd9Sstevel@tonic-gate 					/*
53317c478bd9Sstevel@tonic-gate 					 * Release the resolver associated
53327c478bd9Sstevel@tonic-gate 					 * to the preceding candidate best
53337c478bd9Sstevel@tonic-gate 					 * ire, if any.
53347c478bd9Sstevel@tonic-gate 					 */
53357c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
53367c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
53377c478bd9Sstevel@tonic-gate 				}
53387c478bd9Sstevel@tonic-gate 				best_fire = fire;
53397c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
53407c478bd9Sstevel@tonic-gate 				continue;
53417c478bd9Sstevel@tonic-gate 			}
53427c478bd9Sstevel@tonic-gate 
53437c478bd9Sstevel@tonic-gate 			ire_refrele(gw_ire);
53447c478bd9Sstevel@tonic-gate 		}
53457c478bd9Sstevel@tonic-gate 	}
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
53487c478bd9Sstevel@tonic-gate 		IRE_REFHOLD(best_fire);
53497c478bd9Sstevel@tonic-gate 	}
53507c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
53517c478bd9Sstevel@tonic-gate 
53527c478bd9Sstevel@tonic-gate 	/* Release the first IRE_CACHE we initially looked up, if any. */
53537c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
53547c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 	/* Found a resolvable route. */
53577c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
53587c478bd9Sstevel@tonic-gate 		ASSERT(best_cire != NULL);
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 		if (*fire_arg != NULL)
53617c478bd9Sstevel@tonic-gate 			ire_refrele(*fire_arg);
53627c478bd9Sstevel@tonic-gate 		if (*ire_arg != NULL)
53637c478bd9Sstevel@tonic-gate 			ire_refrele(*ire_arg);
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate 		/*
53667c478bd9Sstevel@tonic-gate 		 * Update the passed-in arguments with the
53677c478bd9Sstevel@tonic-gate 		 * resolvable multirt route we found.
53687c478bd9Sstevel@tonic-gate 		 */
53697c478bd9Sstevel@tonic-gate 		*fire_arg = best_fire;
53707c478bd9Sstevel@tonic-gate 		*ire_arg = best_cire;
53717c478bd9Sstevel@tonic-gate 
53727c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_multirt_lookup: returning B_TRUE, "
53737c478bd9Sstevel@tonic-gate 		    "*fire_arg %p, *ire_arg %p\n",
53747c478bd9Sstevel@tonic-gate 		    (void *)best_fire, (void *)best_cire));
53757c478bd9Sstevel@tonic-gate 
53767c478bd9Sstevel@tonic-gate 		return (B_TRUE);
53777c478bd9Sstevel@tonic-gate 	}
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	ASSERT(best_cire == NULL);
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: returning B_FALSE, *fire_arg %p, "
53827c478bd9Sstevel@tonic-gate 	    "*ire_arg %p\n",
53837c478bd9Sstevel@tonic-gate 	    (void *)*fire_arg, (void *)*ire_arg));
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 	/* No resolvable route. */
53867c478bd9Sstevel@tonic-gate 	return (B_FALSE);
53877c478bd9Sstevel@tonic-gate }
53887c478bd9Sstevel@tonic-gate 
53897c478bd9Sstevel@tonic-gate /*
53907c478bd9Sstevel@tonic-gate  * IRE iterator for inbound and loopback broadcast processing.
53917c478bd9Sstevel@tonic-gate  * Given an IRE_BROADCAST ire, walk the ires with the same destination
53927c478bd9Sstevel@tonic-gate  * address, but skip over the passed-in ire. Returns the next ire without
53937c478bd9Sstevel@tonic-gate  * a hold - assumes that the caller holds a reference on the IRE bucket.
53947c478bd9Sstevel@tonic-gate  */
53957c478bd9Sstevel@tonic-gate ire_t *
53967c478bd9Sstevel@tonic-gate ire_get_next_bcast_ire(ire_t *curr, ire_t *ire)
53977c478bd9Sstevel@tonic-gate {
53987c478bd9Sstevel@tonic-gate 	ill_t *ill;
53997c478bd9Sstevel@tonic-gate 
54007c478bd9Sstevel@tonic-gate 	if (curr == NULL) {
54017c478bd9Sstevel@tonic-gate 		for (curr = ire->ire_bucket->irb_ire; curr != NULL;
54027c478bd9Sstevel@tonic-gate 		    curr = curr->ire_next) {
54037c478bd9Sstevel@tonic-gate 			if (curr->ire_addr == ire->ire_addr)
54047c478bd9Sstevel@tonic-gate 				break;
54057c478bd9Sstevel@tonic-gate 		}
54067c478bd9Sstevel@tonic-gate 	} else {
54077c478bd9Sstevel@tonic-gate 		curr = curr->ire_next;
54087c478bd9Sstevel@tonic-gate 	}
54097c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(ire);
54107c478bd9Sstevel@tonic-gate 	for (; curr != NULL; curr = curr->ire_next) {
54117c478bd9Sstevel@tonic-gate 		if (curr->ire_addr != ire->ire_addr) {
54127c478bd9Sstevel@tonic-gate 			/*
54137c478bd9Sstevel@tonic-gate 			 * All the IREs to a given destination are contiguous;
54147c478bd9Sstevel@tonic-gate 			 * break out once the address doesn't match.
54157c478bd9Sstevel@tonic-gate 			 */
54167c478bd9Sstevel@tonic-gate 			break;
54177c478bd9Sstevel@tonic-gate 		}
54187c478bd9Sstevel@tonic-gate 		if (curr == ire) {
54197c478bd9Sstevel@tonic-gate 			/* skip over the passed-in ire */
54207c478bd9Sstevel@tonic-gate 			continue;
54217c478bd9Sstevel@tonic-gate 		}
54227c478bd9Sstevel@tonic-gate 		if ((curr->ire_stq != NULL && ire->ire_stq == NULL) ||
54237c478bd9Sstevel@tonic-gate 		    (curr->ire_stq == NULL && ire->ire_stq != NULL)) {
54247c478bd9Sstevel@tonic-gate 			/*
54257c478bd9Sstevel@tonic-gate 			 * If the passed-in ire is loopback, skip over
54267c478bd9Sstevel@tonic-gate 			 * non-loopback ires and vice versa.
54277c478bd9Sstevel@tonic-gate 			 */
54287c478bd9Sstevel@tonic-gate 			continue;
54297c478bd9Sstevel@tonic-gate 		}
54307c478bd9Sstevel@tonic-gate 		if (ire_to_ill(curr) != ill) {
54317c478bd9Sstevel@tonic-gate 			/* skip over IREs going through a different interface */
54327c478bd9Sstevel@tonic-gate 			continue;
54337c478bd9Sstevel@tonic-gate 		}
54347c478bd9Sstevel@tonic-gate 		if (curr->ire_marks & IRE_MARK_CONDEMNED) {
54357c478bd9Sstevel@tonic-gate 			/* skip over deleted IREs */
54367c478bd9Sstevel@tonic-gate 			continue;
54377c478bd9Sstevel@tonic-gate 		}
54387c478bd9Sstevel@tonic-gate 		return (curr);
54397c478bd9Sstevel@tonic-gate 	}
54407c478bd9Sstevel@tonic-gate 	return (NULL);
54417c478bd9Sstevel@tonic-gate }
54427c478bd9Sstevel@tonic-gate 
54437c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
54447c478bd9Sstevel@tonic-gate th_trace_t *
54457c478bd9Sstevel@tonic-gate th_trace_ire_lookup(ire_t *ire)
54467c478bd9Sstevel@tonic-gate {
54477c478bd9Sstevel@tonic-gate 	int bucket_id;
54487c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
54497c478bd9Sstevel@tonic-gate 
54507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ire->ire_lock));
54517c478bd9Sstevel@tonic-gate 
54527c478bd9Sstevel@tonic-gate 	bucket_id = IP_TR_HASH(curthread);
54537c478bd9Sstevel@tonic-gate 	ASSERT(bucket_id < IP_TR_HASH_MAX);
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	for (th_trace = ire->ire_trace[bucket_id]; th_trace != NULL;
54567c478bd9Sstevel@tonic-gate 	    th_trace = th_trace->th_next) {
54577c478bd9Sstevel@tonic-gate 		if (th_trace->th_id == curthread)
54587c478bd9Sstevel@tonic-gate 			return (th_trace);
54597c478bd9Sstevel@tonic-gate 	}
54607c478bd9Sstevel@tonic-gate 	return (NULL);
54617c478bd9Sstevel@tonic-gate }
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate void
54647c478bd9Sstevel@tonic-gate ire_trace_ref(ire_t *ire)
54657c478bd9Sstevel@tonic-gate {
54667c478bd9Sstevel@tonic-gate 	int bucket_id;
54677c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	/*
54707c478bd9Sstevel@tonic-gate 	 * Attempt to locate the trace buffer for the curthread.
54717c478bd9Sstevel@tonic-gate 	 * If it does not exist, then allocate a new trace buffer
54727c478bd9Sstevel@tonic-gate 	 * and link it in list of trace bufs for this ipif, at the head
54737c478bd9Sstevel@tonic-gate 	 */
54747c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
54757c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
54767c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
54777c478bd9Sstevel@tonic-gate 		return;
54787c478bd9Sstevel@tonic-gate 	}
54797c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
54807c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
54817c478bd9Sstevel@tonic-gate 		bucket_id = IP_TR_HASH(curthread);
54827c478bd9Sstevel@tonic-gate 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
54837c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
54847c478bd9Sstevel@tonic-gate 		if (th_trace == NULL) {
54857c478bd9Sstevel@tonic-gate 			ire->ire_trace_disable = B_TRUE;
54867c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
54877c478bd9Sstevel@tonic-gate 			ire_trace_inactive(ire);
54887c478bd9Sstevel@tonic-gate 			return;
54897c478bd9Sstevel@tonic-gate 		}
54907c478bd9Sstevel@tonic-gate 
54917c478bd9Sstevel@tonic-gate 		th_trace->th_id = curthread;
54927c478bd9Sstevel@tonic-gate 		th_trace->th_next = ire->ire_trace[bucket_id];
54937c478bd9Sstevel@tonic-gate 		th_trace->th_prev = &ire->ire_trace[bucket_id];
54947c478bd9Sstevel@tonic-gate 		if (th_trace->th_next != NULL)
54957c478bd9Sstevel@tonic-gate 			th_trace->th_next->th_prev = &th_trace->th_next;
54967c478bd9Sstevel@tonic-gate 		ire->ire_trace[bucket_id] = th_trace;
54977c478bd9Sstevel@tonic-gate 	}
54987c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt < TR_BUF_MAX - 1);
54997c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt++;
55007c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
55017c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
55027c478bd9Sstevel@tonic-gate }
55037c478bd9Sstevel@tonic-gate 
55047c478bd9Sstevel@tonic-gate void
55057c478bd9Sstevel@tonic-gate ire_trace_free(th_trace_t *th_trace)
55067c478bd9Sstevel@tonic-gate {
55077c478bd9Sstevel@tonic-gate 	/* unlink th_trace and free it */
55087c478bd9Sstevel@tonic-gate 	*th_trace->th_prev = th_trace->th_next;
55097c478bd9Sstevel@tonic-gate 	if (th_trace->th_next != NULL)
55107c478bd9Sstevel@tonic-gate 		th_trace->th_next->th_prev = th_trace->th_prev;
55117c478bd9Sstevel@tonic-gate 	th_trace->th_next = NULL;
55127c478bd9Sstevel@tonic-gate 	th_trace->th_prev = NULL;
55137c478bd9Sstevel@tonic-gate 	kmem_free(th_trace, sizeof (th_trace_t));
55147c478bd9Sstevel@tonic-gate }
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate void
55177c478bd9Sstevel@tonic-gate ire_untrace_ref(ire_t *ire)
55187c478bd9Sstevel@tonic-gate {
55197c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
55207c478bd9Sstevel@tonic-gate 
55217c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
55247c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
55257c478bd9Sstevel@tonic-gate 		return;
55267c478bd9Sstevel@tonic-gate 	}
55277c478bd9Sstevel@tonic-gate 
55287c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
55297c478bd9Sstevel@tonic-gate 	ASSERT(th_trace != NULL && th_trace->th_refcnt > 0);
55307c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
55317c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt--;
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 	if (th_trace->th_refcnt == 0)
55347c478bd9Sstevel@tonic-gate 		ire_trace_free(th_trace);
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
55377c478bd9Sstevel@tonic-gate }
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate static void
55407c478bd9Sstevel@tonic-gate ire_trace_inactive(ire_t *ire)
55417c478bd9Sstevel@tonic-gate {
55427c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
55437c478bd9Sstevel@tonic-gate 	int i;
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
55467c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
55477c478bd9Sstevel@tonic-gate 		while (ire->ire_trace[i] != NULL) {
55487c478bd9Sstevel@tonic-gate 			th_trace = ire->ire_trace[i];
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 			/* unlink th_trace and free it */
55517c478bd9Sstevel@tonic-gate 			ire->ire_trace[i] = th_trace->th_next;
55527c478bd9Sstevel@tonic-gate 			if (th_trace->th_next != NULL)
55537c478bd9Sstevel@tonic-gate 				th_trace->th_next->th_prev =
55547c478bd9Sstevel@tonic-gate 				    &ire->ire_trace[i];
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate 			th_trace->th_next = NULL;
55577c478bd9Sstevel@tonic-gate 			th_trace->th_prev = NULL;
55587c478bd9Sstevel@tonic-gate 			kmem_free(th_trace, sizeof (th_trace_t));
55597c478bd9Sstevel@tonic-gate 		}
55607c478bd9Sstevel@tonic-gate 	}
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
55637c478bd9Sstevel@tonic-gate }
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate /* ARGSUSED */
55667c478bd9Sstevel@tonic-gate void
55677c478bd9Sstevel@tonic-gate ire_thread_exit(ire_t *ire, caddr_t arg)
55687c478bd9Sstevel@tonic-gate {
55697c478bd9Sstevel@tonic-gate 	th_trace_t	*th_trace;
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
55727c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
55737c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
55747c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
55757c478bd9Sstevel@tonic-gate 		return;
55767c478bd9Sstevel@tonic-gate 	}
55777c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt == 0);
55787c478bd9Sstevel@tonic-gate 
55797c478bd9Sstevel@tonic-gate 	ire_trace_free(th_trace);
55807c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
55817c478bd9Sstevel@tonic-gate }
55827c478bd9Sstevel@tonic-gate 
55837c478bd9Sstevel@tonic-gate #endif
5584c793af95Ssangeeta 
5585c793af95Ssangeeta /*
5586c793af95Ssangeeta  * Generate a message chain with an arp request to resolve the in_ire.
5587c793af95Ssangeeta  * It is assumed that in_ire itself is currently in the ire cache table,
5588c793af95Ssangeeta  * so we create a fake_ire filled with enough information about ire_addr etc.
5589c793af95Ssangeeta  * to retrieve in_ire when the DL_UNITDATA response from the resolver
5590c793af95Ssangeeta  * comes back. The fake_ire itself is created by calling esballoc with
5591c793af95Ssangeeta  * the fr_rtnp (free routine) set to ire_freemblk. This routine will be
5592c793af95Ssangeeta  * invoked when the mblk containing fake_ire is freed.
5593c793af95Ssangeeta  */
5594c793af95Ssangeeta void
5595c793af95Ssangeeta ire_arpresolve(ire_t *in_ire, ill_t *dst_ill)
5596c793af95Ssangeeta {
5597c793af95Ssangeeta 	areq_t		*areq;
5598c793af95Ssangeeta 	ipaddr_t	*addrp;
559954da8755Ssowmini 	mblk_t 		*ire_mp, *areq_mp;
5600c793af95Ssangeeta 	ire_t 		*ire, *buf;
5601c793af95Ssangeeta 	size_t		bufsize;
5602c793af95Ssangeeta 	frtn_t		*frtnp;
5603c793af95Ssangeeta 	ill_t		*ill;
5604f4b3ec61Sdh155122 	ip_stack_t	*ipst = dst_ill->ill_ipst;
5605c793af95Ssangeeta 
5606c793af95Ssangeeta 	/*
5607c793af95Ssangeeta 	 * Construct message chain for the resolver
5608c793af95Ssangeeta 	 * of the form:
5609c793af95Ssangeeta 	 *	ARP_REQ_MBLK-->IRE_MBLK
5610c793af95Ssangeeta 	 *
5611c793af95Ssangeeta 	 * NOTE : If the response does not
5612c793af95Ssangeeta 	 * come back, ARP frees the packet. For this reason,
5613c793af95Ssangeeta 	 * we can't REFHOLD the bucket of save_ire to prevent
5614c793af95Ssangeeta 	 * deletions. We may not be able to REFRELE the bucket
5615c793af95Ssangeeta 	 * if the response never comes back. Thus, before
5616c793af95Ssangeeta 	 * adding the ire, ire_add_v4 will make sure that the
5617c793af95Ssangeeta 	 * interface route does not get deleted. This is the
5618c793af95Ssangeeta 	 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6
5619c793af95Ssangeeta 	 * where we can always prevent deletions because of
5620c793af95Ssangeeta 	 * the synchronous nature of adding IRES i.e
5621c793af95Ssangeeta 	 * ire_add_then_send is called after creating the IRE.
5622c793af95Ssangeeta 	 */
5623c793af95Ssangeeta 
5624c793af95Ssangeeta 	/*
5625c793af95Ssangeeta 	 * We use esballoc to allocate the second part(the ire_t size mblk)
5626c793af95Ssangeeta 	 * of the message chain depicted above. THis mblk will be freed
5627c793af95Ssangeeta 	 * by arp when there is a  timeout, and otherwise passed to IP
5628c793af95Ssangeeta 	 * and IP will * free it after processing the ARP response.
5629c793af95Ssangeeta 	 */
5630c793af95Ssangeeta 
5631c793af95Ssangeeta 	bufsize = sizeof (ire_t) + sizeof (frtn_t);
5632c793af95Ssangeeta 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
5633c793af95Ssangeeta 	if (buf == NULL) {
5634c793af95Ssangeeta 		ip1dbg(("ire_arpresolver:alloc buffer failed\n "));
5635c793af95Ssangeeta 		return;
5636c793af95Ssangeeta 	}
5637c793af95Ssangeeta 	frtnp = (frtn_t *)(buf + 1);
5638c793af95Ssangeeta 	frtnp->free_arg = (caddr_t)buf;
5639c793af95Ssangeeta 	frtnp->free_func = ire_freemblk;
5640c793af95Ssangeeta 
5641c793af95Ssangeeta 	ire_mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp);
5642c793af95Ssangeeta 
5643c793af95Ssangeeta 	if (ire_mp == NULL) {
5644c793af95Ssangeeta 		ip1dbg(("ire_arpresolve: esballoc failed\n"));
5645c793af95Ssangeeta 		kmem_free(buf, bufsize);
5646c793af95Ssangeeta 		return;
5647c793af95Ssangeeta 	}
5648c793af95Ssangeeta 	ASSERT(in_ire->ire_nce != NULL);
564954da8755Ssowmini 	areq_mp = copyb(dst_ill->ill_resolver_mp);
565054da8755Ssowmini 	if (areq_mp == NULL) {
5651c793af95Ssangeeta 		kmem_free(buf, bufsize);
5652c793af95Ssangeeta 		return;
5653c793af95Ssangeeta 	}
5654c793af95Ssangeeta 
5655c793af95Ssangeeta 	ire_mp->b_datap->db_type = IRE_ARPRESOLVE_TYPE;
5656c793af95Ssangeeta 	ire = (ire_t *)buf;
5657c793af95Ssangeeta 	/*
5658c793af95Ssangeeta 	 * keep enough info in the fake ire so that we can pull up
5659c793af95Ssangeeta 	 * the incomplete ire (in_ire) after result comes back from
5660c793af95Ssangeeta 	 * arp and make it complete.
5661c793af95Ssangeeta 	 */
5662c793af95Ssangeeta 	*ire = ire_null;
5663c793af95Ssangeeta 	ire->ire_u = in_ire->ire_u;
5664c793af95Ssangeeta 	ire->ire_ipif_seqid = in_ire->ire_ipif_seqid;
5665c793af95Ssangeeta 	ire->ire_ipif = in_ire->ire_ipif;
5666c793af95Ssangeeta 	ire->ire_stq = in_ire->ire_stq;
5667c793af95Ssangeeta 	ill = ire_to_ill(ire);
5668c793af95Ssangeeta 	ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
5669c793af95Ssangeeta 	ire->ire_zoneid = in_ire->ire_zoneid;
5670f4b3ec61Sdh155122 	ire->ire_ipst = ipst;
5671f4b3ec61Sdh155122 
5672c793af95Ssangeeta 	/*
5673c793af95Ssangeeta 	 * ire_freemblk will be called when ire_mp is freed, both for
5674c793af95Ssangeeta 	 * successful and failed arp resolution. IRE_MARK_UNCACHED will be set
5675c793af95Ssangeeta 	 * when the arp resolution failed.
5676c793af95Ssangeeta 	 */
5677c793af95Ssangeeta 	ire->ire_marks |= IRE_MARK_UNCACHED;
5678c793af95Ssangeeta 	ire->ire_mp = ire_mp;
5679c793af95Ssangeeta 	ire_mp->b_wptr = (uchar_t *)&ire[1];
5680c793af95Ssangeeta 	ire_mp->b_cont = NULL;
568154da8755Ssowmini 	linkb(areq_mp, ire_mp);
5682c793af95Ssangeeta 
5683c793af95Ssangeeta 	/*
5684c793af95Ssangeeta 	 * Fill in the source and dest addrs for the resolver.
5685c793af95Ssangeeta 	 * NOTE: this depends on memory layouts imposed by
5686c793af95Ssangeeta 	 * ill_init().
5687c793af95Ssangeeta 	 */
568854da8755Ssowmini 	areq = (areq_t *)areq_mp->b_rptr;
5689c793af95Ssangeeta 	addrp = (ipaddr_t *)((char *)areq + areq->areq_sender_addr_offset);
5690c793af95Ssangeeta 	*addrp = ire->ire_src_addr;
5691c793af95Ssangeeta 
5692c793af95Ssangeeta 	addrp = (ipaddr_t *)((char *)areq + areq->areq_target_addr_offset);
5693c793af95Ssangeeta 	if (ire->ire_gateway_addr != INADDR_ANY) {
5694c793af95Ssangeeta 		*addrp = ire->ire_gateway_addr;
5695c793af95Ssangeeta 	} else {
5696c793af95Ssangeeta 		*addrp = ire->ire_addr;
5697c793af95Ssangeeta 	}
5698c793af95Ssangeeta 
5699c793af95Ssangeeta 	/* Up to the resolver. */
5700c793af95Ssangeeta 	if (canputnext(dst_ill->ill_rq)) {
570154da8755Ssowmini 		putnext(dst_ill->ill_rq, areq_mp);
5702c793af95Ssangeeta 	} else {
570354da8755Ssowmini 		freemsg(areq_mp);
5704c793af95Ssangeeta 	}
5705c793af95Ssangeeta }
5706c793af95Ssangeeta 
5707c793af95Ssangeeta /*
5708c793af95Ssangeeta  * Esballoc free function for AR_ENTRY_QUERY request to clean up any
5709c793af95Ssangeeta  * unresolved ire_t and/or nce_t structures when ARP resolution fails.
5710c793af95Ssangeeta  *
5711c793af95Ssangeeta  * This function can be called by ARP via free routine for ire_mp or
5712c793af95Ssangeeta  * by IPv4(both host and forwarding path) via ire_delete
5713c793af95Ssangeeta  * in case ARP resolution fails.
5714c793af95Ssangeeta  * NOTE: Since IP is MT, ARP can call into IP but not vice versa
5715c793af95Ssangeeta  * (for IP to talk to ARP, it still has to send AR* messages).
5716c793af95Ssangeeta  *
5717c793af95Ssangeeta  * Note that the ARP/IP merge should replace the functioanlity by providing
5718c793af95Ssangeeta  * direct function calls to clean up unresolved entries in ire/nce lists.
5719c793af95Ssangeeta  */
5720c793af95Ssangeeta void
5721c793af95Ssangeeta ire_freemblk(ire_t *ire_mp)
5722c793af95Ssangeeta {
5723c793af95Ssangeeta 	nce_t		*nce = NULL;
5724c793af95Ssangeeta 	ill_t		*ill;
5725f4b3ec61Sdh155122 	ip_stack_t	*ipst;
5726c793af95Ssangeeta 
5727c793af95Ssangeeta 	ASSERT(ire_mp != NULL);
5728c793af95Ssangeeta 
5729c793af95Ssangeeta 	if ((ire_mp->ire_addr == NULL) && (ire_mp->ire_gateway_addr == NULL)) {
5730c793af95Ssangeeta 		ip1dbg(("ire_freemblk(0x%p) ire_addr is NULL\n",
5731c793af95Ssangeeta 		    (void *)ire_mp));
5732c793af95Ssangeeta 		goto cleanup;
5733c793af95Ssangeeta 	}
5734c793af95Ssangeeta 	if ((ire_mp->ire_marks & IRE_MARK_UNCACHED) == 0) {
5735c793af95Ssangeeta 		goto cleanup; /* everything succeeded. just free and return */
5736c793af95Ssangeeta 	}
5737c793af95Ssangeeta 
5738c793af95Ssangeeta 	/*
5739c793af95Ssangeeta 	 * the arp information corresponding to this ire_mp was not
5740c793af95Ssangeeta 	 * transferred to  a ire_cache entry. Need
5741c793af95Ssangeeta 	 * to clean up incomplete ire's and nce, if necessary.
5742c793af95Ssangeeta 	 */
5743c793af95Ssangeeta 	ASSERT(ire_mp->ire_stq != NULL);
5744c793af95Ssangeeta 	ASSERT(ire_mp->ire_stq_ifindex != 0);
5745f4b3ec61Sdh155122 	ASSERT(ire_mp->ire_ipst != NULL);
5746f4b3ec61Sdh155122 
5747f4b3ec61Sdh155122 	ipst = ire_mp->ire_ipst;
5748f4b3ec61Sdh155122 
5749c793af95Ssangeeta 	/*
5750c793af95Ssangeeta 	 * Get any nce's corresponding to this ire_mp. We first have to
5751c793af95Ssangeeta 	 * make sure that the ill is still around.
5752c793af95Ssangeeta 	 */
5753f4b3ec61Sdh155122 	ill = ill_lookup_on_ifindex(ire_mp->ire_stq_ifindex,
5754f4b3ec61Sdh155122 	    B_FALSE, NULL, NULL, NULL, NULL, ipst);
5755c793af95Ssangeeta 	if (ill == NULL || (ire_mp->ire_stq != ill->ill_wq) ||
5756c793af95Ssangeeta 	    (ill->ill_state_flags & ILL_CONDEMNED)) {
5757c793af95Ssangeeta 		/*
5758c793af95Ssangeeta 		 * ill went away. no nce to clean up.
5759c793af95Ssangeeta 		 * Note that the ill_state_flags could be set to
5760c793af95Ssangeeta 		 * ILL_CONDEMNED after this point, but if we know
5761c793af95Ssangeeta 		 * that it is CONDEMNED now, we just bail out quickly.
5762c793af95Ssangeeta 		 */
5763c793af95Ssangeeta 		if (ill != NULL)
5764c793af95Ssangeeta 			ill_refrele(ill);
5765c793af95Ssangeeta 		goto cleanup;
5766c793af95Ssangeeta 	}
5767c793af95Ssangeeta 	nce = ndp_lookup_v4(ill,
5768c793af95Ssangeeta 	    ((ire_mp->ire_gateway_addr != INADDR_ANY) ?
5769c793af95Ssangeeta 	    &ire_mp->ire_gateway_addr : &ire_mp->ire_addr),
5770c793af95Ssangeeta 	    B_FALSE);
5771c793af95Ssangeeta 	ill_refrele(ill);
5772c793af95Ssangeeta 
5773c793af95Ssangeeta 	if ((nce != NULL) && (nce->nce_state != ND_REACHABLE)) {
5774c793af95Ssangeeta 		/*
5775c793af95Ssangeeta 		 * some incomplete nce was found.
5776c793af95Ssangeeta 		 */
5777c793af95Ssangeeta 		DTRACE_PROBE2(ire__freemblk__arp__resolv__fail,
5778c793af95Ssangeeta 		    nce_t *, nce, ire_t *, ire_mp);
5779c793af95Ssangeeta 		/*
5780c793af95Ssangeeta 		 * Send the icmp_unreachable messages for the queued mblks in
5781c793af95Ssangeeta 		 * ire->ire_nce->nce_qd_mp, since ARP resolution failed
5782c793af95Ssangeeta 		 * for this ire
5783c793af95Ssangeeta 		 */
5784c793af95Ssangeeta 		arp_resolv_failed(nce);
5785c793af95Ssangeeta 		/*
5786c793af95Ssangeeta 		 * Delete the nce and clean up all ire's pointing at this nce
5787c793af95Ssangeeta 		 * in the cachetable
5788c793af95Ssangeeta 		 */
5789c793af95Ssangeeta 		ndp_delete(nce);
5790c793af95Ssangeeta 	}
5791c793af95Ssangeeta 	if (nce != NULL)
5792c793af95Ssangeeta 		NCE_REFRELE(nce); /* release the ref taken by ndp_lookup_v4 */
5793c793af95Ssangeeta 
5794c793af95Ssangeeta cleanup:
5795c793af95Ssangeeta 	/*
5796c793af95Ssangeeta 	 * Get rid of the ire buffer
5797c793af95Ssangeeta 	 * We call kmem_free here(instead of ire_delete()), since
5798c793af95Ssangeeta 	 * this is the freeb's callback.
5799c793af95Ssangeeta 	 */
5800c793af95Ssangeeta 	kmem_free(ire_mp, sizeof (ire_t) + sizeof (frtn_t));
5801c793af95Ssangeeta }
5802c793af95Ssangeeta 
5803c0861880Ssangeeta /*
580454da8755Ssowmini  * find, or create if needed, a neighbor cache entry nce_t for IRE_CACHE and
580554da8755Ssowmini  * non-loopback IRE_BROADCAST ire's.
580654da8755Ssowmini  *
580754da8755Ssowmini  * If a neighbor-cache entry has to be created (i.e., one does not already
580854da8755Ssowmini  * exist in the nce list) the nce_res_mp and nce_state of the neighbor cache
580954da8755Ssowmini  * entry are initialized in ndp_add_v4(). These values are picked from
581054da8755Ssowmini  * the src_nce, if one is passed in. Otherwise (if src_nce == NULL) the
581154da8755Ssowmini  * ire->ire_type and the outgoing interface (ire_to_ill(ire)) values
581254da8755Ssowmini  * determine the {nce_state, nce_res_mp} of the nce_t created. All
581354da8755Ssowmini  * IRE_BROADCAST entries have nce_state = ND_REACHABLE, and the nce_res_mp
581454da8755Ssowmini  * is set to the ill_bcast_mp of the outgoing inerface. For unicast ire
581554da8755Ssowmini  * entries,
581654da8755Ssowmini  *   - if the outgoing interface is of type IRE_IF_RESOLVER, a newly created
581754da8755Ssowmini  *     nce_t will have a null nce_res_mp, and will be in the ND_INITIAL state.
581854da8755Ssowmini  *   - if the outgoing interface is a IRE_IF_NORESOLVER interface, no link
581954da8755Ssowmini  *     layer resolution is necessary, so that the nce_t will be in the
582054da8755Ssowmini  *     ND_REACHABLE state and the nce_res_mp will have a copy of the
582154da8755Ssowmini  *     ill_resolver_mp of the outgoing interface.
582254da8755Ssowmini  *
582354da8755Ssowmini  * The link layer information needed for broadcast addresses, and for
582454da8755Ssowmini  * packets sent on IRE_IF_NORESOLVER interfaces is a constant mapping that
582554da8755Ssowmini  * never needs re-verification for the lifetime of the nce_t. These are
582654da8755Ssowmini  * therefore marked NCE_F_PERMANENT, and never allowed to expire via
582754da8755Ssowmini  * NCE_EXPIRED.
582854da8755Ssowmini  *
582954da8755Ssowmini  * IRE_CACHE ire's contain the information for  the nexthop (ire_gateway_addr)
583054da8755Ssowmini  * in the case of indirect routes, and for the dst itself (ire_addr) in the
5831c793af95Ssangeeta  * case of direct routes, with the nce_res_mp containing a template
5832c793af95Ssangeeta  * DL_UNITDATA request.
5833c793af95Ssangeeta  *
5834c793af95Ssangeeta  * The actual association of the ire_nce to the nce created here is
5835c793af95Ssangeeta  * typically done in ire_add_v4 for IRE_CACHE entries. Exceptions
5836c793af95Ssangeeta  * to this rule are SO_DONTROUTE ire's (IRE_MARK_NO_ADD), for which
5837*5c0b7edeSseb  * the ire_nce assignment is done in ire_add_then_send.
5838c793af95Ssangeeta  */
5839c793af95Ssangeeta int
584054da8755Ssowmini ire_nce_init(ire_t *ire, nce_t *src_nce)
5841c793af95Ssangeeta {
584254da8755Ssowmini 	in_addr_t	addr4;
5843c793af95Ssangeeta 	int		err;
584454da8755Ssowmini 	nce_t		*nce = NULL;
5845c793af95Ssangeeta 	ill_t		*ire_ill;
584654da8755Ssowmini 	uint16_t	nce_flags = 0;
5847f4b3ec61Sdh155122 	ip_stack_t	*ipst;
5848c793af95Ssangeeta 
584954da8755Ssowmini 	if (ire->ire_stq == NULL)
5850c793af95Ssangeeta 		return (0); /* no need to create nce for local/loopback */
5851c793af95Ssangeeta 
5852c793af95Ssangeeta 	switch (ire->ire_type) {
5853c793af95Ssangeeta 	case IRE_CACHE:
5854c793af95Ssangeeta 		if (ire->ire_gateway_addr != INADDR_ANY)
5855c793af95Ssangeeta 			addr4 = ire->ire_gateway_addr; /* 'G' route */
5856c793af95Ssangeeta 		else
5857c793af95Ssangeeta 			addr4 = ire->ire_addr; /* direct route */
5858c793af95Ssangeeta 		break;
5859c793af95Ssangeeta 	case IRE_BROADCAST:
5860c793af95Ssangeeta 		addr4 = ire->ire_addr;
586154da8755Ssowmini 		nce_flags |= (NCE_F_PERMANENT|NCE_F_BCAST);
5862c793af95Ssangeeta 		break;
5863c793af95Ssangeeta 	default:
5864c793af95Ssangeeta 		return (0);
5865c793af95Ssangeeta 	}
5866c793af95Ssangeeta 
5867c793af95Ssangeeta 	/*
5868c793af95Ssangeeta 	 * ire_ipif is picked based on RTF_SETSRC, usesrc etc.
5869c793af95Ssangeeta 	 * rules in ire_forward_src_ipif. We want the dlureq_mp
5870c793af95Ssangeeta 	 * for the outgoing interface, which we get from the ire_stq.
5871c793af95Ssangeeta 	 */
5872c793af95Ssangeeta 	ire_ill = ire_to_ill(ire);
5873f4b3ec61Sdh155122 	ipst = ire_ill->ill_ipst;
5874c793af95Ssangeeta 
5875c793af95Ssangeeta 	/*
587654da8755Ssowmini 	 * IRE_IF_NORESOLVER entries never need re-verification and
587754da8755Ssowmini 	 * do not expire, so we mark them as NCE_F_PERMANENT.
5878c793af95Ssangeeta 	 */
587954da8755Ssowmini 	if (ire_ill->ill_net_type == IRE_IF_NORESOLVER)
588054da8755Ssowmini 		nce_flags |= NCE_F_PERMANENT;
5881c793af95Ssangeeta 
5882b9c344b3Ssowmini retry_nce:
588354da8755Ssowmini 	err = ndp_lookup_then_add_v4(ire_ill, &addr4, nce_flags,
588454da8755Ssowmini 	    &nce, src_nce);
5885c793af95Ssangeeta 
588654da8755Ssowmini 	if (err == EEXIST && NCE_EXPIRED(nce, ipst)) {
5887b9c344b3Ssowmini 		/*
5888b9c344b3Ssowmini 		 * We looked up an expired nce.
5889b9c344b3Ssowmini 		 * Go back and try to create one again.
5890b9c344b3Ssowmini 		 */
589154da8755Ssowmini 		ndp_delete(nce);
589254da8755Ssowmini 		NCE_REFRELE(nce);
589354da8755Ssowmini 		nce = NULL;
5894b9c344b3Ssowmini 		goto retry_nce;
5895b9c344b3Ssowmini 	}
5896b9c344b3Ssowmini 
589754da8755Ssowmini 	ip1dbg(("ire 0x%p addr 0x%lx type 0x%x; found nce 0x%p err %d\n",
589854da8755Ssowmini 	    (void *)ire, (ulong_t)addr4, ire->ire_type, (void *)nce, err));
5899c793af95Ssangeeta 
5900c793af95Ssangeeta 	switch (err) {
5901c793af95Ssangeeta 	case 0:
5902c793af95Ssangeeta 	case EEXIST:
5903c793af95Ssangeeta 		/*
590454da8755Ssowmini 		 * return a pointer to a newly created or existing nce_t;
5905c793af95Ssangeeta 		 * note that the ire-nce mapping is many-one, i.e.,
590654da8755Ssowmini 		 * multiple ire's could point to the same nce_t.
5907c793af95Ssangeeta 		 */
5908c793af95Ssangeeta 		break;
5909c793af95Ssangeeta 	default:
5910c793af95Ssangeeta 		DTRACE_PROBE2(nce__init__fail, ill_t *, ire_ill, int, err);
5911c793af95Ssangeeta 		return (EINVAL);
5912c793af95Ssangeeta 	}
5913c793af95Ssangeeta 	if (ire->ire_type == IRE_BROADCAST) {
5914c793af95Ssangeeta 		/*
5915c793af95Ssangeeta 		 * Two bcast ires are created for each interface;
5916c793af95Ssangeeta 		 * 1. loopback copy (which does not  have an
5917c793af95Ssangeeta 		 *    ire_stq, and therefore has no ire_nce), and,
5918c793af95Ssangeeta 		 * 2. the non-loopback copy, which has the nce_res_mp
5919c793af95Ssangeeta 		 *    initialized to a copy of the ill_bcast_mp, and
5920c793af95Ssangeeta 		 *    is marked as ND_REACHABLE at this point.
5921c793af95Ssangeeta 		 *    This nce does not undergo any further state changes,
5922c793af95Ssangeeta 		 *    and exists as long as the interface is plumbed.
5923c793af95Ssangeeta 		 * Note: we do the ire_nce assignment here for IRE_BROADCAST
5924c793af95Ssangeeta 		 * because some functions like ill_mark_bcast() inline the
592554da8755Ssowmini 		 * ire_add functionality.
5926c793af95Ssangeeta 		 */
592754da8755Ssowmini 		ire->ire_nce = nce;
5928c793af95Ssangeeta 		/*
5929c793af95Ssangeeta 		 * We are associating this nce to the ire,
5930c793af95Ssangeeta 		 * so change the nce ref taken in
5931c793af95Ssangeeta 		 * ndp_lookup_then_add_v4() from
5932c793af95Ssangeeta 		 * NCE_REFHOLD to NCE_REFHOLD_NOTR
5933c793af95Ssangeeta 		 */
5934c793af95Ssangeeta 		NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
5935c793af95Ssangeeta 	} else {
5936c793af95Ssangeeta 		/*
5937c793af95Ssangeeta 		 * We are not using this nce_t just yet so release
5938c793af95Ssangeeta 		 * the ref taken in ndp_lookup_then_add_v4()
5939c793af95Ssangeeta 		 */
594054da8755Ssowmini 		NCE_REFRELE(nce);
5941c793af95Ssangeeta 	}
5942c793af95Ssangeeta 	return (0);
5943c793af95Ssangeeta }
5944