xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_ire.c (revision dc3879f943cd4bf5d746a39c24a94ed38245a5b4)
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 /*
2245916cd2Sjpk  * Copyright 2006 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>
587c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
597c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
607c478bd9Sstevel@tonic-gate #include <inet/ip_rts.h>
617c478bd9Sstevel@tonic-gate #include <inet/nd.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
647c478bd9Sstevel@tonic-gate #include <inet/ipsec_info.h>
657c478bd9Sstevel@tonic-gate #include <inet/sadb.h>
667c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
677c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
687c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
697c478bd9Sstevel@tonic-gate #include <sys/zone.h>
707c478bd9Sstevel@tonic-gate 
7145916cd2Sjpk #include <sys/tsol/label.h>
7245916cd2Sjpk #include <sys/tsol/tnet.h>
7345916cd2Sjpk 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Synchronization notes:
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * The fields of the ire_t struct are protected in the following way :
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * ire_next/ire_ptpn
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  *	- bucket lock of the respective tables (cache or forwarding tables).
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * ire_fp_mp
847c478bd9Sstevel@tonic-gate  * ire_dlureq_mp
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  *	- ire_lock protects multiple threads updating ire_fp_mp
877c478bd9Sstevel@tonic-gate  *	  simultaneously. Otherwise no locks are used while accessing
887c478bd9Sstevel@tonic-gate  *	  (both read/write) both the fields.
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * ire_mp, ire_rfq, ire_stq, ire_u *except* ire_gateway_addr[v6], ire_mask,
917c478bd9Sstevel@tonic-gate  * ire_type, ire_create_time, ire_masklen, ire_ipversion, ire_flags, ire_ipif,
927c478bd9Sstevel@tonic-gate  * ire_ihandle, ire_phandle, ire_nce, ire_bucket, ire_in_ill, ire_in_src_addr
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  *	- Set in ire_create_v4/v6 and never changes after that. Thus,
957c478bd9Sstevel@tonic-gate  *	  we don't need a lock whenever these fields are accessed.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	- ire_bucket and ire_masklen (also set in ire_create) is set in
987c478bd9Sstevel@tonic-gate  *        ire_add_v4/ire_add_v6 before inserting in the bucket and never
997c478bd9Sstevel@tonic-gate  *        changes after that. Thus we don't need a lock whenever these
1007c478bd9Sstevel@tonic-gate  *	  fields are accessed.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * ire_gateway_addr_v4[v6]
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  *	- ire_gateway_addr_v4[v6] is set during ire_create and later modified
1057c478bd9Sstevel@tonic-gate  *	  by rts_setgwr[v6]. As ire_gateway_addr is a uint32_t, updates to
1067c478bd9Sstevel@tonic-gate  *	  it assumed to be atomic and hence the other parts of the code
1077c478bd9Sstevel@tonic-gate  *	  does not use any locks. ire_gateway_addr_v6 updates are not atomic
1087c478bd9Sstevel@tonic-gate  *	  and hence any access to it uses ire_lock to get/set the right value.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * ire_ident, ire_refcnt
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  *	- Updated atomically using atomic_add_32
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * ire_ssthresh, ire_rtt_sd, ire_rtt, ire_ib_pkt_count, ire_ob_pkt_count
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *	- Assumes that 32 bit writes are atomic. No locks. ire_lock is
1177c478bd9Sstevel@tonic-gate  *	  used to serialize updates to ire_ssthresh, ire_rtt_sd, ire_rtt.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * ire_max_frag, ire_frag_flag
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  *	- ire_lock is used to set/read both of them together.
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * ire_tire_mark
1247c478bd9Sstevel@tonic-gate  *
1257c478bd9Sstevel@tonic-gate  *	- Set in ire_create and updated in ire_expire, which is called
1267c478bd9Sstevel@tonic-gate  *	  by only one function namely ip_trash_timer_expire. Thus only
1277c478bd9Sstevel@tonic-gate  *	  one function updates and examines the value.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * ire_marks
1307c478bd9Sstevel@tonic-gate  *	- bucket lock protects this.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * ire_ipsec_overhead/ire_ll_hdr_length
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  *	- Place holder for returning the information to the upper layers
1357c478bd9Sstevel@tonic-gate  *	  when IRE_DB_REQ comes down.
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * ip_ire_default_count protected by the bucket lock of
1387c478bd9Sstevel@tonic-gate  * ip_forwarding_table[0][0].
1397c478bd9Sstevel@tonic-gate  *
1407c478bd9Sstevel@tonic-gate  * ipv6_ire_default_count is protected by the bucket lock of
1417c478bd9Sstevel@tonic-gate  * ip_forwarding_table_v6[0][0].
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * ip_ire_default_index/ipv6_ire_default_index is not protected as it
1447c478bd9Sstevel@tonic-gate  * is just a hint at which default gateway to use. There is nothing
1457c478bd9Sstevel@tonic-gate  * wrong in using the same gateway for two different connections.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * As we always hold the bucket locks in all the places while accessing
1487c478bd9Sstevel@tonic-gate  * the above values, it is natural to use them for protecting them.
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * We have a separate cache table and forwarding table for IPv4 and IPv6.
1517c478bd9Sstevel@tonic-gate  * Cache table (ip_cache_table/ip_cache_table_v6) is a pointer to an
1527c478bd9Sstevel@tonic-gate  * array of irb_t structure and forwarding table (ip_forwarding_table/
1537c478bd9Sstevel@tonic-gate  * ip_forwarding_table_v6) is an array of pointers to array of irb_t
1547c478bd9Sstevel@tonic-gate  * structure. ip_forwarding_table[_v6] is allocated dynamically in
1557c478bd9Sstevel@tonic-gate  * ire_add_v4/v6. ire_ft_init_lock is used to serialize multiple threads
1567c478bd9Sstevel@tonic-gate  * initializing the same bucket. Once a bucket is initialized, it is never
1577c478bd9Sstevel@tonic-gate  * de-alloacted. This assumption enables us to access ip_forwarding_table[i]
1587c478bd9Sstevel@tonic-gate  * or ip_forwarding_table_v6[i] without any locks.
1597c478bd9Sstevel@tonic-gate  *
1607c478bd9Sstevel@tonic-gate  * Each irb_t - ire bucket structure has a lock to protect
1617c478bd9Sstevel@tonic-gate  * a bucket and the ires residing in the bucket have a back pointer to
1627c478bd9Sstevel@tonic-gate  * the bucket structure. It also has a reference count for the number
1637c478bd9Sstevel@tonic-gate  * of threads walking the bucket - irb_refcnt which is bumped up
1647c478bd9Sstevel@tonic-gate  * using the macro IRB_REFHOLD macro. The flags irb_flags can be
1657c478bd9Sstevel@tonic-gate  * set to IRE_MARK_CONDEMNED indicating that there are some ires
1667c478bd9Sstevel@tonic-gate  * in this bucket that are marked with IRE_MARK_CONDEMNED and the
1677c478bd9Sstevel@tonic-gate  * last thread to leave the bucket should delete the ires. Usually
1687c478bd9Sstevel@tonic-gate  * this is done by the IRB_REFRELE macro which is used to decrement
1697c478bd9Sstevel@tonic-gate  * the reference count on a bucket.
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * IRE_REFHOLD/IRE_REFRELE macros operate on the ire which increments/
1727c478bd9Sstevel@tonic-gate  * decrements the reference count, ire_refcnt, atomically on the ire.
1737c478bd9Sstevel@tonic-gate  * ire_refcnt is modified only using this macro. Operations on the IRE
1747c478bd9Sstevel@tonic-gate  * could be described as follows :
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * CREATE an ire with reference count initialized to 1.
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  * ADDITION of an ire holds the bucket lock, checks for duplicates
1797c478bd9Sstevel@tonic-gate  * and then adds the ire. ire_add_v4/ire_add_v6 returns the ire after
1807c478bd9Sstevel@tonic-gate  * bumping up once more i.e the reference count is 2. This is to avoid
1817c478bd9Sstevel@tonic-gate  * an extra lookup in the functions calling ire_add which wants to
1827c478bd9Sstevel@tonic-gate  * work with the ire after adding.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * LOOKUP of an ire bumps up the reference count using IRE_REFHOLD
1857c478bd9Sstevel@tonic-gate  * macro. It is valid to bump up the referece count of the IRE,
1867c478bd9Sstevel@tonic-gate  * after the lookup has returned an ire. Following are the lookup
1877c478bd9Sstevel@tonic-gate  * functions that return an HELD ire :
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  * ire_lookup_local[_v6], ire_ctable_lookup[_v6], ire_ftable_lookup[_v6],
1907c478bd9Sstevel@tonic-gate  * ire_cache_lookup[_v6], ire_lookup_multi[_v6], ire_route_lookup[_v6],
1917c478bd9Sstevel@tonic-gate  * ipif_to_ire[_v6], ire_mrtun_lookup, ire_srcif_table_lookup.
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  * DELETION of an ire holds the bucket lock, removes it from the list
1947c478bd9Sstevel@tonic-gate  * and then decrements the reference count for having removed from the list
1957c478bd9Sstevel@tonic-gate  * by using the IRE_REFRELE macro. If some other thread has looked up
1967c478bd9Sstevel@tonic-gate  * the ire, the reference count would have been bumped up and hence
1977c478bd9Sstevel@tonic-gate  * this ire will not be freed once deleted. It will be freed once the
1987c478bd9Sstevel@tonic-gate  * reference count drops to zero.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * Add and Delete acquires the bucket lock as RW_WRITER, while all the
2017c478bd9Sstevel@tonic-gate  * lookups acquire the bucket lock as RW_READER.
2027c478bd9Sstevel@tonic-gate  *
2037c478bd9Sstevel@tonic-gate  * NOTE : The only functions that does the IRE_REFRELE when an ire is
2047c478bd9Sstevel@tonic-gate  *	  passed as an argument are :
2057c478bd9Sstevel@tonic-gate  *
2067c478bd9Sstevel@tonic-gate  *	  1) ip_wput_ire : This is because it IRE_REFHOLD/RELEs the
2077c478bd9Sstevel@tonic-gate  *			   broadcast ires it looks up internally within
2087c478bd9Sstevel@tonic-gate  *			   the function. Currently, for simplicity it does
2097c478bd9Sstevel@tonic-gate  *			   not differentiate the one that is passed in and
2107c478bd9Sstevel@tonic-gate  *			   the ones it looks up internally. It always
2117c478bd9Sstevel@tonic-gate  *			   IRE_REFRELEs.
2127c478bd9Sstevel@tonic-gate  *	  2) ire_send
2137c478bd9Sstevel@tonic-gate  *	     ire_send_v6 : As ire_send calls ip_wput_ire and other functions
2147c478bd9Sstevel@tonic-gate  *			   that take ire as an argument, it has to selectively
2157c478bd9Sstevel@tonic-gate  *			   IRE_REFRELE the ire. To maintain symmetry,
2167c478bd9Sstevel@tonic-gate  *			   ire_send_v6 does the same.
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * Otherwise, the general rule is to do the IRE_REFRELE in the function
2197c478bd9Sstevel@tonic-gate  * that is passing the ire as an argument.
2207c478bd9Sstevel@tonic-gate  *
2217c478bd9Sstevel@tonic-gate  * In trying to locate ires the following points are to be noted.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * IRE_MARK_CONDEMNED signifies that the ire has been logically deleted and is
2247c478bd9Sstevel@tonic-gate  * to be ignored when walking the ires using ire_next.
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * IRE_MARK_HIDDEN signifies that the ire is a special ire typically for the
2277c478bd9Sstevel@tonic-gate  * benefit of in.mpathd which needs to probe interfaces for failures. Normal
2287c478bd9Sstevel@tonic-gate  * applications should not be seeing this ire and hence this ire is ignored
2297c478bd9Sstevel@tonic-gate  * in most cases in the search using ire_next.
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * Zones note:
2327c478bd9Sstevel@tonic-gate  *	Walking IREs within a given zone also walks certain ires in other
2337c478bd9Sstevel@tonic-gate  *	zones.  This is done intentionally.  IRE walks with a specified
2347c478bd9Sstevel@tonic-gate  *	zoneid are used only when doing informational reports, and
2357c478bd9Sstevel@tonic-gate  *	zone users want to see things that they can access. See block
2367c478bd9Sstevel@tonic-gate  *	comment in ire_walk_ill_match().
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static irb_t *ip_forwarding_table[IP_MASK_TABLE_SIZE];
2407c478bd9Sstevel@tonic-gate /* This is dynamically allocated in ip_ire_init */
2417c478bd9Sstevel@tonic-gate static irb_t *ip_cache_table;
2427c478bd9Sstevel@tonic-gate /* This is dynamically allocated in ire_add_mrtun */
2437c478bd9Sstevel@tonic-gate irb_t	*ip_mrtun_table;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate uint32_t	ire_handle = 1;
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * ire_ft_init_lock is used while initializing ip_forwarding_table
2487c478bd9Sstevel@tonic-gate  * dynamically in ire_add.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate kmutex_t	ire_ft_init_lock;
2517c478bd9Sstevel@tonic-gate kmutex_t	ire_mrtun_lock;  /* Protects creation of table and it's count */
2527c478bd9Sstevel@tonic-gate kmutex_t	ire_srcif_table_lock; /* Same as above */
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * The following counts are used to determine whether a walk is
2557c478bd9Sstevel@tonic-gate  * needed through the reverse tunnel table or through ills
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate kmutex_t ire_handle_lock;	/* Protects ire_handle */
2587c478bd9Sstevel@tonic-gate uint_t	ire_mrtun_count;	/* Number of ires in reverse tun table */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * A per-interface routing table is created ( if not present)
2627c478bd9Sstevel@tonic-gate  * when the first entry is added to this special routing table.
2637c478bd9Sstevel@tonic-gate  * This special routing table is accessed through the ill data structure.
2647c478bd9Sstevel@tonic-gate  * The routing table looks like cache table. For example, currently it
2657c478bd9Sstevel@tonic-gate  * is used by mobile-ip foreign agent to forward data that only comes from
2667c478bd9Sstevel@tonic-gate  * the home agent tunnel for a mobile node. Thus if the outgoing interface
2677c478bd9Sstevel@tonic-gate  * is a RESOLVER interface, IP may need to resolve the hardware address for
2687c478bd9Sstevel@tonic-gate  * the outgoing interface. The routing entries in this table are not updated
2697c478bd9Sstevel@tonic-gate  * in IRE_CACHE. When MCTL msg comes back from ARP, the incoming ill informa-
2707c478bd9Sstevel@tonic-gate  * tion is lost as the write queue is passed to ip_wput.
2717c478bd9Sstevel@tonic-gate  * But, before sending the packet out, the hardware information must be updated
2727c478bd9Sstevel@tonic-gate  * in the special forwarding table. ire_srcif_table_count keeps track of total
2737c478bd9Sstevel@tonic-gate  * number of ires that are in interface based tables. Each interface based
2747c478bd9Sstevel@tonic-gate  * table hangs off of the incoming ill and each ill_t also keeps a refcnt
2757c478bd9Sstevel@tonic-gate  * of ires in that table.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate uint_t	ire_srcif_table_count; /* Number of ires in all srcif tables */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * The minimum size of IRE cache table.  It will be recalcuated in
2827c478bd9Sstevel@tonic-gate  * ip_ire_init().
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate uint32_t ip_cache_table_size = IP_CACHE_TABLE_SIZE;
2857c478bd9Sstevel@tonic-gate uint32_t ip6_cache_table_size = IP6_CACHE_TABLE_SIZE;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * The size of the forwarding table.  We will make sure that it is a
2897c478bd9Sstevel@tonic-gate  * power of 2 in ip_ire_init().
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate uint32_t ip_ftable_hash_size = IP_FTABLE_HASH_SIZE;
2927c478bd9Sstevel@tonic-gate uint32_t ip6_ftable_hash_size = IP6_FTABLE_HASH_SIZE;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate struct	kmem_cache	*ire_cache;
2957c478bd9Sstevel@tonic-gate static ire_t	ire_null;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate ire_stats_t ire_stats_v4;	/* IPv4 ire statistics */
2987c478bd9Sstevel@tonic-gate ire_stats_t ire_stats_v6;	/* IPv6 ire statistics */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * The threshold number of IRE in a bucket when the IREs are
3027c478bd9Sstevel@tonic-gate  * cleaned up.  This threshold is calculated later in ip_open()
3037c478bd9Sstevel@tonic-gate  * based on the speed of CPU and available memory.  This default
3047c478bd9Sstevel@tonic-gate  * value is the maximum.
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  * We have two kinds of cached IRE, temporary and
3077c478bd9Sstevel@tonic-gate  * non-temporary.  Temporary IREs are marked with
3087c478bd9Sstevel@tonic-gate  * IRE_MARK_TEMPORARY.  They are IREs created for non
3097c478bd9Sstevel@tonic-gate  * TCP traffic and for forwarding purposes.  All others
3107c478bd9Sstevel@tonic-gate  * are non-temporary IREs.  We don't mark IRE created for
3117c478bd9Sstevel@tonic-gate  * TCP as temporary because TCP is stateful and there are
3127c478bd9Sstevel@tonic-gate  * info stored in the IRE which can be shared by other TCP
3137c478bd9Sstevel@tonic-gate  * connections to the same destination.  For connected
3147c478bd9Sstevel@tonic-gate  * endpoint, we also don't want to mark the IRE used as
3157c478bd9Sstevel@tonic-gate  * temporary because the same IRE will be used frequently,
3167c478bd9Sstevel@tonic-gate  * otherwise, the app should not do a connect().  We change
3177c478bd9Sstevel@tonic-gate  * the marking at ip_bind_connected_*() if necessary.
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  * We want to keep the cache IRE hash bucket length reasonably
3207c478bd9Sstevel@tonic-gate  * short, otherwise IRE lookup functions will take "forever."
3217c478bd9Sstevel@tonic-gate  * We use the "crude" function that the IRE bucket
3227c478bd9Sstevel@tonic-gate  * length should be based on the CPU speed, which is 1 entry
3237c478bd9Sstevel@tonic-gate  * per x MHz, depending on the shift factor ip_ire_cpu_ratio
3247c478bd9Sstevel@tonic-gate  * (n).  This means that with a 750MHz CPU, the max bucket
3257c478bd9Sstevel@tonic-gate  * length can be (750 >> n) entries.
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * Note that this threshold is separate for temp and non-temp
3287c478bd9Sstevel@tonic-gate  * IREs.  This means that the actual bucket length can be
3297c478bd9Sstevel@tonic-gate  * twice as that.  And while we try to keep temporary IRE
3307c478bd9Sstevel@tonic-gate  * length at most at the threshold value, we do not attempt to
3317c478bd9Sstevel@tonic-gate  * make the length for non-temporary IREs fixed, for the
3327c478bd9Sstevel@tonic-gate  * reason stated above.  Instead, we start trying to find
3337c478bd9Sstevel@tonic-gate  * "unused" non-temporary IREs when the bucket length reaches
3347c478bd9Sstevel@tonic-gate  * this threshold and clean them up.
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * We also want to limit the amount of memory used by
3377c478bd9Sstevel@tonic-gate  * IREs.  So if we are allowed to use ~3% of memory (M)
3387c478bd9Sstevel@tonic-gate  * for those IREs, each bucket should not have more than
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * 	M / num of cache bucket / sizeof (ire_t)
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  * Again the above memory uses are separate for temp and
3437c478bd9Sstevel@tonic-gate  * non-temp cached IREs.
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  * We may also want the limit to be a function of the number
3467c478bd9Sstevel@tonic-gate  * of interfaces and number of CPUs.  Doing the initialization
3477c478bd9Sstevel@tonic-gate  * in ip_open() means that every time an interface is plumbed,
3487c478bd9Sstevel@tonic-gate  * the max is re-calculated.  Right now, we don't do anything
3497c478bd9Sstevel@tonic-gate  * different.  In future, when we have more experience, we
3507c478bd9Sstevel@tonic-gate  * may want to change this behavior.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate uint32_t ip_ire_max_bucket_cnt = 10;
3537c478bd9Sstevel@tonic-gate uint32_t ip6_ire_max_bucket_cnt = 10;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * The minimum of the temporary IRE bucket count.  We do not want
3577c478bd9Sstevel@tonic-gate  * the length of each bucket to be too short.  This may hurt
3587c478bd9Sstevel@tonic-gate  * performance of some apps as the temporary IREs are removed too
3597c478bd9Sstevel@tonic-gate  * often.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate uint32_t ip_ire_min_bucket_cnt = 3;
3627c478bd9Sstevel@tonic-gate uint32_t ip6_ire_min_bucket_cnt = 3;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * The ratio of memory consumed by IRE used for temporary to available
3667c478bd9Sstevel@tonic-gate  * memory.  This is a shift factor, so 6 means the ratio 1 to 64.  This
3677c478bd9Sstevel@tonic-gate  * value can be changed in /etc/system.  6 is a reasonable number.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate uint32_t ip_ire_mem_ratio = 6;
3707c478bd9Sstevel@tonic-gate /* The shift factor for CPU speed to calculate the max IRE bucket length. */
3717c478bd9Sstevel@tonic-gate uint32_t ip_ire_cpu_ratio = 7;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * The maximum number of buckets in IRE cache table.  In future, we may
3757c478bd9Sstevel@tonic-gate  * want to make it a dynamic hash table.  For the moment, we fix the
3767c478bd9Sstevel@tonic-gate  * size and allocate the table in ip_ire_init() when IP is first loaded.
3777c478bd9Sstevel@tonic-gate  * We take into account the amount of memory a system has.
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate #define	IP_MAX_CACHE_TABLE_SIZE	4096
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate static uint32_t	ip_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
3827c478bd9Sstevel@tonic-gate static uint32_t	ip6_max_cache_table_size = IP_MAX_CACHE_TABLE_SIZE;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate #define	NUM_ILLS	3	/* To build the ILL list to unlock */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /* Zero iulp_t for initialization. */
3877c478bd9Sstevel@tonic-gate const iulp_t	ire_uinfo_null = { 0 };
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate static int	ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
3907c478bd9Sstevel@tonic-gate     ipsq_func_t func);
3917c478bd9Sstevel@tonic-gate static int	ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp,
3927c478bd9Sstevel@tonic-gate     ipsq_func_t func);
3937c478bd9Sstevel@tonic-gate static ire_t	*ire_update_srcif_v4(ire_t *ire);
3947c478bd9Sstevel@tonic-gate static void	ire_delete_v4(ire_t *ire);
3957c478bd9Sstevel@tonic-gate static void	ire_report_ftable(ire_t *ire, char *mp);
3967c478bd9Sstevel@tonic-gate static void	ire_report_ctable(ire_t *ire, char *mp);
3977c478bd9Sstevel@tonic-gate static void	ire_report_mrtun_table(ire_t *ire, char *mp);
3987c478bd9Sstevel@tonic-gate static void	ire_report_srcif_table(ire_t *ire, char *mp);
39945916cd2Sjpk static void	ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers,
4007c478bd9Sstevel@tonic-gate     zoneid_t zoneid);
4017c478bd9Sstevel@tonic-gate static void	ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type,
40245916cd2Sjpk     pfv_t func, void *arg, uchar_t vers, ill_t *ill);
4037c478bd9Sstevel@tonic-gate static	void	ire_walk_ill_tables(uint_t match_flags, uint_t ire_type,
40445916cd2Sjpk     pfv_t func, void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
40545916cd2Sjpk     size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid);
4067c478bd9Sstevel@tonic-gate static void	ire_delete_host_redirects(ipaddr_t gateway);
4077c478bd9Sstevel@tonic-gate static boolean_t ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask,
40845916cd2Sjpk     ipaddr_t gateway, int type, const ipif_t *ipif, zoneid_t zoneid,
40945916cd2Sjpk     uint32_t ihandle, const ts_label_t *tsl, int match_flags);
4107c478bd9Sstevel@tonic-gate static void	ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt);
4117c478bd9Sstevel@tonic-gate extern void	ill_unlock_ills(ill_t **list, int cnt);
4127c478bd9Sstevel@tonic-gate static void	ire_fastpath_list_add(ill_t *ill, ire_t *ire);
4137c478bd9Sstevel@tonic-gate extern void	th_trace_rrecord(th_trace_t *);
4147c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
4157c478bd9Sstevel@tonic-gate static void	ire_trace_inactive(ire_t *);
4167c478bd9Sstevel@tonic-gate #endif
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * To avoid bloating the code, we call this function instead of
4207c478bd9Sstevel@tonic-gate  * using the macro IRE_REFRELE. Use macro only in performance
4217c478bd9Sstevel@tonic-gate  * critical paths.
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  * Must not be called while holding any locks. Otherwise if this is
4247c478bd9Sstevel@tonic-gate  * the last reference to be released there is a chance of recursive mutex
4257c478bd9Sstevel@tonic-gate  * panic due to ire_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4267c478bd9Sstevel@tonic-gate  * to restart an ioctl. The one exception is when the caller is sure that
4277c478bd9Sstevel@tonic-gate  * this is not the last reference to be released. Eg. if the caller is
4287c478bd9Sstevel@tonic-gate  * sure that the ire has not been deleted and won't be deleted.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate void
4317c478bd9Sstevel@tonic-gate ire_refrele(ire_t *ire)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	IRE_REFRELE(ire);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate void
4377c478bd9Sstevel@tonic-gate ire_refrele_notr(ire_t *ire)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * kmem_cache_alloc constructor for IRE in kma space.
4447c478bd9Sstevel@tonic-gate  * Note that when ire_mp is set the IRE is stored in that mblk and
4457c478bd9Sstevel@tonic-gate  * not in this cache.
4467c478bd9Sstevel@tonic-gate  */
4477c478bd9Sstevel@tonic-gate /* ARGSUSED */
4487c478bd9Sstevel@tonic-gate static int
4497c478bd9Sstevel@tonic-gate ip_ire_constructor(void *buf, void *cdrarg, int kmflags)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	ire->ire_fp_mp = NULL;
4547c478bd9Sstevel@tonic-gate 	ire->ire_dlureq_mp = NULL;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (0);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
4607c478bd9Sstevel@tonic-gate static void
4617c478bd9Sstevel@tonic-gate ip_ire_destructor(void *buf, void *cdrarg)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	ire_t	*ire = buf;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_fp_mp == NULL);
4667c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_dlureq_mp == NULL);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_ADVISE_NO_REPLY
4717c478bd9Sstevel@tonic-gate  * IOCTL.  It is used by TCP (or other ULPs) to supply revised information
4727c478bd9Sstevel@tonic-gate  * for an existing CACHED IRE.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate /* ARGSUSED */
4757c478bd9Sstevel@tonic-gate int
4767c478bd9Sstevel@tonic-gate ip_ire_advise(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	uchar_t	*addr_ucp;
4797c478bd9Sstevel@tonic-gate 	ipic_t	*ipic;
4807c478bd9Sstevel@tonic-gate 	ire_t	*ire;
4817c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
4827c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
4837c478bd9Sstevel@tonic-gate 	irb_t	*irb;
4847c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
4877c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
4917c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	if (ioc_cr != NULL && secpolicy_net_config(ioc_cr, B_FALSE) != 0)
4947c478bd9Sstevel@tonic-gate 		return (EPERM);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	ipic = (ipic_t *)mp->b_rptr;
4977c478bd9Sstevel@tonic-gate 	if (!(addr_ucp = mi_offset_param(mp, ipic->ipic_addr_offset,
4987c478bd9Sstevel@tonic-gate 	    ipic->ipic_addr_length))) {
4997c478bd9Sstevel@tonic-gate 		return (EINVAL);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	if (!OK_32PTR(addr_ucp))
5027c478bd9Sstevel@tonic-gate 		return (EINVAL);
5037c478bd9Sstevel@tonic-gate 	switch (ipic->ipic_addr_length) {
5047c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN: {
5057c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
5067c478bd9Sstevel@tonic-gate 		addr = *(ipaddr_t *)addr_ucp;
5077c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
50845916cd2Sjpk 		ire = ire_cache_lookup(addr, zoneid, NULL);
5097c478bd9Sstevel@tonic-gate 		break;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 	case IPV6_ADDR_LEN: {
5127c478bd9Sstevel@tonic-gate 		/* Extract the destination address. */
5137c478bd9Sstevel@tonic-gate 		v6addr = *(in6_addr_t *)addr_ucp;
5147c478bd9Sstevel@tonic-gate 		/* Find the corresponding IRE. */
51545916cd2Sjpk 		ire = ire_cache_lookup_v6(&v6addr, zoneid, NULL);
5167c478bd9Sstevel@tonic-gate 		break;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	default:
5197c478bd9Sstevel@tonic-gate 		return (EINVAL);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (ire == NULL)
5237c478bd9Sstevel@tonic-gate 		return (ENOENT);
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * Update the round trip time estimate and/or the max frag size
5267c478bd9Sstevel@tonic-gate 	 * and/or the slow start threshold.
5277c478bd9Sstevel@tonic-gate 	 *
5287c478bd9Sstevel@tonic-gate 	 * We serialize multiple advises using ire_lock.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
5317c478bd9Sstevel@tonic-gate 	if (ipic->ipic_rtt) {
5327c478bd9Sstevel@tonic-gate 		/*
5337c478bd9Sstevel@tonic-gate 		 * If there is no old cached values, initialize them
5347c478bd9Sstevel@tonic-gate 		 * conservatively.  Set them to be (1.5 * new value).
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt != 0) {
5377c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = (ire->ire_uinfo.iulp_rtt +
5387c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt) >> 1;
5397c478bd9Sstevel@tonic-gate 		} else {
5407c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt = ipic->ipic_rtt +
5417c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt >> 1);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_rtt_sd != 0) {
5447c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd =
5457c478bd9Sstevel@tonic-gate 			    (ire->ire_uinfo.iulp_rtt_sd +
5467c478bd9Sstevel@tonic-gate 			    ipic->ipic_rtt_sd) >> 1;
5477c478bd9Sstevel@tonic-gate 		} else {
5487c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_rtt_sd = ipic->ipic_rtt_sd +
5497c478bd9Sstevel@tonic-gate 			    (ipic->ipic_rtt_sd >> 1);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	if (ipic->ipic_max_frag)
5537c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = MIN(ipic->ipic_max_frag, IP_MAXPACKET);
5547c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ssthresh != 0) {
5557c478bd9Sstevel@tonic-gate 		if (ire->ire_uinfo.iulp_ssthresh != 0)
5567c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh =
5577c478bd9Sstevel@tonic-gate 			    (ipic->ipic_ssthresh +
5587c478bd9Sstevel@tonic-gate 			    ire->ire_uinfo.iulp_ssthresh) >> 1;
5597c478bd9Sstevel@tonic-gate 		else
5607c478bd9Sstevel@tonic-gate 			ire->ire_uinfo.iulp_ssthresh = ipic->ipic_ssthresh;
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Don't need the ire_lock below this. ire_type does not change
5647c478bd9Sstevel@tonic-gate 	 * after initialization. ire_marks is protected by irb_lock.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (ipic->ipic_ire_marks != 0 && ire->ire_type == IRE_CACHE) {
5697c478bd9Sstevel@tonic-gate 		/*
5707c478bd9Sstevel@tonic-gate 		 * Only increment the temporary IRE count if the original
5717c478bd9Sstevel@tonic-gate 		 * IRE is not already marked temporary.
5727c478bd9Sstevel@tonic-gate 		 */
5737c478bd9Sstevel@tonic-gate 		irb = ire->ire_bucket;
5747c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_WRITER);
5757c478bd9Sstevel@tonic-gate 		if ((ipic->ipic_ire_marks & IRE_MARK_TEMPORARY) &&
5767c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_TEMPORARY)) {
5777c478bd9Sstevel@tonic-gate 			irb->irb_tmp_ire_cnt++;
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 		ire->ire_marks |= ipic->ipic_ire_marks;
5807c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
5847c478bd9Sstevel@tonic-gate 	return (0);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * This function is associated with the IP_IOC_IRE_DELETE[_NO_REPLY]
5897c478bd9Sstevel@tonic-gate  * IOCTL[s].  The NO_REPLY form is used by TCP to delete a route IRE
5907c478bd9Sstevel@tonic-gate  * for a host that is not responding.  This will force an attempt to
5917c478bd9Sstevel@tonic-gate  * establish a new route, if available.  Management processes may want
5927c478bd9Sstevel@tonic-gate  * to use the version that generates a reply.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * This function does not support IPv6 since Neighbor Unreachability Detection
5957c478bd9Sstevel@tonic-gate  * means that negative advise like this is useless.
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate /* ARGSUSED */
5987c478bd9Sstevel@tonic-gate int
5997c478bd9Sstevel@tonic-gate ip_ire_delete(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	uchar_t	*addr_ucp;
6027c478bd9Sstevel@tonic-gate 	ipaddr_t	addr;
6037c478bd9Sstevel@tonic-gate 	ire_t	*ire;
6047c478bd9Sstevel@tonic-gate 	ipid_t	*ipid;
6057c478bd9Sstevel@tonic-gate 	boolean_t routing_sock_info = B_FALSE;	/* Sent info? */
6067c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	ASSERT(q->q_next == NULL);
6097c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Check privilege using the ioctl credential; if it is NULL
6137c478bd9Sstevel@tonic-gate 	 * then this is a kernel message and therefor privileged.
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	if (ioc_cr != NULL && secpolicy_net_config(ioc_cr, B_FALSE) != 0)
6167c478bd9Sstevel@tonic-gate 		return (EPERM);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	ipid = (ipid_t *)mp->b_rptr;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/* Only actions on IRE_CACHEs are acceptable at present. */
6217c478bd9Sstevel@tonic-gate 	if (ipid->ipid_ire_type != IRE_CACHE)
6227c478bd9Sstevel@tonic-gate 		return (EINVAL);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	addr_ucp = mi_offset_param(mp, ipid->ipid_addr_offset,
6257c478bd9Sstevel@tonic-gate 		ipid->ipid_addr_length);
6267c478bd9Sstevel@tonic-gate 	if (addr_ucp == NULL || !OK_32PTR(addr_ucp))
6277c478bd9Sstevel@tonic-gate 		return (EINVAL);
6287c478bd9Sstevel@tonic-gate 	switch (ipid->ipid_addr_length) {
6297c478bd9Sstevel@tonic-gate 	case IP_ADDR_LEN:
6307c478bd9Sstevel@tonic-gate 		/* addr_ucp points at IP addr */
6317c478bd9Sstevel@tonic-gate 		break;
6327c478bd9Sstevel@tonic-gate 	case sizeof (sin_t): {
6337c478bd9Sstevel@tonic-gate 		sin_t	*sin;
6347c478bd9Sstevel@tonic-gate 		/*
6357c478bd9Sstevel@tonic-gate 		 * got complete (sockaddr) address - increment addr_ucp to point
6367c478bd9Sstevel@tonic-gate 		 * at the ip_addr field.
6377c478bd9Sstevel@tonic-gate 		 */
6387c478bd9Sstevel@tonic-gate 		sin = (sin_t *)addr_ucp;
6397c478bd9Sstevel@tonic-gate 		addr_ucp = (uchar_t *)&sin->sin_addr.s_addr;
6407c478bd9Sstevel@tonic-gate 		break;
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	default:
6437c478bd9Sstevel@tonic-gate 		return (EINVAL);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	/* Extract the destination address. */
6467c478bd9Sstevel@tonic-gate 	bcopy(addr_ucp, &addr, IP_ADDR_LEN);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/* Try to find the CACHED IRE. */
64945916cd2Sjpk 	ire = ire_cache_lookup(addr, zoneid, NULL);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* Nail it. */
6527c478bd9Sstevel@tonic-gate 	if (ire) {
6537c478bd9Sstevel@tonic-gate 		/* Allow delete only on CACHE entries */
6547c478bd9Sstevel@tonic-gate 		if (ire->ire_type != IRE_CACHE) {
6557c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
6567c478bd9Sstevel@tonic-gate 			return (EINVAL);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * Verify that the IRE has been around for a while.
6617c478bd9Sstevel@tonic-gate 		 * This is to protect against transport protocols
6627c478bd9Sstevel@tonic-gate 		 * that are too eager in sending delete messages.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		if (gethrestime_sec() <
6657c478bd9Sstevel@tonic-gate 		    ire->ire_create_time + ip_ignore_delete_time) {
6667c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
6677c478bd9Sstevel@tonic-gate 			return (EINVAL);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		/*
6707c478bd9Sstevel@tonic-gate 		 * Now we have a potentially dead cache entry. We need
6717c478bd9Sstevel@tonic-gate 		 * to remove it.
6727c478bd9Sstevel@tonic-gate 		 * If this cache entry is generated from a default route,
6737c478bd9Sstevel@tonic-gate 		 * search the default list and mark it dead and some
6747c478bd9Sstevel@tonic-gate 		 * background process will try to activate it.
6757c478bd9Sstevel@tonic-gate 		 */
6767c478bd9Sstevel@tonic-gate 		if ((ire->ire_gateway_addr != 0) && (ire->ire_cmask == 0)) {
6777c478bd9Sstevel@tonic-gate 			/*
6787c478bd9Sstevel@tonic-gate 			 * Make sure that we pick a different
6797c478bd9Sstevel@tonic-gate 			 * IRE_DEFAULT next time.
6807c478bd9Sstevel@tonic-gate 			 * The ip_ire_default_count tracks the number of
6817c478bd9Sstevel@tonic-gate 			 * IRE_DEFAULT entries. However, the
6827c478bd9Sstevel@tonic-gate 			 * ip_forwarding_table[0] also contains
6837c478bd9Sstevel@tonic-gate 			 * interface routes thus the count can be zero.
6847c478bd9Sstevel@tonic-gate 			 */
6857c478bd9Sstevel@tonic-gate 			ire_t *gw_ire;
6867c478bd9Sstevel@tonic-gate 			irb_t *irb_ptr;
6877c478bd9Sstevel@tonic-gate 			irb_t *irb;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			if (((irb_ptr = ip_forwarding_table[0]) != NULL) &&
6907c478bd9Sstevel@tonic-gate 			    (irb = &irb_ptr[0])->irb_ire != NULL &&
6917c478bd9Sstevel@tonic-gate 			    ip_ire_default_count != 0) {
6927c478bd9Sstevel@tonic-gate 				uint_t index;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 				/*
6957c478bd9Sstevel@tonic-gate 				 * We grab it as writer just to serialize
6967c478bd9Sstevel@tonic-gate 				 * multiple threads trying to bump up
6977c478bd9Sstevel@tonic-gate 				 * ip_ire_default_index.
6987c478bd9Sstevel@tonic-gate 				 */
6997c478bd9Sstevel@tonic-gate 				rw_enter(&irb->irb_lock, RW_WRITER);
7007c478bd9Sstevel@tonic-gate 				if ((gw_ire = irb->irb_ire) == NULL) {
7017c478bd9Sstevel@tonic-gate 					rw_exit(&irb->irb_lock);
7027c478bd9Sstevel@tonic-gate 					goto done;
7037c478bd9Sstevel@tonic-gate 				}
7047c478bd9Sstevel@tonic-gate 				index = ip_ire_default_index %
7057c478bd9Sstevel@tonic-gate 				    ip_ire_default_count;
7067c478bd9Sstevel@tonic-gate 				while (index-- && gw_ire->ire_next != NULL)
7077c478bd9Sstevel@tonic-gate 					gw_ire = gw_ire->ire_next;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 				/* Skip past the potentially bad gateway */
7107c478bd9Sstevel@tonic-gate 				if (ire->ire_gateway_addr ==
7117c478bd9Sstevel@tonic-gate 				    gw_ire->ire_gateway_addr)
7127c478bd9Sstevel@tonic-gate 					ip_ire_default_index++;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
7157c478bd9Sstevel@tonic-gate 		    }
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate done:
7187c478bd9Sstevel@tonic-gate 		/* report the bad route to routing sockets */
7197c478bd9Sstevel@tonic-gate 		ip_rts_change(RTM_LOSING, ire->ire_addr, ire->ire_gateway_addr,
7207c478bd9Sstevel@tonic-gate 		    ire->ire_mask, ire->ire_src_addr, 0, 0, 0,
7217c478bd9Sstevel@tonic-gate 		    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA));
7227c478bd9Sstevel@tonic-gate 		routing_sock_info = B_TRUE;
7237c478bd9Sstevel@tonic-gate 		ire_delete(ire);
7247c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	/* Also look for an IRE_HOST_REDIRECT and remove it if present */
7277c478bd9Sstevel@tonic-gate 	ire = ire_route_lookup(addr, 0, 0, IRE_HOST_REDIRECT, NULL, NULL,
72845916cd2Sjpk 	    ALL_ZONES, NULL, MATCH_IRE_TYPE);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/* Nail it. */
7317c478bd9Sstevel@tonic-gate 	if (ire) {
7327c478bd9Sstevel@tonic-gate 		if (!routing_sock_info) {
7337c478bd9Sstevel@tonic-gate 			ip_rts_change(RTM_LOSING, ire->ire_addr,
7347c478bd9Sstevel@tonic-gate 			    ire->ire_gateway_addr, ire->ire_mask,
7357c478bd9Sstevel@tonic-gate 			    ire->ire_src_addr, 0, 0, 0,
7367c478bd9Sstevel@tonic-gate 			    (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFA));
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 		ire_delete(ire);
7397c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	return (0);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * Named Dispatch routine to produce a formatted report on all IREs.
7467c478bd9Sstevel@tonic-gate  * This report is accessed by using the ndd utility to "get" ND variable
7477c478bd9Sstevel@tonic-gate  * "ipv4_ire_status".
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate /* ARGSUSED */
7507c478bd9Sstevel@tonic-gate int
7517c478bd9Sstevel@tonic-gate ip_ire_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
7567c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
7577c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7587c478bd9Sstevel@tonic-gate 	    "rfq      " MI_COL_HDRPAD_STR
7597c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7607c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
7617c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
7627c478bd9Sstevel@tonic-gate 	    " zone "
7637c478bd9Sstevel@tonic-gate 	/*   12345 */
7647c478bd9Sstevel@tonic-gate 	    "addr            mask            "
7657c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 */
7667c478bd9Sstevel@tonic-gate 	    "src             gateway         mxfrg rtt   rtt_sd ssthresh ref "
7677c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 123.123.123.123 12345 12345 123456 12345678 123 */
7687c478bd9Sstevel@tonic-gate 	    "rtomax tstamp_ok wscale_ok ecn_ok pmtud_ok sack sendpipe "
7697c478bd9Sstevel@tonic-gate 	/*   123456 123456789 123456789 123456 12345678 1234 12345678 */
7707c478bd9Sstevel@tonic-gate 	    "recvpipe in/out/forward type");
7717c478bd9Sstevel@tonic-gate 	/*   12345678 in/out/forward xxxxxxxxxx */
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * Because of the ndd constraint, at most we can have 64K buffer
7757c478bd9Sstevel@tonic-gate 	 * to put in all IRE info.  So to be more efficient, just
7767c478bd9Sstevel@tonic-gate 	 * allocate a 64K buffer here, assuming we need that large buffer.
7777c478bd9Sstevel@tonic-gate 	 * This should be OK as only root can do ndd /dev/ip.
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
7807c478bd9Sstevel@tonic-gate 		/* The following may work even if we cannot get a large buf. */
7817c478bd9Sstevel@tonic-gate 		(void) mi_mpprintf(mp, "<< Out of buffer >>\n");
7827c478bd9Sstevel@tonic-gate 		return (0);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7867c478bd9Sstevel@tonic-gate 	if (zoneid == GLOBAL_ZONEID)
7877c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
7887c478bd9Sstevel@tonic-gate 
78945916cd2Sjpk 	ire_walk_v4(ire_report_ftable, mp->b_cont, zoneid);
79045916cd2Sjpk 	ire_walk_v4(ire_report_ctable, mp->b_cont, zoneid);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	return (0);
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate /* ire_walk routine invoked for ip_ire_report for each IRE. */
7967c478bd9Sstevel@tonic-gate static void
7977c478bd9Sstevel@tonic-gate ire_report_ftable(ire_t *ire, char *mp)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 	char	buf1[16];
8007c478bd9Sstevel@tonic-gate 	char	buf2[16];
8017c478bd9Sstevel@tonic-gate 	char	buf3[16];
8027c478bd9Sstevel@tonic-gate 	char	buf4[16];
8037c478bd9Sstevel@tonic-gate 	uint_t	fo_pkt_count;
8047c478bd9Sstevel@tonic-gate 	uint_t	ib_pkt_count;
8057c478bd9Sstevel@tonic-gate 	int	ref;
8067c478bd9Sstevel@tonic-gate 	uint_t	print_len, buf_len;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_CACHETABLE)
8097c478bd9Sstevel@tonic-gate 	    return;
8107c478bd9Sstevel@tonic-gate 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
8117c478bd9Sstevel@tonic-gate 	if (buf_len <= 0)
8127c478bd9Sstevel@tonic-gate 		return;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
8157c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
8167c478bd9Sstevel@tonic-gate 	/* "inbound" to a non local address is a forward */
8177c478bd9Sstevel@tonic-gate 	ib_pkt_count = ire->ire_ib_pkt_count;
8187c478bd9Sstevel@tonic-gate 	fo_pkt_count = 0;
8197c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
8207c478bd9Sstevel@tonic-gate 		fo_pkt_count = ib_pkt_count;
8217c478bd9Sstevel@tonic-gate 		ib_pkt_count = 0;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
8247c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
8257c478bd9Sstevel@tonic-gate 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
8267c478bd9Sstevel@tonic-gate 	    "%04d %08d %08d %d/%d/%d %s\n",
8277c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
8287c478bd9Sstevel@tonic-gate 	    (int)ire->ire_zoneid,
8297c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
8307c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_src_addr, buf3),
8317c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
8327c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
8337c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtt_sd,
8347c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_ssthresh, ref,
8357c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtomax,
8367c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
8377c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
8387c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
8397c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
8407c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_sack,
8417c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
8427c478bd9Sstevel@tonic-gate 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
8437c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
8447c478bd9Sstevel@tonic-gate 	if (print_len < buf_len) {
8457c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += print_len;
8467c478bd9Sstevel@tonic-gate 	} else {
8477c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += buf_len;
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate /* ire_walk routine invoked for ip_ire_report for each cached IRE. */
8527c478bd9Sstevel@tonic-gate static void
8537c478bd9Sstevel@tonic-gate ire_report_ctable(ire_t *ire, char *mp)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	char	buf1[16];
8567c478bd9Sstevel@tonic-gate 	char	buf2[16];
8577c478bd9Sstevel@tonic-gate 	char	buf3[16];
8587c478bd9Sstevel@tonic-gate 	char	buf4[16];
8597c478bd9Sstevel@tonic-gate 	uint_t	fo_pkt_count;
8607c478bd9Sstevel@tonic-gate 	uint_t	ib_pkt_count;
8617c478bd9Sstevel@tonic-gate 	int	ref;
8627c478bd9Sstevel@tonic-gate 	uint_t	print_len, buf_len;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHETABLE) == 0)
8657c478bd9Sstevel@tonic-gate 	    return;
8667c478bd9Sstevel@tonic-gate 	buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr;
8677c478bd9Sstevel@tonic-gate 	if (buf_len <= 0)
8687c478bd9Sstevel@tonic-gate 		return;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
8717c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
8727c478bd9Sstevel@tonic-gate 	/* "inbound" to a non local address is a forward */
8737c478bd9Sstevel@tonic-gate 	ib_pkt_count = ire->ire_ib_pkt_count;
8747c478bd9Sstevel@tonic-gate 	fo_pkt_count = 0;
8757c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
8767c478bd9Sstevel@tonic-gate 		fo_pkt_count = ib_pkt_count;
8777c478bd9Sstevel@tonic-gate 		ib_pkt_count = 0;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 	print_len =  snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len,
8807c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
8817c478bd9Sstevel@tonic-gate 	    "%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
8827c478bd9Sstevel@tonic-gate 	    "%04d %08d %08d %d/%d/%d %s\n",
8837c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
8847c478bd9Sstevel@tonic-gate 	    (int)ire->ire_zoneid,
8857c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
8867c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_src_addr, buf3),
8877c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf4),
8887c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
8897c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtt_sd, ire->ire_uinfo.iulp_ssthresh, ref,
8907c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_rtomax,
8917c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
8927c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
8937c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
8947c478bd9Sstevel@tonic-gate 	    (ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
8957c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_sack,
8967c478bd9Sstevel@tonic-gate 	    ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
8977c478bd9Sstevel@tonic-gate 	    ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
8987c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
8997c478bd9Sstevel@tonic-gate 	if (print_len < buf_len) {
9007c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += print_len;
9017c478bd9Sstevel@tonic-gate 	} else {
9027c478bd9Sstevel@tonic-gate 		((mblk_t *)mp)->b_wptr += buf_len;
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /* ARGSUSED */
9077c478bd9Sstevel@tonic-gate int
9087c478bd9Sstevel@tonic-gate ip_ire_report_mrtun(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
9117c478bd9Sstevel@tonic-gate 	"IRE      " MI_COL_HDRPAD_STR
9127c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
9137c478bd9Sstevel@tonic-gate 	"stq      " MI_COL_HDRPAD_STR
9147c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
9157c478bd9Sstevel@tonic-gate 	"in_ill    " MI_COL_HDRPAD_STR
9167c478bd9Sstevel@tonic-gate 	/*   01234567[89ABCDEF] */
9177c478bd9Sstevel@tonic-gate 	"in_src_addr            "
9187c478bd9Sstevel@tonic-gate 	/*   123.123.123.123 */
9197c478bd9Sstevel@tonic-gate 	"max_frag      "
9207c478bd9Sstevel@tonic-gate 	/*   12345 */
9217c478bd9Sstevel@tonic-gate 	"ref     ");
9227c478bd9Sstevel@tonic-gate 	/*   123 */
9237c478bd9Sstevel@tonic-gate 
92445916cd2Sjpk 	ire_walk_ill_mrtun(0, 0, ire_report_mrtun_table, mp, NULL);
9257c478bd9Sstevel@tonic-gate 	return (0);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate /* mrtun report table - supports ipv4_mrtun_ire_status ndd variable */
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate static void
9317c478bd9Sstevel@tonic-gate ire_report_mrtun_table(ire_t *ire, char *mp)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	char	buf1[INET_ADDRSTRLEN];
9347c478bd9Sstevel@tonic-gate 	int	ref;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/* Number of active references of this ire */
9377c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
9387c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type == IRE_MIPRTUN);
9397c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf((mblk_t *)mp,
9407c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
9417c478bd9Sstevel@tonic-gate 	    "%s          %05d             %03d",
9427c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_stq,
9437c478bd9Sstevel@tonic-gate 	    (void *)ire->ire_in_ill,
9447c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_in_src_addr, buf1),
9457c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ref);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * Dispatch routine to format ires in interface based routine
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate /* ARGSUSED */
9527c478bd9Sstevel@tonic-gate int
9537c478bd9Sstevel@tonic-gate ip_ire_report_srcif(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/* Report all interface based ires */
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf(mp,
9597c478bd9Sstevel@tonic-gate 	    "IRE      " MI_COL_HDRPAD_STR
9607c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
9617c478bd9Sstevel@tonic-gate 	    "stq      " MI_COL_HDRPAD_STR
9627c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
9637c478bd9Sstevel@tonic-gate 	    "in_ill    " MI_COL_HDRPAD_STR
9647c478bd9Sstevel@tonic-gate 	    /*   01234567[89ABCDEF] */
9657c478bd9Sstevel@tonic-gate 	    "addr            "
9667c478bd9Sstevel@tonic-gate 	    /*   123.123.123.123 */
9677c478bd9Sstevel@tonic-gate 	    "gateway         "
9687c478bd9Sstevel@tonic-gate 	    /*   123.123.123.123 */
9697c478bd9Sstevel@tonic-gate 	    "max_frag      "
9707c478bd9Sstevel@tonic-gate 	    /*   12345 */
9717c478bd9Sstevel@tonic-gate 	    "ref     "
9727c478bd9Sstevel@tonic-gate 	    /*   123 */
9737c478bd9Sstevel@tonic-gate 	    "type    "
9747c478bd9Sstevel@tonic-gate 	    /* ABCDEFGH */
9757c478bd9Sstevel@tonic-gate 	    "in/out/forward");
97645916cd2Sjpk 	ire_walk_srcif_table_v4(ire_report_srcif_table, mp);
9777c478bd9Sstevel@tonic-gate 	return (0);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /* Reports the interface table ires */
9817c478bd9Sstevel@tonic-gate static void
9827c478bd9Sstevel@tonic-gate ire_report_srcif_table(ire_t *ire, char *mp)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	char    buf1[INET_ADDRSTRLEN];
9857c478bd9Sstevel@tonic-gate 	char    buf2[INET_ADDRSTRLEN];
9867c478bd9Sstevel@tonic-gate 	int	ref;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	ref = ire->ire_refcnt;
9897c478bd9Sstevel@tonic-gate 	(void) mi_mpprintf((mblk_t *)mp,
9907c478bd9Sstevel@tonic-gate 	    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
9917c478bd9Sstevel@tonic-gate 	    "%s    %s      %05d       %03d      %s     %d",
9927c478bd9Sstevel@tonic-gate 	    (void *)ire, (void *)ire->ire_stq,
9937c478bd9Sstevel@tonic-gate 	    (void *)ire->ire_in_ill,
9947c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_addr, buf1),
9957c478bd9Sstevel@tonic-gate 	    ip_dot_addr(ire->ire_gateway_addr, buf2),
9967c478bd9Sstevel@tonic-gate 	    ire->ire_max_frag, ref,
9977c478bd9Sstevel@tonic-gate 	    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type),
9987c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * ip_ire_req is called by ip_wput when an IRE_DB_REQ_TYPE message is handed
10037c478bd9Sstevel@tonic-gate  * down from the Upper Level Protocol to request a copy of the IRE (to check
10047c478bd9Sstevel@tonic-gate  * its type or to extract information like round-trip time estimates or the
10057c478bd9Sstevel@tonic-gate  * MTU.)
10067c478bd9Sstevel@tonic-gate  * The address is assumed to be in the ire_addr field. If no IRE is found
10077c478bd9Sstevel@tonic-gate  * an IRE is returned with ire_type being zero.
10087c478bd9Sstevel@tonic-gate  * Note that the upper lavel protocol has to check for broadcast
10097c478bd9Sstevel@tonic-gate  * (IRE_BROADCAST) and multicast (CLASSD(addr)).
10107c478bd9Sstevel@tonic-gate  * If there is a b_cont the resulting IRE_DB_TYPE mblk is placed at the
10117c478bd9Sstevel@tonic-gate  * end of the returned message.
10127c478bd9Sstevel@tonic-gate  *
10137c478bd9Sstevel@tonic-gate  * TCP sends down a message of this type with a connection request packet
10147c478bd9Sstevel@tonic-gate  * chained on. UDP and ICMP send it down to verify that a route exists for
10157c478bd9Sstevel@tonic-gate  * the destination address when they get connected.
10167c478bd9Sstevel@tonic-gate  */
10177c478bd9Sstevel@tonic-gate void
10187c478bd9Sstevel@tonic-gate ip_ire_req(queue_t *q, mblk_t *mp)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	ire_t	*inire;
10217c478bd9Sstevel@tonic-gate 	ire_t	*ire;
10227c478bd9Sstevel@tonic-gate 	mblk_t	*mp1;
10237c478bd9Sstevel@tonic-gate 	ire_t	*sire = NULL;
10247c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (ire_t) ||
10277c478bd9Sstevel@tonic-gate 	    !OK_32PTR(mp->b_rptr)) {
10287c478bd9Sstevel@tonic-gate 		freemsg(mp);
10297c478bd9Sstevel@tonic-gate 		return;
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 	inire = (ire_t *)mp->b_rptr;
10327c478bd9Sstevel@tonic-gate 	/*
10337c478bd9Sstevel@tonic-gate 	 * Got it, now take our best shot at an IRE.
10347c478bd9Sstevel@tonic-gate 	 */
10357c478bd9Sstevel@tonic-gate 	if (inire->ire_ipversion == IPV6_VERSION) {
10367c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup_v6(&inire->ire_addr_v6, 0, 0, 0,
103745916cd2Sjpk 		    NULL, &sire, zoneid, NULL,
10387c478bd9Sstevel@tonic-gate 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT));
10397c478bd9Sstevel@tonic-gate 	} else {
10407c478bd9Sstevel@tonic-gate 		ASSERT(inire->ire_ipversion == IPV4_VERSION);
10417c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(inire->ire_addr, 0, 0, 0,
104245916cd2Sjpk 		    NULL, &sire, zoneid, NULL,
10437c478bd9Sstevel@tonic-gate 		    (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT));
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/*
10477c478bd9Sstevel@tonic-gate 	 * We prevent returning IRES with source address INADDR_ANY
10487c478bd9Sstevel@tonic-gate 	 * as these were temporarily created for sending packets
10497c478bd9Sstevel@tonic-gate 	 * from endpoints that have conn_unspec_src set.
10507c478bd9Sstevel@tonic-gate 	 */
10517c478bd9Sstevel@tonic-gate 	if (ire == NULL ||
10527c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV4_VERSION &&
10537c478bd9Sstevel@tonic-gate 	    ire->ire_src_addr == INADDR_ANY) ||
10547c478bd9Sstevel@tonic-gate 	    (ire->ire_ipversion == IPV6_VERSION &&
10557c478bd9Sstevel@tonic-gate 	    IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6))) {
10567c478bd9Sstevel@tonic-gate 		inire->ire_type = 0;
10577c478bd9Sstevel@tonic-gate 	} else {
10587c478bd9Sstevel@tonic-gate 		bcopy(ire, inire, sizeof (ire_t));
10597c478bd9Sstevel@tonic-gate 		/* Copy the route metrics from the parent. */
10607c478bd9Sstevel@tonic-gate 		if (sire != NULL) {
10617c478bd9Sstevel@tonic-gate 			bcopy(&(sire->ire_uinfo), &(inire->ire_uinfo),
10627c478bd9Sstevel@tonic-gate 			    sizeof (iulp_t));
10637c478bd9Sstevel@tonic-gate 		}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		/*
10667c478bd9Sstevel@tonic-gate 		 * As we don't lookup global policy here, we may not
10677c478bd9Sstevel@tonic-gate 		 * pass the right size if per-socket policy is not
10687c478bd9Sstevel@tonic-gate 		 * present. For these cases, path mtu discovery will
10697c478bd9Sstevel@tonic-gate 		 * do the right thing.
10707c478bd9Sstevel@tonic-gate 		 */
10717c478bd9Sstevel@tonic-gate 		inire->ire_ipsec_overhead = conn_ipsec_length(Q_TO_CONN(q));
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		/* Pass the latest setting of the ip_path_mtu_discovery */
10747c478bd9Sstevel@tonic-gate 		inire->ire_frag_flag |= (ip_path_mtu_discovery) ? IPH_DF : 0;
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 	if (ire != NULL)
10777c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
10787c478bd9Sstevel@tonic-gate 	if (sire != NULL)
10797c478bd9Sstevel@tonic-gate 		ire_refrele(sire);
10807c478bd9Sstevel@tonic-gate 	mp->b_wptr = &mp->b_rptr[sizeof (ire_t)];
10817c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/* Put the IRE_DB_TYPE mblk last in the chain */
10847c478bd9Sstevel@tonic-gate 	mp1 = mp->b_cont;
10857c478bd9Sstevel@tonic-gate 	if (mp1 != NULL) {
10867c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
10877c478bd9Sstevel@tonic-gate 		linkb(mp1, mp);
10887c478bd9Sstevel@tonic-gate 		mp = mp1;
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate 	qreply(q, mp);
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate /*
10947c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
10957c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
10967c478bd9Sstevel@tonic-gate  * the packet has been sent.
10977c478bd9Sstevel@tonic-gate  */
10987c478bd9Sstevel@tonic-gate static void
10997c478bd9Sstevel@tonic-gate ire_send(queue_t *q, mblk_t *pkt, ire_t *ire)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11027c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
11037c478bd9Sstevel@tonic-gate 	boolean_t is_secure;
11047c478bd9Sstevel@tonic-gate 	uint_t ifindex;
11057c478bd9Sstevel@tonic-gate 	ill_t	*ill;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
11087c478bd9Sstevel@tonic-gate 	ipsec_mp = pkt;
11097c478bd9Sstevel@tonic-gate 	is_secure = (pkt->b_datap->db_type == M_CTL);
11107c478bd9Sstevel@tonic-gate 	if (is_secure)
11117c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
11147c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
11157c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
11167c478bd9Sstevel@tonic-gate 		/*
11177c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_noire).
11187c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
11197c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from ARP)
11207c478bd9Sstevel@tonic-gate 		 */
11217c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
11227c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
11237c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
11247c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
11257c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
11267c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
11277c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
11287c478bd9Sstevel@tonic-gate 			return;
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
11317c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
11327c478bd9Sstevel@tonic-gate 		mp = allocb(0, BPRI_HI);
11337c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
11347c478bd9Sstevel@tonic-gate 			ill_refrele(ill);
11357c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
11367c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
11377c478bd9Sstevel@tonic-gate 			return;
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_BREAK;
11407c478bd9Sstevel@tonic-gate 		/*
11417c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
11427c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
11437c478bd9Sstevel@tonic-gate 		 * prepended.
11447c478bd9Sstevel@tonic-gate 		 */
11457c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
11467c478bd9Sstevel@tonic-gate 		mp->b_cont = ipsec_mp;
11477c478bd9Sstevel@tonic-gate 		put(q, mp);
11487c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
11497c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
11507c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
11517c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
11527c478bd9Sstevel@tonic-gate 		/*
11537c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
11547c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
11557c478bd9Sstevel@tonic-gate 		 */
11567c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
11577c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, ipsec_mp, NULL);
11587c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
11597c478bd9Sstevel@tonic-gate 	} else {
11607c478bd9Sstevel@tonic-gate 		/* Locally originated packets */
11617c478bd9Sstevel@tonic-gate 		boolean_t is_inaddr_any;
11627c478bd9Sstevel@tonic-gate 		ipha_t *ipha = (ipha_t *)pkt->b_rptr;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		/*
11657c478bd9Sstevel@tonic-gate 		 * We need to do an ire_delete below for which
11667c478bd9Sstevel@tonic-gate 		 * we need to make sure that the IRE will be
11677c478bd9Sstevel@tonic-gate 		 * around even after calling ip_wput_ire -
11687c478bd9Sstevel@tonic-gate 		 * which does ire_refrele. Otherwise somebody
11697c478bd9Sstevel@tonic-gate 		 * could potentially delete this ire and hence
11707c478bd9Sstevel@tonic-gate 		 * free this ire and we will be calling ire_delete
11717c478bd9Sstevel@tonic-gate 		 * on a freed ire below.
11727c478bd9Sstevel@tonic-gate 		 */
11737c478bd9Sstevel@tonic-gate 		is_inaddr_any = (ire->ire_src_addr == INADDR_ANY);
11747c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
11757c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 		/*
11787c478bd9Sstevel@tonic-gate 		 * If we were resolving a router we can not use the
11797c478bd9Sstevel@tonic-gate 		 * routers IRE for sending the packet (since it would
11807c478bd9Sstevel@tonic-gate 		 * violate the uniqness of the IP idents) thus we
11817c478bd9Sstevel@tonic-gate 		 * make another pass through ip_wput to create the IRE_CACHE
11827c478bd9Sstevel@tonic-gate 		 * for the destination.
11837c478bd9Sstevel@tonic-gate 		 * When IRE_MARK_NOADD is set, ire_add() is not called.
11847c478bd9Sstevel@tonic-gate 		 * Thus ip_wput() will never find a ire and result in an
11857c478bd9Sstevel@tonic-gate 		 * infinite loop. Thus we check whether IRE_MARK_NOADD is
11867c478bd9Sstevel@tonic-gate 		 * is set. This also implies that IRE_MARK_NOADD can only be
11877c478bd9Sstevel@tonic-gate 		 * used to send packets to directly connected hosts.
11887c478bd9Sstevel@tonic-gate 		 */
11897c478bd9Sstevel@tonic-gate 		if (ipha->ipha_dst != ire->ire_addr &&
11907c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_NOADD)) {
11917c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
11927c478bd9Sstevel@tonic-gate 			(void) ip_output(Q_TO_CONN(q), ipsec_mp, q, IRE_SEND);
11937c478bd9Sstevel@tonic-gate 		} else {
11947c478bd9Sstevel@tonic-gate 			if (is_secure) {
11957c478bd9Sstevel@tonic-gate 				ipsec_out_t *oi;
11967c478bd9Sstevel@tonic-gate 				ipha_t *ipha;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 				oi = (ipsec_out_t *)ipsec_mp->b_rptr;
11997c478bd9Sstevel@tonic-gate 				ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr;
12007c478bd9Sstevel@tonic-gate 				if (oi->ipsec_out_proc_begin) {
12017c478bd9Sstevel@tonic-gate 					/*
12027c478bd9Sstevel@tonic-gate 					 * This is the case where
12037c478bd9Sstevel@tonic-gate 					 * ip_wput_ipsec_out could not find
12047c478bd9Sstevel@tonic-gate 					 * the IRE and recreated a new one.
12057c478bd9Sstevel@tonic-gate 					 * As ip_wput_ipsec_out does ire
12067c478bd9Sstevel@tonic-gate 					 * lookups, ire_refrele for the extra
12077c478bd9Sstevel@tonic-gate 					 * bump in ire_add.
12087c478bd9Sstevel@tonic-gate 					 */
12097c478bd9Sstevel@tonic-gate 					ire_refrele(ire);
12107c478bd9Sstevel@tonic-gate 					ip_wput_ipsec_out(q, ipsec_mp, ipha,
12117c478bd9Sstevel@tonic-gate 					    NULL, NULL);
12127c478bd9Sstevel@tonic-gate 				} else {
12137c478bd9Sstevel@tonic-gate 					/*
12147c478bd9Sstevel@tonic-gate 					 * IRE_REFRELE will be done in
12157c478bd9Sstevel@tonic-gate 					 * ip_wput_ire.
12167c478bd9Sstevel@tonic-gate 					 */
12177c478bd9Sstevel@tonic-gate 					ip_wput_ire(q, ipsec_mp, ire, NULL,
12187c478bd9Sstevel@tonic-gate 					    IRE_SEND);
12197c478bd9Sstevel@tonic-gate 				}
12207c478bd9Sstevel@tonic-gate 			} else {
12217c478bd9Sstevel@tonic-gate 				/*
12227c478bd9Sstevel@tonic-gate 				 * IRE_REFRELE will be done in ip_wput_ire.
12237c478bd9Sstevel@tonic-gate 				 */
12247c478bd9Sstevel@tonic-gate 				ip_wput_ire(q, ipsec_mp, ire, NULL,
12257c478bd9Sstevel@tonic-gate 				    IRE_SEND);
12267c478bd9Sstevel@tonic-gate 			}
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 		/*
12297c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
12307c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
12317c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
12327c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
12337c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
12347c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
12357c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
12367c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append, ip_ire_req and ip_bind_connected.
12377c478bd9Sstevel@tonic-gate 		 * Though, this does not completely prevent other threads
12387c478bd9Sstevel@tonic-gate 		 * from using this ire, this should not cause any problems.
12397c478bd9Sstevel@tonic-gate 		 *
12407c478bd9Sstevel@tonic-gate 		 * NOTE : We use is_inaddr_any instead of using ire_src_addr
12417c478bd9Sstevel@tonic-gate 		 * because for the normal case i.e !is_inaddr_any, ire_refrele
12427c478bd9Sstevel@tonic-gate 		 * above could have potentially freed the ire.
12437c478bd9Sstevel@tonic-gate 		 */
12447c478bd9Sstevel@tonic-gate 		if (is_inaddr_any) {
12457c478bd9Sstevel@tonic-gate 			/*
12467c478bd9Sstevel@tonic-gate 			 * If this IRE has been deleted by another thread, then
12477c478bd9Sstevel@tonic-gate 			 * ire_bucket won't be NULL, but ire_ptpn will be NULL.
12487c478bd9Sstevel@tonic-gate 			 * Thus, ire_delete will do nothing.  This check
12497c478bd9Sstevel@tonic-gate 			 * guards against calling ire_delete when the IRE was
12507c478bd9Sstevel@tonic-gate 			 * never inserted in the table, which is handled by
12517c478bd9Sstevel@tonic-gate 			 * ire_delete as dropping another reference.
12527c478bd9Sstevel@tonic-gate 			 */
12537c478bd9Sstevel@tonic-gate 			if (ire->ire_bucket != NULL) {
12547c478bd9Sstevel@tonic-gate 				ip1dbg(("ire_send: delete IRE\n"));
12557c478bd9Sstevel@tonic-gate 				ire_delete(ire);
12567c478bd9Sstevel@tonic-gate 			}
12577c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held above */
12587c478bd9Sstevel@tonic-gate 		}
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate  * Send a packet using the specified IRE.
12647c478bd9Sstevel@tonic-gate  * If ire_src_addr_v6 is all zero then discard the IRE after
12657c478bd9Sstevel@tonic-gate  * the packet has been sent.
12667c478bd9Sstevel@tonic-gate  */
12677c478bd9Sstevel@tonic-gate static void
12687c478bd9Sstevel@tonic-gate ire_send_v6(queue_t *q, mblk_t *pkt, ire_t *ire)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	mblk_t *ipsec_mp;
12717c478bd9Sstevel@tonic-gate 	boolean_t secure;
12727c478bd9Sstevel@tonic-gate 	uint_t ifindex;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV6_VERSION);
12757c478bd9Sstevel@tonic-gate 	if (pkt->b_datap->db_type == M_CTL) {
12767c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
12777c478bd9Sstevel@tonic-gate 		pkt = pkt->b_cont;
12787c478bd9Sstevel@tonic-gate 		secure = B_TRUE;
12797c478bd9Sstevel@tonic-gate 	} else {
12807c478bd9Sstevel@tonic-gate 		ipsec_mp = pkt;
12817c478bd9Sstevel@tonic-gate 		secure = B_FALSE;
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	/* If the packet originated externally then */
12857c478bd9Sstevel@tonic-gate 	if (pkt->b_prev) {
12867c478bd9Sstevel@tonic-gate 		ill_t	*ill;
12877c478bd9Sstevel@tonic-gate 		/*
12887c478bd9Sstevel@tonic-gate 		 * Extract the ifindex from b_prev (set in ip_rput_data_v6).
12897c478bd9Sstevel@tonic-gate 		 * Look up interface to see if it still exists (it could have
12907c478bd9Sstevel@tonic-gate 		 * been unplumbed by the time the reply came back from the
12917c478bd9Sstevel@tonic-gate 		 * resolver). Unlike IPv4 there is no need for a prepended
12927c478bd9Sstevel@tonic-gate 		 * M_BREAK since ip_rput_data_v6 does not process options
12937c478bd9Sstevel@tonic-gate 		 * before finding an IRE.
12947c478bd9Sstevel@tonic-gate 		 */
12957c478bd9Sstevel@tonic-gate 		ifindex = (uint_t)(uintptr_t)pkt->b_prev;
12967c478bd9Sstevel@tonic-gate 		ill = ill_lookup_on_ifindex(ifindex, B_TRUE,
12977c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL);
12987c478bd9Sstevel@tonic-gate 		if (ill == NULL) {
12997c478bd9Sstevel@tonic-gate 			pkt->b_prev = NULL;
13007c478bd9Sstevel@tonic-gate 			pkt->b_next = NULL;
13017c478bd9Sstevel@tonic-gate 			freemsg(ipsec_mp);
13027c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add */
13037c478bd9Sstevel@tonic-gate 			return;
13047c478bd9Sstevel@tonic-gate 		}
13057c478bd9Sstevel@tonic-gate 		q = ill->ill_rq;
13067c478bd9Sstevel@tonic-gate 		pkt->b_prev = NULL;
13077c478bd9Sstevel@tonic-gate 		/*
13087c478bd9Sstevel@tonic-gate 		 * This packet has not gone through IPSEC processing
13097c478bd9Sstevel@tonic-gate 		 * and hence we should not have any IPSEC message
13107c478bd9Sstevel@tonic-gate 		 * prepended.
13117c478bd9Sstevel@tonic-gate 		 */
13127c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
13137c478bd9Sstevel@tonic-gate 		put(q, pkt);
13147c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
13157c478bd9Sstevel@tonic-gate 	} else if (pkt->b_next) {
13167c478bd9Sstevel@tonic-gate 		/* Packets from multicast router */
13177c478bd9Sstevel@tonic-gate 		pkt->b_next = NULL;
13187c478bd9Sstevel@tonic-gate 		/*
13197c478bd9Sstevel@tonic-gate 		 * We never get the IPSEC_OUT while forwarding the
13207c478bd9Sstevel@tonic-gate 		 * packet for multicast router.
13217c478bd9Sstevel@tonic-gate 		 */
13227c478bd9Sstevel@tonic-gate 		ASSERT(ipsec_mp == pkt);
13237c478bd9Sstevel@tonic-gate 		/*
13247c478bd9Sstevel@tonic-gate 		 * XXX TODO IPv6.
13257c478bd9Sstevel@tonic-gate 		 */
13267c478bd9Sstevel@tonic-gate 		freemsg(pkt);
13277c478bd9Sstevel@tonic-gate #ifdef XXX
13287c478bd9Sstevel@tonic-gate 		ip_rput_forward(ire, (ipha_t *)pkt->b_rptr, pkt, NULL);
13297c478bd9Sstevel@tonic-gate #endif
13307c478bd9Sstevel@tonic-gate 	} else {
13317c478bd9Sstevel@tonic-gate 		if (secure) {
13327c478bd9Sstevel@tonic-gate 			ipsec_out_t *oi;
13337c478bd9Sstevel@tonic-gate 			ip6_t *ip6h;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 			oi = (ipsec_out_t *)ipsec_mp->b_rptr;
13367c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)ipsec_mp->b_cont->b_rptr;
13377c478bd9Sstevel@tonic-gate 			if (oi->ipsec_out_proc_begin) {
13387c478bd9Sstevel@tonic-gate 				/*
13397c478bd9Sstevel@tonic-gate 				 * This is the case where
13407c478bd9Sstevel@tonic-gate 				 * ip_wput_ipsec_out could not find
13417c478bd9Sstevel@tonic-gate 				 * the IRE and recreated a new one.
13427c478bd9Sstevel@tonic-gate 				 */
13437c478bd9Sstevel@tonic-gate 				ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h,
13447c478bd9Sstevel@tonic-gate 				    NULL, NULL);
13457c478bd9Sstevel@tonic-gate 			} else {
13467c478bd9Sstevel@tonic-gate 				(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp,
13477c478bd9Sstevel@tonic-gate 				    q, IRE_SEND);
13487c478bd9Sstevel@tonic-gate 			}
13497c478bd9Sstevel@tonic-gate 		} else {
13507c478bd9Sstevel@tonic-gate 			/*
13517c478bd9Sstevel@tonic-gate 			 * Send packets through ip_output_v6 so that any
13527c478bd9Sstevel@tonic-gate 			 * ip6_info header can be processed again.
13537c478bd9Sstevel@tonic-gate 			 */
13547c478bd9Sstevel@tonic-gate 			(void) ip_output_v6(Q_TO_CONN(q), ipsec_mp, q,
13557c478bd9Sstevel@tonic-gate 			    IRE_SEND);
13567c478bd9Sstevel@tonic-gate 		}
13577c478bd9Sstevel@tonic-gate 		/*
13587c478bd9Sstevel@tonic-gate 		 * Special code to support sending a single packet with
13597c478bd9Sstevel@tonic-gate 		 * conn_unspec_src using an IRE which has no source address.
13607c478bd9Sstevel@tonic-gate 		 * The IRE is deleted here after sending the packet to avoid
13617c478bd9Sstevel@tonic-gate 		 * having other code trip on it. But before we delete the
13627c478bd9Sstevel@tonic-gate 		 * ire, somebody could have looked up this ire.
13637c478bd9Sstevel@tonic-gate 		 * We prevent returning/using this IRE by the upper layers
13647c478bd9Sstevel@tonic-gate 		 * by making checks to NULL source address in other places
13657c478bd9Sstevel@tonic-gate 		 * like e.g ip_ire_append_v6, ip_ire_req and
13667c478bd9Sstevel@tonic-gate 		 * ip_bind_connected_v6. Though, this does not completely
13677c478bd9Sstevel@tonic-gate 		 * prevent other threads from using this ire, this should
13687c478bd9Sstevel@tonic-gate 		 * not cause any problems.
13697c478bd9Sstevel@tonic-gate 		 */
13707c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6)) {
13717c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_send_v6: delete IRE\n"));
13727c478bd9Sstevel@tonic-gate 			ire_delete(ire);
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 	ire_refrele(ire);	/* Held in ire_add */
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate /*
13797c478bd9Sstevel@tonic-gate  * Make sure that IRE bucket does not get too long.
13807c478bd9Sstevel@tonic-gate  * This can cause lock up because ire_cache_lookup()
13817c478bd9Sstevel@tonic-gate  * may take "forever" to finish.
13827c478bd9Sstevel@tonic-gate  *
13837c478bd9Sstevel@tonic-gate  * We just remove cnt IREs each time.  This means that
13847c478bd9Sstevel@tonic-gate  * the bucket length will stay approximately constant,
13857c478bd9Sstevel@tonic-gate  * depending on cnt.  This should be enough to defend
13867c478bd9Sstevel@tonic-gate  * against DoS attack based on creating temporary IREs
13877c478bd9Sstevel@tonic-gate  * (for forwarding and non-TCP traffic).
13887c478bd9Sstevel@tonic-gate  *
13897c478bd9Sstevel@tonic-gate  * Note that new IRE is normally added at the tail of the
13907c478bd9Sstevel@tonic-gate  * bucket.  This means that we are removing the "oldest"
13917c478bd9Sstevel@tonic-gate  * temporary IRE added.  Only if there are IREs with
13927c478bd9Sstevel@tonic-gate  * the same ire_addr, do we not add it at the tail.  Refer
13937c478bd9Sstevel@tonic-gate  * to ire_add_v*().  It should be OK for our purpose.
13947c478bd9Sstevel@tonic-gate  *
13957c478bd9Sstevel@tonic-gate  * For non-temporary cached IREs, we make sure that they
13967c478bd9Sstevel@tonic-gate  * have not been used for some time (defined below), they
13977c478bd9Sstevel@tonic-gate  * are non-local destinations, and there is no one using
13987c478bd9Sstevel@tonic-gate  * them at the moment (refcnt == 1).
13997c478bd9Sstevel@tonic-gate  *
14007c478bd9Sstevel@tonic-gate  * The above means that the IRE bucket length may become
14017c478bd9Sstevel@tonic-gate  * very long, consisting of mostly non-temporary IREs.
14027c478bd9Sstevel@tonic-gate  * This can happen when the hash function does a bad job
14037c478bd9Sstevel@tonic-gate  * so that most TCP connections cluster to a specific bucket.
14047c478bd9Sstevel@tonic-gate  * This "hopefully" should never happen.  It can also
14057c478bd9Sstevel@tonic-gate  * happen if most TCP connections have very long lives.
14067c478bd9Sstevel@tonic-gate  * Even with the minimal hash table size of 256, there
14077c478bd9Sstevel@tonic-gate  * has to be a lot of such connections to make the bucket
14087c478bd9Sstevel@tonic-gate  * length unreasonably long.  This should probably not
14097c478bd9Sstevel@tonic-gate  * happen either.  The third can when this can happen is
14107c478bd9Sstevel@tonic-gate  * when the machine is under attack, such as SYN flooding.
14117c478bd9Sstevel@tonic-gate  * TCP should already have the proper mechanism to protect
14127c478bd9Sstevel@tonic-gate  * that.  So we should be safe.
14137c478bd9Sstevel@tonic-gate  *
14147c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send() after
14157c478bd9Sstevel@tonic-gate  * a new IRE is added and the packet is sent.
14167c478bd9Sstevel@tonic-gate  *
14177c478bd9Sstevel@tonic-gate  * The idle cutoff interval is set to 60s.  It can be
14187c478bd9Sstevel@tonic-gate  * changed using /etc/system.
14197c478bd9Sstevel@tonic-gate  */
14207c478bd9Sstevel@tonic-gate uint32_t ire_idle_cutoff_interval = 60000;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate static void
14237c478bd9Sstevel@tonic-gate ire_cache_cleanup(irb_t *irb, uint32_t threshold, int cnt)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	ire_t *ire;
14267c478bd9Sstevel@tonic-gate 	int tmp_cnt = cnt;
14277c478bd9Sstevel@tonic-gate 	clock_t cut_off = drv_usectohz(ire_idle_cutoff_interval * 1000);
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/*
14307c478bd9Sstevel@tonic-gate 	 * irb is NULL if the IRE is not added to the hash.  This
14317c478bd9Sstevel@tonic-gate 	 * happens when IRE_MARK_NOADD is set in ire_add_then_send()
14327c478bd9Sstevel@tonic-gate 	 * and when ires are returned from ire_update_srcif_v4() routine.
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 	if (irb == NULL)
14357c478bd9Sstevel@tonic-gate 		return;
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
14387c478bd9Sstevel@tonic-gate 	if (irb->irb_tmp_ire_cnt > threshold) {
14397c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && tmp_cnt > 0;
14407c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
14417c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
14427c478bd9Sstevel@tonic-gate 				continue;
14437c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY) {
14447c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_type == IRE_CACHE);
14457c478bd9Sstevel@tonic-gate 				ire_delete(ire);
14467c478bd9Sstevel@tonic-gate 				tmp_cnt--;
14477c478bd9Sstevel@tonic-gate 			}
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	if (irb->irb_ire_cnt - irb->irb_tmp_ire_cnt > threshold) {
14517c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL && cnt > 0;
14527c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
14537c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED ||
14547c478bd9Sstevel@tonic-gate 			    ire->ire_gateway_addr == 0) {
14557c478bd9Sstevel@tonic-gate 				continue;
14567c478bd9Sstevel@tonic-gate 			}
14577c478bd9Sstevel@tonic-gate 			if ((ire->ire_type == IRE_CACHE) &&
14587c478bd9Sstevel@tonic-gate 			    (lbolt - ire->ire_last_used_time > cut_off) &&
14597c478bd9Sstevel@tonic-gate 			    (ire->ire_refcnt == 1)) {
14607c478bd9Sstevel@tonic-gate 				ire_delete(ire);
14617c478bd9Sstevel@tonic-gate 				cnt--;
14627c478bd9Sstevel@tonic-gate 			}
14637c478bd9Sstevel@tonic-gate 		}
14647c478bd9Sstevel@tonic-gate 	}
14657c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate /*
14697c478bd9Sstevel@tonic-gate  * ire_add_then_send is called when a new IRE has been created in order to
14707c478bd9Sstevel@tonic-gate  * route an outgoing packet.  Typically, it is called from ip_wput when
14717c478bd9Sstevel@tonic-gate  * a response comes back down from a resolver.  We add the IRE, and then
14727c478bd9Sstevel@tonic-gate  * possibly run the packet through ip_wput or ip_rput, as appropriate.
14737c478bd9Sstevel@tonic-gate  * However, we do not add the newly created IRE in the cache when
14747c478bd9Sstevel@tonic-gate  * IRE_MARK_NOADD is set in the IRE. IRE_MARK_NOADD is set at
14757c478bd9Sstevel@tonic-gate  * ip_newroute_ipif(). The ires with IRE_MARK_NOADD and ires returned
14767c478bd9Sstevel@tonic-gate  * by ire_update_srcif_v4() are ire_refrele'd by ip_wput_ire() and get
14777c478bd9Sstevel@tonic-gate  * deleted.
14787c478bd9Sstevel@tonic-gate  * Multirouting support: the packet is silently discarded when the new IRE
14797c478bd9Sstevel@tonic-gate  * holds the RTF_MULTIRT flag, but is not the first IRE to be added with the
14807c478bd9Sstevel@tonic-gate  * RTF_MULTIRT flag for the same destination address.
14817c478bd9Sstevel@tonic-gate  * In this case, we just want to register this additional ire without
14827c478bd9Sstevel@tonic-gate  * sending the packet, as it has already been replicated through
14837c478bd9Sstevel@tonic-gate  * existing multirt routes in ip_wput().
14847c478bd9Sstevel@tonic-gate  */
14857c478bd9Sstevel@tonic-gate void
14867c478bd9Sstevel@tonic-gate ire_add_then_send(queue_t *q, ire_t *ire, mblk_t *mp)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	irb_t *irb;
14897c478bd9Sstevel@tonic-gate 	boolean_t drop = B_FALSE;
14907c478bd9Sstevel@tonic-gate 	/* LINTED : set but not used in function */
14917c478bd9Sstevel@tonic-gate 	boolean_t mctl_present;
14927c478bd9Sstevel@tonic-gate 	mblk_t *first_mp = NULL;
14937c478bd9Sstevel@tonic-gate 	mblk_t *save_mp = NULL;
14947c478bd9Sstevel@tonic-gate 	ire_t *dst_ire;
14957c478bd9Sstevel@tonic-gate 	ipha_t *ipha;
14967c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
14997c478bd9Sstevel@tonic-gate 		/*
15007c478bd9Sstevel@tonic-gate 		 * We first have to retrieve the destination address carried
15017c478bd9Sstevel@tonic-gate 		 * by the packet.
15027c478bd9Sstevel@tonic-gate 		 * We can't rely on ire as it can be related to a gateway.
15037c478bd9Sstevel@tonic-gate 		 * The destination address will help in determining if
15047c478bd9Sstevel@tonic-gate 		 * other RTF_MULTIRT ires are already registered.
15057c478bd9Sstevel@tonic-gate 		 *
15067c478bd9Sstevel@tonic-gate 		 * We first need to know where we are going : v4 or V6.
15077c478bd9Sstevel@tonic-gate 		 * the ire version is enough, as there is no risk that
15087c478bd9Sstevel@tonic-gate 		 * we resolve an IPv6 address with an IPv4 ire
15097c478bd9Sstevel@tonic-gate 		 * or vice versa.
15107c478bd9Sstevel@tonic-gate 		 */
15117c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
15127c478bd9Sstevel@tonic-gate 			EXTRACT_PKT_MP(mp, first_mp, mctl_present);
15137c478bd9Sstevel@tonic-gate 			ipha = (ipha_t *)mp->b_rptr;
15147c478bd9Sstevel@tonic-gate 			save_mp = mp;
15157c478bd9Sstevel@tonic-gate 			mp = first_mp;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup(ipha->ipha_dst,
151845916cd2Sjpk 			    ire->ire_zoneid, MBLK_GETLABEL(mp));
15197c478bd9Sstevel@tonic-gate 		} else {
15207c478bd9Sstevel@tonic-gate 			/*
15217c478bd9Sstevel@tonic-gate 			 * Get a pointer to the beginning of the IPv6 header.
15227c478bd9Sstevel@tonic-gate 			 * Ignore leading IPsec control mblks.
15237c478bd9Sstevel@tonic-gate 			 */
15247c478bd9Sstevel@tonic-gate 			first_mp = mp;
15257c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_CTL) {
15267c478bd9Sstevel@tonic-gate 				mp = mp->b_cont;
15277c478bd9Sstevel@tonic-gate 			}
15287c478bd9Sstevel@tonic-gate 			ip6h = (ip6_t *)mp->b_rptr;
15297c478bd9Sstevel@tonic-gate 			save_mp = mp;
15307c478bd9Sstevel@tonic-gate 			mp = first_mp;
15317c478bd9Sstevel@tonic-gate 			dst_ire = ire_cache_lookup_v6(&ip6h->ip6_dst,
153245916cd2Sjpk 			    ire->ire_zoneid, MBLK_GETLABEL(mp));
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 		if (dst_ire != NULL) {
15357c478bd9Sstevel@tonic-gate 			if (dst_ire->ire_flags & RTF_MULTIRT) {
15367c478bd9Sstevel@tonic-gate 				/*
15377c478bd9Sstevel@tonic-gate 				 * At least one resolved multirt route
15387c478bd9Sstevel@tonic-gate 				 * already exists for the destination,
15397c478bd9Sstevel@tonic-gate 				 * don't sent this packet: either drop it
15407c478bd9Sstevel@tonic-gate 				 * or complete the pending resolution,
15417c478bd9Sstevel@tonic-gate 				 * depending on the ire.
15427c478bd9Sstevel@tonic-gate 				 */
15437c478bd9Sstevel@tonic-gate 				drop = B_TRUE;
15447c478bd9Sstevel@tonic-gate 			}
15457c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_then_send: dst_ire %p "
15467c478bd9Sstevel@tonic-gate 			    "[dst %08x, gw %08x], drop %d\n",
15477c478bd9Sstevel@tonic-gate 			    (void *)dst_ire,
15487c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
15497c478bd9Sstevel@tonic-gate 				ntohl(dst_ire->ire_addr) : \
15507c478bd9Sstevel@tonic-gate 				ntohl(V4_PART_OF_V6(dst_ire->ire_addr_v6)),
15517c478bd9Sstevel@tonic-gate 			    (dst_ire->ire_ipversion == IPV4_VERSION) ? \
15527c478bd9Sstevel@tonic-gate 				ntohl(dst_ire->ire_gateway_addr) : \
15537c478bd9Sstevel@tonic-gate 				ntohl(V4_PART_OF_V6(
15547c478bd9Sstevel@tonic-gate 				    dst_ire->ire_gateway_addr_v6)),
15557c478bd9Sstevel@tonic-gate 			    drop));
15567c478bd9Sstevel@tonic-gate 			ire_refrele(dst_ire);
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 	}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	if (!(ire->ire_marks & IRE_MARK_NOADD)) {
15617c478bd9Sstevel@tonic-gate 		/*
15627c478bd9Sstevel@tonic-gate 		 * Regular packets with cache bound ires and
15637c478bd9Sstevel@tonic-gate 		 * the packets from ARP response for ires which
15647c478bd9Sstevel@tonic-gate 		 * belong to the ire_srcif_v4 table, are here.
15657c478bd9Sstevel@tonic-gate 		 */
15667c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill == NULL) {
15677c478bd9Sstevel@tonic-gate 			/* Add the ire */
15687c478bd9Sstevel@tonic-gate 			(void) ire_add(&ire, NULL, NULL, NULL);
15697c478bd9Sstevel@tonic-gate 		} else {
15707c478bd9Sstevel@tonic-gate 			/*
15717c478bd9Sstevel@tonic-gate 			 * This must be ARP response for ire in interface based
15727c478bd9Sstevel@tonic-gate 			 * table. Note that we don't add them in cache table,
15737c478bd9Sstevel@tonic-gate 			 * instead we update the existing table with dlureq_mp
15747c478bd9Sstevel@tonic-gate 			 * information. The reverse tunnel ires do not come
15757c478bd9Sstevel@tonic-gate 			 * here, as reverse tunnel is non-resolver interface.
15767c478bd9Sstevel@tonic-gate 			 * XXX- another design alternative was to mark the
15777c478bd9Sstevel@tonic-gate 			 * ires in interface based table with a special mark to
15787c478bd9Sstevel@tonic-gate 			 * make absolutely sure that we operate in right ires.
15797c478bd9Sstevel@tonic-gate 			 * This idea was not implemented as part of code review
15807c478bd9Sstevel@tonic-gate 			 * suggestion, as ire_in_ill suffice to distinguish
15817c478bd9Sstevel@tonic-gate 			 * between the regular ires and interface based
15827c478bd9Sstevel@tonic-gate 			 * ires now and thus we save a bit in the ire_marks.
15837c478bd9Sstevel@tonic-gate 			 */
15847c478bd9Sstevel@tonic-gate 			ire = ire_update_srcif_v4(ire);
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
15887c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
15897c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
15907c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
15917c478bd9Sstevel@tonic-gate 			freemsg(mp);
15927c478bd9Sstevel@tonic-gate 			return;
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
15957c478bd9Sstevel@tonic-gate 			ire_refrele(ire);	/* Held in ire_add_v4/v6 */
15967c478bd9Sstevel@tonic-gate 			return;
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 	if (drop) {
16007c478bd9Sstevel@tonic-gate 		/*
16017c478bd9Sstevel@tonic-gate 		 * If we're adding an RTF_MULTIRT ire, the resolution
16027c478bd9Sstevel@tonic-gate 		 * is over: we just drop the packet.
16037c478bd9Sstevel@tonic-gate 		 */
16047c478bd9Sstevel@tonic-gate 		if (ire->ire_flags & RTF_MULTIRT) {
16057c478bd9Sstevel@tonic-gate 			if (save_mp) {
16067c478bd9Sstevel@tonic-gate 				save_mp->b_prev = NULL;
16077c478bd9Sstevel@tonic-gate 				save_mp->b_next = NULL;
16087c478bd9Sstevel@tonic-gate 			}
16097c478bd9Sstevel@tonic-gate 			MULTIRT_DEBUG_UNTAG(mp);
16107c478bd9Sstevel@tonic-gate 			freemsg(mp);
16117c478bd9Sstevel@tonic-gate 		} else {
16127c478bd9Sstevel@tonic-gate 			/*
16137c478bd9Sstevel@tonic-gate 			 * Otherwise, we're adding the ire to a gateway
16147c478bd9Sstevel@tonic-gate 			 * for a multirt route.
16157c478bd9Sstevel@tonic-gate 			 * Invoke ip_newroute() to complete the resolution
16167c478bd9Sstevel@tonic-gate 			 * of the route. We will then come back here and
16177c478bd9Sstevel@tonic-gate 			 * finally drop this packet in the above code.
16187c478bd9Sstevel@tonic-gate 			 */
16197c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
16207c478bd9Sstevel@tonic-gate 				/*
16217c478bd9Sstevel@tonic-gate 				 * TODO: in order for CGTP to work in non-global
16227c478bd9Sstevel@tonic-gate 				 * zones, ip_newroute() must create the IRE
16237c478bd9Sstevel@tonic-gate 				 * cache in the zone indicated by
16247c478bd9Sstevel@tonic-gate 				 * ire->ire_zoneid.
16257c478bd9Sstevel@tonic-gate 				 */
16267c478bd9Sstevel@tonic-gate 				ip_newroute(q, mp, ipha->ipha_dst, 0,
16277c478bd9Sstevel@tonic-gate 				    (CONN_Q(q) ? Q_TO_CONN(q) : NULL));
16287c478bd9Sstevel@tonic-gate 			} else {
16297c478bd9Sstevel@tonic-gate 				ip_newroute_v6(q, mp, &ip6h->ip6_dst, NULL,
16307c478bd9Sstevel@tonic-gate 				    NULL, ire->ire_zoneid);
16317c478bd9Sstevel@tonic-gate 			}
16327c478bd9Sstevel@tonic-gate 		}
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 		ire_refrele(ire); /* As done by ire_send(). */
16357c478bd9Sstevel@tonic-gate 		return;
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 	/*
16387c478bd9Sstevel@tonic-gate 	 * Need to remember ire_bucket here as ire_send*() may delete
16397c478bd9Sstevel@tonic-gate 	 * the ire so we cannot reference it after that.
16407c478bd9Sstevel@tonic-gate 	 */
16417c478bd9Sstevel@tonic-gate 	irb = ire->ire_bucket;
16427c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
16437c478bd9Sstevel@tonic-gate 		ire_send_v6(q, mp, ire);
16447c478bd9Sstevel@tonic-gate 		/*
16457c478bd9Sstevel@tonic-gate 		 * Clean up more than 1 IRE so that the clean up does not
16467c478bd9Sstevel@tonic-gate 		 * need to be done every time when a new IRE is added and
16477c478bd9Sstevel@tonic-gate 		 * the threshold is reached.
16487c478bd9Sstevel@tonic-gate 		 */
16497c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip6_ire_max_bucket_cnt, 2);
16507c478bd9Sstevel@tonic-gate 	} else {
16517c478bd9Sstevel@tonic-gate 		ire_send(q, mp, ire);
16527c478bd9Sstevel@tonic-gate 		ire_cache_cleanup(irb, ip_ire_max_bucket_cnt, 2);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate }
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate /*
16577c478bd9Sstevel@tonic-gate  * Initialize the ire that is specific to IPv4 part and call
16587c478bd9Sstevel@tonic-gate  * ire_init_common to finish it.
16597c478bd9Sstevel@tonic-gate  */
16607c478bd9Sstevel@tonic-gate ire_t *
16617c478bd9Sstevel@tonic-gate ire_init(ire_t *ire, uchar_t *addr, uchar_t *mask, uchar_t *src_addr,
16627c478bd9Sstevel@tonic-gate     uchar_t *gateway, uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp,
16637c478bd9Sstevel@tonic-gate     queue_t *rfq, queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif,
16647c478bd9Sstevel@tonic-gate     ill_t *in_ill, ipaddr_t cmask, uint32_t phandle, uint32_t ihandle,
166545916cd2Sjpk     uint32_t flags, const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp)
16667c478bd9Sstevel@tonic-gate {
166745916cd2Sjpk 	/*
166845916cd2Sjpk 	 * Reject IRE security attribute creation/initialization
166945916cd2Sjpk 	 * if system is not running in Trusted mode.
167045916cd2Sjpk 	 */
167145916cd2Sjpk 	if ((gc != NULL || gcgrp != NULL) && !is_system_labeled())
167245916cd2Sjpk 		return (NULL);
167345916cd2Sjpk 
16747c478bd9Sstevel@tonic-gate 	if (fp_mp != NULL) {
16757c478bd9Sstevel@tonic-gate 		/*
16767c478bd9Sstevel@tonic-gate 		 * We can't dupb() here as multiple threads could be
16777c478bd9Sstevel@tonic-gate 		 * calling dupb on the same mp which is incorrect.
16787c478bd9Sstevel@tonic-gate 		 * First dupb() should be called only by one thread.
16797c478bd9Sstevel@tonic-gate 		 */
16807c478bd9Sstevel@tonic-gate 		fp_mp = copyb(fp_mp);
16817c478bd9Sstevel@tonic-gate 		if (fp_mp == NULL)
16827c478bd9Sstevel@tonic-gate 			return (NULL);
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	if (dlureq_mp != NULL) {
16867c478bd9Sstevel@tonic-gate 		/*
16877c478bd9Sstevel@tonic-gate 		 * We can't dupb() here as multiple threads could be
16887c478bd9Sstevel@tonic-gate 		 * calling dupb on the same mp which is incorrect.
16897c478bd9Sstevel@tonic-gate 		 * First dupb() should be called only by one thread.
16907c478bd9Sstevel@tonic-gate 		 */
16917c478bd9Sstevel@tonic-gate 		dlureq_mp = copyb(dlureq_mp);
16927c478bd9Sstevel@tonic-gate 		if (dlureq_mp == NULL) {
16937c478bd9Sstevel@tonic-gate 			if (fp_mp != NULL)
16947c478bd9Sstevel@tonic-gate 				freeb(fp_mp);
16957c478bd9Sstevel@tonic-gate 			return (NULL);
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * Check that IRE_IF_RESOLVER and IRE_IF_NORESOLVER have a
17017c478bd9Sstevel@tonic-gate 	 * dlureq_mp which is the ill_resolver_mp for IRE_IF_RESOLVER
17027c478bd9Sstevel@tonic-gate 	 * and DL_UNITDATA_REQ for IRE_IF_NORESOLVER.
17037c478bd9Sstevel@tonic-gate 	 */
17047c478bd9Sstevel@tonic-gate 	if ((type & IRE_INTERFACE) &&
17057c478bd9Sstevel@tonic-gate 	    dlureq_mp == NULL) {
17067c478bd9Sstevel@tonic-gate 		ASSERT(fp_mp == NULL);
17077c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_init: no dlureq_mp\n"));
17087c478bd9Sstevel@tonic-gate 		return (NULL);
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	BUMP_IRE_STATS(ire_stats_v4, ire_stats_alloced);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	if (addr != NULL)
17147c478bd9Sstevel@tonic-gate 		bcopy(addr, &ire->ire_addr, IP_ADDR_LEN);
17157c478bd9Sstevel@tonic-gate 	if (src_addr != NULL)
17167c478bd9Sstevel@tonic-gate 		bcopy(src_addr, &ire->ire_src_addr, IP_ADDR_LEN);
17177c478bd9Sstevel@tonic-gate 	if (mask != NULL) {
17187c478bd9Sstevel@tonic-gate 		bcopy(mask, &ire->ire_mask, IP_ADDR_LEN);
17197c478bd9Sstevel@tonic-gate 		ire->ire_masklen = ip_mask_to_plen(ire->ire_mask);
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 	if (gateway != NULL) {
17227c478bd9Sstevel@tonic-gate 		bcopy(gateway, &ire->ire_gateway_addr, IP_ADDR_LEN);
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 	if (in_src_addr != NULL) {
17257c478bd9Sstevel@tonic-gate 		bcopy(in_src_addr, &ire->ire_in_src_addr, IP_ADDR_LEN);
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	if (type == IRE_CACHE)
17297c478bd9Sstevel@tonic-gate 		ire->ire_cmask = cmask;
17307c478bd9Sstevel@tonic-gate 
173145916cd2Sjpk 	/* ire_init_common will free the mblks upon encountering any failure */
173245916cd2Sjpk 	if (!ire_init_common(ire, max_fragp, fp_mp, rfq, stq, type, dlureq_mp,
173345916cd2Sjpk 	    ipif, in_ill, phandle, ihandle, flags, IPV4_VERSION, ulp_info,
173445916cd2Sjpk 	    gc, gcgrp))
173545916cd2Sjpk 		return (NULL);
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	return (ire);
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate  * Similar to ire_create except that it is called only when
17427c478bd9Sstevel@tonic-gate  * we want to allocate ire as an mblk e.g. we have an external
17437c478bd9Sstevel@tonic-gate  * resolver ARP.
17447c478bd9Sstevel@tonic-gate  */
17457c478bd9Sstevel@tonic-gate ire_t *
17467c478bd9Sstevel@tonic-gate ire_create_mp(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
17477c478bd9Sstevel@tonic-gate     uchar_t *in_src_addr, uint_t max_frag, mblk_t *fp_mp, queue_t *rfq,
17487c478bd9Sstevel@tonic-gate     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
17497c478bd9Sstevel@tonic-gate     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
175045916cd2Sjpk     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	ire_t	*ire;
17537c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
17547c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/* Allocate the new IRE. */
17577c478bd9Sstevel@tonic-gate 	mp = allocb(sizeof (ire_t), BPRI_MED);
17587c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
17597c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create_mp: alloc failed\n"));
17607c478bd9Sstevel@tonic-gate 		return (NULL);
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	ire = (ire_t *)mp->b_rptr;
17647c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)&ire[1];
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/* Start clean. */
17677c478bd9Sstevel@tonic-gate 	*ire = ire_null;
17687c478bd9Sstevel@tonic-gate 	ire->ire_mp = mp;
17697c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = IRE_DB_TYPE;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
17727c478bd9Sstevel@tonic-gate 	    NULL, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill, cmask,
177345916cd2Sjpk 	    phandle, ihandle, flags, ulp_info, gc, gcgrp);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
17767c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
17777c478bd9Sstevel@tonic-gate 		return (NULL);
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
17807c478bd9Sstevel@tonic-gate 	/*
17817c478bd9Sstevel@tonic-gate 	 * ire_max_frag is normally zero here and is atomically set
17827c478bd9Sstevel@tonic-gate 	 * under the irebucket lock in ire_add_v[46] except for the
17837c478bd9Sstevel@tonic-gate 	 * case of IRE_MARK_NOADD. In that event the the ire_max_frag
17847c478bd9Sstevel@tonic-gate 	 * is non-zero here.
17857c478bd9Sstevel@tonic-gate 	 */
17867c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = max_frag;
17877c478bd9Sstevel@tonic-gate 	return (ire);
17887c478bd9Sstevel@tonic-gate }
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate /*
17917c478bd9Sstevel@tonic-gate  * ire_create is called to allocate and initialize a new IRE.
17927c478bd9Sstevel@tonic-gate  *
17937c478bd9Sstevel@tonic-gate  * NOTE : This is called as writer sometimes though not required
17947c478bd9Sstevel@tonic-gate  * by this function.
17957c478bd9Sstevel@tonic-gate  */
17967c478bd9Sstevel@tonic-gate ire_t *
17977c478bd9Sstevel@tonic-gate ire_create(uchar_t *addr, uchar_t *mask, uchar_t *src_addr, uchar_t *gateway,
17987c478bd9Sstevel@tonic-gate     uchar_t *in_src_addr, uint_t *max_fragp, mblk_t *fp_mp, queue_t *rfq,
17997c478bd9Sstevel@tonic-gate     queue_t *stq, ushort_t type, mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill,
18007c478bd9Sstevel@tonic-gate     ipaddr_t cmask, uint32_t phandle, uint32_t ihandle, uint32_t flags,
180145916cd2Sjpk     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp)
18027c478bd9Sstevel@tonic-gate {
18037c478bd9Sstevel@tonic-gate 	ire_t	*ire;
18047c478bd9Sstevel@tonic-gate 	ire_t	*ret_ire;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
18077c478bd9Sstevel@tonic-gate 	if (ire == NULL) {
18087c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_create: alloc failed\n"));
18097c478bd9Sstevel@tonic-gate 		return (NULL);
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 	*ire = ire_null;
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	ret_ire = ire_init(ire, addr, mask, src_addr, gateway, in_src_addr,
18147c478bd9Sstevel@tonic-gate 	    max_fragp, fp_mp, rfq, stq, type, dlureq_mp, ipif, in_ill,  cmask,
181545916cd2Sjpk 	    phandle, ihandle, flags, ulp_info, gc, gcgrp);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	if (ret_ire == NULL) {
18187c478bd9Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
18197c478bd9Sstevel@tonic-gate 		return (NULL);
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 	ASSERT(ret_ire == ire);
18227c478bd9Sstevel@tonic-gate 	return (ire);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate /*
18277c478bd9Sstevel@tonic-gate  * Common to IPv4 and IPv6
18287c478bd9Sstevel@tonic-gate  */
182945916cd2Sjpk boolean_t
18307c478bd9Sstevel@tonic-gate ire_init_common(ire_t *ire, uint_t *max_fragp, mblk_t *fp_mp,
18317c478bd9Sstevel@tonic-gate     queue_t *rfq, queue_t *stq, ushort_t type,
18327c478bd9Sstevel@tonic-gate     mblk_t *dlureq_mp, ipif_t *ipif, ill_t *in_ill, uint32_t phandle,
18337c478bd9Sstevel@tonic-gate     uint32_t ihandle, uint32_t flags, uchar_t ipversion,
183445916cd2Sjpk     const iulp_t *ulp_info, tsol_gc_t *gc, tsol_gcgrp_t *gcgrp)
18357c478bd9Sstevel@tonic-gate {
18367c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = max_fragp;
18377c478bd9Sstevel@tonic-gate 	ire->ire_frag_flag |= (ip_path_mtu_discovery) ? IPH_DF : 0;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	ASSERT(fp_mp == NULL || fp_mp->b_datap->db_type == M_DATA);
184045916cd2Sjpk #ifdef DEBUG
184145916cd2Sjpk 	if (ipif != NULL) {
18427c478bd9Sstevel@tonic-gate 		if (ipif->ipif_isv6)
18437c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV6_VERSION);
18447c478bd9Sstevel@tonic-gate 		else
18457c478bd9Sstevel@tonic-gate 			ASSERT(ipversion == IPV4_VERSION);
18467c478bd9Sstevel@tonic-gate 	}
184745916cd2Sjpk #endif /* DEBUG */
184845916cd2Sjpk 
184945916cd2Sjpk 	/*
185045916cd2Sjpk 	 * Create/initialize IRE security attribute only in Trusted mode;
185145916cd2Sjpk 	 * if the passed in gc/gcgrp is non-NULL, we expect that the caller
185245916cd2Sjpk 	 * has held a reference to it and will release it when this routine
185345916cd2Sjpk 	 * returns a failure, otherwise we own the reference.  We do this
185445916cd2Sjpk 	 * prior to initializing the rest IRE fields.
1855*dc3879f9Sjarrett 	 *
1856*dc3879f9Sjarrett 	 * Don't allocate ire_gw_secattr for the resolver case to prevent
1857*dc3879f9Sjarrett 	 * memory leak (in case of external resolution failure). We'll
1858*dc3879f9Sjarrett 	 * allocate it after a successful external resolution, in ire_add().
1859*dc3879f9Sjarrett 	 * Note that ire->ire_mp != NULL here means this ire is headed
1860*dc3879f9Sjarrett 	 * to an external resolver.
186145916cd2Sjpk 	 */
186245916cd2Sjpk 	if (is_system_labeled()) {
186345916cd2Sjpk 		if ((type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST |
186445916cd2Sjpk 		    IRE_INTERFACE)) != 0) {
186545916cd2Sjpk 			/* release references on behalf of caller */
186645916cd2Sjpk 			if (gc != NULL)
186745916cd2Sjpk 				GC_REFRELE(gc);
186845916cd2Sjpk 			if (gcgrp != NULL)
186945916cd2Sjpk 				GCGRP_REFRELE(gcgrp);
1870*dc3879f9Sjarrett 		} else if ((ire->ire_mp == NULL) &&
1871*dc3879f9Sjarrett 		    tsol_ire_init_gwattr(ire, ipversion, gc, gcgrp) != 0) {
187245916cd2Sjpk 			/* free any caller-allocated mblks upon failure */
187345916cd2Sjpk 			if (fp_mp != NULL)
187445916cd2Sjpk 				freeb(fp_mp);
187545916cd2Sjpk 			if (dlureq_mp != NULL)
187645916cd2Sjpk 				freeb(dlureq_mp);
187745916cd2Sjpk 			return (B_FALSE);
187845916cd2Sjpk 		}
187945916cd2Sjpk 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	ire->ire_fp_mp = fp_mp;
18827c478bd9Sstevel@tonic-gate 	ire->ire_dlureq_mp = dlureq_mp;
18837c478bd9Sstevel@tonic-gate 	ire->ire_stq = stq;
18847c478bd9Sstevel@tonic-gate 	ire->ire_rfq = rfq;
18857c478bd9Sstevel@tonic-gate 	ire->ire_type = type;
18867c478bd9Sstevel@tonic-gate 	ire->ire_flags = RTF_UP | flags;
18877c478bd9Sstevel@tonic-gate 	ire->ire_ident = TICK_TO_MSEC(lbolt);
18887c478bd9Sstevel@tonic-gate 	bcopy(ulp_info, &ire->ire_uinfo, sizeof (iulp_t));
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
18917c478bd9Sstevel@tonic-gate 	ire->ire_last_used_time = lbolt;
18927c478bd9Sstevel@tonic-gate 	ire->ire_create_time = (uint32_t)gethrestime_sec();
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	/*
18957c478bd9Sstevel@tonic-gate 	 * If this IRE is an IRE_CACHE, inherit the handles from the
18967c478bd9Sstevel@tonic-gate 	 * parent IREs. For others in the forwarding table, assign appropriate
18977c478bd9Sstevel@tonic-gate 	 * new ones.
18987c478bd9Sstevel@tonic-gate 	 *
18997c478bd9Sstevel@tonic-gate 	 * The mutex protecting ire_handle is because ire_create is not always
19007c478bd9Sstevel@tonic-gate 	 * called as a writer.
19017c478bd9Sstevel@tonic-gate 	 */
19027c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_OFFSUBNET) {
19037c478bd9Sstevel@tonic-gate 		mutex_enter(&ire_handle_lock);
19047c478bd9Sstevel@tonic-gate 		ire->ire_phandle = (uint32_t)ire_handle++;
19057c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_handle_lock);
19067c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type & IRE_INTERFACE) {
19077c478bd9Sstevel@tonic-gate 		mutex_enter(&ire_handle_lock);
19087c478bd9Sstevel@tonic-gate 		ire->ire_ihandle = (uint32_t)ire_handle++;
19097c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_handle_lock);
19107c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_CACHE) {
19117c478bd9Sstevel@tonic-gate 		ire->ire_phandle = phandle;
19127c478bd9Sstevel@tonic-gate 		ire->ire_ihandle = ihandle;
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 	ire->ire_in_ill = in_ill;
19157c478bd9Sstevel@tonic-gate 	ire->ire_ipif = ipif;
19167c478bd9Sstevel@tonic-gate 	if (ipif != NULL) {
19177c478bd9Sstevel@tonic-gate 		ire->ire_ipif_seqid = ipif->ipif_seqid;
19187c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = ipif->ipif_zoneid;
19197c478bd9Sstevel@tonic-gate 	} else {
19207c478bd9Sstevel@tonic-gate 		ire->ire_zoneid = GLOBAL_ZONEID;
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 	ire->ire_ipversion = ipversion;
19237c478bd9Sstevel@tonic-gate 	ire->ire_refcnt = 1;
19247c478bd9Sstevel@tonic-gate 	mutex_init(&ire->ire_lock, NULL, MUTEX_DEFAULT, NULL);
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
19277c478bd9Sstevel@tonic-gate 	bzero(ire->ire_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
19287c478bd9Sstevel@tonic-gate #endif
192945916cd2Sjpk 
193045916cd2Sjpk 	return (B_TRUE);
19317c478bd9Sstevel@tonic-gate }
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate /*
19347c478bd9Sstevel@tonic-gate  * This routine is called repeatedly by ipif_up to create broadcast IREs.
19357c478bd9Sstevel@tonic-gate  * It is passed a pointer to a slot in an IRE pointer array into which to
19367c478bd9Sstevel@tonic-gate  * place the pointer to the new IRE, if indeed we create one.  If the
19377c478bd9Sstevel@tonic-gate  * IRE corresponding to the address passed in would be a duplicate of an
19387c478bd9Sstevel@tonic-gate  * existing one, we don't create the new one.  irep is incremented before
19397c478bd9Sstevel@tonic-gate  * return only if we do create a new IRE.  (Always called as writer.)
19407c478bd9Sstevel@tonic-gate  *
19417c478bd9Sstevel@tonic-gate  * Note that with the "match_flags" parameter, we can match on either
19427c478bd9Sstevel@tonic-gate  * a particular logical interface (MATCH_IRE_IPIF) or for all logical
19437c478bd9Sstevel@tonic-gate  * interfaces for a given physical interface (MATCH_IRE_ILL).  Currently,
19447c478bd9Sstevel@tonic-gate  * we only create broadcast ire's on a per physical interface basis. If
19457c478bd9Sstevel@tonic-gate  * someone is going to be mucking with logical interfaces, it is important
19467c478bd9Sstevel@tonic-gate  * to call "ipif_check_bcast_ires()" to make sure that any change to a
19477c478bd9Sstevel@tonic-gate  * logical interface will not cause critical broadcast IRE's to be deleted.
19487c478bd9Sstevel@tonic-gate  */
19497c478bd9Sstevel@tonic-gate ire_t **
19507c478bd9Sstevel@tonic-gate ire_check_and_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep,
19517c478bd9Sstevel@tonic-gate     int match_flags)
19527c478bd9Sstevel@tonic-gate {
19537c478bd9Sstevel@tonic-gate 	ire_t *ire;
19547c478bd9Sstevel@tonic-gate 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/*
19577c478bd9Sstevel@tonic-gate 	 * No broadcast IREs for the LOOPBACK interface
19587c478bd9Sstevel@tonic-gate 	 * or others such as point to point and IPIF_NOXMIT.
19597c478bd9Sstevel@tonic-gate 	 */
19607c478bd9Sstevel@tonic-gate 	if (!(ipif->ipif_flags & IPIF_BROADCAST) ||
19617c478bd9Sstevel@tonic-gate 	    (ipif->ipif_flags & IPIF_NOXMIT))
19627c478bd9Sstevel@tonic-gate 		return (irep);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	/* If this would be a duplicate, don't bother. */
19657c478bd9Sstevel@tonic-gate 	if ((ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ipif,
196645916cd2Sjpk 	    ipif->ipif_zoneid, NULL, match_flags)) != NULL) {
19677c478bd9Sstevel@tonic-gate 		/*
19687c478bd9Sstevel@tonic-gate 		 * We look for non-deprecated (and non-anycast, non-nolocal)
19697c478bd9Sstevel@tonic-gate 		 * ipifs as the best choice. ipifs with check_flags matching
19707c478bd9Sstevel@tonic-gate 		 * (deprecated, etc) are used only if non-deprecated ipifs
19717c478bd9Sstevel@tonic-gate 		 * are not available. if the existing ire's ipif is deprecated
19727c478bd9Sstevel@tonic-gate 		 * and the new ipif is non-deprecated, switch to the new ipif
19737c478bd9Sstevel@tonic-gate 		 */
19747c478bd9Sstevel@tonic-gate 		if ((!(ire->ire_ipif->ipif_flags & check_flags)) ||
19757c478bd9Sstevel@tonic-gate 		    (ipif->ipif_flags & check_flags)) {
19767c478bd9Sstevel@tonic-gate 			ire_refrele(ire);
19777c478bd9Sstevel@tonic-gate 			return (irep);
19787c478bd9Sstevel@tonic-gate 		}
19797c478bd9Sstevel@tonic-gate 		/*
19807c478bd9Sstevel@tonic-gate 		 * Bcast ires exist in pairs. Both have to be deleted,
19817c478bd9Sstevel@tonic-gate 		 * Since we are exclusive we can make the above assertion.
19827c478bd9Sstevel@tonic-gate 		 * The 1st has to be refrele'd since it was ctable_lookup'd.
19837c478bd9Sstevel@tonic-gate 		 */
19847c478bd9Sstevel@tonic-gate 		ASSERT(IAM_WRITER_IPIF(ipif));
19857c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_next->ire_addr == ire->ire_addr);
19867c478bd9Sstevel@tonic-gate 		ire_delete(ire->ire_next);
19877c478bd9Sstevel@tonic-gate 		ire_delete(ire);
19887c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
19897c478bd9Sstevel@tonic-gate 	}
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	irep = ire_create_bcast(ipif, addr, irep);
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	return (irep);
19947c478bd9Sstevel@tonic-gate }
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate uint_t ip_loopback_mtu = IP_LOOPBACK_MTU;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate /*
19997c478bd9Sstevel@tonic-gate  * This routine is called from ipif_check_bcast_ires and ire_check_bcast.
20007c478bd9Sstevel@tonic-gate  * It leaves all the verifying and deleting to those routines. So it always
20017c478bd9Sstevel@tonic-gate  * creates 2 bcast ires and chains them into the ire array passed in.
20027c478bd9Sstevel@tonic-gate  */
20037c478bd9Sstevel@tonic-gate ire_t **
20047c478bd9Sstevel@tonic-gate ire_create_bcast(ipif_t *ipif, ipaddr_t  addr, ire_t **irep)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
20077c478bd9Sstevel@tonic-gate 	    (uchar_t *)&addr,			/* dest addr */
20087c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ip_g_all_ones,		/* mask */
20097c478bd9Sstevel@tonic-gate 	    (uchar_t *)&ipif->ipif_src_addr,	/* source addr */
20107c478bd9Sstevel@tonic-gate 	    NULL,				/* no gateway */
20117c478bd9Sstevel@tonic-gate 	    NULL,				/* no in_src_addr */
20127c478bd9Sstevel@tonic-gate 	    &ipif->ipif_mtu,			/* max frag */
20137c478bd9Sstevel@tonic-gate 	    NULL,				/* fast path header */
20147c478bd9Sstevel@tonic-gate 	    ipif->ipif_rq,			/* recv-from queue */
20157c478bd9Sstevel@tonic-gate 	    ipif->ipif_wq,			/* send-to queue */
20167c478bd9Sstevel@tonic-gate 	    IRE_BROADCAST,
20177c478bd9Sstevel@tonic-gate 	    ipif->ipif_bcast_mp,		/* xmit header */
20187c478bd9Sstevel@tonic-gate 	    ipif,
20197c478bd9Sstevel@tonic-gate 	    NULL,
20207c478bd9Sstevel@tonic-gate 	    0,
20217c478bd9Sstevel@tonic-gate 	    0,
20227c478bd9Sstevel@tonic-gate 	    0,
20237c478bd9Sstevel@tonic-gate 	    0,
202445916cd2Sjpk 	    &ire_uinfo_null,
202545916cd2Sjpk 	    NULL,
202645916cd2Sjpk 	    NULL);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	*irep++ = ire_create(
20297c478bd9Sstevel@tonic-gate 		(uchar_t *)&addr,		 /* dest address */
20307c478bd9Sstevel@tonic-gate 		(uchar_t *)&ip_g_all_ones,	 /* mask */
20317c478bd9Sstevel@tonic-gate 		(uchar_t *)&ipif->ipif_src_addr, /* source address */
20327c478bd9Sstevel@tonic-gate 		NULL,				 /* no gateway */
20337c478bd9Sstevel@tonic-gate 		NULL,				 /* no in_src_addr */
20347c478bd9Sstevel@tonic-gate 		&ip_loopback_mtu,		 /* max frag size */
20357c478bd9Sstevel@tonic-gate 		NULL,				 /* Fast Path header */
20367c478bd9Sstevel@tonic-gate 		ipif->ipif_rq,			 /* recv-from queue */
20377c478bd9Sstevel@tonic-gate 		NULL,				 /* no send-to queue */
20387c478bd9Sstevel@tonic-gate 		IRE_BROADCAST,		/* Needed for fanout in wput */
20397c478bd9Sstevel@tonic-gate 		NULL,
20407c478bd9Sstevel@tonic-gate 		ipif,
20417c478bd9Sstevel@tonic-gate 		NULL,
20427c478bd9Sstevel@tonic-gate 		0,
20437c478bd9Sstevel@tonic-gate 		0,
20447c478bd9Sstevel@tonic-gate 		0,
20457c478bd9Sstevel@tonic-gate 		0,
204645916cd2Sjpk 		&ire_uinfo_null,
204745916cd2Sjpk 		NULL,
204845916cd2Sjpk 		NULL);
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	return (irep);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate /*
20547c478bd9Sstevel@tonic-gate  * ire_walk routine to delete or update any IRE_CACHE that might contain
20557c478bd9Sstevel@tonic-gate  * stale information.
20567c478bd9Sstevel@tonic-gate  * The flags state which entries to delete or update.
20577c478bd9Sstevel@tonic-gate  * Garbage collection is done separately using kmem alloc callbacks to
20587c478bd9Sstevel@tonic-gate  * ip_trash_ire_reclaim.
20597c478bd9Sstevel@tonic-gate  * Used for both IPv4 and IPv6. However, IPv6 only uses FLUSH_MTU_TIME
20607c478bd9Sstevel@tonic-gate  * since other stale information is cleaned up using NUD.
20617c478bd9Sstevel@tonic-gate  */
20627c478bd9Sstevel@tonic-gate void
20637c478bd9Sstevel@tonic-gate ire_expire(ire_t *ire, char *arg)
20647c478bd9Sstevel@tonic-gate {
20657c478bd9Sstevel@tonic-gate 	int flush_flags = (int)(uintptr_t)arg;
20667c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	if ((flush_flags & FLUSH_REDIRECT_TIME) &&
20697c478bd9Sstevel@tonic-gate 	    ire->ire_type == IRE_HOST_REDIRECT) {
20707c478bd9Sstevel@tonic-gate 		/* Make sure we delete the corresponding IRE_CACHE */
20717c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_expire: all redirects\n"));
20727c478bd9Sstevel@tonic-gate 		ip_rts_rtmsg(RTM_DELETE, ire, 0);
20737c478bd9Sstevel@tonic-gate 		ire_delete(ire);
20747c478bd9Sstevel@tonic-gate 		return;
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
20777c478bd9Sstevel@tonic-gate 		return;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	if (flush_flags & FLUSH_ARP_TIME) {
20807c478bd9Sstevel@tonic-gate 		/*
20817c478bd9Sstevel@tonic-gate 		 * Remove all IRE_CACHE.
20827c478bd9Sstevel@tonic-gate 		 * Verify that create time is more than
20837c478bd9Sstevel@tonic-gate 		 * ip_ire_arp_interval milliseconds ago.
20847c478bd9Sstevel@tonic-gate 		 */
20857c478bd9Sstevel@tonic-gate 		if (((uint32_t)gethrestime_sec() - ire->ire_create_time) *
20867c478bd9Sstevel@tonic-gate 		    MILLISEC > ip_ire_arp_interval) {
20877c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_expire: all IRE_CACHE\n"));
20887c478bd9Sstevel@tonic-gate 			ire_delete(ire);
20897c478bd9Sstevel@tonic-gate 			return;
20907c478bd9Sstevel@tonic-gate 		}
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (ip_path_mtu_discovery && (flush_flags & FLUSH_MTU_TIME) &&
20947c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif != NULL)) {
20957c478bd9Sstevel@tonic-gate 		/* Increase pmtu if it is less than the interface mtu */
20967c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
20977c478bd9Sstevel@tonic-gate 		/*
20987c478bd9Sstevel@tonic-gate 		 * If the ipif is a vni (whose mtu is 0, since it's virtual)
20997c478bd9Sstevel@tonic-gate 		 * get the mtu from the sending interfaces' ipif
21007c478bd9Sstevel@tonic-gate 		 */
21017c478bd9Sstevel@tonic-gate 		if (IS_VNI(ire->ire_ipif->ipif_ill)) {
21027c478bd9Sstevel@tonic-gate 			stq_ill = ire->ire_stq->q_ptr;
21037c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(stq_ill->ill_ipif->ipif_mtu,
21047c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
21057c478bd9Sstevel@tonic-gate 		} else {
21067c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = MIN(ire->ire_ipif->ipif_mtu,
21077c478bd9Sstevel@tonic-gate 			    IP_MAXPACKET);
21087c478bd9Sstevel@tonic-gate 		}
21097c478bd9Sstevel@tonic-gate 		ire->ire_frag_flag |= IPH_DF;
21107c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
21117c478bd9Sstevel@tonic-gate 	}
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate /*
21157c478bd9Sstevel@tonic-gate  * Do fast path probing if necessary.
21167c478bd9Sstevel@tonic-gate  */
21177c478bd9Sstevel@tonic-gate static void
21187c478bd9Sstevel@tonic-gate ire_fastpath(ire_t *ire)
21197c478bd9Sstevel@tonic-gate {
21207c478bd9Sstevel@tonic-gate 	ill_t	*ill;
21217c478bd9Sstevel@tonic-gate 	int res;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	if (ire->ire_fp_mp != NULL || ire->ire_dlureq_mp == NULL ||
21247c478bd9Sstevel@tonic-gate 	    (ire->ire_stq == NULL)) {
21257c478bd9Sstevel@tonic-gate 		/*
21267c478bd9Sstevel@tonic-gate 		 * Already contains fastpath info or
21277c478bd9Sstevel@tonic-gate 		 * doesn't have DL_UNITDATA_REQ header
21287c478bd9Sstevel@tonic-gate 		 * or is a loopback broadcast ire i.e. no stq.
21297c478bd9Sstevel@tonic-gate 		 */
21307c478bd9Sstevel@tonic-gate 		return;
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(ire);
21337c478bd9Sstevel@tonic-gate 	if (ill == NULL)
21347c478bd9Sstevel@tonic-gate 		return;
21357c478bd9Sstevel@tonic-gate 	ire_fastpath_list_add(ill, ire);
21367c478bd9Sstevel@tonic-gate 	res = ill_fastpath_probe(ill, ire->ire_dlureq_mp);
21377c478bd9Sstevel@tonic-gate 	/*
21387c478bd9Sstevel@tonic-gate 	 * EAGAIN is an indication of a transient error
21397c478bd9Sstevel@tonic-gate 	 * i.e. allocation failure etc. leave the ire in the list it will
21407c478bd9Sstevel@tonic-gate 	 * be updated when another probe happens for another ire if not
21417c478bd9Sstevel@tonic-gate 	 * it will be taken out of the list when the ire is deleted.
21427c478bd9Sstevel@tonic-gate 	 */
21437c478bd9Sstevel@tonic-gate 	if (res != 0 && res != EAGAIN)
21447c478bd9Sstevel@tonic-gate 		ire_fastpath_list_delete(ill, ire);
21457c478bd9Sstevel@tonic-gate }
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate /*
21487c478bd9Sstevel@tonic-gate  * Update all IRE's that are not in fastpath mode and
21497c478bd9Sstevel@tonic-gate  * have an dlureq_mp that matches mp. mp->b_cont contains
21507c478bd9Sstevel@tonic-gate  * the fastpath header.
21517c478bd9Sstevel@tonic-gate  *
21527c478bd9Sstevel@tonic-gate  * Returns TRUE if entry should be dequeued, or FALSE otherwise.
21537c478bd9Sstevel@tonic-gate  */
21547c478bd9Sstevel@tonic-gate boolean_t
21557c478bd9Sstevel@tonic-gate ire_fastpath_update(ire_t *ire, void *arg)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	mblk_t 	*mp, *fp_mp;
21587c478bd9Sstevel@tonic-gate 	uchar_t 	*up, *up2;
21597c478bd9Sstevel@tonic-gate 	ptrdiff_t	cmplen;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_type & (IRE_CACHE | IRE_BROADCAST |
21627c478bd9Sstevel@tonic-gate 	    IRE_MIPRTUN)) != 0);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	/*
21657c478bd9Sstevel@tonic-gate 	 * Already contains fastpath info or doesn't have
21667c478bd9Sstevel@tonic-gate 	 * DL_UNITDATA_REQ header.
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	if (ire->ire_fp_mp != NULL || ire->ire_dlureq_mp == NULL)
21697c478bd9Sstevel@tonic-gate 		return (B_TRUE);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_fastpath_update: trying\n"));
217245916cd2Sjpk 	mp = (mblk_t *)arg;
21737c478bd9Sstevel@tonic-gate 	up = mp->b_rptr;
21747c478bd9Sstevel@tonic-gate 	cmplen = mp->b_wptr - up;
21757c478bd9Sstevel@tonic-gate 	/* Serialize multiple fast path updates */
21767c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
21777c478bd9Sstevel@tonic-gate 	up2 = ire->ire_dlureq_mp->b_rptr;
21787c478bd9Sstevel@tonic-gate 	ASSERT(cmplen >= 0);
21797c478bd9Sstevel@tonic-gate 	if (ire->ire_dlureq_mp->b_wptr - up2 != cmplen ||
21807c478bd9Sstevel@tonic-gate 	    bcmp(up, up2, cmplen) != 0) {
21817c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
21827c478bd9Sstevel@tonic-gate 		/*
21837c478bd9Sstevel@tonic-gate 		 * Don't take the ire off the fastpath list yet,
21847c478bd9Sstevel@tonic-gate 		 * since the response may come later.
21857c478bd9Sstevel@tonic-gate 		 */
21867c478bd9Sstevel@tonic-gate 		return (B_FALSE);
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 	/* Matched - install mp as the ire_fp_mp */
21897c478bd9Sstevel@tonic-gate 	ip1dbg(("ire_fastpath_update: match\n"));
21907c478bd9Sstevel@tonic-gate 	fp_mp = dupb(mp->b_cont);
21917c478bd9Sstevel@tonic-gate 	if (fp_mp) {
21927c478bd9Sstevel@tonic-gate 		/*
21937c478bd9Sstevel@tonic-gate 		 * We checked ire_fp_mp above. Check it again with the
21947c478bd9Sstevel@tonic-gate 		 * lock. Update fp_mp only if it has not been done
21957c478bd9Sstevel@tonic-gate 		 * already.
21967c478bd9Sstevel@tonic-gate 		 */
21977c478bd9Sstevel@tonic-gate 		if (ire->ire_fp_mp == NULL) {
21987c478bd9Sstevel@tonic-gate 			/*
21997c478bd9Sstevel@tonic-gate 			 * ire_ll_hdr_length is just an optimization to
22007c478bd9Sstevel@tonic-gate 			 * store the length. It is used to return the
22017c478bd9Sstevel@tonic-gate 			 * fast path header length to the upper layers.
22027c478bd9Sstevel@tonic-gate 			 */
22037c478bd9Sstevel@tonic-gate 			ire->ire_fp_mp = fp_mp;
22047c478bd9Sstevel@tonic-gate 			ire->ire_ll_hdr_length =
22057c478bd9Sstevel@tonic-gate 			    (uint_t)(fp_mp->b_wptr - fp_mp->b_rptr);
22067c478bd9Sstevel@tonic-gate 		} else {
22077c478bd9Sstevel@tonic-gate 			freeb(fp_mp);
22087c478bd9Sstevel@tonic-gate 		}
22097c478bd9Sstevel@tonic-gate 	}
22107c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
22117c478bd9Sstevel@tonic-gate 	return (B_TRUE);
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate /*
22157c478bd9Sstevel@tonic-gate  * This function handles the DL_NOTE_FASTPATH_FLUSH notification from the
22167c478bd9Sstevel@tonic-gate  * driver.
22177c478bd9Sstevel@tonic-gate  */
22187c478bd9Sstevel@tonic-gate /* ARGSUSED */
22197c478bd9Sstevel@tonic-gate void
22207c478bd9Sstevel@tonic-gate ire_fastpath_flush(ire_t *ire, void *arg)
22217c478bd9Sstevel@tonic-gate {
22227c478bd9Sstevel@tonic-gate 	ill_t	*ill;
22237c478bd9Sstevel@tonic-gate 	int	res;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	/* No fastpath info? */
22267c478bd9Sstevel@tonic-gate 	if (ire->ire_fp_mp == NULL || ire->ire_dlureq_mp == NULL)
22277c478bd9Sstevel@tonic-gate 		return;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	/*
22307c478bd9Sstevel@tonic-gate 	 * Just remove the IRE if it is for non-broadcast dest.  Then
22317c478bd9Sstevel@tonic-gate 	 * we will create another one which will have the correct
22327c478bd9Sstevel@tonic-gate 	 * fastpath info.
22337c478bd9Sstevel@tonic-gate 	 */
22347c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
22357c478bd9Sstevel@tonic-gate 	case IRE_CACHE:
22367c478bd9Sstevel@tonic-gate 		ire_delete(ire);
22377c478bd9Sstevel@tonic-gate 		break;
22387c478bd9Sstevel@tonic-gate 	case IRE_MIPRTUN:
22397c478bd9Sstevel@tonic-gate 	case IRE_BROADCAST:
22407c478bd9Sstevel@tonic-gate 		/*
22417c478bd9Sstevel@tonic-gate 		 * We can't delete the ire since it is difficult to
22427c478bd9Sstevel@tonic-gate 		 * recreate these ire's without going through the
22437c478bd9Sstevel@tonic-gate 		 * ipif down/up dance. The ire_fp_mp is protected by the
22447c478bd9Sstevel@tonic-gate 		 * ire_lock in the case of IRE_MIPRTUN and IRE_BROADCAST.
22457c478bd9Sstevel@tonic-gate 		 * All access to ire_fp_mp in the case of these 2 ire types
22467c478bd9Sstevel@tonic-gate 		 * is protected by ire_lock.
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
22497c478bd9Sstevel@tonic-gate 		if (ire->ire_fp_mp != NULL) {
22507c478bd9Sstevel@tonic-gate 			freeb(ire->ire_fp_mp);
22517c478bd9Sstevel@tonic-gate 			ire->ire_fp_mp = NULL;
22527c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
22537c478bd9Sstevel@tonic-gate 			/*
22547c478bd9Sstevel@tonic-gate 			 * No fastpath probe if there is no stq i.e.
22557c478bd9Sstevel@tonic-gate 			 * i.e. the case of loopback broadcast ire.
22567c478bd9Sstevel@tonic-gate 			 */
22577c478bd9Sstevel@tonic-gate 			if (ire->ire_stq == NULL)
22587c478bd9Sstevel@tonic-gate 				break;
22597c478bd9Sstevel@tonic-gate 			ill = (ill_t *)((ire->ire_stq)->q_ptr);
22607c478bd9Sstevel@tonic-gate 			ire_fastpath_list_add(ill, ire);
22617c478bd9Sstevel@tonic-gate 			res = ill_fastpath_probe(ill, ire->ire_dlureq_mp);
22627c478bd9Sstevel@tonic-gate 			/*
22637c478bd9Sstevel@tonic-gate 			 * EAGAIN is an indication of a transient error
22647c478bd9Sstevel@tonic-gate 			 * i.e. allocation failure etc. leave the ire in the
22657c478bd9Sstevel@tonic-gate 			 * list it will be updated when another probe happens
22667c478bd9Sstevel@tonic-gate 			 * for another ire if not it will be taken out of the
22677c478bd9Sstevel@tonic-gate 			 * list when the ire is deleted.
22687c478bd9Sstevel@tonic-gate 			 */
22697c478bd9Sstevel@tonic-gate 			if (res != 0 && res != EAGAIN)
22707c478bd9Sstevel@tonic-gate 				ire_fastpath_list_delete(ill, ire);
22717c478bd9Sstevel@tonic-gate 		} else {
22727c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
22737c478bd9Sstevel@tonic-gate 		}
22747c478bd9Sstevel@tonic-gate 		break;
22757c478bd9Sstevel@tonic-gate 	default:
22767c478bd9Sstevel@tonic-gate 		/* This should not happen! */
22777c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_fastpath_flush: Wrong ire type %s\n",
22787c478bd9Sstevel@tonic-gate 		    ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type)));
22797c478bd9Sstevel@tonic-gate 		break;
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate /*
22847c478bd9Sstevel@tonic-gate  * Drain the list of ire's waiting for fastpath response.
22857c478bd9Sstevel@tonic-gate  */
22867c478bd9Sstevel@tonic-gate void
22877c478bd9Sstevel@tonic-gate ire_fastpath_list_dispatch(ill_t *ill, boolean_t (*func)(ire_t *, void *),
22887c478bd9Sstevel@tonic-gate     void *arg)
22897c478bd9Sstevel@tonic-gate {
22907c478bd9Sstevel@tonic-gate 	ire_t	 *next_ire;
22917c478bd9Sstevel@tonic-gate 	ire_t	 *current_ire;
22927c478bd9Sstevel@tonic-gate 	ire_t	 *first_ire;
22937c478bd9Sstevel@tonic-gate 	ire_t	 *prev_ire = NULL;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
22987c478bd9Sstevel@tonic-gate 	first_ire = current_ire = (ire_t *)ill->ill_fastpath_list;
22997c478bd9Sstevel@tonic-gate 	while (current_ire != (ire_t *)&ill->ill_fastpath_list) {
23007c478bd9Sstevel@tonic-gate 		next_ire = current_ire->ire_fastpath;
23017c478bd9Sstevel@tonic-gate 		/*
23027c478bd9Sstevel@tonic-gate 		 * Take it off the list if we're flushing, or if the callback
23037c478bd9Sstevel@tonic-gate 		 * routine tells us to do so.  Otherwise, leave the ire in the
23047c478bd9Sstevel@tonic-gate 		 * fastpath list to handle any pending response from the lower
23057c478bd9Sstevel@tonic-gate 		 * layer.  We can't drain the list when the callback routine
23067c478bd9Sstevel@tonic-gate 		 * comparison failed, because the response is asynchronous in
23077c478bd9Sstevel@tonic-gate 		 * nature, and may not arrive in the same order as the list
23087c478bd9Sstevel@tonic-gate 		 * insertion.
23097c478bd9Sstevel@tonic-gate 		 */
23107c478bd9Sstevel@tonic-gate 		if (func == NULL || func(current_ire, arg)) {
23117c478bd9Sstevel@tonic-gate 			current_ire->ire_fastpath = NULL;
23127c478bd9Sstevel@tonic-gate 			if (current_ire == first_ire)
23137c478bd9Sstevel@tonic-gate 				ill->ill_fastpath_list = first_ire = next_ire;
23147c478bd9Sstevel@tonic-gate 			else
23157c478bd9Sstevel@tonic-gate 				prev_ire->ire_fastpath = next_ire;
23167c478bd9Sstevel@tonic-gate 		} else {
23177c478bd9Sstevel@tonic-gate 			/* previous element that is still in the list */
23187c478bd9Sstevel@tonic-gate 			prev_ire = current_ire;
23197c478bd9Sstevel@tonic-gate 		}
23207c478bd9Sstevel@tonic-gate 		current_ire = next_ire;
23217c478bd9Sstevel@tonic-gate 	}
23227c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
23237c478bd9Sstevel@tonic-gate }
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate /*
23267c478bd9Sstevel@tonic-gate  * Add ire to the ire fastpath list.
23277c478bd9Sstevel@tonic-gate  */
23287c478bd9Sstevel@tonic-gate static void
23297c478bd9Sstevel@tonic-gate ire_fastpath_list_add(ill_t *ill, ire_t *ire)
23307c478bd9Sstevel@tonic-gate {
23317c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
23327c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_stq != NULL);
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	rw_enter(&ire->ire_bucket->irb_lock, RW_READER);
23357c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	/*
23387c478bd9Sstevel@tonic-gate 	 * if ire has not been deleted and
23397c478bd9Sstevel@tonic-gate 	 * is not already in the list add it.
23407c478bd9Sstevel@tonic-gate 	 */
23417c478bd9Sstevel@tonic-gate 	if (((ire->ire_marks & IRE_MARK_CONDEMNED) == 0) &&
23427c478bd9Sstevel@tonic-gate 	    (ire->ire_fastpath == NULL)) {
23437c478bd9Sstevel@tonic-gate 		ire->ire_fastpath = (ire_t *)ill->ill_fastpath_list;
23447c478bd9Sstevel@tonic-gate 		ill->ill_fastpath_list = ire;
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
23487c478bd9Sstevel@tonic-gate 	rw_exit(&ire->ire_bucket->irb_lock);
23497c478bd9Sstevel@tonic-gate }
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate /*
23527c478bd9Sstevel@tonic-gate  * remove ire from the ire fastpath list.
23537c478bd9Sstevel@tonic-gate  */
23547c478bd9Sstevel@tonic-gate void
23557c478bd9Sstevel@tonic-gate ire_fastpath_list_delete(ill_t *ill, ire_t *ire)
23567c478bd9Sstevel@tonic-gate {
23577c478bd9Sstevel@tonic-gate 	ire_t	*ire_ptr;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_stq != NULL && ill != NULL);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
23627c478bd9Sstevel@tonic-gate 	if (ire->ire_fastpath == NULL)
23637c478bd9Sstevel@tonic-gate 		goto done;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_fastpath_list != &ill->ill_fastpath_list);
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	if (ill->ill_fastpath_list == ire) {
23687c478bd9Sstevel@tonic-gate 		ill->ill_fastpath_list = ire->ire_fastpath;
23697c478bd9Sstevel@tonic-gate 	} else {
23707c478bd9Sstevel@tonic-gate 		ire_ptr = ill->ill_fastpath_list;
23717c478bd9Sstevel@tonic-gate 		while (ire_ptr != (ire_t *)&ill->ill_fastpath_list) {
23727c478bd9Sstevel@tonic-gate 			if (ire_ptr->ire_fastpath == ire) {
23737c478bd9Sstevel@tonic-gate 				ire_ptr->ire_fastpath = ire->ire_fastpath;
23747c478bd9Sstevel@tonic-gate 				break;
23757c478bd9Sstevel@tonic-gate 			}
23767c478bd9Sstevel@tonic-gate 			ire_ptr = ire_ptr->ire_fastpath;
23777c478bd9Sstevel@tonic-gate 		}
23787c478bd9Sstevel@tonic-gate 	}
23797c478bd9Sstevel@tonic-gate 	ire->ire_fastpath = NULL;
23807c478bd9Sstevel@tonic-gate done:
23817c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
23827c478bd9Sstevel@tonic-gate }
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate /*
23867c478bd9Sstevel@tonic-gate  * Find an IRE_INTERFACE for the multicast group.
23877c478bd9Sstevel@tonic-gate  * Allows different routes for multicast addresses
23887c478bd9Sstevel@tonic-gate  * in the unicast routing table (akin to 224.0.0.0 but could be more specific)
23897c478bd9Sstevel@tonic-gate  * which point at different interfaces. This is used when IP_MULTICAST_IF
23907c478bd9Sstevel@tonic-gate  * isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
23917c478bd9Sstevel@tonic-gate  * specify the interface to join on.
23927c478bd9Sstevel@tonic-gate  *
23937c478bd9Sstevel@tonic-gate  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
23947c478bd9Sstevel@tonic-gate  */
23957c478bd9Sstevel@tonic-gate ire_t *
23967c478bd9Sstevel@tonic-gate ire_lookup_multi(ipaddr_t group, zoneid_t zoneid)
23977c478bd9Sstevel@tonic-gate {
23987c478bd9Sstevel@tonic-gate 	ire_t	*ire;
23997c478bd9Sstevel@tonic-gate 	ipif_t	*ipif = NULL;
24007c478bd9Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE;
24017c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
240445916cd2Sjpk 	    0, NULL, MATCH_IRE_DEFAULT);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	/* We search a resolvable ire in case of multirouting. */
24077c478bd9Sstevel@tonic-gate 	if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
24087c478bd9Sstevel@tonic-gate 		ire_t *cire = NULL;
24097c478bd9Sstevel@tonic-gate 		/*
24107c478bd9Sstevel@tonic-gate 		 * If the route is not resolvable, the looked up ire
24117c478bd9Sstevel@tonic-gate 		 * may be changed here. In that case, ire_multirt_lookup()
24127c478bd9Sstevel@tonic-gate 		 * IRE_REFRELE the original ire and change it.
24137c478bd9Sstevel@tonic-gate 		 */
241445916cd2Sjpk 		(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW, NULL);
24157c478bd9Sstevel@tonic-gate 		if (cire != NULL)
24167c478bd9Sstevel@tonic-gate 			ire_refrele(cire);
24177c478bd9Sstevel@tonic-gate 	}
24187c478bd9Sstevel@tonic-gate 	if (ire == NULL)
24197c478bd9Sstevel@tonic-gate 		return (NULL);
24207c478bd9Sstevel@tonic-gate 	/*
24217c478bd9Sstevel@tonic-gate 	 * Make sure we follow ire_ipif.
24227c478bd9Sstevel@tonic-gate 	 *
24237c478bd9Sstevel@tonic-gate 	 * We need to determine the interface route through
24247c478bd9Sstevel@tonic-gate 	 * which the gateway will be reached. We don't really
24257c478bd9Sstevel@tonic-gate 	 * care which interface is picked if the interface is
24267c478bd9Sstevel@tonic-gate 	 * part of a group.
24277c478bd9Sstevel@tonic-gate 	 */
24287c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
24297c478bd9Sstevel@tonic-gate 		ipif = ire->ire_ipif;
24307c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
24317c478bd9Sstevel@tonic-gate 	}
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
24347c478bd9Sstevel@tonic-gate 	case IRE_DEFAULT:
24357c478bd9Sstevel@tonic-gate 	case IRE_PREFIX:
24367c478bd9Sstevel@tonic-gate 	case IRE_HOST:
24377c478bd9Sstevel@tonic-gate 		gw_addr = ire->ire_gateway_addr;
24387c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
24397c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(gw_addr, 0, 0,
24407c478bd9Sstevel@tonic-gate 		    IRE_INTERFACE, ipif, NULL, zoneid, 0,
244145916cd2Sjpk 		    NULL, match_flags);
24427c478bd9Sstevel@tonic-gate 		return (ire);
24437c478bd9Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
24447c478bd9Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
24457c478bd9Sstevel@tonic-gate 		return (ire);
24467c478bd9Sstevel@tonic-gate 	default:
24477c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
24487c478bd9Sstevel@tonic-gate 		return (NULL);
24497c478bd9Sstevel@tonic-gate 	}
24507c478bd9Sstevel@tonic-gate }
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate /*
24537c478bd9Sstevel@tonic-gate  * Return any local address.  We use this to target ourselves
24547c478bd9Sstevel@tonic-gate  * when the src address was specified as 'default'.
24557c478bd9Sstevel@tonic-gate  * Preference for IRE_LOCAL entries.
24567c478bd9Sstevel@tonic-gate  */
24577c478bd9Sstevel@tonic-gate ire_t *
24587c478bd9Sstevel@tonic-gate ire_lookup_local(zoneid_t zoneid)
24597c478bd9Sstevel@tonic-gate {
24607c478bd9Sstevel@tonic-gate 	ire_t	*ire;
24617c478bd9Sstevel@tonic-gate 	irb_t	*irb;
24627c478bd9Sstevel@tonic-gate 	ire_t	*maybe = NULL;
24637c478bd9Sstevel@tonic-gate 	int i;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip_cache_table_size;  i++) {
24667c478bd9Sstevel@tonic-gate 		irb = &ip_cache_table[i];
24677c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
24687c478bd9Sstevel@tonic-gate 			continue;
24697c478bd9Sstevel@tonic-gate 		rw_enter(&irb->irb_lock, RW_READER);
24707c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
24717c478bd9Sstevel@tonic-gate 			if ((ire->ire_marks & IRE_MARK_CONDEMNED) ||
247245916cd2Sjpk 			    (ire->ire_zoneid != zoneid &&
247345916cd2Sjpk 			    ire->ire_zoneid != ALL_ZONES))
24747c478bd9Sstevel@tonic-gate 				continue;
24757c478bd9Sstevel@tonic-gate 			switch (ire->ire_type) {
24767c478bd9Sstevel@tonic-gate 			case IRE_LOOPBACK:
24777c478bd9Sstevel@tonic-gate 				if (maybe == NULL) {
24787c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
24797c478bd9Sstevel@tonic-gate 					maybe = ire;
24807c478bd9Sstevel@tonic-gate 				}
24817c478bd9Sstevel@tonic-gate 				break;
24827c478bd9Sstevel@tonic-gate 			case IRE_LOCAL:
24837c478bd9Sstevel@tonic-gate 				if (maybe != NULL) {
24847c478bd9Sstevel@tonic-gate 					ire_refrele(maybe);
24857c478bd9Sstevel@tonic-gate 				}
24867c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
24877c478bd9Sstevel@tonic-gate 				rw_exit(&irb->irb_lock);
24887c478bd9Sstevel@tonic-gate 				return (ire);
24897c478bd9Sstevel@tonic-gate 			}
24907c478bd9Sstevel@tonic-gate 		}
24917c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
24927c478bd9Sstevel@tonic-gate 	}
24937c478bd9Sstevel@tonic-gate 	return (maybe);
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate /*
24977c478bd9Sstevel@tonic-gate  * If the specified IRE is associated with a particular ILL, return
24987c478bd9Sstevel@tonic-gate  * that ILL pointer (May be called as writer.).
24997c478bd9Sstevel@tonic-gate  *
25007c478bd9Sstevel@tonic-gate  * NOTE : This is not a generic function that can be used always.
25017c478bd9Sstevel@tonic-gate  * This function always returns the ill of the outgoing packets
25027c478bd9Sstevel@tonic-gate  * if this ire is used.
25037c478bd9Sstevel@tonic-gate  */
25047c478bd9Sstevel@tonic-gate ill_t *
250545916cd2Sjpk ire_to_ill(const ire_t *ire)
25067c478bd9Sstevel@tonic-gate {
25077c478bd9Sstevel@tonic-gate 	ill_t *ill = NULL;
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	/*
25107c478bd9Sstevel@tonic-gate 	 * 1) For an IRE_CACHE, ire_ipif is the one where it obtained
25117c478bd9Sstevel@tonic-gate 	 *    the source address from. ire_stq is the one where the
25127c478bd9Sstevel@tonic-gate 	 *    packets will be sent out on. We return that here.
25137c478bd9Sstevel@tonic-gate 	 *
25147c478bd9Sstevel@tonic-gate 	 * 2) IRE_BROADCAST normally has a loopback and a non-loopback
25157c478bd9Sstevel@tonic-gate 	 *    copy and they always exist next to each other with loopback
25167c478bd9Sstevel@tonic-gate 	 *    copy being the first one. If we are called on the non-loopback
25177c478bd9Sstevel@tonic-gate 	 *    copy, return the one pointed by ire_stq. If it was called on
25187c478bd9Sstevel@tonic-gate 	 *    a loopback copy, we still return the one pointed by the next
25197c478bd9Sstevel@tonic-gate 	 *    ire's ire_stq pointer i.e the one pointed by the non-loopback
25207c478bd9Sstevel@tonic-gate 	 *    copy. We don't want use ire_ipif as it might represent the
25217c478bd9Sstevel@tonic-gate 	 *    source address (if we borrow source addresses for
25227c478bd9Sstevel@tonic-gate 	 *    IRE_BROADCASTS in the future).
25237c478bd9Sstevel@tonic-gate 	 *    However if an interface is currently coming up, the above
25247c478bd9Sstevel@tonic-gate 	 *    condition may not hold during that period since the ires
25257c478bd9Sstevel@tonic-gate 	 *    are added one at a time. Thus one of the pair could have been
25267c478bd9Sstevel@tonic-gate 	 *    added and the other not yet added.
25277c478bd9Sstevel@tonic-gate 	 * 3) For all others return the ones pointed by ire_ipif->ipif_ill.
25287c478bd9Sstevel@tonic-gate 	 */
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
25317c478bd9Sstevel@tonic-gate 		ill = (ill_t *)ire->ire_stq->q_ptr;
25327c478bd9Sstevel@tonic-gate 	} else if (ire->ire_type == IRE_BROADCAST) {
25337c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
25347c478bd9Sstevel@tonic-gate 			ill = (ill_t *)ire->ire_stq->q_ptr;
25357c478bd9Sstevel@tonic-gate 		} else {
25367c478bd9Sstevel@tonic-gate 			ire_t  *ire_next;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 			ire_next = ire->ire_next;
25397c478bd9Sstevel@tonic-gate 			if (ire_next != NULL &&
25407c478bd9Sstevel@tonic-gate 			    ire_next->ire_type == IRE_BROADCAST &&
25417c478bd9Sstevel@tonic-gate 			    ire_next->ire_addr == ire->ire_addr &&
25427c478bd9Sstevel@tonic-gate 			    ire_next->ire_ipif == ire->ire_ipif) {
25437c478bd9Sstevel@tonic-gate 				ill = (ill_t *)ire_next->ire_stq->q_ptr;
25447c478bd9Sstevel@tonic-gate 			}
25457c478bd9Sstevel@tonic-gate 		}
25467c478bd9Sstevel@tonic-gate 	} else if (ire->ire_ipif != NULL) {
25477c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
25487c478bd9Sstevel@tonic-gate 	}
25497c478bd9Sstevel@tonic-gate 	return (ill);
25507c478bd9Sstevel@tonic-gate }
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate /* Arrange to call the specified function for every IRE in the world. */
25537c478bd9Sstevel@tonic-gate void
255445916cd2Sjpk ire_walk(pfv_t func, void *arg)
25557c478bd9Sstevel@tonic-gate {
25567c478bd9Sstevel@tonic-gate 	ire_walk_ipvers(func, arg, 0, ALL_ZONES);
25577c478bd9Sstevel@tonic-gate }
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate void
256045916cd2Sjpk ire_walk_v4(pfv_t func, void *arg, zoneid_t zoneid)
25617c478bd9Sstevel@tonic-gate {
25627c478bd9Sstevel@tonic-gate 	ire_walk_ipvers(func, arg, IPV4_VERSION, zoneid);
25637c478bd9Sstevel@tonic-gate }
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate void
256645916cd2Sjpk ire_walk_v6(pfv_t func, void *arg, zoneid_t zoneid)
25677c478bd9Sstevel@tonic-gate {
25687c478bd9Sstevel@tonic-gate 	ire_walk_ipvers(func, arg, IPV6_VERSION, zoneid);
25697c478bd9Sstevel@tonic-gate }
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate /*
25727c478bd9Sstevel@tonic-gate  * Walk a particular version. version == 0 means both v4 and v6.
25737c478bd9Sstevel@tonic-gate  */
25747c478bd9Sstevel@tonic-gate static void
257545916cd2Sjpk ire_walk_ipvers(pfv_t func, void *arg, uchar_t vers, zoneid_t zoneid)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
25787c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP_MASK_TABLE_SIZE,
25797c478bd9Sstevel@tonic-gate 		    ip_ftable_hash_size, ip_forwarding_table,
25807c478bd9Sstevel@tonic-gate 		    ip_cache_table_size, ip_cache_table, NULL, zoneid);
25817c478bd9Sstevel@tonic-gate 	}
25827c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
25837c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(0, 0, func, arg, IP6_MASK_TABLE_SIZE,
25847c478bd9Sstevel@tonic-gate 		    ip6_ftable_hash_size, ip_forwarding_table_v6,
25857c478bd9Sstevel@tonic-gate 		    ip6_cache_table_size, ip_cache_table_v6, NULL, zoneid);
25867c478bd9Sstevel@tonic-gate 	}
25877c478bd9Sstevel@tonic-gate }
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate /*
25907c478bd9Sstevel@tonic-gate  * Arrange to call the specified
25917c478bd9Sstevel@tonic-gate  * function for every IRE that matches the ill.
25927c478bd9Sstevel@tonic-gate  */
25937c478bd9Sstevel@tonic-gate void
259445916cd2Sjpk ire_walk_ill(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
25957c478bd9Sstevel@tonic-gate     ill_t *ill)
25967c478bd9Sstevel@tonic-gate {
25977c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, 0, ill);
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate void
260145916cd2Sjpk ire_walk_ill_v4(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
26027c478bd9Sstevel@tonic-gate     ill_t *ill)
26037c478bd9Sstevel@tonic-gate {
26047c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV4_VERSION,
26057c478bd9Sstevel@tonic-gate 	    ill);
26067c478bd9Sstevel@tonic-gate }
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate void
260945916cd2Sjpk ire_walk_ill_v6(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
26107c478bd9Sstevel@tonic-gate     ill_t *ill)
26117c478bd9Sstevel@tonic-gate {
26127c478bd9Sstevel@tonic-gate 	ire_walk_ill_ipvers(match_flags, ire_type, func, arg, IPV6_VERSION,
26137c478bd9Sstevel@tonic-gate 	    ill);
26147c478bd9Sstevel@tonic-gate }
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate /*
26177c478bd9Sstevel@tonic-gate  * Walk a particular ill and version. version == 0 means both v4 and v6.
26187c478bd9Sstevel@tonic-gate  */
26197c478bd9Sstevel@tonic-gate static void
26207c478bd9Sstevel@tonic-gate ire_walk_ill_ipvers(uint_t match_flags, uint_t ire_type, pfv_t func,
262145916cd2Sjpk     void *arg, uchar_t vers, ill_t *ill)
26227c478bd9Sstevel@tonic-gate {
26237c478bd9Sstevel@tonic-gate 	if (vers != IPV6_VERSION) {
26247c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
26257c478bd9Sstevel@tonic-gate 		    IP_MASK_TABLE_SIZE, ip_ftable_hash_size,
26267c478bd9Sstevel@tonic-gate 		    ip_forwarding_table, ip_cache_table_size,
26277c478bd9Sstevel@tonic-gate 		    ip_cache_table, ill, ALL_ZONES);
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 	if (vers != IPV4_VERSION) {
26307c478bd9Sstevel@tonic-gate 		ire_walk_ill_tables(match_flags, ire_type, func, arg,
26317c478bd9Sstevel@tonic-gate 		    IP6_MASK_TABLE_SIZE, ip6_ftable_hash_size,
26327c478bd9Sstevel@tonic-gate 		    ip_forwarding_table_v6, ip6_cache_table_size,
26337c478bd9Sstevel@tonic-gate 		    ip_cache_table_v6, ill, ALL_ZONES);
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate }
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate static boolean_t
26387c478bd9Sstevel@tonic-gate ire_walk_ill_match(uint_t match_flags, uint_t ire_type, ire_t *ire,
26397c478bd9Sstevel@tonic-gate     ill_t *ill, zoneid_t zoneid)
26407c478bd9Sstevel@tonic-gate {
26417c478bd9Sstevel@tonic-gate 	ill_t *ire_stq_ill = NULL;
26427c478bd9Sstevel@tonic-gate 	ill_t *ire_ipif_ill = NULL;
26437c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	ASSERT(match_flags != 0 || zoneid != ALL_ZONES);
26467c478bd9Sstevel@tonic-gate 	/*
26477c478bd9Sstevel@tonic-gate 	 * 1) MATCH_IRE_WQ : Used specifically to match on ire_stq.
26487c478bd9Sstevel@tonic-gate 	 *    The fast path update uses this to make sure it does not
26497c478bd9Sstevel@tonic-gate 	 *    update the fast path header of interface X with the fast
26507c478bd9Sstevel@tonic-gate 	 *    path updates it recieved on interface Y.  It is similar
26517c478bd9Sstevel@tonic-gate 	 *    in handling DL_NOTE_FASTPATH_FLUSH.
26527c478bd9Sstevel@tonic-gate 	 *
26537c478bd9Sstevel@tonic-gate 	 * 2) MATCH_IRE_ILL/MATCH_IRE_ILL_GROUP : We match both on ill
26547c478bd9Sstevel@tonic-gate 	 *    pointed by ire_stq and ire_ipif. Only in the case of
26557c478bd9Sstevel@tonic-gate 	 *    IRE_CACHEs can ire_stq and ire_ipif be pointing to
26567c478bd9Sstevel@tonic-gate 	 *    different ills. But we want to keep this function generic
26577c478bd9Sstevel@tonic-gate 	 *    enough for future use. So, we always try to match on both.
26587c478bd9Sstevel@tonic-gate 	 *    The only caller of this function ire_walk_ill_tables, will
26597c478bd9Sstevel@tonic-gate 	 *    call "func" after we return from this function. We expect
26607c478bd9Sstevel@tonic-gate 	 *    "func" to do the right filtering of ires in this case.
26617c478bd9Sstevel@tonic-gate 	 *
26627c478bd9Sstevel@tonic-gate 	 * NOTE : In the case of MATCH_IRE_ILL_GROUP, groups
26637c478bd9Sstevel@tonic-gate 	 * pointed by ire_stq and ire_ipif should always be the same.
26647c478bd9Sstevel@tonic-gate 	 * So, we just match on only one of them.
26657c478bd9Sstevel@tonic-gate 	 */
26667c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
26677c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL)
26687c478bd9Sstevel@tonic-gate 			ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
26697c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL)
26707c478bd9Sstevel@tonic-gate 			ire_ipif_ill = ire->ire_ipif->ipif_ill;
26717c478bd9Sstevel@tonic-gate 		if (ire_stq_ill != NULL)
26727c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_stq_ill->ill_group;
26737c478bd9Sstevel@tonic-gate 		if ((ire_ill_group == NULL) && (ire_ipif_ill != NULL))
26747c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ipif_ill->ill_group;
26757c478bd9Sstevel@tonic-gate 	}
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	if (zoneid != ALL_ZONES) {
26787c478bd9Sstevel@tonic-gate 		/*
26797c478bd9Sstevel@tonic-gate 		 * We're walking the IREs for a specific zone. The only relevant
26807c478bd9Sstevel@tonic-gate 		 * IREs are:
26817c478bd9Sstevel@tonic-gate 		 * - all IREs with a matching ire_zoneid
26827c478bd9Sstevel@tonic-gate 		 * - all IRE_OFFSUBNETs as they're shared across all zones
26837c478bd9Sstevel@tonic-gate 		 * - IRE_INTERFACE IREs for interfaces with a usable source addr
26847c478bd9Sstevel@tonic-gate 		 *   with a matching zone
26857c478bd9Sstevel@tonic-gate 		 * - IRE_DEFAULTs with a gateway reachable from the zone
26867c478bd9Sstevel@tonic-gate 		 * We should really match on IRE_OFFSUBNETs and IRE_DEFAULTs
26877c478bd9Sstevel@tonic-gate 		 * using the same rule; but the above rules are consistent with
26887c478bd9Sstevel@tonic-gate 		 * the behavior of ire_ftable_lookup[_v6]() so that all the
26897c478bd9Sstevel@tonic-gate 		 * routes that can be matched during lookup are also matched
26907c478bd9Sstevel@tonic-gate 		 * here.
26917c478bd9Sstevel@tonic-gate 		 */
269245916cd2Sjpk 		if (zoneid != ire->ire_zoneid && ire->ire_zoneid != ALL_ZONES) {
26937c478bd9Sstevel@tonic-gate 			/*
26947c478bd9Sstevel@tonic-gate 			 * Note, IRE_INTERFACE can have the stq as NULL. For
26957c478bd9Sstevel@tonic-gate 			 * example, if the default multicast route is tied to
26967c478bd9Sstevel@tonic-gate 			 * the loopback address.
26977c478bd9Sstevel@tonic-gate 			 */
26987c478bd9Sstevel@tonic-gate 			if ((ire->ire_type & IRE_INTERFACE) &&
26997c478bd9Sstevel@tonic-gate 			    (ire->ire_stq != NULL)) {
27007c478bd9Sstevel@tonic-gate 				ire_stq_ill = (ill_t *)ire->ire_stq->q_ptr;
27017c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV4_VERSION) {
27027c478bd9Sstevel@tonic-gate 					if (!ipif_usesrc_avail(ire_stq_ill,
27037c478bd9Sstevel@tonic-gate 					    zoneid))
27047c478bd9Sstevel@tonic-gate 						/* No usable src addr in zone */
27057c478bd9Sstevel@tonic-gate 						return (B_FALSE);
27067c478bd9Sstevel@tonic-gate 				} else if (ire_stq_ill->ill_usesrc_ifindex
27077c478bd9Sstevel@tonic-gate 				    != 0) {
27087c478bd9Sstevel@tonic-gate 					/*
27097c478bd9Sstevel@tonic-gate 					 * For IPv6 use ipif_select_source_v6()
27107c478bd9Sstevel@tonic-gate 					 * so the right scope selection is done
27117c478bd9Sstevel@tonic-gate 					 */
27127c478bd9Sstevel@tonic-gate 					ipif_t *src_ipif;
27137c478bd9Sstevel@tonic-gate 					src_ipif =
27147c478bd9Sstevel@tonic-gate 					    ipif_select_source_v6(ire_stq_ill,
2715309fa174Srk129064 					    &ire->ire_addr_v6, RESTRICT_TO_NONE,
27167c478bd9Sstevel@tonic-gate 					    IPV6_PREFER_SRC_DEFAULT,
27177c478bd9Sstevel@tonic-gate 					    zoneid);
27187c478bd9Sstevel@tonic-gate 					if (src_ipif != NULL) {
27197c478bd9Sstevel@tonic-gate 						ipif_refrele(src_ipif);
27207c478bd9Sstevel@tonic-gate 					} else {
27217c478bd9Sstevel@tonic-gate 						return (B_FALSE);
27227c478bd9Sstevel@tonic-gate 					}
27237c478bd9Sstevel@tonic-gate 				} else {
27247c478bd9Sstevel@tonic-gate 					return (B_FALSE);
27257c478bd9Sstevel@tonic-gate 				}
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 			} else if (!(ire->ire_type & IRE_OFFSUBNET)) {
27287c478bd9Sstevel@tonic-gate 				return (B_FALSE);
27297c478bd9Sstevel@tonic-gate 			}
27307c478bd9Sstevel@tonic-gate 		}
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 		/*
27337c478bd9Sstevel@tonic-gate 		 * Match all default routes from the global zone, irrespective
27347c478bd9Sstevel@tonic-gate 		 * of reachability.
27357c478bd9Sstevel@tonic-gate 		 */
27367c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_DEFAULT && zoneid != GLOBAL_ZONEID) {
27377c478bd9Sstevel@tonic-gate 			int ire_match_flags = 0;
27387c478bd9Sstevel@tonic-gate 			in6_addr_t gw_addr_v6;
27397c478bd9Sstevel@tonic-gate 			ire_t *rire;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 			if (ire->ire_ipif != NULL) {
27427c478bd9Sstevel@tonic-gate 				ire_match_flags |= MATCH_IRE_ILL_GROUP;
27437c478bd9Sstevel@tonic-gate 			}
27447c478bd9Sstevel@tonic-gate 			if (ire->ire_ipversion == IPV4_VERSION) {
27457c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup(ire->ire_gateway_addr,
274645916cd2Sjpk 				    0, 0, 0, ire->ire_ipif, NULL, zoneid, NULL,
27477c478bd9Sstevel@tonic-gate 				    ire_match_flags);
27487c478bd9Sstevel@tonic-gate 			} else {
27497c478bd9Sstevel@tonic-gate 				ASSERT(ire->ire_ipversion == IPV6_VERSION);
27507c478bd9Sstevel@tonic-gate 				mutex_enter(&ire->ire_lock);
27517c478bd9Sstevel@tonic-gate 				gw_addr_v6 = ire->ire_gateway_addr_v6;
27527c478bd9Sstevel@tonic-gate 				mutex_exit(&ire->ire_lock);
27537c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup_v6(&gw_addr_v6,
27547c478bd9Sstevel@tonic-gate 				    NULL, NULL, 0, ire->ire_ipif, NULL, zoneid,
275545916cd2Sjpk 				    NULL, ire_match_flags);
27567c478bd9Sstevel@tonic-gate 			}
27577c478bd9Sstevel@tonic-gate 			if (rire == NULL) {
27587c478bd9Sstevel@tonic-gate 				return (B_FALSE);
27597c478bd9Sstevel@tonic-gate 			}
27607c478bd9Sstevel@tonic-gate 			ire_refrele(rire);
27617c478bd9Sstevel@tonic-gate 		}
27627c478bd9Sstevel@tonic-gate 	}
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	if (((!(match_flags & MATCH_IRE_TYPE)) ||
27657c478bd9Sstevel@tonic-gate 		(ire->ire_type & ire_type)) &&
27667c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_WQ)) ||
27677c478bd9Sstevel@tonic-gate 		(ire->ire_stq == ill->ill_wq)) &&
27687c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
27697c478bd9Sstevel@tonic-gate 		(ire_stq_ill == ill || ire_ipif_ill == ill)) &&
27707c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
27717c478bd9Sstevel@tonic-gate 		(ire_stq_ill == ill) || (ire_ipif_ill == ill) ||
27727c478bd9Sstevel@tonic-gate 		(ire_ill_group != NULL &&
27737c478bd9Sstevel@tonic-gate 		ire_ill_group == ill->ill_group))) {
27747c478bd9Sstevel@tonic-gate 		return (B_TRUE);
27757c478bd9Sstevel@tonic-gate 	}
27767c478bd9Sstevel@tonic-gate 	return (B_FALSE);
27777c478bd9Sstevel@tonic-gate }
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate /*
27807c478bd9Sstevel@tonic-gate  * Walk the ftable and the ctable entries that match the ill.
27817c478bd9Sstevel@tonic-gate  */
27827c478bd9Sstevel@tonic-gate static void
27837c478bd9Sstevel@tonic-gate ire_walk_ill_tables(uint_t match_flags, uint_t ire_type, pfv_t func,
278445916cd2Sjpk     void *arg, size_t ftbl_sz, size_t htbl_sz, irb_t **ipftbl,
27857c478bd9Sstevel@tonic-gate     size_t ctbl_sz, irb_t *ipctbl, ill_t *ill, zoneid_t zoneid)
27867c478bd9Sstevel@tonic-gate {
27877c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
27887c478bd9Sstevel@tonic-gate 	irb_t	*irb;
27897c478bd9Sstevel@tonic-gate 	ire_t	*ire;
27907c478bd9Sstevel@tonic-gate 	int i, j;
27917c478bd9Sstevel@tonic-gate 	boolean_t ret;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
27947c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
27957c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_TYPE) || (ire_type != 0));
27967c478bd9Sstevel@tonic-gate 	/*
27977c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the forwarding table if there
27987c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_FORWARDTABLE
27997c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
28007c478bd9Sstevel@tonic-gate 	 */
28017c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
28027c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_FORWARDTABLE) != 0)) {
28037c478bd9Sstevel@tonic-gate 		for (i = (ftbl_sz - 1);  i >= 0; i--) {
28047c478bd9Sstevel@tonic-gate 			if ((irb_ptr = ipftbl[i]) == NULL)
28057c478bd9Sstevel@tonic-gate 				continue;
28067c478bd9Sstevel@tonic-gate 			for (j = 0; j < htbl_sz; j++) {
28077c478bd9Sstevel@tonic-gate 				irb = &irb_ptr[j];
28087c478bd9Sstevel@tonic-gate 				if (irb->irb_ire == NULL)
28097c478bd9Sstevel@tonic-gate 					continue;
28107c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(irb);
28117c478bd9Sstevel@tonic-gate 				for (ire = irb->irb_ire; ire != NULL;
28127c478bd9Sstevel@tonic-gate 				    ire = ire->ire_next) {
28137c478bd9Sstevel@tonic-gate 					if (match_flags == 0 &&
28147c478bd9Sstevel@tonic-gate 					    zoneid == ALL_ZONES) {
28157c478bd9Sstevel@tonic-gate 						ret = B_TRUE;
28167c478bd9Sstevel@tonic-gate 					} else {
28177c478bd9Sstevel@tonic-gate 						ret = ire_walk_ill_match(
28187c478bd9Sstevel@tonic-gate 						    match_flags, ire_type,
28197c478bd9Sstevel@tonic-gate 						    ire, ill, zoneid);
28207c478bd9Sstevel@tonic-gate 					}
28217c478bd9Sstevel@tonic-gate 					if (ret)
28227c478bd9Sstevel@tonic-gate 						(*func)(ire, arg);
28237c478bd9Sstevel@tonic-gate 				}
28247c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
28257c478bd9Sstevel@tonic-gate 			}
28267c478bd9Sstevel@tonic-gate 		}
28277c478bd9Sstevel@tonic-gate 	}
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	/*
28307c478bd9Sstevel@tonic-gate 	 * Optimize by not looking at the cache table if there
28317c478bd9Sstevel@tonic-gate 	 * is a MATCH_IRE_TYPE specified with no IRE_CACHETABLE
28327c478bd9Sstevel@tonic-gate 	 * specified in ire_type.
28337c478bd9Sstevel@tonic-gate 	 */
28347c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_TYPE) ||
28357c478bd9Sstevel@tonic-gate 	    ((ire_type & IRE_CACHETABLE) != 0)) {
28367c478bd9Sstevel@tonic-gate 		for (i = 0; i < ctbl_sz;  i++) {
28377c478bd9Sstevel@tonic-gate 			irb = &ipctbl[i];
28387c478bd9Sstevel@tonic-gate 			if (irb->irb_ire == NULL)
28397c478bd9Sstevel@tonic-gate 				continue;
28407c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
28417c478bd9Sstevel@tonic-gate 			for (ire = irb->irb_ire; ire != NULL;
28427c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
28437c478bd9Sstevel@tonic-gate 				if (match_flags == 0 && zoneid == ALL_ZONES) {
28447c478bd9Sstevel@tonic-gate 					ret = B_TRUE;
28457c478bd9Sstevel@tonic-gate 				} else {
28467c478bd9Sstevel@tonic-gate 					ret = ire_walk_ill_match(
28477c478bd9Sstevel@tonic-gate 					    match_flags, ire_type,
28487c478bd9Sstevel@tonic-gate 					    ire, ill, zoneid);
28497c478bd9Sstevel@tonic-gate 				}
28507c478bd9Sstevel@tonic-gate 				if (ret)
28517c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
28527c478bd9Sstevel@tonic-gate 			}
28537c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
28547c478bd9Sstevel@tonic-gate 		}
28557c478bd9Sstevel@tonic-gate 	}
28567c478bd9Sstevel@tonic-gate }
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate /*
28597c478bd9Sstevel@tonic-gate  * This routine walks through the ill chain to find if there is any
28607c478bd9Sstevel@tonic-gate  * ire linked to the ill's interface based forwarding table
28617c478bd9Sstevel@tonic-gate  * The arg could be ill or mp. This routine is called when a ill goes
28627c478bd9Sstevel@tonic-gate  * down/deleted or the 'ipv4_ire_srcif_status' report is printed.
28637c478bd9Sstevel@tonic-gate  */
28647c478bd9Sstevel@tonic-gate void
286545916cd2Sjpk ire_walk_srcif_table_v4(pfv_t func, void *arg)
28667c478bd9Sstevel@tonic-gate {
28677c478bd9Sstevel@tonic-gate 	irb_t   *irb;
28687c478bd9Sstevel@tonic-gate 	ire_t   *ire;
28697c478bd9Sstevel@tonic-gate 	ill_t	*ill, *next_ill;
28707c478bd9Sstevel@tonic-gate 	int	i;
28717c478bd9Sstevel@tonic-gate 	int	total_count;
28727c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	/*
28757c478bd9Sstevel@tonic-gate 	 * Take care of ire's in other ill's per-interface forwarding
28767c478bd9Sstevel@tonic-gate 	 * table. Check if any ire in any of the ill's ill_srcif_table
28777c478bd9Sstevel@tonic-gate 	 * is pointing to this ill.
28787c478bd9Sstevel@tonic-gate 	 */
28797c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_srcif_table_lock);
28807c478bd9Sstevel@tonic-gate 	if (ire_srcif_table_count == 0) {
28817c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_srcif_table_lock);
28827c478bd9Sstevel@tonic-gate 		return;
28837c478bd9Sstevel@tonic-gate 	}
28847c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_srcif_table_lock);
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate #ifdef DEBUG
28877c478bd9Sstevel@tonic-gate 	/* Keep accounting of all interface based table ires */
28887c478bd9Sstevel@tonic-gate 	total_count = 0;
28897c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
28907c478bd9Sstevel@tonic-gate 	ill = ILL_START_WALK_V4(&ctx);
28917c478bd9Sstevel@tonic-gate 	while (ill != NULL) {
28927c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
28937c478bd9Sstevel@tonic-gate 		total_count += ill->ill_srcif_refcnt;
28947c478bd9Sstevel@tonic-gate 		next_ill = ill_next(&ctx, ill);
28957c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
28967c478bd9Sstevel@tonic-gate 		ill = next_ill;
28977c478bd9Sstevel@tonic-gate 	}
28987c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 	/* Hold lock here to make sure ire_srcif_table_count is stable */
29017c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_srcif_table_lock);
29027c478bd9Sstevel@tonic-gate 	i = ire_srcif_table_count;
29037c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_srcif_table_lock);
29047c478bd9Sstevel@tonic-gate 	ip1dbg(("ire_walk_srcif_v4: ire_srcif_table_count %d "
29057c478bd9Sstevel@tonic-gate 	    "total ill_srcif_refcnt %d\n", i, total_count));
29067c478bd9Sstevel@tonic-gate #endif
29077c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
29087c478bd9Sstevel@tonic-gate 	ill = ILL_START_WALK_V4(&ctx);
29097c478bd9Sstevel@tonic-gate 	while (ill != NULL) {
29107c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
29117c478bd9Sstevel@tonic-gate 		if ((ill->ill_srcif_refcnt == 0) || !ILL_CAN_LOOKUP(ill)) {
29127c478bd9Sstevel@tonic-gate 			next_ill = ill_next(&ctx, ill);
29137c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
29147c478bd9Sstevel@tonic-gate 			ill = next_ill;
29157c478bd9Sstevel@tonic-gate 			continue;
29167c478bd9Sstevel@tonic-gate 		}
29177c478bd9Sstevel@tonic-gate 		ill_refhold_locked(ill);
29187c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
29197c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
29207c478bd9Sstevel@tonic-gate 		if (ill->ill_srcif_table != NULL) {
29217c478bd9Sstevel@tonic-gate 			for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
29227c478bd9Sstevel@tonic-gate 				irb = &(ill->ill_srcif_table[i]);
29237c478bd9Sstevel@tonic-gate 				if (irb->irb_ire == NULL)
29247c478bd9Sstevel@tonic-gate 					continue;
29257c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(irb);
29267c478bd9Sstevel@tonic-gate 				for (ire = irb->irb_ire; ire != NULL;
29277c478bd9Sstevel@tonic-gate 				    ire = ire->ire_next) {
29287c478bd9Sstevel@tonic-gate 					(*func)(ire, arg);
29297c478bd9Sstevel@tonic-gate 				}
29307c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
29317c478bd9Sstevel@tonic-gate 			}
29327c478bd9Sstevel@tonic-gate 		}
29337c478bd9Sstevel@tonic-gate 		rw_enter(&ill_g_lock, RW_READER);
29347c478bd9Sstevel@tonic-gate 		next_ill = ill_next(&ctx, ill);
29357c478bd9Sstevel@tonic-gate 		ill_refrele(ill);
29367c478bd9Sstevel@tonic-gate 		ill = next_ill;
29377c478bd9Sstevel@tonic-gate 	}
29387c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
29397c478bd9Sstevel@tonic-gate }
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate /*
29427c478bd9Sstevel@tonic-gate  * This function takes a mask and returns
29437c478bd9Sstevel@tonic-gate  * number of bits set in the mask. If no
29447c478bd9Sstevel@tonic-gate  * bit is set it returns 0.
29457c478bd9Sstevel@tonic-gate  * Assumes a contiguous mask.
29467c478bd9Sstevel@tonic-gate  */
29477c478bd9Sstevel@tonic-gate int
29487c478bd9Sstevel@tonic-gate ip_mask_to_plen(ipaddr_t mask)
29497c478bd9Sstevel@tonic-gate {
29507c478bd9Sstevel@tonic-gate 	return (mask == 0 ? 0 : IP_ABITS - (ffs(ntohl(mask)) -1));
29517c478bd9Sstevel@tonic-gate }
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate /*
29547c478bd9Sstevel@tonic-gate  * Convert length for a mask to the mask.
29557c478bd9Sstevel@tonic-gate  */
29567c478bd9Sstevel@tonic-gate ipaddr_t
29577c478bd9Sstevel@tonic-gate ip_plen_to_mask(uint_t masklen)
29587c478bd9Sstevel@tonic-gate {
29597c478bd9Sstevel@tonic-gate 	return (htonl(IP_HOST_MASK << (IP_ABITS - masklen)));
29607c478bd9Sstevel@tonic-gate }
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate void
29637c478bd9Sstevel@tonic-gate ire_atomic_end(irb_t *irb_ptr, ire_t *ire)
29647c478bd9Sstevel@tonic-gate {
29657c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	ill_list[0] = ire->ire_stq != NULL ? ire->ire_stq->q_ptr : NULL;
29687c478bd9Sstevel@tonic-gate 	ill_list[1] = ire->ire_ipif != NULL ? ire->ire_ipif->ipif_ill : NULL;
29697c478bd9Sstevel@tonic-gate 	ill_list[2] = ire->ire_in_ill;
29707c478bd9Sstevel@tonic-gate 	ill_unlock_ills(ill_list, NUM_ILLS);
29717c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
29727c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_usesrc_lock);
29737c478bd9Sstevel@tonic-gate }
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate /*
29767c478bd9Sstevel@tonic-gate  * ire_add_v[46] atomically make sure that the ipif or ill associated
29777c478bd9Sstevel@tonic-gate  * with the new ire being added is stable and not IPIF_CHANGING or ILL_CHANGING
29787c478bd9Sstevel@tonic-gate  * before adding the ire to the table. This ensures that we don't create
29797c478bd9Sstevel@tonic-gate  * new IRE_CACHEs with stale values for parameters that are passed to
29807c478bd9Sstevel@tonic-gate  * ire_create such as ire_max_frag. Note that ire_create() is passed a pointer
29817c478bd9Sstevel@tonic-gate  * to the ipif_mtu, and not the value. The actual value is derived from the
29827c478bd9Sstevel@tonic-gate  * parent ire or ipif under the bucket lock.
29837c478bd9Sstevel@tonic-gate  */
29847c478bd9Sstevel@tonic-gate int
29857c478bd9Sstevel@tonic-gate ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q, mblk_t *mp,
29867c478bd9Sstevel@tonic-gate     ipsq_func_t func)
29877c478bd9Sstevel@tonic-gate {
29887c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
29897c478bd9Sstevel@tonic-gate 	ill_t	*ipif_ill;
29907c478bd9Sstevel@tonic-gate 	ill_t	*in_ill;
29917c478bd9Sstevel@tonic-gate 	ill_t	*ill_list[NUM_ILLS];
29927c478bd9Sstevel@tonic-gate 	int	cnt = NUM_ILLS;
29937c478bd9Sstevel@tonic-gate 	int	error = 0;
29947c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	ill_list[0] = stq_ill = ire->ire_stq !=
29977c478bd9Sstevel@tonic-gate 		NULL ? ire->ire_stq->q_ptr : NULL;
29987c478bd9Sstevel@tonic-gate 	ill_list[1] = ipif_ill = ire->ire_ipif !=
29997c478bd9Sstevel@tonic-gate 		NULL ? ire->ire_ipif->ipif_ill : NULL;
30007c478bd9Sstevel@tonic-gate 	ill_list[2] = in_ill = ire->ire_in_ill;
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	ASSERT((q != NULL && mp != NULL && func != NULL) ||
30037c478bd9Sstevel@tonic-gate 	    (q == NULL && mp == NULL && func == NULL));
30047c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_usesrc_lock, RW_READER);
30057c478bd9Sstevel@tonic-gate 	GRAB_CONN_LOCK(q);
30067c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_WRITER);
30077c478bd9Sstevel@tonic-gate 	ill_lock_ills(ill_list, cnt);
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	/*
30107c478bd9Sstevel@tonic-gate 	 * While the IRE is in the process of being added, a user may have
30117c478bd9Sstevel@tonic-gate 	 * invoked the ifconfig usesrc option on the stq_ill to make it a
30127c478bd9Sstevel@tonic-gate 	 * usesrc client ILL. Check for this possibility here, if it is true
30137c478bd9Sstevel@tonic-gate 	 * then we fail adding the IRE_CACHE. Another check is to make sure
30147c478bd9Sstevel@tonic-gate 	 * that an ipif_ill of an IRE_CACHE being added is not part of a usesrc
30157c478bd9Sstevel@tonic-gate 	 * group. The ill_g_usesrc_lock is released in ire_atomic_end
30167c478bd9Sstevel@tonic-gate 	 */
30177c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHE) &&
30187c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_USESRC_CHECK)) {
30197c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_usesrc_ifindex != 0) {
30207c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_usesrc_grp_next != NULL);
30217c478bd9Sstevel@tonic-gate 			if ((ipif_ill->ill_phyint->phyint_ifindex !=
30227c478bd9Sstevel@tonic-gate 			    stq_ill->ill_usesrc_ifindex) ||
30237c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_grp_next == NULL) ||
30247c478bd9Sstevel@tonic-gate 			    (ipif_ill->ill_usesrc_ifindex != 0)) {
30257c478bd9Sstevel@tonic-gate 				error = EINVAL;
30267c478bd9Sstevel@tonic-gate 				goto done;
30277c478bd9Sstevel@tonic-gate 			}
30287c478bd9Sstevel@tonic-gate 		} else if (ipif_ill->ill_usesrc_grp_next != NULL) {
30297c478bd9Sstevel@tonic-gate 			error = EINVAL;
30307c478bd9Sstevel@tonic-gate 			goto done;
30317c478bd9Sstevel@tonic-gate 		}
30327c478bd9Sstevel@tonic-gate 	}
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 	/*
30357c478bd9Sstevel@tonic-gate 	 * IPMP flag settings happen without taking the exclusive route
30367c478bd9Sstevel@tonic-gate 	 * in ip_sioctl_flags. So we need to make an atomic check here
30377c478bd9Sstevel@tonic-gate 	 * for FAILED/OFFLINE/INACTIVE flags or if it has hit the
30387c478bd9Sstevel@tonic-gate 	 * FAILBACK=no case.
30397c478bd9Sstevel@tonic-gate 	 */
30407c478bd9Sstevel@tonic-gate 	if ((stq_ill != NULL) && !IAM_WRITER_ILL(stq_ill)) {
30417c478bd9Sstevel@tonic-gate 		if (stq_ill->ill_state_flags & ILL_CHANGING) {
30427c478bd9Sstevel@tonic-gate 			ill = stq_ill;
30437c478bd9Sstevel@tonic-gate 			error = EAGAIN;
30447c478bd9Sstevel@tonic-gate 		} else if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
30457c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
30467c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
30477c478bd9Sstevel@tonic-gate 			error = EINVAL;
30487c478bd9Sstevel@tonic-gate 		}
30497c478bd9Sstevel@tonic-gate 		goto done;
30507c478bd9Sstevel@tonic-gate 	}
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	/*
30537c478bd9Sstevel@tonic-gate 	 * We don't check for OFFLINE/FAILED in this case because
30547c478bd9Sstevel@tonic-gate 	 * the source address selection logic (ipif_select_source)
30557c478bd9Sstevel@tonic-gate 	 * may still select a source address from such an ill. The
30567c478bd9Sstevel@tonic-gate 	 * assumption is that these addresses will be moved by in.mpathd
30577c478bd9Sstevel@tonic-gate 	 * soon. (i.e. this is a race). However link local addresses
30587c478bd9Sstevel@tonic-gate 	 * will not move and hence ipif_select_source_v6 tries to avoid
30597c478bd9Sstevel@tonic-gate 	 * FAILED ills. Please see ipif_select_source_v6 for more info
30607c478bd9Sstevel@tonic-gate 	 */
30617c478bd9Sstevel@tonic-gate 	if ((ipif_ill != NULL) && !IAM_WRITER_ILL(ipif_ill) &&
30627c478bd9Sstevel@tonic-gate 	    (ipif_ill->ill_state_flags & ILL_CHANGING)) {
30637c478bd9Sstevel@tonic-gate 		ill = ipif_ill;
30647c478bd9Sstevel@tonic-gate 		error = EAGAIN;
30657c478bd9Sstevel@tonic-gate 		goto done;
30667c478bd9Sstevel@tonic-gate 	}
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	if ((in_ill != NULL) && !IAM_WRITER_ILL(in_ill) &&
30697c478bd9Sstevel@tonic-gate 	    (in_ill->ill_state_flags & ILL_CHANGING)) {
30707c478bd9Sstevel@tonic-gate 		ill = in_ill;
30717c478bd9Sstevel@tonic-gate 		error = EAGAIN;
30727c478bd9Sstevel@tonic-gate 		goto done;
30737c478bd9Sstevel@tonic-gate 	}
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 	if ((ire->ire_ipif != NULL) && !IAM_WRITER_IPIF(ire->ire_ipif) &&
30767c478bd9Sstevel@tonic-gate 	    (ire->ire_ipif->ipif_state_flags & IPIF_CHANGING)) {
30777c478bd9Sstevel@tonic-gate 		ill = ire->ire_ipif->ipif_ill;
30787c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
30797c478bd9Sstevel@tonic-gate 		error = EAGAIN;
30807c478bd9Sstevel@tonic-gate 		goto done;
30817c478bd9Sstevel@tonic-gate 	}
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate done:
30847c478bd9Sstevel@tonic-gate 	if (error == EAGAIN && ILL_CAN_WAIT(ill, q)) {
30857c478bd9Sstevel@tonic-gate 		ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
30867c478bd9Sstevel@tonic-gate 		mutex_enter(&ipsq->ipsq_lock);
30877c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
30887c478bd9Sstevel@tonic-gate 		ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
30897c478bd9Sstevel@tonic-gate 		mutex_exit(&ipsq->ipsq_lock);
30907c478bd9Sstevel@tonic-gate 		error = EINPROGRESS;
30917c478bd9Sstevel@tonic-gate 	} else if (error != 0) {
30927c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb_ptr, ire);
30937c478bd9Sstevel@tonic-gate 	}
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	RELEASE_CONN_LOCK(q);
30967c478bd9Sstevel@tonic-gate 	return (error);
30977c478bd9Sstevel@tonic-gate }
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate /*
31007c478bd9Sstevel@tonic-gate  * Add a fully initialized IRE to an appropriate table based on
31017c478bd9Sstevel@tonic-gate  * ire_type.
31027c478bd9Sstevel@tonic-gate  */
31037c478bd9Sstevel@tonic-gate int
31047c478bd9Sstevel@tonic-gate ire_add(ire_t **irep, queue_t *q, mblk_t *mp, ipsq_func_t func)
31057c478bd9Sstevel@tonic-gate {
31067c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
31077c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
31087c478bd9Sstevel@tonic-gate 	ill_t	*ill;
31097c478bd9Sstevel@tonic-gate 	ipif_t	*ipif = NULL;
31107c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
31117c478bd9Sstevel@tonic-gate 	ire_t	*ire = *irep;
31127c478bd9Sstevel@tonic-gate 	int	error;
3113*dc3879f9Sjarrett 	boolean_t ire_is_mblk = B_FALSE;
3114*dc3879f9Sjarrett 	tsol_gcgrp_t *gcgrp = NULL;
3115*dc3879f9Sjarrett 	tsol_gcgrp_addr_t ga;
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN);
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	/* get ready for the day when original ire is not created as mblk */
31207c478bd9Sstevel@tonic-gate 	if (ire->ire_mp != NULL) {
3121*dc3879f9Sjarrett 		ire_is_mblk = B_TRUE;
31227c478bd9Sstevel@tonic-gate 		/* Copy the ire to a kmem_alloc'ed area */
31237c478bd9Sstevel@tonic-gate 		ire1 = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
31247c478bd9Sstevel@tonic-gate 		if (ire1 == NULL) {
31257c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add: alloc failed\n"));
31267c478bd9Sstevel@tonic-gate 			ire_delete(ire);
31277c478bd9Sstevel@tonic-gate 			*irep = NULL;
31287c478bd9Sstevel@tonic-gate 			return (ENOMEM);
31297c478bd9Sstevel@tonic-gate 		}
31307c478bd9Sstevel@tonic-gate 		*ire1 = *ire;
31317c478bd9Sstevel@tonic-gate 		ire1->ire_mp = NULL;
31327c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
31337c478bd9Sstevel@tonic-gate 		ire = ire1;
31347c478bd9Sstevel@tonic-gate 	}
31357c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
31367c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE) {
31397c478bd9Sstevel@tonic-gate 		/*
31407c478bd9Sstevel@tonic-gate 		 * If this interface is FAILED, or INACTIVE or has hit
31417c478bd9Sstevel@tonic-gate 		 * the FAILBACK=no case, we create IRE_CACHES marked
31427c478bd9Sstevel@tonic-gate 		 * HIDDEN for some special cases e.g. bind to
31437c478bd9Sstevel@tonic-gate 		 * IPIF_NOFAILOVER address etc. So, if this interface
31447c478bd9Sstevel@tonic-gate 		 * is FAILED/INACTIVE/hit FAILBACK=no case, and we are
31457c478bd9Sstevel@tonic-gate 		 * not creating hidden ires, we should not allow that.
31467c478bd9Sstevel@tonic-gate 		 * This happens because the state of the interface
31477c478bd9Sstevel@tonic-gate 		 * changed while we were waiting in ARP. If this is the
31487c478bd9Sstevel@tonic-gate 		 * daemon sending probes, the next probe will create
31497c478bd9Sstevel@tonic-gate 		 * HIDDEN ires and we will create an ire then. This
31507c478bd9Sstevel@tonic-gate 		 * cannot happen with NDP currently because IRE is
31517c478bd9Sstevel@tonic-gate 		 * never queued in NDP. But it can happen in the
31527c478bd9Sstevel@tonic-gate 		 * future when we have external resolvers with IPv6.
31537c478bd9Sstevel@tonic-gate 		 * If the interface gets marked with OFFLINE while we
31547c478bd9Sstevel@tonic-gate 		 * are waiting in ARP, don't add the ire.
31557c478bd9Sstevel@tonic-gate 		 */
31567c478bd9Sstevel@tonic-gate 		if ((stq_ill->ill_phyint->phyint_flags & PHYI_OFFLINE) ||
31577c478bd9Sstevel@tonic-gate 		    (ill_is_probeonly(stq_ill) &&
31587c478bd9Sstevel@tonic-gate 		    !(ire->ire_marks & IRE_MARK_HIDDEN))) {
31597c478bd9Sstevel@tonic-gate 			/*
31607c478bd9Sstevel@tonic-gate 			 * We don't know whether it is a valid ipif or not.
31617c478bd9Sstevel@tonic-gate 			 * unless we do the check below. So, set it to NULL.
31627c478bd9Sstevel@tonic-gate 			 */
31637c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
31647c478bd9Sstevel@tonic-gate 			ire_delete(ire);
31657c478bd9Sstevel@tonic-gate 			*irep = NULL;
31667c478bd9Sstevel@tonic-gate 			return (EINVAL);
31677c478bd9Sstevel@tonic-gate 		}
31687c478bd9Sstevel@tonic-gate 	}
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	if (stq_ill != NULL && ire->ire_type == IRE_CACHE &&
31717c478bd9Sstevel@tonic-gate 	    stq_ill->ill_net_type == IRE_IF_RESOLVER) {
31727c478bd9Sstevel@tonic-gate 		rw_enter(&ill_g_lock, RW_READER);
31737c478bd9Sstevel@tonic-gate 		ill = ILL_START_WALK_ALL(&ctx);
31747c478bd9Sstevel@tonic-gate 		for (; ill != NULL; ill = ill_next(&ctx, ill)) {
31757c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
31767c478bd9Sstevel@tonic-gate 			if (ill->ill_state_flags & ILL_CONDEMNED) {
31777c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
31787c478bd9Sstevel@tonic-gate 				continue;
31797c478bd9Sstevel@tonic-gate 			}
31807c478bd9Sstevel@tonic-gate 			/*
31817c478bd9Sstevel@tonic-gate 			 * We need to make sure that the ipif is a valid one
31827c478bd9Sstevel@tonic-gate 			 * before adding the IRE_CACHE. This happens only
31837c478bd9Sstevel@tonic-gate 			 * with IRE_CACHE when there is an external resolver.
31847c478bd9Sstevel@tonic-gate 			 *
31857c478bd9Sstevel@tonic-gate 			 * We can unplumb a logical interface while the
31867c478bd9Sstevel@tonic-gate 			 * packet is waiting in ARP with the IRE. Then,
31877c478bd9Sstevel@tonic-gate 			 * later on when we feed the IRE back, the ipif
31887c478bd9Sstevel@tonic-gate 			 * has to be re-checked. This can't happen with
31897c478bd9Sstevel@tonic-gate 			 * NDP currently, as we never queue the IRE with
31907c478bd9Sstevel@tonic-gate 			 * the packet. We always try to recreate the IRE
31917c478bd9Sstevel@tonic-gate 			 * when the resolution is completed. But, we do
31927c478bd9Sstevel@tonic-gate 			 * it for IPv6 also here so that in future if
31937c478bd9Sstevel@tonic-gate 			 * we have external resolvers, it will work without
31947c478bd9Sstevel@tonic-gate 			 * any change.
31957c478bd9Sstevel@tonic-gate 			 */
31967c478bd9Sstevel@tonic-gate 			ipif = ipif_lookup_seqid(ill, ire->ire_ipif_seqid);
31977c478bd9Sstevel@tonic-gate 			if (ipif != NULL) {
31987c478bd9Sstevel@tonic-gate 				ipif_refhold_locked(ipif);
31997c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
32007c478bd9Sstevel@tonic-gate 				break;
32017c478bd9Sstevel@tonic-gate 			}
32027c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
32037c478bd9Sstevel@tonic-gate 		}
32047c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
32057c478bd9Sstevel@tonic-gate 		if (ipif == NULL ||
32067c478bd9Sstevel@tonic-gate 		    (ipif->ipif_isv6 &&
32077c478bd9Sstevel@tonic-gate 		    !IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
32087c478bd9Sstevel@tonic-gate 		    &ipif->ipif_v6src_addr)) ||
32097c478bd9Sstevel@tonic-gate 		    (!ipif->ipif_isv6 &&
32107c478bd9Sstevel@tonic-gate 		    ire->ire_src_addr != ipif->ipif_src_addr) ||
321145916cd2Sjpk 		    ire->ire_zoneid != ipif->ipif_zoneid) {
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 			if (ipif != NULL)
32147c478bd9Sstevel@tonic-gate 				ipif_refrele(ipif);
32157c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
32167c478bd9Sstevel@tonic-gate 			ire_delete(ire);
32177c478bd9Sstevel@tonic-gate 			*irep = NULL;
32187c478bd9Sstevel@tonic-gate 			return (EINVAL);
32197c478bd9Sstevel@tonic-gate 		}
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 		ASSERT(ill != NULL);
32237c478bd9Sstevel@tonic-gate 		/*
32247c478bd9Sstevel@tonic-gate 		 * If this group was dismantled while this packets was
32257c478bd9Sstevel@tonic-gate 		 * queued in ARP, don't add it here.
32267c478bd9Sstevel@tonic-gate 		 */
32277c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif->ipif_ill->ill_group != ill->ill_group) {
32287c478bd9Sstevel@tonic-gate 			/* We don't want ire_inactive bump stats for this */
32297c478bd9Sstevel@tonic-gate 			ipif_refrele(ipif);
32307c478bd9Sstevel@tonic-gate 			ire->ire_ipif = NULL;
32317c478bd9Sstevel@tonic-gate 			ire_delete(ire);
32327c478bd9Sstevel@tonic-gate 			*irep = NULL;
32337c478bd9Sstevel@tonic-gate 			return (EINVAL);
32347c478bd9Sstevel@tonic-gate 		}
3235*dc3879f9Sjarrett 
3236*dc3879f9Sjarrett 		/*
3237*dc3879f9Sjarrett 		 * Since we didn't attach label security attributes to the
3238*dc3879f9Sjarrett 		 * ire for the resolver case, we need to add it now. (only
3239*dc3879f9Sjarrett 		 * for v4 resolver and v6 xresolv case).
3240*dc3879f9Sjarrett 		 */
3241*dc3879f9Sjarrett 		if (is_system_labeled() && ire_is_mblk) {
3242*dc3879f9Sjarrett 			if (ire->ire_ipversion == IPV4_VERSION) {
3243*dc3879f9Sjarrett 				ga.ga_af = AF_INET;
3244*dc3879f9Sjarrett 				IN6_IPADDR_TO_V4MAPPED(ire->ire_gateway_addr !=
3245*dc3879f9Sjarrett 				    INADDR_ANY ? ire->ire_gateway_addr :
3246*dc3879f9Sjarrett 				    ire->ire_addr, &ga.ga_addr);
3247*dc3879f9Sjarrett 			} else {
3248*dc3879f9Sjarrett 				ga.ga_af = AF_INET6;
3249*dc3879f9Sjarrett 				ga.ga_addr = IN6_IS_ADDR_UNSPECIFIED(
3250*dc3879f9Sjarrett 				    &ire->ire_gateway_addr_v6) ?
3251*dc3879f9Sjarrett 				    ire->ire_addr_v6 :
3252*dc3879f9Sjarrett 				    ire->ire_gateway_addr_v6;
3253*dc3879f9Sjarrett 			}
3254*dc3879f9Sjarrett 			gcgrp = gcgrp_lookup(&ga, B_FALSE);
3255*dc3879f9Sjarrett 			error = tsol_ire_init_gwattr(ire, ire->ire_ipversion,
3256*dc3879f9Sjarrett 			    NULL, gcgrp);
3257*dc3879f9Sjarrett 			if (error != 0) {
3258*dc3879f9Sjarrett 				if (gcgrp != NULL) {
3259*dc3879f9Sjarrett 					GCGRP_REFRELE(gcgrp);
3260*dc3879f9Sjarrett 					gcgrp = NULL;
3261*dc3879f9Sjarrett 				}
3262*dc3879f9Sjarrett 				ipif_refrele(ipif);
3263*dc3879f9Sjarrett 				ire->ire_ipif = NULL;
3264*dc3879f9Sjarrett 				ire_delete(ire);
3265*dc3879f9Sjarrett 				*irep = NULL;
3266*dc3879f9Sjarrett 				return (error);
3267*dc3879f9Sjarrett 			}
3268*dc3879f9Sjarrett 		}
32697c478bd9Sstevel@tonic-gate 	}
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 	/*
32727c478bd9Sstevel@tonic-gate 	 * In case ire was changed
32737c478bd9Sstevel@tonic-gate 	 */
32747c478bd9Sstevel@tonic-gate 	*irep = ire;
32757c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
32767c478bd9Sstevel@tonic-gate 		error = ire_add_v6(irep, q, mp, func);
32777c478bd9Sstevel@tonic-gate 	} else {
32787c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill == NULL)
32797c478bd9Sstevel@tonic-gate 			error = ire_add_v4(irep, q, mp, func);
32807c478bd9Sstevel@tonic-gate 		else
32817c478bd9Sstevel@tonic-gate 			error = ire_add_srcif_v4(irep, q, mp, func);
32827c478bd9Sstevel@tonic-gate 	}
32837c478bd9Sstevel@tonic-gate 	if (ipif != NULL)
32847c478bd9Sstevel@tonic-gate 		ipif_refrele(ipif);
32857c478bd9Sstevel@tonic-gate 	return (error);
32867c478bd9Sstevel@tonic-gate }
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate /*
3289*dc3879f9Sjarrett  * Add an initialized IRE to an appropriate table based on ire_type.
32907c478bd9Sstevel@tonic-gate  *
32917c478bd9Sstevel@tonic-gate  * The forward table contains IRE_PREFIX/IRE_HOST/IRE_HOST_REDIRECT
32927c478bd9Sstevel@tonic-gate  * IRE_IF_RESOLVER/IRE_IF_NORESOLVER and IRE_DEFAULT.
32937c478bd9Sstevel@tonic-gate  *
32947c478bd9Sstevel@tonic-gate  * The cache table contains IRE_BROADCAST/IRE_LOCAL/IRE_LOOPBACK
32957c478bd9Sstevel@tonic-gate  * and IRE_CACHE.
32967c478bd9Sstevel@tonic-gate  *
32977c478bd9Sstevel@tonic-gate  * NOTE : This function is called as writer though not required
32987c478bd9Sstevel@tonic-gate  * by this function.
32997c478bd9Sstevel@tonic-gate  */
33007c478bd9Sstevel@tonic-gate static int
33017c478bd9Sstevel@tonic-gate ire_add_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
33027c478bd9Sstevel@tonic-gate {
33037c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
33047c478bd9Sstevel@tonic-gate 	int	mask_table_index;
33057c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
33067c478bd9Sstevel@tonic-gate 	ire_t	**irep;
33077c478bd9Sstevel@tonic-gate 	int	flags;
33087c478bd9Sstevel@tonic-gate 	ire_t	*pire = NULL;
33097c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
33107c478bd9Sstevel@tonic-gate 	ire_t	*ire = *ire_p;
33117c478bd9Sstevel@tonic-gate 	int	error;
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
33147c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
33157c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
33167c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(
33177c478bd9Sstevel@tonic-gate 		    &((ill_t *)(ire->ire_stq->q_ptr))->ill_lock));
33187c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
33197c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_mp == NULL); /* Calls should go through ire_add */
33207c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill == NULL); /* No srcif entries */
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 	/* Find the appropriate list head. */
33237c478bd9Sstevel@tonic-gate 	switch (ire->ire_type) {
33247c478bd9Sstevel@tonic-gate 	case IRE_HOST:
33257c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
33267c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
33277c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
33287c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
33297c478bd9Sstevel@tonic-gate 		break;
33307c478bd9Sstevel@tonic-gate 	case IRE_HOST_REDIRECT:
33317c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
33327c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
33337c478bd9Sstevel@tonic-gate 		ire->ire_src_addr = 0;
33347c478bd9Sstevel@tonic-gate 		break;
33357c478bd9Sstevel@tonic-gate 	case IRE_CACHE:
33367c478bd9Sstevel@tonic-gate 	case IRE_BROADCAST:
33377c478bd9Sstevel@tonic-gate 	case IRE_LOCAL:
33387c478bd9Sstevel@tonic-gate 	case IRE_LOOPBACK:
33397c478bd9Sstevel@tonic-gate 		ire->ire_mask = IP_HOST_MASK;
33407c478bd9Sstevel@tonic-gate 		ire->ire_masklen = IP_ABITS;
33417c478bd9Sstevel@tonic-gate 		break;
33427c478bd9Sstevel@tonic-gate 	case IRE_PREFIX:
33437c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
33447c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
33457c478bd9Sstevel@tonic-gate 		break;
33467c478bd9Sstevel@tonic-gate 	case IRE_DEFAULT:
33477c478bd9Sstevel@tonic-gate 		if ((ire->ire_flags & RTF_SETSRC) == 0)
33487c478bd9Sstevel@tonic-gate 			ire->ire_src_addr = 0;
33497c478bd9Sstevel@tonic-gate 		break;
33507c478bd9Sstevel@tonic-gate 	case IRE_IF_RESOLVER:
33517c478bd9Sstevel@tonic-gate 	case IRE_IF_NORESOLVER:
33527c478bd9Sstevel@tonic-gate 		break;
33537c478bd9Sstevel@tonic-gate 	default:
33547c478bd9Sstevel@tonic-gate 		printf("ire_add_v4: ire %p has unrecognized IRE type (%d)\n",
33557c478bd9Sstevel@tonic-gate 		    (void *)ire, ire->ire_type);
33567c478bd9Sstevel@tonic-gate 		ire_delete(ire);
33577c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
33587c478bd9Sstevel@tonic-gate 		return (EINVAL);
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
33627c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	if ((ire->ire_type & IRE_CACHETABLE) == 0) {
33657c478bd9Sstevel@tonic-gate 		/* IRE goes into Forward Table */
33667c478bd9Sstevel@tonic-gate 		mask_table_index = ire->ire_masklen;
33677c478bd9Sstevel@tonic-gate 		if ((ip_forwarding_table[mask_table_index]) == NULL) {
33687c478bd9Sstevel@tonic-gate 			irb_t *ptr;
33697c478bd9Sstevel@tonic-gate 			int i;
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 			ptr = (irb_t *)mi_zalloc((ip_ftable_hash_size *
33727c478bd9Sstevel@tonic-gate 			    sizeof (irb_t)));
33737c478bd9Sstevel@tonic-gate 			if (ptr == NULL) {
33747c478bd9Sstevel@tonic-gate 				ire_delete(ire);
33757c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
33767c478bd9Sstevel@tonic-gate 				return (ENOMEM);
33777c478bd9Sstevel@tonic-gate 			}
33787c478bd9Sstevel@tonic-gate 			for (i = 0; i < ip_ftable_hash_size; i++) {
33797c478bd9Sstevel@tonic-gate 				rw_init(&ptr[i].irb_lock, NULL,
33807c478bd9Sstevel@tonic-gate 				    RW_DEFAULT, NULL);
33817c478bd9Sstevel@tonic-gate 			}
33827c478bd9Sstevel@tonic-gate 			mutex_enter(&ire_ft_init_lock);
33837c478bd9Sstevel@tonic-gate 			if (ip_forwarding_table[mask_table_index] == NULL) {
33847c478bd9Sstevel@tonic-gate 				ip_forwarding_table[mask_table_index] = ptr;
33857c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_ft_init_lock);
33867c478bd9Sstevel@tonic-gate 			} else {
33877c478bd9Sstevel@tonic-gate 				/*
33887c478bd9Sstevel@tonic-gate 				 * Some other thread won the race in
33897c478bd9Sstevel@tonic-gate 				 * initializing the forwarding table at the
33907c478bd9Sstevel@tonic-gate 				 * same index.
33917c478bd9Sstevel@tonic-gate 				 */
33927c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_ft_init_lock);
33937c478bd9Sstevel@tonic-gate 				for (i = 0; i < ip_ftable_hash_size; i++) {
33947c478bd9Sstevel@tonic-gate 					rw_destroy(&ptr[i].irb_lock);
33957c478bd9Sstevel@tonic-gate 				}
33967c478bd9Sstevel@tonic-gate 				mi_free(ptr);
33977c478bd9Sstevel@tonic-gate 			}
33987c478bd9Sstevel@tonic-gate 		}
33997c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_forwarding_table[mask_table_index][
34007c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH(ire->ire_addr, ip_ftable_hash_size)]);
34017c478bd9Sstevel@tonic-gate 	} else {
34027c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_cache_table[IRE_ADDR_HASH(ire->ire_addr,
34037c478bd9Sstevel@tonic-gate 		    ip_cache_table_size)]);
34047c478bd9Sstevel@tonic-gate 	}
34057c478bd9Sstevel@tonic-gate 	/*
34067c478bd9Sstevel@tonic-gate 	 * ip_newroute/ip_newroute_multi are unable to prevent the deletion
34077c478bd9Sstevel@tonic-gate 	 * of the interface route while adding an IRE_CACHE for an on-link
34087c478bd9Sstevel@tonic-gate 	 * destination in the IRE_IF_RESOLVER case, since the ire has to
34097c478bd9Sstevel@tonic-gate 	 * go to ARP and return. We can't do a REFHOLD on the
34107c478bd9Sstevel@tonic-gate 	 * associated interface ire for fear of ARP freeing the message.
34117c478bd9Sstevel@tonic-gate 	 * Here we look up the interface ire in the forwarding table and
34127c478bd9Sstevel@tonic-gate 	 * make sure that the interface route has not been deleted.
34137c478bd9Sstevel@tonic-gate 	 */
34147c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE && ire->ire_gateway_addr == 0 &&
34157c478bd9Sstevel@tonic-gate 	    ((ill_t *)ire->ire_stq->q_ptr)->ill_net_type == IRE_IF_RESOLVER) {
34167c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp == NULL);
34177c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr) && !(ire->ire_flags & RTF_SETSRC)) {
34187c478bd9Sstevel@tonic-gate 			/*
34197c478bd9Sstevel@tonic-gate 			 * The ihandle that we used in ip_newroute_multi
34207c478bd9Sstevel@tonic-gate 			 * comes from the interface route corresponding
34217c478bd9Sstevel@tonic-gate 			 * to ire_ipif. Lookup here to see if it exists
34227c478bd9Sstevel@tonic-gate 			 * still.
34237c478bd9Sstevel@tonic-gate 			 * If the ire has a source address assigned using
34247c478bd9Sstevel@tonic-gate 			 * RTF_SETSRC, ire_ipif is the logical interface holding
34257c478bd9Sstevel@tonic-gate 			 * this source address, so we can't use it to check for
34267c478bd9Sstevel@tonic-gate 			 * the existence of the interface route. Instead we rely
34277c478bd9Sstevel@tonic-gate 			 * on the brute force ihandle search in
34287c478bd9Sstevel@tonic-gate 			 * ire_ihandle_lookup_onlink() below.
34297c478bd9Sstevel@tonic-gate 			 */
34307c478bd9Sstevel@tonic-gate 			pire = ipif_to_ire(ire->ire_ipif);
34317c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
34327c478bd9Sstevel@tonic-gate 				ire_delete(ire);
34337c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
34347c478bd9Sstevel@tonic-gate 				return (EINVAL);
34357c478bd9Sstevel@tonic-gate 			} else if (pire->ire_ihandle != ire->ire_ihandle) {
34367c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
34377c478bd9Sstevel@tonic-gate 				ire_delete(ire);
34387c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
34397c478bd9Sstevel@tonic-gate 				return (EINVAL);
34407c478bd9Sstevel@tonic-gate 			}
34417c478bd9Sstevel@tonic-gate 		} else {
34427c478bd9Sstevel@tonic-gate 			pire = ire_ihandle_lookup_onlink(ire);
34437c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
34447c478bd9Sstevel@tonic-gate 				ire_delete(ire);
34457c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
34467c478bd9Sstevel@tonic-gate 				return (EINVAL);
34477c478bd9Sstevel@tonic-gate 			}
34487c478bd9Sstevel@tonic-gate 		}
34497c478bd9Sstevel@tonic-gate 		/* Prevent pire from getting deleted */
34507c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(pire->ire_bucket);
34517c478bd9Sstevel@tonic-gate 		/* Has it been removed already ? */
34527c478bd9Sstevel@tonic-gate 		if (pire->ire_marks & IRE_MARK_CONDEMNED) {
34537c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
34547c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
34557c478bd9Sstevel@tonic-gate 			ire_delete(ire);
34567c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
34577c478bd9Sstevel@tonic-gate 			return (EINVAL);
34587c478bd9Sstevel@tonic-gate 		}
34597c478bd9Sstevel@tonic-gate 	} else {
34607c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_max_fragp != NULL);
34617c478bd9Sstevel@tonic-gate 	}
34627c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
34657c478bd9Sstevel@tonic-gate 		/*
34667c478bd9Sstevel@tonic-gate 		 * We use MATCH_IRE_IPIF while adding IRE_CACHES only
34677c478bd9Sstevel@tonic-gate 		 * for historic reasons and to maintain symmetry with
34687c478bd9Sstevel@tonic-gate 		 * IPv6 code path. Historically this was used by
34697c478bd9Sstevel@tonic-gate 		 * multicast code to create multiple IRE_CACHES on
34707c478bd9Sstevel@tonic-gate 		 * a single ill with different ipifs. This was used
34717c478bd9Sstevel@tonic-gate 		 * so that multicast packets leaving the node had the
34727c478bd9Sstevel@tonic-gate 		 * right source address. This is no longer needed as
34737c478bd9Sstevel@tonic-gate 		 * ip_wput initializes the address correctly.
34747c478bd9Sstevel@tonic-gate 		 */
34757c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_IPIF;
34767c478bd9Sstevel@tonic-gate 		/*
34777c478bd9Sstevel@tonic-gate 		 * If we are creating hidden ires, make sure we search on
34787c478bd9Sstevel@tonic-gate 		 * this ill (MATCH_IRE_ILL) and a hidden ire,
34797c478bd9Sstevel@tonic-gate 		 * while we are searching for duplicates below. Otherwise we
34807c478bd9Sstevel@tonic-gate 		 * could potentially find an IRE on some other interface
34817c478bd9Sstevel@tonic-gate 		 * and it may not be a IRE marked with IRE_MARK_HIDDEN. We
34827c478bd9Sstevel@tonic-gate 		 * shouldn't do this as this will lead to an infinite loop
34837c478bd9Sstevel@tonic-gate 		 * (if we get to ip_wput again) eventually we need an hidden
34847c478bd9Sstevel@tonic-gate 		 * ire for this packet to go out. MATCH_IRE_ILL is explicitly
34857c478bd9Sstevel@tonic-gate 		 * done below.
34867c478bd9Sstevel@tonic-gate 		 */
34877c478bd9Sstevel@tonic-gate 		if (ire->ire_type == IRE_CACHE &&
34887c478bd9Sstevel@tonic-gate 		    (ire->ire_marks & IRE_MARK_HIDDEN))
34897c478bd9Sstevel@tonic-gate 			flags |= (MATCH_IRE_MARK_HIDDEN);
34907c478bd9Sstevel@tonic-gate 	}
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	/*
34937c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
34947c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock. Check for condemned
34957c478bd9Sstevel@tonic-gate 	 *
34967c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
34977c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
34987c478bd9Sstevel@tonic-gate 	 */
34997c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
35007c478bd9Sstevel@tonic-gate 	if (error != 0) {
35017c478bd9Sstevel@tonic-gate 		/*
35027c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
35037c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
35047c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
35057c478bd9Sstevel@tonic-gate 		 */
35067c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
35077c478bd9Sstevel@tonic-gate 		ire_delete(ire);
35087c478bd9Sstevel@tonic-gate 		if (pire != NULL) {
35097c478bd9Sstevel@tonic-gate 			IRB_REFRELE(pire->ire_bucket);
35107c478bd9Sstevel@tonic-gate 			ire_refrele(pire);
35117c478bd9Sstevel@tonic-gate 		}
35127c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
35137c478bd9Sstevel@tonic-gate 		return (error);
35147c478bd9Sstevel@tonic-gate 	}
35157c478bd9Sstevel@tonic-gate 	/*
35167c478bd9Sstevel@tonic-gate 	 * To avoid creating ires having stale values for the ire_max_frag
35177c478bd9Sstevel@tonic-gate 	 * we get the latest value atomically here. For more details
35187c478bd9Sstevel@tonic-gate 	 * see the block comment in ip_sioctl_mtu and in DL_NOTE_SDU_CHANGE
35197c478bd9Sstevel@tonic-gate 	 * in ip_rput_dlpi_writer
35207c478bd9Sstevel@tonic-gate 	 */
35217c478bd9Sstevel@tonic-gate 	if (ire->ire_max_fragp == NULL) {
35227c478bd9Sstevel@tonic-gate 		if (CLASSD(ire->ire_addr))
35237c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
35247c478bd9Sstevel@tonic-gate 		else
35257c478bd9Sstevel@tonic-gate 			ire->ire_max_frag = pire->ire_max_frag;
35267c478bd9Sstevel@tonic-gate 	} else {
35277c478bd9Sstevel@tonic-gate 		uint_t	max_frag;
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate 		max_frag = *ire->ire_max_fragp;
35307c478bd9Sstevel@tonic-gate 		ire->ire_max_fragp = NULL;
35317c478bd9Sstevel@tonic-gate 		ire->ire_max_frag = max_frag;
35327c478bd9Sstevel@tonic-gate 	}
35337c478bd9Sstevel@tonic-gate 	/*
35347c478bd9Sstevel@tonic-gate 	 * Atomically check for duplicate and insert in the table.
35357c478bd9Sstevel@tonic-gate 	 */
35367c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
35377c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
35387c478bd9Sstevel@tonic-gate 			continue;
35397c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL) {
35407c478bd9Sstevel@tonic-gate 			/*
35417c478bd9Sstevel@tonic-gate 			 * We do MATCH_IRE_ILL implicitly here for IREs
35427c478bd9Sstevel@tonic-gate 			 * with a non-null ire_ipif, including IRE_CACHEs.
35437c478bd9Sstevel@tonic-gate 			 * As ire_ipif and ire_stq could point to two
35447c478bd9Sstevel@tonic-gate 			 * different ills, we can't pass just ire_ipif to
35457c478bd9Sstevel@tonic-gate 			 * ire_match_args and get a match on both ills.
35467c478bd9Sstevel@tonic-gate 			 * This is just needed for duplicate checks here and
35477c478bd9Sstevel@tonic-gate 			 * so we don't add an extra argument to
35487c478bd9Sstevel@tonic-gate 			 * ire_match_args for this. Do it locally.
35497c478bd9Sstevel@tonic-gate 			 *
35507c478bd9Sstevel@tonic-gate 			 * NOTE : Currently there is no part of the code
35517c478bd9Sstevel@tonic-gate 			 * that asks for both MATH_IRE_IPIF and MATCH_IRE_ILL
35527c478bd9Sstevel@tonic-gate 			 * match for IRE_CACHEs. Thus we don't want to
35537c478bd9Sstevel@tonic-gate 			 * extend the arguments to ire_match_args.
35547c478bd9Sstevel@tonic-gate 			 */
35557c478bd9Sstevel@tonic-gate 			if (ire1->ire_stq != ire->ire_stq)
35567c478bd9Sstevel@tonic-gate 				continue;
35577c478bd9Sstevel@tonic-gate 			/*
35587c478bd9Sstevel@tonic-gate 			 * Multiroute IRE_CACHEs for a given destination can
35597c478bd9Sstevel@tonic-gate 			 * have the same ire_ipif, typically if their source
35607c478bd9Sstevel@tonic-gate 			 * address is forced using RTF_SETSRC, and the same
35617c478bd9Sstevel@tonic-gate 			 * send-to queue. We differentiate them using the parent
35627c478bd9Sstevel@tonic-gate 			 * handle.
35637c478bd9Sstevel@tonic-gate 			 */
35647c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_CACHE &&
35657c478bd9Sstevel@tonic-gate 			    (ire1->ire_flags & RTF_MULTIRT) &&
35667c478bd9Sstevel@tonic-gate 			    (ire->ire_flags & RTF_MULTIRT) &&
35677c478bd9Sstevel@tonic-gate 			    (ire1->ire_phandle != ire->ire_phandle))
35687c478bd9Sstevel@tonic-gate 				continue;
35697c478bd9Sstevel@tonic-gate 		}
35707c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
35717c478bd9Sstevel@tonic-gate 			continue;
35727c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask,
35737c478bd9Sstevel@tonic-gate 		    ire->ire_gateway_addr, ire->ire_type, ire->ire_ipif,
357445916cd2Sjpk 		    ire->ire_zoneid, 0, NULL, flags)) {
35757c478bd9Sstevel@tonic-gate 			/*
35767c478bd9Sstevel@tonic-gate 			 * Return the old ire after doing a REFHOLD.
35777c478bd9Sstevel@tonic-gate 			 * As most of the callers continue to use the IRE
35787c478bd9Sstevel@tonic-gate 			 * after adding, we return a held ire. This will
35797c478bd9Sstevel@tonic-gate 			 * avoid a lookup in the caller again. If the callers
35807c478bd9Sstevel@tonic-gate 			 * don't want to use it, they need to do a REFRELE.
35817c478bd9Sstevel@tonic-gate 			 */
35827c478bd9Sstevel@tonic-gate 			ip1dbg(("found dup ire existing %p new %p",
35837c478bd9Sstevel@tonic-gate 			    (void *)ire1, (void *)ire));
35847c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
35857c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
35867c478bd9Sstevel@tonic-gate 			ire_delete(ire);
35877c478bd9Sstevel@tonic-gate 			if (pire != NULL) {
35887c478bd9Sstevel@tonic-gate 				/*
35897c478bd9Sstevel@tonic-gate 				 * Assert that it is not removed from the
35907c478bd9Sstevel@tonic-gate 				 * list yet.
35917c478bd9Sstevel@tonic-gate 				 */
35927c478bd9Sstevel@tonic-gate 				ASSERT(pire->ire_ptpn != NULL);
35937c478bd9Sstevel@tonic-gate 				IRB_REFRELE(pire->ire_bucket);
35947c478bd9Sstevel@tonic-gate 				ire_refrele(pire);
35957c478bd9Sstevel@tonic-gate 			}
35967c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
35977c478bd9Sstevel@tonic-gate 			return (0);
35987c478bd9Sstevel@tonic-gate 		}
35997c478bd9Sstevel@tonic-gate 	}
36007c478bd9Sstevel@tonic-gate 
36017c478bd9Sstevel@tonic-gate 	/*
36027c478bd9Sstevel@tonic-gate 	 * Make it easy for ip_wput_ire() to hit multiple broadcast ires by
36037c478bd9Sstevel@tonic-gate 	 * grouping identical addresses together on the hash chain. We also
36047c478bd9Sstevel@tonic-gate 	 * don't want to send multiple copies out if there are two ills part
36057c478bd9Sstevel@tonic-gate 	 * of the same group. Thus we group the ires with same addr and same
36067c478bd9Sstevel@tonic-gate 	 * ill group together so that ip_wput_ire can easily skip all the
36077c478bd9Sstevel@tonic-gate 	 * ires with same addr and same group after sending the first copy.
36087c478bd9Sstevel@tonic-gate 	 * We do this only for IRE_BROADCASTs as ip_wput_ire is currently
36097c478bd9Sstevel@tonic-gate 	 * interested in such groupings only for broadcasts.
36107c478bd9Sstevel@tonic-gate 	 *
36117c478bd9Sstevel@tonic-gate 	 * NOTE : If the interfaces are brought up first and then grouped,
36127c478bd9Sstevel@tonic-gate 	 * illgrp_insert will handle it. We come here when the interfaces
36137c478bd9Sstevel@tonic-gate 	 * are already in group and we are bringing them UP.
36147c478bd9Sstevel@tonic-gate 	 *
36157c478bd9Sstevel@tonic-gate 	 * Find the first entry that matches ire_addr. *irep will be null
36167c478bd9Sstevel@tonic-gate 	 * if no match.
36177c478bd9Sstevel@tonic-gate 	 */
36187c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
36197c478bd9Sstevel@tonic-gate 	while ((ire1 = *irep) != NULL && ire->ire_addr != ire1->ire_addr)
36207c478bd9Sstevel@tonic-gate 		irep = &ire1->ire_next;
36217c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_BROADCAST && *irep != NULL) {
36227c478bd9Sstevel@tonic-gate 		/*
36237c478bd9Sstevel@tonic-gate 		 * We found some ire (i.e *irep) with a matching addr. We
36247c478bd9Sstevel@tonic-gate 		 * want to group ires with same addr and same ill group
36257c478bd9Sstevel@tonic-gate 		 * together.
36267c478bd9Sstevel@tonic-gate 		 *
36277c478bd9Sstevel@tonic-gate 		 * First get to the entry that matches our address and
36287c478bd9Sstevel@tonic-gate 		 * ill group i.e stop as soon as we find the first ire
36297c478bd9Sstevel@tonic-gate 		 * matching the ill group and address. If there is only
36307c478bd9Sstevel@tonic-gate 		 * an address match, we should walk and look for some
36317c478bd9Sstevel@tonic-gate 		 * group match. These are some of the possible scenarios :
36327c478bd9Sstevel@tonic-gate 		 *
36337c478bd9Sstevel@tonic-gate 		 * 1) There are no groups at all i.e all ire's ill_group
36347c478bd9Sstevel@tonic-gate 		 *    are NULL. In that case we will essentially group
36357c478bd9Sstevel@tonic-gate 		 *    all the ires with the same addr together. Same as
36367c478bd9Sstevel@tonic-gate 		 *    the "else" block of this "if".
36377c478bd9Sstevel@tonic-gate 		 *
36387c478bd9Sstevel@tonic-gate 		 * 2) There are some groups and this ire's ill_group is
36397c478bd9Sstevel@tonic-gate 		 *    NULL. In this case, we will first find the group
36407c478bd9Sstevel@tonic-gate 		 *    that matches the address and a NULL group. Then
36417c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
36427c478bd9Sstevel@tonic-gate 		 *
36437c478bd9Sstevel@tonic-gate 		 * 3) There are some groups and this ires's ill_group is
36447c478bd9Sstevel@tonic-gate 		 *    non-NULL. In this case we will first find the group
36457c478bd9Sstevel@tonic-gate 		 *    that matches the address and the ill_group. Then
36467c478bd9Sstevel@tonic-gate 		 *    we will insert the ire at the end of that group.
36477c478bd9Sstevel@tonic-gate 		 */
36487c478bd9Sstevel@tonic-gate 		/* LINTED : constant in conditional context */
36497c478bd9Sstevel@tonic-gate 		while (1) {
36507c478bd9Sstevel@tonic-gate 			ire1 = *irep;
36517c478bd9Sstevel@tonic-gate 			if ((ire1->ire_next == NULL) ||
36527c478bd9Sstevel@tonic-gate 			    (ire1->ire_next->ire_addr != ire->ire_addr) ||
36537c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
36547c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group ==
36557c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
36567c478bd9Sstevel@tonic-gate 				break;
36577c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
36587c478bd9Sstevel@tonic-gate 		}
36597c478bd9Sstevel@tonic-gate 		ASSERT(*irep != NULL);
36607c478bd9Sstevel@tonic-gate 		irep = &((*irep)->ire_next);
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 		/*
36637c478bd9Sstevel@tonic-gate 		 * Either we have hit the end of the list or the address
36647c478bd9Sstevel@tonic-gate 		 * did not match or the group *matched*. If we found
36657c478bd9Sstevel@tonic-gate 		 * a match on the group, skip to the end of the group.
36667c478bd9Sstevel@tonic-gate 		 */
36677c478bd9Sstevel@tonic-gate 		while (*irep != NULL) {
36687c478bd9Sstevel@tonic-gate 			ire1 = *irep;
36697c478bd9Sstevel@tonic-gate 			if ((ire1->ire_addr != ire->ire_addr) ||
36707c478bd9Sstevel@tonic-gate 			    (ire1->ire_type != IRE_BROADCAST) ||
36717c478bd9Sstevel@tonic-gate 			    (ire1->ire_ipif->ipif_ill->ill_group !=
36727c478bd9Sstevel@tonic-gate 			    ire->ire_ipif->ipif_ill->ill_group))
36737c478bd9Sstevel@tonic-gate 				break;
36747c478bd9Sstevel@tonic-gate 			if (ire1->ire_ipif->ipif_ill->ill_group == NULL &&
36757c478bd9Sstevel@tonic-gate 			    ire1->ire_ipif == ire->ire_ipif) {
36767c478bd9Sstevel@tonic-gate 				irep = &ire1->ire_next;
36777c478bd9Sstevel@tonic-gate 				break;
36787c478bd9Sstevel@tonic-gate 			}
36797c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
36807c478bd9Sstevel@tonic-gate 		}
36817c478bd9Sstevel@tonic-gate 	} else if (*irep != NULL) {
36827c478bd9Sstevel@tonic-gate 		/*
36837c478bd9Sstevel@tonic-gate 		 * Find the last ire which matches ire_addr.
36847c478bd9Sstevel@tonic-gate 		 * Needed to do tail insertion among entries with the same
36857c478bd9Sstevel@tonic-gate 		 * ire_addr.
36867c478bd9Sstevel@tonic-gate 		 */
36877c478bd9Sstevel@tonic-gate 		while (ire->ire_addr == ire1->ire_addr) {
36887c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
36897c478bd9Sstevel@tonic-gate 			ire1 = *irep;
36907c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
36917c478bd9Sstevel@tonic-gate 				break;
36927c478bd9Sstevel@tonic-gate 		}
36937c478bd9Sstevel@tonic-gate 	}
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
36967c478bd9Sstevel@tonic-gate 		/*
36977c478bd9Sstevel@tonic-gate 		 * We keep a count of default gateways which is used when
36987c478bd9Sstevel@tonic-gate 		 * assigning them as routes.
36997c478bd9Sstevel@tonic-gate 		 */
37007c478bd9Sstevel@tonic-gate 		ip_ire_default_count++;
37017c478bd9Sstevel@tonic-gate 		ASSERT(ip_ire_default_count != 0); /* Wraparound */
37027c478bd9Sstevel@tonic-gate 	}
37037c478bd9Sstevel@tonic-gate 	/* Insert at *irep */
37047c478bd9Sstevel@tonic-gate 	ire1 = *irep;
37057c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
37067c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
37077c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
37087c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
37097c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	/*
37127c478bd9Sstevel@tonic-gate 	 * ire_walk routines de-reference ire_next without holding
37137c478bd9Sstevel@tonic-gate 	 * a lock. Before we point to the new ire, we want to make
37147c478bd9Sstevel@tonic-gate 	 * sure the store that sets the ire_next of the new ire
37157c478bd9Sstevel@tonic-gate 	 * reaches global visibility, so that ire_walk routines
37167c478bd9Sstevel@tonic-gate 	 * don't see a truncated list of ires i.e if the ire_next
37177c478bd9Sstevel@tonic-gate 	 * of the new ire gets set after we do "*irep = ire" due
37187c478bd9Sstevel@tonic-gate 	 * to re-ordering, the ire_walk thread will see a NULL
37197c478bd9Sstevel@tonic-gate 	 * once it accesses the ire_next of the new ire.
37207c478bd9Sstevel@tonic-gate 	 * membar_producer() makes sure that the following store
37217c478bd9Sstevel@tonic-gate 	 * happens *after* all of the above stores.
37227c478bd9Sstevel@tonic-gate 	 */
37237c478bd9Sstevel@tonic-gate 	membar_producer();
37247c478bd9Sstevel@tonic-gate 	*irep = ire;
37257c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
37267c478bd9Sstevel@tonic-gate 	/*
37277c478bd9Sstevel@tonic-gate 	 * We return a bumped up IRE above. Keep it symmetrical
37287c478bd9Sstevel@tonic-gate 	 * so that the callers will always have to release. This
37297c478bd9Sstevel@tonic-gate 	 * helps the callers of this function because they continue
37307c478bd9Sstevel@tonic-gate 	 * to use the IRE after adding and hence they don't have to
37317c478bd9Sstevel@tonic-gate 	 * lookup again after we return the IRE.
37327c478bd9Sstevel@tonic-gate 	 *
37337c478bd9Sstevel@tonic-gate 	 * NOTE : We don't have to use atomics as this is appearing
37347c478bd9Sstevel@tonic-gate 	 * in the list for the first time and no one else can bump
37357c478bd9Sstevel@tonic-gate 	 * up the reference count on this yet.
37367c478bd9Sstevel@tonic-gate 	 */
37377c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
37387c478bd9Sstevel@tonic-gate 	BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
37397c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
37407c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
37417c478bd9Sstevel@tonic-gate 		irb_ptr->irb_tmp_ire_cnt++;
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
37447c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
37457c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
37467c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
37477c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
37487c478bd9Sstevel@tonic-gate 		}
37497c478bd9Sstevel@tonic-gate 	} else {
37507c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
37517c478bd9Sstevel@tonic-gate 	}
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate 	if (pire != NULL) {
37567c478bd9Sstevel@tonic-gate 		/* Assert that it is not removed from the list yet */
37577c478bd9Sstevel@tonic-gate 		ASSERT(pire->ire_ptpn != NULL);
37587c478bd9Sstevel@tonic-gate 		IRB_REFRELE(pire->ire_bucket);
37597c478bd9Sstevel@tonic-gate 		ire_refrele(pire);
37607c478bd9Sstevel@tonic-gate 	}
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE) {
37637c478bd9Sstevel@tonic-gate 		/*
37647c478bd9Sstevel@tonic-gate 		 * For ire's with with host mask see if there is an entry
37657c478bd9Sstevel@tonic-gate 		 * in the cache. If there is one flush the whole cache as
37667c478bd9Sstevel@tonic-gate 		 * there might be multiple entries due to RTF_MULTIRT (CGTP).
37677c478bd9Sstevel@tonic-gate 		 * If no entry is found than there is no need to flush the
37687c478bd9Sstevel@tonic-gate 		 * cache.
37697c478bd9Sstevel@tonic-gate 		 */
37707c478bd9Sstevel@tonic-gate 		if (ire->ire_mask == IP_HOST_MASK) {
37717c478bd9Sstevel@tonic-gate 			ire_t *lire;
37727c478bd9Sstevel@tonic-gate 			lire = ire_ctable_lookup(ire->ire_addr, NULL, IRE_CACHE,
377345916cd2Sjpk 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
37747c478bd9Sstevel@tonic-gate 			if (lire != NULL) {
37757c478bd9Sstevel@tonic-gate 				ire_refrele(lire);
37767c478bd9Sstevel@tonic-gate 				ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
37777c478bd9Sstevel@tonic-gate 			}
37787c478bd9Sstevel@tonic-gate 		} else {
37797c478bd9Sstevel@tonic-gate 			ire_flush_cache_v4(ire, IRE_FLUSH_ADD);
37807c478bd9Sstevel@tonic-gate 		}
37817c478bd9Sstevel@tonic-gate 	}
37827c478bd9Sstevel@tonic-gate 	/*
37837c478bd9Sstevel@tonic-gate 	 * We had to delay the fast path probe until the ire is inserted
37847c478bd9Sstevel@tonic-gate 	 * in the list. Otherwise the fast path ack won't find the ire in
37857c478bd9Sstevel@tonic-gate 	 * the table.
37867c478bd9Sstevel@tonic-gate 	 */
37877c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_CACHE || ire->ire_type == IRE_BROADCAST)
37887c478bd9Sstevel@tonic-gate 		ire_fastpath(ire);
37897c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL)
37907c478bd9Sstevel@tonic-gate 		ASSERT(!MUTEX_HELD(&ire->ire_ipif->ipif_ill->ill_lock));
37917c478bd9Sstevel@tonic-gate 	*ire_p = ire;
37927c478bd9Sstevel@tonic-gate 	return (0);
37937c478bd9Sstevel@tonic-gate }
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate /*
37967c478bd9Sstevel@tonic-gate  * Search for all HOST REDIRECT routes that are
37977c478bd9Sstevel@tonic-gate  * pointing at the specified gateway and
37987c478bd9Sstevel@tonic-gate  * delete them. This routine is called only
37997c478bd9Sstevel@tonic-gate  * when a default gateway is going away.
38007c478bd9Sstevel@tonic-gate  */
38017c478bd9Sstevel@tonic-gate static void
38027c478bd9Sstevel@tonic-gate ire_delete_host_redirects(ipaddr_t gateway)
38037c478bd9Sstevel@tonic-gate {
38047c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
38057c478bd9Sstevel@tonic-gate 	irb_t *irb;
38067c478bd9Sstevel@tonic-gate 	ire_t *ire;
38077c478bd9Sstevel@tonic-gate 	int i;
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	/* get the hash table for HOST routes */
38107c478bd9Sstevel@tonic-gate 	irb_ptr = ip_forwarding_table[(IP_MASK_TABLE_SIZE - 1)];
38117c478bd9Sstevel@tonic-gate 	if (irb_ptr == NULL)
38127c478bd9Sstevel@tonic-gate 		return;
38137c478bd9Sstevel@tonic-gate 	for (i = 0; (i < ip_ftable_hash_size); i++) {
38147c478bd9Sstevel@tonic-gate 		irb = &irb_ptr[i];
38157c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb);
38167c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
38177c478bd9Sstevel@tonic-gate 			if (ire->ire_type != IRE_HOST_REDIRECT)
38187c478bd9Sstevel@tonic-gate 				continue;
38197c478bd9Sstevel@tonic-gate 			if (ire->ire_gateway_addr == gateway) {
38207c478bd9Sstevel@tonic-gate 				ire_delete(ire);
38217c478bd9Sstevel@tonic-gate 			}
38227c478bd9Sstevel@tonic-gate 		}
38237c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb);
38247c478bd9Sstevel@tonic-gate 	}
38257c478bd9Sstevel@tonic-gate }
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate /*
38287c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. ire_unlink calls to
38297c478bd9Sstevel@tonic-gate  * do the final cleanup for this ire.
38307c478bd9Sstevel@tonic-gate  */
38317c478bd9Sstevel@tonic-gate void
38327c478bd9Sstevel@tonic-gate ire_cleanup(ire_t *ire)
38337c478bd9Sstevel@tonic-gate {
38347c478bd9Sstevel@tonic-gate 	ire_t *ire_next;
38357c478bd9Sstevel@tonic-gate 
38367c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 	while (ire != NULL) {
38397c478bd9Sstevel@tonic-gate 		ire_next = ire->ire_next;
38407c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV4_VERSION) {
38417c478bd9Sstevel@tonic-gate 			ire_delete_v4(ire);
38427c478bd9Sstevel@tonic-gate 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_deleted);
38437c478bd9Sstevel@tonic-gate 		} else {
38447c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV6_VERSION);
38457c478bd9Sstevel@tonic-gate 			ire_delete_v6(ire);
38467c478bd9Sstevel@tonic-gate 			BUMP_IRE_STATS(ire_stats_v6, ire_stats_deleted);
38477c478bd9Sstevel@tonic-gate 		}
38487c478bd9Sstevel@tonic-gate 		/*
38497c478bd9Sstevel@tonic-gate 		 * Now it's really out of the list. Before doing the
38507c478bd9Sstevel@tonic-gate 		 * REFRELE, set ire_next to NULL as ire_inactive asserts
38517c478bd9Sstevel@tonic-gate 		 * so.
38527c478bd9Sstevel@tonic-gate 		 */
38537c478bd9Sstevel@tonic-gate 		ire->ire_next = NULL;
38547c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
38557c478bd9Sstevel@tonic-gate 		ire = ire_next;
38567c478bd9Sstevel@tonic-gate 	}
38577c478bd9Sstevel@tonic-gate }
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate /*
38607c478bd9Sstevel@tonic-gate  * IRB_REFRELE is the only caller of the function. It calls to unlink
38617c478bd9Sstevel@tonic-gate  * all the CONDEMNED ires from this bucket.
38627c478bd9Sstevel@tonic-gate  */
38637c478bd9Sstevel@tonic-gate ire_t *
38647c478bd9Sstevel@tonic-gate ire_unlink(irb_t *irb)
38657c478bd9Sstevel@tonic-gate {
38667c478bd9Sstevel@tonic-gate 	ire_t *ire;
38677c478bd9Sstevel@tonic-gate 	ire_t *ire1;
38687c478bd9Sstevel@tonic-gate 	ire_t **ptpn;
38697c478bd9Sstevel@tonic-gate 	ire_t *ire_list = NULL;
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&irb->irb_lock));
38727c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_refcnt == 0);
38737c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_marks & IRE_MARK_CONDEMNED);
38747c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_ire != NULL);
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire1) {
38777c478bd9Sstevel@tonic-gate 		ire1 = ire->ire_next;
38787c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
38797c478bd9Sstevel@tonic-gate 			ptpn = ire->ire_ptpn;
38807c478bd9Sstevel@tonic-gate 			ire1 = ire->ire_next;
38817c478bd9Sstevel@tonic-gate 			if (ire1)
38827c478bd9Sstevel@tonic-gate 				ire1->ire_ptpn = ptpn;
38837c478bd9Sstevel@tonic-gate 			*ptpn = ire1;
38847c478bd9Sstevel@tonic-gate 			ire->ire_ptpn = NULL;
38857c478bd9Sstevel@tonic-gate 			ire->ire_next = NULL;
38867c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_DEFAULT) {
38877c478bd9Sstevel@tonic-gate 				/*
38887c478bd9Sstevel@tonic-gate 				 * IRE is out of the list. We need to adjust
38897c478bd9Sstevel@tonic-gate 				 * the accounting before the caller drops
38907c478bd9Sstevel@tonic-gate 				 * the lock.
38917c478bd9Sstevel@tonic-gate 				 */
38927c478bd9Sstevel@tonic-gate 				if (ire->ire_ipversion == IPV6_VERSION) {
38937c478bd9Sstevel@tonic-gate 					ASSERT(ipv6_ire_default_count != 0);
38947c478bd9Sstevel@tonic-gate 					ipv6_ire_default_count--;
38957c478bd9Sstevel@tonic-gate 				} else {
38967c478bd9Sstevel@tonic-gate 					ASSERT(ip_ire_default_count != 0);
38977c478bd9Sstevel@tonic-gate 					ip_ire_default_count--;
38987c478bd9Sstevel@tonic-gate 				}
38997c478bd9Sstevel@tonic-gate 			}
39007c478bd9Sstevel@tonic-gate 			/*
39017c478bd9Sstevel@tonic-gate 			 * We need to call ire_delete_v4 or ire_delete_v6
39027c478bd9Sstevel@tonic-gate 			 * to clean up the cache or the redirects pointing at
39037c478bd9Sstevel@tonic-gate 			 * the default gateway. We need to drop the lock
39047c478bd9Sstevel@tonic-gate 			 * as ire_flush_cache/ire_delete_host_redircts require
39057c478bd9Sstevel@tonic-gate 			 * so. But we can't drop the lock, as ire_unlink needs
39067c478bd9Sstevel@tonic-gate 			 * to atomically remove the ires from the list.
39077c478bd9Sstevel@tonic-gate 			 * So, create a temporary list of CONDEMNED ires
39087c478bd9Sstevel@tonic-gate 			 * for doing ire_delete_v4/ire_delete_v6 operations
39097c478bd9Sstevel@tonic-gate 			 * later on.
39107c478bd9Sstevel@tonic-gate 			 */
39117c478bd9Sstevel@tonic-gate 			ire->ire_next = ire_list;
39127c478bd9Sstevel@tonic-gate 			ire_list = ire;
39137c478bd9Sstevel@tonic-gate 		}
39147c478bd9Sstevel@tonic-gate 	}
39157c478bd9Sstevel@tonic-gate 	ASSERT(irb->irb_refcnt == 0);
39167c478bd9Sstevel@tonic-gate 	irb->irb_marks &= ~IRE_MARK_CONDEMNED;
39177c478bd9Sstevel@tonic-gate 	ASSERT(ire_list != NULL);
39187c478bd9Sstevel@tonic-gate 	return (ire_list);
39197c478bd9Sstevel@tonic-gate }
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate /*
39227c478bd9Sstevel@tonic-gate  * Delete all the cache entries with this 'addr'.  When IP gets a gratuitous
39237c478bd9Sstevel@tonic-gate  * ARP message on any of its interface queue, it scans the cache table and
39247c478bd9Sstevel@tonic-gate  * deletes all the cache entries for that address. This function is called
39257c478bd9Sstevel@tonic-gate  * from ip_arp_news in ip.c and  also for ARP ioctl processing in ip_if.c.
39267c478bd9Sstevel@tonic-gate  * ip_ire_clookup_and_delete returns true if it finds at least one cache entry
39277c478bd9Sstevel@tonic-gate  * which is used by ip_arp_news to determine if it needs to do an ire_walk_v4.
39287c478bd9Sstevel@tonic-gate  * The return value is also  used for the same purpose by ARP IOCTL processing
39297c478bd9Sstevel@tonic-gate  * in ip_if.c when deleting ARP entries. For SIOC*IFARP ioctls in addition to
39307c478bd9Sstevel@tonic-gate  * the address, ip_if->ipif_ill also needs to be matched.
39317c478bd9Sstevel@tonic-gate  */
39327c478bd9Sstevel@tonic-gate boolean_t
39337c478bd9Sstevel@tonic-gate ip_ire_clookup_and_delete(ipaddr_t addr, ipif_t *ipif)
39347c478bd9Sstevel@tonic-gate {
39357c478bd9Sstevel@tonic-gate 	irb_t		*irb;
39367c478bd9Sstevel@tonic-gate 	ire_t		*cire;
39377c478bd9Sstevel@tonic-gate 	ill_t		*ill;
39387c478bd9Sstevel@tonic-gate 	boolean_t	found = B_FALSE, loop_end = B_FALSE;
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate 	irb = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
39417c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
39427c478bd9Sstevel@tonic-gate 	for (cire = irb->irb_ire; cire != NULL; cire = cire->ire_next) {
39437c478bd9Sstevel@tonic-gate 		if (cire->ire_marks & IRE_MARK_CONDEMNED)
39447c478bd9Sstevel@tonic-gate 			continue;
39457c478bd9Sstevel@tonic-gate 		if (cire->ire_addr == addr) {
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 			/* This signifies start of an address match */
39487c478bd9Sstevel@tonic-gate 			if (!loop_end)
39497c478bd9Sstevel@tonic-gate 				loop_end = B_TRUE;
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate 			/* We are interested only in IRE_CACHEs */
39527c478bd9Sstevel@tonic-gate 			if (cire->ire_type == IRE_CACHE) {
39537c478bd9Sstevel@tonic-gate 				/* If we want a match with the ILL */
39547c478bd9Sstevel@tonic-gate 				if (ipif != NULL &&
39557c478bd9Sstevel@tonic-gate 				    ((ill = ire_to_ill(cire)) == NULL ||
39567c478bd9Sstevel@tonic-gate 				    ill != ipif->ipif_ill)) {
39577c478bd9Sstevel@tonic-gate 					continue;
39587c478bd9Sstevel@tonic-gate 				}
39597c478bd9Sstevel@tonic-gate 				if (!found)
39607c478bd9Sstevel@tonic-gate 					found = B_TRUE;
39617c478bd9Sstevel@tonic-gate 				ire_delete(cire);
39627c478bd9Sstevel@tonic-gate 			}
39637c478bd9Sstevel@tonic-gate 		/* End of the match */
39647c478bd9Sstevel@tonic-gate 		} else if (loop_end)
39657c478bd9Sstevel@tonic-gate 			break;
39667c478bd9Sstevel@tonic-gate 	}
39677c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	return (found);
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate }
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate /*
39747c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
39757c478bd9Sstevel@tonic-gate  */
39767c478bd9Sstevel@tonic-gate void
39777c478bd9Sstevel@tonic-gate ire_delete(ire_t *ire)
39787c478bd9Sstevel@tonic-gate {
39797c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
39807c478bd9Sstevel@tonic-gate 	ire_t	**ptpn;
39817c478bd9Sstevel@tonic-gate 	irb_t *irb;
39827c478bd9Sstevel@tonic-gate 
39837c478bd9Sstevel@tonic-gate 	/*
39847c478bd9Sstevel@tonic-gate 	 * It was never inserted in the list. Should call REFRELE
39857c478bd9Sstevel@tonic-gate 	 * to free this IRE.
39867c478bd9Sstevel@tonic-gate 	 */
39877c478bd9Sstevel@tonic-gate 	if ((irb = ire->ire_bucket) == NULL) {
39887c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(ire);
39897c478bd9Sstevel@tonic-gate 		return;
39907c478bd9Sstevel@tonic-gate 	}
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate 	rw_enter(&irb->irb_lock, RW_WRITER);
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	/*
39957c478bd9Sstevel@tonic-gate 	 * In case of V4 we might still be waiting for fastpath ack.
39967c478bd9Sstevel@tonic-gate 	 */
39977c478bd9Sstevel@tonic-gate 	if (ire->ire_nce == NULL && ire->ire_stq != NULL) {
39987c478bd9Sstevel@tonic-gate 		ill_t *ill;
39997c478bd9Sstevel@tonic-gate 
40007c478bd9Sstevel@tonic-gate 		ill = ire_to_ill(ire);
40017c478bd9Sstevel@tonic-gate 		if (ill != NULL)
40027c478bd9Sstevel@tonic-gate 			ire_fastpath_list_delete(ill, ire);
40037c478bd9Sstevel@tonic-gate 	}
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate 	if (ire->ire_ptpn == NULL) {
40067c478bd9Sstevel@tonic-gate 		/*
40077c478bd9Sstevel@tonic-gate 		 * Some other thread has removed us from the list.
40087c478bd9Sstevel@tonic-gate 		 * It should have done the REFRELE for us.
40097c478bd9Sstevel@tonic-gate 		 */
40107c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
40117c478bd9Sstevel@tonic-gate 		return;
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	if (irb->irb_refcnt != 0) {
40157c478bd9Sstevel@tonic-gate 		/*
40167c478bd9Sstevel@tonic-gate 		 * The last thread to leave this bucket will
40177c478bd9Sstevel@tonic-gate 		 * delete this ire.
40187c478bd9Sstevel@tonic-gate 		 */
40197c478bd9Sstevel@tonic-gate 		if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) {
40207c478bd9Sstevel@tonic-gate 			irb->irb_ire_cnt--;
40217c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_TEMPORARY)
40227c478bd9Sstevel@tonic-gate 				irb->irb_tmp_ire_cnt--;
40237c478bd9Sstevel@tonic-gate 			ire->ire_marks |= IRE_MARK_CONDEMNED;
40247c478bd9Sstevel@tonic-gate 		}
40257c478bd9Sstevel@tonic-gate 		irb->irb_marks |= IRE_MARK_CONDEMNED;
40267c478bd9Sstevel@tonic-gate 		rw_exit(&irb->irb_lock);
40277c478bd9Sstevel@tonic-gate 		return;
40287c478bd9Sstevel@tonic-gate 	}
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 	/*
40317c478bd9Sstevel@tonic-gate 	 * Normally to delete an ire, we walk the bucket. While we
40327c478bd9Sstevel@tonic-gate 	 * walk the bucket, we normally bump up irb_refcnt and hence
40337c478bd9Sstevel@tonic-gate 	 * we return from above where we mark CONDEMNED and the ire
40347c478bd9Sstevel@tonic-gate 	 * gets deleted from ire_unlink. This case is where somebody
40357c478bd9Sstevel@tonic-gate 	 * knows the ire e.g by doing a lookup, and wants to delete the
40367c478bd9Sstevel@tonic-gate 	 * IRE. irb_refcnt would be 0 in this case if nobody is walking
40377c478bd9Sstevel@tonic-gate 	 * the bucket.
40387c478bd9Sstevel@tonic-gate 	 */
40397c478bd9Sstevel@tonic-gate 	ptpn = ire->ire_ptpn;
40407c478bd9Sstevel@tonic-gate 	ire1 = ire->ire_next;
40417c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
40427c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = ptpn;
40437c478bd9Sstevel@tonic-gate 	ASSERT(ptpn != NULL);
40447c478bd9Sstevel@tonic-gate 	*ptpn = ire1;
40457c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = NULL;
40467c478bd9Sstevel@tonic-gate 	ire->ire_next = NULL;
40477c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
40487c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v6, ire_stats_deleted);
40497c478bd9Sstevel@tonic-gate 	} else {
40507c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_deleted);
40517c478bd9Sstevel@tonic-gate 	}
40527c478bd9Sstevel@tonic-gate 	/*
40537c478bd9Sstevel@tonic-gate 	 * ip_wput/ip_wput_v6 checks this flag to see whether
40547c478bd9Sstevel@tonic-gate 	 * it should still use the cached ire or not.
40557c478bd9Sstevel@tonic-gate 	 */
40567c478bd9Sstevel@tonic-gate 	ire->ire_marks |= IRE_MARK_CONDEMNED;
40577c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
40587c478bd9Sstevel@tonic-gate 		/*
40597c478bd9Sstevel@tonic-gate 		 * IRE is out of the list. We need to adjust the
40607c478bd9Sstevel@tonic-gate 		 * accounting before we drop the lock.
40617c478bd9Sstevel@tonic-gate 		 */
40627c478bd9Sstevel@tonic-gate 		if (ire->ire_ipversion == IPV6_VERSION) {
40637c478bd9Sstevel@tonic-gate 			ASSERT(ipv6_ire_default_count != 0);
40647c478bd9Sstevel@tonic-gate 			ipv6_ire_default_count--;
40657c478bd9Sstevel@tonic-gate 		} else {
40667c478bd9Sstevel@tonic-gate 			ASSERT(ip_ire_default_count != 0);
40677c478bd9Sstevel@tonic-gate 			ip_ire_default_count--;
40687c478bd9Sstevel@tonic-gate 		}
40697c478bd9Sstevel@tonic-gate 	}
40707c478bd9Sstevel@tonic-gate 	irb->irb_ire_cnt--;
40717c478bd9Sstevel@tonic-gate 	if (ire->ire_marks & IRE_MARK_TEMPORARY)
40727c478bd9Sstevel@tonic-gate 		irb->irb_tmp_ire_cnt--;
40737c478bd9Sstevel@tonic-gate 	rw_exit(&irb->irb_lock);
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
40767c478bd9Sstevel@tonic-gate 		ire_delete_v6(ire);
40777c478bd9Sstevel@tonic-gate 	} else {
40787c478bd9Sstevel@tonic-gate 		ire_delete_v4(ire);
40797c478bd9Sstevel@tonic-gate 	}
40807c478bd9Sstevel@tonic-gate 	/*
40817c478bd9Sstevel@tonic-gate 	 * We removed it from the list. Decrement the
40827c478bd9Sstevel@tonic-gate 	 * reference count.
40837c478bd9Sstevel@tonic-gate 	 */
40847c478bd9Sstevel@tonic-gate 	IRE_REFRELE_NOTR(ire);
40857c478bd9Sstevel@tonic-gate }
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate /*
40887c478bd9Sstevel@tonic-gate  * Delete the specified IRE.
40897c478bd9Sstevel@tonic-gate  * All calls should use ire_delete().
40907c478bd9Sstevel@tonic-gate  * Sometimes called as writer though not required by this function.
40917c478bd9Sstevel@tonic-gate  *
40927c478bd9Sstevel@tonic-gate  * NOTE : This function is called only if the ire was added
40937c478bd9Sstevel@tonic-gate  * in the list.
40947c478bd9Sstevel@tonic-gate  */
40957c478bd9Sstevel@tonic-gate static void
40967c478bd9Sstevel@tonic-gate ire_delete_v4(ire_t *ire)
40977c478bd9Sstevel@tonic-gate {
40987c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt >= 1);
40997c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
41027c478bd9Sstevel@tonic-gate 		ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
41037c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT) {
41047c478bd9Sstevel@tonic-gate 		/*
41057c478bd9Sstevel@tonic-gate 		 * when a default gateway is going away
41067c478bd9Sstevel@tonic-gate 		 * delete all the host redirects pointing at that
41077c478bd9Sstevel@tonic-gate 		 * gateway.
41087c478bd9Sstevel@tonic-gate 		 */
41097c478bd9Sstevel@tonic-gate 		ire_delete_host_redirects(ire->ire_gateway_addr);
41107c478bd9Sstevel@tonic-gate 	}
41117c478bd9Sstevel@tonic-gate }
41127c478bd9Sstevel@tonic-gate 
41137c478bd9Sstevel@tonic-gate /*
41147c478bd9Sstevel@tonic-gate  * IRE_REFRELE/ire_refrele are the only caller of the function. It calls
41157c478bd9Sstevel@tonic-gate  * to free the ire when the reference count goes to zero.
41167c478bd9Sstevel@tonic-gate  */
41177c478bd9Sstevel@tonic-gate void
41187c478bd9Sstevel@tonic-gate ire_inactive(ire_t *ire)
41197c478bd9Sstevel@tonic-gate {
41207c478bd9Sstevel@tonic-gate 	mblk_t *mp;
41217c478bd9Sstevel@tonic-gate 	nce_t	*nce;
41227c478bd9Sstevel@tonic-gate 	ill_t	*ill = NULL;
41237c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill = NULL;
41247c478bd9Sstevel@tonic-gate 	ill_t	*in_ill = NULL;
41257c478bd9Sstevel@tonic-gate 	ipif_t	*ipif;
41267c478bd9Sstevel@tonic-gate 	boolean_t	need_wakeup = B_FALSE;
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_refcnt == 0);
41297c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ptpn == NULL);
41307c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_next == NULL);
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate 	if ((nce = ire->ire_nce) != NULL) {
41337c478bd9Sstevel@tonic-gate 		/* Only IPv6 IRE_CACHE type has an nce */
41347c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_type == IRE_CACHE);
41357c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_ipversion == IPV6_VERSION);
41367c478bd9Sstevel@tonic-gate 		NCE_REFRELE_NOTR(nce);
41377c478bd9Sstevel@tonic-gate 		ire->ire_nce = NULL;
41387c478bd9Sstevel@tonic-gate 	}
41397c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif == NULL)
41407c478bd9Sstevel@tonic-gate 		goto end;
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate 	ipif = ire->ire_ipif;
41437c478bd9Sstevel@tonic-gate 	ill = ipif->ipif_ill;
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	if (ire->ire_bucket == NULL) {
41467c478bd9Sstevel@tonic-gate 		/* The ire was never inserted in the table. */
41477c478bd9Sstevel@tonic-gate 		goto end;
41487c478bd9Sstevel@tonic-gate 	}
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate 	/*
41517c478bd9Sstevel@tonic-gate 	 * ipif_ire_cnt on this ipif goes down by 1. If the ire_stq is
41527c478bd9Sstevel@tonic-gate 	 * non-null ill_ire_count also goes down by 1. If the in_ill is
41537c478bd9Sstevel@tonic-gate 	 * non-null either ill_mrtun_refcnt or ill_srcif_refcnt goes down by 1.
41547c478bd9Sstevel@tonic-gate 	 *
41557c478bd9Sstevel@tonic-gate 	 * The ipif that is associated with an ire is ire->ire_ipif and
41567c478bd9Sstevel@tonic-gate 	 * hence when the ire->ire_ipif->ipif_ire_cnt drops to zero we call
41577c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail. Usually stq_ill is null or the same as
41587c478bd9Sstevel@tonic-gate 	 * ire->ire_ipif->ipif_ill. So nothing more needs to be done. Only
41597c478bd9Sstevel@tonic-gate 	 * in the case of IRE_CACHES when IPMP is used, stq_ill can be
41607c478bd9Sstevel@tonic-gate 	 * different. If this is different from ire->ire_ipif->ipif_ill and
41617c478bd9Sstevel@tonic-gate 	 * if the ill_ire_cnt on the stq_ill also has dropped to zero, we call
41627c478bd9Sstevel@tonic-gate 	 * ipif_ill_refrele_tail on the stq_ill. If mobile ip is in use
41637c478bd9Sstevel@tonic-gate 	 * in_ill could be non-null. If it is a reverse tunnel related ire
41647c478bd9Sstevel@tonic-gate 	 * ill_mrtun_refcnt is non-zero. If it is forward tunnel related ire
41657c478bd9Sstevel@tonic-gate 	 * ill_srcif_refcnt is non-null.
41667c478bd9Sstevel@tonic-gate 	 */
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	if (ire->ire_stq != NULL)
41697c478bd9Sstevel@tonic-gate 		stq_ill = (ill_t *)ire->ire_stq->q_ptr;
41707c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill != NULL)
41717c478bd9Sstevel@tonic-gate 		in_ill = ire->ire_in_ill;
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 	if ((stq_ill == NULL || stq_ill == ill) && (in_ill == NULL)) {
41747c478bd9Sstevel@tonic-gate 		/* Optimize the most common case */
41757c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
41767c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
41777c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
41787c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0)
41797c478bd9Sstevel@tonic-gate 			need_wakeup = B_TRUE;
41807c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
41817c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
41827c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
41837c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)
41847c478bd9Sstevel@tonic-gate 				need_wakeup = B_TRUE;
41857c478bd9Sstevel@tonic-gate 		}
41867c478bd9Sstevel@tonic-gate 		if (need_wakeup) {
41877c478bd9Sstevel@tonic-gate 			/* Drops the ill lock */
41887c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
41897c478bd9Sstevel@tonic-gate 		} else {
41907c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
41917c478bd9Sstevel@tonic-gate 		}
41927c478bd9Sstevel@tonic-gate 	} else {
41937c478bd9Sstevel@tonic-gate 		/*
41947c478bd9Sstevel@tonic-gate 		 * We can't grab all the ill locks at the same time.
41957c478bd9Sstevel@tonic-gate 		 * It can lead to recursive lock enter in the call to
41967c478bd9Sstevel@tonic-gate 		 * ipif_ill_refrele_tail and later. Instead do it 1 at
41977c478bd9Sstevel@tonic-gate 		 * a time.
41987c478bd9Sstevel@tonic-gate 		 */
41997c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
42007c478bd9Sstevel@tonic-gate 		ASSERT(ipif->ipif_ire_cnt != 0);
42017c478bd9Sstevel@tonic-gate 		ipif->ipif_ire_cnt--;
42027c478bd9Sstevel@tonic-gate 		if (ipif->ipif_ire_cnt == 0) {
42037c478bd9Sstevel@tonic-gate 			/* Drops the lock */
42047c478bd9Sstevel@tonic-gate 			ipif_ill_refrele_tail(ill);
42057c478bd9Sstevel@tonic-gate 		} else {
42067c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
42077c478bd9Sstevel@tonic-gate 		}
42087c478bd9Sstevel@tonic-gate 		if (stq_ill != NULL) {
42097c478bd9Sstevel@tonic-gate 			mutex_enter(&stq_ill->ill_lock);
42107c478bd9Sstevel@tonic-gate 			ASSERT(stq_ill->ill_ire_cnt != 0);
42117c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt--;
42127c478bd9Sstevel@tonic-gate 			if (stq_ill->ill_ire_cnt == 0)  {
42137c478bd9Sstevel@tonic-gate 				/* Drops the ill lock */
42147c478bd9Sstevel@tonic-gate 				ipif_ill_refrele_tail(stq_ill);
42157c478bd9Sstevel@tonic-gate 			} else {
42167c478bd9Sstevel@tonic-gate 				mutex_exit(&stq_ill->ill_lock);
42177c478bd9Sstevel@tonic-gate 			}
42187c478bd9Sstevel@tonic-gate 		}
42197c478bd9Sstevel@tonic-gate 		if (in_ill != NULL) {
42207c478bd9Sstevel@tonic-gate 			mutex_enter(&in_ill->ill_lock);
42217c478bd9Sstevel@tonic-gate 			if (ire->ire_type == IRE_MIPRTUN) {
42227c478bd9Sstevel@tonic-gate 				/*
42237c478bd9Sstevel@tonic-gate 				 * Mobile IP reverse tunnel ire.
42247c478bd9Sstevel@tonic-gate 				 * Decrement table count and the
42257c478bd9Sstevel@tonic-gate 				 * ill reference count. This signifies
42267c478bd9Sstevel@tonic-gate 				 * mipagent is deleting reverse tunnel
42277c478bd9Sstevel@tonic-gate 				 * route for a particular mobile node.
42287c478bd9Sstevel@tonic-gate 				 */
42297c478bd9Sstevel@tonic-gate 				mutex_enter(&ire_mrtun_lock);
42307c478bd9Sstevel@tonic-gate 				ire_mrtun_count--;
42317c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_mrtun_lock);
42327c478bd9Sstevel@tonic-gate 				ASSERT(in_ill->ill_mrtun_refcnt != 0);
42337c478bd9Sstevel@tonic-gate 				in_ill->ill_mrtun_refcnt--;
42347c478bd9Sstevel@tonic-gate 				if (in_ill->ill_mrtun_refcnt == 0) {
42357c478bd9Sstevel@tonic-gate 					/* Drops the ill lock */
42367c478bd9Sstevel@tonic-gate 					ipif_ill_refrele_tail(in_ill);
42377c478bd9Sstevel@tonic-gate 				} else {
42387c478bd9Sstevel@tonic-gate 					mutex_exit(&in_ill->ill_lock);
42397c478bd9Sstevel@tonic-gate 				}
42407c478bd9Sstevel@tonic-gate 			} else {
42417c478bd9Sstevel@tonic-gate 				mutex_enter(&ire_srcif_table_lock);
42427c478bd9Sstevel@tonic-gate 				ire_srcif_table_count--;
42437c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_srcif_table_lock);
42447c478bd9Sstevel@tonic-gate 				ASSERT(in_ill->ill_srcif_refcnt != 0);
42457c478bd9Sstevel@tonic-gate 				in_ill->ill_srcif_refcnt--;
42467c478bd9Sstevel@tonic-gate 				if (in_ill->ill_srcif_refcnt == 0) {
42477c478bd9Sstevel@tonic-gate 					/* Drops the ill lock */
42487c478bd9Sstevel@tonic-gate 					ipif_ill_refrele_tail(in_ill);
42497c478bd9Sstevel@tonic-gate 				} else {
42507c478bd9Sstevel@tonic-gate 					mutex_exit(&in_ill->ill_lock);
42517c478bd9Sstevel@tonic-gate 				}
42527c478bd9Sstevel@tonic-gate 			}
42537c478bd9Sstevel@tonic-gate 		}
42547c478bd9Sstevel@tonic-gate 	}
42557c478bd9Sstevel@tonic-gate end:
42567c478bd9Sstevel@tonic-gate 	/* This should be true for both V4 and V6 */
42577c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_fastpath == NULL);
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate 
42607c478bd9Sstevel@tonic-gate 	ire->ire_ipif = NULL;
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	/* Free the xmit header, and the IRE itself. */
42637c478bd9Sstevel@tonic-gate 	if ((mp = ire->ire_dlureq_mp) != NULL) {
42647c478bd9Sstevel@tonic-gate 		freeb(mp);
42657c478bd9Sstevel@tonic-gate 		ire->ire_dlureq_mp = NULL;
42667c478bd9Sstevel@tonic-gate 	}
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate 	if ((mp = ire->ire_fp_mp) != NULL) {
42697c478bd9Sstevel@tonic-gate 		freeb(mp);
42707c478bd9Sstevel@tonic-gate 		ire->ire_fp_mp = NULL;
42717c478bd9Sstevel@tonic-gate 	}
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill != NULL) {
42747c478bd9Sstevel@tonic-gate 		ire->ire_in_ill = NULL;
42757c478bd9Sstevel@tonic-gate 	}
42767c478bd9Sstevel@tonic-gate 
427745916cd2Sjpk 	if (ire->ire_gw_secattr != NULL) {
427845916cd2Sjpk 		ire_gw_secattr_free(ire->ire_gw_secattr);
427945916cd2Sjpk 		ire->ire_gw_secattr = NULL;
428045916cd2Sjpk 	}
42817c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
42827c478bd9Sstevel@tonic-gate 	ire_trace_inactive(ire);
42837c478bd9Sstevel@tonic-gate #endif
42847c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire->ire_lock);
42857c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
42867c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v6, ire_stats_freed);
42877c478bd9Sstevel@tonic-gate 	} else {
42887c478bd9Sstevel@tonic-gate 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_freed);
42897c478bd9Sstevel@tonic-gate 	}
42907c478bd9Sstevel@tonic-gate 	if (ire->ire_mp != NULL) {
42917c478bd9Sstevel@tonic-gate 		/* Still in an mblk */
42927c478bd9Sstevel@tonic-gate 		freeb(ire->ire_mp);
42937c478bd9Sstevel@tonic-gate 	} else {
42947c478bd9Sstevel@tonic-gate 		/* Has been allocated out of the cache */
42957c478bd9Sstevel@tonic-gate 		kmem_cache_free(ire_cache, ire);
42967c478bd9Sstevel@tonic-gate 	}
42977c478bd9Sstevel@tonic-gate }
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate /*
43007c478bd9Sstevel@tonic-gate  * ire_walk routine to delete all IRE_CACHE/IRE_HOST_REDIRECT entries
43017c478bd9Sstevel@tonic-gate  * that have a given gateway address.
43027c478bd9Sstevel@tonic-gate  */
43037c478bd9Sstevel@tonic-gate void
43047c478bd9Sstevel@tonic-gate ire_delete_cache_gw(ire_t *ire, char *cp)
43057c478bd9Sstevel@tonic-gate {
43067c478bd9Sstevel@tonic-gate 	ipaddr_t	gw_addr;
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate 	if (!(ire->ire_type & (IRE_CACHE|IRE_HOST_REDIRECT)))
43097c478bd9Sstevel@tonic-gate 		return;
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 	bcopy(cp, &gw_addr, sizeof (gw_addr));
43127c478bd9Sstevel@tonic-gate 	if (ire->ire_gateway_addr == gw_addr) {
43137c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_delete_cache_gw: deleted 0x%x type %d to 0x%x\n",
43147c478bd9Sstevel@tonic-gate 			(int)ntohl(ire->ire_addr), ire->ire_type,
43157c478bd9Sstevel@tonic-gate 			(int)ntohl(ire->ire_gateway_addr)));
43167c478bd9Sstevel@tonic-gate 		ire_delete(ire);
43177c478bd9Sstevel@tonic-gate 	}
43187c478bd9Sstevel@tonic-gate }
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate /*
43217c478bd9Sstevel@tonic-gate  * Remove all IRE_CACHE entries that match the ire specified.
43227c478bd9Sstevel@tonic-gate  *
43237c478bd9Sstevel@tonic-gate  * The flag argument indicates if the flush request is due to addition
43247c478bd9Sstevel@tonic-gate  * of new route (IRE_FLUSH_ADD) or deletion of old route (IRE_FLUSH_DELETE).
43257c478bd9Sstevel@tonic-gate  *
43267c478bd9Sstevel@tonic-gate  * This routine takes only the IREs from the forwarding table and flushes
43277c478bd9Sstevel@tonic-gate  * the corresponding entries from the cache table.
43287c478bd9Sstevel@tonic-gate  *
43297c478bd9Sstevel@tonic-gate  * When flushing due to the deletion of an old route, it
43307c478bd9Sstevel@tonic-gate  * just checks the cache handles (ire_phandle and ire_ihandle) and
43317c478bd9Sstevel@tonic-gate  * deletes the ones that match.
43327c478bd9Sstevel@tonic-gate  *
43337c478bd9Sstevel@tonic-gate  * When flushing due to the creation of a new route, it checks
43347c478bd9Sstevel@tonic-gate  * if a cache entry's address matches the one in the IRE and
43357c478bd9Sstevel@tonic-gate  * that the cache entry's parent has a less specific mask than the
43367c478bd9Sstevel@tonic-gate  * one in IRE. The destination of such a cache entry could be the
43377c478bd9Sstevel@tonic-gate  * gateway for other cache entries, so we need to flush those as
43387c478bd9Sstevel@tonic-gate  * well by looking for gateway addresses matching the IRE's address.
43397c478bd9Sstevel@tonic-gate  */
43407c478bd9Sstevel@tonic-gate void
43417c478bd9Sstevel@tonic-gate ire_flush_cache_v4(ire_t *ire, int flag)
43427c478bd9Sstevel@tonic-gate {
43437c478bd9Sstevel@tonic-gate 	int i;
43447c478bd9Sstevel@tonic-gate 	ire_t *cire;
43457c478bd9Sstevel@tonic-gate 	irb_t *irb;
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	if (ire->ire_type & IRE_CACHE)
43487c478bd9Sstevel@tonic-gate 	    return;
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 	/*
43517c478bd9Sstevel@tonic-gate 	 * If a default is just created, there is no point
43527c478bd9Sstevel@tonic-gate 	 * in going through the cache, as there will not be any
43537c478bd9Sstevel@tonic-gate 	 * cached ires.
43547c478bd9Sstevel@tonic-gate 	 */
43557c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_DEFAULT && flag == IRE_FLUSH_ADD)
43567c478bd9Sstevel@tonic-gate 		return;
43577c478bd9Sstevel@tonic-gate 	if (flag == IRE_FLUSH_ADD) {
43587c478bd9Sstevel@tonic-gate 		/*
43597c478bd9Sstevel@tonic-gate 		 * This selective flush is due to the addition of
43607c478bd9Sstevel@tonic-gate 		 * new IRE.
43617c478bd9Sstevel@tonic-gate 		 */
43627c478bd9Sstevel@tonic-gate 		for (i = 0; i < ip_cache_table_size; i++) {
43637c478bd9Sstevel@tonic-gate 			irb = &ip_cache_table[i];
43647c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
43657c478bd9Sstevel@tonic-gate 				continue;
43667c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
43677c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
43687c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
43697c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
43707c478bd9Sstevel@tonic-gate 					continue;
43717c478bd9Sstevel@tonic-gate 				/*
43727c478bd9Sstevel@tonic-gate 				 * If 'cire' belongs to the same subnet
43737c478bd9Sstevel@tonic-gate 				 * as the new ire being added, and 'cire'
43747c478bd9Sstevel@tonic-gate 				 * is derived from a prefix that is less
43757c478bd9Sstevel@tonic-gate 				 * specific than the new ire being added,
43767c478bd9Sstevel@tonic-gate 				 * we need to flush 'cire'; for instance,
43777c478bd9Sstevel@tonic-gate 				 * when a new interface comes up.
43787c478bd9Sstevel@tonic-gate 				 */
43797c478bd9Sstevel@tonic-gate 				if (((cire->ire_addr & ire->ire_mask) ==
43807c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) &&
43817c478bd9Sstevel@tonic-gate 				    (ip_mask_to_plen(cire->ire_cmask) <=
43827c478bd9Sstevel@tonic-gate 				    ire->ire_masklen)) {
43837c478bd9Sstevel@tonic-gate 					ire_delete(cire);
43847c478bd9Sstevel@tonic-gate 					continue;
43857c478bd9Sstevel@tonic-gate 				}
43867c478bd9Sstevel@tonic-gate 				/*
43877c478bd9Sstevel@tonic-gate 				 * This is the case when the ire_gateway_addr
43887c478bd9Sstevel@tonic-gate 				 * of 'cire' belongs to the same subnet as
43897c478bd9Sstevel@tonic-gate 				 * the new ire being added.
43907c478bd9Sstevel@tonic-gate 				 * Flushing such ires is sometimes required to
43917c478bd9Sstevel@tonic-gate 				 * avoid misrouting: say we have a machine with
43927c478bd9Sstevel@tonic-gate 				 * two interfaces (I1 and I2), a default router
43937c478bd9Sstevel@tonic-gate 				 * R on the I1 subnet, and a host route to an
43947c478bd9Sstevel@tonic-gate 				 * off-link destination D with a gateway G on
43957c478bd9Sstevel@tonic-gate 				 * the I2 subnet.
43967c478bd9Sstevel@tonic-gate 				 * Under normal operation, we will have an
43977c478bd9Sstevel@tonic-gate 				 * on-link cache entry for G and an off-link
43987c478bd9Sstevel@tonic-gate 				 * cache entry for D with G as ire_gateway_addr,
43997c478bd9Sstevel@tonic-gate 				 * traffic to D will reach its destination
44007c478bd9Sstevel@tonic-gate 				 * through gateway G.
44017c478bd9Sstevel@tonic-gate 				 * If the administrator does 'ifconfig I2 down',
44027c478bd9Sstevel@tonic-gate 				 * the cache entries for D and G will be
44037c478bd9Sstevel@tonic-gate 				 * flushed. However, G will now be resolved as
44047c478bd9Sstevel@tonic-gate 				 * an off-link destination using R (the default
44057c478bd9Sstevel@tonic-gate 				 * router) as gateway. Then D will also be
44067c478bd9Sstevel@tonic-gate 				 * resolved as an off-link destination using G
44077c478bd9Sstevel@tonic-gate 				 * as gateway - this behavior is due to
44087c478bd9Sstevel@tonic-gate 				 * compatibility reasons, see comment in
44097c478bd9Sstevel@tonic-gate 				 * ire_ihandle_lookup_offlink(). Traffic to D
44107c478bd9Sstevel@tonic-gate 				 * will go to the router R and probably won't
44117c478bd9Sstevel@tonic-gate 				 * reach the destination.
44127c478bd9Sstevel@tonic-gate 				 * The administrator then does 'ifconfig I2 up'.
44137c478bd9Sstevel@tonic-gate 				 * Since G is on the I2 subnet, this routine
44147c478bd9Sstevel@tonic-gate 				 * will flush its cache entry. It must also
44157c478bd9Sstevel@tonic-gate 				 * flush the cache entry for D, otherwise
44167c478bd9Sstevel@tonic-gate 				 * traffic will stay misrouted until the IRE
44177c478bd9Sstevel@tonic-gate 				 * times out.
44187c478bd9Sstevel@tonic-gate 				 */
44197c478bd9Sstevel@tonic-gate 				if ((cire->ire_gateway_addr & ire->ire_mask) ==
44207c478bd9Sstevel@tonic-gate 				    (ire->ire_addr & ire->ire_mask)) {
44217c478bd9Sstevel@tonic-gate 					ire_delete(cire);
44227c478bd9Sstevel@tonic-gate 					continue;
44237c478bd9Sstevel@tonic-gate 				}
44247c478bd9Sstevel@tonic-gate 			}
44257c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
44267c478bd9Sstevel@tonic-gate 		}
44277c478bd9Sstevel@tonic-gate 	} else {
44287c478bd9Sstevel@tonic-gate 		/*
44297c478bd9Sstevel@tonic-gate 		 * delete the cache entries based on
44307c478bd9Sstevel@tonic-gate 		 * handle in the IRE as this IRE is
44317c478bd9Sstevel@tonic-gate 		 * being deleted/changed.
44327c478bd9Sstevel@tonic-gate 		 */
44337c478bd9Sstevel@tonic-gate 		for (i = 0; i < ip_cache_table_size; i++) {
44347c478bd9Sstevel@tonic-gate 			irb = &ip_cache_table[i];
44357c478bd9Sstevel@tonic-gate 			if ((cire = irb->irb_ire) == NULL)
44367c478bd9Sstevel@tonic-gate 				continue;
44377c478bd9Sstevel@tonic-gate 			IRB_REFHOLD(irb);
44387c478bd9Sstevel@tonic-gate 			for (cire = irb->irb_ire; cire != NULL;
44397c478bd9Sstevel@tonic-gate 			    cire = cire->ire_next) {
44407c478bd9Sstevel@tonic-gate 				if (cire->ire_type != IRE_CACHE)
44417c478bd9Sstevel@tonic-gate 					continue;
44427c478bd9Sstevel@tonic-gate 				if ((cire->ire_phandle == 0 ||
44437c478bd9Sstevel@tonic-gate 				    cire->ire_phandle != ire->ire_phandle) &&
44447c478bd9Sstevel@tonic-gate 				    (cire->ire_ihandle == 0 ||
44457c478bd9Sstevel@tonic-gate 				    cire->ire_ihandle != ire->ire_ihandle))
44467c478bd9Sstevel@tonic-gate 					continue;
44477c478bd9Sstevel@tonic-gate 				ire_delete(cire);
44487c478bd9Sstevel@tonic-gate 			}
44497c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb);
44507c478bd9Sstevel@tonic-gate 		}
44517c478bd9Sstevel@tonic-gate 	}
44527c478bd9Sstevel@tonic-gate }
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate /*
44557c478bd9Sstevel@tonic-gate  * Matches the arguments passed with the values in the ire.
44567c478bd9Sstevel@tonic-gate  *
44577c478bd9Sstevel@tonic-gate  * Note: for match types that match using "ipif" passed in, ipif
44587c478bd9Sstevel@tonic-gate  * must be checked for non-NULL before calling this routine.
44597c478bd9Sstevel@tonic-gate  */
44607c478bd9Sstevel@tonic-gate static boolean_t
44617c478bd9Sstevel@tonic-gate ire_match_args(ire_t *ire, ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
446245916cd2Sjpk     int type, const ipif_t *ipif, zoneid_t zoneid, uint32_t ihandle,
446345916cd2Sjpk     const ts_label_t *tsl, int match_flags)
44647c478bd9Sstevel@tonic-gate {
44657c478bd9Sstevel@tonic-gate 	ill_t *ire_ill = NULL, *dst_ill;
44667c478bd9Sstevel@tonic-gate 	ill_t *ipif_ill = NULL;
44677c478bd9Sstevel@tonic-gate 	ill_group_t *ire_ill_group = NULL;
44687c478bd9Sstevel@tonic-gate 	ill_group_t *ipif_ill_group = NULL;
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
44717c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_addr & ~ire->ire_mask) == 0);
44727c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP))) ||
44737c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
44747c478bd9Sstevel@tonic-gate 	ASSERT(!(match_flags & MATCH_IRE_WQ));
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	/*
44777c478bd9Sstevel@tonic-gate 	 * HIDDEN cache entries have to be looked up specifically with
44787c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_MARK_HIDDEN. MATCH_IRE_MARK_HIDDEN is usually set
44797c478bd9Sstevel@tonic-gate 	 * when the interface is FAILED or INACTIVE. In that case,
44807c478bd9Sstevel@tonic-gate 	 * any IRE_CACHES that exists should be marked with
44817c478bd9Sstevel@tonic-gate 	 * IRE_MARK_HIDDEN. So, we don't really need to match below
44827c478bd9Sstevel@tonic-gate 	 * for IRE_MARK_HIDDEN. But we do so for consistency.
44837c478bd9Sstevel@tonic-gate 	 */
44847c478bd9Sstevel@tonic-gate 	if (!(match_flags & MATCH_IRE_MARK_HIDDEN) &&
44857c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_HIDDEN))
44867c478bd9Sstevel@tonic-gate 		return (B_FALSE);
44877c478bd9Sstevel@tonic-gate 
448843d18f1cSpriyanka 	/*
448943d18f1cSpriyanka 	 * MATCH_IRE_MARK_PRIVATE_ADDR is set when IP_NEXTHOP option
449043d18f1cSpriyanka 	 * is used. In that case the routing table is bypassed and the
449143d18f1cSpriyanka 	 * packets are sent directly to the specified nexthop. The
449243d18f1cSpriyanka 	 * IRE_CACHE entry representing this route should be marked
449343d18f1cSpriyanka 	 * with IRE_MARK_PRIVATE_ADDR.
449443d18f1cSpriyanka 	 */
449543d18f1cSpriyanka 
449643d18f1cSpriyanka 	if (!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR) &&
449743d18f1cSpriyanka 	    (ire->ire_marks & IRE_MARK_PRIVATE_ADDR))
449843d18f1cSpriyanka 		return (B_FALSE);
449943d18f1cSpriyanka 
450045916cd2Sjpk 	if (zoneid != ALL_ZONES && zoneid != ire->ire_zoneid &&
450145916cd2Sjpk 	    ire->ire_zoneid != ALL_ZONES) {
45027c478bd9Sstevel@tonic-gate 		/*
45037c478bd9Sstevel@tonic-gate 		 * If MATCH_IRE_ZONEONLY has been set and the supplied zoneid is
45047c478bd9Sstevel@tonic-gate 		 * valid and does not match that of ire_zoneid, a failure to
45057c478bd9Sstevel@tonic-gate 		 * match is reported at this point. Otherwise, since some IREs
45067c478bd9Sstevel@tonic-gate 		 * that are available in the global zone can be used in local
45077c478bd9Sstevel@tonic-gate 		 * zones, additional checks need to be performed:
45087c478bd9Sstevel@tonic-gate 		 *
45097c478bd9Sstevel@tonic-gate 		 *	IRE_BROADCAST, IRE_CACHE and IRE_LOOPBACK
45107c478bd9Sstevel@tonic-gate 		 *	entries should never be matched in this situation.
45117c478bd9Sstevel@tonic-gate 		 *
45127c478bd9Sstevel@tonic-gate 		 *	IRE entries that have an interface associated with them
45137c478bd9Sstevel@tonic-gate 		 *	should in general not match unless they are an IRE_LOCAL
45147c478bd9Sstevel@tonic-gate 		 *	or in the case when MATCH_IRE_DEFAULT has been set in
45157c478bd9Sstevel@tonic-gate 		 *	the caller.  In the case of the former, checking of the
45167c478bd9Sstevel@tonic-gate 		 *	other fields supplied should take place.
45177c478bd9Sstevel@tonic-gate 		 *
45187c478bd9Sstevel@tonic-gate 		 *	In the case where MATCH_IRE_DEFAULT has been set,
45197c478bd9Sstevel@tonic-gate 		 *	all of the ipif's associated with the IRE's ill are
45207c478bd9Sstevel@tonic-gate 		 *	checked to see if there is a matching zoneid.  If any
45217c478bd9Sstevel@tonic-gate 		 *	one ipif has a matching zoneid, this IRE is a
45227c478bd9Sstevel@tonic-gate 		 *	potential candidate so checking of the other fields
45237c478bd9Sstevel@tonic-gate 		 *	takes place.
45247c478bd9Sstevel@tonic-gate 		 *
45257c478bd9Sstevel@tonic-gate 		 *	In the case where the IRE_INTERFACE has a usable source
45267c478bd9Sstevel@tonic-gate 		 *	address (indicated by ill_usesrc_ifindex) in the
45277c478bd9Sstevel@tonic-gate 		 *	correct zone then it's permitted to return this IRE
45287c478bd9Sstevel@tonic-gate 		 */
45297c478bd9Sstevel@tonic-gate 		if (match_flags & MATCH_IRE_ZONEONLY)
45307c478bd9Sstevel@tonic-gate 			return (B_FALSE);
45317c478bd9Sstevel@tonic-gate 		if (ire->ire_type & (IRE_BROADCAST | IRE_CACHE | IRE_LOOPBACK))
45327c478bd9Sstevel@tonic-gate 			return (B_FALSE);
45337c478bd9Sstevel@tonic-gate 		/*
45347c478bd9Sstevel@tonic-gate 		 * Note, IRE_INTERFACE can have the stq as NULL. For
45357c478bd9Sstevel@tonic-gate 		 * example, if the default multicast route is tied to
45367c478bd9Sstevel@tonic-gate 		 * the loopback address.
45377c478bd9Sstevel@tonic-gate 		 */
45387c478bd9Sstevel@tonic-gate 		if ((ire->ire_type & IRE_INTERFACE) &&
45397c478bd9Sstevel@tonic-gate 		    (ire->ire_stq != NULL)) {
45407c478bd9Sstevel@tonic-gate 			dst_ill = (ill_t *)ire->ire_stq->q_ptr;
45417c478bd9Sstevel@tonic-gate 			/*
45427c478bd9Sstevel@tonic-gate 			 * If there is a usable source address in the
45437c478bd9Sstevel@tonic-gate 			 * zone, then it's ok to return an
45447c478bd9Sstevel@tonic-gate 			 * IRE_INTERFACE
45457c478bd9Sstevel@tonic-gate 			 */
45467c478bd9Sstevel@tonic-gate 			if (ipif_usesrc_avail(dst_ill, zoneid)) {
45477c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: dst_ill %p match %d\n",
45487c478bd9Sstevel@tonic-gate 				    (void *)dst_ill,
45497c478bd9Sstevel@tonic-gate 				    (ire->ire_addr == (addr & mask))));
45507c478bd9Sstevel@tonic-gate 			} else {
45517c478bd9Sstevel@tonic-gate 				ip3dbg(("ire_match_args: src_ipif NULL"
45527c478bd9Sstevel@tonic-gate 				    " dst_ill %p\n", (void *)dst_ill));
45537c478bd9Sstevel@tonic-gate 				return (B_FALSE);
45547c478bd9Sstevel@tonic-gate 			}
45557c478bd9Sstevel@tonic-gate 		}
45567c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL && ire->ire_type != IRE_LOCAL &&
45577c478bd9Sstevel@tonic-gate 		    !(ire->ire_type & IRE_INTERFACE)) {
45587c478bd9Sstevel@tonic-gate 			ipif_t	*tipif;
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 			if ((match_flags & MATCH_IRE_DEFAULT) == 0) {
45617c478bd9Sstevel@tonic-gate 				return (B_FALSE);
45627c478bd9Sstevel@tonic-gate 			}
45637c478bd9Sstevel@tonic-gate 			mutex_enter(&ire->ire_ipif->ipif_ill->ill_lock);
45647c478bd9Sstevel@tonic-gate 			for (tipif = ire->ire_ipif->ipif_ill->ill_ipif;
45657c478bd9Sstevel@tonic-gate 			    tipif != NULL; tipif = tipif->ipif_next) {
45667c478bd9Sstevel@tonic-gate 				if (IPIF_CAN_LOOKUP(tipif) &&
45677c478bd9Sstevel@tonic-gate 				    (tipif->ipif_flags & IPIF_UP) &&
456845916cd2Sjpk 				    (tipif->ipif_zoneid == zoneid ||
456945916cd2Sjpk 				    tipif->ipif_zoneid == ALL_ZONES))
45707c478bd9Sstevel@tonic-gate 					break;
45717c478bd9Sstevel@tonic-gate 			}
45727c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_ipif->ipif_ill->ill_lock);
45737c478bd9Sstevel@tonic-gate 			if (tipif == NULL) {
45747c478bd9Sstevel@tonic-gate 				return (B_FALSE);
45757c478bd9Sstevel@tonic-gate 			}
45767c478bd9Sstevel@tonic-gate 		}
45777c478bd9Sstevel@tonic-gate 	}
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 	/*
45807c478bd9Sstevel@tonic-gate 	 * For IRE_CACHES, MATCH_IRE_ILL/ILL_GROUP really means that
45817c478bd9Sstevel@tonic-gate 	 * somebody wants to send out on a particular interface which
45827c478bd9Sstevel@tonic-gate 	 * is given by ire_stq and hence use ire_stq to derive the ill
45837c478bd9Sstevel@tonic-gate 	 * value. ire_ipif for IRE_CACHES is just the means of getting
45847c478bd9Sstevel@tonic-gate 	 * a source address i.e ire_src_addr = ire->ire_ipif->ipif_src_addr.
45857c478bd9Sstevel@tonic-gate 	 * ire_to_ill does the right thing for this.
45867c478bd9Sstevel@tonic-gate 	 */
45877c478bd9Sstevel@tonic-gate 	if (match_flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) {
45887c478bd9Sstevel@tonic-gate 		ire_ill = ire_to_ill(ire);
45897c478bd9Sstevel@tonic-gate 		if (ire_ill != NULL)
45907c478bd9Sstevel@tonic-gate 			ire_ill_group = ire_ill->ill_group;
45917c478bd9Sstevel@tonic-gate 		ipif_ill = ipif->ipif_ill;
45927c478bd9Sstevel@tonic-gate 		ipif_ill_group = ipif_ill->ill_group;
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 	if ((ire->ire_addr == (addr & mask)) &&
45967c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_GW)) ||
45977c478bd9Sstevel@tonic-gate 		(ire->ire_gateway_addr == gateway)) &&
45987c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_TYPE)) ||
45997c478bd9Sstevel@tonic-gate 		(ire->ire_type & type)) &&
46007c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_SRC)) ||
46017c478bd9Sstevel@tonic-gate 		(ire->ire_src_addr == ipif->ipif_src_addr)) &&
46027c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IPIF)) ||
46037c478bd9Sstevel@tonic-gate 		(ire->ire_ipif == ipif)) &&
46047c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_MARK_HIDDEN)) ||
46057c478bd9Sstevel@tonic-gate 		(ire->ire_type != IRE_CACHE ||
46067c478bd9Sstevel@tonic-gate 		ire->ire_marks & IRE_MARK_HIDDEN)) &&
460743d18f1cSpriyanka 	    ((!(match_flags & MATCH_IRE_MARK_PRIVATE_ADDR)) ||
460843d18f1cSpriyanka 		(ire->ire_type != IRE_CACHE ||
460943d18f1cSpriyanka 		ire->ire_marks & IRE_MARK_PRIVATE_ADDR)) &&
46107c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL)) ||
46117c478bd9Sstevel@tonic-gate 		(ire_ill == ipif_ill)) &&
46127c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_IHANDLE)) ||
46137c478bd9Sstevel@tonic-gate 		(ire->ire_ihandle == ihandle)) &&
46147c478bd9Sstevel@tonic-gate 	    ((!(match_flags & MATCH_IRE_ILL_GROUP)) ||
46157c478bd9Sstevel@tonic-gate 		(ire_ill == ipif_ill) ||
46167c478bd9Sstevel@tonic-gate 		(ire_ill_group != NULL &&
461745916cd2Sjpk 		ire_ill_group == ipif_ill_group)) &&
461845916cd2Sjpk 	    ((!(match_flags & MATCH_IRE_SECATTR)) ||
461945916cd2Sjpk 		(!is_system_labeled()) ||
462045916cd2Sjpk 		(tsol_ire_match_gwattr(ire, tsl) == 0))) {
46217c478bd9Sstevel@tonic-gate 		/* We found the matched IRE */
46227c478bd9Sstevel@tonic-gate 		return (B_TRUE);
46237c478bd9Sstevel@tonic-gate 	}
46247c478bd9Sstevel@tonic-gate 	return (B_FALSE);
46257c478bd9Sstevel@tonic-gate }
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate /*
46297c478bd9Sstevel@tonic-gate  * Lookup for a route in all the tables
46307c478bd9Sstevel@tonic-gate  */
46317c478bd9Sstevel@tonic-gate ire_t *
46327c478bd9Sstevel@tonic-gate ire_route_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
463345916cd2Sjpk     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
463445916cd2Sjpk     const ts_label_t *tsl, int flags)
46357c478bd9Sstevel@tonic-gate {
46367c478bd9Sstevel@tonic-gate 	ire_t *ire = NULL;
46377c478bd9Sstevel@tonic-gate 
46387c478bd9Sstevel@tonic-gate 	/*
46397c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
46407c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
46417c478bd9Sstevel@tonic-gate 	 */
46427c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
46437c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
46447c478bd9Sstevel@tonic-gate 		return (NULL);
46457c478bd9Sstevel@tonic-gate 
46467c478bd9Sstevel@tonic-gate 	/*
46477c478bd9Sstevel@tonic-gate 	 * might be asking for a cache lookup,
46487c478bd9Sstevel@tonic-gate 	 * This is not best way to lookup cache,
46497c478bd9Sstevel@tonic-gate 	 * user should call ire_cache_lookup directly.
46507c478bd9Sstevel@tonic-gate 	 *
46517c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_TYPE was set, first lookup in the cache table and then
46527c478bd9Sstevel@tonic-gate 	 * in the forwarding table, if the applicable type flags were set.
46537c478bd9Sstevel@tonic-gate 	 */
46547c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_CACHETABLE) != 0) {
46557c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(addr, gateway, type, ipif, zoneid,
465645916cd2Sjpk 		    tsl, flags);
46577c478bd9Sstevel@tonic-gate 		if (ire != NULL)
46587c478bd9Sstevel@tonic-gate 			return (ire);
46597c478bd9Sstevel@tonic-gate 	}
46607c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_TYPE) == 0 || (type & IRE_FORWARDTABLE) != 0) {
46617c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(addr, mask, gateway, type, ipif, pire,
466245916cd2Sjpk 		    zoneid, 0, tsl, flags);
46637c478bd9Sstevel@tonic-gate 	}
46647c478bd9Sstevel@tonic-gate 	return (ire);
46657c478bd9Sstevel@tonic-gate }
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate /*
46687c478bd9Sstevel@tonic-gate  * Lookup a route in forwarding table.
46697c478bd9Sstevel@tonic-gate  * specific lookup is indicated by passing the
46707c478bd9Sstevel@tonic-gate  * required parameters and indicating the
46717c478bd9Sstevel@tonic-gate  * match required in flag field.
46727c478bd9Sstevel@tonic-gate  *
46737c478bd9Sstevel@tonic-gate  * Looking for default route can be done in three ways
46747c478bd9Sstevel@tonic-gate  * 1) pass mask as 0 and set MATCH_IRE_MASK in flags field
46757c478bd9Sstevel@tonic-gate  *    along with other matches.
46767c478bd9Sstevel@tonic-gate  * 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags
46777c478bd9Sstevel@tonic-gate  *    field along with other matches.
46787c478bd9Sstevel@tonic-gate  * 3) if the destination and mask are passed as zeros.
46797c478bd9Sstevel@tonic-gate  *
46807c478bd9Sstevel@tonic-gate  * A request to return a default route if no route
46817c478bd9Sstevel@tonic-gate  * is found, can be specified by setting MATCH_IRE_DEFAULT
46827c478bd9Sstevel@tonic-gate  * in flags.
46837c478bd9Sstevel@tonic-gate  *
46847c478bd9Sstevel@tonic-gate  * It does not support recursion more than one level. It
46857c478bd9Sstevel@tonic-gate  * will do recursive lookup only when the lookup maps to
46867c478bd9Sstevel@tonic-gate  * a prefix or default route and MATCH_IRE_RECURSIVE flag is passed.
46877c478bd9Sstevel@tonic-gate  *
46887c478bd9Sstevel@tonic-gate  * If the routing table is setup to allow more than one level
46897c478bd9Sstevel@tonic-gate  * of recursion, the cleaning up cache table will not work resulting
46907c478bd9Sstevel@tonic-gate  * in invalid routing.
46917c478bd9Sstevel@tonic-gate  *
46927c478bd9Sstevel@tonic-gate  * Supports IP_BOUND_IF by following the ipif/ill when recursing.
46937c478bd9Sstevel@tonic-gate  *
46947c478bd9Sstevel@tonic-gate  * NOTE : When this function returns NULL, pire has already been released.
46957c478bd9Sstevel@tonic-gate  *	  pire is valid only when this function successfully returns an
46967c478bd9Sstevel@tonic-gate  *	  ire.
46977c478bd9Sstevel@tonic-gate  */
46987c478bd9Sstevel@tonic-gate ire_t *
46997c478bd9Sstevel@tonic-gate ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
470045916cd2Sjpk     int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
470145916cd2Sjpk     uint32_t ihandle, const ts_label_t *tsl, int flags)
47027c478bd9Sstevel@tonic-gate {
47037c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
47047c478bd9Sstevel@tonic-gate 	ire_t *ire = NULL;
47057c478bd9Sstevel@tonic-gate 	int i;
47067c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
47097c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & MATCH_IRE_WQ));
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate 	/*
47127c478bd9Sstevel@tonic-gate 	 * When we return NULL from this function, we should make
47137c478bd9Sstevel@tonic-gate 	 * sure that *pire is NULL so that the callers will not
47147c478bd9Sstevel@tonic-gate 	 * wrongly REFRELE the pire.
47157c478bd9Sstevel@tonic-gate 	 */
47167c478bd9Sstevel@tonic-gate 	if (pire != NULL)
47177c478bd9Sstevel@tonic-gate 		*pire = NULL;
47187c478bd9Sstevel@tonic-gate 	/*
47197c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
47207c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
47217c478bd9Sstevel@tonic-gate 	 */
47227c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
47237c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
47247c478bd9Sstevel@tonic-gate 		return (NULL);
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 	/*
47277c478bd9Sstevel@tonic-gate 	 * If the mask is known, the lookup
47287c478bd9Sstevel@tonic-gate 	 * is simple, if the mask is not known
47297c478bd9Sstevel@tonic-gate 	 * we need to search.
47307c478bd9Sstevel@tonic-gate 	 */
47317c478bd9Sstevel@tonic-gate 	if (flags & MATCH_IRE_MASK) {
47327c478bd9Sstevel@tonic-gate 		uint_t masklen;
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 		masklen = ip_mask_to_plen(mask);
47357c478bd9Sstevel@tonic-gate 		if (ip_forwarding_table[masklen] == NULL)
47367c478bd9Sstevel@tonic-gate 			return (NULL);
47377c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_forwarding_table[masklen][
47387c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH(addr & mask, ip_ftable_hash_size)]);
47397c478bd9Sstevel@tonic-gate 		rw_enter(&irb_ptr->irb_lock, RW_READER);
47407c478bd9Sstevel@tonic-gate 		for (ire = irb_ptr->irb_ire; ire != NULL;
47417c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
47427c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
47437c478bd9Sstevel@tonic-gate 				continue;
47447c478bd9Sstevel@tonic-gate 			if (ire_match_args(ire, addr, mask, gateway, type, ipif,
474545916cd2Sjpk 			    zoneid, ihandle, tsl, flags))
47467c478bd9Sstevel@tonic-gate 				goto found_ire;
47477c478bd9Sstevel@tonic-gate 		}
47487c478bd9Sstevel@tonic-gate 		rw_exit(&irb_ptr->irb_lock);
47497c478bd9Sstevel@tonic-gate 	} else {
47507c478bd9Sstevel@tonic-gate 		/*
47517c478bd9Sstevel@tonic-gate 		 * In this case we don't know the mask, we need to
47527c478bd9Sstevel@tonic-gate 		 * search the table assuming different mask sizes.
47537c478bd9Sstevel@tonic-gate 		 * we start with 32 bit mask, we don't allow default here.
47547c478bd9Sstevel@tonic-gate 		 */
47557c478bd9Sstevel@tonic-gate 		for (i = (IP_MASK_TABLE_SIZE - 1); i > 0; i--) {
47567c478bd9Sstevel@tonic-gate 			ipaddr_t tmpmask;
47577c478bd9Sstevel@tonic-gate 
47587c478bd9Sstevel@tonic-gate 			if ((ip_forwarding_table[i]) == NULL)
47597c478bd9Sstevel@tonic-gate 				continue;
47607c478bd9Sstevel@tonic-gate 			tmpmask = ip_plen_to_mask(i);
47617c478bd9Sstevel@tonic-gate 			irb_ptr = &ip_forwarding_table[i][
47627c478bd9Sstevel@tonic-gate 			    IRE_ADDR_HASH(addr & tmpmask,
47637c478bd9Sstevel@tonic-gate 			    ip_ftable_hash_size)];
47647c478bd9Sstevel@tonic-gate 			rw_enter(&irb_ptr->irb_lock, RW_READER);
47657c478bd9Sstevel@tonic-gate 			for (ire = irb_ptr->irb_ire; ire != NULL;
47667c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
47677c478bd9Sstevel@tonic-gate 				if (ire->ire_marks & IRE_MARK_CONDEMNED)
47687c478bd9Sstevel@tonic-gate 					continue;
47697c478bd9Sstevel@tonic-gate 				if (ire_match_args(ire, addr, ire->ire_mask,
47707c478bd9Sstevel@tonic-gate 				    gateway, type, ipif, zoneid, ihandle,
477145916cd2Sjpk 				    tsl, flags))
47727c478bd9Sstevel@tonic-gate 					goto found_ire;
47737c478bd9Sstevel@tonic-gate 			}
47747c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
47757c478bd9Sstevel@tonic-gate 		}
47767c478bd9Sstevel@tonic-gate 	}
47777c478bd9Sstevel@tonic-gate 	/*
47787c478bd9Sstevel@tonic-gate 	 * We come here if no route has yet been found.
47797c478bd9Sstevel@tonic-gate 	 *
47807c478bd9Sstevel@tonic-gate 	 * Handle the case where default route is
47817c478bd9Sstevel@tonic-gate 	 * requested by specifying type as one of the possible
47827c478bd9Sstevel@tonic-gate 	 * types for that can have a zero mask (IRE_DEFAULT and IRE_INTERFACE).
47837c478bd9Sstevel@tonic-gate 	 *
47847c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_MASK is specified, then the appropriate default route
47857c478bd9Sstevel@tonic-gate 	 * would have been found above if it exists so it isn't looked up here.
47867c478bd9Sstevel@tonic-gate 	 * If MATCH_IRE_DEFAULT was also specified, then a default route will be
47877c478bd9Sstevel@tonic-gate 	 * searched for later.
47887c478bd9Sstevel@tonic-gate 	 */
47897c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_TYPE | MATCH_IRE_MASK)) == MATCH_IRE_TYPE &&
47907c478bd9Sstevel@tonic-gate 	    (type & (IRE_DEFAULT | IRE_INTERFACE))) {
47917c478bd9Sstevel@tonic-gate 		if ((ip_forwarding_table[0])) {
47927c478bd9Sstevel@tonic-gate 			/* addr & mask is zero for defaults */
47937c478bd9Sstevel@tonic-gate 			irb_ptr = &ip_forwarding_table[0][
47947c478bd9Sstevel@tonic-gate 			    IRE_ADDR_HASH(0, ip_ftable_hash_size)];
47957c478bd9Sstevel@tonic-gate 			rw_enter(&irb_ptr->irb_lock, RW_READER);
47967c478bd9Sstevel@tonic-gate 			for (ire = irb_ptr->irb_ire; ire != NULL;
47977c478bd9Sstevel@tonic-gate 			    ire = ire->ire_next) {
47987c478bd9Sstevel@tonic-gate 				if (ire->ire_marks & IRE_MARK_CONDEMNED)
47997c478bd9Sstevel@tonic-gate 					continue;
48007c478bd9Sstevel@tonic-gate 				if (ire_match_args(ire, addr, (ipaddr_t)0,
48017c478bd9Sstevel@tonic-gate 				    gateway, type, ipif, zoneid, ihandle,
480245916cd2Sjpk 				    tsl, flags))
48037c478bd9Sstevel@tonic-gate 					goto found_ire;
48047c478bd9Sstevel@tonic-gate 			}
48057c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
48067c478bd9Sstevel@tonic-gate 		}
48077c478bd9Sstevel@tonic-gate 	}
48087c478bd9Sstevel@tonic-gate 	/*
48097c478bd9Sstevel@tonic-gate 	 * we come here only if no route is found.
48107c478bd9Sstevel@tonic-gate 	 * see if the default route can be used which is allowed
48117c478bd9Sstevel@tonic-gate 	 * only if the default matching criteria is specified.
48127c478bd9Sstevel@tonic-gate 	 * The ip_ire_default_count tracks the number of IRE_DEFAULT
48137c478bd9Sstevel@tonic-gate 	 * entries. However, the ip_forwarding_table[0] also contains
48147c478bd9Sstevel@tonic-gate 	 * interface routes thus the count can be zero.
48157c478bd9Sstevel@tonic-gate 	 */
48167c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
48177c478bd9Sstevel@tonic-gate 	    MATCH_IRE_DEFAULT) {
48187c478bd9Sstevel@tonic-gate 		ire_t	*ire_origin;
48197c478bd9Sstevel@tonic-gate 		uint_t  g_index;
48207c478bd9Sstevel@tonic-gate 		uint_t	index;
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate 		if (ip_forwarding_table[0] == NULL)
48237c478bd9Sstevel@tonic-gate 			return (NULL);
48247c478bd9Sstevel@tonic-gate 		irb_ptr = &(ip_forwarding_table[0])[0];
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 		/*
48277c478bd9Sstevel@tonic-gate 		 * Keep a tab on the bucket while looking the IRE_DEFAULT
48287c478bd9Sstevel@tonic-gate 		 * entries. We need to keep track of a particular IRE
48297c478bd9Sstevel@tonic-gate 		 * (ire_origin) so this ensures that it will not be unlinked
48307c478bd9Sstevel@tonic-gate 		 * from the hash list during the recursive lookup below.
48317c478bd9Sstevel@tonic-gate 		 */
48327c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb_ptr);
48337c478bd9Sstevel@tonic-gate 		ire = irb_ptr->irb_ire;
48347c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
48357c478bd9Sstevel@tonic-gate 			IRB_REFRELE(irb_ptr);
48367c478bd9Sstevel@tonic-gate 			return (NULL);
48377c478bd9Sstevel@tonic-gate 		}
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate 		/*
48407c478bd9Sstevel@tonic-gate 		 * Get the index first, since it can be changed by other
48417c478bd9Sstevel@tonic-gate 		 * threads. Then get to the right default route skipping
48427c478bd9Sstevel@tonic-gate 		 * default interface routes if any. As we hold a reference on
48437c478bd9Sstevel@tonic-gate 		 * the IRE bucket, ip_ire_default_count can only increase so we
48447c478bd9Sstevel@tonic-gate 		 * can't reach the end of the hash list unexpectedly.
48457c478bd9Sstevel@tonic-gate 		 */
48467c478bd9Sstevel@tonic-gate 		if (ip_ire_default_count != 0) {
48477c478bd9Sstevel@tonic-gate 			g_index = ip_ire_default_index++;
48487c478bd9Sstevel@tonic-gate 			index = g_index % ip_ire_default_count;
48497c478bd9Sstevel@tonic-gate 			while (index != 0) {
48507c478bd9Sstevel@tonic-gate 				if (!(ire->ire_type & IRE_INTERFACE))
48517c478bd9Sstevel@tonic-gate 					index--;
48527c478bd9Sstevel@tonic-gate 				ire = ire->ire_next;
48537c478bd9Sstevel@tonic-gate 			}
48547c478bd9Sstevel@tonic-gate 			ASSERT(ire != NULL);
48557c478bd9Sstevel@tonic-gate 		} else {
48567c478bd9Sstevel@tonic-gate 			/*
48577c478bd9Sstevel@tonic-gate 			 * No default routes, so we only have default interface
48587c478bd9Sstevel@tonic-gate 			 * routes: don't enter the first loop.
48597c478bd9Sstevel@tonic-gate 			 */
48607c478bd9Sstevel@tonic-gate 			ire = NULL;
48617c478bd9Sstevel@tonic-gate 		}
48627c478bd9Sstevel@tonic-gate 
48637c478bd9Sstevel@tonic-gate 		/*
48647c478bd9Sstevel@tonic-gate 		 * Round-robin the default routers list looking for a route that
48657c478bd9Sstevel@tonic-gate 		 * matches the passed in parameters. If we can't find a default
48667c478bd9Sstevel@tonic-gate 		 * route (IRE_DEFAULT), look for interface default routes.
48677c478bd9Sstevel@tonic-gate 		 * We start with the ire we found above and we walk the hash
48687c478bd9Sstevel@tonic-gate 		 * list until we're back where we started, see
48697c478bd9Sstevel@tonic-gate 		 * ire_get_next_default_ire(). It doesn't matter if default
48707c478bd9Sstevel@tonic-gate 		 * routes are added or deleted by other threads - we know this
48717c478bd9Sstevel@tonic-gate 		 * ire will stay in the list because we hold a reference on the
48727c478bd9Sstevel@tonic-gate 		 * ire bucket.
48737c478bd9Sstevel@tonic-gate 		 * NB: if we only have interface default routes, ire is NULL so
48747c478bd9Sstevel@tonic-gate 		 * we don't even enter this loop (see above).
48757c478bd9Sstevel@tonic-gate 		 */
48767c478bd9Sstevel@tonic-gate 		ire_origin = ire;
48777c478bd9Sstevel@tonic-gate 		for (; ire != NULL;
48787c478bd9Sstevel@tonic-gate 		    ire = ire_get_next_default_ire(ire, ire_origin)) {
48797c478bd9Sstevel@tonic-gate 
48807c478bd9Sstevel@tonic-gate 			if (ire_match_args(ire, addr, (ipaddr_t)0,
488145916cd2Sjpk 			    gateway, type, ipif, zoneid, ihandle, tsl, flags)) {
48827c478bd9Sstevel@tonic-gate 				int match_flags = 0;
48837c478bd9Sstevel@tonic-gate 				ire_t *rire;
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate 				/*
48867c478bd9Sstevel@tonic-gate 				 * The potentially expensive call to
48877c478bd9Sstevel@tonic-gate 				 * ire_route_lookup() is avoided when we have
48887c478bd9Sstevel@tonic-gate 				 * only one default route.
48897c478bd9Sstevel@tonic-gate 				 */
48907c478bd9Sstevel@tonic-gate 				if (ip_ire_default_count == 1 ||
48917c478bd9Sstevel@tonic-gate 				    zoneid == ALL_ZONES) {
48927c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
48937c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb_ptr);
48947c478bd9Sstevel@tonic-gate 					goto found_ire_held;
48957c478bd9Sstevel@tonic-gate 				}
48967c478bd9Sstevel@tonic-gate 				/*
48977c478bd9Sstevel@tonic-gate 				 * When we're in a local zone, we're only
48987c478bd9Sstevel@tonic-gate 				 * interested in default routers that are
48997c478bd9Sstevel@tonic-gate 				 * reachable through ipifs within our zone.
49007c478bd9Sstevel@tonic-gate 				 */
49017c478bd9Sstevel@tonic-gate 				if (ire->ire_ipif != NULL) {
49027c478bd9Sstevel@tonic-gate 					match_flags |= MATCH_IRE_ILL_GROUP;
49037c478bd9Sstevel@tonic-gate 				}
49047c478bd9Sstevel@tonic-gate 				rire = ire_route_lookup(ire->ire_gateway_addr,
490545916cd2Sjpk 				    0, 0, 0, ire->ire_ipif, NULL, zoneid, tsl,
49067c478bd9Sstevel@tonic-gate 				    match_flags);
49077c478bd9Sstevel@tonic-gate 				if (rire != NULL) {
49087c478bd9Sstevel@tonic-gate 					ire_refrele(rire);
49097c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(ire);
49107c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb_ptr);
49117c478bd9Sstevel@tonic-gate 					goto found_ire_held;
49127c478bd9Sstevel@tonic-gate 				}
49137c478bd9Sstevel@tonic-gate 			}
49147c478bd9Sstevel@tonic-gate 		}
49157c478bd9Sstevel@tonic-gate 		/*
49167c478bd9Sstevel@tonic-gate 		 * Either there are no default routes or we could not
49177c478bd9Sstevel@tonic-gate 		 * find a default route. Look for a interface default
49187c478bd9Sstevel@tonic-gate 		 * route matching the args passed in. No round robin
49197c478bd9Sstevel@tonic-gate 		 * here. Just pick the right one.
49207c478bd9Sstevel@tonic-gate 		 */
49217c478bd9Sstevel@tonic-gate 		for (ire = irb_ptr->irb_ire; ire != NULL;
49227c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
49237c478bd9Sstevel@tonic-gate 
49247c478bd9Sstevel@tonic-gate 			if (!(ire->ire_type & IRE_INTERFACE))
49257c478bd9Sstevel@tonic-gate 				continue;
49267c478bd9Sstevel@tonic-gate 
49277c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
49287c478bd9Sstevel@tonic-gate 				continue;
49297c478bd9Sstevel@tonic-gate 
49307c478bd9Sstevel@tonic-gate 			if (ire_match_args(ire, addr, (ipaddr_t)0,
493145916cd2Sjpk 			    gateway, type, ipif, zoneid, ihandle, tsl,
493245916cd2Sjpk 			    flags)) {
49337c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
49347c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb_ptr);
49357c478bd9Sstevel@tonic-gate 				goto found_ire_held;
49367c478bd9Sstevel@tonic-gate 			}
49377c478bd9Sstevel@tonic-gate 		}
49387c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb_ptr);
49397c478bd9Sstevel@tonic-gate 	}
49407c478bd9Sstevel@tonic-gate 	ASSERT(ire == NULL);
49417c478bd9Sstevel@tonic-gate 	return (NULL);
49427c478bd9Sstevel@tonic-gate found_ire:
49437c478bd9Sstevel@tonic-gate 	ASSERT((ire->ire_marks & IRE_MARK_CONDEMNED) == 0);
49447c478bd9Sstevel@tonic-gate 	IRE_REFHOLD(ire);
49457c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate found_ire_held:
49487c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
49497c478bd9Sstevel@tonic-gate 	if ((flags & MATCH_IRE_RJ_BHOLE) &&
49507c478bd9Sstevel@tonic-gate 	    (ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
49517c478bd9Sstevel@tonic-gate 		return (ire);
49527c478bd9Sstevel@tonic-gate 	}
49537c478bd9Sstevel@tonic-gate 	/*
49547c478bd9Sstevel@tonic-gate 	 * At this point, IRE that was found must be an IRE_FORWARDTABLE
49557c478bd9Sstevel@tonic-gate 	 * type.  If this is a recursive lookup and an IRE_INTERFACE type was
49567c478bd9Sstevel@tonic-gate 	 * found, return that.  If it was some other IRE_FORWARDTABLE type of
49577c478bd9Sstevel@tonic-gate 	 * IRE (one of the prefix types), then it is necessary to fill in the
49587c478bd9Sstevel@tonic-gate 	 * parent IRE pointed to by pire, and then lookup the gateway address of
49597c478bd9Sstevel@tonic-gate 	 * the parent.  For backwards compatiblity, if this lookup returns an
49607c478bd9Sstevel@tonic-gate 	 * IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
49617c478bd9Sstevel@tonic-gate 	 * of lookup is done.
49627c478bd9Sstevel@tonic-gate 	 */
49637c478bd9Sstevel@tonic-gate 	if (flags & MATCH_IRE_RECURSIVE) {
496445916cd2Sjpk 		const ipif_t *gw_ipif;
49657c478bd9Sstevel@tonic-gate 		int match_flags = MATCH_IRE_DSTONLY;
49667c478bd9Sstevel@tonic-gate 		ire_t *save_ire;
49677c478bd9Sstevel@tonic-gate 
49687c478bd9Sstevel@tonic-gate 		if (ire->ire_type & IRE_INTERFACE)
49697c478bd9Sstevel@tonic-gate 			return (ire);
49707c478bd9Sstevel@tonic-gate 		if (pire != NULL)
49717c478bd9Sstevel@tonic-gate 			*pire = ire;
49727c478bd9Sstevel@tonic-gate 		/*
49737c478bd9Sstevel@tonic-gate 		 * If we can't find an IRE_INTERFACE or the caller has not
49747c478bd9Sstevel@tonic-gate 		 * asked for pire, we need to REFRELE the save_ire.
49757c478bd9Sstevel@tonic-gate 		 */
49767c478bd9Sstevel@tonic-gate 		save_ire = ire;
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate 		/*
49797c478bd9Sstevel@tonic-gate 		 * Currently MATCH_IRE_ILL is never used with
49807c478bd9Sstevel@tonic-gate 		 * (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
49817c478bd9Sstevel@tonic-gate 		 * sending out packets as MATCH_IRE_ILL is used only
49827c478bd9Sstevel@tonic-gate 		 * for communicating with on-link hosts. We can't assert
49837c478bd9Sstevel@tonic-gate 		 * that here as RTM_GET calls this function with
49847c478bd9Sstevel@tonic-gate 		 * MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
49857c478bd9Sstevel@tonic-gate 		 * We have already used the MATCH_IRE_ILL in determining
49867c478bd9Sstevel@tonic-gate 		 * the right prefix route at this point. To match the
49877c478bd9Sstevel@tonic-gate 		 * behavior of how we locate routes while sending out
49887c478bd9Sstevel@tonic-gate 		 * packets, we don't want to use MATCH_IRE_ILL below
49897c478bd9Sstevel@tonic-gate 		 * while locating the interface route.
49907c478bd9Sstevel@tonic-gate 		 */
49917c478bd9Sstevel@tonic-gate 		if (ire->ire_ipif != NULL)
49927c478bd9Sstevel@tonic-gate 			match_flags |= MATCH_IRE_ILL_GROUP;
49937c478bd9Sstevel@tonic-gate 
49947c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
499545916cd2Sjpk 		    ire->ire_ipif, NULL, zoneid, tsl, match_flags);
49967c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
49977c478bd9Sstevel@tonic-gate 			/*
49987c478bd9Sstevel@tonic-gate 			 * Do not release the parent ire if MATCH_IRE_PARENT
49997c478bd9Sstevel@tonic-gate 			 * is set. Also return it via ire.
50007c478bd9Sstevel@tonic-gate 			 */
50017c478bd9Sstevel@tonic-gate 			if (flags & MATCH_IRE_PARENT) {
50027c478bd9Sstevel@tonic-gate 				if (pire != NULL) {
50037c478bd9Sstevel@tonic-gate 					/*
50047c478bd9Sstevel@tonic-gate 					 * Need an extra REFHOLD, if the parent
50057c478bd9Sstevel@tonic-gate 					 * ire is returned via both ire and
50067c478bd9Sstevel@tonic-gate 					 * pire.
50077c478bd9Sstevel@tonic-gate 					 */
50087c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(save_ire);
50097c478bd9Sstevel@tonic-gate 				}
50107c478bd9Sstevel@tonic-gate 				ire = save_ire;
50117c478bd9Sstevel@tonic-gate 			} else {
50127c478bd9Sstevel@tonic-gate 				ire_refrele(save_ire);
50137c478bd9Sstevel@tonic-gate 				if (pire != NULL)
50147c478bd9Sstevel@tonic-gate 					*pire = NULL;
50157c478bd9Sstevel@tonic-gate 			}
50167c478bd9Sstevel@tonic-gate 			return (ire);
50177c478bd9Sstevel@tonic-gate 		}
50187c478bd9Sstevel@tonic-gate 		if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
50197c478bd9Sstevel@tonic-gate 			/*
50207c478bd9Sstevel@tonic-gate 			 * If the caller did not ask for pire, release
50217c478bd9Sstevel@tonic-gate 			 * it now.
50227c478bd9Sstevel@tonic-gate 			 */
50237c478bd9Sstevel@tonic-gate 			if (pire == NULL) {
50247c478bd9Sstevel@tonic-gate 				ire_refrele(save_ire);
50257c478bd9Sstevel@tonic-gate 			}
50267c478bd9Sstevel@tonic-gate 			return (ire);
50277c478bd9Sstevel@tonic-gate 		}
50287c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_TYPE;
50297c478bd9Sstevel@tonic-gate 		gw_addr = ire->ire_gateway_addr;
50307c478bd9Sstevel@tonic-gate 		gw_ipif = ire->ire_ipif;
50317c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
50327c478bd9Sstevel@tonic-gate 		ire = ire_route_lookup(gw_addr, 0, 0,
50337c478bd9Sstevel@tonic-gate 		    (IRE_CACHETABLE | IRE_INTERFACE), gw_ipif, NULL, zoneid,
503445916cd2Sjpk 		    tsl, match_flags);
50357c478bd9Sstevel@tonic-gate 		if (ire == NULL) {
50367c478bd9Sstevel@tonic-gate 			/*
50377c478bd9Sstevel@tonic-gate 			 * Do not release the parent ire if MATCH_IRE_PARENT
50387c478bd9Sstevel@tonic-gate 			 * is set. Also return it via ire.
50397c478bd9Sstevel@tonic-gate 			 */
50407c478bd9Sstevel@tonic-gate 			if (flags & MATCH_IRE_PARENT) {
50417c478bd9Sstevel@tonic-gate 				if (pire != NULL) {
50427c478bd9Sstevel@tonic-gate 					/*
50437c478bd9Sstevel@tonic-gate 					 * Need an extra REFHOLD, if the
50447c478bd9Sstevel@tonic-gate 					 * parent ire is returned via both
50457c478bd9Sstevel@tonic-gate 					 * ire and pire.
50467c478bd9Sstevel@tonic-gate 					 */
50477c478bd9Sstevel@tonic-gate 					IRE_REFHOLD(save_ire);
50487c478bd9Sstevel@tonic-gate 				}
50497c478bd9Sstevel@tonic-gate 				ire = save_ire;
50507c478bd9Sstevel@tonic-gate 			} else {
50517c478bd9Sstevel@tonic-gate 				ire_refrele(save_ire);
50527c478bd9Sstevel@tonic-gate 				if (pire != NULL)
50537c478bd9Sstevel@tonic-gate 					*pire = NULL;
50547c478bd9Sstevel@tonic-gate 			}
50557c478bd9Sstevel@tonic-gate 			return (ire);
50567c478bd9Sstevel@tonic-gate 		} else if (pire == NULL) {
50577c478bd9Sstevel@tonic-gate 			/*
50587c478bd9Sstevel@tonic-gate 			 * If the caller did not ask for pire, release
50597c478bd9Sstevel@tonic-gate 			 * it now.
50607c478bd9Sstevel@tonic-gate 			 */
50617c478bd9Sstevel@tonic-gate 			ire_refrele(save_ire);
50627c478bd9Sstevel@tonic-gate 		}
50637c478bd9Sstevel@tonic-gate 		return (ire);
50647c478bd9Sstevel@tonic-gate 	}
50657c478bd9Sstevel@tonic-gate 	ASSERT(pire == NULL || *pire == NULL);
50667c478bd9Sstevel@tonic-gate 	return (ire);
50677c478bd9Sstevel@tonic-gate }
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate /*
507045916cd2Sjpk  * Delete the IRE cache for the gateway and all IRE caches whose
507145916cd2Sjpk  * ire_gateway_addr points to this gateway, and allow them to
507245916cd2Sjpk  * be created on demand by ip_newroute.
507345916cd2Sjpk  */
507445916cd2Sjpk void
507545916cd2Sjpk ire_clookup_delete_cache_gw(ipaddr_t addr, zoneid_t zoneid)
507645916cd2Sjpk {
507745916cd2Sjpk 	irb_t *irb;
507845916cd2Sjpk 	ire_t *ire;
507945916cd2Sjpk 
508045916cd2Sjpk 	irb = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
508145916cd2Sjpk 	IRB_REFHOLD(irb);
508245916cd2Sjpk 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
508345916cd2Sjpk 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
508445916cd2Sjpk 			continue;
508545916cd2Sjpk 
508645916cd2Sjpk 		ASSERT(ire->ire_mask == IP_HOST_MASK);
508745916cd2Sjpk 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
508845916cd2Sjpk 		if (ire_match_args(ire, addr, ire->ire_mask, 0, IRE_CACHE,
508945916cd2Sjpk 		    NULL, zoneid, 0, NULL, MATCH_IRE_TYPE)) {
509045916cd2Sjpk 			ire_delete(ire);
509145916cd2Sjpk 		}
509245916cd2Sjpk 	}
509345916cd2Sjpk 	IRB_REFRELE(irb);
509445916cd2Sjpk 
509545916cd2Sjpk 	ire_walk_v4(ire_delete_cache_gw, &addr, zoneid);
509645916cd2Sjpk }
509745916cd2Sjpk 
509845916cd2Sjpk /*
50997c478bd9Sstevel@tonic-gate  * Looks up cache table for a route.
51007c478bd9Sstevel@tonic-gate  * specific lookup can be indicated by
51017c478bd9Sstevel@tonic-gate  * passing the MATCH_* flags and the
51027c478bd9Sstevel@tonic-gate  * necessary parameters.
51037c478bd9Sstevel@tonic-gate  */
51047c478bd9Sstevel@tonic-gate ire_t *
510545916cd2Sjpk ire_ctable_lookup(ipaddr_t addr, ipaddr_t gateway, int type, const ipif_t *ipif,
510645916cd2Sjpk     zoneid_t zoneid, const ts_label_t *tsl, int flags)
51077c478bd9Sstevel@tonic-gate {
51087c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
51097c478bd9Sstevel@tonic-gate 	ire_t *ire;
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 	/*
51127c478bd9Sstevel@tonic-gate 	 * ire_match_args() will dereference ipif MATCH_IRE_SRC or
51137c478bd9Sstevel@tonic-gate 	 * MATCH_IRE_ILL is set.
51147c478bd9Sstevel@tonic-gate 	 */
51157c478bd9Sstevel@tonic-gate 	if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
51167c478bd9Sstevel@tonic-gate 	    (ipif == NULL))
51177c478bd9Sstevel@tonic-gate 		return (NULL);
51187c478bd9Sstevel@tonic-gate 
51197c478bd9Sstevel@tonic-gate 	irb_ptr = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
51207c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
51217c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
51227c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
51237c478bd9Sstevel@tonic-gate 			continue;
51247c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_mask == IP_HOST_MASK);
51257c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
51267c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, addr, ire->ire_mask, gateway, type,
512745916cd2Sjpk 		    ipif, zoneid, 0, tsl, flags)) {
51287c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
51297c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
51307c478bd9Sstevel@tonic-gate 			return (ire);
51317c478bd9Sstevel@tonic-gate 		}
51327c478bd9Sstevel@tonic-gate 	}
51337c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
51347c478bd9Sstevel@tonic-gate 	return (NULL);
51357c478bd9Sstevel@tonic-gate }
51367c478bd9Sstevel@tonic-gate 
51377c478bd9Sstevel@tonic-gate /*
51387c478bd9Sstevel@tonic-gate  * Lookup cache. Don't return IRE_MARK_HIDDEN entries. Callers
51397c478bd9Sstevel@tonic-gate  * should use ire_ctable_lookup with MATCH_IRE_MARK_HIDDEN to get
51407c478bd9Sstevel@tonic-gate  * to the hidden ones.
51417c478bd9Sstevel@tonic-gate  */
51427c478bd9Sstevel@tonic-gate ire_t *
514345916cd2Sjpk ire_cache_lookup(ipaddr_t addr, zoneid_t zoneid, const ts_label_t *tsl)
51447c478bd9Sstevel@tonic-gate {
51457c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
51467c478bd9Sstevel@tonic-gate 	ire_t *ire;
51477c478bd9Sstevel@tonic-gate 
51487c478bd9Sstevel@tonic-gate 	irb_ptr = &ip_cache_table[IRE_ADDR_HASH(addr, ip_cache_table_size)];
51497c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
51507c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
515143d18f1cSpriyanka 		if (ire->ire_marks & (IRE_MARK_CONDEMNED |
515243d18f1cSpriyanka 		    IRE_MARK_HIDDEN | IRE_MARK_PRIVATE_ADDR)) {
51537c478bd9Sstevel@tonic-gate 			continue;
515443d18f1cSpriyanka 		}
51557c478bd9Sstevel@tonic-gate 		if (ire->ire_addr == addr) {
515645916cd2Sjpk 			/*
515745916cd2Sjpk 			 * Finally, check if the security policy has any
515845916cd2Sjpk 			 * restriction on using this route for the specified
515945916cd2Sjpk 			 * message.
516045916cd2Sjpk 			 */
516145916cd2Sjpk 			if (tsl != NULL &&
516245916cd2Sjpk 			    ire->ire_gw_secattr != NULL &&
516345916cd2Sjpk 			    tsol_ire_match_gwattr(ire, tsl) != 0) {
516445916cd2Sjpk 				continue;
516545916cd2Sjpk 			}
516645916cd2Sjpk 
51677c478bd9Sstevel@tonic-gate 			if (zoneid == ALL_ZONES || ire->ire_zoneid == zoneid ||
516845916cd2Sjpk 			    ire->ire_zoneid == ALL_ZONES ||
51697c478bd9Sstevel@tonic-gate 			    ire->ire_type == IRE_LOCAL) {
51707c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
51717c478bd9Sstevel@tonic-gate 				rw_exit(&irb_ptr->irb_lock);
51727c478bd9Sstevel@tonic-gate 				return (ire);
51737c478bd9Sstevel@tonic-gate 			}
51747c478bd9Sstevel@tonic-gate 		}
51757c478bd9Sstevel@tonic-gate 	}
51767c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
51777c478bd9Sstevel@tonic-gate 	return (NULL);
51787c478bd9Sstevel@tonic-gate }
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate /*
51817c478bd9Sstevel@tonic-gate  * Locate the interface ire that is tied to the cache ire 'cire' via
51827c478bd9Sstevel@tonic-gate  * cire->ire_ihandle.
51837c478bd9Sstevel@tonic-gate  *
51847c478bd9Sstevel@tonic-gate  * We are trying to create the cache ire for an offlink destn based
51857c478bd9Sstevel@tonic-gate  * on the cache ire of the gateway in 'cire'. 'pire' is the prefix ire
51867c478bd9Sstevel@tonic-gate  * as found by ip_newroute(). We are called from ip_newroute() in
51877c478bd9Sstevel@tonic-gate  * the IRE_CACHE case.
51887c478bd9Sstevel@tonic-gate  */
51897c478bd9Sstevel@tonic-gate ire_t *
51907c478bd9Sstevel@tonic-gate ire_ihandle_lookup_offlink(ire_t *cire, ire_t *pire)
51917c478bd9Sstevel@tonic-gate {
51927c478bd9Sstevel@tonic-gate 	ire_t	*ire;
51937c478bd9Sstevel@tonic-gate 	int	match_flags;
51947c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
51957c478bd9Sstevel@tonic-gate 	ipif_t	*gw_ipif;
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate 	ASSERT(cire != NULL && pire != NULL);
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 	/*
52007c478bd9Sstevel@tonic-gate 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
52017c478bd9Sstevel@tonic-gate 	 * because the ihandle refers to an ipif which can be in only one zone.
52027c478bd9Sstevel@tonic-gate 	 */
52037c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
52047c478bd9Sstevel@tonic-gate 	/*
52057c478bd9Sstevel@tonic-gate 	 * ip_newroute calls ire_ftable_lookup with MATCH_IRE_ILL only
52067c478bd9Sstevel@tonic-gate 	 * for on-link hosts. We should never be here for onlink.
52077c478bd9Sstevel@tonic-gate 	 * Thus, use MATCH_IRE_ILL_GROUP.
52087c478bd9Sstevel@tonic-gate 	 */
52097c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
52107c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
52117c478bd9Sstevel@tonic-gate 	/*
52127c478bd9Sstevel@tonic-gate 	 * We know that the mask of the interface ire equals cire->ire_cmask.
52137c478bd9Sstevel@tonic-gate 	 * (When ip_newroute() created 'cire' for the gateway it set its
52147c478bd9Sstevel@tonic-gate 	 * cmask from the interface ire's mask)
52157c478bd9Sstevel@tonic-gate 	 */
52167c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
52177c478bd9Sstevel@tonic-gate 	    IRE_INTERFACE, pire->ire_ipif, NULL, ALL_ZONES, cire->ire_ihandle,
521845916cd2Sjpk 	    NULL, match_flags);
52197c478bd9Sstevel@tonic-gate 	if (ire != NULL)
52207c478bd9Sstevel@tonic-gate 		return (ire);
52217c478bd9Sstevel@tonic-gate 	/*
52227c478bd9Sstevel@tonic-gate 	 * If we didn't find an interface ire above, we can't declare failure.
52237c478bd9Sstevel@tonic-gate 	 * For backwards compatibility, we need to support prefix routes
52247c478bd9Sstevel@tonic-gate 	 * pointing to next hop gateways that are not on-link.
52257c478bd9Sstevel@tonic-gate 	 *
52267c478bd9Sstevel@tonic-gate 	 * Assume we are trying to ping some offlink destn, and we have the
52277c478bd9Sstevel@tonic-gate 	 * routing table below.
52287c478bd9Sstevel@tonic-gate 	 *
52297c478bd9Sstevel@tonic-gate 	 * Eg.	default	- gw1		<--- pire	(line 1)
52307c478bd9Sstevel@tonic-gate 	 *	gw1	- gw2				(line 2)
52317c478bd9Sstevel@tonic-gate 	 *	gw2	- hme0				(line 3)
52327c478bd9Sstevel@tonic-gate 	 *
52337c478bd9Sstevel@tonic-gate 	 * If we already have a cache ire for gw1 in 'cire', the
52347c478bd9Sstevel@tonic-gate 	 * ire_ftable_lookup above would have failed, since there is no
52357c478bd9Sstevel@tonic-gate 	 * interface ire to reach gw1. We will fallthru below.
52367c478bd9Sstevel@tonic-gate 	 *
52377c478bd9Sstevel@tonic-gate 	 * Here we duplicate the steps that ire_ftable_lookup() did in
52387c478bd9Sstevel@tonic-gate 	 * getting 'cire' from 'pire', in the MATCH_IRE_RECURSIVE case.
52397c478bd9Sstevel@tonic-gate 	 * The differences are the following
52407c478bd9Sstevel@tonic-gate 	 * i.   We want the interface ire only, so we call ire_ftable_lookup()
52417c478bd9Sstevel@tonic-gate 	 *	instead of ire_route_lookup()
52427c478bd9Sstevel@tonic-gate 	 * ii.  We look for only prefix routes in the 1st call below.
52437c478bd9Sstevel@tonic-gate 	 * ii.  We want to match on the ihandle in the 2nd call below.
52447c478bd9Sstevel@tonic-gate 	 */
52457c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE;
52467c478bd9Sstevel@tonic-gate 	if (pire->ire_ipif != NULL)
52477c478bd9Sstevel@tonic-gate 		match_flags |= MATCH_IRE_ILL_GROUP;
52487c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(pire->ire_gateway_addr, 0, 0, IRE_OFFSUBNET,
524945916cd2Sjpk 	    pire->ire_ipif, NULL, ALL_ZONES, 0, NULL, match_flags);
52507c478bd9Sstevel@tonic-gate 	if (ire == NULL)
52517c478bd9Sstevel@tonic-gate 		return (NULL);
52527c478bd9Sstevel@tonic-gate 	/*
52537c478bd9Sstevel@tonic-gate 	 * At this point 'ire' corresponds to the entry shown in line 2.
52547c478bd9Sstevel@tonic-gate 	 * gw_addr is 'gw2' in the example above.
52557c478bd9Sstevel@tonic-gate 	 */
52567c478bd9Sstevel@tonic-gate 	gw_addr = ire->ire_gateway_addr;
52577c478bd9Sstevel@tonic-gate 	gw_ipif = ire->ire_ipif;
52587c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 	match_flags |= MATCH_IRE_IHANDLE;
52617c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
526245916cd2Sjpk 	    gw_ipif, NULL, ALL_ZONES, cire->ire_ihandle, NULL, match_flags);
52637c478bd9Sstevel@tonic-gate 	return (ire);
52647c478bd9Sstevel@tonic-gate }
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate /*
52677c478bd9Sstevel@tonic-gate  * Locate the interface ire that is tied to the cache ire 'cire' via
52687c478bd9Sstevel@tonic-gate  * cire->ire_ihandle.
52697c478bd9Sstevel@tonic-gate  *
52707c478bd9Sstevel@tonic-gate  * We are trying to create the cache ire for an onlink destn. or
52717c478bd9Sstevel@tonic-gate  * gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
52727c478bd9Sstevel@tonic-gate  * case, after the ire has come back from ARP.
52737c478bd9Sstevel@tonic-gate  */
52747c478bd9Sstevel@tonic-gate ire_t *
52757c478bd9Sstevel@tonic-gate ire_ihandle_lookup_onlink(ire_t *cire)
52767c478bd9Sstevel@tonic-gate {
52777c478bd9Sstevel@tonic-gate 	ire_t	*ire;
52787c478bd9Sstevel@tonic-gate 	int	match_flags;
52797c478bd9Sstevel@tonic-gate 	int	i;
52807c478bd9Sstevel@tonic-gate 	int	j;
52817c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
52827c478bd9Sstevel@tonic-gate 
52837c478bd9Sstevel@tonic-gate 	ASSERT(cire != NULL);
52847c478bd9Sstevel@tonic-gate 
52857c478bd9Sstevel@tonic-gate 	/*
52867c478bd9Sstevel@tonic-gate 	 * We don't need to specify the zoneid to ire_ftable_lookup() below
52877c478bd9Sstevel@tonic-gate 	 * because the ihandle refers to an ipif which can be in only one zone.
52887c478bd9Sstevel@tonic-gate 	 */
52897c478bd9Sstevel@tonic-gate 	match_flags =  MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
52907c478bd9Sstevel@tonic-gate 	/*
52917c478bd9Sstevel@tonic-gate 	 * We know that the mask of the interface ire equals cire->ire_cmask.
52927c478bd9Sstevel@tonic-gate 	 * (When ip_newroute() created 'cire' for an on-link destn. it set its
52937c478bd9Sstevel@tonic-gate 	 * cmask from the interface ire's mask)
52947c478bd9Sstevel@tonic-gate 	 */
52957c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
52967c478bd9Sstevel@tonic-gate 	    IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
529745916cd2Sjpk 	    NULL, match_flags);
52987c478bd9Sstevel@tonic-gate 	if (ire != NULL)
52997c478bd9Sstevel@tonic-gate 		return (ire);
53007c478bd9Sstevel@tonic-gate 	/*
53017c478bd9Sstevel@tonic-gate 	 * If we didn't find an interface ire above, we can't declare failure.
53027c478bd9Sstevel@tonic-gate 	 * For backwards compatibility, we need to support prefix routes
53037c478bd9Sstevel@tonic-gate 	 * pointing to next hop gateways that are not on-link.
53047c478bd9Sstevel@tonic-gate 	 *
53057c478bd9Sstevel@tonic-gate 	 * In the resolver/noresolver case, ip_newroute() thinks it is creating
53067c478bd9Sstevel@tonic-gate 	 * the cache ire for an onlink destination in 'cire'. But 'cire' is
53077c478bd9Sstevel@tonic-gate 	 * not actually onlink, because ire_ftable_lookup() cheated it, by
53087c478bd9Sstevel@tonic-gate 	 * doing ire_route_lookup() twice and returning an interface ire.
53097c478bd9Sstevel@tonic-gate 	 *
53107c478bd9Sstevel@tonic-gate 	 * Eg. default	-	gw1			(line 1)
53117c478bd9Sstevel@tonic-gate 	 *	gw1	-	gw2			(line 2)
53127c478bd9Sstevel@tonic-gate 	 *	gw2	-	hme0			(line 3)
53137c478bd9Sstevel@tonic-gate 	 *
53147c478bd9Sstevel@tonic-gate 	 * In the above example, ip_newroute() tried to create the cache ire
53157c478bd9Sstevel@tonic-gate 	 * 'cire' for gw1, based on the interface route in line 3. The
53167c478bd9Sstevel@tonic-gate 	 * ire_ftable_lookup() above fails, because there is no interface route
53177c478bd9Sstevel@tonic-gate 	 * to reach gw1. (it is gw2). We fall thru below.
53187c478bd9Sstevel@tonic-gate 	 *
53197c478bd9Sstevel@tonic-gate 	 * Do a brute force search based on the ihandle in a subset of the
53207c478bd9Sstevel@tonic-gate 	 * forwarding tables, corresponding to cire->ire_cmask. Otherwise
53217c478bd9Sstevel@tonic-gate 	 * things become very complex, since we don't have 'pire' in this
53227c478bd9Sstevel@tonic-gate 	 * case. (Also note that this method is not possible in the offlink
53237c478bd9Sstevel@tonic-gate 	 * case because we don't know the mask)
53247c478bd9Sstevel@tonic-gate 	 */
53257c478bd9Sstevel@tonic-gate 	i = ip_mask_to_plen(cire->ire_cmask);
53267c478bd9Sstevel@tonic-gate 	if ((ip_forwarding_table[i]) == NULL)
53277c478bd9Sstevel@tonic-gate 		return (NULL);
53287c478bd9Sstevel@tonic-gate 	for (j = 0; j < ip_ftable_hash_size; j++) {
53297c478bd9Sstevel@tonic-gate 		irb_ptr = &ip_forwarding_table[i][j];
53307c478bd9Sstevel@tonic-gate 		rw_enter(&irb_ptr->irb_lock, RW_READER);
53317c478bd9Sstevel@tonic-gate 		for (ire = irb_ptr->irb_ire; ire != NULL;
53327c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
53337c478bd9Sstevel@tonic-gate 			if (ire->ire_marks & IRE_MARK_CONDEMNED)
53347c478bd9Sstevel@tonic-gate 				continue;
53357c478bd9Sstevel@tonic-gate 			if ((ire->ire_type & IRE_INTERFACE) &&
53367c478bd9Sstevel@tonic-gate 			    (ire->ire_ihandle == cire->ire_ihandle)) {
53377c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
53387c478bd9Sstevel@tonic-gate 				rw_exit(&irb_ptr->irb_lock);
53397c478bd9Sstevel@tonic-gate 				return (ire);
53407c478bd9Sstevel@tonic-gate 			}
53417c478bd9Sstevel@tonic-gate 		}
53427c478bd9Sstevel@tonic-gate 		rw_exit(&irb_ptr->irb_lock);
53437c478bd9Sstevel@tonic-gate 	}
53447c478bd9Sstevel@tonic-gate 	return (NULL);
53457c478bd9Sstevel@tonic-gate }
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate /*
53487c478bd9Sstevel@tonic-gate  * ire_mrtun_lookup() is called by ip_rput() when packet is to be
53497c478bd9Sstevel@tonic-gate  * tunneled through reverse tunnel. This is only supported for
53507c478bd9Sstevel@tonic-gate  * IPv4 packets
53517c478bd9Sstevel@tonic-gate  */
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate ire_t *
53547c478bd9Sstevel@tonic-gate ire_mrtun_lookup(ipaddr_t srcaddr, ill_t *ill)
53557c478bd9Sstevel@tonic-gate {
53567c478bd9Sstevel@tonic-gate 	irb_t *irb_ptr;
53577c478bd9Sstevel@tonic-gate 	ire_t *ire;
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
53607c478bd9Sstevel@tonic-gate 	ASSERT(!(ill->ill_isv6));
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	if (ip_mrtun_table == NULL)
53637c478bd9Sstevel@tonic-gate 		return (NULL);
53647c478bd9Sstevel@tonic-gate 	irb_ptr = &ip_mrtun_table[IRE_ADDR_HASH(srcaddr, IP_MRTUN_TABLE_SIZE)];
53657c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
53667c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
53677c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
53687c478bd9Sstevel@tonic-gate 			continue;
53697c478bd9Sstevel@tonic-gate 		if ((ire->ire_in_src_addr == srcaddr) &&
53707c478bd9Sstevel@tonic-gate 		    ire->ire_in_ill == ill) {
53717c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
53727c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
53737c478bd9Sstevel@tonic-gate 			return (ire);
53747c478bd9Sstevel@tonic-gate 		}
53757c478bd9Sstevel@tonic-gate 	}
53767c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
53777c478bd9Sstevel@tonic-gate 	return (NULL);
53787c478bd9Sstevel@tonic-gate }
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate /*
53817c478bd9Sstevel@tonic-gate  * Return the IRE_LOOPBACK, IRE_IF_RESOLVER or IRE_IF_NORESOLVER
53827c478bd9Sstevel@tonic-gate  * ire associated with the specified ipif.
53837c478bd9Sstevel@tonic-gate  *
53847c478bd9Sstevel@tonic-gate  * This might occasionally be called when IPIF_UP is not set since
53857c478bd9Sstevel@tonic-gate  * the IP_MULTICAST_IF as well as creating interface routes
53867c478bd9Sstevel@tonic-gate  * allows specifying a down ipif (ipif_lookup* match ipifs that are down).
53877c478bd9Sstevel@tonic-gate  *
53887c478bd9Sstevel@tonic-gate  * Note that if IPIF_NOLOCAL, IPIF_NOXMIT, or IPIF_DEPRECATED is set on
53897c478bd9Sstevel@tonic-gate  * the ipif, this routine might return NULL.
53907c478bd9Sstevel@tonic-gate  */
53917c478bd9Sstevel@tonic-gate ire_t *
539245916cd2Sjpk ipif_to_ire(const ipif_t *ipif)
53937c478bd9Sstevel@tonic-gate {
53947c478bd9Sstevel@tonic-gate 	ire_t	*ire;
53957c478bd9Sstevel@tonic-gate 
53967c478bd9Sstevel@tonic-gate 	ASSERT(!ipif->ipif_isv6);
53977c478bd9Sstevel@tonic-gate 	if (ipif->ipif_ire_type == IRE_LOOPBACK) {
53987c478bd9Sstevel@tonic-gate 		ire = ire_ctable_lookup(ipif->ipif_lcl_addr, 0, IRE_LOOPBACK,
539945916cd2Sjpk 		    ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
54007c478bd9Sstevel@tonic-gate 	} else if (ipif->ipif_flags & IPIF_POINTOPOINT) {
54017c478bd9Sstevel@tonic-gate 		/* In this case we need to lookup destination address. */
54027c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_pp_dst_addr, IP_HOST_MASK, 0,
540345916cd2Sjpk 		    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
54047c478bd9Sstevel@tonic-gate 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF | MATCH_IRE_MASK));
54057c478bd9Sstevel@tonic-gate 	} else {
54067c478bd9Sstevel@tonic-gate 		ire = ire_ftable_lookup(ipif->ipif_subnet,
54077c478bd9Sstevel@tonic-gate 		    ipif->ipif_net_mask, 0, IRE_INTERFACE, ipif, NULL,
540845916cd2Sjpk 		    ALL_ZONES, 0, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF |
54097c478bd9Sstevel@tonic-gate 		    MATCH_IRE_MASK));
54107c478bd9Sstevel@tonic-gate 	}
54117c478bd9Sstevel@tonic-gate 	return (ire);
54127c478bd9Sstevel@tonic-gate }
54137c478bd9Sstevel@tonic-gate 
54147c478bd9Sstevel@tonic-gate /*
54157c478bd9Sstevel@tonic-gate  * ire_walk function.
54167c478bd9Sstevel@tonic-gate  * Count the number of IRE_CACHE entries in different categories.
54177c478bd9Sstevel@tonic-gate  */
54187c478bd9Sstevel@tonic-gate void
54197c478bd9Sstevel@tonic-gate ire_cache_count(ire_t *ire, char *arg)
54207c478bd9Sstevel@tonic-gate {
54217c478bd9Sstevel@tonic-gate 	ire_cache_count_t *icc = (ire_cache_count_t *)arg;
54227c478bd9Sstevel@tonic-gate 
54237c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
54247c478bd9Sstevel@tonic-gate 		return;
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate 	icc->icc_total++;
54277c478bd9Sstevel@tonic-gate 
54287c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
54297c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
54307c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
54317c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
54327c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
54337c478bd9Sstevel@tonic-gate 			return;
54347c478bd9Sstevel@tonic-gate 		}
54357c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
54367c478bd9Sstevel@tonic-gate 	} else {
54377c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
54387c478bd9Sstevel@tonic-gate 			icc->icc_onlink++;
54397c478bd9Sstevel@tonic-gate 			return;
54407c478bd9Sstevel@tonic-gate 		}
54417c478bd9Sstevel@tonic-gate 	}
54427c478bd9Sstevel@tonic-gate 
54437c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
54447c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu)
54457c478bd9Sstevel@tonic-gate 		icc->icc_pmtu++;
54467c478bd9Sstevel@tonic-gate 	else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
54477c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count)
54487c478bd9Sstevel@tonic-gate 		icc->icc_offlink++;
54497c478bd9Sstevel@tonic-gate 	else
54507c478bd9Sstevel@tonic-gate 		icc->icc_unused++;
54517c478bd9Sstevel@tonic-gate }
54527c478bd9Sstevel@tonic-gate 
54537c478bd9Sstevel@tonic-gate /*
54547c478bd9Sstevel@tonic-gate  * ire_walk function called by ip_trash_ire_reclaim().
54557c478bd9Sstevel@tonic-gate  * Free a fraction of the IRE_CACHE cache entries. The fractions are
54567c478bd9Sstevel@tonic-gate  * different for different categories of IRE_CACHE entries.
54577c478bd9Sstevel@tonic-gate  * A fraction of zero means to not free any in that category.
54587c478bd9Sstevel@tonic-gate  * Use the hash bucket id plus lbolt as a random number. Thus if the fraction
54597c478bd9Sstevel@tonic-gate  * is N then every Nth hash bucket chain will be freed.
54607c478bd9Sstevel@tonic-gate  */
54617c478bd9Sstevel@tonic-gate void
54627c478bd9Sstevel@tonic-gate ire_cache_reclaim(ire_t *ire, char *arg)
54637c478bd9Sstevel@tonic-gate {
54647c478bd9Sstevel@tonic-gate 	ire_cache_reclaim_t *icr = (ire_cache_reclaim_t *)arg;
54657c478bd9Sstevel@tonic-gate 	uint_t rand;
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 	if (ire->ire_type != IRE_CACHE)
54687c478bd9Sstevel@tonic-gate 		return;
54697c478bd9Sstevel@tonic-gate 
54707c478bd9Sstevel@tonic-gate 	if (ire->ire_ipversion == IPV6_VERSION) {
54717c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
54727c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH_V6(ire->ire_addr_v6, ip6_cache_table_size);
54737c478bd9Sstevel@tonic-gate 		mutex_enter(&ire->ire_lock);
54747c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6)) {
54757c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
54767c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
54777c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
54787c478bd9Sstevel@tonic-gate 				ire_delete(ire);
54797c478bd9Sstevel@tonic-gate 				return;
54807c478bd9Sstevel@tonic-gate 			}
54817c478bd9Sstevel@tonic-gate 			goto done;
54827c478bd9Sstevel@tonic-gate 		}
54837c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
54847c478bd9Sstevel@tonic-gate 	} else {
54857c478bd9Sstevel@tonic-gate 		rand = (uint_t)lbolt +
54867c478bd9Sstevel@tonic-gate 		    IRE_ADDR_HASH(ire->ire_addr, ip_cache_table_size);
54877c478bd9Sstevel@tonic-gate 		if (ire->ire_gateway_addr == 0) {
54887c478bd9Sstevel@tonic-gate 			if (icr->icr_onlink != 0 &&
54897c478bd9Sstevel@tonic-gate 			    (rand/icr->icr_onlink)*icr->icr_onlink == rand) {
54907c478bd9Sstevel@tonic-gate 				ire_delete(ire);
54917c478bd9Sstevel@tonic-gate 				return;
54927c478bd9Sstevel@tonic-gate 			}
54937c478bd9Sstevel@tonic-gate 			goto done;
54947c478bd9Sstevel@tonic-gate 		}
54957c478bd9Sstevel@tonic-gate 	}
54967c478bd9Sstevel@tonic-gate 	/* Not onlink IRE */
54977c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipif != NULL);
54987c478bd9Sstevel@tonic-gate 	if (ire->ire_max_frag < ire->ire_ipif->ipif_mtu) {
54997c478bd9Sstevel@tonic-gate 		/* Use ptmu fraction */
55007c478bd9Sstevel@tonic-gate 		if (icr->icr_pmtu != 0 &&
55017c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_pmtu)*icr->icr_pmtu == rand) {
55027c478bd9Sstevel@tonic-gate 			ire_delete(ire);
55037c478bd9Sstevel@tonic-gate 			return;
55047c478bd9Sstevel@tonic-gate 		}
55057c478bd9Sstevel@tonic-gate 	} else if (ire->ire_tire_mark != ire->ire_ob_pkt_count +
55067c478bd9Sstevel@tonic-gate 	    ire->ire_ib_pkt_count) {
55077c478bd9Sstevel@tonic-gate 		/* Use offlink fraction */
55087c478bd9Sstevel@tonic-gate 		if (icr->icr_offlink != 0 &&
55097c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_offlink)*icr->icr_offlink == rand) {
55107c478bd9Sstevel@tonic-gate 			ire_delete(ire);
55117c478bd9Sstevel@tonic-gate 			return;
55127c478bd9Sstevel@tonic-gate 		}
55137c478bd9Sstevel@tonic-gate 	} else {
55147c478bd9Sstevel@tonic-gate 		/* Use unused fraction */
55157c478bd9Sstevel@tonic-gate 		if (icr->icr_unused != 0 &&
55167c478bd9Sstevel@tonic-gate 		    (rand/icr->icr_unused)*icr->icr_unused == rand) {
55177c478bd9Sstevel@tonic-gate 			ire_delete(ire);
55187c478bd9Sstevel@tonic-gate 			return;
55197c478bd9Sstevel@tonic-gate 		}
55207c478bd9Sstevel@tonic-gate 	}
55217c478bd9Sstevel@tonic-gate done:
55227c478bd9Sstevel@tonic-gate 	/*
55237c478bd9Sstevel@tonic-gate 	 * Update tire_mark so that those that haven't been used since this
55247c478bd9Sstevel@tonic-gate 	 * reclaim will be considered unused next time we reclaim.
55257c478bd9Sstevel@tonic-gate 	 */
55267c478bd9Sstevel@tonic-gate 	ire->ire_tire_mark = ire->ire_ob_pkt_count + ire->ire_ib_pkt_count;
55277c478bd9Sstevel@tonic-gate }
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate static void
55307c478bd9Sstevel@tonic-gate power2_roundup(uint32_t *value)
55317c478bd9Sstevel@tonic-gate {
55327c478bd9Sstevel@tonic-gate 	int i;
55337c478bd9Sstevel@tonic-gate 
55347c478bd9Sstevel@tonic-gate 	for (i = 1; i < 31; i++) {
55357c478bd9Sstevel@tonic-gate 		if (*value <= (1 << i))
55367c478bd9Sstevel@tonic-gate 			break;
55377c478bd9Sstevel@tonic-gate 	}
55387c478bd9Sstevel@tonic-gate 	*value = (1 << i);
55397c478bd9Sstevel@tonic-gate }
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate void
55427c478bd9Sstevel@tonic-gate ip_ire_init()
55437c478bd9Sstevel@tonic-gate {
55447c478bd9Sstevel@tonic-gate 	int i;
55457c478bd9Sstevel@tonic-gate 
55467c478bd9Sstevel@tonic-gate 	mutex_init(&ire_ft_init_lock, NULL, MUTEX_DEFAULT, 0);
55477c478bd9Sstevel@tonic-gate 	mutex_init(&ire_handle_lock, NULL, MUTEX_DEFAULT, NULL);
55487c478bd9Sstevel@tonic-gate 	mutex_init(&ire_mrtun_lock, NULL, MUTEX_DEFAULT, NULL);
55497c478bd9Sstevel@tonic-gate 	mutex_init(&ire_srcif_table_lock, NULL, MUTEX_DEFAULT, NULL);
55507c478bd9Sstevel@tonic-gate 
55517c478bd9Sstevel@tonic-gate 	/* Calculate the IPv4 cache table size. */
55527c478bd9Sstevel@tonic-gate 	ip_cache_table_size = MAX(ip_cache_table_size,
55537c478bd9Sstevel@tonic-gate 	    ((kmem_avail() >> ip_ire_mem_ratio) / sizeof (ire_t) /
55547c478bd9Sstevel@tonic-gate 	    ip_ire_max_bucket_cnt));
55557c478bd9Sstevel@tonic-gate 	if (ip_cache_table_size > ip_max_cache_table_size)
55567c478bd9Sstevel@tonic-gate 		ip_cache_table_size = ip_max_cache_table_size;
55577c478bd9Sstevel@tonic-gate 	/*
55587c478bd9Sstevel@tonic-gate 	 * Make sure that the table size is always a power of 2.  The
55597c478bd9Sstevel@tonic-gate 	 * hash macro IRE_ADDR_HASH() depends on that.
55607c478bd9Sstevel@tonic-gate 	 */
55617c478bd9Sstevel@tonic-gate 	power2_roundup(&ip_cache_table_size);
55627c478bd9Sstevel@tonic-gate 
55637c478bd9Sstevel@tonic-gate 	ip_cache_table = (irb_t *)kmem_zalloc(ip_cache_table_size *
55647c478bd9Sstevel@tonic-gate 	    sizeof (irb_t), KM_SLEEP);
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip_cache_table_size; i++) {
55677c478bd9Sstevel@tonic-gate 		rw_init(&ip_cache_table[i].irb_lock, NULL,
55687c478bd9Sstevel@tonic-gate 		    RW_DEFAULT, NULL);
55697c478bd9Sstevel@tonic-gate 	}
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 	/* Calculate the IPv6 cache table size. */
55727c478bd9Sstevel@tonic-gate 	ip6_cache_table_size = MAX(ip6_cache_table_size,
55737c478bd9Sstevel@tonic-gate 	    ((kmem_avail() >> ip_ire_mem_ratio) / sizeof (ire_t) /
55747c478bd9Sstevel@tonic-gate 	    ip6_ire_max_bucket_cnt));
55757c478bd9Sstevel@tonic-gate 	if (ip6_cache_table_size > ip6_max_cache_table_size)
55767c478bd9Sstevel@tonic-gate 		ip6_cache_table_size = ip6_max_cache_table_size;
55777c478bd9Sstevel@tonic-gate 	/*
55787c478bd9Sstevel@tonic-gate 	 * Make sure that the table size is always a power of 2.  The
55797c478bd9Sstevel@tonic-gate 	 * hash macro IRE_ADDR_HASH_V6() depends on that.
55807c478bd9Sstevel@tonic-gate 	 */
55817c478bd9Sstevel@tonic-gate 	power2_roundup(&ip6_cache_table_size);
55827c478bd9Sstevel@tonic-gate 
55837c478bd9Sstevel@tonic-gate 	ip_cache_table_v6 = (irb_t *)kmem_zalloc(ip6_cache_table_size *
55847c478bd9Sstevel@tonic-gate 	    sizeof (irb_t), KM_SLEEP);
55857c478bd9Sstevel@tonic-gate 
55867c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip6_cache_table_size; i++) {
55877c478bd9Sstevel@tonic-gate 		rw_init(&ip_cache_table_v6[i].irb_lock, NULL,
55887c478bd9Sstevel@tonic-gate 		    RW_DEFAULT, NULL);
55897c478bd9Sstevel@tonic-gate 	}
55907c478bd9Sstevel@tonic-gate 	/*
55917c478bd9Sstevel@tonic-gate 	 * Create ire caches, ire_reclaim()
55927c478bd9Sstevel@tonic-gate 	 * will give IRE_CACHE back to system when needed.
55937c478bd9Sstevel@tonic-gate 	 * This needs to be done here before anything else, since
55947c478bd9Sstevel@tonic-gate 	 * ire_add() expects the cache to be created.
55957c478bd9Sstevel@tonic-gate 	 */
55967c478bd9Sstevel@tonic-gate 	ire_cache = kmem_cache_create("ire_cache",
55977c478bd9Sstevel@tonic-gate 		sizeof (ire_t), 0, ip_ire_constructor,
55987c478bd9Sstevel@tonic-gate 		ip_ire_destructor, ip_trash_ire_reclaim, NULL, NULL, 0);
55997c478bd9Sstevel@tonic-gate 
56007c478bd9Sstevel@tonic-gate 	/*
56017c478bd9Sstevel@tonic-gate 	 * Initialize ip_mrtun_table to NULL now, it will be
56027c478bd9Sstevel@tonic-gate 	 * populated by ip_rt_add if reverse tunnel is created
56037c478bd9Sstevel@tonic-gate 	 */
56047c478bd9Sstevel@tonic-gate 	ip_mrtun_table = NULL;
56057c478bd9Sstevel@tonic-gate 
56067c478bd9Sstevel@tonic-gate 	/*
56077c478bd9Sstevel@tonic-gate 	 * Make sure that the forwarding table size is a power of 2.
56087c478bd9Sstevel@tonic-gate 	 * The IRE*_ADDR_HASH() macroes depend on that.
56097c478bd9Sstevel@tonic-gate 	 */
56107c478bd9Sstevel@tonic-gate 	power2_roundup(&ip_ftable_hash_size);
56117c478bd9Sstevel@tonic-gate 	power2_roundup(&ip6_ftable_hash_size);
56127c478bd9Sstevel@tonic-gate }
56137c478bd9Sstevel@tonic-gate 
56147c478bd9Sstevel@tonic-gate void
56157c478bd9Sstevel@tonic-gate ip_ire_fini()
56167c478bd9Sstevel@tonic-gate {
56177c478bd9Sstevel@tonic-gate 	int i;
56187c478bd9Sstevel@tonic-gate 
56197c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire_ft_init_lock);
56207c478bd9Sstevel@tonic-gate 	mutex_destroy(&ire_handle_lock);
56217c478bd9Sstevel@tonic-gate 
56227c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip_cache_table_size; i++) {
56237c478bd9Sstevel@tonic-gate 		rw_destroy(&ip_cache_table[i].irb_lock);
56247c478bd9Sstevel@tonic-gate 	}
56257c478bd9Sstevel@tonic-gate 	kmem_free(ip_cache_table, ip_cache_table_size * sizeof (irb_t));
56267c478bd9Sstevel@tonic-gate 
56277c478bd9Sstevel@tonic-gate 	for (i = 0; i < ip6_cache_table_size; i++) {
56287c478bd9Sstevel@tonic-gate 		rw_destroy(&ip_cache_table_v6[i].irb_lock);
56297c478bd9Sstevel@tonic-gate 	}
56307c478bd9Sstevel@tonic-gate 	kmem_free(ip_cache_table_v6, ip6_cache_table_size * sizeof (irb_t));
56317c478bd9Sstevel@tonic-gate 
56327c478bd9Sstevel@tonic-gate 	if (ip_mrtun_table != NULL) {
56337c478bd9Sstevel@tonic-gate 		for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
56347c478bd9Sstevel@tonic-gate 			rw_destroy(&ip_mrtun_table[i].irb_lock);
56357c478bd9Sstevel@tonic-gate 		}
56367c478bd9Sstevel@tonic-gate 		kmem_free(ip_mrtun_table, IP_MRTUN_TABLE_SIZE * sizeof (irb_t));
56377c478bd9Sstevel@tonic-gate 	}
56387c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(ire_cache);
56397c478bd9Sstevel@tonic-gate }
56407c478bd9Sstevel@tonic-gate 
56417c478bd9Sstevel@tonic-gate int
56427c478bd9Sstevel@tonic-gate ire_add_mrtun(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
56437c478bd9Sstevel@tonic-gate {
56447c478bd9Sstevel@tonic-gate 	ire_t   *ire1;
56457c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
56467c478bd9Sstevel@tonic-gate 	ire_t	**irep;
56477c478bd9Sstevel@tonic-gate 	ire_t	*ire;
56487c478bd9Sstevel@tonic-gate 	int	i;
56497c478bd9Sstevel@tonic-gate 	uint_t	max_frag;
56507c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
56517c478bd9Sstevel@tonic-gate 	int error;
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	ire = *ire_p;
56547c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
56557c478bd9Sstevel@tonic-gate 	/* Is ip_mrtun_table empty ? */
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate 	if (ip_mrtun_table == NULL) {
56587c478bd9Sstevel@tonic-gate 		/* create the mrtun table */
56597c478bd9Sstevel@tonic-gate 		mutex_enter(&ire_mrtun_lock);
56607c478bd9Sstevel@tonic-gate 		if (ip_mrtun_table == NULL) {
56617c478bd9Sstevel@tonic-gate 			ip_mrtun_table =
56627c478bd9Sstevel@tonic-gate 			    (irb_t *)kmem_zalloc(IP_MRTUN_TABLE_SIZE *
56637c478bd9Sstevel@tonic-gate 			    sizeof (irb_t), KM_NOSLEEP);
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 			if (ip_mrtun_table == NULL) {
56667c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_add_mrtun: allocation failure\n"));
56677c478bd9Sstevel@tonic-gate 				mutex_exit(&ire_mrtun_lock);
56687c478bd9Sstevel@tonic-gate 				ire_refrele(ire);
56697c478bd9Sstevel@tonic-gate 				*ire_p = NULL;
56707c478bd9Sstevel@tonic-gate 				return (ENOMEM);
56717c478bd9Sstevel@tonic-gate 			}
56727c478bd9Sstevel@tonic-gate 
56737c478bd9Sstevel@tonic-gate 			for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
56747c478bd9Sstevel@tonic-gate 			    rw_init(&ip_mrtun_table[i].irb_lock, NULL,
56757c478bd9Sstevel@tonic-gate 				    RW_DEFAULT, NULL);
56767c478bd9Sstevel@tonic-gate 			}
56777c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_add_mrtun: mrtun table is created\n"));
56787c478bd9Sstevel@tonic-gate 		}
56797c478bd9Sstevel@tonic-gate 		/* some other thread got it and created the table */
56807c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_mrtun_lock);
56817c478bd9Sstevel@tonic-gate 	}
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate 	/*
56847c478bd9Sstevel@tonic-gate 	 * Check for duplicate in the bucket and insert in the table
56857c478bd9Sstevel@tonic-gate 	 */
56867c478bd9Sstevel@tonic-gate 	irb_ptr = &(ip_mrtun_table[IRE_ADDR_HASH(ire->ire_in_src_addr,
56877c478bd9Sstevel@tonic-gate 	    IP_MRTUN_TABLE_SIZE)]);
56887c478bd9Sstevel@tonic-gate 
56897c478bd9Sstevel@tonic-gate 	/*
56907c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
56917c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
56927c478bd9Sstevel@tonic-gate 	 *
56937c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
56947c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
56957c478bd9Sstevel@tonic-gate 	 */
56967c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
56977c478bd9Sstevel@tonic-gate 	if (error != 0) {
56987c478bd9Sstevel@tonic-gate 		/*
56997c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
57007c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
57017c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
57027c478bd9Sstevel@tonic-gate 		 */
57037c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
57047c478bd9Sstevel@tonic-gate 		ire_delete(ire);
57057c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_add_mrtun: ire_atomic_start failed\n"));
57067c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
57077c478bd9Sstevel@tonic-gate 		return (error);
57087c478bd9Sstevel@tonic-gate 	}
57097c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
57107c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
57117c478bd9Sstevel@tonic-gate 			continue;
57127c478bd9Sstevel@tonic-gate 		/* has anyone inserted the route in the meanwhile ? */
57137c478bd9Sstevel@tonic-gate 		if (ire1->ire_in_ill == ire->ire_in_ill &&
57147c478bd9Sstevel@tonic-gate 		    ire1->ire_in_src_addr == ire->ire_in_src_addr) {
57157c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_mrtun: Duplicate entry exists\n"));
57167c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
57177c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
57187c478bd9Sstevel@tonic-gate 			ire_delete(ire);
57197c478bd9Sstevel@tonic-gate 			/* Return the old ire */
57207c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
57217c478bd9Sstevel@tonic-gate 			return (0);
57227c478bd9Sstevel@tonic-gate 		}
57237c478bd9Sstevel@tonic-gate 	}
57247c478bd9Sstevel@tonic-gate 
57257c478bd9Sstevel@tonic-gate 	/* Atomically set the ire_max_frag */
57267c478bd9Sstevel@tonic-gate 	max_frag = *ire->ire_max_fragp;
57277c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = NULL;
57287c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
57317c478bd9Sstevel@tonic-gate 	if (*irep != NULL) {
57327c478bd9Sstevel@tonic-gate 		/* Find the last ire which matches ire_in_src_addr */
57337c478bd9Sstevel@tonic-gate 		ire1 = *irep;
57347c478bd9Sstevel@tonic-gate 		while (ire1->ire_in_src_addr == ire->ire_in_src_addr) {
57357c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
57367c478bd9Sstevel@tonic-gate 			ire1 = *irep;
57377c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
57387c478bd9Sstevel@tonic-gate 				break;
57397c478bd9Sstevel@tonic-gate 		}
57407c478bd9Sstevel@tonic-gate 	}
57417c478bd9Sstevel@tonic-gate 	ire1 = *irep;
57427c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
57437c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
57447c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
57457c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
57467c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
57477c478bd9Sstevel@tonic-gate 	membar_producer();
57487c478bd9Sstevel@tonic-gate 	*irep = ire;
57497c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
57507c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
57517c478bd9Sstevel@tonic-gate 
57527c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_add_mrtun: created and linked ire %p\n", (void *)*irep));
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 	/*
57557c478bd9Sstevel@tonic-gate 	 * Protect ire_mrtun_count and ill_mrtun_refcnt from
57567c478bd9Sstevel@tonic-gate 	 * another thread trying to add ire in the table
57577c478bd9Sstevel@tonic-gate 	 */
57587c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_mrtun_lock);
57597c478bd9Sstevel@tonic-gate 	ire_mrtun_count++;
57607c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_mrtun_lock);
57617c478bd9Sstevel@tonic-gate 	/*
57627c478bd9Sstevel@tonic-gate 	 * ill_mrtun_refcnt is protected by the ill_lock held via
57637c478bd9Sstevel@tonic-gate 	 * ire_atomic_start
57647c478bd9Sstevel@tonic-gate 	 */
57657c478bd9Sstevel@tonic-gate 	ire->ire_in_ill->ill_mrtun_refcnt++;
57667c478bd9Sstevel@tonic-gate 
57677c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
57687c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
57697c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
57707c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
57717c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
57727c478bd9Sstevel@tonic-gate 		}
57737c478bd9Sstevel@tonic-gate 	} else {
57747c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
57757c478bd9Sstevel@tonic-gate 	}
57767c478bd9Sstevel@tonic-gate 
57777c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
57787c478bd9Sstevel@tonic-gate 	ire_fastpath(ire);
57797c478bd9Sstevel@tonic-gate 	*ire_p = ire;
57807c478bd9Sstevel@tonic-gate 	return (0);
57817c478bd9Sstevel@tonic-gate }
57827c478bd9Sstevel@tonic-gate 
57837c478bd9Sstevel@tonic-gate 
57847c478bd9Sstevel@tonic-gate /* Walks down the mrtun table */
57857c478bd9Sstevel@tonic-gate 
57867c478bd9Sstevel@tonic-gate void
57877c478bd9Sstevel@tonic-gate ire_walk_ill_mrtun(uint_t match_flags, uint_t ire_type, pfv_t func, void *arg,
57887c478bd9Sstevel@tonic-gate     ill_t *ill)
57897c478bd9Sstevel@tonic-gate {
57907c478bd9Sstevel@tonic-gate 	irb_t	*irb;
57917c478bd9Sstevel@tonic-gate 	ire_t	*ire;
57927c478bd9Sstevel@tonic-gate 	int	i;
57937c478bd9Sstevel@tonic-gate 	int	ret;
57947c478bd9Sstevel@tonic-gate 
57957c478bd9Sstevel@tonic-gate 	ASSERT((!(match_flags & (MATCH_IRE_WQ | MATCH_IRE_ILL |
57967c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL_GROUP))) || (ill != NULL));
57977c478bd9Sstevel@tonic-gate 	ASSERT(match_flags == 0 || ire_type == IRE_MIPRTUN);
57987c478bd9Sstevel@tonic-gate 
57997c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_mrtun_lock);
58007c478bd9Sstevel@tonic-gate 	if (ire_mrtun_count == 0) {
58017c478bd9Sstevel@tonic-gate 		mutex_exit(&ire_mrtun_lock);
58027c478bd9Sstevel@tonic-gate 		return;
58037c478bd9Sstevel@tonic-gate 	}
58047c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_mrtun_lock);
58057c478bd9Sstevel@tonic-gate 
58067c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_walk_ill_mrtun:walking the reverse tunnel table \n"));
58077c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_MRTUN_TABLE_SIZE; i++) {
58087c478bd9Sstevel@tonic-gate 
58097c478bd9Sstevel@tonic-gate 		irb = &(ip_mrtun_table[i]);
58107c478bd9Sstevel@tonic-gate 		if (irb->irb_ire == NULL)
58117c478bd9Sstevel@tonic-gate 			continue;
58127c478bd9Sstevel@tonic-gate 		IRB_REFHOLD(irb);
58137c478bd9Sstevel@tonic-gate 		for (ire = irb->irb_ire; ire != NULL;
58147c478bd9Sstevel@tonic-gate 		    ire = ire->ire_next) {
58157c478bd9Sstevel@tonic-gate 			ASSERT(ire->ire_ipversion == IPV4_VERSION);
58167c478bd9Sstevel@tonic-gate 			if (match_flags != 0) {
58177c478bd9Sstevel@tonic-gate 				ret = ire_walk_ill_match(
58187c478bd9Sstevel@tonic-gate 				    match_flags, ire_type,
58197c478bd9Sstevel@tonic-gate 				    ire, ill, ALL_ZONES);
58207c478bd9Sstevel@tonic-gate 			}
58217c478bd9Sstevel@tonic-gate 			if (match_flags == 0 || ret)
58227c478bd9Sstevel@tonic-gate 				(*func)(ire, arg);
58237c478bd9Sstevel@tonic-gate 		}
58247c478bd9Sstevel@tonic-gate 		IRB_REFRELE(irb);
58257c478bd9Sstevel@tonic-gate 	}
58267c478bd9Sstevel@tonic-gate }
58277c478bd9Sstevel@tonic-gate 
58287c478bd9Sstevel@tonic-gate /*
58297c478bd9Sstevel@tonic-gate  * Source interface based lookup routine (IPV4 only).
58307c478bd9Sstevel@tonic-gate  * This routine is called only when RTA_SRCIFP bitflag is set
58317c478bd9Sstevel@tonic-gate  * by routing socket while adding/deleting the route and it is
58327c478bd9Sstevel@tonic-gate  * also called from ip_rput() when packets arrive from an interface
58337c478bd9Sstevel@tonic-gate  * for which ill_srcif_ref_cnt is positive. This function is useful
58347c478bd9Sstevel@tonic-gate  * when a packet coming from one interface must be forwarded to another
58357c478bd9Sstevel@tonic-gate  * designated interface to reach the correct node. This function is also
58367c478bd9Sstevel@tonic-gate  * called from ip_newroute when the link-layer address of an ire is resolved.
58377c478bd9Sstevel@tonic-gate  * We need to make sure that ip_newroute searches for IRE_IF_RESOLVER type
58387c478bd9Sstevel@tonic-gate  * ires--thus the ire_type parameter is needed.
58397c478bd9Sstevel@tonic-gate  */
58407c478bd9Sstevel@tonic-gate 
58417c478bd9Sstevel@tonic-gate ire_t *
58427c478bd9Sstevel@tonic-gate ire_srcif_table_lookup(ipaddr_t dst_addr, int ire_type, ipif_t *ipif,
58437c478bd9Sstevel@tonic-gate     ill_t *in_ill, int flags)
58447c478bd9Sstevel@tonic-gate {
58457c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr;
58467c478bd9Sstevel@tonic-gate 	ire_t	*ire;
58477c478bd9Sstevel@tonic-gate 	irb_t	*ire_srcif_table;
58487c478bd9Sstevel@tonic-gate 
58497c478bd9Sstevel@tonic-gate 	ASSERT(in_ill != NULL && !in_ill->ill_isv6);
58507c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & (MATCH_IRE_ILL|MATCH_IRE_ILL_GROUP)) ||
58517c478bd9Sstevel@tonic-gate 	    (ipif != NULL && !ipif->ipif_isv6));
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate 	/*
58547c478bd9Sstevel@tonic-gate 	 * No need to lock the ill since it is refheld by the caller of this
58557c478bd9Sstevel@tonic-gate 	 * function
58567c478bd9Sstevel@tonic-gate 	 */
58577c478bd9Sstevel@tonic-gate 	if (in_ill->ill_srcif_table == NULL) {
58587c478bd9Sstevel@tonic-gate 		return (NULL);
58597c478bd9Sstevel@tonic-gate 	}
58607c478bd9Sstevel@tonic-gate 
58617c478bd9Sstevel@tonic-gate 	if (!(flags & MATCH_IRE_TYPE)) {
58627c478bd9Sstevel@tonic-gate 		flags |= MATCH_IRE_TYPE;
58637c478bd9Sstevel@tonic-gate 		ire_type = IRE_INTERFACE;
58647c478bd9Sstevel@tonic-gate 	}
58657c478bd9Sstevel@tonic-gate 	ire_srcif_table = in_ill->ill_srcif_table;
58667c478bd9Sstevel@tonic-gate 	irb_ptr = &ire_srcif_table[IRE_ADDR_HASH(dst_addr,
58677c478bd9Sstevel@tonic-gate 	    IP_SRCIF_TABLE_SIZE)];
58687c478bd9Sstevel@tonic-gate 	rw_enter(&irb_ptr->irb_lock, RW_READER);
58697c478bd9Sstevel@tonic-gate 	for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
58707c478bd9Sstevel@tonic-gate 		if (ire->ire_marks & IRE_MARK_CONDEMNED)
58717c478bd9Sstevel@tonic-gate 			continue;
58727c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire, dst_addr, ire->ire_mask, 0,
587345916cd2Sjpk 		    ire_type, ipif, ire->ire_zoneid, 0, NULL, flags)) {
58747c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire);
58757c478bd9Sstevel@tonic-gate 			rw_exit(&irb_ptr->irb_lock);
58767c478bd9Sstevel@tonic-gate 			return (ire);
58777c478bd9Sstevel@tonic-gate 		}
58787c478bd9Sstevel@tonic-gate 	}
58797c478bd9Sstevel@tonic-gate 	/* Not Found */
58807c478bd9Sstevel@tonic-gate 	rw_exit(&irb_ptr->irb_lock);
58817c478bd9Sstevel@tonic-gate 	return (NULL);
58827c478bd9Sstevel@tonic-gate }
58837c478bd9Sstevel@tonic-gate 
58847c478bd9Sstevel@tonic-gate 
58857c478bd9Sstevel@tonic-gate /*
58867c478bd9Sstevel@tonic-gate  * Adds the ire into the special routing table which is hanging off of
58877c478bd9Sstevel@tonic-gate  * the src_ipif->ipif_ill. It also increments the refcnt in the ill.
58887c478bd9Sstevel@tonic-gate  * The forward table contains only IRE_IF_RESOLVER, IRE_IF_NORESOLVER
58897c478bd9Sstevel@tonic-gate  * i,e. IRE_INTERFACE entries. Originally the dlureq_mp field is NULL
58907c478bd9Sstevel@tonic-gate  * for IRE_IF_RESOLVER entry because we do not have the dst_addr's
58917c478bd9Sstevel@tonic-gate  * link-layer address at the time of addition.
58927c478bd9Sstevel@tonic-gate  * Upon resolving the address from ARP, dlureq_mp field is updated with
58937c478bd9Sstevel@tonic-gate  * proper information in ire_update_srcif_v4.
58947c478bd9Sstevel@tonic-gate  */
58957c478bd9Sstevel@tonic-gate static int
58967c478bd9Sstevel@tonic-gate ire_add_srcif_v4(ire_t **ire_p, queue_t *q, mblk_t *mp, ipsq_func_t func)
58977c478bd9Sstevel@tonic-gate {
58987c478bd9Sstevel@tonic-gate 	ire_t	*ire1;
58997c478bd9Sstevel@tonic-gate 	irb_t	*ire_srcifp_table = NULL;
59007c478bd9Sstevel@tonic-gate 	irb_t	*irb_ptr = NULL;
59017c478bd9Sstevel@tonic-gate 	ire_t   **irep;
59027c478bd9Sstevel@tonic-gate 	ire_t   *ire;
59037c478bd9Sstevel@tonic-gate 	int	flags;
59047c478bd9Sstevel@tonic-gate 	int	i;
59057c478bd9Sstevel@tonic-gate 	ill_t	*stq_ill;
59067c478bd9Sstevel@tonic-gate 	uint_t	max_frag;
59077c478bd9Sstevel@tonic-gate 	int error = 0;
59087c478bd9Sstevel@tonic-gate 
59097c478bd9Sstevel@tonic-gate 	ire = *ire_p;
59107c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill != NULL);
59117c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
59127c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type == IRE_IF_NORESOLVER ||
59137c478bd9Sstevel@tonic-gate 	    ire->ire_type == IRE_IF_RESOLVER);
59147c478bd9Sstevel@tonic-gate 
59157c478bd9Sstevel@tonic-gate 	ire->ire_mask = IP_HOST_MASK;
59167c478bd9Sstevel@tonic-gate 	/* Update ire_dlureq_mp with NULL value upon creation */
59177c478bd9Sstevel@tonic-gate 	if (ire->ire_type == IRE_IF_RESOLVER) {
59187c478bd9Sstevel@tonic-gate 		/*
59197c478bd9Sstevel@tonic-gate 		 * assign NULL now, it will be updated
59207c478bd9Sstevel@tonic-gate 		 * with correct value upon returning from
59217c478bd9Sstevel@tonic-gate 		 * ARP
59227c478bd9Sstevel@tonic-gate 		 */
59237c478bd9Sstevel@tonic-gate 		ire->ire_dlureq_mp = NULL;
59247c478bd9Sstevel@tonic-gate 	} else {
59257c478bd9Sstevel@tonic-gate 		ire->ire_dlureq_mp = ill_dlur_gen(NULL,
59267c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_phys_addr_length,
59277c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_sap,
59287c478bd9Sstevel@tonic-gate 		    ire->ire_ipif->ipif_ill->ill_sap_length);
59297c478bd9Sstevel@tonic-gate 	}
59307c478bd9Sstevel@tonic-gate 	/* Make sure the address is properly masked. */
59317c478bd9Sstevel@tonic-gate 	ire->ire_addr &= ire->ire_mask;
59327c478bd9Sstevel@tonic-gate 
59337c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_max_fragp != NULL);
59347c478bd9Sstevel@tonic-gate 	max_frag = *ire->ire_max_fragp;
59357c478bd9Sstevel@tonic-gate 	ire->ire_max_fragp = NULL;
59367c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = MIN(max_frag, IP_MAXPACKET);
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_in_ill->ill_lock);
59397c478bd9Sstevel@tonic-gate 	if (ire->ire_in_ill->ill_srcif_table == NULL) {
59407c478bd9Sstevel@tonic-gate 		/* create the incoming interface based table */
59417c478bd9Sstevel@tonic-gate 		ire->ire_in_ill->ill_srcif_table =
59427c478bd9Sstevel@tonic-gate 		    (irb_t *)kmem_zalloc(IP_SRCIF_TABLE_SIZE *
59437c478bd9Sstevel@tonic-gate 			sizeof (irb_t), KM_NOSLEEP);
59447c478bd9Sstevel@tonic-gate 		if (ire->ire_in_ill->ill_srcif_table == NULL) {
59457c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_srcif_v4: Allocation fail\n"));
59467c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_in_ill->ill_lock);
59477c478bd9Sstevel@tonic-gate 			ire_delete(ire);
59487c478bd9Sstevel@tonic-gate 			*ire_p = NULL;
59497c478bd9Sstevel@tonic-gate 			return (ENOMEM);
59507c478bd9Sstevel@tonic-gate 		}
59517c478bd9Sstevel@tonic-gate 		ire_srcifp_table = ire->ire_in_ill->ill_srcif_table;
59527c478bd9Sstevel@tonic-gate 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
59537c478bd9Sstevel@tonic-gate 			rw_init(&ire_srcifp_table[i].irb_lock, NULL,
59547c478bd9Sstevel@tonic-gate 			    RW_DEFAULT, NULL);
59557c478bd9Sstevel@tonic-gate 		}
59567c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_add_srcif_v4: table created for ill %p\n",
59577c478bd9Sstevel@tonic-gate 		    (void *)ire->ire_in_ill));
59587c478bd9Sstevel@tonic-gate 	}
59597c478bd9Sstevel@tonic-gate 	/* Check for duplicate and insert */
59607c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_in_ill->ill_srcif_table != NULL);
59617c478bd9Sstevel@tonic-gate 	irb_ptr =
59627c478bd9Sstevel@tonic-gate 	    &(ire->ire_in_ill->ill_srcif_table[IRE_ADDR_HASH(ire->ire_addr,
59637c478bd9Sstevel@tonic-gate 	    IP_SRCIF_TABLE_SIZE)]);
59647c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_in_ill->ill_lock);
59657c478bd9Sstevel@tonic-gate 	flags = (MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_GW);
59667c478bd9Sstevel@tonic-gate 	flags |= MATCH_IRE_IPIF;
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate 	/*
59697c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
59707c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
59717c478bd9Sstevel@tonic-gate 	 *
59727c478bd9Sstevel@tonic-gate 	 * If ipif or ill is changing ire_atomic_start() may queue the
59737c478bd9Sstevel@tonic-gate 	 * request and return EINPROGRESS.
59747c478bd9Sstevel@tonic-gate 	 */
59757c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb_ptr, ire, q, mp, func);
59767c478bd9Sstevel@tonic-gate 	if (error != 0) {
59777c478bd9Sstevel@tonic-gate 		/*
59787c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
59797c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
59807c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
59817c478bd9Sstevel@tonic-gate 		 */
59827c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
59837c478bd9Sstevel@tonic-gate 		ire_delete(ire);
59847c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_add_srcif_v4: ire_atomic_start failed\n"));
59857c478bd9Sstevel@tonic-gate 		*ire_p = NULL;
59867c478bd9Sstevel@tonic-gate 		return (error);
59877c478bd9Sstevel@tonic-gate 	}
59887c478bd9Sstevel@tonic-gate 	for (ire1 = irb_ptr->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
59897c478bd9Sstevel@tonic-gate 		if (ire1->ire_marks & IRE_MARK_CONDEMNED)
59907c478bd9Sstevel@tonic-gate 			continue;
59917c478bd9Sstevel@tonic-gate 		if (ire1->ire_zoneid != ire->ire_zoneid)
59927c478bd9Sstevel@tonic-gate 			continue;
59937c478bd9Sstevel@tonic-gate 		/* Has anyone inserted route in the meanwhile ? */
59947c478bd9Sstevel@tonic-gate 		if (ire_match_args(ire1, ire->ire_addr, ire->ire_mask, 0,
599545916cd2Sjpk 		    ire->ire_type, ire->ire_ipif, ire->ire_zoneid, 0, NULL,
599645916cd2Sjpk 		    flags)) {
59977c478bd9Sstevel@tonic-gate 			ip1dbg(("ire_add_srcif_v4 : Duplicate entry exists\n"));
59987c478bd9Sstevel@tonic-gate 			IRE_REFHOLD(ire1);
59997c478bd9Sstevel@tonic-gate 			ire_atomic_end(irb_ptr, ire);
60007c478bd9Sstevel@tonic-gate 			ire_delete(ire);
60017c478bd9Sstevel@tonic-gate 			/* Return old ire as in ire_add_v4 */
60027c478bd9Sstevel@tonic-gate 			*ire_p = ire1;
60037c478bd9Sstevel@tonic-gate 			return (0);
60047c478bd9Sstevel@tonic-gate 		}
60057c478bd9Sstevel@tonic-gate 	}
60067c478bd9Sstevel@tonic-gate 	irep = (ire_t **)irb_ptr;
60077c478bd9Sstevel@tonic-gate 	if (*irep != NULL) {
60087c478bd9Sstevel@tonic-gate 		/* Find the last ire which matches ire_addr */
60097c478bd9Sstevel@tonic-gate 		ire1 = *irep;
60107c478bd9Sstevel@tonic-gate 		while (ire1->ire_addr == ire->ire_addr) {
60117c478bd9Sstevel@tonic-gate 			irep = &ire1->ire_next;
60127c478bd9Sstevel@tonic-gate 			ire1 = *irep;
60137c478bd9Sstevel@tonic-gate 			if (ire1 == NULL)
60147c478bd9Sstevel@tonic-gate 				break;
60157c478bd9Sstevel@tonic-gate 		}
60167c478bd9Sstevel@tonic-gate 	}
60177c478bd9Sstevel@tonic-gate 	ire1 = *irep;
60187c478bd9Sstevel@tonic-gate 	if (ire1 != NULL)
60197c478bd9Sstevel@tonic-gate 		ire1->ire_ptpn = &ire->ire_next;
60207c478bd9Sstevel@tonic-gate 	ire->ire_next = ire1;
60217c478bd9Sstevel@tonic-gate 	/* Link the new one in. */
60227c478bd9Sstevel@tonic-gate 	ire->ire_ptpn = irep;
60237c478bd9Sstevel@tonic-gate 	membar_producer();
60247c478bd9Sstevel@tonic-gate 	*irep = ire;
60257c478bd9Sstevel@tonic-gate 	ire->ire_bucket = irb_ptr;
60267c478bd9Sstevel@tonic-gate 	IRE_REFHOLD_LOCKED(ire);
60277c478bd9Sstevel@tonic-gate 
60287c478bd9Sstevel@tonic-gate 	/*
60297c478bd9Sstevel@tonic-gate 	 * Protect ire_in_ill->ill_srcif_refcnt and table reference count.
60307c478bd9Sstevel@tonic-gate 	 * Note, ire_atomic_start already grabs the ire_in_ill->ill_lock
60317c478bd9Sstevel@tonic-gate 	 * so ill_srcif_refcnt is already protected.
60327c478bd9Sstevel@tonic-gate 	 */
60337c478bd9Sstevel@tonic-gate 	ire->ire_in_ill->ill_srcif_refcnt++;
60347c478bd9Sstevel@tonic-gate 	mutex_enter(&ire_srcif_table_lock);
60357c478bd9Sstevel@tonic-gate 	ire_srcif_table_count++;
60367c478bd9Sstevel@tonic-gate 	mutex_exit(&ire_srcif_table_lock);
60377c478bd9Sstevel@tonic-gate 	irb_ptr->irb_ire_cnt++;
60387c478bd9Sstevel@tonic-gate 	if (ire->ire_ipif != NULL) {
60397c478bd9Sstevel@tonic-gate 		ire->ire_ipif->ipif_ire_cnt++;
60407c478bd9Sstevel@tonic-gate 		if (ire->ire_stq != NULL) {
60417c478bd9Sstevel@tonic-gate 			stq_ill = (ill_t *)ire->ire_stq->q_ptr;
60427c478bd9Sstevel@tonic-gate 			stq_ill->ill_ire_cnt++;
60437c478bd9Sstevel@tonic-gate 		}
60447c478bd9Sstevel@tonic-gate 	} else {
60457c478bd9Sstevel@tonic-gate 		ASSERT(ire->ire_stq == NULL);
60467c478bd9Sstevel@tonic-gate 	}
60477c478bd9Sstevel@tonic-gate 
60487c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb_ptr, ire);
60497c478bd9Sstevel@tonic-gate 	*ire_p = ire;
60507c478bd9Sstevel@tonic-gate 	return (0);
60517c478bd9Sstevel@tonic-gate }
60527c478bd9Sstevel@tonic-gate 
60537c478bd9Sstevel@tonic-gate 
60547c478bd9Sstevel@tonic-gate /*
60557c478bd9Sstevel@tonic-gate  * This function is called by ire_add_then_send when ARP request comes
60567c478bd9Sstevel@tonic-gate  * back to ip_wput->ire_add_then_send for resolved ire in the interface
60577c478bd9Sstevel@tonic-gate  * based routing table. At this point, it only needs to update the resolver
60587c478bd9Sstevel@tonic-gate  * information for the ire. The passed ire is returned to the caller as it
60597c478bd9Sstevel@tonic-gate  * is the ire which is created as mblk.
60607c478bd9Sstevel@tonic-gate  */
60617c478bd9Sstevel@tonic-gate 
60627c478bd9Sstevel@tonic-gate static ire_t *
60637c478bd9Sstevel@tonic-gate ire_update_srcif_v4(ire_t *ire)
60647c478bd9Sstevel@tonic-gate {
60657c478bd9Sstevel@tonic-gate 	ire_t   *ire1;
60667c478bd9Sstevel@tonic-gate 	irb_t	*irb;
60677c478bd9Sstevel@tonic-gate 	int	error;
60687c478bd9Sstevel@tonic-gate 
60697c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_type != IRE_MIPRTUN &&
60707c478bd9Sstevel@tonic-gate 	    ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER);
60717c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_ipversion == IPV4_VERSION);
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate 	/*
60747c478bd9Sstevel@tonic-gate 	 * This ire is from ARP. Update
60757c478bd9Sstevel@tonic-gate 	 * ire_dlureq_mp info
60767c478bd9Sstevel@tonic-gate 	 */
60777c478bd9Sstevel@tonic-gate 	ire1 = ire_srcif_table_lookup(ire->ire_addr,
60787c478bd9Sstevel@tonic-gate 	    IRE_IF_RESOLVER, ire->ire_ipif,
60797c478bd9Sstevel@tonic-gate 	    ire->ire_in_ill,
60807c478bd9Sstevel@tonic-gate 	    MATCH_IRE_ILL | MATCH_IRE_TYPE);
60817c478bd9Sstevel@tonic-gate 	if (ire1 == NULL) {
60827c478bd9Sstevel@tonic-gate 		/* Mobile node registration expired ? */
60837c478bd9Sstevel@tonic-gate 		ire_delete(ire);
60847c478bd9Sstevel@tonic-gate 		return (NULL);
60857c478bd9Sstevel@tonic-gate 	}
60867c478bd9Sstevel@tonic-gate 	irb = ire1->ire_bucket;
60877c478bd9Sstevel@tonic-gate 	ASSERT(irb != NULL);
60887c478bd9Sstevel@tonic-gate 	/*
60897c478bd9Sstevel@tonic-gate 	 * Start the atomic add of the ire. Grab the ill locks,
60907c478bd9Sstevel@tonic-gate 	 * ill_g_usesrc_lock and the bucket lock.
60917c478bd9Sstevel@tonic-gate 	 */
60927c478bd9Sstevel@tonic-gate 	error = ire_atomic_start(irb, ire1, NULL, NULL, NULL);
60937c478bd9Sstevel@tonic-gate 	if (error != 0) {
60947c478bd9Sstevel@tonic-gate 		/*
60957c478bd9Sstevel@tonic-gate 		 * We don't know whether it is a valid ipif or not.
60967c478bd9Sstevel@tonic-gate 		 * So, set it to NULL. This assumes that the ire has not added
60977c478bd9Sstevel@tonic-gate 		 * a reference to the ipif.
60987c478bd9Sstevel@tonic-gate 		 */
60997c478bd9Sstevel@tonic-gate 		ire->ire_ipif = NULL;
61007c478bd9Sstevel@tonic-gate 		ire_delete(ire);
61017c478bd9Sstevel@tonic-gate 		ip1dbg(("ire_update_srcif_v4: ire_atomic_start failed\n"));
61027c478bd9Sstevel@tonic-gate 		return (NULL);
61037c478bd9Sstevel@tonic-gate 	}
61047c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_max_fragp == NULL);
61057c478bd9Sstevel@tonic-gate 	ire->ire_max_frag = ire1->ire_max_frag;
61067c478bd9Sstevel@tonic-gate 	/*
61077c478bd9Sstevel@tonic-gate 	 * Update resolver information and
61087c478bd9Sstevel@tonic-gate 	 * send-to queue.
61097c478bd9Sstevel@tonic-gate 	 */
61107c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_dlureq_mp != NULL);
61117c478bd9Sstevel@tonic-gate 	ire1->ire_dlureq_mp = copyb(ire->ire_dlureq_mp);
61127c478bd9Sstevel@tonic-gate 	if (ire1->ire_dlureq_mp ==  NULL) {
61137c478bd9Sstevel@tonic-gate 		ip0dbg(("ire_update_srcif: copyb failed\n"));
61147c478bd9Sstevel@tonic-gate 		ire_refrele(ire1);
61157c478bd9Sstevel@tonic-gate 		ire_refrele(ire);
61167c478bd9Sstevel@tonic-gate 		ire_atomic_end(irb, ire1);
61177c478bd9Sstevel@tonic-gate 		return (NULL);
61187c478bd9Sstevel@tonic-gate 	}
61197c478bd9Sstevel@tonic-gate 	ire1->ire_stq = ire->ire_stq;
61207c478bd9Sstevel@tonic-gate 
61217c478bd9Sstevel@tonic-gate 	ASSERT(ire->ire_fp_mp == NULL);
61227c478bd9Sstevel@tonic-gate 
61237c478bd9Sstevel@tonic-gate 	ire_atomic_end(irb, ire1);
61247c478bd9Sstevel@tonic-gate 	ire_refrele(ire1);
61257c478bd9Sstevel@tonic-gate 	/* Return the passed ire */
61267c478bd9Sstevel@tonic-gate 	return (ire);   /* Update done */
61277c478bd9Sstevel@tonic-gate }
61287c478bd9Sstevel@tonic-gate 
61297c478bd9Sstevel@tonic-gate 
61307c478bd9Sstevel@tonic-gate /*
61317c478bd9Sstevel@tonic-gate  * Check if another multirt route resolution is needed.
61327c478bd9Sstevel@tonic-gate  * B_TRUE is returned is there remain a resolvable route,
61337c478bd9Sstevel@tonic-gate  * or if no route for that dst is resolved yet.
61347c478bd9Sstevel@tonic-gate  * B_FALSE is returned if all routes for that dst are resolved
61357c478bd9Sstevel@tonic-gate  * or if the remaining unresolved routes are actually not
61367c478bd9Sstevel@tonic-gate  * resolvable.
61377c478bd9Sstevel@tonic-gate  * This only works in the global zone.
61387c478bd9Sstevel@tonic-gate  */
61397c478bd9Sstevel@tonic-gate boolean_t
614045916cd2Sjpk ire_multirt_need_resolve(ipaddr_t dst, const ts_label_t *tsl)
61417c478bd9Sstevel@tonic-gate {
61427c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
61437c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
61447c478bd9Sstevel@tonic-gate 	ire_t	*fire;
61457c478bd9Sstevel@tonic-gate 	ire_t	*cire;
61467c478bd9Sstevel@tonic-gate 	irb_t	*firb;
61477c478bd9Sstevel@tonic-gate 	irb_t	*cirb;
61487c478bd9Sstevel@tonic-gate 	int	unres_cnt = 0;
61497c478bd9Sstevel@tonic-gate 	boolean_t resolvable = B_FALSE;
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_HOST that matches the destination */
61527c478bd9Sstevel@tonic-gate 	first_fire = ire_ftable_lookup(dst, IP_HOST_MASK, 0, IRE_HOST, NULL,
615345916cd2Sjpk 	    NULL, ALL_ZONES, 0, tsl,
615445916cd2Sjpk 	    MATCH_IRE_MASK | MATCH_IRE_TYPE | MATCH_IRE_SECATTR);
61557c478bd9Sstevel@tonic-gate 
61567c478bd9Sstevel@tonic-gate 	/* No route at all */
61577c478bd9Sstevel@tonic-gate 	if (first_fire == NULL) {
61587c478bd9Sstevel@tonic-gate 		return (B_TRUE);
61597c478bd9Sstevel@tonic-gate 	}
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
61627c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate 	/* Retrieve the first IRE_CACHE ire for that destination. */
616545916cd2Sjpk 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl);
61667c478bd9Sstevel@tonic-gate 
61677c478bd9Sstevel@tonic-gate 	/* No resolved route. */
61687c478bd9Sstevel@tonic-gate 	if (first_cire == NULL) {
61697c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
61707c478bd9Sstevel@tonic-gate 		return (B_TRUE);
61717c478bd9Sstevel@tonic-gate 	}
61727c478bd9Sstevel@tonic-gate 
61737c478bd9Sstevel@tonic-gate 	/*
61747c478bd9Sstevel@tonic-gate 	 * At least one route is resolved. Here we look through the forward
61757c478bd9Sstevel@tonic-gate 	 * and cache tables, to compare the number of declared routes
61767c478bd9Sstevel@tonic-gate 	 * with the number of resolved routes. The search for a resolvable
61777c478bd9Sstevel@tonic-gate 	 * route is performed only if at least one route remains
61787c478bd9Sstevel@tonic-gate 	 * unresolved.
61797c478bd9Sstevel@tonic-gate 	 */
61807c478bd9Sstevel@tonic-gate 	cirb = first_cire->ire_bucket;
61817c478bd9Sstevel@tonic-gate 	ASSERT(cirb != NULL);
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 	/* Count the number of routes to that dest that are declared. */
61847c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
61857c478bd9Sstevel@tonic-gate 	for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
61867c478bd9Sstevel@tonic-gate 		if (!(fire->ire_flags & RTF_MULTIRT))
61877c478bd9Sstevel@tonic-gate 			continue;
61887c478bd9Sstevel@tonic-gate 		if (fire->ire_addr != dst)
61897c478bd9Sstevel@tonic-gate 			continue;
61907c478bd9Sstevel@tonic-gate 		unres_cnt++;
61917c478bd9Sstevel@tonic-gate 	}
61927c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
61937c478bd9Sstevel@tonic-gate 
61947c478bd9Sstevel@tonic-gate 	/* Then subtract the number of routes to that dst that are resolved */
61957c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(cirb);
61967c478bd9Sstevel@tonic-gate 	for (cire = first_cire; cire != NULL; cire = cire->ire_next) {
61977c478bd9Sstevel@tonic-gate 		if (!(cire->ire_flags & RTF_MULTIRT))
61987c478bd9Sstevel@tonic-gate 			continue;
61997c478bd9Sstevel@tonic-gate 		if (cire->ire_addr != dst)
62007c478bd9Sstevel@tonic-gate 			continue;
62017c478bd9Sstevel@tonic-gate 		if (cire->ire_marks & (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))
62027c478bd9Sstevel@tonic-gate 			continue;
62037c478bd9Sstevel@tonic-gate 		unres_cnt--;
62047c478bd9Sstevel@tonic-gate 	}
62057c478bd9Sstevel@tonic-gate 	IRB_REFRELE(cirb);
62067c478bd9Sstevel@tonic-gate 
62077c478bd9Sstevel@tonic-gate 	/* At least one route is unresolved; search for a resolvable route. */
62087c478bd9Sstevel@tonic-gate 	if (unres_cnt > 0)
62097c478bd9Sstevel@tonic-gate 		resolvable = ire_multirt_lookup(&first_cire, &first_fire,
621045916cd2Sjpk 		    MULTIRT_USESTAMP | MULTIRT_CACHEGW, tsl);
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	if (first_fire != NULL)
62137c478bd9Sstevel@tonic-gate 		ire_refrele(first_fire);
62147c478bd9Sstevel@tonic-gate 
62157c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
62167c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate 	return (resolvable);
62197c478bd9Sstevel@tonic-gate }
62207c478bd9Sstevel@tonic-gate 
62217c478bd9Sstevel@tonic-gate 
62227c478bd9Sstevel@tonic-gate /*
62237c478bd9Sstevel@tonic-gate  * Explore a forward_table bucket, starting from fire_arg.
62247c478bd9Sstevel@tonic-gate  * fire_arg MUST be an IRE_HOST entry.
62257c478bd9Sstevel@tonic-gate  *
62267c478bd9Sstevel@tonic-gate  * Return B_TRUE and update *ire_arg and *fire_arg
62277c478bd9Sstevel@tonic-gate  * if at least one resolvable route is found. *ire_arg
62287c478bd9Sstevel@tonic-gate  * is the IRE entry for *fire_arg's gateway.
62297c478bd9Sstevel@tonic-gate  *
62307c478bd9Sstevel@tonic-gate  * Return B_FALSE otherwise (all routes are resolved or
62317c478bd9Sstevel@tonic-gate  * the remaining unresolved routes are all unresolvable).
62327c478bd9Sstevel@tonic-gate  *
62337c478bd9Sstevel@tonic-gate  * The IRE selection relies on a priority mechanism
62347c478bd9Sstevel@tonic-gate  * driven by the flags passed in by the caller.
62357c478bd9Sstevel@tonic-gate  * The caller, such as ip_newroute_ipif(), can get the most
62367c478bd9Sstevel@tonic-gate  * relevant ire at each stage of a multiple route resolution.
62377c478bd9Sstevel@tonic-gate  *
62387c478bd9Sstevel@tonic-gate  * The rules are:
62397c478bd9Sstevel@tonic-gate  *
62407c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is specified in flags, IRE_CACHETABLE
62417c478bd9Sstevel@tonic-gate  *   ires are preferred for the gateway. This gives the highest
62427c478bd9Sstevel@tonic-gate  *   priority to routes that can be resolved without using
62437c478bd9Sstevel@tonic-gate  *   a resolver.
62447c478bd9Sstevel@tonic-gate  *
62457c478bd9Sstevel@tonic-gate  * - if MULTIRT_CACHEGW is not specified, or if MULTIRT_CACHEGW
62467c478bd9Sstevel@tonic-gate  *   is specified but no IRE_CACHETABLE ire entry for the gateway
62477c478bd9Sstevel@tonic-gate  *   is found, the following rules apply.
62487c478bd9Sstevel@tonic-gate  *
62497c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is specified in flags, IRE_INTERFACE
62507c478bd9Sstevel@tonic-gate  *   ires for the gateway, that have not been tried since
62517c478bd9Sstevel@tonic-gate  *   a configurable amount of time, are preferred.
62527c478bd9Sstevel@tonic-gate  *   This applies when a resolver must be invoked for
62537c478bd9Sstevel@tonic-gate  *   a missing route, but we don't want to use the resolver
62547c478bd9Sstevel@tonic-gate  *   upon each packet emission. If no such resolver is found,
62557c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
62567c478bd9Sstevel@tonic-gate  *   The MULTIRT_USESTAMP flag can be combined with
62577c478bd9Sstevel@tonic-gate  *   MULTIRT_CACHEGW.
62587c478bd9Sstevel@tonic-gate  *
62597c478bd9Sstevel@tonic-gate  * - if MULTIRT_USESTAMP is not specified in flags, the first
62607c478bd9Sstevel@tonic-gate  *   unresolved but resolvable route is selected.
62617c478bd9Sstevel@tonic-gate  *
62627c478bd9Sstevel@tonic-gate  * - Otherwise, there is no resolvalble route, and
62637c478bd9Sstevel@tonic-gate  *   B_FALSE is returned.
62647c478bd9Sstevel@tonic-gate  *
62657c478bd9Sstevel@tonic-gate  * At last, MULTIRT_SETSTAMP can be specified in flags to
62667c478bd9Sstevel@tonic-gate  * request the timestamp of unresolvable routes to
62677c478bd9Sstevel@tonic-gate  * be refreshed. This prevents the useless exploration
62687c478bd9Sstevel@tonic-gate  * of those routes for a while, when MULTIRT_USESTAMP is used.
62697c478bd9Sstevel@tonic-gate  *
62707c478bd9Sstevel@tonic-gate  * This only works in the global zone.
62717c478bd9Sstevel@tonic-gate  */
62727c478bd9Sstevel@tonic-gate boolean_t
627345916cd2Sjpk ire_multirt_lookup(ire_t **ire_arg, ire_t **fire_arg, uint32_t flags,
627445916cd2Sjpk     const ts_label_t *tsl)
62757c478bd9Sstevel@tonic-gate {
62767c478bd9Sstevel@tonic-gate 	clock_t	delta;
62777c478bd9Sstevel@tonic-gate 	ire_t	*best_fire = NULL;
62787c478bd9Sstevel@tonic-gate 	ire_t	*best_cire = NULL;
62797c478bd9Sstevel@tonic-gate 	ire_t	*first_fire;
62807c478bd9Sstevel@tonic-gate 	ire_t	*first_cire;
62817c478bd9Sstevel@tonic-gate 	ire_t	*fire;
62827c478bd9Sstevel@tonic-gate 	ire_t	*cire;
62837c478bd9Sstevel@tonic-gate 	irb_t	*firb = NULL;
62847c478bd9Sstevel@tonic-gate 	irb_t	*cirb = NULL;
62857c478bd9Sstevel@tonic-gate 	ire_t	*gw_ire;
62867c478bd9Sstevel@tonic-gate 	boolean_t	already_resolved;
62877c478bd9Sstevel@tonic-gate 	boolean_t	res;
62887c478bd9Sstevel@tonic-gate 	ipaddr_t	dst;
62897c478bd9Sstevel@tonic-gate 	ipaddr_t	gw;
62907c478bd9Sstevel@tonic-gate 
62917c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: *ire_arg %p, *fire_arg %p, flags %04x\n",
62927c478bd9Sstevel@tonic-gate 	    (void *)*ire_arg, (void *)*fire_arg, flags));
62937c478bd9Sstevel@tonic-gate 
62947c478bd9Sstevel@tonic-gate 	ASSERT(ire_arg != NULL);
62957c478bd9Sstevel@tonic-gate 	ASSERT(fire_arg != NULL);
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	/* Not an IRE_HOST ire; give up. */
62987c478bd9Sstevel@tonic-gate 	if ((*fire_arg == NULL) || ((*fire_arg)->ire_type != IRE_HOST)) {
62997c478bd9Sstevel@tonic-gate 		return (B_FALSE);
63007c478bd9Sstevel@tonic-gate 	}
63017c478bd9Sstevel@tonic-gate 
63027c478bd9Sstevel@tonic-gate 	/* This is the first IRE_HOST ire for that destination. */
63037c478bd9Sstevel@tonic-gate 	first_fire = *fire_arg;
63047c478bd9Sstevel@tonic-gate 	firb = first_fire->ire_bucket;
63057c478bd9Sstevel@tonic-gate 	ASSERT(firb != NULL);
63067c478bd9Sstevel@tonic-gate 
63077c478bd9Sstevel@tonic-gate 	dst = first_fire->ire_addr;
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: dst %08x\n", ntohl(dst)));
63107c478bd9Sstevel@tonic-gate 
63117c478bd9Sstevel@tonic-gate 	/*
63127c478bd9Sstevel@tonic-gate 	 * Retrieve the first IRE_CACHE ire for that destination;
63137c478bd9Sstevel@tonic-gate 	 * if we don't find one, no route for that dest is
63147c478bd9Sstevel@tonic-gate 	 * resolved yet.
63157c478bd9Sstevel@tonic-gate 	 */
631645916cd2Sjpk 	first_cire = ire_cache_lookup(dst, GLOBAL_ZONEID, tsl);
63177c478bd9Sstevel@tonic-gate 	if (first_cire != NULL) {
63187c478bd9Sstevel@tonic-gate 		cirb = first_cire->ire_bucket;
63197c478bd9Sstevel@tonic-gate 	}
63207c478bd9Sstevel@tonic-gate 
63217c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: first_cire %p\n", (void *)first_cire));
63227c478bd9Sstevel@tonic-gate 
63237c478bd9Sstevel@tonic-gate 	/*
63247c478bd9Sstevel@tonic-gate 	 * Search for a resolvable route, giving the top priority
63257c478bd9Sstevel@tonic-gate 	 * to routes that can be resolved without any call to the resolver.
63267c478bd9Sstevel@tonic-gate 	 */
63277c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(firb);
63287c478bd9Sstevel@tonic-gate 
63297c478bd9Sstevel@tonic-gate 	if (!CLASSD(dst)) {
63307c478bd9Sstevel@tonic-gate 		/*
63317c478bd9Sstevel@tonic-gate 		 * For all multiroute IRE_HOST ires for that destination,
63327c478bd9Sstevel@tonic-gate 		 * check if the route via the IRE_HOST's gateway is
63337c478bd9Sstevel@tonic-gate 		 * resolved yet.
63347c478bd9Sstevel@tonic-gate 		 */
63357c478bd9Sstevel@tonic-gate 		for (fire = first_fire; fire != NULL; fire = fire->ire_next) {
63367c478bd9Sstevel@tonic-gate 
63377c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
63387c478bd9Sstevel@tonic-gate 				continue;
63397c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
63407c478bd9Sstevel@tonic-gate 				continue;
63417c478bd9Sstevel@tonic-gate 
634245916cd2Sjpk 			if (fire->ire_gw_secattr != NULL &&
634345916cd2Sjpk 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
634445916cd2Sjpk 				continue;
634545916cd2Sjpk 			}
634645916cd2Sjpk 
63477c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
63487c478bd9Sstevel@tonic-gate 
63497c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, "
63507c478bd9Sstevel@tonic-gate 			    "ire_addr %08x, ire_gateway_addr %08x\n",
63517c478bd9Sstevel@tonic-gate 			    (void *)fire, ntohl(fire->ire_addr), ntohl(gw)));
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
63567c478bd9Sstevel@tonic-gate 				ASSERT(cirb != NULL);
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
63597c478bd9Sstevel@tonic-gate 				/*
63607c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
63617c478bd9Sstevel@tonic-gate 				 * destination.
63627c478bd9Sstevel@tonic-gate 				 */
63637c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
63647c478bd9Sstevel@tonic-gate 				    cire != NULL;
63657c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
63687c478bd9Sstevel@tonic-gate 						continue;
63697c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
63707c478bd9Sstevel@tonic-gate 						continue;
63717c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
63727c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
63737c478bd9Sstevel@tonic-gate 						IRE_MARK_HIDDEN))
63747c478bd9Sstevel@tonic-gate 						continue;
637545916cd2Sjpk 
637645916cd2Sjpk 					if (cire->ire_gw_secattr != NULL &&
637745916cd2Sjpk 					    tsol_ire_match_gwattr(cire,
637845916cd2Sjpk 					    tsl) != 0) {
637945916cd2Sjpk 						continue;
638045916cd2Sjpk 					}
638145916cd2Sjpk 
63827c478bd9Sstevel@tonic-gate 					/*
63837c478bd9Sstevel@tonic-gate 					 * Check if the IRE_CACHE's gateway
63847c478bd9Sstevel@tonic-gate 					 * matches the IRE_HOST's gateway.
63857c478bd9Sstevel@tonic-gate 					 */
63867c478bd9Sstevel@tonic-gate 					if (cire->ire_gateway_addr == gw) {
63877c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
63887c478bd9Sstevel@tonic-gate 						break;
63897c478bd9Sstevel@tonic-gate 					}
63907c478bd9Sstevel@tonic-gate 				}
63917c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
63927c478bd9Sstevel@tonic-gate 			}
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 			/*
63957c478bd9Sstevel@tonic-gate 			 * This route is already resolved;
63967c478bd9Sstevel@tonic-gate 			 * proceed with next one.
63977c478bd9Sstevel@tonic-gate 			 */
63987c478bd9Sstevel@tonic-gate 			if (already_resolved) {
63997c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found cire %p, "
64007c478bd9Sstevel@tonic-gate 				    "already resolved\n", (void *)cire));
64017c478bd9Sstevel@tonic-gate 				continue;
64027c478bd9Sstevel@tonic-gate 			}
64037c478bd9Sstevel@tonic-gate 
64047c478bd9Sstevel@tonic-gate 			/*
64057c478bd9Sstevel@tonic-gate 			 * The route is unresolved; is it actually
64067c478bd9Sstevel@tonic-gate 			 * resolvable, i.e. is there a cache or a resolver
64077c478bd9Sstevel@tonic-gate 			 * for the gateway?
64087c478bd9Sstevel@tonic-gate 			 */
64097c478bd9Sstevel@tonic-gate 			gw_ire = ire_route_lookup(gw, 0, 0, 0, NULL, NULL,
641045916cd2Sjpk 			    ALL_ZONES, tsl,
641145916cd2Sjpk 			    MATCH_IRE_RECURSIVE | MATCH_IRE_SECATTR);
64127c478bd9Sstevel@tonic-gate 
64137c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: looked up gw_ire %p\n",
64147c478bd9Sstevel@tonic-gate 			    (void *)gw_ire));
64157c478bd9Sstevel@tonic-gate 
64167c478bd9Sstevel@tonic-gate 			/*
64177c478bd9Sstevel@tonic-gate 			 * If gw_ire is typed IRE_CACHETABLE,
64187c478bd9Sstevel@tonic-gate 			 * this route can be resolved without any call to the
64197c478bd9Sstevel@tonic-gate 			 * resolver. If the MULTIRT_CACHEGW flag is set,
64207c478bd9Sstevel@tonic-gate 			 * give the top priority to this ire and exit the
64217c478bd9Sstevel@tonic-gate 			 * loop.
64227c478bd9Sstevel@tonic-gate 			 * This is typically the case when an ARP reply
64237c478bd9Sstevel@tonic-gate 			 * is processed through ip_wput_nondata().
64247c478bd9Sstevel@tonic-gate 			 */
64257c478bd9Sstevel@tonic-gate 			if ((flags & MULTIRT_CACHEGW) &&
64267c478bd9Sstevel@tonic-gate 			    (gw_ire != NULL) &&
64277c478bd9Sstevel@tonic-gate 			    (gw_ire->ire_type & IRE_CACHETABLE)) {
64287c478bd9Sstevel@tonic-gate 				/*
64297c478bd9Sstevel@tonic-gate 				 * Release the resolver associated to the
64307c478bd9Sstevel@tonic-gate 				 * previous candidate best ire, if any.
64317c478bd9Sstevel@tonic-gate 				 */
64327c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
64337c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
64347c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
64357c478bd9Sstevel@tonic-gate 				}
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate 				best_fire = fire;
64387c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
64397c478bd9Sstevel@tonic-gate 
64407c478bd9Sstevel@tonic-gate 				ip2dbg(("ire_multirt_lookup: found top prio "
64417c478bd9Sstevel@tonic-gate 				    "best_fire %p, best_cire %p\n",
64427c478bd9Sstevel@tonic-gate 				    (void *)best_fire, (void *)best_cire));
64437c478bd9Sstevel@tonic-gate 				break;
64447c478bd9Sstevel@tonic-gate 			}
64457c478bd9Sstevel@tonic-gate 
64467c478bd9Sstevel@tonic-gate 			/*
64477c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
64487c478bd9Sstevel@tonic-gate 			 * attempt to  resolve that route.
64497c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take that
64507c478bd9Sstevel@tonic-gate 			 * route into account only if this time interval
64517c478bd9Sstevel@tonic-gate 			 * exceeds ip_multirt_resolution_interval;
64527c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
64537c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
64547c478bd9Sstevel@tonic-gate 			 */
64557c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
64567c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
64577c478bd9Sstevel@tonic-gate 
64587c478bd9Sstevel@tonic-gate 			res = (boolean_t)
64597c478bd9Sstevel@tonic-gate 			    ((delta > ip_multirt_resolution_interval) ||
64607c478bd9Sstevel@tonic-gate 				(!(flags & MULTIRT_USESTAMP)));
64617c478bd9Sstevel@tonic-gate 
64627c478bd9Sstevel@tonic-gate 			ip2dbg(("ire_multirt_lookup: fire %p, delta %lu, "
64637c478bd9Sstevel@tonic-gate 			    "res %d\n",
64647c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, res));
64657c478bd9Sstevel@tonic-gate 
64667c478bd9Sstevel@tonic-gate 			if (res) {
64677c478bd9Sstevel@tonic-gate 				/*
64687c478bd9Sstevel@tonic-gate 				 * We are here if MULTIRT_USESTAMP flag is set
64697c478bd9Sstevel@tonic-gate 				 * and the resolver for fire's gateway
64707c478bd9Sstevel@tonic-gate 				 * has not been tried since
64717c478bd9Sstevel@tonic-gate 				 * ip_multirt_resolution_interval, or if
64727c478bd9Sstevel@tonic-gate 				 * MULTIRT_USESTAMP is not set but gw_ire did
64737c478bd9Sstevel@tonic-gate 				 * not fill the conditions for MULTIRT_CACHEGW,
64747c478bd9Sstevel@tonic-gate 				 * or if neither MULTIRT_USESTAMP nor
64757c478bd9Sstevel@tonic-gate 				 * MULTIRT_CACHEGW are set.
64767c478bd9Sstevel@tonic-gate 				 */
64777c478bd9Sstevel@tonic-gate 				if (gw_ire != NULL) {
64787c478bd9Sstevel@tonic-gate 					if (best_fire == NULL) {
64797c478bd9Sstevel@tonic-gate 						ASSERT(best_cire == NULL);
64807c478bd9Sstevel@tonic-gate 
64817c478bd9Sstevel@tonic-gate 						best_fire = fire;
64827c478bd9Sstevel@tonic-gate 						best_cire = gw_ire;
64837c478bd9Sstevel@tonic-gate 
64847c478bd9Sstevel@tonic-gate 						ip2dbg(("ire_multirt_lookup:"
64857c478bd9Sstevel@tonic-gate 						    "found candidate "
64867c478bd9Sstevel@tonic-gate 						    "best_fire %p, "
64877c478bd9Sstevel@tonic-gate 						    "best_cire %p\n",
64887c478bd9Sstevel@tonic-gate 						    (void *)best_fire,
64897c478bd9Sstevel@tonic-gate 						    (void *)best_cire));
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate 						/*
64927c478bd9Sstevel@tonic-gate 						 * If MULTIRT_CACHEGW is not
64937c478bd9Sstevel@tonic-gate 						 * set, we ignore the top
64947c478bd9Sstevel@tonic-gate 						 * priority ires that can
64957c478bd9Sstevel@tonic-gate 						 * be resolved without any
64967c478bd9Sstevel@tonic-gate 						 * call to the resolver;
64977c478bd9Sstevel@tonic-gate 						 * In that case, there is
64987c478bd9Sstevel@tonic-gate 						 * actually no need
64997c478bd9Sstevel@tonic-gate 						 * to continue the loop.
65007c478bd9Sstevel@tonic-gate 						 */
65017c478bd9Sstevel@tonic-gate 						if (!(flags &
65027c478bd9Sstevel@tonic-gate 						    MULTIRT_CACHEGW)) {
65037c478bd9Sstevel@tonic-gate 							break;
65047c478bd9Sstevel@tonic-gate 						}
65057c478bd9Sstevel@tonic-gate 						continue;
65067c478bd9Sstevel@tonic-gate 					}
65077c478bd9Sstevel@tonic-gate 				} else {
65087c478bd9Sstevel@tonic-gate 					/*
65097c478bd9Sstevel@tonic-gate 					 * No resolver for the gateway: the
65107c478bd9Sstevel@tonic-gate 					 * route is not resolvable.
65117c478bd9Sstevel@tonic-gate 					 * If the MULTIRT_SETSTAMP flag is
65127c478bd9Sstevel@tonic-gate 					 * set, we stamp the IRE_HOST ire,
65137c478bd9Sstevel@tonic-gate 					 * so we will not select it again
65147c478bd9Sstevel@tonic-gate 					 * during this resolution interval.
65157c478bd9Sstevel@tonic-gate 					 */
65167c478bd9Sstevel@tonic-gate 					if (flags & MULTIRT_SETSTAMP)
65177c478bd9Sstevel@tonic-gate 						fire->ire_last_used_time =
65187c478bd9Sstevel@tonic-gate 						    lbolt;
65197c478bd9Sstevel@tonic-gate 				}
65207c478bd9Sstevel@tonic-gate 			}
65217c478bd9Sstevel@tonic-gate 
65227c478bd9Sstevel@tonic-gate 			if (gw_ire != NULL)
65237c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
65247c478bd9Sstevel@tonic-gate 		}
65257c478bd9Sstevel@tonic-gate 	} else { /* CLASSD(dst) */
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate 		for (fire = first_fire;
65287c478bd9Sstevel@tonic-gate 		    fire != NULL;
65297c478bd9Sstevel@tonic-gate 		    fire = fire->ire_next) {
65307c478bd9Sstevel@tonic-gate 
65317c478bd9Sstevel@tonic-gate 			if (!(fire->ire_flags & RTF_MULTIRT))
65327c478bd9Sstevel@tonic-gate 				continue;
65337c478bd9Sstevel@tonic-gate 			if (fire->ire_addr != dst)
65347c478bd9Sstevel@tonic-gate 				continue;
65357c478bd9Sstevel@tonic-gate 
653645916cd2Sjpk 			if (fire->ire_gw_secattr != NULL &&
653745916cd2Sjpk 			    tsol_ire_match_gwattr(fire, tsl) != 0) {
653845916cd2Sjpk 				continue;
653945916cd2Sjpk 			}
654045916cd2Sjpk 
65417c478bd9Sstevel@tonic-gate 			already_resolved = B_FALSE;
65427c478bd9Sstevel@tonic-gate 
65437c478bd9Sstevel@tonic-gate 			gw = fire->ire_gateway_addr;
65447c478bd9Sstevel@tonic-gate 
65457c478bd9Sstevel@tonic-gate 			gw_ire = ire_ftable_lookup(gw, 0, 0, IRE_INTERFACE,
654645916cd2Sjpk 			    NULL, NULL, ALL_ZONES, 0, tsl,
654745916cd2Sjpk 			    MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE |
654845916cd2Sjpk 			    MATCH_IRE_SECATTR);
65497c478bd9Sstevel@tonic-gate 
65507c478bd9Sstevel@tonic-gate 			/* No resolver for the gateway; we skip this ire. */
65517c478bd9Sstevel@tonic-gate 			if (gw_ire == NULL) {
65527c478bd9Sstevel@tonic-gate 				continue;
65537c478bd9Sstevel@tonic-gate 			}
65547c478bd9Sstevel@tonic-gate 
65557c478bd9Sstevel@tonic-gate 			if (first_cire != NULL) {
65567c478bd9Sstevel@tonic-gate 
65577c478bd9Sstevel@tonic-gate 				IRB_REFHOLD(cirb);
65587c478bd9Sstevel@tonic-gate 				/*
65597c478bd9Sstevel@tonic-gate 				 * For all IRE_CACHE ires for that
65607c478bd9Sstevel@tonic-gate 				 * destination.
65617c478bd9Sstevel@tonic-gate 				 */
65627c478bd9Sstevel@tonic-gate 				for (cire = first_cire;
65637c478bd9Sstevel@tonic-gate 				    cire != NULL;
65647c478bd9Sstevel@tonic-gate 				    cire = cire->ire_next) {
65657c478bd9Sstevel@tonic-gate 
65667c478bd9Sstevel@tonic-gate 					if (!(cire->ire_flags & RTF_MULTIRT))
65677c478bd9Sstevel@tonic-gate 						continue;
65687c478bd9Sstevel@tonic-gate 					if (cire->ire_addr != dst)
65697c478bd9Sstevel@tonic-gate 						continue;
65707c478bd9Sstevel@tonic-gate 					if (cire->ire_marks &
65717c478bd9Sstevel@tonic-gate 					    (IRE_MARK_CONDEMNED |
65727c478bd9Sstevel@tonic-gate 						IRE_MARK_HIDDEN))
65737c478bd9Sstevel@tonic-gate 						continue;
65747c478bd9Sstevel@tonic-gate 
657545916cd2Sjpk 					if (cire->ire_gw_secattr != NULL &&
657645916cd2Sjpk 					    tsol_ire_match_gwattr(cire,
657745916cd2Sjpk 					    tsl) != 0) {
657845916cd2Sjpk 						continue;
657945916cd2Sjpk 					}
658045916cd2Sjpk 
65817c478bd9Sstevel@tonic-gate 					/*
65827c478bd9Sstevel@tonic-gate 					 * Cache entries are linked to the
65837c478bd9Sstevel@tonic-gate 					 * parent routes using the parent handle
65847c478bd9Sstevel@tonic-gate 					 * (ire_phandle). If no cache entry has
65857c478bd9Sstevel@tonic-gate 					 * the same handle as fire, fire is
65867c478bd9Sstevel@tonic-gate 					 * still unresolved.
65877c478bd9Sstevel@tonic-gate 					 */
65887c478bd9Sstevel@tonic-gate 					ASSERT(cire->ire_phandle != 0);
65897c478bd9Sstevel@tonic-gate 					if (cire->ire_phandle ==
65907c478bd9Sstevel@tonic-gate 					    fire->ire_phandle) {
65917c478bd9Sstevel@tonic-gate 						already_resolved = B_TRUE;
65927c478bd9Sstevel@tonic-gate 						break;
65937c478bd9Sstevel@tonic-gate 					}
65947c478bd9Sstevel@tonic-gate 				}
65957c478bd9Sstevel@tonic-gate 				IRB_REFRELE(cirb);
65967c478bd9Sstevel@tonic-gate 			}
65977c478bd9Sstevel@tonic-gate 
65987c478bd9Sstevel@tonic-gate 			/*
65997c478bd9Sstevel@tonic-gate 			 * This route is already resolved; proceed with
66007c478bd9Sstevel@tonic-gate 			 * next one.
66017c478bd9Sstevel@tonic-gate 			 */
66027c478bd9Sstevel@tonic-gate 			if (already_resolved) {
66037c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
66047c478bd9Sstevel@tonic-gate 				continue;
66057c478bd9Sstevel@tonic-gate 			}
66067c478bd9Sstevel@tonic-gate 
66077c478bd9Sstevel@tonic-gate 			/*
66087c478bd9Sstevel@tonic-gate 			 * Compute the time elapsed since our preceding
66097c478bd9Sstevel@tonic-gate 			 * attempt to resolve that route.
66107c478bd9Sstevel@tonic-gate 			 * If the MULTIRT_USESTAMP flag is set, we take
66117c478bd9Sstevel@tonic-gate 			 * that route into account only if this time
66127c478bd9Sstevel@tonic-gate 			 * interval exceeds ip_multirt_resolution_interval;
66137c478bd9Sstevel@tonic-gate 			 * this prevents us from attempting to resolve a
66147c478bd9Sstevel@tonic-gate 			 * broken route upon each sending of a packet.
66157c478bd9Sstevel@tonic-gate 			 */
66167c478bd9Sstevel@tonic-gate 			delta = lbolt - fire->ire_last_used_time;
66177c478bd9Sstevel@tonic-gate 			delta = TICK_TO_MSEC(delta);
66187c478bd9Sstevel@tonic-gate 
66197c478bd9Sstevel@tonic-gate 			res = (boolean_t)
66207c478bd9Sstevel@tonic-gate 			    ((delta > ip_multirt_resolution_interval) ||
66217c478bd9Sstevel@tonic-gate 			    (!(flags & MULTIRT_USESTAMP)));
66227c478bd9Sstevel@tonic-gate 
66237c478bd9Sstevel@tonic-gate 			ip3dbg(("ire_multirt_lookup: fire %p, delta %lx, "
66247c478bd9Sstevel@tonic-gate 			    "flags %04x, res %d\n",
66257c478bd9Sstevel@tonic-gate 			    (void *)fire, delta, flags, res));
66267c478bd9Sstevel@tonic-gate 
66277c478bd9Sstevel@tonic-gate 			if (res) {
66287c478bd9Sstevel@tonic-gate 				if (best_cire != NULL) {
66297c478bd9Sstevel@tonic-gate 					/*
66307c478bd9Sstevel@tonic-gate 					 * Release the resolver associated
66317c478bd9Sstevel@tonic-gate 					 * to the preceding candidate best
66327c478bd9Sstevel@tonic-gate 					 * ire, if any.
66337c478bd9Sstevel@tonic-gate 					 */
66347c478bd9Sstevel@tonic-gate 					ire_refrele(best_cire);
66357c478bd9Sstevel@tonic-gate 					ASSERT(best_fire != NULL);
66367c478bd9Sstevel@tonic-gate 				}
66377c478bd9Sstevel@tonic-gate 				best_fire = fire;
66387c478bd9Sstevel@tonic-gate 				best_cire = gw_ire;
66397c478bd9Sstevel@tonic-gate 				continue;
66407c478bd9Sstevel@tonic-gate 			}
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate 			ire_refrele(gw_ire);
66437c478bd9Sstevel@tonic-gate 		}
66447c478bd9Sstevel@tonic-gate 	}
66457c478bd9Sstevel@tonic-gate 
66467c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
66477c478bd9Sstevel@tonic-gate 		IRE_REFHOLD(best_fire);
66487c478bd9Sstevel@tonic-gate 	}
66497c478bd9Sstevel@tonic-gate 	IRB_REFRELE(firb);
66507c478bd9Sstevel@tonic-gate 
66517c478bd9Sstevel@tonic-gate 	/* Release the first IRE_CACHE we initially looked up, if any. */
66527c478bd9Sstevel@tonic-gate 	if (first_cire != NULL)
66537c478bd9Sstevel@tonic-gate 		ire_refrele(first_cire);
66547c478bd9Sstevel@tonic-gate 
66557c478bd9Sstevel@tonic-gate 	/* Found a resolvable route. */
66567c478bd9Sstevel@tonic-gate 	if (best_fire != NULL) {
66577c478bd9Sstevel@tonic-gate 		ASSERT(best_cire != NULL);
66587c478bd9Sstevel@tonic-gate 
66597c478bd9Sstevel@tonic-gate 		if (*fire_arg != NULL)
66607c478bd9Sstevel@tonic-gate 			ire_refrele(*fire_arg);
66617c478bd9Sstevel@tonic-gate 		if (*ire_arg != NULL)
66627c478bd9Sstevel@tonic-gate 			ire_refrele(*ire_arg);
66637c478bd9Sstevel@tonic-gate 
66647c478bd9Sstevel@tonic-gate 		/*
66657c478bd9Sstevel@tonic-gate 		 * Update the passed-in arguments with the
66667c478bd9Sstevel@tonic-gate 		 * resolvable multirt route we found.
66677c478bd9Sstevel@tonic-gate 		 */
66687c478bd9Sstevel@tonic-gate 		*fire_arg = best_fire;
66697c478bd9Sstevel@tonic-gate 		*ire_arg = best_cire;
66707c478bd9Sstevel@tonic-gate 
66717c478bd9Sstevel@tonic-gate 		ip2dbg(("ire_multirt_lookup: returning B_TRUE, "
66727c478bd9Sstevel@tonic-gate 		    "*fire_arg %p, *ire_arg %p\n",
66737c478bd9Sstevel@tonic-gate 		    (void *)best_fire, (void *)best_cire));
66747c478bd9Sstevel@tonic-gate 
66757c478bd9Sstevel@tonic-gate 		return (B_TRUE);
66767c478bd9Sstevel@tonic-gate 	}
66777c478bd9Sstevel@tonic-gate 
66787c478bd9Sstevel@tonic-gate 	ASSERT(best_cire == NULL);
66797c478bd9Sstevel@tonic-gate 
66807c478bd9Sstevel@tonic-gate 	ip2dbg(("ire_multirt_lookup: returning B_FALSE, *fire_arg %p, "
66817c478bd9Sstevel@tonic-gate 	    "*ire_arg %p\n",
66827c478bd9Sstevel@tonic-gate 	    (void *)*fire_arg, (void *)*ire_arg));
66837c478bd9Sstevel@tonic-gate 
66847c478bd9Sstevel@tonic-gate 	/* No resolvable route. */
66857c478bd9Sstevel@tonic-gate 	return (B_FALSE);
66867c478bd9Sstevel@tonic-gate }
66877c478bd9Sstevel@tonic-gate 
66887c478bd9Sstevel@tonic-gate /*
66897c478bd9Sstevel@tonic-gate  * Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
66907c478bd9Sstevel@tonic-gate  * that goes through 'ipif'. As a fallback, a route that goes through
66917c478bd9Sstevel@tonic-gate  * ipif->ipif_ill can be returned.
66927c478bd9Sstevel@tonic-gate  */
66937c478bd9Sstevel@tonic-gate ire_t *
66947c478bd9Sstevel@tonic-gate ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
66957c478bd9Sstevel@tonic-gate {
66967c478bd9Sstevel@tonic-gate 	ire_t	*ire;
66977c478bd9Sstevel@tonic-gate 	ire_t	*save_ire = NULL;
66987c478bd9Sstevel@tonic-gate 	ire_t   *gw_ire;
66997c478bd9Sstevel@tonic-gate 	irb_t   *irb;
67007c478bd9Sstevel@tonic-gate 	ipaddr_t gw_addr;
67017c478bd9Sstevel@tonic-gate 	int	match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
67027c478bd9Sstevel@tonic-gate 
67037c478bd9Sstevel@tonic-gate 	ASSERT(CLASSD(group));
67047c478bd9Sstevel@tonic-gate 
67057c478bd9Sstevel@tonic-gate 	ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
670645916cd2Sjpk 	    NULL, MATCH_IRE_DEFAULT);
67077c478bd9Sstevel@tonic-gate 
67087c478bd9Sstevel@tonic-gate 	if (ire == NULL)
67097c478bd9Sstevel@tonic-gate 		return (NULL);
67107c478bd9Sstevel@tonic-gate 
67117c478bd9Sstevel@tonic-gate 	irb = ire->ire_bucket;
67127c478bd9Sstevel@tonic-gate 	ASSERT(irb);
67137c478bd9Sstevel@tonic-gate 
67147c478bd9Sstevel@tonic-gate 	IRB_REFHOLD(irb);
67157c478bd9Sstevel@tonic-gate 	ire_refrele(ire);
67167c478bd9Sstevel@tonic-gate 	for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
67177c478bd9Sstevel@tonic-gate 		if (ire->ire_addr != group ||
671845916cd2Sjpk 		    (ipif->ipif_zoneid != ire->ire_zoneid &&
671945916cd2Sjpk 		    ire->ire_zoneid != ALL_ZONES)) {
67207c478bd9Sstevel@tonic-gate 			continue;
67217c478bd9Sstevel@tonic-gate 		}
67227c478bd9Sstevel@tonic-gate 
67237c478bd9Sstevel@tonic-gate 		switch (ire->ire_type) {
67247c478bd9Sstevel@tonic-gate 		case IRE_DEFAULT:
67257c478bd9Sstevel@tonic-gate 		case IRE_PREFIX:
67267c478bd9Sstevel@tonic-gate 		case IRE_HOST:
67277c478bd9Sstevel@tonic-gate 			gw_addr = ire->ire_gateway_addr;
67287c478bd9Sstevel@tonic-gate 			gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
672945916cd2Sjpk 			    ipif, NULL, ALL_ZONES, 0, NULL, match_flags);
67307c478bd9Sstevel@tonic-gate 
67317c478bd9Sstevel@tonic-gate 			if (gw_ire != NULL) {
67327c478bd9Sstevel@tonic-gate 				if (save_ire != NULL) {
67337c478bd9Sstevel@tonic-gate 					ire_refrele(save_ire);
67347c478bd9Sstevel@tonic-gate 				}
67357c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
67367c478bd9Sstevel@tonic-gate 				if (gw_ire->ire_ipif == ipif) {
67377c478bd9Sstevel@tonic-gate 					ire_refrele(gw_ire);
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate 					IRB_REFRELE(irb);
67407c478bd9Sstevel@tonic-gate 					return (ire);
67417c478bd9Sstevel@tonic-gate 				}
67427c478bd9Sstevel@tonic-gate 				ire_refrele(gw_ire);
67437c478bd9Sstevel@tonic-gate 				save_ire = ire;
67447c478bd9Sstevel@tonic-gate 			}
67457c478bd9Sstevel@tonic-gate 			break;
67467c478bd9Sstevel@tonic-gate 		case IRE_IF_NORESOLVER:
67477c478bd9Sstevel@tonic-gate 		case IRE_IF_RESOLVER:
67487c478bd9Sstevel@tonic-gate 			if (ire->ire_ipif == ipif) {
67497c478bd9Sstevel@tonic-gate 				if (save_ire != NULL) {
67507c478bd9Sstevel@tonic-gate 					ire_refrele(save_ire);
67517c478bd9Sstevel@tonic-gate 				}
67527c478bd9Sstevel@tonic-gate 				IRE_REFHOLD(ire);
67537c478bd9Sstevel@tonic-gate 
67547c478bd9Sstevel@tonic-gate 				IRB_REFRELE(irb);
67557c478bd9Sstevel@tonic-gate 				return (ire);
67567c478bd9Sstevel@tonic-gate 			}
67577c478bd9Sstevel@tonic-gate 			break;
67587c478bd9Sstevel@tonic-gate 		}
67597c478bd9Sstevel@tonic-gate 	}
67607c478bd9Sstevel@tonic-gate 	IRB_REFRELE(irb);
67617c478bd9Sstevel@tonic-gate 
67627c478bd9Sstevel@tonic-gate 	return (save_ire);
67637c478bd9Sstevel@tonic-gate }
67647c478bd9Sstevel@tonic-gate 
67657c478bd9Sstevel@tonic-gate /*
67667c478bd9Sstevel@tonic-gate  * The purpose of the next two functions is to provide some external access to
67677c478bd9Sstevel@tonic-gate  * routing/l2 lookup functionality while hiding the implementation of routing
67687c478bd9Sstevel@tonic-gate  * and interface data structures (IRE/ILL).  Thus, interfaces are passed/
67697c478bd9Sstevel@tonic-gate  * returned by name instead of by ILL reference.  These functions are used by
67707c478bd9Sstevel@tonic-gate  * IP Filter.
67717c478bd9Sstevel@tonic-gate  * Return a link layer header suitable for an IP packet being sent to the
67727c478bd9Sstevel@tonic-gate  * dst_addr IP address.  The interface associated with the route is put into
67737c478bd9Sstevel@tonic-gate  * ifname, which must be a buffer of LIFNAMSIZ bytes.  The dst_addr is the
67747c478bd9Sstevel@tonic-gate  * packet's ultimate destination address, not a router address.
67757c478bd9Sstevel@tonic-gate  *
67767c478bd9Sstevel@tonic-gate  * This function is used when the caller wants to know the outbound interface
67777c478bd9Sstevel@tonic-gate  * and MAC header for a packet given only the address.
67787c478bd9Sstevel@tonic-gate  */
67797c478bd9Sstevel@tonic-gate mblk_t *
67807c478bd9Sstevel@tonic-gate ip_nexthop_route(const struct sockaddr *target, char *ifname)
67817c478bd9Sstevel@tonic-gate {
67827663b816Sml37995 	struct nce_s *nce;
678387c3980eSdr146992 	ire_t *dir;
67847c478bd9Sstevel@tonic-gate 	ill_t *ill;
67857c478bd9Sstevel@tonic-gate 	mblk_t *mp;
67867c478bd9Sstevel@tonic-gate 
67877c478bd9Sstevel@tonic-gate 	/* parameter sanity */
67887c478bd9Sstevel@tonic-gate 	if (ifname == NULL || target == NULL)
67897c478bd9Sstevel@tonic-gate 		return (NULL);
67907c478bd9Sstevel@tonic-gate 
67917c478bd9Sstevel@tonic-gate 	/* Find the route entry, if it exists. */
67927c478bd9Sstevel@tonic-gate 	switch (target->sa_family) {
67937c478bd9Sstevel@tonic-gate 	case AF_INET:
67947c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup(
67957c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)target)->sin_addr.s_addr,
67967c478bd9Sstevel@tonic-gate 		    0xffffffff,
679745916cd2Sjpk 		    0, 0, NULL, NULL, ALL_ZONES, NULL,
67987c478bd9Sstevel@tonic-gate 		    MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE);
67997c478bd9Sstevel@tonic-gate 		break;
68007c478bd9Sstevel@tonic-gate 	case AF_INET6:
68017c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup_v6(
68027c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in6 *)target)->sin6_addr,
68037c478bd9Sstevel@tonic-gate 		    NULL,
680445916cd2Sjpk 		    0, 0, NULL, NULL, ALL_ZONES, NULL,
68057c478bd9Sstevel@tonic-gate 		    MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE);
68067663b816Sml37995 		if ((dir != NULL) && (dir->ire_nce == NULL)) {
68077663b816Sml37995 			ire_refrele(dir);
68087663b816Sml37995 			dir = NULL;
68097663b816Sml37995 		}
68107c478bd9Sstevel@tonic-gate 		break;
68117c478bd9Sstevel@tonic-gate 	default:
68127c478bd9Sstevel@tonic-gate 		dir = NULL;
68137c478bd9Sstevel@tonic-gate 		break;
68147c478bd9Sstevel@tonic-gate 	}
68157c478bd9Sstevel@tonic-gate 
68167c478bd9Sstevel@tonic-gate 
68177c478bd9Sstevel@tonic-gate 	if (dir == NULL)
68187c478bd9Sstevel@tonic-gate 		return (NULL);
68197c478bd9Sstevel@tonic-gate 
68207c478bd9Sstevel@tonic-gate 	/* Map the IRE to an ILL so we can fill in ifname. */
68217c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(dir);
68227c478bd9Sstevel@tonic-gate 	if (ill == NULL) {
68237c478bd9Sstevel@tonic-gate 		ire_refrele(dir);
68247c478bd9Sstevel@tonic-gate 		return (NULL);
68257c478bd9Sstevel@tonic-gate 	}
68267c478bd9Sstevel@tonic-gate 	(void) strncpy(ifname, ill->ill_name, LIFNAMSIZ);
68277c478bd9Sstevel@tonic-gate 
68287c478bd9Sstevel@tonic-gate 	/* Return a copy of the header to the caller. */
68297663b816Sml37995 	switch (target->sa_family) {
68307663b816Sml37995 	case AF_INET :
68317c478bd9Sstevel@tonic-gate 		if (dir->ire_fp_mp != NULL) {
68327c478bd9Sstevel@tonic-gate 			if ((mp = dupb(dir->ire_fp_mp)) == NULL)
68337c478bd9Sstevel@tonic-gate 				mp = copyb(dir->ire_fp_mp);
68347c478bd9Sstevel@tonic-gate 		} else if (dir->ire_dlureq_mp != NULL) {
68357c478bd9Sstevel@tonic-gate 			if ((mp = dupb(dir->ire_dlureq_mp)) == NULL)
68367c478bd9Sstevel@tonic-gate 				mp = copyb(dir->ire_dlureq_mp);
68377c478bd9Sstevel@tonic-gate 		} else {
68387c478bd9Sstevel@tonic-gate 			mp = NULL;
68397c478bd9Sstevel@tonic-gate 		}
68407663b816Sml37995 		break;
68417663b816Sml37995 	case AF_INET6 :
68427663b816Sml37995 		nce = dir->ire_nce;
68437663b816Sml37995 		if (nce->nce_fp_mp != NULL) {
68447663b816Sml37995 			if ((mp = dupb(nce->nce_fp_mp)) == NULL)
68457663b816Sml37995 				mp = copyb(nce->nce_fp_mp);
68467663b816Sml37995 		} else if (nce->nce_res_mp != NULL) {
68477663b816Sml37995 			if ((mp = dupb(nce->nce_res_mp)) == NULL)
68487663b816Sml37995 				mp = copyb(nce->nce_res_mp);
68497663b816Sml37995 		} else {
68507663b816Sml37995 			mp = NULL;
68517663b816Sml37995 		}
68527663b816Sml37995 		break;
68537663b816Sml37995 	}
68547c478bd9Sstevel@tonic-gate 
68557c478bd9Sstevel@tonic-gate 	ire_refrele(dir);
68567c478bd9Sstevel@tonic-gate 	return (mp);
68577c478bd9Sstevel@tonic-gate }
68587c478bd9Sstevel@tonic-gate 
68597c478bd9Sstevel@tonic-gate 
68607c478bd9Sstevel@tonic-gate /*
68617c478bd9Sstevel@tonic-gate  * Return a link layer header suitable for an IP packet being sent to the
68627c478bd9Sstevel@tonic-gate  * dst_addr IP address on the specified output interface.  The dst_addr
68637c478bd9Sstevel@tonic-gate  * may be the packet's ultimate destination or a predetermined next hop
68647c478bd9Sstevel@tonic-gate  * router's address.
68657c478bd9Sstevel@tonic-gate  * ifname must be nul-terminated.
68667c478bd9Sstevel@tonic-gate  *
68677c478bd9Sstevel@tonic-gate  * This function is used when the caller knows the outbound interface (usually
68687c478bd9Sstevel@tonic-gate  * because it was specified by policy) and only needs the MAC header for a
68697c478bd9Sstevel@tonic-gate  * packet.
68707c478bd9Sstevel@tonic-gate  */
68717c478bd9Sstevel@tonic-gate mblk_t *
68727c478bd9Sstevel@tonic-gate ip_nexthop(const struct sockaddr *target, const char *ifname)
68737c478bd9Sstevel@tonic-gate {
68747663b816Sml37995 	struct nce_s *nce;
68757c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
68767c478bd9Sstevel@tonic-gate 	t_uscalar_t sap;
687787c3980eSdr146992 	ire_t *dir;
68787c478bd9Sstevel@tonic-gate 	ill_t *ill;
68797c478bd9Sstevel@tonic-gate 	mblk_t *mp;
68807c478bd9Sstevel@tonic-gate 
68817c478bd9Sstevel@tonic-gate 	/* parameter sanity */
68827c478bd9Sstevel@tonic-gate 	if (ifname == NULL || target == NULL)
68837c478bd9Sstevel@tonic-gate 		return (NULL);
68847c478bd9Sstevel@tonic-gate 
68857c478bd9Sstevel@tonic-gate 	switch (target->sa_family) {
68867c478bd9Sstevel@tonic-gate 	case AF_INET :
68877c478bd9Sstevel@tonic-gate 		sap = IP_DL_SAP;
68887c478bd9Sstevel@tonic-gate 		break;
68897c478bd9Sstevel@tonic-gate 	case AF_INET6 :
68907c478bd9Sstevel@tonic-gate 		sap = IP6_DL_SAP;
68917c478bd9Sstevel@tonic-gate 		break;
68927c478bd9Sstevel@tonic-gate 	default:
68937c478bd9Sstevel@tonic-gate 		return (NULL);
68947c478bd9Sstevel@tonic-gate 	}
68957c478bd9Sstevel@tonic-gate 
68967c478bd9Sstevel@tonic-gate 	/* Lock ill_g_lock before walking through the list */
68977c478bd9Sstevel@tonic-gate 	rw_enter(&ill_g_lock, RW_READER);
68987c478bd9Sstevel@tonic-gate 	/*
68997c478bd9Sstevel@tonic-gate 	 * Can we find the interface name among those currently configured?
69007c478bd9Sstevel@tonic-gate 	 */
69017c478bd9Sstevel@tonic-gate 	for (ill = ILL_START_WALK_ALL(&ctx); ill != NULL;
69027c478bd9Sstevel@tonic-gate 	    ill = ill_next(&ctx, ill)) {
69037c478bd9Sstevel@tonic-gate 		if ((strcmp(ifname, ill->ill_name) == 0) &&
69047c478bd9Sstevel@tonic-gate 		    (ill->ill_sap == sap))
69057c478bd9Sstevel@tonic-gate 			break;
69067c478bd9Sstevel@tonic-gate 	}
69077c478bd9Sstevel@tonic-gate 	if (ill == NULL || ill->ill_ipif == NULL) {
69087c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
69097c478bd9Sstevel@tonic-gate 		return (NULL);
69107c478bd9Sstevel@tonic-gate 	}
69117c478bd9Sstevel@tonic-gate 
69127c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
69137c478bd9Sstevel@tonic-gate 	if (!ILL_CAN_LOOKUP(ill)) {
69147c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
69157c478bd9Sstevel@tonic-gate 		rw_exit(&ill_g_lock);
69167c478bd9Sstevel@tonic-gate 		return (NULL);
69177c478bd9Sstevel@tonic-gate 	}
69187c478bd9Sstevel@tonic-gate 	ill_refhold_locked(ill);
69197c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
69207c478bd9Sstevel@tonic-gate 	rw_exit(&ill_g_lock);
69217c478bd9Sstevel@tonic-gate 
69227c478bd9Sstevel@tonic-gate 	/* Find the resolver entry, if it exists. */
69237c478bd9Sstevel@tonic-gate 	switch (target->sa_family) {
69247c478bd9Sstevel@tonic-gate 	case AF_INET:
69257c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup(
69267c478bd9Sstevel@tonic-gate 			((struct sockaddr_in *)target)->sin_addr.s_addr,
69277c478bd9Sstevel@tonic-gate 			0xffffffff,
692845916cd2Sjpk 			0, 0, ill->ill_ipif, NULL, ALL_ZONES, NULL,
69297c478bd9Sstevel@tonic-gate 			MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|
69307c478bd9Sstevel@tonic-gate 			MATCH_IRE_RECURSIVE|MATCH_IRE_IPIF);
69317c478bd9Sstevel@tonic-gate 		break;
69327c478bd9Sstevel@tonic-gate 	case AF_INET6:
69337c478bd9Sstevel@tonic-gate 		dir = ire_route_lookup_v6(
69347c478bd9Sstevel@tonic-gate 			&((struct sockaddr_in6 *)target)->sin6_addr, NULL,
693545916cd2Sjpk 			0, 0, ill->ill_ipif, NULL, ALL_ZONES, NULL,
69367c478bd9Sstevel@tonic-gate 			MATCH_IRE_DSTONLY|MATCH_IRE_DEFAULT|
69377c478bd9Sstevel@tonic-gate 			MATCH_IRE_RECURSIVE|MATCH_IRE_IPIF);
69387663b816Sml37995 		if ((dir != NULL) && (dir->ire_nce == NULL)) {
69397663b816Sml37995 			ire_refrele(dir);
69407663b816Sml37995 			dir = NULL;
69417663b816Sml37995 		}
69427c478bd9Sstevel@tonic-gate 		break;
69437c478bd9Sstevel@tonic-gate 	default:
69447c478bd9Sstevel@tonic-gate 		dir = NULL;
69457c478bd9Sstevel@tonic-gate 		break;
69467c478bd9Sstevel@tonic-gate 	}
69477c478bd9Sstevel@tonic-gate 
69487c478bd9Sstevel@tonic-gate 	ill_refrele(ill);
69497c478bd9Sstevel@tonic-gate 
69507c478bd9Sstevel@tonic-gate 	if (dir == NULL)
69517c478bd9Sstevel@tonic-gate 		return (NULL);
69527c478bd9Sstevel@tonic-gate 
69537c478bd9Sstevel@tonic-gate 	/* Return a copy of the header to the caller. */
69547663b816Sml37995 	switch (target->sa_family) {
69557663b816Sml37995 	case AF_INET :
69567c478bd9Sstevel@tonic-gate 		if (dir->ire_fp_mp != NULL) {
69577c478bd9Sstevel@tonic-gate 			if ((mp = dupb(dir->ire_fp_mp)) == NULL)
69587c478bd9Sstevel@tonic-gate 				mp = copyb(dir->ire_fp_mp);
69597c478bd9Sstevel@tonic-gate 		} else if (dir->ire_dlureq_mp != NULL) {
69607c478bd9Sstevel@tonic-gate 			if ((mp = dupb(dir->ire_dlureq_mp)) == NULL)
69617c478bd9Sstevel@tonic-gate 				mp = copyb(dir->ire_dlureq_mp);
69627c478bd9Sstevel@tonic-gate 		} else {
69637c478bd9Sstevel@tonic-gate 			mp = NULL;
69647c478bd9Sstevel@tonic-gate 		}
69657663b816Sml37995 		break;
69667663b816Sml37995 	case AF_INET6 :
69677663b816Sml37995 		nce = dir->ire_nce;
69687663b816Sml37995 		if (nce->nce_fp_mp != NULL) {
69697663b816Sml37995 			if ((mp = dupb(nce->nce_fp_mp)) == NULL)
69707663b816Sml37995 				mp = copyb(nce->nce_fp_mp);
69717663b816Sml37995 		} else if (nce->nce_res_mp != NULL) {
69727663b816Sml37995 			if ((mp = dupb(nce->nce_res_mp)) == NULL)
69737663b816Sml37995 				mp = copyb(nce->nce_res_mp);
69747663b816Sml37995 		} else {
69757663b816Sml37995 			mp = NULL;
69767663b816Sml37995 		}
69777663b816Sml37995 		break;
69787663b816Sml37995 	}
69797c478bd9Sstevel@tonic-gate 
69807c478bd9Sstevel@tonic-gate 	ire_refrele(dir);
69817c478bd9Sstevel@tonic-gate 	return (mp);
69827c478bd9Sstevel@tonic-gate }
69837c478bd9Sstevel@tonic-gate 
69847c478bd9Sstevel@tonic-gate /*
69857c478bd9Sstevel@tonic-gate  * IRE iterator for inbound and loopback broadcast processing.
69867c478bd9Sstevel@tonic-gate  * Given an IRE_BROADCAST ire, walk the ires with the same destination
69877c478bd9Sstevel@tonic-gate  * address, but skip over the passed-in ire. Returns the next ire without
69887c478bd9Sstevel@tonic-gate  * a hold - assumes that the caller holds a reference on the IRE bucket.
69897c478bd9Sstevel@tonic-gate  */
69907c478bd9Sstevel@tonic-gate ire_t *
69917c478bd9Sstevel@tonic-gate ire_get_next_bcast_ire(ire_t *curr, ire_t *ire)
69927c478bd9Sstevel@tonic-gate {
69937c478bd9Sstevel@tonic-gate 	ill_t *ill;
69947c478bd9Sstevel@tonic-gate 
69957c478bd9Sstevel@tonic-gate 	if (curr == NULL) {
69967c478bd9Sstevel@tonic-gate 		for (curr = ire->ire_bucket->irb_ire; curr != NULL;
69977c478bd9Sstevel@tonic-gate 		    curr = curr->ire_next) {
69987c478bd9Sstevel@tonic-gate 			if (curr->ire_addr == ire->ire_addr)
69997c478bd9Sstevel@tonic-gate 				break;
70007c478bd9Sstevel@tonic-gate 		}
70017c478bd9Sstevel@tonic-gate 	} else {
70027c478bd9Sstevel@tonic-gate 		curr = curr->ire_next;
70037c478bd9Sstevel@tonic-gate 	}
70047c478bd9Sstevel@tonic-gate 	ill = ire_to_ill(ire);
70057c478bd9Sstevel@tonic-gate 	for (; curr != NULL; curr = curr->ire_next) {
70067c478bd9Sstevel@tonic-gate 		if (curr->ire_addr != ire->ire_addr) {
70077c478bd9Sstevel@tonic-gate 			/*
70087c478bd9Sstevel@tonic-gate 			 * All the IREs to a given destination are contiguous;
70097c478bd9Sstevel@tonic-gate 			 * break out once the address doesn't match.
70107c478bd9Sstevel@tonic-gate 			 */
70117c478bd9Sstevel@tonic-gate 			break;
70127c478bd9Sstevel@tonic-gate 		}
70137c478bd9Sstevel@tonic-gate 		if (curr == ire) {
70147c478bd9Sstevel@tonic-gate 			/* skip over the passed-in ire */
70157c478bd9Sstevel@tonic-gate 			continue;
70167c478bd9Sstevel@tonic-gate 		}
70177c478bd9Sstevel@tonic-gate 		if ((curr->ire_stq != NULL && ire->ire_stq == NULL) ||
70187c478bd9Sstevel@tonic-gate 		    (curr->ire_stq == NULL && ire->ire_stq != NULL)) {
70197c478bd9Sstevel@tonic-gate 			/*
70207c478bd9Sstevel@tonic-gate 			 * If the passed-in ire is loopback, skip over
70217c478bd9Sstevel@tonic-gate 			 * non-loopback ires and vice versa.
70227c478bd9Sstevel@tonic-gate 			 */
70237c478bd9Sstevel@tonic-gate 			continue;
70247c478bd9Sstevel@tonic-gate 		}
70257c478bd9Sstevel@tonic-gate 		if (ire_to_ill(curr) != ill) {
70267c478bd9Sstevel@tonic-gate 			/* skip over IREs going through a different interface */
70277c478bd9Sstevel@tonic-gate 			continue;
70287c478bd9Sstevel@tonic-gate 		}
70297c478bd9Sstevel@tonic-gate 		if (curr->ire_marks & IRE_MARK_CONDEMNED) {
70307c478bd9Sstevel@tonic-gate 			/* skip over deleted IREs */
70317c478bd9Sstevel@tonic-gate 			continue;
70327c478bd9Sstevel@tonic-gate 		}
70337c478bd9Sstevel@tonic-gate 		return (curr);
70347c478bd9Sstevel@tonic-gate 	}
70357c478bd9Sstevel@tonic-gate 	return (NULL);
70367c478bd9Sstevel@tonic-gate }
70377c478bd9Sstevel@tonic-gate 
70387c478bd9Sstevel@tonic-gate /*
70397c478bd9Sstevel@tonic-gate  * IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
70407c478bd9Sstevel@tonic-gate  * routes. Given a starting point in the hash list (ire_origin), walk the IREs
70417c478bd9Sstevel@tonic-gate  * in the bucket skipping default interface routes and deleted entries.
70427c478bd9Sstevel@tonic-gate  * Returns the next IRE (unheld), or NULL when we're back to the starting point.
70437c478bd9Sstevel@tonic-gate  * Assumes that the caller holds a reference on the IRE bucket.
70447c478bd9Sstevel@tonic-gate  */
70457c478bd9Sstevel@tonic-gate ire_t *
70467c478bd9Sstevel@tonic-gate ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
70477c478bd9Sstevel@tonic-gate {
70487c478bd9Sstevel@tonic-gate 	ASSERT(ire_origin->ire_bucket != NULL);
70497c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
70507c478bd9Sstevel@tonic-gate 
70517c478bd9Sstevel@tonic-gate 	do {
70527c478bd9Sstevel@tonic-gate 		ire = ire->ire_next;
70537c478bd9Sstevel@tonic-gate 		if (ire == NULL)
70547c478bd9Sstevel@tonic-gate 			ire = ire_origin->ire_bucket->irb_ire;
70557c478bd9Sstevel@tonic-gate 		if (ire == ire_origin)
70567c478bd9Sstevel@tonic-gate 			return (NULL);
70577c478bd9Sstevel@tonic-gate 	} while ((ire->ire_type & IRE_INTERFACE) ||
70587c478bd9Sstevel@tonic-gate 	    (ire->ire_marks & IRE_MARK_CONDEMNED));
70597c478bd9Sstevel@tonic-gate 	ASSERT(ire != NULL);
70607c478bd9Sstevel@tonic-gate 	return (ire);
70617c478bd9Sstevel@tonic-gate }
70627c478bd9Sstevel@tonic-gate 
70637c478bd9Sstevel@tonic-gate #ifdef IRE_DEBUG
70647c478bd9Sstevel@tonic-gate th_trace_t *
70657c478bd9Sstevel@tonic-gate th_trace_ire_lookup(ire_t *ire)
70667c478bd9Sstevel@tonic-gate {
70677c478bd9Sstevel@tonic-gate 	int bucket_id;
70687c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
70697c478bd9Sstevel@tonic-gate 
70707c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ire->ire_lock));
70717c478bd9Sstevel@tonic-gate 
70727c478bd9Sstevel@tonic-gate 	bucket_id = IP_TR_HASH(curthread);
70737c478bd9Sstevel@tonic-gate 	ASSERT(bucket_id < IP_TR_HASH_MAX);
70747c478bd9Sstevel@tonic-gate 
70757c478bd9Sstevel@tonic-gate 	for (th_trace = ire->ire_trace[bucket_id]; th_trace != NULL;
70767c478bd9Sstevel@tonic-gate 	    th_trace = th_trace->th_next) {
70777c478bd9Sstevel@tonic-gate 		if (th_trace->th_id == curthread)
70787c478bd9Sstevel@tonic-gate 			return (th_trace);
70797c478bd9Sstevel@tonic-gate 	}
70807c478bd9Sstevel@tonic-gate 	return (NULL);
70817c478bd9Sstevel@tonic-gate }
70827c478bd9Sstevel@tonic-gate 
70837c478bd9Sstevel@tonic-gate void
70847c478bd9Sstevel@tonic-gate ire_trace_ref(ire_t *ire)
70857c478bd9Sstevel@tonic-gate {
70867c478bd9Sstevel@tonic-gate 	int bucket_id;
70877c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
70887c478bd9Sstevel@tonic-gate 
70897c478bd9Sstevel@tonic-gate 	/*
70907c478bd9Sstevel@tonic-gate 	 * Attempt to locate the trace buffer for the curthread.
70917c478bd9Sstevel@tonic-gate 	 * If it does not exist, then allocate a new trace buffer
70927c478bd9Sstevel@tonic-gate 	 * and link it in list of trace bufs for this ipif, at the head
70937c478bd9Sstevel@tonic-gate 	 */
70947c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
70957c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
70967c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
70977c478bd9Sstevel@tonic-gate 		return;
70987c478bd9Sstevel@tonic-gate 	}
70997c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
71007c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
71017c478bd9Sstevel@tonic-gate 		bucket_id = IP_TR_HASH(curthread);
71027c478bd9Sstevel@tonic-gate 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
71037c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
71047c478bd9Sstevel@tonic-gate 		if (th_trace == NULL) {
71057c478bd9Sstevel@tonic-gate 			ire->ire_trace_disable = B_TRUE;
71067c478bd9Sstevel@tonic-gate 			mutex_exit(&ire->ire_lock);
71077c478bd9Sstevel@tonic-gate 			ire_trace_inactive(ire);
71087c478bd9Sstevel@tonic-gate 			return;
71097c478bd9Sstevel@tonic-gate 		}
71107c478bd9Sstevel@tonic-gate 
71117c478bd9Sstevel@tonic-gate 		th_trace->th_id = curthread;
71127c478bd9Sstevel@tonic-gate 		th_trace->th_next = ire->ire_trace[bucket_id];
71137c478bd9Sstevel@tonic-gate 		th_trace->th_prev = &ire->ire_trace[bucket_id];
71147c478bd9Sstevel@tonic-gate 		if (th_trace->th_next != NULL)
71157c478bd9Sstevel@tonic-gate 			th_trace->th_next->th_prev = &th_trace->th_next;
71167c478bd9Sstevel@tonic-gate 		ire->ire_trace[bucket_id] = th_trace;
71177c478bd9Sstevel@tonic-gate 	}
71187c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt < TR_BUF_MAX - 1);
71197c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt++;
71207c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
71217c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
71227c478bd9Sstevel@tonic-gate }
71237c478bd9Sstevel@tonic-gate 
71247c478bd9Sstevel@tonic-gate void
71257c478bd9Sstevel@tonic-gate ire_trace_free(th_trace_t *th_trace)
71267c478bd9Sstevel@tonic-gate {
71277c478bd9Sstevel@tonic-gate 	/* unlink th_trace and free it */
71287c478bd9Sstevel@tonic-gate 	*th_trace->th_prev = th_trace->th_next;
71297c478bd9Sstevel@tonic-gate 	if (th_trace->th_next != NULL)
71307c478bd9Sstevel@tonic-gate 		th_trace->th_next->th_prev = th_trace->th_prev;
71317c478bd9Sstevel@tonic-gate 	th_trace->th_next = NULL;
71327c478bd9Sstevel@tonic-gate 	th_trace->th_prev = NULL;
71337c478bd9Sstevel@tonic-gate 	kmem_free(th_trace, sizeof (th_trace_t));
71347c478bd9Sstevel@tonic-gate }
71357c478bd9Sstevel@tonic-gate 
71367c478bd9Sstevel@tonic-gate void
71377c478bd9Sstevel@tonic-gate ire_untrace_ref(ire_t *ire)
71387c478bd9Sstevel@tonic-gate {
71397c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
71407c478bd9Sstevel@tonic-gate 
71417c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
71427c478bd9Sstevel@tonic-gate 
71437c478bd9Sstevel@tonic-gate 	if (ire->ire_trace_disable == B_TRUE) {
71447c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
71457c478bd9Sstevel@tonic-gate 		return;
71467c478bd9Sstevel@tonic-gate 	}
71477c478bd9Sstevel@tonic-gate 
71487c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
71497c478bd9Sstevel@tonic-gate 	ASSERT(th_trace != NULL && th_trace->th_refcnt > 0);
71507c478bd9Sstevel@tonic-gate 	th_trace_rrecord(th_trace);
71517c478bd9Sstevel@tonic-gate 	th_trace->th_refcnt--;
71527c478bd9Sstevel@tonic-gate 
71537c478bd9Sstevel@tonic-gate 	if (th_trace->th_refcnt == 0)
71547c478bd9Sstevel@tonic-gate 		ire_trace_free(th_trace);
71557c478bd9Sstevel@tonic-gate 
71567c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
71577c478bd9Sstevel@tonic-gate }
71587c478bd9Sstevel@tonic-gate 
71597c478bd9Sstevel@tonic-gate static void
71607c478bd9Sstevel@tonic-gate ire_trace_inactive(ire_t *ire)
71617c478bd9Sstevel@tonic-gate {
71627c478bd9Sstevel@tonic-gate 	th_trace_t *th_trace;
71637c478bd9Sstevel@tonic-gate 	int i;
71647c478bd9Sstevel@tonic-gate 
71657c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
71667c478bd9Sstevel@tonic-gate 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
71677c478bd9Sstevel@tonic-gate 		while (ire->ire_trace[i] != NULL) {
71687c478bd9Sstevel@tonic-gate 			th_trace = ire->ire_trace[i];
71697c478bd9Sstevel@tonic-gate 
71707c478bd9Sstevel@tonic-gate 			/* unlink th_trace and free it */
71717c478bd9Sstevel@tonic-gate 			ire->ire_trace[i] = th_trace->th_next;
71727c478bd9Sstevel@tonic-gate 			if (th_trace->th_next != NULL)
71737c478bd9Sstevel@tonic-gate 				th_trace->th_next->th_prev =
71747c478bd9Sstevel@tonic-gate 				    &ire->ire_trace[i];
71757c478bd9Sstevel@tonic-gate 
71767c478bd9Sstevel@tonic-gate 			th_trace->th_next = NULL;
71777c478bd9Sstevel@tonic-gate 			th_trace->th_prev = NULL;
71787c478bd9Sstevel@tonic-gate 			kmem_free(th_trace, sizeof (th_trace_t));
71797c478bd9Sstevel@tonic-gate 		}
71807c478bd9Sstevel@tonic-gate 	}
71817c478bd9Sstevel@tonic-gate 
71827c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
71837c478bd9Sstevel@tonic-gate }
71847c478bd9Sstevel@tonic-gate 
71857c478bd9Sstevel@tonic-gate /* ARGSUSED */
71867c478bd9Sstevel@tonic-gate void
71877c478bd9Sstevel@tonic-gate ire_thread_exit(ire_t *ire, caddr_t arg)
71887c478bd9Sstevel@tonic-gate {
71897c478bd9Sstevel@tonic-gate 	th_trace_t	*th_trace;
71907c478bd9Sstevel@tonic-gate 
71917c478bd9Sstevel@tonic-gate 	mutex_enter(&ire->ire_lock);
71927c478bd9Sstevel@tonic-gate 	th_trace = th_trace_ire_lookup(ire);
71937c478bd9Sstevel@tonic-gate 	if (th_trace == NULL) {
71947c478bd9Sstevel@tonic-gate 		mutex_exit(&ire->ire_lock);
71957c478bd9Sstevel@tonic-gate 		return;
71967c478bd9Sstevel@tonic-gate 	}
71977c478bd9Sstevel@tonic-gate 	ASSERT(th_trace->th_refcnt == 0);
71987c478bd9Sstevel@tonic-gate 
71997c478bd9Sstevel@tonic-gate 	ire_trace_free(th_trace);
72007c478bd9Sstevel@tonic-gate 	mutex_exit(&ire->ire_lock);
72017c478bd9Sstevel@tonic-gate }
72027c478bd9Sstevel@tonic-gate 
72037c478bd9Sstevel@tonic-gate #endif
7204