xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision b9c344b337ccebc3d3a357178159b2d54338fd87)
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],
1887c478bd9Sstevel@tonic-gate  * ipif_to_ire[_v6], ire_mrtun_lookup, ire_srcif_table_lookup.
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  * A per-interface routing table is created ( if not present)
2387c478bd9Sstevel@tonic-gate  * when the first entry is added to this special routing table.
2397c478bd9Sstevel@tonic-gate  * This special routing table is accessed through the ill data structure.
2407c478bd9Sstevel@tonic-gate  * The routing table looks like cache table. For example, currently it
2417c478bd9Sstevel@tonic-gate  * is used by mobile-ip foreign agent to forward data that only comes from
2427c478bd9Sstevel@tonic-gate  * the home agent tunnel for a mobile node. Thus if the outgoing interface
2437c478bd9Sstevel@tonic-gate  * is a RESOLVER interface, IP may need to resolve the hardware address for
2447c478bd9Sstevel@tonic-gate  * the outgoing interface. The routing entries in this table are not updated
2457c478bd9Sstevel@tonic-gate  * in IRE_CACHE. When MCTL msg comes back from ARP, the incoming ill informa-
2467c478bd9Sstevel@tonic-gate  * tion is lost as the write queue is passed to ip_wput.
2477c478bd9Sstevel@tonic-gate  * But, before sending the packet out, the hardware information must be updated
2487c478bd9Sstevel@tonic-gate  * in the special forwarding table. ire_srcif_table_count keeps track of total
2497c478bd9Sstevel@tonic-gate  * number of ires that are in interface based tables. Each interface based
2507c478bd9Sstevel@tonic-gate  * table hangs off of the incoming ill and each ill_t also keeps a refcnt
2517c478bd9Sstevel@tonic-gate  * of ires in that table.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * The minimum size of IRE cache table.  It will be recalcuated in
2567c478bd9Sstevel@tonic-gate  * ip_ire_init().
257f4b3ec61Sdh155122  * Setable in /etc/system
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate uint32_t ip_cache_table_size = IP_CACHE_TABLE_SIZE;
2607c478bd9Sstevel@tonic-gate uint32_t ip6_cache_table_size = IP6_CACHE_TABLE_SIZE;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * The size of the forwarding table.  We will make sure that it is a
2647c478bd9Sstevel@tonic-gate  * power of 2 in ip_ire_init().
265f4b3ec61Sdh155122  * Setable in /etc/system
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate struct	kmem_cache	*ire_cache;
2707c478bd9Sstevel@tonic-gate static ire_t	ire_null;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * The threshold number of IRE in a bucket when the IREs are
2747c478bd9Sstevel@tonic-gate  * cleaned up.  This threshold is calculated later in ip_open()
2757c478bd9Sstevel@tonic-gate  * based on the speed of CPU and available memory.  This default
2767c478bd9Sstevel@tonic-gate  * value is the maximum.
2777c478bd9Sstevel@tonic-gate  *
2787c478bd9Sstevel@tonic-gate  * We have two kinds of cached IRE, temporary and
2797c478bd9Sstevel@tonic-gate  * non-temporary.  Temporary IREs are marked with
2807c478bd9Sstevel@tonic-gate  * IRE_MARK_TEMPORARY.  They are IREs created for non
2817c478bd9Sstevel@tonic-gate  * TCP traffic and for forwarding purposes.  All others
2827c478bd9Sstevel@tonic-gate  * are non-temporary IREs.  We don't mark IRE created for
2837c478bd9Sstevel@tonic-gate  * TCP as temporary because TCP is stateful and there are
2847c478bd9Sstevel@tonic-gate  * info stored in the IRE which can be shared by other TCP
2857c478bd9Sstevel@tonic-gate  * connections to the same destination.  For connected
2867c478bd9Sstevel@tonic-gate  * endpoint, we also don't want to mark the IRE used as
2877c478bd9Sstevel@tonic-gate  * temporary because the same IRE will be used frequently,
2887c478bd9Sstevel@tonic-gate  * otherwise, the app should not do a connect().  We change
2897c478bd9Sstevel@tonic-gate  * the marking at ip_bind_connected_*() if necessary.
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * We want to keep the cache IRE hash bucket length reasonably
2927c478bd9Sstevel@tonic-gate  * short, otherwise IRE lookup functions will take "forever."
2937c478bd9Sstevel@tonic-gate  * We use the "crude" function that the IRE bucket
2947c478bd9Sstevel@tonic-gate  * length should be based on the CPU speed, which is 1 entry
2957c478bd9Sstevel@tonic-gate  * per x MHz, depending on the shift factor ip_ire_cpu_ratio
2967c478bd9Sstevel@tonic-gate  * (n).  This means that with a 750MHz CPU, the max bucket
2977c478bd9Sstevel@tonic-gate  * length can be (750 >> n) entries.
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * Note that this threshold is separate for temp and non-temp
3007c478bd9Sstevel@tonic-gate  * IREs.  This means that the actual bucket length can be
3017c478bd9Sstevel@tonic-gate  * twice as that.  And while we try to keep temporary IRE
3027c478bd9Sstevel@tonic-gate  * length at most at the threshold value, we do not attempt to
3037c478bd9Sstevel@tonic-gate  * make the length for non-temporary IREs fixed, for the
3047c478bd9Sstevel@tonic-gate  * reason stated above.  Instead, we start trying to find
3057c478bd9Sstevel@tonic-gate  * "unused" non-temporary IREs when the bucket length reaches
3067c478bd9Sstevel@tonic-gate  * this threshold and clean them up.
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * We also want to limit the amount of memory used by
3097c478bd9Sstevel@tonic-gate  * IREs.  So if we are allowed to use ~3% of memory (M)
3107c478bd9Sstevel@tonic-gate  * for those IREs, each bucket should not have more than
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * 	M / num of cache bucket / sizeof (ire_t)
3137c478bd9Sstevel@tonic-gate  *
3147c478bd9Sstevel@tonic-gate  * Again the above memory uses are separate for temp and
3157c478bd9Sstevel@tonic-gate  * non-temp cached IREs.
3167c478bd9Sstevel@tonic-gate  *
3177c478bd9Sstevel@tonic-gate  * We may also want the limit to be a function of the number
3187c478bd9Sstevel@tonic-gate  * of interfaces and number of CPUs.  Doing the initialization
3197c478bd9Sstevel@tonic-gate  * in ip_open() means that every time an interface is plumbed,
3207c478bd9Sstevel@tonic-gate  * the max is re-calculated.  Right now, we don't do anything
3217c478bd9Sstevel@tonic-gate  * different.  In future, when we have more experience, we
3227c478bd9Sstevel@tonic-gate  * may want to change this behavior.
3237c478bd9Sstevel@tonic-gate  */
324f4b3ec61Sdh155122 uint32_t ip_ire_max_bucket_cnt = 10;	/* Setable in /etc/system */
3257c478bd9Sstevel@tonic-gate uint32_t ip6_ire_max_bucket_cnt = 10;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * The minimum of the temporary IRE bucket count.  We do not want
3297c478bd9Sstevel@tonic-gate  * the length of each bucket to be too short.  This may hurt
3307c478bd9Sstevel@tonic-gate  * performance of some apps as the temporary IREs are removed too
3317c478bd9Sstevel@tonic-gate  * often.
3327c478bd9Sstevel@tonic-gate  */
333f4b3ec61Sdh155122 uint32_t ip_ire_min_bucket_cnt = 3;	/* /etc/system - not used */
3347c478bd9Sstevel@tonic-gate uint32_t ip6_ire_min_bucket_cnt = 3;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * The ratio of memory consumed by IRE used for temporary to available
3387c478bd9Sstevel@tonic-gate  * memory.  This is a shift factor, so 6 means the ratio 1 to 64.  This
3397c478bd9Sstevel@tonic-gate  * value can be changed in /etc/system.  6 is a reasonable number.
3407c478bd9Sstevel@tonic-gate  */
341f4b3ec61Sdh155122 uint32_t ip_ire_mem_ratio = 6;	/* /etc/system */
3427c478bd9Sstevel@tonic-gate /* The shift factor for CPU speed to calculate the max IRE bucket length. */
343f4b3ec61Sdh155122 uint32_t ip_ire_cpu_ratio = 7;	/* /etc/system */
3447c478bd9Sstevel@tonic-gate 
345c793af95Ssangeeta typedef struct nce_clookup_s {
346c793af95Ssangeeta 	ipaddr_t ncecl_addr;
347c793af95Ssangeeta 	boolean_t ncecl_found;
348c793af95Ssangeeta } nce_clookup_t;
349c793af95Ssangeeta 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * The maximum number of buckets in IRE cache table.  In future, we may
3527c478bd9Sstevel@tonic-gate  * want to make it a dynamic hash table.  For the moment, we fix the
3537c478bd9Sstevel@tonic-gate  * size and allocate the table in ip_ire_init() when IP is first loaded.
3547c478bd9Sstevel@tonic-gate  * We take into account the amount of memory a system has.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate #define	IP_MAX_CACHE_TABLE_SIZE	4096
3577c478bd9Sstevel@tonic-gate 
358f4b3ec61Sdh155122 /* Setable in /etc/system */
3597c478bd9Sstevel@tonic-gate static uint32_t	ip_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
3607c478bd9Sstevel@tonic-gate static uint32_t	ip6_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate #define	NUM_ILLS	3	/* To build the ILL list to unlock */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /* Zero iulp_t for initialization. */
3657c478bd9Sstevel@tonic-gate const iulp_t	ire_uinfo_null = { 0 };
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int	ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
368c793af95Ssangeeta     ipsq_func_t func, boolean_t);
3697c478bd9Sstevel@tonic-gate static int	ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
3707c478bd9Sstevel@tonic-gate     ipsq_func_t func);
3717c478bd9Sstevel@tonic-gate static ire_t	*ire_update_srcif_v4(ire_t *ire);
3727c478bd9Sstevel@tonic-gate static void	ire_delete_v4(ire_t *ire);
3737c478bd9Sstevel@tonic-gate static void	ire_report_ctable(ire_t *ire, char *mp);
3747c478bd9Sstevel@tonic-gate static void	ire_report_mrtun_table(ire_t *ire, char *mp);
375f4b3ec61Sdh155122 static void	ire_report_srcif_table(ire_t *ire, char *mp, ip_stack_t *ipst);
37645916cd2Sjpk static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
377f4b3ec61Sdh155122     zoneid_t zoneid, ip_stack_t *);
3787c478bd9Sstevel@tonic-gate static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
37945916cd2Sjpk     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
3807c478bd9Sstevel@tonic-gate static void	ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt);
3817c478bd9Sstevel@tonic-gate extern void	ill_unlock_ills(ill_t **list, int cnt);
382c793af95Ssangeeta static	void	ip_nce_clookup_and_delete(nce_t *nce, void *arg);
3837c478bd9Sstevel@tonic-gate extern void	th_trace_rrecord(th_trace_t *);
3847c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
3857c478bd9Sstevel@tonic-gate static void	ire_trace_inactive(ire_t *);
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * To avoid bloating the code, we call this function instead of
3907c478bd9Sstevel@tonic-gate  * using the macro IRE_REFRELE. Use macro only in performance
3917c478bd9Sstevel@tonic-gate  * critical paths.
3927c478bd9Sstevel@tonic-gate  *
3937c478bd9Sstevel@tonic-gate  * Must not be called while holding any locks. Otherwise if this is
3947c478bd9Sstevel@tonic-gate  * the last reference to be released there is a chance of recursive mutex
3957c478bd9Sstevel@tonic-gate  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
3967c478bd9Sstevel@tonic-gate  * to restart an ioctl. The one exception is when the caller is sure that
3977c478bd9Sstevel@tonic-gate  * this is not the last reference to be released. Eg. if the caller is
3987c478bd9Sstevel@tonic-gate  * sure that the ire has not been deleted and won't be deleted.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate void
4017c478bd9Sstevel@tonic-gate ire_refrele(ire_t *ire)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	IRE_REFRELE(ire);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate void
4077c478bd9Sstevel@tonic-gate ire_refrele_notr(ire_t *ire)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * kmem_cache_alloc constructor for IRE in kma space.
4147c478bd9Sstevel@tonic-gate  * Note that when ire_mp is set the IRE is stored in that mblk and
4157c478bd9Sstevel@tonic-gate  * not in this cache.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate /* ARGSUSED */
4187c478bd9Sstevel@tonic-gate static int
4197c478bd9Sstevel@tonic-gate ip_ire_constructor(void *buf, void *cdrarg, int kmflags)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
4227c478bd9Sstevel@tonic-gate 
423c793af95Ssangeeta 	ire->ire_nce = NULL;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	return (0);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
4297c478bd9Sstevel@tonic-gate static void
4307c478bd9Sstevel@tonic-gate ip_ire_destructor(void *buf, void *cdrarg)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
4337c478bd9Sstevel@tonic-gate 
434c793af95Ssangeeta 	ASSERT(ire->ire_nce == NULL);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_ADVISE_NO_REPLY
4397c478bd9Sstevel@tonic-gate  * IOCTL.  It is used by TCP (or other ULPs) to supply revised information
4407c478bd9Sstevel@tonic-gate  * for an existing CACHED IRE.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate /* ARGSUSED */
4437c478bd9Sstevel@tonic-gate int
4447c478bd9Sstevel@tonic-gate ip_ire_advise(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	uchar_t	*addr_ucp;
4477c478bd9Sstevel@tonic-gate 	ipic_t	*ipic;
4487c478bd9Sstevel@tonic-gate 	ire_t	*ire;
4497c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
4507c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
4517c478bd9Sstevel@tonic-gate 	irb_t	*irb;
4527c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
453f4b3ec61Sdh155122 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
4567c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/*
4597c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
4607c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
4617c478bd9Sstevel@tonic-gate 	 */
462f4b3ec61Sdh155122 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
4637c478bd9Sstevel@tonic-gate 		return (EPERM);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	ipic = (ipic_t *)mp->b_rptr;
4667c478bd9Sstevel@tonic-gate 	if (!(addr_ucp = mi_offset_param(mp, ipic->ipic_addr_offset,
4677c478bd9Sstevel@tonic-gate 	    ipic->ipic_addr_length))) {
4687c478bd9Sstevel@tonic-gate 		return (EINVAL);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	if (!OK_32PTR(addr_ucp))
4717c478bd9Sstevel@tonic-gate 		return (EINVAL);
4727c478bd9Sstevel@tonic-gate 	switch (ipic->ipic_addr_length) {
4737c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN: {
4747c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
4757c478bd9Sstevel@tonic-gate 		addr = *(ipaddr_t *)addr_ucp;
4767c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
477f4b3ec61Sdh155122 		ire = ire_cache_lookup(addr, zoneid, NULL, ipst);
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	case IPV6_ADDR_LEN: {
4817c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
4827c478bd9Sstevel@tonic-gate 		v6addr = *(in6_addr_t *)addr_ucp;
4837c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
484f4b3ec61Sdh155122 		ire = ire_cache_lookup_v6(&v6addr, zoneid, NULL, ipst);
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	default:
4887c478bd9Sstevel@tonic-gate 		return (EINVAL);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (ire == NULL)
4927c478bd9Sstevel@tonic-gate 		return (ENOENT);
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * Update the round trip time estimate and/or the max frag size
4957c478bd9Sstevel@tonic-gate 	 * and/or the slow start threshold.
4967c478bd9Sstevel@tonic-gate 	 *
4977c478bd9Sstevel@tonic-gate 	 * We serialize multiple advises using ire_lock.
4987c478bd9Sstevel@tonic-gate 	 */
4997c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
5007c478bd9Sstevel@tonic-gate 	if (ipic->ipic_rtt) {
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * If there is no old cached values, initialize them
5037c478bd9Sstevel@tonic-gate 		 * conservatively.  Set them to be (1.5 * new value).
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt != 0) {
5067c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
5077c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt) >> 1;
5087c478bd9Sstevel@tonic-gate 		} else {
5097c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = ipic->ipic_rtt +
5107c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt >> 1);
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
5137c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd =
5147c478bd9Sstevel@tonic-gate 			    (ire->ire_uinfo.iulp_rtt_sd +
5157c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt_sd) >> 1;
5167c478bd9Sstevel@tonic-gate 		} else {
5177c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd = ipic->ipic_rtt_sd +
5187c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt_sd >> 1);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	if (ipic->ipic_max_frag)
5227c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = MIN(ipic->ipic_max_frag, IP_MAXPACKET);
5237c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ssthresh != 0) {
5247c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_ssthresh != 0)
5257c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh =
5267c478bd9Sstevel@tonic-gate 			    (ipic->ipic_ssthresh +
5277c478bd9Sstevel@tonic-gate 			    ire->ire_uinfo.iulp_ssthresh) >> 1;
5287c478bd9Sstevel@tonic-gate 		else
5297c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh = ipic->ipic_ssthresh;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Don't need the ire_lock below this. ire_type does not change
5337c478bd9Sstevel@tonic-gate 	 * after initialization. ire_marks is protected by irb_lock.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ire_marks != 0 && ire->ire_type == IRE_CACHE) {
5387c478bd9Sstevel@tonic-gate 		/*
5397c478bd9Sstevel@tonic-gate 		 * Only increment the temporary IRE count if the original
5407c478bd9Sstevel@tonic-gate 		 * IRE is not already marked temporary.
5417c478bd9Sstevel@tonic-gate 		 */
5427c478bd9Sstevel@tonic-gate 		irb = ire->ire_bucket;
5437c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_WRITER);
5447c478bd9Sstevel@tonic-gate 		if ((ipic->ipic_ire_marks & IRE_MARK_TEMPORARY) &&
5457c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_TEMPORARY)) {
5467c478bd9Sstevel@tonic-gate 			irb->irb_tmp_ire_cnt++;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		ire->ire_marks |= ipic->ipic_ire_marks;
5497c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
5537c478bd9Sstevel@tonic-gate 	return (0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
5587c478bd9Sstevel@tonic-gate  * IOCTL[s].  The NO_REPLY form is used by TCP to delete a route IRE
5597c478bd9Sstevel@tonic-gate  * for a host that is not responding.  This will force an attempt to
560dc041e83Scarlsonj  * establish a new route, if available, and flush out the ARP entry so
561dc041e83Scarlsonj  * it will re-resolve.  Management processes may want to use the
562dc041e83Scarlsonj  * version that generates a reply.
5637c478bd9Sstevel@tonic-gate  *
5647c478bd9Sstevel@tonic-gate  * This function does not support IPv6 since Neighbor Unreachability Detection
5657c478bd9Sstevel@tonic-gate  * means that negative advise like this is useless.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate /* ARGSUSED */
5687c478bd9Sstevel@tonic-gate int
5697c478bd9Sstevel@tonic-gate ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	uchar_t		*addr_ucp;
5727c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
5737c478bd9Sstevel@tonic-gate 	ire_t		*ire;
5747c478bd9Sstevel@tonic-gate 	ipid_t		*ipid;
5757c478bd9Sstevel@tonic-gate 	boolean_t	routing_sock_info = B_FALSE;	/* Sent info? */
5767c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
577c793af95Ssangeeta 	ire_t		*gire = NULL;
578dc041e83Scarlsonj 	ill_t		*ill;
579dc041e83Scarlsonj 	mblk_t		*arp_mp;
580f4b3ec61Sdh155122 	ip_stack_t	*ipst;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
5837c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
584f4b3ec61Sdh155122 	ipst = CONNQ_TO_IPST(q);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
5887c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
5897c478bd9Sstevel@tonic-gate 	 */
590f4b3ec61Sdh155122 	if (ioc_cr != NULL && secpolicy_ip_config(ioc_cr, B_FALSE) != 0)
5917c478bd9Sstevel@tonic-gate 		return (EPERM);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	ipid = (ipid_t *)mp->b_rptr;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/* Only actions on IRE_CACHEs are acceptable at present. */
5967c478bd9Sstevel@tonic-gate 	if (ipid->ipid_ire_type != IRE_CACHE)
5977c478bd9Sstevel@tonic-gate 		return (EINVAL);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
6007c478bd9Sstevel@tonic-gate 		ipid->ipid_addr_length);
6017c478bd9Sstevel@tonic-gate 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
6027c478bd9Sstevel@tonic-gate 		return (EINVAL);
6037c478bd9Sstevel@tonic-gate 	switch (ipid->ipid_addr_length) {
6047c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN:
6057c478bd9Sstevel@tonic-gate 		/* addr_ucp points at IP addr */
6067c478bd9Sstevel@tonic-gate 		break;
6077c478bd9Sstevel@tonic-gate 	case sizeof (sin_t): {
6087c478bd9Sstevel@tonic-gate 		sin_t	*sin;
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * got complete (sockaddr) address - increment addr_ucp to point
6117c478bd9Sstevel@tonic-gate 		 * at the ip_addr field.
6127c478bd9Sstevel@tonic-gate 		 */
6137c478bd9Sstevel@tonic-gate 		sin = (sin_t *)addr_ucp;
6147c478bd9Sstevel@tonic-gate 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	default:
6187c478bd9Sstevel@tonic-gate 		return (EINVAL);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	/* Extract the destination address. */
6217c478bd9Sstevel@tonic-gate 	bcopy(addr_ucp, &addr, IP_ADDR_LEN);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/* Try to find the CACHED IRE. */
624f4b3ec61Sdh155122 	ire = ire_cache_lookup(addr, zoneid, NULL, ipst);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/* Nail it. */
6277c478bd9Sstevel@tonic-gate 	if (ire) {
6287c478bd9Sstevel@tonic-gate 		/* Allow delete only on CACHE entries */
6297c478bd9Sstevel@tonic-gate 		if (ire->ire_type != IRE_CACHE) {
6307c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
6317c478bd9Sstevel@tonic-gate 			return (EINVAL);
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		/*
6357c478bd9Sstevel@tonic-gate 		 * Verify that the IRE has been around for a while.
6367c478bd9Sstevel@tonic-gate 		 * This is to protect against transport protocols
6377c478bd9Sstevel@tonic-gate 		 * that are too eager in sending delete messages.
6387c478bd9Sstevel@tonic-gate 		 */
6397c478bd9Sstevel@tonic-gate 		if (gethrestime_sec() <
640f4b3ec61Sdh155122 		    ire->ire_create_time + ipst->ips_ip_ignore_delete_time) {
6417c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
6427c478bd9Sstevel@tonic-gate 			return (EINVAL);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 		/*
6457c478bd9Sstevel@tonic-gate 		 * Now we have a potentially dead cache entry. We need
6467c478bd9Sstevel@tonic-gate 		 * to remove it.
647c793af95Ssangeeta 		 * If this cache entry is generated from a
648c793af95Ssangeeta 		 * default route (i.e., ire_cmask == 0),
6497c478bd9Sstevel@tonic-gate 		 * search the default list and mark it dead and some
6507c478bd9Sstevel@tonic-gate 		 * background process will try to activate it.
6517c478bd9Sstevel@tonic-gate 		 */
6527c478bd9Sstevel@tonic-gate 		if ((ire->ire_gateway_addr != 0) && (ire->ire_cmask == 0)) {
6537c478bd9Sstevel@tonic-gate 			/*
6547c478bd9Sstevel@tonic-gate 			 * Make sure that we pick a different
6557c478bd9Sstevel@tonic-gate 			 * IRE_DEFAULT next time.
6567c478bd9Sstevel@tonic-gate 			 */
6577c478bd9Sstevel@tonic-gate 			ire_t *gw_ire;
658c793af95Ssangeeta 			irb_t *irb = NULL;
659c793af95Ssangeeta 			uint_t match_flags;
6607c478bd9Sstevel@tonic-gate 
661c793af95Ssangeeta 			match_flags = (MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE);
662c793af95Ssangeeta 
663c793af95Ssangeeta 			gire = ire_ftable_lookup(ire->ire_addr,
664c793af95Ssangeeta 			    ire->ire_cmask, 0, 0,
665f4b3ec61Sdh155122 			    ire->ire_ipif, NULL, zoneid, 0, NULL, match_flags,
666f4b3ec61Sdh155122 			    ipst);
667c793af95Ssangeeta 
668c793af95Ssangeeta 			ip3dbg(("ire_ftable_lookup() returned gire %p\n",
669c793af95Ssangeeta 			    (void *)gire));
670c793af95Ssangeeta 
671c793af95Ssangeeta 			if (gire != NULL) {
672c793af95Ssangeeta 				irb = gire->ire_bucket;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 				/*
6757c478bd9Sstevel@tonic-gate 				 * We grab it as writer just to serialize
6767c478bd9Sstevel@tonic-gate 				 * multiple threads trying to bump up
677c793af95Ssangeeta 				 * irb_rr_origin
6787c478bd9Sstevel@tonic-gate 				 */
6797c478bd9Sstevel@tonic-gate 				rw_enter(&irb->irb_lock, RW_WRITER);
680c793af95Ssangeeta 				if ((gw_ire = irb->irb_rr_origin) == NULL) {
6817c478bd9Sstevel@tonic-gate 					rw_exit(&irb->irb_lock);
6827c478bd9Sstevel@tonic-gate 					goto done;
6837c478bd9Sstevel@tonic-gate 				}
684c793af95Ssangeeta 
6850ad1ccddSsowmini 				DTRACE_PROBE1(ip__ire__del__origin,
6860ad1ccddSsowmini 				    (ire_t *), gw_ire);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 				/* Skip past the potentially bad gateway */
6897c478bd9Sstevel@tonic-gate 				if (ire->ire_gateway_addr ==
6900ad1ccddSsowmini 				    gw_ire->ire_gateway_addr) {
6910ad1ccddSsowmini 					ire_t *next = gw_ire->ire_next;
6927c478bd9Sstevel@tonic-gate 
6930ad1ccddSsowmini 					DTRACE_PROBE2(ip__ire__del,
6940ad1ccddSsowmini 					    (ire_t *), gw_ire, (irb_t *), irb);
6950ad1ccddSsowmini 					IRE_FIND_NEXT_ORIGIN(next);
6960ad1ccddSsowmini 					irb->irb_rr_origin = next;
6970ad1ccddSsowmini 				}
6987c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate done:
702c793af95Ssangeeta 		if (gire != NULL)
703c793af95Ssangeeta 			IRE_REFRELE(gire);
7047c478bd9Sstevel@tonic-gate 		/* report the bad route to routing sockets */
7057c478bd9Sstevel@tonic-gate 		ip_rts_change(RTM_LOSING, ire->ire_addr, ire->ire_gateway_addr,
7067c478bd9Sstevel@tonic-gate 		    ire->ire_mask, ire->ire_src_addr, 0, 0, 0,
707f4b3ec61Sdh155122 		    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA), ipst);
7087c478bd9Sstevel@tonic-gate 		routing_sock_info = B_TRUE;
709dc041e83Scarlsonj 
710dc041e83Scarlsonj 		/*
711dc041e83Scarlsonj 		 * TCP is really telling us to start over completely, and it
712dc041e83Scarlsonj 		 * expects that we'll resend the ARP query.  Tell ARP to
713dc041e83Scarlsonj 		 * discard the entry, if this is a local destination.
714dc041e83Scarlsonj 		 */
715dc041e83Scarlsonj 		ill = ire->ire_stq->q_ptr;
716dc041e83Scarlsonj 		if (ire->ire_gateway_addr == 0 &&
717dc041e83Scarlsonj 		    (arp_mp = ill_ared_alloc(ill, addr)) != NULL) {
718dc041e83Scarlsonj 			putnext(ill->ill_rq, arp_mp);
719dc041e83Scarlsonj 		}
720dc041e83Scarlsonj 
7217c478bd9Sstevel@tonic-gate 		ire_delete(ire);
7227c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
7237c478bd9Sstevel@tonic-gate 	}
7246bdb8e66Sdd193516 	/*
7256bdb8e66Sdd193516 	 * Also look for an IRE_HOST type redirect ire and
7266bdb8e66Sdd193516 	 * remove it if present.
7276bdb8e66Sdd193516 	 */
7286bdb8e66Sdd193516 	ire = ire_route_lookup(addr, 0, 0, IRE_HOST, NULL, NULL,
729f4b3ec61Sdh155122 	    ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/* Nail it. */
7326bdb8e66Sdd193516 	if (ire != NULL) {
7336bdb8e66Sdd193516 	    if (ire->ire_flags & RTF_DYNAMIC) {
7347c478bd9Sstevel@tonic-gate 		if (!routing_sock_info) {
7357c478bd9Sstevel@tonic-gate 			ip_rts_change(RTM_LOSING, ire->ire_addr,
7367c478bd9Sstevel@tonic-gate 			    ire->ire_gateway_addr, ire->ire_mask,
7377c478bd9Sstevel@tonic-gate 			    ire->ire_src_addr, 0, 0, 0,
738f4b3ec61Sdh155122 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA),
739f4b3ec61Sdh155122 			    ipst);
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 		ire_delete(ire);
7426bdb8e66Sdd193516 	    }
7437c478bd9Sstevel@tonic-gate 	    ire_refrele(ire);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	return (0);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * Named Dispatch routine to produce a formatted report on all IREs.
7507c478bd9Sstevel@tonic-gate  * This report is accessed by using the ndd utility to "get" ND variable
7517c478bd9Sstevel@tonic-gate  * "ipv4_ire_status".
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate /* ARGSUSED */
7547c478bd9Sstevel@tonic-gate int
7557c478bd9Sstevel@tonic-gate ip_ire_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
758f4b3ec61Sdh155122 	ip_stack_t	*ipst;
759f4b3ec61Sdh155122 
760f4b3ec61Sdh155122 	if (CONN_Q(q))
761f4b3ec61Sdh155122 		ipst = CONNQ_TO_IPST(q);
762f4b3ec61Sdh155122 	else
763f4b3ec61Sdh155122 		ipst = ILLQ_TO_IPST(q);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
7667c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
7677c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7687c478bd9Sstevel@tonic-gate 	    "rfq      " MI_COL_HDRPAD_STR
7697c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7707c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
7717c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7727c478bd9Sstevel@tonic-gate 	    " zone "
7737c478bd9Sstevel@tonic-gate 	/*   12345 */
7747c478bd9Sstevel@tonic-gate 	    "addr            mask            "
7757c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 */
7767c478bd9Sstevel@tonic-gate 	    "src             gateway         mxfrg rtt   rtt_sd ssthresh ref "
7777c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 12345 12345 123456 12345678 123 */
7787c478bd9Sstevel@tonic-gate 	    "rtomax tstamp_ok wscale_ok ecn_ok pmtud_ok sack sendpipe "
7797c478bd9Sstevel@tonic-gate 	/*   123456 123456789 123456789 123456 12345678 1234 12345678 */
7807c478bd9Sstevel@tonic-gate 	    "recvpipe in/out/forward type");
7817c478bd9Sstevel@tonic-gate 	/*   12345678 in/out/forward xxxxxxxxxx */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * Because of the ndd constraint, at most we can have 64K buffer
7857c478bd9Sstevel@tonic-gate 	 * to put in all IRE info.  So to be more efficient, just
7867c478bd9Sstevel@tonic-gate 	 * allocate a 64K buffer here, assuming we need that large buffer.
7877c478bd9Sstevel@tonic-gate 	 * This should be OK as only root can do ndd /dev/ip.
7887c478bd9Sstevel@tonic-gate 	 */
7897c478bd9Sstevel@tonic-gate 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
7907c478bd9Sstevel@tonic-gate 		/* The following may work even if we cannot get a large buf. */
7917c478bd9Sstevel@tonic-gate 		(void) mi_mpprintf(mp, "<< Out of buffer >>\n");
7927c478bd9Sstevel@tonic-gate 		return (0);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7967c478bd9Sstevel@tonic-gate 	if (zoneid == GLOBAL_ZONEID)
7977c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
7987c478bd9Sstevel@tonic-gate 
799f4b3ec61Sdh155122 	ire_walk_v4(ire_report_ftable, mp->b_cont, zoneid, ipst);
800f4b3ec61Sdh155122 	ire_walk_v4(ire_report_ctable, mp->b_cont, zoneid, ipst);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	return (0);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /* ire_walk routine invoked for ip_ire_report for each cached IRE. */
8077c478bd9Sstevel@tonic-gate static void
8087c478bd9Sstevel@tonic-gate ire_report_ctable(ire_t *ire, char *mp)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	char	buf1[16];
8117c478bd9Sstevel@tonic-gate 	char	buf2[16];
8127c478bd9Sstevel@tonic-gate 	char	buf3[16];
8137c478bd9Sstevel@tonic-gate 	char	buf4[16];
8147c478bd9Sstevel@tonic-gate 	uint_t	fo_pkt_count;
8157c478bd9Sstevel@tonic-gate 	uint_t	ib_pkt_count;
8167c478bd9Sstevel@tonic-gate 	int	ref;
8177c478bd9Sstevel@tonic-gate 	uint_t	print_len, buf_len;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHETABLE) == 0)
8207c478bd9Sstevel@tonic-gate 	    return;
8217c478bd9Sstevel@tonic-gate 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
8227c478bd9Sstevel@tonic-gate 	if (buf_len <= 0)
8237c478bd9Sstevel@tonic-gate 		return;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
8267c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
8277c478bd9Sstevel@tonic-gate 	/* "inbound" to a non local address is a forward */
8287c478bd9Sstevel@tonic-gate 	ib_pkt_count = ire->ire_ib_pkt_count;
8297c478bd9Sstevel@tonic-gate 	fo_pkt_count = 0;
8307c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
8317c478bd9Sstevel@tonic-gate 		fo_pkt_count = ib_pkt_count;
8327c478bd9Sstevel@tonic-gate 		ib_pkt_count = 0;
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 	print_len =  snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
8357c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
8367c478bd9Sstevel@tonic-gate 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
8377c478bd9Sstevel@tonic-gate 	    "%04d %08d %08d %d/%d/%d %s\n",
8387c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
8397c478bd9Sstevel@tonic-gate 	    (int)ire->ire_zoneid,
8407c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
8417c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_src_addr, buf3),
8427c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
8437c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
8447c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtt_sd, ire->ire_uinfo.iulp_ssthresh, ref,
8457c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtomax,
8467c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
8477c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
8487c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
8497c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
8507c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_sack,
8517c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
8527c478bd9Sstevel@tonic-gate 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
8537c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
8547c478bd9Sstevel@tonic-gate 	if (print_len < buf_len) {
8557c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += print_len;
8567c478bd9Sstevel@tonic-gate 	} else {
8577c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += buf_len;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /* ARGSUSED */
8627c478bd9Sstevel@tonic-gate int
8637c478bd9Sstevel@tonic-gate ip_ire_report_mrtun(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
8647c478bd9Sstevel@tonic-gate {
865f4b3ec61Sdh155122 	ip_stack_t	*ipst;
866f4b3ec61Sdh155122 
867f4b3ec61Sdh155122 	if (CONN_Q(q))
868f4b3ec61Sdh155122 		ipst = CONNQ_TO_IPST(q);
869f4b3ec61Sdh155122 	else
870f4b3ec61Sdh155122 		ipst = ILLQ_TO_IPST(q);
871f4b3ec61Sdh155122 
8727c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
8737c478bd9Sstevel@tonic-gate 	"IRE      " MI_COL_HDRPAD_STR
8747c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
8757c478bd9Sstevel@tonic-gate 	"stq      " MI_COL_HDRPAD_STR
8767c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
8777c478bd9Sstevel@tonic-gate 	"in_ill    " MI_COL_HDRPAD_STR
8787c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
8797c478bd9Sstevel@tonic-gate 	"in_src_addr            "
8807c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 */
8817c478bd9Sstevel@tonic-gate 	"max_frag      "
8827c478bd9Sstevel@tonic-gate 	/*   12345 */
8837c478bd9Sstevel@tonic-gate 	"ref     ");
8847c478bd9Sstevel@tonic-gate 	/*   123 */
8857c478bd9Sstevel@tonic-gate 
886f4b3ec61Sdh155122 	ire_walk_ill_mrtun(0, 0, ire_report_mrtun_table, mp, NULL,
887f4b3ec61Sdh155122 	    ipst);
8887c478bd9Sstevel@tonic-gate 	return (0);
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /* mrtun report table - supports ipv4_mrtun_ire_status ndd variable */
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate static void
8947c478bd9Sstevel@tonic-gate ire_report_mrtun_table(ire_t *ire, char *mp)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	char	buf1[INET_ADDRSTRLEN];
8977c478bd9Sstevel@tonic-gate 	int	ref;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
9007c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
9017c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type == IRE_MIPRTUN);
9027c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf((mblk_t *)mp,
9037c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
9047c478bd9Sstevel@tonic-gate 	    "%s          %05d             %03d",
9057c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_stq,
9067c478bd9Sstevel@tonic-gate 	    (void *)ire->ire_in_ill,
9077c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_in_src_addr, buf1),
9087c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ref);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate /*
9127c478bd9Sstevel@tonic-gate  * Dispatch routine to format ires in interface based routine
9137c478bd9Sstevel@tonic-gate  */
9147c478bd9Sstevel@tonic-gate /* ARGSUSED */
9157c478bd9Sstevel@tonic-gate int
9167c478bd9Sstevel@tonic-gate ip_ire_report_srcif(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
9177c478bd9Sstevel@tonic-gate {
918f4b3ec61Sdh155122 	ip_stack_t	*ipst;
919f4b3ec61Sdh155122 
920f4b3ec61Sdh155122 	if (CONN_Q(q))
921f4b3ec61Sdh155122 		ipst = CONNQ_TO_IPST(q);
922f4b3ec61Sdh155122 	else
923f4b3ec61Sdh155122 		ipst = ILLQ_TO_IPST(q);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/* Report all interface based ires */
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
9287c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
9297c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
9307c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
9317c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
9327c478bd9Sstevel@tonic-gate 	    "in_ill    " MI_COL_HDRPAD_STR
9337c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
9347c478bd9Sstevel@tonic-gate 	    "addr            "
9357c478bd9Sstevel@tonic-gate 	    /*   123.123.123.123 */
9367c478bd9Sstevel@tonic-gate 	    "gateway         "
9377c478bd9Sstevel@tonic-gate 	    /*   123.123.123.123 */
9387c478bd9Sstevel@tonic-gate 	    "max_frag      "
9397c478bd9Sstevel@tonic-gate 	    /*   12345 */
9407c478bd9Sstevel@tonic-gate 	    "ref     "
9417c478bd9Sstevel@tonic-gate 	    /*   123 */
9427c478bd9Sstevel@tonic-gate 	    "type    "
9437c478bd9Sstevel@tonic-gate 	    /* ABCDEFGH */
9447c478bd9Sstevel@tonic-gate 	    "in/out/forward");
945f4b3ec61Sdh155122 	ire_walk_srcif_table_v4(ire_report_srcif_table, mp, ipst);
9467c478bd9Sstevel@tonic-gate 	return (0);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /* Reports the interface table ires */
950f4b3ec61Sdh155122 /* ARGSUSED2 */
9517c478bd9Sstevel@tonic-gate static void
952f4b3ec61Sdh155122 ire_report_srcif_table(ire_t *ire, char *mp, ip_stack_t *ipst)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	char    buf1[INET_ADDRSTRLEN];
9557c478bd9Sstevel@tonic-gate 	char    buf2[INET_ADDRSTRLEN];
9567c478bd9Sstevel@tonic-gate 	int	ref;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
9597c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf((mblk_t *)mp,
9607c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
9617c478bd9Sstevel@tonic-gate 	    "%s    %s      %05d       %03d      %s     %d",
9627c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_stq,
9637c478bd9Sstevel@tonic-gate 	    (void *)ire->ire_in_ill,
9647c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1),
9657c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf2),
9667c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ref,
9677c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type),
9687c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate  * ip_ire_req is called by ip_wput when an IRE_DB_REQ_TYPE message is handed
9737c478bd9Sstevel@tonic-gate  * down from the Upper Level Protocol to request a copy of the IRE (to check
9747c478bd9Sstevel@tonic-gate  * its type or to extract information like round-trip time estimates or the
9757c478bd9Sstevel@tonic-gate  * MTU.)
9767c478bd9Sstevel@tonic-gate  * The address is assumed to be in the ire_addr field. If no IRE is found
9777c478bd9Sstevel@tonic-gate  * an IRE is returned with ire_type being zero.
9787c478bd9Sstevel@tonic-gate  * Note that the upper lavel protocol has to check for broadcast
9797c478bd9Sstevel@tonic-gate  * (IRE_BROADCAST) and multicast (CLASSD(addr)).
9807c478bd9Sstevel@tonic-gate  * If there is a b_cont the resulting IRE_DB_TYPE mblk is placed at the
9817c478bd9Sstevel@tonic-gate  * end of the returned message.
9827c478bd9Sstevel@tonic-gate  *
9837c478bd9Sstevel@tonic-gate  * TCP sends down a message of this type with a connection request packet
9847c478bd9Sstevel@tonic-gate  * chained on. UDP and ICMP send it down to verify that a route exists for
9857c478bd9Sstevel@tonic-gate  * the destination address when they get connected.
9867c478bd9Sstevel@tonic-gate  */
9877c478bd9Sstevel@tonic-gate void
9887c478bd9Sstevel@tonic-gate ip_ire_req(queue_t *q, mblk_t *mp)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	ire_t	*inire;
9917c478bd9Sstevel@tonic-gate 	ire_t	*ire;
9927c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
9937c478bd9Sstevel@tonic-gate 	ire_t	*sire = NULL;
9947c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid;
995f4b3ec61Sdh155122 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
996f4b3ec61Sdh155122 
997f4b3ec61Sdh155122 	ASSERT(q->q_next == NULL);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (ire_t) ||
10007c478bd9Sstevel@tonic-gate 	    !OK_32PTR(mp->b_rptr)) {
10017c478bd9Sstevel@tonic-gate 		freemsg(mp);
10027c478bd9Sstevel@tonic-gate 		return;
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 	inire = (ire_t *)mp->b_rptr;
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Got it, now take our best shot at an IRE.
10077c478bd9Sstevel@tonic-gate 	 */
10087c478bd9Sstevel@tonic-gate 	if (inire->ire_ipversion == IPV6_VERSION) {
10097c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup_v6(&inire->ire_addr_v6, 0, 0, 0,
101045916cd2Sjpk 		    NULL, &sire, zoneid, NULL,
1011f4b3ec61Sdh155122 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst);
10127c478bd9Sstevel@tonic-gate 	} else {
10137c478bd9Sstevel@tonic-gate 		ASSERT(inire->ire_ipversion == IPV4_VERSION);
10147c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(inire->ire_addr, 0, 0, 0,
101545916cd2Sjpk 		    NULL, &sire, zoneid, NULL,
1016f4b3ec61Sdh155122 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT), ipst);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/*
10207c478bd9Sstevel@tonic-gate 	 * We prevent returning IRES with source address INADDR_ANY
10217c478bd9Sstevel@tonic-gate 	 * as these were temporarily created for sending packets
10227c478bd9Sstevel@tonic-gate 	 * from endpoints that have conn_unspec_src set.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	if (ire == NULL ||
10257c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV4_VERSION &&
10267c478bd9Sstevel@tonic-gate 	    ire->ire_src_addr == INADDR_ANY) ||
10277c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV6_VERSION &&
10287c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6))) {
10297c478bd9Sstevel@tonic-gate 		inire->ire_type = 0;
10307c478bd9Sstevel@tonic-gate 	} else {
10317c478bd9Sstevel@tonic-gate 		bcopy(ire, inire, sizeof (ire_t));
10327c478bd9Sstevel@tonic-gate 		/* Copy the route metrics from the parent. */
10337c478bd9Sstevel@tonic-gate 		if (sire != NULL) {
10347c478bd9Sstevel@tonic-gate 			bcopy(&(sire->ire_uinfo), &(inire->ire_uinfo),
10357c478bd9Sstevel@tonic-gate 			    sizeof (iulp_t));
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		/*
10397c478bd9Sstevel@tonic-gate 		 * As we don't lookup global policy here, we may not
10407c478bd9Sstevel@tonic-gate 		 * pass the right size if per-socket policy is not
10417c478bd9Sstevel@tonic-gate 		 * present. For these cases, path mtu discovery will
10427c478bd9Sstevel@tonic-gate 		 * do the right thing.
10437c478bd9Sstevel@tonic-gate 		 */
10447c478bd9Sstevel@tonic-gate 		inire->ire_ipsec_overhead = conn_ipsec_length(Q_TO_CONN(q));
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 		/* Pass the latest setting of the ip_path_mtu_discovery */
1047f4b3ec61Sdh155122 		inire->ire_frag_flag |=
1048f4b3ec61Sdh155122 		    (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0;
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	if (ire != NULL)
10517c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
10527c478bd9Sstevel@tonic-gate 	if (sire != NULL)
10537c478bd9Sstevel@tonic-gate 		ire_refrele(sire);
10547c478bd9Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[sizeof (ire_t)];
10557c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/* Put the IRE_DB_TYPE mblk last in the chain */
10587c478bd9Sstevel@tonic-gate 	mp1 = mp->b_cont;
10597c478bd9Sstevel@tonic-gate 	if (mp1 != NULL) {
10607c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
10617c478bd9Sstevel@tonic-gate 		linkb(mp1, mp);
10627c478bd9Sstevel@tonic-gate 		mp = mp1;
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 	qreply(q, mp);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*
10687c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
10697c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
10707c478bd9Sstevel@tonic-gate  * the packet has been sent.
10717c478bd9Sstevel@tonic-gate  */
10727c478bd9Sstevel@tonic-gate static void
10737c478bd9Sstevel@tonic-gate ire_send(queue_t *q, mblk_t *pkt, ire_t *ire)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
10767c478bd9Sstevel@tonic-gate 	boolean_t is_secure;
10777c478bd9Sstevel@tonic-gate 	uint_t ifindex;
10787c478bd9Sstevel@tonic-gate 	ill_t	*ill;
10795597b60aSnordmark 	zoneid_t zoneid = ire->ire_zoneid;
1080f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
10835597b60aSnordmark 	ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */
10847c478bd9Sstevel@tonic-gate 	ipsec_mp = pkt;
10857c478bd9Sstevel@tonic-gate 	is_secure = (pkt->b_datap->db_type == M_CTL);
10865597b60aSnordmark 	if (is_secure) {
10875597b60aSnordmark 		ipsec_out_t *io;
10885597b60aSnordmark 
10897c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
10905597b60aSnordmark 		io = (ipsec_out_t *)ipsec_mp->b_rptr;
10915597b60aSnordmark 		if (io->ipsec_out_type == IPSEC_OUT)
10925597b60aSnordmark 			zoneid = io->ipsec_out_zoneid;
10935597b60aSnordmark 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
10967c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
10977c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
10987c478bd9Sstevel@tonic-gate 		/*
10997c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_noire).
11007c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
11017c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from ARP)
11027c478bd9Sstevel@tonic-gate 		 */
11037c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
11047c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
1105f4b3ec61Sdh155122 		    NULL, NULL, NULL, NULL, ipst);
11067c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
11077c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
11087c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
11097c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
11107c478bd9Sstevel@tonic-gate 			return;
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
11137c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
11147c478bd9Sstevel@tonic-gate 		/*
11157c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
11167c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
11177c478bd9Sstevel@tonic-gate 		 * prepended.
11187c478bd9Sstevel@tonic-gate 		 */
11197c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
1120c793af95Ssangeeta 		put(q, pkt);
11217c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
11227c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
11237c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
11247c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
11257c478bd9Sstevel@tonic-gate 		/*
11267c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
11277c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
11287c478bd9Sstevel@tonic-gate 		 */
11297c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
11307c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, ipsec_mp, NULL);
11317c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
11327c478bd9Sstevel@tonic-gate 	} else {
11337c478bd9Sstevel@tonic-gate 		/* Locally originated packets */
11347c478bd9Sstevel@tonic-gate 		boolean_t is_inaddr_any;
11357c478bd9Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)pkt->b_rptr;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 		/*
11387c478bd9Sstevel@tonic-gate 		 * We need to do an ire_delete below for which
11397c478bd9Sstevel@tonic-gate 		 * we need to make sure that the IRE will be
11407c478bd9Sstevel@tonic-gate 		 * around even after calling ip_wput_ire -
11417c478bd9Sstevel@tonic-gate 		 * which does ire_refrele. Otherwise somebody
11427c478bd9Sstevel@tonic-gate 		 * could potentially delete this ire and hence
11437c478bd9Sstevel@tonic-gate 		 * free this ire and we will be calling ire_delete
11447c478bd9Sstevel@tonic-gate 		 * on a freed ire below.
11457c478bd9Sstevel@tonic-gate 		 */
11467c478bd9Sstevel@tonic-gate 		is_inaddr_any = (ire->ire_src_addr == INADDR_ANY);
11477c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
11487c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
11497c478bd9Sstevel@tonic-gate 		}
11507c478bd9Sstevel@tonic-gate 		/*
11517c478bd9Sstevel@tonic-gate 		 * If we were resolving a router we can not use the
11527c478bd9Sstevel@tonic-gate 		 * routers IRE for sending the packet (since it would
11537c478bd9Sstevel@tonic-gate 		 * violate the uniqness of the IP idents) thus we
11547c478bd9Sstevel@tonic-gate 		 * make another pass through ip_wput to create the IRE_CACHE
11557c478bd9Sstevel@tonic-gate 		 * for the destination.
11567c478bd9Sstevel@tonic-gate 		 * When IRE_MARK_NOADD is set, ire_add() is not called.
11577c478bd9Sstevel@tonic-gate 		 * Thus ip_wput() will never find a ire and result in an
11587c478bd9Sstevel@tonic-gate 		 * infinite loop. Thus we check whether IRE_MARK_NOADD is
11597c478bd9Sstevel@tonic-gate 		 * is set. This also implies that IRE_MARK_NOADD can only be
11607c478bd9Sstevel@tonic-gate 		 * used to send packets to directly connected hosts.
11617c478bd9Sstevel@tonic-gate 		 */
11627c478bd9Sstevel@tonic-gate 		if (ipha->ipha_dst != ire->ire_addr &&
11637c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_NOADD)) {
11647c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
11655597b60aSnordmark 			if (CONN_Q(q)) {
11665597b60aSnordmark 				(void) ip_output(Q_TO_CONN(q), ipsec_mp, q,
11675597b60aSnordmark 				    IRE_SEND);
11685597b60aSnordmark 			} else {
11695597b60aSnordmark 				(void) ip_output((void *)(uintptr_t)zoneid,
11705597b60aSnordmark 				    ipsec_mp, q, IRE_SEND);
11715597b60aSnordmark 			}
11727c478bd9Sstevel@tonic-gate 		} else {
11737c478bd9Sstevel@tonic-gate 			if (is_secure) {
11747c478bd9Sstevel@tonic-gate 				ipsec_out_t *oi;
11757c478bd9Sstevel@tonic-gate 				ipha_t *ipha;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 				oi = (ipsec_out_t *)ipsec_mp->b_rptr;
11787c478bd9Sstevel@tonic-gate 				ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
11797c478bd9Sstevel@tonic-gate 				if (oi->ipsec_out_proc_begin) {
11807c478bd9Sstevel@tonic-gate 					/*
11817c478bd9Sstevel@tonic-gate 					 * This is the case where
11827c478bd9Sstevel@tonic-gate 					 * ip_wput_ipsec_out could not find
11837c478bd9Sstevel@tonic-gate 					 * the IRE and recreated a new one.
11847c478bd9Sstevel@tonic-gate 					 * As ip_wput_ipsec_out does ire
11857c478bd9Sstevel@tonic-gate 					 * lookups, ire_refrele for the extra
11867c478bd9Sstevel@tonic-gate 					 * bump in ire_add.
11877c478bd9Sstevel@tonic-gate 					 */
11887c478bd9Sstevel@tonic-gate 					ire_refrele(ire);
11897c478bd9Sstevel@tonic-gate 					ip_wput_ipsec_out(q, ipsec_mp, ipha,
11907c478bd9Sstevel@tonic-gate 					    NULL, NULL);
11917c478bd9Sstevel@tonic-gate 				} else {
11927c478bd9Sstevel@tonic-gate 					/*
11937c478bd9Sstevel@tonic-gate 					 * IRE_REFRELE will be done in
11947c478bd9Sstevel@tonic-gate 					 * ip_wput_ire.
11957c478bd9Sstevel@tonic-gate 					 */
11967c478bd9Sstevel@tonic-gate 					ip_wput_ire(q, ipsec_mp, ire, NULL,
11975597b60aSnordmark 					    IRE_SEND, zoneid);
11987c478bd9Sstevel@tonic-gate 				}
11997c478bd9Sstevel@tonic-gate 			} else {
12007c478bd9Sstevel@tonic-gate 				/*
12017c478bd9Sstevel@tonic-gate 				 * IRE_REFRELE will be done in ip_wput_ire.
12027c478bd9Sstevel@tonic-gate 				 */
12037c478bd9Sstevel@tonic-gate 				ip_wput_ire(q, ipsec_mp, ire, NULL,
12045597b60aSnordmark 				    IRE_SEND, zoneid);
12057c478bd9Sstevel@tonic-gate 			}
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 		/*
12087c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
12097c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
12107c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
12117c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
12127c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
12137c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
12147c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
12157c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append, ip_ire_req and ip_bind_connected.
12167c478bd9Sstevel@tonic-gate 		 * Though, this does not completely prevent other threads
12177c478bd9Sstevel@tonic-gate 		 * from using this ire, this should not cause any problems.
12187c478bd9Sstevel@tonic-gate 		 *
12197c478bd9Sstevel@tonic-gate 		 * NOTE : We use is_inaddr_any instead of using ire_src_addr
12207c478bd9Sstevel@tonic-gate 		 * because for the normal case i.e !is_inaddr_any, ire_refrele
12217c478bd9Sstevel@tonic-gate 		 * above could have potentially freed the ire.
12227c478bd9Sstevel@tonic-gate 		 */
12237c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
12247c478bd9Sstevel@tonic-gate 			/*
12257c478bd9Sstevel@tonic-gate 			 * If this IRE has been deleted by another thread, then
12267c478bd9Sstevel@tonic-gate 			 * ire_bucket won't be NULL, but ire_ptpn will be NULL.
12277c478bd9Sstevel@tonic-gate 			 * Thus, ire_delete will do nothing.  This check
12287c478bd9Sstevel@tonic-gate 			 * guards against calling ire_delete when the IRE was
12297c478bd9Sstevel@tonic-gate 			 * never inserted in the table, which is handled by
12307c478bd9Sstevel@tonic-gate 			 * ire_delete as dropping another reference.
12317c478bd9Sstevel@tonic-gate 			 */
12327c478bd9Sstevel@tonic-gate 			if (ire->ire_bucket != NULL) {
12337c478bd9Sstevel@tonic-gate 				ip1dbg(("ire_send: delete IRE\n"));
12347c478bd9Sstevel@tonic-gate 				ire_delete(ire);
12357c478bd9Sstevel@tonic-gate 			}
12367c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held above */
12377c478bd9Sstevel@tonic-gate 		}
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate /*
12427c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
12437c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
12447c478bd9Sstevel@tonic-gate  * the packet has been sent.
12457c478bd9Sstevel@tonic-gate  */
12467c478bd9Sstevel@tonic-gate static void
12477c478bd9Sstevel@tonic-gate ire_send_v6(queue_t *q, mblk_t *pkt, ire_t *ire)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
12507c478bd9Sstevel@tonic-gate 	boolean_t secure;
12517c478bd9Sstevel@tonic-gate 	uint_t ifindex;
12525597b60aSnordmark 	zoneid_t zoneid = ire->ire_zoneid;
1253f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
12565597b60aSnordmark 	ASSERT(!(ire->ire_type & IRE_LOCAL)); /* Has different ire_zoneid */
12577c478bd9Sstevel@tonic-gate 	if (pkt->b_datap->db_type == M_CTL) {
12585597b60aSnordmark 		ipsec_out_t *io;
12595597b60aSnordmark 
12607c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
12617c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
12627c478bd9Sstevel@tonic-gate 		secure = B_TRUE;
12635597b60aSnordmark 		io = (ipsec_out_t *)ipsec_mp->b_rptr;
12645597b60aSnordmark 		if (io->ipsec_out_type == IPSEC_OUT)
12655597b60aSnordmark 			zoneid = io->ipsec_out_zoneid;
12667c478bd9Sstevel@tonic-gate 	} else {
12677c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
12687c478bd9Sstevel@tonic-gate 		secure = B_FALSE;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
12727c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
12737c478bd9Sstevel@tonic-gate 		ill_t	*ill;
12747c478bd9Sstevel@tonic-gate 		/*
12757c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_data_v6).
12767c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
12777c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from the
1278c793af95Ssangeeta 		 * resolver).
12797c478bd9Sstevel@tonic-gate 		 */
12807c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
12817c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_TRUE,
1282f4b3ec61Sdh155122 		    NULL, NULL, NULL, NULL, ipst);
12837c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
12847c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
12857c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
12867c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
12877c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
12887c478bd9Sstevel@tonic-gate 			return;
12897c478bd9Sstevel@tonic-gate 		}
12907c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
12917c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
12927c478bd9Sstevel@tonic-gate 		/*
12937c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
12947c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
12957c478bd9Sstevel@tonic-gate 		 * prepended.
12967c478bd9Sstevel@tonic-gate 		 */
12977c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
12987c478bd9Sstevel@tonic-gate 		put(q, pkt);
12997c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
13007c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
13017c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
13027c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
13037c478bd9Sstevel@tonic-gate 		/*
13047c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
13057c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
13067c478bd9Sstevel@tonic-gate 		 */
13077c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
13087c478bd9Sstevel@tonic-gate 		/*
13097c478bd9Sstevel@tonic-gate 		 * XXX TODO IPv6.
13107c478bd9Sstevel@tonic-gate 		 */
13117c478bd9Sstevel@tonic-gate 		freemsg(pkt);
13127c478bd9Sstevel@tonic-gate #ifdef XXX
13137c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, pkt, NULL);
13147c478bd9Sstevel@tonic-gate #endif
13157c478bd9Sstevel@tonic-gate 	} else {
13167c478bd9Sstevel@tonic-gate 		if (secure) {
13177c478bd9Sstevel@tonic-gate 			ipsec_out_t *oi;
13187c478bd9Sstevel@tonic-gate 			ip6_t *ip6h;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 			oi = (ipsec_out_t *)ipsec_mp->b_rptr;
13217c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)ipsec_mp->b_cont->b_rptr;
13227c478bd9Sstevel@tonic-gate 			if (oi->ipsec_out_proc_begin) {
13237c478bd9Sstevel@tonic-gate 				/*
13247c478bd9Sstevel@tonic-gate 				 * This is the case where
13257c478bd9Sstevel@tonic-gate 				 * ip_wput_ipsec_out could not find
13267c478bd9Sstevel@tonic-gate 				 * the IRE and recreated a new one.
13277c478bd9Sstevel@tonic-gate 				 */
13287c478bd9Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h,
13297c478bd9Sstevel@tonic-gate 				    NULL, NULL);
13307c478bd9Sstevel@tonic-gate 			} else {
13315597b60aSnordmark 				if (CONN_Q(q)) {
13325597b60aSnordmark 					(void) ip_output_v6(Q_TO_CONN(q),
13335597b60aSnordmark 					    ipsec_mp, q, IRE_SEND);
13345597b60aSnordmark 				} else {
13355597b60aSnordmark 					(void) ip_output_v6(
13365597b60aSnordmark 					    (void *)(uintptr_t)zoneid,
13375597b60aSnordmark 					    ipsec_mp, q, IRE_SEND);
13385597b60aSnordmark 				}
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 		} else {
13417c478bd9Sstevel@tonic-gate 			/*
13427c478bd9Sstevel@tonic-gate 			 * Send packets through ip_output_v6 so that any
13437c478bd9Sstevel@tonic-gate 			 * ip6_info header can be processed again.
13447c478bd9Sstevel@tonic-gate 			 */
13455597b60aSnordmark 			if (CONN_Q(q)) {
13467c478bd9Sstevel@tonic-gate 				(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp, q,
13477c478bd9Sstevel@tonic-gate 				    IRE_SEND);
13485597b60aSnordmark 			} else {
13495597b60aSnordmark 				(void) ip_output_v6((void *)(uintptr_t)zoneid,
13505597b60aSnordmark 				    ipsec_mp, q, IRE_SEND);
13515597b60aSnordmark 			}
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 		/*
13547c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
13557c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
13567c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
13577c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
13587c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
13597c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
13607c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
13617c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append_v6, ip_ire_req and
13627c478bd9Sstevel@tonic-gate 		 * ip_bind_connected_v6. Though, this does not completely
13637c478bd9Sstevel@tonic-gate 		 * prevent other threads from using this ire, this should
13647c478bd9Sstevel@tonic-gate 		 * not cause any problems.
13657c478bd9Sstevel@tonic-gate 		 */
13667c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6)) {
13677c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_send_v6: delete IRE\n"));
13687c478bd9Sstevel@tonic-gate 			ire_delete(ire);
13697c478bd9Sstevel@tonic-gate 		}
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 	ire_refrele(ire);	/* Held in ire_add */
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate /*
13757c478bd9Sstevel@tonic-gate  * Make sure that IRE bucket does not get too long.
13767c478bd9Sstevel@tonic-gate  * This can cause lock up because ire_cache_lookup()
13777c478bd9Sstevel@tonic-gate  * may take "forever" to finish.
13787c478bd9Sstevel@tonic-gate  *
13797c478bd9Sstevel@tonic-gate  * We just remove cnt IREs each time.  This means that
13807c478bd9Sstevel@tonic-gate  * the bucket length will stay approximately constant,
13817c478bd9Sstevel@tonic-gate  * depending on cnt.  This should be enough to defend
13827c478bd9Sstevel@tonic-gate  * against DoS attack based on creating temporary IREs
13837c478bd9Sstevel@tonic-gate  * (for forwarding and non-TCP traffic).
13847c478bd9Sstevel@tonic-gate  *
13857c478bd9Sstevel@tonic-gate  * Note that new IRE is normally added at the tail of the
13867c478bd9Sstevel@tonic-gate  * bucket.  This means that we are removing the "oldest"
13877c478bd9Sstevel@tonic-gate  * temporary IRE added.  Only if there are IREs with
13887c478bd9Sstevel@tonic-gate  * the same ire_addr, do we not add it at the tail.  Refer
13897c478bd9Sstevel@tonic-gate  * to ire_add_v*().  It should be OK for our purpose.
13907c478bd9Sstevel@tonic-gate  *
13917c478bd9Sstevel@tonic-gate  * For non-temporary cached IREs, we make sure that they
13927c478bd9Sstevel@tonic-gate  * have not been used for some time (defined below), they
13937c478bd9Sstevel@tonic-gate  * are non-local destinations, and there is no one using
13947c478bd9Sstevel@tonic-gate  * them at the moment (refcnt == 1).
13957c478bd9Sstevel@tonic-gate  *
13967c478bd9Sstevel@tonic-gate  * The above means that the IRE bucket length may become
13977c478bd9Sstevel@tonic-gate  * very long, consisting of mostly non-temporary IREs.
13987c478bd9Sstevel@tonic-gate  * This can happen when the hash function does a bad job
13997c478bd9Sstevel@tonic-gate  * so that most TCP connections cluster to a specific bucket.
14007c478bd9Sstevel@tonic-gate  * This "hopefully" should never happen.  It can also
14017c478bd9Sstevel@tonic-gate  * happen if most TCP connections have very long lives.
14027c478bd9Sstevel@tonic-gate  * Even with the minimal hash table size of 256, there
14037c478bd9Sstevel@tonic-gate  * has to be a lot of such connections to make the bucket
14047c478bd9Sstevel@tonic-gate  * length unreasonably long.  This should probably not
14057c478bd9Sstevel@tonic-gate  * happen either.  The third can when this can happen is
14067c478bd9Sstevel@tonic-gate  * when the machine is under attack, such as SYN flooding.
14077c478bd9Sstevel@tonic-gate  * TCP should already have the proper mechanism to protect
14087c478bd9Sstevel@tonic-gate  * that.  So we should be safe.
14097c478bd9Sstevel@tonic-gate  *
14107c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send() after
14117c478bd9Sstevel@tonic-gate  * a new IRE is added and the packet is sent.
14127c478bd9Sstevel@tonic-gate  *
14137c478bd9Sstevel@tonic-gate  * The idle cutoff interval is set to 60s.  It can be
14147c478bd9Sstevel@tonic-gate  * changed using /etc/system.
14157c478bd9Sstevel@tonic-gate  */
14167c478bd9Sstevel@tonic-gate uint32_t ire_idle_cutoff_interval = 60000;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate static void
14197c478bd9Sstevel@tonic-gate ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	ire_t *ire;
14227c478bd9Sstevel@tonic-gate 	int tmp_cnt = cnt;
14237c478bd9Sstevel@tonic-gate 	clock_t cut_off = drv_usectohz(ire_idle_cutoff_interval * 1000);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/*
14267c478bd9Sstevel@tonic-gate 	 * irb is NULL if the IRE is not added to the hash.  This
14277c478bd9Sstevel@tonic-gate 	 * happens when IRE_MARK_NOADD is set in ire_add_then_send()
14287c478bd9Sstevel@tonic-gate 	 * and when ires are returned from ire_update_srcif_v4() routine.
14297c478bd9Sstevel@tonic-gate 	 */
14307c478bd9Sstevel@tonic-gate 	if (irb == NULL)
14317c478bd9Sstevel@tonic-gate 		return;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
14347c478bd9Sstevel@tonic-gate 	if (irb->irb_tmp_ire_cnt > threshold) {
14357c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && tmp_cnt > 0;
14367c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
14377c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
14387c478bd9Sstevel@tonic-gate 				continue;
14397c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY) {
14407c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_type == IRE_CACHE);
14417c478bd9Sstevel@tonic-gate 				ire_delete(ire);
14427c478bd9Sstevel@tonic-gate 				tmp_cnt--;
14437c478bd9Sstevel@tonic-gate 			}
14447c478bd9Sstevel@tonic-gate 		}
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 	if (irb->irb_ire_cnt - irb->irb_tmp_ire_cnt > threshold) {
14477c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && cnt > 0;
14487c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
1449f4b3ec61Sdh155122 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
1450f4b3ec61Sdh155122 				continue;
1451f4b3ec61Sdh155122 			if (ire->ire_ipversion == IPV4_VERSION) {
1452f4b3ec61Sdh155122 				if (ire->ire_gateway_addr == 0)
1453f4b3ec61Sdh155122 					continue;
1454f4b3ec61Sdh155122 			} else {
1455f4b3ec61Sdh155122 				if (IN6_IS_ADDR_UNSPECIFIED(
1456f4b3ec61Sdh155122 				    &ire->ire_gateway_addr_v6))
14577c478bd9Sstevel@tonic-gate 					continue;
14587c478bd9Sstevel@tonic-gate 			}
14597c478bd9Sstevel@tonic-gate 			if ((ire->ire_type == IRE_CACHE) &&
14607c478bd9Sstevel@tonic-gate 			    (lbolt - ire->ire_last_used_time > cut_off) &&
14617c478bd9Sstevel@tonic-gate 			    (ire->ire_refcnt == 1)) {
14627c478bd9Sstevel@tonic-gate 				ire_delete(ire);
14637c478bd9Sstevel@tonic-gate 				cnt--;
14647c478bd9Sstevel@tonic-gate 			}
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate /*
14717c478bd9Sstevel@tonic-gate  * ire_add_then_send is called when a new IRE has been created in order to
14727c478bd9Sstevel@tonic-gate  * route an outgoing packet.  Typically, it is called from ip_wput when
14737c478bd9Sstevel@tonic-gate  * a response comes back down from a resolver.  We add the IRE, and then
14747c478bd9Sstevel@tonic-gate  * possibly run the packet through ip_wput or ip_rput, as appropriate.
14757c478bd9Sstevel@tonic-gate  * However, we do not add the newly created IRE in the cache when
14767c478bd9Sstevel@tonic-gate  * IRE_MARK_NOADD is set in the IRE. IRE_MARK_NOADD is set at
14777c478bd9Sstevel@tonic-gate  * ip_newroute_ipif(). The ires with IRE_MARK_NOADD and ires returned
14787c478bd9Sstevel@tonic-gate  * by ire_update_srcif_v4() are ire_refrele'd by ip_wput_ire() and get
14797c478bd9Sstevel@tonic-gate  * deleted.
14807c478bd9Sstevel@tonic-gate  * Multirouting support: the packet is silently discarded when the new IRE
14817c478bd9Sstevel@tonic-gate  * holds the RTF_MULTIRT flag, but is not the first IRE to be added with the
14827c478bd9Sstevel@tonic-gate  * RTF_MULTIRT flag for the same destination address.
14837c478bd9Sstevel@tonic-gate  * In this case, we just want to register this additional ire without
14847c478bd9Sstevel@tonic-gate  * sending the packet, as it has already been replicated through
14857c478bd9Sstevel@tonic-gate  * existing multirt routes in ip_wput().
14867c478bd9Sstevel@tonic-gate  */
14877c478bd9Sstevel@tonic-gate void
14887c478bd9Sstevel@tonic-gate ire_add_then_send(queue_t *q, ire_t *ire, mblk_t *mp)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	irb_t *irb;
14917c478bd9Sstevel@tonic-gate 	boolean_t drop = B_FALSE;
14927c478bd9Sstevel@tonic-gate 	/* LINTED : set but not used in function */
14937c478bd9Sstevel@tonic-gate 	boolean_t mctl_present;
14947c478bd9Sstevel@tonic-gate 	mblk_t *first_mp = NULL;
14957c478bd9Sstevel@tonic-gate 	mblk_t *save_mp = NULL;
14967c478bd9Sstevel@tonic-gate 	ire_t *dst_ire;
14977c478bd9Sstevel@tonic-gate 	ipha_t *ipha;
14987c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
1499f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
15027c478bd9Sstevel@tonic-gate 		/*
15037c478bd9Sstevel@tonic-gate 		 * We first have to retrieve the destination address carried
15047c478bd9Sstevel@tonic-gate 		 * by the packet.
15057c478bd9Sstevel@tonic-gate 		 * We can't rely on ire as it can be related to a gateway.
15067c478bd9Sstevel@tonic-gate 		 * The destination address will help in determining if
15077c478bd9Sstevel@tonic-gate 		 * other RTF_MULTIRT ires are already registered.
15087c478bd9Sstevel@tonic-gate 		 *
15097c478bd9Sstevel@tonic-gate 		 * We first need to know where we are going : v4 or V6.
15107c478bd9Sstevel@tonic-gate 		 * the ire version is enough, as there is no risk that
15117c478bd9Sstevel@tonic-gate 		 * we resolve an IPv6 address with an IPv4 ire
15127c478bd9Sstevel@tonic-gate 		 * or vice versa.
15137c478bd9Sstevel@tonic-gate 		 */
15147c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
15157c478bd9Sstevel@tonic-gate 			EXTRACT_PKT_MP(mp, first_mp, mctl_present);
15167c478bd9Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
15177c478bd9Sstevel@tonic-gate 			save_mp = mp;
15187c478bd9Sstevel@tonic-gate 			mp = first_mp;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup(ipha->ipha_dst,
1521f4b3ec61Sdh155122 			    ire->ire_zoneid, MBLK_GETLABEL(mp), ipst);
15227c478bd9Sstevel@tonic-gate 		} else {
1523c793af95Ssangeeta 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
15247c478bd9Sstevel@tonic-gate 			/*
15257c478bd9Sstevel@tonic-gate 			 * Get a pointer to the beginning of the IPv6 header.
15267c478bd9Sstevel@tonic-gate 			 * Ignore leading IPsec control mblks.
15277c478bd9Sstevel@tonic-gate 			 */
15287c478bd9Sstevel@tonic-gate 			first_mp = mp;
15297c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_CTL) {
15307c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
15317c478bd9Sstevel@tonic-gate 			}
15327c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)mp->b_rptr;
15337c478bd9Sstevel@tonic-gate 			save_mp = mp;
15347c478bd9Sstevel@tonic-gate 			mp = first_mp;
15357c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup_v6(&ip6h->ip6_dst,
1536f4b3ec61Sdh155122 			    ire->ire_zoneid, MBLK_GETLABEL(mp), ipst);
15377c478bd9Sstevel@tonic-gate 		}
15387c478bd9Sstevel@tonic-gate 		if (dst_ire != NULL) {
15397c478bd9Sstevel@tonic-gate 			if (dst_ire->ire_flags & RTF_MULTIRT) {
15407c478bd9Sstevel@tonic-gate 				/*
15417c478bd9Sstevel@tonic-gate 				 * At least one resolved multirt route
15427c478bd9Sstevel@tonic-gate 				 * already exists for the destination,
15437c478bd9Sstevel@tonic-gate 				 * don't sent this packet: either drop it
15447c478bd9Sstevel@tonic-gate 				 * or complete the pending resolution,
15457c478bd9Sstevel@tonic-gate 				 * depending on the ire.
15467c478bd9Sstevel@tonic-gate 				 */
15477c478bd9Sstevel@tonic-gate 				drop = B_TRUE;
15487c478bd9Sstevel@tonic-gate 			}
15497c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_then_send: dst_ire %p "
15507c478bd9Sstevel@tonic-gate 			    "[dst %08x, gw %08x], drop %d\n",
15517c478bd9Sstevel@tonic-gate 			    (void *)dst_ire,
15527c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
15537c478bd9Sstevel@tonic-gate 				ntohl(dst_ire->ire_addr) : \
15547c478bd9Sstevel@tonic-gate 				ntohl(V4_PART_OF_V6(dst_ire->ire_addr_v6)),
15557c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
15567c478bd9Sstevel@tonic-gate 				ntohl(dst_ire->ire_gateway_addr) : \
15577c478bd9Sstevel@tonic-gate 				ntohl(V4_PART_OF_V6(
15587c478bd9Sstevel@tonic-gate 				    dst_ire->ire_gateway_addr_v6)),
15597c478bd9Sstevel@tonic-gate 			    drop));
15607c478bd9Sstevel@tonic-gate 			ire_refrele(dst_ire);
15617c478bd9Sstevel@tonic-gate 		}
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	if (!(ire->ire_marks & IRE_MARK_NOADD)) {
15657c478bd9Sstevel@tonic-gate 		/*
15667c478bd9Sstevel@tonic-gate 		 * Regular packets with cache bound ires and
15677c478bd9Sstevel@tonic-gate 		 * the packets from ARP response for ires which
15687c478bd9Sstevel@tonic-gate 		 * belong to the ire_srcif_v4 table, are here.
15697c478bd9Sstevel@tonic-gate 		 */
15707c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill == NULL) {
15717c478bd9Sstevel@tonic-gate 			/* Add the ire */
1572c793af95Ssangeeta 			(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
15737c478bd9Sstevel@tonic-gate 		} else {
15747c478bd9Sstevel@tonic-gate 			/*
15757c478bd9Sstevel@tonic-gate 			 * This must be ARP response for ire in interface based
15767c478bd9Sstevel@tonic-gate 			 * table. Note that we don't add them in cache table,
15777c478bd9Sstevel@tonic-gate 			 * instead we update the existing table with dlureq_mp
15787c478bd9Sstevel@tonic-gate 			 * information. The reverse tunnel ires do not come
15797c478bd9Sstevel@tonic-gate 			 * here, as reverse tunnel is non-resolver interface.
15807c478bd9Sstevel@tonic-gate 			 * XXX- another design alternative was to mark the
15817c478bd9Sstevel@tonic-gate 			 * ires in interface based table with a special mark to
15827c478bd9Sstevel@tonic-gate 			 * make absolutely sure that we operate in right ires.
15837c478bd9Sstevel@tonic-gate 			 * This idea was not implemented as part of code review
15847c478bd9Sstevel@tonic-gate 			 * suggestion, as ire_in_ill suffice to distinguish
15857c478bd9Sstevel@tonic-gate 			 * between the regular ires and interface based
15867c478bd9Sstevel@tonic-gate 			 * ires now and thus we save a bit in the ire_marks.
15877c478bd9Sstevel@tonic-gate 			 */
15887c478bd9Sstevel@tonic-gate 			ire = ire_update_srcif_v4(ire);
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
15927c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
15937c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
15947c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
15957c478bd9Sstevel@tonic-gate 			freemsg(mp);
15967c478bd9Sstevel@tonic-gate 			return;
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
15997c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add_v4/v6 */
16007c478bd9Sstevel@tonic-gate 			return;
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 	if (drop) {
16047c478bd9Sstevel@tonic-gate 		/*
16057c478bd9Sstevel@tonic-gate 		 * If we're adding an RTF_MULTIRT ire, the resolution
16067c478bd9Sstevel@tonic-gate 		 * is over: we just drop the packet.
16077c478bd9Sstevel@tonic-gate 		 */
16087c478bd9Sstevel@tonic-gate 		if (ire->ire_flags & RTF_MULTIRT) {
16097c478bd9Sstevel@tonic-gate 			if (save_mp) {
16107c478bd9Sstevel@tonic-gate 				save_mp->b_prev = NULL;
16117c478bd9Sstevel@tonic-gate 				save_mp->b_next = NULL;
16127c478bd9Sstevel@tonic-gate 			}
16137c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
16147c478bd9Sstevel@tonic-gate 			freemsg(mp);
16157c478bd9Sstevel@tonic-gate 		} else {
16167c478bd9Sstevel@tonic-gate 			/*
16177c478bd9Sstevel@tonic-gate 			 * Otherwise, we're adding the ire to a gateway
16187c478bd9Sstevel@tonic-gate 			 * for a multirt route.
16197c478bd9Sstevel@tonic-gate 			 * Invoke ip_newroute() to complete the resolution
16207c478bd9Sstevel@tonic-gate 			 * of the route. We will then come back here and
16217c478bd9Sstevel@tonic-gate 			 * finally drop this packet in the above code.
16227c478bd9Sstevel@tonic-gate 			 */
16237c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
16247c478bd9Sstevel@tonic-gate 				/*
16257c478bd9Sstevel@tonic-gate 				 * TODO: in order for CGTP to work in non-global
16267c478bd9Sstevel@tonic-gate 				 * zones, ip_newroute() must create the IRE
16277c478bd9Sstevel@tonic-gate 				 * cache in the zone indicated by
16287c478bd9Sstevel@tonic-gate 				 * ire->ire_zoneid.
16297c478bd9Sstevel@tonic-gate 				 */
16307c478bd9Sstevel@tonic-gate 				ip_newroute(q, mp, ipha->ipha_dst, 0,
16315597b60aSnordmark 				    (CONN_Q(q) ? Q_TO_CONN(q) : NULL),
1632f4b3ec61Sdh155122 				    ire->ire_zoneid, ipst);
16337c478bd9Sstevel@tonic-gate 			} else {
1634c793af95Ssangeeta 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
16357c478bd9Sstevel@tonic-gate 				ip_newroute_v6(q, mp, &ip6h->ip6_dst, NULL,
1636f4b3ec61Sdh155122 				    NULL, ire->ire_zoneid, ipst);
16377c478bd9Sstevel@tonic-gate 			}
16387c478bd9Sstevel@tonic-gate 		}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		ire_refrele(ire); /* As done by ire_send(). */
16417c478bd9Sstevel@tonic-gate 		return;
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 	/*
16447c478bd9Sstevel@tonic-gate 	 * Need to remember ire_bucket here as ire_send*() may delete
16457c478bd9Sstevel@tonic-gate 	 * the ire so we cannot reference it after that.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	irb = ire->ire_bucket;
16487c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
16497c478bd9Sstevel@tonic-gate 		ire_send_v6(q, mp, ire);
16507c478bd9Sstevel@tonic-gate 		/*
16517c478bd9Sstevel@tonic-gate 		 * Clean up more than 1 IRE so that the clean up does not
16527c478bd9Sstevel@tonic-gate 		 * need to be done every time when a new IRE is added and
16537c478bd9Sstevel@tonic-gate 		 * the threshold is reached.
16547c478bd9Sstevel@tonic-gate 		 */
16557c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip6_ire_max_bucket_cnt, 2);
16567c478bd9Sstevel@tonic-gate 	} else {
16577c478bd9Sstevel@tonic-gate 		ire_send(q, mp, ire);
16587c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip_ire_max_bucket_cnt, 2);
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate /*
16637c478bd9Sstevel@tonic-gate  * Initialize the ire that is specific to IPv4 part and call
16647c478bd9Sstevel@tonic-gate  * ire_init_common to finish it.
16657c478bd9Sstevel@tonic-gate  */
16667c478bd9Sstevel@tonic-gate ire_t *
16677c478bd9Sstevel@tonic-gate ire_init(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *src_addr,
16687c478bd9Sstevel@tonic-gate     uchar_t *gateway, uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp,
16697c478bd9Sstevel@tonic-gate     queue_t *rfq, queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif,
16707c478bd9Sstevel@tonic-gate     ill_t *in_ill, ipaddr_t cmask, uint32_t phandle, uint32_t ihandle,
1671f4b3ec61Sdh155122     uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1672f4b3ec61Sdh155122     ip_stack_t *ipst)
16737c478bd9Sstevel@tonic-gate {
167445916cd2Sjpk 	/*
167545916cd2Sjpk 	 * Reject IRE security attribute creation/initialization
167645916cd2Sjpk 	 * if system is not running in Trusted mode.
167745916cd2Sjpk 	 */
167845916cd2Sjpk 	if ((gc != NULL || gcgrp != NULL) && !is_system_labeled())
167945916cd2Sjpk 		return (NULL);
168045916cd2Sjpk 
16817c478bd9Sstevel@tonic-gate 	if (fp_mp != NULL) {
16827c478bd9Sstevel@tonic-gate 		/*
16837c478bd9Sstevel@tonic-gate 		 * We can't dupb() here as multiple threads could be
16847c478bd9Sstevel@tonic-gate 		 * calling dupb on the same mp which is incorrect.
16857c478bd9Sstevel@tonic-gate 		 * First dupb() should be called only by one thread.
16867c478bd9Sstevel@tonic-gate 		 */
16877c478bd9Sstevel@tonic-gate 		fp_mp = copyb(fp_mp);
16887c478bd9Sstevel@tonic-gate 		if (fp_mp == NULL)
16897c478bd9Sstevel@tonic-gate 			return (NULL);
16907c478bd9Sstevel@tonic-gate 	}
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	if (dlureq_mp != NULL) {
16937c478bd9Sstevel@tonic-gate 		/*
16947c478bd9Sstevel@tonic-gate 		 * We can't dupb() here as multiple threads could be
16957c478bd9Sstevel@tonic-gate 		 * calling dupb on the same mp which is incorrect.
16967c478bd9Sstevel@tonic-gate 		 * First dupb() should be called only by one thread.
16977c478bd9Sstevel@tonic-gate 		 */
16987c478bd9Sstevel@tonic-gate 		dlureq_mp = copyb(dlureq_mp);
16997c478bd9Sstevel@tonic-gate 		if (dlureq_mp == NULL) {
17007c478bd9Sstevel@tonic-gate 			if (fp_mp != NULL)
17017c478bd9Sstevel@tonic-gate 				freeb(fp_mp);
17027c478bd9Sstevel@tonic-gate 			return (NULL);
17037c478bd9Sstevel@tonic-gate 		}
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	/*
17077c478bd9Sstevel@tonic-gate 	 * Check that IRE_IF_RESOLVER and IRE_IF_NORESOLVER have a
17087c478bd9Sstevel@tonic-gate 	 * dlureq_mp which is the ill_resolver_mp for IRE_IF_RESOLVER
17097c478bd9Sstevel@tonic-gate 	 * and DL_UNITDATA_REQ for IRE_IF_NORESOLVER.
17107c478bd9Sstevel@tonic-gate 	 */
17117c478bd9Sstevel@tonic-gate 	if ((type & IRE_INTERFACE) &&
17127c478bd9Sstevel@tonic-gate 	    dlureq_mp == NULL) {
17137c478bd9Sstevel@tonic-gate 		ASSERT(fp_mp == NULL);
17147c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_init: no dlureq_mp\n"));
17157c478bd9Sstevel@tonic-gate 		return (NULL);
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 
1718f4b3ec61Sdh155122 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_alloced);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	if (addr != NULL)
17217c478bd9Sstevel@tonic-gate 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
17227c478bd9Sstevel@tonic-gate 	if (src_addr != NULL)
17237c478bd9Sstevel@tonic-gate 		bcopy(src_addr, &ire->ire_src_addr, IP_ADDR_LEN);
17247c478bd9Sstevel@tonic-gate 	if (mask != NULL) {
17257c478bd9Sstevel@tonic-gate 		bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
17267c478bd9Sstevel@tonic-gate 		ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 	if (gateway != NULL) {
17297c478bd9Sstevel@tonic-gate 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 	if (in_src_addr != NULL) {
17327c478bd9Sstevel@tonic-gate 		bcopy(in_src_addr, &ire->ire_in_src_addr, IP_ADDR_LEN);
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (type == IRE_CACHE)
17367c478bd9Sstevel@tonic-gate 		ire->ire_cmask = cmask;
17377c478bd9Sstevel@tonic-gate 
173845916cd2Sjpk 	/* ire_init_common will free the mblks upon encountering any failure */
173945916cd2Sjpk 	if (!ire_init_common(ire, max_fragp, fp_mp, rfq, stq, type, dlureq_mp,
174045916cd2Sjpk 	    ipif, in_ill, phandle, ihandle, flags, IPV4_VERSION, ulp_info,
1741f4b3ec61Sdh155122 	    gc, gcgrp, ipst))
174245916cd2Sjpk 		return (NULL);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	return (ire);
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate /*
17487c478bd9Sstevel@tonic-gate  * Similar to ire_create except that it is called only when
17497c478bd9Sstevel@tonic-gate  * we want to allocate ire as an mblk e.g. we have an external
17507c478bd9Sstevel@tonic-gate  * resolver ARP.
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate ire_t *
17537c478bd9Sstevel@tonic-gate ire_create_mp(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
17547c478bd9Sstevel@tonic-gate     uchar_t *in_src_addr, uint_t max_frag, mblk_t *fp_mp, queue_t *rfq,
17557c478bd9Sstevel@tonic-gate     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
17567c478bd9Sstevel@tonic-gate     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
1757f4b3ec61Sdh155122     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1758f4b3ec61Sdh155122     ip_stack_t *ipst)
17597c478bd9Sstevel@tonic-gate {
1760c793af95Ssangeeta 	ire_t	*ire, *buf;
17617c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
17627c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
1763c793af95Ssangeeta 	size_t	bufsize;
1764c793af95Ssangeeta 	frtn_t	*frtnp;
1765c793af95Ssangeeta 	ill_t	*ill;
17667c478bd9Sstevel@tonic-gate 
1767c793af95Ssangeeta 	bufsize = sizeof (ire_t) + sizeof (frtn_t);
1768c793af95Ssangeeta 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
1769c793af95Ssangeeta 	if (buf == NULL) {
17707c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create_mp: alloc failed\n"));
17717c478bd9Sstevel@tonic-gate 		return (NULL);
17727c478bd9Sstevel@tonic-gate 	}
1773c793af95Ssangeeta 	frtnp = (frtn_t *)(buf + 1);
1774c793af95Ssangeeta 	frtnp->free_arg = (caddr_t)buf;
1775c793af95Ssangeeta 	frtnp->free_func = ire_freemblk;
17767c478bd9Sstevel@tonic-gate 
1777c793af95Ssangeeta 	/*
1778c793af95Ssangeeta 	 * Allocate the new IRE. The ire created will hold a ref on
1779c793af95Ssangeeta 	 * an nce_t after ire_nce_init, and this ref must either be
1780c793af95Ssangeeta 	 * (a)  transferred to the ire_cache entry created when ire_add_v4
1781c793af95Ssangeeta 	 *	is called after successful arp resolution, or,
1782c793af95Ssangeeta 	 * (b)  released, when arp resolution fails
1783c793af95Ssangeeta 	 * Case (b) is handled in ire_freemblk() which will be called
1784c793af95Ssangeeta 	 * when mp is freed as a result of failed arp.
1785c793af95Ssangeeta 	 */
1786c793af95Ssangeeta 	mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp);
1787c793af95Ssangeeta 	if (mp == NULL) {
1788c793af95Ssangeeta 		ip1dbg(("ire_create_mp: alloc failed\n"));
1789c793af95Ssangeeta 		kmem_free(buf, bufsize);
1790c793af95Ssangeeta 		return (NULL);
1791c793af95Ssangeeta 	}
17927c478bd9Sstevel@tonic-gate 	ire = (ire_t *)mp->b_rptr;
17937c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&ire[1];
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/* Start clean. */
17967c478bd9Sstevel@tonic-gate 	*ire = ire_null;
17977c478bd9Sstevel@tonic-gate 	ire->ire_mp = mp;
17987c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
1799c793af95Ssangeeta 	ire->ire_marks |= IRE_MARK_UNCACHED;
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
18027c478bd9Sstevel@tonic-gate 	    NULL, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill, cmask,
1803f4b3ec61Sdh155122 	    phandle, ihandle, flags, ulp_info, gc, gcgrp, ipst);
18047c478bd9Sstevel@tonic-gate 
180553bec8caSsowmini 	ill = (ill_t *)(stq->q_ptr);
18067c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
1807f4b3ec61Sdh155122 		/* ire_freemblk needs these set */
180853bec8caSsowmini 		ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
1809f4b3ec61Sdh155122 		ire->ire_ipst = ipst;
18107c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
18117c478bd9Sstevel@tonic-gate 		return (NULL);
18127c478bd9Sstevel@tonic-gate 	}
1813c793af95Ssangeeta 	ret_ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
18147c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
18157c478bd9Sstevel@tonic-gate 	/*
18167c478bd9Sstevel@tonic-gate 	 * ire_max_frag is normally zero here and is atomically set
18177c478bd9Sstevel@tonic-gate 	 * under the irebucket lock in ire_add_v[46] except for the
18187c478bd9Sstevel@tonic-gate 	 * case of IRE_MARK_NOADD. In that event the the ire_max_frag
18197c478bd9Sstevel@tonic-gate 	 * is non-zero here.
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = max_frag;
18227c478bd9Sstevel@tonic-gate 	return (ire);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate /*
18267c478bd9Sstevel@tonic-gate  * ire_create is called to allocate and initialize a new IRE.
18277c478bd9Sstevel@tonic-gate  *
18287c478bd9Sstevel@tonic-gate  * NOTE : This is called as writer sometimes though not required
18297c478bd9Sstevel@tonic-gate  * by this function.
18307c478bd9Sstevel@tonic-gate  */
18317c478bd9Sstevel@tonic-gate ire_t *
18327c478bd9Sstevel@tonic-gate ire_create(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
18337c478bd9Sstevel@tonic-gate     uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp, queue_t *rfq,
18347c478bd9Sstevel@tonic-gate     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
18357c478bd9Sstevel@tonic-gate     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
1836f4b3ec61Sdh155122     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1837f4b3ec61Sdh155122     ip_stack_t *ipst)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate 	ire_t	*ire;
18407c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
18437c478bd9Sstevel@tonic-gate 	if (ire == NULL) {
18447c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create: alloc failed\n"));
18457c478bd9Sstevel@tonic-gate 		return (NULL);
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 	*ire = ire_null;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
18507c478bd9Sstevel@tonic-gate 	    max_fragp, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill,  cmask,
1851f4b3ec61Sdh155122 	    phandle, ihandle, flags, ulp_info, gc, gcgrp, ipst);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
18547c478bd9Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
18557c478bd9Sstevel@tonic-gate 		return (NULL);
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
18587c478bd9Sstevel@tonic-gate 	return (ire);
18597c478bd9Sstevel@tonic-gate }
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate /*
18637c478bd9Sstevel@tonic-gate  * Common to IPv4 and IPv6
18647c478bd9Sstevel@tonic-gate  */
186545916cd2Sjpk boolean_t
18667c478bd9Sstevel@tonic-gate ire_init_common(ire_t *ire, uint_t *max_fragp, mblk_t *fp_mp,
18677c478bd9Sstevel@tonic-gate     queue_t *rfq, queue_t *stq, ushort_t type,
18687c478bd9Sstevel@tonic-gate     mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill, uint32_t phandle,
18697c478bd9Sstevel@tonic-gate     uint32_t ihandle, uint32_t flags, uchar_t ipversion,
1870f4b3ec61Sdh155122     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp,
1871f4b3ec61Sdh155122     ip_stack_t *ipst)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = max_fragp;
1874f4b3ec61Sdh155122 	ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	ASSERT(fp_mp == NULL || fp_mp->b_datap->db_type == M_DATA);
187745916cd2Sjpk #ifdef DEBUG
187845916cd2Sjpk 	if (ipif != NULL) {
18797c478bd9Sstevel@tonic-gate 		if (ipif->ipif_isv6)
18807c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV6_VERSION);
18817c478bd9Sstevel@tonic-gate 		else
18827c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV4_VERSION);
18837c478bd9Sstevel@tonic-gate 	}
188445916cd2Sjpk #endif /* DEBUG */
188545916cd2Sjpk 
188645916cd2Sjpk 	/*
188745916cd2Sjpk 	 * Create/initialize IRE security attribute only in Trusted mode;
188845916cd2Sjpk 	 * if the passed in gc/gcgrp is non-NULL, we expect that the caller
188945916cd2Sjpk 	 * has held a reference to it and will release it when this routine
189045916cd2Sjpk 	 * returns a failure, otherwise we own the reference.  We do this
189145916cd2Sjpk 	 * prior to initializing the rest IRE fields.
1892dc3879f9Sjarrett 	 *
1893dc3879f9Sjarrett 	 * Don't allocate ire_gw_secattr for the resolver case to prevent
1894dc3879f9Sjarrett 	 * memory leak (in case of external resolution failure). We'll
1895dc3879f9Sjarrett 	 * allocate it after a successful external resolution, in ire_add().
1896dc3879f9Sjarrett 	 * Note that ire->ire_mp != NULL here means this ire is headed
1897dc3879f9Sjarrett 	 * to an external resolver.
189845916cd2Sjpk 	 */
189945916cd2Sjpk 	if (is_system_labeled()) {
190045916cd2Sjpk 		if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST |
190145916cd2Sjpk 		    IRE_INTERFACE)) != 0) {
190245916cd2Sjpk 			/* release references on behalf of caller */
190345916cd2Sjpk 			if (gc != NULL)
190445916cd2Sjpk 				GC_REFRELE(gc);
190545916cd2Sjpk 			if (gcgrp != NULL)
190645916cd2Sjpk 				GCGRP_REFRELE(gcgrp);
1907dc3879f9Sjarrett 		} else if ((ire->ire_mp == NULL) &&
1908dc3879f9Sjarrett 		    tsol_ire_init_gwattr(ire, ipversion, gc, gcgrp) != 0) {
190945916cd2Sjpk 			/* free any caller-allocated mblks upon failure */
191045916cd2Sjpk 			if (fp_mp != NULL)
191145916cd2Sjpk 				freeb(fp_mp);
191245916cd2Sjpk 			if (dlureq_mp != NULL)
191345916cd2Sjpk 				freeb(dlureq_mp);
191445916cd2Sjpk 			return (B_FALSE);
191545916cd2Sjpk 		}
191645916cd2Sjpk 	}
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	ire->ire_stq = stq;
19197c478bd9Sstevel@tonic-gate 	ire->ire_rfq = rfq;
19207c478bd9Sstevel@tonic-gate 	ire->ire_type = type;
19217c478bd9Sstevel@tonic-gate 	ire->ire_flags = RTF_UP | flags;
19227c478bd9Sstevel@tonic-gate 	ire->ire_ident = TICK_TO_MSEC(lbolt);
19237c478bd9Sstevel@tonic-gate 	bcopy(ulp_info, &ire->ire_uinfo, sizeof (iulp_t));
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
19267c478bd9Sstevel@tonic-gate 	ire->ire_last_used_time = lbolt;
19277c478bd9Sstevel@tonic-gate 	ire->ire_create_time = (uint32_t)gethrestime_sec();
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	/*
19307c478bd9Sstevel@tonic-gate 	 * If this IRE is an IRE_CACHE, inherit the handles from the
19317c478bd9Sstevel@tonic-gate 	 * parent IREs. For others in the forwarding table, assign appropriate
19327c478bd9Sstevel@tonic-gate 	 * new ones.
19337c478bd9Sstevel@tonic-gate 	 *
19347c478bd9Sstevel@tonic-gate 	 * The mutex protecting ire_handle is because ire_create is not always
19357c478bd9Sstevel@tonic-gate 	 * called as a writer.
19367c478bd9Sstevel@tonic-gate 	 */
19377c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_OFFSUBNET) {
1938f4b3ec61Sdh155122 		mutex_enter(&ipst->ips_ire_handle_lock);
1939f4b3ec61Sdh155122 		ire->ire_phandle = (uint32_t)ipst->ips_ire_handle++;
1940f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_handle_lock);
19417c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type & IRE_INTERFACE) {
1942f4b3ec61Sdh155122 		mutex_enter(&ipst->ips_ire_handle_lock);
1943f4b3ec61Sdh155122 		ire->ire_ihandle = (uint32_t)ipst->ips_ire_handle++;
1944f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_handle_lock);
19457c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_CACHE) {
19467c478bd9Sstevel@tonic-gate 		ire->ire_phandle = phandle;
19477c478bd9Sstevel@tonic-gate 		ire->ire_ihandle = ihandle;
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate 	ire->ire_in_ill = in_ill;
19507c478bd9Sstevel@tonic-gate 	ire->ire_ipif = ipif;
19517c478bd9Sstevel@tonic-gate 	if (ipif != NULL) {
19527c478bd9Sstevel@tonic-gate 		ire->ire_ipif_seqid = ipif->ipif_seqid;
19537c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = ipif->ipif_zoneid;
19547c478bd9Sstevel@tonic-gate 	} else {
19557c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = GLOBAL_ZONEID;
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate 	ire->ire_ipversion = ipversion;
19587c478bd9Sstevel@tonic-gate 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
1959c793af95Ssangeeta 	if (ipversion == IPV4_VERSION) {
1960c793af95Ssangeeta 		if (ire_nce_init(ire, fp_mp, dlureq_mp) != 0) {
1961c793af95Ssangeeta 			/* some failure occurred. propagate error back */
1962c793af95Ssangeeta 			return (B_FALSE);
1963c793af95Ssangeeta 		}
1964c793af95Ssangeeta 	} else {
1965c793af95Ssangeeta 		ASSERT(ipversion == IPV6_VERSION);
1966c793af95Ssangeeta 		/*
1967c793af95Ssangeeta 		 * IPv6 initializes the ire_nce in ire_add_v6,
1968c793af95Ssangeeta 		 * which expects to find the ire_nce to be null when
1969c793af95Ssangeeta 		 * when it is called.
1970c793af95Ssangeeta 		 */
1971c793af95Ssangeeta 		if (dlureq_mp)
1972c793af95Ssangeeta 			freemsg(dlureq_mp);
1973c793af95Ssangeeta 		if (fp_mp)
1974c793af95Ssangeeta 			freemsg(fp_mp);
1975c793af95Ssangeeta 	}
1976c793af95Ssangeeta 	ire->ire_refcnt = 1;
1977f4b3ec61Sdh155122 	ire->ire_ipst = ipst;	/* No netstack_hold */
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
19807c478bd9Sstevel@tonic-gate 	bzero(ire->ire_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
19817c478bd9Sstevel@tonic-gate #endif
198245916cd2Sjpk 
198345916cd2Sjpk 	return (B_TRUE);
19847c478bd9Sstevel@tonic-gate }
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate /*
19877c478bd9Sstevel@tonic-gate  * This routine is called repeatedly by ipif_up to create broadcast IREs.
19887c478bd9Sstevel@tonic-gate  * It is passed a pointer to a slot in an IRE pointer array into which to
19897c478bd9Sstevel@tonic-gate  * place the pointer to the new IRE, if indeed we create one.  If the
19907c478bd9Sstevel@tonic-gate  * IRE corresponding to the address passed in would be a duplicate of an
19917c478bd9Sstevel@tonic-gate  * existing one, we don't create the new one.  irep is incremented before
19927c478bd9Sstevel@tonic-gate  * return only if we do create a new IRE.  (Always called as writer.)
19937c478bd9Sstevel@tonic-gate  *
19947c478bd9Sstevel@tonic-gate  * Note that with the "match_flags" parameter, we can match on either
19957c478bd9Sstevel@tonic-gate  * a particular logical interface (MATCH_IRE_IPIF) or for all logical
19967c478bd9Sstevel@tonic-gate  * interfaces for a given physical interface (MATCH_IRE_ILL).  Currently,
19977c478bd9Sstevel@tonic-gate  * we only create broadcast ire's on a per physical interface basis. If
19987c478bd9Sstevel@tonic-gate  * someone is going to be mucking with logical interfaces, it is important
19997c478bd9Sstevel@tonic-gate  * to call "ipif_check_bcast_ires()" to make sure that any change to a
20007c478bd9Sstevel@tonic-gate  * logical interface will not cause critical broadcast IRE's to be deleted.
20017c478bd9Sstevel@tonic-gate  */
20027c478bd9Sstevel@tonic-gate ire_t **
20037c478bd9Sstevel@tonic-gate ire_check_and_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep,
20047c478bd9Sstevel@tonic-gate     int match_flags)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	ire_t *ire;
20077c478bd9Sstevel@tonic-gate 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
2008f4b3ec61Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * No broadcast IREs for the LOOPBACK interface
20127c478bd9Sstevel@tonic-gate 	 * or others such as point to point and IPIF_NOXMIT.
20137c478bd9Sstevel@tonic-gate 	 */
20147c478bd9Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_BROADCAST) ||
20157c478bd9Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_NOXMIT))
20167c478bd9Sstevel@tonic-gate 		return (irep);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	/* If this would be a duplicate, don't bother. */
20197c478bd9Sstevel@tonic-gate 	if ((ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ipif,
2020f4b3ec61Sdh155122 	    ipif->ipif_zoneid, NULL, match_flags, ipst)) != NULL) {
20217c478bd9Sstevel@tonic-gate 		/*
20227c478bd9Sstevel@tonic-gate 		 * We look for non-deprecated (and non-anycast, non-nolocal)
20237c478bd9Sstevel@tonic-gate 		 * ipifs as the best choice. ipifs with check_flags matching
20247c478bd9Sstevel@tonic-gate 		 * (deprecated, etc) are used only if non-deprecated ipifs
20257c478bd9Sstevel@tonic-gate 		 * are not available. if the existing ire's ipif is deprecated
20267c478bd9Sstevel@tonic-gate 		 * and the new ipif is non-deprecated, switch to the new ipif
20277c478bd9Sstevel@tonic-gate 		 */
20287c478bd9Sstevel@tonic-gate 		if ((!(ire->ire_ipif->ipif_flags & check_flags)) ||
20297c478bd9Sstevel@tonic-gate 		    (ipif->ipif_flags & check_flags)) {
20307c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
20317c478bd9Sstevel@tonic-gate 			return (irep);
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 		/*
20347c478bd9Sstevel@tonic-gate 		 * Bcast ires exist in pairs. Both have to be deleted,
20357c478bd9Sstevel@tonic-gate 		 * Since we are exclusive we can make the above assertion.
20367c478bd9Sstevel@tonic-gate 		 * The 1st has to be refrele'd since it was ctable_lookup'd.
20377c478bd9Sstevel@tonic-gate 		 */
20387c478bd9Sstevel@tonic-gate 		ASSERT(IAM_WRITER_IPIF(ipif));
20397c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_next->ire_addr == ire->ire_addr);
20407c478bd9Sstevel@tonic-gate 		ire_delete(ire->ire_next);
20417c478bd9Sstevel@tonic-gate 		ire_delete(ire);
20427c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	irep = ire_create_bcast(ipif, addr, irep);
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	return (irep);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate uint_t ip_loopback_mtu = IP_LOOPBACK_MTU;
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate /*
20537c478bd9Sstevel@tonic-gate  * This routine is called from ipif_check_bcast_ires and ire_check_bcast.
20547c478bd9Sstevel@tonic-gate  * It leaves all the verifying and deleting to those routines. So it always
20557c478bd9Sstevel@tonic-gate  * creates 2 bcast ires and chains them into the ire array passed in.
20567c478bd9Sstevel@tonic-gate  */
20577c478bd9Sstevel@tonic-gate ire_t **
20587c478bd9Sstevel@tonic-gate ire_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep)
20597c478bd9Sstevel@tonic-gate {
2060f4b3ec61Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
2061f4b3ec61Sdh155122 
20627c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
20637c478bd9Sstevel@tonic-gate 	    (uchar_t *)&addr,			/* dest addr */
20647c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ip_g_all_ones,		/* mask */
20657c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ipif->ipif_src_addr,	/* source addr */
20667c478bd9Sstevel@tonic-gate 	    NULL,				/* no gateway */
20677c478bd9Sstevel@tonic-gate 	    NULL,				/* no in_src_addr */
20687c478bd9Sstevel@tonic-gate 	    &ipif->ipif_mtu,			/* max frag */
20697c478bd9Sstevel@tonic-gate 	    NULL,				/* fast path header */
20707c478bd9Sstevel@tonic-gate 	    ipif->ipif_rq,			/* recv-from queue */
20717c478bd9Sstevel@tonic-gate 	    ipif->ipif_wq,			/* send-to queue */
20727c478bd9Sstevel@tonic-gate 	    IRE_BROADCAST,
20737c478bd9Sstevel@tonic-gate 	    ipif->ipif_bcast_mp,		/* xmit header */
20747c478bd9Sstevel@tonic-gate 	    ipif,
20757c478bd9Sstevel@tonic-gate 	    NULL,
20767c478bd9Sstevel@tonic-gate 	    0,
20777c478bd9Sstevel@tonic-gate 	    0,
20787c478bd9Sstevel@tonic-gate 	    0,
20797c478bd9Sstevel@tonic-gate 	    0,
208045916cd2Sjpk 	    &ire_uinfo_null,
208145916cd2Sjpk 	    NULL,
2082f4b3ec61Sdh155122 	    NULL,
2083f4b3ec61Sdh155122 	    ipst);
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
20867c478bd9Sstevel@tonic-gate 		(uchar_t *)&addr,		 /* dest address */
20877c478bd9Sstevel@tonic-gate 		(uchar_t *)&ip_g_all_ones,	 /* mask */
20887c478bd9Sstevel@tonic-gate 		(uchar_t *)&ipif->ipif_src_addr, /* source address */
20897c478bd9Sstevel@tonic-gate 		NULL,				 /* no gateway */
20907c478bd9Sstevel@tonic-gate 		NULL,				 /* no in_src_addr */
20917c478bd9Sstevel@tonic-gate 		&ip_loopback_mtu,		 /* max frag size */
20927c478bd9Sstevel@tonic-gate 		NULL,				 /* Fast Path header */
20937c478bd9Sstevel@tonic-gate 		ipif->ipif_rq,			 /* recv-from queue */
20947c478bd9Sstevel@tonic-gate 		NULL,				 /* no send-to queue */
20957c478bd9Sstevel@tonic-gate 		IRE_BROADCAST,		/* Needed for fanout in wput */
20967c478bd9Sstevel@tonic-gate 		NULL,
20977c478bd9Sstevel@tonic-gate 		ipif,
20987c478bd9Sstevel@tonic-gate 		NULL,
20997c478bd9Sstevel@tonic-gate 		0,
21007c478bd9Sstevel@tonic-gate 		0,
21017c478bd9Sstevel@tonic-gate 		0,
21027c478bd9Sstevel@tonic-gate 		0,
210345916cd2Sjpk 		&ire_uinfo_null,
210445916cd2Sjpk 		NULL,
2105f4b3ec61Sdh155122 		NULL,
2106f4b3ec61Sdh155122 		ipst);
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	return (irep);
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate /*
21127c478bd9Sstevel@tonic-gate  * ire_walk routine to delete or update any IRE_CACHE that might contain
21137c478bd9Sstevel@tonic-gate  * stale information.
21147c478bd9Sstevel@tonic-gate  * The flags state which entries to delete or update.
21157c478bd9Sstevel@tonic-gate  * Garbage collection is done separately using kmem alloc callbacks to
21167c478bd9Sstevel@tonic-gate  * ip_trash_ire_reclaim.
21177c478bd9Sstevel@tonic-gate  * Used for both IPv4 and IPv6. However, IPv6 only uses FLUSH_MTU_TIME
21187c478bd9Sstevel@tonic-gate  * since other stale information is cleaned up using NUD.
21197c478bd9Sstevel@tonic-gate  */
21207c478bd9Sstevel@tonic-gate void
21217c478bd9Sstevel@tonic-gate ire_expire(ire_t *ire, char *arg)
21227c478bd9Sstevel@tonic-gate {
2123f4b3ec61Sdh155122 	ire_expire_arg_t	*ieap = (ire_expire_arg_t *)(uintptr_t)arg;
21247c478bd9Sstevel@tonic-gate 	ill_t			*stq_ill;
2125f4b3ec61Sdh155122 	int			flush_flags = ieap->iea_flush_flag;
2126f4b3ec61Sdh155122 	ip_stack_t		*ipst = ieap->iea_ipst;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	if ((flush_flags & FLUSH_REDIRECT_TIME) &&
21296bdb8e66Sdd193516 	    (ire->ire_flags & RTF_DYNAMIC)) {
21307c478bd9Sstevel@tonic-gate 		/* Make sure we delete the corresponding IRE_CACHE */
21317c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_expire: all redirects\n"));
2132f4b3ec61Sdh155122 		ip_rts_rtmsg(RTM_DELETE, ire, 0, ipst);
21337c478bd9Sstevel@tonic-gate 		ire_delete(ire);
2134f4b3ec61Sdh155122 		atomic_dec_32(&ipst->ips_ip_redirect_cnt);
21357c478bd9Sstevel@tonic-gate 		return;
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
21387c478bd9Sstevel@tonic-gate 		return;
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	if (flush_flags & FLUSH_ARP_TIME) {
21417c478bd9Sstevel@tonic-gate 		/*
21427c478bd9Sstevel@tonic-gate 		 * Remove all IRE_CACHE.
21437c478bd9Sstevel@tonic-gate 		 * Verify that create time is more than
21447c478bd9Sstevel@tonic-gate 		 * ip_ire_arp_interval milliseconds ago.
21457c478bd9Sstevel@tonic-gate 		 */
2146f4b3ec61Sdh155122 		if (NCE_EXPIRED(ire->ire_nce, ipst)) {
21477c478bd9Sstevel@tonic-gate 			ire_delete(ire);
21487c478bd9Sstevel@tonic-gate 			return;
21497c478bd9Sstevel@tonic-gate 		}
21507c478bd9Sstevel@tonic-gate 	}
21517c478bd9Sstevel@tonic-gate 
2152f4b3ec61Sdh155122 	if (ipst->ips_ip_path_mtu_discovery && (flush_flags & FLUSH_MTU_TIME) &&
21537c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif != NULL)) {
21547c478bd9Sstevel@tonic-gate 		/* Increase pmtu if it is less than the interface mtu */
21557c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
21567c478bd9Sstevel@tonic-gate 		/*
21577c478bd9Sstevel@tonic-gate 		 * If the ipif is a vni (whose mtu is 0, since it's virtual)
21587c478bd9Sstevel@tonic-gate 		 * get the mtu from the sending interfaces' ipif
21597c478bd9Sstevel@tonic-gate 		 */
21607c478bd9Sstevel@tonic-gate 		if (IS_VNI(ire->ire_ipif->ipif_ill)) {
21617c478bd9Sstevel@tonic-gate 			stq_ill = ire->ire_stq->q_ptr;
21627c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(stq_ill->ill_ipif->ipif_mtu,
21637c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
21647c478bd9Sstevel@tonic-gate 		} else {
21657c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
21667c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
21677c478bd9Sstevel@tonic-gate 		}
21687c478bd9Sstevel@tonic-gate 		ire->ire_frag_flag |= IPH_DF;
21697c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
21707c478bd9Sstevel@tonic-gate 	}
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate /*
21747c478bd9Sstevel@tonic-gate  * Return any local address.  We use this to target ourselves
21757c478bd9Sstevel@tonic-gate  * when the src address was specified as 'default'.
21767c478bd9Sstevel@tonic-gate  * Preference for IRE_LOCAL entries.
21777c478bd9Sstevel@tonic-gate  */
21787c478bd9Sstevel@tonic-gate ire_t *
2179f4b3ec61Sdh155122 ire_lookup_local(zoneid_t zoneid, ip_stack_t *ipst)
21807c478bd9Sstevel@tonic-gate {
21817c478bd9Sstevel@tonic-gate 	ire_t	*ire;
21827c478bd9Sstevel@tonic-gate 	irb_t	*irb;
21837c478bd9Sstevel@tonic-gate 	ire_t	*maybe = NULL;
21847c478bd9Sstevel@tonic-gate 	int i;
21857c478bd9Sstevel@tonic-gate 
2186f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip_cache_table_size;  i++) {
2187f4b3ec61Sdh155122 		irb = &ipst->ips_ip_cache_table[i];
21887c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
21897c478bd9Sstevel@tonic-gate 			continue;
21907c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_READER);
21917c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
21927c478bd9Sstevel@tonic-gate 			if ((ire->ire_marks & IRE_MARK_CONDEMNED) ||
219345916cd2Sjpk 			    (ire->ire_zoneid != zoneid &&
219445916cd2Sjpk 			    ire->ire_zoneid != ALL_ZONES))
21957c478bd9Sstevel@tonic-gate 				continue;
21967c478bd9Sstevel@tonic-gate 			switch (ire->ire_type) {
21977c478bd9Sstevel@tonic-gate 			case IRE_LOOPBACK:
21987c478bd9Sstevel@tonic-gate 				if (maybe == NULL) {
21997c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
22007c478bd9Sstevel@tonic-gate 					maybe = ire;
22017c478bd9Sstevel@tonic-gate 				}
22027c478bd9Sstevel@tonic-gate 				break;
22037c478bd9Sstevel@tonic-gate 			case IRE_LOCAL:
22047c478bd9Sstevel@tonic-gate 				if (maybe != NULL) {
22057c478bd9Sstevel@tonic-gate 					ire_refrele(maybe);
22067c478bd9Sstevel@tonic-gate 				}
22077c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
22087c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
22097c478bd9Sstevel@tonic-gate 				return (ire);
22107c478bd9Sstevel@tonic-gate 			}
22117c478bd9Sstevel@tonic-gate 		}
22127c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 	return (maybe);
22157c478bd9Sstevel@tonic-gate }
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate /*
22187c478bd9Sstevel@tonic-gate  * If the specified IRE is associated with a particular ILL, return
22197c478bd9Sstevel@tonic-gate  * that ILL pointer (May be called as writer.).
22207c478bd9Sstevel@tonic-gate  *
22217c478bd9Sstevel@tonic-gate  * NOTE : This is not a generic function that can be used always.
22227c478bd9Sstevel@tonic-gate  * This function always returns the ill of the outgoing packets
22237c478bd9Sstevel@tonic-gate  * if this ire is used.
22247c478bd9Sstevel@tonic-gate  */
22257c478bd9Sstevel@tonic-gate ill_t *
222645916cd2Sjpk ire_to_ill(const ire_t *ire)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	ill_t *ill = NULL;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	/*
22317c478bd9Sstevel@tonic-gate 	 * 1) For an IRE_CACHE, ire_ipif is the one where it obtained
22327c478bd9Sstevel@tonic-gate 	 *    the source address from. ire_stq is the one where the
22337c478bd9Sstevel@tonic-gate 	 *    packets will be sent out on. We return that here.
22347c478bd9Sstevel@tonic-gate 	 *
22357c478bd9Sstevel@tonic-gate 	 * 2) IRE_BROADCAST normally has a loopback and a non-loopback
22367c478bd9Sstevel@tonic-gate 	 *    copy and they always exist next to each other with loopback
22377c478bd9Sstevel@tonic-gate 	 *    copy being the first one. If we are called on the non-loopback
22387c478bd9Sstevel@tonic-gate 	 *    copy, return the one pointed by ire_stq. If it was called on
22397c478bd9Sstevel@tonic-gate 	 *    a loopback copy, we still return the one pointed by the next
22407c478bd9Sstevel@tonic-gate 	 *    ire's ire_stq pointer i.e the one pointed by the non-loopback
22417c478bd9Sstevel@tonic-gate 	 *    copy. We don't want use ire_ipif as it might represent the
22427c478bd9Sstevel@tonic-gate 	 *    source address (if we borrow source addresses for
22437c478bd9Sstevel@tonic-gate 	 *    IRE_BROADCASTS in the future).
22447c478bd9Sstevel@tonic-gate 	 *    However if an interface is currently coming up, the above
22457c478bd9Sstevel@tonic-gate 	 *    condition may not hold during that period since the ires
22467c478bd9Sstevel@tonic-gate 	 *    are added one at a time. Thus one of the pair could have been
22477c478bd9Sstevel@tonic-gate 	 *    added and the other not yet added.
22482bc4236aSnordmark 	 * 3) For many other IREs (e.g., IRE_LOCAL), ire_rfq indicates the ill.
22492bc4236aSnordmark 	 * 4) For all others return the ones pointed by ire_ipif->ipif_ill.
22502bc4236aSnordmark 	 *    That handles IRE_LOOPBACK.
22517c478bd9Sstevel@tonic-gate 	 */
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
22547c478bd9Sstevel@tonic-gate 		ill = (ill_t *)ire->ire_stq->q_ptr;
22557c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_BROADCAST) {
22567c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
22577c478bd9Sstevel@tonic-gate 			ill = (ill_t *)ire->ire_stq->q_ptr;
22587c478bd9Sstevel@tonic-gate 		} else {
22597c478bd9Sstevel@tonic-gate 			ire_t  *ire_next;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 			ire_next = ire->ire_next;
22627c478bd9Sstevel@tonic-gate 			if (ire_next != NULL &&
22637c478bd9Sstevel@tonic-gate 			    ire_next->ire_type == IRE_BROADCAST &&
22647c478bd9Sstevel@tonic-gate 			    ire_next->ire_addr == ire->ire_addr &&
22657c478bd9Sstevel@tonic-gate 			    ire_next->ire_ipif == ire->ire_ipif) {
22667c478bd9Sstevel@tonic-gate 				ill = (ill_t *)ire_next->ire_stq->q_ptr;
22677c478bd9Sstevel@tonic-gate 			}
22687c478bd9Sstevel@tonic-gate 		}
22692bc4236aSnordmark 	} else if (ire->ire_rfq != NULL) {
22702bc4236aSnordmark 		ill = ire->ire_rfq->q_ptr;
22717c478bd9Sstevel@tonic-gate 	} else if (ire->ire_ipif != NULL) {
22727c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
22737c478bd9Sstevel@tonic-gate 	}
22747c478bd9Sstevel@tonic-gate 	return (ill);
22757c478bd9Sstevel@tonic-gate }
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate /* Arrange to call the specified function for every IRE in the world. */
22787c478bd9Sstevel@tonic-gate void
2279f4b3ec61Sdh155122 ire_walk(pfv_t func, void *arg, ip_stack_t *ipst)
22807c478bd9Sstevel@tonic-gate {
2281f4b3ec61Sdh155122 	ire_walk_ipvers(func, arg, 0, ALL_ZONES, ipst);
22827c478bd9Sstevel@tonic-gate }
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate void
2285f4b3ec61Sdh155122 ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
22867c478bd9Sstevel@tonic-gate {
2287f4b3ec61Sdh155122 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid, ipst);
22887c478bd9Sstevel@tonic-gate }
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate void
2291f4b3ec61Sdh155122 ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid, ip_stack_t *ipst)
22927c478bd9Sstevel@tonic-gate {
2293f4b3ec61Sdh155122 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid, ipst);
22947c478bd9Sstevel@tonic-gate }
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate /*
22977c478bd9Sstevel@tonic-gate  * Walk a particular version. version == 0 means both v4 and v6.
22987c478bd9Sstevel@tonic-gate  */
22997c478bd9Sstevel@tonic-gate static void
2300f4b3ec61Sdh155122 ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid,
2301f4b3ec61Sdh155122     ip_stack_t *ipst)
23027c478bd9Sstevel@tonic-gate {
23037c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
2304c793af95Ssangeeta 		/*
2305c793af95Ssangeeta 		 * ip_forwarding_table variable doesn't matter for IPv4 since
2306f4b3ec61Sdh155122 		 * ire_walk_ill_tables uses ips_ip_ftable for IPv4.
2307c793af95Ssangeeta 		 */
23087c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
2309c793af95Ssangeeta 		    0, NULL,
2310f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table_size, ipst->ips_ip_cache_table,
2311f4b3ec61Sdh155122 		    NULL, zoneid, ipst);
23127c478bd9Sstevel@tonic-gate 	}
23137c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
23147c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
2315f4b3ec61Sdh155122 		    ipst->ips_ip6_ftable_hash_size,
2316f4b3ec61Sdh155122 		    ipst->ips_ip_forwarding_table_v6,
2317f4b3ec61Sdh155122 		    ipst->ips_ip6_cache_table_size,
2318f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table_v6, NULL, zoneid, ipst);
23197c478bd9Sstevel@tonic-gate 	}
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate /*
23237c478bd9Sstevel@tonic-gate  * Arrange to call the specified
23247c478bd9Sstevel@tonic-gate  * function for every IRE that matches the ill.
23257c478bd9Sstevel@tonic-gate  */
23267c478bd9Sstevel@tonic-gate void
232745916cd2Sjpk ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
23287c478bd9Sstevel@tonic-gate     ill_t *ill)
23297c478bd9Sstevel@tonic-gate {
23307c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, 0, ill);
23317c478bd9Sstevel@tonic-gate }
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate void
233445916cd2Sjpk ire_walk_ill_v4(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
23357c478bd9Sstevel@tonic-gate     ill_t *ill)
23367c478bd9Sstevel@tonic-gate {
23377c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV4_VERSION,
23387c478bd9Sstevel@tonic-gate 	    ill);
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate void
234245916cd2Sjpk ire_walk_ill_v6(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
23437c478bd9Sstevel@tonic-gate     ill_t *ill)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV6_VERSION,
23467c478bd9Sstevel@tonic-gate 	    ill);
23477c478bd9Sstevel@tonic-gate }
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate /*
23507c478bd9Sstevel@tonic-gate  * Walk a particular ill and version. version == 0 means both v4 and v6.
23517c478bd9Sstevel@tonic-gate  */
23527c478bd9Sstevel@tonic-gate static void
23537c478bd9Sstevel@tonic-gate ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
235445916cd2Sjpk     void *arg, uchar_t vers, ill_t *ill)
23557c478bd9Sstevel@tonic-gate {
2356f4b3ec61Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
2357f4b3ec61Sdh155122 
23587c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
23597c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2360c793af95Ssangeeta 		    IP_MASK_TABLE_SIZE, 0,
2361f4b3ec61Sdh155122 		    NULL, ipst->ips_ip_cache_table_size,
2362f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table, ill, ALL_ZONES, ipst);
23637c478bd9Sstevel@tonic-gate 	}
23647c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
23657c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
2366f4b3ec61Sdh155122 		    IP6_MASK_TABLE_SIZE, ipst->ips_ip6_ftable_hash_size,
2367f4b3ec61Sdh155122 		    ipst->ips_ip_forwarding_table_v6,
2368f4b3ec61Sdh155122 		    ipst->ips_ip6_cache_table_size,
2369f4b3ec61Sdh155122 		    ipst->ips_ip_cache_table_v6, ill, ALL_ZONES, ipst);
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate }
23727c478bd9Sstevel@tonic-gate 
2373c793af95Ssangeeta boolean_t
23747c478bd9Sstevel@tonic-gate ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
2375f4b3ec61Sdh155122     ill_t *ill, zoneid_t zoneid, ip_stack_t *ipst)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	ill_t *ire_stq_ill = NULL;
23787c478bd9Sstevel@tonic-gate 	ill_t *ire_ipif_ill = NULL;
23797c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
23827c478bd9Sstevel@tonic-gate 	/*
23837c478bd9Sstevel@tonic-gate 	 * 1) MATCH_IRE_WQ : Used specifically to match on ire_stq.
23847c478bd9Sstevel@tonic-gate 	 *    The fast path update uses this to make sure it does not
23857c478bd9Sstevel@tonic-gate 	 *    update the fast path header of interface X with the fast
23867c478bd9Sstevel@tonic-gate 	 *    path updates it recieved on interface Y.  It is similar
23877c478bd9Sstevel@tonic-gate 	 *    in handling DL_NOTE_FASTPATH_FLUSH.
23887c478bd9Sstevel@tonic-gate 	 *
23897c478bd9Sstevel@tonic-gate 	 * 2) MATCH_IRE_ILL/MATCH_IRE_ILL_GROUP : We match both on ill
23907c478bd9Sstevel@tonic-gate 	 *    pointed by ire_stq and ire_ipif. Only in the case of
23917c478bd9Sstevel@tonic-gate 	 *    IRE_CACHEs can ire_stq and ire_ipif be pointing to
23927c478bd9Sstevel@tonic-gate 	 *    different ills. But we want to keep this function generic
23937c478bd9Sstevel@tonic-gate 	 *    enough for future use. So, we always try to match on both.
23947c478bd9Sstevel@tonic-gate 	 *    The only caller of this function ire_walk_ill_tables, will
23957c478bd9Sstevel@tonic-gate 	 *    call "func" after we return from this function. We expect
23967c478bd9Sstevel@tonic-gate 	 *    "func" to do the right filtering of ires in this case.
23977c478bd9Sstevel@tonic-gate 	 *
23987c478bd9Sstevel@tonic-gate 	 * NOTE : In the case of MATCH_IRE_ILL_GROUP, groups
23997c478bd9Sstevel@tonic-gate 	 * pointed by ire_stq and ire_ipif should always be the same.
24007c478bd9Sstevel@tonic-gate 	 * So, we just match on only one of them.
24017c478bd9Sstevel@tonic-gate 	 */
24027c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
24037c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL)
24047c478bd9Sstevel@tonic-gate 			ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
24057c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL)
24067c478bd9Sstevel@tonic-gate 			ire_ipif_ill = ire->ire_ipif->ipif_ill;
24077c478bd9Sstevel@tonic-gate 		if (ire_stq_ill != NULL)
24087c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_stq_ill->ill_group;
24097c478bd9Sstevel@tonic-gate 		if ((ire_ill_group == NULL) && (ire_ipif_ill != NULL))
24107c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ipif_ill->ill_group;
24117c478bd9Sstevel@tonic-gate 	}
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES) {
24147c478bd9Sstevel@tonic-gate 		/*
24157c478bd9Sstevel@tonic-gate 		 * We're walking the IREs for a specific zone. The only relevant
24167c478bd9Sstevel@tonic-gate 		 * IREs are:
24177c478bd9Sstevel@tonic-gate 		 * - all IREs with a matching ire_zoneid
24187c478bd9Sstevel@tonic-gate 		 * - all IRE_OFFSUBNETs as they're shared across all zones
24197c478bd9Sstevel@tonic-gate 		 * - IRE_INTERFACE IREs for interfaces with a usable source addr
24207c478bd9Sstevel@tonic-gate 		 *   with a matching zone
24217c478bd9Sstevel@tonic-gate 		 * - IRE_DEFAULTs with a gateway reachable from the zone
24227c478bd9Sstevel@tonic-gate 		 * We should really match on IRE_OFFSUBNETs and IRE_DEFAULTs
24237c478bd9Sstevel@tonic-gate 		 * using the same rule; but the above rules are consistent with
24247c478bd9Sstevel@tonic-gate 		 * the behavior of ire_ftable_lookup[_v6]() so that all the
24257c478bd9Sstevel@tonic-gate 		 * routes that can be matched during lookup are also matched
24267c478bd9Sstevel@tonic-gate 		 * here.
24277c478bd9Sstevel@tonic-gate 		 */
242845916cd2Sjpk 		if (zoneid != ire->ire_zoneid && ire->ire_zoneid != ALL_ZONES) {
24297c478bd9Sstevel@tonic-gate 			/*
24307c478bd9Sstevel@tonic-gate 			 * Note, IRE_INTERFACE can have the stq as NULL. For
24317c478bd9Sstevel@tonic-gate 			 * example, if the default multicast route is tied to
24327c478bd9Sstevel@tonic-gate 			 * the loopback address.
24337c478bd9Sstevel@tonic-gate 			 */
24347c478bd9Sstevel@tonic-gate 			if ((ire->ire_type & IRE_INTERFACE) &&
24357c478bd9Sstevel@tonic-gate 			    (ire->ire_stq != NULL)) {
24367c478bd9Sstevel@tonic-gate 				ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
24377c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV4_VERSION) {
24387c478bd9Sstevel@tonic-gate 					if (!ipif_usesrc_avail(ire_stq_ill,
24397c478bd9Sstevel@tonic-gate 					    zoneid))
24407c478bd9Sstevel@tonic-gate 						/* No usable src addr in zone */
24417c478bd9Sstevel@tonic-gate 						return (B_FALSE);
24427c478bd9Sstevel@tonic-gate 				} else if (ire_stq_ill->ill_usesrc_ifindex
24437c478bd9Sstevel@tonic-gate 				    != 0) {
24447c478bd9Sstevel@tonic-gate 					/*
24457c478bd9Sstevel@tonic-gate 					 * For IPv6 use ipif_select_source_v6()
24467c478bd9Sstevel@tonic-gate 					 * so the right scope selection is done
24477c478bd9Sstevel@tonic-gate 					 */
24487c478bd9Sstevel@tonic-gate 					ipif_t *src_ipif;
24497c478bd9Sstevel@tonic-gate 					src_ipif =
24507c478bd9Sstevel@tonic-gate 					    ipif_select_source_v6(ire_stq_ill,
2451309fa174Srk129064 					    &ire->ire_addr_v6, RESTRICT_TO_NONE,
24527c478bd9Sstevel@tonic-gate 					    IPV6_PREFER_SRC_DEFAULT,
24537c478bd9Sstevel@tonic-gate 					    zoneid);
24547c478bd9Sstevel@tonic-gate 					if (src_ipif != NULL) {
24557c478bd9Sstevel@tonic-gate 						ipif_refrele(src_ipif);
24567c478bd9Sstevel@tonic-gate 					} else {
24577c478bd9Sstevel@tonic-gate 						return (B_FALSE);
24587c478bd9Sstevel@tonic-gate 					}
24597c478bd9Sstevel@tonic-gate 				} else {
24607c478bd9Sstevel@tonic-gate 					return (B_FALSE);
24617c478bd9Sstevel@tonic-gate 				}
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 			} else if (!(ire->ire_type & IRE_OFFSUBNET)) {
24647c478bd9Sstevel@tonic-gate 				return (B_FALSE);
24657c478bd9Sstevel@tonic-gate 			}
24667c478bd9Sstevel@tonic-gate 		}
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 		/*
24697c478bd9Sstevel@tonic-gate 		 * Match all default routes from the global zone, irrespective
24705597b60aSnordmark 		 * of reachability. For a non-global zone only match those
24715597b60aSnordmark 		 * where ire_gateway_addr has a IRE_INTERFACE for the zoneid.
24727c478bd9Sstevel@tonic-gate 		 */
24737c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_DEFAULT && zoneid != GLOBAL_ZONEID) {
24747c478bd9Sstevel@tonic-gate 			int ire_match_flags = 0;
24757c478bd9Sstevel@tonic-gate 			in6_addr_t gw_addr_v6;
24767c478bd9Sstevel@tonic-gate 			ire_t *rire;
24777c478bd9Sstevel@tonic-gate 
24785597b60aSnordmark 			ire_match_flags |= MATCH_IRE_TYPE;
24797c478bd9Sstevel@tonic-gate 			if (ire->ire_ipif != NULL) {
24807c478bd9Sstevel@tonic-gate 				ire_match_flags |= MATCH_IRE_ILL_GROUP;
24817c478bd9Sstevel@tonic-gate 			}
24827c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
24837c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup(ire->ire_gateway_addr,
24845597b60aSnordmark 				    0, 0, IRE_INTERFACE, ire->ire_ipif, NULL,
2485f4b3ec61Sdh155122 				    zoneid, NULL, ire_match_flags, ipst);
24867c478bd9Sstevel@tonic-gate 			} else {
24877c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
24887c478bd9Sstevel@tonic-gate 				mutex_enter(&ire->ire_lock);
24897c478bd9Sstevel@tonic-gate 				gw_addr_v6 = ire->ire_gateway_addr_v6;
24907c478bd9Sstevel@tonic-gate 				mutex_exit(&ire->ire_lock);
24917c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup_v6(&gw_addr_v6,
24925597b60aSnordmark 				    NULL, NULL, IRE_INTERFACE, ire->ire_ipif,
2493f4b3ec61Sdh155122 				    NULL, zoneid, NULL, ire_match_flags, ipst);
24947c478bd9Sstevel@tonic-gate 			}
24957c478bd9Sstevel@tonic-gate 			if (rire == NULL) {
24967c478bd9Sstevel@tonic-gate 				return (B_FALSE);
24977c478bd9Sstevel@tonic-gate 			}
24987c478bd9Sstevel@tonic-gate 			ire_refrele(rire);
24997c478bd9Sstevel@tonic-gate 		}
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
25037c478bd9Sstevel@tonic-gate 		(ire->ire_type & ire_type)) &&
25047c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_WQ)) ||
25057c478bd9Sstevel@tonic-gate 		(ire->ire_stq == ill->ill_wq)) &&
25067c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
25077c478bd9Sstevel@tonic-gate 		(ire_stq_ill == ill || ire_ipif_ill == ill)) &&
25087c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
25097c478bd9Sstevel@tonic-gate 		(ire_stq_ill == ill) || (ire_ipif_ill == ill) ||
25107c478bd9Sstevel@tonic-gate 		(ire_ill_group != NULL &&
25117c478bd9Sstevel@tonic-gate 		ire_ill_group == ill->ill_group))) {
25127c478bd9Sstevel@tonic-gate 		return (B_TRUE);
25137c478bd9Sstevel@tonic-gate 	}
25147c478bd9Sstevel@tonic-gate 	return (B_FALSE);
25157c478bd9Sstevel@tonic-gate }
25167c478bd9Sstevel@tonic-gate 
2517c793af95Ssangeeta int
2518c793af95Ssangeeta rtfunc(struct radix_node *rn, void *arg)
2519c793af95Ssangeeta {
2520c793af95Ssangeeta 	struct rtfuncarg *rtf = arg;
2521c793af95Ssangeeta 	struct rt_entry *rt;
2522c793af95Ssangeeta 	irb_t *irb;
2523c793af95Ssangeeta 	ire_t *ire;
2524c793af95Ssangeeta 	boolean_t ret;
2525c793af95Ssangeeta 
2526c793af95Ssangeeta 	rt = (struct rt_entry *)rn;
2527c793af95Ssangeeta 	ASSERT(rt != NULL);
2528c793af95Ssangeeta 	irb = &rt->rt_irb;
2529c793af95Ssangeeta 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
2530c793af95Ssangeeta 		if ((rtf->rt_match_flags != 0) ||
2531c793af95Ssangeeta 		    (rtf->rt_zoneid != ALL_ZONES)) {
2532c793af95Ssangeeta 			ret = ire_walk_ill_match(rtf->rt_match_flags,
2533c793af95Ssangeeta 			    rtf->rt_ire_type, ire,
2534f4b3ec61Sdh155122 			    rtf->rt_ill, rtf->rt_zoneid, rtf->rt_ipst);
2535c793af95Ssangeeta 		} else
2536c793af95Ssangeeta 			ret = B_TRUE;
2537c793af95Ssangeeta 		if (ret)
2538c793af95Ssangeeta 			(*rtf->rt_func)(ire, rtf->rt_arg);
2539c793af95Ssangeeta 	}
2540c793af95Ssangeeta 	return (0);
2541c793af95Ssangeeta }
2542c793af95Ssangeeta 
25437c478bd9Sstevel@tonic-gate /*
25447c478bd9Sstevel@tonic-gate  * Walk the ftable and the ctable entries that match the ill.
25457c478bd9Sstevel@tonic-gate  */
2546c793af95Ssangeeta void
25477c478bd9Sstevel@tonic-gate ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
254845916cd2Sjpk     void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
2549f4b3ec61Sdh155122     size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid,
2550f4b3ec61Sdh155122     ip_stack_t *ipst)
25517c478bd9Sstevel@tonic-gate {
25527c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
25537c478bd9Sstevel@tonic-gate 	irb_t	*irb;
25547c478bd9Sstevel@tonic-gate 	ire_t	*ire;
25557c478bd9Sstevel@tonic-gate 	int i, j;
25567c478bd9Sstevel@tonic-gate 	boolean_t ret;
2557c793af95Ssangeeta 	struct rtfuncarg rtfarg;
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
25607c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
25617c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
25627c478bd9Sstevel@tonic-gate 	/*
25637c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the forwarding table if there
25647c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_FORWARDTABLE
25657c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
25667c478bd9Sstevel@tonic-gate 	 */
25677c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
25687c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_FORWARDTABLE) != 0)) {
2569c793af95Ssangeeta 		/* knobs such that routine is called only for v6 case */
2570f4b3ec61Sdh155122 		if (ipftbl == ipst->ips_ip_forwarding_table_v6) {
25717c478bd9Sstevel@tonic-gate 			for (i = (ftbl_sz - 1);  i >= 0; i--) {
25727c478bd9Sstevel@tonic-gate 				if ((irb_ptr = ipftbl[i]) == NULL)
25737c478bd9Sstevel@tonic-gate 					continue;
25747c478bd9Sstevel@tonic-gate 				for (j = 0; j < htbl_sz; j++) {
25757c478bd9Sstevel@tonic-gate 					irb = &irb_ptr[j];
25767c478bd9Sstevel@tonic-gate 					if (irb->irb_ire == NULL)
25777c478bd9Sstevel@tonic-gate 						continue;
2578c793af95Ssangeeta 
25797c478bd9Sstevel@tonic-gate 					IRB_REFHOLD(irb);
25807c478bd9Sstevel@tonic-gate 					for (ire = irb->irb_ire; ire != NULL;
25817c478bd9Sstevel@tonic-gate 						ire = ire->ire_next) {
25827c478bd9Sstevel@tonic-gate 						if (match_flags == 0 &&
25837c478bd9Sstevel@tonic-gate 						    zoneid == ALL_ZONES) {
25847c478bd9Sstevel@tonic-gate 							ret = B_TRUE;
25857c478bd9Sstevel@tonic-gate 						} else {
2586c793af95Ssangeeta 							ret =
2587c793af95Ssangeeta 							    ire_walk_ill_match(
2588c793af95Ssangeeta 							    match_flags,
2589c793af95Ssangeeta 							    ire_type, ire, ill,
2590f4b3ec61Sdh155122 							    zoneid, ipst);
25917c478bd9Sstevel@tonic-gate 						}
25927c478bd9Sstevel@tonic-gate 						if (ret)
25937c478bd9Sstevel@tonic-gate 							(*func)(ire, arg);
25947c478bd9Sstevel@tonic-gate 					}
25957c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb);
25967c478bd9Sstevel@tonic-gate 				}
25977c478bd9Sstevel@tonic-gate 			}
2598c793af95Ssangeeta 		} else {
2599c793af95Ssangeeta 			(void) memset(&rtfarg, 0, sizeof (rtfarg));
2600c793af95Ssangeeta 			rtfarg.rt_func = func;
2601c793af95Ssangeeta 			rtfarg.rt_arg = arg;
2602c793af95Ssangeeta 			if (match_flags != 0) {
2603c793af95Ssangeeta 				rtfarg.rt_match_flags = match_flags;
2604c793af95Ssangeeta 			}
2605c793af95Ssangeeta 			rtfarg.rt_ire_type = ire_type;
2606c793af95Ssangeeta 			rtfarg.rt_ill = ill;
2607c793af95Ssangeeta 			rtfarg.rt_zoneid = zoneid;
2608f4b3ec61Sdh155122 			rtfarg.rt_ipst = ipst;	/* No netstack_hold */
2609f4b3ec61Sdh155122 			(void) ipst->ips_ip_ftable->rnh_walktree_mt(
2610f4b3ec61Sdh155122 			    ipst->ips_ip_ftable,
2611f4b3ec61Sdh155122 			    rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
2612c793af95Ssangeeta 		}
26137c478bd9Sstevel@tonic-gate 	}
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	/*
26167c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the cache table if there
26177c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_CACHETABLE
26187c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
26197c478bd9Sstevel@tonic-gate 	 */
26207c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
26217c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_CACHETABLE) != 0)) {
26227c478bd9Sstevel@tonic-gate 		for (i = 0; i < ctbl_sz;  i++) {
26237c478bd9Sstevel@tonic-gate 			irb = &ipctbl[i];
26247c478bd9Sstevel@tonic-gate 			if (irb->irb_ire == NULL)
26257c478bd9Sstevel@tonic-gate 				continue;
26267c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
26277c478bd9Sstevel@tonic-gate 			for (ire = irb->irb_ire; ire != NULL;
26287c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
26297c478bd9Sstevel@tonic-gate 				if (match_flags == 0 && zoneid == ALL_ZONES) {
26307c478bd9Sstevel@tonic-gate 					ret = B_TRUE;
26317c478bd9Sstevel@tonic-gate 				} else {
26327c478bd9Sstevel@tonic-gate 					ret = ire_walk_ill_match(
26337c478bd9Sstevel@tonic-gate 					    match_flags, ire_type,
2634f4b3ec61Sdh155122 					    ire, ill, zoneid, ipst);
26357c478bd9Sstevel@tonic-gate 				}
26367c478bd9Sstevel@tonic-gate 				if (ret)
26377c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
26387c478bd9Sstevel@tonic-gate 			}
26397c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
26407c478bd9Sstevel@tonic-gate 		}
26417c478bd9Sstevel@tonic-gate 	}
26427c478bd9Sstevel@tonic-gate }
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate /*
26457c478bd9Sstevel@tonic-gate  * This routine walks through the ill chain to find if there is any
26467c478bd9Sstevel@tonic-gate  * ire linked to the ill's interface based forwarding table
26477c478bd9Sstevel@tonic-gate  * The arg could be ill or mp. This routine is called when a ill goes
26487c478bd9Sstevel@tonic-gate  * down/deleted or the 'ipv4_ire_srcif_status' report is printed.
26497c478bd9Sstevel@tonic-gate  */
26507c478bd9Sstevel@tonic-gate void
2651f4b3ec61Sdh155122 ire_walk_srcif_table_v4(pfv_t func, void *arg, ip_stack_t *ipst)
26527c478bd9Sstevel@tonic-gate {
26537c478bd9Sstevel@tonic-gate 	irb_t   *irb;
26547c478bd9Sstevel@tonic-gate 	ire_t   *ire;
26557c478bd9Sstevel@tonic-gate 	ill_t	*ill, *next_ill;
26567c478bd9Sstevel@tonic-gate 	int	i;
26577c478bd9Sstevel@tonic-gate 	int	total_count;
26587c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	/*
26617c478bd9Sstevel@tonic-gate 	 * Take care of ire's in other ill's per-interface forwarding
26627c478bd9Sstevel@tonic-gate 	 * table. Check if any ire in any of the ill's ill_srcif_table
26637c478bd9Sstevel@tonic-gate 	 * is pointing to this ill.
26647c478bd9Sstevel@tonic-gate 	 */
2665f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
2666f4b3ec61Sdh155122 	if (ipst->ips_ire_srcif_table_count == 0) {
2667f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_srcif_table_lock);
26687c478bd9Sstevel@tonic-gate 		return;
26697c478bd9Sstevel@tonic-gate 	}
2670f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ire_srcif_table_lock);
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate #ifdef DEBUG
26737c478bd9Sstevel@tonic-gate 	/* Keep accounting of all interface based table ires */
26747c478bd9Sstevel@tonic-gate 	total_count = 0;
2675f4b3ec61Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2676f4b3ec61Sdh155122 	ill = ILL_START_WALK_V4(&ctx, ipst);
26777c478bd9Sstevel@tonic-gate 	while (ill != NULL) {
26787c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
26797c478bd9Sstevel@tonic-gate 		total_count += ill->ill_srcif_refcnt;
26807c478bd9Sstevel@tonic-gate 		next_ill = ill_next(&ctx, ill);
26817c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
26827c478bd9Sstevel@tonic-gate 		ill = next_ill;
26837c478bd9Sstevel@tonic-gate 	}
2684f4b3ec61Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 	/* Hold lock here to make sure ire_srcif_table_count is stable */
2687f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
2688f4b3ec61Sdh155122 	i = ipst->ips_ire_srcif_table_count;
2689f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ire_srcif_table_lock);
26907c478bd9Sstevel@tonic-gate 	ip1dbg(("ire_walk_srcif_v4: ire_srcif_table_count %d "
26917c478bd9Sstevel@tonic-gate 	    "total ill_srcif_refcnt %d\n", i, total_count));
26927c478bd9Sstevel@tonic-gate #endif
2693f4b3ec61Sdh155122 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2694f4b3ec61Sdh155122 	ill = ILL_START_WALK_V4(&ctx, ipst);
26957c478bd9Sstevel@tonic-gate 	while (ill != NULL) {
26967c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
26977c478bd9Sstevel@tonic-gate 		if ((ill->ill_srcif_refcnt == 0) || !ILL_CAN_LOOKUP(ill)) {
26987c478bd9Sstevel@tonic-gate 			next_ill = ill_next(&ctx, ill);
26997c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
27007c478bd9Sstevel@tonic-gate 			ill = next_ill;
27017c478bd9Sstevel@tonic-gate 			continue;
27027c478bd9Sstevel@tonic-gate 		}
27037c478bd9Sstevel@tonic-gate 		ill_refhold_locked(ill);
27047c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
2705f4b3ec61Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
27067c478bd9Sstevel@tonic-gate 		if (ill->ill_srcif_table != NULL) {
27077c478bd9Sstevel@tonic-gate 			for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
27087c478bd9Sstevel@tonic-gate 				irb = &(ill->ill_srcif_table[i]);
27097c478bd9Sstevel@tonic-gate 				if (irb->irb_ire == NULL)
27107c478bd9Sstevel@tonic-gate 					continue;
27117c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(irb);
27127c478bd9Sstevel@tonic-gate 				for (ire = irb->irb_ire; ire != NULL;
27137c478bd9Sstevel@tonic-gate 				    ire = ire->ire_next) {
27147c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
27157c478bd9Sstevel@tonic-gate 				}
27167c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
27177c478bd9Sstevel@tonic-gate 			}
27187c478bd9Sstevel@tonic-gate 		}
2719f4b3ec61Sdh155122 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
27207c478bd9Sstevel@tonic-gate 		next_ill = ill_next(&ctx, ill);
27217c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
27227c478bd9Sstevel@tonic-gate 		ill = next_ill;
27237c478bd9Sstevel@tonic-gate 	}
2724f4b3ec61Sdh155122 	rw_exit(&ipst->ips_ill_g_lock);
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate /*
27287c478bd9Sstevel@tonic-gate  * This function takes a mask and returns
27297c478bd9Sstevel@tonic-gate  * number of bits set in the mask. If no
27307c478bd9Sstevel@tonic-gate  * bit is set it returns 0.
27317c478bd9Sstevel@tonic-gate  * Assumes a contiguous mask.
27327c478bd9Sstevel@tonic-gate  */
27337c478bd9Sstevel@tonic-gate int
27347c478bd9Sstevel@tonic-gate ip_mask_to_plen(ipaddr_t mask)
27357c478bd9Sstevel@tonic-gate {
27367c478bd9Sstevel@tonic-gate 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate /*
27407c478bd9Sstevel@tonic-gate  * Convert length for a mask to the mask.
27417c478bd9Sstevel@tonic-gate  */
27427c478bd9Sstevel@tonic-gate ipaddr_t
27437c478bd9Sstevel@tonic-gate ip_plen_to_mask(uint_t masklen)
27447c478bd9Sstevel@tonic-gate {
27457c478bd9Sstevel@tonic-gate 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate void
27497c478bd9Sstevel@tonic-gate ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
27507c478bd9Sstevel@tonic-gate {
27517c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
2752f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	ill_list[0] = ire->ire_stq != NULL ? ire->ire_stq->q_ptr : NULL;
27557c478bd9Sstevel@tonic-gate 	ill_list[1] = ire->ire_ipif != NULL ? ire->ire_ipif->ipif_ill : NULL;
27567c478bd9Sstevel@tonic-gate 	ill_list[2] = ire->ire_in_ill;
27577c478bd9Sstevel@tonic-gate 	ill_unlock_ills(ill_list, NUM_ILLS);
27587c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
2759f4b3ec61Sdh155122 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate /*
27637c478bd9Sstevel@tonic-gate  * ire_add_v[46] atomically make sure that the ipif or ill associated
27647c478bd9Sstevel@tonic-gate  * with the new ire being added is stable and not IPIF_CHANGING or ILL_CHANGING
27657c478bd9Sstevel@tonic-gate  * before adding the ire to the table. This ensures that we don't create
27667c478bd9Sstevel@tonic-gate  * new IRE_CACHEs with stale values for parameters that are passed to
27677c478bd9Sstevel@tonic-gate  * ire_create such as ire_max_frag. Note that ire_create() is passed a pointer
27687c478bd9Sstevel@tonic-gate  * to the ipif_mtu, and not the value. The actual value is derived from the
27697c478bd9Sstevel@tonic-gate  * parent ire or ipif under the bucket lock.
27707c478bd9Sstevel@tonic-gate  */
27717c478bd9Sstevel@tonic-gate int
27727c478bd9Sstevel@tonic-gate ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q, mblk_t *mp,
27737c478bd9Sstevel@tonic-gate     ipsq_func_t func)
27747c478bd9Sstevel@tonic-gate {
27757c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
27767c478bd9Sstevel@tonic-gate 	ill_t	*ipif_ill;
27777c478bd9Sstevel@tonic-gate 	ill_t	*in_ill;
27787c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
27797c478bd9Sstevel@tonic-gate 	int	cnt = NUM_ILLS;
27807c478bd9Sstevel@tonic-gate 	int	error = 0;
27817c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
2782f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	ill_list[0] = stq_ill = ire->ire_stq !=
27857c478bd9Sstevel@tonic-gate 		NULL ? ire->ire_stq->q_ptr : NULL;
27867c478bd9Sstevel@tonic-gate 	ill_list[1] = ipif_ill = ire->ire_ipif !=
27877c478bd9Sstevel@tonic-gate 		NULL ? ire->ire_ipif->ipif_ill : NULL;
27887c478bd9Sstevel@tonic-gate 	ill_list[2] = in_ill = ire->ire_in_ill;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	ASSERT((q != NULL && mp != NULL && func != NULL) ||
27917c478bd9Sstevel@tonic-gate 	    (q == NULL && mp == NULL && func == NULL));
2792f4b3ec61Sdh155122 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
27937c478bd9Sstevel@tonic-gate 	GRAB_CONN_LOCK(q);
27947c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
27957c478bd9Sstevel@tonic-gate 	ill_lock_ills(ill_list, cnt);
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	/*
27987c478bd9Sstevel@tonic-gate 	 * While the IRE is in the process of being added, a user may have
27997c478bd9Sstevel@tonic-gate 	 * invoked the ifconfig usesrc option on the stq_ill to make it a
28007c478bd9Sstevel@tonic-gate 	 * usesrc client ILL. Check for this possibility here, if it is true
28017c478bd9Sstevel@tonic-gate 	 * then we fail adding the IRE_CACHE. Another check is to make sure
28027c478bd9Sstevel@tonic-gate 	 * that an ipif_ill of an IRE_CACHE being added is not part of a usesrc
28037c478bd9Sstevel@tonic-gate 	 * group. The ill_g_usesrc_lock is released in ire_atomic_end
28047c478bd9Sstevel@tonic-gate 	 */
28057c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHE) &&
28067c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_USESRC_CHECK)) {
28077c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_usesrc_ifindex != 0) {
28087c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_usesrc_grp_next != NULL);
28097c478bd9Sstevel@tonic-gate 			if ((ipif_ill->ill_phyint->phyint_ifindex !=
28107c478bd9Sstevel@tonic-gate 			    stq_ill->ill_usesrc_ifindex) ||
28117c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_grp_next == NULL) ||
28127c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_ifindex != 0)) {
28137c478bd9Sstevel@tonic-gate 				error = EINVAL;
28147c478bd9Sstevel@tonic-gate 				goto done;
28157c478bd9Sstevel@tonic-gate 			}
28167c478bd9Sstevel@tonic-gate 		} else if (ipif_ill->ill_usesrc_grp_next != NULL) {
28177c478bd9Sstevel@tonic-gate 			error = EINVAL;
28187c478bd9Sstevel@tonic-gate 			goto done;
28197c478bd9Sstevel@tonic-gate 		}
28207c478bd9Sstevel@tonic-gate 	}
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	/*
28237c478bd9Sstevel@tonic-gate 	 * IPMP flag settings happen without taking the exclusive route
28247c478bd9Sstevel@tonic-gate 	 * in ip_sioctl_flags. So we need to make an atomic check here
28257c478bd9Sstevel@tonic-gate 	 * for FAILED/OFFLINE/INACTIVE flags or if it has hit the
28267c478bd9Sstevel@tonic-gate 	 * FAILBACK=no case.
28277c478bd9Sstevel@tonic-gate 	 */
28287c478bd9Sstevel@tonic-gate 	if ((stq_ill != NULL) && !IAM_WRITER_ILL(stq_ill)) {
28297c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_state_flags & ILL_CHANGING) {
28307c478bd9Sstevel@tonic-gate 			ill = stq_ill;
28317c478bd9Sstevel@tonic-gate 			error = EAGAIN;
28327c478bd9Sstevel@tonic-gate 		} else if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
28337c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
28347c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
28357c478bd9Sstevel@tonic-gate 			error = EINVAL;
28367c478bd9Sstevel@tonic-gate 		}
28377c478bd9Sstevel@tonic-gate 		goto done;
28387c478bd9Sstevel@tonic-gate 	}
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 	/*
28417c478bd9Sstevel@tonic-gate 	 * We don't check for OFFLINE/FAILED in this case because
28427c478bd9Sstevel@tonic-gate 	 * the source address selection logic (ipif_select_source)
28437c478bd9Sstevel@tonic-gate 	 * may still select a source address from such an ill. The
28447c478bd9Sstevel@tonic-gate 	 * assumption is that these addresses will be moved by in.mpathd
28457c478bd9Sstevel@tonic-gate 	 * soon. (i.e. this is a race). However link local addresses
28467c478bd9Sstevel@tonic-gate 	 * will not move and hence ipif_select_source_v6 tries to avoid
28477c478bd9Sstevel@tonic-gate 	 * FAILED ills. Please see ipif_select_source_v6 for more info
28487c478bd9Sstevel@tonic-gate 	 */
28497c478bd9Sstevel@tonic-gate 	if ((ipif_ill != NULL) && !IAM_WRITER_ILL(ipif_ill) &&
28507c478bd9Sstevel@tonic-gate 	    (ipif_ill->ill_state_flags & ILL_CHANGING)) {
28517c478bd9Sstevel@tonic-gate 		ill = ipif_ill;
28527c478bd9Sstevel@tonic-gate 		error = EAGAIN;
28537c478bd9Sstevel@tonic-gate 		goto done;
28547c478bd9Sstevel@tonic-gate 	}
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	if ((in_ill != NULL) && !IAM_WRITER_ILL(in_ill) &&
28577c478bd9Sstevel@tonic-gate 	    (in_ill->ill_state_flags & ILL_CHANGING)) {
28587c478bd9Sstevel@tonic-gate 		ill = in_ill;
28597c478bd9Sstevel@tonic-gate 		error = EAGAIN;
28607c478bd9Sstevel@tonic-gate 		goto done;
28617c478bd9Sstevel@tonic-gate 	}
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	if ((ire->ire_ipif != NULL) && !IAM_WRITER_IPIF(ire->ire_ipif) &&
28647c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif->ipif_state_flags & IPIF_CHANGING)) {
28657c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
28667c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
28677c478bd9Sstevel@tonic-gate 		error = EAGAIN;
28687c478bd9Sstevel@tonic-gate 		goto done;
28697c478bd9Sstevel@tonic-gate 	}
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate done:
28727c478bd9Sstevel@tonic-gate 	if (error == EAGAIN && ILL_CAN_WAIT(ill, q)) {
28737c478bd9Sstevel@tonic-gate 		ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
28747c478bd9Sstevel@tonic-gate 		mutex_enter(&ipsq->ipsq_lock);
28757c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
28767c478bd9Sstevel@tonic-gate 		ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
28777c478bd9Sstevel@tonic-gate 		mutex_exit(&ipsq->ipsq_lock);
28787c478bd9Sstevel@tonic-gate 		error = EINPROGRESS;
28797c478bd9Sstevel@tonic-gate 	} else if (error != 0) {
28807c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	RELEASE_CONN_LOCK(q);
28847c478bd9Sstevel@tonic-gate 	return (error);
28857c478bd9Sstevel@tonic-gate }
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate /*
28887c478bd9Sstevel@tonic-gate  * Add a fully initialized IRE to an appropriate table based on
28897c478bd9Sstevel@tonic-gate  * ire_type.
2890c793af95Ssangeeta  *
2891c793af95Ssangeeta  * allow_unresolved == B_FALSE indicates a legacy code-path call
2892c793af95Ssangeeta  * that has prohibited the addition of incomplete ire's. If this
2893c793af95Ssangeeta  * parameter is set, and we find an nce that is in a state other
2894c793af95Ssangeeta  * than ND_REACHABLE, we fail the add. Note that nce_state could be
2895*b9c344b3Ssowmini  * something other than ND_REACHABLE if the nce had just expired and
2896*b9c344b3Ssowmini  * the ire_create preceding the ire_add added a new ND_INITIAL nce.
28977c478bd9Sstevel@tonic-gate  */
28987c478bd9Sstevel@tonic-gate int
2899c793af95Ssangeeta ire_add(ire_t **irep, queue_t *q, mblk_t *mp, ipsq_func_t func,
2900c793af95Ssangeeta     boolean_t allow_unresolved)
29017c478bd9Sstevel@tonic-gate {
29027c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
29037c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
29047c478bd9Sstevel@tonic-gate 	ill_t	*ill;
29057c478bd9Sstevel@tonic-gate 	ipif_t	*ipif = NULL;
29067c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
29077c478bd9Sstevel@tonic-gate 	ire_t	*ire = *irep;
29087c478bd9Sstevel@tonic-gate 	int	error;
2909dc3879f9Sjarrett 	boolean_t ire_is_mblk = B_FALSE;
2910dc3879f9Sjarrett 	tsol_gcgrp_t *gcgrp = NULL;
2911dc3879f9Sjarrett 	tsol_gcgrp_addr_t ga;
2912f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN);
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	/* get ready for the day when original ire is not created as mblk */
29177c478bd9Sstevel@tonic-gate 	if (ire->ire_mp != NULL) {
2918dc3879f9Sjarrett 		ire_is_mblk = B_TRUE;
29197c478bd9Sstevel@tonic-gate 		/* Copy the ire to a kmem_alloc'ed area */
29207c478bd9Sstevel@tonic-gate 		ire1 = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
29217c478bd9Sstevel@tonic-gate 		if (ire1 == NULL) {
29227c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add: alloc failed\n"));
29237c478bd9Sstevel@tonic-gate 			ire_delete(ire);
29247c478bd9Sstevel@tonic-gate 			*irep = NULL;
29257c478bd9Sstevel@tonic-gate 			return (ENOMEM);
29267c478bd9Sstevel@tonic-gate 		}
2927c793af95Ssangeeta 		ire->ire_marks &= ~IRE_MARK_UNCACHED;
29287c478bd9Sstevel@tonic-gate 		*ire1 = *ire;
29297c478bd9Sstevel@tonic-gate 		ire1->ire_mp = NULL;
2930c793af95Ssangeeta 		ire1->ire_stq_ifindex = 0;
29317c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
29327c478bd9Sstevel@tonic-gate 		ire = ire1;
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
29357c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
29387c478bd9Sstevel@tonic-gate 		/*
29397c478bd9Sstevel@tonic-gate 		 * If this interface is FAILED, or INACTIVE or has hit
29407c478bd9Sstevel@tonic-gate 		 * the FAILBACK=no case, we create IRE_CACHES marked
29417c478bd9Sstevel@tonic-gate 		 * HIDDEN for some special cases e.g. bind to
29427c478bd9Sstevel@tonic-gate 		 * IPIF_NOFAILOVER address etc. So, if this interface
29437c478bd9Sstevel@tonic-gate 		 * is FAILED/INACTIVE/hit FAILBACK=no case, and we are
29447c478bd9Sstevel@tonic-gate 		 * not creating hidden ires, we should not allow that.
29457c478bd9Sstevel@tonic-gate 		 * This happens because the state of the interface
29467c478bd9Sstevel@tonic-gate 		 * changed while we were waiting in ARP. If this is the
29477c478bd9Sstevel@tonic-gate 		 * daemon sending probes, the next probe will create
29487c478bd9Sstevel@tonic-gate 		 * HIDDEN ires and we will create an ire then. This
29497c478bd9Sstevel@tonic-gate 		 * cannot happen with NDP currently because IRE is
29507c478bd9Sstevel@tonic-gate 		 * never queued in NDP. But it can happen in the
29517c478bd9Sstevel@tonic-gate 		 * future when we have external resolvers with IPv6.
29527c478bd9Sstevel@tonic-gate 		 * If the interface gets marked with OFFLINE while we
29537c478bd9Sstevel@tonic-gate 		 * are waiting in ARP, don't add the ire.
29547c478bd9Sstevel@tonic-gate 		 */
29557c478bd9Sstevel@tonic-gate 		if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
29567c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
29577c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
29587c478bd9Sstevel@tonic-gate 			/*
29597c478bd9Sstevel@tonic-gate 			 * We don't know whether it is a valid ipif or not.
29607c478bd9Sstevel@tonic-gate 			 * unless we do the check below. So, set it to NULL.
29617c478bd9Sstevel@tonic-gate 			 */
29627c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
29637c478bd9Sstevel@tonic-gate 			ire_delete(ire);
29647c478bd9Sstevel@tonic-gate 			*irep = NULL;
29657c478bd9Sstevel@tonic-gate 			return (EINVAL);
29667c478bd9Sstevel@tonic-gate 		}
29677c478bd9Sstevel@tonic-gate 	}
29687c478bd9Sstevel@tonic-gate 
29697c478bd9Sstevel@tonic-gate 	if (stq_ill != NULL && ire->ire_type == IRE_CACHE &&
29707c478bd9Sstevel@tonic-gate 	    stq_ill->ill_net_type == IRE_IF_RESOLVER) {
2971f4b3ec61Sdh155122 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2972f4b3ec61Sdh155122 		ill = ILL_START_WALK_ALL(&ctx, ipst);
29737c478bd9Sstevel@tonic-gate 		for (; ill != NULL; ill = ill_next(&ctx, ill)) {
29747c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
29757c478bd9Sstevel@tonic-gate 			if (ill->ill_state_flags & ILL_CONDEMNED) {
29767c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
29777c478bd9Sstevel@tonic-gate 				continue;
29787c478bd9Sstevel@tonic-gate 			}
29797c478bd9Sstevel@tonic-gate 			/*
29807c478bd9Sstevel@tonic-gate 			 * We need to make sure that the ipif is a valid one
29817c478bd9Sstevel@tonic-gate 			 * before adding the IRE_CACHE. This happens only
29827c478bd9Sstevel@tonic-gate 			 * with IRE_CACHE when there is an external resolver.
29837c478bd9Sstevel@tonic-gate 			 *
29847c478bd9Sstevel@tonic-gate 			 * We can unplumb a logical interface while the
29857c478bd9Sstevel@tonic-gate 			 * packet is waiting in ARP with the IRE. Then,
29867c478bd9Sstevel@tonic-gate 			 * later on when we feed the IRE back, the ipif
29877c478bd9Sstevel@tonic-gate 			 * has to be re-checked. This can't happen with
29887c478bd9Sstevel@tonic-gate 			 * NDP currently, as we never queue the IRE with
29897c478bd9Sstevel@tonic-gate 			 * the packet. We always try to recreate the IRE
29907c478bd9Sstevel@tonic-gate 			 * when the resolution is completed. But, we do
29917c478bd9Sstevel@tonic-gate 			 * it for IPv6 also here so that in future if
29927c478bd9Sstevel@tonic-gate 			 * we have external resolvers, it will work without
29937c478bd9Sstevel@tonic-gate 			 * any change.
29947c478bd9Sstevel@tonic-gate 			 */
29957c478bd9Sstevel@tonic-gate 			ipif = ipif_lookup_seqid(ill, ire->ire_ipif_seqid);
29967c478bd9Sstevel@tonic-gate 			if (ipif != NULL) {
29977c478bd9Sstevel@tonic-gate 				ipif_refhold_locked(ipif);
29987c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
29997c478bd9Sstevel@tonic-gate 				break;
30007c478bd9Sstevel@tonic-gate 			}
30017c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
30027c478bd9Sstevel@tonic-gate 		}
3003f4b3ec61Sdh155122 		rw_exit(&ipst->ips_ill_g_lock);
30047c478bd9Sstevel@tonic-gate 		if (ipif == NULL ||
30057c478bd9Sstevel@tonic-gate 		    (ipif->ipif_isv6 &&
30067c478bd9Sstevel@tonic-gate 		    !IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
30077c478bd9Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr)) ||
30087c478bd9Sstevel@tonic-gate 		    (!ipif->ipif_isv6 &&
30097c478bd9Sstevel@tonic-gate 		    ire->ire_src_addr != ipif->ipif_src_addr) ||
301045916cd2Sjpk 		    ire->ire_zoneid != ipif->ipif_zoneid) {
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 			if (ipif != NULL)
30137c478bd9Sstevel@tonic-gate 				ipif_refrele(ipif);
30147c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
30157c478bd9Sstevel@tonic-gate 			ire_delete(ire);
30167c478bd9Sstevel@tonic-gate 			*irep = NULL;
30177c478bd9Sstevel@tonic-gate 			return (EINVAL);
30187c478bd9Sstevel@tonic-gate 		}
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
30227c478bd9Sstevel@tonic-gate 		/*
30237c478bd9Sstevel@tonic-gate 		 * If this group was dismantled while this packets was
30247c478bd9Sstevel@tonic-gate 		 * queued in ARP, don't add it here.
30257c478bd9Sstevel@tonic-gate 		 */
30267c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif->ipif_ill->ill_group != ill->ill_group) {
30277c478bd9Sstevel@tonic-gate 			/* We don't want ire_inactive bump stats for this */
30287c478bd9Sstevel@tonic-gate 			ipif_refrele(ipif);
30297c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
30307c478bd9Sstevel@tonic-gate 			ire_delete(ire);
30317c478bd9Sstevel@tonic-gate 			*irep = NULL;
30327c478bd9Sstevel@tonic-gate 			return (EINVAL);
30337c478bd9Sstevel@tonic-gate 		}
3034dc3879f9Sjarrett 
3035dc3879f9Sjarrett 		/*
3036dc3879f9Sjarrett 		 * Since we didn't attach label security attributes to the
3037dc3879f9Sjarrett 		 * ire for the resolver case, we need to add it now. (only
3038dc3879f9Sjarrett 		 * for v4 resolver and v6 xresolv case).
3039dc3879f9Sjarrett 		 */
3040dc3879f9Sjarrett 		if (is_system_labeled() && ire_is_mblk) {
3041dc3879f9Sjarrett 			if (ire->ire_ipversion == IPV4_VERSION) {
3042dc3879f9Sjarrett 				ga.ga_af = AF_INET;
3043dc3879f9Sjarrett 				IN6_IPADDR_TO_V4MAPPED(ire->ire_gateway_addr !=
3044dc3879f9Sjarrett 				    INADDR_ANY ? ire->ire_gateway_addr :
3045dc3879f9Sjarrett 				    ire->ire_addr, &ga.ga_addr);
3046dc3879f9Sjarrett 			} else {
3047dc3879f9Sjarrett 				ga.ga_af = AF_INET6;
3048dc3879f9Sjarrett 				ga.ga_addr = IN6_IS_ADDR_UNSPECIFIED(
3049dc3879f9Sjarrett 				    &ire->ire_gateway_addr_v6) ?
3050dc3879f9Sjarrett 				    ire->ire_addr_v6 :
3051dc3879f9Sjarrett 				    ire->ire_gateway_addr_v6;
3052dc3879f9Sjarrett 			}
3053dc3879f9Sjarrett 			gcgrp = gcgrp_lookup(&ga, B_FALSE);
3054dc3879f9Sjarrett 			error = tsol_ire_init_gwattr(ire, ire->ire_ipversion,
3055dc3879f9Sjarrett 			    NULL, gcgrp);
3056dc3879f9Sjarrett 			if (error != 0) {
3057dc3879f9Sjarrett 				if (gcgrp != NULL) {
3058dc3879f9Sjarrett 					GCGRP_REFRELE(gcgrp);
3059dc3879f9Sjarrett 					gcgrp = NULL;
3060dc3879f9Sjarrett 				}
3061dc3879f9Sjarrett 				ipif_refrele(ipif);
3062dc3879f9Sjarrett 				ire->ire_ipif = NULL;
3063dc3879f9Sjarrett 				ire_delete(ire);
3064dc3879f9Sjarrett 				*irep = NULL;
3065dc3879f9Sjarrett 				return (error);
3066dc3879f9Sjarrett 			}
3067dc3879f9Sjarrett 		}
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	/*
30717c478bd9Sstevel@tonic-gate 	 * In case ire was changed
30727c478bd9Sstevel@tonic-gate 	 */
30737c478bd9Sstevel@tonic-gate 	*irep = ire;
30747c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
30757c478bd9Sstevel@tonic-gate 		error = ire_add_v6(irep, q, mp, func);
30767c478bd9Sstevel@tonic-gate 	} else {
30777c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill == NULL)
3078c793af95Ssangeeta 			error = ire_add_v4(irep, q, mp, func, allow_unresolved);
30797c478bd9Sstevel@tonic-gate 		else
30807c478bd9Sstevel@tonic-gate 			error = ire_add_srcif_v4(irep, q, mp, func);
30817c478bd9Sstevel@tonic-gate 	}
30827c478bd9Sstevel@tonic-gate 	if (ipif != NULL)
30837c478bd9Sstevel@tonic-gate 		ipif_refrele(ipif);
30847c478bd9Sstevel@tonic-gate 	return (error);
30857c478bd9Sstevel@tonic-gate }
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate /*
3088dc3879f9Sjarrett  * Add an initialized IRE to an appropriate table based on ire_type.
30897c478bd9Sstevel@tonic-gate  *
30906bdb8e66Sdd193516  * The forward table contains IRE_PREFIX/IRE_HOST and
30917c478bd9Sstevel@tonic-gate  * IRE_IF_RESOLVER/IRE_IF_NORESOLVER and IRE_DEFAULT.
30927c478bd9Sstevel@tonic-gate  *
30937c478bd9Sstevel@tonic-gate  * The cache table contains IRE_BROADCAST/IRE_LOCAL/IRE_LOOPBACK
30947c478bd9Sstevel@tonic-gate  * and IRE_CACHE.
30957c478bd9Sstevel@tonic-gate  *
30967c478bd9Sstevel@tonic-gate  * NOTE : This function is called as writer though not required
30977c478bd9Sstevel@tonic-gate  * by this function.
30987c478bd9Sstevel@tonic-gate  */
30997c478bd9Sstevel@tonic-gate static int
3100c793af95Ssangeeta ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func,
3101c793af95Ssangeeta     boolean_t allow_unresolved)
31027c478bd9Sstevel@tonic-gate {
31037c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
31047c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
31057c478bd9Sstevel@tonic-gate 	ire_t	**irep;
31067c478bd9Sstevel@tonic-gate 	int	flags;
31077c478bd9Sstevel@tonic-gate 	ire_t	*pire = NULL;
31087c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
31097c478bd9Sstevel@tonic-gate 	ire_t	*ire = *ire_p;
31107c478bd9Sstevel@tonic-gate 	int	error;
3111c793af95Ssangeeta 	boolean_t need_refrele = B_FALSE;
3112c793af95Ssangeeta 	nce_t	*nce;
3113f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
31167c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
31177c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
31187c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(
31197c478bd9Sstevel@tonic-gate 		    &((ill_t *)(ire->ire_stq->q_ptr))->ill_lock));
31207c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
31217c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_mp == NULL); /* Calls should go through ire_add */
31227c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill == NULL); /* No srcif entries */
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	/* Find the appropriate list head. */
31257c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
31267c478bd9Sstevel@tonic-gate 	case IRE_HOST:
31277c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
31287c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
31297c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
31307c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
31317c478bd9Sstevel@tonic-gate 		break;
31327c478bd9Sstevel@tonic-gate 	case IRE_CACHE:
31337c478bd9Sstevel@tonic-gate 	case IRE_BROADCAST:
31347c478bd9Sstevel@tonic-gate 	case IRE_LOCAL:
31357c478bd9Sstevel@tonic-gate 	case IRE_LOOPBACK:
31367c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
31377c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
31387c478bd9Sstevel@tonic-gate 		break;
31397c478bd9Sstevel@tonic-gate 	case IRE_PREFIX:
31407c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
31417c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
31427c478bd9Sstevel@tonic-gate 		break;
31437c478bd9Sstevel@tonic-gate 	case IRE_DEFAULT:
31447c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
31457c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
31467c478bd9Sstevel@tonic-gate 		break;
31477c478bd9Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
31487c478bd9Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
31497c478bd9Sstevel@tonic-gate 		break;
31507c478bd9Sstevel@tonic-gate 	default:
3151c793af95Ssangeeta 		ip0dbg(("ire_add_v4: ire %p has unrecognized IRE type (%d)\n",
3152c793af95Ssangeeta 		    (void *)ire, ire->ire_type));
31537c478bd9Sstevel@tonic-gate 		ire_delete(ire);
31547c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
31557c478bd9Sstevel@tonic-gate 		return (EINVAL);
31567c478bd9Sstevel@tonic-gate 	}
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
31597c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate 	/*
31627c478bd9Sstevel@tonic-gate 	 * ip_newroute/ip_newroute_multi are unable to prevent the deletion
31637c478bd9Sstevel@tonic-gate 	 * of the interface route while adding an IRE_CACHE for an on-link
31647c478bd9Sstevel@tonic-gate 	 * destination in the IRE_IF_RESOLVER case, since the ire has to
31657c478bd9Sstevel@tonic-gate 	 * go to ARP and return. We can't do a REFHOLD on the
31667c478bd9Sstevel@tonic-gate 	 * associated interface ire for fear of ARP freeing the message.
31677c478bd9Sstevel@tonic-gate 	 * Here we look up the interface ire in the forwarding table and
31687c478bd9Sstevel@tonic-gate 	 * make sure that the interface route has not been deleted.
31697c478bd9Sstevel@tonic-gate 	 */
31707c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE && ire->ire_gateway_addr == 0 &&
31717c478bd9Sstevel@tonic-gate 	    ((ill_t *)ire->ire_stq->q_ptr)->ill_net_type == IRE_IF_RESOLVER) {
3172c793af95Ssangeeta 
31737c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp == NULL);
31747c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr) && !(ire->ire_flags & RTF_SETSRC)) {
31757c478bd9Sstevel@tonic-gate 			/*
31767c478bd9Sstevel@tonic-gate 			 * The ihandle that we used in ip_newroute_multi
31777c478bd9Sstevel@tonic-gate 			 * comes from the interface route corresponding
31787c478bd9Sstevel@tonic-gate 			 * to ire_ipif. Lookup here to see if it exists
31797c478bd9Sstevel@tonic-gate 			 * still.
31807c478bd9Sstevel@tonic-gate 			 * If the ire has a source address assigned using
31817c478bd9Sstevel@tonic-gate 			 * RTF_SETSRC, ire_ipif is the logical interface holding
31827c478bd9Sstevel@tonic-gate 			 * this source address, so we can't use it to check for
31837c478bd9Sstevel@tonic-gate 			 * the existence of the interface route. Instead we rely
31847c478bd9Sstevel@tonic-gate 			 * on the brute force ihandle search in
31857c478bd9Sstevel@tonic-gate 			 * ire_ihandle_lookup_onlink() below.
31867c478bd9Sstevel@tonic-gate 			 */
31877c478bd9Sstevel@tonic-gate 			pire = ipif_to_ire(ire->ire_ipif);
31887c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
31897c478bd9Sstevel@tonic-gate 				ire_delete(ire);
31907c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
31917c478bd9Sstevel@tonic-gate 				return (EINVAL);
31927c478bd9Sstevel@tonic-gate 			} else if (pire->ire_ihandle != ire->ire_ihandle) {
31937c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
31947c478bd9Sstevel@tonic-gate 				ire_delete(ire);
31957c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
31967c478bd9Sstevel@tonic-gate 				return (EINVAL);
31977c478bd9Sstevel@tonic-gate 			}
31987c478bd9Sstevel@tonic-gate 		} else {
31997c478bd9Sstevel@tonic-gate 			pire = ire_ihandle_lookup_onlink(ire);
32007c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
32017c478bd9Sstevel@tonic-gate 				ire_delete(ire);
32027c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
32037c478bd9Sstevel@tonic-gate 				return (EINVAL);
32047c478bd9Sstevel@tonic-gate 			}
32057c478bd9Sstevel@tonic-gate 		}
32067c478bd9Sstevel@tonic-gate 		/* Prevent pire from getting deleted */
32077c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(pire->ire_bucket);
32087c478bd9Sstevel@tonic-gate 		/* Has it been removed already ? */
32097c478bd9Sstevel@tonic-gate 		if (pire->ire_marks & IRE_MARK_CONDEMNED) {
32107c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
32117c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
32127c478bd9Sstevel@tonic-gate 			ire_delete(ire);
32137c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
32147c478bd9Sstevel@tonic-gate 			return (EINVAL);
32157c478bd9Sstevel@tonic-gate 		}
32167c478bd9Sstevel@tonic-gate 	} else {
32177c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp != NULL);
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
32227c478bd9Sstevel@tonic-gate 		/*
32237c478bd9Sstevel@tonic-gate 		 * We use MATCH_IRE_IPIF while adding IRE_CACHES only
32247c478bd9Sstevel@tonic-gate 		 * for historic reasons and to maintain symmetry with
32257c478bd9Sstevel@tonic-gate 		 * IPv6 code path. Historically this was used by
32267c478bd9Sstevel@tonic-gate 		 * multicast code to create multiple IRE_CACHES on
32277c478bd9Sstevel@tonic-gate 		 * a single ill with different ipifs. This was used
32287c478bd9Sstevel@tonic-gate 		 * so that multicast packets leaving the node had the
32297c478bd9Sstevel@tonic-gate 		 * right source address. This is no longer needed as
32307c478bd9Sstevel@tonic-gate 		 * ip_wput initializes the address correctly.
32317c478bd9Sstevel@tonic-gate 		 */
32327c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_IPIF;
32337c478bd9Sstevel@tonic-gate 		/*
32347c478bd9Sstevel@tonic-gate 		 * If we are creating hidden ires, make sure we search on
32357c478bd9Sstevel@tonic-gate 		 * this ill (MATCH_IRE_ILL) and a hidden ire,
32367c478bd9Sstevel@tonic-gate 		 * while we are searching for duplicates below. Otherwise we
32377c478bd9Sstevel@tonic-gate 		 * could potentially find an IRE on some other interface
32387c478bd9Sstevel@tonic-gate 		 * and it may not be a IRE marked with IRE_MARK_HIDDEN. We
32397c478bd9Sstevel@tonic-gate 		 * shouldn't do this as this will lead to an infinite loop
32407c478bd9Sstevel@tonic-gate 		 * (if we get to ip_wput again) eventually we need an hidden
32417c478bd9Sstevel@tonic-gate 		 * ire for this packet to go out. MATCH_IRE_ILL is explicitly
32427c478bd9Sstevel@tonic-gate 		 * done below.
32437c478bd9Sstevel@tonic-gate 		 */
32447c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_CACHE &&
32457c478bd9Sstevel@tonic-gate 		    (ire->ire_marks & IRE_MARK_HIDDEN))
32467c478bd9Sstevel@tonic-gate 			flags |= (MATCH_IRE_MARK_HIDDEN);
32477c478bd9Sstevel@tonic-gate 	}
3248c793af95Ssangeeta 	if ((ire->ire_type & IRE_CACHETABLE) == 0) {
3249c793af95Ssangeeta 		irb_ptr = ire_get_bucket(ire);
3250c793af95Ssangeeta 		need_refrele = B_TRUE;
3251c793af95Ssangeeta 		if (irb_ptr == NULL) {
3252c793af95Ssangeeta 			/*
3253c793af95Ssangeeta 			 * This assumes that the ire has not added
3254c793af95Ssangeeta 			 * a reference to the ipif.
3255c793af95Ssangeeta 			 */
3256c793af95Ssangeeta 			ire->ire_ipif = NULL;
3257c793af95Ssangeeta 			ire_delete(ire);
3258c793af95Ssangeeta 			if (pire != NULL) {
3259c793af95Ssangeeta 				IRB_REFRELE(pire->ire_bucket);
3260c793af95Ssangeeta 				ire_refrele(pire);
3261c793af95Ssangeeta 			}
3262c793af95Ssangeeta 			*ire_p = NULL;
3263c793af95Ssangeeta 			return (EINVAL);
3264c793af95Ssangeeta 		}
3265c793af95Ssangeeta 	} else {
3266f4b3ec61Sdh155122 		irb_ptr = &(ipst->ips_ip_cache_table[IRE_ADDR_HASH(
3267f4b3ec61Sdh155122 		    ire->ire_addr, ipst->ips_ip_cache_table_size)]);
3268c793af95Ssangeeta 	}
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	/*
32717c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
32727c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock. Check for condemned
32737c478bd9Sstevel@tonic-gate 	 *
32747c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
32757c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
3276f4b3ec61Sdh155122 	 * To avoid lock order problems, get the ndp4->ndp_g_lock.
32777c478bd9Sstevel@tonic-gate 	 */
3278f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ndp4->ndp_g_lock);
32797c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
32807c478bd9Sstevel@tonic-gate 	if (error != 0) {
3281f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
32827c478bd9Sstevel@tonic-gate 		/*
32837c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
32847c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
32857c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
32867c478bd9Sstevel@tonic-gate 		 */
32877c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
32887c478bd9Sstevel@tonic-gate 		ire_delete(ire);
32897c478bd9Sstevel@tonic-gate 		if (pire != NULL) {
32907c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
32917c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
3294c793af95Ssangeeta 		if (need_refrele)
3295c793af95Ssangeeta 			IRB_REFRELE(irb_ptr);
32967c478bd9Sstevel@tonic-gate 		return (error);
32977c478bd9Sstevel@tonic-gate 	}
32987c478bd9Sstevel@tonic-gate 	/*
32997c478bd9Sstevel@tonic-gate 	 * To avoid creating ires having stale values for the ire_max_frag
33007c478bd9Sstevel@tonic-gate 	 * we get the latest value atomically here. For more details
33017c478bd9Sstevel@tonic-gate 	 * see the block comment in ip_sioctl_mtu and in DL_NOTE_SDU_CHANGE
33027c478bd9Sstevel@tonic-gate 	 * in ip_rput_dlpi_writer
33037c478bd9Sstevel@tonic-gate 	 */
33047c478bd9Sstevel@tonic-gate 	if (ire->ire_max_fragp == NULL) {
33057c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr))
33067c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
33077c478bd9Sstevel@tonic-gate 		else
33087c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = pire->ire_max_frag;
33097c478bd9Sstevel@tonic-gate 	} else {
33107c478bd9Sstevel@tonic-gate 		uint_t	max_frag;
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 		max_frag = *ire->ire_max_fragp;
33137c478bd9Sstevel@tonic-gate 		ire->ire_max_fragp = NULL;
33147c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = max_frag;
33157c478bd9Sstevel@tonic-gate 	}
33167c478bd9Sstevel@tonic-gate 	/*
33177c478bd9Sstevel@tonic-gate 	 * Atomically check for duplicate and insert in the table.
33187c478bd9Sstevel@tonic-gate 	 */
33197c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
33207c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
33217c478bd9Sstevel@tonic-gate 			continue;
33227c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL) {
33237c478bd9Sstevel@tonic-gate 			/*
33247c478bd9Sstevel@tonic-gate 			 * We do MATCH_IRE_ILL implicitly here for IREs
33257c478bd9Sstevel@tonic-gate 			 * with a non-null ire_ipif, including IRE_CACHEs.
33267c478bd9Sstevel@tonic-gate 			 * As ire_ipif and ire_stq could point to two
33277c478bd9Sstevel@tonic-gate 			 * different ills, we can't pass just ire_ipif to
33287c478bd9Sstevel@tonic-gate 			 * ire_match_args and get a match on both ills.
33297c478bd9Sstevel@tonic-gate 			 * This is just needed for duplicate checks here and
33307c478bd9Sstevel@tonic-gate 			 * so we don't add an extra argument to
33317c478bd9Sstevel@tonic-gate 			 * ire_match_args for this. Do it locally.
33327c478bd9Sstevel@tonic-gate 			 *
33337c478bd9Sstevel@tonic-gate 			 * NOTE : Currently there is no part of the code
33347c478bd9Sstevel@tonic-gate 			 * that asks for both MATH_IRE_IPIF and MATCH_IRE_ILL
33357c478bd9Sstevel@tonic-gate 			 * match for IRE_CACHEs. Thus we don't want to
33367c478bd9Sstevel@tonic-gate 			 * extend the arguments to ire_match_args.
33377c478bd9Sstevel@tonic-gate 			 */
33387c478bd9Sstevel@tonic-gate 			if (ire1->ire_stq != ire->ire_stq)
33397c478bd9Sstevel@tonic-gate 				continue;
33407c478bd9Sstevel@tonic-gate 			/*
33417c478bd9Sstevel@tonic-gate 			 * Multiroute IRE_CACHEs for a given destination can
33427c478bd9Sstevel@tonic-gate 			 * have the same ire_ipif, typically if their source
33437c478bd9Sstevel@tonic-gate 			 * address is forced using RTF_SETSRC, and the same
33447c478bd9Sstevel@tonic-gate 			 * send-to queue. We differentiate them using the parent
33457c478bd9Sstevel@tonic-gate 			 * handle.
33467c478bd9Sstevel@tonic-gate 			 */
33477c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_CACHE &&
33487c478bd9Sstevel@tonic-gate 			    (ire1->ire_flags & RTF_MULTIRT) &&
33497c478bd9Sstevel@tonic-gate 			    (ire->ire_flags & RTF_MULTIRT) &&
33507c478bd9Sstevel@tonic-gate 			    (ire1->ire_phandle != ire->ire_phandle))
33517c478bd9Sstevel@tonic-gate 				continue;
33527c478bd9Sstevel@tonic-gate 		}
33537c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
33547c478bd9Sstevel@tonic-gate 			continue;
33557c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
33567c478bd9Sstevel@tonic-gate 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ipif,
335745916cd2Sjpk 		    ire->ire_zoneid, 0, NULL, flags)) {
33587c478bd9Sstevel@tonic-gate 			/*
33597c478bd9Sstevel@tonic-gate 			 * Return the old ire after doing a REFHOLD.
33607c478bd9Sstevel@tonic-gate 			 * As most of the callers continue to use the IRE
33617c478bd9Sstevel@tonic-gate 			 * after adding, we return a held ire. This will
33627c478bd9Sstevel@tonic-gate 			 * avoid a lookup in the caller again. If the callers
33637c478bd9Sstevel@tonic-gate 			 * don't want to use it, they need to do a REFRELE.
33647c478bd9Sstevel@tonic-gate 			 */
33657c478bd9Sstevel@tonic-gate 			ip1dbg(("found dup ire existing %p new %p",
33667c478bd9Sstevel@tonic-gate 			    (void *)ire1, (void *)ire));
33677c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
33687c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
3369f4b3ec61Sdh155122 			mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
33707c478bd9Sstevel@tonic-gate 			ire_delete(ire);
33717c478bd9Sstevel@tonic-gate 			if (pire != NULL) {
33727c478bd9Sstevel@tonic-gate 				/*
33737c478bd9Sstevel@tonic-gate 				 * Assert that it is not removed from the
33747c478bd9Sstevel@tonic-gate 				 * list yet.
33757c478bd9Sstevel@tonic-gate 				 */
33767c478bd9Sstevel@tonic-gate 				ASSERT(pire->ire_ptpn != NULL);
33777c478bd9Sstevel@tonic-gate 				IRB_REFRELE(pire->ire_bucket);
33787c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
33797c478bd9Sstevel@tonic-gate 			}
33807c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
3381c793af95Ssangeeta 			if (need_refrele)
3382c793af95Ssangeeta 				IRB_REFRELE(irb_ptr);
33837c478bd9Sstevel@tonic-gate 			return (0);
33847c478bd9Sstevel@tonic-gate 		}
33857c478bd9Sstevel@tonic-gate 	}
3386c793af95Ssangeeta 	if (ire->ire_type & IRE_CACHE) {
3387c793af95Ssangeeta 		ASSERT(ire->ire_stq != NULL);
3388c793af95Ssangeeta 		nce = ndp_lookup_v4(ire_to_ill(ire),
3389c793af95Ssangeeta 		    ((ire->ire_gateway_addr != INADDR_ANY) ?
3390c793af95Ssangeeta 		    &ire->ire_gateway_addr : &ire->ire_addr),
3391c793af95Ssangeeta 		    B_TRUE);
3392c793af95Ssangeeta 		if (nce != NULL)
3393c793af95Ssangeeta 			mutex_enter(&nce->nce_lock);
3394c793af95Ssangeeta 		/*
3395c793af95Ssangeeta 		 * if the nce is NCE_F_CONDEMNED, or if it is not ND_REACHABLE
3396c793af95Ssangeeta 		 * and the caller has prohibited the addition of incomplete
3397c793af95Ssangeeta 		 * ire's, we fail the add. Note that nce_state could be
3398*b9c344b3Ssowmini 		 * something other than ND_REACHABLE if the nce had
3399*b9c344b3Ssowmini 		 * just expired and the ire_create preceding the
3400*b9c344b3Ssowmini 		 * ire_add added a new ND_INITIAL nce.
3401c793af95Ssangeeta 		 */
3402c793af95Ssangeeta 		if ((nce == NULL) ||
3403c793af95Ssangeeta 		    (nce->nce_flags & NCE_F_CONDEMNED) ||
3404c793af95Ssangeeta 		    (!allow_unresolved &&
3405107875b0Ssangeeta 		    (nce->nce_state != ND_REACHABLE))) {
3406*b9c344b3Ssowmini 			if (nce != NULL) {
3407*b9c344b3Ssowmini 				DTRACE_PROBE1(ire__bad__nce, nce_t *, nce);
3408c793af95Ssangeeta 				mutex_exit(&nce->nce_lock);
3409*b9c344b3Ssowmini 			}
3410c793af95Ssangeeta 			ire_atomic_end(irb_ptr, ire);
3411f4b3ec61Sdh155122 			mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
3412c793af95Ssangeeta 			if (nce != NULL)
3413c793af95Ssangeeta 				NCE_REFRELE(nce);
3414c793af95Ssangeeta 			DTRACE_PROBE1(ire__no__nce, ire_t *, ire);
3415c793af95Ssangeeta 			ire_delete(ire);
3416c793af95Ssangeeta 			if (pire != NULL) {
3417c793af95Ssangeeta 				IRB_REFRELE(pire->ire_bucket);
3418c793af95Ssangeeta 				ire_refrele(pire);
3419c793af95Ssangeeta 			}
3420c793af95Ssangeeta 			*ire_p = NULL;
3421c793af95Ssangeeta 			if (need_refrele)
3422c793af95Ssangeeta 				IRB_REFRELE(irb_ptr);
3423c793af95Ssangeeta 			return (EINVAL);
3424c793af95Ssangeeta 		} else {
3425c793af95Ssangeeta 			ire->ire_nce = nce;
3426c793af95Ssangeeta 			mutex_exit(&nce->nce_lock);
3427c793af95Ssangeeta 			/*
3428c793af95Ssangeeta 			 * We are associating this nce to the ire, so
3429c793af95Ssangeeta 			 * change the nce ref taken in ndp_lookup_v4() from
3430c793af95Ssangeeta 			 * NCE_REFHOLD to NCE_REFHOLD_NOTR
3431c793af95Ssangeeta 			 */
3432c793af95Ssangeeta 			NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
3433c793af95Ssangeeta 		}
3434c793af95Ssangeeta 	}
34357c478bd9Sstevel@tonic-gate 	/*
34367c478bd9Sstevel@tonic-gate 	 * Make it easy for ip_wput_ire() to hit multiple broadcast ires by
34377c478bd9Sstevel@tonic-gate 	 * grouping identical addresses together on the hash chain. We also
34387c478bd9Sstevel@tonic-gate 	 * don't want to send multiple copies out if there are two ills part
34397c478bd9Sstevel@tonic-gate 	 * of the same group. Thus we group the ires with same addr and same
34407c478bd9Sstevel@tonic-gate 	 * ill group together so that ip_wput_ire can easily skip all the
34417c478bd9Sstevel@tonic-gate 	 * ires with same addr and same group after sending the first copy.
34427c478bd9Sstevel@tonic-gate 	 * We do this only for IRE_BROADCASTs as ip_wput_ire is currently
34437c478bd9Sstevel@tonic-gate 	 * interested in such groupings only for broadcasts.
34447c478bd9Sstevel@tonic-gate 	 *
34457c478bd9Sstevel@tonic-gate 	 * NOTE : If the interfaces are brought up first and then grouped,
34467c478bd9Sstevel@tonic-gate 	 * illgrp_insert will handle it. We come here when the interfaces
34477c478bd9Sstevel@tonic-gate 	 * are already in group and we are bringing them UP.
34487c478bd9Sstevel@tonic-gate 	 *
34497c478bd9Sstevel@tonic-gate 	 * Find the first entry that matches ire_addr. *irep will be null
34507c478bd9Sstevel@tonic-gate 	 * if no match.
34517c478bd9Sstevel@tonic-gate 	 */
34527c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
34537c478bd9Sstevel@tonic-gate 	while ((ire1 = *irep) != NULL && ire->ire_addr != ire1->ire_addr)
34547c478bd9Sstevel@tonic-gate 		irep = &ire1->ire_next;
34557c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_BROADCAST && *irep != NULL) {
34567c478bd9Sstevel@tonic-gate 		/*
34577c478bd9Sstevel@tonic-gate 		 * We found some ire (i.e *irep) with a matching addr. We
34587c478bd9Sstevel@tonic-gate 		 * want to group ires with same addr and same ill group
34597c478bd9Sstevel@tonic-gate 		 * together.
34607c478bd9Sstevel@tonic-gate 		 *
34617c478bd9Sstevel@tonic-gate 		 * First get to the entry that matches our address and
34627c478bd9Sstevel@tonic-gate 		 * ill group i.e stop as soon as we find the first ire
34637c478bd9Sstevel@tonic-gate 		 * matching the ill group and address. If there is only
34647c478bd9Sstevel@tonic-gate 		 * an address match, we should walk and look for some
34657c478bd9Sstevel@tonic-gate 		 * group match. These are some of the possible scenarios :
34667c478bd9Sstevel@tonic-gate 		 *
34677c478bd9Sstevel@tonic-gate 		 * 1) There are no groups at all i.e all ire's ill_group
34687c478bd9Sstevel@tonic-gate 		 *    are NULL. In that case we will essentially group
34697c478bd9Sstevel@tonic-gate 		 *    all the ires with the same addr together. Same as
34707c478bd9Sstevel@tonic-gate 		 *    the "else" block of this "if".
34717c478bd9Sstevel@tonic-gate 		 *
34727c478bd9Sstevel@tonic-gate 		 * 2) There are some groups and this ire's ill_group is
34737c478bd9Sstevel@tonic-gate 		 *    NULL. In this case, we will first find the group
34747c478bd9Sstevel@tonic-gate 		 *    that matches the address and a NULL group. Then
34757c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
34767c478bd9Sstevel@tonic-gate 		 *
34777c478bd9Sstevel@tonic-gate 		 * 3) There are some groups and this ires's ill_group is
34787c478bd9Sstevel@tonic-gate 		 *    non-NULL. In this case we will first find the group
34797c478bd9Sstevel@tonic-gate 		 *    that matches the address and the ill_group. Then
34807c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
34817c478bd9Sstevel@tonic-gate 		 */
34827c478bd9Sstevel@tonic-gate 		/* LINTED : constant in conditional context */
34837c478bd9Sstevel@tonic-gate 		while (1) {
34847c478bd9Sstevel@tonic-gate 			ire1 = *irep;
34857c478bd9Sstevel@tonic-gate 			if ((ire1->ire_next == NULL) ||
34867c478bd9Sstevel@tonic-gate 			    (ire1->ire_next->ire_addr != ire->ire_addr) ||
34877c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
34887c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group ==
34897c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
34907c478bd9Sstevel@tonic-gate 				break;
34917c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
34927c478bd9Sstevel@tonic-gate 		}
34937c478bd9Sstevel@tonic-gate 		ASSERT(*irep != NULL);
34947c478bd9Sstevel@tonic-gate 		irep = &((*irep)->ire_next);
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 		/*
34977c478bd9Sstevel@tonic-gate 		 * Either we have hit the end of the list or the address
34987c478bd9Sstevel@tonic-gate 		 * did not match or the group *matched*. If we found
34997c478bd9Sstevel@tonic-gate 		 * a match on the group, skip to the end of the group.
35007c478bd9Sstevel@tonic-gate 		 */
35017c478bd9Sstevel@tonic-gate 		while (*irep != NULL) {
35027c478bd9Sstevel@tonic-gate 			ire1 = *irep;
35037c478bd9Sstevel@tonic-gate 			if ((ire1->ire_addr != ire->ire_addr) ||
35047c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
35057c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group !=
35067c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
35077c478bd9Sstevel@tonic-gate 				break;
35087c478bd9Sstevel@tonic-gate 			if (ire1->ire_ipif->ipif_ill->ill_group == NULL &&
35097c478bd9Sstevel@tonic-gate 			    ire1->ire_ipif == ire->ire_ipif) {
35107c478bd9Sstevel@tonic-gate 				irep = &ire1->ire_next;
35117c478bd9Sstevel@tonic-gate 				break;
35127c478bd9Sstevel@tonic-gate 			}
35137c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
35147c478bd9Sstevel@tonic-gate 		}
35157c478bd9Sstevel@tonic-gate 	} else if (*irep != NULL) {
35167c478bd9Sstevel@tonic-gate 		/*
35177c478bd9Sstevel@tonic-gate 		 * Find the last ire which matches ire_addr.
35187c478bd9Sstevel@tonic-gate 		 * Needed to do tail insertion among entries with the same
35197c478bd9Sstevel@tonic-gate 		 * ire_addr.
35207c478bd9Sstevel@tonic-gate 		 */
35217c478bd9Sstevel@tonic-gate 		while (ire->ire_addr == ire1->ire_addr) {
35227c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
35237c478bd9Sstevel@tonic-gate 			ire1 = *irep;
35247c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
35257c478bd9Sstevel@tonic-gate 				break;
35267c478bd9Sstevel@tonic-gate 		}
35277c478bd9Sstevel@tonic-gate 	}
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate 	/* Insert at *irep */
35307c478bd9Sstevel@tonic-gate 	ire1 = *irep;
35317c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
35327c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
35337c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
35347c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
35357c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 	/*
35387c478bd9Sstevel@tonic-gate 	 * ire_walk routines de-reference ire_next without holding
35397c478bd9Sstevel@tonic-gate 	 * a lock. Before we point to the new ire, we want to make
35407c478bd9Sstevel@tonic-gate 	 * sure the store that sets the ire_next of the new ire
35417c478bd9Sstevel@tonic-gate 	 * reaches global visibility, so that ire_walk routines
35427c478bd9Sstevel@tonic-gate 	 * don't see a truncated list of ires i.e if the ire_next
35437c478bd9Sstevel@tonic-gate 	 * of the new ire gets set after we do "*irep = ire" due
35447c478bd9Sstevel@tonic-gate 	 * to re-ordering, the ire_walk thread will see a NULL
35457c478bd9Sstevel@tonic-gate 	 * once it accesses the ire_next of the new ire.
35467c478bd9Sstevel@tonic-gate 	 * membar_producer() makes sure that the following store
35477c478bd9Sstevel@tonic-gate 	 * happens *after* all of the above stores.
35487c478bd9Sstevel@tonic-gate 	 */
35497c478bd9Sstevel@tonic-gate 	membar_producer();
35507c478bd9Sstevel@tonic-gate 	*irep = ire;
35517c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
35527c478bd9Sstevel@tonic-gate 	/*
35537c478bd9Sstevel@tonic-gate 	 * We return a bumped up IRE above. Keep it symmetrical
35547c478bd9Sstevel@tonic-gate 	 * so that the callers will always have to release. This
35557c478bd9Sstevel@tonic-gate 	 * helps the callers of this function because they continue
35567c478bd9Sstevel@tonic-gate 	 * to use the IRE after adding and hence they don't have to
35577c478bd9Sstevel@tonic-gate 	 * lookup again after we return the IRE.
35587c478bd9Sstevel@tonic-gate 	 *
35597c478bd9Sstevel@tonic-gate 	 * NOTE : We don't have to use atomics as this is appearing
35607c478bd9Sstevel@tonic-gate 	 * in the list for the first time and no one else can bump
35617c478bd9Sstevel@tonic-gate 	 * up the reference count on this yet.
35627c478bd9Sstevel@tonic-gate 	 */
35637c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
3564f4b3ec61Sdh155122 	BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
3565c793af95Ssangeeta 
35667c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
3567c793af95Ssangeeta 	if (irb_ptr->irb_marks & IRB_MARK_FTABLE)
3568c793af95Ssangeeta 		irb_ptr->irb_nire++;
3569c793af95Ssangeeta 
35707c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
35717c478bd9Sstevel@tonic-gate 		irb_ptr->irb_tmp_ire_cnt++;
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
35747c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
35757c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
35767c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
35777c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
35787c478bd9Sstevel@tonic-gate 		}
35797c478bd9Sstevel@tonic-gate 	} else {
35807c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
35817c478bd9Sstevel@tonic-gate 	}
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
3584f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 	if (pire != NULL) {
35877c478bd9Sstevel@tonic-gate 		/* Assert that it is not removed from the list yet */
35887c478bd9Sstevel@tonic-gate 		ASSERT(pire->ire_ptpn != NULL);
35897c478bd9Sstevel@tonic-gate 		IRB_REFRELE(pire->ire_bucket);
35907c478bd9Sstevel@tonic-gate 		ire_refrele(pire);
35917c478bd9Sstevel@tonic-gate 	}
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE) {
35947c478bd9Sstevel@tonic-gate 		/*
3595c793af95Ssangeeta 		 * For ire's with host mask see if there is an entry
35967c478bd9Sstevel@tonic-gate 		 * in the cache. If there is one flush the whole cache as
35977c478bd9Sstevel@tonic-gate 		 * there might be multiple entries due to RTF_MULTIRT (CGTP).
35987c478bd9Sstevel@tonic-gate 		 * If no entry is found than there is no need to flush the
35997c478bd9Sstevel@tonic-gate 		 * cache.
36007c478bd9Sstevel@tonic-gate 		 */
36017c478bd9Sstevel@tonic-gate 		if (ire->ire_mask == IP_HOST_MASK) {
36027c478bd9Sstevel@tonic-gate 			ire_t *lire;
36037c478bd9Sstevel@tonic-gate 			lire = ire_ctable_lookup(ire->ire_addr, NULL, IRE_CACHE,
3604f4b3ec61Sdh155122 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
36057c478bd9Sstevel@tonic-gate 			if (lire != NULL) {
36067c478bd9Sstevel@tonic-gate 				ire_refrele(lire);
36077c478bd9Sstevel@tonic-gate 				ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
36087c478bd9Sstevel@tonic-gate 			}
36097c478bd9Sstevel@tonic-gate 		} else {
36107c478bd9Sstevel@tonic-gate 			ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
36117c478bd9Sstevel@tonic-gate 		}
36127c478bd9Sstevel@tonic-gate 	}
36137c478bd9Sstevel@tonic-gate 	/*
36147c478bd9Sstevel@tonic-gate 	 * We had to delay the fast path probe until the ire is inserted
36157c478bd9Sstevel@tonic-gate 	 * in the list. Otherwise the fast path ack won't find the ire in
36167c478bd9Sstevel@tonic-gate 	 * the table.
36177c478bd9Sstevel@tonic-gate 	 */
3618516bda92Ssowmini 	if (ire->ire_type == IRE_CACHE ||
3619516bda92Ssowmini 	    (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL)) {
3620516bda92Ssowmini 		ASSERT(ire->ire_nce != NULL);
3621516bda92Ssowmini 		nce_fastpath(ire->ire_nce);
3622516bda92Ssowmini 	}
36237c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
36247c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
36257c478bd9Sstevel@tonic-gate 	*ire_p = ire;
3626c793af95Ssangeeta 	if (need_refrele) {
3627c793af95Ssangeeta 		IRB_REFRELE(irb_ptr);
3628c793af95Ssangeeta 	}
36297c478bd9Sstevel@tonic-gate 	return (0);
36307c478bd9Sstevel@tonic-gate }
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate /*
36337c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. ire_unlink calls to
36347c478bd9Sstevel@tonic-gate  * do the final cleanup for this ire.
36357c478bd9Sstevel@tonic-gate  */
36367c478bd9Sstevel@tonic-gate void
36377c478bd9Sstevel@tonic-gate ire_cleanup(ire_t *ire)
36387c478bd9Sstevel@tonic-gate {
36397c478bd9Sstevel@tonic-gate 	ire_t *ire_next;
3640f4b3ec61Sdh155122 	ip_stack_t *ipst = ire->ire_ipst;
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	while (ire != NULL) {
36457c478bd9Sstevel@tonic-gate 		ire_next = ire->ire_next;
36467c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
36477c478bd9Sstevel@tonic-gate 			ire_delete_v4(ire);
3648f4b3ec61Sdh155122 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
3649f4b3ec61Sdh155122 			    ire_stats_deleted);
36507c478bd9Sstevel@tonic-gate 		} else {
36517c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
36527c478bd9Sstevel@tonic-gate 			ire_delete_v6(ire);
3653f4b3ec61Sdh155122 			BUMP_IRE_STATS(ipst->ips_ire_stats_v6,
3654f4b3ec61Sdh155122 			    ire_stats_deleted);
36557c478bd9Sstevel@tonic-gate 		}
36567c478bd9Sstevel@tonic-gate 		/*
36577c478bd9Sstevel@tonic-gate 		 * Now it's really out of the list. Before doing the
36587c478bd9Sstevel@tonic-gate 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
36597c478bd9Sstevel@tonic-gate 		 * so.
36607c478bd9Sstevel@tonic-gate 		 */
36617c478bd9Sstevel@tonic-gate 		ire->ire_next = NULL;
36627c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
36637c478bd9Sstevel@tonic-gate 		ire = ire_next;
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate }
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate /*
36687c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. It calls to unlink
36697c478bd9Sstevel@tonic-gate  * all the CONDEMNED ires from this bucket.
36707c478bd9Sstevel@tonic-gate  */
36717c478bd9Sstevel@tonic-gate ire_t *
36727c478bd9Sstevel@tonic-gate ire_unlink(irb_t *irb)
36737c478bd9Sstevel@tonic-gate {
36747c478bd9Sstevel@tonic-gate 	ire_t *ire;
36757c478bd9Sstevel@tonic-gate 	ire_t *ire1;
36767c478bd9Sstevel@tonic-gate 	ire_t **ptpn;
36777c478bd9Sstevel@tonic-gate 	ire_t *ire_list = NULL;
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
3680c793af95Ssangeeta 	ASSERT(((irb->irb_marks & IRB_MARK_FTABLE) && irb->irb_refcnt == 1) ||
3681c793af95Ssangeeta 	    (irb->irb_refcnt == 0));
3682c793af95Ssangeeta 	ASSERT(irb->irb_marks & IRB_MARK_CONDEMNED);
36837c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_ire != NULL);
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
3686f4b3ec61Sdh155122 		ip_stack_t	*ipst = ire->ire_ipst;
3687f4b3ec61Sdh155122 
36887c478bd9Sstevel@tonic-gate 		ire1 = ire->ire_next;
36897c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
36907c478bd9Sstevel@tonic-gate 			ptpn = ire->ire_ptpn;
36917c478bd9Sstevel@tonic-gate 			ire1 = ire->ire_next;
36927c478bd9Sstevel@tonic-gate 			if (ire1)
36937c478bd9Sstevel@tonic-gate 				ire1->ire_ptpn = ptpn;
36947c478bd9Sstevel@tonic-gate 			*ptpn = ire1;
36957c478bd9Sstevel@tonic-gate 			ire->ire_ptpn = NULL;
36967c478bd9Sstevel@tonic-gate 			ire->ire_next = NULL;
36977c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_DEFAULT) {
36987c478bd9Sstevel@tonic-gate 				/*
36997c478bd9Sstevel@tonic-gate 				 * IRE is out of the list. We need to adjust
37007c478bd9Sstevel@tonic-gate 				 * the accounting before the caller drops
37017c478bd9Sstevel@tonic-gate 				 * the lock.
37027c478bd9Sstevel@tonic-gate 				 */
37037c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV6_VERSION) {
3704f4b3ec61Sdh155122 					ASSERT(ipst->
3705f4b3ec61Sdh155122 					    ips_ipv6_ire_default_count !=
3706f4b3ec61Sdh155122 					    0);
3707f4b3ec61Sdh155122 					ipst->ips_ipv6_ire_default_count--;
37087c478bd9Sstevel@tonic-gate 				}
37097c478bd9Sstevel@tonic-gate 			}
37107c478bd9Sstevel@tonic-gate 			/*
37117c478bd9Sstevel@tonic-gate 			 * We need to call ire_delete_v4 or ire_delete_v6
37127c478bd9Sstevel@tonic-gate 			 * to clean up the cache or the redirects pointing at
37137c478bd9Sstevel@tonic-gate 			 * the default gateway. We need to drop the lock
37147c478bd9Sstevel@tonic-gate 			 * as ire_flush_cache/ire_delete_host_redircts require
37157c478bd9Sstevel@tonic-gate 			 * so. But we can't drop the lock, as ire_unlink needs
37167c478bd9Sstevel@tonic-gate 			 * to atomically remove the ires from the list.
37177c478bd9Sstevel@tonic-gate 			 * So, create a temporary list of CONDEMNED ires
37187c478bd9Sstevel@tonic-gate 			 * for doing ire_delete_v4/ire_delete_v6 operations
37197c478bd9Sstevel@tonic-gate 			 * later on.
37207c478bd9Sstevel@tonic-gate 			 */
37217c478bd9Sstevel@tonic-gate 			ire->ire_next = ire_list;
37227c478bd9Sstevel@tonic-gate 			ire_list = ire;
37237c478bd9Sstevel@tonic-gate 		}
37247c478bd9Sstevel@tonic-gate 	}
3725c793af95Ssangeeta 	irb->irb_marks &= ~IRB_MARK_CONDEMNED;
37267c478bd9Sstevel@tonic-gate 	return (ire_list);
37277c478bd9Sstevel@tonic-gate }
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate /*
37307c478bd9Sstevel@tonic-gate  * Delete all the cache entries with this 'addr'.  When IP gets a gratuitous
3731c793af95Ssangeeta  * ARP message on any of its interface queue, it scans the nce table and
3732c793af95Ssangeeta  * deletes and calls ndp_delete() for the appropriate nce. This action
3733c793af95Ssangeeta  * also deletes all the neighbor/ire cache entries for that address.
3734c793af95Ssangeeta  * This function is called from ip_arp_news in ip.c and also for
3735c793af95Ssangeeta  * ARP ioctl processing in ip_if.c. ip_ire_clookup_and_delete returns
3736c793af95Ssangeeta  * true if it finds a nce entry which is used by ip_arp_news to determine if
3737c793af95Ssangeeta  * it needs to do an ire_walk_v4. The return value is also  used for the
3738c793af95Ssangeeta  * same purpose by ARP IOCTL processing * in ip_if.c when deleting
3739c793af95Ssangeeta  * ARP entries. For SIOC*IFARP ioctls in addition to the address,
3740c793af95Ssangeeta  * ip_if->ipif_ill also needs to be matched.
37417c478bd9Sstevel@tonic-gate  */
37427c478bd9Sstevel@tonic-gate boolean_t
3743f4b3ec61Sdh155122 ip_ire_clookup_and_delete(ipaddr_t addr, ipif_t *ipif, ip_stack_t *ipst)
37447c478bd9Sstevel@tonic-gate {
37457c478bd9Sstevel@tonic-gate 	ill_t	*ill;
3746c793af95Ssangeeta 	nce_t	*nce;
37477c478bd9Sstevel@tonic-gate 
3748c793af95Ssangeeta 	ill = (ipif ? ipif->ipif_ill : NULL);
37497c478bd9Sstevel@tonic-gate 
3750c793af95Ssangeeta 	if (ill != NULL) {
3751c793af95Ssangeeta 		/*
3752c793af95Ssangeeta 		 * clean up the nce (and any relevant ire's) that matches
3753c793af95Ssangeeta 		 * on addr and ill.
3754c793af95Ssangeeta 		 */
3755c793af95Ssangeeta 		nce = ndp_lookup_v4(ill, &addr, B_FALSE);
3756c793af95Ssangeeta 		if (nce != NULL) {
3757c793af95Ssangeeta 			ndp_delete(nce);
3758c793af95Ssangeeta 			return (B_TRUE);
37597c478bd9Sstevel@tonic-gate 		}
3760c793af95Ssangeeta 	} else {
3761c793af95Ssangeeta 		/*
3762c793af95Ssangeeta 		 * ill is wildcard. clean up all nce's and
3763c793af95Ssangeeta 		 * ire's that match on addr
3764c793af95Ssangeeta 		 */
3765c793af95Ssangeeta 		nce_clookup_t cl;
37667c478bd9Sstevel@tonic-gate 
3767c793af95Ssangeeta 		cl.ncecl_addr = addr;
3768c793af95Ssangeeta 		cl.ncecl_found = B_FALSE;
37697c478bd9Sstevel@tonic-gate 
3770f4b3ec61Sdh155122 		ndp_walk_common(ipst->ips_ndp4, NULL,
3771c793af95Ssangeeta 		    (pfi_t)ip_nce_clookup_and_delete, (uchar_t *)&cl, B_TRUE);
3772c793af95Ssangeeta 
3773c793af95Ssangeeta 		/*
3774c793af95Ssangeeta 		 *  ncecl_found would be set by ip_nce_clookup_and_delete if
3775c793af95Ssangeeta 		 *  we found a matching nce.
3776c793af95Ssangeeta 		 */
3777c793af95Ssangeeta 		return (cl.ncecl_found);
3778c793af95Ssangeeta 	}
3779c793af95Ssangeeta 	return (B_FALSE);
3780c793af95Ssangeeta 
3781c793af95Ssangeeta }
3782c793af95Ssangeeta 
3783c793af95Ssangeeta /* Delete the supplied nce if its nce_addr matches the supplied address */
3784c793af95Ssangeeta static void
3785c793af95Ssangeeta ip_nce_clookup_and_delete(nce_t *nce, void *arg)
3786c793af95Ssangeeta {
3787c793af95Ssangeeta 	nce_clookup_t *cl = (nce_clookup_t *)arg;
3788c793af95Ssangeeta 	ipaddr_t nce_addr;
3789c793af95Ssangeeta 
3790c793af95Ssangeeta 	IN6_V4MAPPED_TO_IPADDR(&nce->nce_addr, nce_addr);
3791c793af95Ssangeeta 	if (nce_addr == cl->ncecl_addr) {
3792c793af95Ssangeeta 		cl->ncecl_found = B_TRUE;
3793c793af95Ssangeeta 		/* clean up the nce (and any relevant ire's) */
3794c793af95Ssangeeta 		ndp_delete(nce);
3795c793af95Ssangeeta 	}
3796c793af95Ssangeeta }
3797c793af95Ssangeeta 
3798c793af95Ssangeeta /*
3799c793af95Ssangeeta  * Clean up the radix node for this ire. Must be called by IRB_REFRELE
3800c793af95Ssangeeta  * when there are no ire's left in the bucket. Returns TRUE if the bucket
3801c793af95Ssangeeta  * is deleted and freed.
3802c793af95Ssangeeta  */
3803c793af95Ssangeeta boolean_t
3804c793af95Ssangeeta irb_inactive(irb_t *irb)
3805c793af95Ssangeeta {
3806c793af95Ssangeeta 	struct rt_entry *rt;
3807c793af95Ssangeeta 	struct radix_node *rn;
3808f4b3ec61Sdh155122 	ip_stack_t *ipst = irb->irb_ipst;
3809f4b3ec61Sdh155122 
3810f4b3ec61Sdh155122 	ASSERT(irb->irb_ipst != NULL);
3811c793af95Ssangeeta 
3812c793af95Ssangeeta 	rt = IRB2RT(irb);
3813c793af95Ssangeeta 	rn = (struct radix_node *)rt;
3814c793af95Ssangeeta 
3815c793af95Ssangeeta 	/* first remove it from the radix tree. */
3816f4b3ec61Sdh155122 	RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
3817c793af95Ssangeeta 	rw_enter(&irb->irb_lock, RW_WRITER);
3818c793af95Ssangeeta 	if (irb->irb_refcnt == 1 && irb->irb_nire == 0) {
3819f4b3ec61Sdh155122 		rn = ipst->ips_ip_ftable->rnh_deladdr(rn->rn_key, rn->rn_mask,
3820f4b3ec61Sdh155122 		    ipst->ips_ip_ftable);
3821c793af95Ssangeeta 		DTRACE_PROBE1(irb__free, rt_t *,  rt);
3822c793af95Ssangeeta 		ASSERT((void *)rn == (void *)rt);
3823c793af95Ssangeeta 		Free(rt, rt_entry_cache);
3824c793af95Ssangeeta 		/* irb_lock is freed */
3825f4b3ec61Sdh155122 		RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
3826c793af95Ssangeeta 		return (B_TRUE);
3827c793af95Ssangeeta 	}
3828c793af95Ssangeeta 	rw_exit(&irb->irb_lock);
3829f4b3ec61Sdh155122 	RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
3830c793af95Ssangeeta 	return (B_FALSE);
38317c478bd9Sstevel@tonic-gate }
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate /*
38347c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
38357c478bd9Sstevel@tonic-gate  */
38367c478bd9Sstevel@tonic-gate void
38377c478bd9Sstevel@tonic-gate ire_delete(ire_t *ire)
38387c478bd9Sstevel@tonic-gate {
38397c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
38407c478bd9Sstevel@tonic-gate 	ire_t	**ptpn;
38417c478bd9Sstevel@tonic-gate 	irb_t *irb;
3842f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
38437c478bd9Sstevel@tonic-gate 
3844c793af95Ssangeeta 	if ((irb = ire->ire_bucket) == NULL) {
38457c478bd9Sstevel@tonic-gate 		/*
38467c478bd9Sstevel@tonic-gate 		 * It was never inserted in the list. Should call REFRELE
38477c478bd9Sstevel@tonic-gate 		 * to free this IRE.
38487c478bd9Sstevel@tonic-gate 		 */
38497c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
38507c478bd9Sstevel@tonic-gate 		return;
38517c478bd9Sstevel@tonic-gate 	}
38527c478bd9Sstevel@tonic-gate 
38537c478bd9Sstevel@tonic-gate 	rw_enter(&irb->irb_lock, RW_WRITER);
38547c478bd9Sstevel@tonic-gate 
3855c793af95Ssangeeta 	if (irb->irb_rr_origin == ire) {
3856c793af95Ssangeeta 		irb->irb_rr_origin = NULL;
3857c793af95Ssangeeta 	}
3858c793af95Ssangeeta 
38597c478bd9Sstevel@tonic-gate 	/*
38607c478bd9Sstevel@tonic-gate 	 * In case of V4 we might still be waiting for fastpath ack.
38617c478bd9Sstevel@tonic-gate 	 */
3862516bda92Ssowmini 	if (ire->ire_ipversion == IPV4_VERSION &&
3863516bda92Ssowmini 	    (ire->ire_type == IRE_CACHE ||
3864516bda92Ssowmini 	    (ire->ire_type == IRE_BROADCAST && ire->ire_stq != NULL))) {
3865516bda92Ssowmini 		ASSERT(ire->ire_nce != NULL);
3866516bda92Ssowmini 		nce_fastpath_list_delete(ire->ire_nce);
38677c478bd9Sstevel@tonic-gate 	}
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 	if (ire->ire_ptpn == NULL) {
38707c478bd9Sstevel@tonic-gate 		/*
38717c478bd9Sstevel@tonic-gate 		 * Some other thread has removed us from the list.
38727c478bd9Sstevel@tonic-gate 		 * It should have done the REFRELE for us.
38737c478bd9Sstevel@tonic-gate 		 */
38747c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
38757c478bd9Sstevel@tonic-gate 		return;
38767c478bd9Sstevel@tonic-gate 	}
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 	if (irb->irb_refcnt != 0) {
38797c478bd9Sstevel@tonic-gate 		/*
38807c478bd9Sstevel@tonic-gate 		 * The last thread to leave this bucket will
38817c478bd9Sstevel@tonic-gate 		 * delete this ire.
38827c478bd9Sstevel@tonic-gate 		 */
38837c478bd9Sstevel@tonic-gate 		if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) {
38847c478bd9Sstevel@tonic-gate 			irb->irb_ire_cnt--;
38857c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY)
38867c478bd9Sstevel@tonic-gate 				irb->irb_tmp_ire_cnt--;
38877c478bd9Sstevel@tonic-gate 			ire->ire_marks |= IRE_MARK_CONDEMNED;
38887c478bd9Sstevel@tonic-gate 		}
3889c793af95Ssangeeta 		irb->irb_marks |= IRB_MARK_CONDEMNED;
38907c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
38917c478bd9Sstevel@tonic-gate 		return;
38927c478bd9Sstevel@tonic-gate 	}
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 	/*
38957c478bd9Sstevel@tonic-gate 	 * Normally to delete an ire, we walk the bucket. While we
38967c478bd9Sstevel@tonic-gate 	 * walk the bucket, we normally bump up irb_refcnt and hence
38977c478bd9Sstevel@tonic-gate 	 * we return from above where we mark CONDEMNED and the ire
38987c478bd9Sstevel@tonic-gate 	 * gets deleted from ire_unlink. This case is where somebody
38997c478bd9Sstevel@tonic-gate 	 * knows the ire e.g by doing a lookup, and wants to delete the
39007c478bd9Sstevel@tonic-gate 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
39017c478bd9Sstevel@tonic-gate 	 * the bucket.
39027c478bd9Sstevel@tonic-gate 	 */
39037c478bd9Sstevel@tonic-gate 	ptpn = ire->ire_ptpn;
39047c478bd9Sstevel@tonic-gate 	ire1 = ire->ire_next;
39057c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
39067c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = ptpn;
39077c478bd9Sstevel@tonic-gate 	ASSERT(ptpn != NULL);
39087c478bd9Sstevel@tonic-gate 	*ptpn = ire1;
39097c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = NULL;
39107c478bd9Sstevel@tonic-gate 	ire->ire_next = NULL;
39117c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
3912f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_deleted);
39137c478bd9Sstevel@tonic-gate 	} else {
3914f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_deleted);
39157c478bd9Sstevel@tonic-gate 	}
39167c478bd9Sstevel@tonic-gate 	/*
39177c478bd9Sstevel@tonic-gate 	 * ip_wput/ip_wput_v6 checks this flag to see whether
39187c478bd9Sstevel@tonic-gate 	 * it should still use the cached ire or not.
39197c478bd9Sstevel@tonic-gate 	 */
39207c478bd9Sstevel@tonic-gate 	ire->ire_marks |= IRE_MARK_CONDEMNED;
39217c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
39227c478bd9Sstevel@tonic-gate 		/*
39237c478bd9Sstevel@tonic-gate 		 * IRE is out of the list. We need to adjust the
39247c478bd9Sstevel@tonic-gate 		 * accounting before we drop the lock.
39257c478bd9Sstevel@tonic-gate 		 */
39267c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV6_VERSION) {
3927f4b3ec61Sdh155122 			ASSERT(ipst->ips_ipv6_ire_default_count != 0);
3928f4b3ec61Sdh155122 			ipst->ips_ipv6_ire_default_count--;
39297c478bd9Sstevel@tonic-gate 		}
39307c478bd9Sstevel@tonic-gate 	}
39317c478bd9Sstevel@tonic-gate 	irb->irb_ire_cnt--;
3932c793af95Ssangeeta 
39337c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
39347c478bd9Sstevel@tonic-gate 		irb->irb_tmp_ire_cnt--;
39357c478bd9Sstevel@tonic-gate 	rw_exit(&irb->irb_lock);
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
39387c478bd9Sstevel@tonic-gate 		ire_delete_v6(ire);
39397c478bd9Sstevel@tonic-gate 	} else {
39407c478bd9Sstevel@tonic-gate 		ire_delete_v4(ire);
39417c478bd9Sstevel@tonic-gate 	}
39427c478bd9Sstevel@tonic-gate 	/*
39437c478bd9Sstevel@tonic-gate 	 * We removed it from the list. Decrement the
39447c478bd9Sstevel@tonic-gate 	 * reference count.
39457c478bd9Sstevel@tonic-gate 	 */
39467c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
39477c478bd9Sstevel@tonic-gate }
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate /*
39507c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
39517c478bd9Sstevel@tonic-gate  * All calls should use ire_delete().
39527c478bd9Sstevel@tonic-gate  * Sometimes called as writer though not required by this function.
39537c478bd9Sstevel@tonic-gate  *
39547c478bd9Sstevel@tonic-gate  * NOTE : This function is called only if the ire was added
39557c478bd9Sstevel@tonic-gate  * in the list.
39567c478bd9Sstevel@tonic-gate  */
39577c478bd9Sstevel@tonic-gate static void
39587c478bd9Sstevel@tonic-gate ire_delete_v4(ire_t *ire)
39597c478bd9Sstevel@tonic-gate {
3960f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
3961f4b3ec61Sdh155122 
39627c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt >= 1);
39637c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
39667c478bd9Sstevel@tonic-gate 		ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
39677c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
39687c478bd9Sstevel@tonic-gate 		/*
39697c478bd9Sstevel@tonic-gate 		 * when a default gateway is going away
39707c478bd9Sstevel@tonic-gate 		 * delete all the host redirects pointing at that
39717c478bd9Sstevel@tonic-gate 		 * gateway.
39727c478bd9Sstevel@tonic-gate 		 */
3973f4b3ec61Sdh155122 		ire_delete_host_redirects(ire->ire_gateway_addr, ipst);
39747c478bd9Sstevel@tonic-gate 	}
39757c478bd9Sstevel@tonic-gate }
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate /*
39787c478bd9Sstevel@tonic-gate  * IRE_REFRELE/ire_refrele are the only caller of the function. It calls
39797c478bd9Sstevel@tonic-gate  * to free the ire when the reference count goes to zero.
39807c478bd9Sstevel@tonic-gate  */
39817c478bd9Sstevel@tonic-gate void
39827c478bd9Sstevel@tonic-gate ire_inactive(ire_t *ire)
39837c478bd9Sstevel@tonic-gate {
39847c478bd9Sstevel@tonic-gate 	nce_t	*nce;
39857c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
39867c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
39877c478bd9Sstevel@tonic-gate 	ill_t	*in_ill = NULL;
39887c478bd9Sstevel@tonic-gate 	ipif_t	*ipif;
39897c478bd9Sstevel@tonic-gate 	boolean_t	need_wakeup = B_FALSE;
3990c793af95Ssangeeta 	irb_t 	*irb;
3991f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt == 0);
39947c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ptpn == NULL);
39957c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_next == NULL);
39967c478bd9Sstevel@tonic-gate 
3997c793af95Ssangeeta 	if (ire->ire_gw_secattr != NULL) {
3998c793af95Ssangeeta 		ire_gw_secattr_free(ire->ire_gw_secattr);
3999c793af95Ssangeeta 		ire->ire_gw_secattr = NULL;
4000c793af95Ssangeeta 	}
4001c793af95Ssangeeta 
4002c793af95Ssangeeta 	if (ire->ire_mp != NULL) {
4003c793af95Ssangeeta 		ASSERT(ire->ire_bucket == NULL);
4004c793af95Ssangeeta 		mutex_destroy(&ire->ire_lock);
4005f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
4006c793af95Ssangeeta 		if (ire->ire_nce != NULL)
4007c793af95Ssangeeta 			NCE_REFRELE_NOTR(ire->ire_nce);
4008c793af95Ssangeeta 		freeb(ire->ire_mp);
4009c793af95Ssangeeta 		return;
4010c793af95Ssangeeta 	}
4011c793af95Ssangeeta 
40127c478bd9Sstevel@tonic-gate 	if ((nce = ire->ire_nce) != NULL) {
40137c478bd9Sstevel@tonic-gate 		NCE_REFRELE_NOTR(nce);
40147c478bd9Sstevel@tonic-gate 		ire->ire_nce = NULL;
40157c478bd9Sstevel@tonic-gate 	}
4016c793af95Ssangeeta 
40177c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif == NULL)
40187c478bd9Sstevel@tonic-gate 		goto end;
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate 	ipif = ire->ire_ipif;
40217c478bd9Sstevel@tonic-gate 	ill = ipif->ipif_ill;
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	if (ire->ire_bucket == NULL) {
40247c478bd9Sstevel@tonic-gate 		/* The ire was never inserted in the table. */
40257c478bd9Sstevel@tonic-gate 		goto end;
40267c478bd9Sstevel@tonic-gate 	}
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate 	/*
40297c478bd9Sstevel@tonic-gate 	 * ipif_ire_cnt on this ipif goes down by 1. If the ire_stq is
40307c478bd9Sstevel@tonic-gate 	 * non-null ill_ire_count also goes down by 1. If the in_ill is
40317c478bd9Sstevel@tonic-gate 	 * non-null either ill_mrtun_refcnt or ill_srcif_refcnt goes down by 1.
40327c478bd9Sstevel@tonic-gate 	 *
40337c478bd9Sstevel@tonic-gate 	 * The ipif that is associated with an ire is ire->ire_ipif and
40347c478bd9Sstevel@tonic-gate 	 * hence when the ire->ire_ipif->ipif_ire_cnt drops to zero we call
40357c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail. Usually stq_ill is null or the same as
40367c478bd9Sstevel@tonic-gate 	 * ire->ire_ipif->ipif_ill. So nothing more needs to be done. Only
40377c478bd9Sstevel@tonic-gate 	 * in the case of IRE_CACHES when IPMP is used, stq_ill can be
40387c478bd9Sstevel@tonic-gate 	 * different. If this is different from ire->ire_ipif->ipif_ill and
40397c478bd9Sstevel@tonic-gate 	 * if the ill_ire_cnt on the stq_ill also has dropped to zero, we call
40407c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail on the stq_ill. If mobile ip is in use
40417c478bd9Sstevel@tonic-gate 	 * in_ill could be non-null. If it is a reverse tunnel related ire
40427c478bd9Sstevel@tonic-gate 	 * ill_mrtun_refcnt is non-zero. If it is forward tunnel related ire
40437c478bd9Sstevel@tonic-gate 	 * ill_srcif_refcnt is non-null.
40447c478bd9Sstevel@tonic-gate 	 */
40457c478bd9Sstevel@tonic-gate 
40467c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
40477c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
40487c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill != NULL)
40497c478bd9Sstevel@tonic-gate 		in_ill = ire->ire_in_ill;
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate 	if ((stq_ill == NULL || stq_ill == ill) && (in_ill == NULL)) {
40527c478bd9Sstevel@tonic-gate 		/* Optimize the most common case */
40537c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
40547c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
40557c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
40567c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0)
40577c478bd9Sstevel@tonic-gate 			need_wakeup = B_TRUE;
40587c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
40597c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
40607c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
40617c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)
40627c478bd9Sstevel@tonic-gate 				need_wakeup = B_TRUE;
40637c478bd9Sstevel@tonic-gate 		}
40647c478bd9Sstevel@tonic-gate 		if (need_wakeup) {
40657c478bd9Sstevel@tonic-gate 			/* Drops the ill lock */
40667c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
40677c478bd9Sstevel@tonic-gate 		} else {
40687c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
40697c478bd9Sstevel@tonic-gate 		}
40707c478bd9Sstevel@tonic-gate 	} else {
40717c478bd9Sstevel@tonic-gate 		/*
40727c478bd9Sstevel@tonic-gate 		 * We can't grab all the ill locks at the same time.
40737c478bd9Sstevel@tonic-gate 		 * It can lead to recursive lock enter in the call to
40747c478bd9Sstevel@tonic-gate 		 * ipif_ill_refrele_tail and later. Instead do it 1 at
40757c478bd9Sstevel@tonic-gate 		 * a time.
40767c478bd9Sstevel@tonic-gate 		 */
40777c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
40787c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
40797c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
40807c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0) {
40817c478bd9Sstevel@tonic-gate 			/* Drops the lock */
40827c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
40837c478bd9Sstevel@tonic-gate 		} else {
40847c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
40857c478bd9Sstevel@tonic-gate 		}
40867c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
40877c478bd9Sstevel@tonic-gate 			mutex_enter(&stq_ill->ill_lock);
40887c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
40897c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
40907c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)  {
40917c478bd9Sstevel@tonic-gate 				/* Drops the ill lock */
40927c478bd9Sstevel@tonic-gate 				ipif_ill_refrele_tail(stq_ill);
40937c478bd9Sstevel@tonic-gate 			} else {
40947c478bd9Sstevel@tonic-gate 				mutex_exit(&stq_ill->ill_lock);
40957c478bd9Sstevel@tonic-gate 			}
40967c478bd9Sstevel@tonic-gate 		}
40977c478bd9Sstevel@tonic-gate 		if (in_ill != NULL) {
40987c478bd9Sstevel@tonic-gate 			mutex_enter(&in_ill->ill_lock);
40997c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_MIPRTUN) {
41007c478bd9Sstevel@tonic-gate 				/*
41017c478bd9Sstevel@tonic-gate 				 * Mobile IP reverse tunnel ire.
41027c478bd9Sstevel@tonic-gate 				 * Decrement table count and the
41037c478bd9Sstevel@tonic-gate 				 * ill reference count. This signifies
41047c478bd9Sstevel@tonic-gate 				 * mipagent is deleting reverse tunnel
41057c478bd9Sstevel@tonic-gate 				 * route for a particular mobile node.
41067c478bd9Sstevel@tonic-gate 				 */
4107f4b3ec61Sdh155122 				mutex_enter(&ipst->ips_ire_mrtun_lock);
4108f4b3ec61Sdh155122 				ipst->ips_ire_mrtun_count--;
4109f4b3ec61Sdh155122 				mutex_exit(&ipst->ips_ire_mrtun_lock);
41107c478bd9Sstevel@tonic-gate 				ASSERT(in_ill->ill_mrtun_refcnt != 0);
41117c478bd9Sstevel@tonic-gate 				in_ill->ill_mrtun_refcnt--;
41127c478bd9Sstevel@tonic-gate 				if (in_ill->ill_mrtun_refcnt == 0) {
41137c478bd9Sstevel@tonic-gate 					/* Drops the ill lock */
41147c478bd9Sstevel@tonic-gate 					ipif_ill_refrele_tail(in_ill);
41157c478bd9Sstevel@tonic-gate 				} else {
41167c478bd9Sstevel@tonic-gate 					mutex_exit(&in_ill->ill_lock);
41177c478bd9Sstevel@tonic-gate 				}
41187c478bd9Sstevel@tonic-gate 			} else {
4119f4b3ec61Sdh155122 				mutex_enter(&ipst->ips_ire_srcif_table_lock);
4120f4b3ec61Sdh155122 				ipst->ips_ire_srcif_table_count--;
4121f4b3ec61Sdh155122 				mutex_exit(&ipst->ips_ire_srcif_table_lock);
41227c478bd9Sstevel@tonic-gate 				ASSERT(in_ill->ill_srcif_refcnt != 0);
41237c478bd9Sstevel@tonic-gate 				in_ill->ill_srcif_refcnt--;
41247c478bd9Sstevel@tonic-gate 				if (in_ill->ill_srcif_refcnt == 0) {
41257c478bd9Sstevel@tonic-gate 					/* Drops the ill lock */
41267c478bd9Sstevel@tonic-gate 					ipif_ill_refrele_tail(in_ill);
41277c478bd9Sstevel@tonic-gate 				} else {
41287c478bd9Sstevel@tonic-gate 					mutex_exit(&in_ill->ill_lock);
41297c478bd9Sstevel@tonic-gate 				}
41307c478bd9Sstevel@tonic-gate 			}
41317c478bd9Sstevel@tonic-gate 		}
41327c478bd9Sstevel@tonic-gate 	}
41337c478bd9Sstevel@tonic-gate end:
41347c478bd9Sstevel@tonic-gate 	/* This should be true for both V4 and V6 */
41357c478bd9Sstevel@tonic-gate 
4136c793af95Ssangeeta 	if ((ire->ire_type & IRE_FORWARDTABLE) &&
4137c793af95Ssangeeta 	    (ire->ire_ipversion == IPV4_VERSION) &&
4138c793af95Ssangeeta 	    ((irb = ire->ire_bucket) != NULL)) {
4139c793af95Ssangeeta 		rw_enter(&irb->irb_lock, RW_WRITER);
4140c793af95Ssangeeta 		irb->irb_nire--;
4141c793af95Ssangeeta 		/*
4142c793af95Ssangeeta 		 * Instead of examining the conditions for freeing
4143c793af95Ssangeeta 		 * the radix node here, we do it by calling
4144c793af95Ssangeeta 		 * IRB_REFRELE which is a single point in the code
4145c793af95Ssangeeta 		 * that embeds that logic. Bump up the refcnt to
4146c793af95Ssangeeta 		 * be able to call IRB_REFRELE
4147c793af95Ssangeeta 		 */
4148c793af95Ssangeeta 		IRB_REFHOLD_LOCKED(irb);
4149c793af95Ssangeeta 		rw_exit(&irb->irb_lock);
4150c793af95Ssangeeta 		IRB_REFRELE(irb);
4151c793af95Ssangeeta 	}
41527c478bd9Sstevel@tonic-gate 	ire->ire_ipif = NULL;
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill != NULL) {
41557c478bd9Sstevel@tonic-gate 		ire->ire_in_ill = NULL;
41567c478bd9Sstevel@tonic-gate 	}
41577c478bd9Sstevel@tonic-gate 
41587c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
41597c478bd9Sstevel@tonic-gate 	ire_trace_inactive(ire);
41607c478bd9Sstevel@tonic-gate #endif
41617c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire->ire_lock);
41627c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
4163f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v6, ire_stats_freed);
41647c478bd9Sstevel@tonic-gate 	} else {
4165f4b3ec61Sdh155122 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_freed);
41667c478bd9Sstevel@tonic-gate 	}
4167c793af95Ssangeeta 	ASSERT(ire->ire_mp == NULL);
41687c478bd9Sstevel@tonic-gate 	/* Has been allocated out of the cache */
41697c478bd9Sstevel@tonic-gate 	kmem_cache_free(ire_cache, ire);
41707c478bd9Sstevel@tonic-gate }
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate /*
41736bdb8e66Sdd193516  * ire_walk routine to delete all IRE_CACHE/IRE_HOST types redirect
41746bdb8e66Sdd193516  * entries that have a given gateway address.
41757c478bd9Sstevel@tonic-gate  */
41767c478bd9Sstevel@tonic-gate void
41777c478bd9Sstevel@tonic-gate ire_delete_cache_gw(ire_t *ire, char *cp)
41787c478bd9Sstevel@tonic-gate {
41797c478bd9Sstevel@tonic-gate 	ipaddr_t	gw_addr;
41807c478bd9Sstevel@tonic-gate 
41816bdb8e66Sdd193516 	if (!(ire->ire_type & IRE_CACHE) &&
41826bdb8e66Sdd193516 	    !(ire->ire_flags & RTF_DYNAMIC))
41837c478bd9Sstevel@tonic-gate 		return;
41847c478bd9Sstevel@tonic-gate 
41857c478bd9Sstevel@tonic-gate 	bcopy(cp, &gw_addr, sizeof (gw_addr));
41867c478bd9Sstevel@tonic-gate 	if (ire->ire_gateway_addr == gw_addr) {
41877c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_delete_cache_gw: deleted 0x%x type %d to 0x%x\n",
41887c478bd9Sstevel@tonic-gate 			(int)ntohl(ire->ire_addr), ire->ire_type,
41897c478bd9Sstevel@tonic-gate 			(int)ntohl(ire->ire_gateway_addr)));
41907c478bd9Sstevel@tonic-gate 		ire_delete(ire);
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate }
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate /*
41957c478bd9Sstevel@tonic-gate  * Remove all IRE_CACHE entries that match the ire specified.
41967c478bd9Sstevel@tonic-gate  *
41977c478bd9Sstevel@tonic-gate  * The flag argument indicates if the flush request is due to addition
41987c478bd9Sstevel@tonic-gate  * of new route (IRE_FLUSH_ADD) or deletion of old route (IRE_FLUSH_DELETE).
41997c478bd9Sstevel@tonic-gate  *
42007c478bd9Sstevel@tonic-gate  * This routine takes only the IREs from the forwarding table and flushes
42017c478bd9Sstevel@tonic-gate  * the corresponding entries from the cache table.
42027c478bd9Sstevel@tonic-gate  *
42037c478bd9Sstevel@tonic-gate  * When flushing due to the deletion of an old route, it
42047c478bd9Sstevel@tonic-gate  * just checks the cache handles (ire_phandle and ire_ihandle) and
42057c478bd9Sstevel@tonic-gate  * deletes the ones that match.
42067c478bd9Sstevel@tonic-gate  *
42077c478bd9Sstevel@tonic-gate  * When flushing due to the creation of a new route, it checks
42087c478bd9Sstevel@tonic-gate  * if a cache entry's address matches the one in the IRE and
42097c478bd9Sstevel@tonic-gate  * that the cache entry's parent has a less specific mask than the
42107c478bd9Sstevel@tonic-gate  * one in IRE. The destination of such a cache entry could be the
42117c478bd9Sstevel@tonic-gate  * gateway for other cache entries, so we need to flush those as
42127c478bd9Sstevel@tonic-gate  * well by looking for gateway addresses matching the IRE's address.
42137c478bd9Sstevel@tonic-gate  */
42147c478bd9Sstevel@tonic-gate void
42157c478bd9Sstevel@tonic-gate ire_flush_cache_v4(ire_t *ire, int flag)
42167c478bd9Sstevel@tonic-gate {
42177c478bd9Sstevel@tonic-gate 	int i;
42187c478bd9Sstevel@tonic-gate 	ire_t *cire;
42197c478bd9Sstevel@tonic-gate 	irb_t *irb;
4220f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_CACHE)
42237c478bd9Sstevel@tonic-gate 	    return;
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate 	/*
42267c478bd9Sstevel@tonic-gate 	 * If a default is just created, there is no point
42277c478bd9Sstevel@tonic-gate 	 * in going through the cache, as there will not be any
42287c478bd9Sstevel@tonic-gate 	 * cached ires.
42297c478bd9Sstevel@tonic-gate 	 */
42307c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD)
42317c478bd9Sstevel@tonic-gate 		return;
42327c478bd9Sstevel@tonic-gate 	if (flag == IRE_FLUSH_ADD) {
42337c478bd9Sstevel@tonic-gate 		/*
42347c478bd9Sstevel@tonic-gate 		 * This selective flush is due to the addition of
42357c478bd9Sstevel@tonic-gate 		 * new IRE.
42367c478bd9Sstevel@tonic-gate 		 */
4237f4b3ec61Sdh155122 		for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
4238f4b3ec61Sdh155122 			irb = &ipst->ips_ip_cache_table[i];
42397c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
42407c478bd9Sstevel@tonic-gate 				continue;
42417c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
42427c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
42437c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
42447c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
42457c478bd9Sstevel@tonic-gate 					continue;
42467c478bd9Sstevel@tonic-gate 				/*
42477c478bd9Sstevel@tonic-gate 				 * If 'cire' belongs to the same subnet
42487c478bd9Sstevel@tonic-gate 				 * as the new ire being added, and 'cire'
42497c478bd9Sstevel@tonic-gate 				 * is derived from a prefix that is less
42507c478bd9Sstevel@tonic-gate 				 * specific than the new ire being added,
42517c478bd9Sstevel@tonic-gate 				 * we need to flush 'cire'; for instance,
42527c478bd9Sstevel@tonic-gate 				 * when a new interface comes up.
42537c478bd9Sstevel@tonic-gate 				 */
42547c478bd9Sstevel@tonic-gate 				if (((cire->ire_addr & ire->ire_mask) ==
42557c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) &&
42567c478bd9Sstevel@tonic-gate 				    (ip_mask_to_plen(cire->ire_cmask) <=
42577c478bd9Sstevel@tonic-gate 				    ire->ire_masklen)) {
42587c478bd9Sstevel@tonic-gate 					ire_delete(cire);
42597c478bd9Sstevel@tonic-gate 					continue;
42607c478bd9Sstevel@tonic-gate 				}
42617c478bd9Sstevel@tonic-gate 				/*
42627c478bd9Sstevel@tonic-gate 				 * This is the case when the ire_gateway_addr
42637c478bd9Sstevel@tonic-gate 				 * of 'cire' belongs to the same subnet as
42647c478bd9Sstevel@tonic-gate 				 * the new ire being added.
42657c478bd9Sstevel@tonic-gate 				 * Flushing such ires is sometimes required to
42667c478bd9Sstevel@tonic-gate 				 * avoid misrouting: say we have a machine with
42677c478bd9Sstevel@tonic-gate 				 * two interfaces (I1 and I2), a default router
42687c478bd9Sstevel@tonic-gate 				 * R on the I1 subnet, and a host route to an
42697c478bd9Sstevel@tonic-gate 				 * off-link destination D with a gateway G on
42707c478bd9Sstevel@tonic-gate 				 * the I2 subnet.
42717c478bd9Sstevel@tonic-gate 				 * Under normal operation, we will have an
42727c478bd9Sstevel@tonic-gate 				 * on-link cache entry for G and an off-link
42737c478bd9Sstevel@tonic-gate 				 * cache entry for D with G as ire_gateway_addr,
42747c478bd9Sstevel@tonic-gate 				 * traffic to D will reach its destination
42757c478bd9Sstevel@tonic-gate 				 * through gateway G.
42767c478bd9Sstevel@tonic-gate 				 * If the administrator does 'ifconfig I2 down',
42777c478bd9Sstevel@tonic-gate 				 * the cache entries for D and G will be
42787c478bd9Sstevel@tonic-gate 				 * flushed. However, G will now be resolved as
42797c478bd9Sstevel@tonic-gate 				 * an off-link destination using R (the default
42807c478bd9Sstevel@tonic-gate 				 * router) as gateway. Then D will also be
42817c478bd9Sstevel@tonic-gate 				 * resolved as an off-link destination using G
42827c478bd9Sstevel@tonic-gate 				 * as gateway - this behavior is due to
42837c478bd9Sstevel@tonic-gate 				 * compatibility reasons, see comment in
42847c478bd9Sstevel@tonic-gate 				 * ire_ihandle_lookup_offlink(). Traffic to D
42857c478bd9Sstevel@tonic-gate 				 * will go to the router R and probably won't
42867c478bd9Sstevel@tonic-gate 				 * reach the destination.
42877c478bd9Sstevel@tonic-gate 				 * The administrator then does 'ifconfig I2 up'.
42887c478bd9Sstevel@tonic-gate 				 * Since G is on the I2 subnet, this routine
42897c478bd9Sstevel@tonic-gate 				 * will flush its cache entry. It must also
42907c478bd9Sstevel@tonic-gate 				 * flush the cache entry for D, otherwise
42917c478bd9Sstevel@tonic-gate 				 * traffic will stay misrouted until the IRE
42927c478bd9Sstevel@tonic-gate 				 * times out.
42937c478bd9Sstevel@tonic-gate 				 */
42947c478bd9Sstevel@tonic-gate 				if ((cire->ire_gateway_addr & ire->ire_mask) ==
42957c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) {
42967c478bd9Sstevel@tonic-gate 					ire_delete(cire);
42977c478bd9Sstevel@tonic-gate 					continue;
42987c478bd9Sstevel@tonic-gate 				}
42997c478bd9Sstevel@tonic-gate 			}
43007c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
43017c478bd9Sstevel@tonic-gate 		}
43027c478bd9Sstevel@tonic-gate 	} else {
43037c478bd9Sstevel@tonic-gate 		/*
43047c478bd9Sstevel@tonic-gate 		 * delete the cache entries based on
43057c478bd9Sstevel@tonic-gate 		 * handle in the IRE as this IRE is
43067c478bd9Sstevel@tonic-gate 		 * being deleted/changed.
43077c478bd9Sstevel@tonic-gate 		 */
4308f4b3ec61Sdh155122 		for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
4309f4b3ec61Sdh155122 			irb = &ipst->ips_ip_cache_table[i];
43107c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
43117c478bd9Sstevel@tonic-gate 				continue;
43127c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
43137c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
43147c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
43157c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
43167c478bd9Sstevel@tonic-gate 					continue;
43177c478bd9Sstevel@tonic-gate 				if ((cire->ire_phandle == 0 ||
43187c478bd9Sstevel@tonic-gate 				    cire->ire_phandle != ire->ire_phandle) &&
43197c478bd9Sstevel@tonic-gate 				    (cire->ire_ihandle == 0 ||
43207c478bd9Sstevel@tonic-gate 				    cire->ire_ihandle != ire->ire_ihandle))
43217c478bd9Sstevel@tonic-gate 					continue;
43227c478bd9Sstevel@tonic-gate 				ire_delete(cire);
43237c478bd9Sstevel@tonic-gate 			}
43247c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
43257c478bd9Sstevel@tonic-gate 		}
43267c478bd9Sstevel@tonic-gate 	}
43277c478bd9Sstevel@tonic-gate }
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate /*
43307c478bd9Sstevel@tonic-gate  * Matches the arguments passed with the values in the ire.
43317c478bd9Sstevel@tonic-gate  *
43327c478bd9Sstevel@tonic-gate  * Note: for match types that match using "ipif" passed in, ipif
43337c478bd9Sstevel@tonic-gate  * must be checked for non-NULL before calling this routine.
43347c478bd9Sstevel@tonic-gate  */
4335c793af95Ssangeeta boolean_t
43367c478bd9Sstevel@tonic-gate ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
433745916cd2Sjpk     int type, const ipif_t *ipif, zoneid_t zoneid, uint32_t ihandle,
433845916cd2Sjpk     const ts_label_t *tsl, int match_flags)
43397c478bd9Sstevel@tonic-gate {
43407c478bd9Sstevel@tonic-gate 	ill_t *ire_ill = NULL, *dst_ill;
43417c478bd9Sstevel@tonic-gate 	ill_t *ipif_ill = NULL;
43427c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
43437c478bd9Sstevel@tonic-gate 	ill_group_t *ipif_ill_group = NULL;
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
43467c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
43477c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP))) ||
43487c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
43497c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_WQ));
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 	/*
43527c478bd9Sstevel@tonic-gate 	 * HIDDEN cache entries have to be looked up specifically with
43537c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_MARK_HIDDEN. MATCH_IRE_MARK_HIDDEN is usually set
43547c478bd9Sstevel@tonic-gate 	 * when the interface is FAILED or INACTIVE. In that case,
43557c478bd9Sstevel@tonic-gate 	 * any IRE_CACHES that exists should be marked with
43567c478bd9Sstevel@tonic-gate 	 * IRE_MARK_HIDDEN. So, we don't really need to match below
43577c478bd9Sstevel@tonic-gate 	 * for IRE_MARK_HIDDEN. But we do so for consistency.
43587c478bd9Sstevel@tonic-gate 	 */
43597c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_MARK_HIDDEN) &&
43607c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_HIDDEN))
43617c478bd9Sstevel@tonic-gate 		return (B_FALSE);
43627c478bd9Sstevel@tonic-gate 
436343d18f1cSpriyanka 	/*
436443d18f1cSpriyanka 	 * MATCH_IRE_MARK_PRIVATE_ADDR is set when IP_NEXTHOP option
436543d18f1cSpriyanka 	 * is used. In that case the routing table is bypassed and the
436643d18f1cSpriyanka 	 * packets are sent directly to the specified nexthop. The
436743d18f1cSpriyanka 	 * IRE_CACHE entry representing this route should be marked
436843d18f1cSpriyanka 	 * with IRE_MARK_PRIVATE_ADDR.
436943d18f1cSpriyanka 	 */
437043d18f1cSpriyanka 
437143d18f1cSpriyanka 	if (!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR) &&
437243d18f1cSpriyanka 	    (ire->ire_marks & IRE_MARK_PRIVATE_ADDR))
437343d18f1cSpriyanka 		return (B_FALSE);
437443d18f1cSpriyanka 
437545916cd2Sjpk 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
437645916cd2Sjpk 	    ire->ire_zoneid != ALL_ZONES) {
43777c478bd9Sstevel@tonic-gate 		/*
43787c478bd9Sstevel@tonic-gate 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid is
43797c478bd9Sstevel@tonic-gate 		 * valid and does not match that of ire_zoneid, a failure to
43807c478bd9Sstevel@tonic-gate 		 * match is reported at this point. Otherwise, since some IREs
43817c478bd9Sstevel@tonic-gate 		 * that are available in the global zone can be used in local
43827c478bd9Sstevel@tonic-gate 		 * zones, additional checks need to be performed:
43837c478bd9Sstevel@tonic-gate 		 *
43847c478bd9Sstevel@tonic-gate 		 *	IRE_BROADCAST, IRE_CACHE and IRE_LOOPBACK
43857c478bd9Sstevel@tonic-gate 		 *	entries should never be matched in this situation.
43867c478bd9Sstevel@tonic-gate 		 *
43877c478bd9Sstevel@tonic-gate 		 *	IRE entries that have an interface associated with them
43887c478bd9Sstevel@tonic-gate 		 *	should in general not match unless they are an IRE_LOCAL
43897c478bd9Sstevel@tonic-gate 		 *	or in the case when MATCH_IRE_DEFAULT has been set in
43907c478bd9Sstevel@tonic-gate 		 *	the caller.  In the case of the former, checking of the
43917c478bd9Sstevel@tonic-gate 		 *	other fields supplied should take place.
43927c478bd9Sstevel@tonic-gate 		 *
43937c478bd9Sstevel@tonic-gate 		 *	In the case where MATCH_IRE_DEFAULT has been set,
43947c478bd9Sstevel@tonic-gate 		 *	all of the ipif's associated with the IRE's ill are
43957c478bd9Sstevel@tonic-gate 		 *	checked to see if there is a matching zoneid.  If any
43967c478bd9Sstevel@tonic-gate 		 *	one ipif has a matching zoneid, this IRE is a
43977c478bd9Sstevel@tonic-gate 		 *	potential candidate so checking of the other fields
43987c478bd9Sstevel@tonic-gate 		 *	takes place.
43997c478bd9Sstevel@tonic-gate 		 *
44007c478bd9Sstevel@tonic-gate 		 *	In the case where the IRE_INTERFACE has a usable source
44017c478bd9Sstevel@tonic-gate 		 *	address (indicated by ill_usesrc_ifindex) in the
44027c478bd9Sstevel@tonic-gate 		 *	correct zone then it's permitted to return this IRE
44037c478bd9Sstevel@tonic-gate 		 */
44047c478bd9Sstevel@tonic-gate 		if (match_flags & MATCH_IRE_ZONEONLY)
44057c478bd9Sstevel@tonic-gate 			return (B_FALSE);
44067c478bd9Sstevel@tonic-gate 		if (ire->ire_type & (IRE_BROADCAST | IRE_CACHE | IRE_LOOPBACK))
44077c478bd9Sstevel@tonic-gate 			return (B_FALSE);
44087c478bd9Sstevel@tonic-gate 		/*
44097c478bd9Sstevel@tonic-gate 		 * Note, IRE_INTERFACE can have the stq as NULL. For
44107c478bd9Sstevel@tonic-gate 		 * example, if the default multicast route is tied to
44117c478bd9Sstevel@tonic-gate 		 * the loopback address.
44127c478bd9Sstevel@tonic-gate 		 */
44137c478bd9Sstevel@tonic-gate 		if ((ire->ire_type & IRE_INTERFACE) &&
44147c478bd9Sstevel@tonic-gate 		    (ire->ire_stq != NULL)) {
44157c478bd9Sstevel@tonic-gate 			dst_ill = (ill_t *)ire->ire_stq->q_ptr;
44167c478bd9Sstevel@tonic-gate 			/*
44177c478bd9Sstevel@tonic-gate 			 * If there is a usable source address in the
44187c478bd9Sstevel@tonic-gate 			 * zone, then it's ok to return an
44197c478bd9Sstevel@tonic-gate 			 * IRE_INTERFACE
44207c478bd9Sstevel@tonic-gate 			 */
44217c478bd9Sstevel@tonic-gate 			if (ipif_usesrc_avail(dst_ill, zoneid)) {
44227c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: dst_ill %p match %d\n",
44237c478bd9Sstevel@tonic-gate 				    (void *)dst_ill,
44247c478bd9Sstevel@tonic-gate 				    (ire->ire_addr == (addr & mask))));
44257c478bd9Sstevel@tonic-gate 			} else {
44267c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: src_ipif NULL"
44277c478bd9Sstevel@tonic-gate 				    " dst_ill %p\n", (void *)dst_ill));
44287c478bd9Sstevel@tonic-gate 				return (B_FALSE);
44297c478bd9Sstevel@tonic-gate 			}
44307c478bd9Sstevel@tonic-gate 		}
44317c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL && ire->ire_type != IRE_LOCAL &&
44327c478bd9Sstevel@tonic-gate 		    !(ire->ire_type & IRE_INTERFACE)) {
44337c478bd9Sstevel@tonic-gate 			ipif_t	*tipif;
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 			if ((match_flags & MATCH_IRE_DEFAULT) == 0) {
44367c478bd9Sstevel@tonic-gate 				return (B_FALSE);
44377c478bd9Sstevel@tonic-gate 			}
44387c478bd9Sstevel@tonic-gate 			mutex_enter(&ire->ire_ipif->ipif_ill->ill_lock);
44397c478bd9Sstevel@tonic-gate 			for (tipif = ire->ire_ipif->ipif_ill->ill_ipif;
44407c478bd9Sstevel@tonic-gate 			    tipif != NULL; tipif = tipif->ipif_next) {
44417c478bd9Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(tipif) &&
44427c478bd9Sstevel@tonic-gate 				    (tipif->ipif_flags & IPIF_UP) &&
444345916cd2Sjpk 				    (tipif->ipif_zoneid == zoneid ||
444445916cd2Sjpk 				    tipif->ipif_zoneid == ALL_ZONES))
44457c478bd9Sstevel@tonic-gate 					break;
44467c478bd9Sstevel@tonic-gate 			}
44477c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_ipif->ipif_ill->ill_lock);
44487c478bd9Sstevel@tonic-gate 			if (tipif == NULL) {
44497c478bd9Sstevel@tonic-gate 				return (B_FALSE);
44507c478bd9Sstevel@tonic-gate 			}
44517c478bd9Sstevel@tonic-gate 		}
44527c478bd9Sstevel@tonic-gate 	}
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate 	/*
44557c478bd9Sstevel@tonic-gate 	 * For IRE_CACHES, MATCH_IRE_ILL/ILL_GROUP really means that
44567c478bd9Sstevel@tonic-gate 	 * somebody wants to send out on a particular interface which
44577c478bd9Sstevel@tonic-gate 	 * is given by ire_stq and hence use ire_stq to derive the ill
44587c478bd9Sstevel@tonic-gate 	 * value. ire_ipif for IRE_CACHES is just the means of getting
44597c478bd9Sstevel@tonic-gate 	 * a source address i.e ire_src_addr = ire->ire_ipif->ipif_src_addr.
44607c478bd9Sstevel@tonic-gate 	 * ire_to_ill does the right thing for this.
44617c478bd9Sstevel@tonic-gate 	 */
44627c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
44637c478bd9Sstevel@tonic-gate 		ire_ill = ire_to_ill(ire);
44647c478bd9Sstevel@tonic-gate 		if (ire_ill != NULL)
44657c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ill->ill_group;
44667c478bd9Sstevel@tonic-gate 		ipif_ill = ipif->ipif_ill;
44677c478bd9Sstevel@tonic-gate 		ipif_ill_group = ipif_ill->ill_group;
44687c478bd9Sstevel@tonic-gate 	}
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	if ((ire->ire_addr == (addr & mask)) &&
44717c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_GW)) ||
44727c478bd9Sstevel@tonic-gate 		(ire->ire_gateway_addr == gateway)) &&
44737c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_TYPE)) ||
44747c478bd9Sstevel@tonic-gate 		(ire->ire_type & type)) &&
44757c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_SRC)) ||
44767c478bd9Sstevel@tonic-gate 		(ire->ire_src_addr == ipif->ipif_src_addr)) &&
44777c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IPIF)) ||
44787c478bd9Sstevel@tonic-gate 		(ire->ire_ipif == ipif)) &&
44797c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_MARK_HIDDEN)) ||
44807c478bd9Sstevel@tonic-gate 		(ire->ire_type != IRE_CACHE ||
44817c478bd9Sstevel@tonic-gate 		ire->ire_marks & IRE_MARK_HIDDEN)) &&
448243d18f1cSpriyanka 	    ((!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR)) ||
448343d18f1cSpriyanka 		(ire->ire_type != IRE_CACHE ||
448443d18f1cSpriyanka 		ire->ire_marks & IRE_MARK_PRIVATE_ADDR)) &&
44857c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
44867c478bd9Sstevel@tonic-gate 		(ire_ill == ipif_ill)) &&
44877c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IHANDLE)) ||
44887c478bd9Sstevel@tonic-gate 		(ire->ire_ihandle == ihandle)) &&
4489c793af95Ssangeeta 	    ((!(match_flags & MATCH_IRE_MASK)) ||
4490c793af95Ssangeeta 		(ire->ire_mask == mask)) &&
44917c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
44927c478bd9Sstevel@tonic-gate 		(ire_ill == ipif_ill) ||
44937c478bd9Sstevel@tonic-gate 		(ire_ill_group != NULL &&
449445916cd2Sjpk 		ire_ill_group == ipif_ill_group)) &&
449545916cd2Sjpk 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
449645916cd2Sjpk 		(!is_system_labeled()) ||
449745916cd2Sjpk 		(tsol_ire_match_gwattr(ire, tsl) == 0))) {
44987c478bd9Sstevel@tonic-gate 		/* We found the matched IRE */
44997c478bd9Sstevel@tonic-gate 		return (B_TRUE);
45007c478bd9Sstevel@tonic-gate 	}
45017c478bd9Sstevel@tonic-gate 	return (B_FALSE);
45027c478bd9Sstevel@tonic-gate }
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate 
45057c478bd9Sstevel@tonic-gate /*
45067c478bd9Sstevel@tonic-gate  * Lookup for a route in all the tables
45077c478bd9Sstevel@tonic-gate  */
45087c478bd9Sstevel@tonic-gate ire_t *
45097c478bd9Sstevel@tonic-gate ire_route_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
451045916cd2Sjpk     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
4511f4b3ec61Sdh155122     const ts_label_t *tsl, int flags, ip_stack_t *ipst)
45127c478bd9Sstevel@tonic-gate {
45137c478bd9Sstevel@tonic-gate 	ire_t *ire = NULL;
45147c478bd9Sstevel@tonic-gate 
45157c478bd9Sstevel@tonic-gate 	/*
45167c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
45177c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
45187c478bd9Sstevel@tonic-gate 	 */
45197c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
45207c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
45217c478bd9Sstevel@tonic-gate 		return (NULL);
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate 	/*
45247c478bd9Sstevel@tonic-gate 	 * might be asking for a cache lookup,
45257c478bd9Sstevel@tonic-gate 	 * This is not best way to lookup cache,
45267c478bd9Sstevel@tonic-gate 	 * user should call ire_cache_lookup directly.
45277c478bd9Sstevel@tonic-gate 	 *
45287c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_TYPE was set, first lookup in the cache table and then
45297c478bd9Sstevel@tonic-gate 	 * in the forwarding table, if the applicable type flags were set.
45307c478bd9Sstevel@tonic-gate 	 */
45317c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_CACHETABLE) != 0) {
45327c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(addr, gateway, type, ipif, zoneid,
4533f4b3ec61Sdh155122 		    tsl, flags, ipst);
45347c478bd9Sstevel@tonic-gate 		if (ire != NULL)
45357c478bd9Sstevel@tonic-gate 			return (ire);
45367c478bd9Sstevel@tonic-gate 	}
45377c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_FORWARDTABLE) != 0) {
45387c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(addr, mask, gateway, type, ipif, pire,
4539f4b3ec61Sdh155122 		    zoneid, 0, tsl, flags, ipst);
45407c478bd9Sstevel@tonic-gate 	}
45417c478bd9Sstevel@tonic-gate 	return (ire);
45427c478bd9Sstevel@tonic-gate }
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate /*
454645916cd2Sjpk  * Delete the IRE cache for the gateway and all IRE caches whose
454745916cd2Sjpk  * ire_gateway_addr points to this gateway, and allow them to
454845916cd2Sjpk  * be created on demand by ip_newroute.
454945916cd2Sjpk  */
455045916cd2Sjpk void
4551f4b3ec61Sdh155122 ire_clookup_delete_cache_gw(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
455245916cd2Sjpk {
455345916cd2Sjpk 	irb_t *irb;
455445916cd2Sjpk 	ire_t *ire;
455545916cd2Sjpk 
4556f4b3ec61Sdh155122 	irb = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4557f4b3ec61Sdh155122 	    ipst->ips_ip_cache_table_size)];
455845916cd2Sjpk 	IRB_REFHOLD(irb);
455945916cd2Sjpk 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
456045916cd2Sjpk 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
456145916cd2Sjpk 			continue;
456245916cd2Sjpk 
456345916cd2Sjpk 		ASSERT(ire->ire_mask == IP_HOST_MASK);
456445916cd2Sjpk 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
456545916cd2Sjpk 		if (ire_match_args(ire, addr, ire->ire_mask, 0, IRE_CACHE,
456645916cd2Sjpk 		    NULL, zoneid, 0, NULL, MATCH_IRE_TYPE)) {
456745916cd2Sjpk 			ire_delete(ire);
456845916cd2Sjpk 		}
456945916cd2Sjpk 	}
457045916cd2Sjpk 	IRB_REFRELE(irb);
457145916cd2Sjpk 
4572f4b3ec61Sdh155122 	ire_walk_v4(ire_delete_cache_gw, &addr, zoneid, ipst);
457345916cd2Sjpk }
457445916cd2Sjpk 
457545916cd2Sjpk /*
45767c478bd9Sstevel@tonic-gate  * Looks up cache table for a route.
45777c478bd9Sstevel@tonic-gate  * specific lookup can be indicated by
45787c478bd9Sstevel@tonic-gate  * passing the MATCH_* flags and the
45797c478bd9Sstevel@tonic-gate  * necessary parameters.
45807c478bd9Sstevel@tonic-gate  */
45817c478bd9Sstevel@tonic-gate ire_t *
458245916cd2Sjpk ire_ctable_lookup(ipaddr_t addr, ipaddr_t gateway, int type, const ipif_t *ipif,
4583f4b3ec61Sdh155122     zoneid_t zoneid, const ts_label_t *tsl, int flags, ip_stack_t *ipst)
45847c478bd9Sstevel@tonic-gate {
45857c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
45867c478bd9Sstevel@tonic-gate 	ire_t *ire;
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate 	/*
45897c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
45907c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
45917c478bd9Sstevel@tonic-gate 	 */
45927c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
45937c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
45947c478bd9Sstevel@tonic-gate 		return (NULL);
45957c478bd9Sstevel@tonic-gate 
4596f4b3ec61Sdh155122 	irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4597f4b3ec61Sdh155122 					    ipst->ips_ip_cache_table_size)];
45987c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
45997c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
46007c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
46017c478bd9Sstevel@tonic-gate 			continue;
46027c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_mask == IP_HOST_MASK);
46037c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
46047c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, addr, ire->ire_mask, gateway, type,
460545916cd2Sjpk 		    ipif, zoneid, 0, tsl, flags)) {
46067c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
46077c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
46087c478bd9Sstevel@tonic-gate 			return (ire);
46097c478bd9Sstevel@tonic-gate 		}
46107c478bd9Sstevel@tonic-gate 	}
46117c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
46127c478bd9Sstevel@tonic-gate 	return (NULL);
46137c478bd9Sstevel@tonic-gate }
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate /*
46165597b60aSnordmark  * Check whether the IRE_LOCAL and the IRE potentially used to transmit
46175597b60aSnordmark  * (could be an IRE_CACHE, IRE_BROADCAST, or IRE_INTERFACE) are part of
46185597b60aSnordmark  * the same ill group.
46195597b60aSnordmark  */
46205597b60aSnordmark boolean_t
46215597b60aSnordmark ire_local_same_ill_group(ire_t *ire_local, ire_t *xmit_ire)
46225597b60aSnordmark {
46235597b60aSnordmark 	ill_t		*recv_ill, *xmit_ill;
46245597b60aSnordmark 	ill_group_t	*recv_group, *xmit_group;
46255597b60aSnordmark 
46262bc4236aSnordmark 	ASSERT(ire_local->ire_type & (IRE_LOCAL|IRE_LOOPBACK));
4627aa2eaee6Snordmark 	ASSERT(xmit_ire->ire_type & (IRE_CACHETABLE|IRE_INTERFACE));
46285597b60aSnordmark 
46292bc4236aSnordmark 	recv_ill = ire_to_ill(ire_local);
46302bc4236aSnordmark 	xmit_ill = ire_to_ill(xmit_ire);
46312bc4236aSnordmark 
46322bc4236aSnordmark 	ASSERT(recv_ill != NULL);
46332bc4236aSnordmark 	ASSERT(xmit_ill != NULL);
46345597b60aSnordmark 
46355597b60aSnordmark 	if (recv_ill == xmit_ill)
46365597b60aSnordmark 		return (B_TRUE);
46375597b60aSnordmark 
46385597b60aSnordmark 	recv_group = recv_ill->ill_group;
46395597b60aSnordmark 	xmit_group = xmit_ill->ill_group;
46405597b60aSnordmark 
46415597b60aSnordmark 	if (recv_group != NULL && recv_group == xmit_group)
46425597b60aSnordmark 		return (B_TRUE);
46435597b60aSnordmark 
46445597b60aSnordmark 	return (B_FALSE);
46455597b60aSnordmark }
46465597b60aSnordmark 
46475597b60aSnordmark /*
46485597b60aSnordmark  * Check if the IRE_LOCAL uses the same ill (group) as another route would use.
4649aa2eaee6Snordmark  * If there is no alternate route, or the alternate is a REJECT or BLACKHOLE,
4650aa2eaee6Snordmark  * then we don't allow this IRE_LOCAL to be used.
46515597b60aSnordmark  */
46525597b60aSnordmark boolean_t
46535597b60aSnordmark ire_local_ok_across_zones(ire_t *ire_local, zoneid_t zoneid, void *addr,
4654f4b3ec61Sdh155122     const ts_label_t *tsl, ip_stack_t *ipst)
46555597b60aSnordmark {
46565597b60aSnordmark 	ire_t		*alt_ire;
46575597b60aSnordmark 	boolean_t	rval;
46585597b60aSnordmark 
46595597b60aSnordmark 	if (ire_local->ire_ipversion == IPV4_VERSION) {
46605597b60aSnordmark 		alt_ire = ire_ftable_lookup(*((ipaddr_t *)addr), 0, 0, 0, NULL,
46615597b60aSnordmark 		    NULL, zoneid, 0, tsl,
46625597b60aSnordmark 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4663f4b3ec61Sdh155122 		    MATCH_IRE_RJ_BHOLE, ipst);
46645597b60aSnordmark 	} else {
46655597b60aSnordmark 		alt_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
46665597b60aSnordmark 		    0, NULL, NULL, zoneid, 0, tsl,
46675597b60aSnordmark 		    MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4668f4b3ec61Sdh155122 		    MATCH_IRE_RJ_BHOLE, ipst);
46695597b60aSnordmark 	}
46705597b60aSnordmark 
46715597b60aSnordmark 	if (alt_ire == NULL)
46725597b60aSnordmark 		return (B_FALSE);
46735597b60aSnordmark 
4674aa2eaee6Snordmark 	if (alt_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
4675aa2eaee6Snordmark 		ire_refrele(alt_ire);
4676aa2eaee6Snordmark 		return (B_FALSE);
4677aa2eaee6Snordmark 	}
46785597b60aSnordmark 	rval = ire_local_same_ill_group(ire_local, alt_ire);
46795597b60aSnordmark 
46805597b60aSnordmark 	ire_refrele(alt_ire);
46815597b60aSnordmark 	return (rval);
46825597b60aSnordmark }
46835597b60aSnordmark 
46845597b60aSnordmark /*
46857c478bd9Sstevel@tonic-gate  * Lookup cache. Don't return IRE_MARK_HIDDEN entries. Callers
46867c478bd9Sstevel@tonic-gate  * should use ire_ctable_lookup with MATCH_IRE_MARK_HIDDEN to get
46877c478bd9Sstevel@tonic-gate  * to the hidden ones.
46885597b60aSnordmark  *
46895597b60aSnordmark  * In general the zoneid has to match (where ALL_ZONES match all of them).
46905597b60aSnordmark  * But for IRE_LOCAL we also need to handle the case where L2 should
46915597b60aSnordmark  * conceptually loop back the packet. This is necessary since neither
46925597b60aSnordmark  * Ethernet drivers nor Ethernet hardware loops back packets sent to their
46935597b60aSnordmark  * own MAC address. This loopback is needed when the normal
46945597b60aSnordmark  * routes (ignoring IREs with different zoneids) would send out the packet on
46955597b60aSnordmark  * the same ill (or ill group) as the ill with which this IRE_LOCAL is
46965597b60aSnordmark  * associated.
46975597b60aSnordmark  *
46985597b60aSnordmark  * Earlier versions of this code always matched an IRE_LOCAL independently of
46995597b60aSnordmark  * the zoneid. We preserve that earlier behavior when
47005597b60aSnordmark  * ip_restrict_interzone_loopback is turned off.
47017c478bd9Sstevel@tonic-gate  */
47027c478bd9Sstevel@tonic-gate ire_t *
4703f4b3ec61Sdh155122 ire_cache_lookup(ipaddr_t addr, zoneid_t zoneid, const ts_label_t *tsl,
4704f4b3ec61Sdh155122     ip_stack_t *ipst)
47057c478bd9Sstevel@tonic-gate {
47067c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
47077c478bd9Sstevel@tonic-gate 	ire_t *ire;
47087c478bd9Sstevel@tonic-gate 
4709f4b3ec61Sdh155122 	irb_ptr = &ipst->ips_ip_cache_table[IRE_ADDR_HASH(addr,
4710f4b3ec61Sdh155122 					    ipst->ips_ip_cache_table_size)];
47117c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
47127c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
471343d18f1cSpriyanka 		if (ire->ire_marks & (IRE_MARK_CONDEMNED |
471443d18f1cSpriyanka 		    IRE_MARK_HIDDEN | IRE_MARK_PRIVATE_ADDR)) {
47157c478bd9Sstevel@tonic-gate 			continue;
471643d18f1cSpriyanka 		}
47177c478bd9Sstevel@tonic-gate 		if (ire->ire_addr == addr) {
471845916cd2Sjpk 			/*
471945916cd2Sjpk 			 * Finally, check if the security policy has any
472045916cd2Sjpk 			 * restriction on using this route for the specified
472145916cd2Sjpk 			 * message.
472245916cd2Sjpk 			 */
472345916cd2Sjpk 			if (tsl != NULL &&
472445916cd2Sjpk 			    ire->ire_gw_secattr != NULL &&
472545916cd2Sjpk 			    tsol_ire_match_gwattr(ire, tsl) != 0) {
472645916cd2Sjpk 				continue;
472745916cd2Sjpk 			}
472845916cd2Sjpk 
47297c478bd9Sstevel@tonic-gate 			if (zoneid == ALL_ZONES || ire->ire_zoneid == zoneid ||
47305597b60aSnordmark 			    ire->ire_zoneid == ALL_ZONES) {
47315597b60aSnordmark 				IRE_REFHOLD(ire);
47325597b60aSnordmark 				rw_exit(&irb_ptr->irb_lock);
47335597b60aSnordmark 				return (ire);
47345597b60aSnordmark 			}
47355597b60aSnordmark 
47365597b60aSnordmark 			if (ire->ire_type == IRE_LOCAL) {
4737f4b3ec61Sdh155122 				if (ipst->ips_ip_restrict_interzone_loopback &&
47385597b60aSnordmark 				    !ire_local_ok_across_zones(ire, zoneid,
4739f4b3ec61Sdh155122 				    &addr, tsl, ipst))
47405597b60aSnordmark 					continue;
47415597b60aSnordmark 
47427c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
47437c478bd9Sstevel@tonic-gate 				rw_exit(&irb_ptr->irb_lock);
47447c478bd9Sstevel@tonic-gate 				return (ire);
47457c478bd9Sstevel@tonic-gate 			}
47467c478bd9Sstevel@tonic-gate 		}
47477c478bd9Sstevel@tonic-gate 	}
47487c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
47497c478bd9Sstevel@tonic-gate 	return (NULL);
47507c478bd9Sstevel@tonic-gate }
47517c478bd9Sstevel@tonic-gate 
47527c478bd9Sstevel@tonic-gate /*
47537c478bd9Sstevel@tonic-gate  * Locate the interface ire that is tied to the cache ire 'cire' via
47547c478bd9Sstevel@tonic-gate  * cire->ire_ihandle.
47557c478bd9Sstevel@tonic-gate  *
47567c478bd9Sstevel@tonic-gate  * We are trying to create the cache ire for an offlink destn based
47577c478bd9Sstevel@tonic-gate  * on the cache ire of the gateway in 'cire'. 'pire' is the prefix ire
47587c478bd9Sstevel@tonic-gate  * as found by ip_newroute(). We are called from ip_newroute() in
47597c478bd9Sstevel@tonic-gate  * the IRE_CACHE case.
47607c478bd9Sstevel@tonic-gate  */
47617c478bd9Sstevel@tonic-gate ire_t *
47627c478bd9Sstevel@tonic-gate ire_ihandle_lookup_offlink(ire_t *cire, ire_t *pire)
47637c478bd9Sstevel@tonic-gate {
47647c478bd9Sstevel@tonic-gate 	ire_t	*ire;
47657c478bd9Sstevel@tonic-gate 	int	match_flags;
47667c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
47677c478bd9Sstevel@tonic-gate 	ipif_t	*gw_ipif;
4768f4b3ec61Sdh155122 	ip_stack_t	*ipst = cire->ire_ipst;
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 	ASSERT(cire != NULL && pire != NULL);
47717c478bd9Sstevel@tonic-gate 
47727c478bd9Sstevel@tonic-gate 	/*
47737c478bd9Sstevel@tonic-gate 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
47747c478bd9Sstevel@tonic-gate 	 * because the ihandle refers to an ipif which can be in only one zone.
47757c478bd9Sstevel@tonic-gate 	 */
47767c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
47777c478bd9Sstevel@tonic-gate 	/*
47787c478bd9Sstevel@tonic-gate 	 * ip_newroute calls ire_ftable_lookup with MATCH_IRE_ILL only
47797c478bd9Sstevel@tonic-gate 	 * for on-link hosts. We should never be here for onlink.
47807c478bd9Sstevel@tonic-gate 	 * Thus, use MATCH_IRE_ILL_GROUP.
47817c478bd9Sstevel@tonic-gate 	 */
47827c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
47837c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
47847c478bd9Sstevel@tonic-gate 	/*
47857c478bd9Sstevel@tonic-gate 	 * We know that the mask of the interface ire equals cire->ire_cmask.
47867c478bd9Sstevel@tonic-gate 	 * (When ip_newroute() created 'cire' for the gateway it set its
47877c478bd9Sstevel@tonic-gate 	 * cmask from the interface ire's mask)
47887c478bd9Sstevel@tonic-gate 	 */
47897c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
47907c478bd9Sstevel@tonic-gate 	    IRE_INTERFACE, pire->ire_ipif, NULL, ALL_ZONES, cire->ire_ihandle,
4791f4b3ec61Sdh155122 	    NULL, match_flags, ipst);
47927c478bd9Sstevel@tonic-gate 	if (ire != NULL)
47937c478bd9Sstevel@tonic-gate 		return (ire);
47947c478bd9Sstevel@tonic-gate 	/*
47957c478bd9Sstevel@tonic-gate 	 * If we didn't find an interface ire above, we can't declare failure.
47967c478bd9Sstevel@tonic-gate 	 * For backwards compatibility, we need to support prefix routes
47977c478bd9Sstevel@tonic-gate 	 * pointing to next hop gateways that are not on-link.
47987c478bd9Sstevel@tonic-gate 	 *
47997c478bd9Sstevel@tonic-gate 	 * Assume we are trying to ping some offlink destn, and we have the
48007c478bd9Sstevel@tonic-gate 	 * routing table below.
48017c478bd9Sstevel@tonic-gate 	 *
48027c478bd9Sstevel@tonic-gate 	 * Eg.	default	- gw1		<--- pire	(line 1)
48037c478bd9Sstevel@tonic-gate 	 *	gw1	- gw2				(line 2)
48047c478bd9Sstevel@tonic-gate 	 *	gw2	- hme0				(line 3)
48057c478bd9Sstevel@tonic-gate 	 *
48067c478bd9Sstevel@tonic-gate 	 * If we already have a cache ire for gw1 in 'cire', the
48077c478bd9Sstevel@tonic-gate 	 * ire_ftable_lookup above would have failed, since there is no
48087c478bd9Sstevel@tonic-gate 	 * interface ire to reach gw1. We will fallthru below.
48097c478bd9Sstevel@tonic-gate 	 *
48107c478bd9Sstevel@tonic-gate 	 * Here we duplicate the steps that ire_ftable_lookup() did in
48117c478bd9Sstevel@tonic-gate 	 * getting 'cire' from 'pire', in the MATCH_IRE_RECURSIVE case.
48127c478bd9Sstevel@tonic-gate 	 * The differences are the following
48137c478bd9Sstevel@tonic-gate 	 * i.   We want the interface ire only, so we call ire_ftable_lookup()
48147c478bd9Sstevel@tonic-gate 	 *	instead of ire_route_lookup()
48157c478bd9Sstevel@tonic-gate 	 * ii.  We look for only prefix routes in the 1st call below.
48167c478bd9Sstevel@tonic-gate 	 * ii.  We want to match on the ihandle in the 2nd call below.
48177c478bd9Sstevel@tonic-gate 	 */
48187c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE;
48197c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
48207c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
48217c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(pire->ire_gateway_addr, 0, 0, IRE_OFFSUBNET,
4822f4b3ec61Sdh155122 	    pire->ire_ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
48237c478bd9Sstevel@tonic-gate 	if (ire == NULL)
48247c478bd9Sstevel@tonic-gate 		return (NULL);
48257c478bd9Sstevel@tonic-gate 	/*
48267c478bd9Sstevel@tonic-gate 	 * At this point 'ire' corresponds to the entry shown in line 2.
48277c478bd9Sstevel@tonic-gate 	 * gw_addr is 'gw2' in the example above.
48287c478bd9Sstevel@tonic-gate 	 */
48297c478bd9Sstevel@tonic-gate 	gw_addr = ire->ire_gateway_addr;
48307c478bd9Sstevel@tonic-gate 	gw_ipif = ire->ire_ipif;
48317c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 	match_flags |= MATCH_IRE_IHANDLE;
48347c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
4835f4b3ec61Sdh155122 	    gw_ipif, NULL, ALL_ZONES, cire->ire_ihandle, NULL, match_flags,
4836f4b3ec61Sdh155122 	    ipst);
48377c478bd9Sstevel@tonic-gate 	return (ire);
48387c478bd9Sstevel@tonic-gate }
48397c478bd9Sstevel@tonic-gate 
48407c478bd9Sstevel@tonic-gate /*
48417c478bd9Sstevel@tonic-gate  * ire_mrtun_lookup() is called by ip_rput() when packet is to be
48427c478bd9Sstevel@tonic-gate  * tunneled through reverse tunnel. This is only supported for
48437c478bd9Sstevel@tonic-gate  * IPv4 packets
48447c478bd9Sstevel@tonic-gate  */
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate ire_t *
48477c478bd9Sstevel@tonic-gate ire_mrtun_lookup(ipaddr_t srcaddr, ill_t *ill)
48487c478bd9Sstevel@tonic-gate {
48497c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
48507c478bd9Sstevel@tonic-gate 	ire_t *ire;
4851f4b3ec61Sdh155122 	ip_stack_t	*ipst = ill->ill_ipst;
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
48547c478bd9Sstevel@tonic-gate 	ASSERT(!(ill->ill_isv6));
48557c478bd9Sstevel@tonic-gate 
4856f4b3ec61Sdh155122 	if (ipst->ips_ip_mrtun_table == NULL)
48577c478bd9Sstevel@tonic-gate 		return (NULL);
4858f4b3ec61Sdh155122 	irb_ptr = &ipst->ips_ip_mrtun_table[IRE_ADDR_HASH(srcaddr,
4859f4b3ec61Sdh155122 					    IP_MRTUN_TABLE_SIZE)];
48607c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
48617c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
48627c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
48637c478bd9Sstevel@tonic-gate 			continue;
48647c478bd9Sstevel@tonic-gate 		if ((ire->ire_in_src_addr == srcaddr) &&
48657c478bd9Sstevel@tonic-gate 		    ire->ire_in_ill == ill) {
48667c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
48677c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
48687c478bd9Sstevel@tonic-gate 			return (ire);
48697c478bd9Sstevel@tonic-gate 		}
48707c478bd9Sstevel@tonic-gate 	}
48717c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
48727c478bd9Sstevel@tonic-gate 	return (NULL);
48737c478bd9Sstevel@tonic-gate }
48747c478bd9Sstevel@tonic-gate 
48757c478bd9Sstevel@tonic-gate /*
48767c478bd9Sstevel@tonic-gate  * Return the IRE_LOOPBACK, IRE_IF_RESOLVER or IRE_IF_NORESOLVER
48777c478bd9Sstevel@tonic-gate  * ire associated with the specified ipif.
48787c478bd9Sstevel@tonic-gate  *
48797c478bd9Sstevel@tonic-gate  * This might occasionally be called when IPIF_UP is not set since
48807c478bd9Sstevel@tonic-gate  * the IP_MULTICAST_IF as well as creating interface routes
48817c478bd9Sstevel@tonic-gate  * allows specifying a down ipif (ipif_lookup* match ipifs that are down).
48827c478bd9Sstevel@tonic-gate  *
48837c478bd9Sstevel@tonic-gate  * Note that if IPIF_NOLOCAL, IPIF_NOXMIT, or IPIF_DEPRECATED is set on
48847c478bd9Sstevel@tonic-gate  * the ipif, this routine might return NULL.
48857c478bd9Sstevel@tonic-gate  */
48867c478bd9Sstevel@tonic-gate ire_t *
488745916cd2Sjpk ipif_to_ire(const ipif_t *ipif)
48887c478bd9Sstevel@tonic-gate {
48897c478bd9Sstevel@tonic-gate 	ire_t	*ire;
4890f4b3ec61Sdh155122 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 	ASSERT(!ipif->ipif_isv6);
48937c478bd9Sstevel@tonic-gate 	if (ipif->ipif_ire_type == IRE_LOOPBACK) {
48947c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(ipif->ipif_lcl_addr, 0, IRE_LOOPBACK,
4895f4b3ec61Sdh155122 		    ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF),
4896f4b3ec61Sdh155122 		    ipst);
48977c478bd9Sstevel@tonic-gate 	} else if (ipif->ipif_flags & IPIF_POINTOPOINT) {
48987c478bd9Sstevel@tonic-gate 		/* In this case we need to lookup destination address. */
48997c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_pp_dst_addr, IP_HOST_MASK, 0,
490045916cd2Sjpk 		    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
4901f4b3ec61Sdh155122 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF | MATCH_IRE_MASK), ipst);
49027c478bd9Sstevel@tonic-gate 	} else {
49037c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_subnet,
49047c478bd9Sstevel@tonic-gate 		    ipif->ipif_net_mask, 0, IRE_INTERFACE, ipif, NULL,
490545916cd2Sjpk 		    ALL_ZONES, 0, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF |
4906f4b3ec61Sdh155122 		    MATCH_IRE_MASK), ipst);
49077c478bd9Sstevel@tonic-gate 	}
49087c478bd9Sstevel@tonic-gate 	return (ire);
49097c478bd9Sstevel@tonic-gate }
49107c478bd9Sstevel@tonic-gate 
49117c478bd9Sstevel@tonic-gate /*
49127c478bd9Sstevel@tonic-gate  * ire_walk function.
49137c478bd9Sstevel@tonic-gate  * Count the number of IRE_CACHE entries in different categories.
49147c478bd9Sstevel@tonic-gate  */
49157c478bd9Sstevel@tonic-gate void
49167c478bd9Sstevel@tonic-gate ire_cache_count(ire_t *ire, char *arg)
49177c478bd9Sstevel@tonic-gate {
49187c478bd9Sstevel@tonic-gate 	ire_cache_count_t *icc = (ire_cache_count_t *)arg;
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
49217c478bd9Sstevel@tonic-gate 		return;
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 	icc->icc_total++;
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
49267c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
49277c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
49287c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
49297c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
49307c478bd9Sstevel@tonic-gate 			return;
49317c478bd9Sstevel@tonic-gate 		}
49327c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
49337c478bd9Sstevel@tonic-gate 	} else {
49347c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
49357c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
49367c478bd9Sstevel@tonic-gate 			return;
49377c478bd9Sstevel@tonic-gate 		}
49387c478bd9Sstevel@tonic-gate 	}
49397c478bd9Sstevel@tonic-gate 
49407c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
49417c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu)
49427c478bd9Sstevel@tonic-gate 		icc->icc_pmtu++;
49437c478bd9Sstevel@tonic-gate 	else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
49447c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count)
49457c478bd9Sstevel@tonic-gate 		icc->icc_offlink++;
49467c478bd9Sstevel@tonic-gate 	else
49477c478bd9Sstevel@tonic-gate 		icc->icc_unused++;
49487c478bd9Sstevel@tonic-gate }
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate /*
49517c478bd9Sstevel@tonic-gate  * ire_walk function called by ip_trash_ire_reclaim().
49527c478bd9Sstevel@tonic-gate  * Free a fraction of the IRE_CACHE cache entries. The fractions are
49537c478bd9Sstevel@tonic-gate  * different for different categories of IRE_CACHE entries.
49547c478bd9Sstevel@tonic-gate  * A fraction of zero means to not free any in that category.
49557c478bd9Sstevel@tonic-gate  * Use the hash bucket id plus lbolt as a random number. Thus if the fraction
49567c478bd9Sstevel@tonic-gate  * is N then every Nth hash bucket chain will be freed.
49577c478bd9Sstevel@tonic-gate  */
49587c478bd9Sstevel@tonic-gate void
49597c478bd9Sstevel@tonic-gate ire_cache_reclaim(ire_t *ire, char *arg)
49607c478bd9Sstevel@tonic-gate {
49617c478bd9Sstevel@tonic-gate 	ire_cache_reclaim_t *icr = (ire_cache_reclaim_t *)arg;
49627c478bd9Sstevel@tonic-gate 	uint_t rand;
4963f4b3ec61Sdh155122 	ip_stack_t	*ipst = icr->icr_ipst;
49647c478bd9Sstevel@tonic-gate 
49657c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
49667c478bd9Sstevel@tonic-gate 		return;
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
49697c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
4970f4b3ec61Sdh155122 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6,
4971f4b3ec61Sdh155122 			ipst->ips_ip6_cache_table_size);
49727c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
49737c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
49747c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
49757c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
49767c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
49777c478bd9Sstevel@tonic-gate 				ire_delete(ire);
49787c478bd9Sstevel@tonic-gate 				return;
49797c478bd9Sstevel@tonic-gate 			}
49807c478bd9Sstevel@tonic-gate 			goto done;
49817c478bd9Sstevel@tonic-gate 		}
49827c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
49837c478bd9Sstevel@tonic-gate 	} else {
49847c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
4985f4b3ec61Sdh155122 		    IRE_ADDR_HASH(ire->ire_addr, ipst->ips_ip_cache_table_size);
49867c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
49877c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
49887c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
49897c478bd9Sstevel@tonic-gate 				ire_delete(ire);
49907c478bd9Sstevel@tonic-gate 				return;
49917c478bd9Sstevel@tonic-gate 			}
49927c478bd9Sstevel@tonic-gate 			goto done;
49937c478bd9Sstevel@tonic-gate 		}
49947c478bd9Sstevel@tonic-gate 	}
49957c478bd9Sstevel@tonic-gate 	/* Not onlink IRE */
49967c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
49977c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) {
49987c478bd9Sstevel@tonic-gate 		/* Use ptmu fraction */
49997c478bd9Sstevel@tonic-gate 		if (icr->icr_pmtu != 0 &&
50007c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_pmtu)*icr->icr_pmtu == rand) {
50017c478bd9Sstevel@tonic-gate 			ire_delete(ire);
50027c478bd9Sstevel@tonic-gate 			return;
50037c478bd9Sstevel@tonic-gate 		}
50047c478bd9Sstevel@tonic-gate 	} else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
50057c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count) {
50067c478bd9Sstevel@tonic-gate 		/* Use offlink fraction */
50077c478bd9Sstevel@tonic-gate 		if (icr->icr_offlink != 0 &&
50087c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_offlink)*icr->icr_offlink == rand) {
50097c478bd9Sstevel@tonic-gate 			ire_delete(ire);
50107c478bd9Sstevel@tonic-gate 			return;
50117c478bd9Sstevel@tonic-gate 		}
50127c478bd9Sstevel@tonic-gate 	} else {
50137c478bd9Sstevel@tonic-gate 		/* Use unused fraction */
50147c478bd9Sstevel@tonic-gate 		if (icr->icr_unused != 0 &&
50157c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_unused)*icr->icr_unused == rand) {
50167c478bd9Sstevel@tonic-gate 			ire_delete(ire);
50177c478bd9Sstevel@tonic-gate 			return;
50187c478bd9Sstevel@tonic-gate 		}
50197c478bd9Sstevel@tonic-gate 	}
50207c478bd9Sstevel@tonic-gate done:
50217c478bd9Sstevel@tonic-gate 	/*
50227c478bd9Sstevel@tonic-gate 	 * Update tire_mark so that those that haven't been used since this
50237c478bd9Sstevel@tonic-gate 	 * reclaim will be considered unused next time we reclaim.
50247c478bd9Sstevel@tonic-gate 	 */
50257c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
50267c478bd9Sstevel@tonic-gate }
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate static void
50297c478bd9Sstevel@tonic-gate power2_roundup(uint32_t *value)
50307c478bd9Sstevel@tonic-gate {
50317c478bd9Sstevel@tonic-gate 	int i;
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 	for (i = 1; i < 31; i++) {
50347c478bd9Sstevel@tonic-gate 		if (*value <= (1 << i))
50357c478bd9Sstevel@tonic-gate 			break;
50367c478bd9Sstevel@tonic-gate 	}
50377c478bd9Sstevel@tonic-gate 	*value = (1 << i);
50387c478bd9Sstevel@tonic-gate }
50397c478bd9Sstevel@tonic-gate 
5040f4b3ec61Sdh155122 /* Global init for all zones */
50417c478bd9Sstevel@tonic-gate void
5042f4b3ec61Sdh155122 ip_ire_g_init()
50437c478bd9Sstevel@tonic-gate {
50447c478bd9Sstevel@tonic-gate 	/*
50457c478bd9Sstevel@tonic-gate 	 * Create ire caches, ire_reclaim()
50467c478bd9Sstevel@tonic-gate 	 * will give IRE_CACHE back to system when needed.
50477c478bd9Sstevel@tonic-gate 	 * This needs to be done here before anything else, since
50487c478bd9Sstevel@tonic-gate 	 * ire_add() expects the cache to be created.
50497c478bd9Sstevel@tonic-gate 	 */
50507c478bd9Sstevel@tonic-gate 	ire_cache = kmem_cache_create("ire_cache",
50517c478bd9Sstevel@tonic-gate 		sizeof (ire_t), 0, ip_ire_constructor,
50527c478bd9Sstevel@tonic-gate 		ip_ire_destructor, ip_trash_ire_reclaim, NULL, NULL, 0);
50537c478bd9Sstevel@tonic-gate 
5054f4b3ec61Sdh155122 	rt_entry_cache = kmem_cache_create("rt_entry",
5055f4b3ec61Sdh155122 	    sizeof (struct rt_entry), 0, NULL, NULL, NULL, NULL, NULL, 0);
5056f4b3ec61Sdh155122 
5057f4b3ec61Sdh155122 	/*
5058f4b3ec61Sdh155122 	 * Have radix code setup kmem caches etc.
5059f4b3ec61Sdh155122 	 */
5060f4b3ec61Sdh155122 	rn_init();
5061f4b3ec61Sdh155122 }
5062f4b3ec61Sdh155122 
5063f4b3ec61Sdh155122 void
5064f4b3ec61Sdh155122 ip_ire_init(ip_stack_t *ipst)
5065f4b3ec61Sdh155122 {
5066f4b3ec61Sdh155122 	int i;
5067f4b3ec61Sdh155122 	uint32_t mem_cnt;
5068f4b3ec61Sdh155122 	uint32_t cpu_cnt;
5069f4b3ec61Sdh155122 	uint32_t min_cnt;
5070f4b3ec61Sdh155122 	pgcnt_t mem_avail;
5071f4b3ec61Sdh155122 
5072f4b3ec61Sdh155122 	/*
5073f4b3ec61Sdh155122 	 * ip_ire_max_bucket_cnt is sized below based on the memory
5074f4b3ec61Sdh155122 	 * size and the cpu speed of the machine. This is upper
5075f4b3ec61Sdh155122 	 * bounded by the compile time value of ip_ire_max_bucket_cnt
5076f4b3ec61Sdh155122 	 * and is lower bounded by the compile time value of
5077f4b3ec61Sdh155122 	 * ip_ire_min_bucket_cnt.  Similar logic applies to
5078f4b3ec61Sdh155122 	 * ip6_ire_max_bucket_cnt.
5079f4b3ec61Sdh155122 	 *
5080f4b3ec61Sdh155122 	 * We calculate this for each IP Instances in order to use
5081f4b3ec61Sdh155122 	 * the kmem_avail and ip_ire_{min,max}_bucket_cnt that are
5082f4b3ec61Sdh155122 	 * in effect when the zone is booted.
5083f4b3ec61Sdh155122 	 */
5084f4b3ec61Sdh155122 	mem_avail = kmem_avail();
5085f4b3ec61Sdh155122 	mem_cnt = (mem_avail >> ip_ire_mem_ratio) /
5086f4b3ec61Sdh155122 	    ip_cache_table_size / sizeof (ire_t);
5087f4b3ec61Sdh155122 	cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio;
5088f4b3ec61Sdh155122 
5089f4b3ec61Sdh155122 	min_cnt = MIN(cpu_cnt, mem_cnt);
5090f4b3ec61Sdh155122 	if (min_cnt < ip_ire_min_bucket_cnt)
5091f4b3ec61Sdh155122 		min_cnt = ip_ire_min_bucket_cnt;
5092f4b3ec61Sdh155122 	if (ip_ire_max_bucket_cnt > min_cnt) {
5093f4b3ec61Sdh155122 		ip_ire_max_bucket_cnt = min_cnt;
5094f4b3ec61Sdh155122 	}
5095f4b3ec61Sdh155122 
5096f4b3ec61Sdh155122 	mem_cnt = (mem_avail >> ip_ire_mem_ratio) /
5097f4b3ec61Sdh155122 	    ip6_cache_table_size / sizeof (ire_t);
5098f4b3ec61Sdh155122 	min_cnt = MIN(cpu_cnt, mem_cnt);
5099f4b3ec61Sdh155122 	if (min_cnt < ip6_ire_min_bucket_cnt)
5100f4b3ec61Sdh155122 		min_cnt = ip6_ire_min_bucket_cnt;
5101f4b3ec61Sdh155122 	if (ip6_ire_max_bucket_cnt > min_cnt) {
5102f4b3ec61Sdh155122 		ip6_ire_max_bucket_cnt = min_cnt;
5103f4b3ec61Sdh155122 	}
5104f4b3ec61Sdh155122 
5105f4b3ec61Sdh155122 	mutex_init(&ipst->ips_ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
5106f4b3ec61Sdh155122 	mutex_init(&ipst->ips_ire_handle_lock, NULL, MUTEX_DEFAULT, NULL);
5107f4b3ec61Sdh155122 	mutex_init(&ipst->ips_ire_mrtun_lock, NULL, MUTEX_DEFAULT, NULL);
5108f4b3ec61Sdh155122 	mutex_init(&ipst->ips_ire_srcif_table_lock, NULL, MUTEX_DEFAULT, NULL);
5109f4b3ec61Sdh155122 
5110f4b3ec61Sdh155122 	(void) rn_inithead((void **)&ipst->ips_ip_ftable, 32);
5111f4b3ec61Sdh155122 
5112f4b3ec61Sdh155122 
5113f4b3ec61Sdh155122 	/* Calculate the IPv4 cache table size. */
5114f4b3ec61Sdh155122 	ipst->ips_ip_cache_table_size = MAX(ip_cache_table_size,
5115f4b3ec61Sdh155122 	    ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) /
5116f4b3ec61Sdh155122 	    ip_ire_max_bucket_cnt));
5117f4b3ec61Sdh155122 	if (ipst->ips_ip_cache_table_size > ip_max_cache_table_size)
5118f4b3ec61Sdh155122 		ipst->ips_ip_cache_table_size = ip_max_cache_table_size;
5119f4b3ec61Sdh155122 	/*
5120f4b3ec61Sdh155122 	 * Make sure that the table size is always a power of 2.  The
5121f4b3ec61Sdh155122 	 * hash macro IRE_ADDR_HASH() depends on that.
5122f4b3ec61Sdh155122 	 */
5123f4b3ec61Sdh155122 	power2_roundup(&ipst->ips_ip_cache_table_size);
5124f4b3ec61Sdh155122 
5125f4b3ec61Sdh155122 	ipst->ips_ip_cache_table = kmem_zalloc(ipst->ips_ip_cache_table_size *
5126f4b3ec61Sdh155122 	    sizeof (irb_t), KM_SLEEP);
5127f4b3ec61Sdh155122 
5128f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
5129f4b3ec61Sdh155122 		rw_init(&ipst->ips_ip_cache_table[i].irb_lock, NULL,
5130f4b3ec61Sdh155122 		    RW_DEFAULT, NULL);
5131f4b3ec61Sdh155122 	}
5132f4b3ec61Sdh155122 
5133f4b3ec61Sdh155122 	/* Calculate the IPv6 cache table size. */
5134f4b3ec61Sdh155122 	ipst->ips_ip6_cache_table_size = MAX(ip6_cache_table_size,
5135f4b3ec61Sdh155122 	    ((mem_avail >> ip_ire_mem_ratio) / sizeof (ire_t) /
5136f4b3ec61Sdh155122 	    ip6_ire_max_bucket_cnt));
5137f4b3ec61Sdh155122 	if (ipst->ips_ip6_cache_table_size > ip6_max_cache_table_size)
5138f4b3ec61Sdh155122 		ipst->ips_ip6_cache_table_size = ip6_max_cache_table_size;
5139f4b3ec61Sdh155122 	/*
5140f4b3ec61Sdh155122 	 * Make sure that the table size is always a power of 2.  The
5141f4b3ec61Sdh155122 	 * hash macro IRE_ADDR_HASH_V6() depends on that.
5142f4b3ec61Sdh155122 	 */
5143f4b3ec61Sdh155122 	power2_roundup(&ipst->ips_ip6_cache_table_size);
5144f4b3ec61Sdh155122 
5145f4b3ec61Sdh155122 	ipst->ips_ip_cache_table_v6 = kmem_zalloc(
5146f4b3ec61Sdh155122 	    ipst->ips_ip6_cache_table_size * sizeof (irb_t), KM_SLEEP);
5147f4b3ec61Sdh155122 
5148f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) {
5149f4b3ec61Sdh155122 		rw_init(&ipst->ips_ip_cache_table_v6[i].irb_lock, NULL,
5150f4b3ec61Sdh155122 		    RW_DEFAULT, NULL);
5151f4b3ec61Sdh155122 	}
5152f4b3ec61Sdh155122 
51537c478bd9Sstevel@tonic-gate 	/*
51547c478bd9Sstevel@tonic-gate 	 * Initialize ip_mrtun_table to NULL now, it will be
51557c478bd9Sstevel@tonic-gate 	 * populated by ip_rt_add if reverse tunnel is created
51567c478bd9Sstevel@tonic-gate 	 */
5157f4b3ec61Sdh155122 	ipst->ips_ip_mrtun_table = NULL;
51587c478bd9Sstevel@tonic-gate 
51597c478bd9Sstevel@tonic-gate 	/*
51607c478bd9Sstevel@tonic-gate 	 * Make sure that the forwarding table size is a power of 2.
51617c478bd9Sstevel@tonic-gate 	 * The IRE*_ADDR_HASH() macroes depend on that.
51627c478bd9Sstevel@tonic-gate 	 */
5163f4b3ec61Sdh155122 	ipst->ips_ip6_ftable_hash_size = ip6_ftable_hash_size;
5164f4b3ec61Sdh155122 	power2_roundup(&ipst->ips_ip6_ftable_hash_size);
5165f4b3ec61Sdh155122 
5166f4b3ec61Sdh155122 	ipst->ips_ire_handle = 1;
51677c478bd9Sstevel@tonic-gate }
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate void
5170f4b3ec61Sdh155122 ip_ire_g_fini(void)
5171f4b3ec61Sdh155122 {
5172f4b3ec61Sdh155122 	kmem_cache_destroy(ire_cache);
5173f4b3ec61Sdh155122 	kmem_cache_destroy(rt_entry_cache);
5174f4b3ec61Sdh155122 
5175f4b3ec61Sdh155122 	rn_fini();
5176f4b3ec61Sdh155122 }
5177f4b3ec61Sdh155122 
5178f4b3ec61Sdh155122 void
5179f4b3ec61Sdh155122 ip_ire_fini(ip_stack_t *ipst)
51807c478bd9Sstevel@tonic-gate {
51817c478bd9Sstevel@tonic-gate 	int i;
51827c478bd9Sstevel@tonic-gate 
5183f4b3ec61Sdh155122 	/*
5184f4b3ec61Sdh155122 	 * Delete all IREs - assumes that the ill/ipifs have
5185f4b3ec61Sdh155122 	 * been removed so what remains are just the ftable and IRE_CACHE.
5186f4b3ec61Sdh155122 	 */
5187f4b3ec61Sdh155122 	ire_walk_ill_mrtun(0, 0, ire_delete, NULL, NULL, ipst);
5188f4b3ec61Sdh155122 	ire_walk(ire_delete, NULL, ipst);
5189c793af95Ssangeeta 
5190f4b3ec61Sdh155122 	rn_freehead(ipst->ips_ip_ftable);
5191f4b3ec61Sdh155122 	ipst->ips_ip_ftable = NULL;
51927c478bd9Sstevel@tonic-gate 
5193f4b3ec61Sdh155122 	mutex_destroy(&ipst->ips_ire_ft_init_lock);
5194f4b3ec61Sdh155122 	mutex_destroy(&ipst->ips_ire_handle_lock);
5195f4b3ec61Sdh155122 	mutex_destroy(&ipst->ips_ire_mrtun_lock);
5196f4b3ec61Sdh155122 	mutex_destroy(&ipst->ips_ire_srcif_table_lock);
5197f4b3ec61Sdh155122 
5198f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip_cache_table_size; i++) {
5199f4b3ec61Sdh155122 		ASSERT(ipst->ips_ip_cache_table[i].irb_ire == NULL);
5200f4b3ec61Sdh155122 		rw_destroy(&ipst->ips_ip_cache_table[i].irb_lock);
52017c478bd9Sstevel@tonic-gate 	}
5202f4b3ec61Sdh155122 	kmem_free(ipst->ips_ip_cache_table,
5203f4b3ec61Sdh155122 	    ipst->ips_ip_cache_table_size * sizeof (irb_t));
5204f4b3ec61Sdh155122 	ipst->ips_ip_cache_table = NULL;
52057c478bd9Sstevel@tonic-gate 
5206f4b3ec61Sdh155122 	for (i = 0; i < ipst->ips_ip6_cache_table_size; i++) {
5207f4b3ec61Sdh155122 		ASSERT(ipst->ips_ip_cache_table_v6[i].irb_ire == NULL);
5208f4b3ec61Sdh155122 		rw_destroy(&ipst->ips_ip_cache_table_v6[i].irb_lock);
52097c478bd9Sstevel@tonic-gate 	}
5210f4b3ec61Sdh155122 	kmem_free(ipst->ips_ip_cache_table_v6,
5211f4b3ec61Sdh155122 	    ipst->ips_ip6_cache_table_size * sizeof (irb_t));
5212f4b3ec61Sdh155122 	ipst->ips_ip_cache_table_v6 = NULL;
52137c478bd9Sstevel@tonic-gate 
5214f4b3ec61Sdh155122 	if (ipst->ips_ip_mrtun_table != NULL) {
52157c478bd9Sstevel@tonic-gate 		for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5216f4b3ec61Sdh155122 			ASSERT(ipst->ips_ip_mrtun_table[i].irb_ire == NULL);
5217f4b3ec61Sdh155122 			rw_destroy(&ipst->ips_ip_mrtun_table[i].irb_lock);
52187c478bd9Sstevel@tonic-gate 		}
5219f4b3ec61Sdh155122 		kmem_free(ipst->ips_ip_mrtun_table,
5220f4b3ec61Sdh155122 		    IP_MRTUN_TABLE_SIZE * sizeof (irb_t));
5221f4b3ec61Sdh155122 		ipst->ips_ip_mrtun_table = NULL;
52227c478bd9Sstevel@tonic-gate 	}
5223f4b3ec61Sdh155122 
5224f4b3ec61Sdh155122 	for (i = 0; i < IP6_MASK_TABLE_SIZE; i++) {
5225f4b3ec61Sdh155122 		irb_t *ptr;
5226f4b3ec61Sdh155122 		int j;
5227f4b3ec61Sdh155122 
5228f4b3ec61Sdh155122 		if ((ptr = ipst->ips_ip_forwarding_table_v6[i]) == NULL)
5229f4b3ec61Sdh155122 			continue;
5230f4b3ec61Sdh155122 
5231f4b3ec61Sdh155122 		for (j = 0; j < ipst->ips_ip6_ftable_hash_size; j++) {
5232f4b3ec61Sdh155122 			ASSERT(ptr[j].irb_ire == NULL);
5233f4b3ec61Sdh155122 			rw_destroy(&ptr[j].irb_lock);
5234f4b3ec61Sdh155122 		}
5235f4b3ec61Sdh155122 		mi_free(ptr);
5236f4b3ec61Sdh155122 		ipst->ips_ip_forwarding_table_v6[i] = NULL;
5237f4b3ec61Sdh155122 	}
52387c478bd9Sstevel@tonic-gate }
52397c478bd9Sstevel@tonic-gate 
52407c478bd9Sstevel@tonic-gate int
52417c478bd9Sstevel@tonic-gate ire_add_mrtun(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
52427c478bd9Sstevel@tonic-gate {
52437c478bd9Sstevel@tonic-gate 	ire_t   *ire1;
52447c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
52457c478bd9Sstevel@tonic-gate 	ire_t	**irep;
5246f4b3ec61Sdh155122 	ire_t	*ire = *ire_p;
52477c478bd9Sstevel@tonic-gate 	int	i;
52487c478bd9Sstevel@tonic-gate 	uint_t	max_frag;
52497c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
52507c478bd9Sstevel@tonic-gate 	int error;
5251f4b3ec61Sdh155122 	ip_stack_t	*ipst = ire->ire_ipst;
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
52547c478bd9Sstevel@tonic-gate 	/* Is ip_mrtun_table empty ? */
52557c478bd9Sstevel@tonic-gate 
5256f4b3ec61Sdh155122 	if (ipst->ips_ip_mrtun_table == NULL) {
52577c478bd9Sstevel@tonic-gate 		/* create the mrtun table */
5258f4b3ec61Sdh155122 		mutex_enter(&ipst->ips_ire_mrtun_lock);
5259f4b3ec61Sdh155122 		if (ipst->ips_ip_mrtun_table == NULL) {
5260f4b3ec61Sdh155122 			ipst->ips_ip_mrtun_table = kmem_zalloc(
5261f4b3ec61Sdh155122 			    IP_MRTUN_TABLE_SIZE * sizeof (irb_t), KM_NOSLEEP);
52627c478bd9Sstevel@tonic-gate 
5263f4b3ec61Sdh155122 			if (ipst->ips_ip_mrtun_table == NULL) {
52647c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_add_mrtun: allocation failure\n"));
5265f4b3ec61Sdh155122 				mutex_exit(&ipst->ips_ire_mrtun_lock);
52667c478bd9Sstevel@tonic-gate 				ire_refrele(ire);
52677c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
52687c478bd9Sstevel@tonic-gate 				return (ENOMEM);
52697c478bd9Sstevel@tonic-gate 			}
52707c478bd9Sstevel@tonic-gate 
52717c478bd9Sstevel@tonic-gate 			for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
5272f4b3ec61Sdh155122 			    rw_init(&ipst->ips_ip_mrtun_table[i].irb_lock, NULL,
52737c478bd9Sstevel@tonic-gate 				    RW_DEFAULT, NULL);
52747c478bd9Sstevel@tonic-gate 			}
52757c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_add_mrtun: mrtun table is created\n"));
52767c478bd9Sstevel@tonic-gate 		}
52777c478bd9Sstevel@tonic-gate 		/* some other thread got it and created the table */
5278f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_mrtun_lock);
52797c478bd9Sstevel@tonic-gate 	}
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 	/*
52827c478bd9Sstevel@tonic-gate 	 * Check for duplicate in the bucket and insert in the table
52837c478bd9Sstevel@tonic-gate 	 */
5284f4b3ec61Sdh155122 	irb_ptr = &(ipst->ips_ip_mrtun_table[IRE_ADDR_HASH(ire->ire_in_src_addr,
52857c478bd9Sstevel@tonic-gate 	    IP_MRTUN_TABLE_SIZE)]);
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate 	/*
52887c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
52897c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
52907c478bd9Sstevel@tonic-gate 	 *
52917c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
52927c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
52937c478bd9Sstevel@tonic-gate 	 */
52947c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
52957c478bd9Sstevel@tonic-gate 	if (error != 0) {
52967c478bd9Sstevel@tonic-gate 		/*
52977c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
52987c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
52997c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
53007c478bd9Sstevel@tonic-gate 		 */
53017c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
53027c478bd9Sstevel@tonic-gate 		ire_delete(ire);
53037c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_add_mrtun: ire_atomic_start failed\n"));
53047c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
53057c478bd9Sstevel@tonic-gate 		return (error);
53067c478bd9Sstevel@tonic-gate 	}
53077c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
53087c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
53097c478bd9Sstevel@tonic-gate 			continue;
53107c478bd9Sstevel@tonic-gate 		/* has anyone inserted the route in the meanwhile ? */
53117c478bd9Sstevel@tonic-gate 		if (ire1->ire_in_ill == ire->ire_in_ill &&
53127c478bd9Sstevel@tonic-gate 		    ire1->ire_in_src_addr == ire->ire_in_src_addr) {
53137c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_mrtun: Duplicate entry exists\n"));
53147c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
53157c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
53167c478bd9Sstevel@tonic-gate 			ire_delete(ire);
53177c478bd9Sstevel@tonic-gate 			/* Return the old ire */
53187c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
53197c478bd9Sstevel@tonic-gate 			return (0);
53207c478bd9Sstevel@tonic-gate 		}
53217c478bd9Sstevel@tonic-gate 	}
53227c478bd9Sstevel@tonic-gate 
53237c478bd9Sstevel@tonic-gate 	/* Atomically set the ire_max_frag */
53247c478bd9Sstevel@tonic-gate 	max_frag = *ire->ire_max_fragp;
53257c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = NULL;
53267c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
5327c793af95Ssangeeta 	ASSERT(ire->ire_type != IRE_CACHE);
53287c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
53297c478bd9Sstevel@tonic-gate 	if (*irep != NULL) {
53307c478bd9Sstevel@tonic-gate 		/* Find the last ire which matches ire_in_src_addr */
53317c478bd9Sstevel@tonic-gate 		ire1 = *irep;
53327c478bd9Sstevel@tonic-gate 		while (ire1->ire_in_src_addr == ire->ire_in_src_addr) {
53337c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
53347c478bd9Sstevel@tonic-gate 			ire1 = *irep;
53357c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
53367c478bd9Sstevel@tonic-gate 				break;
53377c478bd9Sstevel@tonic-gate 		}
53387c478bd9Sstevel@tonic-gate 	}
53397c478bd9Sstevel@tonic-gate 	ire1 = *irep;
53407c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
53417c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
53427c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
53437c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
53447c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
53457c478bd9Sstevel@tonic-gate 	membar_producer();
53467c478bd9Sstevel@tonic-gate 	*irep = ire;
53477c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
53487c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_add_mrtun: created and linked ire %p\n", (void *)*irep));
53517c478bd9Sstevel@tonic-gate 
53527c478bd9Sstevel@tonic-gate 	/*
53537c478bd9Sstevel@tonic-gate 	 * Protect ire_mrtun_count and ill_mrtun_refcnt from
53547c478bd9Sstevel@tonic-gate 	 * another thread trying to add ire in the table
53557c478bd9Sstevel@tonic-gate 	 */
5356f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ire_mrtun_lock);
5357f4b3ec61Sdh155122 	ipst->ips_ire_mrtun_count++;
5358f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ire_mrtun_lock);
53597c478bd9Sstevel@tonic-gate 	/*
53607c478bd9Sstevel@tonic-gate 	 * ill_mrtun_refcnt is protected by the ill_lock held via
53617c478bd9Sstevel@tonic-gate 	 * ire_atomic_start
53627c478bd9Sstevel@tonic-gate 	 */
53637c478bd9Sstevel@tonic-gate 	ire->ire_in_ill->ill_mrtun_refcnt++;
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
53667c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
53677c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
53687c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
53697c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
53707c478bd9Sstevel@tonic-gate 		}
53717c478bd9Sstevel@tonic-gate 	} else {
53727c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
53737c478bd9Sstevel@tonic-gate 	}
53747c478bd9Sstevel@tonic-gate 
53757c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
5376516bda92Ssowmini 	nce_fastpath(ire->ire_nce);
53777c478bd9Sstevel@tonic-gate 	*ire_p = ire;
53787c478bd9Sstevel@tonic-gate 	return (0);
53797c478bd9Sstevel@tonic-gate }
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 
53827c478bd9Sstevel@tonic-gate /* Walks down the mrtun table */
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate void
53857c478bd9Sstevel@tonic-gate ire_walk_ill_mrtun(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
5386f4b3ec61Sdh155122     ill_t *ill, ip_stack_t *ipst)
53877c478bd9Sstevel@tonic-gate {
53887c478bd9Sstevel@tonic-gate 	irb_t	*irb;
53897c478bd9Sstevel@tonic-gate 	ire_t	*ire;
53907c478bd9Sstevel@tonic-gate 	int	i;
53917c478bd9Sstevel@tonic-gate 	int	ret;
53927c478bd9Sstevel@tonic-gate 
53937c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
53947c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
53957c478bd9Sstevel@tonic-gate 	ASSERT(match_flags == 0 || ire_type == IRE_MIPRTUN);
53967c478bd9Sstevel@tonic-gate 
5397f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ire_mrtun_lock);
5398f4b3ec61Sdh155122 	if (ipst->ips_ire_mrtun_count == 0) {
5399f4b3ec61Sdh155122 		mutex_exit(&ipst->ips_ire_mrtun_lock);
54007c478bd9Sstevel@tonic-gate 		return;
54017c478bd9Sstevel@tonic-gate 	}
5402f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ire_mrtun_lock);
54037c478bd9Sstevel@tonic-gate 
54047c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_walk_ill_mrtun:walking the reverse tunnel table \n"));
54057c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
54067c478bd9Sstevel@tonic-gate 
5407f4b3ec61Sdh155122 		irb = &(ipst->ips_ip_mrtun_table[i]);
54087c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
54097c478bd9Sstevel@tonic-gate 			continue;
54107c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb);
54117c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL;
54127c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
54137c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV4_VERSION);
54147c478bd9Sstevel@tonic-gate 			if (match_flags != 0) {
54157c478bd9Sstevel@tonic-gate 				ret = ire_walk_ill_match(
54167c478bd9Sstevel@tonic-gate 				    match_flags, ire_type,
5417f4b3ec61Sdh155122 				    ire, ill, ALL_ZONES, ipst);
54187c478bd9Sstevel@tonic-gate 			}
54197c478bd9Sstevel@tonic-gate 			if (match_flags == 0 || ret)
54207c478bd9Sstevel@tonic-gate 				(*func)(ire, arg);
54217c478bd9Sstevel@tonic-gate 		}
54227c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb);
54237c478bd9Sstevel@tonic-gate 	}
54247c478bd9Sstevel@tonic-gate }
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate /*
54277c478bd9Sstevel@tonic-gate  * Source interface based lookup routine (IPV4 only).
54287c478bd9Sstevel@tonic-gate  * This routine is called only when RTA_SRCIFP bitflag is set
54297c478bd9Sstevel@tonic-gate  * by routing socket while adding/deleting the route and it is
54307c478bd9Sstevel@tonic-gate  * also called from ip_rput() when packets arrive from an interface
54317c478bd9Sstevel@tonic-gate  * for which ill_srcif_ref_cnt is positive. This function is useful
54327c478bd9Sstevel@tonic-gate  * when a packet coming from one interface must be forwarded to another
54337c478bd9Sstevel@tonic-gate  * designated interface to reach the correct node. This function is also
54347c478bd9Sstevel@tonic-gate  * called from ip_newroute when the link-layer address of an ire is resolved.
54357c478bd9Sstevel@tonic-gate  * We need to make sure that ip_newroute searches for IRE_IF_RESOLVER type
54367c478bd9Sstevel@tonic-gate  * ires--thus the ire_type parameter is needed.
54377c478bd9Sstevel@tonic-gate  */
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate ire_t *
54407c478bd9Sstevel@tonic-gate ire_srcif_table_lookup(ipaddr_t dst_addr, int ire_type, ipif_t *ipif,
54417c478bd9Sstevel@tonic-gate     ill_t *in_ill, int flags)
54427c478bd9Sstevel@tonic-gate {
54437c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
54447c478bd9Sstevel@tonic-gate 	ire_t	*ire;
54457c478bd9Sstevel@tonic-gate 	irb_t	*ire_srcif_table;
54467c478bd9Sstevel@tonic-gate 
54477c478bd9Sstevel@tonic-gate 	ASSERT(in_ill != NULL && !in_ill->ill_isv6);
54487c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) ||
54497c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
54507c478bd9Sstevel@tonic-gate 
54517c478bd9Sstevel@tonic-gate 	/*
54527c478bd9Sstevel@tonic-gate 	 * No need to lock the ill since it is refheld by the caller of this
54537c478bd9Sstevel@tonic-gate 	 * function
54547c478bd9Sstevel@tonic-gate 	 */
54557c478bd9Sstevel@tonic-gate 	if (in_ill->ill_srcif_table == NULL) {
54567c478bd9Sstevel@tonic-gate 		return (NULL);
54577c478bd9Sstevel@tonic-gate 	}
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 	if (!(flags & MATCH_IRE_TYPE)) {
54607c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_TYPE;
54617c478bd9Sstevel@tonic-gate 		ire_type = IRE_INTERFACE;
54627c478bd9Sstevel@tonic-gate 	}
54637c478bd9Sstevel@tonic-gate 	ire_srcif_table = in_ill->ill_srcif_table;
54647c478bd9Sstevel@tonic-gate 	irb_ptr = &ire_srcif_table[IRE_ADDR_HASH(dst_addr,
54657c478bd9Sstevel@tonic-gate 	    IP_SRCIF_TABLE_SIZE)];
54667c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
54677c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
54687c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
54697c478bd9Sstevel@tonic-gate 			continue;
54707c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, dst_addr, ire->ire_mask, 0,
547145916cd2Sjpk 		    ire_type, ipif, ire->ire_zoneid, 0, NULL, flags)) {
54727c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
54737c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
54747c478bd9Sstevel@tonic-gate 			return (ire);
54757c478bd9Sstevel@tonic-gate 		}
54767c478bd9Sstevel@tonic-gate 	}
54777c478bd9Sstevel@tonic-gate 	/* Not Found */
54787c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
54797c478bd9Sstevel@tonic-gate 	return (NULL);
54807c478bd9Sstevel@tonic-gate }
54817c478bd9Sstevel@tonic-gate 
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate /*
54847c478bd9Sstevel@tonic-gate  * Adds the ire into the special routing table which is hanging off of
54857c478bd9Sstevel@tonic-gate  * the src_ipif->ipif_ill. It also increments the refcnt in the ill.
54867c478bd9Sstevel@tonic-gate  * The forward table contains only IRE_IF_RESOLVER, IRE_IF_NORESOLVER
54877c478bd9Sstevel@tonic-gate  * i,e. IRE_INTERFACE entries. Originally the dlureq_mp field is NULL
54887c478bd9Sstevel@tonic-gate  * for IRE_IF_RESOLVER entry because we do not have the dst_addr's
54897c478bd9Sstevel@tonic-gate  * link-layer address at the time of addition.
54907c478bd9Sstevel@tonic-gate  * Upon resolving the address from ARP, dlureq_mp field is updated with
54917c478bd9Sstevel@tonic-gate  * proper information in ire_update_srcif_v4.
54927c478bd9Sstevel@tonic-gate  */
54937c478bd9Sstevel@tonic-gate static int
54947c478bd9Sstevel@tonic-gate ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
54957c478bd9Sstevel@tonic-gate {
54967c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
54977c478bd9Sstevel@tonic-gate 	irb_t	*ire_srcifp_table = NULL;
54987c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr = NULL;
54997c478bd9Sstevel@tonic-gate 	ire_t   **irep;
55007c478bd9Sstevel@tonic-gate 	ire_t   *ire;
55017c478bd9Sstevel@tonic-gate 	int	flags;
55027c478bd9Sstevel@tonic-gate 	int	i;
55037c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
55047c478bd9Sstevel@tonic-gate 	uint_t	max_frag;
55057c478bd9Sstevel@tonic-gate 	int error = 0;
5506f4b3ec61Sdh155122 	ip_stack_t	*ipst;
55077c478bd9Sstevel@tonic-gate 
55087c478bd9Sstevel@tonic-gate 	ire = *ire_p;
5509f4b3ec61Sdh155122 	ipst = ire->ire_ipst;
55107c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill != NULL);
55117c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
55127c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type == IRE_IF_NORESOLVER ||
55137c478bd9Sstevel@tonic-gate 	    ire->ire_type == IRE_IF_RESOLVER);
55147c478bd9Sstevel@tonic-gate 
55157c478bd9Sstevel@tonic-gate 	ire->ire_mask = IP_HOST_MASK;
5516c793af95Ssangeeta 	/*
5517c793af95Ssangeeta 	 * Update ire_nce->nce_res_mp with NULL value upon creation;
5518c793af95Ssangeeta 	 * first free the default res_mp created by ire_nce_init.
5519c793af95Ssangeeta 	 */
5520c793af95Ssangeeta 	freeb(ire->ire_nce->nce_res_mp);
55217c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_IF_RESOLVER) {
55227c478bd9Sstevel@tonic-gate 		/*
55237c478bd9Sstevel@tonic-gate 		 * assign NULL now, it will be updated
55247c478bd9Sstevel@tonic-gate 		 * with correct value upon returning from
55257c478bd9Sstevel@tonic-gate 		 * ARP
55267c478bd9Sstevel@tonic-gate 		 */
5527c793af95Ssangeeta 		ire->ire_nce->nce_res_mp = NULL;
55287c478bd9Sstevel@tonic-gate 	} else {
5529c793af95Ssangeeta 		ire->ire_nce->nce_res_mp = ill_dlur_gen(NULL,
55307c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_phys_addr_length,
55317c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_sap,
55327c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_sap_length);
55337c478bd9Sstevel@tonic-gate 	}
55347c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
55357c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_max_fragp != NULL);
55387c478bd9Sstevel@tonic-gate 	max_frag = *ire->ire_max_fragp;
55397c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = NULL;
55407c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
55417c478bd9Sstevel@tonic-gate 
55427c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_in_ill->ill_lock);
55437c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill->ill_srcif_table == NULL) {
55447c478bd9Sstevel@tonic-gate 		/* create the incoming interface based table */
5545f4b3ec61Sdh155122 		ire->ire_in_ill->ill_srcif_table = kmem_zalloc(
5546f4b3ec61Sdh155122 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t), KM_NOSLEEP);
55477c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill->ill_srcif_table == NULL) {
55487c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_srcif_v4: Allocation fail\n"));
55497c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_in_ill->ill_lock);
55507c478bd9Sstevel@tonic-gate 			ire_delete(ire);
55517c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
55527c478bd9Sstevel@tonic-gate 			return (ENOMEM);
55537c478bd9Sstevel@tonic-gate 		}
55547c478bd9Sstevel@tonic-gate 		ire_srcifp_table = ire->ire_in_ill->ill_srcif_table;
55557c478bd9Sstevel@tonic-gate 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
55567c478bd9Sstevel@tonic-gate 			rw_init(&ire_srcifp_table[i].irb_lock, NULL,
55577c478bd9Sstevel@tonic-gate 			    RW_DEFAULT, NULL);
55587c478bd9Sstevel@tonic-gate 		}
55597c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_add_srcif_v4: table created for ill %p\n",
55607c478bd9Sstevel@tonic-gate 		    (void *)ire->ire_in_ill));
55617c478bd9Sstevel@tonic-gate 	}
55627c478bd9Sstevel@tonic-gate 	/* Check for duplicate and insert */
55637c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill->ill_srcif_table != NULL);
55647c478bd9Sstevel@tonic-gate 	irb_ptr =
55657c478bd9Sstevel@tonic-gate 	    &(ire->ire_in_ill->ill_srcif_table[IRE_ADDR_HASH(ire->ire_addr,
55667c478bd9Sstevel@tonic-gate 	    IP_SRCIF_TABLE_SIZE)]);
55677c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_in_ill->ill_lock);
55687c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
55697c478bd9Sstevel@tonic-gate 	flags |= MATCH_IRE_IPIF;
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 	/*
55727c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
55737c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
55747c478bd9Sstevel@tonic-gate 	 *
55757c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
55767c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
55777c478bd9Sstevel@tonic-gate 	 */
55787c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
55797c478bd9Sstevel@tonic-gate 	if (error != 0) {
55807c478bd9Sstevel@tonic-gate 		/*
55817c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
55827c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
55837c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
55847c478bd9Sstevel@tonic-gate 		 */
55857c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
55867c478bd9Sstevel@tonic-gate 		ire_delete(ire);
55877c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_add_srcif_v4: ire_atomic_start failed\n"));
55887c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
55897c478bd9Sstevel@tonic-gate 		return (error);
55907c478bd9Sstevel@tonic-gate 	}
55917c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
55927c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
55937c478bd9Sstevel@tonic-gate 			continue;
55947c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
55957c478bd9Sstevel@tonic-gate 			continue;
55967c478bd9Sstevel@tonic-gate 		/* Has anyone inserted route in the meanwhile ? */
55977c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask, 0,
559845916cd2Sjpk 		    ire->ire_type, ire->ire_ipif, ire->ire_zoneid, 0, NULL,
559945916cd2Sjpk 		    flags)) {
56007c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_srcif_v4 : Duplicate entry exists\n"));
56017c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
56027c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
56037c478bd9Sstevel@tonic-gate 			ire_delete(ire);
56047c478bd9Sstevel@tonic-gate 			/* Return old ire as in ire_add_v4 */
56057c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
56067c478bd9Sstevel@tonic-gate 			return (0);
56077c478bd9Sstevel@tonic-gate 		}
56087c478bd9Sstevel@tonic-gate 	}
56097c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
56107c478bd9Sstevel@tonic-gate 	if (*irep != NULL) {
56117c478bd9Sstevel@tonic-gate 		/* Find the last ire which matches ire_addr */
56127c478bd9Sstevel@tonic-gate 		ire1 = *irep;
56137c478bd9Sstevel@tonic-gate 		while (ire1->ire_addr == ire->ire_addr) {
56147c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
56157c478bd9Sstevel@tonic-gate 			ire1 = *irep;
56167c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
56177c478bd9Sstevel@tonic-gate 				break;
56187c478bd9Sstevel@tonic-gate 		}
56197c478bd9Sstevel@tonic-gate 	}
56207c478bd9Sstevel@tonic-gate 	ire1 = *irep;
56217c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
56227c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
56237c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
56247c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
56257c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
56267c478bd9Sstevel@tonic-gate 	membar_producer();
56277c478bd9Sstevel@tonic-gate 	*irep = ire;
56287c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
56297c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
56307c478bd9Sstevel@tonic-gate 
56317c478bd9Sstevel@tonic-gate 	/*
56327c478bd9Sstevel@tonic-gate 	 * Protect ire_in_ill->ill_srcif_refcnt and table reference count.
56337c478bd9Sstevel@tonic-gate 	 * Note, ire_atomic_start already grabs the ire_in_ill->ill_lock
56347c478bd9Sstevel@tonic-gate 	 * so ill_srcif_refcnt is already protected.
56357c478bd9Sstevel@tonic-gate 	 */
56367c478bd9Sstevel@tonic-gate 	ire->ire_in_ill->ill_srcif_refcnt++;
5637f4b3ec61Sdh155122 	mutex_enter(&ipst->ips_ire_srcif_table_lock);
5638f4b3ec61Sdh155122 	ipst->ips_ire_srcif_table_count++;
5639f4b3ec61Sdh155122 	mutex_exit(&ipst->ips_ire_srcif_table_lock);
56407c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
56417c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
56427c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
56437c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
56447c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
56457c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
56467c478bd9Sstevel@tonic-gate 		}
56477c478bd9Sstevel@tonic-gate 	} else {
56487c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
56497c478bd9Sstevel@tonic-gate 	}
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
56527c478bd9Sstevel@tonic-gate 	*ire_p = ire;
56537c478bd9Sstevel@tonic-gate 	return (0);
56547c478bd9Sstevel@tonic-gate }
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate /*
56587c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send when ARP request comes
56597c478bd9Sstevel@tonic-gate  * back to ip_wput->ire_add_then_send for resolved ire in the interface
56607c478bd9Sstevel@tonic-gate  * based routing table. At this point, it only needs to update the resolver
56617c478bd9Sstevel@tonic-gate  * information for the ire. The passed ire is returned to the caller as it
56627c478bd9Sstevel@tonic-gate  * is the ire which is created as mblk.
56637c478bd9Sstevel@tonic-gate  */
56647c478bd9Sstevel@tonic-gate static ire_t *
56657c478bd9Sstevel@tonic-gate ire_update_srcif_v4(ire_t *ire)
56667c478bd9Sstevel@tonic-gate {
56677c478bd9Sstevel@tonic-gate 	ire_t   *ire1;
56687c478bd9Sstevel@tonic-gate 	irb_t	*irb;
56697c478bd9Sstevel@tonic-gate 	int	error;
56707c478bd9Sstevel@tonic-gate 
56717c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN &&
56727c478bd9Sstevel@tonic-gate 	    ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER);
56737c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 	/*
56767c478bd9Sstevel@tonic-gate 	 * This ire is from ARP. Update
5677c793af95Ssangeeta 	 * ire_nce->nce_res_mp info
56787c478bd9Sstevel@tonic-gate 	 */
56797c478bd9Sstevel@tonic-gate 	ire1 = ire_srcif_table_lookup(ire->ire_addr,
56807c478bd9Sstevel@tonic-gate 	    IRE_IF_RESOLVER, ire->ire_ipif,
56817c478bd9Sstevel@tonic-gate 	    ire->ire_in_ill,
56827c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL | MATCH_IRE_TYPE);
56837c478bd9Sstevel@tonic-gate 	if (ire1 == NULL) {
56847c478bd9Sstevel@tonic-gate 		/* Mobile node registration expired ? */
56857c478bd9Sstevel@tonic-gate 		ire_delete(ire);
56867c478bd9Sstevel@tonic-gate 		return (NULL);
56877c478bd9Sstevel@tonic-gate 	}
56887c478bd9Sstevel@tonic-gate 	irb = ire1->ire_bucket;
56897c478bd9Sstevel@tonic-gate 	ASSERT(irb != NULL);
56907c478bd9Sstevel@tonic-gate 	/*
56917c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
56927c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
56937c478bd9Sstevel@tonic-gate 	 */
56947c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb, ire1, NULL, NULL, NULL);
56957c478bd9Sstevel@tonic-gate 	if (error != 0) {
56967c478bd9Sstevel@tonic-gate 		/*
56977c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
56987c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
56997c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
57007c478bd9Sstevel@tonic-gate 		 */
57017c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
57027c478bd9Sstevel@tonic-gate 		ire_delete(ire);
57037c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_update_srcif_v4: ire_atomic_start failed\n"));
57047c478bd9Sstevel@tonic-gate 		return (NULL);
57057c478bd9Sstevel@tonic-gate 	}
57067c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_max_fragp == NULL);
57077c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = ire1->ire_max_frag;
57087c478bd9Sstevel@tonic-gate 	/*
57097c478bd9Sstevel@tonic-gate 	 * Update resolver information and
57107c478bd9Sstevel@tonic-gate 	 * send-to queue.
57117c478bd9Sstevel@tonic-gate 	 */
5712c793af95Ssangeeta 	ASSERT(ire->ire_nce->nce_res_mp != NULL);
5713c793af95Ssangeeta 	ire1->ire_nce->nce_res_mp = copyb(ire->ire_nce->nce_res_mp);
5714c793af95Ssangeeta 	if (ire1->ire_nce->nce_res_mp ==  NULL) {
57157c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_update_srcif: copyb failed\n"));
57167c478bd9Sstevel@tonic-gate 		ire_refrele(ire1);
57177c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
57187c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb, ire1);
57197c478bd9Sstevel@tonic-gate 		return (NULL);
57207c478bd9Sstevel@tonic-gate 	}
57217c478bd9Sstevel@tonic-gate 	ire1->ire_stq = ire->ire_stq;
57227c478bd9Sstevel@tonic-gate 
5723c793af95Ssangeeta 	ASSERT(ire->ire_nce->nce_fp_mp == NULL);
57247c478bd9Sstevel@tonic-gate 
57257c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb, ire1);
57267c478bd9Sstevel@tonic-gate 	ire_refrele(ire1);
57277c478bd9Sstevel@tonic-gate 	/* Return the passed ire */
57287c478bd9Sstevel@tonic-gate 	return (ire);   /* Update done */
57297c478bd9Sstevel@tonic-gate }
57307c478bd9Sstevel@tonic-gate 
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate /*
57337c478bd9Sstevel@tonic-gate  * Check if another multirt route resolution is needed.
57347c478bd9Sstevel@tonic-gate  * B_TRUE is returned is there remain a resolvable route,
57357c478bd9Sstevel@tonic-gate  * or if no route for that dst is resolved yet.
57367c478bd9Sstevel@tonic-gate  * B_FALSE is returned if all routes for that dst are resolved
57377c478bd9Sstevel@tonic-gate  * or if the remaining unresolved routes are actually not
57387c478bd9Sstevel@tonic-gate  * resolvable.
57397c478bd9Sstevel@tonic-gate  * This only works in the global zone.
57407c478bd9Sstevel@tonic-gate  */
57417c478bd9Sstevel@tonic-gate boolean_t
5742f4b3ec61Sdh155122 ire_multirt_need_resolve(ipaddr_t dst, const ts_label_t *tsl, ip_stack_t *ipst)
57437c478bd9Sstevel@tonic-gate {
57447c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
57457c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
57467c478bd9Sstevel@tonic-gate 	ire_t	*fire;
57477c478bd9Sstevel@tonic-gate 	ire_t	*cire;
57487c478bd9Sstevel@tonic-gate 	irb_t	*firb;
57497c478bd9Sstevel@tonic-gate 	irb_t	*cirb;
57507c478bd9Sstevel@tonic-gate 	int	unres_cnt = 0;
57517c478bd9Sstevel@tonic-gate 	boolean_t resolvable = B_FALSE;
57527c478bd9Sstevel@tonic-gate 
57537c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_HOST that matches the destination */
57547c478bd9Sstevel@tonic-gate 	first_fire = ire_ftable_lookup(dst, IP_HOST_MASK, 0, IRE_HOST, NULL,
575545916cd2Sjpk 	    NULL, ALL_ZONES, 0, tsl,
5756f4b3ec61Sdh155122 	    MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst);
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 	/* No route at all */
57597c478bd9Sstevel@tonic-gate 	if (first_fire == NULL) {
57607c478bd9Sstevel@tonic-gate 		return (B_TRUE);
57617c478bd9Sstevel@tonic-gate 	}
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
57647c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_CACHE ire for that destination. */
5767f4b3ec61Sdh155122 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst);
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 	/* No resolved route. */
57707c478bd9Sstevel@tonic-gate 	if (first_cire == NULL) {
57717c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
57727c478bd9Sstevel@tonic-gate 		return (B_TRUE);
57737c478bd9Sstevel@tonic-gate 	}
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate 	/*
57767c478bd9Sstevel@tonic-gate 	 * At least one route is resolved. Here we look through the forward
57777c478bd9Sstevel@tonic-gate 	 * and cache tables, to compare the number of declared routes
57787c478bd9Sstevel@tonic-gate 	 * with the number of resolved routes. The search for a resolvable
57797c478bd9Sstevel@tonic-gate 	 * route is performed only if at least one route remains
57807c478bd9Sstevel@tonic-gate 	 * unresolved.
57817c478bd9Sstevel@tonic-gate 	 */
57827c478bd9Sstevel@tonic-gate 	cirb = first_cire->ire_bucket;
57837c478bd9Sstevel@tonic-gate 	ASSERT(cirb != NULL);
57847c478bd9Sstevel@tonic-gate 
57857c478bd9Sstevel@tonic-gate 	/* Count the number of routes to that dest that are declared. */
57867c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
57877c478bd9Sstevel@tonic-gate 	for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
57887c478bd9Sstevel@tonic-gate 		if (!(fire->ire_flags & RTF_MULTIRT))
57897c478bd9Sstevel@tonic-gate 			continue;
57907c478bd9Sstevel@tonic-gate 		if (fire->ire_addr != dst)
57917c478bd9Sstevel@tonic-gate 			continue;
57927c478bd9Sstevel@tonic-gate 		unres_cnt++;
57937c478bd9Sstevel@tonic-gate 	}
57947c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
57957c478bd9Sstevel@tonic-gate 
57967c478bd9Sstevel@tonic-gate 	/* Then subtract the number of routes to that dst that are resolved */
57977c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(cirb);
57987c478bd9Sstevel@tonic-gate 	for (cire = first_cire; cire != NULL; cire = cire->ire_next) {
57997c478bd9Sstevel@tonic-gate 		if (!(cire->ire_flags & RTF_MULTIRT))
58007c478bd9Sstevel@tonic-gate 			continue;
58017c478bd9Sstevel@tonic-gate 		if (cire->ire_addr != dst)
58027c478bd9Sstevel@tonic-gate 			continue;
58037c478bd9Sstevel@tonic-gate 		if (cire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))
58047c478bd9Sstevel@tonic-gate 			continue;
58057c478bd9Sstevel@tonic-gate 		unres_cnt--;
58067c478bd9Sstevel@tonic-gate 	}
58077c478bd9Sstevel@tonic-gate 	IRB_REFRELE(cirb);
58087c478bd9Sstevel@tonic-gate 
58097c478bd9Sstevel@tonic-gate 	/* At least one route is unresolved; search for a resolvable route. */
58107c478bd9Sstevel@tonic-gate 	if (unres_cnt > 0)
58117c478bd9Sstevel@tonic-gate 		resolvable = ire_multirt_lookup(&first_cire, &first_fire,
5812f4b3ec61Sdh155122 		    MULTIRT_USESTAMP | MULTIRT_CACHEGW, tsl, ipst);
58137c478bd9Sstevel@tonic-gate 
58147c478bd9Sstevel@tonic-gate 	if (first_fire != NULL)
58157c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
58187c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 	return (resolvable);
58217c478bd9Sstevel@tonic-gate }
58227c478bd9Sstevel@tonic-gate 
58237c478bd9Sstevel@tonic-gate 
58247c478bd9Sstevel@tonic-gate /*
58257c478bd9Sstevel@tonic-gate  * Explore a forward_table bucket, starting from fire_arg.
58267c478bd9Sstevel@tonic-gate  * fire_arg MUST be an IRE_HOST entry.
58277c478bd9Sstevel@tonic-gate  *
58287c478bd9Sstevel@tonic-gate  * Return B_TRUE and update *ire_arg and *fire_arg
58297c478bd9Sstevel@tonic-gate  * if at least one resolvable route is found. *ire_arg
58307c478bd9Sstevel@tonic-gate  * is the IRE entry for *fire_arg's gateway.
58317c478bd9Sstevel@tonic-gate  *
58327c478bd9Sstevel@tonic-gate  * Return B_FALSE otherwise (all routes are resolved or
58337c478bd9Sstevel@tonic-gate  * the remaining unresolved routes are all unresolvable).
58347c478bd9Sstevel@tonic-gate  *
58357c478bd9Sstevel@tonic-gate  * The IRE selection relies on a priority mechanism
58367c478bd9Sstevel@tonic-gate  * driven by the flags passed in by the caller.
58377c478bd9Sstevel@tonic-gate  * The caller, such as ip_newroute_ipif(), can get the most
58387c478bd9Sstevel@tonic-gate  * relevant ire at each stage of a multiple route resolution.
58397c478bd9Sstevel@tonic-gate  *
58407c478bd9Sstevel@tonic-gate  * The rules are:
58417c478bd9Sstevel@tonic-gate  *
58427c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is specified in flags, IRE_CACHETABLE
58437c478bd9Sstevel@tonic-gate  *   ires are preferred for the gateway. This gives the highest
58447c478bd9Sstevel@tonic-gate  *   priority to routes that can be resolved without using
58457c478bd9Sstevel@tonic-gate  *   a resolver.
58467c478bd9Sstevel@tonic-gate  *
58477c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is not specified, or if MULTIRT_CACHEGW
58487c478bd9Sstevel@tonic-gate  *   is specified but no IRE_CACHETABLE ire entry for the gateway
58497c478bd9Sstevel@tonic-gate  *   is found, the following rules apply.
58507c478bd9Sstevel@tonic-gate  *
58517c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is specified in flags, IRE_INTERFACE
58527c478bd9Sstevel@tonic-gate  *   ires for the gateway, that have not been tried since
58537c478bd9Sstevel@tonic-gate  *   a configurable amount of time, are preferred.
58547c478bd9Sstevel@tonic-gate  *   This applies when a resolver must be invoked for
58557c478bd9Sstevel@tonic-gate  *   a missing route, but we don't want to use the resolver
58567c478bd9Sstevel@tonic-gate  *   upon each packet emission. If no such resolver is found,
58577c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
58587c478bd9Sstevel@tonic-gate  *   The MULTIRT_USESTAMP flag can be combined with
58597c478bd9Sstevel@tonic-gate  *   MULTIRT_CACHEGW.
58607c478bd9Sstevel@tonic-gate  *
58617c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is not specified in flags, the first
58627c478bd9Sstevel@tonic-gate  *   unresolved but resolvable route is selected.
58637c478bd9Sstevel@tonic-gate  *
58647c478bd9Sstevel@tonic-gate  * - Otherwise, there is no resolvalble route, and
58657c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
58667c478bd9Sstevel@tonic-gate  *
58677c478bd9Sstevel@tonic-gate  * At last, MULTIRT_SETSTAMP can be specified in flags to
58687c478bd9Sstevel@tonic-gate  * request the timestamp of unresolvable routes to
58697c478bd9Sstevel@tonic-gate  * be refreshed. This prevents the useless exploration
58707c478bd9Sstevel@tonic-gate  * of those routes for a while, when MULTIRT_USESTAMP is used.
58717c478bd9Sstevel@tonic-gate  *
58727c478bd9Sstevel@tonic-gate  * This only works in the global zone.
58737c478bd9Sstevel@tonic-gate  */
58747c478bd9Sstevel@tonic-gate boolean_t
587545916cd2Sjpk ire_multirt_lookup(ire_t **ire_arg, ire_t **fire_arg, uint32_t flags,
5876f4b3ec61Sdh155122     const ts_label_t *tsl, ip_stack_t *ipst)
58777c478bd9Sstevel@tonic-gate {
58787c478bd9Sstevel@tonic-gate 	clock_t	delta;
58797c478bd9Sstevel@tonic-gate 	ire_t	*best_fire = NULL;
58807c478bd9Sstevel@tonic-gate 	ire_t	*best_cire = NULL;
58817c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
58827c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
58837c478bd9Sstevel@tonic-gate 	ire_t	*fire;
58847c478bd9Sstevel@tonic-gate 	ire_t	*cire;
58857c478bd9Sstevel@tonic-gate 	irb_t	*firb = NULL;
58867c478bd9Sstevel@tonic-gate 	irb_t	*cirb = NULL;
58877c478bd9Sstevel@tonic-gate 	ire_t	*gw_ire;
58887c478bd9Sstevel@tonic-gate 	boolean_t	already_resolved;
58897c478bd9Sstevel@tonic-gate 	boolean_t	res;
58907c478bd9Sstevel@tonic-gate 	ipaddr_t	dst;
58917c478bd9Sstevel@tonic-gate 	ipaddr_t	gw;
58927c478bd9Sstevel@tonic-gate 
58937c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: *ire_arg %p, *fire_arg %p, flags %04x\n",
58947c478bd9Sstevel@tonic-gate 	    (void *)*ire_arg, (void *)*fire_arg, flags));
58957c478bd9Sstevel@tonic-gate 
58967c478bd9Sstevel@tonic-gate 	ASSERT(ire_arg != NULL);
58977c478bd9Sstevel@tonic-gate 	ASSERT(fire_arg != NULL);
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate 	/* Not an IRE_HOST ire; give up. */
59007c478bd9Sstevel@tonic-gate 	if ((*fire_arg == NULL) || ((*fire_arg)->ire_type != IRE_HOST)) {
59017c478bd9Sstevel@tonic-gate 		return (B_FALSE);
59027c478bd9Sstevel@tonic-gate 	}
59037c478bd9Sstevel@tonic-gate 
59047c478bd9Sstevel@tonic-gate 	/* This is the first IRE_HOST ire for that destination. */
59057c478bd9Sstevel@tonic-gate 	first_fire = *fire_arg;
59067c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
59077c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
59087c478bd9Sstevel@tonic-gate 
59097c478bd9Sstevel@tonic-gate 	dst = first_fire->ire_addr;
59107c478bd9Sstevel@tonic-gate 
59117c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: dst %08x\n", ntohl(dst)));
59127c478bd9Sstevel@tonic-gate 
59137c478bd9Sstevel@tonic-gate 	/*
59147c478bd9Sstevel@tonic-gate 	 * Retrieve the first IRE_CACHE ire for that destination;
59157c478bd9Sstevel@tonic-gate 	 * if we don't find one, no route for that dest is
59167c478bd9Sstevel@tonic-gate 	 * resolved yet.
59177c478bd9Sstevel@tonic-gate 	 */
5918f4b3ec61Sdh155122 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl, ipst);
59197c478bd9Sstevel@tonic-gate 	if (first_cire != NULL) {
59207c478bd9Sstevel@tonic-gate 		cirb = first_cire->ire_bucket;
59217c478bd9Sstevel@tonic-gate 	}
59227c478bd9Sstevel@tonic-gate 
59237c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: first_cire %p\n", (void *)first_cire));
59247c478bd9Sstevel@tonic-gate 
59257c478bd9Sstevel@tonic-gate 	/*
59267c478bd9Sstevel@tonic-gate 	 * Search for a resolvable route, giving the top priority
59277c478bd9Sstevel@tonic-gate 	 * to routes that can be resolved without any call to the resolver.
59287c478bd9Sstevel@tonic-gate 	 */
59297c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
59307c478bd9Sstevel@tonic-gate 
59317c478bd9Sstevel@tonic-gate 	if (!CLASSD(dst)) {
59327c478bd9Sstevel@tonic-gate 		/*
59337c478bd9Sstevel@tonic-gate 		 * For all multiroute IRE_HOST ires for that destination,
59347c478bd9Sstevel@tonic-gate 		 * check if the route via the IRE_HOST's gateway is
59357c478bd9Sstevel@tonic-gate 		 * resolved yet.
59367c478bd9Sstevel@tonic-gate 		 */
59377c478bd9Sstevel@tonic-gate 		for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
59387c478bd9Sstevel@tonic-gate 
59397c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
59407c478bd9Sstevel@tonic-gate 				continue;
59417c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
59427c478bd9Sstevel@tonic-gate 				continue;
59437c478bd9Sstevel@tonic-gate 
594445916cd2Sjpk 			if (fire->ire_gw_secattr != NULL &&
594545916cd2Sjpk 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
594645916cd2Sjpk 				continue;
594745916cd2Sjpk 			}
594845916cd2Sjpk 
59497c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
59507c478bd9Sstevel@tonic-gate 
59517c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, "
59527c478bd9Sstevel@tonic-gate 			    "ire_addr %08x, ire_gateway_addr %08x\n",
59537c478bd9Sstevel@tonic-gate 			    (void *)fire, ntohl(fire->ire_addr), ntohl(gw)));
59547c478bd9Sstevel@tonic-gate 
59557c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
59567c478bd9Sstevel@tonic-gate 
59577c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
59587c478bd9Sstevel@tonic-gate 				ASSERT(cirb != NULL);
59597c478bd9Sstevel@tonic-gate 
59607c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
59617c478bd9Sstevel@tonic-gate 				/*
59627c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
59637c478bd9Sstevel@tonic-gate 				 * destination.
59647c478bd9Sstevel@tonic-gate 				 */
59657c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
59667c478bd9Sstevel@tonic-gate 				    cire != NULL;
59677c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
59687c478bd9Sstevel@tonic-gate 
59697c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
59707c478bd9Sstevel@tonic-gate 						continue;
59717c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
59727c478bd9Sstevel@tonic-gate 						continue;
59737c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
59747c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
59757c478bd9Sstevel@tonic-gate 						IRE_MARK_HIDDEN))
59767c478bd9Sstevel@tonic-gate 						continue;
597745916cd2Sjpk 
597845916cd2Sjpk 					if (cire->ire_gw_secattr != NULL &&
597945916cd2Sjpk 					    tsol_ire_match_gwattr(cire,
598045916cd2Sjpk 					    tsl) != 0) {
598145916cd2Sjpk 						continue;
598245916cd2Sjpk 					}
598345916cd2Sjpk 
59847c478bd9Sstevel@tonic-gate 					/*
59857c478bd9Sstevel@tonic-gate 					 * Check if the IRE_CACHE's gateway
59867c478bd9Sstevel@tonic-gate 					 * matches the IRE_HOST's gateway.
59877c478bd9Sstevel@tonic-gate 					 */
59887c478bd9Sstevel@tonic-gate 					if (cire->ire_gateway_addr == gw) {
59897c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
59907c478bd9Sstevel@tonic-gate 						break;
59917c478bd9Sstevel@tonic-gate 					}
59927c478bd9Sstevel@tonic-gate 				}
59937c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
59947c478bd9Sstevel@tonic-gate 			}
59957c478bd9Sstevel@tonic-gate 
59967c478bd9Sstevel@tonic-gate 			/*
59977c478bd9Sstevel@tonic-gate 			 * This route is already resolved;
59987c478bd9Sstevel@tonic-gate 			 * proceed with next one.
59997c478bd9Sstevel@tonic-gate 			 */
60007c478bd9Sstevel@tonic-gate 			if (already_resolved) {
60017c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found cire %p, "
60027c478bd9Sstevel@tonic-gate 				    "already resolved\n", (void *)cire));
60037c478bd9Sstevel@tonic-gate 				continue;
60047c478bd9Sstevel@tonic-gate 			}
60057c478bd9Sstevel@tonic-gate 
60067c478bd9Sstevel@tonic-gate 			/*
60077c478bd9Sstevel@tonic-gate 			 * The route is unresolved; is it actually
60087c478bd9Sstevel@tonic-gate 			 * resolvable, i.e. is there a cache or a resolver
60097c478bd9Sstevel@tonic-gate 			 * for the gateway?
60107c478bd9Sstevel@tonic-gate 			 */
60117c478bd9Sstevel@tonic-gate 			gw_ire = ire_route_lookup(gw, 0, 0, 0, NULL, NULL,
601245916cd2Sjpk 			    ALL_ZONES, tsl,
6013f4b3ec61Sdh155122 			    MATCH_IRE_RECURSIVE | MATCH_IRE_SECATTR, ipst);
60147c478bd9Sstevel@tonic-gate 
60157c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: looked up gw_ire %p\n",
60167c478bd9Sstevel@tonic-gate 			    (void *)gw_ire));
60177c478bd9Sstevel@tonic-gate 
60187c478bd9Sstevel@tonic-gate 			/*
60197c478bd9Sstevel@tonic-gate 			 * If gw_ire is typed IRE_CACHETABLE,
60207c478bd9Sstevel@tonic-gate 			 * this route can be resolved without any call to the
60217c478bd9Sstevel@tonic-gate 			 * resolver. If the MULTIRT_CACHEGW flag is set,
60227c478bd9Sstevel@tonic-gate 			 * give the top priority to this ire and exit the
60237c478bd9Sstevel@tonic-gate 			 * loop.
60247c478bd9Sstevel@tonic-gate 			 * This is typically the case when an ARP reply
60257c478bd9Sstevel@tonic-gate 			 * is processed through ip_wput_nondata().
60267c478bd9Sstevel@tonic-gate 			 */
60277c478bd9Sstevel@tonic-gate 			if ((flags & MULTIRT_CACHEGW) &&
60287c478bd9Sstevel@tonic-gate 			    (gw_ire != NULL) &&
60297c478bd9Sstevel@tonic-gate 			    (gw_ire->ire_type & IRE_CACHETABLE)) {
6030c793af95Ssangeeta 				ASSERT(gw_ire->ire_nce == NULL ||
6031c793af95Ssangeeta 				    gw_ire->ire_nce->nce_state == ND_REACHABLE);
60327c478bd9Sstevel@tonic-gate 				/*
60337c478bd9Sstevel@tonic-gate 				 * Release the resolver associated to the
60347c478bd9Sstevel@tonic-gate 				 * previous candidate best ire, if any.
60357c478bd9Sstevel@tonic-gate 				 */
60367c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
60377c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
60387c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
60397c478bd9Sstevel@tonic-gate 				}
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 				best_fire = fire;
60427c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
60437c478bd9Sstevel@tonic-gate 
60447c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found top prio "
60457c478bd9Sstevel@tonic-gate 				    "best_fire %p, best_cire %p\n",
60467c478bd9Sstevel@tonic-gate 				    (void *)best_fire, (void *)best_cire));
60477c478bd9Sstevel@tonic-gate 				break;
60487c478bd9Sstevel@tonic-gate 			}
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate 			/*
60517c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
60527c478bd9Sstevel@tonic-gate 			 * attempt to  resolve that route.
60537c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take that
60547c478bd9Sstevel@tonic-gate 			 * route into account only if this time interval
60557c478bd9Sstevel@tonic-gate 			 * exceeds ip_multirt_resolution_interval;
60567c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
60577c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
60587c478bd9Sstevel@tonic-gate 			 */
60597c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
60607c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
60617c478bd9Sstevel@tonic-gate 
6062f4b3ec61Sdh155122 			res = (boolean_t)((delta >
6063f4b3ec61Sdh155122 				ipst->ips_ip_multirt_resolution_interval) ||
60647c478bd9Sstevel@tonic-gate 				(!(flags & MULTIRT_USESTAMP)));
60657c478bd9Sstevel@tonic-gate 
60667c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, delta %lu, "
60677c478bd9Sstevel@tonic-gate 			    "res %d\n",
60687c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, res));
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate 			if (res) {
60717c478bd9Sstevel@tonic-gate 				/*
60727c478bd9Sstevel@tonic-gate 				 * We are here if MULTIRT_USESTAMP flag is set
60737c478bd9Sstevel@tonic-gate 				 * and the resolver for fire's gateway
60747c478bd9Sstevel@tonic-gate 				 * has not been tried since
60757c478bd9Sstevel@tonic-gate 				 * ip_multirt_resolution_interval, or if
60767c478bd9Sstevel@tonic-gate 				 * MULTIRT_USESTAMP is not set but gw_ire did
60777c478bd9Sstevel@tonic-gate 				 * not fill the conditions for MULTIRT_CACHEGW,
60787c478bd9Sstevel@tonic-gate 				 * or if neither MULTIRT_USESTAMP nor
60797c478bd9Sstevel@tonic-gate 				 * MULTIRT_CACHEGW are set.
60807c478bd9Sstevel@tonic-gate 				 */
60817c478bd9Sstevel@tonic-gate 				if (gw_ire != NULL) {
60827c478bd9Sstevel@tonic-gate 					if (best_fire == NULL) {
60837c478bd9Sstevel@tonic-gate 						ASSERT(best_cire == NULL);
60847c478bd9Sstevel@tonic-gate 
60857c478bd9Sstevel@tonic-gate 						best_fire = fire;
60867c478bd9Sstevel@tonic-gate 						best_cire = gw_ire;
60877c478bd9Sstevel@tonic-gate 
60887c478bd9Sstevel@tonic-gate 						ip2dbg(("ire_multirt_lookup:"
60897c478bd9Sstevel@tonic-gate 						    "found candidate "
60907c478bd9Sstevel@tonic-gate 						    "best_fire %p, "
60917c478bd9Sstevel@tonic-gate 						    "best_cire %p\n",
60927c478bd9Sstevel@tonic-gate 						    (void *)best_fire,
60937c478bd9Sstevel@tonic-gate 						    (void *)best_cire));
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate 						/*
60967c478bd9Sstevel@tonic-gate 						 * If MULTIRT_CACHEGW is not
60977c478bd9Sstevel@tonic-gate 						 * set, we ignore the top
60987c478bd9Sstevel@tonic-gate 						 * priority ires that can
60997c478bd9Sstevel@tonic-gate 						 * be resolved without any
61007c478bd9Sstevel@tonic-gate 						 * call to the resolver;
61017c478bd9Sstevel@tonic-gate 						 * In that case, there is
61027c478bd9Sstevel@tonic-gate 						 * actually no need
61037c478bd9Sstevel@tonic-gate 						 * to continue the loop.
61047c478bd9Sstevel@tonic-gate 						 */
61057c478bd9Sstevel@tonic-gate 						if (!(flags &
61067c478bd9Sstevel@tonic-gate 						    MULTIRT_CACHEGW)) {
61077c478bd9Sstevel@tonic-gate 							break;
61087c478bd9Sstevel@tonic-gate 						}
61097c478bd9Sstevel@tonic-gate 						continue;
61107c478bd9Sstevel@tonic-gate 					}
61117c478bd9Sstevel@tonic-gate 				} else {
61127c478bd9Sstevel@tonic-gate 					/*
61137c478bd9Sstevel@tonic-gate 					 * No resolver for the gateway: the
61147c478bd9Sstevel@tonic-gate 					 * route is not resolvable.
61157c478bd9Sstevel@tonic-gate 					 * If the MULTIRT_SETSTAMP flag is
61167c478bd9Sstevel@tonic-gate 					 * set, we stamp the IRE_HOST ire,
61177c478bd9Sstevel@tonic-gate 					 * so we will not select it again
61187c478bd9Sstevel@tonic-gate 					 * during this resolution interval.
61197c478bd9Sstevel@tonic-gate 					 */
61207c478bd9Sstevel@tonic-gate 					if (flags & MULTIRT_SETSTAMP)
61217c478bd9Sstevel@tonic-gate 						fire->ire_last_used_time =
61227c478bd9Sstevel@tonic-gate 						    lbolt;
61237c478bd9Sstevel@tonic-gate 				}
61247c478bd9Sstevel@tonic-gate 			}
61257c478bd9Sstevel@tonic-gate 
61267c478bd9Sstevel@tonic-gate 			if (gw_ire != NULL)
61277c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
61287c478bd9Sstevel@tonic-gate 		}
61297c478bd9Sstevel@tonic-gate 	} else { /* CLASSD(dst) */
61307c478bd9Sstevel@tonic-gate 
61317c478bd9Sstevel@tonic-gate 		for (fire = first_fire;
61327c478bd9Sstevel@tonic-gate 		    fire != NULL;
61337c478bd9Sstevel@tonic-gate 		    fire = fire->ire_next) {
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
61367c478bd9Sstevel@tonic-gate 				continue;
61377c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
61387c478bd9Sstevel@tonic-gate 				continue;
61397c478bd9Sstevel@tonic-gate 
614045916cd2Sjpk 			if (fire->ire_gw_secattr != NULL &&
614145916cd2Sjpk 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
614245916cd2Sjpk 				continue;
614345916cd2Sjpk 			}
614445916cd2Sjpk 
61457c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 			gw_ire = ire_ftable_lookup(gw, 0, 0, IRE_INTERFACE,
615045916cd2Sjpk 			    NULL, NULL, ALL_ZONES, 0, tsl,
615145916cd2Sjpk 			    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE |
6152f4b3ec61Sdh155122 			    MATCH_IRE_SECATTR, ipst);
61537c478bd9Sstevel@tonic-gate 
61547c478bd9Sstevel@tonic-gate 			/* No resolver for the gateway; we skip this ire. */
61557c478bd9Sstevel@tonic-gate 			if (gw_ire == NULL) {
61567c478bd9Sstevel@tonic-gate 				continue;
61577c478bd9Sstevel@tonic-gate 			}
6158c793af95Ssangeeta 			ASSERT(gw_ire->ire_nce == NULL ||
6159c793af95Ssangeeta 			    gw_ire->ire_nce->nce_state == ND_REACHABLE);
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
61627c478bd9Sstevel@tonic-gate 
61637c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
61647c478bd9Sstevel@tonic-gate 				/*
61657c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
61667c478bd9Sstevel@tonic-gate 				 * destination.
61677c478bd9Sstevel@tonic-gate 				 */
61687c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
61697c478bd9Sstevel@tonic-gate 				    cire != NULL;
61707c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
61717c478bd9Sstevel@tonic-gate 
61727c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
61737c478bd9Sstevel@tonic-gate 						continue;
61747c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
61757c478bd9Sstevel@tonic-gate 						continue;
61767c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
61777c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
61787c478bd9Sstevel@tonic-gate 						IRE_MARK_HIDDEN))
61797c478bd9Sstevel@tonic-gate 						continue;
61807c478bd9Sstevel@tonic-gate 
618145916cd2Sjpk 					if (cire->ire_gw_secattr != NULL &&
618245916cd2Sjpk 					    tsol_ire_match_gwattr(cire,
618345916cd2Sjpk 					    tsl) != 0) {
618445916cd2Sjpk 						continue;
618545916cd2Sjpk 					}
618645916cd2Sjpk 
61877c478bd9Sstevel@tonic-gate 					/*
61887c478bd9Sstevel@tonic-gate 					 * Cache entries are linked to the
61897c478bd9Sstevel@tonic-gate 					 * parent routes using the parent handle
61907c478bd9Sstevel@tonic-gate 					 * (ire_phandle). If no cache entry has
61917c478bd9Sstevel@tonic-gate 					 * the same handle as fire, fire is
61927c478bd9Sstevel@tonic-gate 					 * still unresolved.
61937c478bd9Sstevel@tonic-gate 					 */
61947c478bd9Sstevel@tonic-gate 					ASSERT(cire->ire_phandle != 0);
61957c478bd9Sstevel@tonic-gate 					if (cire->ire_phandle ==
61967c478bd9Sstevel@tonic-gate 					    fire->ire_phandle) {
61977c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
61987c478bd9Sstevel@tonic-gate 						break;
61997c478bd9Sstevel@tonic-gate 					}
62007c478bd9Sstevel@tonic-gate 				}
62017c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
62027c478bd9Sstevel@tonic-gate 			}
62037c478bd9Sstevel@tonic-gate 
62047c478bd9Sstevel@tonic-gate 			/*
62057c478bd9Sstevel@tonic-gate 			 * This route is already resolved; proceed with
62067c478bd9Sstevel@tonic-gate 			 * next one.
62077c478bd9Sstevel@tonic-gate 			 */
62087c478bd9Sstevel@tonic-gate 			if (already_resolved) {
62097c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
62107c478bd9Sstevel@tonic-gate 				continue;
62117c478bd9Sstevel@tonic-gate 			}
62127c478bd9Sstevel@tonic-gate 
62137c478bd9Sstevel@tonic-gate 			/*
62147c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
62157c478bd9Sstevel@tonic-gate 			 * attempt to resolve that route.
62167c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take
62177c478bd9Sstevel@tonic-gate 			 * that route into account only if this time
62187c478bd9Sstevel@tonic-gate 			 * interval exceeds ip_multirt_resolution_interval;
62197c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
62207c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
62217c478bd9Sstevel@tonic-gate 			 */
62227c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
62237c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
62247c478bd9Sstevel@tonic-gate 
6225f4b3ec61Sdh155122 			res = (boolean_t)((delta >
6226f4b3ec61Sdh155122 				ipst->ips_ip_multirt_resolution_interval) ||
62277c478bd9Sstevel@tonic-gate 				(!(flags & MULTIRT_USESTAMP)));
62287c478bd9Sstevel@tonic-gate 
62297c478bd9Sstevel@tonic-gate 			ip3dbg(("ire_multirt_lookup: fire %p, delta %lx, "
62307c478bd9Sstevel@tonic-gate 			    "flags %04x, res %d\n",
62317c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, flags, res));
62327c478bd9Sstevel@tonic-gate 
62337c478bd9Sstevel@tonic-gate 			if (res) {
62347c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
62357c478bd9Sstevel@tonic-gate 					/*
62367c478bd9Sstevel@tonic-gate 					 * Release the resolver associated
62377c478bd9Sstevel@tonic-gate 					 * to the preceding candidate best
62387c478bd9Sstevel@tonic-gate 					 * ire, if any.
62397c478bd9Sstevel@tonic-gate 					 */
62407c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
62417c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
62427c478bd9Sstevel@tonic-gate 				}
62437c478bd9Sstevel@tonic-gate 				best_fire = fire;
62447c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
62457c478bd9Sstevel@tonic-gate 				continue;
62467c478bd9Sstevel@tonic-gate 			}
62477c478bd9Sstevel@tonic-gate 
62487c478bd9Sstevel@tonic-gate 			ire_refrele(gw_ire);
62497c478bd9Sstevel@tonic-gate 		}
62507c478bd9Sstevel@tonic-gate 	}
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
62537c478bd9Sstevel@tonic-gate 		IRE_REFHOLD(best_fire);
62547c478bd9Sstevel@tonic-gate 	}
62557c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
62567c478bd9Sstevel@tonic-gate 
62577c478bd9Sstevel@tonic-gate 	/* Release the first IRE_CACHE we initially looked up, if any. */
62587c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
62597c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
62607c478bd9Sstevel@tonic-gate 
62617c478bd9Sstevel@tonic-gate 	/* Found a resolvable route. */
62627c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
62637c478bd9Sstevel@tonic-gate 		ASSERT(best_cire != NULL);
62647c478bd9Sstevel@tonic-gate 
62657c478bd9Sstevel@tonic-gate 		if (*fire_arg != NULL)
62667c478bd9Sstevel@tonic-gate 			ire_refrele(*fire_arg);
62677c478bd9Sstevel@tonic-gate 		if (*ire_arg != NULL)
62687c478bd9Sstevel@tonic-gate 			ire_refrele(*ire_arg);
62697c478bd9Sstevel@tonic-gate 
62707c478bd9Sstevel@tonic-gate 		/*
62717c478bd9Sstevel@tonic-gate 		 * Update the passed-in arguments with the
62727c478bd9Sstevel@tonic-gate 		 * resolvable multirt route we found.
62737c478bd9Sstevel@tonic-gate 		 */
62747c478bd9Sstevel@tonic-gate 		*fire_arg = best_fire;
62757c478bd9Sstevel@tonic-gate 		*ire_arg = best_cire;
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_multirt_lookup: returning B_TRUE, "
62787c478bd9Sstevel@tonic-gate 		    "*fire_arg %p, *ire_arg %p\n",
62797c478bd9Sstevel@tonic-gate 		    (void *)best_fire, (void *)best_cire));
62807c478bd9Sstevel@tonic-gate 
62817c478bd9Sstevel@tonic-gate 		return (B_TRUE);
62827c478bd9Sstevel@tonic-gate 	}
62837c478bd9Sstevel@tonic-gate 
62847c478bd9Sstevel@tonic-gate 	ASSERT(best_cire == NULL);
62857c478bd9Sstevel@tonic-gate 
62867c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: returning B_FALSE, *fire_arg %p, "
62877c478bd9Sstevel@tonic-gate 	    "*ire_arg %p\n",
62887c478bd9Sstevel@tonic-gate 	    (void *)*fire_arg, (void *)*ire_arg));
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 	/* No resolvable route. */
62917c478bd9Sstevel@tonic-gate 	return (B_FALSE);
62927c478bd9Sstevel@tonic-gate }
62937c478bd9Sstevel@tonic-gate 
62947c478bd9Sstevel@tonic-gate /*
62957c478bd9Sstevel@tonic-gate  * IRE iterator for inbound and loopback broadcast processing.
62967c478bd9Sstevel@tonic-gate  * Given an IRE_BROADCAST ire, walk the ires with the same destination
62977c478bd9Sstevel@tonic-gate  * address, but skip over the passed-in ire. Returns the next ire without
62987c478bd9Sstevel@tonic-gate  * a hold - assumes that the caller holds a reference on the IRE bucket.
62997c478bd9Sstevel@tonic-gate  */
63007c478bd9Sstevel@tonic-gate ire_t *
63017c478bd9Sstevel@tonic-gate ire_get_next_bcast_ire(ire_t *curr, ire_t *ire)
63027c478bd9Sstevel@tonic-gate {
63037c478bd9Sstevel@tonic-gate 	ill_t *ill;
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 	if (curr == NULL) {
63067c478bd9Sstevel@tonic-gate 		for (curr = ire->ire_bucket->irb_ire; curr != NULL;
63077c478bd9Sstevel@tonic-gate 		    curr = curr->ire_next) {
63087c478bd9Sstevel@tonic-gate 			if (curr->ire_addr == ire->ire_addr)
63097c478bd9Sstevel@tonic-gate 				break;
63107c478bd9Sstevel@tonic-gate 		}
63117c478bd9Sstevel@tonic-gate 	} else {
63127c478bd9Sstevel@tonic-gate 		curr = curr->ire_next;
63137c478bd9Sstevel@tonic-gate 	}
63147c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(ire);
63157c478bd9Sstevel@tonic-gate 	for (; curr != NULL; curr = curr->ire_next) {
63167c478bd9Sstevel@tonic-gate 		if (curr->ire_addr != ire->ire_addr) {
63177c478bd9Sstevel@tonic-gate 			/*
63187c478bd9Sstevel@tonic-gate 			 * All the IREs to a given destination are contiguous;
63197c478bd9Sstevel@tonic-gate 			 * break out once the address doesn't match.
63207c478bd9Sstevel@tonic-gate 			 */
63217c478bd9Sstevel@tonic-gate 			break;
63227c478bd9Sstevel@tonic-gate 		}
63237c478bd9Sstevel@tonic-gate 		if (curr == ire) {
63247c478bd9Sstevel@tonic-gate 			/* skip over the passed-in ire */
63257c478bd9Sstevel@tonic-gate 			continue;
63267c478bd9Sstevel@tonic-gate 		}
63277c478bd9Sstevel@tonic-gate 		if ((curr->ire_stq != NULL && ire->ire_stq == NULL) ||
63287c478bd9Sstevel@tonic-gate 		    (curr->ire_stq == NULL && ire->ire_stq != NULL)) {
63297c478bd9Sstevel@tonic-gate 			/*
63307c478bd9Sstevel@tonic-gate 			 * If the passed-in ire is loopback, skip over
63317c478bd9Sstevel@tonic-gate 			 * non-loopback ires and vice versa.
63327c478bd9Sstevel@tonic-gate 			 */
63337c478bd9Sstevel@tonic-gate 			continue;
63347c478bd9Sstevel@tonic-gate 		}
63357c478bd9Sstevel@tonic-gate 		if (ire_to_ill(curr) != ill) {
63367c478bd9Sstevel@tonic-gate 			/* skip over IREs going through a different interface */
63377c478bd9Sstevel@tonic-gate 			continue;
63387c478bd9Sstevel@tonic-gate 		}
63397c478bd9Sstevel@tonic-gate 		if (curr->ire_marks & IRE_MARK_CONDEMNED) {
63407c478bd9Sstevel@tonic-gate 			/* skip over deleted IREs */
63417c478bd9Sstevel@tonic-gate 			continue;
63427c478bd9Sstevel@tonic-gate 		}
63437c478bd9Sstevel@tonic-gate 		return (curr);
63447c478bd9Sstevel@tonic-gate 	}
63457c478bd9Sstevel@tonic-gate 	return (NULL);
63467c478bd9Sstevel@tonic-gate }
63477c478bd9Sstevel@tonic-gate 
63487c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
63497c478bd9Sstevel@tonic-gate th_trace_t *
63507c478bd9Sstevel@tonic-gate th_trace_ire_lookup(ire_t *ire)
63517c478bd9Sstevel@tonic-gate {
63527c478bd9Sstevel@tonic-gate 	int bucket_id;
63537c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ire->ire_lock));
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate 	bucket_id = IP_TR_HASH(curthread);
63587c478bd9Sstevel@tonic-gate 	ASSERT(bucket_id < IP_TR_HASH_MAX);
63597c478bd9Sstevel@tonic-gate 
63607c478bd9Sstevel@tonic-gate 	for (th_trace = ire->ire_trace[bucket_id]; th_trace != NULL;
63617c478bd9Sstevel@tonic-gate 	    th_trace = th_trace->th_next) {
63627c478bd9Sstevel@tonic-gate 		if (th_trace->th_id == curthread)
63637c478bd9Sstevel@tonic-gate 			return (th_trace);
63647c478bd9Sstevel@tonic-gate 	}
63657c478bd9Sstevel@tonic-gate 	return (NULL);
63667c478bd9Sstevel@tonic-gate }
63677c478bd9Sstevel@tonic-gate 
63687c478bd9Sstevel@tonic-gate void
63697c478bd9Sstevel@tonic-gate ire_trace_ref(ire_t *ire)
63707c478bd9Sstevel@tonic-gate {
63717c478bd9Sstevel@tonic-gate 	int bucket_id;
63727c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
63737c478bd9Sstevel@tonic-gate 
63747c478bd9Sstevel@tonic-gate 	/*
63757c478bd9Sstevel@tonic-gate 	 * Attempt to locate the trace buffer for the curthread.
63767c478bd9Sstevel@tonic-gate 	 * If it does not exist, then allocate a new trace buffer
63777c478bd9Sstevel@tonic-gate 	 * and link it in list of trace bufs for this ipif, at the head
63787c478bd9Sstevel@tonic-gate 	 */
63797c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
63807c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
63817c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
63827c478bd9Sstevel@tonic-gate 		return;
63837c478bd9Sstevel@tonic-gate 	}
63847c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
63857c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
63867c478bd9Sstevel@tonic-gate 		bucket_id = IP_TR_HASH(curthread);
63877c478bd9Sstevel@tonic-gate 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
63887c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
63897c478bd9Sstevel@tonic-gate 		if (th_trace == NULL) {
63907c478bd9Sstevel@tonic-gate 			ire->ire_trace_disable = B_TRUE;
63917c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
63927c478bd9Sstevel@tonic-gate 			ire_trace_inactive(ire);
63937c478bd9Sstevel@tonic-gate 			return;
63947c478bd9Sstevel@tonic-gate 		}
63957c478bd9Sstevel@tonic-gate 
63967c478bd9Sstevel@tonic-gate 		th_trace->th_id = curthread;
63977c478bd9Sstevel@tonic-gate 		th_trace->th_next = ire->ire_trace[bucket_id];
63987c478bd9Sstevel@tonic-gate 		th_trace->th_prev = &ire->ire_trace[bucket_id];
63997c478bd9Sstevel@tonic-gate 		if (th_trace->th_next != NULL)
64007c478bd9Sstevel@tonic-gate 			th_trace->th_next->th_prev = &th_trace->th_next;
64017c478bd9Sstevel@tonic-gate 		ire->ire_trace[bucket_id] = th_trace;
64027c478bd9Sstevel@tonic-gate 	}
64037c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt < TR_BUF_MAX - 1);
64047c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt++;
64057c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
64067c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
64077c478bd9Sstevel@tonic-gate }
64087c478bd9Sstevel@tonic-gate 
64097c478bd9Sstevel@tonic-gate void
64107c478bd9Sstevel@tonic-gate ire_trace_free(th_trace_t *th_trace)
64117c478bd9Sstevel@tonic-gate {
64127c478bd9Sstevel@tonic-gate 	/* unlink th_trace and free it */
64137c478bd9Sstevel@tonic-gate 	*th_trace->th_prev = th_trace->th_next;
64147c478bd9Sstevel@tonic-gate 	if (th_trace->th_next != NULL)
64157c478bd9Sstevel@tonic-gate 		th_trace->th_next->th_prev = th_trace->th_prev;
64167c478bd9Sstevel@tonic-gate 	th_trace->th_next = NULL;
64177c478bd9Sstevel@tonic-gate 	th_trace->th_prev = NULL;
64187c478bd9Sstevel@tonic-gate 	kmem_free(th_trace, sizeof (th_trace_t));
64197c478bd9Sstevel@tonic-gate }
64207c478bd9Sstevel@tonic-gate 
64217c478bd9Sstevel@tonic-gate void
64227c478bd9Sstevel@tonic-gate ire_untrace_ref(ire_t *ire)
64237c478bd9Sstevel@tonic-gate {
64247c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
64257c478bd9Sstevel@tonic-gate 
64267c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
64297c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
64307c478bd9Sstevel@tonic-gate 		return;
64317c478bd9Sstevel@tonic-gate 	}
64327c478bd9Sstevel@tonic-gate 
64337c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
64347c478bd9Sstevel@tonic-gate 	ASSERT(th_trace != NULL && th_trace->th_refcnt > 0);
64357c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
64367c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt--;
64377c478bd9Sstevel@tonic-gate 
64387c478bd9Sstevel@tonic-gate 	if (th_trace->th_refcnt == 0)
64397c478bd9Sstevel@tonic-gate 		ire_trace_free(th_trace);
64407c478bd9Sstevel@tonic-gate 
64417c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
64427c478bd9Sstevel@tonic-gate }
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate static void
64457c478bd9Sstevel@tonic-gate ire_trace_inactive(ire_t *ire)
64467c478bd9Sstevel@tonic-gate {
64477c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
64487c478bd9Sstevel@tonic-gate 	int i;
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
64517c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
64527c478bd9Sstevel@tonic-gate 		while (ire->ire_trace[i] != NULL) {
64537c478bd9Sstevel@tonic-gate 			th_trace = ire->ire_trace[i];
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate 			/* unlink th_trace and free it */
64567c478bd9Sstevel@tonic-gate 			ire->ire_trace[i] = th_trace->th_next;
64577c478bd9Sstevel@tonic-gate 			if (th_trace->th_next != NULL)
64587c478bd9Sstevel@tonic-gate 				th_trace->th_next->th_prev =
64597c478bd9Sstevel@tonic-gate 				    &ire->ire_trace[i];
64607c478bd9Sstevel@tonic-gate 
64617c478bd9Sstevel@tonic-gate 			th_trace->th_next = NULL;
64627c478bd9Sstevel@tonic-gate 			th_trace->th_prev = NULL;
64637c478bd9Sstevel@tonic-gate 			kmem_free(th_trace, sizeof (th_trace_t));
64647c478bd9Sstevel@tonic-gate 		}
64657c478bd9Sstevel@tonic-gate 	}
64667c478bd9Sstevel@tonic-gate 
64677c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
64687c478bd9Sstevel@tonic-gate }
64697c478bd9Sstevel@tonic-gate 
64707c478bd9Sstevel@tonic-gate /* ARGSUSED */
64717c478bd9Sstevel@tonic-gate void
64727c478bd9Sstevel@tonic-gate ire_thread_exit(ire_t *ire, caddr_t arg)
64737c478bd9Sstevel@tonic-gate {
64747c478bd9Sstevel@tonic-gate 	th_trace_t	*th_trace;
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
64777c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
64787c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
64797c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
64807c478bd9Sstevel@tonic-gate 		return;
64817c478bd9Sstevel@tonic-gate 	}
64827c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt == 0);
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 	ire_trace_free(th_trace);
64857c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
64867c478bd9Sstevel@tonic-gate }
64877c478bd9Sstevel@tonic-gate 
64887c478bd9Sstevel@tonic-gate #endif
6489c793af95Ssangeeta 
6490c793af95Ssangeeta /*
6491c793af95Ssangeeta  * Generate a message chain with an arp request to resolve the in_ire.
6492c793af95Ssangeeta  * It is assumed that in_ire itself is currently in the ire cache table,
6493c793af95Ssangeeta  * so we create a fake_ire filled with enough information about ire_addr etc.
6494c793af95Ssangeeta  * to retrieve in_ire when the DL_UNITDATA response from the resolver
6495c793af95Ssangeeta  * comes back. The fake_ire itself is created by calling esballoc with
6496c793af95Ssangeeta  * the fr_rtnp (free routine) set to ire_freemblk. This routine will be
6497c793af95Ssangeeta  * invoked when the mblk containing fake_ire is freed.
6498c793af95Ssangeeta  */
6499c793af95Ssangeeta void
6500c793af95Ssangeeta ire_arpresolve(ire_t *in_ire, ill_t *dst_ill)
6501c793af95Ssangeeta {
6502c793af95Ssangeeta 	areq_t		*areq;
6503c793af95Ssangeeta 	ipaddr_t	*addrp;
6504c793af95Ssangeeta 	mblk_t 		*ire_mp, *dlureq_mp;
6505c793af95Ssangeeta 	ire_t 		*ire, *buf;
6506c793af95Ssangeeta 	size_t		bufsize;
6507c793af95Ssangeeta 	frtn_t		*frtnp;
6508c793af95Ssangeeta 	ill_t		*ill;
6509f4b3ec61Sdh155122 	ip_stack_t	*ipst = dst_ill->ill_ipst;
6510c793af95Ssangeeta 
6511c793af95Ssangeeta 	/*
6512c793af95Ssangeeta 	 * Construct message chain for the resolver
6513c793af95Ssangeeta 	 * of the form:
6514c793af95Ssangeeta 	 *	ARP_REQ_MBLK-->IRE_MBLK
6515c793af95Ssangeeta 	 *
6516c793af95Ssangeeta 	 * NOTE : If the response does not
6517c793af95Ssangeeta 	 * come back, ARP frees the packet. For this reason,
6518c793af95Ssangeeta 	 * we can't REFHOLD the bucket of save_ire to prevent
6519c793af95Ssangeeta 	 * deletions. We may not be able to REFRELE the bucket
6520c793af95Ssangeeta 	 * if the response never comes back. Thus, before
6521c793af95Ssangeeta 	 * adding the ire, ire_add_v4 will make sure that the
6522c793af95Ssangeeta 	 * interface route does not get deleted. This is the
6523c793af95Ssangeeta 	 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6
6524c793af95Ssangeeta 	 * where we can always prevent deletions because of
6525c793af95Ssangeeta 	 * the synchronous nature of adding IRES i.e
6526c793af95Ssangeeta 	 * ire_add_then_send is called after creating the IRE.
6527c793af95Ssangeeta 	 */
6528c793af95Ssangeeta 
6529c793af95Ssangeeta 	/*
6530c793af95Ssangeeta 	 * We use esballoc to allocate the second part(the ire_t size mblk)
6531c793af95Ssangeeta 	 * of the message chain depicted above. THis mblk will be freed
6532c793af95Ssangeeta 	 * by arp when there is a  timeout, and otherwise passed to IP
6533c793af95Ssangeeta 	 * and IP will * free it after processing the ARP response.
6534c793af95Ssangeeta 	 */
6535c793af95Ssangeeta 
6536c793af95Ssangeeta 	bufsize = sizeof (ire_t) + sizeof (frtn_t);
6537c793af95Ssangeeta 	buf = kmem_alloc(bufsize, KM_NOSLEEP);
6538c793af95Ssangeeta 	if (buf == NULL) {
6539c793af95Ssangeeta 		ip1dbg(("ire_arpresolver:alloc buffer failed\n "));
6540c793af95Ssangeeta 		return;
6541c793af95Ssangeeta 	}
6542c793af95Ssangeeta 	frtnp = (frtn_t *)(buf + 1);
6543c793af95Ssangeeta 	frtnp->free_arg = (caddr_t)buf;
6544c793af95Ssangeeta 	frtnp->free_func = ire_freemblk;
6545c793af95Ssangeeta 
6546c793af95Ssangeeta 	ire_mp = esballoc((unsigned char *)buf, bufsize, BPRI_MED, frtnp);
6547c793af95Ssangeeta 
6548c793af95Ssangeeta 	if (ire_mp == NULL) {
6549c793af95Ssangeeta 		ip1dbg(("ire_arpresolve: esballoc failed\n"));
6550c793af95Ssangeeta 		kmem_free(buf, bufsize);
6551c793af95Ssangeeta 		return;
6552c793af95Ssangeeta 	}
6553c793af95Ssangeeta 	ASSERT(in_ire->ire_nce != NULL);
6554c793af95Ssangeeta 	dlureq_mp = copyb(dst_ill->ill_resolver_mp);
6555c793af95Ssangeeta 	if (dlureq_mp == NULL) {
6556c793af95Ssangeeta 		kmem_free(buf, bufsize);
6557c793af95Ssangeeta 		return;
6558c793af95Ssangeeta 	}
6559c793af95Ssangeeta 
6560c793af95Ssangeeta 	ire_mp->b_datap->db_type = IRE_ARPRESOLVE_TYPE;
6561c793af95Ssangeeta 	ire = (ire_t *)buf;
6562c793af95Ssangeeta 	/*
6563c793af95Ssangeeta 	 * keep enough info in the fake ire so that we can pull up
6564c793af95Ssangeeta 	 * the incomplete ire (in_ire) after result comes back from
6565c793af95Ssangeeta 	 * arp and make it complete.
6566c793af95Ssangeeta 	 */
6567c793af95Ssangeeta 	*ire = ire_null;
6568c793af95Ssangeeta 	ire->ire_u = in_ire->ire_u;
6569c793af95Ssangeeta 	ire->ire_ipif_seqid = in_ire->ire_ipif_seqid;
6570c793af95Ssangeeta 	ire->ire_ipif = in_ire->ire_ipif;
6571c793af95Ssangeeta 	ire->ire_stq = in_ire->ire_stq;
6572c793af95Ssangeeta 	ill = ire_to_ill(ire);
6573c793af95Ssangeeta 	ire->ire_stq_ifindex = ill->ill_phyint->phyint_ifindex;
6574c793af95Ssangeeta 	ire->ire_zoneid = in_ire->ire_zoneid;
6575f4b3ec61Sdh155122 	ire->ire_ipst = ipst;
6576f4b3ec61Sdh155122 
6577c793af95Ssangeeta 	/*
6578c793af95Ssangeeta 	 * ire_freemblk will be called when ire_mp is freed, both for
6579c793af95Ssangeeta 	 * successful and failed arp resolution. IRE_MARK_UNCACHED will be set
6580c793af95Ssangeeta 	 * when the arp resolution failed.
6581c793af95Ssangeeta 	 */
6582c793af95Ssangeeta 	ire->ire_marks |= IRE_MARK_UNCACHED;
6583c793af95Ssangeeta 	ire->ire_mp = ire_mp;
6584c793af95Ssangeeta 	ire_mp->b_wptr = (uchar_t *)&ire[1];
6585c793af95Ssangeeta 	ire_mp->b_cont = NULL;
6586c793af95Ssangeeta 	ASSERT(dlureq_mp != NULL);
6587c793af95Ssangeeta 	linkb(dlureq_mp, ire_mp);
6588c793af95Ssangeeta 
6589c793af95Ssangeeta 	/*
6590c793af95Ssangeeta 	 * Fill in the source and dest addrs for the resolver.
6591c793af95Ssangeeta 	 * NOTE: this depends on memory layouts imposed by
6592c793af95Ssangeeta 	 * ill_init().
6593c793af95Ssangeeta 	 */
6594c793af95Ssangeeta 	areq = (areq_t *)dlureq_mp->b_rptr;
6595c793af95Ssangeeta 	addrp = (ipaddr_t *)((char *)areq + areq->areq_sender_addr_offset);
6596c793af95Ssangeeta 	*addrp = ire->ire_src_addr;
6597c793af95Ssangeeta 
6598c793af95Ssangeeta 	addrp = (ipaddr_t *)((char *)areq + areq->areq_target_addr_offset);
6599c793af95Ssangeeta 	if (ire->ire_gateway_addr != INADDR_ANY) {
6600c793af95Ssangeeta 		*addrp = ire->ire_gateway_addr;
6601c793af95Ssangeeta 	} else {
6602c793af95Ssangeeta 		*addrp = ire->ire_addr;
6603c793af95Ssangeeta 	}
6604c793af95Ssangeeta 
6605c793af95Ssangeeta 	/* Up to the resolver. */
6606c793af95Ssangeeta 	if (canputnext(dst_ill->ill_rq)) {
6607c793af95Ssangeeta 		putnext(dst_ill->ill_rq, dlureq_mp);
6608c793af95Ssangeeta 	} else {
6609c793af95Ssangeeta 		/* Prepare for cleanup */
6610c793af95Ssangeeta 		freemsg(dlureq_mp);
6611c793af95Ssangeeta 	}
6612c793af95Ssangeeta }
6613c793af95Ssangeeta 
6614c793af95Ssangeeta /*
6615c793af95Ssangeeta  * Esballoc free function for AR_ENTRY_QUERY request to clean up any
6616c793af95Ssangeeta  * unresolved ire_t and/or nce_t structures when ARP resolution fails.
6617c793af95Ssangeeta  *
6618c793af95Ssangeeta  * This function can be called by ARP via free routine for ire_mp or
6619c793af95Ssangeeta  * by IPv4(both host and forwarding path) via ire_delete
6620c793af95Ssangeeta  * in case ARP resolution fails.
6621c793af95Ssangeeta  * NOTE: Since IP is MT, ARP can call into IP but not vice versa
6622c793af95Ssangeeta  * (for IP to talk to ARP, it still has to send AR* messages).
6623c793af95Ssangeeta  *
6624c793af95Ssangeeta  * Note that the ARP/IP merge should replace the functioanlity by providing
6625c793af95Ssangeeta  * direct function calls to clean up unresolved entries in ire/nce lists.
6626c793af95Ssangeeta  */
6627c793af95Ssangeeta void
6628c793af95Ssangeeta ire_freemblk(ire_t *ire_mp)
6629c793af95Ssangeeta {
6630c793af95Ssangeeta 	nce_t		*nce = NULL;
6631c793af95Ssangeeta 	ill_t		*ill;
6632f4b3ec61Sdh155122 	ip_stack_t	*ipst;
6633c793af95Ssangeeta 
6634c793af95Ssangeeta 	ASSERT(ire_mp != NULL);
6635c793af95Ssangeeta 
6636c793af95Ssangeeta 	if ((ire_mp->ire_addr == NULL) && (ire_mp->ire_gateway_addr == NULL)) {
6637c793af95Ssangeeta 		ip1dbg(("ire_freemblk(0x%p) ire_addr is NULL\n",
6638c793af95Ssangeeta 		    (void *)ire_mp));
6639c793af95Ssangeeta 		goto cleanup;
6640c793af95Ssangeeta 	}
6641c793af95Ssangeeta 	if ((ire_mp->ire_marks & IRE_MARK_UNCACHED) == 0) {
6642c793af95Ssangeeta 		goto cleanup; /* everything succeeded. just free and return */
6643c793af95Ssangeeta 	}
6644c793af95Ssangeeta 
6645c793af95Ssangeeta 	/*
6646c793af95Ssangeeta 	 * the arp information corresponding to this ire_mp was not
6647c793af95Ssangeeta 	 * transferred to  a ire_cache entry. Need
6648c793af95Ssangeeta 	 * to clean up incomplete ire's and nce, if necessary.
6649c793af95Ssangeeta 	 */
6650c793af95Ssangeeta 	ASSERT(ire_mp->ire_stq != NULL);
6651c793af95Ssangeeta 	ASSERT(ire_mp->ire_stq_ifindex != 0);
6652f4b3ec61Sdh155122 	ASSERT(ire_mp->ire_ipst != NULL);
6653f4b3ec61Sdh155122 
6654f4b3ec61Sdh155122 	ipst = ire_mp->ire_ipst;
6655f4b3ec61Sdh155122 
6656c793af95Ssangeeta 	/*
6657c793af95Ssangeeta 	 * Get any nce's corresponding to this ire_mp. We first have to
6658c793af95Ssangeeta 	 * make sure that the ill is still around.
6659c793af95Ssangeeta 	 */
6660f4b3ec61Sdh155122 	ill = ill_lookup_on_ifindex(ire_mp->ire_stq_ifindex,
6661f4b3ec61Sdh155122 	    B_FALSE, NULL, NULL, NULL, NULL, ipst);
6662c793af95Ssangeeta 	if (ill == NULL || (ire_mp->ire_stq != ill->ill_wq) ||
6663c793af95Ssangeeta 	    (ill->ill_state_flags & ILL_CONDEMNED)) {
6664c793af95Ssangeeta 		/*
6665c793af95Ssangeeta 		 * ill went away. no nce to clean up.
6666c793af95Ssangeeta 		 * Note that the ill_state_flags could be set to
6667c793af95Ssangeeta 		 * ILL_CONDEMNED after this point, but if we know
6668c793af95Ssangeeta 		 * that it is CONDEMNED now, we just bail out quickly.
6669c793af95Ssangeeta 		 */
6670c793af95Ssangeeta 		if (ill != NULL)
6671c793af95Ssangeeta 			ill_refrele(ill);
6672c793af95Ssangeeta 		goto cleanup;
6673c793af95Ssangeeta 	}
6674c793af95Ssangeeta 	nce = ndp_lookup_v4(ill,
6675c793af95Ssangeeta 	    ((ire_mp->ire_gateway_addr != INADDR_ANY) ?
6676c793af95Ssangeeta 	    &ire_mp->ire_gateway_addr : &ire_mp->ire_addr),
6677c793af95Ssangeeta 	    B_FALSE);
6678c793af95Ssangeeta 	ill_refrele(ill);
6679c793af95Ssangeeta 
6680c793af95Ssangeeta 	if ((nce != NULL) && (nce->nce_state != ND_REACHABLE)) {
6681c793af95Ssangeeta 		/*
6682c793af95Ssangeeta 		 * some incomplete nce was found.
6683c793af95Ssangeeta 		 */
6684c793af95Ssangeeta 		DTRACE_PROBE2(ire__freemblk__arp__resolv__fail,
6685c793af95Ssangeeta 		    nce_t *, nce, ire_t *, ire_mp);
6686c793af95Ssangeeta 		/*
6687c793af95Ssangeeta 		 * Send the icmp_unreachable messages for the queued mblks in
6688c793af95Ssangeeta 		 * ire->ire_nce->nce_qd_mp, since ARP resolution failed
6689c793af95Ssangeeta 		 * for this ire
6690c793af95Ssangeeta 		 */
6691c793af95Ssangeeta 		arp_resolv_failed(nce);
6692c793af95Ssangeeta 		/*
6693c793af95Ssangeeta 		 * Delete the nce and clean up all ire's pointing at this nce
6694c793af95Ssangeeta 		 * in the cachetable
6695c793af95Ssangeeta 		 */
6696c793af95Ssangeeta 		ndp_delete(nce);
6697c793af95Ssangeeta 	}
6698c793af95Ssangeeta 	if (nce != NULL)
6699c793af95Ssangeeta 		NCE_REFRELE(nce); /* release the ref taken by ndp_lookup_v4 */
6700c793af95Ssangeeta 
6701c793af95Ssangeeta cleanup:
6702c793af95Ssangeeta 	/*
6703c793af95Ssangeeta 	 * Get rid of the ire buffer
6704c793af95Ssangeeta 	 * We call kmem_free here(instead of ire_delete()), since
6705c793af95Ssangeeta 	 * this is the freeb's callback.
6706c793af95Ssangeeta 	 */
6707c793af95Ssangeeta 	kmem_free(ire_mp, sizeof (ire_t) + sizeof (frtn_t));
6708c793af95Ssangeeta }
6709c793af95Ssangeeta 
6710c0861880Ssangeeta /*
6711c0861880Ssangeeta  * The mp passed to this function is typically res_mp.
6712c0861880Ssangeeta  * Note that res_mp field can contain the request (ie AR_ENTRY_QUERY)
6713c0861880Ssangeeta  * or the response (ie DL_UNITDATA_REQ). So in case of the
6714c0861880Ssangeeta  * forwarding path, there is a small window of time between the two
6715c0861880Ssangeeta  * when the forwarding path creates an unresolved nce and the
6716c0861880Ssangeeta  * ip_newroute path finds this and uses this in the creation of an
6717c0861880Ssangeeta  * ire cache. To account for this possible race case we we check
6718c0861880Ssangeeta  * for DL_UNITDATA_REQ to make sure this is indeed the response.
6719c0861880Ssangeeta  */
6720c0861880Ssangeeta boolean_t
6721c0861880Ssangeeta ire_nce_valid_dlureq_mp(mblk_t *mp)
6722c0861880Ssangeeta {
6723c0861880Ssangeeta 	dl_unitdata_req_t *dlur;
6724c0861880Ssangeeta 
6725c0861880Ssangeeta 	if (mp == NULL)
6726c0861880Ssangeeta 		return (B_FALSE);
6727c0861880Ssangeeta 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
6728c0861880Ssangeeta 	if ((DB_TYPE(mp) == M_PROTO) &&
6729c0861880Ssangeeta 	    (dlur->dl_primitive == DL_UNITDATA_REQ)) {
6730c0861880Ssangeeta 		return (B_TRUE);
6731c0861880Ssangeeta 	} else {
6732c0861880Ssangeeta 		return (B_FALSE);
6733c0861880Ssangeeta 	}
6734c0861880Ssangeeta }
6735c793af95Ssangeeta 
6736c793af95Ssangeeta /*
6737c793af95Ssangeeta  * create the neighbor cache entry  nce_t for  IRE_CACHE and
6738c793af95Ssangeeta  * non-loopback IRE_BROADCAST ire's. Note that IRE_BROADCAST
6739c793af95Ssangeeta  * (non-loopback) entries  have the nce_res_mp set to the
6740c793af95Ssangeeta  * template passed in (generated from ill_bcast_mp); IRE_CACHE ire's
6741c793af95Ssangeeta  * contain the information for  the nexthop (ire_gateway_addr) in the
6742c793af95Ssangeeta  * case of indirect routes, and for the dst itself (ire_addr) in the
6743c793af95Ssangeeta  * case of direct routes, with the nce_res_mp containing a template
6744c793af95Ssangeeta  * DL_UNITDATA request.
6745c793af95Ssangeeta  *
6746c793af95Ssangeeta  * This function always consumes res_mp and fp_mp.
6747c793af95Ssangeeta  *
6748c793af95Ssangeeta  * The actual association of the ire_nce to the nce created here is
6749c793af95Ssangeeta  * typically done in ire_add_v4 for IRE_CACHE entries. Exceptions
6750c793af95Ssangeeta  * to this rule are SO_DONTROUTE ire's (IRE_MARK_NO_ADD), for which
6751c793af95Ssangeeta  * the ire_nce assignment is done in ire_add_then_send, and mobile-ip
6752c793af95Ssangeeta  * where the assignment is done in ire_add_mrtun().
6753c793af95Ssangeeta  */
6754c793af95Ssangeeta int
6755c793af95Ssangeeta ire_nce_init(ire_t *ire, mblk_t *fp_mp, mblk_t *res_mp)
6756c793af95Ssangeeta {
6757c793af95Ssangeeta 	in_addr_t	addr4, mask4;
6758c793af95Ssangeeta 	int		err;
6759c793af95Ssangeeta 	nce_t		*arpce = NULL;
6760c793af95Ssangeeta 	ill_t		*ire_ill;
6761c793af95Ssangeeta 	uint16_t	nce_state, nce_flags;
6762f4b3ec61Sdh155122 	ip_stack_t	*ipst;
6763c793af95Ssangeeta 
6764c793af95Ssangeeta 	if (ire->ire_stq == NULL) {
6765c793af95Ssangeeta 		if (res_mp)
6766c793af95Ssangeeta 			freemsg(res_mp);
6767c793af95Ssangeeta 		if (fp_mp)
6768c793af95Ssangeeta 			freemsg(fp_mp);
6769c793af95Ssangeeta 		return (0); /* no need to create nce for local/loopback */
6770c793af95Ssangeeta 	}
6771c793af95Ssangeeta 
6772c793af95Ssangeeta 	mask4 = IP_HOST_MASK;
6773c793af95Ssangeeta 	switch (ire->ire_type) {
6774c793af95Ssangeeta 	case IRE_CACHE:
6775c793af95Ssangeeta 		if (ire->ire_gateway_addr != INADDR_ANY)
6776c793af95Ssangeeta 			addr4 = ire->ire_gateway_addr; /* 'G' route */
6777c793af95Ssangeeta 		else
6778c793af95Ssangeeta 			addr4 = ire->ire_addr; /* direct route */
6779c793af95Ssangeeta 		break;
6780c793af95Ssangeeta 	case IRE_BROADCAST:
6781c793af95Ssangeeta 		addr4 = ire->ire_addr;
6782c793af95Ssangeeta 		break;
6783c793af95Ssangeeta 	default:
6784c793af95Ssangeeta 		if (res_mp)
6785c793af95Ssangeeta 			freemsg(res_mp);
6786c793af95Ssangeeta 		if (fp_mp)
6787c793af95Ssangeeta 			freemsg(fp_mp);
6788c793af95Ssangeeta 		return (0);
6789c793af95Ssangeeta 	}
6790c793af95Ssangeeta 
6791c793af95Ssangeeta 	/*
6792c793af95Ssangeeta 	 * ire_ipif is picked based on RTF_SETSRC, usesrc etc.
6793c793af95Ssangeeta 	 * rules in ire_forward_src_ipif. We want the dlureq_mp
6794c793af95Ssangeeta 	 * for the outgoing interface, which we get from the ire_stq.
6795c793af95Ssangeeta 	 */
6796c793af95Ssangeeta 	ire_ill = ire_to_ill(ire);
6797f4b3ec61Sdh155122 	ipst = ire_ill->ill_ipst;
6798c793af95Ssangeeta 
6799c793af95Ssangeeta 	/*
6800c793af95Ssangeeta 	 * if we are creating an nce for the first time, and this is
6801c793af95Ssangeeta 	 * a NORESOLVER interface, atomically create the nce in the
6802c793af95Ssangeeta 	 * REACHABLE state; else create it in the ND_INITIAL state.
6803c793af95Ssangeeta 	 */
6804c793af95Ssangeeta 	if (ire_ill->ill_net_type == IRE_IF_NORESOLVER)  {
6805c793af95Ssangeeta 		nce_state = ND_REACHABLE;
6806c793af95Ssangeeta 		nce_flags = NCE_F_PERMANENT;
6807c793af95Ssangeeta 	} else {
6808c0861880Ssangeeta 		/* Make sure you have the response and not the request. */
6809c0861880Ssangeeta 		if (ire_nce_valid_dlureq_mp(res_mp)) {
6810c793af95Ssangeeta 			nce_state = ND_REACHABLE;
6811c0861880Ssangeeta 		} else {
6812c793af95Ssangeeta 			nce_state = ND_INITIAL;
6813*b9c344b3Ssowmini 			ASSERT(fp_mp == NULL);
6814c0861880Ssangeeta 		}
6815c793af95Ssangeeta 		nce_flags = 0;
6816c793af95Ssangeeta 	}
6817c793af95Ssangeeta 
6818*b9c344b3Ssowmini retry_nce:
6819c793af95Ssangeeta 	err = ndp_lookup_then_add(ire_ill, NULL,
6820c793af95Ssangeeta 	    &addr4, &mask4, NULL, 0, nce_flags, nce_state, &arpce,
6821c793af95Ssangeeta 	    fp_mp, res_mp);
6822c793af95Ssangeeta 
6823*b9c344b3Ssowmini 	if (err == EEXIST && NCE_EXPIRED(arpce, ipst)) {
6824*b9c344b3Ssowmini 		/*
6825*b9c344b3Ssowmini 		 * We looked up an expired nce.
6826*b9c344b3Ssowmini 		 * Go back and try to create one again.
6827*b9c344b3Ssowmini 		 * The res_mp should be intact for the EEXIST case,
6828*b9c344b3Ssowmini 		 * i.e., there are no new references to it, and there
6829*b9c344b3Ssowmini 		 * is no need to copyb it.
6830*b9c344b3Ssowmini 		 */
6831*b9c344b3Ssowmini 		ndp_delete(arpce);
6832*b9c344b3Ssowmini 		NCE_REFRELE(arpce);
6833*b9c344b3Ssowmini 		arpce = NULL;
6834*b9c344b3Ssowmini 		goto retry_nce;
6835*b9c344b3Ssowmini 	}
6836*b9c344b3Ssowmini 
6837c793af95Ssangeeta 	ip1dbg(("ire 0x%p addr 0x%lx mask 0x%lx type 0x%x; "
6838c793af95Ssangeeta 	    "found nce 0x%p err %d\n", (void *)ire, (ulong_t)addr4,
6839c793af95Ssangeeta 	    (ulong_t)mask4, ire->ire_type, (void *)arpce, err));
6840c793af95Ssangeeta 
6841c793af95Ssangeeta 	switch (err) {
6842c793af95Ssangeeta 	case 0:
6843c793af95Ssangeeta 		break;
6844c793af95Ssangeeta 	case EEXIST:
6845c793af95Ssangeeta 		/*
6846c793af95Ssangeeta 		 * return a pointer to an existing nce_t;
6847c793af95Ssangeeta 		 * note that the ire-nce mapping is many-one, i.e.,
6848c793af95Ssangeeta 		 * multiple ire's could point to the same nce_t;
6849c793af95Ssangeeta 		 */
6850c793af95Ssangeeta 		if (fp_mp != NULL) {
6851c793af95Ssangeeta 			freemsg(fp_mp);
6852c793af95Ssangeeta 		}
6853c793af95Ssangeeta 		if (res_mp != NULL) {
6854c793af95Ssangeeta 			freemsg(res_mp);
6855c793af95Ssangeeta 		}
6856c793af95Ssangeeta 		break;
6857c793af95Ssangeeta 	default:
6858c793af95Ssangeeta 		DTRACE_PROBE2(nce__init__fail, ill_t *, ire_ill, int, err);
6859c793af95Ssangeeta 		if (res_mp)
6860c793af95Ssangeeta 			freemsg(res_mp);
6861c793af95Ssangeeta 		if (fp_mp)
6862c793af95Ssangeeta 			freemsg(fp_mp);
6863c793af95Ssangeeta 		return (EINVAL);
6864c793af95Ssangeeta 	}
6865c793af95Ssangeeta #if DEBUG
6866c793af95Ssangeeta 	/*
6867c0861880Ssangeeta 	 * If an nce_fp_mp was passed in by ndp_lookup_then_add()
6868c0861880Ssangeeta 	 * we should be picking up an existing nce_t in
6869c0861880Ssangeeta 	 * the ND_REACHABLE state.
6870c793af95Ssangeeta 	 */
6871c793af95Ssangeeta 	mutex_enter(&arpce->nce_lock);
6872c793af95Ssangeeta 	ASSERT(arpce->nce_fp_mp == NULL || arpce->nce_state == ND_REACHABLE);
6873c793af95Ssangeeta 	mutex_exit(&arpce->nce_lock);
6874c793af95Ssangeeta #endif
6875c793af95Ssangeeta 	if (ire->ire_type == IRE_BROADCAST) {
6876c793af95Ssangeeta 		/*
6877c793af95Ssangeeta 		 * Two bcast ires are created for each interface;
6878c793af95Ssangeeta 		 * 1. loopback copy (which does not  have an
6879c793af95Ssangeeta 		 *    ire_stq, and therefore has no ire_nce), and,
6880c793af95Ssangeeta 		 * 2. the non-loopback copy, which has the nce_res_mp
6881c793af95Ssangeeta 		 *    initialized to a copy of the ill_bcast_mp, and
6882c793af95Ssangeeta 		 *    is marked as ND_REACHABLE at this point.
6883c793af95Ssangeeta 		 *    This nce does not undergo any further state changes,
6884c793af95Ssangeeta 		 *    and exists as long as the interface is plumbed.
6885c793af95Ssangeeta 		 * Note: we do the ire_nce assignment here for IRE_BROADCAST
6886c793af95Ssangeeta 		 * because some functions like ill_mark_bcast() inline the
6887c793af95Ssangeeta 		 * ire_add functionality;
6888c793af95Ssangeeta 		 */
6889c793af95Ssangeeta 		mutex_enter(&arpce->nce_lock);
6890c793af95Ssangeeta 		arpce->nce_state = ND_REACHABLE;
6891516bda92Ssowmini 		arpce->nce_flags |= (NCE_F_PERMANENT | NCE_F_BCAST);
6892c793af95Ssangeeta 		arpce->nce_last = TICK_TO_MSEC(lbolt64);
6893c793af95Ssangeeta 		ire->ire_nce = arpce;
6894c793af95Ssangeeta 		mutex_exit(&arpce->nce_lock);
6895c793af95Ssangeeta 		/*
6896c793af95Ssangeeta 		 * We are associating this nce to the ire,
6897c793af95Ssangeeta 		 * so change the nce ref taken in
6898c793af95Ssangeeta 		 * ndp_lookup_then_add_v4() from
6899c793af95Ssangeeta 		 * NCE_REFHOLD to NCE_REFHOLD_NOTR
6900c793af95Ssangeeta 		 */
6901c793af95Ssangeeta 		NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
6902c793af95Ssangeeta 	} else {
6903c793af95Ssangeeta 		/*
6904c793af95Ssangeeta 		 * We are not using this nce_t just yet so release
6905c793af95Ssangeeta 		 * the ref taken in ndp_lookup_then_add_v4()
6906c793af95Ssangeeta 		 */
6907c793af95Ssangeeta 		NCE_REFRELE(arpce);
6908c793af95Ssangeeta 	}
6909c793af95Ssangeeta 	return (0);
6910c793af95Ssangeeta }
6911